blob: d754d4c7e383b6cd3070de96ce15d7139de4656e [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;
arunvoddud7401012022-12-15 16:08:12 +000075import android.service.carrier.CarrierIdentifier;
Inseob Kim14bb3d02018-12-13 17:11:34 +090076import android.sysprop.TelephonyProperties;
Santos Cordon7a1885b2015-02-03 11:15:19 -080077import android.telecom.PhoneAccount;
Nancy Chen31f9ba12016-01-06 11:42:12 -080078import android.telecom.PhoneAccountHandle;
Andrew Lee9431b832015-03-09 18:46:45 -070079import android.telecom.TelecomManager;
Gary Jian3aa9a762022-01-24 16:41:19 +080080import android.telephony.AccessNetworkConstants;
81import android.telephony.ActivityStatsTechSpecificInfo;
Chen Xu227e06f2019-09-26 22:48:11 -070082import android.telephony.Annotation.ApnType;
Jack Yu0eda6842022-04-18 00:34:46 -070083import android.telephony.Annotation.DataActivityType;
Jack Nudelmanb0b87642020-11-12 15:04:39 -080084import android.telephony.Annotation.ThermalMitigationResult;
Sarah Chin2ec39f62022-08-31 17:03:26 -070085import android.telephony.AnomalyReporter;
Shuo Qian4a594052020-01-23 11:59:30 -080086import android.telephony.CallForwardingInfo;
Junda Liu12f7d802015-05-01 12:06:44 -070087import android.telephony.CarrierConfigManager;
Michele Berionne482f8202018-11-27 18:57:59 -080088import android.telephony.CarrierRestrictionRules;
Hui Wang9b5793a2022-12-05 14:38:06 -060089import android.telephony.CellBroadcastIdRange;
yincheng zhao2737e882019-09-06 17:06:54 -070090import android.telephony.CellIdentity;
Meng Wanga10e89e2019-12-09 13:13:01 -080091import android.telephony.CellIdentityCdma;
92import android.telephony.CellIdentityGsm;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070093import android.telephony.CellInfo;
Nathan Haroldf180aac2018-06-01 18:43:55 -070094import android.telephony.CellInfoGsm;
95import android.telephony.CellInfoWcdma;
Sooraj Sasindran9a909312016-07-18 11:57:25 -070096import android.telephony.ClientRequestStats;
Jack Nudelmanb0b87642020-11-12 15:04:39 -080097import android.telephony.DataThrottlingRequest;
Hui Wang641e81c2020-10-12 12:14:23 -070098import android.telephony.IBootstrapAuthenticationCallback;
Nathan Haroldfa8da0f2018-09-27 18:51:29 -070099import android.telephony.ICellInfoCallback;
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700100import android.telephony.IccOpenLogicalChannelResponse;
Hall Liu1aa510f2017-11-22 17:40:08 -0800101import android.telephony.LocationAccessPolicy;
Ta-wei Yen87c49842016-05-13 21:19:52 -0700102import android.telephony.ModemActivityInfo;
Jake Hambye994d462014-02-03 13:10:13 -0800103import android.telephony.NeighboringCellInfo;
yinxu504e1392017-04-12 16:03:22 -0700104import android.telephony.NetworkScanRequest;
Michele4245e952019-02-04 11:36:23 -0800105import android.telephony.PhoneCapability;
Hall Liud892bec2018-11-30 14:51:45 -0800106import android.telephony.PhoneNumberRange;
Wink Saville5d475dd2014-10-17 15:00:58 -0700107import android.telephony.RadioAccessFamily;
Hall Liub2ac8ef2019-02-28 15:56:23 -0800108import android.telephony.RadioAccessSpecifier;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700109import android.telephony.ServiceState;
Nathan Harold46b42aa2017-03-10 19:38:22 -0800110import android.telephony.SignalStrength;
Rambo Wanga5cc9b72021-01-07 10:51:54 -0800111import android.telephony.SignalStrengthUpdateRequest;
112import android.telephony.SignalThresholdInfo;
Wink Saville0f3b5fc2014-11-11 08:40:49 -0800113import android.telephony.SubscriptionInfo;
Jeff Sharkey85190e62014-12-05 09:40:12 -0800114import android.telephony.SubscriptionManager;
Peter Wangc035ce42020-01-08 21:00:22 -0800115import android.telephony.TelephonyFrameworkInitializer;
Sanket Padawe99ef1e32016-05-18 16:12:33 -0700116import android.telephony.TelephonyHistogram;
Ta-wei Yenb6929602016-05-24 15:48:27 -0700117import android.telephony.TelephonyManager;
Jack Yuf5badd92022-12-08 00:50:53 -0800118import android.telephony.TelephonyManager.SimState;
Hall Liub2ac8ef2019-02-28 15:56:23 -0800119import android.telephony.TelephonyScanManager;
Jack Nudelmanb0b87642020-11-12 15:04:39 -0800120import android.telephony.ThermalMitigationRequest;
Jordan Liu5aa07002018-12-18 15:44:48 -0800121import android.telephony.UiccCardInfo;
sandeepjsb6c87872021-09-27 15:34:44 +0000122import android.telephony.UiccPortInfo;
Holly Jiuyu Sun01c47ad2018-01-24 17:56:33 +0000123import android.telephony.UiccSlotInfo;
sandeepjsb6c87872021-09-27 15:34:44 +0000124import android.telephony.UiccSlotMapping;
Tyler Gunn65d45c22017-06-05 11:22:26 -0700125import android.telephony.UssdResponse;
Ta-wei Yenb6929602016-05-24 15:48:27 -0700126import android.telephony.VisualVoicemailSmsFilterSettings;
Hongbo Zeng0e18b162021-04-07 16:52:18 +0800127import android.telephony.data.NetworkSlicingConfig;
Jack Yub5d8f642018-11-26 11:20:48 -0800128import android.telephony.emergency.EmergencyNumber;
Hui Wang641e81c2020-10-12 12:14:23 -0700129import android.telephony.gba.GbaAuthRequest;
130import android.telephony.gba.UaSecurityProtocolIdentifier;
Brad Ebinger1ce9c432019-07-16 13:19:44 -0700131import android.telephony.ims.ImsException;
Brad Ebinger1c8542e2019-01-14 13:43:14 -0800132import android.telephony.ims.ProvisioningManager;
Hui Wang761a6682020-10-31 05:12:53 +0000133import android.telephony.ims.RcsClientConfiguration;
Brad Ebinger14d467f2021-02-12 06:18:28 +0000134import android.telephony.ims.RcsContactUceCapability;
Brad Ebingera34a6c22019-10-22 17:36:18 -0700135import android.telephony.ims.RegistrationManager;
joonhunshincffb7fc2021-11-28 07:32:01 +0000136import android.telephony.ims.aidl.IFeatureProvisioningCallback;
Brad Ebinger35c841c2018-10-01 10:40:55 -0700137import android.telephony.ims.aidl.IImsCapabilityCallback;
Brad Ebinger22bc3e42018-01-16 09:39:35 -0800138import android.telephony.ims.aidl.IImsConfig;
Brad Ebingerdf5b4f02018-10-31 11:24:17 -0700139import android.telephony.ims.aidl.IImsConfigCallback;
Brad Ebinger22bc3e42018-01-16 09:39:35 -0800140import android.telephony.ims.aidl.IImsRegistration;
Brad Ebinger35c841c2018-10-01 10:40:55 -0700141import android.telephony.ims.aidl.IImsRegistrationCallback;
Hui Wang761a6682020-10-31 05:12:53 +0000142import android.telephony.ims.aidl.IRcsConfigCallback;
Brad Ebingerbc7dd582019-10-17 17:03:22 -0700143import android.telephony.ims.feature.ImsFeature;
Brad Ebinger1c8542e2019-01-14 13:43:14 -0800144import android.telephony.ims.stub.ImsConfigImplBase;
Brad Ebinger1f2b5082018-02-08 16:11:32 -0800145import android.telephony.ims.stub.ImsRegistrationImplBase;
Sarah Chineccfbd12023-01-20 19:00:35 -0800146import android.telephony.satellite.ISatellitePositionUpdateCallback;
147import android.telephony.satellite.PointingInfo;
148import android.telephony.satellite.SatelliteManager;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700149import android.text.TextUtils;
Jeff Sharkey85190e62014-12-05 09:40:12 -0800150import android.util.ArraySet;
Hall Liud60acc92020-05-21 17:09:35 -0700151import android.util.EventLog;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700152import android.util.Log;
Jake Hambye994d462014-02-03 13:10:13 -0800153import android.util.Pair;
Ta-wei Yen30a69c82016-12-27 14:52:32 -0800154
Andrew Lee312e8172014-10-23 17:01:36 -0700155import com.android.ims.ImsManager;
Brad Ebinger34bef922017-11-09 10:27:08 -0800156import com.android.ims.internal.IImsServiceFeatureCallback;
James.cf Linbcdf8b32021-01-14 16:44:13 +0800157import com.android.ims.rcs.uce.eab.EabUtil;
SongFerngWangfd89b102021-05-27 22:44:54 +0800158import com.android.internal.annotations.VisibleForTesting;
Shuo Qian4a594052020-01-23 11:59:30 -0800159import com.android.internal.telephony.CallForwardInfo;
Shishir Agrawal566b7612013-10-28 14:41:00 -0700160import com.android.internal.telephony.CallManager;
Tyler Gunn52dcf772017-04-26 11:30:31 -0700161import com.android.internal.telephony.CallStateException;
Tyler Gunnd4339262021-05-03 14:46:49 -0700162import com.android.internal.telephony.CallTracker;
Rambo Wang9c9ffdd2022-01-13 21:51:44 -0800163import com.android.internal.telephony.CarrierPrivilegesTracker;
chen xu651eec72018-11-11 19:03:44 -0800164import com.android.internal.telephony.CarrierResolver;
Shishir Agrawal302c8692015-06-19 13:49:39 -0700165import com.android.internal.telephony.CellNetworkScanResult;
Shishir Agrawal566b7612013-10-28 14:41:00 -0700166import com.android.internal.telephony.CommandException;
Shuo Qian4a594052020-01-23 11:59:30 -0800167import com.android.internal.telephony.CommandsInterface;
Hui Wang641e81c2020-10-12 12:14:23 -0700168import com.android.internal.telephony.GbaManager;
Shuo Qianccbaf742021-02-22 18:32:21 -0800169import com.android.internal.telephony.GsmCdmaPhone;
Nathan Harold48d6fd52019-02-06 19:01:40 -0800170import com.android.internal.telephony.HalVersion;
Hall Liu73f5d362020-01-20 13:42:00 -0800171import com.android.internal.telephony.IBooleanConsumer;
Hall Liu27d24262020-09-18 19:04:59 -0700172import com.android.internal.telephony.ICallForwardingInfoCallback;
Hunsuk Choi3b742d62021-10-25 19:48:34 +0000173import com.android.internal.telephony.IImsStateCallback;
Brad Ebingera63db5f2019-04-23 16:31:13 -0700174import com.android.internal.telephony.IIntegerConsumer;
Hall Liud892bec2018-11-30 14:51:45 -0800175import com.android.internal.telephony.INumberVerificationCallback;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700176import com.android.internal.telephony.ITelephony;
Jake Hambye994d462014-02-03 13:10:13 -0800177import com.android.internal.telephony.IccCard;
Jack Yuf5badd92022-12-08 00:50:53 -0800178import com.android.internal.telephony.IccCardConstants;
Rambo Wanga1782702021-11-10 20:15:19 -0800179import com.android.internal.telephony.IccLogicalChannelRequest;
Jack Yu5f7092c2018-04-13 14:05:37 -0700180import com.android.internal.telephony.LocaleTracker;
yinxub1bed742017-04-17 11:45:04 -0700181import com.android.internal.telephony.NetworkScanRequestTracker;
Shishir Agrawal302c8692015-06-19 13:49:39 -0700182import com.android.internal.telephony.OperatorInfo;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700183import com.android.internal.telephony.Phone;
Malcolm Chen2c63d402018-08-14 16:00:53 -0700184import com.android.internal.telephony.PhoneConfigurationManager;
Nathan Harolda667c152016-12-14 11:27:20 -0800185import com.android.internal.telephony.PhoneConstantConversions;
Ta-wei Yen87c49842016-05-13 21:19:52 -0700186import com.android.internal.telephony.PhoneConstants;
Wink Saville36469e72014-06-11 15:17:00 -0700187import com.android.internal.telephony.PhoneFactory;
Wink Saville5d475dd2014-10-17 15:00:58 -0700188import com.android.internal.telephony.ProxyController;
Sanket Padawe99ef1e32016-05-18 16:12:33 -0700189import com.android.internal.telephony.RIL;
SongFerngWang8c6e82e2021-03-02 22:09:29 +0800190import com.android.internal.telephony.RILConstants;
Sarah Chineccfbd12023-01-20 19:00:35 -0800191import com.android.internal.telephony.RILUtils;
Daniel Bright94f43662021-03-01 14:43:40 -0800192import com.android.internal.telephony.RadioInterfaceCapabilityController;
Jack Yu5f7092c2018-04-13 14:05:37 -0700193import com.android.internal.telephony.ServiceStateTracker;
Aishwarya Mallampatifbc70d32022-11-10 20:33:02 +0000194import com.android.internal.telephony.SmsApplication;
Amit Mahajandccb3f12019-05-13 13:48:32 -0700195import com.android.internal.telephony.SmsController;
Brad Ebingera63db5f2019-04-23 16:31:13 -0700196import com.android.internal.telephony.SmsPermissions;
Wink Savilleac1bdfd2014-11-20 23:04:44 -0800197import com.android.internal.telephony.SubscriptionController;
Peter Wang59571be2020-01-27 12:35:15 +0800198import com.android.internal.telephony.TelephonyIntents;
Jeff Davidson7e17e312018-02-13 18:17:36 -0800199import com.android.internal.telephony.TelephonyPermissions;
Jack Yu27422a52022-03-21 10:38:05 -0700200import com.android.internal.telephony.data.DataUtils;
Hunsuk Choi42cc62a2022-10-16 06:03:40 +0000201import com.android.internal.telephony.domainselection.DomainSelectionResolver;
sqianf4ca7ed2019-01-15 18:32:07 -0800202import com.android.internal.telephony.emergency.EmergencyNumberTracker;
Derek Tan740e1672017-06-27 14:56:27 -0700203import com.android.internal.telephony.euicc.EuiccConnector;
Brad Ebinger9c0eb502019-01-23 15:06:19 -0800204import com.android.internal.telephony.ims.ImsResolver;
Tyler Gunn7bcdc742019-10-04 15:56:59 -0700205import com.android.internal.telephony.imsphone.ImsPhone;
206import com.android.internal.telephony.imsphone.ImsPhoneCallTracker;
joonhunshin3e154242021-09-17 06:33:39 +0000207import com.android.internal.telephony.metrics.RcsStats;
Pengquan Meng6c2dc9f2019-02-06 11:12:53 -0800208import com.android.internal.telephony.metrics.TelephonyMetrics;
Jack Yu285100e2022-12-02 22:48:35 -0800209import com.android.internal.telephony.subscription.SubscriptionInfoInternal;
210import com.android.internal.telephony.subscription.SubscriptionManagerService;
Meng Wangafbc5852019-09-19 17:37:13 -0700211import com.android.internal.telephony.uicc.IccCardApplicationStatus.AppType;
Shishir Agrawal566b7612013-10-28 14:41:00 -0700212import com.android.internal.telephony.uicc.IccIoResult;
213import com.android.internal.telephony.uicc.IccUtils;
Nathan Haroldb3014052017-01-25 15:57:32 -0800214import com.android.internal.telephony.uicc.SIMRecords;
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700215import com.android.internal.telephony.uicc.UiccCard;
Nathan Haroldb3014052017-01-25 15:57:32 -0800216import com.android.internal.telephony.uicc.UiccCardApplication;
Shishir Agrawal566b7612013-10-28 14:41:00 -0700217import com.android.internal.telephony.uicc.UiccController;
Muralidhar Reddy472c2ae2021-09-29 19:38:40 +0000218import com.android.internal.telephony.uicc.UiccPort;
Jeff Davidson7e17e312018-02-13 18:17:36 -0800219import com.android.internal.telephony.uicc.UiccProfile;
Holly Jiuyu Sun01c47ad2018-01-24 17:56:33 +0000220import com.android.internal.telephony.uicc.UiccSlot;
zoey chenc730df82019-12-18 17:07:20 +0800221import com.android.internal.telephony.util.LocaleUtils;
Aishwarya Mallampatifbc70d32022-11-10 20:33:02 +0000222import com.android.internal.telephony.util.TelephonyUtils;
fionaxu7ed723d2017-05-30 18:58:54 -0700223import com.android.internal.telephony.util.VoicemailNotificationSettingsUtil;
Hall Liu27d24262020-09-18 19:04:59 -0700224import com.android.internal.util.FunctionalUtils;
Jake Hambye994d462014-02-03 13:10:13 -0800225import com.android.internal.util.HexDump;
Hall Liuaa4211e2021-01-20 15:43:39 -0800226import com.android.phone.callcomposer.CallComposerPictureManager;
227import com.android.phone.callcomposer.CallComposerPictureTransfer;
228import com.android.phone.callcomposer.ImageData;
Brad Ebingera63db5f2019-04-23 16:31:13 -0700229import com.android.phone.settings.PickSmsSubscriptionActivity;
Sarah Chin46355ba2022-11-01 23:51:16 -0700230import com.android.phone.slice.SlicePurchaseController;
arunvoddud7401012022-12-15 16:08:12 +0000231import com.android.phone.utils.CarrierAllowListInfo;
Ta-wei Yenc9df0432017-04-17 17:09:07 -0700232import com.android.phone.vvm.PhoneAccountHandleConverter;
Ta-wei Yen527a9c02017-01-06 15:29:25 -0800233import com.android.phone.vvm.RemoteVvmTaskManager;
Ta-wei Yenc9df0432017-04-17 17:09:07 -0700234import com.android.phone.vvm.VisualVoicemailSettingsUtil;
Ta-wei Yenc8905312017-03-28 11:14:45 -0700235import com.android.phone.vvm.VisualVoicemailSmsFilterConfig;
Jack Nudelmanb0b87642020-11-12 15:04:39 -0800236import com.android.services.telephony.TelecomAccountRegistry;
237import com.android.services.telephony.TelephonyConnectionService;
Peter Wang44b186e2020-01-13 23:33:09 -0800238import com.android.telephony.Rlog;
Ta-wei Yen30a69c82016-12-27 14:52:32 -0800239
Hall Liu82694d52020-12-11 18:22:04 -0800240import java.io.ByteArrayOutputStream;
Ta-wei Yenc236d6b2016-06-21 13:33:12 -0700241import java.io.FileDescriptor;
Hall Liu82694d52020-12-11 18:22:04 -0800242import java.io.IOException;
243import java.io.InputStream;
Ta-wei Yenc236d6b2016-06-21 13:33:12 -0700244import java.io.PrintWriter;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700245import java.util.ArrayList;
Jeff Sharkey85190e62014-12-05 09:40:12 -0800246import java.util.Arrays;
Muralidhar Reddyeb809e32021-11-19 03:07:54 +0000247import java.util.Collection;
Hunter Knepshieldcad7f0b2021-12-13 15:08:35 -0800248import java.util.Collections;
sqian11b7a0e2018-12-05 18:48:28 -0800249import java.util.HashMap;
sqianf4ca7ed2019-01-15 18:32:07 -0800250import java.util.HashSet;
Jake Hambye994d462014-02-03 13:10:13 -0800251import java.util.List;
Narayan Kamath1c496c22015-04-16 14:40:19 +0100252import java.util.Locale;
Jeff Sharkey85190e62014-12-05 09:40:12 -0800253import java.util.Map;
Nazanin Bakhshif71371d2019-04-29 17:29:44 -0700254import java.util.NoSuchElementException;
Hall Liu82694d52020-12-11 18:22:04 -0800255import java.util.Objects;
sqianf4ca7ed2019-01-15 18:32:07 -0800256import java.util.Set;
Sarah Chin2ec39f62022-08-31 17:03:26 -0700257import java.util.UUID;
Sarah Chineccfbd12023-01-20 19:00:35 -0800258import java.util.concurrent.ConcurrentHashMap;
Hall Liu82694d52020-12-11 18:22:04 -0800259import java.util.concurrent.Executors;
Peter Wangdafb9ac2020-01-15 14:13:38 -0800260import java.util.concurrent.atomic.AtomicBoolean;
Hall Liu73f5d362020-01-20 13:42:00 -0800261import java.util.function.Consumer;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700262
263/**
264 * Implementation of the ITelephony interface.
265 */
Santos Cordon117fee72014-05-16 17:56:12 -0700266public class PhoneInterfaceManager extends ITelephony.Stub {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700267 private static final String LOG_TAG = "PhoneInterfaceManager";
268 private static final boolean DBG = (PhoneGlobals.DBG_LEVEL >= 2);
269 private static final boolean DBG_LOC = false;
Jeff Sharkey85190e62014-12-05 09:40:12 -0800270 private static final boolean DBG_MERGE = false;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700271
272 // Message codes used with mMainThreadHandler
273 private static final int CMD_HANDLE_PIN_MMI = 1;
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700274 private static final int CMD_TRANSMIT_APDU_LOGICAL_CHANNEL = 7;
275 private static final int EVENT_TRANSMIT_APDU_LOGICAL_CHANNEL_DONE = 8;
Shishir Agrawal566b7612013-10-28 14:41:00 -0700276 private static final int CMD_OPEN_CHANNEL = 9;
277 private static final int EVENT_OPEN_CHANNEL_DONE = 10;
278 private static final int CMD_CLOSE_CHANNEL = 11;
279 private static final int EVENT_CLOSE_CHANNEL_DONE = 12;
Jake Hambye994d462014-02-03 13:10:13 -0800280 private static final int CMD_NV_READ_ITEM = 13;
281 private static final int EVENT_NV_READ_ITEM_DONE = 14;
282 private static final int CMD_NV_WRITE_ITEM = 15;
283 private static final int EVENT_NV_WRITE_ITEM_DONE = 16;
284 private static final int CMD_NV_WRITE_CDMA_PRL = 17;
285 private static final int EVENT_NV_WRITE_CDMA_PRL_DONE = 18;
chen xu6dac5ab2018-10-26 17:39:23 -0700286 private static final int CMD_RESET_MODEM_CONFIG = 19;
287 private static final int EVENT_RESET_MODEM_CONFIG_DONE = 20;
SongFerngWang3ef3e072020-12-21 16:41:52 +0800288 private static final int CMD_GET_ALLOWED_NETWORK_TYPES_BITMASK = 21;
289 private static final int EVENT_GET_ALLOWED_NETWORK_TYPES_BITMASK_DONE = 22;
Sailesh Nepal35b59452014-03-06 09:26:56 -0800290 private static final int CMD_SEND_ENVELOPE = 25;
291 private static final int EVENT_SEND_ENVELOPE_DONE = 26;
Shuo Qian850e4d6a2018-04-25 21:02:08 +0000292 private static final int CMD_INVOKE_OEM_RIL_REQUEST_RAW = 27;
293 private static final int EVENT_INVOKE_OEM_RIL_REQUEST_RAW_DONE = 28;
Derek Tan6b088ee2014-09-05 14:15:18 -0700294 private static final int CMD_TRANSMIT_APDU_BASIC_CHANNEL = 29;
295 private static final int EVENT_TRANSMIT_APDU_BASIC_CHANNEL_DONE = 30;
296 private static final int CMD_EXCHANGE_SIM_IO = 31;
297 private static final int EVENT_EXCHANGE_SIM_IO_DONE = 32;
Shishir Agrawal76d5da92014-11-09 16:17:25 -0800298 private static final int CMD_SET_VOICEMAIL_NUMBER = 33;
299 private static final int EVENT_SET_VOICEMAIL_NUMBER_DONE = 34;
Stuart Scott54788802015-03-30 13:18:01 -0700300 private static final int CMD_SET_NETWORK_SELECTION_MODE_AUTOMATIC = 35;
301 private static final int EVENT_SET_NETWORK_SELECTION_MODE_AUTOMATIC_DONE = 36;
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -0700302 private static final int CMD_GET_MODEM_ACTIVITY_INFO = 37;
303 private static final int EVENT_GET_MODEM_ACTIVITY_INFO_DONE = 38;
Shishir Agrawal302c8692015-06-19 13:49:39 -0700304 private static final int CMD_PERFORM_NETWORK_SCAN = 39;
305 private static final int EVENT_PERFORM_NETWORK_SCAN_DONE = 40;
306 private static final int CMD_SET_NETWORK_SELECTION_MODE_MANUAL = 41;
307 private static final int EVENT_SET_NETWORK_SELECTION_MODE_MANUAL_DONE = 42;
Meng Wang1a7c35a2016-05-05 20:56:15 -0700308 private static final int CMD_SET_ALLOWED_CARRIERS = 43;
309 private static final int EVENT_SET_ALLOWED_CARRIERS_DONE = 44;
310 private static final int CMD_GET_ALLOWED_CARRIERS = 45;
311 private static final int EVENT_GET_ALLOWED_CARRIERS_DONE = 46;
pkanwar32d516d2016-10-14 19:37:38 -0700312 private static final int CMD_HANDLE_USSD_REQUEST = 47;
Nathan Haroldb3014052017-01-25 15:57:32 -0800313 private static final int CMD_GET_FORBIDDEN_PLMNS = 48;
314 private static final int EVENT_GET_FORBIDDEN_PLMNS_DONE = 49;
Holly Jiuyu Sun01c47ad2018-01-24 17:56:33 +0000315 private static final int CMD_SWITCH_SLOTS = 50;
316 private static final int EVENT_SWITCH_SLOTS_DONE = 51;
Pengquan Menga1bb6272018-09-06 09:59:22 -0700317 private static final int CMD_GET_NETWORK_SELECTION_MODE = 52;
318 private static final int EVENT_GET_NETWORK_SELECTION_MODE_DONE = 53;
319 private static final int CMD_GET_CDMA_ROAMING_MODE = 54;
320 private static final int EVENT_GET_CDMA_ROAMING_MODE_DONE = 55;
321 private static final int CMD_SET_CDMA_ROAMING_MODE = 56;
322 private static final int EVENT_SET_CDMA_ROAMING_MODE_DONE = 57;
323 private static final int CMD_SET_CDMA_SUBSCRIPTION_MODE = 58;
324 private static final int EVENT_SET_CDMA_SUBSCRIPTION_MODE_DONE = 59;
Nathan Harold3ff88932018-08-14 10:19:49 -0700325 private static final int CMD_GET_ALL_CELL_INFO = 60;
326 private static final int EVENT_GET_ALL_CELL_INFO_DONE = 61;
327 private static final int CMD_GET_CELL_LOCATION = 62;
328 private static final int EVENT_GET_CELL_LOCATION_DONE = 63;
chen xu6dac5ab2018-10-26 17:39:23 -0700329 private static final int CMD_MODEM_REBOOT = 64;
330 private static final int EVENT_CMD_MODEM_REBOOT_DONE = 65;
Nathan Haroldfa8da0f2018-09-27 18:51:29 -0700331 private static final int CMD_REQUEST_CELL_INFO_UPDATE = 66;
332 private static final int EVENT_REQUEST_CELL_INFO_UPDATE_DONE = 67;
Malcolm Chen8e4ed912019-01-15 20:22:16 -0800333 private static final int CMD_REQUEST_ENABLE_MODEM = 68;
334 private static final int EVENT_ENABLE_MODEM_DONE = 69;
Nazanin Bakhshif71371d2019-04-29 17:29:44 -0700335 private static final int CMD_GET_MODEM_STATUS = 70;
336 private static final int EVENT_GET_MODEM_STATUS_DONE = 71;
yincheng zhao2737e882019-09-06 17:06:54 -0700337 private static final int CMD_SET_FORBIDDEN_PLMNS = 72;
338 private static final int EVENT_SET_FORBIDDEN_PLMNS_DONE = 73;
Naina Nallurid63128d2019-09-17 14:10:30 -0700339 private static final int CMD_ERASE_MODEM_CONFIG = 74;
340 private static final int EVENT_ERASE_MODEM_CONFIG_DONE = 75;
zoey chene02881a2019-12-30 16:11:23 +0800341 private static final int CMD_CHANGE_ICC_LOCK_PASSWORD = 76;
342 private static final int EVENT_CHANGE_ICC_LOCK_PASSWORD_DONE = 77;
343 private static final int CMD_SET_ICC_LOCK_ENABLED = 78;
344 private static final int EVENT_SET_ICC_LOCK_ENABLED_DONE = 79;
Hall Liu73f5d362020-01-20 13:42:00 -0800345 private static final int CMD_SET_SYSTEM_SELECTION_CHANNELS = 80;
346 private static final int EVENT_SET_SYSTEM_SELECTION_CHANNELS_DONE = 81;
Peter Wangdafb9ac2020-01-15 14:13:38 -0800347 private static final int MSG_NOTIFY_USER_ACTIVITY = 82;
Shuo Qian4a594052020-01-23 11:59:30 -0800348 private static final int CMD_GET_CALL_FORWARDING = 83;
349 private static final int EVENT_GET_CALL_FORWARDING_DONE = 84;
350 private static final int CMD_SET_CALL_FORWARDING = 85;
351 private static final int EVENT_SET_CALL_FORWARDING_DONE = 86;
352 private static final int CMD_GET_CALL_WAITING = 87;
353 private static final int EVENT_GET_CALL_WAITING_DONE = 88;
354 private static final int CMD_SET_CALL_WAITING = 89;
355 private static final int EVENT_SET_CALL_WAITING_DONE = 90;
Sooraj Sasindran37444802020-08-11 10:40:43 -0700356 private static final int CMD_ENABLE_NR_DUAL_CONNECTIVITY = 91;
357 private static final int EVENT_ENABLE_NR_DUAL_CONNECTIVITY_DONE = 92;
358 private static final int CMD_IS_NR_DUAL_CONNECTIVITY_ENABLED = 93;
359 private static final int EVENT_IS_NR_DUAL_CONNECTIVITY_ENABLED_DONE = 94;
Sarah Chinbaab1432020-10-28 13:46:24 -0700360 private static final int CMD_GET_CDMA_SUBSCRIPTION_MODE = 95;
361 private static final int EVENT_GET_CDMA_SUBSCRIPTION_MODE_DONE = 96;
Sarah Chin679c08a2020-11-18 13:39:35 -0800362 private static final int CMD_GET_SYSTEM_SELECTION_CHANNELS = 97;
363 private static final int EVENT_GET_SYSTEM_SELECTION_CHANNELS_DONE = 98;
Jack Nudelmanb0b87642020-11-12 15:04:39 -0800364 private static final int CMD_SET_DATA_THROTTLING = 99;
365 private static final int EVENT_SET_DATA_THROTTLING_DONE = 100;
Jordan Liu109698e2020-11-24 14:50:34 -0800366 private static final int CMD_SET_SIM_POWER = 101;
367 private static final int EVENT_SET_SIM_POWER_DONE = 102;
Rambo Wanga5cc9b72021-01-07 10:51:54 -0800368 private static final int CMD_SET_SIGNAL_STRENGTH_UPDATE_REQUEST = 103;
369 private static final int EVENT_SET_SIGNAL_STRENGTH_UPDATE_REQUEST_DONE = 104;
370 private static final int CMD_CLEAR_SIGNAL_STRENGTH_UPDATE_REQUEST = 105;
371 private static final int EVENT_CLEAR_SIGNAL_STRENGTH_UPDATE_REQUEST_DONE = 106;
SongFerngWang3ef3e072020-12-21 16:41:52 +0800372 private static final int CMD_SET_ALLOWED_NETWORK_TYPES_FOR_REASON = 107;
373 private static final int EVENT_SET_ALLOWED_NETWORK_TYPES_FOR_REASON_DONE = 108;
Michele Berionne5e411512020-11-13 02:36:59 +0000374 private static final int CMD_PREPARE_UNATTENDED_REBOOT = 109;
Hongbo Zeng156aa4a2021-02-08 21:50:28 +0800375 private static final int CMD_GET_SLICING_CONFIG = 110;
376 private static final int EVENT_GET_SLICING_CONFIG_DONE = 111;
Kai Shif70f46f2021-03-03 13:59:46 -0800377 private static final int CMD_ERASE_DATA_SHARED_PREFERENCES = 112;
Sooraj Sasindrandaf060f2021-06-15 14:52:55 -0700378 private static final int CMD_ENABLE_VONR = 113;
379 private static final int EVENT_ENABLE_VONR_DONE = 114;
380 private static final int CMD_IS_VONR_ENABLED = 115;
381 private static final int EVENT_IS_VONR_ENABLED_DONE = 116;
Sarah Chin2ec39f62022-08-31 17:03:26 -0700382 private static final int CMD_PURCHASE_PREMIUM_CAPABILITY = 117;
383 private static final int EVENT_PURCHASE_PREMIUM_CAPABILITY_DONE = 118;
Sarah Chineccfbd12023-01-20 19:00:35 -0800384 private static final int CMD_START_SATELLITE_POSITION_UPDATES = 119;
385 private static final int EVENT_START_SATELLITE_POSITION_UPDATES_DONE = 120;
386 private static final int CMD_STOP_SATELLITE_POSITION_UPDATES = 121;
387 private static final int EVENT_STOP_SATELLITE_POSITION_UPDATES_DONE = 122;
Holly Jiuyu Sun1cc2d552018-01-26 15:51:16 -0800388 // Parameters of select command.
389 private static final int SELECT_COMMAND = 0xA4;
390 private static final int SELECT_P1 = 0x04;
391 private static final int SELECT_P2 = 0;
392 private static final int SELECT_P3 = 0x10;
393
Gil Cukierman1c0eb932022-12-06 22:28:24 +0000394 // Toggling null cipher and integrity support was added in IRadioNetwork 2.1
395 private static final int MIN_NULL_CIPHER_AND_INTEGRITY_VERSION = 201;
396
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700397 /** The singleton instance. */
398 private static PhoneInterfaceManager sInstance;
Jack Nudelman644b91a2021-03-12 14:09:48 -0800399 private static List<String> sThermalMitigationAllowlistedPackages = new ArrayList<>();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700400
Wink Saville3ab207e2014-11-20 13:07:20 -0800401 private PhoneGlobals mApp;
Wink Saville3ab207e2014-11-20 13:07:20 -0800402 private CallManager mCM;
Brad Ebinger24c29992019-12-05 13:03:21 -0800403 private ImsResolver mImsResolver;
Stuart Scott981d8582015-04-21 14:09:50 -0700404 private UserManager mUserManager;
Wink Saville3ab207e2014-11-20 13:07:20 -0800405 private AppOpsManager mAppOps;
Grace Jia0ddb3612021-04-22 13:35:26 -0700406 private PackageManager mPm;
Wink Saville3ab207e2014-11-20 13:07:20 -0800407 private MainThreadHandler mMainThreadHandler;
Jack Yue37dd262022-12-16 11:53:37 -0800408 private final SubscriptionController mSubscriptionController;
Wink Saville3ab207e2014-11-20 13:07:20 -0800409 private SharedPreferences mTelephonySharedPreferences;
Malcolm Chen2c63d402018-08-14 16:00:53 -0700410 private PhoneConfigurationManager mPhoneConfigurationManager;
Daniel Bright94f43662021-03-01 14:43:40 -0800411 private final RadioInterfaceCapabilityController mRadioInterfaceCapabilities;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700412
Peter Wangdafb9ac2020-01-15 14:13:38 -0800413 /** User Activity */
414 private AtomicBoolean mNotifyUserActivity;
Peter Wangdafb9ac2020-01-15 14:13:38 -0800415 private static final int USER_ACTIVITY_NOTIFICATION_DELAY = 200;
416
Jeff Davidson8ab02b22020-03-28 12:24:40 -0700417 private Set<Integer> mCarrierPrivilegeTestOverrideSubIds = new ArraySet<>();
Sarah Chineccfbd12023-01-20 19:00:35 -0800418 private Map<Integer, SatellitePositionUpdateHandler> mSatellitePositionUpdateHandlers =
419 new ConcurrentHashMap<>();
Jeff Davidson8ab02b22020-03-28 12:24:40 -0700420
Derek Tan97ebb422014-09-05 16:55:38 -0700421 private static final String PREF_CARRIERS_ALPHATAG_PREFIX = "carrier_alphtag_";
422 private static final String PREF_CARRIERS_NUMBER_PREFIX = "carrier_number_";
Jeff Sharkey85190e62014-12-05 09:40:12 -0800423 private static final String PREF_CARRIERS_SUBSCRIBER_PREFIX = "carrier_subscriber_";
Brad Ebinger1c8542e2019-01-14 13:43:14 -0800424 private static final String PREF_PROVISION_IMS_MMTEL_PREFIX = "provision_ims_mmtel_";
Derek Tan89e89d42014-07-08 17:00:10 -0700425
Michelecea4cf22018-12-21 15:00:11 -0800426 // String to store multi SIM allowed
427 private static final String PREF_MULTI_SIM_RESTRICTED = "multisim_restricted";
428
Derek Tan740e1672017-06-27 14:56:27 -0700429 // The AID of ISD-R.
430 private static final String ISDR_AID = "A0000005591010FFFFFFFF8900000100";
431
yinxub1bed742017-04-17 11:45:04 -0700432 private NetworkScanRequestTracker mNetworkScanRequestTracker;
433
David Kelly5e06a7f2018-03-12 14:10:59 +0000434 private static final int TYPE_ALLOCATION_CODE_LENGTH = 8;
435 private static final int MANUFACTURER_CODE_LENGTH = 8;
436
Jack Nudelmanb0b87642020-11-12 15:04:39 -0800437 private static final int SET_DATA_THROTTLING_MODEM_THREW_INVALID_PARAMS = -1;
Jack Nudelman5d6a98b2021-03-04 14:26:25 -0800438 private static final int MODEM_DOES_NOT_SUPPORT_DATA_THROTTLING_ERROR_CODE = -2;
Jack Nudelmanb0b87642020-11-12 15:04:39 -0800439
Sarah Chin2ec39f62022-08-31 17:03:26 -0700440 private static final String PURCHASE_PREMIUM_CAPABILITY_ERROR_UUID =
441 "24bf97a6-e8a6-44d8-a6a4-255d7548733c";
442
Derek Tan89e89d42014-07-08 17:00:10 -0700443 /**
Naina Nallurid63128d2019-09-17 14:10:30 -0700444 * Experiment flag to enable erase modem config on reset network, default value is false
445 */
446 public static final String RESET_NETWORK_ERASE_MODEM_CONFIG_ENABLED =
447 "reset_network_erase_modem_config_enabled";
448
Rambo Wang0f050d82021-02-12 11:43:36 -0800449 private static final int SET_NETWORK_SELECTION_MODE_AUTOMATIC_TIMEOUT_MS = 2000; // 2 seconds
Chen Xu540470b2021-12-14 17:15:47 -0800450
Gary Jian76280a42022-12-07 16:18:33 +0800451 private static final int MODEM_ACTIVITY_TIME_OFFSET_CORRECTION_MS = 50;
452
sandeepjsb6c87872021-09-27 15:34:44 +0000453 /**
454 * With support for MEP(multiple enabled profile) in Android T, a SIM card can have more than
455 * one ICCID active at the same time.
456 * Apps should use below API signatures if targeting SDK is T and beyond.
457 *
458 * @hide
459 */
460 @ChangeId
461 @EnabledSince(targetSdkVersion = Build.VERSION_CODES.TIRAMISU)
462 public static final long GET_API_SIGNATURES_FROM_UICC_PORT_INFO = 202110963L;
Rambo Wang0f050d82021-02-12 11:43:36 -0800463
Naina Nallurid63128d2019-09-17 14:10:30 -0700464 /**
Chen Xu540470b2021-12-14 17:15:47 -0800465 * Apps targeting on Android T and beyond will get exception whenever icc close channel
466 * operation fails.
467 */
468 @ChangeId
469 @EnabledSince(targetSdkVersion = Build.VERSION_CODES.TIRAMISU)
470 public static final long ICC_CLOSE_CHANNEL_EXCEPTION_ON_FAILURE = 208739934L;
471
472 /**
Shishir Agrawal566b7612013-10-28 14:41:00 -0700473 * A request object to use for transmitting data to an ICC.
474 */
475 private static final class IccAPDUArgument {
476 public int channel, cla, command, p1, p2, p3;
477 public String data;
478
479 public IccAPDUArgument(int channel, int cla, int command,
480 int p1, int p2, int p3, String data) {
481 this.channel = channel;
482 this.cla = cla;
483 this.command = command;
484 this.p1 = p1;
485 this.p2 = p2;
486 this.p3 = p3;
487 this.data = data;
488 }
489 }
490
491 /**
Shishir Agrawal77ba3172015-09-10 14:50:19 -0700492 * A request object to use for transmitting data to an ICC.
493 */
494 private static final class ManualNetworkSelectionArgument {
495 public OperatorInfo operatorInfo;
496 public boolean persistSelection;
497
498 public ManualNetworkSelectionArgument(OperatorInfo operatorInfo, boolean persistSelection) {
499 this.operatorInfo = operatorInfo;
500 this.persistSelection = persistSelection;
501 }
502 }
503
Sarah Chin71b3a852022-09-28 15:54:19 -0700504 private static final class PurchasePremiumCapabilityArgument {
505 public @TelephonyManager.PremiumCapability int capability;
Sarah Chin71b3a852022-09-28 15:54:19 -0700506 public @NonNull IIntegerConsumer callback;
507
508 PurchasePremiumCapabilityArgument(@TelephonyManager.PremiumCapability int capability,
Sarah Chinb8218c22023-01-04 13:35:29 -0800509 @NonNull IIntegerConsumer callback) {
Sarah Chin71b3a852022-09-28 15:54:19 -0700510 this.capability = capability;
Sarah Chin71b3a852022-09-28 15:54:19 -0700511 this.callback = callback;
512 }
513 }
514
Sarah Chineccfbd12023-01-20 19:00:35 -0800515 private static final class SatellitePositionUpdateHandler extends Handler {
516 public static final int EVENT_POSITION_UPDATE = 1;
517 public static final int EVENT_MESSAGE_TRANSFER_STATE_UPDATE = 2;
518
519 private final ISatellitePositionUpdateCallback mCallback;
520
521 SatellitePositionUpdateHandler(ISatellitePositionUpdateCallback callback, Looper looper) {
522 super(looper);
523 mCallback = callback;
524 }
525
526 @Override
527 public void handleMessage(@NonNull Message msg) {
528 switch (msg.what) {
529 case EVENT_POSITION_UPDATE: {
530 AsyncResult ar = (AsyncResult) msg.obj;
531 PointingInfo pointingInfo = (PointingInfo) ar.result;
532 try {
533 mCallback.onSatellitePositionUpdate(pointingInfo);
534 } catch (RemoteException e) {
535 loge("EVENT_POSITION_UPDATE RemoteException: " + e);
536 }
537 break;
538 }
539 case EVENT_MESSAGE_TRANSFER_STATE_UPDATE: {
540 AsyncResult ar = (AsyncResult) msg.obj;
541 int state = (int) ar.result;
542 try {
543 mCallback.onMessageTransferStateUpdate(state);
544 } catch (RemoteException e) {
545 loge("EVENT_MESSAGE_TRANSFER_STATE_UPDATE RemoteException: " + e);
546 }
547 break;
548 }
549 default:
550 loge("SatellitePositionUpdateHandler unknown event: " + msg.what);
551 }
552 }
553 }
554
Shishir Agrawal77ba3172015-09-10 14:50:19 -0700555 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700556 * A request object for use with {@link MainThreadHandler}. Requesters should wait() on the
557 * request after sending. The main thread will notify the request when it is complete.
558 */
559 private static final class MainThreadRequest {
560 /** The argument to use for the request */
561 public Object argument;
562 /** The result of the request that is run on the main thread */
563 public Object result;
Sanket Padawe56e75a32016-02-08 12:18:19 -0800564 // The subscriber id that this request applies to. Defaults to
565 // SubscriptionManager.INVALID_SUBSCRIPTION_ID
566 public Integer subId = SubscriptionManager.INVALID_SUBSCRIPTION_ID;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700567
Nathan Harold92bed182018-10-12 18:16:49 -0700568 // In cases where subId is unavailable, the caller needs to specify the phone.
569 public Phone phone;
570
vagdeviaf9a5b92018-08-15 16:01:53 -0700571 public WorkSource workSource;
572
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700573 public MainThreadRequest(Object argument) {
574 this.argument = argument;
575 }
Shishir Agrawal76d5da92014-11-09 16:17:25 -0800576
Nathan Harold92bed182018-10-12 18:16:49 -0700577 MainThreadRequest(Object argument, Phone phone, WorkSource workSource) {
578 this.argument = argument;
579 if (phone != null) {
580 this.phone = phone;
581 }
582 this.workSource = workSource;
583 }
584
vagdeviaf9a5b92018-08-15 16:01:53 -0700585 MainThreadRequest(Object argument, Integer subId, WorkSource workSource) {
Shishir Agrawal76d5da92014-11-09 16:17:25 -0800586 this.argument = argument;
Sanket Padawe56e75a32016-02-08 12:18:19 -0800587 if (subId != null) {
588 this.subId = subId;
589 }
vagdeviaf9a5b92018-08-15 16:01:53 -0700590 this.workSource = workSource;
Shishir Agrawal76d5da92014-11-09 16:17:25 -0800591 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700592 }
593
Sailesh Nepalcc0375f2013-11-13 09:15:18 -0800594 private static final class IncomingThirdPartyCallArgs {
595 public final ComponentName component;
596 public final String callId;
597 public final String callerDisplayName;
598
599 public IncomingThirdPartyCallArgs(ComponentName component, String callId,
600 String callerDisplayName) {
601 this.component = component;
602 this.callId = callId;
603 this.callerDisplayName = callerDisplayName;
604 }
605 }
606
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700607 /**
608 * A handler that processes messages on the main thread in the phone process. Since many
609 * of the Phone calls are not thread safe this is needed to shuttle the requests from the
610 * inbound binder threads to the main thread in the phone process. The Binder thread
611 * may provide a {@link MainThreadRequest} object in the msg.obj field that they are waiting
612 * on, which will be notified when the operation completes and will contain the result of the
613 * request.
614 *
615 * <p>If a MainThreadRequest object is provided in the msg.obj field,
616 * note that request.result must be set to something non-null for the calling thread to
617 * unblock.
618 */
619 private final class MainThreadHandler extends Handler {
620 @Override
621 public void handleMessage(Message msg) {
622 MainThreadRequest request;
623 Message onCompleted;
624 AsyncResult ar;
Muralidhar Reddy472c2ae2021-09-29 19:38:40 +0000625 UiccPort uiccPort;
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700626 IccAPDUArgument iccArgument;
Nazanin Bakhshif782e562018-12-11 15:15:39 -0800627 final Phone defaultPhone = getDefaultPhone();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700628
629 switch (msg.what) {
Pengquan Menga1bb6272018-09-06 09:59:22 -0700630 case CMD_HANDLE_USSD_REQUEST: {
631 request = (MainThreadRequest) msg.obj;
632 final Phone phone = getPhoneFromRequest(request);
633 Pair<String, ResultReceiver> ussdObject = (Pair) request.argument;
Chen Xue9d737e2022-01-01 23:41:31 -0800634 String ussdRequest = ussdObject.first;
Pengquan Menga1bb6272018-09-06 09:59:22 -0700635 ResultReceiver wrappedCallback = ussdObject.second;
Tyler Gunn65d45c22017-06-05 11:22:26 -0700636
Pengquan Menga1bb6272018-09-06 09:59:22 -0700637 if (!isUssdApiAllowed(request.subId)) {
638 // Carrier does not support use of this API, return failure.
639 Rlog.w(LOG_TAG, "handleUssdRequest: carrier does not support USSD apis.");
640 UssdResponse response = new UssdResponse(ussdRequest, null);
641 Bundle returnData = new Bundle();
642 returnData.putParcelable(TelephonyManager.USSD_RESPONSE, response);
643 wrappedCallback.send(TelephonyManager.USSD_RETURN_FAILURE, returnData);
Tyler Gunn65d45c22017-06-05 11:22:26 -0700644
Pengquan Menga1bb6272018-09-06 09:59:22 -0700645 request.result = true;
646 notifyRequester(request);
647 return;
648 }
Tyler Gunn65d45c22017-06-05 11:22:26 -0700649
Pengquan Menga1bb6272018-09-06 09:59:22 -0700650 try {
651 request.result = phone != null
652 ? phone.handleUssdRequest(ussdRequest, wrappedCallback) : false;
653 } catch (CallStateException cse) {
654 request.result = false;
655 }
656 // Wake up the requesting thread
657 notifyRequester(request);
658 break;
pkanwar32d516d2016-10-14 19:37:38 -0700659 }
660
Yorke Lee716f67e2015-06-17 15:39:16 -0700661 case CMD_HANDLE_PIN_MMI: {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700662 request = (MainThreadRequest) msg.obj;
Yorke Lee716f67e2015-06-17 15:39:16 -0700663 final Phone phone = getPhoneFromRequest(request);
664 request.result = phone != null ?
665 getPhoneFromRequest(request).handlePinMmi((String) request.argument)
666 : false;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700667 // Wake up the requesting thread
Pengquan Menga1bb6272018-09-06 09:59:22 -0700668 notifyRequester(request);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700669 break;
Yorke Lee716f67e2015-06-17 15:39:16 -0700670 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700671
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700672 case CMD_TRANSMIT_APDU_LOGICAL_CHANNEL:
Shishir Agrawal566b7612013-10-28 14:41:00 -0700673 request = (MainThreadRequest) msg.obj;
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700674 iccArgument = (IccAPDUArgument) request.argument;
Muralidhar Reddy472c2ae2021-09-29 19:38:40 +0000675 uiccPort = getUiccPortFromRequest(request);
676 if (uiccPort == null) {
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700677 loge("iccTransmitApduLogicalChannel: No UICC");
Chen Xue9d737e2022-01-01 23:41:31 -0800678 request.result = new IccIoResult(0x6F, 0, (byte[]) null);
Pengquan Menga1bb6272018-09-06 09:59:22 -0700679 notifyRequester(request);
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700680 } else {
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700681 onCompleted = obtainMessage(EVENT_TRANSMIT_APDU_LOGICAL_CHANNEL_DONE,
Chen Xue9d737e2022-01-01 23:41:31 -0800682 request);
Muralidhar Reddy472c2ae2021-09-29 19:38:40 +0000683 uiccPort.iccTransmitApduLogicalChannel(
Chen Xue9d737e2022-01-01 23:41:31 -0800684 iccArgument.channel, iccArgument.cla, iccArgument.command,
685 iccArgument.p1, iccArgument.p2, iccArgument.p3, iccArgument.data,
686 onCompleted);
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700687 }
Shishir Agrawal566b7612013-10-28 14:41:00 -0700688 break;
689
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700690 case EVENT_TRANSMIT_APDU_LOGICAL_CHANNEL_DONE:
Shishir Agrawal566b7612013-10-28 14:41:00 -0700691 ar = (AsyncResult) msg.obj;
692 request = (MainThreadRequest) ar.userObj;
693 if (ar.exception == null && ar.result != null) {
694 request.result = ar.result;
695 } else {
Chen Xue9d737e2022-01-01 23:41:31 -0800696 request.result = new IccIoResult(0x6F, 0, (byte[]) null);
Shishir Agrawal566b7612013-10-28 14:41:00 -0700697 if (ar.result == null) {
698 loge("iccTransmitApduLogicalChannel: Empty response");
Jake Hambye994d462014-02-03 13:10:13 -0800699 } else if (ar.exception instanceof CommandException) {
Shishir Agrawal566b7612013-10-28 14:41:00 -0700700 loge("iccTransmitApduLogicalChannel: CommandException: " +
Jake Hambye994d462014-02-03 13:10:13 -0800701 ar.exception);
Shishir Agrawal566b7612013-10-28 14:41:00 -0700702 } else {
703 loge("iccTransmitApduLogicalChannel: Unknown exception");
704 }
705 }
Pengquan Menga1bb6272018-09-06 09:59:22 -0700706 notifyRequester(request);
Shishir Agrawal566b7612013-10-28 14:41:00 -0700707 break;
708
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700709 case CMD_TRANSMIT_APDU_BASIC_CHANNEL:
710 request = (MainThreadRequest) msg.obj;
711 iccArgument = (IccAPDUArgument) request.argument;
Muralidhar Reddy472c2ae2021-09-29 19:38:40 +0000712 uiccPort = getUiccPortFromRequest(request);
713 if (uiccPort == null) {
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700714 loge("iccTransmitApduBasicChannel: No UICC");
Chen Xue9d737e2022-01-01 23:41:31 -0800715 request.result = new IccIoResult(0x6F, 0, (byte[]) null);
Pengquan Menga1bb6272018-09-06 09:59:22 -0700716 notifyRequester(request);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700717 } else {
718 onCompleted = obtainMessage(EVENT_TRANSMIT_APDU_BASIC_CHANNEL_DONE,
Chen Xue9d737e2022-01-01 23:41:31 -0800719 request);
Muralidhar Reddy472c2ae2021-09-29 19:38:40 +0000720 uiccPort.iccTransmitApduBasicChannel(
Chen Xue9d737e2022-01-01 23:41:31 -0800721 iccArgument.cla, iccArgument.command, iccArgument.p1,
722 iccArgument.p2,
723 iccArgument.p3, iccArgument.data, onCompleted);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700724 }
725 break;
726
727 case EVENT_TRANSMIT_APDU_BASIC_CHANNEL_DONE:
728 ar = (AsyncResult) msg.obj;
729 request = (MainThreadRequest) ar.userObj;
730 if (ar.exception == null && ar.result != null) {
731 request.result = ar.result;
732 } else {
Chen Xue9d737e2022-01-01 23:41:31 -0800733 request.result = new IccIoResult(0x6F, 0, (byte[]) null);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700734 if (ar.result == null) {
735 loge("iccTransmitApduBasicChannel: Empty response");
736 } else if (ar.exception instanceof CommandException) {
737 loge("iccTransmitApduBasicChannel: CommandException: " +
738 ar.exception);
739 } else {
740 loge("iccTransmitApduBasicChannel: Unknown exception");
741 }
742 }
Pengquan Menga1bb6272018-09-06 09:59:22 -0700743 notifyRequester(request);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700744 break;
745
746 case CMD_EXCHANGE_SIM_IO:
747 request = (MainThreadRequest) msg.obj;
748 iccArgument = (IccAPDUArgument) request.argument;
Muralidhar Reddy472c2ae2021-09-29 19:38:40 +0000749 uiccPort = getUiccPortFromRequest(request);
750 if (uiccPort == null) {
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700751 loge("iccExchangeSimIO: No UICC");
Chen Xue9d737e2022-01-01 23:41:31 -0800752 request.result = new IccIoResult(0x6F, 0, (byte[]) null);
Pengquan Menga1bb6272018-09-06 09:59:22 -0700753 notifyRequester(request);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700754 } else {
755 onCompleted = obtainMessage(EVENT_EXCHANGE_SIM_IO_DONE,
756 request);
Muralidhar Reddy472c2ae2021-09-29 19:38:40 +0000757 uiccPort.iccExchangeSimIO(iccArgument.cla, /* fileID */
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700758 iccArgument.command, iccArgument.p1, iccArgument.p2, iccArgument.p3,
759 iccArgument.data, onCompleted);
760 }
761 break;
762
763 case EVENT_EXCHANGE_SIM_IO_DONE:
764 ar = (AsyncResult) msg.obj;
765 request = (MainThreadRequest) ar.userObj;
766 if (ar.exception == null && ar.result != null) {
767 request.result = ar.result;
768 } else {
Chen Xue9d737e2022-01-01 23:41:31 -0800769 request.result = new IccIoResult(0x6f, 0, (byte[]) null);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700770 }
Pengquan Menga1bb6272018-09-06 09:59:22 -0700771 notifyRequester(request);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700772 break;
773
Derek Tan4d5e5c12014-02-04 11:54:58 -0800774 case CMD_SEND_ENVELOPE:
775 request = (MainThreadRequest) msg.obj;
Muralidhar Reddy472c2ae2021-09-29 19:38:40 +0000776 uiccPort = getUiccPortFromRequest(request);
777 if (uiccPort == null) {
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700778 loge("sendEnvelopeWithStatus: No UICC");
Chen Xue9d737e2022-01-01 23:41:31 -0800779 request.result = new IccIoResult(0x6F, 0, (byte[]) null);
Pengquan Menga1bb6272018-09-06 09:59:22 -0700780 notifyRequester(request);
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700781 } else {
782 onCompleted = obtainMessage(EVENT_SEND_ENVELOPE_DONE, request);
Chen Xue9d737e2022-01-01 23:41:31 -0800783 uiccPort.sendEnvelopeWithStatus((String) request.argument, onCompleted);
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700784 }
Derek Tan4d5e5c12014-02-04 11:54:58 -0800785 break;
786
787 case EVENT_SEND_ENVELOPE_DONE:
788 ar = (AsyncResult) msg.obj;
789 request = (MainThreadRequest) ar.userObj;
Shishir Agrawal9f9877d2014-03-14 09:36:27 -0700790 if (ar.exception == null && ar.result != null) {
791 request.result = ar.result;
Derek Tan4d5e5c12014-02-04 11:54:58 -0800792 } else {
Chen Xue9d737e2022-01-01 23:41:31 -0800793 request.result = new IccIoResult(0x6F, 0, (byte[]) null);
Shishir Agrawal9f9877d2014-03-14 09:36:27 -0700794 if (ar.result == null) {
795 loge("sendEnvelopeWithStatus: Empty response");
796 } else if (ar.exception instanceof CommandException) {
797 loge("sendEnvelopeWithStatus: CommandException: " +
798 ar.exception);
799 } else {
800 loge("sendEnvelopeWithStatus: exception:" + ar.exception);
801 }
Derek Tan4d5e5c12014-02-04 11:54:58 -0800802 }
Pengquan Menga1bb6272018-09-06 09:59:22 -0700803 notifyRequester(request);
Derek Tan4d5e5c12014-02-04 11:54:58 -0800804 break;
805
Shishir Agrawal566b7612013-10-28 14:41:00 -0700806 case CMD_OPEN_CHANNEL:
807 request = (MainThreadRequest) msg.obj;
Muralidhar Reddy472c2ae2021-09-29 19:38:40 +0000808 uiccPort = getUiccPortFromRequest(request);
Rambo Wanga1782702021-11-10 20:15:19 -0800809 IccLogicalChannelRequest openChannelRequest =
810 (IccLogicalChannelRequest) request.argument;
Muralidhar Reddy472c2ae2021-09-29 19:38:40 +0000811 if (uiccPort == null) {
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700812 loge("iccOpenLogicalChannel: No UICC");
Shishir Agrawalfc0492a2016-02-17 11:15:33 -0800813 request.result = new IccOpenLogicalChannelResponse(-1,
Chen Xue9d737e2022-01-01 23:41:31 -0800814 IccOpenLogicalChannelResponse.STATUS_MISSING_RESOURCE, null);
Pengquan Menga1bb6272018-09-06 09:59:22 -0700815 notifyRequester(request);
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700816 } else {
817 onCompleted = obtainMessage(EVENT_OPEN_CHANNEL_DONE, request);
Rambo Wanga1782702021-11-10 20:15:19 -0800818 uiccPort.iccOpenLogicalChannel(openChannelRequest.aid,
819 openChannelRequest.p2, onCompleted);
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700820 }
Shishir Agrawal566b7612013-10-28 14:41:00 -0700821 break;
822
823 case EVENT_OPEN_CHANNEL_DONE:
824 ar = (AsyncResult) msg.obj;
825 request = (MainThreadRequest) ar.userObj;
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700826 IccOpenLogicalChannelResponse openChannelResp;
Shishir Agrawal566b7612013-10-28 14:41:00 -0700827 if (ar.exception == null && ar.result != null) {
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700828 int[] result = (int[]) ar.result;
829 int channelId = result[0];
830 byte[] selectResponse = null;
831 if (result.length > 1) {
832 selectResponse = new byte[result.length - 1];
833 for (int i = 1; i < result.length; ++i) {
834 selectResponse[i - 1] = (byte) result[i];
835 }
836 }
837 openChannelResp = new IccOpenLogicalChannelResponse(channelId,
Chen Xue9d737e2022-01-01 23:41:31 -0800838 IccOpenLogicalChannelResponse.STATUS_NO_ERROR, selectResponse);
Rambo Wang3b77c4c2021-11-10 20:15:19 -0800839
840 uiccPort = getUiccPortFromRequest(request);
Rambo Wange53e07d2022-05-10 13:01:13 -0700841 if (uiccPort == null) {
842 loge("EVENT_OPEN_CHANNEL_DONE: UiccPort is null");
843 } else {
844 IccLogicalChannelRequest channelRequest =
845 (IccLogicalChannelRequest) request.argument;
846 channelRequest.channel = channelId;
847 uiccPort.onLogicalChannelOpened(channelRequest);
848 }
Shishir Agrawal566b7612013-10-28 14:41:00 -0700849 } else {
Shishir Agrawal566b7612013-10-28 14:41:00 -0700850 if (ar.result == null) {
851 loge("iccOpenLogicalChannel: Empty response");
Shishir Agrawal566b7612013-10-28 14:41:00 -0700852 }
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700853 if (ar.exception != null) {
854 loge("iccOpenLogicalChannel: Exception: " + ar.exception);
855 }
856
Shishir Agrawal527e8bf2014-08-25 08:54:56 -0700857 int errorCode = IccOpenLogicalChannelResponse.STATUS_UNKNOWN_ERROR;
Junda Liua754ba12015-05-20 01:17:52 -0700858 if (ar.exception instanceof CommandException) {
859 CommandException.Error error =
Chen Xue9d737e2022-01-01 23:41:31 -0800860 ((CommandException) (ar.exception)).getCommandError();
Junda Liua754ba12015-05-20 01:17:52 -0700861 if (error == CommandException.Error.MISSING_RESOURCE) {
Shishir Agrawal527e8bf2014-08-25 08:54:56 -0700862 errorCode = IccOpenLogicalChannelResponse.STATUS_MISSING_RESOURCE;
Junda Liua754ba12015-05-20 01:17:52 -0700863 } else if (error == CommandException.Error.NO_SUCH_ELEMENT) {
Shishir Agrawal527e8bf2014-08-25 08:54:56 -0700864 errorCode = IccOpenLogicalChannelResponse.STATUS_NO_SUCH_ELEMENT;
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700865 }
866 }
867 openChannelResp = new IccOpenLogicalChannelResponse(
Chen Xue9d737e2022-01-01 23:41:31 -0800868 IccOpenLogicalChannelResponse.INVALID_CHANNEL, errorCode, null);
Shishir Agrawal566b7612013-10-28 14:41:00 -0700869 }
Shishir Agrawal82c8a462014-07-31 18:13:17 -0700870 request.result = openChannelResp;
Pengquan Menga1bb6272018-09-06 09:59:22 -0700871 notifyRequester(request);
Shishir Agrawal566b7612013-10-28 14:41:00 -0700872 break;
873
874 case CMD_CLOSE_CHANNEL:
875 request = (MainThreadRequest) msg.obj;
Muralidhar Reddy472c2ae2021-09-29 19:38:40 +0000876 uiccPort = getUiccPortFromRequest(request);
877 if (uiccPort == null) {
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700878 loge("iccCloseLogicalChannel: No UICC");
Chen Xua8f0dff2022-02-12 00:34:15 -0800879 request.result = new IllegalArgumentException(
Chen Xue9d737e2022-01-01 23:41:31 -0800880 "iccCloseLogicalChannel: No UICC");
Chen Xue9d737e2022-01-01 23:41:31 -0800881 notifyRequester(request);
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700882 } else {
883 onCompleted = obtainMessage(EVENT_CLOSE_CHANNEL_DONE, request);
Muralidhar Reddy472c2ae2021-09-29 19:38:40 +0000884 uiccPort.iccCloseLogicalChannel((Integer) request.argument, onCompleted);
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700885 }
Shishir Agrawal566b7612013-10-28 14:41:00 -0700886 break;
887
888 case EVENT_CLOSE_CHANNEL_DONE:
Chen Xu540470b2021-12-14 17:15:47 -0800889 ar = (AsyncResult) msg.obj;
890 request = (MainThreadRequest) ar.userObj;
891 if (ar.exception == null) {
892 request.result = true;
Rambo Wang3b77c4c2021-11-10 20:15:19 -0800893 uiccPort = getUiccPortFromRequest(request);
Rambo Wange53e07d2022-05-10 13:01:13 -0700894 if (uiccPort == null) {
895 loge("EVENT_CLOSE_CHANNEL_DONE: UiccPort is null");
896 } else {
897 final int channelId = (Integer) request.argument;
898 uiccPort.onLogicalChannelClosed(channelId);
899 }
Chen Xu540470b2021-12-14 17:15:47 -0800900 } else {
901 request.result = false;
Chen Xue9d737e2022-01-01 23:41:31 -0800902 Exception exception = null;
Chen Xu540470b2021-12-14 17:15:47 -0800903 if (ar.exception instanceof CommandException) {
904 loge("iccCloseLogicalChannel: CommandException: " + ar.exception);
905 CommandException.Error error =
906 ((CommandException) (ar.exception)).getCommandError();
Chen Xue9d737e2022-01-01 23:41:31 -0800907 if (error == CommandException.Error.INVALID_ARGUMENTS) {
908 // should only throw exceptions from the binder threads.
909 exception = new IllegalArgumentException(
Chen Xu540470b2021-12-14 17:15:47 -0800910 "iccCloseLogicalChannel: invalid argument ");
911 }
912 } else {
913 loge("iccCloseLogicalChannel: Unknown exception");
914 }
Chen Xua8f0dff2022-02-12 00:34:15 -0800915 request.result = (exception != null) ? exception :
916 new IllegalStateException(
917 "exception from modem to close iccLogical Channel");
Chen Xu540470b2021-12-14 17:15:47 -0800918 }
919 notifyRequester(request);
Jake Hambye994d462014-02-03 13:10:13 -0800920 break;
921
922 case CMD_NV_READ_ITEM:
923 request = (MainThreadRequest) msg.obj;
924 onCompleted = obtainMessage(EVENT_NV_READ_ITEM_DONE, request);
Nazanin Bakhshif782e562018-12-11 15:15:39 -0800925 defaultPhone.nvReadItem((Integer) request.argument, onCompleted,
926 request.workSource);
Jake Hambye994d462014-02-03 13:10:13 -0800927 break;
928
929 case EVENT_NV_READ_ITEM_DONE:
Shishir Agrawal566b7612013-10-28 14:41:00 -0700930 ar = (AsyncResult) msg.obj;
931 request = (MainThreadRequest) ar.userObj;
Jake Hambye994d462014-02-03 13:10:13 -0800932 if (ar.exception == null && ar.result != null) {
933 request.result = ar.result; // String
Shishir Agrawal566b7612013-10-28 14:41:00 -0700934 } else {
Jake Hambye994d462014-02-03 13:10:13 -0800935 request.result = "";
936 if (ar.result == null) {
937 loge("nvReadItem: Empty response");
938 } else if (ar.exception instanceof CommandException) {
939 loge("nvReadItem: CommandException: " +
940 ar.exception);
Shishir Agrawal566b7612013-10-28 14:41:00 -0700941 } else {
Jake Hambye994d462014-02-03 13:10:13 -0800942 loge("nvReadItem: Unknown exception");
Shishir Agrawal566b7612013-10-28 14:41:00 -0700943 }
944 }
Pengquan Menga1bb6272018-09-06 09:59:22 -0700945 notifyRequester(request);
Shishir Agrawal566b7612013-10-28 14:41:00 -0700946 break;
947
Jake Hambye994d462014-02-03 13:10:13 -0800948 case CMD_NV_WRITE_ITEM:
949 request = (MainThreadRequest) msg.obj;
950 onCompleted = obtainMessage(EVENT_NV_WRITE_ITEM_DONE, request);
951 Pair<Integer, String> idValue = (Pair<Integer, String>) request.argument;
Nazanin Bakhshif782e562018-12-11 15:15:39 -0800952 defaultPhone.nvWriteItem(idValue.first, idValue.second, onCompleted,
vagdeviaf9a5b92018-08-15 16:01:53 -0700953 request.workSource);
Jake Hambye994d462014-02-03 13:10:13 -0800954 break;
955
956 case EVENT_NV_WRITE_ITEM_DONE:
957 handleNullReturnEvent(msg, "nvWriteItem");
958 break;
959
960 case CMD_NV_WRITE_CDMA_PRL:
961 request = (MainThreadRequest) msg.obj;
962 onCompleted = obtainMessage(EVENT_NV_WRITE_CDMA_PRL_DONE, request);
Nazanin Bakhshif782e562018-12-11 15:15:39 -0800963 defaultPhone.nvWriteCdmaPrl((byte[]) request.argument, onCompleted);
Jake Hambye994d462014-02-03 13:10:13 -0800964 break;
965
966 case EVENT_NV_WRITE_CDMA_PRL_DONE:
967 handleNullReturnEvent(msg, "nvWriteCdmaPrl");
968 break;
969
chen xu6dac5ab2018-10-26 17:39:23 -0700970 case CMD_RESET_MODEM_CONFIG:
Jake Hambye994d462014-02-03 13:10:13 -0800971 request = (MainThreadRequest) msg.obj;
chen xu6dac5ab2018-10-26 17:39:23 -0700972 onCompleted = obtainMessage(EVENT_RESET_MODEM_CONFIG_DONE, request);
Nazanin Bakhshif782e562018-12-11 15:15:39 -0800973 defaultPhone.resetModemConfig(onCompleted);
Jake Hambye994d462014-02-03 13:10:13 -0800974 break;
975
chen xu6dac5ab2018-10-26 17:39:23 -0700976 case EVENT_RESET_MODEM_CONFIG_DONE:
977 handleNullReturnEvent(msg, "resetModemConfig");
Jake Hambye994d462014-02-03 13:10:13 -0800978 break;
979
Sooraj Sasindran37444802020-08-11 10:40:43 -0700980 case CMD_IS_NR_DUAL_CONNECTIVITY_ENABLED: {
981 request = (MainThreadRequest) msg.obj;
982 onCompleted = obtainMessage(EVENT_IS_NR_DUAL_CONNECTIVITY_ENABLED_DONE,
983 request);
984 Phone phone = getPhoneFromRequest(request);
985 if (phone != null) {
986 phone.isNrDualConnectivityEnabled(onCompleted, request.workSource);
987 } else {
988 loge("isNRDualConnectivityEnabled: No phone object");
989 request.result = false;
990 notifyRequester(request);
991 }
992 break;
993 }
994
995 case EVENT_IS_NR_DUAL_CONNECTIVITY_ENABLED_DONE:
996 ar = (AsyncResult) msg.obj;
997 request = (MainThreadRequest) ar.userObj;
998 if (ar.exception == null && ar.result != null) {
999 request.result = ar.result;
1000 } else {
1001 // request.result must be set to something non-null
1002 // for the calling thread to unblock
Sooraj Sasindrandaf060f2021-06-15 14:52:55 -07001003 if (ar.result != null) {
Sooraj Sasindran37444802020-08-11 10:40:43 -07001004 request.result = ar.result;
1005 } else {
1006 request.result = false;
1007 }
1008 if (ar.result == null) {
1009 loge("isNRDualConnectivityEnabled: Empty response");
1010 } else if (ar.exception instanceof CommandException) {
1011 loge("isNRDualConnectivityEnabled: CommandException: "
1012 + ar.exception);
1013 } else {
1014 loge("isNRDualConnectivityEnabled: Unknown exception");
1015 }
1016 }
1017 notifyRequester(request);
1018 break;
1019
Sooraj Sasindrandaf060f2021-06-15 14:52:55 -07001020 case CMD_IS_VONR_ENABLED: {
1021 request = (MainThreadRequest) msg.obj;
1022 onCompleted = obtainMessage(EVENT_IS_VONR_ENABLED_DONE,
1023 request);
1024 Phone phone = getPhoneFromRequest(request);
1025 if (phone != null) {
1026 phone.isVoNrEnabled(onCompleted, request.workSource);
1027 } else {
1028 loge("isVoNrEnabled: No phone object");
1029 request.result = false;
1030 notifyRequester(request);
1031 }
1032 break;
1033 }
1034
1035 case EVENT_IS_VONR_ENABLED_DONE:
1036 ar = (AsyncResult) msg.obj;
1037 request = (MainThreadRequest) ar.userObj;
1038 if (ar.exception == null && ar.result != null) {
1039 request.result = ar.result;
1040 } else {
1041 // request.result must be set to something non-null
1042 // for the calling thread to unblock
1043 if (ar.result != null) {
1044 request.result = ar.result;
1045 } else {
1046 request.result = false;
1047 }
1048 if (ar.result == null) {
1049 loge("isVoNrEnabled: Empty response");
1050 } else if (ar.exception instanceof CommandException) {
1051 loge("isVoNrEnabled: CommandException: "
1052 + ar.exception);
1053 } else {
1054 loge("isVoNrEnabled: Unknown exception");
1055 }
1056 }
1057 notifyRequester(request);
1058 break;
1059
Sooraj Sasindran37444802020-08-11 10:40:43 -07001060 case CMD_ENABLE_NR_DUAL_CONNECTIVITY: {
1061 request = (MainThreadRequest) msg.obj;
1062 onCompleted = obtainMessage(EVENT_ENABLE_NR_DUAL_CONNECTIVITY_DONE, request);
1063 Phone phone = getPhoneFromRequest(request);
1064 if (phone != null) {
1065 phone.setNrDualConnectivityState((int) request.argument, onCompleted,
1066 request.workSource);
1067 } else {
1068 loge("enableNrDualConnectivity: No phone object");
1069 request.result =
1070 TelephonyManager.ENABLE_NR_DUAL_CONNECTIVITY_RADIO_NOT_AVAILABLE;
1071 notifyRequester(request);
1072 }
1073 break;
1074 }
1075
1076 case EVENT_ENABLE_NR_DUAL_CONNECTIVITY_DONE: {
1077 ar = (AsyncResult) msg.obj;
1078 request = (MainThreadRequest) ar.userObj;
1079 if (ar.exception == null) {
1080 request.result =
1081 TelephonyManager.ENABLE_NR_DUAL_CONNECTIVITY_SUCCESS;
1082 } else {
1083 request.result =
1084 TelephonyManager
1085 .ENABLE_NR_DUAL_CONNECTIVITY_RADIO_ERROR;
1086 if (ar.exception instanceof CommandException) {
1087 CommandException.Error error =
1088 ((CommandException) (ar.exception)).getCommandError();
1089 if (error == CommandException.Error.RADIO_NOT_AVAILABLE) {
1090 request.result =
1091 TelephonyManager
1092 .ENABLE_NR_DUAL_CONNECTIVITY_RADIO_NOT_AVAILABLE;
Sooraj Sasindran29654162021-03-03 23:00:01 +00001093 } else if (error == CommandException.Error.REQUEST_NOT_SUPPORTED) {
1094 request.result =
1095 TelephonyManager
1096 .ENABLE_NR_DUAL_CONNECTIVITY_NOT_SUPPORTED;
Sooraj Sasindran37444802020-08-11 10:40:43 -07001097 }
1098 loge("enableNrDualConnectivity" + ": CommandException: "
1099 + ar.exception);
1100 } else {
1101 loge("enableNrDualConnectivity" + ": Unknown exception");
1102 }
1103 }
1104 notifyRequester(request);
1105 break;
1106 }
1107
Sooraj Sasindrandaf060f2021-06-15 14:52:55 -07001108 case CMD_ENABLE_VONR: {
1109 request = (MainThreadRequest) msg.obj;
1110 onCompleted = obtainMessage(EVENT_ENABLE_VONR_DONE, request);
1111 Phone phone = getPhoneFromRequest(request);
1112 if (phone != null) {
1113 phone.setVoNrEnabled((boolean) request.argument, onCompleted,
1114 request.workSource);
1115 } else {
1116 loge("setVoNrEnabled: No phone object");
1117 request.result =
1118 TelephonyManager.ENABLE_VONR_RADIO_NOT_AVAILABLE;
1119 notifyRequester(request);
1120 }
1121 break;
1122 }
1123
1124 case EVENT_ENABLE_VONR_DONE: {
1125 ar = (AsyncResult) msg.obj;
1126 request = (MainThreadRequest) ar.userObj;
1127 if (ar.exception == null) {
1128 request.result = TelephonyManager.ENABLE_VONR_SUCCESS;
1129 } else {
1130 request.result = TelephonyManager.ENABLE_VONR_RADIO_ERROR;
1131 if (ar.exception instanceof CommandException) {
1132 CommandException.Error error =
1133 ((CommandException) (ar.exception)).getCommandError();
1134 if (error == CommandException.Error.RADIO_NOT_AVAILABLE) {
1135 request.result = TelephonyManager.ENABLE_VONR_RADIO_NOT_AVAILABLE;
1136 } else if (error == CommandException.Error.REQUEST_NOT_SUPPORTED) {
1137 request.result = TelephonyManager.ENABLE_VONR_REQUEST_NOT_SUPPORTED;
1138 } else {
1139 request.result = TelephonyManager.ENABLE_VONR_RADIO_ERROR;
1140 }
1141 loge("setVoNrEnabled" + ": CommandException: "
1142 + ar.exception);
1143 } else {
1144 loge("setVoNrEnabled" + ": Unknown exception");
1145 }
1146 }
1147 notifyRequester(request);
1148 break;
1149 }
1150
SongFerngWang3ef3e072020-12-21 16:41:52 +08001151 case CMD_GET_ALLOWED_NETWORK_TYPES_BITMASK:
Jake Hamby7c27be32014-03-03 13:25:59 -08001152 request = (MainThreadRequest) msg.obj;
SongFerngWang3ef3e072020-12-21 16:41:52 +08001153 onCompleted = obtainMessage(EVENT_GET_ALLOWED_NETWORK_TYPES_BITMASK_DONE,
1154 request);
1155 getPhoneFromRequest(request).getAllowedNetworkTypesBitmask(onCompleted);
Jake Hamby7c27be32014-03-03 13:25:59 -08001156 break;
1157
SongFerngWang3ef3e072020-12-21 16:41:52 +08001158 case EVENT_GET_ALLOWED_NETWORK_TYPES_BITMASK_DONE:
Jake Hamby7c27be32014-03-03 13:25:59 -08001159 ar = (AsyncResult) msg.obj;
1160 request = (MainThreadRequest) ar.userObj;
1161 if (ar.exception == null && ar.result != null) {
1162 request.result = ar.result; // Integer
1163 } else {
Nazish Tabassume8ba43a2020-07-28 14:49:25 +05301164 // request.result must be set to something non-null
1165 // for the calling thread to unblock
1166 request.result = new int[]{-1};
Jake Hamby7c27be32014-03-03 13:25:59 -08001167 if (ar.result == null) {
SongFerngWang3ef3e072020-12-21 16:41:52 +08001168 loge("getAllowedNetworkTypesBitmask: Empty response");
Jake Hamby7c27be32014-03-03 13:25:59 -08001169 } else if (ar.exception instanceof CommandException) {
SongFerngWang3ef3e072020-12-21 16:41:52 +08001170 loge("getAllowedNetworkTypesBitmask: CommandException: "
1171 + ar.exception);
Jake Hamby7c27be32014-03-03 13:25:59 -08001172 } else {
SongFerngWang3ef3e072020-12-21 16:41:52 +08001173 loge("getAllowedNetworkTypesBitmask: Unknown exception");
Jake Hamby7c27be32014-03-03 13:25:59 -08001174 }
1175 }
Pengquan Menga1bb6272018-09-06 09:59:22 -07001176 notifyRequester(request);
Jake Hamby7c27be32014-03-03 13:25:59 -08001177 break;
1178
SongFerngWang3ef3e072020-12-21 16:41:52 +08001179 case CMD_SET_ALLOWED_NETWORK_TYPES_FOR_REASON:
Jake Hamby7c27be32014-03-03 13:25:59 -08001180 request = (MainThreadRequest) msg.obj;
SongFerngWang3ef3e072020-12-21 16:41:52 +08001181 onCompleted = obtainMessage(EVENT_SET_ALLOWED_NETWORK_TYPES_FOR_REASON_DONE,
1182 request);
1183 Pair<Integer, Long> reasonWithNetworkTypes =
1184 (Pair<Integer, Long>) request.argument;
1185 getPhoneFromRequest(request).setAllowedNetworkTypes(
1186 reasonWithNetworkTypes.first,
1187 reasonWithNetworkTypes.second,
1188 onCompleted);
Jake Hamby7c27be32014-03-03 13:25:59 -08001189 break;
1190
SongFerngWang3ef3e072020-12-21 16:41:52 +08001191 case EVENT_SET_ALLOWED_NETWORK_TYPES_FOR_REASON_DONE:
1192 handleNullReturnEvent(msg, "setAllowedNetworkTypesForReason");
Jake Hamby7c27be32014-03-03 13:25:59 -08001193 break;
1194
Shuo Qian850e4d6a2018-04-25 21:02:08 +00001195 case CMD_INVOKE_OEM_RIL_REQUEST_RAW:
1196 request = (MainThreadRequest)msg.obj;
1197 onCompleted = obtainMessage(EVENT_INVOKE_OEM_RIL_REQUEST_RAW_DONE, request);
Nazanin Bakhshif782e562018-12-11 15:15:39 -08001198 defaultPhone.invokeOemRilRequestRaw((byte[]) request.argument, onCompleted);
Shuo Qian850e4d6a2018-04-25 21:02:08 +00001199 break;
1200
1201 case EVENT_INVOKE_OEM_RIL_REQUEST_RAW_DONE:
1202 ar = (AsyncResult)msg.obj;
1203 request = (MainThreadRequest)ar.userObj;
1204 request.result = ar;
Pengquan Menga1bb6272018-09-06 09:59:22 -07001205 notifyRequester(request);
Shuo Qian850e4d6a2018-04-25 21:02:08 +00001206 break;
1207
Shishir Agrawal76d5da92014-11-09 16:17:25 -08001208 case CMD_SET_VOICEMAIL_NUMBER:
1209 request = (MainThreadRequest) msg.obj;
1210 onCompleted = obtainMessage(EVENT_SET_VOICEMAIL_NUMBER_DONE, request);
1211 Pair<String, String> tagNum = (Pair<String, String>) request.argument;
Stuart Scott584921c2015-01-15 17:10:34 -08001212 getPhoneFromRequest(request).setVoiceMailNumber(tagNum.first, tagNum.second,
1213 onCompleted);
Shishir Agrawal76d5da92014-11-09 16:17:25 -08001214 break;
1215
1216 case EVENT_SET_VOICEMAIL_NUMBER_DONE:
1217 handleNullReturnEvent(msg, "setVoicemailNumber");
1218 break;
1219
Stuart Scott54788802015-03-30 13:18:01 -07001220 case CMD_SET_NETWORK_SELECTION_MODE_AUTOMATIC:
1221 request = (MainThreadRequest) msg.obj;
1222 onCompleted = obtainMessage(EVENT_SET_NETWORK_SELECTION_MODE_AUTOMATIC_DONE,
1223 request);
1224 getPhoneFromRequest(request).setNetworkSelectionModeAutomatic(onCompleted);
1225 break;
1226
1227 case EVENT_SET_NETWORK_SELECTION_MODE_AUTOMATIC_DONE:
1228 handleNullReturnEvent(msg, "setNetworkSelectionModeAutomatic");
1229 break;
1230
Shishir Agrawal302c8692015-06-19 13:49:39 -07001231 case CMD_PERFORM_NETWORK_SCAN:
1232 request = (MainThreadRequest) msg.obj;
1233 onCompleted = obtainMessage(EVENT_PERFORM_NETWORK_SCAN_DONE, request);
1234 getPhoneFromRequest(request).getAvailableNetworks(onCompleted);
1235 break;
1236
Hall Liu27d24262020-09-18 19:04:59 -07001237 case CMD_GET_CALL_FORWARDING: {
Shuo Qian4a594052020-01-23 11:59:30 -08001238 request = (MainThreadRequest) msg.obj;
1239 onCompleted = obtainMessage(EVENT_GET_CALL_FORWARDING_DONE, request);
Hall Liu27d24262020-09-18 19:04:59 -07001240 Pair<Integer, TelephonyManager.CallForwardingInfoCallback> args =
1241 (Pair<Integer, TelephonyManager.CallForwardingInfoCallback>)
1242 request.argument;
1243 int callForwardingReason = args.first;
1244 request.phone.getCallForwardingOption(callForwardingReason, onCompleted);
Shuo Qian4a594052020-01-23 11:59:30 -08001245 break;
Hall Liu27d24262020-09-18 19:04:59 -07001246 }
1247 case EVENT_GET_CALL_FORWARDING_DONE: {
Shuo Qian4a594052020-01-23 11:59:30 -08001248 ar = (AsyncResult) msg.obj;
1249 request = (MainThreadRequest) ar.userObj;
Hall Liu27d24262020-09-18 19:04:59 -07001250 TelephonyManager.CallForwardingInfoCallback callback =
1251 ((Pair<Integer, TelephonyManager.CallForwardingInfoCallback>)
1252 request.argument).second;
Shuo Qian4a594052020-01-23 11:59:30 -08001253 if (ar.exception == null && ar.result != null) {
Hall Liu27d24262020-09-18 19:04:59 -07001254 CallForwardingInfo callForwardingInfo = null;
Shuo Qian4a594052020-01-23 11:59:30 -08001255 CallForwardInfo[] callForwardInfos = (CallForwardInfo[]) ar.result;
1256 for (CallForwardInfo callForwardInfo : callForwardInfos) {
1257 // Service Class is a bit mask per 3gpp 27.007. Search for
1258 // any service for voice call.
1259 if ((callForwardInfo.serviceClass
1260 & CommandsInterface.SERVICE_CLASS_VOICE) > 0) {
Yuchen Dong69cc1412021-09-27 20:27:01 +08001261 callForwardingInfo = new CallForwardingInfo(
1262 callForwardInfo.status
1263 == CommandsInterface.CF_ACTION_ENABLE,
Hall Liu27d24262020-09-18 19:04:59 -07001264 callForwardInfo.reason,
1265 callForwardInfo.number,
1266 callForwardInfo.timeSeconds);
Shuo Qian4a594052020-01-23 11:59:30 -08001267 break;
1268 }
1269 }
1270 // Didn't find a call forward info for voice call.
1271 if (callForwardingInfo == null) {
Hall Liu27d24262020-09-18 19:04:59 -07001272 callForwardingInfo = new CallForwardingInfo(false /* enabled */,
1273 0 /* reason */, null /* number */, 0 /* timeout */);
Shuo Qian4a594052020-01-23 11:59:30 -08001274 }
Hall Liu27d24262020-09-18 19:04:59 -07001275 callback.onCallForwardingInfoAvailable(callForwardingInfo);
Shuo Qian4a594052020-01-23 11:59:30 -08001276 } else {
1277 if (ar.result == null) {
1278 loge("EVENT_GET_CALL_FORWARDING_DONE: Empty response");
1279 }
1280 if (ar.exception != null) {
1281 loge("EVENT_GET_CALL_FORWARDING_DONE: Exception: " + ar.exception);
1282 }
Hall Liu940c4ca2020-09-29 17:10:18 -07001283 int errorCode = TelephonyManager
1284 .CallForwardingInfoCallback.RESULT_ERROR_UNKNOWN;
Shuo Qian4a594052020-01-23 11:59:30 -08001285 if (ar.exception instanceof CommandException) {
1286 CommandException.Error error =
1287 ((CommandException) (ar.exception)).getCommandError();
1288 if (error == CommandException.Error.FDN_CHECK_FAILURE) {
Hall Liu940c4ca2020-09-29 17:10:18 -07001289 errorCode = TelephonyManager
1290 .CallForwardingInfoCallback.RESULT_ERROR_FDN_CHECK_FAILURE;
Shuo Qian4a594052020-01-23 11:59:30 -08001291 } else if (error == CommandException.Error.REQUEST_NOT_SUPPORTED) {
Hall Liu940c4ca2020-09-29 17:10:18 -07001292 errorCode = TelephonyManager
1293 .CallForwardingInfoCallback.RESULT_ERROR_NOT_SUPPORTED;
Shuo Qian4a594052020-01-23 11:59:30 -08001294 }
1295 }
Hall Liu27d24262020-09-18 19:04:59 -07001296 callback.onError(errorCode);
Shuo Qian4a594052020-01-23 11:59:30 -08001297 }
Shuo Qian4a594052020-01-23 11:59:30 -08001298 break;
Hall Liu27d24262020-09-18 19:04:59 -07001299 }
Shuo Qian4a594052020-01-23 11:59:30 -08001300
Hall Liu27d24262020-09-18 19:04:59 -07001301 case CMD_SET_CALL_FORWARDING: {
Shuo Qian4a594052020-01-23 11:59:30 -08001302 request = (MainThreadRequest) msg.obj;
1303 onCompleted = obtainMessage(EVENT_SET_CALL_FORWARDING_DONE, request);
Hall Liu27d24262020-09-18 19:04:59 -07001304 request = (MainThreadRequest) msg.obj;
Shuo Qian4a594052020-01-23 11:59:30 -08001305 CallForwardingInfo callForwardingInfoToSet =
Hall Liu27d24262020-09-18 19:04:59 -07001306 ((Pair<CallForwardingInfo, Consumer<Integer>>)
1307 request.argument).first;
1308 request.phone.setCallForwardingOption(
1309 callForwardingInfoToSet.isEnabled()
Calvin Pan258f1f72021-07-28 21:46:56 +08001310 ? CommandsInterface.CF_ACTION_REGISTRATION
Hall Liu27d24262020-09-18 19:04:59 -07001311 : CommandsInterface.CF_ACTION_DISABLE,
Shuo Qian4a594052020-01-23 11:59:30 -08001312 callForwardingInfoToSet.getReason(),
1313 callForwardingInfoToSet.getNumber(),
1314 callForwardingInfoToSet.getTimeoutSeconds(), onCompleted);
1315 break;
Hall Liu27d24262020-09-18 19:04:59 -07001316 }
Shuo Qian4a594052020-01-23 11:59:30 -08001317
Hall Liu27d24262020-09-18 19:04:59 -07001318 case EVENT_SET_CALL_FORWARDING_DONE: {
Shuo Qian4a594052020-01-23 11:59:30 -08001319 ar = (AsyncResult) msg.obj;
1320 request = (MainThreadRequest) ar.userObj;
Hall Liu27d24262020-09-18 19:04:59 -07001321 Consumer<Integer> callback =
1322 ((Pair<CallForwardingInfo, Consumer<Integer>>)
1323 request.argument).second;
1324 if (ar.exception != null) {
Shuo Qian4a594052020-01-23 11:59:30 -08001325 loge("setCallForwarding exception: " + ar.exception);
Hall Liu940c4ca2020-09-29 17:10:18 -07001326 int errorCode = TelephonyManager.CallForwardingInfoCallback
1327 .RESULT_ERROR_UNKNOWN;
Hall Liu27d24262020-09-18 19:04:59 -07001328 if (ar.exception instanceof CommandException) {
1329 CommandException.Error error =
1330 ((CommandException) (ar.exception)).getCommandError();
1331 if (error == CommandException.Error.FDN_CHECK_FAILURE) {
Hall Liu940c4ca2020-09-29 17:10:18 -07001332 errorCode = TelephonyManager.CallForwardingInfoCallback
1333 .RESULT_ERROR_FDN_CHECK_FAILURE;
Hall Liu27d24262020-09-18 19:04:59 -07001334 } else if (error == CommandException.Error.REQUEST_NOT_SUPPORTED) {
Hall Liu940c4ca2020-09-29 17:10:18 -07001335 errorCode = TelephonyManager.CallForwardingInfoCallback
1336 .RESULT_ERROR_NOT_SUPPORTED;
Hall Liu27d24262020-09-18 19:04:59 -07001337 }
1338 }
1339 callback.accept(errorCode);
1340 } else {
Hall Liu940c4ca2020-09-29 17:10:18 -07001341 callback.accept(TelephonyManager.CallForwardingInfoCallback.RESULT_SUCCESS);
Shuo Qian4a594052020-01-23 11:59:30 -08001342 }
Shuo Qian4a594052020-01-23 11:59:30 -08001343 break;
Hall Liu27d24262020-09-18 19:04:59 -07001344 }
Shuo Qian4a594052020-01-23 11:59:30 -08001345
Hall Liu27d24262020-09-18 19:04:59 -07001346 case CMD_GET_CALL_WAITING: {
Shuo Qian4a594052020-01-23 11:59:30 -08001347 request = (MainThreadRequest) msg.obj;
1348 onCompleted = obtainMessage(EVENT_GET_CALL_WAITING_DONE, request);
1349 getPhoneFromRequest(request).getCallWaiting(onCompleted);
1350 break;
Hall Liu27d24262020-09-18 19:04:59 -07001351 }
Shuo Qian4a594052020-01-23 11:59:30 -08001352
Hall Liu27d24262020-09-18 19:04:59 -07001353 case EVENT_GET_CALL_WAITING_DONE: {
Shuo Qian4a594052020-01-23 11:59:30 -08001354 ar = (AsyncResult) msg.obj;
1355 request = (MainThreadRequest) ar.userObj;
Hall Liu27d24262020-09-18 19:04:59 -07001356 Consumer<Integer> callback = (Consumer<Integer>) request.argument;
SongFerngWangebda2c52022-01-11 15:28:38 +08001357 int callWaitingStatus = TelephonyManager.CALL_WAITING_STATUS_UNKNOWN_ERROR;
Shuo Qian4a594052020-01-23 11:59:30 -08001358 if (ar.exception == null && ar.result != null) {
Shuo Qiand6a0dba2020-02-18 18:13:49 -08001359 int[] callForwardResults = (int[]) ar.result;
Shuo Qian4a594052020-01-23 11:59:30 -08001360 // Service Class is a bit mask per 3gpp 27.007.
1361 // Search for any service for voice call.
Shuo Qiand6a0dba2020-02-18 18:13:49 -08001362 if (callForwardResults.length > 1
1363 && ((callForwardResults[1]
Hall Liu27d24262020-09-18 19:04:59 -07001364 & CommandsInterface.SERVICE_CLASS_VOICE) > 0)) {
SongFerngWangebda2c52022-01-11 15:28:38 +08001365 callWaitingStatus = callForwardResults[0] == 0
Hall Liu27d24262020-09-18 19:04:59 -07001366 ? TelephonyManager.CALL_WAITING_STATUS_DISABLED
1367 : TelephonyManager.CALL_WAITING_STATUS_ENABLED;
Shuo Qian4a594052020-01-23 11:59:30 -08001368 } else {
SongFerngWangebda2c52022-01-11 15:28:38 +08001369 callWaitingStatus = TelephonyManager.CALL_WAITING_STATUS_DISABLED;
Shuo Qian4a594052020-01-23 11:59:30 -08001370 }
1371 } else {
1372 if (ar.result == null) {
1373 loge("EVENT_GET_CALL_WAITING_DONE: Empty response");
1374 }
1375 if (ar.exception != null) {
1376 loge("EVENT_GET_CALL_WAITING_DONE: Exception: " + ar.exception);
1377 }
1378 if (ar.exception instanceof CommandException) {
1379 CommandException.Error error =
1380 ((CommandException) (ar.exception)).getCommandError();
1381 if (error == CommandException.Error.REQUEST_NOT_SUPPORTED) {
SongFerngWangebda2c52022-01-11 15:28:38 +08001382 callWaitingStatus =
Shuo Qian4a594052020-01-23 11:59:30 -08001383 TelephonyManager.CALL_WAITING_STATUS_NOT_SUPPORTED;
SongFerngWangebda2c52022-01-11 15:28:38 +08001384 } else if (error == CommandException.Error.FDN_CHECK_FAILURE) {
1385 callWaitingStatus =
1386 TelephonyManager.CALL_WAITING_STATUS_FDN_CHECK_FAILURE;
Shuo Qian4a594052020-01-23 11:59:30 -08001387 }
1388 }
1389 }
SongFerngWangebda2c52022-01-11 15:28:38 +08001390 callback.accept(callWaitingStatus);
Shuo Qian4a594052020-01-23 11:59:30 -08001391 break;
Hall Liu27d24262020-09-18 19:04:59 -07001392 }
Shuo Qian4a594052020-01-23 11:59:30 -08001393
Hall Liu27d24262020-09-18 19:04:59 -07001394 case CMD_SET_CALL_WAITING: {
Shuo Qian4a594052020-01-23 11:59:30 -08001395 request = (MainThreadRequest) msg.obj;
1396 onCompleted = obtainMessage(EVENT_SET_CALL_WAITING_DONE, request);
Hall Liu27d24262020-09-18 19:04:59 -07001397 boolean enable = ((Pair<Boolean, Consumer<Integer>>) request.argument).first;
1398 getPhoneFromRequest(request).setCallWaiting(enable, onCompleted);
Shuo Qian4a594052020-01-23 11:59:30 -08001399 break;
Hall Liu27d24262020-09-18 19:04:59 -07001400 }
Shuo Qian4a594052020-01-23 11:59:30 -08001401
Hall Liu27d24262020-09-18 19:04:59 -07001402 case EVENT_SET_CALL_WAITING_DONE: {
Shuo Qian4a594052020-01-23 11:59:30 -08001403 ar = (AsyncResult) msg.obj;
1404 request = (MainThreadRequest) ar.userObj;
Hall Liu27d24262020-09-18 19:04:59 -07001405 boolean enable = ((Pair<Boolean, Consumer<Integer>>) request.argument).first;
1406 Consumer<Integer> callback =
1407 ((Pair<Boolean, Consumer<Integer>>) request.argument).second;
1408 if (ar.exception != null) {
Shuo Qian4a594052020-01-23 11:59:30 -08001409 loge("setCallWaiting exception: " + ar.exception);
Hall Liu27d24262020-09-18 19:04:59 -07001410 if (ar.exception instanceof CommandException) {
1411 CommandException.Error error =
1412 ((CommandException) (ar.exception)).getCommandError();
1413 if (error == CommandException.Error.REQUEST_NOT_SUPPORTED) {
1414 callback.accept(TelephonyManager.CALL_WAITING_STATUS_NOT_SUPPORTED);
SongFerngWangebda2c52022-01-11 15:28:38 +08001415 } else if (error == CommandException.Error.FDN_CHECK_FAILURE) {
1416 callback.accept(
1417 TelephonyManager.CALL_WAITING_STATUS_FDN_CHECK_FAILURE);
Hall Liu27d24262020-09-18 19:04:59 -07001418 } else {
1419 callback.accept(TelephonyManager.CALL_WAITING_STATUS_UNKNOWN_ERROR);
1420 }
1421 } else {
1422 callback.accept(TelephonyManager.CALL_WAITING_STATUS_UNKNOWN_ERROR);
1423 }
1424 } else {
1425 callback.accept(enable ? TelephonyManager.CALL_WAITING_STATUS_ENABLED
1426 : TelephonyManager.CALL_WAITING_STATUS_DISABLED);
Shuo Qian4a594052020-01-23 11:59:30 -08001427 }
Shuo Qian4a594052020-01-23 11:59:30 -08001428 break;
Hall Liu27d24262020-09-18 19:04:59 -07001429 }
Shishir Agrawal302c8692015-06-19 13:49:39 -07001430 case EVENT_PERFORM_NETWORK_SCAN_DONE:
1431 ar = (AsyncResult) msg.obj;
1432 request = (MainThreadRequest) ar.userObj;
1433 CellNetworkScanResult cellScanResult;
1434 if (ar.exception == null && ar.result != null) {
1435 cellScanResult = new CellNetworkScanResult(
1436 CellNetworkScanResult.STATUS_SUCCESS,
1437 (List<OperatorInfo>) ar.result);
1438 } else {
1439 if (ar.result == null) {
1440 loge("getCellNetworkScanResults: Empty response");
1441 }
1442 if (ar.exception != null) {
1443 loge("getCellNetworkScanResults: Exception: " + ar.exception);
1444 }
1445 int errorCode = CellNetworkScanResult.STATUS_UNKNOWN_ERROR;
1446 if (ar.exception instanceof CommandException) {
1447 CommandException.Error error =
1448 ((CommandException) (ar.exception)).getCommandError();
1449 if (error == CommandException.Error.RADIO_NOT_AVAILABLE) {
1450 errorCode = CellNetworkScanResult.STATUS_RADIO_NOT_AVAILABLE;
1451 } else if (error == CommandException.Error.GENERIC_FAILURE) {
1452 errorCode = CellNetworkScanResult.STATUS_RADIO_GENERIC_FAILURE;
1453 }
1454 }
1455 cellScanResult = new CellNetworkScanResult(errorCode, null);
1456 }
1457 request.result = cellScanResult;
Pengquan Menga1bb6272018-09-06 09:59:22 -07001458 notifyRequester(request);
Shishir Agrawal302c8692015-06-19 13:49:39 -07001459 break;
1460
1461 case CMD_SET_NETWORK_SELECTION_MODE_MANUAL:
1462 request = (MainThreadRequest) msg.obj;
Shishir Agrawal77ba3172015-09-10 14:50:19 -07001463 ManualNetworkSelectionArgument selArg =
1464 (ManualNetworkSelectionArgument) request.argument;
Shishir Agrawal302c8692015-06-19 13:49:39 -07001465 onCompleted = obtainMessage(EVENT_SET_NETWORK_SELECTION_MODE_MANUAL_DONE,
1466 request);
Shishir Agrawal77ba3172015-09-10 14:50:19 -07001467 getPhoneFromRequest(request).selectNetworkManually(selArg.operatorInfo,
1468 selArg.persistSelection, onCompleted);
Shishir Agrawal302c8692015-06-19 13:49:39 -07001469 break;
1470
1471 case EVENT_SET_NETWORK_SELECTION_MODE_MANUAL_DONE:
Pengquan Menge3d01e22018-09-20 15:25:35 -07001472 ar = (AsyncResult) msg.obj;
1473 request = (MainThreadRequest) ar.userObj;
1474 if (ar.exception == null) {
1475 request.result = true;
1476 } else {
1477 request.result = false;
1478 loge("setNetworkSelectionModeManual " + ar.exception);
1479 }
1480 notifyRequester(request);
1481 mApp.onNetworkSelectionChanged(request.subId);
Shishir Agrawal302c8692015-06-19 13:49:39 -07001482 break;
1483
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -07001484 case CMD_GET_MODEM_ACTIVITY_INFO:
1485 request = (MainThreadRequest) msg.obj;
1486 onCompleted = obtainMessage(EVENT_GET_MODEM_ACTIVITY_INFO_DONE, request);
James Mattisab947702019-04-03 14:18:34 -07001487 if (defaultPhone != null) {
1488 defaultPhone.getModemActivityInfo(onCompleted, request.workSource);
Shuo Qian8f4750a2020-02-20 17:12:10 -08001489 } else {
1490 ResultReceiver result = (ResultReceiver) request.argument;
1491 Bundle bundle = new Bundle();
1492 bundle.putParcelable(TelephonyManager.MODEM_ACTIVITY_RESULT_KEY,
Hall Liu49656c02020-10-09 19:00:11 -07001493 new ModemActivityInfo(0, 0, 0,
1494 new int[ModemActivityInfo.getNumTxPowerLevels()], 0));
Shuo Qian8f4750a2020-02-20 17:12:10 -08001495 result.send(0, bundle);
James Mattisab947702019-04-03 14:18:34 -07001496 }
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -07001497 break;
1498
Hall Liud0f208c2020-10-14 16:54:44 -07001499 case EVENT_GET_MODEM_ACTIVITY_INFO_DONE: {
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -07001500 ar = (AsyncResult) msg.obj;
1501 request = (MainThreadRequest) ar.userObj;
Shuo Qian8f4750a2020-02-20 17:12:10 -08001502 ResultReceiver result = (ResultReceiver) request.argument;
Hall Liud0f208c2020-10-14 16:54:44 -07001503 int error = 0;
Kai Shi917fdc62022-11-28 14:01:02 -08001504 ModemActivityInfo ret = null;
Gary Jian3aa9a762022-01-24 16:41:19 +08001505 if (mLastModemActivityInfo == null) {
1506 mLastModemActivitySpecificInfo = new ActivityStatsTechSpecificInfo[1];
1507 mLastModemActivitySpecificInfo[0] =
1508 new ActivityStatsTechSpecificInfo(
1509 0,
1510 0,
1511 new int[ModemActivityInfo.getNumTxPowerLevels()],
1512 0);
1513 mLastModemActivityInfo =
1514 new ModemActivityInfo(0, 0, 0, mLastModemActivitySpecificInfo);
1515 }
1516
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -07001517 if (ar.exception == null && ar.result != null) {
Shuo Qian8f4750a2020-02-20 17:12:10 -08001518 // Update the last modem activity info and the result of the request.
1519 ModemActivityInfo info = (ModemActivityInfo) ar.result;
1520 if (isModemActivityInfoValid(info)) {
Gary Jian3aa9a762022-01-24 16:41:19 +08001521 mergeModemActivityInfo(info);
Gary Jian76280a42022-12-07 16:18:33 +08001522 } else {
1523 loge("queryModemActivityInfo: invalid response");
Shuo Qian8f4750a2020-02-20 17:12:10 -08001524 }
Kai Shi917fdc62022-11-28 14:01:02 -08001525 // This is needed to decouple ret from mLastModemActivityInfo
1526 // We don't want to return mLastModemActivityInfo which is updated
1527 // inside mergeModemActivityInfo()
1528 ret = new ModemActivityInfo(
1529 mLastModemActivityInfo.getTimestampMillis(),
1530 mLastModemActivityInfo.getSleepTimeMillis(),
1531 mLastModemActivityInfo.getIdleTimeMillis(),
1532 deepCopyModemActivitySpecificInfo(mLastModemActivitySpecificInfo));
Gary Jian3aa9a762022-01-24 16:41:19 +08001533
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -07001534 } else {
1535 if (ar.result == null) {
1536 loge("queryModemActivityInfo: Empty response");
Hall Liud0f208c2020-10-14 16:54:44 -07001537 error = TelephonyManager.ModemActivityInfoException
1538 .ERROR_INVALID_INFO_RECEIVED;
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -07001539 } else if (ar.exception instanceof CommandException) {
Gary Jian3aa9a762022-01-24 16:41:19 +08001540 loge("queryModemActivityInfo: CommandException: " + ar.exception);
Hall Liud0f208c2020-10-14 16:54:44 -07001541 error = TelephonyManager.ModemActivityInfoException
1542 .ERROR_MODEM_RESPONSE_ERROR;
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -07001543 } else {
1544 loge("queryModemActivityInfo: Unknown exception");
Hall Liud0f208c2020-10-14 16:54:44 -07001545 error = TelephonyManager.ModemActivityInfoException
1546 .ERROR_UNKNOWN;
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -07001547 }
1548 }
Shuo Qian8f4750a2020-02-20 17:12:10 -08001549 Bundle bundle = new Bundle();
Kai Shi917fdc62022-11-28 14:01:02 -08001550 if (ret != null) {
Gary Jian3aa9a762022-01-24 16:41:19 +08001551 bundle.putParcelable(
1552 TelephonyManager.MODEM_ACTIVITY_RESULT_KEY,
Kai Shi917fdc62022-11-28 14:01:02 -08001553 ret);
Hall Liud0f208c2020-10-14 16:54:44 -07001554 } else {
1555 bundle.putInt(TelephonyManager.EXCEPTION_RESULT_KEY, error);
1556 }
Shuo Qian8f4750a2020-02-20 17:12:10 -08001557 result.send(0, bundle);
Pengquan Menga1bb6272018-09-06 09:59:22 -07001558 notifyRequester(request);
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -07001559 break;
Hall Liud0f208c2020-10-14 16:54:44 -07001560 }
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -07001561
Meng Wang1a7c35a2016-05-05 20:56:15 -07001562 case CMD_SET_ALLOWED_CARRIERS:
1563 request = (MainThreadRequest) msg.obj;
Michele Berionne482f8202018-11-27 18:57:59 -08001564 CarrierRestrictionRules argument =
1565 (CarrierRestrictionRules) request.argument;
Meng Wang1a7c35a2016-05-05 20:56:15 -07001566 onCompleted = obtainMessage(EVENT_SET_ALLOWED_CARRIERS_DONE, request);
Michele Berionne482f8202018-11-27 18:57:59 -08001567 defaultPhone.setAllowedCarriers(argument, onCompleted, request.workSource);
Meng Wang1a7c35a2016-05-05 20:56:15 -07001568 break;
1569
1570 case EVENT_SET_ALLOWED_CARRIERS_DONE:
1571 ar = (AsyncResult) msg.obj;
1572 request = (MainThreadRequest) ar.userObj;
1573 if (ar.exception == null && ar.result != null) {
1574 request.result = ar.result;
1575 } else {
Michele Berionne482f8202018-11-27 18:57:59 -08001576 request.result = TelephonyManager.SET_CARRIER_RESTRICTION_ERROR;
1577 if (ar.exception instanceof CommandException) {
1578 loge("setAllowedCarriers: CommandException: " + ar.exception);
1579 CommandException.Error error =
1580 ((CommandException) (ar.exception)).getCommandError();
1581 if (error == CommandException.Error.REQUEST_NOT_SUPPORTED) {
1582 request.result =
1583 TelephonyManager.SET_CARRIER_RESTRICTION_NOT_SUPPORTED;
1584 }
Meng Wang1a7c35a2016-05-05 20:56:15 -07001585 } else {
1586 loge("setAllowedCarriers: Unknown exception");
1587 }
1588 }
Pengquan Menga1bb6272018-09-06 09:59:22 -07001589 notifyRequester(request);
Meng Wang1a7c35a2016-05-05 20:56:15 -07001590 break;
1591
1592 case CMD_GET_ALLOWED_CARRIERS:
1593 request = (MainThreadRequest) msg.obj;
1594 onCompleted = obtainMessage(EVENT_GET_ALLOWED_CARRIERS_DONE, request);
Nazanin Bakhshif782e562018-12-11 15:15:39 -08001595 defaultPhone.getAllowedCarriers(onCompleted, request.workSource);
Meng Wang1a7c35a2016-05-05 20:56:15 -07001596 break;
1597
1598 case EVENT_GET_ALLOWED_CARRIERS_DONE:
1599 ar = (AsyncResult) msg.obj;
1600 request = (MainThreadRequest) ar.userObj;
1601 if (ar.exception == null && ar.result != null) {
1602 request.result = ar.result;
1603 } else {
Michele Berionne482f8202018-11-27 18:57:59 -08001604 request.result = new IllegalStateException(
1605 "Failed to get carrier restrictions");
Meng Wang1a7c35a2016-05-05 20:56:15 -07001606 if (ar.result == null) {
1607 loge("getAllowedCarriers: Empty response");
1608 } else if (ar.exception instanceof CommandException) {
1609 loge("getAllowedCarriers: CommandException: " +
1610 ar.exception);
1611 } else {
1612 loge("getAllowedCarriers: Unknown exception");
1613 }
1614 }
arunvoddud7401012022-12-15 16:08:12 +00001615 if (request.argument != null) {
1616 // This is for the implementation of carrierRestrictionStatus.
1617 CallerCallbackInfo callbackInfo = (CallerCallbackInfo) request.argument;
1618 Consumer<Integer> callback = callbackInfo.getConsumer();
1619 int callerCarrierId = callbackInfo.getCarrierId();
1620 int lockStatus = TelephonyManager.CARRIER_RESTRICTION_STATUS_UNKNOWN;
1621 if (ar.exception == null && ar.result instanceof CarrierRestrictionRules) {
1622 CarrierRestrictionRules carrierRestrictionRules =
1623 (CarrierRestrictionRules) ar.result;
1624 int carrierId = -1;
1625 try {
1626 CarrierIdentifier carrierIdentifier =
1627 carrierRestrictionRules.getAllowedCarriers().get(0);
1628 carrierId = CarrierResolver.getCarrierIdFromIdentifier(mApp,
1629 carrierIdentifier);
1630 } catch (NullPointerException | IndexOutOfBoundsException ex) {
1631 Rlog.e(LOG_TAG, "CarrierIdentifier exception = " + ex);
1632 }
1633 lockStatus = carrierRestrictionRules.getCarrierRestrictionStatus();
1634 if (carrierId != -1 && callerCarrierId == carrierId && lockStatus
1635 == TelephonyManager.CARRIER_RESTRICTION_STATUS_RESTRICTED) {
1636 lockStatus =
1637 TelephonyManager.CARRIER_RESTRICTION_STATUS_RESTRICTED_TO_CALLER;
1638 }
1639 } else {
1640 Rlog.e(LOG_TAG,
1641 "getCarrierRestrictionStatus: exception ex = " + ar.exception);
1642 }
1643 callback.accept(lockStatus);
1644 } else {
1645 // This is for the implementation of getAllowedCarriers.
1646 notifyRequester(request);
1647 }
Meng Wang1a7c35a2016-05-05 20:56:15 -07001648 break;
1649
Nathan Haroldb3014052017-01-25 15:57:32 -08001650 case EVENT_GET_FORBIDDEN_PLMNS_DONE:
1651 ar = (AsyncResult) msg.obj;
1652 request = (MainThreadRequest) ar.userObj;
1653 if (ar.exception == null && ar.result != null) {
1654 request.result = ar.result;
1655 } else {
1656 request.result = new IllegalArgumentException(
1657 "Failed to retrieve Forbidden Plmns");
1658 if (ar.result == null) {
1659 loge("getForbiddenPlmns: Empty response");
1660 } else {
1661 loge("getForbiddenPlmns: Unknown exception");
1662 }
1663 }
Pengquan Menga1bb6272018-09-06 09:59:22 -07001664 notifyRequester(request);
Nathan Haroldb3014052017-01-25 15:57:32 -08001665 break;
1666
1667 case CMD_GET_FORBIDDEN_PLMNS:
1668 request = (MainThreadRequest) msg.obj;
Muralidhar Reddy472c2ae2021-09-29 19:38:40 +00001669 uiccPort = getUiccPortFromRequest(request);
1670 if (uiccPort == null) {
1671 loge("getForbiddenPlmns() UiccPort is null");
Nathan Haroldb3014052017-01-25 15:57:32 -08001672 request.result = new IllegalArgumentException(
Muralidhar Reddy472c2ae2021-09-29 19:38:40 +00001673 "getForbiddenPlmns() UiccPort is null");
Pengquan Menga1bb6272018-09-06 09:59:22 -07001674 notifyRequester(request);
Nathan Haroldb3014052017-01-25 15:57:32 -08001675 break;
1676 }
1677 Integer appType = (Integer) request.argument;
Muralidhar Reddy472c2ae2021-09-29 19:38:40 +00001678 UiccCardApplication uiccApp = uiccPort.getApplicationByType(appType);
Nathan Haroldb3014052017-01-25 15:57:32 -08001679 if (uiccApp == null) {
1680 loge("getForbiddenPlmns() no app with specified type -- "
1681 + appType);
1682 request.result = new IllegalArgumentException("Failed to get UICC App");
Pengquan Menga1bb6272018-09-06 09:59:22 -07001683 notifyRequester(request);
Nathan Haroldb3014052017-01-25 15:57:32 -08001684 break;
1685 } else {
1686 if (DBG) logv("getForbiddenPlmns() found app " + uiccApp.getAid()
1687 + " specified type -- " + appType);
1688 }
1689 onCompleted = obtainMessage(EVENT_GET_FORBIDDEN_PLMNS_DONE, request);
1690 ((SIMRecords) uiccApp.getIccRecords()).getForbiddenPlmns(
1691 onCompleted);
1692 break;
1693
Holly Jiuyu Sun01c47ad2018-01-24 17:56:33 +00001694 case CMD_SWITCH_SLOTS:
1695 request = (MainThreadRequest) msg.obj;
Muralidhar Reddyeb809e32021-11-19 03:07:54 +00001696 List<UiccSlotMapping> slotMapping = (List<UiccSlotMapping>) request.argument;
Holly Jiuyu Sun01c47ad2018-01-24 17:56:33 +00001697 onCompleted = obtainMessage(EVENT_SWITCH_SLOTS_DONE, request);
Muralidhar Reddyeb809e32021-11-19 03:07:54 +00001698 UiccController.getInstance().switchSlots(slotMapping, onCompleted);
Holly Jiuyu Sun01c47ad2018-01-24 17:56:33 +00001699 break;
1700
1701 case EVENT_SWITCH_SLOTS_DONE:
1702 ar = (AsyncResult) msg.obj;
1703 request = (MainThreadRequest) ar.userObj;
1704 request.result = (ar.exception == null);
Pengquan Menga1bb6272018-09-06 09:59:22 -07001705 notifyRequester(request);
1706 break;
1707 case CMD_GET_NETWORK_SELECTION_MODE:
1708 request = (MainThreadRequest) msg.obj;
1709 onCompleted = obtainMessage(EVENT_GET_NETWORK_SELECTION_MODE_DONE, request);
1710 getPhoneFromRequest(request).getNetworkSelectionMode(onCompleted);
1711 break;
1712
1713 case EVENT_GET_NETWORK_SELECTION_MODE_DONE:
1714 ar = (AsyncResult) msg.obj;
1715 request = (MainThreadRequest) ar.userObj;
1716 if (ar.exception != null) {
1717 request.result = TelephonyManager.NETWORK_SELECTION_MODE_UNKNOWN;
1718 } else {
1719 int mode = ((int[]) ar.result)[0];
1720 if (mode == 0) {
1721 request.result = TelephonyManager.NETWORK_SELECTION_MODE_AUTO;
1722 } else {
1723 request.result = TelephonyManager.NETWORK_SELECTION_MODE_MANUAL;
1724 }
Holly Jiuyu Sun01c47ad2018-01-24 17:56:33 +00001725 }
Pengquan Menga1bb6272018-09-06 09:59:22 -07001726 notifyRequester(request);
1727 break;
1728 case CMD_GET_CDMA_ROAMING_MODE:
1729 request = (MainThreadRequest) msg.obj;
1730 onCompleted = obtainMessage(EVENT_GET_CDMA_ROAMING_MODE_DONE, request);
1731 getPhoneFromRequest(request).queryCdmaRoamingPreference(onCompleted);
1732 break;
1733 case EVENT_GET_CDMA_ROAMING_MODE_DONE:
1734 ar = (AsyncResult) msg.obj;
1735 request = (MainThreadRequest) ar.userObj;
1736 if (ar.exception != null) {
1737 request.result = TelephonyManager.CDMA_ROAMING_MODE_RADIO_DEFAULT;
1738 } else {
1739 request.result = ((int[]) ar.result)[0];
1740 }
1741 notifyRequester(request);
1742 break;
1743 case CMD_SET_CDMA_ROAMING_MODE:
1744 request = (MainThreadRequest) msg.obj;
1745 onCompleted = obtainMessage(EVENT_SET_CDMA_ROAMING_MODE_DONE, request);
1746 int mode = (int) request.argument;
1747 getPhoneFromRequest(request).setCdmaRoamingPreference(mode, onCompleted);
1748 break;
1749 case EVENT_SET_CDMA_ROAMING_MODE_DONE:
1750 ar = (AsyncResult) msg.obj;
1751 request = (MainThreadRequest) ar.userObj;
1752 request.result = ar.exception == null;
1753 notifyRequester(request);
1754 break;
Sarah Chinbaab1432020-10-28 13:46:24 -07001755 case CMD_GET_CDMA_SUBSCRIPTION_MODE:
1756 request = (MainThreadRequest) msg.obj;
1757 onCompleted = obtainMessage(EVENT_GET_CDMA_SUBSCRIPTION_MODE_DONE, request);
1758 getPhoneFromRequest(request).queryCdmaSubscriptionMode(onCompleted);
1759 break;
1760 case EVENT_GET_CDMA_SUBSCRIPTION_MODE_DONE:
1761 ar = (AsyncResult) msg.obj;
1762 request = (MainThreadRequest) ar.userObj;
1763 if (ar.exception != null) {
1764 request.result = TelephonyManager.CDMA_SUBSCRIPTION_RUIM_SIM;
1765 } else {
1766 request.result = ((int[]) ar.result)[0];
1767 }
1768 notifyRequester(request);
1769 break;
Pengquan Menga1bb6272018-09-06 09:59:22 -07001770 case CMD_SET_CDMA_SUBSCRIPTION_MODE:
1771 request = (MainThreadRequest) msg.obj;
1772 onCompleted = obtainMessage(EVENT_SET_CDMA_SUBSCRIPTION_MODE_DONE, request);
1773 int subscriptionMode = (int) request.argument;
Sarah Chinbaab1432020-10-28 13:46:24 -07001774 getPhoneFromRequest(request).setCdmaSubscriptionMode(
1775 subscriptionMode, onCompleted);
Pengquan Menga1bb6272018-09-06 09:59:22 -07001776 break;
1777 case EVENT_SET_CDMA_SUBSCRIPTION_MODE_DONE:
1778 ar = (AsyncResult) msg.obj;
1779 request = (MainThreadRequest) ar.userObj;
1780 request.result = ar.exception == null;
1781 notifyRequester(request);
Holly Jiuyu Sun01c47ad2018-01-24 17:56:33 +00001782 break;
Nathan Harold3ff88932018-08-14 10:19:49 -07001783 case CMD_GET_ALL_CELL_INFO:
1784 request = (MainThreadRequest) msg.obj;
Nathan Harold3ff88932018-08-14 10:19:49 -07001785 onCompleted = obtainMessage(EVENT_GET_ALL_CELL_INFO_DONE, request);
Nathan Harold92bed182018-10-12 18:16:49 -07001786 request.phone.requestCellInfoUpdate(request.workSource, onCompleted);
Nathan Harold3ff88932018-08-14 10:19:49 -07001787 break;
Nathan Harold3ff88932018-08-14 10:19:49 -07001788 case EVENT_GET_ALL_CELL_INFO_DONE:
1789 ar = (AsyncResult) msg.obj;
1790 request = (MainThreadRequest) ar.userObj;
Nathan Harold8d0f1742018-10-02 12:14:47 -07001791 // If a timeout occurs, the response will be null
1792 request.result = (ar.exception == null && ar.result != null)
1793 ? ar.result : new ArrayList<CellInfo>();
Nathan Harold3ff88932018-08-14 10:19:49 -07001794 synchronized (request) {
1795 request.notifyAll();
1796 }
1797 break;
Nathan Haroldfa8da0f2018-09-27 18:51:29 -07001798 case CMD_REQUEST_CELL_INFO_UPDATE:
1799 request = (MainThreadRequest) msg.obj;
1800 request.phone.requestCellInfoUpdate(request.workSource,
1801 obtainMessage(EVENT_REQUEST_CELL_INFO_UPDATE_DONE, request));
1802 break;
1803 case EVENT_REQUEST_CELL_INFO_UPDATE_DONE:
1804 ar = (AsyncResult) msg.obj;
1805 request = (MainThreadRequest) ar.userObj;
1806 ICellInfoCallback cb = (ICellInfoCallback) request.argument;
1807 try {
1808 if (ar.exception != null) {
Nathan Haroldfa8da0f2018-09-27 18:51:29 -07001809 Log.e(LOG_TAG, "Exception retrieving CellInfo=" + ar.exception);
Meng Wangd8921f42019-09-30 17:13:54 -07001810 cb.onError(
1811 TelephonyManager.CellInfoCallback.ERROR_MODEM_ERROR,
1812 ar.exception.getClass().getName(),
1813 ar.exception.toString());
Nathan Haroldfa8da0f2018-09-27 18:51:29 -07001814 } else if (ar.result == null) {
Nathan Haroldfa8da0f2018-09-27 18:51:29 -07001815 Log.w(LOG_TAG, "Timeout Waiting for CellInfo!");
Meng Wangd8921f42019-09-30 17:13:54 -07001816 cb.onError(TelephonyManager.CellInfoCallback.ERROR_TIMEOUT, null, null);
Nathan Haroldfa8da0f2018-09-27 18:51:29 -07001817 } else {
1818 // use the result as returned
1819 cb.onCellInfo((List<CellInfo>) ar.result);
1820 }
1821 } catch (RemoteException re) {
1822 Log.w(LOG_TAG, "Discarded CellInfo due to Callback RemoteException");
1823 }
1824 break;
Sarah Chin679c08a2020-11-18 13:39:35 -08001825 case CMD_GET_CELL_LOCATION: {
Nathan Harold3ff88932018-08-14 10:19:49 -07001826 request = (MainThreadRequest) msg.obj;
1827 WorkSource ws = (WorkSource) request.argument;
1828 Phone phone = getPhoneFromRequest(request);
Meng Wanga10e89e2019-12-09 13:13:01 -08001829 phone.getCellIdentity(ws, obtainMessage(EVENT_GET_CELL_LOCATION_DONE, request));
Nathan Harold3ff88932018-08-14 10:19:49 -07001830 break;
Sarah Chin679c08a2020-11-18 13:39:35 -08001831 }
1832 case EVENT_GET_CELL_LOCATION_DONE: {
Nathan Harold3ff88932018-08-14 10:19:49 -07001833 ar = (AsyncResult) msg.obj;
1834 request = (MainThreadRequest) ar.userObj;
1835 if (ar.exception == null) {
1836 request.result = ar.result;
1837 } else {
Sarah Chin679c08a2020-11-18 13:39:35 -08001838 Phone phone = getPhoneFromRequest(request);
Nathan Harold3ff88932018-08-14 10:19:49 -07001839 request.result = (phone.getPhoneType() == PhoneConstants.PHONE_TYPE_CDMA)
Meng Wanga10e89e2019-12-09 13:13:01 -08001840 ? new CellIdentityCdma() : new CellIdentityGsm();
Nathan Harold3ff88932018-08-14 10:19:49 -07001841 }
1842
1843 synchronized (request) {
1844 request.notifyAll();
1845 }
1846 break;
Sarah Chin679c08a2020-11-18 13:39:35 -08001847 }
chen xu6dac5ab2018-10-26 17:39:23 -07001848 case CMD_MODEM_REBOOT:
1849 request = (MainThreadRequest) msg.obj;
1850 onCompleted = obtainMessage(EVENT_RESET_MODEM_CONFIG_DONE, request);
Nazanin Bakhshif782e562018-12-11 15:15:39 -08001851 defaultPhone.rebootModem(onCompleted);
chen xu6dac5ab2018-10-26 17:39:23 -07001852 break;
chen xu6dac5ab2018-10-26 17:39:23 -07001853 case EVENT_CMD_MODEM_REBOOT_DONE:
1854 handleNullReturnEvent(msg, "rebootModem");
1855 break;
Malcolm Chen8e4ed912019-01-15 20:22:16 -08001856 case CMD_REQUEST_ENABLE_MODEM:
1857 request = (MainThreadRequest) msg.obj;
1858 boolean enable = (boolean) request.argument;
1859 onCompleted = obtainMessage(EVENT_ENABLE_MODEM_DONE, request);
Nazanin Bakhshi33d584b2019-02-27 10:44:32 -08001860 onCompleted.arg1 = enable ? 1 : 0;
Malcolm Chen8e4ed912019-01-15 20:22:16 -08001861 PhoneConfigurationManager.getInstance()
1862 .enablePhone(request.phone, enable, onCompleted);
1863 break;
Michele Berionne5e411512020-11-13 02:36:59 +00001864 case EVENT_ENABLE_MODEM_DONE: {
Malcolm Chen8e4ed912019-01-15 20:22:16 -08001865 ar = (AsyncResult) msg.obj;
1866 request = (MainThreadRequest) ar.userObj;
1867 request.result = (ar.exception == null);
Nazanin Bakhshif71371d2019-04-29 17:29:44 -07001868 int phoneId = request.phone.getPhoneId();
Nazanin Bakhshi33d584b2019-02-27 10:44:32 -08001869 //update the cache as modem status has changed
Nazanin Bakhshif71371d2019-04-29 17:29:44 -07001870 if ((boolean) request.result) {
1871 mPhoneConfigurationManager.addToPhoneStatusCache(phoneId, msg.arg1 == 1);
1872 updateModemStateMetrics();
1873 } else {
1874 Log.e(LOG_TAG, msg.what + " failure. Not updating modem status."
1875 + ar.exception);
1876 }
1877 notifyRequester(request);
1878 break;
Michele Berionne5e411512020-11-13 02:36:59 +00001879 }
Nazanin Bakhshif71371d2019-04-29 17:29:44 -07001880 case CMD_GET_MODEM_STATUS:
1881 request = (MainThreadRequest) msg.obj;
1882 onCompleted = obtainMessage(EVENT_GET_MODEM_STATUS_DONE, request);
1883 PhoneConfigurationManager.getInstance()
1884 .getPhoneStatusFromModem(request.phone, onCompleted);
1885 break;
1886 case EVENT_GET_MODEM_STATUS_DONE:
1887 ar = (AsyncResult) msg.obj;
1888 request = (MainThreadRequest) ar.userObj;
1889 int id = request.phone.getPhoneId();
1890 if (ar.exception == null && ar.result != null) {
1891 request.result = ar.result;
1892 //update the cache as modem status has changed
1893 mPhoneConfigurationManager.addToPhoneStatusCache(id,
1894 (boolean) request.result);
1895 } else {
1896 // Return true if modem status cannot be retrieved. For most cases,
1897 // modem status is on. And for older version modems, GET_MODEM_STATUS
1898 // and disable modem are not supported. Modem is always on.
1899 // TODO: this should be fixed in R to support a third
1900 // status UNKNOWN b/131631629
1901 request.result = true;
1902 Log.e(LOG_TAG, msg.what + " failure. Not updating modem status."
1903 + ar.exception);
1904 }
Malcolm Chen8e4ed912019-01-15 20:22:16 -08001905 notifyRequester(request);
1906 break;
Hall Liu73f5d362020-01-20 13:42:00 -08001907 case CMD_SET_SYSTEM_SELECTION_CHANNELS: {
1908 request = (MainThreadRequest) msg.obj;
1909 onCompleted = obtainMessage(EVENT_SET_SYSTEM_SELECTION_CHANNELS_DONE, request);
1910 Pair<List<RadioAccessSpecifier>, Consumer<Boolean>> args =
1911 (Pair<List<RadioAccessSpecifier>, Consumer<Boolean>>) request.argument;
1912 request.phone.setSystemSelectionChannels(args.first, onCompleted);
1913 break;
1914 }
1915 case EVENT_SET_SYSTEM_SELECTION_CHANNELS_DONE: {
1916 ar = (AsyncResult) msg.obj;
1917 request = (MainThreadRequest) ar.userObj;
1918 Pair<List<RadioAccessSpecifier>, Consumer<Boolean>> args =
1919 (Pair<List<RadioAccessSpecifier>, Consumer<Boolean>>) request.argument;
1920 args.second.accept(ar.exception == null);
1921 notifyRequester(request);
1922 break;
1923 }
Sarah Chin679c08a2020-11-18 13:39:35 -08001924 case CMD_GET_SYSTEM_SELECTION_CHANNELS: {
1925 request = (MainThreadRequest) msg.obj;
1926 onCompleted = obtainMessage(EVENT_GET_SYSTEM_SELECTION_CHANNELS_DONE, request);
1927 Phone phone = getPhoneFromRequest(request);
1928 if (phone != null) {
1929 phone.getSystemSelectionChannels(onCompleted);
1930 } else {
1931 loge("getSystemSelectionChannels: No phone object");
1932 request.result = new ArrayList<RadioAccessSpecifier>();
1933 notifyRequester(request);
1934 }
1935 break;
1936 }
1937 case EVENT_GET_SYSTEM_SELECTION_CHANNELS_DONE:
1938 ar = (AsyncResult) msg.obj;
1939 request = (MainThreadRequest) ar.userObj;
1940 if (ar.exception == null && ar.result != null) {
1941 request.result = ar.result;
1942 } else {
Sarah Chin428d1d62021-03-13 03:17:40 -08001943 request.result = new IllegalStateException(
1944 "Failed to retrieve system selecton channels");
Sarah Chin679c08a2020-11-18 13:39:35 -08001945 if (ar.result == null) {
1946 loge("getSystemSelectionChannels: Empty response");
1947 } else {
1948 loge("getSystemSelectionChannels: Unknown exception");
1949 }
1950 }
1951 notifyRequester(request);
1952 break;
yincheng zhao2737e882019-09-06 17:06:54 -07001953 case EVENT_SET_FORBIDDEN_PLMNS_DONE:
1954 ar = (AsyncResult) msg.obj;
1955 request = (MainThreadRequest) ar.userObj;
1956 if (ar.exception == null && ar.result != null) {
1957 request.result = ar.result;
1958 } else {
1959 request.result = -1;
1960 loge("Failed to set Forbidden Plmns");
1961 if (ar.result == null) {
1962 loge("setForbidenPlmns: Empty response");
1963 } else if (ar.exception != null) {
1964 loge("setForbiddenPlmns: Exception: " + ar.exception);
1965 request.result = -1;
1966 } else {
1967 loge("setForbiddenPlmns: Unknown exception");
1968 }
1969 }
1970 notifyRequester(request);
1971 break;
1972 case CMD_SET_FORBIDDEN_PLMNS:
1973 request = (MainThreadRequest) msg.obj;
Muralidhar Reddy472c2ae2021-09-29 19:38:40 +00001974 uiccPort = getUiccPortFromRequest(request);
1975 if (uiccPort == null) {
1976 loge("setForbiddenPlmns: UiccPort is null");
yincheng zhao2737e882019-09-06 17:06:54 -07001977 request.result = -1;
1978 notifyRequester(request);
1979 break;
1980 }
1981 Pair<Integer, List<String>> setFplmnsArgs =
1982 (Pair<Integer, List<String>>) request.argument;
1983 appType = setFplmnsArgs.first;
1984 List<String> fplmns = setFplmnsArgs.second;
Muralidhar Reddy472c2ae2021-09-29 19:38:40 +00001985 uiccApp = uiccPort.getApplicationByType(appType);
yincheng zhao2737e882019-09-06 17:06:54 -07001986 if (uiccApp == null) {
1987 loge("setForbiddenPlmns: no app with specified type -- " + appType);
1988 request.result = -1;
1989 loge("Failed to get UICC App");
1990 notifyRequester(request);
1991 } else {
1992 onCompleted = obtainMessage(EVENT_SET_FORBIDDEN_PLMNS_DONE, request);
1993 ((SIMRecords) uiccApp.getIccRecords())
1994 .setForbiddenPlmns(onCompleted, fplmns);
1995 }
yinchengzhao4d163c02019-12-12 15:21:47 -08001996 break;
Naina Nallurid63128d2019-09-17 14:10:30 -07001997 case CMD_ERASE_MODEM_CONFIG:
1998 request = (MainThreadRequest) msg.obj;
1999 onCompleted = obtainMessage(EVENT_ERASE_MODEM_CONFIG_DONE, request);
2000 defaultPhone.eraseModemConfig(onCompleted);
2001 break;
2002 case EVENT_ERASE_MODEM_CONFIG_DONE:
2003 handleNullReturnEvent(msg, "eraseModemConfig");
yincheng zhao2737e882019-09-06 17:06:54 -07002004 break;
zoey chene02881a2019-12-30 16:11:23 +08002005
Kai Shif70f46f2021-03-03 13:59:46 -08002006 case CMD_ERASE_DATA_SHARED_PREFERENCES:
2007 request = (MainThreadRequest) msg.obj;
2008 request.result = defaultPhone.eraseDataInSharedPreferences();
2009 notifyRequester(request);
2010 break;
2011
zoey chene02881a2019-12-30 16:11:23 +08002012 case CMD_CHANGE_ICC_LOCK_PASSWORD:
2013 request = (MainThreadRequest) msg.obj;
2014 onCompleted = obtainMessage(EVENT_CHANGE_ICC_LOCK_PASSWORD_DONE, request);
2015 Pair<String, String> changed = (Pair<String, String>) request.argument;
2016 getPhoneFromRequest(request).getIccCard().changeIccLockPassword(
2017 changed.first, changed.second, onCompleted);
2018 break;
2019 case EVENT_CHANGE_ICC_LOCK_PASSWORD_DONE:
2020 ar = (AsyncResult) msg.obj;
2021 request = (MainThreadRequest) ar.userObj;
2022 if (ar.exception == null) {
2023 request.result = TelephonyManager.CHANGE_ICC_LOCK_SUCCESS;
Michele Berionne5e411512020-11-13 02:36:59 +00002024 // If the operation is successful, update the PIN storage
2025 Pair<String, String> passwords = (Pair<String, String>) request.argument;
2026 int phoneId = getPhoneFromRequest(request).getPhoneId();
Jon Spivack9c3bc762021-10-06 20:53:09 +00002027 UiccController.getInstance().getPinStorage()
2028 .storePin(passwords.second, phoneId);
zoey chene02881a2019-12-30 16:11:23 +08002029 } else {
2030 request.result = msg.arg1;
2031 }
2032 notifyRequester(request);
2033 break;
2034
Michele Berionne5e411512020-11-13 02:36:59 +00002035 case CMD_SET_ICC_LOCK_ENABLED: {
zoey chene02881a2019-12-30 16:11:23 +08002036 request = (MainThreadRequest) msg.obj;
2037 onCompleted = obtainMessage(EVENT_SET_ICC_LOCK_ENABLED_DONE, request);
2038 Pair<Boolean, String> enabled = (Pair<Boolean, String>) request.argument;
2039 getPhoneFromRequest(request).getIccCard().setIccLockEnabled(
2040 enabled.first, enabled.second, onCompleted);
2041 break;
Michele Berionne5e411512020-11-13 02:36:59 +00002042 }
zoey chene02881a2019-12-30 16:11:23 +08002043 case EVENT_SET_ICC_LOCK_ENABLED_DONE:
2044 ar = (AsyncResult) msg.obj;
2045 request = (MainThreadRequest) ar.userObj;
2046 if (ar.exception == null) {
2047 request.result = TelephonyManager.CHANGE_ICC_LOCK_SUCCESS;
Michele Berionne5e411512020-11-13 02:36:59 +00002048 // If the operation is successful, update the PIN storage
2049 Pair<Boolean, String> enabled = (Pair<Boolean, String>) request.argument;
2050 int phoneId = getPhoneFromRequest(request).getPhoneId();
2051 if (enabled.first) {
Jon Spivack9c3bc762021-10-06 20:53:09 +00002052 UiccController.getInstance().getPinStorage()
2053 .storePin(enabled.second, phoneId);
Michele Berionne5e411512020-11-13 02:36:59 +00002054 } else {
2055 UiccController.getInstance().getPinStorage().clearPin(phoneId);
2056 }
zoey chene02881a2019-12-30 16:11:23 +08002057 } else {
2058 request.result = msg.arg1;
2059 }
Michele Berionne5e411512020-11-13 02:36:59 +00002060
2061
zoey chene02881a2019-12-30 16:11:23 +08002062 notifyRequester(request);
2063 break;
2064
Peter Wangdafb9ac2020-01-15 14:13:38 -08002065 case MSG_NOTIFY_USER_ACTIVITY:
2066 removeMessages(MSG_NOTIFY_USER_ACTIVITY);
Peter Wang59571be2020-01-27 12:35:15 +08002067 Intent intent = new Intent(TelephonyIntents.ACTION_USER_ACTIVITY_NOTIFICATION);
Peter Wangdafb9ac2020-01-15 14:13:38 -08002068 intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY);
2069 getDefaultPhone().getContext().sendBroadcastAsUser(
2070 intent, UserHandle.ALL, permission.USER_ACTIVITY);
2071 break;
Jack Nudelmanb0b87642020-11-12 15:04:39 -08002072
2073 case CMD_SET_DATA_THROTTLING: {
2074 request = (MainThreadRequest) msg.obj;
2075 onCompleted = obtainMessage(EVENT_SET_DATA_THROTTLING_DONE, request);
2076 DataThrottlingRequest dataThrottlingRequest =
2077 (DataThrottlingRequest) request.argument;
2078 Phone phone = getPhoneFromRequest(request);
2079 if (phone != null) {
2080 phone.setDataThrottling(onCompleted,
2081 request.workSource, dataThrottlingRequest.getDataThrottlingAction(),
2082 dataThrottlingRequest.getCompletionDurationMillis());
2083 } else {
2084 loge("setDataThrottling: No phone object");
2085 request.result =
2086 TelephonyManager.THERMAL_MITIGATION_RESULT_MODEM_NOT_AVAILABLE;
2087 notifyRequester(request);
2088 }
2089
2090 break;
2091 }
2092 case EVENT_SET_DATA_THROTTLING_DONE:
2093 ar = (AsyncResult) msg.obj;
2094 request = (MainThreadRequest) ar.userObj;
2095
2096 if (ar.exception == null) {
2097 request.result = TelephonyManager.THERMAL_MITIGATION_RESULT_SUCCESS;
2098 } else if (ar.exception instanceof CommandException) {
2099 loge("setDataThrottling: CommandException: " + ar.exception);
2100 CommandException.Error error =
2101 ((CommandException) (ar.exception)).getCommandError();
2102
2103 if (error == CommandException.Error.RADIO_NOT_AVAILABLE) {
2104 request.result = TelephonyManager
2105 .THERMAL_MITIGATION_RESULT_MODEM_NOT_AVAILABLE;
2106 } else if (error == CommandException.Error.INVALID_ARGUMENTS) {
2107 request.result = SET_DATA_THROTTLING_MODEM_THREW_INVALID_PARAMS;
Jack Nudelman5d6a98b2021-03-04 14:26:25 -08002108 } else if (error == CommandException.Error.REQUEST_NOT_SUPPORTED) {
2109 request.result = MODEM_DOES_NOT_SUPPORT_DATA_THROTTLING_ERROR_CODE;
Jack Nudelmanb0b87642020-11-12 15:04:39 -08002110 } else {
2111 request.result =
2112 TelephonyManager.THERMAL_MITIGATION_RESULT_MODEM_ERROR;
2113 }
2114 } else {
2115 request.result = TelephonyManager.THERMAL_MITIGATION_RESULT_MODEM_ERROR;
2116 }
2117 Log.w(LOG_TAG, "DataThrottlingResult = " + request.result);
2118 notifyRequester(request);
2119 break;
Jordan Liu109698e2020-11-24 14:50:34 -08002120
2121 case CMD_SET_SIM_POWER: {
2122 request = (MainThreadRequest) msg.obj;
2123 onCompleted = obtainMessage(EVENT_SET_SIM_POWER_DONE, request);
2124 request = (MainThreadRequest) msg.obj;
2125 int stateToSet =
2126 ((Pair<Integer, IIntegerConsumer>)
2127 request.argument).first;
2128 request.phone.setSimPowerState(stateToSet, onCompleted, request.workSource);
2129 break;
2130 }
2131 case EVENT_SET_SIM_POWER_DONE: {
2132 ar = (AsyncResult) msg.obj;
2133 request = (MainThreadRequest) ar.userObj;
2134 IIntegerConsumer callback =
2135 ((Pair<Integer, IIntegerConsumer>) request.argument).second;
2136 if (ar.exception != null) {
2137 loge("setSimPower exception: " + ar.exception);
2138 int errorCode = TelephonyManager.CallForwardingInfoCallback
2139 .RESULT_ERROR_UNKNOWN;
2140 if (ar.exception instanceof CommandException) {
2141 CommandException.Error error =
2142 ((CommandException) (ar.exception)).getCommandError();
2143 if (error == CommandException.Error.SIM_ERR) {
2144 errorCode = TelephonyManager.SET_SIM_POWER_STATE_SIM_ERROR;
2145 } else if (error == CommandException.Error.INVALID_ARGUMENTS) {
2146 errorCode = TelephonyManager.SET_SIM_POWER_STATE_ALREADY_IN_STATE;
2147 } else if (error == CommandException.Error.REQUEST_NOT_SUPPORTED) {
2148 errorCode = TelephonyManager.SET_SIM_POWER_STATE_NOT_SUPPORTED;
2149 } else {
2150 errorCode = TelephonyManager.SET_SIM_POWER_STATE_MODEM_ERROR;
2151 }
2152 }
2153 try {
2154 callback.accept(errorCode);
2155 } catch (RemoteException e) {
2156 // Ignore if the remote process is no longer available to call back.
2157 Log.w(LOG_TAG, "setSimPower: callback not available.");
2158 }
2159 } else {
2160 try {
2161 callback.accept(TelephonyManager.SET_SIM_POWER_STATE_SUCCESS);
2162 } catch (RemoteException e) {
2163 // Ignore if the remote process is no longer available to call back.
2164 Log.w(LOG_TAG, "setSimPower: callback not available.");
2165 }
2166 }
2167 break;
2168 }
Rambo Wanga5cc9b72021-01-07 10:51:54 -08002169 case CMD_SET_SIGNAL_STRENGTH_UPDATE_REQUEST: {
2170 request = (MainThreadRequest) msg.obj;
2171
2172 final Phone phone = getPhoneFromRequest(request);
2173 if (phone == null || phone.getServiceStateTracker() == null) {
2174 request.result = new IllegalStateException("Phone or SST is null");
2175 notifyRequester(request);
2176 break;
2177 }
2178
2179 Pair<Integer, SignalStrengthUpdateRequest> pair =
2180 (Pair<Integer, SignalStrengthUpdateRequest>) request.argument;
2181 onCompleted = obtainMessage(EVENT_SET_SIGNAL_STRENGTH_UPDATE_REQUEST_DONE,
2182 request);
Rambo Wang6568f172021-02-03 16:56:47 -08002183 phone.getSignalStrengthController().setSignalStrengthUpdateRequest(
Rambo Wanga5cc9b72021-01-07 10:51:54 -08002184 request.subId, pair.first /*callingUid*/,
2185 pair.second /*request*/, onCompleted);
2186 break;
2187 }
2188 case EVENT_SET_SIGNAL_STRENGTH_UPDATE_REQUEST_DONE: {
2189 ar = (AsyncResult) msg.obj;
2190 request = (MainThreadRequest) ar.userObj;
2191 // request.result will be the exception of ar if present, true otherwise.
2192 // Be cautious not to leave result null which will wait() forever
2193 request.result = ar.exception != null ? ar.exception : true;
2194 notifyRequester(request);
2195 break;
2196 }
2197 case CMD_CLEAR_SIGNAL_STRENGTH_UPDATE_REQUEST: {
2198 request = (MainThreadRequest) msg.obj;
2199
2200 Phone phone = getPhoneFromRequest(request);
2201 if (phone == null || phone.getServiceStateTracker() == null) {
2202 request.result = new IllegalStateException("Phone or SST is null");
2203 notifyRequester(request);
2204 break;
2205 }
2206
2207 Pair<Integer, SignalStrengthUpdateRequest> pair =
2208 (Pair<Integer, SignalStrengthUpdateRequest>) request.argument;
2209 onCompleted = obtainMessage(EVENT_CLEAR_SIGNAL_STRENGTH_UPDATE_REQUEST_DONE,
2210 request);
Rambo Wang6568f172021-02-03 16:56:47 -08002211 phone.getSignalStrengthController().clearSignalStrengthUpdateRequest(
Rambo Wanga5cc9b72021-01-07 10:51:54 -08002212 request.subId, pair.first /*callingUid*/,
2213 pair.second /*request*/, onCompleted);
2214 break;
2215 }
2216 case EVENT_CLEAR_SIGNAL_STRENGTH_UPDATE_REQUEST_DONE: {
2217 ar = (AsyncResult) msg.obj;
2218 request = (MainThreadRequest) ar.userObj;
2219 request.result = ar.exception != null ? ar.exception : true;
2220 notifyRequester(request);
2221 break;
2222 }
Jordan Liu109698e2020-11-24 14:50:34 -08002223
Hongbo Zeng156aa4a2021-02-08 21:50:28 +08002224 case CMD_GET_SLICING_CONFIG: {
2225 request = (MainThreadRequest) msg.obj;
2226 onCompleted = obtainMessage(EVENT_GET_SLICING_CONFIG_DONE, request);
2227 request.phone.getSlicingConfig(onCompleted);
2228 break;
2229 }
2230 case EVENT_GET_SLICING_CONFIG_DONE: {
2231 ar = (AsyncResult) msg.obj;
2232 request = (MainThreadRequest) ar.userObj;
2233 ResultReceiver result = (ResultReceiver) request.argument;
2234
Hongbo Zeng0e18b162021-04-07 16:52:18 +08002235 NetworkSlicingConfig slicingConfig = null;
Hongbo Zeng156aa4a2021-02-08 21:50:28 +08002236 Bundle bundle = new Bundle();
2237 int resultCode = 0;
2238 if (ar.exception != null) {
2239 Log.e(LOG_TAG, "Exception retrieving slicing configuration="
2240 + ar.exception);
Hongbo Zeng0e18b162021-04-07 16:52:18 +08002241 resultCode = TelephonyManager.NetworkSlicingException.ERROR_MODEM_ERROR;
Hongbo Zeng156aa4a2021-02-08 21:50:28 +08002242 } else if (ar.result == null) {
2243 Log.w(LOG_TAG, "Timeout Waiting for slicing configuration!");
Hongbo Zeng0e18b162021-04-07 16:52:18 +08002244 resultCode = TelephonyManager.NetworkSlicingException.ERROR_TIMEOUT;
Hongbo Zeng156aa4a2021-02-08 21:50:28 +08002245 } else {
2246 // use the result as returned
Hongbo Zeng0e18b162021-04-07 16:52:18 +08002247 resultCode = TelephonyManager.NetworkSlicingException.SUCCESS;
2248 slicingConfig = (NetworkSlicingConfig) ar.result;
Hongbo Zeng156aa4a2021-02-08 21:50:28 +08002249 }
2250
2251 if (slicingConfig == null) {
Hongbo Zeng0e18b162021-04-07 16:52:18 +08002252 slicingConfig = new NetworkSlicingConfig();
Hongbo Zeng156aa4a2021-02-08 21:50:28 +08002253 }
2254 bundle.putParcelable(TelephonyManager.KEY_SLICING_CONFIG_HANDLE, slicingConfig);
2255 result.send(resultCode, bundle);
2256 notifyRequester(request);
2257 break;
2258 }
2259
Sarah Chin71b3a852022-09-28 15:54:19 -07002260 case CMD_PURCHASE_PREMIUM_CAPABILITY: {
Sarah Chin2ec39f62022-08-31 17:03:26 -07002261 request = (MainThreadRequest) msg.obj;
2262 onCompleted = obtainMessage(EVENT_PURCHASE_PREMIUM_CAPABILITY_DONE, request);
Sarah Chin71b3a852022-09-28 15:54:19 -07002263 PurchasePremiumCapabilityArgument arg =
2264 (PurchasePremiumCapabilityArgument) request.argument;
Sarah Chin46355ba2022-11-01 23:51:16 -07002265 SlicePurchaseController.getInstance(request.phone).purchasePremiumCapability(
Sarah Chinb8218c22023-01-04 13:35:29 -08002266 arg.capability, onCompleted);
Sarah Chin2ec39f62022-08-31 17:03:26 -07002267 break;
Sarah Chin71b3a852022-09-28 15:54:19 -07002268 }
Sarah Chin2ec39f62022-08-31 17:03:26 -07002269
Sarah Chin71b3a852022-09-28 15:54:19 -07002270 case EVENT_PURCHASE_PREMIUM_CAPABILITY_DONE: {
Sarah Chin2ec39f62022-08-31 17:03:26 -07002271 ar = (AsyncResult) msg.obj;
2272 request = (MainThreadRequest) ar.userObj;
Sarah Chin71b3a852022-09-28 15:54:19 -07002273 PurchasePremiumCapabilityArgument arg =
2274 (PurchasePremiumCapabilityArgument) request.argument;
Sarah Chin2ec39f62022-08-31 17:03:26 -07002275 try {
2276 int result = (int) ar.result;
Sarah Chin71b3a852022-09-28 15:54:19 -07002277 arg.callback.accept(result);
Sarah Chin2ec39f62022-08-31 17:03:26 -07002278 log("purchasePremiumCapability: capability="
Sarah Chin71b3a852022-09-28 15:54:19 -07002279 + TelephonyManager.convertPremiumCapabilityToString(arg.capability)
Sarah Chin2ec39f62022-08-31 17:03:26 -07002280 + ", result= "
2281 + TelephonyManager.convertPurchaseResultToString(result));
2282 } catch (RemoteException e) {
2283 String logStr = "Purchase premium capability "
Sarah Chin71b3a852022-09-28 15:54:19 -07002284 + TelephonyManager.convertPremiumCapabilityToString(arg.capability)
Sarah Chin2ec39f62022-08-31 17:03:26 -07002285 + " failed: " + e;
2286 if (DBG) log(logStr);
2287 AnomalyReporter.reportAnomaly(
2288 UUID.fromString(PURCHASE_PREMIUM_CAPABILITY_ERROR_UUID), logStr);
2289 }
2290 break;
Sarah Chin71b3a852022-09-28 15:54:19 -07002291 }
Sarah Chin2ec39f62022-08-31 17:03:26 -07002292
Michele Berionne5e411512020-11-13 02:36:59 +00002293 case CMD_PREPARE_UNATTENDED_REBOOT:
2294 request = (MainThreadRequest) msg.obj;
2295 request.result =
Rafael Higuera Silvad9630642021-09-20 15:32:01 +00002296 UiccController.getInstance().getPinStorage()
2297 .prepareUnattendedReboot(request.workSource);
Michele Berionne5e411512020-11-13 02:36:59 +00002298 notifyRequester(request);
2299 break;
2300
Sarah Chineccfbd12023-01-20 19:00:35 -08002301 case CMD_START_SATELLITE_POSITION_UPDATES: {
2302 request = (MainThreadRequest) msg.obj;
2303 onCompleted =
2304 obtainMessage(EVENT_START_SATELLITE_POSITION_UPDATES_DONE, request);
2305 Phone phone = getPhoneFromRequest(request);
2306 if (phone != null) {
2307 phone.startSatellitePositionUpdates(onCompleted);
2308 } else {
2309 loge("startSatellitePositionUpdates: No phone object");
2310 request.result = SatelliteManager.SATELLITE_SERVICE_REQUEST_FAILED;
2311 notifyRequester(request);
2312 }
2313 break;
2314 }
2315
2316 case EVENT_START_SATELLITE_POSITION_UPDATES_DONE: {
2317 ar = (AsyncResult) msg.obj;
2318 request = (MainThreadRequest) ar.userObj;
2319 if (ar.exception == null) {
2320 request.result = SatelliteManager.SATELLITE_SERVICE_SUCCESS;
2321 } else {
2322 request.result = SatelliteManager.SATELLITE_SERVICE_ERROR;
2323 if (ar.exception instanceof CommandException) {
2324 CommandException.Error error =
2325 ((CommandException) (ar.exception)).getCommandError();
2326 request.result = RILUtils.convertToSatelliteError(error);
2327 loge("startSatellitePositionUpdates CommandException: " + ar.exception);
2328 } else {
2329 loge("startSatellitePositionUpdates unknown exception:" + ar.exception);
2330 }
2331 }
2332 notifyRequester(request);
2333 break;
2334 }
2335
2336 case CMD_STOP_SATELLITE_POSITION_UPDATES: {
2337 request = (MainThreadRequest) msg.obj;
2338 onCompleted =
2339 obtainMessage(EVENT_STOP_SATELLITE_POSITION_UPDATES_DONE, request);
2340 Phone phone = getPhoneFromRequest(request);
2341 if (phone != null) {
2342 phone.stopSatellitePositionUpdates(onCompleted);
2343 } else {
2344 loge("stopSatellitePositionUpdates: No phone object");
2345 request.result = SatelliteManager.SATELLITE_SERVICE_REQUEST_FAILED;
2346 notifyRequester(request);
2347 }
2348 break;
2349 }
2350
2351 case EVENT_STOP_SATELLITE_POSITION_UPDATES_DONE: {
2352 ar = (AsyncResult) msg.obj;
2353 request = (MainThreadRequest) ar.userObj;
2354 if (ar.exception == null) {
2355 request.result = SatelliteManager.SATELLITE_SERVICE_SUCCESS;
2356 } else {
2357 request.result = SatelliteManager.SATELLITE_SERVICE_ERROR;
2358 if (ar.exception instanceof CommandException) {
2359 CommandException.Error error =
2360 ((CommandException) (ar.exception)).getCommandError();
2361 request.result = RILUtils.convertToSatelliteError(error);
2362 loge("stopSatellitePositionUpdates CommandException: " + ar.exception);
2363 } else {
2364 loge("stopSatellitePositionUpdates unknown exception:" + ar.exception);
2365 }
2366 }
2367 notifyRequester(request);
2368 break;
2369 }
2370
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002371 default:
2372 Log.w(LOG_TAG, "MainThreadHandler: unexpected message code: " + msg.what);
2373 break;
2374 }
2375 }
Jake Hambye994d462014-02-03 13:10:13 -08002376
Pengquan Menga1bb6272018-09-06 09:59:22 -07002377 private void notifyRequester(MainThreadRequest request) {
2378 synchronized (request) {
2379 request.notifyAll();
2380 }
2381 }
2382
Jake Hambye994d462014-02-03 13:10:13 -08002383 private void handleNullReturnEvent(Message msg, String command) {
2384 AsyncResult ar = (AsyncResult) msg.obj;
2385 MainThreadRequest request = (MainThreadRequest) ar.userObj;
2386 if (ar.exception == null) {
2387 request.result = true;
2388 } else {
2389 request.result = false;
2390 if (ar.exception instanceof CommandException) {
2391 loge(command + ": CommandException: " + ar.exception);
2392 } else {
2393 loge(command + ": Unknown exception");
2394 }
2395 }
Pengquan Menga1bb6272018-09-06 09:59:22 -07002396 notifyRequester(request);
Jake Hambye994d462014-02-03 13:10:13 -08002397 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002398 }
2399
2400 /**
2401 * Posts the specified command to be executed on the main thread,
2402 * waits for the request to complete, and returns the result.
2403 * @see #sendRequestAsync
2404 */
2405 private Object sendRequest(int command, Object argument) {
Rambo Wang0f050d82021-02-12 11:43:36 -08002406 return sendRequest(command, argument, SubscriptionManager.INVALID_SUBSCRIPTION_ID, null,
2407 null, -1 /*timeoutInMs*/);
vagdeviaf9a5b92018-08-15 16:01:53 -07002408 }
2409
2410 /**
2411 * Posts the specified command to be executed on the main thread,
2412 * waits for the request to complete, and returns the result.
2413 * @see #sendRequestAsync
2414 */
2415 private Object sendRequest(int command, Object argument, WorkSource workSource) {
2416 return sendRequest(command, argument, SubscriptionManager.INVALID_SUBSCRIPTION_ID,
Rambo Wang0f050d82021-02-12 11:43:36 -08002417 null, workSource, -1 /*timeoutInMs*/);
Wink Saville36469e72014-06-11 15:17:00 -07002418 }
2419
2420 /**
2421 * Posts the specified command to be executed on the main thread,
2422 * waits for the request to complete, and returns the result.
2423 * @see #sendRequestAsync
2424 */
Shishir Agrawal76d5da92014-11-09 16:17:25 -08002425 private Object sendRequest(int command, Object argument, Integer subId) {
Rambo Wang0f050d82021-02-12 11:43:36 -08002426 return sendRequest(command, argument, subId, null, null, -1 /*timeoutInMs*/);
2427 }
2428
2429 /**
2430 * Posts the specified command to be executed on the main thread,
2431 * waits for the request to complete for at most {@code timeoutInMs}, and returns the result
2432 * if not timeout or null otherwise.
2433 * @see #sendRequestAsync
2434 */
2435 private @Nullable Object sendRequest(int command, Object argument, Integer subId,
2436 long timeoutInMs) {
2437 return sendRequest(command, argument, subId, null, null, timeoutInMs);
vagdeviaf9a5b92018-08-15 16:01:53 -07002438 }
2439
2440 /**
2441 * Posts the specified command to be executed on the main thread,
2442 * waits for the request to complete, and returns the result.
2443 * @see #sendRequestAsync
2444 */
Nathan Harold92bed182018-10-12 18:16:49 -07002445 private Object sendRequest(int command, Object argument, int subId, WorkSource workSource) {
Rambo Wang0f050d82021-02-12 11:43:36 -08002446 return sendRequest(command, argument, subId, null, workSource, -1 /*timeoutInMs*/);
Nathan Harold92bed182018-10-12 18:16:49 -07002447 }
2448
2449 /**
2450 * Posts the specified command to be executed on the main thread,
2451 * waits for the request to complete, and returns the result.
2452 * @see #sendRequestAsync
2453 */
2454 private Object sendRequest(int command, Object argument, Phone phone, WorkSource workSource) {
Rambo Wang0f050d82021-02-12 11:43:36 -08002455 return sendRequest(command, argument, SubscriptionManager.INVALID_SUBSCRIPTION_ID, phone,
2456 workSource, -1 /*timeoutInMs*/);
Nathan Harold92bed182018-10-12 18:16:49 -07002457 }
2458
2459 /**
Rambo Wang0f050d82021-02-12 11:43:36 -08002460 * Posts the specified command to be executed on the main thread. If {@code timeoutInMs} is
2461 * negative, waits for the request to complete, and returns the result. Otherwise, wait for
2462 * maximum of {@code timeoutInMs} milliseconds, interrupt and return null.
Nathan Harold92bed182018-10-12 18:16:49 -07002463 * @see #sendRequestAsync
2464 */
Rambo Wang0f050d82021-02-12 11:43:36 -08002465 private @Nullable Object sendRequest(int command, Object argument, Integer subId, Phone phone,
2466 WorkSource workSource, long timeoutInMs) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002467 if (Looper.myLooper() == mMainThreadHandler.getLooper()) {
2468 throw new RuntimeException("This method will deadlock if called from the main thread.");
2469 }
2470
Nathan Harold92bed182018-10-12 18:16:49 -07002471 MainThreadRequest request = null;
2472 if (subId != SubscriptionManager.INVALID_SUBSCRIPTION_ID && phone != null) {
2473 throw new IllegalArgumentException("subId and phone cannot both be specified!");
2474 } else if (phone != null) {
2475 request = new MainThreadRequest(argument, phone, workSource);
2476 } else {
2477 request = new MainThreadRequest(argument, subId, workSource);
2478 }
2479
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002480 Message msg = mMainThreadHandler.obtainMessage(command, request);
2481 msg.sendToTarget();
2482
Rambo Wang0f050d82021-02-12 11:43:36 -08002483
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002484 synchronized (request) {
Rambo Wang0f050d82021-02-12 11:43:36 -08002485 if (timeoutInMs >= 0) {
2486 // Wait for at least timeoutInMs before returning null request result
2487 long now = SystemClock.elapsedRealtime();
2488 long deadline = now + timeoutInMs;
Grace Jia8a0a1e82021-05-23 22:59:52 -07002489 while (request.result == null && now < deadline) {
Rambo Wang0f050d82021-02-12 11:43:36 -08002490 try {
2491 request.wait(deadline - now);
2492 } catch (InterruptedException e) {
2493 // Do nothing, go back and check if request is completed or timeout
2494 } finally {
2495 now = SystemClock.elapsedRealtime();
2496 }
2497 }
2498 } else {
2499 // Wait for the request to complete
2500 while (request.result == null) {
2501 try {
2502 request.wait();
2503 } catch (InterruptedException e) {
2504 // Do nothing, go back and wait until the request is complete
2505 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002506 }
2507 }
2508 }
Rambo Wang0f050d82021-02-12 11:43:36 -08002509 if (request.result == null) {
2510 Log.wtf(LOG_TAG,
2511 "sendRequest: Blocking command timed out. Something has gone terribly wrong.");
2512 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002513 return request.result;
2514 }
2515
2516 /**
2517 * Asynchronous ("fire and forget") version of sendRequest():
2518 * Posts the specified command to be executed on the main thread, and
2519 * returns immediately.
2520 * @see #sendRequest
2521 */
2522 private void sendRequestAsync(int command) {
2523 mMainThreadHandler.sendEmptyMessage(command);
2524 }
2525
2526 /**
Sailesh Nepalbd76e4e2013-10-27 13:59:44 -07002527 * Same as {@link #sendRequestAsync(int)} except it takes an argument.
Nathan Haroldfa8da0f2018-09-27 18:51:29 -07002528 * @see {@link #sendRequest(int)}
Sailesh Nepalbd76e4e2013-10-27 13:59:44 -07002529 */
2530 private void sendRequestAsync(int command, Object argument) {
Nathan Haroldfa8da0f2018-09-27 18:51:29 -07002531 sendRequestAsync(command, argument, null, null);
2532 }
2533
2534 /**
2535 * Same as {@link #sendRequestAsync(int,Object)} except it takes a Phone and WorkSource.
2536 * @see {@link #sendRequest(int,Object)}
2537 */
2538 private void sendRequestAsync(
2539 int command, Object argument, Phone phone, WorkSource workSource) {
2540 MainThreadRequest request = new MainThreadRequest(argument, phone, workSource);
Sailesh Nepalbd76e4e2013-10-27 13:59:44 -07002541 Message msg = mMainThreadHandler.obtainMessage(command, request);
2542 msg.sendToTarget();
2543 }
2544
2545 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002546 * Initialize the singleton PhoneInterfaceManager instance.
2547 * This is only done once, at startup, from PhoneApp.onCreate().
2548 */
Nazanin Bakhshif782e562018-12-11 15:15:39 -08002549 /* package */ static PhoneInterfaceManager init(PhoneGlobals app) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002550 synchronized (PhoneInterfaceManager.class) {
2551 if (sInstance == null) {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08002552 sInstance = new PhoneInterfaceManager(app);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002553 } else {
2554 Log.wtf(LOG_TAG, "init() called multiple times! sInstance = " + sInstance);
2555 }
2556 return sInstance;
2557 }
2558 }
2559
2560 /** Private constructor; @see init() */
Jordan Liu1979a042020-03-20 21:39:35 +00002561 private PhoneInterfaceManager(PhoneGlobals app) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002562 mApp = app;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002563 mCM = PhoneGlobals.getInstance().mCM;
Brad Ebingerd1947d82021-05-17 20:54:49 +00002564 mImsResolver = ImsResolver.getInstance();
Stuart Scott981d8582015-04-21 14:09:50 -07002565 mUserManager = (UserManager) app.getSystemService(Context.USER_SERVICE);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002566 mAppOps = (AppOpsManager)app.getSystemService(Context.APP_OPS_SERVICE);
Grace Jia0ddb3612021-04-22 13:35:26 -07002567 mPm = app.getSystemService(PackageManager.class);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002568 mMainThreadHandler = new MainThreadHandler();
Jack Yue37dd262022-12-16 11:53:37 -08002569 if (!PhoneFactory.isSubscriptionManagerServiceEnabled()) {
2570 mSubscriptionController = SubscriptionController.getInstance();
2571 } else {
2572 mSubscriptionController = null;
2573 }
Nazanin Bakhshif782e562018-12-11 15:15:39 -08002574 mTelephonySharedPreferences =
2575 PreferenceManager.getDefaultSharedPreferences(mApp);
yinxub1bed742017-04-17 11:45:04 -07002576 mNetworkScanRequestTracker = new NetworkScanRequestTracker();
Malcolm Chen2c63d402018-08-14 16:00:53 -07002577 mPhoneConfigurationManager = PhoneConfigurationManager.getInstance();
Daniel Bright94f43662021-03-01 14:43:40 -08002578 mRadioInterfaceCapabilities = RadioInterfaceCapabilityController.getInstance();
Peter Wanga3cf4ac2020-01-27 09:39:46 +08002579 mNotifyUserActivity = new AtomicBoolean(false);
Tyler Gunn64144d92022-03-17 14:16:41 -07002580 PropertyInvalidatedCache.invalidateCache(TelephonyManager.CACHE_KEY_PHONE_ACCOUNT_TO_SUBID);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002581 publish();
arunvoddud7401012022-12-15 16:08:12 +00002582 CarrierAllowListInfo.loadInstance(mApp);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002583 }
2584
Gil Cukierman1c0eb932022-12-06 22:28:24 +00002585 @VisibleForTesting
2586 public SharedPreferences getSharedPreferences() {
2587 return mTelephonySharedPreferences;
2588 }
2589
Gil Cukierman92cc7db2023-01-06 19:25:53 +00002590 /**
2591 * Get the default phone for this device.
2592 */
2593 @VisibleForTesting
2594 public Phone getDefaultPhone() {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08002595 Phone thePhone = getPhone(getDefaultSubscription());
2596 return (thePhone != null) ? thePhone : PhoneFactory.getDefaultPhone();
2597 }
2598
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002599 private void publish() {
2600 if (DBG) log("publish: " + this);
2601
Peter Wangc035ce42020-01-08 21:00:22 -08002602 TelephonyFrameworkInitializer
2603 .getTelephonyServiceManager()
2604 .getTelephonyServiceRegisterer()
2605 .register(this);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002606 }
2607
Stuart Scott584921c2015-01-15 17:10:34 -08002608 private Phone getPhoneFromRequest(MainThreadRequest request) {
Jordan Liu4c733742019-02-28 12:03:40 -08002609 if (request.phone != null) {
2610 return request.phone;
2611 } else {
2612 return getPhoneFromSubId(request.subId);
2613 }
2614 }
2615
2616 private Phone getPhoneFromSubId(int subId) {
2617 return (subId == SubscriptionManager.INVALID_SUBSCRIPTION_ID)
2618 ? getDefaultPhone() : getPhone(subId);
Stuart Scott584921c2015-01-15 17:10:34 -08002619 }
2620
Aishwarya Mallampati5e581e12023-01-17 21:57:06 +00002621 /**
2622 * Get phone object associated with a subscription.
2623 * Return default phone if phone object associated with subscription is null
2624 * @param subId - subscriptionId
2625 * @return phone object associated with a subscription or default phone if null.
2626 */
2627 private Phone getPhoneFromSubIdOrDefault(int subId) {
2628 Phone phone = getPhoneFromSubId(subId);
2629 if (phone == null) {
2630 phone = getDefaultPhone();
2631 }
2632 return phone;
2633 }
2634
Rambo Wange53e07d2022-05-10 13:01:13 -07002635 @Nullable
2636 private UiccPort getUiccPortFromRequest(@NonNull MainThreadRequest request) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002637 Phone phone = getPhoneFromRequest(request);
2638 return phone == null ? null :
Muralidhar Reddy472c2ae2021-09-29 19:38:40 +00002639 UiccController.getInstance().getUiccPort(phone.getPhoneId());
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002640 }
2641
Wink Saville36469e72014-06-11 15:17:00 -07002642 // returns phone associated with the subId.
Wink Savilleb564aae2014-10-23 10:18:09 -07002643 private Phone getPhone(int subId) {
Jack Yu285100e2022-12-02 22:48:35 -08002644 return PhoneFactory.getPhone(SubscriptionManager.getPhoneId(subId));
Wink Saville36469e72014-06-11 15:17:00 -07002645 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002646
Kai Shif70f46f2021-03-03 13:59:46 -08002647 private void sendEraseModemConfig(@NonNull Phone phone) {
2648 Boolean success = (Boolean) sendRequest(CMD_ERASE_MODEM_CONFIG, null);
2649 if (DBG) log("eraseModemConfig:" + ' ' + (success ? "ok" : "fail"));
2650 }
2651
2652 private void sendEraseDataInSharedPreferences(@NonNull Phone phone) {
2653 Boolean success = (Boolean) sendRequest(CMD_ERASE_DATA_SHARED_PREFERENCES, null);
2654 if (DBG) log("eraseDataInSharedPreferences:" + ' ' + (success ? "ok" : "fail"));
Naina Nallurid63128d2019-09-17 14:10:30 -07002655 }
2656
Peter Wang44b186e2020-01-13 23:33:09 -08002657 private boolean isImsAvailableOnDevice() {
2658 PackageManager pm = getDefaultPhone().getContext().getPackageManager();
2659 if (pm == null) {
2660 // For some reason package manger is not available.. This will fail internally anyway,
2661 // so do not throw error and allow.
2662 return true;
2663 }
2664 return pm.hasSystemFeature(PackageManager.FEATURE_TELEPHONY_IMS, 0);
2665 }
2666
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002667 public void dial(String number) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07002668 dialForSubscriber(getPreferredVoiceSubscription(), number);
Wink Saville36469e72014-06-11 15:17:00 -07002669 }
2670
Wink Savilleb564aae2014-10-23 10:18:09 -07002671 public void dialForSubscriber(int subId, String number) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002672 if (DBG) log("dial: " + number);
2673 // No permission check needed here: This is just a wrapper around the
2674 // ACTION_DIAL intent, which is available to any app since it puts up
2675 // the UI before it does anything.
2676
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002677 final long identity = Binder.clearCallingIdentity();
2678 try {
2679 String url = createTelUrl(number);
2680 if (url == null) {
2681 return;
2682 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002683
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002684 // PENDING: should we just silently fail if phone is offhook or ringing?
2685 PhoneConstants.State state = mCM.getState(subId);
2686 if (state != PhoneConstants.State.OFFHOOK && state != PhoneConstants.State.RINGING) {
2687 Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse(url));
2688 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
2689 mApp.startActivity(intent);
2690 }
2691 } finally {
2692 Binder.restoreCallingIdentity(identity);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002693 }
2694 }
2695
2696 public void call(String callingPackage, String number) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07002697 callForSubscriber(getPreferredVoiceSubscription(), callingPackage, number);
Wink Saville36469e72014-06-11 15:17:00 -07002698 }
2699
Wink Savilleb564aae2014-10-23 10:18:09 -07002700 public void callForSubscriber(int subId, String callingPackage, String number) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002701 if (DBG) log("call: " + number);
2702
2703 // This is just a wrapper around the ACTION_CALL intent, but we still
2704 // need to do a permission check since we're calling startActivity()
2705 // from the context of the phone app.
2706 enforceCallPermission();
2707
Jordan Liu1617b712019-07-10 15:06:26 -07002708 if (mAppOps.noteOp(AppOpsManager.OPSTR_CALL_PHONE, Binder.getCallingUid(), callingPackage)
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002709 != AppOpsManager.MODE_ALLOWED) {
2710 return;
2711 }
2712
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002713 final long identity = Binder.clearCallingIdentity();
2714 try {
2715 String url = createTelUrl(number);
2716 if (url == null) {
2717 return;
2718 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002719
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002720 boolean isValid = false;
2721 final List<SubscriptionInfo> slist = getActiveSubscriptionInfoListPrivileged();
2722 if (slist != null) {
2723 for (SubscriptionInfo subInfoRecord : slist) {
2724 if (subInfoRecord.getSubscriptionId() == subId) {
2725 isValid = true;
2726 break;
2727 }
Wink Saville3ab207e2014-11-20 13:07:20 -08002728 }
Wink Saville08874612014-08-31 19:19:58 -07002729 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002730 if (!isValid) {
2731 return;
2732 }
Wink Saville08874612014-08-31 19:19:58 -07002733
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002734 Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse(url));
2735 intent.putExtra(SUBSCRIPTION_KEY, subId);
2736 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
2737 mApp.startActivity(intent);
2738 } finally {
2739 Binder.restoreCallingIdentity(identity);
2740 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002741 }
2742
Wink Savilleb564aae2014-10-23 10:18:09 -07002743 public boolean supplyPinForSubscriber(int subId, String pin) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07002744 int [] resultArray = supplyPinReportResultForSubscriber(subId, pin);
Wink Saville9de0f752013-10-22 19:04:03 -07002745 return (resultArray[0] == PhoneConstants.PIN_RESULT_SUCCESS) ? true : false;
2746 }
2747
Wink Savilleb564aae2014-10-23 10:18:09 -07002748 public boolean supplyPukForSubscriber(int subId, String puk, String pin) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07002749 int [] resultArray = supplyPukReportResultForSubscriber(subId, puk, pin);
Wink Saville9de0f752013-10-22 19:04:03 -07002750 return (resultArray[0] == PhoneConstants.PIN_RESULT_SUCCESS) ? true : false;
2751 }
2752
Wink Savilleb564aae2014-10-23 10:18:09 -07002753 public int[] supplyPinReportResultForSubscriber(int subId, String pin) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002754 enforceModifyPermission();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002755
2756 final long identity = Binder.clearCallingIdentity();
2757 try {
Michele Berionne5e411512020-11-13 02:36:59 +00002758 Phone phone = getPhone(subId);
2759 final UnlockSim checkSimPin = new UnlockSim(phone.getPhoneId(), phone.getIccCard());
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002760 checkSimPin.start();
2761 return checkSimPin.unlockSim(null, pin);
2762 } finally {
2763 Binder.restoreCallingIdentity(identity);
2764 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002765 }
2766
Wink Savilleb564aae2014-10-23 10:18:09 -07002767 public int[] supplyPukReportResultForSubscriber(int subId, String puk, String pin) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002768 enforceModifyPermission();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002769
2770 final long identity = Binder.clearCallingIdentity();
2771 try {
Michele Berionne5e411512020-11-13 02:36:59 +00002772 Phone phone = getPhone(subId);
2773 final UnlockSim checkSimPuk = new UnlockSim(phone.getPhoneId(), phone.getIccCard());
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002774 checkSimPuk.start();
2775 return checkSimPuk.unlockSim(puk, pin);
2776 } finally {
2777 Binder.restoreCallingIdentity(identity);
2778 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002779 }
2780
2781 /**
Wink Saville9de0f752013-10-22 19:04:03 -07002782 * Helper thread to turn async call to SimCard#supplyPin into
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002783 * a synchronous one.
2784 */
2785 private static class UnlockSim extends Thread {
2786
2787 private final IccCard mSimCard;
Michele Berionne5e411512020-11-13 02:36:59 +00002788 private final int mPhoneId;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002789
2790 private boolean mDone = false;
Wink Saville9de0f752013-10-22 19:04:03 -07002791 private int mResult = PhoneConstants.PIN_GENERAL_FAILURE;
2792 private int mRetryCount = -1;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002793
2794 // For replies from SimCard interface
2795 private Handler mHandler;
2796
2797 // For async handler to identify request type
2798 private static final int SUPPLY_PIN_COMPLETE = 100;
2799
Michele Berionne5e411512020-11-13 02:36:59 +00002800 UnlockSim(int phoneId, IccCard simCard) {
2801 mPhoneId = phoneId;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002802 mSimCard = simCard;
2803 }
2804
2805 @Override
2806 public void run() {
2807 Looper.prepare();
2808 synchronized (UnlockSim.this) {
2809 mHandler = new Handler() {
2810 @Override
2811 public void handleMessage(Message msg) {
2812 AsyncResult ar = (AsyncResult) msg.obj;
2813 switch (msg.what) {
2814 case SUPPLY_PIN_COMPLETE:
2815 Log.d(LOG_TAG, "SUPPLY_PIN_COMPLETE");
2816 synchronized (UnlockSim.this) {
Wink Saville9de0f752013-10-22 19:04:03 -07002817 mRetryCount = msg.arg1;
2818 if (ar.exception != null) {
2819 if (ar.exception instanceof CommandException &&
2820 ((CommandException)(ar.exception)).getCommandError()
2821 == CommandException.Error.PASSWORD_INCORRECT) {
2822 mResult = PhoneConstants.PIN_PASSWORD_INCORRECT;
vivi.lib5e9ada2019-09-12 16:04:24 +08002823 } //When UiccCardApp dispose,handle message and return exception
2824 else if (ar.exception instanceof CommandException &&
2825 ((CommandException) (ar.exception)).getCommandError()
2826 == CommandException.Error.ABORTED) {
2827 mResult = PhoneConstants.PIN_OPERATION_ABORTED;
Wink Saville9de0f752013-10-22 19:04:03 -07002828 } else {
2829 mResult = PhoneConstants.PIN_GENERAL_FAILURE;
2830 }
2831 } else {
2832 mResult = PhoneConstants.PIN_RESULT_SUCCESS;
2833 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002834 mDone = true;
2835 UnlockSim.this.notifyAll();
2836 }
2837 break;
2838 }
2839 }
2840 };
2841 UnlockSim.this.notifyAll();
2842 }
2843 Looper.loop();
2844 }
2845
2846 /*
2847 * Use PIN or PUK to unlock SIM card
2848 *
2849 * If PUK is null, unlock SIM card with PIN
2850 *
2851 * If PUK is not null, unlock SIM card with PUK and set PIN code
2852 */
Wink Saville9de0f752013-10-22 19:04:03 -07002853 synchronized int[] unlockSim(String puk, String pin) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002854
2855 while (mHandler == null) {
2856 try {
2857 wait();
2858 } catch (InterruptedException e) {
2859 Thread.currentThread().interrupt();
2860 }
2861 }
2862 Message callback = Message.obtain(mHandler, SUPPLY_PIN_COMPLETE);
2863
2864 if (puk == null) {
2865 mSimCard.supplyPin(pin, callback);
2866 } else {
2867 mSimCard.supplyPuk(puk, pin, callback);
2868 }
2869
2870 while (!mDone) {
2871 try {
2872 Log.d(LOG_TAG, "wait for done");
2873 wait();
2874 } catch (InterruptedException e) {
2875 // Restore the interrupted status
2876 Thread.currentThread().interrupt();
2877 }
2878 }
2879 Log.d(LOG_TAG, "done");
Wink Saville9de0f752013-10-22 19:04:03 -07002880 int[] resultArray = new int[2];
2881 resultArray[0] = mResult;
2882 resultArray[1] = mRetryCount;
Michele Berionne5e411512020-11-13 02:36:59 +00002883
2884 if (mResult == PhoneConstants.PIN_RESULT_SUCCESS && pin.length() > 0) {
Jon Spivack9c3bc762021-10-06 20:53:09 +00002885 UiccController.getInstance().getPinStorage().storePin(pin, mPhoneId);
Michele Berionne5e411512020-11-13 02:36:59 +00002886 }
2887
Wink Saville9de0f752013-10-22 19:04:03 -07002888 return resultArray;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002889 }
2890 }
2891
Nathan Harold7c8d0f12020-05-28 20:40:31 -07002892 /**
2893 * This method has been removed due to privacy and stability concerns.
2894 */
2895 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002896 public void updateServiceLocation() {
Nathan Harold7c8d0f12020-05-28 20:40:31 -07002897 Log.e(LOG_TAG, "Call to unsupported method updateServiceLocation()");
2898 return;
Wink Saville36469e72014-06-11 15:17:00 -07002899 }
2900
Nathan Harold1f889d82020-06-04 17:05:26 -07002901 @Override
2902 public void updateServiceLocationWithPackageName(String callingPackage) {
2903 mApp.getSystemService(AppOpsManager.class)
2904 .checkPackage(Binder.getCallingUid(), callingPackage);
2905
Nathan Haroldf096d982020-11-18 17:18:06 -08002906 final int targetSdk = TelephonyPermissions.getTargetSdk(mApp, callingPackage);
Nathan Harold1f889d82020-06-04 17:05:26 -07002907 if (targetSdk > android.os.Build.VERSION_CODES.R) {
2908 // Callers targeting S have no business invoking this method.
2909 return;
2910 }
2911
2912 LocationAccessPolicy.LocationPermissionResult locationResult =
2913 LocationAccessPolicy.checkLocationPermission(mApp,
2914 new LocationAccessPolicy.LocationPermissionQuery.Builder()
2915 .setCallingPackage(callingPackage)
2916 .setCallingFeatureId(null)
2917 .setCallingPid(Binder.getCallingPid())
2918 .setCallingUid(Binder.getCallingUid())
2919 .setMethod("updateServiceLocation")
2920 .setMinSdkVersionForCoarse(Build.VERSION_CODES.BASE)
2921 .setMinSdkVersionForFine(Build.VERSION_CODES.Q)
2922 .build());
2923 // Apps that lack location permission have no business calling this method;
2924 // however, because no permission was declared in the public API, denials must
2925 // all be "soft".
2926 switch (locationResult) {
2927 case DENIED_HARD: /* fall through */
2928 case DENIED_SOFT:
2929 return;
2930 }
2931
2932 WorkSource workSource = getWorkSource(Binder.getCallingUid());
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002933 final long identity = Binder.clearCallingIdentity();
2934 try {
Nathan Harold1f889d82020-06-04 17:05:26 -07002935 final Phone phone = getPhone(getDefaultSubscription());
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002936 if (phone != null) {
Nathan Harold1f889d82020-06-04 17:05:26 -07002937 phone.updateServiceLocation(workSource);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002938 }
2939 } finally {
2940 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07002941 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002942 }
2943
Philip P. Moltmann700a9592019-10-03 11:53:50 -07002944 @Deprecated
Robert Greenwalt36b23af2015-07-06 17:59:14 -07002945 @Override
2946 public boolean isRadioOn(String callingPackage) {
Philip P. Moltmann700a9592019-10-03 11:53:50 -07002947 return isRadioOnWithFeature(callingPackage, null);
2948 }
2949
2950
2951 @Override
2952 public boolean isRadioOnWithFeature(String callingPackage, String callingFeatureId) {
2953 return isRadioOnForSubscriberWithFeature(getDefaultSubscription(), callingPackage,
2954 callingFeatureId);
2955 }
2956
2957 @Deprecated
2958 @Override
2959 public boolean isRadioOnForSubscriber(int subId, String callingPackage) {
2960 return isRadioOnForSubscriberWithFeature(subId, callingPackage, null);
Wink Saville36469e72014-06-11 15:17:00 -07002961 }
2962
Robert Greenwalt36b23af2015-07-06 17:59:14 -07002963 @Override
Philip P. Moltmann700a9592019-10-03 11:53:50 -07002964 public boolean isRadioOnForSubscriberWithFeature(int subId, String callingPackage,
2965 String callingFeatureId) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08002966 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
Philip P. Moltmann700a9592019-10-03 11:53:50 -07002967 mApp, subId, callingPackage, callingFeatureId, "isRadioOnForSubscriber")) {
Robert Greenwalt36b23af2015-07-06 17:59:14 -07002968 return false;
2969 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002970
2971 final long identity = Binder.clearCallingIdentity();
2972 try {
2973 return isRadioOnForSubscriber(subId);
2974 } finally {
2975 Binder.restoreCallingIdentity(identity);
2976 }
Robert Greenwalt36b23af2015-07-06 17:59:14 -07002977 }
2978
2979 private boolean isRadioOnForSubscriber(int subId) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002980 final long identity = Binder.clearCallingIdentity();
2981 try {
2982 final Phone phone = getPhone(subId);
2983 if (phone != null) {
2984 return phone.getServiceState().getState() != ServiceState.STATE_POWER_OFF;
2985 } else {
2986 return false;
2987 }
2988 } finally {
2989 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07002990 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002991 }
2992
2993 public void toggleRadioOnOff() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07002994 toggleRadioOnOffForSubscriber(getDefaultSubscription());
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002995 }
Wink Saville36469e72014-06-11 15:17:00 -07002996
Wink Savilleb564aae2014-10-23 10:18:09 -07002997 public void toggleRadioOnOffForSubscriber(int subId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002998 enforceModifyPermission();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002999
3000 final long identity = Binder.clearCallingIdentity();
3001 try {
3002 final Phone phone = getPhone(subId);
3003 if (phone != null) {
3004 phone.setRadioPower(!isRadioOnForSubscriber(subId));
3005 }
3006 } finally {
3007 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07003008 }
Wink Saville36469e72014-06-11 15:17:00 -07003009 }
3010
3011 public boolean setRadio(boolean turnOn) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07003012 return setRadioForSubscriber(getDefaultSubscription(), turnOn);
Wink Saville36469e72014-06-11 15:17:00 -07003013 }
3014
Wink Savilleb564aae2014-10-23 10:18:09 -07003015 public boolean setRadioForSubscriber(int subId, boolean turnOn) {
Wink Saville36469e72014-06-11 15:17:00 -07003016 enforceModifyPermission();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003017
3018 final long identity = Binder.clearCallingIdentity();
3019 try {
3020 final Phone phone = getPhone(subId);
3021 if (phone == null) {
3022 return false;
3023 }
3024 if ((phone.getServiceState().getState() != ServiceState.STATE_POWER_OFF) != turnOn) {
3025 toggleRadioOnOffForSubscriber(subId);
3026 }
3027 return true;
3028 } finally {
3029 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07003030 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003031 }
Wink Saville36469e72014-06-11 15:17:00 -07003032
Naveen Kalla1fd79bd2014-08-08 00:48:59 -07003033 public boolean needMobileRadioShutdown() {
Shuo Qianfa7b6b32019-12-10 10:40:38 -08003034 enforceReadPrivilegedPermission("needMobileRadioShutdown");
Naveen Kalla1fd79bd2014-08-08 00:48:59 -07003035 /*
3036 * If any of the Radios are available, it will need to be
3037 * shutdown. So return true if any Radio is available.
3038 */
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003039 final long identity = Binder.clearCallingIdentity();
3040 try {
3041 for (int i = 0; i < TelephonyManager.getDefault().getPhoneCount(); i++) {
3042 Phone phone = PhoneFactory.getPhone(i);
3043 if (phone != null && phone.isRadioAvailable()) return true;
3044 }
3045 logv(TelephonyManager.getDefault().getPhoneCount() + " Phones are shutdown.");
3046 return false;
3047 } finally {
3048 Binder.restoreCallingIdentity(identity);
Naveen Kalla1fd79bd2014-08-08 00:48:59 -07003049 }
Naveen Kalla1fd79bd2014-08-08 00:48:59 -07003050 }
3051
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003052 @Override
Naveen Kalla1fd79bd2014-08-08 00:48:59 -07003053 public void shutdownMobileRadios() {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003054 enforceModifyPermission();
3055
3056 final long identity = Binder.clearCallingIdentity();
3057 try {
3058 for (int i = 0; i < TelephonyManager.getDefault().getPhoneCount(); i++) {
3059 logv("Shutting down Phone " + i);
3060 shutdownRadioUsingPhoneId(i);
3061 }
3062 } finally {
3063 Binder.restoreCallingIdentity(identity);
Naveen Kalla1fd79bd2014-08-08 00:48:59 -07003064 }
3065 }
3066
3067 private void shutdownRadioUsingPhoneId(int phoneId) {
Naveen Kalla1fd79bd2014-08-08 00:48:59 -07003068 Phone phone = PhoneFactory.getPhone(phoneId);
3069 if (phone != null && phone.isRadioAvailable()) {
3070 phone.shutdownRadio();
3071 }
3072 }
3073
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003074 public boolean setRadioPower(boolean turnOn) {
Jack Yub4e16162017-05-15 12:48:40 -07003075 enforceModifyPermission();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003076
3077 final long identity = Binder.clearCallingIdentity();
3078 try {
3079 final Phone defaultPhone = PhoneFactory.getDefaultPhone();
3080 if (defaultPhone != null) {
3081 defaultPhone.setRadioPower(turnOn);
3082 return true;
3083 } else {
3084 loge("There's no default phone.");
3085 return false;
3086 }
3087 } finally {
3088 Binder.restoreCallingIdentity(identity);
Wei Liu9ae2a062016-08-08 11:09:34 -07003089 }
Wink Saville36469e72014-06-11 15:17:00 -07003090 }
3091
Wink Savilleb564aae2014-10-23 10:18:09 -07003092 public boolean setRadioPowerForSubscriber(int subId, boolean turnOn) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003093 enforceModifyPermission();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003094
3095 final long identity = Binder.clearCallingIdentity();
3096 try {
3097 final Phone phone = getPhone(subId);
3098 if (phone != null) {
3099 phone.setRadioPower(turnOn);
3100 return true;
3101 } else {
3102 return false;
3103 }
3104 } finally {
3105 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07003106 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003107 }
3108
Thomas Nguyenfd0572f2022-07-15 22:28:49 +00003109 /**
3110 * Vote on powering off the radio for a reason. The radio will be turned on only when there is
3111 * no reason to power it off. When any of the voters want to power it off, it will be turned
3112 * off. In case of emergency, the radio will be turned on even if there are some reasons for
3113 * powering it off, and these radio off votes will be cleared.
3114 * Multiple apps can vote for the same reason and the last vote will take effect. Each app is
3115 * responsible for its vote. A powering-off vote of a reason will be maintained until it is
3116 * cleared by calling {@link clearRadioPowerOffForReason} for that reason, or an emergency call
3117 * is made, or the device is rebooted. When an app comes backup from a crash, it needs to make
3118 * sure if its vote is as expected. An app can use the API {@link getRadioPowerOffReasons} to
3119 * check its vote.
3120 *
3121 * @param subId The subscription ID.
3122 * @param reason The reason for powering off radio.
3123 * @return true on success and false on failure.
3124 */
3125 public boolean requestRadioPowerOffForReason(int subId,
3126 @TelephonyManager.RadioPowerReason int reason) {
3127 enforceModifyPermission();
3128
3129 final long identity = Binder.clearCallingIdentity();
3130 try {
3131 final Phone phone = getPhone(subId);
3132 if (phone != null) {
3133 phone.setRadioPowerForReason(false, reason);
3134 return true;
3135 } else {
3136 return false;
3137 }
3138 } finally {
3139 Binder.restoreCallingIdentity(identity);
3140 }
3141 }
3142
3143 /**
3144 * Remove the vote on powering off the radio for a reason, as requested by
3145 * {@link requestRadioPowerOffForReason}.
3146 *
3147 * @param subId The subscription ID.
3148 * @param reason The reason for powering off radio.
3149 * @return true on success and false on failure.
3150 */
3151 public boolean clearRadioPowerOffForReason(int subId,
3152 @TelephonyManager.RadioPowerReason int reason) {
3153 enforceModifyPermission();
3154
3155 final long identity = Binder.clearCallingIdentity();
3156 try {
3157 final Phone phone = getPhone(subId);
3158 if (phone != null) {
3159 phone.setRadioPowerForReason(true, reason);
3160 return true;
3161 } else {
3162 return false;
3163 }
3164 } finally {
3165 Binder.restoreCallingIdentity(identity);
3166 }
3167 }
3168
3169 /**
3170 * Get reasons for powering off radio, as requested by {@link requestRadioPowerOffForReason}.
3171 *
3172 * @param subId The subscription ID.
3173 * @param callingPackage The package making the call.
3174 * @param callingFeatureId The feature in the package.
3175 * @return List of reasons for powering off radio.
3176 */
3177 public List getRadioPowerOffReasons(int subId, String callingPackage, String callingFeatureId) {
3178 enforceReadPrivilegedPermission("getRadioPowerOffReasons");
3179
3180 final long identity = Binder.clearCallingIdentity();
3181 List result = new ArrayList();
3182 try {
3183 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(mApp, subId,
3184 callingPackage, callingFeatureId, "getRadioPowerOffReasons")) {
3185 return result;
3186 }
3187
3188 final Phone phone = getPhone(subId);
3189 if (phone != null) {
3190 result.addAll(phone.getRadioPowerOffReasons());
3191 }
3192 } finally {
3193 Binder.restoreCallingIdentity(identity);
3194 }
3195 return result;
3196 }
3197
Wink Saville36469e72014-06-11 15:17:00 -07003198 // FIXME: subId version needed
Sanket Padawe356d7632015-06-22 14:03:32 -07003199 @Override
Sarah Chinecc78c42022-03-31 21:16:48 -07003200 public boolean enableDataConnectivity(String callingPackage) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003201 enforceModifyPermission();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003202
3203 final long identity = Binder.clearCallingIdentity();
3204 try {
Jack Yu285100e2022-12-02 22:48:35 -08003205 int subId = SubscriptionManager.getDefaultDataSubscriptionId();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003206 final Phone phone = getPhone(subId);
3207 if (phone != null) {
Jack Yu7968c6d2022-07-31 00:43:21 -07003208 phone.getDataSettingsManager().setDataEnabled(
3209 TelephonyManager.DATA_ENABLED_REASON_USER, true, callingPackage);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003210 return true;
3211 } else {
3212 return false;
3213 }
3214 } finally {
3215 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07003216 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003217 }
3218
Wink Saville36469e72014-06-11 15:17:00 -07003219 // FIXME: subId version needed
Sanket Padawe356d7632015-06-22 14:03:32 -07003220 @Override
Sarah Chinecc78c42022-03-31 21:16:48 -07003221 public boolean disableDataConnectivity(String callingPackage) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003222 enforceModifyPermission();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003223
3224 final long identity = Binder.clearCallingIdentity();
3225 try {
Jack Yu285100e2022-12-02 22:48:35 -08003226 int subId = SubscriptionManager.getDefaultDataSubscriptionId();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003227 final Phone phone = getPhone(subId);
3228 if (phone != null) {
Jack Yu7968c6d2022-07-31 00:43:21 -07003229 phone.getDataSettingsManager().setDataEnabled(
3230 TelephonyManager.DATA_ENABLED_REASON_USER, false, callingPackage);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003231 return true;
3232 } else {
3233 return false;
3234 }
3235 } finally {
3236 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07003237 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003238 }
3239
Sanket Padawe356d7632015-06-22 14:03:32 -07003240 @Override
Jack Yuacf8a132017-05-01 17:00:48 -07003241 public boolean isDataConnectivityPossible(int subId) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003242 final long identity = Binder.clearCallingIdentity();
3243 try {
3244 final Phone phone = getPhone(subId);
3245 if (phone != null) {
Jack Yu59824e12022-03-23 01:42:44 -07003246 return phone.isDataAllowed();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003247 } else {
3248 return false;
3249 }
3250 } finally {
3251 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07003252 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003253 }
3254
3255 public boolean handlePinMmi(String dialString) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07003256 return handlePinMmiForSubscriber(getDefaultSubscription(), dialString);
Wink Saville36469e72014-06-11 15:17:00 -07003257 }
3258
pkanwarae03a6b2016-11-06 20:37:09 -08003259 public void handleUssdRequest(int subId, String ussdRequest, ResultReceiver wrappedCallback) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003260 enforceCallPermission();
3261
3262 final long identity = Binder.clearCallingIdentity();
3263 try {
3264 if (!SubscriptionManager.isValidSubscriptionId(subId)) {
3265 return;
3266 }
3267 Pair<String, ResultReceiver> ussdObject = new Pair(ussdRequest, wrappedCallback);
3268 sendRequest(CMD_HANDLE_USSD_REQUEST, ussdObject, subId);
3269 } finally {
3270 Binder.restoreCallingIdentity(identity);
3271 }
pkanwar32d516d2016-10-14 19:37:38 -07003272 };
3273
Wink Savilleb564aae2014-10-23 10:18:09 -07003274 public boolean handlePinMmiForSubscriber(int subId, String dialString) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003275 enforceModifyPermission();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003276
3277 final long identity = Binder.clearCallingIdentity();
3278 try {
3279 if (!SubscriptionManager.isValidSubscriptionId(subId)) {
3280 return false;
3281 }
3282 return (Boolean) sendRequest(CMD_HANDLE_PIN_MMI, dialString, subId);
3283 } finally {
3284 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07003285 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003286 }
3287
Brad Ebinger4f6208e2021-03-23 21:04:45 +00003288 /**
3289 * @deprecated This method is deprecated and is only being kept due to an UnsupportedAppUsage
3290 * tag on getCallState Binder call.
3291 */
3292 @Deprecated
3293 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003294 public int getCallState() {
Brad Ebinger4f6208e2021-03-23 21:04:45 +00003295 if (CompatChanges.isChangeEnabled(
3296 TelecomManager.ENABLE_GET_CALL_STATE_PERMISSION_PROTECTION,
3297 Binder.getCallingUid())) {
3298 // Do not allow this API to be called on API version 31+, it should only be
3299 // called on old apps using this Binder call directly.
3300 throw new SecurityException("This method can only be used for applications "
3301 + "targeting API version 30 or less.");
3302 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003303 final long identity = Binder.clearCallingIdentity();
3304 try {
Brad Ebinger4f6208e2021-03-23 21:04:45 +00003305 Phone phone = getPhone(getDefaultSubscription());
3306 return phone == null ? TelephonyManager.CALL_STATE_IDLE :
3307 PhoneConstantConversions.convertCallState(phone.getState());
3308 } finally {
3309 Binder.restoreCallingIdentity(identity);
3310 }
3311 }
3312
3313 @Override
3314 public int getCallStateForSubscription(int subId, String callingPackage, String featureId) {
3315 if (CompatChanges.isChangeEnabled(
3316 TelecomManager.ENABLE_GET_CALL_STATE_PERMISSION_PROTECTION,
3317 Binder.getCallingUid())) {
3318 // Check READ_PHONE_STATE for API version 31+
3319 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(mApp, subId, callingPackage,
3320 featureId, "getCallStateForSubscription")) {
3321 throw new SecurityException("getCallState requires READ_PHONE_STATE for apps "
3322 + "targeting API level 31+.");
3323 }
3324 }
3325 final long identity = Binder.clearCallingIdentity();
3326 try {
3327 Phone phone = getPhone(subId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003328 return phone == null ? TelephonyManager.CALL_STATE_IDLE :
3329 PhoneConstantConversions.convertCallState(phone.getState());
3330 } finally {
3331 Binder.restoreCallingIdentity(identity);
3332 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003333 }
3334
Sanket Padawe356d7632015-06-22 14:03:32 -07003335 @Override
Nathan Harolde037c472019-06-26 00:41:07 +00003336 public int getDataState() {
Jack Yu285100e2022-12-02 22:48:35 -08003337 return getDataStateForSubId(SubscriptionManager.getDefaultDataSubscriptionId());
Nathan Haroldc4689b12019-06-14 16:58:30 -07003338 }
3339
3340 @Override
3341 public int getDataStateForSubId(int subId) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003342 final long identity = Binder.clearCallingIdentity();
3343 try {
Nathan Haroldc4689b12019-06-14 16:58:30 -07003344 final Phone phone = getPhone(subId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003345 if (phone != null) {
Jack Yu7968c6d2022-07-31 00:43:21 -07003346 return phone.getDataNetworkController().getInternetDataNetworkState();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003347 } else {
3348 return PhoneConstantConversions.convertDataState(
3349 PhoneConstants.DataState.DISCONNECTED);
3350 }
3351 } finally {
3352 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07003353 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003354 }
3355
Sanket Padawe356d7632015-06-22 14:03:32 -07003356 @Override
Jack Yu0eda6842022-04-18 00:34:46 -07003357 public @DataActivityType int getDataActivity() {
Jack Yu285100e2022-12-02 22:48:35 -08003358 return getDataActivityForSubId(SubscriptionManager.getDefaultDataSubscriptionId());
Nathan Haroldc4689b12019-06-14 16:58:30 -07003359 }
3360
3361 @Override
Jack Yu0eda6842022-04-18 00:34:46 -07003362 public @DataActivityType int getDataActivityForSubId(int subId) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003363 final long identity = Binder.clearCallingIdentity();
3364 try {
Nathan Haroldc4689b12019-06-14 16:58:30 -07003365 final Phone phone = getPhone(subId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003366 if (phone != null) {
Jack Yu0eda6842022-04-18 00:34:46 -07003367 return phone.getDataActivityState();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003368 } else {
3369 return TelephonyManager.DATA_ACTIVITY_NONE;
3370 }
3371 } finally {
3372 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07003373 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003374 }
3375
3376 @Override
Meng Wanga10e89e2019-12-09 13:13:01 -08003377 public CellIdentity getCellLocation(String callingPackage, String callingFeatureId) {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08003378 mApp.getSystemService(AppOpsManager.class)
Hall Liu1aa510f2017-11-22 17:40:08 -08003379 .checkPackage(Binder.getCallingUid(), callingPackage);
Hall Liuf19c44f2018-11-27 14:38:17 -08003380
3381 LocationAccessPolicy.LocationPermissionResult locationResult =
3382 LocationAccessPolicy.checkLocationPermission(mApp,
3383 new LocationAccessPolicy.LocationPermissionQuery.Builder()
3384 .setCallingPackage(callingPackage)
Philip P. Moltmann3a2772a2019-10-04 08:15:00 -07003385 .setCallingFeatureId(callingFeatureId)
Hall Liuf19c44f2018-11-27 14:38:17 -08003386 .setCallingPid(Binder.getCallingPid())
3387 .setCallingUid(Binder.getCallingUid())
3388 .setMethod("getCellLocation")
Hall Liu773ba022020-01-24 18:07:12 -08003389 .setMinSdkVersionForCoarse(Build.VERSION_CODES.BASE)
Hall Liuf19c44f2018-11-27 14:38:17 -08003390 .setMinSdkVersionForFine(Build.VERSION_CODES.Q)
3391 .build());
3392 switch (locationResult) {
3393 case DENIED_HARD:
3394 throw new SecurityException("Not allowed to access cell location");
3395 case DENIED_SOFT:
Meng Wanga10e89e2019-12-09 13:13:01 -08003396 return (getDefaultPhone().getPhoneType() == PhoneConstants.PHONE_TYPE_CDMA)
3397 ? new CellIdentityCdma() : new CellIdentityGsm();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003398 }
3399
Narayan Kamathf04b5a12018-01-09 11:47:15 +00003400 WorkSource workSource = getWorkSource(Binder.getCallingUid());
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003401 final long identity = Binder.clearCallingIdentity();
3402 try {
3403 if (DBG_LOC) log("getCellLocation: is active user");
Jack Yu285100e2022-12-02 22:48:35 -08003404 int subId = SubscriptionManager.getDefaultDataSubscriptionId();
Meng Wanga10e89e2019-12-09 13:13:01 -08003405 return (CellIdentity) sendRequest(CMD_GET_CELL_LOCATION, workSource, subId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003406 } finally {
3407 Binder.restoreCallingIdentity(identity);
3408 }
Svetoslav64fad262015-04-14 14:35:21 -07003409 }
3410
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003411 @Override
Jack Yueb1e7fe2020-02-22 19:38:58 -08003412 public String getNetworkCountryIsoForPhone(int phoneId) {
Jonathan Basseribf5362b2017-07-19 12:22:35 -07003413 // Reporting the correct network country is ambiguous when IWLAN could conflict with
3414 // registered cell info, so return a NULL country instead.
3415 final long identity = Binder.clearCallingIdentity();
3416 try {
Malcolm Chen3732c2b2018-07-18 20:15:24 -07003417 if (phoneId == SubscriptionManager.INVALID_PHONE_INDEX) {
3418 // Get default phone in this case.
3419 phoneId = SubscriptionManager.DEFAULT_PHONE_INDEX;
3420 }
Jack Yu285100e2022-12-02 22:48:35 -08003421 final int subId = SubscriptionManager.getSubscriptionId(phoneId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003422 Phone phone = PhoneFactory.getPhone(phoneId);
Nathan Harold532f51c2020-04-21 19:31:10 -07003423 if (phone == null) return "";
3424 ServiceStateTracker sst = phone.getServiceStateTracker();
3425 if (sst == null) return "";
3426 LocaleTracker lt = sst.getLocaleTracker();
3427 if (lt == null) return "";
Shuo Qian9418a922021-03-09 11:21:16 -08003428 return lt.getCurrentCountry();
Jonathan Basseribf5362b2017-07-19 12:22:35 -07003429 } finally {
3430 Binder.restoreCallingIdentity(identity);
3431 }
Jonathan Basseribf5362b2017-07-19 12:22:35 -07003432 }
3433
Nathan Harold7c8d0f12020-05-28 20:40:31 -07003434 /**
3435 * This method was removed due to potential issues caused by performing partial
3436 * updates of service state, and lack of a credible use case.
3437 *
3438 * This has the ability to break the telephony implementation by disabling notification of
3439 * changes in device connectivity. DO NOT USE THIS!
3440 */
Jonathan Basseribf5362b2017-07-19 12:22:35 -07003441 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003442 public void enableLocationUpdates() {
3443 mApp.enforceCallingOrSelfPermission(
3444 android.Manifest.permission.CONTROL_LOCATION_UPDATES, null);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003445 }
3446
Nathan Harold7c8d0f12020-05-28 20:40:31 -07003447 /**
3448 * This method was removed due to potential issues caused by performing partial
3449 * updates of service state, and lack of a credible use case.
3450 *
3451 * This has the ability to break the telephony implementation by disabling notification of
3452 * changes in device connectivity. DO NOT USE THIS!
3453 */
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003454 @Override
3455 public void disableLocationUpdates() {
3456 mApp.enforceCallingOrSelfPermission(
3457 android.Manifest.permission.CONTROL_LOCATION_UPDATES, null);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003458 }
3459
3460 @Override
3461 @SuppressWarnings("unchecked")
Philip P. Moltmann3a2772a2019-10-04 08:15:00 -07003462 public List<NeighboringCellInfo> getNeighboringCellInfo(String callingPackage,
3463 String callingFeatureId) {
Nathan Haroldb55f63b2021-07-27 11:27:38 -07003464 try {
3465 mApp.getSystemService(AppOpsManager.class)
3466 .checkPackage(Binder.getCallingUid(), callingPackage);
3467 } catch (SecurityException e) {
3468 EventLog.writeEvent(0x534e4554, "190619791", Binder.getCallingUid());
3469 throw e;
3470 }
3471
Nathan Haroldf096d982020-11-18 17:18:06 -08003472 final int targetSdk = TelephonyPermissions.getTargetSdk(mApp, callingPackage);
Nathan Harolddbea45a2018-08-30 14:35:07 -07003473 if (targetSdk >= android.os.Build.VERSION_CODES.Q) {
3474 throw new SecurityException(
3475 "getNeighboringCellInfo() is unavailable to callers targeting Q+ SDK levels.");
3476 }
Nathan Haroldb4d55612018-07-20 13:13:08 -07003477
Jordan Liu1617b712019-07-10 15:06:26 -07003478 if (mAppOps.noteOp(AppOpsManager.OPSTR_NEIGHBORING_CELLS, Binder.getCallingUid(),
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003479 callingPackage) != AppOpsManager.MODE_ALLOWED) {
3480 return null;
3481 }
Svetoslav64fad262015-04-14 14:35:21 -07003482
Svetoslav Ganov4a9d4482017-06-20 19:53:35 -07003483 if (DBG_LOC) log("getNeighboringCellInfo: is active user");
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003484
Philip P. Moltmann3a2772a2019-10-04 08:15:00 -07003485 List<CellInfo> info = getAllCellInfo(callingPackage, callingFeatureId);
Nathan Haroldf180aac2018-06-01 18:43:55 -07003486 if (info == null) return null;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003487
Nathan Haroldf180aac2018-06-01 18:43:55 -07003488 List<NeighboringCellInfo> neighbors = new ArrayList<NeighboringCellInfo>();
3489 for (CellInfo ci : info) {
3490 if (ci instanceof CellInfoGsm) {
3491 neighbors.add(new NeighboringCellInfo((CellInfoGsm) ci));
3492 } else if (ci instanceof CellInfoWcdma) {
3493 neighbors.add(new NeighboringCellInfo((CellInfoWcdma) ci));
3494 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003495 }
Nathan Haroldf180aac2018-06-01 18:43:55 -07003496 return (neighbors.size()) > 0 ? neighbors : null;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003497 }
3498
Nathan Haroldfa8da0f2018-09-27 18:51:29 -07003499 private List<CellInfo> getCachedCellInfo() {
3500 List<CellInfo> cellInfos = new ArrayList<CellInfo>();
3501 for (Phone phone : PhoneFactory.getPhones()) {
3502 List<CellInfo> info = phone.getAllCellInfo();
3503 if (info != null) cellInfos.addAll(info);
3504 }
3505 return cellInfos;
3506 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003507
3508 @Override
Philip P. Moltmann3a2772a2019-10-04 08:15:00 -07003509 public List<CellInfo> getAllCellInfo(String callingPackage, String callingFeatureId) {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08003510 mApp.getSystemService(AppOpsManager.class)
Hall Liu1aa510f2017-11-22 17:40:08 -08003511 .checkPackage(Binder.getCallingUid(), callingPackage);
Hall Liuf19c44f2018-11-27 14:38:17 -08003512
3513 LocationAccessPolicy.LocationPermissionResult locationResult =
3514 LocationAccessPolicy.checkLocationPermission(mApp,
3515 new LocationAccessPolicy.LocationPermissionQuery.Builder()
3516 .setCallingPackage(callingPackage)
Philip P. Moltmann3a2772a2019-10-04 08:15:00 -07003517 .setCallingFeatureId(callingFeatureId)
Hall Liuf19c44f2018-11-27 14:38:17 -08003518 .setCallingPid(Binder.getCallingPid())
3519 .setCallingUid(Binder.getCallingUid())
3520 .setMethod("getAllCellInfo")
Nathan Harold5ae50b52019-02-20 15:46:36 -08003521 .setMinSdkVersionForCoarse(Build.VERSION_CODES.BASE)
Hall Liuf19c44f2018-11-27 14:38:17 -08003522 .setMinSdkVersionForFine(Build.VERSION_CODES.Q)
3523 .build());
3524 switch (locationResult) {
3525 case DENIED_HARD:
3526 throw new SecurityException("Not allowed to access cell info");
3527 case DENIED_SOFT:
3528 return new ArrayList<>();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003529 }
3530
Nathan Haroldf096d982020-11-18 17:18:06 -08003531 final int targetSdk = TelephonyPermissions.getTargetSdk(mApp, callingPackage);
Nathan Haroldfa8da0f2018-09-27 18:51:29 -07003532 if (targetSdk >= android.os.Build.VERSION_CODES.Q) {
3533 return getCachedCellInfo();
3534 }
3535
Svetoslav Ganov4a9d4482017-06-20 19:53:35 -07003536 if (DBG_LOC) log("getAllCellInfo: is active user");
Narayan Kamathf04b5a12018-01-09 11:47:15 +00003537 WorkSource workSource = getWorkSource(Binder.getCallingUid());
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003538 final long identity = Binder.clearCallingIdentity();
3539 try {
3540 List<CellInfo> cellInfos = new ArrayList<CellInfo>();
3541 for (Phone phone : PhoneFactory.getPhones()) {
Nathan Harold3ff88932018-08-14 10:19:49 -07003542 final List<CellInfo> info = (List<CellInfo>) sendRequest(
Nathan Harold92bed182018-10-12 18:16:49 -07003543 CMD_GET_ALL_CELL_INFO, null, phone, workSource);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003544 if (info != null) cellInfos.addAll(info);
3545 }
3546 return cellInfos;
3547 } finally {
3548 Binder.restoreCallingIdentity(identity);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003549 }
3550 }
3551
Sailesh Nepalbd76e4e2013-10-27 13:59:44 -07003552 @Override
Philip P. Moltmann3a2772a2019-10-04 08:15:00 -07003553 public void requestCellInfoUpdate(int subId, ICellInfoCallback cb, String callingPackage,
3554 String callingFeatureId) {
3555 requestCellInfoUpdateInternal(subId, cb, callingPackage, callingFeatureId,
3556 getWorkSource(Binder.getCallingUid()));
Nathan Haroldfa8da0f2018-09-27 18:51:29 -07003557 }
3558
3559 @Override
Philip P. Moltmann3a2772a2019-10-04 08:15:00 -07003560 public void requestCellInfoUpdateWithWorkSource(int subId, ICellInfoCallback cb,
3561 String callingPackage, String callingFeatureId, WorkSource workSource) {
Nathan Haroldfa8da0f2018-09-27 18:51:29 -07003562 enforceModifyPermission();
Philip P. Moltmann3a2772a2019-10-04 08:15:00 -07003563 requestCellInfoUpdateInternal(subId, cb, callingPackage, callingFeatureId, workSource);
Nathan Haroldfa8da0f2018-09-27 18:51:29 -07003564 }
3565
Philip P. Moltmann3a2772a2019-10-04 08:15:00 -07003566 private void requestCellInfoUpdateInternal(int subId, ICellInfoCallback cb,
3567 String callingPackage, String callingFeatureId, WorkSource workSource) {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08003568 mApp.getSystemService(AppOpsManager.class)
Nathan Haroldfa8da0f2018-09-27 18:51:29 -07003569 .checkPackage(Binder.getCallingUid(), callingPackage);
Hall Liuf19c44f2018-11-27 14:38:17 -08003570
3571 LocationAccessPolicy.LocationPermissionResult locationResult =
3572 LocationAccessPolicy.checkLocationPermission(mApp,
3573 new LocationAccessPolicy.LocationPermissionQuery.Builder()
3574 .setCallingPackage(callingPackage)
Philip P. Moltmann3a2772a2019-10-04 08:15:00 -07003575 .setCallingFeatureId(callingFeatureId)
Hall Liuf19c44f2018-11-27 14:38:17 -08003576 .setCallingPid(Binder.getCallingPid())
3577 .setCallingUid(Binder.getCallingUid())
3578 .setMethod("requestCellInfoUpdate")
Hall Liud60acc92020-05-21 17:09:35 -07003579 .setMinSdkVersionForCoarse(Build.VERSION_CODES.BASE)
3580 .setMinSdkVersionForFine(Build.VERSION_CODES.BASE)
Hall Liuf19c44f2018-11-27 14:38:17 -08003581 .build());
3582 switch (locationResult) {
3583 case DENIED_HARD:
Nathan Haroldf096d982020-11-18 17:18:06 -08003584 if (TelephonyPermissions
3585 .getTargetSdk(mApp, callingPackage) < Build.VERSION_CODES.Q) {
Hall Liud60acc92020-05-21 17:09:35 -07003586 // Safetynet logging for b/154934934
3587 EventLog.writeEvent(0x534e4554, "154934934", Binder.getCallingUid());
3588 }
Hall Liuf19c44f2018-11-27 14:38:17 -08003589 throw new SecurityException("Not allowed to access cell info");
3590 case DENIED_SOFT:
Nathan Haroldf096d982020-11-18 17:18:06 -08003591 if (TelephonyPermissions
3592 .getTargetSdk(mApp, callingPackage) < Build.VERSION_CODES.Q) {
Hall Liud60acc92020-05-21 17:09:35 -07003593 // Safetynet logging for b/154934934
3594 EventLog.writeEvent(0x534e4554, "154934934", Binder.getCallingUid());
3595 }
Nathan Harold5320c422019-05-09 10:26:08 -07003596 try {
3597 cb.onCellInfo(new ArrayList<CellInfo>());
3598 } catch (RemoteException re) {
3599 // Drop without consequences
3600 }
Hall Liuf19c44f2018-11-27 14:38:17 -08003601 return;
Nathan Haroldfa8da0f2018-09-27 18:51:29 -07003602 }
3603
Nathan Harolda939a962019-05-09 10:13:47 -07003604
3605 final Phone phone = getPhoneFromSubId(subId);
Nathan Haroldfa8da0f2018-09-27 18:51:29 -07003606 if (phone == null) throw new IllegalArgumentException("Invalid Subscription Id: " + subId);
3607
3608 sendRequestAsync(CMD_REQUEST_CELL_INFO_UPDATE, cb, phone, workSource);
3609 }
3610
3611 @Override
Aishwarya Mallampati0603fb12022-08-24 21:16:56 +00003612 public void setCellInfoListRate(int rateInMillis, int subId) {
Jack Yua8d8cb82017-01-16 10:15:34 -08003613 enforceModifyPermission();
Narayan Kamathf04b5a12018-01-09 11:47:15 +00003614 WorkSource workSource = getWorkSource(Binder.getCallingUid());
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003615
3616 final long identity = Binder.clearCallingIdentity();
3617 try {
Aishwarya Mallampati0603fb12022-08-24 21:16:56 +00003618 Phone phone = getPhone(subId);
3619 if (phone == null) {
3620 getDefaultPhone().setCellInfoListRate(rateInMillis, workSource);
3621 } else {
3622 phone.setCellInfoListRate(rateInMillis, workSource);
3623 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003624 } finally {
3625 Binder.restoreCallingIdentity(identity);
3626 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003627 }
3628
Shishir Agrawala9f32182016-04-12 12:00:16 -07003629 @Override
Philip P. Moltmann700a9592019-10-03 11:53:50 -07003630 public String getImeiForSlot(int slotIndex, String callingPackage, String callingFeatureId) {
Jeff Davidson913390f2018-02-23 17:11:49 -08003631 Phone phone = PhoneFactory.getPhone(slotIndex);
3632 if (phone == null) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08003633 return null;
3634 }
Jeff Davidson913390f2018-02-23 17:11:49 -08003635 int subId = phone.getSubId();
Grace Jia0ddb3612021-04-22 13:35:26 -07003636 enforceCallingPackage(callingPackage, Binder.getCallingUid(), "getImeiForSlot");
Michael Groover70af6dc2018-10-01 16:23:15 -07003637 if (!TelephonyPermissions.checkCallingOrSelfReadDeviceIdentifiers(mApp, subId,
Philip P. Moltmann700a9592019-10-03 11:53:50 -07003638 callingPackage, callingFeatureId, "getImeiForSlot")) {
Jeff Davidson913390f2018-02-23 17:11:49 -08003639 return null;
3640 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003641
3642 final long identity = Binder.clearCallingIdentity();
3643 try {
3644 return phone.getImei();
3645 } finally {
3646 Binder.restoreCallingIdentity(identity);
3647 }
Shishir Agrawala9f32182016-04-12 12:00:16 -07003648 }
3649
3650 @Override
arunvoddud5c6ce02022-12-11 06:03:12 +00003651 public String getPrimaryImei(String callingPackage, String callingFeatureId) {
3652 enforceCallingPackage(callingPackage, Binder.getCallingUid(), "getPrimaryImei");
3653 if (!checkCallingOrSelfReadDeviceIdentifiersForAnySub(mApp, callingPackage,
3654 callingFeatureId, "getPrimaryImei")) {
3655 throw new SecurityException("Caller does not have permission");
3656 }
3657 final long identity = Binder.clearCallingIdentity();
3658 try {
3659 for (Phone phone : PhoneFactory.getPhones()) {
3660 if (phone.getImeiType() == Phone.IMEI_TYPE_PRIMARY) {
3661 return phone.getImei();
3662 }
3663 }
3664 throw new UnsupportedOperationException("Operation not supported");
3665 } finally {
3666 Binder.restoreCallingIdentity(identity);
3667 }
3668 }
3669
3670 @Override
David Kelly5e06a7f2018-03-12 14:10:59 +00003671 public String getTypeAllocationCodeForSlot(int slotIndex) {
3672 Phone phone = PhoneFactory.getPhone(slotIndex);
3673 String tac = null;
3674 if (phone != null) {
3675 String imei = phone.getImei();
Vala Zadehab005552021-09-21 15:54:29 -07003676 try {
3677 tac = imei == null ? null : imei.substring(0, TYPE_ALLOCATION_CODE_LENGTH);
3678 } catch (IndexOutOfBoundsException e) {
3679 Log.e(LOG_TAG, "IMEI length shorter than upper index.");
3680 return null;
3681 }
David Kelly5e06a7f2018-03-12 14:10:59 +00003682 }
3683 return tac;
3684 }
3685
3686 @Override
Philip P. Moltmann700a9592019-10-03 11:53:50 -07003687 public String getMeidForSlot(int slotIndex, String callingPackage, String callingFeatureId) {
Shuo Qian13d89152021-05-10 23:58:11 -07003688 try {
3689 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
3690 } catch (SecurityException se) {
3691 EventLog.writeEvent(0x534e4554, "186530496", Binder.getCallingUid());
3692 throw new SecurityException("Package " + callingPackage + " does not belong to "
3693 + Binder.getCallingUid());
3694 }
Jeff Davidson913390f2018-02-23 17:11:49 -08003695 Phone phone = PhoneFactory.getPhone(slotIndex);
3696 if (phone == null) {
Jack Yu2af8d712017-03-15 17:14:14 -07003697 return null;
3698 }
Nazanin Bakhshif782e562018-12-11 15:15:39 -08003699
Jeff Davidson913390f2018-02-23 17:11:49 -08003700 int subId = phone.getSubId();
Michael Groover70af6dc2018-10-01 16:23:15 -07003701 if (!TelephonyPermissions.checkCallingOrSelfReadDeviceIdentifiers(mApp, subId,
Philip P. Moltmann700a9592019-10-03 11:53:50 -07003702 callingPackage, callingFeatureId, "getMeidForSlot")) {
Jeff Davidson913390f2018-02-23 17:11:49 -08003703 return null;
3704 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003705
3706 final long identity = Binder.clearCallingIdentity();
3707 try {
3708 return phone.getMeid();
3709 } finally {
3710 Binder.restoreCallingIdentity(identity);
3711 }
Jack Yu2af8d712017-03-15 17:14:14 -07003712 }
3713
3714 @Override
David Kelly5e06a7f2018-03-12 14:10:59 +00003715 public String getManufacturerCodeForSlot(int slotIndex) {
3716 Phone phone = PhoneFactory.getPhone(slotIndex);
3717 String manufacturerCode = null;
3718 if (phone != null) {
3719 String meid = phone.getMeid();
Vala Zadehab005552021-09-21 15:54:29 -07003720 try {
3721 manufacturerCode =
3722 meid == null ? null : meid.substring(0, MANUFACTURER_CODE_LENGTH);
3723 } catch (IndexOutOfBoundsException e) {
3724 Log.e(LOG_TAG, "MEID length shorter than upper index.");
3725 return null;
3726 }
David Kelly5e06a7f2018-03-12 14:10:59 +00003727 }
3728 return manufacturerCode;
3729 }
3730
3731 @Override
Philip P. Moltmann700a9592019-10-03 11:53:50 -07003732 public String getDeviceSoftwareVersionForSlot(int slotIndex, String callingPackage,
3733 String callingFeatureId) {
Jeff Davidson913390f2018-02-23 17:11:49 -08003734 Phone phone = PhoneFactory.getPhone(slotIndex);
3735 if (phone == null) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08003736 return null;
3737 }
Jeff Davidson913390f2018-02-23 17:11:49 -08003738 int subId = phone.getSubId();
3739 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
Philip P. Moltmann700a9592019-10-03 11:53:50 -07003740 mApp, subId, callingPackage, callingFeatureId,
3741 "getDeviceSoftwareVersionForSlot")) {
Jeff Davidson913390f2018-02-23 17:11:49 -08003742 return null;
3743 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003744
3745 final long identity = Binder.clearCallingIdentity();
3746 try {
3747 return phone.getDeviceSvn();
3748 } finally {
3749 Binder.restoreCallingIdentity(identity);
3750 }
Shishir Agrawala9f32182016-04-12 12:00:16 -07003751 }
3752
fionaxu43304da2017-11-27 22:51:16 -08003753 @Override
3754 public int getSubscriptionCarrierId(int subId) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003755 final long identity = Binder.clearCallingIdentity();
3756 try {
3757 final Phone phone = getPhone(subId);
3758 return phone == null ? TelephonyManager.UNKNOWN_CARRIER_ID : phone.getCarrierId();
3759 } finally {
3760 Binder.restoreCallingIdentity(identity);
3761 }
fionaxu43304da2017-11-27 22:51:16 -08003762 }
3763
3764 @Override
3765 public String getSubscriptionCarrierName(int subId) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003766 final long identity = Binder.clearCallingIdentity();
3767 try {
3768 final Phone phone = getPhone(subId);
3769 return phone == null ? null : phone.getCarrierName();
3770 } finally {
3771 Binder.restoreCallingIdentity(identity);
3772 }
fionaxu43304da2017-11-27 22:51:16 -08003773 }
3774
calvinpanffe225e2018-11-01 19:43:06 +08003775 @Override
chen xu0026ca62019-03-06 15:28:50 -08003776 public int getSubscriptionSpecificCarrierId(int subId) {
chen xu25637222018-11-04 17:17:00 -08003777 final long identity = Binder.clearCallingIdentity();
3778 try {
3779 final Phone phone = getPhone(subId);
3780 return phone == null ? TelephonyManager.UNKNOWN_CARRIER_ID
chen xu0026ca62019-03-06 15:28:50 -08003781 : phone.getSpecificCarrierId();
chen xu25637222018-11-04 17:17:00 -08003782 } finally {
3783 Binder.restoreCallingIdentity(identity);
3784 }
3785 }
3786
3787 @Override
chen xu0026ca62019-03-06 15:28:50 -08003788 public String getSubscriptionSpecificCarrierName(int subId) {
chen xu25637222018-11-04 17:17:00 -08003789 final long identity = Binder.clearCallingIdentity();
3790 try {
3791 final Phone phone = getPhone(subId);
chen xu0026ca62019-03-06 15:28:50 -08003792 return phone == null ? null : phone.getSpecificCarrierName();
chen xu25637222018-11-04 17:17:00 -08003793 } finally {
3794 Binder.restoreCallingIdentity(identity);
3795 }
3796 }
3797
chen xu651eec72018-11-11 19:03:44 -08003798 @Override
chen xu864e11c2018-12-06 22:10:03 -08003799 public int getCarrierIdFromMccMnc(int slotIndex, String mccmnc, boolean isSubscriptionMccMnc) {
3800 if (!isSubscriptionMccMnc) {
3801 enforceReadPrivilegedPermission("getCarrierIdFromMccMnc");
3802 }
chen xu651eec72018-11-11 19:03:44 -08003803 final Phone phone = PhoneFactory.getPhone(slotIndex);
3804 if (phone == null) {
3805 return TelephonyManager.UNKNOWN_CARRIER_ID;
3806 }
3807 final long identity = Binder.clearCallingIdentity();
3808 try {
3809 return CarrierResolver.getCarrierIdFromMccMnc(phone.getContext(), mccmnc);
3810 } finally {
3811 Binder.restoreCallingIdentity(identity);
3812 }
3813 }
3814
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003815 //
3816 // Internal helper methods.
3817 //
3818
Sanket Padaweee13a9b2016-03-08 17:30:28 -08003819 /**
Grace Jia0ddb3612021-04-22 13:35:26 -07003820 * Make sure the caller is the calling package itself
3821 *
3822 * @throws SecurityException if the caller is not the calling package
3823 */
3824 private void enforceCallingPackage(String callingPackage, int callingUid, String message) {
3825 int packageUid = -1;
Grace Jiadbefca02021-04-26 15:13:31 -07003826 PackageManager pm = mApp.getBaseContext().createContextAsUser(
3827 UserHandle.getUserHandleForUid(callingUid), 0).getPackageManager();
Grace Jia0ddb3612021-04-22 13:35:26 -07003828 try {
Grace Jiadbefca02021-04-26 15:13:31 -07003829 packageUid = pm.getPackageUid(callingPackage, 0);
Grace Jia0ddb3612021-04-22 13:35:26 -07003830 } catch (PackageManager.NameNotFoundException e) {
3831 // packageUid is -1
3832 }
3833 if (packageUid != callingUid) {
3834 throw new SecurityException(message + ": Package " + callingPackage
3835 + " does not belong to " + callingUid);
3836 }
3837 }
3838
3839 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003840 * Make sure the caller has the MODIFY_PHONE_STATE permission.
3841 *
3842 * @throws SecurityException if the caller does not have the required permission
3843 */
Gil Cukierman1c0eb932022-12-06 22:28:24 +00003844 @VisibleForTesting
3845 public void enforceModifyPermission() {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003846 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.MODIFY_PHONE_STATE, null);
3847 }
3848
Gil Cukierman1c0eb932022-12-06 22:28:24 +00003849 /**
arunvoddud7401012022-12-15 16:08:12 +00003850 * Make sure the caller has the READ_PHONE_STATE permission.
Gil Cukierman1c0eb932022-12-06 22:28:24 +00003851 *
3852 * @throws SecurityException if the caller does not have the required permission
3853 */
3854 @VisibleForTesting
3855 public void enforceReadPermission() {
arunvoddud7401012022-12-15 16:08:12 +00003856 enforceReadPermission(null);
3857 }
3858
3859 /**
3860 * Make sure the caller has the READ_PHONE_STATE permissions.
3861 *
3862 * @throws SecurityException if the caller does not have the READ_PHONE_STATE permission.
3863 */
3864 @VisibleForTesting
3865 public void enforceReadPermission(String msg) {
3866 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.READ_PHONE_STATE, msg);
Gil Cukierman1c0eb932022-12-06 22:28:24 +00003867 }
3868
Shuo Qian3b6ee772019-11-13 17:43:31 -08003869 private void enforceActiveEmergencySessionPermission() {
3870 mApp.enforceCallingOrSelfPermission(
3871 android.Manifest.permission.READ_ACTIVE_EMERGENCY_SESSION, null);
3872 }
3873
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003874 /**
3875 * Make sure the caller has the CALL_PHONE permission.
3876 *
3877 * @throws SecurityException if the caller does not have the required permission
3878 */
3879 private void enforceCallPermission() {
3880 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.CALL_PHONE, null);
3881 }
3882
paulhu5a773602019-08-23 19:17:33 +08003883 private void enforceSettingsPermission() {
3884 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.NETWORK_SETTINGS, null);
Stuart Scott8eef64f2015-04-08 15:13:54 -07003885 }
3886
Michele Berionne5e411512020-11-13 02:36:59 +00003887 private void enforceRebootPermission() {
3888 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.REBOOT, null);
3889 }
3890
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003891 private String createTelUrl(String number) {
3892 if (TextUtils.isEmpty(number)) {
3893 return null;
3894 }
3895
Jake Hambye994d462014-02-03 13:10:13 -08003896 return "tel:" + number;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003897 }
3898
Ihab Awadf9e92732013-12-05 18:02:52 -08003899 private static void log(String msg) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003900 Log.d(LOG_TAG, "[PhoneIntfMgr] " + msg);
3901 }
3902
Naveen Kalla1fd79bd2014-08-08 00:48:59 -07003903 private static void logv(String msg) {
3904 Log.v(LOG_TAG, "[PhoneIntfMgr] " + msg);
3905 }
3906
Ihab Awadf9e92732013-12-05 18:02:52 -08003907 private static void loge(String msg) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003908 Log.e(LOG_TAG, "[PhoneIntfMgr] " + msg);
3909 }
3910
Robert Greenwalt36b23af2015-07-06 17:59:14 -07003911 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003912 public int getActivePhoneType() {
Shishir Agrawala9f32182016-04-12 12:00:16 -07003913 return getActivePhoneTypeForSlot(getSlotForDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07003914 }
3915
Sanket Padawe356d7632015-06-22 14:03:32 -07003916 @Override
Sanket Padawe13bac7b2017-03-20 15:04:47 -07003917 public int getActivePhoneTypeForSlot(int slotIndex) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003918 final long identity = Binder.clearCallingIdentity();
3919 try {
3920 final Phone phone = PhoneFactory.getPhone(slotIndex);
3921 if (phone == null) {
3922 return PhoneConstants.PHONE_TYPE_NONE;
3923 } else {
3924 return phone.getPhoneType();
3925 }
3926 } finally {
3927 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07003928 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003929 }
3930
3931 /**
3932 * Returns the CDMA ERI icon index to display
3933 */
Robert Greenwalt36b23af2015-07-06 17:59:14 -07003934 @Override
Philip P. Moltmann700a9592019-10-03 11:53:50 -07003935 public int getCdmaEriIconIndex(String callingPackage, String callingFeatureId) {
3936 return getCdmaEriIconIndexForSubscriber(getDefaultSubscription(), callingPackage,
3937 callingFeatureId);
Wink Saville36469e72014-06-11 15:17:00 -07003938 }
3939
Sanket Padawe356d7632015-06-22 14:03:32 -07003940 @Override
Philip P. Moltmann700a9592019-10-03 11:53:50 -07003941 public int getCdmaEriIconIndexForSubscriber(int subId, String callingPackage,
3942 String callingFeatureId) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08003943 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
Philip P. Moltmann700a9592019-10-03 11:53:50 -07003944 mApp, subId, callingPackage, callingFeatureId,
3945 "getCdmaEriIconIndexForSubscriber")) {
Robert Greenwalt36b23af2015-07-06 17:59:14 -07003946 return -1;
3947 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003948
3949 final long identity = Binder.clearCallingIdentity();
3950 try {
3951 final Phone phone = getPhone(subId);
3952 if (phone != null) {
3953 return phone.getCdmaEriIconIndex();
3954 } else {
3955 return -1;
3956 }
3957 } finally {
3958 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07003959 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003960 }
3961
3962 /**
3963 * Returns the CDMA ERI icon mode,
3964 * 0 - ON
3965 * 1 - FLASHING
3966 */
Robert Greenwalt36b23af2015-07-06 17:59:14 -07003967 @Override
Philip P. Moltmann700a9592019-10-03 11:53:50 -07003968 public int getCdmaEriIconMode(String callingPackage, String callingFeatureId) {
3969 return getCdmaEriIconModeForSubscriber(getDefaultSubscription(), callingPackage,
3970 callingFeatureId);
Wink Saville36469e72014-06-11 15:17:00 -07003971 }
3972
Sanket Padawe356d7632015-06-22 14:03:32 -07003973 @Override
Philip P. Moltmann700a9592019-10-03 11:53:50 -07003974 public int getCdmaEriIconModeForSubscriber(int subId, String callingPackage,
3975 String callingFeatureId) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08003976 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
Philip P. Moltmann700a9592019-10-03 11:53:50 -07003977 mApp, subId, callingPackage, callingFeatureId,
3978 "getCdmaEriIconModeForSubscriber")) {
Robert Greenwalt36b23af2015-07-06 17:59:14 -07003979 return -1;
3980 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003981
3982 final long identity = Binder.clearCallingIdentity();
3983 try {
3984 final Phone phone = getPhone(subId);
3985 if (phone != null) {
3986 return phone.getCdmaEriIconMode();
3987 } else {
3988 return -1;
3989 }
3990 } finally {
3991 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07003992 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003993 }
3994
3995 /**
3996 * Returns the CDMA ERI text,
3997 */
Robert Greenwalt36b23af2015-07-06 17:59:14 -07003998 @Override
Philip P. Moltmann700a9592019-10-03 11:53:50 -07003999 public String getCdmaEriText(String callingPackage, String callingFeatureId) {
4000 return getCdmaEriTextForSubscriber(getDefaultSubscription(), callingPackage,
4001 callingFeatureId);
Wink Saville36469e72014-06-11 15:17:00 -07004002 }
4003
Sanket Padawe356d7632015-06-22 14:03:32 -07004004 @Override
Philip P. Moltmann700a9592019-10-03 11:53:50 -07004005 public String getCdmaEriTextForSubscriber(int subId, String callingPackage,
4006 String callingFeatureId) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08004007 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
Philip P. Moltmann700a9592019-10-03 11:53:50 -07004008 mApp, subId, callingPackage, callingFeatureId,
4009 "getCdmaEriIconTextForSubscriber")) {
Robert Greenwalt36b23af2015-07-06 17:59:14 -07004010 return null;
4011 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004012
4013 final long identity = Binder.clearCallingIdentity();
4014 try {
4015 final Phone phone = getPhone(subId);
4016 if (phone != null) {
4017 return phone.getCdmaEriText();
4018 } else {
4019 return null;
4020 }
4021 } finally {
4022 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07004023 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07004024 }
4025
4026 /**
Junda Liuca05d5d2014-08-14 22:36:34 -07004027 * Returns the CDMA MDN.
4028 */
Sanket Padawe356d7632015-06-22 14:03:32 -07004029 @Override
Wink Savilleb564aae2014-10-23 10:18:09 -07004030 public String getCdmaMdn(int subId) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08004031 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
4032 mApp, subId, "getCdmaMdn");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004033
4034 final long identity = Binder.clearCallingIdentity();
4035 try {
4036 final Phone phone = getPhone(subId);
Nazanin Bakhshif782e562018-12-11 15:15:39 -08004037 if (phone != null && phone.getPhoneType() == PhoneConstants.PHONE_TYPE_CDMA) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004038 return phone.getLine1Number();
4039 } else {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08004040 loge("getCdmaMdn: no phone found. Invalid subId: " + subId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004041 return null;
4042 }
4043 } finally {
4044 Binder.restoreCallingIdentity(identity);
Junda Liuca05d5d2014-08-14 22:36:34 -07004045 }
4046 }
4047
4048 /**
4049 * Returns the CDMA MIN.
4050 */
Sanket Padawe356d7632015-06-22 14:03:32 -07004051 @Override
Wink Savilleb564aae2014-10-23 10:18:09 -07004052 public String getCdmaMin(int subId) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08004053 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
4054 mApp, subId, "getCdmaMin");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004055
4056 final long identity = Binder.clearCallingIdentity();
4057 try {
4058 final Phone phone = getPhone(subId);
4059 if (phone != null && phone.getPhoneType() == PhoneConstants.PHONE_TYPE_CDMA) {
4060 return phone.getCdmaMin();
4061 } else {
4062 return null;
4063 }
4064 } finally {
4065 Binder.restoreCallingIdentity(identity);
Junda Liuca05d5d2014-08-14 22:36:34 -07004066 }
4067 }
4068
Hall Liud892bec2018-11-30 14:51:45 -08004069 @Override
4070 public void requestNumberVerification(PhoneNumberRange range, long timeoutMillis,
4071 INumberVerificationCallback callback, String callingPackage) {
4072 if (mApp.checkCallingOrSelfPermission(android.Manifest.permission.MODIFY_PHONE_STATE)
4073 != PERMISSION_GRANTED) {
4074 throw new SecurityException("Caller must hold the MODIFY_PHONE_STATE permission");
4075 }
4076 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
4077
4078 String authorizedPackage = NumberVerificationManager.getAuthorizedPackage(mApp);
4079 if (!TextUtils.equals(callingPackage, authorizedPackage)) {
Hall Liub9d8feb2021-01-13 10:28:04 -08004080 throw new SecurityException("Calling package must be configured in the device config: "
4081 + "calling package: " + callingPackage
4082 + ", configured package: " + authorizedPackage);
Hall Liud892bec2018-11-30 14:51:45 -08004083 }
4084
4085 if (range == null) {
4086 throw new NullPointerException("Range must be non-null");
4087 }
4088
4089 timeoutMillis = Math.min(timeoutMillis,
Hall Liubd069e32019-02-28 18:56:30 -08004090 TelephonyManager.getMaxNumberVerificationTimeoutMillis());
Hall Liud892bec2018-11-30 14:51:45 -08004091
4092 NumberVerificationManager.getInstance().requestVerification(range, callback, timeoutMillis);
4093 }
4094
Junda Liuca05d5d2014-08-14 22:36:34 -07004095 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -07004096 * Returns true if CDMA provisioning needs to run.
4097 */
4098 public boolean needsOtaServiceProvisioning() {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004099 final long identity = Binder.clearCallingIdentity();
4100 try {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08004101 return getDefaultPhone().needsOtaServiceProvisioning();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004102 } finally {
4103 Binder.restoreCallingIdentity(identity);
4104 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07004105 }
4106
4107 /**
Shishir Agrawal76d5da92014-11-09 16:17:25 -08004108 * Sets the voice mail number of a given subId.
4109 */
4110 @Override
4111 public boolean setVoiceMailNumber(int subId, String alphaTag, String number) {
Shuo Qian2c0ae432019-12-05 11:40:37 -08004112 TelephonyPermissions.enforceCallingOrSelfCarrierPrivilege(
4113 mApp, subId, "setVoiceMailNumber");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004114
4115 final long identity = Binder.clearCallingIdentity();
4116 try {
4117 Boolean success = (Boolean) sendRequest(CMD_SET_VOICEMAIL_NUMBER,
4118 new Pair<String, String>(alphaTag, number), new Integer(subId));
4119 return success;
4120 } finally {
4121 Binder.restoreCallingIdentity(identity);
4122 }
Shishir Agrawal76d5da92014-11-09 16:17:25 -08004123 }
4124
Ta-wei Yen87c49842016-05-13 21:19:52 -07004125 @Override
Ta-wei Yenc9df0432017-04-17 17:09:07 -07004126 public Bundle getVisualVoicemailSettings(String callingPackage, int subId) {
4127 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
Tyler Gunn5ddfdc92019-10-31 13:08:23 -07004128 TelecomManager tm = mApp.getSystemService(TelecomManager.class);
4129 String systemDialer = tm.getSystemDialerPackage();
Ta-wei Yenc9df0432017-04-17 17:09:07 -07004130 if (!TextUtils.equals(callingPackage, systemDialer)) {
4131 throw new SecurityException("caller must be system dialer");
4132 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004133
4134 final long identity = Binder.clearCallingIdentity();
4135 try {
4136 PhoneAccountHandle phoneAccountHandle = PhoneAccountHandleConverter.fromSubId(subId);
4137 if (phoneAccountHandle == null) {
4138 return null;
4139 }
Nazanin Bakhshif782e562018-12-11 15:15:39 -08004140 return VisualVoicemailSettingsUtil.dump(mApp, phoneAccountHandle);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004141 } finally {
4142 Binder.restoreCallingIdentity(identity);
Ta-wei Yenc9df0432017-04-17 17:09:07 -07004143 }
Ta-wei Yenc9df0432017-04-17 17:09:07 -07004144 }
4145
4146 @Override
Philip P. Moltmann700a9592019-10-03 11:53:50 -07004147 public String getVisualVoicemailPackageName(String callingPackage, String callingFeatureId,
4148 int subId) {
Ta-wei Yendca928f2017-01-10 16:17:08 -08004149 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
Jeff Davidson7e17e312018-02-13 18:17:36 -08004150 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
Philip P. Moltmann700a9592019-10-03 11:53:50 -07004151 mApp, subId, callingPackage, callingFeatureId,
4152 "getVisualVoicemailPackageName")) {
Ta-wei Yendca928f2017-01-10 16:17:08 -08004153 return null;
4154 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004155
Jeff Davidsona8e4e242018-03-15 17:16:18 -07004156 final long identity = Binder.clearCallingIdentity();
4157 try {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08004158 return RemoteVvmTaskManager.getRemotePackage(mApp, subId).getPackageName();
Jeff Davidsona8e4e242018-03-15 17:16:18 -07004159 } finally {
4160 Binder.restoreCallingIdentity(identity);
4161 }
Ta-wei Yendca928f2017-01-10 16:17:08 -08004162 }
4163
4164 @Override
Ta-wei Yenb6929602016-05-24 15:48:27 -07004165 public void enableVisualVoicemailSmsFilter(String callingPackage, int subId,
4166 VisualVoicemailSmsFilterSettings settings) {
4167 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004168
4169 final long identity = Binder.clearCallingIdentity();
4170 try {
4171 VisualVoicemailSmsFilterConfig.enableVisualVoicemailSmsFilter(
Nazanin Bakhshif782e562018-12-11 15:15:39 -08004172 mApp, callingPackage, subId, settings);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004173 } finally {
4174 Binder.restoreCallingIdentity(identity);
4175 }
Ta-wei Yen87c49842016-05-13 21:19:52 -07004176 }
4177
4178 @Override
Ta-wei Yenb6929602016-05-24 15:48:27 -07004179 public void disableVisualVoicemailSmsFilter(String callingPackage, int subId) {
4180 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004181
4182 final long identity = Binder.clearCallingIdentity();
4183 try {
4184 VisualVoicemailSmsFilterConfig.disableVisualVoicemailSmsFilter(
Nazanin Bakhshif782e562018-12-11 15:15:39 -08004185 mApp, callingPackage, subId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004186 } finally {
4187 Binder.restoreCallingIdentity(identity);
4188 }
Ta-wei Yen87c49842016-05-13 21:19:52 -07004189 }
4190
4191 @Override
Ta-wei Yenb6929602016-05-24 15:48:27 -07004192 public VisualVoicemailSmsFilterSettings getVisualVoicemailSmsFilterSettings(
4193 String callingPackage, int subId) {
4194 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004195
4196 final long identity = Binder.clearCallingIdentity();
4197 try {
4198 return VisualVoicemailSmsFilterConfig.getVisualVoicemailSmsFilterSettings(
Nazanin Bakhshif782e562018-12-11 15:15:39 -08004199 mApp, callingPackage, subId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004200 } finally {
4201 Binder.restoreCallingIdentity(identity);
4202 }
Ta-wei Yen87c49842016-05-13 21:19:52 -07004203 }
4204
4205 @Override
Ta-wei Yen30a69c82016-12-27 14:52:32 -08004206 public VisualVoicemailSmsFilterSettings getActiveVisualVoicemailSmsFilterSettings(int subId) {
Brad Ebinger35c841c2018-10-01 10:40:55 -07004207 enforceReadPrivilegedPermission("getActiveVisualVoicemailSmsFilterSettings");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004208
4209 final long identity = Binder.clearCallingIdentity();
4210 try {
4211 return VisualVoicemailSmsFilterConfig.getActiveVisualVoicemailSmsFilterSettings(
Nazanin Bakhshif782e562018-12-11 15:15:39 -08004212 mApp, subId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004213 } finally {
4214 Binder.restoreCallingIdentity(identity);
4215 }
Ta-wei Yen30a69c82016-12-27 14:52:32 -08004216 }
4217
4218 @Override
Philip P. Moltmann2f6f8ce2020-03-18 18:17:02 -07004219 public void sendVisualVoicemailSmsForSubscriber(String callingPackage,
4220 String callingAttributionTag, int subId, String number, int port, String text,
4221 PendingIntent sentIntent) {
Ta-wei Yen30a69c82016-12-27 14:52:32 -08004222 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
Ta-wei Yen527a9c02017-01-06 15:29:25 -08004223 enforceVisualVoicemailPackage(callingPackage, subId);
Ta-wei Yen30a69c82016-12-27 14:52:32 -08004224 enforceSendSmsPermission();
Amit Mahajandccb3f12019-05-13 13:48:32 -07004225 SmsController smsController = PhoneFactory.getSmsController();
Philip P. Moltmann2f6f8ce2020-03-18 18:17:02 -07004226 smsController.sendVisualVoicemailSmsForSubscriber(callingPackage, callingAttributionTag,
4227 subId, number, port, text, sentIntent);
Ta-wei Yen87c49842016-05-13 21:19:52 -07004228 }
Amit Mahajandccb3f12019-05-13 13:48:32 -07004229
Shishir Agrawal76d5da92014-11-09 16:17:25 -08004230 /**
fionaxu0152e512016-11-14 13:36:14 -08004231 * Sets the voice activation state of a given subId.
4232 */
4233 @Override
4234 public void setVoiceActivationState(int subId, int activationState) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08004235 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
4236 mApp, subId, "setVoiceActivationState");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004237
4238 final long identity = Binder.clearCallingIdentity();
4239 try {
4240 final Phone phone = getPhone(subId);
4241 if (phone != null) {
4242 phone.setVoiceActivationState(activationState);
4243 } else {
4244 loge("setVoiceActivationState fails with invalid subId: " + subId);
4245 }
4246 } finally {
4247 Binder.restoreCallingIdentity(identity);
fionaxu0152e512016-11-14 13:36:14 -08004248 }
4249 }
4250
4251 /**
4252 * Sets the data activation state of a given subId.
4253 */
4254 @Override
4255 public void setDataActivationState(int subId, int activationState) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08004256 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
4257 mApp, subId, "setDataActivationState");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004258
4259 final long identity = Binder.clearCallingIdentity();
4260 try {
4261 final Phone phone = getPhone(subId);
4262 if (phone != null) {
4263 phone.setDataActivationState(activationState);
4264 } else {
Taesu Leef8fbed92019-10-07 18:47:02 +09004265 loge("setDataActivationState fails with invalid subId: " + subId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004266 }
4267 } finally {
4268 Binder.restoreCallingIdentity(identity);
fionaxu0152e512016-11-14 13:36:14 -08004269 }
4270 }
4271
4272 /**
4273 * Returns the voice activation state of a given subId.
4274 */
4275 @Override
4276 public int getVoiceActivationState(int subId, String callingPackage) {
Brad Ebinger35c841c2018-10-01 10:40:55 -07004277 enforceReadPrivilegedPermission("getVoiceActivationState");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004278
fionaxu0152e512016-11-14 13:36:14 -08004279 final Phone phone = getPhone(subId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004280 final long identity = Binder.clearCallingIdentity();
4281 try {
4282 if (phone != null) {
4283 return phone.getVoiceActivationState();
4284 } else {
4285 return TelephonyManager.SIM_ACTIVATION_STATE_UNKNOWN;
4286 }
4287 } finally {
4288 Binder.restoreCallingIdentity(identity);
fionaxu0152e512016-11-14 13:36:14 -08004289 }
4290 }
4291
4292 /**
4293 * Returns the data activation state of a given subId.
4294 */
4295 @Override
4296 public int getDataActivationState(int subId, String callingPackage) {
Brad Ebinger35c841c2018-10-01 10:40:55 -07004297 enforceReadPrivilegedPermission("getDataActivationState");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004298
fionaxu0152e512016-11-14 13:36:14 -08004299 final Phone phone = getPhone(subId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004300 final long identity = Binder.clearCallingIdentity();
4301 try {
4302 if (phone != null) {
4303 return phone.getDataActivationState();
4304 } else {
4305 return TelephonyManager.SIM_ACTIVATION_STATE_UNKNOWN;
4306 }
4307 } finally {
4308 Binder.restoreCallingIdentity(identity);
fionaxu0152e512016-11-14 13:36:14 -08004309 }
4310 }
4311
4312 /**
Wink Saville36469e72014-06-11 15:17:00 -07004313 * Returns the unread count of voicemails for a subId
4314 */
Sanket Padawe356d7632015-06-22 14:03:32 -07004315 @Override
Philip P. Moltmann700a9592019-10-03 11:53:50 -07004316 public int getVoiceMessageCountForSubscriber(int subId, String callingPackage,
4317 String callingFeatureId) {
Brad Ebingerf7664ba2018-11-29 12:43:38 -08004318 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
Philip P. Moltmann700a9592019-10-03 11:53:50 -07004319 mApp, subId, callingPackage, callingFeatureId,
4320 "getVoiceMessageCountForSubscriber")) {
Brad Ebingerf7664ba2018-11-29 12:43:38 -08004321 return 0;
4322 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004323 final long identity = Binder.clearCallingIdentity();
4324 try {
4325 final Phone phone = getPhone(subId);
4326 if (phone != null) {
4327 return phone.getVoiceMessageCount();
4328 } else {
4329 return 0;
4330 }
4331 } finally {
4332 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07004333 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07004334 }
4335
4336 /**
pkanwar8a4dcfb2017-01-19 13:43:16 -08004337 * returns true, if the device is in a state where both voice and data
4338 * are supported simultaneously. This can change based on location or network condition.
4339 */
4340 @Override
4341 public boolean isConcurrentVoiceAndDataAllowed(int subId) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004342 final long identity = Binder.clearCallingIdentity();
4343 try {
4344 final Phone phone = getPhone(subId);
4345 return (phone == null ? false : phone.isConcurrentVoiceAndDataAllowed());
4346 } finally {
4347 Binder.restoreCallingIdentity(identity);
4348 }
pkanwar8a4dcfb2017-01-19 13:43:16 -08004349 }
4350
4351 /**
fionaxu235cc5e2017-03-06 22:25:57 -08004352 * Send the dialer code if called from the current default dialer or the caller has
4353 * carrier privilege.
4354 * @param inputCode The dialer code to send
4355 */
4356 @Override
4357 public void sendDialerSpecialCode(String callingPackage, String inputCode) {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08004358 final Phone defaultPhone = getDefaultPhone();
fionaxu235cc5e2017-03-06 22:25:57 -08004359 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
Tyler Gunn5ddfdc92019-10-31 13:08:23 -07004360 TelecomManager tm = defaultPhone.getContext().getSystemService(TelecomManager.class);
4361 String defaultDialer = tm.getDefaultDialerPackage();
fionaxu235cc5e2017-03-06 22:25:57 -08004362 if (!TextUtils.equals(callingPackage, defaultDialer)) {
Shuo Qian2c0ae432019-12-05 11:40:37 -08004363 TelephonyPermissions.enforceCallingOrSelfCarrierPrivilege(mApp,
Jeff Davidson7e17e312018-02-13 18:17:36 -08004364 getDefaultSubscription(), "sendDialerSpecialCode");
fionaxu235cc5e2017-03-06 22:25:57 -08004365 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004366
4367 final long identity = Binder.clearCallingIdentity();
4368 try {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08004369 defaultPhone.sendDialerSpecialCode(inputCode);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004370 } finally {
4371 Binder.restoreCallingIdentity(identity);
4372 }
fionaxu235cc5e2017-03-06 22:25:57 -08004373 }
4374
Pengquan Menga1bb6272018-09-06 09:59:22 -07004375 @Override
4376 public int getNetworkSelectionMode(int subId) {
shilufc958392020-01-20 11:36:01 -08004377 TelephonyPermissions
Nathan Harold62c68512021-04-06 11:26:02 -07004378 .enforceCallingOrSelfReadPrecisePhoneStatePermissionOrCarrierPrivilege(
shilufc958392020-01-20 11:36:01 -08004379 mApp, subId, "getNetworkSelectionMode");
4380 final long identity = Binder.clearCallingIdentity();
4381 try {
4382 if (!isActiveSubscription(subId)) {
4383 return TelephonyManager.NETWORK_SELECTION_MODE_UNKNOWN;
4384 }
4385 return (int) sendRequest(CMD_GET_NETWORK_SELECTION_MODE, null /* argument */, subId);
4386 } finally {
4387 Binder.restoreCallingIdentity(identity);
Pengquan Menge92a50d2018-09-21 15:54:48 -07004388 }
Pengquan Menga1bb6272018-09-06 09:59:22 -07004389 }
4390
Brad Ebinger35c841c2018-10-01 10:40:55 -07004391 @Override
Brad Ebingerb2b65522019-03-15 13:48:47 -07004392 public boolean isInEmergencySmsMode() {
4393 enforceReadPrivilegedPermission("isInEmergencySmsMode");
4394 final long identity = Binder.clearCallingIdentity();
4395 try {
4396 for (Phone phone : PhoneFactory.getPhones()) {
4397 if (phone.isInEmergencySmsMode()) {
4398 return true;
4399 }
4400 }
4401 } finally {
4402 Binder.restoreCallingIdentity(identity);
4403 }
4404 return false;
4405 }
4406
shilu366312e2019-12-17 09:28:10 -08004407 /**
4408 * Requires carrier privileges or READ_PRECISE_PHONE_STATE permission.
4409 * @param subId The subscription to use to check the configuration.
4410 * @param c The callback that will be used to send the result.
4411 */
Brad Ebingerb2b65522019-03-15 13:48:47 -07004412 @Override
Brad Ebinger9878b0b2018-11-08 17:43:22 -08004413 public void registerImsRegistrationCallback(int subId, IImsRegistrationCallback c)
4414 throws RemoteException {
Nathan Harold62c68512021-04-06 11:26:02 -07004415 TelephonyPermissions.enforceCallingOrSelfReadPrecisePhoneStatePermissionOrCarrierPrivilege(
Rambo Wang37f9c242020-02-10 14:45:28 -08004416 mApp, subId, "registerImsRegistrationCallback");
Brad Ebingera2628302022-02-18 03:44:55 +00004417
4418 if (!ImsManager.isImsSupportedOnDevice(mApp)) {
4419 throw new ServiceSpecificException(ImsException.CODE_ERROR_UNSUPPORTED_OPERATION,
4420 "IMS not available on device.");
4421 }
Brad Ebinger35c841c2018-10-01 10:40:55 -07004422 final long token = Binder.clearCallingIdentity();
4423 try {
Brad Ebingera2628302022-02-18 03:44:55 +00004424 int slotId = getSlotIndexOrException(subId);
4425 verifyImsMmTelConfiguredOrThrow(slotId);
joonhunshin49f0aed2022-08-05 08:33:05 +00004426
4427 ImsStateCallbackController controller = ImsStateCallbackController.getInstance();
4428 if (controller != null) {
4429 ImsManager imsManager = controller.getImsManager(subId);
4430 if (imsManager != null) {
4431 imsManager.addRegistrationCallbackForSubscription(c, subId);
4432 } else {
4433 throw new ServiceSpecificException(ImsException.CODE_ERROR_SERVICE_UNAVAILABLE);
4434 }
4435 } else {
4436 throw new ServiceSpecificException(ImsException.CODE_ERROR_UNSUPPORTED_OPERATION);
4437 }
Brad Ebinger1ce9c432019-07-16 13:19:44 -07004438 } catch (ImsException e) {
4439 throw new ServiceSpecificException(e.getCode());
Brad Ebinger35c841c2018-10-01 10:40:55 -07004440 } finally {
4441 Binder.restoreCallingIdentity(token);
4442 }
4443 }
4444
shilu366312e2019-12-17 09:28:10 -08004445 /**
4446 * Requires carrier privileges or READ_PRECISE_PHONE_STATE permission.
4447 * @param subId The subscription to use to check the configuration.
4448 * @param c The callback that will be used to send the result.
4449 */
Brad Ebinger35c841c2018-10-01 10:40:55 -07004450 @Override
Brad Ebinger9878b0b2018-11-08 17:43:22 -08004451 public void unregisterImsRegistrationCallback(int subId, IImsRegistrationCallback c) {
Nathan Harold62c68512021-04-06 11:26:02 -07004452 TelephonyPermissions.enforceCallingOrSelfReadPrecisePhoneStatePermissionOrCarrierPrivilege(
Rambo Wang37f9c242020-02-10 14:45:28 -08004453 mApp, subId, "unregisterImsRegistrationCallback");
Brad Ebinger4ae57f92019-01-09 16:51:30 -08004454 if (!SubscriptionManager.isValidSubscriptionId(subId)) {
4455 throw new IllegalArgumentException("Invalid Subscription ID: " + subId);
4456 }
Meng Wangafbc5852019-09-19 17:37:13 -07004457 final long token = Binder.clearCallingIdentity();
joonhunshin49f0aed2022-08-05 08:33:05 +00004458
Meng Wangafbc5852019-09-19 17:37:13 -07004459 try {
joonhunshin49f0aed2022-08-05 08:33:05 +00004460 ImsStateCallbackController controller = ImsStateCallbackController.getInstance();
4461 if (controller != null) {
4462 ImsManager imsManager = controller.getImsManager(subId);
4463 if (imsManager != null) {
4464 imsManager.removeRegistrationCallbackForSubscription(c, subId);
4465 } else {
4466 Log.i(LOG_TAG, "unregisterImsRegistrationCallback: " + subId
4467 + "is inactive, ignoring unregister.");
4468 // If the ImsManager is not valid, just return, since the callback
4469 // will already have been removed internally.
4470 }
4471 }
Meng Wangafbc5852019-09-19 17:37:13 -07004472 } finally {
4473 Binder.restoreCallingIdentity(token);
4474 }
Brad Ebinger35c841c2018-10-01 10:40:55 -07004475 }
4476
Brad Ebingera34a6c22019-10-22 17:36:18 -07004477 /**
4478 * Get the IMS service registration state for the MmTelFeature associated with this sub id.
4479 */
4480 @Override
4481 public void getImsMmTelRegistrationState(int subId, IIntegerConsumer consumer) {
4482 enforceReadPrivilegedPermission("getImsMmTelRegistrationState");
4483 if (!ImsManager.isImsSupportedOnDevice(mApp)) {
4484 throw new ServiceSpecificException(ImsException.CODE_ERROR_UNSUPPORTED_OPERATION,
4485 "IMS not available on device.");
4486 }
4487 final long token = Binder.clearCallingIdentity();
4488 try {
4489 Phone phone = getPhone(subId);
4490 if (phone == null) {
4491 Log.w(LOG_TAG, "getImsMmTelRegistrationState: called with an invalid subscription '"
4492 + subId + "'");
4493 throw new ServiceSpecificException(ImsException.CODE_ERROR_INVALID_SUBSCRIPTION);
4494 }
4495 phone.getImsRegistrationState(regState -> {
4496 try {
4497 consumer.accept((regState == null)
4498 ? RegistrationManager.REGISTRATION_STATE_NOT_REGISTERED : regState);
4499 } catch (RemoteException e) {
4500 // Ignore if the remote process is no longer available to call back.
4501 Log.w(LOG_TAG, "getImsMmTelRegistrationState: callback not available.");
4502 }
4503 });
4504 } finally {
4505 Binder.restoreCallingIdentity(token);
4506 }
4507 }
4508
4509 /**
4510 * Get the transport type for the IMS service registration state.
4511 */
4512 @Override
4513 public void getImsMmTelRegistrationTransportType(int subId, IIntegerConsumer consumer) {
Nathan Harold62c68512021-04-06 11:26:02 -07004514 TelephonyPermissions.enforceCallingOrSelfReadPrecisePhoneStatePermissionOrCarrierPrivilege(
Rambo Wang37f9c242020-02-10 14:45:28 -08004515 mApp, subId, "getImsMmTelRegistrationTransportType");
Brad Ebingera34a6c22019-10-22 17:36:18 -07004516 if (!ImsManager.isImsSupportedOnDevice(mApp)) {
4517 throw new ServiceSpecificException(ImsException.CODE_ERROR_UNSUPPORTED_OPERATION,
4518 "IMS not available on device.");
4519 }
4520 final long token = Binder.clearCallingIdentity();
4521 try {
4522 Phone phone = getPhone(subId);
4523 if (phone == null) {
4524 Log.w(LOG_TAG, "getImsMmTelRegistrationState: called with an invalid subscription '"
4525 + subId + "'");
4526 throw new ServiceSpecificException(ImsException.CODE_ERROR_INVALID_SUBSCRIPTION);
4527 }
4528 phone.getImsRegistrationTech(regTech -> {
4529 // Convert registration tech from ImsRegistrationImplBase -> RegistrationManager
4530 int regTechConverted = (regTech == null)
4531 ? ImsRegistrationImplBase.REGISTRATION_TECH_NONE : regTech;
4532 regTechConverted = RegistrationManager.IMS_REG_TO_ACCESS_TYPE_MAP.get(
4533 regTechConverted);
4534 try {
4535 consumer.accept(regTechConverted);
4536 } catch (RemoteException e) {
4537 // Ignore if the remote process is no longer available to call back.
4538 Log.w(LOG_TAG, "getImsMmTelRegistrationState: callback not available.");
4539 }
4540 });
4541 } finally {
4542 Binder.restoreCallingIdentity(token);
4543 }
4544 }
4545
shilu366312e2019-12-17 09:28:10 -08004546 /**
4547 * Requires carrier privileges or READ_PRECISE_PHONE_STATE permission.
4548 * @param subId The subscription to use to check the configuration.
4549 * @param c The callback that will be used to send the result.
4550 */
Brad Ebinger35c841c2018-10-01 10:40:55 -07004551 @Override
Brad Ebinger9878b0b2018-11-08 17:43:22 -08004552 public void registerMmTelCapabilityCallback(int subId, IImsCapabilityCallback c)
4553 throws RemoteException {
Nathan Harold62c68512021-04-06 11:26:02 -07004554 TelephonyPermissions.enforceCallingOrSelfReadPrecisePhoneStatePermissionOrCarrierPrivilege(
Rambo Wang37f9c242020-02-10 14:45:28 -08004555 mApp, subId, "registerMmTelCapabilityCallback");
Brad Ebingera2628302022-02-18 03:44:55 +00004556 if (!ImsManager.isImsSupportedOnDevice(mApp)) {
4557 throw new ServiceSpecificException(ImsException.CODE_ERROR_UNSUPPORTED_OPERATION,
4558 "IMS not available on device.");
4559 }
Brad Ebinger35c841c2018-10-01 10:40:55 -07004560 final long token = Binder.clearCallingIdentity();
4561 try {
Brad Ebingera2628302022-02-18 03:44:55 +00004562 int slotId = getSlotIndexOrException(subId);
4563 verifyImsMmTelConfiguredOrThrow(slotId);
Hwangoo Park8646b0d2023-01-13 02:42:34 +00004564
4565 ImsStateCallbackController controller = ImsStateCallbackController.getInstance();
4566 if (controller != null) {
4567 ImsManager imsManager = controller.getImsManager(subId);
4568 if (imsManager != null) {
4569 imsManager.addCapabilitiesCallbackForSubscription(c, subId);
4570 } else {
4571 throw new ServiceSpecificException(ImsException.CODE_ERROR_SERVICE_UNAVAILABLE);
4572 }
4573 } else {
4574 throw new ServiceSpecificException(ImsException.CODE_ERROR_UNSUPPORTED_OPERATION);
4575 }
Brad Ebinger1ce9c432019-07-16 13:19:44 -07004576 } catch (ImsException e) {
4577 throw new ServiceSpecificException(e.getCode());
Brad Ebinger35c841c2018-10-01 10:40:55 -07004578 } finally {
4579 Binder.restoreCallingIdentity(token);
4580 }
4581 }
4582
shilu366312e2019-12-17 09:28:10 -08004583 /**
4584 * Requires carrier privileges or READ_PRECISE_PHONE_STATE permission.
4585 * @param subId The subscription to use to check the configuration.
4586 * @param c The callback that will be used to send the result.
4587 */
Brad Ebinger35c841c2018-10-01 10:40:55 -07004588 @Override
Brad Ebinger9878b0b2018-11-08 17:43:22 -08004589 public void unregisterMmTelCapabilityCallback(int subId, IImsCapabilityCallback c) {
Nathan Harold62c68512021-04-06 11:26:02 -07004590 TelephonyPermissions.enforceCallingOrSelfReadPrecisePhoneStatePermissionOrCarrierPrivilege(
Rambo Wang37f9c242020-02-10 14:45:28 -08004591 mApp, subId, "unregisterMmTelCapabilityCallback");
Brad Ebinger4ae57f92019-01-09 16:51:30 -08004592 if (!SubscriptionManager.isValidSubscriptionId(subId)) {
4593 throw new IllegalArgumentException("Invalid Subscription ID: " + subId);
4594 }
Meng Wangafbc5852019-09-19 17:37:13 -07004595
4596 final long token = Binder.clearCallingIdentity();
4597 try {
Hwangoo Park8646b0d2023-01-13 02:42:34 +00004598 ImsStateCallbackController controller = ImsStateCallbackController.getInstance();
4599 if (controller != null) {
4600 ImsManager imsManager = controller.getImsManager(subId);
4601 if (imsManager != null) {
4602 imsManager.removeCapabilitiesCallbackForSubscription(c, subId);
4603 } else {
4604 Log.i(LOG_TAG, "unregisterMmTelCapabilityCallback: " + subId
4605 + " is inactive, ignoring unregister.");
4606 // If the ImsManager is not valid, just return, since the callback
4607 // will already have been removed internally.
4608 }
4609 }
Meng Wangafbc5852019-09-19 17:37:13 -07004610 } finally {
4611 Binder.restoreCallingIdentity(token);
4612 }
Brad Ebinger35c841c2018-10-01 10:40:55 -07004613 }
4614
4615 @Override
Brad Ebinger9878b0b2018-11-08 17:43:22 -08004616 public boolean isCapable(int subId, int capability, int regTech) {
4617 enforceReadPrivilegedPermission("isCapable");
Brad Ebinger35c841c2018-10-01 10:40:55 -07004618 final long token = Binder.clearCallingIdentity();
4619 try {
Brad Ebingera2628302022-02-18 03:44:55 +00004620 int slotId = getSlotIndexOrException(subId);
4621 verifyImsMmTelConfiguredOrThrow(slotId);
4622 return ImsManager.getInstance(mApp, slotId).queryMmTelCapability(capability, regTech);
4623 } catch (com.android.ims.ImsException e) {
4624 Log.w(LOG_TAG, "IMS isCapable - service unavailable: " + e.getMessage());
4625 return false;
Brad Ebinger1ce9c432019-07-16 13:19:44 -07004626 } catch (ImsException e) {
Brad Ebinger6b5ac222019-02-04 14:36:52 -08004627 Log.i(LOG_TAG, "isCapable: " + subId + " is inactive, returning false.");
4628 return false;
Brad Ebinger35c841c2018-10-01 10:40:55 -07004629 } finally {
4630 Binder.restoreCallingIdentity(token);
4631 }
4632 }
4633
4634 @Override
Brad Ebinger9878b0b2018-11-08 17:43:22 -08004635 public boolean isAvailable(int subId, int capability, int regTech) {
4636 enforceReadPrivilegedPermission("isAvailable");
Brad Ebinger35c841c2018-10-01 10:40:55 -07004637 final long token = Binder.clearCallingIdentity();
4638 try {
4639 Phone phone = getPhone(subId);
4640 if (phone == null) return false;
4641 return phone.isImsCapabilityAvailable(capability, regTech);
Daniel Bright5e40e4e2020-03-11 16:35:39 -07004642 } catch (com.android.ims.ImsException e) {
4643 Log.w(LOG_TAG, "IMS isAvailable - service unavailable: " + e.getMessage());
4644 return false;
Brad Ebinger35c841c2018-10-01 10:40:55 -07004645 } finally {
4646 Binder.restoreCallingIdentity(token);
4647 }
4648 }
4649
Brad Ebingerbc7dd582019-10-17 17:03:22 -07004650 /**
4651 * Determines if the MmTel feature capability is supported by the carrier configuration for this
4652 * subscription.
4653 * @param subId The subscription to use to check the configuration.
4654 * @param callback The callback that will be used to send the result.
4655 * @param capability The MmTelFeature capability that will be used to send the result.
4656 * @param transportType The transport type of the MmTelFeature capability.
4657 */
4658 @Override
4659 public void isMmTelCapabilitySupported(int subId, IIntegerConsumer callback, int capability,
4660 int transportType) {
4661 enforceReadPrivilegedPermission("isMmTelCapabilitySupported");
Brad Ebingera2628302022-02-18 03:44:55 +00004662 if (!ImsManager.isImsSupportedOnDevice(mApp)) {
4663 throw new ServiceSpecificException(ImsException.CODE_ERROR_UNSUPPORTED_OPERATION,
4664 "IMS not available on device.");
4665 }
Brad Ebingerbc7dd582019-10-17 17:03:22 -07004666 final long token = Binder.clearCallingIdentity();
4667 try {
Brad Ebingera2628302022-02-18 03:44:55 +00004668 int slotId = getSlotIndex(subId);
4669 if (slotId <= SubscriptionManager.INVALID_SIM_SLOT_INDEX) {
4670 Log.w(LOG_TAG, "isMmTelCapabilitySupported: called with an inactive subscription '"
4671 + subId + "'");
4672 throw new ServiceSpecificException(ImsException.CODE_ERROR_INVALID_SUBSCRIPTION);
4673 }
4674 verifyImsMmTelConfiguredOrThrow(slotId);
4675 ImsManager.getInstance(mApp, slotId).isSupported(capability,
4676 transportType, aBoolean -> {
4677 try {
4678 callback.accept((aBoolean == null) ? 0 : (aBoolean ? 1 : 0));
4679 } catch (RemoteException e) {
4680 Log.w(LOG_TAG, "isMmTelCapabilitySupported: remote caller is not "
4681 + "running. Ignore");
4682 }
4683 });
Brad Ebinger919631e2021-06-02 17:46:35 -07004684 } catch (ImsException e) {
4685 throw new ServiceSpecificException(e.getCode());
Brad Ebingerbc7dd582019-10-17 17:03:22 -07004686 } finally {
4687 Binder.restoreCallingIdentity(token);
4688 }
4689 }
4690
shilu366312e2019-12-17 09:28:10 -08004691 /**
4692 * Requires carrier privileges or READ_PRECISE_PHONE_STATE permission.
4693 * @param subId The subscription to use to check the configuration.
4694 */
Brad Ebinger35c841c2018-10-01 10:40:55 -07004695 @Override
4696 public boolean isAdvancedCallingSettingEnabled(int subId) {
Nathan Harold62c68512021-04-06 11:26:02 -07004697 TelephonyPermissions.enforceCallingOrSelfReadPrecisePhoneStatePermissionOrCarrierPrivilege(
Rambo Wang37f9c242020-02-10 14:45:28 -08004698 mApp, subId, "isAdvancedCallingSettingEnabled");
shilu366312e2019-12-17 09:28:10 -08004699
Brad Ebinger35c841c2018-10-01 10:40:55 -07004700 final long token = Binder.clearCallingIdentity();
4701 try {
Brad Ebinger919631e2021-06-02 17:46:35 -07004702 int slotId = getSlotIndexOrException(subId);
Brad Ebinger735c5ce2021-07-12 13:58:21 -07004703 // This setting doesn't require an active ImsService connection, so do not verify.
Brad Ebinger919631e2021-06-02 17:46:35 -07004704 return ImsManager.getInstance(mApp, slotId).isEnhanced4gLteModeSettingEnabledByUser();
Brad Ebinger1ce9c432019-07-16 13:19:44 -07004705 } catch (ImsException e) {
4706 throw new ServiceSpecificException(e.getCode());
Brad Ebinger35c841c2018-10-01 10:40:55 -07004707 } finally {
4708 Binder.restoreCallingIdentity(token);
4709 }
4710 }
4711
4712 @Override
Brad Ebinger1c162042019-02-21 14:49:10 -08004713 public void setAdvancedCallingSettingEnabled(int subId, boolean isEnabled) {
Brad Ebinger35c841c2018-10-01 10:40:55 -07004714 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(mApp, subId,
Brad Ebinger1c162042019-02-21 14:49:10 -08004715 "setAdvancedCallingSettingEnabled");
Brad Ebinger35c841c2018-10-01 10:40:55 -07004716 final long identity = Binder.clearCallingIdentity();
4717 try {
Brad Ebinger919631e2021-06-02 17:46:35 -07004718 int slotId = getSlotIndexOrException(subId);
Brad Ebinger735c5ce2021-07-12 13:58:21 -07004719 // This setting doesn't require an active ImsService connection, so do not verify. The
4720 // new setting will be picked up when the ImsService comes up next if it isn't up.
Brad Ebinger919631e2021-06-02 17:46:35 -07004721 ImsManager.getInstance(mApp, slotId).setEnhanced4gLteModeSetting(isEnabled);
Brad Ebinger1ce9c432019-07-16 13:19:44 -07004722 } catch (ImsException e) {
4723 throw new ServiceSpecificException(e.getCode());
Brad Ebinger35c841c2018-10-01 10:40:55 -07004724 } finally {
4725 Binder.restoreCallingIdentity(identity);
4726 }
4727 }
4728
shilu366312e2019-12-17 09:28:10 -08004729 /**
4730 * Requires carrier privileges or READ_PRECISE_PHONE_STATE permission.
4731 * @param subId The subscription to use to check the configuration.
4732 */
Brad Ebinger35c841c2018-10-01 10:40:55 -07004733 @Override
Brad Ebinger9878b0b2018-11-08 17:43:22 -08004734 public boolean isVtSettingEnabled(int subId) {
Nathan Harold62c68512021-04-06 11:26:02 -07004735 TelephonyPermissions.enforceCallingOrSelfReadPrecisePhoneStatePermissionOrCarrierPrivilege(
Rambo Wang37f9c242020-02-10 14:45:28 -08004736 mApp, subId, "isVtSettingEnabled");
Brad Ebinger35c841c2018-10-01 10:40:55 -07004737 final long identity = Binder.clearCallingIdentity();
4738 try {
Brad Ebinger919631e2021-06-02 17:46:35 -07004739 int slotId = getSlotIndexOrException(subId);
Brad Ebinger735c5ce2021-07-12 13:58:21 -07004740 // This setting doesn't require an active ImsService connection, so do not verify.
Brad Ebinger919631e2021-06-02 17:46:35 -07004741 return ImsManager.getInstance(mApp, slotId).isVtEnabledByUser();
Brad Ebinger1ce9c432019-07-16 13:19:44 -07004742 } catch (ImsException e) {
4743 throw new ServiceSpecificException(e.getCode());
Brad Ebinger35c841c2018-10-01 10:40:55 -07004744 } finally {
4745 Binder.restoreCallingIdentity(identity);
4746 }
4747 }
4748
4749 @Override
Brad Ebinger1c162042019-02-21 14:49:10 -08004750 public void setVtSettingEnabled(int subId, boolean isEnabled) {
Brad Ebinger35c841c2018-10-01 10:40:55 -07004751 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(mApp, subId,
Brad Ebinger1c162042019-02-21 14:49:10 -08004752 "setVtSettingEnabled");
Brad Ebinger35c841c2018-10-01 10:40:55 -07004753 final long identity = Binder.clearCallingIdentity();
4754 try {
Brad Ebinger919631e2021-06-02 17:46:35 -07004755 int slotId = getSlotIndexOrException(subId);
Brad Ebinger735c5ce2021-07-12 13:58:21 -07004756 // This setting doesn't require an active ImsService connection, so do not verify. The
4757 // new setting will be picked up when the ImsService comes up next if it isn't up.
Brad Ebinger919631e2021-06-02 17:46:35 -07004758 ImsManager.getInstance(mApp, slotId).setVtSetting(isEnabled);
Brad Ebinger1ce9c432019-07-16 13:19:44 -07004759 } catch (ImsException e) {
4760 throw new ServiceSpecificException(e.getCode());
Brad Ebinger35c841c2018-10-01 10:40:55 -07004761 } finally {
4762 Binder.restoreCallingIdentity(identity);
4763 }
4764 }
4765
shilu366312e2019-12-17 09:28:10 -08004766 /**
4767 * Requires carrier privileges or READ_PRECISE_PHONE_STATE permission.
4768 * @param subId The subscription to use to check the configuration.
4769 */
Brad Ebinger35c841c2018-10-01 10:40:55 -07004770 @Override
4771 public boolean isVoWiFiSettingEnabled(int subId) {
Nathan Harold62c68512021-04-06 11:26:02 -07004772 TelephonyPermissions.enforceCallingOrSelfReadPrecisePhoneStatePermissionOrCarrierPrivilege(
Rambo Wang37f9c242020-02-10 14:45:28 -08004773 mApp, subId, "isVoWiFiSettingEnabled");
Brad Ebinger35c841c2018-10-01 10:40:55 -07004774 final long identity = Binder.clearCallingIdentity();
4775 try {
Brad Ebinger919631e2021-06-02 17:46:35 -07004776 int slotId = getSlotIndexOrException(subId);
Brad Ebinger735c5ce2021-07-12 13:58:21 -07004777 // This setting doesn't require an active ImsService connection, so do not verify.
Brad Ebinger919631e2021-06-02 17:46:35 -07004778 return ImsManager.getInstance(mApp, slotId).isWfcEnabledByUser();
Brad Ebinger1ce9c432019-07-16 13:19:44 -07004779 } catch (ImsException e) {
4780 throw new ServiceSpecificException(e.getCode());
Brad Ebinger35c841c2018-10-01 10:40:55 -07004781 } finally {
4782 Binder.restoreCallingIdentity(identity);
4783 }
4784 }
4785
4786 @Override
Brad Ebinger1c162042019-02-21 14:49:10 -08004787 public void setVoWiFiSettingEnabled(int subId, boolean isEnabled) {
Brad Ebinger35c841c2018-10-01 10:40:55 -07004788 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(mApp, subId,
Brad Ebinger1c162042019-02-21 14:49:10 -08004789 "setVoWiFiSettingEnabled");
Brad Ebinger35c841c2018-10-01 10:40:55 -07004790 final long identity = Binder.clearCallingIdentity();
4791 try {
Brad Ebinger919631e2021-06-02 17:46:35 -07004792 int slotId = getSlotIndexOrException(subId);
Brad Ebinger735c5ce2021-07-12 13:58:21 -07004793 // This setting doesn't require an active ImsService connection, so do not verify. The
4794 // new setting will be picked up when the ImsService comes up next if it isn't up.
Brad Ebinger919631e2021-06-02 17:46:35 -07004795 ImsManager.getInstance(mApp, slotId).setWfcSetting(isEnabled);
Brad Ebinger1ce9c432019-07-16 13:19:44 -07004796 } catch (ImsException e) {
4797 throw new ServiceSpecificException(e.getCode());
Brad Ebinger35c841c2018-10-01 10:40:55 -07004798 } finally {
4799 Binder.restoreCallingIdentity(identity);
4800 }
4801 }
4802
shilu366312e2019-12-17 09:28:10 -08004803 /**
Sooraj Sasindrane655add2020-11-23 19:40:38 -08004804 * @return true if the user's setting for Voice over Cross SIM is enabled and false if it is not
4805 * Requires carrier privileges or READ_PRECISE_PHONE_STATE permission.
4806 * @param subId The subscription to use to check the configuration.
4807 */
4808 @Override
4809 public boolean isCrossSimCallingEnabledByUser(int subId) {
Nathan Harold62c68512021-04-06 11:26:02 -07004810 TelephonyPermissions.enforceCallingOrSelfReadPrecisePhoneStatePermissionOrCarrierPrivilege(
Sooraj Sasindrane655add2020-11-23 19:40:38 -08004811 mApp, subId, "isCrossSimCallingEnabledByUser");
4812 final long identity = Binder.clearCallingIdentity();
4813 try {
Brad Ebinger919631e2021-06-02 17:46:35 -07004814 int slotId = getSlotIndexOrException(subId);
Brad Ebinger735c5ce2021-07-12 13:58:21 -07004815 // This setting doesn't require an active ImsService connection, so do not verify.
Brad Ebinger919631e2021-06-02 17:46:35 -07004816 return ImsManager.getInstance(mApp, slotId).isCrossSimCallingEnabledByUser();
Sooraj Sasindrane655add2020-11-23 19:40:38 -08004817 } catch (ImsException e) {
4818 throw new ServiceSpecificException(e.getCode());
4819 } finally {
4820 Binder.restoreCallingIdentity(identity);
4821 }
4822 }
4823
4824 /**
4825 * Sets the user's setting for whether or not Voice over Cross SIM is enabled.
4826 * Requires MODIFY_PHONE_STATE permission.
4827 * @param subId The subscription to use to check the configuration.
4828 * @param isEnabled true if the user's setting for Voice over Cross SIM is enabled,
4829 * false otherwise
4830 */
4831 @Override
4832 public void setCrossSimCallingEnabled(int subId, boolean isEnabled) {
4833 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(mApp, subId,
4834 "setCrossSimCallingEnabled");
4835 final long identity = Binder.clearCallingIdentity();
4836 try {
Brad Ebinger919631e2021-06-02 17:46:35 -07004837 int slotId = getSlotIndexOrException(subId);
Brad Ebinger735c5ce2021-07-12 13:58:21 -07004838 // This setting doesn't require an active ImsService connection, so do not verify. The
4839 // new setting will be picked up when the ImsService comes up next if it isn't up.
Brad Ebinger919631e2021-06-02 17:46:35 -07004840 ImsManager.getInstance(mApp, slotId).setCrossSimCallingEnabled(isEnabled);
Sooraj Sasindrane655add2020-11-23 19:40:38 -08004841 } catch (ImsException e) {
4842 throw new ServiceSpecificException(e.getCode());
4843 } finally {
4844 Binder.restoreCallingIdentity(identity);
4845 }
4846 }
4847
4848 /**
shilu366312e2019-12-17 09:28:10 -08004849 * Requires carrier privileges or READ_PRECISE_PHONE_STATE permission.
4850 * @param subId The subscription to use to check the configuration.
4851 */
Brad Ebinger35c841c2018-10-01 10:40:55 -07004852 @Override
Nathan Harold62c68512021-04-06 11:26:02 -07004853
Brad Ebinger35c841c2018-10-01 10:40:55 -07004854 public boolean isVoWiFiRoamingSettingEnabled(int subId) {
Nathan Harold62c68512021-04-06 11:26:02 -07004855 TelephonyPermissions.enforceCallingOrSelfReadPrecisePhoneStatePermissionOrCarrierPrivilege(
Rambo Wang37f9c242020-02-10 14:45:28 -08004856 mApp, subId, "isVoWiFiRoamingSettingEnabled");
Brad Ebinger35c841c2018-10-01 10:40:55 -07004857 final long identity = Binder.clearCallingIdentity();
4858 try {
Brad Ebinger919631e2021-06-02 17:46:35 -07004859 int slotId = getSlotIndexOrException(subId);
Brad Ebinger735c5ce2021-07-12 13:58:21 -07004860 // This setting doesn't require an active ImsService connection, so do not verify.
Brad Ebinger919631e2021-06-02 17:46:35 -07004861 return ImsManager.getInstance(mApp, slotId).isWfcRoamingEnabledByUser();
Brad Ebinger1ce9c432019-07-16 13:19:44 -07004862 } catch (ImsException e) {
4863 throw new ServiceSpecificException(e.getCode());
Brad Ebinger35c841c2018-10-01 10:40:55 -07004864 } finally {
4865 Binder.restoreCallingIdentity(identity);
4866 }
4867 }
4868
4869 @Override
Brad Ebinger1c162042019-02-21 14:49:10 -08004870 public void setVoWiFiRoamingSettingEnabled(int subId, boolean isEnabled) {
Brad Ebinger35c841c2018-10-01 10:40:55 -07004871 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(mApp, subId,
Brad Ebinger1c162042019-02-21 14:49:10 -08004872 "setVoWiFiRoamingSettingEnabled");
Brad Ebinger35c841c2018-10-01 10:40:55 -07004873 final long identity = Binder.clearCallingIdentity();
4874 try {
Brad Ebinger919631e2021-06-02 17:46:35 -07004875 int slotId = getSlotIndexOrException(subId);
Brad Ebinger735c5ce2021-07-12 13:58:21 -07004876 // This setting doesn't require an active ImsService connection, so do not verify. The
4877 // new setting will be picked up when the ImsService comes up next if it isn't up.
Brad Ebinger919631e2021-06-02 17:46:35 -07004878 ImsManager.getInstance(mApp, slotId).setWfcRoamingSetting(isEnabled);
Brad Ebinger1ce9c432019-07-16 13:19:44 -07004879 } catch (ImsException e) {
4880 throw new ServiceSpecificException(e.getCode());
Brad Ebinger35c841c2018-10-01 10:40:55 -07004881 } finally {
4882 Binder.restoreCallingIdentity(identity);
4883 }
4884 }
4885
4886 @Override
4887 public void setVoWiFiNonPersistent(int subId, boolean isCapable, int mode) {
4888 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(mApp, subId,
4889 "setVoWiFiNonPersistent");
4890 final long identity = Binder.clearCallingIdentity();
4891 try {
Brad Ebinger919631e2021-06-02 17:46:35 -07004892 int slotId = getSlotIndexOrException(subId);
Brad Ebinger735c5ce2021-07-12 13:58:21 -07004893 // This setting will be ignored if the ImsService isn't up.
Brad Ebinger919631e2021-06-02 17:46:35 -07004894 ImsManager.getInstance(mApp, slotId).setWfcNonPersistent(isCapable, mode);
Brad Ebinger1ce9c432019-07-16 13:19:44 -07004895 } catch (ImsException e) {
4896 throw new ServiceSpecificException(e.getCode());
Brad Ebinger35c841c2018-10-01 10:40:55 -07004897 } finally {
4898 Binder.restoreCallingIdentity(identity);
4899 }
4900 }
4901
shilu366312e2019-12-17 09:28:10 -08004902 /**
4903 * Requires carrier privileges or READ_PRECISE_PHONE_STATE permission.
4904 * @param subId The subscription to use to check the configuration.
4905 */
Brad Ebinger35c841c2018-10-01 10:40:55 -07004906 @Override
4907 public int getVoWiFiModeSetting(int subId) {
Nathan Harold62c68512021-04-06 11:26:02 -07004908 TelephonyPermissions.enforceCallingOrSelfReadPrecisePhoneStatePermissionOrCarrierPrivilege(
Rambo Wang37f9c242020-02-10 14:45:28 -08004909 mApp, subId, "getVoWiFiModeSetting");
Brad Ebinger35c841c2018-10-01 10:40:55 -07004910 final long identity = Binder.clearCallingIdentity();
4911 try {
Brad Ebinger919631e2021-06-02 17:46:35 -07004912 int slotId = getSlotIndexOrException(subId);
Brad Ebinger735c5ce2021-07-12 13:58:21 -07004913 // This setting doesn't require an active ImsService connection, so do not verify.
Brad Ebinger919631e2021-06-02 17:46:35 -07004914 return ImsManager.getInstance(mApp, slotId).getWfcMode(false /*isRoaming*/);
Brad Ebinger1ce9c432019-07-16 13:19:44 -07004915 } catch (ImsException e) {
4916 throw new ServiceSpecificException(e.getCode());
Brad Ebinger35c841c2018-10-01 10:40:55 -07004917 } finally {
4918 Binder.restoreCallingIdentity(identity);
4919 }
4920 }
4921
4922 @Override
4923 public void setVoWiFiModeSetting(int subId, int mode) {
4924 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(mApp, subId,
4925 "setVoWiFiModeSetting");
4926 final long identity = Binder.clearCallingIdentity();
4927 try {
Brad Ebinger919631e2021-06-02 17:46:35 -07004928 int slotId = getSlotIndexOrException(subId);
Brad Ebinger735c5ce2021-07-12 13:58:21 -07004929 // This setting doesn't require an active ImsService connection, so do not verify. The
4930 // new setting will be picked up when the ImsService comes up next if it isn't up.
Brad Ebinger919631e2021-06-02 17:46:35 -07004931 ImsManager.getInstance(mApp, slotId).setWfcMode(mode, false /*isRoaming*/);
Brad Ebinger1ce9c432019-07-16 13:19:44 -07004932 } catch (ImsException e) {
4933 throw new ServiceSpecificException(e.getCode());
Brad Ebinger35c841c2018-10-01 10:40:55 -07004934 } finally {
4935 Binder.restoreCallingIdentity(identity);
4936 }
4937 }
4938
4939 @Override
4940 public int getVoWiFiRoamingModeSetting(int subId) {
4941 enforceReadPrivilegedPermission("getVoWiFiRoamingModeSetting");
4942 final long identity = Binder.clearCallingIdentity();
4943 try {
Brad Ebinger919631e2021-06-02 17:46:35 -07004944 int slotId = getSlotIndexOrException(subId);
Brad Ebinger735c5ce2021-07-12 13:58:21 -07004945 // This setting doesn't require an active ImsService connection, so do not verify.
Brad Ebinger919631e2021-06-02 17:46:35 -07004946 return ImsManager.getInstance(mApp, slotId).getWfcMode(true /*isRoaming*/);
Brad Ebinger1ce9c432019-07-16 13:19:44 -07004947 } catch (ImsException e) {
4948 throw new ServiceSpecificException(e.getCode());
Brad Ebinger35c841c2018-10-01 10:40:55 -07004949 } finally {
4950 Binder.restoreCallingIdentity(identity);
4951 }
4952 }
4953
4954 @Override
4955 public void setVoWiFiRoamingModeSetting(int subId, int mode) {
4956 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(mApp, subId,
4957 "setVoWiFiRoamingModeSetting");
4958 final long identity = Binder.clearCallingIdentity();
4959 try {
Brad Ebinger919631e2021-06-02 17:46:35 -07004960 int slotId = getSlotIndexOrException(subId);
Brad Ebinger735c5ce2021-07-12 13:58:21 -07004961 // This setting doesn't require an active ImsService connection, so do not verify. The
4962 // new setting will be picked up when the ImsService comes up next if it isn't up.
Brad Ebinger919631e2021-06-02 17:46:35 -07004963 ImsManager.getInstance(mApp, slotId).setWfcMode(mode, true /*isRoaming*/);
Brad Ebinger1ce9c432019-07-16 13:19:44 -07004964 } catch (ImsException e) {
4965 throw new ServiceSpecificException(e.getCode());
Brad Ebinger35c841c2018-10-01 10:40:55 -07004966 } finally {
4967 Binder.restoreCallingIdentity(identity);
4968 }
4969 }
4970
4971 @Override
4972 public void setRttCapabilitySetting(int subId, boolean isEnabled) {
4973 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(mApp, subId,
4974 "setRttCapabilityEnabled");
4975 final long identity = Binder.clearCallingIdentity();
4976 try {
Brad Ebinger919631e2021-06-02 17:46:35 -07004977 int slotId = getSlotIndexOrException(subId);
Brad Ebinger735c5ce2021-07-12 13:58:21 -07004978 // This setting doesn't require an active ImsService connection, so do not verify. The
4979 // new setting will be picked up when the ImsService comes up next if it isn't up.
Brad Ebinger919631e2021-06-02 17:46:35 -07004980 ImsManager.getInstance(mApp, slotId).setRttEnabled(isEnabled);
Brad Ebinger1ce9c432019-07-16 13:19:44 -07004981 } catch (ImsException e) {
4982 throw new ServiceSpecificException(e.getCode());
Brad Ebinger35c841c2018-10-01 10:40:55 -07004983 } finally {
4984 Binder.restoreCallingIdentity(identity);
4985 }
4986 }
4987
shilu366312e2019-12-17 09:28:10 -08004988 /**
4989 * Requires carrier privileges or READ_PRECISE_PHONE_STATE permission.
4990 * @param subId The subscription to use to check the configuration.
4991 */
Brad Ebinger35c841c2018-10-01 10:40:55 -07004992 @Override
4993 public boolean isTtyOverVolteEnabled(int subId) {
Nathan Harold62c68512021-04-06 11:26:02 -07004994 TelephonyPermissions.enforceCallingOrSelfReadPrecisePhoneStatePermissionOrCarrierPrivilege(
Rambo Wang37f9c242020-02-10 14:45:28 -08004995 mApp, subId, "isTtyOverVolteEnabled");
Brad Ebinger35c841c2018-10-01 10:40:55 -07004996 final long identity = Binder.clearCallingIdentity();
4997 try {
Brad Ebinger919631e2021-06-02 17:46:35 -07004998 int slotId = getSlotIndexOrException(subId);
Brad Ebinger735c5ce2021-07-12 13:58:21 -07004999 // This setting doesn't require an active ImsService connection, so do not verify.
Brad Ebinger919631e2021-06-02 17:46:35 -07005000 return ImsManager.getInstance(mApp, slotId).isTtyOnVoLteCapable();
Brad Ebinger1ce9c432019-07-16 13:19:44 -07005001 } catch (ImsException e) {
5002 throw new ServiceSpecificException(e.getCode());
Brad Ebinger35c841c2018-10-01 10:40:55 -07005003 } finally {
5004 Binder.restoreCallingIdentity(identity);
5005 }
5006 }
5007
Brad Ebingerdf5b4f02018-10-31 11:24:17 -07005008 @Override
5009 public void registerImsProvisioningChangedCallback(int subId, IImsConfigCallback callback) {
5010 enforceReadPrivilegedPermission("registerImsProvisioningChangedCallback");
joonhunshincffb7fc2021-11-28 07:32:01 +00005011
Brad Ebingerdf5b4f02018-10-31 11:24:17 -07005012 final long identity = Binder.clearCallingIdentity();
5013 try {
Brad Ebingera2628302022-02-18 03:44:55 +00005014 if (!isImsAvailableOnDevice()) {
5015 throw new ServiceSpecificException(ImsException.CODE_ERROR_UNSUPPORTED_OPERATION,
5016 "IMS not available on device.");
5017 }
5018 int slotId = getSlotIndexOrException(subId);
5019 verifyImsMmTelConfiguredOrThrow(slotId);
Hwangoo Park8646b0d2023-01-13 02:42:34 +00005020
5021 ImsStateCallbackController controller = ImsStateCallbackController.getInstance();
5022 if (controller != null) {
5023 ImsManager imsManager = controller.getImsManager(subId);
5024 if (imsManager != null) {
5025 imsManager.addProvisioningCallbackForSubscription(callback, subId);
5026 } else {
5027 throw new ServiceSpecificException(ImsException.CODE_ERROR_SERVICE_UNAVAILABLE);
5028 }
5029 } else {
5030 throw new ServiceSpecificException(ImsException.CODE_ERROR_UNSUPPORTED_OPERATION);
5031 }
Brad Ebinger1ce9c432019-07-16 13:19:44 -07005032 } catch (ImsException e) {
5033 throw new ServiceSpecificException(e.getCode());
Brad Ebingerdf5b4f02018-10-31 11:24:17 -07005034 } finally {
5035 Binder.restoreCallingIdentity(identity);
5036 }
5037 }
5038
5039 @Override
5040 public void unregisterImsProvisioningChangedCallback(int subId, IImsConfigCallback callback) {
5041 enforceReadPrivilegedPermission("unregisterImsProvisioningChangedCallback");
joonhunshincffb7fc2021-11-28 07:32:01 +00005042
Brad Ebingerdf5b4f02018-10-31 11:24:17 -07005043 final long identity = Binder.clearCallingIdentity();
Brad Ebinger4ae57f92019-01-09 16:51:30 -08005044 if (!SubscriptionManager.isValidSubscriptionId(subId)) {
5045 throw new IllegalArgumentException("Invalid Subscription ID: " + subId);
5046 }
Brad Ebingerdf5b4f02018-10-31 11:24:17 -07005047 try {
Hwangoo Park8646b0d2023-01-13 02:42:34 +00005048 ImsStateCallbackController controller = ImsStateCallbackController.getInstance();
5049 if (controller != null) {
5050 ImsManager imsManager = controller.getImsManager(subId);
5051 if (imsManager != null) {
5052 imsManager.removeProvisioningCallbackForSubscription(callback, subId);
5053 } else {
5054 Log.i(LOG_TAG, "unregisterImsProvisioningChangedCallback: " + subId
5055 + " is inactive, ignoring unregister.");
5056 // If the ImsManager is not valid, just return, since the callback will already
5057 // have been removed internally.
5058 }
5059 }
Brad Ebingerdf5b4f02018-10-31 11:24:17 -07005060 } finally {
5061 Binder.restoreCallingIdentity(identity);
5062 }
5063 }
5064
joonhunshincffb7fc2021-11-28 07:32:01 +00005065 @Override
5066 public void registerFeatureProvisioningChangedCallback(int subId,
5067 IFeatureProvisioningCallback callback) {
5068 TelephonyPermissions.enforceCallingOrSelfReadPrecisePhoneStatePermissionOrCarrierPrivilege(
5069 mApp, subId, "registerFeatureProvisioningChangedCallback");
5070
5071 final long identity = Binder.clearCallingIdentity();
5072 if (!SubscriptionManager.isValidSubscriptionId(subId)) {
5073 throw new IllegalArgumentException("Invalid Subscription ID: " + subId);
5074 }
5075
joonhunshin91bc1952022-04-29 08:47:15 +00005076 try {
5077 ImsProvisioningController controller = ImsProvisioningController.getInstance();
5078 if (controller == null) {
5079 throw new ServiceSpecificException(ImsException.CODE_ERROR_UNSUPPORTED_OPERATION,
5080 "Device does not support IMS");
5081 }
5082 controller.addFeatureProvisioningChangedCallback(subId, callback);
5083 } finally {
5084 Binder.restoreCallingIdentity(identity);
5085 }
joonhunshincffb7fc2021-11-28 07:32:01 +00005086 }
5087
5088 @Override
5089 public void unregisterFeatureProvisioningChangedCallback(int subId,
5090 IFeatureProvisioningCallback callback) {
5091 TelephonyPermissions.enforceCallingOrSelfReadPrecisePhoneStatePermissionOrCarrierPrivilege(
5092 mApp, subId, "unregisterFeatureProvisioningChangedCallback");
5093
5094 final long identity = Binder.clearCallingIdentity();
5095 if (!SubscriptionManager.isValidSubscriptionId(subId)) {
5096 throw new IllegalArgumentException("Invalid Subscription ID: " + subId);
5097 }
5098
joonhunshin91bc1952022-04-29 08:47:15 +00005099 try {
5100 ImsProvisioningController controller = ImsProvisioningController.getInstance();
5101 if (controller == null) {
5102 loge("unregisterFeatureProvisioningChangedCallback: Device does not support IMS");
5103 return;
5104 }
5105 controller.removeFeatureProvisioningChangedCallback(subId, callback);
5106 } finally {
5107 Binder.restoreCallingIdentity(identity);
5108 }
joonhunshincffb7fc2021-11-28 07:32:01 +00005109 }
allenwtsu99c623b2020-01-03 18:24:23 +08005110
5111 private void checkModifyPhoneStatePermission(int subId, String message) {
5112 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(mApp, subId,
5113 message);
5114 }
5115
allenwtsu99c623b2020-01-03 18:24:23 +08005116 @Override
joonhunshincffb7fc2021-11-28 07:32:01 +00005117 public void setRcsProvisioningStatusForCapability(int subId, int capability, int tech,
allenwtsu99c623b2020-01-03 18:24:23 +08005118 boolean isProvisioned) {
5119 checkModifyPhoneStatePermission(subId, "setRcsProvisioningStatusForCapability");
5120
5121 final long identity = Binder.clearCallingIdentity();
5122 try {
joonhunshin91bc1952022-04-29 08:47:15 +00005123 ImsProvisioningController controller = ImsProvisioningController.getInstance();
5124 if (controller == null) {
5125 loge("setRcsProvisioningStatusForCapability: Device does not support IMS");
5126 return;
5127 }
5128 controller.setRcsProvisioningStatusForCapability(
5129 subId, capability, tech, isProvisioned);
allenwtsu99c623b2020-01-03 18:24:23 +08005130 } finally {
5131 Binder.restoreCallingIdentity(identity);
5132 }
allenwtsu99c623b2020-01-03 18:24:23 +08005133 }
5134
5135
5136 @Override
joonhunshincffb7fc2021-11-28 07:32:01 +00005137 public boolean getRcsProvisioningStatusForCapability(int subId, int capability, int tech) {
5138 TelephonyPermissions.enforceCallingOrSelfReadPrecisePhoneStatePermissionOrCarrierPrivilege(
5139 mApp, subId, "getRcsProvisioningStatusForCapability");
5140
allenwtsu99c623b2020-01-03 18:24:23 +08005141 final long identity = Binder.clearCallingIdentity();
5142 try {
joonhunshin91bc1952022-04-29 08:47:15 +00005143 ImsProvisioningController controller = ImsProvisioningController.getInstance();
5144 if (controller == null) {
5145 loge("getRcsProvisioningStatusForCapability: Device does not support IMS");
5146
5147 // device does not support IMS, this method will return true always.
5148 return true;
5149 }
5150 return controller.getRcsProvisioningStatusForCapability(subId, capability, tech);
allenwtsu99c623b2020-01-03 18:24:23 +08005151 } finally {
5152 Binder.restoreCallingIdentity(identity);
5153 }
5154 }
5155
Brad Ebingerdf5b4f02018-10-31 11:24:17 -07005156 @Override
Brad Ebinger1c8542e2019-01-14 13:43:14 -08005157 public void setImsProvisioningStatusForCapability(int subId, int capability, int tech,
5158 boolean isProvisioned) {
allenwtsu99c623b2020-01-03 18:24:23 +08005159 checkModifyPhoneStatePermission(subId, "setImsProvisioningStatusForCapability");
joonhunshincffb7fc2021-11-28 07:32:01 +00005160
Brad Ebinger1c8542e2019-01-14 13:43:14 -08005161 final long identity = Binder.clearCallingIdentity();
5162 try {
joonhunshin91bc1952022-04-29 08:47:15 +00005163 ImsProvisioningController controller = ImsProvisioningController.getInstance();
5164 if (controller == null) {
5165 loge("setImsProvisioningStatusForCapability: Device does not support IMS");
5166 return;
5167 }
5168 controller.setImsProvisioningStatusForCapability(
5169 subId, capability, tech, isProvisioned);
Brad Ebinger1c8542e2019-01-14 13:43:14 -08005170 } finally {
5171 Binder.restoreCallingIdentity(identity);
5172 }
5173 }
5174
5175 @Override
5176 public boolean getImsProvisioningStatusForCapability(int subId, int capability, int tech) {
joonhunshincffb7fc2021-11-28 07:32:01 +00005177 TelephonyPermissions.enforceCallingOrSelfReadPrecisePhoneStatePermissionOrCarrierPrivilege(
5178 mApp, subId, "getProvisioningStatusForCapability");
5179
Brad Ebinger1c8542e2019-01-14 13:43:14 -08005180 final long identity = Binder.clearCallingIdentity();
5181 try {
joonhunshin91bc1952022-04-29 08:47:15 +00005182 ImsProvisioningController controller = ImsProvisioningController.getInstance();
5183 if (controller == null) {
5184 loge("getImsProvisioningStatusForCapability: Device does not support IMS");
Brad Ebinger1c8542e2019-01-14 13:43:14 -08005185
joonhunshin91bc1952022-04-29 08:47:15 +00005186 // device does not support IMS, this method will return true always.
5187 return true;
5188 }
5189 return controller.getImsProvisioningStatusForCapability(subId, capability, tech);
Brad Ebinger1c8542e2019-01-14 13:43:14 -08005190 } finally {
5191 Binder.restoreCallingIdentity(identity);
5192 }
5193 }
5194
5195 @Override
joonhunshincffb7fc2021-11-28 07:32:01 +00005196 public boolean isProvisioningRequiredForCapability(int subId, int capability, int tech) {
5197 TelephonyPermissions.enforceCallingOrSelfReadPrecisePhoneStatePermissionOrCarrierPrivilege(
5198 mApp, subId, "isProvisioningRequiredForCapability");
5199
5200 final long identity = Binder.clearCallingIdentity();
5201 try {
joonhunshin91bc1952022-04-29 08:47:15 +00005202 ImsProvisioningController controller = ImsProvisioningController.getInstance();
5203 if (controller == null) {
5204 loge("isProvisioningRequiredForCapability: Device does not support IMS");
5205
5206 // device does not support IMS, this method will return false
5207 return false;
5208 }
5209 return controller.isImsProvisioningRequiredForCapability(subId, capability, tech);
joonhunshincffb7fc2021-11-28 07:32:01 +00005210 } finally {
5211 Binder.restoreCallingIdentity(identity);
Brad Ebinger1c8542e2019-01-14 13:43:14 -08005212 }
Brad Ebinger1c8542e2019-01-14 13:43:14 -08005213 }
5214
5215 @Override
joonhunshincffb7fc2021-11-28 07:32:01 +00005216 public boolean isRcsProvisioningRequiredForCapability(int subId, int capability, int tech) {
5217 TelephonyPermissions.enforceCallingOrSelfReadPrecisePhoneStatePermissionOrCarrierPrivilege(
5218 mApp, subId, "isProvisioningRequiredForCapability");
Brad Ebinger1c8542e2019-01-14 13:43:14 -08005219
joonhunshincffb7fc2021-11-28 07:32:01 +00005220 final long identity = Binder.clearCallingIdentity();
5221 try {
joonhunshin91bc1952022-04-29 08:47:15 +00005222 ImsProvisioningController controller = ImsProvisioningController.getInstance();
5223 if (controller == null) {
5224 loge("isRcsProvisioningRequiredForCapability: Device does not support IMS");
5225
5226 // device does not support IMS, this method will return false
5227 return false;
5228 }
5229 return controller.isRcsProvisioningRequiredForCapability(subId, capability, tech);
joonhunshincffb7fc2021-11-28 07:32:01 +00005230 } finally {
5231 Binder.restoreCallingIdentity(identity);
5232 }
Brad Ebinger1c8542e2019-01-14 13:43:14 -08005233 }
5234
Brad Ebinger1c8542e2019-01-14 13:43:14 -08005235 @Override
Brad Ebingerdf5b4f02018-10-31 11:24:17 -07005236 public int getImsProvisioningInt(int subId, int key) {
Brad Ebinger1c8542e2019-01-14 13:43:14 -08005237 if (!SubscriptionManager.isValidSubscriptionId(subId)) {
5238 throw new IllegalArgumentException("Invalid Subscription id '" + subId + "'");
5239 }
joonhunshincffb7fc2021-11-28 07:32:01 +00005240 TelephonyPermissions.enforceCallingOrSelfReadPrecisePhoneStatePermissionOrCarrierPrivilege(
5241 mApp, subId, "getImsProvisioningInt");
5242
Brad Ebingerdf5b4f02018-10-31 11:24:17 -07005243 final long identity = Binder.clearCallingIdentity();
5244 try {
5245 // TODO: Refactor to remove ImsManager dependence and query through ImsPhone directly.
Brad Ebinger1c8542e2019-01-14 13:43:14 -08005246 int slotId = getSlotIndex(subId);
5247 if (slotId <= SubscriptionManager.INVALID_SIM_SLOT_INDEX) {
5248 Log.w(LOG_TAG, "getImsProvisioningInt: called with an inactive subscription '"
5249 + subId + "' for key:" + key);
5250 return ImsConfigImplBase.CONFIG_RESULT_UNKNOWN;
5251 }
joonhunshincffb7fc2021-11-28 07:32:01 +00005252
joonhunshin91bc1952022-04-29 08:47:15 +00005253 ImsProvisioningController controller = ImsProvisioningController.getInstance();
5254 if (controller == null) {
5255 loge("getImsProvisioningInt: Device does not support IMS");
5256
5257 // device does not support IMS, this method will return CONFIG_RESULT_UNKNOWN.
5258 return ImsConfigImplBase.CONFIG_RESULT_UNKNOWN;
5259 }
5260 int retVal = controller.getProvisioningValue(subId, key);
joonhunshincffb7fc2021-11-28 07:32:01 +00005261 if (retVal != ImsConfigImplBase.CONFIG_RESULT_UNKNOWN) {
5262 return retVal;
5263 }
5264
calvinpanb5a34062021-02-08 19:59:36 +08005265 return ImsManager.getInstance(mApp, slotId).getConfigInt(key);
Brad Ebinger1ce9c432019-07-16 13:19:44 -07005266 } catch (com.android.ims.ImsException e) {
Brad Ebinger1c8542e2019-01-14 13:43:14 -08005267 Log.w(LOG_TAG, "getImsProvisioningInt: ImsService is not available for subscription '"
5268 + subId + "' for key:" + key);
5269 return ImsConfigImplBase.CONFIG_RESULT_UNKNOWN;
Brad Ebingerdf5b4f02018-10-31 11:24:17 -07005270 } finally {
5271 Binder.restoreCallingIdentity(identity);
5272 }
5273 }
5274
5275 @Override
5276 public String getImsProvisioningString(int subId, int key) {
Brad Ebinger1c8542e2019-01-14 13:43:14 -08005277 if (!SubscriptionManager.isValidSubscriptionId(subId)) {
5278 throw new IllegalArgumentException("Invalid Subscription id '" + subId + "'");
5279 }
joonhunshincffb7fc2021-11-28 07:32:01 +00005280 TelephonyPermissions.enforceCallingOrSelfReadPrecisePhoneStatePermissionOrCarrierPrivilege(
5281 mApp, subId, "getImsProvisioningString");
5282
Brad Ebingerdf5b4f02018-10-31 11:24:17 -07005283 final long identity = Binder.clearCallingIdentity();
5284 try {
5285 // TODO: Refactor to remove ImsManager dependence and query through ImsPhone directly.
Brad Ebinger1c8542e2019-01-14 13:43:14 -08005286 int slotId = getSlotIndex(subId);
5287 if (slotId <= SubscriptionManager.INVALID_SIM_SLOT_INDEX) {
5288 Log.w(LOG_TAG, "getImsProvisioningString: called for an inactive subscription id '"
5289 + subId + "' for key:" + key);
5290 return ProvisioningManager.STRING_QUERY_RESULT_ERROR_GENERIC;
5291 }
calvinpanb5a34062021-02-08 19:59:36 +08005292 return ImsManager.getInstance(mApp, slotId).getConfigString(key);
Brad Ebinger1ce9c432019-07-16 13:19:44 -07005293 } catch (com.android.ims.ImsException e) {
Brad Ebinger1c8542e2019-01-14 13:43:14 -08005294 Log.w(LOG_TAG, "getImsProvisioningString: ImsService is not available for sub '"
5295 + subId + "' for key:" + key);
5296 return ProvisioningManager.STRING_QUERY_RESULT_ERROR_NOT_READY;
Brad Ebingerdf5b4f02018-10-31 11:24:17 -07005297 } finally {
5298 Binder.restoreCallingIdentity(identity);
5299 }
5300 }
5301
5302 @Override
5303 public int setImsProvisioningInt(int subId, int key, int value) {
Brad Ebinger1c8542e2019-01-14 13:43:14 -08005304 if (!SubscriptionManager.isValidSubscriptionId(subId)) {
5305 throw new IllegalArgumentException("Invalid Subscription id '" + subId + "'");
5306 }
Brad Ebinger3d0b34e2018-11-15 14:13:12 -08005307 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(mApp, subId,
5308 "setImsProvisioningInt");
joonhunshincffb7fc2021-11-28 07:32:01 +00005309
Brad Ebingerdf5b4f02018-10-31 11:24:17 -07005310 final long identity = Binder.clearCallingIdentity();
5311 try {
5312 // TODO: Refactor to remove ImsManager dependence and query through ImsPhone directly.
Brad Ebinger1c8542e2019-01-14 13:43:14 -08005313 int slotId = getSlotIndex(subId);
5314 if (slotId <= SubscriptionManager.INVALID_SIM_SLOT_INDEX) {
5315 Log.w(LOG_TAG, "setImsProvisioningInt: called with an inactive subscription id '"
5316 + subId + "' for key:" + key);
5317 return ImsConfigImplBase.CONFIG_RESULT_FAILED;
5318 }
joonhunshincffb7fc2021-11-28 07:32:01 +00005319
joonhunshin91bc1952022-04-29 08:47:15 +00005320 ImsProvisioningController controller = ImsProvisioningController.getInstance();
5321 if (controller == null) {
5322 loge("setImsProvisioningInt: Device does not support IMS");
5323
5324 // device does not support IMS, this method will return CONFIG_RESULT_FAILED.
5325 return ImsConfigImplBase.CONFIG_RESULT_FAILED;
5326 }
5327 int retVal = controller.setProvisioningValue(subId, key, value);
joonhunshincffb7fc2021-11-28 07:32:01 +00005328 if (retVal != ImsConfigImplBase.CONFIG_RESULT_UNKNOWN) {
5329 return retVal;
5330 }
5331
calvinpanb5a34062021-02-08 19:59:36 +08005332 return ImsManager.getInstance(mApp, slotId).setConfig(key, value);
5333 } catch (com.android.ims.ImsException | RemoteException e) {
Brad Ebinger1c8542e2019-01-14 13:43:14 -08005334 Log.w(LOG_TAG, "setImsProvisioningInt: ImsService unavailable for sub '" + subId
calvinpanb5a34062021-02-08 19:59:36 +08005335 + "' for key:" + key, e);
Brad Ebinger1c8542e2019-01-14 13:43:14 -08005336 return ImsConfigImplBase.CONFIG_RESULT_FAILED;
Brad Ebingerdf5b4f02018-10-31 11:24:17 -07005337 } finally {
5338 Binder.restoreCallingIdentity(identity);
5339 }
5340 }
5341
5342 @Override
5343 public int setImsProvisioningString(int subId, int key, String value) {
Brad Ebinger1c8542e2019-01-14 13:43:14 -08005344 if (!SubscriptionManager.isValidSubscriptionId(subId)) {
5345 throw new IllegalArgumentException("Invalid Subscription id '" + subId + "'");
5346 }
Brad Ebinger3d0b34e2018-11-15 14:13:12 -08005347 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(mApp, subId,
5348 "setImsProvisioningString");
Brad Ebingerdf5b4f02018-10-31 11:24:17 -07005349 final long identity = Binder.clearCallingIdentity();
5350 try {
5351 // TODO: Refactor to remove ImsManager dependence and query through ImsPhone directly.
Brad Ebinger1c8542e2019-01-14 13:43:14 -08005352 int slotId = getSlotIndex(subId);
5353 if (slotId <= SubscriptionManager.INVALID_SIM_SLOT_INDEX) {
5354 Log.w(LOG_TAG, "setImsProvisioningString: called with an inactive subscription id '"
5355 + subId + "' for key:" + key);
5356 return ImsConfigImplBase.CONFIG_RESULT_FAILED;
5357 }
calvinpanb5a34062021-02-08 19:59:36 +08005358 return ImsManager.getInstance(mApp, slotId).setConfig(key, value);
5359 } catch (com.android.ims.ImsException | RemoteException e) {
Brad Ebinger1c8542e2019-01-14 13:43:14 -08005360 Log.w(LOG_TAG, "setImsProvisioningString: ImsService unavailable for sub '" + subId
calvinpanb5a34062021-02-08 19:59:36 +08005361 + "' for key:" + key, e);
Brad Ebinger1c8542e2019-01-14 13:43:14 -08005362 return ImsConfigImplBase.CONFIG_RESULT_FAILED;
Brad Ebingerdf5b4f02018-10-31 11:24:17 -07005363 } finally {
5364 Binder.restoreCallingIdentity(identity);
5365 }
5366 }
5367
Brad Ebinger919631e2021-06-02 17:46:35 -07005368 /**
5369 * Throw an ImsException if the IMS resolver does not have an ImsService configured for MMTEL
5370 * for the given slot ID or no ImsResolver instance has been created.
5371 * @param slotId The slot ID that the IMS service is created for.
5372 * @throws ImsException If there is no ImsService configured for this slot.
5373 */
5374 private void verifyImsMmTelConfiguredOrThrow(int slotId) throws ImsException {
5375 if (mImsResolver == null || !mImsResolver.isImsServiceConfiguredForFeature(slotId,
5376 ImsFeature.FEATURE_MMTEL)) {
5377 throw new ImsException("This subscription does not support MMTEL over IMS",
5378 ImsException.CODE_ERROR_UNSUPPORTED_OPERATION);
5379 }
5380 }
5381
Brad Ebinger1ce9c432019-07-16 13:19:44 -07005382 private int getSlotIndexOrException(int subId) throws ImsException {
Brad Ebinger35c841c2018-10-01 10:40:55 -07005383 int slotId = SubscriptionManager.getSlotIndex(subId);
5384 if (!SubscriptionManager.isValidSlotIndex(slotId)) {
Brad Ebinger1ce9c432019-07-16 13:19:44 -07005385 throw new ImsException("Invalid Subscription Id, subId=" + subId,
5386 ImsException.CODE_ERROR_INVALID_SUBSCRIPTION);
Brad Ebinger35c841c2018-10-01 10:40:55 -07005387 }
5388 return slotId;
5389 }
5390
Brad Ebinger1c8542e2019-01-14 13:43:14 -08005391 private int getSlotIndex(int subId) {
5392 int slotId = SubscriptionManager.getSlotIndex(subId);
5393 if (!SubscriptionManager.isValidSlotIndex(slotId)) {
5394 return SubscriptionManager.INVALID_SIM_SLOT_INDEX;
5395 }
5396 return slotId;
5397 }
5398
Wink Saville36469e72014-06-11 15:17:00 -07005399 /**
Nathan Harold9042f0b2019-05-21 15:51:27 -07005400 * Returns the data network type for a subId; does not throw SecurityException.
Wink Saville36469e72014-06-11 15:17:00 -07005401 */
5402 @Override
Philip P. Moltmann700a9592019-10-03 11:53:50 -07005403 public int getNetworkTypeForSubscriber(int subId, String callingPackage,
5404 String callingFeatureId) {
Shuo Qian13d89152021-05-10 23:58:11 -07005405 try {
5406 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
5407 } catch (SecurityException se) {
5408 EventLog.writeEvent(0x534e4554, "186776740", Binder.getCallingUid());
5409 throw new SecurityException("Package " + callingPackage + " does not belong to "
5410 + Binder.getCallingUid());
5411 }
Nathan Haroldf096d982020-11-18 17:18:06 -08005412 final int targetSdk = TelephonyPermissions.getTargetSdk(mApp, callingPackage);
Nathan Haroldef60dba2019-05-22 13:55:14 -07005413 if (targetSdk > android.os.Build.VERSION_CODES.Q) {
Philip P. Moltmann700a9592019-10-03 11:53:50 -07005414 return getDataNetworkTypeForSubscriber(subId, callingPackage, callingFeatureId);
Nathan Haroldef60dba2019-05-22 13:55:14 -07005415 } else if (targetSdk == android.os.Build.VERSION_CODES.Q
Nathan Harold9042f0b2019-05-21 15:51:27 -07005416 && !TelephonyPermissions.checkCallingOrSelfReadPhoneStateNoThrow(
Philip P. Moltmann700a9592019-10-03 11:53:50 -07005417 mApp, subId, callingPackage, callingFeatureId,
5418 "getNetworkTypeForSubscriber")) {
Robert Greenwalt36b23af2015-07-06 17:59:14 -07005419 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
5420 }
Robert Greenwalta5dcfcb2015-07-10 09:06:29 -07005421
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005422 final long identity = Binder.clearCallingIdentity();
5423 try {
5424 final Phone phone = getPhone(subId);
5425 if (phone != null) {
5426 return phone.getServiceState().getDataNetworkType();
5427 } else {
5428 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
5429 }
5430 } finally {
5431 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07005432 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07005433 }
5434
5435 /**
5436 * Returns the data network type
5437 */
5438 @Override
Philip P. Moltmann700a9592019-10-03 11:53:50 -07005439 public int getDataNetworkType(String callingPackage, String callingFeatureId) {
Jack Yu285100e2022-12-02 22:48:35 -08005440 return getDataNetworkTypeForSubscriber(SubscriptionManager.getDefaultDataSubscriptionId(),
Zoey Chenfd61f7f2021-04-21 13:42:10 +08005441 callingPackage, callingFeatureId);
Wink Saville36469e72014-06-11 15:17:00 -07005442 }
5443
5444 /**
5445 * Returns the data network type for a subId
5446 */
5447 @Override
Philip P. Moltmann700a9592019-10-03 11:53:50 -07005448 public int getDataNetworkTypeForSubscriber(int subId, String callingPackage,
5449 String callingFeatureId) {
Sooraj Sasindran5d051bf2021-11-05 13:14:15 -07005450 String functionName = "getDataNetworkTypeForSubscriber";
5451 if (!TelephonyPermissions.checkCallingOrSelfReadNonDangerousPhoneStateNoThrow(
5452 mApp, functionName)) {
5453 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
5454 mApp, subId, callingPackage, callingFeatureId, functionName)) {
5455 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
5456 }
Robert Greenwalt36b23af2015-07-06 17:59:14 -07005457 }
5458
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005459 final long identity = Binder.clearCallingIdentity();
5460 try {
5461 final Phone phone = getPhone(subId);
5462 if (phone != null) {
5463 return phone.getServiceState().getDataNetworkType();
5464 } else {
5465 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
5466 }
5467 } finally {
5468 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07005469 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07005470 }
5471
5472 /**
Wink Saville36469e72014-06-11 15:17:00 -07005473 * Returns the Voice network type for a subId
5474 */
5475 @Override
Philip P. Moltmann700a9592019-10-03 11:53:50 -07005476 public int getVoiceNetworkTypeForSubscriber(int subId, String callingPackage,
5477 String callingFeatureId) {
Sooraj Sasindran5d051bf2021-11-05 13:14:15 -07005478 String functionName = "getVoiceNetworkTypeForSubscriber";
5479 if (!TelephonyPermissions.checkCallingOrSelfReadNonDangerousPhoneStateNoThrow(
5480 mApp, functionName)) {
5481 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
5482 mApp, subId, callingPackage, callingFeatureId, functionName)) {
5483 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
5484 }
Robert Greenwalta5dcfcb2015-07-10 09:06:29 -07005485 }
5486
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005487 final long identity = Binder.clearCallingIdentity();
5488 try {
5489 final Phone phone = getPhone(subId);
5490 if (phone != null) {
5491 return phone.getServiceState().getVoiceNetworkType();
5492 } else {
5493 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
5494 }
5495 } finally {
5496 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07005497 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07005498 }
5499
5500 /**
5501 * @return true if a ICC card is present
5502 */
5503 public boolean hasIccCard() {
Wink Saville36469e72014-06-11 15:17:00 -07005504 // FIXME Make changes to pass defaultSimId of type int
Jack Yu285100e2022-12-02 22:48:35 -08005505 return hasIccCardUsingSlotIndex(SubscriptionManager.getSlotIndex(
Sanket Padawe13bac7b2017-03-20 15:04:47 -07005506 getDefaultSubscription()));
Wink Saville36469e72014-06-11 15:17:00 -07005507 }
5508
5509 /**
Sanket Padawe13bac7b2017-03-20 15:04:47 -07005510 * @return true if a ICC card is present for a slotIndex
Wink Saville36469e72014-06-11 15:17:00 -07005511 */
Sanket Padawe356d7632015-06-22 14:03:32 -07005512 @Override
Sanket Padawe13bac7b2017-03-20 15:04:47 -07005513 public boolean hasIccCardUsingSlotIndex(int slotIndex) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005514 final long identity = Binder.clearCallingIdentity();
5515 try {
5516 final Phone phone = PhoneFactory.getPhone(slotIndex);
5517 if (phone != null) {
5518 return phone.getIccCard().hasIccCard();
5519 } else {
5520 return false;
5521 }
5522 } finally {
5523 Binder.restoreCallingIdentity(identity);
Amit Mahajana6fc2a82015-01-06 11:53:51 -08005524 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07005525 }
5526
5527 /**
5528 * Return if the current radio is LTE on CDMA. This
5529 * is a tri-state return value as for a period of time
5530 * the mode may be unknown.
5531 *
Robert Greenwalt36b23af2015-07-06 17:59:14 -07005532 * @param callingPackage the name of the package making the call.
Santos Cordon7d4ddf62013-07-10 11:58:08 -07005533 * @return {@link Phone#LTE_ON_CDMA_UNKNOWN}, {@link Phone#LTE_ON_CDMA_FALSE}
Jake Hambye994d462014-02-03 13:10:13 -08005534 * or {@link Phone#LTE_ON_CDMA_TRUE}
Santos Cordon7d4ddf62013-07-10 11:58:08 -07005535 */
Robert Greenwalt36b23af2015-07-06 17:59:14 -07005536 @Override
Philip P. Moltmann700a9592019-10-03 11:53:50 -07005537 public int getLteOnCdmaMode(String callingPackage, String callingFeatureId) {
5538 return getLteOnCdmaModeForSubscriber(getDefaultSubscription(), callingPackage,
5539 callingFeatureId);
Wink Saville36469e72014-06-11 15:17:00 -07005540 }
5541
Sanket Padawe356d7632015-06-22 14:03:32 -07005542 @Override
Philip P. Moltmann700a9592019-10-03 11:53:50 -07005543 public int getLteOnCdmaModeForSubscriber(int subId, String callingPackage,
5544 String callingFeatureId) {
Sarah Chin790d2922020-01-16 12:17:23 -08005545 try {
5546 enforceReadPrivilegedPermission("getLteOnCdmaModeForSubscriber");
5547 } catch (SecurityException e) {
Robert Greenwalt36b23af2015-07-06 17:59:14 -07005548 return PhoneConstants.LTE_ON_CDMA_UNKNOWN;
5549 }
5550
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005551 final long identity = Binder.clearCallingIdentity();
5552 try {
5553 final Phone phone = getPhone(subId);
5554 if (phone == null) {
5555 return PhoneConstants.LTE_ON_CDMA_UNKNOWN;
5556 } else {
Nathan Harold05ad6332020-07-10 11:54:36 -07005557 return TelephonyProperties.lte_on_cdma_device()
5558 .orElse(PhoneConstants.LTE_ON_CDMA_FALSE);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005559 }
5560 } finally {
5561 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07005562 }
Wink Saville36469e72014-06-11 15:17:00 -07005563 }
5564
Wink Saville36469e72014-06-11 15:17:00 -07005565 /**
5566 * {@hide}
5567 * Returns Default subId, 0 in the case of single standby.
5568 */
Wink Savilleb564aae2014-10-23 10:18:09 -07005569 private int getDefaultSubscription() {
Jack Yu285100e2022-12-02 22:48:35 -08005570 return SubscriptionManager.getDefaultSubscriptionId();
Wink Saville36469e72014-06-11 15:17:00 -07005571 }
5572
Shishir Agrawala9f32182016-04-12 12:00:16 -07005573 private int getSlotForDefaultSubscription() {
Jack Yu285100e2022-12-02 22:48:35 -08005574 return SubscriptionManager.getPhoneId(getDefaultSubscription());
Shishir Agrawala9f32182016-04-12 12:00:16 -07005575 }
5576
Wink Savilleb564aae2014-10-23 10:18:09 -07005577 private int getPreferredVoiceSubscription() {
Jack Yu285100e2022-12-02 22:48:35 -08005578 return SubscriptionManager.getDefaultVoiceSubscriptionId();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07005579 }
Ihab Awadf2177b72013-11-25 13:33:23 -08005580
Pengquan Menge92a50d2018-09-21 15:54:48 -07005581 private boolean isActiveSubscription(int subId) {
Jack Yu285100e2022-12-02 22:48:35 -08005582 if (PhoneFactory.isSubscriptionManagerServiceEnabled()) {
5583 return SubscriptionManagerService.getInstance().isActiveSubId(subId,
5584 mApp.getOpPackageName(), mApp.getFeatureId());
5585 }
Pengquan Menge92a50d2018-09-21 15:54:48 -07005586 return mSubscriptionController.isActiveSubId(subId);
5587 }
5588
Ihab Awadf2177b72013-11-25 13:33:23 -08005589 /**
5590 * @see android.telephony.TelephonyManager.WifiCallingChoices
5591 */
5592 public int getWhenToMakeWifiCalls() {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005593 final long identity = Binder.clearCallingIdentity();
5594 try {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08005595 return Settings.System.getInt(mApp.getContentResolver(),
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005596 Settings.System.WHEN_TO_MAKE_WIFI_CALLS,
5597 getWhenToMakeWifiCallsDefaultPreference());
5598 } finally {
5599 Binder.restoreCallingIdentity(identity);
5600 }
Ihab Awadf2177b72013-11-25 13:33:23 -08005601 }
5602
5603 /**
5604 * @see android.telephony.TelephonyManager.WifiCallingChoices
5605 */
5606 public void setWhenToMakeWifiCalls(int preference) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005607 final long identity = Binder.clearCallingIdentity();
5608 try {
5609 if (DBG) log("setWhenToMakeWifiCallsStr, storing setting = " + preference);
Nazanin Bakhshif782e562018-12-11 15:15:39 -08005610 Settings.System.putInt(mApp.getContentResolver(),
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005611 Settings.System.WHEN_TO_MAKE_WIFI_CALLS, preference);
5612 } finally {
5613 Binder.restoreCallingIdentity(identity);
5614 }
Ihab Awadf9e92732013-12-05 18:02:52 -08005615 }
5616
Sailesh Nepald1e68152013-12-12 19:08:02 -08005617 private static int getWhenToMakeWifiCallsDefaultPreference() {
Santos Cordonda120f42014-08-06 04:44:34 -07005618 // TODO: Use a build property to choose this value.
Evan Charlton9829e882013-12-19 15:30:38 -08005619 return TelephonyManager.WifiCallingChoices.ALWAYS_USE;
Ihab Awadf2177b72013-11-25 13:33:23 -08005620 }
Shishir Agrawal69f68122013-12-16 17:25:49 -08005621
Muralidhar Reddybd38d952021-12-02 21:04:16 +00005622 private Phone getPhoneFromSlotPortIndexOrThrowException(int slotIndex, int portIndex) {
5623 int phoneId = UiccController.getInstance().getPhoneIdFromSlotPortIndex(slotIndex,
5624 portIndex);
Jordan Liu4c733742019-02-28 12:03:40 -08005625 if (phoneId == -1) {
Muralidhar Reddybd38d952021-12-02 21:04:16 +00005626 throw new IllegalArgumentException("Given slot index: " + slotIndex + " port index: "
5627 + portIndex + " does not correspond to an active phone");
Jordan Liu4c733742019-02-28 12:03:40 -08005628 }
5629 return PhoneFactory.getPhone(phoneId);
5630 }
5631
Shishir Agrawal566b7612013-10-28 14:41:00 -07005632 @Override
Derek Tan740e1672017-06-27 14:56:27 -07005633 public IccOpenLogicalChannelResponse iccOpenLogicalChannel(
Rambo Wanga1782702021-11-10 20:15:19 -08005634 @NonNull IccLogicalChannelRequest request) {
5635 Phone phone = getPhoneFromValidIccLogicalChannelRequest(request,
5636 /*message=*/ "iccOpenLogicalChannel");
5637
5638 if (DBG) log("iccOpenLogicalChannel: request=" + request);
5639 // Verify that the callingPackage in the request belongs to the calling UID
5640 mAppOps.checkPackage(Binder.getCallingUid(), request.callingPackage);
5641
5642 return iccOpenLogicalChannelWithPermission(phone, request);
Jordan Liu4c733742019-02-28 12:03:40 -08005643 }
Shishir Agrawal566b7612013-10-28 14:41:00 -07005644
Rambo Wanga1782702021-11-10 20:15:19 -08005645 private Phone getPhoneFromValidIccLogicalChannelRequest(
5646 @NonNull IccLogicalChannelRequest request, String message) {
5647 Phone phone;
5648 if (request.subId != SubscriptionManager.INVALID_SUBSCRIPTION_ID) {
5649 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
5650 mApp, request.subId, message);
5651 phone = getPhoneFromSubId(request.subId);
5652 } else if (request.slotIndex != SubscriptionManager.INVALID_SIM_SLOT_INDEX) {
5653 enforceModifyPermission();
5654 phone = getPhoneFromSlotPortIndexOrThrowException(request.slotIndex, request.portIndex);
5655 } else {
5656 throw new IllegalArgumentException("Both subId and slotIndex in request are invalid.");
Jordan Liu4c733742019-02-28 12:03:40 -08005657 }
Rambo Wanga1782702021-11-10 20:15:19 -08005658 return phone;
Jordan Liu4c733742019-02-28 12:03:40 -08005659 }
5660
5661 private IccOpenLogicalChannelResponse iccOpenLogicalChannelWithPermission(Phone phone,
Rambo Wanga1782702021-11-10 20:15:19 -08005662 IccLogicalChannelRequest channelRequest) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005663 final long identity = Binder.clearCallingIdentity();
5664 try {
Rambo Wanga1782702021-11-10 20:15:19 -08005665 if (TextUtils.equals(ISDR_AID, channelRequest.aid)) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005666 // Only allows LPA to open logical channel to ISD-R.
Nazanin Bakhshif782e562018-12-11 15:15:39 -08005667 ComponentInfo bestComponent = EuiccConnector.findBestComponent(getDefaultPhone()
5668 .getContext().getPackageManager());
Rambo Wanga1782702021-11-10 20:15:19 -08005669 if (bestComponent == null || !TextUtils.equals(channelRequest.callingPackage,
5670 bestComponent.packageName)) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005671 loge("The calling package is not allowed to access ISD-R.");
5672 throw new SecurityException(
5673 "The calling package is not allowed to access ISD-R.");
5674 }
Derek Tan740e1672017-06-27 14:56:27 -07005675 }
Derek Tan740e1672017-06-27 14:56:27 -07005676
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005677 IccOpenLogicalChannelResponse response = (IccOpenLogicalChannelResponse) sendRequest(
Rambo Wanga1782702021-11-10 20:15:19 -08005678 CMD_OPEN_CHANNEL, channelRequest, phone, null /* workSource */);
5679 if (DBG) log("iccOpenLogicalChannelWithPermission: response=" + response);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005680 return response;
5681 } finally {
5682 Binder.restoreCallingIdentity(identity);
5683 }
Shishir Agrawal566b7612013-10-28 14:41:00 -07005684 }
5685
5686 @Override
Rambo Wanga1782702021-11-10 20:15:19 -08005687 public boolean iccCloseLogicalChannel(@NonNull IccLogicalChannelRequest request) {
5688 Phone phone = getPhoneFromValidIccLogicalChannelRequest(request,
5689 /*message=*/"iccCloseLogicalChannel");
5690
5691 if (DBG) log("iccCloseLogicalChannel: request=" + request);
5692
5693 return iccCloseLogicalChannelWithPermission(phone, request);
Jordan Liu4c733742019-02-28 12:03:40 -08005694 }
Shishir Agrawal566b7612013-10-28 14:41:00 -07005695
Rambo Wanga1782702021-11-10 20:15:19 -08005696 private boolean iccCloseLogicalChannelWithPermission(Phone phone,
5697 IccLogicalChannelRequest request) {
Chen Xua8f0dff2022-02-12 00:34:15 -08005698 // before this feature is enabled, this API should only return false if
5699 // the operation fails instead of throwing runtime exception for
5700 // backward-compatibility.
5701 final boolean shouldThrowExceptionOnFailure = CompatChanges.isChangeEnabled(
5702 ICC_CLOSE_CHANNEL_EXCEPTION_ON_FAILURE, Binder.getCallingUid());
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005703 final long identity = Binder.clearCallingIdentity();
5704 try {
Rambo Wanga1782702021-11-10 20:15:19 -08005705 if (request.channel < 0) {
Chen Xu540470b2021-12-14 17:15:47 -08005706 throw new IllegalArgumentException("request.channel is less than 0");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005707 }
Chen Xue9d737e2022-01-01 23:41:31 -08005708 Object result = sendRequest(CMD_CLOSE_CHANNEL, request.channel, phone,
Jordan Liu4c733742019-02-28 12:03:40 -08005709 null /* workSource */);
Chen Xue9d737e2022-01-01 23:41:31 -08005710 Boolean success = false;
5711 if (result instanceof RuntimeException) {
5712 // if there is an exception returned, throw from the binder thread here.
Chen Xua8f0dff2022-02-12 00:34:15 -08005713 if (shouldThrowExceptionOnFailure) {
5714 throw (RuntimeException) result;
5715 } else {
5716 return false;
5717 }
Chen Xue9d737e2022-01-01 23:41:31 -08005718 } else if (result instanceof Boolean) {
5719 success = (Boolean) result;
5720 } else {
5721 loge("iccCloseLogicalChannelWithPermission: supported return type " + result);
5722 }
Rambo Wanga1782702021-11-10 20:15:19 -08005723 if (DBG) log("iccCloseLogicalChannelWithPermission: success=" + success);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005724 return success;
5725 } finally {
5726 Binder.restoreCallingIdentity(identity);
Shishir Agrawal566b7612013-10-28 14:41:00 -07005727 }
Shishir Agrawal566b7612013-10-28 14:41:00 -07005728 }
5729
5730 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08005731 public String iccTransmitApduLogicalChannel(int subId, int channel, int cla,
Shishir Agrawal566b7612013-10-28 14:41:00 -07005732 int command, int p1, int p2, int p3, String data) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08005733 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
5734 mApp, subId, "iccTransmitApduLogicalChannel");
Jordan Liu4c733742019-02-28 12:03:40 -08005735 if (DBG) {
5736 log("iccTransmitApduLogicalChannel: subId=" + subId + " chnl=" + channel
5737 + " cla=" + cla + " cmd=" + command + " p1=" + p1 + " p2=" + p2 + " p3="
5738 + p3 + " data=" + data);
5739 }
5740 return iccTransmitApduLogicalChannelWithPermission(getPhoneFromSubId(subId), channel, cla,
5741 command, p1, p2, p3, data);
5742 }
Shishir Agrawal566b7612013-10-28 14:41:00 -07005743
Jordan Liu4c733742019-02-28 12:03:40 -08005744 @Override
Muralidhar Reddybd38d952021-12-02 21:04:16 +00005745 public String iccTransmitApduLogicalChannelByPort(int slotIndex, int portIndex, int channel,
5746 int cla, int command, int p1, int p2, int p3, String data) {
Jordan Liu4c733742019-02-28 12:03:40 -08005747 enforceModifyPermission();
5748 if (DBG) {
Muralidhar Reddybd38d952021-12-02 21:04:16 +00005749 log("iccTransmitApduLogicalChannelByPort: slotIndex=" + slotIndex + " portIndex="
5750 + portIndex + " chnl=" + channel + " cla=" + cla + " cmd=" + command + " p1="
5751 + p1 + " p2=" + p2 + " p3=" + p3 + " data=" + data);
Jordan Liu4c733742019-02-28 12:03:40 -08005752 }
5753 return iccTransmitApduLogicalChannelWithPermission(
Muralidhar Reddybd38d952021-12-02 21:04:16 +00005754 getPhoneFromSlotPortIndexOrThrowException(slotIndex, portIndex), channel, cla,
5755 command, p1, p2, p3, data);
Jordan Liu4c733742019-02-28 12:03:40 -08005756 }
5757
5758 private String iccTransmitApduLogicalChannelWithPermission(Phone phone, int channel, int cla,
5759 int command, int p1, int p2, int p3, String data) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005760 final long identity = Binder.clearCallingIdentity();
5761 try {
Hall Liu4fd771b2019-05-02 09:16:29 -07005762 if (channel <= 0) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005763 return "";
5764 }
Shishir Agrawal566b7612013-10-28 14:41:00 -07005765
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005766 IccIoResult response = (IccIoResult) sendRequest(CMD_TRANSMIT_APDU_LOGICAL_CHANNEL,
Jordan Liu4c733742019-02-28 12:03:40 -08005767 new IccAPDUArgument(channel, cla, command, p1, p2, p3, data), phone,
5768 null /* workSource */);
5769 if (DBG) log("iccTransmitApduLogicalChannelWithPermission: " + response);
Shishir Agrawal566b7612013-10-28 14:41:00 -07005770
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005771 // Append the returned status code to the end of the response payload.
5772 String s = Integer.toHexString(
5773 (response.sw1 << 8) + response.sw2 + 0x10000).substring(1);
5774 if (response.payload != null) {
5775 s = IccUtils.bytesToHexString(response.payload) + s;
5776 }
5777 return s;
5778 } finally {
5779 Binder.restoreCallingIdentity(identity);
Shishir Agrawal5ec14172014-08-05 17:05:45 -07005780 }
Shishir Agrawal566b7612013-10-28 14:41:00 -07005781 }
Jake Hambye994d462014-02-03 13:10:13 -08005782
Evan Charltonc66da362014-05-16 14:06:40 -07005783 @Override
Holly Jiuyu Sun1cc2d552018-01-26 15:51:16 -08005784 public String iccTransmitApduBasicChannel(int subId, String callingPackage, int cla,
5785 int command, int p1, int p2, int p3, String data) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08005786 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
5787 mApp, subId, "iccTransmitApduBasicChannel");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005788 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
Jordan Liu4c733742019-02-28 12:03:40 -08005789 if (DBG) {
5790 log("iccTransmitApduBasicChannel: subId=" + subId + " cla=" + cla + " cmd="
5791 + command + " p1=" + p1 + " p2=" + p2 + " p3=" + p3 + " data=" + data);
5792 }
5793 return iccTransmitApduBasicChannelWithPermission(getPhoneFromSubId(subId), callingPackage,
5794 cla, command, p1, p2, p3, data);
5795 }
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07005796
Jordan Liu4c733742019-02-28 12:03:40 -08005797 @Override
Muralidhar Reddybd38d952021-12-02 21:04:16 +00005798 public String iccTransmitApduBasicChannelByPort(int slotIndex, int portIndex,
5799 String callingPackage, int cla, int command, int p1, int p2, int p3, String data) {
Jordan Liu4c733742019-02-28 12:03:40 -08005800 enforceModifyPermission();
5801 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
5802 if (DBG) {
Muralidhar Reddybd38d952021-12-02 21:04:16 +00005803 log("iccTransmitApduBasicChannelByPort: slotIndex=" + slotIndex + " portIndex="
5804 + portIndex + " cla=" + cla + " cmd=" + command + " p1=" + p1 + " p2="
5805 + p2 + " p3=" + p3 + " data=" + data);
Jordan Liu4c733742019-02-28 12:03:40 -08005806 }
5807
5808 return iccTransmitApduBasicChannelWithPermission(
Muralidhar Reddybd38d952021-12-02 21:04:16 +00005809 getPhoneFromSlotPortIndexOrThrowException(slotIndex, portIndex), callingPackage,
5810 cla, command, p1, p2, p3, data);
Jordan Liu4c733742019-02-28 12:03:40 -08005811 }
5812
5813 // open APDU basic channel assuming the caller has sufficient permissions
5814 private String iccTransmitApduBasicChannelWithPermission(Phone phone, String callingPackage,
5815 int cla, int command, int p1, int p2, int p3, String data) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005816 final long identity = Binder.clearCallingIdentity();
5817 try {
5818 if (command == SELECT_COMMAND && p1 == SELECT_P1 && p2 == SELECT_P2 && p3 == SELECT_P3
5819 && TextUtils.equals(ISDR_AID, data)) {
5820 // Only allows LPA to select ISD-R.
Nazanin Bakhshif782e562018-12-11 15:15:39 -08005821 ComponentInfo bestComponent = EuiccConnector.findBestComponent(getDefaultPhone()
5822 .getContext().getPackageManager());
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005823 if (bestComponent == null
5824 || !TextUtils.equals(callingPackage, bestComponent.packageName)) {
5825 loge("The calling package is not allowed to select ISD-R.");
5826 throw new SecurityException(
5827 "The calling package is not allowed to select ISD-R.");
5828 }
Holly Jiuyu Sun1cc2d552018-01-26 15:51:16 -08005829 }
Holly Jiuyu Sun1cc2d552018-01-26 15:51:16 -08005830
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005831 IccIoResult response = (IccIoResult) sendRequest(CMD_TRANSMIT_APDU_BASIC_CHANNEL,
Jordan Liu4c733742019-02-28 12:03:40 -08005832 new IccAPDUArgument(0, cla, command, p1, p2, p3, data), phone,
5833 null /* workSource */);
5834 if (DBG) log("iccTransmitApduBasicChannelWithPermission: " + response);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07005835
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005836 // Append the returned status code to the end of the response payload.
5837 String s = Integer.toHexString(
5838 (response.sw1 << 8) + response.sw2 + 0x10000).substring(1);
5839 if (response.payload != null) {
5840 s = IccUtils.bytesToHexString(response.payload) + s;
5841 }
5842 return s;
5843 } finally {
5844 Binder.restoreCallingIdentity(identity);
Shishir Agrawal5ec14172014-08-05 17:05:45 -07005845 }
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07005846 }
5847
5848 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08005849 public byte[] iccExchangeSimIO(int subId, int fileID, int command, int p1, int p2, int p3,
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07005850 String filePath) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08005851 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
5852 mApp, subId, "iccExchangeSimIO");
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07005853
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005854 final long identity = Binder.clearCallingIdentity();
5855 try {
5856 if (DBG) {
5857 log("Exchange SIM_IO " + subId + ":" + fileID + ":" + command + " "
5858 + p1 + " " + p2 + " " + p3 + ":" + filePath);
5859 }
5860
5861 IccIoResult response =
5862 (IccIoResult) sendRequest(CMD_EXCHANGE_SIM_IO,
5863 new IccAPDUArgument(-1, fileID, command, p1, p2, p3, filePath),
5864 subId);
5865
5866 if (DBG) {
5867 log("Exchange SIM_IO [R]" + response);
5868 }
5869
5870 byte[] result = null;
5871 int length = 2;
5872 if (response.payload != null) {
5873 length = 2 + response.payload.length;
5874 result = new byte[length];
5875 System.arraycopy(response.payload, 0, result, 0, response.payload.length);
5876 } else {
5877 result = new byte[length];
5878 }
5879
5880 result[length - 1] = (byte) response.sw2;
5881 result[length - 2] = (byte) response.sw1;
5882 return result;
5883 } finally {
5884 Binder.restoreCallingIdentity(identity);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07005885 }
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07005886 }
5887
Nathan Haroldb3014052017-01-25 15:57:32 -08005888 /**
5889 * Get the forbidden PLMN List from the given app type (ex APPTYPE_USIM)
5890 * on a particular subscription
5891 */
Philip P. Moltmann700a9592019-10-03 11:53:50 -07005892 public String[] getForbiddenPlmns(int subId, int appType, String callingPackage,
5893 String callingFeatureId) {
sqianb6e41952018-03-12 14:54:01 -07005894 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
Philip P. Moltmann700a9592019-10-03 11:53:50 -07005895 mApp, subId, callingPackage, callingFeatureId, "getForbiddenPlmns")) {
sqianb6e41952018-03-12 14:54:01 -07005896 return null;
5897 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005898
5899 final long identity = Binder.clearCallingIdentity();
5900 try {
5901 if (appType != TelephonyManager.APPTYPE_USIM
5902 && appType != TelephonyManager.APPTYPE_SIM) {
5903 loge("getForbiddenPlmnList(): App Type must be USIM or SIM");
5904 return null;
5905 }
5906 Object response = sendRequest(
5907 CMD_GET_FORBIDDEN_PLMNS, new Integer(appType), subId);
5908 if (response instanceof String[]) {
5909 return (String[]) response;
5910 }
yincheng zhao2737e882019-09-06 17:06:54 -07005911 // Response is an Exception of some kind
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005912 // which is signalled to the user as a NULL retval
Nathan Haroldb3014052017-01-25 15:57:32 -08005913 return null;
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005914 } finally {
5915 Binder.restoreCallingIdentity(identity);
Nathan Haroldb3014052017-01-25 15:57:32 -08005916 }
Nathan Haroldb3014052017-01-25 15:57:32 -08005917 }
5918
yincheng zhao2737e882019-09-06 17:06:54 -07005919 /**
5920 * Set the forbidden PLMN list from the given app type (ex APPTYPE_USIM) on a particular
5921 * subscription.
5922 *
5923 * @param subId the id of the subscription.
5924 * @param appType the uicc app type, must be USIM or SIM.
5925 * @param fplmns the Forbiden plmns list that needed to be written to the SIM.
5926 * @param callingPackage the op Package name.
Philip P. Moltmann700a9592019-10-03 11:53:50 -07005927 * @param callingFeatureId the feature in the package.
yincheng zhao2737e882019-09-06 17:06:54 -07005928 * @return number of fplmns that is successfully written to the SIM.
5929 */
Philip P. Moltmann700a9592019-10-03 11:53:50 -07005930 public int setForbiddenPlmns(int subId, int appType, List<String> fplmns, String callingPackage,
5931 String callingFeatureId) {
Jayachandran C5b0d75a2021-10-21 22:15:27 -07005932 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
5933 mApp, subId, "setForbiddenPlmns");
5934
yincheng zhao2737e882019-09-06 17:06:54 -07005935 if (appType != TelephonyManager.APPTYPE_USIM && appType != TelephonyManager.APPTYPE_SIM) {
5936 loge("setForbiddenPlmnList(): App Type must be USIM or SIM");
5937 throw new IllegalArgumentException("Invalid appType: App Type must be USIM or SIM");
5938 }
5939 if (fplmns == null) {
5940 throw new IllegalArgumentException("Fplmn List provided is null");
5941 }
5942 for (String fplmn : fplmns) {
5943 if (!CellIdentity.isValidPlmn(fplmn)) {
5944 throw new IllegalArgumentException("Invalid fplmn provided: " + fplmn);
5945 }
5946 }
5947 final long identity = Binder.clearCallingIdentity();
5948 try {
5949 Object response = sendRequest(
5950 CMD_SET_FORBIDDEN_PLMNS,
5951 new Pair<Integer, List<String>>(new Integer(appType), fplmns),
5952 subId);
5953 return (int) response;
5954 } finally {
5955 Binder.restoreCallingIdentity(identity);
5956 }
5957 }
5958
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07005959 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08005960 public String sendEnvelopeWithStatus(int subId, String content) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08005961 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
5962 mApp, subId, "sendEnvelopeWithStatus");
Evan Charltonc66da362014-05-16 14:06:40 -07005963
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005964 final long identity = Binder.clearCallingIdentity();
5965 try {
5966 IccIoResult response = (IccIoResult) sendRequest(CMD_SEND_ENVELOPE, content, subId);
5967 if (response.payload == null) {
5968 return "";
5969 }
Evan Charltonc66da362014-05-16 14:06:40 -07005970
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005971 // Append the returned status code to the end of the response payload.
5972 String s = Integer.toHexString(
5973 (response.sw1 << 8) + response.sw2 + 0x10000).substring(1);
5974 s = IccUtils.bytesToHexString(response.payload) + s;
5975 return s;
5976 } finally {
5977 Binder.restoreCallingIdentity(identity);
5978 }
Evan Charltonc66da362014-05-16 14:06:40 -07005979 }
5980
Jake Hambye994d462014-02-03 13:10:13 -08005981 /**
5982 * Read one of the NV items defined in {@link com.android.internal.telephony.RadioNVItems}
5983 * and {@code ril_nv_items.h}. Used for device configuration by some CDMA operators.
5984 *
5985 * @param itemID the ID of the item to read
5986 * @return the NV item as a String, or null on error.
5987 */
5988 @Override
5989 public String nvReadItem(int itemID) {
vagdeviaf9a5b92018-08-15 16:01:53 -07005990 WorkSource workSource = getWorkSource(Binder.getCallingUid());
Jeff Davidson7e17e312018-02-13 18:17:36 -08005991 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
5992 mApp, getDefaultSubscription(), "nvReadItem");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005993
5994 final long identity = Binder.clearCallingIdentity();
5995 try {
5996 if (DBG) log("nvReadItem: item " + itemID);
vagdeviaf9a5b92018-08-15 16:01:53 -07005997 String value = (String) sendRequest(CMD_NV_READ_ITEM, itemID, workSource);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005998 if (DBG) log("nvReadItem: item " + itemID + " is \"" + value + '"');
5999 return value;
6000 } finally {
6001 Binder.restoreCallingIdentity(identity);
6002 }
Jake Hambye994d462014-02-03 13:10:13 -08006003 }
6004
6005 /**
6006 * Write one of the NV items defined in {@link com.android.internal.telephony.RadioNVItems}
6007 * and {@code ril_nv_items.h}. Used for device configuration by some CDMA operators.
6008 *
6009 * @param itemID the ID of the item to read
6010 * @param itemValue the value to write, as a String
6011 * @return true on success; false on any failure
6012 */
6013 @Override
6014 public boolean nvWriteItem(int itemID, String itemValue) {
vagdeviaf9a5b92018-08-15 16:01:53 -07006015 WorkSource workSource = getWorkSource(Binder.getCallingUid());
Jeff Davidson7e17e312018-02-13 18:17:36 -08006016 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
6017 mApp, getDefaultSubscription(), "nvWriteItem");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006018
6019 final long identity = Binder.clearCallingIdentity();
6020 try {
6021 if (DBG) log("nvWriteItem: item " + itemID + " value \"" + itemValue + '"');
6022 Boolean success = (Boolean) sendRequest(CMD_NV_WRITE_ITEM,
vagdeviaf9a5b92018-08-15 16:01:53 -07006023 new Pair<Integer, String>(itemID, itemValue), workSource);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006024 if (DBG) log("nvWriteItem: item " + itemID + ' ' + (success ? "ok" : "fail"));
6025 return success;
6026 } finally {
6027 Binder.restoreCallingIdentity(identity);
6028 }
Jake Hambye994d462014-02-03 13:10:13 -08006029 }
6030
6031 /**
6032 * Update the CDMA Preferred Roaming List (PRL) in the radio NV storage.
6033 * Used for device configuration by some CDMA operators.
6034 *
6035 * @param preferredRoamingList byte array containing the new PRL
6036 * @return true on success; false on any failure
6037 */
6038 @Override
6039 public boolean nvWriteCdmaPrl(byte[] preferredRoamingList) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08006040 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
6041 mApp, getDefaultSubscription(), "nvWriteCdmaPrl");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006042
6043 final long identity = Binder.clearCallingIdentity();
6044 try {
6045 if (DBG) log("nvWriteCdmaPrl: value: " + HexDump.toHexString(preferredRoamingList));
6046 Boolean success = (Boolean) sendRequest(CMD_NV_WRITE_CDMA_PRL, preferredRoamingList);
6047 if (DBG) log("nvWriteCdmaPrl: " + (success ? "ok" : "fail"));
6048 return success;
6049 } finally {
6050 Binder.restoreCallingIdentity(identity);
6051 }
Jake Hambye994d462014-02-03 13:10:13 -08006052 }
6053
6054 /**
chen xu6dac5ab2018-10-26 17:39:23 -07006055 * Rollback modem configurations to factory default except some config which are in whitelist.
Jake Hambye994d462014-02-03 13:10:13 -08006056 * Used for device configuration by some CDMA operators.
6057 *
chen xu6dac5ab2018-10-26 17:39:23 -07006058 * @param slotIndex - device slot.
6059 *
Jake Hambye994d462014-02-03 13:10:13 -08006060 * @return true on success; false on any failure
6061 */
6062 @Override
chen xu6dac5ab2018-10-26 17:39:23 -07006063 public boolean resetModemConfig(int slotIndex) {
6064 Phone phone = PhoneFactory.getPhone(slotIndex);
6065 if (phone != null) {
6066 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
6067 mApp, phone.getSubId(), "resetModemConfig");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006068
chen xu6dac5ab2018-10-26 17:39:23 -07006069 final long identity = Binder.clearCallingIdentity();
6070 try {
6071 Boolean success = (Boolean) sendRequest(CMD_RESET_MODEM_CONFIG, null);
6072 if (DBG) log("resetModemConfig:" + ' ' + (success ? "ok" : "fail"));
6073 return success;
6074 } finally {
6075 Binder.restoreCallingIdentity(identity);
6076 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006077 }
chen xu6dac5ab2018-10-26 17:39:23 -07006078 return false;
6079 }
6080
6081 /**
6082 * Generate a radio modem reset. Used for device configuration by some CDMA operators.
6083 *
6084 * @param slotIndex - device slot.
6085 *
6086 * @return true on success; false on any failure
6087 */
6088 @Override
6089 public boolean rebootModem(int slotIndex) {
6090 Phone phone = PhoneFactory.getPhone(slotIndex);
6091 if (phone != null) {
6092 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
6093 mApp, phone.getSubId(), "rebootModem");
6094
6095 final long identity = Binder.clearCallingIdentity();
6096 try {
6097 Boolean success = (Boolean) sendRequest(CMD_MODEM_REBOOT, null);
6098 if (DBG) log("rebootModem:" + ' ' + (success ? "ok" : "fail"));
6099 return success;
6100 } finally {
6101 Binder.restoreCallingIdentity(identity);
6102 }
6103 }
6104 return false;
Jake Hambye994d462014-02-03 13:10:13 -08006105 }
Jake Hamby7c27be32014-03-03 13:25:59 -08006106
Brad Ebinger51f743a2017-01-23 13:50:20 -08006107 /**
Grace Jiaaa2eb6b2020-01-09 16:26:08 -08006108 * Toggle IMS disable and enable for the framework to reset it. See {@link #enableIms(int)} and
6109 * {@link #disableIms(int)}.
6110 * @param slotIndex device slot.
6111 */
6112 public void resetIms(int slotIndex) {
6113 enforceModifyPermission();
6114
6115 final long identity = Binder.clearCallingIdentity();
6116 try {
6117 if (mImsResolver == null) {
6118 // may happen if the does not support IMS.
6119 return;
6120 }
Hyunhoa17ac7c2022-08-30 12:03:04 +00006121 mImsResolver.resetIms(slotIndex);
Grace Jiaaa2eb6b2020-01-09 16:26:08 -08006122 } finally {
6123 Binder.restoreCallingIdentity(identity);
6124 }
6125 }
6126
6127 /**
Brad Ebinger22bc3e42018-01-16 09:39:35 -08006128 * Enables IMS for the framework. This will trigger IMS registration and ImsFeature capability
6129 * status updates, if not already enabled.
Brad Ebinger51f743a2017-01-23 13:50:20 -08006130 */
Brad Ebinger22bc3e42018-01-16 09:39:35 -08006131 public void enableIms(int slotId) {
Brad Ebinger51f743a2017-01-23 13:50:20 -08006132 enforceModifyPermission();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006133
6134 final long identity = Binder.clearCallingIdentity();
6135 try {
Brad Ebinger24c29992019-12-05 13:03:21 -08006136 if (mImsResolver == null) {
Brad Ebinger9c0eb502019-01-23 15:06:19 -08006137 // may happen if the device does not support IMS.
6138 return;
6139 }
Brad Ebinger24c29992019-12-05 13:03:21 -08006140 mImsResolver.enableIms(slotId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006141 } finally {
6142 Binder.restoreCallingIdentity(identity);
6143 }
Brad Ebinger34bef922017-11-09 10:27:08 -08006144 }
6145
6146 /**
Brad Ebinger22bc3e42018-01-16 09:39:35 -08006147 * Disables IMS for the framework. This will trigger IMS de-registration and trigger ImsFeature
6148 * status updates to disabled.
Brad Ebinger34bef922017-11-09 10:27:08 -08006149 */
Brad Ebinger22bc3e42018-01-16 09:39:35 -08006150 public void disableIms(int slotId) {
6151 enforceModifyPermission();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006152
6153 final long identity = Binder.clearCallingIdentity();
6154 try {
Brad Ebinger24c29992019-12-05 13:03:21 -08006155 if (mImsResolver == null) {
Brad Ebinger9c0eb502019-01-23 15:06:19 -08006156 // may happen if the device does not support IMS.
6157 return;
6158 }
Brad Ebinger24c29992019-12-05 13:03:21 -08006159 mImsResolver.disableIms(slotId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006160 } finally {
6161 Binder.restoreCallingIdentity(identity);
6162 }
Brad Ebinger22bc3e42018-01-16 09:39:35 -08006163 }
6164
6165 /**
Brad Ebinger67b3e042020-09-11 12:45:11 -07006166 * Registers for updates to the MmTelFeature connection through the IImsServiceFeatureCallback
6167 * callback.
Brad Ebinger22bc3e42018-01-16 09:39:35 -08006168 */
Brad Ebinger67b3e042020-09-11 12:45:11 -07006169 @Override
Brad Ebingerf6aca002020-10-01 13:51:05 -07006170 public void registerMmTelFeatureCallback(int slotId, IImsServiceFeatureCallback callback) {
Brad Ebinger34bef922017-11-09 10:27:08 -08006171 enforceModifyPermission();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006172
6173 final long identity = Binder.clearCallingIdentity();
6174 try {
Brad Ebinger24c29992019-12-05 13:03:21 -08006175 if (mImsResolver == null) {
Brad Ebinger67b3e042020-09-11 12:45:11 -07006176 throw new ServiceSpecificException(ImsException.CODE_ERROR_UNSUPPORTED_OPERATION,
6177 "Device does not support IMS");
Brad Ebinger9c0eb502019-01-23 15:06:19 -08006178 }
Brad Ebingerf6aca002020-10-01 13:51:05 -07006179 mImsResolver.listenForFeature(slotId, ImsFeature.FEATURE_MMTEL, callback);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006180 } finally {
6181 Binder.restoreCallingIdentity(identity);
6182 }
Brad Ebinger34bef922017-11-09 10:27:08 -08006183 }
Brad Ebinger5f64b052017-12-14 14:26:15 -08006184 /**
Brad Ebinger075ff3a2020-05-18 17:52:58 -07006185 * Unregister a previously registered IImsServiceFeatureCallback associated with an ImsFeature.
6186 */
Brad Ebinger67b3e042020-09-11 12:45:11 -07006187 @Override
6188 public void unregisterImsFeatureCallback(IImsServiceFeatureCallback callback) {
Brad Ebinger075ff3a2020-05-18 17:52:58 -07006189 enforceModifyPermission();
6190
6191 final long identity = Binder.clearCallingIdentity();
6192 try {
6193 if (mImsResolver == null) return;
Brad Ebinger67b3e042020-09-11 12:45:11 -07006194 mImsResolver.unregisterImsFeatureCallback(callback);
Brad Ebinger075ff3a2020-05-18 17:52:58 -07006195 } finally {
6196 Binder.restoreCallingIdentity(identity);
6197 }
6198 }
6199
6200 /**
Brad Ebinger5f64b052017-12-14 14:26:15 -08006201 * Returns the {@link IImsRegistration} structure associated with the slotId and feature
Brad Ebinger9c0eb502019-01-23 15:06:19 -08006202 * specified or null if IMS is not supported on the slot specified.
Brad Ebinger5f64b052017-12-14 14:26:15 -08006203 */
6204 public IImsRegistration getImsRegistration(int slotId, int feature) throws RemoteException {
6205 enforceModifyPermission();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006206
6207 final long identity = Binder.clearCallingIdentity();
6208 try {
Brad Ebinger24c29992019-12-05 13:03:21 -08006209 if (mImsResolver == null) {
Brad Ebinger9c0eb502019-01-23 15:06:19 -08006210 // may happen if the device does not support IMS.
6211 return null;
6212 }
Brad Ebinger24c29992019-12-05 13:03:21 -08006213 return mImsResolver.getImsRegistration(slotId, feature);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006214 } finally {
6215 Binder.restoreCallingIdentity(identity);
6216 }
Brad Ebinger5f64b052017-12-14 14:26:15 -08006217 }
6218
Brad Ebinger22bc3e42018-01-16 09:39:35 -08006219 /**
6220 * Returns the {@link IImsConfig} structure associated with the slotId and feature
Brad Ebinger9c0eb502019-01-23 15:06:19 -08006221 * specified or null if IMS is not supported on the slot specified.
Brad Ebinger22bc3e42018-01-16 09:39:35 -08006222 */
6223 public IImsConfig getImsConfig(int slotId, int feature) throws RemoteException {
6224 enforceModifyPermission();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006225
6226 final long identity = Binder.clearCallingIdentity();
6227 try {
Brad Ebinger24c29992019-12-05 13:03:21 -08006228 if (mImsResolver == null) {
Brad Ebinger9c0eb502019-01-23 15:06:19 -08006229 // may happen if the device does not support IMS.
6230 return null;
6231 }
Brad Ebinger24c29992019-12-05 13:03:21 -08006232 return mImsResolver.getImsConfig(slotId, feature);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006233 } finally {
6234 Binder.restoreCallingIdentity(identity);
6235 }
Brad Ebinger22bc3e42018-01-16 09:39:35 -08006236 }
6237
Brad Ebinger884c07b2018-02-15 16:17:40 -08006238 /**
Brad Ebingerdac2f002018-04-03 15:17:52 -07006239 * Sets the ImsService Package Name that Telephony will bind to.
6240 *
Brad Ebinger24c29992019-12-05 13:03:21 -08006241 * @param slotIndex the slot ID that the ImsService should bind for.
6242 * @param isCarrierService true if the ImsService is the carrier override, false if the
Brad Ebingerdac2f002018-04-03 15:17:52 -07006243 * ImsService is the device default ImsService.
Brad Ebinger24c29992019-12-05 13:03:21 -08006244 * @param featureTypes An integer array of feature types associated with a packageName.
6245 * @param packageName The name of the package that the current configuration will be replaced
6246 * with.
Brad Ebingerdac2f002018-04-03 15:17:52 -07006247 * @return true if setting the ImsService to bind to succeeded, false if it did not.
Brad Ebingerdac2f002018-04-03 15:17:52 -07006248 */
Brad Ebinger24c29992019-12-05 13:03:21 -08006249 public boolean setBoundImsServiceOverride(int slotIndex, boolean isCarrierService,
6250 int[] featureTypes, String packageName) {
Brad Ebinger24c29992019-12-05 13:03:21 -08006251 TelephonyPermissions.enforceShellOnly(Binder.getCallingUid(), "setBoundImsServiceOverride");
Brad Ebingerde696de2018-04-06 09:56:40 -07006252 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(mApp,
Jack Yu00ece8c2022-11-19 22:29:12 -08006253 SubscriptionManager.getSubscriptionId(slotIndex), "setBoundImsServiceOverride");
Brad Ebingerde696de2018-04-06 09:56:40 -07006254
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006255 final long identity = Binder.clearCallingIdentity();
6256 try {
Brad Ebinger24c29992019-12-05 13:03:21 -08006257 if (mImsResolver == null) {
Brad Ebinger9c0eb502019-01-23 15:06:19 -08006258 // may happen if the device does not support IMS.
6259 return false;
6260 }
Brad Ebinger24c29992019-12-05 13:03:21 -08006261 Map<Integer, String> featureConfig = new HashMap<>();
6262 for (int featureType : featureTypes) {
6263 featureConfig.put(featureType, packageName);
6264 }
6265 return mImsResolver.overrideImsServiceConfiguration(slotIndex, isCarrierService,
6266 featureConfig);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006267 } finally {
6268 Binder.restoreCallingIdentity(identity);
6269 }
Brad Ebingerdac2f002018-04-03 15:17:52 -07006270 }
6271
6272 /**
Brad Ebinger999d3302020-11-25 14:31:39 -08006273 * Clears any carrier ImsService overrides for the slot index specified that were previously
6274 * set with {@link #setBoundImsServiceOverride(int, boolean, int[], String)}.
6275 *
6276 * This should only be used for testing.
6277 *
6278 * @param slotIndex the slot ID that the ImsService should bind for.
6279 * @return true if clearing the carrier ImsService override succeeded or false if it did not.
6280 */
6281 @Override
6282 public boolean clearCarrierImsServiceOverride(int slotIndex) {
Brad Ebinger999d3302020-11-25 14:31:39 -08006283 TelephonyPermissions.enforceShellOnly(Binder.getCallingUid(),
6284 "clearCarrierImsServiceOverride");
6285 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(mApp,
Jack Yu00ece8c2022-11-19 22:29:12 -08006286 SubscriptionManager.getSubscriptionId(slotIndex), "clearCarrierImsServiceOverride");
Brad Ebinger999d3302020-11-25 14:31:39 -08006287
6288 final long identity = Binder.clearCallingIdentity();
6289 try {
6290 if (mImsResolver == null) {
6291 // may happen if the device does not support IMS.
6292 return false;
6293 }
6294 return mImsResolver.clearCarrierImsServiceConfiguration(slotIndex);
6295 } finally {
6296 Binder.restoreCallingIdentity(identity);
6297 }
6298 }
6299
6300 /**
Brad Ebinger24c29992019-12-05 13:03:21 -08006301 * Return the package name of the currently bound ImsService.
Brad Ebingerdac2f002018-04-03 15:17:52 -07006302 *
6303 * @param slotId The slot that the ImsService is associated with.
6304 * @param isCarrierImsService true, if the ImsService is a carrier override, false if it is
6305 * the device default.
Brad Ebinger24c29992019-12-05 13:03:21 -08006306 * @param featureType The feature associated with the queried configuration.
Brad Ebingerdac2f002018-04-03 15:17:52 -07006307 * @return the package name of the ImsService configuration.
6308 */
Brad Ebinger24c29992019-12-05 13:03:21 -08006309 public String getBoundImsServicePackage(int slotId, boolean isCarrierImsService,
6310 @ImsFeature.FeatureType int featureType) {
Brad Ebinger24c29992019-12-05 13:03:21 -08006311 TelephonyPermissions
Jack Yu00ece8c2022-11-19 22:29:12 -08006312 .enforceCallingOrSelfReadPrivilegedPhoneStatePermissionOrCarrierPrivilege(mApp,
6313 SubscriptionManager.getSubscriptionId(slotId), "getBoundImsServicePackage");
Brad Ebingerde696de2018-04-06 09:56:40 -07006314
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006315 final long identity = Binder.clearCallingIdentity();
6316 try {
Brad Ebinger24c29992019-12-05 13:03:21 -08006317 if (mImsResolver == null) {
Brad Ebinger9c0eb502019-01-23 15:06:19 -08006318 // may happen if the device does not support IMS.
6319 return "";
6320 }
Brad Ebingera80c3312019-12-02 10:59:39 -08006321 // TODO: change API to query RCS separately.
Brad Ebinger24c29992019-12-05 13:03:21 -08006322 return mImsResolver.getImsServiceConfiguration(slotId, isCarrierImsService,
6323 featureType);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006324 } finally {
6325 Binder.restoreCallingIdentity(identity);
6326 }
Brad Ebingerdac2f002018-04-03 15:17:52 -07006327 }
6328
Brad Ebingerbc7dd582019-10-17 17:03:22 -07006329 /**
6330 * Get the MmTelFeature state associated with the requested subscription id.
6331 * @param subId The subscription that the MmTelFeature is associated with.
6332 * @param callback A callback with an integer containing the
6333 * {@link android.telephony.ims.feature.ImsFeature.ImsState} associated with the MmTelFeature.
6334 */
6335 @Override
6336 public void getImsMmTelFeatureState(int subId, IIntegerConsumer callback) {
6337 enforceReadPrivilegedPermission("getImsMmTelFeatureState");
Brad Ebingera2628302022-02-18 03:44:55 +00006338 if (!ImsManager.isImsSupportedOnDevice(mApp)) {
6339 throw new ServiceSpecificException(ImsException.CODE_ERROR_UNSUPPORTED_OPERATION,
6340 "IMS not available on device.");
6341 }
Brad Ebingerbc7dd582019-10-17 17:03:22 -07006342 final long token = Binder.clearCallingIdentity();
6343 try {
Brad Ebingera2628302022-02-18 03:44:55 +00006344 int slotId = getSlotIndex(subId);
6345 if (slotId <= SubscriptionManager.INVALID_SIM_SLOT_INDEX) {
6346 Log.w(LOG_TAG, "getImsMmTelFeatureState: called with an inactive subscription '"
6347 + subId + "'");
6348 throw new ServiceSpecificException(ImsException.CODE_ERROR_INVALID_SUBSCRIPTION);
6349 }
6350 verifyImsMmTelConfiguredOrThrow(slotId);
6351 ImsManager.getInstance(mApp, slotId).getImsServiceState(anInteger -> {
6352 try {
6353 callback.accept(anInteger == null ? ImsFeature.STATE_UNAVAILABLE : anInteger);
6354 } catch (RemoteException e) {
6355 Log.w(LOG_TAG, "getImsMmTelFeatureState: remote caller is no longer running. "
6356 + "Ignore");
6357 }
Brad Ebingerbc7dd582019-10-17 17:03:22 -07006358 });
Brad Ebinger919631e2021-06-02 17:46:35 -07006359 } catch (ImsException e) {
6360 throw new ServiceSpecificException(e.getCode());
Brad Ebingerbc7dd582019-10-17 17:03:22 -07006361 } finally {
6362 Binder.restoreCallingIdentity(token);
6363 }
6364 }
6365
Daniel Brightbb5840b2021-01-12 15:48:18 -08006366 /**
6367 * Sets the ims registration state on all valid {@link Phone}s.
6368 */
6369 public void setImsRegistrationState(final boolean registered) {
Wink Saville36469e72014-06-11 15:17:00 -07006370 enforceModifyPermission();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006371
6372 final long identity = Binder.clearCallingIdentity();
6373 try {
Daniel Brightbb5840b2021-01-12 15:48:18 -08006374 // NOTE: Before S, this method only set the default phone.
6375 for (final Phone phone : PhoneFactory.getPhones()) {
6376 if (SubscriptionManager.isValidSubscriptionId(phone.getSubId())) {
6377 phone.setImsRegistrationState(registered);
6378 }
6379 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006380 } finally {
6381 Binder.restoreCallingIdentity(identity);
6382 }
Wink Saville36469e72014-06-11 15:17:00 -07006383 }
6384
6385 /**
Stuart Scott54788802015-03-30 13:18:01 -07006386 * Set the network selection mode to automatic.
6387 *
6388 */
6389 @Override
6390 public void setNetworkSelectionModeAutomatic(int subId) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08006391 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
6392 mApp, subId, "setNetworkSelectionModeAutomatic");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006393
6394 final long identity = Binder.clearCallingIdentity();
6395 try {
shilufc958392020-01-20 11:36:01 -08006396 if (!isActiveSubscription(subId)) {
6397 return;
6398 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006399 if (DBG) log("setNetworkSelectionModeAutomatic: subId " + subId);
Rambo Wang0f050d82021-02-12 11:43:36 -08006400 sendRequest(CMD_SET_NETWORK_SELECTION_MODE_AUTOMATIC, null, subId,
6401 SET_NETWORK_SELECTION_MODE_AUTOMATIC_TIMEOUT_MS);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006402 } finally {
6403 Binder.restoreCallingIdentity(identity);
6404 }
Stuart Scott54788802015-03-30 13:18:01 -07006405 }
6406
Jack Yud10cdd42020-09-28 20:28:01 -07006407 /**
Pengquan Mengea84e042018-09-20 14:57:26 -07006408 * Ask the radio to connect to the input network and change selection mode to manual.
6409 *
6410 * @param subId the id of the subscription.
6411 * @param operatorInfo the operator information, included the PLMN, long name and short name of
6412 * the operator to attach to.
6413 * @param persistSelection whether the selection will persist until reboot. If true, only allows
6414 * attaching to the selected PLMN until reboot; otherwise, attach to the chosen PLMN and resume
6415 * normal network selection next time.
6416 * @return {@code true} on success; {@code true} on any failure.
Shishir Agrawal302c8692015-06-19 13:49:39 -07006417 */
6418 @Override
Pengquan Mengea84e042018-09-20 14:57:26 -07006419 public boolean setNetworkSelectionModeManual(
6420 int subId, OperatorInfo operatorInfo, boolean persistSelection) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08006421 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
6422 mApp, subId, "setNetworkSelectionModeManual");
Pengquan Menge92a50d2018-09-21 15:54:48 -07006423
Jack Yu285100e2022-12-02 22:48:35 -08006424 final long identity = Binder.clearCallingIdentity();
Pengquan Menge92a50d2018-09-21 15:54:48 -07006425 if (!isActiveSubscription(subId)) {
6426 return false;
6427 }
6428
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006429 try {
Pengquan Mengea84e042018-09-20 14:57:26 -07006430 ManualNetworkSelectionArgument arg = new ManualNetworkSelectionArgument(operatorInfo,
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006431 persistSelection);
Pengquan Mengea84e042018-09-20 14:57:26 -07006432 if (DBG) {
6433 log("setNetworkSelectionModeManual: subId: " + subId
6434 + " operator: " + operatorInfo);
6435 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006436 return (Boolean) sendRequest(CMD_SET_NETWORK_SELECTION_MODE_MANUAL, arg, subId);
6437 } finally {
6438 Binder.restoreCallingIdentity(identity);
6439 }
Shishir Agrawal302c8692015-06-19 13:49:39 -07006440 }
shilu84f6e8b2019-12-19 13:58:01 -08006441 /**
6442 * Get the manual network selection
6443 *
6444 * @param subId the id of the subscription.
6445 *
6446 * @return the previously saved user selected PLMN
6447 */
6448 @Override
6449 public String getManualNetworkSelectionPlmn(int subId) {
6450 TelephonyPermissions
Nathan Harold62c68512021-04-06 11:26:02 -07006451 .enforceCallingOrSelfReadPrecisePhoneStatePermissionOrCarrierPrivilege(
shilu84f6e8b2019-12-19 13:58:01 -08006452 mApp, subId, "getManualNetworkSelectionPlmn");
6453
6454 final long identity = Binder.clearCallingIdentity();
6455 try {
6456 if (!isActiveSubscription(subId)) {
shilufa1c2592020-03-10 10:59:43 -07006457 throw new IllegalArgumentException("Invalid Subscription Id: " + subId);
shilu84f6e8b2019-12-19 13:58:01 -08006458 }
6459
6460 final Phone phone = getPhone(subId);
6461 if (phone == null) {
shilufa1c2592020-03-10 10:59:43 -07006462 throw new IllegalArgumentException("Invalid Subscription Id: " + subId);
shilu84f6e8b2019-12-19 13:58:01 -08006463 }
6464 OperatorInfo networkSelection = phone.getSavedNetworkSelection();
6465 return TextUtils.isEmpty(networkSelection.getOperatorNumeric())
6466 ? phone.getManualNetworkSelectionPlmn() : networkSelection.getOperatorNumeric();
6467 } finally {
6468 Binder.restoreCallingIdentity(identity);
6469 }
6470 }
Shishir Agrawal302c8692015-06-19 13:49:39 -07006471
6472 /**
6473 * Scans for available networks.
6474 */
6475 @Override
Philip P. Moltmann3a2772a2019-10-04 08:15:00 -07006476 public CellNetworkScanResult getCellNetworkScanResults(int subId, String callingPackage,
6477 String callingFeatureId) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08006478 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
6479 mApp, subId, "getCellNetworkScanResults");
Hall Liuf19c44f2018-11-27 14:38:17 -08006480 LocationAccessPolicy.LocationPermissionResult locationResult =
6481 LocationAccessPolicy.checkLocationPermission(mApp,
6482 new LocationAccessPolicy.LocationPermissionQuery.Builder()
6483 .setCallingPackage(callingPackage)
Philip P. Moltmann3a2772a2019-10-04 08:15:00 -07006484 .setCallingFeatureId(callingFeatureId)
Hall Liuf19c44f2018-11-27 14:38:17 -08006485 .setCallingPid(Binder.getCallingPid())
6486 .setCallingUid(Binder.getCallingUid())
6487 .setMethod("getCellNetworkScanResults")
6488 .setMinSdkVersionForFine(Build.VERSION_CODES.Q)
Hall Liuc4a3e422020-05-26 17:18:03 -07006489 .setMinSdkVersionForCoarse(Build.VERSION_CODES.Q)
6490 .setMinSdkVersionForEnforcement(Build.VERSION_CODES.Q)
Hall Liuf19c44f2018-11-27 14:38:17 -08006491 .build());
6492 switch (locationResult) {
6493 case DENIED_HARD:
6494 throw new SecurityException("Not allowed to access scan results -- location");
6495 case DENIED_SOFT:
6496 return null;
6497 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006498
Pengquan Menga1bb6272018-09-06 09:59:22 -07006499 long identity = Binder.clearCallingIdentity();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006500 try {
6501 if (DBG) log("getCellNetworkScanResults: subId " + subId);
Pengquan Menga1bb6272018-09-06 09:59:22 -07006502 return (CellNetworkScanResult) sendRequest(
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006503 CMD_PERFORM_NETWORK_SCAN, null, subId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006504 } finally {
6505 Binder.restoreCallingIdentity(identity);
6506 }
Shishir Agrawal302c8692015-06-19 13:49:39 -07006507 }
6508
6509 /**
Shuo Qian4a594052020-01-23 11:59:30 -08006510 * Get the call forwarding info, given the call forwarding reason.
6511 */
6512 @Override
Hall Liu27d24262020-09-18 19:04:59 -07006513 public void getCallForwarding(int subId, int callForwardingReason,
6514 ICallForwardingInfoCallback callback) {
Shuo Qian4a594052020-01-23 11:59:30 -08006515 enforceReadPrivilegedPermission("getCallForwarding");
6516 long identity = Binder.clearCallingIdentity();
6517 try {
6518 if (DBG) {
6519 log("getCallForwarding: subId " + subId
6520 + " callForwardingReason" + callForwardingReason);
6521 }
Hall Liu27d24262020-09-18 19:04:59 -07006522
6523 Phone phone = getPhone(subId);
6524 if (phone == null) {
6525 try {
Hall Liu940c4ca2020-09-29 17:10:18 -07006526 callback.onError(
6527 TelephonyManager.CallForwardingInfoCallback.RESULT_ERROR_UNKNOWN);
Hall Liu27d24262020-09-18 19:04:59 -07006528 } catch (RemoteException e) {
6529 // ignore
6530 }
6531 return;
6532 }
6533
6534 Pair<Integer, TelephonyManager.CallForwardingInfoCallback> argument = Pair.create(
6535 callForwardingReason, new TelephonyManager.CallForwardingInfoCallback() {
6536 @Override
6537 public void onCallForwardingInfoAvailable(CallForwardingInfo info) {
6538 try {
6539 callback.onCallForwardingInfoAvailable(info);
6540 } catch (RemoteException e) {
6541 // ignore
6542 }
6543 }
6544
6545 @Override
6546 public void onError(int error) {
6547 try {
6548 callback.onError(error);
6549 } catch (RemoteException e) {
6550 // ignore
6551 }
6552 }
6553 });
6554 sendRequestAsync(CMD_GET_CALL_FORWARDING, argument, phone, null);
Shuo Qian4a594052020-01-23 11:59:30 -08006555 } finally {
6556 Binder.restoreCallingIdentity(identity);
6557 }
6558 }
6559
6560 /**
6561 * Sets the voice call forwarding info including status (enable/disable), call forwarding
6562 * reason, the number to forward, and the timeout before the forwarding is attempted.
6563 */
6564 @Override
Hall Liu27d24262020-09-18 19:04:59 -07006565 public void setCallForwarding(int subId, CallForwardingInfo callForwardingInfo,
6566 IIntegerConsumer callback) {
Shuo Qian4a594052020-01-23 11:59:30 -08006567 enforceModifyPermission();
6568 long identity = Binder.clearCallingIdentity();
6569 try {
6570 if (DBG) {
6571 log("setCallForwarding: subId " + subId
6572 + " callForwardingInfo" + callForwardingInfo);
6573 }
Hall Liu27d24262020-09-18 19:04:59 -07006574
6575 Phone phone = getPhone(subId);
6576 if (phone == null) {
6577 try {
Hall Liu940c4ca2020-09-29 17:10:18 -07006578 callback.accept(
6579 TelephonyManager.CallForwardingInfoCallback.RESULT_ERROR_UNKNOWN);
Hall Liu27d24262020-09-18 19:04:59 -07006580 } catch (RemoteException e) {
6581 // ignore
6582 }
6583 return;
6584 }
6585
6586 Pair<CallForwardingInfo, Consumer<Integer>> arguments = Pair.create(callForwardingInfo,
6587 FunctionalUtils.ignoreRemoteException(callback::accept));
6588
6589 sendRequestAsync(CMD_SET_CALL_FORWARDING, arguments, phone, null);
Shuo Qian4a594052020-01-23 11:59:30 -08006590 } finally {
6591 Binder.restoreCallingIdentity(identity);
6592 }
6593 }
6594
6595 /**
Hall Liu27d24262020-09-18 19:04:59 -07006596 * Get the call waiting status for a subId.
Shuo Qian4a594052020-01-23 11:59:30 -08006597 */
6598 @Override
Hall Liu27d24262020-09-18 19:04:59 -07006599 public void getCallWaitingStatus(int subId, IIntegerConsumer callback) {
SongFerngWang0e767992021-03-31 22:08:45 +08006600 enforceReadPrivilegedPermission("getCallWaitingStatus");
Shuo Qian4a594052020-01-23 11:59:30 -08006601 long identity = Binder.clearCallingIdentity();
6602 try {
Hall Liu27d24262020-09-18 19:04:59 -07006603 Phone phone = getPhone(subId);
6604 if (phone == null) {
6605 try {
6606 callback.accept(TelephonyManager.CALL_WAITING_STATUS_UNKNOWN_ERROR);
6607 } catch (RemoteException e) {
6608 // ignore
6609 }
6610 return;
6611 }
SongFerngWang0e767992021-03-31 22:08:45 +08006612 CarrierConfigManager configManager = new CarrierConfigManager(phone.getContext());
6613 PersistableBundle c = configManager.getConfigForSubId(subId);
6614 boolean requireUssd = c.getBoolean(
6615 CarrierConfigManager.KEY_USE_CALL_WAITING_USSD_BOOL, false);
Hall Liu27d24262020-09-18 19:04:59 -07006616
Shuo Qian4a594052020-01-23 11:59:30 -08006617 if (DBG) log("getCallWaitingStatus: subId " + subId);
SongFerngWang0e767992021-03-31 22:08:45 +08006618 if (requireUssd) {
6619 CarrierXmlParser carrierXmlParser = new CarrierXmlParser(phone.getContext(),
6620 getSubscriptionCarrierId(subId));
6621 String newUssdCommand = "";
6622 try {
6623 newUssdCommand = carrierXmlParser.getFeature(
6624 CarrierXmlParser.FEATURE_CALL_WAITING)
6625 .makeCommand(CarrierXmlParser.SsEntry.SSAction.QUERY, null);
6626 } catch (NullPointerException e) {
6627 loge("Failed to generate USSD number" + e);
6628 }
6629 ResultReceiver wrappedCallback = new CallWaitingUssdResultReceiver(
6630 mMainThreadHandler, callback, carrierXmlParser,
6631 CarrierXmlParser.SsEntry.SSAction.QUERY);
6632 final String ussdCommand = newUssdCommand;
6633 Executors.newSingleThreadExecutor().execute(() -> {
6634 handleUssdRequest(subId, ussdCommand, wrappedCallback);
6635 });
6636 } else {
6637 Consumer<Integer> argument = FunctionalUtils.ignoreRemoteException(
6638 callback::accept);
6639 sendRequestAsync(CMD_GET_CALL_WAITING, argument, phone, null);
6640 }
Shuo Qian4a594052020-01-23 11:59:30 -08006641 } finally {
6642 Binder.restoreCallingIdentity(identity);
6643 }
6644 }
6645
6646 /**
Hall Liu27d24262020-09-18 19:04:59 -07006647 * Sets whether call waiting is enabled for a given subId.
Shuo Qian4a594052020-01-23 11:59:30 -08006648 */
6649 @Override
Hall Liu27d24262020-09-18 19:04:59 -07006650 public void setCallWaitingStatus(int subId, boolean enable, IIntegerConsumer callback) {
Shuo Qian4a594052020-01-23 11:59:30 -08006651 enforceModifyPermission();
6652 long identity = Binder.clearCallingIdentity();
6653 try {
Hall Liu27d24262020-09-18 19:04:59 -07006654 if (DBG) log("setCallWaitingStatus: subId " + subId + " enable: " + enable);
6655
6656 Phone phone = getPhone(subId);
6657 if (phone == null) {
6658 try {
6659 callback.accept(TelephonyManager.CALL_WAITING_STATUS_UNKNOWN_ERROR);
6660 } catch (RemoteException e) {
6661 // ignore
6662 }
6663 return;
6664 }
6665
SongFerngWang0e767992021-03-31 22:08:45 +08006666 CarrierConfigManager configManager = new CarrierConfigManager(phone.getContext());
6667 PersistableBundle c = configManager.getConfigForSubId(subId);
6668 boolean requireUssd = c.getBoolean(
6669 CarrierConfigManager.KEY_USE_CALL_WAITING_USSD_BOOL, false);
Hall Liu27d24262020-09-18 19:04:59 -07006670
SongFerngWang0e767992021-03-31 22:08:45 +08006671 if (DBG) log("getCallWaitingStatus: subId " + subId);
6672 if (requireUssd) {
6673 CarrierXmlParser carrierXmlParser = new CarrierXmlParser(phone.getContext(),
6674 getSubscriptionCarrierId(subId));
6675 CarrierXmlParser.SsEntry.SSAction ssAction =
6676 enable ? CarrierXmlParser.SsEntry.SSAction.UPDATE_ACTIVATE
6677 : CarrierXmlParser.SsEntry.SSAction.UPDATE_DEACTIVATE;
6678 String newUssdCommand = "";
6679 try {
6680 newUssdCommand = carrierXmlParser.getFeature(
6681 CarrierXmlParser.FEATURE_CALL_WAITING)
6682 .makeCommand(ssAction, null);
6683 } catch (NullPointerException e) {
6684 loge("Failed to generate USSD number" + e);
6685 }
6686 ResultReceiver wrappedCallback = new CallWaitingUssdResultReceiver(
6687 mMainThreadHandler, callback, carrierXmlParser, ssAction);
6688 final String ussdCommand = newUssdCommand;
6689 Executors.newSingleThreadExecutor().execute(() -> {
6690 handleUssdRequest(subId, ussdCommand, wrappedCallback);
6691 });
6692 } else {
6693 Pair<Boolean, Consumer<Integer>> arguments = Pair.create(enable,
6694 FunctionalUtils.ignoreRemoteException(callback::accept));
6695
6696 sendRequestAsync(CMD_SET_CALL_WAITING, arguments, phone, null);
6697 }
Shuo Qian4a594052020-01-23 11:59:30 -08006698 } finally {
6699 Binder.restoreCallingIdentity(identity);
6700 }
6701 }
6702
6703 /**
yinxub1bed742017-04-17 11:45:04 -07006704 * Starts a new network scan and returns the id of this scan.
yinxu504e1392017-04-12 16:03:22 -07006705 *
yinxub1bed742017-04-17 11:45:04 -07006706 * @param subId id of the subscription
Sooraj Sasindran2250dc02021-11-10 16:42:01 -08006707 * @param renounceFineLocationAccess Set this to true if the caller would not like to receive
6708 * location related information which will be sent if the caller already possess
6709 * {@android.Manifest.permission.ACCESS_FINE_LOCATION} and do not renounce the permission
yinxub1bed742017-04-17 11:45:04 -07006710 * @param request contains the radio access networks with bands/channels to scan
6711 * @param messenger callback messenger for scan results or errors
6712 * @param binder for the purpose of auto clean when the user thread crashes
yinxu504e1392017-04-12 16:03:22 -07006713 * @return the id of the requested scan which can be used to stop the scan.
6714 */
6715 @Override
Sooraj Sasindran2250dc02021-11-10 16:42:01 -08006716 public int requestNetworkScan(int subId, boolean renounceFineLocationAccess,
6717 NetworkScanRequest request, Messenger messenger,
Philip P. Moltmann3a2772a2019-10-04 08:15:00 -07006718 IBinder binder, String callingPackage, String callingFeatureId) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08006719 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
6720 mApp, subId, "requestNetworkScan");
Hall Liuf19c44f2018-11-27 14:38:17 -08006721 LocationAccessPolicy.LocationPermissionResult locationResult =
Sooraj Sasindran2250dc02021-11-10 16:42:01 -08006722 LocationAccessPolicy.LocationPermissionResult.DENIED_HARD;
6723 if (!renounceFineLocationAccess) {
6724 locationResult = LocationAccessPolicy.checkLocationPermission(mApp,
6725 new LocationAccessPolicy.LocationPermissionQuery.Builder()
6726 .setCallingPackage(callingPackage)
6727 .setCallingFeatureId(callingFeatureId)
6728 .setCallingPid(Binder.getCallingPid())
6729 .setCallingUid(Binder.getCallingUid())
6730 .setMethod("requestNetworkScan")
6731 .setMinSdkVersionForFine(Build.VERSION_CODES.Q)
6732 .setMinSdkVersionForCoarse(Build.VERSION_CODES.Q)
6733 .setMinSdkVersionForEnforcement(Build.VERSION_CODES.Q)
6734 .build());
6735 }
Hall Liub2ac8ef2019-02-28 15:56:23 -08006736 if (locationResult != LocationAccessPolicy.LocationPermissionResult.ALLOWED) {
Nathan Harold1c11dba2020-09-22 17:54:53 -07006737 SecurityException e = checkNetworkRequestForSanitizedLocationAccess(
6738 request, subId, callingPackage);
Hall Liub2ac8ef2019-02-28 15:56:23 -08006739 if (e != null) {
6740 if (locationResult == LocationAccessPolicy.LocationPermissionResult.DENIED_HARD) {
6741 throw e;
6742 } else {
Hall Liu0e5abaf2019-04-04 01:25:30 -07006743 loge(e.getMessage());
Hall Liub2ac8ef2019-02-28 15:56:23 -08006744 return TelephonyScanManager.INVALID_SCAN_ID;
6745 }
6746 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006747 }
Hall Liu912dfd32019-04-25 14:02:26 -07006748 int callingUid = Binder.getCallingUid();
6749 int callingPid = Binder.getCallingPid();
Ying Xu94a46582019-04-18 17:14:56 -07006750 final long identity = Binder.clearCallingIdentity();
6751 try {
6752 return mNetworkScanRequestTracker.startNetworkScan(
Sooraj Sasindran2250dc02021-11-10 16:42:01 -08006753 renounceFineLocationAccess, request, messenger, binder, getPhone(subId),
Hall Liu912dfd32019-04-25 14:02:26 -07006754 callingUid, callingPid, callingPackage);
Ying Xu94a46582019-04-18 17:14:56 -07006755 } finally {
6756 Binder.restoreCallingIdentity(identity);
6757 }
yinxu504e1392017-04-12 16:03:22 -07006758 }
6759
Hall Liub2ac8ef2019-02-28 15:56:23 -08006760 private SecurityException checkNetworkRequestForSanitizedLocationAccess(
Nathan Harold1c11dba2020-09-22 17:54:53 -07006761 NetworkScanRequest request, int subId, String callingPackage) {
Rambo Wang3dee30a2022-10-20 16:52:29 +00006762 boolean hasCarrierPriv;
6763 final long identity = Binder.clearCallingIdentity();
6764 try {
6765 hasCarrierPriv = checkCarrierPrivilegesForPackage(subId, callingPackage)
6766 == TelephonyManager.CARRIER_PRIVILEGE_STATUS_HAS_ACCESS;
6767 } finally {
6768 Binder.restoreCallingIdentity(identity);
6769 }
Hall Liu558027f2019-05-15 19:14:05 -07006770 boolean hasNetworkScanPermission =
6771 mApp.checkCallingOrSelfPermission(android.Manifest.permission.NETWORK_SCAN)
6772 == PERMISSION_GRANTED;
6773
6774 if (!hasCarrierPriv && !hasNetworkScanPermission) {
6775 return new SecurityException("permission.NETWORK_SCAN or carrier privileges is needed"
6776 + " for network scans without location access.");
Hall Liub2ac8ef2019-02-28 15:56:23 -08006777 }
6778
6779 if (request.getSpecifiers() != null && request.getSpecifiers().length > 0) {
6780 for (RadioAccessSpecifier ras : request.getSpecifiers()) {
Hall Liub2ac8ef2019-02-28 15:56:23 -08006781 if (ras.getChannels() != null && ras.getChannels().length > 0) {
6782 return new SecurityException("Specific channels must not be"
6783 + " scanned without location access.");
6784 }
6785 }
6786 }
6787
Hall Liub2ac8ef2019-02-28 15:56:23 -08006788 return null;
6789 }
6790
yinxu504e1392017-04-12 16:03:22 -07006791 /**
6792 * Stops an existing network scan with the given scanId.
yinxub1bed742017-04-17 11:45:04 -07006793 *
6794 * @param subId id of the subscription
6795 * @param scanId id of the scan that needs to be stopped
yinxu504e1392017-04-12 16:03:22 -07006796 */
6797 @Override
6798 public void stopNetworkScan(int subId, int scanId) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08006799 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
6800 mApp, subId, "stopNetworkScan");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006801
Hall Liu912dfd32019-04-25 14:02:26 -07006802 int callingUid = Binder.getCallingUid();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006803 final long identity = Binder.clearCallingIdentity();
6804 try {
Hall Liu912dfd32019-04-25 14:02:26 -07006805 mNetworkScanRequestTracker.stopNetworkScan(scanId, callingUid);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006806 } finally {
6807 Binder.restoreCallingIdentity(identity);
6808 }
yinxu504e1392017-04-12 16:03:22 -07006809 }
6810
6811 /**
SongFerngWang3ef3e072020-12-21 16:41:52 +08006812 * Get the allowed network types bitmask.
Junda Liu84d15a22014-07-02 11:21:04 -07006813 *
SongFerngWang3ef3e072020-12-21 16:41:52 +08006814 * @return the allowed network types bitmask, defined in RILConstants.java.
Junda Liu84d15a22014-07-02 11:21:04 -07006815 */
6816 @Override
SongFerngWang3ef3e072020-12-21 16:41:52 +08006817 public int getAllowedNetworkTypesBitmask(int subId) {
Pengquan Menga4009cb2018-12-20 11:00:24 -08006818 TelephonyPermissions
Nathan Harold62c68512021-04-06 11:26:02 -07006819 .enforceCallingOrSelfReadPrivilegedPhoneStatePermissionOrCarrierPrivilege(
SongFerngWang3ef3e072020-12-21 16:41:52 +08006820 mApp, subId, "getAllowedNetworkTypesBitmask");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006821
6822 final long identity = Binder.clearCallingIdentity();
6823 try {
SongFerngWang3ef3e072020-12-21 16:41:52 +08006824 if (DBG) log("getAllowedNetworkTypesBitmask");
6825 int[] result = (int[]) sendRequest(CMD_GET_ALLOWED_NETWORK_TYPES_BITMASK, null, subId);
6826 int networkTypesBitmask = (result != null ? result[0] : -1);
6827 if (DBG) log("getAllowedNetworkTypesBitmask: " + networkTypesBitmask);
6828 return networkTypesBitmask;
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006829 } finally {
6830 Binder.restoreCallingIdentity(identity);
6831 }
Jake Hamby7c27be32014-03-03 13:25:59 -08006832 }
6833
6834 /**
Sooraj Sasindranc46dfbd2020-06-03 01:06:00 -07006835 * Get the allowed network types for certain reason.
6836 *
6837 * @param subId the id of the subscription.
6838 * @param reason the reason the allowed network type change is taking place
6839 * @return the allowed network types.
6840 */
6841 @Override
6842 public long getAllowedNetworkTypesForReason(int subId,
6843 @TelephonyManager.AllowedNetworkTypesReason int reason) {
Nathan Harold62c68512021-04-06 11:26:02 -07006844 TelephonyPermissions.enforceCallingOrSelfReadPrecisePhoneStatePermissionOrCarrierPrivilege(
SongFerngWang8c6e82e2021-03-02 22:09:29 +08006845 mApp, subId, "getAllowedNetworkTypesForReason");
Sooraj Sasindranc46dfbd2020-06-03 01:06:00 -07006846 final long identity = Binder.clearCallingIdentity();
6847 try {
6848 return getPhoneFromSubId(subId).getAllowedNetworkTypes(reason);
6849 } finally {
6850 Binder.restoreCallingIdentity(identity);
6851 }
6852 }
6853
6854 /**
Sooraj Sasindran37444802020-08-11 10:40:43 -07006855 * Enable/Disable E-UTRA-NR Dual Connectivity
6856 * @param subId subscription id of the sim card
6857 * @param nrDualConnectivityState expected NR dual connectivity state
6858 * This can be passed following states
6859 * <ol>
6860 * <li>Enable NR dual connectivity {@link TelephonyManager#NR_DUAL_CONNECTIVITY_ENABLE}
6861 * <li>Disable NR dual connectivity {@link TelephonyManager#NR_DUAL_CONNECTIVITY_DISABLE}
6862 * <li>Disable NR dual connectivity and force secondary cell to be released
6863 * {@link TelephonyManager#NR_DUAL_CONNECTIVITY_DISABLE_IMMEDIATE}
6864 * </ol>
6865 * @return operation result.
6866 */
6867 @Override
6868 public int setNrDualConnectivityState(int subId,
6869 @TelephonyManager.NrDualConnectivityState int nrDualConnectivityState) {
6870 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
6871 mApp, subId, "enableNRDualConnectivity");
Sooraj Sasindran0e4e00a2021-03-16 18:02:32 -07006872 if (!isRadioInterfaceCapabilitySupported(
Sooraj Sasindrandfd595b2021-03-11 17:38:13 -08006873 TelephonyManager.CAPABILITY_NR_DUAL_CONNECTIVITY_CONFIGURATION_AVAILABLE)) {
6874 return TelephonyManager.ENABLE_NR_DUAL_CONNECTIVITY_NOT_SUPPORTED;
6875 }
6876
Sooraj Sasindran37444802020-08-11 10:40:43 -07006877 WorkSource workSource = getWorkSource(Binder.getCallingUid());
6878 final long identity = Binder.clearCallingIdentity();
6879 try {
6880 int result = (int) sendRequest(CMD_ENABLE_NR_DUAL_CONNECTIVITY,
6881 nrDualConnectivityState, subId,
6882 workSource);
6883 if (DBG) log("enableNRDualConnectivity result: " + result);
6884 return result;
6885 } finally {
6886 Binder.restoreCallingIdentity(identity);
6887 }
6888 }
6889
6890 /**
6891 * Is E-UTRA-NR Dual Connectivity enabled
6892 * @return true if dual connectivity is enabled else false
6893 */
6894 @Override
6895 public boolean isNrDualConnectivityEnabled(int subId) {
6896 TelephonyPermissions
Nathan Harold62c68512021-04-06 11:26:02 -07006897 .enforceCallingOrSelfReadPrivilegedPhoneStatePermissionOrCarrierPrivilege(
Sooraj Sasindran37444802020-08-11 10:40:43 -07006898 mApp, subId, "isNRDualConnectivityEnabled");
Sooraj Sasindran0e4e00a2021-03-16 18:02:32 -07006899 if (!isRadioInterfaceCapabilitySupported(
Sooraj Sasindrandfd595b2021-03-11 17:38:13 -08006900 TelephonyManager.CAPABILITY_NR_DUAL_CONNECTIVITY_CONFIGURATION_AVAILABLE)) {
6901 return false;
6902 }
Sooraj Sasindran37444802020-08-11 10:40:43 -07006903 WorkSource workSource = getWorkSource(Binder.getCallingUid());
6904 final long identity = Binder.clearCallingIdentity();
6905 try {
6906 boolean isEnabled = (boolean) sendRequest(CMD_IS_NR_DUAL_CONNECTIVITY_ENABLED,
6907 null, subId, workSource);
6908 if (DBG) log("isNRDualConnectivityEnabled: " + isEnabled);
6909 return isEnabled;
6910 } finally {
6911 Binder.restoreCallingIdentity(identity);
6912 }
6913 }
6914
6915 /**
Sooraj Sasindranc46dfbd2020-06-03 01:06:00 -07006916 * Set the allowed network types of the device and
6917 * provide the reason triggering the allowed network change.
6918 *
6919 * @param subId the id of the subscription.
6920 * @param reason the reason the allowed network type change is taking place
6921 * @param allowedNetworkTypes the allowed network types.
6922 * @return true on success; false on any failure.
6923 */
6924 @Override
6925 public boolean setAllowedNetworkTypesForReason(int subId,
SongFerngWang3ef3e072020-12-21 16:41:52 +08006926 @TelephonyManager.AllowedNetworkTypesReason int reason,
6927 @TelephonyManager.NetworkTypeBitMask long allowedNetworkTypes) {
Sooraj Sasindranc46dfbd2020-06-03 01:06:00 -07006928 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
6929 mApp, subId, "setAllowedNetworkTypesForReason");
Gil Cukierman1d3d3752022-10-03 21:31:33 +00006930 // If the caller only has carrier privileges, then they should not be able to override
6931 // any network types which were set for security reasons.
6932 if (mApp.checkCallingOrSelfPermission(Manifest.permission.MODIFY_PHONE_STATE)
6933 != PERMISSION_GRANTED
6934 && (reason == TelephonyManager.ALLOWED_NETWORK_TYPES_REASON_ENABLE_2G
6935 || reason == TelephonyManager.ALLOWED_NETWORK_TYPES_REASON_USER_RESTRICTIONS)) {
6936 throw new SecurityException(
6937 "setAllowedNetworkTypesForReason cannot be called with carrier privileges for"
6938 + " reason "
Jack Yu5b494332023-01-23 18:18:04 +00006939 + reason);
Gil Cukierman1d3d3752022-10-03 21:31:33 +00006940 }
SongFerngWang3ef3e072020-12-21 16:41:52 +08006941 if (!TelephonyManager.isValidAllowedNetworkTypesReason(reason)) {
Jack Yu5b494332023-01-23 18:18:04 +00006942 loge("setAllowedNetworkTypesForReason: Invalid allowed network type reason: " + reason);
SongFerngWang7ffc2732021-04-15 19:46:33 +08006943 return false;
6944 }
6945 if (!SubscriptionManager.isUsableSubscriptionId(subId)) {
6946 loge("setAllowedNetworkTypesForReason: Invalid subscriptionId:" + subId);
SongFerngWang3ef3e072020-12-21 16:41:52 +08006947 return false;
6948 }
6949
Jack Yu5b494332023-01-23 18:18:04 +00006950 log("setAllowedNetworkTypesForReason: subId=" + subId + ", reason=" + reason + " value: "
6951 + TelephonyManager.convertNetworkTypeBitmaskToString(allowedNetworkTypes));
SongFerngWang8c6e82e2021-03-02 22:09:29 +08006952
Jack Yue37dd262022-12-16 11:53:37 -08006953 Phone phone = getPhone(subId);
6954 if (phone == null) {
6955 return false;
6956 }
SongFerngWang8c6e82e2021-03-02 22:09:29 +08006957
Jack Yue37dd262022-12-16 11:53:37 -08006958 if (allowedNetworkTypes == phone.getAllowedNetworkTypes(reason)) {
Jack Yu5b494332023-01-23 18:18:04 +00006959 log("setAllowedNetworkTypesForReason: " + reason + "does not change value");
SongFerngWang8c6e82e2021-03-02 22:09:29 +08006960 return true;
SongFerngWang3ef3e072020-12-21 16:41:52 +08006961 }
SongFerngWang8c6e82e2021-03-02 22:09:29 +08006962
Sooraj Sasindranc46dfbd2020-06-03 01:06:00 -07006963 final long identity = Binder.clearCallingIdentity();
6964 try {
SongFerngWang3ef3e072020-12-21 16:41:52 +08006965 Boolean success = (Boolean) sendRequest(
6966 CMD_SET_ALLOWED_NETWORK_TYPES_FOR_REASON,
6967 new Pair<Integer, Long>(reason, allowedNetworkTypes), subId);
6968
6969 if (DBG) log("setAllowedNetworkTypesForReason: " + (success ? "ok" : "fail"));
6970 return success;
Sooraj Sasindranc46dfbd2020-06-03 01:06:00 -07006971 } finally {
6972 Binder.restoreCallingIdentity(identity);
6973 }
6974 }
6975
6976 /**
Miaoa84611c2019-03-15 09:21:10 +08006977 * Check whether DUN APN is required for tethering with subId.
Junda Liu475951f2014-11-07 16:45:03 -08006978 *
Miaoa84611c2019-03-15 09:21:10 +08006979 * @param subId the id of the subscription to require tethering.
Amit Mahajanfe58cdf2017-07-11 12:01:53 -07006980 * @return {@code true} if DUN APN is required for tethering.
Junda Liu475951f2014-11-07 16:45:03 -08006981 * @hide
6982 */
6983 @Override
SongFerngWangf08d8122019-11-15 14:58:44 +08006984 public boolean isTetheringApnRequiredForSubscriber(int subId) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08006985 enforceModifyPermission();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006986 final long identity = Binder.clearCallingIdentity();
Miaoa84611c2019-03-15 09:21:10 +08006987 final Phone phone = getPhone(subId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006988 try {
Miaoa84611c2019-03-15 09:21:10 +08006989 if (phone != null) {
6990 return phone.hasMatchedTetherApnSetting();
6991 } else {
6992 return false;
6993 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006994 } finally {
6995 Binder.restoreCallingIdentity(identity);
Junda Liu475951f2014-11-07 16:45:03 -08006996 }
Junda Liu475951f2014-11-07 16:45:03 -08006997 }
6998
6999 /**
Malcolm Chen964682d2017-11-28 16:20:07 -08007000 * Get the user enabled state of Mobile Data.
7001 *
7002 * TODO: remove and use isUserDataEnabled.
7003 * This can't be removed now because some vendor codes
7004 * calls through ITelephony directly while they should
7005 * use TelephonyManager.
7006 *
7007 * @return true on enabled
7008 */
7009 @Override
7010 public boolean getDataEnabled(int subId) {
7011 return isUserDataEnabled(subId);
7012 }
7013
7014 /**
7015 * Get whether mobile data is enabled per user setting.
7016 *
7017 * There are other factors deciding whether mobile data is actually enabled, but they are
7018 * not considered here. See {@link #isDataEnabled(int)} for more details.
Robert Greenwalt646120a2014-05-23 11:54:03 -07007019 *
Sooraj Sasindran5d051bf2021-11-05 13:14:15 -07007020 * Accepts either READ_BASIC_PHONE_STATE, ACCESS_NETWORK_STATE, MODIFY_PHONE_STATE
7021 * or carrier privileges.
Robert Greenwalted86e582014-05-21 20:03:20 -07007022 *
7023 * @return {@code true} if data is enabled else {@code false}
7024 */
7025 @Override
Malcolm Chen964682d2017-11-28 16:20:07 -08007026 public boolean isUserDataEnabled(int subId) {
Sooraj Sasindran5d051bf2021-11-05 13:14:15 -07007027 String functionName = "isUserDataEnabled";
Robert Greenwalt646120a2014-05-23 11:54:03 -07007028 try {
Sooraj Sasindran5d051bf2021-11-05 13:14:15 -07007029 try {
7030 mApp.enforceCallingOrSelfPermission(permission.READ_BASIC_PHONE_STATE,
7031 functionName);
Sooraj Sasindran12545cc2022-08-22 14:30:25 -07007032 } catch (SecurityException e) {
Sooraj Sasindran5d051bf2021-11-05 13:14:15 -07007033 mApp.enforceCallingOrSelfPermission(permission.ACCESS_NETWORK_STATE, functionName);
7034 }
Sooraj Sasindran12545cc2022-08-22 14:30:25 -07007035 } catch (SecurityException e) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08007036 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
Sooraj Sasindran5d051bf2021-11-05 13:14:15 -07007037 mApp, subId, functionName);
7038
Robert Greenwalt646120a2014-05-23 11:54:03 -07007039 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007040
7041 final long identity = Binder.clearCallingIdentity();
7042 try {
Jack Yu285100e2022-12-02 22:48:35 -08007043 int phoneId = SubscriptionManager.getPhoneId(subId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007044 if (DBG) log("isUserDataEnabled: subId=" + subId + " phoneId=" + phoneId);
7045 Phone phone = PhoneFactory.getPhone(phoneId);
7046 if (phone != null) {
7047 boolean retVal = phone.isUserDataEnabled();
7048 if (DBG) log("isUserDataEnabled: subId=" + subId + " retVal=" + retVal);
7049 return retVal;
7050 } else {
7051 if (DBG) loge("isUserDataEnabled: no phone subId=" + subId + " retVal=false");
7052 return false;
7053 }
7054 } finally {
7055 Binder.restoreCallingIdentity(identity);
Malcolm Chen964682d2017-11-28 16:20:07 -08007056 }
7057 }
7058
7059 /**
Shuo Qian8ee4e882020-01-08 14:30:06 -08007060 * Checks if the device is capable of mobile data by considering whether whether the
7061 * user has enabled mobile data, whether the carrier has enabled mobile data, and
7062 * whether the network policy allows data connections.
Malcolm Chen964682d2017-11-28 16:20:07 -08007063 *
Shuo Qian8ee4e882020-01-08 14:30:06 -08007064 * @return {@code true} if the overall data connection is capable; {@code false} if not.
Malcolm Chen964682d2017-11-28 16:20:07 -08007065 */
7066 @Override
7067 public boolean isDataEnabled(int subId) {
Sooraj Sasindran5d051bf2021-11-05 13:14:15 -07007068 String functionName = "isDataEnabled";
Sooraj Sasindran95c07a92020-07-15 01:35:24 -07007069 try {
7070 try {
7071 mApp.enforceCallingOrSelfPermission(
7072 android.Manifest.permission.ACCESS_NETWORK_STATE,
Sooraj Sasindran5d051bf2021-11-05 13:14:15 -07007073 functionName);
Sooraj Sasindran12545cc2022-08-22 14:30:25 -07007074 } catch (SecurityException e) {
Sooraj Sasindran5d051bf2021-11-05 13:14:15 -07007075 try {
7076 mApp.enforceCallingOrSelfPermission(
7077 android.Manifest.permission.READ_PHONE_STATE,
7078 functionName);
Sooraj Sasindran12545cc2022-08-22 14:30:25 -07007079 } catch (SecurityException e2) {
Sooraj Sasindran5d051bf2021-11-05 13:14:15 -07007080 mApp.enforceCallingOrSelfPermission(
7081 permission.READ_BASIC_PHONE_STATE, functionName);
7082 }
Sooraj Sasindran95c07a92020-07-15 01:35:24 -07007083 }
Sooraj Sasindran12545cc2022-08-22 14:30:25 -07007084 } catch (SecurityException e) {
Sooraj Sasindran5d051bf2021-11-05 13:14:15 -07007085 enforceReadPrivilegedPermission(functionName);
Sooraj Sasindran95c07a92020-07-15 01:35:24 -07007086 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007087
7088 final long identity = Binder.clearCallingIdentity();
7089 try {
Jack Yu285100e2022-12-02 22:48:35 -08007090 int phoneId = SubscriptionManager.getPhoneId(subId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007091 Phone phone = PhoneFactory.getPhone(phoneId);
7092 if (phone != null) {
Sarah Chine04784a2022-10-31 20:32:34 -07007093 boolean retVal = phone.getDataSettingsManager().isDataEnabled();
Jack Yu4ad64e52021-12-03 14:23:53 -08007094 if (DBG) log("isDataEnabled: " + retVal + ", subId=" + subId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007095 return retVal;
7096 } else {
7097 if (DBG) loge("isDataEnabled: no phone subId=" + subId + " retVal=false");
7098 return false;
7099 }
7100 } finally {
7101 Binder.restoreCallingIdentity(identity);
Wink Savillee7353bb2014-12-05 14:21:41 -08007102 }
Robert Greenwalted86e582014-05-21 20:03:20 -07007103 }
Shishir Agrawal60f9c952014-06-23 12:00:43 -07007104
Sooraj Sasindranaf1b5132020-05-05 21:06:20 +00007105 /**
7106 * Check if data is enabled for a specific reason
7107 * @param subId Subscription index
7108 * @param reason the reason the data enable change is taking place
7109 * @return {@code true} if the overall data is enabled; {@code false} if not.
7110 */
7111 @Override
Sooraj Sasindran95c07a92020-07-15 01:35:24 -07007112 public boolean isDataEnabledForReason(int subId,
Sooraj Sasindranaf1b5132020-05-05 21:06:20 +00007113 @TelephonyManager.DataEnabledReason int reason) {
Sooraj Sasindran5d051bf2021-11-05 13:14:15 -07007114 String functionName = "isDataEnabledForReason";
Sooraj Sasindranaf1b5132020-05-05 21:06:20 +00007115 try {
Sooraj Sasindran5d051bf2021-11-05 13:14:15 -07007116 try {
7117 mApp.enforceCallingOrSelfPermission(
7118 android.Manifest.permission.ACCESS_NETWORK_STATE,
7119 functionName);
Sooraj Sasindran12545cc2022-08-22 14:30:25 -07007120 } catch (SecurityException e) {
Sooraj Sasindran5d051bf2021-11-05 13:14:15 -07007121 mApp.enforceCallingOrSelfPermission(permission.READ_BASIC_PHONE_STATE,
7122 functionName);
7123 }
Sooraj Sasindran12545cc2022-08-22 14:30:25 -07007124 } catch (SecurityException e) {
Sooraj Sasindran0d909a02021-11-08 12:01:16 -08007125 try {
7126 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.READ_PHONE_STATE,
Sooraj Sasindran5d051bf2021-11-05 13:14:15 -07007127 functionName);
Sooraj Sasindran12545cc2022-08-22 14:30:25 -07007128 } catch (SecurityException e2) {
Sooraj Sasindran0d909a02021-11-08 12:01:16 -08007129 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
Sooraj Sasindran5d051bf2021-11-05 13:14:15 -07007130 mApp, subId, functionName);
Sooraj Sasindran0d909a02021-11-08 12:01:16 -08007131 }
Sooraj Sasindranaf1b5132020-05-05 21:06:20 +00007132 }
7133
7134
7135 final long identity = Binder.clearCallingIdentity();
7136 try {
Jack Yu285100e2022-12-02 22:48:35 -08007137 int phoneId = SubscriptionManager.getPhoneId(subId);
Sooraj Sasindranaf1b5132020-05-05 21:06:20 +00007138 if (DBG) {
Sooraj Sasindran95c07a92020-07-15 01:35:24 -07007139 log("isDataEnabledForReason: subId=" + subId + " phoneId=" + phoneId
Sooraj Sasindranaf1b5132020-05-05 21:06:20 +00007140 + " reason=" + reason);
7141 }
7142 Phone phone = PhoneFactory.getPhone(phoneId);
7143 if (phone != null) {
7144 boolean retVal;
Jack Yu7968c6d2022-07-31 00:43:21 -07007145 retVal = phone.getDataSettingsManager().isDataEnabledForReason(reason);
Sooraj Sasindran95c07a92020-07-15 01:35:24 -07007146 if (DBG) log("isDataEnabledForReason: retVal=" + retVal);
Sooraj Sasindranaf1b5132020-05-05 21:06:20 +00007147 return retVal;
7148 } else {
7149 if (DBG) {
Sooraj Sasindran95c07a92020-07-15 01:35:24 -07007150 loge("isDataEnabledForReason: no phone subId="
Sooraj Sasindranaf1b5132020-05-05 21:06:20 +00007151 + subId + " retVal=false");
7152 }
7153 return false;
7154 }
7155 } finally {
7156 Binder.restoreCallingIdentity(identity);
7157 }
7158 }
7159
Shishir Agrawal60f9c952014-06-23 12:00:43 -07007160 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08007161 public int getCarrierPrivilegeStatus(int subId) {
Hunter Knepshieldcad7f0b2021-12-13 15:08:35 -08007162 // No permission needed; this only lets the caller inspect their own status.
7163 return getCarrierPrivilegeStatusForUidWithPermission(subId, Binder.getCallingUid());
Shishir Agrawal60f9c952014-06-23 12:00:43 -07007164 }
Junda Liu29340342014-07-10 15:23:27 -07007165
7166 @Override
Jeff Davidson7e17e312018-02-13 18:17:36 -08007167 public int getCarrierPrivilegeStatusForUid(int subId, int uid) {
Shuo Qian2c0ae432019-12-05 11:40:37 -08007168 enforceReadPrivilegedPermission("getCarrierPrivilegeStatusForUid");
Hunter Knepshieldcad7f0b2021-12-13 15:08:35 -08007169 return getCarrierPrivilegeStatusForUidWithPermission(subId, uid);
7170 }
7171
7172 private int getCarrierPrivilegeStatusForUidWithPermission(int subId, int uid) {
7173 Phone phone = getPhone(subId);
Jeff Davidson7e17e312018-02-13 18:17:36 -08007174 if (phone == null) {
Taesu Leef8fbed92019-10-07 18:47:02 +09007175 loge("getCarrierPrivilegeStatusForUid: Invalid subId");
Jeff Davidson7e17e312018-02-13 18:17:36 -08007176 return TelephonyManager.CARRIER_PRIVILEGE_STATUS_NO_ACCESS;
7177 }
Hunter Knepshieldcad7f0b2021-12-13 15:08:35 -08007178 CarrierPrivilegesTracker cpt = phone.getCarrierPrivilegesTracker();
7179 if (cpt == null) {
7180 loge("getCarrierPrivilegeStatusForUid: No CarrierPrivilegesTracker");
Jeff Davidson7e17e312018-02-13 18:17:36 -08007181 return TelephonyManager.CARRIER_PRIVILEGE_STATUS_RULES_NOT_LOADED;
7182 }
Hunter Knepshieldcad7f0b2021-12-13 15:08:35 -08007183 return cpt.getCarrierPrivilegeStatusForUid(uid);
Jeff Davidson7e17e312018-02-13 18:17:36 -08007184 }
7185
7186 @Override
chen xuf7e9fe82019-05-09 19:31:02 -07007187 public int checkCarrierPrivilegesForPackage(int subId, String pkgName) {
Nazanin1adf4562021-03-29 15:35:30 -07007188 enforceReadPrivilegedPermission("checkCarrierPrivilegesForPackage");
chen xuf7e9fe82019-05-09 19:31:02 -07007189 if (TextUtils.isEmpty(pkgName)) {
Junda Liu317d70b2016-03-08 09:33:53 -08007190 return TelephonyManager.CARRIER_PRIVILEGE_STATUS_NO_ACCESS;
chen xuf7e9fe82019-05-09 19:31:02 -07007191 }
Hunter Knepshieldcad7f0b2021-12-13 15:08:35 -08007192 Phone phone = getPhone(subId);
7193 if (phone == null) {
7194 loge("checkCarrierPrivilegesForPackage: Invalid subId");
7195 return TelephonyManager.CARRIER_PRIVILEGE_STATUS_NO_ACCESS;
7196 }
7197 CarrierPrivilegesTracker cpt = phone.getCarrierPrivilegesTracker();
7198 if (cpt == null) {
7199 loge("checkCarrierPrivilegesForPackage: No CarrierPrivilegesTracker");
Shishir Agrawaleb8771e2014-07-22 11:24:08 -07007200 return TelephonyManager.CARRIER_PRIVILEGE_STATUS_RULES_NOT_LOADED;
7201 }
Hunter Knepshieldcad7f0b2021-12-13 15:08:35 -08007202 return cpt.getCarrierPrivilegeStatusForPackage(pkgName);
Zach Johnson50ecba32015-05-19 00:24:21 -07007203 }
7204
7205 @Override
7206 public int checkCarrierPrivilegesForPackageAnyPhone(String pkgName) {
Hunter Knepshieldcad7f0b2021-12-13 15:08:35 -08007207 enforceReadPrivilegedPermission("checkCarrierPrivilegesForPackageAnyPhone");
Rambo Wange7209ce2022-02-23 13:41:02 -08007208 return checkCarrierPrivilegesForPackageAnyPhoneWithPermission(pkgName);
7209 }
7210
7211 private int checkCarrierPrivilegesForPackageAnyPhoneWithPermission(String pkgName) {
Hunter Knepshieldcad7f0b2021-12-13 15:08:35 -08007212 if (TextUtils.isEmpty(pkgName)) {
Junda Liu317d70b2016-03-08 09:33:53 -08007213 return TelephonyManager.CARRIER_PRIVILEGE_STATUS_NO_ACCESS;
Hunter Knepshieldcad7f0b2021-12-13 15:08:35 -08007214 }
Zach Johnson50ecba32015-05-19 00:24:21 -07007215 int result = TelephonyManager.CARRIER_PRIVILEGE_STATUS_RULES_NOT_LOADED;
Hunter Knepshieldcad7f0b2021-12-13 15:08:35 -08007216 for (int phoneId = 0; phoneId < TelephonyManager.getDefault().getPhoneCount(); phoneId++) {
7217 Phone phone = PhoneFactory.getPhone(phoneId);
7218 if (phone == null) {
7219 continue;
Zach Johnson50ecba32015-05-19 00:24:21 -07007220 }
Hunter Knepshieldcad7f0b2021-12-13 15:08:35 -08007221 CarrierPrivilegesTracker cpt = phone.getCarrierPrivilegesTracker();
7222 if (cpt == null) {
7223 continue;
7224 }
7225 result = cpt.getCarrierPrivilegeStatusForPackage(pkgName);
Zach Johnson50ecba32015-05-19 00:24:21 -07007226 if (result == TelephonyManager.CARRIER_PRIVILEGE_STATUS_HAS_ACCESS) {
7227 break;
7228 }
7229 }
Zach Johnson50ecba32015-05-19 00:24:21 -07007230 return result;
Junda Liu29340342014-07-10 15:23:27 -07007231 }
Derek Tan89e89d42014-07-08 17:00:10 -07007232
7233 @Override
Junda Liue64de782015-04-16 17:19:16 -07007234 public List<String> getCarrierPackageNamesForIntentAndPhone(Intent intent, int phoneId) {
Nazanin1adf4562021-03-29 15:35:30 -07007235 enforceReadPrivilegedPermission("getCarrierPackageNamesForIntentAndPhone");
Rambo Wang8a247eb2022-02-08 21:11:18 +00007236 Phone phone = PhoneFactory.getPhone(phoneId);
7237 if (phone == null) {
7238 return Collections.emptyList();
Junda Liue64de782015-04-16 17:19:16 -07007239 }
Rambo Wang8a247eb2022-02-08 21:11:18 +00007240 CarrierPrivilegesTracker cpt = phone.getCarrierPrivilegesTracker();
7241 if (cpt == null) {
7242 return Collections.emptyList();
Shishir Agrawaleb6439a2014-07-21 13:19:38 -07007243 }
Rambo Wang8a247eb2022-02-08 21:11:18 +00007244 return cpt.getCarrierPackageNamesForIntent(intent);
Shishir Agrawaleb6439a2014-07-21 13:19:38 -07007245 }
7246
Amith Yamasani6e118872016-02-19 12:53:51 -08007247 @Override
chen xuf7e9fe82019-05-09 19:31:02 -07007248 public List<String> getPackagesWithCarrierPrivileges(int phoneId) {
Nazanin1adf4562021-03-29 15:35:30 -07007249 enforceReadPrivilegedPermission("getPackagesWithCarrierPrivileges");
Hunter Knepshieldcad7f0b2021-12-13 15:08:35 -08007250 Phone phone = PhoneFactory.getPhone(phoneId);
7251 if (phone == null) {
7252 return Collections.emptyList();
Amith Yamasani6e118872016-02-19 12:53:51 -08007253 }
Hunter Knepshieldcad7f0b2021-12-13 15:08:35 -08007254 CarrierPrivilegesTracker cpt = phone.getCarrierPrivilegesTracker();
7255 if (cpt == null) {
7256 return Collections.emptyList();
7257 }
7258 return new ArrayList<>(cpt.getPackagesWithCarrierPrivileges());
Amith Yamasani6e118872016-02-19 12:53:51 -08007259 }
7260
chen xuf7e9fe82019-05-09 19:31:02 -07007261 @Override
7262 public List<String> getPackagesWithCarrierPrivilegesForAllPhones() {
Shuo Qian067a06d2019-12-03 23:40:18 +00007263 enforceReadPrivilegedPermission("getPackagesWithCarrierPrivilegesForAllPhones");
Hunter Knepshieldcad7f0b2021-12-13 15:08:35 -08007264 Set<String> privilegedPackages = new ArraySet<>();
Shuo Qian067a06d2019-12-03 23:40:18 +00007265 final long identity = Binder.clearCallingIdentity();
Shuo Qian067a06d2019-12-03 23:40:18 +00007266 try {
7267 for (int i = 0; i < TelephonyManager.getDefault().getPhoneCount(); i++) {
7268 privilegedPackages.addAll(getPackagesWithCarrierPrivileges(i));
7269 }
7270 } finally {
7271 Binder.restoreCallingIdentity(identity);
chen xuf7e9fe82019-05-09 19:31:02 -07007272 }
Hunter Knepshieldcad7f0b2021-12-13 15:08:35 -08007273 return new ArrayList<>(privilegedPackages);
chen xuf7e9fe82019-05-09 19:31:02 -07007274 }
7275
Rambo Wang6812ffb2022-03-15 16:54:17 -07007276 @Override
7277 public @Nullable String getCarrierServicePackageNameForLogicalSlot(int logicalSlotIndex) {
7278 enforceReadPrivilegedPermission("getCarrierServicePackageNameForLogicalSlot");
7279
7280 final Phone phone = PhoneFactory.getPhone(logicalSlotIndex);
7281 if (phone == null) {
7282 return null;
7283 }
7284 final CarrierPrivilegesTracker cpt = phone.getCarrierPrivilegesTracker();
7285 if (cpt == null) {
7286 return null;
7287 }
7288 return cpt.getCarrierServicePackageName();
7289 }
7290
Wink Savilleb564aae2014-10-23 10:18:09 -07007291 private String getIccId(int subId) {
Sanket Padawe356d7632015-06-22 14:03:32 -07007292 final Phone phone = getPhone(subId);
Muralidhar Reddy472c2ae2021-09-29 19:38:40 +00007293 UiccPort port = phone == null ? null : phone.getUiccPort();
7294 if (port == null) {
Derek Tan97ebb422014-09-05 16:55:38 -07007295 return null;
7296 }
Muralidhar Reddy472c2ae2021-09-29 19:38:40 +00007297 String iccId = port.getIccId();
Derek Tan97ebb422014-09-05 16:55:38 -07007298 if (TextUtils.isEmpty(iccId)) {
Derek Tan97ebb422014-09-05 16:55:38 -07007299 return null;
7300 }
7301 return iccId;
7302 }
7303
Shishir Agrawaleb6439a2014-07-21 13:19:38 -07007304 @Override
Shuo Qiane4e11672020-12-15 22:15:33 -08007305 public void setCallComposerStatus(int subId, int status) {
7306 enforceModifyPermission();
7307
7308 final long identity = Binder.clearCallingIdentity();
7309 try {
7310 Phone phone = getPhone(subId);
7311 if (phone != null) {
7312 Phone defaultPhone = phone.getImsPhone();
7313 if (defaultPhone != null && defaultPhone.getPhoneType() == PHONE_TYPE_IMS) {
7314 ImsPhone imsPhone = (ImsPhone) defaultPhone;
7315 imsPhone.setCallComposerStatus(status);
Shuo Qian284ae752020-12-22 19:10:14 -08007316 ImsManager.getInstance(mApp, getSlotIndexOrException(subId))
7317 .updateImsServiceConfig();
Shuo Qiane4e11672020-12-15 22:15:33 -08007318 }
7319 }
Shuo Qian284ae752020-12-22 19:10:14 -08007320 } catch (ImsException e) {
7321 throw new ServiceSpecificException(e.getCode());
7322 } finally {
Shuo Qiane4e11672020-12-15 22:15:33 -08007323 Binder.restoreCallingIdentity(identity);
7324 }
7325 }
7326
7327 @Override
7328 public int getCallComposerStatus(int subId) {
7329 enforceReadPrivilegedPermission("getCallComposerStatus");
7330
7331 final long identity = Binder.clearCallingIdentity();
7332 try {
7333 Phone phone = getPhone(subId);
7334 if (phone != null) {
7335 Phone defaultPhone = phone.getImsPhone();
7336 if (defaultPhone != null && defaultPhone.getPhoneType() == PHONE_TYPE_IMS) {
7337 ImsPhone imsPhone = (ImsPhone) defaultPhone;
7338 return imsPhone.getCallComposerStatus();
7339 }
7340 }
7341 } finally {
7342 Binder.restoreCallingIdentity(identity);
7343 }
7344 return TelephonyManager.CALL_COMPOSER_STATUS_OFF;
7345 }
7346
7347 @Override
Jeff Sharkey85190e62014-12-05 09:40:12 -08007348 public boolean setLine1NumberForDisplayForSubscriber(int subId, String alphaTag,
7349 String number) {
Shuo Qian2c0ae432019-12-05 11:40:37 -08007350 TelephonyPermissions.enforceCallingOrSelfCarrierPrivilege(mApp,
Jeff Davidson7e17e312018-02-13 18:17:36 -08007351 subId, "setLine1NumberForDisplayForSubscriber");
Derek Tan97ebb422014-09-05 16:55:38 -07007352
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007353 final long identity = Binder.clearCallingIdentity();
7354 try {
7355 final String iccId = getIccId(subId);
7356 final Phone phone = getPhone(subId);
7357 if (phone == null) {
7358 return false;
7359 }
7360 final String subscriberId = phone.getSubscriberId();
7361
7362 if (DBG_MERGE) {
Amit Mahajanb8f13202020-01-27 18:16:07 -08007363 Rlog.d(LOG_TAG, "Setting line number for ICC=" + iccId + ", subscriberId="
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007364 + subscriberId + " to " + number);
7365 }
7366
7367 if (TextUtils.isEmpty(iccId)) {
7368 return false;
7369 }
7370
7371 final SharedPreferences.Editor editor = mTelephonySharedPreferences.edit();
7372
7373 final String alphaTagPrefKey = PREF_CARRIERS_ALPHATAG_PREFIX + iccId;
7374 if (alphaTag == null) {
7375 editor.remove(alphaTagPrefKey);
7376 } else {
7377 editor.putString(alphaTagPrefKey, alphaTag);
7378 }
7379
7380 // Record both the line number and IMSI for this ICCID, since we need to
7381 // track all merged IMSIs based on line number
7382 final String numberPrefKey = PREF_CARRIERS_NUMBER_PREFIX + iccId;
7383 final String subscriberPrefKey = PREF_CARRIERS_SUBSCRIBER_PREFIX + iccId;
7384 if (number == null) {
7385 editor.remove(numberPrefKey);
7386 editor.remove(subscriberPrefKey);
7387 } else {
7388 editor.putString(numberPrefKey, number);
7389 editor.putString(subscriberPrefKey, subscriberId);
7390 }
7391
7392 editor.commit();
7393 return true;
7394 } finally {
7395 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07007396 }
Derek Tan7226c842014-07-02 17:42:23 -07007397 }
7398
7399 @Override
Philip P. Moltmann700a9592019-10-03 11:53:50 -07007400 public String getLine1NumberForDisplay(int subId, String callingPackage,
7401 String callingFeatureId) {
Makoto Onukifee69342015-06-29 14:44:50 -07007402 // This is open to apps with WRITE_SMS.
Jeff Davidson7e17e312018-02-13 18:17:36 -08007403 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneNumber(
Philip P. Moltmann700a9592019-10-03 11:53:50 -07007404 mApp, subId, callingPackage, callingFeatureId, "getLine1NumberForDisplay")) {
Amit Mahajan9cf11512015-11-09 11:40:48 -08007405 if (DBG_MERGE) log("getLine1NumberForDisplay returning null due to permission");
Svet Ganovb320e182015-04-16 12:30:10 -07007406 return null;
7407 }
Derek Tan97ebb422014-09-05 16:55:38 -07007408
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007409 final long identity = Binder.clearCallingIdentity();
7410 try {
7411 String iccId = getIccId(subId);
7412 if (iccId != null) {
7413 String numberPrefKey = PREF_CARRIERS_NUMBER_PREFIX + iccId;
7414 if (DBG_MERGE) {
7415 log("getLine1NumberForDisplay returning "
7416 + mTelephonySharedPreferences.getString(numberPrefKey, null));
7417 }
7418 return mTelephonySharedPreferences.getString(numberPrefKey, null);
Amit Mahajan9cf11512015-11-09 11:40:48 -08007419 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007420 if (DBG_MERGE) log("getLine1NumberForDisplay returning null as iccId is null");
7421 return null;
7422 } finally {
7423 Binder.restoreCallingIdentity(identity);
Derek Tan7226c842014-07-02 17:42:23 -07007424 }
Derek Tan7226c842014-07-02 17:42:23 -07007425 }
7426
7427 @Override
Philip P. Moltmann700a9592019-10-03 11:53:50 -07007428 public String getLine1AlphaTagForDisplay(int subId, String callingPackage,
7429 String callingFeatureId) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08007430 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
Philip P. Moltmann700a9592019-10-03 11:53:50 -07007431 mApp, subId, callingPackage, callingFeatureId, "getLine1AlphaTagForDisplay")) {
Svet Ganovb320e182015-04-16 12:30:10 -07007432 return null;
7433 }
Derek Tan97ebb422014-09-05 16:55:38 -07007434
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007435 final long identity = Binder.clearCallingIdentity();
7436 try {
7437 String iccId = getIccId(subId);
7438 if (iccId != null) {
7439 String alphaTagPrefKey = PREF_CARRIERS_ALPHATAG_PREFIX + iccId;
7440 return mTelephonySharedPreferences.getString(alphaTagPrefKey, null);
7441 }
7442 return null;
7443 } finally {
7444 Binder.restoreCallingIdentity(identity);
Derek Tan7226c842014-07-02 17:42:23 -07007445 }
Derek Tan7226c842014-07-02 17:42:23 -07007446 }
Shishir Agrawalb1ebf8c2014-07-17 16:32:41 -07007447
7448 @Override
Philip P. Moltmann700a9592019-10-03 11:53:50 -07007449 public String[] getMergedSubscriberIds(int subId, String callingPackage,
7450 String callingFeatureId) {
Jeff Davidson913390f2018-02-23 17:11:49 -08007451 // This API isn't public, so no need to provide a valid subscription ID - we're not worried
7452 // about carrier-privileged callers not having access.
Jeff Davidson7e17e312018-02-13 18:17:36 -08007453 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
Jeff Davidson913390f2018-02-23 17:11:49 -08007454 mApp, SubscriptionManager.INVALID_SUBSCRIPTION_ID, callingPackage,
Philip P. Moltmann700a9592019-10-03 11:53:50 -07007455 callingFeatureId, "getMergedSubscriberIds")) {
Fyodor Kupolov8e53b0b2015-06-17 13:17:50 -07007456 return null;
7457 }
Jeff Sharkey85190e62014-12-05 09:40:12 -08007458
Jordan Liub49b04b2019-05-06 14:45:15 -07007459 // Clear calling identity, when calling TelephonyManager, because callerUid must be
7460 // the process, where TelephonyManager was instantiated.
7461 // Otherwise AppOps check will fail.
Fyodor Kupolov8e53b0b2015-06-17 13:17:50 -07007462 final long identity = Binder.clearCallingIdentity();
7463 try {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08007464 final Context context = mApp;
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007465 final TelephonyManager tele = TelephonyManager.from(context);
7466 final SubscriptionManager sub = SubscriptionManager.from(context);
7467
7468 // Figure out what subscribers are currently active
7469 final ArraySet<String> activeSubscriberIds = new ArraySet<>();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007470
Jordan Liub49b04b2019-05-06 14:45:15 -07007471 // Only consider subs which match the current subId
7472 // This logic can be simplified. See b/131189269 for progress.
7473 if (isActiveSubscription(subId)) {
Fyodor Kupolov8e53b0b2015-06-17 13:17:50 -07007474 activeSubscriberIds.add(tele.getSubscriberId(subId));
7475 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007476
7477 // First pass, find a number override for an active subscriber
7478 String mergeNumber = null;
7479 final Map<String, ?> prefs = mTelephonySharedPreferences.getAll();
7480 for (String key : prefs.keySet()) {
7481 if (key.startsWith(PREF_CARRIERS_SUBSCRIBER_PREFIX)) {
7482 final String subscriberId = (String) prefs.get(key);
7483 if (activeSubscriberIds.contains(subscriberId)) {
7484 final String iccId = key.substring(
7485 PREF_CARRIERS_SUBSCRIBER_PREFIX.length());
7486 final String numberKey = PREF_CARRIERS_NUMBER_PREFIX + iccId;
7487 mergeNumber = (String) prefs.get(numberKey);
7488 if (DBG_MERGE) {
Amit Mahajanb8f13202020-01-27 18:16:07 -08007489 Rlog.d(LOG_TAG, "Found line number " + mergeNumber
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007490 + " for active subscriber " + subscriberId);
7491 }
7492 if (!TextUtils.isEmpty(mergeNumber)) {
7493 break;
7494 }
7495 }
7496 }
7497 }
7498
7499 // Shortcut when no active merged subscribers
7500 if (TextUtils.isEmpty(mergeNumber)) {
7501 return null;
7502 }
7503
7504 // Second pass, find all subscribers under that line override
7505 final ArraySet<String> result = new ArraySet<>();
7506 for (String key : prefs.keySet()) {
7507 if (key.startsWith(PREF_CARRIERS_NUMBER_PREFIX)) {
7508 final String number = (String) prefs.get(key);
7509 if (mergeNumber.equals(number)) {
7510 final String iccId = key.substring(PREF_CARRIERS_NUMBER_PREFIX.length());
7511 final String subscriberKey = PREF_CARRIERS_SUBSCRIBER_PREFIX + iccId;
7512 final String subscriberId = (String) prefs.get(subscriberKey);
7513 if (!TextUtils.isEmpty(subscriberId)) {
7514 result.add(subscriberId);
7515 }
7516 }
7517 }
7518 }
7519
7520 final String[] resultArray = result.toArray(new String[result.size()]);
7521 Arrays.sort(resultArray);
7522 if (DBG_MERGE) {
Amit Mahajanb8f13202020-01-27 18:16:07 -08007523 Rlog.d(LOG_TAG,
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007524 "Found subscribers " + Arrays.toString(resultArray) + " after merge");
7525 }
7526 return resultArray;
Fyodor Kupolov8e53b0b2015-06-17 13:17:50 -07007527 } finally {
7528 Binder.restoreCallingIdentity(identity);
Jeff Sharkey85190e62014-12-05 09:40:12 -08007529 }
Jeff Sharkey85190e62014-12-05 09:40:12 -08007530 }
7531
7532 @Override
zoey chen38003472019-12-13 17:16:31 +08007533 public String[] getMergedImsisFromGroup(int subId, String callingPackage) {
7534 enforceReadPrivilegedPermission("getMergedImsisFromGroup");
Malcolm Chen6ca97372019-07-01 16:28:21 -07007535
7536 final long identity = Binder.clearCallingIdentity();
7537 try {
7538 final TelephonyManager telephonyManager = mApp.getSystemService(
7539 TelephonyManager.class);
7540 String subscriberId = telephonyManager.getSubscriberId(subId);
7541 if (subscriberId == null) {
7542 if (DBG) {
zoey chen38003472019-12-13 17:16:31 +08007543 log("getMergedImsisFromGroup can't find subscriberId for subId "
Malcolm Chen6ca97372019-07-01 16:28:21 -07007544 + subId);
7545 }
7546 return null;
7547 }
7548
Jack Yu285100e2022-12-02 22:48:35 -08007549 ParcelUuid groupUuid;
7550 if (PhoneFactory.isSubscriptionManagerServiceEnabled()) {
7551 final SubscriptionInfo info = SubscriptionManagerService.getInstance()
7552 .getSubscriptionInfo(subId);
7553 groupUuid = info.getGroupUuid();
7554 } else {
7555 final SubscriptionInfo info = mSubscriptionController
7556 .getSubscriptionInfo(subId);
7557 groupUuid = info.getGroupUuid();
7558 }
Malcolm Chen6ca97372019-07-01 16:28:21 -07007559 // If it doesn't belong to any group, return just subscriberId of itself.
7560 if (groupUuid == null) {
7561 return new String[]{subscriberId};
7562 }
7563
7564 // Get all subscriberIds from the group.
7565 final List<String> mergedSubscriberIds = new ArrayList<>();
Jack Yu285100e2022-12-02 22:48:35 -08007566 List<SubscriptionInfo> groupInfos;
7567 if (PhoneFactory.isSubscriptionManagerServiceEnabled()) {
7568 groupInfos = SubscriptionManagerService.getInstance()
7569 .getSubscriptionsInGroup(groupUuid, mApp.getOpPackageName(),
7570 mApp.getAttributionTag());
7571 } else {
7572 groupInfos = mSubscriptionController
7573 .getSubscriptionsInGroup(groupUuid, mApp.getOpPackageName(),
7574 mApp.getAttributionTag());
7575 }
Malcolm Chen6ca97372019-07-01 16:28:21 -07007576 for (SubscriptionInfo subInfo : groupInfos) {
7577 subscriberId = telephonyManager.getSubscriberId(subInfo.getSubscriptionId());
7578 if (subscriberId != null) {
7579 mergedSubscriberIds.add(subscriberId);
7580 }
7581 }
7582
7583 return mergedSubscriberIds.toArray(new String[mergedSubscriberIds.size()]);
7584 } finally {
7585 Binder.restoreCallingIdentity(identity);
7586
7587 }
7588 }
7589
7590 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08007591 public boolean setOperatorBrandOverride(int subId, String brand) {
Shuo Qian2c0ae432019-12-05 11:40:37 -08007592 TelephonyPermissions.enforceCallingOrSelfCarrierPrivilege(mApp,
Jeff Davidson7e17e312018-02-13 18:17:36 -08007593 subId, "setOperatorBrandOverride");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007594
7595 final long identity = Binder.clearCallingIdentity();
7596 try {
7597 final Phone phone = getPhone(subId);
7598 return phone == null ? false : phone.setOperatorBrandOverride(brand);
7599 } finally {
7600 Binder.restoreCallingIdentity(identity);
7601 }
Shishir Agrawalb1ebf8c2014-07-17 16:32:41 -07007602 }
Steven Liu4bf01bc2014-07-17 11:05:29 -05007603
7604 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08007605 public boolean setRoamingOverride(int subId, List<String> gsmRoamingList,
Shishir Agrawal621a47c2014-12-01 10:25:09 -08007606 List<String> gsmNonRoamingList, List<String> cdmaRoamingList,
7607 List<String> cdmaNonRoamingList) {
Shuo Qian2c0ae432019-12-05 11:40:37 -08007608 TelephonyPermissions.enforceCallingOrSelfCarrierPrivilege(
7609 mApp, subId, "setRoamingOverride");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007610
7611 final long identity = Binder.clearCallingIdentity();
7612 try {
7613 final Phone phone = getPhone(subId);
7614 if (phone == null) {
7615 return false;
7616 }
7617 return phone.setRoamingOverride(gsmRoamingList, gsmNonRoamingList, cdmaRoamingList,
7618 cdmaNonRoamingList);
7619 } finally {
7620 Binder.restoreCallingIdentity(identity);
Shishir Agrawalc04d9752016-02-19 10:41:00 -08007621 }
Shishir Agrawal621a47c2014-12-01 10:25:09 -08007622 }
7623
7624 @Override
Shuo Qian850e4d6a2018-04-25 21:02:08 +00007625 @Deprecated
7626 public int invokeOemRilRequestRaw(byte[] oemReq, byte[] oemResp) {
7627 enforceModifyPermission();
7628
7629 int returnValue = 0;
7630 try {
vagdeviaf9a5b92018-08-15 16:01:53 -07007631 AsyncResult result = (AsyncResult) sendRequest(CMD_INVOKE_OEM_RIL_REQUEST_RAW, oemReq);
Shuo Qian850e4d6a2018-04-25 21:02:08 +00007632 if(result.exception == null) {
7633 if (result.result != null) {
7634 byte[] responseData = (byte[])(result.result);
7635 if(responseData.length > oemResp.length) {
7636 Log.w(LOG_TAG, "Buffer to copy response too small: Response length is " +
7637 responseData.length + "bytes. Buffer Size is " +
7638 oemResp.length + "bytes.");
7639 }
7640 System.arraycopy(responseData, 0, oemResp, 0, responseData.length);
7641 returnValue = responseData.length;
7642 }
7643 } else {
7644 CommandException ex = (CommandException) result.exception;
7645 returnValue = ex.getCommandError().ordinal();
7646 if(returnValue > 0) returnValue *= -1;
7647 }
7648 } catch (RuntimeException e) {
7649 Log.w(LOG_TAG, "sendOemRilRequestRaw: Runtime Exception");
7650 returnValue = (CommandException.Error.GENERIC_FAILURE.ordinal());
7651 if(returnValue > 0) returnValue *= -1;
7652 }
7653
7654 return returnValue;
7655 }
7656
7657 @Override
Robert Greenwalt36b23af2015-07-06 17:59:14 -07007658 public int getRadioAccessFamily(int phoneId, String callingPackage) {
Jeff Davidson913390f2018-02-23 17:11:49 -08007659 Phone phone = PhoneFactory.getPhone(phoneId);
Shuo Qiandee53402020-05-29 14:08:15 -07007660 try {
7661 TelephonyPermissions
Nathan Harold62c68512021-04-06 11:26:02 -07007662 .enforceCallingOrSelfReadPrivilegedPhoneStatePermissionOrCarrierPrivilege(
Shuo Qiandee53402020-05-29 14:08:15 -07007663 mApp, phone.getSubId(), "getRadioAccessFamily");
7664 } catch (SecurityException e) {
7665 EventLog.writeEvent(0x534e4554, "150857259", -1, "Missing Permission");
7666 throw e;
7667 }
chen xub97461a2018-10-26 14:17:57 -07007668 int raf = RadioAccessFamily.RAF_UNKNOWN;
Jeff Davidson913390f2018-02-23 17:11:49 -08007669 if (phone == null) {
chen xub97461a2018-10-26 14:17:57 -07007670 return raf;
Jeff Davidson913390f2018-02-23 17:11:49 -08007671 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007672 final long identity = Binder.clearCallingIdentity();
7673 try {
chen xub97461a2018-10-26 14:17:57 -07007674 TelephonyPermissions
Nathan Harold62c68512021-04-06 11:26:02 -07007675 .enforceCallingOrSelfReadPrivilegedPhoneStatePermissionOrCarrierPrivilege(
chen xub97461a2018-10-26 14:17:57 -07007676 mApp, phone.getSubId(), "getRadioAccessFamily");
7677 raf = ProxyController.getInstance().getRadioAccessFamily(phoneId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007678 } finally {
7679 Binder.restoreCallingIdentity(identity);
7680 }
chen xub97461a2018-10-26 14:17:57 -07007681 return raf;
Wink Saville5d475dd2014-10-17 15:00:58 -07007682 }
Andrew Leedf14ead2014-10-17 14:22:52 -07007683
7684 @Override
Hall Liu82694d52020-12-11 18:22:04 -08007685 public void uploadCallComposerPicture(int subscriptionId, String callingPackage,
Hall Liue31bac62020-12-23 19:16:10 -08007686 String contentType, ParcelFileDescriptor fd, ResultReceiver callback) {
Hall Liu82694d52020-12-11 18:22:04 -08007687 try {
7688 if (!Objects.equals(mApp.getPackageManager().getPackageUid(callingPackage, 0),
7689 Binder.getCallingUid())) {
Tyler Gunnb87925a2021-06-10 14:54:27 -07007690 throw new SecurityException("Invalid package:" + callingPackage);
Hall Liu82694d52020-12-11 18:22:04 -08007691 }
7692 } catch (PackageManager.NameNotFoundException e) {
Tyler Gunnb87925a2021-06-10 14:54:27 -07007693 throw new SecurityException("Invalid package:" + callingPackage);
Hall Liu82694d52020-12-11 18:22:04 -08007694 }
7695 RoleManager rm = mApp.getSystemService(RoleManager.class);
7696 List<String> dialerRoleHolders = rm.getRoleHolders(RoleManager.ROLE_DIALER);
7697 if (!dialerRoleHolders.contains(callingPackage)) {
7698 throw new SecurityException("App must be the dialer role holder to"
7699 + " upload a call composer pic");
7700 }
7701
7702 Executors.newSingleThreadExecutor().execute(() -> {
7703 ByteArrayOutputStream output = new ByteArrayOutputStream(
7704 (int) TelephonyManager.getMaximumCallComposerPictureSize());
7705 InputStream input = new ParcelFileDescriptor.AutoCloseInputStream(fd);
7706 boolean readUntilEnd = false;
7707 int totalBytesRead = 0;
7708 byte[] buffer = new byte[16 * 1024];
7709 while (true) {
7710 int numRead;
7711 try {
7712 numRead = input.read(buffer);
7713 } catch (IOException e) {
7714 try {
7715 fd.checkError();
7716 callback.send(TelephonyManager.CallComposerException.ERROR_INPUT_CLOSED,
7717 null);
7718 } catch (IOException e1) {
7719 // This means that the other side closed explicitly with an error. If this
7720 // happens, log and ignore.
7721 loge("Remote end of call composer picture pipe closed: " + e1);
7722 }
7723 break;
7724 }
7725 if (numRead == -1) {
7726 readUntilEnd = true;
7727 break;
7728 }
7729 totalBytesRead += numRead;
7730 if (totalBytesRead > TelephonyManager.getMaximumCallComposerPictureSize()) {
7731 loge("Too many bytes read for call composer picture: " + totalBytesRead);
7732 try {
7733 input.close();
7734 } catch (IOException e) {
7735 // ignore
7736 }
7737 break;
7738 }
7739 output.write(buffer, 0, numRead);
7740 }
7741 // Generally, the remote end will close the file descriptors. The only case where we
7742 // close is above, where the picture size is too big.
7743
7744 try {
7745 fd.checkError();
7746 } catch (IOException e) {
7747 loge("Remote end for call composer closed with an error: " + e);
7748 return;
7749 }
7750
Hall Liuaa4211e2021-01-20 15:43:39 -08007751 if (!readUntilEnd) {
7752 loge("Did not finish reading entire image; aborting");
7753 return;
7754 }
Hall Liu82694d52020-12-11 18:22:04 -08007755
Hall Liuaa4211e2021-01-20 15:43:39 -08007756 ImageData imageData = new ImageData(output.toByteArray(), contentType, null);
7757 CallComposerPictureManager.getInstance(mApp, subscriptionId).handleUploadToServer(
7758 new CallComposerPictureTransfer.Factory() {},
7759 imageData,
7760 (result) -> {
7761 if (result.first != null) {
7762 ParcelUuid parcelUuid = new ParcelUuid(result.first);
7763 Bundle outputResult = new Bundle();
7764 outputResult.putParcelable(
7765 TelephonyManager.KEY_CALL_COMPOSER_PICTURE_HANDLE, parcelUuid);
7766 callback.send(TelephonyManager.CallComposerException.SUCCESS,
7767 outputResult);
7768 } else {
7769 callback.send(result.second, null);
7770 }
7771 }
7772 );
Hall Liu82694d52020-12-11 18:22:04 -08007773 });
7774 }
7775
7776 @Override
Andrew Leedf14ead2014-10-17 14:22:52 -07007777 public void enableVideoCalling(boolean enable) {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08007778 final Phone defaultPhone = getDefaultPhone();
Andrew Leedf14ead2014-10-17 14:22:52 -07007779 enforceModifyPermission();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007780
7781 final long identity = Binder.clearCallingIdentity();
7782 try {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08007783 ImsManager.getInstance(defaultPhone.getContext(),
7784 defaultPhone.getPhoneId()).setVtSetting(enable);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007785 } finally {
7786 Binder.restoreCallingIdentity(identity);
7787 }
Andrew Leedf14ead2014-10-17 14:22:52 -07007788 }
7789
7790 @Override
Philip P. Moltmann700a9592019-10-03 11:53:50 -07007791 public boolean isVideoCallingEnabled(String callingPackage, String callingFeatureId) {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08007792 final Phone defaultPhone = getDefaultPhone();
Philip P. Moltmann700a9592019-10-03 11:53:50 -07007793 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(mApp, defaultPhone.getSubId(),
7794 callingPackage, callingFeatureId, "isVideoCallingEnabled")) {
Amit Mahajan578e53d2018-03-20 16:18:38 +00007795 return false;
7796 }
Svet Ganovb320e182015-04-16 12:30:10 -07007797
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007798 final long identity = Binder.clearCallingIdentity();
7799 try {
7800 // Check the user preference and the system-level IMS setting. Even if the user has
7801 // enabled video calling, if IMS is disabled we aren't able to support video calling.
7802 // In the long run, we may instead need to check if there exists a connection service
7803 // which can support video calling.
7804 ImsManager imsManager =
Nazanin Bakhshif782e562018-12-11 15:15:39 -08007805 ImsManager.getInstance(defaultPhone.getContext(), defaultPhone.getPhoneId());
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007806 return imsManager.isVtEnabledByPlatform()
7807 && imsManager.isEnhanced4gLteModeSettingEnabledByUser()
7808 && imsManager.isVtEnabledByUser();
7809 } finally {
7810 Binder.restoreCallingIdentity(identity);
7811 }
Andrew Leedf14ead2014-10-17 14:22:52 -07007812 }
Libin.Tang@motorola.comafe82642014-12-18 13:27:53 -06007813
Andrew Leea1239f22015-03-02 17:44:07 -08007814 @Override
Philip P. Moltmann700a9592019-10-03 11:53:50 -07007815 public boolean canChangeDtmfToneLength(int subId, String callingPackage,
7816 String callingFeatureId) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007817 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
Philip P. Moltmann700a9592019-10-03 11:53:50 -07007818 mApp, subId, callingPackage, callingFeatureId,
7819 "isVideoCallingEnabled")) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007820 return false;
7821 }
7822
7823 final long identity = Binder.clearCallingIdentity();
7824 try {
7825 CarrierConfigManager configManager =
7826 (CarrierConfigManager) mApp.getSystemService(Context.CARRIER_CONFIG_SERVICE);
Nazanin Bakhshif782e562018-12-11 15:15:39 -08007827 return configManager.getConfigForSubId(subId)
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007828 .getBoolean(CarrierConfigManager.KEY_DTMF_TYPE_ENABLED_BOOL);
7829 } finally {
7830 Binder.restoreCallingIdentity(identity);
7831 }
Andrew Leea1239f22015-03-02 17:44:07 -08007832 }
7833
7834 @Override
Philip P. Moltmann700a9592019-10-03 11:53:50 -07007835 public boolean isWorldPhone(int subId, String callingPackage, String callingFeatureId) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007836 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
Philip P. Moltmann700a9592019-10-03 11:53:50 -07007837 mApp, subId, callingPackage, callingFeatureId, "isVideoCallingEnabled")) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007838 return false;
7839 }
7840
7841 final long identity = Binder.clearCallingIdentity();
7842 try {
7843 CarrierConfigManager configManager =
7844 (CarrierConfigManager) mApp.getSystemService(Context.CARRIER_CONFIG_SERVICE);
Nazanin Bakhshif782e562018-12-11 15:15:39 -08007845 return configManager.getConfigForSubId(subId)
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007846 .getBoolean(CarrierConfigManager.KEY_WORLD_PHONE_BOOL);
7847 } finally {
7848 Binder.restoreCallingIdentity(identity);
7849 }
Andrew Leea1239f22015-03-02 17:44:07 -08007850 }
7851
Andrew Lee9431b832015-03-09 18:46:45 -07007852 @Override
7853 public boolean isTtyModeSupported() {
Tyler Gunn5ddfdc92019-10-31 13:08:23 -07007854 TelecomManager telecomManager = mApp.getSystemService(TelecomManager.class);
Wooki Wu1f82f7a2016-02-15 15:59:58 +08007855 return telecomManager.isTtySupported();
Andrew Lee9431b832015-03-09 18:46:45 -07007856 }
7857
7858 @Override
7859 public boolean isHearingAidCompatibilitySupported() {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007860 final long identity = Binder.clearCallingIdentity();
7861 try {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08007862 return mApp.getResources().getBoolean(R.bool.hac_enabled);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007863 } finally {
7864 Binder.restoreCallingIdentity(identity);
7865 }
Andrew Lee9431b832015-03-09 18:46:45 -07007866 }
7867
Hall Liuf6668912018-10-31 17:05:23 -07007868 /**
7869 * Determines whether the device currently supports RTT (Real-time text). Based both on carrier
7870 * support for the feature and device firmware support.
7871 *
7872 * @return {@code true} if the device and carrier both support RTT, {@code false} otherwise.
7873 */
7874 @Override
7875 public boolean isRttSupported(int subscriptionId) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007876 final long identity = Binder.clearCallingIdentity();
Nazanin Bakhshif782e562018-12-11 15:15:39 -08007877 final Phone phone = getPhone(subscriptionId);
7878 if (phone == null) {
7879 loge("isRttSupported: no Phone found. Invalid subId:" + subscriptionId);
7880 return false;
7881 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007882 try {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08007883 boolean isCarrierSupported = mApp.getCarrierConfigForSubId(subscriptionId).getBoolean(
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007884 CarrierConfigManager.KEY_RTT_SUPPORTED_BOOL);
7885 boolean isDeviceSupported =
Nazanin Bakhshif782e562018-12-11 15:15:39 -08007886 phone.getContext().getResources().getBoolean(R.bool.config_support_rtt);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007887 return isCarrierSupported && isDeviceSupported;
7888 } finally {
7889 Binder.restoreCallingIdentity(identity);
7890 }
Hall Liu98187582018-01-22 19:15:32 -08007891 }
7892
Hall Liuf6668912018-10-31 17:05:23 -07007893 /**
Hall Liuf2daa022019-07-23 18:39:00 -07007894 * Determines whether the user has turned on RTT. If the carrier wants to ignore the user-set
7895 * RTT setting, will return true if the device and carrier both support RTT.
7896 * Otherwise. only returns true if the device and carrier both also support RTT.
Hall Liuf6668912018-10-31 17:05:23 -07007897 */
7898 public boolean isRttEnabled(int subscriptionId) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007899 final long identity = Binder.clearCallingIdentity();
7900 try {
Hall Liu5bab75c2019-12-11 23:58:20 +00007901 boolean isRttSupported = isRttSupported(subscriptionId);
7902 boolean isUserRttSettingOn = Settings.Secure.getInt(
7903 mApp.getContentResolver(), Settings.Secure.RTT_CALLING_MODE, 0) != 0;
7904 boolean shouldIgnoreUserRttSetting = mApp.getCarrierConfigForSubId(subscriptionId)
7905 .getBoolean(CarrierConfigManager.KEY_IGNORE_RTT_MODE_SETTING_BOOL);
7906 return isRttSupported && (isUserRttSettingOn || shouldIgnoreUserRttSetting);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007907 } finally {
7908 Binder.restoreCallingIdentity(identity);
7909 }
Hall Liu3ad5f012018-04-06 16:23:39 -07007910 }
7911
Philip P. Moltmann700a9592019-10-03 11:53:50 -07007912 @Deprecated
7913 @Override
7914 public String getDeviceId(String callingPackage) {
7915 return getDeviceIdWithFeature(callingPackage, null);
7916 }
7917
Sanket Padawe7310cc72015-01-14 09:53:20 -08007918 /**
7919 * Returns the unique device ID of phone, for example, the IMEI for
7920 * GSM and the MEID for CDMA phones. Return null if device ID is not available.
7921 *
7922 * <p>Requires Permission:
7923 * {@link android.Manifest.permission#READ_PHONE_STATE READ_PHONE_STATE}
7924 */
7925 @Override
Philip P. Moltmann700a9592019-10-03 11:53:50 -07007926 public String getDeviceIdWithFeature(String callingPackage, String callingFeatureId) {
Shuo Qian13d89152021-05-10 23:58:11 -07007927 try {
7928 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
7929 } catch (SecurityException se) {
7930 EventLog.writeEvent(0x534e4554, "186530889", Binder.getCallingUid());
7931 throw new SecurityException("Package " + callingPackage + " does not belong to "
7932 + Binder.getCallingUid());
7933 }
Sanket Padawe7310cc72015-01-14 09:53:20 -08007934 final Phone phone = PhoneFactory.getPhone(0);
Jeff Davidson913390f2018-02-23 17:11:49 -08007935 if (phone == null) {
Sanket Padawe7310cc72015-01-14 09:53:20 -08007936 return null;
7937 }
Jeff Davidson913390f2018-02-23 17:11:49 -08007938 int subId = phone.getSubId();
Michael Groover70af6dc2018-10-01 16:23:15 -07007939 if (!TelephonyPermissions.checkCallingOrSelfReadDeviceIdentifiers(mApp, subId,
Philip P. Moltmann700a9592019-10-03 11:53:50 -07007940 callingPackage, callingFeatureId, "getDeviceId")) {
Jeff Davidson913390f2018-02-23 17:11:49 -08007941 return null;
7942 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007943
7944 final long identity = Binder.clearCallingIdentity();
7945 try {
7946 return phone.getDeviceId();
7947 } finally {
7948 Binder.restoreCallingIdentity(identity);
7949 }
Sanket Padawe7310cc72015-01-14 09:53:20 -08007950 }
7951
Ping Sunc67b7c22016-03-02 19:16:45 +08007952 /**
7953 * {@hide}
7954 * Returns the IMS Registration Status on a particular subid
7955 *
7956 * @param subId
7957 */
Brad Ebinger1f2b5082018-02-08 16:11:32 -08007958 public boolean isImsRegistered(int subId) {
Ping Sunc67b7c22016-03-02 19:16:45 +08007959 Phone phone = getPhone(subId);
7960 if (phone != null) {
7961 return phone.isImsRegistered();
7962 } else {
7963 return false;
7964 }
7965 }
7966
Santos Cordon7a1885b2015-02-03 11:15:19 -08007967 @Override
Shuo Qian6e6137d2019-10-30 16:33:31 -07007968 public int getSubIdForPhoneAccountHandle(
Philip P. Moltmann700a9592019-10-03 11:53:50 -07007969 PhoneAccountHandle phoneAccountHandle, String callingPackage, String callingFeatureId) {
Shuo Qian6e6137d2019-10-30 16:33:31 -07007970 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(mApp, getDefaultSubscription(),
Philip P. Moltmann700a9592019-10-03 11:53:50 -07007971 callingPackage, callingFeatureId, "getSubIdForPhoneAccountHandle")) {
Shuo Qian6e6137d2019-10-30 16:33:31 -07007972 throw new SecurityException("Requires READ_PHONE_STATE permission.");
7973 }
7974 final long identity = Binder.clearCallingIdentity();
7975 try {
7976 return PhoneUtils.getSubIdForPhoneAccountHandle(phoneAccountHandle);
7977 } finally {
7978 Binder.restoreCallingIdentity(identity);
7979 }
7980 }
7981
7982 @Override
Tyler Gunnf70ed162019-04-03 15:28:53 -07007983 public @Nullable PhoneAccountHandle getPhoneAccountHandleForSubscriptionId(int subscriptionId) {
Alireza Forouzan4ac4f982021-03-16 22:18:52 -07007984 TelephonyPermissions
Nathan Harold62c68512021-04-06 11:26:02 -07007985 .enforceCallingOrSelfReadPrivilegedPhoneStatePermissionOrCarrierPrivilege(
Alireza Forouzan4ac4f982021-03-16 22:18:52 -07007986 mApp,
7987 subscriptionId,
7988 "getPhoneAccountHandleForSubscriptionId, " + "subscriptionId: " + subscriptionId);
Tyler Gunnf70ed162019-04-03 15:28:53 -07007989 final long identity = Binder.clearCallingIdentity();
7990 try {
7991 Phone phone = getPhone(subscriptionId);
7992 if (phone == null) {
7993 return null;
7994 }
7995 return PhoneUtils.makePstnPhoneAccountHandle(phone);
7996 } finally {
7997 Binder.restoreCallingIdentity(identity);
7998 }
7999 }
8000
Brad Ebinger1f2b5082018-02-08 16:11:32 -08008001 /**
8002 * @return the VoWiFi calling availability.
Nathan Haroldc55097a2015-03-11 18:14:50 -07008003 */
Brad Ebinger1f2b5082018-02-08 16:11:32 -08008004 public boolean isWifiCallingAvailable(int subId) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008005 final long identity = Binder.clearCallingIdentity();
8006 try {
8007 Phone phone = getPhone(subId);
8008 if (phone != null) {
8009 return phone.isWifiCallingEnabled();
8010 } else {
8011 return false;
8012 }
8013 } finally {
8014 Binder.restoreCallingIdentity(identity);
Brad Ebinger1f2b5082018-02-08 16:11:32 -08008015 }
Nathan Haroldc55097a2015-03-11 18:14:50 -07008016 }
8017
Brad Ebinger1f2b5082018-02-08 16:11:32 -08008018 /**
Brad Ebinger1f2b5082018-02-08 16:11:32 -08008019 * @return the VT calling availability.
Etan Cohen3b7a1bc2015-05-28 15:57:13 -07008020 */
Brad Ebinger1f2b5082018-02-08 16:11:32 -08008021 public boolean isVideoTelephonyAvailable(int subId) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008022 final long identity = Binder.clearCallingIdentity();
8023 try {
8024 Phone phone = getPhone(subId);
8025 if (phone != null) {
8026 return phone.isVideoEnabled();
8027 } else {
8028 return false;
8029 }
8030 } finally {
8031 Binder.restoreCallingIdentity(identity);
Brad Ebinger1f2b5082018-02-08 16:11:32 -08008032 }
8033 }
8034
8035 /**
8036 * @return the IMS registration technology for the MMTEL feature. Valid return values are
8037 * defined in {@link ImsRegistrationImplBase}.
8038 */
8039 public @ImsRegistrationImplBase.ImsRegistrationTech int getImsRegTechnologyForMmTel(int subId) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008040 final long identity = Binder.clearCallingIdentity();
8041 try {
8042 Phone phone = getPhone(subId);
8043 if (phone != null) {
8044 return phone.getImsRegistrationTech();
8045 } else {
8046 return ImsRegistrationImplBase.REGISTRATION_TECH_NONE;
8047 }
8048 } finally {
8049 Binder.restoreCallingIdentity(identity);
Brad Ebinger1f2b5082018-02-08 16:11:32 -08008050 }
Etan Cohen3b7a1bc2015-05-28 15:57:13 -07008051 }
8052
Stuart Scott8eef64f2015-04-08 15:13:54 -07008053 @Override
Sarah Chinecc78c42022-03-31 21:16:48 -07008054 public void factoryReset(int subId, String callingPackage) {
paulhu5a773602019-08-23 19:17:33 +08008055 enforceSettingsPermission();
Stuart Scott981d8582015-04-21 14:09:50 -07008056 if (mUserManager.hasUserRestriction(UserManager.DISALLOW_NETWORK_RESET)) {
8057 return;
8058 }
Kai Shif70f46f2021-03-03 13:59:46 -08008059 Phone defaultPhone = getDefaultPhone();
8060 if (defaultPhone != null) {
8061 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
8062 mApp, getDefaultPhone().getSubId(), "factoryReset");
8063 }
Svet Ganovcc087f82015-05-12 20:35:54 -07008064 final long identity = Binder.clearCallingIdentity();
Nazanin Bakhshif782e562018-12-11 15:15:39 -08008065
Svet Ganovcc087f82015-05-12 20:35:54 -07008066 try {
Stuart Scott981d8582015-04-21 14:09:50 -07008067 if (SubscriptionManager.isUsableSubIdValue(subId) && !mUserManager.hasUserRestriction(
8068 UserManager.DISALLOW_CONFIG_MOBILE_NETWORKS)) {
Sooraj Sasindran95c07a92020-07-15 01:35:24 -07008069 setDataEnabledForReason(subId, TelephonyManager.DATA_ENABLED_REASON_USER,
Sarah Chinecc78c42022-03-31 21:16:48 -07008070 getDefaultDataEnabled(), callingPackage);
Svet Ganovcc087f82015-05-12 20:35:54 -07008071 setNetworkSelectionModeAutomatic(subId);
SongFerngWang8c6e82e2021-03-02 22:09:29 +08008072 Phone phone = getPhone(subId);
SongFerngWangfd89b102021-05-27 22:44:54 +08008073 cleanUpAllowedNetworkTypes(phone, subId);
Nazanin Bakhshif782e562018-12-11 15:15:39 -08008074 setDataRoamingEnabled(subId, getDefaultDataRoamingEnabled(subId));
Jordan Liu857e73a2021-03-05 16:24:04 -08008075 getPhone(subId).resetCarrierKeysForImsiEncryption();
Svet Ganovcc087f82015-05-12 20:35:54 -07008076 }
Amit Mahajan7dbbd822019-03-13 17:33:47 -07008077 // There has been issues when Sms raw table somehow stores orphan
8078 // fragments. They lead to garbled message when new fragments come
8079 // in and combined with those stale ones. In case this happens again,
8080 // user can reset all network settings which will clean up this table.
8081 cleanUpSmsRawTable(getDefaultPhone().getContext());
Brad Ebingerbc7dd582019-10-17 17:03:22 -07008082 // Clean up IMS settings as well here.
8083 int slotId = getSlotIndex(subId);
8084 if (slotId > SubscriptionManager.INVALID_SIM_SLOT_INDEX) {
8085 ImsManager.getInstance(mApp, slotId).factoryReset();
8086 }
Naina Nallurid63128d2019-09-17 14:10:30 -07008087
Kai Shif70f46f2021-03-03 13:59:46 -08008088 if (defaultPhone == null) {
8089 return;
8090 }
Naina Nallurid63128d2019-09-17 14:10:30 -07008091 // Erase modem config if erase modem on network setting is enabled.
8092 String configValue = DeviceConfig.getProperty(DeviceConfig.NAMESPACE_TELEPHONY,
8093 RESET_NETWORK_ERASE_MODEM_CONFIG_ENABLED);
8094 if (configValue != null && Boolean.parseBoolean(configValue)) {
Kai Shif70f46f2021-03-03 13:59:46 -08008095 sendEraseModemConfig(defaultPhone);
Naina Nallurid63128d2019-09-17 14:10:30 -07008096 }
Kai Shif70f46f2021-03-03 13:59:46 -08008097
8098 sendEraseDataInSharedPreferences(defaultPhone);
Svet Ganovcc087f82015-05-12 20:35:54 -07008099 } finally {
8100 Binder.restoreCallingIdentity(identity);
Stuart Scott8eef64f2015-04-08 15:13:54 -07008101 }
8102 }
Narayan Kamath1c496c22015-04-16 14:40:19 +01008103
SongFerngWangfd89b102021-05-27 22:44:54 +08008104 @VisibleForTesting
8105 void cleanUpAllowedNetworkTypes(Phone phone, int subId) {
8106 if (phone == null || !SubscriptionManager.isUsableSubscriptionId(subId)) {
8107 return;
8108 }
8109 long defaultNetworkType = RadioAccessFamily.getRafFromNetworkType(
8110 RILConstants.PREFERRED_NETWORK_MODE);
8111 SubscriptionManager.setSubscriptionProperty(subId,
8112 SubscriptionManager.ALLOWED_NETWORK_TYPES,
8113 "user=" + defaultNetworkType);
8114 phone.loadAllowedNetworksFromSubscriptionDatabase();
8115 phone.setAllowedNetworkTypes(TelephonyManager.ALLOWED_NETWORK_TYPES_REASON_USER,
8116 defaultNetworkType, null);
8117 }
8118
Amit Mahajan7dbbd822019-03-13 17:33:47 -07008119 private void cleanUpSmsRawTable(Context context) {
8120 ContentResolver resolver = context.getContentResolver();
8121 Uri uri = Uri.withAppendedPath(Telephony.Sms.CONTENT_URI, "raw/permanentDelete");
8122 resolver.delete(uri, null, null);
8123 }
8124
Narayan Kamath1c496c22015-04-16 14:40:19 +01008125 @Override
chen xu5d3637b2019-01-21 23:31:38 -08008126 public String getSimLocaleForSubscriber(int subId) {
8127 enforceReadPrivilegedPermission("getSimLocaleForSubscriber, subId: " + subId);
8128 final Phone phone = getPhone(subId);
8129 if (phone == null) {
8130 log("getSimLocaleForSubscriber, invalid subId");
chen xu2bb91e42019-01-24 14:35:54 -08008131 return null;
chen xu5d3637b2019-01-21 23:31:38 -08008132 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008133 final long identity = Binder.clearCallingIdentity();
8134 try {
Jack Yu285100e2022-12-02 22:48:35 -08008135 SubscriptionInfo info;
8136 if (PhoneFactory.isSubscriptionManagerServiceEnabled()) {
8137 info = SubscriptionManagerService.getInstance().getActiveSubscriptionInfo(subId,
8138 phone.getContext().getOpPackageName(),
8139 phone.getContext().getAttributionTag());
8140 if (info == null) {
8141 log("getSimLocaleForSubscriber, inactive subId: " + subId);
8142 return null;
8143 }
8144 } else {
8145 info = mSubscriptionController.getActiveSubscriptionInfo(subId,
8146 phone.getContext().getOpPackageName(),
8147 phone.getContext().getAttributionTag());
8148 if (info == null) {
8149 log("getSimLocaleForSubscriber, inactive subId: " + subId);
8150 return null;
8151 }
chen xu6291c472019-02-04 12:55:53 -08008152 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008153 // Try and fetch the locale from the carrier properties or from the SIM language
8154 // preferences (EF-PL and EF-LI)...
8155 final int mcc = info.getMcc();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008156 String simLanguage = null;
chen xu5d3637b2019-01-21 23:31:38 -08008157 final Locale localeFromDefaultSim = phone.getLocaleFromSimAndCarrierPrefs();
8158 if (localeFromDefaultSim != null) {
8159 if (!localeFromDefaultSim.getCountry().isEmpty()) {
8160 if (DBG) log("Using locale from subId: " + subId + " locale: "
8161 + localeFromDefaultSim);
tom hsu60a8dc52022-10-27 00:10:04 +08008162 return matchLocaleFromSupportedLocaleList(phone, localeFromDefaultSim);
chen xu5d3637b2019-01-21 23:31:38 -08008163 } else {
8164 simLanguage = localeFromDefaultSim.getLanguage();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008165 }
8166 }
Narayan Kamath1c496c22015-04-16 14:40:19 +01008167
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008168 // The SIM language preferences only store a language (e.g. fr = French), not an
8169 // exact locale (e.g. fr_FR = French/France). So, if the locale returned from
8170 // the SIM and carrier preferences does not include a country we add the country
8171 // determined from the SIM MCC to provide an exact locale.
zoey chenc730df82019-12-18 17:07:20 +08008172 final Locale mccLocale = LocaleUtils.getLocaleFromMcc(mApp, mcc, simLanguage);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008173 if (mccLocale != null) {
chen xu5d3637b2019-01-21 23:31:38 -08008174 if (DBG) log("No locale from SIM, using mcc locale:" + mccLocale);
tom hsu60a8dc52022-10-27 00:10:04 +08008175 return matchLocaleFromSupportedLocaleList(phone, mccLocale);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008176 }
8177
8178 if (DBG) log("No locale found - returning null");
8179 return null;
8180 } finally {
8181 Binder.restoreCallingIdentity(identity);
8182 }
Narayan Kamath1c496c22015-04-16 14:40:19 +01008183 }
8184
tom hsu0b59d292022-09-29 23:49:21 +08008185 @VisibleForTesting
tom hsu60a8dc52022-10-27 00:10:04 +08008186 String matchLocaleFromSupportedLocaleList(Phone phone, @NonNull Locale inputLocale) {
tom hsu0b59d292022-09-29 23:49:21 +08008187 String[] supportedLocale = com.android.internal.app.LocalePicker.getSupportedLocales(
tom hsu60a8dc52022-10-27 00:10:04 +08008188 phone.getContext());
tom hsu0b59d292022-09-29 23:49:21 +08008189 for (String localeTag : supportedLocale) {
tom hsu60a8dc52022-10-27 00:10:04 +08008190 if (LocaleList.matchesLanguageAndScript(inputLocale, Locale.forLanguageTag(localeTag))
8191 && TextUtils.equals(inputLocale.getCountry(),
tom hsu0b59d292022-09-29 23:49:21 +08008192 Locale.forLanguageTag(localeTag).getCountry())) {
8193 return localeTag;
8194 }
8195 }
8196 return inputLocale.toLanguageTag();
8197 }
8198
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008199 /**
8200 * NOTE: this method assumes permission checks are done and caller identity has been cleared.
8201 */
8202 private List<SubscriptionInfo> getActiveSubscriptionInfoListPrivileged() {
Jack Yu285100e2022-12-02 22:48:35 -08008203 if (PhoneFactory.isSubscriptionManagerServiceEnabled()) {
8204 return SubscriptionManagerService.getInstance().getActiveSubscriptionInfoList(
8205 mApp.getOpPackageName(), mApp.getAttributionTag());
8206 }
Philip P. Moltmann700a9592019-10-03 11:53:50 -07008207 return mSubscriptionController.getActiveSubscriptionInfoList(mApp.getOpPackageName(),
Philip P. Moltmann8d34f0c2020-03-05 16:24:02 -08008208 mApp.getAttributionTag());
Narayan Kamath1c496c22015-04-16 14:40:19 +01008209 }
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -07008210
Gary Jian3aa9a762022-01-24 16:41:19 +08008211 private ActivityStatsTechSpecificInfo[] mLastModemActivitySpecificInfo = null;
8212 private ModemActivityInfo mLastModemActivityInfo = null;
Chenjie Yu1ba97252018-01-11 18:16:20 -08008213
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -07008214 /**
Adam Lesinski903a54c2016-04-11 14:49:52 -07008215 * Responds to the ResultReceiver with the {@link android.telephony.ModemActivityInfo} object
8216 * representing the state of the modem.
8217 *
Chenjie Yu1ba97252018-01-11 18:16:20 -08008218 * NOTE: The underlying implementation clears the modem state, so there should only ever be one
8219 * caller to it. Everyone should call this class to get cumulative data.
Adam Lesinski903a54c2016-04-11 14:49:52 -07008220 * @hide
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -07008221 */
8222 @Override
Adam Lesinski903a54c2016-04-11 14:49:52 -07008223 public void requestModemActivityInfo(ResultReceiver result) {
8224 enforceModifyPermission();
vagdeviaf9a5b92018-08-15 16:01:53 -07008225 WorkSource workSource = getWorkSource(Binder.getCallingUid());
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008226
8227 final long identity = Binder.clearCallingIdentity();
8228 try {
Shuo Qian8f4750a2020-02-20 17:12:10 -08008229 sendRequestAsync(CMD_GET_MODEM_ACTIVITY_INFO, result, null, workSource);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008230 } finally {
8231 Binder.restoreCallingIdentity(identity);
Chenjie Yu1ba97252018-01-11 18:16:20 -08008232 }
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -07008233 }
Jack Yu85bd38a2015-11-09 11:34:32 -08008234
Gary Jian76280a42022-12-07 16:18:33 +08008235 // Checks that ModemActivityInfo is valid. Sleep time and Idle time should be
Siddharth Rayb8114062018-06-17 15:02:38 -07008236 // less than total activity duration.
8237 private boolean isModemActivityInfoValid(ModemActivityInfo info) {
8238 if (info == null) {
8239 return false;
8240 }
8241 int activityDurationMs =
Hall Liu49656c02020-10-09 19:00:11 -07008242 (int) (info.getTimestampMillis() - mLastModemActivityInfo.getTimestampMillis());
Gary Jian76280a42022-12-07 16:18:33 +08008243 activityDurationMs += MODEM_ACTIVITY_TIME_OFFSET_CORRECTION_MS;
8244
Hall Liu49656c02020-10-09 19:00:11 -07008245 int totalTxTimeMs = Arrays.stream(info.getTransmitTimeMillis()).sum();
8246
Siddharth Rayb8114062018-06-17 15:02:38 -07008247 return (info.isValid()
8248 && (info.getSleepTimeMillis() <= activityDurationMs)
Gary Jian76280a42022-12-07 16:18:33 +08008249 && (info.getIdleTimeMillis() <= activityDurationMs));
Siddharth Rayb8114062018-06-17 15:02:38 -07008250 }
8251
Gary Jian3aa9a762022-01-24 16:41:19 +08008252 private void updateLastModemActivityInfo(ModemActivityInfo info, int rat, int freq) {
8253 int[] mergedTxTimeMs = new int [ModemActivityInfo.getNumTxPowerLevels()];
8254 int[] txTimeMs = info.getTransmitTimeMillis(rat, freq);
8255 int[] lastModemTxTimeMs = mLastModemActivityInfo.getTransmitTimeMillis(rat, freq);
8256
8257 for (int lvl = 0; lvl < mergedTxTimeMs.length; lvl++) {
8258 mergedTxTimeMs[lvl] = txTimeMs[lvl] + lastModemTxTimeMs[lvl];
8259 }
8260
8261 mLastModemActivityInfo.setTransmitTimeMillis(rat, freq, mergedTxTimeMs);
8262 mLastModemActivityInfo.setReceiveTimeMillis(
8263 rat,
8264 freq,
8265 info.getReceiveTimeMillis(rat, freq)
8266 + mLastModemActivityInfo.getReceiveTimeMillis(rat, freq));
8267 }
8268
8269 private void updateLastModemActivityInfo(ModemActivityInfo info, int rat) {
8270 int[] mergedTxTimeMs = new int [ModemActivityInfo.getNumTxPowerLevels()];
8271 int[] txTimeMs = info.getTransmitTimeMillis(rat);
8272 int[] lastModemTxTimeMs = mLastModemActivityInfo.getTransmitTimeMillis(rat);
8273
8274 for (int lvl = 0; lvl < mergedTxTimeMs.length; lvl++) {
8275 mergedTxTimeMs[lvl] = txTimeMs[lvl] + lastModemTxTimeMs[lvl];
8276 }
8277 mLastModemActivityInfo.setTransmitTimeMillis(rat, mergedTxTimeMs);
8278 mLastModemActivityInfo.setReceiveTimeMillis(
8279 rat,
8280 info.getReceiveTimeMillis(rat) + mLastModemActivityInfo.getReceiveTimeMillis(rat));
8281 }
8282
8283 /**
8284 * Merge this ModemActivityInfo with mLastModemActivitySpecificInfo
8285 * @param info recent ModemActivityInfo
8286 */
8287 private void mergeModemActivityInfo(ModemActivityInfo info) {
8288 List<ActivityStatsTechSpecificInfo> merged = new ArrayList<>();
Kai Shi917fdc62022-11-28 14:01:02 -08008289 ActivityStatsTechSpecificInfo deltaSpecificInfo;
Gary Jian3aa9a762022-01-24 16:41:19 +08008290 boolean matched;
8291 for (int i = 0; i < info.getSpecificInfoLength(); i++) {
8292 matched = false;
8293 int rat = info.getSpecificInfoRat(i);
8294 int freq = info.getSpecificInfoFrequencyRange(i);
8295 //Check each ActivityStatsTechSpecificInfo in this ModemActivityInfo for new rat returns
8296 //Add a new ActivityStatsTechSpecificInfo if is a new rat, and merge with the original
8297 //if it already exists
8298 for (int j = 0; j < mLastModemActivitySpecificInfo.length; j++) {
8299 if (rat == mLastModemActivityInfo.getSpecificInfoRat(j) && !matched) {
8300 //Merged based on frequency range (MMWAVE vs SUB6) for 5G
8301 if (rat == AccessNetworkConstants.AccessNetworkType.NGRAN) {
8302 if (freq == mLastModemActivityInfo.getSpecificInfoFrequencyRange(j)) {
8303 updateLastModemActivityInfo(info, rat, freq);
8304 matched = true;
8305 }
8306 } else {
8307 updateLastModemActivityInfo(info, rat);
8308 matched = true;
8309 }
8310 }
8311 }
8312
8313 if (!matched) {
Kai Shi917fdc62022-11-28 14:01:02 -08008314 deltaSpecificInfo =
Gary Jian3aa9a762022-01-24 16:41:19 +08008315 new ActivityStatsTechSpecificInfo(
8316 rat,
8317 freq,
8318 info.getTransmitTimeMillis(rat, freq),
8319 (int) info.getReceiveTimeMillis(rat, freq));
Kai Shi917fdc62022-11-28 14:01:02 -08008320 merged.addAll(Arrays.asList(deltaSpecificInfo));
Gary Jian3aa9a762022-01-24 16:41:19 +08008321 }
8322 }
8323 merged.addAll(Arrays.asList(mLastModemActivitySpecificInfo));
8324 mLastModemActivitySpecificInfo =
8325 new ActivityStatsTechSpecificInfo[merged.size()];
8326 merged.toArray(mLastModemActivitySpecificInfo);
8327
8328 mLastModemActivityInfo.setTimestamp(info.getTimestampMillis());
8329 mLastModemActivityInfo.setSleepTimeMillis(
8330 info.getSleepTimeMillis()
8331 + mLastModemActivityInfo.getSleepTimeMillis());
8332 mLastModemActivityInfo.setIdleTimeMillis(
8333 info.getIdleTimeMillis()
8334 + mLastModemActivityInfo.getIdleTimeMillis());
Kai Shi917fdc62022-11-28 14:01:02 -08008335
8336 mLastModemActivityInfo =
8337 new ModemActivityInfo(
8338 mLastModemActivityInfo.getTimestampMillis(),
8339 mLastModemActivityInfo.getSleepTimeMillis(),
8340 mLastModemActivityInfo.getIdleTimeMillis(),
8341 mLastModemActivitySpecificInfo);
8342 }
8343
8344 private ActivityStatsTechSpecificInfo[] deepCopyModemActivitySpecificInfo(
8345 ActivityStatsTechSpecificInfo[] info) {
8346 int infoSize = info.length;
8347 ActivityStatsTechSpecificInfo[] ret = new ActivityStatsTechSpecificInfo[infoSize];
8348 for (int i = 0; i < infoSize; i++) {
8349 ret[i] = new ActivityStatsTechSpecificInfo(
8350 info[i].getRat(), info[i].getFrequencyRange(),
8351 info[i].getTransmitTimeMillis(),
8352 (int) info[i].getReceiveTimeMillis());
8353 }
8354 return ret;
Gary Jian3aa9a762022-01-24 16:41:19 +08008355 }
8356
Jack Yu85bd38a2015-11-09 11:34:32 -08008357 /**
Jack Yu85bd38a2015-11-09 11:34:32 -08008358 * Returns the service state information on specified subscription.
8359 */
8360 @Override
Sooraj Sasindran2250dc02021-11-10 16:42:01 -08008361 public ServiceState getServiceStateForSubscriber(int subId,
8362 boolean renounceFineLocationAccess, boolean renounceCoarseLocationAccess,
8363 String callingPackage, String callingFeatureId) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08008364 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
Philip P. Moltmann700a9592019-10-03 11:53:50 -07008365 mApp, subId, callingPackage, callingFeatureId, "getServiceStateForSubscriber")) {
Jack Yu85bd38a2015-11-09 11:34:32 -08008366 return null;
8367 }
8368
Sooraj Sasindran2250dc02021-11-10 16:42:01 -08008369 boolean hasFinePermission = false;
8370 boolean hasCoarsePermission = false;
8371 if (!renounceFineLocationAccess) {
8372 LocationAccessPolicy.LocationPermissionResult fineLocationResult =
8373 LocationAccessPolicy.checkLocationPermission(mApp,
8374 new LocationAccessPolicy.LocationPermissionQuery.Builder()
8375 .setCallingPackage(callingPackage)
8376 .setCallingFeatureId(callingFeatureId)
8377 .setCallingPid(Binder.getCallingPid())
8378 .setCallingUid(Binder.getCallingUid())
8379 .setMethod("getServiceStateForSubscriber")
8380 .setLogAsInfo(true)
8381 .setMinSdkVersionForFine(Build.VERSION_CODES.Q)
8382 .setMinSdkVersionForCoarse(Build.VERSION_CODES.Q)
8383 .setMinSdkVersionForEnforcement(Build.VERSION_CODES.Q)
8384 .build());
8385 hasFinePermission =
8386 fineLocationResult == LocationAccessPolicy.LocationPermissionResult.ALLOWED;
8387 }
Hall Liuf19c44f2018-11-27 14:38:17 -08008388
Sooraj Sasindran2250dc02021-11-10 16:42:01 -08008389 if (!renounceCoarseLocationAccess) {
8390 LocationAccessPolicy.LocationPermissionResult coarseLocationResult =
8391 LocationAccessPolicy.checkLocationPermission(mApp,
8392 new LocationAccessPolicy.LocationPermissionQuery.Builder()
8393 .setCallingPackage(callingPackage)
8394 .setCallingFeatureId(callingFeatureId)
8395 .setCallingPid(Binder.getCallingPid())
8396 .setCallingUid(Binder.getCallingUid())
8397 .setMethod("getServiceStateForSubscriber")
8398 .setLogAsInfo(true)
8399 .setMinSdkVersionForCoarse(Build.VERSION_CODES.Q)
8400 .setMinSdkVersionForFine(Integer.MAX_VALUE)
8401 .setMinSdkVersionForEnforcement(Build.VERSION_CODES.Q)
8402 .build());
8403 hasCoarsePermission =
8404 coarseLocationResult == LocationAccessPolicy.LocationPermissionResult.ALLOWED;
8405 }
Hall Liuf19c44f2018-11-27 14:38:17 -08008406
Jack Yu479f40e2020-10-27 21:29:25 -07008407 final Phone phone = getPhone(subId);
8408 if (phone == null) {
8409 return null;
8410 }
8411
Jordan Liu0f2bc442020-11-18 16:47:37 -08008412 final long identity = Binder.clearCallingIdentity();
8413
Jack Yu479f40e2020-10-27 21:29:25 -07008414 boolean isCallingPackageDataService = phone.getDataServicePackages()
8415 .contains(callingPackage);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008416 try {
Jordan Liuc437b192020-08-17 10:59:12 -07008417 // isActiveSubId requires READ_PHONE_STATE, which we already check for above
Jack Yu285100e2022-12-02 22:48:35 -08008418 if (PhoneFactory.isSubscriptionManagerServiceEnabled()) {
8419 SubscriptionInfoInternal subInfo = SubscriptionManagerService.getInstance()
8420 .getSubscriptionInfoInternal(subId);
8421 if (subInfo == null || !subInfo.isActive()) {
8422 Rlog.d(LOG_TAG, "getServiceStateForSubscriber returning null for inactive "
8423 + "subId=" + subId);
8424 return null;
8425 }
8426 } else {
8427 if (!mSubscriptionController.isActiveSubId(subId, callingPackage,
8428 callingFeatureId)) {
8429 Rlog.d(LOG_TAG, "getServiceStateForSubscriber returning null for inactive "
8430 + "subId=" + subId);
8431 return null;
8432 }
Jordan Liuc437b192020-08-17 10:59:12 -07008433 }
8434
Hall Liuf19c44f2018-11-27 14:38:17 -08008435 ServiceState ss = phone.getServiceState();
8436
8437 // Scrub out the location info in ServiceState depending on what level of access
8438 // the caller has.
Jack Yu479f40e2020-10-27 21:29:25 -07008439 if (hasFinePermission || isCallingPackageDataService) return ss;
Malcolm Chen5052de62019-12-30 13:56:38 -08008440 if (hasCoarsePermission) return ss.createLocationInfoSanitizedCopy(false);
8441 return ss.createLocationInfoSanitizedCopy(true);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008442 } finally {
8443 Binder.restoreCallingIdentity(identity);
8444 }
Jack Yu85bd38a2015-11-09 11:34:32 -08008445 }
Nancy Chen31f9ba12016-01-06 11:42:12 -08008446
8447 /**
8448 * Returns the URI for the per-account voicemail ringtone set in Phone settings.
8449 *
8450 * @param accountHandle The handle for the {@link PhoneAccount} for which to retrieve the
8451 * voicemail ringtone.
8452 * @return The URI for the ringtone to play when receiving a voicemail from a specific
8453 * PhoneAccount.
8454 */
8455 @Override
8456 public Uri getVoicemailRingtoneUri(PhoneAccountHandle accountHandle) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008457 final long identity = Binder.clearCallingIdentity();
8458 try {
8459 Phone phone = PhoneUtils.getPhoneForPhoneAccountHandle(accountHandle);
8460 if (phone == null) {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08008461 phone = getDefaultPhone();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008462 }
Nancy Chen31f9ba12016-01-06 11:42:12 -08008463
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008464 return VoicemailNotificationSettingsUtil.getRingtoneUri(phone.getContext());
8465 } finally {
8466 Binder.restoreCallingIdentity(identity);
8467 }
Nancy Chen31f9ba12016-01-06 11:42:12 -08008468 }
8469
8470 /**
Ta-wei Yenc33877d2017-01-23 18:11:21 -08008471 * Sets the per-account voicemail ringtone.
8472 *
8473 * <p>Requires that the calling app is the default dialer, or has carrier privileges, or
8474 * has permission {@link android.Manifest.permission#MODIFY_PHONE_STATE MODIFY_PHONE_STATE}.
8475 *
8476 * @param phoneAccountHandle The handle for the {@link PhoneAccount} for which to set the
8477 * voicemail ringtone.
8478 * @param uri The URI for the ringtone to play when receiving a voicemail from a specific
8479 * PhoneAccount.
8480 */
8481 @Override
8482 public void setVoicemailRingtoneUri(String callingPackage,
8483 PhoneAccountHandle phoneAccountHandle, Uri uri) {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08008484 final Phone defaultPhone = getDefaultPhone();
Ta-wei Yenc33877d2017-01-23 18:11:21 -08008485 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
Tyler Gunn5ddfdc92019-10-31 13:08:23 -07008486 TelecomManager tm = defaultPhone.getContext().getSystemService(TelecomManager.class);
8487 if (!TextUtils.equals(callingPackage, tm.getDefaultDialerPackage())) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08008488 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
8489 mApp, PhoneUtils.getSubIdForPhoneAccountHandle(phoneAccountHandle),
8490 "setVoicemailRingtoneUri");
Ta-wei Yenc33877d2017-01-23 18:11:21 -08008491 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008492
8493 final long identity = Binder.clearCallingIdentity();
8494 try {
8495 Phone phone = PhoneUtils.getPhoneForPhoneAccountHandle(phoneAccountHandle);
8496 if (phone == null) {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08008497 phone = defaultPhone;
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008498 }
8499 VoicemailNotificationSettingsUtil.setRingtoneUri(phone.getContext(), uri);
8500 } finally {
8501 Binder.restoreCallingIdentity(identity);
Ta-wei Yenc33877d2017-01-23 18:11:21 -08008502 }
Ta-wei Yenc33877d2017-01-23 18:11:21 -08008503 }
8504
8505 /**
Nancy Chen31f9ba12016-01-06 11:42:12 -08008506 * Returns whether vibration is set for voicemail notification in Phone settings.
8507 *
8508 * @param accountHandle The handle for the {@link PhoneAccount} for which to retrieve the
8509 * voicemail vibration setting.
8510 * @return {@code true} if the vibration is set for this PhoneAccount, {@code false} otherwise.
8511 */
8512 @Override
8513 public boolean isVoicemailVibrationEnabled(PhoneAccountHandle accountHandle) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008514 final long identity = Binder.clearCallingIdentity();
8515 try {
8516 Phone phone = PhoneUtils.getPhoneForPhoneAccountHandle(accountHandle);
8517 if (phone == null) {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08008518 phone = getDefaultPhone();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008519 }
Nancy Chen31f9ba12016-01-06 11:42:12 -08008520
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008521 return VoicemailNotificationSettingsUtil.isVibrationEnabled(phone.getContext());
8522 } finally {
8523 Binder.restoreCallingIdentity(identity);
8524 }
Nancy Chen31f9ba12016-01-06 11:42:12 -08008525 }
8526
Youhan Wange64578a2016-05-02 15:32:42 -07008527 /**
Ta-wei Yenc33877d2017-01-23 18:11:21 -08008528 * Sets the per-account voicemail vibration.
8529 *
8530 * <p>Requires that the calling app is the default dialer, or has carrier privileges, or
8531 * has permission {@link android.Manifest.permission#MODIFY_PHONE_STATE MODIFY_PHONE_STATE}.
8532 *
8533 * @param phoneAccountHandle The handle for the {@link PhoneAccount} for which to set the
8534 * voicemail vibration setting.
8535 * @param enabled Whether to enable or disable vibration for voicemail notifications from a
8536 * specific PhoneAccount.
8537 */
8538 @Override
8539 public void setVoicemailVibrationEnabled(String callingPackage,
8540 PhoneAccountHandle phoneAccountHandle, boolean enabled) {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08008541 final Phone defaultPhone = getDefaultPhone();
Ta-wei Yenc33877d2017-01-23 18:11:21 -08008542 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
Tyler Gunn5ddfdc92019-10-31 13:08:23 -07008543 TelecomManager tm = defaultPhone.getContext().getSystemService(TelecomManager.class);
8544 if (!TextUtils.equals(callingPackage, tm.getDefaultDialerPackage())) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08008545 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
8546 mApp, PhoneUtils.getSubIdForPhoneAccountHandle(phoneAccountHandle),
8547 "setVoicemailVibrationEnabled");
Ta-wei Yenc33877d2017-01-23 18:11:21 -08008548 }
8549
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008550 final long identity = Binder.clearCallingIdentity();
8551 try {
8552 Phone phone = PhoneUtils.getPhoneForPhoneAccountHandle(phoneAccountHandle);
8553 if (phone == null) {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08008554 phone = defaultPhone;
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008555 }
8556 VoicemailNotificationSettingsUtil.setVibrationEnabled(phone.getContext(), enabled);
8557 } finally {
8558 Binder.restoreCallingIdentity(identity);
Ta-wei Yenc33877d2017-01-23 18:11:21 -08008559 }
Ta-wei Yenc33877d2017-01-23 18:11:21 -08008560 }
8561
8562 /**
Youhan Wange64578a2016-05-02 15:32:42 -07008563 * Make sure either called from same process as self (phone) or IPC caller has read privilege.
8564 *
8565 * @throws SecurityException if the caller does not have the required permission
8566 */
arunvoddud7401012022-12-15 16:08:12 +00008567 @VisibleForTesting
8568 public void enforceReadPrivilegedPermission(String message) {
Youhan Wange64578a2016-05-02 15:32:42 -07008569 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.READ_PRIVILEGED_PHONE_STATE,
Brad Ebinger35c841c2018-10-01 10:40:55 -07008570 message);
Youhan Wange64578a2016-05-02 15:32:42 -07008571 }
8572
8573 /**
Ta-wei Yen30a69c82016-12-27 14:52:32 -08008574 * Make sure either called from same process as self (phone) or IPC caller has send SMS
8575 * permission.
8576 *
8577 * @throws SecurityException if the caller does not have the required permission
8578 */
8579 private void enforceSendSmsPermission() {
8580 mApp.enforceCallingOrSelfPermission(permission.SEND_SMS, null);
8581 }
8582
8583 /**
Aishwarya Mallampatifbc70d32022-11-10 20:33:02 +00008584 * Make sure either called from same process as self (phone) or IPC caller has interact across
8585 * users permission.
8586 *
8587 * @throws SecurityException if the caller does not have the required permission
8588 */
8589 private void enforceInteractAcrossUsersPermission(String message) {
8590 mApp.enforceCallingOrSelfPermission(permission.INTERACT_ACROSS_USERS, message);
8591 }
8592
8593 /**
Ta-wei Yen527a9c02017-01-06 15:29:25 -08008594 * Make sure called from the package in charge of visual voicemail.
Ta-wei Yen30a69c82016-12-27 14:52:32 -08008595 *
Ta-wei Yen527a9c02017-01-06 15:29:25 -08008596 * @throws SecurityException if the caller is not the visual voicemail package.
Ta-wei Yen30a69c82016-12-27 14:52:32 -08008597 */
Ta-wei Yen527a9c02017-01-06 15:29:25 -08008598 private void enforceVisualVoicemailPackage(String callingPackage, int subId) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008599 final long identity = Binder.clearCallingIdentity();
8600 try {
8601 ComponentName componentName =
Nazanin Bakhshif782e562018-12-11 15:15:39 -08008602 RemoteVvmTaskManager.getRemotePackage(mApp, subId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008603 if (componentName == null) {
8604 throw new SecurityException(
8605 "Caller not current active visual voicemail package[null]");
8606 }
8607 String vvmPackage = componentName.getPackageName();
8608 if (!callingPackage.equals(vvmPackage)) {
Hui Wang7f657552022-08-16 16:58:25 +00008609 throw new SecurityException("Caller not current active visual voicemail package");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008610 }
8611 } finally {
8612 Binder.restoreCallingIdentity(identity);
Ta-wei Yen30a69c82016-12-27 14:52:32 -08008613 }
8614 }
8615
8616 /**
Youhan Wange64578a2016-05-02 15:32:42 -07008617 * Return the application ID for the app type.
8618 *
8619 * @param subId the subscription ID that this request applies to.
8620 * @param appType the uicc app type.
8621 * @return Application ID for specificied app type, or null if no uicc.
8622 */
8623 @Override
8624 public String getAidForAppType(int subId, int appType) {
Brad Ebinger35c841c2018-10-01 10:40:55 -07008625 enforceReadPrivilegedPermission("getAidForAppType");
Youhan Wange64578a2016-05-02 15:32:42 -07008626 Phone phone = getPhone(subId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008627
8628 final long identity = Binder.clearCallingIdentity();
Youhan Wange64578a2016-05-02 15:32:42 -07008629 try {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008630 if (phone == null) {
8631 return null;
8632 }
8633 String aid = null;
8634 try {
Muralidhar Reddy472c2ae2021-09-29 19:38:40 +00008635 aid = UiccController.getInstance().getUiccPort(phone.getPhoneId())
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008636 .getApplicationByType(appType).getAid();
8637 } catch (Exception e) {
8638 Log.e(LOG_TAG, "Not getting aid. Exception ex=" + e);
8639 }
8640 return aid;
8641 } finally {
8642 Binder.restoreCallingIdentity(identity);
Youhan Wange64578a2016-05-02 15:32:42 -07008643 }
Youhan Wange64578a2016-05-02 15:32:42 -07008644 }
8645
Youhan Wang4001d252016-05-11 10:29:41 -07008646 /**
8647 * Return the Electronic Serial Number.
8648 *
8649 * @param subId the subscription ID that this request applies to.
8650 * @return ESN or null if error.
8651 */
8652 @Override
8653 public String getEsn(int subId) {
Brad Ebinger35c841c2018-10-01 10:40:55 -07008654 enforceReadPrivilegedPermission("getEsn");
Youhan Wang4001d252016-05-11 10:29:41 -07008655 Phone phone = getPhone(subId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008656
8657 final long identity = Binder.clearCallingIdentity();
Youhan Wang4001d252016-05-11 10:29:41 -07008658 try {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008659 if (phone == null) {
8660 return null;
8661 }
8662 String esn = null;
8663 try {
8664 esn = phone.getEsn();
8665 } catch (Exception e) {
8666 Log.e(LOG_TAG, "Not getting ESN. Exception ex=" + e);
8667 }
8668 return esn;
8669 } finally {
8670 Binder.restoreCallingIdentity(identity);
Youhan Wang4001d252016-05-11 10:29:41 -07008671 }
Youhan Wang4001d252016-05-11 10:29:41 -07008672 }
8673
Sanket Padawe99ef1e32016-05-18 16:12:33 -07008674 /**
Youhan Wang66ad5d72016-07-18 17:56:58 -07008675 * Return the Preferred Roaming List Version.
8676 *
8677 * @param subId the subscription ID that this request applies to.
8678 * @return PRLVersion or null if error.
8679 */
8680 @Override
8681 public String getCdmaPrlVersion(int subId) {
Brad Ebinger35c841c2018-10-01 10:40:55 -07008682 enforceReadPrivilegedPermission("getCdmaPrlVersion");
Youhan Wang66ad5d72016-07-18 17:56:58 -07008683 Phone phone = getPhone(subId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008684
8685 final long identity = Binder.clearCallingIdentity();
Youhan Wang66ad5d72016-07-18 17:56:58 -07008686 try {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008687 if (phone == null) {
8688 return null;
8689 }
8690 String cdmaPrlVersion = null;
8691 try {
8692 cdmaPrlVersion = phone.getCdmaPrlVersion();
8693 } catch (Exception e) {
8694 Log.e(LOG_TAG, "Not getting PRLVersion", e);
8695 }
8696 return cdmaPrlVersion;
8697 } finally {
8698 Binder.restoreCallingIdentity(identity);
Youhan Wang66ad5d72016-07-18 17:56:58 -07008699 }
Youhan Wang66ad5d72016-07-18 17:56:58 -07008700 }
8701
8702 /**
Sanket Padawe99ef1e32016-05-18 16:12:33 -07008703 * Get snapshot of Telephony histograms
8704 * @return List of Telephony histograms
8705 * @hide
8706 */
8707 @Override
8708 public List<TelephonyHistogram> getTelephonyHistograms() {
Jeff Davidson7e17e312018-02-13 18:17:36 -08008709 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
8710 mApp, getDefaultSubscription(), "getTelephonyHistograms");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008711
8712 final long identity = Binder.clearCallingIdentity();
8713 try {
8714 return RIL.getTelephonyRILTimingHistograms();
8715 } finally {
8716 Binder.restoreCallingIdentity(identity);
8717 }
Sanket Padawe99ef1e32016-05-18 16:12:33 -07008718 }
Meng Wang1a7c35a2016-05-05 20:56:15 -07008719
8720 /**
8721 * {@hide}
Michele Berionne482f8202018-11-27 18:57:59 -08008722 * Set the allowed carrier list and the excluded carrier list, indicating the priority between
8723 * the two lists.
Meng Wang1a7c35a2016-05-05 20:56:15 -07008724 * Require system privileges. In the future we may add this to carrier APIs.
8725 *
Michele Berionne482f8202018-11-27 18:57:59 -08008726 * @return Integer with the result of the operation, as defined in {@link TelephonyManager}.
Meng Wang1a7c35a2016-05-05 20:56:15 -07008727 */
8728 @Override
Michele Berionne482f8202018-11-27 18:57:59 -08008729 @TelephonyManager.SetCarrierRestrictionResult
8730 public int setAllowedCarriers(CarrierRestrictionRules carrierRestrictionRules) {
Meng Wang1a7c35a2016-05-05 20:56:15 -07008731 enforceModifyPermission();
vagdeviaf9a5b92018-08-15 16:01:53 -07008732 WorkSource workSource = getWorkSource(Binder.getCallingUid());
Sanket Padawe13bac7b2017-03-20 15:04:47 -07008733
Michele Berionne482f8202018-11-27 18:57:59 -08008734 if (carrierRestrictionRules == null) {
8735 throw new NullPointerException("carrier restriction cannot be null");
Meng Wang9b7c4e92017-02-17 11:41:27 -08008736 }
Sanket Padawe13bac7b2017-03-20 15:04:47 -07008737
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008738 final long identity = Binder.clearCallingIdentity();
8739 try {
Michele Berionne482f8202018-11-27 18:57:59 -08008740 return (int) sendRequest(CMD_SET_ALLOWED_CARRIERS, carrierRestrictionRules,
vagdeviaf9a5b92018-08-15 16:01:53 -07008741 workSource);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008742 } finally {
8743 Binder.restoreCallingIdentity(identity);
8744 }
Meng Wang1a7c35a2016-05-05 20:56:15 -07008745 }
8746
8747 /**
8748 * {@hide}
Michele Berionne482f8202018-11-27 18:57:59 -08008749 * Get the allowed carrier list and the excluded carrier list, including the priority between
8750 * the two lists.
Meng Wang1a7c35a2016-05-05 20:56:15 -07008751 * Require system privileges. In the future we may add this to carrier APIs.
8752 *
Michele Berionne482f8202018-11-27 18:57:59 -08008753 * @return {@link android.telephony.CarrierRestrictionRules}
Meng Wang1a7c35a2016-05-05 20:56:15 -07008754 */
8755 @Override
Michele Berionne482f8202018-11-27 18:57:59 -08008756 public CarrierRestrictionRules getAllowedCarriers() {
Brad Ebinger35c841c2018-10-01 10:40:55 -07008757 enforceReadPrivilegedPermission("getAllowedCarriers");
vagdeviaf9a5b92018-08-15 16:01:53 -07008758 WorkSource workSource = getWorkSource(Binder.getCallingUid());
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008759
8760 final long identity = Binder.clearCallingIdentity();
8761 try {
Michele Berionne482f8202018-11-27 18:57:59 -08008762 Object response = sendRequest(CMD_GET_ALLOWED_CARRIERS, null, workSource);
8763 if (response instanceof CarrierRestrictionRules) {
8764 return (CarrierRestrictionRules) response;
8765 }
8766 // Response is an Exception of some kind,
8767 // which is signalled to the user as a NULL retval
8768 return null;
8769 } catch (Exception e) {
8770 Log.e(LOG_TAG, "getAllowedCarriers. Exception ex=" + e);
8771 return null;
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008772 } finally {
8773 Binder.restoreCallingIdentity(identity);
8774 }
Meng Wang1a7c35a2016-05-05 20:56:15 -07008775 }
8776
fionaxu59545b42016-05-25 15:53:37 -07008777 /**
arunvoddud7401012022-12-15 16:08:12 +00008778 * Fetches the carrier restriction status of the device and sends the status to the caller
8779 * through the callback.
8780 *
8781 * @param callback The callback that will be used to send the result.
8782 * @throws SecurityException if the caller does not have the required permission/privileges or
8783 * the caller is not allowlisted.
8784 */
8785 @Override
8786 public void getCarrierRestrictionStatus(IIntegerConsumer callback, String packageName) {
8787 enforceReadPermission("getCarrierRestrictionStatus");
8788 int carrierId = validateCallerAndGetCarrierId(packageName);
8789 if (carrierId == CarrierAllowListInfo.INVALID_CARRIER_ID) {
8790 Rlog.e(LOG_TAG, "getCarrierRestrictionStatus: caller is not registered");
8791 throw new SecurityException("Not an authorized caller");
8792 }
8793 final long identity = Binder.clearCallingIdentity();
8794 try {
8795 Consumer<Integer> consumer = FunctionalUtils.ignoreRemoteException(callback::accept);
8796 CallerCallbackInfo callbackInfo = new CallerCallbackInfo(consumer, carrierId);
8797 sendRequestAsync(CMD_GET_ALLOWED_CARRIERS, callbackInfo);
8798 } finally {
8799 Binder.restoreCallingIdentity(identity);
8800 }
8801 }
8802
8803 @VisibleForTesting
8804 public int validateCallerAndGetCarrierId(String packageName) {
8805 CarrierAllowListInfo allowListInfo = CarrierAllowListInfo.loadInstance(mApp);
8806 return allowListInfo.validateCallerAndGetCarrierId(packageName);
8807 }
8808
8809 /**
fionaxu59545b42016-05-25 15:53:37 -07008810 * Action set from carrier signalling broadcast receivers to enable/disable radio
8811 * @param subId the subscription ID that this action applies to.
8812 * @param enabled control enable or disable radio.
8813 * {@hide}
8814 */
8815 @Override
8816 public void carrierActionSetRadioEnabled(int subId, boolean enabled) {
8817 enforceModifyPermission();
8818 final Phone phone = getPhone(subId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008819
8820 final long identity = Binder.clearCallingIdentity();
fionaxu59545b42016-05-25 15:53:37 -07008821 if (phone == null) {
8822 loge("carrierAction: SetRadioEnabled fails with invalid sibId: " + subId);
8823 return;
8824 }
8825 try {
8826 phone.carrierActionSetRadioEnabled(enabled);
8827 } catch (Exception e) {
8828 Log.e(LOG_TAG, "carrierAction: SetRadioEnabled fails. Exception ex=" + e);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008829 } finally {
8830 Binder.restoreCallingIdentity(identity);
fionaxu59545b42016-05-25 15:53:37 -07008831 }
8832 }
8833
Ta-wei Yenc236d6b2016-06-21 13:33:12 -07008834 /**
Sooraj Sasindrandaf060f2021-06-15 14:52:55 -07008835 * Enable or disable Voice over NR (VoNR)
8836 * @param subId the subscription ID that this action applies to.
8837 * @param enabled enable or disable VoNR.
8838 * @return operation result.
8839 */
8840 @Override
8841 public int setVoNrEnabled(int subId, boolean enabled) {
8842 enforceModifyPermission();
8843 final Phone phone = getPhone(subId);
8844
8845 final long identity = Binder.clearCallingIdentity();
8846 if (phone == null) {
8847 loge("setVoNrEnabled fails with no phone object for subId: " + subId);
8848 return TelephonyManager.ENABLE_VONR_RADIO_NOT_AVAILABLE;
8849 }
8850
8851 WorkSource workSource = getWorkSource(Binder.getCallingUid());
8852 try {
8853 int result = (int) sendRequest(CMD_ENABLE_VONR, enabled, subId,
8854 workSource);
8855 if (DBG) log("setVoNrEnabled result: " + result);
Gary Jian8dd305f2021-10-14 16:31:35 +08008856
8857 if (result == TelephonyManager.ENABLE_VONR_SUCCESS) {
8858 if (DBG) {
8859 log("Set VoNR settings in siminfo db; subId=" + subId + ", value:" + enabled);
8860 }
8861 SubscriptionManager.setSubscriptionProperty(
8862 subId, SubscriptionManager.NR_ADVANCED_CALLING_ENABLED,
8863 (enabled ? "1" : "0"));
8864 }
8865
Sooraj Sasindrandaf060f2021-06-15 14:52:55 -07008866 return result;
8867 } finally {
8868 Binder.restoreCallingIdentity(identity);
8869 }
8870 }
8871
8872 /**
8873 * Is voice over NR enabled
8874 * @return true if VoNR is enabled else false
8875 */
8876 @Override
8877 public boolean isVoNrEnabled(int subId) {
8878 enforceReadPrivilegedPermission("isVoNrEnabled");
8879 WorkSource workSource = getWorkSource(Binder.getCallingUid());
8880 final long identity = Binder.clearCallingIdentity();
8881 try {
8882 boolean isEnabled = (boolean) sendRequest(CMD_IS_VONR_ENABLED,
8883 null, subId, workSource);
8884 if (DBG) log("isVoNrEnabled: " + isEnabled);
8885 return isEnabled;
8886 } finally {
8887 Binder.restoreCallingIdentity(identity);
8888 }
8889 }
8890
8891 /**
fionaxu8da9cb12017-05-23 15:02:46 -07008892 * Action set from carrier signalling broadcast receivers to start/stop reporting the default
8893 * network status based on which carrier apps could apply actions accordingly,
8894 * enable/disable default url handler for example.
8895 *
8896 * @param subId the subscription ID that this action applies to.
8897 * @param report control start/stop reporting the default network status.
8898 * {@hide}
8899 */
8900 @Override
8901 public void carrierActionReportDefaultNetworkStatus(int subId, boolean report) {
8902 enforceModifyPermission();
8903 final Phone phone = getPhone(subId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008904
8905 final long identity = Binder.clearCallingIdentity();
fionaxu8da9cb12017-05-23 15:02:46 -07008906 if (phone == null) {
8907 loge("carrierAction: ReportDefaultNetworkStatus fails with invalid sibId: " + subId);
8908 return;
8909 }
8910 try {
8911 phone.carrierActionReportDefaultNetworkStatus(report);
8912 } catch (Exception e) {
8913 Log.e(LOG_TAG, "carrierAction: ReportDefaultNetworkStatus fails. Exception ex=" + e);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008914 } finally {
8915 Binder.restoreCallingIdentity(identity);
fionaxu8da9cb12017-05-23 15:02:46 -07008916 }
8917 }
8918
8919 /**
fionaxud9622282017-07-17 17:51:30 -07008920 * Action set from carrier signalling broadcast receivers to reset all carrier actions
8921 * @param subId the subscription ID that this action applies to.
8922 * {@hide}
8923 */
8924 @Override
8925 public void carrierActionResetAll(int subId) {
8926 enforceModifyPermission();
8927 final Phone phone = getPhone(subId);
8928 if (phone == null) {
8929 loge("carrierAction: ResetAll fails with invalid sibId: " + subId);
8930 return;
8931 }
8932 try {
8933 phone.carrierActionResetAll();
8934 } catch (Exception e) {
8935 Log.e(LOG_TAG, "carrierAction: ResetAll fails. Exception ex=" + e);
8936 }
8937 }
8938
8939 /**
Ta-wei Yenc236d6b2016-06-21 13:33:12 -07008940 * Called when "adb shell dumpsys phone" is invoked. Dump is also automatically invoked when a
8941 * bug report is being generated.
8942 */
8943 @Override
Ta-wei Yen99282e02016-06-21 18:19:35 -07008944 protected void dump(FileDescriptor fd, PrintWriter writer, String[] args) {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08008945 if (mApp.checkCallingOrSelfPermission(android.Manifest.permission.DUMP)
8946 != PackageManager.PERMISSION_GRANTED) {
dcashman22b950d2016-06-27 11:39:02 -07008947 writer.println("Permission Denial: can't dump Phone from pid="
8948 + Binder.getCallingPid()
8949 + ", uid=" + Binder.getCallingUid()
8950 + "without permission "
8951 + android.Manifest.permission.DUMP);
8952 return;
8953 }
Nazanin Bakhshif782e562018-12-11 15:15:39 -08008954 DumpsysHandler.dump(mApp, fd, writer, args);
Ta-wei Yenc236d6b2016-06-21 13:33:12 -07008955 }
Jack Yueb89b242016-06-22 13:27:47 -07008956
Brad Ebingerdac2f002018-04-03 15:17:52 -07008957 @Override
Hall Liua1548bd2019-12-24 14:14:12 -08008958 public int handleShellCommand(@NonNull ParcelFileDescriptor in,
8959 @NonNull ParcelFileDescriptor out, @NonNull ParcelFileDescriptor err,
8960 @NonNull String[] args) {
8961 return new TelephonyShellCommand(this, getDefaultPhone().getContext()).exec(
8962 this, in.getFileDescriptor(), out.getFileDescriptor(),
8963 err.getFileDescriptor(), args);
Brad Ebingerdac2f002018-04-03 15:17:52 -07008964 }
8965
Jack Yueb89b242016-06-22 13:27:47 -07008966 /**
Sooraj Sasindranaf1b5132020-05-05 21:06:20 +00008967 * Policy control of data connection with reason {@@TelephonyManager.DataEnabledReason}
Greg Kaiser17f41752020-05-05 16:47:47 +00008968 * @param subId Subscription index
Sarah Chinecc78c42022-03-31 21:16:48 -07008969 * @param reason The reason the data enable change is taking place.
Sooraj Sasindranaf1b5132020-05-05 21:06:20 +00008970 * @param enabled True if enabling the data, otherwise disabling.
Sarah Chinecc78c42022-03-31 21:16:48 -07008971 * @param callingPackage The package that changed the data enabled state.
Sooraj Sasindranaf1b5132020-05-05 21:06:20 +00008972 * @hide
Jack Yu75ab2952016-07-08 14:29:33 -07008973 */
8974 @Override
Sooraj Sasindran95c07a92020-07-15 01:35:24 -07008975 public void setDataEnabledForReason(int subId, @TelephonyManager.DataEnabledReason int reason,
Sarah Chinecc78c42022-03-31 21:16:48 -07008976 boolean enabled, String callingPackage) {
Sooraj Sasindranaf1b5132020-05-05 21:06:20 +00008977 if (reason == TelephonyManager.DATA_ENABLED_REASON_USER
8978 || reason == TelephonyManager.DATA_ENABLED_REASON_CARRIER) {
8979 try {
8980 TelephonyPermissions.enforceCallingOrSelfCarrierPrivilege(
Sooraj Sasindran95c07a92020-07-15 01:35:24 -07008981 mApp, subId, "setDataEnabledForReason");
Sooraj Sasindranaf1b5132020-05-05 21:06:20 +00008982 } catch (SecurityException se) {
8983 enforceModifyPermission();
8984 }
8985 } else {
8986 enforceModifyPermission();
8987 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008988
8989 final long identity = Binder.clearCallingIdentity();
8990 try {
8991 Phone phone = getPhone(subId);
8992 if (phone != null) {
Sooraj Sasindranaf1b5132020-05-05 21:06:20 +00008993 if (reason == TelephonyManager.DATA_ENABLED_REASON_CARRIER) {
8994 phone.carrierActionSetMeteredApnsEnabled(enabled);
8995 } else {
Jack Yu7968c6d2022-07-31 00:43:21 -07008996 phone.getDataSettingsManager().setDataEnabled(
8997 reason, enabled, callingPackage);
Sooraj Sasindranaf1b5132020-05-05 21:06:20 +00008998 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008999 }
9000 } finally {
9001 Binder.restoreCallingIdentity(identity);
Jack Yu75ab2952016-07-08 14:29:33 -07009002 }
9003 }
Sooraj Sasindran9a909312016-07-18 11:57:25 -07009004
9005 /**
9006 * Get Client request stats
9007 * @return List of Client Request Stats
9008 * @hide
9009 */
9010 @Override
Philip P. Moltmann700a9592019-10-03 11:53:50 -07009011 public List<ClientRequestStats> getClientRequestStats(String callingPackage,
9012 String callingFeatureId, int subId) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08009013 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
Philip P. Moltmann700a9592019-10-03 11:53:50 -07009014 mApp, subId, callingPackage, callingFeatureId, "getClientRequestStats")) {
Sooraj Sasindran9a909312016-07-18 11:57:25 -07009015 return null;
9016 }
Sooraj Sasindran9a909312016-07-18 11:57:25 -07009017 Phone phone = getPhone(subId);
Sooraj Sasindran9a909312016-07-18 11:57:25 -07009018
Malcolm Chenaa4a8532018-02-28 15:00:40 -08009019 final long identity = Binder.clearCallingIdentity();
9020 try {
9021 if (phone != null) {
9022 return phone.getClientRequestStats();
9023 }
9024
9025 return null;
9026 } finally {
9027 Binder.restoreCallingIdentity(identity);
9028 }
Sooraj Sasindran9a909312016-07-18 11:57:25 -07009029 }
9030
Narayan Kamathf04b5a12018-01-09 11:47:15 +00009031 private WorkSource getWorkSource(int uid) {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08009032 String packageName = mApp.getPackageManager().getNameForUid(uid);
Hunter Knepshieldd03383b2022-03-29 22:47:54 +00009033 if (uid == Process.ROOT_UID && packageName == null) {
9034 // Downstream WorkSource attribution inside the RIL requires both a UID and package name
9035 // to be set for wakelock tracking, otherwise RIL requests fail with a runtime
9036 // exception. ROOT_UID seems not to have a valid package name returned by
9037 // PackageManager, so just fake it here to avoid issues when running telephony shell
9038 // commands that plumb through the RIL as root, like so:
9039 // $ adb root
9040 // $ adb shell cmd phone ...
9041 packageName = "root";
9042 }
Narayan Kamathf04b5a12018-01-09 11:47:15 +00009043 return new WorkSource(uid, packageName);
Sooraj Sasindran9a909312016-07-18 11:57:25 -07009044 }
Jack Yueb4124c2017-02-16 15:32:43 -08009045
9046 /**
Grace Chen70990072017-03-24 17:21:30 -07009047 * Set SIM card power state.
Jack Yueb4124c2017-02-16 15:32:43 -08009048 *
Sanket Padawe13bac7b2017-03-20 15:04:47 -07009049 * @param slotIndex SIM slot id.
Grace Chen70990072017-03-24 17:21:30 -07009050 * @param state State of SIM (power down, power up, pass through)
9051 * - {@link android.telephony.TelephonyManager#CARD_POWER_DOWN}
9052 * - {@link android.telephony.TelephonyManager#CARD_POWER_UP}
9053 * - {@link android.telephony.TelephonyManager#CARD_POWER_UP_PASS_THROUGH}
Jack Yueb4124c2017-02-16 15:32:43 -08009054 *
9055 **/
9056 @Override
Grace Chen70990072017-03-24 17:21:30 -07009057 public void setSimPowerStateForSlot(int slotIndex, int state) {
Jack Yueb4124c2017-02-16 15:32:43 -08009058 enforceModifyPermission();
Sanket Padawe13bac7b2017-03-20 15:04:47 -07009059 Phone phone = PhoneFactory.getPhone(slotIndex);
9060
vagdeviaf9a5b92018-08-15 16:01:53 -07009061 WorkSource workSource = getWorkSource(Binder.getCallingUid());
9062
Malcolm Chenaa4a8532018-02-28 15:00:40 -08009063 final long identity = Binder.clearCallingIdentity();
9064 try {
9065 if (phone != null) {
Jordan Liu109698e2020-11-24 14:50:34 -08009066 phone.setSimPowerState(state, null, workSource);
9067 }
9068 } finally {
9069 Binder.restoreCallingIdentity(identity);
9070 }
9071 }
9072
9073 /**
9074 * Set SIM card power state.
9075 *
9076 * @param slotIndex SIM slot id.
9077 * @param state State of SIM (power down, power up, pass through)
9078 * @param callback callback to trigger after success or failure
9079 * - {@link android.telephony.TelephonyManager#CARD_POWER_DOWN}
9080 * - {@link android.telephony.TelephonyManager#CARD_POWER_UP}
9081 * - {@link android.telephony.TelephonyManager#CARD_POWER_UP_PASS_THROUGH}
9082 *
9083 **/
9084 @Override
9085 public void setSimPowerStateForSlotWithCallback(int slotIndex, int state,
9086 IIntegerConsumer callback) {
9087 enforceModifyPermission();
9088 Phone phone = PhoneFactory.getPhone(slotIndex);
9089
9090 WorkSource workSource = getWorkSource(Binder.getCallingUid());
9091
9092 final long identity = Binder.clearCallingIdentity();
9093 try {
9094 if (phone != null) {
9095 Pair<Integer, IIntegerConsumer> arguments = Pair.create(state, callback);
9096 sendRequestAsync(CMD_SET_SIM_POWER, arguments, phone, workSource);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08009097 }
9098 } finally {
9099 Binder.restoreCallingIdentity(identity);
Jack Yueb4124c2017-02-16 15:32:43 -08009100 }
9101 }
Shuo Qiandd210312017-04-12 22:11:33 +00009102
Tyler Gunn65d45c22017-06-05 11:22:26 -07009103 private boolean isUssdApiAllowed(int subId) {
9104 CarrierConfigManager configManager =
Nazanin Bakhshif782e562018-12-11 15:15:39 -08009105 (CarrierConfigManager) mApp.getSystemService(Context.CARRIER_CONFIG_SERVICE);
Tyler Gunn65d45c22017-06-05 11:22:26 -07009106 if (configManager == null) {
9107 return false;
9108 }
9109 PersistableBundle pb = configManager.getConfigForSubId(subId);
9110 if (pb == null) {
9111 return false;
9112 }
9113 return pb.getBoolean(
9114 CarrierConfigManager.KEY_ALLOW_USSD_REQUESTS_VIA_TELEPHONY_MANAGER_BOOL);
9115 }
9116
Shuo Qiandd210312017-04-12 22:11:33 +00009117 /**
9118 * Check if phone is in emergency callback mode
9119 * @return true if phone is in emergency callback mode
9120 * @param subId sub id
9121 */
goneil9c5f4872017-12-05 14:07:56 -08009122 @Override
Shuo Qiandd210312017-04-12 22:11:33 +00009123 public boolean getEmergencyCallbackMode(int subId) {
Brad Ebinger35c841c2018-10-01 10:40:55 -07009124 enforceReadPrivilegedPermission("getEmergencyCallbackMode");
Shuo Qiandd210312017-04-12 22:11:33 +00009125 final Phone phone = getPhone(subId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08009126
9127 final long identity = Binder.clearCallingIdentity();
9128 try {
9129 if (phone != null) {
9130 return phone.isInEcm();
9131 } else {
9132 return false;
9133 }
9134 } finally {
9135 Binder.restoreCallingIdentity(identity);
Shuo Qiandd210312017-04-12 22:11:33 +00009136 }
9137 }
Nathan Harold46b42aa2017-03-10 19:38:22 -08009138
9139 /**
9140 * Get the current signal strength information for the given subscription.
9141 * Because this information is not updated when the device is in a low power state
9142 * it should not be relied-upon to be current.
9143 * @param subId Subscription index
9144 * @return the most recent cached signal strength info from the modem
9145 */
9146 @Override
9147 public SignalStrength getSignalStrength(int subId) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08009148 final long identity = Binder.clearCallingIdentity();
9149 try {
9150 Phone p = getPhone(subId);
9151 if (p == null) {
9152 return null;
9153 }
Nathan Harold46b42aa2017-03-10 19:38:22 -08009154
Malcolm Chenaa4a8532018-02-28 15:00:40 -08009155 return p.getSignalStrength();
9156 } finally {
9157 Binder.restoreCallingIdentity(identity);
9158 }
Nathan Harold46b42aa2017-03-10 19:38:22 -08009159 }
Holly Jiuyu Sun01c47ad2018-01-24 17:56:33 +00009160
Pengquan Meng77b7f132018-08-22 14:49:57 -07009161 /**
Chen Xuf792fd62018-10-17 17:54:36 +00009162 * Get the current modem radio state for the given slot.
9163 * @param slotIndex slot index.
9164 * @param callingPackage the name of the package making the call.
Philip P. Moltmann700a9592019-10-03 11:53:50 -07009165 * @param callingFeatureId The feature in the package.
Chen Xuf792fd62018-10-17 17:54:36 +00009166 * @return the current radio power state from the modem
9167 */
9168 @Override
Philip P. Moltmann700a9592019-10-03 11:53:50 -07009169 public int getRadioPowerState(int slotIndex, String callingPackage, String callingFeatureId) {
Chen Xuf792fd62018-10-17 17:54:36 +00009170 Phone phone = PhoneFactory.getPhone(slotIndex);
9171 if (phone != null) {
Philip P. Moltmann700a9592019-10-03 11:53:50 -07009172 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(mApp, phone.getSubId(),
9173 callingPackage, callingFeatureId, "getRadioPowerState")) {
Chen Xuf792fd62018-10-17 17:54:36 +00009174 return TelephonyManager.RADIO_POWER_UNAVAILABLE;
9175 }
9176
9177 final long identity = Binder.clearCallingIdentity();
9178 try {
9179 return phone.getRadioPowerState();
9180 } finally {
9181 Binder.restoreCallingIdentity(identity);
9182 }
9183 }
9184 return TelephonyManager.RADIO_POWER_UNAVAILABLE;
9185 }
9186
9187 /**
Pengquan Meng77b7f132018-08-22 14:49:57 -07009188 * Checks if data roaming is enabled on the subscription with id {@code subId}.
9189 *
9190 * <p>Requires one of the following permissions:
9191 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE},
Sooraj Sasindran5d051bf2021-11-05 13:14:15 -07009192 * {@link android.Manifest.permission#READ_BASIC_PHONE_STATE},
Pengquan Meng77b7f132018-08-22 14:49:57 -07009193 * {@link android.Manifest.permission#READ_PHONE_STATE} or that the calling app has carrier
9194 * privileges.
9195 *
9196 * @param subId subscription id
9197 * @return {@code true} if data roaming is enabled on this subscription, otherwise return
9198 * {@code false}.
9199 */
9200 @Override
9201 public boolean isDataRoamingEnabled(int subId) {
Sooraj Sasindran5d051bf2021-11-05 13:14:15 -07009202 String functionName = "isDataRoamingEnabled";
Shuo Qian093013d2020-08-13 15:42:55 -07009203 try {
Sooraj Sasindran5d051bf2021-11-05 13:14:15 -07009204 try {
9205 mApp.enforceCallingOrSelfPermission(
9206 android.Manifest.permission.ACCESS_NETWORK_STATE,
9207 functionName);
Sooraj Sasindran12545cc2022-08-22 14:30:25 -07009208 } catch (SecurityException e) {
Sooraj Sasindran5d051bf2021-11-05 13:14:15 -07009209 mApp.enforceCallingOrSelfPermission(
9210 permission.READ_BASIC_PHONE_STATE, functionName);
9211 }
Sooraj Sasindran12545cc2022-08-22 14:30:25 -07009212 } catch (SecurityException e) {
Nathan Harold62c68512021-04-06 11:26:02 -07009213 TelephonyPermissions.enforceCallingOrSelfReadPhoneStatePermissionOrCarrierPrivilege(
Sooraj Sasindran5d051bf2021-11-05 13:14:15 -07009214 mApp, subId, functionName);
Shuo Qian093013d2020-08-13 15:42:55 -07009215 }
Pengquan Meng44e66f12019-04-01 10:48:20 -07009216
Pengquan Menga1bb6272018-09-06 09:59:22 -07009217 boolean isEnabled = false;
9218 final long identity = Binder.clearCallingIdentity();
Pengquan Meng77b7f132018-08-22 14:49:57 -07009219 try {
Pengquan Menga1bb6272018-09-06 09:59:22 -07009220 Phone phone = getPhone(subId);
9221 isEnabled = phone != null ? phone.getDataRoamingEnabled() : false;
Pengquan Menga1bb6272018-09-06 09:59:22 -07009222 } finally {
9223 Binder.restoreCallingIdentity(identity);
Pengquan Meng77b7f132018-08-22 14:49:57 -07009224 }
Pengquan Menga1bb6272018-09-06 09:59:22 -07009225 return isEnabled;
Pengquan Meng77b7f132018-08-22 14:49:57 -07009226 }
9227
9228
9229 /**
9230 * Enables/Disables the data roaming on the subscription with id {@code subId}.
9231 *
9232 * <p> Requires permission:
9233 * {@link android.Manifest.permission#MODIFY_PHONE_STATE} or that the calling app has carrier
9234 * privileges.
9235 *
9236 * @param subId subscription id
9237 * @param isEnabled {@code true} means enable, {@code false} means disable.
9238 */
9239 @Override
9240 public void setDataRoamingEnabled(int subId, boolean isEnabled) {
Pengquan Meng44e66f12019-04-01 10:48:20 -07009241 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
9242 mApp, subId, "setDataRoamingEnabled");
9243
Pengquan Menga1bb6272018-09-06 09:59:22 -07009244 final long identity = Binder.clearCallingIdentity();
9245 try {
Pengquan Menga1bb6272018-09-06 09:59:22 -07009246 Phone phone = getPhone(subId);
9247 if (phone != null) {
9248 phone.setDataRoamingEnabled(isEnabled);
9249 }
9250 } finally {
9251 Binder.restoreCallingIdentity(identity);
Pengquan Meng77b7f132018-08-22 14:49:57 -07009252 }
9253 }
9254
Holly Jiuyu Sun01c47ad2018-01-24 17:56:33 +00009255 @Override
Pengquan Meng6884a2c2018-10-03 12:19:13 -07009256 public boolean isManualNetworkSelectionAllowed(int subId) {
tom hsuc91afc72020-01-06 23:46:07 +08009257 TelephonyPermissions
Nathan Harold62c68512021-04-06 11:26:02 -07009258 .enforceCallingOrSelfReadPrivilegedPhoneStatePermissionOrCarrierPrivilege(
Pengquan Meng44e66f12019-04-01 10:48:20 -07009259 mApp, subId, "isManualNetworkSelectionAllowed");
9260
Pengquan Meng6884a2c2018-10-03 12:19:13 -07009261 boolean isAllowed = true;
9262 final long identity = Binder.clearCallingIdentity();
9263 try {
Pengquan Meng6884a2c2018-10-03 12:19:13 -07009264 Phone phone = getPhone(subId);
9265 if (phone != null) {
9266 isAllowed = phone.isCspPlmnEnabled();
9267 }
9268 } finally {
9269 Binder.restoreCallingIdentity(identity);
9270 }
9271 return isAllowed;
9272 }
9273
Muralidhar Reddyeb809e32021-11-19 03:07:54 +00009274 private boolean haveCarrierPrivilegeAccess(UiccPort port, String callingPackage) {
9275 UiccProfile profile = port.getUiccProfile();
Hunter Knepshieldcad7f0b2021-12-13 15:08:35 -08009276 if (profile == null) {
Muralidhar Reddyeb809e32021-11-19 03:07:54 +00009277 return false;
Muralidhar Reddy472c2ae2021-09-29 19:38:40 +00009278 }
Hunter Knepshieldcad7f0b2021-12-13 15:08:35 -08009279 Phone phone = PhoneFactory.getPhone(profile.getPhoneId());
9280 if (phone == null) {
9281 return false;
9282 }
9283 CarrierPrivilegesTracker cpt = phone.getCarrierPrivilegesTracker();
9284 return cpt != null && cpt.getCarrierPrivilegeStatusForPackage(callingPackage)
9285 == TelephonyManager.CARRIER_PRIVILEGE_STATUS_HAS_ACCESS;
Muralidhar Reddy472c2ae2021-09-29 19:38:40 +00009286 }
9287
Pengquan Meng6884a2c2018-10-03 12:19:13 -07009288 @Override
Jordan Liu75f43ea2019-01-17 16:56:37 -08009289 public List<UiccCardInfo> getUiccCardsInfo(String callingPackage) {
sandeepjsa208e3b2021-11-17 04:05:58 +00009290 // Verify that the callingPackage belongs to the calling UID
Jordan Liu4cda4552020-03-23 11:55:07 -07009291 mApp.getSystemService(AppOpsManager.class)
9292 .checkPackage(Binder.getCallingUid(), callingPackage);
9293
Jordan Liu1e142fc2019-04-22 15:10:43 -07009294 boolean hasReadPermission = false;
sandeepjsb6c87872021-09-27 15:34:44 +00009295 boolean isIccIdAccessRestricted = false;
Jordan Liuc65bc952019-02-12 17:54:02 -08009296 try {
9297 enforceReadPrivilegedPermission("getUiccCardsInfo");
Jordan Liu1e142fc2019-04-22 15:10:43 -07009298 hasReadPermission = true;
Jordan Liuc65bc952019-02-12 17:54:02 -08009299 } catch (SecurityException e) {
9300 // even without READ_PRIVILEGED_PHONE_STATE, we allow the call to continue if the caller
9301 // has carrier privileges on an active UICC
Rambo Wange7209ce2022-02-23 13:41:02 -08009302 if (checkCarrierPrivilegesForPackageAnyPhoneWithPermission(callingPackage)
Jordan Liuc65bc952019-02-12 17:54:02 -08009303 != TelephonyManager.CARRIER_PRIVILEGE_STATUS_HAS_ACCESS) {
Jordan Liu1e142fc2019-04-22 15:10:43 -07009304 throw new SecurityException("Caller does not have permission.");
Jordan Liuc65bc952019-02-12 17:54:02 -08009305 }
Jordan Liu75f43ea2019-01-17 16:56:37 -08009306 }
sandeepjsb6c87872021-09-27 15:34:44 +00009307 // checking compatibility, if calling app's target SDK is T and beyond.
9308 if (CompatChanges.isChangeEnabled(GET_API_SIGNATURES_FROM_UICC_PORT_INFO,
9309 Binder.getCallingUid())) {
9310 isIccIdAccessRestricted = true;
9311 }
Jordan Liu5aa07002018-12-18 15:44:48 -08009312 final long identity = Binder.clearCallingIdentity();
9313 try {
Jordan Liu75f43ea2019-01-17 16:56:37 -08009314 UiccController uiccController = UiccController.getInstance();
9315 ArrayList<UiccCardInfo> cardInfos = uiccController.getAllUiccCardInfos();
Jordan Liu1e142fc2019-04-22 15:10:43 -07009316 if (hasReadPermission) {
9317 return cardInfos;
Jordan Liu75f43ea2019-01-17 16:56:37 -08009318 }
Jordan Liu1e142fc2019-04-22 15:10:43 -07009319
9320 // Remove private info if the caller doesn't have access
9321 ArrayList<UiccCardInfo> filteredInfos = new ArrayList<>();
9322 for (UiccCardInfo cardInfo : cardInfos) {
sandeepjsb6c87872021-09-27 15:34:44 +00009323 //setting the value after compatibility check
9324 cardInfo.setIccIdAccessRestricted(isIccIdAccessRestricted);
Jordan Liu1e142fc2019-04-22 15:10:43 -07009325 // For an inactive eUICC, the UiccCard will be null even though the UiccCardInfo
9326 // is available
sandeepjsb6c87872021-09-27 15:34:44 +00009327 UiccCard card = uiccController.getUiccCardForSlot(cardInfo.getPhysicalSlotIndex());
Muralidhar Reddyeb809e32021-11-19 03:07:54 +00009328 if (card == null) {
9329 // assume no access if the card is unavailable
sandeepjsb6c87872021-09-27 15:34:44 +00009330 filteredInfos.add(getUiccCardInfoUnPrivileged(cardInfo));
Jordan Liu1e142fc2019-04-22 15:10:43 -07009331 continue;
9332 }
Muralidhar Reddyeb809e32021-11-19 03:07:54 +00009333 Collection<UiccPortInfo> portInfos = cardInfo.getPorts();
9334 if (portInfos.isEmpty()) {
sandeepjsb6c87872021-09-27 15:34:44 +00009335 filteredInfos.add(getUiccCardInfoUnPrivileged(cardInfo));
Muralidhar Reddyeb809e32021-11-19 03:07:54 +00009336 continue;
Jordan Liu1e142fc2019-04-22 15:10:43 -07009337 }
Muralidhar Reddyeb809e32021-11-19 03:07:54 +00009338 List<UiccPortInfo> uiccPortInfos = new ArrayList<>();
9339 for (UiccPortInfo portInfo : portInfos) {
9340 UiccPort port = uiccController.getUiccPortForSlot(
9341 cardInfo.getPhysicalSlotIndex(), portInfo.getPortIndex());
9342 if (port == null) {
9343 // assume no access if port is null
9344 uiccPortInfos.add(getUiccPortInfoUnPrivileged(portInfo));
9345 continue;
9346 }
9347 if (haveCarrierPrivilegeAccess(port, callingPackage)) {
9348 uiccPortInfos.add(portInfo);
9349 } else {
9350 uiccPortInfos.add(getUiccPortInfoUnPrivileged(portInfo));
9351 }
9352 }
9353 filteredInfos.add(new UiccCardInfo(
9354 cardInfo.isEuicc(),
9355 cardInfo.getCardId(),
9356 null,
9357 cardInfo.getPhysicalSlotIndex(),
9358 cardInfo.isRemovable(),
9359 cardInfo.isMultipleEnabledProfilesSupported(),
9360 uiccPortInfos));
Jordan Liu1e142fc2019-04-22 15:10:43 -07009361 }
9362 return filteredInfos;
Jordan Liu5aa07002018-12-18 15:44:48 -08009363 } finally {
9364 Binder.restoreCallingIdentity(identity);
9365 }
9366 }
9367
sandeepjsb6c87872021-09-27 15:34:44 +00009368 /**
9369 * Returns a copy of the UiccCardinfo with the EID and ICCID set to null. These values are
9370 * generally private and require carrier privileges to view.
9371 *
9372 * @hide
9373 */
9374 @NonNull
9375 public UiccCardInfo getUiccCardInfoUnPrivileged(UiccCardInfo cardInfo) {
9376 List<UiccPortInfo> portinfo = new ArrayList<>();
9377 for (UiccPortInfo portinfos : cardInfo.getPorts()) {
9378 portinfo.add(getUiccPortInfoUnPrivileged(portinfos));
9379 }
9380 return new UiccCardInfo(
9381 cardInfo.isEuicc(),
9382 cardInfo.getCardId(),
9383 null,
9384 cardInfo.getPhysicalSlotIndex(),
9385 cardInfo.isRemovable(),
9386 cardInfo.isMultipleEnabledProfilesSupported(),
9387 portinfo
9388 );
9389 }
Holly Jiuyu Sun01c47ad2018-01-24 17:56:33 +00009390
sandeepjsb6c87872021-09-27 15:34:44 +00009391 /**
9392 * @hide
9393 * @return a copy of the UiccPortInfo with ICCID set to {@link UiccPortInfo#ICCID_REDACTED}.
9394 * These values are generally private and require carrier privileges to view.
9395 */
9396 @NonNull
9397 public UiccPortInfo getUiccPortInfoUnPrivileged(UiccPortInfo portInfo) {
9398 return new UiccPortInfo(
9399 UiccPortInfo.ICCID_REDACTED,
9400 portInfo.getPortIndex(),
9401 portInfo.getLogicalSlotIndex(),
9402 portInfo.isActive()
9403 );
9404 }
9405 @Override
9406 public UiccSlotInfo[] getUiccSlotsInfo(String callingPackage) {
sandeepjsa208e3b2021-11-17 04:05:58 +00009407 // Verify that the callingPackage belongs to the calling UID
sandeepjsb6c87872021-09-27 15:34:44 +00009408 mApp.getSystemService(AppOpsManager.class)
9409 .checkPackage(Binder.getCallingUid(), callingPackage);
9410
sandeepjsb6c87872021-09-27 15:34:44 +00009411 boolean isLogicalSlotAccessRestricted = false;
sandeepjsb6c87872021-09-27 15:34:44 +00009412
Aman Guptaf3c90b32022-03-17 04:54:16 +00009413 // This will make sure caller has the READ_PRIVILEGED_PHONE_STATE. Do not remove this as
9414 // we are reading iccId which is PII data.
9415 enforceReadPrivilegedPermission("getUiccSlotsInfo");
sandeepjsb6c87872021-09-27 15:34:44 +00009416
9417 // checking compatibility, if calling app's target SDK is T and beyond.
9418 if (CompatChanges.isChangeEnabled(GET_API_SIGNATURES_FROM_UICC_PORT_INFO,
9419 Binder.getCallingUid())) {
9420 isLogicalSlotAccessRestricted = true;
9421 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08009422 final long identity = Binder.clearCallingIdentity();
9423 try {
9424 UiccSlot[] slots = UiccController.getInstance().getUiccSlots();
Muralidhar Reddyd196bbf2022-01-17 17:56:30 +00009425 if (slots == null || slots.length == 0) {
9426 Rlog.i(LOG_TAG, "slots is null or empty.");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08009427 return null;
9428 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08009429 UiccSlotInfo[] infos = new UiccSlotInfo[slots.length];
9430 for (int i = 0; i < slots.length; i++) {
9431 UiccSlot slot = slots[i];
9432 if (slot == null) {
9433 continue;
9434 }
9435
Jordan Liu7be7e652019-05-06 18:55:02 +00009436 String cardId;
Malcolm Chenaa4a8532018-02-28 15:00:40 -08009437 UiccCard card = slot.getUiccCard();
9438 if (card != null) {
9439 cardId = card.getCardId();
Jordan Liu7be7e652019-05-06 18:55:02 +00009440 } else {
Jordan Liu01bd00d2019-09-12 16:19:43 -07009441 cardId = slot.getEid();
9442 if (TextUtils.isEmpty(cardId)) {
Aman Guptaf3c90b32022-03-17 04:54:16 +00009443 // If cardId is null, use iccId of default port as cardId.
9444 cardId = slot.getIccId(TelephonyManager.DEFAULT_PORT_INDEX);
Jordan Liu01bd00d2019-09-12 16:19:43 -07009445 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08009446 }
9447
Jordan Liu857451f2019-05-09 16:35:35 -07009448 if (cardId != null) {
9449 // if cardId is an ICCID, strip off trailing Fs before exposing to user
9450 // if cardId is an EID, it's all digits so this is fine
9451 cardId = IccUtils.stripTrailingFs(cardId);
9452 }
9453
Malcolm Chenaa4a8532018-02-28 15:00:40 -08009454 int cardState = 0;
9455 switch (slot.getCardState()) {
9456 case CARDSTATE_ABSENT:
9457 cardState = UiccSlotInfo.CARD_STATE_INFO_ABSENT;
9458 break;
9459 case CARDSTATE_PRESENT:
9460 cardState = UiccSlotInfo.CARD_STATE_INFO_PRESENT;
9461 break;
9462 case CARDSTATE_ERROR:
9463 cardState = UiccSlotInfo.CARD_STATE_INFO_ERROR;
9464 break;
9465 case CARDSTATE_RESTRICTED:
9466 cardState = UiccSlotInfo.CARD_STATE_INFO_RESTRICTED;
9467 break;
9468 default:
9469 break;
9470
9471 }
Muralidhar Reddyeb809e32021-11-19 03:07:54 +00009472 List<UiccPortInfo> portInfos = new ArrayList<>();
9473 int[] portIndexes = slot.getPortList();
9474 for (int portIdx : portIndexes) {
9475 String iccId = IccUtils.stripTrailingFs(getIccId(slot, portIdx,
Aman Guptaf3c90b32022-03-17 04:54:16 +00009476 callingPackage, /* hasReadPermission= */ true));
Muralidhar Reddyfbcff0c2022-01-19 13:07:57 +00009477 portInfos.add(new UiccPortInfo(iccId, portIdx,
9478 slot.getPhoneIdFromPortIndex(portIdx), slot.isPortActive(portIdx)));
Muralidhar Reddyeb809e32021-11-19 03:07:54 +00009479 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08009480 infos[i] = new UiccSlotInfo(
Malcolm Chenaa4a8532018-02-28 15:00:40 -08009481 slot.isEuicc(),
9482 cardId,
9483 cardState,
Jordan Liua2619582019-02-14 12:56:40 -08009484 slot.isExtendedApduSupported(),
Muralidhar Reddyeb809e32021-11-19 03:07:54 +00009485 slot.isRemovable(), portInfos);
sandeepjsb6c87872021-09-27 15:34:44 +00009486 //setting the value after compatibility check
9487 infos[i].setLogicalSlotAccessRestricted(isLogicalSlotAccessRestricted);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08009488 }
9489 return infos;
9490 } finally {
9491 Binder.restoreCallingIdentity(identity);
Holly Jiuyu Sun1d957c52018-04-04 13:52:42 -07009492 }
Holly Jiuyu Sun01c47ad2018-01-24 17:56:33 +00009493 }
9494
Muralidhar Reddyeb809e32021-11-19 03:07:54 +00009495 /* Returns null if doesn't have read permission or carrier privilege access. */
9496 private String getIccId(UiccSlot slot, int portIndex, String callingPackage,
9497 boolean hasReadPermission) {
9498 String iccId = slot.getIccId(portIndex);
9499 if (hasReadPermission) { // if has read permission
9500 return iccId;
9501 } else {
9502 if (slot.getUiccCard() != null && slot.getUiccCard().getUiccPort(portIndex) != null) {
9503 UiccPort port = slot.getUiccCard().getUiccPort(portIndex);
9504 // if no read permission, checking carrier privilege access
9505 if (haveCarrierPrivilegeAccess(port, callingPackage)) {
9506 return iccId;
9507 }
9508 }
9509 }
9510 // No read permission or carrier privilege access.
9511 return UiccPortInfo.ICCID_REDACTED;
9512 }
9513
Holly Jiuyu Sun01c47ad2018-01-24 17:56:33 +00009514 @Override
sandeepjsb6c87872021-09-27 15:34:44 +00009515 @Deprecated
Holly Jiuyu Sun01c47ad2018-01-24 17:56:33 +00009516 public boolean switchSlots(int[] physicalSlots) {
9517 enforceModifyPermission();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08009518
9519 final long identity = Binder.clearCallingIdentity();
9520 try {
Muralidhar Reddyeb809e32021-11-19 03:07:54 +00009521 List<UiccSlotMapping> slotMappings = new ArrayList<>();
9522 for (int i = 0; i < physicalSlots.length; i++) {
9523 // Deprecated API, hence MEP is not supported. Adding default portIndex 0.
9524 slotMappings.add(new UiccSlotMapping(TelephonyManager.DEFAULT_PORT_INDEX,
9525 physicalSlots[i], i));
9526 }
9527 return (Boolean) sendRequest(CMD_SWITCH_SLOTS, slotMappings);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08009528 } finally {
9529 Binder.restoreCallingIdentity(identity);
9530 }
Holly Jiuyu Sun01c47ad2018-01-24 17:56:33 +00009531 }
Jack Yu4c988042018-02-27 15:30:01 -08009532
9533 @Override
sandeepjsb6c87872021-09-27 15:34:44 +00009534 @RequiresPermission(android.Manifest.permission.MODIFY_PHONE_STATE)
9535 public boolean setSimSlotMapping(@NonNull List<UiccSlotMapping> slotMapping) {
9536 enforceModifyPermission();
9537
9538 final long identity = Binder.clearCallingIdentity();
9539 try {
Muralidhar Reddyeb809e32021-11-19 03:07:54 +00009540 return (Boolean) sendRequest(CMD_SWITCH_SLOTS, slotMapping);
sandeepjsb6c87872021-09-27 15:34:44 +00009541 } finally {
9542 Binder.restoreCallingIdentity(identity);
9543 }
9544 }
9545
9546 @Override
Jordan Liu7de49fa2018-12-06 14:48:49 -08009547 public int getCardIdForDefaultEuicc(int subId, String callingPackage) {
Jordan Liu7de49fa2018-12-06 14:48:49 -08009548 final long identity = Binder.clearCallingIdentity();
9549 try {
9550 return UiccController.getInstance().getCardIdForDefaultEuicc();
9551 } finally {
9552 Binder.restoreCallingIdentity(identity);
9553 }
9554 }
9555
Pengquan Meng85728fb2018-03-12 16:31:21 -07009556 /**
goneil47ffb6e2018-04-06 15:40:58 -07009557 * A test API to reload the UICC profile.
9558 *
9559 * <p>Requires that the calling app has permission
9560 * {@link android.Manifest.permission#MODIFY_PHONE_STATE MODIFY_PHONE_STATE}.
9561 * @hide
9562 */
9563 @Override
9564 public void refreshUiccProfile(int subId) {
9565 enforceModifyPermission();
9566
9567 final long identity = Binder.clearCallingIdentity();
9568 try {
9569 Phone phone = getPhone(subId);
9570 if (phone == null) {
9571 return;
9572 }
Muralidhar Reddy472c2ae2021-09-29 19:38:40 +00009573 UiccPort uiccPort = phone.getUiccPort();
9574 if (uiccPort == null) {
goneil47ffb6e2018-04-06 15:40:58 -07009575 return;
9576 }
Muralidhar Reddy472c2ae2021-09-29 19:38:40 +00009577 UiccProfile uiccProfile = uiccPort.getUiccProfile();
goneil47ffb6e2018-04-06 15:40:58 -07009578 if (uiccProfile == null) {
9579 return;
9580 }
9581 uiccProfile.refresh();
9582 } finally {
9583 Binder.restoreCallingIdentity(identity);
9584 }
9585 }
9586
9587 /**
Pengquan Meng85728fb2018-03-12 16:31:21 -07009588 * Returns false if the mobile data is disabled by default, otherwise return true.
9589 */
9590 private boolean getDefaultDataEnabled() {
Inseob Kim14bb3d02018-12-13 17:11:34 +09009591 return TelephonyProperties.mobile_data().orElse(true);
Pengquan Meng85728fb2018-03-12 16:31:21 -07009592 }
9593
9594 /**
9595 * Returns true if the data roaming is enabled by default, i.e the system property
9596 * of {@link #DEFAULT_DATA_ROAMING_PROPERTY_NAME} is true or the config of
9597 * {@link CarrierConfigManager#KEY_CARRIER_DEFAULT_DATA_ROAMING_ENABLED_BOOL} is true.
9598 */
9599 private boolean getDefaultDataRoamingEnabled(int subId) {
9600 final CarrierConfigManager configMgr = (CarrierConfigManager)
Nazanin Bakhshif782e562018-12-11 15:15:39 -08009601 mApp.getSystemService(Context.CARRIER_CONFIG_SERVICE);
Shuo Qian1d84a0e2020-07-15 12:36:44 -07009602 boolean isDataRoamingEnabled = TelephonyProperties.data_roaming().orElse(false);
Pengquan Meng85728fb2018-03-12 16:31:21 -07009603 isDataRoamingEnabled |= configMgr.getConfigForSubId(subId).getBoolean(
9604 CarrierConfigManager.KEY_CARRIER_DEFAULT_DATA_ROAMING_ENABLED_BOOL);
9605 return isDataRoamingEnabled;
9606 }
9607
9608 /**
9609 * Returns the default network type for the given {@code subId}, if the default network type is
9610 * not set, return {@link Phone#PREFERRED_NT_MODE}.
9611 */
9612 private int getDefaultNetworkType(int subId) {
Inseob Kim14bb3d02018-12-13 17:11:34 +09009613 List<Integer> list = TelephonyProperties.default_network();
Jack Yu285100e2022-12-02 22:48:35 -08009614 int phoneId = SubscriptionManager.getPhoneId(subId);
Inseob Kim14bb3d02018-12-13 17:11:34 +09009615 if (phoneId >= 0 && phoneId < list.size() && list.get(phoneId) != null) {
9616 return list.get(phoneId);
9617 }
9618 return Phone.PREFERRED_NT_MODE;
Pengquan Meng85728fb2018-03-12 16:31:21 -07009619 }
fionaxua13278b2018-03-21 00:08:13 -07009620
9621 @Override
9622 public void setCarrierTestOverride(int subId, String mccmnc, String imsi, String iccid, String
chen xueaba88a2019-03-15 13:15:10 -07009623 gid1, String gid2, String plmn, String spn, String carrierPrivilegeRules, String apn) {
fionaxua13278b2018-03-21 00:08:13 -07009624 enforceModifyPermission();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08009625
9626 final long identity = Binder.clearCallingIdentity();
9627 try {
9628 final Phone phone = getPhone(subId);
9629 if (phone == null) {
9630 loge("setCarrierTestOverride fails with invalid subId: " + subId);
9631 return;
9632 }
Rambo Wang9c9ffdd2022-01-13 21:51:44 -08009633 CarrierPrivilegesTracker cpt = phone.getCarrierPrivilegesTracker();
9634 if (cpt != null) {
9635 cpt.setTestOverrideCarrierPrivilegeRules(carrierPrivilegeRules);
9636 }
9637 // TODO(b/211796398): remove the legacy logic below once CPT migration is done.
chen xueaba88a2019-03-15 13:15:10 -07009638 phone.setCarrierTestOverride(mccmnc, imsi, iccid, gid1, gid2, plmn, spn,
9639 carrierPrivilegeRules, apn);
Jeff Davidson8ab02b22020-03-28 12:24:40 -07009640 if (carrierPrivilegeRules == null) {
9641 mCarrierPrivilegeTestOverrideSubIds.remove(subId);
9642 } else {
9643 mCarrierPrivilegeTestOverrideSubIds.add(subId);
9644 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08009645 } finally {
9646 Binder.restoreCallingIdentity(identity);
fionaxua13278b2018-03-21 00:08:13 -07009647 }
fionaxua13278b2018-03-21 00:08:13 -07009648 }
9649
9650 @Override
9651 public int getCarrierIdListVersion(int subId) {
Brad Ebinger35c841c2018-10-01 10:40:55 -07009652 enforceReadPrivilegedPermission("getCarrierIdListVersion");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08009653
9654 final long identity = Binder.clearCallingIdentity();
9655 try {
9656 final Phone phone = getPhone(subId);
9657 if (phone == null) {
9658 loge("getCarrierIdListVersion fails with invalid subId: " + subId);
9659 return TelephonyManager.UNKNOWN_CARRIER_ID_LIST_VERSION;
9660 }
9661 return phone.getCarrierIdListVersion();
9662 } finally {
9663 Binder.restoreCallingIdentity(identity);
fionaxua13278b2018-03-21 00:08:13 -07009664 }
fionaxua13278b2018-03-21 00:08:13 -07009665 }
Malcolm Chen2c63d402018-08-14 16:00:53 -07009666
9667 @Override
Philip P. Moltmann700a9592019-10-03 11:53:50 -07009668 public int getNumberOfModemsWithSimultaneousDataConnections(int subId, String callingPackage,
9669 String callingFeatureId) {
Malcolm Chen2c63d402018-08-14 16:00:53 -07009670 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
Philip P. Moltmann700a9592019-10-03 11:53:50 -07009671 mApp, subId, callingPackage, callingFeatureId,
9672 "getNumberOfModemsWithSimultaneousDataConnections")) {
Malcolm Chen2c63d402018-08-14 16:00:53 -07009673 return -1;
9674 }
9675
9676 final long identity = Binder.clearCallingIdentity();
9677 try {
9678 return mPhoneConfigurationManager.getNumberOfModemsWithSimultaneousDataConnections();
9679 } finally {
9680 Binder.restoreCallingIdentity(identity);
9681 }
9682 }
Pengquan Menga1bb6272018-09-06 09:59:22 -07009683
9684 @Override
9685 public int getCdmaRoamingMode(int subId) {
zoey chen7e6d4e52019-12-17 18:18:59 +08009686 TelephonyPermissions
Nathan Harold62c68512021-04-06 11:26:02 -07009687 .enforceCallingOrSelfReadPrivilegedPhoneStatePermissionOrCarrierPrivilege(
Pengquan Menga1bb6272018-09-06 09:59:22 -07009688 mApp, subId, "getCdmaRoamingMode");
9689
9690 final long identity = Binder.clearCallingIdentity();
9691 try {
9692 return (int) sendRequest(CMD_GET_CDMA_ROAMING_MODE, null /* argument */, subId);
9693 } finally {
9694 Binder.restoreCallingIdentity(identity);
9695 }
9696 }
9697
9698 @Override
9699 public boolean setCdmaRoamingMode(int subId, int mode) {
9700 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
9701 mApp, subId, "setCdmaRoamingMode");
9702
9703 final long identity = Binder.clearCallingIdentity();
9704 try {
9705 return (boolean) sendRequest(CMD_SET_CDMA_ROAMING_MODE, mode, subId);
9706 } finally {
9707 Binder.restoreCallingIdentity(identity);
9708 }
9709 }
9710
9711 @Override
Sarah Chinbaab1432020-10-28 13:46:24 -07009712 public int getCdmaSubscriptionMode(int subId) {
9713 TelephonyPermissions
Nathan Harold62c68512021-04-06 11:26:02 -07009714 .enforceCallingOrSelfReadPrivilegedPhoneStatePermissionOrCarrierPrivilege(
Sarah Chinbaab1432020-10-28 13:46:24 -07009715 mApp, subId, "getCdmaSubscriptionMode");
9716
9717 final long identity = Binder.clearCallingIdentity();
9718 try {
9719 return (int) sendRequest(CMD_GET_CDMA_SUBSCRIPTION_MODE, null /* argument */, subId);
9720 } finally {
9721 Binder.restoreCallingIdentity(identity);
9722 }
9723 }
9724
9725 @Override
Pengquan Menga1bb6272018-09-06 09:59:22 -07009726 public boolean setCdmaSubscriptionMode(int subId, int mode) {
9727 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
9728 mApp, subId, "setCdmaSubscriptionMode");
9729
9730 final long identity = Binder.clearCallingIdentity();
9731 try {
9732 return (boolean) sendRequest(CMD_SET_CDMA_SUBSCRIPTION_MODE, mode, subId);
9733 } finally {
9734 Binder.restoreCallingIdentity(identity);
9735 }
9736 }
Makoto Onukida3bf792018-09-18 16:06:29 -07009737
sqianc5eccab2018-10-19 18:46:41 -07009738 @Override
sqian8c685422019-02-22 15:55:18 -08009739 public Map<Integer, List<EmergencyNumber>> getEmergencyNumberList(
Philip P. Moltmann700a9592019-10-03 11:53:50 -07009740 String callingPackage, String callingFeatureId) {
sqian11b7a0e2018-12-05 18:48:28 -08009741 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
Philip P. Moltmann700a9592019-10-03 11:53:50 -07009742 mApp, getDefaultSubscription(), callingPackage, callingFeatureId,
9743 "getEmergencyNumberList")) {
sqian11b7a0e2018-12-05 18:48:28 -08009744 throw new SecurityException("Requires READ_PHONE_STATE permission.");
9745 }
9746 final long identity = Binder.clearCallingIdentity();
9747 try {
sqian854d44b2018-12-12 16:48:18 -08009748 Map<Integer, List<EmergencyNumber>> emergencyNumberListInternal = new HashMap<>();
9749 for (Phone phone: PhoneFactory.getPhones()) {
9750 if (phone.getEmergencyNumberTracker() != null
9751 && phone.getEmergencyNumberTracker().getEmergencyNumberList() != null) {
9752 emergencyNumberListInternal.put(
9753 phone.getSubId(),
9754 phone.getEmergencyNumberTracker().getEmergencyNumberList());
9755 }
sqian11b7a0e2018-12-05 18:48:28 -08009756 }
sqian854d44b2018-12-12 16:48:18 -08009757 return emergencyNumberListInternal;
sqian11b7a0e2018-12-05 18:48:28 -08009758 } finally {
9759 Binder.restoreCallingIdentity(identity);
9760 }
sqianc5eccab2018-10-19 18:46:41 -07009761 }
9762
9763 @Override
sqian8c685422019-02-22 15:55:18 -08009764 public boolean isEmergencyNumber(String number, boolean exactMatch) {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08009765 final Phone defaultPhone = getDefaultPhone();
sqian11b7a0e2018-12-05 18:48:28 -08009766 if (!exactMatch) {
9767 TelephonyPermissions
Nathan Harold62c68512021-04-06 11:26:02 -07009768 .enforceCallingOrSelfReadPrivilegedPhoneStatePermissionOrCarrierPrivilege(
sqian8c685422019-02-22 15:55:18 -08009769 mApp, defaultPhone.getSubId(), "isEmergencyNumber(Potential)");
sqian11b7a0e2018-12-05 18:48:28 -08009770 }
9771 final long identity = Binder.clearCallingIdentity();
9772 try {
sqian854d44b2018-12-12 16:48:18 -08009773 for (Phone phone: PhoneFactory.getPhones()) {
Chinmay Dhodapkard521bb12022-08-16 15:49:54 -07009774 //Note: we ignore passed in param exactMatch. We can remove it once
9775 // TelephonyManager#isPotentialEmergencyNumber is removed completely
sqian854d44b2018-12-12 16:48:18 -08009776 if (phone.getEmergencyNumberTracker() != null
Taesu Leee050c002020-10-13 17:19:35 +09009777 && phone.getEmergencyNumberTracker()
Chinmay Dhodapkard521bb12022-08-16 15:49:54 -07009778 .isEmergencyNumber(number)) {
Taesu Leee050c002020-10-13 17:19:35 +09009779 return true;
sqian11b7a0e2018-12-05 18:48:28 -08009780 }
sqian11b7a0e2018-12-05 18:48:28 -08009781 }
9782 return false;
9783 } finally {
9784 Binder.restoreCallingIdentity(identity);
9785 }
9786 }
9787
sqianf4ca7ed2019-01-15 18:32:07 -08009788 /**
Shuo Qianccbaf742021-02-22 18:32:21 -08009789 * Start emergency callback mode for GsmCdmaPhone for testing.
9790 */
9791 @Override
9792 public void startEmergencyCallbackMode() {
9793 TelephonyPermissions.enforceShellOnly(Binder.getCallingUid(),
9794 "startEmergencyCallbackMode");
9795 enforceModifyPermission();
9796 final long identity = Binder.clearCallingIdentity();
9797 try {
9798 for (Phone phone : PhoneFactory.getPhones()) {
9799 Rlog.d(LOG_TAG, "startEmergencyCallbackMode phone type: " + phone.getPhoneType());
9800 if (phone != null && ((phone.getPhoneType() == PHONE_TYPE_GSM)
9801 || (phone.getPhoneType() == PHONE_TYPE_CDMA))) {
9802 GsmCdmaPhone gsmCdmaPhone = (GsmCdmaPhone) phone;
9803 gsmCdmaPhone.obtainMessage(
9804 GsmCdmaPhone.EVENT_EMERGENCY_CALLBACK_MODE_ENTER).sendToTarget();
9805 Rlog.d(LOG_TAG, "startEmergencyCallbackMode: triggered");
9806 }
9807 }
9808 } finally {
9809 Binder.restoreCallingIdentity(identity);
9810 }
9811 }
9812
9813 /**
sqianf4ca7ed2019-01-15 18:32:07 -08009814 * Update emergency number list for test mode.
9815 */
9816 @Override
9817 public void updateEmergencyNumberListTestMode(int action, EmergencyNumber num) {
9818 TelephonyPermissions.enforceShellOnly(Binder.getCallingUid(),
9819 "updateEmergencyNumberListTestMode");
9820
9821 final long identity = Binder.clearCallingIdentity();
9822 try {
9823 for (Phone phone: PhoneFactory.getPhones()) {
9824 EmergencyNumberTracker tracker = phone.getEmergencyNumberTracker();
9825 if (tracker != null) {
9826 tracker.executeEmergencyNumberTestModeCommand(action, num);
9827 }
9828 }
9829 } finally {
9830 Binder.restoreCallingIdentity(identity);
9831 }
9832 }
9833
9834 /**
9835 * Get the full emergency number list for test mode.
9836 */
9837 @Override
9838 public List<String> getEmergencyNumberListTestMode() {
9839 TelephonyPermissions.enforceShellOnly(Binder.getCallingUid(),
9840 "getEmergencyNumberListTestMode");
9841
9842 final long identity = Binder.clearCallingIdentity();
9843 try {
9844 Set<String> emergencyNumbers = new HashSet<>();
9845 for (Phone phone: PhoneFactory.getPhones()) {
9846 EmergencyNumberTracker tracker = phone.getEmergencyNumberTracker();
9847 if (tracker != null) {
9848 for (EmergencyNumber num : tracker.getEmergencyNumberList()) {
9849 emergencyNumbers.add(num.getNumber());
9850 }
9851 }
9852 }
9853 return new ArrayList<>(emergencyNumbers);
9854 } finally {
9855 Binder.restoreCallingIdentity(identity);
9856 }
9857 }
9858
chen xud6b45bd2018-10-30 22:27:10 -07009859 @Override
Shuo Qian3b6ee772019-11-13 17:43:31 -08009860 public int getEmergencyNumberDbVersion(int subId) {
9861 enforceReadPrivilegedPermission("getEmergencyNumberDbVersion");
9862
9863 final long identity = Binder.clearCallingIdentity();
9864 try {
9865 final Phone phone = getPhone(subId);
9866 if (phone == null) {
9867 loge("getEmergencyNumberDbVersion fails with invalid subId: " + subId);
9868 return TelephonyManager.INVALID_EMERGENCY_NUMBER_DB_VERSION;
9869 }
9870 return phone.getEmergencyNumberDbVersion();
9871 } finally {
9872 Binder.restoreCallingIdentity(identity);
9873 }
9874 }
9875
9876 @Override
9877 public void notifyOtaEmergencyNumberDbInstalled() {
9878 enforceModifyPermission();
9879
9880 final long identity = Binder.clearCallingIdentity();
9881 try {
9882 for (Phone phone: PhoneFactory.getPhones()) {
9883 EmergencyNumberTracker tracker = phone.getEmergencyNumberTracker();
9884 if (tracker != null) {
9885 tracker.updateOtaEmergencyNumberDatabase();
9886 }
9887 }
9888 } finally {
9889 Binder.restoreCallingIdentity(identity);
9890 }
9891 }
9892
9893 @Override
Shuo Qianc373f112020-03-05 17:55:34 -08009894 public void updateOtaEmergencyNumberDbFilePath(ParcelFileDescriptor otaParcelFileDescriptor) {
Shuo Qian3b6ee772019-11-13 17:43:31 -08009895 enforceActiveEmergencySessionPermission();
9896
9897 final long identity = Binder.clearCallingIdentity();
9898 try {
9899 for (Phone phone: PhoneFactory.getPhones()) {
9900 EmergencyNumberTracker tracker = phone.getEmergencyNumberTracker();
9901 if (tracker != null) {
Shuo Qianc373f112020-03-05 17:55:34 -08009902 tracker.updateOtaEmergencyNumberDbFilePath(otaParcelFileDescriptor);
9903 }
9904 }
9905 } finally {
9906 Binder.restoreCallingIdentity(identity);
9907 }
9908 }
9909
9910 @Override
9911 public void resetOtaEmergencyNumberDbFilePath() {
9912 enforceActiveEmergencySessionPermission();
9913
9914 final long identity = Binder.clearCallingIdentity();
9915 try {
9916 for (Phone phone: PhoneFactory.getPhones()) {
9917 EmergencyNumberTracker tracker = phone.getEmergencyNumberTracker();
9918 if (tracker != null) {
9919 tracker.resetOtaEmergencyNumberDbFilePath();
Shuo Qian3b6ee772019-11-13 17:43:31 -08009920 }
9921 }
9922 } finally {
9923 Binder.restoreCallingIdentity(identity);
9924 }
9925 }
9926
9927 @Override
chen xud6b45bd2018-10-30 22:27:10 -07009928 public List<String> getCertsFromCarrierPrivilegeAccessRules(int subId) {
9929 enforceReadPrivilegedPermission("getCertsFromCarrierPrivilegeAccessRules");
9930 Phone phone = getPhone(subId);
9931 if (phone == null) {
9932 return null;
9933 }
9934 final long identity = Binder.clearCallingIdentity();
9935 try {
9936 UiccProfile profile = UiccController.getInstance()
9937 .getUiccProfileForPhone(phone.getPhoneId());
9938 if (profile != null) {
9939 return profile.getCertsFromCarrierPrivilegeAccessRules();
9940 }
9941 } finally {
9942 Binder.restoreCallingIdentity(identity);
9943 }
9944 return null;
9945 }
Malcolm Chen8e4ed912019-01-15 20:22:16 -08009946
9947 /**
9948 * Enable or disable a modem stack.
9949 */
9950 @Override
9951 public boolean enableModemForSlot(int slotIndex, boolean enable) {
9952 enforceModifyPermission();
9953
9954 final long identity = Binder.clearCallingIdentity();
9955 try {
9956 Phone phone = PhoneFactory.getPhone(slotIndex);
9957 if (phone == null) {
9958 return false;
9959 } else {
9960 return (Boolean) sendRequest(CMD_REQUEST_ENABLE_MODEM, enable, phone, null);
9961 }
9962 } finally {
9963 Binder.restoreCallingIdentity(identity);
9964 }
9965 }
Michelecea4cf22018-12-21 15:00:11 -08009966
Malcolm Chen4bcd9822019-03-27 18:34:05 -07009967 /**
9968 * Whether a modem stack is enabled or not.
9969 */
9970 @Override
Philip P. Moltmann700a9592019-10-03 11:53:50 -07009971 public boolean isModemEnabledForSlot(int slotIndex, String callingPackage,
9972 String callingFeatureId) {
Malcolm Chen4bcd9822019-03-27 18:34:05 -07009973 Phone phone = PhoneFactory.getPhone(slotIndex);
9974 if (phone == null) return false;
9975
9976 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
Philip P. Moltmann700a9592019-10-03 11:53:50 -07009977 mApp, phone.getSubId(), callingPackage, callingFeatureId,
9978 "isModemEnabledForSlot")) {
Malcolm Chen4bcd9822019-03-27 18:34:05 -07009979 throw new SecurityException("Requires READ_PHONE_STATE permission.");
9980 }
9981
9982 final long identity = Binder.clearCallingIdentity();
9983 try {
Nazanin Bakhshif71371d2019-04-29 17:29:44 -07009984 try {
9985 return mPhoneConfigurationManager.getPhoneStatusFromCache(phone.getPhoneId());
9986 } catch (NoSuchElementException ex) {
9987 return (Boolean) sendRequest(CMD_GET_MODEM_STATUS, null, phone, null);
9988 }
Malcolm Chen4bcd9822019-03-27 18:34:05 -07009989 } finally {
9990 Binder.restoreCallingIdentity(identity);
9991 }
9992 }
9993
Michelecea4cf22018-12-21 15:00:11 -08009994 @Override
Michele0ea7d782019-03-19 14:58:42 -07009995 public void setMultiSimCarrierRestriction(boolean isMultiSimCarrierRestricted) {
Michelecea4cf22018-12-21 15:00:11 -08009996 enforceModifyPermission();
9997
9998 final long identity = Binder.clearCallingIdentity();
9999 try {
10000 mTelephonySharedPreferences.edit()
Michele0ea7d782019-03-19 14:58:42 -070010001 .putBoolean(PREF_MULTI_SIM_RESTRICTED, isMultiSimCarrierRestricted)
Michelecea4cf22018-12-21 15:00:11 -080010002 .commit();
10003 } finally {
10004 Binder.restoreCallingIdentity(identity);
10005 }
10006 }
10007
10008 @Override
Michele0ea7d782019-03-19 14:58:42 -070010009 @TelephonyManager.IsMultiSimSupportedResult
Philip P. Moltmann700a9592019-10-03 11:53:50 -070010010 public int isMultiSimSupported(String callingPackage, String callingFeatureId) {
Michele4245e952019-02-04 11:36:23 -080010011 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(mApp,
Philip P. Moltmann700a9592019-10-03 11:53:50 -070010012 getDefaultPhone().getSubId(), callingPackage, callingFeatureId,
10013 "isMultiSimSupported")) {
Michele0ea7d782019-03-19 14:58:42 -070010014 return TelephonyManager.MULTISIM_NOT_SUPPORTED_BY_HARDWARE;
Michele4245e952019-02-04 11:36:23 -080010015 }
Michelecea4cf22018-12-21 15:00:11 -080010016
10017 final long identity = Binder.clearCallingIdentity();
10018 try {
Michele0ea7d782019-03-19 14:58:42 -070010019 return isMultiSimSupportedInternal();
Michelecea4cf22018-12-21 15:00:11 -080010020 } finally {
10021 Binder.restoreCallingIdentity(identity);
10022 }
10023 }
Nazanin Bakhshi628473f2019-01-29 17:37:52 -080010024
Michele0ea7d782019-03-19 14:58:42 -070010025 @TelephonyManager.IsMultiSimSupportedResult
10026 private int isMultiSimSupportedInternal() {
Michele30b57b22019-03-01 12:01:14 -080010027 // If the device has less than 2 SIM cards, indicate that multisim is restricted.
10028 int numPhysicalSlots = UiccController.getInstance().getUiccSlots().length;
10029 if (numPhysicalSlots < 2) {
Michele0ea7d782019-03-19 14:58:42 -070010030 loge("isMultiSimSupportedInternal: requires at least 2 cards");
10031 return TelephonyManager.MULTISIM_NOT_SUPPORTED_BY_HARDWARE;
Michele30b57b22019-03-01 12:01:14 -080010032 }
10033 // Check if the hardware supports multisim functionality. If usage of multisim is not
10034 // supported by the modem, indicate that it is restricted.
10035 PhoneCapability staticCapability =
10036 mPhoneConfigurationManager.getStaticPhoneCapability();
10037 if (staticCapability == null) {
Michele0ea7d782019-03-19 14:58:42 -070010038 loge("isMultiSimSupportedInternal: no static configuration available");
10039 return TelephonyManager.MULTISIM_NOT_SUPPORTED_BY_HARDWARE;
Michele30b57b22019-03-01 12:01:14 -080010040 }
SongFerngWang8236caa2021-01-17 21:51:44 +080010041 if (staticCapability.getLogicalModemList().size() < 2) {
Michele0ea7d782019-03-19 14:58:42 -070010042 loge("isMultiSimSupportedInternal: maximum number of modem is < 2");
10043 return TelephonyManager.MULTISIM_NOT_SUPPORTED_BY_HARDWARE;
Michele30b57b22019-03-01 12:01:14 -080010044 }
10045 // Check if support of multiple SIMs is restricted by carrier
10046 if (mTelephonySharedPreferences.getBoolean(PREF_MULTI_SIM_RESTRICTED, false)) {
Michele0ea7d782019-03-19 14:58:42 -070010047 return TelephonyManager.MULTISIM_NOT_SUPPORTED_BY_CARRIER;
Michele30b57b22019-03-01 12:01:14 -080010048 }
10049
Michele0ea7d782019-03-19 14:58:42 -070010050 return TelephonyManager.MULTISIM_ALLOWED;
Michele30b57b22019-03-01 12:01:14 -080010051 }
10052
Nazanin Bakhshi628473f2019-01-29 17:37:52 -080010053 /**
10054 * Switch configs to enable multi-sim or switch back to single-sim
Nazanin Bakhshi17318782019-03-01 11:56:08 -080010055 * Note: Switch from multi-sim to single-sim is only possible with MODIFY_PHONE_STATE
10056 * permission, but the other way around is possible with either MODIFY_PHONE_STATE
10057 * or carrier privileges
Nazanin Bakhshi628473f2019-01-29 17:37:52 -080010058 * @param numOfSims number of active sims we want to switch to
10059 */
10060 @Override
10061 public void switchMultiSimConfig(int numOfSims) {
Nazanin Bakhshi17318782019-03-01 11:56:08 -080010062 if (numOfSims == 1) {
10063 enforceModifyPermission();
10064 } else {
10065 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
10066 mApp, SubscriptionManager.DEFAULT_SUBSCRIPTION_ID, "switchMultiSimConfig");
10067 }
Nazanin Bakhshi628473f2019-01-29 17:37:52 -080010068 final long identity = Binder.clearCallingIdentity();
Michele30b57b22019-03-01 12:01:14 -080010069
Nazanin Bakhshi628473f2019-01-29 17:37:52 -080010070 try {
Michele30b57b22019-03-01 12:01:14 -080010071 //only proceed if multi-sim is not restricted
Michele0ea7d782019-03-19 14:58:42 -070010072 if (isMultiSimSupportedInternal() != TelephonyManager.MULTISIM_ALLOWED) {
Michele30b57b22019-03-01 12:01:14 -080010073 loge("switchMultiSimConfig not possible. It is restricted or not supported.");
10074 return;
10075 }
Nazanin Bakhshi628473f2019-01-29 17:37:52 -080010076 mPhoneConfigurationManager.switchMultiSimConfig(numOfSims);
10077 } finally {
10078 Binder.restoreCallingIdentity(identity);
10079 }
10080 }
10081
Hyungjun Parkbb07fde2019-01-10 15:28:51 +090010082 @Override
10083 public boolean isApplicationOnUicc(int subId, int appType) {
10084 enforceReadPrivilegedPermission("isApplicationOnUicc");
10085 Phone phone = getPhone(subId);
10086 if (phone == null) {
10087 return false;
10088 }
10089 final long identity = Binder.clearCallingIdentity();
10090 try {
Muralidhar Reddy472c2ae2021-09-29 19:38:40 +000010091 UiccPort uiccPort = phone.getUiccPort();
10092 if (uiccPort == null) {
Hyungjun Parkbb07fde2019-01-10 15:28:51 +090010093 return false;
10094 }
Muralidhar Reddy472c2ae2021-09-29 19:38:40 +000010095 UiccProfile uiccProfile = uiccPort.getUiccProfile();
Hyungjun Parkbb07fde2019-01-10 15:28:51 +090010096 if (uiccProfile == null) {
10097 return false;
10098 }
10099 if (TelephonyManager.APPTYPE_SIM <= appType
10100 && appType <= TelephonyManager.APPTYPE_ISIM) {
10101 return uiccProfile.isApplicationOnIcc(AppType.values()[appType]);
10102 }
10103 return false;
10104 } finally {
10105 Binder.restoreCallingIdentity(identity);
10106 }
10107 }
10108
Nazanin Bakhshi628473f2019-01-29 17:37:52 -080010109 /**
chen xub4baa772019-04-03 10:23:41 -070010110 * Get whether making changes to modem configurations will trigger reboot.
10111 * Return value defaults to true.
Nazanin Bakhshi5fe5ef22019-01-30 10:52:09 -080010112 */
10113 @Override
Philip P. Moltmann700a9592019-10-03 11:53:50 -070010114 public boolean doesSwitchMultiSimConfigTriggerReboot(int subId, String callingPackage,
10115 String callingFeatureId) {
chen xub4baa772019-04-03 10:23:41 -070010116 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
Philip P. Moltmann700a9592019-10-03 11:53:50 -070010117 mApp, subId, callingPackage, callingFeatureId,
10118 "doesSwitchMultiSimConfigTriggerReboot")) {
chen xub4baa772019-04-03 10:23:41 -070010119 return false;
10120 }
Nazanin Bakhshi5fe5ef22019-01-30 10:52:09 -080010121 final long identity = Binder.clearCallingIdentity();
10122 try {
10123 return mPhoneConfigurationManager.isRebootRequiredForModemConfigChange();
10124 } finally {
10125 Binder.restoreCallingIdentity(identity);
10126 }
10127 }
10128
Nathan Harold29f5f052019-02-15 13:41:57 -080010129 private void updateModemStateMetrics() {
10130 TelephonyMetrics metrics = TelephonyMetrics.getInstance();
10131 // TODO: check the state for each modem if the api is ready.
10132 metrics.updateEnabledModemBitmap((1 << TelephonyManager.from(mApp).getPhoneCount()) - 1);
10133 }
10134
Pengquan Meng3889a572019-01-23 11:16:29 -080010135 @Override
sandeepjsa208e3b2021-11-17 04:05:58 +000010136 public List<UiccSlotMapping> getSlotsMapping(String callingPackage) {
Pengquan Meng3889a572019-01-23 11:16:29 -080010137 enforceReadPrivilegedPermission("getSlotsMapping");
sandeepjsa208e3b2021-11-17 04:05:58 +000010138 // Verify that the callingPackage belongs to the calling UID
10139 mApp.getSystemService(AppOpsManager.class)
10140 .checkPackage(Binder.getCallingUid(), callingPackage);
Pengquan Meng3889a572019-01-23 11:16:29 -080010141 final long identity = Binder.clearCallingIdentity();
sandeepjsa208e3b2021-11-17 04:05:58 +000010142 List<UiccSlotMapping> slotMap = new ArrayList<>();
Pengquan Meng3889a572019-01-23 11:16:29 -080010143 try {
sandeepjsa208e3b2021-11-17 04:05:58 +000010144 UiccSlotInfo[] slotInfos = getUiccSlotsInfo(mApp.getOpPackageName());
10145 if (slotInfos != null) {
10146 for (int i = 0; i < slotInfos.length; i++) {
10147 for (UiccPortInfo portInfo : slotInfos[i].getPorts()) {
10148 if (SubscriptionManager.isValidPhoneId(portInfo.getLogicalSlotIndex())) {
10149 slotMap.add(new UiccSlotMapping(portInfo.getPortIndex(), i,
10150 portInfo.getLogicalSlotIndex()));
10151 }
10152 }
Pengquan Meng3889a572019-01-23 11:16:29 -080010153 }
10154 }
sandeepjsa208e3b2021-11-17 04:05:58 +000010155 return slotMap;
Pengquan Meng3889a572019-01-23 11:16:29 -080010156 } finally {
10157 Binder.restoreCallingIdentity(identity);
10158 }
10159 }
Nathan Harold48d6fd52019-02-06 19:01:40 -080010160
10161 /**
10162 * Get the IRadio HAL Version
jimsunf9ec1622022-09-13 21:18:43 +080010163 * @deprecated use getHalVersion instead
Nathan Harold48d6fd52019-02-06 19:01:40 -080010164 */
jimsunf9ec1622022-09-13 21:18:43 +080010165 @Deprecated
Nathan Harold48d6fd52019-02-06 19:01:40 -080010166 @Override
10167 public int getRadioHalVersion() {
jimsunf9ec1622022-09-13 21:18:43 +080010168 return getHalVersion(HAL_SERVICE_RADIO);
10169 }
10170
10171 /**
10172 * Get the HAL Version of a specific service
10173 */
10174 @Override
10175 public int getHalVersion(int service) {
Nathan Harold48d6fd52019-02-06 19:01:40 -080010176 Phone phone = getDefaultPhone();
10177 if (phone == null) return -1;
jimsunf9ec1622022-09-13 21:18:43 +080010178 HalVersion hv = phone.getHalVersion(service);
Nathan Harold48d6fd52019-02-06 19:01:40 -080010179 if (hv.equals(HalVersion.UNKNOWN)) return -1;
10180 return hv.major * 100 + hv.minor;
10181 }
Malcolm Chendc8c10e2019-04-10 18:25:07 -070010182
10183 /**
Shuo Qianda2d6ec2020-01-14 15:18:28 -080010184 * Get the current calling package name.
10185 * @return the current calling package name
10186 */
10187 @Override
10188 public String getCurrentPackageName() {
10189 return mApp.getPackageManager().getPackagesForUid(Binder.getCallingUid())[0];
10190 }
10191
10192 /**
Malcolm Chene5ad5792019-04-18 13:51:02 -070010193 * Return whether data is enabled for certain APN type. This will tell if framework will accept
10194 * corresponding network requests on a subId.
10195 *
10196 * Data is enabled if:
Malcolm Chendc8c10e2019-04-10 18:25:07 -070010197 * 1) user data is turned on, or
Malcolm Chene5ad5792019-04-18 13:51:02 -070010198 * 2) APN is un-metered for this subscription, or
10199 * 3) APN type is whitelisted. E.g. MMS is whitelisted if
Hall Liu746e03c2020-09-25 11:13:49 -070010200 * {@link TelephonyManager#MOBILE_DATA_POLICY_MMS_ALWAYS_ALLOWED} is enabled.
Malcolm Chene5ad5792019-04-18 13:51:02 -070010201 *
10202 * @return whether data is allowed for a apn type.
10203 *
10204 * @hide
Malcolm Chendc8c10e2019-04-10 18:25:07 -070010205 */
10206 @Override
Malcolm Chene5ad5792019-04-18 13:51:02 -070010207 public boolean isDataEnabledForApn(int apnType, int subId, String callingPackage) {
Amit Mahajan5d4e1922019-10-07 16:20:43 -070010208 enforceReadPrivilegedPermission("Needs READ_PRIVILEGED_PHONE_STATE for "
10209 + "isDataEnabledForApn");
Malcolm Chendc8c10e2019-04-10 18:25:07 -070010210
10211 // Now that all security checks passes, perform the operation as ourselves.
10212 final long identity = Binder.clearCallingIdentity();
10213 try {
10214 Phone phone = getPhone(subId);
10215 if (phone == null) return false;
10216
Jack Yu27422a52022-03-21 10:38:05 -070010217 boolean isMetered;
Jack Yu99e87332021-12-17 23:14:15 -080010218 boolean isDataEnabled;
Jack Yu7968c6d2022-07-31 00:43:21 -070010219 isMetered = phone.getDataNetworkController().getDataConfigManager()
10220 .isMeteredCapability(DataUtils.apnTypeToNetworkCapability(apnType),
10221 phone.getServiceState().getDataRoaming());
10222 isDataEnabled = phone.getDataSettingsManager().isDataEnabled(apnType);
Jack Yu99e87332021-12-17 23:14:15 -080010223 return !isMetered || isDataEnabled;
Malcolm Chene5ad5792019-04-18 13:51:02 -070010224 } finally {
10225 Binder.restoreCallingIdentity(identity);
10226 }
10227 }
10228
10229 @Override
Jack Yu41407ee2019-05-13 16:54:09 -070010230 public boolean isApnMetered(@ApnType int apnType, int subId) {
Malcolm Chene5ad5792019-04-18 13:51:02 -070010231 enforceReadPrivilegedPermission("isApnMetered");
10232
10233 // Now that all security checks passes, perform the operation as ourselves.
10234 final long identity = Binder.clearCallingIdentity();
10235 try {
10236 Phone phone = getPhone(subId);
10237 if (phone == null) return true; // By default return true.
Jack Yu7968c6d2022-07-31 00:43:21 -070010238 return phone.getDataNetworkController().getDataConfigManager().isMeteredCapability(
10239 DataUtils.apnTypeToNetworkCapability(apnType),
10240 phone.getServiceState().getDataRoaming());
Malcolm Chendc8c10e2019-04-10 18:25:07 -070010241 } finally {
10242 Binder.restoreCallingIdentity(identity);
10243 }
10244 }
Brad Ebingera63db5f2019-04-23 16:31:13 -070010245
10246 @Override
Hall Liu73f5d362020-01-20 13:42:00 -080010247 public void setSystemSelectionChannels(List<RadioAccessSpecifier> specifiers,
10248 int subscriptionId, IBooleanConsumer resultCallback) {
10249 enforceModifyPermission();
10250 long token = Binder.clearCallingIdentity();
10251 try {
10252 Phone phone = getPhone(subscriptionId);
10253 if (phone == null) {
10254 try {
10255 if (resultCallback != null) {
10256 resultCallback.accept(false);
10257 }
10258 } catch (RemoteException e) {
10259 // ignore
10260 }
10261 return;
10262 }
10263 Pair<List<RadioAccessSpecifier>, Consumer<Boolean>> argument =
10264 Pair.create(specifiers, (x) -> {
10265 try {
10266 if (resultCallback != null) {
10267 resultCallback.accept(x);
10268 }
10269 } catch (RemoteException e) {
10270 // ignore
10271 }
10272 });
10273 sendRequestAsync(CMD_SET_SYSTEM_SELECTION_CHANNELS, argument, phone, null);
10274 } finally {
10275 Binder.restoreCallingIdentity(token);
10276 }
10277 }
10278
10279 @Override
Sarah Chin679c08a2020-11-18 13:39:35 -080010280 public List<RadioAccessSpecifier> getSystemSelectionChannels(int subId) {
10281 TelephonyPermissions
Nathan Harold62c68512021-04-06 11:26:02 -070010282 .enforceCallingOrSelfReadPrivilegedPhoneStatePermissionOrCarrierPrivilege(
Sarah Chin679c08a2020-11-18 13:39:35 -080010283 mApp, subId, "getSystemSelectionChannels");
10284 WorkSource workSource = getWorkSource(Binder.getCallingUid());
10285 final long identity = Binder.clearCallingIdentity();
10286 try {
Sarah Chin428d1d62021-03-13 03:17:40 -080010287 Object result = sendRequest(CMD_GET_SYSTEM_SELECTION_CHANNELS, null, subId, workSource);
10288 if (result instanceof IllegalStateException) {
10289 throw (IllegalStateException) result;
10290 }
10291 List<RadioAccessSpecifier> specifiers = (List<RadioAccessSpecifier>) result;
Sarah Chin679c08a2020-11-18 13:39:35 -080010292 if (DBG) log("getSystemSelectionChannels: " + specifiers);
10293 return specifiers;
10294 } finally {
10295 Binder.restoreCallingIdentity(identity);
10296 }
10297 }
10298
10299 @Override
Jack Yu8b766fc2022-03-21 09:42:33 -070010300 public boolean isMvnoMatched(int slotIndex, int mvnoType, @NonNull String mvnoMatchData) {
changbettyca3d40d2020-03-03 16:27:31 +080010301 enforceReadPrivilegedPermission("isMvnoMatched");
Jack Yu8b766fc2022-03-21 09:42:33 -070010302 return UiccController.getInstance().mvnoMatches(slotIndex, mvnoType, mvnoMatchData);
changbetty7157e9e2019-12-06 18:16:37 +080010303 }
10304
10305 @Override
Philip P. Moltmannd02b7372020-03-18 17:06:12 -070010306 public void enqueueSmsPickResult(String callingPackage, String callingAttributionTag,
10307 IIntegerConsumer pendingSubIdResult) {
Shuo Qianda2d6ec2020-01-14 15:18:28 -080010308 if (callingPackage == null) {
10309 callingPackage = getCurrentPackageName();
10310 }
Brad Ebingera63db5f2019-04-23 16:31:13 -070010311 SmsPermissions permissions = new SmsPermissions(getDefaultPhone(), mApp,
10312 (AppOpsManager) mApp.getSystemService(Context.APP_OPS_SERVICE));
Philip P. Moltmannd02b7372020-03-18 17:06:12 -070010313 if (!permissions.checkCallingCanSendSms(callingPackage, callingAttributionTag,
10314 "Sending message")) {
Brad Ebingera63db5f2019-04-23 16:31:13 -070010315 throw new SecurityException("Requires SEND_SMS permission to perform this operation");
10316 }
10317 PickSmsSubscriptionActivity.addPendingResult(pendingSubIdResult);
10318 Intent intent = new Intent();
10319 intent.setClass(mApp, PickSmsSubscriptionActivity.class);
10320 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
10321 // Bring up choose default SMS subscription dialog right now
10322 intent.putExtra(PickSmsSubscriptionActivity.DIALOG_TYPE_KEY,
10323 PickSmsSubscriptionActivity.SMS_PICK_FOR_MESSAGE);
10324 mApp.startActivity(intent);
10325 }
chen xud5ca2d52019-05-28 15:20:57 -070010326
10327 @Override
Ayush Sharma787854b2022-12-12 14:55:02 +000010328 public void showSwitchToManagedProfileDialog() {
10329 enforceModifyPermission();
10330
10331 Intent intent = new Intent();
10332 intent.setClass(mApp, ErrorDialogActivity.class);
10333 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
10334 mApp.startActivity(intent);
10335 }
10336
10337 @Override
chen xud5ca2d52019-05-28 15:20:57 -070010338 public String getMmsUAProfUrl(int subId) {
10339 //TODO investigate if this API should require proper permission check in R b/133791609
10340 final long identity = Binder.clearCallingIdentity();
10341 try {
Guoqiang.Qiu171383d2020-07-13 09:38:32 +080010342 String carrierUAProfUrl = mApp.getCarrierConfigForSubId(subId).getString(
10343 CarrierConfigManager.KEY_MMS_UA_PROF_URL_STRING);
10344 if (!TextUtils.isEmpty(carrierUAProfUrl)) {
10345 return carrierUAProfUrl;
10346 }
Daniel Brightebb4eb72020-02-18 15:16:33 -080010347 return SubscriptionManager.getResourcesForSubId(getDefaultPhone().getContext(), subId)
10348 .getString(com.android.internal.R.string.config_mms_user_agent_profile_url);
chen xud5ca2d52019-05-28 15:20:57 -070010349 } finally {
10350 Binder.restoreCallingIdentity(identity);
10351 }
10352 }
10353
10354 @Override
10355 public String getMmsUserAgent(int subId) {
10356 //TODO investigate if this API should require proper permission check in R b/133791609
10357 final long identity = Binder.clearCallingIdentity();
10358 try {
Guoqiang.Qiu171383d2020-07-13 09:38:32 +080010359 String carrierUserAgent = mApp.getCarrierConfigForSubId(subId).getString(
10360 CarrierConfigManager.KEY_MMS_USER_AGENT_STRING);
10361 if (!TextUtils.isEmpty(carrierUserAgent)) {
10362 return carrierUserAgent;
10363 }
Daniel Brightebb4eb72020-02-18 15:16:33 -080010364 return SubscriptionManager.getResourcesForSubId(getDefaultPhone().getContext(), subId)
10365 .getString(com.android.internal.R.string.config_mms_user_agent);
chen xud5ca2d52019-05-28 15:20:57 -070010366 } finally {
10367 Binder.restoreCallingIdentity(identity);
10368 }
10369 }
Jack Yub07d4972019-05-28 16:12:25 -070010370
10371 @Override
Hall Liua62f5da2020-09-25 10:42:19 -070010372 public boolean isMobileDataPolicyEnabled(int subscriptionId, int policy) {
10373 enforceReadPrivilegedPermission("isMobileDataPolicyEnabled");
Jack Yub07d4972019-05-28 16:12:25 -070010374
Jack Yub07d4972019-05-28 16:12:25 -070010375 final long identity = Binder.clearCallingIdentity();
10376 try {
Hall Liua62f5da2020-09-25 10:42:19 -070010377 Phone phone = getPhone(subscriptionId);
Jack Yub07d4972019-05-28 16:12:25 -070010378 if (phone == null) return false;
10379
Ling Maf188d502022-09-16 15:22:36 -070010380 return phone.getDataSettingsManager().isMobileDataPolicyEnabled(policy);
Jack Yub07d4972019-05-28 16:12:25 -070010381 } finally {
10382 Binder.restoreCallingIdentity(identity);
10383 }
10384 }
10385
10386 @Override
Hall Liuc66bb112021-02-02 12:09:32 -080010387 public void setMobileDataPolicyEnabled(int subscriptionId, int policy,
Hall Liua62f5da2020-09-25 10:42:19 -070010388 boolean enabled) {
changbettyd5c246e2019-12-24 15:40:37 +080010389 enforceModifyPermission();
10390
changbettyd5c246e2019-12-24 15:40:37 +080010391 final long identity = Binder.clearCallingIdentity();
10392 try {
Hall Liua62f5da2020-09-25 10:42:19 -070010393 Phone phone = getPhone(subscriptionId);
10394 if (phone == null) return;
changbettyd5c246e2019-12-24 15:40:37 +080010395
Ling Maf188d502022-09-16 15:22:36 -070010396 phone.getDataSettingsManager().setMobileDataPolicy(policy, enabled);
changbettyd5c246e2019-12-24 15:40:37 +080010397 } finally {
10398 Binder.restoreCallingIdentity(identity);
10399 }
10400 }
10401
Tyler Gunn7bcdc742019-10-04 15:56:59 -070010402 /**
Hall Liu746e03c2020-09-25 11:13:49 -070010403 * Updates whether conference event package handling is enabled.
Tyler Gunn7bcdc742019-10-04 15:56:59 -070010404 * @param isCepEnabled {@code true} if CEP handling is enabled (default), or {@code false}
10405 * otherwise.
10406 */
10407 @Override
10408 public void setCepEnabled(boolean isCepEnabled) {
10409 TelephonyPermissions.enforceShellOnly(Binder.getCallingUid(), "setCepEnabled");
10410
10411 final long identity = Binder.clearCallingIdentity();
10412 try {
10413 Rlog.i(LOG_TAG, "setCepEnabled isCepEnabled=" + isCepEnabled);
10414 for (Phone phone : PhoneFactory.getPhones()) {
10415 Phone defaultPhone = phone.getImsPhone();
10416 if (defaultPhone != null && defaultPhone.getPhoneType() == PHONE_TYPE_IMS) {
10417 ImsPhone imsPhone = (ImsPhone) defaultPhone;
10418 ImsPhoneCallTracker imsPhoneCallTracker =
10419 (ImsPhoneCallTracker) imsPhone.getCallTracker();
10420 imsPhoneCallTracker.setConferenceEventPackageEnabled(isCepEnabled);
10421 Rlog.i(LOG_TAG, "setCepEnabled isCepEnabled=" + isCepEnabled + ", for imsPhone "
10422 + imsPhone.getMsisdn());
10423 }
10424 }
10425 } finally {
10426 Binder.restoreCallingIdentity(identity);
10427 }
10428 }
allenwtsu46dcc572020-01-08 18:24:03 +080010429
10430 /**
10431 * Notify that an RCS autoconfiguration XML file has been received for provisioning.
10432 *
10433 * @param config The XML file to be read. ASCII/UTF8 encoded text if not compressed.
10434 * @param isCompressed The XML file is compressed in gzip format and must be decompressed
10435 * before being read.
10436 */
10437 @Override
10438 public void notifyRcsAutoConfigurationReceived(int subId, @NonNull byte[] config, boolean
10439 isCompressed) {
10440 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
10441 mApp, subId, "notifyRcsAutoConfigurationReceived");
Hui Wang761a6682020-10-31 05:12:53 +000010442 if (!SubscriptionManager.isValidSubscriptionId(subId)) {
10443 throw new IllegalArgumentException("Invalid Subscription ID: " + subId);
10444 }
10445 if (!isImsAvailableOnDevice()) {
joonhunshin46b49a32022-12-21 05:33:23 +000010446 // ProvisioningManager can not handle ServiceSpecificException.
10447 // Throw the IllegalStateException and annotate ProvisioningManager.
10448 throw new IllegalStateException("IMS not available on device.");
Hui Wang761a6682020-10-31 05:12:53 +000010449 }
10450
10451 final long identity = Binder.clearCallingIdentity();
allenwtsu46dcc572020-01-08 18:24:03 +080010452 try {
Hui Wang761a6682020-10-31 05:12:53 +000010453 RcsProvisioningMonitor.getInstance().updateConfig(subId, config, isCompressed);
10454 } finally {
10455 Binder.restoreCallingIdentity(identity);
allenwtsu46dcc572020-01-08 18:24:03 +080010456 }
10457 }
zoey chene02881a2019-12-30 16:11:23 +080010458
10459 @Override
10460 public boolean isIccLockEnabled(int subId) {
10461 enforceReadPrivilegedPermission("isIccLockEnabled");
10462
10463 // Now that all security checks passes, perform the operation as ourselves.
10464 final long identity = Binder.clearCallingIdentity();
10465 try {
10466 Phone phone = getPhone(subId);
10467 if (phone != null && phone.getIccCard() != null) {
10468 return phone.getIccCard().getIccLockEnabled();
10469 } else {
10470 return false;
10471 }
10472 } finally {
10473 Binder.restoreCallingIdentity(identity);
10474 }
10475 }
10476
10477 /**
10478 * Set the ICC pin lock enabled or disabled.
10479 *
10480 * @return an integer representing the status of IccLock enabled or disabled in the following
10481 * three cases:
10482 * - {@link TelephonyManager#CHANGE_ICC_LOCK_SUCCESS} if enabled or disabled IccLock
10483 * successfully.
10484 * - Positive number and zero for remaining password attempts.
10485 * - Negative number for other failure cases (such like enabling/disabling PIN failed).
10486 *
10487 */
10488 @Override
10489 public int setIccLockEnabled(int subId, boolean enabled, String password) {
10490 enforceModifyPermission();
10491
10492 Phone phone = getPhone(subId);
10493 if (phone == null) {
10494 return 0;
10495 }
10496 // Now that all security checks passes, perform the operation as ourselves.
10497 final long identity = Binder.clearCallingIdentity();
10498 try {
10499 int attemptsRemaining = (int) sendRequest(CMD_SET_ICC_LOCK_ENABLED,
10500 new Pair<Boolean, String>(enabled, password), phone, null);
10501 return attemptsRemaining;
10502
10503 } catch (Exception e) {
10504 Log.e(LOG_TAG, "setIccLockEnabled. Exception e =" + e);
10505 } finally {
10506 Binder.restoreCallingIdentity(identity);
10507 }
10508 return 0;
10509 }
10510
10511 /**
10512 * Change the ICC password used in ICC pin lock.
10513 *
10514 * @return an integer representing the status of IccLock changed in the following three cases:
10515 * - {@link TelephonyManager#CHANGE_ICC_LOCK_SUCCESS} if changed IccLock successfully.
10516 * - Positive number and zero for remaining password attempts.
10517 * - Negative number for other failure cases (such like enabling/disabling PIN failed).
10518 *
10519 */
10520 @Override
10521 public int changeIccLockPassword(int subId, String oldPassword, String newPassword) {
10522 enforceModifyPermission();
10523
10524 Phone phone = getPhone(subId);
10525 if (phone == null) {
10526 return 0;
10527 }
10528 // Now that all security checks passes, perform the operation as ourselves.
10529 final long identity = Binder.clearCallingIdentity();
10530 try {
10531 int attemptsRemaining = (int) sendRequest(CMD_CHANGE_ICC_LOCK_PASSWORD,
10532 new Pair<String, String>(oldPassword, newPassword), phone, null);
10533 return attemptsRemaining;
10534
10535 } catch (Exception e) {
10536 Log.e(LOG_TAG, "changeIccLockPassword. Exception e =" + e);
10537 } finally {
10538 Binder.restoreCallingIdentity(identity);
10539 }
10540 return 0;
10541 }
Peter Wangdafb9ac2020-01-15 14:13:38 -080010542
10543 /**
10544 * Request for receiving user activity notification
10545 */
10546 @Override
10547 public void requestUserActivityNotification() {
10548 if (!mNotifyUserActivity.get()
10549 && !mMainThreadHandler.hasMessages(MSG_NOTIFY_USER_ACTIVITY)) {
10550 mNotifyUserActivity.set(true);
10551 }
10552 }
10553
10554 /**
10555 * Called when userActivity is signalled in the power manager.
10556 * This is safe to call from any thread, with any window manager locks held or not.
10557 */
10558 @Override
10559 public void userActivity() {
10560 // ***************************************
10561 // * Inherited from PhoneWindowManager *
10562 // ***************************************
10563 // THIS IS CALLED FROM DEEP IN THE POWER MANAGER
10564 // WITH ITS LOCKS HELD.
10565 //
10566 // This code must be VERY careful about the locks
10567 // it acquires.
10568 // In fact, the current code acquires way too many,
10569 // and probably has lurking deadlocks.
10570
10571 if (Binder.getCallingUid() != Process.SYSTEM_UID) {
10572 throw new SecurityException("Only the OS may call notifyUserActivity()");
10573 }
10574
10575 if (mNotifyUserActivity.getAndSet(false)) {
10576 mMainThreadHandler.sendEmptyMessageDelayed(MSG_NOTIFY_USER_ACTIVITY,
10577 USER_ACTIVITY_NOTIFICATION_DELAY);
10578 }
10579 }
Malcolm Chen4639c562020-04-13 11:59:40 -070010580
10581 @Override
10582 public boolean canConnectTo5GInDsdsMode() {
10583 return mApp.getResources().getBoolean(R.bool.config_5g_connection_in_dsds_mode);
10584 }
Jack Yud10cdd42020-09-28 20:28:01 -070010585
10586 @Override
10587 public @NonNull List<String> getEquivalentHomePlmns(int subId, String callingPackage,
10588 String callingFeatureId) {
10589 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
10590 mApp, subId, callingPackage, callingFeatureId, "getEquivalentHomePlmns")) {
10591 throw new SecurityException("Requires READ_PHONE_STATE permission.");
10592 }
10593
10594 Phone phone = getPhone(subId);
10595 if (phone == null) {
10596 throw new RuntimeException("phone is not available");
10597 }
10598 // Now that all security checks passes, perform the operation as ourselves.
10599 final long identity = Binder.clearCallingIdentity();
10600 try {
10601 return phone.getEquivalentHomePlmns();
10602 } finally {
10603 Binder.restoreCallingIdentity(identity);
10604 }
10605 }
Daniel Bright59e67312020-11-13 11:49:37 -080010606
10607 @Override
10608 public boolean isRadioInterfaceCapabilitySupported(
Daniel Bright95a4c1f2021-02-11 09:57:16 -080010609 final @NonNull @TelephonyManager.RadioInterfaceCapability String capability) {
10610 Set<String> radioInterfaceCapabilities =
Daniel Bright94f43662021-03-01 14:43:40 -080010611 mRadioInterfaceCapabilities.getCapabilities();
Daniel Bright59e67312020-11-13 11:49:37 -080010612 if (radioInterfaceCapabilities == null) {
10613 throw new RuntimeException("radio interface capabilities are not available");
Daniel Bright59e67312020-11-13 11:49:37 -080010614 }
Daniel Bright95a4c1f2021-02-11 09:57:16 -080010615 return radioInterfaceCapabilities.contains(capability);
Daniel Bright59e67312020-11-13 11:49:37 -080010616 }
Jack Nudelmanb0b87642020-11-12 15:04:39 -080010617
Hui Wang641e81c2020-10-12 12:14:23 -070010618 @Override
10619 public void bootstrapAuthenticationRequest(int subId, int appType, Uri nafUrl,
10620 UaSecurityProtocolIdentifier securityProtocol,
Brad Ebinger34c09a52021-02-17 23:23:21 +000010621 boolean forceBootStrapping, IBootstrapAuthenticationCallback callback) {
10622 TelephonyPermissions.enforceAnyPermissionGrantedOrCarrierPrivileges(mApp, subId,
10623 Binder.getCallingUid(), "bootstrapAuthenticationRequest",
10624 Manifest.permission.PERFORM_IMS_SINGLE_REGISTRATION,
10625 Manifest.permission.MODIFY_PHONE_STATE);
Hui Wang641e81c2020-10-12 12:14:23 -070010626 if (DBG) {
10627 log("bootstrapAuthenticationRequest, subId:" + subId + ", appType:"
10628 + appType + ", NAF:" + nafUrl + ", sp:" + securityProtocol
10629 + ", forceBootStrapping:" + forceBootStrapping + ", callback:" + callback);
10630 }
10631
10632 if (!SubscriptionManager.isValidSubscriptionId(subId)
10633 || appType < TelephonyManager.APPTYPE_UNKNOWN
10634 || appType > TelephonyManager.APPTYPE_ISIM
10635 || nafUrl == null || securityProtocol == null || callback == null) {
10636 Log.d(LOG_TAG, "bootstrapAuthenticationRequest failed due to invalid parameters");
10637 if (callback != null) {
10638 try {
10639 callback.onAuthenticationFailure(
10640 0, TelephonyManager.GBA_FAILURE_REASON_FEATURE_NOT_SUPPORTED);
10641 } catch (RemoteException exception) {
10642 log("Fail to notify onAuthenticationFailure due to " + exception);
10643 }
10644 return;
10645 }
10646 }
10647
10648 final long token = Binder.clearCallingIdentity();
10649 try {
10650 getGbaManager(subId).bootstrapAuthenticationRequest(
10651 new GbaAuthRequest(subId, appType, nafUrl, securityProtocol.toByteArray(),
10652 forceBootStrapping, callback));
10653 } finally {
10654 Binder.restoreCallingIdentity(token);
10655 }
10656 }
10657
Jack Nudelmanb0b87642020-11-12 15:04:39 -080010658 /**
Sooraj Sasindran72cff492021-07-29 09:42:42 -070010659 * Attempts to set the radio power state for all phones for thermal reason.
10660 * This does not guarantee that the
Jack Nudelmanb0b87642020-11-12 15:04:39 -080010661 * requested radio power state will actually be set. See {@link
10662 * PhoneInternalInterface#setRadioPowerForReason} for more details.
10663 *
Jack Nudelmanb0b87642020-11-12 15:04:39 -080010664 * @param enable {@code true} if trying to turn radio on.
10665 * @return {@code true} if phone setRadioPowerForReason was called. Otherwise, returns {@code
10666 * false}.
10667 */
Sooraj Sasindran72cff492021-07-29 09:42:42 -070010668 private boolean setRadioPowerForThermal(boolean enable) {
10669 boolean isPhoneAvailable = false;
10670 for (int i = 0; i < TelephonyManager.getDefault().getActiveModemCount(); i++) {
10671 Phone phone = PhoneFactory.getPhone(i);
10672 if (phone != null) {
Thomas Nguyenfd0572f2022-07-15 22:28:49 +000010673 phone.setRadioPowerForReason(enable, TelephonyManager.RADIO_POWER_REASON_THERMAL);
Sooraj Sasindran72cff492021-07-29 09:42:42 -070010674 isPhoneAvailable = true;
10675 }
Jack Nudelmanb0b87642020-11-12 15:04:39 -080010676 }
Sooraj Sasindran72cff492021-07-29 09:42:42 -070010677
10678 // return true if successfully informed the phone object about the thermal radio power
10679 // request.
10680 return isPhoneAvailable;
Jack Nudelmanb0b87642020-11-12 15:04:39 -080010681 }
10682
10683 private int handleDataThrottlingRequest(int subId,
Sarah Chinecc78c42022-03-31 21:16:48 -070010684 DataThrottlingRequest dataThrottlingRequest, String callingPackage) {
Jack Nudelman5d6a98b2021-03-04 14:26:25 -080010685 boolean isDataThrottlingSupported = isRadioInterfaceCapabilitySupported(
10686 TelephonyManager.CAPABILITY_THERMAL_MITIGATION_DATA_THROTTLING);
10687 if (!isDataThrottlingSupported && dataThrottlingRequest.getDataThrottlingAction()
10688 != DataThrottlingRequest.DATA_THROTTLING_ACTION_NO_DATA_THROTTLING) {
10689 throw new IllegalArgumentException("modem does not support data throttling");
10690 }
10691
Jack Nudelmanb0b87642020-11-12 15:04:39 -080010692 // Ensure that radio is on. If not able to power on due to phone being unavailable, return
10693 // THERMAL_MITIGATION_RESULT_MODEM_NOT_AVAILABLE.
Sooraj Sasindran72cff492021-07-29 09:42:42 -070010694 if (!setRadioPowerForThermal(true)) {
Jack Nudelmanb0b87642020-11-12 15:04:39 -080010695 return TelephonyManager.THERMAL_MITIGATION_RESULT_MODEM_NOT_AVAILABLE;
10696 }
10697
Sarah Chinecc78c42022-03-31 21:16:48 -070010698 setDataEnabledForReason(
10699 subId, TelephonyManager.DATA_ENABLED_REASON_THERMAL, true, callingPackage);
Jack Nudelmanb0b87642020-11-12 15:04:39 -080010700
Jack Nudelman5d6a98b2021-03-04 14:26:25 -080010701 if (isDataThrottlingSupported) {
10702 int thermalMitigationResult =
Jack Nudelmanb0b87642020-11-12 15:04:39 -080010703 (int) sendRequest(CMD_SET_DATA_THROTTLING, dataThrottlingRequest, subId);
Jack Nudelman5d6a98b2021-03-04 14:26:25 -080010704 if (thermalMitigationResult == SET_DATA_THROTTLING_MODEM_THREW_INVALID_PARAMS) {
10705 throw new IllegalArgumentException("modem returned INVALID_ARGUMENTS");
10706 } else if (thermalMitigationResult
10707 == MODEM_DOES_NOT_SUPPORT_DATA_THROTTLING_ERROR_CODE) {
Jack Nudelman760d0962021-05-20 13:57:30 -070010708 log("Modem likely does not support data throttling on secondary carrier. Data " +
10709 "throttling action = " + dataThrottlingRequest.getDataThrottlingAction());
10710 return TelephonyManager.THERMAL_MITIGATION_RESULT_MODEM_ERROR;
Jack Nudelman5d6a98b2021-03-04 14:26:25 -080010711 }
10712 return thermalMitigationResult;
Jack Nudelmanb0b87642020-11-12 15:04:39 -080010713 }
Jack Nudelman5d6a98b2021-03-04 14:26:25 -080010714
10715 return TelephonyManager.THERMAL_MITIGATION_RESULT_SUCCESS;
Jack Nudelmanb0b87642020-11-12 15:04:39 -080010716 }
10717
Jack Nudelman644b91a2021-03-12 14:09:48 -080010718 private static List<String> getThermalMitigationAllowlist(Context context) {
10719 if (sThermalMitigationAllowlistedPackages.isEmpty()) {
10720 for (String pckg : context.getResources()
10721 .getStringArray(R.array.thermal_mitigation_allowlisted_packages)) {
10722 sThermalMitigationAllowlistedPackages.add(pckg);
10723 }
10724 }
10725
10726 return sThermalMitigationAllowlistedPackages;
10727 }
10728
Jack Nudelmane69bbc82021-05-13 10:00:15 -070010729 private boolean isAnyPhoneInEmergencyState() {
10730 TelecomManager tm = mApp.getSystemService(TelecomManager.class);
10731 if (tm.isInEmergencyCall()) {
10732 Log.e(LOG_TAG , "Phone state is not valid. One of the phones is in an emergency call");
10733 return true;
10734 }
10735 for (Phone phone : PhoneFactory.getPhones()) {
10736 if (phone.isInEmergencySmsMode() || phone.isInEcm()) {
10737 Log.e(LOG_TAG, "Phone state is not valid. isInEmergencySmsMode = "
10738 + phone.isInEmergencySmsMode() + " isInEmergencyCallbackMode = "
10739 + phone.isInEcm());
10740 return true;
10741 }
10742 }
10743
10744 return false;
10745 }
10746
Jack Nudelman644b91a2021-03-12 14:09:48 -080010747 /**
10748 * Used by shell commands to add an authorized package name for thermal mitigation.
10749 * @param packageName name of package to be allowlisted
10750 * @param context
10751 */
10752 static void addPackageToThermalMitigationAllowlist(String packageName, Context context) {
10753 sThermalMitigationAllowlistedPackages = getThermalMitigationAllowlist(context);
10754 sThermalMitigationAllowlistedPackages.add(packageName);
10755 }
10756
10757 /**
10758 * Used by shell commands to remove an authorized package name for thermal mitigation.
10759 * @param packageName name of package to remove from allowlist
10760 * @param context
10761 */
10762 static void removePackageFromThermalMitigationAllowlist(String packageName, Context context) {
10763 sThermalMitigationAllowlistedPackages = getThermalMitigationAllowlist(context);
10764 sThermalMitigationAllowlistedPackages.remove(packageName);
10765 }
10766
Jack Nudelmanb0b87642020-11-12 15:04:39 -080010767 /**
10768 * Thermal mitigation request to control functionalities at modem.
10769 *
10770 * @param subId the id of the subscription.
10771 * @param thermalMitigationRequest holds all necessary information to be passed down to modem.
Jack Nudelman644b91a2021-03-12 14:09:48 -080010772 * @param callingPackage the package name of the calling package.
Jack Nudelmanb0b87642020-11-12 15:04:39 -080010773 *
10774 * @return thermalMitigationResult enum as defined in android.telephony.Annotation.
10775 */
10776 @Override
10777 @ThermalMitigationResult
10778 public int sendThermalMitigationRequest(
10779 int subId,
Jack Nudelman644b91a2021-03-12 14:09:48 -080010780 ThermalMitigationRequest thermalMitigationRequest,
10781 String callingPackage) throws IllegalArgumentException {
Jack Nudelmanb0b87642020-11-12 15:04:39 -080010782 enforceModifyPermission();
10783
Jack Nudelman644b91a2021-03-12 14:09:48 -080010784 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
10785 if (!getThermalMitigationAllowlist(getDefaultPhone().getContext())
10786 .contains(callingPackage)) {
10787 throw new SecurityException("Calling package must be configured in the device config. "
10788 + "calling package: " + callingPackage);
10789 }
10790
Jack Nudelmanb0b87642020-11-12 15:04:39 -080010791 WorkSource workSource = getWorkSource(Binder.getCallingUid());
10792 final long identity = Binder.clearCallingIdentity();
10793
10794 int thermalMitigationResult = TelephonyManager.THERMAL_MITIGATION_RESULT_UNKNOWN_ERROR;
10795 try {
10796 int thermalMitigationAction = thermalMitigationRequest.getThermalMitigationAction();
10797 switch (thermalMitigationAction) {
10798 case ThermalMitigationRequest.THERMAL_MITIGATION_ACTION_DATA_THROTTLING:
10799 thermalMitigationResult =
10800 handleDataThrottlingRequest(subId,
Sarah Chinecc78c42022-03-31 21:16:48 -070010801 thermalMitigationRequest.getDataThrottlingRequest(),
10802 callingPackage);
Jack Nudelmanb0b87642020-11-12 15:04:39 -080010803 break;
10804 case ThermalMitigationRequest.THERMAL_MITIGATION_ACTION_VOICE_ONLY:
10805 if (thermalMitigationRequest.getDataThrottlingRequest() != null) {
10806 throw new IllegalArgumentException("dataThrottlingRequest must be null for "
10807 + "ThermalMitigationRequest.THERMAL_MITIGATION_ACTION_VOICE_ONLY");
10808 }
10809
10810 // Ensure that radio is on. If not able to power on due to phone being
10811 // unavailable, return THERMAL_MITIGATION_RESULT_MODEM_NOT_AVAILABLE.
Sooraj Sasindran72cff492021-07-29 09:42:42 -070010812 if (!setRadioPowerForThermal(true)) {
Jack Nudelmanb0b87642020-11-12 15:04:39 -080010813 thermalMitigationResult =
10814 TelephonyManager.THERMAL_MITIGATION_RESULT_MODEM_NOT_AVAILABLE;
10815 break;
10816 }
10817
10818 setDataEnabledForReason(subId, TelephonyManager.DATA_ENABLED_REASON_THERMAL,
Sarah Chinecc78c42022-03-31 21:16:48 -070010819 false, callingPackage);
Jack Nudelmanb0b87642020-11-12 15:04:39 -080010820 thermalMitigationResult = TelephonyManager.THERMAL_MITIGATION_RESULT_SUCCESS;
10821 break;
10822 case ThermalMitigationRequest.THERMAL_MITIGATION_ACTION_RADIO_OFF:
10823 if (thermalMitigationRequest.getDataThrottlingRequest() != null) {
10824 throw new IllegalArgumentException("dataThrottlingRequest must be null for"
10825 + " ThermalMitigationRequest.THERMAL_MITIGATION_ACTION_RADIO_OFF");
10826 }
10827
10828 TelecomAccountRegistry registry = TelecomAccountRegistry.getInstance(null);
10829 if (registry != null) {
Jack Nudelmanb0b87642020-11-12 15:04:39 -080010830 Phone phone = getPhone(subId);
10831 if (phone == null) {
10832 thermalMitigationResult =
10833 TelephonyManager.THERMAL_MITIGATION_RESULT_MODEM_NOT_AVAILABLE;
10834 break;
10835 }
10836
Jack Nudelmane69bbc82021-05-13 10:00:15 -070010837 TelephonyConnectionService service =
10838 registry.getTelephonyConnectionService();
Jack Nudelmanb30ac302021-06-17 15:39:58 -070010839 if (service != null && service.isEmergencyCallPending()) {
Jack Nudelmane69bbc82021-05-13 10:00:15 -070010840 Log.e(LOG_TAG, "An emergency call is pending");
10841 thermalMitigationResult =
10842 TelephonyManager.THERMAL_MITIGATION_RESULT_INVALID_STATE;
10843 break;
10844 } else if (isAnyPhoneInEmergencyState()) {
Jack Nudelmanb0b87642020-11-12 15:04:39 -080010845 thermalMitigationResult =
10846 TelephonyManager.THERMAL_MITIGATION_RESULT_INVALID_STATE;
10847 break;
10848 }
10849 } else {
10850 thermalMitigationResult =
10851 TelephonyManager.THERMAL_MITIGATION_RESULT_MODEM_NOT_AVAILABLE;
10852 break;
10853 }
10854
10855 // Turn radio off. If not able to power off due to phone being unavailable,
10856 // return THERMAL_MITIGATION_RESULT_MODEM_NOT_AVAILABLE.
Sooraj Sasindran72cff492021-07-29 09:42:42 -070010857 if (!setRadioPowerForThermal(false)) {
Jack Nudelmanb0b87642020-11-12 15:04:39 -080010858 thermalMitigationResult =
10859 TelephonyManager.THERMAL_MITIGATION_RESULT_MODEM_NOT_AVAILABLE;
10860 break;
10861 }
10862 thermalMitigationResult =
10863 TelephonyManager.THERMAL_MITIGATION_RESULT_SUCCESS;
10864 break;
10865 default:
10866 throw new IllegalArgumentException("the requested thermalMitigationAction does "
10867 + "not exist. Requested action: " + thermalMitigationAction);
10868 }
10869 } catch (IllegalArgumentException e) {
10870 throw e;
10871 } catch (Exception e) {
10872 Log.e(LOG_TAG, "thermalMitigationRequest. Exception e =" + e);
10873 thermalMitigationResult = TelephonyManager.THERMAL_MITIGATION_RESULT_MODEM_ERROR;
10874 } finally {
10875 Binder.restoreCallingIdentity(identity);
10876 }
10877
10878 if (DBG) {
10879 log("thermalMitigationRequest returning with thermalMitigationResult: "
10880 + thermalMitigationResult);
10881 }
10882
10883 return thermalMitigationResult;
10884 }
Hui Wang641e81c2020-10-12 12:14:23 -070010885
10886 /**
10887 * Set the GbaService Package Name that Telephony will bind to.
10888 *
10889 * @param subId The sim that the GbaService is associated with.
10890 * @param packageName The name of the package to be replaced with.
10891 * @return true if setting the GbaService to bind to succeeded, false if it did not.
10892 */
10893 @Override
10894 public boolean setBoundGbaServiceOverride(int subId, String packageName) {
10895 enforceModifyPermission();
10896
10897 final long identity = Binder.clearCallingIdentity();
10898 try {
10899 return getGbaManager(subId).overrideServicePackage(packageName);
10900 } finally {
10901 Binder.restoreCallingIdentity(identity);
10902 }
10903 }
10904
10905 /**
10906 * Return the package name of the currently bound GbaService.
10907 *
10908 * @param subId The sim that the GbaService is associated with.
10909 * @return the package name of the GbaService configuration, null if GBA is not supported.
10910 */
10911 @Override
10912 public String getBoundGbaService(int subId) {
10913 enforceReadPrivilegedPermission("getBoundGbaServicePackage");
10914
10915 final long identity = Binder.clearCallingIdentity();
10916 try {
10917 return getGbaManager(subId).getServicePackage();
10918 } finally {
10919 Binder.restoreCallingIdentity(identity);
10920 }
10921 }
10922
10923 /**
10924 * Set the release time for telephony to unbind GbaService.
10925 *
10926 * @param subId The sim that the GbaService is associated with.
10927 * @param interval The release time to unbind GbaService by millisecond.
10928 * @return true if setting the GbaService to bind to succeeded, false if it did not.
10929 */
10930 @Override
10931 public boolean setGbaReleaseTimeOverride(int subId, int interval) {
10932 enforceModifyPermission();
10933
10934 final long identity = Binder.clearCallingIdentity();
10935 try {
10936 return getGbaManager(subId).overrideReleaseTime(interval);
10937 } finally {
10938 Binder.restoreCallingIdentity(identity);
10939 }
10940 }
10941
10942 /**
10943 * Return the release time for telephony to unbind GbaService.
10944 *
10945 * @param subId The sim that the GbaService is associated with.
10946 * @return The release time to unbind GbaService by millisecond.
10947 */
10948 @Override
10949 public int getGbaReleaseTime(int subId) {
10950 enforceReadPrivilegedPermission("getGbaReleaseTime");
10951
10952 final long identity = Binder.clearCallingIdentity();
10953 try {
10954 return getGbaManager(subId).getReleaseTime();
10955 } finally {
10956 Binder.restoreCallingIdentity(identity);
10957 }
10958 }
10959
10960 private GbaManager getGbaManager(int subId) {
10961 GbaManager instance = GbaManager.getInstance(subId);
10962 if (instance == null) {
10963 String packageName = mApp.getResources().getString(R.string.config_gba_package);
10964 int releaseTime = mApp.getResources().getInteger(R.integer.config_gba_release_time);
10965 instance = GbaManager.make(mApp, subId, packageName, releaseTime);
10966 }
10967 return instance;
10968 }
Hui Wang761a6682020-10-31 05:12:53 +000010969
10970 /**
10971 * indicate whether the device and the carrier can support
10972 * RCS VoLTE single registration.
10973 */
10974 @Override
10975 public boolean isRcsVolteSingleRegistrationCapable(int subId) {
Brad Ebinger34c09a52021-02-17 23:23:21 +000010976 TelephonyPermissions.enforceAnyPermissionGrantedOrCarrierPrivileges(mApp, subId,
10977 Binder.getCallingUid(), "isRcsVolteSingleRegistrationCapable",
10978 Manifest.permission.PERFORM_IMS_SINGLE_REGISTRATION,
10979 permission.READ_PRIVILEGED_PHONE_STATE);
Hui Wang761a6682020-10-31 05:12:53 +000010980
10981 if (!SubscriptionManager.isValidSubscriptionId(subId)) {
10982 throw new IllegalArgumentException("Invalid Subscription ID: " + subId);
10983 }
10984
10985 final long identity = Binder.clearCallingIdentity();
10986 try {
10987 RcsProvisioningMonitor rpm = RcsProvisioningMonitor.getInstance();
10988 if (rpm != null) {
Hui Wang67af90e2021-06-04 16:57:15 -070010989 Boolean isCapable = rpm.isRcsVolteSingleRegistrationEnabled(subId);
10990 if (isCapable != null) {
10991 return isCapable;
10992 }
Hui Wang761a6682020-10-31 05:12:53 +000010993 }
Hui Wang67af90e2021-06-04 16:57:15 -070010994 throw new ServiceSpecificException(ImsException.CODE_ERROR_SERVICE_UNAVAILABLE,
10995 "service is temporarily unavailable.");
Hui Wang761a6682020-10-31 05:12:53 +000010996 } finally {
10997 Binder.restoreCallingIdentity(identity);
10998 }
10999 }
11000
11001 /**
11002 * Register RCS provisioning callback.
11003 */
11004 @Override
Hui Wang3cac7e52021-01-27 14:45:25 -080011005 public void registerRcsProvisioningCallback(int subId,
Hui Wang761a6682020-10-31 05:12:53 +000011006 IRcsConfigCallback callback) {
Brad Ebinger34c09a52021-02-17 23:23:21 +000011007 TelephonyPermissions.enforceAnyPermissionGrantedOrCarrierPrivileges(mApp, subId,
Hui Wang3cac7e52021-01-27 14:45:25 -080011008 Binder.getCallingUid(), "registerRcsProvisioningCallback",
Brad Ebinger34c09a52021-02-17 23:23:21 +000011009 Manifest.permission.PERFORM_IMS_SINGLE_REGISTRATION,
11010 permission.READ_PRIVILEGED_PHONE_STATE);
Hui Wang761a6682020-10-31 05:12:53 +000011011
11012 if (!SubscriptionManager.isValidSubscriptionId(subId)) {
11013 throw new IllegalArgumentException("Invalid Subscription ID: " + subId);
11014 }
11015 if (!isImsAvailableOnDevice()) {
11016 throw new ServiceSpecificException(ImsException.CODE_ERROR_UNSUPPORTED_OPERATION,
11017 "IMS not available on device.");
11018 }
11019
11020 final long identity = Binder.clearCallingIdentity();
11021 try {
Hui Wang68cd3722021-01-11 20:04:53 -080011022 if (!RcsProvisioningMonitor.getInstance()
Hui Wang3cac7e52021-01-27 14:45:25 -080011023 .registerRcsProvisioningCallback(subId, callback)) {
Brad Ebinger919631e2021-06-02 17:46:35 -070011024 throw new ServiceSpecificException(ImsException.CODE_ERROR_INVALID_SUBSCRIPTION,
11025 "Active subscription not found.");
Hui Wang68cd3722021-01-11 20:04:53 -080011026 }
Hui Wang761a6682020-10-31 05:12:53 +000011027 } finally {
11028 Binder.restoreCallingIdentity(identity);
11029 }
11030 }
11031
11032 /**
11033 * Unregister RCS provisioning callback.
11034 */
11035 @Override
Hui Wang3cac7e52021-01-27 14:45:25 -080011036 public void unregisterRcsProvisioningCallback(int subId,
Hui Wang761a6682020-10-31 05:12:53 +000011037 IRcsConfigCallback callback) {
Brad Ebinger34c09a52021-02-17 23:23:21 +000011038 TelephonyPermissions.enforceAnyPermissionGrantedOrCarrierPrivileges(mApp, subId,
Hui Wang3cac7e52021-01-27 14:45:25 -080011039 Binder.getCallingUid(), "unregisterRcsProvisioningCallback",
Brad Ebinger34c09a52021-02-17 23:23:21 +000011040 Manifest.permission.PERFORM_IMS_SINGLE_REGISTRATION,
11041 permission.READ_PRIVILEGED_PHONE_STATE);
Hui Wang761a6682020-10-31 05:12:53 +000011042
11043 if (!SubscriptionManager.isValidSubscriptionId(subId)) {
11044 throw new IllegalArgumentException("Invalid Subscription ID: " + subId);
11045 }
11046 if (!isImsAvailableOnDevice()) {
joonhunshin46b49a32022-12-21 05:33:23 +000011047 // operation failed silently
11048 Rlog.w(LOG_TAG, "IMS not available on device.");
11049 return;
Hui Wang761a6682020-10-31 05:12:53 +000011050 }
11051
11052 final long identity = Binder.clearCallingIdentity();
11053 try {
Hui Wang68cd3722021-01-11 20:04:53 -080011054 RcsProvisioningMonitor.getInstance()
Hui Wang3cac7e52021-01-27 14:45:25 -080011055 .unregisterRcsProvisioningCallback(subId, callback);
Hui Wang761a6682020-10-31 05:12:53 +000011056 } finally {
11057 Binder.restoreCallingIdentity(identity);
11058 }
11059 }
11060
11061 /**
11062 * trigger RCS reconfiguration.
11063 */
11064 public void triggerRcsReconfiguration(int subId) {
Brad Ebinger34c09a52021-02-17 23:23:21 +000011065 TelephonyPermissions.enforceAnyPermissionGranted(mApp, Binder.getCallingUid(),
11066 "triggerRcsReconfiguration",
11067 Manifest.permission.PERFORM_IMS_SINGLE_REGISTRATION);
Hui Wang761a6682020-10-31 05:12:53 +000011068
11069 if (!SubscriptionManager.isValidSubscriptionId(subId)) {
11070 throw new IllegalArgumentException("Invalid Subscription ID: " + subId);
11071 }
11072 if (!isImsAvailableOnDevice()) {
joonhunshin46b49a32022-12-21 05:33:23 +000011073 // ProvisioningManager can not handle ServiceSpecificException.
11074 // Throw the IllegalStateException and annotate ProvisioningManager.
11075 throw new IllegalStateException("IMS not available on device.");
Hui Wang761a6682020-10-31 05:12:53 +000011076 }
11077
11078 final long identity = Binder.clearCallingIdentity();
11079 try {
11080 RcsProvisioningMonitor.getInstance().requestReconfig(subId);
11081 } finally {
11082 Binder.restoreCallingIdentity(identity);
11083 }
11084 }
11085
11086 /**
11087 * Provide the client configuration parameters of the RCS application.
11088 */
11089 public void setRcsClientConfiguration(int subId, RcsClientConfiguration rcc) {
Brad Ebinger34c09a52021-02-17 23:23:21 +000011090 TelephonyPermissions.enforceAnyPermissionGranted(mApp, Binder.getCallingUid(),
11091 "setRcsClientConfiguration",
11092 Manifest.permission.PERFORM_IMS_SINGLE_REGISTRATION);
Hui Wang761a6682020-10-31 05:12:53 +000011093
11094 if (!SubscriptionManager.isValidSubscriptionId(subId)) {
11095 throw new IllegalArgumentException("Invalid Subscription ID: " + subId);
11096 }
11097 if (!isImsAvailableOnDevice()) {
11098 throw new ServiceSpecificException(ImsException.CODE_ERROR_UNSUPPORTED_OPERATION,
11099 "IMS not available on device.");
11100 }
11101
11102 final long identity = Binder.clearCallingIdentity();
11103
11104 try {
11105 IImsConfig configBinder = getImsConfig(getSlotIndex(subId), ImsFeature.FEATURE_RCS);
11106 if (configBinder == null) {
11107 Rlog.e(LOG_TAG, "null result for setRcsClientConfiguration");
Brad Ebinger919631e2021-06-02 17:46:35 -070011108 throw new ServiceSpecificException(ImsException.CODE_ERROR_INVALID_SUBSCRIPTION,
11109 "could not find the requested subscription");
Hui Wang761a6682020-10-31 05:12:53 +000011110 } else {
11111 configBinder.setRcsClientConfiguration(rcc);
11112 }
joonhunshin3e154242021-09-17 06:33:39 +000011113
11114 RcsStats.getInstance().onRcsClientProvisioningStats(subId,
11115 RCS_CLIENT_PROVISIONING_STATS__EVENT__CLIENT_PARAMS_SENT);
Hui Wang761a6682020-10-31 05:12:53 +000011116 } catch (RemoteException e) {
11117 Rlog.e(LOG_TAG, "fail to setRcsClientConfiguration " + e.getMessage());
Brad Ebinger919631e2021-06-02 17:46:35 -070011118 throw new ServiceSpecificException(ImsException.CODE_ERROR_SERVICE_UNAVAILABLE,
11119 "service is temporarily unavailable.");
Hui Wang761a6682020-10-31 05:12:53 +000011120 } finally {
11121 Binder.restoreCallingIdentity(identity);
11122 }
11123 }
11124
11125 /**
Hui Wangbaaee6a2021-02-19 20:45:36 -080011126 * Enables or disables the test mode for RCS VoLTE single registration.
11127 */
11128 @Override
11129 public void setRcsSingleRegistrationTestModeEnabled(boolean enabled) {
11130 TelephonyPermissions.enforceShellOnly(Binder.getCallingUid(),
11131 "setRcsSingleRegistrationTestModeEnabled");
11132
11133 RcsProvisioningMonitor.getInstance().setTestModeEnabled(enabled);
11134 }
11135
11136 /**
11137 * Gets the test mode for RCS VoLTE single registration.
11138 */
11139 @Override
11140 public boolean getRcsSingleRegistrationTestModeEnabled() {
11141 TelephonyPermissions.enforceShellOnly(Binder.getCallingUid(),
11142 "getRcsSingleRegistrationTestModeEnabled");
11143
11144 return RcsProvisioningMonitor.getInstance().getTestModeEnabled();
11145 }
11146
11147 /**
Hui Wang761a6682020-10-31 05:12:53 +000011148 * Overrides the config of RCS VoLTE single registration enabled for the device.
11149 */
11150 @Override
11151 public void setDeviceSingleRegistrationEnabledOverride(String enabledStr) {
11152 TelephonyPermissions.enforceShellOnly(Binder.getCallingUid(),
11153 "setDeviceSingleRegistrationEnabledOverride");
11154 enforceModifyPermission();
11155
11156 Boolean enabled = "NULL".equalsIgnoreCase(enabledStr) ? null
11157 : Boolean.parseBoolean(enabledStr);
11158 RcsProvisioningMonitor.getInstance().overrideDeviceSingleRegistrationEnabled(enabled);
Brad Ebinger49a72b42021-01-29 00:55:24 +000011159 mApp.imsRcsController.setDeviceSingleRegistrationSupportOverride(enabled);
Hui Wang761a6682020-10-31 05:12:53 +000011160 }
11161
11162 /**
Tyler Gunn92479152021-01-20 16:30:10 -080011163 * Sends a device to device communication message. Only usable via shell.
11164 * @param message message to send.
11165 * @param value message value.
11166 */
11167 @Override
11168 public void sendDeviceToDeviceMessage(int message, int value) {
11169 TelephonyPermissions.enforceShellOnly(Binder.getCallingUid(),
Tyler Gunnbabbda02021-02-10 11:05:02 -080011170 "sendDeviceToDeviceMessage");
Tyler Gunn92479152021-01-20 16:30:10 -080011171 enforceModifyPermission();
11172
11173 final long identity = Binder.clearCallingIdentity();
11174 try {
11175 TelephonyConnectionService service =
11176 TelecomAccountRegistry.getInstance(null).getTelephonyConnectionService();
11177 if (service == null) {
11178 Rlog.e(LOG_TAG, "sendDeviceToDeviceMessage: not in a call.");
11179 return;
11180 }
11181 service.sendTestDeviceToDeviceMessage(message, value);
11182 } finally {
11183 Binder.restoreCallingIdentity(identity);
11184 }
11185 }
11186
Tyler Gunnbabbda02021-02-10 11:05:02 -080011187 /**
11188 * Sets the specified device to device transport active.
11189 * @param transport The transport to set active.
11190 */
11191 @Override
11192 public void setActiveDeviceToDeviceTransport(@NonNull String transport) {
11193 TelephonyPermissions.enforceShellOnly(Binder.getCallingUid(),
11194 "setActiveDeviceToDeviceTransport");
11195 enforceModifyPermission();
11196
11197 final long identity = Binder.clearCallingIdentity();
11198 try {
11199 TelephonyConnectionService service =
11200 TelecomAccountRegistry.getInstance(null).getTelephonyConnectionService();
11201 if (service == null) {
11202 Rlog.e(LOG_TAG, "setActiveDeviceToDeviceTransport: not in a call.");
11203 return;
11204 }
11205 service.setActiveDeviceToDeviceTransport(transport);
11206 } finally {
11207 Binder.restoreCallingIdentity(identity);
11208 }
11209 }
Tyler Gunn92479152021-01-20 16:30:10 -080011210
Tyler Gunnd4339262021-05-03 14:46:49 -070011211 @Override
11212 public void setDeviceToDeviceForceEnabled(boolean isForceEnabled) {
11213 TelephonyPermissions.enforceShellOnly(Binder.getCallingUid(),
11214 "setDeviceToDeviceForceEnabled");
11215
11216 final long identity = Binder.clearCallingIdentity();
11217 try {
11218 Arrays.stream(PhoneFactory.getPhones()).forEach(
11219 p -> {
11220 Phone thePhone = p.getImsPhone();
11221 if (thePhone != null && thePhone instanceof ImsPhone) {
11222 ImsPhone imsPhone = (ImsPhone) thePhone;
11223 CallTracker tracker = imsPhone.getCallTracker();
11224 if (tracker != null && tracker instanceof ImsPhoneCallTracker) {
11225 ImsPhoneCallTracker imsPhoneCallTracker =
11226 (ImsPhoneCallTracker) tracker;
11227 imsPhoneCallTracker.setDeviceToDeviceForceEnabled(isForceEnabled);
11228 }
11229 }
11230 }
11231 );
11232 } finally {
11233 Binder.restoreCallingIdentity(identity);
11234 }
11235 }
11236
Tyler Gunn92479152021-01-20 16:30:10 -080011237 /**
Hui Wang761a6682020-10-31 05:12:53 +000011238 * Gets the config of RCS VoLTE single registration enabled for the device.
11239 */
11240 @Override
11241 public boolean getDeviceSingleRegistrationEnabled() {
11242 enforceReadPrivilegedPermission("getDeviceSingleRegistrationEnabled");
11243 return RcsProvisioningMonitor.getInstance().getDeviceSingleRegistrationEnabled();
11244 }
11245
11246 /**
11247 * Overrides the config of RCS VoLTE single registration enabled for the carrier/subscription.
11248 */
11249 @Override
11250 public boolean setCarrierSingleRegistrationEnabledOverride(int subId, String enabledStr) {
11251 TelephonyPermissions.enforceShellOnly(Binder.getCallingUid(),
11252 "setCarrierSingleRegistrationEnabledOverride");
11253 enforceModifyPermission();
11254
11255 Boolean enabled = "NULL".equalsIgnoreCase(enabledStr) ? null
11256 : Boolean.parseBoolean(enabledStr);
11257 return RcsProvisioningMonitor.getInstance().overrideCarrierSingleRegistrationEnabled(
11258 subId, enabled);
11259 }
11260
11261 /**
11262 * Gets the config of RCS VoLTE single registration enabled for the carrier/subscription.
11263 */
11264 @Override
11265 public boolean getCarrierSingleRegistrationEnabled(int subId) {
11266 enforceReadPrivilegedPermission("getCarrierSingleRegistrationEnabled");
11267 return RcsProvisioningMonitor.getInstance().getCarrierSingleRegistrationEnabled(subId);
11268 }
Chiachang Wangd6d34772020-12-22 11:38:27 +080011269
11270 /**
Hui Wangb647abe2021-02-26 09:33:38 -080011271 * Overrides the ims feature validation result
11272 */
11273 @Override
11274 public boolean setImsFeatureValidationOverride(int subId, String enabledStr) {
11275 TelephonyPermissions.enforceShellOnly(Binder.getCallingUid(),
11276 "setImsFeatureValidationOverride");
11277
11278 Boolean enabled = "NULL".equalsIgnoreCase(enabledStr) ? null
11279 : Boolean.parseBoolean(enabledStr);
11280 return RcsProvisioningMonitor.getInstance().overrideImsFeatureValidation(
11281 subId, enabled);
11282 }
11283
11284 /**
11285 * Gets the ims feature validation override value
11286 */
11287 @Override
11288 public boolean getImsFeatureValidationOverride(int subId) {
11289 TelephonyPermissions.enforceShellOnly(Binder.getCallingUid(),
11290 "getImsFeatureValidationOverride");
11291 return RcsProvisioningMonitor.getInstance().getImsFeatureValidationOverride(subId);
11292 }
11293
11294 /**
Chiachang Wangd6d34772020-12-22 11:38:27 +080011295 * Get the mobile provisioning url that is used to launch a browser to allow users to manage
11296 * their mobile plan.
11297 */
11298 @Override
11299 public String getMobileProvisioningUrl() {
11300 enforceReadPrivilegedPermission("getMobileProvisioningUrl");
11301 final long identity = Binder.clearCallingIdentity();
11302 try {
11303 return getDefaultPhone().getMobileProvisioningUrl();
11304 } finally {
11305 Binder.restoreCallingIdentity(identity);
11306 }
11307 }
Rambo Wanga5cc9b72021-01-07 10:51:54 -080011308
James.cf Linbcdf8b32021-01-14 16:44:13 +080011309 /**
calvinpane4a8a1d2021-01-25 13:51:18 +080011310 * Get the EAB contact from the EAB database.
11311 */
11312 @Override
11313 public String getContactFromEab(String contact) {
11314 TelephonyPermissions.enforceShellOnly(Binder.getCallingUid(), "getContactFromEab");
11315 enforceModifyPermission();
11316 final long identity = Binder.clearCallingIdentity();
11317 try {
11318 return EabUtil.getContactFromEab(getDefaultPhone().getContext(), contact);
11319 } finally {
11320 Binder.restoreCallingIdentity(identity);
11321 }
11322 }
11323
11324 /**
Calvin Pana1434322021-07-01 19:27:01 +080011325 * Get the EAB capability from the EAB database.
11326 */
11327 @Override
11328 public String getCapabilityFromEab(String contact) {
11329 TelephonyPermissions.enforceShellOnly(Binder.getCallingUid(), "getCapabilityFromEab");
11330 enforceModifyPermission();
11331 final long identity = Binder.clearCallingIdentity();
11332 try {
11333 return EabUtil.getCapabilityFromEab(getDefaultPhone().getContext(), contact);
11334 } finally {
11335 Binder.restoreCallingIdentity(identity);
11336 }
11337 }
11338
11339 /**
James.cf Linbcdf8b32021-01-14 16:44:13 +080011340 * Remove the EAB contacts from the EAB database.
11341 */
11342 @Override
11343 public int removeContactFromEab(int subId, String contacts) {
11344 TelephonyPermissions.enforceShellOnly(Binder.getCallingUid(), "removeCapabilitiesFromEab");
11345 enforceModifyPermission();
11346 final long identity = Binder.clearCallingIdentity();
11347 try {
11348 return EabUtil.removeContactFromEab(subId, contacts, getDefaultPhone().getContext());
11349 } finally {
11350 Binder.restoreCallingIdentity(identity);
11351 }
11352 }
11353
Rambo Wanga5cc9b72021-01-07 10:51:54 -080011354 @Override
James.cf Lin4b784aa2021-01-31 03:25:15 +080011355 public boolean getDeviceUceEnabled() {
11356 TelephonyPermissions.enforceShellOnly(Binder.getCallingUid(), "getDeviceUceEnabled");
11357 final long identity = Binder.clearCallingIdentity();
11358 try {
11359 return mApp.getDeviceUceEnabled();
11360 } finally {
11361 Binder.restoreCallingIdentity(identity);
11362 }
11363 }
11364
11365 @Override
11366 public void setDeviceUceEnabled(boolean isEnabled) {
11367 TelephonyPermissions.enforceShellOnly(Binder.getCallingUid(), "setDeviceUceEnabled");
11368 final long identity = Binder.clearCallingIdentity();
11369 try {
11370 mApp.setDeviceUceEnabled(isEnabled);
11371 } finally {
11372 Binder.restoreCallingIdentity(identity);
11373 }
11374 }
11375
Brad Ebinger14d467f2021-02-12 06:18:28 +000011376 /**
11377 * Add new feature tags to the Set used to calculate the capabilities in PUBLISH.
11378 * @return current RcsContactUceCapability instance that will be used for PUBLISH.
11379 */
11380 // Used for SHELL command only right now.
11381 @Override
11382 public RcsContactUceCapability addUceRegistrationOverrideShell(int subId,
11383 List<String> featureTags) {
11384 TelephonyPermissions.enforceShellOnly(Binder.getCallingUid(),
11385 "addUceRegistrationOverrideShell");
11386 final long identity = Binder.clearCallingIdentity();
11387 try {
11388 return mApp.imsRcsController.addUceRegistrationOverrideShell(subId,
11389 new ArraySet<>(featureTags));
11390 } catch (ImsException e) {
11391 throw new ServiceSpecificException(e.getCode(), e.getMessage());
11392 } finally {
11393 Binder.restoreCallingIdentity(identity);
11394 }
11395 }
11396
11397 /**
11398 * Remove existing feature tags to the Set used to calculate the capabilities in PUBLISH.
11399 * @return current RcsContactUceCapability instance that will be used for PUBLISH.
11400 */
11401 // Used for SHELL command only right now.
11402 @Override
11403 public RcsContactUceCapability removeUceRegistrationOverrideShell(int subId,
11404 List<String> featureTags) {
11405 TelephonyPermissions.enforceShellOnly(Binder.getCallingUid(),
11406 "removeUceRegistrationOverrideShell");
11407 final long identity = Binder.clearCallingIdentity();
11408 try {
11409 return mApp.imsRcsController.removeUceRegistrationOverrideShell(subId,
11410 new ArraySet<>(featureTags));
11411 } catch (ImsException e) {
11412 throw new ServiceSpecificException(e.getCode(), e.getMessage());
11413 } finally {
11414 Binder.restoreCallingIdentity(identity);
11415 }
11416 }
11417
11418 /**
11419 * Clear all overrides in the Set used to calculate the capabilities in PUBLISH.
11420 * @return current RcsContactUceCapability instance that will be used for PUBLISH.
11421 */
11422 // Used for SHELL command only right now.
11423 @Override
11424 public RcsContactUceCapability clearUceRegistrationOverrideShell(int subId) {
11425 TelephonyPermissions.enforceShellOnly(Binder.getCallingUid(),
11426 "clearUceRegistrationOverrideShell");
11427 final long identity = Binder.clearCallingIdentity();
11428 try {
11429 return mApp.imsRcsController.clearUceRegistrationOverrideShell(subId);
11430 } catch (ImsException e) {
11431 throw new ServiceSpecificException(e.getCode(), e.getMessage());
11432 } finally {
11433 Binder.restoreCallingIdentity(identity);
11434 }
11435 }
11436
11437 /**
11438 * @return current RcsContactUceCapability instance that will be used for PUBLISH.
11439 */
11440 // Used for SHELL command only right now.
11441 @Override
11442 public RcsContactUceCapability getLatestRcsContactUceCapabilityShell(int subId) {
11443 TelephonyPermissions.enforceShellOnly(Binder.getCallingUid(),
11444 "getLatestRcsContactUceCapabilityShell");
11445 final long identity = Binder.clearCallingIdentity();
11446 try {
11447 return mApp.imsRcsController.getLatestRcsContactUceCapabilityShell(subId);
11448 } catch (ImsException e) {
11449 throw new ServiceSpecificException(e.getCode(), e.getMessage());
11450 } finally {
11451 Binder.restoreCallingIdentity(identity);
11452 }
11453 }
11454
11455 /**
11456 * Returns the last PIDF XML sent to the network during the last PUBLISH or "none" if the
11457 * device does not have an active PUBLISH.
11458 */
11459 // Used for SHELL command only right now.
11460 @Override
11461 public String getLastUcePidfXmlShell(int subId) {
11462 TelephonyPermissions.enforceShellOnly(Binder.getCallingUid(), "uceGetLastPidfXml");
11463 final long identity = Binder.clearCallingIdentity();
11464 try {
11465 return mApp.imsRcsController.getLastUcePidfXmlShell(subId);
11466 } catch (ImsException e) {
11467 throw new ServiceSpecificException(e.getCode(), e.getMessage());
11468 } finally {
11469 Binder.restoreCallingIdentity(identity);
11470 }
11471 }
11472
James.cf Line8713a42021-04-29 16:04:26 +080011473 /**
11474 * Remove UCE requests cannot be sent to the network status.
11475 */
11476 // Used for SHELL command only right now.
11477 @Override
11478 public boolean removeUceRequestDisallowedStatus(int subId) {
11479 TelephonyPermissions.enforceShellOnly(Binder.getCallingUid(), "uceRemoveDisallowedStatus");
11480 final long identity = Binder.clearCallingIdentity();
11481 try {
11482 return mApp.imsRcsController.removeUceRequestDisallowedStatus(subId);
11483 } catch (ImsException e) {
11484 throw new ServiceSpecificException(e.getCode(), e.getMessage());
11485 } finally {
11486 Binder.restoreCallingIdentity(identity);
11487 }
11488 }
11489
James.cf Lin18bb9002021-05-25 01:37:38 +080011490 /**
11491 * Remove UCE requests cannot be sent to the network status.
11492 */
11493 // Used for SHELL command only.
11494 @Override
11495 public boolean setCapabilitiesRequestTimeout(int subId, long timeoutAfterMs) {
11496 TelephonyPermissions.enforceShellOnly(Binder.getCallingUid(), "setCapRequestTimeout");
11497 final long identity = Binder.clearCallingIdentity();
11498 try {
11499 return mApp.imsRcsController.setCapabilitiesRequestTimeout(subId, timeoutAfterMs);
11500 } catch (ImsException e) {
11501 throw new ServiceSpecificException(e.getCode(), e.getMessage());
11502 } finally {
11503 Binder.restoreCallingIdentity(identity);
11504 }
11505 }
Brad Ebinger14d467f2021-02-12 06:18:28 +000011506
James.cf Lin4b784aa2021-01-31 03:25:15 +080011507 @Override
Rambo Wanga5cc9b72021-01-07 10:51:54 -080011508 public void setSignalStrengthUpdateRequest(int subId, SignalStrengthUpdateRequest request,
11509 String callingPackage) {
11510 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
11511 mApp, subId, "setSignalStrengthUpdateRequest");
11512
11513 final int callingUid = Binder.getCallingUid();
11514 // Verify that tha callingPackage belongs to the calling UID
11515 mApp.getSystemService(AppOpsManager.class)
11516 .checkPackage(callingUid, callingPackage);
11517
Rambo Wang3607f502021-02-01 21:51:40 -080011518 validateSignalStrengthUpdateRequest(mApp, request, callingUid);
Rambo Wanga5cc9b72021-01-07 10:51:54 -080011519
11520 final long identity = Binder.clearCallingIdentity();
11521 try {
11522 Object result = sendRequest(CMD_SET_SIGNAL_STRENGTH_UPDATE_REQUEST,
11523 new Pair<Integer, SignalStrengthUpdateRequest>(callingUid, request), subId);
11524
11525 if (result instanceof IllegalStateException) {
11526 throw (IllegalStateException) result;
11527 }
11528 } finally {
11529 Binder.restoreCallingIdentity(identity);
11530 }
11531 }
11532
11533 @Override
11534 public void clearSignalStrengthUpdateRequest(int subId, SignalStrengthUpdateRequest request,
11535 String callingPackage) {
11536 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
11537 mApp, subId, "clearSignalStrengthUpdateRequest");
11538
11539 final int callingUid = Binder.getCallingUid();
11540 // Verify that tha callingPackage belongs to the calling UID
11541 mApp.getSystemService(AppOpsManager.class)
11542 .checkPackage(callingUid, callingPackage);
11543
11544 final long identity = Binder.clearCallingIdentity();
11545 try {
11546 Object result = sendRequest(CMD_CLEAR_SIGNAL_STRENGTH_UPDATE_REQUEST,
11547 new Pair<Integer, SignalStrengthUpdateRequest>(callingUid, request), subId);
11548
11549 if (result instanceof IllegalStateException) {
11550 throw (IllegalStateException) result;
11551 }
11552 } finally {
11553 Binder.restoreCallingIdentity(identity);
11554 }
11555 }
11556
Rambo Wang3607f502021-02-01 21:51:40 -080011557 private static void validateSignalStrengthUpdateRequest(Context context,
11558 SignalStrengthUpdateRequest request, int callingUid) {
Rambo Wanga5cc9b72021-01-07 10:51:54 -080011559 if (callingUid == Process.PHONE_UID || callingUid == Process.SYSTEM_UID) {
11560 // phone/system process do not have further restriction on request
11561 return;
11562 }
11563
11564 // Applications has restrictions on how to use the request:
Rambo Wang3607f502021-02-01 21:51:40 -080011565 // Non-system callers need permission to set mIsSystemThresholdReportingRequestedWhileIdle
Rambo Wanga5cc9b72021-01-07 10:51:54 -080011566 if (request.isSystemThresholdReportingRequestedWhileIdle()) {
Rambo Wang3607f502021-02-01 21:51:40 -080011567 context.enforceCallingOrSelfPermission(
11568 android.Manifest.permission.LISTEN_ALWAYS_REPORTED_SIGNAL_STRENGTH,
11569 "validateSignalStrengthUpdateRequest");
Rambo Wanga5cc9b72021-01-07 10:51:54 -080011570 }
11571
11572 for (SignalThresholdInfo info : request.getSignalThresholdInfos()) {
Nagendra Prasad Nagarle Basavarajufee544c2022-12-07 16:34:52 +000011573 // Only system caller can set mHysteresisMs/mIsEnabled.
Rambo Wanga5cc9b72021-01-07 10:51:54 -080011574 if (info.getHysteresisMs() != SignalThresholdInfo.HYSTERESIS_MS_DISABLED
Rambo Wanga5cc9b72021-01-07 10:51:54 -080011575 || info.isEnabled()) {
11576 throw new IllegalArgumentException(
11577 "Only system can set hide fields in SignalThresholdInfo");
11578 }
11579
11580 // Thresholds length for each RAN need in range. This has been validated in
11581 // SignalThresholdInfo#Builder#setThreshold. Here we prevent apps calling hide method
11582 // setThresholdUnlimited (e.g. through reflection) with too short or too long thresholds
11583 final int[] thresholds = info.getThresholds();
11584 Objects.requireNonNull(thresholds);
11585 if (thresholds.length < SignalThresholdInfo.getMinimumNumberOfThresholdsAllowed()
11586 || thresholds.length
11587 > SignalThresholdInfo.getMaximumNumberOfThresholdsAllowed()) {
11588 throw new IllegalArgumentException(
11589 "thresholds length is out of range: " + thresholds.length);
11590 }
11591 }
11592 }
SongFerngWang8236caa2021-01-17 21:51:44 +080011593
11594 /**
11595 * Gets the current phone capability.
11596 *
11597 * Requires carrier privileges or READ_PRECISE_PHONE_STATE permission.
11598 * @return the PhoneCapability which describes the data connection capability of modem.
11599 * It's used to evaluate possible phone config change, for example from single
11600 * SIM device to multi-SIM device.
11601 */
11602 @Override
11603 public PhoneCapability getPhoneCapability() {
11604 enforceReadPrivilegedPermission("getPhoneCapability");
11605 final long identity = Binder.clearCallingIdentity();
11606 try {
11607 return mPhoneConfigurationManager.getCurrentPhoneCapability();
11608 } finally {
11609 Binder.restoreCallingIdentity(identity);
11610 }
11611 }
Michele Berionne5e411512020-11-13 02:36:59 +000011612
11613 /**
11614 * Prepare TelephonyManager for an unattended reboot. The reboot is
11615 * required to be done shortly after the API is invoked.
11616 */
11617 @Override
11618 @TelephonyManager.PrepareUnattendedRebootResult
11619 public int prepareForUnattendedReboot() {
Rafael Higuera Silvad9630642021-09-20 15:32:01 +000011620 WorkSource workSource = getWorkSource(Binder.getCallingUid());
Michele Berionne5e411512020-11-13 02:36:59 +000011621 enforceRebootPermission();
11622
11623 final long identity = Binder.clearCallingIdentity();
11624 try {
Rafael Higuera Silvad9630642021-09-20 15:32:01 +000011625 return (int) sendRequest(CMD_PREPARE_UNATTENDED_REBOOT, null, workSource);
Michele Berionne5e411512020-11-13 02:36:59 +000011626 } finally {
11627 Binder.restoreCallingIdentity(identity);
11628 }
11629 }
Hongbo Zeng156aa4a2021-02-08 21:50:28 +080011630
11631 /**
11632 * Request to get the current slicing configuration including URSP rules and
11633 * NSSAIs (configured, allowed and rejected).
11634 *
11635 * Requires carrier privileges or READ_PRIVILEGED_PHONE_STATE permission.
11636 */
11637 @Override
11638 public void getSlicingConfig(ResultReceiver callback) {
Hongbo Zeng1b2063d2022-02-21 01:33:03 +000011639 TelephonyPermissions
11640 .enforceCallingOrSelfReadPrivilegedPhoneStatePermissionOrCarrierPrivilege(
11641 mApp, SubscriptionManager.INVALID_SUBSCRIPTION_ID, "getSlicingConfig");
Hongbo Zeng156aa4a2021-02-08 21:50:28 +080011642
11643 final long identity = Binder.clearCallingIdentity();
11644 try {
11645 Phone phone = getDefaultPhone();
11646 sendRequestAsync(CMD_GET_SLICING_CONFIG, callback, phone, null);
11647 } finally {
11648 Binder.restoreCallingIdentity(identity);
11649 }
11650 }
Hunsuk Choi3b742d62021-10-25 19:48:34 +000011651
11652 /**
Sarah Chin2ec39f62022-08-31 17:03:26 -070011653 * Check whether the given premium capability is available for purchase from the carrier.
11654 *
11655 * @param capability The premium capability to check.
11656 * @param subId The subId to check the premium capability for.
11657 *
11658 * @return Whether the given premium capability is available to purchase.
11659 */
11660 @Override
11661 public boolean isPremiumCapabilityAvailableForPurchase(int capability, int subId) {
11662 if (!TelephonyPermissions.checkCallingOrSelfReadNonDangerousPhoneStateNoThrow(
11663 mApp, "isPremiumCapabilityAvailableForPurchase")) {
11664 log("Premium capability "
11665 + TelephonyManager.convertPremiumCapabilityToString(capability)
11666 + " is not available for purchase due to missing permissions.");
11667 throw new SecurityException("isPremiumCapabilityAvailableForPurchase requires "
11668 + "permission READ_BASIC_PHONE_STATE.");
11669 }
11670
11671 Phone phone = getPhone(subId);
Thomas Nguyen7216ed62022-11-29 16:45:31 -080011672 if (phone == null) {
11673 loge("isPremiumCapabilityAvailableForPurchase: phone is null, subId=" + subId);
11674 return false;
11675 }
Sarah Chin2ec39f62022-08-31 17:03:26 -070011676 final long identity = Binder.clearCallingIdentity();
11677 try {
Sarah Chin46355ba2022-11-01 23:51:16 -070011678 return SlicePurchaseController.getInstance(phone)
Sarah Chin2ec39f62022-08-31 17:03:26 -070011679 .isPremiumCapabilityAvailableForPurchase(capability);
11680 } finally {
11681 Binder.restoreCallingIdentity(identity);
11682 }
11683 }
11684
11685 /**
11686 * Purchase the given premium capability from the carrier.
11687 *
11688 * @param capability The premium capability to purchase.
11689 * @param callback The result of the purchase request.
11690 * @param subId The subId to purchase the premium capability for.
11691 */
11692 @Override
11693 public void purchasePremiumCapability(int capability, IIntegerConsumer callback, int subId) {
11694 log("purchasePremiumCapability: capability="
11695 + TelephonyManager.convertPremiumCapabilityToString(capability) + ", caller="
11696 + getCurrentPackageName());
11697
11698 if (!TelephonyPermissions.checkCallingOrSelfReadNonDangerousPhoneStateNoThrow(
11699 mApp, "purchasePremiumCapability")) {
11700 log("purchasePremiumCapability "
11701 + TelephonyManager.convertPremiumCapabilityToString(capability)
11702 + " failed due to missing permissions.");
11703 throw new SecurityException("purchasePremiumCapability requires permission "
11704 + "READ_BASIC_PHONE_STATE.");
Sarah Chin532d6bb2022-12-28 22:50:43 -080011705 } else if (!TelephonyPermissions.checkInternetPermissionNoThrow(
11706 mApp, "purchasePremiumCapability")) {
11707 log("purchasePremiumCapability "
11708 + TelephonyManager.convertPremiumCapabilityToString(capability)
11709 + " failed due to missing permissions.");
11710 throw new SecurityException("purchasePremiumCapability requires permission INTERNET.");
Sarah Chin2ec39f62022-08-31 17:03:26 -070011711 }
11712
11713 Phone phone = getPhone(subId);
Sarah Chin19694112022-12-06 15:41:37 -080011714 if (phone == null) {
11715 try {
11716 int result = TelephonyManager.PURCHASE_PREMIUM_CAPABILITY_RESULT_REQUEST_FAILED;
11717 callback.accept(result);
11718 loge("purchasePremiumCapability: phone is null, subId=" + subId);
11719 } catch (RemoteException e) {
11720 String logStr = "Purchase premium capability "
11721 + TelephonyManager.convertPremiumCapabilityToString(capability)
11722 + " failed due to RemoteException handling null phone: " + e;
11723 if (DBG) log(logStr);
11724 AnomalyReporter.reportAnomaly(
11725 UUID.fromString(PURCHASE_PREMIUM_CAPABILITY_ERROR_UUID), logStr);
11726 }
11727 return;
11728 }
Sarah Chin532d6bb2022-12-28 22:50:43 -080011729
11730 String callingProcess;
Sarah Chin71b3a852022-09-28 15:54:19 -070011731 try {
Sarah Chin532d6bb2022-12-28 22:50:43 -080011732 callingProcess = mApp.getPackageManager().getApplicationInfo(
11733 getCurrentPackageName(), 0).processName;
Sarah Chin71b3a852022-09-28 15:54:19 -070011734 } catch (PackageManager.NameNotFoundException e) {
Sarah Chin532d6bb2022-12-28 22:50:43 -080011735 callingProcess = getCurrentPackageName();
Sarah Chin71b3a852022-09-28 15:54:19 -070011736 }
Sarah Chin532d6bb2022-12-28 22:50:43 -080011737
11738 boolean isVisible = false;
11739 ActivityManager am = mApp.getSystemService(ActivityManager.class);
11740 if (am != null) {
11741 List<ActivityManager.RunningAppProcessInfo> processes = am.getRunningAppProcesses();
11742 if (processes != null) {
11743 for (ActivityManager.RunningAppProcessInfo process : processes) {
11744 log("purchasePremiumCapability: process " + process.processName
11745 + "has importance " + process.importance);
11746 if (process.processName.equals(callingProcess) && process.importance
11747 <= ActivityManager.RunningAppProcessInfo.IMPORTANCE_VISIBLE) {
11748 isVisible = true;
11749 break;
11750 }
11751 }
11752 }
11753 }
11754
11755 if (!isVisible) {
11756 try {
11757 int result = TelephonyManager.PURCHASE_PREMIUM_CAPABILITY_RESULT_NOT_FOREGROUND;
11758 callback.accept(result);
11759 loge("purchasePremiumCapability: " + callingProcess + " is not in the foreground.");
11760 } catch (RemoteException e) {
11761 String logStr = "Purchase premium capability "
11762 + TelephonyManager.convertPremiumCapabilityToString(capability)
11763 + " failed due to RemoteException handling background application: " + e;
11764 if (DBG) log(logStr);
11765 AnomalyReporter.reportAnomaly(
11766 UUID.fromString(PURCHASE_PREMIUM_CAPABILITY_ERROR_UUID), logStr);
11767 }
11768 return;
11769 }
11770
Sarah Chin71b3a852022-09-28 15:54:19 -070011771 sendRequestAsync(CMD_PURCHASE_PREMIUM_CAPABILITY,
Sarah Chinb8218c22023-01-04 13:35:29 -080011772 new PurchasePremiumCapabilityArgument(capability, callback), phone, null);
Sarah Chin2ec39f62022-08-31 17:03:26 -070011773 }
11774
11775 /**
Hunsuk Choi3b742d62021-10-25 19:48:34 +000011776 * Register an IMS connection state callback
11777 */
11778 @Override
Hunsuk Choi89bd22c2021-11-01 13:04:54 +000011779 public void registerImsStateCallback(int subId, int feature, IImsStateCallback cb,
11780 String callingPackage) {
Hunsuk Choi3b742d62021-10-25 19:48:34 +000011781 if (feature == ImsFeature.FEATURE_MMTEL) {
11782 // ImsMmTelManager
11783 // The following also checks READ_PRIVILEGED_PHONE_STATE.
11784 TelephonyPermissions
11785 .enforceCallingOrSelfReadPrecisePhoneStatePermissionOrCarrierPrivilege(
11786 mApp, subId, "registerImsStateCallback");
11787 } else if (feature == ImsFeature.FEATURE_RCS) {
11788 // ImsRcsManager or SipDelegateManager
11789 TelephonyPermissions.enforceAnyPermissionGrantedOrCarrierPrivileges(mApp, subId,
11790 Binder.getCallingUid(), "registerImsStateCallback",
11791 Manifest.permission.READ_PRIVILEGED_PHONE_STATE,
11792 Manifest.permission.READ_PRECISE_PHONE_STATE,
11793 Manifest.permission.ACCESS_RCS_USER_CAPABILITY_EXCHANGE,
11794 Manifest.permission.PERFORM_IMS_SINGLE_REGISTRATION);
11795 }
11796
11797 if (!ImsManager.isImsSupportedOnDevice(mApp)) {
11798 throw new ServiceSpecificException(ImsException.CODE_ERROR_UNSUPPORTED_OPERATION,
11799 "IMS not available on device.");
11800 }
11801
11802 if (subId == SubscriptionManager.DEFAULT_SUBSCRIPTION_ID) {
11803 throw new ServiceSpecificException(ImsException.CODE_ERROR_INVALID_SUBSCRIPTION);
11804 }
11805
11806 ImsStateCallbackController controller = ImsStateCallbackController.getInstance();
11807 if (controller == null) {
11808 throw new ServiceSpecificException(ImsException.CODE_ERROR_UNSUPPORTED_OPERATION,
11809 "IMS not available on device.");
11810 }
11811
Hunsuk Choi89bd22c2021-11-01 13:04:54 +000011812 if (callingPackage == null) {
11813 callingPackage = getCurrentPackageName();
11814 }
11815
Hunsuk Choi3b742d62021-10-25 19:48:34 +000011816 final long token = Binder.clearCallingIdentity();
11817 try {
11818 int slotId = getSlotIndexOrException(subId);
Hunsuk Choi89bd22c2021-11-01 13:04:54 +000011819 controller.registerImsStateCallback(subId, feature, cb, callingPackage);
Hunsuk Choi3b742d62021-10-25 19:48:34 +000011820 } catch (ImsException e) {
11821 throw new ServiceSpecificException(e.getCode());
11822 } finally {
11823 Binder.restoreCallingIdentity(token);
11824 }
11825 }
11826
11827 /**
11828 * Unregister an IMS connection state callback
11829 */
11830 @Override
11831 public void unregisterImsStateCallback(IImsStateCallback cb) {
11832 final long token = Binder.clearCallingIdentity();
11833 ImsStateCallbackController controller = ImsStateCallbackController.getInstance();
11834 if (controller == null) {
11835 return;
11836 }
11837 try {
11838 controller.unregisterImsStateCallback(cb);
11839 } finally {
11840 Binder.restoreCallingIdentity(token);
11841 }
11842 }
Sooraj Sasindranfae41b32021-10-26 02:10:05 -070011843
11844 /**
11845 * @return {@CellIdentity} last known cell identity {@CellIdentity}.
11846 *
11847 * Require {@link android.Manifest.permission#ACCESS_FINE_LOCATION} and
11848 * com.android.phone.permission.ACCESS_LAST_KNOWN_CELL_ID, otherwise throws
11849 * SecurityException.
11850 * If there is current registered network this value will be same as the registered cell
11851 * identity. If the device goes out of service the previous cell identity is cached and
11852 * will be returned. If the cache age of the Cell identity is more than 24 hours
11853 * it will be cleared and null will be returned.
11854 *
11855 */
11856 @Override
11857 public @Nullable CellIdentity getLastKnownCellIdentity(int subId, String callingPackage,
11858 String callingFeatureId) {
11859 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
11860 LocationAccessPolicy.LocationPermissionResult fineLocationResult =
11861 LocationAccessPolicy.checkLocationPermission(mApp,
11862 new LocationAccessPolicy.LocationPermissionQuery.Builder()
11863 .setCallingPackage(callingPackage)
11864 .setCallingFeatureId(callingFeatureId)
11865 .setCallingPid(Binder.getCallingPid())
11866 .setCallingUid(Binder.getCallingUid())
11867 .setMethod("getLastKnownCellIdentity")
11868 .setLogAsInfo(true)
11869 .setMinSdkVersionForFine(Build.VERSION_CODES.Q)
11870 .setMinSdkVersionForCoarse(Build.VERSION_CODES.Q)
11871 .setMinSdkVersionForEnforcement(Build.VERSION_CODES.Q)
11872 .build());
11873
11874 boolean hasFinePermission =
11875 fineLocationResult == LocationAccessPolicy.LocationPermissionResult.ALLOWED;
11876 if (!hasFinePermission
11877 || !TelephonyPermissions.checkLastKnownCellIdAccessPermission(mApp)) {
11878 throw new SecurityException("getLastKnownCellIdentity need ACCESS_FINE_LOCATION "
Rambo Wang918993a2022-04-27 09:08:36 -070011879 + "and ACCESS_LAST_KNOWN_CELL_ID permission.");
Sooraj Sasindranfae41b32021-10-26 02:10:05 -070011880 }
11881
11882 final long identity = Binder.clearCallingIdentity();
11883 try {
11884 Phone phone = getPhone(subId);
11885 if (phone == null) return null;
11886 ServiceStateTracker sst = phone.getServiceStateTracker();
11887 if (sst == null) return null;
11888 return sst.getLastKnownCellIdentity();
11889 } finally {
11890 Binder.restoreCallingIdentity(identity);
11891 }
11892 }
Jack Yu4c0a5502021-12-03 23:58:26 -080011893
jimsun3b9ccac2021-10-26 15:01:23 +080011894 /**
11895 * Sets the modem service class Name that Telephony will bind to.
11896 *
11897 * @param serviceName The class name of the modem service.
11898 * @return true if the operation is succeed, otherwise false.
11899 */
11900 public boolean setModemService(String serviceName) {
11901 Log.d(LOG_TAG, "setModemService - " + serviceName);
11902 TelephonyPermissions.enforceShellOnly(Binder.getCallingUid(), "setModemService");
11903 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(mApp,
11904 SubscriptionManager.INVALID_SUBSCRIPTION_ID,
11905 "setModemService");
11906 return mPhoneConfigurationManager.setModemService(serviceName);
11907 }
11908
11909 /**
11910 * Return the class name of the currently bounded modem service.
11911 *
11912 * @return the class name of the modem service.
11913 */
11914 public String getModemService() {
11915 String result;
11916 Log.d(LOG_TAG, "getModemService");
11917 TelephonyPermissions.enforceShellOnly(Binder.getCallingUid(), "getModemService");
11918 TelephonyPermissions
11919 .enforceCallingOrSelfReadPrivilegedPhoneStatePermissionOrCarrierPrivilege(
11920 mApp, SubscriptionManager.INVALID_SUBSCRIPTION_ID,
11921 "getModemService");
11922 result = mPhoneConfigurationManager.getModemService();
11923 Log.d(LOG_TAG, "result = " + result);
11924 return result;
11925 }
Hunter Knepshield2b076fa2022-01-19 02:26:22 -080011926
11927 @Override
11928 public void setVoiceServiceStateOverride(int subId, boolean hasService, String callingPackage) {
11929 // Only telecom (and shell, for CTS purposes) is allowed to call this method.
11930 mApp.enforceCallingOrSelfPermission(
11931 permission.BIND_TELECOM_CONNECTION_SERVICE, "setVoiceServiceStateOverride");
11932 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
11933
11934 final long identity = Binder.clearCallingIdentity();
11935 try {
11936 Phone phone = getPhone(subId);
11937 if (phone == null) return;
Grant Menke63ade122023-01-20 14:31:54 -080011938 Log.i(LOG_TAG, "setVoiceServiceStateOverride: subId=" + subId + ", phone=" + phone
11939 + ", hasService=" + hasService + ", callingPackage=" + callingPackage);
Hunter Knepshield2b076fa2022-01-19 02:26:22 -080011940 phone.setVoiceServiceStateOverride(hasService);
11941 } finally {
11942 Binder.restoreCallingIdentity(identity);
11943 }
11944 }
Muralidhar Reddy4e5a8012022-05-11 14:49:00 +000011945
11946 /**
11947 * set removable eSIM as default eUICC.
11948 *
11949 * @hide
11950 */
11951 @Override
11952 public void setRemovableEsimAsDefaultEuicc(boolean isDefault, String callingPackage) {
11953 enforceModifyPermission();
11954 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
11955
11956 final long identity = Binder.clearCallingIdentity();
11957 try {
11958 UiccController.getInstance().setRemovableEsimAsDefaultEuicc(isDefault);
11959 } finally {
11960 Binder.restoreCallingIdentity(identity);
11961 }
11962 }
11963
11964 /**
11965 * Returns whether the removable eSIM is default eUICC or not.
11966 *
11967 * @hide
11968 */
11969 @Override
11970 public boolean isRemovableEsimDefaultEuicc(String callingPackage) {
11971 enforceReadPrivilegedPermission("isRemovableEsimDefaultEuicc");
11972 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
11973
11974 final long identity = Binder.clearCallingIdentity();
11975 try {
11976 return UiccController.getInstance().isRemovableEsimDefaultEuicc();
11977 } finally {
11978 Binder.restoreCallingIdentity(identity);
11979 }
11980 }
11981
Aishwarya Mallampatifbc70d32022-11-10 20:33:02 +000011982 /**
11983 * Get the component name of the default app to direct respond-via-message intent for the
11984 * user associated with this subscription, update the cache if there is no respond-via-message
11985 * application currently configured for this user.
11986 * @return component name of the app and class to direct Respond Via Message intent to, or
11987 * {@code null} if the functionality is not supported.
11988 * @hide
11989 */
11990 @Override
11991 public @Nullable ComponentName getDefaultRespondViaMessageApplication(int subId,
11992 boolean updateIfNeeded) {
11993 enforceInteractAcrossUsersPermission("getDefaultRespondViaMessageApplication");
Muralidhar Reddy4e5a8012022-05-11 14:49:00 +000011994
Aishwarya Mallampati5e581e12023-01-17 21:57:06 +000011995 Context context = getPhoneFromSubIdOrDefault(subId).getContext();
11996
Aishwarya Mallampatifbc70d32022-11-10 20:33:02 +000011997 UserHandle userHandle = null;
11998 final long identity = Binder.clearCallingIdentity();
11999 try {
12000 userHandle = TelephonyUtils.getSubscriptionUserHandle(context, subId);
12001 } finally {
12002 Binder.restoreCallingIdentity(identity);
12003 }
12004 return SmsApplication.getDefaultRespondViaMessageApplicationAsUser(context,
12005 updateIfNeeded, userHandle);
12006 }
Jack Yuf5badd92022-12-08 00:50:53 -080012007
12008 /**
Gil Cukierman1c0eb932022-12-06 22:28:24 +000012009 * Set whether the device is able to connect with null ciphering or integrity
12010 * algorithms. This is a global setting and will apply to all active subscriptions
12011 * and all new subscriptions after this.
12012 *
12013 * @param enabled when true, null cipher and integrity algorithms are allowed.
12014 * @hide
12015 */
12016 @Override
12017 @RequiresPermission(android.Manifest.permission.MODIFY_PHONE_STATE)
12018 public void setNullCipherAndIntegrityEnabled(boolean enabled) {
12019 enforceModifyPermission();
12020 checkForNullCipherAndIntegritySupport();
12021
12022 // Persist the state of our preference. Each GsmCdmaPhone instance is responsible
12023 // for listening to these preference changes and applying them immediately.
12024 SharedPreferences.Editor editor = mTelephonySharedPreferences.edit();
12025 editor.putBoolean(Phone.PREF_NULL_CIPHER_AND_INTEGRITY_ENABLED, enabled);
12026 editor.apply();
12027
12028 for (Phone phone: PhoneFactory.getPhones()) {
12029 phone.handleNullCipherEnabledChange();
12030 }
12031 }
12032
12033
12034 /**
12035 * Get whether the device is able to connect with null ciphering or integrity
12036 * algorithms. Note that this retrieves the phone-global preference and not
12037 * the state of the radio.
12038 *
12039 * @throws SecurityException if {@link permission#MODIFY_PHONE_STATE} is not satisfied
12040 * @throws UnsupportedOperationException if the device does not support the minimum HAL
12041 * version for this feature.
12042 * @hide
12043 */
12044 @Override
12045 @RequiresPermission(android.Manifest.permission.READ_PHONE_STATE)
12046 public boolean isNullCipherAndIntegrityPreferenceEnabled() {
12047 enforceReadPermission();
12048 checkForNullCipherAndIntegritySupport();
12049 return getDefaultPhone().getNullCipherAndIntegrityEnabledPreference();
12050 }
12051
12052 private void checkForNullCipherAndIntegritySupport() {
12053 if (getHalVersion(HAL_SERVICE_NETWORK) < MIN_NULL_CIPHER_AND_INTEGRITY_VERSION) {
12054 throw new UnsupportedOperationException(
12055 "Null cipher and integrity operations require HAL 2.1 or above");
12056 }
Gil Cukierman92cc7db2023-01-06 19:25:53 +000012057 if (!getDefaultPhone().isNullCipherAndIntegritySupported()) {
12058 throw new UnsupportedOperationException(
12059 "Null cipher and integrity operations unsupported by modem");
12060 }
Gil Cukierman1c0eb932022-12-06 22:28:24 +000012061 }
12062
12063 /**
Jack Yuf5badd92022-12-08 00:50:53 -080012064 * Get the SIM state for the slot index.
12065 * For Remote-SIMs, this method returns {@link IccCardConstants.State#UNKNOWN}
12066 *
12067 * @return SIM state as the ordinal of {@link IccCardConstants.State}
12068 */
12069 @Override
12070 @SimState
12071 public int getSimStateForSlotIndex(int slotIndex) {
12072 IccCardConstants.State simState;
12073 if (slotIndex < 0) {
12074 simState = IccCardConstants.State.UNKNOWN;
12075 } else {
12076 Phone phone = null;
12077 try {
12078 phone = PhoneFactory.getPhone(slotIndex);
12079 } catch (IllegalStateException e) {
12080 // ignore
12081 }
12082 if (phone == null) {
12083 simState = IccCardConstants.State.UNKNOWN;
12084 } else {
12085 IccCard icc = phone.getIccCard();
12086 if (icc == null) {
12087 simState = IccCardConstants.State.UNKNOWN;
12088 } else {
12089 simState = icc.getState();
12090 }
12091 }
12092 }
12093 return simState.ordinal();
12094 }
Hui Wang9b5793a2022-12-05 14:38:06 -060012095
12096 /**
12097 * Get current cell broadcast ranges.
12098 */
12099 @Override
12100 @RequiresPermission(android.Manifest.permission.MODIFY_CELL_BROADCASTS)
12101 public List<CellBroadcastIdRange> getCellBroadcastIdRanges(int subId) {
12102 mApp.enforceCallingPermission(android.Manifest.permission.MODIFY_CELL_BROADCASTS,
12103 "getCellBroadcastIdRanges");
12104 final long identity = Binder.clearCallingIdentity();
12105 try {
12106 return getPhone(subId).getCellBroadcastIdRanges();
12107 } finally {
12108 Binder.restoreCallingIdentity(identity);
12109 }
12110 }
12111
12112 /**
12113 * Set reception of cell broadcast messages with the list of the given ranges
12114 *
12115 * @param ranges the list of {@link CellBroadcastIdRange} to be enabled
12116 */
12117 @Override
12118 @RequiresPermission(android.Manifest.permission.MODIFY_CELL_BROADCASTS)
12119 public void setCellBroadcastIdRanges(int subId, @NonNull List<CellBroadcastIdRange> ranges,
12120 @Nullable IIntegerConsumer callback) {
12121 mApp.enforceCallingPermission(android.Manifest.permission.MODIFY_CELL_BROADCASTS,
12122 "setCellBroadcastIdRanges");
12123 final long identity = Binder.clearCallingIdentity();
12124 try {
12125 Phone phone = getPhoneFromSubId(subId);
12126 if (DBG) {
12127 log("setCellBroadcastIdRanges for subId :" + subId + ", phone:" + phone);
12128 }
12129 phone.setCellBroadcastIdRanges(ranges, result -> {
12130 if (callback != null) {
12131 try {
12132 callback.accept(result);
12133 } catch (RemoteException e) {
12134 Log.w(LOG_TAG, "setCellBroadcastIdRanges: callback not available.");
12135 }
12136 }
12137 });
12138 } finally {
12139 Binder.restoreCallingIdentity(identity);
12140 }
12141 }
Hunsuk Choi42cc62a2022-10-16 06:03:40 +000012142
12143 /**
12144 * Returns whether the device supports the domain selection service.
12145 *
12146 * @return {@code true} if the device supports the domain selection service.
12147 */
12148 @Override
12149 public boolean isDomainSelectionSupported() {
12150 mApp.enforceCallingOrSelfPermission(Manifest.permission.READ_PRIVILEGED_PHONE_STATE,
12151 "isDomainSelectionSupported");
12152
12153 final long identity = Binder.clearCallingIdentity();
12154 try {
12155 return DomainSelectionResolver.getInstance().isDomainSelectionSupported();
12156 } finally {
12157 Binder.restoreCallingIdentity(identity);
12158 }
12159 }
arunvoddud5c6ce02022-12-11 06:03:12 +000012160
12161 /**
Sarah Chineccfbd12023-01-20 19:00:35 -080012162 * Start receiving satellite position updates.
12163 * This can be called by the pointing UI when the user starts pointing to the satellite.
12164 * Modem should continue to report the pointing input as the device or satellite moves.
12165 *
12166 * @param subId The subId to start satellite position updates for.
12167 * @param callbackId The callback ID associating the public SatellitePositionUpdateCallback to
12168 * the internal ISatellitePositionUpdateCallback below.
12169 * @param callback The callback to notify of changes in satellite position.
12170 * @return The result of the operation.
12171 */
12172 @Override
12173 @SatelliteManager.SatelliteServiceResult public int startSatellitePositionUpdates(int subId,
12174 int callbackId, @NonNull ISatellitePositionUpdateCallback callback) {
12175 // TODO: check for SATELLITE_COMMUNICATION permission
12176 Phone phone = getPhone(subId);
12177 if (phone == null) {
12178 loge("startSatellitePositionUpdates called with invalid subId: " + subId
12179 + ". Retrying with default phone.");
12180 phone = getDefaultPhone();
12181 if (phone == null) {
12182 loge("startSatellitePositionUpdates failed with no phone object.");
12183 return SatelliteManager.SATELLITE_SERVICE_REQUEST_FAILED;
12184 }
12185 }
12186
12187 if (mSatellitePositionUpdateHandlers.containsKey(callbackId)) {
12188 log("startSatellitePositionUpdates: callback already registered: " + callbackId);
12189 return SatelliteManager.SATELLITE_SERVICE_SUCCESS;
12190 }
12191
12192 SatellitePositionUpdateHandler handler =
12193 new SatellitePositionUpdateHandler(callback, Looper.getMainLooper());
12194 phone.registerForSatellitePointingInfoChanged(handler,
12195 SatellitePositionUpdateHandler.EVENT_POSITION_UPDATE, null);
12196 phone.registerForSatelliteMessagesTransferComplete(handler,
12197 SatellitePositionUpdateHandler.EVENT_MESSAGE_TRANSFER_STATE_UPDATE, null);
12198 mSatellitePositionUpdateHandlers.put(callbackId, handler);
12199
12200 int result = (int) sendRequest(CMD_START_SATELLITE_POSITION_UPDATES, null, subId);
12201 if (DBG) log("startSatellitePositionUpdates result: " + result);
12202 return result;
12203 }
12204
12205 /**
12206 * Stop receiving satellite position updates.
12207 * This can be called by the pointing UI when the user stops pointing to the satellite.
12208 *
12209 * @param subId The subId to stop satellite position updates for.
12210 * @param callbackId The ID of the callback that was passed in {@link
12211 * #startSatellitePositionUpdates(int, int, ISatellitePositionUpdateCallback)}
12212 * @return The result of the operation.
12213 */
12214 @Override
12215 @SatelliteManager.SatelliteServiceResult public int stopSatellitePositionUpdates(int subId,
12216 int callbackId) {
12217 // TODO: check for SATELLITE_COMMUNICATION permission
12218 Phone phone = getPhone(subId);
12219 if (phone == null) {
12220 loge("stopSatellitePositionUpdates called with invalid subId: " + subId
12221 + ". Retrying with default phone.");
12222 phone = getDefaultPhone();
12223 if (phone == null) {
12224 loge("stopSatellitePositionUpdates failed with no phone object.");
12225 return SatelliteManager.SATELLITE_SERVICE_REQUEST_FAILED;
12226 }
12227 }
12228
12229 SatellitePositionUpdateHandler handler =
12230 mSatellitePositionUpdateHandlers.remove(callbackId);
12231 if (handler == null) {
12232 loge("stopSatellitePositionUpdates: No SatellitePositionArgument");
12233 return SatelliteManager.SATELLITE_SERVICE_REQUEST_FAILED;
12234 } else {
12235 phone.unregisterForSatellitePointingInfoChanged(handler);
12236 phone.unregisterForSatelliteMessagesTransferComplete(handler);
12237 }
12238
12239 if (!mSatellitePositionUpdateHandlers.isEmpty()) {
12240 log("stopSatellitePositionUpdates: other listeners still exist.");
12241 return SatelliteManager.SATELLITE_SERVICE_SUCCESS;
12242 }
12243
12244 int result = (int) sendRequest(CMD_STOP_SATELLITE_POSITION_UPDATES, null, subId);
12245 if (DBG) log("stopSatellitePositionUpdates result: " + result);
12246 return result;
12247 }
12248
12249 /**
arunvoddud5c6ce02022-12-11 06:03:12 +000012250 * Check whether the caller (or self, if not processing an IPC) can read device identifiers.
12251 *
12252 * <p>This method behaves in one of the following ways:
12253 * <ul>
12254 * <li>return true : if the calling package has the appop permission {@link
12255 * Manifest.permission#USE_ICC_AUTH_WITH_DEVICE_IDENTIFIER} in the manifest </>
12256 * <li>return true : if any one subscription has the READ_PRIVILEGED_PHONE_STATE
12257 * permission, the calling package passes a DevicePolicyManager Device Owner / Profile
12258 * Owner device identifier access check, or the calling package has carrier privileges</>
12259 * <li>throw SecurityException: if the caller does not meet any of the requirements.
12260 * </ul>
12261 */
12262 private static boolean checkCallingOrSelfReadDeviceIdentifiersForAnySub(Context context,
12263 String callingPackage, @Nullable String callingFeatureId, String message) {
12264 for (Phone phone : PhoneFactory.getPhones()) {
12265 if (TelephonyPermissions.checkCallingOrSelfReadDeviceIdentifiers(context,
12266 phone.getSubId(), callingPackage, callingFeatureId, message)) {
12267 return true;
12268 }
12269 }
12270 return false;
12271 }
arunvoddud7401012022-12-15 16:08:12 +000012272
12273 /**
12274 * Class binds the consumer[callback] and carrierId.
12275 */
12276 private static class CallerCallbackInfo {
12277 private final Consumer<Integer> mConsumer;
12278 private final int mCarrierId;
12279
12280 public CallerCallbackInfo(Consumer<Integer> consumer, int carrierId) {
12281 mConsumer = consumer;
12282 mCarrierId = carrierId;
12283 }
12284
12285 public Consumer<Integer> getConsumer() {
12286 return mConsumer;
12287 }
12288
12289 public int getCarrierId() {
12290 return mCarrierId;
12291 }
12292 }
arunvoddud5c6ce02022-12-11 06:03:12 +000012293}