blob: 194b8f721c568a5be2e427e31ca3ec4512100a9d [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;
54import android.os.Handler;
yinxu504e1392017-04-12 16:03:22 -070055import android.os.IBinder;
tom hsu0b59d292022-09-29 23:49:21 +080056import android.os.LocaleList;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070057import android.os.Looper;
58import android.os.Message;
yinxu504e1392017-04-12 16:03:22 -070059import android.os.Messenger;
Hall Liua1548bd2019-12-24 14:14:12 -080060import android.os.ParcelFileDescriptor;
Malcolm Chen6ca97372019-07-01 16:28:21 -070061import android.os.ParcelUuid;
Tyler Gunn65d45c22017-06-05 11:22:26 -070062import android.os.PersistableBundle;
Shuo Qiancd19c462020-01-16 20:51:11 -080063import android.os.Process;
Brad Ebinger5f64b052017-12-14 14:26:15 -080064import android.os.RemoteException;
Adam Lesinski903a54c2016-04-11 14:49:52 -070065import android.os.ResultReceiver;
Brad Ebinger1ce9c432019-07-16 13:19:44 -070066import android.os.ServiceSpecificException;
Rambo Wang0f050d82021-02-12 11:43:36 -080067import android.os.SystemClock;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070068import android.os.UserHandle;
Stuart Scott981d8582015-04-21 14:09:50 -070069import android.os.UserManager;
Sooraj Sasindran9a909312016-07-18 11:57:25 -070070import android.os.WorkSource;
Derek Tan97ebb422014-09-05 16:55:38 -070071import android.preference.PreferenceManager;
Naina Nallurid63128d2019-09-17 14:10:30 -070072import android.provider.DeviceConfig;
Ihab Awadf2177b72013-11-25 13:33:23 -080073import android.provider.Settings;
Amit Mahajan7dbbd822019-03-13 17:33:47 -070074import android.provider.Telephony;
Inseob Kim14bb3d02018-12-13 17:11:34 +090075import android.sysprop.TelephonyProperties;
Santos Cordon7a1885b2015-02-03 11:15:19 -080076import android.telecom.PhoneAccount;
Nancy Chen31f9ba12016-01-06 11:42:12 -080077import android.telecom.PhoneAccountHandle;
Andrew Lee9431b832015-03-09 18:46:45 -070078import android.telecom.TelecomManager;
Gary Jian3aa9a762022-01-24 16:41:19 +080079import android.telephony.AccessNetworkConstants;
80import android.telephony.ActivityStatsTechSpecificInfo;
Chen Xu227e06f2019-09-26 22:48:11 -070081import android.telephony.Annotation.ApnType;
Jack Yu0eda6842022-04-18 00:34:46 -070082import android.telephony.Annotation.DataActivityType;
Jack Nudelmanb0b87642020-11-12 15:04:39 -080083import android.telephony.Annotation.ThermalMitigationResult;
Sarah Chin2ec39f62022-08-31 17:03:26 -070084import android.telephony.AnomalyReporter;
Shuo Qian4a594052020-01-23 11:59:30 -080085import android.telephony.CallForwardingInfo;
Junda Liu12f7d802015-05-01 12:06:44 -070086import android.telephony.CarrierConfigManager;
Michele Berionne482f8202018-11-27 18:57:59 -080087import android.telephony.CarrierRestrictionRules;
Hui Wang9b5793a2022-12-05 14:38:06 -060088import android.telephony.CellBroadcastIdRange;
yincheng zhao2737e882019-09-06 17:06:54 -070089import android.telephony.CellIdentity;
Meng Wanga10e89e2019-12-09 13:13:01 -080090import android.telephony.CellIdentityCdma;
91import android.telephony.CellIdentityGsm;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070092import android.telephony.CellInfo;
Nathan Haroldf180aac2018-06-01 18:43:55 -070093import android.telephony.CellInfoGsm;
94import android.telephony.CellInfoWcdma;
Sooraj Sasindran9a909312016-07-18 11:57:25 -070095import android.telephony.ClientRequestStats;
Jack Nudelmanb0b87642020-11-12 15:04:39 -080096import android.telephony.DataThrottlingRequest;
Hui Wang641e81c2020-10-12 12:14:23 -070097import android.telephony.IBootstrapAuthenticationCallback;
Nathan Haroldfa8da0f2018-09-27 18:51:29 -070098import android.telephony.ICellInfoCallback;
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -070099import android.telephony.IccOpenLogicalChannelResponse;
Hall Liu1aa510f2017-11-22 17:40:08 -0800100import android.telephony.LocationAccessPolicy;
Ta-wei Yen87c49842016-05-13 21:19:52 -0700101import android.telephony.ModemActivityInfo;
Jake Hambye994d462014-02-03 13:10:13 -0800102import android.telephony.NeighboringCellInfo;
yinxu504e1392017-04-12 16:03:22 -0700103import android.telephony.NetworkScanRequest;
Michele4245e952019-02-04 11:36:23 -0800104import android.telephony.PhoneCapability;
Hall Liud892bec2018-11-30 14:51:45 -0800105import android.telephony.PhoneNumberRange;
Wink Saville5d475dd2014-10-17 15:00:58 -0700106import android.telephony.RadioAccessFamily;
Hall Liub2ac8ef2019-02-28 15:56:23 -0800107import android.telephony.RadioAccessSpecifier;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700108import android.telephony.ServiceState;
Nathan Harold46b42aa2017-03-10 19:38:22 -0800109import android.telephony.SignalStrength;
Rambo Wanga5cc9b72021-01-07 10:51:54 -0800110import android.telephony.SignalStrengthUpdateRequest;
111import android.telephony.SignalThresholdInfo;
Wink Saville0f3b5fc2014-11-11 08:40:49 -0800112import android.telephony.SubscriptionInfo;
Jeff Sharkey85190e62014-12-05 09:40:12 -0800113import android.telephony.SubscriptionManager;
Peter Wangc035ce42020-01-08 21:00:22 -0800114import android.telephony.TelephonyFrameworkInitializer;
Sanket Padawe99ef1e32016-05-18 16:12:33 -0700115import android.telephony.TelephonyHistogram;
Ta-wei Yenb6929602016-05-24 15:48:27 -0700116import android.telephony.TelephonyManager;
Jack Yuf5badd92022-12-08 00:50:53 -0800117import android.telephony.TelephonyManager.SimState;
Hall Liub2ac8ef2019-02-28 15:56:23 -0800118import android.telephony.TelephonyScanManager;
Jack Nudelmanb0b87642020-11-12 15:04:39 -0800119import android.telephony.ThermalMitigationRequest;
Jordan Liu5aa07002018-12-18 15:44:48 -0800120import android.telephony.UiccCardInfo;
sandeepjsb6c87872021-09-27 15:34:44 +0000121import android.telephony.UiccPortInfo;
Holly Jiuyu Sun01c47ad2018-01-24 17:56:33 +0000122import android.telephony.UiccSlotInfo;
sandeepjsb6c87872021-09-27 15:34:44 +0000123import android.telephony.UiccSlotMapping;
Tyler Gunn65d45c22017-06-05 11:22:26 -0700124import android.telephony.UssdResponse;
Ta-wei Yenb6929602016-05-24 15:48:27 -0700125import android.telephony.VisualVoicemailSmsFilterSettings;
Hongbo Zeng0e18b162021-04-07 16:52:18 +0800126import android.telephony.data.NetworkSlicingConfig;
Jack Yub5d8f642018-11-26 11:20:48 -0800127import android.telephony.emergency.EmergencyNumber;
Hui Wang641e81c2020-10-12 12:14:23 -0700128import android.telephony.gba.GbaAuthRequest;
129import android.telephony.gba.UaSecurityProtocolIdentifier;
Brad Ebinger1ce9c432019-07-16 13:19:44 -0700130import android.telephony.ims.ImsException;
Brad Ebinger1c8542e2019-01-14 13:43:14 -0800131import android.telephony.ims.ProvisioningManager;
Hui Wang761a6682020-10-31 05:12:53 +0000132import android.telephony.ims.RcsClientConfiguration;
Brad Ebinger14d467f2021-02-12 06:18:28 +0000133import android.telephony.ims.RcsContactUceCapability;
Brad Ebingera34a6c22019-10-22 17:36:18 -0700134import android.telephony.ims.RegistrationManager;
joonhunshincffb7fc2021-11-28 07:32:01 +0000135import android.telephony.ims.aidl.IFeatureProvisioningCallback;
Brad Ebinger35c841c2018-10-01 10:40:55 -0700136import android.telephony.ims.aidl.IImsCapabilityCallback;
Brad Ebinger22bc3e42018-01-16 09:39:35 -0800137import android.telephony.ims.aidl.IImsConfig;
Brad Ebingerdf5b4f02018-10-31 11:24:17 -0700138import android.telephony.ims.aidl.IImsConfigCallback;
Brad Ebinger22bc3e42018-01-16 09:39:35 -0800139import android.telephony.ims.aidl.IImsRegistration;
Brad Ebinger35c841c2018-10-01 10:40:55 -0700140import android.telephony.ims.aidl.IImsRegistrationCallback;
Hui Wang761a6682020-10-31 05:12:53 +0000141import android.telephony.ims.aidl.IRcsConfigCallback;
Brad Ebingerbc7dd582019-10-17 17:03:22 -0700142import android.telephony.ims.feature.ImsFeature;
Brad Ebinger1c8542e2019-01-14 13:43:14 -0800143import android.telephony.ims.stub.ImsConfigImplBase;
Brad Ebinger1f2b5082018-02-08 16:11:32 -0800144import android.telephony.ims.stub.ImsRegistrationImplBase;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700145import android.text.TextUtils;
Jeff Sharkey85190e62014-12-05 09:40:12 -0800146import android.util.ArraySet;
Hall Liud60acc92020-05-21 17:09:35 -0700147import android.util.EventLog;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700148import android.util.Log;
Jake Hambye994d462014-02-03 13:10:13 -0800149import android.util.Pair;
Ta-wei Yen30a69c82016-12-27 14:52:32 -0800150
Andrew Lee312e8172014-10-23 17:01:36 -0700151import com.android.ims.ImsManager;
Brad Ebinger34bef922017-11-09 10:27:08 -0800152import com.android.ims.internal.IImsServiceFeatureCallback;
James.cf Linbcdf8b32021-01-14 16:44:13 +0800153import com.android.ims.rcs.uce.eab.EabUtil;
SongFerngWangfd89b102021-05-27 22:44:54 +0800154import com.android.internal.annotations.VisibleForTesting;
Shuo Qian4a594052020-01-23 11:59:30 -0800155import com.android.internal.telephony.CallForwardInfo;
Shishir Agrawal566b7612013-10-28 14:41:00 -0700156import com.android.internal.telephony.CallManager;
Tyler Gunn52dcf772017-04-26 11:30:31 -0700157import com.android.internal.telephony.CallStateException;
Tyler Gunnd4339262021-05-03 14:46:49 -0700158import com.android.internal.telephony.CallTracker;
Rambo Wang9c9ffdd2022-01-13 21:51:44 -0800159import com.android.internal.telephony.CarrierPrivilegesTracker;
chen xu651eec72018-11-11 19:03:44 -0800160import com.android.internal.telephony.CarrierResolver;
Shishir Agrawal302c8692015-06-19 13:49:39 -0700161import com.android.internal.telephony.CellNetworkScanResult;
Shishir Agrawal566b7612013-10-28 14:41:00 -0700162import com.android.internal.telephony.CommandException;
Shuo Qian4a594052020-01-23 11:59:30 -0800163import com.android.internal.telephony.CommandsInterface;
Hui Wang641e81c2020-10-12 12:14:23 -0700164import com.android.internal.telephony.GbaManager;
Shuo Qianccbaf742021-02-22 18:32:21 -0800165import com.android.internal.telephony.GsmCdmaPhone;
Nathan Harold48d6fd52019-02-06 19:01:40 -0800166import com.android.internal.telephony.HalVersion;
Hall Liu73f5d362020-01-20 13:42:00 -0800167import com.android.internal.telephony.IBooleanConsumer;
Hall Liu27d24262020-09-18 19:04:59 -0700168import com.android.internal.telephony.ICallForwardingInfoCallback;
Hunsuk Choi3b742d62021-10-25 19:48:34 +0000169import com.android.internal.telephony.IImsStateCallback;
Brad Ebingera63db5f2019-04-23 16:31:13 -0700170import com.android.internal.telephony.IIntegerConsumer;
Hall Liud892bec2018-11-30 14:51:45 -0800171import com.android.internal.telephony.INumberVerificationCallback;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700172import com.android.internal.telephony.ITelephony;
Jake Hambye994d462014-02-03 13:10:13 -0800173import com.android.internal.telephony.IccCard;
Jack Yuf5badd92022-12-08 00:50:53 -0800174import com.android.internal.telephony.IccCardConstants;
Rambo Wanga1782702021-11-10 20:15:19 -0800175import com.android.internal.telephony.IccLogicalChannelRequest;
Jack Yu5f7092c2018-04-13 14:05:37 -0700176import com.android.internal.telephony.LocaleTracker;
yinxub1bed742017-04-17 11:45:04 -0700177import com.android.internal.telephony.NetworkScanRequestTracker;
Shishir Agrawal302c8692015-06-19 13:49:39 -0700178import com.android.internal.telephony.OperatorInfo;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700179import com.android.internal.telephony.Phone;
Malcolm Chen2c63d402018-08-14 16:00:53 -0700180import com.android.internal.telephony.PhoneConfigurationManager;
Nathan Harolda667c152016-12-14 11:27:20 -0800181import com.android.internal.telephony.PhoneConstantConversions;
Ta-wei Yen87c49842016-05-13 21:19:52 -0700182import com.android.internal.telephony.PhoneConstants;
Wink Saville36469e72014-06-11 15:17:00 -0700183import com.android.internal.telephony.PhoneFactory;
Wink Saville5d475dd2014-10-17 15:00:58 -0700184import com.android.internal.telephony.ProxyController;
Sanket Padawe99ef1e32016-05-18 16:12:33 -0700185import com.android.internal.telephony.RIL;
SongFerngWang8c6e82e2021-03-02 22:09:29 +0800186import com.android.internal.telephony.RILConstants;
Daniel Bright94f43662021-03-01 14:43:40 -0800187import com.android.internal.telephony.RadioInterfaceCapabilityController;
Jack Yu5f7092c2018-04-13 14:05:37 -0700188import com.android.internal.telephony.ServiceStateTracker;
Aishwarya Mallampatifbc70d32022-11-10 20:33:02 +0000189import com.android.internal.telephony.SmsApplication;
Amit Mahajandccb3f12019-05-13 13:48:32 -0700190import com.android.internal.telephony.SmsController;
Brad Ebingera63db5f2019-04-23 16:31:13 -0700191import com.android.internal.telephony.SmsPermissions;
Wink Savilleac1bdfd2014-11-20 23:04:44 -0800192import com.android.internal.telephony.SubscriptionController;
Peter Wang59571be2020-01-27 12:35:15 +0800193import com.android.internal.telephony.TelephonyIntents;
Jeff Davidson7e17e312018-02-13 18:17:36 -0800194import com.android.internal.telephony.TelephonyPermissions;
Jack Yu27422a52022-03-21 10:38:05 -0700195import com.android.internal.telephony.data.DataUtils;
Hunsuk Choi42cc62a2022-10-16 06:03:40 +0000196import com.android.internal.telephony.domainselection.DomainSelectionResolver;
sqianf4ca7ed2019-01-15 18:32:07 -0800197import com.android.internal.telephony.emergency.EmergencyNumberTracker;
Derek Tan740e1672017-06-27 14:56:27 -0700198import com.android.internal.telephony.euicc.EuiccConnector;
Brad Ebinger9c0eb502019-01-23 15:06:19 -0800199import com.android.internal.telephony.ims.ImsResolver;
Tyler Gunn7bcdc742019-10-04 15:56:59 -0700200import com.android.internal.telephony.imsphone.ImsPhone;
201import com.android.internal.telephony.imsphone.ImsPhoneCallTracker;
joonhunshin3e154242021-09-17 06:33:39 +0000202import com.android.internal.telephony.metrics.RcsStats;
Pengquan Meng6c2dc9f2019-02-06 11:12:53 -0800203import com.android.internal.telephony.metrics.TelephonyMetrics;
Jack Yu285100e2022-12-02 22:48:35 -0800204import com.android.internal.telephony.subscription.SubscriptionInfoInternal;
205import com.android.internal.telephony.subscription.SubscriptionManagerService;
Meng Wangafbc5852019-09-19 17:37:13 -0700206import com.android.internal.telephony.uicc.IccCardApplicationStatus.AppType;
Shishir Agrawal566b7612013-10-28 14:41:00 -0700207import com.android.internal.telephony.uicc.IccIoResult;
208import com.android.internal.telephony.uicc.IccUtils;
Nathan Haroldb3014052017-01-25 15:57:32 -0800209import com.android.internal.telephony.uicc.SIMRecords;
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700210import com.android.internal.telephony.uicc.UiccCard;
Nathan Haroldb3014052017-01-25 15:57:32 -0800211import com.android.internal.telephony.uicc.UiccCardApplication;
Shishir Agrawal566b7612013-10-28 14:41:00 -0700212import com.android.internal.telephony.uicc.UiccController;
Muralidhar Reddy472c2ae2021-09-29 19:38:40 +0000213import com.android.internal.telephony.uicc.UiccPort;
Jeff Davidson7e17e312018-02-13 18:17:36 -0800214import com.android.internal.telephony.uicc.UiccProfile;
Holly Jiuyu Sun01c47ad2018-01-24 17:56:33 +0000215import com.android.internal.telephony.uicc.UiccSlot;
zoey chenc730df82019-12-18 17:07:20 +0800216import com.android.internal.telephony.util.LocaleUtils;
Aishwarya Mallampatifbc70d32022-11-10 20:33:02 +0000217import com.android.internal.telephony.util.TelephonyUtils;
fionaxu7ed723d2017-05-30 18:58:54 -0700218import com.android.internal.telephony.util.VoicemailNotificationSettingsUtil;
Hall Liu27d24262020-09-18 19:04:59 -0700219import com.android.internal.util.FunctionalUtils;
Jake Hambye994d462014-02-03 13:10:13 -0800220import com.android.internal.util.HexDump;
Hall Liuaa4211e2021-01-20 15:43:39 -0800221import com.android.phone.callcomposer.CallComposerPictureManager;
222import com.android.phone.callcomposer.CallComposerPictureTransfer;
223import com.android.phone.callcomposer.ImageData;
Brad Ebingera63db5f2019-04-23 16:31:13 -0700224import com.android.phone.settings.PickSmsSubscriptionActivity;
Sarah Chin46355ba2022-11-01 23:51:16 -0700225import com.android.phone.slice.SlicePurchaseController;
Ta-wei Yenc9df0432017-04-17 17:09:07 -0700226import com.android.phone.vvm.PhoneAccountHandleConverter;
Ta-wei Yen527a9c02017-01-06 15:29:25 -0800227import com.android.phone.vvm.RemoteVvmTaskManager;
Ta-wei Yenc9df0432017-04-17 17:09:07 -0700228import com.android.phone.vvm.VisualVoicemailSettingsUtil;
Ta-wei Yenc8905312017-03-28 11:14:45 -0700229import com.android.phone.vvm.VisualVoicemailSmsFilterConfig;
Jack Nudelmanb0b87642020-11-12 15:04:39 -0800230import com.android.services.telephony.TelecomAccountRegistry;
231import com.android.services.telephony.TelephonyConnectionService;
Peter Wang44b186e2020-01-13 23:33:09 -0800232import com.android.telephony.Rlog;
Ta-wei Yen30a69c82016-12-27 14:52:32 -0800233
Hall Liu82694d52020-12-11 18:22:04 -0800234import java.io.ByteArrayOutputStream;
Ta-wei Yenc236d6b2016-06-21 13:33:12 -0700235import java.io.FileDescriptor;
Hall Liu82694d52020-12-11 18:22:04 -0800236import java.io.IOException;
237import java.io.InputStream;
Ta-wei Yenc236d6b2016-06-21 13:33:12 -0700238import java.io.PrintWriter;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700239import java.util.ArrayList;
Jeff Sharkey85190e62014-12-05 09:40:12 -0800240import java.util.Arrays;
Muralidhar Reddyeb809e32021-11-19 03:07:54 +0000241import java.util.Collection;
Hunter Knepshieldcad7f0b2021-12-13 15:08:35 -0800242import java.util.Collections;
sqian11b7a0e2018-12-05 18:48:28 -0800243import java.util.HashMap;
sqianf4ca7ed2019-01-15 18:32:07 -0800244import java.util.HashSet;
Jake Hambye994d462014-02-03 13:10:13 -0800245import java.util.List;
Narayan Kamath1c496c22015-04-16 14:40:19 +0100246import java.util.Locale;
Jeff Sharkey85190e62014-12-05 09:40:12 -0800247import java.util.Map;
Nazanin Bakhshif71371d2019-04-29 17:29:44 -0700248import java.util.NoSuchElementException;
Hall Liu82694d52020-12-11 18:22:04 -0800249import java.util.Objects;
sqianf4ca7ed2019-01-15 18:32:07 -0800250import java.util.Set;
Sarah Chin2ec39f62022-08-31 17:03:26 -0700251import java.util.UUID;
Hall Liu82694d52020-12-11 18:22:04 -0800252import java.util.concurrent.Executors;
Peter Wangdafb9ac2020-01-15 14:13:38 -0800253import java.util.concurrent.atomic.AtomicBoolean;
Hall Liu73f5d362020-01-20 13:42:00 -0800254import java.util.function.Consumer;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700255
256/**
257 * Implementation of the ITelephony interface.
258 */
Santos Cordon117fee72014-05-16 17:56:12 -0700259public class PhoneInterfaceManager extends ITelephony.Stub {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700260 private static final String LOG_TAG = "PhoneInterfaceManager";
261 private static final boolean DBG = (PhoneGlobals.DBG_LEVEL >= 2);
262 private static final boolean DBG_LOC = false;
Jeff Sharkey85190e62014-12-05 09:40:12 -0800263 private static final boolean DBG_MERGE = false;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700264
265 // Message codes used with mMainThreadHandler
266 private static final int CMD_HANDLE_PIN_MMI = 1;
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700267 private static final int CMD_TRANSMIT_APDU_LOGICAL_CHANNEL = 7;
268 private static final int EVENT_TRANSMIT_APDU_LOGICAL_CHANNEL_DONE = 8;
Shishir Agrawal566b7612013-10-28 14:41:00 -0700269 private static final int CMD_OPEN_CHANNEL = 9;
270 private static final int EVENT_OPEN_CHANNEL_DONE = 10;
271 private static final int CMD_CLOSE_CHANNEL = 11;
272 private static final int EVENT_CLOSE_CHANNEL_DONE = 12;
Jake Hambye994d462014-02-03 13:10:13 -0800273 private static final int CMD_NV_READ_ITEM = 13;
274 private static final int EVENT_NV_READ_ITEM_DONE = 14;
275 private static final int CMD_NV_WRITE_ITEM = 15;
276 private static final int EVENT_NV_WRITE_ITEM_DONE = 16;
277 private static final int CMD_NV_WRITE_CDMA_PRL = 17;
278 private static final int EVENT_NV_WRITE_CDMA_PRL_DONE = 18;
chen xu6dac5ab2018-10-26 17:39:23 -0700279 private static final int CMD_RESET_MODEM_CONFIG = 19;
280 private static final int EVENT_RESET_MODEM_CONFIG_DONE = 20;
SongFerngWang3ef3e072020-12-21 16:41:52 +0800281 private static final int CMD_GET_ALLOWED_NETWORK_TYPES_BITMASK = 21;
282 private static final int EVENT_GET_ALLOWED_NETWORK_TYPES_BITMASK_DONE = 22;
Sailesh Nepal35b59452014-03-06 09:26:56 -0800283 private static final int CMD_SEND_ENVELOPE = 25;
284 private static final int EVENT_SEND_ENVELOPE_DONE = 26;
Shuo Qian850e4d6a2018-04-25 21:02:08 +0000285 private static final int CMD_INVOKE_OEM_RIL_REQUEST_RAW = 27;
286 private static final int EVENT_INVOKE_OEM_RIL_REQUEST_RAW_DONE = 28;
Derek Tan6b088ee2014-09-05 14:15:18 -0700287 private static final int CMD_TRANSMIT_APDU_BASIC_CHANNEL = 29;
288 private static final int EVENT_TRANSMIT_APDU_BASIC_CHANNEL_DONE = 30;
289 private static final int CMD_EXCHANGE_SIM_IO = 31;
290 private static final int EVENT_EXCHANGE_SIM_IO_DONE = 32;
Shishir Agrawal76d5da92014-11-09 16:17:25 -0800291 private static final int CMD_SET_VOICEMAIL_NUMBER = 33;
292 private static final int EVENT_SET_VOICEMAIL_NUMBER_DONE = 34;
Stuart Scott54788802015-03-30 13:18:01 -0700293 private static final int CMD_SET_NETWORK_SELECTION_MODE_AUTOMATIC = 35;
294 private static final int EVENT_SET_NETWORK_SELECTION_MODE_AUTOMATIC_DONE = 36;
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -0700295 private static final int CMD_GET_MODEM_ACTIVITY_INFO = 37;
296 private static final int EVENT_GET_MODEM_ACTIVITY_INFO_DONE = 38;
Shishir Agrawal302c8692015-06-19 13:49:39 -0700297 private static final int CMD_PERFORM_NETWORK_SCAN = 39;
298 private static final int EVENT_PERFORM_NETWORK_SCAN_DONE = 40;
299 private static final int CMD_SET_NETWORK_SELECTION_MODE_MANUAL = 41;
300 private static final int EVENT_SET_NETWORK_SELECTION_MODE_MANUAL_DONE = 42;
Meng Wang1a7c35a2016-05-05 20:56:15 -0700301 private static final int CMD_SET_ALLOWED_CARRIERS = 43;
302 private static final int EVENT_SET_ALLOWED_CARRIERS_DONE = 44;
303 private static final int CMD_GET_ALLOWED_CARRIERS = 45;
304 private static final int EVENT_GET_ALLOWED_CARRIERS_DONE = 46;
pkanwar32d516d2016-10-14 19:37:38 -0700305 private static final int CMD_HANDLE_USSD_REQUEST = 47;
Nathan Haroldb3014052017-01-25 15:57:32 -0800306 private static final int CMD_GET_FORBIDDEN_PLMNS = 48;
307 private static final int EVENT_GET_FORBIDDEN_PLMNS_DONE = 49;
Holly Jiuyu Sun01c47ad2018-01-24 17:56:33 +0000308 private static final int CMD_SWITCH_SLOTS = 50;
309 private static final int EVENT_SWITCH_SLOTS_DONE = 51;
Pengquan Menga1bb6272018-09-06 09:59:22 -0700310 private static final int CMD_GET_NETWORK_SELECTION_MODE = 52;
311 private static final int EVENT_GET_NETWORK_SELECTION_MODE_DONE = 53;
312 private static final int CMD_GET_CDMA_ROAMING_MODE = 54;
313 private static final int EVENT_GET_CDMA_ROAMING_MODE_DONE = 55;
314 private static final int CMD_SET_CDMA_ROAMING_MODE = 56;
315 private static final int EVENT_SET_CDMA_ROAMING_MODE_DONE = 57;
316 private static final int CMD_SET_CDMA_SUBSCRIPTION_MODE = 58;
317 private static final int EVENT_SET_CDMA_SUBSCRIPTION_MODE_DONE = 59;
Nathan Harold3ff88932018-08-14 10:19:49 -0700318 private static final int CMD_GET_ALL_CELL_INFO = 60;
319 private static final int EVENT_GET_ALL_CELL_INFO_DONE = 61;
320 private static final int CMD_GET_CELL_LOCATION = 62;
321 private static final int EVENT_GET_CELL_LOCATION_DONE = 63;
chen xu6dac5ab2018-10-26 17:39:23 -0700322 private static final int CMD_MODEM_REBOOT = 64;
323 private static final int EVENT_CMD_MODEM_REBOOT_DONE = 65;
Nathan Haroldfa8da0f2018-09-27 18:51:29 -0700324 private static final int CMD_REQUEST_CELL_INFO_UPDATE = 66;
325 private static final int EVENT_REQUEST_CELL_INFO_UPDATE_DONE = 67;
Malcolm Chen8e4ed912019-01-15 20:22:16 -0800326 private static final int CMD_REQUEST_ENABLE_MODEM = 68;
327 private static final int EVENT_ENABLE_MODEM_DONE = 69;
Nazanin Bakhshif71371d2019-04-29 17:29:44 -0700328 private static final int CMD_GET_MODEM_STATUS = 70;
329 private static final int EVENT_GET_MODEM_STATUS_DONE = 71;
yincheng zhao2737e882019-09-06 17:06:54 -0700330 private static final int CMD_SET_FORBIDDEN_PLMNS = 72;
331 private static final int EVENT_SET_FORBIDDEN_PLMNS_DONE = 73;
Naina Nallurid63128d2019-09-17 14:10:30 -0700332 private static final int CMD_ERASE_MODEM_CONFIG = 74;
333 private static final int EVENT_ERASE_MODEM_CONFIG_DONE = 75;
zoey chene02881a2019-12-30 16:11:23 +0800334 private static final int CMD_CHANGE_ICC_LOCK_PASSWORD = 76;
335 private static final int EVENT_CHANGE_ICC_LOCK_PASSWORD_DONE = 77;
336 private static final int CMD_SET_ICC_LOCK_ENABLED = 78;
337 private static final int EVENT_SET_ICC_LOCK_ENABLED_DONE = 79;
Hall Liu73f5d362020-01-20 13:42:00 -0800338 private static final int CMD_SET_SYSTEM_SELECTION_CHANNELS = 80;
339 private static final int EVENT_SET_SYSTEM_SELECTION_CHANNELS_DONE = 81;
Peter Wangdafb9ac2020-01-15 14:13:38 -0800340 private static final int MSG_NOTIFY_USER_ACTIVITY = 82;
Shuo Qian4a594052020-01-23 11:59:30 -0800341 private static final int CMD_GET_CALL_FORWARDING = 83;
342 private static final int EVENT_GET_CALL_FORWARDING_DONE = 84;
343 private static final int CMD_SET_CALL_FORWARDING = 85;
344 private static final int EVENT_SET_CALL_FORWARDING_DONE = 86;
345 private static final int CMD_GET_CALL_WAITING = 87;
346 private static final int EVENT_GET_CALL_WAITING_DONE = 88;
347 private static final int CMD_SET_CALL_WAITING = 89;
348 private static final int EVENT_SET_CALL_WAITING_DONE = 90;
Sooraj Sasindran37444802020-08-11 10:40:43 -0700349 private static final int CMD_ENABLE_NR_DUAL_CONNECTIVITY = 91;
350 private static final int EVENT_ENABLE_NR_DUAL_CONNECTIVITY_DONE = 92;
351 private static final int CMD_IS_NR_DUAL_CONNECTIVITY_ENABLED = 93;
352 private static final int EVENT_IS_NR_DUAL_CONNECTIVITY_ENABLED_DONE = 94;
Sarah Chinbaab1432020-10-28 13:46:24 -0700353 private static final int CMD_GET_CDMA_SUBSCRIPTION_MODE = 95;
354 private static final int EVENT_GET_CDMA_SUBSCRIPTION_MODE_DONE = 96;
Sarah Chin679c08a2020-11-18 13:39:35 -0800355 private static final int CMD_GET_SYSTEM_SELECTION_CHANNELS = 97;
356 private static final int EVENT_GET_SYSTEM_SELECTION_CHANNELS_DONE = 98;
Jack Nudelmanb0b87642020-11-12 15:04:39 -0800357 private static final int CMD_SET_DATA_THROTTLING = 99;
358 private static final int EVENT_SET_DATA_THROTTLING_DONE = 100;
Jordan Liu109698e2020-11-24 14:50:34 -0800359 private static final int CMD_SET_SIM_POWER = 101;
360 private static final int EVENT_SET_SIM_POWER_DONE = 102;
Rambo Wanga5cc9b72021-01-07 10:51:54 -0800361 private static final int CMD_SET_SIGNAL_STRENGTH_UPDATE_REQUEST = 103;
362 private static final int EVENT_SET_SIGNAL_STRENGTH_UPDATE_REQUEST_DONE = 104;
363 private static final int CMD_CLEAR_SIGNAL_STRENGTH_UPDATE_REQUEST = 105;
364 private static final int EVENT_CLEAR_SIGNAL_STRENGTH_UPDATE_REQUEST_DONE = 106;
SongFerngWang3ef3e072020-12-21 16:41:52 +0800365 private static final int CMD_SET_ALLOWED_NETWORK_TYPES_FOR_REASON = 107;
366 private static final int EVENT_SET_ALLOWED_NETWORK_TYPES_FOR_REASON_DONE = 108;
Michele Berionne5e411512020-11-13 02:36:59 +0000367 private static final int CMD_PREPARE_UNATTENDED_REBOOT = 109;
Hongbo Zeng156aa4a2021-02-08 21:50:28 +0800368 private static final int CMD_GET_SLICING_CONFIG = 110;
369 private static final int EVENT_GET_SLICING_CONFIG_DONE = 111;
Kai Shif70f46f2021-03-03 13:59:46 -0800370 private static final int CMD_ERASE_DATA_SHARED_PREFERENCES = 112;
Sooraj Sasindrandaf060f2021-06-15 14:52:55 -0700371 private static final int CMD_ENABLE_VONR = 113;
372 private static final int EVENT_ENABLE_VONR_DONE = 114;
373 private static final int CMD_IS_VONR_ENABLED = 115;
374 private static final int EVENT_IS_VONR_ENABLED_DONE = 116;
Sarah Chin2ec39f62022-08-31 17:03:26 -0700375 private static final int CMD_PURCHASE_PREMIUM_CAPABILITY = 117;
376 private static final int EVENT_PURCHASE_PREMIUM_CAPABILITY_DONE = 118;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700377
Holly Jiuyu Sun1cc2d552018-01-26 15:51:16 -0800378 // Parameters of select command.
379 private static final int SELECT_COMMAND = 0xA4;
380 private static final int SELECT_P1 = 0x04;
381 private static final int SELECT_P2 = 0;
382 private static final int SELECT_P3 = 0x10;
383
Gil Cukierman1c0eb932022-12-06 22:28:24 +0000384 // Toggling null cipher and integrity support was added in IRadioNetwork 2.1
385 private static final int MIN_NULL_CIPHER_AND_INTEGRITY_VERSION = 201;
386
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700387 /** The singleton instance. */
388 private static PhoneInterfaceManager sInstance;
Jack Nudelman644b91a2021-03-12 14:09:48 -0800389 private static List<String> sThermalMitigationAllowlistedPackages = new ArrayList<>();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700390
Wink Saville3ab207e2014-11-20 13:07:20 -0800391 private PhoneGlobals mApp;
Wink Saville3ab207e2014-11-20 13:07:20 -0800392 private CallManager mCM;
Brad Ebinger24c29992019-12-05 13:03:21 -0800393 private ImsResolver mImsResolver;
Stuart Scott981d8582015-04-21 14:09:50 -0700394 private UserManager mUserManager;
Wink Saville3ab207e2014-11-20 13:07:20 -0800395 private AppOpsManager mAppOps;
Grace Jia0ddb3612021-04-22 13:35:26 -0700396 private PackageManager mPm;
Wink Saville3ab207e2014-11-20 13:07:20 -0800397 private MainThreadHandler mMainThreadHandler;
Aishwarya Mallampati32336e82023-01-04 23:02:56 +0000398 private SubscriptionController mSubscriptionController;
Wink Saville3ab207e2014-11-20 13:07:20 -0800399 private SharedPreferences mTelephonySharedPreferences;
Malcolm Chen2c63d402018-08-14 16:00:53 -0700400 private PhoneConfigurationManager mPhoneConfigurationManager;
Daniel Bright94f43662021-03-01 14:43:40 -0800401 private final RadioInterfaceCapabilityController mRadioInterfaceCapabilities;
Gil Cukierman6dac5eb2022-09-19 16:09:04 +0000402 private final Telephony2gUpdater mTelephony2gUpdater;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700403
Peter Wangdafb9ac2020-01-15 14:13:38 -0800404 /** User Activity */
405 private AtomicBoolean mNotifyUserActivity;
Peter Wangdafb9ac2020-01-15 14:13:38 -0800406 private static final int USER_ACTIVITY_NOTIFICATION_DELAY = 200;
407
Jeff Davidson8ab02b22020-03-28 12:24:40 -0700408 private Set<Integer> mCarrierPrivilegeTestOverrideSubIds = new ArraySet<>();
409
Derek Tan97ebb422014-09-05 16:55:38 -0700410 private static final String PREF_CARRIERS_ALPHATAG_PREFIX = "carrier_alphtag_";
411 private static final String PREF_CARRIERS_NUMBER_PREFIX = "carrier_number_";
Jeff Sharkey85190e62014-12-05 09:40:12 -0800412 private static final String PREF_CARRIERS_SUBSCRIBER_PREFIX = "carrier_subscriber_";
Brad Ebinger1c8542e2019-01-14 13:43:14 -0800413 private static final String PREF_PROVISION_IMS_MMTEL_PREFIX = "provision_ims_mmtel_";
Derek Tan89e89d42014-07-08 17:00:10 -0700414
Michelecea4cf22018-12-21 15:00:11 -0800415 // String to store multi SIM allowed
416 private static final String PREF_MULTI_SIM_RESTRICTED = "multisim_restricted";
417
Derek Tan740e1672017-06-27 14:56:27 -0700418 // The AID of ISD-R.
419 private static final String ISDR_AID = "A0000005591010FFFFFFFF8900000100";
420
yinxub1bed742017-04-17 11:45:04 -0700421 private NetworkScanRequestTracker mNetworkScanRequestTracker;
422
David Kelly5e06a7f2018-03-12 14:10:59 +0000423 private static final int TYPE_ALLOCATION_CODE_LENGTH = 8;
424 private static final int MANUFACTURER_CODE_LENGTH = 8;
425
Jack Nudelmanb0b87642020-11-12 15:04:39 -0800426 private static final int SET_DATA_THROTTLING_MODEM_THREW_INVALID_PARAMS = -1;
Jack Nudelman5d6a98b2021-03-04 14:26:25 -0800427 private static final int MODEM_DOES_NOT_SUPPORT_DATA_THROTTLING_ERROR_CODE = -2;
Jack Nudelmanb0b87642020-11-12 15:04:39 -0800428
Sarah Chin2ec39f62022-08-31 17:03:26 -0700429 private static final String PURCHASE_PREMIUM_CAPABILITY_ERROR_UUID =
430 "24bf97a6-e8a6-44d8-a6a4-255d7548733c";
431
Derek Tan89e89d42014-07-08 17:00:10 -0700432 /**
Naina Nallurid63128d2019-09-17 14:10:30 -0700433 * Experiment flag to enable erase modem config on reset network, default value is false
434 */
435 public static final String RESET_NETWORK_ERASE_MODEM_CONFIG_ENABLED =
436 "reset_network_erase_modem_config_enabled";
437
Rambo Wang0f050d82021-02-12 11:43:36 -0800438 private static final int SET_NETWORK_SELECTION_MODE_AUTOMATIC_TIMEOUT_MS = 2000; // 2 seconds
Chen Xu540470b2021-12-14 17:15:47 -0800439
sandeepjsb6c87872021-09-27 15:34:44 +0000440 /**
441 * With support for MEP(multiple enabled profile) in Android T, a SIM card can have more than
442 * one ICCID active at the same time.
443 * Apps should use below API signatures if targeting SDK is T and beyond.
444 *
445 * @hide
446 */
447 @ChangeId
448 @EnabledSince(targetSdkVersion = Build.VERSION_CODES.TIRAMISU)
449 public static final long GET_API_SIGNATURES_FROM_UICC_PORT_INFO = 202110963L;
Rambo Wang0f050d82021-02-12 11:43:36 -0800450
Naina Nallurid63128d2019-09-17 14:10:30 -0700451 /**
Chen Xu540470b2021-12-14 17:15:47 -0800452 * Apps targeting on Android T and beyond will get exception whenever icc close channel
453 * operation fails.
454 */
455 @ChangeId
456 @EnabledSince(targetSdkVersion = Build.VERSION_CODES.TIRAMISU)
457 public static final long ICC_CLOSE_CHANNEL_EXCEPTION_ON_FAILURE = 208739934L;
458
459 /**
Shishir Agrawal566b7612013-10-28 14:41:00 -0700460 * A request object to use for transmitting data to an ICC.
461 */
462 private static final class IccAPDUArgument {
463 public int channel, cla, command, p1, p2, p3;
464 public String data;
465
466 public IccAPDUArgument(int channel, int cla, int command,
467 int p1, int p2, int p3, String data) {
468 this.channel = channel;
469 this.cla = cla;
470 this.command = command;
471 this.p1 = p1;
472 this.p2 = p2;
473 this.p3 = p3;
474 this.data = data;
475 }
476 }
477
478 /**
Shishir Agrawal77ba3172015-09-10 14:50:19 -0700479 * A request object to use for transmitting data to an ICC.
480 */
481 private static final class ManualNetworkSelectionArgument {
482 public OperatorInfo operatorInfo;
483 public boolean persistSelection;
484
485 public ManualNetworkSelectionArgument(OperatorInfo operatorInfo, boolean persistSelection) {
486 this.operatorInfo = operatorInfo;
487 this.persistSelection = persistSelection;
488 }
489 }
490
Sarah Chin71b3a852022-09-28 15:54:19 -0700491 private static final class PurchasePremiumCapabilityArgument {
492 public @TelephonyManager.PremiumCapability int capability;
Sarah Chin71b3a852022-09-28 15:54:19 -0700493 public @NonNull IIntegerConsumer callback;
494
495 PurchasePremiumCapabilityArgument(@TelephonyManager.PremiumCapability int capability,
Sarah Chinb8218c22023-01-04 13:35:29 -0800496 @NonNull IIntegerConsumer callback) {
Sarah Chin71b3a852022-09-28 15:54:19 -0700497 this.capability = capability;
Sarah Chin71b3a852022-09-28 15:54:19 -0700498 this.callback = callback;
499 }
500 }
501
Shishir Agrawal77ba3172015-09-10 14:50:19 -0700502 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700503 * A request object for use with {@link MainThreadHandler}. Requesters should wait() on the
504 * request after sending. The main thread will notify the request when it is complete.
505 */
506 private static final class MainThreadRequest {
507 /** The argument to use for the request */
508 public Object argument;
509 /** The result of the request that is run on the main thread */
510 public Object result;
Sanket Padawe56e75a32016-02-08 12:18:19 -0800511 // The subscriber id that this request applies to. Defaults to
512 // SubscriptionManager.INVALID_SUBSCRIPTION_ID
513 public Integer subId = SubscriptionManager.INVALID_SUBSCRIPTION_ID;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700514
Nathan Harold92bed182018-10-12 18:16:49 -0700515 // In cases where subId is unavailable, the caller needs to specify the phone.
516 public Phone phone;
517
vagdeviaf9a5b92018-08-15 16:01:53 -0700518 public WorkSource workSource;
519
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700520 public MainThreadRequest(Object argument) {
521 this.argument = argument;
522 }
Shishir Agrawal76d5da92014-11-09 16:17:25 -0800523
Nathan Harold92bed182018-10-12 18:16:49 -0700524 MainThreadRequest(Object argument, Phone phone, WorkSource workSource) {
525 this.argument = argument;
526 if (phone != null) {
527 this.phone = phone;
528 }
529 this.workSource = workSource;
530 }
531
vagdeviaf9a5b92018-08-15 16:01:53 -0700532 MainThreadRequest(Object argument, Integer subId, WorkSource workSource) {
Shishir Agrawal76d5da92014-11-09 16:17:25 -0800533 this.argument = argument;
Sanket Padawe56e75a32016-02-08 12:18:19 -0800534 if (subId != null) {
535 this.subId = subId;
536 }
vagdeviaf9a5b92018-08-15 16:01:53 -0700537 this.workSource = workSource;
Shishir Agrawal76d5da92014-11-09 16:17:25 -0800538 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700539 }
540
Sailesh Nepalcc0375f2013-11-13 09:15:18 -0800541 private static final class IncomingThirdPartyCallArgs {
542 public final ComponentName component;
543 public final String callId;
544 public final String callerDisplayName;
545
546 public IncomingThirdPartyCallArgs(ComponentName component, String callId,
547 String callerDisplayName) {
548 this.component = component;
549 this.callId = callId;
550 this.callerDisplayName = callerDisplayName;
551 }
552 }
553
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700554 /**
555 * A handler that processes messages on the main thread in the phone process. Since many
556 * of the Phone calls are not thread safe this is needed to shuttle the requests from the
557 * inbound binder threads to the main thread in the phone process. The Binder thread
558 * may provide a {@link MainThreadRequest} object in the msg.obj field that they are waiting
559 * on, which will be notified when the operation completes and will contain the result of the
560 * request.
561 *
562 * <p>If a MainThreadRequest object is provided in the msg.obj field,
563 * note that request.result must be set to something non-null for the calling thread to
564 * unblock.
565 */
566 private final class MainThreadHandler extends Handler {
567 @Override
568 public void handleMessage(Message msg) {
569 MainThreadRequest request;
570 Message onCompleted;
571 AsyncResult ar;
Muralidhar Reddy472c2ae2021-09-29 19:38:40 +0000572 UiccPort uiccPort;
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700573 IccAPDUArgument iccArgument;
Nazanin Bakhshif782e562018-12-11 15:15:39 -0800574 final Phone defaultPhone = getDefaultPhone();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700575
576 switch (msg.what) {
Pengquan Menga1bb6272018-09-06 09:59:22 -0700577 case CMD_HANDLE_USSD_REQUEST: {
578 request = (MainThreadRequest) msg.obj;
579 final Phone phone = getPhoneFromRequest(request);
580 Pair<String, ResultReceiver> ussdObject = (Pair) request.argument;
Chen Xue9d737e2022-01-01 23:41:31 -0800581 String ussdRequest = ussdObject.first;
Pengquan Menga1bb6272018-09-06 09:59:22 -0700582 ResultReceiver wrappedCallback = ussdObject.second;
Tyler Gunn65d45c22017-06-05 11:22:26 -0700583
Pengquan Menga1bb6272018-09-06 09:59:22 -0700584 if (!isUssdApiAllowed(request.subId)) {
585 // Carrier does not support use of this API, return failure.
586 Rlog.w(LOG_TAG, "handleUssdRequest: carrier does not support USSD apis.");
587 UssdResponse response = new UssdResponse(ussdRequest, null);
588 Bundle returnData = new Bundle();
589 returnData.putParcelable(TelephonyManager.USSD_RESPONSE, response);
590 wrappedCallback.send(TelephonyManager.USSD_RETURN_FAILURE, returnData);
Tyler Gunn65d45c22017-06-05 11:22:26 -0700591
Pengquan Menga1bb6272018-09-06 09:59:22 -0700592 request.result = true;
593 notifyRequester(request);
594 return;
595 }
Tyler Gunn65d45c22017-06-05 11:22:26 -0700596
Pengquan Menga1bb6272018-09-06 09:59:22 -0700597 try {
598 request.result = phone != null
599 ? phone.handleUssdRequest(ussdRequest, wrappedCallback) : false;
600 } catch (CallStateException cse) {
601 request.result = false;
602 }
603 // Wake up the requesting thread
604 notifyRequester(request);
605 break;
pkanwar32d516d2016-10-14 19:37:38 -0700606 }
607
Yorke Lee716f67e2015-06-17 15:39:16 -0700608 case CMD_HANDLE_PIN_MMI: {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700609 request = (MainThreadRequest) msg.obj;
Yorke Lee716f67e2015-06-17 15:39:16 -0700610 final Phone phone = getPhoneFromRequest(request);
611 request.result = phone != null ?
612 getPhoneFromRequest(request).handlePinMmi((String) request.argument)
613 : false;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700614 // Wake up the requesting thread
Pengquan Menga1bb6272018-09-06 09:59:22 -0700615 notifyRequester(request);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700616 break;
Yorke Lee716f67e2015-06-17 15:39:16 -0700617 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700618
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700619 case CMD_TRANSMIT_APDU_LOGICAL_CHANNEL:
Shishir Agrawal566b7612013-10-28 14:41:00 -0700620 request = (MainThreadRequest) msg.obj;
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700621 iccArgument = (IccAPDUArgument) request.argument;
Muralidhar Reddy472c2ae2021-09-29 19:38:40 +0000622 uiccPort = getUiccPortFromRequest(request);
623 if (uiccPort == null) {
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700624 loge("iccTransmitApduLogicalChannel: No UICC");
Chen Xue9d737e2022-01-01 23:41:31 -0800625 request.result = new IccIoResult(0x6F, 0, (byte[]) null);
Pengquan Menga1bb6272018-09-06 09:59:22 -0700626 notifyRequester(request);
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700627 } else {
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700628 onCompleted = obtainMessage(EVENT_TRANSMIT_APDU_LOGICAL_CHANNEL_DONE,
Chen Xue9d737e2022-01-01 23:41:31 -0800629 request);
Muralidhar Reddy472c2ae2021-09-29 19:38:40 +0000630 uiccPort.iccTransmitApduLogicalChannel(
Chen Xue9d737e2022-01-01 23:41:31 -0800631 iccArgument.channel, iccArgument.cla, iccArgument.command,
632 iccArgument.p1, iccArgument.p2, iccArgument.p3, iccArgument.data,
633 onCompleted);
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700634 }
Shishir Agrawal566b7612013-10-28 14:41:00 -0700635 break;
636
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700637 case EVENT_TRANSMIT_APDU_LOGICAL_CHANNEL_DONE:
Shishir Agrawal566b7612013-10-28 14:41:00 -0700638 ar = (AsyncResult) msg.obj;
639 request = (MainThreadRequest) ar.userObj;
640 if (ar.exception == null && ar.result != null) {
641 request.result = ar.result;
642 } else {
Chen Xue9d737e2022-01-01 23:41:31 -0800643 request.result = new IccIoResult(0x6F, 0, (byte[]) null);
Shishir Agrawal566b7612013-10-28 14:41:00 -0700644 if (ar.result == null) {
645 loge("iccTransmitApduLogicalChannel: Empty response");
Jake Hambye994d462014-02-03 13:10:13 -0800646 } else if (ar.exception instanceof CommandException) {
Shishir Agrawal566b7612013-10-28 14:41:00 -0700647 loge("iccTransmitApduLogicalChannel: CommandException: " +
Jake Hambye994d462014-02-03 13:10:13 -0800648 ar.exception);
Shishir Agrawal566b7612013-10-28 14:41:00 -0700649 } else {
650 loge("iccTransmitApduLogicalChannel: Unknown exception");
651 }
652 }
Pengquan Menga1bb6272018-09-06 09:59:22 -0700653 notifyRequester(request);
Shishir Agrawal566b7612013-10-28 14:41:00 -0700654 break;
655
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700656 case CMD_TRANSMIT_APDU_BASIC_CHANNEL:
657 request = (MainThreadRequest) msg.obj;
658 iccArgument = (IccAPDUArgument) request.argument;
Muralidhar Reddy472c2ae2021-09-29 19:38:40 +0000659 uiccPort = getUiccPortFromRequest(request);
660 if (uiccPort == null) {
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700661 loge("iccTransmitApduBasicChannel: No UICC");
Chen Xue9d737e2022-01-01 23:41:31 -0800662 request.result = new IccIoResult(0x6F, 0, (byte[]) null);
Pengquan Menga1bb6272018-09-06 09:59:22 -0700663 notifyRequester(request);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700664 } else {
665 onCompleted = obtainMessage(EVENT_TRANSMIT_APDU_BASIC_CHANNEL_DONE,
Chen Xue9d737e2022-01-01 23:41:31 -0800666 request);
Muralidhar Reddy472c2ae2021-09-29 19:38:40 +0000667 uiccPort.iccTransmitApduBasicChannel(
Chen Xue9d737e2022-01-01 23:41:31 -0800668 iccArgument.cla, iccArgument.command, iccArgument.p1,
669 iccArgument.p2,
670 iccArgument.p3, iccArgument.data, onCompleted);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700671 }
672 break;
673
674 case EVENT_TRANSMIT_APDU_BASIC_CHANNEL_DONE:
675 ar = (AsyncResult) msg.obj;
676 request = (MainThreadRequest) ar.userObj;
677 if (ar.exception == null && ar.result != null) {
678 request.result = ar.result;
679 } else {
Chen Xue9d737e2022-01-01 23:41:31 -0800680 request.result = new IccIoResult(0x6F, 0, (byte[]) null);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700681 if (ar.result == null) {
682 loge("iccTransmitApduBasicChannel: Empty response");
683 } else if (ar.exception instanceof CommandException) {
684 loge("iccTransmitApduBasicChannel: CommandException: " +
685 ar.exception);
686 } else {
687 loge("iccTransmitApduBasicChannel: Unknown exception");
688 }
689 }
Pengquan Menga1bb6272018-09-06 09:59:22 -0700690 notifyRequester(request);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700691 break;
692
693 case CMD_EXCHANGE_SIM_IO:
694 request = (MainThreadRequest) msg.obj;
695 iccArgument = (IccAPDUArgument) request.argument;
Muralidhar Reddy472c2ae2021-09-29 19:38:40 +0000696 uiccPort = getUiccPortFromRequest(request);
697 if (uiccPort == null) {
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700698 loge("iccExchangeSimIO: No UICC");
Chen Xue9d737e2022-01-01 23:41:31 -0800699 request.result = new IccIoResult(0x6F, 0, (byte[]) null);
Pengquan Menga1bb6272018-09-06 09:59:22 -0700700 notifyRequester(request);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700701 } else {
702 onCompleted = obtainMessage(EVENT_EXCHANGE_SIM_IO_DONE,
703 request);
Muralidhar Reddy472c2ae2021-09-29 19:38:40 +0000704 uiccPort.iccExchangeSimIO(iccArgument.cla, /* fileID */
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700705 iccArgument.command, iccArgument.p1, iccArgument.p2, iccArgument.p3,
706 iccArgument.data, onCompleted);
707 }
708 break;
709
710 case EVENT_EXCHANGE_SIM_IO_DONE:
711 ar = (AsyncResult) msg.obj;
712 request = (MainThreadRequest) ar.userObj;
713 if (ar.exception == null && ar.result != null) {
714 request.result = ar.result;
715 } else {
Chen Xue9d737e2022-01-01 23:41:31 -0800716 request.result = new IccIoResult(0x6f, 0, (byte[]) null);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700717 }
Pengquan Menga1bb6272018-09-06 09:59:22 -0700718 notifyRequester(request);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700719 break;
720
Derek Tan4d5e5c12014-02-04 11:54:58 -0800721 case CMD_SEND_ENVELOPE:
722 request = (MainThreadRequest) msg.obj;
Muralidhar Reddy472c2ae2021-09-29 19:38:40 +0000723 uiccPort = getUiccPortFromRequest(request);
724 if (uiccPort == null) {
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700725 loge("sendEnvelopeWithStatus: No UICC");
Chen Xue9d737e2022-01-01 23:41:31 -0800726 request.result = new IccIoResult(0x6F, 0, (byte[]) null);
Pengquan Menga1bb6272018-09-06 09:59:22 -0700727 notifyRequester(request);
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700728 } else {
729 onCompleted = obtainMessage(EVENT_SEND_ENVELOPE_DONE, request);
Chen Xue9d737e2022-01-01 23:41:31 -0800730 uiccPort.sendEnvelopeWithStatus((String) request.argument, onCompleted);
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700731 }
Derek Tan4d5e5c12014-02-04 11:54:58 -0800732 break;
733
734 case EVENT_SEND_ENVELOPE_DONE:
735 ar = (AsyncResult) msg.obj;
736 request = (MainThreadRequest) ar.userObj;
Shishir Agrawal9f9877d2014-03-14 09:36:27 -0700737 if (ar.exception == null && ar.result != null) {
738 request.result = ar.result;
Derek Tan4d5e5c12014-02-04 11:54:58 -0800739 } else {
Chen Xue9d737e2022-01-01 23:41:31 -0800740 request.result = new IccIoResult(0x6F, 0, (byte[]) null);
Shishir Agrawal9f9877d2014-03-14 09:36:27 -0700741 if (ar.result == null) {
742 loge("sendEnvelopeWithStatus: Empty response");
743 } else if (ar.exception instanceof CommandException) {
744 loge("sendEnvelopeWithStatus: CommandException: " +
745 ar.exception);
746 } else {
747 loge("sendEnvelopeWithStatus: exception:" + ar.exception);
748 }
Derek Tan4d5e5c12014-02-04 11:54:58 -0800749 }
Pengquan Menga1bb6272018-09-06 09:59:22 -0700750 notifyRequester(request);
Derek Tan4d5e5c12014-02-04 11:54:58 -0800751 break;
752
Shishir Agrawal566b7612013-10-28 14:41:00 -0700753 case CMD_OPEN_CHANNEL:
754 request = (MainThreadRequest) msg.obj;
Muralidhar Reddy472c2ae2021-09-29 19:38:40 +0000755 uiccPort = getUiccPortFromRequest(request);
Rambo Wanga1782702021-11-10 20:15:19 -0800756 IccLogicalChannelRequest openChannelRequest =
757 (IccLogicalChannelRequest) request.argument;
Muralidhar Reddy472c2ae2021-09-29 19:38:40 +0000758 if (uiccPort == null) {
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700759 loge("iccOpenLogicalChannel: No UICC");
Shishir Agrawalfc0492a2016-02-17 11:15:33 -0800760 request.result = new IccOpenLogicalChannelResponse(-1,
Chen Xue9d737e2022-01-01 23:41:31 -0800761 IccOpenLogicalChannelResponse.STATUS_MISSING_RESOURCE, null);
Pengquan Menga1bb6272018-09-06 09:59:22 -0700762 notifyRequester(request);
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700763 } else {
764 onCompleted = obtainMessage(EVENT_OPEN_CHANNEL_DONE, request);
Rambo Wanga1782702021-11-10 20:15:19 -0800765 uiccPort.iccOpenLogicalChannel(openChannelRequest.aid,
766 openChannelRequest.p2, onCompleted);
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700767 }
Shishir Agrawal566b7612013-10-28 14:41:00 -0700768 break;
769
770 case EVENT_OPEN_CHANNEL_DONE:
771 ar = (AsyncResult) msg.obj;
772 request = (MainThreadRequest) ar.userObj;
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700773 IccOpenLogicalChannelResponse openChannelResp;
Shishir Agrawal566b7612013-10-28 14:41:00 -0700774 if (ar.exception == null && ar.result != null) {
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700775 int[] result = (int[]) ar.result;
776 int channelId = result[0];
777 byte[] selectResponse = null;
778 if (result.length > 1) {
779 selectResponse = new byte[result.length - 1];
780 for (int i = 1; i < result.length; ++i) {
781 selectResponse[i - 1] = (byte) result[i];
782 }
783 }
784 openChannelResp = new IccOpenLogicalChannelResponse(channelId,
Chen Xue9d737e2022-01-01 23:41:31 -0800785 IccOpenLogicalChannelResponse.STATUS_NO_ERROR, selectResponse);
Rambo Wang3b77c4c2021-11-10 20:15:19 -0800786
787 uiccPort = getUiccPortFromRequest(request);
Rambo Wange53e07d2022-05-10 13:01:13 -0700788 if (uiccPort == null) {
789 loge("EVENT_OPEN_CHANNEL_DONE: UiccPort is null");
790 } else {
791 IccLogicalChannelRequest channelRequest =
792 (IccLogicalChannelRequest) request.argument;
793 channelRequest.channel = channelId;
794 uiccPort.onLogicalChannelOpened(channelRequest);
795 }
Shishir Agrawal566b7612013-10-28 14:41:00 -0700796 } else {
Shishir Agrawal566b7612013-10-28 14:41:00 -0700797 if (ar.result == null) {
798 loge("iccOpenLogicalChannel: Empty response");
Shishir Agrawal566b7612013-10-28 14:41:00 -0700799 }
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700800 if (ar.exception != null) {
801 loge("iccOpenLogicalChannel: Exception: " + ar.exception);
802 }
803
Shishir Agrawal527e8bf2014-08-25 08:54:56 -0700804 int errorCode = IccOpenLogicalChannelResponse.STATUS_UNKNOWN_ERROR;
Junda Liua754ba12015-05-20 01:17:52 -0700805 if (ar.exception instanceof CommandException) {
806 CommandException.Error error =
Chen Xue9d737e2022-01-01 23:41:31 -0800807 ((CommandException) (ar.exception)).getCommandError();
Junda Liua754ba12015-05-20 01:17:52 -0700808 if (error == CommandException.Error.MISSING_RESOURCE) {
Shishir Agrawal527e8bf2014-08-25 08:54:56 -0700809 errorCode = IccOpenLogicalChannelResponse.STATUS_MISSING_RESOURCE;
Junda Liua754ba12015-05-20 01:17:52 -0700810 } else if (error == CommandException.Error.NO_SUCH_ELEMENT) {
Shishir Agrawal527e8bf2014-08-25 08:54:56 -0700811 errorCode = IccOpenLogicalChannelResponse.STATUS_NO_SUCH_ELEMENT;
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700812 }
813 }
814 openChannelResp = new IccOpenLogicalChannelResponse(
Chen Xue9d737e2022-01-01 23:41:31 -0800815 IccOpenLogicalChannelResponse.INVALID_CHANNEL, errorCode, null);
Shishir Agrawal566b7612013-10-28 14:41:00 -0700816 }
Shishir Agrawal82c8a462014-07-31 18:13:17 -0700817 request.result = openChannelResp;
Pengquan Menga1bb6272018-09-06 09:59:22 -0700818 notifyRequester(request);
Shishir Agrawal566b7612013-10-28 14:41:00 -0700819 break;
820
821 case CMD_CLOSE_CHANNEL:
822 request = (MainThreadRequest) msg.obj;
Muralidhar Reddy472c2ae2021-09-29 19:38:40 +0000823 uiccPort = getUiccPortFromRequest(request);
824 if (uiccPort == null) {
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700825 loge("iccCloseLogicalChannel: No UICC");
Chen Xua8f0dff2022-02-12 00:34:15 -0800826 request.result = new IllegalArgumentException(
Chen Xue9d737e2022-01-01 23:41:31 -0800827 "iccCloseLogicalChannel: No UICC");
Chen Xue9d737e2022-01-01 23:41:31 -0800828 notifyRequester(request);
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700829 } else {
830 onCompleted = obtainMessage(EVENT_CLOSE_CHANNEL_DONE, request);
Muralidhar Reddy472c2ae2021-09-29 19:38:40 +0000831 uiccPort.iccCloseLogicalChannel((Integer) request.argument, onCompleted);
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700832 }
Shishir Agrawal566b7612013-10-28 14:41:00 -0700833 break;
834
835 case EVENT_CLOSE_CHANNEL_DONE:
Chen Xu540470b2021-12-14 17:15:47 -0800836 ar = (AsyncResult) msg.obj;
837 request = (MainThreadRequest) ar.userObj;
838 if (ar.exception == null) {
839 request.result = true;
Rambo Wang3b77c4c2021-11-10 20:15:19 -0800840 uiccPort = getUiccPortFromRequest(request);
Rambo Wange53e07d2022-05-10 13:01:13 -0700841 if (uiccPort == null) {
842 loge("EVENT_CLOSE_CHANNEL_DONE: UiccPort is null");
843 } else {
844 final int channelId = (Integer) request.argument;
845 uiccPort.onLogicalChannelClosed(channelId);
846 }
Chen Xu540470b2021-12-14 17:15:47 -0800847 } else {
848 request.result = false;
Chen Xue9d737e2022-01-01 23:41:31 -0800849 Exception exception = null;
Chen Xu540470b2021-12-14 17:15:47 -0800850 if (ar.exception instanceof CommandException) {
851 loge("iccCloseLogicalChannel: CommandException: " + ar.exception);
852 CommandException.Error error =
853 ((CommandException) (ar.exception)).getCommandError();
Chen Xue9d737e2022-01-01 23:41:31 -0800854 if (error == CommandException.Error.INVALID_ARGUMENTS) {
855 // should only throw exceptions from the binder threads.
856 exception = new IllegalArgumentException(
Chen Xu540470b2021-12-14 17:15:47 -0800857 "iccCloseLogicalChannel: invalid argument ");
858 }
859 } else {
860 loge("iccCloseLogicalChannel: Unknown exception");
861 }
Chen Xua8f0dff2022-02-12 00:34:15 -0800862 request.result = (exception != null) ? exception :
863 new IllegalStateException(
864 "exception from modem to close iccLogical Channel");
Chen Xu540470b2021-12-14 17:15:47 -0800865 }
866 notifyRequester(request);
Jake Hambye994d462014-02-03 13:10:13 -0800867 break;
868
869 case CMD_NV_READ_ITEM:
870 request = (MainThreadRequest) msg.obj;
871 onCompleted = obtainMessage(EVENT_NV_READ_ITEM_DONE, request);
Nazanin Bakhshif782e562018-12-11 15:15:39 -0800872 defaultPhone.nvReadItem((Integer) request.argument, onCompleted,
873 request.workSource);
Jake Hambye994d462014-02-03 13:10:13 -0800874 break;
875
876 case EVENT_NV_READ_ITEM_DONE:
Shishir Agrawal566b7612013-10-28 14:41:00 -0700877 ar = (AsyncResult) msg.obj;
878 request = (MainThreadRequest) ar.userObj;
Jake Hambye994d462014-02-03 13:10:13 -0800879 if (ar.exception == null && ar.result != null) {
880 request.result = ar.result; // String
Shishir Agrawal566b7612013-10-28 14:41:00 -0700881 } else {
Jake Hambye994d462014-02-03 13:10:13 -0800882 request.result = "";
883 if (ar.result == null) {
884 loge("nvReadItem: Empty response");
885 } else if (ar.exception instanceof CommandException) {
886 loge("nvReadItem: CommandException: " +
887 ar.exception);
Shishir Agrawal566b7612013-10-28 14:41:00 -0700888 } else {
Jake Hambye994d462014-02-03 13:10:13 -0800889 loge("nvReadItem: Unknown exception");
Shishir Agrawal566b7612013-10-28 14:41:00 -0700890 }
891 }
Pengquan Menga1bb6272018-09-06 09:59:22 -0700892 notifyRequester(request);
Shishir Agrawal566b7612013-10-28 14:41:00 -0700893 break;
894
Jake Hambye994d462014-02-03 13:10:13 -0800895 case CMD_NV_WRITE_ITEM:
896 request = (MainThreadRequest) msg.obj;
897 onCompleted = obtainMessage(EVENT_NV_WRITE_ITEM_DONE, request);
898 Pair<Integer, String> idValue = (Pair<Integer, String>) request.argument;
Nazanin Bakhshif782e562018-12-11 15:15:39 -0800899 defaultPhone.nvWriteItem(idValue.first, idValue.second, onCompleted,
vagdeviaf9a5b92018-08-15 16:01:53 -0700900 request.workSource);
Jake Hambye994d462014-02-03 13:10:13 -0800901 break;
902
903 case EVENT_NV_WRITE_ITEM_DONE:
904 handleNullReturnEvent(msg, "nvWriteItem");
905 break;
906
907 case CMD_NV_WRITE_CDMA_PRL:
908 request = (MainThreadRequest) msg.obj;
909 onCompleted = obtainMessage(EVENT_NV_WRITE_CDMA_PRL_DONE, request);
Nazanin Bakhshif782e562018-12-11 15:15:39 -0800910 defaultPhone.nvWriteCdmaPrl((byte[]) request.argument, onCompleted);
Jake Hambye994d462014-02-03 13:10:13 -0800911 break;
912
913 case EVENT_NV_WRITE_CDMA_PRL_DONE:
914 handleNullReturnEvent(msg, "nvWriteCdmaPrl");
915 break;
916
chen xu6dac5ab2018-10-26 17:39:23 -0700917 case CMD_RESET_MODEM_CONFIG:
Jake Hambye994d462014-02-03 13:10:13 -0800918 request = (MainThreadRequest) msg.obj;
chen xu6dac5ab2018-10-26 17:39:23 -0700919 onCompleted = obtainMessage(EVENT_RESET_MODEM_CONFIG_DONE, request);
Nazanin Bakhshif782e562018-12-11 15:15:39 -0800920 defaultPhone.resetModemConfig(onCompleted);
Jake Hambye994d462014-02-03 13:10:13 -0800921 break;
922
chen xu6dac5ab2018-10-26 17:39:23 -0700923 case EVENT_RESET_MODEM_CONFIG_DONE:
924 handleNullReturnEvent(msg, "resetModemConfig");
Jake Hambye994d462014-02-03 13:10:13 -0800925 break;
926
Sooraj Sasindran37444802020-08-11 10:40:43 -0700927 case CMD_IS_NR_DUAL_CONNECTIVITY_ENABLED: {
928 request = (MainThreadRequest) msg.obj;
929 onCompleted = obtainMessage(EVENT_IS_NR_DUAL_CONNECTIVITY_ENABLED_DONE,
930 request);
931 Phone phone = getPhoneFromRequest(request);
932 if (phone != null) {
933 phone.isNrDualConnectivityEnabled(onCompleted, request.workSource);
934 } else {
935 loge("isNRDualConnectivityEnabled: No phone object");
936 request.result = false;
937 notifyRequester(request);
938 }
939 break;
940 }
941
942 case EVENT_IS_NR_DUAL_CONNECTIVITY_ENABLED_DONE:
943 ar = (AsyncResult) msg.obj;
944 request = (MainThreadRequest) ar.userObj;
945 if (ar.exception == null && ar.result != null) {
946 request.result = ar.result;
947 } else {
948 // request.result must be set to something non-null
949 // for the calling thread to unblock
Sooraj Sasindrandaf060f2021-06-15 14:52:55 -0700950 if (ar.result != null) {
Sooraj Sasindran37444802020-08-11 10:40:43 -0700951 request.result = ar.result;
952 } else {
953 request.result = false;
954 }
955 if (ar.result == null) {
956 loge("isNRDualConnectivityEnabled: Empty response");
957 } else if (ar.exception instanceof CommandException) {
958 loge("isNRDualConnectivityEnabled: CommandException: "
959 + ar.exception);
960 } else {
961 loge("isNRDualConnectivityEnabled: Unknown exception");
962 }
963 }
964 notifyRequester(request);
965 break;
966
Sooraj Sasindrandaf060f2021-06-15 14:52:55 -0700967 case CMD_IS_VONR_ENABLED: {
968 request = (MainThreadRequest) msg.obj;
969 onCompleted = obtainMessage(EVENT_IS_VONR_ENABLED_DONE,
970 request);
971 Phone phone = getPhoneFromRequest(request);
972 if (phone != null) {
973 phone.isVoNrEnabled(onCompleted, request.workSource);
974 } else {
975 loge("isVoNrEnabled: No phone object");
976 request.result = false;
977 notifyRequester(request);
978 }
979 break;
980 }
981
982 case EVENT_IS_VONR_ENABLED_DONE:
983 ar = (AsyncResult) msg.obj;
984 request = (MainThreadRequest) ar.userObj;
985 if (ar.exception == null && ar.result != null) {
986 request.result = ar.result;
987 } else {
988 // request.result must be set to something non-null
989 // for the calling thread to unblock
990 if (ar.result != null) {
991 request.result = ar.result;
992 } else {
993 request.result = false;
994 }
995 if (ar.result == null) {
996 loge("isVoNrEnabled: Empty response");
997 } else if (ar.exception instanceof CommandException) {
998 loge("isVoNrEnabled: CommandException: "
999 + ar.exception);
1000 } else {
1001 loge("isVoNrEnabled: Unknown exception");
1002 }
1003 }
1004 notifyRequester(request);
1005 break;
1006
Sooraj Sasindran37444802020-08-11 10:40:43 -07001007 case CMD_ENABLE_NR_DUAL_CONNECTIVITY: {
1008 request = (MainThreadRequest) msg.obj;
1009 onCompleted = obtainMessage(EVENT_ENABLE_NR_DUAL_CONNECTIVITY_DONE, request);
1010 Phone phone = getPhoneFromRequest(request);
1011 if (phone != null) {
1012 phone.setNrDualConnectivityState((int) request.argument, onCompleted,
1013 request.workSource);
1014 } else {
1015 loge("enableNrDualConnectivity: No phone object");
1016 request.result =
1017 TelephonyManager.ENABLE_NR_DUAL_CONNECTIVITY_RADIO_NOT_AVAILABLE;
1018 notifyRequester(request);
1019 }
1020 break;
1021 }
1022
1023 case EVENT_ENABLE_NR_DUAL_CONNECTIVITY_DONE: {
1024 ar = (AsyncResult) msg.obj;
1025 request = (MainThreadRequest) ar.userObj;
1026 if (ar.exception == null) {
1027 request.result =
1028 TelephonyManager.ENABLE_NR_DUAL_CONNECTIVITY_SUCCESS;
1029 } else {
1030 request.result =
1031 TelephonyManager
1032 .ENABLE_NR_DUAL_CONNECTIVITY_RADIO_ERROR;
1033 if (ar.exception instanceof CommandException) {
1034 CommandException.Error error =
1035 ((CommandException) (ar.exception)).getCommandError();
1036 if (error == CommandException.Error.RADIO_NOT_AVAILABLE) {
1037 request.result =
1038 TelephonyManager
1039 .ENABLE_NR_DUAL_CONNECTIVITY_RADIO_NOT_AVAILABLE;
Sooraj Sasindran29654162021-03-03 23:00:01 +00001040 } else if (error == CommandException.Error.REQUEST_NOT_SUPPORTED) {
1041 request.result =
1042 TelephonyManager
1043 .ENABLE_NR_DUAL_CONNECTIVITY_NOT_SUPPORTED;
Sooraj Sasindran37444802020-08-11 10:40:43 -07001044 }
1045 loge("enableNrDualConnectivity" + ": CommandException: "
1046 + ar.exception);
1047 } else {
1048 loge("enableNrDualConnectivity" + ": Unknown exception");
1049 }
1050 }
1051 notifyRequester(request);
1052 break;
1053 }
1054
Sooraj Sasindrandaf060f2021-06-15 14:52:55 -07001055 case CMD_ENABLE_VONR: {
1056 request = (MainThreadRequest) msg.obj;
1057 onCompleted = obtainMessage(EVENT_ENABLE_VONR_DONE, request);
1058 Phone phone = getPhoneFromRequest(request);
1059 if (phone != null) {
1060 phone.setVoNrEnabled((boolean) request.argument, onCompleted,
1061 request.workSource);
1062 } else {
1063 loge("setVoNrEnabled: No phone object");
1064 request.result =
1065 TelephonyManager.ENABLE_VONR_RADIO_NOT_AVAILABLE;
1066 notifyRequester(request);
1067 }
1068 break;
1069 }
1070
1071 case EVENT_ENABLE_VONR_DONE: {
1072 ar = (AsyncResult) msg.obj;
1073 request = (MainThreadRequest) ar.userObj;
1074 if (ar.exception == null) {
1075 request.result = TelephonyManager.ENABLE_VONR_SUCCESS;
1076 } else {
1077 request.result = TelephonyManager.ENABLE_VONR_RADIO_ERROR;
1078 if (ar.exception instanceof CommandException) {
1079 CommandException.Error error =
1080 ((CommandException) (ar.exception)).getCommandError();
1081 if (error == CommandException.Error.RADIO_NOT_AVAILABLE) {
1082 request.result = TelephonyManager.ENABLE_VONR_RADIO_NOT_AVAILABLE;
1083 } else if (error == CommandException.Error.REQUEST_NOT_SUPPORTED) {
1084 request.result = TelephonyManager.ENABLE_VONR_REQUEST_NOT_SUPPORTED;
1085 } else {
1086 request.result = TelephonyManager.ENABLE_VONR_RADIO_ERROR;
1087 }
1088 loge("setVoNrEnabled" + ": CommandException: "
1089 + ar.exception);
1090 } else {
1091 loge("setVoNrEnabled" + ": Unknown exception");
1092 }
1093 }
1094 notifyRequester(request);
1095 break;
1096 }
1097
SongFerngWang3ef3e072020-12-21 16:41:52 +08001098 case CMD_GET_ALLOWED_NETWORK_TYPES_BITMASK:
Jake Hamby7c27be32014-03-03 13:25:59 -08001099 request = (MainThreadRequest) msg.obj;
SongFerngWang3ef3e072020-12-21 16:41:52 +08001100 onCompleted = obtainMessage(EVENT_GET_ALLOWED_NETWORK_TYPES_BITMASK_DONE,
1101 request);
1102 getPhoneFromRequest(request).getAllowedNetworkTypesBitmask(onCompleted);
Jake Hamby7c27be32014-03-03 13:25:59 -08001103 break;
1104
SongFerngWang3ef3e072020-12-21 16:41:52 +08001105 case EVENT_GET_ALLOWED_NETWORK_TYPES_BITMASK_DONE:
Jake Hamby7c27be32014-03-03 13:25:59 -08001106 ar = (AsyncResult) msg.obj;
1107 request = (MainThreadRequest) ar.userObj;
1108 if (ar.exception == null && ar.result != null) {
1109 request.result = ar.result; // Integer
1110 } else {
Nazish Tabassume8ba43a2020-07-28 14:49:25 +05301111 // request.result must be set to something non-null
1112 // for the calling thread to unblock
1113 request.result = new int[]{-1};
Jake Hamby7c27be32014-03-03 13:25:59 -08001114 if (ar.result == null) {
SongFerngWang3ef3e072020-12-21 16:41:52 +08001115 loge("getAllowedNetworkTypesBitmask: Empty response");
Jake Hamby7c27be32014-03-03 13:25:59 -08001116 } else if (ar.exception instanceof CommandException) {
SongFerngWang3ef3e072020-12-21 16:41:52 +08001117 loge("getAllowedNetworkTypesBitmask: CommandException: "
1118 + ar.exception);
Jake Hamby7c27be32014-03-03 13:25:59 -08001119 } else {
SongFerngWang3ef3e072020-12-21 16:41:52 +08001120 loge("getAllowedNetworkTypesBitmask: Unknown exception");
Jake Hamby7c27be32014-03-03 13:25:59 -08001121 }
1122 }
Pengquan Menga1bb6272018-09-06 09:59:22 -07001123 notifyRequester(request);
Jake Hamby7c27be32014-03-03 13:25:59 -08001124 break;
1125
SongFerngWang3ef3e072020-12-21 16:41:52 +08001126 case CMD_SET_ALLOWED_NETWORK_TYPES_FOR_REASON:
Jake Hamby7c27be32014-03-03 13:25:59 -08001127 request = (MainThreadRequest) msg.obj;
SongFerngWang3ef3e072020-12-21 16:41:52 +08001128 onCompleted = obtainMessage(EVENT_SET_ALLOWED_NETWORK_TYPES_FOR_REASON_DONE,
1129 request);
1130 Pair<Integer, Long> reasonWithNetworkTypes =
1131 (Pair<Integer, Long>) request.argument;
1132 getPhoneFromRequest(request).setAllowedNetworkTypes(
1133 reasonWithNetworkTypes.first,
1134 reasonWithNetworkTypes.second,
1135 onCompleted);
Jake Hamby7c27be32014-03-03 13:25:59 -08001136 break;
1137
SongFerngWang3ef3e072020-12-21 16:41:52 +08001138 case EVENT_SET_ALLOWED_NETWORK_TYPES_FOR_REASON_DONE:
1139 handleNullReturnEvent(msg, "setAllowedNetworkTypesForReason");
Jake Hamby7c27be32014-03-03 13:25:59 -08001140 break;
1141
Shuo Qian850e4d6a2018-04-25 21:02:08 +00001142 case CMD_INVOKE_OEM_RIL_REQUEST_RAW:
1143 request = (MainThreadRequest)msg.obj;
1144 onCompleted = obtainMessage(EVENT_INVOKE_OEM_RIL_REQUEST_RAW_DONE, request);
Nazanin Bakhshif782e562018-12-11 15:15:39 -08001145 defaultPhone.invokeOemRilRequestRaw((byte[]) request.argument, onCompleted);
Shuo Qian850e4d6a2018-04-25 21:02:08 +00001146 break;
1147
1148 case EVENT_INVOKE_OEM_RIL_REQUEST_RAW_DONE:
1149 ar = (AsyncResult)msg.obj;
1150 request = (MainThreadRequest)ar.userObj;
1151 request.result = ar;
Pengquan Menga1bb6272018-09-06 09:59:22 -07001152 notifyRequester(request);
Shuo Qian850e4d6a2018-04-25 21:02:08 +00001153 break;
1154
Shishir Agrawal76d5da92014-11-09 16:17:25 -08001155 case CMD_SET_VOICEMAIL_NUMBER:
1156 request = (MainThreadRequest) msg.obj;
1157 onCompleted = obtainMessage(EVENT_SET_VOICEMAIL_NUMBER_DONE, request);
1158 Pair<String, String> tagNum = (Pair<String, String>) request.argument;
Stuart Scott584921c2015-01-15 17:10:34 -08001159 getPhoneFromRequest(request).setVoiceMailNumber(tagNum.first, tagNum.second,
1160 onCompleted);
Shishir Agrawal76d5da92014-11-09 16:17:25 -08001161 break;
1162
1163 case EVENT_SET_VOICEMAIL_NUMBER_DONE:
1164 handleNullReturnEvent(msg, "setVoicemailNumber");
1165 break;
1166
Stuart Scott54788802015-03-30 13:18:01 -07001167 case CMD_SET_NETWORK_SELECTION_MODE_AUTOMATIC:
1168 request = (MainThreadRequest) msg.obj;
1169 onCompleted = obtainMessage(EVENT_SET_NETWORK_SELECTION_MODE_AUTOMATIC_DONE,
1170 request);
1171 getPhoneFromRequest(request).setNetworkSelectionModeAutomatic(onCompleted);
1172 break;
1173
1174 case EVENT_SET_NETWORK_SELECTION_MODE_AUTOMATIC_DONE:
1175 handleNullReturnEvent(msg, "setNetworkSelectionModeAutomatic");
1176 break;
1177
Shishir Agrawal302c8692015-06-19 13:49:39 -07001178 case CMD_PERFORM_NETWORK_SCAN:
1179 request = (MainThreadRequest) msg.obj;
1180 onCompleted = obtainMessage(EVENT_PERFORM_NETWORK_SCAN_DONE, request);
1181 getPhoneFromRequest(request).getAvailableNetworks(onCompleted);
1182 break;
1183
Hall Liu27d24262020-09-18 19:04:59 -07001184 case CMD_GET_CALL_FORWARDING: {
Shuo Qian4a594052020-01-23 11:59:30 -08001185 request = (MainThreadRequest) msg.obj;
1186 onCompleted = obtainMessage(EVENT_GET_CALL_FORWARDING_DONE, request);
Hall Liu27d24262020-09-18 19:04:59 -07001187 Pair<Integer, TelephonyManager.CallForwardingInfoCallback> args =
1188 (Pair<Integer, TelephonyManager.CallForwardingInfoCallback>)
1189 request.argument;
1190 int callForwardingReason = args.first;
1191 request.phone.getCallForwardingOption(callForwardingReason, onCompleted);
Shuo Qian4a594052020-01-23 11:59:30 -08001192 break;
Hall Liu27d24262020-09-18 19:04:59 -07001193 }
1194 case EVENT_GET_CALL_FORWARDING_DONE: {
Shuo Qian4a594052020-01-23 11:59:30 -08001195 ar = (AsyncResult) msg.obj;
1196 request = (MainThreadRequest) ar.userObj;
Hall Liu27d24262020-09-18 19:04:59 -07001197 TelephonyManager.CallForwardingInfoCallback callback =
1198 ((Pair<Integer, TelephonyManager.CallForwardingInfoCallback>)
1199 request.argument).second;
Shuo Qian4a594052020-01-23 11:59:30 -08001200 if (ar.exception == null && ar.result != null) {
Hall Liu27d24262020-09-18 19:04:59 -07001201 CallForwardingInfo callForwardingInfo = null;
Shuo Qian4a594052020-01-23 11:59:30 -08001202 CallForwardInfo[] callForwardInfos = (CallForwardInfo[]) ar.result;
1203 for (CallForwardInfo callForwardInfo : callForwardInfos) {
1204 // Service Class is a bit mask per 3gpp 27.007. Search for
1205 // any service for voice call.
1206 if ((callForwardInfo.serviceClass
1207 & CommandsInterface.SERVICE_CLASS_VOICE) > 0) {
Yuchen Dong69cc1412021-09-27 20:27:01 +08001208 callForwardingInfo = new CallForwardingInfo(
1209 callForwardInfo.status
1210 == CommandsInterface.CF_ACTION_ENABLE,
Hall Liu27d24262020-09-18 19:04:59 -07001211 callForwardInfo.reason,
1212 callForwardInfo.number,
1213 callForwardInfo.timeSeconds);
Shuo Qian4a594052020-01-23 11:59:30 -08001214 break;
1215 }
1216 }
1217 // Didn't find a call forward info for voice call.
1218 if (callForwardingInfo == null) {
Hall Liu27d24262020-09-18 19:04:59 -07001219 callForwardingInfo = new CallForwardingInfo(false /* enabled */,
1220 0 /* reason */, null /* number */, 0 /* timeout */);
Shuo Qian4a594052020-01-23 11:59:30 -08001221 }
Hall Liu27d24262020-09-18 19:04:59 -07001222 callback.onCallForwardingInfoAvailable(callForwardingInfo);
Shuo Qian4a594052020-01-23 11:59:30 -08001223 } else {
1224 if (ar.result == null) {
1225 loge("EVENT_GET_CALL_FORWARDING_DONE: Empty response");
1226 }
1227 if (ar.exception != null) {
1228 loge("EVENT_GET_CALL_FORWARDING_DONE: Exception: " + ar.exception);
1229 }
Hall Liu940c4ca2020-09-29 17:10:18 -07001230 int errorCode = TelephonyManager
1231 .CallForwardingInfoCallback.RESULT_ERROR_UNKNOWN;
Shuo Qian4a594052020-01-23 11:59:30 -08001232 if (ar.exception instanceof CommandException) {
1233 CommandException.Error error =
1234 ((CommandException) (ar.exception)).getCommandError();
1235 if (error == CommandException.Error.FDN_CHECK_FAILURE) {
Hall Liu940c4ca2020-09-29 17:10:18 -07001236 errorCode = TelephonyManager
1237 .CallForwardingInfoCallback.RESULT_ERROR_FDN_CHECK_FAILURE;
Shuo Qian4a594052020-01-23 11:59:30 -08001238 } else if (error == CommandException.Error.REQUEST_NOT_SUPPORTED) {
Hall Liu940c4ca2020-09-29 17:10:18 -07001239 errorCode = TelephonyManager
1240 .CallForwardingInfoCallback.RESULT_ERROR_NOT_SUPPORTED;
Shuo Qian4a594052020-01-23 11:59:30 -08001241 }
1242 }
Hall Liu27d24262020-09-18 19:04:59 -07001243 callback.onError(errorCode);
Shuo Qian4a594052020-01-23 11:59:30 -08001244 }
Shuo Qian4a594052020-01-23 11:59:30 -08001245 break;
Hall Liu27d24262020-09-18 19:04:59 -07001246 }
Shuo Qian4a594052020-01-23 11:59:30 -08001247
Hall Liu27d24262020-09-18 19:04:59 -07001248 case CMD_SET_CALL_FORWARDING: {
Shuo Qian4a594052020-01-23 11:59:30 -08001249 request = (MainThreadRequest) msg.obj;
1250 onCompleted = obtainMessage(EVENT_SET_CALL_FORWARDING_DONE, request);
Hall Liu27d24262020-09-18 19:04:59 -07001251 request = (MainThreadRequest) msg.obj;
Shuo Qian4a594052020-01-23 11:59:30 -08001252 CallForwardingInfo callForwardingInfoToSet =
Hall Liu27d24262020-09-18 19:04:59 -07001253 ((Pair<CallForwardingInfo, Consumer<Integer>>)
1254 request.argument).first;
1255 request.phone.setCallForwardingOption(
1256 callForwardingInfoToSet.isEnabled()
Calvin Pan258f1f72021-07-28 21:46:56 +08001257 ? CommandsInterface.CF_ACTION_REGISTRATION
Hall Liu27d24262020-09-18 19:04:59 -07001258 : CommandsInterface.CF_ACTION_DISABLE,
Shuo Qian4a594052020-01-23 11:59:30 -08001259 callForwardingInfoToSet.getReason(),
1260 callForwardingInfoToSet.getNumber(),
1261 callForwardingInfoToSet.getTimeoutSeconds(), onCompleted);
1262 break;
Hall Liu27d24262020-09-18 19:04:59 -07001263 }
Shuo Qian4a594052020-01-23 11:59:30 -08001264
Hall Liu27d24262020-09-18 19:04:59 -07001265 case EVENT_SET_CALL_FORWARDING_DONE: {
Shuo Qian4a594052020-01-23 11:59:30 -08001266 ar = (AsyncResult) msg.obj;
1267 request = (MainThreadRequest) ar.userObj;
Hall Liu27d24262020-09-18 19:04:59 -07001268 Consumer<Integer> callback =
1269 ((Pair<CallForwardingInfo, Consumer<Integer>>)
1270 request.argument).second;
1271 if (ar.exception != null) {
Shuo Qian4a594052020-01-23 11:59:30 -08001272 loge("setCallForwarding exception: " + ar.exception);
Hall Liu940c4ca2020-09-29 17:10:18 -07001273 int errorCode = TelephonyManager.CallForwardingInfoCallback
1274 .RESULT_ERROR_UNKNOWN;
Hall Liu27d24262020-09-18 19:04:59 -07001275 if (ar.exception instanceof CommandException) {
1276 CommandException.Error error =
1277 ((CommandException) (ar.exception)).getCommandError();
1278 if (error == CommandException.Error.FDN_CHECK_FAILURE) {
Hall Liu940c4ca2020-09-29 17:10:18 -07001279 errorCode = TelephonyManager.CallForwardingInfoCallback
1280 .RESULT_ERROR_FDN_CHECK_FAILURE;
Hall Liu27d24262020-09-18 19:04:59 -07001281 } else if (error == CommandException.Error.REQUEST_NOT_SUPPORTED) {
Hall Liu940c4ca2020-09-29 17:10:18 -07001282 errorCode = TelephonyManager.CallForwardingInfoCallback
1283 .RESULT_ERROR_NOT_SUPPORTED;
Hall Liu27d24262020-09-18 19:04:59 -07001284 }
1285 }
1286 callback.accept(errorCode);
1287 } else {
Hall Liu940c4ca2020-09-29 17:10:18 -07001288 callback.accept(TelephonyManager.CallForwardingInfoCallback.RESULT_SUCCESS);
Shuo Qian4a594052020-01-23 11:59:30 -08001289 }
Shuo Qian4a594052020-01-23 11:59:30 -08001290 break;
Hall Liu27d24262020-09-18 19:04:59 -07001291 }
Shuo Qian4a594052020-01-23 11:59:30 -08001292
Hall Liu27d24262020-09-18 19:04:59 -07001293 case CMD_GET_CALL_WAITING: {
Shuo Qian4a594052020-01-23 11:59:30 -08001294 request = (MainThreadRequest) msg.obj;
1295 onCompleted = obtainMessage(EVENT_GET_CALL_WAITING_DONE, request);
1296 getPhoneFromRequest(request).getCallWaiting(onCompleted);
1297 break;
Hall Liu27d24262020-09-18 19:04:59 -07001298 }
Shuo Qian4a594052020-01-23 11:59:30 -08001299
Hall Liu27d24262020-09-18 19:04:59 -07001300 case EVENT_GET_CALL_WAITING_DONE: {
Shuo Qian4a594052020-01-23 11:59:30 -08001301 ar = (AsyncResult) msg.obj;
1302 request = (MainThreadRequest) ar.userObj;
Hall Liu27d24262020-09-18 19:04:59 -07001303 Consumer<Integer> callback = (Consumer<Integer>) request.argument;
SongFerngWangebda2c52022-01-11 15:28:38 +08001304 int callWaitingStatus = TelephonyManager.CALL_WAITING_STATUS_UNKNOWN_ERROR;
Shuo Qian4a594052020-01-23 11:59:30 -08001305 if (ar.exception == null && ar.result != null) {
Shuo Qiand6a0dba2020-02-18 18:13:49 -08001306 int[] callForwardResults = (int[]) ar.result;
Shuo Qian4a594052020-01-23 11:59:30 -08001307 // Service Class is a bit mask per 3gpp 27.007.
1308 // Search for any service for voice call.
Shuo Qiand6a0dba2020-02-18 18:13:49 -08001309 if (callForwardResults.length > 1
1310 && ((callForwardResults[1]
Hall Liu27d24262020-09-18 19:04:59 -07001311 & CommandsInterface.SERVICE_CLASS_VOICE) > 0)) {
SongFerngWangebda2c52022-01-11 15:28:38 +08001312 callWaitingStatus = callForwardResults[0] == 0
Hall Liu27d24262020-09-18 19:04:59 -07001313 ? TelephonyManager.CALL_WAITING_STATUS_DISABLED
1314 : TelephonyManager.CALL_WAITING_STATUS_ENABLED;
Shuo Qian4a594052020-01-23 11:59:30 -08001315 } else {
SongFerngWangebda2c52022-01-11 15:28:38 +08001316 callWaitingStatus = TelephonyManager.CALL_WAITING_STATUS_DISABLED;
Shuo Qian4a594052020-01-23 11:59:30 -08001317 }
1318 } else {
1319 if (ar.result == null) {
1320 loge("EVENT_GET_CALL_WAITING_DONE: Empty response");
1321 }
1322 if (ar.exception != null) {
1323 loge("EVENT_GET_CALL_WAITING_DONE: Exception: " + ar.exception);
1324 }
1325 if (ar.exception instanceof CommandException) {
1326 CommandException.Error error =
1327 ((CommandException) (ar.exception)).getCommandError();
1328 if (error == CommandException.Error.REQUEST_NOT_SUPPORTED) {
SongFerngWangebda2c52022-01-11 15:28:38 +08001329 callWaitingStatus =
Shuo Qian4a594052020-01-23 11:59:30 -08001330 TelephonyManager.CALL_WAITING_STATUS_NOT_SUPPORTED;
SongFerngWangebda2c52022-01-11 15:28:38 +08001331 } else if (error == CommandException.Error.FDN_CHECK_FAILURE) {
1332 callWaitingStatus =
1333 TelephonyManager.CALL_WAITING_STATUS_FDN_CHECK_FAILURE;
Shuo Qian4a594052020-01-23 11:59:30 -08001334 }
1335 }
1336 }
SongFerngWangebda2c52022-01-11 15:28:38 +08001337 callback.accept(callWaitingStatus);
Shuo Qian4a594052020-01-23 11:59:30 -08001338 break;
Hall Liu27d24262020-09-18 19:04:59 -07001339 }
Shuo Qian4a594052020-01-23 11:59:30 -08001340
Hall Liu27d24262020-09-18 19:04:59 -07001341 case CMD_SET_CALL_WAITING: {
Shuo Qian4a594052020-01-23 11:59:30 -08001342 request = (MainThreadRequest) msg.obj;
1343 onCompleted = obtainMessage(EVENT_SET_CALL_WAITING_DONE, request);
Hall Liu27d24262020-09-18 19:04:59 -07001344 boolean enable = ((Pair<Boolean, Consumer<Integer>>) request.argument).first;
1345 getPhoneFromRequest(request).setCallWaiting(enable, onCompleted);
Shuo Qian4a594052020-01-23 11:59:30 -08001346 break;
Hall Liu27d24262020-09-18 19:04:59 -07001347 }
Shuo Qian4a594052020-01-23 11:59:30 -08001348
Hall Liu27d24262020-09-18 19:04:59 -07001349 case EVENT_SET_CALL_WAITING_DONE: {
Shuo Qian4a594052020-01-23 11:59:30 -08001350 ar = (AsyncResult) msg.obj;
1351 request = (MainThreadRequest) ar.userObj;
Hall Liu27d24262020-09-18 19:04:59 -07001352 boolean enable = ((Pair<Boolean, Consumer<Integer>>) request.argument).first;
1353 Consumer<Integer> callback =
1354 ((Pair<Boolean, Consumer<Integer>>) request.argument).second;
1355 if (ar.exception != null) {
Shuo Qian4a594052020-01-23 11:59:30 -08001356 loge("setCallWaiting exception: " + ar.exception);
Hall Liu27d24262020-09-18 19:04:59 -07001357 if (ar.exception instanceof CommandException) {
1358 CommandException.Error error =
1359 ((CommandException) (ar.exception)).getCommandError();
1360 if (error == CommandException.Error.REQUEST_NOT_SUPPORTED) {
1361 callback.accept(TelephonyManager.CALL_WAITING_STATUS_NOT_SUPPORTED);
SongFerngWangebda2c52022-01-11 15:28:38 +08001362 } else if (error == CommandException.Error.FDN_CHECK_FAILURE) {
1363 callback.accept(
1364 TelephonyManager.CALL_WAITING_STATUS_FDN_CHECK_FAILURE);
Hall Liu27d24262020-09-18 19:04:59 -07001365 } else {
1366 callback.accept(TelephonyManager.CALL_WAITING_STATUS_UNKNOWN_ERROR);
1367 }
1368 } else {
1369 callback.accept(TelephonyManager.CALL_WAITING_STATUS_UNKNOWN_ERROR);
1370 }
1371 } else {
1372 callback.accept(enable ? TelephonyManager.CALL_WAITING_STATUS_ENABLED
1373 : TelephonyManager.CALL_WAITING_STATUS_DISABLED);
Shuo Qian4a594052020-01-23 11:59:30 -08001374 }
Shuo Qian4a594052020-01-23 11:59:30 -08001375 break;
Hall Liu27d24262020-09-18 19:04:59 -07001376 }
Shishir Agrawal302c8692015-06-19 13:49:39 -07001377 case EVENT_PERFORM_NETWORK_SCAN_DONE:
1378 ar = (AsyncResult) msg.obj;
1379 request = (MainThreadRequest) ar.userObj;
1380 CellNetworkScanResult cellScanResult;
1381 if (ar.exception == null && ar.result != null) {
1382 cellScanResult = new CellNetworkScanResult(
1383 CellNetworkScanResult.STATUS_SUCCESS,
1384 (List<OperatorInfo>) ar.result);
1385 } else {
1386 if (ar.result == null) {
1387 loge("getCellNetworkScanResults: Empty response");
1388 }
1389 if (ar.exception != null) {
1390 loge("getCellNetworkScanResults: Exception: " + ar.exception);
1391 }
1392 int errorCode = CellNetworkScanResult.STATUS_UNKNOWN_ERROR;
1393 if (ar.exception instanceof CommandException) {
1394 CommandException.Error error =
1395 ((CommandException) (ar.exception)).getCommandError();
1396 if (error == CommandException.Error.RADIO_NOT_AVAILABLE) {
1397 errorCode = CellNetworkScanResult.STATUS_RADIO_NOT_AVAILABLE;
1398 } else if (error == CommandException.Error.GENERIC_FAILURE) {
1399 errorCode = CellNetworkScanResult.STATUS_RADIO_GENERIC_FAILURE;
1400 }
1401 }
1402 cellScanResult = new CellNetworkScanResult(errorCode, null);
1403 }
1404 request.result = cellScanResult;
Pengquan Menga1bb6272018-09-06 09:59:22 -07001405 notifyRequester(request);
Shishir Agrawal302c8692015-06-19 13:49:39 -07001406 break;
1407
1408 case CMD_SET_NETWORK_SELECTION_MODE_MANUAL:
1409 request = (MainThreadRequest) msg.obj;
Shishir Agrawal77ba3172015-09-10 14:50:19 -07001410 ManualNetworkSelectionArgument selArg =
1411 (ManualNetworkSelectionArgument) request.argument;
Shishir Agrawal302c8692015-06-19 13:49:39 -07001412 onCompleted = obtainMessage(EVENT_SET_NETWORK_SELECTION_MODE_MANUAL_DONE,
1413 request);
Shishir Agrawal77ba3172015-09-10 14:50:19 -07001414 getPhoneFromRequest(request).selectNetworkManually(selArg.operatorInfo,
1415 selArg.persistSelection, onCompleted);
Shishir Agrawal302c8692015-06-19 13:49:39 -07001416 break;
1417
1418 case EVENT_SET_NETWORK_SELECTION_MODE_MANUAL_DONE:
Pengquan Menge3d01e22018-09-20 15:25:35 -07001419 ar = (AsyncResult) msg.obj;
1420 request = (MainThreadRequest) ar.userObj;
1421 if (ar.exception == null) {
1422 request.result = true;
1423 } else {
1424 request.result = false;
1425 loge("setNetworkSelectionModeManual " + ar.exception);
1426 }
1427 notifyRequester(request);
1428 mApp.onNetworkSelectionChanged(request.subId);
Shishir Agrawal302c8692015-06-19 13:49:39 -07001429 break;
1430
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -07001431 case CMD_GET_MODEM_ACTIVITY_INFO:
1432 request = (MainThreadRequest) msg.obj;
1433 onCompleted = obtainMessage(EVENT_GET_MODEM_ACTIVITY_INFO_DONE, request);
James Mattisab947702019-04-03 14:18:34 -07001434 if (defaultPhone != null) {
1435 defaultPhone.getModemActivityInfo(onCompleted, request.workSource);
Shuo Qian8f4750a2020-02-20 17:12:10 -08001436 } else {
1437 ResultReceiver result = (ResultReceiver) request.argument;
1438 Bundle bundle = new Bundle();
1439 bundle.putParcelable(TelephonyManager.MODEM_ACTIVITY_RESULT_KEY,
Hall Liu49656c02020-10-09 19:00:11 -07001440 new ModemActivityInfo(0, 0, 0,
1441 new int[ModemActivityInfo.getNumTxPowerLevels()], 0));
Shuo Qian8f4750a2020-02-20 17:12:10 -08001442 result.send(0, bundle);
James Mattisab947702019-04-03 14:18:34 -07001443 }
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -07001444 break;
1445
Hall Liud0f208c2020-10-14 16:54:44 -07001446 case EVENT_GET_MODEM_ACTIVITY_INFO_DONE: {
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -07001447 ar = (AsyncResult) msg.obj;
1448 request = (MainThreadRequest) ar.userObj;
Shuo Qian8f4750a2020-02-20 17:12:10 -08001449 ResultReceiver result = (ResultReceiver) request.argument;
Hall Liud0f208c2020-10-14 16:54:44 -07001450 int error = 0;
Kai Shi917fdc62022-11-28 14:01:02 -08001451 ModemActivityInfo ret = null;
Gary Jian3aa9a762022-01-24 16:41:19 +08001452 if (mLastModemActivityInfo == null) {
1453 mLastModemActivitySpecificInfo = new ActivityStatsTechSpecificInfo[1];
1454 mLastModemActivitySpecificInfo[0] =
1455 new ActivityStatsTechSpecificInfo(
1456 0,
1457 0,
1458 new int[ModemActivityInfo.getNumTxPowerLevels()],
1459 0);
1460 mLastModemActivityInfo =
1461 new ModemActivityInfo(0, 0, 0, mLastModemActivitySpecificInfo);
1462 }
1463
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -07001464 if (ar.exception == null && ar.result != null) {
Shuo Qian8f4750a2020-02-20 17:12:10 -08001465 // Update the last modem activity info and the result of the request.
1466 ModemActivityInfo info = (ModemActivityInfo) ar.result;
1467 if (isModemActivityInfoValid(info)) {
Gary Jian3aa9a762022-01-24 16:41:19 +08001468 mergeModemActivityInfo(info);
Shuo Qian8f4750a2020-02-20 17:12:10 -08001469 }
Kai Shi917fdc62022-11-28 14:01:02 -08001470 // This is needed to decouple ret from mLastModemActivityInfo
1471 // We don't want to return mLastModemActivityInfo which is updated
1472 // inside mergeModemActivityInfo()
1473 ret = new ModemActivityInfo(
1474 mLastModemActivityInfo.getTimestampMillis(),
1475 mLastModemActivityInfo.getSleepTimeMillis(),
1476 mLastModemActivityInfo.getIdleTimeMillis(),
1477 deepCopyModemActivitySpecificInfo(mLastModemActivitySpecificInfo));
Gary Jian3aa9a762022-01-24 16:41:19 +08001478
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -07001479 } else {
1480 if (ar.result == null) {
1481 loge("queryModemActivityInfo: Empty response");
Hall Liud0f208c2020-10-14 16:54:44 -07001482 error = TelephonyManager.ModemActivityInfoException
1483 .ERROR_INVALID_INFO_RECEIVED;
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -07001484 } else if (ar.exception instanceof CommandException) {
Gary Jian3aa9a762022-01-24 16:41:19 +08001485 loge("queryModemActivityInfo: CommandException: " + ar.exception);
Hall Liud0f208c2020-10-14 16:54:44 -07001486 error = TelephonyManager.ModemActivityInfoException
1487 .ERROR_MODEM_RESPONSE_ERROR;
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -07001488 } else {
1489 loge("queryModemActivityInfo: Unknown exception");
Hall Liud0f208c2020-10-14 16:54:44 -07001490 error = TelephonyManager.ModemActivityInfoException
1491 .ERROR_UNKNOWN;
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -07001492 }
1493 }
Shuo Qian8f4750a2020-02-20 17:12:10 -08001494 Bundle bundle = new Bundle();
Kai Shi917fdc62022-11-28 14:01:02 -08001495 if (ret != null) {
Gary Jian3aa9a762022-01-24 16:41:19 +08001496 bundle.putParcelable(
1497 TelephonyManager.MODEM_ACTIVITY_RESULT_KEY,
Kai Shi917fdc62022-11-28 14:01:02 -08001498 ret);
Hall Liud0f208c2020-10-14 16:54:44 -07001499 } else {
1500 bundle.putInt(TelephonyManager.EXCEPTION_RESULT_KEY, error);
1501 }
Shuo Qian8f4750a2020-02-20 17:12:10 -08001502 result.send(0, bundle);
Pengquan Menga1bb6272018-09-06 09:59:22 -07001503 notifyRequester(request);
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -07001504 break;
Hall Liud0f208c2020-10-14 16:54:44 -07001505 }
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -07001506
Meng Wang1a7c35a2016-05-05 20:56:15 -07001507 case CMD_SET_ALLOWED_CARRIERS:
1508 request = (MainThreadRequest) msg.obj;
Michele Berionne482f8202018-11-27 18:57:59 -08001509 CarrierRestrictionRules argument =
1510 (CarrierRestrictionRules) request.argument;
Meng Wang1a7c35a2016-05-05 20:56:15 -07001511 onCompleted = obtainMessage(EVENT_SET_ALLOWED_CARRIERS_DONE, request);
Michele Berionne482f8202018-11-27 18:57:59 -08001512 defaultPhone.setAllowedCarriers(argument, onCompleted, request.workSource);
Meng Wang1a7c35a2016-05-05 20:56:15 -07001513 break;
1514
1515 case EVENT_SET_ALLOWED_CARRIERS_DONE:
1516 ar = (AsyncResult) msg.obj;
1517 request = (MainThreadRequest) ar.userObj;
1518 if (ar.exception == null && ar.result != null) {
1519 request.result = ar.result;
1520 } else {
Michele Berionne482f8202018-11-27 18:57:59 -08001521 request.result = TelephonyManager.SET_CARRIER_RESTRICTION_ERROR;
1522 if (ar.exception instanceof CommandException) {
1523 loge("setAllowedCarriers: CommandException: " + ar.exception);
1524 CommandException.Error error =
1525 ((CommandException) (ar.exception)).getCommandError();
1526 if (error == CommandException.Error.REQUEST_NOT_SUPPORTED) {
1527 request.result =
1528 TelephonyManager.SET_CARRIER_RESTRICTION_NOT_SUPPORTED;
1529 }
Meng Wang1a7c35a2016-05-05 20:56:15 -07001530 } else {
1531 loge("setAllowedCarriers: Unknown exception");
1532 }
1533 }
Pengquan Menga1bb6272018-09-06 09:59:22 -07001534 notifyRequester(request);
Meng Wang1a7c35a2016-05-05 20:56:15 -07001535 break;
1536
1537 case CMD_GET_ALLOWED_CARRIERS:
1538 request = (MainThreadRequest) msg.obj;
1539 onCompleted = obtainMessage(EVENT_GET_ALLOWED_CARRIERS_DONE, request);
Nazanin Bakhshif782e562018-12-11 15:15:39 -08001540 defaultPhone.getAllowedCarriers(onCompleted, request.workSource);
Meng Wang1a7c35a2016-05-05 20:56:15 -07001541 break;
1542
1543 case EVENT_GET_ALLOWED_CARRIERS_DONE:
1544 ar = (AsyncResult) msg.obj;
1545 request = (MainThreadRequest) ar.userObj;
1546 if (ar.exception == null && ar.result != null) {
1547 request.result = ar.result;
1548 } else {
Michele Berionne482f8202018-11-27 18:57:59 -08001549 request.result = new IllegalStateException(
1550 "Failed to get carrier restrictions");
Meng Wang1a7c35a2016-05-05 20:56:15 -07001551 if (ar.result == null) {
1552 loge("getAllowedCarriers: Empty response");
1553 } else if (ar.exception instanceof CommandException) {
1554 loge("getAllowedCarriers: CommandException: " +
1555 ar.exception);
1556 } else {
1557 loge("getAllowedCarriers: Unknown exception");
1558 }
1559 }
Pengquan Menga1bb6272018-09-06 09:59:22 -07001560 notifyRequester(request);
Meng Wang1a7c35a2016-05-05 20:56:15 -07001561 break;
1562
Nathan Haroldb3014052017-01-25 15:57:32 -08001563 case EVENT_GET_FORBIDDEN_PLMNS_DONE:
1564 ar = (AsyncResult) msg.obj;
1565 request = (MainThreadRequest) ar.userObj;
1566 if (ar.exception == null && ar.result != null) {
1567 request.result = ar.result;
1568 } else {
1569 request.result = new IllegalArgumentException(
1570 "Failed to retrieve Forbidden Plmns");
1571 if (ar.result == null) {
1572 loge("getForbiddenPlmns: Empty response");
1573 } else {
1574 loge("getForbiddenPlmns: Unknown exception");
1575 }
1576 }
Pengquan Menga1bb6272018-09-06 09:59:22 -07001577 notifyRequester(request);
Nathan Haroldb3014052017-01-25 15:57:32 -08001578 break;
1579
1580 case CMD_GET_FORBIDDEN_PLMNS:
1581 request = (MainThreadRequest) msg.obj;
Muralidhar Reddy472c2ae2021-09-29 19:38:40 +00001582 uiccPort = getUiccPortFromRequest(request);
1583 if (uiccPort == null) {
1584 loge("getForbiddenPlmns() UiccPort is null");
Nathan Haroldb3014052017-01-25 15:57:32 -08001585 request.result = new IllegalArgumentException(
Muralidhar Reddy472c2ae2021-09-29 19:38:40 +00001586 "getForbiddenPlmns() UiccPort is null");
Pengquan Menga1bb6272018-09-06 09:59:22 -07001587 notifyRequester(request);
Nathan Haroldb3014052017-01-25 15:57:32 -08001588 break;
1589 }
1590 Integer appType = (Integer) request.argument;
Muralidhar Reddy472c2ae2021-09-29 19:38:40 +00001591 UiccCardApplication uiccApp = uiccPort.getApplicationByType(appType);
Nathan Haroldb3014052017-01-25 15:57:32 -08001592 if (uiccApp == null) {
1593 loge("getForbiddenPlmns() no app with specified type -- "
1594 + appType);
1595 request.result = new IllegalArgumentException("Failed to get UICC App");
Pengquan Menga1bb6272018-09-06 09:59:22 -07001596 notifyRequester(request);
Nathan Haroldb3014052017-01-25 15:57:32 -08001597 break;
1598 } else {
1599 if (DBG) logv("getForbiddenPlmns() found app " + uiccApp.getAid()
1600 + " specified type -- " + appType);
1601 }
1602 onCompleted = obtainMessage(EVENT_GET_FORBIDDEN_PLMNS_DONE, request);
1603 ((SIMRecords) uiccApp.getIccRecords()).getForbiddenPlmns(
1604 onCompleted);
1605 break;
1606
Holly Jiuyu Sun01c47ad2018-01-24 17:56:33 +00001607 case CMD_SWITCH_SLOTS:
1608 request = (MainThreadRequest) msg.obj;
Muralidhar Reddyeb809e32021-11-19 03:07:54 +00001609 List<UiccSlotMapping> slotMapping = (List<UiccSlotMapping>) request.argument;
Holly Jiuyu Sun01c47ad2018-01-24 17:56:33 +00001610 onCompleted = obtainMessage(EVENT_SWITCH_SLOTS_DONE, request);
Muralidhar Reddyeb809e32021-11-19 03:07:54 +00001611 UiccController.getInstance().switchSlots(slotMapping, onCompleted);
Holly Jiuyu Sun01c47ad2018-01-24 17:56:33 +00001612 break;
1613
1614 case EVENT_SWITCH_SLOTS_DONE:
1615 ar = (AsyncResult) msg.obj;
1616 request = (MainThreadRequest) ar.userObj;
1617 request.result = (ar.exception == null);
Pengquan Menga1bb6272018-09-06 09:59:22 -07001618 notifyRequester(request);
1619 break;
1620 case CMD_GET_NETWORK_SELECTION_MODE:
1621 request = (MainThreadRequest) msg.obj;
1622 onCompleted = obtainMessage(EVENT_GET_NETWORK_SELECTION_MODE_DONE, request);
1623 getPhoneFromRequest(request).getNetworkSelectionMode(onCompleted);
1624 break;
1625
1626 case EVENT_GET_NETWORK_SELECTION_MODE_DONE:
1627 ar = (AsyncResult) msg.obj;
1628 request = (MainThreadRequest) ar.userObj;
1629 if (ar.exception != null) {
1630 request.result = TelephonyManager.NETWORK_SELECTION_MODE_UNKNOWN;
1631 } else {
1632 int mode = ((int[]) ar.result)[0];
1633 if (mode == 0) {
1634 request.result = TelephonyManager.NETWORK_SELECTION_MODE_AUTO;
1635 } else {
1636 request.result = TelephonyManager.NETWORK_SELECTION_MODE_MANUAL;
1637 }
Holly Jiuyu Sun01c47ad2018-01-24 17:56:33 +00001638 }
Pengquan Menga1bb6272018-09-06 09:59:22 -07001639 notifyRequester(request);
1640 break;
1641 case CMD_GET_CDMA_ROAMING_MODE:
1642 request = (MainThreadRequest) msg.obj;
1643 onCompleted = obtainMessage(EVENT_GET_CDMA_ROAMING_MODE_DONE, request);
1644 getPhoneFromRequest(request).queryCdmaRoamingPreference(onCompleted);
1645 break;
1646 case EVENT_GET_CDMA_ROAMING_MODE_DONE:
1647 ar = (AsyncResult) msg.obj;
1648 request = (MainThreadRequest) ar.userObj;
1649 if (ar.exception != null) {
1650 request.result = TelephonyManager.CDMA_ROAMING_MODE_RADIO_DEFAULT;
1651 } else {
1652 request.result = ((int[]) ar.result)[0];
1653 }
1654 notifyRequester(request);
1655 break;
1656 case CMD_SET_CDMA_ROAMING_MODE:
1657 request = (MainThreadRequest) msg.obj;
1658 onCompleted = obtainMessage(EVENT_SET_CDMA_ROAMING_MODE_DONE, request);
1659 int mode = (int) request.argument;
1660 getPhoneFromRequest(request).setCdmaRoamingPreference(mode, onCompleted);
1661 break;
1662 case EVENT_SET_CDMA_ROAMING_MODE_DONE:
1663 ar = (AsyncResult) msg.obj;
1664 request = (MainThreadRequest) ar.userObj;
1665 request.result = ar.exception == null;
1666 notifyRequester(request);
1667 break;
Sarah Chinbaab1432020-10-28 13:46:24 -07001668 case CMD_GET_CDMA_SUBSCRIPTION_MODE:
1669 request = (MainThreadRequest) msg.obj;
1670 onCompleted = obtainMessage(EVENT_GET_CDMA_SUBSCRIPTION_MODE_DONE, request);
1671 getPhoneFromRequest(request).queryCdmaSubscriptionMode(onCompleted);
1672 break;
1673 case EVENT_GET_CDMA_SUBSCRIPTION_MODE_DONE:
1674 ar = (AsyncResult) msg.obj;
1675 request = (MainThreadRequest) ar.userObj;
1676 if (ar.exception != null) {
1677 request.result = TelephonyManager.CDMA_SUBSCRIPTION_RUIM_SIM;
1678 } else {
1679 request.result = ((int[]) ar.result)[0];
1680 }
1681 notifyRequester(request);
1682 break;
Pengquan Menga1bb6272018-09-06 09:59:22 -07001683 case CMD_SET_CDMA_SUBSCRIPTION_MODE:
1684 request = (MainThreadRequest) msg.obj;
1685 onCompleted = obtainMessage(EVENT_SET_CDMA_SUBSCRIPTION_MODE_DONE, request);
1686 int subscriptionMode = (int) request.argument;
Sarah Chinbaab1432020-10-28 13:46:24 -07001687 getPhoneFromRequest(request).setCdmaSubscriptionMode(
1688 subscriptionMode, onCompleted);
Pengquan Menga1bb6272018-09-06 09:59:22 -07001689 break;
1690 case EVENT_SET_CDMA_SUBSCRIPTION_MODE_DONE:
1691 ar = (AsyncResult) msg.obj;
1692 request = (MainThreadRequest) ar.userObj;
1693 request.result = ar.exception == null;
1694 notifyRequester(request);
Holly Jiuyu Sun01c47ad2018-01-24 17:56:33 +00001695 break;
Nathan Harold3ff88932018-08-14 10:19:49 -07001696 case CMD_GET_ALL_CELL_INFO:
1697 request = (MainThreadRequest) msg.obj;
Nathan Harold3ff88932018-08-14 10:19:49 -07001698 onCompleted = obtainMessage(EVENT_GET_ALL_CELL_INFO_DONE, request);
Nathan Harold92bed182018-10-12 18:16:49 -07001699 request.phone.requestCellInfoUpdate(request.workSource, onCompleted);
Nathan Harold3ff88932018-08-14 10:19:49 -07001700 break;
Nathan Harold3ff88932018-08-14 10:19:49 -07001701 case EVENT_GET_ALL_CELL_INFO_DONE:
1702 ar = (AsyncResult) msg.obj;
1703 request = (MainThreadRequest) ar.userObj;
Nathan Harold8d0f1742018-10-02 12:14:47 -07001704 // If a timeout occurs, the response will be null
1705 request.result = (ar.exception == null && ar.result != null)
1706 ? ar.result : new ArrayList<CellInfo>();
Nathan Harold3ff88932018-08-14 10:19:49 -07001707 synchronized (request) {
1708 request.notifyAll();
1709 }
1710 break;
Nathan Haroldfa8da0f2018-09-27 18:51:29 -07001711 case CMD_REQUEST_CELL_INFO_UPDATE:
1712 request = (MainThreadRequest) msg.obj;
1713 request.phone.requestCellInfoUpdate(request.workSource,
1714 obtainMessage(EVENT_REQUEST_CELL_INFO_UPDATE_DONE, request));
1715 break;
1716 case EVENT_REQUEST_CELL_INFO_UPDATE_DONE:
1717 ar = (AsyncResult) msg.obj;
1718 request = (MainThreadRequest) ar.userObj;
1719 ICellInfoCallback cb = (ICellInfoCallback) request.argument;
1720 try {
1721 if (ar.exception != null) {
Nathan Haroldfa8da0f2018-09-27 18:51:29 -07001722 Log.e(LOG_TAG, "Exception retrieving CellInfo=" + ar.exception);
Meng Wangd8921f42019-09-30 17:13:54 -07001723 cb.onError(
1724 TelephonyManager.CellInfoCallback.ERROR_MODEM_ERROR,
1725 ar.exception.getClass().getName(),
1726 ar.exception.toString());
Nathan Haroldfa8da0f2018-09-27 18:51:29 -07001727 } else if (ar.result == null) {
Nathan Haroldfa8da0f2018-09-27 18:51:29 -07001728 Log.w(LOG_TAG, "Timeout Waiting for CellInfo!");
Meng Wangd8921f42019-09-30 17:13:54 -07001729 cb.onError(TelephonyManager.CellInfoCallback.ERROR_TIMEOUT, null, null);
Nathan Haroldfa8da0f2018-09-27 18:51:29 -07001730 } else {
1731 // use the result as returned
1732 cb.onCellInfo((List<CellInfo>) ar.result);
1733 }
1734 } catch (RemoteException re) {
1735 Log.w(LOG_TAG, "Discarded CellInfo due to Callback RemoteException");
1736 }
1737 break;
Sarah Chin679c08a2020-11-18 13:39:35 -08001738 case CMD_GET_CELL_LOCATION: {
Nathan Harold3ff88932018-08-14 10:19:49 -07001739 request = (MainThreadRequest) msg.obj;
1740 WorkSource ws = (WorkSource) request.argument;
1741 Phone phone = getPhoneFromRequest(request);
Meng Wanga10e89e2019-12-09 13:13:01 -08001742 phone.getCellIdentity(ws, obtainMessage(EVENT_GET_CELL_LOCATION_DONE, request));
Nathan Harold3ff88932018-08-14 10:19:49 -07001743 break;
Sarah Chin679c08a2020-11-18 13:39:35 -08001744 }
1745 case EVENT_GET_CELL_LOCATION_DONE: {
Nathan Harold3ff88932018-08-14 10:19:49 -07001746 ar = (AsyncResult) msg.obj;
1747 request = (MainThreadRequest) ar.userObj;
1748 if (ar.exception == null) {
1749 request.result = ar.result;
1750 } else {
Sarah Chin679c08a2020-11-18 13:39:35 -08001751 Phone phone = getPhoneFromRequest(request);
Nathan Harold3ff88932018-08-14 10:19:49 -07001752 request.result = (phone.getPhoneType() == PhoneConstants.PHONE_TYPE_CDMA)
Meng Wanga10e89e2019-12-09 13:13:01 -08001753 ? new CellIdentityCdma() : new CellIdentityGsm();
Nathan Harold3ff88932018-08-14 10:19:49 -07001754 }
1755
1756 synchronized (request) {
1757 request.notifyAll();
1758 }
1759 break;
Sarah Chin679c08a2020-11-18 13:39:35 -08001760 }
chen xu6dac5ab2018-10-26 17:39:23 -07001761 case CMD_MODEM_REBOOT:
1762 request = (MainThreadRequest) msg.obj;
1763 onCompleted = obtainMessage(EVENT_RESET_MODEM_CONFIG_DONE, request);
Nazanin Bakhshif782e562018-12-11 15:15:39 -08001764 defaultPhone.rebootModem(onCompleted);
chen xu6dac5ab2018-10-26 17:39:23 -07001765 break;
chen xu6dac5ab2018-10-26 17:39:23 -07001766 case EVENT_CMD_MODEM_REBOOT_DONE:
1767 handleNullReturnEvent(msg, "rebootModem");
1768 break;
Malcolm Chen8e4ed912019-01-15 20:22:16 -08001769 case CMD_REQUEST_ENABLE_MODEM:
1770 request = (MainThreadRequest) msg.obj;
1771 boolean enable = (boolean) request.argument;
1772 onCompleted = obtainMessage(EVENT_ENABLE_MODEM_DONE, request);
Nazanin Bakhshi33d584b2019-02-27 10:44:32 -08001773 onCompleted.arg1 = enable ? 1 : 0;
Malcolm Chen8e4ed912019-01-15 20:22:16 -08001774 PhoneConfigurationManager.getInstance()
1775 .enablePhone(request.phone, enable, onCompleted);
1776 break;
Michele Berionne5e411512020-11-13 02:36:59 +00001777 case EVENT_ENABLE_MODEM_DONE: {
Malcolm Chen8e4ed912019-01-15 20:22:16 -08001778 ar = (AsyncResult) msg.obj;
1779 request = (MainThreadRequest) ar.userObj;
1780 request.result = (ar.exception == null);
Nazanin Bakhshif71371d2019-04-29 17:29:44 -07001781 int phoneId = request.phone.getPhoneId();
Nazanin Bakhshi33d584b2019-02-27 10:44:32 -08001782 //update the cache as modem status has changed
Nazanin Bakhshif71371d2019-04-29 17:29:44 -07001783 if ((boolean) request.result) {
1784 mPhoneConfigurationManager.addToPhoneStatusCache(phoneId, msg.arg1 == 1);
1785 updateModemStateMetrics();
1786 } else {
1787 Log.e(LOG_TAG, msg.what + " failure. Not updating modem status."
1788 + ar.exception);
1789 }
1790 notifyRequester(request);
1791 break;
Michele Berionne5e411512020-11-13 02:36:59 +00001792 }
Nazanin Bakhshif71371d2019-04-29 17:29:44 -07001793 case CMD_GET_MODEM_STATUS:
1794 request = (MainThreadRequest) msg.obj;
1795 onCompleted = obtainMessage(EVENT_GET_MODEM_STATUS_DONE, request);
1796 PhoneConfigurationManager.getInstance()
1797 .getPhoneStatusFromModem(request.phone, onCompleted);
1798 break;
1799 case EVENT_GET_MODEM_STATUS_DONE:
1800 ar = (AsyncResult) msg.obj;
1801 request = (MainThreadRequest) ar.userObj;
1802 int id = request.phone.getPhoneId();
1803 if (ar.exception == null && ar.result != null) {
1804 request.result = ar.result;
1805 //update the cache as modem status has changed
1806 mPhoneConfigurationManager.addToPhoneStatusCache(id,
1807 (boolean) request.result);
1808 } else {
1809 // Return true if modem status cannot be retrieved. For most cases,
1810 // modem status is on. And for older version modems, GET_MODEM_STATUS
1811 // and disable modem are not supported. Modem is always on.
1812 // TODO: this should be fixed in R to support a third
1813 // status UNKNOWN b/131631629
1814 request.result = true;
1815 Log.e(LOG_TAG, msg.what + " failure. Not updating modem status."
1816 + ar.exception);
1817 }
Malcolm Chen8e4ed912019-01-15 20:22:16 -08001818 notifyRequester(request);
1819 break;
Hall Liu73f5d362020-01-20 13:42:00 -08001820 case CMD_SET_SYSTEM_SELECTION_CHANNELS: {
1821 request = (MainThreadRequest) msg.obj;
1822 onCompleted = obtainMessage(EVENT_SET_SYSTEM_SELECTION_CHANNELS_DONE, request);
1823 Pair<List<RadioAccessSpecifier>, Consumer<Boolean>> args =
1824 (Pair<List<RadioAccessSpecifier>, Consumer<Boolean>>) request.argument;
1825 request.phone.setSystemSelectionChannels(args.first, onCompleted);
1826 break;
1827 }
1828 case EVENT_SET_SYSTEM_SELECTION_CHANNELS_DONE: {
1829 ar = (AsyncResult) msg.obj;
1830 request = (MainThreadRequest) ar.userObj;
1831 Pair<List<RadioAccessSpecifier>, Consumer<Boolean>> args =
1832 (Pair<List<RadioAccessSpecifier>, Consumer<Boolean>>) request.argument;
1833 args.second.accept(ar.exception == null);
1834 notifyRequester(request);
1835 break;
1836 }
Sarah Chin679c08a2020-11-18 13:39:35 -08001837 case CMD_GET_SYSTEM_SELECTION_CHANNELS: {
1838 request = (MainThreadRequest) msg.obj;
1839 onCompleted = obtainMessage(EVENT_GET_SYSTEM_SELECTION_CHANNELS_DONE, request);
1840 Phone phone = getPhoneFromRequest(request);
1841 if (phone != null) {
1842 phone.getSystemSelectionChannels(onCompleted);
1843 } else {
1844 loge("getSystemSelectionChannels: No phone object");
1845 request.result = new ArrayList<RadioAccessSpecifier>();
1846 notifyRequester(request);
1847 }
1848 break;
1849 }
1850 case EVENT_GET_SYSTEM_SELECTION_CHANNELS_DONE:
1851 ar = (AsyncResult) msg.obj;
1852 request = (MainThreadRequest) ar.userObj;
1853 if (ar.exception == null && ar.result != null) {
1854 request.result = ar.result;
1855 } else {
Sarah Chin428d1d62021-03-13 03:17:40 -08001856 request.result = new IllegalStateException(
1857 "Failed to retrieve system selecton channels");
Sarah Chin679c08a2020-11-18 13:39:35 -08001858 if (ar.result == null) {
1859 loge("getSystemSelectionChannels: Empty response");
1860 } else {
1861 loge("getSystemSelectionChannels: Unknown exception");
1862 }
1863 }
1864 notifyRequester(request);
1865 break;
yincheng zhao2737e882019-09-06 17:06:54 -07001866 case EVENT_SET_FORBIDDEN_PLMNS_DONE:
1867 ar = (AsyncResult) msg.obj;
1868 request = (MainThreadRequest) ar.userObj;
1869 if (ar.exception == null && ar.result != null) {
1870 request.result = ar.result;
1871 } else {
1872 request.result = -1;
1873 loge("Failed to set Forbidden Plmns");
1874 if (ar.result == null) {
1875 loge("setForbidenPlmns: Empty response");
1876 } else if (ar.exception != null) {
1877 loge("setForbiddenPlmns: Exception: " + ar.exception);
1878 request.result = -1;
1879 } else {
1880 loge("setForbiddenPlmns: Unknown exception");
1881 }
1882 }
1883 notifyRequester(request);
1884 break;
1885 case CMD_SET_FORBIDDEN_PLMNS:
1886 request = (MainThreadRequest) msg.obj;
Muralidhar Reddy472c2ae2021-09-29 19:38:40 +00001887 uiccPort = getUiccPortFromRequest(request);
1888 if (uiccPort == null) {
1889 loge("setForbiddenPlmns: UiccPort is null");
yincheng zhao2737e882019-09-06 17:06:54 -07001890 request.result = -1;
1891 notifyRequester(request);
1892 break;
1893 }
1894 Pair<Integer, List<String>> setFplmnsArgs =
1895 (Pair<Integer, List<String>>) request.argument;
1896 appType = setFplmnsArgs.first;
1897 List<String> fplmns = setFplmnsArgs.second;
Muralidhar Reddy472c2ae2021-09-29 19:38:40 +00001898 uiccApp = uiccPort.getApplicationByType(appType);
yincheng zhao2737e882019-09-06 17:06:54 -07001899 if (uiccApp == null) {
1900 loge("setForbiddenPlmns: no app with specified type -- " + appType);
1901 request.result = -1;
1902 loge("Failed to get UICC App");
1903 notifyRequester(request);
1904 } else {
1905 onCompleted = obtainMessage(EVENT_SET_FORBIDDEN_PLMNS_DONE, request);
1906 ((SIMRecords) uiccApp.getIccRecords())
1907 .setForbiddenPlmns(onCompleted, fplmns);
1908 }
yinchengzhao4d163c02019-12-12 15:21:47 -08001909 break;
Naina Nallurid63128d2019-09-17 14:10:30 -07001910 case CMD_ERASE_MODEM_CONFIG:
1911 request = (MainThreadRequest) msg.obj;
1912 onCompleted = obtainMessage(EVENT_ERASE_MODEM_CONFIG_DONE, request);
1913 defaultPhone.eraseModemConfig(onCompleted);
1914 break;
1915 case EVENT_ERASE_MODEM_CONFIG_DONE:
1916 handleNullReturnEvent(msg, "eraseModemConfig");
yincheng zhao2737e882019-09-06 17:06:54 -07001917 break;
zoey chene02881a2019-12-30 16:11:23 +08001918
Kai Shif70f46f2021-03-03 13:59:46 -08001919 case CMD_ERASE_DATA_SHARED_PREFERENCES:
1920 request = (MainThreadRequest) msg.obj;
1921 request.result = defaultPhone.eraseDataInSharedPreferences();
1922 notifyRequester(request);
1923 break;
1924
zoey chene02881a2019-12-30 16:11:23 +08001925 case CMD_CHANGE_ICC_LOCK_PASSWORD:
1926 request = (MainThreadRequest) msg.obj;
1927 onCompleted = obtainMessage(EVENT_CHANGE_ICC_LOCK_PASSWORD_DONE, request);
1928 Pair<String, String> changed = (Pair<String, String>) request.argument;
1929 getPhoneFromRequest(request).getIccCard().changeIccLockPassword(
1930 changed.first, changed.second, onCompleted);
1931 break;
1932 case EVENT_CHANGE_ICC_LOCK_PASSWORD_DONE:
1933 ar = (AsyncResult) msg.obj;
1934 request = (MainThreadRequest) ar.userObj;
1935 if (ar.exception == null) {
1936 request.result = TelephonyManager.CHANGE_ICC_LOCK_SUCCESS;
Michele Berionne5e411512020-11-13 02:36:59 +00001937 // If the operation is successful, update the PIN storage
1938 Pair<String, String> passwords = (Pair<String, String>) request.argument;
1939 int phoneId = getPhoneFromRequest(request).getPhoneId();
Jon Spivack9c3bc762021-10-06 20:53:09 +00001940 UiccController.getInstance().getPinStorage()
1941 .storePin(passwords.second, phoneId);
zoey chene02881a2019-12-30 16:11:23 +08001942 } else {
1943 request.result = msg.arg1;
1944 }
1945 notifyRequester(request);
1946 break;
1947
Michele Berionne5e411512020-11-13 02:36:59 +00001948 case CMD_SET_ICC_LOCK_ENABLED: {
zoey chene02881a2019-12-30 16:11:23 +08001949 request = (MainThreadRequest) msg.obj;
1950 onCompleted = obtainMessage(EVENT_SET_ICC_LOCK_ENABLED_DONE, request);
1951 Pair<Boolean, String> enabled = (Pair<Boolean, String>) request.argument;
1952 getPhoneFromRequest(request).getIccCard().setIccLockEnabled(
1953 enabled.first, enabled.second, onCompleted);
1954 break;
Michele Berionne5e411512020-11-13 02:36:59 +00001955 }
zoey chene02881a2019-12-30 16:11:23 +08001956 case EVENT_SET_ICC_LOCK_ENABLED_DONE:
1957 ar = (AsyncResult) msg.obj;
1958 request = (MainThreadRequest) ar.userObj;
1959 if (ar.exception == null) {
1960 request.result = TelephonyManager.CHANGE_ICC_LOCK_SUCCESS;
Michele Berionne5e411512020-11-13 02:36:59 +00001961 // If the operation is successful, update the PIN storage
1962 Pair<Boolean, String> enabled = (Pair<Boolean, String>) request.argument;
1963 int phoneId = getPhoneFromRequest(request).getPhoneId();
1964 if (enabled.first) {
Jon Spivack9c3bc762021-10-06 20:53:09 +00001965 UiccController.getInstance().getPinStorage()
1966 .storePin(enabled.second, phoneId);
Michele Berionne5e411512020-11-13 02:36:59 +00001967 } else {
1968 UiccController.getInstance().getPinStorage().clearPin(phoneId);
1969 }
zoey chene02881a2019-12-30 16:11:23 +08001970 } else {
1971 request.result = msg.arg1;
1972 }
Michele Berionne5e411512020-11-13 02:36:59 +00001973
1974
zoey chene02881a2019-12-30 16:11:23 +08001975 notifyRequester(request);
1976 break;
1977
Peter Wangdafb9ac2020-01-15 14:13:38 -08001978 case MSG_NOTIFY_USER_ACTIVITY:
1979 removeMessages(MSG_NOTIFY_USER_ACTIVITY);
Peter Wang59571be2020-01-27 12:35:15 +08001980 Intent intent = new Intent(TelephonyIntents.ACTION_USER_ACTIVITY_NOTIFICATION);
Peter Wangdafb9ac2020-01-15 14:13:38 -08001981 intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY);
1982 getDefaultPhone().getContext().sendBroadcastAsUser(
1983 intent, UserHandle.ALL, permission.USER_ACTIVITY);
1984 break;
Jack Nudelmanb0b87642020-11-12 15:04:39 -08001985
1986 case CMD_SET_DATA_THROTTLING: {
1987 request = (MainThreadRequest) msg.obj;
1988 onCompleted = obtainMessage(EVENT_SET_DATA_THROTTLING_DONE, request);
1989 DataThrottlingRequest dataThrottlingRequest =
1990 (DataThrottlingRequest) request.argument;
1991 Phone phone = getPhoneFromRequest(request);
1992 if (phone != null) {
1993 phone.setDataThrottling(onCompleted,
1994 request.workSource, dataThrottlingRequest.getDataThrottlingAction(),
1995 dataThrottlingRequest.getCompletionDurationMillis());
1996 } else {
1997 loge("setDataThrottling: No phone object");
1998 request.result =
1999 TelephonyManager.THERMAL_MITIGATION_RESULT_MODEM_NOT_AVAILABLE;
2000 notifyRequester(request);
2001 }
2002
2003 break;
2004 }
2005 case EVENT_SET_DATA_THROTTLING_DONE:
2006 ar = (AsyncResult) msg.obj;
2007 request = (MainThreadRequest) ar.userObj;
2008
2009 if (ar.exception == null) {
2010 request.result = TelephonyManager.THERMAL_MITIGATION_RESULT_SUCCESS;
2011 } else if (ar.exception instanceof CommandException) {
2012 loge("setDataThrottling: CommandException: " + ar.exception);
2013 CommandException.Error error =
2014 ((CommandException) (ar.exception)).getCommandError();
2015
2016 if (error == CommandException.Error.RADIO_NOT_AVAILABLE) {
2017 request.result = TelephonyManager
2018 .THERMAL_MITIGATION_RESULT_MODEM_NOT_AVAILABLE;
2019 } else if (error == CommandException.Error.INVALID_ARGUMENTS) {
2020 request.result = SET_DATA_THROTTLING_MODEM_THREW_INVALID_PARAMS;
Jack Nudelman5d6a98b2021-03-04 14:26:25 -08002021 } else if (error == CommandException.Error.REQUEST_NOT_SUPPORTED) {
2022 request.result = MODEM_DOES_NOT_SUPPORT_DATA_THROTTLING_ERROR_CODE;
Jack Nudelmanb0b87642020-11-12 15:04:39 -08002023 } else {
2024 request.result =
2025 TelephonyManager.THERMAL_MITIGATION_RESULT_MODEM_ERROR;
2026 }
2027 } else {
2028 request.result = TelephonyManager.THERMAL_MITIGATION_RESULT_MODEM_ERROR;
2029 }
2030 Log.w(LOG_TAG, "DataThrottlingResult = " + request.result);
2031 notifyRequester(request);
2032 break;
Jordan Liu109698e2020-11-24 14:50:34 -08002033
2034 case CMD_SET_SIM_POWER: {
2035 request = (MainThreadRequest) msg.obj;
2036 onCompleted = obtainMessage(EVENT_SET_SIM_POWER_DONE, request);
2037 request = (MainThreadRequest) msg.obj;
2038 int stateToSet =
2039 ((Pair<Integer, IIntegerConsumer>)
2040 request.argument).first;
2041 request.phone.setSimPowerState(stateToSet, onCompleted, request.workSource);
2042 break;
2043 }
2044 case EVENT_SET_SIM_POWER_DONE: {
2045 ar = (AsyncResult) msg.obj;
2046 request = (MainThreadRequest) ar.userObj;
2047 IIntegerConsumer callback =
2048 ((Pair<Integer, IIntegerConsumer>) request.argument).second;
2049 if (ar.exception != null) {
2050 loge("setSimPower exception: " + ar.exception);
2051 int errorCode = TelephonyManager.CallForwardingInfoCallback
2052 .RESULT_ERROR_UNKNOWN;
2053 if (ar.exception instanceof CommandException) {
2054 CommandException.Error error =
2055 ((CommandException) (ar.exception)).getCommandError();
2056 if (error == CommandException.Error.SIM_ERR) {
2057 errorCode = TelephonyManager.SET_SIM_POWER_STATE_SIM_ERROR;
2058 } else if (error == CommandException.Error.INVALID_ARGUMENTS) {
2059 errorCode = TelephonyManager.SET_SIM_POWER_STATE_ALREADY_IN_STATE;
2060 } else if (error == CommandException.Error.REQUEST_NOT_SUPPORTED) {
2061 errorCode = TelephonyManager.SET_SIM_POWER_STATE_NOT_SUPPORTED;
2062 } else {
2063 errorCode = TelephonyManager.SET_SIM_POWER_STATE_MODEM_ERROR;
2064 }
2065 }
2066 try {
2067 callback.accept(errorCode);
2068 } catch (RemoteException e) {
2069 // Ignore if the remote process is no longer available to call back.
2070 Log.w(LOG_TAG, "setSimPower: callback not available.");
2071 }
2072 } else {
2073 try {
2074 callback.accept(TelephonyManager.SET_SIM_POWER_STATE_SUCCESS);
2075 } catch (RemoteException e) {
2076 // Ignore if the remote process is no longer available to call back.
2077 Log.w(LOG_TAG, "setSimPower: callback not available.");
2078 }
2079 }
2080 break;
2081 }
Rambo Wanga5cc9b72021-01-07 10:51:54 -08002082 case CMD_SET_SIGNAL_STRENGTH_UPDATE_REQUEST: {
2083 request = (MainThreadRequest) msg.obj;
2084
2085 final Phone phone = getPhoneFromRequest(request);
2086 if (phone == null || phone.getServiceStateTracker() == null) {
2087 request.result = new IllegalStateException("Phone or SST is null");
2088 notifyRequester(request);
2089 break;
2090 }
2091
2092 Pair<Integer, SignalStrengthUpdateRequest> pair =
2093 (Pair<Integer, SignalStrengthUpdateRequest>) request.argument;
2094 onCompleted = obtainMessage(EVENT_SET_SIGNAL_STRENGTH_UPDATE_REQUEST_DONE,
2095 request);
Rambo Wang6568f172021-02-03 16:56:47 -08002096 phone.getSignalStrengthController().setSignalStrengthUpdateRequest(
Rambo Wanga5cc9b72021-01-07 10:51:54 -08002097 request.subId, pair.first /*callingUid*/,
2098 pair.second /*request*/, onCompleted);
2099 break;
2100 }
2101 case EVENT_SET_SIGNAL_STRENGTH_UPDATE_REQUEST_DONE: {
2102 ar = (AsyncResult) msg.obj;
2103 request = (MainThreadRequest) ar.userObj;
2104 // request.result will be the exception of ar if present, true otherwise.
2105 // Be cautious not to leave result null which will wait() forever
2106 request.result = ar.exception != null ? ar.exception : true;
2107 notifyRequester(request);
2108 break;
2109 }
2110 case CMD_CLEAR_SIGNAL_STRENGTH_UPDATE_REQUEST: {
2111 request = (MainThreadRequest) msg.obj;
2112
2113 Phone phone = getPhoneFromRequest(request);
2114 if (phone == null || phone.getServiceStateTracker() == null) {
2115 request.result = new IllegalStateException("Phone or SST is null");
2116 notifyRequester(request);
2117 break;
2118 }
2119
2120 Pair<Integer, SignalStrengthUpdateRequest> pair =
2121 (Pair<Integer, SignalStrengthUpdateRequest>) request.argument;
2122 onCompleted = obtainMessage(EVENT_CLEAR_SIGNAL_STRENGTH_UPDATE_REQUEST_DONE,
2123 request);
Rambo Wang6568f172021-02-03 16:56:47 -08002124 phone.getSignalStrengthController().clearSignalStrengthUpdateRequest(
Rambo Wanga5cc9b72021-01-07 10:51:54 -08002125 request.subId, pair.first /*callingUid*/,
2126 pair.second /*request*/, onCompleted);
2127 break;
2128 }
2129 case EVENT_CLEAR_SIGNAL_STRENGTH_UPDATE_REQUEST_DONE: {
2130 ar = (AsyncResult) msg.obj;
2131 request = (MainThreadRequest) ar.userObj;
2132 request.result = ar.exception != null ? ar.exception : true;
2133 notifyRequester(request);
2134 break;
2135 }
Jordan Liu109698e2020-11-24 14:50:34 -08002136
Hongbo Zeng156aa4a2021-02-08 21:50:28 +08002137 case CMD_GET_SLICING_CONFIG: {
2138 request = (MainThreadRequest) msg.obj;
2139 onCompleted = obtainMessage(EVENT_GET_SLICING_CONFIG_DONE, request);
2140 request.phone.getSlicingConfig(onCompleted);
2141 break;
2142 }
2143 case EVENT_GET_SLICING_CONFIG_DONE: {
2144 ar = (AsyncResult) msg.obj;
2145 request = (MainThreadRequest) ar.userObj;
2146 ResultReceiver result = (ResultReceiver) request.argument;
2147
Hongbo Zeng0e18b162021-04-07 16:52:18 +08002148 NetworkSlicingConfig slicingConfig = null;
Hongbo Zeng156aa4a2021-02-08 21:50:28 +08002149 Bundle bundle = new Bundle();
2150 int resultCode = 0;
2151 if (ar.exception != null) {
2152 Log.e(LOG_TAG, "Exception retrieving slicing configuration="
2153 + ar.exception);
Hongbo Zeng0e18b162021-04-07 16:52:18 +08002154 resultCode = TelephonyManager.NetworkSlicingException.ERROR_MODEM_ERROR;
Hongbo Zeng156aa4a2021-02-08 21:50:28 +08002155 } else if (ar.result == null) {
2156 Log.w(LOG_TAG, "Timeout Waiting for slicing configuration!");
Hongbo Zeng0e18b162021-04-07 16:52:18 +08002157 resultCode = TelephonyManager.NetworkSlicingException.ERROR_TIMEOUT;
Hongbo Zeng156aa4a2021-02-08 21:50:28 +08002158 } else {
2159 // use the result as returned
Hongbo Zeng0e18b162021-04-07 16:52:18 +08002160 resultCode = TelephonyManager.NetworkSlicingException.SUCCESS;
2161 slicingConfig = (NetworkSlicingConfig) ar.result;
Hongbo Zeng156aa4a2021-02-08 21:50:28 +08002162 }
2163
2164 if (slicingConfig == null) {
Hongbo Zeng0e18b162021-04-07 16:52:18 +08002165 slicingConfig = new NetworkSlicingConfig();
Hongbo Zeng156aa4a2021-02-08 21:50:28 +08002166 }
2167 bundle.putParcelable(TelephonyManager.KEY_SLICING_CONFIG_HANDLE, slicingConfig);
2168 result.send(resultCode, bundle);
2169 notifyRequester(request);
2170 break;
2171 }
2172
Sarah Chin71b3a852022-09-28 15:54:19 -07002173 case CMD_PURCHASE_PREMIUM_CAPABILITY: {
Sarah Chin2ec39f62022-08-31 17:03:26 -07002174 request = (MainThreadRequest) msg.obj;
2175 onCompleted = obtainMessage(EVENT_PURCHASE_PREMIUM_CAPABILITY_DONE, request);
Sarah Chin71b3a852022-09-28 15:54:19 -07002176 PurchasePremiumCapabilityArgument arg =
2177 (PurchasePremiumCapabilityArgument) request.argument;
Sarah Chin46355ba2022-11-01 23:51:16 -07002178 SlicePurchaseController.getInstance(request.phone).purchasePremiumCapability(
Sarah Chinb8218c22023-01-04 13:35:29 -08002179 arg.capability, onCompleted);
Sarah Chin2ec39f62022-08-31 17:03:26 -07002180 break;
Sarah Chin71b3a852022-09-28 15:54:19 -07002181 }
Sarah Chin2ec39f62022-08-31 17:03:26 -07002182
Sarah Chin71b3a852022-09-28 15:54:19 -07002183 case EVENT_PURCHASE_PREMIUM_CAPABILITY_DONE: {
Sarah Chin2ec39f62022-08-31 17:03:26 -07002184 ar = (AsyncResult) msg.obj;
2185 request = (MainThreadRequest) ar.userObj;
Sarah Chin71b3a852022-09-28 15:54:19 -07002186 PurchasePremiumCapabilityArgument arg =
2187 (PurchasePremiumCapabilityArgument) request.argument;
Sarah Chin2ec39f62022-08-31 17:03:26 -07002188 try {
2189 int result = (int) ar.result;
Sarah Chin71b3a852022-09-28 15:54:19 -07002190 arg.callback.accept(result);
Sarah Chin2ec39f62022-08-31 17:03:26 -07002191 log("purchasePremiumCapability: capability="
Sarah Chin71b3a852022-09-28 15:54:19 -07002192 + TelephonyManager.convertPremiumCapabilityToString(arg.capability)
Sarah Chin2ec39f62022-08-31 17:03:26 -07002193 + ", result= "
2194 + TelephonyManager.convertPurchaseResultToString(result));
2195 } catch (RemoteException e) {
2196 String logStr = "Purchase premium capability "
Sarah Chin71b3a852022-09-28 15:54:19 -07002197 + TelephonyManager.convertPremiumCapabilityToString(arg.capability)
Sarah Chin2ec39f62022-08-31 17:03:26 -07002198 + " failed: " + e;
2199 if (DBG) log(logStr);
2200 AnomalyReporter.reportAnomaly(
2201 UUID.fromString(PURCHASE_PREMIUM_CAPABILITY_ERROR_UUID), logStr);
2202 }
2203 break;
Sarah Chin71b3a852022-09-28 15:54:19 -07002204 }
Sarah Chin2ec39f62022-08-31 17:03:26 -07002205
Michele Berionne5e411512020-11-13 02:36:59 +00002206 case CMD_PREPARE_UNATTENDED_REBOOT:
2207 request = (MainThreadRequest) msg.obj;
2208 request.result =
Rafael Higuera Silvad9630642021-09-20 15:32:01 +00002209 UiccController.getInstance().getPinStorage()
2210 .prepareUnattendedReboot(request.workSource);
Michele Berionne5e411512020-11-13 02:36:59 +00002211 notifyRequester(request);
2212 break;
2213
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002214 default:
2215 Log.w(LOG_TAG, "MainThreadHandler: unexpected message code: " + msg.what);
2216 break;
2217 }
2218 }
Jake Hambye994d462014-02-03 13:10:13 -08002219
Pengquan Menga1bb6272018-09-06 09:59:22 -07002220 private void notifyRequester(MainThreadRequest request) {
2221 synchronized (request) {
2222 request.notifyAll();
2223 }
2224 }
2225
Jake Hambye994d462014-02-03 13:10:13 -08002226 private void handleNullReturnEvent(Message msg, String command) {
2227 AsyncResult ar = (AsyncResult) msg.obj;
2228 MainThreadRequest request = (MainThreadRequest) ar.userObj;
2229 if (ar.exception == null) {
2230 request.result = true;
2231 } else {
2232 request.result = false;
2233 if (ar.exception instanceof CommandException) {
2234 loge(command + ": CommandException: " + ar.exception);
2235 } else {
2236 loge(command + ": Unknown exception");
2237 }
2238 }
Pengquan Menga1bb6272018-09-06 09:59:22 -07002239 notifyRequester(request);
Jake Hambye994d462014-02-03 13:10:13 -08002240 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002241 }
2242
2243 /**
2244 * Posts the specified command to be executed on the main thread,
2245 * waits for the request to complete, and returns the result.
2246 * @see #sendRequestAsync
2247 */
2248 private Object sendRequest(int command, Object argument) {
Rambo Wang0f050d82021-02-12 11:43:36 -08002249 return sendRequest(command, argument, SubscriptionManager.INVALID_SUBSCRIPTION_ID, null,
2250 null, -1 /*timeoutInMs*/);
vagdeviaf9a5b92018-08-15 16:01:53 -07002251 }
2252
2253 /**
2254 * Posts the specified command to be executed on the main thread,
2255 * waits for the request to complete, and returns the result.
2256 * @see #sendRequestAsync
2257 */
2258 private Object sendRequest(int command, Object argument, WorkSource workSource) {
2259 return sendRequest(command, argument, SubscriptionManager.INVALID_SUBSCRIPTION_ID,
Rambo Wang0f050d82021-02-12 11:43:36 -08002260 null, workSource, -1 /*timeoutInMs*/);
Wink Saville36469e72014-06-11 15:17:00 -07002261 }
2262
2263 /**
2264 * Posts the specified command to be executed on the main thread,
2265 * waits for the request to complete, and returns the result.
2266 * @see #sendRequestAsync
2267 */
Shishir Agrawal76d5da92014-11-09 16:17:25 -08002268 private Object sendRequest(int command, Object argument, Integer subId) {
Rambo Wang0f050d82021-02-12 11:43:36 -08002269 return sendRequest(command, argument, subId, null, null, -1 /*timeoutInMs*/);
2270 }
2271
2272 /**
2273 * Posts the specified command to be executed on the main thread,
2274 * waits for the request to complete for at most {@code timeoutInMs}, and returns the result
2275 * if not timeout or null otherwise.
2276 * @see #sendRequestAsync
2277 */
2278 private @Nullable Object sendRequest(int command, Object argument, Integer subId,
2279 long timeoutInMs) {
2280 return sendRequest(command, argument, subId, null, null, timeoutInMs);
vagdeviaf9a5b92018-08-15 16:01:53 -07002281 }
2282
2283 /**
2284 * Posts the specified command to be executed on the main thread,
2285 * waits for the request to complete, and returns the result.
2286 * @see #sendRequestAsync
2287 */
Nathan Harold92bed182018-10-12 18:16:49 -07002288 private Object sendRequest(int command, Object argument, int subId, WorkSource workSource) {
Rambo Wang0f050d82021-02-12 11:43:36 -08002289 return sendRequest(command, argument, subId, null, workSource, -1 /*timeoutInMs*/);
Nathan Harold92bed182018-10-12 18:16:49 -07002290 }
2291
2292 /**
2293 * Posts the specified command to be executed on the main thread,
2294 * waits for the request to complete, and returns the result.
2295 * @see #sendRequestAsync
2296 */
2297 private Object sendRequest(int command, Object argument, Phone phone, WorkSource workSource) {
Rambo Wang0f050d82021-02-12 11:43:36 -08002298 return sendRequest(command, argument, SubscriptionManager.INVALID_SUBSCRIPTION_ID, phone,
2299 workSource, -1 /*timeoutInMs*/);
Nathan Harold92bed182018-10-12 18:16:49 -07002300 }
2301
2302 /**
Rambo Wang0f050d82021-02-12 11:43:36 -08002303 * Posts the specified command to be executed on the main thread. If {@code timeoutInMs} is
2304 * negative, waits for the request to complete, and returns the result. Otherwise, wait for
2305 * maximum of {@code timeoutInMs} milliseconds, interrupt and return null.
Nathan Harold92bed182018-10-12 18:16:49 -07002306 * @see #sendRequestAsync
2307 */
Rambo Wang0f050d82021-02-12 11:43:36 -08002308 private @Nullable Object sendRequest(int command, Object argument, Integer subId, Phone phone,
2309 WorkSource workSource, long timeoutInMs) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002310 if (Looper.myLooper() == mMainThreadHandler.getLooper()) {
2311 throw new RuntimeException("This method will deadlock if called from the main thread.");
2312 }
2313
Nathan Harold92bed182018-10-12 18:16:49 -07002314 MainThreadRequest request = null;
2315 if (subId != SubscriptionManager.INVALID_SUBSCRIPTION_ID && phone != null) {
2316 throw new IllegalArgumentException("subId and phone cannot both be specified!");
2317 } else if (phone != null) {
2318 request = new MainThreadRequest(argument, phone, workSource);
2319 } else {
2320 request = new MainThreadRequest(argument, subId, workSource);
2321 }
2322
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002323 Message msg = mMainThreadHandler.obtainMessage(command, request);
2324 msg.sendToTarget();
2325
Rambo Wang0f050d82021-02-12 11:43:36 -08002326
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002327 synchronized (request) {
Rambo Wang0f050d82021-02-12 11:43:36 -08002328 if (timeoutInMs >= 0) {
2329 // Wait for at least timeoutInMs before returning null request result
2330 long now = SystemClock.elapsedRealtime();
2331 long deadline = now + timeoutInMs;
Grace Jia8a0a1e82021-05-23 22:59:52 -07002332 while (request.result == null && now < deadline) {
Rambo Wang0f050d82021-02-12 11:43:36 -08002333 try {
2334 request.wait(deadline - now);
2335 } catch (InterruptedException e) {
2336 // Do nothing, go back and check if request is completed or timeout
2337 } finally {
2338 now = SystemClock.elapsedRealtime();
2339 }
2340 }
2341 } else {
2342 // Wait for the request to complete
2343 while (request.result == null) {
2344 try {
2345 request.wait();
2346 } catch (InterruptedException e) {
2347 // Do nothing, go back and wait until the request is complete
2348 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002349 }
2350 }
2351 }
Rambo Wang0f050d82021-02-12 11:43:36 -08002352 if (request.result == null) {
2353 Log.wtf(LOG_TAG,
2354 "sendRequest: Blocking command timed out. Something has gone terribly wrong.");
2355 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002356 return request.result;
2357 }
2358
2359 /**
2360 * Asynchronous ("fire and forget") version of sendRequest():
2361 * Posts the specified command to be executed on the main thread, and
2362 * returns immediately.
2363 * @see #sendRequest
2364 */
2365 private void sendRequestAsync(int command) {
2366 mMainThreadHandler.sendEmptyMessage(command);
2367 }
2368
2369 /**
Sailesh Nepalbd76e4e2013-10-27 13:59:44 -07002370 * Same as {@link #sendRequestAsync(int)} except it takes an argument.
Nathan Haroldfa8da0f2018-09-27 18:51:29 -07002371 * @see {@link #sendRequest(int)}
Sailesh Nepalbd76e4e2013-10-27 13:59:44 -07002372 */
2373 private void sendRequestAsync(int command, Object argument) {
Nathan Haroldfa8da0f2018-09-27 18:51:29 -07002374 sendRequestAsync(command, argument, null, null);
2375 }
2376
2377 /**
2378 * Same as {@link #sendRequestAsync(int,Object)} except it takes a Phone and WorkSource.
2379 * @see {@link #sendRequest(int,Object)}
2380 */
2381 private void sendRequestAsync(
2382 int command, Object argument, Phone phone, WorkSource workSource) {
2383 MainThreadRequest request = new MainThreadRequest(argument, phone, workSource);
Sailesh Nepalbd76e4e2013-10-27 13:59:44 -07002384 Message msg = mMainThreadHandler.obtainMessage(command, request);
2385 msg.sendToTarget();
2386 }
2387
2388 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002389 * Initialize the singleton PhoneInterfaceManager instance.
2390 * This is only done once, at startup, from PhoneApp.onCreate().
2391 */
Nazanin Bakhshif782e562018-12-11 15:15:39 -08002392 /* package */ static PhoneInterfaceManager init(PhoneGlobals app) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002393 synchronized (PhoneInterfaceManager.class) {
2394 if (sInstance == null) {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08002395 sInstance = new PhoneInterfaceManager(app);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002396 } else {
2397 Log.wtf(LOG_TAG, "init() called multiple times! sInstance = " + sInstance);
2398 }
2399 return sInstance;
2400 }
2401 }
2402
2403 /** Private constructor; @see init() */
Jordan Liu1979a042020-03-20 21:39:35 +00002404 private PhoneInterfaceManager(PhoneGlobals app) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002405 mApp = app;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002406 mCM = PhoneGlobals.getInstance().mCM;
Brad Ebingerd1947d82021-05-17 20:54:49 +00002407 mImsResolver = ImsResolver.getInstance();
Stuart Scott981d8582015-04-21 14:09:50 -07002408 mUserManager = (UserManager) app.getSystemService(Context.USER_SERVICE);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002409 mAppOps = (AppOpsManager)app.getSystemService(Context.APP_OPS_SERVICE);
Grace Jia0ddb3612021-04-22 13:35:26 -07002410 mPm = app.getSystemService(PackageManager.class);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002411 mMainThreadHandler = new MainThreadHandler();
Aishwarya Mallampati32336e82023-01-04 23:02:56 +00002412 mSubscriptionController = SubscriptionController.getInstance();
Nazanin Bakhshif782e562018-12-11 15:15:39 -08002413 mTelephonySharedPreferences =
2414 PreferenceManager.getDefaultSharedPreferences(mApp);
yinxub1bed742017-04-17 11:45:04 -07002415 mNetworkScanRequestTracker = new NetworkScanRequestTracker();
Malcolm Chen2c63d402018-08-14 16:00:53 -07002416 mPhoneConfigurationManager = PhoneConfigurationManager.getInstance();
Daniel Bright94f43662021-03-01 14:43:40 -08002417 mRadioInterfaceCapabilities = RadioInterfaceCapabilityController.getInstance();
Peter Wanga3cf4ac2020-01-27 09:39:46 +08002418 mNotifyUserActivity = new AtomicBoolean(false);
Tyler Gunn64144d92022-03-17 14:16:41 -07002419 PropertyInvalidatedCache.invalidateCache(TelephonyManager.CACHE_KEY_PHONE_ACCOUNT_TO_SUBID);
Gil Cukierman6dac5eb2022-09-19 16:09:04 +00002420 mTelephony2gUpdater = new Telephony2gUpdater(
2421 Executors.newSingleThreadExecutor(), mApp);
2422 mTelephony2gUpdater.init();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002423 publish();
2424 }
2425
Gil Cukierman1c0eb932022-12-06 22:28:24 +00002426 @VisibleForTesting
2427 public SharedPreferences getSharedPreferences() {
2428 return mTelephonySharedPreferences;
2429 }
2430
Gil Cukierman92cc7db2023-01-06 19:25:53 +00002431 /**
2432 * Get the default phone for this device.
2433 */
2434 @VisibleForTesting
2435 public Phone getDefaultPhone() {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08002436 Phone thePhone = getPhone(getDefaultSubscription());
2437 return (thePhone != null) ? thePhone : PhoneFactory.getDefaultPhone();
2438 }
2439
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002440 private void publish() {
2441 if (DBG) log("publish: " + this);
2442
Peter Wangc035ce42020-01-08 21:00:22 -08002443 TelephonyFrameworkInitializer
2444 .getTelephonyServiceManager()
2445 .getTelephonyServiceRegisterer()
2446 .register(this);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002447 }
2448
Stuart Scott584921c2015-01-15 17:10:34 -08002449 private Phone getPhoneFromRequest(MainThreadRequest request) {
Jordan Liu4c733742019-02-28 12:03:40 -08002450 if (request.phone != null) {
2451 return request.phone;
2452 } else {
2453 return getPhoneFromSubId(request.subId);
2454 }
2455 }
2456
2457 private Phone getPhoneFromSubId(int subId) {
2458 return (subId == SubscriptionManager.INVALID_SUBSCRIPTION_ID)
2459 ? getDefaultPhone() : getPhone(subId);
Stuart Scott584921c2015-01-15 17:10:34 -08002460 }
2461
Rambo Wange53e07d2022-05-10 13:01:13 -07002462 @Nullable
2463 private UiccPort getUiccPortFromRequest(@NonNull MainThreadRequest request) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002464 Phone phone = getPhoneFromRequest(request);
2465 return phone == null ? null :
Muralidhar Reddy472c2ae2021-09-29 19:38:40 +00002466 UiccController.getInstance().getUiccPort(phone.getPhoneId());
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002467 }
2468
Wink Saville36469e72014-06-11 15:17:00 -07002469 // returns phone associated with the subId.
Wink Savilleb564aae2014-10-23 10:18:09 -07002470 private Phone getPhone(int subId) {
Jack Yu285100e2022-12-02 22:48:35 -08002471 return PhoneFactory.getPhone(SubscriptionManager.getPhoneId(subId));
Wink Saville36469e72014-06-11 15:17:00 -07002472 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002473
Kai Shif70f46f2021-03-03 13:59:46 -08002474 private void sendEraseModemConfig(@NonNull Phone phone) {
2475 Boolean success = (Boolean) sendRequest(CMD_ERASE_MODEM_CONFIG, null);
2476 if (DBG) log("eraseModemConfig:" + ' ' + (success ? "ok" : "fail"));
2477 }
2478
2479 private void sendEraseDataInSharedPreferences(@NonNull Phone phone) {
2480 Boolean success = (Boolean) sendRequest(CMD_ERASE_DATA_SHARED_PREFERENCES, null);
2481 if (DBG) log("eraseDataInSharedPreferences:" + ' ' + (success ? "ok" : "fail"));
Naina Nallurid63128d2019-09-17 14:10:30 -07002482 }
2483
Peter Wang44b186e2020-01-13 23:33:09 -08002484 private boolean isImsAvailableOnDevice() {
2485 PackageManager pm = getDefaultPhone().getContext().getPackageManager();
2486 if (pm == null) {
2487 // For some reason package manger is not available.. This will fail internally anyway,
2488 // so do not throw error and allow.
2489 return true;
2490 }
2491 return pm.hasSystemFeature(PackageManager.FEATURE_TELEPHONY_IMS, 0);
2492 }
2493
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002494 public void dial(String number) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07002495 dialForSubscriber(getPreferredVoiceSubscription(), number);
Wink Saville36469e72014-06-11 15:17:00 -07002496 }
2497
Wink Savilleb564aae2014-10-23 10:18:09 -07002498 public void dialForSubscriber(int subId, String number) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002499 if (DBG) log("dial: " + number);
2500 // No permission check needed here: This is just a wrapper around the
2501 // ACTION_DIAL intent, which is available to any app since it puts up
2502 // the UI before it does anything.
2503
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002504 final long identity = Binder.clearCallingIdentity();
2505 try {
2506 String url = createTelUrl(number);
2507 if (url == null) {
2508 return;
2509 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002510
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002511 // PENDING: should we just silently fail if phone is offhook or ringing?
2512 PhoneConstants.State state = mCM.getState(subId);
2513 if (state != PhoneConstants.State.OFFHOOK && state != PhoneConstants.State.RINGING) {
2514 Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse(url));
2515 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
2516 mApp.startActivity(intent);
2517 }
2518 } finally {
2519 Binder.restoreCallingIdentity(identity);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002520 }
2521 }
2522
2523 public void call(String callingPackage, String number) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07002524 callForSubscriber(getPreferredVoiceSubscription(), callingPackage, number);
Wink Saville36469e72014-06-11 15:17:00 -07002525 }
2526
Wink Savilleb564aae2014-10-23 10:18:09 -07002527 public void callForSubscriber(int subId, String callingPackage, String number) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002528 if (DBG) log("call: " + number);
2529
2530 // This is just a wrapper around the ACTION_CALL intent, but we still
2531 // need to do a permission check since we're calling startActivity()
2532 // from the context of the phone app.
2533 enforceCallPermission();
2534
Jordan Liu1617b712019-07-10 15:06:26 -07002535 if (mAppOps.noteOp(AppOpsManager.OPSTR_CALL_PHONE, Binder.getCallingUid(), callingPackage)
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002536 != AppOpsManager.MODE_ALLOWED) {
2537 return;
2538 }
2539
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002540 final long identity = Binder.clearCallingIdentity();
2541 try {
2542 String url = createTelUrl(number);
2543 if (url == null) {
2544 return;
2545 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002546
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002547 boolean isValid = false;
2548 final List<SubscriptionInfo> slist = getActiveSubscriptionInfoListPrivileged();
2549 if (slist != null) {
2550 for (SubscriptionInfo subInfoRecord : slist) {
2551 if (subInfoRecord.getSubscriptionId() == subId) {
2552 isValid = true;
2553 break;
2554 }
Wink Saville3ab207e2014-11-20 13:07:20 -08002555 }
Wink Saville08874612014-08-31 19:19:58 -07002556 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002557 if (!isValid) {
2558 return;
2559 }
Wink Saville08874612014-08-31 19:19:58 -07002560
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002561 Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse(url));
2562 intent.putExtra(SUBSCRIPTION_KEY, subId);
2563 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
2564 mApp.startActivity(intent);
2565 } finally {
2566 Binder.restoreCallingIdentity(identity);
2567 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002568 }
2569
Wink Savilleb564aae2014-10-23 10:18:09 -07002570 public boolean supplyPinForSubscriber(int subId, String pin) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07002571 int [] resultArray = supplyPinReportResultForSubscriber(subId, pin);
Wink Saville9de0f752013-10-22 19:04:03 -07002572 return (resultArray[0] == PhoneConstants.PIN_RESULT_SUCCESS) ? true : false;
2573 }
2574
Wink Savilleb564aae2014-10-23 10:18:09 -07002575 public boolean supplyPukForSubscriber(int subId, String puk, String pin) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07002576 int [] resultArray = supplyPukReportResultForSubscriber(subId, puk, pin);
Wink Saville9de0f752013-10-22 19:04:03 -07002577 return (resultArray[0] == PhoneConstants.PIN_RESULT_SUCCESS) ? true : false;
2578 }
2579
Wink Savilleb564aae2014-10-23 10:18:09 -07002580 public int[] supplyPinReportResultForSubscriber(int subId, String pin) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002581 enforceModifyPermission();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002582
2583 final long identity = Binder.clearCallingIdentity();
2584 try {
Michele Berionne5e411512020-11-13 02:36:59 +00002585 Phone phone = getPhone(subId);
2586 final UnlockSim checkSimPin = new UnlockSim(phone.getPhoneId(), phone.getIccCard());
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002587 checkSimPin.start();
2588 return checkSimPin.unlockSim(null, pin);
2589 } finally {
2590 Binder.restoreCallingIdentity(identity);
2591 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002592 }
2593
Wink Savilleb564aae2014-10-23 10:18:09 -07002594 public int[] supplyPukReportResultForSubscriber(int subId, String puk, String pin) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002595 enforceModifyPermission();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002596
2597 final long identity = Binder.clearCallingIdentity();
2598 try {
Michele Berionne5e411512020-11-13 02:36:59 +00002599 Phone phone = getPhone(subId);
2600 final UnlockSim checkSimPuk = new UnlockSim(phone.getPhoneId(), phone.getIccCard());
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002601 checkSimPuk.start();
2602 return checkSimPuk.unlockSim(puk, pin);
2603 } finally {
2604 Binder.restoreCallingIdentity(identity);
2605 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002606 }
2607
2608 /**
Wink Saville9de0f752013-10-22 19:04:03 -07002609 * Helper thread to turn async call to SimCard#supplyPin into
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002610 * a synchronous one.
2611 */
2612 private static class UnlockSim extends Thread {
2613
2614 private final IccCard mSimCard;
Michele Berionne5e411512020-11-13 02:36:59 +00002615 private final int mPhoneId;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002616
2617 private boolean mDone = false;
Wink Saville9de0f752013-10-22 19:04:03 -07002618 private int mResult = PhoneConstants.PIN_GENERAL_FAILURE;
2619 private int mRetryCount = -1;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002620
2621 // For replies from SimCard interface
2622 private Handler mHandler;
2623
2624 // For async handler to identify request type
2625 private static final int SUPPLY_PIN_COMPLETE = 100;
2626
Michele Berionne5e411512020-11-13 02:36:59 +00002627 UnlockSim(int phoneId, IccCard simCard) {
2628 mPhoneId = phoneId;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002629 mSimCard = simCard;
2630 }
2631
2632 @Override
2633 public void run() {
2634 Looper.prepare();
2635 synchronized (UnlockSim.this) {
2636 mHandler = new Handler() {
2637 @Override
2638 public void handleMessage(Message msg) {
2639 AsyncResult ar = (AsyncResult) msg.obj;
2640 switch (msg.what) {
2641 case SUPPLY_PIN_COMPLETE:
2642 Log.d(LOG_TAG, "SUPPLY_PIN_COMPLETE");
2643 synchronized (UnlockSim.this) {
Wink Saville9de0f752013-10-22 19:04:03 -07002644 mRetryCount = msg.arg1;
2645 if (ar.exception != null) {
2646 if (ar.exception instanceof CommandException &&
2647 ((CommandException)(ar.exception)).getCommandError()
2648 == CommandException.Error.PASSWORD_INCORRECT) {
2649 mResult = PhoneConstants.PIN_PASSWORD_INCORRECT;
vivi.lib5e9ada2019-09-12 16:04:24 +08002650 } //When UiccCardApp dispose,handle message and return exception
2651 else if (ar.exception instanceof CommandException &&
2652 ((CommandException) (ar.exception)).getCommandError()
2653 == CommandException.Error.ABORTED) {
2654 mResult = PhoneConstants.PIN_OPERATION_ABORTED;
Wink Saville9de0f752013-10-22 19:04:03 -07002655 } else {
2656 mResult = PhoneConstants.PIN_GENERAL_FAILURE;
2657 }
2658 } else {
2659 mResult = PhoneConstants.PIN_RESULT_SUCCESS;
2660 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002661 mDone = true;
2662 UnlockSim.this.notifyAll();
2663 }
2664 break;
2665 }
2666 }
2667 };
2668 UnlockSim.this.notifyAll();
2669 }
2670 Looper.loop();
2671 }
2672
2673 /*
2674 * Use PIN or PUK to unlock SIM card
2675 *
2676 * If PUK is null, unlock SIM card with PIN
2677 *
2678 * If PUK is not null, unlock SIM card with PUK and set PIN code
2679 */
Wink Saville9de0f752013-10-22 19:04:03 -07002680 synchronized int[] unlockSim(String puk, String pin) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002681
2682 while (mHandler == null) {
2683 try {
2684 wait();
2685 } catch (InterruptedException e) {
2686 Thread.currentThread().interrupt();
2687 }
2688 }
2689 Message callback = Message.obtain(mHandler, SUPPLY_PIN_COMPLETE);
2690
2691 if (puk == null) {
2692 mSimCard.supplyPin(pin, callback);
2693 } else {
2694 mSimCard.supplyPuk(puk, pin, callback);
2695 }
2696
2697 while (!mDone) {
2698 try {
2699 Log.d(LOG_TAG, "wait for done");
2700 wait();
2701 } catch (InterruptedException e) {
2702 // Restore the interrupted status
2703 Thread.currentThread().interrupt();
2704 }
2705 }
2706 Log.d(LOG_TAG, "done");
Wink Saville9de0f752013-10-22 19:04:03 -07002707 int[] resultArray = new int[2];
2708 resultArray[0] = mResult;
2709 resultArray[1] = mRetryCount;
Michele Berionne5e411512020-11-13 02:36:59 +00002710
2711 if (mResult == PhoneConstants.PIN_RESULT_SUCCESS && pin.length() > 0) {
Jon Spivack9c3bc762021-10-06 20:53:09 +00002712 UiccController.getInstance().getPinStorage().storePin(pin, mPhoneId);
Michele Berionne5e411512020-11-13 02:36:59 +00002713 }
2714
Wink Saville9de0f752013-10-22 19:04:03 -07002715 return resultArray;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002716 }
2717 }
2718
Nathan Harold7c8d0f12020-05-28 20:40:31 -07002719 /**
2720 * This method has been removed due to privacy and stability concerns.
2721 */
2722 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002723 public void updateServiceLocation() {
Nathan Harold7c8d0f12020-05-28 20:40:31 -07002724 Log.e(LOG_TAG, "Call to unsupported method updateServiceLocation()");
2725 return;
Wink Saville36469e72014-06-11 15:17:00 -07002726 }
2727
Nathan Harold1f889d82020-06-04 17:05:26 -07002728 @Override
2729 public void updateServiceLocationWithPackageName(String callingPackage) {
2730 mApp.getSystemService(AppOpsManager.class)
2731 .checkPackage(Binder.getCallingUid(), callingPackage);
2732
Nathan Haroldf096d982020-11-18 17:18:06 -08002733 final int targetSdk = TelephonyPermissions.getTargetSdk(mApp, callingPackage);
Nathan Harold1f889d82020-06-04 17:05:26 -07002734 if (targetSdk > android.os.Build.VERSION_CODES.R) {
2735 // Callers targeting S have no business invoking this method.
2736 return;
2737 }
2738
2739 LocationAccessPolicy.LocationPermissionResult locationResult =
2740 LocationAccessPolicy.checkLocationPermission(mApp,
2741 new LocationAccessPolicy.LocationPermissionQuery.Builder()
2742 .setCallingPackage(callingPackage)
2743 .setCallingFeatureId(null)
2744 .setCallingPid(Binder.getCallingPid())
2745 .setCallingUid(Binder.getCallingUid())
2746 .setMethod("updateServiceLocation")
2747 .setMinSdkVersionForCoarse(Build.VERSION_CODES.BASE)
2748 .setMinSdkVersionForFine(Build.VERSION_CODES.Q)
2749 .build());
2750 // Apps that lack location permission have no business calling this method;
2751 // however, because no permission was declared in the public API, denials must
2752 // all be "soft".
2753 switch (locationResult) {
2754 case DENIED_HARD: /* fall through */
2755 case DENIED_SOFT:
2756 return;
2757 }
2758
2759 WorkSource workSource = getWorkSource(Binder.getCallingUid());
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002760 final long identity = Binder.clearCallingIdentity();
2761 try {
Nathan Harold1f889d82020-06-04 17:05:26 -07002762 final Phone phone = getPhone(getDefaultSubscription());
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002763 if (phone != null) {
Nathan Harold1f889d82020-06-04 17:05:26 -07002764 phone.updateServiceLocation(workSource);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002765 }
2766 } finally {
2767 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07002768 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002769 }
2770
Philip P. Moltmann700a9592019-10-03 11:53:50 -07002771 @Deprecated
Robert Greenwalt36b23af2015-07-06 17:59:14 -07002772 @Override
2773 public boolean isRadioOn(String callingPackage) {
Philip P. Moltmann700a9592019-10-03 11:53:50 -07002774 return isRadioOnWithFeature(callingPackage, null);
2775 }
2776
2777
2778 @Override
2779 public boolean isRadioOnWithFeature(String callingPackage, String callingFeatureId) {
2780 return isRadioOnForSubscriberWithFeature(getDefaultSubscription(), callingPackage,
2781 callingFeatureId);
2782 }
2783
2784 @Deprecated
2785 @Override
2786 public boolean isRadioOnForSubscriber(int subId, String callingPackage) {
2787 return isRadioOnForSubscriberWithFeature(subId, callingPackage, null);
Wink Saville36469e72014-06-11 15:17:00 -07002788 }
2789
Robert Greenwalt36b23af2015-07-06 17:59:14 -07002790 @Override
Philip P. Moltmann700a9592019-10-03 11:53:50 -07002791 public boolean isRadioOnForSubscriberWithFeature(int subId, String callingPackage,
2792 String callingFeatureId) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08002793 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
Philip P. Moltmann700a9592019-10-03 11:53:50 -07002794 mApp, subId, callingPackage, callingFeatureId, "isRadioOnForSubscriber")) {
Robert Greenwalt36b23af2015-07-06 17:59:14 -07002795 return false;
2796 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002797
2798 final long identity = Binder.clearCallingIdentity();
2799 try {
2800 return isRadioOnForSubscriber(subId);
2801 } finally {
2802 Binder.restoreCallingIdentity(identity);
2803 }
Robert Greenwalt36b23af2015-07-06 17:59:14 -07002804 }
2805
2806 private boolean isRadioOnForSubscriber(int subId) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002807 final long identity = Binder.clearCallingIdentity();
2808 try {
2809 final Phone phone = getPhone(subId);
2810 if (phone != null) {
2811 return phone.getServiceState().getState() != ServiceState.STATE_POWER_OFF;
2812 } else {
2813 return false;
2814 }
2815 } finally {
2816 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07002817 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002818 }
2819
2820 public void toggleRadioOnOff() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07002821 toggleRadioOnOffForSubscriber(getDefaultSubscription());
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002822 }
Wink Saville36469e72014-06-11 15:17:00 -07002823
Wink Savilleb564aae2014-10-23 10:18:09 -07002824 public void toggleRadioOnOffForSubscriber(int subId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002825 enforceModifyPermission();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002826
2827 final long identity = Binder.clearCallingIdentity();
2828 try {
2829 final Phone phone = getPhone(subId);
2830 if (phone != null) {
2831 phone.setRadioPower(!isRadioOnForSubscriber(subId));
2832 }
2833 } finally {
2834 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07002835 }
Wink Saville36469e72014-06-11 15:17:00 -07002836 }
2837
2838 public boolean setRadio(boolean turnOn) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07002839 return setRadioForSubscriber(getDefaultSubscription(), turnOn);
Wink Saville36469e72014-06-11 15:17:00 -07002840 }
2841
Wink Savilleb564aae2014-10-23 10:18:09 -07002842 public boolean setRadioForSubscriber(int subId, boolean turnOn) {
Wink Saville36469e72014-06-11 15:17:00 -07002843 enforceModifyPermission();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002844
2845 final long identity = Binder.clearCallingIdentity();
2846 try {
2847 final Phone phone = getPhone(subId);
2848 if (phone == null) {
2849 return false;
2850 }
2851 if ((phone.getServiceState().getState() != ServiceState.STATE_POWER_OFF) != turnOn) {
2852 toggleRadioOnOffForSubscriber(subId);
2853 }
2854 return true;
2855 } finally {
2856 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07002857 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002858 }
Wink Saville36469e72014-06-11 15:17:00 -07002859
Naveen Kalla1fd79bd2014-08-08 00:48:59 -07002860 public boolean needMobileRadioShutdown() {
Shuo Qianfa7b6b32019-12-10 10:40:38 -08002861 enforceReadPrivilegedPermission("needMobileRadioShutdown");
Naveen Kalla1fd79bd2014-08-08 00:48:59 -07002862 /*
2863 * If any of the Radios are available, it will need to be
2864 * shutdown. So return true if any Radio is available.
2865 */
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002866 final long identity = Binder.clearCallingIdentity();
2867 try {
2868 for (int i = 0; i < TelephonyManager.getDefault().getPhoneCount(); i++) {
2869 Phone phone = PhoneFactory.getPhone(i);
2870 if (phone != null && phone.isRadioAvailable()) return true;
2871 }
2872 logv(TelephonyManager.getDefault().getPhoneCount() + " Phones are shutdown.");
2873 return false;
2874 } finally {
2875 Binder.restoreCallingIdentity(identity);
Naveen Kalla1fd79bd2014-08-08 00:48:59 -07002876 }
Naveen Kalla1fd79bd2014-08-08 00:48:59 -07002877 }
2878
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002879 @Override
Naveen Kalla1fd79bd2014-08-08 00:48:59 -07002880 public void shutdownMobileRadios() {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002881 enforceModifyPermission();
2882
2883 final long identity = Binder.clearCallingIdentity();
2884 try {
2885 for (int i = 0; i < TelephonyManager.getDefault().getPhoneCount(); i++) {
2886 logv("Shutting down Phone " + i);
2887 shutdownRadioUsingPhoneId(i);
2888 }
2889 } finally {
2890 Binder.restoreCallingIdentity(identity);
Naveen Kalla1fd79bd2014-08-08 00:48:59 -07002891 }
2892 }
2893
2894 private void shutdownRadioUsingPhoneId(int phoneId) {
Naveen Kalla1fd79bd2014-08-08 00:48:59 -07002895 Phone phone = PhoneFactory.getPhone(phoneId);
2896 if (phone != null && phone.isRadioAvailable()) {
2897 phone.shutdownRadio();
2898 }
2899 }
2900
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002901 public boolean setRadioPower(boolean turnOn) {
Jack Yub4e16162017-05-15 12:48:40 -07002902 enforceModifyPermission();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002903
2904 final long identity = Binder.clearCallingIdentity();
2905 try {
2906 final Phone defaultPhone = PhoneFactory.getDefaultPhone();
2907 if (defaultPhone != null) {
2908 defaultPhone.setRadioPower(turnOn);
2909 return true;
2910 } else {
2911 loge("There's no default phone.");
2912 return false;
2913 }
2914 } finally {
2915 Binder.restoreCallingIdentity(identity);
Wei Liu9ae2a062016-08-08 11:09:34 -07002916 }
Wink Saville36469e72014-06-11 15:17:00 -07002917 }
2918
Wink Savilleb564aae2014-10-23 10:18:09 -07002919 public boolean setRadioPowerForSubscriber(int subId, boolean turnOn) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002920 enforceModifyPermission();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002921
2922 final long identity = Binder.clearCallingIdentity();
2923 try {
2924 final Phone phone = getPhone(subId);
2925 if (phone != null) {
2926 phone.setRadioPower(turnOn);
2927 return true;
2928 } else {
2929 return false;
2930 }
2931 } finally {
2932 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07002933 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002934 }
2935
Thomas Nguyenfd0572f2022-07-15 22:28:49 +00002936 /**
2937 * Vote on powering off the radio for a reason. The radio will be turned on only when there is
2938 * no reason to power it off. When any of the voters want to power it off, it will be turned
2939 * off. In case of emergency, the radio will be turned on even if there are some reasons for
2940 * powering it off, and these radio off votes will be cleared.
2941 * Multiple apps can vote for the same reason and the last vote will take effect. Each app is
2942 * responsible for its vote. A powering-off vote of a reason will be maintained until it is
2943 * cleared by calling {@link clearRadioPowerOffForReason} for that reason, or an emergency call
2944 * is made, or the device is rebooted. When an app comes backup from a crash, it needs to make
2945 * sure if its vote is as expected. An app can use the API {@link getRadioPowerOffReasons} to
2946 * check its vote.
2947 *
2948 * @param subId The subscription ID.
2949 * @param reason The reason for powering off radio.
2950 * @return true on success and false on failure.
2951 */
2952 public boolean requestRadioPowerOffForReason(int subId,
2953 @TelephonyManager.RadioPowerReason int reason) {
2954 enforceModifyPermission();
2955
2956 final long identity = Binder.clearCallingIdentity();
2957 try {
2958 final Phone phone = getPhone(subId);
2959 if (phone != null) {
2960 phone.setRadioPowerForReason(false, reason);
2961 return true;
2962 } else {
2963 return false;
2964 }
2965 } finally {
2966 Binder.restoreCallingIdentity(identity);
2967 }
2968 }
2969
2970 /**
2971 * Remove the vote on powering off the radio for a reason, as requested by
2972 * {@link requestRadioPowerOffForReason}.
2973 *
2974 * @param subId The subscription ID.
2975 * @param reason The reason for powering off radio.
2976 * @return true on success and false on failure.
2977 */
2978 public boolean clearRadioPowerOffForReason(int subId,
2979 @TelephonyManager.RadioPowerReason int reason) {
2980 enforceModifyPermission();
2981
2982 final long identity = Binder.clearCallingIdentity();
2983 try {
2984 final Phone phone = getPhone(subId);
2985 if (phone != null) {
2986 phone.setRadioPowerForReason(true, reason);
2987 return true;
2988 } else {
2989 return false;
2990 }
2991 } finally {
2992 Binder.restoreCallingIdentity(identity);
2993 }
2994 }
2995
2996 /**
2997 * Get reasons for powering off radio, as requested by {@link requestRadioPowerOffForReason}.
2998 *
2999 * @param subId The subscription ID.
3000 * @param callingPackage The package making the call.
3001 * @param callingFeatureId The feature in the package.
3002 * @return List of reasons for powering off radio.
3003 */
3004 public List getRadioPowerOffReasons(int subId, String callingPackage, String callingFeatureId) {
3005 enforceReadPrivilegedPermission("getRadioPowerOffReasons");
3006
3007 final long identity = Binder.clearCallingIdentity();
3008 List result = new ArrayList();
3009 try {
3010 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(mApp, subId,
3011 callingPackage, callingFeatureId, "getRadioPowerOffReasons")) {
3012 return result;
3013 }
3014
3015 final Phone phone = getPhone(subId);
3016 if (phone != null) {
3017 result.addAll(phone.getRadioPowerOffReasons());
3018 }
3019 } finally {
3020 Binder.restoreCallingIdentity(identity);
3021 }
3022 return result;
3023 }
3024
Wink Saville36469e72014-06-11 15:17:00 -07003025 // FIXME: subId version needed
Sanket Padawe356d7632015-06-22 14:03:32 -07003026 @Override
Sarah Chinecc78c42022-03-31 21:16:48 -07003027 public boolean enableDataConnectivity(String callingPackage) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003028 enforceModifyPermission();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003029
3030 final long identity = Binder.clearCallingIdentity();
3031 try {
Jack Yu285100e2022-12-02 22:48:35 -08003032 int subId = SubscriptionManager.getDefaultDataSubscriptionId();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003033 final Phone phone = getPhone(subId);
3034 if (phone != null) {
Jack Yu7968c6d2022-07-31 00:43:21 -07003035 phone.getDataSettingsManager().setDataEnabled(
3036 TelephonyManager.DATA_ENABLED_REASON_USER, true, callingPackage);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003037 return true;
3038 } else {
3039 return false;
3040 }
3041 } finally {
3042 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07003043 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003044 }
3045
Wink Saville36469e72014-06-11 15:17:00 -07003046 // FIXME: subId version needed
Sanket Padawe356d7632015-06-22 14:03:32 -07003047 @Override
Sarah Chinecc78c42022-03-31 21:16:48 -07003048 public boolean disableDataConnectivity(String callingPackage) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003049 enforceModifyPermission();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003050
3051 final long identity = Binder.clearCallingIdentity();
3052 try {
Jack Yu285100e2022-12-02 22:48:35 -08003053 int subId = SubscriptionManager.getDefaultDataSubscriptionId();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003054 final Phone phone = getPhone(subId);
3055 if (phone != null) {
Jack Yu7968c6d2022-07-31 00:43:21 -07003056 phone.getDataSettingsManager().setDataEnabled(
3057 TelephonyManager.DATA_ENABLED_REASON_USER, false, callingPackage);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003058 return true;
3059 } else {
3060 return false;
3061 }
3062 } finally {
3063 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07003064 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003065 }
3066
Sanket Padawe356d7632015-06-22 14:03:32 -07003067 @Override
Jack Yuacf8a132017-05-01 17:00:48 -07003068 public boolean isDataConnectivityPossible(int subId) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003069 final long identity = Binder.clearCallingIdentity();
3070 try {
3071 final Phone phone = getPhone(subId);
3072 if (phone != null) {
Jack Yu59824e12022-03-23 01:42:44 -07003073 return phone.isDataAllowed();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003074 } else {
3075 return false;
3076 }
3077 } finally {
3078 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07003079 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003080 }
3081
3082 public boolean handlePinMmi(String dialString) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07003083 return handlePinMmiForSubscriber(getDefaultSubscription(), dialString);
Wink Saville36469e72014-06-11 15:17:00 -07003084 }
3085
pkanwarae03a6b2016-11-06 20:37:09 -08003086 public void handleUssdRequest(int subId, String ussdRequest, ResultReceiver wrappedCallback) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003087 enforceCallPermission();
3088
3089 final long identity = Binder.clearCallingIdentity();
3090 try {
3091 if (!SubscriptionManager.isValidSubscriptionId(subId)) {
3092 return;
3093 }
3094 Pair<String, ResultReceiver> ussdObject = new Pair(ussdRequest, wrappedCallback);
3095 sendRequest(CMD_HANDLE_USSD_REQUEST, ussdObject, subId);
3096 } finally {
3097 Binder.restoreCallingIdentity(identity);
3098 }
pkanwar32d516d2016-10-14 19:37:38 -07003099 };
3100
Wink Savilleb564aae2014-10-23 10:18:09 -07003101 public boolean handlePinMmiForSubscriber(int subId, String dialString) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003102 enforceModifyPermission();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003103
3104 final long identity = Binder.clearCallingIdentity();
3105 try {
3106 if (!SubscriptionManager.isValidSubscriptionId(subId)) {
3107 return false;
3108 }
3109 return (Boolean) sendRequest(CMD_HANDLE_PIN_MMI, dialString, subId);
3110 } finally {
3111 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07003112 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003113 }
3114
Brad Ebinger4f6208e2021-03-23 21:04:45 +00003115 /**
3116 * @deprecated This method is deprecated and is only being kept due to an UnsupportedAppUsage
3117 * tag on getCallState Binder call.
3118 */
3119 @Deprecated
3120 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003121 public int getCallState() {
Brad Ebinger4f6208e2021-03-23 21:04:45 +00003122 if (CompatChanges.isChangeEnabled(
3123 TelecomManager.ENABLE_GET_CALL_STATE_PERMISSION_PROTECTION,
3124 Binder.getCallingUid())) {
3125 // Do not allow this API to be called on API version 31+, it should only be
3126 // called on old apps using this Binder call directly.
3127 throw new SecurityException("This method can only be used for applications "
3128 + "targeting API version 30 or less.");
3129 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003130 final long identity = Binder.clearCallingIdentity();
3131 try {
Brad Ebinger4f6208e2021-03-23 21:04:45 +00003132 Phone phone = getPhone(getDefaultSubscription());
3133 return phone == null ? TelephonyManager.CALL_STATE_IDLE :
3134 PhoneConstantConversions.convertCallState(phone.getState());
3135 } finally {
3136 Binder.restoreCallingIdentity(identity);
3137 }
3138 }
3139
3140 @Override
3141 public int getCallStateForSubscription(int subId, String callingPackage, String featureId) {
3142 if (CompatChanges.isChangeEnabled(
3143 TelecomManager.ENABLE_GET_CALL_STATE_PERMISSION_PROTECTION,
3144 Binder.getCallingUid())) {
3145 // Check READ_PHONE_STATE for API version 31+
3146 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(mApp, subId, callingPackage,
3147 featureId, "getCallStateForSubscription")) {
3148 throw new SecurityException("getCallState requires READ_PHONE_STATE for apps "
3149 + "targeting API level 31+.");
3150 }
3151 }
3152 final long identity = Binder.clearCallingIdentity();
3153 try {
3154 Phone phone = getPhone(subId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003155 return phone == null ? TelephonyManager.CALL_STATE_IDLE :
3156 PhoneConstantConversions.convertCallState(phone.getState());
3157 } finally {
3158 Binder.restoreCallingIdentity(identity);
3159 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003160 }
3161
Sanket Padawe356d7632015-06-22 14:03:32 -07003162 @Override
Nathan Harolde037c472019-06-26 00:41:07 +00003163 public int getDataState() {
Jack Yu285100e2022-12-02 22:48:35 -08003164 return getDataStateForSubId(SubscriptionManager.getDefaultDataSubscriptionId());
Nathan Haroldc4689b12019-06-14 16:58:30 -07003165 }
3166
3167 @Override
3168 public int getDataStateForSubId(int subId) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003169 final long identity = Binder.clearCallingIdentity();
3170 try {
Nathan Haroldc4689b12019-06-14 16:58:30 -07003171 final Phone phone = getPhone(subId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003172 if (phone != null) {
Jack Yu7968c6d2022-07-31 00:43:21 -07003173 return phone.getDataNetworkController().getInternetDataNetworkState();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003174 } else {
3175 return PhoneConstantConversions.convertDataState(
3176 PhoneConstants.DataState.DISCONNECTED);
3177 }
3178 } finally {
3179 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07003180 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003181 }
3182
Sanket Padawe356d7632015-06-22 14:03:32 -07003183 @Override
Jack Yu0eda6842022-04-18 00:34:46 -07003184 public @DataActivityType int getDataActivity() {
Jack Yu285100e2022-12-02 22:48:35 -08003185 return getDataActivityForSubId(SubscriptionManager.getDefaultDataSubscriptionId());
Nathan Haroldc4689b12019-06-14 16:58:30 -07003186 }
3187
3188 @Override
Jack Yu0eda6842022-04-18 00:34:46 -07003189 public @DataActivityType int getDataActivityForSubId(int subId) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003190 final long identity = Binder.clearCallingIdentity();
3191 try {
Nathan Haroldc4689b12019-06-14 16:58:30 -07003192 final Phone phone = getPhone(subId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003193 if (phone != null) {
Jack Yu0eda6842022-04-18 00:34:46 -07003194 return phone.getDataActivityState();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003195 } else {
3196 return TelephonyManager.DATA_ACTIVITY_NONE;
3197 }
3198 } finally {
3199 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07003200 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003201 }
3202
3203 @Override
Meng Wanga10e89e2019-12-09 13:13:01 -08003204 public CellIdentity getCellLocation(String callingPackage, String callingFeatureId) {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08003205 mApp.getSystemService(AppOpsManager.class)
Hall Liu1aa510f2017-11-22 17:40:08 -08003206 .checkPackage(Binder.getCallingUid(), callingPackage);
Hall Liuf19c44f2018-11-27 14:38:17 -08003207
3208 LocationAccessPolicy.LocationPermissionResult locationResult =
3209 LocationAccessPolicy.checkLocationPermission(mApp,
3210 new LocationAccessPolicy.LocationPermissionQuery.Builder()
3211 .setCallingPackage(callingPackage)
Philip P. Moltmann3a2772a2019-10-04 08:15:00 -07003212 .setCallingFeatureId(callingFeatureId)
Hall Liuf19c44f2018-11-27 14:38:17 -08003213 .setCallingPid(Binder.getCallingPid())
3214 .setCallingUid(Binder.getCallingUid())
3215 .setMethod("getCellLocation")
Hall Liu773ba022020-01-24 18:07:12 -08003216 .setMinSdkVersionForCoarse(Build.VERSION_CODES.BASE)
Hall Liuf19c44f2018-11-27 14:38:17 -08003217 .setMinSdkVersionForFine(Build.VERSION_CODES.Q)
3218 .build());
3219 switch (locationResult) {
3220 case DENIED_HARD:
3221 throw new SecurityException("Not allowed to access cell location");
3222 case DENIED_SOFT:
Meng Wanga10e89e2019-12-09 13:13:01 -08003223 return (getDefaultPhone().getPhoneType() == PhoneConstants.PHONE_TYPE_CDMA)
3224 ? new CellIdentityCdma() : new CellIdentityGsm();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003225 }
3226
Narayan Kamathf04b5a12018-01-09 11:47:15 +00003227 WorkSource workSource = getWorkSource(Binder.getCallingUid());
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003228 final long identity = Binder.clearCallingIdentity();
3229 try {
3230 if (DBG_LOC) log("getCellLocation: is active user");
Jack Yu285100e2022-12-02 22:48:35 -08003231 int subId = SubscriptionManager.getDefaultDataSubscriptionId();
Meng Wanga10e89e2019-12-09 13:13:01 -08003232 return (CellIdentity) sendRequest(CMD_GET_CELL_LOCATION, workSource, subId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003233 } finally {
3234 Binder.restoreCallingIdentity(identity);
3235 }
Svetoslav64fad262015-04-14 14:35:21 -07003236 }
3237
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003238 @Override
Jack Yueb1e7fe2020-02-22 19:38:58 -08003239 public String getNetworkCountryIsoForPhone(int phoneId) {
Jonathan Basseribf5362b2017-07-19 12:22:35 -07003240 // Reporting the correct network country is ambiguous when IWLAN could conflict with
3241 // registered cell info, so return a NULL country instead.
3242 final long identity = Binder.clearCallingIdentity();
3243 try {
Malcolm Chen3732c2b2018-07-18 20:15:24 -07003244 if (phoneId == SubscriptionManager.INVALID_PHONE_INDEX) {
3245 // Get default phone in this case.
3246 phoneId = SubscriptionManager.DEFAULT_PHONE_INDEX;
3247 }
Jack Yu285100e2022-12-02 22:48:35 -08003248 final int subId = SubscriptionManager.getSubscriptionId(phoneId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003249 Phone phone = PhoneFactory.getPhone(phoneId);
Nathan Harold532f51c2020-04-21 19:31:10 -07003250 if (phone == null) return "";
3251 ServiceStateTracker sst = phone.getServiceStateTracker();
3252 if (sst == null) return "";
3253 LocaleTracker lt = sst.getLocaleTracker();
3254 if (lt == null) return "";
Shuo Qian9418a922021-03-09 11:21:16 -08003255 return lt.getCurrentCountry();
Jonathan Basseribf5362b2017-07-19 12:22:35 -07003256 } finally {
3257 Binder.restoreCallingIdentity(identity);
3258 }
Jonathan Basseribf5362b2017-07-19 12:22:35 -07003259 }
3260
Nathan Harold7c8d0f12020-05-28 20:40:31 -07003261 /**
3262 * This method was removed due to potential issues caused by performing partial
3263 * updates of service state, and lack of a credible use case.
3264 *
3265 * This has the ability to break the telephony implementation by disabling notification of
3266 * changes in device connectivity. DO NOT USE THIS!
3267 */
Jonathan Basseribf5362b2017-07-19 12:22:35 -07003268 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003269 public void enableLocationUpdates() {
3270 mApp.enforceCallingOrSelfPermission(
3271 android.Manifest.permission.CONTROL_LOCATION_UPDATES, null);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003272 }
3273
Nathan Harold7c8d0f12020-05-28 20:40:31 -07003274 /**
3275 * This method was removed due to potential issues caused by performing partial
3276 * updates of service state, and lack of a credible use case.
3277 *
3278 * This has the ability to break the telephony implementation by disabling notification of
3279 * changes in device connectivity. DO NOT USE THIS!
3280 */
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003281 @Override
3282 public void disableLocationUpdates() {
3283 mApp.enforceCallingOrSelfPermission(
3284 android.Manifest.permission.CONTROL_LOCATION_UPDATES, null);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003285 }
3286
3287 @Override
3288 @SuppressWarnings("unchecked")
Philip P. Moltmann3a2772a2019-10-04 08:15:00 -07003289 public List<NeighboringCellInfo> getNeighboringCellInfo(String callingPackage,
3290 String callingFeatureId) {
Nathan Haroldb55f63b2021-07-27 11:27:38 -07003291 try {
3292 mApp.getSystemService(AppOpsManager.class)
3293 .checkPackage(Binder.getCallingUid(), callingPackage);
3294 } catch (SecurityException e) {
3295 EventLog.writeEvent(0x534e4554, "190619791", Binder.getCallingUid());
3296 throw e;
3297 }
3298
Nathan Haroldf096d982020-11-18 17:18:06 -08003299 final int targetSdk = TelephonyPermissions.getTargetSdk(mApp, callingPackage);
Nathan Harolddbea45a2018-08-30 14:35:07 -07003300 if (targetSdk >= android.os.Build.VERSION_CODES.Q) {
3301 throw new SecurityException(
3302 "getNeighboringCellInfo() is unavailable to callers targeting Q+ SDK levels.");
3303 }
Nathan Haroldb4d55612018-07-20 13:13:08 -07003304
Jordan Liu1617b712019-07-10 15:06:26 -07003305 if (mAppOps.noteOp(AppOpsManager.OPSTR_NEIGHBORING_CELLS, Binder.getCallingUid(),
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003306 callingPackage) != AppOpsManager.MODE_ALLOWED) {
3307 return null;
3308 }
Svetoslav64fad262015-04-14 14:35:21 -07003309
Svetoslav Ganov4a9d4482017-06-20 19:53:35 -07003310 if (DBG_LOC) log("getNeighboringCellInfo: is active user");
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003311
Philip P. Moltmann3a2772a2019-10-04 08:15:00 -07003312 List<CellInfo> info = getAllCellInfo(callingPackage, callingFeatureId);
Nathan Haroldf180aac2018-06-01 18:43:55 -07003313 if (info == null) return null;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003314
Nathan Haroldf180aac2018-06-01 18:43:55 -07003315 List<NeighboringCellInfo> neighbors = new ArrayList<NeighboringCellInfo>();
3316 for (CellInfo ci : info) {
3317 if (ci instanceof CellInfoGsm) {
3318 neighbors.add(new NeighboringCellInfo((CellInfoGsm) ci));
3319 } else if (ci instanceof CellInfoWcdma) {
3320 neighbors.add(new NeighboringCellInfo((CellInfoWcdma) ci));
3321 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003322 }
Nathan Haroldf180aac2018-06-01 18:43:55 -07003323 return (neighbors.size()) > 0 ? neighbors : null;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003324 }
3325
Nathan Haroldfa8da0f2018-09-27 18:51:29 -07003326 private List<CellInfo> getCachedCellInfo() {
3327 List<CellInfo> cellInfos = new ArrayList<CellInfo>();
3328 for (Phone phone : PhoneFactory.getPhones()) {
3329 List<CellInfo> info = phone.getAllCellInfo();
3330 if (info != null) cellInfos.addAll(info);
3331 }
3332 return cellInfos;
3333 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003334
3335 @Override
Philip P. Moltmann3a2772a2019-10-04 08:15:00 -07003336 public List<CellInfo> getAllCellInfo(String callingPackage, String callingFeatureId) {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08003337 mApp.getSystemService(AppOpsManager.class)
Hall Liu1aa510f2017-11-22 17:40:08 -08003338 .checkPackage(Binder.getCallingUid(), callingPackage);
Hall Liuf19c44f2018-11-27 14:38:17 -08003339
3340 LocationAccessPolicy.LocationPermissionResult locationResult =
3341 LocationAccessPolicy.checkLocationPermission(mApp,
3342 new LocationAccessPolicy.LocationPermissionQuery.Builder()
3343 .setCallingPackage(callingPackage)
Philip P. Moltmann3a2772a2019-10-04 08:15:00 -07003344 .setCallingFeatureId(callingFeatureId)
Hall Liuf19c44f2018-11-27 14:38:17 -08003345 .setCallingPid(Binder.getCallingPid())
3346 .setCallingUid(Binder.getCallingUid())
3347 .setMethod("getAllCellInfo")
Nathan Harold5ae50b52019-02-20 15:46:36 -08003348 .setMinSdkVersionForCoarse(Build.VERSION_CODES.BASE)
Hall Liuf19c44f2018-11-27 14:38:17 -08003349 .setMinSdkVersionForFine(Build.VERSION_CODES.Q)
3350 .build());
3351 switch (locationResult) {
3352 case DENIED_HARD:
3353 throw new SecurityException("Not allowed to access cell info");
3354 case DENIED_SOFT:
3355 return new ArrayList<>();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003356 }
3357
Nathan Haroldf096d982020-11-18 17:18:06 -08003358 final int targetSdk = TelephonyPermissions.getTargetSdk(mApp, callingPackage);
Nathan Haroldfa8da0f2018-09-27 18:51:29 -07003359 if (targetSdk >= android.os.Build.VERSION_CODES.Q) {
3360 return getCachedCellInfo();
3361 }
3362
Svetoslav Ganov4a9d4482017-06-20 19:53:35 -07003363 if (DBG_LOC) log("getAllCellInfo: is active user");
Narayan Kamathf04b5a12018-01-09 11:47:15 +00003364 WorkSource workSource = getWorkSource(Binder.getCallingUid());
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003365 final long identity = Binder.clearCallingIdentity();
3366 try {
3367 List<CellInfo> cellInfos = new ArrayList<CellInfo>();
3368 for (Phone phone : PhoneFactory.getPhones()) {
Nathan Harold3ff88932018-08-14 10:19:49 -07003369 final List<CellInfo> info = (List<CellInfo>) sendRequest(
Nathan Harold92bed182018-10-12 18:16:49 -07003370 CMD_GET_ALL_CELL_INFO, null, phone, workSource);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003371 if (info != null) cellInfos.addAll(info);
3372 }
3373 return cellInfos;
3374 } finally {
3375 Binder.restoreCallingIdentity(identity);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003376 }
3377 }
3378
Sailesh Nepalbd76e4e2013-10-27 13:59:44 -07003379 @Override
Philip P. Moltmann3a2772a2019-10-04 08:15:00 -07003380 public void requestCellInfoUpdate(int subId, ICellInfoCallback cb, String callingPackage,
3381 String callingFeatureId) {
3382 requestCellInfoUpdateInternal(subId, cb, callingPackage, callingFeatureId,
3383 getWorkSource(Binder.getCallingUid()));
Nathan Haroldfa8da0f2018-09-27 18:51:29 -07003384 }
3385
3386 @Override
Philip P. Moltmann3a2772a2019-10-04 08:15:00 -07003387 public void requestCellInfoUpdateWithWorkSource(int subId, ICellInfoCallback cb,
3388 String callingPackage, String callingFeatureId, WorkSource workSource) {
Nathan Haroldfa8da0f2018-09-27 18:51:29 -07003389 enforceModifyPermission();
Philip P. Moltmann3a2772a2019-10-04 08:15:00 -07003390 requestCellInfoUpdateInternal(subId, cb, callingPackage, callingFeatureId, workSource);
Nathan Haroldfa8da0f2018-09-27 18:51:29 -07003391 }
3392
Philip P. Moltmann3a2772a2019-10-04 08:15:00 -07003393 private void requestCellInfoUpdateInternal(int subId, ICellInfoCallback cb,
3394 String callingPackage, String callingFeatureId, WorkSource workSource) {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08003395 mApp.getSystemService(AppOpsManager.class)
Nathan Haroldfa8da0f2018-09-27 18:51:29 -07003396 .checkPackage(Binder.getCallingUid(), callingPackage);
Hall Liuf19c44f2018-11-27 14:38:17 -08003397
3398 LocationAccessPolicy.LocationPermissionResult locationResult =
3399 LocationAccessPolicy.checkLocationPermission(mApp,
3400 new LocationAccessPolicy.LocationPermissionQuery.Builder()
3401 .setCallingPackage(callingPackage)
Philip P. Moltmann3a2772a2019-10-04 08:15:00 -07003402 .setCallingFeatureId(callingFeatureId)
Hall Liuf19c44f2018-11-27 14:38:17 -08003403 .setCallingPid(Binder.getCallingPid())
3404 .setCallingUid(Binder.getCallingUid())
3405 .setMethod("requestCellInfoUpdate")
Hall Liud60acc92020-05-21 17:09:35 -07003406 .setMinSdkVersionForCoarse(Build.VERSION_CODES.BASE)
3407 .setMinSdkVersionForFine(Build.VERSION_CODES.BASE)
Hall Liuf19c44f2018-11-27 14:38:17 -08003408 .build());
3409 switch (locationResult) {
3410 case DENIED_HARD:
Nathan Haroldf096d982020-11-18 17:18:06 -08003411 if (TelephonyPermissions
3412 .getTargetSdk(mApp, callingPackage) < Build.VERSION_CODES.Q) {
Hall Liud60acc92020-05-21 17:09:35 -07003413 // Safetynet logging for b/154934934
3414 EventLog.writeEvent(0x534e4554, "154934934", Binder.getCallingUid());
3415 }
Hall Liuf19c44f2018-11-27 14:38:17 -08003416 throw new SecurityException("Not allowed to access cell info");
3417 case DENIED_SOFT:
Nathan Haroldf096d982020-11-18 17:18:06 -08003418 if (TelephonyPermissions
3419 .getTargetSdk(mApp, callingPackage) < Build.VERSION_CODES.Q) {
Hall Liud60acc92020-05-21 17:09:35 -07003420 // Safetynet logging for b/154934934
3421 EventLog.writeEvent(0x534e4554, "154934934", Binder.getCallingUid());
3422 }
Nathan Harold5320c422019-05-09 10:26:08 -07003423 try {
3424 cb.onCellInfo(new ArrayList<CellInfo>());
3425 } catch (RemoteException re) {
3426 // Drop without consequences
3427 }
Hall Liuf19c44f2018-11-27 14:38:17 -08003428 return;
Nathan Haroldfa8da0f2018-09-27 18:51:29 -07003429 }
3430
Nathan Harolda939a962019-05-09 10:13:47 -07003431
3432 final Phone phone = getPhoneFromSubId(subId);
Nathan Haroldfa8da0f2018-09-27 18:51:29 -07003433 if (phone == null) throw new IllegalArgumentException("Invalid Subscription Id: " + subId);
3434
3435 sendRequestAsync(CMD_REQUEST_CELL_INFO_UPDATE, cb, phone, workSource);
3436 }
3437
3438 @Override
Aishwarya Mallampati0603fb12022-08-24 21:16:56 +00003439 public void setCellInfoListRate(int rateInMillis, int subId) {
Jack Yua8d8cb82017-01-16 10:15:34 -08003440 enforceModifyPermission();
Narayan Kamathf04b5a12018-01-09 11:47:15 +00003441 WorkSource workSource = getWorkSource(Binder.getCallingUid());
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003442
3443 final long identity = Binder.clearCallingIdentity();
3444 try {
Aishwarya Mallampati0603fb12022-08-24 21:16:56 +00003445 Phone phone = getPhone(subId);
3446 if (phone == null) {
3447 getDefaultPhone().setCellInfoListRate(rateInMillis, workSource);
3448 } else {
3449 phone.setCellInfoListRate(rateInMillis, workSource);
3450 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003451 } finally {
3452 Binder.restoreCallingIdentity(identity);
3453 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003454 }
3455
Shishir Agrawala9f32182016-04-12 12:00:16 -07003456 @Override
Philip P. Moltmann700a9592019-10-03 11:53:50 -07003457 public String getImeiForSlot(int slotIndex, String callingPackage, String callingFeatureId) {
Jeff Davidson913390f2018-02-23 17:11:49 -08003458 Phone phone = PhoneFactory.getPhone(slotIndex);
3459 if (phone == null) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08003460 return null;
3461 }
Jeff Davidson913390f2018-02-23 17:11:49 -08003462 int subId = phone.getSubId();
Grace Jia0ddb3612021-04-22 13:35:26 -07003463 enforceCallingPackage(callingPackage, Binder.getCallingUid(), "getImeiForSlot");
Michael Groover70af6dc2018-10-01 16:23:15 -07003464 if (!TelephonyPermissions.checkCallingOrSelfReadDeviceIdentifiers(mApp, subId,
Philip P. Moltmann700a9592019-10-03 11:53:50 -07003465 callingPackage, callingFeatureId, "getImeiForSlot")) {
Jeff Davidson913390f2018-02-23 17:11:49 -08003466 return null;
3467 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003468
3469 final long identity = Binder.clearCallingIdentity();
3470 try {
3471 return phone.getImei();
3472 } finally {
3473 Binder.restoreCallingIdentity(identity);
3474 }
Shishir Agrawala9f32182016-04-12 12:00:16 -07003475 }
3476
3477 @Override
arunvoddud5c6ce02022-12-11 06:03:12 +00003478 public String getPrimaryImei(String callingPackage, String callingFeatureId) {
3479 enforceCallingPackage(callingPackage, Binder.getCallingUid(), "getPrimaryImei");
3480 if (!checkCallingOrSelfReadDeviceIdentifiersForAnySub(mApp, callingPackage,
3481 callingFeatureId, "getPrimaryImei")) {
3482 throw new SecurityException("Caller does not have permission");
3483 }
3484 final long identity = Binder.clearCallingIdentity();
3485 try {
3486 for (Phone phone : PhoneFactory.getPhones()) {
3487 if (phone.getImeiType() == Phone.IMEI_TYPE_PRIMARY) {
3488 return phone.getImei();
3489 }
3490 }
3491 throw new UnsupportedOperationException("Operation not supported");
3492 } finally {
3493 Binder.restoreCallingIdentity(identity);
3494 }
3495 }
3496
3497 @Override
David Kelly5e06a7f2018-03-12 14:10:59 +00003498 public String getTypeAllocationCodeForSlot(int slotIndex) {
3499 Phone phone = PhoneFactory.getPhone(slotIndex);
3500 String tac = null;
3501 if (phone != null) {
3502 String imei = phone.getImei();
Vala Zadehab005552021-09-21 15:54:29 -07003503 try {
3504 tac = imei == null ? null : imei.substring(0, TYPE_ALLOCATION_CODE_LENGTH);
3505 } catch (IndexOutOfBoundsException e) {
3506 Log.e(LOG_TAG, "IMEI length shorter than upper index.");
3507 return null;
3508 }
David Kelly5e06a7f2018-03-12 14:10:59 +00003509 }
3510 return tac;
3511 }
3512
3513 @Override
Philip P. Moltmann700a9592019-10-03 11:53:50 -07003514 public String getMeidForSlot(int slotIndex, String callingPackage, String callingFeatureId) {
Shuo Qian13d89152021-05-10 23:58:11 -07003515 try {
3516 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
3517 } catch (SecurityException se) {
3518 EventLog.writeEvent(0x534e4554, "186530496", Binder.getCallingUid());
3519 throw new SecurityException("Package " + callingPackage + " does not belong to "
3520 + Binder.getCallingUid());
3521 }
Jeff Davidson913390f2018-02-23 17:11:49 -08003522 Phone phone = PhoneFactory.getPhone(slotIndex);
3523 if (phone == null) {
Jack Yu2af8d712017-03-15 17:14:14 -07003524 return null;
3525 }
Nazanin Bakhshif782e562018-12-11 15:15:39 -08003526
Jeff Davidson913390f2018-02-23 17:11:49 -08003527 int subId = phone.getSubId();
Michael Groover70af6dc2018-10-01 16:23:15 -07003528 if (!TelephonyPermissions.checkCallingOrSelfReadDeviceIdentifiers(mApp, subId,
Philip P. Moltmann700a9592019-10-03 11:53:50 -07003529 callingPackage, callingFeatureId, "getMeidForSlot")) {
Jeff Davidson913390f2018-02-23 17:11:49 -08003530 return null;
3531 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003532
3533 final long identity = Binder.clearCallingIdentity();
3534 try {
3535 return phone.getMeid();
3536 } finally {
3537 Binder.restoreCallingIdentity(identity);
3538 }
Jack Yu2af8d712017-03-15 17:14:14 -07003539 }
3540
3541 @Override
David Kelly5e06a7f2018-03-12 14:10:59 +00003542 public String getManufacturerCodeForSlot(int slotIndex) {
3543 Phone phone = PhoneFactory.getPhone(slotIndex);
3544 String manufacturerCode = null;
3545 if (phone != null) {
3546 String meid = phone.getMeid();
Vala Zadehab005552021-09-21 15:54:29 -07003547 try {
3548 manufacturerCode =
3549 meid == null ? null : meid.substring(0, MANUFACTURER_CODE_LENGTH);
3550 } catch (IndexOutOfBoundsException e) {
3551 Log.e(LOG_TAG, "MEID length shorter than upper index.");
3552 return null;
3553 }
David Kelly5e06a7f2018-03-12 14:10:59 +00003554 }
3555 return manufacturerCode;
3556 }
3557
3558 @Override
Philip P. Moltmann700a9592019-10-03 11:53:50 -07003559 public String getDeviceSoftwareVersionForSlot(int slotIndex, String callingPackage,
3560 String callingFeatureId) {
Jeff Davidson913390f2018-02-23 17:11:49 -08003561 Phone phone = PhoneFactory.getPhone(slotIndex);
3562 if (phone == null) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08003563 return null;
3564 }
Jeff Davidson913390f2018-02-23 17:11:49 -08003565 int subId = phone.getSubId();
3566 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
Philip P. Moltmann700a9592019-10-03 11:53:50 -07003567 mApp, subId, callingPackage, callingFeatureId,
3568 "getDeviceSoftwareVersionForSlot")) {
Jeff Davidson913390f2018-02-23 17:11:49 -08003569 return null;
3570 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003571
3572 final long identity = Binder.clearCallingIdentity();
3573 try {
3574 return phone.getDeviceSvn();
3575 } finally {
3576 Binder.restoreCallingIdentity(identity);
3577 }
Shishir Agrawala9f32182016-04-12 12:00:16 -07003578 }
3579
fionaxu43304da2017-11-27 22:51:16 -08003580 @Override
3581 public int getSubscriptionCarrierId(int subId) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003582 final long identity = Binder.clearCallingIdentity();
3583 try {
3584 final Phone phone = getPhone(subId);
3585 return phone == null ? TelephonyManager.UNKNOWN_CARRIER_ID : phone.getCarrierId();
3586 } finally {
3587 Binder.restoreCallingIdentity(identity);
3588 }
fionaxu43304da2017-11-27 22:51:16 -08003589 }
3590
3591 @Override
3592 public String getSubscriptionCarrierName(int subId) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003593 final long identity = Binder.clearCallingIdentity();
3594 try {
3595 final Phone phone = getPhone(subId);
3596 return phone == null ? null : phone.getCarrierName();
3597 } finally {
3598 Binder.restoreCallingIdentity(identity);
3599 }
fionaxu43304da2017-11-27 22:51:16 -08003600 }
3601
calvinpanffe225e2018-11-01 19:43:06 +08003602 @Override
chen xu0026ca62019-03-06 15:28:50 -08003603 public int getSubscriptionSpecificCarrierId(int subId) {
chen xu25637222018-11-04 17:17:00 -08003604 final long identity = Binder.clearCallingIdentity();
3605 try {
3606 final Phone phone = getPhone(subId);
3607 return phone == null ? TelephonyManager.UNKNOWN_CARRIER_ID
chen xu0026ca62019-03-06 15:28:50 -08003608 : phone.getSpecificCarrierId();
chen xu25637222018-11-04 17:17:00 -08003609 } finally {
3610 Binder.restoreCallingIdentity(identity);
3611 }
3612 }
3613
3614 @Override
chen xu0026ca62019-03-06 15:28:50 -08003615 public String getSubscriptionSpecificCarrierName(int subId) {
chen xu25637222018-11-04 17:17:00 -08003616 final long identity = Binder.clearCallingIdentity();
3617 try {
3618 final Phone phone = getPhone(subId);
chen xu0026ca62019-03-06 15:28:50 -08003619 return phone == null ? null : phone.getSpecificCarrierName();
chen xu25637222018-11-04 17:17:00 -08003620 } finally {
3621 Binder.restoreCallingIdentity(identity);
3622 }
3623 }
3624
chen xu651eec72018-11-11 19:03:44 -08003625 @Override
chen xu864e11c2018-12-06 22:10:03 -08003626 public int getCarrierIdFromMccMnc(int slotIndex, String mccmnc, boolean isSubscriptionMccMnc) {
3627 if (!isSubscriptionMccMnc) {
3628 enforceReadPrivilegedPermission("getCarrierIdFromMccMnc");
3629 }
chen xu651eec72018-11-11 19:03:44 -08003630 final Phone phone = PhoneFactory.getPhone(slotIndex);
3631 if (phone == null) {
3632 return TelephonyManager.UNKNOWN_CARRIER_ID;
3633 }
3634 final long identity = Binder.clearCallingIdentity();
3635 try {
3636 return CarrierResolver.getCarrierIdFromMccMnc(phone.getContext(), mccmnc);
3637 } finally {
3638 Binder.restoreCallingIdentity(identity);
3639 }
3640 }
3641
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003642 //
3643 // Internal helper methods.
3644 //
3645
Sanket Padaweee13a9b2016-03-08 17:30:28 -08003646 /**
Grace Jia0ddb3612021-04-22 13:35:26 -07003647 * Make sure the caller is the calling package itself
3648 *
3649 * @throws SecurityException if the caller is not the calling package
3650 */
3651 private void enforceCallingPackage(String callingPackage, int callingUid, String message) {
3652 int packageUid = -1;
Grace Jiadbefca02021-04-26 15:13:31 -07003653 PackageManager pm = mApp.getBaseContext().createContextAsUser(
3654 UserHandle.getUserHandleForUid(callingUid), 0).getPackageManager();
Grace Jia0ddb3612021-04-22 13:35:26 -07003655 try {
Grace Jiadbefca02021-04-26 15:13:31 -07003656 packageUid = pm.getPackageUid(callingPackage, 0);
Grace Jia0ddb3612021-04-22 13:35:26 -07003657 } catch (PackageManager.NameNotFoundException e) {
3658 // packageUid is -1
3659 }
3660 if (packageUid != callingUid) {
3661 throw new SecurityException(message + ": Package " + callingPackage
3662 + " does not belong to " + callingUid);
3663 }
3664 }
3665
3666 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003667 * Make sure the caller has the MODIFY_PHONE_STATE permission.
3668 *
3669 * @throws SecurityException if the caller does not have the required permission
3670 */
Gil Cukierman1c0eb932022-12-06 22:28:24 +00003671 @VisibleForTesting
3672 public void enforceModifyPermission() {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003673 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.MODIFY_PHONE_STATE, null);
3674 }
3675
Gil Cukierman1c0eb932022-12-06 22:28:24 +00003676 /**
3677 * Make sure the caller has the MODIFY_PHONE_STATE permission.
3678 *
3679 * @throws SecurityException if the caller does not have the required permission
3680 */
3681 @VisibleForTesting
3682 public void enforceReadPermission() {
3683 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.READ_PHONE_STATE, null);
3684 }
3685
Shuo Qian3b6ee772019-11-13 17:43:31 -08003686 private void enforceActiveEmergencySessionPermission() {
3687 mApp.enforceCallingOrSelfPermission(
3688 android.Manifest.permission.READ_ACTIVE_EMERGENCY_SESSION, null);
3689 }
3690
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003691 /**
3692 * Make sure the caller has the CALL_PHONE permission.
3693 *
3694 * @throws SecurityException if the caller does not have the required permission
3695 */
3696 private void enforceCallPermission() {
3697 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.CALL_PHONE, null);
3698 }
3699
paulhu5a773602019-08-23 19:17:33 +08003700 private void enforceSettingsPermission() {
3701 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.NETWORK_SETTINGS, null);
Stuart Scott8eef64f2015-04-08 15:13:54 -07003702 }
3703
Michele Berionne5e411512020-11-13 02:36:59 +00003704 private void enforceRebootPermission() {
3705 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.REBOOT, null);
3706 }
3707
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003708 private String createTelUrl(String number) {
3709 if (TextUtils.isEmpty(number)) {
3710 return null;
3711 }
3712
Jake Hambye994d462014-02-03 13:10:13 -08003713 return "tel:" + number;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003714 }
3715
Ihab Awadf9e92732013-12-05 18:02:52 -08003716 private static void log(String msg) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003717 Log.d(LOG_TAG, "[PhoneIntfMgr] " + msg);
3718 }
3719
Naveen Kalla1fd79bd2014-08-08 00:48:59 -07003720 private static void logv(String msg) {
3721 Log.v(LOG_TAG, "[PhoneIntfMgr] " + msg);
3722 }
3723
Ihab Awadf9e92732013-12-05 18:02:52 -08003724 private static void loge(String msg) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003725 Log.e(LOG_TAG, "[PhoneIntfMgr] " + msg);
3726 }
3727
Robert Greenwalt36b23af2015-07-06 17:59:14 -07003728 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003729 public int getActivePhoneType() {
Shishir Agrawala9f32182016-04-12 12:00:16 -07003730 return getActivePhoneTypeForSlot(getSlotForDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07003731 }
3732
Sanket Padawe356d7632015-06-22 14:03:32 -07003733 @Override
Sanket Padawe13bac7b2017-03-20 15:04:47 -07003734 public int getActivePhoneTypeForSlot(int slotIndex) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003735 final long identity = Binder.clearCallingIdentity();
3736 try {
3737 final Phone phone = PhoneFactory.getPhone(slotIndex);
3738 if (phone == null) {
3739 return PhoneConstants.PHONE_TYPE_NONE;
3740 } else {
3741 return phone.getPhoneType();
3742 }
3743 } finally {
3744 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07003745 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003746 }
3747
3748 /**
3749 * Returns the CDMA ERI icon index to display
3750 */
Robert Greenwalt36b23af2015-07-06 17:59:14 -07003751 @Override
Philip P. Moltmann700a9592019-10-03 11:53:50 -07003752 public int getCdmaEriIconIndex(String callingPackage, String callingFeatureId) {
3753 return getCdmaEriIconIndexForSubscriber(getDefaultSubscription(), callingPackage,
3754 callingFeatureId);
Wink Saville36469e72014-06-11 15:17:00 -07003755 }
3756
Sanket Padawe356d7632015-06-22 14:03:32 -07003757 @Override
Philip P. Moltmann700a9592019-10-03 11:53:50 -07003758 public int getCdmaEriIconIndexForSubscriber(int subId, String callingPackage,
3759 String callingFeatureId) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08003760 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
Philip P. Moltmann700a9592019-10-03 11:53:50 -07003761 mApp, subId, callingPackage, callingFeatureId,
3762 "getCdmaEriIconIndexForSubscriber")) {
Robert Greenwalt36b23af2015-07-06 17:59:14 -07003763 return -1;
3764 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003765
3766 final long identity = Binder.clearCallingIdentity();
3767 try {
3768 final Phone phone = getPhone(subId);
3769 if (phone != null) {
3770 return phone.getCdmaEriIconIndex();
3771 } else {
3772 return -1;
3773 }
3774 } finally {
3775 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07003776 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003777 }
3778
3779 /**
3780 * Returns the CDMA ERI icon mode,
3781 * 0 - ON
3782 * 1 - FLASHING
3783 */
Robert Greenwalt36b23af2015-07-06 17:59:14 -07003784 @Override
Philip P. Moltmann700a9592019-10-03 11:53:50 -07003785 public int getCdmaEriIconMode(String callingPackage, String callingFeatureId) {
3786 return getCdmaEriIconModeForSubscriber(getDefaultSubscription(), callingPackage,
3787 callingFeatureId);
Wink Saville36469e72014-06-11 15:17:00 -07003788 }
3789
Sanket Padawe356d7632015-06-22 14:03:32 -07003790 @Override
Philip P. Moltmann700a9592019-10-03 11:53:50 -07003791 public int getCdmaEriIconModeForSubscriber(int subId, String callingPackage,
3792 String callingFeatureId) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08003793 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
Philip P. Moltmann700a9592019-10-03 11:53:50 -07003794 mApp, subId, callingPackage, callingFeatureId,
3795 "getCdmaEriIconModeForSubscriber")) {
Robert Greenwalt36b23af2015-07-06 17:59:14 -07003796 return -1;
3797 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003798
3799 final long identity = Binder.clearCallingIdentity();
3800 try {
3801 final Phone phone = getPhone(subId);
3802 if (phone != null) {
3803 return phone.getCdmaEriIconMode();
3804 } else {
3805 return -1;
3806 }
3807 } finally {
3808 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07003809 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003810 }
3811
3812 /**
3813 * Returns the CDMA ERI text,
3814 */
Robert Greenwalt36b23af2015-07-06 17:59:14 -07003815 @Override
Philip P. Moltmann700a9592019-10-03 11:53:50 -07003816 public String getCdmaEriText(String callingPackage, String callingFeatureId) {
3817 return getCdmaEriTextForSubscriber(getDefaultSubscription(), callingPackage,
3818 callingFeatureId);
Wink Saville36469e72014-06-11 15:17:00 -07003819 }
3820
Sanket Padawe356d7632015-06-22 14:03:32 -07003821 @Override
Philip P. Moltmann700a9592019-10-03 11:53:50 -07003822 public String getCdmaEriTextForSubscriber(int subId, String callingPackage,
3823 String callingFeatureId) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08003824 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
Philip P. Moltmann700a9592019-10-03 11:53:50 -07003825 mApp, subId, callingPackage, callingFeatureId,
3826 "getCdmaEriIconTextForSubscriber")) {
Robert Greenwalt36b23af2015-07-06 17:59:14 -07003827 return null;
3828 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003829
3830 final long identity = Binder.clearCallingIdentity();
3831 try {
3832 final Phone phone = getPhone(subId);
3833 if (phone != null) {
3834 return phone.getCdmaEriText();
3835 } else {
3836 return null;
3837 }
3838 } finally {
3839 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07003840 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003841 }
3842
3843 /**
Junda Liuca05d5d2014-08-14 22:36:34 -07003844 * Returns the CDMA MDN.
3845 */
Sanket Padawe356d7632015-06-22 14:03:32 -07003846 @Override
Wink Savilleb564aae2014-10-23 10:18:09 -07003847 public String getCdmaMdn(int subId) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08003848 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
3849 mApp, subId, "getCdmaMdn");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003850
3851 final long identity = Binder.clearCallingIdentity();
3852 try {
3853 final Phone phone = getPhone(subId);
Nazanin Bakhshif782e562018-12-11 15:15:39 -08003854 if (phone != null && phone.getPhoneType() == PhoneConstants.PHONE_TYPE_CDMA) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003855 return phone.getLine1Number();
3856 } else {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08003857 loge("getCdmaMdn: no phone found. Invalid subId: " + subId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003858 return null;
3859 }
3860 } finally {
3861 Binder.restoreCallingIdentity(identity);
Junda Liuca05d5d2014-08-14 22:36:34 -07003862 }
3863 }
3864
3865 /**
3866 * Returns the CDMA MIN.
3867 */
Sanket Padawe356d7632015-06-22 14:03:32 -07003868 @Override
Wink Savilleb564aae2014-10-23 10:18:09 -07003869 public String getCdmaMin(int subId) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08003870 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
3871 mApp, subId, "getCdmaMin");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003872
3873 final long identity = Binder.clearCallingIdentity();
3874 try {
3875 final Phone phone = getPhone(subId);
3876 if (phone != null && phone.getPhoneType() == PhoneConstants.PHONE_TYPE_CDMA) {
3877 return phone.getCdmaMin();
3878 } else {
3879 return null;
3880 }
3881 } finally {
3882 Binder.restoreCallingIdentity(identity);
Junda Liuca05d5d2014-08-14 22:36:34 -07003883 }
3884 }
3885
Hall Liud892bec2018-11-30 14:51:45 -08003886 @Override
3887 public void requestNumberVerification(PhoneNumberRange range, long timeoutMillis,
3888 INumberVerificationCallback callback, String callingPackage) {
3889 if (mApp.checkCallingOrSelfPermission(android.Manifest.permission.MODIFY_PHONE_STATE)
3890 != PERMISSION_GRANTED) {
3891 throw new SecurityException("Caller must hold the MODIFY_PHONE_STATE permission");
3892 }
3893 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
3894
3895 String authorizedPackage = NumberVerificationManager.getAuthorizedPackage(mApp);
3896 if (!TextUtils.equals(callingPackage, authorizedPackage)) {
Hall Liub9d8feb2021-01-13 10:28:04 -08003897 throw new SecurityException("Calling package must be configured in the device config: "
3898 + "calling package: " + callingPackage
3899 + ", configured package: " + authorizedPackage);
Hall Liud892bec2018-11-30 14:51:45 -08003900 }
3901
3902 if (range == null) {
3903 throw new NullPointerException("Range must be non-null");
3904 }
3905
3906 timeoutMillis = Math.min(timeoutMillis,
Hall Liubd069e32019-02-28 18:56:30 -08003907 TelephonyManager.getMaxNumberVerificationTimeoutMillis());
Hall Liud892bec2018-11-30 14:51:45 -08003908
3909 NumberVerificationManager.getInstance().requestVerification(range, callback, timeoutMillis);
3910 }
3911
Junda Liuca05d5d2014-08-14 22:36:34 -07003912 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003913 * Returns true if CDMA provisioning needs to run.
3914 */
3915 public boolean needsOtaServiceProvisioning() {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003916 final long identity = Binder.clearCallingIdentity();
3917 try {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08003918 return getDefaultPhone().needsOtaServiceProvisioning();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003919 } finally {
3920 Binder.restoreCallingIdentity(identity);
3921 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003922 }
3923
3924 /**
Shishir Agrawal76d5da92014-11-09 16:17:25 -08003925 * Sets the voice mail number of a given subId.
3926 */
3927 @Override
3928 public boolean setVoiceMailNumber(int subId, String alphaTag, String number) {
Shuo Qian2c0ae432019-12-05 11:40:37 -08003929 TelephonyPermissions.enforceCallingOrSelfCarrierPrivilege(
3930 mApp, subId, "setVoiceMailNumber");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003931
3932 final long identity = Binder.clearCallingIdentity();
3933 try {
3934 Boolean success = (Boolean) sendRequest(CMD_SET_VOICEMAIL_NUMBER,
3935 new Pair<String, String>(alphaTag, number), new Integer(subId));
3936 return success;
3937 } finally {
3938 Binder.restoreCallingIdentity(identity);
3939 }
Shishir Agrawal76d5da92014-11-09 16:17:25 -08003940 }
3941
Ta-wei Yen87c49842016-05-13 21:19:52 -07003942 @Override
Ta-wei Yenc9df0432017-04-17 17:09:07 -07003943 public Bundle getVisualVoicemailSettings(String callingPackage, int subId) {
3944 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
Tyler Gunn5ddfdc92019-10-31 13:08:23 -07003945 TelecomManager tm = mApp.getSystemService(TelecomManager.class);
3946 String systemDialer = tm.getSystemDialerPackage();
Ta-wei Yenc9df0432017-04-17 17:09:07 -07003947 if (!TextUtils.equals(callingPackage, systemDialer)) {
3948 throw new SecurityException("caller must be system dialer");
3949 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003950
3951 final long identity = Binder.clearCallingIdentity();
3952 try {
3953 PhoneAccountHandle phoneAccountHandle = PhoneAccountHandleConverter.fromSubId(subId);
3954 if (phoneAccountHandle == null) {
3955 return null;
3956 }
Nazanin Bakhshif782e562018-12-11 15:15:39 -08003957 return VisualVoicemailSettingsUtil.dump(mApp, phoneAccountHandle);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003958 } finally {
3959 Binder.restoreCallingIdentity(identity);
Ta-wei Yenc9df0432017-04-17 17:09:07 -07003960 }
Ta-wei Yenc9df0432017-04-17 17:09:07 -07003961 }
3962
3963 @Override
Philip P. Moltmann700a9592019-10-03 11:53:50 -07003964 public String getVisualVoicemailPackageName(String callingPackage, String callingFeatureId,
3965 int subId) {
Ta-wei Yendca928f2017-01-10 16:17:08 -08003966 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
Jeff Davidson7e17e312018-02-13 18:17:36 -08003967 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
Philip P. Moltmann700a9592019-10-03 11:53:50 -07003968 mApp, subId, callingPackage, callingFeatureId,
3969 "getVisualVoicemailPackageName")) {
Ta-wei Yendca928f2017-01-10 16:17:08 -08003970 return null;
3971 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003972
Jeff Davidsona8e4e242018-03-15 17:16:18 -07003973 final long identity = Binder.clearCallingIdentity();
3974 try {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08003975 return RemoteVvmTaskManager.getRemotePackage(mApp, subId).getPackageName();
Jeff Davidsona8e4e242018-03-15 17:16:18 -07003976 } finally {
3977 Binder.restoreCallingIdentity(identity);
3978 }
Ta-wei Yendca928f2017-01-10 16:17:08 -08003979 }
3980
3981 @Override
Ta-wei Yenb6929602016-05-24 15:48:27 -07003982 public void enableVisualVoicemailSmsFilter(String callingPackage, int subId,
3983 VisualVoicemailSmsFilterSettings settings) {
3984 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003985
3986 final long identity = Binder.clearCallingIdentity();
3987 try {
3988 VisualVoicemailSmsFilterConfig.enableVisualVoicemailSmsFilter(
Nazanin Bakhshif782e562018-12-11 15:15:39 -08003989 mApp, callingPackage, subId, settings);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003990 } finally {
3991 Binder.restoreCallingIdentity(identity);
3992 }
Ta-wei Yen87c49842016-05-13 21:19:52 -07003993 }
3994
3995 @Override
Ta-wei Yenb6929602016-05-24 15:48:27 -07003996 public void disableVisualVoicemailSmsFilter(String callingPackage, int subId) {
3997 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003998
3999 final long identity = Binder.clearCallingIdentity();
4000 try {
4001 VisualVoicemailSmsFilterConfig.disableVisualVoicemailSmsFilter(
Nazanin Bakhshif782e562018-12-11 15:15:39 -08004002 mApp, callingPackage, subId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004003 } finally {
4004 Binder.restoreCallingIdentity(identity);
4005 }
Ta-wei Yen87c49842016-05-13 21:19:52 -07004006 }
4007
4008 @Override
Ta-wei Yenb6929602016-05-24 15:48:27 -07004009 public VisualVoicemailSmsFilterSettings getVisualVoicemailSmsFilterSettings(
4010 String callingPackage, int subId) {
4011 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004012
4013 final long identity = Binder.clearCallingIdentity();
4014 try {
4015 return VisualVoicemailSmsFilterConfig.getVisualVoicemailSmsFilterSettings(
Nazanin Bakhshif782e562018-12-11 15:15:39 -08004016 mApp, callingPackage, subId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004017 } finally {
4018 Binder.restoreCallingIdentity(identity);
4019 }
Ta-wei Yen87c49842016-05-13 21:19:52 -07004020 }
4021
4022 @Override
Ta-wei Yen30a69c82016-12-27 14:52:32 -08004023 public VisualVoicemailSmsFilterSettings getActiveVisualVoicemailSmsFilterSettings(int subId) {
Brad Ebinger35c841c2018-10-01 10:40:55 -07004024 enforceReadPrivilegedPermission("getActiveVisualVoicemailSmsFilterSettings");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004025
4026 final long identity = Binder.clearCallingIdentity();
4027 try {
4028 return VisualVoicemailSmsFilterConfig.getActiveVisualVoicemailSmsFilterSettings(
Nazanin Bakhshif782e562018-12-11 15:15:39 -08004029 mApp, subId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004030 } finally {
4031 Binder.restoreCallingIdentity(identity);
4032 }
Ta-wei Yen30a69c82016-12-27 14:52:32 -08004033 }
4034
4035 @Override
Philip P. Moltmann2f6f8ce2020-03-18 18:17:02 -07004036 public void sendVisualVoicemailSmsForSubscriber(String callingPackage,
4037 String callingAttributionTag, int subId, String number, int port, String text,
4038 PendingIntent sentIntent) {
Ta-wei Yen30a69c82016-12-27 14:52:32 -08004039 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
Ta-wei Yen527a9c02017-01-06 15:29:25 -08004040 enforceVisualVoicemailPackage(callingPackage, subId);
Ta-wei Yen30a69c82016-12-27 14:52:32 -08004041 enforceSendSmsPermission();
Amit Mahajandccb3f12019-05-13 13:48:32 -07004042 SmsController smsController = PhoneFactory.getSmsController();
Philip P. Moltmann2f6f8ce2020-03-18 18:17:02 -07004043 smsController.sendVisualVoicemailSmsForSubscriber(callingPackage, callingAttributionTag,
4044 subId, number, port, text, sentIntent);
Ta-wei Yen87c49842016-05-13 21:19:52 -07004045 }
Amit Mahajandccb3f12019-05-13 13:48:32 -07004046
Shishir Agrawal76d5da92014-11-09 16:17:25 -08004047 /**
fionaxu0152e512016-11-14 13:36:14 -08004048 * Sets the voice activation state of a given subId.
4049 */
4050 @Override
4051 public void setVoiceActivationState(int subId, int activationState) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08004052 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
4053 mApp, subId, "setVoiceActivationState");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004054
4055 final long identity = Binder.clearCallingIdentity();
4056 try {
4057 final Phone phone = getPhone(subId);
4058 if (phone != null) {
4059 phone.setVoiceActivationState(activationState);
4060 } else {
4061 loge("setVoiceActivationState fails with invalid subId: " + subId);
4062 }
4063 } finally {
4064 Binder.restoreCallingIdentity(identity);
fionaxu0152e512016-11-14 13:36:14 -08004065 }
4066 }
4067
4068 /**
4069 * Sets the data activation state of a given subId.
4070 */
4071 @Override
4072 public void setDataActivationState(int subId, int activationState) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08004073 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
4074 mApp, subId, "setDataActivationState");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004075
4076 final long identity = Binder.clearCallingIdentity();
4077 try {
4078 final Phone phone = getPhone(subId);
4079 if (phone != null) {
4080 phone.setDataActivationState(activationState);
4081 } else {
Taesu Leef8fbed92019-10-07 18:47:02 +09004082 loge("setDataActivationState fails with invalid subId: " + subId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004083 }
4084 } finally {
4085 Binder.restoreCallingIdentity(identity);
fionaxu0152e512016-11-14 13:36:14 -08004086 }
4087 }
4088
4089 /**
4090 * Returns the voice activation state of a given subId.
4091 */
4092 @Override
4093 public int getVoiceActivationState(int subId, String callingPackage) {
Brad Ebinger35c841c2018-10-01 10:40:55 -07004094 enforceReadPrivilegedPermission("getVoiceActivationState");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004095
fionaxu0152e512016-11-14 13:36:14 -08004096 final Phone phone = getPhone(subId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004097 final long identity = Binder.clearCallingIdentity();
4098 try {
4099 if (phone != null) {
4100 return phone.getVoiceActivationState();
4101 } else {
4102 return TelephonyManager.SIM_ACTIVATION_STATE_UNKNOWN;
4103 }
4104 } finally {
4105 Binder.restoreCallingIdentity(identity);
fionaxu0152e512016-11-14 13:36:14 -08004106 }
4107 }
4108
4109 /**
4110 * Returns the data activation state of a given subId.
4111 */
4112 @Override
4113 public int getDataActivationState(int subId, String callingPackage) {
Brad Ebinger35c841c2018-10-01 10:40:55 -07004114 enforceReadPrivilegedPermission("getDataActivationState");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004115
fionaxu0152e512016-11-14 13:36:14 -08004116 final Phone phone = getPhone(subId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004117 final long identity = Binder.clearCallingIdentity();
4118 try {
4119 if (phone != null) {
4120 return phone.getDataActivationState();
4121 } else {
4122 return TelephonyManager.SIM_ACTIVATION_STATE_UNKNOWN;
4123 }
4124 } finally {
4125 Binder.restoreCallingIdentity(identity);
fionaxu0152e512016-11-14 13:36:14 -08004126 }
4127 }
4128
4129 /**
Wink Saville36469e72014-06-11 15:17:00 -07004130 * Returns the unread count of voicemails for a subId
4131 */
Sanket Padawe356d7632015-06-22 14:03:32 -07004132 @Override
Philip P. Moltmann700a9592019-10-03 11:53:50 -07004133 public int getVoiceMessageCountForSubscriber(int subId, String callingPackage,
4134 String callingFeatureId) {
Brad Ebingerf7664ba2018-11-29 12:43:38 -08004135 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
Philip P. Moltmann700a9592019-10-03 11:53:50 -07004136 mApp, subId, callingPackage, callingFeatureId,
4137 "getVoiceMessageCountForSubscriber")) {
Brad Ebingerf7664ba2018-11-29 12:43:38 -08004138 return 0;
4139 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004140 final long identity = Binder.clearCallingIdentity();
4141 try {
4142 final Phone phone = getPhone(subId);
4143 if (phone != null) {
4144 return phone.getVoiceMessageCount();
4145 } else {
4146 return 0;
4147 }
4148 } finally {
4149 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07004150 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07004151 }
4152
4153 /**
pkanwar8a4dcfb2017-01-19 13:43:16 -08004154 * returns true, if the device is in a state where both voice and data
4155 * are supported simultaneously. This can change based on location or network condition.
4156 */
4157 @Override
4158 public boolean isConcurrentVoiceAndDataAllowed(int subId) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004159 final long identity = Binder.clearCallingIdentity();
4160 try {
4161 final Phone phone = getPhone(subId);
4162 return (phone == null ? false : phone.isConcurrentVoiceAndDataAllowed());
4163 } finally {
4164 Binder.restoreCallingIdentity(identity);
4165 }
pkanwar8a4dcfb2017-01-19 13:43:16 -08004166 }
4167
4168 /**
fionaxu235cc5e2017-03-06 22:25:57 -08004169 * Send the dialer code if called from the current default dialer or the caller has
4170 * carrier privilege.
4171 * @param inputCode The dialer code to send
4172 */
4173 @Override
4174 public void sendDialerSpecialCode(String callingPackage, String inputCode) {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08004175 final Phone defaultPhone = getDefaultPhone();
fionaxu235cc5e2017-03-06 22:25:57 -08004176 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
Tyler Gunn5ddfdc92019-10-31 13:08:23 -07004177 TelecomManager tm = defaultPhone.getContext().getSystemService(TelecomManager.class);
4178 String defaultDialer = tm.getDefaultDialerPackage();
fionaxu235cc5e2017-03-06 22:25:57 -08004179 if (!TextUtils.equals(callingPackage, defaultDialer)) {
Shuo Qian2c0ae432019-12-05 11:40:37 -08004180 TelephonyPermissions.enforceCallingOrSelfCarrierPrivilege(mApp,
Jeff Davidson7e17e312018-02-13 18:17:36 -08004181 getDefaultSubscription(), "sendDialerSpecialCode");
fionaxu235cc5e2017-03-06 22:25:57 -08004182 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004183
4184 final long identity = Binder.clearCallingIdentity();
4185 try {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08004186 defaultPhone.sendDialerSpecialCode(inputCode);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004187 } finally {
4188 Binder.restoreCallingIdentity(identity);
4189 }
fionaxu235cc5e2017-03-06 22:25:57 -08004190 }
4191
Pengquan Menga1bb6272018-09-06 09:59:22 -07004192 @Override
4193 public int getNetworkSelectionMode(int subId) {
shilufc958392020-01-20 11:36:01 -08004194 TelephonyPermissions
Nathan Harold62c68512021-04-06 11:26:02 -07004195 .enforceCallingOrSelfReadPrecisePhoneStatePermissionOrCarrierPrivilege(
shilufc958392020-01-20 11:36:01 -08004196 mApp, subId, "getNetworkSelectionMode");
4197 final long identity = Binder.clearCallingIdentity();
4198 try {
4199 if (!isActiveSubscription(subId)) {
4200 return TelephonyManager.NETWORK_SELECTION_MODE_UNKNOWN;
4201 }
4202 return (int) sendRequest(CMD_GET_NETWORK_SELECTION_MODE, null /* argument */, subId);
4203 } finally {
4204 Binder.restoreCallingIdentity(identity);
Pengquan Menge92a50d2018-09-21 15:54:48 -07004205 }
Pengquan Menga1bb6272018-09-06 09:59:22 -07004206 }
4207
Brad Ebinger35c841c2018-10-01 10:40:55 -07004208 @Override
Brad Ebingerb2b65522019-03-15 13:48:47 -07004209 public boolean isInEmergencySmsMode() {
4210 enforceReadPrivilegedPermission("isInEmergencySmsMode");
4211 final long identity = Binder.clearCallingIdentity();
4212 try {
4213 for (Phone phone : PhoneFactory.getPhones()) {
4214 if (phone.isInEmergencySmsMode()) {
4215 return true;
4216 }
4217 }
4218 } finally {
4219 Binder.restoreCallingIdentity(identity);
4220 }
4221 return false;
4222 }
4223
shilu366312e2019-12-17 09:28:10 -08004224 /**
4225 * Requires carrier privileges or READ_PRECISE_PHONE_STATE permission.
4226 * @param subId The subscription to use to check the configuration.
4227 * @param c The callback that will be used to send the result.
4228 */
Brad Ebingerb2b65522019-03-15 13:48:47 -07004229 @Override
Brad Ebinger9878b0b2018-11-08 17:43:22 -08004230 public void registerImsRegistrationCallback(int subId, IImsRegistrationCallback c)
4231 throws RemoteException {
Nathan Harold62c68512021-04-06 11:26:02 -07004232 TelephonyPermissions.enforceCallingOrSelfReadPrecisePhoneStatePermissionOrCarrierPrivilege(
Rambo Wang37f9c242020-02-10 14:45:28 -08004233 mApp, subId, "registerImsRegistrationCallback");
Brad Ebingera2628302022-02-18 03:44:55 +00004234
4235 if (!ImsManager.isImsSupportedOnDevice(mApp)) {
4236 throw new ServiceSpecificException(ImsException.CODE_ERROR_UNSUPPORTED_OPERATION,
4237 "IMS not available on device.");
4238 }
Brad Ebinger35c841c2018-10-01 10:40:55 -07004239 final long token = Binder.clearCallingIdentity();
4240 try {
Brad Ebingera2628302022-02-18 03:44:55 +00004241 int slotId = getSlotIndexOrException(subId);
4242 verifyImsMmTelConfiguredOrThrow(slotId);
joonhunshin49f0aed2022-08-05 08:33:05 +00004243
4244 ImsStateCallbackController controller = ImsStateCallbackController.getInstance();
4245 if (controller != null) {
4246 ImsManager imsManager = controller.getImsManager(subId);
4247 if (imsManager != null) {
4248 imsManager.addRegistrationCallbackForSubscription(c, subId);
4249 } else {
4250 throw new ServiceSpecificException(ImsException.CODE_ERROR_SERVICE_UNAVAILABLE);
4251 }
4252 } else {
4253 throw new ServiceSpecificException(ImsException.CODE_ERROR_UNSUPPORTED_OPERATION);
4254 }
Brad Ebinger1ce9c432019-07-16 13:19:44 -07004255 } catch (ImsException e) {
4256 throw new ServiceSpecificException(e.getCode());
Brad Ebinger35c841c2018-10-01 10:40:55 -07004257 } finally {
4258 Binder.restoreCallingIdentity(token);
4259 }
4260 }
4261
shilu366312e2019-12-17 09:28:10 -08004262 /**
4263 * Requires carrier privileges or READ_PRECISE_PHONE_STATE permission.
4264 * @param subId The subscription to use to check the configuration.
4265 * @param c The callback that will be used to send the result.
4266 */
Brad Ebinger35c841c2018-10-01 10:40:55 -07004267 @Override
Brad Ebinger9878b0b2018-11-08 17:43:22 -08004268 public void unregisterImsRegistrationCallback(int subId, IImsRegistrationCallback c) {
Nathan Harold62c68512021-04-06 11:26:02 -07004269 TelephonyPermissions.enforceCallingOrSelfReadPrecisePhoneStatePermissionOrCarrierPrivilege(
Rambo Wang37f9c242020-02-10 14:45:28 -08004270 mApp, subId, "unregisterImsRegistrationCallback");
Brad Ebinger4ae57f92019-01-09 16:51:30 -08004271 if (!SubscriptionManager.isValidSubscriptionId(subId)) {
4272 throw new IllegalArgumentException("Invalid Subscription ID: " + subId);
4273 }
Meng Wangafbc5852019-09-19 17:37:13 -07004274 final long token = Binder.clearCallingIdentity();
joonhunshin49f0aed2022-08-05 08:33:05 +00004275
Meng Wangafbc5852019-09-19 17:37:13 -07004276 try {
joonhunshin49f0aed2022-08-05 08:33:05 +00004277 ImsStateCallbackController controller = ImsStateCallbackController.getInstance();
4278 if (controller != null) {
4279 ImsManager imsManager = controller.getImsManager(subId);
4280 if (imsManager != null) {
4281 imsManager.removeRegistrationCallbackForSubscription(c, subId);
4282 } else {
4283 Log.i(LOG_TAG, "unregisterImsRegistrationCallback: " + subId
4284 + "is inactive, ignoring unregister.");
4285 // If the ImsManager is not valid, just return, since the callback
4286 // will already have been removed internally.
4287 }
4288 }
Meng Wangafbc5852019-09-19 17:37:13 -07004289 } finally {
4290 Binder.restoreCallingIdentity(token);
4291 }
Brad Ebinger35c841c2018-10-01 10:40:55 -07004292 }
4293
Brad Ebingera34a6c22019-10-22 17:36:18 -07004294 /**
4295 * Get the IMS service registration state for the MmTelFeature associated with this sub id.
4296 */
4297 @Override
4298 public void getImsMmTelRegistrationState(int subId, IIntegerConsumer consumer) {
4299 enforceReadPrivilegedPermission("getImsMmTelRegistrationState");
4300 if (!ImsManager.isImsSupportedOnDevice(mApp)) {
4301 throw new ServiceSpecificException(ImsException.CODE_ERROR_UNSUPPORTED_OPERATION,
4302 "IMS not available on device.");
4303 }
4304 final long token = Binder.clearCallingIdentity();
4305 try {
4306 Phone phone = getPhone(subId);
4307 if (phone == null) {
4308 Log.w(LOG_TAG, "getImsMmTelRegistrationState: called with an invalid subscription '"
4309 + subId + "'");
4310 throw new ServiceSpecificException(ImsException.CODE_ERROR_INVALID_SUBSCRIPTION);
4311 }
4312 phone.getImsRegistrationState(regState -> {
4313 try {
4314 consumer.accept((regState == null)
4315 ? RegistrationManager.REGISTRATION_STATE_NOT_REGISTERED : regState);
4316 } catch (RemoteException e) {
4317 // Ignore if the remote process is no longer available to call back.
4318 Log.w(LOG_TAG, "getImsMmTelRegistrationState: callback not available.");
4319 }
4320 });
4321 } finally {
4322 Binder.restoreCallingIdentity(token);
4323 }
4324 }
4325
4326 /**
4327 * Get the transport type for the IMS service registration state.
4328 */
4329 @Override
4330 public void getImsMmTelRegistrationTransportType(int subId, IIntegerConsumer consumer) {
Nathan Harold62c68512021-04-06 11:26:02 -07004331 TelephonyPermissions.enforceCallingOrSelfReadPrecisePhoneStatePermissionOrCarrierPrivilege(
Rambo Wang37f9c242020-02-10 14:45:28 -08004332 mApp, subId, "getImsMmTelRegistrationTransportType");
Brad Ebingera34a6c22019-10-22 17:36:18 -07004333 if (!ImsManager.isImsSupportedOnDevice(mApp)) {
4334 throw new ServiceSpecificException(ImsException.CODE_ERROR_UNSUPPORTED_OPERATION,
4335 "IMS not available on device.");
4336 }
4337 final long token = Binder.clearCallingIdentity();
4338 try {
4339 Phone phone = getPhone(subId);
4340 if (phone == null) {
4341 Log.w(LOG_TAG, "getImsMmTelRegistrationState: called with an invalid subscription '"
4342 + subId + "'");
4343 throw new ServiceSpecificException(ImsException.CODE_ERROR_INVALID_SUBSCRIPTION);
4344 }
4345 phone.getImsRegistrationTech(regTech -> {
4346 // Convert registration tech from ImsRegistrationImplBase -> RegistrationManager
4347 int regTechConverted = (regTech == null)
4348 ? ImsRegistrationImplBase.REGISTRATION_TECH_NONE : regTech;
4349 regTechConverted = RegistrationManager.IMS_REG_TO_ACCESS_TYPE_MAP.get(
4350 regTechConverted);
4351 try {
4352 consumer.accept(regTechConverted);
4353 } catch (RemoteException e) {
4354 // Ignore if the remote process is no longer available to call back.
4355 Log.w(LOG_TAG, "getImsMmTelRegistrationState: callback not available.");
4356 }
4357 });
4358 } finally {
4359 Binder.restoreCallingIdentity(token);
4360 }
4361 }
4362
shilu366312e2019-12-17 09:28:10 -08004363 /**
4364 * Requires carrier privileges or READ_PRECISE_PHONE_STATE permission.
4365 * @param subId The subscription to use to check the configuration.
4366 * @param c The callback that will be used to send the result.
4367 */
Brad Ebinger35c841c2018-10-01 10:40:55 -07004368 @Override
Brad Ebinger9878b0b2018-11-08 17:43:22 -08004369 public void registerMmTelCapabilityCallback(int subId, IImsCapabilityCallback c)
4370 throws RemoteException {
Nathan Harold62c68512021-04-06 11:26:02 -07004371 TelephonyPermissions.enforceCallingOrSelfReadPrecisePhoneStatePermissionOrCarrierPrivilege(
Rambo Wang37f9c242020-02-10 14:45:28 -08004372 mApp, subId, "registerMmTelCapabilityCallback");
Brad Ebingera2628302022-02-18 03:44:55 +00004373 if (!ImsManager.isImsSupportedOnDevice(mApp)) {
4374 throw new ServiceSpecificException(ImsException.CODE_ERROR_UNSUPPORTED_OPERATION,
4375 "IMS not available on device.");
4376 }
Brad Ebinger35c841c2018-10-01 10:40:55 -07004377 final long token = Binder.clearCallingIdentity();
4378 try {
Brad Ebingera2628302022-02-18 03:44:55 +00004379 int slotId = getSlotIndexOrException(subId);
4380 verifyImsMmTelConfiguredOrThrow(slotId);
4381 ImsManager.getInstance(mApp, slotId).addCapabilitiesCallbackForSubscription(c, subId);
Brad Ebinger1ce9c432019-07-16 13:19:44 -07004382 } catch (ImsException e) {
4383 throw new ServiceSpecificException(e.getCode());
Brad Ebinger35c841c2018-10-01 10:40:55 -07004384 } finally {
4385 Binder.restoreCallingIdentity(token);
4386 }
4387 }
4388
shilu366312e2019-12-17 09:28:10 -08004389 /**
4390 * Requires carrier privileges or READ_PRECISE_PHONE_STATE permission.
4391 * @param subId The subscription to use to check the configuration.
4392 * @param c The callback that will be used to send the result.
4393 */
Brad Ebinger35c841c2018-10-01 10:40:55 -07004394 @Override
Brad Ebinger9878b0b2018-11-08 17:43:22 -08004395 public void unregisterMmTelCapabilityCallback(int subId, IImsCapabilityCallback c) {
Nathan Harold62c68512021-04-06 11:26:02 -07004396 TelephonyPermissions.enforceCallingOrSelfReadPrecisePhoneStatePermissionOrCarrierPrivilege(
Rambo Wang37f9c242020-02-10 14:45:28 -08004397 mApp, subId, "unregisterMmTelCapabilityCallback");
Brad Ebinger4ae57f92019-01-09 16:51:30 -08004398 if (!SubscriptionManager.isValidSubscriptionId(subId)) {
4399 throw new IllegalArgumentException("Invalid Subscription ID: " + subId);
4400 }
Meng Wangafbc5852019-09-19 17:37:13 -07004401
4402 final long token = Binder.clearCallingIdentity();
4403 try {
Meng Wangafbc5852019-09-19 17:37:13 -07004404 ImsManager.getInstance(mApp, getSlotIndexOrException(subId))
Brad Ebinger4ae57f92019-01-09 16:51:30 -08004405 .removeCapabilitiesCallbackForSubscription(c, subId);
Meng Wangafbc5852019-09-19 17:37:13 -07004406 } catch (ImsException e) {
4407 Log.i(LOG_TAG, "unregisterMmTelCapabilityCallback: " + subId
4408 + "is inactive, ignoring unregister.");
4409 // If the subscription is no longer active, just return, since the callback
4410 // will already have been removed internally.
4411 } finally {
4412 Binder.restoreCallingIdentity(token);
4413 }
Brad Ebinger35c841c2018-10-01 10:40:55 -07004414 }
4415
4416 @Override
Brad Ebinger9878b0b2018-11-08 17:43:22 -08004417 public boolean isCapable(int subId, int capability, int regTech) {
4418 enforceReadPrivilegedPermission("isCapable");
Brad Ebinger35c841c2018-10-01 10:40:55 -07004419 final long token = Binder.clearCallingIdentity();
4420 try {
Brad Ebingera2628302022-02-18 03:44:55 +00004421 int slotId = getSlotIndexOrException(subId);
4422 verifyImsMmTelConfiguredOrThrow(slotId);
4423 return ImsManager.getInstance(mApp, slotId).queryMmTelCapability(capability, regTech);
4424 } catch (com.android.ims.ImsException e) {
4425 Log.w(LOG_TAG, "IMS isCapable - service unavailable: " + e.getMessage());
4426 return false;
Brad Ebinger1ce9c432019-07-16 13:19:44 -07004427 } catch (ImsException e) {
Brad Ebinger6b5ac222019-02-04 14:36:52 -08004428 Log.i(LOG_TAG, "isCapable: " + subId + " is inactive, returning false.");
4429 return false;
Brad Ebinger35c841c2018-10-01 10:40:55 -07004430 } finally {
4431 Binder.restoreCallingIdentity(token);
4432 }
4433 }
4434
4435 @Override
Brad Ebinger9878b0b2018-11-08 17:43:22 -08004436 public boolean isAvailable(int subId, int capability, int regTech) {
4437 enforceReadPrivilegedPermission("isAvailable");
Brad Ebinger35c841c2018-10-01 10:40:55 -07004438 final long token = Binder.clearCallingIdentity();
4439 try {
4440 Phone phone = getPhone(subId);
4441 if (phone == null) return false;
4442 return phone.isImsCapabilityAvailable(capability, regTech);
Daniel Bright5e40e4e2020-03-11 16:35:39 -07004443 } catch (com.android.ims.ImsException e) {
4444 Log.w(LOG_TAG, "IMS isAvailable - service unavailable: " + e.getMessage());
4445 return false;
Brad Ebinger35c841c2018-10-01 10:40:55 -07004446 } finally {
4447 Binder.restoreCallingIdentity(token);
4448 }
4449 }
4450
Brad Ebingerbc7dd582019-10-17 17:03:22 -07004451 /**
4452 * Determines if the MmTel feature capability is supported by the carrier configuration for this
4453 * subscription.
4454 * @param subId The subscription to use to check the configuration.
4455 * @param callback The callback that will be used to send the result.
4456 * @param capability The MmTelFeature capability that will be used to send the result.
4457 * @param transportType The transport type of the MmTelFeature capability.
4458 */
4459 @Override
4460 public void isMmTelCapabilitySupported(int subId, IIntegerConsumer callback, int capability,
4461 int transportType) {
4462 enforceReadPrivilegedPermission("isMmTelCapabilitySupported");
Brad Ebingera2628302022-02-18 03:44:55 +00004463 if (!ImsManager.isImsSupportedOnDevice(mApp)) {
4464 throw new ServiceSpecificException(ImsException.CODE_ERROR_UNSUPPORTED_OPERATION,
4465 "IMS not available on device.");
4466 }
Brad Ebingerbc7dd582019-10-17 17:03:22 -07004467 final long token = Binder.clearCallingIdentity();
4468 try {
Brad Ebingera2628302022-02-18 03:44:55 +00004469 int slotId = getSlotIndex(subId);
4470 if (slotId <= SubscriptionManager.INVALID_SIM_SLOT_INDEX) {
4471 Log.w(LOG_TAG, "isMmTelCapabilitySupported: called with an inactive subscription '"
4472 + subId + "'");
4473 throw new ServiceSpecificException(ImsException.CODE_ERROR_INVALID_SUBSCRIPTION);
4474 }
4475 verifyImsMmTelConfiguredOrThrow(slotId);
4476 ImsManager.getInstance(mApp, slotId).isSupported(capability,
4477 transportType, aBoolean -> {
4478 try {
4479 callback.accept((aBoolean == null) ? 0 : (aBoolean ? 1 : 0));
4480 } catch (RemoteException e) {
4481 Log.w(LOG_TAG, "isMmTelCapabilitySupported: remote caller is not "
4482 + "running. Ignore");
4483 }
4484 });
Brad Ebinger919631e2021-06-02 17:46:35 -07004485 } catch (ImsException e) {
4486 throw new ServiceSpecificException(e.getCode());
Brad Ebingerbc7dd582019-10-17 17:03:22 -07004487 } finally {
4488 Binder.restoreCallingIdentity(token);
4489 }
4490 }
4491
shilu366312e2019-12-17 09:28:10 -08004492 /**
4493 * Requires carrier privileges or READ_PRECISE_PHONE_STATE permission.
4494 * @param subId The subscription to use to check the configuration.
4495 */
Brad Ebinger35c841c2018-10-01 10:40:55 -07004496 @Override
4497 public boolean isAdvancedCallingSettingEnabled(int subId) {
Nathan Harold62c68512021-04-06 11:26:02 -07004498 TelephonyPermissions.enforceCallingOrSelfReadPrecisePhoneStatePermissionOrCarrierPrivilege(
Rambo Wang37f9c242020-02-10 14:45:28 -08004499 mApp, subId, "isAdvancedCallingSettingEnabled");
shilu366312e2019-12-17 09:28:10 -08004500
Brad Ebinger35c841c2018-10-01 10:40:55 -07004501 final long token = Binder.clearCallingIdentity();
4502 try {
Brad Ebinger919631e2021-06-02 17:46:35 -07004503 int slotId = getSlotIndexOrException(subId);
Brad Ebinger735c5ce2021-07-12 13:58:21 -07004504 // This setting doesn't require an active ImsService connection, so do not verify.
Brad Ebinger919631e2021-06-02 17:46:35 -07004505 return ImsManager.getInstance(mApp, slotId).isEnhanced4gLteModeSettingEnabledByUser();
Brad Ebinger1ce9c432019-07-16 13:19:44 -07004506 } catch (ImsException e) {
4507 throw new ServiceSpecificException(e.getCode());
Brad Ebinger35c841c2018-10-01 10:40:55 -07004508 } finally {
4509 Binder.restoreCallingIdentity(token);
4510 }
4511 }
4512
4513 @Override
Brad Ebinger1c162042019-02-21 14:49:10 -08004514 public void setAdvancedCallingSettingEnabled(int subId, boolean isEnabled) {
Brad Ebinger35c841c2018-10-01 10:40:55 -07004515 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(mApp, subId,
Brad Ebinger1c162042019-02-21 14:49:10 -08004516 "setAdvancedCallingSettingEnabled");
Brad Ebinger35c841c2018-10-01 10:40:55 -07004517 final long identity = Binder.clearCallingIdentity();
4518 try {
Brad Ebinger919631e2021-06-02 17:46:35 -07004519 int slotId = getSlotIndexOrException(subId);
Brad Ebinger735c5ce2021-07-12 13:58:21 -07004520 // This setting doesn't require an active ImsService connection, so do not verify. The
4521 // new setting will be picked up when the ImsService comes up next if it isn't up.
Brad Ebinger919631e2021-06-02 17:46:35 -07004522 ImsManager.getInstance(mApp, slotId).setEnhanced4gLteModeSetting(isEnabled);
Brad Ebinger1ce9c432019-07-16 13:19:44 -07004523 } catch (ImsException e) {
4524 throw new ServiceSpecificException(e.getCode());
Brad Ebinger35c841c2018-10-01 10:40:55 -07004525 } finally {
4526 Binder.restoreCallingIdentity(identity);
4527 }
4528 }
4529
shilu366312e2019-12-17 09:28:10 -08004530 /**
4531 * Requires carrier privileges or READ_PRECISE_PHONE_STATE permission.
4532 * @param subId The subscription to use to check the configuration.
4533 */
Brad Ebinger35c841c2018-10-01 10:40:55 -07004534 @Override
Brad Ebinger9878b0b2018-11-08 17:43:22 -08004535 public boolean isVtSettingEnabled(int subId) {
Nathan Harold62c68512021-04-06 11:26:02 -07004536 TelephonyPermissions.enforceCallingOrSelfReadPrecisePhoneStatePermissionOrCarrierPrivilege(
Rambo Wang37f9c242020-02-10 14:45:28 -08004537 mApp, subId, "isVtSettingEnabled");
Brad Ebinger35c841c2018-10-01 10:40:55 -07004538 final long identity = Binder.clearCallingIdentity();
4539 try {
Brad Ebinger919631e2021-06-02 17:46:35 -07004540 int slotId = getSlotIndexOrException(subId);
Brad Ebinger735c5ce2021-07-12 13:58:21 -07004541 // This setting doesn't require an active ImsService connection, so do not verify.
Brad Ebinger919631e2021-06-02 17:46:35 -07004542 return ImsManager.getInstance(mApp, slotId).isVtEnabledByUser();
Brad Ebinger1ce9c432019-07-16 13:19:44 -07004543 } catch (ImsException e) {
4544 throw new ServiceSpecificException(e.getCode());
Brad Ebinger35c841c2018-10-01 10:40:55 -07004545 } finally {
4546 Binder.restoreCallingIdentity(identity);
4547 }
4548 }
4549
4550 @Override
Brad Ebinger1c162042019-02-21 14:49:10 -08004551 public void setVtSettingEnabled(int subId, boolean isEnabled) {
Brad Ebinger35c841c2018-10-01 10:40:55 -07004552 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(mApp, subId,
Brad Ebinger1c162042019-02-21 14:49:10 -08004553 "setVtSettingEnabled");
Brad Ebinger35c841c2018-10-01 10:40:55 -07004554 final long identity = Binder.clearCallingIdentity();
4555 try {
Brad Ebinger919631e2021-06-02 17:46:35 -07004556 int slotId = getSlotIndexOrException(subId);
Brad Ebinger735c5ce2021-07-12 13:58:21 -07004557 // This setting doesn't require an active ImsService connection, so do not verify. The
4558 // new setting will be picked up when the ImsService comes up next if it isn't up.
Brad Ebinger919631e2021-06-02 17:46:35 -07004559 ImsManager.getInstance(mApp, slotId).setVtSetting(isEnabled);
Brad Ebinger1ce9c432019-07-16 13:19:44 -07004560 } catch (ImsException e) {
4561 throw new ServiceSpecificException(e.getCode());
Brad Ebinger35c841c2018-10-01 10:40:55 -07004562 } finally {
4563 Binder.restoreCallingIdentity(identity);
4564 }
4565 }
4566
shilu366312e2019-12-17 09:28:10 -08004567 /**
4568 * Requires carrier privileges or READ_PRECISE_PHONE_STATE permission.
4569 * @param subId The subscription to use to check the configuration.
4570 */
Brad Ebinger35c841c2018-10-01 10:40:55 -07004571 @Override
4572 public boolean isVoWiFiSettingEnabled(int subId) {
Nathan Harold62c68512021-04-06 11:26:02 -07004573 TelephonyPermissions.enforceCallingOrSelfReadPrecisePhoneStatePermissionOrCarrierPrivilege(
Rambo Wang37f9c242020-02-10 14:45:28 -08004574 mApp, subId, "isVoWiFiSettingEnabled");
Brad Ebinger35c841c2018-10-01 10:40:55 -07004575 final long identity = Binder.clearCallingIdentity();
4576 try {
Brad Ebinger919631e2021-06-02 17:46:35 -07004577 int slotId = getSlotIndexOrException(subId);
Brad Ebinger735c5ce2021-07-12 13:58:21 -07004578 // This setting doesn't require an active ImsService connection, so do not verify.
Brad Ebinger919631e2021-06-02 17:46:35 -07004579 return ImsManager.getInstance(mApp, slotId).isWfcEnabledByUser();
Brad Ebinger1ce9c432019-07-16 13:19:44 -07004580 } catch (ImsException e) {
4581 throw new ServiceSpecificException(e.getCode());
Brad Ebinger35c841c2018-10-01 10:40:55 -07004582 } finally {
4583 Binder.restoreCallingIdentity(identity);
4584 }
4585 }
4586
4587 @Override
Brad Ebinger1c162042019-02-21 14:49:10 -08004588 public void setVoWiFiSettingEnabled(int subId, boolean isEnabled) {
Brad Ebinger35c841c2018-10-01 10:40:55 -07004589 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(mApp, subId,
Brad Ebinger1c162042019-02-21 14:49:10 -08004590 "setVoWiFiSettingEnabled");
Brad Ebinger35c841c2018-10-01 10:40:55 -07004591 final long identity = Binder.clearCallingIdentity();
4592 try {
Brad Ebinger919631e2021-06-02 17:46:35 -07004593 int slotId = getSlotIndexOrException(subId);
Brad Ebinger735c5ce2021-07-12 13:58:21 -07004594 // This setting doesn't require an active ImsService connection, so do not verify. The
4595 // new setting will be picked up when the ImsService comes up next if it isn't up.
Brad Ebinger919631e2021-06-02 17:46:35 -07004596 ImsManager.getInstance(mApp, slotId).setWfcSetting(isEnabled);
Brad Ebinger1ce9c432019-07-16 13:19:44 -07004597 } catch (ImsException e) {
4598 throw new ServiceSpecificException(e.getCode());
Brad Ebinger35c841c2018-10-01 10:40:55 -07004599 } finally {
4600 Binder.restoreCallingIdentity(identity);
4601 }
4602 }
4603
shilu366312e2019-12-17 09:28:10 -08004604 /**
Sooraj Sasindrane655add2020-11-23 19:40:38 -08004605 * @return true if the user's setting for Voice over Cross SIM is enabled and false if it is not
4606 * Requires carrier privileges or READ_PRECISE_PHONE_STATE permission.
4607 * @param subId The subscription to use to check the configuration.
4608 */
4609 @Override
4610 public boolean isCrossSimCallingEnabledByUser(int subId) {
Nathan Harold62c68512021-04-06 11:26:02 -07004611 TelephonyPermissions.enforceCallingOrSelfReadPrecisePhoneStatePermissionOrCarrierPrivilege(
Sooraj Sasindrane655add2020-11-23 19:40:38 -08004612 mApp, subId, "isCrossSimCallingEnabledByUser");
4613 final long identity = Binder.clearCallingIdentity();
4614 try {
Brad Ebinger919631e2021-06-02 17:46:35 -07004615 int slotId = getSlotIndexOrException(subId);
Brad Ebinger735c5ce2021-07-12 13:58:21 -07004616 // This setting doesn't require an active ImsService connection, so do not verify.
Brad Ebinger919631e2021-06-02 17:46:35 -07004617 return ImsManager.getInstance(mApp, slotId).isCrossSimCallingEnabledByUser();
Sooraj Sasindrane655add2020-11-23 19:40:38 -08004618 } catch (ImsException e) {
4619 throw new ServiceSpecificException(e.getCode());
4620 } finally {
4621 Binder.restoreCallingIdentity(identity);
4622 }
4623 }
4624
4625 /**
4626 * Sets the user's setting for whether or not Voice over Cross SIM is enabled.
4627 * Requires MODIFY_PHONE_STATE permission.
4628 * @param subId The subscription to use to check the configuration.
4629 * @param isEnabled true if the user's setting for Voice over Cross SIM is enabled,
4630 * false otherwise
4631 */
4632 @Override
4633 public void setCrossSimCallingEnabled(int subId, boolean isEnabled) {
4634 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(mApp, subId,
4635 "setCrossSimCallingEnabled");
4636 final long identity = Binder.clearCallingIdentity();
4637 try {
Brad Ebinger919631e2021-06-02 17:46:35 -07004638 int slotId = getSlotIndexOrException(subId);
Brad Ebinger735c5ce2021-07-12 13:58:21 -07004639 // This setting doesn't require an active ImsService connection, so do not verify. The
4640 // new setting will be picked up when the ImsService comes up next if it isn't up.
Brad Ebinger919631e2021-06-02 17:46:35 -07004641 ImsManager.getInstance(mApp, slotId).setCrossSimCallingEnabled(isEnabled);
Sooraj Sasindrane655add2020-11-23 19:40:38 -08004642 } catch (ImsException e) {
4643 throw new ServiceSpecificException(e.getCode());
4644 } finally {
4645 Binder.restoreCallingIdentity(identity);
4646 }
4647 }
4648
4649 /**
shilu366312e2019-12-17 09:28:10 -08004650 * Requires carrier privileges or READ_PRECISE_PHONE_STATE permission.
4651 * @param subId The subscription to use to check the configuration.
4652 */
Brad Ebinger35c841c2018-10-01 10:40:55 -07004653 @Override
Nathan Harold62c68512021-04-06 11:26:02 -07004654
Brad Ebinger35c841c2018-10-01 10:40:55 -07004655 public boolean isVoWiFiRoamingSettingEnabled(int subId) {
Nathan Harold62c68512021-04-06 11:26:02 -07004656 TelephonyPermissions.enforceCallingOrSelfReadPrecisePhoneStatePermissionOrCarrierPrivilege(
Rambo Wang37f9c242020-02-10 14:45:28 -08004657 mApp, subId, "isVoWiFiRoamingSettingEnabled");
Brad Ebinger35c841c2018-10-01 10:40:55 -07004658 final long identity = Binder.clearCallingIdentity();
4659 try {
Brad Ebinger919631e2021-06-02 17:46:35 -07004660 int slotId = getSlotIndexOrException(subId);
Brad Ebinger735c5ce2021-07-12 13:58:21 -07004661 // This setting doesn't require an active ImsService connection, so do not verify.
Brad Ebinger919631e2021-06-02 17:46:35 -07004662 return ImsManager.getInstance(mApp, slotId).isWfcRoamingEnabledByUser();
Brad Ebinger1ce9c432019-07-16 13:19:44 -07004663 } catch (ImsException e) {
4664 throw new ServiceSpecificException(e.getCode());
Brad Ebinger35c841c2018-10-01 10:40:55 -07004665 } finally {
4666 Binder.restoreCallingIdentity(identity);
4667 }
4668 }
4669
4670 @Override
Brad Ebinger1c162042019-02-21 14:49:10 -08004671 public void setVoWiFiRoamingSettingEnabled(int subId, boolean isEnabled) {
Brad Ebinger35c841c2018-10-01 10:40:55 -07004672 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(mApp, subId,
Brad Ebinger1c162042019-02-21 14:49:10 -08004673 "setVoWiFiRoamingSettingEnabled");
Brad Ebinger35c841c2018-10-01 10:40:55 -07004674 final long identity = Binder.clearCallingIdentity();
4675 try {
Brad Ebinger919631e2021-06-02 17:46:35 -07004676 int slotId = getSlotIndexOrException(subId);
Brad Ebinger735c5ce2021-07-12 13:58:21 -07004677 // This setting doesn't require an active ImsService connection, so do not verify. The
4678 // new setting will be picked up when the ImsService comes up next if it isn't up.
Brad Ebinger919631e2021-06-02 17:46:35 -07004679 ImsManager.getInstance(mApp, slotId).setWfcRoamingSetting(isEnabled);
Brad Ebinger1ce9c432019-07-16 13:19:44 -07004680 } catch (ImsException e) {
4681 throw new ServiceSpecificException(e.getCode());
Brad Ebinger35c841c2018-10-01 10:40:55 -07004682 } finally {
4683 Binder.restoreCallingIdentity(identity);
4684 }
4685 }
4686
4687 @Override
4688 public void setVoWiFiNonPersistent(int subId, boolean isCapable, int mode) {
4689 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(mApp, subId,
4690 "setVoWiFiNonPersistent");
4691 final long identity = Binder.clearCallingIdentity();
4692 try {
Brad Ebinger919631e2021-06-02 17:46:35 -07004693 int slotId = getSlotIndexOrException(subId);
Brad Ebinger735c5ce2021-07-12 13:58:21 -07004694 // This setting will be ignored if the ImsService isn't up.
Brad Ebinger919631e2021-06-02 17:46:35 -07004695 ImsManager.getInstance(mApp, slotId).setWfcNonPersistent(isCapable, mode);
Brad Ebinger1ce9c432019-07-16 13:19:44 -07004696 } catch (ImsException e) {
4697 throw new ServiceSpecificException(e.getCode());
Brad Ebinger35c841c2018-10-01 10:40:55 -07004698 } finally {
4699 Binder.restoreCallingIdentity(identity);
4700 }
4701 }
4702
shilu366312e2019-12-17 09:28:10 -08004703 /**
4704 * Requires carrier privileges or READ_PRECISE_PHONE_STATE permission.
4705 * @param subId The subscription to use to check the configuration.
4706 */
Brad Ebinger35c841c2018-10-01 10:40:55 -07004707 @Override
4708 public int getVoWiFiModeSetting(int subId) {
Nathan Harold62c68512021-04-06 11:26:02 -07004709 TelephonyPermissions.enforceCallingOrSelfReadPrecisePhoneStatePermissionOrCarrierPrivilege(
Rambo Wang37f9c242020-02-10 14:45:28 -08004710 mApp, subId, "getVoWiFiModeSetting");
Brad Ebinger35c841c2018-10-01 10:40:55 -07004711 final long identity = Binder.clearCallingIdentity();
4712 try {
Brad Ebinger919631e2021-06-02 17:46:35 -07004713 int slotId = getSlotIndexOrException(subId);
Brad Ebinger735c5ce2021-07-12 13:58:21 -07004714 // This setting doesn't require an active ImsService connection, so do not verify.
Brad Ebinger919631e2021-06-02 17:46:35 -07004715 return ImsManager.getInstance(mApp, slotId).getWfcMode(false /*isRoaming*/);
Brad Ebinger1ce9c432019-07-16 13:19:44 -07004716 } catch (ImsException e) {
4717 throw new ServiceSpecificException(e.getCode());
Brad Ebinger35c841c2018-10-01 10:40:55 -07004718 } finally {
4719 Binder.restoreCallingIdentity(identity);
4720 }
4721 }
4722
4723 @Override
4724 public void setVoWiFiModeSetting(int subId, int mode) {
4725 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(mApp, subId,
4726 "setVoWiFiModeSetting");
4727 final long identity = Binder.clearCallingIdentity();
4728 try {
Brad Ebinger919631e2021-06-02 17:46:35 -07004729 int slotId = getSlotIndexOrException(subId);
Brad Ebinger735c5ce2021-07-12 13:58:21 -07004730 // This setting doesn't require an active ImsService connection, so do not verify. The
4731 // new setting will be picked up when the ImsService comes up next if it isn't up.
Brad Ebinger919631e2021-06-02 17:46:35 -07004732 ImsManager.getInstance(mApp, slotId).setWfcMode(mode, false /*isRoaming*/);
Brad Ebinger1ce9c432019-07-16 13:19:44 -07004733 } catch (ImsException e) {
4734 throw new ServiceSpecificException(e.getCode());
Brad Ebinger35c841c2018-10-01 10:40:55 -07004735 } finally {
4736 Binder.restoreCallingIdentity(identity);
4737 }
4738 }
4739
4740 @Override
4741 public int getVoWiFiRoamingModeSetting(int subId) {
4742 enforceReadPrivilegedPermission("getVoWiFiRoamingModeSetting");
4743 final long identity = Binder.clearCallingIdentity();
4744 try {
Brad Ebinger919631e2021-06-02 17:46:35 -07004745 int slotId = getSlotIndexOrException(subId);
Brad Ebinger735c5ce2021-07-12 13:58:21 -07004746 // This setting doesn't require an active ImsService connection, so do not verify.
Brad Ebinger919631e2021-06-02 17:46:35 -07004747 return ImsManager.getInstance(mApp, slotId).getWfcMode(true /*isRoaming*/);
Brad Ebinger1ce9c432019-07-16 13:19:44 -07004748 } catch (ImsException e) {
4749 throw new ServiceSpecificException(e.getCode());
Brad Ebinger35c841c2018-10-01 10:40:55 -07004750 } finally {
4751 Binder.restoreCallingIdentity(identity);
4752 }
4753 }
4754
4755 @Override
4756 public void setVoWiFiRoamingModeSetting(int subId, int mode) {
4757 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(mApp, subId,
4758 "setVoWiFiRoamingModeSetting");
4759 final long identity = Binder.clearCallingIdentity();
4760 try {
Brad Ebinger919631e2021-06-02 17:46:35 -07004761 int slotId = getSlotIndexOrException(subId);
Brad Ebinger735c5ce2021-07-12 13:58:21 -07004762 // This setting doesn't require an active ImsService connection, so do not verify. The
4763 // new setting will be picked up when the ImsService comes up next if it isn't up.
Brad Ebinger919631e2021-06-02 17:46:35 -07004764 ImsManager.getInstance(mApp, slotId).setWfcMode(mode, true /*isRoaming*/);
Brad Ebinger1ce9c432019-07-16 13:19:44 -07004765 } catch (ImsException e) {
4766 throw new ServiceSpecificException(e.getCode());
Brad Ebinger35c841c2018-10-01 10:40:55 -07004767 } finally {
4768 Binder.restoreCallingIdentity(identity);
4769 }
4770 }
4771
4772 @Override
4773 public void setRttCapabilitySetting(int subId, boolean isEnabled) {
4774 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(mApp, subId,
4775 "setRttCapabilityEnabled");
4776 final long identity = Binder.clearCallingIdentity();
4777 try {
Brad Ebinger919631e2021-06-02 17:46:35 -07004778 int slotId = getSlotIndexOrException(subId);
Brad Ebinger735c5ce2021-07-12 13:58:21 -07004779 // This setting doesn't require an active ImsService connection, so do not verify. The
4780 // new setting will be picked up when the ImsService comes up next if it isn't up.
Brad Ebinger919631e2021-06-02 17:46:35 -07004781 ImsManager.getInstance(mApp, slotId).setRttEnabled(isEnabled);
Brad Ebinger1ce9c432019-07-16 13:19:44 -07004782 } catch (ImsException e) {
4783 throw new ServiceSpecificException(e.getCode());
Brad Ebinger35c841c2018-10-01 10:40:55 -07004784 } finally {
4785 Binder.restoreCallingIdentity(identity);
4786 }
4787 }
4788
shilu366312e2019-12-17 09:28:10 -08004789 /**
4790 * Requires carrier privileges or READ_PRECISE_PHONE_STATE permission.
4791 * @param subId The subscription to use to check the configuration.
4792 */
Brad Ebinger35c841c2018-10-01 10:40:55 -07004793 @Override
4794 public boolean isTtyOverVolteEnabled(int subId) {
Nathan Harold62c68512021-04-06 11:26:02 -07004795 TelephonyPermissions.enforceCallingOrSelfReadPrecisePhoneStatePermissionOrCarrierPrivilege(
Rambo Wang37f9c242020-02-10 14:45:28 -08004796 mApp, subId, "isTtyOverVolteEnabled");
Brad Ebinger35c841c2018-10-01 10:40:55 -07004797 final long identity = Binder.clearCallingIdentity();
4798 try {
Brad Ebinger919631e2021-06-02 17:46:35 -07004799 int slotId = getSlotIndexOrException(subId);
Brad Ebinger735c5ce2021-07-12 13:58:21 -07004800 // This setting doesn't require an active ImsService connection, so do not verify.
Brad Ebinger919631e2021-06-02 17:46:35 -07004801 return ImsManager.getInstance(mApp, slotId).isTtyOnVoLteCapable();
Brad Ebinger1ce9c432019-07-16 13:19:44 -07004802 } catch (ImsException e) {
4803 throw new ServiceSpecificException(e.getCode());
Brad Ebinger35c841c2018-10-01 10:40:55 -07004804 } finally {
4805 Binder.restoreCallingIdentity(identity);
4806 }
4807 }
4808
Brad Ebingerdf5b4f02018-10-31 11:24:17 -07004809 @Override
4810 public void registerImsProvisioningChangedCallback(int subId, IImsConfigCallback callback) {
4811 enforceReadPrivilegedPermission("registerImsProvisioningChangedCallback");
joonhunshincffb7fc2021-11-28 07:32:01 +00004812
Brad Ebingerdf5b4f02018-10-31 11:24:17 -07004813 final long identity = Binder.clearCallingIdentity();
4814 try {
Brad Ebingera2628302022-02-18 03:44:55 +00004815 if (!isImsAvailableOnDevice()) {
4816 throw new ServiceSpecificException(ImsException.CODE_ERROR_UNSUPPORTED_OPERATION,
4817 "IMS not available on device.");
4818 }
4819 int slotId = getSlotIndexOrException(subId);
4820 verifyImsMmTelConfiguredOrThrow(slotId);
4821 ImsManager.getInstance(mApp, slotId)
4822 .addProvisioningCallbackForSubscription(callback, subId);
Brad Ebinger1ce9c432019-07-16 13:19:44 -07004823 } catch (ImsException e) {
4824 throw new ServiceSpecificException(e.getCode());
Brad Ebingerdf5b4f02018-10-31 11:24:17 -07004825 } finally {
4826 Binder.restoreCallingIdentity(identity);
4827 }
4828 }
4829
4830 @Override
4831 public void unregisterImsProvisioningChangedCallback(int subId, IImsConfigCallback callback) {
4832 enforceReadPrivilegedPermission("unregisterImsProvisioningChangedCallback");
joonhunshincffb7fc2021-11-28 07:32:01 +00004833
Brad Ebingerdf5b4f02018-10-31 11:24:17 -07004834 final long identity = Binder.clearCallingIdentity();
Brad Ebinger4ae57f92019-01-09 16:51:30 -08004835 if (!SubscriptionManager.isValidSubscriptionId(subId)) {
4836 throw new IllegalArgumentException("Invalid Subscription ID: " + subId);
4837 }
Brad Ebingerdf5b4f02018-10-31 11:24:17 -07004838 try {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08004839 ImsManager.getInstance(mApp, getSlotIndexOrException(subId))
Brad Ebinger4ae57f92019-01-09 16:51:30 -08004840 .removeProvisioningCallbackForSubscription(callback, subId);
Brad Ebinger1ce9c432019-07-16 13:19:44 -07004841 } catch (ImsException e) {
Brad Ebinger4ae57f92019-01-09 16:51:30 -08004842 Log.i(LOG_TAG, "unregisterImsProvisioningChangedCallback: " + subId
4843 + "is inactive, ignoring unregister.");
4844 // If the subscription is no longer active, just return, since the callback will already
4845 // have been removed internally.
Brad Ebingerdf5b4f02018-10-31 11:24:17 -07004846 } finally {
4847 Binder.restoreCallingIdentity(identity);
4848 }
4849 }
4850
joonhunshincffb7fc2021-11-28 07:32:01 +00004851 @Override
4852 public void registerFeatureProvisioningChangedCallback(int subId,
4853 IFeatureProvisioningCallback callback) {
4854 TelephonyPermissions.enforceCallingOrSelfReadPrecisePhoneStatePermissionOrCarrierPrivilege(
4855 mApp, subId, "registerFeatureProvisioningChangedCallback");
4856
4857 final long identity = Binder.clearCallingIdentity();
4858 if (!SubscriptionManager.isValidSubscriptionId(subId)) {
4859 throw new IllegalArgumentException("Invalid Subscription ID: " + subId);
4860 }
4861
joonhunshin91bc1952022-04-29 08:47:15 +00004862 try {
4863 ImsProvisioningController controller = ImsProvisioningController.getInstance();
4864 if (controller == null) {
4865 throw new ServiceSpecificException(ImsException.CODE_ERROR_UNSUPPORTED_OPERATION,
4866 "Device does not support IMS");
4867 }
4868 controller.addFeatureProvisioningChangedCallback(subId, callback);
4869 } finally {
4870 Binder.restoreCallingIdentity(identity);
4871 }
joonhunshincffb7fc2021-11-28 07:32:01 +00004872 }
4873
4874 @Override
4875 public void unregisterFeatureProvisioningChangedCallback(int subId,
4876 IFeatureProvisioningCallback callback) {
4877 TelephonyPermissions.enforceCallingOrSelfReadPrecisePhoneStatePermissionOrCarrierPrivilege(
4878 mApp, subId, "unregisterFeatureProvisioningChangedCallback");
4879
4880 final long identity = Binder.clearCallingIdentity();
4881 if (!SubscriptionManager.isValidSubscriptionId(subId)) {
4882 throw new IllegalArgumentException("Invalid Subscription ID: " + subId);
4883 }
4884
joonhunshin91bc1952022-04-29 08:47:15 +00004885 try {
4886 ImsProvisioningController controller = ImsProvisioningController.getInstance();
4887 if (controller == null) {
4888 loge("unregisterFeatureProvisioningChangedCallback: Device does not support IMS");
4889 return;
4890 }
4891 controller.removeFeatureProvisioningChangedCallback(subId, callback);
4892 } finally {
4893 Binder.restoreCallingIdentity(identity);
4894 }
joonhunshincffb7fc2021-11-28 07:32:01 +00004895 }
allenwtsu99c623b2020-01-03 18:24:23 +08004896
4897 private void checkModifyPhoneStatePermission(int subId, String message) {
4898 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(mApp, subId,
4899 message);
4900 }
4901
allenwtsu99c623b2020-01-03 18:24:23 +08004902 @Override
joonhunshincffb7fc2021-11-28 07:32:01 +00004903 public void setRcsProvisioningStatusForCapability(int subId, int capability, int tech,
allenwtsu99c623b2020-01-03 18:24:23 +08004904 boolean isProvisioned) {
4905 checkModifyPhoneStatePermission(subId, "setRcsProvisioningStatusForCapability");
4906
4907 final long identity = Binder.clearCallingIdentity();
4908 try {
joonhunshin91bc1952022-04-29 08:47:15 +00004909 ImsProvisioningController controller = ImsProvisioningController.getInstance();
4910 if (controller == null) {
4911 loge("setRcsProvisioningStatusForCapability: Device does not support IMS");
4912 return;
4913 }
4914 controller.setRcsProvisioningStatusForCapability(
4915 subId, capability, tech, isProvisioned);
allenwtsu99c623b2020-01-03 18:24:23 +08004916 } finally {
4917 Binder.restoreCallingIdentity(identity);
4918 }
allenwtsu99c623b2020-01-03 18:24:23 +08004919 }
4920
4921
4922 @Override
joonhunshincffb7fc2021-11-28 07:32:01 +00004923 public boolean getRcsProvisioningStatusForCapability(int subId, int capability, int tech) {
4924 TelephonyPermissions.enforceCallingOrSelfReadPrecisePhoneStatePermissionOrCarrierPrivilege(
4925 mApp, subId, "getRcsProvisioningStatusForCapability");
4926
allenwtsu99c623b2020-01-03 18:24:23 +08004927 final long identity = Binder.clearCallingIdentity();
4928 try {
joonhunshin91bc1952022-04-29 08:47:15 +00004929 ImsProvisioningController controller = ImsProvisioningController.getInstance();
4930 if (controller == null) {
4931 loge("getRcsProvisioningStatusForCapability: Device does not support IMS");
4932
4933 // device does not support IMS, this method will return true always.
4934 return true;
4935 }
4936 return controller.getRcsProvisioningStatusForCapability(subId, capability, tech);
allenwtsu99c623b2020-01-03 18:24:23 +08004937 } finally {
4938 Binder.restoreCallingIdentity(identity);
4939 }
4940 }
4941
Brad Ebingerdf5b4f02018-10-31 11:24:17 -07004942 @Override
Brad Ebinger1c8542e2019-01-14 13:43:14 -08004943 public void setImsProvisioningStatusForCapability(int subId, int capability, int tech,
4944 boolean isProvisioned) {
allenwtsu99c623b2020-01-03 18:24:23 +08004945 checkModifyPhoneStatePermission(subId, "setImsProvisioningStatusForCapability");
joonhunshincffb7fc2021-11-28 07:32:01 +00004946
Brad Ebinger1c8542e2019-01-14 13:43:14 -08004947 final long identity = Binder.clearCallingIdentity();
4948 try {
joonhunshin91bc1952022-04-29 08:47:15 +00004949 ImsProvisioningController controller = ImsProvisioningController.getInstance();
4950 if (controller == null) {
4951 loge("setImsProvisioningStatusForCapability: Device does not support IMS");
4952 return;
4953 }
4954 controller.setImsProvisioningStatusForCapability(
4955 subId, capability, tech, isProvisioned);
Brad Ebinger1c8542e2019-01-14 13:43:14 -08004956 } finally {
4957 Binder.restoreCallingIdentity(identity);
4958 }
4959 }
4960
4961 @Override
4962 public boolean getImsProvisioningStatusForCapability(int subId, int capability, int tech) {
joonhunshincffb7fc2021-11-28 07:32:01 +00004963 TelephonyPermissions.enforceCallingOrSelfReadPrecisePhoneStatePermissionOrCarrierPrivilege(
4964 mApp, subId, "getProvisioningStatusForCapability");
4965
Brad Ebinger1c8542e2019-01-14 13:43:14 -08004966 final long identity = Binder.clearCallingIdentity();
4967 try {
joonhunshin91bc1952022-04-29 08:47:15 +00004968 ImsProvisioningController controller = ImsProvisioningController.getInstance();
4969 if (controller == null) {
4970 loge("getImsProvisioningStatusForCapability: Device does not support IMS");
Brad Ebinger1c8542e2019-01-14 13:43:14 -08004971
joonhunshin91bc1952022-04-29 08:47:15 +00004972 // device does not support IMS, this method will return true always.
4973 return true;
4974 }
4975 return controller.getImsProvisioningStatusForCapability(subId, capability, tech);
Brad Ebinger1c8542e2019-01-14 13:43:14 -08004976 } finally {
4977 Binder.restoreCallingIdentity(identity);
4978 }
4979 }
4980
4981 @Override
joonhunshincffb7fc2021-11-28 07:32:01 +00004982 public boolean isProvisioningRequiredForCapability(int subId, int capability, int tech) {
4983 TelephonyPermissions.enforceCallingOrSelfReadPrecisePhoneStatePermissionOrCarrierPrivilege(
4984 mApp, subId, "isProvisioningRequiredForCapability");
4985
4986 final long identity = Binder.clearCallingIdentity();
4987 try {
joonhunshin91bc1952022-04-29 08:47:15 +00004988 ImsProvisioningController controller = ImsProvisioningController.getInstance();
4989 if (controller == null) {
4990 loge("isProvisioningRequiredForCapability: Device does not support IMS");
4991
4992 // device does not support IMS, this method will return false
4993 return false;
4994 }
4995 return controller.isImsProvisioningRequiredForCapability(subId, capability, tech);
joonhunshincffb7fc2021-11-28 07:32:01 +00004996 } finally {
4997 Binder.restoreCallingIdentity(identity);
Brad Ebinger1c8542e2019-01-14 13:43:14 -08004998 }
Brad Ebinger1c8542e2019-01-14 13:43:14 -08004999 }
5000
5001 @Override
joonhunshincffb7fc2021-11-28 07:32:01 +00005002 public boolean isRcsProvisioningRequiredForCapability(int subId, int capability, int tech) {
5003 TelephonyPermissions.enforceCallingOrSelfReadPrecisePhoneStatePermissionOrCarrierPrivilege(
5004 mApp, subId, "isProvisioningRequiredForCapability");
Brad Ebinger1c8542e2019-01-14 13:43:14 -08005005
joonhunshincffb7fc2021-11-28 07:32:01 +00005006 final long identity = Binder.clearCallingIdentity();
5007 try {
joonhunshin91bc1952022-04-29 08:47:15 +00005008 ImsProvisioningController controller = ImsProvisioningController.getInstance();
5009 if (controller == null) {
5010 loge("isRcsProvisioningRequiredForCapability: Device does not support IMS");
5011
5012 // device does not support IMS, this method will return false
5013 return false;
5014 }
5015 return controller.isRcsProvisioningRequiredForCapability(subId, capability, tech);
joonhunshincffb7fc2021-11-28 07:32:01 +00005016 } finally {
5017 Binder.restoreCallingIdentity(identity);
5018 }
Brad Ebinger1c8542e2019-01-14 13:43:14 -08005019 }
5020
Brad Ebinger1c8542e2019-01-14 13:43:14 -08005021 @Override
Brad Ebingerdf5b4f02018-10-31 11:24:17 -07005022 public int getImsProvisioningInt(int subId, int key) {
Brad Ebinger1c8542e2019-01-14 13:43:14 -08005023 if (!SubscriptionManager.isValidSubscriptionId(subId)) {
5024 throw new IllegalArgumentException("Invalid Subscription id '" + subId + "'");
5025 }
joonhunshincffb7fc2021-11-28 07:32:01 +00005026 TelephonyPermissions.enforceCallingOrSelfReadPrecisePhoneStatePermissionOrCarrierPrivilege(
5027 mApp, subId, "getImsProvisioningInt");
5028
Brad Ebingerdf5b4f02018-10-31 11:24:17 -07005029 final long identity = Binder.clearCallingIdentity();
5030 try {
5031 // TODO: Refactor to remove ImsManager dependence and query through ImsPhone directly.
Brad Ebinger1c8542e2019-01-14 13:43:14 -08005032 int slotId = getSlotIndex(subId);
5033 if (slotId <= SubscriptionManager.INVALID_SIM_SLOT_INDEX) {
5034 Log.w(LOG_TAG, "getImsProvisioningInt: called with an inactive subscription '"
5035 + subId + "' for key:" + key);
5036 return ImsConfigImplBase.CONFIG_RESULT_UNKNOWN;
5037 }
joonhunshincffb7fc2021-11-28 07:32:01 +00005038
joonhunshin91bc1952022-04-29 08:47:15 +00005039 ImsProvisioningController controller = ImsProvisioningController.getInstance();
5040 if (controller == null) {
5041 loge("getImsProvisioningInt: Device does not support IMS");
5042
5043 // device does not support IMS, this method will return CONFIG_RESULT_UNKNOWN.
5044 return ImsConfigImplBase.CONFIG_RESULT_UNKNOWN;
5045 }
5046 int retVal = controller.getProvisioningValue(subId, key);
joonhunshincffb7fc2021-11-28 07:32:01 +00005047 if (retVal != ImsConfigImplBase.CONFIG_RESULT_UNKNOWN) {
5048 return retVal;
5049 }
5050
calvinpanb5a34062021-02-08 19:59:36 +08005051 return ImsManager.getInstance(mApp, slotId).getConfigInt(key);
Brad Ebinger1ce9c432019-07-16 13:19:44 -07005052 } catch (com.android.ims.ImsException e) {
Brad Ebinger1c8542e2019-01-14 13:43:14 -08005053 Log.w(LOG_TAG, "getImsProvisioningInt: ImsService is not available for subscription '"
5054 + subId + "' for key:" + key);
5055 return ImsConfigImplBase.CONFIG_RESULT_UNKNOWN;
Brad Ebingerdf5b4f02018-10-31 11:24:17 -07005056 } finally {
5057 Binder.restoreCallingIdentity(identity);
5058 }
5059 }
5060
5061 @Override
5062 public String getImsProvisioningString(int subId, int key) {
Brad Ebinger1c8542e2019-01-14 13:43:14 -08005063 if (!SubscriptionManager.isValidSubscriptionId(subId)) {
5064 throw new IllegalArgumentException("Invalid Subscription id '" + subId + "'");
5065 }
joonhunshincffb7fc2021-11-28 07:32:01 +00005066 TelephonyPermissions.enforceCallingOrSelfReadPrecisePhoneStatePermissionOrCarrierPrivilege(
5067 mApp, subId, "getImsProvisioningString");
5068
Brad Ebingerdf5b4f02018-10-31 11:24:17 -07005069 final long identity = Binder.clearCallingIdentity();
5070 try {
5071 // TODO: Refactor to remove ImsManager dependence and query through ImsPhone directly.
Brad Ebinger1c8542e2019-01-14 13:43:14 -08005072 int slotId = getSlotIndex(subId);
5073 if (slotId <= SubscriptionManager.INVALID_SIM_SLOT_INDEX) {
5074 Log.w(LOG_TAG, "getImsProvisioningString: called for an inactive subscription id '"
5075 + subId + "' for key:" + key);
5076 return ProvisioningManager.STRING_QUERY_RESULT_ERROR_GENERIC;
5077 }
calvinpanb5a34062021-02-08 19:59:36 +08005078 return ImsManager.getInstance(mApp, slotId).getConfigString(key);
Brad Ebinger1ce9c432019-07-16 13:19:44 -07005079 } catch (com.android.ims.ImsException e) {
Brad Ebinger1c8542e2019-01-14 13:43:14 -08005080 Log.w(LOG_TAG, "getImsProvisioningString: ImsService is not available for sub '"
5081 + subId + "' for key:" + key);
5082 return ProvisioningManager.STRING_QUERY_RESULT_ERROR_NOT_READY;
Brad Ebingerdf5b4f02018-10-31 11:24:17 -07005083 } finally {
5084 Binder.restoreCallingIdentity(identity);
5085 }
5086 }
5087
5088 @Override
5089 public int setImsProvisioningInt(int subId, int key, int value) {
Brad Ebinger1c8542e2019-01-14 13:43:14 -08005090 if (!SubscriptionManager.isValidSubscriptionId(subId)) {
5091 throw new IllegalArgumentException("Invalid Subscription id '" + subId + "'");
5092 }
Brad Ebinger3d0b34e2018-11-15 14:13:12 -08005093 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(mApp, subId,
5094 "setImsProvisioningInt");
joonhunshincffb7fc2021-11-28 07:32:01 +00005095
Brad Ebingerdf5b4f02018-10-31 11:24:17 -07005096 final long identity = Binder.clearCallingIdentity();
5097 try {
5098 // TODO: Refactor to remove ImsManager dependence and query through ImsPhone directly.
Brad Ebinger1c8542e2019-01-14 13:43:14 -08005099 int slotId = getSlotIndex(subId);
5100 if (slotId <= SubscriptionManager.INVALID_SIM_SLOT_INDEX) {
5101 Log.w(LOG_TAG, "setImsProvisioningInt: called with an inactive subscription id '"
5102 + subId + "' for key:" + key);
5103 return ImsConfigImplBase.CONFIG_RESULT_FAILED;
5104 }
joonhunshincffb7fc2021-11-28 07:32:01 +00005105
joonhunshin91bc1952022-04-29 08:47:15 +00005106 ImsProvisioningController controller = ImsProvisioningController.getInstance();
5107 if (controller == null) {
5108 loge("setImsProvisioningInt: Device does not support IMS");
5109
5110 // device does not support IMS, this method will return CONFIG_RESULT_FAILED.
5111 return ImsConfigImplBase.CONFIG_RESULT_FAILED;
5112 }
5113 int retVal = controller.setProvisioningValue(subId, key, value);
joonhunshincffb7fc2021-11-28 07:32:01 +00005114 if (retVal != ImsConfigImplBase.CONFIG_RESULT_UNKNOWN) {
5115 return retVal;
5116 }
5117
calvinpanb5a34062021-02-08 19:59:36 +08005118 return ImsManager.getInstance(mApp, slotId).setConfig(key, value);
5119 } catch (com.android.ims.ImsException | RemoteException e) {
Brad Ebinger1c8542e2019-01-14 13:43:14 -08005120 Log.w(LOG_TAG, "setImsProvisioningInt: ImsService unavailable for sub '" + subId
calvinpanb5a34062021-02-08 19:59:36 +08005121 + "' for key:" + key, e);
Brad Ebinger1c8542e2019-01-14 13:43:14 -08005122 return ImsConfigImplBase.CONFIG_RESULT_FAILED;
Brad Ebingerdf5b4f02018-10-31 11:24:17 -07005123 } finally {
5124 Binder.restoreCallingIdentity(identity);
5125 }
5126 }
5127
5128 @Override
5129 public int setImsProvisioningString(int subId, int key, String value) {
Brad Ebinger1c8542e2019-01-14 13:43:14 -08005130 if (!SubscriptionManager.isValidSubscriptionId(subId)) {
5131 throw new IllegalArgumentException("Invalid Subscription id '" + subId + "'");
5132 }
Brad Ebinger3d0b34e2018-11-15 14:13:12 -08005133 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(mApp, subId,
5134 "setImsProvisioningString");
Brad Ebingerdf5b4f02018-10-31 11:24:17 -07005135 final long identity = Binder.clearCallingIdentity();
5136 try {
5137 // TODO: Refactor to remove ImsManager dependence and query through ImsPhone directly.
Brad Ebinger1c8542e2019-01-14 13:43:14 -08005138 int slotId = getSlotIndex(subId);
5139 if (slotId <= SubscriptionManager.INVALID_SIM_SLOT_INDEX) {
5140 Log.w(LOG_TAG, "setImsProvisioningString: called with an inactive subscription id '"
5141 + subId + "' for key:" + key);
5142 return ImsConfigImplBase.CONFIG_RESULT_FAILED;
5143 }
calvinpanb5a34062021-02-08 19:59:36 +08005144 return ImsManager.getInstance(mApp, slotId).setConfig(key, value);
5145 } catch (com.android.ims.ImsException | RemoteException e) {
Brad Ebinger1c8542e2019-01-14 13:43:14 -08005146 Log.w(LOG_TAG, "setImsProvisioningString: ImsService unavailable for sub '" + subId
calvinpanb5a34062021-02-08 19:59:36 +08005147 + "' for key:" + key, e);
Brad Ebinger1c8542e2019-01-14 13:43:14 -08005148 return ImsConfigImplBase.CONFIG_RESULT_FAILED;
Brad Ebingerdf5b4f02018-10-31 11:24:17 -07005149 } finally {
5150 Binder.restoreCallingIdentity(identity);
5151 }
5152 }
5153
Brad Ebinger919631e2021-06-02 17:46:35 -07005154 /**
5155 * Throw an ImsException if the IMS resolver does not have an ImsService configured for MMTEL
5156 * for the given slot ID or no ImsResolver instance has been created.
5157 * @param slotId The slot ID that the IMS service is created for.
5158 * @throws ImsException If there is no ImsService configured for this slot.
5159 */
5160 private void verifyImsMmTelConfiguredOrThrow(int slotId) throws ImsException {
5161 if (mImsResolver == null || !mImsResolver.isImsServiceConfiguredForFeature(slotId,
5162 ImsFeature.FEATURE_MMTEL)) {
5163 throw new ImsException("This subscription does not support MMTEL over IMS",
5164 ImsException.CODE_ERROR_UNSUPPORTED_OPERATION);
5165 }
5166 }
5167
Brad Ebinger1ce9c432019-07-16 13:19:44 -07005168 private int getSlotIndexOrException(int subId) throws ImsException {
Brad Ebinger35c841c2018-10-01 10:40:55 -07005169 int slotId = SubscriptionManager.getSlotIndex(subId);
5170 if (!SubscriptionManager.isValidSlotIndex(slotId)) {
Brad Ebinger1ce9c432019-07-16 13:19:44 -07005171 throw new ImsException("Invalid Subscription Id, subId=" + subId,
5172 ImsException.CODE_ERROR_INVALID_SUBSCRIPTION);
Brad Ebinger35c841c2018-10-01 10:40:55 -07005173 }
5174 return slotId;
5175 }
5176
Brad Ebinger1c8542e2019-01-14 13:43:14 -08005177 private int getSlotIndex(int subId) {
5178 int slotId = SubscriptionManager.getSlotIndex(subId);
5179 if (!SubscriptionManager.isValidSlotIndex(slotId)) {
5180 return SubscriptionManager.INVALID_SIM_SLOT_INDEX;
5181 }
5182 return slotId;
5183 }
5184
Wink Saville36469e72014-06-11 15:17:00 -07005185 /**
Nathan Harold9042f0b2019-05-21 15:51:27 -07005186 * Returns the data network type for a subId; does not throw SecurityException.
Wink Saville36469e72014-06-11 15:17:00 -07005187 */
5188 @Override
Philip P. Moltmann700a9592019-10-03 11:53:50 -07005189 public int getNetworkTypeForSubscriber(int subId, String callingPackage,
5190 String callingFeatureId) {
Shuo Qian13d89152021-05-10 23:58:11 -07005191 try {
5192 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
5193 } catch (SecurityException se) {
5194 EventLog.writeEvent(0x534e4554, "186776740", Binder.getCallingUid());
5195 throw new SecurityException("Package " + callingPackage + " does not belong to "
5196 + Binder.getCallingUid());
5197 }
Nathan Haroldf096d982020-11-18 17:18:06 -08005198 final int targetSdk = TelephonyPermissions.getTargetSdk(mApp, callingPackage);
Nathan Haroldef60dba2019-05-22 13:55:14 -07005199 if (targetSdk > android.os.Build.VERSION_CODES.Q) {
Philip P. Moltmann700a9592019-10-03 11:53:50 -07005200 return getDataNetworkTypeForSubscriber(subId, callingPackage, callingFeatureId);
Nathan Haroldef60dba2019-05-22 13:55:14 -07005201 } else if (targetSdk == android.os.Build.VERSION_CODES.Q
Nathan Harold9042f0b2019-05-21 15:51:27 -07005202 && !TelephonyPermissions.checkCallingOrSelfReadPhoneStateNoThrow(
Philip P. Moltmann700a9592019-10-03 11:53:50 -07005203 mApp, subId, callingPackage, callingFeatureId,
5204 "getNetworkTypeForSubscriber")) {
Robert Greenwalt36b23af2015-07-06 17:59:14 -07005205 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
5206 }
Robert Greenwalta5dcfcb2015-07-10 09:06:29 -07005207
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005208 final long identity = Binder.clearCallingIdentity();
5209 try {
5210 final Phone phone = getPhone(subId);
5211 if (phone != null) {
5212 return phone.getServiceState().getDataNetworkType();
5213 } else {
5214 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
5215 }
5216 } finally {
5217 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07005218 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07005219 }
5220
5221 /**
5222 * Returns the data network type
5223 */
5224 @Override
Philip P. Moltmann700a9592019-10-03 11:53:50 -07005225 public int getDataNetworkType(String callingPackage, String callingFeatureId) {
Jack Yu285100e2022-12-02 22:48:35 -08005226 return getDataNetworkTypeForSubscriber(SubscriptionManager.getDefaultDataSubscriptionId(),
Zoey Chenfd61f7f2021-04-21 13:42:10 +08005227 callingPackage, callingFeatureId);
Wink Saville36469e72014-06-11 15:17:00 -07005228 }
5229
5230 /**
5231 * Returns the data network type for a subId
5232 */
5233 @Override
Philip P. Moltmann700a9592019-10-03 11:53:50 -07005234 public int getDataNetworkTypeForSubscriber(int subId, String callingPackage,
5235 String callingFeatureId) {
Sooraj Sasindran5d051bf2021-11-05 13:14:15 -07005236 String functionName = "getDataNetworkTypeForSubscriber";
5237 if (!TelephonyPermissions.checkCallingOrSelfReadNonDangerousPhoneStateNoThrow(
5238 mApp, functionName)) {
5239 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
5240 mApp, subId, callingPackage, callingFeatureId, functionName)) {
5241 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
5242 }
Robert Greenwalt36b23af2015-07-06 17:59:14 -07005243 }
5244
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005245 final long identity = Binder.clearCallingIdentity();
5246 try {
5247 final Phone phone = getPhone(subId);
5248 if (phone != null) {
5249 return phone.getServiceState().getDataNetworkType();
5250 } else {
5251 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
5252 }
5253 } finally {
5254 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07005255 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07005256 }
5257
5258 /**
Wink Saville36469e72014-06-11 15:17:00 -07005259 * Returns the Voice network type for a subId
5260 */
5261 @Override
Philip P. Moltmann700a9592019-10-03 11:53:50 -07005262 public int getVoiceNetworkTypeForSubscriber(int subId, String callingPackage,
5263 String callingFeatureId) {
Sooraj Sasindran5d051bf2021-11-05 13:14:15 -07005264 String functionName = "getVoiceNetworkTypeForSubscriber";
5265 if (!TelephonyPermissions.checkCallingOrSelfReadNonDangerousPhoneStateNoThrow(
5266 mApp, functionName)) {
5267 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
5268 mApp, subId, callingPackage, callingFeatureId, functionName)) {
5269 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
5270 }
Robert Greenwalta5dcfcb2015-07-10 09:06:29 -07005271 }
5272
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005273 final long identity = Binder.clearCallingIdentity();
5274 try {
5275 final Phone phone = getPhone(subId);
5276 if (phone != null) {
5277 return phone.getServiceState().getVoiceNetworkType();
5278 } else {
5279 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
5280 }
5281 } finally {
5282 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07005283 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07005284 }
5285
5286 /**
5287 * @return true if a ICC card is present
5288 */
5289 public boolean hasIccCard() {
Wink Saville36469e72014-06-11 15:17:00 -07005290 // FIXME Make changes to pass defaultSimId of type int
Jack Yu285100e2022-12-02 22:48:35 -08005291 return hasIccCardUsingSlotIndex(SubscriptionManager.getSlotIndex(
Sanket Padawe13bac7b2017-03-20 15:04:47 -07005292 getDefaultSubscription()));
Wink Saville36469e72014-06-11 15:17:00 -07005293 }
5294
5295 /**
Sanket Padawe13bac7b2017-03-20 15:04:47 -07005296 * @return true if a ICC card is present for a slotIndex
Wink Saville36469e72014-06-11 15:17:00 -07005297 */
Sanket Padawe356d7632015-06-22 14:03:32 -07005298 @Override
Sanket Padawe13bac7b2017-03-20 15:04:47 -07005299 public boolean hasIccCardUsingSlotIndex(int slotIndex) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005300 final long identity = Binder.clearCallingIdentity();
5301 try {
5302 final Phone phone = PhoneFactory.getPhone(slotIndex);
5303 if (phone != null) {
5304 return phone.getIccCard().hasIccCard();
5305 } else {
5306 return false;
5307 }
5308 } finally {
5309 Binder.restoreCallingIdentity(identity);
Amit Mahajana6fc2a82015-01-06 11:53:51 -08005310 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07005311 }
5312
5313 /**
5314 * Return if the current radio is LTE on CDMA. This
5315 * is a tri-state return value as for a period of time
5316 * the mode may be unknown.
5317 *
Robert Greenwalt36b23af2015-07-06 17:59:14 -07005318 * @param callingPackage the name of the package making the call.
Santos Cordon7d4ddf62013-07-10 11:58:08 -07005319 * @return {@link Phone#LTE_ON_CDMA_UNKNOWN}, {@link Phone#LTE_ON_CDMA_FALSE}
Jake Hambye994d462014-02-03 13:10:13 -08005320 * or {@link Phone#LTE_ON_CDMA_TRUE}
Santos Cordon7d4ddf62013-07-10 11:58:08 -07005321 */
Robert Greenwalt36b23af2015-07-06 17:59:14 -07005322 @Override
Philip P. Moltmann700a9592019-10-03 11:53:50 -07005323 public int getLteOnCdmaMode(String callingPackage, String callingFeatureId) {
5324 return getLteOnCdmaModeForSubscriber(getDefaultSubscription(), callingPackage,
5325 callingFeatureId);
Wink Saville36469e72014-06-11 15:17:00 -07005326 }
5327
Sanket Padawe356d7632015-06-22 14:03:32 -07005328 @Override
Philip P. Moltmann700a9592019-10-03 11:53:50 -07005329 public int getLteOnCdmaModeForSubscriber(int subId, String callingPackage,
5330 String callingFeatureId) {
Sarah Chin790d2922020-01-16 12:17:23 -08005331 try {
5332 enforceReadPrivilegedPermission("getLteOnCdmaModeForSubscriber");
5333 } catch (SecurityException e) {
Robert Greenwalt36b23af2015-07-06 17:59:14 -07005334 return PhoneConstants.LTE_ON_CDMA_UNKNOWN;
5335 }
5336
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005337 final long identity = Binder.clearCallingIdentity();
5338 try {
5339 final Phone phone = getPhone(subId);
5340 if (phone == null) {
5341 return PhoneConstants.LTE_ON_CDMA_UNKNOWN;
5342 } else {
Nathan Harold05ad6332020-07-10 11:54:36 -07005343 return TelephonyProperties.lte_on_cdma_device()
5344 .orElse(PhoneConstants.LTE_ON_CDMA_FALSE);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005345 }
5346 } finally {
5347 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07005348 }
Wink Saville36469e72014-06-11 15:17:00 -07005349 }
5350
Wink Saville36469e72014-06-11 15:17:00 -07005351 /**
5352 * {@hide}
5353 * Returns Default subId, 0 in the case of single standby.
5354 */
Wink Savilleb564aae2014-10-23 10:18:09 -07005355 private int getDefaultSubscription() {
Jack Yu285100e2022-12-02 22:48:35 -08005356 return SubscriptionManager.getDefaultSubscriptionId();
Wink Saville36469e72014-06-11 15:17:00 -07005357 }
5358
Shishir Agrawala9f32182016-04-12 12:00:16 -07005359 private int getSlotForDefaultSubscription() {
Jack Yu285100e2022-12-02 22:48:35 -08005360 return SubscriptionManager.getPhoneId(getDefaultSubscription());
Shishir Agrawala9f32182016-04-12 12:00:16 -07005361 }
5362
Wink Savilleb564aae2014-10-23 10:18:09 -07005363 private int getPreferredVoiceSubscription() {
Jack Yu285100e2022-12-02 22:48:35 -08005364 return SubscriptionManager.getDefaultVoiceSubscriptionId();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07005365 }
Ihab Awadf2177b72013-11-25 13:33:23 -08005366
Pengquan Menge92a50d2018-09-21 15:54:48 -07005367 private boolean isActiveSubscription(int subId) {
Jack Yu285100e2022-12-02 22:48:35 -08005368 if (PhoneFactory.isSubscriptionManagerServiceEnabled()) {
5369 return SubscriptionManagerService.getInstance().isActiveSubId(subId,
5370 mApp.getOpPackageName(), mApp.getFeatureId());
5371 }
Pengquan Menge92a50d2018-09-21 15:54:48 -07005372 return mSubscriptionController.isActiveSubId(subId);
5373 }
5374
Ihab Awadf2177b72013-11-25 13:33:23 -08005375 /**
5376 * @see android.telephony.TelephonyManager.WifiCallingChoices
5377 */
5378 public int getWhenToMakeWifiCalls() {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005379 final long identity = Binder.clearCallingIdentity();
5380 try {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08005381 return Settings.System.getInt(mApp.getContentResolver(),
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005382 Settings.System.WHEN_TO_MAKE_WIFI_CALLS,
5383 getWhenToMakeWifiCallsDefaultPreference());
5384 } finally {
5385 Binder.restoreCallingIdentity(identity);
5386 }
Ihab Awadf2177b72013-11-25 13:33:23 -08005387 }
5388
5389 /**
5390 * @see android.telephony.TelephonyManager.WifiCallingChoices
5391 */
5392 public void setWhenToMakeWifiCalls(int preference) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005393 final long identity = Binder.clearCallingIdentity();
5394 try {
5395 if (DBG) log("setWhenToMakeWifiCallsStr, storing setting = " + preference);
Nazanin Bakhshif782e562018-12-11 15:15:39 -08005396 Settings.System.putInt(mApp.getContentResolver(),
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005397 Settings.System.WHEN_TO_MAKE_WIFI_CALLS, preference);
5398 } finally {
5399 Binder.restoreCallingIdentity(identity);
5400 }
Ihab Awadf9e92732013-12-05 18:02:52 -08005401 }
5402
Sailesh Nepald1e68152013-12-12 19:08:02 -08005403 private static int getWhenToMakeWifiCallsDefaultPreference() {
Santos Cordonda120f42014-08-06 04:44:34 -07005404 // TODO: Use a build property to choose this value.
Evan Charlton9829e882013-12-19 15:30:38 -08005405 return TelephonyManager.WifiCallingChoices.ALWAYS_USE;
Ihab Awadf2177b72013-11-25 13:33:23 -08005406 }
Shishir Agrawal69f68122013-12-16 17:25:49 -08005407
Muralidhar Reddybd38d952021-12-02 21:04:16 +00005408 private Phone getPhoneFromSlotPortIndexOrThrowException(int slotIndex, int portIndex) {
5409 int phoneId = UiccController.getInstance().getPhoneIdFromSlotPortIndex(slotIndex,
5410 portIndex);
Jordan Liu4c733742019-02-28 12:03:40 -08005411 if (phoneId == -1) {
Muralidhar Reddybd38d952021-12-02 21:04:16 +00005412 throw new IllegalArgumentException("Given slot index: " + slotIndex + " port index: "
5413 + portIndex + " does not correspond to an active phone");
Jordan Liu4c733742019-02-28 12:03:40 -08005414 }
5415 return PhoneFactory.getPhone(phoneId);
5416 }
5417
Shishir Agrawal566b7612013-10-28 14:41:00 -07005418 @Override
Derek Tan740e1672017-06-27 14:56:27 -07005419 public IccOpenLogicalChannelResponse iccOpenLogicalChannel(
Rambo Wanga1782702021-11-10 20:15:19 -08005420 @NonNull IccLogicalChannelRequest request) {
5421 Phone phone = getPhoneFromValidIccLogicalChannelRequest(request,
5422 /*message=*/ "iccOpenLogicalChannel");
5423
5424 if (DBG) log("iccOpenLogicalChannel: request=" + request);
5425 // Verify that the callingPackage in the request belongs to the calling UID
5426 mAppOps.checkPackage(Binder.getCallingUid(), request.callingPackage);
5427
5428 return iccOpenLogicalChannelWithPermission(phone, request);
Jordan Liu4c733742019-02-28 12:03:40 -08005429 }
Shishir Agrawal566b7612013-10-28 14:41:00 -07005430
Rambo Wanga1782702021-11-10 20:15:19 -08005431 private Phone getPhoneFromValidIccLogicalChannelRequest(
5432 @NonNull IccLogicalChannelRequest request, String message) {
5433 Phone phone;
5434 if (request.subId != SubscriptionManager.INVALID_SUBSCRIPTION_ID) {
5435 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
5436 mApp, request.subId, message);
5437 phone = getPhoneFromSubId(request.subId);
5438 } else if (request.slotIndex != SubscriptionManager.INVALID_SIM_SLOT_INDEX) {
5439 enforceModifyPermission();
5440 phone = getPhoneFromSlotPortIndexOrThrowException(request.slotIndex, request.portIndex);
5441 } else {
5442 throw new IllegalArgumentException("Both subId and slotIndex in request are invalid.");
Jordan Liu4c733742019-02-28 12:03:40 -08005443 }
Rambo Wanga1782702021-11-10 20:15:19 -08005444 return phone;
Jordan Liu4c733742019-02-28 12:03:40 -08005445 }
5446
5447 private IccOpenLogicalChannelResponse iccOpenLogicalChannelWithPermission(Phone phone,
Rambo Wanga1782702021-11-10 20:15:19 -08005448 IccLogicalChannelRequest channelRequest) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005449 final long identity = Binder.clearCallingIdentity();
5450 try {
Rambo Wanga1782702021-11-10 20:15:19 -08005451 if (TextUtils.equals(ISDR_AID, channelRequest.aid)) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005452 // Only allows LPA to open logical channel to ISD-R.
Nazanin Bakhshif782e562018-12-11 15:15:39 -08005453 ComponentInfo bestComponent = EuiccConnector.findBestComponent(getDefaultPhone()
5454 .getContext().getPackageManager());
Rambo Wanga1782702021-11-10 20:15:19 -08005455 if (bestComponent == null || !TextUtils.equals(channelRequest.callingPackage,
5456 bestComponent.packageName)) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005457 loge("The calling package is not allowed to access ISD-R.");
5458 throw new SecurityException(
5459 "The calling package is not allowed to access ISD-R.");
5460 }
Derek Tan740e1672017-06-27 14:56:27 -07005461 }
Derek Tan740e1672017-06-27 14:56:27 -07005462
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005463 IccOpenLogicalChannelResponse response = (IccOpenLogicalChannelResponse) sendRequest(
Rambo Wanga1782702021-11-10 20:15:19 -08005464 CMD_OPEN_CHANNEL, channelRequest, phone, null /* workSource */);
5465 if (DBG) log("iccOpenLogicalChannelWithPermission: response=" + response);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005466 return response;
5467 } finally {
5468 Binder.restoreCallingIdentity(identity);
5469 }
Shishir Agrawal566b7612013-10-28 14:41:00 -07005470 }
5471
5472 @Override
Rambo Wanga1782702021-11-10 20:15:19 -08005473 public boolean iccCloseLogicalChannel(@NonNull IccLogicalChannelRequest request) {
5474 Phone phone = getPhoneFromValidIccLogicalChannelRequest(request,
5475 /*message=*/"iccCloseLogicalChannel");
5476
5477 if (DBG) log("iccCloseLogicalChannel: request=" + request);
5478
5479 return iccCloseLogicalChannelWithPermission(phone, request);
Jordan Liu4c733742019-02-28 12:03:40 -08005480 }
Shishir Agrawal566b7612013-10-28 14:41:00 -07005481
Rambo Wanga1782702021-11-10 20:15:19 -08005482 private boolean iccCloseLogicalChannelWithPermission(Phone phone,
5483 IccLogicalChannelRequest request) {
Chen Xua8f0dff2022-02-12 00:34:15 -08005484 // before this feature is enabled, this API should only return false if
5485 // the operation fails instead of throwing runtime exception for
5486 // backward-compatibility.
5487 final boolean shouldThrowExceptionOnFailure = CompatChanges.isChangeEnabled(
5488 ICC_CLOSE_CHANNEL_EXCEPTION_ON_FAILURE, Binder.getCallingUid());
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005489 final long identity = Binder.clearCallingIdentity();
5490 try {
Rambo Wanga1782702021-11-10 20:15:19 -08005491 if (request.channel < 0) {
Chen Xu540470b2021-12-14 17:15:47 -08005492 throw new IllegalArgumentException("request.channel is less than 0");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005493 }
Chen Xue9d737e2022-01-01 23:41:31 -08005494 Object result = sendRequest(CMD_CLOSE_CHANNEL, request.channel, phone,
Jordan Liu4c733742019-02-28 12:03:40 -08005495 null /* workSource */);
Chen Xue9d737e2022-01-01 23:41:31 -08005496 Boolean success = false;
5497 if (result instanceof RuntimeException) {
5498 // if there is an exception returned, throw from the binder thread here.
Chen Xua8f0dff2022-02-12 00:34:15 -08005499 if (shouldThrowExceptionOnFailure) {
5500 throw (RuntimeException) result;
5501 } else {
5502 return false;
5503 }
Chen Xue9d737e2022-01-01 23:41:31 -08005504 } else if (result instanceof Boolean) {
5505 success = (Boolean) result;
5506 } else {
5507 loge("iccCloseLogicalChannelWithPermission: supported return type " + result);
5508 }
Rambo Wanga1782702021-11-10 20:15:19 -08005509 if (DBG) log("iccCloseLogicalChannelWithPermission: success=" + success);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005510 return success;
5511 } finally {
5512 Binder.restoreCallingIdentity(identity);
Shishir Agrawal566b7612013-10-28 14:41:00 -07005513 }
Shishir Agrawal566b7612013-10-28 14:41:00 -07005514 }
5515
5516 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08005517 public String iccTransmitApduLogicalChannel(int subId, int channel, int cla,
Shishir Agrawal566b7612013-10-28 14:41:00 -07005518 int command, int p1, int p2, int p3, String data) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08005519 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
5520 mApp, subId, "iccTransmitApduLogicalChannel");
Jordan Liu4c733742019-02-28 12:03:40 -08005521 if (DBG) {
5522 log("iccTransmitApduLogicalChannel: subId=" + subId + " chnl=" + channel
5523 + " cla=" + cla + " cmd=" + command + " p1=" + p1 + " p2=" + p2 + " p3="
5524 + p3 + " data=" + data);
5525 }
5526 return iccTransmitApduLogicalChannelWithPermission(getPhoneFromSubId(subId), channel, cla,
5527 command, p1, p2, p3, data);
5528 }
Shishir Agrawal566b7612013-10-28 14:41:00 -07005529
Jordan Liu4c733742019-02-28 12:03:40 -08005530 @Override
Muralidhar Reddybd38d952021-12-02 21:04:16 +00005531 public String iccTransmitApduLogicalChannelByPort(int slotIndex, int portIndex, int channel,
5532 int cla, int command, int p1, int p2, int p3, String data) {
Jordan Liu4c733742019-02-28 12:03:40 -08005533 enforceModifyPermission();
5534 if (DBG) {
Muralidhar Reddybd38d952021-12-02 21:04:16 +00005535 log("iccTransmitApduLogicalChannelByPort: slotIndex=" + slotIndex + " portIndex="
5536 + portIndex + " chnl=" + channel + " cla=" + cla + " cmd=" + command + " p1="
5537 + p1 + " p2=" + p2 + " p3=" + p3 + " data=" + data);
Jordan Liu4c733742019-02-28 12:03:40 -08005538 }
5539 return iccTransmitApduLogicalChannelWithPermission(
Muralidhar Reddybd38d952021-12-02 21:04:16 +00005540 getPhoneFromSlotPortIndexOrThrowException(slotIndex, portIndex), channel, cla,
5541 command, p1, p2, p3, data);
Jordan Liu4c733742019-02-28 12:03:40 -08005542 }
5543
5544 private String iccTransmitApduLogicalChannelWithPermission(Phone phone, int channel, int cla,
5545 int command, int p1, int p2, int p3, String data) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005546 final long identity = Binder.clearCallingIdentity();
5547 try {
Hall Liu4fd771b2019-05-02 09:16:29 -07005548 if (channel <= 0) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005549 return "";
5550 }
Shishir Agrawal566b7612013-10-28 14:41:00 -07005551
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005552 IccIoResult response = (IccIoResult) sendRequest(CMD_TRANSMIT_APDU_LOGICAL_CHANNEL,
Jordan Liu4c733742019-02-28 12:03:40 -08005553 new IccAPDUArgument(channel, cla, command, p1, p2, p3, data), phone,
5554 null /* workSource */);
5555 if (DBG) log("iccTransmitApduLogicalChannelWithPermission: " + response);
Shishir Agrawal566b7612013-10-28 14:41:00 -07005556
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005557 // Append the returned status code to the end of the response payload.
5558 String s = Integer.toHexString(
5559 (response.sw1 << 8) + response.sw2 + 0x10000).substring(1);
5560 if (response.payload != null) {
5561 s = IccUtils.bytesToHexString(response.payload) + s;
5562 }
5563 return s;
5564 } finally {
5565 Binder.restoreCallingIdentity(identity);
Shishir Agrawal5ec14172014-08-05 17:05:45 -07005566 }
Shishir Agrawal566b7612013-10-28 14:41:00 -07005567 }
Jake Hambye994d462014-02-03 13:10:13 -08005568
Evan Charltonc66da362014-05-16 14:06:40 -07005569 @Override
Holly Jiuyu Sun1cc2d552018-01-26 15:51:16 -08005570 public String iccTransmitApduBasicChannel(int subId, String callingPackage, int cla,
5571 int command, int p1, int p2, int p3, String data) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08005572 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
5573 mApp, subId, "iccTransmitApduBasicChannel");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005574 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
Jordan Liu4c733742019-02-28 12:03:40 -08005575 if (DBG) {
5576 log("iccTransmitApduBasicChannel: subId=" + subId + " cla=" + cla + " cmd="
5577 + command + " p1=" + p1 + " p2=" + p2 + " p3=" + p3 + " data=" + data);
5578 }
5579 return iccTransmitApduBasicChannelWithPermission(getPhoneFromSubId(subId), callingPackage,
5580 cla, command, p1, p2, p3, data);
5581 }
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07005582
Jordan Liu4c733742019-02-28 12:03:40 -08005583 @Override
Muralidhar Reddybd38d952021-12-02 21:04:16 +00005584 public String iccTransmitApduBasicChannelByPort(int slotIndex, int portIndex,
5585 String callingPackage, int cla, int command, int p1, int p2, int p3, String data) {
Jordan Liu4c733742019-02-28 12:03:40 -08005586 enforceModifyPermission();
5587 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
5588 if (DBG) {
Muralidhar Reddybd38d952021-12-02 21:04:16 +00005589 log("iccTransmitApduBasicChannelByPort: slotIndex=" + slotIndex + " portIndex="
5590 + portIndex + " cla=" + cla + " cmd=" + command + " p1=" + p1 + " p2="
5591 + p2 + " p3=" + p3 + " data=" + data);
Jordan Liu4c733742019-02-28 12:03:40 -08005592 }
5593
5594 return iccTransmitApduBasicChannelWithPermission(
Muralidhar Reddybd38d952021-12-02 21:04:16 +00005595 getPhoneFromSlotPortIndexOrThrowException(slotIndex, portIndex), callingPackage,
5596 cla, command, p1, p2, p3, data);
Jordan Liu4c733742019-02-28 12:03:40 -08005597 }
5598
5599 // open APDU basic channel assuming the caller has sufficient permissions
5600 private String iccTransmitApduBasicChannelWithPermission(Phone phone, String callingPackage,
5601 int cla, int command, int p1, int p2, int p3, String data) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005602 final long identity = Binder.clearCallingIdentity();
5603 try {
5604 if (command == SELECT_COMMAND && p1 == SELECT_P1 && p2 == SELECT_P2 && p3 == SELECT_P3
5605 && TextUtils.equals(ISDR_AID, data)) {
5606 // Only allows LPA to select ISD-R.
Nazanin Bakhshif782e562018-12-11 15:15:39 -08005607 ComponentInfo bestComponent = EuiccConnector.findBestComponent(getDefaultPhone()
5608 .getContext().getPackageManager());
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005609 if (bestComponent == null
5610 || !TextUtils.equals(callingPackage, bestComponent.packageName)) {
5611 loge("The calling package is not allowed to select ISD-R.");
5612 throw new SecurityException(
5613 "The calling package is not allowed to select ISD-R.");
5614 }
Holly Jiuyu Sun1cc2d552018-01-26 15:51:16 -08005615 }
Holly Jiuyu Sun1cc2d552018-01-26 15:51:16 -08005616
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005617 IccIoResult response = (IccIoResult) sendRequest(CMD_TRANSMIT_APDU_BASIC_CHANNEL,
Jordan Liu4c733742019-02-28 12:03:40 -08005618 new IccAPDUArgument(0, cla, command, p1, p2, p3, data), phone,
5619 null /* workSource */);
5620 if (DBG) log("iccTransmitApduBasicChannelWithPermission: " + response);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07005621
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005622 // Append the returned status code to the end of the response payload.
5623 String s = Integer.toHexString(
5624 (response.sw1 << 8) + response.sw2 + 0x10000).substring(1);
5625 if (response.payload != null) {
5626 s = IccUtils.bytesToHexString(response.payload) + s;
5627 }
5628 return s;
5629 } finally {
5630 Binder.restoreCallingIdentity(identity);
Shishir Agrawal5ec14172014-08-05 17:05:45 -07005631 }
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07005632 }
5633
5634 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08005635 public byte[] iccExchangeSimIO(int subId, int fileID, int command, int p1, int p2, int p3,
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07005636 String filePath) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08005637 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
5638 mApp, subId, "iccExchangeSimIO");
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07005639
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005640 final long identity = Binder.clearCallingIdentity();
5641 try {
5642 if (DBG) {
5643 log("Exchange SIM_IO " + subId + ":" + fileID + ":" + command + " "
5644 + p1 + " " + p2 + " " + p3 + ":" + filePath);
5645 }
5646
5647 IccIoResult response =
5648 (IccIoResult) sendRequest(CMD_EXCHANGE_SIM_IO,
5649 new IccAPDUArgument(-1, fileID, command, p1, p2, p3, filePath),
5650 subId);
5651
5652 if (DBG) {
5653 log("Exchange SIM_IO [R]" + response);
5654 }
5655
5656 byte[] result = null;
5657 int length = 2;
5658 if (response.payload != null) {
5659 length = 2 + response.payload.length;
5660 result = new byte[length];
5661 System.arraycopy(response.payload, 0, result, 0, response.payload.length);
5662 } else {
5663 result = new byte[length];
5664 }
5665
5666 result[length - 1] = (byte) response.sw2;
5667 result[length - 2] = (byte) response.sw1;
5668 return result;
5669 } finally {
5670 Binder.restoreCallingIdentity(identity);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07005671 }
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07005672 }
5673
Nathan Haroldb3014052017-01-25 15:57:32 -08005674 /**
5675 * Get the forbidden PLMN List from the given app type (ex APPTYPE_USIM)
5676 * on a particular subscription
5677 */
Philip P. Moltmann700a9592019-10-03 11:53:50 -07005678 public String[] getForbiddenPlmns(int subId, int appType, String callingPackage,
5679 String callingFeatureId) {
sqianb6e41952018-03-12 14:54:01 -07005680 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
Philip P. Moltmann700a9592019-10-03 11:53:50 -07005681 mApp, subId, callingPackage, callingFeatureId, "getForbiddenPlmns")) {
sqianb6e41952018-03-12 14:54:01 -07005682 return null;
5683 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005684
5685 final long identity = Binder.clearCallingIdentity();
5686 try {
5687 if (appType != TelephonyManager.APPTYPE_USIM
5688 && appType != TelephonyManager.APPTYPE_SIM) {
5689 loge("getForbiddenPlmnList(): App Type must be USIM or SIM");
5690 return null;
5691 }
5692 Object response = sendRequest(
5693 CMD_GET_FORBIDDEN_PLMNS, new Integer(appType), subId);
5694 if (response instanceof String[]) {
5695 return (String[]) response;
5696 }
yincheng zhao2737e882019-09-06 17:06:54 -07005697 // Response is an Exception of some kind
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005698 // which is signalled to the user as a NULL retval
Nathan Haroldb3014052017-01-25 15:57:32 -08005699 return null;
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005700 } finally {
5701 Binder.restoreCallingIdentity(identity);
Nathan Haroldb3014052017-01-25 15:57:32 -08005702 }
Nathan Haroldb3014052017-01-25 15:57:32 -08005703 }
5704
yincheng zhao2737e882019-09-06 17:06:54 -07005705 /**
5706 * Set the forbidden PLMN list from the given app type (ex APPTYPE_USIM) on a particular
5707 * subscription.
5708 *
5709 * @param subId the id of the subscription.
5710 * @param appType the uicc app type, must be USIM or SIM.
5711 * @param fplmns the Forbiden plmns list that needed to be written to the SIM.
5712 * @param callingPackage the op Package name.
Philip P. Moltmann700a9592019-10-03 11:53:50 -07005713 * @param callingFeatureId the feature in the package.
yincheng zhao2737e882019-09-06 17:06:54 -07005714 * @return number of fplmns that is successfully written to the SIM.
5715 */
Philip P. Moltmann700a9592019-10-03 11:53:50 -07005716 public int setForbiddenPlmns(int subId, int appType, List<String> fplmns, String callingPackage,
5717 String callingFeatureId) {
Jayachandran C5b0d75a2021-10-21 22:15:27 -07005718 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
5719 mApp, subId, "setForbiddenPlmns");
5720
yincheng zhao2737e882019-09-06 17:06:54 -07005721 if (appType != TelephonyManager.APPTYPE_USIM && appType != TelephonyManager.APPTYPE_SIM) {
5722 loge("setForbiddenPlmnList(): App Type must be USIM or SIM");
5723 throw new IllegalArgumentException("Invalid appType: App Type must be USIM or SIM");
5724 }
5725 if (fplmns == null) {
5726 throw new IllegalArgumentException("Fplmn List provided is null");
5727 }
5728 for (String fplmn : fplmns) {
5729 if (!CellIdentity.isValidPlmn(fplmn)) {
5730 throw new IllegalArgumentException("Invalid fplmn provided: " + fplmn);
5731 }
5732 }
5733 final long identity = Binder.clearCallingIdentity();
5734 try {
5735 Object response = sendRequest(
5736 CMD_SET_FORBIDDEN_PLMNS,
5737 new Pair<Integer, List<String>>(new Integer(appType), fplmns),
5738 subId);
5739 return (int) response;
5740 } finally {
5741 Binder.restoreCallingIdentity(identity);
5742 }
5743 }
5744
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07005745 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08005746 public String sendEnvelopeWithStatus(int subId, String content) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08005747 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
5748 mApp, subId, "sendEnvelopeWithStatus");
Evan Charltonc66da362014-05-16 14:06:40 -07005749
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005750 final long identity = Binder.clearCallingIdentity();
5751 try {
5752 IccIoResult response = (IccIoResult) sendRequest(CMD_SEND_ENVELOPE, content, subId);
5753 if (response.payload == null) {
5754 return "";
5755 }
Evan Charltonc66da362014-05-16 14:06:40 -07005756
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005757 // Append the returned status code to the end of the response payload.
5758 String s = Integer.toHexString(
5759 (response.sw1 << 8) + response.sw2 + 0x10000).substring(1);
5760 s = IccUtils.bytesToHexString(response.payload) + s;
5761 return s;
5762 } finally {
5763 Binder.restoreCallingIdentity(identity);
5764 }
Evan Charltonc66da362014-05-16 14:06:40 -07005765 }
5766
Jake Hambye994d462014-02-03 13:10:13 -08005767 /**
5768 * Read one of the NV items defined in {@link com.android.internal.telephony.RadioNVItems}
5769 * and {@code ril_nv_items.h}. Used for device configuration by some CDMA operators.
5770 *
5771 * @param itemID the ID of the item to read
5772 * @return the NV item as a String, or null on error.
5773 */
5774 @Override
5775 public String nvReadItem(int itemID) {
vagdeviaf9a5b92018-08-15 16:01:53 -07005776 WorkSource workSource = getWorkSource(Binder.getCallingUid());
Jeff Davidson7e17e312018-02-13 18:17:36 -08005777 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
5778 mApp, getDefaultSubscription(), "nvReadItem");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005779
5780 final long identity = Binder.clearCallingIdentity();
5781 try {
5782 if (DBG) log("nvReadItem: item " + itemID);
vagdeviaf9a5b92018-08-15 16:01:53 -07005783 String value = (String) sendRequest(CMD_NV_READ_ITEM, itemID, workSource);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005784 if (DBG) log("nvReadItem: item " + itemID + " is \"" + value + '"');
5785 return value;
5786 } finally {
5787 Binder.restoreCallingIdentity(identity);
5788 }
Jake Hambye994d462014-02-03 13:10:13 -08005789 }
5790
5791 /**
5792 * Write one of the NV items defined in {@link com.android.internal.telephony.RadioNVItems}
5793 * and {@code ril_nv_items.h}. Used for device configuration by some CDMA operators.
5794 *
5795 * @param itemID the ID of the item to read
5796 * @param itemValue the value to write, as a String
5797 * @return true on success; false on any failure
5798 */
5799 @Override
5800 public boolean nvWriteItem(int itemID, String itemValue) {
vagdeviaf9a5b92018-08-15 16:01:53 -07005801 WorkSource workSource = getWorkSource(Binder.getCallingUid());
Jeff Davidson7e17e312018-02-13 18:17:36 -08005802 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
5803 mApp, getDefaultSubscription(), "nvWriteItem");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005804
5805 final long identity = Binder.clearCallingIdentity();
5806 try {
5807 if (DBG) log("nvWriteItem: item " + itemID + " value \"" + itemValue + '"');
5808 Boolean success = (Boolean) sendRequest(CMD_NV_WRITE_ITEM,
vagdeviaf9a5b92018-08-15 16:01:53 -07005809 new Pair<Integer, String>(itemID, itemValue), workSource);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005810 if (DBG) log("nvWriteItem: item " + itemID + ' ' + (success ? "ok" : "fail"));
5811 return success;
5812 } finally {
5813 Binder.restoreCallingIdentity(identity);
5814 }
Jake Hambye994d462014-02-03 13:10:13 -08005815 }
5816
5817 /**
5818 * Update the CDMA Preferred Roaming List (PRL) in the radio NV storage.
5819 * Used for device configuration by some CDMA operators.
5820 *
5821 * @param preferredRoamingList byte array containing the new PRL
5822 * @return true on success; false on any failure
5823 */
5824 @Override
5825 public boolean nvWriteCdmaPrl(byte[] preferredRoamingList) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08005826 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
5827 mApp, getDefaultSubscription(), "nvWriteCdmaPrl");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005828
5829 final long identity = Binder.clearCallingIdentity();
5830 try {
5831 if (DBG) log("nvWriteCdmaPrl: value: " + HexDump.toHexString(preferredRoamingList));
5832 Boolean success = (Boolean) sendRequest(CMD_NV_WRITE_CDMA_PRL, preferredRoamingList);
5833 if (DBG) log("nvWriteCdmaPrl: " + (success ? "ok" : "fail"));
5834 return success;
5835 } finally {
5836 Binder.restoreCallingIdentity(identity);
5837 }
Jake Hambye994d462014-02-03 13:10:13 -08005838 }
5839
5840 /**
chen xu6dac5ab2018-10-26 17:39:23 -07005841 * Rollback modem configurations to factory default except some config which are in whitelist.
Jake Hambye994d462014-02-03 13:10:13 -08005842 * Used for device configuration by some CDMA operators.
5843 *
chen xu6dac5ab2018-10-26 17:39:23 -07005844 * @param slotIndex - device slot.
5845 *
Jake Hambye994d462014-02-03 13:10:13 -08005846 * @return true on success; false on any failure
5847 */
5848 @Override
chen xu6dac5ab2018-10-26 17:39:23 -07005849 public boolean resetModemConfig(int slotIndex) {
5850 Phone phone = PhoneFactory.getPhone(slotIndex);
5851 if (phone != null) {
5852 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
5853 mApp, phone.getSubId(), "resetModemConfig");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005854
chen xu6dac5ab2018-10-26 17:39:23 -07005855 final long identity = Binder.clearCallingIdentity();
5856 try {
5857 Boolean success = (Boolean) sendRequest(CMD_RESET_MODEM_CONFIG, null);
5858 if (DBG) log("resetModemConfig:" + ' ' + (success ? "ok" : "fail"));
5859 return success;
5860 } finally {
5861 Binder.restoreCallingIdentity(identity);
5862 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005863 }
chen xu6dac5ab2018-10-26 17:39:23 -07005864 return false;
5865 }
5866
5867 /**
5868 * Generate a radio modem reset. Used for device configuration by some CDMA operators.
5869 *
5870 * @param slotIndex - device slot.
5871 *
5872 * @return true on success; false on any failure
5873 */
5874 @Override
5875 public boolean rebootModem(int slotIndex) {
5876 Phone phone = PhoneFactory.getPhone(slotIndex);
5877 if (phone != null) {
5878 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
5879 mApp, phone.getSubId(), "rebootModem");
5880
5881 final long identity = Binder.clearCallingIdentity();
5882 try {
5883 Boolean success = (Boolean) sendRequest(CMD_MODEM_REBOOT, null);
5884 if (DBG) log("rebootModem:" + ' ' + (success ? "ok" : "fail"));
5885 return success;
5886 } finally {
5887 Binder.restoreCallingIdentity(identity);
5888 }
5889 }
5890 return false;
Jake Hambye994d462014-02-03 13:10:13 -08005891 }
Jake Hamby7c27be32014-03-03 13:25:59 -08005892
Brad Ebinger51f743a2017-01-23 13:50:20 -08005893 /**
Grace Jiaaa2eb6b2020-01-09 16:26:08 -08005894 * Toggle IMS disable and enable for the framework to reset it. See {@link #enableIms(int)} and
5895 * {@link #disableIms(int)}.
5896 * @param slotIndex device slot.
5897 */
5898 public void resetIms(int slotIndex) {
5899 enforceModifyPermission();
5900
5901 final long identity = Binder.clearCallingIdentity();
5902 try {
5903 if (mImsResolver == null) {
5904 // may happen if the does not support IMS.
5905 return;
5906 }
Hyunhoa17ac7c2022-08-30 12:03:04 +00005907 mImsResolver.resetIms(slotIndex);
Grace Jiaaa2eb6b2020-01-09 16:26:08 -08005908 } finally {
5909 Binder.restoreCallingIdentity(identity);
5910 }
5911 }
5912
5913 /**
Brad Ebinger22bc3e42018-01-16 09:39:35 -08005914 * Enables IMS for the framework. This will trigger IMS registration and ImsFeature capability
5915 * status updates, if not already enabled.
Brad Ebinger51f743a2017-01-23 13:50:20 -08005916 */
Brad Ebinger22bc3e42018-01-16 09:39:35 -08005917 public void enableIms(int slotId) {
Brad Ebinger51f743a2017-01-23 13:50:20 -08005918 enforceModifyPermission();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005919
5920 final long identity = Binder.clearCallingIdentity();
5921 try {
Brad Ebinger24c29992019-12-05 13:03:21 -08005922 if (mImsResolver == null) {
Brad Ebinger9c0eb502019-01-23 15:06:19 -08005923 // may happen if the device does not support IMS.
5924 return;
5925 }
Brad Ebinger24c29992019-12-05 13:03:21 -08005926 mImsResolver.enableIms(slotId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005927 } finally {
5928 Binder.restoreCallingIdentity(identity);
5929 }
Brad Ebinger34bef922017-11-09 10:27:08 -08005930 }
5931
5932 /**
Brad Ebinger22bc3e42018-01-16 09:39:35 -08005933 * Disables IMS for the framework. This will trigger IMS de-registration and trigger ImsFeature
5934 * status updates to disabled.
Brad Ebinger34bef922017-11-09 10:27:08 -08005935 */
Brad Ebinger22bc3e42018-01-16 09:39:35 -08005936 public void disableIms(int slotId) {
5937 enforceModifyPermission();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005938
5939 final long identity = Binder.clearCallingIdentity();
5940 try {
Brad Ebinger24c29992019-12-05 13:03:21 -08005941 if (mImsResolver == null) {
Brad Ebinger9c0eb502019-01-23 15:06:19 -08005942 // may happen if the device does not support IMS.
5943 return;
5944 }
Brad Ebinger24c29992019-12-05 13:03:21 -08005945 mImsResolver.disableIms(slotId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005946 } finally {
5947 Binder.restoreCallingIdentity(identity);
5948 }
Brad Ebinger22bc3e42018-01-16 09:39:35 -08005949 }
5950
5951 /**
Brad Ebinger67b3e042020-09-11 12:45:11 -07005952 * Registers for updates to the MmTelFeature connection through the IImsServiceFeatureCallback
5953 * callback.
Brad Ebinger22bc3e42018-01-16 09:39:35 -08005954 */
Brad Ebinger67b3e042020-09-11 12:45:11 -07005955 @Override
Brad Ebingerf6aca002020-10-01 13:51:05 -07005956 public void registerMmTelFeatureCallback(int slotId, IImsServiceFeatureCallback callback) {
Brad Ebinger34bef922017-11-09 10:27:08 -08005957 enforceModifyPermission();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005958
5959 final long identity = Binder.clearCallingIdentity();
5960 try {
Brad Ebinger24c29992019-12-05 13:03:21 -08005961 if (mImsResolver == null) {
Brad Ebinger67b3e042020-09-11 12:45:11 -07005962 throw new ServiceSpecificException(ImsException.CODE_ERROR_UNSUPPORTED_OPERATION,
5963 "Device does not support IMS");
Brad Ebinger9c0eb502019-01-23 15:06:19 -08005964 }
Brad Ebingerf6aca002020-10-01 13:51:05 -07005965 mImsResolver.listenForFeature(slotId, ImsFeature.FEATURE_MMTEL, callback);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005966 } finally {
5967 Binder.restoreCallingIdentity(identity);
5968 }
Brad Ebinger34bef922017-11-09 10:27:08 -08005969 }
Brad Ebinger5f64b052017-12-14 14:26:15 -08005970 /**
Brad Ebinger075ff3a2020-05-18 17:52:58 -07005971 * Unregister a previously registered IImsServiceFeatureCallback associated with an ImsFeature.
5972 */
Brad Ebinger67b3e042020-09-11 12:45:11 -07005973 @Override
5974 public void unregisterImsFeatureCallback(IImsServiceFeatureCallback callback) {
Brad Ebinger075ff3a2020-05-18 17:52:58 -07005975 enforceModifyPermission();
5976
5977 final long identity = Binder.clearCallingIdentity();
5978 try {
5979 if (mImsResolver == null) return;
Brad Ebinger67b3e042020-09-11 12:45:11 -07005980 mImsResolver.unregisterImsFeatureCallback(callback);
Brad Ebinger075ff3a2020-05-18 17:52:58 -07005981 } finally {
5982 Binder.restoreCallingIdentity(identity);
5983 }
5984 }
5985
5986 /**
Brad Ebinger5f64b052017-12-14 14:26:15 -08005987 * Returns the {@link IImsRegistration} structure associated with the slotId and feature
Brad Ebinger9c0eb502019-01-23 15:06:19 -08005988 * specified or null if IMS is not supported on the slot specified.
Brad Ebinger5f64b052017-12-14 14:26:15 -08005989 */
5990 public IImsRegistration getImsRegistration(int slotId, int feature) throws RemoteException {
5991 enforceModifyPermission();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005992
5993 final long identity = Binder.clearCallingIdentity();
5994 try {
Brad Ebinger24c29992019-12-05 13:03:21 -08005995 if (mImsResolver == null) {
Brad Ebinger9c0eb502019-01-23 15:06:19 -08005996 // may happen if the device does not support IMS.
5997 return null;
5998 }
Brad Ebinger24c29992019-12-05 13:03:21 -08005999 return mImsResolver.getImsRegistration(slotId, feature);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006000 } finally {
6001 Binder.restoreCallingIdentity(identity);
6002 }
Brad Ebinger5f64b052017-12-14 14:26:15 -08006003 }
6004
Brad Ebinger22bc3e42018-01-16 09:39:35 -08006005 /**
6006 * Returns the {@link IImsConfig} structure associated with the slotId and feature
Brad Ebinger9c0eb502019-01-23 15:06:19 -08006007 * specified or null if IMS is not supported on the slot specified.
Brad Ebinger22bc3e42018-01-16 09:39:35 -08006008 */
6009 public IImsConfig getImsConfig(int slotId, int feature) throws RemoteException {
6010 enforceModifyPermission();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006011
6012 final long identity = Binder.clearCallingIdentity();
6013 try {
Brad Ebinger24c29992019-12-05 13:03:21 -08006014 if (mImsResolver == null) {
Brad Ebinger9c0eb502019-01-23 15:06:19 -08006015 // may happen if the device does not support IMS.
6016 return null;
6017 }
Brad Ebinger24c29992019-12-05 13:03:21 -08006018 return mImsResolver.getImsConfig(slotId, feature);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006019 } finally {
6020 Binder.restoreCallingIdentity(identity);
6021 }
Brad Ebinger22bc3e42018-01-16 09:39:35 -08006022 }
6023
Brad Ebinger884c07b2018-02-15 16:17:40 -08006024 /**
Brad Ebingerdac2f002018-04-03 15:17:52 -07006025 * Sets the ImsService Package Name that Telephony will bind to.
6026 *
Brad Ebinger24c29992019-12-05 13:03:21 -08006027 * @param slotIndex the slot ID that the ImsService should bind for.
6028 * @param isCarrierService true if the ImsService is the carrier override, false if the
Brad Ebingerdac2f002018-04-03 15:17:52 -07006029 * ImsService is the device default ImsService.
Brad Ebinger24c29992019-12-05 13:03:21 -08006030 * @param featureTypes An integer array of feature types associated with a packageName.
6031 * @param packageName The name of the package that the current configuration will be replaced
6032 * with.
Brad Ebingerdac2f002018-04-03 15:17:52 -07006033 * @return true if setting the ImsService to bind to succeeded, false if it did not.
Brad Ebingerdac2f002018-04-03 15:17:52 -07006034 */
Brad Ebinger24c29992019-12-05 13:03:21 -08006035 public boolean setBoundImsServiceOverride(int slotIndex, boolean isCarrierService,
6036 int[] featureTypes, String packageName) {
Brad Ebinger24c29992019-12-05 13:03:21 -08006037 TelephonyPermissions.enforceShellOnly(Binder.getCallingUid(), "setBoundImsServiceOverride");
Brad Ebingerde696de2018-04-06 09:56:40 -07006038 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(mApp,
Jack Yu00ece8c2022-11-19 22:29:12 -08006039 SubscriptionManager.getSubscriptionId(slotIndex), "setBoundImsServiceOverride");
Brad Ebingerde696de2018-04-06 09:56:40 -07006040
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006041 final long identity = Binder.clearCallingIdentity();
6042 try {
Brad Ebinger24c29992019-12-05 13:03:21 -08006043 if (mImsResolver == null) {
Brad Ebinger9c0eb502019-01-23 15:06:19 -08006044 // may happen if the device does not support IMS.
6045 return false;
6046 }
Brad Ebinger24c29992019-12-05 13:03:21 -08006047 Map<Integer, String> featureConfig = new HashMap<>();
6048 for (int featureType : featureTypes) {
6049 featureConfig.put(featureType, packageName);
6050 }
6051 return mImsResolver.overrideImsServiceConfiguration(slotIndex, isCarrierService,
6052 featureConfig);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006053 } finally {
6054 Binder.restoreCallingIdentity(identity);
6055 }
Brad Ebingerdac2f002018-04-03 15:17:52 -07006056 }
6057
6058 /**
Brad Ebinger999d3302020-11-25 14:31:39 -08006059 * Clears any carrier ImsService overrides for the slot index specified that were previously
6060 * set with {@link #setBoundImsServiceOverride(int, boolean, int[], String)}.
6061 *
6062 * This should only be used for testing.
6063 *
6064 * @param slotIndex the slot ID that the ImsService should bind for.
6065 * @return true if clearing the carrier ImsService override succeeded or false if it did not.
6066 */
6067 @Override
6068 public boolean clearCarrierImsServiceOverride(int slotIndex) {
Brad Ebinger999d3302020-11-25 14:31:39 -08006069 TelephonyPermissions.enforceShellOnly(Binder.getCallingUid(),
6070 "clearCarrierImsServiceOverride");
6071 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(mApp,
Jack Yu00ece8c2022-11-19 22:29:12 -08006072 SubscriptionManager.getSubscriptionId(slotIndex), "clearCarrierImsServiceOverride");
Brad Ebinger999d3302020-11-25 14:31:39 -08006073
6074 final long identity = Binder.clearCallingIdentity();
6075 try {
6076 if (mImsResolver == null) {
6077 // may happen if the device does not support IMS.
6078 return false;
6079 }
6080 return mImsResolver.clearCarrierImsServiceConfiguration(slotIndex);
6081 } finally {
6082 Binder.restoreCallingIdentity(identity);
6083 }
6084 }
6085
6086 /**
Brad Ebinger24c29992019-12-05 13:03:21 -08006087 * Return the package name of the currently bound ImsService.
Brad Ebingerdac2f002018-04-03 15:17:52 -07006088 *
6089 * @param slotId The slot that the ImsService is associated with.
6090 * @param isCarrierImsService true, if the ImsService is a carrier override, false if it is
6091 * the device default.
Brad Ebinger24c29992019-12-05 13:03:21 -08006092 * @param featureType The feature associated with the queried configuration.
Brad Ebingerdac2f002018-04-03 15:17:52 -07006093 * @return the package name of the ImsService configuration.
6094 */
Brad Ebinger24c29992019-12-05 13:03:21 -08006095 public String getBoundImsServicePackage(int slotId, boolean isCarrierImsService,
6096 @ImsFeature.FeatureType int featureType) {
Brad Ebinger24c29992019-12-05 13:03:21 -08006097 TelephonyPermissions
Jack Yu00ece8c2022-11-19 22:29:12 -08006098 .enforceCallingOrSelfReadPrivilegedPhoneStatePermissionOrCarrierPrivilege(mApp,
6099 SubscriptionManager.getSubscriptionId(slotId), "getBoundImsServicePackage");
Brad Ebingerde696de2018-04-06 09:56:40 -07006100
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006101 final long identity = Binder.clearCallingIdentity();
6102 try {
Brad Ebinger24c29992019-12-05 13:03:21 -08006103 if (mImsResolver == null) {
Brad Ebinger9c0eb502019-01-23 15:06:19 -08006104 // may happen if the device does not support IMS.
6105 return "";
6106 }
Brad Ebingera80c3312019-12-02 10:59:39 -08006107 // TODO: change API to query RCS separately.
Brad Ebinger24c29992019-12-05 13:03:21 -08006108 return mImsResolver.getImsServiceConfiguration(slotId, isCarrierImsService,
6109 featureType);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006110 } finally {
6111 Binder.restoreCallingIdentity(identity);
6112 }
Brad Ebingerdac2f002018-04-03 15:17:52 -07006113 }
6114
Brad Ebingerbc7dd582019-10-17 17:03:22 -07006115 /**
6116 * Get the MmTelFeature state associated with the requested subscription id.
6117 * @param subId The subscription that the MmTelFeature is associated with.
6118 * @param callback A callback with an integer containing the
6119 * {@link android.telephony.ims.feature.ImsFeature.ImsState} associated with the MmTelFeature.
6120 */
6121 @Override
6122 public void getImsMmTelFeatureState(int subId, IIntegerConsumer callback) {
6123 enforceReadPrivilegedPermission("getImsMmTelFeatureState");
Brad Ebingera2628302022-02-18 03:44:55 +00006124 if (!ImsManager.isImsSupportedOnDevice(mApp)) {
6125 throw new ServiceSpecificException(ImsException.CODE_ERROR_UNSUPPORTED_OPERATION,
6126 "IMS not available on device.");
6127 }
Brad Ebingerbc7dd582019-10-17 17:03:22 -07006128 final long token = Binder.clearCallingIdentity();
6129 try {
Brad Ebingera2628302022-02-18 03:44:55 +00006130 int slotId = getSlotIndex(subId);
6131 if (slotId <= SubscriptionManager.INVALID_SIM_SLOT_INDEX) {
6132 Log.w(LOG_TAG, "getImsMmTelFeatureState: called with an inactive subscription '"
6133 + subId + "'");
6134 throw new ServiceSpecificException(ImsException.CODE_ERROR_INVALID_SUBSCRIPTION);
6135 }
6136 verifyImsMmTelConfiguredOrThrow(slotId);
6137 ImsManager.getInstance(mApp, slotId).getImsServiceState(anInteger -> {
6138 try {
6139 callback.accept(anInteger == null ? ImsFeature.STATE_UNAVAILABLE : anInteger);
6140 } catch (RemoteException e) {
6141 Log.w(LOG_TAG, "getImsMmTelFeatureState: remote caller is no longer running. "
6142 + "Ignore");
6143 }
Brad Ebingerbc7dd582019-10-17 17:03:22 -07006144 });
Brad Ebinger919631e2021-06-02 17:46:35 -07006145 } catch (ImsException e) {
6146 throw new ServiceSpecificException(e.getCode());
Brad Ebingerbc7dd582019-10-17 17:03:22 -07006147 } finally {
6148 Binder.restoreCallingIdentity(token);
6149 }
6150 }
6151
Daniel Brightbb5840b2021-01-12 15:48:18 -08006152 /**
6153 * Sets the ims registration state on all valid {@link Phone}s.
6154 */
6155 public void setImsRegistrationState(final boolean registered) {
Wink Saville36469e72014-06-11 15:17:00 -07006156 enforceModifyPermission();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006157
6158 final long identity = Binder.clearCallingIdentity();
6159 try {
Daniel Brightbb5840b2021-01-12 15:48:18 -08006160 // NOTE: Before S, this method only set the default phone.
6161 for (final Phone phone : PhoneFactory.getPhones()) {
6162 if (SubscriptionManager.isValidSubscriptionId(phone.getSubId())) {
6163 phone.setImsRegistrationState(registered);
6164 }
6165 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006166 } finally {
6167 Binder.restoreCallingIdentity(identity);
6168 }
Wink Saville36469e72014-06-11 15:17:00 -07006169 }
6170
6171 /**
Stuart Scott54788802015-03-30 13:18:01 -07006172 * Set the network selection mode to automatic.
6173 *
6174 */
6175 @Override
6176 public void setNetworkSelectionModeAutomatic(int subId) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08006177 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
6178 mApp, subId, "setNetworkSelectionModeAutomatic");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006179
6180 final long identity = Binder.clearCallingIdentity();
6181 try {
shilufc958392020-01-20 11:36:01 -08006182 if (!isActiveSubscription(subId)) {
6183 return;
6184 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006185 if (DBG) log("setNetworkSelectionModeAutomatic: subId " + subId);
Rambo Wang0f050d82021-02-12 11:43:36 -08006186 sendRequest(CMD_SET_NETWORK_SELECTION_MODE_AUTOMATIC, null, subId,
6187 SET_NETWORK_SELECTION_MODE_AUTOMATIC_TIMEOUT_MS);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006188 } finally {
6189 Binder.restoreCallingIdentity(identity);
6190 }
Stuart Scott54788802015-03-30 13:18:01 -07006191 }
6192
Jack Yud10cdd42020-09-28 20:28:01 -07006193 /**
Pengquan Mengea84e042018-09-20 14:57:26 -07006194 * Ask the radio to connect to the input network and change selection mode to manual.
6195 *
6196 * @param subId the id of the subscription.
6197 * @param operatorInfo the operator information, included the PLMN, long name and short name of
6198 * the operator to attach to.
6199 * @param persistSelection whether the selection will persist until reboot. If true, only allows
6200 * attaching to the selected PLMN until reboot; otherwise, attach to the chosen PLMN and resume
6201 * normal network selection next time.
6202 * @return {@code true} on success; {@code true} on any failure.
Shishir Agrawal302c8692015-06-19 13:49:39 -07006203 */
6204 @Override
Pengquan Mengea84e042018-09-20 14:57:26 -07006205 public boolean setNetworkSelectionModeManual(
6206 int subId, OperatorInfo operatorInfo, boolean persistSelection) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08006207 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
6208 mApp, subId, "setNetworkSelectionModeManual");
Pengquan Menge92a50d2018-09-21 15:54:48 -07006209
Jack Yu285100e2022-12-02 22:48:35 -08006210 final long identity = Binder.clearCallingIdentity();
Pengquan Menge92a50d2018-09-21 15:54:48 -07006211 if (!isActiveSubscription(subId)) {
6212 return false;
6213 }
6214
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006215 try {
Pengquan Mengea84e042018-09-20 14:57:26 -07006216 ManualNetworkSelectionArgument arg = new ManualNetworkSelectionArgument(operatorInfo,
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006217 persistSelection);
Pengquan Mengea84e042018-09-20 14:57:26 -07006218 if (DBG) {
6219 log("setNetworkSelectionModeManual: subId: " + subId
6220 + " operator: " + operatorInfo);
6221 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006222 return (Boolean) sendRequest(CMD_SET_NETWORK_SELECTION_MODE_MANUAL, arg, subId);
6223 } finally {
6224 Binder.restoreCallingIdentity(identity);
6225 }
Shishir Agrawal302c8692015-06-19 13:49:39 -07006226 }
shilu84f6e8b2019-12-19 13:58:01 -08006227 /**
6228 * Get the manual network selection
6229 *
6230 * @param subId the id of the subscription.
6231 *
6232 * @return the previously saved user selected PLMN
6233 */
6234 @Override
6235 public String getManualNetworkSelectionPlmn(int subId) {
6236 TelephonyPermissions
Nathan Harold62c68512021-04-06 11:26:02 -07006237 .enforceCallingOrSelfReadPrecisePhoneStatePermissionOrCarrierPrivilege(
shilu84f6e8b2019-12-19 13:58:01 -08006238 mApp, subId, "getManualNetworkSelectionPlmn");
6239
6240 final long identity = Binder.clearCallingIdentity();
6241 try {
6242 if (!isActiveSubscription(subId)) {
shilufa1c2592020-03-10 10:59:43 -07006243 throw new IllegalArgumentException("Invalid Subscription Id: " + subId);
shilu84f6e8b2019-12-19 13:58:01 -08006244 }
6245
6246 final Phone phone = getPhone(subId);
6247 if (phone == null) {
shilufa1c2592020-03-10 10:59:43 -07006248 throw new IllegalArgumentException("Invalid Subscription Id: " + subId);
shilu84f6e8b2019-12-19 13:58:01 -08006249 }
6250 OperatorInfo networkSelection = phone.getSavedNetworkSelection();
6251 return TextUtils.isEmpty(networkSelection.getOperatorNumeric())
6252 ? phone.getManualNetworkSelectionPlmn() : networkSelection.getOperatorNumeric();
6253 } finally {
6254 Binder.restoreCallingIdentity(identity);
6255 }
6256 }
Shishir Agrawal302c8692015-06-19 13:49:39 -07006257
6258 /**
6259 * Scans for available networks.
6260 */
6261 @Override
Philip P. Moltmann3a2772a2019-10-04 08:15:00 -07006262 public CellNetworkScanResult getCellNetworkScanResults(int subId, String callingPackage,
6263 String callingFeatureId) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08006264 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
6265 mApp, subId, "getCellNetworkScanResults");
Hall Liuf19c44f2018-11-27 14:38:17 -08006266 LocationAccessPolicy.LocationPermissionResult locationResult =
6267 LocationAccessPolicy.checkLocationPermission(mApp,
6268 new LocationAccessPolicy.LocationPermissionQuery.Builder()
6269 .setCallingPackage(callingPackage)
Philip P. Moltmann3a2772a2019-10-04 08:15:00 -07006270 .setCallingFeatureId(callingFeatureId)
Hall Liuf19c44f2018-11-27 14:38:17 -08006271 .setCallingPid(Binder.getCallingPid())
6272 .setCallingUid(Binder.getCallingUid())
6273 .setMethod("getCellNetworkScanResults")
6274 .setMinSdkVersionForFine(Build.VERSION_CODES.Q)
Hall Liuc4a3e422020-05-26 17:18:03 -07006275 .setMinSdkVersionForCoarse(Build.VERSION_CODES.Q)
6276 .setMinSdkVersionForEnforcement(Build.VERSION_CODES.Q)
Hall Liuf19c44f2018-11-27 14:38:17 -08006277 .build());
6278 switch (locationResult) {
6279 case DENIED_HARD:
6280 throw new SecurityException("Not allowed to access scan results -- location");
6281 case DENIED_SOFT:
6282 return null;
6283 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006284
Pengquan Menga1bb6272018-09-06 09:59:22 -07006285 long identity = Binder.clearCallingIdentity();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006286 try {
6287 if (DBG) log("getCellNetworkScanResults: subId " + subId);
Pengquan Menga1bb6272018-09-06 09:59:22 -07006288 return (CellNetworkScanResult) sendRequest(
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006289 CMD_PERFORM_NETWORK_SCAN, null, subId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006290 } finally {
6291 Binder.restoreCallingIdentity(identity);
6292 }
Shishir Agrawal302c8692015-06-19 13:49:39 -07006293 }
6294
6295 /**
Shuo Qian4a594052020-01-23 11:59:30 -08006296 * Get the call forwarding info, given the call forwarding reason.
6297 */
6298 @Override
Hall Liu27d24262020-09-18 19:04:59 -07006299 public void getCallForwarding(int subId, int callForwardingReason,
6300 ICallForwardingInfoCallback callback) {
Shuo Qian4a594052020-01-23 11:59:30 -08006301 enforceReadPrivilegedPermission("getCallForwarding");
6302 long identity = Binder.clearCallingIdentity();
6303 try {
6304 if (DBG) {
6305 log("getCallForwarding: subId " + subId
6306 + " callForwardingReason" + callForwardingReason);
6307 }
Hall Liu27d24262020-09-18 19:04:59 -07006308
6309 Phone phone = getPhone(subId);
6310 if (phone == null) {
6311 try {
Hall Liu940c4ca2020-09-29 17:10:18 -07006312 callback.onError(
6313 TelephonyManager.CallForwardingInfoCallback.RESULT_ERROR_UNKNOWN);
Hall Liu27d24262020-09-18 19:04:59 -07006314 } catch (RemoteException e) {
6315 // ignore
6316 }
6317 return;
6318 }
6319
6320 Pair<Integer, TelephonyManager.CallForwardingInfoCallback> argument = Pair.create(
6321 callForwardingReason, new TelephonyManager.CallForwardingInfoCallback() {
6322 @Override
6323 public void onCallForwardingInfoAvailable(CallForwardingInfo info) {
6324 try {
6325 callback.onCallForwardingInfoAvailable(info);
6326 } catch (RemoteException e) {
6327 // ignore
6328 }
6329 }
6330
6331 @Override
6332 public void onError(int error) {
6333 try {
6334 callback.onError(error);
6335 } catch (RemoteException e) {
6336 // ignore
6337 }
6338 }
6339 });
6340 sendRequestAsync(CMD_GET_CALL_FORWARDING, argument, phone, null);
Shuo Qian4a594052020-01-23 11:59:30 -08006341 } finally {
6342 Binder.restoreCallingIdentity(identity);
6343 }
6344 }
6345
6346 /**
6347 * Sets the voice call forwarding info including status (enable/disable), call forwarding
6348 * reason, the number to forward, and the timeout before the forwarding is attempted.
6349 */
6350 @Override
Hall Liu27d24262020-09-18 19:04:59 -07006351 public void setCallForwarding(int subId, CallForwardingInfo callForwardingInfo,
6352 IIntegerConsumer callback) {
Shuo Qian4a594052020-01-23 11:59:30 -08006353 enforceModifyPermission();
6354 long identity = Binder.clearCallingIdentity();
6355 try {
6356 if (DBG) {
6357 log("setCallForwarding: subId " + subId
6358 + " callForwardingInfo" + callForwardingInfo);
6359 }
Hall Liu27d24262020-09-18 19:04:59 -07006360
6361 Phone phone = getPhone(subId);
6362 if (phone == null) {
6363 try {
Hall Liu940c4ca2020-09-29 17:10:18 -07006364 callback.accept(
6365 TelephonyManager.CallForwardingInfoCallback.RESULT_ERROR_UNKNOWN);
Hall Liu27d24262020-09-18 19:04:59 -07006366 } catch (RemoteException e) {
6367 // ignore
6368 }
6369 return;
6370 }
6371
6372 Pair<CallForwardingInfo, Consumer<Integer>> arguments = Pair.create(callForwardingInfo,
6373 FunctionalUtils.ignoreRemoteException(callback::accept));
6374
6375 sendRequestAsync(CMD_SET_CALL_FORWARDING, arguments, phone, null);
Shuo Qian4a594052020-01-23 11:59:30 -08006376 } finally {
6377 Binder.restoreCallingIdentity(identity);
6378 }
6379 }
6380
6381 /**
Hall Liu27d24262020-09-18 19:04:59 -07006382 * Get the call waiting status for a subId.
Shuo Qian4a594052020-01-23 11:59:30 -08006383 */
6384 @Override
Hall Liu27d24262020-09-18 19:04:59 -07006385 public void getCallWaitingStatus(int subId, IIntegerConsumer callback) {
SongFerngWang0e767992021-03-31 22:08:45 +08006386 enforceReadPrivilegedPermission("getCallWaitingStatus");
Shuo Qian4a594052020-01-23 11:59:30 -08006387 long identity = Binder.clearCallingIdentity();
6388 try {
Hall Liu27d24262020-09-18 19:04:59 -07006389 Phone phone = getPhone(subId);
6390 if (phone == null) {
6391 try {
6392 callback.accept(TelephonyManager.CALL_WAITING_STATUS_UNKNOWN_ERROR);
6393 } catch (RemoteException e) {
6394 // ignore
6395 }
6396 return;
6397 }
SongFerngWang0e767992021-03-31 22:08:45 +08006398 CarrierConfigManager configManager = new CarrierConfigManager(phone.getContext());
6399 PersistableBundle c = configManager.getConfigForSubId(subId);
6400 boolean requireUssd = c.getBoolean(
6401 CarrierConfigManager.KEY_USE_CALL_WAITING_USSD_BOOL, false);
Hall Liu27d24262020-09-18 19:04:59 -07006402
Shuo Qian4a594052020-01-23 11:59:30 -08006403 if (DBG) log("getCallWaitingStatus: subId " + subId);
SongFerngWang0e767992021-03-31 22:08:45 +08006404 if (requireUssd) {
6405 CarrierXmlParser carrierXmlParser = new CarrierXmlParser(phone.getContext(),
6406 getSubscriptionCarrierId(subId));
6407 String newUssdCommand = "";
6408 try {
6409 newUssdCommand = carrierXmlParser.getFeature(
6410 CarrierXmlParser.FEATURE_CALL_WAITING)
6411 .makeCommand(CarrierXmlParser.SsEntry.SSAction.QUERY, null);
6412 } catch (NullPointerException e) {
6413 loge("Failed to generate USSD number" + e);
6414 }
6415 ResultReceiver wrappedCallback = new CallWaitingUssdResultReceiver(
6416 mMainThreadHandler, callback, carrierXmlParser,
6417 CarrierXmlParser.SsEntry.SSAction.QUERY);
6418 final String ussdCommand = newUssdCommand;
6419 Executors.newSingleThreadExecutor().execute(() -> {
6420 handleUssdRequest(subId, ussdCommand, wrappedCallback);
6421 });
6422 } else {
6423 Consumer<Integer> argument = FunctionalUtils.ignoreRemoteException(
6424 callback::accept);
6425 sendRequestAsync(CMD_GET_CALL_WAITING, argument, phone, null);
6426 }
Shuo Qian4a594052020-01-23 11:59:30 -08006427 } finally {
6428 Binder.restoreCallingIdentity(identity);
6429 }
6430 }
6431
6432 /**
Hall Liu27d24262020-09-18 19:04:59 -07006433 * Sets whether call waiting is enabled for a given subId.
Shuo Qian4a594052020-01-23 11:59:30 -08006434 */
6435 @Override
Hall Liu27d24262020-09-18 19:04:59 -07006436 public void setCallWaitingStatus(int subId, boolean enable, IIntegerConsumer callback) {
Shuo Qian4a594052020-01-23 11:59:30 -08006437 enforceModifyPermission();
6438 long identity = Binder.clearCallingIdentity();
6439 try {
Hall Liu27d24262020-09-18 19:04:59 -07006440 if (DBG) log("setCallWaitingStatus: subId " + subId + " enable: " + enable);
6441
6442 Phone phone = getPhone(subId);
6443 if (phone == null) {
6444 try {
6445 callback.accept(TelephonyManager.CALL_WAITING_STATUS_UNKNOWN_ERROR);
6446 } catch (RemoteException e) {
6447 // ignore
6448 }
6449 return;
6450 }
6451
SongFerngWang0e767992021-03-31 22:08:45 +08006452 CarrierConfigManager configManager = new CarrierConfigManager(phone.getContext());
6453 PersistableBundle c = configManager.getConfigForSubId(subId);
6454 boolean requireUssd = c.getBoolean(
6455 CarrierConfigManager.KEY_USE_CALL_WAITING_USSD_BOOL, false);
Hall Liu27d24262020-09-18 19:04:59 -07006456
SongFerngWang0e767992021-03-31 22:08:45 +08006457 if (DBG) log("getCallWaitingStatus: subId " + subId);
6458 if (requireUssd) {
6459 CarrierXmlParser carrierXmlParser = new CarrierXmlParser(phone.getContext(),
6460 getSubscriptionCarrierId(subId));
6461 CarrierXmlParser.SsEntry.SSAction ssAction =
6462 enable ? CarrierXmlParser.SsEntry.SSAction.UPDATE_ACTIVATE
6463 : CarrierXmlParser.SsEntry.SSAction.UPDATE_DEACTIVATE;
6464 String newUssdCommand = "";
6465 try {
6466 newUssdCommand = carrierXmlParser.getFeature(
6467 CarrierXmlParser.FEATURE_CALL_WAITING)
6468 .makeCommand(ssAction, null);
6469 } catch (NullPointerException e) {
6470 loge("Failed to generate USSD number" + e);
6471 }
6472 ResultReceiver wrappedCallback = new CallWaitingUssdResultReceiver(
6473 mMainThreadHandler, callback, carrierXmlParser, ssAction);
6474 final String ussdCommand = newUssdCommand;
6475 Executors.newSingleThreadExecutor().execute(() -> {
6476 handleUssdRequest(subId, ussdCommand, wrappedCallback);
6477 });
6478 } else {
6479 Pair<Boolean, Consumer<Integer>> arguments = Pair.create(enable,
6480 FunctionalUtils.ignoreRemoteException(callback::accept));
6481
6482 sendRequestAsync(CMD_SET_CALL_WAITING, arguments, phone, null);
6483 }
Shuo Qian4a594052020-01-23 11:59:30 -08006484 } finally {
6485 Binder.restoreCallingIdentity(identity);
6486 }
6487 }
6488
6489 /**
yinxub1bed742017-04-17 11:45:04 -07006490 * Starts a new network scan and returns the id of this scan.
yinxu504e1392017-04-12 16:03:22 -07006491 *
yinxub1bed742017-04-17 11:45:04 -07006492 * @param subId id of the subscription
Sooraj Sasindran2250dc02021-11-10 16:42:01 -08006493 * @param renounceFineLocationAccess Set this to true if the caller would not like to receive
6494 * location related information which will be sent if the caller already possess
6495 * {@android.Manifest.permission.ACCESS_FINE_LOCATION} and do not renounce the permission
yinxub1bed742017-04-17 11:45:04 -07006496 * @param request contains the radio access networks with bands/channels to scan
6497 * @param messenger callback messenger for scan results or errors
6498 * @param binder for the purpose of auto clean when the user thread crashes
yinxu504e1392017-04-12 16:03:22 -07006499 * @return the id of the requested scan which can be used to stop the scan.
6500 */
6501 @Override
Sooraj Sasindran2250dc02021-11-10 16:42:01 -08006502 public int requestNetworkScan(int subId, boolean renounceFineLocationAccess,
6503 NetworkScanRequest request, Messenger messenger,
Philip P. Moltmann3a2772a2019-10-04 08:15:00 -07006504 IBinder binder, String callingPackage, String callingFeatureId) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08006505 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
6506 mApp, subId, "requestNetworkScan");
Hall Liuf19c44f2018-11-27 14:38:17 -08006507 LocationAccessPolicy.LocationPermissionResult locationResult =
Sooraj Sasindran2250dc02021-11-10 16:42:01 -08006508 LocationAccessPolicy.LocationPermissionResult.DENIED_HARD;
6509 if (!renounceFineLocationAccess) {
6510 locationResult = LocationAccessPolicy.checkLocationPermission(mApp,
6511 new LocationAccessPolicy.LocationPermissionQuery.Builder()
6512 .setCallingPackage(callingPackage)
6513 .setCallingFeatureId(callingFeatureId)
6514 .setCallingPid(Binder.getCallingPid())
6515 .setCallingUid(Binder.getCallingUid())
6516 .setMethod("requestNetworkScan")
6517 .setMinSdkVersionForFine(Build.VERSION_CODES.Q)
6518 .setMinSdkVersionForCoarse(Build.VERSION_CODES.Q)
6519 .setMinSdkVersionForEnforcement(Build.VERSION_CODES.Q)
6520 .build());
6521 }
Hall Liub2ac8ef2019-02-28 15:56:23 -08006522 if (locationResult != LocationAccessPolicy.LocationPermissionResult.ALLOWED) {
Nathan Harold1c11dba2020-09-22 17:54:53 -07006523 SecurityException e = checkNetworkRequestForSanitizedLocationAccess(
6524 request, subId, callingPackage);
Hall Liub2ac8ef2019-02-28 15:56:23 -08006525 if (e != null) {
6526 if (locationResult == LocationAccessPolicy.LocationPermissionResult.DENIED_HARD) {
6527 throw e;
6528 } else {
Hall Liu0e5abaf2019-04-04 01:25:30 -07006529 loge(e.getMessage());
Hall Liub2ac8ef2019-02-28 15:56:23 -08006530 return TelephonyScanManager.INVALID_SCAN_ID;
6531 }
6532 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006533 }
Hall Liu912dfd32019-04-25 14:02:26 -07006534 int callingUid = Binder.getCallingUid();
6535 int callingPid = Binder.getCallingPid();
Ying Xu94a46582019-04-18 17:14:56 -07006536 final long identity = Binder.clearCallingIdentity();
6537 try {
6538 return mNetworkScanRequestTracker.startNetworkScan(
Sooraj Sasindran2250dc02021-11-10 16:42:01 -08006539 renounceFineLocationAccess, request, messenger, binder, getPhone(subId),
Hall Liu912dfd32019-04-25 14:02:26 -07006540 callingUid, callingPid, callingPackage);
Ying Xu94a46582019-04-18 17:14:56 -07006541 } finally {
6542 Binder.restoreCallingIdentity(identity);
6543 }
yinxu504e1392017-04-12 16:03:22 -07006544 }
6545
Hall Liub2ac8ef2019-02-28 15:56:23 -08006546 private SecurityException checkNetworkRequestForSanitizedLocationAccess(
Nathan Harold1c11dba2020-09-22 17:54:53 -07006547 NetworkScanRequest request, int subId, String callingPackage) {
Rambo Wang3dee30a2022-10-20 16:52:29 +00006548 boolean hasCarrierPriv;
6549 final long identity = Binder.clearCallingIdentity();
6550 try {
6551 hasCarrierPriv = checkCarrierPrivilegesForPackage(subId, callingPackage)
6552 == TelephonyManager.CARRIER_PRIVILEGE_STATUS_HAS_ACCESS;
6553 } finally {
6554 Binder.restoreCallingIdentity(identity);
6555 }
Hall Liu558027f2019-05-15 19:14:05 -07006556 boolean hasNetworkScanPermission =
6557 mApp.checkCallingOrSelfPermission(android.Manifest.permission.NETWORK_SCAN)
6558 == PERMISSION_GRANTED;
6559
6560 if (!hasCarrierPriv && !hasNetworkScanPermission) {
6561 return new SecurityException("permission.NETWORK_SCAN or carrier privileges is needed"
6562 + " for network scans without location access.");
Hall Liub2ac8ef2019-02-28 15:56:23 -08006563 }
6564
6565 if (request.getSpecifiers() != null && request.getSpecifiers().length > 0) {
6566 for (RadioAccessSpecifier ras : request.getSpecifiers()) {
Hall Liub2ac8ef2019-02-28 15:56:23 -08006567 if (ras.getChannels() != null && ras.getChannels().length > 0) {
6568 return new SecurityException("Specific channels must not be"
6569 + " scanned without location access.");
6570 }
6571 }
6572 }
6573
Hall Liub2ac8ef2019-02-28 15:56:23 -08006574 return null;
6575 }
6576
yinxu504e1392017-04-12 16:03:22 -07006577 /**
6578 * Stops an existing network scan with the given scanId.
yinxub1bed742017-04-17 11:45:04 -07006579 *
6580 * @param subId id of the subscription
6581 * @param scanId id of the scan that needs to be stopped
yinxu504e1392017-04-12 16:03:22 -07006582 */
6583 @Override
6584 public void stopNetworkScan(int subId, int scanId) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08006585 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
6586 mApp, subId, "stopNetworkScan");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006587
Hall Liu912dfd32019-04-25 14:02:26 -07006588 int callingUid = Binder.getCallingUid();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006589 final long identity = Binder.clearCallingIdentity();
6590 try {
Hall Liu912dfd32019-04-25 14:02:26 -07006591 mNetworkScanRequestTracker.stopNetworkScan(scanId, callingUid);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006592 } finally {
6593 Binder.restoreCallingIdentity(identity);
6594 }
yinxu504e1392017-04-12 16:03:22 -07006595 }
6596
6597 /**
SongFerngWang3ef3e072020-12-21 16:41:52 +08006598 * Get the allowed network types bitmask.
Junda Liu84d15a22014-07-02 11:21:04 -07006599 *
SongFerngWang3ef3e072020-12-21 16:41:52 +08006600 * @return the allowed network types bitmask, defined in RILConstants.java.
Junda Liu84d15a22014-07-02 11:21:04 -07006601 */
6602 @Override
SongFerngWang3ef3e072020-12-21 16:41:52 +08006603 public int getAllowedNetworkTypesBitmask(int subId) {
Pengquan Menga4009cb2018-12-20 11:00:24 -08006604 TelephonyPermissions
Nathan Harold62c68512021-04-06 11:26:02 -07006605 .enforceCallingOrSelfReadPrivilegedPhoneStatePermissionOrCarrierPrivilege(
SongFerngWang3ef3e072020-12-21 16:41:52 +08006606 mApp, subId, "getAllowedNetworkTypesBitmask");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006607
6608 final long identity = Binder.clearCallingIdentity();
6609 try {
SongFerngWang3ef3e072020-12-21 16:41:52 +08006610 if (DBG) log("getAllowedNetworkTypesBitmask");
6611 int[] result = (int[]) sendRequest(CMD_GET_ALLOWED_NETWORK_TYPES_BITMASK, null, subId);
6612 int networkTypesBitmask = (result != null ? result[0] : -1);
6613 if (DBG) log("getAllowedNetworkTypesBitmask: " + networkTypesBitmask);
6614 return networkTypesBitmask;
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006615 } finally {
6616 Binder.restoreCallingIdentity(identity);
6617 }
Jake Hamby7c27be32014-03-03 13:25:59 -08006618 }
6619
6620 /**
Sooraj Sasindranc46dfbd2020-06-03 01:06:00 -07006621 * Get the allowed network types for certain reason.
6622 *
6623 * @param subId the id of the subscription.
6624 * @param reason the reason the allowed network type change is taking place
6625 * @return the allowed network types.
6626 */
6627 @Override
6628 public long getAllowedNetworkTypesForReason(int subId,
6629 @TelephonyManager.AllowedNetworkTypesReason int reason) {
Nathan Harold62c68512021-04-06 11:26:02 -07006630 TelephonyPermissions.enforceCallingOrSelfReadPrecisePhoneStatePermissionOrCarrierPrivilege(
SongFerngWang8c6e82e2021-03-02 22:09:29 +08006631 mApp, subId, "getAllowedNetworkTypesForReason");
Sooraj Sasindranc46dfbd2020-06-03 01:06:00 -07006632 final long identity = Binder.clearCallingIdentity();
6633 try {
6634 return getPhoneFromSubId(subId).getAllowedNetworkTypes(reason);
6635 } finally {
6636 Binder.restoreCallingIdentity(identity);
6637 }
6638 }
6639
6640 /**
Sooraj Sasindran37444802020-08-11 10:40:43 -07006641 * Enable/Disable E-UTRA-NR Dual Connectivity
6642 * @param subId subscription id of the sim card
6643 * @param nrDualConnectivityState expected NR dual connectivity state
6644 * This can be passed following states
6645 * <ol>
6646 * <li>Enable NR dual connectivity {@link TelephonyManager#NR_DUAL_CONNECTIVITY_ENABLE}
6647 * <li>Disable NR dual connectivity {@link TelephonyManager#NR_DUAL_CONNECTIVITY_DISABLE}
6648 * <li>Disable NR dual connectivity and force secondary cell to be released
6649 * {@link TelephonyManager#NR_DUAL_CONNECTIVITY_DISABLE_IMMEDIATE}
6650 * </ol>
6651 * @return operation result.
6652 */
6653 @Override
6654 public int setNrDualConnectivityState(int subId,
6655 @TelephonyManager.NrDualConnectivityState int nrDualConnectivityState) {
6656 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
6657 mApp, subId, "enableNRDualConnectivity");
Sooraj Sasindran0e4e00a2021-03-16 18:02:32 -07006658 if (!isRadioInterfaceCapabilitySupported(
Sooraj Sasindrandfd595b2021-03-11 17:38:13 -08006659 TelephonyManager.CAPABILITY_NR_DUAL_CONNECTIVITY_CONFIGURATION_AVAILABLE)) {
6660 return TelephonyManager.ENABLE_NR_DUAL_CONNECTIVITY_NOT_SUPPORTED;
6661 }
6662
Sooraj Sasindran37444802020-08-11 10:40:43 -07006663 WorkSource workSource = getWorkSource(Binder.getCallingUid());
6664 final long identity = Binder.clearCallingIdentity();
6665 try {
6666 int result = (int) sendRequest(CMD_ENABLE_NR_DUAL_CONNECTIVITY,
6667 nrDualConnectivityState, subId,
6668 workSource);
6669 if (DBG) log("enableNRDualConnectivity result: " + result);
6670 return result;
6671 } finally {
6672 Binder.restoreCallingIdentity(identity);
6673 }
6674 }
6675
6676 /**
6677 * Is E-UTRA-NR Dual Connectivity enabled
6678 * @return true if dual connectivity is enabled else false
6679 */
6680 @Override
6681 public boolean isNrDualConnectivityEnabled(int subId) {
6682 TelephonyPermissions
Nathan Harold62c68512021-04-06 11:26:02 -07006683 .enforceCallingOrSelfReadPrivilegedPhoneStatePermissionOrCarrierPrivilege(
Sooraj Sasindran37444802020-08-11 10:40:43 -07006684 mApp, subId, "isNRDualConnectivityEnabled");
Sooraj Sasindran0e4e00a2021-03-16 18:02:32 -07006685 if (!isRadioInterfaceCapabilitySupported(
Sooraj Sasindrandfd595b2021-03-11 17:38:13 -08006686 TelephonyManager.CAPABILITY_NR_DUAL_CONNECTIVITY_CONFIGURATION_AVAILABLE)) {
6687 return false;
6688 }
Sooraj Sasindran37444802020-08-11 10:40:43 -07006689 WorkSource workSource = getWorkSource(Binder.getCallingUid());
6690 final long identity = Binder.clearCallingIdentity();
6691 try {
6692 boolean isEnabled = (boolean) sendRequest(CMD_IS_NR_DUAL_CONNECTIVITY_ENABLED,
6693 null, subId, workSource);
6694 if (DBG) log("isNRDualConnectivityEnabled: " + isEnabled);
6695 return isEnabled;
6696 } finally {
6697 Binder.restoreCallingIdentity(identity);
6698 }
6699 }
6700
6701 /**
Sooraj Sasindranc46dfbd2020-06-03 01:06:00 -07006702 * Set the allowed network types of the device and
6703 * provide the reason triggering the allowed network change.
6704 *
6705 * @param subId the id of the subscription.
6706 * @param reason the reason the allowed network type change is taking place
6707 * @param allowedNetworkTypes the allowed network types.
6708 * @return true on success; false on any failure.
6709 */
6710 @Override
6711 public boolean setAllowedNetworkTypesForReason(int subId,
SongFerngWang3ef3e072020-12-21 16:41:52 +08006712 @TelephonyManager.AllowedNetworkTypesReason int reason,
6713 @TelephonyManager.NetworkTypeBitMask long allowedNetworkTypes) {
Sooraj Sasindranc46dfbd2020-06-03 01:06:00 -07006714 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
6715 mApp, subId, "setAllowedNetworkTypesForReason");
Gil Cukierman1d3d3752022-10-03 21:31:33 +00006716 // If the caller only has carrier privileges, then they should not be able to override
6717 // any network types which were set for security reasons.
6718 if (mApp.checkCallingOrSelfPermission(Manifest.permission.MODIFY_PHONE_STATE)
6719 != PERMISSION_GRANTED
6720 && (reason == TelephonyManager.ALLOWED_NETWORK_TYPES_REASON_ENABLE_2G
6721 || reason == TelephonyManager.ALLOWED_NETWORK_TYPES_REASON_USER_RESTRICTIONS)) {
6722 throw new SecurityException(
6723 "setAllowedNetworkTypesForReason cannot be called with carrier privileges for"
6724 + " reason "
6725 + reason);
6726 }
SongFerngWang3ef3e072020-12-21 16:41:52 +08006727 if (!TelephonyManager.isValidAllowedNetworkTypesReason(reason)) {
SongFerngWang7ffc2732021-04-15 19:46:33 +08006728 loge("setAllowedNetworkTypesForReason: Invalid allowed network type reason: " + reason);
6729 return false;
6730 }
6731 if (!SubscriptionManager.isUsableSubscriptionId(subId)) {
6732 loge("setAllowedNetworkTypesForReason: Invalid subscriptionId:" + subId);
SongFerngWang3ef3e072020-12-21 16:41:52 +08006733 return false;
6734 }
6735
Aishwarya Mallampati32336e82023-01-04 23:02:56 +00006736 log("setAllowedNetworkTypesForReason: " + reason + " value: "
SongFerngWang8c6e82e2021-03-02 22:09:29 +08006737 + TelephonyManager.convertNetworkTypeBitmaskToString(allowedNetworkTypes));
6738
6739
Aishwarya Mallampati32336e82023-01-04 23:02:56 +00006740 if (allowedNetworkTypes == getPhoneFromSubId(subId).getAllowedNetworkTypes(reason)) {
SongFerngWang8c6e82e2021-03-02 22:09:29 +08006741 log("setAllowedNetworkTypesForReason: " + reason + "does not change value");
6742 return true;
SongFerngWang3ef3e072020-12-21 16:41:52 +08006743 }
SongFerngWang8c6e82e2021-03-02 22:09:29 +08006744
Sooraj Sasindranc46dfbd2020-06-03 01:06:00 -07006745 final long identity = Binder.clearCallingIdentity();
6746 try {
SongFerngWang3ef3e072020-12-21 16:41:52 +08006747 Boolean success = (Boolean) sendRequest(
6748 CMD_SET_ALLOWED_NETWORK_TYPES_FOR_REASON,
6749 new Pair<Integer, Long>(reason, allowedNetworkTypes), subId);
6750
6751 if (DBG) log("setAllowedNetworkTypesForReason: " + (success ? "ok" : "fail"));
6752 return success;
Sooraj Sasindranc46dfbd2020-06-03 01:06:00 -07006753 } finally {
6754 Binder.restoreCallingIdentity(identity);
6755 }
6756 }
6757
6758 /**
Miaoa84611c2019-03-15 09:21:10 +08006759 * Check whether DUN APN is required for tethering with subId.
Junda Liu475951f2014-11-07 16:45:03 -08006760 *
Miaoa84611c2019-03-15 09:21:10 +08006761 * @param subId the id of the subscription to require tethering.
Amit Mahajanfe58cdf2017-07-11 12:01:53 -07006762 * @return {@code true} if DUN APN is required for tethering.
Junda Liu475951f2014-11-07 16:45:03 -08006763 * @hide
6764 */
6765 @Override
SongFerngWangf08d8122019-11-15 14:58:44 +08006766 public boolean isTetheringApnRequiredForSubscriber(int subId) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08006767 enforceModifyPermission();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006768 final long identity = Binder.clearCallingIdentity();
Miaoa84611c2019-03-15 09:21:10 +08006769 final Phone phone = getPhone(subId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006770 try {
Miaoa84611c2019-03-15 09:21:10 +08006771 if (phone != null) {
6772 return phone.hasMatchedTetherApnSetting();
6773 } else {
6774 return false;
6775 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006776 } finally {
6777 Binder.restoreCallingIdentity(identity);
Junda Liu475951f2014-11-07 16:45:03 -08006778 }
Junda Liu475951f2014-11-07 16:45:03 -08006779 }
6780
6781 /**
Malcolm Chen964682d2017-11-28 16:20:07 -08006782 * Get the user enabled state of Mobile Data.
6783 *
6784 * TODO: remove and use isUserDataEnabled.
6785 * This can't be removed now because some vendor codes
6786 * calls through ITelephony directly while they should
6787 * use TelephonyManager.
6788 *
6789 * @return true on enabled
6790 */
6791 @Override
6792 public boolean getDataEnabled(int subId) {
6793 return isUserDataEnabled(subId);
6794 }
6795
6796 /**
6797 * Get whether mobile data is enabled per user setting.
6798 *
6799 * There are other factors deciding whether mobile data is actually enabled, but they are
6800 * not considered here. See {@link #isDataEnabled(int)} for more details.
Robert Greenwalt646120a2014-05-23 11:54:03 -07006801 *
Sooraj Sasindran5d051bf2021-11-05 13:14:15 -07006802 * Accepts either READ_BASIC_PHONE_STATE, ACCESS_NETWORK_STATE, MODIFY_PHONE_STATE
6803 * or carrier privileges.
Robert Greenwalted86e582014-05-21 20:03:20 -07006804 *
6805 * @return {@code true} if data is enabled else {@code false}
6806 */
6807 @Override
Malcolm Chen964682d2017-11-28 16:20:07 -08006808 public boolean isUserDataEnabled(int subId) {
Sooraj Sasindran5d051bf2021-11-05 13:14:15 -07006809 String functionName = "isUserDataEnabled";
Robert Greenwalt646120a2014-05-23 11:54:03 -07006810 try {
Sooraj Sasindran5d051bf2021-11-05 13:14:15 -07006811 try {
6812 mApp.enforceCallingOrSelfPermission(permission.READ_BASIC_PHONE_STATE,
6813 functionName);
Sooraj Sasindran12545cc2022-08-22 14:30:25 -07006814 } catch (SecurityException e) {
Sooraj Sasindran5d051bf2021-11-05 13:14:15 -07006815 mApp.enforceCallingOrSelfPermission(permission.ACCESS_NETWORK_STATE, functionName);
6816 }
Sooraj Sasindran12545cc2022-08-22 14:30:25 -07006817 } catch (SecurityException e) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08006818 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
Sooraj Sasindran5d051bf2021-11-05 13:14:15 -07006819 mApp, subId, functionName);
6820
Robert Greenwalt646120a2014-05-23 11:54:03 -07006821 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006822
6823 final long identity = Binder.clearCallingIdentity();
6824 try {
Jack Yu285100e2022-12-02 22:48:35 -08006825 int phoneId = SubscriptionManager.getPhoneId(subId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006826 if (DBG) log("isUserDataEnabled: subId=" + subId + " phoneId=" + phoneId);
6827 Phone phone = PhoneFactory.getPhone(phoneId);
6828 if (phone != null) {
6829 boolean retVal = phone.isUserDataEnabled();
6830 if (DBG) log("isUserDataEnabled: subId=" + subId + " retVal=" + retVal);
6831 return retVal;
6832 } else {
6833 if (DBG) loge("isUserDataEnabled: no phone subId=" + subId + " retVal=false");
6834 return false;
6835 }
6836 } finally {
6837 Binder.restoreCallingIdentity(identity);
Malcolm Chen964682d2017-11-28 16:20:07 -08006838 }
6839 }
6840
6841 /**
Shuo Qian8ee4e882020-01-08 14:30:06 -08006842 * Checks if the device is capable of mobile data by considering whether whether the
6843 * user has enabled mobile data, whether the carrier has enabled mobile data, and
6844 * whether the network policy allows data connections.
Malcolm Chen964682d2017-11-28 16:20:07 -08006845 *
Shuo Qian8ee4e882020-01-08 14:30:06 -08006846 * @return {@code true} if the overall data connection is capable; {@code false} if not.
Malcolm Chen964682d2017-11-28 16:20:07 -08006847 */
6848 @Override
6849 public boolean isDataEnabled(int subId) {
Sooraj Sasindran5d051bf2021-11-05 13:14:15 -07006850 String functionName = "isDataEnabled";
Sooraj Sasindran95c07a92020-07-15 01:35:24 -07006851 try {
6852 try {
6853 mApp.enforceCallingOrSelfPermission(
6854 android.Manifest.permission.ACCESS_NETWORK_STATE,
Sooraj Sasindran5d051bf2021-11-05 13:14:15 -07006855 functionName);
Sooraj Sasindran12545cc2022-08-22 14:30:25 -07006856 } catch (SecurityException e) {
Sooraj Sasindran5d051bf2021-11-05 13:14:15 -07006857 try {
6858 mApp.enforceCallingOrSelfPermission(
6859 android.Manifest.permission.READ_PHONE_STATE,
6860 functionName);
Sooraj Sasindran12545cc2022-08-22 14:30:25 -07006861 } catch (SecurityException e2) {
Sooraj Sasindran5d051bf2021-11-05 13:14:15 -07006862 mApp.enforceCallingOrSelfPermission(
6863 permission.READ_BASIC_PHONE_STATE, functionName);
6864 }
Sooraj Sasindran95c07a92020-07-15 01:35:24 -07006865 }
Sooraj Sasindran12545cc2022-08-22 14:30:25 -07006866 } catch (SecurityException e) {
Sooraj Sasindran5d051bf2021-11-05 13:14:15 -07006867 enforceReadPrivilegedPermission(functionName);
Sooraj Sasindran95c07a92020-07-15 01:35:24 -07006868 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006869
6870 final long identity = Binder.clearCallingIdentity();
6871 try {
Jack Yu285100e2022-12-02 22:48:35 -08006872 int phoneId = SubscriptionManager.getPhoneId(subId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006873 Phone phone = PhoneFactory.getPhone(phoneId);
6874 if (phone != null) {
Sarah Chine04784a2022-10-31 20:32:34 -07006875 boolean retVal = phone.getDataSettingsManager().isDataEnabled();
Jack Yu4ad64e52021-12-03 14:23:53 -08006876 if (DBG) log("isDataEnabled: " + retVal + ", subId=" + subId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006877 return retVal;
6878 } else {
6879 if (DBG) loge("isDataEnabled: no phone subId=" + subId + " retVal=false");
6880 return false;
6881 }
6882 } finally {
6883 Binder.restoreCallingIdentity(identity);
Wink Savillee7353bb2014-12-05 14:21:41 -08006884 }
Robert Greenwalted86e582014-05-21 20:03:20 -07006885 }
Shishir Agrawal60f9c952014-06-23 12:00:43 -07006886
Sooraj Sasindranaf1b5132020-05-05 21:06:20 +00006887 /**
6888 * Check if data is enabled for a specific reason
6889 * @param subId Subscription index
6890 * @param reason the reason the data enable change is taking place
6891 * @return {@code true} if the overall data is enabled; {@code false} if not.
6892 */
6893 @Override
Sooraj Sasindran95c07a92020-07-15 01:35:24 -07006894 public boolean isDataEnabledForReason(int subId,
Sooraj Sasindranaf1b5132020-05-05 21:06:20 +00006895 @TelephonyManager.DataEnabledReason int reason) {
Sooraj Sasindran5d051bf2021-11-05 13:14:15 -07006896 String functionName = "isDataEnabledForReason";
Sooraj Sasindranaf1b5132020-05-05 21:06:20 +00006897 try {
Sooraj Sasindran5d051bf2021-11-05 13:14:15 -07006898 try {
6899 mApp.enforceCallingOrSelfPermission(
6900 android.Manifest.permission.ACCESS_NETWORK_STATE,
6901 functionName);
Sooraj Sasindran12545cc2022-08-22 14:30:25 -07006902 } catch (SecurityException e) {
Sooraj Sasindran5d051bf2021-11-05 13:14:15 -07006903 mApp.enforceCallingOrSelfPermission(permission.READ_BASIC_PHONE_STATE,
6904 functionName);
6905 }
Sooraj Sasindran12545cc2022-08-22 14:30:25 -07006906 } catch (SecurityException e) {
Sooraj Sasindran0d909a02021-11-08 12:01:16 -08006907 try {
6908 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.READ_PHONE_STATE,
Sooraj Sasindran5d051bf2021-11-05 13:14:15 -07006909 functionName);
Sooraj Sasindran12545cc2022-08-22 14:30:25 -07006910 } catch (SecurityException e2) {
Sooraj Sasindran0d909a02021-11-08 12:01:16 -08006911 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
Sooraj Sasindran5d051bf2021-11-05 13:14:15 -07006912 mApp, subId, functionName);
Sooraj Sasindran0d909a02021-11-08 12:01:16 -08006913 }
Sooraj Sasindranaf1b5132020-05-05 21:06:20 +00006914 }
6915
6916
6917 final long identity = Binder.clearCallingIdentity();
6918 try {
Jack Yu285100e2022-12-02 22:48:35 -08006919 int phoneId = SubscriptionManager.getPhoneId(subId);
Sooraj Sasindranaf1b5132020-05-05 21:06:20 +00006920 if (DBG) {
Sooraj Sasindran95c07a92020-07-15 01:35:24 -07006921 log("isDataEnabledForReason: subId=" + subId + " phoneId=" + phoneId
Sooraj Sasindranaf1b5132020-05-05 21:06:20 +00006922 + " reason=" + reason);
6923 }
6924 Phone phone = PhoneFactory.getPhone(phoneId);
6925 if (phone != null) {
6926 boolean retVal;
Jack Yu7968c6d2022-07-31 00:43:21 -07006927 retVal = phone.getDataSettingsManager().isDataEnabledForReason(reason);
Sooraj Sasindran95c07a92020-07-15 01:35:24 -07006928 if (DBG) log("isDataEnabledForReason: retVal=" + retVal);
Sooraj Sasindranaf1b5132020-05-05 21:06:20 +00006929 return retVal;
6930 } else {
6931 if (DBG) {
Sooraj Sasindran95c07a92020-07-15 01:35:24 -07006932 loge("isDataEnabledForReason: no phone subId="
Sooraj Sasindranaf1b5132020-05-05 21:06:20 +00006933 + subId + " retVal=false");
6934 }
6935 return false;
6936 }
6937 } finally {
6938 Binder.restoreCallingIdentity(identity);
6939 }
6940 }
6941
Shishir Agrawal60f9c952014-06-23 12:00:43 -07006942 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08006943 public int getCarrierPrivilegeStatus(int subId) {
Hunter Knepshieldcad7f0b2021-12-13 15:08:35 -08006944 // No permission needed; this only lets the caller inspect their own status.
6945 return getCarrierPrivilegeStatusForUidWithPermission(subId, Binder.getCallingUid());
Shishir Agrawal60f9c952014-06-23 12:00:43 -07006946 }
Junda Liu29340342014-07-10 15:23:27 -07006947
6948 @Override
Jeff Davidson7e17e312018-02-13 18:17:36 -08006949 public int getCarrierPrivilegeStatusForUid(int subId, int uid) {
Shuo Qian2c0ae432019-12-05 11:40:37 -08006950 enforceReadPrivilegedPermission("getCarrierPrivilegeStatusForUid");
Hunter Knepshieldcad7f0b2021-12-13 15:08:35 -08006951 return getCarrierPrivilegeStatusForUidWithPermission(subId, uid);
6952 }
6953
6954 private int getCarrierPrivilegeStatusForUidWithPermission(int subId, int uid) {
6955 Phone phone = getPhone(subId);
Jeff Davidson7e17e312018-02-13 18:17:36 -08006956 if (phone == null) {
Taesu Leef8fbed92019-10-07 18:47:02 +09006957 loge("getCarrierPrivilegeStatusForUid: Invalid subId");
Jeff Davidson7e17e312018-02-13 18:17:36 -08006958 return TelephonyManager.CARRIER_PRIVILEGE_STATUS_NO_ACCESS;
6959 }
Hunter Knepshieldcad7f0b2021-12-13 15:08:35 -08006960 CarrierPrivilegesTracker cpt = phone.getCarrierPrivilegesTracker();
6961 if (cpt == null) {
6962 loge("getCarrierPrivilegeStatusForUid: No CarrierPrivilegesTracker");
Jeff Davidson7e17e312018-02-13 18:17:36 -08006963 return TelephonyManager.CARRIER_PRIVILEGE_STATUS_RULES_NOT_LOADED;
6964 }
Hunter Knepshieldcad7f0b2021-12-13 15:08:35 -08006965 return cpt.getCarrierPrivilegeStatusForUid(uid);
Jeff Davidson7e17e312018-02-13 18:17:36 -08006966 }
6967
6968 @Override
chen xuf7e9fe82019-05-09 19:31:02 -07006969 public int checkCarrierPrivilegesForPackage(int subId, String pkgName) {
Nazanin1adf4562021-03-29 15:35:30 -07006970 enforceReadPrivilegedPermission("checkCarrierPrivilegesForPackage");
chen xuf7e9fe82019-05-09 19:31:02 -07006971 if (TextUtils.isEmpty(pkgName)) {
Junda Liu317d70b2016-03-08 09:33:53 -08006972 return TelephonyManager.CARRIER_PRIVILEGE_STATUS_NO_ACCESS;
chen xuf7e9fe82019-05-09 19:31:02 -07006973 }
Hunter Knepshieldcad7f0b2021-12-13 15:08:35 -08006974 Phone phone = getPhone(subId);
6975 if (phone == null) {
6976 loge("checkCarrierPrivilegesForPackage: Invalid subId");
6977 return TelephonyManager.CARRIER_PRIVILEGE_STATUS_NO_ACCESS;
6978 }
6979 CarrierPrivilegesTracker cpt = phone.getCarrierPrivilegesTracker();
6980 if (cpt == null) {
6981 loge("checkCarrierPrivilegesForPackage: No CarrierPrivilegesTracker");
Shishir Agrawaleb8771e2014-07-22 11:24:08 -07006982 return TelephonyManager.CARRIER_PRIVILEGE_STATUS_RULES_NOT_LOADED;
6983 }
Hunter Knepshieldcad7f0b2021-12-13 15:08:35 -08006984 return cpt.getCarrierPrivilegeStatusForPackage(pkgName);
Zach Johnson50ecba32015-05-19 00:24:21 -07006985 }
6986
6987 @Override
6988 public int checkCarrierPrivilegesForPackageAnyPhone(String pkgName) {
Hunter Knepshieldcad7f0b2021-12-13 15:08:35 -08006989 enforceReadPrivilegedPermission("checkCarrierPrivilegesForPackageAnyPhone");
Rambo Wange7209ce2022-02-23 13:41:02 -08006990 return checkCarrierPrivilegesForPackageAnyPhoneWithPermission(pkgName);
6991 }
6992
6993 private int checkCarrierPrivilegesForPackageAnyPhoneWithPermission(String pkgName) {
Hunter Knepshieldcad7f0b2021-12-13 15:08:35 -08006994 if (TextUtils.isEmpty(pkgName)) {
Junda Liu317d70b2016-03-08 09:33:53 -08006995 return TelephonyManager.CARRIER_PRIVILEGE_STATUS_NO_ACCESS;
Hunter Knepshieldcad7f0b2021-12-13 15:08:35 -08006996 }
Zach Johnson50ecba32015-05-19 00:24:21 -07006997 int result = TelephonyManager.CARRIER_PRIVILEGE_STATUS_RULES_NOT_LOADED;
Hunter Knepshieldcad7f0b2021-12-13 15:08:35 -08006998 for (int phoneId = 0; phoneId < TelephonyManager.getDefault().getPhoneCount(); phoneId++) {
6999 Phone phone = PhoneFactory.getPhone(phoneId);
7000 if (phone == null) {
7001 continue;
Zach Johnson50ecba32015-05-19 00:24:21 -07007002 }
Hunter Knepshieldcad7f0b2021-12-13 15:08:35 -08007003 CarrierPrivilegesTracker cpt = phone.getCarrierPrivilegesTracker();
7004 if (cpt == null) {
7005 continue;
7006 }
7007 result = cpt.getCarrierPrivilegeStatusForPackage(pkgName);
Zach Johnson50ecba32015-05-19 00:24:21 -07007008 if (result == TelephonyManager.CARRIER_PRIVILEGE_STATUS_HAS_ACCESS) {
7009 break;
7010 }
7011 }
Zach Johnson50ecba32015-05-19 00:24:21 -07007012 return result;
Junda Liu29340342014-07-10 15:23:27 -07007013 }
Derek Tan89e89d42014-07-08 17:00:10 -07007014
7015 @Override
Junda Liue64de782015-04-16 17:19:16 -07007016 public List<String> getCarrierPackageNamesForIntentAndPhone(Intent intent, int phoneId) {
Nazanin1adf4562021-03-29 15:35:30 -07007017 enforceReadPrivilegedPermission("getCarrierPackageNamesForIntentAndPhone");
Rambo Wang8a247eb2022-02-08 21:11:18 +00007018 Phone phone = PhoneFactory.getPhone(phoneId);
7019 if (phone == null) {
7020 return Collections.emptyList();
Junda Liue64de782015-04-16 17:19:16 -07007021 }
Rambo Wang8a247eb2022-02-08 21:11:18 +00007022 CarrierPrivilegesTracker cpt = phone.getCarrierPrivilegesTracker();
7023 if (cpt == null) {
7024 return Collections.emptyList();
Shishir Agrawaleb6439a2014-07-21 13:19:38 -07007025 }
Rambo Wang8a247eb2022-02-08 21:11:18 +00007026 return cpt.getCarrierPackageNamesForIntent(intent);
Shishir Agrawaleb6439a2014-07-21 13:19:38 -07007027 }
7028
Amith Yamasani6e118872016-02-19 12:53:51 -08007029 @Override
chen xuf7e9fe82019-05-09 19:31:02 -07007030 public List<String> getPackagesWithCarrierPrivileges(int phoneId) {
Nazanin1adf4562021-03-29 15:35:30 -07007031 enforceReadPrivilegedPermission("getPackagesWithCarrierPrivileges");
Hunter Knepshieldcad7f0b2021-12-13 15:08:35 -08007032 Phone phone = PhoneFactory.getPhone(phoneId);
7033 if (phone == null) {
7034 return Collections.emptyList();
Amith Yamasani6e118872016-02-19 12:53:51 -08007035 }
Hunter Knepshieldcad7f0b2021-12-13 15:08:35 -08007036 CarrierPrivilegesTracker cpt = phone.getCarrierPrivilegesTracker();
7037 if (cpt == null) {
7038 return Collections.emptyList();
7039 }
7040 return new ArrayList<>(cpt.getPackagesWithCarrierPrivileges());
Amith Yamasani6e118872016-02-19 12:53:51 -08007041 }
7042
chen xuf7e9fe82019-05-09 19:31:02 -07007043 @Override
7044 public List<String> getPackagesWithCarrierPrivilegesForAllPhones() {
Shuo Qian067a06d2019-12-03 23:40:18 +00007045 enforceReadPrivilegedPermission("getPackagesWithCarrierPrivilegesForAllPhones");
Hunter Knepshieldcad7f0b2021-12-13 15:08:35 -08007046 Set<String> privilegedPackages = new ArraySet<>();
Shuo Qian067a06d2019-12-03 23:40:18 +00007047 final long identity = Binder.clearCallingIdentity();
Shuo Qian067a06d2019-12-03 23:40:18 +00007048 try {
7049 for (int i = 0; i < TelephonyManager.getDefault().getPhoneCount(); i++) {
7050 privilegedPackages.addAll(getPackagesWithCarrierPrivileges(i));
7051 }
7052 } finally {
7053 Binder.restoreCallingIdentity(identity);
chen xuf7e9fe82019-05-09 19:31:02 -07007054 }
Hunter Knepshieldcad7f0b2021-12-13 15:08:35 -08007055 return new ArrayList<>(privilegedPackages);
chen xuf7e9fe82019-05-09 19:31:02 -07007056 }
7057
Rambo Wang6812ffb2022-03-15 16:54:17 -07007058 @Override
7059 public @Nullable String getCarrierServicePackageNameForLogicalSlot(int logicalSlotIndex) {
7060 enforceReadPrivilegedPermission("getCarrierServicePackageNameForLogicalSlot");
7061
7062 final Phone phone = PhoneFactory.getPhone(logicalSlotIndex);
7063 if (phone == null) {
7064 return null;
7065 }
7066 final CarrierPrivilegesTracker cpt = phone.getCarrierPrivilegesTracker();
7067 if (cpt == null) {
7068 return null;
7069 }
7070 return cpt.getCarrierServicePackageName();
7071 }
7072
Wink Savilleb564aae2014-10-23 10:18:09 -07007073 private String getIccId(int subId) {
Sanket Padawe356d7632015-06-22 14:03:32 -07007074 final Phone phone = getPhone(subId);
Muralidhar Reddy472c2ae2021-09-29 19:38:40 +00007075 UiccPort port = phone == null ? null : phone.getUiccPort();
7076 if (port == null) {
Derek Tan97ebb422014-09-05 16:55:38 -07007077 return null;
7078 }
Muralidhar Reddy472c2ae2021-09-29 19:38:40 +00007079 String iccId = port.getIccId();
Derek Tan97ebb422014-09-05 16:55:38 -07007080 if (TextUtils.isEmpty(iccId)) {
Derek Tan97ebb422014-09-05 16:55:38 -07007081 return null;
7082 }
7083 return iccId;
7084 }
7085
Shishir Agrawaleb6439a2014-07-21 13:19:38 -07007086 @Override
Shuo Qiane4e11672020-12-15 22:15:33 -08007087 public void setCallComposerStatus(int subId, int status) {
7088 enforceModifyPermission();
7089
7090 final long identity = Binder.clearCallingIdentity();
7091 try {
7092 Phone phone = getPhone(subId);
7093 if (phone != null) {
7094 Phone defaultPhone = phone.getImsPhone();
7095 if (defaultPhone != null && defaultPhone.getPhoneType() == PHONE_TYPE_IMS) {
7096 ImsPhone imsPhone = (ImsPhone) defaultPhone;
7097 imsPhone.setCallComposerStatus(status);
Shuo Qian284ae752020-12-22 19:10:14 -08007098 ImsManager.getInstance(mApp, getSlotIndexOrException(subId))
7099 .updateImsServiceConfig();
Shuo Qiane4e11672020-12-15 22:15:33 -08007100 }
7101 }
Shuo Qian284ae752020-12-22 19:10:14 -08007102 } catch (ImsException e) {
7103 throw new ServiceSpecificException(e.getCode());
7104 } finally {
Shuo Qiane4e11672020-12-15 22:15:33 -08007105 Binder.restoreCallingIdentity(identity);
7106 }
7107 }
7108
7109 @Override
7110 public int getCallComposerStatus(int subId) {
7111 enforceReadPrivilegedPermission("getCallComposerStatus");
7112
7113 final long identity = Binder.clearCallingIdentity();
7114 try {
7115 Phone phone = getPhone(subId);
7116 if (phone != null) {
7117 Phone defaultPhone = phone.getImsPhone();
7118 if (defaultPhone != null && defaultPhone.getPhoneType() == PHONE_TYPE_IMS) {
7119 ImsPhone imsPhone = (ImsPhone) defaultPhone;
7120 return imsPhone.getCallComposerStatus();
7121 }
7122 }
7123 } finally {
7124 Binder.restoreCallingIdentity(identity);
7125 }
7126 return TelephonyManager.CALL_COMPOSER_STATUS_OFF;
7127 }
7128
7129 @Override
Jeff Sharkey85190e62014-12-05 09:40:12 -08007130 public boolean setLine1NumberForDisplayForSubscriber(int subId, String alphaTag,
7131 String number) {
Shuo Qian2c0ae432019-12-05 11:40:37 -08007132 TelephonyPermissions.enforceCallingOrSelfCarrierPrivilege(mApp,
Jeff Davidson7e17e312018-02-13 18:17:36 -08007133 subId, "setLine1NumberForDisplayForSubscriber");
Derek Tan97ebb422014-09-05 16:55:38 -07007134
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007135 final long identity = Binder.clearCallingIdentity();
7136 try {
7137 final String iccId = getIccId(subId);
7138 final Phone phone = getPhone(subId);
7139 if (phone == null) {
7140 return false;
7141 }
7142 final String subscriberId = phone.getSubscriberId();
7143
7144 if (DBG_MERGE) {
Amit Mahajanb8f13202020-01-27 18:16:07 -08007145 Rlog.d(LOG_TAG, "Setting line number for ICC=" + iccId + ", subscriberId="
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007146 + subscriberId + " to " + number);
7147 }
7148
7149 if (TextUtils.isEmpty(iccId)) {
7150 return false;
7151 }
7152
7153 final SharedPreferences.Editor editor = mTelephonySharedPreferences.edit();
7154
7155 final String alphaTagPrefKey = PREF_CARRIERS_ALPHATAG_PREFIX + iccId;
7156 if (alphaTag == null) {
7157 editor.remove(alphaTagPrefKey);
7158 } else {
7159 editor.putString(alphaTagPrefKey, alphaTag);
7160 }
7161
7162 // Record both the line number and IMSI for this ICCID, since we need to
7163 // track all merged IMSIs based on line number
7164 final String numberPrefKey = PREF_CARRIERS_NUMBER_PREFIX + iccId;
7165 final String subscriberPrefKey = PREF_CARRIERS_SUBSCRIBER_PREFIX + iccId;
7166 if (number == null) {
7167 editor.remove(numberPrefKey);
7168 editor.remove(subscriberPrefKey);
7169 } else {
7170 editor.putString(numberPrefKey, number);
7171 editor.putString(subscriberPrefKey, subscriberId);
7172 }
7173
7174 editor.commit();
7175 return true;
7176 } finally {
7177 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07007178 }
Derek Tan7226c842014-07-02 17:42:23 -07007179 }
7180
7181 @Override
Philip P. Moltmann700a9592019-10-03 11:53:50 -07007182 public String getLine1NumberForDisplay(int subId, String callingPackage,
7183 String callingFeatureId) {
Makoto Onukifee69342015-06-29 14:44:50 -07007184 // This is open to apps with WRITE_SMS.
Jeff Davidson7e17e312018-02-13 18:17:36 -08007185 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneNumber(
Philip P. Moltmann700a9592019-10-03 11:53:50 -07007186 mApp, subId, callingPackage, callingFeatureId, "getLine1NumberForDisplay")) {
Amit Mahajan9cf11512015-11-09 11:40:48 -08007187 if (DBG_MERGE) log("getLine1NumberForDisplay returning null due to permission");
Svet Ganovb320e182015-04-16 12:30:10 -07007188 return null;
7189 }
Derek Tan97ebb422014-09-05 16:55:38 -07007190
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007191 final long identity = Binder.clearCallingIdentity();
7192 try {
7193 String iccId = getIccId(subId);
7194 if (iccId != null) {
7195 String numberPrefKey = PREF_CARRIERS_NUMBER_PREFIX + iccId;
7196 if (DBG_MERGE) {
7197 log("getLine1NumberForDisplay returning "
7198 + mTelephonySharedPreferences.getString(numberPrefKey, null));
7199 }
7200 return mTelephonySharedPreferences.getString(numberPrefKey, null);
Amit Mahajan9cf11512015-11-09 11:40:48 -08007201 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007202 if (DBG_MERGE) log("getLine1NumberForDisplay returning null as iccId is null");
7203 return null;
7204 } finally {
7205 Binder.restoreCallingIdentity(identity);
Derek Tan7226c842014-07-02 17:42:23 -07007206 }
Derek Tan7226c842014-07-02 17:42:23 -07007207 }
7208
7209 @Override
Philip P. Moltmann700a9592019-10-03 11:53:50 -07007210 public String getLine1AlphaTagForDisplay(int subId, String callingPackage,
7211 String callingFeatureId) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08007212 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
Philip P. Moltmann700a9592019-10-03 11:53:50 -07007213 mApp, subId, callingPackage, callingFeatureId, "getLine1AlphaTagForDisplay")) {
Svet Ganovb320e182015-04-16 12:30:10 -07007214 return null;
7215 }
Derek Tan97ebb422014-09-05 16:55:38 -07007216
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007217 final long identity = Binder.clearCallingIdentity();
7218 try {
7219 String iccId = getIccId(subId);
7220 if (iccId != null) {
7221 String alphaTagPrefKey = PREF_CARRIERS_ALPHATAG_PREFIX + iccId;
7222 return mTelephonySharedPreferences.getString(alphaTagPrefKey, null);
7223 }
7224 return null;
7225 } finally {
7226 Binder.restoreCallingIdentity(identity);
Derek Tan7226c842014-07-02 17:42:23 -07007227 }
Derek Tan7226c842014-07-02 17:42:23 -07007228 }
Shishir Agrawalb1ebf8c2014-07-17 16:32:41 -07007229
7230 @Override
Philip P. Moltmann700a9592019-10-03 11:53:50 -07007231 public String[] getMergedSubscriberIds(int subId, String callingPackage,
7232 String callingFeatureId) {
Jeff Davidson913390f2018-02-23 17:11:49 -08007233 // This API isn't public, so no need to provide a valid subscription ID - we're not worried
7234 // about carrier-privileged callers not having access.
Jeff Davidson7e17e312018-02-13 18:17:36 -08007235 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
Jeff Davidson913390f2018-02-23 17:11:49 -08007236 mApp, SubscriptionManager.INVALID_SUBSCRIPTION_ID, callingPackage,
Philip P. Moltmann700a9592019-10-03 11:53:50 -07007237 callingFeatureId, "getMergedSubscriberIds")) {
Fyodor Kupolov8e53b0b2015-06-17 13:17:50 -07007238 return null;
7239 }
Jeff Sharkey85190e62014-12-05 09:40:12 -08007240
Jordan Liub49b04b2019-05-06 14:45:15 -07007241 // Clear calling identity, when calling TelephonyManager, because callerUid must be
7242 // the process, where TelephonyManager was instantiated.
7243 // Otherwise AppOps check will fail.
Fyodor Kupolov8e53b0b2015-06-17 13:17:50 -07007244 final long identity = Binder.clearCallingIdentity();
7245 try {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08007246 final Context context = mApp;
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007247 final TelephonyManager tele = TelephonyManager.from(context);
7248 final SubscriptionManager sub = SubscriptionManager.from(context);
7249
7250 // Figure out what subscribers are currently active
7251 final ArraySet<String> activeSubscriberIds = new ArraySet<>();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007252
Jordan Liub49b04b2019-05-06 14:45:15 -07007253 // Only consider subs which match the current subId
7254 // This logic can be simplified. See b/131189269 for progress.
7255 if (isActiveSubscription(subId)) {
Fyodor Kupolov8e53b0b2015-06-17 13:17:50 -07007256 activeSubscriberIds.add(tele.getSubscriberId(subId));
7257 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007258
7259 // First pass, find a number override for an active subscriber
7260 String mergeNumber = null;
7261 final Map<String, ?> prefs = mTelephonySharedPreferences.getAll();
7262 for (String key : prefs.keySet()) {
7263 if (key.startsWith(PREF_CARRIERS_SUBSCRIBER_PREFIX)) {
7264 final String subscriberId = (String) prefs.get(key);
7265 if (activeSubscriberIds.contains(subscriberId)) {
7266 final String iccId = key.substring(
7267 PREF_CARRIERS_SUBSCRIBER_PREFIX.length());
7268 final String numberKey = PREF_CARRIERS_NUMBER_PREFIX + iccId;
7269 mergeNumber = (String) prefs.get(numberKey);
7270 if (DBG_MERGE) {
Amit Mahajanb8f13202020-01-27 18:16:07 -08007271 Rlog.d(LOG_TAG, "Found line number " + mergeNumber
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007272 + " for active subscriber " + subscriberId);
7273 }
7274 if (!TextUtils.isEmpty(mergeNumber)) {
7275 break;
7276 }
7277 }
7278 }
7279 }
7280
7281 // Shortcut when no active merged subscribers
7282 if (TextUtils.isEmpty(mergeNumber)) {
7283 return null;
7284 }
7285
7286 // Second pass, find all subscribers under that line override
7287 final ArraySet<String> result = new ArraySet<>();
7288 for (String key : prefs.keySet()) {
7289 if (key.startsWith(PREF_CARRIERS_NUMBER_PREFIX)) {
7290 final String number = (String) prefs.get(key);
7291 if (mergeNumber.equals(number)) {
7292 final String iccId = key.substring(PREF_CARRIERS_NUMBER_PREFIX.length());
7293 final String subscriberKey = PREF_CARRIERS_SUBSCRIBER_PREFIX + iccId;
7294 final String subscriberId = (String) prefs.get(subscriberKey);
7295 if (!TextUtils.isEmpty(subscriberId)) {
7296 result.add(subscriberId);
7297 }
7298 }
7299 }
7300 }
7301
7302 final String[] resultArray = result.toArray(new String[result.size()]);
7303 Arrays.sort(resultArray);
7304 if (DBG_MERGE) {
Amit Mahajanb8f13202020-01-27 18:16:07 -08007305 Rlog.d(LOG_TAG,
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007306 "Found subscribers " + Arrays.toString(resultArray) + " after merge");
7307 }
7308 return resultArray;
Fyodor Kupolov8e53b0b2015-06-17 13:17:50 -07007309 } finally {
7310 Binder.restoreCallingIdentity(identity);
Jeff Sharkey85190e62014-12-05 09:40:12 -08007311 }
Jeff Sharkey85190e62014-12-05 09:40:12 -08007312 }
7313
7314 @Override
zoey chen38003472019-12-13 17:16:31 +08007315 public String[] getMergedImsisFromGroup(int subId, String callingPackage) {
7316 enforceReadPrivilegedPermission("getMergedImsisFromGroup");
Malcolm Chen6ca97372019-07-01 16:28:21 -07007317
7318 final long identity = Binder.clearCallingIdentity();
7319 try {
7320 final TelephonyManager telephonyManager = mApp.getSystemService(
7321 TelephonyManager.class);
7322 String subscriberId = telephonyManager.getSubscriberId(subId);
7323 if (subscriberId == null) {
7324 if (DBG) {
zoey chen38003472019-12-13 17:16:31 +08007325 log("getMergedImsisFromGroup can't find subscriberId for subId "
Malcolm Chen6ca97372019-07-01 16:28:21 -07007326 + subId);
7327 }
7328 return null;
7329 }
7330
Jack Yu285100e2022-12-02 22:48:35 -08007331 ParcelUuid groupUuid;
7332 if (PhoneFactory.isSubscriptionManagerServiceEnabled()) {
7333 final SubscriptionInfo info = SubscriptionManagerService.getInstance()
7334 .getSubscriptionInfo(subId);
7335 groupUuid = info.getGroupUuid();
7336 } else {
7337 final SubscriptionInfo info = mSubscriptionController
7338 .getSubscriptionInfo(subId);
7339 groupUuid = info.getGroupUuid();
7340 }
Malcolm Chen6ca97372019-07-01 16:28:21 -07007341 // If it doesn't belong to any group, return just subscriberId of itself.
7342 if (groupUuid == null) {
7343 return new String[]{subscriberId};
7344 }
7345
7346 // Get all subscriberIds from the group.
7347 final List<String> mergedSubscriberIds = new ArrayList<>();
Jack Yu285100e2022-12-02 22:48:35 -08007348 List<SubscriptionInfo> groupInfos;
7349 if (PhoneFactory.isSubscriptionManagerServiceEnabled()) {
7350 groupInfos = SubscriptionManagerService.getInstance()
7351 .getSubscriptionsInGroup(groupUuid, mApp.getOpPackageName(),
7352 mApp.getAttributionTag());
7353 } else {
7354 groupInfos = mSubscriptionController
7355 .getSubscriptionsInGroup(groupUuid, mApp.getOpPackageName(),
7356 mApp.getAttributionTag());
7357 }
Malcolm Chen6ca97372019-07-01 16:28:21 -07007358 for (SubscriptionInfo subInfo : groupInfos) {
7359 subscriberId = telephonyManager.getSubscriberId(subInfo.getSubscriptionId());
7360 if (subscriberId != null) {
7361 mergedSubscriberIds.add(subscriberId);
7362 }
7363 }
7364
7365 return mergedSubscriberIds.toArray(new String[mergedSubscriberIds.size()]);
7366 } finally {
7367 Binder.restoreCallingIdentity(identity);
7368
7369 }
7370 }
7371
7372 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08007373 public boolean setOperatorBrandOverride(int subId, String brand) {
Shuo Qian2c0ae432019-12-05 11:40:37 -08007374 TelephonyPermissions.enforceCallingOrSelfCarrierPrivilege(mApp,
Jeff Davidson7e17e312018-02-13 18:17:36 -08007375 subId, "setOperatorBrandOverride");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007376
7377 final long identity = Binder.clearCallingIdentity();
7378 try {
7379 final Phone phone = getPhone(subId);
7380 return phone == null ? false : phone.setOperatorBrandOverride(brand);
7381 } finally {
7382 Binder.restoreCallingIdentity(identity);
7383 }
Shishir Agrawalb1ebf8c2014-07-17 16:32:41 -07007384 }
Steven Liu4bf01bc2014-07-17 11:05:29 -05007385
7386 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08007387 public boolean setRoamingOverride(int subId, List<String> gsmRoamingList,
Shishir Agrawal621a47c2014-12-01 10:25:09 -08007388 List<String> gsmNonRoamingList, List<String> cdmaRoamingList,
7389 List<String> cdmaNonRoamingList) {
Shuo Qian2c0ae432019-12-05 11:40:37 -08007390 TelephonyPermissions.enforceCallingOrSelfCarrierPrivilege(
7391 mApp, subId, "setRoamingOverride");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007392
7393 final long identity = Binder.clearCallingIdentity();
7394 try {
7395 final Phone phone = getPhone(subId);
7396 if (phone == null) {
7397 return false;
7398 }
7399 return phone.setRoamingOverride(gsmRoamingList, gsmNonRoamingList, cdmaRoamingList,
7400 cdmaNonRoamingList);
7401 } finally {
7402 Binder.restoreCallingIdentity(identity);
Shishir Agrawalc04d9752016-02-19 10:41:00 -08007403 }
Shishir Agrawal621a47c2014-12-01 10:25:09 -08007404 }
7405
7406 @Override
Shuo Qian850e4d6a2018-04-25 21:02:08 +00007407 @Deprecated
7408 public int invokeOemRilRequestRaw(byte[] oemReq, byte[] oemResp) {
7409 enforceModifyPermission();
7410
7411 int returnValue = 0;
7412 try {
vagdeviaf9a5b92018-08-15 16:01:53 -07007413 AsyncResult result = (AsyncResult) sendRequest(CMD_INVOKE_OEM_RIL_REQUEST_RAW, oemReq);
Shuo Qian850e4d6a2018-04-25 21:02:08 +00007414 if(result.exception == null) {
7415 if (result.result != null) {
7416 byte[] responseData = (byte[])(result.result);
7417 if(responseData.length > oemResp.length) {
7418 Log.w(LOG_TAG, "Buffer to copy response too small: Response length is " +
7419 responseData.length + "bytes. Buffer Size is " +
7420 oemResp.length + "bytes.");
7421 }
7422 System.arraycopy(responseData, 0, oemResp, 0, responseData.length);
7423 returnValue = responseData.length;
7424 }
7425 } else {
7426 CommandException ex = (CommandException) result.exception;
7427 returnValue = ex.getCommandError().ordinal();
7428 if(returnValue > 0) returnValue *= -1;
7429 }
7430 } catch (RuntimeException e) {
7431 Log.w(LOG_TAG, "sendOemRilRequestRaw: Runtime Exception");
7432 returnValue = (CommandException.Error.GENERIC_FAILURE.ordinal());
7433 if(returnValue > 0) returnValue *= -1;
7434 }
7435
7436 return returnValue;
7437 }
7438
7439 @Override
Robert Greenwalt36b23af2015-07-06 17:59:14 -07007440 public int getRadioAccessFamily(int phoneId, String callingPackage) {
Jeff Davidson913390f2018-02-23 17:11:49 -08007441 Phone phone = PhoneFactory.getPhone(phoneId);
Shuo Qiandee53402020-05-29 14:08:15 -07007442 try {
7443 TelephonyPermissions
Nathan Harold62c68512021-04-06 11:26:02 -07007444 .enforceCallingOrSelfReadPrivilegedPhoneStatePermissionOrCarrierPrivilege(
Shuo Qiandee53402020-05-29 14:08:15 -07007445 mApp, phone.getSubId(), "getRadioAccessFamily");
7446 } catch (SecurityException e) {
7447 EventLog.writeEvent(0x534e4554, "150857259", -1, "Missing Permission");
7448 throw e;
7449 }
chen xub97461a2018-10-26 14:17:57 -07007450 int raf = RadioAccessFamily.RAF_UNKNOWN;
Jeff Davidson913390f2018-02-23 17:11:49 -08007451 if (phone == null) {
chen xub97461a2018-10-26 14:17:57 -07007452 return raf;
Jeff Davidson913390f2018-02-23 17:11:49 -08007453 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007454 final long identity = Binder.clearCallingIdentity();
7455 try {
chen xub97461a2018-10-26 14:17:57 -07007456 TelephonyPermissions
Nathan Harold62c68512021-04-06 11:26:02 -07007457 .enforceCallingOrSelfReadPrivilegedPhoneStatePermissionOrCarrierPrivilege(
chen xub97461a2018-10-26 14:17:57 -07007458 mApp, phone.getSubId(), "getRadioAccessFamily");
7459 raf = ProxyController.getInstance().getRadioAccessFamily(phoneId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007460 } finally {
7461 Binder.restoreCallingIdentity(identity);
7462 }
chen xub97461a2018-10-26 14:17:57 -07007463 return raf;
Wink Saville5d475dd2014-10-17 15:00:58 -07007464 }
Andrew Leedf14ead2014-10-17 14:22:52 -07007465
7466 @Override
Hall Liu82694d52020-12-11 18:22:04 -08007467 public void uploadCallComposerPicture(int subscriptionId, String callingPackage,
Hall Liue31bac62020-12-23 19:16:10 -08007468 String contentType, ParcelFileDescriptor fd, ResultReceiver callback) {
Hall Liu82694d52020-12-11 18:22:04 -08007469 try {
7470 if (!Objects.equals(mApp.getPackageManager().getPackageUid(callingPackage, 0),
7471 Binder.getCallingUid())) {
Tyler Gunnb87925a2021-06-10 14:54:27 -07007472 throw new SecurityException("Invalid package:" + callingPackage);
Hall Liu82694d52020-12-11 18:22:04 -08007473 }
7474 } catch (PackageManager.NameNotFoundException e) {
Tyler Gunnb87925a2021-06-10 14:54:27 -07007475 throw new SecurityException("Invalid package:" + callingPackage);
Hall Liu82694d52020-12-11 18:22:04 -08007476 }
7477 RoleManager rm = mApp.getSystemService(RoleManager.class);
7478 List<String> dialerRoleHolders = rm.getRoleHolders(RoleManager.ROLE_DIALER);
7479 if (!dialerRoleHolders.contains(callingPackage)) {
7480 throw new SecurityException("App must be the dialer role holder to"
7481 + " upload a call composer pic");
7482 }
7483
7484 Executors.newSingleThreadExecutor().execute(() -> {
7485 ByteArrayOutputStream output = new ByteArrayOutputStream(
7486 (int) TelephonyManager.getMaximumCallComposerPictureSize());
7487 InputStream input = new ParcelFileDescriptor.AutoCloseInputStream(fd);
7488 boolean readUntilEnd = false;
7489 int totalBytesRead = 0;
7490 byte[] buffer = new byte[16 * 1024];
7491 while (true) {
7492 int numRead;
7493 try {
7494 numRead = input.read(buffer);
7495 } catch (IOException e) {
7496 try {
7497 fd.checkError();
7498 callback.send(TelephonyManager.CallComposerException.ERROR_INPUT_CLOSED,
7499 null);
7500 } catch (IOException e1) {
7501 // This means that the other side closed explicitly with an error. If this
7502 // happens, log and ignore.
7503 loge("Remote end of call composer picture pipe closed: " + e1);
7504 }
7505 break;
7506 }
7507 if (numRead == -1) {
7508 readUntilEnd = true;
7509 break;
7510 }
7511 totalBytesRead += numRead;
7512 if (totalBytesRead > TelephonyManager.getMaximumCallComposerPictureSize()) {
7513 loge("Too many bytes read for call composer picture: " + totalBytesRead);
7514 try {
7515 input.close();
7516 } catch (IOException e) {
7517 // ignore
7518 }
7519 break;
7520 }
7521 output.write(buffer, 0, numRead);
7522 }
7523 // Generally, the remote end will close the file descriptors. The only case where we
7524 // close is above, where the picture size is too big.
7525
7526 try {
7527 fd.checkError();
7528 } catch (IOException e) {
7529 loge("Remote end for call composer closed with an error: " + e);
7530 return;
7531 }
7532
Hall Liuaa4211e2021-01-20 15:43:39 -08007533 if (!readUntilEnd) {
7534 loge("Did not finish reading entire image; aborting");
7535 return;
7536 }
Hall Liu82694d52020-12-11 18:22:04 -08007537
Hall Liuaa4211e2021-01-20 15:43:39 -08007538 ImageData imageData = new ImageData(output.toByteArray(), contentType, null);
7539 CallComposerPictureManager.getInstance(mApp, subscriptionId).handleUploadToServer(
7540 new CallComposerPictureTransfer.Factory() {},
7541 imageData,
7542 (result) -> {
7543 if (result.first != null) {
7544 ParcelUuid parcelUuid = new ParcelUuid(result.first);
7545 Bundle outputResult = new Bundle();
7546 outputResult.putParcelable(
7547 TelephonyManager.KEY_CALL_COMPOSER_PICTURE_HANDLE, parcelUuid);
7548 callback.send(TelephonyManager.CallComposerException.SUCCESS,
7549 outputResult);
7550 } else {
7551 callback.send(result.second, null);
7552 }
7553 }
7554 );
Hall Liu82694d52020-12-11 18:22:04 -08007555 });
7556 }
7557
7558 @Override
Andrew Leedf14ead2014-10-17 14:22:52 -07007559 public void enableVideoCalling(boolean enable) {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08007560 final Phone defaultPhone = getDefaultPhone();
Andrew Leedf14ead2014-10-17 14:22:52 -07007561 enforceModifyPermission();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007562
7563 final long identity = Binder.clearCallingIdentity();
7564 try {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08007565 ImsManager.getInstance(defaultPhone.getContext(),
7566 defaultPhone.getPhoneId()).setVtSetting(enable);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007567 } finally {
7568 Binder.restoreCallingIdentity(identity);
7569 }
Andrew Leedf14ead2014-10-17 14:22:52 -07007570 }
7571
7572 @Override
Philip P. Moltmann700a9592019-10-03 11:53:50 -07007573 public boolean isVideoCallingEnabled(String callingPackage, String callingFeatureId) {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08007574 final Phone defaultPhone = getDefaultPhone();
Philip P. Moltmann700a9592019-10-03 11:53:50 -07007575 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(mApp, defaultPhone.getSubId(),
7576 callingPackage, callingFeatureId, "isVideoCallingEnabled")) {
Amit Mahajan578e53d2018-03-20 16:18:38 +00007577 return false;
7578 }
Svet Ganovb320e182015-04-16 12:30:10 -07007579
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007580 final long identity = Binder.clearCallingIdentity();
7581 try {
7582 // Check the user preference and the system-level IMS setting. Even if the user has
7583 // enabled video calling, if IMS is disabled we aren't able to support video calling.
7584 // In the long run, we may instead need to check if there exists a connection service
7585 // which can support video calling.
7586 ImsManager imsManager =
Nazanin Bakhshif782e562018-12-11 15:15:39 -08007587 ImsManager.getInstance(defaultPhone.getContext(), defaultPhone.getPhoneId());
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007588 return imsManager.isVtEnabledByPlatform()
7589 && imsManager.isEnhanced4gLteModeSettingEnabledByUser()
7590 && imsManager.isVtEnabledByUser();
7591 } finally {
7592 Binder.restoreCallingIdentity(identity);
7593 }
Andrew Leedf14ead2014-10-17 14:22:52 -07007594 }
Libin.Tang@motorola.comafe82642014-12-18 13:27:53 -06007595
Andrew Leea1239f22015-03-02 17:44:07 -08007596 @Override
Philip P. Moltmann700a9592019-10-03 11:53:50 -07007597 public boolean canChangeDtmfToneLength(int subId, String callingPackage,
7598 String callingFeatureId) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007599 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
Philip P. Moltmann700a9592019-10-03 11:53:50 -07007600 mApp, subId, callingPackage, callingFeatureId,
7601 "isVideoCallingEnabled")) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007602 return false;
7603 }
7604
7605 final long identity = Binder.clearCallingIdentity();
7606 try {
7607 CarrierConfigManager configManager =
7608 (CarrierConfigManager) mApp.getSystemService(Context.CARRIER_CONFIG_SERVICE);
Nazanin Bakhshif782e562018-12-11 15:15:39 -08007609 return configManager.getConfigForSubId(subId)
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007610 .getBoolean(CarrierConfigManager.KEY_DTMF_TYPE_ENABLED_BOOL);
7611 } finally {
7612 Binder.restoreCallingIdentity(identity);
7613 }
Andrew Leea1239f22015-03-02 17:44:07 -08007614 }
7615
7616 @Override
Philip P. Moltmann700a9592019-10-03 11:53:50 -07007617 public boolean isWorldPhone(int subId, String callingPackage, String callingFeatureId) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007618 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
Philip P. Moltmann700a9592019-10-03 11:53:50 -07007619 mApp, subId, callingPackage, callingFeatureId, "isVideoCallingEnabled")) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007620 return false;
7621 }
7622
7623 final long identity = Binder.clearCallingIdentity();
7624 try {
7625 CarrierConfigManager configManager =
7626 (CarrierConfigManager) mApp.getSystemService(Context.CARRIER_CONFIG_SERVICE);
Nazanin Bakhshif782e562018-12-11 15:15:39 -08007627 return configManager.getConfigForSubId(subId)
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007628 .getBoolean(CarrierConfigManager.KEY_WORLD_PHONE_BOOL);
7629 } finally {
7630 Binder.restoreCallingIdentity(identity);
7631 }
Andrew Leea1239f22015-03-02 17:44:07 -08007632 }
7633
Andrew Lee9431b832015-03-09 18:46:45 -07007634 @Override
7635 public boolean isTtyModeSupported() {
Tyler Gunn5ddfdc92019-10-31 13:08:23 -07007636 TelecomManager telecomManager = mApp.getSystemService(TelecomManager.class);
Wooki Wu1f82f7a2016-02-15 15:59:58 +08007637 return telecomManager.isTtySupported();
Andrew Lee9431b832015-03-09 18:46:45 -07007638 }
7639
7640 @Override
7641 public boolean isHearingAidCompatibilitySupported() {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007642 final long identity = Binder.clearCallingIdentity();
7643 try {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08007644 return mApp.getResources().getBoolean(R.bool.hac_enabled);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007645 } finally {
7646 Binder.restoreCallingIdentity(identity);
7647 }
Andrew Lee9431b832015-03-09 18:46:45 -07007648 }
7649
Hall Liuf6668912018-10-31 17:05:23 -07007650 /**
7651 * Determines whether the device currently supports RTT (Real-time text). Based both on carrier
7652 * support for the feature and device firmware support.
7653 *
7654 * @return {@code true} if the device and carrier both support RTT, {@code false} otherwise.
7655 */
7656 @Override
7657 public boolean isRttSupported(int subscriptionId) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007658 final long identity = Binder.clearCallingIdentity();
Nazanin Bakhshif782e562018-12-11 15:15:39 -08007659 final Phone phone = getPhone(subscriptionId);
7660 if (phone == null) {
7661 loge("isRttSupported: no Phone found. Invalid subId:" + subscriptionId);
7662 return false;
7663 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007664 try {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08007665 boolean isCarrierSupported = mApp.getCarrierConfigForSubId(subscriptionId).getBoolean(
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007666 CarrierConfigManager.KEY_RTT_SUPPORTED_BOOL);
7667 boolean isDeviceSupported =
Nazanin Bakhshif782e562018-12-11 15:15:39 -08007668 phone.getContext().getResources().getBoolean(R.bool.config_support_rtt);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007669 return isCarrierSupported && isDeviceSupported;
7670 } finally {
7671 Binder.restoreCallingIdentity(identity);
7672 }
Hall Liu98187582018-01-22 19:15:32 -08007673 }
7674
Hall Liuf6668912018-10-31 17:05:23 -07007675 /**
Hall Liuf2daa022019-07-23 18:39:00 -07007676 * Determines whether the user has turned on RTT. If the carrier wants to ignore the user-set
7677 * RTT setting, will return true if the device and carrier both support RTT.
7678 * Otherwise. only returns true if the device and carrier both also support RTT.
Hall Liuf6668912018-10-31 17:05:23 -07007679 */
7680 public boolean isRttEnabled(int subscriptionId) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007681 final long identity = Binder.clearCallingIdentity();
7682 try {
Hall Liu5bab75c2019-12-11 23:58:20 +00007683 boolean isRttSupported = isRttSupported(subscriptionId);
7684 boolean isUserRttSettingOn = Settings.Secure.getInt(
7685 mApp.getContentResolver(), Settings.Secure.RTT_CALLING_MODE, 0) != 0;
7686 boolean shouldIgnoreUserRttSetting = mApp.getCarrierConfigForSubId(subscriptionId)
7687 .getBoolean(CarrierConfigManager.KEY_IGNORE_RTT_MODE_SETTING_BOOL);
7688 return isRttSupported && (isUserRttSettingOn || shouldIgnoreUserRttSetting);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007689 } finally {
7690 Binder.restoreCallingIdentity(identity);
7691 }
Hall Liu3ad5f012018-04-06 16:23:39 -07007692 }
7693
Philip P. Moltmann700a9592019-10-03 11:53:50 -07007694 @Deprecated
7695 @Override
7696 public String getDeviceId(String callingPackage) {
7697 return getDeviceIdWithFeature(callingPackage, null);
7698 }
7699
Sanket Padawe7310cc72015-01-14 09:53:20 -08007700 /**
7701 * Returns the unique device ID of phone, for example, the IMEI for
7702 * GSM and the MEID for CDMA phones. Return null if device ID is not available.
7703 *
7704 * <p>Requires Permission:
7705 * {@link android.Manifest.permission#READ_PHONE_STATE READ_PHONE_STATE}
7706 */
7707 @Override
Philip P. Moltmann700a9592019-10-03 11:53:50 -07007708 public String getDeviceIdWithFeature(String callingPackage, String callingFeatureId) {
Shuo Qian13d89152021-05-10 23:58:11 -07007709 try {
7710 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
7711 } catch (SecurityException se) {
7712 EventLog.writeEvent(0x534e4554, "186530889", Binder.getCallingUid());
7713 throw new SecurityException("Package " + callingPackage + " does not belong to "
7714 + Binder.getCallingUid());
7715 }
Sanket Padawe7310cc72015-01-14 09:53:20 -08007716 final Phone phone = PhoneFactory.getPhone(0);
Jeff Davidson913390f2018-02-23 17:11:49 -08007717 if (phone == null) {
Sanket Padawe7310cc72015-01-14 09:53:20 -08007718 return null;
7719 }
Jeff Davidson913390f2018-02-23 17:11:49 -08007720 int subId = phone.getSubId();
Michael Groover70af6dc2018-10-01 16:23:15 -07007721 if (!TelephonyPermissions.checkCallingOrSelfReadDeviceIdentifiers(mApp, subId,
Philip P. Moltmann700a9592019-10-03 11:53:50 -07007722 callingPackage, callingFeatureId, "getDeviceId")) {
Jeff Davidson913390f2018-02-23 17:11:49 -08007723 return null;
7724 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007725
7726 final long identity = Binder.clearCallingIdentity();
7727 try {
7728 return phone.getDeviceId();
7729 } finally {
7730 Binder.restoreCallingIdentity(identity);
7731 }
Sanket Padawe7310cc72015-01-14 09:53:20 -08007732 }
7733
Ping Sunc67b7c22016-03-02 19:16:45 +08007734 /**
7735 * {@hide}
7736 * Returns the IMS Registration Status on a particular subid
7737 *
7738 * @param subId
7739 */
Brad Ebinger1f2b5082018-02-08 16:11:32 -08007740 public boolean isImsRegistered(int subId) {
Ping Sunc67b7c22016-03-02 19:16:45 +08007741 Phone phone = getPhone(subId);
7742 if (phone != null) {
7743 return phone.isImsRegistered();
7744 } else {
7745 return false;
7746 }
7747 }
7748
Santos Cordon7a1885b2015-02-03 11:15:19 -08007749 @Override
Shuo Qian6e6137d2019-10-30 16:33:31 -07007750 public int getSubIdForPhoneAccountHandle(
Philip P. Moltmann700a9592019-10-03 11:53:50 -07007751 PhoneAccountHandle phoneAccountHandle, String callingPackage, String callingFeatureId) {
Shuo Qian6e6137d2019-10-30 16:33:31 -07007752 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(mApp, getDefaultSubscription(),
Philip P. Moltmann700a9592019-10-03 11:53:50 -07007753 callingPackage, callingFeatureId, "getSubIdForPhoneAccountHandle")) {
Shuo Qian6e6137d2019-10-30 16:33:31 -07007754 throw new SecurityException("Requires READ_PHONE_STATE permission.");
7755 }
7756 final long identity = Binder.clearCallingIdentity();
7757 try {
7758 return PhoneUtils.getSubIdForPhoneAccountHandle(phoneAccountHandle);
7759 } finally {
7760 Binder.restoreCallingIdentity(identity);
7761 }
7762 }
7763
7764 @Override
Tyler Gunnf70ed162019-04-03 15:28:53 -07007765 public @Nullable PhoneAccountHandle getPhoneAccountHandleForSubscriptionId(int subscriptionId) {
Alireza Forouzan4ac4f982021-03-16 22:18:52 -07007766 TelephonyPermissions
Nathan Harold62c68512021-04-06 11:26:02 -07007767 .enforceCallingOrSelfReadPrivilegedPhoneStatePermissionOrCarrierPrivilege(
Alireza Forouzan4ac4f982021-03-16 22:18:52 -07007768 mApp,
7769 subscriptionId,
7770 "getPhoneAccountHandleForSubscriptionId, " + "subscriptionId: " + subscriptionId);
Tyler Gunnf70ed162019-04-03 15:28:53 -07007771 final long identity = Binder.clearCallingIdentity();
7772 try {
7773 Phone phone = getPhone(subscriptionId);
7774 if (phone == null) {
7775 return null;
7776 }
7777 return PhoneUtils.makePstnPhoneAccountHandle(phone);
7778 } finally {
7779 Binder.restoreCallingIdentity(identity);
7780 }
7781 }
7782
Brad Ebinger1f2b5082018-02-08 16:11:32 -08007783 /**
7784 * @return the VoWiFi calling availability.
Nathan Haroldc55097a2015-03-11 18:14:50 -07007785 */
Brad Ebinger1f2b5082018-02-08 16:11:32 -08007786 public boolean isWifiCallingAvailable(int subId) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007787 final long identity = Binder.clearCallingIdentity();
7788 try {
7789 Phone phone = getPhone(subId);
7790 if (phone != null) {
7791 return phone.isWifiCallingEnabled();
7792 } else {
7793 return false;
7794 }
7795 } finally {
7796 Binder.restoreCallingIdentity(identity);
Brad Ebinger1f2b5082018-02-08 16:11:32 -08007797 }
Nathan Haroldc55097a2015-03-11 18:14:50 -07007798 }
7799
Brad Ebinger1f2b5082018-02-08 16:11:32 -08007800 /**
Brad Ebinger1f2b5082018-02-08 16:11:32 -08007801 * @return the VT calling availability.
Etan Cohen3b7a1bc2015-05-28 15:57:13 -07007802 */
Brad Ebinger1f2b5082018-02-08 16:11:32 -08007803 public boolean isVideoTelephonyAvailable(int subId) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007804 final long identity = Binder.clearCallingIdentity();
7805 try {
7806 Phone phone = getPhone(subId);
7807 if (phone != null) {
7808 return phone.isVideoEnabled();
7809 } else {
7810 return false;
7811 }
7812 } finally {
7813 Binder.restoreCallingIdentity(identity);
Brad Ebinger1f2b5082018-02-08 16:11:32 -08007814 }
7815 }
7816
7817 /**
7818 * @return the IMS registration technology for the MMTEL feature. Valid return values are
7819 * defined in {@link ImsRegistrationImplBase}.
7820 */
7821 public @ImsRegistrationImplBase.ImsRegistrationTech int getImsRegTechnologyForMmTel(int subId) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007822 final long identity = Binder.clearCallingIdentity();
7823 try {
7824 Phone phone = getPhone(subId);
7825 if (phone != null) {
7826 return phone.getImsRegistrationTech();
7827 } else {
7828 return ImsRegistrationImplBase.REGISTRATION_TECH_NONE;
7829 }
7830 } finally {
7831 Binder.restoreCallingIdentity(identity);
Brad Ebinger1f2b5082018-02-08 16:11:32 -08007832 }
Etan Cohen3b7a1bc2015-05-28 15:57:13 -07007833 }
7834
Stuart Scott8eef64f2015-04-08 15:13:54 -07007835 @Override
Sarah Chinecc78c42022-03-31 21:16:48 -07007836 public void factoryReset(int subId, String callingPackage) {
paulhu5a773602019-08-23 19:17:33 +08007837 enforceSettingsPermission();
Stuart Scott981d8582015-04-21 14:09:50 -07007838 if (mUserManager.hasUserRestriction(UserManager.DISALLOW_NETWORK_RESET)) {
7839 return;
7840 }
Kai Shif70f46f2021-03-03 13:59:46 -08007841 Phone defaultPhone = getDefaultPhone();
7842 if (defaultPhone != null) {
7843 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
7844 mApp, getDefaultPhone().getSubId(), "factoryReset");
7845 }
Svet Ganovcc087f82015-05-12 20:35:54 -07007846 final long identity = Binder.clearCallingIdentity();
Nazanin Bakhshif782e562018-12-11 15:15:39 -08007847
Svet Ganovcc087f82015-05-12 20:35:54 -07007848 try {
Stuart Scott981d8582015-04-21 14:09:50 -07007849 if (SubscriptionManager.isUsableSubIdValue(subId) && !mUserManager.hasUserRestriction(
7850 UserManager.DISALLOW_CONFIG_MOBILE_NETWORKS)) {
Sooraj Sasindran95c07a92020-07-15 01:35:24 -07007851 setDataEnabledForReason(subId, TelephonyManager.DATA_ENABLED_REASON_USER,
Sarah Chinecc78c42022-03-31 21:16:48 -07007852 getDefaultDataEnabled(), callingPackage);
Svet Ganovcc087f82015-05-12 20:35:54 -07007853 setNetworkSelectionModeAutomatic(subId);
SongFerngWang8c6e82e2021-03-02 22:09:29 +08007854 Phone phone = getPhone(subId);
SongFerngWangfd89b102021-05-27 22:44:54 +08007855 cleanUpAllowedNetworkTypes(phone, subId);
Nazanin Bakhshif782e562018-12-11 15:15:39 -08007856 setDataRoamingEnabled(subId, getDefaultDataRoamingEnabled(subId));
Jordan Liu857e73a2021-03-05 16:24:04 -08007857 getPhone(subId).resetCarrierKeysForImsiEncryption();
Svet Ganovcc087f82015-05-12 20:35:54 -07007858 }
Amit Mahajan7dbbd822019-03-13 17:33:47 -07007859 // There has been issues when Sms raw table somehow stores orphan
7860 // fragments. They lead to garbled message when new fragments come
7861 // in and combined with those stale ones. In case this happens again,
7862 // user can reset all network settings which will clean up this table.
7863 cleanUpSmsRawTable(getDefaultPhone().getContext());
Brad Ebingerbc7dd582019-10-17 17:03:22 -07007864 // Clean up IMS settings as well here.
7865 int slotId = getSlotIndex(subId);
7866 if (slotId > SubscriptionManager.INVALID_SIM_SLOT_INDEX) {
7867 ImsManager.getInstance(mApp, slotId).factoryReset();
7868 }
Naina Nallurid63128d2019-09-17 14:10:30 -07007869
Kai Shif70f46f2021-03-03 13:59:46 -08007870 if (defaultPhone == null) {
7871 return;
7872 }
Naina Nallurid63128d2019-09-17 14:10:30 -07007873 // Erase modem config if erase modem on network setting is enabled.
7874 String configValue = DeviceConfig.getProperty(DeviceConfig.NAMESPACE_TELEPHONY,
7875 RESET_NETWORK_ERASE_MODEM_CONFIG_ENABLED);
7876 if (configValue != null && Boolean.parseBoolean(configValue)) {
Kai Shif70f46f2021-03-03 13:59:46 -08007877 sendEraseModemConfig(defaultPhone);
Naina Nallurid63128d2019-09-17 14:10:30 -07007878 }
Kai Shif70f46f2021-03-03 13:59:46 -08007879
7880 sendEraseDataInSharedPreferences(defaultPhone);
Svet Ganovcc087f82015-05-12 20:35:54 -07007881 } finally {
7882 Binder.restoreCallingIdentity(identity);
Stuart Scott8eef64f2015-04-08 15:13:54 -07007883 }
7884 }
Narayan Kamath1c496c22015-04-16 14:40:19 +01007885
SongFerngWangfd89b102021-05-27 22:44:54 +08007886 @VisibleForTesting
7887 void cleanUpAllowedNetworkTypes(Phone phone, int subId) {
7888 if (phone == null || !SubscriptionManager.isUsableSubscriptionId(subId)) {
7889 return;
7890 }
7891 long defaultNetworkType = RadioAccessFamily.getRafFromNetworkType(
7892 RILConstants.PREFERRED_NETWORK_MODE);
7893 SubscriptionManager.setSubscriptionProperty(subId,
7894 SubscriptionManager.ALLOWED_NETWORK_TYPES,
7895 "user=" + defaultNetworkType);
7896 phone.loadAllowedNetworksFromSubscriptionDatabase();
7897 phone.setAllowedNetworkTypes(TelephonyManager.ALLOWED_NETWORK_TYPES_REASON_USER,
7898 defaultNetworkType, null);
7899 }
7900
Amit Mahajan7dbbd822019-03-13 17:33:47 -07007901 private void cleanUpSmsRawTable(Context context) {
7902 ContentResolver resolver = context.getContentResolver();
7903 Uri uri = Uri.withAppendedPath(Telephony.Sms.CONTENT_URI, "raw/permanentDelete");
7904 resolver.delete(uri, null, null);
7905 }
7906
Narayan Kamath1c496c22015-04-16 14:40:19 +01007907 @Override
chen xu5d3637b2019-01-21 23:31:38 -08007908 public String getSimLocaleForSubscriber(int subId) {
7909 enforceReadPrivilegedPermission("getSimLocaleForSubscriber, subId: " + subId);
7910 final Phone phone = getPhone(subId);
7911 if (phone == null) {
7912 log("getSimLocaleForSubscriber, invalid subId");
chen xu2bb91e42019-01-24 14:35:54 -08007913 return null;
chen xu5d3637b2019-01-21 23:31:38 -08007914 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007915 final long identity = Binder.clearCallingIdentity();
7916 try {
Jack Yu285100e2022-12-02 22:48:35 -08007917 SubscriptionInfo info;
7918 if (PhoneFactory.isSubscriptionManagerServiceEnabled()) {
7919 info = SubscriptionManagerService.getInstance().getActiveSubscriptionInfo(subId,
7920 phone.getContext().getOpPackageName(),
7921 phone.getContext().getAttributionTag());
7922 if (info == null) {
7923 log("getSimLocaleForSubscriber, inactive subId: " + subId);
7924 return null;
7925 }
7926 } else {
7927 info = mSubscriptionController.getActiveSubscriptionInfo(subId,
7928 phone.getContext().getOpPackageName(),
7929 phone.getContext().getAttributionTag());
7930 if (info == null) {
7931 log("getSimLocaleForSubscriber, inactive subId: " + subId);
7932 return null;
7933 }
chen xu6291c472019-02-04 12:55:53 -08007934 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007935 // Try and fetch the locale from the carrier properties or from the SIM language
7936 // preferences (EF-PL and EF-LI)...
7937 final int mcc = info.getMcc();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007938 String simLanguage = null;
chen xu5d3637b2019-01-21 23:31:38 -08007939 final Locale localeFromDefaultSim = phone.getLocaleFromSimAndCarrierPrefs();
7940 if (localeFromDefaultSim != null) {
7941 if (!localeFromDefaultSim.getCountry().isEmpty()) {
7942 if (DBG) log("Using locale from subId: " + subId + " locale: "
7943 + localeFromDefaultSim);
tom hsu60a8dc52022-10-27 00:10:04 +08007944 return matchLocaleFromSupportedLocaleList(phone, localeFromDefaultSim);
chen xu5d3637b2019-01-21 23:31:38 -08007945 } else {
7946 simLanguage = localeFromDefaultSim.getLanguage();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007947 }
7948 }
Narayan Kamath1c496c22015-04-16 14:40:19 +01007949
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007950 // The SIM language preferences only store a language (e.g. fr = French), not an
7951 // exact locale (e.g. fr_FR = French/France). So, if the locale returned from
7952 // the SIM and carrier preferences does not include a country we add the country
7953 // determined from the SIM MCC to provide an exact locale.
zoey chenc730df82019-12-18 17:07:20 +08007954 final Locale mccLocale = LocaleUtils.getLocaleFromMcc(mApp, mcc, simLanguage);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007955 if (mccLocale != null) {
chen xu5d3637b2019-01-21 23:31:38 -08007956 if (DBG) log("No locale from SIM, using mcc locale:" + mccLocale);
tom hsu60a8dc52022-10-27 00:10:04 +08007957 return matchLocaleFromSupportedLocaleList(phone, mccLocale);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007958 }
7959
7960 if (DBG) log("No locale found - returning null");
7961 return null;
7962 } finally {
7963 Binder.restoreCallingIdentity(identity);
7964 }
Narayan Kamath1c496c22015-04-16 14:40:19 +01007965 }
7966
tom hsu0b59d292022-09-29 23:49:21 +08007967 @VisibleForTesting
tom hsu60a8dc52022-10-27 00:10:04 +08007968 String matchLocaleFromSupportedLocaleList(Phone phone, @NonNull Locale inputLocale) {
tom hsu0b59d292022-09-29 23:49:21 +08007969 String[] supportedLocale = com.android.internal.app.LocalePicker.getSupportedLocales(
tom hsu60a8dc52022-10-27 00:10:04 +08007970 phone.getContext());
tom hsu0b59d292022-09-29 23:49:21 +08007971 for (String localeTag : supportedLocale) {
tom hsu60a8dc52022-10-27 00:10:04 +08007972 if (LocaleList.matchesLanguageAndScript(inputLocale, Locale.forLanguageTag(localeTag))
7973 && TextUtils.equals(inputLocale.getCountry(),
tom hsu0b59d292022-09-29 23:49:21 +08007974 Locale.forLanguageTag(localeTag).getCountry())) {
7975 return localeTag;
7976 }
7977 }
7978 return inputLocale.toLanguageTag();
7979 }
7980
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007981 /**
7982 * NOTE: this method assumes permission checks are done and caller identity has been cleared.
7983 */
7984 private List<SubscriptionInfo> getActiveSubscriptionInfoListPrivileged() {
Jack Yu285100e2022-12-02 22:48:35 -08007985 if (PhoneFactory.isSubscriptionManagerServiceEnabled()) {
7986 return SubscriptionManagerService.getInstance().getActiveSubscriptionInfoList(
7987 mApp.getOpPackageName(), mApp.getAttributionTag());
7988 }
Philip P. Moltmann700a9592019-10-03 11:53:50 -07007989 return mSubscriptionController.getActiveSubscriptionInfoList(mApp.getOpPackageName(),
Philip P. Moltmann8d34f0c2020-03-05 16:24:02 -08007990 mApp.getAttributionTag());
Narayan Kamath1c496c22015-04-16 14:40:19 +01007991 }
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -07007992
Gary Jian3aa9a762022-01-24 16:41:19 +08007993 private ActivityStatsTechSpecificInfo[] mLastModemActivitySpecificInfo = null;
7994 private ModemActivityInfo mLastModemActivityInfo = null;
Chenjie Yu1ba97252018-01-11 18:16:20 -08007995
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -07007996 /**
Adam Lesinski903a54c2016-04-11 14:49:52 -07007997 * Responds to the ResultReceiver with the {@link android.telephony.ModemActivityInfo} object
7998 * representing the state of the modem.
7999 *
Chenjie Yu1ba97252018-01-11 18:16:20 -08008000 * NOTE: The underlying implementation clears the modem state, so there should only ever be one
8001 * caller to it. Everyone should call this class to get cumulative data.
Adam Lesinski903a54c2016-04-11 14:49:52 -07008002 * @hide
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -07008003 */
8004 @Override
Adam Lesinski903a54c2016-04-11 14:49:52 -07008005 public void requestModemActivityInfo(ResultReceiver result) {
8006 enforceModifyPermission();
vagdeviaf9a5b92018-08-15 16:01:53 -07008007 WorkSource workSource = getWorkSource(Binder.getCallingUid());
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008008
8009 final long identity = Binder.clearCallingIdentity();
8010 try {
Shuo Qian8f4750a2020-02-20 17:12:10 -08008011 sendRequestAsync(CMD_GET_MODEM_ACTIVITY_INFO, result, null, workSource);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008012 } finally {
8013 Binder.restoreCallingIdentity(identity);
Chenjie Yu1ba97252018-01-11 18:16:20 -08008014 }
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -07008015 }
Jack Yu85bd38a2015-11-09 11:34:32 -08008016
Siddharth Rayb8114062018-06-17 15:02:38 -07008017 // Checks that ModemActivityInfo is valid. Sleep time, Idle time, Rx time and Tx time should be
8018 // less than total activity duration.
8019 private boolean isModemActivityInfoValid(ModemActivityInfo info) {
8020 if (info == null) {
8021 return false;
8022 }
8023 int activityDurationMs =
Hall Liu49656c02020-10-09 19:00:11 -07008024 (int) (info.getTimestampMillis() - mLastModemActivityInfo.getTimestampMillis());
8025 int totalTxTimeMs = Arrays.stream(info.getTransmitTimeMillis()).sum();
8026
Siddharth Rayb8114062018-06-17 15:02:38 -07008027 return (info.isValid()
8028 && (info.getSleepTimeMillis() <= activityDurationMs)
8029 && (info.getIdleTimeMillis() <= activityDurationMs)
Chen Xud78231e2019-09-10 18:49:52 -07008030 && (info.getReceiveTimeMillis() <= activityDurationMs)
Siddharth Rayb8114062018-06-17 15:02:38 -07008031 && (totalTxTimeMs <= activityDurationMs));
8032 }
8033
Gary Jian3aa9a762022-01-24 16:41:19 +08008034 private void updateLastModemActivityInfo(ModemActivityInfo info, int rat, int freq) {
8035 int[] mergedTxTimeMs = new int [ModemActivityInfo.getNumTxPowerLevels()];
8036 int[] txTimeMs = info.getTransmitTimeMillis(rat, freq);
8037 int[] lastModemTxTimeMs = mLastModemActivityInfo.getTransmitTimeMillis(rat, freq);
8038
8039 for (int lvl = 0; lvl < mergedTxTimeMs.length; lvl++) {
8040 mergedTxTimeMs[lvl] = txTimeMs[lvl] + lastModemTxTimeMs[lvl];
8041 }
8042
8043 mLastModemActivityInfo.setTransmitTimeMillis(rat, freq, mergedTxTimeMs);
8044 mLastModemActivityInfo.setReceiveTimeMillis(
8045 rat,
8046 freq,
8047 info.getReceiveTimeMillis(rat, freq)
8048 + mLastModemActivityInfo.getReceiveTimeMillis(rat, freq));
8049 }
8050
8051 private void updateLastModemActivityInfo(ModemActivityInfo info, int rat) {
8052 int[] mergedTxTimeMs = new int [ModemActivityInfo.getNumTxPowerLevels()];
8053 int[] txTimeMs = info.getTransmitTimeMillis(rat);
8054 int[] lastModemTxTimeMs = mLastModemActivityInfo.getTransmitTimeMillis(rat);
8055
8056 for (int lvl = 0; lvl < mergedTxTimeMs.length; lvl++) {
8057 mergedTxTimeMs[lvl] = txTimeMs[lvl] + lastModemTxTimeMs[lvl];
8058 }
8059 mLastModemActivityInfo.setTransmitTimeMillis(rat, mergedTxTimeMs);
8060 mLastModemActivityInfo.setReceiveTimeMillis(
8061 rat,
8062 info.getReceiveTimeMillis(rat) + mLastModemActivityInfo.getReceiveTimeMillis(rat));
8063 }
8064
8065 /**
8066 * Merge this ModemActivityInfo with mLastModemActivitySpecificInfo
8067 * @param info recent ModemActivityInfo
8068 */
8069 private void mergeModemActivityInfo(ModemActivityInfo info) {
8070 List<ActivityStatsTechSpecificInfo> merged = new ArrayList<>();
Kai Shi917fdc62022-11-28 14:01:02 -08008071 ActivityStatsTechSpecificInfo deltaSpecificInfo;
Gary Jian3aa9a762022-01-24 16:41:19 +08008072 boolean matched;
8073 for (int i = 0; i < info.getSpecificInfoLength(); i++) {
8074 matched = false;
8075 int rat = info.getSpecificInfoRat(i);
8076 int freq = info.getSpecificInfoFrequencyRange(i);
8077 //Check each ActivityStatsTechSpecificInfo in this ModemActivityInfo for new rat returns
8078 //Add a new ActivityStatsTechSpecificInfo if is a new rat, and merge with the original
8079 //if it already exists
8080 for (int j = 0; j < mLastModemActivitySpecificInfo.length; j++) {
8081 if (rat == mLastModemActivityInfo.getSpecificInfoRat(j) && !matched) {
8082 //Merged based on frequency range (MMWAVE vs SUB6) for 5G
8083 if (rat == AccessNetworkConstants.AccessNetworkType.NGRAN) {
8084 if (freq == mLastModemActivityInfo.getSpecificInfoFrequencyRange(j)) {
8085 updateLastModemActivityInfo(info, rat, freq);
8086 matched = true;
8087 }
8088 } else {
8089 updateLastModemActivityInfo(info, rat);
8090 matched = true;
8091 }
8092 }
8093 }
8094
8095 if (!matched) {
Kai Shi917fdc62022-11-28 14:01:02 -08008096 deltaSpecificInfo =
Gary Jian3aa9a762022-01-24 16:41:19 +08008097 new ActivityStatsTechSpecificInfo(
8098 rat,
8099 freq,
8100 info.getTransmitTimeMillis(rat, freq),
8101 (int) info.getReceiveTimeMillis(rat, freq));
Kai Shi917fdc62022-11-28 14:01:02 -08008102 merged.addAll(Arrays.asList(deltaSpecificInfo));
Gary Jian3aa9a762022-01-24 16:41:19 +08008103 }
8104 }
8105 merged.addAll(Arrays.asList(mLastModemActivitySpecificInfo));
8106 mLastModemActivitySpecificInfo =
8107 new ActivityStatsTechSpecificInfo[merged.size()];
8108 merged.toArray(mLastModemActivitySpecificInfo);
8109
8110 mLastModemActivityInfo.setTimestamp(info.getTimestampMillis());
8111 mLastModemActivityInfo.setSleepTimeMillis(
8112 info.getSleepTimeMillis()
8113 + mLastModemActivityInfo.getSleepTimeMillis());
8114 mLastModemActivityInfo.setIdleTimeMillis(
8115 info.getIdleTimeMillis()
8116 + mLastModemActivityInfo.getIdleTimeMillis());
Kai Shi917fdc62022-11-28 14:01:02 -08008117
8118 mLastModemActivityInfo =
8119 new ModemActivityInfo(
8120 mLastModemActivityInfo.getTimestampMillis(),
8121 mLastModemActivityInfo.getSleepTimeMillis(),
8122 mLastModemActivityInfo.getIdleTimeMillis(),
8123 mLastModemActivitySpecificInfo);
8124 }
8125
8126 private ActivityStatsTechSpecificInfo[] deepCopyModemActivitySpecificInfo(
8127 ActivityStatsTechSpecificInfo[] info) {
8128 int infoSize = info.length;
8129 ActivityStatsTechSpecificInfo[] ret = new ActivityStatsTechSpecificInfo[infoSize];
8130 for (int i = 0; i < infoSize; i++) {
8131 ret[i] = new ActivityStatsTechSpecificInfo(
8132 info[i].getRat(), info[i].getFrequencyRange(),
8133 info[i].getTransmitTimeMillis(),
8134 (int) info[i].getReceiveTimeMillis());
8135 }
8136 return ret;
Gary Jian3aa9a762022-01-24 16:41:19 +08008137 }
8138
Jack Yu85bd38a2015-11-09 11:34:32 -08008139 /**
Jack Yu85bd38a2015-11-09 11:34:32 -08008140 * Returns the service state information on specified subscription.
8141 */
8142 @Override
Sooraj Sasindran2250dc02021-11-10 16:42:01 -08008143 public ServiceState getServiceStateForSubscriber(int subId,
8144 boolean renounceFineLocationAccess, boolean renounceCoarseLocationAccess,
8145 String callingPackage, String callingFeatureId) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08008146 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
Philip P. Moltmann700a9592019-10-03 11:53:50 -07008147 mApp, subId, callingPackage, callingFeatureId, "getServiceStateForSubscriber")) {
Jack Yu85bd38a2015-11-09 11:34:32 -08008148 return null;
8149 }
8150
Sooraj Sasindran2250dc02021-11-10 16:42:01 -08008151 boolean hasFinePermission = false;
8152 boolean hasCoarsePermission = false;
8153 if (!renounceFineLocationAccess) {
8154 LocationAccessPolicy.LocationPermissionResult fineLocationResult =
8155 LocationAccessPolicy.checkLocationPermission(mApp,
8156 new LocationAccessPolicy.LocationPermissionQuery.Builder()
8157 .setCallingPackage(callingPackage)
8158 .setCallingFeatureId(callingFeatureId)
8159 .setCallingPid(Binder.getCallingPid())
8160 .setCallingUid(Binder.getCallingUid())
8161 .setMethod("getServiceStateForSubscriber")
8162 .setLogAsInfo(true)
8163 .setMinSdkVersionForFine(Build.VERSION_CODES.Q)
8164 .setMinSdkVersionForCoarse(Build.VERSION_CODES.Q)
8165 .setMinSdkVersionForEnforcement(Build.VERSION_CODES.Q)
8166 .build());
8167 hasFinePermission =
8168 fineLocationResult == LocationAccessPolicy.LocationPermissionResult.ALLOWED;
8169 }
Hall Liuf19c44f2018-11-27 14:38:17 -08008170
Sooraj Sasindran2250dc02021-11-10 16:42:01 -08008171 if (!renounceCoarseLocationAccess) {
8172 LocationAccessPolicy.LocationPermissionResult coarseLocationResult =
8173 LocationAccessPolicy.checkLocationPermission(mApp,
8174 new LocationAccessPolicy.LocationPermissionQuery.Builder()
8175 .setCallingPackage(callingPackage)
8176 .setCallingFeatureId(callingFeatureId)
8177 .setCallingPid(Binder.getCallingPid())
8178 .setCallingUid(Binder.getCallingUid())
8179 .setMethod("getServiceStateForSubscriber")
8180 .setLogAsInfo(true)
8181 .setMinSdkVersionForCoarse(Build.VERSION_CODES.Q)
8182 .setMinSdkVersionForFine(Integer.MAX_VALUE)
8183 .setMinSdkVersionForEnforcement(Build.VERSION_CODES.Q)
8184 .build());
8185 hasCoarsePermission =
8186 coarseLocationResult == LocationAccessPolicy.LocationPermissionResult.ALLOWED;
8187 }
Hall Liuf19c44f2018-11-27 14:38:17 -08008188
Jack Yu479f40e2020-10-27 21:29:25 -07008189 final Phone phone = getPhone(subId);
8190 if (phone == null) {
8191 return null;
8192 }
8193
Jordan Liu0f2bc442020-11-18 16:47:37 -08008194 final long identity = Binder.clearCallingIdentity();
8195
Jack Yu479f40e2020-10-27 21:29:25 -07008196 boolean isCallingPackageDataService = phone.getDataServicePackages()
8197 .contains(callingPackage);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008198 try {
Jordan Liuc437b192020-08-17 10:59:12 -07008199 // isActiveSubId requires READ_PHONE_STATE, which we already check for above
Jack Yu285100e2022-12-02 22:48:35 -08008200 if (PhoneFactory.isSubscriptionManagerServiceEnabled()) {
8201 SubscriptionInfoInternal subInfo = SubscriptionManagerService.getInstance()
8202 .getSubscriptionInfoInternal(subId);
8203 if (subInfo == null || !subInfo.isActive()) {
8204 Rlog.d(LOG_TAG, "getServiceStateForSubscriber returning null for inactive "
8205 + "subId=" + subId);
8206 return null;
8207 }
8208 } else {
8209 if (!mSubscriptionController.isActiveSubId(subId, callingPackage,
8210 callingFeatureId)) {
8211 Rlog.d(LOG_TAG, "getServiceStateForSubscriber returning null for inactive "
8212 + "subId=" + subId);
8213 return null;
8214 }
Jordan Liuc437b192020-08-17 10:59:12 -07008215 }
8216
Hall Liuf19c44f2018-11-27 14:38:17 -08008217 ServiceState ss = phone.getServiceState();
8218
8219 // Scrub out the location info in ServiceState depending on what level of access
8220 // the caller has.
Jack Yu479f40e2020-10-27 21:29:25 -07008221 if (hasFinePermission || isCallingPackageDataService) return ss;
Malcolm Chen5052de62019-12-30 13:56:38 -08008222 if (hasCoarsePermission) return ss.createLocationInfoSanitizedCopy(false);
8223 return ss.createLocationInfoSanitizedCopy(true);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008224 } finally {
8225 Binder.restoreCallingIdentity(identity);
8226 }
Jack Yu85bd38a2015-11-09 11:34:32 -08008227 }
Nancy Chen31f9ba12016-01-06 11:42:12 -08008228
8229 /**
8230 * Returns the URI for the per-account voicemail ringtone set in Phone settings.
8231 *
8232 * @param accountHandle The handle for the {@link PhoneAccount} for which to retrieve the
8233 * voicemail ringtone.
8234 * @return The URI for the ringtone to play when receiving a voicemail from a specific
8235 * PhoneAccount.
8236 */
8237 @Override
8238 public Uri getVoicemailRingtoneUri(PhoneAccountHandle accountHandle) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008239 final long identity = Binder.clearCallingIdentity();
8240 try {
8241 Phone phone = PhoneUtils.getPhoneForPhoneAccountHandle(accountHandle);
8242 if (phone == null) {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08008243 phone = getDefaultPhone();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008244 }
Nancy Chen31f9ba12016-01-06 11:42:12 -08008245
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008246 return VoicemailNotificationSettingsUtil.getRingtoneUri(phone.getContext());
8247 } finally {
8248 Binder.restoreCallingIdentity(identity);
8249 }
Nancy Chen31f9ba12016-01-06 11:42:12 -08008250 }
8251
8252 /**
Ta-wei Yenc33877d2017-01-23 18:11:21 -08008253 * Sets the per-account voicemail ringtone.
8254 *
8255 * <p>Requires that the calling app is the default dialer, or has carrier privileges, or
8256 * has permission {@link android.Manifest.permission#MODIFY_PHONE_STATE MODIFY_PHONE_STATE}.
8257 *
8258 * @param phoneAccountHandle The handle for the {@link PhoneAccount} for which to set the
8259 * voicemail ringtone.
8260 * @param uri The URI for the ringtone to play when receiving a voicemail from a specific
8261 * PhoneAccount.
8262 */
8263 @Override
8264 public void setVoicemailRingtoneUri(String callingPackage,
8265 PhoneAccountHandle phoneAccountHandle, Uri uri) {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08008266 final Phone defaultPhone = getDefaultPhone();
Ta-wei Yenc33877d2017-01-23 18:11:21 -08008267 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
Tyler Gunn5ddfdc92019-10-31 13:08:23 -07008268 TelecomManager tm = defaultPhone.getContext().getSystemService(TelecomManager.class);
8269 if (!TextUtils.equals(callingPackage, tm.getDefaultDialerPackage())) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08008270 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
8271 mApp, PhoneUtils.getSubIdForPhoneAccountHandle(phoneAccountHandle),
8272 "setVoicemailRingtoneUri");
Ta-wei Yenc33877d2017-01-23 18:11:21 -08008273 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008274
8275 final long identity = Binder.clearCallingIdentity();
8276 try {
8277 Phone phone = PhoneUtils.getPhoneForPhoneAccountHandle(phoneAccountHandle);
8278 if (phone == null) {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08008279 phone = defaultPhone;
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008280 }
8281 VoicemailNotificationSettingsUtil.setRingtoneUri(phone.getContext(), uri);
8282 } finally {
8283 Binder.restoreCallingIdentity(identity);
Ta-wei Yenc33877d2017-01-23 18:11:21 -08008284 }
Ta-wei Yenc33877d2017-01-23 18:11:21 -08008285 }
8286
8287 /**
Nancy Chen31f9ba12016-01-06 11:42:12 -08008288 * Returns whether vibration is set for voicemail notification in Phone settings.
8289 *
8290 * @param accountHandle The handle for the {@link PhoneAccount} for which to retrieve the
8291 * voicemail vibration setting.
8292 * @return {@code true} if the vibration is set for this PhoneAccount, {@code false} otherwise.
8293 */
8294 @Override
8295 public boolean isVoicemailVibrationEnabled(PhoneAccountHandle accountHandle) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008296 final long identity = Binder.clearCallingIdentity();
8297 try {
8298 Phone phone = PhoneUtils.getPhoneForPhoneAccountHandle(accountHandle);
8299 if (phone == null) {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08008300 phone = getDefaultPhone();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008301 }
Nancy Chen31f9ba12016-01-06 11:42:12 -08008302
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008303 return VoicemailNotificationSettingsUtil.isVibrationEnabled(phone.getContext());
8304 } finally {
8305 Binder.restoreCallingIdentity(identity);
8306 }
Nancy Chen31f9ba12016-01-06 11:42:12 -08008307 }
8308
Youhan Wange64578a2016-05-02 15:32:42 -07008309 /**
Ta-wei Yenc33877d2017-01-23 18:11:21 -08008310 * Sets the per-account voicemail vibration.
8311 *
8312 * <p>Requires that the calling app is the default dialer, or has carrier privileges, or
8313 * has permission {@link android.Manifest.permission#MODIFY_PHONE_STATE MODIFY_PHONE_STATE}.
8314 *
8315 * @param phoneAccountHandle The handle for the {@link PhoneAccount} for which to set the
8316 * voicemail vibration setting.
8317 * @param enabled Whether to enable or disable vibration for voicemail notifications from a
8318 * specific PhoneAccount.
8319 */
8320 @Override
8321 public void setVoicemailVibrationEnabled(String callingPackage,
8322 PhoneAccountHandle phoneAccountHandle, boolean enabled) {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08008323 final Phone defaultPhone = getDefaultPhone();
Ta-wei Yenc33877d2017-01-23 18:11:21 -08008324 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
Tyler Gunn5ddfdc92019-10-31 13:08:23 -07008325 TelecomManager tm = defaultPhone.getContext().getSystemService(TelecomManager.class);
8326 if (!TextUtils.equals(callingPackage, tm.getDefaultDialerPackage())) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08008327 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
8328 mApp, PhoneUtils.getSubIdForPhoneAccountHandle(phoneAccountHandle),
8329 "setVoicemailVibrationEnabled");
Ta-wei Yenc33877d2017-01-23 18:11:21 -08008330 }
8331
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008332 final long identity = Binder.clearCallingIdentity();
8333 try {
8334 Phone phone = PhoneUtils.getPhoneForPhoneAccountHandle(phoneAccountHandle);
8335 if (phone == null) {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08008336 phone = defaultPhone;
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008337 }
8338 VoicemailNotificationSettingsUtil.setVibrationEnabled(phone.getContext(), enabled);
8339 } finally {
8340 Binder.restoreCallingIdentity(identity);
Ta-wei Yenc33877d2017-01-23 18:11:21 -08008341 }
Ta-wei Yenc33877d2017-01-23 18:11:21 -08008342 }
8343
8344 /**
Youhan Wange64578a2016-05-02 15:32:42 -07008345 * Make sure either called from same process as self (phone) or IPC caller has read privilege.
8346 *
8347 * @throws SecurityException if the caller does not have the required permission
8348 */
Brad Ebinger35c841c2018-10-01 10:40:55 -07008349 private void enforceReadPrivilegedPermission(String message) {
Youhan Wange64578a2016-05-02 15:32:42 -07008350 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.READ_PRIVILEGED_PHONE_STATE,
Brad Ebinger35c841c2018-10-01 10:40:55 -07008351 message);
Youhan Wange64578a2016-05-02 15:32:42 -07008352 }
8353
8354 /**
Ta-wei Yen30a69c82016-12-27 14:52:32 -08008355 * Make sure either called from same process as self (phone) or IPC caller has send SMS
8356 * permission.
8357 *
8358 * @throws SecurityException if the caller does not have the required permission
8359 */
8360 private void enforceSendSmsPermission() {
8361 mApp.enforceCallingOrSelfPermission(permission.SEND_SMS, null);
8362 }
8363
8364 /**
Aishwarya Mallampatifbc70d32022-11-10 20:33:02 +00008365 * Make sure either called from same process as self (phone) or IPC caller has interact across
8366 * users permission.
8367 *
8368 * @throws SecurityException if the caller does not have the required permission
8369 */
8370 private void enforceInteractAcrossUsersPermission(String message) {
8371 mApp.enforceCallingOrSelfPermission(permission.INTERACT_ACROSS_USERS, message);
8372 }
8373
8374 /**
Ta-wei Yen527a9c02017-01-06 15:29:25 -08008375 * Make sure called from the package in charge of visual voicemail.
Ta-wei Yen30a69c82016-12-27 14:52:32 -08008376 *
Ta-wei Yen527a9c02017-01-06 15:29:25 -08008377 * @throws SecurityException if the caller is not the visual voicemail package.
Ta-wei Yen30a69c82016-12-27 14:52:32 -08008378 */
Ta-wei Yen527a9c02017-01-06 15:29:25 -08008379 private void enforceVisualVoicemailPackage(String callingPackage, int subId) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008380 final long identity = Binder.clearCallingIdentity();
8381 try {
8382 ComponentName componentName =
Nazanin Bakhshif782e562018-12-11 15:15:39 -08008383 RemoteVvmTaskManager.getRemotePackage(mApp, subId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008384 if (componentName == null) {
8385 throw new SecurityException(
8386 "Caller not current active visual voicemail package[null]");
8387 }
8388 String vvmPackage = componentName.getPackageName();
8389 if (!callingPackage.equals(vvmPackage)) {
Hui Wang7f657552022-08-16 16:58:25 +00008390 throw new SecurityException("Caller not current active visual voicemail package");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008391 }
8392 } finally {
8393 Binder.restoreCallingIdentity(identity);
Ta-wei Yen30a69c82016-12-27 14:52:32 -08008394 }
8395 }
8396
8397 /**
Youhan Wange64578a2016-05-02 15:32:42 -07008398 * Return the application ID for the app type.
8399 *
8400 * @param subId the subscription ID that this request applies to.
8401 * @param appType the uicc app type.
8402 * @return Application ID for specificied app type, or null if no uicc.
8403 */
8404 @Override
8405 public String getAidForAppType(int subId, int appType) {
Brad Ebinger35c841c2018-10-01 10:40:55 -07008406 enforceReadPrivilegedPermission("getAidForAppType");
Youhan Wange64578a2016-05-02 15:32:42 -07008407 Phone phone = getPhone(subId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008408
8409 final long identity = Binder.clearCallingIdentity();
Youhan Wange64578a2016-05-02 15:32:42 -07008410 try {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008411 if (phone == null) {
8412 return null;
8413 }
8414 String aid = null;
8415 try {
Muralidhar Reddy472c2ae2021-09-29 19:38:40 +00008416 aid = UiccController.getInstance().getUiccPort(phone.getPhoneId())
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008417 .getApplicationByType(appType).getAid();
8418 } catch (Exception e) {
8419 Log.e(LOG_TAG, "Not getting aid. Exception ex=" + e);
8420 }
8421 return aid;
8422 } finally {
8423 Binder.restoreCallingIdentity(identity);
Youhan Wange64578a2016-05-02 15:32:42 -07008424 }
Youhan Wange64578a2016-05-02 15:32:42 -07008425 }
8426
Youhan Wang4001d252016-05-11 10:29:41 -07008427 /**
8428 * Return the Electronic Serial Number.
8429 *
8430 * @param subId the subscription ID that this request applies to.
8431 * @return ESN or null if error.
8432 */
8433 @Override
8434 public String getEsn(int subId) {
Brad Ebinger35c841c2018-10-01 10:40:55 -07008435 enforceReadPrivilegedPermission("getEsn");
Youhan Wang4001d252016-05-11 10:29:41 -07008436 Phone phone = getPhone(subId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008437
8438 final long identity = Binder.clearCallingIdentity();
Youhan Wang4001d252016-05-11 10:29:41 -07008439 try {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008440 if (phone == null) {
8441 return null;
8442 }
8443 String esn = null;
8444 try {
8445 esn = phone.getEsn();
8446 } catch (Exception e) {
8447 Log.e(LOG_TAG, "Not getting ESN. Exception ex=" + e);
8448 }
8449 return esn;
8450 } finally {
8451 Binder.restoreCallingIdentity(identity);
Youhan Wang4001d252016-05-11 10:29:41 -07008452 }
Youhan Wang4001d252016-05-11 10:29:41 -07008453 }
8454
Sanket Padawe99ef1e32016-05-18 16:12:33 -07008455 /**
Youhan Wang66ad5d72016-07-18 17:56:58 -07008456 * Return the Preferred Roaming List Version.
8457 *
8458 * @param subId the subscription ID that this request applies to.
8459 * @return PRLVersion or null if error.
8460 */
8461 @Override
8462 public String getCdmaPrlVersion(int subId) {
Brad Ebinger35c841c2018-10-01 10:40:55 -07008463 enforceReadPrivilegedPermission("getCdmaPrlVersion");
Youhan Wang66ad5d72016-07-18 17:56:58 -07008464 Phone phone = getPhone(subId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008465
8466 final long identity = Binder.clearCallingIdentity();
Youhan Wang66ad5d72016-07-18 17:56:58 -07008467 try {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008468 if (phone == null) {
8469 return null;
8470 }
8471 String cdmaPrlVersion = null;
8472 try {
8473 cdmaPrlVersion = phone.getCdmaPrlVersion();
8474 } catch (Exception e) {
8475 Log.e(LOG_TAG, "Not getting PRLVersion", e);
8476 }
8477 return cdmaPrlVersion;
8478 } finally {
8479 Binder.restoreCallingIdentity(identity);
Youhan Wang66ad5d72016-07-18 17:56:58 -07008480 }
Youhan Wang66ad5d72016-07-18 17:56:58 -07008481 }
8482
8483 /**
Sanket Padawe99ef1e32016-05-18 16:12:33 -07008484 * Get snapshot of Telephony histograms
8485 * @return List of Telephony histograms
8486 * @hide
8487 */
8488 @Override
8489 public List<TelephonyHistogram> getTelephonyHistograms() {
Jeff Davidson7e17e312018-02-13 18:17:36 -08008490 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
8491 mApp, getDefaultSubscription(), "getTelephonyHistograms");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008492
8493 final long identity = Binder.clearCallingIdentity();
8494 try {
8495 return RIL.getTelephonyRILTimingHistograms();
8496 } finally {
8497 Binder.restoreCallingIdentity(identity);
8498 }
Sanket Padawe99ef1e32016-05-18 16:12:33 -07008499 }
Meng Wang1a7c35a2016-05-05 20:56:15 -07008500
8501 /**
8502 * {@hide}
Michele Berionne482f8202018-11-27 18:57:59 -08008503 * Set the allowed carrier list and the excluded carrier list, indicating the priority between
8504 * the two lists.
Meng Wang1a7c35a2016-05-05 20:56:15 -07008505 * Require system privileges. In the future we may add this to carrier APIs.
8506 *
Michele Berionne482f8202018-11-27 18:57:59 -08008507 * @return Integer with the result of the operation, as defined in {@link TelephonyManager}.
Meng Wang1a7c35a2016-05-05 20:56:15 -07008508 */
8509 @Override
Michele Berionne482f8202018-11-27 18:57:59 -08008510 @TelephonyManager.SetCarrierRestrictionResult
8511 public int setAllowedCarriers(CarrierRestrictionRules carrierRestrictionRules) {
Meng Wang1a7c35a2016-05-05 20:56:15 -07008512 enforceModifyPermission();
vagdeviaf9a5b92018-08-15 16:01:53 -07008513 WorkSource workSource = getWorkSource(Binder.getCallingUid());
Sanket Padawe13bac7b2017-03-20 15:04:47 -07008514
Michele Berionne482f8202018-11-27 18:57:59 -08008515 if (carrierRestrictionRules == null) {
8516 throw new NullPointerException("carrier restriction cannot be null");
Meng Wang9b7c4e92017-02-17 11:41:27 -08008517 }
Sanket Padawe13bac7b2017-03-20 15:04:47 -07008518
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008519 final long identity = Binder.clearCallingIdentity();
8520 try {
Michele Berionne482f8202018-11-27 18:57:59 -08008521 return (int) sendRequest(CMD_SET_ALLOWED_CARRIERS, carrierRestrictionRules,
vagdeviaf9a5b92018-08-15 16:01:53 -07008522 workSource);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008523 } finally {
8524 Binder.restoreCallingIdentity(identity);
8525 }
Meng Wang1a7c35a2016-05-05 20:56:15 -07008526 }
8527
8528 /**
8529 * {@hide}
Michele Berionne482f8202018-11-27 18:57:59 -08008530 * Get the allowed carrier list and the excluded carrier list, including the priority between
8531 * the two lists.
Meng Wang1a7c35a2016-05-05 20:56:15 -07008532 * Require system privileges. In the future we may add this to carrier APIs.
8533 *
Michele Berionne482f8202018-11-27 18:57:59 -08008534 * @return {@link android.telephony.CarrierRestrictionRules}
Meng Wang1a7c35a2016-05-05 20:56:15 -07008535 */
8536 @Override
Michele Berionne482f8202018-11-27 18:57:59 -08008537 public CarrierRestrictionRules getAllowedCarriers() {
Brad Ebinger35c841c2018-10-01 10:40:55 -07008538 enforceReadPrivilegedPermission("getAllowedCarriers");
vagdeviaf9a5b92018-08-15 16:01:53 -07008539 WorkSource workSource = getWorkSource(Binder.getCallingUid());
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008540
8541 final long identity = Binder.clearCallingIdentity();
8542 try {
Michele Berionne482f8202018-11-27 18:57:59 -08008543 Object response = sendRequest(CMD_GET_ALLOWED_CARRIERS, null, workSource);
8544 if (response instanceof CarrierRestrictionRules) {
8545 return (CarrierRestrictionRules) response;
8546 }
8547 // Response is an Exception of some kind,
8548 // which is signalled to the user as a NULL retval
8549 return null;
8550 } catch (Exception e) {
8551 Log.e(LOG_TAG, "getAllowedCarriers. Exception ex=" + e);
8552 return null;
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008553 } finally {
8554 Binder.restoreCallingIdentity(identity);
8555 }
Meng Wang1a7c35a2016-05-05 20:56:15 -07008556 }
8557
fionaxu59545b42016-05-25 15:53:37 -07008558 /**
fionaxu59545b42016-05-25 15:53:37 -07008559 * Action set from carrier signalling broadcast receivers to enable/disable radio
8560 * @param subId the subscription ID that this action applies to.
8561 * @param enabled control enable or disable radio.
8562 * {@hide}
8563 */
8564 @Override
8565 public void carrierActionSetRadioEnabled(int subId, boolean enabled) {
8566 enforceModifyPermission();
8567 final Phone phone = getPhone(subId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008568
8569 final long identity = Binder.clearCallingIdentity();
fionaxu59545b42016-05-25 15:53:37 -07008570 if (phone == null) {
8571 loge("carrierAction: SetRadioEnabled fails with invalid sibId: " + subId);
8572 return;
8573 }
8574 try {
8575 phone.carrierActionSetRadioEnabled(enabled);
8576 } catch (Exception e) {
8577 Log.e(LOG_TAG, "carrierAction: SetRadioEnabled fails. Exception ex=" + e);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008578 } finally {
8579 Binder.restoreCallingIdentity(identity);
fionaxu59545b42016-05-25 15:53:37 -07008580 }
8581 }
8582
Ta-wei Yenc236d6b2016-06-21 13:33:12 -07008583 /**
Sooraj Sasindrandaf060f2021-06-15 14:52:55 -07008584 * Enable or disable Voice over NR (VoNR)
8585 * @param subId the subscription ID that this action applies to.
8586 * @param enabled enable or disable VoNR.
8587 * @return operation result.
8588 */
8589 @Override
8590 public int setVoNrEnabled(int subId, boolean enabled) {
8591 enforceModifyPermission();
8592 final Phone phone = getPhone(subId);
8593
8594 final long identity = Binder.clearCallingIdentity();
8595 if (phone == null) {
8596 loge("setVoNrEnabled fails with no phone object for subId: " + subId);
8597 return TelephonyManager.ENABLE_VONR_RADIO_NOT_AVAILABLE;
8598 }
8599
8600 WorkSource workSource = getWorkSource(Binder.getCallingUid());
8601 try {
8602 int result = (int) sendRequest(CMD_ENABLE_VONR, enabled, subId,
8603 workSource);
8604 if (DBG) log("setVoNrEnabled result: " + result);
Gary Jian8dd305f2021-10-14 16:31:35 +08008605
8606 if (result == TelephonyManager.ENABLE_VONR_SUCCESS) {
8607 if (DBG) {
8608 log("Set VoNR settings in siminfo db; subId=" + subId + ", value:" + enabled);
8609 }
8610 SubscriptionManager.setSubscriptionProperty(
8611 subId, SubscriptionManager.NR_ADVANCED_CALLING_ENABLED,
8612 (enabled ? "1" : "0"));
8613 }
8614
Sooraj Sasindrandaf060f2021-06-15 14:52:55 -07008615 return result;
8616 } finally {
8617 Binder.restoreCallingIdentity(identity);
8618 }
8619 }
8620
8621 /**
8622 * Is voice over NR enabled
8623 * @return true if VoNR is enabled else false
8624 */
8625 @Override
8626 public boolean isVoNrEnabled(int subId) {
8627 enforceReadPrivilegedPermission("isVoNrEnabled");
8628 WorkSource workSource = getWorkSource(Binder.getCallingUid());
8629 final long identity = Binder.clearCallingIdentity();
8630 try {
8631 boolean isEnabled = (boolean) sendRequest(CMD_IS_VONR_ENABLED,
8632 null, subId, workSource);
8633 if (DBG) log("isVoNrEnabled: " + isEnabled);
8634 return isEnabled;
8635 } finally {
8636 Binder.restoreCallingIdentity(identity);
8637 }
8638 }
8639
8640 /**
fionaxu8da9cb12017-05-23 15:02:46 -07008641 * Action set from carrier signalling broadcast receivers to start/stop reporting the default
8642 * network status based on which carrier apps could apply actions accordingly,
8643 * enable/disable default url handler for example.
8644 *
8645 * @param subId the subscription ID that this action applies to.
8646 * @param report control start/stop reporting the default network status.
8647 * {@hide}
8648 */
8649 @Override
8650 public void carrierActionReportDefaultNetworkStatus(int subId, boolean report) {
8651 enforceModifyPermission();
8652 final Phone phone = getPhone(subId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008653
8654 final long identity = Binder.clearCallingIdentity();
fionaxu8da9cb12017-05-23 15:02:46 -07008655 if (phone == null) {
8656 loge("carrierAction: ReportDefaultNetworkStatus fails with invalid sibId: " + subId);
8657 return;
8658 }
8659 try {
8660 phone.carrierActionReportDefaultNetworkStatus(report);
8661 } catch (Exception e) {
8662 Log.e(LOG_TAG, "carrierAction: ReportDefaultNetworkStatus fails. Exception ex=" + e);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008663 } finally {
8664 Binder.restoreCallingIdentity(identity);
fionaxu8da9cb12017-05-23 15:02:46 -07008665 }
8666 }
8667
8668 /**
fionaxud9622282017-07-17 17:51:30 -07008669 * Action set from carrier signalling broadcast receivers to reset all carrier actions
8670 * @param subId the subscription ID that this action applies to.
8671 * {@hide}
8672 */
8673 @Override
8674 public void carrierActionResetAll(int subId) {
8675 enforceModifyPermission();
8676 final Phone phone = getPhone(subId);
8677 if (phone == null) {
8678 loge("carrierAction: ResetAll fails with invalid sibId: " + subId);
8679 return;
8680 }
8681 try {
8682 phone.carrierActionResetAll();
8683 } catch (Exception e) {
8684 Log.e(LOG_TAG, "carrierAction: ResetAll fails. Exception ex=" + e);
8685 }
8686 }
8687
8688 /**
Ta-wei Yenc236d6b2016-06-21 13:33:12 -07008689 * Called when "adb shell dumpsys phone" is invoked. Dump is also automatically invoked when a
8690 * bug report is being generated.
8691 */
8692 @Override
Ta-wei Yen99282e02016-06-21 18:19:35 -07008693 protected void dump(FileDescriptor fd, PrintWriter writer, String[] args) {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08008694 if (mApp.checkCallingOrSelfPermission(android.Manifest.permission.DUMP)
8695 != PackageManager.PERMISSION_GRANTED) {
dcashman22b950d2016-06-27 11:39:02 -07008696 writer.println("Permission Denial: can't dump Phone from pid="
8697 + Binder.getCallingPid()
8698 + ", uid=" + Binder.getCallingUid()
8699 + "without permission "
8700 + android.Manifest.permission.DUMP);
8701 return;
8702 }
Nazanin Bakhshif782e562018-12-11 15:15:39 -08008703 DumpsysHandler.dump(mApp, fd, writer, args);
Ta-wei Yenc236d6b2016-06-21 13:33:12 -07008704 }
Jack Yueb89b242016-06-22 13:27:47 -07008705
Brad Ebingerdac2f002018-04-03 15:17:52 -07008706 @Override
Hall Liua1548bd2019-12-24 14:14:12 -08008707 public int handleShellCommand(@NonNull ParcelFileDescriptor in,
8708 @NonNull ParcelFileDescriptor out, @NonNull ParcelFileDescriptor err,
8709 @NonNull String[] args) {
8710 return new TelephonyShellCommand(this, getDefaultPhone().getContext()).exec(
8711 this, in.getFileDescriptor(), out.getFileDescriptor(),
8712 err.getFileDescriptor(), args);
Brad Ebingerdac2f002018-04-03 15:17:52 -07008713 }
8714
Jack Yueb89b242016-06-22 13:27:47 -07008715 /**
Sooraj Sasindranaf1b5132020-05-05 21:06:20 +00008716 * Policy control of data connection with reason {@@TelephonyManager.DataEnabledReason}
Greg Kaiser17f41752020-05-05 16:47:47 +00008717 * @param subId Subscription index
Sarah Chinecc78c42022-03-31 21:16:48 -07008718 * @param reason The reason the data enable change is taking place.
Sooraj Sasindranaf1b5132020-05-05 21:06:20 +00008719 * @param enabled True if enabling the data, otherwise disabling.
Sarah Chinecc78c42022-03-31 21:16:48 -07008720 * @param callingPackage The package that changed the data enabled state.
Sooraj Sasindranaf1b5132020-05-05 21:06:20 +00008721 * @hide
Jack Yu75ab2952016-07-08 14:29:33 -07008722 */
8723 @Override
Sooraj Sasindran95c07a92020-07-15 01:35:24 -07008724 public void setDataEnabledForReason(int subId, @TelephonyManager.DataEnabledReason int reason,
Sarah Chinecc78c42022-03-31 21:16:48 -07008725 boolean enabled, String callingPackage) {
Sooraj Sasindranaf1b5132020-05-05 21:06:20 +00008726 if (reason == TelephonyManager.DATA_ENABLED_REASON_USER
8727 || reason == TelephonyManager.DATA_ENABLED_REASON_CARRIER) {
8728 try {
8729 TelephonyPermissions.enforceCallingOrSelfCarrierPrivilege(
Sooraj Sasindran95c07a92020-07-15 01:35:24 -07008730 mApp, subId, "setDataEnabledForReason");
Sooraj Sasindranaf1b5132020-05-05 21:06:20 +00008731 } catch (SecurityException se) {
8732 enforceModifyPermission();
8733 }
8734 } else {
8735 enforceModifyPermission();
8736 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008737
8738 final long identity = Binder.clearCallingIdentity();
8739 try {
8740 Phone phone = getPhone(subId);
8741 if (phone != null) {
Sooraj Sasindranaf1b5132020-05-05 21:06:20 +00008742 if (reason == TelephonyManager.DATA_ENABLED_REASON_CARRIER) {
8743 phone.carrierActionSetMeteredApnsEnabled(enabled);
8744 } else {
Jack Yu7968c6d2022-07-31 00:43:21 -07008745 phone.getDataSettingsManager().setDataEnabled(
8746 reason, enabled, callingPackage);
Sooraj Sasindranaf1b5132020-05-05 21:06:20 +00008747 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008748 }
8749 } finally {
8750 Binder.restoreCallingIdentity(identity);
Jack Yu75ab2952016-07-08 14:29:33 -07008751 }
8752 }
Sooraj Sasindran9a909312016-07-18 11:57:25 -07008753
8754 /**
8755 * Get Client request stats
8756 * @return List of Client Request Stats
8757 * @hide
8758 */
8759 @Override
Philip P. Moltmann700a9592019-10-03 11:53:50 -07008760 public List<ClientRequestStats> getClientRequestStats(String callingPackage,
8761 String callingFeatureId, int subId) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08008762 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
Philip P. Moltmann700a9592019-10-03 11:53:50 -07008763 mApp, subId, callingPackage, callingFeatureId, "getClientRequestStats")) {
Sooraj Sasindran9a909312016-07-18 11:57:25 -07008764 return null;
8765 }
Sooraj Sasindran9a909312016-07-18 11:57:25 -07008766 Phone phone = getPhone(subId);
Sooraj Sasindran9a909312016-07-18 11:57:25 -07008767
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008768 final long identity = Binder.clearCallingIdentity();
8769 try {
8770 if (phone != null) {
8771 return phone.getClientRequestStats();
8772 }
8773
8774 return null;
8775 } finally {
8776 Binder.restoreCallingIdentity(identity);
8777 }
Sooraj Sasindran9a909312016-07-18 11:57:25 -07008778 }
8779
Narayan Kamathf04b5a12018-01-09 11:47:15 +00008780 private WorkSource getWorkSource(int uid) {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08008781 String packageName = mApp.getPackageManager().getNameForUid(uid);
Hunter Knepshieldd03383b2022-03-29 22:47:54 +00008782 if (uid == Process.ROOT_UID && packageName == null) {
8783 // Downstream WorkSource attribution inside the RIL requires both a UID and package name
8784 // to be set for wakelock tracking, otherwise RIL requests fail with a runtime
8785 // exception. ROOT_UID seems not to have a valid package name returned by
8786 // PackageManager, so just fake it here to avoid issues when running telephony shell
8787 // commands that plumb through the RIL as root, like so:
8788 // $ adb root
8789 // $ adb shell cmd phone ...
8790 packageName = "root";
8791 }
Narayan Kamathf04b5a12018-01-09 11:47:15 +00008792 return new WorkSource(uid, packageName);
Sooraj Sasindran9a909312016-07-18 11:57:25 -07008793 }
Jack Yueb4124c2017-02-16 15:32:43 -08008794
8795 /**
Grace Chen70990072017-03-24 17:21:30 -07008796 * Set SIM card power state.
Jack Yueb4124c2017-02-16 15:32:43 -08008797 *
Sanket Padawe13bac7b2017-03-20 15:04:47 -07008798 * @param slotIndex SIM slot id.
Grace Chen70990072017-03-24 17:21:30 -07008799 * @param state State of SIM (power down, power up, pass through)
8800 * - {@link android.telephony.TelephonyManager#CARD_POWER_DOWN}
8801 * - {@link android.telephony.TelephonyManager#CARD_POWER_UP}
8802 * - {@link android.telephony.TelephonyManager#CARD_POWER_UP_PASS_THROUGH}
Jack Yueb4124c2017-02-16 15:32:43 -08008803 *
8804 **/
8805 @Override
Grace Chen70990072017-03-24 17:21:30 -07008806 public void setSimPowerStateForSlot(int slotIndex, int state) {
Jack Yueb4124c2017-02-16 15:32:43 -08008807 enforceModifyPermission();
Sanket Padawe13bac7b2017-03-20 15:04:47 -07008808 Phone phone = PhoneFactory.getPhone(slotIndex);
8809
vagdeviaf9a5b92018-08-15 16:01:53 -07008810 WorkSource workSource = getWorkSource(Binder.getCallingUid());
8811
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008812 final long identity = Binder.clearCallingIdentity();
8813 try {
8814 if (phone != null) {
Jordan Liu109698e2020-11-24 14:50:34 -08008815 phone.setSimPowerState(state, null, workSource);
8816 }
8817 } finally {
8818 Binder.restoreCallingIdentity(identity);
8819 }
8820 }
8821
8822 /**
8823 * Set SIM card power state.
8824 *
8825 * @param slotIndex SIM slot id.
8826 * @param state State of SIM (power down, power up, pass through)
8827 * @param callback callback to trigger after success or failure
8828 * - {@link android.telephony.TelephonyManager#CARD_POWER_DOWN}
8829 * - {@link android.telephony.TelephonyManager#CARD_POWER_UP}
8830 * - {@link android.telephony.TelephonyManager#CARD_POWER_UP_PASS_THROUGH}
8831 *
8832 **/
8833 @Override
8834 public void setSimPowerStateForSlotWithCallback(int slotIndex, int state,
8835 IIntegerConsumer callback) {
8836 enforceModifyPermission();
8837 Phone phone = PhoneFactory.getPhone(slotIndex);
8838
8839 WorkSource workSource = getWorkSource(Binder.getCallingUid());
8840
8841 final long identity = Binder.clearCallingIdentity();
8842 try {
8843 if (phone != null) {
8844 Pair<Integer, IIntegerConsumer> arguments = Pair.create(state, callback);
8845 sendRequestAsync(CMD_SET_SIM_POWER, arguments, phone, workSource);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008846 }
8847 } finally {
8848 Binder.restoreCallingIdentity(identity);
Jack Yueb4124c2017-02-16 15:32:43 -08008849 }
8850 }
Shuo Qiandd210312017-04-12 22:11:33 +00008851
Tyler Gunn65d45c22017-06-05 11:22:26 -07008852 private boolean isUssdApiAllowed(int subId) {
8853 CarrierConfigManager configManager =
Nazanin Bakhshif782e562018-12-11 15:15:39 -08008854 (CarrierConfigManager) mApp.getSystemService(Context.CARRIER_CONFIG_SERVICE);
Tyler Gunn65d45c22017-06-05 11:22:26 -07008855 if (configManager == null) {
8856 return false;
8857 }
8858 PersistableBundle pb = configManager.getConfigForSubId(subId);
8859 if (pb == null) {
8860 return false;
8861 }
8862 return pb.getBoolean(
8863 CarrierConfigManager.KEY_ALLOW_USSD_REQUESTS_VIA_TELEPHONY_MANAGER_BOOL);
8864 }
8865
Shuo Qiandd210312017-04-12 22:11:33 +00008866 /**
8867 * Check if phone is in emergency callback mode
8868 * @return true if phone is in emergency callback mode
8869 * @param subId sub id
8870 */
goneil9c5f4872017-12-05 14:07:56 -08008871 @Override
Shuo Qiandd210312017-04-12 22:11:33 +00008872 public boolean getEmergencyCallbackMode(int subId) {
Brad Ebinger35c841c2018-10-01 10:40:55 -07008873 enforceReadPrivilegedPermission("getEmergencyCallbackMode");
Shuo Qiandd210312017-04-12 22:11:33 +00008874 final Phone phone = getPhone(subId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008875
8876 final long identity = Binder.clearCallingIdentity();
8877 try {
8878 if (phone != null) {
8879 return phone.isInEcm();
8880 } else {
8881 return false;
8882 }
8883 } finally {
8884 Binder.restoreCallingIdentity(identity);
Shuo Qiandd210312017-04-12 22:11:33 +00008885 }
8886 }
Nathan Harold46b42aa2017-03-10 19:38:22 -08008887
8888 /**
8889 * Get the current signal strength information for the given subscription.
8890 * Because this information is not updated when the device is in a low power state
8891 * it should not be relied-upon to be current.
8892 * @param subId Subscription index
8893 * @return the most recent cached signal strength info from the modem
8894 */
8895 @Override
8896 public SignalStrength getSignalStrength(int subId) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008897 final long identity = Binder.clearCallingIdentity();
8898 try {
8899 Phone p = getPhone(subId);
8900 if (p == null) {
8901 return null;
8902 }
Nathan Harold46b42aa2017-03-10 19:38:22 -08008903
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008904 return p.getSignalStrength();
8905 } finally {
8906 Binder.restoreCallingIdentity(identity);
8907 }
Nathan Harold46b42aa2017-03-10 19:38:22 -08008908 }
Holly Jiuyu Sun01c47ad2018-01-24 17:56:33 +00008909
Pengquan Meng77b7f132018-08-22 14:49:57 -07008910 /**
Chen Xuf792fd62018-10-17 17:54:36 +00008911 * Get the current modem radio state for the given slot.
8912 * @param slotIndex slot index.
8913 * @param callingPackage the name of the package making the call.
Philip P. Moltmann700a9592019-10-03 11:53:50 -07008914 * @param callingFeatureId The feature in the package.
Chen Xuf792fd62018-10-17 17:54:36 +00008915 * @return the current radio power state from the modem
8916 */
8917 @Override
Philip P. Moltmann700a9592019-10-03 11:53:50 -07008918 public int getRadioPowerState(int slotIndex, String callingPackage, String callingFeatureId) {
Chen Xuf792fd62018-10-17 17:54:36 +00008919 Phone phone = PhoneFactory.getPhone(slotIndex);
8920 if (phone != null) {
Philip P. Moltmann700a9592019-10-03 11:53:50 -07008921 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(mApp, phone.getSubId(),
8922 callingPackage, callingFeatureId, "getRadioPowerState")) {
Chen Xuf792fd62018-10-17 17:54:36 +00008923 return TelephonyManager.RADIO_POWER_UNAVAILABLE;
8924 }
8925
8926 final long identity = Binder.clearCallingIdentity();
8927 try {
8928 return phone.getRadioPowerState();
8929 } finally {
8930 Binder.restoreCallingIdentity(identity);
8931 }
8932 }
8933 return TelephonyManager.RADIO_POWER_UNAVAILABLE;
8934 }
8935
8936 /**
Pengquan Meng77b7f132018-08-22 14:49:57 -07008937 * Checks if data roaming is enabled on the subscription with id {@code subId}.
8938 *
8939 * <p>Requires one of the following permissions:
8940 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE},
Sooraj Sasindran5d051bf2021-11-05 13:14:15 -07008941 * {@link android.Manifest.permission#READ_BASIC_PHONE_STATE},
Pengquan Meng77b7f132018-08-22 14:49:57 -07008942 * {@link android.Manifest.permission#READ_PHONE_STATE} or that the calling app has carrier
8943 * privileges.
8944 *
8945 * @param subId subscription id
8946 * @return {@code true} if data roaming is enabled on this subscription, otherwise return
8947 * {@code false}.
8948 */
8949 @Override
8950 public boolean isDataRoamingEnabled(int subId) {
Sooraj Sasindran5d051bf2021-11-05 13:14:15 -07008951 String functionName = "isDataRoamingEnabled";
Shuo Qian093013d2020-08-13 15:42:55 -07008952 try {
Sooraj Sasindran5d051bf2021-11-05 13:14:15 -07008953 try {
8954 mApp.enforceCallingOrSelfPermission(
8955 android.Manifest.permission.ACCESS_NETWORK_STATE,
8956 functionName);
Sooraj Sasindran12545cc2022-08-22 14:30:25 -07008957 } catch (SecurityException e) {
Sooraj Sasindran5d051bf2021-11-05 13:14:15 -07008958 mApp.enforceCallingOrSelfPermission(
8959 permission.READ_BASIC_PHONE_STATE, functionName);
8960 }
Sooraj Sasindran12545cc2022-08-22 14:30:25 -07008961 } catch (SecurityException e) {
Nathan Harold62c68512021-04-06 11:26:02 -07008962 TelephonyPermissions.enforceCallingOrSelfReadPhoneStatePermissionOrCarrierPrivilege(
Sooraj Sasindran5d051bf2021-11-05 13:14:15 -07008963 mApp, subId, functionName);
Shuo Qian093013d2020-08-13 15:42:55 -07008964 }
Pengquan Meng44e66f12019-04-01 10:48:20 -07008965
Pengquan Menga1bb6272018-09-06 09:59:22 -07008966 boolean isEnabled = false;
8967 final long identity = Binder.clearCallingIdentity();
Pengquan Meng77b7f132018-08-22 14:49:57 -07008968 try {
Pengquan Menga1bb6272018-09-06 09:59:22 -07008969 Phone phone = getPhone(subId);
8970 isEnabled = phone != null ? phone.getDataRoamingEnabled() : false;
Pengquan Menga1bb6272018-09-06 09:59:22 -07008971 } finally {
8972 Binder.restoreCallingIdentity(identity);
Pengquan Meng77b7f132018-08-22 14:49:57 -07008973 }
Pengquan Menga1bb6272018-09-06 09:59:22 -07008974 return isEnabled;
Pengquan Meng77b7f132018-08-22 14:49:57 -07008975 }
8976
8977
8978 /**
8979 * Enables/Disables the data roaming on the subscription with id {@code subId}.
8980 *
8981 * <p> Requires permission:
8982 * {@link android.Manifest.permission#MODIFY_PHONE_STATE} or that the calling app has carrier
8983 * privileges.
8984 *
8985 * @param subId subscription id
8986 * @param isEnabled {@code true} means enable, {@code false} means disable.
8987 */
8988 @Override
8989 public void setDataRoamingEnabled(int subId, boolean isEnabled) {
Pengquan Meng44e66f12019-04-01 10:48:20 -07008990 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
8991 mApp, subId, "setDataRoamingEnabled");
8992
Pengquan Menga1bb6272018-09-06 09:59:22 -07008993 final long identity = Binder.clearCallingIdentity();
8994 try {
Pengquan Menga1bb6272018-09-06 09:59:22 -07008995 Phone phone = getPhone(subId);
8996 if (phone != null) {
8997 phone.setDataRoamingEnabled(isEnabled);
8998 }
8999 } finally {
9000 Binder.restoreCallingIdentity(identity);
Pengquan Meng77b7f132018-08-22 14:49:57 -07009001 }
9002 }
9003
Holly Jiuyu Sun01c47ad2018-01-24 17:56:33 +00009004 @Override
Pengquan Meng6884a2c2018-10-03 12:19:13 -07009005 public boolean isManualNetworkSelectionAllowed(int subId) {
tom hsuc91afc72020-01-06 23:46:07 +08009006 TelephonyPermissions
Nathan Harold62c68512021-04-06 11:26:02 -07009007 .enforceCallingOrSelfReadPrivilegedPhoneStatePermissionOrCarrierPrivilege(
Pengquan Meng44e66f12019-04-01 10:48:20 -07009008 mApp, subId, "isManualNetworkSelectionAllowed");
9009
Pengquan Meng6884a2c2018-10-03 12:19:13 -07009010 boolean isAllowed = true;
9011 final long identity = Binder.clearCallingIdentity();
9012 try {
Pengquan Meng6884a2c2018-10-03 12:19:13 -07009013 Phone phone = getPhone(subId);
9014 if (phone != null) {
9015 isAllowed = phone.isCspPlmnEnabled();
9016 }
9017 } finally {
9018 Binder.restoreCallingIdentity(identity);
9019 }
9020 return isAllowed;
9021 }
9022
Muralidhar Reddyeb809e32021-11-19 03:07:54 +00009023 private boolean haveCarrierPrivilegeAccess(UiccPort port, String callingPackage) {
9024 UiccProfile profile = port.getUiccProfile();
Hunter Knepshieldcad7f0b2021-12-13 15:08:35 -08009025 if (profile == null) {
Muralidhar Reddyeb809e32021-11-19 03:07:54 +00009026 return false;
Muralidhar Reddy472c2ae2021-09-29 19:38:40 +00009027 }
Hunter Knepshieldcad7f0b2021-12-13 15:08:35 -08009028 Phone phone = PhoneFactory.getPhone(profile.getPhoneId());
9029 if (phone == null) {
9030 return false;
9031 }
9032 CarrierPrivilegesTracker cpt = phone.getCarrierPrivilegesTracker();
9033 return cpt != null && cpt.getCarrierPrivilegeStatusForPackage(callingPackage)
9034 == TelephonyManager.CARRIER_PRIVILEGE_STATUS_HAS_ACCESS;
Muralidhar Reddy472c2ae2021-09-29 19:38:40 +00009035 }
9036
Pengquan Meng6884a2c2018-10-03 12:19:13 -07009037 @Override
Jordan Liu75f43ea2019-01-17 16:56:37 -08009038 public List<UiccCardInfo> getUiccCardsInfo(String callingPackage) {
sandeepjsa208e3b2021-11-17 04:05:58 +00009039 // Verify that the callingPackage belongs to the calling UID
Jordan Liu4cda4552020-03-23 11:55:07 -07009040 mApp.getSystemService(AppOpsManager.class)
9041 .checkPackage(Binder.getCallingUid(), callingPackage);
9042
Jordan Liu1e142fc2019-04-22 15:10:43 -07009043 boolean hasReadPermission = false;
sandeepjsb6c87872021-09-27 15:34:44 +00009044 boolean isIccIdAccessRestricted = false;
Jordan Liuc65bc952019-02-12 17:54:02 -08009045 try {
9046 enforceReadPrivilegedPermission("getUiccCardsInfo");
Jordan Liu1e142fc2019-04-22 15:10:43 -07009047 hasReadPermission = true;
Jordan Liuc65bc952019-02-12 17:54:02 -08009048 } catch (SecurityException e) {
9049 // even without READ_PRIVILEGED_PHONE_STATE, we allow the call to continue if the caller
9050 // has carrier privileges on an active UICC
Rambo Wange7209ce2022-02-23 13:41:02 -08009051 if (checkCarrierPrivilegesForPackageAnyPhoneWithPermission(callingPackage)
Jordan Liuc65bc952019-02-12 17:54:02 -08009052 != TelephonyManager.CARRIER_PRIVILEGE_STATUS_HAS_ACCESS) {
Jordan Liu1e142fc2019-04-22 15:10:43 -07009053 throw new SecurityException("Caller does not have permission.");
Jordan Liuc65bc952019-02-12 17:54:02 -08009054 }
Jordan Liu75f43ea2019-01-17 16:56:37 -08009055 }
sandeepjsb6c87872021-09-27 15:34:44 +00009056 // checking compatibility, if calling app's target SDK is T and beyond.
9057 if (CompatChanges.isChangeEnabled(GET_API_SIGNATURES_FROM_UICC_PORT_INFO,
9058 Binder.getCallingUid())) {
9059 isIccIdAccessRestricted = true;
9060 }
Jordan Liu5aa07002018-12-18 15:44:48 -08009061 final long identity = Binder.clearCallingIdentity();
9062 try {
Jordan Liu75f43ea2019-01-17 16:56:37 -08009063 UiccController uiccController = UiccController.getInstance();
9064 ArrayList<UiccCardInfo> cardInfos = uiccController.getAllUiccCardInfos();
Jordan Liu1e142fc2019-04-22 15:10:43 -07009065 if (hasReadPermission) {
9066 return cardInfos;
Jordan Liu75f43ea2019-01-17 16:56:37 -08009067 }
Jordan Liu1e142fc2019-04-22 15:10:43 -07009068
9069 // Remove private info if the caller doesn't have access
9070 ArrayList<UiccCardInfo> filteredInfos = new ArrayList<>();
9071 for (UiccCardInfo cardInfo : cardInfos) {
sandeepjsb6c87872021-09-27 15:34:44 +00009072 //setting the value after compatibility check
9073 cardInfo.setIccIdAccessRestricted(isIccIdAccessRestricted);
Jordan Liu1e142fc2019-04-22 15:10:43 -07009074 // For an inactive eUICC, the UiccCard will be null even though the UiccCardInfo
9075 // is available
sandeepjsb6c87872021-09-27 15:34:44 +00009076 UiccCard card = uiccController.getUiccCardForSlot(cardInfo.getPhysicalSlotIndex());
Muralidhar Reddyeb809e32021-11-19 03:07:54 +00009077 if (card == null) {
9078 // assume no access if the card is unavailable
sandeepjsb6c87872021-09-27 15:34:44 +00009079 filteredInfos.add(getUiccCardInfoUnPrivileged(cardInfo));
Jordan Liu1e142fc2019-04-22 15:10:43 -07009080 continue;
9081 }
Muralidhar Reddyeb809e32021-11-19 03:07:54 +00009082 Collection<UiccPortInfo> portInfos = cardInfo.getPorts();
9083 if (portInfos.isEmpty()) {
sandeepjsb6c87872021-09-27 15:34:44 +00009084 filteredInfos.add(getUiccCardInfoUnPrivileged(cardInfo));
Muralidhar Reddyeb809e32021-11-19 03:07:54 +00009085 continue;
Jordan Liu1e142fc2019-04-22 15:10:43 -07009086 }
Muralidhar Reddyeb809e32021-11-19 03:07:54 +00009087 List<UiccPortInfo> uiccPortInfos = new ArrayList<>();
9088 for (UiccPortInfo portInfo : portInfos) {
9089 UiccPort port = uiccController.getUiccPortForSlot(
9090 cardInfo.getPhysicalSlotIndex(), portInfo.getPortIndex());
9091 if (port == null) {
9092 // assume no access if port is null
9093 uiccPortInfos.add(getUiccPortInfoUnPrivileged(portInfo));
9094 continue;
9095 }
9096 if (haveCarrierPrivilegeAccess(port, callingPackage)) {
9097 uiccPortInfos.add(portInfo);
9098 } else {
9099 uiccPortInfos.add(getUiccPortInfoUnPrivileged(portInfo));
9100 }
9101 }
9102 filteredInfos.add(new UiccCardInfo(
9103 cardInfo.isEuicc(),
9104 cardInfo.getCardId(),
9105 null,
9106 cardInfo.getPhysicalSlotIndex(),
9107 cardInfo.isRemovable(),
9108 cardInfo.isMultipleEnabledProfilesSupported(),
9109 uiccPortInfos));
Jordan Liu1e142fc2019-04-22 15:10:43 -07009110 }
9111 return filteredInfos;
Jordan Liu5aa07002018-12-18 15:44:48 -08009112 } finally {
9113 Binder.restoreCallingIdentity(identity);
9114 }
9115 }
9116
sandeepjsb6c87872021-09-27 15:34:44 +00009117 /**
9118 * Returns a copy of the UiccCardinfo with the EID and ICCID set to null. These values are
9119 * generally private and require carrier privileges to view.
9120 *
9121 * @hide
9122 */
9123 @NonNull
9124 public UiccCardInfo getUiccCardInfoUnPrivileged(UiccCardInfo cardInfo) {
9125 List<UiccPortInfo> portinfo = new ArrayList<>();
9126 for (UiccPortInfo portinfos : cardInfo.getPorts()) {
9127 portinfo.add(getUiccPortInfoUnPrivileged(portinfos));
9128 }
9129 return new UiccCardInfo(
9130 cardInfo.isEuicc(),
9131 cardInfo.getCardId(),
9132 null,
9133 cardInfo.getPhysicalSlotIndex(),
9134 cardInfo.isRemovable(),
9135 cardInfo.isMultipleEnabledProfilesSupported(),
9136 portinfo
9137 );
9138 }
Holly Jiuyu Sun01c47ad2018-01-24 17:56:33 +00009139
sandeepjsb6c87872021-09-27 15:34:44 +00009140 /**
9141 * @hide
9142 * @return a copy of the UiccPortInfo with ICCID set to {@link UiccPortInfo#ICCID_REDACTED}.
9143 * These values are generally private and require carrier privileges to view.
9144 */
9145 @NonNull
9146 public UiccPortInfo getUiccPortInfoUnPrivileged(UiccPortInfo portInfo) {
9147 return new UiccPortInfo(
9148 UiccPortInfo.ICCID_REDACTED,
9149 portInfo.getPortIndex(),
9150 portInfo.getLogicalSlotIndex(),
9151 portInfo.isActive()
9152 );
9153 }
9154 @Override
9155 public UiccSlotInfo[] getUiccSlotsInfo(String callingPackage) {
sandeepjsa208e3b2021-11-17 04:05:58 +00009156 // Verify that the callingPackage belongs to the calling UID
sandeepjsb6c87872021-09-27 15:34:44 +00009157 mApp.getSystemService(AppOpsManager.class)
9158 .checkPackage(Binder.getCallingUid(), callingPackage);
9159
sandeepjsb6c87872021-09-27 15:34:44 +00009160 boolean isLogicalSlotAccessRestricted = false;
sandeepjsb6c87872021-09-27 15:34:44 +00009161
Aman Guptaf3c90b32022-03-17 04:54:16 +00009162 // This will make sure caller has the READ_PRIVILEGED_PHONE_STATE. Do not remove this as
9163 // we are reading iccId which is PII data.
9164 enforceReadPrivilegedPermission("getUiccSlotsInfo");
sandeepjsb6c87872021-09-27 15:34:44 +00009165
9166 // checking compatibility, if calling app's target SDK is T and beyond.
9167 if (CompatChanges.isChangeEnabled(GET_API_SIGNATURES_FROM_UICC_PORT_INFO,
9168 Binder.getCallingUid())) {
9169 isLogicalSlotAccessRestricted = true;
9170 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08009171 final long identity = Binder.clearCallingIdentity();
9172 try {
9173 UiccSlot[] slots = UiccController.getInstance().getUiccSlots();
Muralidhar Reddyd196bbf2022-01-17 17:56:30 +00009174 if (slots == null || slots.length == 0) {
9175 Rlog.i(LOG_TAG, "slots is null or empty.");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08009176 return null;
9177 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08009178 UiccSlotInfo[] infos = new UiccSlotInfo[slots.length];
9179 for (int i = 0; i < slots.length; i++) {
9180 UiccSlot slot = slots[i];
9181 if (slot == null) {
9182 continue;
9183 }
9184
Jordan Liu7be7e652019-05-06 18:55:02 +00009185 String cardId;
Malcolm Chenaa4a8532018-02-28 15:00:40 -08009186 UiccCard card = slot.getUiccCard();
9187 if (card != null) {
9188 cardId = card.getCardId();
Jordan Liu7be7e652019-05-06 18:55:02 +00009189 } else {
Jordan Liu01bd00d2019-09-12 16:19:43 -07009190 cardId = slot.getEid();
9191 if (TextUtils.isEmpty(cardId)) {
Aman Guptaf3c90b32022-03-17 04:54:16 +00009192 // If cardId is null, use iccId of default port as cardId.
9193 cardId = slot.getIccId(TelephonyManager.DEFAULT_PORT_INDEX);
Jordan Liu01bd00d2019-09-12 16:19:43 -07009194 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08009195 }
9196
Jordan Liu857451f2019-05-09 16:35:35 -07009197 if (cardId != null) {
9198 // if cardId is an ICCID, strip off trailing Fs before exposing to user
9199 // if cardId is an EID, it's all digits so this is fine
9200 cardId = IccUtils.stripTrailingFs(cardId);
9201 }
9202
Malcolm Chenaa4a8532018-02-28 15:00:40 -08009203 int cardState = 0;
9204 switch (slot.getCardState()) {
9205 case CARDSTATE_ABSENT:
9206 cardState = UiccSlotInfo.CARD_STATE_INFO_ABSENT;
9207 break;
9208 case CARDSTATE_PRESENT:
9209 cardState = UiccSlotInfo.CARD_STATE_INFO_PRESENT;
9210 break;
9211 case CARDSTATE_ERROR:
9212 cardState = UiccSlotInfo.CARD_STATE_INFO_ERROR;
9213 break;
9214 case CARDSTATE_RESTRICTED:
9215 cardState = UiccSlotInfo.CARD_STATE_INFO_RESTRICTED;
9216 break;
9217 default:
9218 break;
9219
9220 }
Muralidhar Reddyeb809e32021-11-19 03:07:54 +00009221 List<UiccPortInfo> portInfos = new ArrayList<>();
9222 int[] portIndexes = slot.getPortList();
9223 for (int portIdx : portIndexes) {
9224 String iccId = IccUtils.stripTrailingFs(getIccId(slot, portIdx,
Aman Guptaf3c90b32022-03-17 04:54:16 +00009225 callingPackage, /* hasReadPermission= */ true));
Muralidhar Reddyfbcff0c2022-01-19 13:07:57 +00009226 portInfos.add(new UiccPortInfo(iccId, portIdx,
9227 slot.getPhoneIdFromPortIndex(portIdx), slot.isPortActive(portIdx)));
Muralidhar Reddyeb809e32021-11-19 03:07:54 +00009228 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08009229 infos[i] = new UiccSlotInfo(
Malcolm Chenaa4a8532018-02-28 15:00:40 -08009230 slot.isEuicc(),
9231 cardId,
9232 cardState,
Jordan Liua2619582019-02-14 12:56:40 -08009233 slot.isExtendedApduSupported(),
Muralidhar Reddyeb809e32021-11-19 03:07:54 +00009234 slot.isRemovable(), portInfos);
sandeepjsb6c87872021-09-27 15:34:44 +00009235 //setting the value after compatibility check
9236 infos[i].setLogicalSlotAccessRestricted(isLogicalSlotAccessRestricted);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08009237 }
9238 return infos;
9239 } finally {
9240 Binder.restoreCallingIdentity(identity);
Holly Jiuyu Sun1d957c52018-04-04 13:52:42 -07009241 }
Holly Jiuyu Sun01c47ad2018-01-24 17:56:33 +00009242 }
9243
Muralidhar Reddyeb809e32021-11-19 03:07:54 +00009244 /* Returns null if doesn't have read permission or carrier privilege access. */
9245 private String getIccId(UiccSlot slot, int portIndex, String callingPackage,
9246 boolean hasReadPermission) {
9247 String iccId = slot.getIccId(portIndex);
9248 if (hasReadPermission) { // if has read permission
9249 return iccId;
9250 } else {
9251 if (slot.getUiccCard() != null && slot.getUiccCard().getUiccPort(portIndex) != null) {
9252 UiccPort port = slot.getUiccCard().getUiccPort(portIndex);
9253 // if no read permission, checking carrier privilege access
9254 if (haveCarrierPrivilegeAccess(port, callingPackage)) {
9255 return iccId;
9256 }
9257 }
9258 }
9259 // No read permission or carrier privilege access.
9260 return UiccPortInfo.ICCID_REDACTED;
9261 }
9262
Holly Jiuyu Sun01c47ad2018-01-24 17:56:33 +00009263 @Override
sandeepjsb6c87872021-09-27 15:34:44 +00009264 @Deprecated
Holly Jiuyu Sun01c47ad2018-01-24 17:56:33 +00009265 public boolean switchSlots(int[] physicalSlots) {
9266 enforceModifyPermission();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08009267
9268 final long identity = Binder.clearCallingIdentity();
9269 try {
Muralidhar Reddyeb809e32021-11-19 03:07:54 +00009270 List<UiccSlotMapping> slotMappings = new ArrayList<>();
9271 for (int i = 0; i < physicalSlots.length; i++) {
9272 // Deprecated API, hence MEP is not supported. Adding default portIndex 0.
9273 slotMappings.add(new UiccSlotMapping(TelephonyManager.DEFAULT_PORT_INDEX,
9274 physicalSlots[i], i));
9275 }
9276 return (Boolean) sendRequest(CMD_SWITCH_SLOTS, slotMappings);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08009277 } finally {
9278 Binder.restoreCallingIdentity(identity);
9279 }
Holly Jiuyu Sun01c47ad2018-01-24 17:56:33 +00009280 }
Jack Yu4c988042018-02-27 15:30:01 -08009281
9282 @Override
sandeepjsb6c87872021-09-27 15:34:44 +00009283 @RequiresPermission(android.Manifest.permission.MODIFY_PHONE_STATE)
9284 public boolean setSimSlotMapping(@NonNull List<UiccSlotMapping> slotMapping) {
9285 enforceModifyPermission();
9286
9287 final long identity = Binder.clearCallingIdentity();
9288 try {
Muralidhar Reddyeb809e32021-11-19 03:07:54 +00009289 return (Boolean) sendRequest(CMD_SWITCH_SLOTS, slotMapping);
sandeepjsb6c87872021-09-27 15:34:44 +00009290 } finally {
9291 Binder.restoreCallingIdentity(identity);
9292 }
9293 }
9294
9295 @Override
Jordan Liu7de49fa2018-12-06 14:48:49 -08009296 public int getCardIdForDefaultEuicc(int subId, String callingPackage) {
Jordan Liu7de49fa2018-12-06 14:48:49 -08009297 final long identity = Binder.clearCallingIdentity();
9298 try {
9299 return UiccController.getInstance().getCardIdForDefaultEuicc();
9300 } finally {
9301 Binder.restoreCallingIdentity(identity);
9302 }
9303 }
9304
Pengquan Meng85728fb2018-03-12 16:31:21 -07009305 /**
goneil47ffb6e2018-04-06 15:40:58 -07009306 * A test API to reload the UICC profile.
9307 *
9308 * <p>Requires that the calling app has permission
9309 * {@link android.Manifest.permission#MODIFY_PHONE_STATE MODIFY_PHONE_STATE}.
9310 * @hide
9311 */
9312 @Override
9313 public void refreshUiccProfile(int subId) {
9314 enforceModifyPermission();
9315
9316 final long identity = Binder.clearCallingIdentity();
9317 try {
9318 Phone phone = getPhone(subId);
9319 if (phone == null) {
9320 return;
9321 }
Muralidhar Reddy472c2ae2021-09-29 19:38:40 +00009322 UiccPort uiccPort = phone.getUiccPort();
9323 if (uiccPort == null) {
goneil47ffb6e2018-04-06 15:40:58 -07009324 return;
9325 }
Muralidhar Reddy472c2ae2021-09-29 19:38:40 +00009326 UiccProfile uiccProfile = uiccPort.getUiccProfile();
goneil47ffb6e2018-04-06 15:40:58 -07009327 if (uiccProfile == null) {
9328 return;
9329 }
9330 uiccProfile.refresh();
9331 } finally {
9332 Binder.restoreCallingIdentity(identity);
9333 }
9334 }
9335
9336 /**
Pengquan Meng85728fb2018-03-12 16:31:21 -07009337 * Returns false if the mobile data is disabled by default, otherwise return true.
9338 */
9339 private boolean getDefaultDataEnabled() {
Inseob Kim14bb3d02018-12-13 17:11:34 +09009340 return TelephonyProperties.mobile_data().orElse(true);
Pengquan Meng85728fb2018-03-12 16:31:21 -07009341 }
9342
9343 /**
9344 * Returns true if the data roaming is enabled by default, i.e the system property
9345 * of {@link #DEFAULT_DATA_ROAMING_PROPERTY_NAME} is true or the config of
9346 * {@link CarrierConfigManager#KEY_CARRIER_DEFAULT_DATA_ROAMING_ENABLED_BOOL} is true.
9347 */
9348 private boolean getDefaultDataRoamingEnabled(int subId) {
9349 final CarrierConfigManager configMgr = (CarrierConfigManager)
Nazanin Bakhshif782e562018-12-11 15:15:39 -08009350 mApp.getSystemService(Context.CARRIER_CONFIG_SERVICE);
Shuo Qian1d84a0e2020-07-15 12:36:44 -07009351 boolean isDataRoamingEnabled = TelephonyProperties.data_roaming().orElse(false);
Pengquan Meng85728fb2018-03-12 16:31:21 -07009352 isDataRoamingEnabled |= configMgr.getConfigForSubId(subId).getBoolean(
9353 CarrierConfigManager.KEY_CARRIER_DEFAULT_DATA_ROAMING_ENABLED_BOOL);
9354 return isDataRoamingEnabled;
9355 }
9356
9357 /**
9358 * Returns the default network type for the given {@code subId}, if the default network type is
9359 * not set, return {@link Phone#PREFERRED_NT_MODE}.
9360 */
9361 private int getDefaultNetworkType(int subId) {
Inseob Kim14bb3d02018-12-13 17:11:34 +09009362 List<Integer> list = TelephonyProperties.default_network();
Jack Yu285100e2022-12-02 22:48:35 -08009363 int phoneId = SubscriptionManager.getPhoneId(subId);
Inseob Kim14bb3d02018-12-13 17:11:34 +09009364 if (phoneId >= 0 && phoneId < list.size() && list.get(phoneId) != null) {
9365 return list.get(phoneId);
9366 }
9367 return Phone.PREFERRED_NT_MODE;
Pengquan Meng85728fb2018-03-12 16:31:21 -07009368 }
fionaxua13278b2018-03-21 00:08:13 -07009369
9370 @Override
9371 public void setCarrierTestOverride(int subId, String mccmnc, String imsi, String iccid, String
chen xueaba88a2019-03-15 13:15:10 -07009372 gid1, String gid2, String plmn, String spn, String carrierPrivilegeRules, String apn) {
fionaxua13278b2018-03-21 00:08:13 -07009373 enforceModifyPermission();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08009374
9375 final long identity = Binder.clearCallingIdentity();
9376 try {
9377 final Phone phone = getPhone(subId);
9378 if (phone == null) {
9379 loge("setCarrierTestOverride fails with invalid subId: " + subId);
9380 return;
9381 }
Rambo Wang9c9ffdd2022-01-13 21:51:44 -08009382 CarrierPrivilegesTracker cpt = phone.getCarrierPrivilegesTracker();
9383 if (cpt != null) {
9384 cpt.setTestOverrideCarrierPrivilegeRules(carrierPrivilegeRules);
9385 }
9386 // TODO(b/211796398): remove the legacy logic below once CPT migration is done.
chen xueaba88a2019-03-15 13:15:10 -07009387 phone.setCarrierTestOverride(mccmnc, imsi, iccid, gid1, gid2, plmn, spn,
9388 carrierPrivilegeRules, apn);
Jeff Davidson8ab02b22020-03-28 12:24:40 -07009389 if (carrierPrivilegeRules == null) {
9390 mCarrierPrivilegeTestOverrideSubIds.remove(subId);
9391 } else {
9392 mCarrierPrivilegeTestOverrideSubIds.add(subId);
9393 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08009394 } finally {
9395 Binder.restoreCallingIdentity(identity);
fionaxua13278b2018-03-21 00:08:13 -07009396 }
fionaxua13278b2018-03-21 00:08:13 -07009397 }
9398
9399 @Override
9400 public int getCarrierIdListVersion(int subId) {
Brad Ebinger35c841c2018-10-01 10:40:55 -07009401 enforceReadPrivilegedPermission("getCarrierIdListVersion");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08009402
9403 final long identity = Binder.clearCallingIdentity();
9404 try {
9405 final Phone phone = getPhone(subId);
9406 if (phone == null) {
9407 loge("getCarrierIdListVersion fails with invalid subId: " + subId);
9408 return TelephonyManager.UNKNOWN_CARRIER_ID_LIST_VERSION;
9409 }
9410 return phone.getCarrierIdListVersion();
9411 } finally {
9412 Binder.restoreCallingIdentity(identity);
fionaxua13278b2018-03-21 00:08:13 -07009413 }
fionaxua13278b2018-03-21 00:08:13 -07009414 }
Malcolm Chen2c63d402018-08-14 16:00:53 -07009415
9416 @Override
Philip P. Moltmann700a9592019-10-03 11:53:50 -07009417 public int getNumberOfModemsWithSimultaneousDataConnections(int subId, String callingPackage,
9418 String callingFeatureId) {
Malcolm Chen2c63d402018-08-14 16:00:53 -07009419 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
Philip P. Moltmann700a9592019-10-03 11:53:50 -07009420 mApp, subId, callingPackage, callingFeatureId,
9421 "getNumberOfModemsWithSimultaneousDataConnections")) {
Malcolm Chen2c63d402018-08-14 16:00:53 -07009422 return -1;
9423 }
9424
9425 final long identity = Binder.clearCallingIdentity();
9426 try {
9427 return mPhoneConfigurationManager.getNumberOfModemsWithSimultaneousDataConnections();
9428 } finally {
9429 Binder.restoreCallingIdentity(identity);
9430 }
9431 }
Pengquan Menga1bb6272018-09-06 09:59:22 -07009432
9433 @Override
9434 public int getCdmaRoamingMode(int subId) {
zoey chen7e6d4e52019-12-17 18:18:59 +08009435 TelephonyPermissions
Nathan Harold62c68512021-04-06 11:26:02 -07009436 .enforceCallingOrSelfReadPrivilegedPhoneStatePermissionOrCarrierPrivilege(
Pengquan Menga1bb6272018-09-06 09:59:22 -07009437 mApp, subId, "getCdmaRoamingMode");
9438
9439 final long identity = Binder.clearCallingIdentity();
9440 try {
9441 return (int) sendRequest(CMD_GET_CDMA_ROAMING_MODE, null /* argument */, subId);
9442 } finally {
9443 Binder.restoreCallingIdentity(identity);
9444 }
9445 }
9446
9447 @Override
9448 public boolean setCdmaRoamingMode(int subId, int mode) {
9449 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
9450 mApp, subId, "setCdmaRoamingMode");
9451
9452 final long identity = Binder.clearCallingIdentity();
9453 try {
9454 return (boolean) sendRequest(CMD_SET_CDMA_ROAMING_MODE, mode, subId);
9455 } finally {
9456 Binder.restoreCallingIdentity(identity);
9457 }
9458 }
9459
9460 @Override
Sarah Chinbaab1432020-10-28 13:46:24 -07009461 public int getCdmaSubscriptionMode(int subId) {
9462 TelephonyPermissions
Nathan Harold62c68512021-04-06 11:26:02 -07009463 .enforceCallingOrSelfReadPrivilegedPhoneStatePermissionOrCarrierPrivilege(
Sarah Chinbaab1432020-10-28 13:46:24 -07009464 mApp, subId, "getCdmaSubscriptionMode");
9465
9466 final long identity = Binder.clearCallingIdentity();
9467 try {
9468 return (int) sendRequest(CMD_GET_CDMA_SUBSCRIPTION_MODE, null /* argument */, subId);
9469 } finally {
9470 Binder.restoreCallingIdentity(identity);
9471 }
9472 }
9473
9474 @Override
Pengquan Menga1bb6272018-09-06 09:59:22 -07009475 public boolean setCdmaSubscriptionMode(int subId, int mode) {
9476 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
9477 mApp, subId, "setCdmaSubscriptionMode");
9478
9479 final long identity = Binder.clearCallingIdentity();
9480 try {
9481 return (boolean) sendRequest(CMD_SET_CDMA_SUBSCRIPTION_MODE, mode, subId);
9482 } finally {
9483 Binder.restoreCallingIdentity(identity);
9484 }
9485 }
Makoto Onukida3bf792018-09-18 16:06:29 -07009486
sqianc5eccab2018-10-19 18:46:41 -07009487 @Override
sqian8c685422019-02-22 15:55:18 -08009488 public Map<Integer, List<EmergencyNumber>> getEmergencyNumberList(
Philip P. Moltmann700a9592019-10-03 11:53:50 -07009489 String callingPackage, String callingFeatureId) {
sqian11b7a0e2018-12-05 18:48:28 -08009490 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
Philip P. Moltmann700a9592019-10-03 11:53:50 -07009491 mApp, getDefaultSubscription(), callingPackage, callingFeatureId,
9492 "getEmergencyNumberList")) {
sqian11b7a0e2018-12-05 18:48:28 -08009493 throw new SecurityException("Requires READ_PHONE_STATE permission.");
9494 }
9495 final long identity = Binder.clearCallingIdentity();
9496 try {
sqian854d44b2018-12-12 16:48:18 -08009497 Map<Integer, List<EmergencyNumber>> emergencyNumberListInternal = new HashMap<>();
9498 for (Phone phone: PhoneFactory.getPhones()) {
9499 if (phone.getEmergencyNumberTracker() != null
9500 && phone.getEmergencyNumberTracker().getEmergencyNumberList() != null) {
9501 emergencyNumberListInternal.put(
9502 phone.getSubId(),
9503 phone.getEmergencyNumberTracker().getEmergencyNumberList());
9504 }
sqian11b7a0e2018-12-05 18:48:28 -08009505 }
sqian854d44b2018-12-12 16:48:18 -08009506 return emergencyNumberListInternal;
sqian11b7a0e2018-12-05 18:48:28 -08009507 } finally {
9508 Binder.restoreCallingIdentity(identity);
9509 }
sqianc5eccab2018-10-19 18:46:41 -07009510 }
9511
9512 @Override
sqian8c685422019-02-22 15:55:18 -08009513 public boolean isEmergencyNumber(String number, boolean exactMatch) {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08009514 final Phone defaultPhone = getDefaultPhone();
sqian11b7a0e2018-12-05 18:48:28 -08009515 if (!exactMatch) {
9516 TelephonyPermissions
Nathan Harold62c68512021-04-06 11:26:02 -07009517 .enforceCallingOrSelfReadPrivilegedPhoneStatePermissionOrCarrierPrivilege(
sqian8c685422019-02-22 15:55:18 -08009518 mApp, defaultPhone.getSubId(), "isEmergencyNumber(Potential)");
sqian11b7a0e2018-12-05 18:48:28 -08009519 }
9520 final long identity = Binder.clearCallingIdentity();
9521 try {
sqian854d44b2018-12-12 16:48:18 -08009522 for (Phone phone: PhoneFactory.getPhones()) {
Chinmay Dhodapkard521bb12022-08-16 15:49:54 -07009523 //Note: we ignore passed in param exactMatch. We can remove it once
9524 // TelephonyManager#isPotentialEmergencyNumber is removed completely
sqian854d44b2018-12-12 16:48:18 -08009525 if (phone.getEmergencyNumberTracker() != null
Taesu Leee050c002020-10-13 17:19:35 +09009526 && phone.getEmergencyNumberTracker()
Chinmay Dhodapkard521bb12022-08-16 15:49:54 -07009527 .isEmergencyNumber(number)) {
Taesu Leee050c002020-10-13 17:19:35 +09009528 return true;
sqian11b7a0e2018-12-05 18:48:28 -08009529 }
sqian11b7a0e2018-12-05 18:48:28 -08009530 }
9531 return false;
9532 } finally {
9533 Binder.restoreCallingIdentity(identity);
9534 }
9535 }
9536
sqianf4ca7ed2019-01-15 18:32:07 -08009537 /**
Shuo Qianccbaf742021-02-22 18:32:21 -08009538 * Start emergency callback mode for GsmCdmaPhone for testing.
9539 */
9540 @Override
9541 public void startEmergencyCallbackMode() {
9542 TelephonyPermissions.enforceShellOnly(Binder.getCallingUid(),
9543 "startEmergencyCallbackMode");
9544 enforceModifyPermission();
9545 final long identity = Binder.clearCallingIdentity();
9546 try {
9547 for (Phone phone : PhoneFactory.getPhones()) {
9548 Rlog.d(LOG_TAG, "startEmergencyCallbackMode phone type: " + phone.getPhoneType());
9549 if (phone != null && ((phone.getPhoneType() == PHONE_TYPE_GSM)
9550 || (phone.getPhoneType() == PHONE_TYPE_CDMA))) {
9551 GsmCdmaPhone gsmCdmaPhone = (GsmCdmaPhone) phone;
9552 gsmCdmaPhone.obtainMessage(
9553 GsmCdmaPhone.EVENT_EMERGENCY_CALLBACK_MODE_ENTER).sendToTarget();
9554 Rlog.d(LOG_TAG, "startEmergencyCallbackMode: triggered");
9555 }
9556 }
9557 } finally {
9558 Binder.restoreCallingIdentity(identity);
9559 }
9560 }
9561
9562 /**
sqianf4ca7ed2019-01-15 18:32:07 -08009563 * Update emergency number list for test mode.
9564 */
9565 @Override
9566 public void updateEmergencyNumberListTestMode(int action, EmergencyNumber num) {
9567 TelephonyPermissions.enforceShellOnly(Binder.getCallingUid(),
9568 "updateEmergencyNumberListTestMode");
9569
9570 final long identity = Binder.clearCallingIdentity();
9571 try {
9572 for (Phone phone: PhoneFactory.getPhones()) {
9573 EmergencyNumberTracker tracker = phone.getEmergencyNumberTracker();
9574 if (tracker != null) {
9575 tracker.executeEmergencyNumberTestModeCommand(action, num);
9576 }
9577 }
9578 } finally {
9579 Binder.restoreCallingIdentity(identity);
9580 }
9581 }
9582
9583 /**
9584 * Get the full emergency number list for test mode.
9585 */
9586 @Override
9587 public List<String> getEmergencyNumberListTestMode() {
9588 TelephonyPermissions.enforceShellOnly(Binder.getCallingUid(),
9589 "getEmergencyNumberListTestMode");
9590
9591 final long identity = Binder.clearCallingIdentity();
9592 try {
9593 Set<String> emergencyNumbers = new HashSet<>();
9594 for (Phone phone: PhoneFactory.getPhones()) {
9595 EmergencyNumberTracker tracker = phone.getEmergencyNumberTracker();
9596 if (tracker != null) {
9597 for (EmergencyNumber num : tracker.getEmergencyNumberList()) {
9598 emergencyNumbers.add(num.getNumber());
9599 }
9600 }
9601 }
9602 return new ArrayList<>(emergencyNumbers);
9603 } finally {
9604 Binder.restoreCallingIdentity(identity);
9605 }
9606 }
9607
chen xud6b45bd2018-10-30 22:27:10 -07009608 @Override
Shuo Qian3b6ee772019-11-13 17:43:31 -08009609 public int getEmergencyNumberDbVersion(int subId) {
9610 enforceReadPrivilegedPermission("getEmergencyNumberDbVersion");
9611
9612 final long identity = Binder.clearCallingIdentity();
9613 try {
9614 final Phone phone = getPhone(subId);
9615 if (phone == null) {
9616 loge("getEmergencyNumberDbVersion fails with invalid subId: " + subId);
9617 return TelephonyManager.INVALID_EMERGENCY_NUMBER_DB_VERSION;
9618 }
9619 return phone.getEmergencyNumberDbVersion();
9620 } finally {
9621 Binder.restoreCallingIdentity(identity);
9622 }
9623 }
9624
9625 @Override
9626 public void notifyOtaEmergencyNumberDbInstalled() {
9627 enforceModifyPermission();
9628
9629 final long identity = Binder.clearCallingIdentity();
9630 try {
9631 for (Phone phone: PhoneFactory.getPhones()) {
9632 EmergencyNumberTracker tracker = phone.getEmergencyNumberTracker();
9633 if (tracker != null) {
9634 tracker.updateOtaEmergencyNumberDatabase();
9635 }
9636 }
9637 } finally {
9638 Binder.restoreCallingIdentity(identity);
9639 }
9640 }
9641
9642 @Override
Shuo Qianc373f112020-03-05 17:55:34 -08009643 public void updateOtaEmergencyNumberDbFilePath(ParcelFileDescriptor otaParcelFileDescriptor) {
Shuo Qian3b6ee772019-11-13 17:43:31 -08009644 enforceActiveEmergencySessionPermission();
9645
9646 final long identity = Binder.clearCallingIdentity();
9647 try {
9648 for (Phone phone: PhoneFactory.getPhones()) {
9649 EmergencyNumberTracker tracker = phone.getEmergencyNumberTracker();
9650 if (tracker != null) {
Shuo Qianc373f112020-03-05 17:55:34 -08009651 tracker.updateOtaEmergencyNumberDbFilePath(otaParcelFileDescriptor);
9652 }
9653 }
9654 } finally {
9655 Binder.restoreCallingIdentity(identity);
9656 }
9657 }
9658
9659 @Override
9660 public void resetOtaEmergencyNumberDbFilePath() {
9661 enforceActiveEmergencySessionPermission();
9662
9663 final long identity = Binder.clearCallingIdentity();
9664 try {
9665 for (Phone phone: PhoneFactory.getPhones()) {
9666 EmergencyNumberTracker tracker = phone.getEmergencyNumberTracker();
9667 if (tracker != null) {
9668 tracker.resetOtaEmergencyNumberDbFilePath();
Shuo Qian3b6ee772019-11-13 17:43:31 -08009669 }
9670 }
9671 } finally {
9672 Binder.restoreCallingIdentity(identity);
9673 }
9674 }
9675
9676 @Override
chen xud6b45bd2018-10-30 22:27:10 -07009677 public List<String> getCertsFromCarrierPrivilegeAccessRules(int subId) {
9678 enforceReadPrivilegedPermission("getCertsFromCarrierPrivilegeAccessRules");
9679 Phone phone = getPhone(subId);
9680 if (phone == null) {
9681 return null;
9682 }
9683 final long identity = Binder.clearCallingIdentity();
9684 try {
9685 UiccProfile profile = UiccController.getInstance()
9686 .getUiccProfileForPhone(phone.getPhoneId());
9687 if (profile != null) {
9688 return profile.getCertsFromCarrierPrivilegeAccessRules();
9689 }
9690 } finally {
9691 Binder.restoreCallingIdentity(identity);
9692 }
9693 return null;
9694 }
Malcolm Chen8e4ed912019-01-15 20:22:16 -08009695
9696 /**
9697 * Enable or disable a modem stack.
9698 */
9699 @Override
9700 public boolean enableModemForSlot(int slotIndex, boolean enable) {
9701 enforceModifyPermission();
9702
9703 final long identity = Binder.clearCallingIdentity();
9704 try {
9705 Phone phone = PhoneFactory.getPhone(slotIndex);
9706 if (phone == null) {
9707 return false;
9708 } else {
9709 return (Boolean) sendRequest(CMD_REQUEST_ENABLE_MODEM, enable, phone, null);
9710 }
9711 } finally {
9712 Binder.restoreCallingIdentity(identity);
9713 }
9714 }
Michelecea4cf22018-12-21 15:00:11 -08009715
Malcolm Chen4bcd9822019-03-27 18:34:05 -07009716 /**
9717 * Whether a modem stack is enabled or not.
9718 */
9719 @Override
Philip P. Moltmann700a9592019-10-03 11:53:50 -07009720 public boolean isModemEnabledForSlot(int slotIndex, String callingPackage,
9721 String callingFeatureId) {
Malcolm Chen4bcd9822019-03-27 18:34:05 -07009722 Phone phone = PhoneFactory.getPhone(slotIndex);
9723 if (phone == null) return false;
9724
9725 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
Philip P. Moltmann700a9592019-10-03 11:53:50 -07009726 mApp, phone.getSubId(), callingPackage, callingFeatureId,
9727 "isModemEnabledForSlot")) {
Malcolm Chen4bcd9822019-03-27 18:34:05 -07009728 throw new SecurityException("Requires READ_PHONE_STATE permission.");
9729 }
9730
9731 final long identity = Binder.clearCallingIdentity();
9732 try {
Nazanin Bakhshif71371d2019-04-29 17:29:44 -07009733 try {
9734 return mPhoneConfigurationManager.getPhoneStatusFromCache(phone.getPhoneId());
9735 } catch (NoSuchElementException ex) {
9736 return (Boolean) sendRequest(CMD_GET_MODEM_STATUS, null, phone, null);
9737 }
Malcolm Chen4bcd9822019-03-27 18:34:05 -07009738 } finally {
9739 Binder.restoreCallingIdentity(identity);
9740 }
9741 }
9742
Michelecea4cf22018-12-21 15:00:11 -08009743 @Override
Michele0ea7d782019-03-19 14:58:42 -07009744 public void setMultiSimCarrierRestriction(boolean isMultiSimCarrierRestricted) {
Michelecea4cf22018-12-21 15:00:11 -08009745 enforceModifyPermission();
9746
9747 final long identity = Binder.clearCallingIdentity();
9748 try {
9749 mTelephonySharedPreferences.edit()
Michele0ea7d782019-03-19 14:58:42 -07009750 .putBoolean(PREF_MULTI_SIM_RESTRICTED, isMultiSimCarrierRestricted)
Michelecea4cf22018-12-21 15:00:11 -08009751 .commit();
9752 } finally {
9753 Binder.restoreCallingIdentity(identity);
9754 }
9755 }
9756
9757 @Override
Michele0ea7d782019-03-19 14:58:42 -07009758 @TelephonyManager.IsMultiSimSupportedResult
Philip P. Moltmann700a9592019-10-03 11:53:50 -07009759 public int isMultiSimSupported(String callingPackage, String callingFeatureId) {
Michele4245e952019-02-04 11:36:23 -08009760 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(mApp,
Philip P. Moltmann700a9592019-10-03 11:53:50 -07009761 getDefaultPhone().getSubId(), callingPackage, callingFeatureId,
9762 "isMultiSimSupported")) {
Michele0ea7d782019-03-19 14:58:42 -07009763 return TelephonyManager.MULTISIM_NOT_SUPPORTED_BY_HARDWARE;
Michele4245e952019-02-04 11:36:23 -08009764 }
Michelecea4cf22018-12-21 15:00:11 -08009765
9766 final long identity = Binder.clearCallingIdentity();
9767 try {
Michele0ea7d782019-03-19 14:58:42 -07009768 return isMultiSimSupportedInternal();
Michelecea4cf22018-12-21 15:00:11 -08009769 } finally {
9770 Binder.restoreCallingIdentity(identity);
9771 }
9772 }
Nazanin Bakhshi628473f2019-01-29 17:37:52 -08009773
Michele0ea7d782019-03-19 14:58:42 -07009774 @TelephonyManager.IsMultiSimSupportedResult
9775 private int isMultiSimSupportedInternal() {
Michele30b57b22019-03-01 12:01:14 -08009776 // If the device has less than 2 SIM cards, indicate that multisim is restricted.
9777 int numPhysicalSlots = UiccController.getInstance().getUiccSlots().length;
9778 if (numPhysicalSlots < 2) {
Michele0ea7d782019-03-19 14:58:42 -07009779 loge("isMultiSimSupportedInternal: requires at least 2 cards");
9780 return TelephonyManager.MULTISIM_NOT_SUPPORTED_BY_HARDWARE;
Michele30b57b22019-03-01 12:01:14 -08009781 }
9782 // Check if the hardware supports multisim functionality. If usage of multisim is not
9783 // supported by the modem, indicate that it is restricted.
9784 PhoneCapability staticCapability =
9785 mPhoneConfigurationManager.getStaticPhoneCapability();
9786 if (staticCapability == null) {
Michele0ea7d782019-03-19 14:58:42 -07009787 loge("isMultiSimSupportedInternal: no static configuration available");
9788 return TelephonyManager.MULTISIM_NOT_SUPPORTED_BY_HARDWARE;
Michele30b57b22019-03-01 12:01:14 -08009789 }
SongFerngWang8236caa2021-01-17 21:51:44 +08009790 if (staticCapability.getLogicalModemList().size() < 2) {
Michele0ea7d782019-03-19 14:58:42 -07009791 loge("isMultiSimSupportedInternal: maximum number of modem is < 2");
9792 return TelephonyManager.MULTISIM_NOT_SUPPORTED_BY_HARDWARE;
Michele30b57b22019-03-01 12:01:14 -08009793 }
9794 // Check if support of multiple SIMs is restricted by carrier
9795 if (mTelephonySharedPreferences.getBoolean(PREF_MULTI_SIM_RESTRICTED, false)) {
Michele0ea7d782019-03-19 14:58:42 -07009796 return TelephonyManager.MULTISIM_NOT_SUPPORTED_BY_CARRIER;
Michele30b57b22019-03-01 12:01:14 -08009797 }
9798
Michele0ea7d782019-03-19 14:58:42 -07009799 return TelephonyManager.MULTISIM_ALLOWED;
Michele30b57b22019-03-01 12:01:14 -08009800 }
9801
Nazanin Bakhshi628473f2019-01-29 17:37:52 -08009802 /**
9803 * Switch configs to enable multi-sim or switch back to single-sim
Nazanin Bakhshi17318782019-03-01 11:56:08 -08009804 * Note: Switch from multi-sim to single-sim is only possible with MODIFY_PHONE_STATE
9805 * permission, but the other way around is possible with either MODIFY_PHONE_STATE
9806 * or carrier privileges
Nazanin Bakhshi628473f2019-01-29 17:37:52 -08009807 * @param numOfSims number of active sims we want to switch to
9808 */
9809 @Override
9810 public void switchMultiSimConfig(int numOfSims) {
Nazanin Bakhshi17318782019-03-01 11:56:08 -08009811 if (numOfSims == 1) {
9812 enforceModifyPermission();
9813 } else {
9814 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
9815 mApp, SubscriptionManager.DEFAULT_SUBSCRIPTION_ID, "switchMultiSimConfig");
9816 }
Nazanin Bakhshi628473f2019-01-29 17:37:52 -08009817 final long identity = Binder.clearCallingIdentity();
Michele30b57b22019-03-01 12:01:14 -08009818
Nazanin Bakhshi628473f2019-01-29 17:37:52 -08009819 try {
Michele30b57b22019-03-01 12:01:14 -08009820 //only proceed if multi-sim is not restricted
Michele0ea7d782019-03-19 14:58:42 -07009821 if (isMultiSimSupportedInternal() != TelephonyManager.MULTISIM_ALLOWED) {
Michele30b57b22019-03-01 12:01:14 -08009822 loge("switchMultiSimConfig not possible. It is restricted or not supported.");
9823 return;
9824 }
Nazanin Bakhshi628473f2019-01-29 17:37:52 -08009825 mPhoneConfigurationManager.switchMultiSimConfig(numOfSims);
9826 } finally {
9827 Binder.restoreCallingIdentity(identity);
9828 }
9829 }
9830
Hyungjun Parkbb07fde2019-01-10 15:28:51 +09009831 @Override
9832 public boolean isApplicationOnUicc(int subId, int appType) {
9833 enforceReadPrivilegedPermission("isApplicationOnUicc");
9834 Phone phone = getPhone(subId);
9835 if (phone == null) {
9836 return false;
9837 }
9838 final long identity = Binder.clearCallingIdentity();
9839 try {
Muralidhar Reddy472c2ae2021-09-29 19:38:40 +00009840 UiccPort uiccPort = phone.getUiccPort();
9841 if (uiccPort == null) {
Hyungjun Parkbb07fde2019-01-10 15:28:51 +09009842 return false;
9843 }
Muralidhar Reddy472c2ae2021-09-29 19:38:40 +00009844 UiccProfile uiccProfile = uiccPort.getUiccProfile();
Hyungjun Parkbb07fde2019-01-10 15:28:51 +09009845 if (uiccProfile == null) {
9846 return false;
9847 }
9848 if (TelephonyManager.APPTYPE_SIM <= appType
9849 && appType <= TelephonyManager.APPTYPE_ISIM) {
9850 return uiccProfile.isApplicationOnIcc(AppType.values()[appType]);
9851 }
9852 return false;
9853 } finally {
9854 Binder.restoreCallingIdentity(identity);
9855 }
9856 }
9857
Nazanin Bakhshi628473f2019-01-29 17:37:52 -08009858 /**
chen xub4baa772019-04-03 10:23:41 -07009859 * Get whether making changes to modem configurations will trigger reboot.
9860 * Return value defaults to true.
Nazanin Bakhshi5fe5ef22019-01-30 10:52:09 -08009861 */
9862 @Override
Philip P. Moltmann700a9592019-10-03 11:53:50 -07009863 public boolean doesSwitchMultiSimConfigTriggerReboot(int subId, String callingPackage,
9864 String callingFeatureId) {
chen xub4baa772019-04-03 10:23:41 -07009865 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
Philip P. Moltmann700a9592019-10-03 11:53:50 -07009866 mApp, subId, callingPackage, callingFeatureId,
9867 "doesSwitchMultiSimConfigTriggerReboot")) {
chen xub4baa772019-04-03 10:23:41 -07009868 return false;
9869 }
Nazanin Bakhshi5fe5ef22019-01-30 10:52:09 -08009870 final long identity = Binder.clearCallingIdentity();
9871 try {
9872 return mPhoneConfigurationManager.isRebootRequiredForModemConfigChange();
9873 } finally {
9874 Binder.restoreCallingIdentity(identity);
9875 }
9876 }
9877
Nathan Harold29f5f052019-02-15 13:41:57 -08009878 private void updateModemStateMetrics() {
9879 TelephonyMetrics metrics = TelephonyMetrics.getInstance();
9880 // TODO: check the state for each modem if the api is ready.
9881 metrics.updateEnabledModemBitmap((1 << TelephonyManager.from(mApp).getPhoneCount()) - 1);
9882 }
9883
Pengquan Meng3889a572019-01-23 11:16:29 -08009884 @Override
sandeepjsa208e3b2021-11-17 04:05:58 +00009885 public List<UiccSlotMapping> getSlotsMapping(String callingPackage) {
Pengquan Meng3889a572019-01-23 11:16:29 -08009886 enforceReadPrivilegedPermission("getSlotsMapping");
sandeepjsa208e3b2021-11-17 04:05:58 +00009887 // Verify that the callingPackage belongs to the calling UID
9888 mApp.getSystemService(AppOpsManager.class)
9889 .checkPackage(Binder.getCallingUid(), callingPackage);
Pengquan Meng3889a572019-01-23 11:16:29 -08009890 final long identity = Binder.clearCallingIdentity();
sandeepjsa208e3b2021-11-17 04:05:58 +00009891 List<UiccSlotMapping> slotMap = new ArrayList<>();
Pengquan Meng3889a572019-01-23 11:16:29 -08009892 try {
sandeepjsa208e3b2021-11-17 04:05:58 +00009893 UiccSlotInfo[] slotInfos = getUiccSlotsInfo(mApp.getOpPackageName());
9894 if (slotInfos != null) {
9895 for (int i = 0; i < slotInfos.length; i++) {
9896 for (UiccPortInfo portInfo : slotInfos[i].getPorts()) {
9897 if (SubscriptionManager.isValidPhoneId(portInfo.getLogicalSlotIndex())) {
9898 slotMap.add(new UiccSlotMapping(portInfo.getPortIndex(), i,
9899 portInfo.getLogicalSlotIndex()));
9900 }
9901 }
Pengquan Meng3889a572019-01-23 11:16:29 -08009902 }
9903 }
sandeepjsa208e3b2021-11-17 04:05:58 +00009904 return slotMap;
Pengquan Meng3889a572019-01-23 11:16:29 -08009905 } finally {
9906 Binder.restoreCallingIdentity(identity);
9907 }
9908 }
Nathan Harold48d6fd52019-02-06 19:01:40 -08009909
9910 /**
9911 * Get the IRadio HAL Version
jimsunf9ec1622022-09-13 21:18:43 +08009912 * @deprecated use getHalVersion instead
Nathan Harold48d6fd52019-02-06 19:01:40 -08009913 */
jimsunf9ec1622022-09-13 21:18:43 +08009914 @Deprecated
Nathan Harold48d6fd52019-02-06 19:01:40 -08009915 @Override
9916 public int getRadioHalVersion() {
jimsunf9ec1622022-09-13 21:18:43 +08009917 return getHalVersion(HAL_SERVICE_RADIO);
9918 }
9919
9920 /**
9921 * Get the HAL Version of a specific service
9922 */
9923 @Override
9924 public int getHalVersion(int service) {
Nathan Harold48d6fd52019-02-06 19:01:40 -08009925 Phone phone = getDefaultPhone();
9926 if (phone == null) return -1;
jimsunf9ec1622022-09-13 21:18:43 +08009927 HalVersion hv = phone.getHalVersion(service);
Nathan Harold48d6fd52019-02-06 19:01:40 -08009928 if (hv.equals(HalVersion.UNKNOWN)) return -1;
9929 return hv.major * 100 + hv.minor;
9930 }
Malcolm Chendc8c10e2019-04-10 18:25:07 -07009931
9932 /**
Shuo Qianda2d6ec2020-01-14 15:18:28 -08009933 * Get the current calling package name.
9934 * @return the current calling package name
9935 */
9936 @Override
9937 public String getCurrentPackageName() {
9938 return mApp.getPackageManager().getPackagesForUid(Binder.getCallingUid())[0];
9939 }
9940
9941 /**
Malcolm Chene5ad5792019-04-18 13:51:02 -07009942 * Return whether data is enabled for certain APN type. This will tell if framework will accept
9943 * corresponding network requests on a subId.
9944 *
9945 * Data is enabled if:
Malcolm Chendc8c10e2019-04-10 18:25:07 -07009946 * 1) user data is turned on, or
Malcolm Chene5ad5792019-04-18 13:51:02 -07009947 * 2) APN is un-metered for this subscription, or
9948 * 3) APN type is whitelisted. E.g. MMS is whitelisted if
Hall Liu746e03c2020-09-25 11:13:49 -07009949 * {@link TelephonyManager#MOBILE_DATA_POLICY_MMS_ALWAYS_ALLOWED} is enabled.
Malcolm Chene5ad5792019-04-18 13:51:02 -07009950 *
9951 * @return whether data is allowed for a apn type.
9952 *
9953 * @hide
Malcolm Chendc8c10e2019-04-10 18:25:07 -07009954 */
9955 @Override
Malcolm Chene5ad5792019-04-18 13:51:02 -07009956 public boolean isDataEnabledForApn(int apnType, int subId, String callingPackage) {
Amit Mahajan5d4e1922019-10-07 16:20:43 -07009957 enforceReadPrivilegedPermission("Needs READ_PRIVILEGED_PHONE_STATE for "
9958 + "isDataEnabledForApn");
Malcolm Chendc8c10e2019-04-10 18:25:07 -07009959
9960 // Now that all security checks passes, perform the operation as ourselves.
9961 final long identity = Binder.clearCallingIdentity();
9962 try {
9963 Phone phone = getPhone(subId);
9964 if (phone == null) return false;
9965
Jack Yu27422a52022-03-21 10:38:05 -07009966 boolean isMetered;
Jack Yu99e87332021-12-17 23:14:15 -08009967 boolean isDataEnabled;
Jack Yu7968c6d2022-07-31 00:43:21 -07009968 isMetered = phone.getDataNetworkController().getDataConfigManager()
9969 .isMeteredCapability(DataUtils.apnTypeToNetworkCapability(apnType),
9970 phone.getServiceState().getDataRoaming());
9971 isDataEnabled = phone.getDataSettingsManager().isDataEnabled(apnType);
Jack Yu99e87332021-12-17 23:14:15 -08009972 return !isMetered || isDataEnabled;
Malcolm Chene5ad5792019-04-18 13:51:02 -07009973 } finally {
9974 Binder.restoreCallingIdentity(identity);
9975 }
9976 }
9977
9978 @Override
Jack Yu41407ee2019-05-13 16:54:09 -07009979 public boolean isApnMetered(@ApnType int apnType, int subId) {
Malcolm Chene5ad5792019-04-18 13:51:02 -07009980 enforceReadPrivilegedPermission("isApnMetered");
9981
9982 // Now that all security checks passes, perform the operation as ourselves.
9983 final long identity = Binder.clearCallingIdentity();
9984 try {
9985 Phone phone = getPhone(subId);
9986 if (phone == null) return true; // By default return true.
Jack Yu7968c6d2022-07-31 00:43:21 -07009987 return phone.getDataNetworkController().getDataConfigManager().isMeteredCapability(
9988 DataUtils.apnTypeToNetworkCapability(apnType),
9989 phone.getServiceState().getDataRoaming());
Malcolm Chendc8c10e2019-04-10 18:25:07 -07009990 } finally {
9991 Binder.restoreCallingIdentity(identity);
9992 }
9993 }
Brad Ebingera63db5f2019-04-23 16:31:13 -07009994
9995 @Override
Hall Liu73f5d362020-01-20 13:42:00 -08009996 public void setSystemSelectionChannels(List<RadioAccessSpecifier> specifiers,
9997 int subscriptionId, IBooleanConsumer resultCallback) {
9998 enforceModifyPermission();
9999 long token = Binder.clearCallingIdentity();
10000 try {
10001 Phone phone = getPhone(subscriptionId);
10002 if (phone == null) {
10003 try {
10004 if (resultCallback != null) {
10005 resultCallback.accept(false);
10006 }
10007 } catch (RemoteException e) {
10008 // ignore
10009 }
10010 return;
10011 }
10012 Pair<List<RadioAccessSpecifier>, Consumer<Boolean>> argument =
10013 Pair.create(specifiers, (x) -> {
10014 try {
10015 if (resultCallback != null) {
10016 resultCallback.accept(x);
10017 }
10018 } catch (RemoteException e) {
10019 // ignore
10020 }
10021 });
10022 sendRequestAsync(CMD_SET_SYSTEM_SELECTION_CHANNELS, argument, phone, null);
10023 } finally {
10024 Binder.restoreCallingIdentity(token);
10025 }
10026 }
10027
10028 @Override
Sarah Chin679c08a2020-11-18 13:39:35 -080010029 public List<RadioAccessSpecifier> getSystemSelectionChannels(int subId) {
10030 TelephonyPermissions
Nathan Harold62c68512021-04-06 11:26:02 -070010031 .enforceCallingOrSelfReadPrivilegedPhoneStatePermissionOrCarrierPrivilege(
Sarah Chin679c08a2020-11-18 13:39:35 -080010032 mApp, subId, "getSystemSelectionChannels");
10033 WorkSource workSource = getWorkSource(Binder.getCallingUid());
10034 final long identity = Binder.clearCallingIdentity();
10035 try {
Sarah Chin428d1d62021-03-13 03:17:40 -080010036 Object result = sendRequest(CMD_GET_SYSTEM_SELECTION_CHANNELS, null, subId, workSource);
10037 if (result instanceof IllegalStateException) {
10038 throw (IllegalStateException) result;
10039 }
10040 List<RadioAccessSpecifier> specifiers = (List<RadioAccessSpecifier>) result;
Sarah Chin679c08a2020-11-18 13:39:35 -080010041 if (DBG) log("getSystemSelectionChannels: " + specifiers);
10042 return specifiers;
10043 } finally {
10044 Binder.restoreCallingIdentity(identity);
10045 }
10046 }
10047
10048 @Override
Jack Yu8b766fc2022-03-21 09:42:33 -070010049 public boolean isMvnoMatched(int slotIndex, int mvnoType, @NonNull String mvnoMatchData) {
changbettyca3d40d2020-03-03 16:27:31 +080010050 enforceReadPrivilegedPermission("isMvnoMatched");
Jack Yu8b766fc2022-03-21 09:42:33 -070010051 return UiccController.getInstance().mvnoMatches(slotIndex, mvnoType, mvnoMatchData);
changbetty7157e9e2019-12-06 18:16:37 +080010052 }
10053
10054 @Override
Philip P. Moltmannd02b7372020-03-18 17:06:12 -070010055 public void enqueueSmsPickResult(String callingPackage, String callingAttributionTag,
10056 IIntegerConsumer pendingSubIdResult) {
Shuo Qianda2d6ec2020-01-14 15:18:28 -080010057 if (callingPackage == null) {
10058 callingPackage = getCurrentPackageName();
10059 }
Brad Ebingera63db5f2019-04-23 16:31:13 -070010060 SmsPermissions permissions = new SmsPermissions(getDefaultPhone(), mApp,
10061 (AppOpsManager) mApp.getSystemService(Context.APP_OPS_SERVICE));
Philip P. Moltmannd02b7372020-03-18 17:06:12 -070010062 if (!permissions.checkCallingCanSendSms(callingPackage, callingAttributionTag,
10063 "Sending message")) {
Brad Ebingera63db5f2019-04-23 16:31:13 -070010064 throw new SecurityException("Requires SEND_SMS permission to perform this operation");
10065 }
10066 PickSmsSubscriptionActivity.addPendingResult(pendingSubIdResult);
10067 Intent intent = new Intent();
10068 intent.setClass(mApp, PickSmsSubscriptionActivity.class);
10069 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
10070 // Bring up choose default SMS subscription dialog right now
10071 intent.putExtra(PickSmsSubscriptionActivity.DIALOG_TYPE_KEY,
10072 PickSmsSubscriptionActivity.SMS_PICK_FOR_MESSAGE);
10073 mApp.startActivity(intent);
10074 }
chen xud5ca2d52019-05-28 15:20:57 -070010075
10076 @Override
10077 public String getMmsUAProfUrl(int subId) {
10078 //TODO investigate if this API should require proper permission check in R b/133791609
10079 final long identity = Binder.clearCallingIdentity();
10080 try {
Guoqiang.Qiu171383d2020-07-13 09:38:32 +080010081 String carrierUAProfUrl = mApp.getCarrierConfigForSubId(subId).getString(
10082 CarrierConfigManager.KEY_MMS_UA_PROF_URL_STRING);
10083 if (!TextUtils.isEmpty(carrierUAProfUrl)) {
10084 return carrierUAProfUrl;
10085 }
Daniel Brightebb4eb72020-02-18 15:16:33 -080010086 return SubscriptionManager.getResourcesForSubId(getDefaultPhone().getContext(), subId)
10087 .getString(com.android.internal.R.string.config_mms_user_agent_profile_url);
chen xud5ca2d52019-05-28 15:20:57 -070010088 } finally {
10089 Binder.restoreCallingIdentity(identity);
10090 }
10091 }
10092
10093 @Override
10094 public String getMmsUserAgent(int subId) {
10095 //TODO investigate if this API should require proper permission check in R b/133791609
10096 final long identity = Binder.clearCallingIdentity();
10097 try {
Guoqiang.Qiu171383d2020-07-13 09:38:32 +080010098 String carrierUserAgent = mApp.getCarrierConfigForSubId(subId).getString(
10099 CarrierConfigManager.KEY_MMS_USER_AGENT_STRING);
10100 if (!TextUtils.isEmpty(carrierUserAgent)) {
10101 return carrierUserAgent;
10102 }
Daniel Brightebb4eb72020-02-18 15:16:33 -080010103 return SubscriptionManager.getResourcesForSubId(getDefaultPhone().getContext(), subId)
10104 .getString(com.android.internal.R.string.config_mms_user_agent);
chen xud5ca2d52019-05-28 15:20:57 -070010105 } finally {
10106 Binder.restoreCallingIdentity(identity);
10107 }
10108 }
Jack Yub07d4972019-05-28 16:12:25 -070010109
10110 @Override
Hall Liua62f5da2020-09-25 10:42:19 -070010111 public boolean isMobileDataPolicyEnabled(int subscriptionId, int policy) {
10112 enforceReadPrivilegedPermission("isMobileDataPolicyEnabled");
Jack Yub07d4972019-05-28 16:12:25 -070010113
Jack Yub07d4972019-05-28 16:12:25 -070010114 final long identity = Binder.clearCallingIdentity();
10115 try {
Hall Liua62f5da2020-09-25 10:42:19 -070010116 Phone phone = getPhone(subscriptionId);
Jack Yub07d4972019-05-28 16:12:25 -070010117 if (phone == null) return false;
10118
Ling Maf188d502022-09-16 15:22:36 -070010119 return phone.getDataSettingsManager().isMobileDataPolicyEnabled(policy);
Jack Yub07d4972019-05-28 16:12:25 -070010120 } finally {
10121 Binder.restoreCallingIdentity(identity);
10122 }
10123 }
10124
10125 @Override
Hall Liuc66bb112021-02-02 12:09:32 -080010126 public void setMobileDataPolicyEnabled(int subscriptionId, int policy,
Hall Liua62f5da2020-09-25 10:42:19 -070010127 boolean enabled) {
changbettyd5c246e2019-12-24 15:40:37 +080010128 enforceModifyPermission();
10129
changbettyd5c246e2019-12-24 15:40:37 +080010130 final long identity = Binder.clearCallingIdentity();
10131 try {
Hall Liua62f5da2020-09-25 10:42:19 -070010132 Phone phone = getPhone(subscriptionId);
10133 if (phone == null) return;
changbettyd5c246e2019-12-24 15:40:37 +080010134
Ling Maf188d502022-09-16 15:22:36 -070010135 phone.getDataSettingsManager().setMobileDataPolicy(policy, enabled);
changbettyd5c246e2019-12-24 15:40:37 +080010136 } finally {
10137 Binder.restoreCallingIdentity(identity);
10138 }
10139 }
10140
Tyler Gunn7bcdc742019-10-04 15:56:59 -070010141 /**
Hall Liu746e03c2020-09-25 11:13:49 -070010142 * Updates whether conference event package handling is enabled.
Tyler Gunn7bcdc742019-10-04 15:56:59 -070010143 * @param isCepEnabled {@code true} if CEP handling is enabled (default), or {@code false}
10144 * otherwise.
10145 */
10146 @Override
10147 public void setCepEnabled(boolean isCepEnabled) {
10148 TelephonyPermissions.enforceShellOnly(Binder.getCallingUid(), "setCepEnabled");
10149
10150 final long identity = Binder.clearCallingIdentity();
10151 try {
10152 Rlog.i(LOG_TAG, "setCepEnabled isCepEnabled=" + isCepEnabled);
10153 for (Phone phone : PhoneFactory.getPhones()) {
10154 Phone defaultPhone = phone.getImsPhone();
10155 if (defaultPhone != null && defaultPhone.getPhoneType() == PHONE_TYPE_IMS) {
10156 ImsPhone imsPhone = (ImsPhone) defaultPhone;
10157 ImsPhoneCallTracker imsPhoneCallTracker =
10158 (ImsPhoneCallTracker) imsPhone.getCallTracker();
10159 imsPhoneCallTracker.setConferenceEventPackageEnabled(isCepEnabled);
10160 Rlog.i(LOG_TAG, "setCepEnabled isCepEnabled=" + isCepEnabled + ", for imsPhone "
10161 + imsPhone.getMsisdn());
10162 }
10163 }
10164 } finally {
10165 Binder.restoreCallingIdentity(identity);
10166 }
10167 }
allenwtsu46dcc572020-01-08 18:24:03 +080010168
10169 /**
10170 * Notify that an RCS autoconfiguration XML file has been received for provisioning.
10171 *
10172 * @param config The XML file to be read. ASCII/UTF8 encoded text if not compressed.
10173 * @param isCompressed The XML file is compressed in gzip format and must be decompressed
10174 * before being read.
10175 */
10176 @Override
10177 public void notifyRcsAutoConfigurationReceived(int subId, @NonNull byte[] config, boolean
10178 isCompressed) {
10179 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
10180 mApp, subId, "notifyRcsAutoConfigurationReceived");
Hui Wang761a6682020-10-31 05:12:53 +000010181 if (!SubscriptionManager.isValidSubscriptionId(subId)) {
10182 throw new IllegalArgumentException("Invalid Subscription ID: " + subId);
10183 }
10184 if (!isImsAvailableOnDevice()) {
10185 throw new ServiceSpecificException(ImsException.CODE_ERROR_UNSUPPORTED_OPERATION,
10186 "IMS not available on device.");
10187 }
10188
10189 final long identity = Binder.clearCallingIdentity();
allenwtsu46dcc572020-01-08 18:24:03 +080010190 try {
Hui Wang761a6682020-10-31 05:12:53 +000010191 RcsProvisioningMonitor.getInstance().updateConfig(subId, config, isCompressed);
10192 } finally {
10193 Binder.restoreCallingIdentity(identity);
allenwtsu46dcc572020-01-08 18:24:03 +080010194 }
10195 }
zoey chene02881a2019-12-30 16:11:23 +080010196
10197 @Override
10198 public boolean isIccLockEnabled(int subId) {
10199 enforceReadPrivilegedPermission("isIccLockEnabled");
10200
10201 // Now that all security checks passes, perform the operation as ourselves.
10202 final long identity = Binder.clearCallingIdentity();
10203 try {
10204 Phone phone = getPhone(subId);
10205 if (phone != null && phone.getIccCard() != null) {
10206 return phone.getIccCard().getIccLockEnabled();
10207 } else {
10208 return false;
10209 }
10210 } finally {
10211 Binder.restoreCallingIdentity(identity);
10212 }
10213 }
10214
10215 /**
10216 * Set the ICC pin lock enabled or disabled.
10217 *
10218 * @return an integer representing the status of IccLock enabled or disabled in the following
10219 * three cases:
10220 * - {@link TelephonyManager#CHANGE_ICC_LOCK_SUCCESS} if enabled or disabled IccLock
10221 * successfully.
10222 * - Positive number and zero for remaining password attempts.
10223 * - Negative number for other failure cases (such like enabling/disabling PIN failed).
10224 *
10225 */
10226 @Override
10227 public int setIccLockEnabled(int subId, boolean enabled, String password) {
10228 enforceModifyPermission();
10229
10230 Phone phone = getPhone(subId);
10231 if (phone == null) {
10232 return 0;
10233 }
10234 // Now that all security checks passes, perform the operation as ourselves.
10235 final long identity = Binder.clearCallingIdentity();
10236 try {
10237 int attemptsRemaining = (int) sendRequest(CMD_SET_ICC_LOCK_ENABLED,
10238 new Pair<Boolean, String>(enabled, password), phone, null);
10239 return attemptsRemaining;
10240
10241 } catch (Exception e) {
10242 Log.e(LOG_TAG, "setIccLockEnabled. Exception e =" + e);
10243 } finally {
10244 Binder.restoreCallingIdentity(identity);
10245 }
10246 return 0;
10247 }
10248
10249 /**
10250 * Change the ICC password used in ICC pin lock.
10251 *
10252 * @return an integer representing the status of IccLock changed in the following three cases:
10253 * - {@link TelephonyManager#CHANGE_ICC_LOCK_SUCCESS} if changed IccLock successfully.
10254 * - Positive number and zero for remaining password attempts.
10255 * - Negative number for other failure cases (such like enabling/disabling PIN failed).
10256 *
10257 */
10258 @Override
10259 public int changeIccLockPassword(int subId, String oldPassword, String newPassword) {
10260 enforceModifyPermission();
10261
10262 Phone phone = getPhone(subId);
10263 if (phone == null) {
10264 return 0;
10265 }
10266 // Now that all security checks passes, perform the operation as ourselves.
10267 final long identity = Binder.clearCallingIdentity();
10268 try {
10269 int attemptsRemaining = (int) sendRequest(CMD_CHANGE_ICC_LOCK_PASSWORD,
10270 new Pair<String, String>(oldPassword, newPassword), phone, null);
10271 return attemptsRemaining;
10272
10273 } catch (Exception e) {
10274 Log.e(LOG_TAG, "changeIccLockPassword. Exception e =" + e);
10275 } finally {
10276 Binder.restoreCallingIdentity(identity);
10277 }
10278 return 0;
10279 }
Peter Wangdafb9ac2020-01-15 14:13:38 -080010280
10281 /**
10282 * Request for receiving user activity notification
10283 */
10284 @Override
10285 public void requestUserActivityNotification() {
10286 if (!mNotifyUserActivity.get()
10287 && !mMainThreadHandler.hasMessages(MSG_NOTIFY_USER_ACTIVITY)) {
10288 mNotifyUserActivity.set(true);
10289 }
10290 }
10291
10292 /**
10293 * Called when userActivity is signalled in the power manager.
10294 * This is safe to call from any thread, with any window manager locks held or not.
10295 */
10296 @Override
10297 public void userActivity() {
10298 // ***************************************
10299 // * Inherited from PhoneWindowManager *
10300 // ***************************************
10301 // THIS IS CALLED FROM DEEP IN THE POWER MANAGER
10302 // WITH ITS LOCKS HELD.
10303 //
10304 // This code must be VERY careful about the locks
10305 // it acquires.
10306 // In fact, the current code acquires way too many,
10307 // and probably has lurking deadlocks.
10308
10309 if (Binder.getCallingUid() != Process.SYSTEM_UID) {
10310 throw new SecurityException("Only the OS may call notifyUserActivity()");
10311 }
10312
10313 if (mNotifyUserActivity.getAndSet(false)) {
10314 mMainThreadHandler.sendEmptyMessageDelayed(MSG_NOTIFY_USER_ACTIVITY,
10315 USER_ACTIVITY_NOTIFICATION_DELAY);
10316 }
10317 }
Malcolm Chen4639c562020-04-13 11:59:40 -070010318
10319 @Override
10320 public boolean canConnectTo5GInDsdsMode() {
10321 return mApp.getResources().getBoolean(R.bool.config_5g_connection_in_dsds_mode);
10322 }
Jack Yud10cdd42020-09-28 20:28:01 -070010323
10324 @Override
10325 public @NonNull List<String> getEquivalentHomePlmns(int subId, String callingPackage,
10326 String callingFeatureId) {
10327 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
10328 mApp, subId, callingPackage, callingFeatureId, "getEquivalentHomePlmns")) {
10329 throw new SecurityException("Requires READ_PHONE_STATE permission.");
10330 }
10331
10332 Phone phone = getPhone(subId);
10333 if (phone == null) {
10334 throw new RuntimeException("phone is not available");
10335 }
10336 // Now that all security checks passes, perform the operation as ourselves.
10337 final long identity = Binder.clearCallingIdentity();
10338 try {
10339 return phone.getEquivalentHomePlmns();
10340 } finally {
10341 Binder.restoreCallingIdentity(identity);
10342 }
10343 }
Daniel Bright59e67312020-11-13 11:49:37 -080010344
10345 @Override
10346 public boolean isRadioInterfaceCapabilitySupported(
Daniel Bright95a4c1f2021-02-11 09:57:16 -080010347 final @NonNull @TelephonyManager.RadioInterfaceCapability String capability) {
10348 Set<String> radioInterfaceCapabilities =
Daniel Bright94f43662021-03-01 14:43:40 -080010349 mRadioInterfaceCapabilities.getCapabilities();
Daniel Bright59e67312020-11-13 11:49:37 -080010350 if (radioInterfaceCapabilities == null) {
10351 throw new RuntimeException("radio interface capabilities are not available");
Daniel Bright59e67312020-11-13 11:49:37 -080010352 }
Daniel Bright95a4c1f2021-02-11 09:57:16 -080010353 return radioInterfaceCapabilities.contains(capability);
Daniel Bright59e67312020-11-13 11:49:37 -080010354 }
Jack Nudelmanb0b87642020-11-12 15:04:39 -080010355
Hui Wang641e81c2020-10-12 12:14:23 -070010356 @Override
10357 public void bootstrapAuthenticationRequest(int subId, int appType, Uri nafUrl,
10358 UaSecurityProtocolIdentifier securityProtocol,
Brad Ebinger34c09a52021-02-17 23:23:21 +000010359 boolean forceBootStrapping, IBootstrapAuthenticationCallback callback) {
10360 TelephonyPermissions.enforceAnyPermissionGrantedOrCarrierPrivileges(mApp, subId,
10361 Binder.getCallingUid(), "bootstrapAuthenticationRequest",
10362 Manifest.permission.PERFORM_IMS_SINGLE_REGISTRATION,
10363 Manifest.permission.MODIFY_PHONE_STATE);
Hui Wang641e81c2020-10-12 12:14:23 -070010364 if (DBG) {
10365 log("bootstrapAuthenticationRequest, subId:" + subId + ", appType:"
10366 + appType + ", NAF:" + nafUrl + ", sp:" + securityProtocol
10367 + ", forceBootStrapping:" + forceBootStrapping + ", callback:" + callback);
10368 }
10369
10370 if (!SubscriptionManager.isValidSubscriptionId(subId)
10371 || appType < TelephonyManager.APPTYPE_UNKNOWN
10372 || appType > TelephonyManager.APPTYPE_ISIM
10373 || nafUrl == null || securityProtocol == null || callback == null) {
10374 Log.d(LOG_TAG, "bootstrapAuthenticationRequest failed due to invalid parameters");
10375 if (callback != null) {
10376 try {
10377 callback.onAuthenticationFailure(
10378 0, TelephonyManager.GBA_FAILURE_REASON_FEATURE_NOT_SUPPORTED);
10379 } catch (RemoteException exception) {
10380 log("Fail to notify onAuthenticationFailure due to " + exception);
10381 }
10382 return;
10383 }
10384 }
10385
10386 final long token = Binder.clearCallingIdentity();
10387 try {
10388 getGbaManager(subId).bootstrapAuthenticationRequest(
10389 new GbaAuthRequest(subId, appType, nafUrl, securityProtocol.toByteArray(),
10390 forceBootStrapping, callback));
10391 } finally {
10392 Binder.restoreCallingIdentity(token);
10393 }
10394 }
10395
Jack Nudelmanb0b87642020-11-12 15:04:39 -080010396 /**
Sooraj Sasindran72cff492021-07-29 09:42:42 -070010397 * Attempts to set the radio power state for all phones for thermal reason.
10398 * This does not guarantee that the
Jack Nudelmanb0b87642020-11-12 15:04:39 -080010399 * requested radio power state will actually be set. See {@link
10400 * PhoneInternalInterface#setRadioPowerForReason} for more details.
10401 *
Jack Nudelmanb0b87642020-11-12 15:04:39 -080010402 * @param enable {@code true} if trying to turn radio on.
10403 * @return {@code true} if phone setRadioPowerForReason was called. Otherwise, returns {@code
10404 * false}.
10405 */
Sooraj Sasindran72cff492021-07-29 09:42:42 -070010406 private boolean setRadioPowerForThermal(boolean enable) {
10407 boolean isPhoneAvailable = false;
10408 for (int i = 0; i < TelephonyManager.getDefault().getActiveModemCount(); i++) {
10409 Phone phone = PhoneFactory.getPhone(i);
10410 if (phone != null) {
Thomas Nguyenfd0572f2022-07-15 22:28:49 +000010411 phone.setRadioPowerForReason(enable, TelephonyManager.RADIO_POWER_REASON_THERMAL);
Sooraj Sasindran72cff492021-07-29 09:42:42 -070010412 isPhoneAvailable = true;
10413 }
Jack Nudelmanb0b87642020-11-12 15:04:39 -080010414 }
Sooraj Sasindran72cff492021-07-29 09:42:42 -070010415
10416 // return true if successfully informed the phone object about the thermal radio power
10417 // request.
10418 return isPhoneAvailable;
Jack Nudelmanb0b87642020-11-12 15:04:39 -080010419 }
10420
10421 private int handleDataThrottlingRequest(int subId,
Sarah Chinecc78c42022-03-31 21:16:48 -070010422 DataThrottlingRequest dataThrottlingRequest, String callingPackage) {
Jack Nudelman5d6a98b2021-03-04 14:26:25 -080010423 boolean isDataThrottlingSupported = isRadioInterfaceCapabilitySupported(
10424 TelephonyManager.CAPABILITY_THERMAL_MITIGATION_DATA_THROTTLING);
10425 if (!isDataThrottlingSupported && dataThrottlingRequest.getDataThrottlingAction()
10426 != DataThrottlingRequest.DATA_THROTTLING_ACTION_NO_DATA_THROTTLING) {
10427 throw new IllegalArgumentException("modem does not support data throttling");
10428 }
10429
Jack Nudelmanb0b87642020-11-12 15:04:39 -080010430 // Ensure that radio is on. If not able to power on due to phone being unavailable, return
10431 // THERMAL_MITIGATION_RESULT_MODEM_NOT_AVAILABLE.
Sooraj Sasindran72cff492021-07-29 09:42:42 -070010432 if (!setRadioPowerForThermal(true)) {
Jack Nudelmanb0b87642020-11-12 15:04:39 -080010433 return TelephonyManager.THERMAL_MITIGATION_RESULT_MODEM_NOT_AVAILABLE;
10434 }
10435
Sarah Chinecc78c42022-03-31 21:16:48 -070010436 setDataEnabledForReason(
10437 subId, TelephonyManager.DATA_ENABLED_REASON_THERMAL, true, callingPackage);
Jack Nudelmanb0b87642020-11-12 15:04:39 -080010438
Jack Nudelman5d6a98b2021-03-04 14:26:25 -080010439 if (isDataThrottlingSupported) {
10440 int thermalMitigationResult =
Jack Nudelmanb0b87642020-11-12 15:04:39 -080010441 (int) sendRequest(CMD_SET_DATA_THROTTLING, dataThrottlingRequest, subId);
Jack Nudelman5d6a98b2021-03-04 14:26:25 -080010442 if (thermalMitigationResult == SET_DATA_THROTTLING_MODEM_THREW_INVALID_PARAMS) {
10443 throw new IllegalArgumentException("modem returned INVALID_ARGUMENTS");
10444 } else if (thermalMitigationResult
10445 == MODEM_DOES_NOT_SUPPORT_DATA_THROTTLING_ERROR_CODE) {
Jack Nudelman760d0962021-05-20 13:57:30 -070010446 log("Modem likely does not support data throttling on secondary carrier. Data " +
10447 "throttling action = " + dataThrottlingRequest.getDataThrottlingAction());
10448 return TelephonyManager.THERMAL_MITIGATION_RESULT_MODEM_ERROR;
Jack Nudelman5d6a98b2021-03-04 14:26:25 -080010449 }
10450 return thermalMitigationResult;
Jack Nudelmanb0b87642020-11-12 15:04:39 -080010451 }
Jack Nudelman5d6a98b2021-03-04 14:26:25 -080010452
10453 return TelephonyManager.THERMAL_MITIGATION_RESULT_SUCCESS;
Jack Nudelmanb0b87642020-11-12 15:04:39 -080010454 }
10455
Jack Nudelman644b91a2021-03-12 14:09:48 -080010456 private static List<String> getThermalMitigationAllowlist(Context context) {
10457 if (sThermalMitigationAllowlistedPackages.isEmpty()) {
10458 for (String pckg : context.getResources()
10459 .getStringArray(R.array.thermal_mitigation_allowlisted_packages)) {
10460 sThermalMitigationAllowlistedPackages.add(pckg);
10461 }
10462 }
10463
10464 return sThermalMitigationAllowlistedPackages;
10465 }
10466
Jack Nudelmane69bbc82021-05-13 10:00:15 -070010467 private boolean isAnyPhoneInEmergencyState() {
10468 TelecomManager tm = mApp.getSystemService(TelecomManager.class);
10469 if (tm.isInEmergencyCall()) {
10470 Log.e(LOG_TAG , "Phone state is not valid. One of the phones is in an emergency call");
10471 return true;
10472 }
10473 for (Phone phone : PhoneFactory.getPhones()) {
10474 if (phone.isInEmergencySmsMode() || phone.isInEcm()) {
10475 Log.e(LOG_TAG, "Phone state is not valid. isInEmergencySmsMode = "
10476 + phone.isInEmergencySmsMode() + " isInEmergencyCallbackMode = "
10477 + phone.isInEcm());
10478 return true;
10479 }
10480 }
10481
10482 return false;
10483 }
10484
Jack Nudelman644b91a2021-03-12 14:09:48 -080010485 /**
10486 * Used by shell commands to add an authorized package name for thermal mitigation.
10487 * @param packageName name of package to be allowlisted
10488 * @param context
10489 */
10490 static void addPackageToThermalMitigationAllowlist(String packageName, Context context) {
10491 sThermalMitigationAllowlistedPackages = getThermalMitigationAllowlist(context);
10492 sThermalMitigationAllowlistedPackages.add(packageName);
10493 }
10494
10495 /**
10496 * Used by shell commands to remove an authorized package name for thermal mitigation.
10497 * @param packageName name of package to remove from allowlist
10498 * @param context
10499 */
10500 static void removePackageFromThermalMitigationAllowlist(String packageName, Context context) {
10501 sThermalMitigationAllowlistedPackages = getThermalMitigationAllowlist(context);
10502 sThermalMitigationAllowlistedPackages.remove(packageName);
10503 }
10504
Jack Nudelmanb0b87642020-11-12 15:04:39 -080010505 /**
10506 * Thermal mitigation request to control functionalities at modem.
10507 *
10508 * @param subId the id of the subscription.
10509 * @param thermalMitigationRequest holds all necessary information to be passed down to modem.
Jack Nudelman644b91a2021-03-12 14:09:48 -080010510 * @param callingPackage the package name of the calling package.
Jack Nudelmanb0b87642020-11-12 15:04:39 -080010511 *
10512 * @return thermalMitigationResult enum as defined in android.telephony.Annotation.
10513 */
10514 @Override
10515 @ThermalMitigationResult
10516 public int sendThermalMitigationRequest(
10517 int subId,
Jack Nudelman644b91a2021-03-12 14:09:48 -080010518 ThermalMitigationRequest thermalMitigationRequest,
10519 String callingPackage) throws IllegalArgumentException {
Jack Nudelmanb0b87642020-11-12 15:04:39 -080010520 enforceModifyPermission();
10521
Jack Nudelman644b91a2021-03-12 14:09:48 -080010522 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
10523 if (!getThermalMitigationAllowlist(getDefaultPhone().getContext())
10524 .contains(callingPackage)) {
10525 throw new SecurityException("Calling package must be configured in the device config. "
10526 + "calling package: " + callingPackage);
10527 }
10528
Jack Nudelmanb0b87642020-11-12 15:04:39 -080010529 WorkSource workSource = getWorkSource(Binder.getCallingUid());
10530 final long identity = Binder.clearCallingIdentity();
10531
10532 int thermalMitigationResult = TelephonyManager.THERMAL_MITIGATION_RESULT_UNKNOWN_ERROR;
10533 try {
10534 int thermalMitigationAction = thermalMitigationRequest.getThermalMitigationAction();
10535 switch (thermalMitigationAction) {
10536 case ThermalMitigationRequest.THERMAL_MITIGATION_ACTION_DATA_THROTTLING:
10537 thermalMitigationResult =
10538 handleDataThrottlingRequest(subId,
Sarah Chinecc78c42022-03-31 21:16:48 -070010539 thermalMitigationRequest.getDataThrottlingRequest(),
10540 callingPackage);
Jack Nudelmanb0b87642020-11-12 15:04:39 -080010541 break;
10542 case ThermalMitigationRequest.THERMAL_MITIGATION_ACTION_VOICE_ONLY:
10543 if (thermalMitigationRequest.getDataThrottlingRequest() != null) {
10544 throw new IllegalArgumentException("dataThrottlingRequest must be null for "
10545 + "ThermalMitigationRequest.THERMAL_MITIGATION_ACTION_VOICE_ONLY");
10546 }
10547
10548 // Ensure that radio is on. If not able to power on due to phone being
10549 // unavailable, return THERMAL_MITIGATION_RESULT_MODEM_NOT_AVAILABLE.
Sooraj Sasindran72cff492021-07-29 09:42:42 -070010550 if (!setRadioPowerForThermal(true)) {
Jack Nudelmanb0b87642020-11-12 15:04:39 -080010551 thermalMitigationResult =
10552 TelephonyManager.THERMAL_MITIGATION_RESULT_MODEM_NOT_AVAILABLE;
10553 break;
10554 }
10555
10556 setDataEnabledForReason(subId, TelephonyManager.DATA_ENABLED_REASON_THERMAL,
Sarah Chinecc78c42022-03-31 21:16:48 -070010557 false, callingPackage);
Jack Nudelmanb0b87642020-11-12 15:04:39 -080010558 thermalMitigationResult = TelephonyManager.THERMAL_MITIGATION_RESULT_SUCCESS;
10559 break;
10560 case ThermalMitigationRequest.THERMAL_MITIGATION_ACTION_RADIO_OFF:
10561 if (thermalMitigationRequest.getDataThrottlingRequest() != null) {
10562 throw new IllegalArgumentException("dataThrottlingRequest must be null for"
10563 + " ThermalMitigationRequest.THERMAL_MITIGATION_ACTION_RADIO_OFF");
10564 }
10565
10566 TelecomAccountRegistry registry = TelecomAccountRegistry.getInstance(null);
10567 if (registry != null) {
Jack Nudelmanb0b87642020-11-12 15:04:39 -080010568 Phone phone = getPhone(subId);
10569 if (phone == null) {
10570 thermalMitigationResult =
10571 TelephonyManager.THERMAL_MITIGATION_RESULT_MODEM_NOT_AVAILABLE;
10572 break;
10573 }
10574
Jack Nudelmane69bbc82021-05-13 10:00:15 -070010575 TelephonyConnectionService service =
10576 registry.getTelephonyConnectionService();
Jack Nudelmanb30ac302021-06-17 15:39:58 -070010577 if (service != null && service.isEmergencyCallPending()) {
Jack Nudelmane69bbc82021-05-13 10:00:15 -070010578 Log.e(LOG_TAG, "An emergency call is pending");
10579 thermalMitigationResult =
10580 TelephonyManager.THERMAL_MITIGATION_RESULT_INVALID_STATE;
10581 break;
10582 } else if (isAnyPhoneInEmergencyState()) {
Jack Nudelmanb0b87642020-11-12 15:04:39 -080010583 thermalMitigationResult =
10584 TelephonyManager.THERMAL_MITIGATION_RESULT_INVALID_STATE;
10585 break;
10586 }
10587 } else {
10588 thermalMitigationResult =
10589 TelephonyManager.THERMAL_MITIGATION_RESULT_MODEM_NOT_AVAILABLE;
10590 break;
10591 }
10592
10593 // Turn radio off. If not able to power off due to phone being unavailable,
10594 // return THERMAL_MITIGATION_RESULT_MODEM_NOT_AVAILABLE.
Sooraj Sasindran72cff492021-07-29 09:42:42 -070010595 if (!setRadioPowerForThermal(false)) {
Jack Nudelmanb0b87642020-11-12 15:04:39 -080010596 thermalMitigationResult =
10597 TelephonyManager.THERMAL_MITIGATION_RESULT_MODEM_NOT_AVAILABLE;
10598 break;
10599 }
10600 thermalMitigationResult =
10601 TelephonyManager.THERMAL_MITIGATION_RESULT_SUCCESS;
10602 break;
10603 default:
10604 throw new IllegalArgumentException("the requested thermalMitigationAction does "
10605 + "not exist. Requested action: " + thermalMitigationAction);
10606 }
10607 } catch (IllegalArgumentException e) {
10608 throw e;
10609 } catch (Exception e) {
10610 Log.e(LOG_TAG, "thermalMitigationRequest. Exception e =" + e);
10611 thermalMitigationResult = TelephonyManager.THERMAL_MITIGATION_RESULT_MODEM_ERROR;
10612 } finally {
10613 Binder.restoreCallingIdentity(identity);
10614 }
10615
10616 if (DBG) {
10617 log("thermalMitigationRequest returning with thermalMitigationResult: "
10618 + thermalMitigationResult);
10619 }
10620
10621 return thermalMitigationResult;
10622 }
Hui Wang641e81c2020-10-12 12:14:23 -070010623
10624 /**
10625 * Set the GbaService Package Name that Telephony will bind to.
10626 *
10627 * @param subId The sim that the GbaService is associated with.
10628 * @param packageName The name of the package to be replaced with.
10629 * @return true if setting the GbaService to bind to succeeded, false if it did not.
10630 */
10631 @Override
10632 public boolean setBoundGbaServiceOverride(int subId, String packageName) {
10633 enforceModifyPermission();
10634
10635 final long identity = Binder.clearCallingIdentity();
10636 try {
10637 return getGbaManager(subId).overrideServicePackage(packageName);
10638 } finally {
10639 Binder.restoreCallingIdentity(identity);
10640 }
10641 }
10642
10643 /**
10644 * Return the package name of the currently bound GbaService.
10645 *
10646 * @param subId The sim that the GbaService is associated with.
10647 * @return the package name of the GbaService configuration, null if GBA is not supported.
10648 */
10649 @Override
10650 public String getBoundGbaService(int subId) {
10651 enforceReadPrivilegedPermission("getBoundGbaServicePackage");
10652
10653 final long identity = Binder.clearCallingIdentity();
10654 try {
10655 return getGbaManager(subId).getServicePackage();
10656 } finally {
10657 Binder.restoreCallingIdentity(identity);
10658 }
10659 }
10660
10661 /**
10662 * Set the release time for telephony to unbind GbaService.
10663 *
10664 * @param subId The sim that the GbaService is associated with.
10665 * @param interval The release time to unbind GbaService by millisecond.
10666 * @return true if setting the GbaService to bind to succeeded, false if it did not.
10667 */
10668 @Override
10669 public boolean setGbaReleaseTimeOverride(int subId, int interval) {
10670 enforceModifyPermission();
10671
10672 final long identity = Binder.clearCallingIdentity();
10673 try {
10674 return getGbaManager(subId).overrideReleaseTime(interval);
10675 } finally {
10676 Binder.restoreCallingIdentity(identity);
10677 }
10678 }
10679
10680 /**
10681 * Return the release time for telephony to unbind GbaService.
10682 *
10683 * @param subId The sim that the GbaService is associated with.
10684 * @return The release time to unbind GbaService by millisecond.
10685 */
10686 @Override
10687 public int getGbaReleaseTime(int subId) {
10688 enforceReadPrivilegedPermission("getGbaReleaseTime");
10689
10690 final long identity = Binder.clearCallingIdentity();
10691 try {
10692 return getGbaManager(subId).getReleaseTime();
10693 } finally {
10694 Binder.restoreCallingIdentity(identity);
10695 }
10696 }
10697
10698 private GbaManager getGbaManager(int subId) {
10699 GbaManager instance = GbaManager.getInstance(subId);
10700 if (instance == null) {
10701 String packageName = mApp.getResources().getString(R.string.config_gba_package);
10702 int releaseTime = mApp.getResources().getInteger(R.integer.config_gba_release_time);
10703 instance = GbaManager.make(mApp, subId, packageName, releaseTime);
10704 }
10705 return instance;
10706 }
Hui Wang761a6682020-10-31 05:12:53 +000010707
10708 /**
10709 * indicate whether the device and the carrier can support
10710 * RCS VoLTE single registration.
10711 */
10712 @Override
10713 public boolean isRcsVolteSingleRegistrationCapable(int subId) {
Brad Ebinger34c09a52021-02-17 23:23:21 +000010714 TelephonyPermissions.enforceAnyPermissionGrantedOrCarrierPrivileges(mApp, subId,
10715 Binder.getCallingUid(), "isRcsVolteSingleRegistrationCapable",
10716 Manifest.permission.PERFORM_IMS_SINGLE_REGISTRATION,
10717 permission.READ_PRIVILEGED_PHONE_STATE);
Hui Wang761a6682020-10-31 05:12:53 +000010718
10719 if (!SubscriptionManager.isValidSubscriptionId(subId)) {
10720 throw new IllegalArgumentException("Invalid Subscription ID: " + subId);
10721 }
10722
10723 final long identity = Binder.clearCallingIdentity();
10724 try {
10725 RcsProvisioningMonitor rpm = RcsProvisioningMonitor.getInstance();
10726 if (rpm != null) {
Hui Wang67af90e2021-06-04 16:57:15 -070010727 Boolean isCapable = rpm.isRcsVolteSingleRegistrationEnabled(subId);
10728 if (isCapable != null) {
10729 return isCapable;
10730 }
Hui Wang761a6682020-10-31 05:12:53 +000010731 }
Hui Wang67af90e2021-06-04 16:57:15 -070010732 throw new ServiceSpecificException(ImsException.CODE_ERROR_SERVICE_UNAVAILABLE,
10733 "service is temporarily unavailable.");
Hui Wang761a6682020-10-31 05:12:53 +000010734 } finally {
10735 Binder.restoreCallingIdentity(identity);
10736 }
10737 }
10738
10739 /**
10740 * Register RCS provisioning callback.
10741 */
10742 @Override
Hui Wang3cac7e52021-01-27 14:45:25 -080010743 public void registerRcsProvisioningCallback(int subId,
Hui Wang761a6682020-10-31 05:12:53 +000010744 IRcsConfigCallback callback) {
Brad Ebinger34c09a52021-02-17 23:23:21 +000010745 TelephonyPermissions.enforceAnyPermissionGrantedOrCarrierPrivileges(mApp, subId,
Hui Wang3cac7e52021-01-27 14:45:25 -080010746 Binder.getCallingUid(), "registerRcsProvisioningCallback",
Brad Ebinger34c09a52021-02-17 23:23:21 +000010747 Manifest.permission.PERFORM_IMS_SINGLE_REGISTRATION,
10748 permission.READ_PRIVILEGED_PHONE_STATE);
Hui Wang761a6682020-10-31 05:12:53 +000010749
10750 if (!SubscriptionManager.isValidSubscriptionId(subId)) {
10751 throw new IllegalArgumentException("Invalid Subscription ID: " + subId);
10752 }
10753 if (!isImsAvailableOnDevice()) {
10754 throw new ServiceSpecificException(ImsException.CODE_ERROR_UNSUPPORTED_OPERATION,
10755 "IMS not available on device.");
10756 }
10757
10758 final long identity = Binder.clearCallingIdentity();
10759 try {
Hui Wang68cd3722021-01-11 20:04:53 -080010760 if (!RcsProvisioningMonitor.getInstance()
Hui Wang3cac7e52021-01-27 14:45:25 -080010761 .registerRcsProvisioningCallback(subId, callback)) {
Brad Ebinger919631e2021-06-02 17:46:35 -070010762 throw new ServiceSpecificException(ImsException.CODE_ERROR_INVALID_SUBSCRIPTION,
10763 "Active subscription not found.");
Hui Wang68cd3722021-01-11 20:04:53 -080010764 }
Hui Wang761a6682020-10-31 05:12:53 +000010765 } finally {
10766 Binder.restoreCallingIdentity(identity);
10767 }
10768 }
10769
10770 /**
10771 * Unregister RCS provisioning callback.
10772 */
10773 @Override
Hui Wang3cac7e52021-01-27 14:45:25 -080010774 public void unregisterRcsProvisioningCallback(int subId,
Hui Wang761a6682020-10-31 05:12:53 +000010775 IRcsConfigCallback callback) {
Brad Ebinger34c09a52021-02-17 23:23:21 +000010776 TelephonyPermissions.enforceAnyPermissionGrantedOrCarrierPrivileges(mApp, subId,
Hui Wang3cac7e52021-01-27 14:45:25 -080010777 Binder.getCallingUid(), "unregisterRcsProvisioningCallback",
Brad Ebinger34c09a52021-02-17 23:23:21 +000010778 Manifest.permission.PERFORM_IMS_SINGLE_REGISTRATION,
10779 permission.READ_PRIVILEGED_PHONE_STATE);
Hui Wang761a6682020-10-31 05:12:53 +000010780
10781 if (!SubscriptionManager.isValidSubscriptionId(subId)) {
10782 throw new IllegalArgumentException("Invalid Subscription ID: " + subId);
10783 }
10784 if (!isImsAvailableOnDevice()) {
10785 throw new ServiceSpecificException(ImsException.CODE_ERROR_UNSUPPORTED_OPERATION,
10786 "IMS not available on device.");
10787 }
10788
10789 final long identity = Binder.clearCallingIdentity();
10790 try {
Hui Wang68cd3722021-01-11 20:04:53 -080010791 RcsProvisioningMonitor.getInstance()
Hui Wang3cac7e52021-01-27 14:45:25 -080010792 .unregisterRcsProvisioningCallback(subId, callback);
Hui Wang761a6682020-10-31 05:12:53 +000010793 } finally {
10794 Binder.restoreCallingIdentity(identity);
10795 }
10796 }
10797
10798 /**
10799 * trigger RCS reconfiguration.
10800 */
10801 public void triggerRcsReconfiguration(int subId) {
Brad Ebinger34c09a52021-02-17 23:23:21 +000010802 TelephonyPermissions.enforceAnyPermissionGranted(mApp, Binder.getCallingUid(),
10803 "triggerRcsReconfiguration",
10804 Manifest.permission.PERFORM_IMS_SINGLE_REGISTRATION);
Hui Wang761a6682020-10-31 05:12:53 +000010805
10806 if (!SubscriptionManager.isValidSubscriptionId(subId)) {
10807 throw new IllegalArgumentException("Invalid Subscription ID: " + subId);
10808 }
10809 if (!isImsAvailableOnDevice()) {
10810 throw new ServiceSpecificException(ImsException.CODE_ERROR_UNSUPPORTED_OPERATION,
10811 "IMS not available on device.");
10812 }
10813
10814 final long identity = Binder.clearCallingIdentity();
10815 try {
10816 RcsProvisioningMonitor.getInstance().requestReconfig(subId);
10817 } finally {
10818 Binder.restoreCallingIdentity(identity);
10819 }
10820 }
10821
10822 /**
10823 * Provide the client configuration parameters of the RCS application.
10824 */
10825 public void setRcsClientConfiguration(int subId, RcsClientConfiguration rcc) {
Brad Ebinger34c09a52021-02-17 23:23:21 +000010826 TelephonyPermissions.enforceAnyPermissionGranted(mApp, Binder.getCallingUid(),
10827 "setRcsClientConfiguration",
10828 Manifest.permission.PERFORM_IMS_SINGLE_REGISTRATION);
Hui Wang761a6682020-10-31 05:12:53 +000010829
10830 if (!SubscriptionManager.isValidSubscriptionId(subId)) {
10831 throw new IllegalArgumentException("Invalid Subscription ID: " + subId);
10832 }
10833 if (!isImsAvailableOnDevice()) {
10834 throw new ServiceSpecificException(ImsException.CODE_ERROR_UNSUPPORTED_OPERATION,
10835 "IMS not available on device.");
10836 }
10837
10838 final long identity = Binder.clearCallingIdentity();
10839
10840 try {
10841 IImsConfig configBinder = getImsConfig(getSlotIndex(subId), ImsFeature.FEATURE_RCS);
10842 if (configBinder == null) {
10843 Rlog.e(LOG_TAG, "null result for setRcsClientConfiguration");
Brad Ebinger919631e2021-06-02 17:46:35 -070010844 throw new ServiceSpecificException(ImsException.CODE_ERROR_INVALID_SUBSCRIPTION,
10845 "could not find the requested subscription");
Hui Wang761a6682020-10-31 05:12:53 +000010846 } else {
10847 configBinder.setRcsClientConfiguration(rcc);
10848 }
joonhunshin3e154242021-09-17 06:33:39 +000010849
10850 RcsStats.getInstance().onRcsClientProvisioningStats(subId,
10851 RCS_CLIENT_PROVISIONING_STATS__EVENT__CLIENT_PARAMS_SENT);
Hui Wang761a6682020-10-31 05:12:53 +000010852 } catch (RemoteException e) {
10853 Rlog.e(LOG_TAG, "fail to setRcsClientConfiguration " + e.getMessage());
Brad Ebinger919631e2021-06-02 17:46:35 -070010854 throw new ServiceSpecificException(ImsException.CODE_ERROR_SERVICE_UNAVAILABLE,
10855 "service is temporarily unavailable.");
Hui Wang761a6682020-10-31 05:12:53 +000010856 } finally {
10857 Binder.restoreCallingIdentity(identity);
10858 }
10859 }
10860
10861 /**
Hui Wangbaaee6a2021-02-19 20:45:36 -080010862 * Enables or disables the test mode for RCS VoLTE single registration.
10863 */
10864 @Override
10865 public void setRcsSingleRegistrationTestModeEnabled(boolean enabled) {
10866 TelephonyPermissions.enforceShellOnly(Binder.getCallingUid(),
10867 "setRcsSingleRegistrationTestModeEnabled");
10868
10869 RcsProvisioningMonitor.getInstance().setTestModeEnabled(enabled);
10870 }
10871
10872 /**
10873 * Gets the test mode for RCS VoLTE single registration.
10874 */
10875 @Override
10876 public boolean getRcsSingleRegistrationTestModeEnabled() {
10877 TelephonyPermissions.enforceShellOnly(Binder.getCallingUid(),
10878 "getRcsSingleRegistrationTestModeEnabled");
10879
10880 return RcsProvisioningMonitor.getInstance().getTestModeEnabled();
10881 }
10882
10883 /**
Hui Wang761a6682020-10-31 05:12:53 +000010884 * Overrides the config of RCS VoLTE single registration enabled for the device.
10885 */
10886 @Override
10887 public void setDeviceSingleRegistrationEnabledOverride(String enabledStr) {
10888 TelephonyPermissions.enforceShellOnly(Binder.getCallingUid(),
10889 "setDeviceSingleRegistrationEnabledOverride");
10890 enforceModifyPermission();
10891
10892 Boolean enabled = "NULL".equalsIgnoreCase(enabledStr) ? null
10893 : Boolean.parseBoolean(enabledStr);
10894 RcsProvisioningMonitor.getInstance().overrideDeviceSingleRegistrationEnabled(enabled);
Brad Ebinger49a72b42021-01-29 00:55:24 +000010895 mApp.imsRcsController.setDeviceSingleRegistrationSupportOverride(enabled);
Hui Wang761a6682020-10-31 05:12:53 +000010896 }
10897
10898 /**
Tyler Gunn92479152021-01-20 16:30:10 -080010899 * Sends a device to device communication message. Only usable via shell.
10900 * @param message message to send.
10901 * @param value message value.
10902 */
10903 @Override
10904 public void sendDeviceToDeviceMessage(int message, int value) {
10905 TelephonyPermissions.enforceShellOnly(Binder.getCallingUid(),
Tyler Gunnbabbda02021-02-10 11:05:02 -080010906 "sendDeviceToDeviceMessage");
Tyler Gunn92479152021-01-20 16:30:10 -080010907 enforceModifyPermission();
10908
10909 final long identity = Binder.clearCallingIdentity();
10910 try {
10911 TelephonyConnectionService service =
10912 TelecomAccountRegistry.getInstance(null).getTelephonyConnectionService();
10913 if (service == null) {
10914 Rlog.e(LOG_TAG, "sendDeviceToDeviceMessage: not in a call.");
10915 return;
10916 }
10917 service.sendTestDeviceToDeviceMessage(message, value);
10918 } finally {
10919 Binder.restoreCallingIdentity(identity);
10920 }
10921 }
10922
Tyler Gunnbabbda02021-02-10 11:05:02 -080010923 /**
10924 * Sets the specified device to device transport active.
10925 * @param transport The transport to set active.
10926 */
10927 @Override
10928 public void setActiveDeviceToDeviceTransport(@NonNull String transport) {
10929 TelephonyPermissions.enforceShellOnly(Binder.getCallingUid(),
10930 "setActiveDeviceToDeviceTransport");
10931 enforceModifyPermission();
10932
10933 final long identity = Binder.clearCallingIdentity();
10934 try {
10935 TelephonyConnectionService service =
10936 TelecomAccountRegistry.getInstance(null).getTelephonyConnectionService();
10937 if (service == null) {
10938 Rlog.e(LOG_TAG, "setActiveDeviceToDeviceTransport: not in a call.");
10939 return;
10940 }
10941 service.setActiveDeviceToDeviceTransport(transport);
10942 } finally {
10943 Binder.restoreCallingIdentity(identity);
10944 }
10945 }
Tyler Gunn92479152021-01-20 16:30:10 -080010946
Tyler Gunnd4339262021-05-03 14:46:49 -070010947 @Override
10948 public void setDeviceToDeviceForceEnabled(boolean isForceEnabled) {
10949 TelephonyPermissions.enforceShellOnly(Binder.getCallingUid(),
10950 "setDeviceToDeviceForceEnabled");
10951
10952 final long identity = Binder.clearCallingIdentity();
10953 try {
10954 Arrays.stream(PhoneFactory.getPhones()).forEach(
10955 p -> {
10956 Phone thePhone = p.getImsPhone();
10957 if (thePhone != null && thePhone instanceof ImsPhone) {
10958 ImsPhone imsPhone = (ImsPhone) thePhone;
10959 CallTracker tracker = imsPhone.getCallTracker();
10960 if (tracker != null && tracker instanceof ImsPhoneCallTracker) {
10961 ImsPhoneCallTracker imsPhoneCallTracker =
10962 (ImsPhoneCallTracker) tracker;
10963 imsPhoneCallTracker.setDeviceToDeviceForceEnabled(isForceEnabled);
10964 }
10965 }
10966 }
10967 );
10968 } finally {
10969 Binder.restoreCallingIdentity(identity);
10970 }
10971 }
10972
Tyler Gunn92479152021-01-20 16:30:10 -080010973 /**
Hui Wang761a6682020-10-31 05:12:53 +000010974 * Gets the config of RCS VoLTE single registration enabled for the device.
10975 */
10976 @Override
10977 public boolean getDeviceSingleRegistrationEnabled() {
10978 enforceReadPrivilegedPermission("getDeviceSingleRegistrationEnabled");
10979 return RcsProvisioningMonitor.getInstance().getDeviceSingleRegistrationEnabled();
10980 }
10981
10982 /**
10983 * Overrides the config of RCS VoLTE single registration enabled for the carrier/subscription.
10984 */
10985 @Override
10986 public boolean setCarrierSingleRegistrationEnabledOverride(int subId, String enabledStr) {
10987 TelephonyPermissions.enforceShellOnly(Binder.getCallingUid(),
10988 "setCarrierSingleRegistrationEnabledOverride");
10989 enforceModifyPermission();
10990
10991 Boolean enabled = "NULL".equalsIgnoreCase(enabledStr) ? null
10992 : Boolean.parseBoolean(enabledStr);
10993 return RcsProvisioningMonitor.getInstance().overrideCarrierSingleRegistrationEnabled(
10994 subId, enabled);
10995 }
10996
10997 /**
10998 * Gets the config of RCS VoLTE single registration enabled for the carrier/subscription.
10999 */
11000 @Override
11001 public boolean getCarrierSingleRegistrationEnabled(int subId) {
11002 enforceReadPrivilegedPermission("getCarrierSingleRegistrationEnabled");
11003 return RcsProvisioningMonitor.getInstance().getCarrierSingleRegistrationEnabled(subId);
11004 }
Chiachang Wangd6d34772020-12-22 11:38:27 +080011005
11006 /**
Hui Wangb647abe2021-02-26 09:33:38 -080011007 * Overrides the ims feature validation result
11008 */
11009 @Override
11010 public boolean setImsFeatureValidationOverride(int subId, String enabledStr) {
11011 TelephonyPermissions.enforceShellOnly(Binder.getCallingUid(),
11012 "setImsFeatureValidationOverride");
11013
11014 Boolean enabled = "NULL".equalsIgnoreCase(enabledStr) ? null
11015 : Boolean.parseBoolean(enabledStr);
11016 return RcsProvisioningMonitor.getInstance().overrideImsFeatureValidation(
11017 subId, enabled);
11018 }
11019
11020 /**
11021 * Gets the ims feature validation override value
11022 */
11023 @Override
11024 public boolean getImsFeatureValidationOverride(int subId) {
11025 TelephonyPermissions.enforceShellOnly(Binder.getCallingUid(),
11026 "getImsFeatureValidationOverride");
11027 return RcsProvisioningMonitor.getInstance().getImsFeatureValidationOverride(subId);
11028 }
11029
11030 /**
Chiachang Wangd6d34772020-12-22 11:38:27 +080011031 * Get the mobile provisioning url that is used to launch a browser to allow users to manage
11032 * their mobile plan.
11033 */
11034 @Override
11035 public String getMobileProvisioningUrl() {
11036 enforceReadPrivilegedPermission("getMobileProvisioningUrl");
11037 final long identity = Binder.clearCallingIdentity();
11038 try {
11039 return getDefaultPhone().getMobileProvisioningUrl();
11040 } finally {
11041 Binder.restoreCallingIdentity(identity);
11042 }
11043 }
Rambo Wanga5cc9b72021-01-07 10:51:54 -080011044
James.cf Linbcdf8b32021-01-14 16:44:13 +080011045 /**
calvinpane4a8a1d2021-01-25 13:51:18 +080011046 * Get the EAB contact from the EAB database.
11047 */
11048 @Override
11049 public String getContactFromEab(String contact) {
11050 TelephonyPermissions.enforceShellOnly(Binder.getCallingUid(), "getContactFromEab");
11051 enforceModifyPermission();
11052 final long identity = Binder.clearCallingIdentity();
11053 try {
11054 return EabUtil.getContactFromEab(getDefaultPhone().getContext(), contact);
11055 } finally {
11056 Binder.restoreCallingIdentity(identity);
11057 }
11058 }
11059
11060 /**
Calvin Pana1434322021-07-01 19:27:01 +080011061 * Get the EAB capability from the EAB database.
11062 */
11063 @Override
11064 public String getCapabilityFromEab(String contact) {
11065 TelephonyPermissions.enforceShellOnly(Binder.getCallingUid(), "getCapabilityFromEab");
11066 enforceModifyPermission();
11067 final long identity = Binder.clearCallingIdentity();
11068 try {
11069 return EabUtil.getCapabilityFromEab(getDefaultPhone().getContext(), contact);
11070 } finally {
11071 Binder.restoreCallingIdentity(identity);
11072 }
11073 }
11074
11075 /**
James.cf Linbcdf8b32021-01-14 16:44:13 +080011076 * Remove the EAB contacts from the EAB database.
11077 */
11078 @Override
11079 public int removeContactFromEab(int subId, String contacts) {
11080 TelephonyPermissions.enforceShellOnly(Binder.getCallingUid(), "removeCapabilitiesFromEab");
11081 enforceModifyPermission();
11082 final long identity = Binder.clearCallingIdentity();
11083 try {
11084 return EabUtil.removeContactFromEab(subId, contacts, getDefaultPhone().getContext());
11085 } finally {
11086 Binder.restoreCallingIdentity(identity);
11087 }
11088 }
11089
Rambo Wanga5cc9b72021-01-07 10:51:54 -080011090 @Override
James.cf Lin4b784aa2021-01-31 03:25:15 +080011091 public boolean getDeviceUceEnabled() {
11092 TelephonyPermissions.enforceShellOnly(Binder.getCallingUid(), "getDeviceUceEnabled");
11093 final long identity = Binder.clearCallingIdentity();
11094 try {
11095 return mApp.getDeviceUceEnabled();
11096 } finally {
11097 Binder.restoreCallingIdentity(identity);
11098 }
11099 }
11100
11101 @Override
11102 public void setDeviceUceEnabled(boolean isEnabled) {
11103 TelephonyPermissions.enforceShellOnly(Binder.getCallingUid(), "setDeviceUceEnabled");
11104 final long identity = Binder.clearCallingIdentity();
11105 try {
11106 mApp.setDeviceUceEnabled(isEnabled);
11107 } finally {
11108 Binder.restoreCallingIdentity(identity);
11109 }
11110 }
11111
Brad Ebinger14d467f2021-02-12 06:18:28 +000011112 /**
11113 * Add new feature tags to the Set used to calculate the capabilities in PUBLISH.
11114 * @return current RcsContactUceCapability instance that will be used for PUBLISH.
11115 */
11116 // Used for SHELL command only right now.
11117 @Override
11118 public RcsContactUceCapability addUceRegistrationOverrideShell(int subId,
11119 List<String> featureTags) {
11120 TelephonyPermissions.enforceShellOnly(Binder.getCallingUid(),
11121 "addUceRegistrationOverrideShell");
11122 final long identity = Binder.clearCallingIdentity();
11123 try {
11124 return mApp.imsRcsController.addUceRegistrationOverrideShell(subId,
11125 new ArraySet<>(featureTags));
11126 } catch (ImsException e) {
11127 throw new ServiceSpecificException(e.getCode(), e.getMessage());
11128 } finally {
11129 Binder.restoreCallingIdentity(identity);
11130 }
11131 }
11132
11133 /**
11134 * Remove existing feature tags to the Set used to calculate the capabilities in PUBLISH.
11135 * @return current RcsContactUceCapability instance that will be used for PUBLISH.
11136 */
11137 // Used for SHELL command only right now.
11138 @Override
11139 public RcsContactUceCapability removeUceRegistrationOverrideShell(int subId,
11140 List<String> featureTags) {
11141 TelephonyPermissions.enforceShellOnly(Binder.getCallingUid(),
11142 "removeUceRegistrationOverrideShell");
11143 final long identity = Binder.clearCallingIdentity();
11144 try {
11145 return mApp.imsRcsController.removeUceRegistrationOverrideShell(subId,
11146 new ArraySet<>(featureTags));
11147 } catch (ImsException e) {
11148 throw new ServiceSpecificException(e.getCode(), e.getMessage());
11149 } finally {
11150 Binder.restoreCallingIdentity(identity);
11151 }
11152 }
11153
11154 /**
11155 * Clear all overrides in the Set used to calculate the capabilities in PUBLISH.
11156 * @return current RcsContactUceCapability instance that will be used for PUBLISH.
11157 */
11158 // Used for SHELL command only right now.
11159 @Override
11160 public RcsContactUceCapability clearUceRegistrationOverrideShell(int subId) {
11161 TelephonyPermissions.enforceShellOnly(Binder.getCallingUid(),
11162 "clearUceRegistrationOverrideShell");
11163 final long identity = Binder.clearCallingIdentity();
11164 try {
11165 return mApp.imsRcsController.clearUceRegistrationOverrideShell(subId);
11166 } catch (ImsException e) {
11167 throw new ServiceSpecificException(e.getCode(), e.getMessage());
11168 } finally {
11169 Binder.restoreCallingIdentity(identity);
11170 }
11171 }
11172
11173 /**
11174 * @return current RcsContactUceCapability instance that will be used for PUBLISH.
11175 */
11176 // Used for SHELL command only right now.
11177 @Override
11178 public RcsContactUceCapability getLatestRcsContactUceCapabilityShell(int subId) {
11179 TelephonyPermissions.enforceShellOnly(Binder.getCallingUid(),
11180 "getLatestRcsContactUceCapabilityShell");
11181 final long identity = Binder.clearCallingIdentity();
11182 try {
11183 return mApp.imsRcsController.getLatestRcsContactUceCapabilityShell(subId);
11184 } catch (ImsException e) {
11185 throw new ServiceSpecificException(e.getCode(), e.getMessage());
11186 } finally {
11187 Binder.restoreCallingIdentity(identity);
11188 }
11189 }
11190
11191 /**
11192 * Returns the last PIDF XML sent to the network during the last PUBLISH or "none" if the
11193 * device does not have an active PUBLISH.
11194 */
11195 // Used for SHELL command only right now.
11196 @Override
11197 public String getLastUcePidfXmlShell(int subId) {
11198 TelephonyPermissions.enforceShellOnly(Binder.getCallingUid(), "uceGetLastPidfXml");
11199 final long identity = Binder.clearCallingIdentity();
11200 try {
11201 return mApp.imsRcsController.getLastUcePidfXmlShell(subId);
11202 } catch (ImsException e) {
11203 throw new ServiceSpecificException(e.getCode(), e.getMessage());
11204 } finally {
11205 Binder.restoreCallingIdentity(identity);
11206 }
11207 }
11208
James.cf Line8713a42021-04-29 16:04:26 +080011209 /**
11210 * Remove UCE requests cannot be sent to the network status.
11211 */
11212 // Used for SHELL command only right now.
11213 @Override
11214 public boolean removeUceRequestDisallowedStatus(int subId) {
11215 TelephonyPermissions.enforceShellOnly(Binder.getCallingUid(), "uceRemoveDisallowedStatus");
11216 final long identity = Binder.clearCallingIdentity();
11217 try {
11218 return mApp.imsRcsController.removeUceRequestDisallowedStatus(subId);
11219 } catch (ImsException e) {
11220 throw new ServiceSpecificException(e.getCode(), e.getMessage());
11221 } finally {
11222 Binder.restoreCallingIdentity(identity);
11223 }
11224 }
11225
James.cf Lin18bb9002021-05-25 01:37:38 +080011226 /**
11227 * Remove UCE requests cannot be sent to the network status.
11228 */
11229 // Used for SHELL command only.
11230 @Override
11231 public boolean setCapabilitiesRequestTimeout(int subId, long timeoutAfterMs) {
11232 TelephonyPermissions.enforceShellOnly(Binder.getCallingUid(), "setCapRequestTimeout");
11233 final long identity = Binder.clearCallingIdentity();
11234 try {
11235 return mApp.imsRcsController.setCapabilitiesRequestTimeout(subId, timeoutAfterMs);
11236 } catch (ImsException e) {
11237 throw new ServiceSpecificException(e.getCode(), e.getMessage());
11238 } finally {
11239 Binder.restoreCallingIdentity(identity);
11240 }
11241 }
Brad Ebinger14d467f2021-02-12 06:18:28 +000011242
James.cf Lin4b784aa2021-01-31 03:25:15 +080011243 @Override
Rambo Wanga5cc9b72021-01-07 10:51:54 -080011244 public void setSignalStrengthUpdateRequest(int subId, SignalStrengthUpdateRequest request,
11245 String callingPackage) {
11246 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
11247 mApp, subId, "setSignalStrengthUpdateRequest");
11248
11249 final int callingUid = Binder.getCallingUid();
11250 // Verify that tha callingPackage belongs to the calling UID
11251 mApp.getSystemService(AppOpsManager.class)
11252 .checkPackage(callingUid, callingPackage);
11253
Rambo Wang3607f502021-02-01 21:51:40 -080011254 validateSignalStrengthUpdateRequest(mApp, request, callingUid);
Rambo Wanga5cc9b72021-01-07 10:51:54 -080011255
11256 final long identity = Binder.clearCallingIdentity();
11257 try {
11258 Object result = sendRequest(CMD_SET_SIGNAL_STRENGTH_UPDATE_REQUEST,
11259 new Pair<Integer, SignalStrengthUpdateRequest>(callingUid, request), subId);
11260
11261 if (result instanceof IllegalStateException) {
11262 throw (IllegalStateException) result;
11263 }
11264 } finally {
11265 Binder.restoreCallingIdentity(identity);
11266 }
11267 }
11268
11269 @Override
11270 public void clearSignalStrengthUpdateRequest(int subId, SignalStrengthUpdateRequest request,
11271 String callingPackage) {
11272 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
11273 mApp, subId, "clearSignalStrengthUpdateRequest");
11274
11275 final int callingUid = Binder.getCallingUid();
11276 // Verify that tha callingPackage belongs to the calling UID
11277 mApp.getSystemService(AppOpsManager.class)
11278 .checkPackage(callingUid, callingPackage);
11279
11280 final long identity = Binder.clearCallingIdentity();
11281 try {
11282 Object result = sendRequest(CMD_CLEAR_SIGNAL_STRENGTH_UPDATE_REQUEST,
11283 new Pair<Integer, SignalStrengthUpdateRequest>(callingUid, request), subId);
11284
11285 if (result instanceof IllegalStateException) {
11286 throw (IllegalStateException) result;
11287 }
11288 } finally {
11289 Binder.restoreCallingIdentity(identity);
11290 }
11291 }
11292
Rambo Wang3607f502021-02-01 21:51:40 -080011293 private static void validateSignalStrengthUpdateRequest(Context context,
11294 SignalStrengthUpdateRequest request, int callingUid) {
Rambo Wanga5cc9b72021-01-07 10:51:54 -080011295 if (callingUid == Process.PHONE_UID || callingUid == Process.SYSTEM_UID) {
11296 // phone/system process do not have further restriction on request
11297 return;
11298 }
11299
11300 // Applications has restrictions on how to use the request:
Rambo Wang3607f502021-02-01 21:51:40 -080011301 // Non-system callers need permission to set mIsSystemThresholdReportingRequestedWhileIdle
Rambo Wanga5cc9b72021-01-07 10:51:54 -080011302 if (request.isSystemThresholdReportingRequestedWhileIdle()) {
Rambo Wang3607f502021-02-01 21:51:40 -080011303 context.enforceCallingOrSelfPermission(
11304 android.Manifest.permission.LISTEN_ALWAYS_REPORTED_SIGNAL_STRENGTH,
11305 "validateSignalStrengthUpdateRequest");
Rambo Wanga5cc9b72021-01-07 10:51:54 -080011306 }
11307
11308 for (SignalThresholdInfo info : request.getSignalThresholdInfos()) {
Nagendra Prasad Nagarle Basavarajufee544c2022-12-07 16:34:52 +000011309 // Only system caller can set mHysteresisMs/mIsEnabled.
Rambo Wanga5cc9b72021-01-07 10:51:54 -080011310 if (info.getHysteresisMs() != SignalThresholdInfo.HYSTERESIS_MS_DISABLED
Rambo Wanga5cc9b72021-01-07 10:51:54 -080011311 || info.isEnabled()) {
11312 throw new IllegalArgumentException(
11313 "Only system can set hide fields in SignalThresholdInfo");
11314 }
11315
11316 // Thresholds length for each RAN need in range. This has been validated in
11317 // SignalThresholdInfo#Builder#setThreshold. Here we prevent apps calling hide method
11318 // setThresholdUnlimited (e.g. through reflection) with too short or too long thresholds
11319 final int[] thresholds = info.getThresholds();
11320 Objects.requireNonNull(thresholds);
11321 if (thresholds.length < SignalThresholdInfo.getMinimumNumberOfThresholdsAllowed()
11322 || thresholds.length
11323 > SignalThresholdInfo.getMaximumNumberOfThresholdsAllowed()) {
11324 throw new IllegalArgumentException(
11325 "thresholds length is out of range: " + thresholds.length);
11326 }
11327 }
11328 }
SongFerngWang8236caa2021-01-17 21:51:44 +080011329
11330 /**
11331 * Gets the current phone capability.
11332 *
11333 * Requires carrier privileges or READ_PRECISE_PHONE_STATE permission.
11334 * @return the PhoneCapability which describes the data connection capability of modem.
11335 * It's used to evaluate possible phone config change, for example from single
11336 * SIM device to multi-SIM device.
11337 */
11338 @Override
11339 public PhoneCapability getPhoneCapability() {
11340 enforceReadPrivilegedPermission("getPhoneCapability");
11341 final long identity = Binder.clearCallingIdentity();
11342 try {
11343 return mPhoneConfigurationManager.getCurrentPhoneCapability();
11344 } finally {
11345 Binder.restoreCallingIdentity(identity);
11346 }
11347 }
Michele Berionne5e411512020-11-13 02:36:59 +000011348
11349 /**
11350 * Prepare TelephonyManager for an unattended reboot. The reboot is
11351 * required to be done shortly after the API is invoked.
11352 */
11353 @Override
11354 @TelephonyManager.PrepareUnattendedRebootResult
11355 public int prepareForUnattendedReboot() {
Rafael Higuera Silvad9630642021-09-20 15:32:01 +000011356 WorkSource workSource = getWorkSource(Binder.getCallingUid());
Michele Berionne5e411512020-11-13 02:36:59 +000011357 enforceRebootPermission();
11358
11359 final long identity = Binder.clearCallingIdentity();
11360 try {
Rafael Higuera Silvad9630642021-09-20 15:32:01 +000011361 return (int) sendRequest(CMD_PREPARE_UNATTENDED_REBOOT, null, workSource);
Michele Berionne5e411512020-11-13 02:36:59 +000011362 } finally {
11363 Binder.restoreCallingIdentity(identity);
11364 }
11365 }
Hongbo Zeng156aa4a2021-02-08 21:50:28 +080011366
11367 /**
11368 * Request to get the current slicing configuration including URSP rules and
11369 * NSSAIs (configured, allowed and rejected).
11370 *
11371 * Requires carrier privileges or READ_PRIVILEGED_PHONE_STATE permission.
11372 */
11373 @Override
11374 public void getSlicingConfig(ResultReceiver callback) {
Hongbo Zeng1b2063d2022-02-21 01:33:03 +000011375 TelephonyPermissions
11376 .enforceCallingOrSelfReadPrivilegedPhoneStatePermissionOrCarrierPrivilege(
11377 mApp, SubscriptionManager.INVALID_SUBSCRIPTION_ID, "getSlicingConfig");
Hongbo Zeng156aa4a2021-02-08 21:50:28 +080011378
11379 final long identity = Binder.clearCallingIdentity();
11380 try {
11381 Phone phone = getDefaultPhone();
11382 sendRequestAsync(CMD_GET_SLICING_CONFIG, callback, phone, null);
11383 } finally {
11384 Binder.restoreCallingIdentity(identity);
11385 }
11386 }
Hunsuk Choi3b742d62021-10-25 19:48:34 +000011387
11388 /**
Sarah Chin2ec39f62022-08-31 17:03:26 -070011389 * Check whether the given premium capability is available for purchase from the carrier.
11390 *
11391 * @param capability The premium capability to check.
11392 * @param subId The subId to check the premium capability for.
11393 *
11394 * @return Whether the given premium capability is available to purchase.
11395 */
11396 @Override
11397 public boolean isPremiumCapabilityAvailableForPurchase(int capability, int subId) {
11398 if (!TelephonyPermissions.checkCallingOrSelfReadNonDangerousPhoneStateNoThrow(
11399 mApp, "isPremiumCapabilityAvailableForPurchase")) {
11400 log("Premium capability "
11401 + TelephonyManager.convertPremiumCapabilityToString(capability)
11402 + " is not available for purchase due to missing permissions.");
11403 throw new SecurityException("isPremiumCapabilityAvailableForPurchase requires "
11404 + "permission READ_BASIC_PHONE_STATE.");
11405 }
11406
11407 Phone phone = getPhone(subId);
Thomas Nguyen7216ed62022-11-29 16:45:31 -080011408 if (phone == null) {
11409 loge("isPremiumCapabilityAvailableForPurchase: phone is null, subId=" + subId);
11410 return false;
11411 }
Sarah Chin2ec39f62022-08-31 17:03:26 -070011412 final long identity = Binder.clearCallingIdentity();
11413 try {
Sarah Chin46355ba2022-11-01 23:51:16 -070011414 return SlicePurchaseController.getInstance(phone)
Sarah Chin2ec39f62022-08-31 17:03:26 -070011415 .isPremiumCapabilityAvailableForPurchase(capability);
11416 } finally {
11417 Binder.restoreCallingIdentity(identity);
11418 }
11419 }
11420
11421 /**
11422 * Purchase the given premium capability from the carrier.
11423 *
11424 * @param capability The premium capability to purchase.
11425 * @param callback The result of the purchase request.
11426 * @param subId The subId to purchase the premium capability for.
11427 */
11428 @Override
11429 public void purchasePremiumCapability(int capability, IIntegerConsumer callback, int subId) {
11430 log("purchasePremiumCapability: capability="
11431 + TelephonyManager.convertPremiumCapabilityToString(capability) + ", caller="
11432 + getCurrentPackageName());
11433
11434 if (!TelephonyPermissions.checkCallingOrSelfReadNonDangerousPhoneStateNoThrow(
11435 mApp, "purchasePremiumCapability")) {
11436 log("purchasePremiumCapability "
11437 + TelephonyManager.convertPremiumCapabilityToString(capability)
11438 + " failed due to missing permissions.");
11439 throw new SecurityException("purchasePremiumCapability requires permission "
11440 + "READ_BASIC_PHONE_STATE.");
Sarah Chin532d6bb2022-12-28 22:50:43 -080011441 } else if (!TelephonyPermissions.checkInternetPermissionNoThrow(
11442 mApp, "purchasePremiumCapability")) {
11443 log("purchasePremiumCapability "
11444 + TelephonyManager.convertPremiumCapabilityToString(capability)
11445 + " failed due to missing permissions.");
11446 throw new SecurityException("purchasePremiumCapability requires permission INTERNET.");
Sarah Chin2ec39f62022-08-31 17:03:26 -070011447 }
11448
11449 Phone phone = getPhone(subId);
Sarah Chin19694112022-12-06 15:41:37 -080011450 if (phone == null) {
11451 try {
11452 int result = TelephonyManager.PURCHASE_PREMIUM_CAPABILITY_RESULT_REQUEST_FAILED;
11453 callback.accept(result);
11454 loge("purchasePremiumCapability: phone is null, subId=" + subId);
11455 } catch (RemoteException e) {
11456 String logStr = "Purchase premium capability "
11457 + TelephonyManager.convertPremiumCapabilityToString(capability)
11458 + " failed due to RemoteException handling null phone: " + e;
11459 if (DBG) log(logStr);
11460 AnomalyReporter.reportAnomaly(
11461 UUID.fromString(PURCHASE_PREMIUM_CAPABILITY_ERROR_UUID), logStr);
11462 }
11463 return;
11464 }
Sarah Chin532d6bb2022-12-28 22:50:43 -080011465
11466 String callingProcess;
Sarah Chin71b3a852022-09-28 15:54:19 -070011467 try {
Sarah Chin532d6bb2022-12-28 22:50:43 -080011468 callingProcess = mApp.getPackageManager().getApplicationInfo(
11469 getCurrentPackageName(), 0).processName;
Sarah Chin71b3a852022-09-28 15:54:19 -070011470 } catch (PackageManager.NameNotFoundException e) {
Sarah Chin532d6bb2022-12-28 22:50:43 -080011471 callingProcess = getCurrentPackageName();
Sarah Chin71b3a852022-09-28 15:54:19 -070011472 }
Sarah Chin532d6bb2022-12-28 22:50:43 -080011473
11474 boolean isVisible = false;
11475 ActivityManager am = mApp.getSystemService(ActivityManager.class);
11476 if (am != null) {
11477 List<ActivityManager.RunningAppProcessInfo> processes = am.getRunningAppProcesses();
11478 if (processes != null) {
11479 for (ActivityManager.RunningAppProcessInfo process : processes) {
11480 log("purchasePremiumCapability: process " + process.processName
11481 + "has importance " + process.importance);
11482 if (process.processName.equals(callingProcess) && process.importance
11483 <= ActivityManager.RunningAppProcessInfo.IMPORTANCE_VISIBLE) {
11484 isVisible = true;
11485 break;
11486 }
11487 }
11488 }
11489 }
11490
11491 if (!isVisible) {
11492 try {
11493 int result = TelephonyManager.PURCHASE_PREMIUM_CAPABILITY_RESULT_NOT_FOREGROUND;
11494 callback.accept(result);
11495 loge("purchasePremiumCapability: " + callingProcess + " is not in the foreground.");
11496 } catch (RemoteException e) {
11497 String logStr = "Purchase premium capability "
11498 + TelephonyManager.convertPremiumCapabilityToString(capability)
11499 + " failed due to RemoteException handling background application: " + e;
11500 if (DBG) log(logStr);
11501 AnomalyReporter.reportAnomaly(
11502 UUID.fromString(PURCHASE_PREMIUM_CAPABILITY_ERROR_UUID), logStr);
11503 }
11504 return;
11505 }
11506
Sarah Chin71b3a852022-09-28 15:54:19 -070011507 sendRequestAsync(CMD_PURCHASE_PREMIUM_CAPABILITY,
Sarah Chinb8218c22023-01-04 13:35:29 -080011508 new PurchasePremiumCapabilityArgument(capability, callback), phone, null);
Sarah Chin2ec39f62022-08-31 17:03:26 -070011509 }
11510
11511 /**
Hunsuk Choi3b742d62021-10-25 19:48:34 +000011512 * Register an IMS connection state callback
11513 */
11514 @Override
Hunsuk Choi89bd22c2021-11-01 13:04:54 +000011515 public void registerImsStateCallback(int subId, int feature, IImsStateCallback cb,
11516 String callingPackage) {
Hunsuk Choi3b742d62021-10-25 19:48:34 +000011517 if (feature == ImsFeature.FEATURE_MMTEL) {
11518 // ImsMmTelManager
11519 // The following also checks READ_PRIVILEGED_PHONE_STATE.
11520 TelephonyPermissions
11521 .enforceCallingOrSelfReadPrecisePhoneStatePermissionOrCarrierPrivilege(
11522 mApp, subId, "registerImsStateCallback");
11523 } else if (feature == ImsFeature.FEATURE_RCS) {
11524 // ImsRcsManager or SipDelegateManager
11525 TelephonyPermissions.enforceAnyPermissionGrantedOrCarrierPrivileges(mApp, subId,
11526 Binder.getCallingUid(), "registerImsStateCallback",
11527 Manifest.permission.READ_PRIVILEGED_PHONE_STATE,
11528 Manifest.permission.READ_PRECISE_PHONE_STATE,
11529 Manifest.permission.ACCESS_RCS_USER_CAPABILITY_EXCHANGE,
11530 Manifest.permission.PERFORM_IMS_SINGLE_REGISTRATION);
11531 }
11532
11533 if (!ImsManager.isImsSupportedOnDevice(mApp)) {
11534 throw new ServiceSpecificException(ImsException.CODE_ERROR_UNSUPPORTED_OPERATION,
11535 "IMS not available on device.");
11536 }
11537
11538 if (subId == SubscriptionManager.DEFAULT_SUBSCRIPTION_ID) {
11539 throw new ServiceSpecificException(ImsException.CODE_ERROR_INVALID_SUBSCRIPTION);
11540 }
11541
11542 ImsStateCallbackController controller = ImsStateCallbackController.getInstance();
11543 if (controller == null) {
11544 throw new ServiceSpecificException(ImsException.CODE_ERROR_UNSUPPORTED_OPERATION,
11545 "IMS not available on device.");
11546 }
11547
Hunsuk Choi89bd22c2021-11-01 13:04:54 +000011548 if (callingPackage == null) {
11549 callingPackage = getCurrentPackageName();
11550 }
11551
Hunsuk Choi3b742d62021-10-25 19:48:34 +000011552 final long token = Binder.clearCallingIdentity();
11553 try {
11554 int slotId = getSlotIndexOrException(subId);
Hunsuk Choi89bd22c2021-11-01 13:04:54 +000011555 controller.registerImsStateCallback(subId, feature, cb, callingPackage);
Hunsuk Choi3b742d62021-10-25 19:48:34 +000011556 } catch (ImsException e) {
11557 throw new ServiceSpecificException(e.getCode());
11558 } finally {
11559 Binder.restoreCallingIdentity(token);
11560 }
11561 }
11562
11563 /**
11564 * Unregister an IMS connection state callback
11565 */
11566 @Override
11567 public void unregisterImsStateCallback(IImsStateCallback cb) {
11568 final long token = Binder.clearCallingIdentity();
11569 ImsStateCallbackController controller = ImsStateCallbackController.getInstance();
11570 if (controller == null) {
11571 return;
11572 }
11573 try {
11574 controller.unregisterImsStateCallback(cb);
11575 } finally {
11576 Binder.restoreCallingIdentity(token);
11577 }
11578 }
Sooraj Sasindranfae41b32021-10-26 02:10:05 -070011579
11580 /**
11581 * @return {@CellIdentity} last known cell identity {@CellIdentity}.
11582 *
11583 * Require {@link android.Manifest.permission#ACCESS_FINE_LOCATION} and
11584 * com.android.phone.permission.ACCESS_LAST_KNOWN_CELL_ID, otherwise throws
11585 * SecurityException.
11586 * If there is current registered network this value will be same as the registered cell
11587 * identity. If the device goes out of service the previous cell identity is cached and
11588 * will be returned. If the cache age of the Cell identity is more than 24 hours
11589 * it will be cleared and null will be returned.
11590 *
11591 */
11592 @Override
11593 public @Nullable CellIdentity getLastKnownCellIdentity(int subId, String callingPackage,
11594 String callingFeatureId) {
11595 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
11596 LocationAccessPolicy.LocationPermissionResult fineLocationResult =
11597 LocationAccessPolicy.checkLocationPermission(mApp,
11598 new LocationAccessPolicy.LocationPermissionQuery.Builder()
11599 .setCallingPackage(callingPackage)
11600 .setCallingFeatureId(callingFeatureId)
11601 .setCallingPid(Binder.getCallingPid())
11602 .setCallingUid(Binder.getCallingUid())
11603 .setMethod("getLastKnownCellIdentity")
11604 .setLogAsInfo(true)
11605 .setMinSdkVersionForFine(Build.VERSION_CODES.Q)
11606 .setMinSdkVersionForCoarse(Build.VERSION_CODES.Q)
11607 .setMinSdkVersionForEnforcement(Build.VERSION_CODES.Q)
11608 .build());
11609
11610 boolean hasFinePermission =
11611 fineLocationResult == LocationAccessPolicy.LocationPermissionResult.ALLOWED;
11612 if (!hasFinePermission
11613 || !TelephonyPermissions.checkLastKnownCellIdAccessPermission(mApp)) {
11614 throw new SecurityException("getLastKnownCellIdentity need ACCESS_FINE_LOCATION "
Rambo Wang918993a2022-04-27 09:08:36 -070011615 + "and ACCESS_LAST_KNOWN_CELL_ID permission.");
Sooraj Sasindranfae41b32021-10-26 02:10:05 -070011616 }
11617
11618 final long identity = Binder.clearCallingIdentity();
11619 try {
11620 Phone phone = getPhone(subId);
11621 if (phone == null) return null;
11622 ServiceStateTracker sst = phone.getServiceStateTracker();
11623 if (sst == null) return null;
11624 return sst.getLastKnownCellIdentity();
11625 } finally {
11626 Binder.restoreCallingIdentity(identity);
11627 }
11628 }
Jack Yu4c0a5502021-12-03 23:58:26 -080011629
jimsun3b9ccac2021-10-26 15:01:23 +080011630 /**
11631 * Sets the modem service class Name that Telephony will bind to.
11632 *
11633 * @param serviceName The class name of the modem service.
11634 * @return true if the operation is succeed, otherwise false.
11635 */
11636 public boolean setModemService(String serviceName) {
11637 Log.d(LOG_TAG, "setModemService - " + serviceName);
11638 TelephonyPermissions.enforceShellOnly(Binder.getCallingUid(), "setModemService");
11639 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(mApp,
11640 SubscriptionManager.INVALID_SUBSCRIPTION_ID,
11641 "setModemService");
11642 return mPhoneConfigurationManager.setModemService(serviceName);
11643 }
11644
11645 /**
11646 * Return the class name of the currently bounded modem service.
11647 *
11648 * @return the class name of the modem service.
11649 */
11650 public String getModemService() {
11651 String result;
11652 Log.d(LOG_TAG, "getModemService");
11653 TelephonyPermissions.enforceShellOnly(Binder.getCallingUid(), "getModemService");
11654 TelephonyPermissions
11655 .enforceCallingOrSelfReadPrivilegedPhoneStatePermissionOrCarrierPrivilege(
11656 mApp, SubscriptionManager.INVALID_SUBSCRIPTION_ID,
11657 "getModemService");
11658 result = mPhoneConfigurationManager.getModemService();
11659 Log.d(LOG_TAG, "result = " + result);
11660 return result;
11661 }
Hunter Knepshield2b076fa2022-01-19 02:26:22 -080011662
11663 @Override
11664 public void setVoiceServiceStateOverride(int subId, boolean hasService, String callingPackage) {
11665 // Only telecom (and shell, for CTS purposes) is allowed to call this method.
11666 mApp.enforceCallingOrSelfPermission(
11667 permission.BIND_TELECOM_CONNECTION_SERVICE, "setVoiceServiceStateOverride");
11668 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
11669
11670 final long identity = Binder.clearCallingIdentity();
11671 try {
11672 Phone phone = getPhone(subId);
11673 if (phone == null) return;
11674 phone.setVoiceServiceStateOverride(hasService);
11675 } finally {
11676 Binder.restoreCallingIdentity(identity);
11677 }
11678 }
Muralidhar Reddy4e5a8012022-05-11 14:49:00 +000011679
11680 /**
11681 * set removable eSIM as default eUICC.
11682 *
11683 * @hide
11684 */
11685 @Override
11686 public void setRemovableEsimAsDefaultEuicc(boolean isDefault, String callingPackage) {
11687 enforceModifyPermission();
11688 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
11689
11690 final long identity = Binder.clearCallingIdentity();
11691 try {
11692 UiccController.getInstance().setRemovableEsimAsDefaultEuicc(isDefault);
11693 } finally {
11694 Binder.restoreCallingIdentity(identity);
11695 }
11696 }
11697
11698 /**
11699 * Returns whether the removable eSIM is default eUICC or not.
11700 *
11701 * @hide
11702 */
11703 @Override
11704 public boolean isRemovableEsimDefaultEuicc(String callingPackage) {
11705 enforceReadPrivilegedPermission("isRemovableEsimDefaultEuicc");
11706 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
11707
11708 final long identity = Binder.clearCallingIdentity();
11709 try {
11710 return UiccController.getInstance().isRemovableEsimDefaultEuicc();
11711 } finally {
11712 Binder.restoreCallingIdentity(identity);
11713 }
11714 }
11715
Aishwarya Mallampatifbc70d32022-11-10 20:33:02 +000011716 /**
11717 * Get the component name of the default app to direct respond-via-message intent for the
11718 * user associated with this subscription, update the cache if there is no respond-via-message
11719 * application currently configured for this user.
11720 * @return component name of the app and class to direct Respond Via Message intent to, or
11721 * {@code null} if the functionality is not supported.
11722 * @hide
11723 */
11724 @Override
11725 public @Nullable ComponentName getDefaultRespondViaMessageApplication(int subId,
11726 boolean updateIfNeeded) {
11727 enforceInteractAcrossUsersPermission("getDefaultRespondViaMessageApplication");
Muralidhar Reddy4e5a8012022-05-11 14:49:00 +000011728
Aishwarya Mallampatifbc70d32022-11-10 20:33:02 +000011729 Context context = getPhone(subId).getContext();
11730 UserHandle userHandle = null;
11731 final long identity = Binder.clearCallingIdentity();
11732 try {
11733 userHandle = TelephonyUtils.getSubscriptionUserHandle(context, subId);
11734 } finally {
11735 Binder.restoreCallingIdentity(identity);
11736 }
11737 return SmsApplication.getDefaultRespondViaMessageApplicationAsUser(context,
11738 updateIfNeeded, userHandle);
11739 }
Jack Yuf5badd92022-12-08 00:50:53 -080011740
11741 /**
Gil Cukierman1c0eb932022-12-06 22:28:24 +000011742 * Set whether the device is able to connect with null ciphering or integrity
11743 * algorithms. This is a global setting and will apply to all active subscriptions
11744 * and all new subscriptions after this.
11745 *
11746 * @param enabled when true, null cipher and integrity algorithms are allowed.
11747 * @hide
11748 */
11749 @Override
11750 @RequiresPermission(android.Manifest.permission.MODIFY_PHONE_STATE)
11751 public void setNullCipherAndIntegrityEnabled(boolean enabled) {
11752 enforceModifyPermission();
11753 checkForNullCipherAndIntegritySupport();
11754
11755 // Persist the state of our preference. Each GsmCdmaPhone instance is responsible
11756 // for listening to these preference changes and applying them immediately.
11757 SharedPreferences.Editor editor = mTelephonySharedPreferences.edit();
11758 editor.putBoolean(Phone.PREF_NULL_CIPHER_AND_INTEGRITY_ENABLED, enabled);
11759 editor.apply();
11760
11761 for (Phone phone: PhoneFactory.getPhones()) {
11762 phone.handleNullCipherEnabledChange();
11763 }
11764 }
11765
11766
11767 /**
11768 * Get whether the device is able to connect with null ciphering or integrity
11769 * algorithms. Note that this retrieves the phone-global preference and not
11770 * the state of the radio.
11771 *
11772 * @throws SecurityException if {@link permission#MODIFY_PHONE_STATE} is not satisfied
11773 * @throws UnsupportedOperationException if the device does not support the minimum HAL
11774 * version for this feature.
11775 * @hide
11776 */
11777 @Override
11778 @RequiresPermission(android.Manifest.permission.READ_PHONE_STATE)
11779 public boolean isNullCipherAndIntegrityPreferenceEnabled() {
11780 enforceReadPermission();
11781 checkForNullCipherAndIntegritySupport();
11782 return getDefaultPhone().getNullCipherAndIntegrityEnabledPreference();
11783 }
11784
11785 private void checkForNullCipherAndIntegritySupport() {
11786 if (getHalVersion(HAL_SERVICE_NETWORK) < MIN_NULL_CIPHER_AND_INTEGRITY_VERSION) {
11787 throw new UnsupportedOperationException(
11788 "Null cipher and integrity operations require HAL 2.1 or above");
11789 }
Gil Cukierman92cc7db2023-01-06 19:25:53 +000011790 if (!getDefaultPhone().isNullCipherAndIntegritySupported()) {
11791 throw new UnsupportedOperationException(
11792 "Null cipher and integrity operations unsupported by modem");
11793 }
Gil Cukierman1c0eb932022-12-06 22:28:24 +000011794 }
11795
11796 /**
Jack Yuf5badd92022-12-08 00:50:53 -080011797 * Get the SIM state for the slot index.
11798 * For Remote-SIMs, this method returns {@link IccCardConstants.State#UNKNOWN}
11799 *
11800 * @return SIM state as the ordinal of {@link IccCardConstants.State}
11801 */
11802 @Override
11803 @SimState
11804 public int getSimStateForSlotIndex(int slotIndex) {
11805 IccCardConstants.State simState;
11806 if (slotIndex < 0) {
11807 simState = IccCardConstants.State.UNKNOWN;
11808 } else {
11809 Phone phone = null;
11810 try {
11811 phone = PhoneFactory.getPhone(slotIndex);
11812 } catch (IllegalStateException e) {
11813 // ignore
11814 }
11815 if (phone == null) {
11816 simState = IccCardConstants.State.UNKNOWN;
11817 } else {
11818 IccCard icc = phone.getIccCard();
11819 if (icc == null) {
11820 simState = IccCardConstants.State.UNKNOWN;
11821 } else {
11822 simState = icc.getState();
11823 }
11824 }
11825 }
11826 return simState.ordinal();
11827 }
Hui Wang9b5793a2022-12-05 14:38:06 -060011828
11829 /**
11830 * Get current cell broadcast ranges.
11831 */
11832 @Override
11833 @RequiresPermission(android.Manifest.permission.MODIFY_CELL_BROADCASTS)
11834 public List<CellBroadcastIdRange> getCellBroadcastIdRanges(int subId) {
11835 mApp.enforceCallingPermission(android.Manifest.permission.MODIFY_CELL_BROADCASTS,
11836 "getCellBroadcastIdRanges");
11837 final long identity = Binder.clearCallingIdentity();
11838 try {
11839 return getPhone(subId).getCellBroadcastIdRanges();
11840 } finally {
11841 Binder.restoreCallingIdentity(identity);
11842 }
11843 }
11844
11845 /**
11846 * Set reception of cell broadcast messages with the list of the given ranges
11847 *
11848 * @param ranges the list of {@link CellBroadcastIdRange} to be enabled
11849 */
11850 @Override
11851 @RequiresPermission(android.Manifest.permission.MODIFY_CELL_BROADCASTS)
11852 public void setCellBroadcastIdRanges(int subId, @NonNull List<CellBroadcastIdRange> ranges,
11853 @Nullable IIntegerConsumer callback) {
11854 mApp.enforceCallingPermission(android.Manifest.permission.MODIFY_CELL_BROADCASTS,
11855 "setCellBroadcastIdRanges");
11856 final long identity = Binder.clearCallingIdentity();
11857 try {
11858 Phone phone = getPhoneFromSubId(subId);
11859 if (DBG) {
11860 log("setCellBroadcastIdRanges for subId :" + subId + ", phone:" + phone);
11861 }
11862 phone.setCellBroadcastIdRanges(ranges, result -> {
11863 if (callback != null) {
11864 try {
11865 callback.accept(result);
11866 } catch (RemoteException e) {
11867 Log.w(LOG_TAG, "setCellBroadcastIdRanges: callback not available.");
11868 }
11869 }
11870 });
11871 } finally {
11872 Binder.restoreCallingIdentity(identity);
11873 }
11874 }
Hunsuk Choi42cc62a2022-10-16 06:03:40 +000011875
11876 /**
11877 * Returns whether the device supports the domain selection service.
11878 *
11879 * @return {@code true} if the device supports the domain selection service.
11880 */
11881 @Override
11882 public boolean isDomainSelectionSupported() {
11883 mApp.enforceCallingOrSelfPermission(Manifest.permission.READ_PRIVILEGED_PHONE_STATE,
11884 "isDomainSelectionSupported");
11885
11886 final long identity = Binder.clearCallingIdentity();
11887 try {
11888 return DomainSelectionResolver.getInstance().isDomainSelectionSupported();
11889 } finally {
11890 Binder.restoreCallingIdentity(identity);
11891 }
11892 }
arunvoddud5c6ce02022-12-11 06:03:12 +000011893
11894 /**
11895 * Check whether the caller (or self, if not processing an IPC) can read device identifiers.
11896 *
11897 * <p>This method behaves in one of the following ways:
11898 * <ul>
11899 * <li>return true : if the calling package has the appop permission {@link
11900 * Manifest.permission#USE_ICC_AUTH_WITH_DEVICE_IDENTIFIER} in the manifest </>
11901 * <li>return true : if any one subscription has the READ_PRIVILEGED_PHONE_STATE
11902 * permission, the calling package passes a DevicePolicyManager Device Owner / Profile
11903 * Owner device identifier access check, or the calling package has carrier privileges</>
11904 * <li>throw SecurityException: if the caller does not meet any of the requirements.
11905 * </ul>
11906 */
11907 private static boolean checkCallingOrSelfReadDeviceIdentifiersForAnySub(Context context,
11908 String callingPackage, @Nullable String callingFeatureId, String message) {
11909 for (Phone phone : PhoneFactory.getPhones()) {
11910 if (TelephonyPermissions.checkCallingOrSelfReadDeviceIdentifiers(context,
11911 phone.getSubId(), callingPackage, callingFeatureId, message)) {
11912 return true;
11913 }
11914 }
11915 return false;
11916 }
11917}