blob: de51497e694a8f25f76e73e3df9f57a679990380 [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;
Jack Yue37dd262022-12-16 11:53:37 -0800398 private final 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
Gary Jian76280a42022-12-07 16:18:33 +0800440 private static final int MODEM_ACTIVITY_TIME_OFFSET_CORRECTION_MS = 50;
441
sandeepjsb6c87872021-09-27 15:34:44 +0000442 /**
443 * With support for MEP(multiple enabled profile) in Android T, a SIM card can have more than
444 * one ICCID active at the same time.
445 * Apps should use below API signatures if targeting SDK is T and beyond.
446 *
447 * @hide
448 */
449 @ChangeId
450 @EnabledSince(targetSdkVersion = Build.VERSION_CODES.TIRAMISU)
451 public static final long GET_API_SIGNATURES_FROM_UICC_PORT_INFO = 202110963L;
Rambo Wang0f050d82021-02-12 11:43:36 -0800452
Naina Nallurid63128d2019-09-17 14:10:30 -0700453 /**
Chen Xu540470b2021-12-14 17:15:47 -0800454 * Apps targeting on Android T and beyond will get exception whenever icc close channel
455 * operation fails.
456 */
457 @ChangeId
458 @EnabledSince(targetSdkVersion = Build.VERSION_CODES.TIRAMISU)
459 public static final long ICC_CLOSE_CHANNEL_EXCEPTION_ON_FAILURE = 208739934L;
460
461 /**
Shishir Agrawal566b7612013-10-28 14:41:00 -0700462 * A request object to use for transmitting data to an ICC.
463 */
464 private static final class IccAPDUArgument {
465 public int channel, cla, command, p1, p2, p3;
466 public String data;
467
468 public IccAPDUArgument(int channel, int cla, int command,
469 int p1, int p2, int p3, String data) {
470 this.channel = channel;
471 this.cla = cla;
472 this.command = command;
473 this.p1 = p1;
474 this.p2 = p2;
475 this.p3 = p3;
476 this.data = data;
477 }
478 }
479
480 /**
Shishir Agrawal77ba3172015-09-10 14:50:19 -0700481 * A request object to use for transmitting data to an ICC.
482 */
483 private static final class ManualNetworkSelectionArgument {
484 public OperatorInfo operatorInfo;
485 public boolean persistSelection;
486
487 public ManualNetworkSelectionArgument(OperatorInfo operatorInfo, boolean persistSelection) {
488 this.operatorInfo = operatorInfo;
489 this.persistSelection = persistSelection;
490 }
491 }
492
Sarah Chin71b3a852022-09-28 15:54:19 -0700493 private static final class PurchasePremiumCapabilityArgument {
494 public @TelephonyManager.PremiumCapability int capability;
Sarah Chin71b3a852022-09-28 15:54:19 -0700495 public @NonNull IIntegerConsumer callback;
496
497 PurchasePremiumCapabilityArgument(@TelephonyManager.PremiumCapability int capability,
Sarah Chinb8218c22023-01-04 13:35:29 -0800498 @NonNull IIntegerConsumer callback) {
Sarah Chin71b3a852022-09-28 15:54:19 -0700499 this.capability = capability;
Sarah Chin71b3a852022-09-28 15:54:19 -0700500 this.callback = callback;
501 }
502 }
503
Shishir Agrawal77ba3172015-09-10 14:50:19 -0700504 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700505 * A request object for use with {@link MainThreadHandler}. Requesters should wait() on the
506 * request after sending. The main thread will notify the request when it is complete.
507 */
508 private static final class MainThreadRequest {
509 /** The argument to use for the request */
510 public Object argument;
511 /** The result of the request that is run on the main thread */
512 public Object result;
Sanket Padawe56e75a32016-02-08 12:18:19 -0800513 // The subscriber id that this request applies to. Defaults to
514 // SubscriptionManager.INVALID_SUBSCRIPTION_ID
515 public Integer subId = SubscriptionManager.INVALID_SUBSCRIPTION_ID;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700516
Nathan Harold92bed182018-10-12 18:16:49 -0700517 // In cases where subId is unavailable, the caller needs to specify the phone.
518 public Phone phone;
519
vagdeviaf9a5b92018-08-15 16:01:53 -0700520 public WorkSource workSource;
521
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700522 public MainThreadRequest(Object argument) {
523 this.argument = argument;
524 }
Shishir Agrawal76d5da92014-11-09 16:17:25 -0800525
Nathan Harold92bed182018-10-12 18:16:49 -0700526 MainThreadRequest(Object argument, Phone phone, WorkSource workSource) {
527 this.argument = argument;
528 if (phone != null) {
529 this.phone = phone;
530 }
531 this.workSource = workSource;
532 }
533
vagdeviaf9a5b92018-08-15 16:01:53 -0700534 MainThreadRequest(Object argument, Integer subId, WorkSource workSource) {
Shishir Agrawal76d5da92014-11-09 16:17:25 -0800535 this.argument = argument;
Sanket Padawe56e75a32016-02-08 12:18:19 -0800536 if (subId != null) {
537 this.subId = subId;
538 }
vagdeviaf9a5b92018-08-15 16:01:53 -0700539 this.workSource = workSource;
Shishir Agrawal76d5da92014-11-09 16:17:25 -0800540 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700541 }
542
Sailesh Nepalcc0375f2013-11-13 09:15:18 -0800543 private static final class IncomingThirdPartyCallArgs {
544 public final ComponentName component;
545 public final String callId;
546 public final String callerDisplayName;
547
548 public IncomingThirdPartyCallArgs(ComponentName component, String callId,
549 String callerDisplayName) {
550 this.component = component;
551 this.callId = callId;
552 this.callerDisplayName = callerDisplayName;
553 }
554 }
555
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700556 /**
557 * A handler that processes messages on the main thread in the phone process. Since many
558 * of the Phone calls are not thread safe this is needed to shuttle the requests from the
559 * inbound binder threads to the main thread in the phone process. The Binder thread
560 * may provide a {@link MainThreadRequest} object in the msg.obj field that they are waiting
561 * on, which will be notified when the operation completes and will contain the result of the
562 * request.
563 *
564 * <p>If a MainThreadRequest object is provided in the msg.obj field,
565 * note that request.result must be set to something non-null for the calling thread to
566 * unblock.
567 */
568 private final class MainThreadHandler extends Handler {
569 @Override
570 public void handleMessage(Message msg) {
571 MainThreadRequest request;
572 Message onCompleted;
573 AsyncResult ar;
Muralidhar Reddy472c2ae2021-09-29 19:38:40 +0000574 UiccPort uiccPort;
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700575 IccAPDUArgument iccArgument;
Nazanin Bakhshif782e562018-12-11 15:15:39 -0800576 final Phone defaultPhone = getDefaultPhone();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700577
578 switch (msg.what) {
Pengquan Menga1bb6272018-09-06 09:59:22 -0700579 case CMD_HANDLE_USSD_REQUEST: {
580 request = (MainThreadRequest) msg.obj;
581 final Phone phone = getPhoneFromRequest(request);
582 Pair<String, ResultReceiver> ussdObject = (Pair) request.argument;
Chen Xue9d737e2022-01-01 23:41:31 -0800583 String ussdRequest = ussdObject.first;
Pengquan Menga1bb6272018-09-06 09:59:22 -0700584 ResultReceiver wrappedCallback = ussdObject.second;
Tyler Gunn65d45c22017-06-05 11:22:26 -0700585
Pengquan Menga1bb6272018-09-06 09:59:22 -0700586 if (!isUssdApiAllowed(request.subId)) {
587 // Carrier does not support use of this API, return failure.
588 Rlog.w(LOG_TAG, "handleUssdRequest: carrier does not support USSD apis.");
589 UssdResponse response = new UssdResponse(ussdRequest, null);
590 Bundle returnData = new Bundle();
591 returnData.putParcelable(TelephonyManager.USSD_RESPONSE, response);
592 wrappedCallback.send(TelephonyManager.USSD_RETURN_FAILURE, returnData);
Tyler Gunn65d45c22017-06-05 11:22:26 -0700593
Pengquan Menga1bb6272018-09-06 09:59:22 -0700594 request.result = true;
595 notifyRequester(request);
596 return;
597 }
Tyler Gunn65d45c22017-06-05 11:22:26 -0700598
Pengquan Menga1bb6272018-09-06 09:59:22 -0700599 try {
600 request.result = phone != null
601 ? phone.handleUssdRequest(ussdRequest, wrappedCallback) : false;
602 } catch (CallStateException cse) {
603 request.result = false;
604 }
605 // Wake up the requesting thread
606 notifyRequester(request);
607 break;
pkanwar32d516d2016-10-14 19:37:38 -0700608 }
609
Yorke Lee716f67e2015-06-17 15:39:16 -0700610 case CMD_HANDLE_PIN_MMI: {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700611 request = (MainThreadRequest) msg.obj;
Yorke Lee716f67e2015-06-17 15:39:16 -0700612 final Phone phone = getPhoneFromRequest(request);
613 request.result = phone != null ?
614 getPhoneFromRequest(request).handlePinMmi((String) request.argument)
615 : false;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700616 // Wake up the requesting thread
Pengquan Menga1bb6272018-09-06 09:59:22 -0700617 notifyRequester(request);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700618 break;
Yorke Lee716f67e2015-06-17 15:39:16 -0700619 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700620
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700621 case CMD_TRANSMIT_APDU_LOGICAL_CHANNEL:
Shishir Agrawal566b7612013-10-28 14:41:00 -0700622 request = (MainThreadRequest) msg.obj;
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700623 iccArgument = (IccAPDUArgument) request.argument;
Muralidhar Reddy472c2ae2021-09-29 19:38:40 +0000624 uiccPort = getUiccPortFromRequest(request);
625 if (uiccPort == null) {
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700626 loge("iccTransmitApduLogicalChannel: No UICC");
Chen Xue9d737e2022-01-01 23:41:31 -0800627 request.result = new IccIoResult(0x6F, 0, (byte[]) null);
Pengquan Menga1bb6272018-09-06 09:59:22 -0700628 notifyRequester(request);
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700629 } else {
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700630 onCompleted = obtainMessage(EVENT_TRANSMIT_APDU_LOGICAL_CHANNEL_DONE,
Chen Xue9d737e2022-01-01 23:41:31 -0800631 request);
Muralidhar Reddy472c2ae2021-09-29 19:38:40 +0000632 uiccPort.iccTransmitApduLogicalChannel(
Chen Xue9d737e2022-01-01 23:41:31 -0800633 iccArgument.channel, iccArgument.cla, iccArgument.command,
634 iccArgument.p1, iccArgument.p2, iccArgument.p3, iccArgument.data,
635 onCompleted);
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700636 }
Shishir Agrawal566b7612013-10-28 14:41:00 -0700637 break;
638
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700639 case EVENT_TRANSMIT_APDU_LOGICAL_CHANNEL_DONE:
Shishir Agrawal566b7612013-10-28 14:41:00 -0700640 ar = (AsyncResult) msg.obj;
641 request = (MainThreadRequest) ar.userObj;
642 if (ar.exception == null && ar.result != null) {
643 request.result = ar.result;
644 } else {
Chen Xue9d737e2022-01-01 23:41:31 -0800645 request.result = new IccIoResult(0x6F, 0, (byte[]) null);
Shishir Agrawal566b7612013-10-28 14:41:00 -0700646 if (ar.result == null) {
647 loge("iccTransmitApduLogicalChannel: Empty response");
Jake Hambye994d462014-02-03 13:10:13 -0800648 } else if (ar.exception instanceof CommandException) {
Shishir Agrawal566b7612013-10-28 14:41:00 -0700649 loge("iccTransmitApduLogicalChannel: CommandException: " +
Jake Hambye994d462014-02-03 13:10:13 -0800650 ar.exception);
Shishir Agrawal566b7612013-10-28 14:41:00 -0700651 } else {
652 loge("iccTransmitApduLogicalChannel: Unknown exception");
653 }
654 }
Pengquan Menga1bb6272018-09-06 09:59:22 -0700655 notifyRequester(request);
Shishir Agrawal566b7612013-10-28 14:41:00 -0700656 break;
657
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700658 case CMD_TRANSMIT_APDU_BASIC_CHANNEL:
659 request = (MainThreadRequest) msg.obj;
660 iccArgument = (IccAPDUArgument) request.argument;
Muralidhar Reddy472c2ae2021-09-29 19:38:40 +0000661 uiccPort = getUiccPortFromRequest(request);
662 if (uiccPort == null) {
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700663 loge("iccTransmitApduBasicChannel: No UICC");
Chen Xue9d737e2022-01-01 23:41:31 -0800664 request.result = new IccIoResult(0x6F, 0, (byte[]) null);
Pengquan Menga1bb6272018-09-06 09:59:22 -0700665 notifyRequester(request);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700666 } else {
667 onCompleted = obtainMessage(EVENT_TRANSMIT_APDU_BASIC_CHANNEL_DONE,
Chen Xue9d737e2022-01-01 23:41:31 -0800668 request);
Muralidhar Reddy472c2ae2021-09-29 19:38:40 +0000669 uiccPort.iccTransmitApduBasicChannel(
Chen Xue9d737e2022-01-01 23:41:31 -0800670 iccArgument.cla, iccArgument.command, iccArgument.p1,
671 iccArgument.p2,
672 iccArgument.p3, iccArgument.data, onCompleted);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700673 }
674 break;
675
676 case EVENT_TRANSMIT_APDU_BASIC_CHANNEL_DONE:
677 ar = (AsyncResult) msg.obj;
678 request = (MainThreadRequest) ar.userObj;
679 if (ar.exception == null && ar.result != null) {
680 request.result = ar.result;
681 } else {
Chen Xue9d737e2022-01-01 23:41:31 -0800682 request.result = new IccIoResult(0x6F, 0, (byte[]) null);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700683 if (ar.result == null) {
684 loge("iccTransmitApduBasicChannel: Empty response");
685 } else if (ar.exception instanceof CommandException) {
686 loge("iccTransmitApduBasicChannel: CommandException: " +
687 ar.exception);
688 } else {
689 loge("iccTransmitApduBasicChannel: Unknown exception");
690 }
691 }
Pengquan Menga1bb6272018-09-06 09:59:22 -0700692 notifyRequester(request);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700693 break;
694
695 case CMD_EXCHANGE_SIM_IO:
696 request = (MainThreadRequest) msg.obj;
697 iccArgument = (IccAPDUArgument) request.argument;
Muralidhar Reddy472c2ae2021-09-29 19:38:40 +0000698 uiccPort = getUiccPortFromRequest(request);
699 if (uiccPort == null) {
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700700 loge("iccExchangeSimIO: No UICC");
Chen Xue9d737e2022-01-01 23:41:31 -0800701 request.result = new IccIoResult(0x6F, 0, (byte[]) null);
Pengquan Menga1bb6272018-09-06 09:59:22 -0700702 notifyRequester(request);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700703 } else {
704 onCompleted = obtainMessage(EVENT_EXCHANGE_SIM_IO_DONE,
705 request);
Muralidhar Reddy472c2ae2021-09-29 19:38:40 +0000706 uiccPort.iccExchangeSimIO(iccArgument.cla, /* fileID */
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700707 iccArgument.command, iccArgument.p1, iccArgument.p2, iccArgument.p3,
708 iccArgument.data, onCompleted);
709 }
710 break;
711
712 case EVENT_EXCHANGE_SIM_IO_DONE:
713 ar = (AsyncResult) msg.obj;
714 request = (MainThreadRequest) ar.userObj;
715 if (ar.exception == null && ar.result != null) {
716 request.result = ar.result;
717 } else {
Chen Xue9d737e2022-01-01 23:41:31 -0800718 request.result = new IccIoResult(0x6f, 0, (byte[]) null);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700719 }
Pengquan Menga1bb6272018-09-06 09:59:22 -0700720 notifyRequester(request);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700721 break;
722
Derek Tan4d5e5c12014-02-04 11:54:58 -0800723 case CMD_SEND_ENVELOPE:
724 request = (MainThreadRequest) msg.obj;
Muralidhar Reddy472c2ae2021-09-29 19:38:40 +0000725 uiccPort = getUiccPortFromRequest(request);
726 if (uiccPort == null) {
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700727 loge("sendEnvelopeWithStatus: No UICC");
Chen Xue9d737e2022-01-01 23:41:31 -0800728 request.result = new IccIoResult(0x6F, 0, (byte[]) null);
Pengquan Menga1bb6272018-09-06 09:59:22 -0700729 notifyRequester(request);
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700730 } else {
731 onCompleted = obtainMessage(EVENT_SEND_ENVELOPE_DONE, request);
Chen Xue9d737e2022-01-01 23:41:31 -0800732 uiccPort.sendEnvelopeWithStatus((String) request.argument, onCompleted);
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700733 }
Derek Tan4d5e5c12014-02-04 11:54:58 -0800734 break;
735
736 case EVENT_SEND_ENVELOPE_DONE:
737 ar = (AsyncResult) msg.obj;
738 request = (MainThreadRequest) ar.userObj;
Shishir Agrawal9f9877d2014-03-14 09:36:27 -0700739 if (ar.exception == null && ar.result != null) {
740 request.result = ar.result;
Derek Tan4d5e5c12014-02-04 11:54:58 -0800741 } else {
Chen Xue9d737e2022-01-01 23:41:31 -0800742 request.result = new IccIoResult(0x6F, 0, (byte[]) null);
Shishir Agrawal9f9877d2014-03-14 09:36:27 -0700743 if (ar.result == null) {
744 loge("sendEnvelopeWithStatus: Empty response");
745 } else if (ar.exception instanceof CommandException) {
746 loge("sendEnvelopeWithStatus: CommandException: " +
747 ar.exception);
748 } else {
749 loge("sendEnvelopeWithStatus: exception:" + ar.exception);
750 }
Derek Tan4d5e5c12014-02-04 11:54:58 -0800751 }
Pengquan Menga1bb6272018-09-06 09:59:22 -0700752 notifyRequester(request);
Derek Tan4d5e5c12014-02-04 11:54:58 -0800753 break;
754
Shishir Agrawal566b7612013-10-28 14:41:00 -0700755 case CMD_OPEN_CHANNEL:
756 request = (MainThreadRequest) msg.obj;
Muralidhar Reddy472c2ae2021-09-29 19:38:40 +0000757 uiccPort = getUiccPortFromRequest(request);
Rambo Wanga1782702021-11-10 20:15:19 -0800758 IccLogicalChannelRequest openChannelRequest =
759 (IccLogicalChannelRequest) request.argument;
Muralidhar Reddy472c2ae2021-09-29 19:38:40 +0000760 if (uiccPort == null) {
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700761 loge("iccOpenLogicalChannel: No UICC");
Shishir Agrawalfc0492a2016-02-17 11:15:33 -0800762 request.result = new IccOpenLogicalChannelResponse(-1,
Chen Xue9d737e2022-01-01 23:41:31 -0800763 IccOpenLogicalChannelResponse.STATUS_MISSING_RESOURCE, null);
Pengquan Menga1bb6272018-09-06 09:59:22 -0700764 notifyRequester(request);
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700765 } else {
766 onCompleted = obtainMessage(EVENT_OPEN_CHANNEL_DONE, request);
Rambo Wanga1782702021-11-10 20:15:19 -0800767 uiccPort.iccOpenLogicalChannel(openChannelRequest.aid,
768 openChannelRequest.p2, onCompleted);
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700769 }
Shishir Agrawal566b7612013-10-28 14:41:00 -0700770 break;
771
772 case EVENT_OPEN_CHANNEL_DONE:
773 ar = (AsyncResult) msg.obj;
774 request = (MainThreadRequest) ar.userObj;
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700775 IccOpenLogicalChannelResponse openChannelResp;
Shishir Agrawal566b7612013-10-28 14:41:00 -0700776 if (ar.exception == null && ar.result != null) {
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700777 int[] result = (int[]) ar.result;
778 int channelId = result[0];
779 byte[] selectResponse = null;
780 if (result.length > 1) {
781 selectResponse = new byte[result.length - 1];
782 for (int i = 1; i < result.length; ++i) {
783 selectResponse[i - 1] = (byte) result[i];
784 }
785 }
786 openChannelResp = new IccOpenLogicalChannelResponse(channelId,
Chen Xue9d737e2022-01-01 23:41:31 -0800787 IccOpenLogicalChannelResponse.STATUS_NO_ERROR, selectResponse);
Rambo Wang3b77c4c2021-11-10 20:15:19 -0800788
789 uiccPort = getUiccPortFromRequest(request);
Rambo Wange53e07d2022-05-10 13:01:13 -0700790 if (uiccPort == null) {
791 loge("EVENT_OPEN_CHANNEL_DONE: UiccPort is null");
792 } else {
793 IccLogicalChannelRequest channelRequest =
794 (IccLogicalChannelRequest) request.argument;
795 channelRequest.channel = channelId;
796 uiccPort.onLogicalChannelOpened(channelRequest);
797 }
Shishir Agrawal566b7612013-10-28 14:41:00 -0700798 } else {
Shishir Agrawal566b7612013-10-28 14:41:00 -0700799 if (ar.result == null) {
800 loge("iccOpenLogicalChannel: Empty response");
Shishir Agrawal566b7612013-10-28 14:41:00 -0700801 }
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700802 if (ar.exception != null) {
803 loge("iccOpenLogicalChannel: Exception: " + ar.exception);
804 }
805
Shishir Agrawal527e8bf2014-08-25 08:54:56 -0700806 int errorCode = IccOpenLogicalChannelResponse.STATUS_UNKNOWN_ERROR;
Junda Liua754ba12015-05-20 01:17:52 -0700807 if (ar.exception instanceof CommandException) {
808 CommandException.Error error =
Chen Xue9d737e2022-01-01 23:41:31 -0800809 ((CommandException) (ar.exception)).getCommandError();
Junda Liua754ba12015-05-20 01:17:52 -0700810 if (error == CommandException.Error.MISSING_RESOURCE) {
Shishir Agrawal527e8bf2014-08-25 08:54:56 -0700811 errorCode = IccOpenLogicalChannelResponse.STATUS_MISSING_RESOURCE;
Junda Liua754ba12015-05-20 01:17:52 -0700812 } else if (error == CommandException.Error.NO_SUCH_ELEMENT) {
Shishir Agrawal527e8bf2014-08-25 08:54:56 -0700813 errorCode = IccOpenLogicalChannelResponse.STATUS_NO_SUCH_ELEMENT;
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700814 }
815 }
816 openChannelResp = new IccOpenLogicalChannelResponse(
Chen Xue9d737e2022-01-01 23:41:31 -0800817 IccOpenLogicalChannelResponse.INVALID_CHANNEL, errorCode, null);
Shishir Agrawal566b7612013-10-28 14:41:00 -0700818 }
Shishir Agrawal82c8a462014-07-31 18:13:17 -0700819 request.result = openChannelResp;
Pengquan Menga1bb6272018-09-06 09:59:22 -0700820 notifyRequester(request);
Shishir Agrawal566b7612013-10-28 14:41:00 -0700821 break;
822
823 case CMD_CLOSE_CHANNEL:
824 request = (MainThreadRequest) msg.obj;
Muralidhar Reddy472c2ae2021-09-29 19:38:40 +0000825 uiccPort = getUiccPortFromRequest(request);
826 if (uiccPort == null) {
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700827 loge("iccCloseLogicalChannel: No UICC");
Chen Xua8f0dff2022-02-12 00:34:15 -0800828 request.result = new IllegalArgumentException(
Chen Xue9d737e2022-01-01 23:41:31 -0800829 "iccCloseLogicalChannel: No UICC");
Chen Xue9d737e2022-01-01 23:41:31 -0800830 notifyRequester(request);
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700831 } else {
832 onCompleted = obtainMessage(EVENT_CLOSE_CHANNEL_DONE, request);
Muralidhar Reddy472c2ae2021-09-29 19:38:40 +0000833 uiccPort.iccCloseLogicalChannel((Integer) request.argument, onCompleted);
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700834 }
Shishir Agrawal566b7612013-10-28 14:41:00 -0700835 break;
836
837 case EVENT_CLOSE_CHANNEL_DONE:
Chen Xu540470b2021-12-14 17:15:47 -0800838 ar = (AsyncResult) msg.obj;
839 request = (MainThreadRequest) ar.userObj;
840 if (ar.exception == null) {
841 request.result = true;
Rambo Wang3b77c4c2021-11-10 20:15:19 -0800842 uiccPort = getUiccPortFromRequest(request);
Rambo Wange53e07d2022-05-10 13:01:13 -0700843 if (uiccPort == null) {
844 loge("EVENT_CLOSE_CHANNEL_DONE: UiccPort is null");
845 } else {
846 final int channelId = (Integer) request.argument;
847 uiccPort.onLogicalChannelClosed(channelId);
848 }
Chen Xu540470b2021-12-14 17:15:47 -0800849 } else {
850 request.result = false;
Chen Xue9d737e2022-01-01 23:41:31 -0800851 Exception exception = null;
Chen Xu540470b2021-12-14 17:15:47 -0800852 if (ar.exception instanceof CommandException) {
853 loge("iccCloseLogicalChannel: CommandException: " + ar.exception);
854 CommandException.Error error =
855 ((CommandException) (ar.exception)).getCommandError();
Chen Xue9d737e2022-01-01 23:41:31 -0800856 if (error == CommandException.Error.INVALID_ARGUMENTS) {
857 // should only throw exceptions from the binder threads.
858 exception = new IllegalArgumentException(
Chen Xu540470b2021-12-14 17:15:47 -0800859 "iccCloseLogicalChannel: invalid argument ");
860 }
861 } else {
862 loge("iccCloseLogicalChannel: Unknown exception");
863 }
Chen Xua8f0dff2022-02-12 00:34:15 -0800864 request.result = (exception != null) ? exception :
865 new IllegalStateException(
866 "exception from modem to close iccLogical Channel");
Chen Xu540470b2021-12-14 17:15:47 -0800867 }
868 notifyRequester(request);
Jake Hambye994d462014-02-03 13:10:13 -0800869 break;
870
871 case CMD_NV_READ_ITEM:
872 request = (MainThreadRequest) msg.obj;
873 onCompleted = obtainMessage(EVENT_NV_READ_ITEM_DONE, request);
Nazanin Bakhshif782e562018-12-11 15:15:39 -0800874 defaultPhone.nvReadItem((Integer) request.argument, onCompleted,
875 request.workSource);
Jake Hambye994d462014-02-03 13:10:13 -0800876 break;
877
878 case EVENT_NV_READ_ITEM_DONE:
Shishir Agrawal566b7612013-10-28 14:41:00 -0700879 ar = (AsyncResult) msg.obj;
880 request = (MainThreadRequest) ar.userObj;
Jake Hambye994d462014-02-03 13:10:13 -0800881 if (ar.exception == null && ar.result != null) {
882 request.result = ar.result; // String
Shishir Agrawal566b7612013-10-28 14:41:00 -0700883 } else {
Jake Hambye994d462014-02-03 13:10:13 -0800884 request.result = "";
885 if (ar.result == null) {
886 loge("nvReadItem: Empty response");
887 } else if (ar.exception instanceof CommandException) {
888 loge("nvReadItem: CommandException: " +
889 ar.exception);
Shishir Agrawal566b7612013-10-28 14:41:00 -0700890 } else {
Jake Hambye994d462014-02-03 13:10:13 -0800891 loge("nvReadItem: Unknown exception");
Shishir Agrawal566b7612013-10-28 14:41:00 -0700892 }
893 }
Pengquan Menga1bb6272018-09-06 09:59:22 -0700894 notifyRequester(request);
Shishir Agrawal566b7612013-10-28 14:41:00 -0700895 break;
896
Jake Hambye994d462014-02-03 13:10:13 -0800897 case CMD_NV_WRITE_ITEM:
898 request = (MainThreadRequest) msg.obj;
899 onCompleted = obtainMessage(EVENT_NV_WRITE_ITEM_DONE, request);
900 Pair<Integer, String> idValue = (Pair<Integer, String>) request.argument;
Nazanin Bakhshif782e562018-12-11 15:15:39 -0800901 defaultPhone.nvWriteItem(idValue.first, idValue.second, onCompleted,
vagdeviaf9a5b92018-08-15 16:01:53 -0700902 request.workSource);
Jake Hambye994d462014-02-03 13:10:13 -0800903 break;
904
905 case EVENT_NV_WRITE_ITEM_DONE:
906 handleNullReturnEvent(msg, "nvWriteItem");
907 break;
908
909 case CMD_NV_WRITE_CDMA_PRL:
910 request = (MainThreadRequest) msg.obj;
911 onCompleted = obtainMessage(EVENT_NV_WRITE_CDMA_PRL_DONE, request);
Nazanin Bakhshif782e562018-12-11 15:15:39 -0800912 defaultPhone.nvWriteCdmaPrl((byte[]) request.argument, onCompleted);
Jake Hambye994d462014-02-03 13:10:13 -0800913 break;
914
915 case EVENT_NV_WRITE_CDMA_PRL_DONE:
916 handleNullReturnEvent(msg, "nvWriteCdmaPrl");
917 break;
918
chen xu6dac5ab2018-10-26 17:39:23 -0700919 case CMD_RESET_MODEM_CONFIG:
Jake Hambye994d462014-02-03 13:10:13 -0800920 request = (MainThreadRequest) msg.obj;
chen xu6dac5ab2018-10-26 17:39:23 -0700921 onCompleted = obtainMessage(EVENT_RESET_MODEM_CONFIG_DONE, request);
Nazanin Bakhshif782e562018-12-11 15:15:39 -0800922 defaultPhone.resetModemConfig(onCompleted);
Jake Hambye994d462014-02-03 13:10:13 -0800923 break;
924
chen xu6dac5ab2018-10-26 17:39:23 -0700925 case EVENT_RESET_MODEM_CONFIG_DONE:
926 handleNullReturnEvent(msg, "resetModemConfig");
Jake Hambye994d462014-02-03 13:10:13 -0800927 break;
928
Sooraj Sasindran37444802020-08-11 10:40:43 -0700929 case CMD_IS_NR_DUAL_CONNECTIVITY_ENABLED: {
930 request = (MainThreadRequest) msg.obj;
931 onCompleted = obtainMessage(EVENT_IS_NR_DUAL_CONNECTIVITY_ENABLED_DONE,
932 request);
933 Phone phone = getPhoneFromRequest(request);
934 if (phone != null) {
935 phone.isNrDualConnectivityEnabled(onCompleted, request.workSource);
936 } else {
937 loge("isNRDualConnectivityEnabled: No phone object");
938 request.result = false;
939 notifyRequester(request);
940 }
941 break;
942 }
943
944 case EVENT_IS_NR_DUAL_CONNECTIVITY_ENABLED_DONE:
945 ar = (AsyncResult) msg.obj;
946 request = (MainThreadRequest) ar.userObj;
947 if (ar.exception == null && ar.result != null) {
948 request.result = ar.result;
949 } else {
950 // request.result must be set to something non-null
951 // for the calling thread to unblock
Sooraj Sasindrandaf060f2021-06-15 14:52:55 -0700952 if (ar.result != null) {
Sooraj Sasindran37444802020-08-11 10:40:43 -0700953 request.result = ar.result;
954 } else {
955 request.result = false;
956 }
957 if (ar.result == null) {
958 loge("isNRDualConnectivityEnabled: Empty response");
959 } else if (ar.exception instanceof CommandException) {
960 loge("isNRDualConnectivityEnabled: CommandException: "
961 + ar.exception);
962 } else {
963 loge("isNRDualConnectivityEnabled: Unknown exception");
964 }
965 }
966 notifyRequester(request);
967 break;
968
Sooraj Sasindrandaf060f2021-06-15 14:52:55 -0700969 case CMD_IS_VONR_ENABLED: {
970 request = (MainThreadRequest) msg.obj;
971 onCompleted = obtainMessage(EVENT_IS_VONR_ENABLED_DONE,
972 request);
973 Phone phone = getPhoneFromRequest(request);
974 if (phone != null) {
975 phone.isVoNrEnabled(onCompleted, request.workSource);
976 } else {
977 loge("isVoNrEnabled: No phone object");
978 request.result = false;
979 notifyRequester(request);
980 }
981 break;
982 }
983
984 case EVENT_IS_VONR_ENABLED_DONE:
985 ar = (AsyncResult) msg.obj;
986 request = (MainThreadRequest) ar.userObj;
987 if (ar.exception == null && ar.result != null) {
988 request.result = ar.result;
989 } else {
990 // request.result must be set to something non-null
991 // for the calling thread to unblock
992 if (ar.result != null) {
993 request.result = ar.result;
994 } else {
995 request.result = false;
996 }
997 if (ar.result == null) {
998 loge("isVoNrEnabled: Empty response");
999 } else if (ar.exception instanceof CommandException) {
1000 loge("isVoNrEnabled: CommandException: "
1001 + ar.exception);
1002 } else {
1003 loge("isVoNrEnabled: Unknown exception");
1004 }
1005 }
1006 notifyRequester(request);
1007 break;
1008
Sooraj Sasindran37444802020-08-11 10:40:43 -07001009 case CMD_ENABLE_NR_DUAL_CONNECTIVITY: {
1010 request = (MainThreadRequest) msg.obj;
1011 onCompleted = obtainMessage(EVENT_ENABLE_NR_DUAL_CONNECTIVITY_DONE, request);
1012 Phone phone = getPhoneFromRequest(request);
1013 if (phone != null) {
1014 phone.setNrDualConnectivityState((int) request.argument, onCompleted,
1015 request.workSource);
1016 } else {
1017 loge("enableNrDualConnectivity: No phone object");
1018 request.result =
1019 TelephonyManager.ENABLE_NR_DUAL_CONNECTIVITY_RADIO_NOT_AVAILABLE;
1020 notifyRequester(request);
1021 }
1022 break;
1023 }
1024
1025 case EVENT_ENABLE_NR_DUAL_CONNECTIVITY_DONE: {
1026 ar = (AsyncResult) msg.obj;
1027 request = (MainThreadRequest) ar.userObj;
1028 if (ar.exception == null) {
1029 request.result =
1030 TelephonyManager.ENABLE_NR_DUAL_CONNECTIVITY_SUCCESS;
1031 } else {
1032 request.result =
1033 TelephonyManager
1034 .ENABLE_NR_DUAL_CONNECTIVITY_RADIO_ERROR;
1035 if (ar.exception instanceof CommandException) {
1036 CommandException.Error error =
1037 ((CommandException) (ar.exception)).getCommandError();
1038 if (error == CommandException.Error.RADIO_NOT_AVAILABLE) {
1039 request.result =
1040 TelephonyManager
1041 .ENABLE_NR_DUAL_CONNECTIVITY_RADIO_NOT_AVAILABLE;
Sooraj Sasindran29654162021-03-03 23:00:01 +00001042 } else if (error == CommandException.Error.REQUEST_NOT_SUPPORTED) {
1043 request.result =
1044 TelephonyManager
1045 .ENABLE_NR_DUAL_CONNECTIVITY_NOT_SUPPORTED;
Sooraj Sasindran37444802020-08-11 10:40:43 -07001046 }
1047 loge("enableNrDualConnectivity" + ": CommandException: "
1048 + ar.exception);
1049 } else {
1050 loge("enableNrDualConnectivity" + ": Unknown exception");
1051 }
1052 }
1053 notifyRequester(request);
1054 break;
1055 }
1056
Sooraj Sasindrandaf060f2021-06-15 14:52:55 -07001057 case CMD_ENABLE_VONR: {
1058 request = (MainThreadRequest) msg.obj;
1059 onCompleted = obtainMessage(EVENT_ENABLE_VONR_DONE, request);
1060 Phone phone = getPhoneFromRequest(request);
1061 if (phone != null) {
1062 phone.setVoNrEnabled((boolean) request.argument, onCompleted,
1063 request.workSource);
1064 } else {
1065 loge("setVoNrEnabled: No phone object");
1066 request.result =
1067 TelephonyManager.ENABLE_VONR_RADIO_NOT_AVAILABLE;
1068 notifyRequester(request);
1069 }
1070 break;
1071 }
1072
1073 case EVENT_ENABLE_VONR_DONE: {
1074 ar = (AsyncResult) msg.obj;
1075 request = (MainThreadRequest) ar.userObj;
1076 if (ar.exception == null) {
1077 request.result = TelephonyManager.ENABLE_VONR_SUCCESS;
1078 } else {
1079 request.result = TelephonyManager.ENABLE_VONR_RADIO_ERROR;
1080 if (ar.exception instanceof CommandException) {
1081 CommandException.Error error =
1082 ((CommandException) (ar.exception)).getCommandError();
1083 if (error == CommandException.Error.RADIO_NOT_AVAILABLE) {
1084 request.result = TelephonyManager.ENABLE_VONR_RADIO_NOT_AVAILABLE;
1085 } else if (error == CommandException.Error.REQUEST_NOT_SUPPORTED) {
1086 request.result = TelephonyManager.ENABLE_VONR_REQUEST_NOT_SUPPORTED;
1087 } else {
1088 request.result = TelephonyManager.ENABLE_VONR_RADIO_ERROR;
1089 }
1090 loge("setVoNrEnabled" + ": CommandException: "
1091 + ar.exception);
1092 } else {
1093 loge("setVoNrEnabled" + ": Unknown exception");
1094 }
1095 }
1096 notifyRequester(request);
1097 break;
1098 }
1099
SongFerngWang3ef3e072020-12-21 16:41:52 +08001100 case CMD_GET_ALLOWED_NETWORK_TYPES_BITMASK:
Jake Hamby7c27be32014-03-03 13:25:59 -08001101 request = (MainThreadRequest) msg.obj;
SongFerngWang3ef3e072020-12-21 16:41:52 +08001102 onCompleted = obtainMessage(EVENT_GET_ALLOWED_NETWORK_TYPES_BITMASK_DONE,
1103 request);
1104 getPhoneFromRequest(request).getAllowedNetworkTypesBitmask(onCompleted);
Jake Hamby7c27be32014-03-03 13:25:59 -08001105 break;
1106
SongFerngWang3ef3e072020-12-21 16:41:52 +08001107 case EVENT_GET_ALLOWED_NETWORK_TYPES_BITMASK_DONE:
Jake Hamby7c27be32014-03-03 13:25:59 -08001108 ar = (AsyncResult) msg.obj;
1109 request = (MainThreadRequest) ar.userObj;
1110 if (ar.exception == null && ar.result != null) {
1111 request.result = ar.result; // Integer
1112 } else {
Nazish Tabassume8ba43a2020-07-28 14:49:25 +05301113 // request.result must be set to something non-null
1114 // for the calling thread to unblock
1115 request.result = new int[]{-1};
Jake Hamby7c27be32014-03-03 13:25:59 -08001116 if (ar.result == null) {
SongFerngWang3ef3e072020-12-21 16:41:52 +08001117 loge("getAllowedNetworkTypesBitmask: Empty response");
Jake Hamby7c27be32014-03-03 13:25:59 -08001118 } else if (ar.exception instanceof CommandException) {
SongFerngWang3ef3e072020-12-21 16:41:52 +08001119 loge("getAllowedNetworkTypesBitmask: CommandException: "
1120 + ar.exception);
Jake Hamby7c27be32014-03-03 13:25:59 -08001121 } else {
SongFerngWang3ef3e072020-12-21 16:41:52 +08001122 loge("getAllowedNetworkTypesBitmask: Unknown exception");
Jake Hamby7c27be32014-03-03 13:25:59 -08001123 }
1124 }
Pengquan Menga1bb6272018-09-06 09:59:22 -07001125 notifyRequester(request);
Jake Hamby7c27be32014-03-03 13:25:59 -08001126 break;
1127
SongFerngWang3ef3e072020-12-21 16:41:52 +08001128 case CMD_SET_ALLOWED_NETWORK_TYPES_FOR_REASON:
Jake Hamby7c27be32014-03-03 13:25:59 -08001129 request = (MainThreadRequest) msg.obj;
SongFerngWang3ef3e072020-12-21 16:41:52 +08001130 onCompleted = obtainMessage(EVENT_SET_ALLOWED_NETWORK_TYPES_FOR_REASON_DONE,
1131 request);
1132 Pair<Integer, Long> reasonWithNetworkTypes =
1133 (Pair<Integer, Long>) request.argument;
1134 getPhoneFromRequest(request).setAllowedNetworkTypes(
1135 reasonWithNetworkTypes.first,
1136 reasonWithNetworkTypes.second,
1137 onCompleted);
Jake Hamby7c27be32014-03-03 13:25:59 -08001138 break;
1139
SongFerngWang3ef3e072020-12-21 16:41:52 +08001140 case EVENT_SET_ALLOWED_NETWORK_TYPES_FOR_REASON_DONE:
1141 handleNullReturnEvent(msg, "setAllowedNetworkTypesForReason");
Jake Hamby7c27be32014-03-03 13:25:59 -08001142 break;
1143
Shuo Qian850e4d6a2018-04-25 21:02:08 +00001144 case CMD_INVOKE_OEM_RIL_REQUEST_RAW:
1145 request = (MainThreadRequest)msg.obj;
1146 onCompleted = obtainMessage(EVENT_INVOKE_OEM_RIL_REQUEST_RAW_DONE, request);
Nazanin Bakhshif782e562018-12-11 15:15:39 -08001147 defaultPhone.invokeOemRilRequestRaw((byte[]) request.argument, onCompleted);
Shuo Qian850e4d6a2018-04-25 21:02:08 +00001148 break;
1149
1150 case EVENT_INVOKE_OEM_RIL_REQUEST_RAW_DONE:
1151 ar = (AsyncResult)msg.obj;
1152 request = (MainThreadRequest)ar.userObj;
1153 request.result = ar;
Pengquan Menga1bb6272018-09-06 09:59:22 -07001154 notifyRequester(request);
Shuo Qian850e4d6a2018-04-25 21:02:08 +00001155 break;
1156
Shishir Agrawal76d5da92014-11-09 16:17:25 -08001157 case CMD_SET_VOICEMAIL_NUMBER:
1158 request = (MainThreadRequest) msg.obj;
1159 onCompleted = obtainMessage(EVENT_SET_VOICEMAIL_NUMBER_DONE, request);
1160 Pair<String, String> tagNum = (Pair<String, String>) request.argument;
Stuart Scott584921c2015-01-15 17:10:34 -08001161 getPhoneFromRequest(request).setVoiceMailNumber(tagNum.first, tagNum.second,
1162 onCompleted);
Shishir Agrawal76d5da92014-11-09 16:17:25 -08001163 break;
1164
1165 case EVENT_SET_VOICEMAIL_NUMBER_DONE:
1166 handleNullReturnEvent(msg, "setVoicemailNumber");
1167 break;
1168
Stuart Scott54788802015-03-30 13:18:01 -07001169 case CMD_SET_NETWORK_SELECTION_MODE_AUTOMATIC:
1170 request = (MainThreadRequest) msg.obj;
1171 onCompleted = obtainMessage(EVENT_SET_NETWORK_SELECTION_MODE_AUTOMATIC_DONE,
1172 request);
1173 getPhoneFromRequest(request).setNetworkSelectionModeAutomatic(onCompleted);
1174 break;
1175
1176 case EVENT_SET_NETWORK_SELECTION_MODE_AUTOMATIC_DONE:
1177 handleNullReturnEvent(msg, "setNetworkSelectionModeAutomatic");
1178 break;
1179
Shishir Agrawal302c8692015-06-19 13:49:39 -07001180 case CMD_PERFORM_NETWORK_SCAN:
1181 request = (MainThreadRequest) msg.obj;
1182 onCompleted = obtainMessage(EVENT_PERFORM_NETWORK_SCAN_DONE, request);
1183 getPhoneFromRequest(request).getAvailableNetworks(onCompleted);
1184 break;
1185
Hall Liu27d24262020-09-18 19:04:59 -07001186 case CMD_GET_CALL_FORWARDING: {
Shuo Qian4a594052020-01-23 11:59:30 -08001187 request = (MainThreadRequest) msg.obj;
1188 onCompleted = obtainMessage(EVENT_GET_CALL_FORWARDING_DONE, request);
Hall Liu27d24262020-09-18 19:04:59 -07001189 Pair<Integer, TelephonyManager.CallForwardingInfoCallback> args =
1190 (Pair<Integer, TelephonyManager.CallForwardingInfoCallback>)
1191 request.argument;
1192 int callForwardingReason = args.first;
1193 request.phone.getCallForwardingOption(callForwardingReason, onCompleted);
Shuo Qian4a594052020-01-23 11:59:30 -08001194 break;
Hall Liu27d24262020-09-18 19:04:59 -07001195 }
1196 case EVENT_GET_CALL_FORWARDING_DONE: {
Shuo Qian4a594052020-01-23 11:59:30 -08001197 ar = (AsyncResult) msg.obj;
1198 request = (MainThreadRequest) ar.userObj;
Hall Liu27d24262020-09-18 19:04:59 -07001199 TelephonyManager.CallForwardingInfoCallback callback =
1200 ((Pair<Integer, TelephonyManager.CallForwardingInfoCallback>)
1201 request.argument).second;
Shuo Qian4a594052020-01-23 11:59:30 -08001202 if (ar.exception == null && ar.result != null) {
Hall Liu27d24262020-09-18 19:04:59 -07001203 CallForwardingInfo callForwardingInfo = null;
Shuo Qian4a594052020-01-23 11:59:30 -08001204 CallForwardInfo[] callForwardInfos = (CallForwardInfo[]) ar.result;
1205 for (CallForwardInfo callForwardInfo : callForwardInfos) {
1206 // Service Class is a bit mask per 3gpp 27.007. Search for
1207 // any service for voice call.
1208 if ((callForwardInfo.serviceClass
1209 & CommandsInterface.SERVICE_CLASS_VOICE) > 0) {
Yuchen Dong69cc1412021-09-27 20:27:01 +08001210 callForwardingInfo = new CallForwardingInfo(
1211 callForwardInfo.status
1212 == CommandsInterface.CF_ACTION_ENABLE,
Hall Liu27d24262020-09-18 19:04:59 -07001213 callForwardInfo.reason,
1214 callForwardInfo.number,
1215 callForwardInfo.timeSeconds);
Shuo Qian4a594052020-01-23 11:59:30 -08001216 break;
1217 }
1218 }
1219 // Didn't find a call forward info for voice call.
1220 if (callForwardingInfo == null) {
Hall Liu27d24262020-09-18 19:04:59 -07001221 callForwardingInfo = new CallForwardingInfo(false /* enabled */,
1222 0 /* reason */, null /* number */, 0 /* timeout */);
Shuo Qian4a594052020-01-23 11:59:30 -08001223 }
Hall Liu27d24262020-09-18 19:04:59 -07001224 callback.onCallForwardingInfoAvailable(callForwardingInfo);
Shuo Qian4a594052020-01-23 11:59:30 -08001225 } else {
1226 if (ar.result == null) {
1227 loge("EVENT_GET_CALL_FORWARDING_DONE: Empty response");
1228 }
1229 if (ar.exception != null) {
1230 loge("EVENT_GET_CALL_FORWARDING_DONE: Exception: " + ar.exception);
1231 }
Hall Liu940c4ca2020-09-29 17:10:18 -07001232 int errorCode = TelephonyManager
1233 .CallForwardingInfoCallback.RESULT_ERROR_UNKNOWN;
Shuo Qian4a594052020-01-23 11:59:30 -08001234 if (ar.exception instanceof CommandException) {
1235 CommandException.Error error =
1236 ((CommandException) (ar.exception)).getCommandError();
1237 if (error == CommandException.Error.FDN_CHECK_FAILURE) {
Hall Liu940c4ca2020-09-29 17:10:18 -07001238 errorCode = TelephonyManager
1239 .CallForwardingInfoCallback.RESULT_ERROR_FDN_CHECK_FAILURE;
Shuo Qian4a594052020-01-23 11:59:30 -08001240 } else if (error == CommandException.Error.REQUEST_NOT_SUPPORTED) {
Hall Liu940c4ca2020-09-29 17:10:18 -07001241 errorCode = TelephonyManager
1242 .CallForwardingInfoCallback.RESULT_ERROR_NOT_SUPPORTED;
Shuo Qian4a594052020-01-23 11:59:30 -08001243 }
1244 }
Hall Liu27d24262020-09-18 19:04:59 -07001245 callback.onError(errorCode);
Shuo Qian4a594052020-01-23 11:59:30 -08001246 }
Shuo Qian4a594052020-01-23 11:59:30 -08001247 break;
Hall Liu27d24262020-09-18 19:04:59 -07001248 }
Shuo Qian4a594052020-01-23 11:59:30 -08001249
Hall Liu27d24262020-09-18 19:04:59 -07001250 case CMD_SET_CALL_FORWARDING: {
Shuo Qian4a594052020-01-23 11:59:30 -08001251 request = (MainThreadRequest) msg.obj;
1252 onCompleted = obtainMessage(EVENT_SET_CALL_FORWARDING_DONE, request);
Hall Liu27d24262020-09-18 19:04:59 -07001253 request = (MainThreadRequest) msg.obj;
Shuo Qian4a594052020-01-23 11:59:30 -08001254 CallForwardingInfo callForwardingInfoToSet =
Hall Liu27d24262020-09-18 19:04:59 -07001255 ((Pair<CallForwardingInfo, Consumer<Integer>>)
1256 request.argument).first;
1257 request.phone.setCallForwardingOption(
1258 callForwardingInfoToSet.isEnabled()
Calvin Pan258f1f72021-07-28 21:46:56 +08001259 ? CommandsInterface.CF_ACTION_REGISTRATION
Hall Liu27d24262020-09-18 19:04:59 -07001260 : CommandsInterface.CF_ACTION_DISABLE,
Shuo Qian4a594052020-01-23 11:59:30 -08001261 callForwardingInfoToSet.getReason(),
1262 callForwardingInfoToSet.getNumber(),
1263 callForwardingInfoToSet.getTimeoutSeconds(), onCompleted);
1264 break;
Hall Liu27d24262020-09-18 19:04:59 -07001265 }
Shuo Qian4a594052020-01-23 11:59:30 -08001266
Hall Liu27d24262020-09-18 19:04:59 -07001267 case EVENT_SET_CALL_FORWARDING_DONE: {
Shuo Qian4a594052020-01-23 11:59:30 -08001268 ar = (AsyncResult) msg.obj;
1269 request = (MainThreadRequest) ar.userObj;
Hall Liu27d24262020-09-18 19:04:59 -07001270 Consumer<Integer> callback =
1271 ((Pair<CallForwardingInfo, Consumer<Integer>>)
1272 request.argument).second;
1273 if (ar.exception != null) {
Shuo Qian4a594052020-01-23 11:59:30 -08001274 loge("setCallForwarding exception: " + ar.exception);
Hall Liu940c4ca2020-09-29 17:10:18 -07001275 int errorCode = TelephonyManager.CallForwardingInfoCallback
1276 .RESULT_ERROR_UNKNOWN;
Hall Liu27d24262020-09-18 19:04:59 -07001277 if (ar.exception instanceof CommandException) {
1278 CommandException.Error error =
1279 ((CommandException) (ar.exception)).getCommandError();
1280 if (error == CommandException.Error.FDN_CHECK_FAILURE) {
Hall Liu940c4ca2020-09-29 17:10:18 -07001281 errorCode = TelephonyManager.CallForwardingInfoCallback
1282 .RESULT_ERROR_FDN_CHECK_FAILURE;
Hall Liu27d24262020-09-18 19:04:59 -07001283 } else if (error == CommandException.Error.REQUEST_NOT_SUPPORTED) {
Hall Liu940c4ca2020-09-29 17:10:18 -07001284 errorCode = TelephonyManager.CallForwardingInfoCallback
1285 .RESULT_ERROR_NOT_SUPPORTED;
Hall Liu27d24262020-09-18 19:04:59 -07001286 }
1287 }
1288 callback.accept(errorCode);
1289 } else {
Hall Liu940c4ca2020-09-29 17:10:18 -07001290 callback.accept(TelephonyManager.CallForwardingInfoCallback.RESULT_SUCCESS);
Shuo Qian4a594052020-01-23 11:59:30 -08001291 }
Shuo Qian4a594052020-01-23 11:59:30 -08001292 break;
Hall Liu27d24262020-09-18 19:04:59 -07001293 }
Shuo Qian4a594052020-01-23 11:59:30 -08001294
Hall Liu27d24262020-09-18 19:04:59 -07001295 case CMD_GET_CALL_WAITING: {
Shuo Qian4a594052020-01-23 11:59:30 -08001296 request = (MainThreadRequest) msg.obj;
1297 onCompleted = obtainMessage(EVENT_GET_CALL_WAITING_DONE, request);
1298 getPhoneFromRequest(request).getCallWaiting(onCompleted);
1299 break;
Hall Liu27d24262020-09-18 19:04:59 -07001300 }
Shuo Qian4a594052020-01-23 11:59:30 -08001301
Hall Liu27d24262020-09-18 19:04:59 -07001302 case EVENT_GET_CALL_WAITING_DONE: {
Shuo Qian4a594052020-01-23 11:59:30 -08001303 ar = (AsyncResult) msg.obj;
1304 request = (MainThreadRequest) ar.userObj;
Hall Liu27d24262020-09-18 19:04:59 -07001305 Consumer<Integer> callback = (Consumer<Integer>) request.argument;
SongFerngWangebda2c52022-01-11 15:28:38 +08001306 int callWaitingStatus = TelephonyManager.CALL_WAITING_STATUS_UNKNOWN_ERROR;
Shuo Qian4a594052020-01-23 11:59:30 -08001307 if (ar.exception == null && ar.result != null) {
Shuo Qiand6a0dba2020-02-18 18:13:49 -08001308 int[] callForwardResults = (int[]) ar.result;
Shuo Qian4a594052020-01-23 11:59:30 -08001309 // Service Class is a bit mask per 3gpp 27.007.
1310 // Search for any service for voice call.
Shuo Qiand6a0dba2020-02-18 18:13:49 -08001311 if (callForwardResults.length > 1
1312 && ((callForwardResults[1]
Hall Liu27d24262020-09-18 19:04:59 -07001313 & CommandsInterface.SERVICE_CLASS_VOICE) > 0)) {
SongFerngWangebda2c52022-01-11 15:28:38 +08001314 callWaitingStatus = callForwardResults[0] == 0
Hall Liu27d24262020-09-18 19:04:59 -07001315 ? TelephonyManager.CALL_WAITING_STATUS_DISABLED
1316 : TelephonyManager.CALL_WAITING_STATUS_ENABLED;
Shuo Qian4a594052020-01-23 11:59:30 -08001317 } else {
SongFerngWangebda2c52022-01-11 15:28:38 +08001318 callWaitingStatus = TelephonyManager.CALL_WAITING_STATUS_DISABLED;
Shuo Qian4a594052020-01-23 11:59:30 -08001319 }
1320 } else {
1321 if (ar.result == null) {
1322 loge("EVENT_GET_CALL_WAITING_DONE: Empty response");
1323 }
1324 if (ar.exception != null) {
1325 loge("EVENT_GET_CALL_WAITING_DONE: Exception: " + ar.exception);
1326 }
1327 if (ar.exception instanceof CommandException) {
1328 CommandException.Error error =
1329 ((CommandException) (ar.exception)).getCommandError();
1330 if (error == CommandException.Error.REQUEST_NOT_SUPPORTED) {
SongFerngWangebda2c52022-01-11 15:28:38 +08001331 callWaitingStatus =
Shuo Qian4a594052020-01-23 11:59:30 -08001332 TelephonyManager.CALL_WAITING_STATUS_NOT_SUPPORTED;
SongFerngWangebda2c52022-01-11 15:28:38 +08001333 } else if (error == CommandException.Error.FDN_CHECK_FAILURE) {
1334 callWaitingStatus =
1335 TelephonyManager.CALL_WAITING_STATUS_FDN_CHECK_FAILURE;
Shuo Qian4a594052020-01-23 11:59:30 -08001336 }
1337 }
1338 }
SongFerngWangebda2c52022-01-11 15:28:38 +08001339 callback.accept(callWaitingStatus);
Shuo Qian4a594052020-01-23 11:59:30 -08001340 break;
Hall Liu27d24262020-09-18 19:04:59 -07001341 }
Shuo Qian4a594052020-01-23 11:59:30 -08001342
Hall Liu27d24262020-09-18 19:04:59 -07001343 case CMD_SET_CALL_WAITING: {
Shuo Qian4a594052020-01-23 11:59:30 -08001344 request = (MainThreadRequest) msg.obj;
1345 onCompleted = obtainMessage(EVENT_SET_CALL_WAITING_DONE, request);
Hall Liu27d24262020-09-18 19:04:59 -07001346 boolean enable = ((Pair<Boolean, Consumer<Integer>>) request.argument).first;
1347 getPhoneFromRequest(request).setCallWaiting(enable, onCompleted);
Shuo Qian4a594052020-01-23 11:59:30 -08001348 break;
Hall Liu27d24262020-09-18 19:04:59 -07001349 }
Shuo Qian4a594052020-01-23 11:59:30 -08001350
Hall Liu27d24262020-09-18 19:04:59 -07001351 case EVENT_SET_CALL_WAITING_DONE: {
Shuo Qian4a594052020-01-23 11:59:30 -08001352 ar = (AsyncResult) msg.obj;
1353 request = (MainThreadRequest) ar.userObj;
Hall Liu27d24262020-09-18 19:04:59 -07001354 boolean enable = ((Pair<Boolean, Consumer<Integer>>) request.argument).first;
1355 Consumer<Integer> callback =
1356 ((Pair<Boolean, Consumer<Integer>>) request.argument).second;
1357 if (ar.exception != null) {
Shuo Qian4a594052020-01-23 11:59:30 -08001358 loge("setCallWaiting exception: " + ar.exception);
Hall Liu27d24262020-09-18 19:04:59 -07001359 if (ar.exception instanceof CommandException) {
1360 CommandException.Error error =
1361 ((CommandException) (ar.exception)).getCommandError();
1362 if (error == CommandException.Error.REQUEST_NOT_SUPPORTED) {
1363 callback.accept(TelephonyManager.CALL_WAITING_STATUS_NOT_SUPPORTED);
SongFerngWangebda2c52022-01-11 15:28:38 +08001364 } else if (error == CommandException.Error.FDN_CHECK_FAILURE) {
1365 callback.accept(
1366 TelephonyManager.CALL_WAITING_STATUS_FDN_CHECK_FAILURE);
Hall Liu27d24262020-09-18 19:04:59 -07001367 } else {
1368 callback.accept(TelephonyManager.CALL_WAITING_STATUS_UNKNOWN_ERROR);
1369 }
1370 } else {
1371 callback.accept(TelephonyManager.CALL_WAITING_STATUS_UNKNOWN_ERROR);
1372 }
1373 } else {
1374 callback.accept(enable ? TelephonyManager.CALL_WAITING_STATUS_ENABLED
1375 : TelephonyManager.CALL_WAITING_STATUS_DISABLED);
Shuo Qian4a594052020-01-23 11:59:30 -08001376 }
Shuo Qian4a594052020-01-23 11:59:30 -08001377 break;
Hall Liu27d24262020-09-18 19:04:59 -07001378 }
Shishir Agrawal302c8692015-06-19 13:49:39 -07001379 case EVENT_PERFORM_NETWORK_SCAN_DONE:
1380 ar = (AsyncResult) msg.obj;
1381 request = (MainThreadRequest) ar.userObj;
1382 CellNetworkScanResult cellScanResult;
1383 if (ar.exception == null && ar.result != null) {
1384 cellScanResult = new CellNetworkScanResult(
1385 CellNetworkScanResult.STATUS_SUCCESS,
1386 (List<OperatorInfo>) ar.result);
1387 } else {
1388 if (ar.result == null) {
1389 loge("getCellNetworkScanResults: Empty response");
1390 }
1391 if (ar.exception != null) {
1392 loge("getCellNetworkScanResults: Exception: " + ar.exception);
1393 }
1394 int errorCode = CellNetworkScanResult.STATUS_UNKNOWN_ERROR;
1395 if (ar.exception instanceof CommandException) {
1396 CommandException.Error error =
1397 ((CommandException) (ar.exception)).getCommandError();
1398 if (error == CommandException.Error.RADIO_NOT_AVAILABLE) {
1399 errorCode = CellNetworkScanResult.STATUS_RADIO_NOT_AVAILABLE;
1400 } else if (error == CommandException.Error.GENERIC_FAILURE) {
1401 errorCode = CellNetworkScanResult.STATUS_RADIO_GENERIC_FAILURE;
1402 }
1403 }
1404 cellScanResult = new CellNetworkScanResult(errorCode, null);
1405 }
1406 request.result = cellScanResult;
Pengquan Menga1bb6272018-09-06 09:59:22 -07001407 notifyRequester(request);
Shishir Agrawal302c8692015-06-19 13:49:39 -07001408 break;
1409
1410 case CMD_SET_NETWORK_SELECTION_MODE_MANUAL:
1411 request = (MainThreadRequest) msg.obj;
Shishir Agrawal77ba3172015-09-10 14:50:19 -07001412 ManualNetworkSelectionArgument selArg =
1413 (ManualNetworkSelectionArgument) request.argument;
Shishir Agrawal302c8692015-06-19 13:49:39 -07001414 onCompleted = obtainMessage(EVENT_SET_NETWORK_SELECTION_MODE_MANUAL_DONE,
1415 request);
Shishir Agrawal77ba3172015-09-10 14:50:19 -07001416 getPhoneFromRequest(request).selectNetworkManually(selArg.operatorInfo,
1417 selArg.persistSelection, onCompleted);
Shishir Agrawal302c8692015-06-19 13:49:39 -07001418 break;
1419
1420 case EVENT_SET_NETWORK_SELECTION_MODE_MANUAL_DONE:
Pengquan Menge3d01e22018-09-20 15:25:35 -07001421 ar = (AsyncResult) msg.obj;
1422 request = (MainThreadRequest) ar.userObj;
1423 if (ar.exception == null) {
1424 request.result = true;
1425 } else {
1426 request.result = false;
1427 loge("setNetworkSelectionModeManual " + ar.exception);
1428 }
1429 notifyRequester(request);
1430 mApp.onNetworkSelectionChanged(request.subId);
Shishir Agrawal302c8692015-06-19 13:49:39 -07001431 break;
1432
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -07001433 case CMD_GET_MODEM_ACTIVITY_INFO:
1434 request = (MainThreadRequest) msg.obj;
1435 onCompleted = obtainMessage(EVENT_GET_MODEM_ACTIVITY_INFO_DONE, request);
James Mattisab947702019-04-03 14:18:34 -07001436 if (defaultPhone != null) {
1437 defaultPhone.getModemActivityInfo(onCompleted, request.workSource);
Shuo Qian8f4750a2020-02-20 17:12:10 -08001438 } else {
1439 ResultReceiver result = (ResultReceiver) request.argument;
1440 Bundle bundle = new Bundle();
1441 bundle.putParcelable(TelephonyManager.MODEM_ACTIVITY_RESULT_KEY,
Hall Liu49656c02020-10-09 19:00:11 -07001442 new ModemActivityInfo(0, 0, 0,
1443 new int[ModemActivityInfo.getNumTxPowerLevels()], 0));
Shuo Qian8f4750a2020-02-20 17:12:10 -08001444 result.send(0, bundle);
James Mattisab947702019-04-03 14:18:34 -07001445 }
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -07001446 break;
1447
Hall Liud0f208c2020-10-14 16:54:44 -07001448 case EVENT_GET_MODEM_ACTIVITY_INFO_DONE: {
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -07001449 ar = (AsyncResult) msg.obj;
1450 request = (MainThreadRequest) ar.userObj;
Shuo Qian8f4750a2020-02-20 17:12:10 -08001451 ResultReceiver result = (ResultReceiver) request.argument;
Hall Liud0f208c2020-10-14 16:54:44 -07001452 int error = 0;
Kai Shi917fdc62022-11-28 14:01:02 -08001453 ModemActivityInfo ret = null;
Gary Jian3aa9a762022-01-24 16:41:19 +08001454 if (mLastModemActivityInfo == null) {
1455 mLastModemActivitySpecificInfo = new ActivityStatsTechSpecificInfo[1];
1456 mLastModemActivitySpecificInfo[0] =
1457 new ActivityStatsTechSpecificInfo(
1458 0,
1459 0,
1460 new int[ModemActivityInfo.getNumTxPowerLevels()],
1461 0);
1462 mLastModemActivityInfo =
1463 new ModemActivityInfo(0, 0, 0, mLastModemActivitySpecificInfo);
1464 }
1465
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -07001466 if (ar.exception == null && ar.result != null) {
Shuo Qian8f4750a2020-02-20 17:12:10 -08001467 // Update the last modem activity info and the result of the request.
1468 ModemActivityInfo info = (ModemActivityInfo) ar.result;
1469 if (isModemActivityInfoValid(info)) {
Gary Jian3aa9a762022-01-24 16:41:19 +08001470 mergeModemActivityInfo(info);
Gary Jian76280a42022-12-07 16:18:33 +08001471 } else {
1472 loge("queryModemActivityInfo: invalid response");
Shuo Qian8f4750a2020-02-20 17:12:10 -08001473 }
Kai Shi917fdc62022-11-28 14:01:02 -08001474 // This is needed to decouple ret from mLastModemActivityInfo
1475 // We don't want to return mLastModemActivityInfo which is updated
1476 // inside mergeModemActivityInfo()
1477 ret = new ModemActivityInfo(
1478 mLastModemActivityInfo.getTimestampMillis(),
1479 mLastModemActivityInfo.getSleepTimeMillis(),
1480 mLastModemActivityInfo.getIdleTimeMillis(),
1481 deepCopyModemActivitySpecificInfo(mLastModemActivitySpecificInfo));
Gary Jian3aa9a762022-01-24 16:41:19 +08001482
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -07001483 } else {
1484 if (ar.result == null) {
1485 loge("queryModemActivityInfo: Empty response");
Hall Liud0f208c2020-10-14 16:54:44 -07001486 error = TelephonyManager.ModemActivityInfoException
1487 .ERROR_INVALID_INFO_RECEIVED;
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -07001488 } else if (ar.exception instanceof CommandException) {
Gary Jian3aa9a762022-01-24 16:41:19 +08001489 loge("queryModemActivityInfo: CommandException: " + ar.exception);
Hall Liud0f208c2020-10-14 16:54:44 -07001490 error = TelephonyManager.ModemActivityInfoException
1491 .ERROR_MODEM_RESPONSE_ERROR;
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -07001492 } else {
1493 loge("queryModemActivityInfo: Unknown exception");
Hall Liud0f208c2020-10-14 16:54:44 -07001494 error = TelephonyManager.ModemActivityInfoException
1495 .ERROR_UNKNOWN;
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -07001496 }
1497 }
Shuo Qian8f4750a2020-02-20 17:12:10 -08001498 Bundle bundle = new Bundle();
Kai Shi917fdc62022-11-28 14:01:02 -08001499 if (ret != null) {
Gary Jian3aa9a762022-01-24 16:41:19 +08001500 bundle.putParcelable(
1501 TelephonyManager.MODEM_ACTIVITY_RESULT_KEY,
Kai Shi917fdc62022-11-28 14:01:02 -08001502 ret);
Hall Liud0f208c2020-10-14 16:54:44 -07001503 } else {
1504 bundle.putInt(TelephonyManager.EXCEPTION_RESULT_KEY, error);
1505 }
Shuo Qian8f4750a2020-02-20 17:12:10 -08001506 result.send(0, bundle);
Pengquan Menga1bb6272018-09-06 09:59:22 -07001507 notifyRequester(request);
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -07001508 break;
Hall Liud0f208c2020-10-14 16:54:44 -07001509 }
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -07001510
Meng Wang1a7c35a2016-05-05 20:56:15 -07001511 case CMD_SET_ALLOWED_CARRIERS:
1512 request = (MainThreadRequest) msg.obj;
Michele Berionne482f8202018-11-27 18:57:59 -08001513 CarrierRestrictionRules argument =
1514 (CarrierRestrictionRules) request.argument;
Meng Wang1a7c35a2016-05-05 20:56:15 -07001515 onCompleted = obtainMessage(EVENT_SET_ALLOWED_CARRIERS_DONE, request);
Michele Berionne482f8202018-11-27 18:57:59 -08001516 defaultPhone.setAllowedCarriers(argument, onCompleted, request.workSource);
Meng Wang1a7c35a2016-05-05 20:56:15 -07001517 break;
1518
1519 case EVENT_SET_ALLOWED_CARRIERS_DONE:
1520 ar = (AsyncResult) msg.obj;
1521 request = (MainThreadRequest) ar.userObj;
1522 if (ar.exception == null && ar.result != null) {
1523 request.result = ar.result;
1524 } else {
Michele Berionne482f8202018-11-27 18:57:59 -08001525 request.result = TelephonyManager.SET_CARRIER_RESTRICTION_ERROR;
1526 if (ar.exception instanceof CommandException) {
1527 loge("setAllowedCarriers: CommandException: " + ar.exception);
1528 CommandException.Error error =
1529 ((CommandException) (ar.exception)).getCommandError();
1530 if (error == CommandException.Error.REQUEST_NOT_SUPPORTED) {
1531 request.result =
1532 TelephonyManager.SET_CARRIER_RESTRICTION_NOT_SUPPORTED;
1533 }
Meng Wang1a7c35a2016-05-05 20:56:15 -07001534 } else {
1535 loge("setAllowedCarriers: Unknown exception");
1536 }
1537 }
Pengquan Menga1bb6272018-09-06 09:59:22 -07001538 notifyRequester(request);
Meng Wang1a7c35a2016-05-05 20:56:15 -07001539 break;
1540
1541 case CMD_GET_ALLOWED_CARRIERS:
1542 request = (MainThreadRequest) msg.obj;
1543 onCompleted = obtainMessage(EVENT_GET_ALLOWED_CARRIERS_DONE, request);
Nazanin Bakhshif782e562018-12-11 15:15:39 -08001544 defaultPhone.getAllowedCarriers(onCompleted, request.workSource);
Meng Wang1a7c35a2016-05-05 20:56:15 -07001545 break;
1546
1547 case EVENT_GET_ALLOWED_CARRIERS_DONE:
1548 ar = (AsyncResult) msg.obj;
1549 request = (MainThreadRequest) ar.userObj;
1550 if (ar.exception == null && ar.result != null) {
1551 request.result = ar.result;
1552 } else {
Michele Berionne482f8202018-11-27 18:57:59 -08001553 request.result = new IllegalStateException(
1554 "Failed to get carrier restrictions");
Meng Wang1a7c35a2016-05-05 20:56:15 -07001555 if (ar.result == null) {
1556 loge("getAllowedCarriers: Empty response");
1557 } else if (ar.exception instanceof CommandException) {
1558 loge("getAllowedCarriers: CommandException: " +
1559 ar.exception);
1560 } else {
1561 loge("getAllowedCarriers: Unknown exception");
1562 }
1563 }
Pengquan Menga1bb6272018-09-06 09:59:22 -07001564 notifyRequester(request);
Meng Wang1a7c35a2016-05-05 20:56:15 -07001565 break;
1566
Nathan Haroldb3014052017-01-25 15:57:32 -08001567 case EVENT_GET_FORBIDDEN_PLMNS_DONE:
1568 ar = (AsyncResult) msg.obj;
1569 request = (MainThreadRequest) ar.userObj;
1570 if (ar.exception == null && ar.result != null) {
1571 request.result = ar.result;
1572 } else {
1573 request.result = new IllegalArgumentException(
1574 "Failed to retrieve Forbidden Plmns");
1575 if (ar.result == null) {
1576 loge("getForbiddenPlmns: Empty response");
1577 } else {
1578 loge("getForbiddenPlmns: Unknown exception");
1579 }
1580 }
Pengquan Menga1bb6272018-09-06 09:59:22 -07001581 notifyRequester(request);
Nathan Haroldb3014052017-01-25 15:57:32 -08001582 break;
1583
1584 case CMD_GET_FORBIDDEN_PLMNS:
1585 request = (MainThreadRequest) msg.obj;
Muralidhar Reddy472c2ae2021-09-29 19:38:40 +00001586 uiccPort = getUiccPortFromRequest(request);
1587 if (uiccPort == null) {
1588 loge("getForbiddenPlmns() UiccPort is null");
Nathan Haroldb3014052017-01-25 15:57:32 -08001589 request.result = new IllegalArgumentException(
Muralidhar Reddy472c2ae2021-09-29 19:38:40 +00001590 "getForbiddenPlmns() UiccPort is null");
Pengquan Menga1bb6272018-09-06 09:59:22 -07001591 notifyRequester(request);
Nathan Haroldb3014052017-01-25 15:57:32 -08001592 break;
1593 }
1594 Integer appType = (Integer) request.argument;
Muralidhar Reddy472c2ae2021-09-29 19:38:40 +00001595 UiccCardApplication uiccApp = uiccPort.getApplicationByType(appType);
Nathan Haroldb3014052017-01-25 15:57:32 -08001596 if (uiccApp == null) {
1597 loge("getForbiddenPlmns() no app with specified type -- "
1598 + appType);
1599 request.result = new IllegalArgumentException("Failed to get UICC App");
Pengquan Menga1bb6272018-09-06 09:59:22 -07001600 notifyRequester(request);
Nathan Haroldb3014052017-01-25 15:57:32 -08001601 break;
1602 } else {
1603 if (DBG) logv("getForbiddenPlmns() found app " + uiccApp.getAid()
1604 + " specified type -- " + appType);
1605 }
1606 onCompleted = obtainMessage(EVENT_GET_FORBIDDEN_PLMNS_DONE, request);
1607 ((SIMRecords) uiccApp.getIccRecords()).getForbiddenPlmns(
1608 onCompleted);
1609 break;
1610
Holly Jiuyu Sun01c47ad2018-01-24 17:56:33 +00001611 case CMD_SWITCH_SLOTS:
1612 request = (MainThreadRequest) msg.obj;
Muralidhar Reddyeb809e32021-11-19 03:07:54 +00001613 List<UiccSlotMapping> slotMapping = (List<UiccSlotMapping>) request.argument;
Holly Jiuyu Sun01c47ad2018-01-24 17:56:33 +00001614 onCompleted = obtainMessage(EVENT_SWITCH_SLOTS_DONE, request);
Muralidhar Reddyeb809e32021-11-19 03:07:54 +00001615 UiccController.getInstance().switchSlots(slotMapping, onCompleted);
Holly Jiuyu Sun01c47ad2018-01-24 17:56:33 +00001616 break;
1617
1618 case EVENT_SWITCH_SLOTS_DONE:
1619 ar = (AsyncResult) msg.obj;
1620 request = (MainThreadRequest) ar.userObj;
1621 request.result = (ar.exception == null);
Pengquan Menga1bb6272018-09-06 09:59:22 -07001622 notifyRequester(request);
1623 break;
1624 case CMD_GET_NETWORK_SELECTION_MODE:
1625 request = (MainThreadRequest) msg.obj;
1626 onCompleted = obtainMessage(EVENT_GET_NETWORK_SELECTION_MODE_DONE, request);
1627 getPhoneFromRequest(request).getNetworkSelectionMode(onCompleted);
1628 break;
1629
1630 case EVENT_GET_NETWORK_SELECTION_MODE_DONE:
1631 ar = (AsyncResult) msg.obj;
1632 request = (MainThreadRequest) ar.userObj;
1633 if (ar.exception != null) {
1634 request.result = TelephonyManager.NETWORK_SELECTION_MODE_UNKNOWN;
1635 } else {
1636 int mode = ((int[]) ar.result)[0];
1637 if (mode == 0) {
1638 request.result = TelephonyManager.NETWORK_SELECTION_MODE_AUTO;
1639 } else {
1640 request.result = TelephonyManager.NETWORK_SELECTION_MODE_MANUAL;
1641 }
Holly Jiuyu Sun01c47ad2018-01-24 17:56:33 +00001642 }
Pengquan Menga1bb6272018-09-06 09:59:22 -07001643 notifyRequester(request);
1644 break;
1645 case CMD_GET_CDMA_ROAMING_MODE:
1646 request = (MainThreadRequest) msg.obj;
1647 onCompleted = obtainMessage(EVENT_GET_CDMA_ROAMING_MODE_DONE, request);
1648 getPhoneFromRequest(request).queryCdmaRoamingPreference(onCompleted);
1649 break;
1650 case EVENT_GET_CDMA_ROAMING_MODE_DONE:
1651 ar = (AsyncResult) msg.obj;
1652 request = (MainThreadRequest) ar.userObj;
1653 if (ar.exception != null) {
1654 request.result = TelephonyManager.CDMA_ROAMING_MODE_RADIO_DEFAULT;
1655 } else {
1656 request.result = ((int[]) ar.result)[0];
1657 }
1658 notifyRequester(request);
1659 break;
1660 case CMD_SET_CDMA_ROAMING_MODE:
1661 request = (MainThreadRequest) msg.obj;
1662 onCompleted = obtainMessage(EVENT_SET_CDMA_ROAMING_MODE_DONE, request);
1663 int mode = (int) request.argument;
1664 getPhoneFromRequest(request).setCdmaRoamingPreference(mode, onCompleted);
1665 break;
1666 case EVENT_SET_CDMA_ROAMING_MODE_DONE:
1667 ar = (AsyncResult) msg.obj;
1668 request = (MainThreadRequest) ar.userObj;
1669 request.result = ar.exception == null;
1670 notifyRequester(request);
1671 break;
Sarah Chinbaab1432020-10-28 13:46:24 -07001672 case CMD_GET_CDMA_SUBSCRIPTION_MODE:
1673 request = (MainThreadRequest) msg.obj;
1674 onCompleted = obtainMessage(EVENT_GET_CDMA_SUBSCRIPTION_MODE_DONE, request);
1675 getPhoneFromRequest(request).queryCdmaSubscriptionMode(onCompleted);
1676 break;
1677 case EVENT_GET_CDMA_SUBSCRIPTION_MODE_DONE:
1678 ar = (AsyncResult) msg.obj;
1679 request = (MainThreadRequest) ar.userObj;
1680 if (ar.exception != null) {
1681 request.result = TelephonyManager.CDMA_SUBSCRIPTION_RUIM_SIM;
1682 } else {
1683 request.result = ((int[]) ar.result)[0];
1684 }
1685 notifyRequester(request);
1686 break;
Pengquan Menga1bb6272018-09-06 09:59:22 -07001687 case CMD_SET_CDMA_SUBSCRIPTION_MODE:
1688 request = (MainThreadRequest) msg.obj;
1689 onCompleted = obtainMessage(EVENT_SET_CDMA_SUBSCRIPTION_MODE_DONE, request);
1690 int subscriptionMode = (int) request.argument;
Sarah Chinbaab1432020-10-28 13:46:24 -07001691 getPhoneFromRequest(request).setCdmaSubscriptionMode(
1692 subscriptionMode, onCompleted);
Pengquan Menga1bb6272018-09-06 09:59:22 -07001693 break;
1694 case EVENT_SET_CDMA_SUBSCRIPTION_MODE_DONE:
1695 ar = (AsyncResult) msg.obj;
1696 request = (MainThreadRequest) ar.userObj;
1697 request.result = ar.exception == null;
1698 notifyRequester(request);
Holly Jiuyu Sun01c47ad2018-01-24 17:56:33 +00001699 break;
Nathan Harold3ff88932018-08-14 10:19:49 -07001700 case CMD_GET_ALL_CELL_INFO:
1701 request = (MainThreadRequest) msg.obj;
Nathan Harold3ff88932018-08-14 10:19:49 -07001702 onCompleted = obtainMessage(EVENT_GET_ALL_CELL_INFO_DONE, request);
Nathan Harold92bed182018-10-12 18:16:49 -07001703 request.phone.requestCellInfoUpdate(request.workSource, onCompleted);
Nathan Harold3ff88932018-08-14 10:19:49 -07001704 break;
Nathan Harold3ff88932018-08-14 10:19:49 -07001705 case EVENT_GET_ALL_CELL_INFO_DONE:
1706 ar = (AsyncResult) msg.obj;
1707 request = (MainThreadRequest) ar.userObj;
Nathan Harold8d0f1742018-10-02 12:14:47 -07001708 // If a timeout occurs, the response will be null
1709 request.result = (ar.exception == null && ar.result != null)
1710 ? ar.result : new ArrayList<CellInfo>();
Nathan Harold3ff88932018-08-14 10:19:49 -07001711 synchronized (request) {
1712 request.notifyAll();
1713 }
1714 break;
Nathan Haroldfa8da0f2018-09-27 18:51:29 -07001715 case CMD_REQUEST_CELL_INFO_UPDATE:
1716 request = (MainThreadRequest) msg.obj;
1717 request.phone.requestCellInfoUpdate(request.workSource,
1718 obtainMessage(EVENT_REQUEST_CELL_INFO_UPDATE_DONE, request));
1719 break;
1720 case EVENT_REQUEST_CELL_INFO_UPDATE_DONE:
1721 ar = (AsyncResult) msg.obj;
1722 request = (MainThreadRequest) ar.userObj;
1723 ICellInfoCallback cb = (ICellInfoCallback) request.argument;
1724 try {
1725 if (ar.exception != null) {
Nathan Haroldfa8da0f2018-09-27 18:51:29 -07001726 Log.e(LOG_TAG, "Exception retrieving CellInfo=" + ar.exception);
Meng Wangd8921f42019-09-30 17:13:54 -07001727 cb.onError(
1728 TelephonyManager.CellInfoCallback.ERROR_MODEM_ERROR,
1729 ar.exception.getClass().getName(),
1730 ar.exception.toString());
Nathan Haroldfa8da0f2018-09-27 18:51:29 -07001731 } else if (ar.result == null) {
Nathan Haroldfa8da0f2018-09-27 18:51:29 -07001732 Log.w(LOG_TAG, "Timeout Waiting for CellInfo!");
Meng Wangd8921f42019-09-30 17:13:54 -07001733 cb.onError(TelephonyManager.CellInfoCallback.ERROR_TIMEOUT, null, null);
Nathan Haroldfa8da0f2018-09-27 18:51:29 -07001734 } else {
1735 // use the result as returned
1736 cb.onCellInfo((List<CellInfo>) ar.result);
1737 }
1738 } catch (RemoteException re) {
1739 Log.w(LOG_TAG, "Discarded CellInfo due to Callback RemoteException");
1740 }
1741 break;
Sarah Chin679c08a2020-11-18 13:39:35 -08001742 case CMD_GET_CELL_LOCATION: {
Nathan Harold3ff88932018-08-14 10:19:49 -07001743 request = (MainThreadRequest) msg.obj;
1744 WorkSource ws = (WorkSource) request.argument;
1745 Phone phone = getPhoneFromRequest(request);
Meng Wanga10e89e2019-12-09 13:13:01 -08001746 phone.getCellIdentity(ws, obtainMessage(EVENT_GET_CELL_LOCATION_DONE, request));
Nathan Harold3ff88932018-08-14 10:19:49 -07001747 break;
Sarah Chin679c08a2020-11-18 13:39:35 -08001748 }
1749 case EVENT_GET_CELL_LOCATION_DONE: {
Nathan Harold3ff88932018-08-14 10:19:49 -07001750 ar = (AsyncResult) msg.obj;
1751 request = (MainThreadRequest) ar.userObj;
1752 if (ar.exception == null) {
1753 request.result = ar.result;
1754 } else {
Sarah Chin679c08a2020-11-18 13:39:35 -08001755 Phone phone = getPhoneFromRequest(request);
Nathan Harold3ff88932018-08-14 10:19:49 -07001756 request.result = (phone.getPhoneType() == PhoneConstants.PHONE_TYPE_CDMA)
Meng Wanga10e89e2019-12-09 13:13:01 -08001757 ? new CellIdentityCdma() : new CellIdentityGsm();
Nathan Harold3ff88932018-08-14 10:19:49 -07001758 }
1759
1760 synchronized (request) {
1761 request.notifyAll();
1762 }
1763 break;
Sarah Chin679c08a2020-11-18 13:39:35 -08001764 }
chen xu6dac5ab2018-10-26 17:39:23 -07001765 case CMD_MODEM_REBOOT:
1766 request = (MainThreadRequest) msg.obj;
1767 onCompleted = obtainMessage(EVENT_RESET_MODEM_CONFIG_DONE, request);
Nazanin Bakhshif782e562018-12-11 15:15:39 -08001768 defaultPhone.rebootModem(onCompleted);
chen xu6dac5ab2018-10-26 17:39:23 -07001769 break;
chen xu6dac5ab2018-10-26 17:39:23 -07001770 case EVENT_CMD_MODEM_REBOOT_DONE:
1771 handleNullReturnEvent(msg, "rebootModem");
1772 break;
Malcolm Chen8e4ed912019-01-15 20:22:16 -08001773 case CMD_REQUEST_ENABLE_MODEM:
1774 request = (MainThreadRequest) msg.obj;
1775 boolean enable = (boolean) request.argument;
1776 onCompleted = obtainMessage(EVENT_ENABLE_MODEM_DONE, request);
Nazanin Bakhshi33d584b2019-02-27 10:44:32 -08001777 onCompleted.arg1 = enable ? 1 : 0;
Malcolm Chen8e4ed912019-01-15 20:22:16 -08001778 PhoneConfigurationManager.getInstance()
1779 .enablePhone(request.phone, enable, onCompleted);
1780 break;
Michele Berionne5e411512020-11-13 02:36:59 +00001781 case EVENT_ENABLE_MODEM_DONE: {
Malcolm Chen8e4ed912019-01-15 20:22:16 -08001782 ar = (AsyncResult) msg.obj;
1783 request = (MainThreadRequest) ar.userObj;
1784 request.result = (ar.exception == null);
Nazanin Bakhshif71371d2019-04-29 17:29:44 -07001785 int phoneId = request.phone.getPhoneId();
Nazanin Bakhshi33d584b2019-02-27 10:44:32 -08001786 //update the cache as modem status has changed
Nazanin Bakhshif71371d2019-04-29 17:29:44 -07001787 if ((boolean) request.result) {
1788 mPhoneConfigurationManager.addToPhoneStatusCache(phoneId, msg.arg1 == 1);
1789 updateModemStateMetrics();
1790 } else {
1791 Log.e(LOG_TAG, msg.what + " failure. Not updating modem status."
1792 + ar.exception);
1793 }
1794 notifyRequester(request);
1795 break;
Michele Berionne5e411512020-11-13 02:36:59 +00001796 }
Nazanin Bakhshif71371d2019-04-29 17:29:44 -07001797 case CMD_GET_MODEM_STATUS:
1798 request = (MainThreadRequest) msg.obj;
1799 onCompleted = obtainMessage(EVENT_GET_MODEM_STATUS_DONE, request);
1800 PhoneConfigurationManager.getInstance()
1801 .getPhoneStatusFromModem(request.phone, onCompleted);
1802 break;
1803 case EVENT_GET_MODEM_STATUS_DONE:
1804 ar = (AsyncResult) msg.obj;
1805 request = (MainThreadRequest) ar.userObj;
1806 int id = request.phone.getPhoneId();
1807 if (ar.exception == null && ar.result != null) {
1808 request.result = ar.result;
1809 //update the cache as modem status has changed
1810 mPhoneConfigurationManager.addToPhoneStatusCache(id,
1811 (boolean) request.result);
1812 } else {
1813 // Return true if modem status cannot be retrieved. For most cases,
1814 // modem status is on. And for older version modems, GET_MODEM_STATUS
1815 // and disable modem are not supported. Modem is always on.
1816 // TODO: this should be fixed in R to support a third
1817 // status UNKNOWN b/131631629
1818 request.result = true;
1819 Log.e(LOG_TAG, msg.what + " failure. Not updating modem status."
1820 + ar.exception);
1821 }
Malcolm Chen8e4ed912019-01-15 20:22:16 -08001822 notifyRequester(request);
1823 break;
Hall Liu73f5d362020-01-20 13:42:00 -08001824 case CMD_SET_SYSTEM_SELECTION_CHANNELS: {
1825 request = (MainThreadRequest) msg.obj;
1826 onCompleted = obtainMessage(EVENT_SET_SYSTEM_SELECTION_CHANNELS_DONE, request);
1827 Pair<List<RadioAccessSpecifier>, Consumer<Boolean>> args =
1828 (Pair<List<RadioAccessSpecifier>, Consumer<Boolean>>) request.argument;
1829 request.phone.setSystemSelectionChannels(args.first, onCompleted);
1830 break;
1831 }
1832 case EVENT_SET_SYSTEM_SELECTION_CHANNELS_DONE: {
1833 ar = (AsyncResult) msg.obj;
1834 request = (MainThreadRequest) ar.userObj;
1835 Pair<List<RadioAccessSpecifier>, Consumer<Boolean>> args =
1836 (Pair<List<RadioAccessSpecifier>, Consumer<Boolean>>) request.argument;
1837 args.second.accept(ar.exception == null);
1838 notifyRequester(request);
1839 break;
1840 }
Sarah Chin679c08a2020-11-18 13:39:35 -08001841 case CMD_GET_SYSTEM_SELECTION_CHANNELS: {
1842 request = (MainThreadRequest) msg.obj;
1843 onCompleted = obtainMessage(EVENT_GET_SYSTEM_SELECTION_CHANNELS_DONE, request);
1844 Phone phone = getPhoneFromRequest(request);
1845 if (phone != null) {
1846 phone.getSystemSelectionChannels(onCompleted);
1847 } else {
1848 loge("getSystemSelectionChannels: No phone object");
1849 request.result = new ArrayList<RadioAccessSpecifier>();
1850 notifyRequester(request);
1851 }
1852 break;
1853 }
1854 case EVENT_GET_SYSTEM_SELECTION_CHANNELS_DONE:
1855 ar = (AsyncResult) msg.obj;
1856 request = (MainThreadRequest) ar.userObj;
1857 if (ar.exception == null && ar.result != null) {
1858 request.result = ar.result;
1859 } else {
Sarah Chin428d1d62021-03-13 03:17:40 -08001860 request.result = new IllegalStateException(
1861 "Failed to retrieve system selecton channels");
Sarah Chin679c08a2020-11-18 13:39:35 -08001862 if (ar.result == null) {
1863 loge("getSystemSelectionChannels: Empty response");
1864 } else {
1865 loge("getSystemSelectionChannels: Unknown exception");
1866 }
1867 }
1868 notifyRequester(request);
1869 break;
yincheng zhao2737e882019-09-06 17:06:54 -07001870 case EVENT_SET_FORBIDDEN_PLMNS_DONE:
1871 ar = (AsyncResult) msg.obj;
1872 request = (MainThreadRequest) ar.userObj;
1873 if (ar.exception == null && ar.result != null) {
1874 request.result = ar.result;
1875 } else {
1876 request.result = -1;
1877 loge("Failed to set Forbidden Plmns");
1878 if (ar.result == null) {
1879 loge("setForbidenPlmns: Empty response");
1880 } else if (ar.exception != null) {
1881 loge("setForbiddenPlmns: Exception: " + ar.exception);
1882 request.result = -1;
1883 } else {
1884 loge("setForbiddenPlmns: Unknown exception");
1885 }
1886 }
1887 notifyRequester(request);
1888 break;
1889 case CMD_SET_FORBIDDEN_PLMNS:
1890 request = (MainThreadRequest) msg.obj;
Muralidhar Reddy472c2ae2021-09-29 19:38:40 +00001891 uiccPort = getUiccPortFromRequest(request);
1892 if (uiccPort == null) {
1893 loge("setForbiddenPlmns: UiccPort is null");
yincheng zhao2737e882019-09-06 17:06:54 -07001894 request.result = -1;
1895 notifyRequester(request);
1896 break;
1897 }
1898 Pair<Integer, List<String>> setFplmnsArgs =
1899 (Pair<Integer, List<String>>) request.argument;
1900 appType = setFplmnsArgs.first;
1901 List<String> fplmns = setFplmnsArgs.second;
Muralidhar Reddy472c2ae2021-09-29 19:38:40 +00001902 uiccApp = uiccPort.getApplicationByType(appType);
yincheng zhao2737e882019-09-06 17:06:54 -07001903 if (uiccApp == null) {
1904 loge("setForbiddenPlmns: no app with specified type -- " + appType);
1905 request.result = -1;
1906 loge("Failed to get UICC App");
1907 notifyRequester(request);
1908 } else {
1909 onCompleted = obtainMessage(EVENT_SET_FORBIDDEN_PLMNS_DONE, request);
1910 ((SIMRecords) uiccApp.getIccRecords())
1911 .setForbiddenPlmns(onCompleted, fplmns);
1912 }
yinchengzhao4d163c02019-12-12 15:21:47 -08001913 break;
Naina Nallurid63128d2019-09-17 14:10:30 -07001914 case CMD_ERASE_MODEM_CONFIG:
1915 request = (MainThreadRequest) msg.obj;
1916 onCompleted = obtainMessage(EVENT_ERASE_MODEM_CONFIG_DONE, request);
1917 defaultPhone.eraseModemConfig(onCompleted);
1918 break;
1919 case EVENT_ERASE_MODEM_CONFIG_DONE:
1920 handleNullReturnEvent(msg, "eraseModemConfig");
yincheng zhao2737e882019-09-06 17:06:54 -07001921 break;
zoey chene02881a2019-12-30 16:11:23 +08001922
Kai Shif70f46f2021-03-03 13:59:46 -08001923 case CMD_ERASE_DATA_SHARED_PREFERENCES:
1924 request = (MainThreadRequest) msg.obj;
1925 request.result = defaultPhone.eraseDataInSharedPreferences();
1926 notifyRequester(request);
1927 break;
1928
zoey chene02881a2019-12-30 16:11:23 +08001929 case CMD_CHANGE_ICC_LOCK_PASSWORD:
1930 request = (MainThreadRequest) msg.obj;
1931 onCompleted = obtainMessage(EVENT_CHANGE_ICC_LOCK_PASSWORD_DONE, request);
1932 Pair<String, String> changed = (Pair<String, String>) request.argument;
1933 getPhoneFromRequest(request).getIccCard().changeIccLockPassword(
1934 changed.first, changed.second, onCompleted);
1935 break;
1936 case EVENT_CHANGE_ICC_LOCK_PASSWORD_DONE:
1937 ar = (AsyncResult) msg.obj;
1938 request = (MainThreadRequest) ar.userObj;
1939 if (ar.exception == null) {
1940 request.result = TelephonyManager.CHANGE_ICC_LOCK_SUCCESS;
Michele Berionne5e411512020-11-13 02:36:59 +00001941 // If the operation is successful, update the PIN storage
1942 Pair<String, String> passwords = (Pair<String, String>) request.argument;
1943 int phoneId = getPhoneFromRequest(request).getPhoneId();
Jon Spivack9c3bc762021-10-06 20:53:09 +00001944 UiccController.getInstance().getPinStorage()
1945 .storePin(passwords.second, phoneId);
zoey chene02881a2019-12-30 16:11:23 +08001946 } else {
1947 request.result = msg.arg1;
1948 }
1949 notifyRequester(request);
1950 break;
1951
Michele Berionne5e411512020-11-13 02:36:59 +00001952 case CMD_SET_ICC_LOCK_ENABLED: {
zoey chene02881a2019-12-30 16:11:23 +08001953 request = (MainThreadRequest) msg.obj;
1954 onCompleted = obtainMessage(EVENT_SET_ICC_LOCK_ENABLED_DONE, request);
1955 Pair<Boolean, String> enabled = (Pair<Boolean, String>) request.argument;
1956 getPhoneFromRequest(request).getIccCard().setIccLockEnabled(
1957 enabled.first, enabled.second, onCompleted);
1958 break;
Michele Berionne5e411512020-11-13 02:36:59 +00001959 }
zoey chene02881a2019-12-30 16:11:23 +08001960 case EVENT_SET_ICC_LOCK_ENABLED_DONE:
1961 ar = (AsyncResult) msg.obj;
1962 request = (MainThreadRequest) ar.userObj;
1963 if (ar.exception == null) {
1964 request.result = TelephonyManager.CHANGE_ICC_LOCK_SUCCESS;
Michele Berionne5e411512020-11-13 02:36:59 +00001965 // If the operation is successful, update the PIN storage
1966 Pair<Boolean, String> enabled = (Pair<Boolean, String>) request.argument;
1967 int phoneId = getPhoneFromRequest(request).getPhoneId();
1968 if (enabled.first) {
Jon Spivack9c3bc762021-10-06 20:53:09 +00001969 UiccController.getInstance().getPinStorage()
1970 .storePin(enabled.second, phoneId);
Michele Berionne5e411512020-11-13 02:36:59 +00001971 } else {
1972 UiccController.getInstance().getPinStorage().clearPin(phoneId);
1973 }
zoey chene02881a2019-12-30 16:11:23 +08001974 } else {
1975 request.result = msg.arg1;
1976 }
Michele Berionne5e411512020-11-13 02:36:59 +00001977
1978
zoey chene02881a2019-12-30 16:11:23 +08001979 notifyRequester(request);
1980 break;
1981
Peter Wangdafb9ac2020-01-15 14:13:38 -08001982 case MSG_NOTIFY_USER_ACTIVITY:
1983 removeMessages(MSG_NOTIFY_USER_ACTIVITY);
Peter Wang59571be2020-01-27 12:35:15 +08001984 Intent intent = new Intent(TelephonyIntents.ACTION_USER_ACTIVITY_NOTIFICATION);
Peter Wangdafb9ac2020-01-15 14:13:38 -08001985 intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY);
1986 getDefaultPhone().getContext().sendBroadcastAsUser(
1987 intent, UserHandle.ALL, permission.USER_ACTIVITY);
1988 break;
Jack Nudelmanb0b87642020-11-12 15:04:39 -08001989
1990 case CMD_SET_DATA_THROTTLING: {
1991 request = (MainThreadRequest) msg.obj;
1992 onCompleted = obtainMessage(EVENT_SET_DATA_THROTTLING_DONE, request);
1993 DataThrottlingRequest dataThrottlingRequest =
1994 (DataThrottlingRequest) request.argument;
1995 Phone phone = getPhoneFromRequest(request);
1996 if (phone != null) {
1997 phone.setDataThrottling(onCompleted,
1998 request.workSource, dataThrottlingRequest.getDataThrottlingAction(),
1999 dataThrottlingRequest.getCompletionDurationMillis());
2000 } else {
2001 loge("setDataThrottling: No phone object");
2002 request.result =
2003 TelephonyManager.THERMAL_MITIGATION_RESULT_MODEM_NOT_AVAILABLE;
2004 notifyRequester(request);
2005 }
2006
2007 break;
2008 }
2009 case EVENT_SET_DATA_THROTTLING_DONE:
2010 ar = (AsyncResult) msg.obj;
2011 request = (MainThreadRequest) ar.userObj;
2012
2013 if (ar.exception == null) {
2014 request.result = TelephonyManager.THERMAL_MITIGATION_RESULT_SUCCESS;
2015 } else if (ar.exception instanceof CommandException) {
2016 loge("setDataThrottling: CommandException: " + ar.exception);
2017 CommandException.Error error =
2018 ((CommandException) (ar.exception)).getCommandError();
2019
2020 if (error == CommandException.Error.RADIO_NOT_AVAILABLE) {
2021 request.result = TelephonyManager
2022 .THERMAL_MITIGATION_RESULT_MODEM_NOT_AVAILABLE;
2023 } else if (error == CommandException.Error.INVALID_ARGUMENTS) {
2024 request.result = SET_DATA_THROTTLING_MODEM_THREW_INVALID_PARAMS;
Jack Nudelman5d6a98b2021-03-04 14:26:25 -08002025 } else if (error == CommandException.Error.REQUEST_NOT_SUPPORTED) {
2026 request.result = MODEM_DOES_NOT_SUPPORT_DATA_THROTTLING_ERROR_CODE;
Jack Nudelmanb0b87642020-11-12 15:04:39 -08002027 } else {
2028 request.result =
2029 TelephonyManager.THERMAL_MITIGATION_RESULT_MODEM_ERROR;
2030 }
2031 } else {
2032 request.result = TelephonyManager.THERMAL_MITIGATION_RESULT_MODEM_ERROR;
2033 }
2034 Log.w(LOG_TAG, "DataThrottlingResult = " + request.result);
2035 notifyRequester(request);
2036 break;
Jordan Liu109698e2020-11-24 14:50:34 -08002037
2038 case CMD_SET_SIM_POWER: {
2039 request = (MainThreadRequest) msg.obj;
2040 onCompleted = obtainMessage(EVENT_SET_SIM_POWER_DONE, request);
2041 request = (MainThreadRequest) msg.obj;
2042 int stateToSet =
2043 ((Pair<Integer, IIntegerConsumer>)
2044 request.argument).first;
2045 request.phone.setSimPowerState(stateToSet, onCompleted, request.workSource);
2046 break;
2047 }
2048 case EVENT_SET_SIM_POWER_DONE: {
2049 ar = (AsyncResult) msg.obj;
2050 request = (MainThreadRequest) ar.userObj;
2051 IIntegerConsumer callback =
2052 ((Pair<Integer, IIntegerConsumer>) request.argument).second;
2053 if (ar.exception != null) {
2054 loge("setSimPower exception: " + ar.exception);
2055 int errorCode = TelephonyManager.CallForwardingInfoCallback
2056 .RESULT_ERROR_UNKNOWN;
2057 if (ar.exception instanceof CommandException) {
2058 CommandException.Error error =
2059 ((CommandException) (ar.exception)).getCommandError();
2060 if (error == CommandException.Error.SIM_ERR) {
2061 errorCode = TelephonyManager.SET_SIM_POWER_STATE_SIM_ERROR;
2062 } else if (error == CommandException.Error.INVALID_ARGUMENTS) {
2063 errorCode = TelephonyManager.SET_SIM_POWER_STATE_ALREADY_IN_STATE;
2064 } else if (error == CommandException.Error.REQUEST_NOT_SUPPORTED) {
2065 errorCode = TelephonyManager.SET_SIM_POWER_STATE_NOT_SUPPORTED;
2066 } else {
2067 errorCode = TelephonyManager.SET_SIM_POWER_STATE_MODEM_ERROR;
2068 }
2069 }
2070 try {
2071 callback.accept(errorCode);
2072 } catch (RemoteException e) {
2073 // Ignore if the remote process is no longer available to call back.
2074 Log.w(LOG_TAG, "setSimPower: callback not available.");
2075 }
2076 } else {
2077 try {
2078 callback.accept(TelephonyManager.SET_SIM_POWER_STATE_SUCCESS);
2079 } catch (RemoteException e) {
2080 // Ignore if the remote process is no longer available to call back.
2081 Log.w(LOG_TAG, "setSimPower: callback not available.");
2082 }
2083 }
2084 break;
2085 }
Rambo Wanga5cc9b72021-01-07 10:51:54 -08002086 case CMD_SET_SIGNAL_STRENGTH_UPDATE_REQUEST: {
2087 request = (MainThreadRequest) msg.obj;
2088
2089 final Phone phone = getPhoneFromRequest(request);
2090 if (phone == null || phone.getServiceStateTracker() == null) {
2091 request.result = new IllegalStateException("Phone or SST is null");
2092 notifyRequester(request);
2093 break;
2094 }
2095
2096 Pair<Integer, SignalStrengthUpdateRequest> pair =
2097 (Pair<Integer, SignalStrengthUpdateRequest>) request.argument;
2098 onCompleted = obtainMessage(EVENT_SET_SIGNAL_STRENGTH_UPDATE_REQUEST_DONE,
2099 request);
Rambo Wang6568f172021-02-03 16:56:47 -08002100 phone.getSignalStrengthController().setSignalStrengthUpdateRequest(
Rambo Wanga5cc9b72021-01-07 10:51:54 -08002101 request.subId, pair.first /*callingUid*/,
2102 pair.second /*request*/, onCompleted);
2103 break;
2104 }
2105 case EVENT_SET_SIGNAL_STRENGTH_UPDATE_REQUEST_DONE: {
2106 ar = (AsyncResult) msg.obj;
2107 request = (MainThreadRequest) ar.userObj;
2108 // request.result will be the exception of ar if present, true otherwise.
2109 // Be cautious not to leave result null which will wait() forever
2110 request.result = ar.exception != null ? ar.exception : true;
2111 notifyRequester(request);
2112 break;
2113 }
2114 case CMD_CLEAR_SIGNAL_STRENGTH_UPDATE_REQUEST: {
2115 request = (MainThreadRequest) msg.obj;
2116
2117 Phone phone = getPhoneFromRequest(request);
2118 if (phone == null || phone.getServiceStateTracker() == null) {
2119 request.result = new IllegalStateException("Phone or SST is null");
2120 notifyRequester(request);
2121 break;
2122 }
2123
2124 Pair<Integer, SignalStrengthUpdateRequest> pair =
2125 (Pair<Integer, SignalStrengthUpdateRequest>) request.argument;
2126 onCompleted = obtainMessage(EVENT_CLEAR_SIGNAL_STRENGTH_UPDATE_REQUEST_DONE,
2127 request);
Rambo Wang6568f172021-02-03 16:56:47 -08002128 phone.getSignalStrengthController().clearSignalStrengthUpdateRequest(
Rambo Wanga5cc9b72021-01-07 10:51:54 -08002129 request.subId, pair.first /*callingUid*/,
2130 pair.second /*request*/, onCompleted);
2131 break;
2132 }
2133 case EVENT_CLEAR_SIGNAL_STRENGTH_UPDATE_REQUEST_DONE: {
2134 ar = (AsyncResult) msg.obj;
2135 request = (MainThreadRequest) ar.userObj;
2136 request.result = ar.exception != null ? ar.exception : true;
2137 notifyRequester(request);
2138 break;
2139 }
Jordan Liu109698e2020-11-24 14:50:34 -08002140
Hongbo Zeng156aa4a2021-02-08 21:50:28 +08002141 case CMD_GET_SLICING_CONFIG: {
2142 request = (MainThreadRequest) msg.obj;
2143 onCompleted = obtainMessage(EVENT_GET_SLICING_CONFIG_DONE, request);
2144 request.phone.getSlicingConfig(onCompleted);
2145 break;
2146 }
2147 case EVENT_GET_SLICING_CONFIG_DONE: {
2148 ar = (AsyncResult) msg.obj;
2149 request = (MainThreadRequest) ar.userObj;
2150 ResultReceiver result = (ResultReceiver) request.argument;
2151
Hongbo Zeng0e18b162021-04-07 16:52:18 +08002152 NetworkSlicingConfig slicingConfig = null;
Hongbo Zeng156aa4a2021-02-08 21:50:28 +08002153 Bundle bundle = new Bundle();
2154 int resultCode = 0;
2155 if (ar.exception != null) {
2156 Log.e(LOG_TAG, "Exception retrieving slicing configuration="
2157 + ar.exception);
Hongbo Zeng0e18b162021-04-07 16:52:18 +08002158 resultCode = TelephonyManager.NetworkSlicingException.ERROR_MODEM_ERROR;
Hongbo Zeng156aa4a2021-02-08 21:50:28 +08002159 } else if (ar.result == null) {
2160 Log.w(LOG_TAG, "Timeout Waiting for slicing configuration!");
Hongbo Zeng0e18b162021-04-07 16:52:18 +08002161 resultCode = TelephonyManager.NetworkSlicingException.ERROR_TIMEOUT;
Hongbo Zeng156aa4a2021-02-08 21:50:28 +08002162 } else {
2163 // use the result as returned
Hongbo Zeng0e18b162021-04-07 16:52:18 +08002164 resultCode = TelephonyManager.NetworkSlicingException.SUCCESS;
2165 slicingConfig = (NetworkSlicingConfig) ar.result;
Hongbo Zeng156aa4a2021-02-08 21:50:28 +08002166 }
2167
2168 if (slicingConfig == null) {
Hongbo Zeng0e18b162021-04-07 16:52:18 +08002169 slicingConfig = new NetworkSlicingConfig();
Hongbo Zeng156aa4a2021-02-08 21:50:28 +08002170 }
2171 bundle.putParcelable(TelephonyManager.KEY_SLICING_CONFIG_HANDLE, slicingConfig);
2172 result.send(resultCode, bundle);
2173 notifyRequester(request);
2174 break;
2175 }
2176
Sarah Chin71b3a852022-09-28 15:54:19 -07002177 case CMD_PURCHASE_PREMIUM_CAPABILITY: {
Sarah Chin2ec39f62022-08-31 17:03:26 -07002178 request = (MainThreadRequest) msg.obj;
2179 onCompleted = obtainMessage(EVENT_PURCHASE_PREMIUM_CAPABILITY_DONE, request);
Sarah Chin71b3a852022-09-28 15:54:19 -07002180 PurchasePremiumCapabilityArgument arg =
2181 (PurchasePremiumCapabilityArgument) request.argument;
Sarah Chin46355ba2022-11-01 23:51:16 -07002182 SlicePurchaseController.getInstance(request.phone).purchasePremiumCapability(
Sarah Chinb8218c22023-01-04 13:35:29 -08002183 arg.capability, onCompleted);
Sarah Chin2ec39f62022-08-31 17:03:26 -07002184 break;
Sarah Chin71b3a852022-09-28 15:54:19 -07002185 }
Sarah Chin2ec39f62022-08-31 17:03:26 -07002186
Sarah Chin71b3a852022-09-28 15:54:19 -07002187 case EVENT_PURCHASE_PREMIUM_CAPABILITY_DONE: {
Sarah Chin2ec39f62022-08-31 17:03:26 -07002188 ar = (AsyncResult) msg.obj;
2189 request = (MainThreadRequest) ar.userObj;
Sarah Chin71b3a852022-09-28 15:54:19 -07002190 PurchasePremiumCapabilityArgument arg =
2191 (PurchasePremiumCapabilityArgument) request.argument;
Sarah Chin2ec39f62022-08-31 17:03:26 -07002192 try {
2193 int result = (int) ar.result;
Sarah Chin71b3a852022-09-28 15:54:19 -07002194 arg.callback.accept(result);
Sarah Chin2ec39f62022-08-31 17:03:26 -07002195 log("purchasePremiumCapability: capability="
Sarah Chin71b3a852022-09-28 15:54:19 -07002196 + TelephonyManager.convertPremiumCapabilityToString(arg.capability)
Sarah Chin2ec39f62022-08-31 17:03:26 -07002197 + ", result= "
2198 + TelephonyManager.convertPurchaseResultToString(result));
2199 } catch (RemoteException e) {
2200 String logStr = "Purchase premium capability "
Sarah Chin71b3a852022-09-28 15:54:19 -07002201 + TelephonyManager.convertPremiumCapabilityToString(arg.capability)
Sarah Chin2ec39f62022-08-31 17:03:26 -07002202 + " failed: " + e;
2203 if (DBG) log(logStr);
2204 AnomalyReporter.reportAnomaly(
2205 UUID.fromString(PURCHASE_PREMIUM_CAPABILITY_ERROR_UUID), logStr);
2206 }
2207 break;
Sarah Chin71b3a852022-09-28 15:54:19 -07002208 }
Sarah Chin2ec39f62022-08-31 17:03:26 -07002209
Michele Berionne5e411512020-11-13 02:36:59 +00002210 case CMD_PREPARE_UNATTENDED_REBOOT:
2211 request = (MainThreadRequest) msg.obj;
2212 request.result =
Rafael Higuera Silvad9630642021-09-20 15:32:01 +00002213 UiccController.getInstance().getPinStorage()
2214 .prepareUnattendedReboot(request.workSource);
Michele Berionne5e411512020-11-13 02:36:59 +00002215 notifyRequester(request);
2216 break;
2217
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002218 default:
2219 Log.w(LOG_TAG, "MainThreadHandler: unexpected message code: " + msg.what);
2220 break;
2221 }
2222 }
Jake Hambye994d462014-02-03 13:10:13 -08002223
Pengquan Menga1bb6272018-09-06 09:59:22 -07002224 private void notifyRequester(MainThreadRequest request) {
2225 synchronized (request) {
2226 request.notifyAll();
2227 }
2228 }
2229
Jake Hambye994d462014-02-03 13:10:13 -08002230 private void handleNullReturnEvent(Message msg, String command) {
2231 AsyncResult ar = (AsyncResult) msg.obj;
2232 MainThreadRequest request = (MainThreadRequest) ar.userObj;
2233 if (ar.exception == null) {
2234 request.result = true;
2235 } else {
2236 request.result = false;
2237 if (ar.exception instanceof CommandException) {
2238 loge(command + ": CommandException: " + ar.exception);
2239 } else {
2240 loge(command + ": Unknown exception");
2241 }
2242 }
Pengquan Menga1bb6272018-09-06 09:59:22 -07002243 notifyRequester(request);
Jake Hambye994d462014-02-03 13:10:13 -08002244 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002245 }
2246
2247 /**
2248 * Posts the specified command to be executed on the main thread,
2249 * waits for the request to complete, and returns the result.
2250 * @see #sendRequestAsync
2251 */
2252 private Object sendRequest(int command, Object argument) {
Rambo Wang0f050d82021-02-12 11:43:36 -08002253 return sendRequest(command, argument, SubscriptionManager.INVALID_SUBSCRIPTION_ID, null,
2254 null, -1 /*timeoutInMs*/);
vagdeviaf9a5b92018-08-15 16:01:53 -07002255 }
2256
2257 /**
2258 * Posts the specified command to be executed on the main thread,
2259 * waits for the request to complete, and returns the result.
2260 * @see #sendRequestAsync
2261 */
2262 private Object sendRequest(int command, Object argument, WorkSource workSource) {
2263 return sendRequest(command, argument, SubscriptionManager.INVALID_SUBSCRIPTION_ID,
Rambo Wang0f050d82021-02-12 11:43:36 -08002264 null, workSource, -1 /*timeoutInMs*/);
Wink Saville36469e72014-06-11 15:17:00 -07002265 }
2266
2267 /**
2268 * Posts the specified command to be executed on the main thread,
2269 * waits for the request to complete, and returns the result.
2270 * @see #sendRequestAsync
2271 */
Shishir Agrawal76d5da92014-11-09 16:17:25 -08002272 private Object sendRequest(int command, Object argument, Integer subId) {
Rambo Wang0f050d82021-02-12 11:43:36 -08002273 return sendRequest(command, argument, subId, null, null, -1 /*timeoutInMs*/);
2274 }
2275
2276 /**
2277 * Posts the specified command to be executed on the main thread,
2278 * waits for the request to complete for at most {@code timeoutInMs}, and returns the result
2279 * if not timeout or null otherwise.
2280 * @see #sendRequestAsync
2281 */
2282 private @Nullable Object sendRequest(int command, Object argument, Integer subId,
2283 long timeoutInMs) {
2284 return sendRequest(command, argument, subId, null, null, timeoutInMs);
vagdeviaf9a5b92018-08-15 16:01:53 -07002285 }
2286
2287 /**
2288 * Posts the specified command to be executed on the main thread,
2289 * waits for the request to complete, and returns the result.
2290 * @see #sendRequestAsync
2291 */
Nathan Harold92bed182018-10-12 18:16:49 -07002292 private Object sendRequest(int command, Object argument, int subId, WorkSource workSource) {
Rambo Wang0f050d82021-02-12 11:43:36 -08002293 return sendRequest(command, argument, subId, null, workSource, -1 /*timeoutInMs*/);
Nathan Harold92bed182018-10-12 18:16:49 -07002294 }
2295
2296 /**
2297 * Posts the specified command to be executed on the main thread,
2298 * waits for the request to complete, and returns the result.
2299 * @see #sendRequestAsync
2300 */
2301 private Object sendRequest(int command, Object argument, Phone phone, WorkSource workSource) {
Rambo Wang0f050d82021-02-12 11:43:36 -08002302 return sendRequest(command, argument, SubscriptionManager.INVALID_SUBSCRIPTION_ID, phone,
2303 workSource, -1 /*timeoutInMs*/);
Nathan Harold92bed182018-10-12 18:16:49 -07002304 }
2305
2306 /**
Rambo Wang0f050d82021-02-12 11:43:36 -08002307 * Posts the specified command to be executed on the main thread. If {@code timeoutInMs} is
2308 * negative, waits for the request to complete, and returns the result. Otherwise, wait for
2309 * maximum of {@code timeoutInMs} milliseconds, interrupt and return null.
Nathan Harold92bed182018-10-12 18:16:49 -07002310 * @see #sendRequestAsync
2311 */
Rambo Wang0f050d82021-02-12 11:43:36 -08002312 private @Nullable Object sendRequest(int command, Object argument, Integer subId, Phone phone,
2313 WorkSource workSource, long timeoutInMs) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002314 if (Looper.myLooper() == mMainThreadHandler.getLooper()) {
2315 throw new RuntimeException("This method will deadlock if called from the main thread.");
2316 }
2317
Nathan Harold92bed182018-10-12 18:16:49 -07002318 MainThreadRequest request = null;
2319 if (subId != SubscriptionManager.INVALID_SUBSCRIPTION_ID && phone != null) {
2320 throw new IllegalArgumentException("subId and phone cannot both be specified!");
2321 } else if (phone != null) {
2322 request = new MainThreadRequest(argument, phone, workSource);
2323 } else {
2324 request = new MainThreadRequest(argument, subId, workSource);
2325 }
2326
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002327 Message msg = mMainThreadHandler.obtainMessage(command, request);
2328 msg.sendToTarget();
2329
Rambo Wang0f050d82021-02-12 11:43:36 -08002330
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002331 synchronized (request) {
Rambo Wang0f050d82021-02-12 11:43:36 -08002332 if (timeoutInMs >= 0) {
2333 // Wait for at least timeoutInMs before returning null request result
2334 long now = SystemClock.elapsedRealtime();
2335 long deadline = now + timeoutInMs;
Grace Jia8a0a1e82021-05-23 22:59:52 -07002336 while (request.result == null && now < deadline) {
Rambo Wang0f050d82021-02-12 11:43:36 -08002337 try {
2338 request.wait(deadline - now);
2339 } catch (InterruptedException e) {
2340 // Do nothing, go back and check if request is completed or timeout
2341 } finally {
2342 now = SystemClock.elapsedRealtime();
2343 }
2344 }
2345 } else {
2346 // Wait for the request to complete
2347 while (request.result == null) {
2348 try {
2349 request.wait();
2350 } catch (InterruptedException e) {
2351 // Do nothing, go back and wait until the request is complete
2352 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002353 }
2354 }
2355 }
Rambo Wang0f050d82021-02-12 11:43:36 -08002356 if (request.result == null) {
2357 Log.wtf(LOG_TAG,
2358 "sendRequest: Blocking command timed out. Something has gone terribly wrong.");
2359 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002360 return request.result;
2361 }
2362
2363 /**
2364 * Asynchronous ("fire and forget") version of sendRequest():
2365 * Posts the specified command to be executed on the main thread, and
2366 * returns immediately.
2367 * @see #sendRequest
2368 */
2369 private void sendRequestAsync(int command) {
2370 mMainThreadHandler.sendEmptyMessage(command);
2371 }
2372
2373 /**
Sailesh Nepalbd76e4e2013-10-27 13:59:44 -07002374 * Same as {@link #sendRequestAsync(int)} except it takes an argument.
Nathan Haroldfa8da0f2018-09-27 18:51:29 -07002375 * @see {@link #sendRequest(int)}
Sailesh Nepalbd76e4e2013-10-27 13:59:44 -07002376 */
2377 private void sendRequestAsync(int command, Object argument) {
Nathan Haroldfa8da0f2018-09-27 18:51:29 -07002378 sendRequestAsync(command, argument, null, null);
2379 }
2380
2381 /**
2382 * Same as {@link #sendRequestAsync(int,Object)} except it takes a Phone and WorkSource.
2383 * @see {@link #sendRequest(int,Object)}
2384 */
2385 private void sendRequestAsync(
2386 int command, Object argument, Phone phone, WorkSource workSource) {
2387 MainThreadRequest request = new MainThreadRequest(argument, phone, workSource);
Sailesh Nepalbd76e4e2013-10-27 13:59:44 -07002388 Message msg = mMainThreadHandler.obtainMessage(command, request);
2389 msg.sendToTarget();
2390 }
2391
2392 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002393 * Initialize the singleton PhoneInterfaceManager instance.
2394 * This is only done once, at startup, from PhoneApp.onCreate().
2395 */
Nazanin Bakhshif782e562018-12-11 15:15:39 -08002396 /* package */ static PhoneInterfaceManager init(PhoneGlobals app) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002397 synchronized (PhoneInterfaceManager.class) {
2398 if (sInstance == null) {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08002399 sInstance = new PhoneInterfaceManager(app);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002400 } else {
2401 Log.wtf(LOG_TAG, "init() called multiple times! sInstance = " + sInstance);
2402 }
2403 return sInstance;
2404 }
2405 }
2406
2407 /** Private constructor; @see init() */
Jordan Liu1979a042020-03-20 21:39:35 +00002408 private PhoneInterfaceManager(PhoneGlobals app) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002409 mApp = app;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002410 mCM = PhoneGlobals.getInstance().mCM;
Brad Ebingerd1947d82021-05-17 20:54:49 +00002411 mImsResolver = ImsResolver.getInstance();
Stuart Scott981d8582015-04-21 14:09:50 -07002412 mUserManager = (UserManager) app.getSystemService(Context.USER_SERVICE);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002413 mAppOps = (AppOpsManager)app.getSystemService(Context.APP_OPS_SERVICE);
Grace Jia0ddb3612021-04-22 13:35:26 -07002414 mPm = app.getSystemService(PackageManager.class);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002415 mMainThreadHandler = new MainThreadHandler();
Jack Yue37dd262022-12-16 11:53:37 -08002416 if (!PhoneFactory.isSubscriptionManagerServiceEnabled()) {
2417 mSubscriptionController = SubscriptionController.getInstance();
2418 } else {
2419 mSubscriptionController = null;
2420 }
Nazanin Bakhshif782e562018-12-11 15:15:39 -08002421 mTelephonySharedPreferences =
2422 PreferenceManager.getDefaultSharedPreferences(mApp);
yinxub1bed742017-04-17 11:45:04 -07002423 mNetworkScanRequestTracker = new NetworkScanRequestTracker();
Malcolm Chen2c63d402018-08-14 16:00:53 -07002424 mPhoneConfigurationManager = PhoneConfigurationManager.getInstance();
Daniel Bright94f43662021-03-01 14:43:40 -08002425 mRadioInterfaceCapabilities = RadioInterfaceCapabilityController.getInstance();
Peter Wanga3cf4ac2020-01-27 09:39:46 +08002426 mNotifyUserActivity = new AtomicBoolean(false);
Tyler Gunn64144d92022-03-17 14:16:41 -07002427 PropertyInvalidatedCache.invalidateCache(TelephonyManager.CACHE_KEY_PHONE_ACCOUNT_TO_SUBID);
Gil Cukierman6dac5eb2022-09-19 16:09:04 +00002428 mTelephony2gUpdater = new Telephony2gUpdater(
2429 Executors.newSingleThreadExecutor(), mApp);
2430 mTelephony2gUpdater.init();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002431 publish();
2432 }
2433
Gil Cukierman1c0eb932022-12-06 22:28:24 +00002434 @VisibleForTesting
2435 public SharedPreferences getSharedPreferences() {
2436 return mTelephonySharedPreferences;
2437 }
2438
Gil Cukierman92cc7db2023-01-06 19:25:53 +00002439 /**
2440 * Get the default phone for this device.
2441 */
2442 @VisibleForTesting
2443 public Phone getDefaultPhone() {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08002444 Phone thePhone = getPhone(getDefaultSubscription());
2445 return (thePhone != null) ? thePhone : PhoneFactory.getDefaultPhone();
2446 }
2447
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002448 private void publish() {
2449 if (DBG) log("publish: " + this);
2450
Peter Wangc035ce42020-01-08 21:00:22 -08002451 TelephonyFrameworkInitializer
2452 .getTelephonyServiceManager()
2453 .getTelephonyServiceRegisterer()
2454 .register(this);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002455 }
2456
Stuart Scott584921c2015-01-15 17:10:34 -08002457 private Phone getPhoneFromRequest(MainThreadRequest request) {
Jordan Liu4c733742019-02-28 12:03:40 -08002458 if (request.phone != null) {
2459 return request.phone;
2460 } else {
2461 return getPhoneFromSubId(request.subId);
2462 }
2463 }
2464
2465 private Phone getPhoneFromSubId(int subId) {
2466 return (subId == SubscriptionManager.INVALID_SUBSCRIPTION_ID)
2467 ? getDefaultPhone() : getPhone(subId);
Stuart Scott584921c2015-01-15 17:10:34 -08002468 }
2469
Rambo Wange53e07d2022-05-10 13:01:13 -07002470 @Nullable
2471 private UiccPort getUiccPortFromRequest(@NonNull MainThreadRequest request) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002472 Phone phone = getPhoneFromRequest(request);
2473 return phone == null ? null :
Muralidhar Reddy472c2ae2021-09-29 19:38:40 +00002474 UiccController.getInstance().getUiccPort(phone.getPhoneId());
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002475 }
2476
Wink Saville36469e72014-06-11 15:17:00 -07002477 // returns phone associated with the subId.
Wink Savilleb564aae2014-10-23 10:18:09 -07002478 private Phone getPhone(int subId) {
Jack Yu285100e2022-12-02 22:48:35 -08002479 return PhoneFactory.getPhone(SubscriptionManager.getPhoneId(subId));
Wink Saville36469e72014-06-11 15:17:00 -07002480 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002481
Kai Shif70f46f2021-03-03 13:59:46 -08002482 private void sendEraseModemConfig(@NonNull Phone phone) {
2483 Boolean success = (Boolean) sendRequest(CMD_ERASE_MODEM_CONFIG, null);
2484 if (DBG) log("eraseModemConfig:" + ' ' + (success ? "ok" : "fail"));
2485 }
2486
2487 private void sendEraseDataInSharedPreferences(@NonNull Phone phone) {
2488 Boolean success = (Boolean) sendRequest(CMD_ERASE_DATA_SHARED_PREFERENCES, null);
2489 if (DBG) log("eraseDataInSharedPreferences:" + ' ' + (success ? "ok" : "fail"));
Naina Nallurid63128d2019-09-17 14:10:30 -07002490 }
2491
Peter Wang44b186e2020-01-13 23:33:09 -08002492 private boolean isImsAvailableOnDevice() {
2493 PackageManager pm = getDefaultPhone().getContext().getPackageManager();
2494 if (pm == null) {
2495 // For some reason package manger is not available.. This will fail internally anyway,
2496 // so do not throw error and allow.
2497 return true;
2498 }
2499 return pm.hasSystemFeature(PackageManager.FEATURE_TELEPHONY_IMS, 0);
2500 }
2501
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002502 public void dial(String number) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07002503 dialForSubscriber(getPreferredVoiceSubscription(), number);
Wink Saville36469e72014-06-11 15:17:00 -07002504 }
2505
Wink Savilleb564aae2014-10-23 10:18:09 -07002506 public void dialForSubscriber(int subId, String number) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002507 if (DBG) log("dial: " + number);
2508 // No permission check needed here: This is just a wrapper around the
2509 // ACTION_DIAL intent, which is available to any app since it puts up
2510 // the UI before it does anything.
2511
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002512 final long identity = Binder.clearCallingIdentity();
2513 try {
2514 String url = createTelUrl(number);
2515 if (url == null) {
2516 return;
2517 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002518
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002519 // PENDING: should we just silently fail if phone is offhook or ringing?
2520 PhoneConstants.State state = mCM.getState(subId);
2521 if (state != PhoneConstants.State.OFFHOOK && state != PhoneConstants.State.RINGING) {
2522 Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse(url));
2523 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
2524 mApp.startActivity(intent);
2525 }
2526 } finally {
2527 Binder.restoreCallingIdentity(identity);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002528 }
2529 }
2530
2531 public void call(String callingPackage, String number) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07002532 callForSubscriber(getPreferredVoiceSubscription(), callingPackage, number);
Wink Saville36469e72014-06-11 15:17:00 -07002533 }
2534
Wink Savilleb564aae2014-10-23 10:18:09 -07002535 public void callForSubscriber(int subId, String callingPackage, String number) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002536 if (DBG) log("call: " + number);
2537
2538 // This is just a wrapper around the ACTION_CALL intent, but we still
2539 // need to do a permission check since we're calling startActivity()
2540 // from the context of the phone app.
2541 enforceCallPermission();
2542
Jordan Liu1617b712019-07-10 15:06:26 -07002543 if (mAppOps.noteOp(AppOpsManager.OPSTR_CALL_PHONE, Binder.getCallingUid(), callingPackage)
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002544 != AppOpsManager.MODE_ALLOWED) {
2545 return;
2546 }
2547
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002548 final long identity = Binder.clearCallingIdentity();
2549 try {
2550 String url = createTelUrl(number);
2551 if (url == null) {
2552 return;
2553 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002554
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002555 boolean isValid = false;
2556 final List<SubscriptionInfo> slist = getActiveSubscriptionInfoListPrivileged();
2557 if (slist != null) {
2558 for (SubscriptionInfo subInfoRecord : slist) {
2559 if (subInfoRecord.getSubscriptionId() == subId) {
2560 isValid = true;
2561 break;
2562 }
Wink Saville3ab207e2014-11-20 13:07:20 -08002563 }
Wink Saville08874612014-08-31 19:19:58 -07002564 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002565 if (!isValid) {
2566 return;
2567 }
Wink Saville08874612014-08-31 19:19:58 -07002568
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002569 Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse(url));
2570 intent.putExtra(SUBSCRIPTION_KEY, subId);
2571 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
2572 mApp.startActivity(intent);
2573 } finally {
2574 Binder.restoreCallingIdentity(identity);
2575 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002576 }
2577
Wink Savilleb564aae2014-10-23 10:18:09 -07002578 public boolean supplyPinForSubscriber(int subId, String pin) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07002579 int [] resultArray = supplyPinReportResultForSubscriber(subId, pin);
Wink Saville9de0f752013-10-22 19:04:03 -07002580 return (resultArray[0] == PhoneConstants.PIN_RESULT_SUCCESS) ? true : false;
2581 }
2582
Wink Savilleb564aae2014-10-23 10:18:09 -07002583 public boolean supplyPukForSubscriber(int subId, String puk, String pin) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07002584 int [] resultArray = supplyPukReportResultForSubscriber(subId, puk, pin);
Wink Saville9de0f752013-10-22 19:04:03 -07002585 return (resultArray[0] == PhoneConstants.PIN_RESULT_SUCCESS) ? true : false;
2586 }
2587
Wink Savilleb564aae2014-10-23 10:18:09 -07002588 public int[] supplyPinReportResultForSubscriber(int subId, String pin) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002589 enforceModifyPermission();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002590
2591 final long identity = Binder.clearCallingIdentity();
2592 try {
Michele Berionne5e411512020-11-13 02:36:59 +00002593 Phone phone = getPhone(subId);
2594 final UnlockSim checkSimPin = new UnlockSim(phone.getPhoneId(), phone.getIccCard());
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002595 checkSimPin.start();
2596 return checkSimPin.unlockSim(null, pin);
2597 } finally {
2598 Binder.restoreCallingIdentity(identity);
2599 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002600 }
2601
Wink Savilleb564aae2014-10-23 10:18:09 -07002602 public int[] supplyPukReportResultForSubscriber(int subId, String puk, String pin) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002603 enforceModifyPermission();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002604
2605 final long identity = Binder.clearCallingIdentity();
2606 try {
Michele Berionne5e411512020-11-13 02:36:59 +00002607 Phone phone = getPhone(subId);
2608 final UnlockSim checkSimPuk = new UnlockSim(phone.getPhoneId(), phone.getIccCard());
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002609 checkSimPuk.start();
2610 return checkSimPuk.unlockSim(puk, pin);
2611 } finally {
2612 Binder.restoreCallingIdentity(identity);
2613 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002614 }
2615
2616 /**
Wink Saville9de0f752013-10-22 19:04:03 -07002617 * Helper thread to turn async call to SimCard#supplyPin into
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002618 * a synchronous one.
2619 */
2620 private static class UnlockSim extends Thread {
2621
2622 private final IccCard mSimCard;
Michele Berionne5e411512020-11-13 02:36:59 +00002623 private final int mPhoneId;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002624
2625 private boolean mDone = false;
Wink Saville9de0f752013-10-22 19:04:03 -07002626 private int mResult = PhoneConstants.PIN_GENERAL_FAILURE;
2627 private int mRetryCount = -1;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002628
2629 // For replies from SimCard interface
2630 private Handler mHandler;
2631
2632 // For async handler to identify request type
2633 private static final int SUPPLY_PIN_COMPLETE = 100;
2634
Michele Berionne5e411512020-11-13 02:36:59 +00002635 UnlockSim(int phoneId, IccCard simCard) {
2636 mPhoneId = phoneId;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002637 mSimCard = simCard;
2638 }
2639
2640 @Override
2641 public void run() {
2642 Looper.prepare();
2643 synchronized (UnlockSim.this) {
2644 mHandler = new Handler() {
2645 @Override
2646 public void handleMessage(Message msg) {
2647 AsyncResult ar = (AsyncResult) msg.obj;
2648 switch (msg.what) {
2649 case SUPPLY_PIN_COMPLETE:
2650 Log.d(LOG_TAG, "SUPPLY_PIN_COMPLETE");
2651 synchronized (UnlockSim.this) {
Wink Saville9de0f752013-10-22 19:04:03 -07002652 mRetryCount = msg.arg1;
2653 if (ar.exception != null) {
2654 if (ar.exception instanceof CommandException &&
2655 ((CommandException)(ar.exception)).getCommandError()
2656 == CommandException.Error.PASSWORD_INCORRECT) {
2657 mResult = PhoneConstants.PIN_PASSWORD_INCORRECT;
vivi.lib5e9ada2019-09-12 16:04:24 +08002658 } //When UiccCardApp dispose,handle message and return exception
2659 else if (ar.exception instanceof CommandException &&
2660 ((CommandException) (ar.exception)).getCommandError()
2661 == CommandException.Error.ABORTED) {
2662 mResult = PhoneConstants.PIN_OPERATION_ABORTED;
Wink Saville9de0f752013-10-22 19:04:03 -07002663 } else {
2664 mResult = PhoneConstants.PIN_GENERAL_FAILURE;
2665 }
2666 } else {
2667 mResult = PhoneConstants.PIN_RESULT_SUCCESS;
2668 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002669 mDone = true;
2670 UnlockSim.this.notifyAll();
2671 }
2672 break;
2673 }
2674 }
2675 };
2676 UnlockSim.this.notifyAll();
2677 }
2678 Looper.loop();
2679 }
2680
2681 /*
2682 * Use PIN or PUK to unlock SIM card
2683 *
2684 * If PUK is null, unlock SIM card with PIN
2685 *
2686 * If PUK is not null, unlock SIM card with PUK and set PIN code
2687 */
Wink Saville9de0f752013-10-22 19:04:03 -07002688 synchronized int[] unlockSim(String puk, String pin) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002689
2690 while (mHandler == null) {
2691 try {
2692 wait();
2693 } catch (InterruptedException e) {
2694 Thread.currentThread().interrupt();
2695 }
2696 }
2697 Message callback = Message.obtain(mHandler, SUPPLY_PIN_COMPLETE);
2698
2699 if (puk == null) {
2700 mSimCard.supplyPin(pin, callback);
2701 } else {
2702 mSimCard.supplyPuk(puk, pin, callback);
2703 }
2704
2705 while (!mDone) {
2706 try {
2707 Log.d(LOG_TAG, "wait for done");
2708 wait();
2709 } catch (InterruptedException e) {
2710 // Restore the interrupted status
2711 Thread.currentThread().interrupt();
2712 }
2713 }
2714 Log.d(LOG_TAG, "done");
Wink Saville9de0f752013-10-22 19:04:03 -07002715 int[] resultArray = new int[2];
2716 resultArray[0] = mResult;
2717 resultArray[1] = mRetryCount;
Michele Berionne5e411512020-11-13 02:36:59 +00002718
2719 if (mResult == PhoneConstants.PIN_RESULT_SUCCESS && pin.length() > 0) {
Jon Spivack9c3bc762021-10-06 20:53:09 +00002720 UiccController.getInstance().getPinStorage().storePin(pin, mPhoneId);
Michele Berionne5e411512020-11-13 02:36:59 +00002721 }
2722
Wink Saville9de0f752013-10-22 19:04:03 -07002723 return resultArray;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002724 }
2725 }
2726
Nathan Harold7c8d0f12020-05-28 20:40:31 -07002727 /**
2728 * This method has been removed due to privacy and stability concerns.
2729 */
2730 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002731 public void updateServiceLocation() {
Nathan Harold7c8d0f12020-05-28 20:40:31 -07002732 Log.e(LOG_TAG, "Call to unsupported method updateServiceLocation()");
2733 return;
Wink Saville36469e72014-06-11 15:17:00 -07002734 }
2735
Nathan Harold1f889d82020-06-04 17:05:26 -07002736 @Override
2737 public void updateServiceLocationWithPackageName(String callingPackage) {
2738 mApp.getSystemService(AppOpsManager.class)
2739 .checkPackage(Binder.getCallingUid(), callingPackage);
2740
Nathan Haroldf096d982020-11-18 17:18:06 -08002741 final int targetSdk = TelephonyPermissions.getTargetSdk(mApp, callingPackage);
Nathan Harold1f889d82020-06-04 17:05:26 -07002742 if (targetSdk > android.os.Build.VERSION_CODES.R) {
2743 // Callers targeting S have no business invoking this method.
2744 return;
2745 }
2746
2747 LocationAccessPolicy.LocationPermissionResult locationResult =
2748 LocationAccessPolicy.checkLocationPermission(mApp,
2749 new LocationAccessPolicy.LocationPermissionQuery.Builder()
2750 .setCallingPackage(callingPackage)
2751 .setCallingFeatureId(null)
2752 .setCallingPid(Binder.getCallingPid())
2753 .setCallingUid(Binder.getCallingUid())
2754 .setMethod("updateServiceLocation")
2755 .setMinSdkVersionForCoarse(Build.VERSION_CODES.BASE)
2756 .setMinSdkVersionForFine(Build.VERSION_CODES.Q)
2757 .build());
2758 // Apps that lack location permission have no business calling this method;
2759 // however, because no permission was declared in the public API, denials must
2760 // all be "soft".
2761 switch (locationResult) {
2762 case DENIED_HARD: /* fall through */
2763 case DENIED_SOFT:
2764 return;
2765 }
2766
2767 WorkSource workSource = getWorkSource(Binder.getCallingUid());
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002768 final long identity = Binder.clearCallingIdentity();
2769 try {
Nathan Harold1f889d82020-06-04 17:05:26 -07002770 final Phone phone = getPhone(getDefaultSubscription());
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002771 if (phone != null) {
Nathan Harold1f889d82020-06-04 17:05:26 -07002772 phone.updateServiceLocation(workSource);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002773 }
2774 } finally {
2775 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07002776 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002777 }
2778
Philip P. Moltmann700a9592019-10-03 11:53:50 -07002779 @Deprecated
Robert Greenwalt36b23af2015-07-06 17:59:14 -07002780 @Override
2781 public boolean isRadioOn(String callingPackage) {
Philip P. Moltmann700a9592019-10-03 11:53:50 -07002782 return isRadioOnWithFeature(callingPackage, null);
2783 }
2784
2785
2786 @Override
2787 public boolean isRadioOnWithFeature(String callingPackage, String callingFeatureId) {
2788 return isRadioOnForSubscriberWithFeature(getDefaultSubscription(), callingPackage,
2789 callingFeatureId);
2790 }
2791
2792 @Deprecated
2793 @Override
2794 public boolean isRadioOnForSubscriber(int subId, String callingPackage) {
2795 return isRadioOnForSubscriberWithFeature(subId, callingPackage, null);
Wink Saville36469e72014-06-11 15:17:00 -07002796 }
2797
Robert Greenwalt36b23af2015-07-06 17:59:14 -07002798 @Override
Philip P. Moltmann700a9592019-10-03 11:53:50 -07002799 public boolean isRadioOnForSubscriberWithFeature(int subId, String callingPackage,
2800 String callingFeatureId) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08002801 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
Philip P. Moltmann700a9592019-10-03 11:53:50 -07002802 mApp, subId, callingPackage, callingFeatureId, "isRadioOnForSubscriber")) {
Robert Greenwalt36b23af2015-07-06 17:59:14 -07002803 return false;
2804 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002805
2806 final long identity = Binder.clearCallingIdentity();
2807 try {
2808 return isRadioOnForSubscriber(subId);
2809 } finally {
2810 Binder.restoreCallingIdentity(identity);
2811 }
Robert Greenwalt36b23af2015-07-06 17:59:14 -07002812 }
2813
2814 private boolean isRadioOnForSubscriber(int subId) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002815 final long identity = Binder.clearCallingIdentity();
2816 try {
2817 final Phone phone = getPhone(subId);
2818 if (phone != null) {
2819 return phone.getServiceState().getState() != ServiceState.STATE_POWER_OFF;
2820 } else {
2821 return false;
2822 }
2823 } finally {
2824 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07002825 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002826 }
2827
2828 public void toggleRadioOnOff() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07002829 toggleRadioOnOffForSubscriber(getDefaultSubscription());
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002830 }
Wink Saville36469e72014-06-11 15:17:00 -07002831
Wink Savilleb564aae2014-10-23 10:18:09 -07002832 public void toggleRadioOnOffForSubscriber(int subId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002833 enforceModifyPermission();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002834
2835 final long identity = Binder.clearCallingIdentity();
2836 try {
2837 final Phone phone = getPhone(subId);
2838 if (phone != null) {
2839 phone.setRadioPower(!isRadioOnForSubscriber(subId));
2840 }
2841 } finally {
2842 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07002843 }
Wink Saville36469e72014-06-11 15:17:00 -07002844 }
2845
2846 public boolean setRadio(boolean turnOn) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07002847 return setRadioForSubscriber(getDefaultSubscription(), turnOn);
Wink Saville36469e72014-06-11 15:17:00 -07002848 }
2849
Wink Savilleb564aae2014-10-23 10:18:09 -07002850 public boolean setRadioForSubscriber(int subId, boolean turnOn) {
Wink Saville36469e72014-06-11 15:17:00 -07002851 enforceModifyPermission();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002852
2853 final long identity = Binder.clearCallingIdentity();
2854 try {
2855 final Phone phone = getPhone(subId);
2856 if (phone == null) {
2857 return false;
2858 }
2859 if ((phone.getServiceState().getState() != ServiceState.STATE_POWER_OFF) != turnOn) {
2860 toggleRadioOnOffForSubscriber(subId);
2861 }
2862 return true;
2863 } finally {
2864 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07002865 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002866 }
Wink Saville36469e72014-06-11 15:17:00 -07002867
Naveen Kalla1fd79bd2014-08-08 00:48:59 -07002868 public boolean needMobileRadioShutdown() {
Shuo Qianfa7b6b32019-12-10 10:40:38 -08002869 enforceReadPrivilegedPermission("needMobileRadioShutdown");
Naveen Kalla1fd79bd2014-08-08 00:48:59 -07002870 /*
2871 * If any of the Radios are available, it will need to be
2872 * shutdown. So return true if any Radio is available.
2873 */
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002874 final long identity = Binder.clearCallingIdentity();
2875 try {
2876 for (int i = 0; i < TelephonyManager.getDefault().getPhoneCount(); i++) {
2877 Phone phone = PhoneFactory.getPhone(i);
2878 if (phone != null && phone.isRadioAvailable()) return true;
2879 }
2880 logv(TelephonyManager.getDefault().getPhoneCount() + " Phones are shutdown.");
2881 return false;
2882 } finally {
2883 Binder.restoreCallingIdentity(identity);
Naveen Kalla1fd79bd2014-08-08 00:48:59 -07002884 }
Naveen Kalla1fd79bd2014-08-08 00:48:59 -07002885 }
2886
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002887 @Override
Naveen Kalla1fd79bd2014-08-08 00:48:59 -07002888 public void shutdownMobileRadios() {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002889 enforceModifyPermission();
2890
2891 final long identity = Binder.clearCallingIdentity();
2892 try {
2893 for (int i = 0; i < TelephonyManager.getDefault().getPhoneCount(); i++) {
2894 logv("Shutting down Phone " + i);
2895 shutdownRadioUsingPhoneId(i);
2896 }
2897 } finally {
2898 Binder.restoreCallingIdentity(identity);
Naveen Kalla1fd79bd2014-08-08 00:48:59 -07002899 }
2900 }
2901
2902 private void shutdownRadioUsingPhoneId(int phoneId) {
Naveen Kalla1fd79bd2014-08-08 00:48:59 -07002903 Phone phone = PhoneFactory.getPhone(phoneId);
2904 if (phone != null && phone.isRadioAvailable()) {
2905 phone.shutdownRadio();
2906 }
2907 }
2908
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002909 public boolean setRadioPower(boolean turnOn) {
Jack Yub4e16162017-05-15 12:48:40 -07002910 enforceModifyPermission();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002911
2912 final long identity = Binder.clearCallingIdentity();
2913 try {
2914 final Phone defaultPhone = PhoneFactory.getDefaultPhone();
2915 if (defaultPhone != null) {
2916 defaultPhone.setRadioPower(turnOn);
2917 return true;
2918 } else {
2919 loge("There's no default phone.");
2920 return false;
2921 }
2922 } finally {
2923 Binder.restoreCallingIdentity(identity);
Wei Liu9ae2a062016-08-08 11:09:34 -07002924 }
Wink Saville36469e72014-06-11 15:17:00 -07002925 }
2926
Wink Savilleb564aae2014-10-23 10:18:09 -07002927 public boolean setRadioPowerForSubscriber(int subId, boolean turnOn) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002928 enforceModifyPermission();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002929
2930 final long identity = Binder.clearCallingIdentity();
2931 try {
2932 final Phone phone = getPhone(subId);
2933 if (phone != null) {
2934 phone.setRadioPower(turnOn);
2935 return true;
2936 } else {
2937 return false;
2938 }
2939 } finally {
2940 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07002941 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002942 }
2943
Thomas Nguyenfd0572f2022-07-15 22:28:49 +00002944 /**
2945 * Vote on powering off the radio for a reason. The radio will be turned on only when there is
2946 * no reason to power it off. When any of the voters want to power it off, it will be turned
2947 * off. In case of emergency, the radio will be turned on even if there are some reasons for
2948 * powering it off, and these radio off votes will be cleared.
2949 * Multiple apps can vote for the same reason and the last vote will take effect. Each app is
2950 * responsible for its vote. A powering-off vote of a reason will be maintained until it is
2951 * cleared by calling {@link clearRadioPowerOffForReason} for that reason, or an emergency call
2952 * is made, or the device is rebooted. When an app comes backup from a crash, it needs to make
2953 * sure if its vote is as expected. An app can use the API {@link getRadioPowerOffReasons} to
2954 * check its vote.
2955 *
2956 * @param subId The subscription ID.
2957 * @param reason The reason for powering off radio.
2958 * @return true on success and false on failure.
2959 */
2960 public boolean requestRadioPowerOffForReason(int subId,
2961 @TelephonyManager.RadioPowerReason int reason) {
2962 enforceModifyPermission();
2963
2964 final long identity = Binder.clearCallingIdentity();
2965 try {
2966 final Phone phone = getPhone(subId);
2967 if (phone != null) {
2968 phone.setRadioPowerForReason(false, reason);
2969 return true;
2970 } else {
2971 return false;
2972 }
2973 } finally {
2974 Binder.restoreCallingIdentity(identity);
2975 }
2976 }
2977
2978 /**
2979 * Remove the vote on powering off the radio for a reason, as requested by
2980 * {@link requestRadioPowerOffForReason}.
2981 *
2982 * @param subId The subscription ID.
2983 * @param reason The reason for powering off radio.
2984 * @return true on success and false on failure.
2985 */
2986 public boolean clearRadioPowerOffForReason(int subId,
2987 @TelephonyManager.RadioPowerReason int reason) {
2988 enforceModifyPermission();
2989
2990 final long identity = Binder.clearCallingIdentity();
2991 try {
2992 final Phone phone = getPhone(subId);
2993 if (phone != null) {
2994 phone.setRadioPowerForReason(true, reason);
2995 return true;
2996 } else {
2997 return false;
2998 }
2999 } finally {
3000 Binder.restoreCallingIdentity(identity);
3001 }
3002 }
3003
3004 /**
3005 * Get reasons for powering off radio, as requested by {@link requestRadioPowerOffForReason}.
3006 *
3007 * @param subId The subscription ID.
3008 * @param callingPackage The package making the call.
3009 * @param callingFeatureId The feature in the package.
3010 * @return List of reasons for powering off radio.
3011 */
3012 public List getRadioPowerOffReasons(int subId, String callingPackage, String callingFeatureId) {
3013 enforceReadPrivilegedPermission("getRadioPowerOffReasons");
3014
3015 final long identity = Binder.clearCallingIdentity();
3016 List result = new ArrayList();
3017 try {
3018 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(mApp, subId,
3019 callingPackage, callingFeatureId, "getRadioPowerOffReasons")) {
3020 return result;
3021 }
3022
3023 final Phone phone = getPhone(subId);
3024 if (phone != null) {
3025 result.addAll(phone.getRadioPowerOffReasons());
3026 }
3027 } finally {
3028 Binder.restoreCallingIdentity(identity);
3029 }
3030 return result;
3031 }
3032
Wink Saville36469e72014-06-11 15:17:00 -07003033 // FIXME: subId version needed
Sanket Padawe356d7632015-06-22 14:03:32 -07003034 @Override
Sarah Chinecc78c42022-03-31 21:16:48 -07003035 public boolean enableDataConnectivity(String callingPackage) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003036 enforceModifyPermission();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003037
3038 final long identity = Binder.clearCallingIdentity();
3039 try {
Jack Yu285100e2022-12-02 22:48:35 -08003040 int subId = SubscriptionManager.getDefaultDataSubscriptionId();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003041 final Phone phone = getPhone(subId);
3042 if (phone != null) {
Jack Yu7968c6d2022-07-31 00:43:21 -07003043 phone.getDataSettingsManager().setDataEnabled(
3044 TelephonyManager.DATA_ENABLED_REASON_USER, true, callingPackage);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003045 return true;
3046 } else {
3047 return false;
3048 }
3049 } finally {
3050 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07003051 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003052 }
3053
Wink Saville36469e72014-06-11 15:17:00 -07003054 // FIXME: subId version needed
Sanket Padawe356d7632015-06-22 14:03:32 -07003055 @Override
Sarah Chinecc78c42022-03-31 21:16:48 -07003056 public boolean disableDataConnectivity(String callingPackage) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003057 enforceModifyPermission();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003058
3059 final long identity = Binder.clearCallingIdentity();
3060 try {
Jack Yu285100e2022-12-02 22:48:35 -08003061 int subId = SubscriptionManager.getDefaultDataSubscriptionId();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003062 final Phone phone = getPhone(subId);
3063 if (phone != null) {
Jack Yu7968c6d2022-07-31 00:43:21 -07003064 phone.getDataSettingsManager().setDataEnabled(
3065 TelephonyManager.DATA_ENABLED_REASON_USER, false, callingPackage);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003066 return true;
3067 } else {
3068 return false;
3069 }
3070 } finally {
3071 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07003072 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003073 }
3074
Sanket Padawe356d7632015-06-22 14:03:32 -07003075 @Override
Jack Yuacf8a132017-05-01 17:00:48 -07003076 public boolean isDataConnectivityPossible(int subId) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003077 final long identity = Binder.clearCallingIdentity();
3078 try {
3079 final Phone phone = getPhone(subId);
3080 if (phone != null) {
Jack Yu59824e12022-03-23 01:42:44 -07003081 return phone.isDataAllowed();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003082 } else {
3083 return false;
3084 }
3085 } finally {
3086 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07003087 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003088 }
3089
3090 public boolean handlePinMmi(String dialString) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07003091 return handlePinMmiForSubscriber(getDefaultSubscription(), dialString);
Wink Saville36469e72014-06-11 15:17:00 -07003092 }
3093
pkanwarae03a6b2016-11-06 20:37:09 -08003094 public void handleUssdRequest(int subId, String ussdRequest, ResultReceiver wrappedCallback) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003095 enforceCallPermission();
3096
3097 final long identity = Binder.clearCallingIdentity();
3098 try {
3099 if (!SubscriptionManager.isValidSubscriptionId(subId)) {
3100 return;
3101 }
3102 Pair<String, ResultReceiver> ussdObject = new Pair(ussdRequest, wrappedCallback);
3103 sendRequest(CMD_HANDLE_USSD_REQUEST, ussdObject, subId);
3104 } finally {
3105 Binder.restoreCallingIdentity(identity);
3106 }
pkanwar32d516d2016-10-14 19:37:38 -07003107 };
3108
Wink Savilleb564aae2014-10-23 10:18:09 -07003109 public boolean handlePinMmiForSubscriber(int subId, String dialString) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003110 enforceModifyPermission();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003111
3112 final long identity = Binder.clearCallingIdentity();
3113 try {
3114 if (!SubscriptionManager.isValidSubscriptionId(subId)) {
3115 return false;
3116 }
3117 return (Boolean) sendRequest(CMD_HANDLE_PIN_MMI, dialString, subId);
3118 } finally {
3119 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07003120 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003121 }
3122
Brad Ebinger4f6208e2021-03-23 21:04:45 +00003123 /**
3124 * @deprecated This method is deprecated and is only being kept due to an UnsupportedAppUsage
3125 * tag on getCallState Binder call.
3126 */
3127 @Deprecated
3128 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003129 public int getCallState() {
Brad Ebinger4f6208e2021-03-23 21:04:45 +00003130 if (CompatChanges.isChangeEnabled(
3131 TelecomManager.ENABLE_GET_CALL_STATE_PERMISSION_PROTECTION,
3132 Binder.getCallingUid())) {
3133 // Do not allow this API to be called on API version 31+, it should only be
3134 // called on old apps using this Binder call directly.
3135 throw new SecurityException("This method can only be used for applications "
3136 + "targeting API version 30 or less.");
3137 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003138 final long identity = Binder.clearCallingIdentity();
3139 try {
Brad Ebinger4f6208e2021-03-23 21:04:45 +00003140 Phone phone = getPhone(getDefaultSubscription());
3141 return phone == null ? TelephonyManager.CALL_STATE_IDLE :
3142 PhoneConstantConversions.convertCallState(phone.getState());
3143 } finally {
3144 Binder.restoreCallingIdentity(identity);
3145 }
3146 }
3147
3148 @Override
3149 public int getCallStateForSubscription(int subId, String callingPackage, String featureId) {
3150 if (CompatChanges.isChangeEnabled(
3151 TelecomManager.ENABLE_GET_CALL_STATE_PERMISSION_PROTECTION,
3152 Binder.getCallingUid())) {
3153 // Check READ_PHONE_STATE for API version 31+
3154 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(mApp, subId, callingPackage,
3155 featureId, "getCallStateForSubscription")) {
3156 throw new SecurityException("getCallState requires READ_PHONE_STATE for apps "
3157 + "targeting API level 31+.");
3158 }
3159 }
3160 final long identity = Binder.clearCallingIdentity();
3161 try {
3162 Phone phone = getPhone(subId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003163 return phone == null ? TelephonyManager.CALL_STATE_IDLE :
3164 PhoneConstantConversions.convertCallState(phone.getState());
3165 } finally {
3166 Binder.restoreCallingIdentity(identity);
3167 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003168 }
3169
Sanket Padawe356d7632015-06-22 14:03:32 -07003170 @Override
Nathan Harolde037c472019-06-26 00:41:07 +00003171 public int getDataState() {
Jack Yu285100e2022-12-02 22:48:35 -08003172 return getDataStateForSubId(SubscriptionManager.getDefaultDataSubscriptionId());
Nathan Haroldc4689b12019-06-14 16:58:30 -07003173 }
3174
3175 @Override
3176 public int getDataStateForSubId(int subId) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003177 final long identity = Binder.clearCallingIdentity();
3178 try {
Nathan Haroldc4689b12019-06-14 16:58:30 -07003179 final Phone phone = getPhone(subId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003180 if (phone != null) {
Jack Yu7968c6d2022-07-31 00:43:21 -07003181 return phone.getDataNetworkController().getInternetDataNetworkState();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003182 } else {
3183 return PhoneConstantConversions.convertDataState(
3184 PhoneConstants.DataState.DISCONNECTED);
3185 }
3186 } finally {
3187 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07003188 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003189 }
3190
Sanket Padawe356d7632015-06-22 14:03:32 -07003191 @Override
Jack Yu0eda6842022-04-18 00:34:46 -07003192 public @DataActivityType int getDataActivity() {
Jack Yu285100e2022-12-02 22:48:35 -08003193 return getDataActivityForSubId(SubscriptionManager.getDefaultDataSubscriptionId());
Nathan Haroldc4689b12019-06-14 16:58:30 -07003194 }
3195
3196 @Override
Jack Yu0eda6842022-04-18 00:34:46 -07003197 public @DataActivityType int getDataActivityForSubId(int subId) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003198 final long identity = Binder.clearCallingIdentity();
3199 try {
Nathan Haroldc4689b12019-06-14 16:58:30 -07003200 final Phone phone = getPhone(subId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003201 if (phone != null) {
Jack Yu0eda6842022-04-18 00:34:46 -07003202 return phone.getDataActivityState();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003203 } else {
3204 return TelephonyManager.DATA_ACTIVITY_NONE;
3205 }
3206 } finally {
3207 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07003208 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003209 }
3210
3211 @Override
Meng Wanga10e89e2019-12-09 13:13:01 -08003212 public CellIdentity getCellLocation(String callingPackage, String callingFeatureId) {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08003213 mApp.getSystemService(AppOpsManager.class)
Hall Liu1aa510f2017-11-22 17:40:08 -08003214 .checkPackage(Binder.getCallingUid(), callingPackage);
Hall Liuf19c44f2018-11-27 14:38:17 -08003215
3216 LocationAccessPolicy.LocationPermissionResult locationResult =
3217 LocationAccessPolicy.checkLocationPermission(mApp,
3218 new LocationAccessPolicy.LocationPermissionQuery.Builder()
3219 .setCallingPackage(callingPackage)
Philip P. Moltmann3a2772a2019-10-04 08:15:00 -07003220 .setCallingFeatureId(callingFeatureId)
Hall Liuf19c44f2018-11-27 14:38:17 -08003221 .setCallingPid(Binder.getCallingPid())
3222 .setCallingUid(Binder.getCallingUid())
3223 .setMethod("getCellLocation")
Hall Liu773ba022020-01-24 18:07:12 -08003224 .setMinSdkVersionForCoarse(Build.VERSION_CODES.BASE)
Hall Liuf19c44f2018-11-27 14:38:17 -08003225 .setMinSdkVersionForFine(Build.VERSION_CODES.Q)
3226 .build());
3227 switch (locationResult) {
3228 case DENIED_HARD:
3229 throw new SecurityException("Not allowed to access cell location");
3230 case DENIED_SOFT:
Meng Wanga10e89e2019-12-09 13:13:01 -08003231 return (getDefaultPhone().getPhoneType() == PhoneConstants.PHONE_TYPE_CDMA)
3232 ? new CellIdentityCdma() : new CellIdentityGsm();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003233 }
3234
Narayan Kamathf04b5a12018-01-09 11:47:15 +00003235 WorkSource workSource = getWorkSource(Binder.getCallingUid());
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003236 final long identity = Binder.clearCallingIdentity();
3237 try {
3238 if (DBG_LOC) log("getCellLocation: is active user");
Jack Yu285100e2022-12-02 22:48:35 -08003239 int subId = SubscriptionManager.getDefaultDataSubscriptionId();
Meng Wanga10e89e2019-12-09 13:13:01 -08003240 return (CellIdentity) sendRequest(CMD_GET_CELL_LOCATION, workSource, subId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003241 } finally {
3242 Binder.restoreCallingIdentity(identity);
3243 }
Svetoslav64fad262015-04-14 14:35:21 -07003244 }
3245
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003246 @Override
Jack Yueb1e7fe2020-02-22 19:38:58 -08003247 public String getNetworkCountryIsoForPhone(int phoneId) {
Jonathan Basseribf5362b2017-07-19 12:22:35 -07003248 // Reporting the correct network country is ambiguous when IWLAN could conflict with
3249 // registered cell info, so return a NULL country instead.
3250 final long identity = Binder.clearCallingIdentity();
3251 try {
Malcolm Chen3732c2b2018-07-18 20:15:24 -07003252 if (phoneId == SubscriptionManager.INVALID_PHONE_INDEX) {
3253 // Get default phone in this case.
3254 phoneId = SubscriptionManager.DEFAULT_PHONE_INDEX;
3255 }
Jack Yu285100e2022-12-02 22:48:35 -08003256 final int subId = SubscriptionManager.getSubscriptionId(phoneId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003257 Phone phone = PhoneFactory.getPhone(phoneId);
Nathan Harold532f51c2020-04-21 19:31:10 -07003258 if (phone == null) return "";
3259 ServiceStateTracker sst = phone.getServiceStateTracker();
3260 if (sst == null) return "";
3261 LocaleTracker lt = sst.getLocaleTracker();
3262 if (lt == null) return "";
Shuo Qian9418a922021-03-09 11:21:16 -08003263 return lt.getCurrentCountry();
Jonathan Basseribf5362b2017-07-19 12:22:35 -07003264 } finally {
3265 Binder.restoreCallingIdentity(identity);
3266 }
Jonathan Basseribf5362b2017-07-19 12:22:35 -07003267 }
3268
Nathan Harold7c8d0f12020-05-28 20:40:31 -07003269 /**
3270 * This method was removed due to potential issues caused by performing partial
3271 * updates of service state, and lack of a credible use case.
3272 *
3273 * This has the ability to break the telephony implementation by disabling notification of
3274 * changes in device connectivity. DO NOT USE THIS!
3275 */
Jonathan Basseribf5362b2017-07-19 12:22:35 -07003276 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003277 public void enableLocationUpdates() {
3278 mApp.enforceCallingOrSelfPermission(
3279 android.Manifest.permission.CONTROL_LOCATION_UPDATES, null);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003280 }
3281
Nathan Harold7c8d0f12020-05-28 20:40:31 -07003282 /**
3283 * This method was removed due to potential issues caused by performing partial
3284 * updates of service state, and lack of a credible use case.
3285 *
3286 * This has the ability to break the telephony implementation by disabling notification of
3287 * changes in device connectivity. DO NOT USE THIS!
3288 */
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003289 @Override
3290 public void disableLocationUpdates() {
3291 mApp.enforceCallingOrSelfPermission(
3292 android.Manifest.permission.CONTROL_LOCATION_UPDATES, null);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003293 }
3294
3295 @Override
3296 @SuppressWarnings("unchecked")
Philip P. Moltmann3a2772a2019-10-04 08:15:00 -07003297 public List<NeighboringCellInfo> getNeighboringCellInfo(String callingPackage,
3298 String callingFeatureId) {
Nathan Haroldb55f63b2021-07-27 11:27:38 -07003299 try {
3300 mApp.getSystemService(AppOpsManager.class)
3301 .checkPackage(Binder.getCallingUid(), callingPackage);
3302 } catch (SecurityException e) {
3303 EventLog.writeEvent(0x534e4554, "190619791", Binder.getCallingUid());
3304 throw e;
3305 }
3306
Nathan Haroldf096d982020-11-18 17:18:06 -08003307 final int targetSdk = TelephonyPermissions.getTargetSdk(mApp, callingPackage);
Nathan Harolddbea45a2018-08-30 14:35:07 -07003308 if (targetSdk >= android.os.Build.VERSION_CODES.Q) {
3309 throw new SecurityException(
3310 "getNeighboringCellInfo() is unavailable to callers targeting Q+ SDK levels.");
3311 }
Nathan Haroldb4d55612018-07-20 13:13:08 -07003312
Jordan Liu1617b712019-07-10 15:06:26 -07003313 if (mAppOps.noteOp(AppOpsManager.OPSTR_NEIGHBORING_CELLS, Binder.getCallingUid(),
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003314 callingPackage) != AppOpsManager.MODE_ALLOWED) {
3315 return null;
3316 }
Svetoslav64fad262015-04-14 14:35:21 -07003317
Svetoslav Ganov4a9d4482017-06-20 19:53:35 -07003318 if (DBG_LOC) log("getNeighboringCellInfo: is active user");
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003319
Philip P. Moltmann3a2772a2019-10-04 08:15:00 -07003320 List<CellInfo> info = getAllCellInfo(callingPackage, callingFeatureId);
Nathan Haroldf180aac2018-06-01 18:43:55 -07003321 if (info == null) return null;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003322
Nathan Haroldf180aac2018-06-01 18:43:55 -07003323 List<NeighboringCellInfo> neighbors = new ArrayList<NeighboringCellInfo>();
3324 for (CellInfo ci : info) {
3325 if (ci instanceof CellInfoGsm) {
3326 neighbors.add(new NeighboringCellInfo((CellInfoGsm) ci));
3327 } else if (ci instanceof CellInfoWcdma) {
3328 neighbors.add(new NeighboringCellInfo((CellInfoWcdma) ci));
3329 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003330 }
Nathan Haroldf180aac2018-06-01 18:43:55 -07003331 return (neighbors.size()) > 0 ? neighbors : null;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003332 }
3333
Nathan Haroldfa8da0f2018-09-27 18:51:29 -07003334 private List<CellInfo> getCachedCellInfo() {
3335 List<CellInfo> cellInfos = new ArrayList<CellInfo>();
3336 for (Phone phone : PhoneFactory.getPhones()) {
3337 List<CellInfo> info = phone.getAllCellInfo();
3338 if (info != null) cellInfos.addAll(info);
3339 }
3340 return cellInfos;
3341 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003342
3343 @Override
Philip P. Moltmann3a2772a2019-10-04 08:15:00 -07003344 public List<CellInfo> getAllCellInfo(String callingPackage, String callingFeatureId) {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08003345 mApp.getSystemService(AppOpsManager.class)
Hall Liu1aa510f2017-11-22 17:40:08 -08003346 .checkPackage(Binder.getCallingUid(), callingPackage);
Hall Liuf19c44f2018-11-27 14:38:17 -08003347
3348 LocationAccessPolicy.LocationPermissionResult locationResult =
3349 LocationAccessPolicy.checkLocationPermission(mApp,
3350 new LocationAccessPolicy.LocationPermissionQuery.Builder()
3351 .setCallingPackage(callingPackage)
Philip P. Moltmann3a2772a2019-10-04 08:15:00 -07003352 .setCallingFeatureId(callingFeatureId)
Hall Liuf19c44f2018-11-27 14:38:17 -08003353 .setCallingPid(Binder.getCallingPid())
3354 .setCallingUid(Binder.getCallingUid())
3355 .setMethod("getAllCellInfo")
Nathan Harold5ae50b52019-02-20 15:46:36 -08003356 .setMinSdkVersionForCoarse(Build.VERSION_CODES.BASE)
Hall Liuf19c44f2018-11-27 14:38:17 -08003357 .setMinSdkVersionForFine(Build.VERSION_CODES.Q)
3358 .build());
3359 switch (locationResult) {
3360 case DENIED_HARD:
3361 throw new SecurityException("Not allowed to access cell info");
3362 case DENIED_SOFT:
3363 return new ArrayList<>();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003364 }
3365
Nathan Haroldf096d982020-11-18 17:18:06 -08003366 final int targetSdk = TelephonyPermissions.getTargetSdk(mApp, callingPackage);
Nathan Haroldfa8da0f2018-09-27 18:51:29 -07003367 if (targetSdk >= android.os.Build.VERSION_CODES.Q) {
3368 return getCachedCellInfo();
3369 }
3370
Svetoslav Ganov4a9d4482017-06-20 19:53:35 -07003371 if (DBG_LOC) log("getAllCellInfo: is active user");
Narayan Kamathf04b5a12018-01-09 11:47:15 +00003372 WorkSource workSource = getWorkSource(Binder.getCallingUid());
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003373 final long identity = Binder.clearCallingIdentity();
3374 try {
3375 List<CellInfo> cellInfos = new ArrayList<CellInfo>();
3376 for (Phone phone : PhoneFactory.getPhones()) {
Nathan Harold3ff88932018-08-14 10:19:49 -07003377 final List<CellInfo> info = (List<CellInfo>) sendRequest(
Nathan Harold92bed182018-10-12 18:16:49 -07003378 CMD_GET_ALL_CELL_INFO, null, phone, workSource);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003379 if (info != null) cellInfos.addAll(info);
3380 }
3381 return cellInfos;
3382 } finally {
3383 Binder.restoreCallingIdentity(identity);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003384 }
3385 }
3386
Sailesh Nepalbd76e4e2013-10-27 13:59:44 -07003387 @Override
Philip P. Moltmann3a2772a2019-10-04 08:15:00 -07003388 public void requestCellInfoUpdate(int subId, ICellInfoCallback cb, String callingPackage,
3389 String callingFeatureId) {
3390 requestCellInfoUpdateInternal(subId, cb, callingPackage, callingFeatureId,
3391 getWorkSource(Binder.getCallingUid()));
Nathan Haroldfa8da0f2018-09-27 18:51:29 -07003392 }
3393
3394 @Override
Philip P. Moltmann3a2772a2019-10-04 08:15:00 -07003395 public void requestCellInfoUpdateWithWorkSource(int subId, ICellInfoCallback cb,
3396 String callingPackage, String callingFeatureId, WorkSource workSource) {
Nathan Haroldfa8da0f2018-09-27 18:51:29 -07003397 enforceModifyPermission();
Philip P. Moltmann3a2772a2019-10-04 08:15:00 -07003398 requestCellInfoUpdateInternal(subId, cb, callingPackage, callingFeatureId, workSource);
Nathan Haroldfa8da0f2018-09-27 18:51:29 -07003399 }
3400
Philip P. Moltmann3a2772a2019-10-04 08:15:00 -07003401 private void requestCellInfoUpdateInternal(int subId, ICellInfoCallback cb,
3402 String callingPackage, String callingFeatureId, WorkSource workSource) {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08003403 mApp.getSystemService(AppOpsManager.class)
Nathan Haroldfa8da0f2018-09-27 18:51:29 -07003404 .checkPackage(Binder.getCallingUid(), callingPackage);
Hall Liuf19c44f2018-11-27 14:38:17 -08003405
3406 LocationAccessPolicy.LocationPermissionResult locationResult =
3407 LocationAccessPolicy.checkLocationPermission(mApp,
3408 new LocationAccessPolicy.LocationPermissionQuery.Builder()
3409 .setCallingPackage(callingPackage)
Philip P. Moltmann3a2772a2019-10-04 08:15:00 -07003410 .setCallingFeatureId(callingFeatureId)
Hall Liuf19c44f2018-11-27 14:38:17 -08003411 .setCallingPid(Binder.getCallingPid())
3412 .setCallingUid(Binder.getCallingUid())
3413 .setMethod("requestCellInfoUpdate")
Hall Liud60acc92020-05-21 17:09:35 -07003414 .setMinSdkVersionForCoarse(Build.VERSION_CODES.BASE)
3415 .setMinSdkVersionForFine(Build.VERSION_CODES.BASE)
Hall Liuf19c44f2018-11-27 14:38:17 -08003416 .build());
3417 switch (locationResult) {
3418 case DENIED_HARD:
Nathan Haroldf096d982020-11-18 17:18:06 -08003419 if (TelephonyPermissions
3420 .getTargetSdk(mApp, callingPackage) < Build.VERSION_CODES.Q) {
Hall Liud60acc92020-05-21 17:09:35 -07003421 // Safetynet logging for b/154934934
3422 EventLog.writeEvent(0x534e4554, "154934934", Binder.getCallingUid());
3423 }
Hall Liuf19c44f2018-11-27 14:38:17 -08003424 throw new SecurityException("Not allowed to access cell info");
3425 case DENIED_SOFT:
Nathan Haroldf096d982020-11-18 17:18:06 -08003426 if (TelephonyPermissions
3427 .getTargetSdk(mApp, callingPackage) < Build.VERSION_CODES.Q) {
Hall Liud60acc92020-05-21 17:09:35 -07003428 // Safetynet logging for b/154934934
3429 EventLog.writeEvent(0x534e4554, "154934934", Binder.getCallingUid());
3430 }
Nathan Harold5320c422019-05-09 10:26:08 -07003431 try {
3432 cb.onCellInfo(new ArrayList<CellInfo>());
3433 } catch (RemoteException re) {
3434 // Drop without consequences
3435 }
Hall Liuf19c44f2018-11-27 14:38:17 -08003436 return;
Nathan Haroldfa8da0f2018-09-27 18:51:29 -07003437 }
3438
Nathan Harolda939a962019-05-09 10:13:47 -07003439
3440 final Phone phone = getPhoneFromSubId(subId);
Nathan Haroldfa8da0f2018-09-27 18:51:29 -07003441 if (phone == null) throw new IllegalArgumentException("Invalid Subscription Id: " + subId);
3442
3443 sendRequestAsync(CMD_REQUEST_CELL_INFO_UPDATE, cb, phone, workSource);
3444 }
3445
3446 @Override
Aishwarya Mallampati0603fb12022-08-24 21:16:56 +00003447 public void setCellInfoListRate(int rateInMillis, int subId) {
Jack Yua8d8cb82017-01-16 10:15:34 -08003448 enforceModifyPermission();
Narayan Kamathf04b5a12018-01-09 11:47:15 +00003449 WorkSource workSource = getWorkSource(Binder.getCallingUid());
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003450
3451 final long identity = Binder.clearCallingIdentity();
3452 try {
Aishwarya Mallampati0603fb12022-08-24 21:16:56 +00003453 Phone phone = getPhone(subId);
3454 if (phone == null) {
3455 getDefaultPhone().setCellInfoListRate(rateInMillis, workSource);
3456 } else {
3457 phone.setCellInfoListRate(rateInMillis, workSource);
3458 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003459 } finally {
3460 Binder.restoreCallingIdentity(identity);
3461 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003462 }
3463
Shishir Agrawala9f32182016-04-12 12:00:16 -07003464 @Override
Philip P. Moltmann700a9592019-10-03 11:53:50 -07003465 public String getImeiForSlot(int slotIndex, String callingPackage, String callingFeatureId) {
Jeff Davidson913390f2018-02-23 17:11:49 -08003466 Phone phone = PhoneFactory.getPhone(slotIndex);
3467 if (phone == null) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08003468 return null;
3469 }
Jeff Davidson913390f2018-02-23 17:11:49 -08003470 int subId = phone.getSubId();
Grace Jia0ddb3612021-04-22 13:35:26 -07003471 enforceCallingPackage(callingPackage, Binder.getCallingUid(), "getImeiForSlot");
Michael Groover70af6dc2018-10-01 16:23:15 -07003472 if (!TelephonyPermissions.checkCallingOrSelfReadDeviceIdentifiers(mApp, subId,
Philip P. Moltmann700a9592019-10-03 11:53:50 -07003473 callingPackage, callingFeatureId, "getImeiForSlot")) {
Jeff Davidson913390f2018-02-23 17:11:49 -08003474 return null;
3475 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003476
3477 final long identity = Binder.clearCallingIdentity();
3478 try {
3479 return phone.getImei();
3480 } finally {
3481 Binder.restoreCallingIdentity(identity);
3482 }
Shishir Agrawala9f32182016-04-12 12:00:16 -07003483 }
3484
3485 @Override
arunvoddud5c6ce02022-12-11 06:03:12 +00003486 public String getPrimaryImei(String callingPackage, String callingFeatureId) {
3487 enforceCallingPackage(callingPackage, Binder.getCallingUid(), "getPrimaryImei");
3488 if (!checkCallingOrSelfReadDeviceIdentifiersForAnySub(mApp, callingPackage,
3489 callingFeatureId, "getPrimaryImei")) {
3490 throw new SecurityException("Caller does not have permission");
3491 }
3492 final long identity = Binder.clearCallingIdentity();
3493 try {
3494 for (Phone phone : PhoneFactory.getPhones()) {
3495 if (phone.getImeiType() == Phone.IMEI_TYPE_PRIMARY) {
3496 return phone.getImei();
3497 }
3498 }
3499 throw new UnsupportedOperationException("Operation not supported");
3500 } finally {
3501 Binder.restoreCallingIdentity(identity);
3502 }
3503 }
3504
3505 @Override
David Kelly5e06a7f2018-03-12 14:10:59 +00003506 public String getTypeAllocationCodeForSlot(int slotIndex) {
3507 Phone phone = PhoneFactory.getPhone(slotIndex);
3508 String tac = null;
3509 if (phone != null) {
3510 String imei = phone.getImei();
Vala Zadehab005552021-09-21 15:54:29 -07003511 try {
3512 tac = imei == null ? null : imei.substring(0, TYPE_ALLOCATION_CODE_LENGTH);
3513 } catch (IndexOutOfBoundsException e) {
3514 Log.e(LOG_TAG, "IMEI length shorter than upper index.");
3515 return null;
3516 }
David Kelly5e06a7f2018-03-12 14:10:59 +00003517 }
3518 return tac;
3519 }
3520
3521 @Override
Philip P. Moltmann700a9592019-10-03 11:53:50 -07003522 public String getMeidForSlot(int slotIndex, String callingPackage, String callingFeatureId) {
Shuo Qian13d89152021-05-10 23:58:11 -07003523 try {
3524 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
3525 } catch (SecurityException se) {
3526 EventLog.writeEvent(0x534e4554, "186530496", Binder.getCallingUid());
3527 throw new SecurityException("Package " + callingPackage + " does not belong to "
3528 + Binder.getCallingUid());
3529 }
Jeff Davidson913390f2018-02-23 17:11:49 -08003530 Phone phone = PhoneFactory.getPhone(slotIndex);
3531 if (phone == null) {
Jack Yu2af8d712017-03-15 17:14:14 -07003532 return null;
3533 }
Nazanin Bakhshif782e562018-12-11 15:15:39 -08003534
Jeff Davidson913390f2018-02-23 17:11:49 -08003535 int subId = phone.getSubId();
Michael Groover70af6dc2018-10-01 16:23:15 -07003536 if (!TelephonyPermissions.checkCallingOrSelfReadDeviceIdentifiers(mApp, subId,
Philip P. Moltmann700a9592019-10-03 11:53:50 -07003537 callingPackage, callingFeatureId, "getMeidForSlot")) {
Jeff Davidson913390f2018-02-23 17:11:49 -08003538 return null;
3539 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003540
3541 final long identity = Binder.clearCallingIdentity();
3542 try {
3543 return phone.getMeid();
3544 } finally {
3545 Binder.restoreCallingIdentity(identity);
3546 }
Jack Yu2af8d712017-03-15 17:14:14 -07003547 }
3548
3549 @Override
David Kelly5e06a7f2018-03-12 14:10:59 +00003550 public String getManufacturerCodeForSlot(int slotIndex) {
3551 Phone phone = PhoneFactory.getPhone(slotIndex);
3552 String manufacturerCode = null;
3553 if (phone != null) {
3554 String meid = phone.getMeid();
Vala Zadehab005552021-09-21 15:54:29 -07003555 try {
3556 manufacturerCode =
3557 meid == null ? null : meid.substring(0, MANUFACTURER_CODE_LENGTH);
3558 } catch (IndexOutOfBoundsException e) {
3559 Log.e(LOG_TAG, "MEID length shorter than upper index.");
3560 return null;
3561 }
David Kelly5e06a7f2018-03-12 14:10:59 +00003562 }
3563 return manufacturerCode;
3564 }
3565
3566 @Override
Philip P. Moltmann700a9592019-10-03 11:53:50 -07003567 public String getDeviceSoftwareVersionForSlot(int slotIndex, String callingPackage,
3568 String callingFeatureId) {
Jeff Davidson913390f2018-02-23 17:11:49 -08003569 Phone phone = PhoneFactory.getPhone(slotIndex);
3570 if (phone == null) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08003571 return null;
3572 }
Jeff Davidson913390f2018-02-23 17:11:49 -08003573 int subId = phone.getSubId();
3574 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
Philip P. Moltmann700a9592019-10-03 11:53:50 -07003575 mApp, subId, callingPackage, callingFeatureId,
3576 "getDeviceSoftwareVersionForSlot")) {
Jeff Davidson913390f2018-02-23 17:11:49 -08003577 return null;
3578 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003579
3580 final long identity = Binder.clearCallingIdentity();
3581 try {
3582 return phone.getDeviceSvn();
3583 } finally {
3584 Binder.restoreCallingIdentity(identity);
3585 }
Shishir Agrawala9f32182016-04-12 12:00:16 -07003586 }
3587
fionaxu43304da2017-11-27 22:51:16 -08003588 @Override
3589 public int getSubscriptionCarrierId(int subId) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003590 final long identity = Binder.clearCallingIdentity();
3591 try {
3592 final Phone phone = getPhone(subId);
3593 return phone == null ? TelephonyManager.UNKNOWN_CARRIER_ID : phone.getCarrierId();
3594 } finally {
3595 Binder.restoreCallingIdentity(identity);
3596 }
fionaxu43304da2017-11-27 22:51:16 -08003597 }
3598
3599 @Override
3600 public String getSubscriptionCarrierName(int subId) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003601 final long identity = Binder.clearCallingIdentity();
3602 try {
3603 final Phone phone = getPhone(subId);
3604 return phone == null ? null : phone.getCarrierName();
3605 } finally {
3606 Binder.restoreCallingIdentity(identity);
3607 }
fionaxu43304da2017-11-27 22:51:16 -08003608 }
3609
calvinpanffe225e2018-11-01 19:43:06 +08003610 @Override
chen xu0026ca62019-03-06 15:28:50 -08003611 public int getSubscriptionSpecificCarrierId(int subId) {
chen xu25637222018-11-04 17:17:00 -08003612 final long identity = Binder.clearCallingIdentity();
3613 try {
3614 final Phone phone = getPhone(subId);
3615 return phone == null ? TelephonyManager.UNKNOWN_CARRIER_ID
chen xu0026ca62019-03-06 15:28:50 -08003616 : phone.getSpecificCarrierId();
chen xu25637222018-11-04 17:17:00 -08003617 } finally {
3618 Binder.restoreCallingIdentity(identity);
3619 }
3620 }
3621
3622 @Override
chen xu0026ca62019-03-06 15:28:50 -08003623 public String getSubscriptionSpecificCarrierName(int subId) {
chen xu25637222018-11-04 17:17:00 -08003624 final long identity = Binder.clearCallingIdentity();
3625 try {
3626 final Phone phone = getPhone(subId);
chen xu0026ca62019-03-06 15:28:50 -08003627 return phone == null ? null : phone.getSpecificCarrierName();
chen xu25637222018-11-04 17:17:00 -08003628 } finally {
3629 Binder.restoreCallingIdentity(identity);
3630 }
3631 }
3632
chen xu651eec72018-11-11 19:03:44 -08003633 @Override
chen xu864e11c2018-12-06 22:10:03 -08003634 public int getCarrierIdFromMccMnc(int slotIndex, String mccmnc, boolean isSubscriptionMccMnc) {
3635 if (!isSubscriptionMccMnc) {
3636 enforceReadPrivilegedPermission("getCarrierIdFromMccMnc");
3637 }
chen xu651eec72018-11-11 19:03:44 -08003638 final Phone phone = PhoneFactory.getPhone(slotIndex);
3639 if (phone == null) {
3640 return TelephonyManager.UNKNOWN_CARRIER_ID;
3641 }
3642 final long identity = Binder.clearCallingIdentity();
3643 try {
3644 return CarrierResolver.getCarrierIdFromMccMnc(phone.getContext(), mccmnc);
3645 } finally {
3646 Binder.restoreCallingIdentity(identity);
3647 }
3648 }
3649
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003650 //
3651 // Internal helper methods.
3652 //
3653
Sanket Padaweee13a9b2016-03-08 17:30:28 -08003654 /**
Grace Jia0ddb3612021-04-22 13:35:26 -07003655 * Make sure the caller is the calling package itself
3656 *
3657 * @throws SecurityException if the caller is not the calling package
3658 */
3659 private void enforceCallingPackage(String callingPackage, int callingUid, String message) {
3660 int packageUid = -1;
Grace Jiadbefca02021-04-26 15:13:31 -07003661 PackageManager pm = mApp.getBaseContext().createContextAsUser(
3662 UserHandle.getUserHandleForUid(callingUid), 0).getPackageManager();
Grace Jia0ddb3612021-04-22 13:35:26 -07003663 try {
Grace Jiadbefca02021-04-26 15:13:31 -07003664 packageUid = pm.getPackageUid(callingPackage, 0);
Grace Jia0ddb3612021-04-22 13:35:26 -07003665 } catch (PackageManager.NameNotFoundException e) {
3666 // packageUid is -1
3667 }
3668 if (packageUid != callingUid) {
3669 throw new SecurityException(message + ": Package " + callingPackage
3670 + " does not belong to " + callingUid);
3671 }
3672 }
3673
3674 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003675 * Make sure the caller has the MODIFY_PHONE_STATE permission.
3676 *
3677 * @throws SecurityException if the caller does not have the required permission
3678 */
Gil Cukierman1c0eb932022-12-06 22:28:24 +00003679 @VisibleForTesting
3680 public void enforceModifyPermission() {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003681 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.MODIFY_PHONE_STATE, null);
3682 }
3683
Gil Cukierman1c0eb932022-12-06 22:28:24 +00003684 /**
3685 * Make sure the caller has the MODIFY_PHONE_STATE permission.
3686 *
3687 * @throws SecurityException if the caller does not have the required permission
3688 */
3689 @VisibleForTesting
3690 public void enforceReadPermission() {
3691 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.READ_PHONE_STATE, null);
3692 }
3693
Shuo Qian3b6ee772019-11-13 17:43:31 -08003694 private void enforceActiveEmergencySessionPermission() {
3695 mApp.enforceCallingOrSelfPermission(
3696 android.Manifest.permission.READ_ACTIVE_EMERGENCY_SESSION, null);
3697 }
3698
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003699 /**
3700 * Make sure the caller has the CALL_PHONE permission.
3701 *
3702 * @throws SecurityException if the caller does not have the required permission
3703 */
3704 private void enforceCallPermission() {
3705 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.CALL_PHONE, null);
3706 }
3707
paulhu5a773602019-08-23 19:17:33 +08003708 private void enforceSettingsPermission() {
3709 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.NETWORK_SETTINGS, null);
Stuart Scott8eef64f2015-04-08 15:13:54 -07003710 }
3711
Michele Berionne5e411512020-11-13 02:36:59 +00003712 private void enforceRebootPermission() {
3713 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.REBOOT, null);
3714 }
3715
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003716 private String createTelUrl(String number) {
3717 if (TextUtils.isEmpty(number)) {
3718 return null;
3719 }
3720
Jake Hambye994d462014-02-03 13:10:13 -08003721 return "tel:" + number;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003722 }
3723
Ihab Awadf9e92732013-12-05 18:02:52 -08003724 private static void log(String msg) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003725 Log.d(LOG_TAG, "[PhoneIntfMgr] " + msg);
3726 }
3727
Naveen Kalla1fd79bd2014-08-08 00:48:59 -07003728 private static void logv(String msg) {
3729 Log.v(LOG_TAG, "[PhoneIntfMgr] " + msg);
3730 }
3731
Ihab Awadf9e92732013-12-05 18:02:52 -08003732 private static void loge(String msg) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003733 Log.e(LOG_TAG, "[PhoneIntfMgr] " + msg);
3734 }
3735
Robert Greenwalt36b23af2015-07-06 17:59:14 -07003736 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003737 public int getActivePhoneType() {
Shishir Agrawala9f32182016-04-12 12:00:16 -07003738 return getActivePhoneTypeForSlot(getSlotForDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07003739 }
3740
Sanket Padawe356d7632015-06-22 14:03:32 -07003741 @Override
Sanket Padawe13bac7b2017-03-20 15:04:47 -07003742 public int getActivePhoneTypeForSlot(int slotIndex) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003743 final long identity = Binder.clearCallingIdentity();
3744 try {
3745 final Phone phone = PhoneFactory.getPhone(slotIndex);
3746 if (phone == null) {
3747 return PhoneConstants.PHONE_TYPE_NONE;
3748 } else {
3749 return phone.getPhoneType();
3750 }
3751 } finally {
3752 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07003753 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003754 }
3755
3756 /**
3757 * Returns the CDMA ERI icon index to display
3758 */
Robert Greenwalt36b23af2015-07-06 17:59:14 -07003759 @Override
Philip P. Moltmann700a9592019-10-03 11:53:50 -07003760 public int getCdmaEriIconIndex(String callingPackage, String callingFeatureId) {
3761 return getCdmaEriIconIndexForSubscriber(getDefaultSubscription(), callingPackage,
3762 callingFeatureId);
Wink Saville36469e72014-06-11 15:17:00 -07003763 }
3764
Sanket Padawe356d7632015-06-22 14:03:32 -07003765 @Override
Philip P. Moltmann700a9592019-10-03 11:53:50 -07003766 public int getCdmaEriIconIndexForSubscriber(int subId, String callingPackage,
3767 String callingFeatureId) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08003768 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
Philip P. Moltmann700a9592019-10-03 11:53:50 -07003769 mApp, subId, callingPackage, callingFeatureId,
3770 "getCdmaEriIconIndexForSubscriber")) {
Robert Greenwalt36b23af2015-07-06 17:59:14 -07003771 return -1;
3772 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003773
3774 final long identity = Binder.clearCallingIdentity();
3775 try {
3776 final Phone phone = getPhone(subId);
3777 if (phone != null) {
3778 return phone.getCdmaEriIconIndex();
3779 } else {
3780 return -1;
3781 }
3782 } finally {
3783 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07003784 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003785 }
3786
3787 /**
3788 * Returns the CDMA ERI icon mode,
3789 * 0 - ON
3790 * 1 - FLASHING
3791 */
Robert Greenwalt36b23af2015-07-06 17:59:14 -07003792 @Override
Philip P. Moltmann700a9592019-10-03 11:53:50 -07003793 public int getCdmaEriIconMode(String callingPackage, String callingFeatureId) {
3794 return getCdmaEriIconModeForSubscriber(getDefaultSubscription(), callingPackage,
3795 callingFeatureId);
Wink Saville36469e72014-06-11 15:17:00 -07003796 }
3797
Sanket Padawe356d7632015-06-22 14:03:32 -07003798 @Override
Philip P. Moltmann700a9592019-10-03 11:53:50 -07003799 public int getCdmaEriIconModeForSubscriber(int subId, String callingPackage,
3800 String callingFeatureId) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08003801 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
Philip P. Moltmann700a9592019-10-03 11:53:50 -07003802 mApp, subId, callingPackage, callingFeatureId,
3803 "getCdmaEriIconModeForSubscriber")) {
Robert Greenwalt36b23af2015-07-06 17:59:14 -07003804 return -1;
3805 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003806
3807 final long identity = Binder.clearCallingIdentity();
3808 try {
3809 final Phone phone = getPhone(subId);
3810 if (phone != null) {
3811 return phone.getCdmaEriIconMode();
3812 } else {
3813 return -1;
3814 }
3815 } finally {
3816 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07003817 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003818 }
3819
3820 /**
3821 * Returns the CDMA ERI text,
3822 */
Robert Greenwalt36b23af2015-07-06 17:59:14 -07003823 @Override
Philip P. Moltmann700a9592019-10-03 11:53:50 -07003824 public String getCdmaEriText(String callingPackage, String callingFeatureId) {
3825 return getCdmaEriTextForSubscriber(getDefaultSubscription(), callingPackage,
3826 callingFeatureId);
Wink Saville36469e72014-06-11 15:17:00 -07003827 }
3828
Sanket Padawe356d7632015-06-22 14:03:32 -07003829 @Override
Philip P. Moltmann700a9592019-10-03 11:53:50 -07003830 public String getCdmaEriTextForSubscriber(int subId, String callingPackage,
3831 String callingFeatureId) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08003832 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
Philip P. Moltmann700a9592019-10-03 11:53:50 -07003833 mApp, subId, callingPackage, callingFeatureId,
3834 "getCdmaEriIconTextForSubscriber")) {
Robert Greenwalt36b23af2015-07-06 17:59:14 -07003835 return null;
3836 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003837
3838 final long identity = Binder.clearCallingIdentity();
3839 try {
3840 final Phone phone = getPhone(subId);
3841 if (phone != null) {
3842 return phone.getCdmaEriText();
3843 } else {
3844 return null;
3845 }
3846 } finally {
3847 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07003848 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003849 }
3850
3851 /**
Junda Liuca05d5d2014-08-14 22:36:34 -07003852 * Returns the CDMA MDN.
3853 */
Sanket Padawe356d7632015-06-22 14:03:32 -07003854 @Override
Wink Savilleb564aae2014-10-23 10:18:09 -07003855 public String getCdmaMdn(int subId) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08003856 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
3857 mApp, subId, "getCdmaMdn");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003858
3859 final long identity = Binder.clearCallingIdentity();
3860 try {
3861 final Phone phone = getPhone(subId);
Nazanin Bakhshif782e562018-12-11 15:15:39 -08003862 if (phone != null && phone.getPhoneType() == PhoneConstants.PHONE_TYPE_CDMA) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003863 return phone.getLine1Number();
3864 } else {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08003865 loge("getCdmaMdn: no phone found. Invalid subId: " + subId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003866 return null;
3867 }
3868 } finally {
3869 Binder.restoreCallingIdentity(identity);
Junda Liuca05d5d2014-08-14 22:36:34 -07003870 }
3871 }
3872
3873 /**
3874 * Returns the CDMA MIN.
3875 */
Sanket Padawe356d7632015-06-22 14:03:32 -07003876 @Override
Wink Savilleb564aae2014-10-23 10:18:09 -07003877 public String getCdmaMin(int subId) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08003878 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
3879 mApp, subId, "getCdmaMin");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003880
3881 final long identity = Binder.clearCallingIdentity();
3882 try {
3883 final Phone phone = getPhone(subId);
3884 if (phone != null && phone.getPhoneType() == PhoneConstants.PHONE_TYPE_CDMA) {
3885 return phone.getCdmaMin();
3886 } else {
3887 return null;
3888 }
3889 } finally {
3890 Binder.restoreCallingIdentity(identity);
Junda Liuca05d5d2014-08-14 22:36:34 -07003891 }
3892 }
3893
Hall Liud892bec2018-11-30 14:51:45 -08003894 @Override
3895 public void requestNumberVerification(PhoneNumberRange range, long timeoutMillis,
3896 INumberVerificationCallback callback, String callingPackage) {
3897 if (mApp.checkCallingOrSelfPermission(android.Manifest.permission.MODIFY_PHONE_STATE)
3898 != PERMISSION_GRANTED) {
3899 throw new SecurityException("Caller must hold the MODIFY_PHONE_STATE permission");
3900 }
3901 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
3902
3903 String authorizedPackage = NumberVerificationManager.getAuthorizedPackage(mApp);
3904 if (!TextUtils.equals(callingPackage, authorizedPackage)) {
Hall Liub9d8feb2021-01-13 10:28:04 -08003905 throw new SecurityException("Calling package must be configured in the device config: "
3906 + "calling package: " + callingPackage
3907 + ", configured package: " + authorizedPackage);
Hall Liud892bec2018-11-30 14:51:45 -08003908 }
3909
3910 if (range == null) {
3911 throw new NullPointerException("Range must be non-null");
3912 }
3913
3914 timeoutMillis = Math.min(timeoutMillis,
Hall Liubd069e32019-02-28 18:56:30 -08003915 TelephonyManager.getMaxNumberVerificationTimeoutMillis());
Hall Liud892bec2018-11-30 14:51:45 -08003916
3917 NumberVerificationManager.getInstance().requestVerification(range, callback, timeoutMillis);
3918 }
3919
Junda Liuca05d5d2014-08-14 22:36:34 -07003920 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003921 * Returns true if CDMA provisioning needs to run.
3922 */
3923 public boolean needsOtaServiceProvisioning() {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003924 final long identity = Binder.clearCallingIdentity();
3925 try {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08003926 return getDefaultPhone().needsOtaServiceProvisioning();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003927 } finally {
3928 Binder.restoreCallingIdentity(identity);
3929 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003930 }
3931
3932 /**
Shishir Agrawal76d5da92014-11-09 16:17:25 -08003933 * Sets the voice mail number of a given subId.
3934 */
3935 @Override
3936 public boolean setVoiceMailNumber(int subId, String alphaTag, String number) {
Shuo Qian2c0ae432019-12-05 11:40:37 -08003937 TelephonyPermissions.enforceCallingOrSelfCarrierPrivilege(
3938 mApp, subId, "setVoiceMailNumber");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003939
3940 final long identity = Binder.clearCallingIdentity();
3941 try {
3942 Boolean success = (Boolean) sendRequest(CMD_SET_VOICEMAIL_NUMBER,
3943 new Pair<String, String>(alphaTag, number), new Integer(subId));
3944 return success;
3945 } finally {
3946 Binder.restoreCallingIdentity(identity);
3947 }
Shishir Agrawal76d5da92014-11-09 16:17:25 -08003948 }
3949
Ta-wei Yen87c49842016-05-13 21:19:52 -07003950 @Override
Ta-wei Yenc9df0432017-04-17 17:09:07 -07003951 public Bundle getVisualVoicemailSettings(String callingPackage, int subId) {
3952 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
Tyler Gunn5ddfdc92019-10-31 13:08:23 -07003953 TelecomManager tm = mApp.getSystemService(TelecomManager.class);
3954 String systemDialer = tm.getSystemDialerPackage();
Ta-wei Yenc9df0432017-04-17 17:09:07 -07003955 if (!TextUtils.equals(callingPackage, systemDialer)) {
3956 throw new SecurityException("caller must be system dialer");
3957 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003958
3959 final long identity = Binder.clearCallingIdentity();
3960 try {
3961 PhoneAccountHandle phoneAccountHandle = PhoneAccountHandleConverter.fromSubId(subId);
3962 if (phoneAccountHandle == null) {
3963 return null;
3964 }
Nazanin Bakhshif782e562018-12-11 15:15:39 -08003965 return VisualVoicemailSettingsUtil.dump(mApp, phoneAccountHandle);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003966 } finally {
3967 Binder.restoreCallingIdentity(identity);
Ta-wei Yenc9df0432017-04-17 17:09:07 -07003968 }
Ta-wei Yenc9df0432017-04-17 17:09:07 -07003969 }
3970
3971 @Override
Philip P. Moltmann700a9592019-10-03 11:53:50 -07003972 public String getVisualVoicemailPackageName(String callingPackage, String callingFeatureId,
3973 int subId) {
Ta-wei Yendca928f2017-01-10 16:17:08 -08003974 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
Jeff Davidson7e17e312018-02-13 18:17:36 -08003975 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
Philip P. Moltmann700a9592019-10-03 11:53:50 -07003976 mApp, subId, callingPackage, callingFeatureId,
3977 "getVisualVoicemailPackageName")) {
Ta-wei Yendca928f2017-01-10 16:17:08 -08003978 return null;
3979 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003980
Jeff Davidsona8e4e242018-03-15 17:16:18 -07003981 final long identity = Binder.clearCallingIdentity();
3982 try {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08003983 return RemoteVvmTaskManager.getRemotePackage(mApp, subId).getPackageName();
Jeff Davidsona8e4e242018-03-15 17:16:18 -07003984 } finally {
3985 Binder.restoreCallingIdentity(identity);
3986 }
Ta-wei Yendca928f2017-01-10 16:17:08 -08003987 }
3988
3989 @Override
Ta-wei Yenb6929602016-05-24 15:48:27 -07003990 public void enableVisualVoicemailSmsFilter(String callingPackage, int subId,
3991 VisualVoicemailSmsFilterSettings settings) {
3992 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003993
3994 final long identity = Binder.clearCallingIdentity();
3995 try {
3996 VisualVoicemailSmsFilterConfig.enableVisualVoicemailSmsFilter(
Nazanin Bakhshif782e562018-12-11 15:15:39 -08003997 mApp, callingPackage, subId, settings);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003998 } finally {
3999 Binder.restoreCallingIdentity(identity);
4000 }
Ta-wei Yen87c49842016-05-13 21:19:52 -07004001 }
4002
4003 @Override
Ta-wei Yenb6929602016-05-24 15:48:27 -07004004 public void disableVisualVoicemailSmsFilter(String callingPackage, int subId) {
4005 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004006
4007 final long identity = Binder.clearCallingIdentity();
4008 try {
4009 VisualVoicemailSmsFilterConfig.disableVisualVoicemailSmsFilter(
Nazanin Bakhshif782e562018-12-11 15:15:39 -08004010 mApp, callingPackage, subId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004011 } finally {
4012 Binder.restoreCallingIdentity(identity);
4013 }
Ta-wei Yen87c49842016-05-13 21:19:52 -07004014 }
4015
4016 @Override
Ta-wei Yenb6929602016-05-24 15:48:27 -07004017 public VisualVoicemailSmsFilterSettings getVisualVoicemailSmsFilterSettings(
4018 String callingPackage, int subId) {
4019 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004020
4021 final long identity = Binder.clearCallingIdentity();
4022 try {
4023 return VisualVoicemailSmsFilterConfig.getVisualVoicemailSmsFilterSettings(
Nazanin Bakhshif782e562018-12-11 15:15:39 -08004024 mApp, callingPackage, subId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004025 } finally {
4026 Binder.restoreCallingIdentity(identity);
4027 }
Ta-wei Yen87c49842016-05-13 21:19:52 -07004028 }
4029
4030 @Override
Ta-wei Yen30a69c82016-12-27 14:52:32 -08004031 public VisualVoicemailSmsFilterSettings getActiveVisualVoicemailSmsFilterSettings(int subId) {
Brad Ebinger35c841c2018-10-01 10:40:55 -07004032 enforceReadPrivilegedPermission("getActiveVisualVoicemailSmsFilterSettings");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004033
4034 final long identity = Binder.clearCallingIdentity();
4035 try {
4036 return VisualVoicemailSmsFilterConfig.getActiveVisualVoicemailSmsFilterSettings(
Nazanin Bakhshif782e562018-12-11 15:15:39 -08004037 mApp, subId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004038 } finally {
4039 Binder.restoreCallingIdentity(identity);
4040 }
Ta-wei Yen30a69c82016-12-27 14:52:32 -08004041 }
4042
4043 @Override
Philip P. Moltmann2f6f8ce2020-03-18 18:17:02 -07004044 public void sendVisualVoicemailSmsForSubscriber(String callingPackage,
4045 String callingAttributionTag, int subId, String number, int port, String text,
4046 PendingIntent sentIntent) {
Ta-wei Yen30a69c82016-12-27 14:52:32 -08004047 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
Ta-wei Yen527a9c02017-01-06 15:29:25 -08004048 enforceVisualVoicemailPackage(callingPackage, subId);
Ta-wei Yen30a69c82016-12-27 14:52:32 -08004049 enforceSendSmsPermission();
Amit Mahajandccb3f12019-05-13 13:48:32 -07004050 SmsController smsController = PhoneFactory.getSmsController();
Philip P. Moltmann2f6f8ce2020-03-18 18:17:02 -07004051 smsController.sendVisualVoicemailSmsForSubscriber(callingPackage, callingAttributionTag,
4052 subId, number, port, text, sentIntent);
Ta-wei Yen87c49842016-05-13 21:19:52 -07004053 }
Amit Mahajandccb3f12019-05-13 13:48:32 -07004054
Shishir Agrawal76d5da92014-11-09 16:17:25 -08004055 /**
fionaxu0152e512016-11-14 13:36:14 -08004056 * Sets the voice activation state of a given subId.
4057 */
4058 @Override
4059 public void setVoiceActivationState(int subId, int activationState) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08004060 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
4061 mApp, subId, "setVoiceActivationState");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004062
4063 final long identity = Binder.clearCallingIdentity();
4064 try {
4065 final Phone phone = getPhone(subId);
4066 if (phone != null) {
4067 phone.setVoiceActivationState(activationState);
4068 } else {
4069 loge("setVoiceActivationState fails with invalid subId: " + subId);
4070 }
4071 } finally {
4072 Binder.restoreCallingIdentity(identity);
fionaxu0152e512016-11-14 13:36:14 -08004073 }
4074 }
4075
4076 /**
4077 * Sets the data activation state of a given subId.
4078 */
4079 @Override
4080 public void setDataActivationState(int subId, int activationState) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08004081 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
4082 mApp, subId, "setDataActivationState");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004083
4084 final long identity = Binder.clearCallingIdentity();
4085 try {
4086 final Phone phone = getPhone(subId);
4087 if (phone != null) {
4088 phone.setDataActivationState(activationState);
4089 } else {
Taesu Leef8fbed92019-10-07 18:47:02 +09004090 loge("setDataActivationState fails with invalid subId: " + subId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004091 }
4092 } finally {
4093 Binder.restoreCallingIdentity(identity);
fionaxu0152e512016-11-14 13:36:14 -08004094 }
4095 }
4096
4097 /**
4098 * Returns the voice activation state of a given subId.
4099 */
4100 @Override
4101 public int getVoiceActivationState(int subId, String callingPackage) {
Brad Ebinger35c841c2018-10-01 10:40:55 -07004102 enforceReadPrivilegedPermission("getVoiceActivationState");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004103
fionaxu0152e512016-11-14 13:36:14 -08004104 final Phone phone = getPhone(subId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004105 final long identity = Binder.clearCallingIdentity();
4106 try {
4107 if (phone != null) {
4108 return phone.getVoiceActivationState();
4109 } else {
4110 return TelephonyManager.SIM_ACTIVATION_STATE_UNKNOWN;
4111 }
4112 } finally {
4113 Binder.restoreCallingIdentity(identity);
fionaxu0152e512016-11-14 13:36:14 -08004114 }
4115 }
4116
4117 /**
4118 * Returns the data activation state of a given subId.
4119 */
4120 @Override
4121 public int getDataActivationState(int subId, String callingPackage) {
Brad Ebinger35c841c2018-10-01 10:40:55 -07004122 enforceReadPrivilegedPermission("getDataActivationState");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004123
fionaxu0152e512016-11-14 13:36:14 -08004124 final Phone phone = getPhone(subId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004125 final long identity = Binder.clearCallingIdentity();
4126 try {
4127 if (phone != null) {
4128 return phone.getDataActivationState();
4129 } else {
4130 return TelephonyManager.SIM_ACTIVATION_STATE_UNKNOWN;
4131 }
4132 } finally {
4133 Binder.restoreCallingIdentity(identity);
fionaxu0152e512016-11-14 13:36:14 -08004134 }
4135 }
4136
4137 /**
Wink Saville36469e72014-06-11 15:17:00 -07004138 * Returns the unread count of voicemails for a subId
4139 */
Sanket Padawe356d7632015-06-22 14:03:32 -07004140 @Override
Philip P. Moltmann700a9592019-10-03 11:53:50 -07004141 public int getVoiceMessageCountForSubscriber(int subId, String callingPackage,
4142 String callingFeatureId) {
Brad Ebingerf7664ba2018-11-29 12:43:38 -08004143 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
Philip P. Moltmann700a9592019-10-03 11:53:50 -07004144 mApp, subId, callingPackage, callingFeatureId,
4145 "getVoiceMessageCountForSubscriber")) {
Brad Ebingerf7664ba2018-11-29 12:43:38 -08004146 return 0;
4147 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004148 final long identity = Binder.clearCallingIdentity();
4149 try {
4150 final Phone phone = getPhone(subId);
4151 if (phone != null) {
4152 return phone.getVoiceMessageCount();
4153 } else {
4154 return 0;
4155 }
4156 } finally {
4157 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07004158 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07004159 }
4160
4161 /**
pkanwar8a4dcfb2017-01-19 13:43:16 -08004162 * returns true, if the device is in a state where both voice and data
4163 * are supported simultaneously. This can change based on location or network condition.
4164 */
4165 @Override
4166 public boolean isConcurrentVoiceAndDataAllowed(int subId) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004167 final long identity = Binder.clearCallingIdentity();
4168 try {
4169 final Phone phone = getPhone(subId);
4170 return (phone == null ? false : phone.isConcurrentVoiceAndDataAllowed());
4171 } finally {
4172 Binder.restoreCallingIdentity(identity);
4173 }
pkanwar8a4dcfb2017-01-19 13:43:16 -08004174 }
4175
4176 /**
fionaxu235cc5e2017-03-06 22:25:57 -08004177 * Send the dialer code if called from the current default dialer or the caller has
4178 * carrier privilege.
4179 * @param inputCode The dialer code to send
4180 */
4181 @Override
4182 public void sendDialerSpecialCode(String callingPackage, String inputCode) {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08004183 final Phone defaultPhone = getDefaultPhone();
fionaxu235cc5e2017-03-06 22:25:57 -08004184 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
Tyler Gunn5ddfdc92019-10-31 13:08:23 -07004185 TelecomManager tm = defaultPhone.getContext().getSystemService(TelecomManager.class);
4186 String defaultDialer = tm.getDefaultDialerPackage();
fionaxu235cc5e2017-03-06 22:25:57 -08004187 if (!TextUtils.equals(callingPackage, defaultDialer)) {
Shuo Qian2c0ae432019-12-05 11:40:37 -08004188 TelephonyPermissions.enforceCallingOrSelfCarrierPrivilege(mApp,
Jeff Davidson7e17e312018-02-13 18:17:36 -08004189 getDefaultSubscription(), "sendDialerSpecialCode");
fionaxu235cc5e2017-03-06 22:25:57 -08004190 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004191
4192 final long identity = Binder.clearCallingIdentity();
4193 try {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08004194 defaultPhone.sendDialerSpecialCode(inputCode);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004195 } finally {
4196 Binder.restoreCallingIdentity(identity);
4197 }
fionaxu235cc5e2017-03-06 22:25:57 -08004198 }
4199
Pengquan Menga1bb6272018-09-06 09:59:22 -07004200 @Override
4201 public int getNetworkSelectionMode(int subId) {
shilufc958392020-01-20 11:36:01 -08004202 TelephonyPermissions
Nathan Harold62c68512021-04-06 11:26:02 -07004203 .enforceCallingOrSelfReadPrecisePhoneStatePermissionOrCarrierPrivilege(
shilufc958392020-01-20 11:36:01 -08004204 mApp, subId, "getNetworkSelectionMode");
4205 final long identity = Binder.clearCallingIdentity();
4206 try {
4207 if (!isActiveSubscription(subId)) {
4208 return TelephonyManager.NETWORK_SELECTION_MODE_UNKNOWN;
4209 }
4210 return (int) sendRequest(CMD_GET_NETWORK_SELECTION_MODE, null /* argument */, subId);
4211 } finally {
4212 Binder.restoreCallingIdentity(identity);
Pengquan Menge92a50d2018-09-21 15:54:48 -07004213 }
Pengquan Menga1bb6272018-09-06 09:59:22 -07004214 }
4215
Brad Ebinger35c841c2018-10-01 10:40:55 -07004216 @Override
Brad Ebingerb2b65522019-03-15 13:48:47 -07004217 public boolean isInEmergencySmsMode() {
4218 enforceReadPrivilegedPermission("isInEmergencySmsMode");
4219 final long identity = Binder.clearCallingIdentity();
4220 try {
4221 for (Phone phone : PhoneFactory.getPhones()) {
4222 if (phone.isInEmergencySmsMode()) {
4223 return true;
4224 }
4225 }
4226 } finally {
4227 Binder.restoreCallingIdentity(identity);
4228 }
4229 return false;
4230 }
4231
shilu366312e2019-12-17 09:28:10 -08004232 /**
4233 * Requires carrier privileges or READ_PRECISE_PHONE_STATE permission.
4234 * @param subId The subscription to use to check the configuration.
4235 * @param c The callback that will be used to send the result.
4236 */
Brad Ebingerb2b65522019-03-15 13:48:47 -07004237 @Override
Brad Ebinger9878b0b2018-11-08 17:43:22 -08004238 public void registerImsRegistrationCallback(int subId, IImsRegistrationCallback c)
4239 throws RemoteException {
Nathan Harold62c68512021-04-06 11:26:02 -07004240 TelephonyPermissions.enforceCallingOrSelfReadPrecisePhoneStatePermissionOrCarrierPrivilege(
Rambo Wang37f9c242020-02-10 14:45:28 -08004241 mApp, subId, "registerImsRegistrationCallback");
Brad Ebingera2628302022-02-18 03:44:55 +00004242
4243 if (!ImsManager.isImsSupportedOnDevice(mApp)) {
4244 throw new ServiceSpecificException(ImsException.CODE_ERROR_UNSUPPORTED_OPERATION,
4245 "IMS not available on device.");
4246 }
Brad Ebinger35c841c2018-10-01 10:40:55 -07004247 final long token = Binder.clearCallingIdentity();
4248 try {
Brad Ebingera2628302022-02-18 03:44:55 +00004249 int slotId = getSlotIndexOrException(subId);
4250 verifyImsMmTelConfiguredOrThrow(slotId);
joonhunshin49f0aed2022-08-05 08:33:05 +00004251
4252 ImsStateCallbackController controller = ImsStateCallbackController.getInstance();
4253 if (controller != null) {
4254 ImsManager imsManager = controller.getImsManager(subId);
4255 if (imsManager != null) {
4256 imsManager.addRegistrationCallbackForSubscription(c, subId);
4257 } else {
4258 throw new ServiceSpecificException(ImsException.CODE_ERROR_SERVICE_UNAVAILABLE);
4259 }
4260 } else {
4261 throw new ServiceSpecificException(ImsException.CODE_ERROR_UNSUPPORTED_OPERATION);
4262 }
Brad Ebinger1ce9c432019-07-16 13:19:44 -07004263 } catch (ImsException e) {
4264 throw new ServiceSpecificException(e.getCode());
Brad Ebinger35c841c2018-10-01 10:40:55 -07004265 } finally {
4266 Binder.restoreCallingIdentity(token);
4267 }
4268 }
4269
shilu366312e2019-12-17 09:28:10 -08004270 /**
4271 * Requires carrier privileges or READ_PRECISE_PHONE_STATE permission.
4272 * @param subId The subscription to use to check the configuration.
4273 * @param c The callback that will be used to send the result.
4274 */
Brad Ebinger35c841c2018-10-01 10:40:55 -07004275 @Override
Brad Ebinger9878b0b2018-11-08 17:43:22 -08004276 public void unregisterImsRegistrationCallback(int subId, IImsRegistrationCallback c) {
Nathan Harold62c68512021-04-06 11:26:02 -07004277 TelephonyPermissions.enforceCallingOrSelfReadPrecisePhoneStatePermissionOrCarrierPrivilege(
Rambo Wang37f9c242020-02-10 14:45:28 -08004278 mApp, subId, "unregisterImsRegistrationCallback");
Brad Ebinger4ae57f92019-01-09 16:51:30 -08004279 if (!SubscriptionManager.isValidSubscriptionId(subId)) {
4280 throw new IllegalArgumentException("Invalid Subscription ID: " + subId);
4281 }
Meng Wangafbc5852019-09-19 17:37:13 -07004282 final long token = Binder.clearCallingIdentity();
joonhunshin49f0aed2022-08-05 08:33:05 +00004283
Meng Wangafbc5852019-09-19 17:37:13 -07004284 try {
joonhunshin49f0aed2022-08-05 08:33:05 +00004285 ImsStateCallbackController controller = ImsStateCallbackController.getInstance();
4286 if (controller != null) {
4287 ImsManager imsManager = controller.getImsManager(subId);
4288 if (imsManager != null) {
4289 imsManager.removeRegistrationCallbackForSubscription(c, subId);
4290 } else {
4291 Log.i(LOG_TAG, "unregisterImsRegistrationCallback: " + subId
4292 + "is inactive, ignoring unregister.");
4293 // If the ImsManager is not valid, just return, since the callback
4294 // will already have been removed internally.
4295 }
4296 }
Meng Wangafbc5852019-09-19 17:37:13 -07004297 } finally {
4298 Binder.restoreCallingIdentity(token);
4299 }
Brad Ebinger35c841c2018-10-01 10:40:55 -07004300 }
4301
Brad Ebingera34a6c22019-10-22 17:36:18 -07004302 /**
4303 * Get the IMS service registration state for the MmTelFeature associated with this sub id.
4304 */
4305 @Override
4306 public void getImsMmTelRegistrationState(int subId, IIntegerConsumer consumer) {
4307 enforceReadPrivilegedPermission("getImsMmTelRegistrationState");
4308 if (!ImsManager.isImsSupportedOnDevice(mApp)) {
4309 throw new ServiceSpecificException(ImsException.CODE_ERROR_UNSUPPORTED_OPERATION,
4310 "IMS not available on device.");
4311 }
4312 final long token = Binder.clearCallingIdentity();
4313 try {
4314 Phone phone = getPhone(subId);
4315 if (phone == null) {
4316 Log.w(LOG_TAG, "getImsMmTelRegistrationState: called with an invalid subscription '"
4317 + subId + "'");
4318 throw new ServiceSpecificException(ImsException.CODE_ERROR_INVALID_SUBSCRIPTION);
4319 }
4320 phone.getImsRegistrationState(regState -> {
4321 try {
4322 consumer.accept((regState == null)
4323 ? RegistrationManager.REGISTRATION_STATE_NOT_REGISTERED : regState);
4324 } catch (RemoteException e) {
4325 // Ignore if the remote process is no longer available to call back.
4326 Log.w(LOG_TAG, "getImsMmTelRegistrationState: callback not available.");
4327 }
4328 });
4329 } finally {
4330 Binder.restoreCallingIdentity(token);
4331 }
4332 }
4333
4334 /**
4335 * Get the transport type for the IMS service registration state.
4336 */
4337 @Override
4338 public void getImsMmTelRegistrationTransportType(int subId, IIntegerConsumer consumer) {
Nathan Harold62c68512021-04-06 11:26:02 -07004339 TelephonyPermissions.enforceCallingOrSelfReadPrecisePhoneStatePermissionOrCarrierPrivilege(
Rambo Wang37f9c242020-02-10 14:45:28 -08004340 mApp, subId, "getImsMmTelRegistrationTransportType");
Brad Ebingera34a6c22019-10-22 17:36:18 -07004341 if (!ImsManager.isImsSupportedOnDevice(mApp)) {
4342 throw new ServiceSpecificException(ImsException.CODE_ERROR_UNSUPPORTED_OPERATION,
4343 "IMS not available on device.");
4344 }
4345 final long token = Binder.clearCallingIdentity();
4346 try {
4347 Phone phone = getPhone(subId);
4348 if (phone == null) {
4349 Log.w(LOG_TAG, "getImsMmTelRegistrationState: called with an invalid subscription '"
4350 + subId + "'");
4351 throw new ServiceSpecificException(ImsException.CODE_ERROR_INVALID_SUBSCRIPTION);
4352 }
4353 phone.getImsRegistrationTech(regTech -> {
4354 // Convert registration tech from ImsRegistrationImplBase -> RegistrationManager
4355 int regTechConverted = (regTech == null)
4356 ? ImsRegistrationImplBase.REGISTRATION_TECH_NONE : regTech;
4357 regTechConverted = RegistrationManager.IMS_REG_TO_ACCESS_TYPE_MAP.get(
4358 regTechConverted);
4359 try {
4360 consumer.accept(regTechConverted);
4361 } catch (RemoteException e) {
4362 // Ignore if the remote process is no longer available to call back.
4363 Log.w(LOG_TAG, "getImsMmTelRegistrationState: callback not available.");
4364 }
4365 });
4366 } finally {
4367 Binder.restoreCallingIdentity(token);
4368 }
4369 }
4370
shilu366312e2019-12-17 09:28:10 -08004371 /**
4372 * Requires carrier privileges or READ_PRECISE_PHONE_STATE permission.
4373 * @param subId The subscription to use to check the configuration.
4374 * @param c The callback that will be used to send the result.
4375 */
Brad Ebinger35c841c2018-10-01 10:40:55 -07004376 @Override
Brad Ebinger9878b0b2018-11-08 17:43:22 -08004377 public void registerMmTelCapabilityCallback(int subId, IImsCapabilityCallback c)
4378 throws RemoteException {
Nathan Harold62c68512021-04-06 11:26:02 -07004379 TelephonyPermissions.enforceCallingOrSelfReadPrecisePhoneStatePermissionOrCarrierPrivilege(
Rambo Wang37f9c242020-02-10 14:45:28 -08004380 mApp, subId, "registerMmTelCapabilityCallback");
Brad Ebingera2628302022-02-18 03:44:55 +00004381 if (!ImsManager.isImsSupportedOnDevice(mApp)) {
4382 throw new ServiceSpecificException(ImsException.CODE_ERROR_UNSUPPORTED_OPERATION,
4383 "IMS not available on device.");
4384 }
Brad Ebinger35c841c2018-10-01 10:40:55 -07004385 final long token = Binder.clearCallingIdentity();
4386 try {
Brad Ebingera2628302022-02-18 03:44:55 +00004387 int slotId = getSlotIndexOrException(subId);
4388 verifyImsMmTelConfiguredOrThrow(slotId);
4389 ImsManager.getInstance(mApp, slotId).addCapabilitiesCallbackForSubscription(c, subId);
Brad Ebinger1ce9c432019-07-16 13:19:44 -07004390 } catch (ImsException e) {
4391 throw new ServiceSpecificException(e.getCode());
Brad Ebinger35c841c2018-10-01 10:40:55 -07004392 } finally {
4393 Binder.restoreCallingIdentity(token);
4394 }
4395 }
4396
shilu366312e2019-12-17 09:28:10 -08004397 /**
4398 * Requires carrier privileges or READ_PRECISE_PHONE_STATE permission.
4399 * @param subId The subscription to use to check the configuration.
4400 * @param c The callback that will be used to send the result.
4401 */
Brad Ebinger35c841c2018-10-01 10:40:55 -07004402 @Override
Brad Ebinger9878b0b2018-11-08 17:43:22 -08004403 public void unregisterMmTelCapabilityCallback(int subId, IImsCapabilityCallback c) {
Nathan Harold62c68512021-04-06 11:26:02 -07004404 TelephonyPermissions.enforceCallingOrSelfReadPrecisePhoneStatePermissionOrCarrierPrivilege(
Rambo Wang37f9c242020-02-10 14:45:28 -08004405 mApp, subId, "unregisterMmTelCapabilityCallback");
Brad Ebinger4ae57f92019-01-09 16:51:30 -08004406 if (!SubscriptionManager.isValidSubscriptionId(subId)) {
4407 throw new IllegalArgumentException("Invalid Subscription ID: " + subId);
4408 }
Meng Wangafbc5852019-09-19 17:37:13 -07004409
4410 final long token = Binder.clearCallingIdentity();
4411 try {
Meng Wangafbc5852019-09-19 17:37:13 -07004412 ImsManager.getInstance(mApp, getSlotIndexOrException(subId))
Brad Ebinger4ae57f92019-01-09 16:51:30 -08004413 .removeCapabilitiesCallbackForSubscription(c, subId);
Meng Wangafbc5852019-09-19 17:37:13 -07004414 } catch (ImsException e) {
4415 Log.i(LOG_TAG, "unregisterMmTelCapabilityCallback: " + subId
4416 + "is inactive, ignoring unregister.");
4417 // If the subscription is no longer active, just return, since the callback
4418 // will already have been removed internally.
4419 } finally {
4420 Binder.restoreCallingIdentity(token);
4421 }
Brad Ebinger35c841c2018-10-01 10:40:55 -07004422 }
4423
4424 @Override
Brad Ebinger9878b0b2018-11-08 17:43:22 -08004425 public boolean isCapable(int subId, int capability, int regTech) {
4426 enforceReadPrivilegedPermission("isCapable");
Brad Ebinger35c841c2018-10-01 10:40:55 -07004427 final long token = Binder.clearCallingIdentity();
4428 try {
Brad Ebingera2628302022-02-18 03:44:55 +00004429 int slotId = getSlotIndexOrException(subId);
4430 verifyImsMmTelConfiguredOrThrow(slotId);
4431 return ImsManager.getInstance(mApp, slotId).queryMmTelCapability(capability, regTech);
4432 } catch (com.android.ims.ImsException e) {
4433 Log.w(LOG_TAG, "IMS isCapable - service unavailable: " + e.getMessage());
4434 return false;
Brad Ebinger1ce9c432019-07-16 13:19:44 -07004435 } catch (ImsException e) {
Brad Ebinger6b5ac222019-02-04 14:36:52 -08004436 Log.i(LOG_TAG, "isCapable: " + subId + " is inactive, returning false.");
4437 return false;
Brad Ebinger35c841c2018-10-01 10:40:55 -07004438 } finally {
4439 Binder.restoreCallingIdentity(token);
4440 }
4441 }
4442
4443 @Override
Brad Ebinger9878b0b2018-11-08 17:43:22 -08004444 public boolean isAvailable(int subId, int capability, int regTech) {
4445 enforceReadPrivilegedPermission("isAvailable");
Brad Ebinger35c841c2018-10-01 10:40:55 -07004446 final long token = Binder.clearCallingIdentity();
4447 try {
4448 Phone phone = getPhone(subId);
4449 if (phone == null) return false;
4450 return phone.isImsCapabilityAvailable(capability, regTech);
Daniel Bright5e40e4e2020-03-11 16:35:39 -07004451 } catch (com.android.ims.ImsException e) {
4452 Log.w(LOG_TAG, "IMS isAvailable - service unavailable: " + e.getMessage());
4453 return false;
Brad Ebinger35c841c2018-10-01 10:40:55 -07004454 } finally {
4455 Binder.restoreCallingIdentity(token);
4456 }
4457 }
4458
Brad Ebingerbc7dd582019-10-17 17:03:22 -07004459 /**
4460 * Determines if the MmTel feature capability is supported by the carrier configuration for this
4461 * subscription.
4462 * @param subId The subscription to use to check the configuration.
4463 * @param callback The callback that will be used to send the result.
4464 * @param capability The MmTelFeature capability that will be used to send the result.
4465 * @param transportType The transport type of the MmTelFeature capability.
4466 */
4467 @Override
4468 public void isMmTelCapabilitySupported(int subId, IIntegerConsumer callback, int capability,
4469 int transportType) {
4470 enforceReadPrivilegedPermission("isMmTelCapabilitySupported");
Brad Ebingera2628302022-02-18 03:44:55 +00004471 if (!ImsManager.isImsSupportedOnDevice(mApp)) {
4472 throw new ServiceSpecificException(ImsException.CODE_ERROR_UNSUPPORTED_OPERATION,
4473 "IMS not available on device.");
4474 }
Brad Ebingerbc7dd582019-10-17 17:03:22 -07004475 final long token = Binder.clearCallingIdentity();
4476 try {
Brad Ebingera2628302022-02-18 03:44:55 +00004477 int slotId = getSlotIndex(subId);
4478 if (slotId <= SubscriptionManager.INVALID_SIM_SLOT_INDEX) {
4479 Log.w(LOG_TAG, "isMmTelCapabilitySupported: called with an inactive subscription '"
4480 + subId + "'");
4481 throw new ServiceSpecificException(ImsException.CODE_ERROR_INVALID_SUBSCRIPTION);
4482 }
4483 verifyImsMmTelConfiguredOrThrow(slotId);
4484 ImsManager.getInstance(mApp, slotId).isSupported(capability,
4485 transportType, aBoolean -> {
4486 try {
4487 callback.accept((aBoolean == null) ? 0 : (aBoolean ? 1 : 0));
4488 } catch (RemoteException e) {
4489 Log.w(LOG_TAG, "isMmTelCapabilitySupported: remote caller is not "
4490 + "running. Ignore");
4491 }
4492 });
Brad Ebinger919631e2021-06-02 17:46:35 -07004493 } catch (ImsException e) {
4494 throw new ServiceSpecificException(e.getCode());
Brad Ebingerbc7dd582019-10-17 17:03:22 -07004495 } finally {
4496 Binder.restoreCallingIdentity(token);
4497 }
4498 }
4499
shilu366312e2019-12-17 09:28:10 -08004500 /**
4501 * Requires carrier privileges or READ_PRECISE_PHONE_STATE permission.
4502 * @param subId The subscription to use to check the configuration.
4503 */
Brad Ebinger35c841c2018-10-01 10:40:55 -07004504 @Override
4505 public boolean isAdvancedCallingSettingEnabled(int subId) {
Nathan Harold62c68512021-04-06 11:26:02 -07004506 TelephonyPermissions.enforceCallingOrSelfReadPrecisePhoneStatePermissionOrCarrierPrivilege(
Rambo Wang37f9c242020-02-10 14:45:28 -08004507 mApp, subId, "isAdvancedCallingSettingEnabled");
shilu366312e2019-12-17 09:28:10 -08004508
Brad Ebinger35c841c2018-10-01 10:40:55 -07004509 final long token = Binder.clearCallingIdentity();
4510 try {
Brad Ebinger919631e2021-06-02 17:46:35 -07004511 int slotId = getSlotIndexOrException(subId);
Brad Ebinger735c5ce2021-07-12 13:58:21 -07004512 // This setting doesn't require an active ImsService connection, so do not verify.
Brad Ebinger919631e2021-06-02 17:46:35 -07004513 return ImsManager.getInstance(mApp, slotId).isEnhanced4gLteModeSettingEnabledByUser();
Brad Ebinger1ce9c432019-07-16 13:19:44 -07004514 } catch (ImsException e) {
4515 throw new ServiceSpecificException(e.getCode());
Brad Ebinger35c841c2018-10-01 10:40:55 -07004516 } finally {
4517 Binder.restoreCallingIdentity(token);
4518 }
4519 }
4520
4521 @Override
Brad Ebinger1c162042019-02-21 14:49:10 -08004522 public void setAdvancedCallingSettingEnabled(int subId, boolean isEnabled) {
Brad Ebinger35c841c2018-10-01 10:40:55 -07004523 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(mApp, subId,
Brad Ebinger1c162042019-02-21 14:49:10 -08004524 "setAdvancedCallingSettingEnabled");
Brad Ebinger35c841c2018-10-01 10:40:55 -07004525 final long identity = Binder.clearCallingIdentity();
4526 try {
Brad Ebinger919631e2021-06-02 17:46:35 -07004527 int slotId = getSlotIndexOrException(subId);
Brad Ebinger735c5ce2021-07-12 13:58:21 -07004528 // This setting doesn't require an active ImsService connection, so do not verify. The
4529 // new setting will be picked up when the ImsService comes up next if it isn't up.
Brad Ebinger919631e2021-06-02 17:46:35 -07004530 ImsManager.getInstance(mApp, slotId).setEnhanced4gLteModeSetting(isEnabled);
Brad Ebinger1ce9c432019-07-16 13:19:44 -07004531 } catch (ImsException e) {
4532 throw new ServiceSpecificException(e.getCode());
Brad Ebinger35c841c2018-10-01 10:40:55 -07004533 } finally {
4534 Binder.restoreCallingIdentity(identity);
4535 }
4536 }
4537
shilu366312e2019-12-17 09:28:10 -08004538 /**
4539 * Requires carrier privileges or READ_PRECISE_PHONE_STATE permission.
4540 * @param subId The subscription to use to check the configuration.
4541 */
Brad Ebinger35c841c2018-10-01 10:40:55 -07004542 @Override
Brad Ebinger9878b0b2018-11-08 17:43:22 -08004543 public boolean isVtSettingEnabled(int subId) {
Nathan Harold62c68512021-04-06 11:26:02 -07004544 TelephonyPermissions.enforceCallingOrSelfReadPrecisePhoneStatePermissionOrCarrierPrivilege(
Rambo Wang37f9c242020-02-10 14:45:28 -08004545 mApp, subId, "isVtSettingEnabled");
Brad Ebinger35c841c2018-10-01 10:40:55 -07004546 final long identity = Binder.clearCallingIdentity();
4547 try {
Brad Ebinger919631e2021-06-02 17:46:35 -07004548 int slotId = getSlotIndexOrException(subId);
Brad Ebinger735c5ce2021-07-12 13:58:21 -07004549 // This setting doesn't require an active ImsService connection, so do not verify.
Brad Ebinger919631e2021-06-02 17:46:35 -07004550 return ImsManager.getInstance(mApp, slotId).isVtEnabledByUser();
Brad Ebinger1ce9c432019-07-16 13:19:44 -07004551 } catch (ImsException e) {
4552 throw new ServiceSpecificException(e.getCode());
Brad Ebinger35c841c2018-10-01 10:40:55 -07004553 } finally {
4554 Binder.restoreCallingIdentity(identity);
4555 }
4556 }
4557
4558 @Override
Brad Ebinger1c162042019-02-21 14:49:10 -08004559 public void setVtSettingEnabled(int subId, boolean isEnabled) {
Brad Ebinger35c841c2018-10-01 10:40:55 -07004560 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(mApp, subId,
Brad Ebinger1c162042019-02-21 14:49:10 -08004561 "setVtSettingEnabled");
Brad Ebinger35c841c2018-10-01 10:40:55 -07004562 final long identity = Binder.clearCallingIdentity();
4563 try {
Brad Ebinger919631e2021-06-02 17:46:35 -07004564 int slotId = getSlotIndexOrException(subId);
Brad Ebinger735c5ce2021-07-12 13:58:21 -07004565 // This setting doesn't require an active ImsService connection, so do not verify. The
4566 // new setting will be picked up when the ImsService comes up next if it isn't up.
Brad Ebinger919631e2021-06-02 17:46:35 -07004567 ImsManager.getInstance(mApp, slotId).setVtSetting(isEnabled);
Brad Ebinger1ce9c432019-07-16 13:19:44 -07004568 } catch (ImsException e) {
4569 throw new ServiceSpecificException(e.getCode());
Brad Ebinger35c841c2018-10-01 10:40:55 -07004570 } finally {
4571 Binder.restoreCallingIdentity(identity);
4572 }
4573 }
4574
shilu366312e2019-12-17 09:28:10 -08004575 /**
4576 * Requires carrier privileges or READ_PRECISE_PHONE_STATE permission.
4577 * @param subId The subscription to use to check the configuration.
4578 */
Brad Ebinger35c841c2018-10-01 10:40:55 -07004579 @Override
4580 public boolean isVoWiFiSettingEnabled(int subId) {
Nathan Harold62c68512021-04-06 11:26:02 -07004581 TelephonyPermissions.enforceCallingOrSelfReadPrecisePhoneStatePermissionOrCarrierPrivilege(
Rambo Wang37f9c242020-02-10 14:45:28 -08004582 mApp, subId, "isVoWiFiSettingEnabled");
Brad Ebinger35c841c2018-10-01 10:40:55 -07004583 final long identity = Binder.clearCallingIdentity();
4584 try {
Brad Ebinger919631e2021-06-02 17:46:35 -07004585 int slotId = getSlotIndexOrException(subId);
Brad Ebinger735c5ce2021-07-12 13:58:21 -07004586 // This setting doesn't require an active ImsService connection, so do not verify.
Brad Ebinger919631e2021-06-02 17:46:35 -07004587 return ImsManager.getInstance(mApp, slotId).isWfcEnabledByUser();
Brad Ebinger1ce9c432019-07-16 13:19:44 -07004588 } catch (ImsException e) {
4589 throw new ServiceSpecificException(e.getCode());
Brad Ebinger35c841c2018-10-01 10:40:55 -07004590 } finally {
4591 Binder.restoreCallingIdentity(identity);
4592 }
4593 }
4594
4595 @Override
Brad Ebinger1c162042019-02-21 14:49:10 -08004596 public void setVoWiFiSettingEnabled(int subId, boolean isEnabled) {
Brad Ebinger35c841c2018-10-01 10:40:55 -07004597 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(mApp, subId,
Brad Ebinger1c162042019-02-21 14:49:10 -08004598 "setVoWiFiSettingEnabled");
Brad Ebinger35c841c2018-10-01 10:40:55 -07004599 final long identity = Binder.clearCallingIdentity();
4600 try {
Brad Ebinger919631e2021-06-02 17:46:35 -07004601 int slotId = getSlotIndexOrException(subId);
Brad Ebinger735c5ce2021-07-12 13:58:21 -07004602 // This setting doesn't require an active ImsService connection, so do not verify. The
4603 // new setting will be picked up when the ImsService comes up next if it isn't up.
Brad Ebinger919631e2021-06-02 17:46:35 -07004604 ImsManager.getInstance(mApp, slotId).setWfcSetting(isEnabled);
Brad Ebinger1ce9c432019-07-16 13:19:44 -07004605 } catch (ImsException e) {
4606 throw new ServiceSpecificException(e.getCode());
Brad Ebinger35c841c2018-10-01 10:40:55 -07004607 } finally {
4608 Binder.restoreCallingIdentity(identity);
4609 }
4610 }
4611
shilu366312e2019-12-17 09:28:10 -08004612 /**
Sooraj Sasindrane655add2020-11-23 19:40:38 -08004613 * @return true if the user's setting for Voice over Cross SIM is enabled and false if it is not
4614 * Requires carrier privileges or READ_PRECISE_PHONE_STATE permission.
4615 * @param subId The subscription to use to check the configuration.
4616 */
4617 @Override
4618 public boolean isCrossSimCallingEnabledByUser(int subId) {
Nathan Harold62c68512021-04-06 11:26:02 -07004619 TelephonyPermissions.enforceCallingOrSelfReadPrecisePhoneStatePermissionOrCarrierPrivilege(
Sooraj Sasindrane655add2020-11-23 19:40:38 -08004620 mApp, subId, "isCrossSimCallingEnabledByUser");
4621 final long identity = Binder.clearCallingIdentity();
4622 try {
Brad Ebinger919631e2021-06-02 17:46:35 -07004623 int slotId = getSlotIndexOrException(subId);
Brad Ebinger735c5ce2021-07-12 13:58:21 -07004624 // This setting doesn't require an active ImsService connection, so do not verify.
Brad Ebinger919631e2021-06-02 17:46:35 -07004625 return ImsManager.getInstance(mApp, slotId).isCrossSimCallingEnabledByUser();
Sooraj Sasindrane655add2020-11-23 19:40:38 -08004626 } catch (ImsException e) {
4627 throw new ServiceSpecificException(e.getCode());
4628 } finally {
4629 Binder.restoreCallingIdentity(identity);
4630 }
4631 }
4632
4633 /**
4634 * Sets the user's setting for whether or not Voice over Cross SIM is enabled.
4635 * Requires MODIFY_PHONE_STATE permission.
4636 * @param subId The subscription to use to check the configuration.
4637 * @param isEnabled true if the user's setting for Voice over Cross SIM is enabled,
4638 * false otherwise
4639 */
4640 @Override
4641 public void setCrossSimCallingEnabled(int subId, boolean isEnabled) {
4642 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(mApp, subId,
4643 "setCrossSimCallingEnabled");
4644 final long identity = Binder.clearCallingIdentity();
4645 try {
Brad Ebinger919631e2021-06-02 17:46:35 -07004646 int slotId = getSlotIndexOrException(subId);
Brad Ebinger735c5ce2021-07-12 13:58:21 -07004647 // This setting doesn't require an active ImsService connection, so do not verify. The
4648 // new setting will be picked up when the ImsService comes up next if it isn't up.
Brad Ebinger919631e2021-06-02 17:46:35 -07004649 ImsManager.getInstance(mApp, slotId).setCrossSimCallingEnabled(isEnabled);
Sooraj Sasindrane655add2020-11-23 19:40:38 -08004650 } catch (ImsException e) {
4651 throw new ServiceSpecificException(e.getCode());
4652 } finally {
4653 Binder.restoreCallingIdentity(identity);
4654 }
4655 }
4656
4657 /**
shilu366312e2019-12-17 09:28:10 -08004658 * Requires carrier privileges or READ_PRECISE_PHONE_STATE permission.
4659 * @param subId The subscription to use to check the configuration.
4660 */
Brad Ebinger35c841c2018-10-01 10:40:55 -07004661 @Override
Nathan Harold62c68512021-04-06 11:26:02 -07004662
Brad Ebinger35c841c2018-10-01 10:40:55 -07004663 public boolean isVoWiFiRoamingSettingEnabled(int subId) {
Nathan Harold62c68512021-04-06 11:26:02 -07004664 TelephonyPermissions.enforceCallingOrSelfReadPrecisePhoneStatePermissionOrCarrierPrivilege(
Rambo Wang37f9c242020-02-10 14:45:28 -08004665 mApp, subId, "isVoWiFiRoamingSettingEnabled");
Brad Ebinger35c841c2018-10-01 10:40:55 -07004666 final long identity = Binder.clearCallingIdentity();
4667 try {
Brad Ebinger919631e2021-06-02 17:46:35 -07004668 int slotId = getSlotIndexOrException(subId);
Brad Ebinger735c5ce2021-07-12 13:58:21 -07004669 // This setting doesn't require an active ImsService connection, so do not verify.
Brad Ebinger919631e2021-06-02 17:46:35 -07004670 return ImsManager.getInstance(mApp, slotId).isWfcRoamingEnabledByUser();
Brad Ebinger1ce9c432019-07-16 13:19:44 -07004671 } catch (ImsException e) {
4672 throw new ServiceSpecificException(e.getCode());
Brad Ebinger35c841c2018-10-01 10:40:55 -07004673 } finally {
4674 Binder.restoreCallingIdentity(identity);
4675 }
4676 }
4677
4678 @Override
Brad Ebinger1c162042019-02-21 14:49:10 -08004679 public void setVoWiFiRoamingSettingEnabled(int subId, boolean isEnabled) {
Brad Ebinger35c841c2018-10-01 10:40:55 -07004680 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(mApp, subId,
Brad Ebinger1c162042019-02-21 14:49:10 -08004681 "setVoWiFiRoamingSettingEnabled");
Brad Ebinger35c841c2018-10-01 10:40:55 -07004682 final long identity = Binder.clearCallingIdentity();
4683 try {
Brad Ebinger919631e2021-06-02 17:46:35 -07004684 int slotId = getSlotIndexOrException(subId);
Brad Ebinger735c5ce2021-07-12 13:58:21 -07004685 // This setting doesn't require an active ImsService connection, so do not verify. The
4686 // new setting will be picked up when the ImsService comes up next if it isn't up.
Brad Ebinger919631e2021-06-02 17:46:35 -07004687 ImsManager.getInstance(mApp, slotId).setWfcRoamingSetting(isEnabled);
Brad Ebinger1ce9c432019-07-16 13:19:44 -07004688 } catch (ImsException e) {
4689 throw new ServiceSpecificException(e.getCode());
Brad Ebinger35c841c2018-10-01 10:40:55 -07004690 } finally {
4691 Binder.restoreCallingIdentity(identity);
4692 }
4693 }
4694
4695 @Override
4696 public void setVoWiFiNonPersistent(int subId, boolean isCapable, int mode) {
4697 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(mApp, subId,
4698 "setVoWiFiNonPersistent");
4699 final long identity = Binder.clearCallingIdentity();
4700 try {
Brad Ebinger919631e2021-06-02 17:46:35 -07004701 int slotId = getSlotIndexOrException(subId);
Brad Ebinger735c5ce2021-07-12 13:58:21 -07004702 // This setting will be ignored if the ImsService isn't up.
Brad Ebinger919631e2021-06-02 17:46:35 -07004703 ImsManager.getInstance(mApp, slotId).setWfcNonPersistent(isCapable, mode);
Brad Ebinger1ce9c432019-07-16 13:19:44 -07004704 } catch (ImsException e) {
4705 throw new ServiceSpecificException(e.getCode());
Brad Ebinger35c841c2018-10-01 10:40:55 -07004706 } finally {
4707 Binder.restoreCallingIdentity(identity);
4708 }
4709 }
4710
shilu366312e2019-12-17 09:28:10 -08004711 /**
4712 * Requires carrier privileges or READ_PRECISE_PHONE_STATE permission.
4713 * @param subId The subscription to use to check the configuration.
4714 */
Brad Ebinger35c841c2018-10-01 10:40:55 -07004715 @Override
4716 public int getVoWiFiModeSetting(int subId) {
Nathan Harold62c68512021-04-06 11:26:02 -07004717 TelephonyPermissions.enforceCallingOrSelfReadPrecisePhoneStatePermissionOrCarrierPrivilege(
Rambo Wang37f9c242020-02-10 14:45:28 -08004718 mApp, subId, "getVoWiFiModeSetting");
Brad Ebinger35c841c2018-10-01 10:40:55 -07004719 final long identity = Binder.clearCallingIdentity();
4720 try {
Brad Ebinger919631e2021-06-02 17:46:35 -07004721 int slotId = getSlotIndexOrException(subId);
Brad Ebinger735c5ce2021-07-12 13:58:21 -07004722 // This setting doesn't require an active ImsService connection, so do not verify.
Brad Ebinger919631e2021-06-02 17:46:35 -07004723 return ImsManager.getInstance(mApp, slotId).getWfcMode(false /*isRoaming*/);
Brad Ebinger1ce9c432019-07-16 13:19:44 -07004724 } catch (ImsException e) {
4725 throw new ServiceSpecificException(e.getCode());
Brad Ebinger35c841c2018-10-01 10:40:55 -07004726 } finally {
4727 Binder.restoreCallingIdentity(identity);
4728 }
4729 }
4730
4731 @Override
4732 public void setVoWiFiModeSetting(int subId, int mode) {
4733 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(mApp, subId,
4734 "setVoWiFiModeSetting");
4735 final long identity = Binder.clearCallingIdentity();
4736 try {
Brad Ebinger919631e2021-06-02 17:46:35 -07004737 int slotId = getSlotIndexOrException(subId);
Brad Ebinger735c5ce2021-07-12 13:58:21 -07004738 // This setting doesn't require an active ImsService connection, so do not verify. The
4739 // new setting will be picked up when the ImsService comes up next if it isn't up.
Brad Ebinger919631e2021-06-02 17:46:35 -07004740 ImsManager.getInstance(mApp, slotId).setWfcMode(mode, false /*isRoaming*/);
Brad Ebinger1ce9c432019-07-16 13:19:44 -07004741 } catch (ImsException e) {
4742 throw new ServiceSpecificException(e.getCode());
Brad Ebinger35c841c2018-10-01 10:40:55 -07004743 } finally {
4744 Binder.restoreCallingIdentity(identity);
4745 }
4746 }
4747
4748 @Override
4749 public int getVoWiFiRoamingModeSetting(int subId) {
4750 enforceReadPrivilegedPermission("getVoWiFiRoamingModeSetting");
4751 final long identity = Binder.clearCallingIdentity();
4752 try {
Brad Ebinger919631e2021-06-02 17:46:35 -07004753 int slotId = getSlotIndexOrException(subId);
Brad Ebinger735c5ce2021-07-12 13:58:21 -07004754 // This setting doesn't require an active ImsService connection, so do not verify.
Brad Ebinger919631e2021-06-02 17:46:35 -07004755 return ImsManager.getInstance(mApp, slotId).getWfcMode(true /*isRoaming*/);
Brad Ebinger1ce9c432019-07-16 13:19:44 -07004756 } catch (ImsException e) {
4757 throw new ServiceSpecificException(e.getCode());
Brad Ebinger35c841c2018-10-01 10:40:55 -07004758 } finally {
4759 Binder.restoreCallingIdentity(identity);
4760 }
4761 }
4762
4763 @Override
4764 public void setVoWiFiRoamingModeSetting(int subId, int mode) {
4765 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(mApp, subId,
4766 "setVoWiFiRoamingModeSetting");
4767 final long identity = Binder.clearCallingIdentity();
4768 try {
Brad Ebinger919631e2021-06-02 17:46:35 -07004769 int slotId = getSlotIndexOrException(subId);
Brad Ebinger735c5ce2021-07-12 13:58:21 -07004770 // This setting doesn't require an active ImsService connection, so do not verify. The
4771 // new setting will be picked up when the ImsService comes up next if it isn't up.
Brad Ebinger919631e2021-06-02 17:46:35 -07004772 ImsManager.getInstance(mApp, slotId).setWfcMode(mode, true /*isRoaming*/);
Brad Ebinger1ce9c432019-07-16 13:19:44 -07004773 } catch (ImsException e) {
4774 throw new ServiceSpecificException(e.getCode());
Brad Ebinger35c841c2018-10-01 10:40:55 -07004775 } finally {
4776 Binder.restoreCallingIdentity(identity);
4777 }
4778 }
4779
4780 @Override
4781 public void setRttCapabilitySetting(int subId, boolean isEnabled) {
4782 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(mApp, subId,
4783 "setRttCapabilityEnabled");
4784 final long identity = Binder.clearCallingIdentity();
4785 try {
Brad Ebinger919631e2021-06-02 17:46:35 -07004786 int slotId = getSlotIndexOrException(subId);
Brad Ebinger735c5ce2021-07-12 13:58:21 -07004787 // This setting doesn't require an active ImsService connection, so do not verify. The
4788 // new setting will be picked up when the ImsService comes up next if it isn't up.
Brad Ebinger919631e2021-06-02 17:46:35 -07004789 ImsManager.getInstance(mApp, slotId).setRttEnabled(isEnabled);
Brad Ebinger1ce9c432019-07-16 13:19:44 -07004790 } catch (ImsException e) {
4791 throw new ServiceSpecificException(e.getCode());
Brad Ebinger35c841c2018-10-01 10:40:55 -07004792 } finally {
4793 Binder.restoreCallingIdentity(identity);
4794 }
4795 }
4796
shilu366312e2019-12-17 09:28:10 -08004797 /**
4798 * Requires carrier privileges or READ_PRECISE_PHONE_STATE permission.
4799 * @param subId The subscription to use to check the configuration.
4800 */
Brad Ebinger35c841c2018-10-01 10:40:55 -07004801 @Override
4802 public boolean isTtyOverVolteEnabled(int subId) {
Nathan Harold62c68512021-04-06 11:26:02 -07004803 TelephonyPermissions.enforceCallingOrSelfReadPrecisePhoneStatePermissionOrCarrierPrivilege(
Rambo Wang37f9c242020-02-10 14:45:28 -08004804 mApp, subId, "isTtyOverVolteEnabled");
Brad Ebinger35c841c2018-10-01 10:40:55 -07004805 final long identity = Binder.clearCallingIdentity();
4806 try {
Brad Ebinger919631e2021-06-02 17:46:35 -07004807 int slotId = getSlotIndexOrException(subId);
Brad Ebinger735c5ce2021-07-12 13:58:21 -07004808 // This setting doesn't require an active ImsService connection, so do not verify.
Brad Ebinger919631e2021-06-02 17:46:35 -07004809 return ImsManager.getInstance(mApp, slotId).isTtyOnVoLteCapable();
Brad Ebinger1ce9c432019-07-16 13:19:44 -07004810 } catch (ImsException e) {
4811 throw new ServiceSpecificException(e.getCode());
Brad Ebinger35c841c2018-10-01 10:40:55 -07004812 } finally {
4813 Binder.restoreCallingIdentity(identity);
4814 }
4815 }
4816
Brad Ebingerdf5b4f02018-10-31 11:24:17 -07004817 @Override
4818 public void registerImsProvisioningChangedCallback(int subId, IImsConfigCallback callback) {
4819 enforceReadPrivilegedPermission("registerImsProvisioningChangedCallback");
joonhunshincffb7fc2021-11-28 07:32:01 +00004820
Brad Ebingerdf5b4f02018-10-31 11:24:17 -07004821 final long identity = Binder.clearCallingIdentity();
4822 try {
Brad Ebingera2628302022-02-18 03:44:55 +00004823 if (!isImsAvailableOnDevice()) {
4824 throw new ServiceSpecificException(ImsException.CODE_ERROR_UNSUPPORTED_OPERATION,
4825 "IMS not available on device.");
4826 }
4827 int slotId = getSlotIndexOrException(subId);
4828 verifyImsMmTelConfiguredOrThrow(slotId);
4829 ImsManager.getInstance(mApp, slotId)
4830 .addProvisioningCallbackForSubscription(callback, subId);
Brad Ebinger1ce9c432019-07-16 13:19:44 -07004831 } catch (ImsException e) {
4832 throw new ServiceSpecificException(e.getCode());
Brad Ebingerdf5b4f02018-10-31 11:24:17 -07004833 } finally {
4834 Binder.restoreCallingIdentity(identity);
4835 }
4836 }
4837
4838 @Override
4839 public void unregisterImsProvisioningChangedCallback(int subId, IImsConfigCallback callback) {
4840 enforceReadPrivilegedPermission("unregisterImsProvisioningChangedCallback");
joonhunshincffb7fc2021-11-28 07:32:01 +00004841
Brad Ebingerdf5b4f02018-10-31 11:24:17 -07004842 final long identity = Binder.clearCallingIdentity();
Brad Ebinger4ae57f92019-01-09 16:51:30 -08004843 if (!SubscriptionManager.isValidSubscriptionId(subId)) {
4844 throw new IllegalArgumentException("Invalid Subscription ID: " + subId);
4845 }
Brad Ebingerdf5b4f02018-10-31 11:24:17 -07004846 try {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08004847 ImsManager.getInstance(mApp, getSlotIndexOrException(subId))
Brad Ebinger4ae57f92019-01-09 16:51:30 -08004848 .removeProvisioningCallbackForSubscription(callback, subId);
Brad Ebinger1ce9c432019-07-16 13:19:44 -07004849 } catch (ImsException e) {
Brad Ebinger4ae57f92019-01-09 16:51:30 -08004850 Log.i(LOG_TAG, "unregisterImsProvisioningChangedCallback: " + subId
4851 + "is inactive, ignoring unregister.");
4852 // If the subscription is no longer active, just return, since the callback will already
4853 // have been removed internally.
Brad Ebingerdf5b4f02018-10-31 11:24:17 -07004854 } finally {
4855 Binder.restoreCallingIdentity(identity);
4856 }
4857 }
4858
joonhunshincffb7fc2021-11-28 07:32:01 +00004859 @Override
4860 public void registerFeatureProvisioningChangedCallback(int subId,
4861 IFeatureProvisioningCallback callback) {
4862 TelephonyPermissions.enforceCallingOrSelfReadPrecisePhoneStatePermissionOrCarrierPrivilege(
4863 mApp, subId, "registerFeatureProvisioningChangedCallback");
4864
4865 final long identity = Binder.clearCallingIdentity();
4866 if (!SubscriptionManager.isValidSubscriptionId(subId)) {
4867 throw new IllegalArgumentException("Invalid Subscription ID: " + subId);
4868 }
4869
joonhunshin91bc1952022-04-29 08:47:15 +00004870 try {
4871 ImsProvisioningController controller = ImsProvisioningController.getInstance();
4872 if (controller == null) {
4873 throw new ServiceSpecificException(ImsException.CODE_ERROR_UNSUPPORTED_OPERATION,
4874 "Device does not support IMS");
4875 }
4876 controller.addFeatureProvisioningChangedCallback(subId, callback);
4877 } finally {
4878 Binder.restoreCallingIdentity(identity);
4879 }
joonhunshincffb7fc2021-11-28 07:32:01 +00004880 }
4881
4882 @Override
4883 public void unregisterFeatureProvisioningChangedCallback(int subId,
4884 IFeatureProvisioningCallback callback) {
4885 TelephonyPermissions.enforceCallingOrSelfReadPrecisePhoneStatePermissionOrCarrierPrivilege(
4886 mApp, subId, "unregisterFeatureProvisioningChangedCallback");
4887
4888 final long identity = Binder.clearCallingIdentity();
4889 if (!SubscriptionManager.isValidSubscriptionId(subId)) {
4890 throw new IllegalArgumentException("Invalid Subscription ID: " + subId);
4891 }
4892
joonhunshin91bc1952022-04-29 08:47:15 +00004893 try {
4894 ImsProvisioningController controller = ImsProvisioningController.getInstance();
4895 if (controller == null) {
4896 loge("unregisterFeatureProvisioningChangedCallback: Device does not support IMS");
4897 return;
4898 }
4899 controller.removeFeatureProvisioningChangedCallback(subId, callback);
4900 } finally {
4901 Binder.restoreCallingIdentity(identity);
4902 }
joonhunshincffb7fc2021-11-28 07:32:01 +00004903 }
allenwtsu99c623b2020-01-03 18:24:23 +08004904
4905 private void checkModifyPhoneStatePermission(int subId, String message) {
4906 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(mApp, subId,
4907 message);
4908 }
4909
allenwtsu99c623b2020-01-03 18:24:23 +08004910 @Override
joonhunshincffb7fc2021-11-28 07:32:01 +00004911 public void setRcsProvisioningStatusForCapability(int subId, int capability, int tech,
allenwtsu99c623b2020-01-03 18:24:23 +08004912 boolean isProvisioned) {
4913 checkModifyPhoneStatePermission(subId, "setRcsProvisioningStatusForCapability");
4914
4915 final long identity = Binder.clearCallingIdentity();
4916 try {
joonhunshin91bc1952022-04-29 08:47:15 +00004917 ImsProvisioningController controller = ImsProvisioningController.getInstance();
4918 if (controller == null) {
4919 loge("setRcsProvisioningStatusForCapability: Device does not support IMS");
4920 return;
4921 }
4922 controller.setRcsProvisioningStatusForCapability(
4923 subId, capability, tech, isProvisioned);
allenwtsu99c623b2020-01-03 18:24:23 +08004924 } finally {
4925 Binder.restoreCallingIdentity(identity);
4926 }
allenwtsu99c623b2020-01-03 18:24:23 +08004927 }
4928
4929
4930 @Override
joonhunshincffb7fc2021-11-28 07:32:01 +00004931 public boolean getRcsProvisioningStatusForCapability(int subId, int capability, int tech) {
4932 TelephonyPermissions.enforceCallingOrSelfReadPrecisePhoneStatePermissionOrCarrierPrivilege(
4933 mApp, subId, "getRcsProvisioningStatusForCapability");
4934
allenwtsu99c623b2020-01-03 18:24:23 +08004935 final long identity = Binder.clearCallingIdentity();
4936 try {
joonhunshin91bc1952022-04-29 08:47:15 +00004937 ImsProvisioningController controller = ImsProvisioningController.getInstance();
4938 if (controller == null) {
4939 loge("getRcsProvisioningStatusForCapability: Device does not support IMS");
4940
4941 // device does not support IMS, this method will return true always.
4942 return true;
4943 }
4944 return controller.getRcsProvisioningStatusForCapability(subId, capability, tech);
allenwtsu99c623b2020-01-03 18:24:23 +08004945 } finally {
4946 Binder.restoreCallingIdentity(identity);
4947 }
4948 }
4949
Brad Ebingerdf5b4f02018-10-31 11:24:17 -07004950 @Override
Brad Ebinger1c8542e2019-01-14 13:43:14 -08004951 public void setImsProvisioningStatusForCapability(int subId, int capability, int tech,
4952 boolean isProvisioned) {
allenwtsu99c623b2020-01-03 18:24:23 +08004953 checkModifyPhoneStatePermission(subId, "setImsProvisioningStatusForCapability");
joonhunshincffb7fc2021-11-28 07:32:01 +00004954
Brad Ebinger1c8542e2019-01-14 13:43:14 -08004955 final long identity = Binder.clearCallingIdentity();
4956 try {
joonhunshin91bc1952022-04-29 08:47:15 +00004957 ImsProvisioningController controller = ImsProvisioningController.getInstance();
4958 if (controller == null) {
4959 loge("setImsProvisioningStatusForCapability: Device does not support IMS");
4960 return;
4961 }
4962 controller.setImsProvisioningStatusForCapability(
4963 subId, capability, tech, isProvisioned);
Brad Ebinger1c8542e2019-01-14 13:43:14 -08004964 } finally {
4965 Binder.restoreCallingIdentity(identity);
4966 }
4967 }
4968
4969 @Override
4970 public boolean getImsProvisioningStatusForCapability(int subId, int capability, int tech) {
joonhunshincffb7fc2021-11-28 07:32:01 +00004971 TelephonyPermissions.enforceCallingOrSelfReadPrecisePhoneStatePermissionOrCarrierPrivilege(
4972 mApp, subId, "getProvisioningStatusForCapability");
4973
Brad Ebinger1c8542e2019-01-14 13:43:14 -08004974 final long identity = Binder.clearCallingIdentity();
4975 try {
joonhunshin91bc1952022-04-29 08:47:15 +00004976 ImsProvisioningController controller = ImsProvisioningController.getInstance();
4977 if (controller == null) {
4978 loge("getImsProvisioningStatusForCapability: Device does not support IMS");
Brad Ebinger1c8542e2019-01-14 13:43:14 -08004979
joonhunshin91bc1952022-04-29 08:47:15 +00004980 // device does not support IMS, this method will return true always.
4981 return true;
4982 }
4983 return controller.getImsProvisioningStatusForCapability(subId, capability, tech);
Brad Ebinger1c8542e2019-01-14 13:43:14 -08004984 } finally {
4985 Binder.restoreCallingIdentity(identity);
4986 }
4987 }
4988
4989 @Override
joonhunshincffb7fc2021-11-28 07:32:01 +00004990 public boolean isProvisioningRequiredForCapability(int subId, int capability, int tech) {
4991 TelephonyPermissions.enforceCallingOrSelfReadPrecisePhoneStatePermissionOrCarrierPrivilege(
4992 mApp, subId, "isProvisioningRequiredForCapability");
4993
4994 final long identity = Binder.clearCallingIdentity();
4995 try {
joonhunshin91bc1952022-04-29 08:47:15 +00004996 ImsProvisioningController controller = ImsProvisioningController.getInstance();
4997 if (controller == null) {
4998 loge("isProvisioningRequiredForCapability: Device does not support IMS");
4999
5000 // device does not support IMS, this method will return false
5001 return false;
5002 }
5003 return controller.isImsProvisioningRequiredForCapability(subId, capability, tech);
joonhunshincffb7fc2021-11-28 07:32:01 +00005004 } finally {
5005 Binder.restoreCallingIdentity(identity);
Brad Ebinger1c8542e2019-01-14 13:43:14 -08005006 }
Brad Ebinger1c8542e2019-01-14 13:43:14 -08005007 }
5008
5009 @Override
joonhunshincffb7fc2021-11-28 07:32:01 +00005010 public boolean isRcsProvisioningRequiredForCapability(int subId, int capability, int tech) {
5011 TelephonyPermissions.enforceCallingOrSelfReadPrecisePhoneStatePermissionOrCarrierPrivilege(
5012 mApp, subId, "isProvisioningRequiredForCapability");
Brad Ebinger1c8542e2019-01-14 13:43:14 -08005013
joonhunshincffb7fc2021-11-28 07:32:01 +00005014 final long identity = Binder.clearCallingIdentity();
5015 try {
joonhunshin91bc1952022-04-29 08:47:15 +00005016 ImsProvisioningController controller = ImsProvisioningController.getInstance();
5017 if (controller == null) {
5018 loge("isRcsProvisioningRequiredForCapability: Device does not support IMS");
5019
5020 // device does not support IMS, this method will return false
5021 return false;
5022 }
5023 return controller.isRcsProvisioningRequiredForCapability(subId, capability, tech);
joonhunshincffb7fc2021-11-28 07:32:01 +00005024 } finally {
5025 Binder.restoreCallingIdentity(identity);
5026 }
Brad Ebinger1c8542e2019-01-14 13:43:14 -08005027 }
5028
Brad Ebinger1c8542e2019-01-14 13:43:14 -08005029 @Override
Brad Ebingerdf5b4f02018-10-31 11:24:17 -07005030 public int getImsProvisioningInt(int subId, int key) {
Brad Ebinger1c8542e2019-01-14 13:43:14 -08005031 if (!SubscriptionManager.isValidSubscriptionId(subId)) {
5032 throw new IllegalArgumentException("Invalid Subscription id '" + subId + "'");
5033 }
joonhunshincffb7fc2021-11-28 07:32:01 +00005034 TelephonyPermissions.enforceCallingOrSelfReadPrecisePhoneStatePermissionOrCarrierPrivilege(
5035 mApp, subId, "getImsProvisioningInt");
5036
Brad Ebingerdf5b4f02018-10-31 11:24:17 -07005037 final long identity = Binder.clearCallingIdentity();
5038 try {
5039 // TODO: Refactor to remove ImsManager dependence and query through ImsPhone directly.
Brad Ebinger1c8542e2019-01-14 13:43:14 -08005040 int slotId = getSlotIndex(subId);
5041 if (slotId <= SubscriptionManager.INVALID_SIM_SLOT_INDEX) {
5042 Log.w(LOG_TAG, "getImsProvisioningInt: called with an inactive subscription '"
5043 + subId + "' for key:" + key);
5044 return ImsConfigImplBase.CONFIG_RESULT_UNKNOWN;
5045 }
joonhunshincffb7fc2021-11-28 07:32:01 +00005046
joonhunshin91bc1952022-04-29 08:47:15 +00005047 ImsProvisioningController controller = ImsProvisioningController.getInstance();
5048 if (controller == null) {
5049 loge("getImsProvisioningInt: Device does not support IMS");
5050
5051 // device does not support IMS, this method will return CONFIG_RESULT_UNKNOWN.
5052 return ImsConfigImplBase.CONFIG_RESULT_UNKNOWN;
5053 }
5054 int retVal = controller.getProvisioningValue(subId, key);
joonhunshincffb7fc2021-11-28 07:32:01 +00005055 if (retVal != ImsConfigImplBase.CONFIG_RESULT_UNKNOWN) {
5056 return retVal;
5057 }
5058
calvinpanb5a34062021-02-08 19:59:36 +08005059 return ImsManager.getInstance(mApp, slotId).getConfigInt(key);
Brad Ebinger1ce9c432019-07-16 13:19:44 -07005060 } catch (com.android.ims.ImsException e) {
Brad Ebinger1c8542e2019-01-14 13:43:14 -08005061 Log.w(LOG_TAG, "getImsProvisioningInt: ImsService is not available for subscription '"
5062 + subId + "' for key:" + key);
5063 return ImsConfigImplBase.CONFIG_RESULT_UNKNOWN;
Brad Ebingerdf5b4f02018-10-31 11:24:17 -07005064 } finally {
5065 Binder.restoreCallingIdentity(identity);
5066 }
5067 }
5068
5069 @Override
5070 public String getImsProvisioningString(int subId, int key) {
Brad Ebinger1c8542e2019-01-14 13:43:14 -08005071 if (!SubscriptionManager.isValidSubscriptionId(subId)) {
5072 throw new IllegalArgumentException("Invalid Subscription id '" + subId + "'");
5073 }
joonhunshincffb7fc2021-11-28 07:32:01 +00005074 TelephonyPermissions.enforceCallingOrSelfReadPrecisePhoneStatePermissionOrCarrierPrivilege(
5075 mApp, subId, "getImsProvisioningString");
5076
Brad Ebingerdf5b4f02018-10-31 11:24:17 -07005077 final long identity = Binder.clearCallingIdentity();
5078 try {
5079 // TODO: Refactor to remove ImsManager dependence and query through ImsPhone directly.
Brad Ebinger1c8542e2019-01-14 13:43:14 -08005080 int slotId = getSlotIndex(subId);
5081 if (slotId <= SubscriptionManager.INVALID_SIM_SLOT_INDEX) {
5082 Log.w(LOG_TAG, "getImsProvisioningString: called for an inactive subscription id '"
5083 + subId + "' for key:" + key);
5084 return ProvisioningManager.STRING_QUERY_RESULT_ERROR_GENERIC;
5085 }
calvinpanb5a34062021-02-08 19:59:36 +08005086 return ImsManager.getInstance(mApp, slotId).getConfigString(key);
Brad Ebinger1ce9c432019-07-16 13:19:44 -07005087 } catch (com.android.ims.ImsException e) {
Brad Ebinger1c8542e2019-01-14 13:43:14 -08005088 Log.w(LOG_TAG, "getImsProvisioningString: ImsService is not available for sub '"
5089 + subId + "' for key:" + key);
5090 return ProvisioningManager.STRING_QUERY_RESULT_ERROR_NOT_READY;
Brad Ebingerdf5b4f02018-10-31 11:24:17 -07005091 } finally {
5092 Binder.restoreCallingIdentity(identity);
5093 }
5094 }
5095
5096 @Override
5097 public int setImsProvisioningInt(int subId, int key, int value) {
Brad Ebinger1c8542e2019-01-14 13:43:14 -08005098 if (!SubscriptionManager.isValidSubscriptionId(subId)) {
5099 throw new IllegalArgumentException("Invalid Subscription id '" + subId + "'");
5100 }
Brad Ebinger3d0b34e2018-11-15 14:13:12 -08005101 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(mApp, subId,
5102 "setImsProvisioningInt");
joonhunshincffb7fc2021-11-28 07:32:01 +00005103
Brad Ebingerdf5b4f02018-10-31 11:24:17 -07005104 final long identity = Binder.clearCallingIdentity();
5105 try {
5106 // TODO: Refactor to remove ImsManager dependence and query through ImsPhone directly.
Brad Ebinger1c8542e2019-01-14 13:43:14 -08005107 int slotId = getSlotIndex(subId);
5108 if (slotId <= SubscriptionManager.INVALID_SIM_SLOT_INDEX) {
5109 Log.w(LOG_TAG, "setImsProvisioningInt: called with an inactive subscription id '"
5110 + subId + "' for key:" + key);
5111 return ImsConfigImplBase.CONFIG_RESULT_FAILED;
5112 }
joonhunshincffb7fc2021-11-28 07:32:01 +00005113
joonhunshin91bc1952022-04-29 08:47:15 +00005114 ImsProvisioningController controller = ImsProvisioningController.getInstance();
5115 if (controller == null) {
5116 loge("setImsProvisioningInt: Device does not support IMS");
5117
5118 // device does not support IMS, this method will return CONFIG_RESULT_FAILED.
5119 return ImsConfigImplBase.CONFIG_RESULT_FAILED;
5120 }
5121 int retVal = controller.setProvisioningValue(subId, key, value);
joonhunshincffb7fc2021-11-28 07:32:01 +00005122 if (retVal != ImsConfigImplBase.CONFIG_RESULT_UNKNOWN) {
5123 return retVal;
5124 }
5125
calvinpanb5a34062021-02-08 19:59:36 +08005126 return ImsManager.getInstance(mApp, slotId).setConfig(key, value);
5127 } catch (com.android.ims.ImsException | RemoteException e) {
Brad Ebinger1c8542e2019-01-14 13:43:14 -08005128 Log.w(LOG_TAG, "setImsProvisioningInt: ImsService unavailable for sub '" + subId
calvinpanb5a34062021-02-08 19:59:36 +08005129 + "' for key:" + key, e);
Brad Ebinger1c8542e2019-01-14 13:43:14 -08005130 return ImsConfigImplBase.CONFIG_RESULT_FAILED;
Brad Ebingerdf5b4f02018-10-31 11:24:17 -07005131 } finally {
5132 Binder.restoreCallingIdentity(identity);
5133 }
5134 }
5135
5136 @Override
5137 public int setImsProvisioningString(int subId, int key, String value) {
Brad Ebinger1c8542e2019-01-14 13:43:14 -08005138 if (!SubscriptionManager.isValidSubscriptionId(subId)) {
5139 throw new IllegalArgumentException("Invalid Subscription id '" + subId + "'");
5140 }
Brad Ebinger3d0b34e2018-11-15 14:13:12 -08005141 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(mApp, subId,
5142 "setImsProvisioningString");
Brad Ebingerdf5b4f02018-10-31 11:24:17 -07005143 final long identity = Binder.clearCallingIdentity();
5144 try {
5145 // TODO: Refactor to remove ImsManager dependence and query through ImsPhone directly.
Brad Ebinger1c8542e2019-01-14 13:43:14 -08005146 int slotId = getSlotIndex(subId);
5147 if (slotId <= SubscriptionManager.INVALID_SIM_SLOT_INDEX) {
5148 Log.w(LOG_TAG, "setImsProvisioningString: called with an inactive subscription id '"
5149 + subId + "' for key:" + key);
5150 return ImsConfigImplBase.CONFIG_RESULT_FAILED;
5151 }
calvinpanb5a34062021-02-08 19:59:36 +08005152 return ImsManager.getInstance(mApp, slotId).setConfig(key, value);
5153 } catch (com.android.ims.ImsException | RemoteException e) {
Brad Ebinger1c8542e2019-01-14 13:43:14 -08005154 Log.w(LOG_TAG, "setImsProvisioningString: ImsService unavailable for sub '" + subId
calvinpanb5a34062021-02-08 19:59:36 +08005155 + "' for key:" + key, e);
Brad Ebinger1c8542e2019-01-14 13:43:14 -08005156 return ImsConfigImplBase.CONFIG_RESULT_FAILED;
Brad Ebingerdf5b4f02018-10-31 11:24:17 -07005157 } finally {
5158 Binder.restoreCallingIdentity(identity);
5159 }
5160 }
5161
Brad Ebinger919631e2021-06-02 17:46:35 -07005162 /**
5163 * Throw an ImsException if the IMS resolver does not have an ImsService configured for MMTEL
5164 * for the given slot ID or no ImsResolver instance has been created.
5165 * @param slotId The slot ID that the IMS service is created for.
5166 * @throws ImsException If there is no ImsService configured for this slot.
5167 */
5168 private void verifyImsMmTelConfiguredOrThrow(int slotId) throws ImsException {
5169 if (mImsResolver == null || !mImsResolver.isImsServiceConfiguredForFeature(slotId,
5170 ImsFeature.FEATURE_MMTEL)) {
5171 throw new ImsException("This subscription does not support MMTEL over IMS",
5172 ImsException.CODE_ERROR_UNSUPPORTED_OPERATION);
5173 }
5174 }
5175
Brad Ebinger1ce9c432019-07-16 13:19:44 -07005176 private int getSlotIndexOrException(int subId) throws ImsException {
Brad Ebinger35c841c2018-10-01 10:40:55 -07005177 int slotId = SubscriptionManager.getSlotIndex(subId);
5178 if (!SubscriptionManager.isValidSlotIndex(slotId)) {
Brad Ebinger1ce9c432019-07-16 13:19:44 -07005179 throw new ImsException("Invalid Subscription Id, subId=" + subId,
5180 ImsException.CODE_ERROR_INVALID_SUBSCRIPTION);
Brad Ebinger35c841c2018-10-01 10:40:55 -07005181 }
5182 return slotId;
5183 }
5184
Brad Ebinger1c8542e2019-01-14 13:43:14 -08005185 private int getSlotIndex(int subId) {
5186 int slotId = SubscriptionManager.getSlotIndex(subId);
5187 if (!SubscriptionManager.isValidSlotIndex(slotId)) {
5188 return SubscriptionManager.INVALID_SIM_SLOT_INDEX;
5189 }
5190 return slotId;
5191 }
5192
Wink Saville36469e72014-06-11 15:17:00 -07005193 /**
Nathan Harold9042f0b2019-05-21 15:51:27 -07005194 * Returns the data network type for a subId; does not throw SecurityException.
Wink Saville36469e72014-06-11 15:17:00 -07005195 */
5196 @Override
Philip P. Moltmann700a9592019-10-03 11:53:50 -07005197 public int getNetworkTypeForSubscriber(int subId, String callingPackage,
5198 String callingFeatureId) {
Shuo Qian13d89152021-05-10 23:58:11 -07005199 try {
5200 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
5201 } catch (SecurityException se) {
5202 EventLog.writeEvent(0x534e4554, "186776740", Binder.getCallingUid());
5203 throw new SecurityException("Package " + callingPackage + " does not belong to "
5204 + Binder.getCallingUid());
5205 }
Nathan Haroldf096d982020-11-18 17:18:06 -08005206 final int targetSdk = TelephonyPermissions.getTargetSdk(mApp, callingPackage);
Nathan Haroldef60dba2019-05-22 13:55:14 -07005207 if (targetSdk > android.os.Build.VERSION_CODES.Q) {
Philip P. Moltmann700a9592019-10-03 11:53:50 -07005208 return getDataNetworkTypeForSubscriber(subId, callingPackage, callingFeatureId);
Nathan Haroldef60dba2019-05-22 13:55:14 -07005209 } else if (targetSdk == android.os.Build.VERSION_CODES.Q
Nathan Harold9042f0b2019-05-21 15:51:27 -07005210 && !TelephonyPermissions.checkCallingOrSelfReadPhoneStateNoThrow(
Philip P. Moltmann700a9592019-10-03 11:53:50 -07005211 mApp, subId, callingPackage, callingFeatureId,
5212 "getNetworkTypeForSubscriber")) {
Robert Greenwalt36b23af2015-07-06 17:59:14 -07005213 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
5214 }
Robert Greenwalta5dcfcb2015-07-10 09:06:29 -07005215
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005216 final long identity = Binder.clearCallingIdentity();
5217 try {
5218 final Phone phone = getPhone(subId);
5219 if (phone != null) {
5220 return phone.getServiceState().getDataNetworkType();
5221 } else {
5222 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
5223 }
5224 } finally {
5225 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07005226 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07005227 }
5228
5229 /**
5230 * Returns the data network type
5231 */
5232 @Override
Philip P. Moltmann700a9592019-10-03 11:53:50 -07005233 public int getDataNetworkType(String callingPackage, String callingFeatureId) {
Jack Yu285100e2022-12-02 22:48:35 -08005234 return getDataNetworkTypeForSubscriber(SubscriptionManager.getDefaultDataSubscriptionId(),
Zoey Chenfd61f7f2021-04-21 13:42:10 +08005235 callingPackage, callingFeatureId);
Wink Saville36469e72014-06-11 15:17:00 -07005236 }
5237
5238 /**
5239 * Returns the data network type for a subId
5240 */
5241 @Override
Philip P. Moltmann700a9592019-10-03 11:53:50 -07005242 public int getDataNetworkTypeForSubscriber(int subId, String callingPackage,
5243 String callingFeatureId) {
Sooraj Sasindran5d051bf2021-11-05 13:14:15 -07005244 String functionName = "getDataNetworkTypeForSubscriber";
5245 if (!TelephonyPermissions.checkCallingOrSelfReadNonDangerousPhoneStateNoThrow(
5246 mApp, functionName)) {
5247 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
5248 mApp, subId, callingPackage, callingFeatureId, functionName)) {
5249 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
5250 }
Robert Greenwalt36b23af2015-07-06 17:59:14 -07005251 }
5252
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005253 final long identity = Binder.clearCallingIdentity();
5254 try {
5255 final Phone phone = getPhone(subId);
5256 if (phone != null) {
5257 return phone.getServiceState().getDataNetworkType();
5258 } else {
5259 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
5260 }
5261 } finally {
5262 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07005263 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07005264 }
5265
5266 /**
Wink Saville36469e72014-06-11 15:17:00 -07005267 * Returns the Voice network type for a subId
5268 */
5269 @Override
Philip P. Moltmann700a9592019-10-03 11:53:50 -07005270 public int getVoiceNetworkTypeForSubscriber(int subId, String callingPackage,
5271 String callingFeatureId) {
Sooraj Sasindran5d051bf2021-11-05 13:14:15 -07005272 String functionName = "getVoiceNetworkTypeForSubscriber";
5273 if (!TelephonyPermissions.checkCallingOrSelfReadNonDangerousPhoneStateNoThrow(
5274 mApp, functionName)) {
5275 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
5276 mApp, subId, callingPackage, callingFeatureId, functionName)) {
5277 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
5278 }
Robert Greenwalta5dcfcb2015-07-10 09:06:29 -07005279 }
5280
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005281 final long identity = Binder.clearCallingIdentity();
5282 try {
5283 final Phone phone = getPhone(subId);
5284 if (phone != null) {
5285 return phone.getServiceState().getVoiceNetworkType();
5286 } else {
5287 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
5288 }
5289 } finally {
5290 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07005291 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07005292 }
5293
5294 /**
5295 * @return true if a ICC card is present
5296 */
5297 public boolean hasIccCard() {
Wink Saville36469e72014-06-11 15:17:00 -07005298 // FIXME Make changes to pass defaultSimId of type int
Jack Yu285100e2022-12-02 22:48:35 -08005299 return hasIccCardUsingSlotIndex(SubscriptionManager.getSlotIndex(
Sanket Padawe13bac7b2017-03-20 15:04:47 -07005300 getDefaultSubscription()));
Wink Saville36469e72014-06-11 15:17:00 -07005301 }
5302
5303 /**
Sanket Padawe13bac7b2017-03-20 15:04:47 -07005304 * @return true if a ICC card is present for a slotIndex
Wink Saville36469e72014-06-11 15:17:00 -07005305 */
Sanket Padawe356d7632015-06-22 14:03:32 -07005306 @Override
Sanket Padawe13bac7b2017-03-20 15:04:47 -07005307 public boolean hasIccCardUsingSlotIndex(int slotIndex) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005308 final long identity = Binder.clearCallingIdentity();
5309 try {
5310 final Phone phone = PhoneFactory.getPhone(slotIndex);
5311 if (phone != null) {
5312 return phone.getIccCard().hasIccCard();
5313 } else {
5314 return false;
5315 }
5316 } finally {
5317 Binder.restoreCallingIdentity(identity);
Amit Mahajana6fc2a82015-01-06 11:53:51 -08005318 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07005319 }
5320
5321 /**
5322 * Return if the current radio is LTE on CDMA. This
5323 * is a tri-state return value as for a period of time
5324 * the mode may be unknown.
5325 *
Robert Greenwalt36b23af2015-07-06 17:59:14 -07005326 * @param callingPackage the name of the package making the call.
Santos Cordon7d4ddf62013-07-10 11:58:08 -07005327 * @return {@link Phone#LTE_ON_CDMA_UNKNOWN}, {@link Phone#LTE_ON_CDMA_FALSE}
Jake Hambye994d462014-02-03 13:10:13 -08005328 * or {@link Phone#LTE_ON_CDMA_TRUE}
Santos Cordon7d4ddf62013-07-10 11:58:08 -07005329 */
Robert Greenwalt36b23af2015-07-06 17:59:14 -07005330 @Override
Philip P. Moltmann700a9592019-10-03 11:53:50 -07005331 public int getLteOnCdmaMode(String callingPackage, String callingFeatureId) {
5332 return getLteOnCdmaModeForSubscriber(getDefaultSubscription(), callingPackage,
5333 callingFeatureId);
Wink Saville36469e72014-06-11 15:17:00 -07005334 }
5335
Sanket Padawe356d7632015-06-22 14:03:32 -07005336 @Override
Philip P. Moltmann700a9592019-10-03 11:53:50 -07005337 public int getLteOnCdmaModeForSubscriber(int subId, String callingPackage,
5338 String callingFeatureId) {
Sarah Chin790d2922020-01-16 12:17:23 -08005339 try {
5340 enforceReadPrivilegedPermission("getLteOnCdmaModeForSubscriber");
5341 } catch (SecurityException e) {
Robert Greenwalt36b23af2015-07-06 17:59:14 -07005342 return PhoneConstants.LTE_ON_CDMA_UNKNOWN;
5343 }
5344
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005345 final long identity = Binder.clearCallingIdentity();
5346 try {
5347 final Phone phone = getPhone(subId);
5348 if (phone == null) {
5349 return PhoneConstants.LTE_ON_CDMA_UNKNOWN;
5350 } else {
Nathan Harold05ad6332020-07-10 11:54:36 -07005351 return TelephonyProperties.lte_on_cdma_device()
5352 .orElse(PhoneConstants.LTE_ON_CDMA_FALSE);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005353 }
5354 } finally {
5355 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07005356 }
Wink Saville36469e72014-06-11 15:17:00 -07005357 }
5358
Wink Saville36469e72014-06-11 15:17:00 -07005359 /**
5360 * {@hide}
5361 * Returns Default subId, 0 in the case of single standby.
5362 */
Wink Savilleb564aae2014-10-23 10:18:09 -07005363 private int getDefaultSubscription() {
Jack Yu285100e2022-12-02 22:48:35 -08005364 return SubscriptionManager.getDefaultSubscriptionId();
Wink Saville36469e72014-06-11 15:17:00 -07005365 }
5366
Shishir Agrawala9f32182016-04-12 12:00:16 -07005367 private int getSlotForDefaultSubscription() {
Jack Yu285100e2022-12-02 22:48:35 -08005368 return SubscriptionManager.getPhoneId(getDefaultSubscription());
Shishir Agrawala9f32182016-04-12 12:00:16 -07005369 }
5370
Wink Savilleb564aae2014-10-23 10:18:09 -07005371 private int getPreferredVoiceSubscription() {
Jack Yu285100e2022-12-02 22:48:35 -08005372 return SubscriptionManager.getDefaultVoiceSubscriptionId();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07005373 }
Ihab Awadf2177b72013-11-25 13:33:23 -08005374
Pengquan Menge92a50d2018-09-21 15:54:48 -07005375 private boolean isActiveSubscription(int subId) {
Jack Yu285100e2022-12-02 22:48:35 -08005376 if (PhoneFactory.isSubscriptionManagerServiceEnabled()) {
5377 return SubscriptionManagerService.getInstance().isActiveSubId(subId,
5378 mApp.getOpPackageName(), mApp.getFeatureId());
5379 }
Pengquan Menge92a50d2018-09-21 15:54:48 -07005380 return mSubscriptionController.isActiveSubId(subId);
5381 }
5382
Ihab Awadf2177b72013-11-25 13:33:23 -08005383 /**
5384 * @see android.telephony.TelephonyManager.WifiCallingChoices
5385 */
5386 public int getWhenToMakeWifiCalls() {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005387 final long identity = Binder.clearCallingIdentity();
5388 try {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08005389 return Settings.System.getInt(mApp.getContentResolver(),
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005390 Settings.System.WHEN_TO_MAKE_WIFI_CALLS,
5391 getWhenToMakeWifiCallsDefaultPreference());
5392 } finally {
5393 Binder.restoreCallingIdentity(identity);
5394 }
Ihab Awadf2177b72013-11-25 13:33:23 -08005395 }
5396
5397 /**
5398 * @see android.telephony.TelephonyManager.WifiCallingChoices
5399 */
5400 public void setWhenToMakeWifiCalls(int preference) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005401 final long identity = Binder.clearCallingIdentity();
5402 try {
5403 if (DBG) log("setWhenToMakeWifiCallsStr, storing setting = " + preference);
Nazanin Bakhshif782e562018-12-11 15:15:39 -08005404 Settings.System.putInt(mApp.getContentResolver(),
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005405 Settings.System.WHEN_TO_MAKE_WIFI_CALLS, preference);
5406 } finally {
5407 Binder.restoreCallingIdentity(identity);
5408 }
Ihab Awadf9e92732013-12-05 18:02:52 -08005409 }
5410
Sailesh Nepald1e68152013-12-12 19:08:02 -08005411 private static int getWhenToMakeWifiCallsDefaultPreference() {
Santos Cordonda120f42014-08-06 04:44:34 -07005412 // TODO: Use a build property to choose this value.
Evan Charlton9829e882013-12-19 15:30:38 -08005413 return TelephonyManager.WifiCallingChoices.ALWAYS_USE;
Ihab Awadf2177b72013-11-25 13:33:23 -08005414 }
Shishir Agrawal69f68122013-12-16 17:25:49 -08005415
Muralidhar Reddybd38d952021-12-02 21:04:16 +00005416 private Phone getPhoneFromSlotPortIndexOrThrowException(int slotIndex, int portIndex) {
5417 int phoneId = UiccController.getInstance().getPhoneIdFromSlotPortIndex(slotIndex,
5418 portIndex);
Jordan Liu4c733742019-02-28 12:03:40 -08005419 if (phoneId == -1) {
Muralidhar Reddybd38d952021-12-02 21:04:16 +00005420 throw new IllegalArgumentException("Given slot index: " + slotIndex + " port index: "
5421 + portIndex + " does not correspond to an active phone");
Jordan Liu4c733742019-02-28 12:03:40 -08005422 }
5423 return PhoneFactory.getPhone(phoneId);
5424 }
5425
Shishir Agrawal566b7612013-10-28 14:41:00 -07005426 @Override
Derek Tan740e1672017-06-27 14:56:27 -07005427 public IccOpenLogicalChannelResponse iccOpenLogicalChannel(
Rambo Wanga1782702021-11-10 20:15:19 -08005428 @NonNull IccLogicalChannelRequest request) {
5429 Phone phone = getPhoneFromValidIccLogicalChannelRequest(request,
5430 /*message=*/ "iccOpenLogicalChannel");
5431
5432 if (DBG) log("iccOpenLogicalChannel: request=" + request);
5433 // Verify that the callingPackage in the request belongs to the calling UID
5434 mAppOps.checkPackage(Binder.getCallingUid(), request.callingPackage);
5435
5436 return iccOpenLogicalChannelWithPermission(phone, request);
Jordan Liu4c733742019-02-28 12:03:40 -08005437 }
Shishir Agrawal566b7612013-10-28 14:41:00 -07005438
Rambo Wanga1782702021-11-10 20:15:19 -08005439 private Phone getPhoneFromValidIccLogicalChannelRequest(
5440 @NonNull IccLogicalChannelRequest request, String message) {
5441 Phone phone;
5442 if (request.subId != SubscriptionManager.INVALID_SUBSCRIPTION_ID) {
5443 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
5444 mApp, request.subId, message);
5445 phone = getPhoneFromSubId(request.subId);
5446 } else if (request.slotIndex != SubscriptionManager.INVALID_SIM_SLOT_INDEX) {
5447 enforceModifyPermission();
5448 phone = getPhoneFromSlotPortIndexOrThrowException(request.slotIndex, request.portIndex);
5449 } else {
5450 throw new IllegalArgumentException("Both subId and slotIndex in request are invalid.");
Jordan Liu4c733742019-02-28 12:03:40 -08005451 }
Rambo Wanga1782702021-11-10 20:15:19 -08005452 return phone;
Jordan Liu4c733742019-02-28 12:03:40 -08005453 }
5454
5455 private IccOpenLogicalChannelResponse iccOpenLogicalChannelWithPermission(Phone phone,
Rambo Wanga1782702021-11-10 20:15:19 -08005456 IccLogicalChannelRequest channelRequest) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005457 final long identity = Binder.clearCallingIdentity();
5458 try {
Rambo Wanga1782702021-11-10 20:15:19 -08005459 if (TextUtils.equals(ISDR_AID, channelRequest.aid)) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005460 // Only allows LPA to open logical channel to ISD-R.
Nazanin Bakhshif782e562018-12-11 15:15:39 -08005461 ComponentInfo bestComponent = EuiccConnector.findBestComponent(getDefaultPhone()
5462 .getContext().getPackageManager());
Rambo Wanga1782702021-11-10 20:15:19 -08005463 if (bestComponent == null || !TextUtils.equals(channelRequest.callingPackage,
5464 bestComponent.packageName)) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005465 loge("The calling package is not allowed to access ISD-R.");
5466 throw new SecurityException(
5467 "The calling package is not allowed to access ISD-R.");
5468 }
Derek Tan740e1672017-06-27 14:56:27 -07005469 }
Derek Tan740e1672017-06-27 14:56:27 -07005470
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005471 IccOpenLogicalChannelResponse response = (IccOpenLogicalChannelResponse) sendRequest(
Rambo Wanga1782702021-11-10 20:15:19 -08005472 CMD_OPEN_CHANNEL, channelRequest, phone, null /* workSource */);
5473 if (DBG) log("iccOpenLogicalChannelWithPermission: response=" + response);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005474 return response;
5475 } finally {
5476 Binder.restoreCallingIdentity(identity);
5477 }
Shishir Agrawal566b7612013-10-28 14:41:00 -07005478 }
5479
5480 @Override
Rambo Wanga1782702021-11-10 20:15:19 -08005481 public boolean iccCloseLogicalChannel(@NonNull IccLogicalChannelRequest request) {
5482 Phone phone = getPhoneFromValidIccLogicalChannelRequest(request,
5483 /*message=*/"iccCloseLogicalChannel");
5484
5485 if (DBG) log("iccCloseLogicalChannel: request=" + request);
5486
5487 return iccCloseLogicalChannelWithPermission(phone, request);
Jordan Liu4c733742019-02-28 12:03:40 -08005488 }
Shishir Agrawal566b7612013-10-28 14:41:00 -07005489
Rambo Wanga1782702021-11-10 20:15:19 -08005490 private boolean iccCloseLogicalChannelWithPermission(Phone phone,
5491 IccLogicalChannelRequest request) {
Chen Xua8f0dff2022-02-12 00:34:15 -08005492 // before this feature is enabled, this API should only return false if
5493 // the operation fails instead of throwing runtime exception for
5494 // backward-compatibility.
5495 final boolean shouldThrowExceptionOnFailure = CompatChanges.isChangeEnabled(
5496 ICC_CLOSE_CHANNEL_EXCEPTION_ON_FAILURE, Binder.getCallingUid());
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005497 final long identity = Binder.clearCallingIdentity();
5498 try {
Rambo Wanga1782702021-11-10 20:15:19 -08005499 if (request.channel < 0) {
Chen Xu540470b2021-12-14 17:15:47 -08005500 throw new IllegalArgumentException("request.channel is less than 0");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005501 }
Chen Xue9d737e2022-01-01 23:41:31 -08005502 Object result = sendRequest(CMD_CLOSE_CHANNEL, request.channel, phone,
Jordan Liu4c733742019-02-28 12:03:40 -08005503 null /* workSource */);
Chen Xue9d737e2022-01-01 23:41:31 -08005504 Boolean success = false;
5505 if (result instanceof RuntimeException) {
5506 // if there is an exception returned, throw from the binder thread here.
Chen Xua8f0dff2022-02-12 00:34:15 -08005507 if (shouldThrowExceptionOnFailure) {
5508 throw (RuntimeException) result;
5509 } else {
5510 return false;
5511 }
Chen Xue9d737e2022-01-01 23:41:31 -08005512 } else if (result instanceof Boolean) {
5513 success = (Boolean) result;
5514 } else {
5515 loge("iccCloseLogicalChannelWithPermission: supported return type " + result);
5516 }
Rambo Wanga1782702021-11-10 20:15:19 -08005517 if (DBG) log("iccCloseLogicalChannelWithPermission: success=" + success);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005518 return success;
5519 } finally {
5520 Binder.restoreCallingIdentity(identity);
Shishir Agrawal566b7612013-10-28 14:41:00 -07005521 }
Shishir Agrawal566b7612013-10-28 14:41:00 -07005522 }
5523
5524 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08005525 public String iccTransmitApduLogicalChannel(int subId, int channel, int cla,
Shishir Agrawal566b7612013-10-28 14:41:00 -07005526 int command, int p1, int p2, int p3, String data) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08005527 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
5528 mApp, subId, "iccTransmitApduLogicalChannel");
Jordan Liu4c733742019-02-28 12:03:40 -08005529 if (DBG) {
5530 log("iccTransmitApduLogicalChannel: subId=" + subId + " chnl=" + channel
5531 + " cla=" + cla + " cmd=" + command + " p1=" + p1 + " p2=" + p2 + " p3="
5532 + p3 + " data=" + data);
5533 }
5534 return iccTransmitApduLogicalChannelWithPermission(getPhoneFromSubId(subId), channel, cla,
5535 command, p1, p2, p3, data);
5536 }
Shishir Agrawal566b7612013-10-28 14:41:00 -07005537
Jordan Liu4c733742019-02-28 12:03:40 -08005538 @Override
Muralidhar Reddybd38d952021-12-02 21:04:16 +00005539 public String iccTransmitApduLogicalChannelByPort(int slotIndex, int portIndex, int channel,
5540 int cla, int command, int p1, int p2, int p3, String data) {
Jordan Liu4c733742019-02-28 12:03:40 -08005541 enforceModifyPermission();
5542 if (DBG) {
Muralidhar Reddybd38d952021-12-02 21:04:16 +00005543 log("iccTransmitApduLogicalChannelByPort: slotIndex=" + slotIndex + " portIndex="
5544 + portIndex + " chnl=" + channel + " cla=" + cla + " cmd=" + command + " p1="
5545 + p1 + " p2=" + p2 + " p3=" + p3 + " data=" + data);
Jordan Liu4c733742019-02-28 12:03:40 -08005546 }
5547 return iccTransmitApduLogicalChannelWithPermission(
Muralidhar Reddybd38d952021-12-02 21:04:16 +00005548 getPhoneFromSlotPortIndexOrThrowException(slotIndex, portIndex), channel, cla,
5549 command, p1, p2, p3, data);
Jordan Liu4c733742019-02-28 12:03:40 -08005550 }
5551
5552 private String iccTransmitApduLogicalChannelWithPermission(Phone phone, int channel, int cla,
5553 int command, int p1, int p2, int p3, String data) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005554 final long identity = Binder.clearCallingIdentity();
5555 try {
Hall Liu4fd771b2019-05-02 09:16:29 -07005556 if (channel <= 0) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005557 return "";
5558 }
Shishir Agrawal566b7612013-10-28 14:41:00 -07005559
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005560 IccIoResult response = (IccIoResult) sendRequest(CMD_TRANSMIT_APDU_LOGICAL_CHANNEL,
Jordan Liu4c733742019-02-28 12:03:40 -08005561 new IccAPDUArgument(channel, cla, command, p1, p2, p3, data), phone,
5562 null /* workSource */);
5563 if (DBG) log("iccTransmitApduLogicalChannelWithPermission: " + response);
Shishir Agrawal566b7612013-10-28 14:41:00 -07005564
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005565 // Append the returned status code to the end of the response payload.
5566 String s = Integer.toHexString(
5567 (response.sw1 << 8) + response.sw2 + 0x10000).substring(1);
5568 if (response.payload != null) {
5569 s = IccUtils.bytesToHexString(response.payload) + s;
5570 }
5571 return s;
5572 } finally {
5573 Binder.restoreCallingIdentity(identity);
Shishir Agrawal5ec14172014-08-05 17:05:45 -07005574 }
Shishir Agrawal566b7612013-10-28 14:41:00 -07005575 }
Jake Hambye994d462014-02-03 13:10:13 -08005576
Evan Charltonc66da362014-05-16 14:06:40 -07005577 @Override
Holly Jiuyu Sun1cc2d552018-01-26 15:51:16 -08005578 public String iccTransmitApduBasicChannel(int subId, String callingPackage, int cla,
5579 int command, int p1, int p2, int p3, String data) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08005580 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
5581 mApp, subId, "iccTransmitApduBasicChannel");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005582 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
Jordan Liu4c733742019-02-28 12:03:40 -08005583 if (DBG) {
5584 log("iccTransmitApduBasicChannel: subId=" + subId + " cla=" + cla + " cmd="
5585 + command + " p1=" + p1 + " p2=" + p2 + " p3=" + p3 + " data=" + data);
5586 }
5587 return iccTransmitApduBasicChannelWithPermission(getPhoneFromSubId(subId), callingPackage,
5588 cla, command, p1, p2, p3, data);
5589 }
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07005590
Jordan Liu4c733742019-02-28 12:03:40 -08005591 @Override
Muralidhar Reddybd38d952021-12-02 21:04:16 +00005592 public String iccTransmitApduBasicChannelByPort(int slotIndex, int portIndex,
5593 String callingPackage, int cla, int command, int p1, int p2, int p3, String data) {
Jordan Liu4c733742019-02-28 12:03:40 -08005594 enforceModifyPermission();
5595 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
5596 if (DBG) {
Muralidhar Reddybd38d952021-12-02 21:04:16 +00005597 log("iccTransmitApduBasicChannelByPort: slotIndex=" + slotIndex + " portIndex="
5598 + portIndex + " cla=" + cla + " cmd=" + command + " p1=" + p1 + " p2="
5599 + p2 + " p3=" + p3 + " data=" + data);
Jordan Liu4c733742019-02-28 12:03:40 -08005600 }
5601
5602 return iccTransmitApduBasicChannelWithPermission(
Muralidhar Reddybd38d952021-12-02 21:04:16 +00005603 getPhoneFromSlotPortIndexOrThrowException(slotIndex, portIndex), callingPackage,
5604 cla, command, p1, p2, p3, data);
Jordan Liu4c733742019-02-28 12:03:40 -08005605 }
5606
5607 // open APDU basic channel assuming the caller has sufficient permissions
5608 private String iccTransmitApduBasicChannelWithPermission(Phone phone, String callingPackage,
5609 int cla, int command, int p1, int p2, int p3, String data) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005610 final long identity = Binder.clearCallingIdentity();
5611 try {
5612 if (command == SELECT_COMMAND && p1 == SELECT_P1 && p2 == SELECT_P2 && p3 == SELECT_P3
5613 && TextUtils.equals(ISDR_AID, data)) {
5614 // Only allows LPA to select ISD-R.
Nazanin Bakhshif782e562018-12-11 15:15:39 -08005615 ComponentInfo bestComponent = EuiccConnector.findBestComponent(getDefaultPhone()
5616 .getContext().getPackageManager());
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005617 if (bestComponent == null
5618 || !TextUtils.equals(callingPackage, bestComponent.packageName)) {
5619 loge("The calling package is not allowed to select ISD-R.");
5620 throw new SecurityException(
5621 "The calling package is not allowed to select ISD-R.");
5622 }
Holly Jiuyu Sun1cc2d552018-01-26 15:51:16 -08005623 }
Holly Jiuyu Sun1cc2d552018-01-26 15:51:16 -08005624
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005625 IccIoResult response = (IccIoResult) sendRequest(CMD_TRANSMIT_APDU_BASIC_CHANNEL,
Jordan Liu4c733742019-02-28 12:03:40 -08005626 new IccAPDUArgument(0, cla, command, p1, p2, p3, data), phone,
5627 null /* workSource */);
5628 if (DBG) log("iccTransmitApduBasicChannelWithPermission: " + response);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07005629
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005630 // Append the returned status code to the end of the response payload.
5631 String s = Integer.toHexString(
5632 (response.sw1 << 8) + response.sw2 + 0x10000).substring(1);
5633 if (response.payload != null) {
5634 s = IccUtils.bytesToHexString(response.payload) + s;
5635 }
5636 return s;
5637 } finally {
5638 Binder.restoreCallingIdentity(identity);
Shishir Agrawal5ec14172014-08-05 17:05:45 -07005639 }
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07005640 }
5641
5642 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08005643 public byte[] iccExchangeSimIO(int subId, int fileID, int command, int p1, int p2, int p3,
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07005644 String filePath) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08005645 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
5646 mApp, subId, "iccExchangeSimIO");
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07005647
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005648 final long identity = Binder.clearCallingIdentity();
5649 try {
5650 if (DBG) {
5651 log("Exchange SIM_IO " + subId + ":" + fileID + ":" + command + " "
5652 + p1 + " " + p2 + " " + p3 + ":" + filePath);
5653 }
5654
5655 IccIoResult response =
5656 (IccIoResult) sendRequest(CMD_EXCHANGE_SIM_IO,
5657 new IccAPDUArgument(-1, fileID, command, p1, p2, p3, filePath),
5658 subId);
5659
5660 if (DBG) {
5661 log("Exchange SIM_IO [R]" + response);
5662 }
5663
5664 byte[] result = null;
5665 int length = 2;
5666 if (response.payload != null) {
5667 length = 2 + response.payload.length;
5668 result = new byte[length];
5669 System.arraycopy(response.payload, 0, result, 0, response.payload.length);
5670 } else {
5671 result = new byte[length];
5672 }
5673
5674 result[length - 1] = (byte) response.sw2;
5675 result[length - 2] = (byte) response.sw1;
5676 return result;
5677 } finally {
5678 Binder.restoreCallingIdentity(identity);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07005679 }
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07005680 }
5681
Nathan Haroldb3014052017-01-25 15:57:32 -08005682 /**
5683 * Get the forbidden PLMN List from the given app type (ex APPTYPE_USIM)
5684 * on a particular subscription
5685 */
Philip P. Moltmann700a9592019-10-03 11:53:50 -07005686 public String[] getForbiddenPlmns(int subId, int appType, String callingPackage,
5687 String callingFeatureId) {
sqianb6e41952018-03-12 14:54:01 -07005688 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
Philip P. Moltmann700a9592019-10-03 11:53:50 -07005689 mApp, subId, callingPackage, callingFeatureId, "getForbiddenPlmns")) {
sqianb6e41952018-03-12 14:54:01 -07005690 return null;
5691 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005692
5693 final long identity = Binder.clearCallingIdentity();
5694 try {
5695 if (appType != TelephonyManager.APPTYPE_USIM
5696 && appType != TelephonyManager.APPTYPE_SIM) {
5697 loge("getForbiddenPlmnList(): App Type must be USIM or SIM");
5698 return null;
5699 }
5700 Object response = sendRequest(
5701 CMD_GET_FORBIDDEN_PLMNS, new Integer(appType), subId);
5702 if (response instanceof String[]) {
5703 return (String[]) response;
5704 }
yincheng zhao2737e882019-09-06 17:06:54 -07005705 // Response is an Exception of some kind
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005706 // which is signalled to the user as a NULL retval
Nathan Haroldb3014052017-01-25 15:57:32 -08005707 return null;
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005708 } finally {
5709 Binder.restoreCallingIdentity(identity);
Nathan Haroldb3014052017-01-25 15:57:32 -08005710 }
Nathan Haroldb3014052017-01-25 15:57:32 -08005711 }
5712
yincheng zhao2737e882019-09-06 17:06:54 -07005713 /**
5714 * Set the forbidden PLMN list from the given app type (ex APPTYPE_USIM) on a particular
5715 * subscription.
5716 *
5717 * @param subId the id of the subscription.
5718 * @param appType the uicc app type, must be USIM or SIM.
5719 * @param fplmns the Forbiden plmns list that needed to be written to the SIM.
5720 * @param callingPackage the op Package name.
Philip P. Moltmann700a9592019-10-03 11:53:50 -07005721 * @param callingFeatureId the feature in the package.
yincheng zhao2737e882019-09-06 17:06:54 -07005722 * @return number of fplmns that is successfully written to the SIM.
5723 */
Philip P. Moltmann700a9592019-10-03 11:53:50 -07005724 public int setForbiddenPlmns(int subId, int appType, List<String> fplmns, String callingPackage,
5725 String callingFeatureId) {
Jayachandran C5b0d75a2021-10-21 22:15:27 -07005726 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
5727 mApp, subId, "setForbiddenPlmns");
5728
yincheng zhao2737e882019-09-06 17:06:54 -07005729 if (appType != TelephonyManager.APPTYPE_USIM && appType != TelephonyManager.APPTYPE_SIM) {
5730 loge("setForbiddenPlmnList(): App Type must be USIM or SIM");
5731 throw new IllegalArgumentException("Invalid appType: App Type must be USIM or SIM");
5732 }
5733 if (fplmns == null) {
5734 throw new IllegalArgumentException("Fplmn List provided is null");
5735 }
5736 for (String fplmn : fplmns) {
5737 if (!CellIdentity.isValidPlmn(fplmn)) {
5738 throw new IllegalArgumentException("Invalid fplmn provided: " + fplmn);
5739 }
5740 }
5741 final long identity = Binder.clearCallingIdentity();
5742 try {
5743 Object response = sendRequest(
5744 CMD_SET_FORBIDDEN_PLMNS,
5745 new Pair<Integer, List<String>>(new Integer(appType), fplmns),
5746 subId);
5747 return (int) response;
5748 } finally {
5749 Binder.restoreCallingIdentity(identity);
5750 }
5751 }
5752
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07005753 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08005754 public String sendEnvelopeWithStatus(int subId, String content) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08005755 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
5756 mApp, subId, "sendEnvelopeWithStatus");
Evan Charltonc66da362014-05-16 14:06:40 -07005757
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005758 final long identity = Binder.clearCallingIdentity();
5759 try {
5760 IccIoResult response = (IccIoResult) sendRequest(CMD_SEND_ENVELOPE, content, subId);
5761 if (response.payload == null) {
5762 return "";
5763 }
Evan Charltonc66da362014-05-16 14:06:40 -07005764
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005765 // Append the returned status code to the end of the response payload.
5766 String s = Integer.toHexString(
5767 (response.sw1 << 8) + response.sw2 + 0x10000).substring(1);
5768 s = IccUtils.bytesToHexString(response.payload) + s;
5769 return s;
5770 } finally {
5771 Binder.restoreCallingIdentity(identity);
5772 }
Evan Charltonc66da362014-05-16 14:06:40 -07005773 }
5774
Jake Hambye994d462014-02-03 13:10:13 -08005775 /**
5776 * Read one of the NV items defined in {@link com.android.internal.telephony.RadioNVItems}
5777 * and {@code ril_nv_items.h}. Used for device configuration by some CDMA operators.
5778 *
5779 * @param itemID the ID of the item to read
5780 * @return the NV item as a String, or null on error.
5781 */
5782 @Override
5783 public String nvReadItem(int itemID) {
vagdeviaf9a5b92018-08-15 16:01:53 -07005784 WorkSource workSource = getWorkSource(Binder.getCallingUid());
Jeff Davidson7e17e312018-02-13 18:17:36 -08005785 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
5786 mApp, getDefaultSubscription(), "nvReadItem");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005787
5788 final long identity = Binder.clearCallingIdentity();
5789 try {
5790 if (DBG) log("nvReadItem: item " + itemID);
vagdeviaf9a5b92018-08-15 16:01:53 -07005791 String value = (String) sendRequest(CMD_NV_READ_ITEM, itemID, workSource);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005792 if (DBG) log("nvReadItem: item " + itemID + " is \"" + value + '"');
5793 return value;
5794 } finally {
5795 Binder.restoreCallingIdentity(identity);
5796 }
Jake Hambye994d462014-02-03 13:10:13 -08005797 }
5798
5799 /**
5800 * Write one of the NV items defined in {@link com.android.internal.telephony.RadioNVItems}
5801 * and {@code ril_nv_items.h}. Used for device configuration by some CDMA operators.
5802 *
5803 * @param itemID the ID of the item to read
5804 * @param itemValue the value to write, as a String
5805 * @return true on success; false on any failure
5806 */
5807 @Override
5808 public boolean nvWriteItem(int itemID, String itemValue) {
vagdeviaf9a5b92018-08-15 16:01:53 -07005809 WorkSource workSource = getWorkSource(Binder.getCallingUid());
Jeff Davidson7e17e312018-02-13 18:17:36 -08005810 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
5811 mApp, getDefaultSubscription(), "nvWriteItem");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005812
5813 final long identity = Binder.clearCallingIdentity();
5814 try {
5815 if (DBG) log("nvWriteItem: item " + itemID + " value \"" + itemValue + '"');
5816 Boolean success = (Boolean) sendRequest(CMD_NV_WRITE_ITEM,
vagdeviaf9a5b92018-08-15 16:01:53 -07005817 new Pair<Integer, String>(itemID, itemValue), workSource);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005818 if (DBG) log("nvWriteItem: item " + itemID + ' ' + (success ? "ok" : "fail"));
5819 return success;
5820 } finally {
5821 Binder.restoreCallingIdentity(identity);
5822 }
Jake Hambye994d462014-02-03 13:10:13 -08005823 }
5824
5825 /**
5826 * Update the CDMA Preferred Roaming List (PRL) in the radio NV storage.
5827 * Used for device configuration by some CDMA operators.
5828 *
5829 * @param preferredRoamingList byte array containing the new PRL
5830 * @return true on success; false on any failure
5831 */
5832 @Override
5833 public boolean nvWriteCdmaPrl(byte[] preferredRoamingList) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08005834 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
5835 mApp, getDefaultSubscription(), "nvWriteCdmaPrl");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005836
5837 final long identity = Binder.clearCallingIdentity();
5838 try {
5839 if (DBG) log("nvWriteCdmaPrl: value: " + HexDump.toHexString(preferredRoamingList));
5840 Boolean success = (Boolean) sendRequest(CMD_NV_WRITE_CDMA_PRL, preferredRoamingList);
5841 if (DBG) log("nvWriteCdmaPrl: " + (success ? "ok" : "fail"));
5842 return success;
5843 } finally {
5844 Binder.restoreCallingIdentity(identity);
5845 }
Jake Hambye994d462014-02-03 13:10:13 -08005846 }
5847
5848 /**
chen xu6dac5ab2018-10-26 17:39:23 -07005849 * Rollback modem configurations to factory default except some config which are in whitelist.
Jake Hambye994d462014-02-03 13:10:13 -08005850 * Used for device configuration by some CDMA operators.
5851 *
chen xu6dac5ab2018-10-26 17:39:23 -07005852 * @param slotIndex - device slot.
5853 *
Jake Hambye994d462014-02-03 13:10:13 -08005854 * @return true on success; false on any failure
5855 */
5856 @Override
chen xu6dac5ab2018-10-26 17:39:23 -07005857 public boolean resetModemConfig(int slotIndex) {
5858 Phone phone = PhoneFactory.getPhone(slotIndex);
5859 if (phone != null) {
5860 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
5861 mApp, phone.getSubId(), "resetModemConfig");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005862
chen xu6dac5ab2018-10-26 17:39:23 -07005863 final long identity = Binder.clearCallingIdentity();
5864 try {
5865 Boolean success = (Boolean) sendRequest(CMD_RESET_MODEM_CONFIG, null);
5866 if (DBG) log("resetModemConfig:" + ' ' + (success ? "ok" : "fail"));
5867 return success;
5868 } finally {
5869 Binder.restoreCallingIdentity(identity);
5870 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005871 }
chen xu6dac5ab2018-10-26 17:39:23 -07005872 return false;
5873 }
5874
5875 /**
5876 * Generate a radio modem reset. Used for device configuration by some CDMA operators.
5877 *
5878 * @param slotIndex - device slot.
5879 *
5880 * @return true on success; false on any failure
5881 */
5882 @Override
5883 public boolean rebootModem(int slotIndex) {
5884 Phone phone = PhoneFactory.getPhone(slotIndex);
5885 if (phone != null) {
5886 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
5887 mApp, phone.getSubId(), "rebootModem");
5888
5889 final long identity = Binder.clearCallingIdentity();
5890 try {
5891 Boolean success = (Boolean) sendRequest(CMD_MODEM_REBOOT, null);
5892 if (DBG) log("rebootModem:" + ' ' + (success ? "ok" : "fail"));
5893 return success;
5894 } finally {
5895 Binder.restoreCallingIdentity(identity);
5896 }
5897 }
5898 return false;
Jake Hambye994d462014-02-03 13:10:13 -08005899 }
Jake Hamby7c27be32014-03-03 13:25:59 -08005900
Brad Ebinger51f743a2017-01-23 13:50:20 -08005901 /**
Grace Jiaaa2eb6b2020-01-09 16:26:08 -08005902 * Toggle IMS disable and enable for the framework to reset it. See {@link #enableIms(int)} and
5903 * {@link #disableIms(int)}.
5904 * @param slotIndex device slot.
5905 */
5906 public void resetIms(int slotIndex) {
5907 enforceModifyPermission();
5908
5909 final long identity = Binder.clearCallingIdentity();
5910 try {
5911 if (mImsResolver == null) {
5912 // may happen if the does not support IMS.
5913 return;
5914 }
Hyunhoa17ac7c2022-08-30 12:03:04 +00005915 mImsResolver.resetIms(slotIndex);
Grace Jiaaa2eb6b2020-01-09 16:26:08 -08005916 } finally {
5917 Binder.restoreCallingIdentity(identity);
5918 }
5919 }
5920
5921 /**
Brad Ebinger22bc3e42018-01-16 09:39:35 -08005922 * Enables IMS for the framework. This will trigger IMS registration and ImsFeature capability
5923 * status updates, if not already enabled.
Brad Ebinger51f743a2017-01-23 13:50:20 -08005924 */
Brad Ebinger22bc3e42018-01-16 09:39:35 -08005925 public void enableIms(int slotId) {
Brad Ebinger51f743a2017-01-23 13:50:20 -08005926 enforceModifyPermission();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005927
5928 final long identity = Binder.clearCallingIdentity();
5929 try {
Brad Ebinger24c29992019-12-05 13:03:21 -08005930 if (mImsResolver == null) {
Brad Ebinger9c0eb502019-01-23 15:06:19 -08005931 // may happen if the device does not support IMS.
5932 return;
5933 }
Brad Ebinger24c29992019-12-05 13:03:21 -08005934 mImsResolver.enableIms(slotId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005935 } finally {
5936 Binder.restoreCallingIdentity(identity);
5937 }
Brad Ebinger34bef922017-11-09 10:27:08 -08005938 }
5939
5940 /**
Brad Ebinger22bc3e42018-01-16 09:39:35 -08005941 * Disables IMS for the framework. This will trigger IMS de-registration and trigger ImsFeature
5942 * status updates to disabled.
Brad Ebinger34bef922017-11-09 10:27:08 -08005943 */
Brad Ebinger22bc3e42018-01-16 09:39:35 -08005944 public void disableIms(int slotId) {
5945 enforceModifyPermission();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005946
5947 final long identity = Binder.clearCallingIdentity();
5948 try {
Brad Ebinger24c29992019-12-05 13:03:21 -08005949 if (mImsResolver == null) {
Brad Ebinger9c0eb502019-01-23 15:06:19 -08005950 // may happen if the device does not support IMS.
5951 return;
5952 }
Brad Ebinger24c29992019-12-05 13:03:21 -08005953 mImsResolver.disableIms(slotId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005954 } finally {
5955 Binder.restoreCallingIdentity(identity);
5956 }
Brad Ebinger22bc3e42018-01-16 09:39:35 -08005957 }
5958
5959 /**
Brad Ebinger67b3e042020-09-11 12:45:11 -07005960 * Registers for updates to the MmTelFeature connection through the IImsServiceFeatureCallback
5961 * callback.
Brad Ebinger22bc3e42018-01-16 09:39:35 -08005962 */
Brad Ebinger67b3e042020-09-11 12:45:11 -07005963 @Override
Brad Ebingerf6aca002020-10-01 13:51:05 -07005964 public void registerMmTelFeatureCallback(int slotId, IImsServiceFeatureCallback callback) {
Brad Ebinger34bef922017-11-09 10:27:08 -08005965 enforceModifyPermission();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005966
5967 final long identity = Binder.clearCallingIdentity();
5968 try {
Brad Ebinger24c29992019-12-05 13:03:21 -08005969 if (mImsResolver == null) {
Brad Ebinger67b3e042020-09-11 12:45:11 -07005970 throw new ServiceSpecificException(ImsException.CODE_ERROR_UNSUPPORTED_OPERATION,
5971 "Device does not support IMS");
Brad Ebinger9c0eb502019-01-23 15:06:19 -08005972 }
Brad Ebingerf6aca002020-10-01 13:51:05 -07005973 mImsResolver.listenForFeature(slotId, ImsFeature.FEATURE_MMTEL, callback);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005974 } finally {
5975 Binder.restoreCallingIdentity(identity);
5976 }
Brad Ebinger34bef922017-11-09 10:27:08 -08005977 }
Brad Ebinger5f64b052017-12-14 14:26:15 -08005978 /**
Brad Ebinger075ff3a2020-05-18 17:52:58 -07005979 * Unregister a previously registered IImsServiceFeatureCallback associated with an ImsFeature.
5980 */
Brad Ebinger67b3e042020-09-11 12:45:11 -07005981 @Override
5982 public void unregisterImsFeatureCallback(IImsServiceFeatureCallback callback) {
Brad Ebinger075ff3a2020-05-18 17:52:58 -07005983 enforceModifyPermission();
5984
5985 final long identity = Binder.clearCallingIdentity();
5986 try {
5987 if (mImsResolver == null) return;
Brad Ebinger67b3e042020-09-11 12:45:11 -07005988 mImsResolver.unregisterImsFeatureCallback(callback);
Brad Ebinger075ff3a2020-05-18 17:52:58 -07005989 } finally {
5990 Binder.restoreCallingIdentity(identity);
5991 }
5992 }
5993
5994 /**
Brad Ebinger5f64b052017-12-14 14:26:15 -08005995 * Returns the {@link IImsRegistration} structure associated with the slotId and feature
Brad Ebinger9c0eb502019-01-23 15:06:19 -08005996 * specified or null if IMS is not supported on the slot specified.
Brad Ebinger5f64b052017-12-14 14:26:15 -08005997 */
5998 public IImsRegistration getImsRegistration(int slotId, int feature) throws RemoteException {
5999 enforceModifyPermission();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006000
6001 final long identity = Binder.clearCallingIdentity();
6002 try {
Brad Ebinger24c29992019-12-05 13:03:21 -08006003 if (mImsResolver == null) {
Brad Ebinger9c0eb502019-01-23 15:06:19 -08006004 // may happen if the device does not support IMS.
6005 return null;
6006 }
Brad Ebinger24c29992019-12-05 13:03:21 -08006007 return mImsResolver.getImsRegistration(slotId, feature);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006008 } finally {
6009 Binder.restoreCallingIdentity(identity);
6010 }
Brad Ebinger5f64b052017-12-14 14:26:15 -08006011 }
6012
Brad Ebinger22bc3e42018-01-16 09:39:35 -08006013 /**
6014 * Returns the {@link IImsConfig} structure associated with the slotId and feature
Brad Ebinger9c0eb502019-01-23 15:06:19 -08006015 * specified or null if IMS is not supported on the slot specified.
Brad Ebinger22bc3e42018-01-16 09:39:35 -08006016 */
6017 public IImsConfig getImsConfig(int slotId, int feature) throws RemoteException {
6018 enforceModifyPermission();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006019
6020 final long identity = Binder.clearCallingIdentity();
6021 try {
Brad Ebinger24c29992019-12-05 13:03:21 -08006022 if (mImsResolver == null) {
Brad Ebinger9c0eb502019-01-23 15:06:19 -08006023 // may happen if the device does not support IMS.
6024 return null;
6025 }
Brad Ebinger24c29992019-12-05 13:03:21 -08006026 return mImsResolver.getImsConfig(slotId, feature);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006027 } finally {
6028 Binder.restoreCallingIdentity(identity);
6029 }
Brad Ebinger22bc3e42018-01-16 09:39:35 -08006030 }
6031
Brad Ebinger884c07b2018-02-15 16:17:40 -08006032 /**
Brad Ebingerdac2f002018-04-03 15:17:52 -07006033 * Sets the ImsService Package Name that Telephony will bind to.
6034 *
Brad Ebinger24c29992019-12-05 13:03:21 -08006035 * @param slotIndex the slot ID that the ImsService should bind for.
6036 * @param isCarrierService true if the ImsService is the carrier override, false if the
Brad Ebingerdac2f002018-04-03 15:17:52 -07006037 * ImsService is the device default ImsService.
Brad Ebinger24c29992019-12-05 13:03:21 -08006038 * @param featureTypes An integer array of feature types associated with a packageName.
6039 * @param packageName The name of the package that the current configuration will be replaced
6040 * with.
Brad Ebingerdac2f002018-04-03 15:17:52 -07006041 * @return true if setting the ImsService to bind to succeeded, false if it did not.
Brad Ebingerdac2f002018-04-03 15:17:52 -07006042 */
Brad Ebinger24c29992019-12-05 13:03:21 -08006043 public boolean setBoundImsServiceOverride(int slotIndex, boolean isCarrierService,
6044 int[] featureTypes, String packageName) {
Brad Ebinger24c29992019-12-05 13:03:21 -08006045 TelephonyPermissions.enforceShellOnly(Binder.getCallingUid(), "setBoundImsServiceOverride");
Brad Ebingerde696de2018-04-06 09:56:40 -07006046 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(mApp,
Jack Yu00ece8c2022-11-19 22:29:12 -08006047 SubscriptionManager.getSubscriptionId(slotIndex), "setBoundImsServiceOverride");
Brad Ebingerde696de2018-04-06 09:56:40 -07006048
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006049 final long identity = Binder.clearCallingIdentity();
6050 try {
Brad Ebinger24c29992019-12-05 13:03:21 -08006051 if (mImsResolver == null) {
Brad Ebinger9c0eb502019-01-23 15:06:19 -08006052 // may happen if the device does not support IMS.
6053 return false;
6054 }
Brad Ebinger24c29992019-12-05 13:03:21 -08006055 Map<Integer, String> featureConfig = new HashMap<>();
6056 for (int featureType : featureTypes) {
6057 featureConfig.put(featureType, packageName);
6058 }
6059 return mImsResolver.overrideImsServiceConfiguration(slotIndex, isCarrierService,
6060 featureConfig);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006061 } finally {
6062 Binder.restoreCallingIdentity(identity);
6063 }
Brad Ebingerdac2f002018-04-03 15:17:52 -07006064 }
6065
6066 /**
Brad Ebinger999d3302020-11-25 14:31:39 -08006067 * Clears any carrier ImsService overrides for the slot index specified that were previously
6068 * set with {@link #setBoundImsServiceOverride(int, boolean, int[], String)}.
6069 *
6070 * This should only be used for testing.
6071 *
6072 * @param slotIndex the slot ID that the ImsService should bind for.
6073 * @return true if clearing the carrier ImsService override succeeded or false if it did not.
6074 */
6075 @Override
6076 public boolean clearCarrierImsServiceOverride(int slotIndex) {
Brad Ebinger999d3302020-11-25 14:31:39 -08006077 TelephonyPermissions.enforceShellOnly(Binder.getCallingUid(),
6078 "clearCarrierImsServiceOverride");
6079 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(mApp,
Jack Yu00ece8c2022-11-19 22:29:12 -08006080 SubscriptionManager.getSubscriptionId(slotIndex), "clearCarrierImsServiceOverride");
Brad Ebinger999d3302020-11-25 14:31:39 -08006081
6082 final long identity = Binder.clearCallingIdentity();
6083 try {
6084 if (mImsResolver == null) {
6085 // may happen if the device does not support IMS.
6086 return false;
6087 }
6088 return mImsResolver.clearCarrierImsServiceConfiguration(slotIndex);
6089 } finally {
6090 Binder.restoreCallingIdentity(identity);
6091 }
6092 }
6093
6094 /**
Brad Ebinger24c29992019-12-05 13:03:21 -08006095 * Return the package name of the currently bound ImsService.
Brad Ebingerdac2f002018-04-03 15:17:52 -07006096 *
6097 * @param slotId The slot that the ImsService is associated with.
6098 * @param isCarrierImsService true, if the ImsService is a carrier override, false if it is
6099 * the device default.
Brad Ebinger24c29992019-12-05 13:03:21 -08006100 * @param featureType The feature associated with the queried configuration.
Brad Ebingerdac2f002018-04-03 15:17:52 -07006101 * @return the package name of the ImsService configuration.
6102 */
Brad Ebinger24c29992019-12-05 13:03:21 -08006103 public String getBoundImsServicePackage(int slotId, boolean isCarrierImsService,
6104 @ImsFeature.FeatureType int featureType) {
Brad Ebinger24c29992019-12-05 13:03:21 -08006105 TelephonyPermissions
Jack Yu00ece8c2022-11-19 22:29:12 -08006106 .enforceCallingOrSelfReadPrivilegedPhoneStatePermissionOrCarrierPrivilege(mApp,
6107 SubscriptionManager.getSubscriptionId(slotId), "getBoundImsServicePackage");
Brad Ebingerde696de2018-04-06 09:56:40 -07006108
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006109 final long identity = Binder.clearCallingIdentity();
6110 try {
Brad Ebinger24c29992019-12-05 13:03:21 -08006111 if (mImsResolver == null) {
Brad Ebinger9c0eb502019-01-23 15:06:19 -08006112 // may happen if the device does not support IMS.
6113 return "";
6114 }
Brad Ebingera80c3312019-12-02 10:59:39 -08006115 // TODO: change API to query RCS separately.
Brad Ebinger24c29992019-12-05 13:03:21 -08006116 return mImsResolver.getImsServiceConfiguration(slotId, isCarrierImsService,
6117 featureType);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006118 } finally {
6119 Binder.restoreCallingIdentity(identity);
6120 }
Brad Ebingerdac2f002018-04-03 15:17:52 -07006121 }
6122
Brad Ebingerbc7dd582019-10-17 17:03:22 -07006123 /**
6124 * Get the MmTelFeature state associated with the requested subscription id.
6125 * @param subId The subscription that the MmTelFeature is associated with.
6126 * @param callback A callback with an integer containing the
6127 * {@link android.telephony.ims.feature.ImsFeature.ImsState} associated with the MmTelFeature.
6128 */
6129 @Override
6130 public void getImsMmTelFeatureState(int subId, IIntegerConsumer callback) {
6131 enforceReadPrivilegedPermission("getImsMmTelFeatureState");
Brad Ebingera2628302022-02-18 03:44:55 +00006132 if (!ImsManager.isImsSupportedOnDevice(mApp)) {
6133 throw new ServiceSpecificException(ImsException.CODE_ERROR_UNSUPPORTED_OPERATION,
6134 "IMS not available on device.");
6135 }
Brad Ebingerbc7dd582019-10-17 17:03:22 -07006136 final long token = Binder.clearCallingIdentity();
6137 try {
Brad Ebingera2628302022-02-18 03:44:55 +00006138 int slotId = getSlotIndex(subId);
6139 if (slotId <= SubscriptionManager.INVALID_SIM_SLOT_INDEX) {
6140 Log.w(LOG_TAG, "getImsMmTelFeatureState: called with an inactive subscription '"
6141 + subId + "'");
6142 throw new ServiceSpecificException(ImsException.CODE_ERROR_INVALID_SUBSCRIPTION);
6143 }
6144 verifyImsMmTelConfiguredOrThrow(slotId);
6145 ImsManager.getInstance(mApp, slotId).getImsServiceState(anInteger -> {
6146 try {
6147 callback.accept(anInteger == null ? ImsFeature.STATE_UNAVAILABLE : anInteger);
6148 } catch (RemoteException e) {
6149 Log.w(LOG_TAG, "getImsMmTelFeatureState: remote caller is no longer running. "
6150 + "Ignore");
6151 }
Brad Ebingerbc7dd582019-10-17 17:03:22 -07006152 });
Brad Ebinger919631e2021-06-02 17:46:35 -07006153 } catch (ImsException e) {
6154 throw new ServiceSpecificException(e.getCode());
Brad Ebingerbc7dd582019-10-17 17:03:22 -07006155 } finally {
6156 Binder.restoreCallingIdentity(token);
6157 }
6158 }
6159
Daniel Brightbb5840b2021-01-12 15:48:18 -08006160 /**
6161 * Sets the ims registration state on all valid {@link Phone}s.
6162 */
6163 public void setImsRegistrationState(final boolean registered) {
Wink Saville36469e72014-06-11 15:17:00 -07006164 enforceModifyPermission();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006165
6166 final long identity = Binder.clearCallingIdentity();
6167 try {
Daniel Brightbb5840b2021-01-12 15:48:18 -08006168 // NOTE: Before S, this method only set the default phone.
6169 for (final Phone phone : PhoneFactory.getPhones()) {
6170 if (SubscriptionManager.isValidSubscriptionId(phone.getSubId())) {
6171 phone.setImsRegistrationState(registered);
6172 }
6173 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006174 } finally {
6175 Binder.restoreCallingIdentity(identity);
6176 }
Wink Saville36469e72014-06-11 15:17:00 -07006177 }
6178
6179 /**
Stuart Scott54788802015-03-30 13:18:01 -07006180 * Set the network selection mode to automatic.
6181 *
6182 */
6183 @Override
6184 public void setNetworkSelectionModeAutomatic(int subId) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08006185 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
6186 mApp, subId, "setNetworkSelectionModeAutomatic");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006187
6188 final long identity = Binder.clearCallingIdentity();
6189 try {
shilufc958392020-01-20 11:36:01 -08006190 if (!isActiveSubscription(subId)) {
6191 return;
6192 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006193 if (DBG) log("setNetworkSelectionModeAutomatic: subId " + subId);
Rambo Wang0f050d82021-02-12 11:43:36 -08006194 sendRequest(CMD_SET_NETWORK_SELECTION_MODE_AUTOMATIC, null, subId,
6195 SET_NETWORK_SELECTION_MODE_AUTOMATIC_TIMEOUT_MS);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006196 } finally {
6197 Binder.restoreCallingIdentity(identity);
6198 }
Stuart Scott54788802015-03-30 13:18:01 -07006199 }
6200
Jack Yud10cdd42020-09-28 20:28:01 -07006201 /**
Pengquan Mengea84e042018-09-20 14:57:26 -07006202 * Ask the radio to connect to the input network and change selection mode to manual.
6203 *
6204 * @param subId the id of the subscription.
6205 * @param operatorInfo the operator information, included the PLMN, long name and short name of
6206 * the operator to attach to.
6207 * @param persistSelection whether the selection will persist until reboot. If true, only allows
6208 * attaching to the selected PLMN until reboot; otherwise, attach to the chosen PLMN and resume
6209 * normal network selection next time.
6210 * @return {@code true} on success; {@code true} on any failure.
Shishir Agrawal302c8692015-06-19 13:49:39 -07006211 */
6212 @Override
Pengquan Mengea84e042018-09-20 14:57:26 -07006213 public boolean setNetworkSelectionModeManual(
6214 int subId, OperatorInfo operatorInfo, boolean persistSelection) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08006215 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
6216 mApp, subId, "setNetworkSelectionModeManual");
Pengquan Menge92a50d2018-09-21 15:54:48 -07006217
Jack Yu285100e2022-12-02 22:48:35 -08006218 final long identity = Binder.clearCallingIdentity();
Pengquan Menge92a50d2018-09-21 15:54:48 -07006219 if (!isActiveSubscription(subId)) {
6220 return false;
6221 }
6222
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006223 try {
Pengquan Mengea84e042018-09-20 14:57:26 -07006224 ManualNetworkSelectionArgument arg = new ManualNetworkSelectionArgument(operatorInfo,
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006225 persistSelection);
Pengquan Mengea84e042018-09-20 14:57:26 -07006226 if (DBG) {
6227 log("setNetworkSelectionModeManual: subId: " + subId
6228 + " operator: " + operatorInfo);
6229 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006230 return (Boolean) sendRequest(CMD_SET_NETWORK_SELECTION_MODE_MANUAL, arg, subId);
6231 } finally {
6232 Binder.restoreCallingIdentity(identity);
6233 }
Shishir Agrawal302c8692015-06-19 13:49:39 -07006234 }
shilu84f6e8b2019-12-19 13:58:01 -08006235 /**
6236 * Get the manual network selection
6237 *
6238 * @param subId the id of the subscription.
6239 *
6240 * @return the previously saved user selected PLMN
6241 */
6242 @Override
6243 public String getManualNetworkSelectionPlmn(int subId) {
6244 TelephonyPermissions
Nathan Harold62c68512021-04-06 11:26:02 -07006245 .enforceCallingOrSelfReadPrecisePhoneStatePermissionOrCarrierPrivilege(
shilu84f6e8b2019-12-19 13:58:01 -08006246 mApp, subId, "getManualNetworkSelectionPlmn");
6247
6248 final long identity = Binder.clearCallingIdentity();
6249 try {
6250 if (!isActiveSubscription(subId)) {
shilufa1c2592020-03-10 10:59:43 -07006251 throw new IllegalArgumentException("Invalid Subscription Id: " + subId);
shilu84f6e8b2019-12-19 13:58:01 -08006252 }
6253
6254 final Phone phone = getPhone(subId);
6255 if (phone == null) {
shilufa1c2592020-03-10 10:59:43 -07006256 throw new IllegalArgumentException("Invalid Subscription Id: " + subId);
shilu84f6e8b2019-12-19 13:58:01 -08006257 }
6258 OperatorInfo networkSelection = phone.getSavedNetworkSelection();
6259 return TextUtils.isEmpty(networkSelection.getOperatorNumeric())
6260 ? phone.getManualNetworkSelectionPlmn() : networkSelection.getOperatorNumeric();
6261 } finally {
6262 Binder.restoreCallingIdentity(identity);
6263 }
6264 }
Shishir Agrawal302c8692015-06-19 13:49:39 -07006265
6266 /**
6267 * Scans for available networks.
6268 */
6269 @Override
Philip P. Moltmann3a2772a2019-10-04 08:15:00 -07006270 public CellNetworkScanResult getCellNetworkScanResults(int subId, String callingPackage,
6271 String callingFeatureId) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08006272 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
6273 mApp, subId, "getCellNetworkScanResults");
Hall Liuf19c44f2018-11-27 14:38:17 -08006274 LocationAccessPolicy.LocationPermissionResult locationResult =
6275 LocationAccessPolicy.checkLocationPermission(mApp,
6276 new LocationAccessPolicy.LocationPermissionQuery.Builder()
6277 .setCallingPackage(callingPackage)
Philip P. Moltmann3a2772a2019-10-04 08:15:00 -07006278 .setCallingFeatureId(callingFeatureId)
Hall Liuf19c44f2018-11-27 14:38:17 -08006279 .setCallingPid(Binder.getCallingPid())
6280 .setCallingUid(Binder.getCallingUid())
6281 .setMethod("getCellNetworkScanResults")
6282 .setMinSdkVersionForFine(Build.VERSION_CODES.Q)
Hall Liuc4a3e422020-05-26 17:18:03 -07006283 .setMinSdkVersionForCoarse(Build.VERSION_CODES.Q)
6284 .setMinSdkVersionForEnforcement(Build.VERSION_CODES.Q)
Hall Liuf19c44f2018-11-27 14:38:17 -08006285 .build());
6286 switch (locationResult) {
6287 case DENIED_HARD:
6288 throw new SecurityException("Not allowed to access scan results -- location");
6289 case DENIED_SOFT:
6290 return null;
6291 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006292
Pengquan Menga1bb6272018-09-06 09:59:22 -07006293 long identity = Binder.clearCallingIdentity();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006294 try {
6295 if (DBG) log("getCellNetworkScanResults: subId " + subId);
Pengquan Menga1bb6272018-09-06 09:59:22 -07006296 return (CellNetworkScanResult) sendRequest(
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006297 CMD_PERFORM_NETWORK_SCAN, null, subId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006298 } finally {
6299 Binder.restoreCallingIdentity(identity);
6300 }
Shishir Agrawal302c8692015-06-19 13:49:39 -07006301 }
6302
6303 /**
Shuo Qian4a594052020-01-23 11:59:30 -08006304 * Get the call forwarding info, given the call forwarding reason.
6305 */
6306 @Override
Hall Liu27d24262020-09-18 19:04:59 -07006307 public void getCallForwarding(int subId, int callForwardingReason,
6308 ICallForwardingInfoCallback callback) {
Shuo Qian4a594052020-01-23 11:59:30 -08006309 enforceReadPrivilegedPermission("getCallForwarding");
6310 long identity = Binder.clearCallingIdentity();
6311 try {
6312 if (DBG) {
6313 log("getCallForwarding: subId " + subId
6314 + " callForwardingReason" + callForwardingReason);
6315 }
Hall Liu27d24262020-09-18 19:04:59 -07006316
6317 Phone phone = getPhone(subId);
6318 if (phone == null) {
6319 try {
Hall Liu940c4ca2020-09-29 17:10:18 -07006320 callback.onError(
6321 TelephonyManager.CallForwardingInfoCallback.RESULT_ERROR_UNKNOWN);
Hall Liu27d24262020-09-18 19:04:59 -07006322 } catch (RemoteException e) {
6323 // ignore
6324 }
6325 return;
6326 }
6327
6328 Pair<Integer, TelephonyManager.CallForwardingInfoCallback> argument = Pair.create(
6329 callForwardingReason, new TelephonyManager.CallForwardingInfoCallback() {
6330 @Override
6331 public void onCallForwardingInfoAvailable(CallForwardingInfo info) {
6332 try {
6333 callback.onCallForwardingInfoAvailable(info);
6334 } catch (RemoteException e) {
6335 // ignore
6336 }
6337 }
6338
6339 @Override
6340 public void onError(int error) {
6341 try {
6342 callback.onError(error);
6343 } catch (RemoteException e) {
6344 // ignore
6345 }
6346 }
6347 });
6348 sendRequestAsync(CMD_GET_CALL_FORWARDING, argument, phone, null);
Shuo Qian4a594052020-01-23 11:59:30 -08006349 } finally {
6350 Binder.restoreCallingIdentity(identity);
6351 }
6352 }
6353
6354 /**
6355 * Sets the voice call forwarding info including status (enable/disable), call forwarding
6356 * reason, the number to forward, and the timeout before the forwarding is attempted.
6357 */
6358 @Override
Hall Liu27d24262020-09-18 19:04:59 -07006359 public void setCallForwarding(int subId, CallForwardingInfo callForwardingInfo,
6360 IIntegerConsumer callback) {
Shuo Qian4a594052020-01-23 11:59:30 -08006361 enforceModifyPermission();
6362 long identity = Binder.clearCallingIdentity();
6363 try {
6364 if (DBG) {
6365 log("setCallForwarding: subId " + subId
6366 + " callForwardingInfo" + callForwardingInfo);
6367 }
Hall Liu27d24262020-09-18 19:04:59 -07006368
6369 Phone phone = getPhone(subId);
6370 if (phone == null) {
6371 try {
Hall Liu940c4ca2020-09-29 17:10:18 -07006372 callback.accept(
6373 TelephonyManager.CallForwardingInfoCallback.RESULT_ERROR_UNKNOWN);
Hall Liu27d24262020-09-18 19:04:59 -07006374 } catch (RemoteException e) {
6375 // ignore
6376 }
6377 return;
6378 }
6379
6380 Pair<CallForwardingInfo, Consumer<Integer>> arguments = Pair.create(callForwardingInfo,
6381 FunctionalUtils.ignoreRemoteException(callback::accept));
6382
6383 sendRequestAsync(CMD_SET_CALL_FORWARDING, arguments, phone, null);
Shuo Qian4a594052020-01-23 11:59:30 -08006384 } finally {
6385 Binder.restoreCallingIdentity(identity);
6386 }
6387 }
6388
6389 /**
Hall Liu27d24262020-09-18 19:04:59 -07006390 * Get the call waiting status for a subId.
Shuo Qian4a594052020-01-23 11:59:30 -08006391 */
6392 @Override
Hall Liu27d24262020-09-18 19:04:59 -07006393 public void getCallWaitingStatus(int subId, IIntegerConsumer callback) {
SongFerngWang0e767992021-03-31 22:08:45 +08006394 enforceReadPrivilegedPermission("getCallWaitingStatus");
Shuo Qian4a594052020-01-23 11:59:30 -08006395 long identity = Binder.clearCallingIdentity();
6396 try {
Hall Liu27d24262020-09-18 19:04:59 -07006397 Phone phone = getPhone(subId);
6398 if (phone == null) {
6399 try {
6400 callback.accept(TelephonyManager.CALL_WAITING_STATUS_UNKNOWN_ERROR);
6401 } catch (RemoteException e) {
6402 // ignore
6403 }
6404 return;
6405 }
SongFerngWang0e767992021-03-31 22:08:45 +08006406 CarrierConfigManager configManager = new CarrierConfigManager(phone.getContext());
6407 PersistableBundle c = configManager.getConfigForSubId(subId);
6408 boolean requireUssd = c.getBoolean(
6409 CarrierConfigManager.KEY_USE_CALL_WAITING_USSD_BOOL, false);
Hall Liu27d24262020-09-18 19:04:59 -07006410
Shuo Qian4a594052020-01-23 11:59:30 -08006411 if (DBG) log("getCallWaitingStatus: subId " + subId);
SongFerngWang0e767992021-03-31 22:08:45 +08006412 if (requireUssd) {
6413 CarrierXmlParser carrierXmlParser = new CarrierXmlParser(phone.getContext(),
6414 getSubscriptionCarrierId(subId));
6415 String newUssdCommand = "";
6416 try {
6417 newUssdCommand = carrierXmlParser.getFeature(
6418 CarrierXmlParser.FEATURE_CALL_WAITING)
6419 .makeCommand(CarrierXmlParser.SsEntry.SSAction.QUERY, null);
6420 } catch (NullPointerException e) {
6421 loge("Failed to generate USSD number" + e);
6422 }
6423 ResultReceiver wrappedCallback = new CallWaitingUssdResultReceiver(
6424 mMainThreadHandler, callback, carrierXmlParser,
6425 CarrierXmlParser.SsEntry.SSAction.QUERY);
6426 final String ussdCommand = newUssdCommand;
6427 Executors.newSingleThreadExecutor().execute(() -> {
6428 handleUssdRequest(subId, ussdCommand, wrappedCallback);
6429 });
6430 } else {
6431 Consumer<Integer> argument = FunctionalUtils.ignoreRemoteException(
6432 callback::accept);
6433 sendRequestAsync(CMD_GET_CALL_WAITING, argument, phone, null);
6434 }
Shuo Qian4a594052020-01-23 11:59:30 -08006435 } finally {
6436 Binder.restoreCallingIdentity(identity);
6437 }
6438 }
6439
6440 /**
Hall Liu27d24262020-09-18 19:04:59 -07006441 * Sets whether call waiting is enabled for a given subId.
Shuo Qian4a594052020-01-23 11:59:30 -08006442 */
6443 @Override
Hall Liu27d24262020-09-18 19:04:59 -07006444 public void setCallWaitingStatus(int subId, boolean enable, IIntegerConsumer callback) {
Shuo Qian4a594052020-01-23 11:59:30 -08006445 enforceModifyPermission();
6446 long identity = Binder.clearCallingIdentity();
6447 try {
Hall Liu27d24262020-09-18 19:04:59 -07006448 if (DBG) log("setCallWaitingStatus: subId " + subId + " enable: " + enable);
6449
6450 Phone phone = getPhone(subId);
6451 if (phone == null) {
6452 try {
6453 callback.accept(TelephonyManager.CALL_WAITING_STATUS_UNKNOWN_ERROR);
6454 } catch (RemoteException e) {
6455 // ignore
6456 }
6457 return;
6458 }
6459
SongFerngWang0e767992021-03-31 22:08:45 +08006460 CarrierConfigManager configManager = new CarrierConfigManager(phone.getContext());
6461 PersistableBundle c = configManager.getConfigForSubId(subId);
6462 boolean requireUssd = c.getBoolean(
6463 CarrierConfigManager.KEY_USE_CALL_WAITING_USSD_BOOL, false);
Hall Liu27d24262020-09-18 19:04:59 -07006464
SongFerngWang0e767992021-03-31 22:08:45 +08006465 if (DBG) log("getCallWaitingStatus: subId " + subId);
6466 if (requireUssd) {
6467 CarrierXmlParser carrierXmlParser = new CarrierXmlParser(phone.getContext(),
6468 getSubscriptionCarrierId(subId));
6469 CarrierXmlParser.SsEntry.SSAction ssAction =
6470 enable ? CarrierXmlParser.SsEntry.SSAction.UPDATE_ACTIVATE
6471 : CarrierXmlParser.SsEntry.SSAction.UPDATE_DEACTIVATE;
6472 String newUssdCommand = "";
6473 try {
6474 newUssdCommand = carrierXmlParser.getFeature(
6475 CarrierXmlParser.FEATURE_CALL_WAITING)
6476 .makeCommand(ssAction, null);
6477 } catch (NullPointerException e) {
6478 loge("Failed to generate USSD number" + e);
6479 }
6480 ResultReceiver wrappedCallback = new CallWaitingUssdResultReceiver(
6481 mMainThreadHandler, callback, carrierXmlParser, ssAction);
6482 final String ussdCommand = newUssdCommand;
6483 Executors.newSingleThreadExecutor().execute(() -> {
6484 handleUssdRequest(subId, ussdCommand, wrappedCallback);
6485 });
6486 } else {
6487 Pair<Boolean, Consumer<Integer>> arguments = Pair.create(enable,
6488 FunctionalUtils.ignoreRemoteException(callback::accept));
6489
6490 sendRequestAsync(CMD_SET_CALL_WAITING, arguments, phone, null);
6491 }
Shuo Qian4a594052020-01-23 11:59:30 -08006492 } finally {
6493 Binder.restoreCallingIdentity(identity);
6494 }
6495 }
6496
6497 /**
yinxub1bed742017-04-17 11:45:04 -07006498 * Starts a new network scan and returns the id of this scan.
yinxu504e1392017-04-12 16:03:22 -07006499 *
yinxub1bed742017-04-17 11:45:04 -07006500 * @param subId id of the subscription
Sooraj Sasindran2250dc02021-11-10 16:42:01 -08006501 * @param renounceFineLocationAccess Set this to true if the caller would not like to receive
6502 * location related information which will be sent if the caller already possess
6503 * {@android.Manifest.permission.ACCESS_FINE_LOCATION} and do not renounce the permission
yinxub1bed742017-04-17 11:45:04 -07006504 * @param request contains the radio access networks with bands/channels to scan
6505 * @param messenger callback messenger for scan results or errors
6506 * @param binder for the purpose of auto clean when the user thread crashes
yinxu504e1392017-04-12 16:03:22 -07006507 * @return the id of the requested scan which can be used to stop the scan.
6508 */
6509 @Override
Sooraj Sasindran2250dc02021-11-10 16:42:01 -08006510 public int requestNetworkScan(int subId, boolean renounceFineLocationAccess,
6511 NetworkScanRequest request, Messenger messenger,
Philip P. Moltmann3a2772a2019-10-04 08:15:00 -07006512 IBinder binder, String callingPackage, String callingFeatureId) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08006513 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
6514 mApp, subId, "requestNetworkScan");
Hall Liuf19c44f2018-11-27 14:38:17 -08006515 LocationAccessPolicy.LocationPermissionResult locationResult =
Sooraj Sasindran2250dc02021-11-10 16:42:01 -08006516 LocationAccessPolicy.LocationPermissionResult.DENIED_HARD;
6517 if (!renounceFineLocationAccess) {
6518 locationResult = LocationAccessPolicy.checkLocationPermission(mApp,
6519 new LocationAccessPolicy.LocationPermissionQuery.Builder()
6520 .setCallingPackage(callingPackage)
6521 .setCallingFeatureId(callingFeatureId)
6522 .setCallingPid(Binder.getCallingPid())
6523 .setCallingUid(Binder.getCallingUid())
6524 .setMethod("requestNetworkScan")
6525 .setMinSdkVersionForFine(Build.VERSION_CODES.Q)
6526 .setMinSdkVersionForCoarse(Build.VERSION_CODES.Q)
6527 .setMinSdkVersionForEnforcement(Build.VERSION_CODES.Q)
6528 .build());
6529 }
Hall Liub2ac8ef2019-02-28 15:56:23 -08006530 if (locationResult != LocationAccessPolicy.LocationPermissionResult.ALLOWED) {
Nathan Harold1c11dba2020-09-22 17:54:53 -07006531 SecurityException e = checkNetworkRequestForSanitizedLocationAccess(
6532 request, subId, callingPackage);
Hall Liub2ac8ef2019-02-28 15:56:23 -08006533 if (e != null) {
6534 if (locationResult == LocationAccessPolicy.LocationPermissionResult.DENIED_HARD) {
6535 throw e;
6536 } else {
Hall Liu0e5abaf2019-04-04 01:25:30 -07006537 loge(e.getMessage());
Hall Liub2ac8ef2019-02-28 15:56:23 -08006538 return TelephonyScanManager.INVALID_SCAN_ID;
6539 }
6540 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006541 }
Hall Liu912dfd32019-04-25 14:02:26 -07006542 int callingUid = Binder.getCallingUid();
6543 int callingPid = Binder.getCallingPid();
Ying Xu94a46582019-04-18 17:14:56 -07006544 final long identity = Binder.clearCallingIdentity();
6545 try {
6546 return mNetworkScanRequestTracker.startNetworkScan(
Sooraj Sasindran2250dc02021-11-10 16:42:01 -08006547 renounceFineLocationAccess, request, messenger, binder, getPhone(subId),
Hall Liu912dfd32019-04-25 14:02:26 -07006548 callingUid, callingPid, callingPackage);
Ying Xu94a46582019-04-18 17:14:56 -07006549 } finally {
6550 Binder.restoreCallingIdentity(identity);
6551 }
yinxu504e1392017-04-12 16:03:22 -07006552 }
6553
Hall Liub2ac8ef2019-02-28 15:56:23 -08006554 private SecurityException checkNetworkRequestForSanitizedLocationAccess(
Nathan Harold1c11dba2020-09-22 17:54:53 -07006555 NetworkScanRequest request, int subId, String callingPackage) {
Rambo Wang3dee30a2022-10-20 16:52:29 +00006556 boolean hasCarrierPriv;
6557 final long identity = Binder.clearCallingIdentity();
6558 try {
6559 hasCarrierPriv = checkCarrierPrivilegesForPackage(subId, callingPackage)
6560 == TelephonyManager.CARRIER_PRIVILEGE_STATUS_HAS_ACCESS;
6561 } finally {
6562 Binder.restoreCallingIdentity(identity);
6563 }
Hall Liu558027f2019-05-15 19:14:05 -07006564 boolean hasNetworkScanPermission =
6565 mApp.checkCallingOrSelfPermission(android.Manifest.permission.NETWORK_SCAN)
6566 == PERMISSION_GRANTED;
6567
6568 if (!hasCarrierPriv && !hasNetworkScanPermission) {
6569 return new SecurityException("permission.NETWORK_SCAN or carrier privileges is needed"
6570 + " for network scans without location access.");
Hall Liub2ac8ef2019-02-28 15:56:23 -08006571 }
6572
6573 if (request.getSpecifiers() != null && request.getSpecifiers().length > 0) {
6574 for (RadioAccessSpecifier ras : request.getSpecifiers()) {
Hall Liub2ac8ef2019-02-28 15:56:23 -08006575 if (ras.getChannels() != null && ras.getChannels().length > 0) {
6576 return new SecurityException("Specific channels must not be"
6577 + " scanned without location access.");
6578 }
6579 }
6580 }
6581
Hall Liub2ac8ef2019-02-28 15:56:23 -08006582 return null;
6583 }
6584
yinxu504e1392017-04-12 16:03:22 -07006585 /**
6586 * Stops an existing network scan with the given scanId.
yinxub1bed742017-04-17 11:45:04 -07006587 *
6588 * @param subId id of the subscription
6589 * @param scanId id of the scan that needs to be stopped
yinxu504e1392017-04-12 16:03:22 -07006590 */
6591 @Override
6592 public void stopNetworkScan(int subId, int scanId) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08006593 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
6594 mApp, subId, "stopNetworkScan");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006595
Hall Liu912dfd32019-04-25 14:02:26 -07006596 int callingUid = Binder.getCallingUid();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006597 final long identity = Binder.clearCallingIdentity();
6598 try {
Hall Liu912dfd32019-04-25 14:02:26 -07006599 mNetworkScanRequestTracker.stopNetworkScan(scanId, callingUid);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006600 } finally {
6601 Binder.restoreCallingIdentity(identity);
6602 }
yinxu504e1392017-04-12 16:03:22 -07006603 }
6604
6605 /**
SongFerngWang3ef3e072020-12-21 16:41:52 +08006606 * Get the allowed network types bitmask.
Junda Liu84d15a22014-07-02 11:21:04 -07006607 *
SongFerngWang3ef3e072020-12-21 16:41:52 +08006608 * @return the allowed network types bitmask, defined in RILConstants.java.
Junda Liu84d15a22014-07-02 11:21:04 -07006609 */
6610 @Override
SongFerngWang3ef3e072020-12-21 16:41:52 +08006611 public int getAllowedNetworkTypesBitmask(int subId) {
Pengquan Menga4009cb2018-12-20 11:00:24 -08006612 TelephonyPermissions
Nathan Harold62c68512021-04-06 11:26:02 -07006613 .enforceCallingOrSelfReadPrivilegedPhoneStatePermissionOrCarrierPrivilege(
SongFerngWang3ef3e072020-12-21 16:41:52 +08006614 mApp, subId, "getAllowedNetworkTypesBitmask");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006615
6616 final long identity = Binder.clearCallingIdentity();
6617 try {
SongFerngWang3ef3e072020-12-21 16:41:52 +08006618 if (DBG) log("getAllowedNetworkTypesBitmask");
6619 int[] result = (int[]) sendRequest(CMD_GET_ALLOWED_NETWORK_TYPES_BITMASK, null, subId);
6620 int networkTypesBitmask = (result != null ? result[0] : -1);
6621 if (DBG) log("getAllowedNetworkTypesBitmask: " + networkTypesBitmask);
6622 return networkTypesBitmask;
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006623 } finally {
6624 Binder.restoreCallingIdentity(identity);
6625 }
Jake Hamby7c27be32014-03-03 13:25:59 -08006626 }
6627
6628 /**
Sooraj Sasindranc46dfbd2020-06-03 01:06:00 -07006629 * Get the allowed network types for certain reason.
6630 *
6631 * @param subId the id of the subscription.
6632 * @param reason the reason the allowed network type change is taking place
6633 * @return the allowed network types.
6634 */
6635 @Override
6636 public long getAllowedNetworkTypesForReason(int subId,
6637 @TelephonyManager.AllowedNetworkTypesReason int reason) {
Nathan Harold62c68512021-04-06 11:26:02 -07006638 TelephonyPermissions.enforceCallingOrSelfReadPrecisePhoneStatePermissionOrCarrierPrivilege(
SongFerngWang8c6e82e2021-03-02 22:09:29 +08006639 mApp, subId, "getAllowedNetworkTypesForReason");
Sooraj Sasindranc46dfbd2020-06-03 01:06:00 -07006640 final long identity = Binder.clearCallingIdentity();
6641 try {
6642 return getPhoneFromSubId(subId).getAllowedNetworkTypes(reason);
6643 } finally {
6644 Binder.restoreCallingIdentity(identity);
6645 }
6646 }
6647
6648 /**
Sooraj Sasindran37444802020-08-11 10:40:43 -07006649 * Enable/Disable E-UTRA-NR Dual Connectivity
6650 * @param subId subscription id of the sim card
6651 * @param nrDualConnectivityState expected NR dual connectivity state
6652 * This can be passed following states
6653 * <ol>
6654 * <li>Enable NR dual connectivity {@link TelephonyManager#NR_DUAL_CONNECTIVITY_ENABLE}
6655 * <li>Disable NR dual connectivity {@link TelephonyManager#NR_DUAL_CONNECTIVITY_DISABLE}
6656 * <li>Disable NR dual connectivity and force secondary cell to be released
6657 * {@link TelephonyManager#NR_DUAL_CONNECTIVITY_DISABLE_IMMEDIATE}
6658 * </ol>
6659 * @return operation result.
6660 */
6661 @Override
6662 public int setNrDualConnectivityState(int subId,
6663 @TelephonyManager.NrDualConnectivityState int nrDualConnectivityState) {
6664 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
6665 mApp, subId, "enableNRDualConnectivity");
Sooraj Sasindran0e4e00a2021-03-16 18:02:32 -07006666 if (!isRadioInterfaceCapabilitySupported(
Sooraj Sasindrandfd595b2021-03-11 17:38:13 -08006667 TelephonyManager.CAPABILITY_NR_DUAL_CONNECTIVITY_CONFIGURATION_AVAILABLE)) {
6668 return TelephonyManager.ENABLE_NR_DUAL_CONNECTIVITY_NOT_SUPPORTED;
6669 }
6670
Sooraj Sasindran37444802020-08-11 10:40:43 -07006671 WorkSource workSource = getWorkSource(Binder.getCallingUid());
6672 final long identity = Binder.clearCallingIdentity();
6673 try {
6674 int result = (int) sendRequest(CMD_ENABLE_NR_DUAL_CONNECTIVITY,
6675 nrDualConnectivityState, subId,
6676 workSource);
6677 if (DBG) log("enableNRDualConnectivity result: " + result);
6678 return result;
6679 } finally {
6680 Binder.restoreCallingIdentity(identity);
6681 }
6682 }
6683
6684 /**
6685 * Is E-UTRA-NR Dual Connectivity enabled
6686 * @return true if dual connectivity is enabled else false
6687 */
6688 @Override
6689 public boolean isNrDualConnectivityEnabled(int subId) {
6690 TelephonyPermissions
Nathan Harold62c68512021-04-06 11:26:02 -07006691 .enforceCallingOrSelfReadPrivilegedPhoneStatePermissionOrCarrierPrivilege(
Sooraj Sasindran37444802020-08-11 10:40:43 -07006692 mApp, subId, "isNRDualConnectivityEnabled");
Sooraj Sasindran0e4e00a2021-03-16 18:02:32 -07006693 if (!isRadioInterfaceCapabilitySupported(
Sooraj Sasindrandfd595b2021-03-11 17:38:13 -08006694 TelephonyManager.CAPABILITY_NR_DUAL_CONNECTIVITY_CONFIGURATION_AVAILABLE)) {
6695 return false;
6696 }
Sooraj Sasindran37444802020-08-11 10:40:43 -07006697 WorkSource workSource = getWorkSource(Binder.getCallingUid());
6698 final long identity = Binder.clearCallingIdentity();
6699 try {
6700 boolean isEnabled = (boolean) sendRequest(CMD_IS_NR_DUAL_CONNECTIVITY_ENABLED,
6701 null, subId, workSource);
6702 if (DBG) log("isNRDualConnectivityEnabled: " + isEnabled);
6703 return isEnabled;
6704 } finally {
6705 Binder.restoreCallingIdentity(identity);
6706 }
6707 }
6708
6709 /**
Sooraj Sasindranc46dfbd2020-06-03 01:06:00 -07006710 * Set the allowed network types of the device and
6711 * provide the reason triggering the allowed network change.
6712 *
6713 * @param subId the id of the subscription.
6714 * @param reason the reason the allowed network type change is taking place
6715 * @param allowedNetworkTypes the allowed network types.
6716 * @return true on success; false on any failure.
6717 */
6718 @Override
6719 public boolean setAllowedNetworkTypesForReason(int subId,
SongFerngWang3ef3e072020-12-21 16:41:52 +08006720 @TelephonyManager.AllowedNetworkTypesReason int reason,
6721 @TelephonyManager.NetworkTypeBitMask long allowedNetworkTypes) {
Sooraj Sasindranc46dfbd2020-06-03 01:06:00 -07006722 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
6723 mApp, subId, "setAllowedNetworkTypesForReason");
Gil Cukierman1d3d3752022-10-03 21:31:33 +00006724 // If the caller only has carrier privileges, then they should not be able to override
6725 // any network types which were set for security reasons.
6726 if (mApp.checkCallingOrSelfPermission(Manifest.permission.MODIFY_PHONE_STATE)
6727 != PERMISSION_GRANTED
6728 && (reason == TelephonyManager.ALLOWED_NETWORK_TYPES_REASON_ENABLE_2G
6729 || reason == TelephonyManager.ALLOWED_NETWORK_TYPES_REASON_USER_RESTRICTIONS)) {
6730 throw new SecurityException(
6731 "setAllowedNetworkTypesForReason cannot be called with carrier privileges for"
6732 + " reason "
6733 + reason);
6734 }
SongFerngWang3ef3e072020-12-21 16:41:52 +08006735 if (!TelephonyManager.isValidAllowedNetworkTypesReason(reason)) {
SongFerngWang7ffc2732021-04-15 19:46:33 +08006736 loge("setAllowedNetworkTypesForReason: Invalid allowed network type reason: " + reason);
6737 return false;
6738 }
6739 if (!SubscriptionManager.isUsableSubscriptionId(subId)) {
6740 loge("setAllowedNetworkTypesForReason: Invalid subscriptionId:" + subId);
SongFerngWang3ef3e072020-12-21 16:41:52 +08006741 return false;
6742 }
6743
Jack Yue37dd262022-12-16 11:53:37 -08006744 log("setAllowedNetworkTypesForReason: subId=" + subId + ", reason=" + reason + " value: "
SongFerngWang8c6e82e2021-03-02 22:09:29 +08006745 + TelephonyManager.convertNetworkTypeBitmaskToString(allowedNetworkTypes));
6746
Jack Yue37dd262022-12-16 11:53:37 -08006747 Phone phone = getPhone(subId);
6748 if (phone == null) {
6749 return false;
6750 }
SongFerngWang8c6e82e2021-03-02 22:09:29 +08006751
Jack Yue37dd262022-12-16 11:53:37 -08006752 if (allowedNetworkTypes == phone.getAllowedNetworkTypes(reason)) {
SongFerngWang8c6e82e2021-03-02 22:09:29 +08006753 log("setAllowedNetworkTypesForReason: " + reason + "does not change value");
6754 return true;
SongFerngWang3ef3e072020-12-21 16:41:52 +08006755 }
SongFerngWang8c6e82e2021-03-02 22:09:29 +08006756
Sooraj Sasindranc46dfbd2020-06-03 01:06:00 -07006757 final long identity = Binder.clearCallingIdentity();
6758 try {
SongFerngWang3ef3e072020-12-21 16:41:52 +08006759 Boolean success = (Boolean) sendRequest(
6760 CMD_SET_ALLOWED_NETWORK_TYPES_FOR_REASON,
6761 new Pair<Integer, Long>(reason, allowedNetworkTypes), subId);
6762
6763 if (DBG) log("setAllowedNetworkTypesForReason: " + (success ? "ok" : "fail"));
6764 return success;
Sooraj Sasindranc46dfbd2020-06-03 01:06:00 -07006765 } finally {
6766 Binder.restoreCallingIdentity(identity);
6767 }
6768 }
6769
6770 /**
Miaoa84611c2019-03-15 09:21:10 +08006771 * Check whether DUN APN is required for tethering with subId.
Junda Liu475951f2014-11-07 16:45:03 -08006772 *
Miaoa84611c2019-03-15 09:21:10 +08006773 * @param subId the id of the subscription to require tethering.
Amit Mahajanfe58cdf2017-07-11 12:01:53 -07006774 * @return {@code true} if DUN APN is required for tethering.
Junda Liu475951f2014-11-07 16:45:03 -08006775 * @hide
6776 */
6777 @Override
SongFerngWangf08d8122019-11-15 14:58:44 +08006778 public boolean isTetheringApnRequiredForSubscriber(int subId) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08006779 enforceModifyPermission();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006780 final long identity = Binder.clearCallingIdentity();
Miaoa84611c2019-03-15 09:21:10 +08006781 final Phone phone = getPhone(subId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006782 try {
Miaoa84611c2019-03-15 09:21:10 +08006783 if (phone != null) {
6784 return phone.hasMatchedTetherApnSetting();
6785 } else {
6786 return false;
6787 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006788 } finally {
6789 Binder.restoreCallingIdentity(identity);
Junda Liu475951f2014-11-07 16:45:03 -08006790 }
Junda Liu475951f2014-11-07 16:45:03 -08006791 }
6792
6793 /**
Malcolm Chen964682d2017-11-28 16:20:07 -08006794 * Get the user enabled state of Mobile Data.
6795 *
6796 * TODO: remove and use isUserDataEnabled.
6797 * This can't be removed now because some vendor codes
6798 * calls through ITelephony directly while they should
6799 * use TelephonyManager.
6800 *
6801 * @return true on enabled
6802 */
6803 @Override
6804 public boolean getDataEnabled(int subId) {
6805 return isUserDataEnabled(subId);
6806 }
6807
6808 /**
6809 * Get whether mobile data is enabled per user setting.
6810 *
6811 * There are other factors deciding whether mobile data is actually enabled, but they are
6812 * not considered here. See {@link #isDataEnabled(int)} for more details.
Robert Greenwalt646120a2014-05-23 11:54:03 -07006813 *
Sooraj Sasindran5d051bf2021-11-05 13:14:15 -07006814 * Accepts either READ_BASIC_PHONE_STATE, ACCESS_NETWORK_STATE, MODIFY_PHONE_STATE
6815 * or carrier privileges.
Robert Greenwalted86e582014-05-21 20:03:20 -07006816 *
6817 * @return {@code true} if data is enabled else {@code false}
6818 */
6819 @Override
Malcolm Chen964682d2017-11-28 16:20:07 -08006820 public boolean isUserDataEnabled(int subId) {
Sooraj Sasindran5d051bf2021-11-05 13:14:15 -07006821 String functionName = "isUserDataEnabled";
Robert Greenwalt646120a2014-05-23 11:54:03 -07006822 try {
Sooraj Sasindran5d051bf2021-11-05 13:14:15 -07006823 try {
6824 mApp.enforceCallingOrSelfPermission(permission.READ_BASIC_PHONE_STATE,
6825 functionName);
Sooraj Sasindran12545cc2022-08-22 14:30:25 -07006826 } catch (SecurityException e) {
Sooraj Sasindran5d051bf2021-11-05 13:14:15 -07006827 mApp.enforceCallingOrSelfPermission(permission.ACCESS_NETWORK_STATE, functionName);
6828 }
Sooraj Sasindran12545cc2022-08-22 14:30:25 -07006829 } catch (SecurityException e) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08006830 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
Sooraj Sasindran5d051bf2021-11-05 13:14:15 -07006831 mApp, subId, functionName);
6832
Robert Greenwalt646120a2014-05-23 11:54:03 -07006833 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006834
6835 final long identity = Binder.clearCallingIdentity();
6836 try {
Jack Yu285100e2022-12-02 22:48:35 -08006837 int phoneId = SubscriptionManager.getPhoneId(subId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006838 if (DBG) log("isUserDataEnabled: subId=" + subId + " phoneId=" + phoneId);
6839 Phone phone = PhoneFactory.getPhone(phoneId);
6840 if (phone != null) {
6841 boolean retVal = phone.isUserDataEnabled();
6842 if (DBG) log("isUserDataEnabled: subId=" + subId + " retVal=" + retVal);
6843 return retVal;
6844 } else {
6845 if (DBG) loge("isUserDataEnabled: no phone subId=" + subId + " retVal=false");
6846 return false;
6847 }
6848 } finally {
6849 Binder.restoreCallingIdentity(identity);
Malcolm Chen964682d2017-11-28 16:20:07 -08006850 }
6851 }
6852
6853 /**
Shuo Qian8ee4e882020-01-08 14:30:06 -08006854 * Checks if the device is capable of mobile data by considering whether whether the
6855 * user has enabled mobile data, whether the carrier has enabled mobile data, and
6856 * whether the network policy allows data connections.
Malcolm Chen964682d2017-11-28 16:20:07 -08006857 *
Shuo Qian8ee4e882020-01-08 14:30:06 -08006858 * @return {@code true} if the overall data connection is capable; {@code false} if not.
Malcolm Chen964682d2017-11-28 16:20:07 -08006859 */
6860 @Override
6861 public boolean isDataEnabled(int subId) {
Sooraj Sasindran5d051bf2021-11-05 13:14:15 -07006862 String functionName = "isDataEnabled";
Sooraj Sasindran95c07a92020-07-15 01:35:24 -07006863 try {
6864 try {
6865 mApp.enforceCallingOrSelfPermission(
6866 android.Manifest.permission.ACCESS_NETWORK_STATE,
Sooraj Sasindran5d051bf2021-11-05 13:14:15 -07006867 functionName);
Sooraj Sasindran12545cc2022-08-22 14:30:25 -07006868 } catch (SecurityException e) {
Sooraj Sasindran5d051bf2021-11-05 13:14:15 -07006869 try {
6870 mApp.enforceCallingOrSelfPermission(
6871 android.Manifest.permission.READ_PHONE_STATE,
6872 functionName);
Sooraj Sasindran12545cc2022-08-22 14:30:25 -07006873 } catch (SecurityException e2) {
Sooraj Sasindran5d051bf2021-11-05 13:14:15 -07006874 mApp.enforceCallingOrSelfPermission(
6875 permission.READ_BASIC_PHONE_STATE, functionName);
6876 }
Sooraj Sasindran95c07a92020-07-15 01:35:24 -07006877 }
Sooraj Sasindran12545cc2022-08-22 14:30:25 -07006878 } catch (SecurityException e) {
Sooraj Sasindran5d051bf2021-11-05 13:14:15 -07006879 enforceReadPrivilegedPermission(functionName);
Sooraj Sasindran95c07a92020-07-15 01:35:24 -07006880 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006881
6882 final long identity = Binder.clearCallingIdentity();
6883 try {
Jack Yu285100e2022-12-02 22:48:35 -08006884 int phoneId = SubscriptionManager.getPhoneId(subId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006885 Phone phone = PhoneFactory.getPhone(phoneId);
6886 if (phone != null) {
Sarah Chine04784a2022-10-31 20:32:34 -07006887 boolean retVal = phone.getDataSettingsManager().isDataEnabled();
Jack Yu4ad64e52021-12-03 14:23:53 -08006888 if (DBG) log("isDataEnabled: " + retVal + ", subId=" + subId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006889 return retVal;
6890 } else {
6891 if (DBG) loge("isDataEnabled: no phone subId=" + subId + " retVal=false");
6892 return false;
6893 }
6894 } finally {
6895 Binder.restoreCallingIdentity(identity);
Wink Savillee7353bb2014-12-05 14:21:41 -08006896 }
Robert Greenwalted86e582014-05-21 20:03:20 -07006897 }
Shishir Agrawal60f9c952014-06-23 12:00:43 -07006898
Sooraj Sasindranaf1b5132020-05-05 21:06:20 +00006899 /**
6900 * Check if data is enabled for a specific reason
6901 * @param subId Subscription index
6902 * @param reason the reason the data enable change is taking place
6903 * @return {@code true} if the overall data is enabled; {@code false} if not.
6904 */
6905 @Override
Sooraj Sasindran95c07a92020-07-15 01:35:24 -07006906 public boolean isDataEnabledForReason(int subId,
Sooraj Sasindranaf1b5132020-05-05 21:06:20 +00006907 @TelephonyManager.DataEnabledReason int reason) {
Sooraj Sasindran5d051bf2021-11-05 13:14:15 -07006908 String functionName = "isDataEnabledForReason";
Sooraj Sasindranaf1b5132020-05-05 21:06:20 +00006909 try {
Sooraj Sasindran5d051bf2021-11-05 13:14:15 -07006910 try {
6911 mApp.enforceCallingOrSelfPermission(
6912 android.Manifest.permission.ACCESS_NETWORK_STATE,
6913 functionName);
Sooraj Sasindran12545cc2022-08-22 14:30:25 -07006914 } catch (SecurityException e) {
Sooraj Sasindran5d051bf2021-11-05 13:14:15 -07006915 mApp.enforceCallingOrSelfPermission(permission.READ_BASIC_PHONE_STATE,
6916 functionName);
6917 }
Sooraj Sasindran12545cc2022-08-22 14:30:25 -07006918 } catch (SecurityException e) {
Sooraj Sasindran0d909a02021-11-08 12:01:16 -08006919 try {
6920 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.READ_PHONE_STATE,
Sooraj Sasindran5d051bf2021-11-05 13:14:15 -07006921 functionName);
Sooraj Sasindran12545cc2022-08-22 14:30:25 -07006922 } catch (SecurityException e2) {
Sooraj Sasindran0d909a02021-11-08 12:01:16 -08006923 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
Sooraj Sasindran5d051bf2021-11-05 13:14:15 -07006924 mApp, subId, functionName);
Sooraj Sasindran0d909a02021-11-08 12:01:16 -08006925 }
Sooraj Sasindranaf1b5132020-05-05 21:06:20 +00006926 }
6927
6928
6929 final long identity = Binder.clearCallingIdentity();
6930 try {
Jack Yu285100e2022-12-02 22:48:35 -08006931 int phoneId = SubscriptionManager.getPhoneId(subId);
Sooraj Sasindranaf1b5132020-05-05 21:06:20 +00006932 if (DBG) {
Sooraj Sasindran95c07a92020-07-15 01:35:24 -07006933 log("isDataEnabledForReason: subId=" + subId + " phoneId=" + phoneId
Sooraj Sasindranaf1b5132020-05-05 21:06:20 +00006934 + " reason=" + reason);
6935 }
6936 Phone phone = PhoneFactory.getPhone(phoneId);
6937 if (phone != null) {
6938 boolean retVal;
Jack Yu7968c6d2022-07-31 00:43:21 -07006939 retVal = phone.getDataSettingsManager().isDataEnabledForReason(reason);
Sooraj Sasindran95c07a92020-07-15 01:35:24 -07006940 if (DBG) log("isDataEnabledForReason: retVal=" + retVal);
Sooraj Sasindranaf1b5132020-05-05 21:06:20 +00006941 return retVal;
6942 } else {
6943 if (DBG) {
Sooraj Sasindran95c07a92020-07-15 01:35:24 -07006944 loge("isDataEnabledForReason: no phone subId="
Sooraj Sasindranaf1b5132020-05-05 21:06:20 +00006945 + subId + " retVal=false");
6946 }
6947 return false;
6948 }
6949 } finally {
6950 Binder.restoreCallingIdentity(identity);
6951 }
6952 }
6953
Shishir Agrawal60f9c952014-06-23 12:00:43 -07006954 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08006955 public int getCarrierPrivilegeStatus(int subId) {
Hunter Knepshieldcad7f0b2021-12-13 15:08:35 -08006956 // No permission needed; this only lets the caller inspect their own status.
6957 return getCarrierPrivilegeStatusForUidWithPermission(subId, Binder.getCallingUid());
Shishir Agrawal60f9c952014-06-23 12:00:43 -07006958 }
Junda Liu29340342014-07-10 15:23:27 -07006959
6960 @Override
Jeff Davidson7e17e312018-02-13 18:17:36 -08006961 public int getCarrierPrivilegeStatusForUid(int subId, int uid) {
Shuo Qian2c0ae432019-12-05 11:40:37 -08006962 enforceReadPrivilegedPermission("getCarrierPrivilegeStatusForUid");
Hunter Knepshieldcad7f0b2021-12-13 15:08:35 -08006963 return getCarrierPrivilegeStatusForUidWithPermission(subId, uid);
6964 }
6965
6966 private int getCarrierPrivilegeStatusForUidWithPermission(int subId, int uid) {
6967 Phone phone = getPhone(subId);
Jeff Davidson7e17e312018-02-13 18:17:36 -08006968 if (phone == null) {
Taesu Leef8fbed92019-10-07 18:47:02 +09006969 loge("getCarrierPrivilegeStatusForUid: Invalid subId");
Jeff Davidson7e17e312018-02-13 18:17:36 -08006970 return TelephonyManager.CARRIER_PRIVILEGE_STATUS_NO_ACCESS;
6971 }
Hunter Knepshieldcad7f0b2021-12-13 15:08:35 -08006972 CarrierPrivilegesTracker cpt = phone.getCarrierPrivilegesTracker();
6973 if (cpt == null) {
6974 loge("getCarrierPrivilegeStatusForUid: No CarrierPrivilegesTracker");
Jeff Davidson7e17e312018-02-13 18:17:36 -08006975 return TelephonyManager.CARRIER_PRIVILEGE_STATUS_RULES_NOT_LOADED;
6976 }
Hunter Knepshieldcad7f0b2021-12-13 15:08:35 -08006977 return cpt.getCarrierPrivilegeStatusForUid(uid);
Jeff Davidson7e17e312018-02-13 18:17:36 -08006978 }
6979
6980 @Override
chen xuf7e9fe82019-05-09 19:31:02 -07006981 public int checkCarrierPrivilegesForPackage(int subId, String pkgName) {
Nazanin1adf4562021-03-29 15:35:30 -07006982 enforceReadPrivilegedPermission("checkCarrierPrivilegesForPackage");
chen xuf7e9fe82019-05-09 19:31:02 -07006983 if (TextUtils.isEmpty(pkgName)) {
Junda Liu317d70b2016-03-08 09:33:53 -08006984 return TelephonyManager.CARRIER_PRIVILEGE_STATUS_NO_ACCESS;
chen xuf7e9fe82019-05-09 19:31:02 -07006985 }
Hunter Knepshieldcad7f0b2021-12-13 15:08:35 -08006986 Phone phone = getPhone(subId);
6987 if (phone == null) {
6988 loge("checkCarrierPrivilegesForPackage: Invalid subId");
6989 return TelephonyManager.CARRIER_PRIVILEGE_STATUS_NO_ACCESS;
6990 }
6991 CarrierPrivilegesTracker cpt = phone.getCarrierPrivilegesTracker();
6992 if (cpt == null) {
6993 loge("checkCarrierPrivilegesForPackage: No CarrierPrivilegesTracker");
Shishir Agrawaleb8771e2014-07-22 11:24:08 -07006994 return TelephonyManager.CARRIER_PRIVILEGE_STATUS_RULES_NOT_LOADED;
6995 }
Hunter Knepshieldcad7f0b2021-12-13 15:08:35 -08006996 return cpt.getCarrierPrivilegeStatusForPackage(pkgName);
Zach Johnson50ecba32015-05-19 00:24:21 -07006997 }
6998
6999 @Override
7000 public int checkCarrierPrivilegesForPackageAnyPhone(String pkgName) {
Hunter Knepshieldcad7f0b2021-12-13 15:08:35 -08007001 enforceReadPrivilegedPermission("checkCarrierPrivilegesForPackageAnyPhone");
Rambo Wange7209ce2022-02-23 13:41:02 -08007002 return checkCarrierPrivilegesForPackageAnyPhoneWithPermission(pkgName);
7003 }
7004
7005 private int checkCarrierPrivilegesForPackageAnyPhoneWithPermission(String pkgName) {
Hunter Knepshieldcad7f0b2021-12-13 15:08:35 -08007006 if (TextUtils.isEmpty(pkgName)) {
Junda Liu317d70b2016-03-08 09:33:53 -08007007 return TelephonyManager.CARRIER_PRIVILEGE_STATUS_NO_ACCESS;
Hunter Knepshieldcad7f0b2021-12-13 15:08:35 -08007008 }
Zach Johnson50ecba32015-05-19 00:24:21 -07007009 int result = TelephonyManager.CARRIER_PRIVILEGE_STATUS_RULES_NOT_LOADED;
Hunter Knepshieldcad7f0b2021-12-13 15:08:35 -08007010 for (int phoneId = 0; phoneId < TelephonyManager.getDefault().getPhoneCount(); phoneId++) {
7011 Phone phone = PhoneFactory.getPhone(phoneId);
7012 if (phone == null) {
7013 continue;
Zach Johnson50ecba32015-05-19 00:24:21 -07007014 }
Hunter Knepshieldcad7f0b2021-12-13 15:08:35 -08007015 CarrierPrivilegesTracker cpt = phone.getCarrierPrivilegesTracker();
7016 if (cpt == null) {
7017 continue;
7018 }
7019 result = cpt.getCarrierPrivilegeStatusForPackage(pkgName);
Zach Johnson50ecba32015-05-19 00:24:21 -07007020 if (result == TelephonyManager.CARRIER_PRIVILEGE_STATUS_HAS_ACCESS) {
7021 break;
7022 }
7023 }
Zach Johnson50ecba32015-05-19 00:24:21 -07007024 return result;
Junda Liu29340342014-07-10 15:23:27 -07007025 }
Derek Tan89e89d42014-07-08 17:00:10 -07007026
7027 @Override
Junda Liue64de782015-04-16 17:19:16 -07007028 public List<String> getCarrierPackageNamesForIntentAndPhone(Intent intent, int phoneId) {
Nazanin1adf4562021-03-29 15:35:30 -07007029 enforceReadPrivilegedPermission("getCarrierPackageNamesForIntentAndPhone");
Rambo Wang8a247eb2022-02-08 21:11:18 +00007030 Phone phone = PhoneFactory.getPhone(phoneId);
7031 if (phone == null) {
7032 return Collections.emptyList();
Junda Liue64de782015-04-16 17:19:16 -07007033 }
Rambo Wang8a247eb2022-02-08 21:11:18 +00007034 CarrierPrivilegesTracker cpt = phone.getCarrierPrivilegesTracker();
7035 if (cpt == null) {
7036 return Collections.emptyList();
Shishir Agrawaleb6439a2014-07-21 13:19:38 -07007037 }
Rambo Wang8a247eb2022-02-08 21:11:18 +00007038 return cpt.getCarrierPackageNamesForIntent(intent);
Shishir Agrawaleb6439a2014-07-21 13:19:38 -07007039 }
7040
Amith Yamasani6e118872016-02-19 12:53:51 -08007041 @Override
chen xuf7e9fe82019-05-09 19:31:02 -07007042 public List<String> getPackagesWithCarrierPrivileges(int phoneId) {
Nazanin1adf4562021-03-29 15:35:30 -07007043 enforceReadPrivilegedPermission("getPackagesWithCarrierPrivileges");
Hunter Knepshieldcad7f0b2021-12-13 15:08:35 -08007044 Phone phone = PhoneFactory.getPhone(phoneId);
7045 if (phone == null) {
7046 return Collections.emptyList();
Amith Yamasani6e118872016-02-19 12:53:51 -08007047 }
Hunter Knepshieldcad7f0b2021-12-13 15:08:35 -08007048 CarrierPrivilegesTracker cpt = phone.getCarrierPrivilegesTracker();
7049 if (cpt == null) {
7050 return Collections.emptyList();
7051 }
7052 return new ArrayList<>(cpt.getPackagesWithCarrierPrivileges());
Amith Yamasani6e118872016-02-19 12:53:51 -08007053 }
7054
chen xuf7e9fe82019-05-09 19:31:02 -07007055 @Override
7056 public List<String> getPackagesWithCarrierPrivilegesForAllPhones() {
Shuo Qian067a06d2019-12-03 23:40:18 +00007057 enforceReadPrivilegedPermission("getPackagesWithCarrierPrivilegesForAllPhones");
Hunter Knepshieldcad7f0b2021-12-13 15:08:35 -08007058 Set<String> privilegedPackages = new ArraySet<>();
Shuo Qian067a06d2019-12-03 23:40:18 +00007059 final long identity = Binder.clearCallingIdentity();
Shuo Qian067a06d2019-12-03 23:40:18 +00007060 try {
7061 for (int i = 0; i < TelephonyManager.getDefault().getPhoneCount(); i++) {
7062 privilegedPackages.addAll(getPackagesWithCarrierPrivileges(i));
7063 }
7064 } finally {
7065 Binder.restoreCallingIdentity(identity);
chen xuf7e9fe82019-05-09 19:31:02 -07007066 }
Hunter Knepshieldcad7f0b2021-12-13 15:08:35 -08007067 return new ArrayList<>(privilegedPackages);
chen xuf7e9fe82019-05-09 19:31:02 -07007068 }
7069
Rambo Wang6812ffb2022-03-15 16:54:17 -07007070 @Override
7071 public @Nullable String getCarrierServicePackageNameForLogicalSlot(int logicalSlotIndex) {
7072 enforceReadPrivilegedPermission("getCarrierServicePackageNameForLogicalSlot");
7073
7074 final Phone phone = PhoneFactory.getPhone(logicalSlotIndex);
7075 if (phone == null) {
7076 return null;
7077 }
7078 final CarrierPrivilegesTracker cpt = phone.getCarrierPrivilegesTracker();
7079 if (cpt == null) {
7080 return null;
7081 }
7082 return cpt.getCarrierServicePackageName();
7083 }
7084
Wink Savilleb564aae2014-10-23 10:18:09 -07007085 private String getIccId(int subId) {
Sanket Padawe356d7632015-06-22 14:03:32 -07007086 final Phone phone = getPhone(subId);
Muralidhar Reddy472c2ae2021-09-29 19:38:40 +00007087 UiccPort port = phone == null ? null : phone.getUiccPort();
7088 if (port == null) {
Derek Tan97ebb422014-09-05 16:55:38 -07007089 return null;
7090 }
Muralidhar Reddy472c2ae2021-09-29 19:38:40 +00007091 String iccId = port.getIccId();
Derek Tan97ebb422014-09-05 16:55:38 -07007092 if (TextUtils.isEmpty(iccId)) {
Derek Tan97ebb422014-09-05 16:55:38 -07007093 return null;
7094 }
7095 return iccId;
7096 }
7097
Shishir Agrawaleb6439a2014-07-21 13:19:38 -07007098 @Override
Shuo Qiane4e11672020-12-15 22:15:33 -08007099 public void setCallComposerStatus(int subId, int status) {
7100 enforceModifyPermission();
7101
7102 final long identity = Binder.clearCallingIdentity();
7103 try {
7104 Phone phone = getPhone(subId);
7105 if (phone != null) {
7106 Phone defaultPhone = phone.getImsPhone();
7107 if (defaultPhone != null && defaultPhone.getPhoneType() == PHONE_TYPE_IMS) {
7108 ImsPhone imsPhone = (ImsPhone) defaultPhone;
7109 imsPhone.setCallComposerStatus(status);
Shuo Qian284ae752020-12-22 19:10:14 -08007110 ImsManager.getInstance(mApp, getSlotIndexOrException(subId))
7111 .updateImsServiceConfig();
Shuo Qiane4e11672020-12-15 22:15:33 -08007112 }
7113 }
Shuo Qian284ae752020-12-22 19:10:14 -08007114 } catch (ImsException e) {
7115 throw new ServiceSpecificException(e.getCode());
7116 } finally {
Shuo Qiane4e11672020-12-15 22:15:33 -08007117 Binder.restoreCallingIdentity(identity);
7118 }
7119 }
7120
7121 @Override
7122 public int getCallComposerStatus(int subId) {
7123 enforceReadPrivilegedPermission("getCallComposerStatus");
7124
7125 final long identity = Binder.clearCallingIdentity();
7126 try {
7127 Phone phone = getPhone(subId);
7128 if (phone != null) {
7129 Phone defaultPhone = phone.getImsPhone();
7130 if (defaultPhone != null && defaultPhone.getPhoneType() == PHONE_TYPE_IMS) {
7131 ImsPhone imsPhone = (ImsPhone) defaultPhone;
7132 return imsPhone.getCallComposerStatus();
7133 }
7134 }
7135 } finally {
7136 Binder.restoreCallingIdentity(identity);
7137 }
7138 return TelephonyManager.CALL_COMPOSER_STATUS_OFF;
7139 }
7140
7141 @Override
Jeff Sharkey85190e62014-12-05 09:40:12 -08007142 public boolean setLine1NumberForDisplayForSubscriber(int subId, String alphaTag,
7143 String number) {
Shuo Qian2c0ae432019-12-05 11:40:37 -08007144 TelephonyPermissions.enforceCallingOrSelfCarrierPrivilege(mApp,
Jeff Davidson7e17e312018-02-13 18:17:36 -08007145 subId, "setLine1NumberForDisplayForSubscriber");
Derek Tan97ebb422014-09-05 16:55:38 -07007146
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007147 final long identity = Binder.clearCallingIdentity();
7148 try {
7149 final String iccId = getIccId(subId);
7150 final Phone phone = getPhone(subId);
7151 if (phone == null) {
7152 return false;
7153 }
7154 final String subscriberId = phone.getSubscriberId();
7155
7156 if (DBG_MERGE) {
Amit Mahajanb8f13202020-01-27 18:16:07 -08007157 Rlog.d(LOG_TAG, "Setting line number for ICC=" + iccId + ", subscriberId="
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007158 + subscriberId + " to " + number);
7159 }
7160
7161 if (TextUtils.isEmpty(iccId)) {
7162 return false;
7163 }
7164
7165 final SharedPreferences.Editor editor = mTelephonySharedPreferences.edit();
7166
7167 final String alphaTagPrefKey = PREF_CARRIERS_ALPHATAG_PREFIX + iccId;
7168 if (alphaTag == null) {
7169 editor.remove(alphaTagPrefKey);
7170 } else {
7171 editor.putString(alphaTagPrefKey, alphaTag);
7172 }
7173
7174 // Record both the line number and IMSI for this ICCID, since we need to
7175 // track all merged IMSIs based on line number
7176 final String numberPrefKey = PREF_CARRIERS_NUMBER_PREFIX + iccId;
7177 final String subscriberPrefKey = PREF_CARRIERS_SUBSCRIBER_PREFIX + iccId;
7178 if (number == null) {
7179 editor.remove(numberPrefKey);
7180 editor.remove(subscriberPrefKey);
7181 } else {
7182 editor.putString(numberPrefKey, number);
7183 editor.putString(subscriberPrefKey, subscriberId);
7184 }
7185
7186 editor.commit();
7187 return true;
7188 } finally {
7189 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07007190 }
Derek Tan7226c842014-07-02 17:42:23 -07007191 }
7192
7193 @Override
Philip P. Moltmann700a9592019-10-03 11:53:50 -07007194 public String getLine1NumberForDisplay(int subId, String callingPackage,
7195 String callingFeatureId) {
Makoto Onukifee69342015-06-29 14:44:50 -07007196 // This is open to apps with WRITE_SMS.
Jeff Davidson7e17e312018-02-13 18:17:36 -08007197 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneNumber(
Philip P. Moltmann700a9592019-10-03 11:53:50 -07007198 mApp, subId, callingPackage, callingFeatureId, "getLine1NumberForDisplay")) {
Amit Mahajan9cf11512015-11-09 11:40:48 -08007199 if (DBG_MERGE) log("getLine1NumberForDisplay returning null due to permission");
Svet Ganovb320e182015-04-16 12:30:10 -07007200 return null;
7201 }
Derek Tan97ebb422014-09-05 16:55:38 -07007202
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007203 final long identity = Binder.clearCallingIdentity();
7204 try {
7205 String iccId = getIccId(subId);
7206 if (iccId != null) {
7207 String numberPrefKey = PREF_CARRIERS_NUMBER_PREFIX + iccId;
7208 if (DBG_MERGE) {
7209 log("getLine1NumberForDisplay returning "
7210 + mTelephonySharedPreferences.getString(numberPrefKey, null));
7211 }
7212 return mTelephonySharedPreferences.getString(numberPrefKey, null);
Amit Mahajan9cf11512015-11-09 11:40:48 -08007213 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007214 if (DBG_MERGE) log("getLine1NumberForDisplay returning null as iccId is null");
7215 return null;
7216 } finally {
7217 Binder.restoreCallingIdentity(identity);
Derek Tan7226c842014-07-02 17:42:23 -07007218 }
Derek Tan7226c842014-07-02 17:42:23 -07007219 }
7220
7221 @Override
Philip P. Moltmann700a9592019-10-03 11:53:50 -07007222 public String getLine1AlphaTagForDisplay(int subId, String callingPackage,
7223 String callingFeatureId) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08007224 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
Philip P. Moltmann700a9592019-10-03 11:53:50 -07007225 mApp, subId, callingPackage, callingFeatureId, "getLine1AlphaTagForDisplay")) {
Svet Ganovb320e182015-04-16 12:30:10 -07007226 return null;
7227 }
Derek Tan97ebb422014-09-05 16:55:38 -07007228
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007229 final long identity = Binder.clearCallingIdentity();
7230 try {
7231 String iccId = getIccId(subId);
7232 if (iccId != null) {
7233 String alphaTagPrefKey = PREF_CARRIERS_ALPHATAG_PREFIX + iccId;
7234 return mTelephonySharedPreferences.getString(alphaTagPrefKey, null);
7235 }
7236 return null;
7237 } finally {
7238 Binder.restoreCallingIdentity(identity);
Derek Tan7226c842014-07-02 17:42:23 -07007239 }
Derek Tan7226c842014-07-02 17:42:23 -07007240 }
Shishir Agrawalb1ebf8c2014-07-17 16:32:41 -07007241
7242 @Override
Philip P. Moltmann700a9592019-10-03 11:53:50 -07007243 public String[] getMergedSubscriberIds(int subId, String callingPackage,
7244 String callingFeatureId) {
Jeff Davidson913390f2018-02-23 17:11:49 -08007245 // This API isn't public, so no need to provide a valid subscription ID - we're not worried
7246 // about carrier-privileged callers not having access.
Jeff Davidson7e17e312018-02-13 18:17:36 -08007247 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
Jeff Davidson913390f2018-02-23 17:11:49 -08007248 mApp, SubscriptionManager.INVALID_SUBSCRIPTION_ID, callingPackage,
Philip P. Moltmann700a9592019-10-03 11:53:50 -07007249 callingFeatureId, "getMergedSubscriberIds")) {
Fyodor Kupolov8e53b0b2015-06-17 13:17:50 -07007250 return null;
7251 }
Jeff Sharkey85190e62014-12-05 09:40:12 -08007252
Jordan Liub49b04b2019-05-06 14:45:15 -07007253 // Clear calling identity, when calling TelephonyManager, because callerUid must be
7254 // the process, where TelephonyManager was instantiated.
7255 // Otherwise AppOps check will fail.
Fyodor Kupolov8e53b0b2015-06-17 13:17:50 -07007256 final long identity = Binder.clearCallingIdentity();
7257 try {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08007258 final Context context = mApp;
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007259 final TelephonyManager tele = TelephonyManager.from(context);
7260 final SubscriptionManager sub = SubscriptionManager.from(context);
7261
7262 // Figure out what subscribers are currently active
7263 final ArraySet<String> activeSubscriberIds = new ArraySet<>();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007264
Jordan Liub49b04b2019-05-06 14:45:15 -07007265 // Only consider subs which match the current subId
7266 // This logic can be simplified. See b/131189269 for progress.
7267 if (isActiveSubscription(subId)) {
Fyodor Kupolov8e53b0b2015-06-17 13:17:50 -07007268 activeSubscriberIds.add(tele.getSubscriberId(subId));
7269 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007270
7271 // First pass, find a number override for an active subscriber
7272 String mergeNumber = null;
7273 final Map<String, ?> prefs = mTelephonySharedPreferences.getAll();
7274 for (String key : prefs.keySet()) {
7275 if (key.startsWith(PREF_CARRIERS_SUBSCRIBER_PREFIX)) {
7276 final String subscriberId = (String) prefs.get(key);
7277 if (activeSubscriberIds.contains(subscriberId)) {
7278 final String iccId = key.substring(
7279 PREF_CARRIERS_SUBSCRIBER_PREFIX.length());
7280 final String numberKey = PREF_CARRIERS_NUMBER_PREFIX + iccId;
7281 mergeNumber = (String) prefs.get(numberKey);
7282 if (DBG_MERGE) {
Amit Mahajanb8f13202020-01-27 18:16:07 -08007283 Rlog.d(LOG_TAG, "Found line number " + mergeNumber
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007284 + " for active subscriber " + subscriberId);
7285 }
7286 if (!TextUtils.isEmpty(mergeNumber)) {
7287 break;
7288 }
7289 }
7290 }
7291 }
7292
7293 // Shortcut when no active merged subscribers
7294 if (TextUtils.isEmpty(mergeNumber)) {
7295 return null;
7296 }
7297
7298 // Second pass, find all subscribers under that line override
7299 final ArraySet<String> result = new ArraySet<>();
7300 for (String key : prefs.keySet()) {
7301 if (key.startsWith(PREF_CARRIERS_NUMBER_PREFIX)) {
7302 final String number = (String) prefs.get(key);
7303 if (mergeNumber.equals(number)) {
7304 final String iccId = key.substring(PREF_CARRIERS_NUMBER_PREFIX.length());
7305 final String subscriberKey = PREF_CARRIERS_SUBSCRIBER_PREFIX + iccId;
7306 final String subscriberId = (String) prefs.get(subscriberKey);
7307 if (!TextUtils.isEmpty(subscriberId)) {
7308 result.add(subscriberId);
7309 }
7310 }
7311 }
7312 }
7313
7314 final String[] resultArray = result.toArray(new String[result.size()]);
7315 Arrays.sort(resultArray);
7316 if (DBG_MERGE) {
Amit Mahajanb8f13202020-01-27 18:16:07 -08007317 Rlog.d(LOG_TAG,
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007318 "Found subscribers " + Arrays.toString(resultArray) + " after merge");
7319 }
7320 return resultArray;
Fyodor Kupolov8e53b0b2015-06-17 13:17:50 -07007321 } finally {
7322 Binder.restoreCallingIdentity(identity);
Jeff Sharkey85190e62014-12-05 09:40:12 -08007323 }
Jeff Sharkey85190e62014-12-05 09:40:12 -08007324 }
7325
7326 @Override
zoey chen38003472019-12-13 17:16:31 +08007327 public String[] getMergedImsisFromGroup(int subId, String callingPackage) {
7328 enforceReadPrivilegedPermission("getMergedImsisFromGroup");
Malcolm Chen6ca97372019-07-01 16:28:21 -07007329
7330 final long identity = Binder.clearCallingIdentity();
7331 try {
7332 final TelephonyManager telephonyManager = mApp.getSystemService(
7333 TelephonyManager.class);
7334 String subscriberId = telephonyManager.getSubscriberId(subId);
7335 if (subscriberId == null) {
7336 if (DBG) {
zoey chen38003472019-12-13 17:16:31 +08007337 log("getMergedImsisFromGroup can't find subscriberId for subId "
Malcolm Chen6ca97372019-07-01 16:28:21 -07007338 + subId);
7339 }
7340 return null;
7341 }
7342
Jack Yu285100e2022-12-02 22:48:35 -08007343 ParcelUuid groupUuid;
7344 if (PhoneFactory.isSubscriptionManagerServiceEnabled()) {
7345 final SubscriptionInfo info = SubscriptionManagerService.getInstance()
7346 .getSubscriptionInfo(subId);
7347 groupUuid = info.getGroupUuid();
7348 } else {
7349 final SubscriptionInfo info = mSubscriptionController
7350 .getSubscriptionInfo(subId);
7351 groupUuid = info.getGroupUuid();
7352 }
Malcolm Chen6ca97372019-07-01 16:28:21 -07007353 // If it doesn't belong to any group, return just subscriberId of itself.
7354 if (groupUuid == null) {
7355 return new String[]{subscriberId};
7356 }
7357
7358 // Get all subscriberIds from the group.
7359 final List<String> mergedSubscriberIds = new ArrayList<>();
Jack Yu285100e2022-12-02 22:48:35 -08007360 List<SubscriptionInfo> groupInfos;
7361 if (PhoneFactory.isSubscriptionManagerServiceEnabled()) {
7362 groupInfos = SubscriptionManagerService.getInstance()
7363 .getSubscriptionsInGroup(groupUuid, mApp.getOpPackageName(),
7364 mApp.getAttributionTag());
7365 } else {
7366 groupInfos = mSubscriptionController
7367 .getSubscriptionsInGroup(groupUuid, mApp.getOpPackageName(),
7368 mApp.getAttributionTag());
7369 }
Malcolm Chen6ca97372019-07-01 16:28:21 -07007370 for (SubscriptionInfo subInfo : groupInfos) {
7371 subscriberId = telephonyManager.getSubscriberId(subInfo.getSubscriptionId());
7372 if (subscriberId != null) {
7373 mergedSubscriberIds.add(subscriberId);
7374 }
7375 }
7376
7377 return mergedSubscriberIds.toArray(new String[mergedSubscriberIds.size()]);
7378 } finally {
7379 Binder.restoreCallingIdentity(identity);
7380
7381 }
7382 }
7383
7384 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08007385 public boolean setOperatorBrandOverride(int subId, String brand) {
Shuo Qian2c0ae432019-12-05 11:40:37 -08007386 TelephonyPermissions.enforceCallingOrSelfCarrierPrivilege(mApp,
Jeff Davidson7e17e312018-02-13 18:17:36 -08007387 subId, "setOperatorBrandOverride");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007388
7389 final long identity = Binder.clearCallingIdentity();
7390 try {
7391 final Phone phone = getPhone(subId);
7392 return phone == null ? false : phone.setOperatorBrandOverride(brand);
7393 } finally {
7394 Binder.restoreCallingIdentity(identity);
7395 }
Shishir Agrawalb1ebf8c2014-07-17 16:32:41 -07007396 }
Steven Liu4bf01bc2014-07-17 11:05:29 -05007397
7398 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08007399 public boolean setRoamingOverride(int subId, List<String> gsmRoamingList,
Shishir Agrawal621a47c2014-12-01 10:25:09 -08007400 List<String> gsmNonRoamingList, List<String> cdmaRoamingList,
7401 List<String> cdmaNonRoamingList) {
Shuo Qian2c0ae432019-12-05 11:40:37 -08007402 TelephonyPermissions.enforceCallingOrSelfCarrierPrivilege(
7403 mApp, subId, "setRoamingOverride");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007404
7405 final long identity = Binder.clearCallingIdentity();
7406 try {
7407 final Phone phone = getPhone(subId);
7408 if (phone == null) {
7409 return false;
7410 }
7411 return phone.setRoamingOverride(gsmRoamingList, gsmNonRoamingList, cdmaRoamingList,
7412 cdmaNonRoamingList);
7413 } finally {
7414 Binder.restoreCallingIdentity(identity);
Shishir Agrawalc04d9752016-02-19 10:41:00 -08007415 }
Shishir Agrawal621a47c2014-12-01 10:25:09 -08007416 }
7417
7418 @Override
Shuo Qian850e4d6a2018-04-25 21:02:08 +00007419 @Deprecated
7420 public int invokeOemRilRequestRaw(byte[] oemReq, byte[] oemResp) {
7421 enforceModifyPermission();
7422
7423 int returnValue = 0;
7424 try {
vagdeviaf9a5b92018-08-15 16:01:53 -07007425 AsyncResult result = (AsyncResult) sendRequest(CMD_INVOKE_OEM_RIL_REQUEST_RAW, oemReq);
Shuo Qian850e4d6a2018-04-25 21:02:08 +00007426 if(result.exception == null) {
7427 if (result.result != null) {
7428 byte[] responseData = (byte[])(result.result);
7429 if(responseData.length > oemResp.length) {
7430 Log.w(LOG_TAG, "Buffer to copy response too small: Response length is " +
7431 responseData.length + "bytes. Buffer Size is " +
7432 oemResp.length + "bytes.");
7433 }
7434 System.arraycopy(responseData, 0, oemResp, 0, responseData.length);
7435 returnValue = responseData.length;
7436 }
7437 } else {
7438 CommandException ex = (CommandException) result.exception;
7439 returnValue = ex.getCommandError().ordinal();
7440 if(returnValue > 0) returnValue *= -1;
7441 }
7442 } catch (RuntimeException e) {
7443 Log.w(LOG_TAG, "sendOemRilRequestRaw: Runtime Exception");
7444 returnValue = (CommandException.Error.GENERIC_FAILURE.ordinal());
7445 if(returnValue > 0) returnValue *= -1;
7446 }
7447
7448 return returnValue;
7449 }
7450
7451 @Override
Robert Greenwalt36b23af2015-07-06 17:59:14 -07007452 public int getRadioAccessFamily(int phoneId, String callingPackage) {
Jeff Davidson913390f2018-02-23 17:11:49 -08007453 Phone phone = PhoneFactory.getPhone(phoneId);
Shuo Qiandee53402020-05-29 14:08:15 -07007454 try {
7455 TelephonyPermissions
Nathan Harold62c68512021-04-06 11:26:02 -07007456 .enforceCallingOrSelfReadPrivilegedPhoneStatePermissionOrCarrierPrivilege(
Shuo Qiandee53402020-05-29 14:08:15 -07007457 mApp, phone.getSubId(), "getRadioAccessFamily");
7458 } catch (SecurityException e) {
7459 EventLog.writeEvent(0x534e4554, "150857259", -1, "Missing Permission");
7460 throw e;
7461 }
chen xub97461a2018-10-26 14:17:57 -07007462 int raf = RadioAccessFamily.RAF_UNKNOWN;
Jeff Davidson913390f2018-02-23 17:11:49 -08007463 if (phone == null) {
chen xub97461a2018-10-26 14:17:57 -07007464 return raf;
Jeff Davidson913390f2018-02-23 17:11:49 -08007465 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007466 final long identity = Binder.clearCallingIdentity();
7467 try {
chen xub97461a2018-10-26 14:17:57 -07007468 TelephonyPermissions
Nathan Harold62c68512021-04-06 11:26:02 -07007469 .enforceCallingOrSelfReadPrivilegedPhoneStatePermissionOrCarrierPrivilege(
chen xub97461a2018-10-26 14:17:57 -07007470 mApp, phone.getSubId(), "getRadioAccessFamily");
7471 raf = ProxyController.getInstance().getRadioAccessFamily(phoneId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007472 } finally {
7473 Binder.restoreCallingIdentity(identity);
7474 }
chen xub97461a2018-10-26 14:17:57 -07007475 return raf;
Wink Saville5d475dd2014-10-17 15:00:58 -07007476 }
Andrew Leedf14ead2014-10-17 14:22:52 -07007477
7478 @Override
Hall Liu82694d52020-12-11 18:22:04 -08007479 public void uploadCallComposerPicture(int subscriptionId, String callingPackage,
Hall Liue31bac62020-12-23 19:16:10 -08007480 String contentType, ParcelFileDescriptor fd, ResultReceiver callback) {
Hall Liu82694d52020-12-11 18:22:04 -08007481 try {
7482 if (!Objects.equals(mApp.getPackageManager().getPackageUid(callingPackage, 0),
7483 Binder.getCallingUid())) {
Tyler Gunnb87925a2021-06-10 14:54:27 -07007484 throw new SecurityException("Invalid package:" + callingPackage);
Hall Liu82694d52020-12-11 18:22:04 -08007485 }
7486 } catch (PackageManager.NameNotFoundException e) {
Tyler Gunnb87925a2021-06-10 14:54:27 -07007487 throw new SecurityException("Invalid package:" + callingPackage);
Hall Liu82694d52020-12-11 18:22:04 -08007488 }
7489 RoleManager rm = mApp.getSystemService(RoleManager.class);
7490 List<String> dialerRoleHolders = rm.getRoleHolders(RoleManager.ROLE_DIALER);
7491 if (!dialerRoleHolders.contains(callingPackage)) {
7492 throw new SecurityException("App must be the dialer role holder to"
7493 + " upload a call composer pic");
7494 }
7495
7496 Executors.newSingleThreadExecutor().execute(() -> {
7497 ByteArrayOutputStream output = new ByteArrayOutputStream(
7498 (int) TelephonyManager.getMaximumCallComposerPictureSize());
7499 InputStream input = new ParcelFileDescriptor.AutoCloseInputStream(fd);
7500 boolean readUntilEnd = false;
7501 int totalBytesRead = 0;
7502 byte[] buffer = new byte[16 * 1024];
7503 while (true) {
7504 int numRead;
7505 try {
7506 numRead = input.read(buffer);
7507 } catch (IOException e) {
7508 try {
7509 fd.checkError();
7510 callback.send(TelephonyManager.CallComposerException.ERROR_INPUT_CLOSED,
7511 null);
7512 } catch (IOException e1) {
7513 // This means that the other side closed explicitly with an error. If this
7514 // happens, log and ignore.
7515 loge("Remote end of call composer picture pipe closed: " + e1);
7516 }
7517 break;
7518 }
7519 if (numRead == -1) {
7520 readUntilEnd = true;
7521 break;
7522 }
7523 totalBytesRead += numRead;
7524 if (totalBytesRead > TelephonyManager.getMaximumCallComposerPictureSize()) {
7525 loge("Too many bytes read for call composer picture: " + totalBytesRead);
7526 try {
7527 input.close();
7528 } catch (IOException e) {
7529 // ignore
7530 }
7531 break;
7532 }
7533 output.write(buffer, 0, numRead);
7534 }
7535 // Generally, the remote end will close the file descriptors. The only case where we
7536 // close is above, where the picture size is too big.
7537
7538 try {
7539 fd.checkError();
7540 } catch (IOException e) {
7541 loge("Remote end for call composer closed with an error: " + e);
7542 return;
7543 }
7544
Hall Liuaa4211e2021-01-20 15:43:39 -08007545 if (!readUntilEnd) {
7546 loge("Did not finish reading entire image; aborting");
7547 return;
7548 }
Hall Liu82694d52020-12-11 18:22:04 -08007549
Hall Liuaa4211e2021-01-20 15:43:39 -08007550 ImageData imageData = new ImageData(output.toByteArray(), contentType, null);
7551 CallComposerPictureManager.getInstance(mApp, subscriptionId).handleUploadToServer(
7552 new CallComposerPictureTransfer.Factory() {},
7553 imageData,
7554 (result) -> {
7555 if (result.first != null) {
7556 ParcelUuid parcelUuid = new ParcelUuid(result.first);
7557 Bundle outputResult = new Bundle();
7558 outputResult.putParcelable(
7559 TelephonyManager.KEY_CALL_COMPOSER_PICTURE_HANDLE, parcelUuid);
7560 callback.send(TelephonyManager.CallComposerException.SUCCESS,
7561 outputResult);
7562 } else {
7563 callback.send(result.second, null);
7564 }
7565 }
7566 );
Hall Liu82694d52020-12-11 18:22:04 -08007567 });
7568 }
7569
7570 @Override
Andrew Leedf14ead2014-10-17 14:22:52 -07007571 public void enableVideoCalling(boolean enable) {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08007572 final Phone defaultPhone = getDefaultPhone();
Andrew Leedf14ead2014-10-17 14:22:52 -07007573 enforceModifyPermission();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007574
7575 final long identity = Binder.clearCallingIdentity();
7576 try {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08007577 ImsManager.getInstance(defaultPhone.getContext(),
7578 defaultPhone.getPhoneId()).setVtSetting(enable);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007579 } finally {
7580 Binder.restoreCallingIdentity(identity);
7581 }
Andrew Leedf14ead2014-10-17 14:22:52 -07007582 }
7583
7584 @Override
Philip P. Moltmann700a9592019-10-03 11:53:50 -07007585 public boolean isVideoCallingEnabled(String callingPackage, String callingFeatureId) {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08007586 final Phone defaultPhone = getDefaultPhone();
Philip P. Moltmann700a9592019-10-03 11:53:50 -07007587 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(mApp, defaultPhone.getSubId(),
7588 callingPackage, callingFeatureId, "isVideoCallingEnabled")) {
Amit Mahajan578e53d2018-03-20 16:18:38 +00007589 return false;
7590 }
Svet Ganovb320e182015-04-16 12:30:10 -07007591
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007592 final long identity = Binder.clearCallingIdentity();
7593 try {
7594 // Check the user preference and the system-level IMS setting. Even if the user has
7595 // enabled video calling, if IMS is disabled we aren't able to support video calling.
7596 // In the long run, we may instead need to check if there exists a connection service
7597 // which can support video calling.
7598 ImsManager imsManager =
Nazanin Bakhshif782e562018-12-11 15:15:39 -08007599 ImsManager.getInstance(defaultPhone.getContext(), defaultPhone.getPhoneId());
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007600 return imsManager.isVtEnabledByPlatform()
7601 && imsManager.isEnhanced4gLteModeSettingEnabledByUser()
7602 && imsManager.isVtEnabledByUser();
7603 } finally {
7604 Binder.restoreCallingIdentity(identity);
7605 }
Andrew Leedf14ead2014-10-17 14:22:52 -07007606 }
Libin.Tang@motorola.comafe82642014-12-18 13:27:53 -06007607
Andrew Leea1239f22015-03-02 17:44:07 -08007608 @Override
Philip P. Moltmann700a9592019-10-03 11:53:50 -07007609 public boolean canChangeDtmfToneLength(int subId, String callingPackage,
7610 String callingFeatureId) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007611 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
Philip P. Moltmann700a9592019-10-03 11:53:50 -07007612 mApp, subId, callingPackage, callingFeatureId,
7613 "isVideoCallingEnabled")) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007614 return false;
7615 }
7616
7617 final long identity = Binder.clearCallingIdentity();
7618 try {
7619 CarrierConfigManager configManager =
7620 (CarrierConfigManager) mApp.getSystemService(Context.CARRIER_CONFIG_SERVICE);
Nazanin Bakhshif782e562018-12-11 15:15:39 -08007621 return configManager.getConfigForSubId(subId)
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007622 .getBoolean(CarrierConfigManager.KEY_DTMF_TYPE_ENABLED_BOOL);
7623 } finally {
7624 Binder.restoreCallingIdentity(identity);
7625 }
Andrew Leea1239f22015-03-02 17:44:07 -08007626 }
7627
7628 @Override
Philip P. Moltmann700a9592019-10-03 11:53:50 -07007629 public boolean isWorldPhone(int subId, String callingPackage, String callingFeatureId) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007630 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
Philip P. Moltmann700a9592019-10-03 11:53:50 -07007631 mApp, subId, callingPackage, callingFeatureId, "isVideoCallingEnabled")) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007632 return false;
7633 }
7634
7635 final long identity = Binder.clearCallingIdentity();
7636 try {
7637 CarrierConfigManager configManager =
7638 (CarrierConfigManager) mApp.getSystemService(Context.CARRIER_CONFIG_SERVICE);
Nazanin Bakhshif782e562018-12-11 15:15:39 -08007639 return configManager.getConfigForSubId(subId)
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007640 .getBoolean(CarrierConfigManager.KEY_WORLD_PHONE_BOOL);
7641 } finally {
7642 Binder.restoreCallingIdentity(identity);
7643 }
Andrew Leea1239f22015-03-02 17:44:07 -08007644 }
7645
Andrew Lee9431b832015-03-09 18:46:45 -07007646 @Override
7647 public boolean isTtyModeSupported() {
Tyler Gunn5ddfdc92019-10-31 13:08:23 -07007648 TelecomManager telecomManager = mApp.getSystemService(TelecomManager.class);
Wooki Wu1f82f7a2016-02-15 15:59:58 +08007649 return telecomManager.isTtySupported();
Andrew Lee9431b832015-03-09 18:46:45 -07007650 }
7651
7652 @Override
7653 public boolean isHearingAidCompatibilitySupported() {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007654 final long identity = Binder.clearCallingIdentity();
7655 try {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08007656 return mApp.getResources().getBoolean(R.bool.hac_enabled);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007657 } finally {
7658 Binder.restoreCallingIdentity(identity);
7659 }
Andrew Lee9431b832015-03-09 18:46:45 -07007660 }
7661
Hall Liuf6668912018-10-31 17:05:23 -07007662 /**
7663 * Determines whether the device currently supports RTT (Real-time text). Based both on carrier
7664 * support for the feature and device firmware support.
7665 *
7666 * @return {@code true} if the device and carrier both support RTT, {@code false} otherwise.
7667 */
7668 @Override
7669 public boolean isRttSupported(int subscriptionId) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007670 final long identity = Binder.clearCallingIdentity();
Nazanin Bakhshif782e562018-12-11 15:15:39 -08007671 final Phone phone = getPhone(subscriptionId);
7672 if (phone == null) {
7673 loge("isRttSupported: no Phone found. Invalid subId:" + subscriptionId);
7674 return false;
7675 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007676 try {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08007677 boolean isCarrierSupported = mApp.getCarrierConfigForSubId(subscriptionId).getBoolean(
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007678 CarrierConfigManager.KEY_RTT_SUPPORTED_BOOL);
7679 boolean isDeviceSupported =
Nazanin Bakhshif782e562018-12-11 15:15:39 -08007680 phone.getContext().getResources().getBoolean(R.bool.config_support_rtt);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007681 return isCarrierSupported && isDeviceSupported;
7682 } finally {
7683 Binder.restoreCallingIdentity(identity);
7684 }
Hall Liu98187582018-01-22 19:15:32 -08007685 }
7686
Hall Liuf6668912018-10-31 17:05:23 -07007687 /**
Hall Liuf2daa022019-07-23 18:39:00 -07007688 * Determines whether the user has turned on RTT. If the carrier wants to ignore the user-set
7689 * RTT setting, will return true if the device and carrier both support RTT.
7690 * Otherwise. only returns true if the device and carrier both also support RTT.
Hall Liuf6668912018-10-31 17:05:23 -07007691 */
7692 public boolean isRttEnabled(int subscriptionId) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007693 final long identity = Binder.clearCallingIdentity();
7694 try {
Hall Liu5bab75c2019-12-11 23:58:20 +00007695 boolean isRttSupported = isRttSupported(subscriptionId);
7696 boolean isUserRttSettingOn = Settings.Secure.getInt(
7697 mApp.getContentResolver(), Settings.Secure.RTT_CALLING_MODE, 0) != 0;
7698 boolean shouldIgnoreUserRttSetting = mApp.getCarrierConfigForSubId(subscriptionId)
7699 .getBoolean(CarrierConfigManager.KEY_IGNORE_RTT_MODE_SETTING_BOOL);
7700 return isRttSupported && (isUserRttSettingOn || shouldIgnoreUserRttSetting);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007701 } finally {
7702 Binder.restoreCallingIdentity(identity);
7703 }
Hall Liu3ad5f012018-04-06 16:23:39 -07007704 }
7705
Philip P. Moltmann700a9592019-10-03 11:53:50 -07007706 @Deprecated
7707 @Override
7708 public String getDeviceId(String callingPackage) {
7709 return getDeviceIdWithFeature(callingPackage, null);
7710 }
7711
Sanket Padawe7310cc72015-01-14 09:53:20 -08007712 /**
7713 * Returns the unique device ID of phone, for example, the IMEI for
7714 * GSM and the MEID for CDMA phones. Return null if device ID is not available.
7715 *
7716 * <p>Requires Permission:
7717 * {@link android.Manifest.permission#READ_PHONE_STATE READ_PHONE_STATE}
7718 */
7719 @Override
Philip P. Moltmann700a9592019-10-03 11:53:50 -07007720 public String getDeviceIdWithFeature(String callingPackage, String callingFeatureId) {
Shuo Qian13d89152021-05-10 23:58:11 -07007721 try {
7722 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
7723 } catch (SecurityException se) {
7724 EventLog.writeEvent(0x534e4554, "186530889", Binder.getCallingUid());
7725 throw new SecurityException("Package " + callingPackage + " does not belong to "
7726 + Binder.getCallingUid());
7727 }
Sanket Padawe7310cc72015-01-14 09:53:20 -08007728 final Phone phone = PhoneFactory.getPhone(0);
Jeff Davidson913390f2018-02-23 17:11:49 -08007729 if (phone == null) {
Sanket Padawe7310cc72015-01-14 09:53:20 -08007730 return null;
7731 }
Jeff Davidson913390f2018-02-23 17:11:49 -08007732 int subId = phone.getSubId();
Michael Groover70af6dc2018-10-01 16:23:15 -07007733 if (!TelephonyPermissions.checkCallingOrSelfReadDeviceIdentifiers(mApp, subId,
Philip P. Moltmann700a9592019-10-03 11:53:50 -07007734 callingPackage, callingFeatureId, "getDeviceId")) {
Jeff Davidson913390f2018-02-23 17:11:49 -08007735 return null;
7736 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007737
7738 final long identity = Binder.clearCallingIdentity();
7739 try {
7740 return phone.getDeviceId();
7741 } finally {
7742 Binder.restoreCallingIdentity(identity);
7743 }
Sanket Padawe7310cc72015-01-14 09:53:20 -08007744 }
7745
Ping Sunc67b7c22016-03-02 19:16:45 +08007746 /**
7747 * {@hide}
7748 * Returns the IMS Registration Status on a particular subid
7749 *
7750 * @param subId
7751 */
Brad Ebinger1f2b5082018-02-08 16:11:32 -08007752 public boolean isImsRegistered(int subId) {
Ping Sunc67b7c22016-03-02 19:16:45 +08007753 Phone phone = getPhone(subId);
7754 if (phone != null) {
7755 return phone.isImsRegistered();
7756 } else {
7757 return false;
7758 }
7759 }
7760
Santos Cordon7a1885b2015-02-03 11:15:19 -08007761 @Override
Shuo Qian6e6137d2019-10-30 16:33:31 -07007762 public int getSubIdForPhoneAccountHandle(
Philip P. Moltmann700a9592019-10-03 11:53:50 -07007763 PhoneAccountHandle phoneAccountHandle, String callingPackage, String callingFeatureId) {
Shuo Qian6e6137d2019-10-30 16:33:31 -07007764 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(mApp, getDefaultSubscription(),
Philip P. Moltmann700a9592019-10-03 11:53:50 -07007765 callingPackage, callingFeatureId, "getSubIdForPhoneAccountHandle")) {
Shuo Qian6e6137d2019-10-30 16:33:31 -07007766 throw new SecurityException("Requires READ_PHONE_STATE permission.");
7767 }
7768 final long identity = Binder.clearCallingIdentity();
7769 try {
7770 return PhoneUtils.getSubIdForPhoneAccountHandle(phoneAccountHandle);
7771 } finally {
7772 Binder.restoreCallingIdentity(identity);
7773 }
7774 }
7775
7776 @Override
Tyler Gunnf70ed162019-04-03 15:28:53 -07007777 public @Nullable PhoneAccountHandle getPhoneAccountHandleForSubscriptionId(int subscriptionId) {
Alireza Forouzan4ac4f982021-03-16 22:18:52 -07007778 TelephonyPermissions
Nathan Harold62c68512021-04-06 11:26:02 -07007779 .enforceCallingOrSelfReadPrivilegedPhoneStatePermissionOrCarrierPrivilege(
Alireza Forouzan4ac4f982021-03-16 22:18:52 -07007780 mApp,
7781 subscriptionId,
7782 "getPhoneAccountHandleForSubscriptionId, " + "subscriptionId: " + subscriptionId);
Tyler Gunnf70ed162019-04-03 15:28:53 -07007783 final long identity = Binder.clearCallingIdentity();
7784 try {
7785 Phone phone = getPhone(subscriptionId);
7786 if (phone == null) {
7787 return null;
7788 }
7789 return PhoneUtils.makePstnPhoneAccountHandle(phone);
7790 } finally {
7791 Binder.restoreCallingIdentity(identity);
7792 }
7793 }
7794
Brad Ebinger1f2b5082018-02-08 16:11:32 -08007795 /**
7796 * @return the VoWiFi calling availability.
Nathan Haroldc55097a2015-03-11 18:14:50 -07007797 */
Brad Ebinger1f2b5082018-02-08 16:11:32 -08007798 public boolean isWifiCallingAvailable(int subId) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007799 final long identity = Binder.clearCallingIdentity();
7800 try {
7801 Phone phone = getPhone(subId);
7802 if (phone != null) {
7803 return phone.isWifiCallingEnabled();
7804 } else {
7805 return false;
7806 }
7807 } finally {
7808 Binder.restoreCallingIdentity(identity);
Brad Ebinger1f2b5082018-02-08 16:11:32 -08007809 }
Nathan Haroldc55097a2015-03-11 18:14:50 -07007810 }
7811
Brad Ebinger1f2b5082018-02-08 16:11:32 -08007812 /**
Brad Ebinger1f2b5082018-02-08 16:11:32 -08007813 * @return the VT calling availability.
Etan Cohen3b7a1bc2015-05-28 15:57:13 -07007814 */
Brad Ebinger1f2b5082018-02-08 16:11:32 -08007815 public boolean isVideoTelephonyAvailable(int subId) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007816 final long identity = Binder.clearCallingIdentity();
7817 try {
7818 Phone phone = getPhone(subId);
7819 if (phone != null) {
7820 return phone.isVideoEnabled();
7821 } else {
7822 return false;
7823 }
7824 } finally {
7825 Binder.restoreCallingIdentity(identity);
Brad Ebinger1f2b5082018-02-08 16:11:32 -08007826 }
7827 }
7828
7829 /**
7830 * @return the IMS registration technology for the MMTEL feature. Valid return values are
7831 * defined in {@link ImsRegistrationImplBase}.
7832 */
7833 public @ImsRegistrationImplBase.ImsRegistrationTech int getImsRegTechnologyForMmTel(int subId) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007834 final long identity = Binder.clearCallingIdentity();
7835 try {
7836 Phone phone = getPhone(subId);
7837 if (phone != null) {
7838 return phone.getImsRegistrationTech();
7839 } else {
7840 return ImsRegistrationImplBase.REGISTRATION_TECH_NONE;
7841 }
7842 } finally {
7843 Binder.restoreCallingIdentity(identity);
Brad Ebinger1f2b5082018-02-08 16:11:32 -08007844 }
Etan Cohen3b7a1bc2015-05-28 15:57:13 -07007845 }
7846
Stuart Scott8eef64f2015-04-08 15:13:54 -07007847 @Override
Sarah Chinecc78c42022-03-31 21:16:48 -07007848 public void factoryReset(int subId, String callingPackage) {
paulhu5a773602019-08-23 19:17:33 +08007849 enforceSettingsPermission();
Stuart Scott981d8582015-04-21 14:09:50 -07007850 if (mUserManager.hasUserRestriction(UserManager.DISALLOW_NETWORK_RESET)) {
7851 return;
7852 }
Kai Shif70f46f2021-03-03 13:59:46 -08007853 Phone defaultPhone = getDefaultPhone();
7854 if (defaultPhone != null) {
7855 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
7856 mApp, getDefaultPhone().getSubId(), "factoryReset");
7857 }
Svet Ganovcc087f82015-05-12 20:35:54 -07007858 final long identity = Binder.clearCallingIdentity();
Nazanin Bakhshif782e562018-12-11 15:15:39 -08007859
Svet Ganovcc087f82015-05-12 20:35:54 -07007860 try {
Stuart Scott981d8582015-04-21 14:09:50 -07007861 if (SubscriptionManager.isUsableSubIdValue(subId) && !mUserManager.hasUserRestriction(
7862 UserManager.DISALLOW_CONFIG_MOBILE_NETWORKS)) {
Sooraj Sasindran95c07a92020-07-15 01:35:24 -07007863 setDataEnabledForReason(subId, TelephonyManager.DATA_ENABLED_REASON_USER,
Sarah Chinecc78c42022-03-31 21:16:48 -07007864 getDefaultDataEnabled(), callingPackage);
Svet Ganovcc087f82015-05-12 20:35:54 -07007865 setNetworkSelectionModeAutomatic(subId);
SongFerngWang8c6e82e2021-03-02 22:09:29 +08007866 Phone phone = getPhone(subId);
SongFerngWangfd89b102021-05-27 22:44:54 +08007867 cleanUpAllowedNetworkTypes(phone, subId);
Nazanin Bakhshif782e562018-12-11 15:15:39 -08007868 setDataRoamingEnabled(subId, getDefaultDataRoamingEnabled(subId));
Jordan Liu857e73a2021-03-05 16:24:04 -08007869 getPhone(subId).resetCarrierKeysForImsiEncryption();
Svet Ganovcc087f82015-05-12 20:35:54 -07007870 }
Amit Mahajan7dbbd822019-03-13 17:33:47 -07007871 // There has been issues when Sms raw table somehow stores orphan
7872 // fragments. They lead to garbled message when new fragments come
7873 // in and combined with those stale ones. In case this happens again,
7874 // user can reset all network settings which will clean up this table.
7875 cleanUpSmsRawTable(getDefaultPhone().getContext());
Brad Ebingerbc7dd582019-10-17 17:03:22 -07007876 // Clean up IMS settings as well here.
7877 int slotId = getSlotIndex(subId);
7878 if (slotId > SubscriptionManager.INVALID_SIM_SLOT_INDEX) {
7879 ImsManager.getInstance(mApp, slotId).factoryReset();
7880 }
Naina Nallurid63128d2019-09-17 14:10:30 -07007881
Kai Shif70f46f2021-03-03 13:59:46 -08007882 if (defaultPhone == null) {
7883 return;
7884 }
Naina Nallurid63128d2019-09-17 14:10:30 -07007885 // Erase modem config if erase modem on network setting is enabled.
7886 String configValue = DeviceConfig.getProperty(DeviceConfig.NAMESPACE_TELEPHONY,
7887 RESET_NETWORK_ERASE_MODEM_CONFIG_ENABLED);
7888 if (configValue != null && Boolean.parseBoolean(configValue)) {
Kai Shif70f46f2021-03-03 13:59:46 -08007889 sendEraseModemConfig(defaultPhone);
Naina Nallurid63128d2019-09-17 14:10:30 -07007890 }
Kai Shif70f46f2021-03-03 13:59:46 -08007891
7892 sendEraseDataInSharedPreferences(defaultPhone);
Svet Ganovcc087f82015-05-12 20:35:54 -07007893 } finally {
7894 Binder.restoreCallingIdentity(identity);
Stuart Scott8eef64f2015-04-08 15:13:54 -07007895 }
7896 }
Narayan Kamath1c496c22015-04-16 14:40:19 +01007897
SongFerngWangfd89b102021-05-27 22:44:54 +08007898 @VisibleForTesting
7899 void cleanUpAllowedNetworkTypes(Phone phone, int subId) {
7900 if (phone == null || !SubscriptionManager.isUsableSubscriptionId(subId)) {
7901 return;
7902 }
7903 long defaultNetworkType = RadioAccessFamily.getRafFromNetworkType(
7904 RILConstants.PREFERRED_NETWORK_MODE);
7905 SubscriptionManager.setSubscriptionProperty(subId,
7906 SubscriptionManager.ALLOWED_NETWORK_TYPES,
7907 "user=" + defaultNetworkType);
7908 phone.loadAllowedNetworksFromSubscriptionDatabase();
7909 phone.setAllowedNetworkTypes(TelephonyManager.ALLOWED_NETWORK_TYPES_REASON_USER,
7910 defaultNetworkType, null);
7911 }
7912
Amit Mahajan7dbbd822019-03-13 17:33:47 -07007913 private void cleanUpSmsRawTable(Context context) {
7914 ContentResolver resolver = context.getContentResolver();
7915 Uri uri = Uri.withAppendedPath(Telephony.Sms.CONTENT_URI, "raw/permanentDelete");
7916 resolver.delete(uri, null, null);
7917 }
7918
Narayan Kamath1c496c22015-04-16 14:40:19 +01007919 @Override
chen xu5d3637b2019-01-21 23:31:38 -08007920 public String getSimLocaleForSubscriber(int subId) {
7921 enforceReadPrivilegedPermission("getSimLocaleForSubscriber, subId: " + subId);
7922 final Phone phone = getPhone(subId);
7923 if (phone == null) {
7924 log("getSimLocaleForSubscriber, invalid subId");
chen xu2bb91e42019-01-24 14:35:54 -08007925 return null;
chen xu5d3637b2019-01-21 23:31:38 -08007926 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007927 final long identity = Binder.clearCallingIdentity();
7928 try {
Jack Yu285100e2022-12-02 22:48:35 -08007929 SubscriptionInfo info;
7930 if (PhoneFactory.isSubscriptionManagerServiceEnabled()) {
7931 info = SubscriptionManagerService.getInstance().getActiveSubscriptionInfo(subId,
7932 phone.getContext().getOpPackageName(),
7933 phone.getContext().getAttributionTag());
7934 if (info == null) {
7935 log("getSimLocaleForSubscriber, inactive subId: " + subId);
7936 return null;
7937 }
7938 } else {
7939 info = mSubscriptionController.getActiveSubscriptionInfo(subId,
7940 phone.getContext().getOpPackageName(),
7941 phone.getContext().getAttributionTag());
7942 if (info == null) {
7943 log("getSimLocaleForSubscriber, inactive subId: " + subId);
7944 return null;
7945 }
chen xu6291c472019-02-04 12:55:53 -08007946 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007947 // Try and fetch the locale from the carrier properties or from the SIM language
7948 // preferences (EF-PL and EF-LI)...
7949 final int mcc = info.getMcc();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007950 String simLanguage = null;
chen xu5d3637b2019-01-21 23:31:38 -08007951 final Locale localeFromDefaultSim = phone.getLocaleFromSimAndCarrierPrefs();
7952 if (localeFromDefaultSim != null) {
7953 if (!localeFromDefaultSim.getCountry().isEmpty()) {
7954 if (DBG) log("Using locale from subId: " + subId + " locale: "
7955 + localeFromDefaultSim);
tom hsu60a8dc52022-10-27 00:10:04 +08007956 return matchLocaleFromSupportedLocaleList(phone, localeFromDefaultSim);
chen xu5d3637b2019-01-21 23:31:38 -08007957 } else {
7958 simLanguage = localeFromDefaultSim.getLanguage();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007959 }
7960 }
Narayan Kamath1c496c22015-04-16 14:40:19 +01007961
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007962 // The SIM language preferences only store a language (e.g. fr = French), not an
7963 // exact locale (e.g. fr_FR = French/France). So, if the locale returned from
7964 // the SIM and carrier preferences does not include a country we add the country
7965 // determined from the SIM MCC to provide an exact locale.
zoey chenc730df82019-12-18 17:07:20 +08007966 final Locale mccLocale = LocaleUtils.getLocaleFromMcc(mApp, mcc, simLanguage);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007967 if (mccLocale != null) {
chen xu5d3637b2019-01-21 23:31:38 -08007968 if (DBG) log("No locale from SIM, using mcc locale:" + mccLocale);
tom hsu60a8dc52022-10-27 00:10:04 +08007969 return matchLocaleFromSupportedLocaleList(phone, mccLocale);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007970 }
7971
7972 if (DBG) log("No locale found - returning null");
7973 return null;
7974 } finally {
7975 Binder.restoreCallingIdentity(identity);
7976 }
Narayan Kamath1c496c22015-04-16 14:40:19 +01007977 }
7978
tom hsu0b59d292022-09-29 23:49:21 +08007979 @VisibleForTesting
tom hsu60a8dc52022-10-27 00:10:04 +08007980 String matchLocaleFromSupportedLocaleList(Phone phone, @NonNull Locale inputLocale) {
tom hsu0b59d292022-09-29 23:49:21 +08007981 String[] supportedLocale = com.android.internal.app.LocalePicker.getSupportedLocales(
tom hsu60a8dc52022-10-27 00:10:04 +08007982 phone.getContext());
tom hsu0b59d292022-09-29 23:49:21 +08007983 for (String localeTag : supportedLocale) {
tom hsu60a8dc52022-10-27 00:10:04 +08007984 if (LocaleList.matchesLanguageAndScript(inputLocale, Locale.forLanguageTag(localeTag))
7985 && TextUtils.equals(inputLocale.getCountry(),
tom hsu0b59d292022-09-29 23:49:21 +08007986 Locale.forLanguageTag(localeTag).getCountry())) {
7987 return localeTag;
7988 }
7989 }
7990 return inputLocale.toLanguageTag();
7991 }
7992
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007993 /**
7994 * NOTE: this method assumes permission checks are done and caller identity has been cleared.
7995 */
7996 private List<SubscriptionInfo> getActiveSubscriptionInfoListPrivileged() {
Jack Yu285100e2022-12-02 22:48:35 -08007997 if (PhoneFactory.isSubscriptionManagerServiceEnabled()) {
7998 return SubscriptionManagerService.getInstance().getActiveSubscriptionInfoList(
7999 mApp.getOpPackageName(), mApp.getAttributionTag());
8000 }
Philip P. Moltmann700a9592019-10-03 11:53:50 -07008001 return mSubscriptionController.getActiveSubscriptionInfoList(mApp.getOpPackageName(),
Philip P. Moltmann8d34f0c2020-03-05 16:24:02 -08008002 mApp.getAttributionTag());
Narayan Kamath1c496c22015-04-16 14:40:19 +01008003 }
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -07008004
Gary Jian3aa9a762022-01-24 16:41:19 +08008005 private ActivityStatsTechSpecificInfo[] mLastModemActivitySpecificInfo = null;
8006 private ModemActivityInfo mLastModemActivityInfo = null;
Chenjie Yu1ba97252018-01-11 18:16:20 -08008007
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -07008008 /**
Adam Lesinski903a54c2016-04-11 14:49:52 -07008009 * Responds to the ResultReceiver with the {@link android.telephony.ModemActivityInfo} object
8010 * representing the state of the modem.
8011 *
Chenjie Yu1ba97252018-01-11 18:16:20 -08008012 * NOTE: The underlying implementation clears the modem state, so there should only ever be one
8013 * caller to it. Everyone should call this class to get cumulative data.
Adam Lesinski903a54c2016-04-11 14:49:52 -07008014 * @hide
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -07008015 */
8016 @Override
Adam Lesinski903a54c2016-04-11 14:49:52 -07008017 public void requestModemActivityInfo(ResultReceiver result) {
8018 enforceModifyPermission();
vagdeviaf9a5b92018-08-15 16:01:53 -07008019 WorkSource workSource = getWorkSource(Binder.getCallingUid());
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008020
8021 final long identity = Binder.clearCallingIdentity();
8022 try {
Shuo Qian8f4750a2020-02-20 17:12:10 -08008023 sendRequestAsync(CMD_GET_MODEM_ACTIVITY_INFO, result, null, workSource);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008024 } finally {
8025 Binder.restoreCallingIdentity(identity);
Chenjie Yu1ba97252018-01-11 18:16:20 -08008026 }
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -07008027 }
Jack Yu85bd38a2015-11-09 11:34:32 -08008028
Gary Jian76280a42022-12-07 16:18:33 +08008029 // Checks that ModemActivityInfo is valid. Sleep time and Idle time should be
Siddharth Rayb8114062018-06-17 15:02:38 -07008030 // less than total activity duration.
8031 private boolean isModemActivityInfoValid(ModemActivityInfo info) {
8032 if (info == null) {
8033 return false;
8034 }
8035 int activityDurationMs =
Hall Liu49656c02020-10-09 19:00:11 -07008036 (int) (info.getTimestampMillis() - mLastModemActivityInfo.getTimestampMillis());
Gary Jian76280a42022-12-07 16:18:33 +08008037 activityDurationMs += MODEM_ACTIVITY_TIME_OFFSET_CORRECTION_MS;
8038
Hall Liu49656c02020-10-09 19:00:11 -07008039 int totalTxTimeMs = Arrays.stream(info.getTransmitTimeMillis()).sum();
8040
Siddharth Rayb8114062018-06-17 15:02:38 -07008041 return (info.isValid()
8042 && (info.getSleepTimeMillis() <= activityDurationMs)
Gary Jian76280a42022-12-07 16:18:33 +08008043 && (info.getIdleTimeMillis() <= activityDurationMs));
Siddharth Rayb8114062018-06-17 15:02:38 -07008044 }
8045
Gary Jian3aa9a762022-01-24 16:41:19 +08008046 private void updateLastModemActivityInfo(ModemActivityInfo info, int rat, int freq) {
8047 int[] mergedTxTimeMs = new int [ModemActivityInfo.getNumTxPowerLevels()];
8048 int[] txTimeMs = info.getTransmitTimeMillis(rat, freq);
8049 int[] lastModemTxTimeMs = mLastModemActivityInfo.getTransmitTimeMillis(rat, freq);
8050
8051 for (int lvl = 0; lvl < mergedTxTimeMs.length; lvl++) {
8052 mergedTxTimeMs[lvl] = txTimeMs[lvl] + lastModemTxTimeMs[lvl];
8053 }
8054
8055 mLastModemActivityInfo.setTransmitTimeMillis(rat, freq, mergedTxTimeMs);
8056 mLastModemActivityInfo.setReceiveTimeMillis(
8057 rat,
8058 freq,
8059 info.getReceiveTimeMillis(rat, freq)
8060 + mLastModemActivityInfo.getReceiveTimeMillis(rat, freq));
8061 }
8062
8063 private void updateLastModemActivityInfo(ModemActivityInfo info, int rat) {
8064 int[] mergedTxTimeMs = new int [ModemActivityInfo.getNumTxPowerLevels()];
8065 int[] txTimeMs = info.getTransmitTimeMillis(rat);
8066 int[] lastModemTxTimeMs = mLastModemActivityInfo.getTransmitTimeMillis(rat);
8067
8068 for (int lvl = 0; lvl < mergedTxTimeMs.length; lvl++) {
8069 mergedTxTimeMs[lvl] = txTimeMs[lvl] + lastModemTxTimeMs[lvl];
8070 }
8071 mLastModemActivityInfo.setTransmitTimeMillis(rat, mergedTxTimeMs);
8072 mLastModemActivityInfo.setReceiveTimeMillis(
8073 rat,
8074 info.getReceiveTimeMillis(rat) + mLastModemActivityInfo.getReceiveTimeMillis(rat));
8075 }
8076
8077 /**
8078 * Merge this ModemActivityInfo with mLastModemActivitySpecificInfo
8079 * @param info recent ModemActivityInfo
8080 */
8081 private void mergeModemActivityInfo(ModemActivityInfo info) {
8082 List<ActivityStatsTechSpecificInfo> merged = new ArrayList<>();
Kai Shi917fdc62022-11-28 14:01:02 -08008083 ActivityStatsTechSpecificInfo deltaSpecificInfo;
Gary Jian3aa9a762022-01-24 16:41:19 +08008084 boolean matched;
8085 for (int i = 0; i < info.getSpecificInfoLength(); i++) {
8086 matched = false;
8087 int rat = info.getSpecificInfoRat(i);
8088 int freq = info.getSpecificInfoFrequencyRange(i);
8089 //Check each ActivityStatsTechSpecificInfo in this ModemActivityInfo for new rat returns
8090 //Add a new ActivityStatsTechSpecificInfo if is a new rat, and merge with the original
8091 //if it already exists
8092 for (int j = 0; j < mLastModemActivitySpecificInfo.length; j++) {
8093 if (rat == mLastModemActivityInfo.getSpecificInfoRat(j) && !matched) {
8094 //Merged based on frequency range (MMWAVE vs SUB6) for 5G
8095 if (rat == AccessNetworkConstants.AccessNetworkType.NGRAN) {
8096 if (freq == mLastModemActivityInfo.getSpecificInfoFrequencyRange(j)) {
8097 updateLastModemActivityInfo(info, rat, freq);
8098 matched = true;
8099 }
8100 } else {
8101 updateLastModemActivityInfo(info, rat);
8102 matched = true;
8103 }
8104 }
8105 }
8106
8107 if (!matched) {
Kai Shi917fdc62022-11-28 14:01:02 -08008108 deltaSpecificInfo =
Gary Jian3aa9a762022-01-24 16:41:19 +08008109 new ActivityStatsTechSpecificInfo(
8110 rat,
8111 freq,
8112 info.getTransmitTimeMillis(rat, freq),
8113 (int) info.getReceiveTimeMillis(rat, freq));
Kai Shi917fdc62022-11-28 14:01:02 -08008114 merged.addAll(Arrays.asList(deltaSpecificInfo));
Gary Jian3aa9a762022-01-24 16:41:19 +08008115 }
8116 }
8117 merged.addAll(Arrays.asList(mLastModemActivitySpecificInfo));
8118 mLastModemActivitySpecificInfo =
8119 new ActivityStatsTechSpecificInfo[merged.size()];
8120 merged.toArray(mLastModemActivitySpecificInfo);
8121
8122 mLastModemActivityInfo.setTimestamp(info.getTimestampMillis());
8123 mLastModemActivityInfo.setSleepTimeMillis(
8124 info.getSleepTimeMillis()
8125 + mLastModemActivityInfo.getSleepTimeMillis());
8126 mLastModemActivityInfo.setIdleTimeMillis(
8127 info.getIdleTimeMillis()
8128 + mLastModemActivityInfo.getIdleTimeMillis());
Kai Shi917fdc62022-11-28 14:01:02 -08008129
8130 mLastModemActivityInfo =
8131 new ModemActivityInfo(
8132 mLastModemActivityInfo.getTimestampMillis(),
8133 mLastModemActivityInfo.getSleepTimeMillis(),
8134 mLastModemActivityInfo.getIdleTimeMillis(),
8135 mLastModemActivitySpecificInfo);
8136 }
8137
8138 private ActivityStatsTechSpecificInfo[] deepCopyModemActivitySpecificInfo(
8139 ActivityStatsTechSpecificInfo[] info) {
8140 int infoSize = info.length;
8141 ActivityStatsTechSpecificInfo[] ret = new ActivityStatsTechSpecificInfo[infoSize];
8142 for (int i = 0; i < infoSize; i++) {
8143 ret[i] = new ActivityStatsTechSpecificInfo(
8144 info[i].getRat(), info[i].getFrequencyRange(),
8145 info[i].getTransmitTimeMillis(),
8146 (int) info[i].getReceiveTimeMillis());
8147 }
8148 return ret;
Gary Jian3aa9a762022-01-24 16:41:19 +08008149 }
8150
Jack Yu85bd38a2015-11-09 11:34:32 -08008151 /**
Jack Yu85bd38a2015-11-09 11:34:32 -08008152 * Returns the service state information on specified subscription.
8153 */
8154 @Override
Sooraj Sasindran2250dc02021-11-10 16:42:01 -08008155 public ServiceState getServiceStateForSubscriber(int subId,
8156 boolean renounceFineLocationAccess, boolean renounceCoarseLocationAccess,
8157 String callingPackage, String callingFeatureId) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08008158 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
Philip P. Moltmann700a9592019-10-03 11:53:50 -07008159 mApp, subId, callingPackage, callingFeatureId, "getServiceStateForSubscriber")) {
Jack Yu85bd38a2015-11-09 11:34:32 -08008160 return null;
8161 }
8162
Sooraj Sasindran2250dc02021-11-10 16:42:01 -08008163 boolean hasFinePermission = false;
8164 boolean hasCoarsePermission = false;
8165 if (!renounceFineLocationAccess) {
8166 LocationAccessPolicy.LocationPermissionResult fineLocationResult =
8167 LocationAccessPolicy.checkLocationPermission(mApp,
8168 new LocationAccessPolicy.LocationPermissionQuery.Builder()
8169 .setCallingPackage(callingPackage)
8170 .setCallingFeatureId(callingFeatureId)
8171 .setCallingPid(Binder.getCallingPid())
8172 .setCallingUid(Binder.getCallingUid())
8173 .setMethod("getServiceStateForSubscriber")
8174 .setLogAsInfo(true)
8175 .setMinSdkVersionForFine(Build.VERSION_CODES.Q)
8176 .setMinSdkVersionForCoarse(Build.VERSION_CODES.Q)
8177 .setMinSdkVersionForEnforcement(Build.VERSION_CODES.Q)
8178 .build());
8179 hasFinePermission =
8180 fineLocationResult == LocationAccessPolicy.LocationPermissionResult.ALLOWED;
8181 }
Hall Liuf19c44f2018-11-27 14:38:17 -08008182
Sooraj Sasindran2250dc02021-11-10 16:42:01 -08008183 if (!renounceCoarseLocationAccess) {
8184 LocationAccessPolicy.LocationPermissionResult coarseLocationResult =
8185 LocationAccessPolicy.checkLocationPermission(mApp,
8186 new LocationAccessPolicy.LocationPermissionQuery.Builder()
8187 .setCallingPackage(callingPackage)
8188 .setCallingFeatureId(callingFeatureId)
8189 .setCallingPid(Binder.getCallingPid())
8190 .setCallingUid(Binder.getCallingUid())
8191 .setMethod("getServiceStateForSubscriber")
8192 .setLogAsInfo(true)
8193 .setMinSdkVersionForCoarse(Build.VERSION_CODES.Q)
8194 .setMinSdkVersionForFine(Integer.MAX_VALUE)
8195 .setMinSdkVersionForEnforcement(Build.VERSION_CODES.Q)
8196 .build());
8197 hasCoarsePermission =
8198 coarseLocationResult == LocationAccessPolicy.LocationPermissionResult.ALLOWED;
8199 }
Hall Liuf19c44f2018-11-27 14:38:17 -08008200
Jack Yu479f40e2020-10-27 21:29:25 -07008201 final Phone phone = getPhone(subId);
8202 if (phone == null) {
8203 return null;
8204 }
8205
Jordan Liu0f2bc442020-11-18 16:47:37 -08008206 final long identity = Binder.clearCallingIdentity();
8207
Jack Yu479f40e2020-10-27 21:29:25 -07008208 boolean isCallingPackageDataService = phone.getDataServicePackages()
8209 .contains(callingPackage);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008210 try {
Jordan Liuc437b192020-08-17 10:59:12 -07008211 // isActiveSubId requires READ_PHONE_STATE, which we already check for above
Jack Yu285100e2022-12-02 22:48:35 -08008212 if (PhoneFactory.isSubscriptionManagerServiceEnabled()) {
8213 SubscriptionInfoInternal subInfo = SubscriptionManagerService.getInstance()
8214 .getSubscriptionInfoInternal(subId);
8215 if (subInfo == null || !subInfo.isActive()) {
8216 Rlog.d(LOG_TAG, "getServiceStateForSubscriber returning null for inactive "
8217 + "subId=" + subId);
8218 return null;
8219 }
8220 } else {
8221 if (!mSubscriptionController.isActiveSubId(subId, callingPackage,
8222 callingFeatureId)) {
8223 Rlog.d(LOG_TAG, "getServiceStateForSubscriber returning null for inactive "
8224 + "subId=" + subId);
8225 return null;
8226 }
Jordan Liuc437b192020-08-17 10:59:12 -07008227 }
8228
Hall Liuf19c44f2018-11-27 14:38:17 -08008229 ServiceState ss = phone.getServiceState();
8230
8231 // Scrub out the location info in ServiceState depending on what level of access
8232 // the caller has.
Jack Yu479f40e2020-10-27 21:29:25 -07008233 if (hasFinePermission || isCallingPackageDataService) return ss;
Malcolm Chen5052de62019-12-30 13:56:38 -08008234 if (hasCoarsePermission) return ss.createLocationInfoSanitizedCopy(false);
8235 return ss.createLocationInfoSanitizedCopy(true);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008236 } finally {
8237 Binder.restoreCallingIdentity(identity);
8238 }
Jack Yu85bd38a2015-11-09 11:34:32 -08008239 }
Nancy Chen31f9ba12016-01-06 11:42:12 -08008240
8241 /**
8242 * Returns the URI for the per-account voicemail ringtone set in Phone settings.
8243 *
8244 * @param accountHandle The handle for the {@link PhoneAccount} for which to retrieve the
8245 * voicemail ringtone.
8246 * @return The URI for the ringtone to play when receiving a voicemail from a specific
8247 * PhoneAccount.
8248 */
8249 @Override
8250 public Uri getVoicemailRingtoneUri(PhoneAccountHandle accountHandle) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008251 final long identity = Binder.clearCallingIdentity();
8252 try {
8253 Phone phone = PhoneUtils.getPhoneForPhoneAccountHandle(accountHandle);
8254 if (phone == null) {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08008255 phone = getDefaultPhone();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008256 }
Nancy Chen31f9ba12016-01-06 11:42:12 -08008257
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008258 return VoicemailNotificationSettingsUtil.getRingtoneUri(phone.getContext());
8259 } finally {
8260 Binder.restoreCallingIdentity(identity);
8261 }
Nancy Chen31f9ba12016-01-06 11:42:12 -08008262 }
8263
8264 /**
Ta-wei Yenc33877d2017-01-23 18:11:21 -08008265 * Sets the per-account voicemail ringtone.
8266 *
8267 * <p>Requires that the calling app is the default dialer, or has carrier privileges, or
8268 * has permission {@link android.Manifest.permission#MODIFY_PHONE_STATE MODIFY_PHONE_STATE}.
8269 *
8270 * @param phoneAccountHandle The handle for the {@link PhoneAccount} for which to set the
8271 * voicemail ringtone.
8272 * @param uri The URI for the ringtone to play when receiving a voicemail from a specific
8273 * PhoneAccount.
8274 */
8275 @Override
8276 public void setVoicemailRingtoneUri(String callingPackage,
8277 PhoneAccountHandle phoneAccountHandle, Uri uri) {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08008278 final Phone defaultPhone = getDefaultPhone();
Ta-wei Yenc33877d2017-01-23 18:11:21 -08008279 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
Tyler Gunn5ddfdc92019-10-31 13:08:23 -07008280 TelecomManager tm = defaultPhone.getContext().getSystemService(TelecomManager.class);
8281 if (!TextUtils.equals(callingPackage, tm.getDefaultDialerPackage())) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08008282 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
8283 mApp, PhoneUtils.getSubIdForPhoneAccountHandle(phoneAccountHandle),
8284 "setVoicemailRingtoneUri");
Ta-wei Yenc33877d2017-01-23 18:11:21 -08008285 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008286
8287 final long identity = Binder.clearCallingIdentity();
8288 try {
8289 Phone phone = PhoneUtils.getPhoneForPhoneAccountHandle(phoneAccountHandle);
8290 if (phone == null) {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08008291 phone = defaultPhone;
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008292 }
8293 VoicemailNotificationSettingsUtil.setRingtoneUri(phone.getContext(), uri);
8294 } finally {
8295 Binder.restoreCallingIdentity(identity);
Ta-wei Yenc33877d2017-01-23 18:11:21 -08008296 }
Ta-wei Yenc33877d2017-01-23 18:11:21 -08008297 }
8298
8299 /**
Nancy Chen31f9ba12016-01-06 11:42:12 -08008300 * Returns whether vibration is set for voicemail notification in Phone settings.
8301 *
8302 * @param accountHandle The handle for the {@link PhoneAccount} for which to retrieve the
8303 * voicemail vibration setting.
8304 * @return {@code true} if the vibration is set for this PhoneAccount, {@code false} otherwise.
8305 */
8306 @Override
8307 public boolean isVoicemailVibrationEnabled(PhoneAccountHandle accountHandle) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008308 final long identity = Binder.clearCallingIdentity();
8309 try {
8310 Phone phone = PhoneUtils.getPhoneForPhoneAccountHandle(accountHandle);
8311 if (phone == null) {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08008312 phone = getDefaultPhone();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008313 }
Nancy Chen31f9ba12016-01-06 11:42:12 -08008314
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008315 return VoicemailNotificationSettingsUtil.isVibrationEnabled(phone.getContext());
8316 } finally {
8317 Binder.restoreCallingIdentity(identity);
8318 }
Nancy Chen31f9ba12016-01-06 11:42:12 -08008319 }
8320
Youhan Wange64578a2016-05-02 15:32:42 -07008321 /**
Ta-wei Yenc33877d2017-01-23 18:11:21 -08008322 * Sets the per-account voicemail vibration.
8323 *
8324 * <p>Requires that the calling app is the default dialer, or has carrier privileges, or
8325 * has permission {@link android.Manifest.permission#MODIFY_PHONE_STATE MODIFY_PHONE_STATE}.
8326 *
8327 * @param phoneAccountHandle The handle for the {@link PhoneAccount} for which to set the
8328 * voicemail vibration setting.
8329 * @param enabled Whether to enable or disable vibration for voicemail notifications from a
8330 * specific PhoneAccount.
8331 */
8332 @Override
8333 public void setVoicemailVibrationEnabled(String callingPackage,
8334 PhoneAccountHandle phoneAccountHandle, boolean enabled) {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08008335 final Phone defaultPhone = getDefaultPhone();
Ta-wei Yenc33877d2017-01-23 18:11:21 -08008336 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
Tyler Gunn5ddfdc92019-10-31 13:08:23 -07008337 TelecomManager tm = defaultPhone.getContext().getSystemService(TelecomManager.class);
8338 if (!TextUtils.equals(callingPackage, tm.getDefaultDialerPackage())) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08008339 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
8340 mApp, PhoneUtils.getSubIdForPhoneAccountHandle(phoneAccountHandle),
8341 "setVoicemailVibrationEnabled");
Ta-wei Yenc33877d2017-01-23 18:11:21 -08008342 }
8343
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008344 final long identity = Binder.clearCallingIdentity();
8345 try {
8346 Phone phone = PhoneUtils.getPhoneForPhoneAccountHandle(phoneAccountHandle);
8347 if (phone == null) {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08008348 phone = defaultPhone;
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008349 }
8350 VoicemailNotificationSettingsUtil.setVibrationEnabled(phone.getContext(), enabled);
8351 } finally {
8352 Binder.restoreCallingIdentity(identity);
Ta-wei Yenc33877d2017-01-23 18:11:21 -08008353 }
Ta-wei Yenc33877d2017-01-23 18:11:21 -08008354 }
8355
8356 /**
Youhan Wange64578a2016-05-02 15:32:42 -07008357 * Make sure either called from same process as self (phone) or IPC caller has read privilege.
8358 *
8359 * @throws SecurityException if the caller does not have the required permission
8360 */
Brad Ebinger35c841c2018-10-01 10:40:55 -07008361 private void enforceReadPrivilegedPermission(String message) {
Youhan Wange64578a2016-05-02 15:32:42 -07008362 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.READ_PRIVILEGED_PHONE_STATE,
Brad Ebinger35c841c2018-10-01 10:40:55 -07008363 message);
Youhan Wange64578a2016-05-02 15:32:42 -07008364 }
8365
8366 /**
Ta-wei Yen30a69c82016-12-27 14:52:32 -08008367 * Make sure either called from same process as self (phone) or IPC caller has send SMS
8368 * permission.
8369 *
8370 * @throws SecurityException if the caller does not have the required permission
8371 */
8372 private void enforceSendSmsPermission() {
8373 mApp.enforceCallingOrSelfPermission(permission.SEND_SMS, null);
8374 }
8375
8376 /**
Aishwarya Mallampatifbc70d32022-11-10 20:33:02 +00008377 * Make sure either called from same process as self (phone) or IPC caller has interact across
8378 * users permission.
8379 *
8380 * @throws SecurityException if the caller does not have the required permission
8381 */
8382 private void enforceInteractAcrossUsersPermission(String message) {
8383 mApp.enforceCallingOrSelfPermission(permission.INTERACT_ACROSS_USERS, message);
8384 }
8385
8386 /**
Ta-wei Yen527a9c02017-01-06 15:29:25 -08008387 * Make sure called from the package in charge of visual voicemail.
Ta-wei Yen30a69c82016-12-27 14:52:32 -08008388 *
Ta-wei Yen527a9c02017-01-06 15:29:25 -08008389 * @throws SecurityException if the caller is not the visual voicemail package.
Ta-wei Yen30a69c82016-12-27 14:52:32 -08008390 */
Ta-wei Yen527a9c02017-01-06 15:29:25 -08008391 private void enforceVisualVoicemailPackage(String callingPackage, int subId) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008392 final long identity = Binder.clearCallingIdentity();
8393 try {
8394 ComponentName componentName =
Nazanin Bakhshif782e562018-12-11 15:15:39 -08008395 RemoteVvmTaskManager.getRemotePackage(mApp, subId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008396 if (componentName == null) {
8397 throw new SecurityException(
8398 "Caller not current active visual voicemail package[null]");
8399 }
8400 String vvmPackage = componentName.getPackageName();
8401 if (!callingPackage.equals(vvmPackage)) {
Hui Wang7f657552022-08-16 16:58:25 +00008402 throw new SecurityException("Caller not current active visual voicemail package");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008403 }
8404 } finally {
8405 Binder.restoreCallingIdentity(identity);
Ta-wei Yen30a69c82016-12-27 14:52:32 -08008406 }
8407 }
8408
8409 /**
Youhan Wange64578a2016-05-02 15:32:42 -07008410 * Return the application ID for the app type.
8411 *
8412 * @param subId the subscription ID that this request applies to.
8413 * @param appType the uicc app type.
8414 * @return Application ID for specificied app type, or null if no uicc.
8415 */
8416 @Override
8417 public String getAidForAppType(int subId, int appType) {
Brad Ebinger35c841c2018-10-01 10:40:55 -07008418 enforceReadPrivilegedPermission("getAidForAppType");
Youhan Wange64578a2016-05-02 15:32:42 -07008419 Phone phone = getPhone(subId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008420
8421 final long identity = Binder.clearCallingIdentity();
Youhan Wange64578a2016-05-02 15:32:42 -07008422 try {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008423 if (phone == null) {
8424 return null;
8425 }
8426 String aid = null;
8427 try {
Muralidhar Reddy472c2ae2021-09-29 19:38:40 +00008428 aid = UiccController.getInstance().getUiccPort(phone.getPhoneId())
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008429 .getApplicationByType(appType).getAid();
8430 } catch (Exception e) {
8431 Log.e(LOG_TAG, "Not getting aid. Exception ex=" + e);
8432 }
8433 return aid;
8434 } finally {
8435 Binder.restoreCallingIdentity(identity);
Youhan Wange64578a2016-05-02 15:32:42 -07008436 }
Youhan Wange64578a2016-05-02 15:32:42 -07008437 }
8438
Youhan Wang4001d252016-05-11 10:29:41 -07008439 /**
8440 * Return the Electronic Serial Number.
8441 *
8442 * @param subId the subscription ID that this request applies to.
8443 * @return ESN or null if error.
8444 */
8445 @Override
8446 public String getEsn(int subId) {
Brad Ebinger35c841c2018-10-01 10:40:55 -07008447 enforceReadPrivilegedPermission("getEsn");
Youhan Wang4001d252016-05-11 10:29:41 -07008448 Phone phone = getPhone(subId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008449
8450 final long identity = Binder.clearCallingIdentity();
Youhan Wang4001d252016-05-11 10:29:41 -07008451 try {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008452 if (phone == null) {
8453 return null;
8454 }
8455 String esn = null;
8456 try {
8457 esn = phone.getEsn();
8458 } catch (Exception e) {
8459 Log.e(LOG_TAG, "Not getting ESN. Exception ex=" + e);
8460 }
8461 return esn;
8462 } finally {
8463 Binder.restoreCallingIdentity(identity);
Youhan Wang4001d252016-05-11 10:29:41 -07008464 }
Youhan Wang4001d252016-05-11 10:29:41 -07008465 }
8466
Sanket Padawe99ef1e32016-05-18 16:12:33 -07008467 /**
Youhan Wang66ad5d72016-07-18 17:56:58 -07008468 * Return the Preferred Roaming List Version.
8469 *
8470 * @param subId the subscription ID that this request applies to.
8471 * @return PRLVersion or null if error.
8472 */
8473 @Override
8474 public String getCdmaPrlVersion(int subId) {
Brad Ebinger35c841c2018-10-01 10:40:55 -07008475 enforceReadPrivilegedPermission("getCdmaPrlVersion");
Youhan Wang66ad5d72016-07-18 17:56:58 -07008476 Phone phone = getPhone(subId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008477
8478 final long identity = Binder.clearCallingIdentity();
Youhan Wang66ad5d72016-07-18 17:56:58 -07008479 try {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008480 if (phone == null) {
8481 return null;
8482 }
8483 String cdmaPrlVersion = null;
8484 try {
8485 cdmaPrlVersion = phone.getCdmaPrlVersion();
8486 } catch (Exception e) {
8487 Log.e(LOG_TAG, "Not getting PRLVersion", e);
8488 }
8489 return cdmaPrlVersion;
8490 } finally {
8491 Binder.restoreCallingIdentity(identity);
Youhan Wang66ad5d72016-07-18 17:56:58 -07008492 }
Youhan Wang66ad5d72016-07-18 17:56:58 -07008493 }
8494
8495 /**
Sanket Padawe99ef1e32016-05-18 16:12:33 -07008496 * Get snapshot of Telephony histograms
8497 * @return List of Telephony histograms
8498 * @hide
8499 */
8500 @Override
8501 public List<TelephonyHistogram> getTelephonyHistograms() {
Jeff Davidson7e17e312018-02-13 18:17:36 -08008502 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
8503 mApp, getDefaultSubscription(), "getTelephonyHistograms");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008504
8505 final long identity = Binder.clearCallingIdentity();
8506 try {
8507 return RIL.getTelephonyRILTimingHistograms();
8508 } finally {
8509 Binder.restoreCallingIdentity(identity);
8510 }
Sanket Padawe99ef1e32016-05-18 16:12:33 -07008511 }
Meng Wang1a7c35a2016-05-05 20:56:15 -07008512
8513 /**
8514 * {@hide}
Michele Berionne482f8202018-11-27 18:57:59 -08008515 * Set the allowed carrier list and the excluded carrier list, indicating the priority between
8516 * the two lists.
Meng Wang1a7c35a2016-05-05 20:56:15 -07008517 * Require system privileges. In the future we may add this to carrier APIs.
8518 *
Michele Berionne482f8202018-11-27 18:57:59 -08008519 * @return Integer with the result of the operation, as defined in {@link TelephonyManager}.
Meng Wang1a7c35a2016-05-05 20:56:15 -07008520 */
8521 @Override
Michele Berionne482f8202018-11-27 18:57:59 -08008522 @TelephonyManager.SetCarrierRestrictionResult
8523 public int setAllowedCarriers(CarrierRestrictionRules carrierRestrictionRules) {
Meng Wang1a7c35a2016-05-05 20:56:15 -07008524 enforceModifyPermission();
vagdeviaf9a5b92018-08-15 16:01:53 -07008525 WorkSource workSource = getWorkSource(Binder.getCallingUid());
Sanket Padawe13bac7b2017-03-20 15:04:47 -07008526
Michele Berionne482f8202018-11-27 18:57:59 -08008527 if (carrierRestrictionRules == null) {
8528 throw new NullPointerException("carrier restriction cannot be null");
Meng Wang9b7c4e92017-02-17 11:41:27 -08008529 }
Sanket Padawe13bac7b2017-03-20 15:04:47 -07008530
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008531 final long identity = Binder.clearCallingIdentity();
8532 try {
Michele Berionne482f8202018-11-27 18:57:59 -08008533 return (int) sendRequest(CMD_SET_ALLOWED_CARRIERS, carrierRestrictionRules,
vagdeviaf9a5b92018-08-15 16:01:53 -07008534 workSource);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008535 } finally {
8536 Binder.restoreCallingIdentity(identity);
8537 }
Meng Wang1a7c35a2016-05-05 20:56:15 -07008538 }
8539
8540 /**
8541 * {@hide}
Michele Berionne482f8202018-11-27 18:57:59 -08008542 * Get the allowed carrier list and the excluded carrier list, including the priority between
8543 * the two lists.
Meng Wang1a7c35a2016-05-05 20:56:15 -07008544 * Require system privileges. In the future we may add this to carrier APIs.
8545 *
Michele Berionne482f8202018-11-27 18:57:59 -08008546 * @return {@link android.telephony.CarrierRestrictionRules}
Meng Wang1a7c35a2016-05-05 20:56:15 -07008547 */
8548 @Override
Michele Berionne482f8202018-11-27 18:57:59 -08008549 public CarrierRestrictionRules getAllowedCarriers() {
Brad Ebinger35c841c2018-10-01 10:40:55 -07008550 enforceReadPrivilegedPermission("getAllowedCarriers");
vagdeviaf9a5b92018-08-15 16:01:53 -07008551 WorkSource workSource = getWorkSource(Binder.getCallingUid());
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008552
8553 final long identity = Binder.clearCallingIdentity();
8554 try {
Michele Berionne482f8202018-11-27 18:57:59 -08008555 Object response = sendRequest(CMD_GET_ALLOWED_CARRIERS, null, workSource);
8556 if (response instanceof CarrierRestrictionRules) {
8557 return (CarrierRestrictionRules) response;
8558 }
8559 // Response is an Exception of some kind,
8560 // which is signalled to the user as a NULL retval
8561 return null;
8562 } catch (Exception e) {
8563 Log.e(LOG_TAG, "getAllowedCarriers. Exception ex=" + e);
8564 return null;
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008565 } finally {
8566 Binder.restoreCallingIdentity(identity);
8567 }
Meng Wang1a7c35a2016-05-05 20:56:15 -07008568 }
8569
fionaxu59545b42016-05-25 15:53:37 -07008570 /**
fionaxu59545b42016-05-25 15:53:37 -07008571 * Action set from carrier signalling broadcast receivers to enable/disable radio
8572 * @param subId the subscription ID that this action applies to.
8573 * @param enabled control enable or disable radio.
8574 * {@hide}
8575 */
8576 @Override
8577 public void carrierActionSetRadioEnabled(int subId, boolean enabled) {
8578 enforceModifyPermission();
8579 final Phone phone = getPhone(subId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008580
8581 final long identity = Binder.clearCallingIdentity();
fionaxu59545b42016-05-25 15:53:37 -07008582 if (phone == null) {
8583 loge("carrierAction: SetRadioEnabled fails with invalid sibId: " + subId);
8584 return;
8585 }
8586 try {
8587 phone.carrierActionSetRadioEnabled(enabled);
8588 } catch (Exception e) {
8589 Log.e(LOG_TAG, "carrierAction: SetRadioEnabled fails. Exception ex=" + e);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008590 } finally {
8591 Binder.restoreCallingIdentity(identity);
fionaxu59545b42016-05-25 15:53:37 -07008592 }
8593 }
8594
Ta-wei Yenc236d6b2016-06-21 13:33:12 -07008595 /**
Sooraj Sasindrandaf060f2021-06-15 14:52:55 -07008596 * Enable or disable Voice over NR (VoNR)
8597 * @param subId the subscription ID that this action applies to.
8598 * @param enabled enable or disable VoNR.
8599 * @return operation result.
8600 */
8601 @Override
8602 public int setVoNrEnabled(int subId, boolean enabled) {
8603 enforceModifyPermission();
8604 final Phone phone = getPhone(subId);
8605
8606 final long identity = Binder.clearCallingIdentity();
8607 if (phone == null) {
8608 loge("setVoNrEnabled fails with no phone object for subId: " + subId);
8609 return TelephonyManager.ENABLE_VONR_RADIO_NOT_AVAILABLE;
8610 }
8611
8612 WorkSource workSource = getWorkSource(Binder.getCallingUid());
8613 try {
8614 int result = (int) sendRequest(CMD_ENABLE_VONR, enabled, subId,
8615 workSource);
8616 if (DBG) log("setVoNrEnabled result: " + result);
Gary Jian8dd305f2021-10-14 16:31:35 +08008617
8618 if (result == TelephonyManager.ENABLE_VONR_SUCCESS) {
8619 if (DBG) {
8620 log("Set VoNR settings in siminfo db; subId=" + subId + ", value:" + enabled);
8621 }
8622 SubscriptionManager.setSubscriptionProperty(
8623 subId, SubscriptionManager.NR_ADVANCED_CALLING_ENABLED,
8624 (enabled ? "1" : "0"));
8625 }
8626
Sooraj Sasindrandaf060f2021-06-15 14:52:55 -07008627 return result;
8628 } finally {
8629 Binder.restoreCallingIdentity(identity);
8630 }
8631 }
8632
8633 /**
8634 * Is voice over NR enabled
8635 * @return true if VoNR is enabled else false
8636 */
8637 @Override
8638 public boolean isVoNrEnabled(int subId) {
8639 enforceReadPrivilegedPermission("isVoNrEnabled");
8640 WorkSource workSource = getWorkSource(Binder.getCallingUid());
8641 final long identity = Binder.clearCallingIdentity();
8642 try {
8643 boolean isEnabled = (boolean) sendRequest(CMD_IS_VONR_ENABLED,
8644 null, subId, workSource);
8645 if (DBG) log("isVoNrEnabled: " + isEnabled);
8646 return isEnabled;
8647 } finally {
8648 Binder.restoreCallingIdentity(identity);
8649 }
8650 }
8651
8652 /**
fionaxu8da9cb12017-05-23 15:02:46 -07008653 * Action set from carrier signalling broadcast receivers to start/stop reporting the default
8654 * network status based on which carrier apps could apply actions accordingly,
8655 * enable/disable default url handler for example.
8656 *
8657 * @param subId the subscription ID that this action applies to.
8658 * @param report control start/stop reporting the default network status.
8659 * {@hide}
8660 */
8661 @Override
8662 public void carrierActionReportDefaultNetworkStatus(int subId, boolean report) {
8663 enforceModifyPermission();
8664 final Phone phone = getPhone(subId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008665
8666 final long identity = Binder.clearCallingIdentity();
fionaxu8da9cb12017-05-23 15:02:46 -07008667 if (phone == null) {
8668 loge("carrierAction: ReportDefaultNetworkStatus fails with invalid sibId: " + subId);
8669 return;
8670 }
8671 try {
8672 phone.carrierActionReportDefaultNetworkStatus(report);
8673 } catch (Exception e) {
8674 Log.e(LOG_TAG, "carrierAction: ReportDefaultNetworkStatus fails. Exception ex=" + e);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008675 } finally {
8676 Binder.restoreCallingIdentity(identity);
fionaxu8da9cb12017-05-23 15:02:46 -07008677 }
8678 }
8679
8680 /**
fionaxud9622282017-07-17 17:51:30 -07008681 * Action set from carrier signalling broadcast receivers to reset all carrier actions
8682 * @param subId the subscription ID that this action applies to.
8683 * {@hide}
8684 */
8685 @Override
8686 public void carrierActionResetAll(int subId) {
8687 enforceModifyPermission();
8688 final Phone phone = getPhone(subId);
8689 if (phone == null) {
8690 loge("carrierAction: ResetAll fails with invalid sibId: " + subId);
8691 return;
8692 }
8693 try {
8694 phone.carrierActionResetAll();
8695 } catch (Exception e) {
8696 Log.e(LOG_TAG, "carrierAction: ResetAll fails. Exception ex=" + e);
8697 }
8698 }
8699
8700 /**
Ta-wei Yenc236d6b2016-06-21 13:33:12 -07008701 * Called when "adb shell dumpsys phone" is invoked. Dump is also automatically invoked when a
8702 * bug report is being generated.
8703 */
8704 @Override
Ta-wei Yen99282e02016-06-21 18:19:35 -07008705 protected void dump(FileDescriptor fd, PrintWriter writer, String[] args) {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08008706 if (mApp.checkCallingOrSelfPermission(android.Manifest.permission.DUMP)
8707 != PackageManager.PERMISSION_GRANTED) {
dcashman22b950d2016-06-27 11:39:02 -07008708 writer.println("Permission Denial: can't dump Phone from pid="
8709 + Binder.getCallingPid()
8710 + ", uid=" + Binder.getCallingUid()
8711 + "without permission "
8712 + android.Manifest.permission.DUMP);
8713 return;
8714 }
Nazanin Bakhshif782e562018-12-11 15:15:39 -08008715 DumpsysHandler.dump(mApp, fd, writer, args);
Ta-wei Yenc236d6b2016-06-21 13:33:12 -07008716 }
Jack Yueb89b242016-06-22 13:27:47 -07008717
Brad Ebingerdac2f002018-04-03 15:17:52 -07008718 @Override
Hall Liua1548bd2019-12-24 14:14:12 -08008719 public int handleShellCommand(@NonNull ParcelFileDescriptor in,
8720 @NonNull ParcelFileDescriptor out, @NonNull ParcelFileDescriptor err,
8721 @NonNull String[] args) {
8722 return new TelephonyShellCommand(this, getDefaultPhone().getContext()).exec(
8723 this, in.getFileDescriptor(), out.getFileDescriptor(),
8724 err.getFileDescriptor(), args);
Brad Ebingerdac2f002018-04-03 15:17:52 -07008725 }
8726
Jack Yueb89b242016-06-22 13:27:47 -07008727 /**
Sooraj Sasindranaf1b5132020-05-05 21:06:20 +00008728 * Policy control of data connection with reason {@@TelephonyManager.DataEnabledReason}
Greg Kaiser17f41752020-05-05 16:47:47 +00008729 * @param subId Subscription index
Sarah Chinecc78c42022-03-31 21:16:48 -07008730 * @param reason The reason the data enable change is taking place.
Sooraj Sasindranaf1b5132020-05-05 21:06:20 +00008731 * @param enabled True if enabling the data, otherwise disabling.
Sarah Chinecc78c42022-03-31 21:16:48 -07008732 * @param callingPackage The package that changed the data enabled state.
Sooraj Sasindranaf1b5132020-05-05 21:06:20 +00008733 * @hide
Jack Yu75ab2952016-07-08 14:29:33 -07008734 */
8735 @Override
Sooraj Sasindran95c07a92020-07-15 01:35:24 -07008736 public void setDataEnabledForReason(int subId, @TelephonyManager.DataEnabledReason int reason,
Sarah Chinecc78c42022-03-31 21:16:48 -07008737 boolean enabled, String callingPackage) {
Sooraj Sasindranaf1b5132020-05-05 21:06:20 +00008738 if (reason == TelephonyManager.DATA_ENABLED_REASON_USER
8739 || reason == TelephonyManager.DATA_ENABLED_REASON_CARRIER) {
8740 try {
8741 TelephonyPermissions.enforceCallingOrSelfCarrierPrivilege(
Sooraj Sasindran95c07a92020-07-15 01:35:24 -07008742 mApp, subId, "setDataEnabledForReason");
Sooraj Sasindranaf1b5132020-05-05 21:06:20 +00008743 } catch (SecurityException se) {
8744 enforceModifyPermission();
8745 }
8746 } else {
8747 enforceModifyPermission();
8748 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008749
8750 final long identity = Binder.clearCallingIdentity();
8751 try {
8752 Phone phone = getPhone(subId);
8753 if (phone != null) {
Sooraj Sasindranaf1b5132020-05-05 21:06:20 +00008754 if (reason == TelephonyManager.DATA_ENABLED_REASON_CARRIER) {
8755 phone.carrierActionSetMeteredApnsEnabled(enabled);
8756 } else {
Jack Yu7968c6d2022-07-31 00:43:21 -07008757 phone.getDataSettingsManager().setDataEnabled(
8758 reason, enabled, callingPackage);
Sooraj Sasindranaf1b5132020-05-05 21:06:20 +00008759 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008760 }
8761 } finally {
8762 Binder.restoreCallingIdentity(identity);
Jack Yu75ab2952016-07-08 14:29:33 -07008763 }
8764 }
Sooraj Sasindran9a909312016-07-18 11:57:25 -07008765
8766 /**
8767 * Get Client request stats
8768 * @return List of Client Request Stats
8769 * @hide
8770 */
8771 @Override
Philip P. Moltmann700a9592019-10-03 11:53:50 -07008772 public List<ClientRequestStats> getClientRequestStats(String callingPackage,
8773 String callingFeatureId, int subId) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08008774 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
Philip P. Moltmann700a9592019-10-03 11:53:50 -07008775 mApp, subId, callingPackage, callingFeatureId, "getClientRequestStats")) {
Sooraj Sasindran9a909312016-07-18 11:57:25 -07008776 return null;
8777 }
Sooraj Sasindran9a909312016-07-18 11:57:25 -07008778 Phone phone = getPhone(subId);
Sooraj Sasindran9a909312016-07-18 11:57:25 -07008779
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008780 final long identity = Binder.clearCallingIdentity();
8781 try {
8782 if (phone != null) {
8783 return phone.getClientRequestStats();
8784 }
8785
8786 return null;
8787 } finally {
8788 Binder.restoreCallingIdentity(identity);
8789 }
Sooraj Sasindran9a909312016-07-18 11:57:25 -07008790 }
8791
Narayan Kamathf04b5a12018-01-09 11:47:15 +00008792 private WorkSource getWorkSource(int uid) {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08008793 String packageName = mApp.getPackageManager().getNameForUid(uid);
Hunter Knepshieldd03383b2022-03-29 22:47:54 +00008794 if (uid == Process.ROOT_UID && packageName == null) {
8795 // Downstream WorkSource attribution inside the RIL requires both a UID and package name
8796 // to be set for wakelock tracking, otherwise RIL requests fail with a runtime
8797 // exception. ROOT_UID seems not to have a valid package name returned by
8798 // PackageManager, so just fake it here to avoid issues when running telephony shell
8799 // commands that plumb through the RIL as root, like so:
8800 // $ adb root
8801 // $ adb shell cmd phone ...
8802 packageName = "root";
8803 }
Narayan Kamathf04b5a12018-01-09 11:47:15 +00008804 return new WorkSource(uid, packageName);
Sooraj Sasindran9a909312016-07-18 11:57:25 -07008805 }
Jack Yueb4124c2017-02-16 15:32:43 -08008806
8807 /**
Grace Chen70990072017-03-24 17:21:30 -07008808 * Set SIM card power state.
Jack Yueb4124c2017-02-16 15:32:43 -08008809 *
Sanket Padawe13bac7b2017-03-20 15:04:47 -07008810 * @param slotIndex SIM slot id.
Grace Chen70990072017-03-24 17:21:30 -07008811 * @param state State of SIM (power down, power up, pass through)
8812 * - {@link android.telephony.TelephonyManager#CARD_POWER_DOWN}
8813 * - {@link android.telephony.TelephonyManager#CARD_POWER_UP}
8814 * - {@link android.telephony.TelephonyManager#CARD_POWER_UP_PASS_THROUGH}
Jack Yueb4124c2017-02-16 15:32:43 -08008815 *
8816 **/
8817 @Override
Grace Chen70990072017-03-24 17:21:30 -07008818 public void setSimPowerStateForSlot(int slotIndex, int state) {
Jack Yueb4124c2017-02-16 15:32:43 -08008819 enforceModifyPermission();
Sanket Padawe13bac7b2017-03-20 15:04:47 -07008820 Phone phone = PhoneFactory.getPhone(slotIndex);
8821
vagdeviaf9a5b92018-08-15 16:01:53 -07008822 WorkSource workSource = getWorkSource(Binder.getCallingUid());
8823
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008824 final long identity = Binder.clearCallingIdentity();
8825 try {
8826 if (phone != null) {
Jordan Liu109698e2020-11-24 14:50:34 -08008827 phone.setSimPowerState(state, null, workSource);
8828 }
8829 } finally {
8830 Binder.restoreCallingIdentity(identity);
8831 }
8832 }
8833
8834 /**
8835 * Set SIM card power state.
8836 *
8837 * @param slotIndex SIM slot id.
8838 * @param state State of SIM (power down, power up, pass through)
8839 * @param callback callback to trigger after success or failure
8840 * - {@link android.telephony.TelephonyManager#CARD_POWER_DOWN}
8841 * - {@link android.telephony.TelephonyManager#CARD_POWER_UP}
8842 * - {@link android.telephony.TelephonyManager#CARD_POWER_UP_PASS_THROUGH}
8843 *
8844 **/
8845 @Override
8846 public void setSimPowerStateForSlotWithCallback(int slotIndex, int state,
8847 IIntegerConsumer callback) {
8848 enforceModifyPermission();
8849 Phone phone = PhoneFactory.getPhone(slotIndex);
8850
8851 WorkSource workSource = getWorkSource(Binder.getCallingUid());
8852
8853 final long identity = Binder.clearCallingIdentity();
8854 try {
8855 if (phone != null) {
8856 Pair<Integer, IIntegerConsumer> arguments = Pair.create(state, callback);
8857 sendRequestAsync(CMD_SET_SIM_POWER, arguments, phone, workSource);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008858 }
8859 } finally {
8860 Binder.restoreCallingIdentity(identity);
Jack Yueb4124c2017-02-16 15:32:43 -08008861 }
8862 }
Shuo Qiandd210312017-04-12 22:11:33 +00008863
Tyler Gunn65d45c22017-06-05 11:22:26 -07008864 private boolean isUssdApiAllowed(int subId) {
8865 CarrierConfigManager configManager =
Nazanin Bakhshif782e562018-12-11 15:15:39 -08008866 (CarrierConfigManager) mApp.getSystemService(Context.CARRIER_CONFIG_SERVICE);
Tyler Gunn65d45c22017-06-05 11:22:26 -07008867 if (configManager == null) {
8868 return false;
8869 }
8870 PersistableBundle pb = configManager.getConfigForSubId(subId);
8871 if (pb == null) {
8872 return false;
8873 }
8874 return pb.getBoolean(
8875 CarrierConfigManager.KEY_ALLOW_USSD_REQUESTS_VIA_TELEPHONY_MANAGER_BOOL);
8876 }
8877
Shuo Qiandd210312017-04-12 22:11:33 +00008878 /**
8879 * Check if phone is in emergency callback mode
8880 * @return true if phone is in emergency callback mode
8881 * @param subId sub id
8882 */
goneil9c5f4872017-12-05 14:07:56 -08008883 @Override
Shuo Qiandd210312017-04-12 22:11:33 +00008884 public boolean getEmergencyCallbackMode(int subId) {
Brad Ebinger35c841c2018-10-01 10:40:55 -07008885 enforceReadPrivilegedPermission("getEmergencyCallbackMode");
Shuo Qiandd210312017-04-12 22:11:33 +00008886 final Phone phone = getPhone(subId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008887
8888 final long identity = Binder.clearCallingIdentity();
8889 try {
8890 if (phone != null) {
8891 return phone.isInEcm();
8892 } else {
8893 return false;
8894 }
8895 } finally {
8896 Binder.restoreCallingIdentity(identity);
Shuo Qiandd210312017-04-12 22:11:33 +00008897 }
8898 }
Nathan Harold46b42aa2017-03-10 19:38:22 -08008899
8900 /**
8901 * Get the current signal strength information for the given subscription.
8902 * Because this information is not updated when the device is in a low power state
8903 * it should not be relied-upon to be current.
8904 * @param subId Subscription index
8905 * @return the most recent cached signal strength info from the modem
8906 */
8907 @Override
8908 public SignalStrength getSignalStrength(int subId) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008909 final long identity = Binder.clearCallingIdentity();
8910 try {
8911 Phone p = getPhone(subId);
8912 if (p == null) {
8913 return null;
8914 }
Nathan Harold46b42aa2017-03-10 19:38:22 -08008915
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008916 return p.getSignalStrength();
8917 } finally {
8918 Binder.restoreCallingIdentity(identity);
8919 }
Nathan Harold46b42aa2017-03-10 19:38:22 -08008920 }
Holly Jiuyu Sun01c47ad2018-01-24 17:56:33 +00008921
Pengquan Meng77b7f132018-08-22 14:49:57 -07008922 /**
Chen Xuf792fd62018-10-17 17:54:36 +00008923 * Get the current modem radio state for the given slot.
8924 * @param slotIndex slot index.
8925 * @param callingPackage the name of the package making the call.
Philip P. Moltmann700a9592019-10-03 11:53:50 -07008926 * @param callingFeatureId The feature in the package.
Chen Xuf792fd62018-10-17 17:54:36 +00008927 * @return the current radio power state from the modem
8928 */
8929 @Override
Philip P. Moltmann700a9592019-10-03 11:53:50 -07008930 public int getRadioPowerState(int slotIndex, String callingPackage, String callingFeatureId) {
Chen Xuf792fd62018-10-17 17:54:36 +00008931 Phone phone = PhoneFactory.getPhone(slotIndex);
8932 if (phone != null) {
Philip P. Moltmann700a9592019-10-03 11:53:50 -07008933 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(mApp, phone.getSubId(),
8934 callingPackage, callingFeatureId, "getRadioPowerState")) {
Chen Xuf792fd62018-10-17 17:54:36 +00008935 return TelephonyManager.RADIO_POWER_UNAVAILABLE;
8936 }
8937
8938 final long identity = Binder.clearCallingIdentity();
8939 try {
8940 return phone.getRadioPowerState();
8941 } finally {
8942 Binder.restoreCallingIdentity(identity);
8943 }
8944 }
8945 return TelephonyManager.RADIO_POWER_UNAVAILABLE;
8946 }
8947
8948 /**
Pengquan Meng77b7f132018-08-22 14:49:57 -07008949 * Checks if data roaming is enabled on the subscription with id {@code subId}.
8950 *
8951 * <p>Requires one of the following permissions:
8952 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE},
Sooraj Sasindran5d051bf2021-11-05 13:14:15 -07008953 * {@link android.Manifest.permission#READ_BASIC_PHONE_STATE},
Pengquan Meng77b7f132018-08-22 14:49:57 -07008954 * {@link android.Manifest.permission#READ_PHONE_STATE} or that the calling app has carrier
8955 * privileges.
8956 *
8957 * @param subId subscription id
8958 * @return {@code true} if data roaming is enabled on this subscription, otherwise return
8959 * {@code false}.
8960 */
8961 @Override
8962 public boolean isDataRoamingEnabled(int subId) {
Sooraj Sasindran5d051bf2021-11-05 13:14:15 -07008963 String functionName = "isDataRoamingEnabled";
Shuo Qian093013d2020-08-13 15:42:55 -07008964 try {
Sooraj Sasindran5d051bf2021-11-05 13:14:15 -07008965 try {
8966 mApp.enforceCallingOrSelfPermission(
8967 android.Manifest.permission.ACCESS_NETWORK_STATE,
8968 functionName);
Sooraj Sasindran12545cc2022-08-22 14:30:25 -07008969 } catch (SecurityException e) {
Sooraj Sasindran5d051bf2021-11-05 13:14:15 -07008970 mApp.enforceCallingOrSelfPermission(
8971 permission.READ_BASIC_PHONE_STATE, functionName);
8972 }
Sooraj Sasindran12545cc2022-08-22 14:30:25 -07008973 } catch (SecurityException e) {
Nathan Harold62c68512021-04-06 11:26:02 -07008974 TelephonyPermissions.enforceCallingOrSelfReadPhoneStatePermissionOrCarrierPrivilege(
Sooraj Sasindran5d051bf2021-11-05 13:14:15 -07008975 mApp, subId, functionName);
Shuo Qian093013d2020-08-13 15:42:55 -07008976 }
Pengquan Meng44e66f12019-04-01 10:48:20 -07008977
Pengquan Menga1bb6272018-09-06 09:59:22 -07008978 boolean isEnabled = false;
8979 final long identity = Binder.clearCallingIdentity();
Pengquan Meng77b7f132018-08-22 14:49:57 -07008980 try {
Pengquan Menga1bb6272018-09-06 09:59:22 -07008981 Phone phone = getPhone(subId);
8982 isEnabled = phone != null ? phone.getDataRoamingEnabled() : false;
Pengquan Menga1bb6272018-09-06 09:59:22 -07008983 } finally {
8984 Binder.restoreCallingIdentity(identity);
Pengquan Meng77b7f132018-08-22 14:49:57 -07008985 }
Pengquan Menga1bb6272018-09-06 09:59:22 -07008986 return isEnabled;
Pengquan Meng77b7f132018-08-22 14:49:57 -07008987 }
8988
8989
8990 /**
8991 * Enables/Disables the data roaming on the subscription with id {@code subId}.
8992 *
8993 * <p> Requires permission:
8994 * {@link android.Manifest.permission#MODIFY_PHONE_STATE} or that the calling app has carrier
8995 * privileges.
8996 *
8997 * @param subId subscription id
8998 * @param isEnabled {@code true} means enable, {@code false} means disable.
8999 */
9000 @Override
9001 public void setDataRoamingEnabled(int subId, boolean isEnabled) {
Pengquan Meng44e66f12019-04-01 10:48:20 -07009002 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
9003 mApp, subId, "setDataRoamingEnabled");
9004
Pengquan Menga1bb6272018-09-06 09:59:22 -07009005 final long identity = Binder.clearCallingIdentity();
9006 try {
Pengquan Menga1bb6272018-09-06 09:59:22 -07009007 Phone phone = getPhone(subId);
9008 if (phone != null) {
9009 phone.setDataRoamingEnabled(isEnabled);
9010 }
9011 } finally {
9012 Binder.restoreCallingIdentity(identity);
Pengquan Meng77b7f132018-08-22 14:49:57 -07009013 }
9014 }
9015
Holly Jiuyu Sun01c47ad2018-01-24 17:56:33 +00009016 @Override
Pengquan Meng6884a2c2018-10-03 12:19:13 -07009017 public boolean isManualNetworkSelectionAllowed(int subId) {
tom hsuc91afc72020-01-06 23:46:07 +08009018 TelephonyPermissions
Nathan Harold62c68512021-04-06 11:26:02 -07009019 .enforceCallingOrSelfReadPrivilegedPhoneStatePermissionOrCarrierPrivilege(
Pengquan Meng44e66f12019-04-01 10:48:20 -07009020 mApp, subId, "isManualNetworkSelectionAllowed");
9021
Pengquan Meng6884a2c2018-10-03 12:19:13 -07009022 boolean isAllowed = true;
9023 final long identity = Binder.clearCallingIdentity();
9024 try {
Pengquan Meng6884a2c2018-10-03 12:19:13 -07009025 Phone phone = getPhone(subId);
9026 if (phone != null) {
9027 isAllowed = phone.isCspPlmnEnabled();
9028 }
9029 } finally {
9030 Binder.restoreCallingIdentity(identity);
9031 }
9032 return isAllowed;
9033 }
9034
Muralidhar Reddyeb809e32021-11-19 03:07:54 +00009035 private boolean haveCarrierPrivilegeAccess(UiccPort port, String callingPackage) {
9036 UiccProfile profile = port.getUiccProfile();
Hunter Knepshieldcad7f0b2021-12-13 15:08:35 -08009037 if (profile == null) {
Muralidhar Reddyeb809e32021-11-19 03:07:54 +00009038 return false;
Muralidhar Reddy472c2ae2021-09-29 19:38:40 +00009039 }
Hunter Knepshieldcad7f0b2021-12-13 15:08:35 -08009040 Phone phone = PhoneFactory.getPhone(profile.getPhoneId());
9041 if (phone == null) {
9042 return false;
9043 }
9044 CarrierPrivilegesTracker cpt = phone.getCarrierPrivilegesTracker();
9045 return cpt != null && cpt.getCarrierPrivilegeStatusForPackage(callingPackage)
9046 == TelephonyManager.CARRIER_PRIVILEGE_STATUS_HAS_ACCESS;
Muralidhar Reddy472c2ae2021-09-29 19:38:40 +00009047 }
9048
Pengquan Meng6884a2c2018-10-03 12:19:13 -07009049 @Override
Jordan Liu75f43ea2019-01-17 16:56:37 -08009050 public List<UiccCardInfo> getUiccCardsInfo(String callingPackage) {
sandeepjsa208e3b2021-11-17 04:05:58 +00009051 // Verify that the callingPackage belongs to the calling UID
Jordan Liu4cda4552020-03-23 11:55:07 -07009052 mApp.getSystemService(AppOpsManager.class)
9053 .checkPackage(Binder.getCallingUid(), callingPackage);
9054
Jordan Liu1e142fc2019-04-22 15:10:43 -07009055 boolean hasReadPermission = false;
sandeepjsb6c87872021-09-27 15:34:44 +00009056 boolean isIccIdAccessRestricted = false;
Jordan Liuc65bc952019-02-12 17:54:02 -08009057 try {
9058 enforceReadPrivilegedPermission("getUiccCardsInfo");
Jordan Liu1e142fc2019-04-22 15:10:43 -07009059 hasReadPermission = true;
Jordan Liuc65bc952019-02-12 17:54:02 -08009060 } catch (SecurityException e) {
9061 // even without READ_PRIVILEGED_PHONE_STATE, we allow the call to continue if the caller
9062 // has carrier privileges on an active UICC
Rambo Wange7209ce2022-02-23 13:41:02 -08009063 if (checkCarrierPrivilegesForPackageAnyPhoneWithPermission(callingPackage)
Jordan Liuc65bc952019-02-12 17:54:02 -08009064 != TelephonyManager.CARRIER_PRIVILEGE_STATUS_HAS_ACCESS) {
Jordan Liu1e142fc2019-04-22 15:10:43 -07009065 throw new SecurityException("Caller does not have permission.");
Jordan Liuc65bc952019-02-12 17:54:02 -08009066 }
Jordan Liu75f43ea2019-01-17 16:56:37 -08009067 }
sandeepjsb6c87872021-09-27 15:34:44 +00009068 // checking compatibility, if calling app's target SDK is T and beyond.
9069 if (CompatChanges.isChangeEnabled(GET_API_SIGNATURES_FROM_UICC_PORT_INFO,
9070 Binder.getCallingUid())) {
9071 isIccIdAccessRestricted = true;
9072 }
Jordan Liu5aa07002018-12-18 15:44:48 -08009073 final long identity = Binder.clearCallingIdentity();
9074 try {
Jordan Liu75f43ea2019-01-17 16:56:37 -08009075 UiccController uiccController = UiccController.getInstance();
9076 ArrayList<UiccCardInfo> cardInfos = uiccController.getAllUiccCardInfos();
Jordan Liu1e142fc2019-04-22 15:10:43 -07009077 if (hasReadPermission) {
9078 return cardInfos;
Jordan Liu75f43ea2019-01-17 16:56:37 -08009079 }
Jordan Liu1e142fc2019-04-22 15:10:43 -07009080
9081 // Remove private info if the caller doesn't have access
9082 ArrayList<UiccCardInfo> filteredInfos = new ArrayList<>();
9083 for (UiccCardInfo cardInfo : cardInfos) {
sandeepjsb6c87872021-09-27 15:34:44 +00009084 //setting the value after compatibility check
9085 cardInfo.setIccIdAccessRestricted(isIccIdAccessRestricted);
Jordan Liu1e142fc2019-04-22 15:10:43 -07009086 // For an inactive eUICC, the UiccCard will be null even though the UiccCardInfo
9087 // is available
sandeepjsb6c87872021-09-27 15:34:44 +00009088 UiccCard card = uiccController.getUiccCardForSlot(cardInfo.getPhysicalSlotIndex());
Muralidhar Reddyeb809e32021-11-19 03:07:54 +00009089 if (card == null) {
9090 // assume no access if the card is unavailable
sandeepjsb6c87872021-09-27 15:34:44 +00009091 filteredInfos.add(getUiccCardInfoUnPrivileged(cardInfo));
Jordan Liu1e142fc2019-04-22 15:10:43 -07009092 continue;
9093 }
Muralidhar Reddyeb809e32021-11-19 03:07:54 +00009094 Collection<UiccPortInfo> portInfos = cardInfo.getPorts();
9095 if (portInfos.isEmpty()) {
sandeepjsb6c87872021-09-27 15:34:44 +00009096 filteredInfos.add(getUiccCardInfoUnPrivileged(cardInfo));
Muralidhar Reddyeb809e32021-11-19 03:07:54 +00009097 continue;
Jordan Liu1e142fc2019-04-22 15:10:43 -07009098 }
Muralidhar Reddyeb809e32021-11-19 03:07:54 +00009099 List<UiccPortInfo> uiccPortInfos = new ArrayList<>();
9100 for (UiccPortInfo portInfo : portInfos) {
9101 UiccPort port = uiccController.getUiccPortForSlot(
9102 cardInfo.getPhysicalSlotIndex(), portInfo.getPortIndex());
9103 if (port == null) {
9104 // assume no access if port is null
9105 uiccPortInfos.add(getUiccPortInfoUnPrivileged(portInfo));
9106 continue;
9107 }
9108 if (haveCarrierPrivilegeAccess(port, callingPackage)) {
9109 uiccPortInfos.add(portInfo);
9110 } else {
9111 uiccPortInfos.add(getUiccPortInfoUnPrivileged(portInfo));
9112 }
9113 }
9114 filteredInfos.add(new UiccCardInfo(
9115 cardInfo.isEuicc(),
9116 cardInfo.getCardId(),
9117 null,
9118 cardInfo.getPhysicalSlotIndex(),
9119 cardInfo.isRemovable(),
9120 cardInfo.isMultipleEnabledProfilesSupported(),
9121 uiccPortInfos));
Jordan Liu1e142fc2019-04-22 15:10:43 -07009122 }
9123 return filteredInfos;
Jordan Liu5aa07002018-12-18 15:44:48 -08009124 } finally {
9125 Binder.restoreCallingIdentity(identity);
9126 }
9127 }
9128
sandeepjsb6c87872021-09-27 15:34:44 +00009129 /**
9130 * Returns a copy of the UiccCardinfo with the EID and ICCID set to null. These values are
9131 * generally private and require carrier privileges to view.
9132 *
9133 * @hide
9134 */
9135 @NonNull
9136 public UiccCardInfo getUiccCardInfoUnPrivileged(UiccCardInfo cardInfo) {
9137 List<UiccPortInfo> portinfo = new ArrayList<>();
9138 for (UiccPortInfo portinfos : cardInfo.getPorts()) {
9139 portinfo.add(getUiccPortInfoUnPrivileged(portinfos));
9140 }
9141 return new UiccCardInfo(
9142 cardInfo.isEuicc(),
9143 cardInfo.getCardId(),
9144 null,
9145 cardInfo.getPhysicalSlotIndex(),
9146 cardInfo.isRemovable(),
9147 cardInfo.isMultipleEnabledProfilesSupported(),
9148 portinfo
9149 );
9150 }
Holly Jiuyu Sun01c47ad2018-01-24 17:56:33 +00009151
sandeepjsb6c87872021-09-27 15:34:44 +00009152 /**
9153 * @hide
9154 * @return a copy of the UiccPortInfo with ICCID set to {@link UiccPortInfo#ICCID_REDACTED}.
9155 * These values are generally private and require carrier privileges to view.
9156 */
9157 @NonNull
9158 public UiccPortInfo getUiccPortInfoUnPrivileged(UiccPortInfo portInfo) {
9159 return new UiccPortInfo(
9160 UiccPortInfo.ICCID_REDACTED,
9161 portInfo.getPortIndex(),
9162 portInfo.getLogicalSlotIndex(),
9163 portInfo.isActive()
9164 );
9165 }
9166 @Override
9167 public UiccSlotInfo[] getUiccSlotsInfo(String callingPackage) {
sandeepjsa208e3b2021-11-17 04:05:58 +00009168 // Verify that the callingPackage belongs to the calling UID
sandeepjsb6c87872021-09-27 15:34:44 +00009169 mApp.getSystemService(AppOpsManager.class)
9170 .checkPackage(Binder.getCallingUid(), callingPackage);
9171
sandeepjsb6c87872021-09-27 15:34:44 +00009172 boolean isLogicalSlotAccessRestricted = false;
sandeepjsb6c87872021-09-27 15:34:44 +00009173
Aman Guptaf3c90b32022-03-17 04:54:16 +00009174 // This will make sure caller has the READ_PRIVILEGED_PHONE_STATE. Do not remove this as
9175 // we are reading iccId which is PII data.
9176 enforceReadPrivilegedPermission("getUiccSlotsInfo");
sandeepjsb6c87872021-09-27 15:34:44 +00009177
9178 // checking compatibility, if calling app's target SDK is T and beyond.
9179 if (CompatChanges.isChangeEnabled(GET_API_SIGNATURES_FROM_UICC_PORT_INFO,
9180 Binder.getCallingUid())) {
9181 isLogicalSlotAccessRestricted = true;
9182 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08009183 final long identity = Binder.clearCallingIdentity();
9184 try {
9185 UiccSlot[] slots = UiccController.getInstance().getUiccSlots();
Muralidhar Reddyd196bbf2022-01-17 17:56:30 +00009186 if (slots == null || slots.length == 0) {
9187 Rlog.i(LOG_TAG, "slots is null or empty.");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08009188 return null;
9189 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08009190 UiccSlotInfo[] infos = new UiccSlotInfo[slots.length];
9191 for (int i = 0; i < slots.length; i++) {
9192 UiccSlot slot = slots[i];
9193 if (slot == null) {
9194 continue;
9195 }
9196
Jordan Liu7be7e652019-05-06 18:55:02 +00009197 String cardId;
Malcolm Chenaa4a8532018-02-28 15:00:40 -08009198 UiccCard card = slot.getUiccCard();
9199 if (card != null) {
9200 cardId = card.getCardId();
Jordan Liu7be7e652019-05-06 18:55:02 +00009201 } else {
Jordan Liu01bd00d2019-09-12 16:19:43 -07009202 cardId = slot.getEid();
9203 if (TextUtils.isEmpty(cardId)) {
Aman Guptaf3c90b32022-03-17 04:54:16 +00009204 // If cardId is null, use iccId of default port as cardId.
9205 cardId = slot.getIccId(TelephonyManager.DEFAULT_PORT_INDEX);
Jordan Liu01bd00d2019-09-12 16:19:43 -07009206 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08009207 }
9208
Jordan Liu857451f2019-05-09 16:35:35 -07009209 if (cardId != null) {
9210 // if cardId is an ICCID, strip off trailing Fs before exposing to user
9211 // if cardId is an EID, it's all digits so this is fine
9212 cardId = IccUtils.stripTrailingFs(cardId);
9213 }
9214
Malcolm Chenaa4a8532018-02-28 15:00:40 -08009215 int cardState = 0;
9216 switch (slot.getCardState()) {
9217 case CARDSTATE_ABSENT:
9218 cardState = UiccSlotInfo.CARD_STATE_INFO_ABSENT;
9219 break;
9220 case CARDSTATE_PRESENT:
9221 cardState = UiccSlotInfo.CARD_STATE_INFO_PRESENT;
9222 break;
9223 case CARDSTATE_ERROR:
9224 cardState = UiccSlotInfo.CARD_STATE_INFO_ERROR;
9225 break;
9226 case CARDSTATE_RESTRICTED:
9227 cardState = UiccSlotInfo.CARD_STATE_INFO_RESTRICTED;
9228 break;
9229 default:
9230 break;
9231
9232 }
Muralidhar Reddyeb809e32021-11-19 03:07:54 +00009233 List<UiccPortInfo> portInfos = new ArrayList<>();
9234 int[] portIndexes = slot.getPortList();
9235 for (int portIdx : portIndexes) {
9236 String iccId = IccUtils.stripTrailingFs(getIccId(slot, portIdx,
Aman Guptaf3c90b32022-03-17 04:54:16 +00009237 callingPackage, /* hasReadPermission= */ true));
Muralidhar Reddyfbcff0c2022-01-19 13:07:57 +00009238 portInfos.add(new UiccPortInfo(iccId, portIdx,
9239 slot.getPhoneIdFromPortIndex(portIdx), slot.isPortActive(portIdx)));
Muralidhar Reddyeb809e32021-11-19 03:07:54 +00009240 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08009241 infos[i] = new UiccSlotInfo(
Malcolm Chenaa4a8532018-02-28 15:00:40 -08009242 slot.isEuicc(),
9243 cardId,
9244 cardState,
Jordan Liua2619582019-02-14 12:56:40 -08009245 slot.isExtendedApduSupported(),
Muralidhar Reddyeb809e32021-11-19 03:07:54 +00009246 slot.isRemovable(), portInfos);
sandeepjsb6c87872021-09-27 15:34:44 +00009247 //setting the value after compatibility check
9248 infos[i].setLogicalSlotAccessRestricted(isLogicalSlotAccessRestricted);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08009249 }
9250 return infos;
9251 } finally {
9252 Binder.restoreCallingIdentity(identity);
Holly Jiuyu Sun1d957c52018-04-04 13:52:42 -07009253 }
Holly Jiuyu Sun01c47ad2018-01-24 17:56:33 +00009254 }
9255
Muralidhar Reddyeb809e32021-11-19 03:07:54 +00009256 /* Returns null if doesn't have read permission or carrier privilege access. */
9257 private String getIccId(UiccSlot slot, int portIndex, String callingPackage,
9258 boolean hasReadPermission) {
9259 String iccId = slot.getIccId(portIndex);
9260 if (hasReadPermission) { // if has read permission
9261 return iccId;
9262 } else {
9263 if (slot.getUiccCard() != null && slot.getUiccCard().getUiccPort(portIndex) != null) {
9264 UiccPort port = slot.getUiccCard().getUiccPort(portIndex);
9265 // if no read permission, checking carrier privilege access
9266 if (haveCarrierPrivilegeAccess(port, callingPackage)) {
9267 return iccId;
9268 }
9269 }
9270 }
9271 // No read permission or carrier privilege access.
9272 return UiccPortInfo.ICCID_REDACTED;
9273 }
9274
Holly Jiuyu Sun01c47ad2018-01-24 17:56:33 +00009275 @Override
sandeepjsb6c87872021-09-27 15:34:44 +00009276 @Deprecated
Holly Jiuyu Sun01c47ad2018-01-24 17:56:33 +00009277 public boolean switchSlots(int[] physicalSlots) {
9278 enforceModifyPermission();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08009279
9280 final long identity = Binder.clearCallingIdentity();
9281 try {
Muralidhar Reddyeb809e32021-11-19 03:07:54 +00009282 List<UiccSlotMapping> slotMappings = new ArrayList<>();
9283 for (int i = 0; i < physicalSlots.length; i++) {
9284 // Deprecated API, hence MEP is not supported. Adding default portIndex 0.
9285 slotMappings.add(new UiccSlotMapping(TelephonyManager.DEFAULT_PORT_INDEX,
9286 physicalSlots[i], i));
9287 }
9288 return (Boolean) sendRequest(CMD_SWITCH_SLOTS, slotMappings);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08009289 } finally {
9290 Binder.restoreCallingIdentity(identity);
9291 }
Holly Jiuyu Sun01c47ad2018-01-24 17:56:33 +00009292 }
Jack Yu4c988042018-02-27 15:30:01 -08009293
9294 @Override
sandeepjsb6c87872021-09-27 15:34:44 +00009295 @RequiresPermission(android.Manifest.permission.MODIFY_PHONE_STATE)
9296 public boolean setSimSlotMapping(@NonNull List<UiccSlotMapping> slotMapping) {
9297 enforceModifyPermission();
9298
9299 final long identity = Binder.clearCallingIdentity();
9300 try {
Muralidhar Reddyeb809e32021-11-19 03:07:54 +00009301 return (Boolean) sendRequest(CMD_SWITCH_SLOTS, slotMapping);
sandeepjsb6c87872021-09-27 15:34:44 +00009302 } finally {
9303 Binder.restoreCallingIdentity(identity);
9304 }
9305 }
9306
9307 @Override
Jordan Liu7de49fa2018-12-06 14:48:49 -08009308 public int getCardIdForDefaultEuicc(int subId, String callingPackage) {
Jordan Liu7de49fa2018-12-06 14:48:49 -08009309 final long identity = Binder.clearCallingIdentity();
9310 try {
9311 return UiccController.getInstance().getCardIdForDefaultEuicc();
9312 } finally {
9313 Binder.restoreCallingIdentity(identity);
9314 }
9315 }
9316
Pengquan Meng85728fb2018-03-12 16:31:21 -07009317 /**
goneil47ffb6e2018-04-06 15:40:58 -07009318 * A test API to reload the UICC profile.
9319 *
9320 * <p>Requires that the calling app has permission
9321 * {@link android.Manifest.permission#MODIFY_PHONE_STATE MODIFY_PHONE_STATE}.
9322 * @hide
9323 */
9324 @Override
9325 public void refreshUiccProfile(int subId) {
9326 enforceModifyPermission();
9327
9328 final long identity = Binder.clearCallingIdentity();
9329 try {
9330 Phone phone = getPhone(subId);
9331 if (phone == null) {
9332 return;
9333 }
Muralidhar Reddy472c2ae2021-09-29 19:38:40 +00009334 UiccPort uiccPort = phone.getUiccPort();
9335 if (uiccPort == null) {
goneil47ffb6e2018-04-06 15:40:58 -07009336 return;
9337 }
Muralidhar Reddy472c2ae2021-09-29 19:38:40 +00009338 UiccProfile uiccProfile = uiccPort.getUiccProfile();
goneil47ffb6e2018-04-06 15:40:58 -07009339 if (uiccProfile == null) {
9340 return;
9341 }
9342 uiccProfile.refresh();
9343 } finally {
9344 Binder.restoreCallingIdentity(identity);
9345 }
9346 }
9347
9348 /**
Pengquan Meng85728fb2018-03-12 16:31:21 -07009349 * Returns false if the mobile data is disabled by default, otherwise return true.
9350 */
9351 private boolean getDefaultDataEnabled() {
Inseob Kim14bb3d02018-12-13 17:11:34 +09009352 return TelephonyProperties.mobile_data().orElse(true);
Pengquan Meng85728fb2018-03-12 16:31:21 -07009353 }
9354
9355 /**
9356 * Returns true if the data roaming is enabled by default, i.e the system property
9357 * of {@link #DEFAULT_DATA_ROAMING_PROPERTY_NAME} is true or the config of
9358 * {@link CarrierConfigManager#KEY_CARRIER_DEFAULT_DATA_ROAMING_ENABLED_BOOL} is true.
9359 */
9360 private boolean getDefaultDataRoamingEnabled(int subId) {
9361 final CarrierConfigManager configMgr = (CarrierConfigManager)
Nazanin Bakhshif782e562018-12-11 15:15:39 -08009362 mApp.getSystemService(Context.CARRIER_CONFIG_SERVICE);
Shuo Qian1d84a0e2020-07-15 12:36:44 -07009363 boolean isDataRoamingEnabled = TelephonyProperties.data_roaming().orElse(false);
Pengquan Meng85728fb2018-03-12 16:31:21 -07009364 isDataRoamingEnabled |= configMgr.getConfigForSubId(subId).getBoolean(
9365 CarrierConfigManager.KEY_CARRIER_DEFAULT_DATA_ROAMING_ENABLED_BOOL);
9366 return isDataRoamingEnabled;
9367 }
9368
9369 /**
9370 * Returns the default network type for the given {@code subId}, if the default network type is
9371 * not set, return {@link Phone#PREFERRED_NT_MODE}.
9372 */
9373 private int getDefaultNetworkType(int subId) {
Inseob Kim14bb3d02018-12-13 17:11:34 +09009374 List<Integer> list = TelephonyProperties.default_network();
Jack Yu285100e2022-12-02 22:48:35 -08009375 int phoneId = SubscriptionManager.getPhoneId(subId);
Inseob Kim14bb3d02018-12-13 17:11:34 +09009376 if (phoneId >= 0 && phoneId < list.size() && list.get(phoneId) != null) {
9377 return list.get(phoneId);
9378 }
9379 return Phone.PREFERRED_NT_MODE;
Pengquan Meng85728fb2018-03-12 16:31:21 -07009380 }
fionaxua13278b2018-03-21 00:08:13 -07009381
9382 @Override
9383 public void setCarrierTestOverride(int subId, String mccmnc, String imsi, String iccid, String
chen xueaba88a2019-03-15 13:15:10 -07009384 gid1, String gid2, String plmn, String spn, String carrierPrivilegeRules, String apn) {
fionaxua13278b2018-03-21 00:08:13 -07009385 enforceModifyPermission();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08009386
9387 final long identity = Binder.clearCallingIdentity();
9388 try {
9389 final Phone phone = getPhone(subId);
9390 if (phone == null) {
9391 loge("setCarrierTestOverride fails with invalid subId: " + subId);
9392 return;
9393 }
Rambo Wang9c9ffdd2022-01-13 21:51:44 -08009394 CarrierPrivilegesTracker cpt = phone.getCarrierPrivilegesTracker();
9395 if (cpt != null) {
9396 cpt.setTestOverrideCarrierPrivilegeRules(carrierPrivilegeRules);
9397 }
9398 // TODO(b/211796398): remove the legacy logic below once CPT migration is done.
chen xueaba88a2019-03-15 13:15:10 -07009399 phone.setCarrierTestOverride(mccmnc, imsi, iccid, gid1, gid2, plmn, spn,
9400 carrierPrivilegeRules, apn);
Jeff Davidson8ab02b22020-03-28 12:24:40 -07009401 if (carrierPrivilegeRules == null) {
9402 mCarrierPrivilegeTestOverrideSubIds.remove(subId);
9403 } else {
9404 mCarrierPrivilegeTestOverrideSubIds.add(subId);
9405 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08009406 } finally {
9407 Binder.restoreCallingIdentity(identity);
fionaxua13278b2018-03-21 00:08:13 -07009408 }
fionaxua13278b2018-03-21 00:08:13 -07009409 }
9410
9411 @Override
9412 public int getCarrierIdListVersion(int subId) {
Brad Ebinger35c841c2018-10-01 10:40:55 -07009413 enforceReadPrivilegedPermission("getCarrierIdListVersion");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08009414
9415 final long identity = Binder.clearCallingIdentity();
9416 try {
9417 final Phone phone = getPhone(subId);
9418 if (phone == null) {
9419 loge("getCarrierIdListVersion fails with invalid subId: " + subId);
9420 return TelephonyManager.UNKNOWN_CARRIER_ID_LIST_VERSION;
9421 }
9422 return phone.getCarrierIdListVersion();
9423 } finally {
9424 Binder.restoreCallingIdentity(identity);
fionaxua13278b2018-03-21 00:08:13 -07009425 }
fionaxua13278b2018-03-21 00:08:13 -07009426 }
Malcolm Chen2c63d402018-08-14 16:00:53 -07009427
9428 @Override
Philip P. Moltmann700a9592019-10-03 11:53:50 -07009429 public int getNumberOfModemsWithSimultaneousDataConnections(int subId, String callingPackage,
9430 String callingFeatureId) {
Malcolm Chen2c63d402018-08-14 16:00:53 -07009431 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
Philip P. Moltmann700a9592019-10-03 11:53:50 -07009432 mApp, subId, callingPackage, callingFeatureId,
9433 "getNumberOfModemsWithSimultaneousDataConnections")) {
Malcolm Chen2c63d402018-08-14 16:00:53 -07009434 return -1;
9435 }
9436
9437 final long identity = Binder.clearCallingIdentity();
9438 try {
9439 return mPhoneConfigurationManager.getNumberOfModemsWithSimultaneousDataConnections();
9440 } finally {
9441 Binder.restoreCallingIdentity(identity);
9442 }
9443 }
Pengquan Menga1bb6272018-09-06 09:59:22 -07009444
9445 @Override
9446 public int getCdmaRoamingMode(int subId) {
zoey chen7e6d4e52019-12-17 18:18:59 +08009447 TelephonyPermissions
Nathan Harold62c68512021-04-06 11:26:02 -07009448 .enforceCallingOrSelfReadPrivilegedPhoneStatePermissionOrCarrierPrivilege(
Pengquan Menga1bb6272018-09-06 09:59:22 -07009449 mApp, subId, "getCdmaRoamingMode");
9450
9451 final long identity = Binder.clearCallingIdentity();
9452 try {
9453 return (int) sendRequest(CMD_GET_CDMA_ROAMING_MODE, null /* argument */, subId);
9454 } finally {
9455 Binder.restoreCallingIdentity(identity);
9456 }
9457 }
9458
9459 @Override
9460 public boolean setCdmaRoamingMode(int subId, int mode) {
9461 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
9462 mApp, subId, "setCdmaRoamingMode");
9463
9464 final long identity = Binder.clearCallingIdentity();
9465 try {
9466 return (boolean) sendRequest(CMD_SET_CDMA_ROAMING_MODE, mode, subId);
9467 } finally {
9468 Binder.restoreCallingIdentity(identity);
9469 }
9470 }
9471
9472 @Override
Sarah Chinbaab1432020-10-28 13:46:24 -07009473 public int getCdmaSubscriptionMode(int subId) {
9474 TelephonyPermissions
Nathan Harold62c68512021-04-06 11:26:02 -07009475 .enforceCallingOrSelfReadPrivilegedPhoneStatePermissionOrCarrierPrivilege(
Sarah Chinbaab1432020-10-28 13:46:24 -07009476 mApp, subId, "getCdmaSubscriptionMode");
9477
9478 final long identity = Binder.clearCallingIdentity();
9479 try {
9480 return (int) sendRequest(CMD_GET_CDMA_SUBSCRIPTION_MODE, null /* argument */, subId);
9481 } finally {
9482 Binder.restoreCallingIdentity(identity);
9483 }
9484 }
9485
9486 @Override
Pengquan Menga1bb6272018-09-06 09:59:22 -07009487 public boolean setCdmaSubscriptionMode(int subId, int mode) {
9488 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
9489 mApp, subId, "setCdmaSubscriptionMode");
9490
9491 final long identity = Binder.clearCallingIdentity();
9492 try {
9493 return (boolean) sendRequest(CMD_SET_CDMA_SUBSCRIPTION_MODE, mode, subId);
9494 } finally {
9495 Binder.restoreCallingIdentity(identity);
9496 }
9497 }
Makoto Onukida3bf792018-09-18 16:06:29 -07009498
sqianc5eccab2018-10-19 18:46:41 -07009499 @Override
sqian8c685422019-02-22 15:55:18 -08009500 public Map<Integer, List<EmergencyNumber>> getEmergencyNumberList(
Philip P. Moltmann700a9592019-10-03 11:53:50 -07009501 String callingPackage, String callingFeatureId) {
sqian11b7a0e2018-12-05 18:48:28 -08009502 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
Philip P. Moltmann700a9592019-10-03 11:53:50 -07009503 mApp, getDefaultSubscription(), callingPackage, callingFeatureId,
9504 "getEmergencyNumberList")) {
sqian11b7a0e2018-12-05 18:48:28 -08009505 throw new SecurityException("Requires READ_PHONE_STATE permission.");
9506 }
9507 final long identity = Binder.clearCallingIdentity();
9508 try {
sqian854d44b2018-12-12 16:48:18 -08009509 Map<Integer, List<EmergencyNumber>> emergencyNumberListInternal = new HashMap<>();
9510 for (Phone phone: PhoneFactory.getPhones()) {
9511 if (phone.getEmergencyNumberTracker() != null
9512 && phone.getEmergencyNumberTracker().getEmergencyNumberList() != null) {
9513 emergencyNumberListInternal.put(
9514 phone.getSubId(),
9515 phone.getEmergencyNumberTracker().getEmergencyNumberList());
9516 }
sqian11b7a0e2018-12-05 18:48:28 -08009517 }
sqian854d44b2018-12-12 16:48:18 -08009518 return emergencyNumberListInternal;
sqian11b7a0e2018-12-05 18:48:28 -08009519 } finally {
9520 Binder.restoreCallingIdentity(identity);
9521 }
sqianc5eccab2018-10-19 18:46:41 -07009522 }
9523
9524 @Override
sqian8c685422019-02-22 15:55:18 -08009525 public boolean isEmergencyNumber(String number, boolean exactMatch) {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08009526 final Phone defaultPhone = getDefaultPhone();
sqian11b7a0e2018-12-05 18:48:28 -08009527 if (!exactMatch) {
9528 TelephonyPermissions
Nathan Harold62c68512021-04-06 11:26:02 -07009529 .enforceCallingOrSelfReadPrivilegedPhoneStatePermissionOrCarrierPrivilege(
sqian8c685422019-02-22 15:55:18 -08009530 mApp, defaultPhone.getSubId(), "isEmergencyNumber(Potential)");
sqian11b7a0e2018-12-05 18:48:28 -08009531 }
9532 final long identity = Binder.clearCallingIdentity();
9533 try {
sqian854d44b2018-12-12 16:48:18 -08009534 for (Phone phone: PhoneFactory.getPhones()) {
Chinmay Dhodapkard521bb12022-08-16 15:49:54 -07009535 //Note: we ignore passed in param exactMatch. We can remove it once
9536 // TelephonyManager#isPotentialEmergencyNumber is removed completely
sqian854d44b2018-12-12 16:48:18 -08009537 if (phone.getEmergencyNumberTracker() != null
Taesu Leee050c002020-10-13 17:19:35 +09009538 && phone.getEmergencyNumberTracker()
Chinmay Dhodapkard521bb12022-08-16 15:49:54 -07009539 .isEmergencyNumber(number)) {
Taesu Leee050c002020-10-13 17:19:35 +09009540 return true;
sqian11b7a0e2018-12-05 18:48:28 -08009541 }
sqian11b7a0e2018-12-05 18:48:28 -08009542 }
9543 return false;
9544 } finally {
9545 Binder.restoreCallingIdentity(identity);
9546 }
9547 }
9548
sqianf4ca7ed2019-01-15 18:32:07 -08009549 /**
Shuo Qianccbaf742021-02-22 18:32:21 -08009550 * Start emergency callback mode for GsmCdmaPhone for testing.
9551 */
9552 @Override
9553 public void startEmergencyCallbackMode() {
9554 TelephonyPermissions.enforceShellOnly(Binder.getCallingUid(),
9555 "startEmergencyCallbackMode");
9556 enforceModifyPermission();
9557 final long identity = Binder.clearCallingIdentity();
9558 try {
9559 for (Phone phone : PhoneFactory.getPhones()) {
9560 Rlog.d(LOG_TAG, "startEmergencyCallbackMode phone type: " + phone.getPhoneType());
9561 if (phone != null && ((phone.getPhoneType() == PHONE_TYPE_GSM)
9562 || (phone.getPhoneType() == PHONE_TYPE_CDMA))) {
9563 GsmCdmaPhone gsmCdmaPhone = (GsmCdmaPhone) phone;
9564 gsmCdmaPhone.obtainMessage(
9565 GsmCdmaPhone.EVENT_EMERGENCY_CALLBACK_MODE_ENTER).sendToTarget();
9566 Rlog.d(LOG_TAG, "startEmergencyCallbackMode: triggered");
9567 }
9568 }
9569 } finally {
9570 Binder.restoreCallingIdentity(identity);
9571 }
9572 }
9573
9574 /**
sqianf4ca7ed2019-01-15 18:32:07 -08009575 * Update emergency number list for test mode.
9576 */
9577 @Override
9578 public void updateEmergencyNumberListTestMode(int action, EmergencyNumber num) {
9579 TelephonyPermissions.enforceShellOnly(Binder.getCallingUid(),
9580 "updateEmergencyNumberListTestMode");
9581
9582 final long identity = Binder.clearCallingIdentity();
9583 try {
9584 for (Phone phone: PhoneFactory.getPhones()) {
9585 EmergencyNumberTracker tracker = phone.getEmergencyNumberTracker();
9586 if (tracker != null) {
9587 tracker.executeEmergencyNumberTestModeCommand(action, num);
9588 }
9589 }
9590 } finally {
9591 Binder.restoreCallingIdentity(identity);
9592 }
9593 }
9594
9595 /**
9596 * Get the full emergency number list for test mode.
9597 */
9598 @Override
9599 public List<String> getEmergencyNumberListTestMode() {
9600 TelephonyPermissions.enforceShellOnly(Binder.getCallingUid(),
9601 "getEmergencyNumberListTestMode");
9602
9603 final long identity = Binder.clearCallingIdentity();
9604 try {
9605 Set<String> emergencyNumbers = new HashSet<>();
9606 for (Phone phone: PhoneFactory.getPhones()) {
9607 EmergencyNumberTracker tracker = phone.getEmergencyNumberTracker();
9608 if (tracker != null) {
9609 for (EmergencyNumber num : tracker.getEmergencyNumberList()) {
9610 emergencyNumbers.add(num.getNumber());
9611 }
9612 }
9613 }
9614 return new ArrayList<>(emergencyNumbers);
9615 } finally {
9616 Binder.restoreCallingIdentity(identity);
9617 }
9618 }
9619
chen xud6b45bd2018-10-30 22:27:10 -07009620 @Override
Shuo Qian3b6ee772019-11-13 17:43:31 -08009621 public int getEmergencyNumberDbVersion(int subId) {
9622 enforceReadPrivilegedPermission("getEmergencyNumberDbVersion");
9623
9624 final long identity = Binder.clearCallingIdentity();
9625 try {
9626 final Phone phone = getPhone(subId);
9627 if (phone == null) {
9628 loge("getEmergencyNumberDbVersion fails with invalid subId: " + subId);
9629 return TelephonyManager.INVALID_EMERGENCY_NUMBER_DB_VERSION;
9630 }
9631 return phone.getEmergencyNumberDbVersion();
9632 } finally {
9633 Binder.restoreCallingIdentity(identity);
9634 }
9635 }
9636
9637 @Override
9638 public void notifyOtaEmergencyNumberDbInstalled() {
9639 enforceModifyPermission();
9640
9641 final long identity = Binder.clearCallingIdentity();
9642 try {
9643 for (Phone phone: PhoneFactory.getPhones()) {
9644 EmergencyNumberTracker tracker = phone.getEmergencyNumberTracker();
9645 if (tracker != null) {
9646 tracker.updateOtaEmergencyNumberDatabase();
9647 }
9648 }
9649 } finally {
9650 Binder.restoreCallingIdentity(identity);
9651 }
9652 }
9653
9654 @Override
Shuo Qianc373f112020-03-05 17:55:34 -08009655 public void updateOtaEmergencyNumberDbFilePath(ParcelFileDescriptor otaParcelFileDescriptor) {
Shuo Qian3b6ee772019-11-13 17:43:31 -08009656 enforceActiveEmergencySessionPermission();
9657
9658 final long identity = Binder.clearCallingIdentity();
9659 try {
9660 for (Phone phone: PhoneFactory.getPhones()) {
9661 EmergencyNumberTracker tracker = phone.getEmergencyNumberTracker();
9662 if (tracker != null) {
Shuo Qianc373f112020-03-05 17:55:34 -08009663 tracker.updateOtaEmergencyNumberDbFilePath(otaParcelFileDescriptor);
9664 }
9665 }
9666 } finally {
9667 Binder.restoreCallingIdentity(identity);
9668 }
9669 }
9670
9671 @Override
9672 public void resetOtaEmergencyNumberDbFilePath() {
9673 enforceActiveEmergencySessionPermission();
9674
9675 final long identity = Binder.clearCallingIdentity();
9676 try {
9677 for (Phone phone: PhoneFactory.getPhones()) {
9678 EmergencyNumberTracker tracker = phone.getEmergencyNumberTracker();
9679 if (tracker != null) {
9680 tracker.resetOtaEmergencyNumberDbFilePath();
Shuo Qian3b6ee772019-11-13 17:43:31 -08009681 }
9682 }
9683 } finally {
9684 Binder.restoreCallingIdentity(identity);
9685 }
9686 }
9687
9688 @Override
chen xud6b45bd2018-10-30 22:27:10 -07009689 public List<String> getCertsFromCarrierPrivilegeAccessRules(int subId) {
9690 enforceReadPrivilegedPermission("getCertsFromCarrierPrivilegeAccessRules");
9691 Phone phone = getPhone(subId);
9692 if (phone == null) {
9693 return null;
9694 }
9695 final long identity = Binder.clearCallingIdentity();
9696 try {
9697 UiccProfile profile = UiccController.getInstance()
9698 .getUiccProfileForPhone(phone.getPhoneId());
9699 if (profile != null) {
9700 return profile.getCertsFromCarrierPrivilegeAccessRules();
9701 }
9702 } finally {
9703 Binder.restoreCallingIdentity(identity);
9704 }
9705 return null;
9706 }
Malcolm Chen8e4ed912019-01-15 20:22:16 -08009707
9708 /**
9709 * Enable or disable a modem stack.
9710 */
9711 @Override
9712 public boolean enableModemForSlot(int slotIndex, boolean enable) {
9713 enforceModifyPermission();
9714
9715 final long identity = Binder.clearCallingIdentity();
9716 try {
9717 Phone phone = PhoneFactory.getPhone(slotIndex);
9718 if (phone == null) {
9719 return false;
9720 } else {
9721 return (Boolean) sendRequest(CMD_REQUEST_ENABLE_MODEM, enable, phone, null);
9722 }
9723 } finally {
9724 Binder.restoreCallingIdentity(identity);
9725 }
9726 }
Michelecea4cf22018-12-21 15:00:11 -08009727
Malcolm Chen4bcd9822019-03-27 18:34:05 -07009728 /**
9729 * Whether a modem stack is enabled or not.
9730 */
9731 @Override
Philip P. Moltmann700a9592019-10-03 11:53:50 -07009732 public boolean isModemEnabledForSlot(int slotIndex, String callingPackage,
9733 String callingFeatureId) {
Malcolm Chen4bcd9822019-03-27 18:34:05 -07009734 Phone phone = PhoneFactory.getPhone(slotIndex);
9735 if (phone == null) return false;
9736
9737 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
Philip P. Moltmann700a9592019-10-03 11:53:50 -07009738 mApp, phone.getSubId(), callingPackage, callingFeatureId,
9739 "isModemEnabledForSlot")) {
Malcolm Chen4bcd9822019-03-27 18:34:05 -07009740 throw new SecurityException("Requires READ_PHONE_STATE permission.");
9741 }
9742
9743 final long identity = Binder.clearCallingIdentity();
9744 try {
Nazanin Bakhshif71371d2019-04-29 17:29:44 -07009745 try {
9746 return mPhoneConfigurationManager.getPhoneStatusFromCache(phone.getPhoneId());
9747 } catch (NoSuchElementException ex) {
9748 return (Boolean) sendRequest(CMD_GET_MODEM_STATUS, null, phone, null);
9749 }
Malcolm Chen4bcd9822019-03-27 18:34:05 -07009750 } finally {
9751 Binder.restoreCallingIdentity(identity);
9752 }
9753 }
9754
Michelecea4cf22018-12-21 15:00:11 -08009755 @Override
Michele0ea7d782019-03-19 14:58:42 -07009756 public void setMultiSimCarrierRestriction(boolean isMultiSimCarrierRestricted) {
Michelecea4cf22018-12-21 15:00:11 -08009757 enforceModifyPermission();
9758
9759 final long identity = Binder.clearCallingIdentity();
9760 try {
9761 mTelephonySharedPreferences.edit()
Michele0ea7d782019-03-19 14:58:42 -07009762 .putBoolean(PREF_MULTI_SIM_RESTRICTED, isMultiSimCarrierRestricted)
Michelecea4cf22018-12-21 15:00:11 -08009763 .commit();
9764 } finally {
9765 Binder.restoreCallingIdentity(identity);
9766 }
9767 }
9768
9769 @Override
Michele0ea7d782019-03-19 14:58:42 -07009770 @TelephonyManager.IsMultiSimSupportedResult
Philip P. Moltmann700a9592019-10-03 11:53:50 -07009771 public int isMultiSimSupported(String callingPackage, String callingFeatureId) {
Michele4245e952019-02-04 11:36:23 -08009772 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(mApp,
Philip P. Moltmann700a9592019-10-03 11:53:50 -07009773 getDefaultPhone().getSubId(), callingPackage, callingFeatureId,
9774 "isMultiSimSupported")) {
Michele0ea7d782019-03-19 14:58:42 -07009775 return TelephonyManager.MULTISIM_NOT_SUPPORTED_BY_HARDWARE;
Michele4245e952019-02-04 11:36:23 -08009776 }
Michelecea4cf22018-12-21 15:00:11 -08009777
9778 final long identity = Binder.clearCallingIdentity();
9779 try {
Michele0ea7d782019-03-19 14:58:42 -07009780 return isMultiSimSupportedInternal();
Michelecea4cf22018-12-21 15:00:11 -08009781 } finally {
9782 Binder.restoreCallingIdentity(identity);
9783 }
9784 }
Nazanin Bakhshi628473f2019-01-29 17:37:52 -08009785
Michele0ea7d782019-03-19 14:58:42 -07009786 @TelephonyManager.IsMultiSimSupportedResult
9787 private int isMultiSimSupportedInternal() {
Michele30b57b22019-03-01 12:01:14 -08009788 // If the device has less than 2 SIM cards, indicate that multisim is restricted.
9789 int numPhysicalSlots = UiccController.getInstance().getUiccSlots().length;
9790 if (numPhysicalSlots < 2) {
Michele0ea7d782019-03-19 14:58:42 -07009791 loge("isMultiSimSupportedInternal: requires at least 2 cards");
9792 return TelephonyManager.MULTISIM_NOT_SUPPORTED_BY_HARDWARE;
Michele30b57b22019-03-01 12:01:14 -08009793 }
9794 // Check if the hardware supports multisim functionality. If usage of multisim is not
9795 // supported by the modem, indicate that it is restricted.
9796 PhoneCapability staticCapability =
9797 mPhoneConfigurationManager.getStaticPhoneCapability();
9798 if (staticCapability == null) {
Michele0ea7d782019-03-19 14:58:42 -07009799 loge("isMultiSimSupportedInternal: no static configuration available");
9800 return TelephonyManager.MULTISIM_NOT_SUPPORTED_BY_HARDWARE;
Michele30b57b22019-03-01 12:01:14 -08009801 }
SongFerngWang8236caa2021-01-17 21:51:44 +08009802 if (staticCapability.getLogicalModemList().size() < 2) {
Michele0ea7d782019-03-19 14:58:42 -07009803 loge("isMultiSimSupportedInternal: maximum number of modem is < 2");
9804 return TelephonyManager.MULTISIM_NOT_SUPPORTED_BY_HARDWARE;
Michele30b57b22019-03-01 12:01:14 -08009805 }
9806 // Check if support of multiple SIMs is restricted by carrier
9807 if (mTelephonySharedPreferences.getBoolean(PREF_MULTI_SIM_RESTRICTED, false)) {
Michele0ea7d782019-03-19 14:58:42 -07009808 return TelephonyManager.MULTISIM_NOT_SUPPORTED_BY_CARRIER;
Michele30b57b22019-03-01 12:01:14 -08009809 }
9810
Michele0ea7d782019-03-19 14:58:42 -07009811 return TelephonyManager.MULTISIM_ALLOWED;
Michele30b57b22019-03-01 12:01:14 -08009812 }
9813
Nazanin Bakhshi628473f2019-01-29 17:37:52 -08009814 /**
9815 * Switch configs to enable multi-sim or switch back to single-sim
Nazanin Bakhshi17318782019-03-01 11:56:08 -08009816 * Note: Switch from multi-sim to single-sim is only possible with MODIFY_PHONE_STATE
9817 * permission, but the other way around is possible with either MODIFY_PHONE_STATE
9818 * or carrier privileges
Nazanin Bakhshi628473f2019-01-29 17:37:52 -08009819 * @param numOfSims number of active sims we want to switch to
9820 */
9821 @Override
9822 public void switchMultiSimConfig(int numOfSims) {
Nazanin Bakhshi17318782019-03-01 11:56:08 -08009823 if (numOfSims == 1) {
9824 enforceModifyPermission();
9825 } else {
9826 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
9827 mApp, SubscriptionManager.DEFAULT_SUBSCRIPTION_ID, "switchMultiSimConfig");
9828 }
Nazanin Bakhshi628473f2019-01-29 17:37:52 -08009829 final long identity = Binder.clearCallingIdentity();
Michele30b57b22019-03-01 12:01:14 -08009830
Nazanin Bakhshi628473f2019-01-29 17:37:52 -08009831 try {
Michele30b57b22019-03-01 12:01:14 -08009832 //only proceed if multi-sim is not restricted
Michele0ea7d782019-03-19 14:58:42 -07009833 if (isMultiSimSupportedInternal() != TelephonyManager.MULTISIM_ALLOWED) {
Michele30b57b22019-03-01 12:01:14 -08009834 loge("switchMultiSimConfig not possible. It is restricted or not supported.");
9835 return;
9836 }
Nazanin Bakhshi628473f2019-01-29 17:37:52 -08009837 mPhoneConfigurationManager.switchMultiSimConfig(numOfSims);
9838 } finally {
9839 Binder.restoreCallingIdentity(identity);
9840 }
9841 }
9842
Hyungjun Parkbb07fde2019-01-10 15:28:51 +09009843 @Override
9844 public boolean isApplicationOnUicc(int subId, int appType) {
9845 enforceReadPrivilegedPermission("isApplicationOnUicc");
9846 Phone phone = getPhone(subId);
9847 if (phone == null) {
9848 return false;
9849 }
9850 final long identity = Binder.clearCallingIdentity();
9851 try {
Muralidhar Reddy472c2ae2021-09-29 19:38:40 +00009852 UiccPort uiccPort = phone.getUiccPort();
9853 if (uiccPort == null) {
Hyungjun Parkbb07fde2019-01-10 15:28:51 +09009854 return false;
9855 }
Muralidhar Reddy472c2ae2021-09-29 19:38:40 +00009856 UiccProfile uiccProfile = uiccPort.getUiccProfile();
Hyungjun Parkbb07fde2019-01-10 15:28:51 +09009857 if (uiccProfile == null) {
9858 return false;
9859 }
9860 if (TelephonyManager.APPTYPE_SIM <= appType
9861 && appType <= TelephonyManager.APPTYPE_ISIM) {
9862 return uiccProfile.isApplicationOnIcc(AppType.values()[appType]);
9863 }
9864 return false;
9865 } finally {
9866 Binder.restoreCallingIdentity(identity);
9867 }
9868 }
9869
Nazanin Bakhshi628473f2019-01-29 17:37:52 -08009870 /**
chen xub4baa772019-04-03 10:23:41 -07009871 * Get whether making changes to modem configurations will trigger reboot.
9872 * Return value defaults to true.
Nazanin Bakhshi5fe5ef22019-01-30 10:52:09 -08009873 */
9874 @Override
Philip P. Moltmann700a9592019-10-03 11:53:50 -07009875 public boolean doesSwitchMultiSimConfigTriggerReboot(int subId, String callingPackage,
9876 String callingFeatureId) {
chen xub4baa772019-04-03 10:23:41 -07009877 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
Philip P. Moltmann700a9592019-10-03 11:53:50 -07009878 mApp, subId, callingPackage, callingFeatureId,
9879 "doesSwitchMultiSimConfigTriggerReboot")) {
chen xub4baa772019-04-03 10:23:41 -07009880 return false;
9881 }
Nazanin Bakhshi5fe5ef22019-01-30 10:52:09 -08009882 final long identity = Binder.clearCallingIdentity();
9883 try {
9884 return mPhoneConfigurationManager.isRebootRequiredForModemConfigChange();
9885 } finally {
9886 Binder.restoreCallingIdentity(identity);
9887 }
9888 }
9889
Nathan Harold29f5f052019-02-15 13:41:57 -08009890 private void updateModemStateMetrics() {
9891 TelephonyMetrics metrics = TelephonyMetrics.getInstance();
9892 // TODO: check the state for each modem if the api is ready.
9893 metrics.updateEnabledModemBitmap((1 << TelephonyManager.from(mApp).getPhoneCount()) - 1);
9894 }
9895
Pengquan Meng3889a572019-01-23 11:16:29 -08009896 @Override
sandeepjsa208e3b2021-11-17 04:05:58 +00009897 public List<UiccSlotMapping> getSlotsMapping(String callingPackage) {
Pengquan Meng3889a572019-01-23 11:16:29 -08009898 enforceReadPrivilegedPermission("getSlotsMapping");
sandeepjsa208e3b2021-11-17 04:05:58 +00009899 // Verify that the callingPackage belongs to the calling UID
9900 mApp.getSystemService(AppOpsManager.class)
9901 .checkPackage(Binder.getCallingUid(), callingPackage);
Pengquan Meng3889a572019-01-23 11:16:29 -08009902 final long identity = Binder.clearCallingIdentity();
sandeepjsa208e3b2021-11-17 04:05:58 +00009903 List<UiccSlotMapping> slotMap = new ArrayList<>();
Pengquan Meng3889a572019-01-23 11:16:29 -08009904 try {
sandeepjsa208e3b2021-11-17 04:05:58 +00009905 UiccSlotInfo[] slotInfos = getUiccSlotsInfo(mApp.getOpPackageName());
9906 if (slotInfos != null) {
9907 for (int i = 0; i < slotInfos.length; i++) {
9908 for (UiccPortInfo portInfo : slotInfos[i].getPorts()) {
9909 if (SubscriptionManager.isValidPhoneId(portInfo.getLogicalSlotIndex())) {
9910 slotMap.add(new UiccSlotMapping(portInfo.getPortIndex(), i,
9911 portInfo.getLogicalSlotIndex()));
9912 }
9913 }
Pengquan Meng3889a572019-01-23 11:16:29 -08009914 }
9915 }
sandeepjsa208e3b2021-11-17 04:05:58 +00009916 return slotMap;
Pengquan Meng3889a572019-01-23 11:16:29 -08009917 } finally {
9918 Binder.restoreCallingIdentity(identity);
9919 }
9920 }
Nathan Harold48d6fd52019-02-06 19:01:40 -08009921
9922 /**
9923 * Get the IRadio HAL Version
jimsunf9ec1622022-09-13 21:18:43 +08009924 * @deprecated use getHalVersion instead
Nathan Harold48d6fd52019-02-06 19:01:40 -08009925 */
jimsunf9ec1622022-09-13 21:18:43 +08009926 @Deprecated
Nathan Harold48d6fd52019-02-06 19:01:40 -08009927 @Override
9928 public int getRadioHalVersion() {
jimsunf9ec1622022-09-13 21:18:43 +08009929 return getHalVersion(HAL_SERVICE_RADIO);
9930 }
9931
9932 /**
9933 * Get the HAL Version of a specific service
9934 */
9935 @Override
9936 public int getHalVersion(int service) {
Nathan Harold48d6fd52019-02-06 19:01:40 -08009937 Phone phone = getDefaultPhone();
9938 if (phone == null) return -1;
jimsunf9ec1622022-09-13 21:18:43 +08009939 HalVersion hv = phone.getHalVersion(service);
Nathan Harold48d6fd52019-02-06 19:01:40 -08009940 if (hv.equals(HalVersion.UNKNOWN)) return -1;
9941 return hv.major * 100 + hv.minor;
9942 }
Malcolm Chendc8c10e2019-04-10 18:25:07 -07009943
9944 /**
Shuo Qianda2d6ec2020-01-14 15:18:28 -08009945 * Get the current calling package name.
9946 * @return the current calling package name
9947 */
9948 @Override
9949 public String getCurrentPackageName() {
9950 return mApp.getPackageManager().getPackagesForUid(Binder.getCallingUid())[0];
9951 }
9952
9953 /**
Malcolm Chene5ad5792019-04-18 13:51:02 -07009954 * Return whether data is enabled for certain APN type. This will tell if framework will accept
9955 * corresponding network requests on a subId.
9956 *
9957 * Data is enabled if:
Malcolm Chendc8c10e2019-04-10 18:25:07 -07009958 * 1) user data is turned on, or
Malcolm Chene5ad5792019-04-18 13:51:02 -07009959 * 2) APN is un-metered for this subscription, or
9960 * 3) APN type is whitelisted. E.g. MMS is whitelisted if
Hall Liu746e03c2020-09-25 11:13:49 -07009961 * {@link TelephonyManager#MOBILE_DATA_POLICY_MMS_ALWAYS_ALLOWED} is enabled.
Malcolm Chene5ad5792019-04-18 13:51:02 -07009962 *
9963 * @return whether data is allowed for a apn type.
9964 *
9965 * @hide
Malcolm Chendc8c10e2019-04-10 18:25:07 -07009966 */
9967 @Override
Malcolm Chene5ad5792019-04-18 13:51:02 -07009968 public boolean isDataEnabledForApn(int apnType, int subId, String callingPackage) {
Amit Mahajan5d4e1922019-10-07 16:20:43 -07009969 enforceReadPrivilegedPermission("Needs READ_PRIVILEGED_PHONE_STATE for "
9970 + "isDataEnabledForApn");
Malcolm Chendc8c10e2019-04-10 18:25:07 -07009971
9972 // Now that all security checks passes, perform the operation as ourselves.
9973 final long identity = Binder.clearCallingIdentity();
9974 try {
9975 Phone phone = getPhone(subId);
9976 if (phone == null) return false;
9977
Jack Yu27422a52022-03-21 10:38:05 -07009978 boolean isMetered;
Jack Yu99e87332021-12-17 23:14:15 -08009979 boolean isDataEnabled;
Jack Yu7968c6d2022-07-31 00:43:21 -07009980 isMetered = phone.getDataNetworkController().getDataConfigManager()
9981 .isMeteredCapability(DataUtils.apnTypeToNetworkCapability(apnType),
9982 phone.getServiceState().getDataRoaming());
9983 isDataEnabled = phone.getDataSettingsManager().isDataEnabled(apnType);
Jack Yu99e87332021-12-17 23:14:15 -08009984 return !isMetered || isDataEnabled;
Malcolm Chene5ad5792019-04-18 13:51:02 -07009985 } finally {
9986 Binder.restoreCallingIdentity(identity);
9987 }
9988 }
9989
9990 @Override
Jack Yu41407ee2019-05-13 16:54:09 -07009991 public boolean isApnMetered(@ApnType int apnType, int subId) {
Malcolm Chene5ad5792019-04-18 13:51:02 -07009992 enforceReadPrivilegedPermission("isApnMetered");
9993
9994 // Now that all security checks passes, perform the operation as ourselves.
9995 final long identity = Binder.clearCallingIdentity();
9996 try {
9997 Phone phone = getPhone(subId);
9998 if (phone == null) return true; // By default return true.
Jack Yu7968c6d2022-07-31 00:43:21 -07009999 return phone.getDataNetworkController().getDataConfigManager().isMeteredCapability(
10000 DataUtils.apnTypeToNetworkCapability(apnType),
10001 phone.getServiceState().getDataRoaming());
Malcolm Chendc8c10e2019-04-10 18:25:07 -070010002 } finally {
10003 Binder.restoreCallingIdentity(identity);
10004 }
10005 }
Brad Ebingera63db5f2019-04-23 16:31:13 -070010006
10007 @Override
Hall Liu73f5d362020-01-20 13:42:00 -080010008 public void setSystemSelectionChannels(List<RadioAccessSpecifier> specifiers,
10009 int subscriptionId, IBooleanConsumer resultCallback) {
10010 enforceModifyPermission();
10011 long token = Binder.clearCallingIdentity();
10012 try {
10013 Phone phone = getPhone(subscriptionId);
10014 if (phone == null) {
10015 try {
10016 if (resultCallback != null) {
10017 resultCallback.accept(false);
10018 }
10019 } catch (RemoteException e) {
10020 // ignore
10021 }
10022 return;
10023 }
10024 Pair<List<RadioAccessSpecifier>, Consumer<Boolean>> argument =
10025 Pair.create(specifiers, (x) -> {
10026 try {
10027 if (resultCallback != null) {
10028 resultCallback.accept(x);
10029 }
10030 } catch (RemoteException e) {
10031 // ignore
10032 }
10033 });
10034 sendRequestAsync(CMD_SET_SYSTEM_SELECTION_CHANNELS, argument, phone, null);
10035 } finally {
10036 Binder.restoreCallingIdentity(token);
10037 }
10038 }
10039
10040 @Override
Sarah Chin679c08a2020-11-18 13:39:35 -080010041 public List<RadioAccessSpecifier> getSystemSelectionChannels(int subId) {
10042 TelephonyPermissions
Nathan Harold62c68512021-04-06 11:26:02 -070010043 .enforceCallingOrSelfReadPrivilegedPhoneStatePermissionOrCarrierPrivilege(
Sarah Chin679c08a2020-11-18 13:39:35 -080010044 mApp, subId, "getSystemSelectionChannels");
10045 WorkSource workSource = getWorkSource(Binder.getCallingUid());
10046 final long identity = Binder.clearCallingIdentity();
10047 try {
Sarah Chin428d1d62021-03-13 03:17:40 -080010048 Object result = sendRequest(CMD_GET_SYSTEM_SELECTION_CHANNELS, null, subId, workSource);
10049 if (result instanceof IllegalStateException) {
10050 throw (IllegalStateException) result;
10051 }
10052 List<RadioAccessSpecifier> specifiers = (List<RadioAccessSpecifier>) result;
Sarah Chin679c08a2020-11-18 13:39:35 -080010053 if (DBG) log("getSystemSelectionChannels: " + specifiers);
10054 return specifiers;
10055 } finally {
10056 Binder.restoreCallingIdentity(identity);
10057 }
10058 }
10059
10060 @Override
Jack Yu8b766fc2022-03-21 09:42:33 -070010061 public boolean isMvnoMatched(int slotIndex, int mvnoType, @NonNull String mvnoMatchData) {
changbettyca3d40d2020-03-03 16:27:31 +080010062 enforceReadPrivilegedPermission("isMvnoMatched");
Jack Yu8b766fc2022-03-21 09:42:33 -070010063 return UiccController.getInstance().mvnoMatches(slotIndex, mvnoType, mvnoMatchData);
changbetty7157e9e2019-12-06 18:16:37 +080010064 }
10065
10066 @Override
Philip P. Moltmannd02b7372020-03-18 17:06:12 -070010067 public void enqueueSmsPickResult(String callingPackage, String callingAttributionTag,
10068 IIntegerConsumer pendingSubIdResult) {
Shuo Qianda2d6ec2020-01-14 15:18:28 -080010069 if (callingPackage == null) {
10070 callingPackage = getCurrentPackageName();
10071 }
Brad Ebingera63db5f2019-04-23 16:31:13 -070010072 SmsPermissions permissions = new SmsPermissions(getDefaultPhone(), mApp,
10073 (AppOpsManager) mApp.getSystemService(Context.APP_OPS_SERVICE));
Philip P. Moltmannd02b7372020-03-18 17:06:12 -070010074 if (!permissions.checkCallingCanSendSms(callingPackage, callingAttributionTag,
10075 "Sending message")) {
Brad Ebingera63db5f2019-04-23 16:31:13 -070010076 throw new SecurityException("Requires SEND_SMS permission to perform this operation");
10077 }
10078 PickSmsSubscriptionActivity.addPendingResult(pendingSubIdResult);
10079 Intent intent = new Intent();
10080 intent.setClass(mApp, PickSmsSubscriptionActivity.class);
10081 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
10082 // Bring up choose default SMS subscription dialog right now
10083 intent.putExtra(PickSmsSubscriptionActivity.DIALOG_TYPE_KEY,
10084 PickSmsSubscriptionActivity.SMS_PICK_FOR_MESSAGE);
10085 mApp.startActivity(intent);
10086 }
chen xud5ca2d52019-05-28 15:20:57 -070010087
10088 @Override
10089 public String getMmsUAProfUrl(int subId) {
10090 //TODO investigate if this API should require proper permission check in R b/133791609
10091 final long identity = Binder.clearCallingIdentity();
10092 try {
Guoqiang.Qiu171383d2020-07-13 09:38:32 +080010093 String carrierUAProfUrl = mApp.getCarrierConfigForSubId(subId).getString(
10094 CarrierConfigManager.KEY_MMS_UA_PROF_URL_STRING);
10095 if (!TextUtils.isEmpty(carrierUAProfUrl)) {
10096 return carrierUAProfUrl;
10097 }
Daniel Brightebb4eb72020-02-18 15:16:33 -080010098 return SubscriptionManager.getResourcesForSubId(getDefaultPhone().getContext(), subId)
10099 .getString(com.android.internal.R.string.config_mms_user_agent_profile_url);
chen xud5ca2d52019-05-28 15:20:57 -070010100 } finally {
10101 Binder.restoreCallingIdentity(identity);
10102 }
10103 }
10104
10105 @Override
10106 public String getMmsUserAgent(int subId) {
10107 //TODO investigate if this API should require proper permission check in R b/133791609
10108 final long identity = Binder.clearCallingIdentity();
10109 try {
Guoqiang.Qiu171383d2020-07-13 09:38:32 +080010110 String carrierUserAgent = mApp.getCarrierConfigForSubId(subId).getString(
10111 CarrierConfigManager.KEY_MMS_USER_AGENT_STRING);
10112 if (!TextUtils.isEmpty(carrierUserAgent)) {
10113 return carrierUserAgent;
10114 }
Daniel Brightebb4eb72020-02-18 15:16:33 -080010115 return SubscriptionManager.getResourcesForSubId(getDefaultPhone().getContext(), subId)
10116 .getString(com.android.internal.R.string.config_mms_user_agent);
chen xud5ca2d52019-05-28 15:20:57 -070010117 } finally {
10118 Binder.restoreCallingIdentity(identity);
10119 }
10120 }
Jack Yub07d4972019-05-28 16:12:25 -070010121
10122 @Override
Hall Liua62f5da2020-09-25 10:42:19 -070010123 public boolean isMobileDataPolicyEnabled(int subscriptionId, int policy) {
10124 enforceReadPrivilegedPermission("isMobileDataPolicyEnabled");
Jack Yub07d4972019-05-28 16:12:25 -070010125
Jack Yub07d4972019-05-28 16:12:25 -070010126 final long identity = Binder.clearCallingIdentity();
10127 try {
Hall Liua62f5da2020-09-25 10:42:19 -070010128 Phone phone = getPhone(subscriptionId);
Jack Yub07d4972019-05-28 16:12:25 -070010129 if (phone == null) return false;
10130
Ling Maf188d502022-09-16 15:22:36 -070010131 return phone.getDataSettingsManager().isMobileDataPolicyEnabled(policy);
Jack Yub07d4972019-05-28 16:12:25 -070010132 } finally {
10133 Binder.restoreCallingIdentity(identity);
10134 }
10135 }
10136
10137 @Override
Hall Liuc66bb112021-02-02 12:09:32 -080010138 public void setMobileDataPolicyEnabled(int subscriptionId, int policy,
Hall Liua62f5da2020-09-25 10:42:19 -070010139 boolean enabled) {
changbettyd5c246e2019-12-24 15:40:37 +080010140 enforceModifyPermission();
10141
changbettyd5c246e2019-12-24 15:40:37 +080010142 final long identity = Binder.clearCallingIdentity();
10143 try {
Hall Liua62f5da2020-09-25 10:42:19 -070010144 Phone phone = getPhone(subscriptionId);
10145 if (phone == null) return;
changbettyd5c246e2019-12-24 15:40:37 +080010146
Ling Maf188d502022-09-16 15:22:36 -070010147 phone.getDataSettingsManager().setMobileDataPolicy(policy, enabled);
changbettyd5c246e2019-12-24 15:40:37 +080010148 } finally {
10149 Binder.restoreCallingIdentity(identity);
10150 }
10151 }
10152
Tyler Gunn7bcdc742019-10-04 15:56:59 -070010153 /**
Hall Liu746e03c2020-09-25 11:13:49 -070010154 * Updates whether conference event package handling is enabled.
Tyler Gunn7bcdc742019-10-04 15:56:59 -070010155 * @param isCepEnabled {@code true} if CEP handling is enabled (default), or {@code false}
10156 * otherwise.
10157 */
10158 @Override
10159 public void setCepEnabled(boolean isCepEnabled) {
10160 TelephonyPermissions.enforceShellOnly(Binder.getCallingUid(), "setCepEnabled");
10161
10162 final long identity = Binder.clearCallingIdentity();
10163 try {
10164 Rlog.i(LOG_TAG, "setCepEnabled isCepEnabled=" + isCepEnabled);
10165 for (Phone phone : PhoneFactory.getPhones()) {
10166 Phone defaultPhone = phone.getImsPhone();
10167 if (defaultPhone != null && defaultPhone.getPhoneType() == PHONE_TYPE_IMS) {
10168 ImsPhone imsPhone = (ImsPhone) defaultPhone;
10169 ImsPhoneCallTracker imsPhoneCallTracker =
10170 (ImsPhoneCallTracker) imsPhone.getCallTracker();
10171 imsPhoneCallTracker.setConferenceEventPackageEnabled(isCepEnabled);
10172 Rlog.i(LOG_TAG, "setCepEnabled isCepEnabled=" + isCepEnabled + ", for imsPhone "
10173 + imsPhone.getMsisdn());
10174 }
10175 }
10176 } finally {
10177 Binder.restoreCallingIdentity(identity);
10178 }
10179 }
allenwtsu46dcc572020-01-08 18:24:03 +080010180
10181 /**
10182 * Notify that an RCS autoconfiguration XML file has been received for provisioning.
10183 *
10184 * @param config The XML file to be read. ASCII/UTF8 encoded text if not compressed.
10185 * @param isCompressed The XML file is compressed in gzip format and must be decompressed
10186 * before being read.
10187 */
10188 @Override
10189 public void notifyRcsAutoConfigurationReceived(int subId, @NonNull byte[] config, boolean
10190 isCompressed) {
10191 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
10192 mApp, subId, "notifyRcsAutoConfigurationReceived");
Hui Wang761a6682020-10-31 05:12:53 +000010193 if (!SubscriptionManager.isValidSubscriptionId(subId)) {
10194 throw new IllegalArgumentException("Invalid Subscription ID: " + subId);
10195 }
10196 if (!isImsAvailableOnDevice()) {
joonhunshin46b49a32022-12-21 05:33:23 +000010197 // ProvisioningManager can not handle ServiceSpecificException.
10198 // Throw the IllegalStateException and annotate ProvisioningManager.
10199 throw new IllegalStateException("IMS not available on device.");
Hui Wang761a6682020-10-31 05:12:53 +000010200 }
10201
10202 final long identity = Binder.clearCallingIdentity();
allenwtsu46dcc572020-01-08 18:24:03 +080010203 try {
Hui Wang761a6682020-10-31 05:12:53 +000010204 RcsProvisioningMonitor.getInstance().updateConfig(subId, config, isCompressed);
10205 } finally {
10206 Binder.restoreCallingIdentity(identity);
allenwtsu46dcc572020-01-08 18:24:03 +080010207 }
10208 }
zoey chene02881a2019-12-30 16:11:23 +080010209
10210 @Override
10211 public boolean isIccLockEnabled(int subId) {
10212 enforceReadPrivilegedPermission("isIccLockEnabled");
10213
10214 // Now that all security checks passes, perform the operation as ourselves.
10215 final long identity = Binder.clearCallingIdentity();
10216 try {
10217 Phone phone = getPhone(subId);
10218 if (phone != null && phone.getIccCard() != null) {
10219 return phone.getIccCard().getIccLockEnabled();
10220 } else {
10221 return false;
10222 }
10223 } finally {
10224 Binder.restoreCallingIdentity(identity);
10225 }
10226 }
10227
10228 /**
10229 * Set the ICC pin lock enabled or disabled.
10230 *
10231 * @return an integer representing the status of IccLock enabled or disabled in the following
10232 * three cases:
10233 * - {@link TelephonyManager#CHANGE_ICC_LOCK_SUCCESS} if enabled or disabled IccLock
10234 * successfully.
10235 * - Positive number and zero for remaining password attempts.
10236 * - Negative number for other failure cases (such like enabling/disabling PIN failed).
10237 *
10238 */
10239 @Override
10240 public int setIccLockEnabled(int subId, boolean enabled, String password) {
10241 enforceModifyPermission();
10242
10243 Phone phone = getPhone(subId);
10244 if (phone == null) {
10245 return 0;
10246 }
10247 // Now that all security checks passes, perform the operation as ourselves.
10248 final long identity = Binder.clearCallingIdentity();
10249 try {
10250 int attemptsRemaining = (int) sendRequest(CMD_SET_ICC_LOCK_ENABLED,
10251 new Pair<Boolean, String>(enabled, password), phone, null);
10252 return attemptsRemaining;
10253
10254 } catch (Exception e) {
10255 Log.e(LOG_TAG, "setIccLockEnabled. Exception e =" + e);
10256 } finally {
10257 Binder.restoreCallingIdentity(identity);
10258 }
10259 return 0;
10260 }
10261
10262 /**
10263 * Change the ICC password used in ICC pin lock.
10264 *
10265 * @return an integer representing the status of IccLock changed in the following three cases:
10266 * - {@link TelephonyManager#CHANGE_ICC_LOCK_SUCCESS} if changed IccLock successfully.
10267 * - Positive number and zero for remaining password attempts.
10268 * - Negative number for other failure cases (such like enabling/disabling PIN failed).
10269 *
10270 */
10271 @Override
10272 public int changeIccLockPassword(int subId, String oldPassword, String newPassword) {
10273 enforceModifyPermission();
10274
10275 Phone phone = getPhone(subId);
10276 if (phone == null) {
10277 return 0;
10278 }
10279 // Now that all security checks passes, perform the operation as ourselves.
10280 final long identity = Binder.clearCallingIdentity();
10281 try {
10282 int attemptsRemaining = (int) sendRequest(CMD_CHANGE_ICC_LOCK_PASSWORD,
10283 new Pair<String, String>(oldPassword, newPassword), phone, null);
10284 return attemptsRemaining;
10285
10286 } catch (Exception e) {
10287 Log.e(LOG_TAG, "changeIccLockPassword. Exception e =" + e);
10288 } finally {
10289 Binder.restoreCallingIdentity(identity);
10290 }
10291 return 0;
10292 }
Peter Wangdafb9ac2020-01-15 14:13:38 -080010293
10294 /**
10295 * Request for receiving user activity notification
10296 */
10297 @Override
10298 public void requestUserActivityNotification() {
10299 if (!mNotifyUserActivity.get()
10300 && !mMainThreadHandler.hasMessages(MSG_NOTIFY_USER_ACTIVITY)) {
10301 mNotifyUserActivity.set(true);
10302 }
10303 }
10304
10305 /**
10306 * Called when userActivity is signalled in the power manager.
10307 * This is safe to call from any thread, with any window manager locks held or not.
10308 */
10309 @Override
10310 public void userActivity() {
10311 // ***************************************
10312 // * Inherited from PhoneWindowManager *
10313 // ***************************************
10314 // THIS IS CALLED FROM DEEP IN THE POWER MANAGER
10315 // WITH ITS LOCKS HELD.
10316 //
10317 // This code must be VERY careful about the locks
10318 // it acquires.
10319 // In fact, the current code acquires way too many,
10320 // and probably has lurking deadlocks.
10321
10322 if (Binder.getCallingUid() != Process.SYSTEM_UID) {
10323 throw new SecurityException("Only the OS may call notifyUserActivity()");
10324 }
10325
10326 if (mNotifyUserActivity.getAndSet(false)) {
10327 mMainThreadHandler.sendEmptyMessageDelayed(MSG_NOTIFY_USER_ACTIVITY,
10328 USER_ACTIVITY_NOTIFICATION_DELAY);
10329 }
10330 }
Malcolm Chen4639c562020-04-13 11:59:40 -070010331
10332 @Override
10333 public boolean canConnectTo5GInDsdsMode() {
10334 return mApp.getResources().getBoolean(R.bool.config_5g_connection_in_dsds_mode);
10335 }
Jack Yud10cdd42020-09-28 20:28:01 -070010336
10337 @Override
10338 public @NonNull List<String> getEquivalentHomePlmns(int subId, String callingPackage,
10339 String callingFeatureId) {
10340 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
10341 mApp, subId, callingPackage, callingFeatureId, "getEquivalentHomePlmns")) {
10342 throw new SecurityException("Requires READ_PHONE_STATE permission.");
10343 }
10344
10345 Phone phone = getPhone(subId);
10346 if (phone == null) {
10347 throw new RuntimeException("phone is not available");
10348 }
10349 // Now that all security checks passes, perform the operation as ourselves.
10350 final long identity = Binder.clearCallingIdentity();
10351 try {
10352 return phone.getEquivalentHomePlmns();
10353 } finally {
10354 Binder.restoreCallingIdentity(identity);
10355 }
10356 }
Daniel Bright59e67312020-11-13 11:49:37 -080010357
10358 @Override
10359 public boolean isRadioInterfaceCapabilitySupported(
Daniel Bright95a4c1f2021-02-11 09:57:16 -080010360 final @NonNull @TelephonyManager.RadioInterfaceCapability String capability) {
10361 Set<String> radioInterfaceCapabilities =
Daniel Bright94f43662021-03-01 14:43:40 -080010362 mRadioInterfaceCapabilities.getCapabilities();
Daniel Bright59e67312020-11-13 11:49:37 -080010363 if (radioInterfaceCapabilities == null) {
10364 throw new RuntimeException("radio interface capabilities are not available");
Daniel Bright59e67312020-11-13 11:49:37 -080010365 }
Daniel Bright95a4c1f2021-02-11 09:57:16 -080010366 return radioInterfaceCapabilities.contains(capability);
Daniel Bright59e67312020-11-13 11:49:37 -080010367 }
Jack Nudelmanb0b87642020-11-12 15:04:39 -080010368
Hui Wang641e81c2020-10-12 12:14:23 -070010369 @Override
10370 public void bootstrapAuthenticationRequest(int subId, int appType, Uri nafUrl,
10371 UaSecurityProtocolIdentifier securityProtocol,
Brad Ebinger34c09a52021-02-17 23:23:21 +000010372 boolean forceBootStrapping, IBootstrapAuthenticationCallback callback) {
10373 TelephonyPermissions.enforceAnyPermissionGrantedOrCarrierPrivileges(mApp, subId,
10374 Binder.getCallingUid(), "bootstrapAuthenticationRequest",
10375 Manifest.permission.PERFORM_IMS_SINGLE_REGISTRATION,
10376 Manifest.permission.MODIFY_PHONE_STATE);
Hui Wang641e81c2020-10-12 12:14:23 -070010377 if (DBG) {
10378 log("bootstrapAuthenticationRequest, subId:" + subId + ", appType:"
10379 + appType + ", NAF:" + nafUrl + ", sp:" + securityProtocol
10380 + ", forceBootStrapping:" + forceBootStrapping + ", callback:" + callback);
10381 }
10382
10383 if (!SubscriptionManager.isValidSubscriptionId(subId)
10384 || appType < TelephonyManager.APPTYPE_UNKNOWN
10385 || appType > TelephonyManager.APPTYPE_ISIM
10386 || nafUrl == null || securityProtocol == null || callback == null) {
10387 Log.d(LOG_TAG, "bootstrapAuthenticationRequest failed due to invalid parameters");
10388 if (callback != null) {
10389 try {
10390 callback.onAuthenticationFailure(
10391 0, TelephonyManager.GBA_FAILURE_REASON_FEATURE_NOT_SUPPORTED);
10392 } catch (RemoteException exception) {
10393 log("Fail to notify onAuthenticationFailure due to " + exception);
10394 }
10395 return;
10396 }
10397 }
10398
10399 final long token = Binder.clearCallingIdentity();
10400 try {
10401 getGbaManager(subId).bootstrapAuthenticationRequest(
10402 new GbaAuthRequest(subId, appType, nafUrl, securityProtocol.toByteArray(),
10403 forceBootStrapping, callback));
10404 } finally {
10405 Binder.restoreCallingIdentity(token);
10406 }
10407 }
10408
Jack Nudelmanb0b87642020-11-12 15:04:39 -080010409 /**
Sooraj Sasindran72cff492021-07-29 09:42:42 -070010410 * Attempts to set the radio power state for all phones for thermal reason.
10411 * This does not guarantee that the
Jack Nudelmanb0b87642020-11-12 15:04:39 -080010412 * requested radio power state will actually be set. See {@link
10413 * PhoneInternalInterface#setRadioPowerForReason} for more details.
10414 *
Jack Nudelmanb0b87642020-11-12 15:04:39 -080010415 * @param enable {@code true} if trying to turn radio on.
10416 * @return {@code true} if phone setRadioPowerForReason was called. Otherwise, returns {@code
10417 * false}.
10418 */
Sooraj Sasindran72cff492021-07-29 09:42:42 -070010419 private boolean setRadioPowerForThermal(boolean enable) {
10420 boolean isPhoneAvailable = false;
10421 for (int i = 0; i < TelephonyManager.getDefault().getActiveModemCount(); i++) {
10422 Phone phone = PhoneFactory.getPhone(i);
10423 if (phone != null) {
Thomas Nguyenfd0572f2022-07-15 22:28:49 +000010424 phone.setRadioPowerForReason(enable, TelephonyManager.RADIO_POWER_REASON_THERMAL);
Sooraj Sasindran72cff492021-07-29 09:42:42 -070010425 isPhoneAvailable = true;
10426 }
Jack Nudelmanb0b87642020-11-12 15:04:39 -080010427 }
Sooraj Sasindran72cff492021-07-29 09:42:42 -070010428
10429 // return true if successfully informed the phone object about the thermal radio power
10430 // request.
10431 return isPhoneAvailable;
Jack Nudelmanb0b87642020-11-12 15:04:39 -080010432 }
10433
10434 private int handleDataThrottlingRequest(int subId,
Sarah Chinecc78c42022-03-31 21:16:48 -070010435 DataThrottlingRequest dataThrottlingRequest, String callingPackage) {
Jack Nudelman5d6a98b2021-03-04 14:26:25 -080010436 boolean isDataThrottlingSupported = isRadioInterfaceCapabilitySupported(
10437 TelephonyManager.CAPABILITY_THERMAL_MITIGATION_DATA_THROTTLING);
10438 if (!isDataThrottlingSupported && dataThrottlingRequest.getDataThrottlingAction()
10439 != DataThrottlingRequest.DATA_THROTTLING_ACTION_NO_DATA_THROTTLING) {
10440 throw new IllegalArgumentException("modem does not support data throttling");
10441 }
10442
Jack Nudelmanb0b87642020-11-12 15:04:39 -080010443 // Ensure that radio is on. If not able to power on due to phone being unavailable, return
10444 // THERMAL_MITIGATION_RESULT_MODEM_NOT_AVAILABLE.
Sooraj Sasindran72cff492021-07-29 09:42:42 -070010445 if (!setRadioPowerForThermal(true)) {
Jack Nudelmanb0b87642020-11-12 15:04:39 -080010446 return TelephonyManager.THERMAL_MITIGATION_RESULT_MODEM_NOT_AVAILABLE;
10447 }
10448
Sarah Chinecc78c42022-03-31 21:16:48 -070010449 setDataEnabledForReason(
10450 subId, TelephonyManager.DATA_ENABLED_REASON_THERMAL, true, callingPackage);
Jack Nudelmanb0b87642020-11-12 15:04:39 -080010451
Jack Nudelman5d6a98b2021-03-04 14:26:25 -080010452 if (isDataThrottlingSupported) {
10453 int thermalMitigationResult =
Jack Nudelmanb0b87642020-11-12 15:04:39 -080010454 (int) sendRequest(CMD_SET_DATA_THROTTLING, dataThrottlingRequest, subId);
Jack Nudelman5d6a98b2021-03-04 14:26:25 -080010455 if (thermalMitigationResult == SET_DATA_THROTTLING_MODEM_THREW_INVALID_PARAMS) {
10456 throw new IllegalArgumentException("modem returned INVALID_ARGUMENTS");
10457 } else if (thermalMitigationResult
10458 == MODEM_DOES_NOT_SUPPORT_DATA_THROTTLING_ERROR_CODE) {
Jack Nudelman760d0962021-05-20 13:57:30 -070010459 log("Modem likely does not support data throttling on secondary carrier. Data " +
10460 "throttling action = " + dataThrottlingRequest.getDataThrottlingAction());
10461 return TelephonyManager.THERMAL_MITIGATION_RESULT_MODEM_ERROR;
Jack Nudelman5d6a98b2021-03-04 14:26:25 -080010462 }
10463 return thermalMitigationResult;
Jack Nudelmanb0b87642020-11-12 15:04:39 -080010464 }
Jack Nudelman5d6a98b2021-03-04 14:26:25 -080010465
10466 return TelephonyManager.THERMAL_MITIGATION_RESULT_SUCCESS;
Jack Nudelmanb0b87642020-11-12 15:04:39 -080010467 }
10468
Jack Nudelman644b91a2021-03-12 14:09:48 -080010469 private static List<String> getThermalMitigationAllowlist(Context context) {
10470 if (sThermalMitigationAllowlistedPackages.isEmpty()) {
10471 for (String pckg : context.getResources()
10472 .getStringArray(R.array.thermal_mitigation_allowlisted_packages)) {
10473 sThermalMitigationAllowlistedPackages.add(pckg);
10474 }
10475 }
10476
10477 return sThermalMitigationAllowlistedPackages;
10478 }
10479
Jack Nudelmane69bbc82021-05-13 10:00:15 -070010480 private boolean isAnyPhoneInEmergencyState() {
10481 TelecomManager tm = mApp.getSystemService(TelecomManager.class);
10482 if (tm.isInEmergencyCall()) {
10483 Log.e(LOG_TAG , "Phone state is not valid. One of the phones is in an emergency call");
10484 return true;
10485 }
10486 for (Phone phone : PhoneFactory.getPhones()) {
10487 if (phone.isInEmergencySmsMode() || phone.isInEcm()) {
10488 Log.e(LOG_TAG, "Phone state is not valid. isInEmergencySmsMode = "
10489 + phone.isInEmergencySmsMode() + " isInEmergencyCallbackMode = "
10490 + phone.isInEcm());
10491 return true;
10492 }
10493 }
10494
10495 return false;
10496 }
10497
Jack Nudelman644b91a2021-03-12 14:09:48 -080010498 /**
10499 * Used by shell commands to add an authorized package name for thermal mitigation.
10500 * @param packageName name of package to be allowlisted
10501 * @param context
10502 */
10503 static void addPackageToThermalMitigationAllowlist(String packageName, Context context) {
10504 sThermalMitigationAllowlistedPackages = getThermalMitigationAllowlist(context);
10505 sThermalMitigationAllowlistedPackages.add(packageName);
10506 }
10507
10508 /**
10509 * Used by shell commands to remove an authorized package name for thermal mitigation.
10510 * @param packageName name of package to remove from allowlist
10511 * @param context
10512 */
10513 static void removePackageFromThermalMitigationAllowlist(String packageName, Context context) {
10514 sThermalMitigationAllowlistedPackages = getThermalMitigationAllowlist(context);
10515 sThermalMitigationAllowlistedPackages.remove(packageName);
10516 }
10517
Jack Nudelmanb0b87642020-11-12 15:04:39 -080010518 /**
10519 * Thermal mitigation request to control functionalities at modem.
10520 *
10521 * @param subId the id of the subscription.
10522 * @param thermalMitigationRequest holds all necessary information to be passed down to modem.
Jack Nudelman644b91a2021-03-12 14:09:48 -080010523 * @param callingPackage the package name of the calling package.
Jack Nudelmanb0b87642020-11-12 15:04:39 -080010524 *
10525 * @return thermalMitigationResult enum as defined in android.telephony.Annotation.
10526 */
10527 @Override
10528 @ThermalMitigationResult
10529 public int sendThermalMitigationRequest(
10530 int subId,
Jack Nudelman644b91a2021-03-12 14:09:48 -080010531 ThermalMitigationRequest thermalMitigationRequest,
10532 String callingPackage) throws IllegalArgumentException {
Jack Nudelmanb0b87642020-11-12 15:04:39 -080010533 enforceModifyPermission();
10534
Jack Nudelman644b91a2021-03-12 14:09:48 -080010535 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
10536 if (!getThermalMitigationAllowlist(getDefaultPhone().getContext())
10537 .contains(callingPackage)) {
10538 throw new SecurityException("Calling package must be configured in the device config. "
10539 + "calling package: " + callingPackage);
10540 }
10541
Jack Nudelmanb0b87642020-11-12 15:04:39 -080010542 WorkSource workSource = getWorkSource(Binder.getCallingUid());
10543 final long identity = Binder.clearCallingIdentity();
10544
10545 int thermalMitigationResult = TelephonyManager.THERMAL_MITIGATION_RESULT_UNKNOWN_ERROR;
10546 try {
10547 int thermalMitigationAction = thermalMitigationRequest.getThermalMitigationAction();
10548 switch (thermalMitigationAction) {
10549 case ThermalMitigationRequest.THERMAL_MITIGATION_ACTION_DATA_THROTTLING:
10550 thermalMitigationResult =
10551 handleDataThrottlingRequest(subId,
Sarah Chinecc78c42022-03-31 21:16:48 -070010552 thermalMitigationRequest.getDataThrottlingRequest(),
10553 callingPackage);
Jack Nudelmanb0b87642020-11-12 15:04:39 -080010554 break;
10555 case ThermalMitigationRequest.THERMAL_MITIGATION_ACTION_VOICE_ONLY:
10556 if (thermalMitigationRequest.getDataThrottlingRequest() != null) {
10557 throw new IllegalArgumentException("dataThrottlingRequest must be null for "
10558 + "ThermalMitigationRequest.THERMAL_MITIGATION_ACTION_VOICE_ONLY");
10559 }
10560
10561 // Ensure that radio is on. If not able to power on due to phone being
10562 // unavailable, return THERMAL_MITIGATION_RESULT_MODEM_NOT_AVAILABLE.
Sooraj Sasindran72cff492021-07-29 09:42:42 -070010563 if (!setRadioPowerForThermal(true)) {
Jack Nudelmanb0b87642020-11-12 15:04:39 -080010564 thermalMitigationResult =
10565 TelephonyManager.THERMAL_MITIGATION_RESULT_MODEM_NOT_AVAILABLE;
10566 break;
10567 }
10568
10569 setDataEnabledForReason(subId, TelephonyManager.DATA_ENABLED_REASON_THERMAL,
Sarah Chinecc78c42022-03-31 21:16:48 -070010570 false, callingPackage);
Jack Nudelmanb0b87642020-11-12 15:04:39 -080010571 thermalMitigationResult = TelephonyManager.THERMAL_MITIGATION_RESULT_SUCCESS;
10572 break;
10573 case ThermalMitigationRequest.THERMAL_MITIGATION_ACTION_RADIO_OFF:
10574 if (thermalMitigationRequest.getDataThrottlingRequest() != null) {
10575 throw new IllegalArgumentException("dataThrottlingRequest must be null for"
10576 + " ThermalMitigationRequest.THERMAL_MITIGATION_ACTION_RADIO_OFF");
10577 }
10578
10579 TelecomAccountRegistry registry = TelecomAccountRegistry.getInstance(null);
10580 if (registry != null) {
Jack Nudelmanb0b87642020-11-12 15:04:39 -080010581 Phone phone = getPhone(subId);
10582 if (phone == null) {
10583 thermalMitigationResult =
10584 TelephonyManager.THERMAL_MITIGATION_RESULT_MODEM_NOT_AVAILABLE;
10585 break;
10586 }
10587
Jack Nudelmane69bbc82021-05-13 10:00:15 -070010588 TelephonyConnectionService service =
10589 registry.getTelephonyConnectionService();
Jack Nudelmanb30ac302021-06-17 15:39:58 -070010590 if (service != null && service.isEmergencyCallPending()) {
Jack Nudelmane69bbc82021-05-13 10:00:15 -070010591 Log.e(LOG_TAG, "An emergency call is pending");
10592 thermalMitigationResult =
10593 TelephonyManager.THERMAL_MITIGATION_RESULT_INVALID_STATE;
10594 break;
10595 } else if (isAnyPhoneInEmergencyState()) {
Jack Nudelmanb0b87642020-11-12 15:04:39 -080010596 thermalMitigationResult =
10597 TelephonyManager.THERMAL_MITIGATION_RESULT_INVALID_STATE;
10598 break;
10599 }
10600 } else {
10601 thermalMitigationResult =
10602 TelephonyManager.THERMAL_MITIGATION_RESULT_MODEM_NOT_AVAILABLE;
10603 break;
10604 }
10605
10606 // Turn radio off. If not able to power off due to phone being unavailable,
10607 // return THERMAL_MITIGATION_RESULT_MODEM_NOT_AVAILABLE.
Sooraj Sasindran72cff492021-07-29 09:42:42 -070010608 if (!setRadioPowerForThermal(false)) {
Jack Nudelmanb0b87642020-11-12 15:04:39 -080010609 thermalMitigationResult =
10610 TelephonyManager.THERMAL_MITIGATION_RESULT_MODEM_NOT_AVAILABLE;
10611 break;
10612 }
10613 thermalMitigationResult =
10614 TelephonyManager.THERMAL_MITIGATION_RESULT_SUCCESS;
10615 break;
10616 default:
10617 throw new IllegalArgumentException("the requested thermalMitigationAction does "
10618 + "not exist. Requested action: " + thermalMitigationAction);
10619 }
10620 } catch (IllegalArgumentException e) {
10621 throw e;
10622 } catch (Exception e) {
10623 Log.e(LOG_TAG, "thermalMitigationRequest. Exception e =" + e);
10624 thermalMitigationResult = TelephonyManager.THERMAL_MITIGATION_RESULT_MODEM_ERROR;
10625 } finally {
10626 Binder.restoreCallingIdentity(identity);
10627 }
10628
10629 if (DBG) {
10630 log("thermalMitigationRequest returning with thermalMitigationResult: "
10631 + thermalMitigationResult);
10632 }
10633
10634 return thermalMitigationResult;
10635 }
Hui Wang641e81c2020-10-12 12:14:23 -070010636
10637 /**
10638 * Set the GbaService Package Name that Telephony will bind to.
10639 *
10640 * @param subId The sim that the GbaService is associated with.
10641 * @param packageName The name of the package to be replaced with.
10642 * @return true if setting the GbaService to bind to succeeded, false if it did not.
10643 */
10644 @Override
10645 public boolean setBoundGbaServiceOverride(int subId, String packageName) {
10646 enforceModifyPermission();
10647
10648 final long identity = Binder.clearCallingIdentity();
10649 try {
10650 return getGbaManager(subId).overrideServicePackage(packageName);
10651 } finally {
10652 Binder.restoreCallingIdentity(identity);
10653 }
10654 }
10655
10656 /**
10657 * Return the package name of the currently bound GbaService.
10658 *
10659 * @param subId The sim that the GbaService is associated with.
10660 * @return the package name of the GbaService configuration, null if GBA is not supported.
10661 */
10662 @Override
10663 public String getBoundGbaService(int subId) {
10664 enforceReadPrivilegedPermission("getBoundGbaServicePackage");
10665
10666 final long identity = Binder.clearCallingIdentity();
10667 try {
10668 return getGbaManager(subId).getServicePackage();
10669 } finally {
10670 Binder.restoreCallingIdentity(identity);
10671 }
10672 }
10673
10674 /**
10675 * Set the release time for telephony to unbind GbaService.
10676 *
10677 * @param subId The sim that the GbaService is associated with.
10678 * @param interval The release time to unbind GbaService by millisecond.
10679 * @return true if setting the GbaService to bind to succeeded, false if it did not.
10680 */
10681 @Override
10682 public boolean setGbaReleaseTimeOverride(int subId, int interval) {
10683 enforceModifyPermission();
10684
10685 final long identity = Binder.clearCallingIdentity();
10686 try {
10687 return getGbaManager(subId).overrideReleaseTime(interval);
10688 } finally {
10689 Binder.restoreCallingIdentity(identity);
10690 }
10691 }
10692
10693 /**
10694 * Return the release time for telephony to unbind GbaService.
10695 *
10696 * @param subId The sim that the GbaService is associated with.
10697 * @return The release time to unbind GbaService by millisecond.
10698 */
10699 @Override
10700 public int getGbaReleaseTime(int subId) {
10701 enforceReadPrivilegedPermission("getGbaReleaseTime");
10702
10703 final long identity = Binder.clearCallingIdentity();
10704 try {
10705 return getGbaManager(subId).getReleaseTime();
10706 } finally {
10707 Binder.restoreCallingIdentity(identity);
10708 }
10709 }
10710
10711 private GbaManager getGbaManager(int subId) {
10712 GbaManager instance = GbaManager.getInstance(subId);
10713 if (instance == null) {
10714 String packageName = mApp.getResources().getString(R.string.config_gba_package);
10715 int releaseTime = mApp.getResources().getInteger(R.integer.config_gba_release_time);
10716 instance = GbaManager.make(mApp, subId, packageName, releaseTime);
10717 }
10718 return instance;
10719 }
Hui Wang761a6682020-10-31 05:12:53 +000010720
10721 /**
10722 * indicate whether the device and the carrier can support
10723 * RCS VoLTE single registration.
10724 */
10725 @Override
10726 public boolean isRcsVolteSingleRegistrationCapable(int subId) {
Brad Ebinger34c09a52021-02-17 23:23:21 +000010727 TelephonyPermissions.enforceAnyPermissionGrantedOrCarrierPrivileges(mApp, subId,
10728 Binder.getCallingUid(), "isRcsVolteSingleRegistrationCapable",
10729 Manifest.permission.PERFORM_IMS_SINGLE_REGISTRATION,
10730 permission.READ_PRIVILEGED_PHONE_STATE);
Hui Wang761a6682020-10-31 05:12:53 +000010731
10732 if (!SubscriptionManager.isValidSubscriptionId(subId)) {
10733 throw new IllegalArgumentException("Invalid Subscription ID: " + subId);
10734 }
10735
10736 final long identity = Binder.clearCallingIdentity();
10737 try {
10738 RcsProvisioningMonitor rpm = RcsProvisioningMonitor.getInstance();
10739 if (rpm != null) {
Hui Wang67af90e2021-06-04 16:57:15 -070010740 Boolean isCapable = rpm.isRcsVolteSingleRegistrationEnabled(subId);
10741 if (isCapable != null) {
10742 return isCapable;
10743 }
Hui Wang761a6682020-10-31 05:12:53 +000010744 }
Hui Wang67af90e2021-06-04 16:57:15 -070010745 throw new ServiceSpecificException(ImsException.CODE_ERROR_SERVICE_UNAVAILABLE,
10746 "service is temporarily unavailable.");
Hui Wang761a6682020-10-31 05:12:53 +000010747 } finally {
10748 Binder.restoreCallingIdentity(identity);
10749 }
10750 }
10751
10752 /**
10753 * Register RCS provisioning callback.
10754 */
10755 @Override
Hui Wang3cac7e52021-01-27 14:45:25 -080010756 public void registerRcsProvisioningCallback(int subId,
Hui Wang761a6682020-10-31 05:12:53 +000010757 IRcsConfigCallback callback) {
Brad Ebinger34c09a52021-02-17 23:23:21 +000010758 TelephonyPermissions.enforceAnyPermissionGrantedOrCarrierPrivileges(mApp, subId,
Hui Wang3cac7e52021-01-27 14:45:25 -080010759 Binder.getCallingUid(), "registerRcsProvisioningCallback",
Brad Ebinger34c09a52021-02-17 23:23:21 +000010760 Manifest.permission.PERFORM_IMS_SINGLE_REGISTRATION,
10761 permission.READ_PRIVILEGED_PHONE_STATE);
Hui Wang761a6682020-10-31 05:12:53 +000010762
10763 if (!SubscriptionManager.isValidSubscriptionId(subId)) {
10764 throw new IllegalArgumentException("Invalid Subscription ID: " + subId);
10765 }
10766 if (!isImsAvailableOnDevice()) {
10767 throw new ServiceSpecificException(ImsException.CODE_ERROR_UNSUPPORTED_OPERATION,
10768 "IMS not available on device.");
10769 }
10770
10771 final long identity = Binder.clearCallingIdentity();
10772 try {
Hui Wang68cd3722021-01-11 20:04:53 -080010773 if (!RcsProvisioningMonitor.getInstance()
Hui Wang3cac7e52021-01-27 14:45:25 -080010774 .registerRcsProvisioningCallback(subId, callback)) {
Brad Ebinger919631e2021-06-02 17:46:35 -070010775 throw new ServiceSpecificException(ImsException.CODE_ERROR_INVALID_SUBSCRIPTION,
10776 "Active subscription not found.");
Hui Wang68cd3722021-01-11 20:04:53 -080010777 }
Hui Wang761a6682020-10-31 05:12:53 +000010778 } finally {
10779 Binder.restoreCallingIdentity(identity);
10780 }
10781 }
10782
10783 /**
10784 * Unregister RCS provisioning callback.
10785 */
10786 @Override
Hui Wang3cac7e52021-01-27 14:45:25 -080010787 public void unregisterRcsProvisioningCallback(int subId,
Hui Wang761a6682020-10-31 05:12:53 +000010788 IRcsConfigCallback callback) {
Brad Ebinger34c09a52021-02-17 23:23:21 +000010789 TelephonyPermissions.enforceAnyPermissionGrantedOrCarrierPrivileges(mApp, subId,
Hui Wang3cac7e52021-01-27 14:45:25 -080010790 Binder.getCallingUid(), "unregisterRcsProvisioningCallback",
Brad Ebinger34c09a52021-02-17 23:23:21 +000010791 Manifest.permission.PERFORM_IMS_SINGLE_REGISTRATION,
10792 permission.READ_PRIVILEGED_PHONE_STATE);
Hui Wang761a6682020-10-31 05:12:53 +000010793
10794 if (!SubscriptionManager.isValidSubscriptionId(subId)) {
10795 throw new IllegalArgumentException("Invalid Subscription ID: " + subId);
10796 }
10797 if (!isImsAvailableOnDevice()) {
joonhunshin46b49a32022-12-21 05:33:23 +000010798 // operation failed silently
10799 Rlog.w(LOG_TAG, "IMS not available on device.");
10800 return;
Hui Wang761a6682020-10-31 05:12:53 +000010801 }
10802
10803 final long identity = Binder.clearCallingIdentity();
10804 try {
Hui Wang68cd3722021-01-11 20:04:53 -080010805 RcsProvisioningMonitor.getInstance()
Hui Wang3cac7e52021-01-27 14:45:25 -080010806 .unregisterRcsProvisioningCallback(subId, callback);
Hui Wang761a6682020-10-31 05:12:53 +000010807 } finally {
10808 Binder.restoreCallingIdentity(identity);
10809 }
10810 }
10811
10812 /**
10813 * trigger RCS reconfiguration.
10814 */
10815 public void triggerRcsReconfiguration(int subId) {
Brad Ebinger34c09a52021-02-17 23:23:21 +000010816 TelephonyPermissions.enforceAnyPermissionGranted(mApp, Binder.getCallingUid(),
10817 "triggerRcsReconfiguration",
10818 Manifest.permission.PERFORM_IMS_SINGLE_REGISTRATION);
Hui Wang761a6682020-10-31 05:12:53 +000010819
10820 if (!SubscriptionManager.isValidSubscriptionId(subId)) {
10821 throw new IllegalArgumentException("Invalid Subscription ID: " + subId);
10822 }
10823 if (!isImsAvailableOnDevice()) {
joonhunshin46b49a32022-12-21 05:33:23 +000010824 // ProvisioningManager can not handle ServiceSpecificException.
10825 // Throw the IllegalStateException and annotate ProvisioningManager.
10826 throw new IllegalStateException("IMS not available on device.");
Hui Wang761a6682020-10-31 05:12:53 +000010827 }
10828
10829 final long identity = Binder.clearCallingIdentity();
10830 try {
10831 RcsProvisioningMonitor.getInstance().requestReconfig(subId);
10832 } finally {
10833 Binder.restoreCallingIdentity(identity);
10834 }
10835 }
10836
10837 /**
10838 * Provide the client configuration parameters of the RCS application.
10839 */
10840 public void setRcsClientConfiguration(int subId, RcsClientConfiguration rcc) {
Brad Ebinger34c09a52021-02-17 23:23:21 +000010841 TelephonyPermissions.enforceAnyPermissionGranted(mApp, Binder.getCallingUid(),
10842 "setRcsClientConfiguration",
10843 Manifest.permission.PERFORM_IMS_SINGLE_REGISTRATION);
Hui Wang761a6682020-10-31 05:12:53 +000010844
10845 if (!SubscriptionManager.isValidSubscriptionId(subId)) {
10846 throw new IllegalArgumentException("Invalid Subscription ID: " + subId);
10847 }
10848 if (!isImsAvailableOnDevice()) {
10849 throw new ServiceSpecificException(ImsException.CODE_ERROR_UNSUPPORTED_OPERATION,
10850 "IMS not available on device.");
10851 }
10852
10853 final long identity = Binder.clearCallingIdentity();
10854
10855 try {
10856 IImsConfig configBinder = getImsConfig(getSlotIndex(subId), ImsFeature.FEATURE_RCS);
10857 if (configBinder == null) {
10858 Rlog.e(LOG_TAG, "null result for setRcsClientConfiguration");
Brad Ebinger919631e2021-06-02 17:46:35 -070010859 throw new ServiceSpecificException(ImsException.CODE_ERROR_INVALID_SUBSCRIPTION,
10860 "could not find the requested subscription");
Hui Wang761a6682020-10-31 05:12:53 +000010861 } else {
10862 configBinder.setRcsClientConfiguration(rcc);
10863 }
joonhunshin3e154242021-09-17 06:33:39 +000010864
10865 RcsStats.getInstance().onRcsClientProvisioningStats(subId,
10866 RCS_CLIENT_PROVISIONING_STATS__EVENT__CLIENT_PARAMS_SENT);
Hui Wang761a6682020-10-31 05:12:53 +000010867 } catch (RemoteException e) {
10868 Rlog.e(LOG_TAG, "fail to setRcsClientConfiguration " + e.getMessage());
Brad Ebinger919631e2021-06-02 17:46:35 -070010869 throw new ServiceSpecificException(ImsException.CODE_ERROR_SERVICE_UNAVAILABLE,
10870 "service is temporarily unavailable.");
Hui Wang761a6682020-10-31 05:12:53 +000010871 } finally {
10872 Binder.restoreCallingIdentity(identity);
10873 }
10874 }
10875
10876 /**
Hui Wangbaaee6a2021-02-19 20:45:36 -080010877 * Enables or disables the test mode for RCS VoLTE single registration.
10878 */
10879 @Override
10880 public void setRcsSingleRegistrationTestModeEnabled(boolean enabled) {
10881 TelephonyPermissions.enforceShellOnly(Binder.getCallingUid(),
10882 "setRcsSingleRegistrationTestModeEnabled");
10883
10884 RcsProvisioningMonitor.getInstance().setTestModeEnabled(enabled);
10885 }
10886
10887 /**
10888 * Gets the test mode for RCS VoLTE single registration.
10889 */
10890 @Override
10891 public boolean getRcsSingleRegistrationTestModeEnabled() {
10892 TelephonyPermissions.enforceShellOnly(Binder.getCallingUid(),
10893 "getRcsSingleRegistrationTestModeEnabled");
10894
10895 return RcsProvisioningMonitor.getInstance().getTestModeEnabled();
10896 }
10897
10898 /**
Hui Wang761a6682020-10-31 05:12:53 +000010899 * Overrides the config of RCS VoLTE single registration enabled for the device.
10900 */
10901 @Override
10902 public void setDeviceSingleRegistrationEnabledOverride(String enabledStr) {
10903 TelephonyPermissions.enforceShellOnly(Binder.getCallingUid(),
10904 "setDeviceSingleRegistrationEnabledOverride");
10905 enforceModifyPermission();
10906
10907 Boolean enabled = "NULL".equalsIgnoreCase(enabledStr) ? null
10908 : Boolean.parseBoolean(enabledStr);
10909 RcsProvisioningMonitor.getInstance().overrideDeviceSingleRegistrationEnabled(enabled);
Brad Ebinger49a72b42021-01-29 00:55:24 +000010910 mApp.imsRcsController.setDeviceSingleRegistrationSupportOverride(enabled);
Hui Wang761a6682020-10-31 05:12:53 +000010911 }
10912
10913 /**
Tyler Gunn92479152021-01-20 16:30:10 -080010914 * Sends a device to device communication message. Only usable via shell.
10915 * @param message message to send.
10916 * @param value message value.
10917 */
10918 @Override
10919 public void sendDeviceToDeviceMessage(int message, int value) {
10920 TelephonyPermissions.enforceShellOnly(Binder.getCallingUid(),
Tyler Gunnbabbda02021-02-10 11:05:02 -080010921 "sendDeviceToDeviceMessage");
Tyler Gunn92479152021-01-20 16:30:10 -080010922 enforceModifyPermission();
10923
10924 final long identity = Binder.clearCallingIdentity();
10925 try {
10926 TelephonyConnectionService service =
10927 TelecomAccountRegistry.getInstance(null).getTelephonyConnectionService();
10928 if (service == null) {
10929 Rlog.e(LOG_TAG, "sendDeviceToDeviceMessage: not in a call.");
10930 return;
10931 }
10932 service.sendTestDeviceToDeviceMessage(message, value);
10933 } finally {
10934 Binder.restoreCallingIdentity(identity);
10935 }
10936 }
10937
Tyler Gunnbabbda02021-02-10 11:05:02 -080010938 /**
10939 * Sets the specified device to device transport active.
10940 * @param transport The transport to set active.
10941 */
10942 @Override
10943 public void setActiveDeviceToDeviceTransport(@NonNull String transport) {
10944 TelephonyPermissions.enforceShellOnly(Binder.getCallingUid(),
10945 "setActiveDeviceToDeviceTransport");
10946 enforceModifyPermission();
10947
10948 final long identity = Binder.clearCallingIdentity();
10949 try {
10950 TelephonyConnectionService service =
10951 TelecomAccountRegistry.getInstance(null).getTelephonyConnectionService();
10952 if (service == null) {
10953 Rlog.e(LOG_TAG, "setActiveDeviceToDeviceTransport: not in a call.");
10954 return;
10955 }
10956 service.setActiveDeviceToDeviceTransport(transport);
10957 } finally {
10958 Binder.restoreCallingIdentity(identity);
10959 }
10960 }
Tyler Gunn92479152021-01-20 16:30:10 -080010961
Tyler Gunnd4339262021-05-03 14:46:49 -070010962 @Override
10963 public void setDeviceToDeviceForceEnabled(boolean isForceEnabled) {
10964 TelephonyPermissions.enforceShellOnly(Binder.getCallingUid(),
10965 "setDeviceToDeviceForceEnabled");
10966
10967 final long identity = Binder.clearCallingIdentity();
10968 try {
10969 Arrays.stream(PhoneFactory.getPhones()).forEach(
10970 p -> {
10971 Phone thePhone = p.getImsPhone();
10972 if (thePhone != null && thePhone instanceof ImsPhone) {
10973 ImsPhone imsPhone = (ImsPhone) thePhone;
10974 CallTracker tracker = imsPhone.getCallTracker();
10975 if (tracker != null && tracker instanceof ImsPhoneCallTracker) {
10976 ImsPhoneCallTracker imsPhoneCallTracker =
10977 (ImsPhoneCallTracker) tracker;
10978 imsPhoneCallTracker.setDeviceToDeviceForceEnabled(isForceEnabled);
10979 }
10980 }
10981 }
10982 );
10983 } finally {
10984 Binder.restoreCallingIdentity(identity);
10985 }
10986 }
10987
Tyler Gunn92479152021-01-20 16:30:10 -080010988 /**
Hui Wang761a6682020-10-31 05:12:53 +000010989 * Gets the config of RCS VoLTE single registration enabled for the device.
10990 */
10991 @Override
10992 public boolean getDeviceSingleRegistrationEnabled() {
10993 enforceReadPrivilegedPermission("getDeviceSingleRegistrationEnabled");
10994 return RcsProvisioningMonitor.getInstance().getDeviceSingleRegistrationEnabled();
10995 }
10996
10997 /**
10998 * Overrides the config of RCS VoLTE single registration enabled for the carrier/subscription.
10999 */
11000 @Override
11001 public boolean setCarrierSingleRegistrationEnabledOverride(int subId, String enabledStr) {
11002 TelephonyPermissions.enforceShellOnly(Binder.getCallingUid(),
11003 "setCarrierSingleRegistrationEnabledOverride");
11004 enforceModifyPermission();
11005
11006 Boolean enabled = "NULL".equalsIgnoreCase(enabledStr) ? null
11007 : Boolean.parseBoolean(enabledStr);
11008 return RcsProvisioningMonitor.getInstance().overrideCarrierSingleRegistrationEnabled(
11009 subId, enabled);
11010 }
11011
11012 /**
11013 * Gets the config of RCS VoLTE single registration enabled for the carrier/subscription.
11014 */
11015 @Override
11016 public boolean getCarrierSingleRegistrationEnabled(int subId) {
11017 enforceReadPrivilegedPermission("getCarrierSingleRegistrationEnabled");
11018 return RcsProvisioningMonitor.getInstance().getCarrierSingleRegistrationEnabled(subId);
11019 }
Chiachang Wangd6d34772020-12-22 11:38:27 +080011020
11021 /**
Hui Wangb647abe2021-02-26 09:33:38 -080011022 * Overrides the ims feature validation result
11023 */
11024 @Override
11025 public boolean setImsFeatureValidationOverride(int subId, String enabledStr) {
11026 TelephonyPermissions.enforceShellOnly(Binder.getCallingUid(),
11027 "setImsFeatureValidationOverride");
11028
11029 Boolean enabled = "NULL".equalsIgnoreCase(enabledStr) ? null
11030 : Boolean.parseBoolean(enabledStr);
11031 return RcsProvisioningMonitor.getInstance().overrideImsFeatureValidation(
11032 subId, enabled);
11033 }
11034
11035 /**
11036 * Gets the ims feature validation override value
11037 */
11038 @Override
11039 public boolean getImsFeatureValidationOverride(int subId) {
11040 TelephonyPermissions.enforceShellOnly(Binder.getCallingUid(),
11041 "getImsFeatureValidationOverride");
11042 return RcsProvisioningMonitor.getInstance().getImsFeatureValidationOverride(subId);
11043 }
11044
11045 /**
Chiachang Wangd6d34772020-12-22 11:38:27 +080011046 * Get the mobile provisioning url that is used to launch a browser to allow users to manage
11047 * their mobile plan.
11048 */
11049 @Override
11050 public String getMobileProvisioningUrl() {
11051 enforceReadPrivilegedPermission("getMobileProvisioningUrl");
11052 final long identity = Binder.clearCallingIdentity();
11053 try {
11054 return getDefaultPhone().getMobileProvisioningUrl();
11055 } finally {
11056 Binder.restoreCallingIdentity(identity);
11057 }
11058 }
Rambo Wanga5cc9b72021-01-07 10:51:54 -080011059
James.cf Linbcdf8b32021-01-14 16:44:13 +080011060 /**
calvinpane4a8a1d2021-01-25 13:51:18 +080011061 * Get the EAB contact from the EAB database.
11062 */
11063 @Override
11064 public String getContactFromEab(String contact) {
11065 TelephonyPermissions.enforceShellOnly(Binder.getCallingUid(), "getContactFromEab");
11066 enforceModifyPermission();
11067 final long identity = Binder.clearCallingIdentity();
11068 try {
11069 return EabUtil.getContactFromEab(getDefaultPhone().getContext(), contact);
11070 } finally {
11071 Binder.restoreCallingIdentity(identity);
11072 }
11073 }
11074
11075 /**
Calvin Pana1434322021-07-01 19:27:01 +080011076 * Get the EAB capability from the EAB database.
11077 */
11078 @Override
11079 public String getCapabilityFromEab(String contact) {
11080 TelephonyPermissions.enforceShellOnly(Binder.getCallingUid(), "getCapabilityFromEab");
11081 enforceModifyPermission();
11082 final long identity = Binder.clearCallingIdentity();
11083 try {
11084 return EabUtil.getCapabilityFromEab(getDefaultPhone().getContext(), contact);
11085 } finally {
11086 Binder.restoreCallingIdentity(identity);
11087 }
11088 }
11089
11090 /**
James.cf Linbcdf8b32021-01-14 16:44:13 +080011091 * Remove the EAB contacts from the EAB database.
11092 */
11093 @Override
11094 public int removeContactFromEab(int subId, String contacts) {
11095 TelephonyPermissions.enforceShellOnly(Binder.getCallingUid(), "removeCapabilitiesFromEab");
11096 enforceModifyPermission();
11097 final long identity = Binder.clearCallingIdentity();
11098 try {
11099 return EabUtil.removeContactFromEab(subId, contacts, getDefaultPhone().getContext());
11100 } finally {
11101 Binder.restoreCallingIdentity(identity);
11102 }
11103 }
11104
Rambo Wanga5cc9b72021-01-07 10:51:54 -080011105 @Override
James.cf Lin4b784aa2021-01-31 03:25:15 +080011106 public boolean getDeviceUceEnabled() {
11107 TelephonyPermissions.enforceShellOnly(Binder.getCallingUid(), "getDeviceUceEnabled");
11108 final long identity = Binder.clearCallingIdentity();
11109 try {
11110 return mApp.getDeviceUceEnabled();
11111 } finally {
11112 Binder.restoreCallingIdentity(identity);
11113 }
11114 }
11115
11116 @Override
11117 public void setDeviceUceEnabled(boolean isEnabled) {
11118 TelephonyPermissions.enforceShellOnly(Binder.getCallingUid(), "setDeviceUceEnabled");
11119 final long identity = Binder.clearCallingIdentity();
11120 try {
11121 mApp.setDeviceUceEnabled(isEnabled);
11122 } finally {
11123 Binder.restoreCallingIdentity(identity);
11124 }
11125 }
11126
Brad Ebinger14d467f2021-02-12 06:18:28 +000011127 /**
11128 * Add new feature tags to the Set used to calculate the capabilities in PUBLISH.
11129 * @return current RcsContactUceCapability instance that will be used for PUBLISH.
11130 */
11131 // Used for SHELL command only right now.
11132 @Override
11133 public RcsContactUceCapability addUceRegistrationOverrideShell(int subId,
11134 List<String> featureTags) {
11135 TelephonyPermissions.enforceShellOnly(Binder.getCallingUid(),
11136 "addUceRegistrationOverrideShell");
11137 final long identity = Binder.clearCallingIdentity();
11138 try {
11139 return mApp.imsRcsController.addUceRegistrationOverrideShell(subId,
11140 new ArraySet<>(featureTags));
11141 } catch (ImsException e) {
11142 throw new ServiceSpecificException(e.getCode(), e.getMessage());
11143 } finally {
11144 Binder.restoreCallingIdentity(identity);
11145 }
11146 }
11147
11148 /**
11149 * Remove existing feature tags to the Set used to calculate the capabilities in PUBLISH.
11150 * @return current RcsContactUceCapability instance that will be used for PUBLISH.
11151 */
11152 // Used for SHELL command only right now.
11153 @Override
11154 public RcsContactUceCapability removeUceRegistrationOverrideShell(int subId,
11155 List<String> featureTags) {
11156 TelephonyPermissions.enforceShellOnly(Binder.getCallingUid(),
11157 "removeUceRegistrationOverrideShell");
11158 final long identity = Binder.clearCallingIdentity();
11159 try {
11160 return mApp.imsRcsController.removeUceRegistrationOverrideShell(subId,
11161 new ArraySet<>(featureTags));
11162 } catch (ImsException e) {
11163 throw new ServiceSpecificException(e.getCode(), e.getMessage());
11164 } finally {
11165 Binder.restoreCallingIdentity(identity);
11166 }
11167 }
11168
11169 /**
11170 * Clear all overrides in the Set used to calculate the capabilities in PUBLISH.
11171 * @return current RcsContactUceCapability instance that will be used for PUBLISH.
11172 */
11173 // Used for SHELL command only right now.
11174 @Override
11175 public RcsContactUceCapability clearUceRegistrationOverrideShell(int subId) {
11176 TelephonyPermissions.enforceShellOnly(Binder.getCallingUid(),
11177 "clearUceRegistrationOverrideShell");
11178 final long identity = Binder.clearCallingIdentity();
11179 try {
11180 return mApp.imsRcsController.clearUceRegistrationOverrideShell(subId);
11181 } catch (ImsException e) {
11182 throw new ServiceSpecificException(e.getCode(), e.getMessage());
11183 } finally {
11184 Binder.restoreCallingIdentity(identity);
11185 }
11186 }
11187
11188 /**
11189 * @return current RcsContactUceCapability instance that will be used for PUBLISH.
11190 */
11191 // Used for SHELL command only right now.
11192 @Override
11193 public RcsContactUceCapability getLatestRcsContactUceCapabilityShell(int subId) {
11194 TelephonyPermissions.enforceShellOnly(Binder.getCallingUid(),
11195 "getLatestRcsContactUceCapabilityShell");
11196 final long identity = Binder.clearCallingIdentity();
11197 try {
11198 return mApp.imsRcsController.getLatestRcsContactUceCapabilityShell(subId);
11199 } catch (ImsException e) {
11200 throw new ServiceSpecificException(e.getCode(), e.getMessage());
11201 } finally {
11202 Binder.restoreCallingIdentity(identity);
11203 }
11204 }
11205
11206 /**
11207 * Returns the last PIDF XML sent to the network during the last PUBLISH or "none" if the
11208 * device does not have an active PUBLISH.
11209 */
11210 // Used for SHELL command only right now.
11211 @Override
11212 public String getLastUcePidfXmlShell(int subId) {
11213 TelephonyPermissions.enforceShellOnly(Binder.getCallingUid(), "uceGetLastPidfXml");
11214 final long identity = Binder.clearCallingIdentity();
11215 try {
11216 return mApp.imsRcsController.getLastUcePidfXmlShell(subId);
11217 } catch (ImsException e) {
11218 throw new ServiceSpecificException(e.getCode(), e.getMessage());
11219 } finally {
11220 Binder.restoreCallingIdentity(identity);
11221 }
11222 }
11223
James.cf Line8713a42021-04-29 16:04:26 +080011224 /**
11225 * Remove UCE requests cannot be sent to the network status.
11226 */
11227 // Used for SHELL command only right now.
11228 @Override
11229 public boolean removeUceRequestDisallowedStatus(int subId) {
11230 TelephonyPermissions.enforceShellOnly(Binder.getCallingUid(), "uceRemoveDisallowedStatus");
11231 final long identity = Binder.clearCallingIdentity();
11232 try {
11233 return mApp.imsRcsController.removeUceRequestDisallowedStatus(subId);
11234 } catch (ImsException e) {
11235 throw new ServiceSpecificException(e.getCode(), e.getMessage());
11236 } finally {
11237 Binder.restoreCallingIdentity(identity);
11238 }
11239 }
11240
James.cf Lin18bb9002021-05-25 01:37:38 +080011241 /**
11242 * Remove UCE requests cannot be sent to the network status.
11243 */
11244 // Used for SHELL command only.
11245 @Override
11246 public boolean setCapabilitiesRequestTimeout(int subId, long timeoutAfterMs) {
11247 TelephonyPermissions.enforceShellOnly(Binder.getCallingUid(), "setCapRequestTimeout");
11248 final long identity = Binder.clearCallingIdentity();
11249 try {
11250 return mApp.imsRcsController.setCapabilitiesRequestTimeout(subId, timeoutAfterMs);
11251 } catch (ImsException e) {
11252 throw new ServiceSpecificException(e.getCode(), e.getMessage());
11253 } finally {
11254 Binder.restoreCallingIdentity(identity);
11255 }
11256 }
Brad Ebinger14d467f2021-02-12 06:18:28 +000011257
James.cf Lin4b784aa2021-01-31 03:25:15 +080011258 @Override
Rambo Wanga5cc9b72021-01-07 10:51:54 -080011259 public void setSignalStrengthUpdateRequest(int subId, SignalStrengthUpdateRequest request,
11260 String callingPackage) {
11261 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
11262 mApp, subId, "setSignalStrengthUpdateRequest");
11263
11264 final int callingUid = Binder.getCallingUid();
11265 // Verify that tha callingPackage belongs to the calling UID
11266 mApp.getSystemService(AppOpsManager.class)
11267 .checkPackage(callingUid, callingPackage);
11268
Rambo Wang3607f502021-02-01 21:51:40 -080011269 validateSignalStrengthUpdateRequest(mApp, request, callingUid);
Rambo Wanga5cc9b72021-01-07 10:51:54 -080011270
11271 final long identity = Binder.clearCallingIdentity();
11272 try {
11273 Object result = sendRequest(CMD_SET_SIGNAL_STRENGTH_UPDATE_REQUEST,
11274 new Pair<Integer, SignalStrengthUpdateRequest>(callingUid, request), subId);
11275
11276 if (result instanceof IllegalStateException) {
11277 throw (IllegalStateException) result;
11278 }
11279 } finally {
11280 Binder.restoreCallingIdentity(identity);
11281 }
11282 }
11283
11284 @Override
11285 public void clearSignalStrengthUpdateRequest(int subId, SignalStrengthUpdateRequest request,
11286 String callingPackage) {
11287 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
11288 mApp, subId, "clearSignalStrengthUpdateRequest");
11289
11290 final int callingUid = Binder.getCallingUid();
11291 // Verify that tha callingPackage belongs to the calling UID
11292 mApp.getSystemService(AppOpsManager.class)
11293 .checkPackage(callingUid, callingPackage);
11294
11295 final long identity = Binder.clearCallingIdentity();
11296 try {
11297 Object result = sendRequest(CMD_CLEAR_SIGNAL_STRENGTH_UPDATE_REQUEST,
11298 new Pair<Integer, SignalStrengthUpdateRequest>(callingUid, request), subId);
11299
11300 if (result instanceof IllegalStateException) {
11301 throw (IllegalStateException) result;
11302 }
11303 } finally {
11304 Binder.restoreCallingIdentity(identity);
11305 }
11306 }
11307
Rambo Wang3607f502021-02-01 21:51:40 -080011308 private static void validateSignalStrengthUpdateRequest(Context context,
11309 SignalStrengthUpdateRequest request, int callingUid) {
Rambo Wanga5cc9b72021-01-07 10:51:54 -080011310 if (callingUid == Process.PHONE_UID || callingUid == Process.SYSTEM_UID) {
11311 // phone/system process do not have further restriction on request
11312 return;
11313 }
11314
11315 // Applications has restrictions on how to use the request:
Rambo Wang3607f502021-02-01 21:51:40 -080011316 // Non-system callers need permission to set mIsSystemThresholdReportingRequestedWhileIdle
Rambo Wanga5cc9b72021-01-07 10:51:54 -080011317 if (request.isSystemThresholdReportingRequestedWhileIdle()) {
Rambo Wang3607f502021-02-01 21:51:40 -080011318 context.enforceCallingOrSelfPermission(
11319 android.Manifest.permission.LISTEN_ALWAYS_REPORTED_SIGNAL_STRENGTH,
11320 "validateSignalStrengthUpdateRequest");
Rambo Wanga5cc9b72021-01-07 10:51:54 -080011321 }
11322
11323 for (SignalThresholdInfo info : request.getSignalThresholdInfos()) {
Nagendra Prasad Nagarle Basavarajufee544c2022-12-07 16:34:52 +000011324 // Only system caller can set mHysteresisMs/mIsEnabled.
Rambo Wanga5cc9b72021-01-07 10:51:54 -080011325 if (info.getHysteresisMs() != SignalThresholdInfo.HYSTERESIS_MS_DISABLED
Rambo Wanga5cc9b72021-01-07 10:51:54 -080011326 || info.isEnabled()) {
11327 throw new IllegalArgumentException(
11328 "Only system can set hide fields in SignalThresholdInfo");
11329 }
11330
11331 // Thresholds length for each RAN need in range. This has been validated in
11332 // SignalThresholdInfo#Builder#setThreshold. Here we prevent apps calling hide method
11333 // setThresholdUnlimited (e.g. through reflection) with too short or too long thresholds
11334 final int[] thresholds = info.getThresholds();
11335 Objects.requireNonNull(thresholds);
11336 if (thresholds.length < SignalThresholdInfo.getMinimumNumberOfThresholdsAllowed()
11337 || thresholds.length
11338 > SignalThresholdInfo.getMaximumNumberOfThresholdsAllowed()) {
11339 throw new IllegalArgumentException(
11340 "thresholds length is out of range: " + thresholds.length);
11341 }
11342 }
11343 }
SongFerngWang8236caa2021-01-17 21:51:44 +080011344
11345 /**
11346 * Gets the current phone capability.
11347 *
11348 * Requires carrier privileges or READ_PRECISE_PHONE_STATE permission.
11349 * @return the PhoneCapability which describes the data connection capability of modem.
11350 * It's used to evaluate possible phone config change, for example from single
11351 * SIM device to multi-SIM device.
11352 */
11353 @Override
11354 public PhoneCapability getPhoneCapability() {
11355 enforceReadPrivilegedPermission("getPhoneCapability");
11356 final long identity = Binder.clearCallingIdentity();
11357 try {
11358 return mPhoneConfigurationManager.getCurrentPhoneCapability();
11359 } finally {
11360 Binder.restoreCallingIdentity(identity);
11361 }
11362 }
Michele Berionne5e411512020-11-13 02:36:59 +000011363
11364 /**
11365 * Prepare TelephonyManager for an unattended reboot. The reboot is
11366 * required to be done shortly after the API is invoked.
11367 */
11368 @Override
11369 @TelephonyManager.PrepareUnattendedRebootResult
11370 public int prepareForUnattendedReboot() {
Rafael Higuera Silvad9630642021-09-20 15:32:01 +000011371 WorkSource workSource = getWorkSource(Binder.getCallingUid());
Michele Berionne5e411512020-11-13 02:36:59 +000011372 enforceRebootPermission();
11373
11374 final long identity = Binder.clearCallingIdentity();
11375 try {
Rafael Higuera Silvad9630642021-09-20 15:32:01 +000011376 return (int) sendRequest(CMD_PREPARE_UNATTENDED_REBOOT, null, workSource);
Michele Berionne5e411512020-11-13 02:36:59 +000011377 } finally {
11378 Binder.restoreCallingIdentity(identity);
11379 }
11380 }
Hongbo Zeng156aa4a2021-02-08 21:50:28 +080011381
11382 /**
11383 * Request to get the current slicing configuration including URSP rules and
11384 * NSSAIs (configured, allowed and rejected).
11385 *
11386 * Requires carrier privileges or READ_PRIVILEGED_PHONE_STATE permission.
11387 */
11388 @Override
11389 public void getSlicingConfig(ResultReceiver callback) {
Hongbo Zeng1b2063d2022-02-21 01:33:03 +000011390 TelephonyPermissions
11391 .enforceCallingOrSelfReadPrivilegedPhoneStatePermissionOrCarrierPrivilege(
11392 mApp, SubscriptionManager.INVALID_SUBSCRIPTION_ID, "getSlicingConfig");
Hongbo Zeng156aa4a2021-02-08 21:50:28 +080011393
11394 final long identity = Binder.clearCallingIdentity();
11395 try {
11396 Phone phone = getDefaultPhone();
11397 sendRequestAsync(CMD_GET_SLICING_CONFIG, callback, phone, null);
11398 } finally {
11399 Binder.restoreCallingIdentity(identity);
11400 }
11401 }
Hunsuk Choi3b742d62021-10-25 19:48:34 +000011402
11403 /**
Sarah Chin2ec39f62022-08-31 17:03:26 -070011404 * Check whether the given premium capability is available for purchase from the carrier.
11405 *
11406 * @param capability The premium capability to check.
11407 * @param subId The subId to check the premium capability for.
11408 *
11409 * @return Whether the given premium capability is available to purchase.
11410 */
11411 @Override
11412 public boolean isPremiumCapabilityAvailableForPurchase(int capability, int subId) {
11413 if (!TelephonyPermissions.checkCallingOrSelfReadNonDangerousPhoneStateNoThrow(
11414 mApp, "isPremiumCapabilityAvailableForPurchase")) {
11415 log("Premium capability "
11416 + TelephonyManager.convertPremiumCapabilityToString(capability)
11417 + " is not available for purchase due to missing permissions.");
11418 throw new SecurityException("isPremiumCapabilityAvailableForPurchase requires "
11419 + "permission READ_BASIC_PHONE_STATE.");
11420 }
11421
11422 Phone phone = getPhone(subId);
Thomas Nguyen7216ed62022-11-29 16:45:31 -080011423 if (phone == null) {
11424 loge("isPremiumCapabilityAvailableForPurchase: phone is null, subId=" + subId);
11425 return false;
11426 }
Sarah Chin2ec39f62022-08-31 17:03:26 -070011427 final long identity = Binder.clearCallingIdentity();
11428 try {
Sarah Chin46355ba2022-11-01 23:51:16 -070011429 return SlicePurchaseController.getInstance(phone)
Sarah Chin2ec39f62022-08-31 17:03:26 -070011430 .isPremiumCapabilityAvailableForPurchase(capability);
11431 } finally {
11432 Binder.restoreCallingIdentity(identity);
11433 }
11434 }
11435
11436 /**
11437 * Purchase the given premium capability from the carrier.
11438 *
11439 * @param capability The premium capability to purchase.
11440 * @param callback The result of the purchase request.
11441 * @param subId The subId to purchase the premium capability for.
11442 */
11443 @Override
11444 public void purchasePremiumCapability(int capability, IIntegerConsumer callback, int subId) {
11445 log("purchasePremiumCapability: capability="
11446 + TelephonyManager.convertPremiumCapabilityToString(capability) + ", caller="
11447 + getCurrentPackageName());
11448
11449 if (!TelephonyPermissions.checkCallingOrSelfReadNonDangerousPhoneStateNoThrow(
11450 mApp, "purchasePremiumCapability")) {
11451 log("purchasePremiumCapability "
11452 + TelephonyManager.convertPremiumCapabilityToString(capability)
11453 + " failed due to missing permissions.");
11454 throw new SecurityException("purchasePremiumCapability requires permission "
11455 + "READ_BASIC_PHONE_STATE.");
Sarah Chin532d6bb2022-12-28 22:50:43 -080011456 } else if (!TelephonyPermissions.checkInternetPermissionNoThrow(
11457 mApp, "purchasePremiumCapability")) {
11458 log("purchasePremiumCapability "
11459 + TelephonyManager.convertPremiumCapabilityToString(capability)
11460 + " failed due to missing permissions.");
11461 throw new SecurityException("purchasePremiumCapability requires permission INTERNET.");
Sarah Chin2ec39f62022-08-31 17:03:26 -070011462 }
11463
11464 Phone phone = getPhone(subId);
Sarah Chin19694112022-12-06 15:41:37 -080011465 if (phone == null) {
11466 try {
11467 int result = TelephonyManager.PURCHASE_PREMIUM_CAPABILITY_RESULT_REQUEST_FAILED;
11468 callback.accept(result);
11469 loge("purchasePremiumCapability: phone is null, subId=" + subId);
11470 } catch (RemoteException e) {
11471 String logStr = "Purchase premium capability "
11472 + TelephonyManager.convertPremiumCapabilityToString(capability)
11473 + " failed due to RemoteException handling null phone: " + e;
11474 if (DBG) log(logStr);
11475 AnomalyReporter.reportAnomaly(
11476 UUID.fromString(PURCHASE_PREMIUM_CAPABILITY_ERROR_UUID), logStr);
11477 }
11478 return;
11479 }
Sarah Chin532d6bb2022-12-28 22:50:43 -080011480
11481 String callingProcess;
Sarah Chin71b3a852022-09-28 15:54:19 -070011482 try {
Sarah Chin532d6bb2022-12-28 22:50:43 -080011483 callingProcess = mApp.getPackageManager().getApplicationInfo(
11484 getCurrentPackageName(), 0).processName;
Sarah Chin71b3a852022-09-28 15:54:19 -070011485 } catch (PackageManager.NameNotFoundException e) {
Sarah Chin532d6bb2022-12-28 22:50:43 -080011486 callingProcess = getCurrentPackageName();
Sarah Chin71b3a852022-09-28 15:54:19 -070011487 }
Sarah Chin532d6bb2022-12-28 22:50:43 -080011488
11489 boolean isVisible = false;
11490 ActivityManager am = mApp.getSystemService(ActivityManager.class);
11491 if (am != null) {
11492 List<ActivityManager.RunningAppProcessInfo> processes = am.getRunningAppProcesses();
11493 if (processes != null) {
11494 for (ActivityManager.RunningAppProcessInfo process : processes) {
11495 log("purchasePremiumCapability: process " + process.processName
11496 + "has importance " + process.importance);
11497 if (process.processName.equals(callingProcess) && process.importance
11498 <= ActivityManager.RunningAppProcessInfo.IMPORTANCE_VISIBLE) {
11499 isVisible = true;
11500 break;
11501 }
11502 }
11503 }
11504 }
11505
11506 if (!isVisible) {
11507 try {
11508 int result = TelephonyManager.PURCHASE_PREMIUM_CAPABILITY_RESULT_NOT_FOREGROUND;
11509 callback.accept(result);
11510 loge("purchasePremiumCapability: " + callingProcess + " is not in the foreground.");
11511 } catch (RemoteException e) {
11512 String logStr = "Purchase premium capability "
11513 + TelephonyManager.convertPremiumCapabilityToString(capability)
11514 + " failed due to RemoteException handling background application: " + e;
11515 if (DBG) log(logStr);
11516 AnomalyReporter.reportAnomaly(
11517 UUID.fromString(PURCHASE_PREMIUM_CAPABILITY_ERROR_UUID), logStr);
11518 }
11519 return;
11520 }
11521
Sarah Chin71b3a852022-09-28 15:54:19 -070011522 sendRequestAsync(CMD_PURCHASE_PREMIUM_CAPABILITY,
Sarah Chinb8218c22023-01-04 13:35:29 -080011523 new PurchasePremiumCapabilityArgument(capability, callback), phone, null);
Sarah Chin2ec39f62022-08-31 17:03:26 -070011524 }
11525
11526 /**
Hunsuk Choi3b742d62021-10-25 19:48:34 +000011527 * Register an IMS connection state callback
11528 */
11529 @Override
Hunsuk Choi89bd22c2021-11-01 13:04:54 +000011530 public void registerImsStateCallback(int subId, int feature, IImsStateCallback cb,
11531 String callingPackage) {
Hunsuk Choi3b742d62021-10-25 19:48:34 +000011532 if (feature == ImsFeature.FEATURE_MMTEL) {
11533 // ImsMmTelManager
11534 // The following also checks READ_PRIVILEGED_PHONE_STATE.
11535 TelephonyPermissions
11536 .enforceCallingOrSelfReadPrecisePhoneStatePermissionOrCarrierPrivilege(
11537 mApp, subId, "registerImsStateCallback");
11538 } else if (feature == ImsFeature.FEATURE_RCS) {
11539 // ImsRcsManager or SipDelegateManager
11540 TelephonyPermissions.enforceAnyPermissionGrantedOrCarrierPrivileges(mApp, subId,
11541 Binder.getCallingUid(), "registerImsStateCallback",
11542 Manifest.permission.READ_PRIVILEGED_PHONE_STATE,
11543 Manifest.permission.READ_PRECISE_PHONE_STATE,
11544 Manifest.permission.ACCESS_RCS_USER_CAPABILITY_EXCHANGE,
11545 Manifest.permission.PERFORM_IMS_SINGLE_REGISTRATION);
11546 }
11547
11548 if (!ImsManager.isImsSupportedOnDevice(mApp)) {
11549 throw new ServiceSpecificException(ImsException.CODE_ERROR_UNSUPPORTED_OPERATION,
11550 "IMS not available on device.");
11551 }
11552
11553 if (subId == SubscriptionManager.DEFAULT_SUBSCRIPTION_ID) {
11554 throw new ServiceSpecificException(ImsException.CODE_ERROR_INVALID_SUBSCRIPTION);
11555 }
11556
11557 ImsStateCallbackController controller = ImsStateCallbackController.getInstance();
11558 if (controller == null) {
11559 throw new ServiceSpecificException(ImsException.CODE_ERROR_UNSUPPORTED_OPERATION,
11560 "IMS not available on device.");
11561 }
11562
Hunsuk Choi89bd22c2021-11-01 13:04:54 +000011563 if (callingPackage == null) {
11564 callingPackage = getCurrentPackageName();
11565 }
11566
Hunsuk Choi3b742d62021-10-25 19:48:34 +000011567 final long token = Binder.clearCallingIdentity();
11568 try {
11569 int slotId = getSlotIndexOrException(subId);
Hunsuk Choi89bd22c2021-11-01 13:04:54 +000011570 controller.registerImsStateCallback(subId, feature, cb, callingPackage);
Hunsuk Choi3b742d62021-10-25 19:48:34 +000011571 } catch (ImsException e) {
11572 throw new ServiceSpecificException(e.getCode());
11573 } finally {
11574 Binder.restoreCallingIdentity(token);
11575 }
11576 }
11577
11578 /**
11579 * Unregister an IMS connection state callback
11580 */
11581 @Override
11582 public void unregisterImsStateCallback(IImsStateCallback cb) {
11583 final long token = Binder.clearCallingIdentity();
11584 ImsStateCallbackController controller = ImsStateCallbackController.getInstance();
11585 if (controller == null) {
11586 return;
11587 }
11588 try {
11589 controller.unregisterImsStateCallback(cb);
11590 } finally {
11591 Binder.restoreCallingIdentity(token);
11592 }
11593 }
Sooraj Sasindranfae41b32021-10-26 02:10:05 -070011594
11595 /**
11596 * @return {@CellIdentity} last known cell identity {@CellIdentity}.
11597 *
11598 * Require {@link android.Manifest.permission#ACCESS_FINE_LOCATION} and
11599 * com.android.phone.permission.ACCESS_LAST_KNOWN_CELL_ID, otherwise throws
11600 * SecurityException.
11601 * If there is current registered network this value will be same as the registered cell
11602 * identity. If the device goes out of service the previous cell identity is cached and
11603 * will be returned. If the cache age of the Cell identity is more than 24 hours
11604 * it will be cleared and null will be returned.
11605 *
11606 */
11607 @Override
11608 public @Nullable CellIdentity getLastKnownCellIdentity(int subId, String callingPackage,
11609 String callingFeatureId) {
11610 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
11611 LocationAccessPolicy.LocationPermissionResult fineLocationResult =
11612 LocationAccessPolicy.checkLocationPermission(mApp,
11613 new LocationAccessPolicy.LocationPermissionQuery.Builder()
11614 .setCallingPackage(callingPackage)
11615 .setCallingFeatureId(callingFeatureId)
11616 .setCallingPid(Binder.getCallingPid())
11617 .setCallingUid(Binder.getCallingUid())
11618 .setMethod("getLastKnownCellIdentity")
11619 .setLogAsInfo(true)
11620 .setMinSdkVersionForFine(Build.VERSION_CODES.Q)
11621 .setMinSdkVersionForCoarse(Build.VERSION_CODES.Q)
11622 .setMinSdkVersionForEnforcement(Build.VERSION_CODES.Q)
11623 .build());
11624
11625 boolean hasFinePermission =
11626 fineLocationResult == LocationAccessPolicy.LocationPermissionResult.ALLOWED;
11627 if (!hasFinePermission
11628 || !TelephonyPermissions.checkLastKnownCellIdAccessPermission(mApp)) {
11629 throw new SecurityException("getLastKnownCellIdentity need ACCESS_FINE_LOCATION "
Rambo Wang918993a2022-04-27 09:08:36 -070011630 + "and ACCESS_LAST_KNOWN_CELL_ID permission.");
Sooraj Sasindranfae41b32021-10-26 02:10:05 -070011631 }
11632
11633 final long identity = Binder.clearCallingIdentity();
11634 try {
11635 Phone phone = getPhone(subId);
11636 if (phone == null) return null;
11637 ServiceStateTracker sst = phone.getServiceStateTracker();
11638 if (sst == null) return null;
11639 return sst.getLastKnownCellIdentity();
11640 } finally {
11641 Binder.restoreCallingIdentity(identity);
11642 }
11643 }
Jack Yu4c0a5502021-12-03 23:58:26 -080011644
jimsun3b9ccac2021-10-26 15:01:23 +080011645 /**
11646 * Sets the modem service class Name that Telephony will bind to.
11647 *
11648 * @param serviceName The class name of the modem service.
11649 * @return true if the operation is succeed, otherwise false.
11650 */
11651 public boolean setModemService(String serviceName) {
11652 Log.d(LOG_TAG, "setModemService - " + serviceName);
11653 TelephonyPermissions.enforceShellOnly(Binder.getCallingUid(), "setModemService");
11654 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(mApp,
11655 SubscriptionManager.INVALID_SUBSCRIPTION_ID,
11656 "setModemService");
11657 return mPhoneConfigurationManager.setModemService(serviceName);
11658 }
11659
11660 /**
11661 * Return the class name of the currently bounded modem service.
11662 *
11663 * @return the class name of the modem service.
11664 */
11665 public String getModemService() {
11666 String result;
11667 Log.d(LOG_TAG, "getModemService");
11668 TelephonyPermissions.enforceShellOnly(Binder.getCallingUid(), "getModemService");
11669 TelephonyPermissions
11670 .enforceCallingOrSelfReadPrivilegedPhoneStatePermissionOrCarrierPrivilege(
11671 mApp, SubscriptionManager.INVALID_SUBSCRIPTION_ID,
11672 "getModemService");
11673 result = mPhoneConfigurationManager.getModemService();
11674 Log.d(LOG_TAG, "result = " + result);
11675 return result;
11676 }
Hunter Knepshield2b076fa2022-01-19 02:26:22 -080011677
11678 @Override
11679 public void setVoiceServiceStateOverride(int subId, boolean hasService, String callingPackage) {
11680 // Only telecom (and shell, for CTS purposes) is allowed to call this method.
11681 mApp.enforceCallingOrSelfPermission(
11682 permission.BIND_TELECOM_CONNECTION_SERVICE, "setVoiceServiceStateOverride");
11683 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
11684
11685 final long identity = Binder.clearCallingIdentity();
11686 try {
11687 Phone phone = getPhone(subId);
11688 if (phone == null) return;
11689 phone.setVoiceServiceStateOverride(hasService);
11690 } finally {
11691 Binder.restoreCallingIdentity(identity);
11692 }
11693 }
Muralidhar Reddy4e5a8012022-05-11 14:49:00 +000011694
11695 /**
11696 * set removable eSIM as default eUICC.
11697 *
11698 * @hide
11699 */
11700 @Override
11701 public void setRemovableEsimAsDefaultEuicc(boolean isDefault, String callingPackage) {
11702 enforceModifyPermission();
11703 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
11704
11705 final long identity = Binder.clearCallingIdentity();
11706 try {
11707 UiccController.getInstance().setRemovableEsimAsDefaultEuicc(isDefault);
11708 } finally {
11709 Binder.restoreCallingIdentity(identity);
11710 }
11711 }
11712
11713 /**
11714 * Returns whether the removable eSIM is default eUICC or not.
11715 *
11716 * @hide
11717 */
11718 @Override
11719 public boolean isRemovableEsimDefaultEuicc(String callingPackage) {
11720 enforceReadPrivilegedPermission("isRemovableEsimDefaultEuicc");
11721 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
11722
11723 final long identity = Binder.clearCallingIdentity();
11724 try {
11725 return UiccController.getInstance().isRemovableEsimDefaultEuicc();
11726 } finally {
11727 Binder.restoreCallingIdentity(identity);
11728 }
11729 }
11730
Aishwarya Mallampatifbc70d32022-11-10 20:33:02 +000011731 /**
11732 * Get the component name of the default app to direct respond-via-message intent for the
11733 * user associated with this subscription, update the cache if there is no respond-via-message
11734 * application currently configured for this user.
11735 * @return component name of the app and class to direct Respond Via Message intent to, or
11736 * {@code null} if the functionality is not supported.
11737 * @hide
11738 */
11739 @Override
11740 public @Nullable ComponentName getDefaultRespondViaMessageApplication(int subId,
11741 boolean updateIfNeeded) {
11742 enforceInteractAcrossUsersPermission("getDefaultRespondViaMessageApplication");
Muralidhar Reddy4e5a8012022-05-11 14:49:00 +000011743
Aishwarya Mallampatifbc70d32022-11-10 20:33:02 +000011744 Context context = getPhone(subId).getContext();
11745 UserHandle userHandle = null;
11746 final long identity = Binder.clearCallingIdentity();
11747 try {
11748 userHandle = TelephonyUtils.getSubscriptionUserHandle(context, subId);
11749 } finally {
11750 Binder.restoreCallingIdentity(identity);
11751 }
11752 return SmsApplication.getDefaultRespondViaMessageApplicationAsUser(context,
11753 updateIfNeeded, userHandle);
11754 }
Jack Yuf5badd92022-12-08 00:50:53 -080011755
11756 /**
Gil Cukierman1c0eb932022-12-06 22:28:24 +000011757 * Set whether the device is able to connect with null ciphering or integrity
11758 * algorithms. This is a global setting and will apply to all active subscriptions
11759 * and all new subscriptions after this.
11760 *
11761 * @param enabled when true, null cipher and integrity algorithms are allowed.
11762 * @hide
11763 */
11764 @Override
11765 @RequiresPermission(android.Manifest.permission.MODIFY_PHONE_STATE)
11766 public void setNullCipherAndIntegrityEnabled(boolean enabled) {
11767 enforceModifyPermission();
11768 checkForNullCipherAndIntegritySupport();
11769
11770 // Persist the state of our preference. Each GsmCdmaPhone instance is responsible
11771 // for listening to these preference changes and applying them immediately.
11772 SharedPreferences.Editor editor = mTelephonySharedPreferences.edit();
11773 editor.putBoolean(Phone.PREF_NULL_CIPHER_AND_INTEGRITY_ENABLED, enabled);
11774 editor.apply();
11775
11776 for (Phone phone: PhoneFactory.getPhones()) {
11777 phone.handleNullCipherEnabledChange();
11778 }
11779 }
11780
11781
11782 /**
11783 * Get whether the device is able to connect with null ciphering or integrity
11784 * algorithms. Note that this retrieves the phone-global preference and not
11785 * the state of the radio.
11786 *
11787 * @throws SecurityException if {@link permission#MODIFY_PHONE_STATE} is not satisfied
11788 * @throws UnsupportedOperationException if the device does not support the minimum HAL
11789 * version for this feature.
11790 * @hide
11791 */
11792 @Override
11793 @RequiresPermission(android.Manifest.permission.READ_PHONE_STATE)
11794 public boolean isNullCipherAndIntegrityPreferenceEnabled() {
11795 enforceReadPermission();
11796 checkForNullCipherAndIntegritySupport();
11797 return getDefaultPhone().getNullCipherAndIntegrityEnabledPreference();
11798 }
11799
11800 private void checkForNullCipherAndIntegritySupport() {
11801 if (getHalVersion(HAL_SERVICE_NETWORK) < MIN_NULL_CIPHER_AND_INTEGRITY_VERSION) {
11802 throw new UnsupportedOperationException(
11803 "Null cipher and integrity operations require HAL 2.1 or above");
11804 }
Gil Cukierman92cc7db2023-01-06 19:25:53 +000011805 if (!getDefaultPhone().isNullCipherAndIntegritySupported()) {
11806 throw new UnsupportedOperationException(
11807 "Null cipher and integrity operations unsupported by modem");
11808 }
Gil Cukierman1c0eb932022-12-06 22:28:24 +000011809 }
11810
11811 /**
Jack Yuf5badd92022-12-08 00:50:53 -080011812 * Get the SIM state for the slot index.
11813 * For Remote-SIMs, this method returns {@link IccCardConstants.State#UNKNOWN}
11814 *
11815 * @return SIM state as the ordinal of {@link IccCardConstants.State}
11816 */
11817 @Override
11818 @SimState
11819 public int getSimStateForSlotIndex(int slotIndex) {
11820 IccCardConstants.State simState;
11821 if (slotIndex < 0) {
11822 simState = IccCardConstants.State.UNKNOWN;
11823 } else {
11824 Phone phone = null;
11825 try {
11826 phone = PhoneFactory.getPhone(slotIndex);
11827 } catch (IllegalStateException e) {
11828 // ignore
11829 }
11830 if (phone == null) {
11831 simState = IccCardConstants.State.UNKNOWN;
11832 } else {
11833 IccCard icc = phone.getIccCard();
11834 if (icc == null) {
11835 simState = IccCardConstants.State.UNKNOWN;
11836 } else {
11837 simState = icc.getState();
11838 }
11839 }
11840 }
11841 return simState.ordinal();
11842 }
Hui Wang9b5793a2022-12-05 14:38:06 -060011843
11844 /**
11845 * Get current cell broadcast ranges.
11846 */
11847 @Override
11848 @RequiresPermission(android.Manifest.permission.MODIFY_CELL_BROADCASTS)
11849 public List<CellBroadcastIdRange> getCellBroadcastIdRanges(int subId) {
11850 mApp.enforceCallingPermission(android.Manifest.permission.MODIFY_CELL_BROADCASTS,
11851 "getCellBroadcastIdRanges");
11852 final long identity = Binder.clearCallingIdentity();
11853 try {
11854 return getPhone(subId).getCellBroadcastIdRanges();
11855 } finally {
11856 Binder.restoreCallingIdentity(identity);
11857 }
11858 }
11859
11860 /**
11861 * Set reception of cell broadcast messages with the list of the given ranges
11862 *
11863 * @param ranges the list of {@link CellBroadcastIdRange} to be enabled
11864 */
11865 @Override
11866 @RequiresPermission(android.Manifest.permission.MODIFY_CELL_BROADCASTS)
11867 public void setCellBroadcastIdRanges(int subId, @NonNull List<CellBroadcastIdRange> ranges,
11868 @Nullable IIntegerConsumer callback) {
11869 mApp.enforceCallingPermission(android.Manifest.permission.MODIFY_CELL_BROADCASTS,
11870 "setCellBroadcastIdRanges");
11871 final long identity = Binder.clearCallingIdentity();
11872 try {
11873 Phone phone = getPhoneFromSubId(subId);
11874 if (DBG) {
11875 log("setCellBroadcastIdRanges for subId :" + subId + ", phone:" + phone);
11876 }
11877 phone.setCellBroadcastIdRanges(ranges, result -> {
11878 if (callback != null) {
11879 try {
11880 callback.accept(result);
11881 } catch (RemoteException e) {
11882 Log.w(LOG_TAG, "setCellBroadcastIdRanges: callback not available.");
11883 }
11884 }
11885 });
11886 } finally {
11887 Binder.restoreCallingIdentity(identity);
11888 }
11889 }
Hunsuk Choi42cc62a2022-10-16 06:03:40 +000011890
11891 /**
11892 * Returns whether the device supports the domain selection service.
11893 *
11894 * @return {@code true} if the device supports the domain selection service.
11895 */
11896 @Override
11897 public boolean isDomainSelectionSupported() {
11898 mApp.enforceCallingOrSelfPermission(Manifest.permission.READ_PRIVILEGED_PHONE_STATE,
11899 "isDomainSelectionSupported");
11900
11901 final long identity = Binder.clearCallingIdentity();
11902 try {
11903 return DomainSelectionResolver.getInstance().isDomainSelectionSupported();
11904 } finally {
11905 Binder.restoreCallingIdentity(identity);
11906 }
11907 }
arunvoddud5c6ce02022-12-11 06:03:12 +000011908
11909 /**
11910 * Check whether the caller (or self, if not processing an IPC) can read device identifiers.
11911 *
11912 * <p>This method behaves in one of the following ways:
11913 * <ul>
11914 * <li>return true : if the calling package has the appop permission {@link
11915 * Manifest.permission#USE_ICC_AUTH_WITH_DEVICE_IDENTIFIER} in the manifest </>
11916 * <li>return true : if any one subscription has the READ_PRIVILEGED_PHONE_STATE
11917 * permission, the calling package passes a DevicePolicyManager Device Owner / Profile
11918 * Owner device identifier access check, or the calling package has carrier privileges</>
11919 * <li>throw SecurityException: if the caller does not meet any of the requirements.
11920 * </ul>
11921 */
11922 private static boolean checkCallingOrSelfReadDeviceIdentifiersForAnySub(Context context,
11923 String callingPackage, @Nullable String callingFeatureId, String message) {
11924 for (Phone phone : PhoneFactory.getPhones()) {
11925 if (TelephonyPermissions.checkCallingOrSelfReadDeviceIdentifiers(context,
11926 phone.getSubId(), callingPackage, callingFeatureId, message)) {
11927 return true;
11928 }
11929 }
11930 return false;
11931 }
11932}