blob: 14182e0144470916266b8c85891cdad9b4695133 [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;
20
Shuo Qianccbaf742021-02-22 18:32:21 -080021import static com.android.internal.telephony.PhoneConstants.PHONE_TYPE_CDMA;
22import static com.android.internal.telephony.PhoneConstants.PHONE_TYPE_GSM;
Tyler Gunn7bcdc742019-10-04 15:56:59 -070023import static com.android.internal.telephony.PhoneConstants.PHONE_TYPE_IMS;
Ta-wei Yen87c49842016-05-13 21:19:52 -070024import static com.android.internal.telephony.PhoneConstants.SUBSCRIPTION_KEY;
joonhunshin3e154242021-09-17 06:33:39 +000025import static com.android.internal.telephony.TelephonyStatsLog.RCS_CLIENT_PROVISIONING_STATS__EVENT__CLIENT_PARAMS_SENT;
Ta-wei Yen87c49842016-05-13 21:19:52 -070026
Brad Ebinger34c09a52021-02-17 23:23:21 +000027import android.Manifest;
Ta-wei Yen30a69c82016-12-27 14:52:32 -080028import android.Manifest.permission;
Hall Liua1548bd2019-12-24 14:14:12 -080029import android.annotation.NonNull;
Tyler Gunnf70ed162019-04-03 15:28:53 -070030import android.annotation.Nullable;
sandeepjsb6c87872021-09-27 15:34:44 +000031import android.annotation.RequiresPermission;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070032import android.app.AppOpsManager;
Ta-wei Yen30a69c82016-12-27 14:52:32 -080033import android.app.PendingIntent;
Brad Ebinger4f6208e2021-03-23 21:04:45 +000034import android.app.compat.CompatChanges;
Hall Liu82694d52020-12-11 18:22:04 -080035import android.app.role.RoleManager;
Chen Xu540470b2021-12-14 17:15:47 -080036import android.compat.Compatibility;
sandeepjsb6c87872021-09-27 15:34:44 +000037import android.compat.annotation.ChangeId;
38import android.compat.annotation.EnabledSince;
Sailesh Nepalbd76e4e2013-10-27 13:59:44 -070039import android.content.ComponentName;
Amit Mahajan7dbbd822019-03-13 17:33:47 -070040import android.content.ContentResolver;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070041import android.content.Context;
42import android.content.Intent;
Derek Tan97ebb422014-09-05 16:55:38 -070043import android.content.SharedPreferences;
Derek Tan740e1672017-06-27 14:56:27 -070044import android.content.pm.ComponentInfo;
Amith Yamasani6e118872016-02-19 12:53:51 -080045import android.content.pm.PackageInfo;
Shishir Agrawal60f9c952014-06-23 12:00:43 -070046import android.content.pm.PackageManager;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070047import android.net.Uri;
48import android.os.AsyncResult;
49import android.os.Binder;
Hall Liuf19c44f2018-11-27 14:38:17 -080050import android.os.Build;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070051import android.os.Bundle;
52import android.os.Handler;
yinxu504e1392017-04-12 16:03:22 -070053import android.os.IBinder;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070054import android.os.Looper;
55import android.os.Message;
yinxu504e1392017-04-12 16:03:22 -070056import android.os.Messenger;
Hall Liua1548bd2019-12-24 14:14:12 -080057import android.os.ParcelFileDescriptor;
Malcolm Chen6ca97372019-07-01 16:28:21 -070058import android.os.ParcelUuid;
Tyler Gunn65d45c22017-06-05 11:22:26 -070059import android.os.PersistableBundle;
Shuo Qiancd19c462020-01-16 20:51:11 -080060import android.os.Process;
Brad Ebinger5f64b052017-12-14 14:26:15 -080061import android.os.RemoteException;
Adam Lesinski903a54c2016-04-11 14:49:52 -070062import android.os.ResultReceiver;
Brad Ebinger1ce9c432019-07-16 13:19:44 -070063import android.os.ServiceSpecificException;
Rambo Wang0f050d82021-02-12 11:43:36 -080064import android.os.SystemClock;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070065import android.os.UserHandle;
Stuart Scott981d8582015-04-21 14:09:50 -070066import android.os.UserManager;
Sooraj Sasindran9a909312016-07-18 11:57:25 -070067import android.os.WorkSource;
Derek Tan97ebb422014-09-05 16:55:38 -070068import android.preference.PreferenceManager;
Naina Nallurid63128d2019-09-17 14:10:30 -070069import android.provider.DeviceConfig;
Ihab Awadf2177b72013-11-25 13:33:23 -080070import android.provider.Settings;
Amit Mahajan7dbbd822019-03-13 17:33:47 -070071import android.provider.Telephony;
Inseob Kim14bb3d02018-12-13 17:11:34 +090072import android.sysprop.TelephonyProperties;
Santos Cordon7a1885b2015-02-03 11:15:19 -080073import android.telecom.PhoneAccount;
Nancy Chen31f9ba12016-01-06 11:42:12 -080074import android.telecom.PhoneAccountHandle;
Andrew Lee9431b832015-03-09 18:46:45 -070075import android.telecom.TelecomManager;
Chen Xu227e06f2019-09-26 22:48:11 -070076import android.telephony.Annotation.ApnType;
Jack Nudelmanb0b87642020-11-12 15:04:39 -080077import android.telephony.Annotation.ThermalMitigationResult;
Shuo Qian4a594052020-01-23 11:59:30 -080078import android.telephony.CallForwardingInfo;
Junda Liu12f7d802015-05-01 12:06:44 -070079import android.telephony.CarrierConfigManager;
Michele Berionne482f8202018-11-27 18:57:59 -080080import android.telephony.CarrierRestrictionRules;
yincheng zhao2737e882019-09-06 17:06:54 -070081import android.telephony.CellIdentity;
Meng Wanga10e89e2019-12-09 13:13:01 -080082import android.telephony.CellIdentityCdma;
83import android.telephony.CellIdentityGsm;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070084import android.telephony.CellInfo;
Nathan Haroldf180aac2018-06-01 18:43:55 -070085import android.telephony.CellInfoGsm;
86import android.telephony.CellInfoWcdma;
Sooraj Sasindran9a909312016-07-18 11:57:25 -070087import android.telephony.ClientRequestStats;
Jack Nudelmanb0b87642020-11-12 15:04:39 -080088import android.telephony.DataThrottlingRequest;
Hui Wang641e81c2020-10-12 12:14:23 -070089import android.telephony.IBootstrapAuthenticationCallback;
Nathan Haroldfa8da0f2018-09-27 18:51:29 -070090import android.telephony.ICellInfoCallback;
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -070091import android.telephony.IccOpenLogicalChannelResponse;
Hall Liu1aa510f2017-11-22 17:40:08 -080092import android.telephony.LocationAccessPolicy;
Ta-wei Yen87c49842016-05-13 21:19:52 -070093import android.telephony.ModemActivityInfo;
Jake Hambye994d462014-02-03 13:10:13 -080094import android.telephony.NeighboringCellInfo;
yinxu504e1392017-04-12 16:03:22 -070095import android.telephony.NetworkScanRequest;
Michele4245e952019-02-04 11:36:23 -080096import android.telephony.PhoneCapability;
Hall Liud892bec2018-11-30 14:51:45 -080097import android.telephony.PhoneNumberRange;
Wink Saville5d475dd2014-10-17 15:00:58 -070098import android.telephony.RadioAccessFamily;
Hall Liub2ac8ef2019-02-28 15:56:23 -080099import android.telephony.RadioAccessSpecifier;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700100import android.telephony.ServiceState;
Nathan Harold46b42aa2017-03-10 19:38:22 -0800101import android.telephony.SignalStrength;
Rambo Wanga5cc9b72021-01-07 10:51:54 -0800102import android.telephony.SignalStrengthUpdateRequest;
103import android.telephony.SignalThresholdInfo;
Wink Saville0f3b5fc2014-11-11 08:40:49 -0800104import android.telephony.SubscriptionInfo;
Jeff Sharkey85190e62014-12-05 09:40:12 -0800105import android.telephony.SubscriptionManager;
Peter Wangc035ce42020-01-08 21:00:22 -0800106import android.telephony.TelephonyFrameworkInitializer;
Sanket Padawe99ef1e32016-05-18 16:12:33 -0700107import android.telephony.TelephonyHistogram;
Ta-wei Yenb6929602016-05-24 15:48:27 -0700108import android.telephony.TelephonyManager;
Hall Liub2ac8ef2019-02-28 15:56:23 -0800109import android.telephony.TelephonyScanManager;
Jack Nudelmanb0b87642020-11-12 15:04:39 -0800110import android.telephony.ThermalMitigationRequest;
Jordan Liu5aa07002018-12-18 15:44:48 -0800111import android.telephony.UiccCardInfo;
sandeepjsb6c87872021-09-27 15:34:44 +0000112import android.telephony.UiccPortInfo;
Holly Jiuyu Sun01c47ad2018-01-24 17:56:33 +0000113import android.telephony.UiccSlotInfo;
sandeepjsb6c87872021-09-27 15:34:44 +0000114import android.telephony.UiccSlotMapping;
Tyler Gunn65d45c22017-06-05 11:22:26 -0700115import android.telephony.UssdResponse;
Ta-wei Yenb6929602016-05-24 15:48:27 -0700116import android.telephony.VisualVoicemailSmsFilterSettings;
Jack Yub5d8f642018-11-26 11:20:48 -0800117import android.telephony.data.ApnSetting;
Hongbo Zeng0e18b162021-04-07 16:52:18 +0800118import android.telephony.data.NetworkSlicingConfig;
Jack Yub5d8f642018-11-26 11:20:48 -0800119import android.telephony.emergency.EmergencyNumber;
Hui Wang641e81c2020-10-12 12:14:23 -0700120import android.telephony.gba.GbaAuthRequest;
121import android.telephony.gba.UaSecurityProtocolIdentifier;
Brad Ebinger1ce9c432019-07-16 13:19:44 -0700122import android.telephony.ims.ImsException;
Brad Ebinger1c8542e2019-01-14 13:43:14 -0800123import android.telephony.ims.ProvisioningManager;
Hui Wang761a6682020-10-31 05:12:53 +0000124import android.telephony.ims.RcsClientConfiguration;
Brad Ebinger14d467f2021-02-12 06:18:28 +0000125import android.telephony.ims.RcsContactUceCapability;
Brad Ebingera34a6c22019-10-22 17:36:18 -0700126import android.telephony.ims.RegistrationManager;
Brad Ebinger35c841c2018-10-01 10:40:55 -0700127import android.telephony.ims.aidl.IImsCapabilityCallback;
Brad Ebinger22bc3e42018-01-16 09:39:35 -0800128import android.telephony.ims.aidl.IImsConfig;
Brad Ebingerdf5b4f02018-10-31 11:24:17 -0700129import android.telephony.ims.aidl.IImsConfigCallback;
Brad Ebinger22bc3e42018-01-16 09:39:35 -0800130import android.telephony.ims.aidl.IImsRegistration;
Brad Ebinger35c841c2018-10-01 10:40:55 -0700131import android.telephony.ims.aidl.IImsRegistrationCallback;
Hui Wang761a6682020-10-31 05:12:53 +0000132import android.telephony.ims.aidl.IRcsConfigCallback;
Brad Ebingerbc7dd582019-10-17 17:03:22 -0700133import android.telephony.ims.feature.ImsFeature;
Brad Ebinger1c8542e2019-01-14 13:43:14 -0800134import android.telephony.ims.feature.MmTelFeature;
allenwtsu99c623b2020-01-03 18:24:23 +0800135import android.telephony.ims.feature.RcsFeature;
Brad Ebinger1c8542e2019-01-14 13:43:14 -0800136import android.telephony.ims.stub.ImsConfigImplBase;
Brad Ebinger1f2b5082018-02-08 16:11:32 -0800137import android.telephony.ims.stub.ImsRegistrationImplBase;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700138import android.text.TextUtils;
Jeff Sharkey85190e62014-12-05 09:40:12 -0800139import android.util.ArraySet;
Hall Liud60acc92020-05-21 17:09:35 -0700140import android.util.EventLog;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700141import android.util.Log;
Jake Hambye994d462014-02-03 13:10:13 -0800142import android.util.Pair;
Ta-wei Yen30a69c82016-12-27 14:52:32 -0800143
Andrew Lee312e8172014-10-23 17:01:36 -0700144import com.android.ims.ImsManager;
Brad Ebinger34bef922017-11-09 10:27:08 -0800145import com.android.ims.internal.IImsServiceFeatureCallback;
James.cf Linbcdf8b32021-01-14 16:44:13 +0800146import com.android.ims.rcs.uce.eab.EabUtil;
SongFerngWangfd89b102021-05-27 22:44:54 +0800147import com.android.internal.annotations.VisibleForTesting;
Shuo Qian4a594052020-01-23 11:59:30 -0800148import com.android.internal.telephony.CallForwardInfo;
Shishir Agrawal566b7612013-10-28 14:41:00 -0700149import com.android.internal.telephony.CallManager;
Tyler Gunn52dcf772017-04-26 11:30:31 -0700150import com.android.internal.telephony.CallStateException;
Tyler Gunnd4339262021-05-03 14:46:49 -0700151import com.android.internal.telephony.CallTracker;
Rambo Wang9c9ffdd2022-01-13 21:51:44 -0800152import com.android.internal.telephony.CarrierPrivilegesTracker;
chen xu651eec72018-11-11 19:03:44 -0800153import com.android.internal.telephony.CarrierResolver;
Shishir Agrawal302c8692015-06-19 13:49:39 -0700154import com.android.internal.telephony.CellNetworkScanResult;
Shishir Agrawal566b7612013-10-28 14:41:00 -0700155import com.android.internal.telephony.CommandException;
Shuo Qian4a594052020-01-23 11:59:30 -0800156import com.android.internal.telephony.CommandsInterface;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700157import com.android.internal.telephony.DefaultPhoneNotifier;
Hui Wang641e81c2020-10-12 12:14:23 -0700158import com.android.internal.telephony.GbaManager;
Shuo Qianccbaf742021-02-22 18:32:21 -0800159import com.android.internal.telephony.GsmCdmaPhone;
Nathan Harold48d6fd52019-02-06 19:01:40 -0800160import com.android.internal.telephony.HalVersion;
Hall Liu73f5d362020-01-20 13:42:00 -0800161import com.android.internal.telephony.IBooleanConsumer;
Hall Liu27d24262020-09-18 19:04:59 -0700162import com.android.internal.telephony.ICallForwardingInfoCallback;
Hunsuk Choi3b742d62021-10-25 19:48:34 +0000163import com.android.internal.telephony.IImsStateCallback;
Brad Ebingera63db5f2019-04-23 16:31:13 -0700164import com.android.internal.telephony.IIntegerConsumer;
Hall Liud892bec2018-11-30 14:51:45 -0800165import com.android.internal.telephony.INumberVerificationCallback;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700166import com.android.internal.telephony.ITelephony;
Jake Hambye994d462014-02-03 13:10:13 -0800167import com.android.internal.telephony.IccCard;
Rambo Wanga1782702021-11-10 20:15:19 -0800168import com.android.internal.telephony.IccLogicalChannelRequest;
Jack Yu5f7092c2018-04-13 14:05:37 -0700169import com.android.internal.telephony.LocaleTracker;
yinxub1bed742017-04-17 11:45:04 -0700170import com.android.internal.telephony.NetworkScanRequestTracker;
Shishir Agrawal302c8692015-06-19 13:49:39 -0700171import com.android.internal.telephony.OperatorInfo;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700172import com.android.internal.telephony.Phone;
Malcolm Chen2c63d402018-08-14 16:00:53 -0700173import com.android.internal.telephony.PhoneConfigurationManager;
Nathan Harolda667c152016-12-14 11:27:20 -0800174import com.android.internal.telephony.PhoneConstantConversions;
Ta-wei Yen87c49842016-05-13 21:19:52 -0700175import com.android.internal.telephony.PhoneConstants;
Wink Saville36469e72014-06-11 15:17:00 -0700176import com.android.internal.telephony.PhoneFactory;
Wink Saville5d475dd2014-10-17 15:00:58 -0700177import com.android.internal.telephony.ProxyController;
Sanket Padawe99ef1e32016-05-18 16:12:33 -0700178import com.android.internal.telephony.RIL;
SongFerngWang8c6e82e2021-03-02 22:09:29 +0800179import com.android.internal.telephony.RILConstants;
Daniel Bright94f43662021-03-01 14:43:40 -0800180import com.android.internal.telephony.RadioInterfaceCapabilityController;
Jack Yu5f7092c2018-04-13 14:05:37 -0700181import com.android.internal.telephony.ServiceStateTracker;
Amit Mahajandccb3f12019-05-13 13:48:32 -0700182import com.android.internal.telephony.SmsController;
Brad Ebingera63db5f2019-04-23 16:31:13 -0700183import com.android.internal.telephony.SmsPermissions;
Wink Savilleac1bdfd2014-11-20 23:04:44 -0800184import com.android.internal.telephony.SubscriptionController;
Peter Wang59571be2020-01-27 12:35:15 +0800185import com.android.internal.telephony.TelephonyIntents;
Jeff Davidson7e17e312018-02-13 18:17:36 -0800186import com.android.internal.telephony.TelephonyPermissions;
Malcolm Chendc8c10e2019-04-10 18:25:07 -0700187import com.android.internal.telephony.dataconnection.ApnSettingUtils;
sqianf4ca7ed2019-01-15 18:32:07 -0800188import com.android.internal.telephony.emergency.EmergencyNumberTracker;
Derek Tan740e1672017-06-27 14:56:27 -0700189import com.android.internal.telephony.euicc.EuiccConnector;
Brad Ebinger9c0eb502019-01-23 15:06:19 -0800190import com.android.internal.telephony.ims.ImsResolver;
Tyler Gunn7bcdc742019-10-04 15:56:59 -0700191import com.android.internal.telephony.imsphone.ImsPhone;
192import com.android.internal.telephony.imsphone.ImsPhoneCallTracker;
joonhunshin3e154242021-09-17 06:33:39 +0000193import com.android.internal.telephony.metrics.RcsStats;
Pengquan Meng6c2dc9f2019-02-06 11:12:53 -0800194import com.android.internal.telephony.metrics.TelephonyMetrics;
Meng Wangafbc5852019-09-19 17:37:13 -0700195import com.android.internal.telephony.uicc.IccCardApplicationStatus.AppType;
Shishir Agrawal566b7612013-10-28 14:41:00 -0700196import com.android.internal.telephony.uicc.IccIoResult;
changbetty7157e9e2019-12-06 18:16:37 +0800197import com.android.internal.telephony.uicc.IccRecords;
Shishir Agrawal566b7612013-10-28 14:41:00 -0700198import com.android.internal.telephony.uicc.IccUtils;
Nathan Haroldb3014052017-01-25 15:57:32 -0800199import com.android.internal.telephony.uicc.SIMRecords;
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700200import com.android.internal.telephony.uicc.UiccCard;
Nathan Haroldb3014052017-01-25 15:57:32 -0800201import com.android.internal.telephony.uicc.UiccCardApplication;
Shishir Agrawal566b7612013-10-28 14:41:00 -0700202import com.android.internal.telephony.uicc.UiccController;
Muralidhar Reddy472c2ae2021-09-29 19:38:40 +0000203import com.android.internal.telephony.uicc.UiccPort;
Jeff Davidson7e17e312018-02-13 18:17:36 -0800204import com.android.internal.telephony.uicc.UiccProfile;
Holly Jiuyu Sun01c47ad2018-01-24 17:56:33 +0000205import com.android.internal.telephony.uicc.UiccSlot;
zoey chenc730df82019-12-18 17:07:20 +0800206import com.android.internal.telephony.util.LocaleUtils;
fionaxu7ed723d2017-05-30 18:58:54 -0700207import com.android.internal.telephony.util.VoicemailNotificationSettingsUtil;
Hall Liu27d24262020-09-18 19:04:59 -0700208import com.android.internal.util.FunctionalUtils;
Jake Hambye994d462014-02-03 13:10:13 -0800209import com.android.internal.util.HexDump;
Hall Liuaa4211e2021-01-20 15:43:39 -0800210import com.android.phone.callcomposer.CallComposerPictureManager;
211import com.android.phone.callcomposer.CallComposerPictureTransfer;
212import com.android.phone.callcomposer.ImageData;
Brad Ebingera63db5f2019-04-23 16:31:13 -0700213import com.android.phone.settings.PickSmsSubscriptionActivity;
Ta-wei Yenc9df0432017-04-17 17:09:07 -0700214import com.android.phone.vvm.PhoneAccountHandleConverter;
Ta-wei Yen527a9c02017-01-06 15:29:25 -0800215import com.android.phone.vvm.RemoteVvmTaskManager;
Ta-wei Yenc9df0432017-04-17 17:09:07 -0700216import com.android.phone.vvm.VisualVoicemailSettingsUtil;
Ta-wei Yenc8905312017-03-28 11:14:45 -0700217import com.android.phone.vvm.VisualVoicemailSmsFilterConfig;
Jack Nudelmanb0b87642020-11-12 15:04:39 -0800218import com.android.services.telephony.TelecomAccountRegistry;
219import com.android.services.telephony.TelephonyConnectionService;
Peter Wang44b186e2020-01-13 23:33:09 -0800220import com.android.telephony.Rlog;
Ta-wei Yen30a69c82016-12-27 14:52:32 -0800221
Hall Liu82694d52020-12-11 18:22:04 -0800222import java.io.ByteArrayOutputStream;
Ta-wei Yenc236d6b2016-06-21 13:33:12 -0700223import java.io.FileDescriptor;
Hall Liu82694d52020-12-11 18:22:04 -0800224import java.io.IOException;
225import java.io.InputStream;
Ta-wei Yenc236d6b2016-06-21 13:33:12 -0700226import java.io.PrintWriter;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700227import java.util.ArrayList;
Jeff Sharkey85190e62014-12-05 09:40:12 -0800228import java.util.Arrays;
Muralidhar Reddyeb809e32021-11-19 03:07:54 +0000229import java.util.Collection;
sqian11b7a0e2018-12-05 18:48:28 -0800230import java.util.HashMap;
sqianf4ca7ed2019-01-15 18:32:07 -0800231import java.util.HashSet;
Jake Hambye994d462014-02-03 13:10:13 -0800232import java.util.List;
Narayan Kamath1c496c22015-04-16 14:40:19 +0100233import java.util.Locale;
Jeff Sharkey85190e62014-12-05 09:40:12 -0800234import java.util.Map;
Nazanin Bakhshif71371d2019-04-29 17:29:44 -0700235import java.util.NoSuchElementException;
Hall Liu82694d52020-12-11 18:22:04 -0800236import java.util.Objects;
sqianf4ca7ed2019-01-15 18:32:07 -0800237import java.util.Set;
Hall Liu82694d52020-12-11 18:22:04 -0800238import java.util.concurrent.Executors;
Peter Wangdafb9ac2020-01-15 14:13:38 -0800239import java.util.concurrent.atomic.AtomicBoolean;
Hall Liu73f5d362020-01-20 13:42:00 -0800240import java.util.function.Consumer;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700241
242/**
243 * Implementation of the ITelephony interface.
244 */
Santos Cordon117fee72014-05-16 17:56:12 -0700245public class PhoneInterfaceManager extends ITelephony.Stub {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700246 private static final String LOG_TAG = "PhoneInterfaceManager";
247 private static final boolean DBG = (PhoneGlobals.DBG_LEVEL >= 2);
248 private static final boolean DBG_LOC = false;
Jeff Sharkey85190e62014-12-05 09:40:12 -0800249 private static final boolean DBG_MERGE = false;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700250
251 // Message codes used with mMainThreadHandler
252 private static final int CMD_HANDLE_PIN_MMI = 1;
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700253 private static final int CMD_TRANSMIT_APDU_LOGICAL_CHANNEL = 7;
254 private static final int EVENT_TRANSMIT_APDU_LOGICAL_CHANNEL_DONE = 8;
Shishir Agrawal566b7612013-10-28 14:41:00 -0700255 private static final int CMD_OPEN_CHANNEL = 9;
256 private static final int EVENT_OPEN_CHANNEL_DONE = 10;
257 private static final int CMD_CLOSE_CHANNEL = 11;
258 private static final int EVENT_CLOSE_CHANNEL_DONE = 12;
Jake Hambye994d462014-02-03 13:10:13 -0800259 private static final int CMD_NV_READ_ITEM = 13;
260 private static final int EVENT_NV_READ_ITEM_DONE = 14;
261 private static final int CMD_NV_WRITE_ITEM = 15;
262 private static final int EVENT_NV_WRITE_ITEM_DONE = 16;
263 private static final int CMD_NV_WRITE_CDMA_PRL = 17;
264 private static final int EVENT_NV_WRITE_CDMA_PRL_DONE = 18;
chen xu6dac5ab2018-10-26 17:39:23 -0700265 private static final int CMD_RESET_MODEM_CONFIG = 19;
266 private static final int EVENT_RESET_MODEM_CONFIG_DONE = 20;
SongFerngWang3ef3e072020-12-21 16:41:52 +0800267 private static final int CMD_GET_ALLOWED_NETWORK_TYPES_BITMASK = 21;
268 private static final int EVENT_GET_ALLOWED_NETWORK_TYPES_BITMASK_DONE = 22;
Sailesh Nepal35b59452014-03-06 09:26:56 -0800269 private static final int CMD_SEND_ENVELOPE = 25;
270 private static final int EVENT_SEND_ENVELOPE_DONE = 26;
Shuo Qian850e4d6a2018-04-25 21:02:08 +0000271 private static final int CMD_INVOKE_OEM_RIL_REQUEST_RAW = 27;
272 private static final int EVENT_INVOKE_OEM_RIL_REQUEST_RAW_DONE = 28;
Derek Tan6b088ee2014-09-05 14:15:18 -0700273 private static final int CMD_TRANSMIT_APDU_BASIC_CHANNEL = 29;
274 private static final int EVENT_TRANSMIT_APDU_BASIC_CHANNEL_DONE = 30;
275 private static final int CMD_EXCHANGE_SIM_IO = 31;
276 private static final int EVENT_EXCHANGE_SIM_IO_DONE = 32;
Shishir Agrawal76d5da92014-11-09 16:17:25 -0800277 private static final int CMD_SET_VOICEMAIL_NUMBER = 33;
278 private static final int EVENT_SET_VOICEMAIL_NUMBER_DONE = 34;
Stuart Scott54788802015-03-30 13:18:01 -0700279 private static final int CMD_SET_NETWORK_SELECTION_MODE_AUTOMATIC = 35;
280 private static final int EVENT_SET_NETWORK_SELECTION_MODE_AUTOMATIC_DONE = 36;
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -0700281 private static final int CMD_GET_MODEM_ACTIVITY_INFO = 37;
282 private static final int EVENT_GET_MODEM_ACTIVITY_INFO_DONE = 38;
Shishir Agrawal302c8692015-06-19 13:49:39 -0700283 private static final int CMD_PERFORM_NETWORK_SCAN = 39;
284 private static final int EVENT_PERFORM_NETWORK_SCAN_DONE = 40;
285 private static final int CMD_SET_NETWORK_SELECTION_MODE_MANUAL = 41;
286 private static final int EVENT_SET_NETWORK_SELECTION_MODE_MANUAL_DONE = 42;
Meng Wang1a7c35a2016-05-05 20:56:15 -0700287 private static final int CMD_SET_ALLOWED_CARRIERS = 43;
288 private static final int EVENT_SET_ALLOWED_CARRIERS_DONE = 44;
289 private static final int CMD_GET_ALLOWED_CARRIERS = 45;
290 private static final int EVENT_GET_ALLOWED_CARRIERS_DONE = 46;
pkanwar32d516d2016-10-14 19:37:38 -0700291 private static final int CMD_HANDLE_USSD_REQUEST = 47;
Nathan Haroldb3014052017-01-25 15:57:32 -0800292 private static final int CMD_GET_FORBIDDEN_PLMNS = 48;
293 private static final int EVENT_GET_FORBIDDEN_PLMNS_DONE = 49;
Holly Jiuyu Sun01c47ad2018-01-24 17:56:33 +0000294 private static final int CMD_SWITCH_SLOTS = 50;
295 private static final int EVENT_SWITCH_SLOTS_DONE = 51;
Pengquan Menga1bb6272018-09-06 09:59:22 -0700296 private static final int CMD_GET_NETWORK_SELECTION_MODE = 52;
297 private static final int EVENT_GET_NETWORK_SELECTION_MODE_DONE = 53;
298 private static final int CMD_GET_CDMA_ROAMING_MODE = 54;
299 private static final int EVENT_GET_CDMA_ROAMING_MODE_DONE = 55;
300 private static final int CMD_SET_CDMA_ROAMING_MODE = 56;
301 private static final int EVENT_SET_CDMA_ROAMING_MODE_DONE = 57;
302 private static final int CMD_SET_CDMA_SUBSCRIPTION_MODE = 58;
303 private static final int EVENT_SET_CDMA_SUBSCRIPTION_MODE_DONE = 59;
Nathan Harold3ff88932018-08-14 10:19:49 -0700304 private static final int CMD_GET_ALL_CELL_INFO = 60;
305 private static final int EVENT_GET_ALL_CELL_INFO_DONE = 61;
306 private static final int CMD_GET_CELL_LOCATION = 62;
307 private static final int EVENT_GET_CELL_LOCATION_DONE = 63;
chen xu6dac5ab2018-10-26 17:39:23 -0700308 private static final int CMD_MODEM_REBOOT = 64;
309 private static final int EVENT_CMD_MODEM_REBOOT_DONE = 65;
Nathan Haroldfa8da0f2018-09-27 18:51:29 -0700310 private static final int CMD_REQUEST_CELL_INFO_UPDATE = 66;
311 private static final int EVENT_REQUEST_CELL_INFO_UPDATE_DONE = 67;
Malcolm Chen8e4ed912019-01-15 20:22:16 -0800312 private static final int CMD_REQUEST_ENABLE_MODEM = 68;
313 private static final int EVENT_ENABLE_MODEM_DONE = 69;
Nazanin Bakhshif71371d2019-04-29 17:29:44 -0700314 private static final int CMD_GET_MODEM_STATUS = 70;
315 private static final int EVENT_GET_MODEM_STATUS_DONE = 71;
yincheng zhao2737e882019-09-06 17:06:54 -0700316 private static final int CMD_SET_FORBIDDEN_PLMNS = 72;
317 private static final int EVENT_SET_FORBIDDEN_PLMNS_DONE = 73;
Naina Nallurid63128d2019-09-17 14:10:30 -0700318 private static final int CMD_ERASE_MODEM_CONFIG = 74;
319 private static final int EVENT_ERASE_MODEM_CONFIG_DONE = 75;
zoey chene02881a2019-12-30 16:11:23 +0800320 private static final int CMD_CHANGE_ICC_LOCK_PASSWORD = 76;
321 private static final int EVENT_CHANGE_ICC_LOCK_PASSWORD_DONE = 77;
322 private static final int CMD_SET_ICC_LOCK_ENABLED = 78;
323 private static final int EVENT_SET_ICC_LOCK_ENABLED_DONE = 79;
Hall Liu73f5d362020-01-20 13:42:00 -0800324 private static final int CMD_SET_SYSTEM_SELECTION_CHANNELS = 80;
325 private static final int EVENT_SET_SYSTEM_SELECTION_CHANNELS_DONE = 81;
Peter Wangdafb9ac2020-01-15 14:13:38 -0800326 private static final int MSG_NOTIFY_USER_ACTIVITY = 82;
Shuo Qian4a594052020-01-23 11:59:30 -0800327 private static final int CMD_GET_CALL_FORWARDING = 83;
328 private static final int EVENT_GET_CALL_FORWARDING_DONE = 84;
329 private static final int CMD_SET_CALL_FORWARDING = 85;
330 private static final int EVENT_SET_CALL_FORWARDING_DONE = 86;
331 private static final int CMD_GET_CALL_WAITING = 87;
332 private static final int EVENT_GET_CALL_WAITING_DONE = 88;
333 private static final int CMD_SET_CALL_WAITING = 89;
334 private static final int EVENT_SET_CALL_WAITING_DONE = 90;
Sooraj Sasindran37444802020-08-11 10:40:43 -0700335 private static final int CMD_ENABLE_NR_DUAL_CONNECTIVITY = 91;
336 private static final int EVENT_ENABLE_NR_DUAL_CONNECTIVITY_DONE = 92;
337 private static final int CMD_IS_NR_DUAL_CONNECTIVITY_ENABLED = 93;
338 private static final int EVENT_IS_NR_DUAL_CONNECTIVITY_ENABLED_DONE = 94;
Sarah Chinbaab1432020-10-28 13:46:24 -0700339 private static final int CMD_GET_CDMA_SUBSCRIPTION_MODE = 95;
340 private static final int EVENT_GET_CDMA_SUBSCRIPTION_MODE_DONE = 96;
Sarah Chin679c08a2020-11-18 13:39:35 -0800341 private static final int CMD_GET_SYSTEM_SELECTION_CHANNELS = 97;
342 private static final int EVENT_GET_SYSTEM_SELECTION_CHANNELS_DONE = 98;
Jack Nudelmanb0b87642020-11-12 15:04:39 -0800343 private static final int CMD_SET_DATA_THROTTLING = 99;
344 private static final int EVENT_SET_DATA_THROTTLING_DONE = 100;
Jordan Liu109698e2020-11-24 14:50:34 -0800345 private static final int CMD_SET_SIM_POWER = 101;
346 private static final int EVENT_SET_SIM_POWER_DONE = 102;
Rambo Wanga5cc9b72021-01-07 10:51:54 -0800347 private static final int CMD_SET_SIGNAL_STRENGTH_UPDATE_REQUEST = 103;
348 private static final int EVENT_SET_SIGNAL_STRENGTH_UPDATE_REQUEST_DONE = 104;
349 private static final int CMD_CLEAR_SIGNAL_STRENGTH_UPDATE_REQUEST = 105;
350 private static final int EVENT_CLEAR_SIGNAL_STRENGTH_UPDATE_REQUEST_DONE = 106;
SongFerngWang3ef3e072020-12-21 16:41:52 +0800351 private static final int CMD_SET_ALLOWED_NETWORK_TYPES_FOR_REASON = 107;
352 private static final int EVENT_SET_ALLOWED_NETWORK_TYPES_FOR_REASON_DONE = 108;
Michele Berionne5e411512020-11-13 02:36:59 +0000353 private static final int CMD_PREPARE_UNATTENDED_REBOOT = 109;
Hongbo Zeng156aa4a2021-02-08 21:50:28 +0800354 private static final int CMD_GET_SLICING_CONFIG = 110;
355 private static final int EVENT_GET_SLICING_CONFIG_DONE = 111;
Kai Shif70f46f2021-03-03 13:59:46 -0800356 private static final int CMD_ERASE_DATA_SHARED_PREFERENCES = 112;
Sooraj Sasindrandaf060f2021-06-15 14:52:55 -0700357 private static final int CMD_ENABLE_VONR = 113;
358 private static final int EVENT_ENABLE_VONR_DONE = 114;
359 private static final int CMD_IS_VONR_ENABLED = 115;
360 private static final int EVENT_IS_VONR_ENABLED_DONE = 116;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700361
Holly Jiuyu Sun1cc2d552018-01-26 15:51:16 -0800362 // Parameters of select command.
363 private static final int SELECT_COMMAND = 0xA4;
364 private static final int SELECT_P1 = 0x04;
365 private static final int SELECT_P2 = 0;
366 private static final int SELECT_P3 = 0x10;
367
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700368 /** The singleton instance. */
369 private static PhoneInterfaceManager sInstance;
Jack Nudelman644b91a2021-03-12 14:09:48 -0800370 private static List<String> sThermalMitigationAllowlistedPackages = new ArrayList<>();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700371
Wink Saville3ab207e2014-11-20 13:07:20 -0800372 private PhoneGlobals mApp;
Wink Saville3ab207e2014-11-20 13:07:20 -0800373 private CallManager mCM;
Brad Ebinger24c29992019-12-05 13:03:21 -0800374 private ImsResolver mImsResolver;
Stuart Scott981d8582015-04-21 14:09:50 -0700375 private UserManager mUserManager;
Wink Saville3ab207e2014-11-20 13:07:20 -0800376 private AppOpsManager mAppOps;
Grace Jia0ddb3612021-04-22 13:35:26 -0700377 private PackageManager mPm;
Wink Saville3ab207e2014-11-20 13:07:20 -0800378 private MainThreadHandler mMainThreadHandler;
Wink Savilleac1bdfd2014-11-20 23:04:44 -0800379 private SubscriptionController mSubscriptionController;
Wink Saville3ab207e2014-11-20 13:07:20 -0800380 private SharedPreferences mTelephonySharedPreferences;
Malcolm Chen2c63d402018-08-14 16:00:53 -0700381 private PhoneConfigurationManager mPhoneConfigurationManager;
Daniel Bright94f43662021-03-01 14:43:40 -0800382 private final RadioInterfaceCapabilityController mRadioInterfaceCapabilities;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700383
Peter Wangdafb9ac2020-01-15 14:13:38 -0800384 /** User Activity */
385 private AtomicBoolean mNotifyUserActivity;
Peter Wangdafb9ac2020-01-15 14:13:38 -0800386 private static final int USER_ACTIVITY_NOTIFICATION_DELAY = 200;
387
Jeff Davidson8ab02b22020-03-28 12:24:40 -0700388 private Set<Integer> mCarrierPrivilegeTestOverrideSubIds = new ArraySet<>();
389
Derek Tan97ebb422014-09-05 16:55:38 -0700390 private static final String PREF_CARRIERS_ALPHATAG_PREFIX = "carrier_alphtag_";
391 private static final String PREF_CARRIERS_NUMBER_PREFIX = "carrier_number_";
Jeff Sharkey85190e62014-12-05 09:40:12 -0800392 private static final String PREF_CARRIERS_SUBSCRIBER_PREFIX = "carrier_subscriber_";
Brad Ebinger1c8542e2019-01-14 13:43:14 -0800393 private static final String PREF_PROVISION_IMS_MMTEL_PREFIX = "provision_ims_mmtel_";
Derek Tan89e89d42014-07-08 17:00:10 -0700394
Michelecea4cf22018-12-21 15:00:11 -0800395 // String to store multi SIM allowed
396 private static final String PREF_MULTI_SIM_RESTRICTED = "multisim_restricted";
397
Derek Tan740e1672017-06-27 14:56:27 -0700398 // The AID of ISD-R.
399 private static final String ISDR_AID = "A0000005591010FFFFFFFF8900000100";
400
yinxub1bed742017-04-17 11:45:04 -0700401 private NetworkScanRequestTracker mNetworkScanRequestTracker;
402
David Kelly5e06a7f2018-03-12 14:10:59 +0000403 private static final int TYPE_ALLOCATION_CODE_LENGTH = 8;
404 private static final int MANUFACTURER_CODE_LENGTH = 8;
405
Jack Nudelmanb0b87642020-11-12 15:04:39 -0800406 private static final int SET_DATA_THROTTLING_MODEM_THREW_INVALID_PARAMS = -1;
Jack Nudelman5d6a98b2021-03-04 14:26:25 -0800407 private static final int MODEM_DOES_NOT_SUPPORT_DATA_THROTTLING_ERROR_CODE = -2;
Jack Nudelmanb0b87642020-11-12 15:04:39 -0800408
Derek Tan89e89d42014-07-08 17:00:10 -0700409 /**
Naina Nallurid63128d2019-09-17 14:10:30 -0700410 * Experiment flag to enable erase modem config on reset network, default value is false
411 */
412 public static final String RESET_NETWORK_ERASE_MODEM_CONFIG_ENABLED =
413 "reset_network_erase_modem_config_enabled";
414
Rambo Wang0f050d82021-02-12 11:43:36 -0800415 private static final int SET_NETWORK_SELECTION_MODE_AUTOMATIC_TIMEOUT_MS = 2000; // 2 seconds
Chen Xu540470b2021-12-14 17:15:47 -0800416
sandeepjsb6c87872021-09-27 15:34:44 +0000417 /**
418 * With support for MEP(multiple enabled profile) in Android T, a SIM card can have more than
419 * one ICCID active at the same time.
420 * Apps should use below API signatures if targeting SDK is T and beyond.
421 *
422 * @hide
423 */
424 @ChangeId
425 @EnabledSince(targetSdkVersion = Build.VERSION_CODES.TIRAMISU)
426 public static final long GET_API_SIGNATURES_FROM_UICC_PORT_INFO = 202110963L;
Rambo Wang0f050d82021-02-12 11:43:36 -0800427
Naina Nallurid63128d2019-09-17 14:10:30 -0700428 /**
Chen Xu540470b2021-12-14 17:15:47 -0800429 * Apps targeting on Android T and beyond will get exception whenever icc close channel
430 * operation fails.
431 */
432 @ChangeId
433 @EnabledSince(targetSdkVersion = Build.VERSION_CODES.TIRAMISU)
434 public static final long ICC_CLOSE_CHANNEL_EXCEPTION_ON_FAILURE = 208739934L;
435
436 /**
Shishir Agrawal566b7612013-10-28 14:41:00 -0700437 * A request object to use for transmitting data to an ICC.
438 */
439 private static final class IccAPDUArgument {
440 public int channel, cla, command, p1, p2, p3;
441 public String data;
442
443 public IccAPDUArgument(int channel, int cla, int command,
444 int p1, int p2, int p3, String data) {
445 this.channel = channel;
446 this.cla = cla;
447 this.command = command;
448 this.p1 = p1;
449 this.p2 = p2;
450 this.p3 = p3;
451 this.data = data;
452 }
453 }
454
455 /**
Shishir Agrawal77ba3172015-09-10 14:50:19 -0700456 * A request object to use for transmitting data to an ICC.
457 */
458 private static final class ManualNetworkSelectionArgument {
459 public OperatorInfo operatorInfo;
460 public boolean persistSelection;
461
462 public ManualNetworkSelectionArgument(OperatorInfo operatorInfo, boolean persistSelection) {
463 this.operatorInfo = operatorInfo;
464 this.persistSelection = persistSelection;
465 }
466 }
467
468 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700469 * A request object for use with {@link MainThreadHandler}. Requesters should wait() on the
470 * request after sending. The main thread will notify the request when it is complete.
471 */
472 private static final class MainThreadRequest {
473 /** The argument to use for the request */
474 public Object argument;
475 /** The result of the request that is run on the main thread */
476 public Object result;
Sanket Padawe56e75a32016-02-08 12:18:19 -0800477 // The subscriber id that this request applies to. Defaults to
478 // SubscriptionManager.INVALID_SUBSCRIPTION_ID
479 public Integer subId = SubscriptionManager.INVALID_SUBSCRIPTION_ID;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700480
Nathan Harold92bed182018-10-12 18:16:49 -0700481 // In cases where subId is unavailable, the caller needs to specify the phone.
482 public Phone phone;
483
vagdeviaf9a5b92018-08-15 16:01:53 -0700484 public WorkSource workSource;
485
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700486 public MainThreadRequest(Object argument) {
487 this.argument = argument;
488 }
Shishir Agrawal76d5da92014-11-09 16:17:25 -0800489
Nathan Harold92bed182018-10-12 18:16:49 -0700490 MainThreadRequest(Object argument, Phone phone, WorkSource workSource) {
491 this.argument = argument;
492 if (phone != null) {
493 this.phone = phone;
494 }
495 this.workSource = workSource;
496 }
497
vagdeviaf9a5b92018-08-15 16:01:53 -0700498 MainThreadRequest(Object argument, Integer subId, WorkSource workSource) {
Shishir Agrawal76d5da92014-11-09 16:17:25 -0800499 this.argument = argument;
Sanket Padawe56e75a32016-02-08 12:18:19 -0800500 if (subId != null) {
501 this.subId = subId;
502 }
vagdeviaf9a5b92018-08-15 16:01:53 -0700503 this.workSource = workSource;
Shishir Agrawal76d5da92014-11-09 16:17:25 -0800504 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700505 }
506
Sailesh Nepalcc0375f2013-11-13 09:15:18 -0800507 private static final class IncomingThirdPartyCallArgs {
508 public final ComponentName component;
509 public final String callId;
510 public final String callerDisplayName;
511
512 public IncomingThirdPartyCallArgs(ComponentName component, String callId,
513 String callerDisplayName) {
514 this.component = component;
515 this.callId = callId;
516 this.callerDisplayName = callerDisplayName;
517 }
518 }
519
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700520 /**
521 * A handler that processes messages on the main thread in the phone process. Since many
522 * of the Phone calls are not thread safe this is needed to shuttle the requests from the
523 * inbound binder threads to the main thread in the phone process. The Binder thread
524 * may provide a {@link MainThreadRequest} object in the msg.obj field that they are waiting
525 * on, which will be notified when the operation completes and will contain the result of the
526 * request.
527 *
528 * <p>If a MainThreadRequest object is provided in the msg.obj field,
529 * note that request.result must be set to something non-null for the calling thread to
530 * unblock.
531 */
532 private final class MainThreadHandler extends Handler {
533 @Override
534 public void handleMessage(Message msg) {
535 MainThreadRequest request;
536 Message onCompleted;
537 AsyncResult ar;
Muralidhar Reddy472c2ae2021-09-29 19:38:40 +0000538 UiccPort uiccPort;
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700539 IccAPDUArgument iccArgument;
Nazanin Bakhshif782e562018-12-11 15:15:39 -0800540 final Phone defaultPhone = getDefaultPhone();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700541
542 switch (msg.what) {
Pengquan Menga1bb6272018-09-06 09:59:22 -0700543 case CMD_HANDLE_USSD_REQUEST: {
544 request = (MainThreadRequest) msg.obj;
545 final Phone phone = getPhoneFromRequest(request);
546 Pair<String, ResultReceiver> ussdObject = (Pair) request.argument;
Chen Xue9d737e2022-01-01 23:41:31 -0800547 String ussdRequest = ussdObject.first;
Pengquan Menga1bb6272018-09-06 09:59:22 -0700548 ResultReceiver wrappedCallback = ussdObject.second;
Tyler Gunn65d45c22017-06-05 11:22:26 -0700549
Pengquan Menga1bb6272018-09-06 09:59:22 -0700550 if (!isUssdApiAllowed(request.subId)) {
551 // Carrier does not support use of this API, return failure.
552 Rlog.w(LOG_TAG, "handleUssdRequest: carrier does not support USSD apis.");
553 UssdResponse response = new UssdResponse(ussdRequest, null);
554 Bundle returnData = new Bundle();
555 returnData.putParcelable(TelephonyManager.USSD_RESPONSE, response);
556 wrappedCallback.send(TelephonyManager.USSD_RETURN_FAILURE, returnData);
Tyler Gunn65d45c22017-06-05 11:22:26 -0700557
Pengquan Menga1bb6272018-09-06 09:59:22 -0700558 request.result = true;
559 notifyRequester(request);
560 return;
561 }
Tyler Gunn65d45c22017-06-05 11:22:26 -0700562
Pengquan Menga1bb6272018-09-06 09:59:22 -0700563 try {
564 request.result = phone != null
565 ? phone.handleUssdRequest(ussdRequest, wrappedCallback) : false;
566 } catch (CallStateException cse) {
567 request.result = false;
568 }
569 // Wake up the requesting thread
570 notifyRequester(request);
571 break;
pkanwar32d516d2016-10-14 19:37:38 -0700572 }
573
Yorke Lee716f67e2015-06-17 15:39:16 -0700574 case CMD_HANDLE_PIN_MMI: {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700575 request = (MainThreadRequest) msg.obj;
Yorke Lee716f67e2015-06-17 15:39:16 -0700576 final Phone phone = getPhoneFromRequest(request);
577 request.result = phone != null ?
578 getPhoneFromRequest(request).handlePinMmi((String) request.argument)
579 : false;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700580 // Wake up the requesting thread
Pengquan Menga1bb6272018-09-06 09:59:22 -0700581 notifyRequester(request);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700582 break;
Yorke Lee716f67e2015-06-17 15:39:16 -0700583 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700584
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700585 case CMD_TRANSMIT_APDU_LOGICAL_CHANNEL:
Shishir Agrawal566b7612013-10-28 14:41:00 -0700586 request = (MainThreadRequest) msg.obj;
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700587 iccArgument = (IccAPDUArgument) request.argument;
Muralidhar Reddy472c2ae2021-09-29 19:38:40 +0000588 uiccPort = getUiccPortFromRequest(request);
589 if (uiccPort == null) {
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700590 loge("iccTransmitApduLogicalChannel: No UICC");
Chen Xue9d737e2022-01-01 23:41:31 -0800591 request.result = new IccIoResult(0x6F, 0, (byte[]) null);
Pengquan Menga1bb6272018-09-06 09:59:22 -0700592 notifyRequester(request);
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700593 } else {
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700594 onCompleted = obtainMessage(EVENT_TRANSMIT_APDU_LOGICAL_CHANNEL_DONE,
Chen Xue9d737e2022-01-01 23:41:31 -0800595 request);
Muralidhar Reddy472c2ae2021-09-29 19:38:40 +0000596 uiccPort.iccTransmitApduLogicalChannel(
Chen Xue9d737e2022-01-01 23:41:31 -0800597 iccArgument.channel, iccArgument.cla, iccArgument.command,
598 iccArgument.p1, iccArgument.p2, iccArgument.p3, iccArgument.data,
599 onCompleted);
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700600 }
Shishir Agrawal566b7612013-10-28 14:41:00 -0700601 break;
602
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700603 case EVENT_TRANSMIT_APDU_LOGICAL_CHANNEL_DONE:
Shishir Agrawal566b7612013-10-28 14:41:00 -0700604 ar = (AsyncResult) msg.obj;
605 request = (MainThreadRequest) ar.userObj;
606 if (ar.exception == null && ar.result != null) {
607 request.result = ar.result;
608 } else {
Chen Xue9d737e2022-01-01 23:41:31 -0800609 request.result = new IccIoResult(0x6F, 0, (byte[]) null);
Shishir Agrawal566b7612013-10-28 14:41:00 -0700610 if (ar.result == null) {
611 loge("iccTransmitApduLogicalChannel: Empty response");
Jake Hambye994d462014-02-03 13:10:13 -0800612 } else if (ar.exception instanceof CommandException) {
Shishir Agrawal566b7612013-10-28 14:41:00 -0700613 loge("iccTransmitApduLogicalChannel: CommandException: " +
Jake Hambye994d462014-02-03 13:10:13 -0800614 ar.exception);
Shishir Agrawal566b7612013-10-28 14:41:00 -0700615 } else {
616 loge("iccTransmitApduLogicalChannel: Unknown exception");
617 }
618 }
Pengquan Menga1bb6272018-09-06 09:59:22 -0700619 notifyRequester(request);
Shishir Agrawal566b7612013-10-28 14:41:00 -0700620 break;
621
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700622 case CMD_TRANSMIT_APDU_BASIC_CHANNEL:
623 request = (MainThreadRequest) msg.obj;
624 iccArgument = (IccAPDUArgument) request.argument;
Muralidhar Reddy472c2ae2021-09-29 19:38:40 +0000625 uiccPort = getUiccPortFromRequest(request);
626 if (uiccPort == null) {
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700627 loge("iccTransmitApduBasicChannel: No UICC");
Chen Xue9d737e2022-01-01 23:41:31 -0800628 request.result = new IccIoResult(0x6F, 0, (byte[]) null);
Pengquan Menga1bb6272018-09-06 09:59:22 -0700629 notifyRequester(request);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700630 } else {
631 onCompleted = obtainMessage(EVENT_TRANSMIT_APDU_BASIC_CHANNEL_DONE,
Chen Xue9d737e2022-01-01 23:41:31 -0800632 request);
Muralidhar Reddy472c2ae2021-09-29 19:38:40 +0000633 uiccPort.iccTransmitApduBasicChannel(
Chen Xue9d737e2022-01-01 23:41:31 -0800634 iccArgument.cla, iccArgument.command, iccArgument.p1,
635 iccArgument.p2,
636 iccArgument.p3, iccArgument.data, onCompleted);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700637 }
638 break;
639
640 case EVENT_TRANSMIT_APDU_BASIC_CHANNEL_DONE:
641 ar = (AsyncResult) msg.obj;
642 request = (MainThreadRequest) ar.userObj;
643 if (ar.exception == null && ar.result != null) {
644 request.result = ar.result;
645 } else {
Chen Xue9d737e2022-01-01 23:41:31 -0800646 request.result = new IccIoResult(0x6F, 0, (byte[]) null);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700647 if (ar.result == null) {
648 loge("iccTransmitApduBasicChannel: Empty response");
649 } else if (ar.exception instanceof CommandException) {
650 loge("iccTransmitApduBasicChannel: CommandException: " +
651 ar.exception);
652 } else {
653 loge("iccTransmitApduBasicChannel: Unknown exception");
654 }
655 }
Pengquan Menga1bb6272018-09-06 09:59:22 -0700656 notifyRequester(request);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700657 break;
658
659 case CMD_EXCHANGE_SIM_IO:
660 request = (MainThreadRequest) msg.obj;
661 iccArgument = (IccAPDUArgument) request.argument;
Muralidhar Reddy472c2ae2021-09-29 19:38:40 +0000662 uiccPort = getUiccPortFromRequest(request);
663 if (uiccPort == null) {
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700664 loge("iccExchangeSimIO: No UICC");
Chen Xue9d737e2022-01-01 23:41:31 -0800665 request.result = new IccIoResult(0x6F, 0, (byte[]) null);
Pengquan Menga1bb6272018-09-06 09:59:22 -0700666 notifyRequester(request);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700667 } else {
668 onCompleted = obtainMessage(EVENT_EXCHANGE_SIM_IO_DONE,
669 request);
Muralidhar Reddy472c2ae2021-09-29 19:38:40 +0000670 uiccPort.iccExchangeSimIO(iccArgument.cla, /* fileID */
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700671 iccArgument.command, iccArgument.p1, iccArgument.p2, iccArgument.p3,
672 iccArgument.data, onCompleted);
673 }
674 break;
675
676 case EVENT_EXCHANGE_SIM_IO_DONE:
677 ar = (AsyncResult) msg.obj;
678 request = (MainThreadRequest) ar.userObj;
679 if (ar.exception == null && ar.result != null) {
680 request.result = ar.result;
681 } else {
Chen Xue9d737e2022-01-01 23:41:31 -0800682 request.result = new IccIoResult(0x6f, 0, (byte[]) null);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700683 }
Pengquan Menga1bb6272018-09-06 09:59:22 -0700684 notifyRequester(request);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700685 break;
686
Derek Tan4d5e5c12014-02-04 11:54:58 -0800687 case CMD_SEND_ENVELOPE:
688 request = (MainThreadRequest) msg.obj;
Muralidhar Reddy472c2ae2021-09-29 19:38:40 +0000689 uiccPort = getUiccPortFromRequest(request);
690 if (uiccPort == null) {
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700691 loge("sendEnvelopeWithStatus: No UICC");
Chen Xue9d737e2022-01-01 23:41:31 -0800692 request.result = new IccIoResult(0x6F, 0, (byte[]) null);
Pengquan Menga1bb6272018-09-06 09:59:22 -0700693 notifyRequester(request);
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700694 } else {
695 onCompleted = obtainMessage(EVENT_SEND_ENVELOPE_DONE, request);
Chen Xue9d737e2022-01-01 23:41:31 -0800696 uiccPort.sendEnvelopeWithStatus((String) request.argument, onCompleted);
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700697 }
Derek Tan4d5e5c12014-02-04 11:54:58 -0800698 break;
699
700 case EVENT_SEND_ENVELOPE_DONE:
701 ar = (AsyncResult) msg.obj;
702 request = (MainThreadRequest) ar.userObj;
Shishir Agrawal9f9877d2014-03-14 09:36:27 -0700703 if (ar.exception == null && ar.result != null) {
704 request.result = ar.result;
Derek Tan4d5e5c12014-02-04 11:54:58 -0800705 } else {
Chen Xue9d737e2022-01-01 23:41:31 -0800706 request.result = new IccIoResult(0x6F, 0, (byte[]) null);
Shishir Agrawal9f9877d2014-03-14 09:36:27 -0700707 if (ar.result == null) {
708 loge("sendEnvelopeWithStatus: Empty response");
709 } else if (ar.exception instanceof CommandException) {
710 loge("sendEnvelopeWithStatus: CommandException: " +
711 ar.exception);
712 } else {
713 loge("sendEnvelopeWithStatus: exception:" + ar.exception);
714 }
Derek Tan4d5e5c12014-02-04 11:54:58 -0800715 }
Pengquan Menga1bb6272018-09-06 09:59:22 -0700716 notifyRequester(request);
Derek Tan4d5e5c12014-02-04 11:54:58 -0800717 break;
718
Shishir Agrawal566b7612013-10-28 14:41:00 -0700719 case CMD_OPEN_CHANNEL:
720 request = (MainThreadRequest) msg.obj;
Muralidhar Reddy472c2ae2021-09-29 19:38:40 +0000721 uiccPort = getUiccPortFromRequest(request);
Rambo Wanga1782702021-11-10 20:15:19 -0800722 IccLogicalChannelRequest openChannelRequest =
723 (IccLogicalChannelRequest) request.argument;
Muralidhar Reddy472c2ae2021-09-29 19:38:40 +0000724 if (uiccPort == null) {
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700725 loge("iccOpenLogicalChannel: No UICC");
Shishir Agrawalfc0492a2016-02-17 11:15:33 -0800726 request.result = new IccOpenLogicalChannelResponse(-1,
Chen Xue9d737e2022-01-01 23:41:31 -0800727 IccOpenLogicalChannelResponse.STATUS_MISSING_RESOURCE, null);
Pengquan Menga1bb6272018-09-06 09:59:22 -0700728 notifyRequester(request);
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700729 } else {
730 onCompleted = obtainMessage(EVENT_OPEN_CHANNEL_DONE, request);
Rambo Wanga1782702021-11-10 20:15:19 -0800731 uiccPort.iccOpenLogicalChannel(openChannelRequest.aid,
732 openChannelRequest.p2, onCompleted);
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700733 }
Shishir Agrawal566b7612013-10-28 14:41:00 -0700734 break;
735
736 case EVENT_OPEN_CHANNEL_DONE:
737 ar = (AsyncResult) msg.obj;
738 request = (MainThreadRequest) ar.userObj;
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700739 IccOpenLogicalChannelResponse openChannelResp;
Shishir Agrawal566b7612013-10-28 14:41:00 -0700740 if (ar.exception == null && ar.result != null) {
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700741 int[] result = (int[]) ar.result;
742 int channelId = result[0];
743 byte[] selectResponse = null;
744 if (result.length > 1) {
745 selectResponse = new byte[result.length - 1];
746 for (int i = 1; i < result.length; ++i) {
747 selectResponse[i - 1] = (byte) result[i];
748 }
749 }
750 openChannelResp = new IccOpenLogicalChannelResponse(channelId,
Chen Xue9d737e2022-01-01 23:41:31 -0800751 IccOpenLogicalChannelResponse.STATUS_NO_ERROR, selectResponse);
Rambo Wang3b77c4c2021-11-10 20:15:19 -0800752
753 uiccPort = getUiccPortFromRequest(request);
754 IccLogicalChannelRequest channelRequest =
755 (IccLogicalChannelRequest) request.argument;
756 channelRequest.channel = channelId;
757 uiccPort.onLogicalChannelOpened(channelRequest);
Shishir Agrawal566b7612013-10-28 14:41:00 -0700758 } else {
Shishir Agrawal566b7612013-10-28 14:41:00 -0700759 if (ar.result == null) {
760 loge("iccOpenLogicalChannel: Empty response");
Shishir Agrawal566b7612013-10-28 14:41:00 -0700761 }
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700762 if (ar.exception != null) {
763 loge("iccOpenLogicalChannel: Exception: " + ar.exception);
764 }
765
Shishir Agrawal527e8bf2014-08-25 08:54:56 -0700766 int errorCode = IccOpenLogicalChannelResponse.STATUS_UNKNOWN_ERROR;
Junda Liua754ba12015-05-20 01:17:52 -0700767 if (ar.exception instanceof CommandException) {
768 CommandException.Error error =
Chen Xue9d737e2022-01-01 23:41:31 -0800769 ((CommandException) (ar.exception)).getCommandError();
Junda Liua754ba12015-05-20 01:17:52 -0700770 if (error == CommandException.Error.MISSING_RESOURCE) {
Shishir Agrawal527e8bf2014-08-25 08:54:56 -0700771 errorCode = IccOpenLogicalChannelResponse.STATUS_MISSING_RESOURCE;
Junda Liua754ba12015-05-20 01:17:52 -0700772 } else if (error == CommandException.Error.NO_SUCH_ELEMENT) {
Shishir Agrawal527e8bf2014-08-25 08:54:56 -0700773 errorCode = IccOpenLogicalChannelResponse.STATUS_NO_SUCH_ELEMENT;
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700774 }
775 }
776 openChannelResp = new IccOpenLogicalChannelResponse(
Chen Xue9d737e2022-01-01 23:41:31 -0800777 IccOpenLogicalChannelResponse.INVALID_CHANNEL, errorCode, null);
Shishir Agrawal566b7612013-10-28 14:41:00 -0700778 }
Shishir Agrawal82c8a462014-07-31 18:13:17 -0700779 request.result = openChannelResp;
Pengquan Menga1bb6272018-09-06 09:59:22 -0700780 notifyRequester(request);
Shishir Agrawal566b7612013-10-28 14:41:00 -0700781 break;
782
783 case CMD_CLOSE_CHANNEL:
784 request = (MainThreadRequest) msg.obj;
Muralidhar Reddy472c2ae2021-09-29 19:38:40 +0000785 uiccPort = getUiccPortFromRequest(request);
786 if (uiccPort == null) {
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700787 loge("iccCloseLogicalChannel: No UICC");
Chen Xue9d737e2022-01-01 23:41:31 -0800788 // before this feature is enabled, this API should only return false if
789 // the operation fails instead of throwing runtime exception for
790 // backward-compatibility.
791 if (Compatibility.isChangeEnabled(ICC_CLOSE_CHANNEL_EXCEPTION_ON_FAILURE)) {
792 request.result = new IllegalArgumentException(
793 "iccCloseLogicalChannel: No UICC");
794 } else {
795 request.result = false;
796 }
797 notifyRequester(request);
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700798 } else {
799 onCompleted = obtainMessage(EVENT_CLOSE_CHANNEL_DONE, request);
Muralidhar Reddy472c2ae2021-09-29 19:38:40 +0000800 uiccPort.iccCloseLogicalChannel((Integer) request.argument, onCompleted);
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700801 }
Shishir Agrawal566b7612013-10-28 14:41:00 -0700802 break;
803
804 case EVENT_CLOSE_CHANNEL_DONE:
Chen Xu540470b2021-12-14 17:15:47 -0800805 ar = (AsyncResult) msg.obj;
806 request = (MainThreadRequest) ar.userObj;
807 if (ar.exception == null) {
808 request.result = true;
Rambo Wang3b77c4c2021-11-10 20:15:19 -0800809 uiccPort = getUiccPortFromRequest(request);
810 final int channelId = (Integer) request.argument;
811 uiccPort.onLogicalChannelClosed(channelId);
Chen Xu540470b2021-12-14 17:15:47 -0800812 } else {
813 request.result = false;
Chen Xue9d737e2022-01-01 23:41:31 -0800814 Exception exception = null;
Chen Xu540470b2021-12-14 17:15:47 -0800815 if (ar.exception instanceof CommandException) {
816 loge("iccCloseLogicalChannel: CommandException: " + ar.exception);
817 CommandException.Error error =
818 ((CommandException) (ar.exception)).getCommandError();
Chen Xue9d737e2022-01-01 23:41:31 -0800819 if (error == CommandException.Error.INVALID_ARGUMENTS) {
820 // should only throw exceptions from the binder threads.
821 exception = new IllegalArgumentException(
Chen Xu540470b2021-12-14 17:15:47 -0800822 "iccCloseLogicalChannel: invalid argument ");
823 }
824 } else {
825 loge("iccCloseLogicalChannel: Unknown exception");
826 }
Chen Xue9d737e2022-01-01 23:41:31 -0800827 // before this feature is enabled, this API should only return false if
828 // the operation fails instead of throwing runtime exception for
829 // backward-compatibility.
830 if (Compatibility.isChangeEnabled(ICC_CLOSE_CHANNEL_EXCEPTION_ON_FAILURE))
831 request.result = (exception != null) ? exception :
832 new IllegalStateException(
833 "exception from modem to close iccLogical Channel");
Chen Xu540470b2021-12-14 17:15:47 -0800834 }
835 notifyRequester(request);
Jake Hambye994d462014-02-03 13:10:13 -0800836 break;
837
838 case CMD_NV_READ_ITEM:
839 request = (MainThreadRequest) msg.obj;
840 onCompleted = obtainMessage(EVENT_NV_READ_ITEM_DONE, request);
Nazanin Bakhshif782e562018-12-11 15:15:39 -0800841 defaultPhone.nvReadItem((Integer) request.argument, onCompleted,
842 request.workSource);
Jake Hambye994d462014-02-03 13:10:13 -0800843 break;
844
845 case EVENT_NV_READ_ITEM_DONE:
Shishir Agrawal566b7612013-10-28 14:41:00 -0700846 ar = (AsyncResult) msg.obj;
847 request = (MainThreadRequest) ar.userObj;
Jake Hambye994d462014-02-03 13:10:13 -0800848 if (ar.exception == null && ar.result != null) {
849 request.result = ar.result; // String
Shishir Agrawal566b7612013-10-28 14:41:00 -0700850 } else {
Jake Hambye994d462014-02-03 13:10:13 -0800851 request.result = "";
852 if (ar.result == null) {
853 loge("nvReadItem: Empty response");
854 } else if (ar.exception instanceof CommandException) {
855 loge("nvReadItem: CommandException: " +
856 ar.exception);
Shishir Agrawal566b7612013-10-28 14:41:00 -0700857 } else {
Jake Hambye994d462014-02-03 13:10:13 -0800858 loge("nvReadItem: Unknown exception");
Shishir Agrawal566b7612013-10-28 14:41:00 -0700859 }
860 }
Pengquan Menga1bb6272018-09-06 09:59:22 -0700861 notifyRequester(request);
Shishir Agrawal566b7612013-10-28 14:41:00 -0700862 break;
863
Jake Hambye994d462014-02-03 13:10:13 -0800864 case CMD_NV_WRITE_ITEM:
865 request = (MainThreadRequest) msg.obj;
866 onCompleted = obtainMessage(EVENT_NV_WRITE_ITEM_DONE, request);
867 Pair<Integer, String> idValue = (Pair<Integer, String>) request.argument;
Nazanin Bakhshif782e562018-12-11 15:15:39 -0800868 defaultPhone.nvWriteItem(idValue.first, idValue.second, onCompleted,
vagdeviaf9a5b92018-08-15 16:01:53 -0700869 request.workSource);
Jake Hambye994d462014-02-03 13:10:13 -0800870 break;
871
872 case EVENT_NV_WRITE_ITEM_DONE:
873 handleNullReturnEvent(msg, "nvWriteItem");
874 break;
875
876 case CMD_NV_WRITE_CDMA_PRL:
877 request = (MainThreadRequest) msg.obj;
878 onCompleted = obtainMessage(EVENT_NV_WRITE_CDMA_PRL_DONE, request);
Nazanin Bakhshif782e562018-12-11 15:15:39 -0800879 defaultPhone.nvWriteCdmaPrl((byte[]) request.argument, onCompleted);
Jake Hambye994d462014-02-03 13:10:13 -0800880 break;
881
882 case EVENT_NV_WRITE_CDMA_PRL_DONE:
883 handleNullReturnEvent(msg, "nvWriteCdmaPrl");
884 break;
885
chen xu6dac5ab2018-10-26 17:39:23 -0700886 case CMD_RESET_MODEM_CONFIG:
Jake Hambye994d462014-02-03 13:10:13 -0800887 request = (MainThreadRequest) msg.obj;
chen xu6dac5ab2018-10-26 17:39:23 -0700888 onCompleted = obtainMessage(EVENT_RESET_MODEM_CONFIG_DONE, request);
Nazanin Bakhshif782e562018-12-11 15:15:39 -0800889 defaultPhone.resetModemConfig(onCompleted);
Jake Hambye994d462014-02-03 13:10:13 -0800890 break;
891
chen xu6dac5ab2018-10-26 17:39:23 -0700892 case EVENT_RESET_MODEM_CONFIG_DONE:
893 handleNullReturnEvent(msg, "resetModemConfig");
Jake Hambye994d462014-02-03 13:10:13 -0800894 break;
895
Sooraj Sasindran37444802020-08-11 10:40:43 -0700896 case CMD_IS_NR_DUAL_CONNECTIVITY_ENABLED: {
897 request = (MainThreadRequest) msg.obj;
898 onCompleted = obtainMessage(EVENT_IS_NR_DUAL_CONNECTIVITY_ENABLED_DONE,
899 request);
900 Phone phone = getPhoneFromRequest(request);
901 if (phone != null) {
902 phone.isNrDualConnectivityEnabled(onCompleted, request.workSource);
903 } else {
904 loge("isNRDualConnectivityEnabled: No phone object");
905 request.result = false;
906 notifyRequester(request);
907 }
908 break;
909 }
910
911 case EVENT_IS_NR_DUAL_CONNECTIVITY_ENABLED_DONE:
912 ar = (AsyncResult) msg.obj;
913 request = (MainThreadRequest) ar.userObj;
914 if (ar.exception == null && ar.result != null) {
915 request.result = ar.result;
916 } else {
917 // request.result must be set to something non-null
918 // for the calling thread to unblock
Sooraj Sasindrandaf060f2021-06-15 14:52:55 -0700919 if (ar.result != null) {
Sooraj Sasindran37444802020-08-11 10:40:43 -0700920 request.result = ar.result;
921 } else {
922 request.result = false;
923 }
924 if (ar.result == null) {
925 loge("isNRDualConnectivityEnabled: Empty response");
926 } else if (ar.exception instanceof CommandException) {
927 loge("isNRDualConnectivityEnabled: CommandException: "
928 + ar.exception);
929 } else {
930 loge("isNRDualConnectivityEnabled: Unknown exception");
931 }
932 }
933 notifyRequester(request);
934 break;
935
Sooraj Sasindrandaf060f2021-06-15 14:52:55 -0700936 case CMD_IS_VONR_ENABLED: {
937 request = (MainThreadRequest) msg.obj;
938 onCompleted = obtainMessage(EVENT_IS_VONR_ENABLED_DONE,
939 request);
940 Phone phone = getPhoneFromRequest(request);
941 if (phone != null) {
942 phone.isVoNrEnabled(onCompleted, request.workSource);
943 } else {
944 loge("isVoNrEnabled: No phone object");
945 request.result = false;
946 notifyRequester(request);
947 }
948 break;
949 }
950
951 case EVENT_IS_VONR_ENABLED_DONE:
952 ar = (AsyncResult) msg.obj;
953 request = (MainThreadRequest) ar.userObj;
954 if (ar.exception == null && ar.result != null) {
955 request.result = ar.result;
956 } else {
957 // request.result must be set to something non-null
958 // for the calling thread to unblock
959 if (ar.result != null) {
960 request.result = ar.result;
961 } else {
962 request.result = false;
963 }
964 if (ar.result == null) {
965 loge("isVoNrEnabled: Empty response");
966 } else if (ar.exception instanceof CommandException) {
967 loge("isVoNrEnabled: CommandException: "
968 + ar.exception);
969 } else {
970 loge("isVoNrEnabled: Unknown exception");
971 }
972 }
973 notifyRequester(request);
974 break;
975
Sooraj Sasindran37444802020-08-11 10:40:43 -0700976 case CMD_ENABLE_NR_DUAL_CONNECTIVITY: {
977 request = (MainThreadRequest) msg.obj;
978 onCompleted = obtainMessage(EVENT_ENABLE_NR_DUAL_CONNECTIVITY_DONE, request);
979 Phone phone = getPhoneFromRequest(request);
980 if (phone != null) {
981 phone.setNrDualConnectivityState((int) request.argument, onCompleted,
982 request.workSource);
983 } else {
984 loge("enableNrDualConnectivity: No phone object");
985 request.result =
986 TelephonyManager.ENABLE_NR_DUAL_CONNECTIVITY_RADIO_NOT_AVAILABLE;
987 notifyRequester(request);
988 }
989 break;
990 }
991
992 case EVENT_ENABLE_NR_DUAL_CONNECTIVITY_DONE: {
993 ar = (AsyncResult) msg.obj;
994 request = (MainThreadRequest) ar.userObj;
995 if (ar.exception == null) {
996 request.result =
997 TelephonyManager.ENABLE_NR_DUAL_CONNECTIVITY_SUCCESS;
998 } else {
999 request.result =
1000 TelephonyManager
1001 .ENABLE_NR_DUAL_CONNECTIVITY_RADIO_ERROR;
1002 if (ar.exception instanceof CommandException) {
1003 CommandException.Error error =
1004 ((CommandException) (ar.exception)).getCommandError();
1005 if (error == CommandException.Error.RADIO_NOT_AVAILABLE) {
1006 request.result =
1007 TelephonyManager
1008 .ENABLE_NR_DUAL_CONNECTIVITY_RADIO_NOT_AVAILABLE;
Sooraj Sasindran29654162021-03-03 23:00:01 +00001009 } else if (error == CommandException.Error.REQUEST_NOT_SUPPORTED) {
1010 request.result =
1011 TelephonyManager
1012 .ENABLE_NR_DUAL_CONNECTIVITY_NOT_SUPPORTED;
Sooraj Sasindran37444802020-08-11 10:40:43 -07001013 }
1014 loge("enableNrDualConnectivity" + ": CommandException: "
1015 + ar.exception);
1016 } else {
1017 loge("enableNrDualConnectivity" + ": Unknown exception");
1018 }
1019 }
1020 notifyRequester(request);
1021 break;
1022 }
1023
Sooraj Sasindrandaf060f2021-06-15 14:52:55 -07001024 case CMD_ENABLE_VONR: {
1025 request = (MainThreadRequest) msg.obj;
1026 onCompleted = obtainMessage(EVENT_ENABLE_VONR_DONE, request);
1027 Phone phone = getPhoneFromRequest(request);
1028 if (phone != null) {
1029 phone.setVoNrEnabled((boolean) request.argument, onCompleted,
1030 request.workSource);
1031 } else {
1032 loge("setVoNrEnabled: No phone object");
1033 request.result =
1034 TelephonyManager.ENABLE_VONR_RADIO_NOT_AVAILABLE;
1035 notifyRequester(request);
1036 }
1037 break;
1038 }
1039
1040 case EVENT_ENABLE_VONR_DONE: {
1041 ar = (AsyncResult) msg.obj;
1042 request = (MainThreadRequest) ar.userObj;
1043 if (ar.exception == null) {
1044 request.result = TelephonyManager.ENABLE_VONR_SUCCESS;
1045 } else {
1046 request.result = TelephonyManager.ENABLE_VONR_RADIO_ERROR;
1047 if (ar.exception instanceof CommandException) {
1048 CommandException.Error error =
1049 ((CommandException) (ar.exception)).getCommandError();
1050 if (error == CommandException.Error.RADIO_NOT_AVAILABLE) {
1051 request.result = TelephonyManager.ENABLE_VONR_RADIO_NOT_AVAILABLE;
1052 } else if (error == CommandException.Error.REQUEST_NOT_SUPPORTED) {
1053 request.result = TelephonyManager.ENABLE_VONR_REQUEST_NOT_SUPPORTED;
1054 } else {
1055 request.result = TelephonyManager.ENABLE_VONR_RADIO_ERROR;
1056 }
1057 loge("setVoNrEnabled" + ": CommandException: "
1058 + ar.exception);
1059 } else {
1060 loge("setVoNrEnabled" + ": Unknown exception");
1061 }
1062 }
1063 notifyRequester(request);
1064 break;
1065 }
1066
SongFerngWang3ef3e072020-12-21 16:41:52 +08001067 case CMD_GET_ALLOWED_NETWORK_TYPES_BITMASK:
Jake Hamby7c27be32014-03-03 13:25:59 -08001068 request = (MainThreadRequest) msg.obj;
SongFerngWang3ef3e072020-12-21 16:41:52 +08001069 onCompleted = obtainMessage(EVENT_GET_ALLOWED_NETWORK_TYPES_BITMASK_DONE,
1070 request);
1071 getPhoneFromRequest(request).getAllowedNetworkTypesBitmask(onCompleted);
Jake Hamby7c27be32014-03-03 13:25:59 -08001072 break;
1073
SongFerngWang3ef3e072020-12-21 16:41:52 +08001074 case EVENT_GET_ALLOWED_NETWORK_TYPES_BITMASK_DONE:
Jake Hamby7c27be32014-03-03 13:25:59 -08001075 ar = (AsyncResult) msg.obj;
1076 request = (MainThreadRequest) ar.userObj;
1077 if (ar.exception == null && ar.result != null) {
1078 request.result = ar.result; // Integer
1079 } else {
Nazish Tabassume8ba43a2020-07-28 14:49:25 +05301080 // request.result must be set to something non-null
1081 // for the calling thread to unblock
1082 request.result = new int[]{-1};
Jake Hamby7c27be32014-03-03 13:25:59 -08001083 if (ar.result == null) {
SongFerngWang3ef3e072020-12-21 16:41:52 +08001084 loge("getAllowedNetworkTypesBitmask: Empty response");
Jake Hamby7c27be32014-03-03 13:25:59 -08001085 } else if (ar.exception instanceof CommandException) {
SongFerngWang3ef3e072020-12-21 16:41:52 +08001086 loge("getAllowedNetworkTypesBitmask: CommandException: "
1087 + ar.exception);
Jake Hamby7c27be32014-03-03 13:25:59 -08001088 } else {
SongFerngWang3ef3e072020-12-21 16:41:52 +08001089 loge("getAllowedNetworkTypesBitmask: Unknown exception");
Jake Hamby7c27be32014-03-03 13:25:59 -08001090 }
1091 }
Pengquan Menga1bb6272018-09-06 09:59:22 -07001092 notifyRequester(request);
Jake Hamby7c27be32014-03-03 13:25:59 -08001093 break;
1094
SongFerngWang3ef3e072020-12-21 16:41:52 +08001095 case CMD_SET_ALLOWED_NETWORK_TYPES_FOR_REASON:
Jake Hamby7c27be32014-03-03 13:25:59 -08001096 request = (MainThreadRequest) msg.obj;
SongFerngWang3ef3e072020-12-21 16:41:52 +08001097 onCompleted = obtainMessage(EVENT_SET_ALLOWED_NETWORK_TYPES_FOR_REASON_DONE,
1098 request);
1099 Pair<Integer, Long> reasonWithNetworkTypes =
1100 (Pair<Integer, Long>) request.argument;
1101 getPhoneFromRequest(request).setAllowedNetworkTypes(
1102 reasonWithNetworkTypes.first,
1103 reasonWithNetworkTypes.second,
1104 onCompleted);
Jake Hamby7c27be32014-03-03 13:25:59 -08001105 break;
1106
SongFerngWang3ef3e072020-12-21 16:41:52 +08001107 case EVENT_SET_ALLOWED_NETWORK_TYPES_FOR_REASON_DONE:
1108 handleNullReturnEvent(msg, "setAllowedNetworkTypesForReason");
Jake Hamby7c27be32014-03-03 13:25:59 -08001109 break;
1110
Shuo Qian850e4d6a2018-04-25 21:02:08 +00001111 case CMD_INVOKE_OEM_RIL_REQUEST_RAW:
1112 request = (MainThreadRequest)msg.obj;
1113 onCompleted = obtainMessage(EVENT_INVOKE_OEM_RIL_REQUEST_RAW_DONE, request);
Nazanin Bakhshif782e562018-12-11 15:15:39 -08001114 defaultPhone.invokeOemRilRequestRaw((byte[]) request.argument, onCompleted);
Shuo Qian850e4d6a2018-04-25 21:02:08 +00001115 break;
1116
1117 case EVENT_INVOKE_OEM_RIL_REQUEST_RAW_DONE:
1118 ar = (AsyncResult)msg.obj;
1119 request = (MainThreadRequest)ar.userObj;
1120 request.result = ar;
Pengquan Menga1bb6272018-09-06 09:59:22 -07001121 notifyRequester(request);
Shuo Qian850e4d6a2018-04-25 21:02:08 +00001122 break;
1123
Shishir Agrawal76d5da92014-11-09 16:17:25 -08001124 case CMD_SET_VOICEMAIL_NUMBER:
1125 request = (MainThreadRequest) msg.obj;
1126 onCompleted = obtainMessage(EVENT_SET_VOICEMAIL_NUMBER_DONE, request);
1127 Pair<String, String> tagNum = (Pair<String, String>) request.argument;
Stuart Scott584921c2015-01-15 17:10:34 -08001128 getPhoneFromRequest(request).setVoiceMailNumber(tagNum.first, tagNum.second,
1129 onCompleted);
Shishir Agrawal76d5da92014-11-09 16:17:25 -08001130 break;
1131
1132 case EVENT_SET_VOICEMAIL_NUMBER_DONE:
1133 handleNullReturnEvent(msg, "setVoicemailNumber");
1134 break;
1135
Stuart Scott54788802015-03-30 13:18:01 -07001136 case CMD_SET_NETWORK_SELECTION_MODE_AUTOMATIC:
1137 request = (MainThreadRequest) msg.obj;
1138 onCompleted = obtainMessage(EVENT_SET_NETWORK_SELECTION_MODE_AUTOMATIC_DONE,
1139 request);
1140 getPhoneFromRequest(request).setNetworkSelectionModeAutomatic(onCompleted);
1141 break;
1142
1143 case EVENT_SET_NETWORK_SELECTION_MODE_AUTOMATIC_DONE:
1144 handleNullReturnEvent(msg, "setNetworkSelectionModeAutomatic");
1145 break;
1146
Shishir Agrawal302c8692015-06-19 13:49:39 -07001147 case CMD_PERFORM_NETWORK_SCAN:
1148 request = (MainThreadRequest) msg.obj;
1149 onCompleted = obtainMessage(EVENT_PERFORM_NETWORK_SCAN_DONE, request);
1150 getPhoneFromRequest(request).getAvailableNetworks(onCompleted);
1151 break;
1152
Hall Liu27d24262020-09-18 19:04:59 -07001153 case CMD_GET_CALL_FORWARDING: {
Shuo Qian4a594052020-01-23 11:59:30 -08001154 request = (MainThreadRequest) msg.obj;
1155 onCompleted = obtainMessage(EVENT_GET_CALL_FORWARDING_DONE, request);
Hall Liu27d24262020-09-18 19:04:59 -07001156 Pair<Integer, TelephonyManager.CallForwardingInfoCallback> args =
1157 (Pair<Integer, TelephonyManager.CallForwardingInfoCallback>)
1158 request.argument;
1159 int callForwardingReason = args.first;
1160 request.phone.getCallForwardingOption(callForwardingReason, onCompleted);
Shuo Qian4a594052020-01-23 11:59:30 -08001161 break;
Hall Liu27d24262020-09-18 19:04:59 -07001162 }
1163 case EVENT_GET_CALL_FORWARDING_DONE: {
Shuo Qian4a594052020-01-23 11:59:30 -08001164 ar = (AsyncResult) msg.obj;
1165 request = (MainThreadRequest) ar.userObj;
Hall Liu27d24262020-09-18 19:04:59 -07001166 TelephonyManager.CallForwardingInfoCallback callback =
1167 ((Pair<Integer, TelephonyManager.CallForwardingInfoCallback>)
1168 request.argument).second;
Shuo Qian4a594052020-01-23 11:59:30 -08001169 if (ar.exception == null && ar.result != null) {
Hall Liu27d24262020-09-18 19:04:59 -07001170 CallForwardingInfo callForwardingInfo = null;
Shuo Qian4a594052020-01-23 11:59:30 -08001171 CallForwardInfo[] callForwardInfos = (CallForwardInfo[]) ar.result;
1172 for (CallForwardInfo callForwardInfo : callForwardInfos) {
1173 // Service Class is a bit mask per 3gpp 27.007. Search for
1174 // any service for voice call.
1175 if ((callForwardInfo.serviceClass
1176 & CommandsInterface.SERVICE_CLASS_VOICE) > 0) {
Yuchen Dong69cc1412021-09-27 20:27:01 +08001177 callForwardingInfo = new CallForwardingInfo(
1178 callForwardInfo.status
1179 == CommandsInterface.CF_ACTION_ENABLE,
Hall Liu27d24262020-09-18 19:04:59 -07001180 callForwardInfo.reason,
1181 callForwardInfo.number,
1182 callForwardInfo.timeSeconds);
Shuo Qian4a594052020-01-23 11:59:30 -08001183 break;
1184 }
1185 }
1186 // Didn't find a call forward info for voice call.
1187 if (callForwardingInfo == null) {
Hall Liu27d24262020-09-18 19:04:59 -07001188 callForwardingInfo = new CallForwardingInfo(false /* enabled */,
1189 0 /* reason */, null /* number */, 0 /* timeout */);
Shuo Qian4a594052020-01-23 11:59:30 -08001190 }
Hall Liu27d24262020-09-18 19:04:59 -07001191 callback.onCallForwardingInfoAvailable(callForwardingInfo);
Shuo Qian4a594052020-01-23 11:59:30 -08001192 } else {
1193 if (ar.result == null) {
1194 loge("EVENT_GET_CALL_FORWARDING_DONE: Empty response");
1195 }
1196 if (ar.exception != null) {
1197 loge("EVENT_GET_CALL_FORWARDING_DONE: Exception: " + ar.exception);
1198 }
Hall Liu940c4ca2020-09-29 17:10:18 -07001199 int errorCode = TelephonyManager
1200 .CallForwardingInfoCallback.RESULT_ERROR_UNKNOWN;
Shuo Qian4a594052020-01-23 11:59:30 -08001201 if (ar.exception instanceof CommandException) {
1202 CommandException.Error error =
1203 ((CommandException) (ar.exception)).getCommandError();
1204 if (error == CommandException.Error.FDN_CHECK_FAILURE) {
Hall Liu940c4ca2020-09-29 17:10:18 -07001205 errorCode = TelephonyManager
1206 .CallForwardingInfoCallback.RESULT_ERROR_FDN_CHECK_FAILURE;
Shuo Qian4a594052020-01-23 11:59:30 -08001207 } else if (error == CommandException.Error.REQUEST_NOT_SUPPORTED) {
Hall Liu940c4ca2020-09-29 17:10:18 -07001208 errorCode = TelephonyManager
1209 .CallForwardingInfoCallback.RESULT_ERROR_NOT_SUPPORTED;
Shuo Qian4a594052020-01-23 11:59:30 -08001210 }
1211 }
Hall Liu27d24262020-09-18 19:04:59 -07001212 callback.onError(errorCode);
Shuo Qian4a594052020-01-23 11:59:30 -08001213 }
Shuo Qian4a594052020-01-23 11:59:30 -08001214 break;
Hall Liu27d24262020-09-18 19:04:59 -07001215 }
Shuo Qian4a594052020-01-23 11:59:30 -08001216
Hall Liu27d24262020-09-18 19:04:59 -07001217 case CMD_SET_CALL_FORWARDING: {
Shuo Qian4a594052020-01-23 11:59:30 -08001218 request = (MainThreadRequest) msg.obj;
1219 onCompleted = obtainMessage(EVENT_SET_CALL_FORWARDING_DONE, request);
Hall Liu27d24262020-09-18 19:04:59 -07001220 request = (MainThreadRequest) msg.obj;
Shuo Qian4a594052020-01-23 11:59:30 -08001221 CallForwardingInfo callForwardingInfoToSet =
Hall Liu27d24262020-09-18 19:04:59 -07001222 ((Pair<CallForwardingInfo, Consumer<Integer>>)
1223 request.argument).first;
1224 request.phone.setCallForwardingOption(
1225 callForwardingInfoToSet.isEnabled()
Calvin Pan258f1f72021-07-28 21:46:56 +08001226 ? CommandsInterface.CF_ACTION_REGISTRATION
Hall Liu27d24262020-09-18 19:04:59 -07001227 : CommandsInterface.CF_ACTION_DISABLE,
Shuo Qian4a594052020-01-23 11:59:30 -08001228 callForwardingInfoToSet.getReason(),
1229 callForwardingInfoToSet.getNumber(),
1230 callForwardingInfoToSet.getTimeoutSeconds(), onCompleted);
1231 break;
Hall Liu27d24262020-09-18 19:04:59 -07001232 }
Shuo Qian4a594052020-01-23 11:59:30 -08001233
Hall Liu27d24262020-09-18 19:04:59 -07001234 case EVENT_SET_CALL_FORWARDING_DONE: {
Shuo Qian4a594052020-01-23 11:59:30 -08001235 ar = (AsyncResult) msg.obj;
1236 request = (MainThreadRequest) ar.userObj;
Hall Liu27d24262020-09-18 19:04:59 -07001237 Consumer<Integer> callback =
1238 ((Pair<CallForwardingInfo, Consumer<Integer>>)
1239 request.argument).second;
1240 if (ar.exception != null) {
Shuo Qian4a594052020-01-23 11:59:30 -08001241 loge("setCallForwarding exception: " + ar.exception);
Hall Liu940c4ca2020-09-29 17:10:18 -07001242 int errorCode = TelephonyManager.CallForwardingInfoCallback
1243 .RESULT_ERROR_UNKNOWN;
Hall Liu27d24262020-09-18 19:04:59 -07001244 if (ar.exception instanceof CommandException) {
1245 CommandException.Error error =
1246 ((CommandException) (ar.exception)).getCommandError();
1247 if (error == CommandException.Error.FDN_CHECK_FAILURE) {
Hall Liu940c4ca2020-09-29 17:10:18 -07001248 errorCode = TelephonyManager.CallForwardingInfoCallback
1249 .RESULT_ERROR_FDN_CHECK_FAILURE;
Hall Liu27d24262020-09-18 19:04:59 -07001250 } else if (error == CommandException.Error.REQUEST_NOT_SUPPORTED) {
Hall Liu940c4ca2020-09-29 17:10:18 -07001251 errorCode = TelephonyManager.CallForwardingInfoCallback
1252 .RESULT_ERROR_NOT_SUPPORTED;
Hall Liu27d24262020-09-18 19:04:59 -07001253 }
1254 }
1255 callback.accept(errorCode);
1256 } else {
Hall Liu940c4ca2020-09-29 17:10:18 -07001257 callback.accept(TelephonyManager.CallForwardingInfoCallback.RESULT_SUCCESS);
Shuo Qian4a594052020-01-23 11:59:30 -08001258 }
Shuo Qian4a594052020-01-23 11:59:30 -08001259 break;
Hall Liu27d24262020-09-18 19:04:59 -07001260 }
Shuo Qian4a594052020-01-23 11:59:30 -08001261
Hall Liu27d24262020-09-18 19:04:59 -07001262 case CMD_GET_CALL_WAITING: {
Shuo Qian4a594052020-01-23 11:59:30 -08001263 request = (MainThreadRequest) msg.obj;
1264 onCompleted = obtainMessage(EVENT_GET_CALL_WAITING_DONE, request);
1265 getPhoneFromRequest(request).getCallWaiting(onCompleted);
1266 break;
Hall Liu27d24262020-09-18 19:04:59 -07001267 }
Shuo Qian4a594052020-01-23 11:59:30 -08001268
Hall Liu27d24262020-09-18 19:04:59 -07001269 case EVENT_GET_CALL_WAITING_DONE: {
Shuo Qian4a594052020-01-23 11:59:30 -08001270 ar = (AsyncResult) msg.obj;
1271 request = (MainThreadRequest) ar.userObj;
Hall Liu27d24262020-09-18 19:04:59 -07001272 Consumer<Integer> callback = (Consumer<Integer>) request.argument;
Shuo Qian4a594052020-01-23 11:59:30 -08001273 int callForwardingStatus = TelephonyManager.CALL_WAITING_STATUS_UNKNOWN_ERROR;
1274 if (ar.exception == null && ar.result != null) {
Shuo Qiand6a0dba2020-02-18 18:13:49 -08001275 int[] callForwardResults = (int[]) ar.result;
Shuo Qian4a594052020-01-23 11:59:30 -08001276 // Service Class is a bit mask per 3gpp 27.007.
1277 // Search for any service for voice call.
Shuo Qiand6a0dba2020-02-18 18:13:49 -08001278 if (callForwardResults.length > 1
1279 && ((callForwardResults[1]
Hall Liu27d24262020-09-18 19:04:59 -07001280 & CommandsInterface.SERVICE_CLASS_VOICE) > 0)) {
Shuo Qiand6a0dba2020-02-18 18:13:49 -08001281 callForwardingStatus = callForwardResults[0] == 0
Hall Liu27d24262020-09-18 19:04:59 -07001282 ? TelephonyManager.CALL_WAITING_STATUS_DISABLED
1283 : TelephonyManager.CALL_WAITING_STATUS_ENABLED;
Shuo Qian4a594052020-01-23 11:59:30 -08001284 } else {
Hall Liu27d24262020-09-18 19:04:59 -07001285 callForwardingStatus = TelephonyManager.CALL_WAITING_STATUS_DISABLED;
Shuo Qian4a594052020-01-23 11:59:30 -08001286 }
1287 } else {
1288 if (ar.result == null) {
1289 loge("EVENT_GET_CALL_WAITING_DONE: Empty response");
1290 }
1291 if (ar.exception != null) {
1292 loge("EVENT_GET_CALL_WAITING_DONE: Exception: " + ar.exception);
1293 }
1294 if (ar.exception instanceof CommandException) {
1295 CommandException.Error error =
1296 ((CommandException) (ar.exception)).getCommandError();
1297 if (error == CommandException.Error.REQUEST_NOT_SUPPORTED) {
1298 callForwardingStatus =
1299 TelephonyManager.CALL_WAITING_STATUS_NOT_SUPPORTED;
1300 }
1301 }
1302 }
Hall Liu27d24262020-09-18 19:04:59 -07001303 callback.accept(callForwardingStatus);
Shuo Qian4a594052020-01-23 11:59:30 -08001304 break;
Hall Liu27d24262020-09-18 19:04:59 -07001305 }
Shuo Qian4a594052020-01-23 11:59:30 -08001306
Hall Liu27d24262020-09-18 19:04:59 -07001307 case CMD_SET_CALL_WAITING: {
Shuo Qian4a594052020-01-23 11:59:30 -08001308 request = (MainThreadRequest) msg.obj;
1309 onCompleted = obtainMessage(EVENT_SET_CALL_WAITING_DONE, request);
Hall Liu27d24262020-09-18 19:04:59 -07001310 boolean enable = ((Pair<Boolean, Consumer<Integer>>) request.argument).first;
1311 getPhoneFromRequest(request).setCallWaiting(enable, onCompleted);
Shuo Qian4a594052020-01-23 11:59:30 -08001312 break;
Hall Liu27d24262020-09-18 19:04:59 -07001313 }
Shuo Qian4a594052020-01-23 11:59:30 -08001314
Hall Liu27d24262020-09-18 19:04:59 -07001315 case EVENT_SET_CALL_WAITING_DONE: {
Shuo Qian4a594052020-01-23 11:59:30 -08001316 ar = (AsyncResult) msg.obj;
1317 request = (MainThreadRequest) ar.userObj;
Hall Liu27d24262020-09-18 19:04:59 -07001318 boolean enable = ((Pair<Boolean, Consumer<Integer>>) request.argument).first;
1319 Consumer<Integer> callback =
1320 ((Pair<Boolean, Consumer<Integer>>) request.argument).second;
1321 if (ar.exception != null) {
Shuo Qian4a594052020-01-23 11:59:30 -08001322 loge("setCallWaiting exception: " + ar.exception);
Hall Liu27d24262020-09-18 19:04:59 -07001323 if (ar.exception instanceof CommandException) {
1324 CommandException.Error error =
1325 ((CommandException) (ar.exception)).getCommandError();
1326 if (error == CommandException.Error.REQUEST_NOT_SUPPORTED) {
1327 callback.accept(TelephonyManager.CALL_WAITING_STATUS_NOT_SUPPORTED);
1328 } else {
1329 callback.accept(TelephonyManager.CALL_WAITING_STATUS_UNKNOWN_ERROR);
1330 }
1331 } else {
1332 callback.accept(TelephonyManager.CALL_WAITING_STATUS_UNKNOWN_ERROR);
1333 }
1334 } else {
1335 callback.accept(enable ? TelephonyManager.CALL_WAITING_STATUS_ENABLED
1336 : TelephonyManager.CALL_WAITING_STATUS_DISABLED);
Shuo Qian4a594052020-01-23 11:59:30 -08001337 }
Shuo Qian4a594052020-01-23 11:59:30 -08001338 break;
Hall Liu27d24262020-09-18 19:04:59 -07001339 }
Shishir Agrawal302c8692015-06-19 13:49:39 -07001340 case EVENT_PERFORM_NETWORK_SCAN_DONE:
1341 ar = (AsyncResult) msg.obj;
1342 request = (MainThreadRequest) ar.userObj;
1343 CellNetworkScanResult cellScanResult;
1344 if (ar.exception == null && ar.result != null) {
1345 cellScanResult = new CellNetworkScanResult(
1346 CellNetworkScanResult.STATUS_SUCCESS,
1347 (List<OperatorInfo>) ar.result);
1348 } else {
1349 if (ar.result == null) {
1350 loge("getCellNetworkScanResults: Empty response");
1351 }
1352 if (ar.exception != null) {
1353 loge("getCellNetworkScanResults: Exception: " + ar.exception);
1354 }
1355 int errorCode = CellNetworkScanResult.STATUS_UNKNOWN_ERROR;
1356 if (ar.exception instanceof CommandException) {
1357 CommandException.Error error =
1358 ((CommandException) (ar.exception)).getCommandError();
1359 if (error == CommandException.Error.RADIO_NOT_AVAILABLE) {
1360 errorCode = CellNetworkScanResult.STATUS_RADIO_NOT_AVAILABLE;
1361 } else if (error == CommandException.Error.GENERIC_FAILURE) {
1362 errorCode = CellNetworkScanResult.STATUS_RADIO_GENERIC_FAILURE;
1363 }
1364 }
1365 cellScanResult = new CellNetworkScanResult(errorCode, null);
1366 }
1367 request.result = cellScanResult;
Pengquan Menga1bb6272018-09-06 09:59:22 -07001368 notifyRequester(request);
Shishir Agrawal302c8692015-06-19 13:49:39 -07001369 break;
1370
1371 case CMD_SET_NETWORK_SELECTION_MODE_MANUAL:
1372 request = (MainThreadRequest) msg.obj;
Shishir Agrawal77ba3172015-09-10 14:50:19 -07001373 ManualNetworkSelectionArgument selArg =
1374 (ManualNetworkSelectionArgument) request.argument;
Shishir Agrawal302c8692015-06-19 13:49:39 -07001375 onCompleted = obtainMessage(EVENT_SET_NETWORK_SELECTION_MODE_MANUAL_DONE,
1376 request);
Shishir Agrawal77ba3172015-09-10 14:50:19 -07001377 getPhoneFromRequest(request).selectNetworkManually(selArg.operatorInfo,
1378 selArg.persistSelection, onCompleted);
Shishir Agrawal302c8692015-06-19 13:49:39 -07001379 break;
1380
1381 case EVENT_SET_NETWORK_SELECTION_MODE_MANUAL_DONE:
Pengquan Menge3d01e22018-09-20 15:25:35 -07001382 ar = (AsyncResult) msg.obj;
1383 request = (MainThreadRequest) ar.userObj;
1384 if (ar.exception == null) {
1385 request.result = true;
1386 } else {
1387 request.result = false;
1388 loge("setNetworkSelectionModeManual " + ar.exception);
1389 }
1390 notifyRequester(request);
1391 mApp.onNetworkSelectionChanged(request.subId);
Shishir Agrawal302c8692015-06-19 13:49:39 -07001392 break;
1393
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -07001394 case CMD_GET_MODEM_ACTIVITY_INFO:
1395 request = (MainThreadRequest) msg.obj;
1396 onCompleted = obtainMessage(EVENT_GET_MODEM_ACTIVITY_INFO_DONE, request);
James Mattisab947702019-04-03 14:18:34 -07001397 if (defaultPhone != null) {
1398 defaultPhone.getModemActivityInfo(onCompleted, request.workSource);
Shuo Qian8f4750a2020-02-20 17:12:10 -08001399 } else {
1400 ResultReceiver result = (ResultReceiver) request.argument;
1401 Bundle bundle = new Bundle();
1402 bundle.putParcelable(TelephonyManager.MODEM_ACTIVITY_RESULT_KEY,
Hall Liu49656c02020-10-09 19:00:11 -07001403 new ModemActivityInfo(0, 0, 0,
1404 new int[ModemActivityInfo.getNumTxPowerLevels()], 0));
Shuo Qian8f4750a2020-02-20 17:12:10 -08001405 result.send(0, bundle);
James Mattisab947702019-04-03 14:18:34 -07001406 }
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -07001407 break;
1408
Hall Liud0f208c2020-10-14 16:54:44 -07001409 case EVENT_GET_MODEM_ACTIVITY_INFO_DONE: {
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -07001410 ar = (AsyncResult) msg.obj;
1411 request = (MainThreadRequest) ar.userObj;
Shuo Qian8f4750a2020-02-20 17:12:10 -08001412 ResultReceiver result = (ResultReceiver) request.argument;
1413
Hall Liud0f208c2020-10-14 16:54:44 -07001414 ModemActivityInfo ret = null;
1415 int error = 0;
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -07001416 if (ar.exception == null && ar.result != null) {
Shuo Qian8f4750a2020-02-20 17:12:10 -08001417 // Update the last modem activity info and the result of the request.
1418 ModemActivityInfo info = (ModemActivityInfo) ar.result;
1419 if (isModemActivityInfoValid(info)) {
Hall Liu49656c02020-10-09 19:00:11 -07001420 int[] mergedTxTimeMs = new int[ModemActivityInfo.getNumTxPowerLevels()];
Shuo Qian8f4750a2020-02-20 17:12:10 -08001421 int[] txTimeMs = info.getTransmitTimeMillis();
1422 int[] lastModemTxTimeMs = mLastModemActivityInfo
1423 .getTransmitTimeMillis();
1424 for (int i = 0; i < mergedTxTimeMs.length; i++) {
1425 mergedTxTimeMs[i] = txTimeMs[i] + lastModemTxTimeMs[i];
1426 }
Hall Liu49656c02020-10-09 19:00:11 -07001427 mLastModemActivityInfo.setTimestamp(info.getTimestampMillis());
Shuo Qian8f4750a2020-02-20 17:12:10 -08001428 mLastModemActivityInfo.setSleepTimeMillis(info.getSleepTimeMillis()
1429 + mLastModemActivityInfo.getSleepTimeMillis());
1430 mLastModemActivityInfo.setIdleTimeMillis(info.getIdleTimeMillis()
1431 + mLastModemActivityInfo.getIdleTimeMillis());
1432 mLastModemActivityInfo.setTransmitTimeMillis(mergedTxTimeMs);
1433 mLastModemActivityInfo.setReceiveTimeMillis(
1434 info.getReceiveTimeMillis()
1435 + mLastModemActivityInfo.getReceiveTimeMillis());
1436 }
Hall Liu49656c02020-10-09 19:00:11 -07001437 ret = new ModemActivityInfo(mLastModemActivityInfo.getTimestampMillis(),
Shuo Qian8f4750a2020-02-20 17:12:10 -08001438 mLastModemActivityInfo.getSleepTimeMillis(),
1439 mLastModemActivityInfo.getIdleTimeMillis(),
1440 mLastModemActivityInfo.getTransmitTimeMillis(),
1441 mLastModemActivityInfo.getReceiveTimeMillis());
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -07001442 } else {
1443 if (ar.result == null) {
1444 loge("queryModemActivityInfo: Empty response");
Hall Liud0f208c2020-10-14 16:54:44 -07001445 error = TelephonyManager.ModemActivityInfoException
1446 .ERROR_INVALID_INFO_RECEIVED;
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -07001447 } else if (ar.exception instanceof CommandException) {
1448 loge("queryModemActivityInfo: CommandException: " +
1449 ar.exception);
Hall Liud0f208c2020-10-14 16:54:44 -07001450 error = TelephonyManager.ModemActivityInfoException
1451 .ERROR_MODEM_RESPONSE_ERROR;
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -07001452 } else {
1453 loge("queryModemActivityInfo: Unknown exception");
Hall Liud0f208c2020-10-14 16:54:44 -07001454 error = TelephonyManager.ModemActivityInfoException
1455 .ERROR_UNKNOWN;
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -07001456 }
1457 }
Shuo Qian8f4750a2020-02-20 17:12:10 -08001458 Bundle bundle = new Bundle();
Hall Liud0f208c2020-10-14 16:54:44 -07001459 if (ret != null) {
1460 bundle.putParcelable(TelephonyManager.MODEM_ACTIVITY_RESULT_KEY, ret);
1461 } else {
1462 bundle.putInt(TelephonyManager.EXCEPTION_RESULT_KEY, error);
1463 }
Shuo Qian8f4750a2020-02-20 17:12:10 -08001464 result.send(0, bundle);
Pengquan Menga1bb6272018-09-06 09:59:22 -07001465 notifyRequester(request);
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -07001466 break;
Hall Liud0f208c2020-10-14 16:54:44 -07001467 }
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -07001468
Meng Wang1a7c35a2016-05-05 20:56:15 -07001469 case CMD_SET_ALLOWED_CARRIERS:
1470 request = (MainThreadRequest) msg.obj;
Michele Berionne482f8202018-11-27 18:57:59 -08001471 CarrierRestrictionRules argument =
1472 (CarrierRestrictionRules) request.argument;
Meng Wang1a7c35a2016-05-05 20:56:15 -07001473 onCompleted = obtainMessage(EVENT_SET_ALLOWED_CARRIERS_DONE, request);
Michele Berionne482f8202018-11-27 18:57:59 -08001474 defaultPhone.setAllowedCarriers(argument, onCompleted, request.workSource);
Meng Wang1a7c35a2016-05-05 20:56:15 -07001475 break;
1476
1477 case EVENT_SET_ALLOWED_CARRIERS_DONE:
1478 ar = (AsyncResult) msg.obj;
1479 request = (MainThreadRequest) ar.userObj;
1480 if (ar.exception == null && ar.result != null) {
1481 request.result = ar.result;
1482 } else {
Michele Berionne482f8202018-11-27 18:57:59 -08001483 request.result = TelephonyManager.SET_CARRIER_RESTRICTION_ERROR;
1484 if (ar.exception instanceof CommandException) {
1485 loge("setAllowedCarriers: CommandException: " + ar.exception);
1486 CommandException.Error error =
1487 ((CommandException) (ar.exception)).getCommandError();
1488 if (error == CommandException.Error.REQUEST_NOT_SUPPORTED) {
1489 request.result =
1490 TelephonyManager.SET_CARRIER_RESTRICTION_NOT_SUPPORTED;
1491 }
Meng Wang1a7c35a2016-05-05 20:56:15 -07001492 } else {
1493 loge("setAllowedCarriers: Unknown exception");
1494 }
1495 }
Pengquan Menga1bb6272018-09-06 09:59:22 -07001496 notifyRequester(request);
Meng Wang1a7c35a2016-05-05 20:56:15 -07001497 break;
1498
1499 case CMD_GET_ALLOWED_CARRIERS:
1500 request = (MainThreadRequest) msg.obj;
1501 onCompleted = obtainMessage(EVENT_GET_ALLOWED_CARRIERS_DONE, request);
Nazanin Bakhshif782e562018-12-11 15:15:39 -08001502 defaultPhone.getAllowedCarriers(onCompleted, request.workSource);
Meng Wang1a7c35a2016-05-05 20:56:15 -07001503 break;
1504
1505 case EVENT_GET_ALLOWED_CARRIERS_DONE:
1506 ar = (AsyncResult) msg.obj;
1507 request = (MainThreadRequest) ar.userObj;
1508 if (ar.exception == null && ar.result != null) {
1509 request.result = ar.result;
1510 } else {
Michele Berionne482f8202018-11-27 18:57:59 -08001511 request.result = new IllegalStateException(
1512 "Failed to get carrier restrictions");
Meng Wang1a7c35a2016-05-05 20:56:15 -07001513 if (ar.result == null) {
1514 loge("getAllowedCarriers: Empty response");
1515 } else if (ar.exception instanceof CommandException) {
1516 loge("getAllowedCarriers: CommandException: " +
1517 ar.exception);
1518 } else {
1519 loge("getAllowedCarriers: Unknown exception");
1520 }
1521 }
Pengquan Menga1bb6272018-09-06 09:59:22 -07001522 notifyRequester(request);
Meng Wang1a7c35a2016-05-05 20:56:15 -07001523 break;
1524
Nathan Haroldb3014052017-01-25 15:57:32 -08001525 case EVENT_GET_FORBIDDEN_PLMNS_DONE:
1526 ar = (AsyncResult) msg.obj;
1527 request = (MainThreadRequest) ar.userObj;
1528 if (ar.exception == null && ar.result != null) {
1529 request.result = ar.result;
1530 } else {
1531 request.result = new IllegalArgumentException(
1532 "Failed to retrieve Forbidden Plmns");
1533 if (ar.result == null) {
1534 loge("getForbiddenPlmns: Empty response");
1535 } else {
1536 loge("getForbiddenPlmns: Unknown exception");
1537 }
1538 }
Pengquan Menga1bb6272018-09-06 09:59:22 -07001539 notifyRequester(request);
Nathan Haroldb3014052017-01-25 15:57:32 -08001540 break;
1541
1542 case CMD_GET_FORBIDDEN_PLMNS:
1543 request = (MainThreadRequest) msg.obj;
Muralidhar Reddy472c2ae2021-09-29 19:38:40 +00001544 uiccPort = getUiccPortFromRequest(request);
1545 if (uiccPort == null) {
1546 loge("getForbiddenPlmns() UiccPort is null");
Nathan Haroldb3014052017-01-25 15:57:32 -08001547 request.result = new IllegalArgumentException(
Muralidhar Reddy472c2ae2021-09-29 19:38:40 +00001548 "getForbiddenPlmns() UiccPort is null");
Pengquan Menga1bb6272018-09-06 09:59:22 -07001549 notifyRequester(request);
Nathan Haroldb3014052017-01-25 15:57:32 -08001550 break;
1551 }
1552 Integer appType = (Integer) request.argument;
Muralidhar Reddy472c2ae2021-09-29 19:38:40 +00001553 UiccCardApplication uiccApp = uiccPort.getApplicationByType(appType);
Nathan Haroldb3014052017-01-25 15:57:32 -08001554 if (uiccApp == null) {
1555 loge("getForbiddenPlmns() no app with specified type -- "
1556 + appType);
1557 request.result = new IllegalArgumentException("Failed to get UICC App");
Pengquan Menga1bb6272018-09-06 09:59:22 -07001558 notifyRequester(request);
Nathan Haroldb3014052017-01-25 15:57:32 -08001559 break;
1560 } else {
1561 if (DBG) logv("getForbiddenPlmns() found app " + uiccApp.getAid()
1562 + " specified type -- " + appType);
1563 }
1564 onCompleted = obtainMessage(EVENT_GET_FORBIDDEN_PLMNS_DONE, request);
1565 ((SIMRecords) uiccApp.getIccRecords()).getForbiddenPlmns(
1566 onCompleted);
1567 break;
1568
Holly Jiuyu Sun01c47ad2018-01-24 17:56:33 +00001569 case CMD_SWITCH_SLOTS:
1570 request = (MainThreadRequest) msg.obj;
Muralidhar Reddyeb809e32021-11-19 03:07:54 +00001571 List<UiccSlotMapping> slotMapping = (List<UiccSlotMapping>) request.argument;
Holly Jiuyu Sun01c47ad2018-01-24 17:56:33 +00001572 onCompleted = obtainMessage(EVENT_SWITCH_SLOTS_DONE, request);
Muralidhar Reddyeb809e32021-11-19 03:07:54 +00001573 UiccController.getInstance().switchSlots(slotMapping, onCompleted);
Holly Jiuyu Sun01c47ad2018-01-24 17:56:33 +00001574 break;
1575
1576 case EVENT_SWITCH_SLOTS_DONE:
1577 ar = (AsyncResult) msg.obj;
1578 request = (MainThreadRequest) ar.userObj;
1579 request.result = (ar.exception == null);
Pengquan Menga1bb6272018-09-06 09:59:22 -07001580 notifyRequester(request);
1581 break;
1582 case CMD_GET_NETWORK_SELECTION_MODE:
1583 request = (MainThreadRequest) msg.obj;
1584 onCompleted = obtainMessage(EVENT_GET_NETWORK_SELECTION_MODE_DONE, request);
1585 getPhoneFromRequest(request).getNetworkSelectionMode(onCompleted);
1586 break;
1587
1588 case EVENT_GET_NETWORK_SELECTION_MODE_DONE:
1589 ar = (AsyncResult) msg.obj;
1590 request = (MainThreadRequest) ar.userObj;
1591 if (ar.exception != null) {
1592 request.result = TelephonyManager.NETWORK_SELECTION_MODE_UNKNOWN;
1593 } else {
1594 int mode = ((int[]) ar.result)[0];
1595 if (mode == 0) {
1596 request.result = TelephonyManager.NETWORK_SELECTION_MODE_AUTO;
1597 } else {
1598 request.result = TelephonyManager.NETWORK_SELECTION_MODE_MANUAL;
1599 }
Holly Jiuyu Sun01c47ad2018-01-24 17:56:33 +00001600 }
Pengquan Menga1bb6272018-09-06 09:59:22 -07001601 notifyRequester(request);
1602 break;
1603 case CMD_GET_CDMA_ROAMING_MODE:
1604 request = (MainThreadRequest) msg.obj;
1605 onCompleted = obtainMessage(EVENT_GET_CDMA_ROAMING_MODE_DONE, request);
1606 getPhoneFromRequest(request).queryCdmaRoamingPreference(onCompleted);
1607 break;
1608 case EVENT_GET_CDMA_ROAMING_MODE_DONE:
1609 ar = (AsyncResult) msg.obj;
1610 request = (MainThreadRequest) ar.userObj;
1611 if (ar.exception != null) {
1612 request.result = TelephonyManager.CDMA_ROAMING_MODE_RADIO_DEFAULT;
1613 } else {
1614 request.result = ((int[]) ar.result)[0];
1615 }
1616 notifyRequester(request);
1617 break;
1618 case CMD_SET_CDMA_ROAMING_MODE:
1619 request = (MainThreadRequest) msg.obj;
1620 onCompleted = obtainMessage(EVENT_SET_CDMA_ROAMING_MODE_DONE, request);
1621 int mode = (int) request.argument;
1622 getPhoneFromRequest(request).setCdmaRoamingPreference(mode, onCompleted);
1623 break;
1624 case EVENT_SET_CDMA_ROAMING_MODE_DONE:
1625 ar = (AsyncResult) msg.obj;
1626 request = (MainThreadRequest) ar.userObj;
1627 request.result = ar.exception == null;
1628 notifyRequester(request);
1629 break;
Sarah Chinbaab1432020-10-28 13:46:24 -07001630 case CMD_GET_CDMA_SUBSCRIPTION_MODE:
1631 request = (MainThreadRequest) msg.obj;
1632 onCompleted = obtainMessage(EVENT_GET_CDMA_SUBSCRIPTION_MODE_DONE, request);
1633 getPhoneFromRequest(request).queryCdmaSubscriptionMode(onCompleted);
1634 break;
1635 case EVENT_GET_CDMA_SUBSCRIPTION_MODE_DONE:
1636 ar = (AsyncResult) msg.obj;
1637 request = (MainThreadRequest) ar.userObj;
1638 if (ar.exception != null) {
1639 request.result = TelephonyManager.CDMA_SUBSCRIPTION_RUIM_SIM;
1640 } else {
1641 request.result = ((int[]) ar.result)[0];
1642 }
1643 notifyRequester(request);
1644 break;
Pengquan Menga1bb6272018-09-06 09:59:22 -07001645 case CMD_SET_CDMA_SUBSCRIPTION_MODE:
1646 request = (MainThreadRequest) msg.obj;
1647 onCompleted = obtainMessage(EVENT_SET_CDMA_SUBSCRIPTION_MODE_DONE, request);
1648 int subscriptionMode = (int) request.argument;
Sarah Chinbaab1432020-10-28 13:46:24 -07001649 getPhoneFromRequest(request).setCdmaSubscriptionMode(
1650 subscriptionMode, onCompleted);
Pengquan Menga1bb6272018-09-06 09:59:22 -07001651 break;
1652 case EVENT_SET_CDMA_SUBSCRIPTION_MODE_DONE:
1653 ar = (AsyncResult) msg.obj;
1654 request = (MainThreadRequest) ar.userObj;
1655 request.result = ar.exception == null;
1656 notifyRequester(request);
Holly Jiuyu Sun01c47ad2018-01-24 17:56:33 +00001657 break;
Nathan Harold3ff88932018-08-14 10:19:49 -07001658 case CMD_GET_ALL_CELL_INFO:
1659 request = (MainThreadRequest) msg.obj;
Nathan Harold3ff88932018-08-14 10:19:49 -07001660 onCompleted = obtainMessage(EVENT_GET_ALL_CELL_INFO_DONE, request);
Nathan Harold92bed182018-10-12 18:16:49 -07001661 request.phone.requestCellInfoUpdate(request.workSource, onCompleted);
Nathan Harold3ff88932018-08-14 10:19:49 -07001662 break;
Nathan Harold3ff88932018-08-14 10:19:49 -07001663 case EVENT_GET_ALL_CELL_INFO_DONE:
1664 ar = (AsyncResult) msg.obj;
1665 request = (MainThreadRequest) ar.userObj;
Nathan Harold8d0f1742018-10-02 12:14:47 -07001666 // If a timeout occurs, the response will be null
1667 request.result = (ar.exception == null && ar.result != null)
1668 ? ar.result : new ArrayList<CellInfo>();
Nathan Harold3ff88932018-08-14 10:19:49 -07001669 synchronized (request) {
1670 request.notifyAll();
1671 }
1672 break;
Nathan Haroldfa8da0f2018-09-27 18:51:29 -07001673 case CMD_REQUEST_CELL_INFO_UPDATE:
1674 request = (MainThreadRequest) msg.obj;
1675 request.phone.requestCellInfoUpdate(request.workSource,
1676 obtainMessage(EVENT_REQUEST_CELL_INFO_UPDATE_DONE, request));
1677 break;
1678 case EVENT_REQUEST_CELL_INFO_UPDATE_DONE:
1679 ar = (AsyncResult) msg.obj;
1680 request = (MainThreadRequest) ar.userObj;
1681 ICellInfoCallback cb = (ICellInfoCallback) request.argument;
1682 try {
1683 if (ar.exception != null) {
Nathan Haroldfa8da0f2018-09-27 18:51:29 -07001684 Log.e(LOG_TAG, "Exception retrieving CellInfo=" + ar.exception);
Meng Wangd8921f42019-09-30 17:13:54 -07001685 cb.onError(
1686 TelephonyManager.CellInfoCallback.ERROR_MODEM_ERROR,
1687 ar.exception.getClass().getName(),
1688 ar.exception.toString());
Nathan Haroldfa8da0f2018-09-27 18:51:29 -07001689 } else if (ar.result == null) {
Nathan Haroldfa8da0f2018-09-27 18:51:29 -07001690 Log.w(LOG_TAG, "Timeout Waiting for CellInfo!");
Meng Wangd8921f42019-09-30 17:13:54 -07001691 cb.onError(TelephonyManager.CellInfoCallback.ERROR_TIMEOUT, null, null);
Nathan Haroldfa8da0f2018-09-27 18:51:29 -07001692 } else {
1693 // use the result as returned
1694 cb.onCellInfo((List<CellInfo>) ar.result);
1695 }
1696 } catch (RemoteException re) {
1697 Log.w(LOG_TAG, "Discarded CellInfo due to Callback RemoteException");
1698 }
1699 break;
Sarah Chin679c08a2020-11-18 13:39:35 -08001700 case CMD_GET_CELL_LOCATION: {
Nathan Harold3ff88932018-08-14 10:19:49 -07001701 request = (MainThreadRequest) msg.obj;
1702 WorkSource ws = (WorkSource) request.argument;
1703 Phone phone = getPhoneFromRequest(request);
Meng Wanga10e89e2019-12-09 13:13:01 -08001704 phone.getCellIdentity(ws, obtainMessage(EVENT_GET_CELL_LOCATION_DONE, request));
Nathan Harold3ff88932018-08-14 10:19:49 -07001705 break;
Sarah Chin679c08a2020-11-18 13:39:35 -08001706 }
1707 case EVENT_GET_CELL_LOCATION_DONE: {
Nathan Harold3ff88932018-08-14 10:19:49 -07001708 ar = (AsyncResult) msg.obj;
1709 request = (MainThreadRequest) ar.userObj;
1710 if (ar.exception == null) {
1711 request.result = ar.result;
1712 } else {
Sarah Chin679c08a2020-11-18 13:39:35 -08001713 Phone phone = getPhoneFromRequest(request);
Nathan Harold3ff88932018-08-14 10:19:49 -07001714 request.result = (phone.getPhoneType() == PhoneConstants.PHONE_TYPE_CDMA)
Meng Wanga10e89e2019-12-09 13:13:01 -08001715 ? new CellIdentityCdma() : new CellIdentityGsm();
Nathan Harold3ff88932018-08-14 10:19:49 -07001716 }
1717
1718 synchronized (request) {
1719 request.notifyAll();
1720 }
1721 break;
Sarah Chin679c08a2020-11-18 13:39:35 -08001722 }
chen xu6dac5ab2018-10-26 17:39:23 -07001723 case CMD_MODEM_REBOOT:
1724 request = (MainThreadRequest) msg.obj;
1725 onCompleted = obtainMessage(EVENT_RESET_MODEM_CONFIG_DONE, request);
Nazanin Bakhshif782e562018-12-11 15:15:39 -08001726 defaultPhone.rebootModem(onCompleted);
chen xu6dac5ab2018-10-26 17:39:23 -07001727 break;
chen xu6dac5ab2018-10-26 17:39:23 -07001728 case EVENT_CMD_MODEM_REBOOT_DONE:
1729 handleNullReturnEvent(msg, "rebootModem");
1730 break;
Malcolm Chen8e4ed912019-01-15 20:22:16 -08001731 case CMD_REQUEST_ENABLE_MODEM:
1732 request = (MainThreadRequest) msg.obj;
1733 boolean enable = (boolean) request.argument;
1734 onCompleted = obtainMessage(EVENT_ENABLE_MODEM_DONE, request);
Nazanin Bakhshi33d584b2019-02-27 10:44:32 -08001735 onCompleted.arg1 = enable ? 1 : 0;
Malcolm Chen8e4ed912019-01-15 20:22:16 -08001736 PhoneConfigurationManager.getInstance()
1737 .enablePhone(request.phone, enable, onCompleted);
1738 break;
Michele Berionne5e411512020-11-13 02:36:59 +00001739 case EVENT_ENABLE_MODEM_DONE: {
Malcolm Chen8e4ed912019-01-15 20:22:16 -08001740 ar = (AsyncResult) msg.obj;
1741 request = (MainThreadRequest) ar.userObj;
1742 request.result = (ar.exception == null);
Nazanin Bakhshif71371d2019-04-29 17:29:44 -07001743 int phoneId = request.phone.getPhoneId();
Nazanin Bakhshi33d584b2019-02-27 10:44:32 -08001744 //update the cache as modem status has changed
Nazanin Bakhshif71371d2019-04-29 17:29:44 -07001745 if ((boolean) request.result) {
1746 mPhoneConfigurationManager.addToPhoneStatusCache(phoneId, msg.arg1 == 1);
1747 updateModemStateMetrics();
1748 } else {
1749 Log.e(LOG_TAG, msg.what + " failure. Not updating modem status."
1750 + ar.exception);
1751 }
1752 notifyRequester(request);
1753 break;
Michele Berionne5e411512020-11-13 02:36:59 +00001754 }
Nazanin Bakhshif71371d2019-04-29 17:29:44 -07001755 case CMD_GET_MODEM_STATUS:
1756 request = (MainThreadRequest) msg.obj;
1757 onCompleted = obtainMessage(EVENT_GET_MODEM_STATUS_DONE, request);
1758 PhoneConfigurationManager.getInstance()
1759 .getPhoneStatusFromModem(request.phone, onCompleted);
1760 break;
1761 case EVENT_GET_MODEM_STATUS_DONE:
1762 ar = (AsyncResult) msg.obj;
1763 request = (MainThreadRequest) ar.userObj;
1764 int id = request.phone.getPhoneId();
1765 if (ar.exception == null && ar.result != null) {
1766 request.result = ar.result;
1767 //update the cache as modem status has changed
1768 mPhoneConfigurationManager.addToPhoneStatusCache(id,
1769 (boolean) request.result);
1770 } else {
1771 // Return true if modem status cannot be retrieved. For most cases,
1772 // modem status is on. And for older version modems, GET_MODEM_STATUS
1773 // and disable modem are not supported. Modem is always on.
1774 // TODO: this should be fixed in R to support a third
1775 // status UNKNOWN b/131631629
1776 request.result = true;
1777 Log.e(LOG_TAG, msg.what + " failure. Not updating modem status."
1778 + ar.exception);
1779 }
Malcolm Chen8e4ed912019-01-15 20:22:16 -08001780 notifyRequester(request);
1781 break;
Hall Liu73f5d362020-01-20 13:42:00 -08001782 case CMD_SET_SYSTEM_SELECTION_CHANNELS: {
1783 request = (MainThreadRequest) msg.obj;
1784 onCompleted = obtainMessage(EVENT_SET_SYSTEM_SELECTION_CHANNELS_DONE, request);
1785 Pair<List<RadioAccessSpecifier>, Consumer<Boolean>> args =
1786 (Pair<List<RadioAccessSpecifier>, Consumer<Boolean>>) request.argument;
1787 request.phone.setSystemSelectionChannels(args.first, onCompleted);
1788 break;
1789 }
1790 case EVENT_SET_SYSTEM_SELECTION_CHANNELS_DONE: {
1791 ar = (AsyncResult) msg.obj;
1792 request = (MainThreadRequest) ar.userObj;
1793 Pair<List<RadioAccessSpecifier>, Consumer<Boolean>> args =
1794 (Pair<List<RadioAccessSpecifier>, Consumer<Boolean>>) request.argument;
1795 args.second.accept(ar.exception == null);
1796 notifyRequester(request);
1797 break;
1798 }
Sarah Chin679c08a2020-11-18 13:39:35 -08001799 case CMD_GET_SYSTEM_SELECTION_CHANNELS: {
1800 request = (MainThreadRequest) msg.obj;
1801 onCompleted = obtainMessage(EVENT_GET_SYSTEM_SELECTION_CHANNELS_DONE, request);
1802 Phone phone = getPhoneFromRequest(request);
1803 if (phone != null) {
1804 phone.getSystemSelectionChannels(onCompleted);
1805 } else {
1806 loge("getSystemSelectionChannels: No phone object");
1807 request.result = new ArrayList<RadioAccessSpecifier>();
1808 notifyRequester(request);
1809 }
1810 break;
1811 }
1812 case EVENT_GET_SYSTEM_SELECTION_CHANNELS_DONE:
1813 ar = (AsyncResult) msg.obj;
1814 request = (MainThreadRequest) ar.userObj;
1815 if (ar.exception == null && ar.result != null) {
1816 request.result = ar.result;
1817 } else {
Sarah Chin428d1d62021-03-13 03:17:40 -08001818 request.result = new IllegalStateException(
1819 "Failed to retrieve system selecton channels");
Sarah Chin679c08a2020-11-18 13:39:35 -08001820 if (ar.result == null) {
1821 loge("getSystemSelectionChannels: Empty response");
1822 } else {
1823 loge("getSystemSelectionChannels: Unknown exception");
1824 }
1825 }
1826 notifyRequester(request);
1827 break;
yincheng zhao2737e882019-09-06 17:06:54 -07001828 case EVENT_SET_FORBIDDEN_PLMNS_DONE:
1829 ar = (AsyncResult) msg.obj;
1830 request = (MainThreadRequest) ar.userObj;
1831 if (ar.exception == null && ar.result != null) {
1832 request.result = ar.result;
1833 } else {
1834 request.result = -1;
1835 loge("Failed to set Forbidden Plmns");
1836 if (ar.result == null) {
1837 loge("setForbidenPlmns: Empty response");
1838 } else if (ar.exception != null) {
1839 loge("setForbiddenPlmns: Exception: " + ar.exception);
1840 request.result = -1;
1841 } else {
1842 loge("setForbiddenPlmns: Unknown exception");
1843 }
1844 }
1845 notifyRequester(request);
1846 break;
1847 case CMD_SET_FORBIDDEN_PLMNS:
1848 request = (MainThreadRequest) msg.obj;
Muralidhar Reddy472c2ae2021-09-29 19:38:40 +00001849 uiccPort = getUiccPortFromRequest(request);
1850 if (uiccPort == null) {
1851 loge("setForbiddenPlmns: UiccPort is null");
yincheng zhao2737e882019-09-06 17:06:54 -07001852 request.result = -1;
1853 notifyRequester(request);
1854 break;
1855 }
1856 Pair<Integer, List<String>> setFplmnsArgs =
1857 (Pair<Integer, List<String>>) request.argument;
1858 appType = setFplmnsArgs.first;
1859 List<String> fplmns = setFplmnsArgs.second;
Muralidhar Reddy472c2ae2021-09-29 19:38:40 +00001860 uiccApp = uiccPort.getApplicationByType(appType);
yincheng zhao2737e882019-09-06 17:06:54 -07001861 if (uiccApp == null) {
1862 loge("setForbiddenPlmns: no app with specified type -- " + appType);
1863 request.result = -1;
1864 loge("Failed to get UICC App");
1865 notifyRequester(request);
1866 } else {
1867 onCompleted = obtainMessage(EVENT_SET_FORBIDDEN_PLMNS_DONE, request);
1868 ((SIMRecords) uiccApp.getIccRecords())
1869 .setForbiddenPlmns(onCompleted, fplmns);
1870 }
yinchengzhao4d163c02019-12-12 15:21:47 -08001871 break;
Naina Nallurid63128d2019-09-17 14:10:30 -07001872 case CMD_ERASE_MODEM_CONFIG:
1873 request = (MainThreadRequest) msg.obj;
1874 onCompleted = obtainMessage(EVENT_ERASE_MODEM_CONFIG_DONE, request);
1875 defaultPhone.eraseModemConfig(onCompleted);
1876 break;
1877 case EVENT_ERASE_MODEM_CONFIG_DONE:
1878 handleNullReturnEvent(msg, "eraseModemConfig");
yincheng zhao2737e882019-09-06 17:06:54 -07001879 break;
zoey chene02881a2019-12-30 16:11:23 +08001880
Kai Shif70f46f2021-03-03 13:59:46 -08001881 case CMD_ERASE_DATA_SHARED_PREFERENCES:
1882 request = (MainThreadRequest) msg.obj;
1883 request.result = defaultPhone.eraseDataInSharedPreferences();
1884 notifyRequester(request);
1885 break;
1886
zoey chene02881a2019-12-30 16:11:23 +08001887 case CMD_CHANGE_ICC_LOCK_PASSWORD:
1888 request = (MainThreadRequest) msg.obj;
1889 onCompleted = obtainMessage(EVENT_CHANGE_ICC_LOCK_PASSWORD_DONE, request);
1890 Pair<String, String> changed = (Pair<String, String>) request.argument;
1891 getPhoneFromRequest(request).getIccCard().changeIccLockPassword(
1892 changed.first, changed.second, onCompleted);
1893 break;
1894 case EVENT_CHANGE_ICC_LOCK_PASSWORD_DONE:
1895 ar = (AsyncResult) msg.obj;
1896 request = (MainThreadRequest) ar.userObj;
1897 if (ar.exception == null) {
1898 request.result = TelephonyManager.CHANGE_ICC_LOCK_SUCCESS;
Michele Berionne5e411512020-11-13 02:36:59 +00001899 // If the operation is successful, update the PIN storage
1900 Pair<String, String> passwords = (Pair<String, String>) request.argument;
1901 int phoneId = getPhoneFromRequest(request).getPhoneId();
Jon Spivack9c3bc762021-10-06 20:53:09 +00001902 UiccController.getInstance().getPinStorage()
1903 .storePin(passwords.second, phoneId);
zoey chene02881a2019-12-30 16:11:23 +08001904 } else {
1905 request.result = msg.arg1;
1906 }
1907 notifyRequester(request);
1908 break;
1909
Michele Berionne5e411512020-11-13 02:36:59 +00001910 case CMD_SET_ICC_LOCK_ENABLED: {
zoey chene02881a2019-12-30 16:11:23 +08001911 request = (MainThreadRequest) msg.obj;
1912 onCompleted = obtainMessage(EVENT_SET_ICC_LOCK_ENABLED_DONE, request);
1913 Pair<Boolean, String> enabled = (Pair<Boolean, String>) request.argument;
1914 getPhoneFromRequest(request).getIccCard().setIccLockEnabled(
1915 enabled.first, enabled.second, onCompleted);
1916 break;
Michele Berionne5e411512020-11-13 02:36:59 +00001917 }
zoey chene02881a2019-12-30 16:11:23 +08001918 case EVENT_SET_ICC_LOCK_ENABLED_DONE:
1919 ar = (AsyncResult) msg.obj;
1920 request = (MainThreadRequest) ar.userObj;
1921 if (ar.exception == null) {
1922 request.result = TelephonyManager.CHANGE_ICC_LOCK_SUCCESS;
Michele Berionne5e411512020-11-13 02:36:59 +00001923 // If the operation is successful, update the PIN storage
1924 Pair<Boolean, String> enabled = (Pair<Boolean, String>) request.argument;
1925 int phoneId = getPhoneFromRequest(request).getPhoneId();
1926 if (enabled.first) {
Jon Spivack9c3bc762021-10-06 20:53:09 +00001927 UiccController.getInstance().getPinStorage()
1928 .storePin(enabled.second, phoneId);
Michele Berionne5e411512020-11-13 02:36:59 +00001929 } else {
1930 UiccController.getInstance().getPinStorage().clearPin(phoneId);
1931 }
zoey chene02881a2019-12-30 16:11:23 +08001932 } else {
1933 request.result = msg.arg1;
1934 }
Michele Berionne5e411512020-11-13 02:36:59 +00001935
1936
zoey chene02881a2019-12-30 16:11:23 +08001937 notifyRequester(request);
1938 break;
1939
Peter Wangdafb9ac2020-01-15 14:13:38 -08001940 case MSG_NOTIFY_USER_ACTIVITY:
1941 removeMessages(MSG_NOTIFY_USER_ACTIVITY);
Peter Wang59571be2020-01-27 12:35:15 +08001942 Intent intent = new Intent(TelephonyIntents.ACTION_USER_ACTIVITY_NOTIFICATION);
Peter Wangdafb9ac2020-01-15 14:13:38 -08001943 intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY);
1944 getDefaultPhone().getContext().sendBroadcastAsUser(
1945 intent, UserHandle.ALL, permission.USER_ACTIVITY);
1946 break;
Jack Nudelmanb0b87642020-11-12 15:04:39 -08001947
1948 case CMD_SET_DATA_THROTTLING: {
1949 request = (MainThreadRequest) msg.obj;
1950 onCompleted = obtainMessage(EVENT_SET_DATA_THROTTLING_DONE, request);
1951 DataThrottlingRequest dataThrottlingRequest =
1952 (DataThrottlingRequest) request.argument;
1953 Phone phone = getPhoneFromRequest(request);
1954 if (phone != null) {
1955 phone.setDataThrottling(onCompleted,
1956 request.workSource, dataThrottlingRequest.getDataThrottlingAction(),
1957 dataThrottlingRequest.getCompletionDurationMillis());
1958 } else {
1959 loge("setDataThrottling: No phone object");
1960 request.result =
1961 TelephonyManager.THERMAL_MITIGATION_RESULT_MODEM_NOT_AVAILABLE;
1962 notifyRequester(request);
1963 }
1964
1965 break;
1966 }
1967 case EVENT_SET_DATA_THROTTLING_DONE:
1968 ar = (AsyncResult) msg.obj;
1969 request = (MainThreadRequest) ar.userObj;
1970
1971 if (ar.exception == null) {
1972 request.result = TelephonyManager.THERMAL_MITIGATION_RESULT_SUCCESS;
1973 } else if (ar.exception instanceof CommandException) {
1974 loge("setDataThrottling: CommandException: " + ar.exception);
1975 CommandException.Error error =
1976 ((CommandException) (ar.exception)).getCommandError();
1977
1978 if (error == CommandException.Error.RADIO_NOT_AVAILABLE) {
1979 request.result = TelephonyManager
1980 .THERMAL_MITIGATION_RESULT_MODEM_NOT_AVAILABLE;
1981 } else if (error == CommandException.Error.INVALID_ARGUMENTS) {
1982 request.result = SET_DATA_THROTTLING_MODEM_THREW_INVALID_PARAMS;
Jack Nudelman5d6a98b2021-03-04 14:26:25 -08001983 } else if (error == CommandException.Error.REQUEST_NOT_SUPPORTED) {
1984 request.result = MODEM_DOES_NOT_SUPPORT_DATA_THROTTLING_ERROR_CODE;
Jack Nudelmanb0b87642020-11-12 15:04:39 -08001985 } else {
1986 request.result =
1987 TelephonyManager.THERMAL_MITIGATION_RESULT_MODEM_ERROR;
1988 }
1989 } else {
1990 request.result = TelephonyManager.THERMAL_MITIGATION_RESULT_MODEM_ERROR;
1991 }
1992 Log.w(LOG_TAG, "DataThrottlingResult = " + request.result);
1993 notifyRequester(request);
1994 break;
Jordan Liu109698e2020-11-24 14:50:34 -08001995
1996 case CMD_SET_SIM_POWER: {
1997 request = (MainThreadRequest) msg.obj;
1998 onCompleted = obtainMessage(EVENT_SET_SIM_POWER_DONE, request);
1999 request = (MainThreadRequest) msg.obj;
2000 int stateToSet =
2001 ((Pair<Integer, IIntegerConsumer>)
2002 request.argument).first;
2003 request.phone.setSimPowerState(stateToSet, onCompleted, request.workSource);
2004 break;
2005 }
2006 case EVENT_SET_SIM_POWER_DONE: {
2007 ar = (AsyncResult) msg.obj;
2008 request = (MainThreadRequest) ar.userObj;
2009 IIntegerConsumer callback =
2010 ((Pair<Integer, IIntegerConsumer>) request.argument).second;
2011 if (ar.exception != null) {
2012 loge("setSimPower exception: " + ar.exception);
2013 int errorCode = TelephonyManager.CallForwardingInfoCallback
2014 .RESULT_ERROR_UNKNOWN;
2015 if (ar.exception instanceof CommandException) {
2016 CommandException.Error error =
2017 ((CommandException) (ar.exception)).getCommandError();
2018 if (error == CommandException.Error.SIM_ERR) {
2019 errorCode = TelephonyManager.SET_SIM_POWER_STATE_SIM_ERROR;
2020 } else if (error == CommandException.Error.INVALID_ARGUMENTS) {
2021 errorCode = TelephonyManager.SET_SIM_POWER_STATE_ALREADY_IN_STATE;
2022 } else if (error == CommandException.Error.REQUEST_NOT_SUPPORTED) {
2023 errorCode = TelephonyManager.SET_SIM_POWER_STATE_NOT_SUPPORTED;
2024 } else {
2025 errorCode = TelephonyManager.SET_SIM_POWER_STATE_MODEM_ERROR;
2026 }
2027 }
2028 try {
2029 callback.accept(errorCode);
2030 } catch (RemoteException e) {
2031 // Ignore if the remote process is no longer available to call back.
2032 Log.w(LOG_TAG, "setSimPower: callback not available.");
2033 }
2034 } else {
2035 try {
2036 callback.accept(TelephonyManager.SET_SIM_POWER_STATE_SUCCESS);
2037 } catch (RemoteException e) {
2038 // Ignore if the remote process is no longer available to call back.
2039 Log.w(LOG_TAG, "setSimPower: callback not available.");
2040 }
2041 }
2042 break;
2043 }
Rambo Wanga5cc9b72021-01-07 10:51:54 -08002044 case CMD_SET_SIGNAL_STRENGTH_UPDATE_REQUEST: {
2045 request = (MainThreadRequest) msg.obj;
2046
2047 final Phone phone = getPhoneFromRequest(request);
2048 if (phone == null || phone.getServiceStateTracker() == null) {
2049 request.result = new IllegalStateException("Phone or SST is null");
2050 notifyRequester(request);
2051 break;
2052 }
2053
2054 Pair<Integer, SignalStrengthUpdateRequest> pair =
2055 (Pair<Integer, SignalStrengthUpdateRequest>) request.argument;
2056 onCompleted = obtainMessage(EVENT_SET_SIGNAL_STRENGTH_UPDATE_REQUEST_DONE,
2057 request);
Rambo Wang6568f172021-02-03 16:56:47 -08002058 phone.getSignalStrengthController().setSignalStrengthUpdateRequest(
Rambo Wanga5cc9b72021-01-07 10:51:54 -08002059 request.subId, pair.first /*callingUid*/,
2060 pair.second /*request*/, onCompleted);
2061 break;
2062 }
2063 case EVENT_SET_SIGNAL_STRENGTH_UPDATE_REQUEST_DONE: {
2064 ar = (AsyncResult) msg.obj;
2065 request = (MainThreadRequest) ar.userObj;
2066 // request.result will be the exception of ar if present, true otherwise.
2067 // Be cautious not to leave result null which will wait() forever
2068 request.result = ar.exception != null ? ar.exception : true;
2069 notifyRequester(request);
2070 break;
2071 }
2072 case CMD_CLEAR_SIGNAL_STRENGTH_UPDATE_REQUEST: {
2073 request = (MainThreadRequest) msg.obj;
2074
2075 Phone phone = getPhoneFromRequest(request);
2076 if (phone == null || phone.getServiceStateTracker() == null) {
2077 request.result = new IllegalStateException("Phone or SST is null");
2078 notifyRequester(request);
2079 break;
2080 }
2081
2082 Pair<Integer, SignalStrengthUpdateRequest> pair =
2083 (Pair<Integer, SignalStrengthUpdateRequest>) request.argument;
2084 onCompleted = obtainMessage(EVENT_CLEAR_SIGNAL_STRENGTH_UPDATE_REQUEST_DONE,
2085 request);
Rambo Wang6568f172021-02-03 16:56:47 -08002086 phone.getSignalStrengthController().clearSignalStrengthUpdateRequest(
Rambo Wanga5cc9b72021-01-07 10:51:54 -08002087 request.subId, pair.first /*callingUid*/,
2088 pair.second /*request*/, onCompleted);
2089 break;
2090 }
2091 case EVENT_CLEAR_SIGNAL_STRENGTH_UPDATE_REQUEST_DONE: {
2092 ar = (AsyncResult) msg.obj;
2093 request = (MainThreadRequest) ar.userObj;
2094 request.result = ar.exception != null ? ar.exception : true;
2095 notifyRequester(request);
2096 break;
2097 }
Jordan Liu109698e2020-11-24 14:50:34 -08002098
Hongbo Zeng156aa4a2021-02-08 21:50:28 +08002099 case CMD_GET_SLICING_CONFIG: {
2100 request = (MainThreadRequest) msg.obj;
2101 onCompleted = obtainMessage(EVENT_GET_SLICING_CONFIG_DONE, request);
2102 request.phone.getSlicingConfig(onCompleted);
2103 break;
2104 }
2105 case EVENT_GET_SLICING_CONFIG_DONE: {
2106 ar = (AsyncResult) msg.obj;
2107 request = (MainThreadRequest) ar.userObj;
2108 ResultReceiver result = (ResultReceiver) request.argument;
2109
Hongbo Zeng0e18b162021-04-07 16:52:18 +08002110 NetworkSlicingConfig slicingConfig = null;
Hongbo Zeng156aa4a2021-02-08 21:50:28 +08002111 Bundle bundle = new Bundle();
2112 int resultCode = 0;
2113 if (ar.exception != null) {
2114 Log.e(LOG_TAG, "Exception retrieving slicing configuration="
2115 + ar.exception);
Hongbo Zeng0e18b162021-04-07 16:52:18 +08002116 resultCode = TelephonyManager.NetworkSlicingException.ERROR_MODEM_ERROR;
Hongbo Zeng156aa4a2021-02-08 21:50:28 +08002117 } else if (ar.result == null) {
2118 Log.w(LOG_TAG, "Timeout Waiting for slicing configuration!");
Hongbo Zeng0e18b162021-04-07 16:52:18 +08002119 resultCode = TelephonyManager.NetworkSlicingException.ERROR_TIMEOUT;
Hongbo Zeng156aa4a2021-02-08 21:50:28 +08002120 } else {
2121 // use the result as returned
Hongbo Zeng0e18b162021-04-07 16:52:18 +08002122 resultCode = TelephonyManager.NetworkSlicingException.SUCCESS;
2123 slicingConfig = (NetworkSlicingConfig) ar.result;
Hongbo Zeng156aa4a2021-02-08 21:50:28 +08002124 }
2125
2126 if (slicingConfig == null) {
Hongbo Zeng0e18b162021-04-07 16:52:18 +08002127 slicingConfig = new NetworkSlicingConfig();
Hongbo Zeng156aa4a2021-02-08 21:50:28 +08002128 }
2129 bundle.putParcelable(TelephonyManager.KEY_SLICING_CONFIG_HANDLE, slicingConfig);
2130 result.send(resultCode, bundle);
2131 notifyRequester(request);
2132 break;
2133 }
2134
Michele Berionne5e411512020-11-13 02:36:59 +00002135 case CMD_PREPARE_UNATTENDED_REBOOT:
2136 request = (MainThreadRequest) msg.obj;
2137 request.result =
Rafael Higuera Silvad9630642021-09-20 15:32:01 +00002138 UiccController.getInstance().getPinStorage()
2139 .prepareUnattendedReboot(request.workSource);
Michele Berionne5e411512020-11-13 02:36:59 +00002140 notifyRequester(request);
2141 break;
2142
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002143 default:
2144 Log.w(LOG_TAG, "MainThreadHandler: unexpected message code: " + msg.what);
2145 break;
2146 }
2147 }
Jake Hambye994d462014-02-03 13:10:13 -08002148
Pengquan Menga1bb6272018-09-06 09:59:22 -07002149 private void notifyRequester(MainThreadRequest request) {
2150 synchronized (request) {
2151 request.notifyAll();
2152 }
2153 }
2154
Jake Hambye994d462014-02-03 13:10:13 -08002155 private void handleNullReturnEvent(Message msg, String command) {
2156 AsyncResult ar = (AsyncResult) msg.obj;
2157 MainThreadRequest request = (MainThreadRequest) ar.userObj;
2158 if (ar.exception == null) {
2159 request.result = true;
2160 } else {
2161 request.result = false;
2162 if (ar.exception instanceof CommandException) {
2163 loge(command + ": CommandException: " + ar.exception);
2164 } else {
2165 loge(command + ": Unknown exception");
2166 }
2167 }
Pengquan Menga1bb6272018-09-06 09:59:22 -07002168 notifyRequester(request);
Jake Hambye994d462014-02-03 13:10:13 -08002169 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002170 }
2171
2172 /**
2173 * Posts the specified command to be executed on the main thread,
2174 * waits for the request to complete, and returns the result.
2175 * @see #sendRequestAsync
2176 */
2177 private Object sendRequest(int command, Object argument) {
Rambo Wang0f050d82021-02-12 11:43:36 -08002178 return sendRequest(command, argument, SubscriptionManager.INVALID_SUBSCRIPTION_ID, null,
2179 null, -1 /*timeoutInMs*/);
vagdeviaf9a5b92018-08-15 16:01:53 -07002180 }
2181
2182 /**
2183 * Posts the specified command to be executed on the main thread,
2184 * waits for the request to complete, and returns the result.
2185 * @see #sendRequestAsync
2186 */
2187 private Object sendRequest(int command, Object argument, WorkSource workSource) {
2188 return sendRequest(command, argument, SubscriptionManager.INVALID_SUBSCRIPTION_ID,
Rambo Wang0f050d82021-02-12 11:43:36 -08002189 null, workSource, -1 /*timeoutInMs*/);
Wink Saville36469e72014-06-11 15:17:00 -07002190 }
2191
2192 /**
2193 * Posts the specified command to be executed on the main thread,
2194 * waits for the request to complete, and returns the result.
2195 * @see #sendRequestAsync
2196 */
Shishir Agrawal76d5da92014-11-09 16:17:25 -08002197 private Object sendRequest(int command, Object argument, Integer subId) {
Rambo Wang0f050d82021-02-12 11:43:36 -08002198 return sendRequest(command, argument, subId, null, null, -1 /*timeoutInMs*/);
2199 }
2200
2201 /**
2202 * Posts the specified command to be executed on the main thread,
2203 * waits for the request to complete for at most {@code timeoutInMs}, and returns the result
2204 * if not timeout or null otherwise.
2205 * @see #sendRequestAsync
2206 */
2207 private @Nullable Object sendRequest(int command, Object argument, Integer subId,
2208 long timeoutInMs) {
2209 return sendRequest(command, argument, subId, null, null, timeoutInMs);
vagdeviaf9a5b92018-08-15 16:01:53 -07002210 }
2211
2212 /**
2213 * Posts the specified command to be executed on the main thread,
2214 * waits for the request to complete, and returns the result.
2215 * @see #sendRequestAsync
2216 */
Nathan Harold92bed182018-10-12 18:16:49 -07002217 private Object sendRequest(int command, Object argument, int subId, WorkSource workSource) {
Rambo Wang0f050d82021-02-12 11:43:36 -08002218 return sendRequest(command, argument, subId, null, workSource, -1 /*timeoutInMs*/);
Nathan Harold92bed182018-10-12 18:16:49 -07002219 }
2220
2221 /**
2222 * Posts the specified command to be executed on the main thread,
2223 * waits for the request to complete, and returns the result.
2224 * @see #sendRequestAsync
2225 */
2226 private Object sendRequest(int command, Object argument, Phone phone, WorkSource workSource) {
Rambo Wang0f050d82021-02-12 11:43:36 -08002227 return sendRequest(command, argument, SubscriptionManager.INVALID_SUBSCRIPTION_ID, phone,
2228 workSource, -1 /*timeoutInMs*/);
Nathan Harold92bed182018-10-12 18:16:49 -07002229 }
2230
2231 /**
Rambo Wang0f050d82021-02-12 11:43:36 -08002232 * Posts the specified command to be executed on the main thread. If {@code timeoutInMs} is
2233 * negative, waits for the request to complete, and returns the result. Otherwise, wait for
2234 * maximum of {@code timeoutInMs} milliseconds, interrupt and return null.
Nathan Harold92bed182018-10-12 18:16:49 -07002235 * @see #sendRequestAsync
2236 */
Rambo Wang0f050d82021-02-12 11:43:36 -08002237 private @Nullable Object sendRequest(int command, Object argument, Integer subId, Phone phone,
2238 WorkSource workSource, long timeoutInMs) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002239 if (Looper.myLooper() == mMainThreadHandler.getLooper()) {
2240 throw new RuntimeException("This method will deadlock if called from the main thread.");
2241 }
2242
Nathan Harold92bed182018-10-12 18:16:49 -07002243 MainThreadRequest request = null;
2244 if (subId != SubscriptionManager.INVALID_SUBSCRIPTION_ID && phone != null) {
2245 throw new IllegalArgumentException("subId and phone cannot both be specified!");
2246 } else if (phone != null) {
2247 request = new MainThreadRequest(argument, phone, workSource);
2248 } else {
2249 request = new MainThreadRequest(argument, subId, workSource);
2250 }
2251
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002252 Message msg = mMainThreadHandler.obtainMessage(command, request);
2253 msg.sendToTarget();
2254
Rambo Wang0f050d82021-02-12 11:43:36 -08002255
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002256 synchronized (request) {
Rambo Wang0f050d82021-02-12 11:43:36 -08002257 if (timeoutInMs >= 0) {
2258 // Wait for at least timeoutInMs before returning null request result
2259 long now = SystemClock.elapsedRealtime();
2260 long deadline = now + timeoutInMs;
Grace Jia8a0a1e82021-05-23 22:59:52 -07002261 while (request.result == null && now < deadline) {
Rambo Wang0f050d82021-02-12 11:43:36 -08002262 try {
2263 request.wait(deadline - now);
2264 } catch (InterruptedException e) {
2265 // Do nothing, go back and check if request is completed or timeout
2266 } finally {
2267 now = SystemClock.elapsedRealtime();
2268 }
2269 }
2270 } else {
2271 // Wait for the request to complete
2272 while (request.result == null) {
2273 try {
2274 request.wait();
2275 } catch (InterruptedException e) {
2276 // Do nothing, go back and wait until the request is complete
2277 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002278 }
2279 }
2280 }
Rambo Wang0f050d82021-02-12 11:43:36 -08002281 if (request.result == null) {
2282 Log.wtf(LOG_TAG,
2283 "sendRequest: Blocking command timed out. Something has gone terribly wrong.");
2284 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002285 return request.result;
2286 }
2287
2288 /**
2289 * Asynchronous ("fire and forget") version of sendRequest():
2290 * Posts the specified command to be executed on the main thread, and
2291 * returns immediately.
2292 * @see #sendRequest
2293 */
2294 private void sendRequestAsync(int command) {
2295 mMainThreadHandler.sendEmptyMessage(command);
2296 }
2297
2298 /**
Sailesh Nepalbd76e4e2013-10-27 13:59:44 -07002299 * Same as {@link #sendRequestAsync(int)} except it takes an argument.
Nathan Haroldfa8da0f2018-09-27 18:51:29 -07002300 * @see {@link #sendRequest(int)}
Sailesh Nepalbd76e4e2013-10-27 13:59:44 -07002301 */
2302 private void sendRequestAsync(int command, Object argument) {
Nathan Haroldfa8da0f2018-09-27 18:51:29 -07002303 sendRequestAsync(command, argument, null, null);
2304 }
2305
2306 /**
2307 * Same as {@link #sendRequestAsync(int,Object)} except it takes a Phone and WorkSource.
2308 * @see {@link #sendRequest(int,Object)}
2309 */
2310 private void sendRequestAsync(
2311 int command, Object argument, Phone phone, WorkSource workSource) {
2312 MainThreadRequest request = new MainThreadRequest(argument, phone, workSource);
Sailesh Nepalbd76e4e2013-10-27 13:59:44 -07002313 Message msg = mMainThreadHandler.obtainMessage(command, request);
2314 msg.sendToTarget();
2315 }
2316
2317 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002318 * Initialize the singleton PhoneInterfaceManager instance.
2319 * This is only done once, at startup, from PhoneApp.onCreate().
2320 */
Nazanin Bakhshif782e562018-12-11 15:15:39 -08002321 /* package */ static PhoneInterfaceManager init(PhoneGlobals app) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002322 synchronized (PhoneInterfaceManager.class) {
2323 if (sInstance == null) {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08002324 sInstance = new PhoneInterfaceManager(app);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002325 } else {
2326 Log.wtf(LOG_TAG, "init() called multiple times! sInstance = " + sInstance);
2327 }
2328 return sInstance;
2329 }
2330 }
2331
2332 /** Private constructor; @see init() */
Jordan Liu1979a042020-03-20 21:39:35 +00002333 private PhoneInterfaceManager(PhoneGlobals app) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002334 mApp = app;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002335 mCM = PhoneGlobals.getInstance().mCM;
Brad Ebingerd1947d82021-05-17 20:54:49 +00002336 mImsResolver = ImsResolver.getInstance();
Stuart Scott981d8582015-04-21 14:09:50 -07002337 mUserManager = (UserManager) app.getSystemService(Context.USER_SERVICE);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002338 mAppOps = (AppOpsManager)app.getSystemService(Context.APP_OPS_SERVICE);
Grace Jia0ddb3612021-04-22 13:35:26 -07002339 mPm = app.getSystemService(PackageManager.class);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002340 mMainThreadHandler = new MainThreadHandler();
Tobias Thiererb19e1f12018-12-11 17:54:03 +00002341 mSubscriptionController = SubscriptionController.getInstance();
Nazanin Bakhshif782e562018-12-11 15:15:39 -08002342 mTelephonySharedPreferences =
2343 PreferenceManager.getDefaultSharedPreferences(mApp);
yinxub1bed742017-04-17 11:45:04 -07002344 mNetworkScanRequestTracker = new NetworkScanRequestTracker();
Malcolm Chen2c63d402018-08-14 16:00:53 -07002345 mPhoneConfigurationManager = PhoneConfigurationManager.getInstance();
Daniel Bright94f43662021-03-01 14:43:40 -08002346 mRadioInterfaceCapabilities = RadioInterfaceCapabilityController.getInstance();
Peter Wanga3cf4ac2020-01-27 09:39:46 +08002347 mNotifyUserActivity = new AtomicBoolean(false);
Wink Saville3ab207e2014-11-20 13:07:20 -08002348
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002349 publish();
2350 }
2351
Nazanin Bakhshif782e562018-12-11 15:15:39 -08002352 private Phone getDefaultPhone() {
2353 Phone thePhone = getPhone(getDefaultSubscription());
2354 return (thePhone != null) ? thePhone : PhoneFactory.getDefaultPhone();
2355 }
2356
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002357 private void publish() {
2358 if (DBG) log("publish: " + this);
2359
Peter Wangc035ce42020-01-08 21:00:22 -08002360 TelephonyFrameworkInitializer
2361 .getTelephonyServiceManager()
2362 .getTelephonyServiceRegisterer()
2363 .register(this);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002364 }
2365
Stuart Scott584921c2015-01-15 17:10:34 -08002366 private Phone getPhoneFromRequest(MainThreadRequest request) {
Jordan Liu4c733742019-02-28 12:03:40 -08002367 if (request.phone != null) {
2368 return request.phone;
2369 } else {
2370 return getPhoneFromSubId(request.subId);
2371 }
2372 }
2373
2374 private Phone getPhoneFromSubId(int subId) {
2375 return (subId == SubscriptionManager.INVALID_SUBSCRIPTION_ID)
2376 ? getDefaultPhone() : getPhone(subId);
Stuart Scott584921c2015-01-15 17:10:34 -08002377 }
2378
Muralidhar Reddy472c2ae2021-09-29 19:38:40 +00002379 private UiccPort getUiccPortFromRequest(MainThreadRequest request) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002380 Phone phone = getPhoneFromRequest(request);
2381 return phone == null ? null :
Muralidhar Reddy472c2ae2021-09-29 19:38:40 +00002382 UiccController.getInstance().getUiccPort(phone.getPhoneId());
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002383 }
2384
Wink Saville36469e72014-06-11 15:17:00 -07002385 // returns phone associated with the subId.
Wink Savilleb564aae2014-10-23 10:18:09 -07002386 private Phone getPhone(int subId) {
Wink Savilleac1bdfd2014-11-20 23:04:44 -08002387 return PhoneFactory.getPhone(mSubscriptionController.getPhoneId(subId));
Wink Saville36469e72014-06-11 15:17:00 -07002388 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002389
Kai Shif70f46f2021-03-03 13:59:46 -08002390 private void sendEraseModemConfig(@NonNull Phone phone) {
2391 Boolean success = (Boolean) sendRequest(CMD_ERASE_MODEM_CONFIG, null);
2392 if (DBG) log("eraseModemConfig:" + ' ' + (success ? "ok" : "fail"));
2393 }
2394
2395 private void sendEraseDataInSharedPreferences(@NonNull Phone phone) {
2396 Boolean success = (Boolean) sendRequest(CMD_ERASE_DATA_SHARED_PREFERENCES, null);
2397 if (DBG) log("eraseDataInSharedPreferences:" + ' ' + (success ? "ok" : "fail"));
Naina Nallurid63128d2019-09-17 14:10:30 -07002398 }
2399
Peter Wang44b186e2020-01-13 23:33:09 -08002400 private boolean isImsAvailableOnDevice() {
2401 PackageManager pm = getDefaultPhone().getContext().getPackageManager();
2402 if (pm == null) {
2403 // For some reason package manger is not available.. This will fail internally anyway,
2404 // so do not throw error and allow.
2405 return true;
2406 }
2407 return pm.hasSystemFeature(PackageManager.FEATURE_TELEPHONY_IMS, 0);
2408 }
2409
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002410 public void dial(String number) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07002411 dialForSubscriber(getPreferredVoiceSubscription(), number);
Wink Saville36469e72014-06-11 15:17:00 -07002412 }
2413
Wink Savilleb564aae2014-10-23 10:18:09 -07002414 public void dialForSubscriber(int subId, String number) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002415 if (DBG) log("dial: " + number);
2416 // No permission check needed here: This is just a wrapper around the
2417 // ACTION_DIAL intent, which is available to any app since it puts up
2418 // the UI before it does anything.
2419
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002420 final long identity = Binder.clearCallingIdentity();
2421 try {
2422 String url = createTelUrl(number);
2423 if (url == null) {
2424 return;
2425 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002426
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002427 // PENDING: should we just silently fail if phone is offhook or ringing?
2428 PhoneConstants.State state = mCM.getState(subId);
2429 if (state != PhoneConstants.State.OFFHOOK && state != PhoneConstants.State.RINGING) {
2430 Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse(url));
2431 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
2432 mApp.startActivity(intent);
2433 }
2434 } finally {
2435 Binder.restoreCallingIdentity(identity);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002436 }
2437 }
2438
2439 public void call(String callingPackage, String number) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07002440 callForSubscriber(getPreferredVoiceSubscription(), callingPackage, number);
Wink Saville36469e72014-06-11 15:17:00 -07002441 }
2442
Wink Savilleb564aae2014-10-23 10:18:09 -07002443 public void callForSubscriber(int subId, String callingPackage, String number) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002444 if (DBG) log("call: " + number);
2445
2446 // This is just a wrapper around the ACTION_CALL intent, but we still
2447 // need to do a permission check since we're calling startActivity()
2448 // from the context of the phone app.
2449 enforceCallPermission();
2450
Jordan Liu1617b712019-07-10 15:06:26 -07002451 if (mAppOps.noteOp(AppOpsManager.OPSTR_CALL_PHONE, Binder.getCallingUid(), callingPackage)
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002452 != AppOpsManager.MODE_ALLOWED) {
2453 return;
2454 }
2455
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002456 final long identity = Binder.clearCallingIdentity();
2457 try {
2458 String url = createTelUrl(number);
2459 if (url == null) {
2460 return;
2461 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002462
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002463 boolean isValid = false;
2464 final List<SubscriptionInfo> slist = getActiveSubscriptionInfoListPrivileged();
2465 if (slist != null) {
2466 for (SubscriptionInfo subInfoRecord : slist) {
2467 if (subInfoRecord.getSubscriptionId() == subId) {
2468 isValid = true;
2469 break;
2470 }
Wink Saville3ab207e2014-11-20 13:07:20 -08002471 }
Wink Saville08874612014-08-31 19:19:58 -07002472 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002473 if (!isValid) {
2474 return;
2475 }
Wink Saville08874612014-08-31 19:19:58 -07002476
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002477 Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse(url));
2478 intent.putExtra(SUBSCRIPTION_KEY, subId);
2479 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
2480 mApp.startActivity(intent);
2481 } finally {
2482 Binder.restoreCallingIdentity(identity);
2483 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002484 }
2485
Wink Savilleb564aae2014-10-23 10:18:09 -07002486 public boolean supplyPinForSubscriber(int subId, String pin) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07002487 int [] resultArray = supplyPinReportResultForSubscriber(subId, pin);
Wink Saville9de0f752013-10-22 19:04:03 -07002488 return (resultArray[0] == PhoneConstants.PIN_RESULT_SUCCESS) ? true : false;
2489 }
2490
Wink Savilleb564aae2014-10-23 10:18:09 -07002491 public boolean supplyPukForSubscriber(int subId, String puk, String pin) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07002492 int [] resultArray = supplyPukReportResultForSubscriber(subId, puk, pin);
Wink Saville9de0f752013-10-22 19:04:03 -07002493 return (resultArray[0] == PhoneConstants.PIN_RESULT_SUCCESS) ? true : false;
2494 }
2495
Wink Savilleb564aae2014-10-23 10:18:09 -07002496 public int[] supplyPinReportResultForSubscriber(int subId, String pin) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002497 enforceModifyPermission();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002498
2499 final long identity = Binder.clearCallingIdentity();
2500 try {
Michele Berionne5e411512020-11-13 02:36:59 +00002501 Phone phone = getPhone(subId);
2502 final UnlockSim checkSimPin = new UnlockSim(phone.getPhoneId(), phone.getIccCard());
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002503 checkSimPin.start();
2504 return checkSimPin.unlockSim(null, pin);
2505 } finally {
2506 Binder.restoreCallingIdentity(identity);
2507 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002508 }
2509
Wink Savilleb564aae2014-10-23 10:18:09 -07002510 public int[] supplyPukReportResultForSubscriber(int subId, String puk, String pin) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002511 enforceModifyPermission();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002512
2513 final long identity = Binder.clearCallingIdentity();
2514 try {
Michele Berionne5e411512020-11-13 02:36:59 +00002515 Phone phone = getPhone(subId);
2516 final UnlockSim checkSimPuk = new UnlockSim(phone.getPhoneId(), phone.getIccCard());
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002517 checkSimPuk.start();
2518 return checkSimPuk.unlockSim(puk, pin);
2519 } finally {
2520 Binder.restoreCallingIdentity(identity);
2521 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002522 }
2523
2524 /**
Wink Saville9de0f752013-10-22 19:04:03 -07002525 * Helper thread to turn async call to SimCard#supplyPin into
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002526 * a synchronous one.
2527 */
2528 private static class UnlockSim extends Thread {
2529
2530 private final IccCard mSimCard;
Michele Berionne5e411512020-11-13 02:36:59 +00002531 private final int mPhoneId;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002532
2533 private boolean mDone = false;
Wink Saville9de0f752013-10-22 19:04:03 -07002534 private int mResult = PhoneConstants.PIN_GENERAL_FAILURE;
2535 private int mRetryCount = -1;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002536
2537 // For replies from SimCard interface
2538 private Handler mHandler;
2539
2540 // For async handler to identify request type
2541 private static final int SUPPLY_PIN_COMPLETE = 100;
2542
Michele Berionne5e411512020-11-13 02:36:59 +00002543 UnlockSim(int phoneId, IccCard simCard) {
2544 mPhoneId = phoneId;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002545 mSimCard = simCard;
2546 }
2547
2548 @Override
2549 public void run() {
2550 Looper.prepare();
2551 synchronized (UnlockSim.this) {
2552 mHandler = new Handler() {
2553 @Override
2554 public void handleMessage(Message msg) {
2555 AsyncResult ar = (AsyncResult) msg.obj;
2556 switch (msg.what) {
2557 case SUPPLY_PIN_COMPLETE:
2558 Log.d(LOG_TAG, "SUPPLY_PIN_COMPLETE");
2559 synchronized (UnlockSim.this) {
Wink Saville9de0f752013-10-22 19:04:03 -07002560 mRetryCount = msg.arg1;
2561 if (ar.exception != null) {
2562 if (ar.exception instanceof CommandException &&
2563 ((CommandException)(ar.exception)).getCommandError()
2564 == CommandException.Error.PASSWORD_INCORRECT) {
2565 mResult = PhoneConstants.PIN_PASSWORD_INCORRECT;
vivi.lib5e9ada2019-09-12 16:04:24 +08002566 } //When UiccCardApp dispose,handle message and return exception
2567 else if (ar.exception instanceof CommandException &&
2568 ((CommandException) (ar.exception)).getCommandError()
2569 == CommandException.Error.ABORTED) {
2570 mResult = PhoneConstants.PIN_OPERATION_ABORTED;
Wink Saville9de0f752013-10-22 19:04:03 -07002571 } else {
2572 mResult = PhoneConstants.PIN_GENERAL_FAILURE;
2573 }
2574 } else {
2575 mResult = PhoneConstants.PIN_RESULT_SUCCESS;
2576 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002577 mDone = true;
2578 UnlockSim.this.notifyAll();
2579 }
2580 break;
2581 }
2582 }
2583 };
2584 UnlockSim.this.notifyAll();
2585 }
2586 Looper.loop();
2587 }
2588
2589 /*
2590 * Use PIN or PUK to unlock SIM card
2591 *
2592 * If PUK is null, unlock SIM card with PIN
2593 *
2594 * If PUK is not null, unlock SIM card with PUK and set PIN code
2595 */
Wink Saville9de0f752013-10-22 19:04:03 -07002596 synchronized int[] unlockSim(String puk, String pin) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002597
2598 while (mHandler == null) {
2599 try {
2600 wait();
2601 } catch (InterruptedException e) {
2602 Thread.currentThread().interrupt();
2603 }
2604 }
2605 Message callback = Message.obtain(mHandler, SUPPLY_PIN_COMPLETE);
2606
2607 if (puk == null) {
2608 mSimCard.supplyPin(pin, callback);
2609 } else {
2610 mSimCard.supplyPuk(puk, pin, callback);
2611 }
2612
2613 while (!mDone) {
2614 try {
2615 Log.d(LOG_TAG, "wait for done");
2616 wait();
2617 } catch (InterruptedException e) {
2618 // Restore the interrupted status
2619 Thread.currentThread().interrupt();
2620 }
2621 }
2622 Log.d(LOG_TAG, "done");
Wink Saville9de0f752013-10-22 19:04:03 -07002623 int[] resultArray = new int[2];
2624 resultArray[0] = mResult;
2625 resultArray[1] = mRetryCount;
Michele Berionne5e411512020-11-13 02:36:59 +00002626
2627 if (mResult == PhoneConstants.PIN_RESULT_SUCCESS && pin.length() > 0) {
Jon Spivack9c3bc762021-10-06 20:53:09 +00002628 UiccController.getInstance().getPinStorage().storePin(pin, mPhoneId);
Michele Berionne5e411512020-11-13 02:36:59 +00002629 }
2630
Wink Saville9de0f752013-10-22 19:04:03 -07002631 return resultArray;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002632 }
2633 }
2634
Nathan Harold7c8d0f12020-05-28 20:40:31 -07002635 /**
2636 * This method has been removed due to privacy and stability concerns.
2637 */
2638 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002639 public void updateServiceLocation() {
Nathan Harold7c8d0f12020-05-28 20:40:31 -07002640 Log.e(LOG_TAG, "Call to unsupported method updateServiceLocation()");
2641 return;
Wink Saville36469e72014-06-11 15:17:00 -07002642 }
2643
Nathan Harold1f889d82020-06-04 17:05:26 -07002644 @Override
2645 public void updateServiceLocationWithPackageName(String callingPackage) {
2646 mApp.getSystemService(AppOpsManager.class)
2647 .checkPackage(Binder.getCallingUid(), callingPackage);
2648
Nathan Haroldf096d982020-11-18 17:18:06 -08002649 final int targetSdk = TelephonyPermissions.getTargetSdk(mApp, callingPackage);
Nathan Harold1f889d82020-06-04 17:05:26 -07002650 if (targetSdk > android.os.Build.VERSION_CODES.R) {
2651 // Callers targeting S have no business invoking this method.
2652 return;
2653 }
2654
2655 LocationAccessPolicy.LocationPermissionResult locationResult =
2656 LocationAccessPolicy.checkLocationPermission(mApp,
2657 new LocationAccessPolicy.LocationPermissionQuery.Builder()
2658 .setCallingPackage(callingPackage)
2659 .setCallingFeatureId(null)
2660 .setCallingPid(Binder.getCallingPid())
2661 .setCallingUid(Binder.getCallingUid())
2662 .setMethod("updateServiceLocation")
2663 .setMinSdkVersionForCoarse(Build.VERSION_CODES.BASE)
2664 .setMinSdkVersionForFine(Build.VERSION_CODES.Q)
2665 .build());
2666 // Apps that lack location permission have no business calling this method;
2667 // however, because no permission was declared in the public API, denials must
2668 // all be "soft".
2669 switch (locationResult) {
2670 case DENIED_HARD: /* fall through */
2671 case DENIED_SOFT:
2672 return;
2673 }
2674
2675 WorkSource workSource = getWorkSource(Binder.getCallingUid());
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002676 final long identity = Binder.clearCallingIdentity();
2677 try {
Nathan Harold1f889d82020-06-04 17:05:26 -07002678 final Phone phone = getPhone(getDefaultSubscription());
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002679 if (phone != null) {
Nathan Harold1f889d82020-06-04 17:05:26 -07002680 phone.updateServiceLocation(workSource);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002681 }
2682 } finally {
2683 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07002684 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002685 }
2686
Philip P. Moltmann700a9592019-10-03 11:53:50 -07002687 @Deprecated
Robert Greenwalt36b23af2015-07-06 17:59:14 -07002688 @Override
2689 public boolean isRadioOn(String callingPackage) {
Philip P. Moltmann700a9592019-10-03 11:53:50 -07002690 return isRadioOnWithFeature(callingPackage, null);
2691 }
2692
2693
2694 @Override
2695 public boolean isRadioOnWithFeature(String callingPackage, String callingFeatureId) {
2696 return isRadioOnForSubscriberWithFeature(getDefaultSubscription(), callingPackage,
2697 callingFeatureId);
2698 }
2699
2700 @Deprecated
2701 @Override
2702 public boolean isRadioOnForSubscriber(int subId, String callingPackage) {
2703 return isRadioOnForSubscriberWithFeature(subId, callingPackage, null);
Wink Saville36469e72014-06-11 15:17:00 -07002704 }
2705
Robert Greenwalt36b23af2015-07-06 17:59:14 -07002706 @Override
Philip P. Moltmann700a9592019-10-03 11:53:50 -07002707 public boolean isRadioOnForSubscriberWithFeature(int subId, String callingPackage,
2708 String callingFeatureId) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08002709 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
Philip P. Moltmann700a9592019-10-03 11:53:50 -07002710 mApp, subId, callingPackage, callingFeatureId, "isRadioOnForSubscriber")) {
Robert Greenwalt36b23af2015-07-06 17:59:14 -07002711 return false;
2712 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002713
2714 final long identity = Binder.clearCallingIdentity();
2715 try {
2716 return isRadioOnForSubscriber(subId);
2717 } finally {
2718 Binder.restoreCallingIdentity(identity);
2719 }
Robert Greenwalt36b23af2015-07-06 17:59:14 -07002720 }
2721
2722 private boolean isRadioOnForSubscriber(int subId) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002723 final long identity = Binder.clearCallingIdentity();
2724 try {
2725 final Phone phone = getPhone(subId);
2726 if (phone != null) {
2727 return phone.getServiceState().getState() != ServiceState.STATE_POWER_OFF;
2728 } else {
2729 return false;
2730 }
2731 } finally {
2732 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07002733 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002734 }
2735
2736 public void toggleRadioOnOff() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07002737 toggleRadioOnOffForSubscriber(getDefaultSubscription());
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002738 }
Wink Saville36469e72014-06-11 15:17:00 -07002739
Wink Savilleb564aae2014-10-23 10:18:09 -07002740 public void toggleRadioOnOffForSubscriber(int subId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002741 enforceModifyPermission();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002742
2743 final long identity = Binder.clearCallingIdentity();
2744 try {
2745 final Phone phone = getPhone(subId);
2746 if (phone != null) {
2747 phone.setRadioPower(!isRadioOnForSubscriber(subId));
2748 }
2749 } finally {
2750 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07002751 }
Wink Saville36469e72014-06-11 15:17:00 -07002752 }
2753
2754 public boolean setRadio(boolean turnOn) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07002755 return setRadioForSubscriber(getDefaultSubscription(), turnOn);
Wink Saville36469e72014-06-11 15:17:00 -07002756 }
2757
Wink Savilleb564aae2014-10-23 10:18:09 -07002758 public boolean setRadioForSubscriber(int subId, boolean turnOn) {
Wink Saville36469e72014-06-11 15:17:00 -07002759 enforceModifyPermission();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002760
2761 final long identity = Binder.clearCallingIdentity();
2762 try {
2763 final Phone phone = getPhone(subId);
2764 if (phone == null) {
2765 return false;
2766 }
2767 if ((phone.getServiceState().getState() != ServiceState.STATE_POWER_OFF) != turnOn) {
2768 toggleRadioOnOffForSubscriber(subId);
2769 }
2770 return true;
2771 } finally {
2772 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07002773 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002774 }
Wink Saville36469e72014-06-11 15:17:00 -07002775
Naveen Kalla1fd79bd2014-08-08 00:48:59 -07002776 public boolean needMobileRadioShutdown() {
Shuo Qianfa7b6b32019-12-10 10:40:38 -08002777 enforceReadPrivilegedPermission("needMobileRadioShutdown");
Naveen Kalla1fd79bd2014-08-08 00:48:59 -07002778 /*
2779 * If any of the Radios are available, it will need to be
2780 * shutdown. So return true if any Radio is available.
2781 */
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002782 final long identity = Binder.clearCallingIdentity();
2783 try {
2784 for (int i = 0; i < TelephonyManager.getDefault().getPhoneCount(); i++) {
2785 Phone phone = PhoneFactory.getPhone(i);
2786 if (phone != null && phone.isRadioAvailable()) return true;
2787 }
2788 logv(TelephonyManager.getDefault().getPhoneCount() + " Phones are shutdown.");
2789 return false;
2790 } finally {
2791 Binder.restoreCallingIdentity(identity);
Naveen Kalla1fd79bd2014-08-08 00:48:59 -07002792 }
Naveen Kalla1fd79bd2014-08-08 00:48:59 -07002793 }
2794
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002795 @Override
Naveen Kalla1fd79bd2014-08-08 00:48:59 -07002796 public void shutdownMobileRadios() {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002797 enforceModifyPermission();
2798
2799 final long identity = Binder.clearCallingIdentity();
2800 try {
2801 for (int i = 0; i < TelephonyManager.getDefault().getPhoneCount(); i++) {
2802 logv("Shutting down Phone " + i);
2803 shutdownRadioUsingPhoneId(i);
2804 }
2805 } finally {
2806 Binder.restoreCallingIdentity(identity);
Naveen Kalla1fd79bd2014-08-08 00:48:59 -07002807 }
2808 }
2809
2810 private void shutdownRadioUsingPhoneId(int phoneId) {
Naveen Kalla1fd79bd2014-08-08 00:48:59 -07002811 Phone phone = PhoneFactory.getPhone(phoneId);
2812 if (phone != null && phone.isRadioAvailable()) {
2813 phone.shutdownRadio();
2814 }
2815 }
2816
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002817 public boolean setRadioPower(boolean turnOn) {
Jack Yub4e16162017-05-15 12:48:40 -07002818 enforceModifyPermission();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002819
2820 final long identity = Binder.clearCallingIdentity();
2821 try {
2822 final Phone defaultPhone = PhoneFactory.getDefaultPhone();
2823 if (defaultPhone != null) {
2824 defaultPhone.setRadioPower(turnOn);
2825 return true;
2826 } else {
2827 loge("There's no default phone.");
2828 return false;
2829 }
2830 } finally {
2831 Binder.restoreCallingIdentity(identity);
Wei Liu9ae2a062016-08-08 11:09:34 -07002832 }
Wink Saville36469e72014-06-11 15:17:00 -07002833 }
2834
Wink Savilleb564aae2014-10-23 10:18:09 -07002835 public boolean setRadioPowerForSubscriber(int subId, boolean turnOn) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002836 enforceModifyPermission();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002837
2838 final long identity = Binder.clearCallingIdentity();
2839 try {
2840 final Phone phone = getPhone(subId);
2841 if (phone != null) {
2842 phone.setRadioPower(turnOn);
2843 return true;
2844 } else {
2845 return false;
2846 }
2847 } finally {
2848 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07002849 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002850 }
2851
Wink Saville36469e72014-06-11 15:17:00 -07002852 // FIXME: subId version needed
Sanket Padawe356d7632015-06-22 14:03:32 -07002853 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002854 public boolean enableDataConnectivity() {
2855 enforceModifyPermission();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002856
2857 final long identity = Binder.clearCallingIdentity();
2858 try {
2859 int subId = mSubscriptionController.getDefaultDataSubId();
2860 final Phone phone = getPhone(subId);
2861 if (phone != null) {
Jack Yu99e87332021-12-17 23:14:15 -08002862 if (phone.isUsingNewDataStack()) {
2863 phone.getDataSettingsManager().setDataEnabled(
2864 TelephonyManager.DATA_ENABLED_REASON_USER, true);
2865 } else {
2866 phone.getDataEnabledSettings().setDataEnabled(
2867 TelephonyManager.DATA_ENABLED_REASON_USER, true);
2868 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002869 return true;
2870 } else {
2871 return false;
2872 }
2873 } finally {
2874 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07002875 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002876 }
2877
Wink Saville36469e72014-06-11 15:17:00 -07002878 // FIXME: subId version needed
Sanket Padawe356d7632015-06-22 14:03:32 -07002879 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002880 public boolean disableDataConnectivity() {
2881 enforceModifyPermission();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002882
2883 final long identity = Binder.clearCallingIdentity();
2884 try {
2885 int subId = mSubscriptionController.getDefaultDataSubId();
2886 final Phone phone = getPhone(subId);
2887 if (phone != null) {
Jack Yu99e87332021-12-17 23:14:15 -08002888 if (phone.isUsingNewDataStack()) {
2889 phone.getDataSettingsManager().setDataEnabled(
2890 TelephonyManager.DATA_ENABLED_REASON_USER, false);
2891 } else {
2892 phone.getDataEnabledSettings().setDataEnabled(
2893 TelephonyManager.DATA_ENABLED_REASON_USER, false);
2894 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002895 return true;
2896 } else {
2897 return false;
2898 }
2899 } finally {
2900 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07002901 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002902 }
2903
Sanket Padawe356d7632015-06-22 14:03:32 -07002904 @Override
Jack Yuacf8a132017-05-01 17:00:48 -07002905 public boolean isDataConnectivityPossible(int subId) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002906 final long identity = Binder.clearCallingIdentity();
2907 try {
2908 final Phone phone = getPhone(subId);
2909 if (phone != null) {
Jack Yub5d8f642018-11-26 11:20:48 -08002910 return phone.isDataAllowed(ApnSetting.TYPE_DEFAULT);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002911 } else {
2912 return false;
2913 }
2914 } finally {
2915 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07002916 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002917 }
2918
2919 public boolean handlePinMmi(String dialString) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07002920 return handlePinMmiForSubscriber(getDefaultSubscription(), dialString);
Wink Saville36469e72014-06-11 15:17:00 -07002921 }
2922
pkanwarae03a6b2016-11-06 20:37:09 -08002923 public void handleUssdRequest(int subId, String ussdRequest, ResultReceiver wrappedCallback) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002924 enforceCallPermission();
2925
2926 final long identity = Binder.clearCallingIdentity();
2927 try {
2928 if (!SubscriptionManager.isValidSubscriptionId(subId)) {
2929 return;
2930 }
2931 Pair<String, ResultReceiver> ussdObject = new Pair(ussdRequest, wrappedCallback);
2932 sendRequest(CMD_HANDLE_USSD_REQUEST, ussdObject, subId);
2933 } finally {
2934 Binder.restoreCallingIdentity(identity);
2935 }
pkanwar32d516d2016-10-14 19:37:38 -07002936 };
2937
Wink Savilleb564aae2014-10-23 10:18:09 -07002938 public boolean handlePinMmiForSubscriber(int subId, String dialString) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002939 enforceModifyPermission();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002940
2941 final long identity = Binder.clearCallingIdentity();
2942 try {
2943 if (!SubscriptionManager.isValidSubscriptionId(subId)) {
2944 return false;
2945 }
2946 return (Boolean) sendRequest(CMD_HANDLE_PIN_MMI, dialString, subId);
2947 } finally {
2948 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07002949 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002950 }
2951
Brad Ebinger4f6208e2021-03-23 21:04:45 +00002952 /**
2953 * @deprecated This method is deprecated and is only being kept due to an UnsupportedAppUsage
2954 * tag on getCallState Binder call.
2955 */
2956 @Deprecated
2957 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002958 public int getCallState() {
Brad Ebinger4f6208e2021-03-23 21:04:45 +00002959 if (CompatChanges.isChangeEnabled(
2960 TelecomManager.ENABLE_GET_CALL_STATE_PERMISSION_PROTECTION,
2961 Binder.getCallingUid())) {
2962 // Do not allow this API to be called on API version 31+, it should only be
2963 // called on old apps using this Binder call directly.
2964 throw new SecurityException("This method can only be used for applications "
2965 + "targeting API version 30 or less.");
2966 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002967 final long identity = Binder.clearCallingIdentity();
2968 try {
Brad Ebinger4f6208e2021-03-23 21:04:45 +00002969 Phone phone = getPhone(getDefaultSubscription());
2970 return phone == null ? TelephonyManager.CALL_STATE_IDLE :
2971 PhoneConstantConversions.convertCallState(phone.getState());
2972 } finally {
2973 Binder.restoreCallingIdentity(identity);
2974 }
2975 }
2976
2977 @Override
2978 public int getCallStateForSubscription(int subId, String callingPackage, String featureId) {
2979 if (CompatChanges.isChangeEnabled(
2980 TelecomManager.ENABLE_GET_CALL_STATE_PERMISSION_PROTECTION,
2981 Binder.getCallingUid())) {
2982 // Check READ_PHONE_STATE for API version 31+
2983 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(mApp, subId, callingPackage,
2984 featureId, "getCallStateForSubscription")) {
2985 throw new SecurityException("getCallState requires READ_PHONE_STATE for apps "
2986 + "targeting API level 31+.");
2987 }
2988 }
2989 final long identity = Binder.clearCallingIdentity();
2990 try {
2991 Phone phone = getPhone(subId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002992 return phone == null ? TelephonyManager.CALL_STATE_IDLE :
2993 PhoneConstantConversions.convertCallState(phone.getState());
2994 } finally {
2995 Binder.restoreCallingIdentity(identity);
2996 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002997 }
2998
Sanket Padawe356d7632015-06-22 14:03:32 -07002999 @Override
Nathan Harolde037c472019-06-26 00:41:07 +00003000 public int getDataState() {
Nathan Haroldc4689b12019-06-14 16:58:30 -07003001 return getDataStateForSubId(mSubscriptionController.getDefaultDataSubId());
3002 }
3003
3004 @Override
3005 public int getDataStateForSubId(int subId) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003006 final long identity = Binder.clearCallingIdentity();
3007 try {
Nathan Haroldc4689b12019-06-14 16:58:30 -07003008 final Phone phone = getPhone(subId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003009 if (phone != null) {
Jack Yu2c450952021-11-29 11:36:17 -08003010 if (phone.isUsingNewDataStack()) {
3011 return phone.getDataNetworkController().getInternetDataNetworkState();
3012 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003013 return PhoneConstantConversions.convertDataState(phone.getDataConnectionState());
3014 } else {
3015 return PhoneConstantConversions.convertDataState(
3016 PhoneConstants.DataState.DISCONNECTED);
3017 }
3018 } finally {
3019 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07003020 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003021 }
3022
Sanket Padawe356d7632015-06-22 14:03:32 -07003023 @Override
Nathan Harolde037c472019-06-26 00:41:07 +00003024 public int getDataActivity() {
Nathan Haroldc4689b12019-06-14 16:58:30 -07003025 return getDataActivityForSubId(mSubscriptionController.getDefaultDataSubId());
3026 }
3027
3028 @Override
3029 public int getDataActivityForSubId(int subId) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003030 final long identity = Binder.clearCallingIdentity();
3031 try {
Nathan Haroldc4689b12019-06-14 16:58:30 -07003032 final Phone phone = getPhone(subId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003033 if (phone != null) {
3034 return DefaultPhoneNotifier.convertDataActivityState(phone.getDataActivityState());
3035 } else {
3036 return TelephonyManager.DATA_ACTIVITY_NONE;
3037 }
3038 } finally {
3039 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07003040 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003041 }
3042
3043 @Override
Meng Wanga10e89e2019-12-09 13:13:01 -08003044 public CellIdentity getCellLocation(String callingPackage, String callingFeatureId) {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08003045 mApp.getSystemService(AppOpsManager.class)
Hall Liu1aa510f2017-11-22 17:40:08 -08003046 .checkPackage(Binder.getCallingUid(), callingPackage);
Hall Liuf19c44f2018-11-27 14:38:17 -08003047
3048 LocationAccessPolicy.LocationPermissionResult locationResult =
3049 LocationAccessPolicy.checkLocationPermission(mApp,
3050 new LocationAccessPolicy.LocationPermissionQuery.Builder()
3051 .setCallingPackage(callingPackage)
Philip P. Moltmann3a2772a2019-10-04 08:15:00 -07003052 .setCallingFeatureId(callingFeatureId)
Hall Liuf19c44f2018-11-27 14:38:17 -08003053 .setCallingPid(Binder.getCallingPid())
3054 .setCallingUid(Binder.getCallingUid())
3055 .setMethod("getCellLocation")
Hall Liu773ba022020-01-24 18:07:12 -08003056 .setMinSdkVersionForCoarse(Build.VERSION_CODES.BASE)
Hall Liuf19c44f2018-11-27 14:38:17 -08003057 .setMinSdkVersionForFine(Build.VERSION_CODES.Q)
3058 .build());
3059 switch (locationResult) {
3060 case DENIED_HARD:
3061 throw new SecurityException("Not allowed to access cell location");
3062 case DENIED_SOFT:
Meng Wanga10e89e2019-12-09 13:13:01 -08003063 return (getDefaultPhone().getPhoneType() == PhoneConstants.PHONE_TYPE_CDMA)
3064 ? new CellIdentityCdma() : new CellIdentityGsm();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003065 }
3066
Narayan Kamathf04b5a12018-01-09 11:47:15 +00003067 WorkSource workSource = getWorkSource(Binder.getCallingUid());
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003068 final long identity = Binder.clearCallingIdentity();
3069 try {
3070 if (DBG_LOC) log("getCellLocation: is active user");
Nathan Harold3ff88932018-08-14 10:19:49 -07003071 int subId = mSubscriptionController.getDefaultDataSubId();
Meng Wanga10e89e2019-12-09 13:13:01 -08003072 return (CellIdentity) sendRequest(CMD_GET_CELL_LOCATION, workSource, subId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003073 } finally {
3074 Binder.restoreCallingIdentity(identity);
3075 }
Svetoslav64fad262015-04-14 14:35:21 -07003076 }
3077
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003078 @Override
Jack Yueb1e7fe2020-02-22 19:38:58 -08003079 public String getNetworkCountryIsoForPhone(int phoneId) {
Jonathan Basseribf5362b2017-07-19 12:22:35 -07003080 // Reporting the correct network country is ambiguous when IWLAN could conflict with
3081 // registered cell info, so return a NULL country instead.
3082 final long identity = Binder.clearCallingIdentity();
3083 try {
Malcolm Chen3732c2b2018-07-18 20:15:24 -07003084 if (phoneId == SubscriptionManager.INVALID_PHONE_INDEX) {
3085 // Get default phone in this case.
3086 phoneId = SubscriptionManager.DEFAULT_PHONE_INDEX;
3087 }
Jonathan Basseribf5362b2017-07-19 12:22:35 -07003088 final int subId = mSubscriptionController.getSubIdUsingPhoneId(phoneId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003089 Phone phone = PhoneFactory.getPhone(phoneId);
Nathan Harold532f51c2020-04-21 19:31:10 -07003090 if (phone == null) return "";
3091 ServiceStateTracker sst = phone.getServiceStateTracker();
3092 if (sst == null) return "";
3093 LocaleTracker lt = sst.getLocaleTracker();
3094 if (lt == null) return "";
Shuo Qian9418a922021-03-09 11:21:16 -08003095 return lt.getCurrentCountry();
Jonathan Basseribf5362b2017-07-19 12:22:35 -07003096 } finally {
3097 Binder.restoreCallingIdentity(identity);
3098 }
Jonathan Basseribf5362b2017-07-19 12:22:35 -07003099 }
3100
Nathan Harold7c8d0f12020-05-28 20:40:31 -07003101 /**
3102 * This method was removed due to potential issues caused by performing partial
3103 * updates of service state, and lack of a credible use case.
3104 *
3105 * This has the ability to break the telephony implementation by disabling notification of
3106 * changes in device connectivity. DO NOT USE THIS!
3107 */
Jonathan Basseribf5362b2017-07-19 12:22:35 -07003108 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003109 public void enableLocationUpdates() {
3110 mApp.enforceCallingOrSelfPermission(
3111 android.Manifest.permission.CONTROL_LOCATION_UPDATES, null);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003112 }
3113
Nathan Harold7c8d0f12020-05-28 20:40:31 -07003114 /**
3115 * This method was removed due to potential issues caused by performing partial
3116 * updates of service state, and lack of a credible use case.
3117 *
3118 * This has the ability to break the telephony implementation by disabling notification of
3119 * changes in device connectivity. DO NOT USE THIS!
3120 */
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003121 @Override
3122 public void disableLocationUpdates() {
3123 mApp.enforceCallingOrSelfPermission(
3124 android.Manifest.permission.CONTROL_LOCATION_UPDATES, null);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003125 }
3126
3127 @Override
3128 @SuppressWarnings("unchecked")
Philip P. Moltmann3a2772a2019-10-04 08:15:00 -07003129 public List<NeighboringCellInfo> getNeighboringCellInfo(String callingPackage,
3130 String callingFeatureId) {
Nathan Haroldb55f63b2021-07-27 11:27:38 -07003131 try {
3132 mApp.getSystemService(AppOpsManager.class)
3133 .checkPackage(Binder.getCallingUid(), callingPackage);
3134 } catch (SecurityException e) {
3135 EventLog.writeEvent(0x534e4554, "190619791", Binder.getCallingUid());
3136 throw e;
3137 }
3138
Nathan Haroldf096d982020-11-18 17:18:06 -08003139 final int targetSdk = TelephonyPermissions.getTargetSdk(mApp, callingPackage);
Nathan Harolddbea45a2018-08-30 14:35:07 -07003140 if (targetSdk >= android.os.Build.VERSION_CODES.Q) {
3141 throw new SecurityException(
3142 "getNeighboringCellInfo() is unavailable to callers targeting Q+ SDK levels.");
3143 }
Nathan Haroldb4d55612018-07-20 13:13:08 -07003144
Jordan Liu1617b712019-07-10 15:06:26 -07003145 if (mAppOps.noteOp(AppOpsManager.OPSTR_NEIGHBORING_CELLS, Binder.getCallingUid(),
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003146 callingPackage) != AppOpsManager.MODE_ALLOWED) {
3147 return null;
3148 }
Svetoslav64fad262015-04-14 14:35:21 -07003149
Svetoslav Ganov4a9d4482017-06-20 19:53:35 -07003150 if (DBG_LOC) log("getNeighboringCellInfo: is active user");
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003151
Philip P. Moltmann3a2772a2019-10-04 08:15:00 -07003152 List<CellInfo> info = getAllCellInfo(callingPackage, callingFeatureId);
Nathan Haroldf180aac2018-06-01 18:43:55 -07003153 if (info == null) return null;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003154
Nathan Haroldf180aac2018-06-01 18:43:55 -07003155 List<NeighboringCellInfo> neighbors = new ArrayList<NeighboringCellInfo>();
3156 for (CellInfo ci : info) {
3157 if (ci instanceof CellInfoGsm) {
3158 neighbors.add(new NeighboringCellInfo((CellInfoGsm) ci));
3159 } else if (ci instanceof CellInfoWcdma) {
3160 neighbors.add(new NeighboringCellInfo((CellInfoWcdma) ci));
3161 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003162 }
Nathan Haroldf180aac2018-06-01 18:43:55 -07003163 return (neighbors.size()) > 0 ? neighbors : null;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003164 }
3165
Nathan Haroldfa8da0f2018-09-27 18:51:29 -07003166 private List<CellInfo> getCachedCellInfo() {
3167 List<CellInfo> cellInfos = new ArrayList<CellInfo>();
3168 for (Phone phone : PhoneFactory.getPhones()) {
3169 List<CellInfo> info = phone.getAllCellInfo();
3170 if (info != null) cellInfos.addAll(info);
3171 }
3172 return cellInfos;
3173 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003174
3175 @Override
Philip P. Moltmann3a2772a2019-10-04 08:15:00 -07003176 public List<CellInfo> getAllCellInfo(String callingPackage, String callingFeatureId) {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08003177 mApp.getSystemService(AppOpsManager.class)
Hall Liu1aa510f2017-11-22 17:40:08 -08003178 .checkPackage(Binder.getCallingUid(), callingPackage);
Hall Liuf19c44f2018-11-27 14:38:17 -08003179
3180 LocationAccessPolicy.LocationPermissionResult locationResult =
3181 LocationAccessPolicy.checkLocationPermission(mApp,
3182 new LocationAccessPolicy.LocationPermissionQuery.Builder()
3183 .setCallingPackage(callingPackage)
Philip P. Moltmann3a2772a2019-10-04 08:15:00 -07003184 .setCallingFeatureId(callingFeatureId)
Hall Liuf19c44f2018-11-27 14:38:17 -08003185 .setCallingPid(Binder.getCallingPid())
3186 .setCallingUid(Binder.getCallingUid())
3187 .setMethod("getAllCellInfo")
Nathan Harold5ae50b52019-02-20 15:46:36 -08003188 .setMinSdkVersionForCoarse(Build.VERSION_CODES.BASE)
Hall Liuf19c44f2018-11-27 14:38:17 -08003189 .setMinSdkVersionForFine(Build.VERSION_CODES.Q)
3190 .build());
3191 switch (locationResult) {
3192 case DENIED_HARD:
3193 throw new SecurityException("Not allowed to access cell info");
3194 case DENIED_SOFT:
3195 return new ArrayList<>();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003196 }
3197
Nathan Haroldf096d982020-11-18 17:18:06 -08003198 final int targetSdk = TelephonyPermissions.getTargetSdk(mApp, callingPackage);
Nathan Haroldfa8da0f2018-09-27 18:51:29 -07003199 if (targetSdk >= android.os.Build.VERSION_CODES.Q) {
3200 return getCachedCellInfo();
3201 }
3202
Svetoslav Ganov4a9d4482017-06-20 19:53:35 -07003203 if (DBG_LOC) log("getAllCellInfo: is active user");
Narayan Kamathf04b5a12018-01-09 11:47:15 +00003204 WorkSource workSource = getWorkSource(Binder.getCallingUid());
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003205 final long identity = Binder.clearCallingIdentity();
3206 try {
3207 List<CellInfo> cellInfos = new ArrayList<CellInfo>();
3208 for (Phone phone : PhoneFactory.getPhones()) {
Nathan Harold3ff88932018-08-14 10:19:49 -07003209 final List<CellInfo> info = (List<CellInfo>) sendRequest(
Nathan Harold92bed182018-10-12 18:16:49 -07003210 CMD_GET_ALL_CELL_INFO, null, phone, workSource);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003211 if (info != null) cellInfos.addAll(info);
3212 }
3213 return cellInfos;
3214 } finally {
3215 Binder.restoreCallingIdentity(identity);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003216 }
3217 }
3218
Sailesh Nepalbd76e4e2013-10-27 13:59:44 -07003219 @Override
Philip P. Moltmann3a2772a2019-10-04 08:15:00 -07003220 public void requestCellInfoUpdate(int subId, ICellInfoCallback cb, String callingPackage,
3221 String callingFeatureId) {
3222 requestCellInfoUpdateInternal(subId, cb, callingPackage, callingFeatureId,
3223 getWorkSource(Binder.getCallingUid()));
Nathan Haroldfa8da0f2018-09-27 18:51:29 -07003224 }
3225
3226 @Override
Philip P. Moltmann3a2772a2019-10-04 08:15:00 -07003227 public void requestCellInfoUpdateWithWorkSource(int subId, ICellInfoCallback cb,
3228 String callingPackage, String callingFeatureId, WorkSource workSource) {
Nathan Haroldfa8da0f2018-09-27 18:51:29 -07003229 enforceModifyPermission();
Philip P. Moltmann3a2772a2019-10-04 08:15:00 -07003230 requestCellInfoUpdateInternal(subId, cb, callingPackage, callingFeatureId, workSource);
Nathan Haroldfa8da0f2018-09-27 18:51:29 -07003231 }
3232
Philip P. Moltmann3a2772a2019-10-04 08:15:00 -07003233 private void requestCellInfoUpdateInternal(int subId, ICellInfoCallback cb,
3234 String callingPackage, String callingFeatureId, WorkSource workSource) {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08003235 mApp.getSystemService(AppOpsManager.class)
Nathan Haroldfa8da0f2018-09-27 18:51:29 -07003236 .checkPackage(Binder.getCallingUid(), callingPackage);
Hall Liuf19c44f2018-11-27 14:38:17 -08003237
3238 LocationAccessPolicy.LocationPermissionResult locationResult =
3239 LocationAccessPolicy.checkLocationPermission(mApp,
3240 new LocationAccessPolicy.LocationPermissionQuery.Builder()
3241 .setCallingPackage(callingPackage)
Philip P. Moltmann3a2772a2019-10-04 08:15:00 -07003242 .setCallingFeatureId(callingFeatureId)
Hall Liuf19c44f2018-11-27 14:38:17 -08003243 .setCallingPid(Binder.getCallingPid())
3244 .setCallingUid(Binder.getCallingUid())
3245 .setMethod("requestCellInfoUpdate")
Hall Liud60acc92020-05-21 17:09:35 -07003246 .setMinSdkVersionForCoarse(Build.VERSION_CODES.BASE)
3247 .setMinSdkVersionForFine(Build.VERSION_CODES.BASE)
Hall Liuf19c44f2018-11-27 14:38:17 -08003248 .build());
3249 switch (locationResult) {
3250 case DENIED_HARD:
Nathan Haroldf096d982020-11-18 17:18:06 -08003251 if (TelephonyPermissions
3252 .getTargetSdk(mApp, callingPackage) < Build.VERSION_CODES.Q) {
Hall Liud60acc92020-05-21 17:09:35 -07003253 // Safetynet logging for b/154934934
3254 EventLog.writeEvent(0x534e4554, "154934934", Binder.getCallingUid());
3255 }
Hall Liuf19c44f2018-11-27 14:38:17 -08003256 throw new SecurityException("Not allowed to access cell info");
3257 case DENIED_SOFT:
Nathan Haroldf096d982020-11-18 17:18:06 -08003258 if (TelephonyPermissions
3259 .getTargetSdk(mApp, callingPackage) < Build.VERSION_CODES.Q) {
Hall Liud60acc92020-05-21 17:09:35 -07003260 // Safetynet logging for b/154934934
3261 EventLog.writeEvent(0x534e4554, "154934934", Binder.getCallingUid());
3262 }
Nathan Harold5320c422019-05-09 10:26:08 -07003263 try {
3264 cb.onCellInfo(new ArrayList<CellInfo>());
3265 } catch (RemoteException re) {
3266 // Drop without consequences
3267 }
Hall Liuf19c44f2018-11-27 14:38:17 -08003268 return;
Nathan Haroldfa8da0f2018-09-27 18:51:29 -07003269 }
3270
Nathan Harolda939a962019-05-09 10:13:47 -07003271
3272 final Phone phone = getPhoneFromSubId(subId);
Nathan Haroldfa8da0f2018-09-27 18:51:29 -07003273 if (phone == null) throw new IllegalArgumentException("Invalid Subscription Id: " + subId);
3274
3275 sendRequestAsync(CMD_REQUEST_CELL_INFO_UPDATE, cb, phone, workSource);
3276 }
3277
3278 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003279 public void setCellInfoListRate(int rateInMillis) {
Jack Yua8d8cb82017-01-16 10:15:34 -08003280 enforceModifyPermission();
Narayan Kamathf04b5a12018-01-09 11:47:15 +00003281 WorkSource workSource = getWorkSource(Binder.getCallingUid());
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003282
3283 final long identity = Binder.clearCallingIdentity();
3284 try {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08003285 getDefaultPhone().setCellInfoListRate(rateInMillis, workSource);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003286 } finally {
3287 Binder.restoreCallingIdentity(identity);
3288 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003289 }
3290
Shishir Agrawala9f32182016-04-12 12:00:16 -07003291 @Override
Philip P. Moltmann700a9592019-10-03 11:53:50 -07003292 public String getImeiForSlot(int slotIndex, String callingPackage, String callingFeatureId) {
Jeff Davidson913390f2018-02-23 17:11:49 -08003293 Phone phone = PhoneFactory.getPhone(slotIndex);
3294 if (phone == null) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08003295 return null;
3296 }
Jeff Davidson913390f2018-02-23 17:11:49 -08003297 int subId = phone.getSubId();
Grace Jia0ddb3612021-04-22 13:35:26 -07003298 enforceCallingPackage(callingPackage, Binder.getCallingUid(), "getImeiForSlot");
Michael Groover70af6dc2018-10-01 16:23:15 -07003299 if (!TelephonyPermissions.checkCallingOrSelfReadDeviceIdentifiers(mApp, subId,
Philip P. Moltmann700a9592019-10-03 11:53:50 -07003300 callingPackage, callingFeatureId, "getImeiForSlot")) {
Jeff Davidson913390f2018-02-23 17:11:49 -08003301 return null;
3302 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003303
3304 final long identity = Binder.clearCallingIdentity();
3305 try {
3306 return phone.getImei();
3307 } finally {
3308 Binder.restoreCallingIdentity(identity);
3309 }
Shishir Agrawala9f32182016-04-12 12:00:16 -07003310 }
3311
3312 @Override
David Kelly5e06a7f2018-03-12 14:10:59 +00003313 public String getTypeAllocationCodeForSlot(int slotIndex) {
3314 Phone phone = PhoneFactory.getPhone(slotIndex);
3315 String tac = null;
3316 if (phone != null) {
3317 String imei = phone.getImei();
Vala Zadehab005552021-09-21 15:54:29 -07003318 try {
3319 tac = imei == null ? null : imei.substring(0, TYPE_ALLOCATION_CODE_LENGTH);
3320 } catch (IndexOutOfBoundsException e) {
3321 Log.e(LOG_TAG, "IMEI length shorter than upper index.");
3322 return null;
3323 }
David Kelly5e06a7f2018-03-12 14:10:59 +00003324 }
3325 return tac;
3326 }
3327
3328 @Override
Philip P. Moltmann700a9592019-10-03 11:53:50 -07003329 public String getMeidForSlot(int slotIndex, String callingPackage, String callingFeatureId) {
Shuo Qian13d89152021-05-10 23:58:11 -07003330 try {
3331 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
3332 } catch (SecurityException se) {
3333 EventLog.writeEvent(0x534e4554, "186530496", Binder.getCallingUid());
3334 throw new SecurityException("Package " + callingPackage + " does not belong to "
3335 + Binder.getCallingUid());
3336 }
Jeff Davidson913390f2018-02-23 17:11:49 -08003337 Phone phone = PhoneFactory.getPhone(slotIndex);
3338 if (phone == null) {
Jack Yu2af8d712017-03-15 17:14:14 -07003339 return null;
3340 }
Nazanin Bakhshif782e562018-12-11 15:15:39 -08003341
Jeff Davidson913390f2018-02-23 17:11:49 -08003342 int subId = phone.getSubId();
Michael Groover70af6dc2018-10-01 16:23:15 -07003343 if (!TelephonyPermissions.checkCallingOrSelfReadDeviceIdentifiers(mApp, subId,
Philip P. Moltmann700a9592019-10-03 11:53:50 -07003344 callingPackage, callingFeatureId, "getMeidForSlot")) {
Jeff Davidson913390f2018-02-23 17:11:49 -08003345 return null;
3346 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003347
3348 final long identity = Binder.clearCallingIdentity();
3349 try {
3350 return phone.getMeid();
3351 } finally {
3352 Binder.restoreCallingIdentity(identity);
3353 }
Jack Yu2af8d712017-03-15 17:14:14 -07003354 }
3355
3356 @Override
David Kelly5e06a7f2018-03-12 14:10:59 +00003357 public String getManufacturerCodeForSlot(int slotIndex) {
3358 Phone phone = PhoneFactory.getPhone(slotIndex);
3359 String manufacturerCode = null;
3360 if (phone != null) {
3361 String meid = phone.getMeid();
Vala Zadehab005552021-09-21 15:54:29 -07003362 try {
3363 manufacturerCode =
3364 meid == null ? null : meid.substring(0, MANUFACTURER_CODE_LENGTH);
3365 } catch (IndexOutOfBoundsException e) {
3366 Log.e(LOG_TAG, "MEID length shorter than upper index.");
3367 return null;
3368 }
David Kelly5e06a7f2018-03-12 14:10:59 +00003369 }
3370 return manufacturerCode;
3371 }
3372
3373 @Override
Philip P. Moltmann700a9592019-10-03 11:53:50 -07003374 public String getDeviceSoftwareVersionForSlot(int slotIndex, String callingPackage,
3375 String callingFeatureId) {
Jeff Davidson913390f2018-02-23 17:11:49 -08003376 Phone phone = PhoneFactory.getPhone(slotIndex);
3377 if (phone == null) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08003378 return null;
3379 }
Jeff Davidson913390f2018-02-23 17:11:49 -08003380 int subId = phone.getSubId();
3381 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
Philip P. Moltmann700a9592019-10-03 11:53:50 -07003382 mApp, subId, callingPackage, callingFeatureId,
3383 "getDeviceSoftwareVersionForSlot")) {
Jeff Davidson913390f2018-02-23 17:11:49 -08003384 return null;
3385 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003386
3387 final long identity = Binder.clearCallingIdentity();
3388 try {
3389 return phone.getDeviceSvn();
3390 } finally {
3391 Binder.restoreCallingIdentity(identity);
3392 }
Shishir Agrawala9f32182016-04-12 12:00:16 -07003393 }
3394
fionaxu43304da2017-11-27 22:51:16 -08003395 @Override
3396 public int getSubscriptionCarrierId(int subId) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003397 final long identity = Binder.clearCallingIdentity();
3398 try {
3399 final Phone phone = getPhone(subId);
3400 return phone == null ? TelephonyManager.UNKNOWN_CARRIER_ID : phone.getCarrierId();
3401 } finally {
3402 Binder.restoreCallingIdentity(identity);
3403 }
fionaxu43304da2017-11-27 22:51:16 -08003404 }
3405
3406 @Override
3407 public String getSubscriptionCarrierName(int subId) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003408 final long identity = Binder.clearCallingIdentity();
3409 try {
3410 final Phone phone = getPhone(subId);
3411 return phone == null ? null : phone.getCarrierName();
3412 } finally {
3413 Binder.restoreCallingIdentity(identity);
3414 }
fionaxu43304da2017-11-27 22:51:16 -08003415 }
3416
calvinpanffe225e2018-11-01 19:43:06 +08003417 @Override
chen xu0026ca62019-03-06 15:28:50 -08003418 public int getSubscriptionSpecificCarrierId(int subId) {
chen xu25637222018-11-04 17:17:00 -08003419 final long identity = Binder.clearCallingIdentity();
3420 try {
3421 final Phone phone = getPhone(subId);
3422 return phone == null ? TelephonyManager.UNKNOWN_CARRIER_ID
chen xu0026ca62019-03-06 15:28:50 -08003423 : phone.getSpecificCarrierId();
chen xu25637222018-11-04 17:17:00 -08003424 } finally {
3425 Binder.restoreCallingIdentity(identity);
3426 }
3427 }
3428
3429 @Override
chen xu0026ca62019-03-06 15:28:50 -08003430 public String getSubscriptionSpecificCarrierName(int subId) {
chen xu25637222018-11-04 17:17:00 -08003431 final long identity = Binder.clearCallingIdentity();
3432 try {
3433 final Phone phone = getPhone(subId);
chen xu0026ca62019-03-06 15:28:50 -08003434 return phone == null ? null : phone.getSpecificCarrierName();
chen xu25637222018-11-04 17:17:00 -08003435 } finally {
3436 Binder.restoreCallingIdentity(identity);
3437 }
3438 }
3439
chen xu651eec72018-11-11 19:03:44 -08003440 @Override
chen xu864e11c2018-12-06 22:10:03 -08003441 public int getCarrierIdFromMccMnc(int slotIndex, String mccmnc, boolean isSubscriptionMccMnc) {
3442 if (!isSubscriptionMccMnc) {
3443 enforceReadPrivilegedPermission("getCarrierIdFromMccMnc");
3444 }
chen xu651eec72018-11-11 19:03:44 -08003445 final Phone phone = PhoneFactory.getPhone(slotIndex);
3446 if (phone == null) {
3447 return TelephonyManager.UNKNOWN_CARRIER_ID;
3448 }
3449 final long identity = Binder.clearCallingIdentity();
3450 try {
3451 return CarrierResolver.getCarrierIdFromMccMnc(phone.getContext(), mccmnc);
3452 } finally {
3453 Binder.restoreCallingIdentity(identity);
3454 }
3455 }
3456
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003457 //
3458 // Internal helper methods.
3459 //
3460
Sanket Padaweee13a9b2016-03-08 17:30:28 -08003461 /**
Grace Jia0ddb3612021-04-22 13:35:26 -07003462 * Make sure the caller is the calling package itself
3463 *
3464 * @throws SecurityException if the caller is not the calling package
3465 */
3466 private void enforceCallingPackage(String callingPackage, int callingUid, String message) {
3467 int packageUid = -1;
Grace Jiadbefca02021-04-26 15:13:31 -07003468 PackageManager pm = mApp.getBaseContext().createContextAsUser(
3469 UserHandle.getUserHandleForUid(callingUid), 0).getPackageManager();
Grace Jia0ddb3612021-04-22 13:35:26 -07003470 try {
Grace Jiadbefca02021-04-26 15:13:31 -07003471 packageUid = pm.getPackageUid(callingPackage, 0);
Grace Jia0ddb3612021-04-22 13:35:26 -07003472 } catch (PackageManager.NameNotFoundException e) {
3473 // packageUid is -1
3474 }
3475 if (packageUid != callingUid) {
3476 throw new SecurityException(message + ": Package " + callingPackage
3477 + " does not belong to " + callingUid);
3478 }
3479 }
3480
3481 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003482 * Make sure the caller has the MODIFY_PHONE_STATE permission.
3483 *
3484 * @throws SecurityException if the caller does not have the required permission
3485 */
3486 private void enforceModifyPermission() {
3487 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.MODIFY_PHONE_STATE, null);
3488 }
3489
Shuo Qian3b6ee772019-11-13 17:43:31 -08003490 private void enforceActiveEmergencySessionPermission() {
3491 mApp.enforceCallingOrSelfPermission(
3492 android.Manifest.permission.READ_ACTIVE_EMERGENCY_SESSION, null);
3493 }
3494
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003495 /**
3496 * Make sure the caller has the CALL_PHONE permission.
3497 *
3498 * @throws SecurityException if the caller does not have the required permission
3499 */
3500 private void enforceCallPermission() {
3501 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.CALL_PHONE, null);
3502 }
3503
paulhu5a773602019-08-23 19:17:33 +08003504 private void enforceSettingsPermission() {
3505 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.NETWORK_SETTINGS, null);
Stuart Scott8eef64f2015-04-08 15:13:54 -07003506 }
3507
Michele Berionne5e411512020-11-13 02:36:59 +00003508 private void enforceRebootPermission() {
3509 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.REBOOT, null);
3510 }
3511
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003512 private String createTelUrl(String number) {
3513 if (TextUtils.isEmpty(number)) {
3514 return null;
3515 }
3516
Jake Hambye994d462014-02-03 13:10:13 -08003517 return "tel:" + number;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003518 }
3519
Ihab Awadf9e92732013-12-05 18:02:52 -08003520 private static void log(String msg) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003521 Log.d(LOG_TAG, "[PhoneIntfMgr] " + msg);
3522 }
3523
Naveen Kalla1fd79bd2014-08-08 00:48:59 -07003524 private static void logv(String msg) {
3525 Log.v(LOG_TAG, "[PhoneIntfMgr] " + msg);
3526 }
3527
Ihab Awadf9e92732013-12-05 18:02:52 -08003528 private static void loge(String msg) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003529 Log.e(LOG_TAG, "[PhoneIntfMgr] " + msg);
3530 }
3531
Robert Greenwalt36b23af2015-07-06 17:59:14 -07003532 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003533 public int getActivePhoneType() {
Shishir Agrawala9f32182016-04-12 12:00:16 -07003534 return getActivePhoneTypeForSlot(getSlotForDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07003535 }
3536
Sanket Padawe356d7632015-06-22 14:03:32 -07003537 @Override
Sanket Padawe13bac7b2017-03-20 15:04:47 -07003538 public int getActivePhoneTypeForSlot(int slotIndex) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003539 final long identity = Binder.clearCallingIdentity();
3540 try {
3541 final Phone phone = PhoneFactory.getPhone(slotIndex);
3542 if (phone == null) {
3543 return PhoneConstants.PHONE_TYPE_NONE;
3544 } else {
3545 return phone.getPhoneType();
3546 }
3547 } finally {
3548 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07003549 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003550 }
3551
3552 /**
3553 * Returns the CDMA ERI icon index to display
3554 */
Robert Greenwalt36b23af2015-07-06 17:59:14 -07003555 @Override
Philip P. Moltmann700a9592019-10-03 11:53:50 -07003556 public int getCdmaEriIconIndex(String callingPackage, String callingFeatureId) {
3557 return getCdmaEriIconIndexForSubscriber(getDefaultSubscription(), callingPackage,
3558 callingFeatureId);
Wink Saville36469e72014-06-11 15:17:00 -07003559 }
3560
Sanket Padawe356d7632015-06-22 14:03:32 -07003561 @Override
Philip P. Moltmann700a9592019-10-03 11:53:50 -07003562 public int getCdmaEriIconIndexForSubscriber(int subId, String callingPackage,
3563 String callingFeatureId) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08003564 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
Philip P. Moltmann700a9592019-10-03 11:53:50 -07003565 mApp, subId, callingPackage, callingFeatureId,
3566 "getCdmaEriIconIndexForSubscriber")) {
Robert Greenwalt36b23af2015-07-06 17:59:14 -07003567 return -1;
3568 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003569
3570 final long identity = Binder.clearCallingIdentity();
3571 try {
3572 final Phone phone = getPhone(subId);
3573 if (phone != null) {
3574 return phone.getCdmaEriIconIndex();
3575 } else {
3576 return -1;
3577 }
3578 } finally {
3579 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07003580 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003581 }
3582
3583 /**
3584 * Returns the CDMA ERI icon mode,
3585 * 0 - ON
3586 * 1 - FLASHING
3587 */
Robert Greenwalt36b23af2015-07-06 17:59:14 -07003588 @Override
Philip P. Moltmann700a9592019-10-03 11:53:50 -07003589 public int getCdmaEriIconMode(String callingPackage, String callingFeatureId) {
3590 return getCdmaEriIconModeForSubscriber(getDefaultSubscription(), callingPackage,
3591 callingFeatureId);
Wink Saville36469e72014-06-11 15:17:00 -07003592 }
3593
Sanket Padawe356d7632015-06-22 14:03:32 -07003594 @Override
Philip P. Moltmann700a9592019-10-03 11:53:50 -07003595 public int getCdmaEriIconModeForSubscriber(int subId, String callingPackage,
3596 String callingFeatureId) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08003597 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
Philip P. Moltmann700a9592019-10-03 11:53:50 -07003598 mApp, subId, callingPackage, callingFeatureId,
3599 "getCdmaEriIconModeForSubscriber")) {
Robert Greenwalt36b23af2015-07-06 17:59:14 -07003600 return -1;
3601 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003602
3603 final long identity = Binder.clearCallingIdentity();
3604 try {
3605 final Phone phone = getPhone(subId);
3606 if (phone != null) {
3607 return phone.getCdmaEriIconMode();
3608 } else {
3609 return -1;
3610 }
3611 } finally {
3612 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07003613 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003614 }
3615
3616 /**
3617 * Returns the CDMA ERI text,
3618 */
Robert Greenwalt36b23af2015-07-06 17:59:14 -07003619 @Override
Philip P. Moltmann700a9592019-10-03 11:53:50 -07003620 public String getCdmaEriText(String callingPackage, String callingFeatureId) {
3621 return getCdmaEriTextForSubscriber(getDefaultSubscription(), callingPackage,
3622 callingFeatureId);
Wink Saville36469e72014-06-11 15:17:00 -07003623 }
3624
Sanket Padawe356d7632015-06-22 14:03:32 -07003625 @Override
Philip P. Moltmann700a9592019-10-03 11:53:50 -07003626 public String getCdmaEriTextForSubscriber(int subId, String callingPackage,
3627 String callingFeatureId) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08003628 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
Philip P. Moltmann700a9592019-10-03 11:53:50 -07003629 mApp, subId, callingPackage, callingFeatureId,
3630 "getCdmaEriIconTextForSubscriber")) {
Robert Greenwalt36b23af2015-07-06 17:59:14 -07003631 return null;
3632 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003633
3634 final long identity = Binder.clearCallingIdentity();
3635 try {
3636 final Phone phone = getPhone(subId);
3637 if (phone != null) {
3638 return phone.getCdmaEriText();
3639 } else {
3640 return null;
3641 }
3642 } finally {
3643 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07003644 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003645 }
3646
3647 /**
Junda Liuca05d5d2014-08-14 22:36:34 -07003648 * Returns the CDMA MDN.
3649 */
Sanket Padawe356d7632015-06-22 14:03:32 -07003650 @Override
Wink Savilleb564aae2014-10-23 10:18:09 -07003651 public String getCdmaMdn(int subId) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08003652 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
3653 mApp, subId, "getCdmaMdn");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003654
3655 final long identity = Binder.clearCallingIdentity();
3656 try {
3657 final Phone phone = getPhone(subId);
Nazanin Bakhshif782e562018-12-11 15:15:39 -08003658 if (phone != null && phone.getPhoneType() == PhoneConstants.PHONE_TYPE_CDMA) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003659 return phone.getLine1Number();
3660 } else {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08003661 loge("getCdmaMdn: no phone found. Invalid subId: " + subId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003662 return null;
3663 }
3664 } finally {
3665 Binder.restoreCallingIdentity(identity);
Junda Liuca05d5d2014-08-14 22:36:34 -07003666 }
3667 }
3668
3669 /**
3670 * Returns the CDMA MIN.
3671 */
Sanket Padawe356d7632015-06-22 14:03:32 -07003672 @Override
Wink Savilleb564aae2014-10-23 10:18:09 -07003673 public String getCdmaMin(int subId) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08003674 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
3675 mApp, subId, "getCdmaMin");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003676
3677 final long identity = Binder.clearCallingIdentity();
3678 try {
3679 final Phone phone = getPhone(subId);
3680 if (phone != null && phone.getPhoneType() == PhoneConstants.PHONE_TYPE_CDMA) {
3681 return phone.getCdmaMin();
3682 } else {
3683 return null;
3684 }
3685 } finally {
3686 Binder.restoreCallingIdentity(identity);
Junda Liuca05d5d2014-08-14 22:36:34 -07003687 }
3688 }
3689
Hall Liud892bec2018-11-30 14:51:45 -08003690 @Override
3691 public void requestNumberVerification(PhoneNumberRange range, long timeoutMillis,
3692 INumberVerificationCallback callback, String callingPackage) {
3693 if (mApp.checkCallingOrSelfPermission(android.Manifest.permission.MODIFY_PHONE_STATE)
3694 != PERMISSION_GRANTED) {
3695 throw new SecurityException("Caller must hold the MODIFY_PHONE_STATE permission");
3696 }
3697 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
3698
3699 String authorizedPackage = NumberVerificationManager.getAuthorizedPackage(mApp);
3700 if (!TextUtils.equals(callingPackage, authorizedPackage)) {
Hall Liub9d8feb2021-01-13 10:28:04 -08003701 throw new SecurityException("Calling package must be configured in the device config: "
3702 + "calling package: " + callingPackage
3703 + ", configured package: " + authorizedPackage);
Hall Liud892bec2018-11-30 14:51:45 -08003704 }
3705
3706 if (range == null) {
3707 throw new NullPointerException("Range must be non-null");
3708 }
3709
3710 timeoutMillis = Math.min(timeoutMillis,
Hall Liubd069e32019-02-28 18:56:30 -08003711 TelephonyManager.getMaxNumberVerificationTimeoutMillis());
Hall Liud892bec2018-11-30 14:51:45 -08003712
3713 NumberVerificationManager.getInstance().requestVerification(range, callback, timeoutMillis);
3714 }
3715
Junda Liuca05d5d2014-08-14 22:36:34 -07003716 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003717 * Returns true if CDMA provisioning needs to run.
3718 */
3719 public boolean needsOtaServiceProvisioning() {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003720 final long identity = Binder.clearCallingIdentity();
3721 try {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08003722 return getDefaultPhone().needsOtaServiceProvisioning();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003723 } finally {
3724 Binder.restoreCallingIdentity(identity);
3725 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003726 }
3727
3728 /**
Shishir Agrawal76d5da92014-11-09 16:17:25 -08003729 * Sets the voice mail number of a given subId.
3730 */
3731 @Override
3732 public boolean setVoiceMailNumber(int subId, String alphaTag, String number) {
Shuo Qian2c0ae432019-12-05 11:40:37 -08003733 TelephonyPermissions.enforceCallingOrSelfCarrierPrivilege(
3734 mApp, subId, "setVoiceMailNumber");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003735
3736 final long identity = Binder.clearCallingIdentity();
3737 try {
3738 Boolean success = (Boolean) sendRequest(CMD_SET_VOICEMAIL_NUMBER,
3739 new Pair<String, String>(alphaTag, number), new Integer(subId));
3740 return success;
3741 } finally {
3742 Binder.restoreCallingIdentity(identity);
3743 }
Shishir Agrawal76d5da92014-11-09 16:17:25 -08003744 }
3745
Ta-wei Yen87c49842016-05-13 21:19:52 -07003746 @Override
Ta-wei Yenc9df0432017-04-17 17:09:07 -07003747 public Bundle getVisualVoicemailSettings(String callingPackage, int subId) {
3748 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
Tyler Gunn5ddfdc92019-10-31 13:08:23 -07003749 TelecomManager tm = mApp.getSystemService(TelecomManager.class);
3750 String systemDialer = tm.getSystemDialerPackage();
Ta-wei Yenc9df0432017-04-17 17:09:07 -07003751 if (!TextUtils.equals(callingPackage, systemDialer)) {
3752 throw new SecurityException("caller must be system dialer");
3753 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003754
3755 final long identity = Binder.clearCallingIdentity();
3756 try {
3757 PhoneAccountHandle phoneAccountHandle = PhoneAccountHandleConverter.fromSubId(subId);
3758 if (phoneAccountHandle == null) {
3759 return null;
3760 }
Nazanin Bakhshif782e562018-12-11 15:15:39 -08003761 return VisualVoicemailSettingsUtil.dump(mApp, phoneAccountHandle);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003762 } finally {
3763 Binder.restoreCallingIdentity(identity);
Ta-wei Yenc9df0432017-04-17 17:09:07 -07003764 }
Ta-wei Yenc9df0432017-04-17 17:09:07 -07003765 }
3766
3767 @Override
Philip P. Moltmann700a9592019-10-03 11:53:50 -07003768 public String getVisualVoicemailPackageName(String callingPackage, String callingFeatureId,
3769 int subId) {
Ta-wei Yendca928f2017-01-10 16:17:08 -08003770 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
Jeff Davidson7e17e312018-02-13 18:17:36 -08003771 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
Philip P. Moltmann700a9592019-10-03 11:53:50 -07003772 mApp, subId, callingPackage, callingFeatureId,
3773 "getVisualVoicemailPackageName")) {
Ta-wei Yendca928f2017-01-10 16:17:08 -08003774 return null;
3775 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003776
Jeff Davidsona8e4e242018-03-15 17:16:18 -07003777 final long identity = Binder.clearCallingIdentity();
3778 try {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08003779 return RemoteVvmTaskManager.getRemotePackage(mApp, subId).getPackageName();
Jeff Davidsona8e4e242018-03-15 17:16:18 -07003780 } finally {
3781 Binder.restoreCallingIdentity(identity);
3782 }
Ta-wei Yendca928f2017-01-10 16:17:08 -08003783 }
3784
3785 @Override
Ta-wei Yenb6929602016-05-24 15:48:27 -07003786 public void enableVisualVoicemailSmsFilter(String callingPackage, int subId,
3787 VisualVoicemailSmsFilterSettings settings) {
3788 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003789
3790 final long identity = Binder.clearCallingIdentity();
3791 try {
3792 VisualVoicemailSmsFilterConfig.enableVisualVoicemailSmsFilter(
Nazanin Bakhshif782e562018-12-11 15:15:39 -08003793 mApp, callingPackage, subId, settings);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003794 } finally {
3795 Binder.restoreCallingIdentity(identity);
3796 }
Ta-wei Yen87c49842016-05-13 21:19:52 -07003797 }
3798
3799 @Override
Ta-wei Yenb6929602016-05-24 15:48:27 -07003800 public void disableVisualVoicemailSmsFilter(String callingPackage, int subId) {
3801 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003802
3803 final long identity = Binder.clearCallingIdentity();
3804 try {
3805 VisualVoicemailSmsFilterConfig.disableVisualVoicemailSmsFilter(
Nazanin Bakhshif782e562018-12-11 15:15:39 -08003806 mApp, callingPackage, subId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003807 } finally {
3808 Binder.restoreCallingIdentity(identity);
3809 }
Ta-wei Yen87c49842016-05-13 21:19:52 -07003810 }
3811
3812 @Override
Ta-wei Yenb6929602016-05-24 15:48:27 -07003813 public VisualVoicemailSmsFilterSettings getVisualVoicemailSmsFilterSettings(
3814 String callingPackage, int subId) {
3815 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003816
3817 final long identity = Binder.clearCallingIdentity();
3818 try {
3819 return VisualVoicemailSmsFilterConfig.getVisualVoicemailSmsFilterSettings(
Nazanin Bakhshif782e562018-12-11 15:15:39 -08003820 mApp, callingPackage, subId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003821 } finally {
3822 Binder.restoreCallingIdentity(identity);
3823 }
Ta-wei Yen87c49842016-05-13 21:19:52 -07003824 }
3825
3826 @Override
Ta-wei Yen30a69c82016-12-27 14:52:32 -08003827 public VisualVoicemailSmsFilterSettings getActiveVisualVoicemailSmsFilterSettings(int subId) {
Brad Ebinger35c841c2018-10-01 10:40:55 -07003828 enforceReadPrivilegedPermission("getActiveVisualVoicemailSmsFilterSettings");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003829
3830 final long identity = Binder.clearCallingIdentity();
3831 try {
3832 return VisualVoicemailSmsFilterConfig.getActiveVisualVoicemailSmsFilterSettings(
Nazanin Bakhshif782e562018-12-11 15:15:39 -08003833 mApp, subId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003834 } finally {
3835 Binder.restoreCallingIdentity(identity);
3836 }
Ta-wei Yen30a69c82016-12-27 14:52:32 -08003837 }
3838
3839 @Override
Philip P. Moltmann2f6f8ce2020-03-18 18:17:02 -07003840 public void sendVisualVoicemailSmsForSubscriber(String callingPackage,
3841 String callingAttributionTag, int subId, String number, int port, String text,
3842 PendingIntent sentIntent) {
Ta-wei Yen30a69c82016-12-27 14:52:32 -08003843 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
Ta-wei Yen527a9c02017-01-06 15:29:25 -08003844 enforceVisualVoicemailPackage(callingPackage, subId);
Ta-wei Yen30a69c82016-12-27 14:52:32 -08003845 enforceSendSmsPermission();
Amit Mahajandccb3f12019-05-13 13:48:32 -07003846 SmsController smsController = PhoneFactory.getSmsController();
Philip P. Moltmann2f6f8ce2020-03-18 18:17:02 -07003847 smsController.sendVisualVoicemailSmsForSubscriber(callingPackage, callingAttributionTag,
3848 subId, number, port, text, sentIntent);
Ta-wei Yen87c49842016-05-13 21:19:52 -07003849 }
Amit Mahajandccb3f12019-05-13 13:48:32 -07003850
Shishir Agrawal76d5da92014-11-09 16:17:25 -08003851 /**
fionaxu0152e512016-11-14 13:36:14 -08003852 * Sets the voice activation state of a given subId.
3853 */
3854 @Override
3855 public void setVoiceActivationState(int subId, int activationState) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08003856 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
3857 mApp, subId, "setVoiceActivationState");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003858
3859 final long identity = Binder.clearCallingIdentity();
3860 try {
3861 final Phone phone = getPhone(subId);
3862 if (phone != null) {
3863 phone.setVoiceActivationState(activationState);
3864 } else {
3865 loge("setVoiceActivationState fails with invalid subId: " + subId);
3866 }
3867 } finally {
3868 Binder.restoreCallingIdentity(identity);
fionaxu0152e512016-11-14 13:36:14 -08003869 }
3870 }
3871
3872 /**
3873 * Sets the data activation state of a given subId.
3874 */
3875 @Override
3876 public void setDataActivationState(int subId, int activationState) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08003877 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
3878 mApp, subId, "setDataActivationState");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003879
3880 final long identity = Binder.clearCallingIdentity();
3881 try {
3882 final Phone phone = getPhone(subId);
3883 if (phone != null) {
3884 phone.setDataActivationState(activationState);
3885 } else {
Taesu Leef8fbed92019-10-07 18:47:02 +09003886 loge("setDataActivationState fails with invalid subId: " + subId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003887 }
3888 } finally {
3889 Binder.restoreCallingIdentity(identity);
fionaxu0152e512016-11-14 13:36:14 -08003890 }
3891 }
3892
3893 /**
3894 * Returns the voice activation state of a given subId.
3895 */
3896 @Override
3897 public int getVoiceActivationState(int subId, String callingPackage) {
Brad Ebinger35c841c2018-10-01 10:40:55 -07003898 enforceReadPrivilegedPermission("getVoiceActivationState");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003899
fionaxu0152e512016-11-14 13:36:14 -08003900 final Phone phone = getPhone(subId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003901 final long identity = Binder.clearCallingIdentity();
3902 try {
3903 if (phone != null) {
3904 return phone.getVoiceActivationState();
3905 } else {
3906 return TelephonyManager.SIM_ACTIVATION_STATE_UNKNOWN;
3907 }
3908 } finally {
3909 Binder.restoreCallingIdentity(identity);
fionaxu0152e512016-11-14 13:36:14 -08003910 }
3911 }
3912
3913 /**
3914 * Returns the data activation state of a given subId.
3915 */
3916 @Override
3917 public int getDataActivationState(int subId, String callingPackage) {
Brad Ebinger35c841c2018-10-01 10:40:55 -07003918 enforceReadPrivilegedPermission("getDataActivationState");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003919
fionaxu0152e512016-11-14 13:36:14 -08003920 final Phone phone = getPhone(subId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003921 final long identity = Binder.clearCallingIdentity();
3922 try {
3923 if (phone != null) {
3924 return phone.getDataActivationState();
3925 } else {
3926 return TelephonyManager.SIM_ACTIVATION_STATE_UNKNOWN;
3927 }
3928 } finally {
3929 Binder.restoreCallingIdentity(identity);
fionaxu0152e512016-11-14 13:36:14 -08003930 }
3931 }
3932
3933 /**
Wink Saville36469e72014-06-11 15:17:00 -07003934 * Returns the unread count of voicemails for a subId
3935 */
Sanket Padawe356d7632015-06-22 14:03:32 -07003936 @Override
Philip P. Moltmann700a9592019-10-03 11:53:50 -07003937 public int getVoiceMessageCountForSubscriber(int subId, String callingPackage,
3938 String callingFeatureId) {
Brad Ebingerf7664ba2018-11-29 12:43:38 -08003939 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
Philip P. Moltmann700a9592019-10-03 11:53:50 -07003940 mApp, subId, callingPackage, callingFeatureId,
3941 "getVoiceMessageCountForSubscriber")) {
Brad Ebingerf7664ba2018-11-29 12:43:38 -08003942 return 0;
3943 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003944 final long identity = Binder.clearCallingIdentity();
3945 try {
3946 final Phone phone = getPhone(subId);
3947 if (phone != null) {
3948 return phone.getVoiceMessageCount();
3949 } else {
3950 return 0;
3951 }
3952 } finally {
3953 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07003954 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003955 }
3956
3957 /**
pkanwar8a4dcfb2017-01-19 13:43:16 -08003958 * returns true, if the device is in a state where both voice and data
3959 * are supported simultaneously. This can change based on location or network condition.
3960 */
3961 @Override
3962 public boolean isConcurrentVoiceAndDataAllowed(int subId) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003963 final long identity = Binder.clearCallingIdentity();
3964 try {
3965 final Phone phone = getPhone(subId);
3966 return (phone == null ? false : phone.isConcurrentVoiceAndDataAllowed());
3967 } finally {
3968 Binder.restoreCallingIdentity(identity);
3969 }
pkanwar8a4dcfb2017-01-19 13:43:16 -08003970 }
3971
3972 /**
fionaxu235cc5e2017-03-06 22:25:57 -08003973 * Send the dialer code if called from the current default dialer or the caller has
3974 * carrier privilege.
3975 * @param inputCode The dialer code to send
3976 */
3977 @Override
3978 public void sendDialerSpecialCode(String callingPackage, String inputCode) {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08003979 final Phone defaultPhone = getDefaultPhone();
fionaxu235cc5e2017-03-06 22:25:57 -08003980 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
Tyler Gunn5ddfdc92019-10-31 13:08:23 -07003981 TelecomManager tm = defaultPhone.getContext().getSystemService(TelecomManager.class);
3982 String defaultDialer = tm.getDefaultDialerPackage();
fionaxu235cc5e2017-03-06 22:25:57 -08003983 if (!TextUtils.equals(callingPackage, defaultDialer)) {
Shuo Qian2c0ae432019-12-05 11:40:37 -08003984 TelephonyPermissions.enforceCallingOrSelfCarrierPrivilege(mApp,
Jeff Davidson7e17e312018-02-13 18:17:36 -08003985 getDefaultSubscription(), "sendDialerSpecialCode");
fionaxu235cc5e2017-03-06 22:25:57 -08003986 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003987
3988 final long identity = Binder.clearCallingIdentity();
3989 try {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08003990 defaultPhone.sendDialerSpecialCode(inputCode);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003991 } finally {
3992 Binder.restoreCallingIdentity(identity);
3993 }
fionaxu235cc5e2017-03-06 22:25:57 -08003994 }
3995
Pengquan Menga1bb6272018-09-06 09:59:22 -07003996 @Override
3997 public int getNetworkSelectionMode(int subId) {
shilufc958392020-01-20 11:36:01 -08003998 TelephonyPermissions
Nathan Harold62c68512021-04-06 11:26:02 -07003999 .enforceCallingOrSelfReadPrecisePhoneStatePermissionOrCarrierPrivilege(
shilufc958392020-01-20 11:36:01 -08004000 mApp, subId, "getNetworkSelectionMode");
4001 final long identity = Binder.clearCallingIdentity();
4002 try {
4003 if (!isActiveSubscription(subId)) {
4004 return TelephonyManager.NETWORK_SELECTION_MODE_UNKNOWN;
4005 }
4006 return (int) sendRequest(CMD_GET_NETWORK_SELECTION_MODE, null /* argument */, subId);
4007 } finally {
4008 Binder.restoreCallingIdentity(identity);
Pengquan Menge92a50d2018-09-21 15:54:48 -07004009 }
Pengquan Menga1bb6272018-09-06 09:59:22 -07004010 }
4011
Brad Ebinger35c841c2018-10-01 10:40:55 -07004012 @Override
Brad Ebingerb2b65522019-03-15 13:48:47 -07004013 public boolean isInEmergencySmsMode() {
4014 enforceReadPrivilegedPermission("isInEmergencySmsMode");
4015 final long identity = Binder.clearCallingIdentity();
4016 try {
4017 for (Phone phone : PhoneFactory.getPhones()) {
4018 if (phone.isInEmergencySmsMode()) {
4019 return true;
4020 }
4021 }
4022 } finally {
4023 Binder.restoreCallingIdentity(identity);
4024 }
4025 return false;
4026 }
4027
shilu366312e2019-12-17 09:28:10 -08004028 /**
4029 * Requires carrier privileges or READ_PRECISE_PHONE_STATE permission.
4030 * @param subId The subscription to use to check the configuration.
4031 * @param c The callback that will be used to send the result.
4032 */
Brad Ebingerb2b65522019-03-15 13:48:47 -07004033 @Override
Brad Ebinger9878b0b2018-11-08 17:43:22 -08004034 public void registerImsRegistrationCallback(int subId, IImsRegistrationCallback c)
4035 throws RemoteException {
Nathan Harold62c68512021-04-06 11:26:02 -07004036 TelephonyPermissions.enforceCallingOrSelfReadPrecisePhoneStatePermissionOrCarrierPrivilege(
Rambo Wang37f9c242020-02-10 14:45:28 -08004037 mApp, subId, "registerImsRegistrationCallback");
shilu366312e2019-12-17 09:28:10 -08004038
Brad Ebingerbc7dd582019-10-17 17:03:22 -07004039 if (!ImsManager.isImsSupportedOnDevice(mApp)) {
4040 throw new ServiceSpecificException(ImsException.CODE_ERROR_UNSUPPORTED_OPERATION,
4041 "IMS not available on device.");
4042 }
Brad Ebinger35c841c2018-10-01 10:40:55 -07004043 final long token = Binder.clearCallingIdentity();
4044 try {
Brad Ebinger919631e2021-06-02 17:46:35 -07004045 int slotId = getSlotIndexOrException(subId);
4046 verifyImsMmTelConfiguredOrThrow(slotId);
4047 ImsManager.getInstance(mApp, slotId).addRegistrationCallbackForSubscription(c, subId);
Brad Ebinger1ce9c432019-07-16 13:19:44 -07004048 } catch (ImsException e) {
4049 throw new ServiceSpecificException(e.getCode());
Brad Ebinger35c841c2018-10-01 10:40:55 -07004050 } finally {
4051 Binder.restoreCallingIdentity(token);
4052 }
4053 }
4054
shilu366312e2019-12-17 09:28:10 -08004055 /**
4056 * Requires carrier privileges or READ_PRECISE_PHONE_STATE permission.
4057 * @param subId The subscription to use to check the configuration.
4058 * @param c The callback that will be used to send the result.
4059 */
Brad Ebinger35c841c2018-10-01 10:40:55 -07004060 @Override
Brad Ebinger9878b0b2018-11-08 17:43:22 -08004061 public void unregisterImsRegistrationCallback(int subId, IImsRegistrationCallback c) {
Nathan Harold62c68512021-04-06 11:26:02 -07004062 TelephonyPermissions.enforceCallingOrSelfReadPrecisePhoneStatePermissionOrCarrierPrivilege(
Rambo Wang37f9c242020-02-10 14:45:28 -08004063 mApp, subId, "unregisterImsRegistrationCallback");
Brad Ebinger4ae57f92019-01-09 16:51:30 -08004064 if (!SubscriptionManager.isValidSubscriptionId(subId)) {
4065 throw new IllegalArgumentException("Invalid Subscription ID: " + subId);
4066 }
Meng Wangafbc5852019-09-19 17:37:13 -07004067 final long token = Binder.clearCallingIdentity();
4068 try {
Meng Wangafbc5852019-09-19 17:37:13 -07004069 ImsManager.getInstance(mApp, getSlotIndexOrException(subId))
4070 .removeRegistrationCallbackForSubscription(c, subId);
4071 } catch (ImsException e) {
4072 Log.i(LOG_TAG, "unregisterImsRegistrationCallback: " + subId
4073 + "is inactive, ignoring unregister.");
4074 // If the subscription is no longer active, just return, since the callback
4075 // will already have been removed internally.
4076 } finally {
4077 Binder.restoreCallingIdentity(token);
4078 }
Brad Ebinger35c841c2018-10-01 10:40:55 -07004079 }
4080
Brad Ebingera34a6c22019-10-22 17:36:18 -07004081 /**
4082 * Get the IMS service registration state for the MmTelFeature associated with this sub id.
4083 */
4084 @Override
4085 public void getImsMmTelRegistrationState(int subId, IIntegerConsumer consumer) {
4086 enforceReadPrivilegedPermission("getImsMmTelRegistrationState");
4087 if (!ImsManager.isImsSupportedOnDevice(mApp)) {
4088 throw new ServiceSpecificException(ImsException.CODE_ERROR_UNSUPPORTED_OPERATION,
4089 "IMS not available on device.");
4090 }
4091 final long token = Binder.clearCallingIdentity();
4092 try {
4093 Phone phone = getPhone(subId);
4094 if (phone == null) {
4095 Log.w(LOG_TAG, "getImsMmTelRegistrationState: called with an invalid subscription '"
4096 + subId + "'");
4097 throw new ServiceSpecificException(ImsException.CODE_ERROR_INVALID_SUBSCRIPTION);
4098 }
4099 phone.getImsRegistrationState(regState -> {
4100 try {
4101 consumer.accept((regState == null)
4102 ? RegistrationManager.REGISTRATION_STATE_NOT_REGISTERED : regState);
4103 } catch (RemoteException e) {
4104 // Ignore if the remote process is no longer available to call back.
4105 Log.w(LOG_TAG, "getImsMmTelRegistrationState: callback not available.");
4106 }
4107 });
4108 } finally {
4109 Binder.restoreCallingIdentity(token);
4110 }
4111 }
4112
4113 /**
4114 * Get the transport type for the IMS service registration state.
4115 */
4116 @Override
4117 public void getImsMmTelRegistrationTransportType(int subId, IIntegerConsumer consumer) {
Nathan Harold62c68512021-04-06 11:26:02 -07004118 TelephonyPermissions.enforceCallingOrSelfReadPrecisePhoneStatePermissionOrCarrierPrivilege(
Rambo Wang37f9c242020-02-10 14:45:28 -08004119 mApp, subId, "getImsMmTelRegistrationTransportType");
Brad Ebingera34a6c22019-10-22 17:36:18 -07004120 if (!ImsManager.isImsSupportedOnDevice(mApp)) {
4121 throw new ServiceSpecificException(ImsException.CODE_ERROR_UNSUPPORTED_OPERATION,
4122 "IMS not available on device.");
4123 }
4124 final long token = Binder.clearCallingIdentity();
4125 try {
4126 Phone phone = getPhone(subId);
4127 if (phone == null) {
4128 Log.w(LOG_TAG, "getImsMmTelRegistrationState: called with an invalid subscription '"
4129 + subId + "'");
4130 throw new ServiceSpecificException(ImsException.CODE_ERROR_INVALID_SUBSCRIPTION);
4131 }
4132 phone.getImsRegistrationTech(regTech -> {
4133 // Convert registration tech from ImsRegistrationImplBase -> RegistrationManager
4134 int regTechConverted = (regTech == null)
4135 ? ImsRegistrationImplBase.REGISTRATION_TECH_NONE : regTech;
4136 regTechConverted = RegistrationManager.IMS_REG_TO_ACCESS_TYPE_MAP.get(
4137 regTechConverted);
4138 try {
4139 consumer.accept(regTechConverted);
4140 } catch (RemoteException e) {
4141 // Ignore if the remote process is no longer available to call back.
4142 Log.w(LOG_TAG, "getImsMmTelRegistrationState: callback not available.");
4143 }
4144 });
4145 } finally {
4146 Binder.restoreCallingIdentity(token);
4147 }
4148 }
4149
shilu366312e2019-12-17 09:28:10 -08004150 /**
4151 * Requires carrier privileges or READ_PRECISE_PHONE_STATE permission.
4152 * @param subId The subscription to use to check the configuration.
4153 * @param c The callback that will be used to send the result.
4154 */
Brad Ebinger35c841c2018-10-01 10:40:55 -07004155 @Override
Brad Ebinger9878b0b2018-11-08 17:43:22 -08004156 public void registerMmTelCapabilityCallback(int subId, IImsCapabilityCallback c)
4157 throws RemoteException {
Nathan Harold62c68512021-04-06 11:26:02 -07004158 TelephonyPermissions.enforceCallingOrSelfReadPrecisePhoneStatePermissionOrCarrierPrivilege(
Rambo Wang37f9c242020-02-10 14:45:28 -08004159 mApp, subId, "registerMmTelCapabilityCallback");
Brad Ebingerbc7dd582019-10-17 17:03:22 -07004160 if (!ImsManager.isImsSupportedOnDevice(mApp)) {
4161 throw new ServiceSpecificException(ImsException.CODE_ERROR_UNSUPPORTED_OPERATION,
4162 "IMS not available on device.");
4163 }
Brad Ebinger35c841c2018-10-01 10:40:55 -07004164 final long token = Binder.clearCallingIdentity();
4165 try {
Brad Ebinger919631e2021-06-02 17:46:35 -07004166 int slotId = getSlotIndexOrException(subId);
4167 verifyImsMmTelConfiguredOrThrow(slotId);
4168 ImsManager.getInstance(mApp, slotId).addCapabilitiesCallbackForSubscription(c, subId);
Brad Ebinger1ce9c432019-07-16 13:19:44 -07004169 } catch (ImsException e) {
4170 throw new ServiceSpecificException(e.getCode());
Brad Ebinger35c841c2018-10-01 10:40:55 -07004171 } finally {
4172 Binder.restoreCallingIdentity(token);
4173 }
4174 }
4175
shilu366312e2019-12-17 09:28:10 -08004176 /**
4177 * Requires carrier privileges or READ_PRECISE_PHONE_STATE permission.
4178 * @param subId The subscription to use to check the configuration.
4179 * @param c The callback that will be used to send the result.
4180 */
Brad Ebinger35c841c2018-10-01 10:40:55 -07004181 @Override
Brad Ebinger9878b0b2018-11-08 17:43:22 -08004182 public void unregisterMmTelCapabilityCallback(int subId, IImsCapabilityCallback c) {
Nathan Harold62c68512021-04-06 11:26:02 -07004183 TelephonyPermissions.enforceCallingOrSelfReadPrecisePhoneStatePermissionOrCarrierPrivilege(
Rambo Wang37f9c242020-02-10 14:45:28 -08004184 mApp, subId, "unregisterMmTelCapabilityCallback");
Brad Ebinger4ae57f92019-01-09 16:51:30 -08004185 if (!SubscriptionManager.isValidSubscriptionId(subId)) {
4186 throw new IllegalArgumentException("Invalid Subscription ID: " + subId);
4187 }
Meng Wangafbc5852019-09-19 17:37:13 -07004188
4189 final long token = Binder.clearCallingIdentity();
4190 try {
Meng Wangafbc5852019-09-19 17:37:13 -07004191 ImsManager.getInstance(mApp, getSlotIndexOrException(subId))
Brad Ebinger4ae57f92019-01-09 16:51:30 -08004192 .removeCapabilitiesCallbackForSubscription(c, subId);
Meng Wangafbc5852019-09-19 17:37:13 -07004193 } catch (ImsException e) {
4194 Log.i(LOG_TAG, "unregisterMmTelCapabilityCallback: " + subId
4195 + "is inactive, ignoring unregister.");
4196 // If the subscription is no longer active, just return, since the callback
4197 // will already have been removed internally.
4198 } finally {
4199 Binder.restoreCallingIdentity(token);
4200 }
Brad Ebinger35c841c2018-10-01 10:40:55 -07004201 }
4202
4203 @Override
Brad Ebinger9878b0b2018-11-08 17:43:22 -08004204 public boolean isCapable(int subId, int capability, int regTech) {
4205 enforceReadPrivilegedPermission("isCapable");
Brad Ebinger35c841c2018-10-01 10:40:55 -07004206 final long token = Binder.clearCallingIdentity();
4207 try {
Brad Ebinger919631e2021-06-02 17:46:35 -07004208 int slotId = getSlotIndexOrException(subId);
4209 verifyImsMmTelConfiguredOrThrow(slotId);
4210 return ImsManager.getInstance(mApp, slotId).queryMmTelCapability(capability, regTech);
Brad Ebinger1ce9c432019-07-16 13:19:44 -07004211 } catch (com.android.ims.ImsException e) {
Brad Ebinger35c841c2018-10-01 10:40:55 -07004212 Log.w(LOG_TAG, "IMS isCapable - service unavailable: " + e.getMessage());
4213 return false;
Brad Ebinger1ce9c432019-07-16 13:19:44 -07004214 } catch (ImsException e) {
Brad Ebinger6b5ac222019-02-04 14:36:52 -08004215 Log.i(LOG_TAG, "isCapable: " + subId + " is inactive, returning false.");
4216 return false;
Brad Ebinger35c841c2018-10-01 10:40:55 -07004217 } finally {
4218 Binder.restoreCallingIdentity(token);
4219 }
4220 }
4221
4222 @Override
Brad Ebinger9878b0b2018-11-08 17:43:22 -08004223 public boolean isAvailable(int subId, int capability, int regTech) {
4224 enforceReadPrivilegedPermission("isAvailable");
Brad Ebinger35c841c2018-10-01 10:40:55 -07004225 final long token = Binder.clearCallingIdentity();
4226 try {
4227 Phone phone = getPhone(subId);
4228 if (phone == null) return false;
4229 return phone.isImsCapabilityAvailable(capability, regTech);
Daniel Bright5e40e4e2020-03-11 16:35:39 -07004230 } catch (com.android.ims.ImsException e) {
4231 Log.w(LOG_TAG, "IMS isAvailable - service unavailable: " + e.getMessage());
4232 return false;
Brad Ebinger35c841c2018-10-01 10:40:55 -07004233 } finally {
4234 Binder.restoreCallingIdentity(token);
4235 }
4236 }
4237
Brad Ebingerbc7dd582019-10-17 17:03:22 -07004238 /**
4239 * Determines if the MmTel feature capability is supported by the carrier configuration for this
4240 * subscription.
4241 * @param subId The subscription to use to check the configuration.
4242 * @param callback The callback that will be used to send the result.
4243 * @param capability The MmTelFeature capability that will be used to send the result.
4244 * @param transportType The transport type of the MmTelFeature capability.
4245 */
4246 @Override
4247 public void isMmTelCapabilitySupported(int subId, IIntegerConsumer callback, int capability,
4248 int transportType) {
4249 enforceReadPrivilegedPermission("isMmTelCapabilitySupported");
4250 if (!ImsManager.isImsSupportedOnDevice(mApp)) {
4251 throw new ServiceSpecificException(ImsException.CODE_ERROR_UNSUPPORTED_OPERATION,
4252 "IMS not available on device.");
4253 }
4254 final long token = Binder.clearCallingIdentity();
4255 try {
4256 int slotId = getSlotIndex(subId);
4257 if (slotId <= SubscriptionManager.INVALID_SIM_SLOT_INDEX) {
4258 Log.w(LOG_TAG, "isMmTelCapabilitySupported: called with an inactive subscription '"
4259 + subId + "'");
4260 throw new ServiceSpecificException(ImsException.CODE_ERROR_INVALID_SUBSCRIPTION);
4261 }
Brad Ebinger919631e2021-06-02 17:46:35 -07004262 verifyImsMmTelConfiguredOrThrow(slotId);
Brad Ebingerbc7dd582019-10-17 17:03:22 -07004263 ImsManager.getInstance(mApp, slotId).isSupported(capability,
4264 transportType, aBoolean -> {
4265 try {
4266 callback.accept((aBoolean == null) ? 0 : (aBoolean ? 1 : 0));
4267 } catch (RemoteException e) {
4268 Log.w(LOG_TAG, "isMmTelCapabilitySupported: remote caller is not "
4269 + "running. Ignore");
4270 }
4271 });
Brad Ebinger919631e2021-06-02 17:46:35 -07004272 } catch (ImsException e) {
4273 throw new ServiceSpecificException(e.getCode());
Brad Ebingerbc7dd582019-10-17 17:03:22 -07004274 } finally {
4275 Binder.restoreCallingIdentity(token);
4276 }
4277 }
4278
shilu366312e2019-12-17 09:28:10 -08004279 /**
4280 * Requires carrier privileges or READ_PRECISE_PHONE_STATE permission.
4281 * @param subId The subscription to use to check the configuration.
4282 */
Brad Ebinger35c841c2018-10-01 10:40:55 -07004283 @Override
4284 public boolean isAdvancedCallingSettingEnabled(int subId) {
Nathan Harold62c68512021-04-06 11:26:02 -07004285 TelephonyPermissions.enforceCallingOrSelfReadPrecisePhoneStatePermissionOrCarrierPrivilege(
Rambo Wang37f9c242020-02-10 14:45:28 -08004286 mApp, subId, "isAdvancedCallingSettingEnabled");
shilu366312e2019-12-17 09:28:10 -08004287
Brad Ebinger35c841c2018-10-01 10:40:55 -07004288 final long token = Binder.clearCallingIdentity();
4289 try {
Brad Ebinger919631e2021-06-02 17:46:35 -07004290 int slotId = getSlotIndexOrException(subId);
Brad Ebinger735c5ce2021-07-12 13:58:21 -07004291 // This setting doesn't require an active ImsService connection, so do not verify.
Brad Ebinger919631e2021-06-02 17:46:35 -07004292 return ImsManager.getInstance(mApp, slotId).isEnhanced4gLteModeSettingEnabledByUser();
Brad Ebinger1ce9c432019-07-16 13:19:44 -07004293 } catch (ImsException e) {
4294 throw new ServiceSpecificException(e.getCode());
Brad Ebinger35c841c2018-10-01 10:40:55 -07004295 } finally {
4296 Binder.restoreCallingIdentity(token);
4297 }
4298 }
4299
4300 @Override
Brad Ebinger1c162042019-02-21 14:49:10 -08004301 public void setAdvancedCallingSettingEnabled(int subId, boolean isEnabled) {
Brad Ebinger35c841c2018-10-01 10:40:55 -07004302 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(mApp, subId,
Brad Ebinger1c162042019-02-21 14:49:10 -08004303 "setAdvancedCallingSettingEnabled");
Brad Ebinger35c841c2018-10-01 10:40:55 -07004304 final long identity = Binder.clearCallingIdentity();
4305 try {
Brad Ebinger919631e2021-06-02 17:46:35 -07004306 int slotId = getSlotIndexOrException(subId);
Brad Ebinger735c5ce2021-07-12 13:58:21 -07004307 // This setting doesn't require an active ImsService connection, so do not verify. The
4308 // new setting will be picked up when the ImsService comes up next if it isn't up.
Brad Ebinger919631e2021-06-02 17:46:35 -07004309 ImsManager.getInstance(mApp, slotId).setEnhanced4gLteModeSetting(isEnabled);
Brad Ebinger1ce9c432019-07-16 13:19:44 -07004310 } catch (ImsException e) {
4311 throw new ServiceSpecificException(e.getCode());
Brad Ebinger35c841c2018-10-01 10:40:55 -07004312 } finally {
4313 Binder.restoreCallingIdentity(identity);
4314 }
4315 }
4316
shilu366312e2019-12-17 09:28:10 -08004317 /**
4318 * Requires carrier privileges or READ_PRECISE_PHONE_STATE permission.
4319 * @param subId The subscription to use to check the configuration.
4320 */
Brad Ebinger35c841c2018-10-01 10:40:55 -07004321 @Override
Brad Ebinger9878b0b2018-11-08 17:43:22 -08004322 public boolean isVtSettingEnabled(int subId) {
Nathan Harold62c68512021-04-06 11:26:02 -07004323 TelephonyPermissions.enforceCallingOrSelfReadPrecisePhoneStatePermissionOrCarrierPrivilege(
Rambo Wang37f9c242020-02-10 14:45:28 -08004324 mApp, subId, "isVtSettingEnabled");
Brad Ebinger35c841c2018-10-01 10:40:55 -07004325 final long identity = Binder.clearCallingIdentity();
4326 try {
Brad Ebinger919631e2021-06-02 17:46:35 -07004327 int slotId = getSlotIndexOrException(subId);
Brad Ebinger735c5ce2021-07-12 13:58:21 -07004328 // This setting doesn't require an active ImsService connection, so do not verify.
Brad Ebinger919631e2021-06-02 17:46:35 -07004329 return ImsManager.getInstance(mApp, slotId).isVtEnabledByUser();
Brad Ebinger1ce9c432019-07-16 13:19:44 -07004330 } catch (ImsException e) {
4331 throw new ServiceSpecificException(e.getCode());
Brad Ebinger35c841c2018-10-01 10:40:55 -07004332 } finally {
4333 Binder.restoreCallingIdentity(identity);
4334 }
4335 }
4336
4337 @Override
Brad Ebinger1c162042019-02-21 14:49:10 -08004338 public void setVtSettingEnabled(int subId, boolean isEnabled) {
Brad Ebinger35c841c2018-10-01 10:40:55 -07004339 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(mApp, subId,
Brad Ebinger1c162042019-02-21 14:49:10 -08004340 "setVtSettingEnabled");
Brad Ebinger35c841c2018-10-01 10:40:55 -07004341 final long identity = Binder.clearCallingIdentity();
4342 try {
Brad Ebinger919631e2021-06-02 17:46:35 -07004343 int slotId = getSlotIndexOrException(subId);
Brad Ebinger735c5ce2021-07-12 13:58:21 -07004344 // This setting doesn't require an active ImsService connection, so do not verify. The
4345 // new setting will be picked up when the ImsService comes up next if it isn't up.
Brad Ebinger919631e2021-06-02 17:46:35 -07004346 ImsManager.getInstance(mApp, slotId).setVtSetting(isEnabled);
Brad Ebinger1ce9c432019-07-16 13:19:44 -07004347 } catch (ImsException e) {
4348 throw new ServiceSpecificException(e.getCode());
Brad Ebinger35c841c2018-10-01 10:40:55 -07004349 } finally {
4350 Binder.restoreCallingIdentity(identity);
4351 }
4352 }
4353
shilu366312e2019-12-17 09:28:10 -08004354 /**
4355 * Requires carrier privileges or READ_PRECISE_PHONE_STATE permission.
4356 * @param subId The subscription to use to check the configuration.
4357 */
Brad Ebinger35c841c2018-10-01 10:40:55 -07004358 @Override
4359 public boolean isVoWiFiSettingEnabled(int subId) {
Nathan Harold62c68512021-04-06 11:26:02 -07004360 TelephonyPermissions.enforceCallingOrSelfReadPrecisePhoneStatePermissionOrCarrierPrivilege(
Rambo Wang37f9c242020-02-10 14:45:28 -08004361 mApp, subId, "isVoWiFiSettingEnabled");
Brad Ebinger35c841c2018-10-01 10:40:55 -07004362 final long identity = Binder.clearCallingIdentity();
4363 try {
Brad Ebinger919631e2021-06-02 17:46:35 -07004364 int slotId = getSlotIndexOrException(subId);
Brad Ebinger735c5ce2021-07-12 13:58:21 -07004365 // This setting doesn't require an active ImsService connection, so do not verify.
Brad Ebinger919631e2021-06-02 17:46:35 -07004366 return ImsManager.getInstance(mApp, slotId).isWfcEnabledByUser();
Brad Ebinger1ce9c432019-07-16 13:19:44 -07004367 } catch (ImsException e) {
4368 throw new ServiceSpecificException(e.getCode());
Brad Ebinger35c841c2018-10-01 10:40:55 -07004369 } finally {
4370 Binder.restoreCallingIdentity(identity);
4371 }
4372 }
4373
4374 @Override
Brad Ebinger1c162042019-02-21 14:49:10 -08004375 public void setVoWiFiSettingEnabled(int subId, boolean isEnabled) {
Brad Ebinger35c841c2018-10-01 10:40:55 -07004376 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(mApp, subId,
Brad Ebinger1c162042019-02-21 14:49:10 -08004377 "setVoWiFiSettingEnabled");
Brad Ebinger35c841c2018-10-01 10:40:55 -07004378 final long identity = Binder.clearCallingIdentity();
4379 try {
Brad Ebinger919631e2021-06-02 17:46:35 -07004380 int slotId = getSlotIndexOrException(subId);
Brad Ebinger735c5ce2021-07-12 13:58:21 -07004381 // This setting doesn't require an active ImsService connection, so do not verify. The
4382 // new setting will be picked up when the ImsService comes up next if it isn't up.
Brad Ebinger919631e2021-06-02 17:46:35 -07004383 ImsManager.getInstance(mApp, slotId).setWfcSetting(isEnabled);
Brad Ebinger1ce9c432019-07-16 13:19:44 -07004384 } catch (ImsException e) {
4385 throw new ServiceSpecificException(e.getCode());
Brad Ebinger35c841c2018-10-01 10:40:55 -07004386 } finally {
4387 Binder.restoreCallingIdentity(identity);
4388 }
4389 }
4390
shilu366312e2019-12-17 09:28:10 -08004391 /**
Sooraj Sasindrane655add2020-11-23 19:40:38 -08004392 * @return true if the user's setting for Voice over Cross SIM is enabled and false if it is not
4393 * Requires carrier privileges or READ_PRECISE_PHONE_STATE permission.
4394 * @param subId The subscription to use to check the configuration.
4395 */
4396 @Override
4397 public boolean isCrossSimCallingEnabledByUser(int subId) {
Nathan Harold62c68512021-04-06 11:26:02 -07004398 TelephonyPermissions.enforceCallingOrSelfReadPrecisePhoneStatePermissionOrCarrierPrivilege(
Sooraj Sasindrane655add2020-11-23 19:40:38 -08004399 mApp, subId, "isCrossSimCallingEnabledByUser");
4400 final long identity = Binder.clearCallingIdentity();
4401 try {
Brad Ebinger919631e2021-06-02 17:46:35 -07004402 int slotId = getSlotIndexOrException(subId);
Brad Ebinger735c5ce2021-07-12 13:58:21 -07004403 // This setting doesn't require an active ImsService connection, so do not verify.
Brad Ebinger919631e2021-06-02 17:46:35 -07004404 return ImsManager.getInstance(mApp, slotId).isCrossSimCallingEnabledByUser();
Sooraj Sasindrane655add2020-11-23 19:40:38 -08004405 } catch (ImsException e) {
4406 throw new ServiceSpecificException(e.getCode());
4407 } finally {
4408 Binder.restoreCallingIdentity(identity);
4409 }
4410 }
4411
4412 /**
4413 * Sets the user's setting for whether or not Voice over Cross SIM is enabled.
4414 * Requires MODIFY_PHONE_STATE permission.
4415 * @param subId The subscription to use to check the configuration.
4416 * @param isEnabled true if the user's setting for Voice over Cross SIM is enabled,
4417 * false otherwise
4418 */
4419 @Override
4420 public void setCrossSimCallingEnabled(int subId, boolean isEnabled) {
4421 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(mApp, subId,
4422 "setCrossSimCallingEnabled");
4423 final long identity = Binder.clearCallingIdentity();
4424 try {
Brad Ebinger919631e2021-06-02 17:46:35 -07004425 int slotId = getSlotIndexOrException(subId);
Brad Ebinger735c5ce2021-07-12 13:58:21 -07004426 // This setting doesn't require an active ImsService connection, so do not verify. The
4427 // new setting will be picked up when the ImsService comes up next if it isn't up.
Brad Ebinger919631e2021-06-02 17:46:35 -07004428 ImsManager.getInstance(mApp, slotId).setCrossSimCallingEnabled(isEnabled);
Sooraj Sasindrane655add2020-11-23 19:40:38 -08004429 } catch (ImsException e) {
4430 throw new ServiceSpecificException(e.getCode());
4431 } finally {
4432 Binder.restoreCallingIdentity(identity);
4433 }
4434 }
4435
4436 /**
shilu366312e2019-12-17 09:28:10 -08004437 * Requires carrier privileges or READ_PRECISE_PHONE_STATE permission.
4438 * @param subId The subscription to use to check the configuration.
4439 */
Brad Ebinger35c841c2018-10-01 10:40:55 -07004440 @Override
Nathan Harold62c68512021-04-06 11:26:02 -07004441
Brad Ebinger35c841c2018-10-01 10:40:55 -07004442 public boolean isVoWiFiRoamingSettingEnabled(int subId) {
Nathan Harold62c68512021-04-06 11:26:02 -07004443 TelephonyPermissions.enforceCallingOrSelfReadPrecisePhoneStatePermissionOrCarrierPrivilege(
Rambo Wang37f9c242020-02-10 14:45:28 -08004444 mApp, subId, "isVoWiFiRoamingSettingEnabled");
Brad Ebinger35c841c2018-10-01 10:40:55 -07004445 final long identity = Binder.clearCallingIdentity();
4446 try {
Brad Ebinger919631e2021-06-02 17:46:35 -07004447 int slotId = getSlotIndexOrException(subId);
Brad Ebinger735c5ce2021-07-12 13:58:21 -07004448 // This setting doesn't require an active ImsService connection, so do not verify.
Brad Ebinger919631e2021-06-02 17:46:35 -07004449 return ImsManager.getInstance(mApp, slotId).isWfcRoamingEnabledByUser();
Brad Ebinger1ce9c432019-07-16 13:19:44 -07004450 } catch (ImsException e) {
4451 throw new ServiceSpecificException(e.getCode());
Brad Ebinger35c841c2018-10-01 10:40:55 -07004452 } finally {
4453 Binder.restoreCallingIdentity(identity);
4454 }
4455 }
4456
4457 @Override
Brad Ebinger1c162042019-02-21 14:49:10 -08004458 public void setVoWiFiRoamingSettingEnabled(int subId, boolean isEnabled) {
Brad Ebinger35c841c2018-10-01 10:40:55 -07004459 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(mApp, subId,
Brad Ebinger1c162042019-02-21 14:49:10 -08004460 "setVoWiFiRoamingSettingEnabled");
Brad Ebinger35c841c2018-10-01 10:40:55 -07004461 final long identity = Binder.clearCallingIdentity();
4462 try {
Brad Ebinger919631e2021-06-02 17:46:35 -07004463 int slotId = getSlotIndexOrException(subId);
Brad Ebinger735c5ce2021-07-12 13:58:21 -07004464 // This setting doesn't require an active ImsService connection, so do not verify. The
4465 // new setting will be picked up when the ImsService comes up next if it isn't up.
Brad Ebinger919631e2021-06-02 17:46:35 -07004466 ImsManager.getInstance(mApp, slotId).setWfcRoamingSetting(isEnabled);
Brad Ebinger1ce9c432019-07-16 13:19:44 -07004467 } catch (ImsException e) {
4468 throw new ServiceSpecificException(e.getCode());
Brad Ebinger35c841c2018-10-01 10:40:55 -07004469 } finally {
4470 Binder.restoreCallingIdentity(identity);
4471 }
4472 }
4473
4474 @Override
4475 public void setVoWiFiNonPersistent(int subId, boolean isCapable, int mode) {
4476 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(mApp, subId,
4477 "setVoWiFiNonPersistent");
4478 final long identity = Binder.clearCallingIdentity();
4479 try {
Brad Ebinger919631e2021-06-02 17:46:35 -07004480 int slotId = getSlotIndexOrException(subId);
Brad Ebinger735c5ce2021-07-12 13:58:21 -07004481 // This setting will be ignored if the ImsService isn't up.
Brad Ebinger919631e2021-06-02 17:46:35 -07004482 ImsManager.getInstance(mApp, slotId).setWfcNonPersistent(isCapable, mode);
Brad Ebinger1ce9c432019-07-16 13:19:44 -07004483 } catch (ImsException e) {
4484 throw new ServiceSpecificException(e.getCode());
Brad Ebinger35c841c2018-10-01 10:40:55 -07004485 } finally {
4486 Binder.restoreCallingIdentity(identity);
4487 }
4488 }
4489
shilu366312e2019-12-17 09:28:10 -08004490 /**
4491 * Requires carrier privileges or READ_PRECISE_PHONE_STATE permission.
4492 * @param subId The subscription to use to check the configuration.
4493 */
Brad Ebinger35c841c2018-10-01 10:40:55 -07004494 @Override
4495 public int getVoWiFiModeSetting(int subId) {
Nathan Harold62c68512021-04-06 11:26:02 -07004496 TelephonyPermissions.enforceCallingOrSelfReadPrecisePhoneStatePermissionOrCarrierPrivilege(
Rambo Wang37f9c242020-02-10 14:45:28 -08004497 mApp, subId, "getVoWiFiModeSetting");
Brad Ebinger35c841c2018-10-01 10:40:55 -07004498 final long identity = Binder.clearCallingIdentity();
4499 try {
Brad Ebinger919631e2021-06-02 17:46:35 -07004500 int slotId = getSlotIndexOrException(subId);
Brad Ebinger735c5ce2021-07-12 13:58:21 -07004501 // This setting doesn't require an active ImsService connection, so do not verify.
Brad Ebinger919631e2021-06-02 17:46:35 -07004502 return ImsManager.getInstance(mApp, slotId).getWfcMode(false /*isRoaming*/);
Brad Ebinger1ce9c432019-07-16 13:19:44 -07004503 } catch (ImsException e) {
4504 throw new ServiceSpecificException(e.getCode());
Brad Ebinger35c841c2018-10-01 10:40:55 -07004505 } finally {
4506 Binder.restoreCallingIdentity(identity);
4507 }
4508 }
4509
4510 @Override
4511 public void setVoWiFiModeSetting(int subId, int mode) {
4512 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(mApp, subId,
4513 "setVoWiFiModeSetting");
4514 final long identity = Binder.clearCallingIdentity();
4515 try {
Brad Ebinger919631e2021-06-02 17:46:35 -07004516 int slotId = getSlotIndexOrException(subId);
Brad Ebinger735c5ce2021-07-12 13:58:21 -07004517 // This setting doesn't require an active ImsService connection, so do not verify. The
4518 // new setting will be picked up when the ImsService comes up next if it isn't up.
Brad Ebinger919631e2021-06-02 17:46:35 -07004519 ImsManager.getInstance(mApp, slotId).setWfcMode(mode, false /*isRoaming*/);
Brad Ebinger1ce9c432019-07-16 13:19:44 -07004520 } catch (ImsException e) {
4521 throw new ServiceSpecificException(e.getCode());
Brad Ebinger35c841c2018-10-01 10:40:55 -07004522 } finally {
4523 Binder.restoreCallingIdentity(identity);
4524 }
4525 }
4526
4527 @Override
4528 public int getVoWiFiRoamingModeSetting(int subId) {
4529 enforceReadPrivilegedPermission("getVoWiFiRoamingModeSetting");
4530 final long identity = Binder.clearCallingIdentity();
4531 try {
Brad Ebinger919631e2021-06-02 17:46:35 -07004532 int slotId = getSlotIndexOrException(subId);
Brad Ebinger735c5ce2021-07-12 13:58:21 -07004533 // This setting doesn't require an active ImsService connection, so do not verify.
Brad Ebinger919631e2021-06-02 17:46:35 -07004534 return ImsManager.getInstance(mApp, slotId).getWfcMode(true /*isRoaming*/);
Brad Ebinger1ce9c432019-07-16 13:19:44 -07004535 } catch (ImsException e) {
4536 throw new ServiceSpecificException(e.getCode());
Brad Ebinger35c841c2018-10-01 10:40:55 -07004537 } finally {
4538 Binder.restoreCallingIdentity(identity);
4539 }
4540 }
4541
4542 @Override
4543 public void setVoWiFiRoamingModeSetting(int subId, int mode) {
4544 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(mApp, subId,
4545 "setVoWiFiRoamingModeSetting");
4546 final long identity = Binder.clearCallingIdentity();
4547 try {
Brad Ebinger919631e2021-06-02 17:46:35 -07004548 int slotId = getSlotIndexOrException(subId);
Brad Ebinger735c5ce2021-07-12 13:58:21 -07004549 // This setting doesn't require an active ImsService connection, so do not verify. The
4550 // new setting will be picked up when the ImsService comes up next if it isn't up.
Brad Ebinger919631e2021-06-02 17:46:35 -07004551 ImsManager.getInstance(mApp, slotId).setWfcMode(mode, true /*isRoaming*/);
Brad Ebinger1ce9c432019-07-16 13:19:44 -07004552 } catch (ImsException e) {
4553 throw new ServiceSpecificException(e.getCode());
Brad Ebinger35c841c2018-10-01 10:40:55 -07004554 } finally {
4555 Binder.restoreCallingIdentity(identity);
4556 }
4557 }
4558
4559 @Override
4560 public void setRttCapabilitySetting(int subId, boolean isEnabled) {
4561 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(mApp, subId,
4562 "setRttCapabilityEnabled");
4563 final long identity = Binder.clearCallingIdentity();
4564 try {
Brad Ebinger919631e2021-06-02 17:46:35 -07004565 int slotId = getSlotIndexOrException(subId);
Brad Ebinger735c5ce2021-07-12 13:58:21 -07004566 // This setting doesn't require an active ImsService connection, so do not verify. The
4567 // new setting will be picked up when the ImsService comes up next if it isn't up.
Brad Ebinger919631e2021-06-02 17:46:35 -07004568 ImsManager.getInstance(mApp, slotId).setRttEnabled(isEnabled);
Brad Ebinger1ce9c432019-07-16 13:19:44 -07004569 } catch (ImsException e) {
4570 throw new ServiceSpecificException(e.getCode());
Brad Ebinger35c841c2018-10-01 10:40:55 -07004571 } finally {
4572 Binder.restoreCallingIdentity(identity);
4573 }
4574 }
4575
shilu366312e2019-12-17 09:28:10 -08004576 /**
4577 * Requires carrier privileges or READ_PRECISE_PHONE_STATE permission.
4578 * @param subId The subscription to use to check the configuration.
4579 */
Brad Ebinger35c841c2018-10-01 10:40:55 -07004580 @Override
4581 public boolean isTtyOverVolteEnabled(int subId) {
Nathan Harold62c68512021-04-06 11:26:02 -07004582 TelephonyPermissions.enforceCallingOrSelfReadPrecisePhoneStatePermissionOrCarrierPrivilege(
Rambo Wang37f9c242020-02-10 14:45:28 -08004583 mApp, subId, "isTtyOverVolteEnabled");
Brad Ebinger35c841c2018-10-01 10:40:55 -07004584 final long identity = Binder.clearCallingIdentity();
4585 try {
Brad Ebinger919631e2021-06-02 17:46:35 -07004586 int slotId = getSlotIndexOrException(subId);
Brad Ebinger735c5ce2021-07-12 13:58:21 -07004587 // This setting doesn't require an active ImsService connection, so do not verify.
Brad Ebinger919631e2021-06-02 17:46:35 -07004588 return ImsManager.getInstance(mApp, slotId).isTtyOnVoLteCapable();
Brad Ebinger1ce9c432019-07-16 13:19:44 -07004589 } catch (ImsException e) {
4590 throw new ServiceSpecificException(e.getCode());
Brad Ebinger35c841c2018-10-01 10:40:55 -07004591 } finally {
4592 Binder.restoreCallingIdentity(identity);
4593 }
4594 }
4595
Brad Ebingerdf5b4f02018-10-31 11:24:17 -07004596 @Override
4597 public void registerImsProvisioningChangedCallback(int subId, IImsConfigCallback callback) {
4598 enforceReadPrivilegedPermission("registerImsProvisioningChangedCallback");
4599 final long identity = Binder.clearCallingIdentity();
4600 try {
Brad Ebingerd0331732020-01-16 11:21:18 -08004601 if (!isImsAvailableOnDevice()) {
4602 throw new ServiceSpecificException(ImsException.CODE_ERROR_UNSUPPORTED_OPERATION,
4603 "IMS not available on device.");
Peter Wang44b186e2020-01-13 23:33:09 -08004604 }
Brad Ebinger919631e2021-06-02 17:46:35 -07004605 int slotId = getSlotIndexOrException(subId);
4606 verifyImsMmTelConfiguredOrThrow(slotId);
4607 ImsManager.getInstance(mApp, slotId)
Brad Ebinger4ae57f92019-01-09 16:51:30 -08004608 .addProvisioningCallbackForSubscription(callback, subId);
Brad Ebinger1ce9c432019-07-16 13:19:44 -07004609 } catch (ImsException e) {
4610 throw new ServiceSpecificException(e.getCode());
Brad Ebingerdf5b4f02018-10-31 11:24:17 -07004611 } finally {
4612 Binder.restoreCallingIdentity(identity);
4613 }
4614 }
4615
4616 @Override
4617 public void unregisterImsProvisioningChangedCallback(int subId, IImsConfigCallback callback) {
4618 enforceReadPrivilegedPermission("unregisterImsProvisioningChangedCallback");
4619 final long identity = Binder.clearCallingIdentity();
Brad Ebinger4ae57f92019-01-09 16:51:30 -08004620 if (!SubscriptionManager.isValidSubscriptionId(subId)) {
4621 throw new IllegalArgumentException("Invalid Subscription ID: " + subId);
4622 }
Brad Ebingerdf5b4f02018-10-31 11:24:17 -07004623 try {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08004624 ImsManager.getInstance(mApp, getSlotIndexOrException(subId))
Brad Ebinger4ae57f92019-01-09 16:51:30 -08004625 .removeProvisioningCallbackForSubscription(callback, subId);
Brad Ebinger1ce9c432019-07-16 13:19:44 -07004626 } catch (ImsException e) {
Brad Ebinger4ae57f92019-01-09 16:51:30 -08004627 Log.i(LOG_TAG, "unregisterImsProvisioningChangedCallback: " + subId
4628 + "is inactive, ignoring unregister.");
4629 // If the subscription is no longer active, just return, since the callback will already
4630 // have been removed internally.
Brad Ebingerdf5b4f02018-10-31 11:24:17 -07004631 } finally {
4632 Binder.restoreCallingIdentity(identity);
4633 }
4634 }
4635
allenwtsu99c623b2020-01-03 18:24:23 +08004636
4637 private void checkModifyPhoneStatePermission(int subId, String message) {
4638 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(mApp, subId,
4639 message);
4640 }
4641
4642 private boolean isImsProvisioningRequired(int subId, int capability,
4643 boolean isMmtelCapability) {
4644 Phone phone = getPhone(subId);
4645 if (phone == null) {
4646 loge("phone instance null for subid " + subId);
4647 return false;
4648 }
4649 if (isMmtelCapability) {
4650 if (!doesImsCapabilityRequireProvisioning(phone.getContext(), subId, capability)) {
4651 return false;
4652 }
4653 } else {
4654 if (!doesRcsCapabilityRequireProvisioning(phone.getContext(), subId, capability)) {
4655 return false;
4656 }
4657 }
4658 return true;
4659 }
4660
4661 @Override
4662 public void setRcsProvisioningStatusForCapability(int subId, int capability,
4663 boolean isProvisioned) {
4664 checkModifyPhoneStatePermission(subId, "setRcsProvisioningStatusForCapability");
4665
4666 final long identity = Binder.clearCallingIdentity();
4667 try {
4668 // TODO: Refactor to remove ImsManager dependence and query through ImsPhone directly.
4669 if (!isImsProvisioningRequired(subId, capability, false)) {
4670 return;
4671 }
4672
4673 // this capability requires provisioning, route to the correct API.
4674 ImsManager ims = ImsManager.getInstance(mApp, getSlotIndex(subId));
4675 switch (capability) {
4676 case RcsFeature.RcsImsCapabilities.CAPABILITY_TYPE_OPTIONS_UCE:
4677 case RcsFeature.RcsImsCapabilities.CAPABILITY_TYPE_PRESENCE_UCE:
4678 ims.setEabProvisioned(isProvisioned);
4679 break;
4680 default: {
4681 throw new IllegalArgumentException("Tried to set provisioning for "
4682 + "rcs capability '" + capability + "', which does not require "
4683 + "provisioning.");
4684 }
4685 }
4686 } finally {
4687 Binder.restoreCallingIdentity(identity);
4688 }
4689
4690 }
4691
4692
4693 @Override
4694 public boolean getRcsProvisioningStatusForCapability(int subId, int capability) {
4695 enforceReadPrivilegedPermission("getRcsProvisioningStatusForCapability");
4696 final long identity = Binder.clearCallingIdentity();
4697 try {
4698 // TODO: Refactor to remove ImsManager dependence and query through ImsPhone directly.
4699 if (!isImsProvisioningRequired(subId, capability, false)) {
4700 return true;
4701 }
4702
4703 ImsManager ims = ImsManager.getInstance(mApp, getSlotIndex(subId));
4704 switch (capability) {
4705 case RcsFeature.RcsImsCapabilities.CAPABILITY_TYPE_OPTIONS_UCE:
4706 case RcsFeature.RcsImsCapabilities.CAPABILITY_TYPE_PRESENCE_UCE:
4707 return ims.isEabProvisionedOnDevice();
4708
4709 default: {
4710 throw new IllegalArgumentException("Tried to get rcs provisioning for "
4711 + "capability '" + capability + "', which does not require "
4712 + "provisioning.");
4713 }
4714 }
4715
4716 } finally {
4717 Binder.restoreCallingIdentity(identity);
4718 }
4719 }
4720
Brad Ebingerdf5b4f02018-10-31 11:24:17 -07004721 @Override
Brad Ebinger1c8542e2019-01-14 13:43:14 -08004722 public void setImsProvisioningStatusForCapability(int subId, int capability, int tech,
4723 boolean isProvisioned) {
4724 if (tech != ImsRegistrationImplBase.REGISTRATION_TECH_IWLAN
Brad Ebinger0d79c572021-04-17 15:20:49 -07004725 && tech != ImsRegistrationImplBase.REGISTRATION_TECH_LTE
4726 && tech != ImsRegistrationImplBase.REGISTRATION_TECH_NR
4727 && tech != ImsRegistrationImplBase.REGISTRATION_TECH_CROSS_SIM) {
Brad Ebinger1c8542e2019-01-14 13:43:14 -08004728 throw new IllegalArgumentException("Registration technology '" + tech + "' is invalid");
4729 }
allenwtsu99c623b2020-01-03 18:24:23 +08004730 checkModifyPhoneStatePermission(subId, "setImsProvisioningStatusForCapability");
Brad Ebinger1c8542e2019-01-14 13:43:14 -08004731 final long identity = Binder.clearCallingIdentity();
4732 try {
4733 // TODO: Refactor to remove ImsManager dependence and query through ImsPhone directly.
allenwtsu99c623b2020-01-03 18:24:23 +08004734 if (!isImsProvisioningRequired(subId, capability, true)) {
Brad Ebinger1c8542e2019-01-14 13:43:14 -08004735 return;
4736 }
Brad Ebinger0d79c572021-04-17 15:20:49 -07004737 if (tech == ImsRegistrationImplBase.REGISTRATION_TECH_NR
4738 || tech == ImsRegistrationImplBase.REGISTRATION_TECH_CROSS_SIM) {
4739 loge("setImsProvisioningStatusForCapability: called for technology that does "
4740 + "not support provisioning - " + tech);
4741 return;
4742 }
Brad Ebinger1c8542e2019-01-14 13:43:14 -08004743
4744 // this capability requires provisioning, route to the correct API.
4745 ImsManager ims = ImsManager.getInstance(mApp, getSlotIndex(subId));
4746 switch (capability) {
4747 case MmTelFeature.MmTelCapabilities.CAPABILITY_TYPE_VOICE: {
4748 if (tech == ImsRegistrationImplBase.REGISTRATION_TECH_LTE) {
4749 ims.setVolteProvisioned(isProvisioned);
4750 } else if (tech == ImsRegistrationImplBase.REGISTRATION_TECH_IWLAN) {
4751 ims.setWfcProvisioned(isProvisioned);
4752 }
4753 break;
4754 }
4755 case MmTelFeature.MmTelCapabilities.CAPABILITY_TYPE_VIDEO: {
4756 // There is currently no difference in VT provisioning type.
4757 ims.setVtProvisioned(isProvisioned);
4758 break;
4759 }
4760 case MmTelFeature.MmTelCapabilities.CAPABILITY_TYPE_UT: {
4761 // There is no "deprecated" UT provisioning mechanism through ImsConfig, so
4762 // change the capability of the feature instead if needed.
4763 if (isMmTelCapabilityProvisionedInCache(subId, capability, tech)
4764 == isProvisioned) {
4765 // No change in provisioning.
4766 return;
4767 }
4768 cacheMmTelCapabilityProvisioning(subId, capability, tech, isProvisioned);
4769 try {
Brad Ebinger0d79c572021-04-17 15:20:49 -07004770 ims.changeMmTelCapability(isProvisioned, capability, tech);
Brad Ebinger1ce9c432019-07-16 13:19:44 -07004771 } catch (com.android.ims.ImsException e) {
Brad Ebinger1c8542e2019-01-14 13:43:14 -08004772 loge("setImsProvisioningStatusForCapability: couldn't change UT capability"
4773 + ", Exception" + e.getMessage());
4774 }
4775 break;
4776 }
4777 default: {
allenwtsu99c623b2020-01-03 18:24:23 +08004778 throw new IllegalArgumentException("Tried to set provisioning for "
4779 + "MmTel capability '" + capability + "', which does not require "
4780 + "provisioning. ");
Brad Ebinger1c8542e2019-01-14 13:43:14 -08004781 }
4782 }
4783
4784 } finally {
4785 Binder.restoreCallingIdentity(identity);
4786 }
4787 }
4788
4789 @Override
4790 public boolean getImsProvisioningStatusForCapability(int subId, int capability, int tech) {
4791 if (tech != ImsRegistrationImplBase.REGISTRATION_TECH_IWLAN
Brad Ebinger0d79c572021-04-17 15:20:49 -07004792 && tech != ImsRegistrationImplBase.REGISTRATION_TECH_LTE
4793 && tech != ImsRegistrationImplBase.REGISTRATION_TECH_NR
4794 && tech != ImsRegistrationImplBase.REGISTRATION_TECH_CROSS_SIM) {
Brad Ebinger1c8542e2019-01-14 13:43:14 -08004795 throw new IllegalArgumentException("Registration technology '" + tech + "' is invalid");
4796 }
4797 enforceReadPrivilegedPermission("getProvisioningStatusForCapability");
4798 final long identity = Binder.clearCallingIdentity();
4799 try {
4800 // TODO: Refactor to remove ImsManager dependence and query through ImsPhone directly.
allenwtsu99c623b2020-01-03 18:24:23 +08004801 if (!isImsProvisioningRequired(subId, capability, true)) {
Brad Ebinger1c8542e2019-01-14 13:43:14 -08004802 return true;
4803 }
4804
Brad Ebinger0d79c572021-04-17 15:20:49 -07004805 if (tech == ImsRegistrationImplBase.REGISTRATION_TECH_NR
4806 || tech == ImsRegistrationImplBase.REGISTRATION_TECH_CROSS_SIM) {
4807 loge("getImsProvisioningStatusForCapability: called for technology that does "
4808 + "not support provisioning - " + tech);
4809 return true;
4810 }
4811
Brad Ebinger1c8542e2019-01-14 13:43:14 -08004812 ImsManager ims = ImsManager.getInstance(mApp, getSlotIndex(subId));
4813 switch (capability) {
4814 case MmTelFeature.MmTelCapabilities.CAPABILITY_TYPE_VOICE: {
4815 if (tech == ImsRegistrationImplBase.REGISTRATION_TECH_LTE) {
4816 return ims.isVolteProvisionedOnDevice();
4817 } else if (tech == ImsRegistrationImplBase.REGISTRATION_TECH_IWLAN) {
4818 return ims.isWfcProvisionedOnDevice();
4819 }
4820 // This should never happen, since we are checking tech above to make sure it
4821 // is either LTE or IWLAN.
4822 throw new IllegalArgumentException("Invalid radio technology for voice "
4823 + "capability.");
4824 }
4825 case MmTelFeature.MmTelCapabilities.CAPABILITY_TYPE_VIDEO: {
4826 // There is currently no difference in VT provisioning type.
4827 return ims.isVtProvisionedOnDevice();
4828 }
4829 case MmTelFeature.MmTelCapabilities.CAPABILITY_TYPE_UT: {
4830 // There is no "deprecated" UT provisioning mechanism, so get from shared prefs.
4831 return isMmTelCapabilityProvisionedInCache(subId, capability, tech);
4832 }
4833 default: {
allenwtsu99c623b2020-01-03 18:24:23 +08004834 throw new IllegalArgumentException(
4835 "Tried to get provisioning for MmTel capability '" + capability
4836 + "', which does not require provisioning.");
Brad Ebinger1c8542e2019-01-14 13:43:14 -08004837 }
4838 }
4839
4840 } finally {
4841 Binder.restoreCallingIdentity(identity);
4842 }
4843 }
4844
4845 @Override
4846 public boolean isMmTelCapabilityProvisionedInCache(int subId, int capability, int tech) {
4847 if (tech != ImsRegistrationImplBase.REGISTRATION_TECH_IWLAN
4848 && tech != ImsRegistrationImplBase.REGISTRATION_TECH_LTE) {
4849 throw new IllegalArgumentException("Registration technology '" + tech + "' is invalid");
4850 }
4851 enforceReadPrivilegedPermission("isMmTelCapabilityProvisionedInCache");
4852 int provisionedBits = getMmTelCapabilityProvisioningBitfield(subId, tech);
4853 return (provisionedBits & capability) > 0;
4854 }
4855
4856 @Override
4857 public void cacheMmTelCapabilityProvisioning(int subId, int capability, int tech,
4858 boolean isProvisioned) {
4859 if (tech != ImsRegistrationImplBase.REGISTRATION_TECH_IWLAN
4860 && tech != ImsRegistrationImplBase.REGISTRATION_TECH_LTE) {
4861 throw new IllegalArgumentException("Registration technology '" + tech + "' is invalid");
4862 }
4863 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(mApp, subId,
4864 "setProvisioningStatusForCapability");
4865 int provisionedBits = getMmTelCapabilityProvisioningBitfield(subId, tech);
4866 // If the current provisioning status for capability already matches isProvisioned,
4867 // do nothing.
4868 if (((provisionedBits & capability) > 0) == isProvisioned) {
4869 return;
4870 }
4871 if (isProvisioned) {
4872 setMmTelCapabilityProvisioningBitfield(subId, tech, (provisionedBits | capability));
4873 } else {
4874 setMmTelCapabilityProvisioningBitfield(subId, tech, (provisionedBits & ~capability));
4875 }
4876 }
4877
4878 /**
4879 * @return the bitfield containing the MmTel provisioning for the provided subscription and
4880 * technology. The bitfield should mirror the bitfield defined by
4881 * {@link MmTelFeature.MmTelCapabilities.MmTelCapability}.
4882 */
4883 private int getMmTelCapabilityProvisioningBitfield(int subId, int tech) {
4884 String key = getMmTelProvisioningKey(subId, tech);
4885 // Default is no capabilities are provisioned.
4886 return mTelephonySharedPreferences.getInt(key, 0 /*default*/);
4887 }
4888
4889 /**
4890 * Sets the MmTel capability provisioning bitfield (defined by
4891 * {@link MmTelFeature.MmTelCapabilities.MmTelCapability}) for the subscription and
4892 * technology specified.
4893 *
4894 * Note: This is a synchronous command and should not be called on UI thread.
4895 */
4896 private void setMmTelCapabilityProvisioningBitfield(int subId, int tech, int newField) {
4897 final SharedPreferences.Editor editor = mTelephonySharedPreferences.edit();
4898 String key = getMmTelProvisioningKey(subId, tech);
4899 editor.putInt(key, newField);
4900 editor.commit();
4901 }
4902
4903 private static String getMmTelProvisioningKey(int subId, int tech) {
4904 // resulting key is provision_ims_mmtel_{subId}_{tech}
4905 return PREF_PROVISION_IMS_MMTEL_PREFIX + subId + "_" + tech;
4906 }
4907
4908 /**
4909 * Query CarrierConfig to see if the specified capability requires provisioning for the
4910 * carrier associated with the subscription id.
4911 */
4912 private boolean doesImsCapabilityRequireProvisioning(Context context, int subId,
4913 int capability) {
4914 CarrierConfigManager configManager = new CarrierConfigManager(context);
4915 PersistableBundle c = configManager.getConfigForSubId(subId);
4916 boolean requireUtProvisioning = c.getBoolean(
Brad Ebinger076903f2019-05-13 10:00:22 -07004917 CarrierConfigManager.KEY_CARRIER_SUPPORTS_SS_OVER_UT_BOOL, false)
Brad Ebinger1c8542e2019-01-14 13:43:14 -08004918 && c.getBoolean(CarrierConfigManager.KEY_CARRIER_UT_PROVISIONING_REQUIRED_BOOL,
4919 false);
4920 boolean requireVoiceVtProvisioning = c.getBoolean(
4921 CarrierConfigManager.KEY_CARRIER_VOLTE_PROVISIONING_REQUIRED_BOOL, false);
4922
4923 // First check to make sure that the capability requires provisioning.
4924 switch (capability) {
4925 case MmTelFeature.MmTelCapabilities.CAPABILITY_TYPE_VOICE:
4926 // intentional fallthrough
4927 case MmTelFeature.MmTelCapabilities.CAPABILITY_TYPE_VIDEO: {
4928 if (requireVoiceVtProvisioning) {
4929 // Voice and Video requires provisioning
4930 return true;
4931 }
4932 break;
4933 }
4934 case MmTelFeature.MmTelCapabilities.CAPABILITY_TYPE_UT: {
4935 if (requireUtProvisioning) {
4936 // UT requires provisioning
4937 return true;
4938 }
4939 break;
4940 }
4941 }
4942 return false;
4943 }
4944
allenwtsu99c623b2020-01-03 18:24:23 +08004945 private boolean doesRcsCapabilityRequireProvisioning(Context context, int subId,
4946 int capability) {
4947 CarrierConfigManager configManager = new CarrierConfigManager(context);
4948 PersistableBundle c = configManager.getConfigForSubId(subId);
4949
4950 boolean requireRcsProvisioning = c.getBoolean(
4951 CarrierConfigManager.KEY_CARRIER_RCS_PROVISIONING_REQUIRED_BOOL, false);
4952
4953 // First check to make sure that the capability requires provisioning.
4954 switch (capability) {
4955 case RcsFeature.RcsImsCapabilities.CAPABILITY_TYPE_PRESENCE_UCE:
4956 // intentional fallthrough
4957 case RcsFeature.RcsImsCapabilities.CAPABILITY_TYPE_OPTIONS_UCE: {
4958 if (requireRcsProvisioning) {
4959 // OPTION or PRESENCE requires provisioning
4960 return true;
4961 }
4962 break;
4963 }
4964 }
4965 return false;
4966 }
4967
Brad Ebinger1c8542e2019-01-14 13:43:14 -08004968 @Override
Brad Ebingerdf5b4f02018-10-31 11:24:17 -07004969 public int getImsProvisioningInt(int subId, int key) {
Brad Ebinger1c8542e2019-01-14 13:43:14 -08004970 if (!SubscriptionManager.isValidSubscriptionId(subId)) {
4971 throw new IllegalArgumentException("Invalid Subscription id '" + subId + "'");
4972 }
Brad Ebingerdf5b4f02018-10-31 11:24:17 -07004973 enforceReadPrivilegedPermission("getImsProvisioningInt");
4974 final long identity = Binder.clearCallingIdentity();
4975 try {
4976 // TODO: Refactor to remove ImsManager dependence and query through ImsPhone directly.
Brad Ebinger1c8542e2019-01-14 13:43:14 -08004977 int slotId = getSlotIndex(subId);
4978 if (slotId <= SubscriptionManager.INVALID_SIM_SLOT_INDEX) {
4979 Log.w(LOG_TAG, "getImsProvisioningInt: called with an inactive subscription '"
4980 + subId + "' for key:" + key);
4981 return ImsConfigImplBase.CONFIG_RESULT_UNKNOWN;
4982 }
calvinpanb5a34062021-02-08 19:59:36 +08004983 return ImsManager.getInstance(mApp, slotId).getConfigInt(key);
Brad Ebinger1ce9c432019-07-16 13:19:44 -07004984 } catch (com.android.ims.ImsException e) {
Brad Ebinger1c8542e2019-01-14 13:43:14 -08004985 Log.w(LOG_TAG, "getImsProvisioningInt: ImsService is not available for subscription '"
4986 + subId + "' for key:" + key);
4987 return ImsConfigImplBase.CONFIG_RESULT_UNKNOWN;
Brad Ebingerdf5b4f02018-10-31 11:24:17 -07004988 } finally {
4989 Binder.restoreCallingIdentity(identity);
4990 }
4991 }
4992
4993 @Override
4994 public String getImsProvisioningString(int subId, int key) {
Brad Ebinger1c8542e2019-01-14 13:43:14 -08004995 if (!SubscriptionManager.isValidSubscriptionId(subId)) {
4996 throw new IllegalArgumentException("Invalid Subscription id '" + subId + "'");
4997 }
Brad Ebingerdf5b4f02018-10-31 11:24:17 -07004998 enforceReadPrivilegedPermission("getImsProvisioningString");
4999 final long identity = Binder.clearCallingIdentity();
5000 try {
5001 // TODO: Refactor to remove ImsManager dependence and query through ImsPhone directly.
Brad Ebinger1c8542e2019-01-14 13:43:14 -08005002 int slotId = getSlotIndex(subId);
5003 if (slotId <= SubscriptionManager.INVALID_SIM_SLOT_INDEX) {
5004 Log.w(LOG_TAG, "getImsProvisioningString: called for an inactive subscription id '"
5005 + subId + "' for key:" + key);
5006 return ProvisioningManager.STRING_QUERY_RESULT_ERROR_GENERIC;
5007 }
calvinpanb5a34062021-02-08 19:59:36 +08005008 return ImsManager.getInstance(mApp, slotId).getConfigString(key);
Brad Ebinger1ce9c432019-07-16 13:19:44 -07005009 } catch (com.android.ims.ImsException e) {
Brad Ebinger1c8542e2019-01-14 13:43:14 -08005010 Log.w(LOG_TAG, "getImsProvisioningString: ImsService is not available for sub '"
5011 + subId + "' for key:" + key);
5012 return ProvisioningManager.STRING_QUERY_RESULT_ERROR_NOT_READY;
Brad Ebingerdf5b4f02018-10-31 11:24:17 -07005013 } finally {
5014 Binder.restoreCallingIdentity(identity);
5015 }
5016 }
5017
5018 @Override
5019 public int setImsProvisioningInt(int subId, int key, int value) {
Brad Ebinger1c8542e2019-01-14 13:43:14 -08005020 if (!SubscriptionManager.isValidSubscriptionId(subId)) {
5021 throw new IllegalArgumentException("Invalid Subscription id '" + subId + "'");
5022 }
Brad Ebinger3d0b34e2018-11-15 14:13:12 -08005023 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(mApp, subId,
5024 "setImsProvisioningInt");
Brad Ebingerdf5b4f02018-10-31 11:24:17 -07005025 final long identity = Binder.clearCallingIdentity();
5026 try {
5027 // TODO: Refactor to remove ImsManager dependence and query through ImsPhone directly.
Brad Ebinger1c8542e2019-01-14 13:43:14 -08005028 int slotId = getSlotIndex(subId);
5029 if (slotId <= SubscriptionManager.INVALID_SIM_SLOT_INDEX) {
5030 Log.w(LOG_TAG, "setImsProvisioningInt: called with an inactive subscription id '"
5031 + subId + "' for key:" + key);
5032 return ImsConfigImplBase.CONFIG_RESULT_FAILED;
5033 }
calvinpanb5a34062021-02-08 19:59:36 +08005034 return ImsManager.getInstance(mApp, slotId).setConfig(key, value);
5035 } catch (com.android.ims.ImsException | RemoteException e) {
Brad Ebinger1c8542e2019-01-14 13:43:14 -08005036 Log.w(LOG_TAG, "setImsProvisioningInt: ImsService unavailable for sub '" + subId
calvinpanb5a34062021-02-08 19:59:36 +08005037 + "' for key:" + key, e);
Brad Ebinger1c8542e2019-01-14 13:43:14 -08005038 return ImsConfigImplBase.CONFIG_RESULT_FAILED;
Brad Ebingerdf5b4f02018-10-31 11:24:17 -07005039 } finally {
5040 Binder.restoreCallingIdentity(identity);
5041 }
5042 }
5043
5044 @Override
5045 public int setImsProvisioningString(int subId, int key, String value) {
Brad Ebinger1c8542e2019-01-14 13:43:14 -08005046 if (!SubscriptionManager.isValidSubscriptionId(subId)) {
5047 throw new IllegalArgumentException("Invalid Subscription id '" + subId + "'");
5048 }
Brad Ebinger3d0b34e2018-11-15 14:13:12 -08005049 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(mApp, subId,
5050 "setImsProvisioningString");
Brad Ebingerdf5b4f02018-10-31 11:24:17 -07005051 final long identity = Binder.clearCallingIdentity();
5052 try {
5053 // TODO: Refactor to remove ImsManager dependence and query through ImsPhone directly.
Brad Ebinger1c8542e2019-01-14 13:43:14 -08005054 int slotId = getSlotIndex(subId);
5055 if (slotId <= SubscriptionManager.INVALID_SIM_SLOT_INDEX) {
5056 Log.w(LOG_TAG, "setImsProvisioningString: called with an inactive subscription id '"
5057 + subId + "' for key:" + key);
5058 return ImsConfigImplBase.CONFIG_RESULT_FAILED;
5059 }
calvinpanb5a34062021-02-08 19:59:36 +08005060 return ImsManager.getInstance(mApp, slotId).setConfig(key, value);
5061 } catch (com.android.ims.ImsException | RemoteException e) {
Brad Ebinger1c8542e2019-01-14 13:43:14 -08005062 Log.w(LOG_TAG, "setImsProvisioningString: ImsService unavailable for sub '" + subId
calvinpanb5a34062021-02-08 19:59:36 +08005063 + "' for key:" + key, e);
Brad Ebinger1c8542e2019-01-14 13:43:14 -08005064 return ImsConfigImplBase.CONFIG_RESULT_FAILED;
Brad Ebingerdf5b4f02018-10-31 11:24:17 -07005065 } finally {
5066 Binder.restoreCallingIdentity(identity);
5067 }
5068 }
5069
Brad Ebinger919631e2021-06-02 17:46:35 -07005070 /**
5071 * Throw an ImsException if the IMS resolver does not have an ImsService configured for MMTEL
5072 * for the given slot ID or no ImsResolver instance has been created.
5073 * @param slotId The slot ID that the IMS service is created for.
5074 * @throws ImsException If there is no ImsService configured for this slot.
5075 */
5076 private void verifyImsMmTelConfiguredOrThrow(int slotId) throws ImsException {
5077 if (mImsResolver == null || !mImsResolver.isImsServiceConfiguredForFeature(slotId,
5078 ImsFeature.FEATURE_MMTEL)) {
5079 throw new ImsException("This subscription does not support MMTEL over IMS",
5080 ImsException.CODE_ERROR_UNSUPPORTED_OPERATION);
5081 }
5082 }
5083
Brad Ebinger1ce9c432019-07-16 13:19:44 -07005084 private int getSlotIndexOrException(int subId) throws ImsException {
Brad Ebinger35c841c2018-10-01 10:40:55 -07005085 int slotId = SubscriptionManager.getSlotIndex(subId);
5086 if (!SubscriptionManager.isValidSlotIndex(slotId)) {
Brad Ebinger1ce9c432019-07-16 13:19:44 -07005087 throw new ImsException("Invalid Subscription Id, subId=" + subId,
5088 ImsException.CODE_ERROR_INVALID_SUBSCRIPTION);
Brad Ebinger35c841c2018-10-01 10:40:55 -07005089 }
5090 return slotId;
5091 }
5092
Brad Ebinger1c8542e2019-01-14 13:43:14 -08005093 private int getSlotIndex(int subId) {
5094 int slotId = SubscriptionManager.getSlotIndex(subId);
5095 if (!SubscriptionManager.isValidSlotIndex(slotId)) {
5096 return SubscriptionManager.INVALID_SIM_SLOT_INDEX;
5097 }
5098 return slotId;
5099 }
5100
Wink Saville36469e72014-06-11 15:17:00 -07005101 /**
Nathan Harold9042f0b2019-05-21 15:51:27 -07005102 * Returns the data network type for a subId; does not throw SecurityException.
Wink Saville36469e72014-06-11 15:17:00 -07005103 */
5104 @Override
Philip P. Moltmann700a9592019-10-03 11:53:50 -07005105 public int getNetworkTypeForSubscriber(int subId, String callingPackage,
5106 String callingFeatureId) {
Shuo Qian13d89152021-05-10 23:58:11 -07005107 try {
5108 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
5109 } catch (SecurityException se) {
5110 EventLog.writeEvent(0x534e4554, "186776740", Binder.getCallingUid());
5111 throw new SecurityException("Package " + callingPackage + " does not belong to "
5112 + Binder.getCallingUid());
5113 }
Nathan Haroldf096d982020-11-18 17:18:06 -08005114 final int targetSdk = TelephonyPermissions.getTargetSdk(mApp, callingPackage);
Nathan Haroldef60dba2019-05-22 13:55:14 -07005115 if (targetSdk > android.os.Build.VERSION_CODES.Q) {
Philip P. Moltmann700a9592019-10-03 11:53:50 -07005116 return getDataNetworkTypeForSubscriber(subId, callingPackage, callingFeatureId);
Nathan Haroldef60dba2019-05-22 13:55:14 -07005117 } else if (targetSdk == android.os.Build.VERSION_CODES.Q
Nathan Harold9042f0b2019-05-21 15:51:27 -07005118 && !TelephonyPermissions.checkCallingOrSelfReadPhoneStateNoThrow(
Philip P. Moltmann700a9592019-10-03 11:53:50 -07005119 mApp, subId, callingPackage, callingFeatureId,
5120 "getNetworkTypeForSubscriber")) {
Robert Greenwalt36b23af2015-07-06 17:59:14 -07005121 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
5122 }
Robert Greenwalta5dcfcb2015-07-10 09:06:29 -07005123
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005124 final long identity = Binder.clearCallingIdentity();
5125 try {
5126 final Phone phone = getPhone(subId);
5127 if (phone != null) {
5128 return phone.getServiceState().getDataNetworkType();
5129 } else {
5130 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
5131 }
5132 } finally {
5133 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07005134 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07005135 }
5136
5137 /**
5138 * Returns the data network type
5139 */
5140 @Override
Philip P. Moltmann700a9592019-10-03 11:53:50 -07005141 public int getDataNetworkType(String callingPackage, String callingFeatureId) {
Zoey Chenfd61f7f2021-04-21 13:42:10 +08005142 return getDataNetworkTypeForSubscriber(mSubscriptionController.getDefaultDataSubId(),
5143 callingPackage, callingFeatureId);
Wink Saville36469e72014-06-11 15:17:00 -07005144 }
5145
5146 /**
5147 * Returns the data network type for a subId
5148 */
5149 @Override
Philip P. Moltmann700a9592019-10-03 11:53:50 -07005150 public int getDataNetworkTypeForSubscriber(int subId, String callingPackage,
5151 String callingFeatureId) {
Sooraj Sasindran5d051bf2021-11-05 13:14:15 -07005152 String functionName = "getDataNetworkTypeForSubscriber";
5153 if (!TelephonyPermissions.checkCallingOrSelfReadNonDangerousPhoneStateNoThrow(
5154 mApp, functionName)) {
5155 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
5156 mApp, subId, callingPackage, callingFeatureId, functionName)) {
5157 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
5158 }
Robert Greenwalt36b23af2015-07-06 17:59:14 -07005159 }
5160
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005161 final long identity = Binder.clearCallingIdentity();
5162 try {
5163 final Phone phone = getPhone(subId);
5164 if (phone != null) {
5165 return phone.getServiceState().getDataNetworkType();
5166 } else {
5167 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
5168 }
5169 } finally {
5170 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07005171 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07005172 }
5173
5174 /**
Wink Saville36469e72014-06-11 15:17:00 -07005175 * Returns the Voice network type for a subId
5176 */
5177 @Override
Philip P. Moltmann700a9592019-10-03 11:53:50 -07005178 public int getVoiceNetworkTypeForSubscriber(int subId, String callingPackage,
5179 String callingFeatureId) {
Sooraj Sasindran5d051bf2021-11-05 13:14:15 -07005180 String functionName = "getVoiceNetworkTypeForSubscriber";
5181 if (!TelephonyPermissions.checkCallingOrSelfReadNonDangerousPhoneStateNoThrow(
5182 mApp, functionName)) {
5183 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
5184 mApp, subId, callingPackage, callingFeatureId, functionName)) {
5185 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
5186 }
Robert Greenwalta5dcfcb2015-07-10 09:06:29 -07005187 }
5188
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005189 final long identity = Binder.clearCallingIdentity();
5190 try {
5191 final Phone phone = getPhone(subId);
5192 if (phone != null) {
5193 return phone.getServiceState().getVoiceNetworkType();
5194 } else {
5195 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
5196 }
5197 } finally {
5198 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07005199 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07005200 }
5201
5202 /**
5203 * @return true if a ICC card is present
5204 */
5205 public boolean hasIccCard() {
Wink Saville36469e72014-06-11 15:17:00 -07005206 // FIXME Make changes to pass defaultSimId of type int
Sanket Padawe13bac7b2017-03-20 15:04:47 -07005207 return hasIccCardUsingSlotIndex(mSubscriptionController.getSlotIndex(
5208 getDefaultSubscription()));
Wink Saville36469e72014-06-11 15:17:00 -07005209 }
5210
5211 /**
Sanket Padawe13bac7b2017-03-20 15:04:47 -07005212 * @return true if a ICC card is present for a slotIndex
Wink Saville36469e72014-06-11 15:17:00 -07005213 */
Sanket Padawe356d7632015-06-22 14:03:32 -07005214 @Override
Sanket Padawe13bac7b2017-03-20 15:04:47 -07005215 public boolean hasIccCardUsingSlotIndex(int slotIndex) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005216 final long identity = Binder.clearCallingIdentity();
5217 try {
5218 final Phone phone = PhoneFactory.getPhone(slotIndex);
5219 if (phone != null) {
5220 return phone.getIccCard().hasIccCard();
5221 } else {
5222 return false;
5223 }
5224 } finally {
5225 Binder.restoreCallingIdentity(identity);
Amit Mahajana6fc2a82015-01-06 11:53:51 -08005226 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07005227 }
5228
5229 /**
5230 * Return if the current radio is LTE on CDMA. This
5231 * is a tri-state return value as for a period of time
5232 * the mode may be unknown.
5233 *
Robert Greenwalt36b23af2015-07-06 17:59:14 -07005234 * @param callingPackage the name of the package making the call.
Santos Cordon7d4ddf62013-07-10 11:58:08 -07005235 * @return {@link Phone#LTE_ON_CDMA_UNKNOWN}, {@link Phone#LTE_ON_CDMA_FALSE}
Jake Hambye994d462014-02-03 13:10:13 -08005236 * or {@link Phone#LTE_ON_CDMA_TRUE}
Santos Cordon7d4ddf62013-07-10 11:58:08 -07005237 */
Robert Greenwalt36b23af2015-07-06 17:59:14 -07005238 @Override
Philip P. Moltmann700a9592019-10-03 11:53:50 -07005239 public int getLteOnCdmaMode(String callingPackage, String callingFeatureId) {
5240 return getLteOnCdmaModeForSubscriber(getDefaultSubscription(), callingPackage,
5241 callingFeatureId);
Wink Saville36469e72014-06-11 15:17:00 -07005242 }
5243
Sanket Padawe356d7632015-06-22 14:03:32 -07005244 @Override
Philip P. Moltmann700a9592019-10-03 11:53:50 -07005245 public int getLteOnCdmaModeForSubscriber(int subId, String callingPackage,
5246 String callingFeatureId) {
Sarah Chin790d2922020-01-16 12:17:23 -08005247 try {
5248 enforceReadPrivilegedPermission("getLteOnCdmaModeForSubscriber");
5249 } catch (SecurityException e) {
Robert Greenwalt36b23af2015-07-06 17:59:14 -07005250 return PhoneConstants.LTE_ON_CDMA_UNKNOWN;
5251 }
5252
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005253 final long identity = Binder.clearCallingIdentity();
5254 try {
5255 final Phone phone = getPhone(subId);
5256 if (phone == null) {
5257 return PhoneConstants.LTE_ON_CDMA_UNKNOWN;
5258 } else {
Nathan Harold05ad6332020-07-10 11:54:36 -07005259 return TelephonyProperties.lte_on_cdma_device()
5260 .orElse(PhoneConstants.LTE_ON_CDMA_FALSE);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005261 }
5262 } finally {
5263 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07005264 }
Wink Saville36469e72014-06-11 15:17:00 -07005265 }
5266
Wink Saville36469e72014-06-11 15:17:00 -07005267 /**
5268 * {@hide}
5269 * Returns Default subId, 0 in the case of single standby.
5270 */
Wink Savilleb564aae2014-10-23 10:18:09 -07005271 private int getDefaultSubscription() {
Wink Savilleac1bdfd2014-11-20 23:04:44 -08005272 return mSubscriptionController.getDefaultSubId();
Wink Saville36469e72014-06-11 15:17:00 -07005273 }
5274
Shishir Agrawala9f32182016-04-12 12:00:16 -07005275 private int getSlotForDefaultSubscription() {
5276 return mSubscriptionController.getPhoneId(getDefaultSubscription());
5277 }
5278
Wink Savilleb564aae2014-10-23 10:18:09 -07005279 private int getPreferredVoiceSubscription() {
Wink Savilleac1bdfd2014-11-20 23:04:44 -08005280 return mSubscriptionController.getDefaultVoiceSubId();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07005281 }
Ihab Awadf2177b72013-11-25 13:33:23 -08005282
Pengquan Menge92a50d2018-09-21 15:54:48 -07005283 private boolean isActiveSubscription(int subId) {
5284 return mSubscriptionController.isActiveSubId(subId);
5285 }
5286
Ihab Awadf2177b72013-11-25 13:33:23 -08005287 /**
5288 * @see android.telephony.TelephonyManager.WifiCallingChoices
5289 */
5290 public int getWhenToMakeWifiCalls() {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005291 final long identity = Binder.clearCallingIdentity();
5292 try {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08005293 return Settings.System.getInt(mApp.getContentResolver(),
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005294 Settings.System.WHEN_TO_MAKE_WIFI_CALLS,
5295 getWhenToMakeWifiCallsDefaultPreference());
5296 } finally {
5297 Binder.restoreCallingIdentity(identity);
5298 }
Ihab Awadf2177b72013-11-25 13:33:23 -08005299 }
5300
5301 /**
5302 * @see android.telephony.TelephonyManager.WifiCallingChoices
5303 */
5304 public void setWhenToMakeWifiCalls(int preference) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005305 final long identity = Binder.clearCallingIdentity();
5306 try {
5307 if (DBG) log("setWhenToMakeWifiCallsStr, storing setting = " + preference);
Nazanin Bakhshif782e562018-12-11 15:15:39 -08005308 Settings.System.putInt(mApp.getContentResolver(),
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005309 Settings.System.WHEN_TO_MAKE_WIFI_CALLS, preference);
5310 } finally {
5311 Binder.restoreCallingIdentity(identity);
5312 }
Ihab Awadf9e92732013-12-05 18:02:52 -08005313 }
5314
Sailesh Nepald1e68152013-12-12 19:08:02 -08005315 private static int getWhenToMakeWifiCallsDefaultPreference() {
Santos Cordonda120f42014-08-06 04:44:34 -07005316 // TODO: Use a build property to choose this value.
Evan Charlton9829e882013-12-19 15:30:38 -08005317 return TelephonyManager.WifiCallingChoices.ALWAYS_USE;
Ihab Awadf2177b72013-11-25 13:33:23 -08005318 }
Shishir Agrawal69f68122013-12-16 17:25:49 -08005319
Muralidhar Reddybd38d952021-12-02 21:04:16 +00005320 private Phone getPhoneFromSlotPortIndexOrThrowException(int slotIndex, int portIndex) {
5321 int phoneId = UiccController.getInstance().getPhoneIdFromSlotPortIndex(slotIndex,
5322 portIndex);
Jordan Liu4c733742019-02-28 12:03:40 -08005323 if (phoneId == -1) {
Muralidhar Reddybd38d952021-12-02 21:04:16 +00005324 throw new IllegalArgumentException("Given slot index: " + slotIndex + " port index: "
5325 + portIndex + " does not correspond to an active phone");
Jordan Liu4c733742019-02-28 12:03:40 -08005326 }
5327 return PhoneFactory.getPhone(phoneId);
5328 }
5329
Shishir Agrawal566b7612013-10-28 14:41:00 -07005330 @Override
Derek Tan740e1672017-06-27 14:56:27 -07005331 public IccOpenLogicalChannelResponse iccOpenLogicalChannel(
Rambo Wanga1782702021-11-10 20:15:19 -08005332 @NonNull IccLogicalChannelRequest request) {
5333 Phone phone = getPhoneFromValidIccLogicalChannelRequest(request,
5334 /*message=*/ "iccOpenLogicalChannel");
5335
5336 if (DBG) log("iccOpenLogicalChannel: request=" + request);
5337 // Verify that the callingPackage in the request belongs to the calling UID
5338 mAppOps.checkPackage(Binder.getCallingUid(), request.callingPackage);
5339
5340 return iccOpenLogicalChannelWithPermission(phone, request);
Jordan Liu4c733742019-02-28 12:03:40 -08005341 }
Shishir Agrawal566b7612013-10-28 14:41:00 -07005342
Rambo Wanga1782702021-11-10 20:15:19 -08005343 private Phone getPhoneFromValidIccLogicalChannelRequest(
5344 @NonNull IccLogicalChannelRequest request, String message) {
5345 Phone phone;
5346 if (request.subId != SubscriptionManager.INVALID_SUBSCRIPTION_ID) {
5347 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
5348 mApp, request.subId, message);
5349 phone = getPhoneFromSubId(request.subId);
5350 } else if (request.slotIndex != SubscriptionManager.INVALID_SIM_SLOT_INDEX) {
5351 enforceModifyPermission();
5352 phone = getPhoneFromSlotPortIndexOrThrowException(request.slotIndex, request.portIndex);
5353 } else {
5354 throw new IllegalArgumentException("Both subId and slotIndex in request are invalid.");
Jordan Liu4c733742019-02-28 12:03:40 -08005355 }
Rambo Wanga1782702021-11-10 20:15:19 -08005356 return phone;
Jordan Liu4c733742019-02-28 12:03:40 -08005357 }
5358
5359 private IccOpenLogicalChannelResponse iccOpenLogicalChannelWithPermission(Phone phone,
Rambo Wanga1782702021-11-10 20:15:19 -08005360 IccLogicalChannelRequest channelRequest) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005361 final long identity = Binder.clearCallingIdentity();
5362 try {
Rambo Wanga1782702021-11-10 20:15:19 -08005363 if (TextUtils.equals(ISDR_AID, channelRequest.aid)) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005364 // Only allows LPA to open logical channel to ISD-R.
Nazanin Bakhshif782e562018-12-11 15:15:39 -08005365 ComponentInfo bestComponent = EuiccConnector.findBestComponent(getDefaultPhone()
5366 .getContext().getPackageManager());
Rambo Wanga1782702021-11-10 20:15:19 -08005367 if (bestComponent == null || !TextUtils.equals(channelRequest.callingPackage,
5368 bestComponent.packageName)) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005369 loge("The calling package is not allowed to access ISD-R.");
5370 throw new SecurityException(
5371 "The calling package is not allowed to access ISD-R.");
5372 }
Derek Tan740e1672017-06-27 14:56:27 -07005373 }
Derek Tan740e1672017-06-27 14:56:27 -07005374
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005375 IccOpenLogicalChannelResponse response = (IccOpenLogicalChannelResponse) sendRequest(
Rambo Wanga1782702021-11-10 20:15:19 -08005376 CMD_OPEN_CHANNEL, channelRequest, phone, null /* workSource */);
5377 if (DBG) log("iccOpenLogicalChannelWithPermission: response=" + response);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005378 return response;
5379 } finally {
5380 Binder.restoreCallingIdentity(identity);
5381 }
Shishir Agrawal566b7612013-10-28 14:41:00 -07005382 }
5383
5384 @Override
Rambo Wanga1782702021-11-10 20:15:19 -08005385 public boolean iccCloseLogicalChannel(@NonNull IccLogicalChannelRequest request) {
5386 Phone phone = getPhoneFromValidIccLogicalChannelRequest(request,
5387 /*message=*/"iccCloseLogicalChannel");
5388
5389 if (DBG) log("iccCloseLogicalChannel: request=" + request);
5390
5391 return iccCloseLogicalChannelWithPermission(phone, request);
Jordan Liu4c733742019-02-28 12:03:40 -08005392 }
Shishir Agrawal566b7612013-10-28 14:41:00 -07005393
Rambo Wanga1782702021-11-10 20:15:19 -08005394 private boolean iccCloseLogicalChannelWithPermission(Phone phone,
5395 IccLogicalChannelRequest request) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005396 final long identity = Binder.clearCallingIdentity();
5397 try {
Rambo Wanga1782702021-11-10 20:15:19 -08005398 if (request.channel < 0) {
Chen Xu540470b2021-12-14 17:15:47 -08005399 throw new IllegalArgumentException("request.channel is less than 0");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005400 }
Chen Xue9d737e2022-01-01 23:41:31 -08005401 Object result = sendRequest(CMD_CLOSE_CHANNEL, request.channel, phone,
Jordan Liu4c733742019-02-28 12:03:40 -08005402 null /* workSource */);
Chen Xue9d737e2022-01-01 23:41:31 -08005403 Boolean success = false;
5404 if (result instanceof RuntimeException) {
5405 // if there is an exception returned, throw from the binder thread here.
5406 throw (RuntimeException) result;
5407 } else if (result instanceof Boolean) {
5408 success = (Boolean) result;
5409 } else {
5410 loge("iccCloseLogicalChannelWithPermission: supported return type " + result);
5411 }
Rambo Wanga1782702021-11-10 20:15:19 -08005412 if (DBG) log("iccCloseLogicalChannelWithPermission: success=" + success);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005413 return success;
5414 } finally {
5415 Binder.restoreCallingIdentity(identity);
Shishir Agrawal566b7612013-10-28 14:41:00 -07005416 }
Shishir Agrawal566b7612013-10-28 14:41:00 -07005417 }
5418
5419 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08005420 public String iccTransmitApduLogicalChannel(int subId, int channel, int cla,
Shishir Agrawal566b7612013-10-28 14:41:00 -07005421 int command, int p1, int p2, int p3, String data) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08005422 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
5423 mApp, subId, "iccTransmitApduLogicalChannel");
Jordan Liu4c733742019-02-28 12:03:40 -08005424 if (DBG) {
5425 log("iccTransmitApduLogicalChannel: subId=" + subId + " chnl=" + channel
5426 + " cla=" + cla + " cmd=" + command + " p1=" + p1 + " p2=" + p2 + " p3="
5427 + p3 + " data=" + data);
5428 }
5429 return iccTransmitApduLogicalChannelWithPermission(getPhoneFromSubId(subId), channel, cla,
5430 command, p1, p2, p3, data);
5431 }
Shishir Agrawal566b7612013-10-28 14:41:00 -07005432
Jordan Liu4c733742019-02-28 12:03:40 -08005433 @Override
Muralidhar Reddybd38d952021-12-02 21:04:16 +00005434 public String iccTransmitApduLogicalChannelByPort(int slotIndex, int portIndex, int channel,
5435 int cla, int command, int p1, int p2, int p3, String data) {
Jordan Liu4c733742019-02-28 12:03:40 -08005436 enforceModifyPermission();
5437 if (DBG) {
Muralidhar Reddybd38d952021-12-02 21:04:16 +00005438 log("iccTransmitApduLogicalChannelByPort: slotIndex=" + slotIndex + " portIndex="
5439 + portIndex + " chnl=" + channel + " cla=" + cla + " cmd=" + command + " p1="
5440 + p1 + " p2=" + p2 + " p3=" + p3 + " data=" + data);
Jordan Liu4c733742019-02-28 12:03:40 -08005441 }
5442 return iccTransmitApduLogicalChannelWithPermission(
Muralidhar Reddybd38d952021-12-02 21:04:16 +00005443 getPhoneFromSlotPortIndexOrThrowException(slotIndex, portIndex), channel, cla,
5444 command, p1, p2, p3, data);
Jordan Liu4c733742019-02-28 12:03:40 -08005445 }
5446
5447 private String iccTransmitApduLogicalChannelWithPermission(Phone phone, int channel, int cla,
5448 int command, int p1, int p2, int p3, String data) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005449 final long identity = Binder.clearCallingIdentity();
5450 try {
Hall Liu4fd771b2019-05-02 09:16:29 -07005451 if (channel <= 0) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005452 return "";
5453 }
Shishir Agrawal566b7612013-10-28 14:41:00 -07005454
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005455 IccIoResult response = (IccIoResult) sendRequest(CMD_TRANSMIT_APDU_LOGICAL_CHANNEL,
Jordan Liu4c733742019-02-28 12:03:40 -08005456 new IccAPDUArgument(channel, cla, command, p1, p2, p3, data), phone,
5457 null /* workSource */);
5458 if (DBG) log("iccTransmitApduLogicalChannelWithPermission: " + response);
Shishir Agrawal566b7612013-10-28 14:41:00 -07005459
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005460 // Append the returned status code to the end of the response payload.
5461 String s = Integer.toHexString(
5462 (response.sw1 << 8) + response.sw2 + 0x10000).substring(1);
5463 if (response.payload != null) {
5464 s = IccUtils.bytesToHexString(response.payload) + s;
5465 }
5466 return s;
5467 } finally {
5468 Binder.restoreCallingIdentity(identity);
Shishir Agrawal5ec14172014-08-05 17:05:45 -07005469 }
Shishir Agrawal566b7612013-10-28 14:41:00 -07005470 }
Jake Hambye994d462014-02-03 13:10:13 -08005471
Evan Charltonc66da362014-05-16 14:06:40 -07005472 @Override
Holly Jiuyu Sun1cc2d552018-01-26 15:51:16 -08005473 public String iccTransmitApduBasicChannel(int subId, String callingPackage, int cla,
5474 int command, int p1, int p2, int p3, String data) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08005475 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
5476 mApp, subId, "iccTransmitApduBasicChannel");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005477 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
Jordan Liu4c733742019-02-28 12:03:40 -08005478 if (DBG) {
5479 log("iccTransmitApduBasicChannel: subId=" + subId + " cla=" + cla + " cmd="
5480 + command + " p1=" + p1 + " p2=" + p2 + " p3=" + p3 + " data=" + data);
5481 }
5482 return iccTransmitApduBasicChannelWithPermission(getPhoneFromSubId(subId), callingPackage,
5483 cla, command, p1, p2, p3, data);
5484 }
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07005485
Jordan Liu4c733742019-02-28 12:03:40 -08005486 @Override
Muralidhar Reddybd38d952021-12-02 21:04:16 +00005487 public String iccTransmitApduBasicChannelByPort(int slotIndex, int portIndex,
5488 String callingPackage, int cla, int command, int p1, int p2, int p3, String data) {
Jordan Liu4c733742019-02-28 12:03:40 -08005489 enforceModifyPermission();
5490 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
5491 if (DBG) {
Muralidhar Reddybd38d952021-12-02 21:04:16 +00005492 log("iccTransmitApduBasicChannelByPort: slotIndex=" + slotIndex + " portIndex="
5493 + portIndex + " cla=" + cla + " cmd=" + command + " p1=" + p1 + " p2="
5494 + p2 + " p3=" + p3 + " data=" + data);
Jordan Liu4c733742019-02-28 12:03:40 -08005495 }
5496
5497 return iccTransmitApduBasicChannelWithPermission(
Muralidhar Reddybd38d952021-12-02 21:04:16 +00005498 getPhoneFromSlotPortIndexOrThrowException(slotIndex, portIndex), callingPackage,
5499 cla, command, p1, p2, p3, data);
Jordan Liu4c733742019-02-28 12:03:40 -08005500 }
5501
5502 // open APDU basic channel assuming the caller has sufficient permissions
5503 private String iccTransmitApduBasicChannelWithPermission(Phone phone, String callingPackage,
5504 int cla, int command, int p1, int p2, int p3, String data) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005505 final long identity = Binder.clearCallingIdentity();
5506 try {
5507 if (command == SELECT_COMMAND && p1 == SELECT_P1 && p2 == SELECT_P2 && p3 == SELECT_P3
5508 && TextUtils.equals(ISDR_AID, data)) {
5509 // Only allows LPA to select ISD-R.
Nazanin Bakhshif782e562018-12-11 15:15:39 -08005510 ComponentInfo bestComponent = EuiccConnector.findBestComponent(getDefaultPhone()
5511 .getContext().getPackageManager());
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005512 if (bestComponent == null
5513 || !TextUtils.equals(callingPackage, bestComponent.packageName)) {
5514 loge("The calling package is not allowed to select ISD-R.");
5515 throw new SecurityException(
5516 "The calling package is not allowed to select ISD-R.");
5517 }
Holly Jiuyu Sun1cc2d552018-01-26 15:51:16 -08005518 }
Holly Jiuyu Sun1cc2d552018-01-26 15:51:16 -08005519
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005520 IccIoResult response = (IccIoResult) sendRequest(CMD_TRANSMIT_APDU_BASIC_CHANNEL,
Jordan Liu4c733742019-02-28 12:03:40 -08005521 new IccAPDUArgument(0, cla, command, p1, p2, p3, data), phone,
5522 null /* workSource */);
5523 if (DBG) log("iccTransmitApduBasicChannelWithPermission: " + response);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07005524
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005525 // Append the returned status code to the end of the response payload.
5526 String s = Integer.toHexString(
5527 (response.sw1 << 8) + response.sw2 + 0x10000).substring(1);
5528 if (response.payload != null) {
5529 s = IccUtils.bytesToHexString(response.payload) + s;
5530 }
5531 return s;
5532 } finally {
5533 Binder.restoreCallingIdentity(identity);
Shishir Agrawal5ec14172014-08-05 17:05:45 -07005534 }
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07005535 }
5536
5537 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08005538 public byte[] iccExchangeSimIO(int subId, int fileID, int command, int p1, int p2, int p3,
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07005539 String filePath) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08005540 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
5541 mApp, subId, "iccExchangeSimIO");
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07005542
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005543 final long identity = Binder.clearCallingIdentity();
5544 try {
5545 if (DBG) {
5546 log("Exchange SIM_IO " + subId + ":" + fileID + ":" + command + " "
5547 + p1 + " " + p2 + " " + p3 + ":" + filePath);
5548 }
5549
5550 IccIoResult response =
5551 (IccIoResult) sendRequest(CMD_EXCHANGE_SIM_IO,
5552 new IccAPDUArgument(-1, fileID, command, p1, p2, p3, filePath),
5553 subId);
5554
5555 if (DBG) {
5556 log("Exchange SIM_IO [R]" + response);
5557 }
5558
5559 byte[] result = null;
5560 int length = 2;
5561 if (response.payload != null) {
5562 length = 2 + response.payload.length;
5563 result = new byte[length];
5564 System.arraycopy(response.payload, 0, result, 0, response.payload.length);
5565 } else {
5566 result = new byte[length];
5567 }
5568
5569 result[length - 1] = (byte) response.sw2;
5570 result[length - 2] = (byte) response.sw1;
5571 return result;
5572 } finally {
5573 Binder.restoreCallingIdentity(identity);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07005574 }
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07005575 }
5576
Nathan Haroldb3014052017-01-25 15:57:32 -08005577 /**
5578 * Get the forbidden PLMN List from the given app type (ex APPTYPE_USIM)
5579 * on a particular subscription
5580 */
Philip P. Moltmann700a9592019-10-03 11:53:50 -07005581 public String[] getForbiddenPlmns(int subId, int appType, String callingPackage,
5582 String callingFeatureId) {
sqianb6e41952018-03-12 14:54:01 -07005583 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
Philip P. Moltmann700a9592019-10-03 11:53:50 -07005584 mApp, subId, callingPackage, callingFeatureId, "getForbiddenPlmns")) {
sqianb6e41952018-03-12 14:54:01 -07005585 return null;
5586 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005587
5588 final long identity = Binder.clearCallingIdentity();
5589 try {
5590 if (appType != TelephonyManager.APPTYPE_USIM
5591 && appType != TelephonyManager.APPTYPE_SIM) {
5592 loge("getForbiddenPlmnList(): App Type must be USIM or SIM");
5593 return null;
5594 }
5595 Object response = sendRequest(
5596 CMD_GET_FORBIDDEN_PLMNS, new Integer(appType), subId);
5597 if (response instanceof String[]) {
5598 return (String[]) response;
5599 }
yincheng zhao2737e882019-09-06 17:06:54 -07005600 // Response is an Exception of some kind
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005601 // which is signalled to the user as a NULL retval
Nathan Haroldb3014052017-01-25 15:57:32 -08005602 return null;
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005603 } finally {
5604 Binder.restoreCallingIdentity(identity);
Nathan Haroldb3014052017-01-25 15:57:32 -08005605 }
Nathan Haroldb3014052017-01-25 15:57:32 -08005606 }
5607
yincheng zhao2737e882019-09-06 17:06:54 -07005608 /**
5609 * Set the forbidden PLMN list from the given app type (ex APPTYPE_USIM) on a particular
5610 * subscription.
5611 *
5612 * @param subId the id of the subscription.
5613 * @param appType the uicc app type, must be USIM or SIM.
5614 * @param fplmns the Forbiden plmns list that needed to be written to the SIM.
5615 * @param callingPackage the op Package name.
Philip P. Moltmann700a9592019-10-03 11:53:50 -07005616 * @param callingFeatureId the feature in the package.
yincheng zhao2737e882019-09-06 17:06:54 -07005617 * @return number of fplmns that is successfully written to the SIM.
5618 */
Philip P. Moltmann700a9592019-10-03 11:53:50 -07005619 public int setForbiddenPlmns(int subId, int appType, List<String> fplmns, String callingPackage,
5620 String callingFeatureId) {
Jayachandran C5b0d75a2021-10-21 22:15:27 -07005621 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
5622 mApp, subId, "setForbiddenPlmns");
5623
yincheng zhao2737e882019-09-06 17:06:54 -07005624 if (appType != TelephonyManager.APPTYPE_USIM && appType != TelephonyManager.APPTYPE_SIM) {
5625 loge("setForbiddenPlmnList(): App Type must be USIM or SIM");
5626 throw new IllegalArgumentException("Invalid appType: App Type must be USIM or SIM");
5627 }
5628 if (fplmns == null) {
5629 throw new IllegalArgumentException("Fplmn List provided is null");
5630 }
5631 for (String fplmn : fplmns) {
5632 if (!CellIdentity.isValidPlmn(fplmn)) {
5633 throw new IllegalArgumentException("Invalid fplmn provided: " + fplmn);
5634 }
5635 }
5636 final long identity = Binder.clearCallingIdentity();
5637 try {
5638 Object response = sendRequest(
5639 CMD_SET_FORBIDDEN_PLMNS,
5640 new Pair<Integer, List<String>>(new Integer(appType), fplmns),
5641 subId);
5642 return (int) response;
5643 } finally {
5644 Binder.restoreCallingIdentity(identity);
5645 }
5646 }
5647
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07005648 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08005649 public String sendEnvelopeWithStatus(int subId, String content) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08005650 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
5651 mApp, subId, "sendEnvelopeWithStatus");
Evan Charltonc66da362014-05-16 14:06:40 -07005652
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005653 final long identity = Binder.clearCallingIdentity();
5654 try {
5655 IccIoResult response = (IccIoResult) sendRequest(CMD_SEND_ENVELOPE, content, subId);
5656 if (response.payload == null) {
5657 return "";
5658 }
Evan Charltonc66da362014-05-16 14:06:40 -07005659
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005660 // Append the returned status code to the end of the response payload.
5661 String s = Integer.toHexString(
5662 (response.sw1 << 8) + response.sw2 + 0x10000).substring(1);
5663 s = IccUtils.bytesToHexString(response.payload) + s;
5664 return s;
5665 } finally {
5666 Binder.restoreCallingIdentity(identity);
5667 }
Evan Charltonc66da362014-05-16 14:06:40 -07005668 }
5669
Jake Hambye994d462014-02-03 13:10:13 -08005670 /**
5671 * Read one of the NV items defined in {@link com.android.internal.telephony.RadioNVItems}
5672 * and {@code ril_nv_items.h}. Used for device configuration by some CDMA operators.
5673 *
5674 * @param itemID the ID of the item to read
5675 * @return the NV item as a String, or null on error.
5676 */
5677 @Override
5678 public String nvReadItem(int itemID) {
vagdeviaf9a5b92018-08-15 16:01:53 -07005679 WorkSource workSource = getWorkSource(Binder.getCallingUid());
Jeff Davidson7e17e312018-02-13 18:17:36 -08005680 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
5681 mApp, getDefaultSubscription(), "nvReadItem");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005682
5683 final long identity = Binder.clearCallingIdentity();
5684 try {
5685 if (DBG) log("nvReadItem: item " + itemID);
vagdeviaf9a5b92018-08-15 16:01:53 -07005686 String value = (String) sendRequest(CMD_NV_READ_ITEM, itemID, workSource);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005687 if (DBG) log("nvReadItem: item " + itemID + " is \"" + value + '"');
5688 return value;
5689 } finally {
5690 Binder.restoreCallingIdentity(identity);
5691 }
Jake Hambye994d462014-02-03 13:10:13 -08005692 }
5693
5694 /**
5695 * Write one of the NV items defined in {@link com.android.internal.telephony.RadioNVItems}
5696 * and {@code ril_nv_items.h}. Used for device configuration by some CDMA operators.
5697 *
5698 * @param itemID the ID of the item to read
5699 * @param itemValue the value to write, as a String
5700 * @return true on success; false on any failure
5701 */
5702 @Override
5703 public boolean nvWriteItem(int itemID, String itemValue) {
vagdeviaf9a5b92018-08-15 16:01:53 -07005704 WorkSource workSource = getWorkSource(Binder.getCallingUid());
Jeff Davidson7e17e312018-02-13 18:17:36 -08005705 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
5706 mApp, getDefaultSubscription(), "nvWriteItem");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005707
5708 final long identity = Binder.clearCallingIdentity();
5709 try {
5710 if (DBG) log("nvWriteItem: item " + itemID + " value \"" + itemValue + '"');
5711 Boolean success = (Boolean) sendRequest(CMD_NV_WRITE_ITEM,
vagdeviaf9a5b92018-08-15 16:01:53 -07005712 new Pair<Integer, String>(itemID, itemValue), workSource);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005713 if (DBG) log("nvWriteItem: item " + itemID + ' ' + (success ? "ok" : "fail"));
5714 return success;
5715 } finally {
5716 Binder.restoreCallingIdentity(identity);
5717 }
Jake Hambye994d462014-02-03 13:10:13 -08005718 }
5719
5720 /**
5721 * Update the CDMA Preferred Roaming List (PRL) in the radio NV storage.
5722 * Used for device configuration by some CDMA operators.
5723 *
5724 * @param preferredRoamingList byte array containing the new PRL
5725 * @return true on success; false on any failure
5726 */
5727 @Override
5728 public boolean nvWriteCdmaPrl(byte[] preferredRoamingList) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08005729 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
5730 mApp, getDefaultSubscription(), "nvWriteCdmaPrl");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005731
5732 final long identity = Binder.clearCallingIdentity();
5733 try {
5734 if (DBG) log("nvWriteCdmaPrl: value: " + HexDump.toHexString(preferredRoamingList));
5735 Boolean success = (Boolean) sendRequest(CMD_NV_WRITE_CDMA_PRL, preferredRoamingList);
5736 if (DBG) log("nvWriteCdmaPrl: " + (success ? "ok" : "fail"));
5737 return success;
5738 } finally {
5739 Binder.restoreCallingIdentity(identity);
5740 }
Jake Hambye994d462014-02-03 13:10:13 -08005741 }
5742
5743 /**
chen xu6dac5ab2018-10-26 17:39:23 -07005744 * Rollback modem configurations to factory default except some config which are in whitelist.
Jake Hambye994d462014-02-03 13:10:13 -08005745 * Used for device configuration by some CDMA operators.
5746 *
chen xu6dac5ab2018-10-26 17:39:23 -07005747 * @param slotIndex - device slot.
5748 *
Jake Hambye994d462014-02-03 13:10:13 -08005749 * @return true on success; false on any failure
5750 */
5751 @Override
chen xu6dac5ab2018-10-26 17:39:23 -07005752 public boolean resetModemConfig(int slotIndex) {
5753 Phone phone = PhoneFactory.getPhone(slotIndex);
5754 if (phone != null) {
5755 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
5756 mApp, phone.getSubId(), "resetModemConfig");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005757
chen xu6dac5ab2018-10-26 17:39:23 -07005758 final long identity = Binder.clearCallingIdentity();
5759 try {
5760 Boolean success = (Boolean) sendRequest(CMD_RESET_MODEM_CONFIG, null);
5761 if (DBG) log("resetModemConfig:" + ' ' + (success ? "ok" : "fail"));
5762 return success;
5763 } finally {
5764 Binder.restoreCallingIdentity(identity);
5765 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005766 }
chen xu6dac5ab2018-10-26 17:39:23 -07005767 return false;
5768 }
5769
5770 /**
5771 * Generate a radio modem reset. Used for device configuration by some CDMA operators.
5772 *
5773 * @param slotIndex - device slot.
5774 *
5775 * @return true on success; false on any failure
5776 */
5777 @Override
5778 public boolean rebootModem(int slotIndex) {
5779 Phone phone = PhoneFactory.getPhone(slotIndex);
5780 if (phone != null) {
5781 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
5782 mApp, phone.getSubId(), "rebootModem");
5783
5784 final long identity = Binder.clearCallingIdentity();
5785 try {
5786 Boolean success = (Boolean) sendRequest(CMD_MODEM_REBOOT, null);
5787 if (DBG) log("rebootModem:" + ' ' + (success ? "ok" : "fail"));
5788 return success;
5789 } finally {
5790 Binder.restoreCallingIdentity(identity);
5791 }
5792 }
5793 return false;
Jake Hambye994d462014-02-03 13:10:13 -08005794 }
Jake Hamby7c27be32014-03-03 13:25:59 -08005795
Philip P. Moltmann700a9592019-10-03 11:53:50 -07005796 public String[] getPcscfAddress(String apnType, String callingPackage,
5797 String callingFeatureId) {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08005798 final Phone defaultPhone = getDefaultPhone();
Philip P. Moltmann700a9592019-10-03 11:53:50 -07005799 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(mApp, defaultPhone.getSubId(),
5800 callingPackage, callingFeatureId, "getPcscfAddress")) {
Svet Ganovb320e182015-04-16 12:30:10 -07005801 return new String[0];
5802 }
5803
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005804 final long identity = Binder.clearCallingIdentity();
5805 try {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08005806 return defaultPhone.getPcscfAddress(apnType);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005807 } finally {
5808 Binder.restoreCallingIdentity(identity);
5809 }
Wink Saville36469e72014-06-11 15:17:00 -07005810 }
5811
Brad Ebinger51f743a2017-01-23 13:50:20 -08005812 /**
Grace Jiaaa2eb6b2020-01-09 16:26:08 -08005813 * Toggle IMS disable and enable for the framework to reset it. See {@link #enableIms(int)} and
5814 * {@link #disableIms(int)}.
5815 * @param slotIndex device slot.
5816 */
5817 public void resetIms(int slotIndex) {
5818 enforceModifyPermission();
5819
5820 final long identity = Binder.clearCallingIdentity();
5821 try {
5822 if (mImsResolver == null) {
5823 // may happen if the does not support IMS.
5824 return;
5825 }
5826 mImsResolver.disableIms(slotIndex);
5827 mImsResolver.enableIms(slotIndex);
5828 } finally {
5829 Binder.restoreCallingIdentity(identity);
5830 }
5831 }
5832
5833 /**
Brad Ebinger22bc3e42018-01-16 09:39:35 -08005834 * Enables IMS for the framework. This will trigger IMS registration and ImsFeature capability
5835 * status updates, if not already enabled.
Brad Ebinger51f743a2017-01-23 13:50:20 -08005836 */
Brad Ebinger22bc3e42018-01-16 09:39:35 -08005837 public void enableIms(int slotId) {
Brad Ebinger51f743a2017-01-23 13:50:20 -08005838 enforceModifyPermission();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005839
5840 final long identity = Binder.clearCallingIdentity();
5841 try {
Brad Ebinger24c29992019-12-05 13:03:21 -08005842 if (mImsResolver == null) {
Brad Ebinger9c0eb502019-01-23 15:06:19 -08005843 // may happen if the device does not support IMS.
5844 return;
5845 }
Brad Ebinger24c29992019-12-05 13:03:21 -08005846 mImsResolver.enableIms(slotId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005847 } finally {
5848 Binder.restoreCallingIdentity(identity);
5849 }
Brad Ebinger34bef922017-11-09 10:27:08 -08005850 }
5851
5852 /**
Brad Ebinger22bc3e42018-01-16 09:39:35 -08005853 * Disables IMS for the framework. This will trigger IMS de-registration and trigger ImsFeature
5854 * status updates to disabled.
Brad Ebinger34bef922017-11-09 10:27:08 -08005855 */
Brad Ebinger22bc3e42018-01-16 09:39:35 -08005856 public void disableIms(int slotId) {
5857 enforceModifyPermission();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005858
5859 final long identity = Binder.clearCallingIdentity();
5860 try {
Brad Ebinger24c29992019-12-05 13:03:21 -08005861 if (mImsResolver == null) {
Brad Ebinger9c0eb502019-01-23 15:06:19 -08005862 // may happen if the device does not support IMS.
5863 return;
5864 }
Brad Ebinger24c29992019-12-05 13:03:21 -08005865 mImsResolver.disableIms(slotId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005866 } finally {
5867 Binder.restoreCallingIdentity(identity);
5868 }
Brad Ebinger22bc3e42018-01-16 09:39:35 -08005869 }
5870
5871 /**
Brad Ebinger67b3e042020-09-11 12:45:11 -07005872 * Registers for updates to the MmTelFeature connection through the IImsServiceFeatureCallback
5873 * callback.
Brad Ebinger22bc3e42018-01-16 09:39:35 -08005874 */
Brad Ebinger67b3e042020-09-11 12:45:11 -07005875 @Override
Brad Ebingerf6aca002020-10-01 13:51:05 -07005876 public void registerMmTelFeatureCallback(int slotId, IImsServiceFeatureCallback callback) {
Brad Ebinger34bef922017-11-09 10:27:08 -08005877 enforceModifyPermission();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005878
5879 final long identity = Binder.clearCallingIdentity();
5880 try {
Brad Ebinger24c29992019-12-05 13:03:21 -08005881 if (mImsResolver == null) {
Brad Ebinger67b3e042020-09-11 12:45:11 -07005882 throw new ServiceSpecificException(ImsException.CODE_ERROR_UNSUPPORTED_OPERATION,
5883 "Device does not support IMS");
Brad Ebinger9c0eb502019-01-23 15:06:19 -08005884 }
Brad Ebingerf6aca002020-10-01 13:51:05 -07005885 mImsResolver.listenForFeature(slotId, ImsFeature.FEATURE_MMTEL, callback);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005886 } finally {
5887 Binder.restoreCallingIdentity(identity);
5888 }
Brad Ebinger34bef922017-11-09 10:27:08 -08005889 }
Brad Ebinger5f64b052017-12-14 14:26:15 -08005890 /**
Brad Ebinger075ff3a2020-05-18 17:52:58 -07005891 * Unregister a previously registered IImsServiceFeatureCallback associated with an ImsFeature.
5892 */
Brad Ebinger67b3e042020-09-11 12:45:11 -07005893 @Override
5894 public void unregisterImsFeatureCallback(IImsServiceFeatureCallback callback) {
Brad Ebinger075ff3a2020-05-18 17:52:58 -07005895 enforceModifyPermission();
5896
5897 final long identity = Binder.clearCallingIdentity();
5898 try {
5899 if (mImsResolver == null) return;
Brad Ebinger67b3e042020-09-11 12:45:11 -07005900 mImsResolver.unregisterImsFeatureCallback(callback);
Brad Ebinger075ff3a2020-05-18 17:52:58 -07005901 } finally {
5902 Binder.restoreCallingIdentity(identity);
5903 }
5904 }
5905
5906 /**
Brad Ebinger5f64b052017-12-14 14:26:15 -08005907 * Returns the {@link IImsRegistration} structure associated with the slotId and feature
Brad Ebinger9c0eb502019-01-23 15:06:19 -08005908 * specified or null if IMS is not supported on the slot specified.
Brad Ebinger5f64b052017-12-14 14:26:15 -08005909 */
5910 public IImsRegistration getImsRegistration(int slotId, int feature) throws RemoteException {
5911 enforceModifyPermission();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005912
5913 final long identity = Binder.clearCallingIdentity();
5914 try {
Brad Ebinger24c29992019-12-05 13:03:21 -08005915 if (mImsResolver == null) {
Brad Ebinger9c0eb502019-01-23 15:06:19 -08005916 // may happen if the device does not support IMS.
5917 return null;
5918 }
Brad Ebinger24c29992019-12-05 13:03:21 -08005919 return mImsResolver.getImsRegistration(slotId, feature);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005920 } finally {
5921 Binder.restoreCallingIdentity(identity);
5922 }
Brad Ebinger5f64b052017-12-14 14:26:15 -08005923 }
5924
Brad Ebinger22bc3e42018-01-16 09:39:35 -08005925 /**
5926 * Returns the {@link IImsConfig} structure associated with the slotId and feature
Brad Ebinger9c0eb502019-01-23 15:06:19 -08005927 * specified or null if IMS is not supported on the slot specified.
Brad Ebinger22bc3e42018-01-16 09:39:35 -08005928 */
5929 public IImsConfig getImsConfig(int slotId, int feature) throws RemoteException {
5930 enforceModifyPermission();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005931
5932 final long identity = Binder.clearCallingIdentity();
5933 try {
Brad Ebinger24c29992019-12-05 13:03:21 -08005934 if (mImsResolver == null) {
Brad Ebinger9c0eb502019-01-23 15:06:19 -08005935 // may happen if the device does not support IMS.
5936 return null;
5937 }
Brad Ebinger24c29992019-12-05 13:03:21 -08005938 return mImsResolver.getImsConfig(slotId, feature);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005939 } finally {
5940 Binder.restoreCallingIdentity(identity);
5941 }
Brad Ebinger22bc3e42018-01-16 09:39:35 -08005942 }
5943
Brad Ebinger884c07b2018-02-15 16:17:40 -08005944 /**
Brad Ebingerdac2f002018-04-03 15:17:52 -07005945 * Sets the ImsService Package Name that Telephony will bind to.
5946 *
Brad Ebinger24c29992019-12-05 13:03:21 -08005947 * @param slotIndex the slot ID that the ImsService should bind for.
5948 * @param isCarrierService true if the ImsService is the carrier override, false if the
Brad Ebingerdac2f002018-04-03 15:17:52 -07005949 * ImsService is the device default ImsService.
Brad Ebinger24c29992019-12-05 13:03:21 -08005950 * @param featureTypes An integer array of feature types associated with a packageName.
5951 * @param packageName The name of the package that the current configuration will be replaced
5952 * with.
Brad Ebingerdac2f002018-04-03 15:17:52 -07005953 * @return true if setting the ImsService to bind to succeeded, false if it did not.
Brad Ebingerdac2f002018-04-03 15:17:52 -07005954 */
Brad Ebinger24c29992019-12-05 13:03:21 -08005955 public boolean setBoundImsServiceOverride(int slotIndex, boolean isCarrierService,
5956 int[] featureTypes, String packageName) {
5957 int[] subIds = SubscriptionManager.getSubId(slotIndex);
5958 TelephonyPermissions.enforceShellOnly(Binder.getCallingUid(), "setBoundImsServiceOverride");
Brad Ebingerde696de2018-04-06 09:56:40 -07005959 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(mApp,
5960 (subIds != null ? subIds[0] : SubscriptionManager.INVALID_SUBSCRIPTION_ID),
Brad Ebinger24c29992019-12-05 13:03:21 -08005961 "setBoundImsServiceOverride");
Brad Ebingerde696de2018-04-06 09:56:40 -07005962
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005963 final long identity = Binder.clearCallingIdentity();
5964 try {
Brad Ebinger24c29992019-12-05 13:03:21 -08005965 if (mImsResolver == null) {
Brad Ebinger9c0eb502019-01-23 15:06:19 -08005966 // may happen if the device does not support IMS.
5967 return false;
5968 }
Brad Ebinger24c29992019-12-05 13:03:21 -08005969 Map<Integer, String> featureConfig = new HashMap<>();
5970 for (int featureType : featureTypes) {
5971 featureConfig.put(featureType, packageName);
5972 }
5973 return mImsResolver.overrideImsServiceConfiguration(slotIndex, isCarrierService,
5974 featureConfig);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005975 } finally {
5976 Binder.restoreCallingIdentity(identity);
5977 }
Brad Ebingerdac2f002018-04-03 15:17:52 -07005978 }
5979
5980 /**
Brad Ebinger999d3302020-11-25 14:31:39 -08005981 * Clears any carrier ImsService overrides for the slot index specified that were previously
5982 * set with {@link #setBoundImsServiceOverride(int, boolean, int[], String)}.
5983 *
5984 * This should only be used for testing.
5985 *
5986 * @param slotIndex the slot ID that the ImsService should bind for.
5987 * @return true if clearing the carrier ImsService override succeeded or false if it did not.
5988 */
5989 @Override
5990 public boolean clearCarrierImsServiceOverride(int slotIndex) {
5991 int[] subIds = SubscriptionManager.getSubId(slotIndex);
5992 TelephonyPermissions.enforceShellOnly(Binder.getCallingUid(),
5993 "clearCarrierImsServiceOverride");
5994 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(mApp,
5995 (subIds != null ? subIds[0] : SubscriptionManager.INVALID_SUBSCRIPTION_ID),
5996 "clearCarrierImsServiceOverride");
5997
5998 final long identity = Binder.clearCallingIdentity();
5999 try {
6000 if (mImsResolver == null) {
6001 // may happen if the device does not support IMS.
6002 return false;
6003 }
6004 return mImsResolver.clearCarrierImsServiceConfiguration(slotIndex);
6005 } finally {
6006 Binder.restoreCallingIdentity(identity);
6007 }
6008 }
6009
6010 /**
Brad Ebinger24c29992019-12-05 13:03:21 -08006011 * Return the package name of the currently bound ImsService.
Brad Ebingerdac2f002018-04-03 15:17:52 -07006012 *
6013 * @param slotId The slot that the ImsService is associated with.
6014 * @param isCarrierImsService true, if the ImsService is a carrier override, false if it is
6015 * the device default.
Brad Ebinger24c29992019-12-05 13:03:21 -08006016 * @param featureType The feature associated with the queried configuration.
Brad Ebingerdac2f002018-04-03 15:17:52 -07006017 * @return the package name of the ImsService configuration.
6018 */
Brad Ebinger24c29992019-12-05 13:03:21 -08006019 public String getBoundImsServicePackage(int slotId, boolean isCarrierImsService,
6020 @ImsFeature.FeatureType int featureType) {
Brad Ebingerde696de2018-04-06 09:56:40 -07006021 int[] subIds = SubscriptionManager.getSubId(slotId);
Brad Ebinger24c29992019-12-05 13:03:21 -08006022 TelephonyPermissions
Nathan Harold62c68512021-04-06 11:26:02 -07006023 .enforceCallingOrSelfReadPrivilegedPhoneStatePermissionOrCarrierPrivilege(
Brad Ebinger24c29992019-12-05 13:03:21 -08006024 mApp, (subIds != null ? subIds[0] : SubscriptionManager.INVALID_SUBSCRIPTION_ID),
6025 "getBoundImsServicePackage");
Brad Ebingerde696de2018-04-06 09:56:40 -07006026
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006027 final long identity = Binder.clearCallingIdentity();
6028 try {
Brad Ebinger24c29992019-12-05 13:03:21 -08006029 if (mImsResolver == null) {
Brad Ebinger9c0eb502019-01-23 15:06:19 -08006030 // may happen if the device does not support IMS.
6031 return "";
6032 }
Brad Ebingera80c3312019-12-02 10:59:39 -08006033 // TODO: change API to query RCS separately.
Brad Ebinger24c29992019-12-05 13:03:21 -08006034 return mImsResolver.getImsServiceConfiguration(slotId, isCarrierImsService,
6035 featureType);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006036 } finally {
6037 Binder.restoreCallingIdentity(identity);
6038 }
Brad Ebingerdac2f002018-04-03 15:17:52 -07006039 }
6040
Brad Ebingerbc7dd582019-10-17 17:03:22 -07006041 /**
6042 * Get the MmTelFeature state associated with the requested subscription id.
6043 * @param subId The subscription that the MmTelFeature is associated with.
6044 * @param callback A callback with an integer containing the
6045 * {@link android.telephony.ims.feature.ImsFeature.ImsState} associated with the MmTelFeature.
6046 */
6047 @Override
6048 public void getImsMmTelFeatureState(int subId, IIntegerConsumer callback) {
6049 enforceReadPrivilegedPermission("getImsMmTelFeatureState");
6050 if (!ImsManager.isImsSupportedOnDevice(mApp)) {
6051 throw new ServiceSpecificException(ImsException.CODE_ERROR_UNSUPPORTED_OPERATION,
6052 "IMS not available on device.");
6053 }
6054 final long token = Binder.clearCallingIdentity();
6055 try {
6056 int slotId = getSlotIndex(subId);
6057 if (slotId <= SubscriptionManager.INVALID_SIM_SLOT_INDEX) {
6058 Log.w(LOG_TAG, "getImsMmTelFeatureState: called with an inactive subscription '"
6059 + subId + "'");
6060 throw new ServiceSpecificException(ImsException.CODE_ERROR_INVALID_SUBSCRIPTION);
6061 }
Brad Ebinger919631e2021-06-02 17:46:35 -07006062 verifyImsMmTelConfiguredOrThrow(slotId);
Brad Ebingerbc7dd582019-10-17 17:03:22 -07006063 ImsManager.getInstance(mApp, slotId).getImsServiceState(anInteger -> {
6064 try {
6065 callback.accept(anInteger == null ? ImsFeature.STATE_UNAVAILABLE : anInteger);
6066 } catch (RemoteException e) {
6067 Log.w(LOG_TAG, "getImsMmTelFeatureState: remote caller is no longer running. "
6068 + "Ignore");
6069 }
6070 });
Brad Ebinger919631e2021-06-02 17:46:35 -07006071 } catch (ImsException e) {
6072 throw new ServiceSpecificException(e.getCode());
Brad Ebingerbc7dd582019-10-17 17:03:22 -07006073 } finally {
6074 Binder.restoreCallingIdentity(token);
6075 }
6076 }
6077
Daniel Brightbb5840b2021-01-12 15:48:18 -08006078 /**
6079 * Sets the ims registration state on all valid {@link Phone}s.
6080 */
6081 public void setImsRegistrationState(final boolean registered) {
Wink Saville36469e72014-06-11 15:17:00 -07006082 enforceModifyPermission();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006083
6084 final long identity = Binder.clearCallingIdentity();
6085 try {
Daniel Brightbb5840b2021-01-12 15:48:18 -08006086 // NOTE: Before S, this method only set the default phone.
6087 for (final Phone phone : PhoneFactory.getPhones()) {
6088 if (SubscriptionManager.isValidSubscriptionId(phone.getSubId())) {
6089 phone.setImsRegistrationState(registered);
6090 }
6091 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006092 } finally {
6093 Binder.restoreCallingIdentity(identity);
6094 }
Wink Saville36469e72014-06-11 15:17:00 -07006095 }
6096
6097 /**
Stuart Scott54788802015-03-30 13:18:01 -07006098 * Set the network selection mode to automatic.
6099 *
6100 */
6101 @Override
6102 public void setNetworkSelectionModeAutomatic(int subId) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08006103 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
6104 mApp, subId, "setNetworkSelectionModeAutomatic");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006105
6106 final long identity = Binder.clearCallingIdentity();
6107 try {
shilufc958392020-01-20 11:36:01 -08006108 if (!isActiveSubscription(subId)) {
6109 return;
6110 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006111 if (DBG) log("setNetworkSelectionModeAutomatic: subId " + subId);
Rambo Wang0f050d82021-02-12 11:43:36 -08006112 sendRequest(CMD_SET_NETWORK_SELECTION_MODE_AUTOMATIC, null, subId,
6113 SET_NETWORK_SELECTION_MODE_AUTOMATIC_TIMEOUT_MS);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006114 } finally {
6115 Binder.restoreCallingIdentity(identity);
6116 }
Stuart Scott54788802015-03-30 13:18:01 -07006117 }
6118
Jack Yud10cdd42020-09-28 20:28:01 -07006119 /**
Pengquan Mengea84e042018-09-20 14:57:26 -07006120 * Ask the radio to connect to the input network and change selection mode to manual.
6121 *
6122 * @param subId the id of the subscription.
6123 * @param operatorInfo the operator information, included the PLMN, long name and short name of
6124 * the operator to attach to.
6125 * @param persistSelection whether the selection will persist until reboot. If true, only allows
6126 * attaching to the selected PLMN until reboot; otherwise, attach to the chosen PLMN and resume
6127 * normal network selection next time.
6128 * @return {@code true} on success; {@code true} on any failure.
Shishir Agrawal302c8692015-06-19 13:49:39 -07006129 */
6130 @Override
Pengquan Mengea84e042018-09-20 14:57:26 -07006131 public boolean setNetworkSelectionModeManual(
6132 int subId, OperatorInfo operatorInfo, boolean persistSelection) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08006133 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
6134 mApp, subId, "setNetworkSelectionModeManual");
Pengquan Menge92a50d2018-09-21 15:54:48 -07006135
6136 if (!isActiveSubscription(subId)) {
6137 return false;
6138 }
6139
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006140 final long identity = Binder.clearCallingIdentity();
6141 try {
Pengquan Mengea84e042018-09-20 14:57:26 -07006142 ManualNetworkSelectionArgument arg = new ManualNetworkSelectionArgument(operatorInfo,
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006143 persistSelection);
Pengquan Mengea84e042018-09-20 14:57:26 -07006144 if (DBG) {
6145 log("setNetworkSelectionModeManual: subId: " + subId
6146 + " operator: " + operatorInfo);
6147 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006148 return (Boolean) sendRequest(CMD_SET_NETWORK_SELECTION_MODE_MANUAL, arg, subId);
6149 } finally {
6150 Binder.restoreCallingIdentity(identity);
6151 }
Shishir Agrawal302c8692015-06-19 13:49:39 -07006152 }
shilu84f6e8b2019-12-19 13:58:01 -08006153 /**
6154 * Get the manual network selection
6155 *
6156 * @param subId the id of the subscription.
6157 *
6158 * @return the previously saved user selected PLMN
6159 */
6160 @Override
6161 public String getManualNetworkSelectionPlmn(int subId) {
6162 TelephonyPermissions
Nathan Harold62c68512021-04-06 11:26:02 -07006163 .enforceCallingOrSelfReadPrecisePhoneStatePermissionOrCarrierPrivilege(
shilu84f6e8b2019-12-19 13:58:01 -08006164 mApp, subId, "getManualNetworkSelectionPlmn");
6165
6166 final long identity = Binder.clearCallingIdentity();
6167 try {
6168 if (!isActiveSubscription(subId)) {
shilufa1c2592020-03-10 10:59:43 -07006169 throw new IllegalArgumentException("Invalid Subscription Id: " + subId);
shilu84f6e8b2019-12-19 13:58:01 -08006170 }
6171
6172 final Phone phone = getPhone(subId);
6173 if (phone == null) {
shilufa1c2592020-03-10 10:59:43 -07006174 throw new IllegalArgumentException("Invalid Subscription Id: " + subId);
shilu84f6e8b2019-12-19 13:58:01 -08006175 }
6176 OperatorInfo networkSelection = phone.getSavedNetworkSelection();
6177 return TextUtils.isEmpty(networkSelection.getOperatorNumeric())
6178 ? phone.getManualNetworkSelectionPlmn() : networkSelection.getOperatorNumeric();
6179 } finally {
6180 Binder.restoreCallingIdentity(identity);
6181 }
6182 }
Shishir Agrawal302c8692015-06-19 13:49:39 -07006183
6184 /**
6185 * Scans for available networks.
6186 */
6187 @Override
Philip P. Moltmann3a2772a2019-10-04 08:15:00 -07006188 public CellNetworkScanResult getCellNetworkScanResults(int subId, String callingPackage,
6189 String callingFeatureId) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08006190 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
6191 mApp, subId, "getCellNetworkScanResults");
Hall Liuf19c44f2018-11-27 14:38:17 -08006192 LocationAccessPolicy.LocationPermissionResult locationResult =
6193 LocationAccessPolicy.checkLocationPermission(mApp,
6194 new LocationAccessPolicy.LocationPermissionQuery.Builder()
6195 .setCallingPackage(callingPackage)
Philip P. Moltmann3a2772a2019-10-04 08:15:00 -07006196 .setCallingFeatureId(callingFeatureId)
Hall Liuf19c44f2018-11-27 14:38:17 -08006197 .setCallingPid(Binder.getCallingPid())
6198 .setCallingUid(Binder.getCallingUid())
6199 .setMethod("getCellNetworkScanResults")
6200 .setMinSdkVersionForFine(Build.VERSION_CODES.Q)
Hall Liuc4a3e422020-05-26 17:18:03 -07006201 .setMinSdkVersionForCoarse(Build.VERSION_CODES.Q)
6202 .setMinSdkVersionForEnforcement(Build.VERSION_CODES.Q)
Hall Liuf19c44f2018-11-27 14:38:17 -08006203 .build());
6204 switch (locationResult) {
6205 case DENIED_HARD:
6206 throw new SecurityException("Not allowed to access scan results -- location");
6207 case DENIED_SOFT:
6208 return null;
6209 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006210
Pengquan Menga1bb6272018-09-06 09:59:22 -07006211 long identity = Binder.clearCallingIdentity();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006212 try {
6213 if (DBG) log("getCellNetworkScanResults: subId " + subId);
Pengquan Menga1bb6272018-09-06 09:59:22 -07006214 return (CellNetworkScanResult) sendRequest(
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006215 CMD_PERFORM_NETWORK_SCAN, null, subId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006216 } finally {
6217 Binder.restoreCallingIdentity(identity);
6218 }
Shishir Agrawal302c8692015-06-19 13:49:39 -07006219 }
6220
6221 /**
Shuo Qian4a594052020-01-23 11:59:30 -08006222 * Get the call forwarding info, given the call forwarding reason.
6223 */
6224 @Override
Hall Liu27d24262020-09-18 19:04:59 -07006225 public void getCallForwarding(int subId, int callForwardingReason,
6226 ICallForwardingInfoCallback callback) {
Shuo Qian4a594052020-01-23 11:59:30 -08006227 enforceReadPrivilegedPermission("getCallForwarding");
6228 long identity = Binder.clearCallingIdentity();
6229 try {
6230 if (DBG) {
6231 log("getCallForwarding: subId " + subId
6232 + " callForwardingReason" + callForwardingReason);
6233 }
Hall Liu27d24262020-09-18 19:04:59 -07006234
6235 Phone phone = getPhone(subId);
6236 if (phone == null) {
6237 try {
Hall Liu940c4ca2020-09-29 17:10:18 -07006238 callback.onError(
6239 TelephonyManager.CallForwardingInfoCallback.RESULT_ERROR_UNKNOWN);
Hall Liu27d24262020-09-18 19:04:59 -07006240 } catch (RemoteException e) {
6241 // ignore
6242 }
6243 return;
6244 }
6245
6246 Pair<Integer, TelephonyManager.CallForwardingInfoCallback> argument = Pair.create(
6247 callForwardingReason, new TelephonyManager.CallForwardingInfoCallback() {
6248 @Override
6249 public void onCallForwardingInfoAvailable(CallForwardingInfo info) {
6250 try {
6251 callback.onCallForwardingInfoAvailable(info);
6252 } catch (RemoteException e) {
6253 // ignore
6254 }
6255 }
6256
6257 @Override
6258 public void onError(int error) {
6259 try {
6260 callback.onError(error);
6261 } catch (RemoteException e) {
6262 // ignore
6263 }
6264 }
6265 });
6266 sendRequestAsync(CMD_GET_CALL_FORWARDING, argument, phone, null);
Shuo Qian4a594052020-01-23 11:59:30 -08006267 } finally {
6268 Binder.restoreCallingIdentity(identity);
6269 }
6270 }
6271
6272 /**
6273 * Sets the voice call forwarding info including status (enable/disable), call forwarding
6274 * reason, the number to forward, and the timeout before the forwarding is attempted.
6275 */
6276 @Override
Hall Liu27d24262020-09-18 19:04:59 -07006277 public void setCallForwarding(int subId, CallForwardingInfo callForwardingInfo,
6278 IIntegerConsumer callback) {
Shuo Qian4a594052020-01-23 11:59:30 -08006279 enforceModifyPermission();
6280 long identity = Binder.clearCallingIdentity();
6281 try {
6282 if (DBG) {
6283 log("setCallForwarding: subId " + subId
6284 + " callForwardingInfo" + callForwardingInfo);
6285 }
Hall Liu27d24262020-09-18 19:04:59 -07006286
6287 Phone phone = getPhone(subId);
6288 if (phone == null) {
6289 try {
Hall Liu940c4ca2020-09-29 17:10:18 -07006290 callback.accept(
6291 TelephonyManager.CallForwardingInfoCallback.RESULT_ERROR_UNKNOWN);
Hall Liu27d24262020-09-18 19:04:59 -07006292 } catch (RemoteException e) {
6293 // ignore
6294 }
6295 return;
6296 }
6297
6298 Pair<CallForwardingInfo, Consumer<Integer>> arguments = Pair.create(callForwardingInfo,
6299 FunctionalUtils.ignoreRemoteException(callback::accept));
6300
6301 sendRequestAsync(CMD_SET_CALL_FORWARDING, arguments, phone, null);
Shuo Qian4a594052020-01-23 11:59:30 -08006302 } finally {
6303 Binder.restoreCallingIdentity(identity);
6304 }
6305 }
6306
6307 /**
Hall Liu27d24262020-09-18 19:04:59 -07006308 * Get the call waiting status for a subId.
Shuo Qian4a594052020-01-23 11:59:30 -08006309 */
6310 @Override
Hall Liu27d24262020-09-18 19:04:59 -07006311 public void getCallWaitingStatus(int subId, IIntegerConsumer callback) {
SongFerngWang0e767992021-03-31 22:08:45 +08006312 enforceReadPrivilegedPermission("getCallWaitingStatus");
Shuo Qian4a594052020-01-23 11:59:30 -08006313 long identity = Binder.clearCallingIdentity();
6314 try {
Hall Liu27d24262020-09-18 19:04:59 -07006315 Phone phone = getPhone(subId);
6316 if (phone == null) {
6317 try {
6318 callback.accept(TelephonyManager.CALL_WAITING_STATUS_UNKNOWN_ERROR);
6319 } catch (RemoteException e) {
6320 // ignore
6321 }
6322 return;
6323 }
SongFerngWang0e767992021-03-31 22:08:45 +08006324 CarrierConfigManager configManager = new CarrierConfigManager(phone.getContext());
6325 PersistableBundle c = configManager.getConfigForSubId(subId);
6326 boolean requireUssd = c.getBoolean(
6327 CarrierConfigManager.KEY_USE_CALL_WAITING_USSD_BOOL, false);
Hall Liu27d24262020-09-18 19:04:59 -07006328
Shuo Qian4a594052020-01-23 11:59:30 -08006329 if (DBG) log("getCallWaitingStatus: subId " + subId);
SongFerngWang0e767992021-03-31 22:08:45 +08006330 if (requireUssd) {
6331 CarrierXmlParser carrierXmlParser = new CarrierXmlParser(phone.getContext(),
6332 getSubscriptionCarrierId(subId));
6333 String newUssdCommand = "";
6334 try {
6335 newUssdCommand = carrierXmlParser.getFeature(
6336 CarrierXmlParser.FEATURE_CALL_WAITING)
6337 .makeCommand(CarrierXmlParser.SsEntry.SSAction.QUERY, null);
6338 } catch (NullPointerException e) {
6339 loge("Failed to generate USSD number" + e);
6340 }
6341 ResultReceiver wrappedCallback = new CallWaitingUssdResultReceiver(
6342 mMainThreadHandler, callback, carrierXmlParser,
6343 CarrierXmlParser.SsEntry.SSAction.QUERY);
6344 final String ussdCommand = newUssdCommand;
6345 Executors.newSingleThreadExecutor().execute(() -> {
6346 handleUssdRequest(subId, ussdCommand, wrappedCallback);
6347 });
6348 } else {
6349 Consumer<Integer> argument = FunctionalUtils.ignoreRemoteException(
6350 callback::accept);
6351 sendRequestAsync(CMD_GET_CALL_WAITING, argument, phone, null);
6352 }
Shuo Qian4a594052020-01-23 11:59:30 -08006353 } finally {
6354 Binder.restoreCallingIdentity(identity);
6355 }
6356 }
6357
6358 /**
Hall Liu27d24262020-09-18 19:04:59 -07006359 * Sets whether call waiting is enabled for a given subId.
Shuo Qian4a594052020-01-23 11:59:30 -08006360 */
6361 @Override
Hall Liu27d24262020-09-18 19:04:59 -07006362 public void setCallWaitingStatus(int subId, boolean enable, IIntegerConsumer callback) {
Shuo Qian4a594052020-01-23 11:59:30 -08006363 enforceModifyPermission();
6364 long identity = Binder.clearCallingIdentity();
6365 try {
Hall Liu27d24262020-09-18 19:04:59 -07006366 if (DBG) log("setCallWaitingStatus: subId " + subId + " enable: " + enable);
6367
6368 Phone phone = getPhone(subId);
6369 if (phone == null) {
6370 try {
6371 callback.accept(TelephonyManager.CALL_WAITING_STATUS_UNKNOWN_ERROR);
6372 } catch (RemoteException e) {
6373 // ignore
6374 }
6375 return;
6376 }
6377
SongFerngWang0e767992021-03-31 22:08:45 +08006378 CarrierConfigManager configManager = new CarrierConfigManager(phone.getContext());
6379 PersistableBundle c = configManager.getConfigForSubId(subId);
6380 boolean requireUssd = c.getBoolean(
6381 CarrierConfigManager.KEY_USE_CALL_WAITING_USSD_BOOL, false);
Hall Liu27d24262020-09-18 19:04:59 -07006382
SongFerngWang0e767992021-03-31 22:08:45 +08006383 if (DBG) log("getCallWaitingStatus: subId " + subId);
6384 if (requireUssd) {
6385 CarrierXmlParser carrierXmlParser = new CarrierXmlParser(phone.getContext(),
6386 getSubscriptionCarrierId(subId));
6387 CarrierXmlParser.SsEntry.SSAction ssAction =
6388 enable ? CarrierXmlParser.SsEntry.SSAction.UPDATE_ACTIVATE
6389 : CarrierXmlParser.SsEntry.SSAction.UPDATE_DEACTIVATE;
6390 String newUssdCommand = "";
6391 try {
6392 newUssdCommand = carrierXmlParser.getFeature(
6393 CarrierXmlParser.FEATURE_CALL_WAITING)
6394 .makeCommand(ssAction, null);
6395 } catch (NullPointerException e) {
6396 loge("Failed to generate USSD number" + e);
6397 }
6398 ResultReceiver wrappedCallback = new CallWaitingUssdResultReceiver(
6399 mMainThreadHandler, callback, carrierXmlParser, ssAction);
6400 final String ussdCommand = newUssdCommand;
6401 Executors.newSingleThreadExecutor().execute(() -> {
6402 handleUssdRequest(subId, ussdCommand, wrappedCallback);
6403 });
6404 } else {
6405 Pair<Boolean, Consumer<Integer>> arguments = Pair.create(enable,
6406 FunctionalUtils.ignoreRemoteException(callback::accept));
6407
6408 sendRequestAsync(CMD_SET_CALL_WAITING, arguments, phone, null);
6409 }
Shuo Qian4a594052020-01-23 11:59:30 -08006410 } finally {
6411 Binder.restoreCallingIdentity(identity);
6412 }
6413 }
6414
6415 /**
yinxub1bed742017-04-17 11:45:04 -07006416 * Starts a new network scan and returns the id of this scan.
yinxu504e1392017-04-12 16:03:22 -07006417 *
yinxub1bed742017-04-17 11:45:04 -07006418 * @param subId id of the subscription
Sooraj Sasindran2250dc02021-11-10 16:42:01 -08006419 * @param renounceFineLocationAccess Set this to true if the caller would not like to receive
6420 * location related information which will be sent if the caller already possess
6421 * {@android.Manifest.permission.ACCESS_FINE_LOCATION} and do not renounce the permission
yinxub1bed742017-04-17 11:45:04 -07006422 * @param request contains the radio access networks with bands/channels to scan
6423 * @param messenger callback messenger for scan results or errors
6424 * @param binder for the purpose of auto clean when the user thread crashes
yinxu504e1392017-04-12 16:03:22 -07006425 * @return the id of the requested scan which can be used to stop the scan.
6426 */
6427 @Override
Sooraj Sasindran2250dc02021-11-10 16:42:01 -08006428 public int requestNetworkScan(int subId, boolean renounceFineLocationAccess,
6429 NetworkScanRequest request, Messenger messenger,
Philip P. Moltmann3a2772a2019-10-04 08:15:00 -07006430 IBinder binder, String callingPackage, String callingFeatureId) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08006431 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
6432 mApp, subId, "requestNetworkScan");
Hall Liuf19c44f2018-11-27 14:38:17 -08006433 LocationAccessPolicy.LocationPermissionResult locationResult =
Sooraj Sasindran2250dc02021-11-10 16:42:01 -08006434 LocationAccessPolicy.LocationPermissionResult.DENIED_HARD;
6435 if (!renounceFineLocationAccess) {
6436 locationResult = LocationAccessPolicy.checkLocationPermission(mApp,
6437 new LocationAccessPolicy.LocationPermissionQuery.Builder()
6438 .setCallingPackage(callingPackage)
6439 .setCallingFeatureId(callingFeatureId)
6440 .setCallingPid(Binder.getCallingPid())
6441 .setCallingUid(Binder.getCallingUid())
6442 .setMethod("requestNetworkScan")
6443 .setMinSdkVersionForFine(Build.VERSION_CODES.Q)
6444 .setMinSdkVersionForCoarse(Build.VERSION_CODES.Q)
6445 .setMinSdkVersionForEnforcement(Build.VERSION_CODES.Q)
6446 .build());
6447 }
Hall Liub2ac8ef2019-02-28 15:56:23 -08006448 if (locationResult != LocationAccessPolicy.LocationPermissionResult.ALLOWED) {
Nathan Harold1c11dba2020-09-22 17:54:53 -07006449 SecurityException e = checkNetworkRequestForSanitizedLocationAccess(
6450 request, subId, callingPackage);
Hall Liub2ac8ef2019-02-28 15:56:23 -08006451 if (e != null) {
6452 if (locationResult == LocationAccessPolicy.LocationPermissionResult.DENIED_HARD) {
6453 throw e;
6454 } else {
Hall Liu0e5abaf2019-04-04 01:25:30 -07006455 loge(e.getMessage());
Hall Liub2ac8ef2019-02-28 15:56:23 -08006456 return TelephonyScanManager.INVALID_SCAN_ID;
6457 }
6458 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006459 }
Hall Liu912dfd32019-04-25 14:02:26 -07006460 int callingUid = Binder.getCallingUid();
6461 int callingPid = Binder.getCallingPid();
Ying Xu94a46582019-04-18 17:14:56 -07006462 final long identity = Binder.clearCallingIdentity();
6463 try {
6464 return mNetworkScanRequestTracker.startNetworkScan(
Sooraj Sasindran2250dc02021-11-10 16:42:01 -08006465 renounceFineLocationAccess, request, messenger, binder, getPhone(subId),
Hall Liu912dfd32019-04-25 14:02:26 -07006466 callingUid, callingPid, callingPackage);
Ying Xu94a46582019-04-18 17:14:56 -07006467 } finally {
6468 Binder.restoreCallingIdentity(identity);
6469 }
yinxu504e1392017-04-12 16:03:22 -07006470 }
6471
Hall Liub2ac8ef2019-02-28 15:56:23 -08006472 private SecurityException checkNetworkRequestForSanitizedLocationAccess(
Nathan Harold1c11dba2020-09-22 17:54:53 -07006473 NetworkScanRequest request, int subId, String callingPackage) {
6474 boolean hasCarrierPriv = checkCarrierPrivilegesForPackage(subId, callingPackage)
Hall Liu558027f2019-05-15 19:14:05 -07006475 == TelephonyManager.CARRIER_PRIVILEGE_STATUS_HAS_ACCESS;
6476 boolean hasNetworkScanPermission =
6477 mApp.checkCallingOrSelfPermission(android.Manifest.permission.NETWORK_SCAN)
6478 == PERMISSION_GRANTED;
6479
6480 if (!hasCarrierPriv && !hasNetworkScanPermission) {
6481 return new SecurityException("permission.NETWORK_SCAN or carrier privileges is needed"
6482 + " for network scans without location access.");
Hall Liub2ac8ef2019-02-28 15:56:23 -08006483 }
6484
6485 if (request.getSpecifiers() != null && request.getSpecifiers().length > 0) {
6486 for (RadioAccessSpecifier ras : request.getSpecifiers()) {
Hall Liub2ac8ef2019-02-28 15:56:23 -08006487 if (ras.getChannels() != null && ras.getChannels().length > 0) {
6488 return new SecurityException("Specific channels must not be"
6489 + " scanned without location access.");
6490 }
6491 }
6492 }
6493
Hall Liub2ac8ef2019-02-28 15:56:23 -08006494 return null;
6495 }
6496
yinxu504e1392017-04-12 16:03:22 -07006497 /**
6498 * Stops an existing network scan with the given scanId.
yinxub1bed742017-04-17 11:45:04 -07006499 *
6500 * @param subId id of the subscription
6501 * @param scanId id of the scan that needs to be stopped
yinxu504e1392017-04-12 16:03:22 -07006502 */
6503 @Override
6504 public void stopNetworkScan(int subId, int scanId) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08006505 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
6506 mApp, subId, "stopNetworkScan");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006507
Hall Liu912dfd32019-04-25 14:02:26 -07006508 int callingUid = Binder.getCallingUid();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006509 final long identity = Binder.clearCallingIdentity();
6510 try {
Hall Liu912dfd32019-04-25 14:02:26 -07006511 mNetworkScanRequestTracker.stopNetworkScan(scanId, callingUid);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006512 } finally {
6513 Binder.restoreCallingIdentity(identity);
6514 }
yinxu504e1392017-04-12 16:03:22 -07006515 }
6516
6517 /**
SongFerngWang3ef3e072020-12-21 16:41:52 +08006518 * Get the allowed network types bitmask.
Junda Liu84d15a22014-07-02 11:21:04 -07006519 *
SongFerngWang3ef3e072020-12-21 16:41:52 +08006520 * @return the allowed network types bitmask, defined in RILConstants.java.
Junda Liu84d15a22014-07-02 11:21:04 -07006521 */
6522 @Override
SongFerngWang3ef3e072020-12-21 16:41:52 +08006523 public int getAllowedNetworkTypesBitmask(int subId) {
Pengquan Menga4009cb2018-12-20 11:00:24 -08006524 TelephonyPermissions
Nathan Harold62c68512021-04-06 11:26:02 -07006525 .enforceCallingOrSelfReadPrivilegedPhoneStatePermissionOrCarrierPrivilege(
SongFerngWang3ef3e072020-12-21 16:41:52 +08006526 mApp, subId, "getAllowedNetworkTypesBitmask");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006527
6528 final long identity = Binder.clearCallingIdentity();
6529 try {
SongFerngWang3ef3e072020-12-21 16:41:52 +08006530 if (DBG) log("getAllowedNetworkTypesBitmask");
6531 int[] result = (int[]) sendRequest(CMD_GET_ALLOWED_NETWORK_TYPES_BITMASK, null, subId);
6532 int networkTypesBitmask = (result != null ? result[0] : -1);
6533 if (DBG) log("getAllowedNetworkTypesBitmask: " + networkTypesBitmask);
6534 return networkTypesBitmask;
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006535 } finally {
6536 Binder.restoreCallingIdentity(identity);
6537 }
Jake Hamby7c27be32014-03-03 13:25:59 -08006538 }
6539
6540 /**
Sooraj Sasindranc46dfbd2020-06-03 01:06:00 -07006541 * Get the allowed network types for certain reason.
6542 *
6543 * @param subId the id of the subscription.
6544 * @param reason the reason the allowed network type change is taking place
6545 * @return the allowed network types.
6546 */
6547 @Override
6548 public long getAllowedNetworkTypesForReason(int subId,
6549 @TelephonyManager.AllowedNetworkTypesReason int reason) {
Nathan Harold62c68512021-04-06 11:26:02 -07006550 TelephonyPermissions.enforceCallingOrSelfReadPrecisePhoneStatePermissionOrCarrierPrivilege(
SongFerngWang8c6e82e2021-03-02 22:09:29 +08006551 mApp, subId, "getAllowedNetworkTypesForReason");
Sooraj Sasindranc46dfbd2020-06-03 01:06:00 -07006552 final long identity = Binder.clearCallingIdentity();
6553 try {
6554 return getPhoneFromSubId(subId).getAllowedNetworkTypes(reason);
6555 } finally {
6556 Binder.restoreCallingIdentity(identity);
6557 }
6558 }
6559
6560 /**
Sooraj Sasindran37444802020-08-11 10:40:43 -07006561 * Enable/Disable E-UTRA-NR Dual Connectivity
6562 * @param subId subscription id of the sim card
6563 * @param nrDualConnectivityState expected NR dual connectivity state
6564 * This can be passed following states
6565 * <ol>
6566 * <li>Enable NR dual connectivity {@link TelephonyManager#NR_DUAL_CONNECTIVITY_ENABLE}
6567 * <li>Disable NR dual connectivity {@link TelephonyManager#NR_DUAL_CONNECTIVITY_DISABLE}
6568 * <li>Disable NR dual connectivity and force secondary cell to be released
6569 * {@link TelephonyManager#NR_DUAL_CONNECTIVITY_DISABLE_IMMEDIATE}
6570 * </ol>
6571 * @return operation result.
6572 */
6573 @Override
6574 public int setNrDualConnectivityState(int subId,
6575 @TelephonyManager.NrDualConnectivityState int nrDualConnectivityState) {
6576 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
6577 mApp, subId, "enableNRDualConnectivity");
Sooraj Sasindran0e4e00a2021-03-16 18:02:32 -07006578 if (!isRadioInterfaceCapabilitySupported(
Sooraj Sasindrandfd595b2021-03-11 17:38:13 -08006579 TelephonyManager.CAPABILITY_NR_DUAL_CONNECTIVITY_CONFIGURATION_AVAILABLE)) {
6580 return TelephonyManager.ENABLE_NR_DUAL_CONNECTIVITY_NOT_SUPPORTED;
6581 }
6582
Sooraj Sasindran37444802020-08-11 10:40:43 -07006583 WorkSource workSource = getWorkSource(Binder.getCallingUid());
6584 final long identity = Binder.clearCallingIdentity();
6585 try {
6586 int result = (int) sendRequest(CMD_ENABLE_NR_DUAL_CONNECTIVITY,
6587 nrDualConnectivityState, subId,
6588 workSource);
6589 if (DBG) log("enableNRDualConnectivity result: " + result);
6590 return result;
6591 } finally {
6592 Binder.restoreCallingIdentity(identity);
6593 }
6594 }
6595
6596 /**
6597 * Is E-UTRA-NR Dual Connectivity enabled
6598 * @return true if dual connectivity is enabled else false
6599 */
6600 @Override
6601 public boolean isNrDualConnectivityEnabled(int subId) {
6602 TelephonyPermissions
Nathan Harold62c68512021-04-06 11:26:02 -07006603 .enforceCallingOrSelfReadPrivilegedPhoneStatePermissionOrCarrierPrivilege(
Sooraj Sasindran37444802020-08-11 10:40:43 -07006604 mApp, subId, "isNRDualConnectivityEnabled");
Sooraj Sasindran0e4e00a2021-03-16 18:02:32 -07006605 if (!isRadioInterfaceCapabilitySupported(
Sooraj Sasindrandfd595b2021-03-11 17:38:13 -08006606 TelephonyManager.CAPABILITY_NR_DUAL_CONNECTIVITY_CONFIGURATION_AVAILABLE)) {
6607 return false;
6608 }
Sooraj Sasindran37444802020-08-11 10:40:43 -07006609 WorkSource workSource = getWorkSource(Binder.getCallingUid());
6610 final long identity = Binder.clearCallingIdentity();
6611 try {
6612 boolean isEnabled = (boolean) sendRequest(CMD_IS_NR_DUAL_CONNECTIVITY_ENABLED,
6613 null, subId, workSource);
6614 if (DBG) log("isNRDualConnectivityEnabled: " + isEnabled);
6615 return isEnabled;
6616 } finally {
6617 Binder.restoreCallingIdentity(identity);
6618 }
6619 }
6620
6621 /**
Sooraj Sasindranc46dfbd2020-06-03 01:06:00 -07006622 * Set the allowed network types of the device and
6623 * provide the reason triggering the allowed network change.
6624 *
6625 * @param subId the id of the subscription.
6626 * @param reason the reason the allowed network type change is taking place
6627 * @param allowedNetworkTypes the allowed network types.
6628 * @return true on success; false on any failure.
6629 */
6630 @Override
6631 public boolean setAllowedNetworkTypesForReason(int subId,
SongFerngWang3ef3e072020-12-21 16:41:52 +08006632 @TelephonyManager.AllowedNetworkTypesReason int reason,
6633 @TelephonyManager.NetworkTypeBitMask long allowedNetworkTypes) {
Sooraj Sasindranc46dfbd2020-06-03 01:06:00 -07006634 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
6635 mApp, subId, "setAllowedNetworkTypesForReason");
SongFerngWang3ef3e072020-12-21 16:41:52 +08006636 if (!TelephonyManager.isValidAllowedNetworkTypesReason(reason)) {
SongFerngWang7ffc2732021-04-15 19:46:33 +08006637 loge("setAllowedNetworkTypesForReason: Invalid allowed network type reason: " + reason);
6638 return false;
6639 }
6640 if (!SubscriptionManager.isUsableSubscriptionId(subId)) {
6641 loge("setAllowedNetworkTypesForReason: Invalid subscriptionId:" + subId);
SongFerngWang3ef3e072020-12-21 16:41:52 +08006642 return false;
6643 }
6644
SongFerngWang8c6e82e2021-03-02 22:09:29 +08006645 log("setAllowedNetworkTypesForReason: " + reason + " value: "
6646 + TelephonyManager.convertNetworkTypeBitmaskToString(allowedNetworkTypes));
6647
6648
6649 if (allowedNetworkTypes == getPhoneFromSubId(subId).getAllowedNetworkTypes(reason)) {
6650 log("setAllowedNetworkTypesForReason: " + reason + "does not change value");
6651 return true;
SongFerngWang3ef3e072020-12-21 16:41:52 +08006652 }
SongFerngWang8c6e82e2021-03-02 22:09:29 +08006653
Sooraj Sasindranc46dfbd2020-06-03 01:06:00 -07006654 final long identity = Binder.clearCallingIdentity();
6655 try {
SongFerngWang3ef3e072020-12-21 16:41:52 +08006656 Boolean success = (Boolean) sendRequest(
6657 CMD_SET_ALLOWED_NETWORK_TYPES_FOR_REASON,
6658 new Pair<Integer, Long>(reason, allowedNetworkTypes), subId);
6659
6660 if (DBG) log("setAllowedNetworkTypesForReason: " + (success ? "ok" : "fail"));
6661 return success;
Sooraj Sasindranc46dfbd2020-06-03 01:06:00 -07006662 } finally {
6663 Binder.restoreCallingIdentity(identity);
6664 }
6665 }
6666
6667 /**
Miaoa84611c2019-03-15 09:21:10 +08006668 * Check whether DUN APN is required for tethering with subId.
Junda Liu475951f2014-11-07 16:45:03 -08006669 *
Miaoa84611c2019-03-15 09:21:10 +08006670 * @param subId the id of the subscription to require tethering.
Amit Mahajanfe58cdf2017-07-11 12:01:53 -07006671 * @return {@code true} if DUN APN is required for tethering.
Junda Liu475951f2014-11-07 16:45:03 -08006672 * @hide
6673 */
6674 @Override
SongFerngWangf08d8122019-11-15 14:58:44 +08006675 public boolean isTetheringApnRequiredForSubscriber(int subId) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08006676 enforceModifyPermission();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006677 final long identity = Binder.clearCallingIdentity();
Miaoa84611c2019-03-15 09:21:10 +08006678 final Phone phone = getPhone(subId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006679 try {
Miaoa84611c2019-03-15 09:21:10 +08006680 if (phone != null) {
6681 return phone.hasMatchedTetherApnSetting();
6682 } else {
6683 return false;
6684 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006685 } finally {
6686 Binder.restoreCallingIdentity(identity);
Junda Liu475951f2014-11-07 16:45:03 -08006687 }
Junda Liu475951f2014-11-07 16:45:03 -08006688 }
6689
6690 /**
Malcolm Chen964682d2017-11-28 16:20:07 -08006691 * Get the user enabled state of Mobile Data.
6692 *
6693 * TODO: remove and use isUserDataEnabled.
6694 * This can't be removed now because some vendor codes
6695 * calls through ITelephony directly while they should
6696 * use TelephonyManager.
6697 *
6698 * @return true on enabled
6699 */
6700 @Override
6701 public boolean getDataEnabled(int subId) {
6702 return isUserDataEnabled(subId);
6703 }
6704
6705 /**
6706 * Get whether mobile data is enabled per user setting.
6707 *
6708 * There are other factors deciding whether mobile data is actually enabled, but they are
6709 * not considered here. See {@link #isDataEnabled(int)} for more details.
Robert Greenwalt646120a2014-05-23 11:54:03 -07006710 *
Sooraj Sasindran5d051bf2021-11-05 13:14:15 -07006711 * Accepts either READ_BASIC_PHONE_STATE, ACCESS_NETWORK_STATE, MODIFY_PHONE_STATE
6712 * or carrier privileges.
Robert Greenwalted86e582014-05-21 20:03:20 -07006713 *
6714 * @return {@code true} if data is enabled else {@code false}
6715 */
6716 @Override
Malcolm Chen964682d2017-11-28 16:20:07 -08006717 public boolean isUserDataEnabled(int subId) {
Sooraj Sasindran5d051bf2021-11-05 13:14:15 -07006718 String functionName = "isUserDataEnabled";
Robert Greenwalt646120a2014-05-23 11:54:03 -07006719 try {
Sooraj Sasindran5d051bf2021-11-05 13:14:15 -07006720 try {
6721 mApp.enforceCallingOrSelfPermission(permission.READ_BASIC_PHONE_STATE,
6722 functionName);
6723 } catch (Exception e) {
6724 mApp.enforceCallingOrSelfPermission(permission.ACCESS_NETWORK_STATE, functionName);
6725 }
Robert Greenwalt646120a2014-05-23 11:54:03 -07006726 } catch (Exception e) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08006727 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
Sooraj Sasindran5d051bf2021-11-05 13:14:15 -07006728 mApp, subId, functionName);
6729
Robert Greenwalt646120a2014-05-23 11:54:03 -07006730 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006731
6732 final long identity = Binder.clearCallingIdentity();
6733 try {
6734 int phoneId = mSubscriptionController.getPhoneId(subId);
6735 if (DBG) log("isUserDataEnabled: subId=" + subId + " phoneId=" + phoneId);
6736 Phone phone = PhoneFactory.getPhone(phoneId);
6737 if (phone != null) {
6738 boolean retVal = phone.isUserDataEnabled();
6739 if (DBG) log("isUserDataEnabled: subId=" + subId + " retVal=" + retVal);
6740 return retVal;
6741 } else {
6742 if (DBG) loge("isUserDataEnabled: no phone subId=" + subId + " retVal=false");
6743 return false;
6744 }
6745 } finally {
6746 Binder.restoreCallingIdentity(identity);
Malcolm Chen964682d2017-11-28 16:20:07 -08006747 }
6748 }
6749
6750 /**
Shuo Qian8ee4e882020-01-08 14:30:06 -08006751 * Checks if the device is capable of mobile data by considering whether whether the
6752 * user has enabled mobile data, whether the carrier has enabled mobile data, and
6753 * whether the network policy allows data connections.
Malcolm Chen964682d2017-11-28 16:20:07 -08006754 *
Shuo Qian8ee4e882020-01-08 14:30:06 -08006755 * @return {@code true} if the overall data connection is capable; {@code false} if not.
Malcolm Chen964682d2017-11-28 16:20:07 -08006756 */
6757 @Override
6758 public boolean isDataEnabled(int subId) {
Sooraj Sasindran5d051bf2021-11-05 13:14:15 -07006759 String functionName = "isDataEnabled";
Sooraj Sasindran95c07a92020-07-15 01:35:24 -07006760 try {
6761 try {
6762 mApp.enforceCallingOrSelfPermission(
6763 android.Manifest.permission.ACCESS_NETWORK_STATE,
Sooraj Sasindran5d051bf2021-11-05 13:14:15 -07006764 functionName);
Sooraj Sasindran95c07a92020-07-15 01:35:24 -07006765 } catch (Exception e) {
Sooraj Sasindran5d051bf2021-11-05 13:14:15 -07006766 try {
6767 mApp.enforceCallingOrSelfPermission(
6768 android.Manifest.permission.READ_PHONE_STATE,
6769 functionName);
6770 } catch (Exception e2) {
6771 mApp.enforceCallingOrSelfPermission(
6772 permission.READ_BASIC_PHONE_STATE, functionName);
6773 }
Sooraj Sasindran95c07a92020-07-15 01:35:24 -07006774 }
6775 } catch (Exception e) {
Sooraj Sasindran5d051bf2021-11-05 13:14:15 -07006776 enforceReadPrivilegedPermission(functionName);
Sooraj Sasindran95c07a92020-07-15 01:35:24 -07006777 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006778
6779 final long identity = Binder.clearCallingIdentity();
6780 try {
6781 int phoneId = mSubscriptionController.getPhoneId(subId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006782 Phone phone = PhoneFactory.getPhone(phoneId);
6783 if (phone != null) {
Jack Yu4ad64e52021-12-03 14:23:53 -08006784 boolean retVal;
6785 if (phone.isUsingNewDataStack()) {
6786 retVal = phone.getDataNetworkController().getDataSettingsManager()
6787 .isDataEnabled();
6788 } else {
6789 retVal = phone.getDataEnabledSettings().isDataEnabled();
6790 }
6791 if (DBG) log("isDataEnabled: " + retVal + ", subId=" + subId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006792 return retVal;
6793 } else {
6794 if (DBG) loge("isDataEnabled: no phone subId=" + subId + " retVal=false");
6795 return false;
6796 }
6797 } finally {
6798 Binder.restoreCallingIdentity(identity);
Wink Savillee7353bb2014-12-05 14:21:41 -08006799 }
Robert Greenwalted86e582014-05-21 20:03:20 -07006800 }
Shishir Agrawal60f9c952014-06-23 12:00:43 -07006801
Sooraj Sasindranaf1b5132020-05-05 21:06:20 +00006802 /**
6803 * Check if data is enabled for a specific reason
6804 * @param subId Subscription index
6805 * @param reason the reason the data enable change is taking place
6806 * @return {@code true} if the overall data is enabled; {@code false} if not.
6807 */
6808 @Override
Sooraj Sasindran95c07a92020-07-15 01:35:24 -07006809 public boolean isDataEnabledForReason(int subId,
Sooraj Sasindranaf1b5132020-05-05 21:06:20 +00006810 @TelephonyManager.DataEnabledReason int reason) {
Sooraj Sasindran5d051bf2021-11-05 13:14:15 -07006811 String functionName = "isDataEnabledForReason";
Sooraj Sasindranaf1b5132020-05-05 21:06:20 +00006812 try {
Sooraj Sasindran5d051bf2021-11-05 13:14:15 -07006813 try {
6814 mApp.enforceCallingOrSelfPermission(
6815 android.Manifest.permission.ACCESS_NETWORK_STATE,
6816 functionName);
6817 } catch (Exception e) {
6818 mApp.enforceCallingOrSelfPermission(permission.READ_BASIC_PHONE_STATE,
6819 functionName);
6820 }
Sooraj Sasindranaf1b5132020-05-05 21:06:20 +00006821 } catch (Exception e) {
Sooraj Sasindran0d909a02021-11-08 12:01:16 -08006822 try {
6823 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.READ_PHONE_STATE,
Sooraj Sasindran5d051bf2021-11-05 13:14:15 -07006824 functionName);
Sooraj Sasindran0d909a02021-11-08 12:01:16 -08006825 } catch (Exception e2) {
6826 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
Sooraj Sasindran5d051bf2021-11-05 13:14:15 -07006827 mApp, subId, functionName);
Sooraj Sasindran0d909a02021-11-08 12:01:16 -08006828 }
Sooraj Sasindranaf1b5132020-05-05 21:06:20 +00006829 }
6830
6831
6832 final long identity = Binder.clearCallingIdentity();
6833 try {
6834 int phoneId = mSubscriptionController.getPhoneId(subId);
6835 if (DBG) {
Sooraj Sasindran95c07a92020-07-15 01:35:24 -07006836 log("isDataEnabledForReason: subId=" + subId + " phoneId=" + phoneId
Sooraj Sasindranaf1b5132020-05-05 21:06:20 +00006837 + " reason=" + reason);
6838 }
6839 Phone phone = PhoneFactory.getPhone(phoneId);
6840 if (phone != null) {
6841 boolean retVal;
Jack Yu99e87332021-12-17 23:14:15 -08006842 if (phone.isUsingNewDataStack()) {
6843 retVal = phone.getDataSettingsManager().isDataEnabledForReason(reason);
Sooraj Sasindranaf1b5132020-05-05 21:06:20 +00006844 } else {
Jack Yu99e87332021-12-17 23:14:15 -08006845 if (reason == TelephonyManager.DATA_ENABLED_REASON_USER) {
6846 retVal = phone.isUserDataEnabled();
6847 } else {
6848 retVal = phone.getDataEnabledSettings().isDataEnabledForReason(reason);
6849 }
Sooraj Sasindranaf1b5132020-05-05 21:06:20 +00006850 }
Sooraj Sasindran95c07a92020-07-15 01:35:24 -07006851 if (DBG) log("isDataEnabledForReason: retVal=" + retVal);
Sooraj Sasindranaf1b5132020-05-05 21:06:20 +00006852 return retVal;
6853 } else {
6854 if (DBG) {
Sooraj Sasindran95c07a92020-07-15 01:35:24 -07006855 loge("isDataEnabledForReason: no phone subId="
Sooraj Sasindranaf1b5132020-05-05 21:06:20 +00006856 + subId + " retVal=false");
6857 }
6858 return false;
6859 }
6860 } finally {
6861 Binder.restoreCallingIdentity(identity);
6862 }
6863 }
6864
Malcolm Chendf0b4cc2020-02-24 15:12:43 -08006865 private int getCarrierPrivilegeStatusFromCarrierConfigRules(int privilegeFromSim, int uid,
Nazanin Bakhshi5d0636e2019-08-19 16:29:37 -07006866 Phone phone) {
Sarah Chin3394efe2021-04-09 10:37:15 -07006867 if (uid == Process.PHONE_UID) {
6868 // Skip the check if it's the phone UID (system UID removed in b/184713596)
6869 // TODO (b/184954344): Check for system/phone UID at call site instead of here
Hall Liu54a2a0c2020-07-13 12:13:03 -07006870 return TelephonyManager.CARRIER_PRIVILEGE_STATUS_HAS_ACCESS;
6871 }
6872
Nazanin Bakhshi5d0636e2019-08-19 16:29:37 -07006873 //load access rules from carrier configs, and check those as well: b/139133814
6874 SubscriptionController subController = SubscriptionController.getInstance();
6875 if (privilegeFromSim == TelephonyManager.CARRIER_PRIVILEGE_STATUS_HAS_ACCESS
6876 || subController == null) return privilegeFromSim;
6877
Nazanin Bakhshi5d0636e2019-08-19 16:29:37 -07006878 PackageManager pkgMgr = phone.getContext().getPackageManager();
6879 String[] packages = pkgMgr.getPackagesForUid(uid);
6880
6881 final long identity = Binder.clearCallingIdentity();
6882 try {
Jeff Davidson8ab02b22020-03-28 12:24:40 -07006883 int subId = phone.getSubId();
6884 if (mCarrierPrivilegeTestOverrideSubIds.contains(subId)) {
6885 // A test override is in place for the privileges for this subId, so don't try to
6886 // read the subscription privileges.
6887 return privilegeFromSim;
6888 }
6889 SubscriptionInfo subInfo = subController.getSubscriptionInfo(subId);
Nazanin Bakhshi5d0636e2019-08-19 16:29:37 -07006890 SubscriptionManager subManager = (SubscriptionManager)
6891 phone.getContext().getSystemService(Context.TELEPHONY_SUBSCRIPTION_SERVICE);
6892 for (String pkg : packages) {
6893 if (subManager.canManageSubscription(subInfo, pkg)) {
6894 return TelephonyManager.CARRIER_PRIVILEGE_STATUS_HAS_ACCESS;
6895 }
6896 }
6897 return privilegeFromSim;
6898 } finally {
6899 Binder.restoreCallingIdentity(identity);
6900 }
6901 }
6902
6903 private int getCarrierPrivilegeStatusFromCarrierConfigRules(int privilegeFromSim, Phone phone,
6904 String pkgName) {
6905 //load access rules from carrier configs, and check those as well: b/139133814
6906 SubscriptionController subController = SubscriptionController.getInstance();
6907 if (privilegeFromSim == TelephonyManager.CARRIER_PRIVILEGE_STATUS_HAS_ACCESS
6908 || subController == null) return privilegeFromSim;
6909
6910 final long identity = Binder.clearCallingIdentity();
6911 try {
Jeff Davidson8ab02b22020-03-28 12:24:40 -07006912 int subId = phone.getSubId();
6913 if (mCarrierPrivilegeTestOverrideSubIds.contains(subId)) {
6914 // A test override is in place for the privileges for this subId, so don't try to
6915 // read the subscription privileges.
6916 return privilegeFromSim;
6917 }
6918 SubscriptionInfo subInfo = subController.getSubscriptionInfo(subId);
Nazanin Bakhshi5d0636e2019-08-19 16:29:37 -07006919 SubscriptionManager subManager = (SubscriptionManager)
6920 phone.getContext().getSystemService(Context.TELEPHONY_SUBSCRIPTION_SERVICE);
6921 return subManager.canManageSubscription(subInfo, pkgName)
6922 ? TelephonyManager.CARRIER_PRIVILEGE_STATUS_HAS_ACCESS : privilegeFromSim;
6923 } finally {
6924 Binder.restoreCallingIdentity(identity);
6925 }
6926 }
6927
Shishir Agrawal60f9c952014-06-23 12:00:43 -07006928 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08006929 public int getCarrierPrivilegeStatus(int subId) {
6930 final Phone phone = getPhone(subId);
6931 if (phone == null) {
6932 loge("getCarrierPrivilegeStatus: Invalid subId");
6933 return TelephonyManager.CARRIER_PRIVILEGE_STATUS_NO_ACCESS;
6934 }
Muralidhar Reddy472c2ae2021-09-29 19:38:40 +00006935 UiccPort port = UiccController.getInstance().getUiccPort(phone.getPhoneId());
6936 if (port == null) {
Shishir Agrawal5e5becd2014-11-18 11:38:23 -08006937 loge("getCarrierPrivilegeStatus: No UICC");
Shishir Agrawaleb8771e2014-07-22 11:24:08 -07006938 return TelephonyManager.CARRIER_PRIVILEGE_STATUS_RULES_NOT_LOADED;
6939 }
Nazanin Bakhshi5d0636e2019-08-19 16:29:37 -07006940
6941 return getCarrierPrivilegeStatusFromCarrierConfigRules(
Muralidhar Reddy472c2ae2021-09-29 19:38:40 +00006942 port.getCarrierPrivilegeStatusForCurrentTransaction(
Malcolm Chendf0b4cc2020-02-24 15:12:43 -08006943 phone.getContext().getPackageManager()), Binder.getCallingUid(), phone);
Shishir Agrawal60f9c952014-06-23 12:00:43 -07006944 }
Junda Liu29340342014-07-10 15:23:27 -07006945
6946 @Override
Jeff Davidson7e17e312018-02-13 18:17:36 -08006947 public int getCarrierPrivilegeStatusForUid(int subId, int uid) {
Shuo Qian2c0ae432019-12-05 11:40:37 -08006948 enforceReadPrivilegedPermission("getCarrierPrivilegeStatusForUid");
Jeff Davidson7e17e312018-02-13 18:17:36 -08006949 final Phone phone = getPhone(subId);
6950 if (phone == null) {
Taesu Leef8fbed92019-10-07 18:47:02 +09006951 loge("getCarrierPrivilegeStatusForUid: Invalid subId");
Jeff Davidson7e17e312018-02-13 18:17:36 -08006952 return TelephonyManager.CARRIER_PRIVILEGE_STATUS_NO_ACCESS;
6953 }
6954 UiccProfile profile =
6955 UiccController.getInstance().getUiccProfileForPhone(phone.getPhoneId());
6956 if (profile == null) {
Taesu Leef8fbed92019-10-07 18:47:02 +09006957 loge("getCarrierPrivilegeStatusForUid: No UICC");
Jeff Davidson7e17e312018-02-13 18:17:36 -08006958 return TelephonyManager.CARRIER_PRIVILEGE_STATUS_RULES_NOT_LOADED;
6959 }
Nazanin Bakhshi5d0636e2019-08-19 16:29:37 -07006960 return getCarrierPrivilegeStatusFromCarrierConfigRules(
Shuo Qian2c0ae432019-12-05 11:40:37 -08006961 profile.getCarrierPrivilegeStatusForUid(
Malcolm Chendf0b4cc2020-02-24 15:12:43 -08006962 phone.getContext().getPackageManager(), uid), uid, phone);
Jeff Davidson7e17e312018-02-13 18:17:36 -08006963 }
6964
6965 @Override
chen xuf7e9fe82019-05-09 19:31:02 -07006966 public int checkCarrierPrivilegesForPackage(int subId, String pkgName) {
Nazanin1adf4562021-03-29 15:35:30 -07006967 enforceReadPrivilegedPermission("checkCarrierPrivilegesForPackage");
chen xuf7e9fe82019-05-09 19:31:02 -07006968 if (TextUtils.isEmpty(pkgName)) {
Junda Liu317d70b2016-03-08 09:33:53 -08006969 return TelephonyManager.CARRIER_PRIVILEGE_STATUS_NO_ACCESS;
chen xuf7e9fe82019-05-09 19:31:02 -07006970 }
6971
6972 int phoneId = SubscriptionManager.getPhoneId(subId);
Muralidhar Reddy472c2ae2021-09-29 19:38:40 +00006973 UiccPort port = UiccController.getInstance().getUiccPort(phoneId);
6974 if (port == null) {
chen xuf7e9fe82019-05-09 19:31:02 -07006975 loge("checkCarrierPrivilegesForPackage: No UICC on subId " + subId);
Shishir Agrawaleb8771e2014-07-22 11:24:08 -07006976 return TelephonyManager.CARRIER_PRIVILEGE_STATUS_RULES_NOT_LOADED;
6977 }
Nazanin Bakhshi5d0636e2019-08-19 16:29:37 -07006978 return getCarrierPrivilegeStatusFromCarrierConfigRules(
Muralidhar Reddy472c2ae2021-09-29 19:38:40 +00006979 port.getCarrierPrivilegeStatus(mApp.getPackageManager(), pkgName),
Nazanin Bakhshi5d0636e2019-08-19 16:29:37 -07006980 getPhone(phoneId), pkgName);
Zach Johnson50ecba32015-05-19 00:24:21 -07006981 }
6982
6983 @Override
6984 public int checkCarrierPrivilegesForPackageAnyPhone(String pkgName) {
Sarah Chinfc3169b2021-04-28 20:21:03 -07006985 // TODO(b/186774706): Remove @RequiresPermission from TelephonyManager API
Junda Liu317d70b2016-03-08 09:33:53 -08006986 if (TextUtils.isEmpty(pkgName))
6987 return TelephonyManager.CARRIER_PRIVILEGE_STATUS_NO_ACCESS;
Zach Johnson50ecba32015-05-19 00:24:21 -07006988 int result = TelephonyManager.CARRIER_PRIVILEGE_STATUS_RULES_NOT_LOADED;
6989 for (int i = 0; i < TelephonyManager.getDefault().getPhoneCount(); i++) {
Muralidhar Reddy472c2ae2021-09-29 19:38:40 +00006990 UiccPort port = UiccController.getInstance().getUiccPort(i);
6991 if (port == null) {
Jonathan Basseri7d320df2015-06-16 12:17:08 -07006992 // No UICC in that slot.
Zach Johnson50ecba32015-05-19 00:24:21 -07006993 continue;
6994 }
6995
Nazanin Bakhshi5d0636e2019-08-19 16:29:37 -07006996 result = getCarrierPrivilegeStatusFromCarrierConfigRules(
Muralidhar Reddy472c2ae2021-09-29 19:38:40 +00006997 port.getCarrierPrivilegeStatus(mApp.getPackageManager(), pkgName),
Nazanin Bakhshi5d0636e2019-08-19 16:29:37 -07006998 getPhone(i), pkgName);
Zach Johnson50ecba32015-05-19 00:24:21 -07006999 if (result == TelephonyManager.CARRIER_PRIVILEGE_STATUS_HAS_ACCESS) {
7000 break;
7001 }
7002 }
7003
7004 return result;
Junda Liu29340342014-07-10 15:23:27 -07007005 }
Derek Tan89e89d42014-07-08 17:00:10 -07007006
7007 @Override
Junda Liue64de782015-04-16 17:19:16 -07007008 public List<String> getCarrierPackageNamesForIntentAndPhone(Intent intent, int phoneId) {
Nazanin1adf4562021-03-29 15:35:30 -07007009 enforceReadPrivilegedPermission("getCarrierPackageNamesForIntentAndPhone");
Junda Liue64de782015-04-16 17:19:16 -07007010 if (!SubscriptionManager.isValidPhoneId(phoneId)) {
7011 loge("phoneId " + phoneId + " is not valid.");
7012 return null;
7013 }
Muralidhar Reddy472c2ae2021-09-29 19:38:40 +00007014 UiccPort port = UiccController.getInstance().getUiccPort(phoneId);
7015 if (port == null) {
Taesu Leef8fbed92019-10-07 18:47:02 +09007016 loge("getCarrierPackageNamesForIntentAndPhone: No UICC");
Shishir Agrawaleb6439a2014-07-21 13:19:38 -07007017 return null ;
7018 }
Muralidhar Reddy472c2ae2021-09-29 19:38:40 +00007019 return port.getCarrierPackageNamesForIntent(mApp.getPackageManager(), intent);
Shishir Agrawaleb6439a2014-07-21 13:19:38 -07007020 }
7021
Amith Yamasani6e118872016-02-19 12:53:51 -08007022 @Override
chen xuf7e9fe82019-05-09 19:31:02 -07007023 public List<String> getPackagesWithCarrierPrivileges(int phoneId) {
Nazanin1adf4562021-03-29 15:35:30 -07007024 enforceReadPrivilegedPermission("getPackagesWithCarrierPrivileges");
Nazanin Bakhshif782e562018-12-11 15:15:39 -08007025 PackageManager pm = mApp.getPackageManager();
Amith Yamasani6e118872016-02-19 12:53:51 -08007026 List<String> privilegedPackages = new ArrayList<>();
7027 List<PackageInfo> packages = null;
Muralidhar Reddy472c2ae2021-09-29 19:38:40 +00007028 UiccPort port = UiccController.getInstance().getUiccPort(phoneId);
chen xuf7e9fe82019-05-09 19:31:02 -07007029 // has UICC in that slot.
Muralidhar Reddy472c2ae2021-09-29 19:38:40 +00007030 if (port != null) {
7031 if (port.hasCarrierPrivilegeRules()) {
Amith Yamasani6e118872016-02-19 12:53:51 -08007032 if (packages == null) {
7033 // Only check packages in user 0 for now
7034 packages = pm.getInstalledPackagesAsUser(
Nazanin Bakhshi5d0636e2019-08-19 16:29:37 -07007035 PackageManager.MATCH_DISABLED_COMPONENTS
7036 | PackageManager.MATCH_DISABLED_UNTIL_USED_COMPONENTS
Cheonho Park17089c62019-08-01 15:23:12 +09007037 | PackageManager.GET_SIGNING_CERTIFICATES,
Amit Mahajanb8f13202020-01-27 18:16:07 -08007038 UserHandle.SYSTEM.getIdentifier());
Amith Yamasani6e118872016-02-19 12:53:51 -08007039 }
7040 for (int p = packages.size() - 1; p >= 0; p--) {
7041 PackageInfo pkgInfo = packages.get(p);
7042 if (pkgInfo != null && pkgInfo.packageName != null
Sooraj Sasindran97bce6f2021-09-28 21:37:29 +00007043 && getCarrierPrivilegeStatusFromCarrierConfigRules(
7044 port.getCarrierPrivilegeStatus(pkgInfo),
7045 getPhone(phoneId), pkgInfo.packageName)
chen xuf7e9fe82019-05-09 19:31:02 -07007046 == TelephonyManager.CARRIER_PRIVILEGE_STATUS_HAS_ACCESS) {
Amith Yamasani6e118872016-02-19 12:53:51 -08007047 privilegedPackages.add(pkgInfo.packageName);
7048 }
7049 }
7050 }
7051 }
7052 return privilegedPackages;
7053 }
7054
chen xuf7e9fe82019-05-09 19:31:02 -07007055 @Override
7056 public List<String> getPackagesWithCarrierPrivilegesForAllPhones() {
Shuo Qian067a06d2019-12-03 23:40:18 +00007057 enforceReadPrivilegedPermission("getPackagesWithCarrierPrivilegesForAllPhones");
7058
7059 final long identity = Binder.clearCallingIdentity();
7060
chen xuf7e9fe82019-05-09 19:31:02 -07007061 List<String> privilegedPackages = new ArrayList<>();
Shuo Qian067a06d2019-12-03 23:40:18 +00007062 try {
7063 for (int i = 0; i < TelephonyManager.getDefault().getPhoneCount(); i++) {
7064 privilegedPackages.addAll(getPackagesWithCarrierPrivileges(i));
7065 }
7066 } finally {
7067 Binder.restoreCallingIdentity(identity);
chen xuf7e9fe82019-05-09 19:31:02 -07007068 }
7069 return privilegedPackages;
7070 }
7071
Wink Savilleb564aae2014-10-23 10:18:09 -07007072 private String getIccId(int subId) {
Sanket Padawe356d7632015-06-22 14:03:32 -07007073 final Phone phone = getPhone(subId);
Muralidhar Reddy472c2ae2021-09-29 19:38:40 +00007074 UiccPort port = phone == null ? null : phone.getUiccPort();
7075 if (port == null) {
Derek Tan97ebb422014-09-05 16:55:38 -07007076 return null;
7077 }
Muralidhar Reddy472c2ae2021-09-29 19:38:40 +00007078 String iccId = port.getIccId();
Derek Tan97ebb422014-09-05 16:55:38 -07007079 if (TextUtils.isEmpty(iccId)) {
Derek Tan97ebb422014-09-05 16:55:38 -07007080 return null;
7081 }
7082 return iccId;
7083 }
7084
Shishir Agrawaleb6439a2014-07-21 13:19:38 -07007085 @Override
Shuo Qiane4e11672020-12-15 22:15:33 -08007086 public void setCallComposerStatus(int subId, int status) {
7087 enforceModifyPermission();
7088
7089 final long identity = Binder.clearCallingIdentity();
7090 try {
7091 Phone phone = getPhone(subId);
7092 if (phone != null) {
7093 Phone defaultPhone = phone.getImsPhone();
7094 if (defaultPhone != null && defaultPhone.getPhoneType() == PHONE_TYPE_IMS) {
7095 ImsPhone imsPhone = (ImsPhone) defaultPhone;
7096 imsPhone.setCallComposerStatus(status);
Shuo Qian284ae752020-12-22 19:10:14 -08007097 ImsManager.getInstance(mApp, getSlotIndexOrException(subId))
7098 .updateImsServiceConfig();
Shuo Qiane4e11672020-12-15 22:15:33 -08007099 }
7100 }
Shuo Qian284ae752020-12-22 19:10:14 -08007101 } catch (ImsException e) {
7102 throw new ServiceSpecificException(e.getCode());
7103 } finally {
Shuo Qiane4e11672020-12-15 22:15:33 -08007104 Binder.restoreCallingIdentity(identity);
7105 }
7106 }
7107
7108 @Override
7109 public int getCallComposerStatus(int subId) {
7110 enforceReadPrivilegedPermission("getCallComposerStatus");
7111
7112 final long identity = Binder.clearCallingIdentity();
7113 try {
7114 Phone phone = getPhone(subId);
7115 if (phone != null) {
7116 Phone defaultPhone = phone.getImsPhone();
7117 if (defaultPhone != null && defaultPhone.getPhoneType() == PHONE_TYPE_IMS) {
7118 ImsPhone imsPhone = (ImsPhone) defaultPhone;
7119 return imsPhone.getCallComposerStatus();
7120 }
7121 }
7122 } finally {
7123 Binder.restoreCallingIdentity(identity);
7124 }
7125 return TelephonyManager.CALL_COMPOSER_STATUS_OFF;
7126 }
7127
7128 @Override
Jeff Sharkey85190e62014-12-05 09:40:12 -08007129 public boolean setLine1NumberForDisplayForSubscriber(int subId, String alphaTag,
7130 String number) {
Shuo Qian2c0ae432019-12-05 11:40:37 -08007131 TelephonyPermissions.enforceCallingOrSelfCarrierPrivilege(mApp,
Jeff Davidson7e17e312018-02-13 18:17:36 -08007132 subId, "setLine1NumberForDisplayForSubscriber");
Derek Tan97ebb422014-09-05 16:55:38 -07007133
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007134 final long identity = Binder.clearCallingIdentity();
7135 try {
7136 final String iccId = getIccId(subId);
7137 final Phone phone = getPhone(subId);
7138 if (phone == null) {
7139 return false;
7140 }
7141 final String subscriberId = phone.getSubscriberId();
7142
7143 if (DBG_MERGE) {
Amit Mahajanb8f13202020-01-27 18:16:07 -08007144 Rlog.d(LOG_TAG, "Setting line number for ICC=" + iccId + ", subscriberId="
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007145 + subscriberId + " to " + number);
7146 }
7147
7148 if (TextUtils.isEmpty(iccId)) {
7149 return false;
7150 }
7151
7152 final SharedPreferences.Editor editor = mTelephonySharedPreferences.edit();
7153
7154 final String alphaTagPrefKey = PREF_CARRIERS_ALPHATAG_PREFIX + iccId;
7155 if (alphaTag == null) {
7156 editor.remove(alphaTagPrefKey);
7157 } else {
7158 editor.putString(alphaTagPrefKey, alphaTag);
7159 }
7160
7161 // Record both the line number and IMSI for this ICCID, since we need to
7162 // track all merged IMSIs based on line number
7163 final String numberPrefKey = PREF_CARRIERS_NUMBER_PREFIX + iccId;
7164 final String subscriberPrefKey = PREF_CARRIERS_SUBSCRIBER_PREFIX + iccId;
7165 if (number == null) {
7166 editor.remove(numberPrefKey);
7167 editor.remove(subscriberPrefKey);
7168 } else {
7169 editor.putString(numberPrefKey, number);
7170 editor.putString(subscriberPrefKey, subscriberId);
7171 }
7172
7173 editor.commit();
7174 return true;
7175 } finally {
7176 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07007177 }
Derek Tan7226c842014-07-02 17:42:23 -07007178 }
7179
7180 @Override
Philip P. Moltmann700a9592019-10-03 11:53:50 -07007181 public String getLine1NumberForDisplay(int subId, String callingPackage,
7182 String callingFeatureId) {
Makoto Onukifee69342015-06-29 14:44:50 -07007183 // This is open to apps with WRITE_SMS.
Jeff Davidson7e17e312018-02-13 18:17:36 -08007184 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneNumber(
Philip P. Moltmann700a9592019-10-03 11:53:50 -07007185 mApp, subId, callingPackage, callingFeatureId, "getLine1NumberForDisplay")) {
Amit Mahajan9cf11512015-11-09 11:40:48 -08007186 if (DBG_MERGE) log("getLine1NumberForDisplay returning null due to permission");
Svet Ganovb320e182015-04-16 12:30:10 -07007187 return null;
7188 }
Derek Tan97ebb422014-09-05 16:55:38 -07007189
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007190 final long identity = Binder.clearCallingIdentity();
7191 try {
7192 String iccId = getIccId(subId);
7193 if (iccId != null) {
7194 String numberPrefKey = PREF_CARRIERS_NUMBER_PREFIX + iccId;
7195 if (DBG_MERGE) {
7196 log("getLine1NumberForDisplay returning "
7197 + mTelephonySharedPreferences.getString(numberPrefKey, null));
7198 }
7199 return mTelephonySharedPreferences.getString(numberPrefKey, null);
Amit Mahajan9cf11512015-11-09 11:40:48 -08007200 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007201 if (DBG_MERGE) log("getLine1NumberForDisplay returning null as iccId is null");
7202 return null;
7203 } finally {
7204 Binder.restoreCallingIdentity(identity);
Derek Tan7226c842014-07-02 17:42:23 -07007205 }
Derek Tan7226c842014-07-02 17:42:23 -07007206 }
7207
7208 @Override
Philip P. Moltmann700a9592019-10-03 11:53:50 -07007209 public String getLine1AlphaTagForDisplay(int subId, String callingPackage,
7210 String callingFeatureId) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08007211 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
Philip P. Moltmann700a9592019-10-03 11:53:50 -07007212 mApp, subId, callingPackage, callingFeatureId, "getLine1AlphaTagForDisplay")) {
Svet Ganovb320e182015-04-16 12:30:10 -07007213 return null;
7214 }
Derek Tan97ebb422014-09-05 16:55:38 -07007215
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007216 final long identity = Binder.clearCallingIdentity();
7217 try {
7218 String iccId = getIccId(subId);
7219 if (iccId != null) {
7220 String alphaTagPrefKey = PREF_CARRIERS_ALPHATAG_PREFIX + iccId;
7221 return mTelephonySharedPreferences.getString(alphaTagPrefKey, null);
7222 }
7223 return null;
7224 } finally {
7225 Binder.restoreCallingIdentity(identity);
Derek Tan7226c842014-07-02 17:42:23 -07007226 }
Derek Tan7226c842014-07-02 17:42:23 -07007227 }
Shishir Agrawalb1ebf8c2014-07-17 16:32:41 -07007228
7229 @Override
Philip P. Moltmann700a9592019-10-03 11:53:50 -07007230 public String[] getMergedSubscriberIds(int subId, String callingPackage,
7231 String callingFeatureId) {
Jeff Davidson913390f2018-02-23 17:11:49 -08007232 // This API isn't public, so no need to provide a valid subscription ID - we're not worried
7233 // about carrier-privileged callers not having access.
Jeff Davidson7e17e312018-02-13 18:17:36 -08007234 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
Jeff Davidson913390f2018-02-23 17:11:49 -08007235 mApp, SubscriptionManager.INVALID_SUBSCRIPTION_ID, callingPackage,
Philip P. Moltmann700a9592019-10-03 11:53:50 -07007236 callingFeatureId, "getMergedSubscriberIds")) {
Fyodor Kupolov8e53b0b2015-06-17 13:17:50 -07007237 return null;
7238 }
Jeff Sharkey85190e62014-12-05 09:40:12 -08007239
Jordan Liub49b04b2019-05-06 14:45:15 -07007240 // Clear calling identity, when calling TelephonyManager, because callerUid must be
7241 // the process, where TelephonyManager was instantiated.
7242 // Otherwise AppOps check will fail.
Fyodor Kupolov8e53b0b2015-06-17 13:17:50 -07007243 final long identity = Binder.clearCallingIdentity();
7244 try {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08007245 final Context context = mApp;
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007246 final TelephonyManager tele = TelephonyManager.from(context);
7247 final SubscriptionManager sub = SubscriptionManager.from(context);
7248
7249 // Figure out what subscribers are currently active
7250 final ArraySet<String> activeSubscriberIds = new ArraySet<>();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007251
Jordan Liub49b04b2019-05-06 14:45:15 -07007252 // Only consider subs which match the current subId
7253 // This logic can be simplified. See b/131189269 for progress.
7254 if (isActiveSubscription(subId)) {
Fyodor Kupolov8e53b0b2015-06-17 13:17:50 -07007255 activeSubscriberIds.add(tele.getSubscriberId(subId));
7256 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007257
7258 // First pass, find a number override for an active subscriber
7259 String mergeNumber = null;
7260 final Map<String, ?> prefs = mTelephonySharedPreferences.getAll();
7261 for (String key : prefs.keySet()) {
7262 if (key.startsWith(PREF_CARRIERS_SUBSCRIBER_PREFIX)) {
7263 final String subscriberId = (String) prefs.get(key);
7264 if (activeSubscriberIds.contains(subscriberId)) {
7265 final String iccId = key.substring(
7266 PREF_CARRIERS_SUBSCRIBER_PREFIX.length());
7267 final String numberKey = PREF_CARRIERS_NUMBER_PREFIX + iccId;
7268 mergeNumber = (String) prefs.get(numberKey);
7269 if (DBG_MERGE) {
Amit Mahajanb8f13202020-01-27 18:16:07 -08007270 Rlog.d(LOG_TAG, "Found line number " + mergeNumber
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007271 + " for active subscriber " + subscriberId);
7272 }
7273 if (!TextUtils.isEmpty(mergeNumber)) {
7274 break;
7275 }
7276 }
7277 }
7278 }
7279
7280 // Shortcut when no active merged subscribers
7281 if (TextUtils.isEmpty(mergeNumber)) {
7282 return null;
7283 }
7284
7285 // Second pass, find all subscribers under that line override
7286 final ArraySet<String> result = new ArraySet<>();
7287 for (String key : prefs.keySet()) {
7288 if (key.startsWith(PREF_CARRIERS_NUMBER_PREFIX)) {
7289 final String number = (String) prefs.get(key);
7290 if (mergeNumber.equals(number)) {
7291 final String iccId = key.substring(PREF_CARRIERS_NUMBER_PREFIX.length());
7292 final String subscriberKey = PREF_CARRIERS_SUBSCRIBER_PREFIX + iccId;
7293 final String subscriberId = (String) prefs.get(subscriberKey);
7294 if (!TextUtils.isEmpty(subscriberId)) {
7295 result.add(subscriberId);
7296 }
7297 }
7298 }
7299 }
7300
7301 final String[] resultArray = result.toArray(new String[result.size()]);
7302 Arrays.sort(resultArray);
7303 if (DBG_MERGE) {
Amit Mahajanb8f13202020-01-27 18:16:07 -08007304 Rlog.d(LOG_TAG,
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007305 "Found subscribers " + Arrays.toString(resultArray) + " after merge");
7306 }
7307 return resultArray;
Fyodor Kupolov8e53b0b2015-06-17 13:17:50 -07007308 } finally {
7309 Binder.restoreCallingIdentity(identity);
Jeff Sharkey85190e62014-12-05 09:40:12 -08007310 }
Jeff Sharkey85190e62014-12-05 09:40:12 -08007311 }
7312
7313 @Override
zoey chen38003472019-12-13 17:16:31 +08007314 public String[] getMergedImsisFromGroup(int subId, String callingPackage) {
7315 enforceReadPrivilegedPermission("getMergedImsisFromGroup");
Malcolm Chen6ca97372019-07-01 16:28:21 -07007316
7317 final long identity = Binder.clearCallingIdentity();
7318 try {
7319 final TelephonyManager telephonyManager = mApp.getSystemService(
7320 TelephonyManager.class);
7321 String subscriberId = telephonyManager.getSubscriberId(subId);
7322 if (subscriberId == null) {
7323 if (DBG) {
zoey chen38003472019-12-13 17:16:31 +08007324 log("getMergedImsisFromGroup can't find subscriberId for subId "
Malcolm Chen6ca97372019-07-01 16:28:21 -07007325 + subId);
7326 }
7327 return null;
7328 }
7329
7330 final SubscriptionInfo info = SubscriptionController.getInstance()
7331 .getSubscriptionInfo(subId);
7332 final ParcelUuid groupUuid = info.getGroupUuid();
7333 // If it doesn't belong to any group, return just subscriberId of itself.
7334 if (groupUuid == null) {
7335 return new String[]{subscriberId};
7336 }
7337
7338 // Get all subscriberIds from the group.
7339 final List<String> mergedSubscriberIds = new ArrayList<>();
7340 final List<SubscriptionInfo> groupInfos = SubscriptionController.getInstance()
Philip P. Moltmann700a9592019-10-03 11:53:50 -07007341 .getSubscriptionsInGroup(groupUuid, mApp.getOpPackageName(),
Philip P. Moltmann8d34f0c2020-03-05 16:24:02 -08007342 mApp.getAttributionTag());
Malcolm Chen6ca97372019-07-01 16:28:21 -07007343 for (SubscriptionInfo subInfo : groupInfos) {
7344 subscriberId = telephonyManager.getSubscriberId(subInfo.getSubscriptionId());
7345 if (subscriberId != null) {
7346 mergedSubscriberIds.add(subscriberId);
7347 }
7348 }
7349
7350 return mergedSubscriberIds.toArray(new String[mergedSubscriberIds.size()]);
7351 } finally {
7352 Binder.restoreCallingIdentity(identity);
7353
7354 }
7355 }
7356
7357 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08007358 public boolean setOperatorBrandOverride(int subId, String brand) {
Shuo Qian2c0ae432019-12-05 11:40:37 -08007359 TelephonyPermissions.enforceCallingOrSelfCarrierPrivilege(mApp,
Jeff Davidson7e17e312018-02-13 18:17:36 -08007360 subId, "setOperatorBrandOverride");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007361
7362 final long identity = Binder.clearCallingIdentity();
7363 try {
7364 final Phone phone = getPhone(subId);
7365 return phone == null ? false : phone.setOperatorBrandOverride(brand);
7366 } finally {
7367 Binder.restoreCallingIdentity(identity);
7368 }
Shishir Agrawalb1ebf8c2014-07-17 16:32:41 -07007369 }
Steven Liu4bf01bc2014-07-17 11:05:29 -05007370
7371 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08007372 public boolean setRoamingOverride(int subId, List<String> gsmRoamingList,
Shishir Agrawal621a47c2014-12-01 10:25:09 -08007373 List<String> gsmNonRoamingList, List<String> cdmaRoamingList,
7374 List<String> cdmaNonRoamingList) {
Shuo Qian2c0ae432019-12-05 11:40:37 -08007375 TelephonyPermissions.enforceCallingOrSelfCarrierPrivilege(
7376 mApp, subId, "setRoamingOverride");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007377
7378 final long identity = Binder.clearCallingIdentity();
7379 try {
7380 final Phone phone = getPhone(subId);
7381 if (phone == null) {
7382 return false;
7383 }
7384 return phone.setRoamingOverride(gsmRoamingList, gsmNonRoamingList, cdmaRoamingList,
7385 cdmaNonRoamingList);
7386 } finally {
7387 Binder.restoreCallingIdentity(identity);
Shishir Agrawalc04d9752016-02-19 10:41:00 -08007388 }
Shishir Agrawal621a47c2014-12-01 10:25:09 -08007389 }
7390
7391 @Override
Shuo Qian850e4d6a2018-04-25 21:02:08 +00007392 @Deprecated
7393 public int invokeOemRilRequestRaw(byte[] oemReq, byte[] oemResp) {
7394 enforceModifyPermission();
7395
7396 int returnValue = 0;
7397 try {
vagdeviaf9a5b92018-08-15 16:01:53 -07007398 AsyncResult result = (AsyncResult) sendRequest(CMD_INVOKE_OEM_RIL_REQUEST_RAW, oemReq);
Shuo Qian850e4d6a2018-04-25 21:02:08 +00007399 if(result.exception == null) {
7400 if (result.result != null) {
7401 byte[] responseData = (byte[])(result.result);
7402 if(responseData.length > oemResp.length) {
7403 Log.w(LOG_TAG, "Buffer to copy response too small: Response length is " +
7404 responseData.length + "bytes. Buffer Size is " +
7405 oemResp.length + "bytes.");
7406 }
7407 System.arraycopy(responseData, 0, oemResp, 0, responseData.length);
7408 returnValue = responseData.length;
7409 }
7410 } else {
7411 CommandException ex = (CommandException) result.exception;
7412 returnValue = ex.getCommandError().ordinal();
7413 if(returnValue > 0) returnValue *= -1;
7414 }
7415 } catch (RuntimeException e) {
7416 Log.w(LOG_TAG, "sendOemRilRequestRaw: Runtime Exception");
7417 returnValue = (CommandException.Error.GENERIC_FAILURE.ordinal());
7418 if(returnValue > 0) returnValue *= -1;
7419 }
7420
7421 return returnValue;
7422 }
7423
7424 @Override
Robert Greenwalt36b23af2015-07-06 17:59:14 -07007425 public int getRadioAccessFamily(int phoneId, String callingPackage) {
Jeff Davidson913390f2018-02-23 17:11:49 -08007426 Phone phone = PhoneFactory.getPhone(phoneId);
Shuo Qiandee53402020-05-29 14:08:15 -07007427 try {
7428 TelephonyPermissions
Nathan Harold62c68512021-04-06 11:26:02 -07007429 .enforceCallingOrSelfReadPrivilegedPhoneStatePermissionOrCarrierPrivilege(
Shuo Qiandee53402020-05-29 14:08:15 -07007430 mApp, phone.getSubId(), "getRadioAccessFamily");
7431 } catch (SecurityException e) {
7432 EventLog.writeEvent(0x534e4554, "150857259", -1, "Missing Permission");
7433 throw e;
7434 }
chen xub97461a2018-10-26 14:17:57 -07007435 int raf = RadioAccessFamily.RAF_UNKNOWN;
Jeff Davidson913390f2018-02-23 17:11:49 -08007436 if (phone == null) {
chen xub97461a2018-10-26 14:17:57 -07007437 return raf;
Jeff Davidson913390f2018-02-23 17:11:49 -08007438 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007439 final long identity = Binder.clearCallingIdentity();
7440 try {
chen xub97461a2018-10-26 14:17:57 -07007441 TelephonyPermissions
Nathan Harold62c68512021-04-06 11:26:02 -07007442 .enforceCallingOrSelfReadPrivilegedPhoneStatePermissionOrCarrierPrivilege(
chen xub97461a2018-10-26 14:17:57 -07007443 mApp, phone.getSubId(), "getRadioAccessFamily");
7444 raf = ProxyController.getInstance().getRadioAccessFamily(phoneId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007445 } finally {
7446 Binder.restoreCallingIdentity(identity);
7447 }
chen xub97461a2018-10-26 14:17:57 -07007448 return raf;
Wink Saville5d475dd2014-10-17 15:00:58 -07007449 }
Andrew Leedf14ead2014-10-17 14:22:52 -07007450
7451 @Override
Hall Liu82694d52020-12-11 18:22:04 -08007452 public void uploadCallComposerPicture(int subscriptionId, String callingPackage,
Hall Liue31bac62020-12-23 19:16:10 -08007453 String contentType, ParcelFileDescriptor fd, ResultReceiver callback) {
Hall Liu82694d52020-12-11 18:22:04 -08007454 try {
7455 if (!Objects.equals(mApp.getPackageManager().getPackageUid(callingPackage, 0),
7456 Binder.getCallingUid())) {
Tyler Gunnb87925a2021-06-10 14:54:27 -07007457 throw new SecurityException("Invalid package:" + callingPackage);
Hall Liu82694d52020-12-11 18:22:04 -08007458 }
7459 } catch (PackageManager.NameNotFoundException e) {
Tyler Gunnb87925a2021-06-10 14:54:27 -07007460 throw new SecurityException("Invalid package:" + callingPackage);
Hall Liu82694d52020-12-11 18:22:04 -08007461 }
7462 RoleManager rm = mApp.getSystemService(RoleManager.class);
7463 List<String> dialerRoleHolders = rm.getRoleHolders(RoleManager.ROLE_DIALER);
7464 if (!dialerRoleHolders.contains(callingPackage)) {
7465 throw new SecurityException("App must be the dialer role holder to"
7466 + " upload a call composer pic");
7467 }
7468
7469 Executors.newSingleThreadExecutor().execute(() -> {
7470 ByteArrayOutputStream output = new ByteArrayOutputStream(
7471 (int) TelephonyManager.getMaximumCallComposerPictureSize());
7472 InputStream input = new ParcelFileDescriptor.AutoCloseInputStream(fd);
7473 boolean readUntilEnd = false;
7474 int totalBytesRead = 0;
7475 byte[] buffer = new byte[16 * 1024];
7476 while (true) {
7477 int numRead;
7478 try {
7479 numRead = input.read(buffer);
7480 } catch (IOException e) {
7481 try {
7482 fd.checkError();
7483 callback.send(TelephonyManager.CallComposerException.ERROR_INPUT_CLOSED,
7484 null);
7485 } catch (IOException e1) {
7486 // This means that the other side closed explicitly with an error. If this
7487 // happens, log and ignore.
7488 loge("Remote end of call composer picture pipe closed: " + e1);
7489 }
7490 break;
7491 }
7492 if (numRead == -1) {
7493 readUntilEnd = true;
7494 break;
7495 }
7496 totalBytesRead += numRead;
7497 if (totalBytesRead > TelephonyManager.getMaximumCallComposerPictureSize()) {
7498 loge("Too many bytes read for call composer picture: " + totalBytesRead);
7499 try {
7500 input.close();
7501 } catch (IOException e) {
7502 // ignore
7503 }
7504 break;
7505 }
7506 output.write(buffer, 0, numRead);
7507 }
7508 // Generally, the remote end will close the file descriptors. The only case where we
7509 // close is above, where the picture size is too big.
7510
7511 try {
7512 fd.checkError();
7513 } catch (IOException e) {
7514 loge("Remote end for call composer closed with an error: " + e);
7515 return;
7516 }
7517
Hall Liuaa4211e2021-01-20 15:43:39 -08007518 if (!readUntilEnd) {
7519 loge("Did not finish reading entire image; aborting");
7520 return;
7521 }
Hall Liu82694d52020-12-11 18:22:04 -08007522
Hall Liuaa4211e2021-01-20 15:43:39 -08007523 ImageData imageData = new ImageData(output.toByteArray(), contentType, null);
7524 CallComposerPictureManager.getInstance(mApp, subscriptionId).handleUploadToServer(
7525 new CallComposerPictureTransfer.Factory() {},
7526 imageData,
7527 (result) -> {
7528 if (result.first != null) {
7529 ParcelUuid parcelUuid = new ParcelUuid(result.first);
7530 Bundle outputResult = new Bundle();
7531 outputResult.putParcelable(
7532 TelephonyManager.KEY_CALL_COMPOSER_PICTURE_HANDLE, parcelUuid);
7533 callback.send(TelephonyManager.CallComposerException.SUCCESS,
7534 outputResult);
7535 } else {
7536 callback.send(result.second, null);
7537 }
7538 }
7539 );
Hall Liu82694d52020-12-11 18:22:04 -08007540 });
7541 }
7542
7543 @Override
Andrew Leedf14ead2014-10-17 14:22:52 -07007544 public void enableVideoCalling(boolean enable) {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08007545 final Phone defaultPhone = getDefaultPhone();
Andrew Leedf14ead2014-10-17 14:22:52 -07007546 enforceModifyPermission();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007547
7548 final long identity = Binder.clearCallingIdentity();
7549 try {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08007550 ImsManager.getInstance(defaultPhone.getContext(),
7551 defaultPhone.getPhoneId()).setVtSetting(enable);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007552 } finally {
7553 Binder.restoreCallingIdentity(identity);
7554 }
Andrew Leedf14ead2014-10-17 14:22:52 -07007555 }
7556
7557 @Override
Philip P. Moltmann700a9592019-10-03 11:53:50 -07007558 public boolean isVideoCallingEnabled(String callingPackage, String callingFeatureId) {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08007559 final Phone defaultPhone = getDefaultPhone();
Philip P. Moltmann700a9592019-10-03 11:53:50 -07007560 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(mApp, defaultPhone.getSubId(),
7561 callingPackage, callingFeatureId, "isVideoCallingEnabled")) {
Amit Mahajan578e53d2018-03-20 16:18:38 +00007562 return false;
7563 }
Svet Ganovb320e182015-04-16 12:30:10 -07007564
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007565 final long identity = Binder.clearCallingIdentity();
7566 try {
7567 // Check the user preference and the system-level IMS setting. Even if the user has
7568 // enabled video calling, if IMS is disabled we aren't able to support video calling.
7569 // In the long run, we may instead need to check if there exists a connection service
7570 // which can support video calling.
7571 ImsManager imsManager =
Nazanin Bakhshif782e562018-12-11 15:15:39 -08007572 ImsManager.getInstance(defaultPhone.getContext(), defaultPhone.getPhoneId());
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007573 return imsManager.isVtEnabledByPlatform()
7574 && imsManager.isEnhanced4gLteModeSettingEnabledByUser()
7575 && imsManager.isVtEnabledByUser();
7576 } finally {
7577 Binder.restoreCallingIdentity(identity);
7578 }
Andrew Leedf14ead2014-10-17 14:22:52 -07007579 }
Libin.Tang@motorola.comafe82642014-12-18 13:27:53 -06007580
Andrew Leea1239f22015-03-02 17:44:07 -08007581 @Override
Philip P. Moltmann700a9592019-10-03 11:53:50 -07007582 public boolean canChangeDtmfToneLength(int subId, String callingPackage,
7583 String callingFeatureId) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007584 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
Philip P. Moltmann700a9592019-10-03 11:53:50 -07007585 mApp, subId, callingPackage, callingFeatureId,
7586 "isVideoCallingEnabled")) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007587 return false;
7588 }
7589
7590 final long identity = Binder.clearCallingIdentity();
7591 try {
7592 CarrierConfigManager configManager =
7593 (CarrierConfigManager) mApp.getSystemService(Context.CARRIER_CONFIG_SERVICE);
Nazanin Bakhshif782e562018-12-11 15:15:39 -08007594 return configManager.getConfigForSubId(subId)
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007595 .getBoolean(CarrierConfigManager.KEY_DTMF_TYPE_ENABLED_BOOL);
7596 } finally {
7597 Binder.restoreCallingIdentity(identity);
7598 }
Andrew Leea1239f22015-03-02 17:44:07 -08007599 }
7600
7601 @Override
Philip P. Moltmann700a9592019-10-03 11:53:50 -07007602 public boolean isWorldPhone(int subId, String callingPackage, String callingFeatureId) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007603 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
Philip P. Moltmann700a9592019-10-03 11:53:50 -07007604 mApp, subId, callingPackage, callingFeatureId, "isVideoCallingEnabled")) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007605 return false;
7606 }
7607
7608 final long identity = Binder.clearCallingIdentity();
7609 try {
7610 CarrierConfigManager configManager =
7611 (CarrierConfigManager) mApp.getSystemService(Context.CARRIER_CONFIG_SERVICE);
Nazanin Bakhshif782e562018-12-11 15:15:39 -08007612 return configManager.getConfigForSubId(subId)
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007613 .getBoolean(CarrierConfigManager.KEY_WORLD_PHONE_BOOL);
7614 } finally {
7615 Binder.restoreCallingIdentity(identity);
7616 }
Andrew Leea1239f22015-03-02 17:44:07 -08007617 }
7618
Andrew Lee9431b832015-03-09 18:46:45 -07007619 @Override
7620 public boolean isTtyModeSupported() {
Tyler Gunn5ddfdc92019-10-31 13:08:23 -07007621 TelecomManager telecomManager = mApp.getSystemService(TelecomManager.class);
Wooki Wu1f82f7a2016-02-15 15:59:58 +08007622 return telecomManager.isTtySupported();
Andrew Lee9431b832015-03-09 18:46:45 -07007623 }
7624
7625 @Override
7626 public boolean isHearingAidCompatibilitySupported() {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007627 final long identity = Binder.clearCallingIdentity();
7628 try {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08007629 return mApp.getResources().getBoolean(R.bool.hac_enabled);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007630 } finally {
7631 Binder.restoreCallingIdentity(identity);
7632 }
Andrew Lee9431b832015-03-09 18:46:45 -07007633 }
7634
Hall Liuf6668912018-10-31 17:05:23 -07007635 /**
7636 * Determines whether the device currently supports RTT (Real-time text). Based both on carrier
7637 * support for the feature and device firmware support.
7638 *
7639 * @return {@code true} if the device and carrier both support RTT, {@code false} otherwise.
7640 */
7641 @Override
7642 public boolean isRttSupported(int subscriptionId) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007643 final long identity = Binder.clearCallingIdentity();
Nazanin Bakhshif782e562018-12-11 15:15:39 -08007644 final Phone phone = getPhone(subscriptionId);
7645 if (phone == null) {
7646 loge("isRttSupported: no Phone found. Invalid subId:" + subscriptionId);
7647 return false;
7648 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007649 try {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08007650 boolean isCarrierSupported = mApp.getCarrierConfigForSubId(subscriptionId).getBoolean(
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007651 CarrierConfigManager.KEY_RTT_SUPPORTED_BOOL);
7652 boolean isDeviceSupported =
Nazanin Bakhshif782e562018-12-11 15:15:39 -08007653 phone.getContext().getResources().getBoolean(R.bool.config_support_rtt);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007654 return isCarrierSupported && isDeviceSupported;
7655 } finally {
7656 Binder.restoreCallingIdentity(identity);
7657 }
Hall Liu98187582018-01-22 19:15:32 -08007658 }
7659
Hall Liuf6668912018-10-31 17:05:23 -07007660 /**
Hall Liuf2daa022019-07-23 18:39:00 -07007661 * Determines whether the user has turned on RTT. If the carrier wants to ignore the user-set
7662 * RTT setting, will return true if the device and carrier both support RTT.
7663 * Otherwise. only returns true if the device and carrier both also support RTT.
Hall Liuf6668912018-10-31 17:05:23 -07007664 */
7665 public boolean isRttEnabled(int subscriptionId) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007666 final long identity = Binder.clearCallingIdentity();
7667 try {
Hall Liu5bab75c2019-12-11 23:58:20 +00007668 boolean isRttSupported = isRttSupported(subscriptionId);
7669 boolean isUserRttSettingOn = Settings.Secure.getInt(
7670 mApp.getContentResolver(), Settings.Secure.RTT_CALLING_MODE, 0) != 0;
7671 boolean shouldIgnoreUserRttSetting = mApp.getCarrierConfigForSubId(subscriptionId)
7672 .getBoolean(CarrierConfigManager.KEY_IGNORE_RTT_MODE_SETTING_BOOL);
7673 return isRttSupported && (isUserRttSettingOn || shouldIgnoreUserRttSetting);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007674 } finally {
7675 Binder.restoreCallingIdentity(identity);
7676 }
Hall Liu3ad5f012018-04-06 16:23:39 -07007677 }
7678
Philip P. Moltmann700a9592019-10-03 11:53:50 -07007679 @Deprecated
7680 @Override
7681 public String getDeviceId(String callingPackage) {
7682 return getDeviceIdWithFeature(callingPackage, null);
7683 }
7684
Sanket Padawe7310cc72015-01-14 09:53:20 -08007685 /**
7686 * Returns the unique device ID of phone, for example, the IMEI for
7687 * GSM and the MEID for CDMA phones. Return null if device ID is not available.
7688 *
7689 * <p>Requires Permission:
7690 * {@link android.Manifest.permission#READ_PHONE_STATE READ_PHONE_STATE}
7691 */
7692 @Override
Philip P. Moltmann700a9592019-10-03 11:53:50 -07007693 public String getDeviceIdWithFeature(String callingPackage, String callingFeatureId) {
Shuo Qian13d89152021-05-10 23:58:11 -07007694 try {
7695 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
7696 } catch (SecurityException se) {
7697 EventLog.writeEvent(0x534e4554, "186530889", Binder.getCallingUid());
7698 throw new SecurityException("Package " + callingPackage + " does not belong to "
7699 + Binder.getCallingUid());
7700 }
Sanket Padawe7310cc72015-01-14 09:53:20 -08007701 final Phone phone = PhoneFactory.getPhone(0);
Jeff Davidson913390f2018-02-23 17:11:49 -08007702 if (phone == null) {
Sanket Padawe7310cc72015-01-14 09:53:20 -08007703 return null;
7704 }
Jeff Davidson913390f2018-02-23 17:11:49 -08007705 int subId = phone.getSubId();
Michael Groover70af6dc2018-10-01 16:23:15 -07007706 if (!TelephonyPermissions.checkCallingOrSelfReadDeviceIdentifiers(mApp, subId,
Philip P. Moltmann700a9592019-10-03 11:53:50 -07007707 callingPackage, callingFeatureId, "getDeviceId")) {
Jeff Davidson913390f2018-02-23 17:11:49 -08007708 return null;
7709 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007710
7711 final long identity = Binder.clearCallingIdentity();
7712 try {
7713 return phone.getDeviceId();
7714 } finally {
7715 Binder.restoreCallingIdentity(identity);
7716 }
Sanket Padawe7310cc72015-01-14 09:53:20 -08007717 }
7718
Ping Sunc67b7c22016-03-02 19:16:45 +08007719 /**
7720 * {@hide}
7721 * Returns the IMS Registration Status on a particular subid
7722 *
7723 * @param subId
7724 */
Brad Ebinger1f2b5082018-02-08 16:11:32 -08007725 public boolean isImsRegistered(int subId) {
Ping Sunc67b7c22016-03-02 19:16:45 +08007726 Phone phone = getPhone(subId);
7727 if (phone != null) {
7728 return phone.isImsRegistered();
7729 } else {
7730 return false;
7731 }
7732 }
7733
Santos Cordon7a1885b2015-02-03 11:15:19 -08007734 @Override
Shuo Qian6e6137d2019-10-30 16:33:31 -07007735 public int getSubIdForPhoneAccountHandle(
Philip P. Moltmann700a9592019-10-03 11:53:50 -07007736 PhoneAccountHandle phoneAccountHandle, String callingPackage, String callingFeatureId) {
Shuo Qian6e6137d2019-10-30 16:33:31 -07007737 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(mApp, getDefaultSubscription(),
Philip P. Moltmann700a9592019-10-03 11:53:50 -07007738 callingPackage, callingFeatureId, "getSubIdForPhoneAccountHandle")) {
Shuo Qian6e6137d2019-10-30 16:33:31 -07007739 throw new SecurityException("Requires READ_PHONE_STATE permission.");
7740 }
7741 final long identity = Binder.clearCallingIdentity();
7742 try {
7743 return PhoneUtils.getSubIdForPhoneAccountHandle(phoneAccountHandle);
7744 } finally {
7745 Binder.restoreCallingIdentity(identity);
7746 }
7747 }
7748
7749 @Override
Tyler Gunnf70ed162019-04-03 15:28:53 -07007750 public @Nullable PhoneAccountHandle getPhoneAccountHandleForSubscriptionId(int subscriptionId) {
Alireza Forouzan4ac4f982021-03-16 22:18:52 -07007751 TelephonyPermissions
Nathan Harold62c68512021-04-06 11:26:02 -07007752 .enforceCallingOrSelfReadPrivilegedPhoneStatePermissionOrCarrierPrivilege(
Alireza Forouzan4ac4f982021-03-16 22:18:52 -07007753 mApp,
7754 subscriptionId,
7755 "getPhoneAccountHandleForSubscriptionId, " + "subscriptionId: " + subscriptionId);
Tyler Gunnf70ed162019-04-03 15:28:53 -07007756 final long identity = Binder.clearCallingIdentity();
7757 try {
7758 Phone phone = getPhone(subscriptionId);
7759 if (phone == null) {
7760 return null;
7761 }
7762 return PhoneUtils.makePstnPhoneAccountHandle(phone);
7763 } finally {
7764 Binder.restoreCallingIdentity(identity);
7765 }
7766 }
7767
Brad Ebinger1f2b5082018-02-08 16:11:32 -08007768 /**
7769 * @return the VoWiFi calling availability.
Nathan Haroldc55097a2015-03-11 18:14:50 -07007770 */
Brad Ebinger1f2b5082018-02-08 16:11:32 -08007771 public boolean isWifiCallingAvailable(int subId) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007772 final long identity = Binder.clearCallingIdentity();
7773 try {
7774 Phone phone = getPhone(subId);
7775 if (phone != null) {
7776 return phone.isWifiCallingEnabled();
7777 } else {
7778 return false;
7779 }
7780 } finally {
7781 Binder.restoreCallingIdentity(identity);
Brad Ebinger1f2b5082018-02-08 16:11:32 -08007782 }
Nathan Haroldc55097a2015-03-11 18:14:50 -07007783 }
7784
Brad Ebinger1f2b5082018-02-08 16:11:32 -08007785 /**
Brad Ebinger1f2b5082018-02-08 16:11:32 -08007786 * @return the VT calling availability.
Etan Cohen3b7a1bc2015-05-28 15:57:13 -07007787 */
Brad Ebinger1f2b5082018-02-08 16:11:32 -08007788 public boolean isVideoTelephonyAvailable(int subId) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007789 final long identity = Binder.clearCallingIdentity();
7790 try {
7791 Phone phone = getPhone(subId);
7792 if (phone != null) {
7793 return phone.isVideoEnabled();
7794 } else {
7795 return false;
7796 }
7797 } finally {
7798 Binder.restoreCallingIdentity(identity);
Brad Ebinger1f2b5082018-02-08 16:11:32 -08007799 }
7800 }
7801
7802 /**
7803 * @return the IMS registration technology for the MMTEL feature. Valid return values are
7804 * defined in {@link ImsRegistrationImplBase}.
7805 */
7806 public @ImsRegistrationImplBase.ImsRegistrationTech int getImsRegTechnologyForMmTel(int subId) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007807 final long identity = Binder.clearCallingIdentity();
7808 try {
7809 Phone phone = getPhone(subId);
7810 if (phone != null) {
7811 return phone.getImsRegistrationTech();
7812 } else {
7813 return ImsRegistrationImplBase.REGISTRATION_TECH_NONE;
7814 }
7815 } finally {
7816 Binder.restoreCallingIdentity(identity);
Brad Ebinger1f2b5082018-02-08 16:11:32 -08007817 }
Etan Cohen3b7a1bc2015-05-28 15:57:13 -07007818 }
7819
Stuart Scott8eef64f2015-04-08 15:13:54 -07007820 @Override
7821 public void factoryReset(int subId) {
paulhu5a773602019-08-23 19:17:33 +08007822 enforceSettingsPermission();
Stuart Scott981d8582015-04-21 14:09:50 -07007823 if (mUserManager.hasUserRestriction(UserManager.DISALLOW_NETWORK_RESET)) {
7824 return;
7825 }
Kai Shif70f46f2021-03-03 13:59:46 -08007826 Phone defaultPhone = getDefaultPhone();
7827 if (defaultPhone != null) {
7828 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
7829 mApp, getDefaultPhone().getSubId(), "factoryReset");
7830 }
Svet Ganovcc087f82015-05-12 20:35:54 -07007831 final long identity = Binder.clearCallingIdentity();
Nazanin Bakhshif782e562018-12-11 15:15:39 -08007832
Svet Ganovcc087f82015-05-12 20:35:54 -07007833 try {
Stuart Scott981d8582015-04-21 14:09:50 -07007834 if (SubscriptionManager.isUsableSubIdValue(subId) && !mUserManager.hasUserRestriction(
7835 UserManager.DISALLOW_CONFIG_MOBILE_NETWORKS)) {
Sooraj Sasindran95c07a92020-07-15 01:35:24 -07007836 setDataEnabledForReason(subId, TelephonyManager.DATA_ENABLED_REASON_USER,
Sooraj Sasindranaf1b5132020-05-05 21:06:20 +00007837 getDefaultDataEnabled());
Svet Ganovcc087f82015-05-12 20:35:54 -07007838 setNetworkSelectionModeAutomatic(subId);
SongFerngWang8c6e82e2021-03-02 22:09:29 +08007839 Phone phone = getPhone(subId);
SongFerngWangfd89b102021-05-27 22:44:54 +08007840 cleanUpAllowedNetworkTypes(phone, subId);
Nazanin Bakhshif782e562018-12-11 15:15:39 -08007841 setDataRoamingEnabled(subId, getDefaultDataRoamingEnabled(subId));
Jordan Liu857e73a2021-03-05 16:24:04 -08007842 getPhone(subId).resetCarrierKeysForImsiEncryption();
Svet Ganovcc087f82015-05-12 20:35:54 -07007843 }
Amit Mahajan7dbbd822019-03-13 17:33:47 -07007844 // There has been issues when Sms raw table somehow stores orphan
7845 // fragments. They lead to garbled message when new fragments come
7846 // in and combined with those stale ones. In case this happens again,
7847 // user can reset all network settings which will clean up this table.
7848 cleanUpSmsRawTable(getDefaultPhone().getContext());
Brad Ebingerbc7dd582019-10-17 17:03:22 -07007849 // Clean up IMS settings as well here.
7850 int slotId = getSlotIndex(subId);
7851 if (slotId > SubscriptionManager.INVALID_SIM_SLOT_INDEX) {
7852 ImsManager.getInstance(mApp, slotId).factoryReset();
7853 }
Naina Nallurid63128d2019-09-17 14:10:30 -07007854
Kai Shif70f46f2021-03-03 13:59:46 -08007855 if (defaultPhone == null) {
7856 return;
7857 }
Naina Nallurid63128d2019-09-17 14:10:30 -07007858 // Erase modem config if erase modem on network setting is enabled.
7859 String configValue = DeviceConfig.getProperty(DeviceConfig.NAMESPACE_TELEPHONY,
7860 RESET_NETWORK_ERASE_MODEM_CONFIG_ENABLED);
7861 if (configValue != null && Boolean.parseBoolean(configValue)) {
Kai Shif70f46f2021-03-03 13:59:46 -08007862 sendEraseModemConfig(defaultPhone);
Naina Nallurid63128d2019-09-17 14:10:30 -07007863 }
Kai Shif70f46f2021-03-03 13:59:46 -08007864
7865 sendEraseDataInSharedPreferences(defaultPhone);
Svet Ganovcc087f82015-05-12 20:35:54 -07007866 } finally {
7867 Binder.restoreCallingIdentity(identity);
Stuart Scott8eef64f2015-04-08 15:13:54 -07007868 }
7869 }
Narayan Kamath1c496c22015-04-16 14:40:19 +01007870
SongFerngWangfd89b102021-05-27 22:44:54 +08007871 @VisibleForTesting
7872 void cleanUpAllowedNetworkTypes(Phone phone, int subId) {
7873 if (phone == null || !SubscriptionManager.isUsableSubscriptionId(subId)) {
7874 return;
7875 }
7876 long defaultNetworkType = RadioAccessFamily.getRafFromNetworkType(
7877 RILConstants.PREFERRED_NETWORK_MODE);
7878 SubscriptionManager.setSubscriptionProperty(subId,
7879 SubscriptionManager.ALLOWED_NETWORK_TYPES,
7880 "user=" + defaultNetworkType);
7881 phone.loadAllowedNetworksFromSubscriptionDatabase();
7882 phone.setAllowedNetworkTypes(TelephonyManager.ALLOWED_NETWORK_TYPES_REASON_USER,
7883 defaultNetworkType, null);
7884 }
7885
Amit Mahajan7dbbd822019-03-13 17:33:47 -07007886 private void cleanUpSmsRawTable(Context context) {
7887 ContentResolver resolver = context.getContentResolver();
7888 Uri uri = Uri.withAppendedPath(Telephony.Sms.CONTENT_URI, "raw/permanentDelete");
7889 resolver.delete(uri, null, null);
7890 }
7891
Narayan Kamath1c496c22015-04-16 14:40:19 +01007892 @Override
chen xu5d3637b2019-01-21 23:31:38 -08007893 public String getSimLocaleForSubscriber(int subId) {
7894 enforceReadPrivilegedPermission("getSimLocaleForSubscriber, subId: " + subId);
7895 final Phone phone = getPhone(subId);
7896 if (phone == null) {
7897 log("getSimLocaleForSubscriber, invalid subId");
chen xu2bb91e42019-01-24 14:35:54 -08007898 return null;
chen xu5d3637b2019-01-21 23:31:38 -08007899 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007900 final long identity = Binder.clearCallingIdentity();
7901 try {
chen xu5d3637b2019-01-21 23:31:38 -08007902 final SubscriptionInfo info = mSubscriptionController.getActiveSubscriptionInfo(subId,
Philip P. Moltmann8d34f0c2020-03-05 16:24:02 -08007903 phone.getContext().getOpPackageName(), phone.getContext().getAttributionTag());
chen xu6291c472019-02-04 12:55:53 -08007904 if (info == null) {
7905 log("getSimLocaleForSubscriber, inactive subId: " + subId);
7906 return null;
7907 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007908 // Try and fetch the locale from the carrier properties or from the SIM language
7909 // preferences (EF-PL and EF-LI)...
7910 final int mcc = info.getMcc();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007911 String simLanguage = null;
chen xu5d3637b2019-01-21 23:31:38 -08007912 final Locale localeFromDefaultSim = phone.getLocaleFromSimAndCarrierPrefs();
7913 if (localeFromDefaultSim != null) {
7914 if (!localeFromDefaultSim.getCountry().isEmpty()) {
7915 if (DBG) log("Using locale from subId: " + subId + " locale: "
7916 + localeFromDefaultSim);
7917 return localeFromDefaultSim.toLanguageTag();
7918 } else {
7919 simLanguage = localeFromDefaultSim.getLanguage();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007920 }
7921 }
Narayan Kamath1c496c22015-04-16 14:40:19 +01007922
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007923 // The SIM language preferences only store a language (e.g. fr = French), not an
7924 // exact locale (e.g. fr_FR = French/France). So, if the locale returned from
7925 // the SIM and carrier preferences does not include a country we add the country
7926 // determined from the SIM MCC to provide an exact locale.
zoey chenc730df82019-12-18 17:07:20 +08007927 final Locale mccLocale = LocaleUtils.getLocaleFromMcc(mApp, mcc, simLanguage);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007928 if (mccLocale != null) {
chen xu5d3637b2019-01-21 23:31:38 -08007929 if (DBG) log("No locale from SIM, using mcc locale:" + mccLocale);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007930 return mccLocale.toLanguageTag();
7931 }
7932
7933 if (DBG) log("No locale found - returning null");
7934 return null;
7935 } finally {
7936 Binder.restoreCallingIdentity(identity);
7937 }
Narayan Kamath1c496c22015-04-16 14:40:19 +01007938 }
7939
7940 private List<SubscriptionInfo> getAllSubscriptionInfoList() {
Philip P. Moltmann700a9592019-10-03 11:53:50 -07007941 return mSubscriptionController.getAllSubInfoList(mApp.getOpPackageName(),
Philip P. Moltmann8d34f0c2020-03-05 16:24:02 -08007942 mApp.getAttributionTag());
Narayan Kamath1c496c22015-04-16 14:40:19 +01007943 }
7944
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007945 /**
7946 * NOTE: this method assumes permission checks are done and caller identity has been cleared.
7947 */
7948 private List<SubscriptionInfo> getActiveSubscriptionInfoListPrivileged() {
Philip P. Moltmann700a9592019-10-03 11:53:50 -07007949 return mSubscriptionController.getActiveSubscriptionInfoList(mApp.getOpPackageName(),
Philip P. Moltmann8d34f0c2020-03-05 16:24:02 -08007950 mApp.getAttributionTag());
Narayan Kamath1c496c22015-04-16 14:40:19 +01007951 }
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -07007952
Chenjie Yu1ba97252018-01-11 18:16:20 -08007953 private final ModemActivityInfo mLastModemActivityInfo =
Hall Liu49656c02020-10-09 19:00:11 -07007954 new ModemActivityInfo(0, 0, 0, new int[ModemActivityInfo.getNumTxPowerLevels()], 0);
Chenjie Yu1ba97252018-01-11 18:16:20 -08007955
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -07007956 /**
Adam Lesinski903a54c2016-04-11 14:49:52 -07007957 * Responds to the ResultReceiver with the {@link android.telephony.ModemActivityInfo} object
7958 * representing the state of the modem.
7959 *
Chenjie Yu1ba97252018-01-11 18:16:20 -08007960 * NOTE: The underlying implementation clears the modem state, so there should only ever be one
7961 * caller to it. Everyone should call this class to get cumulative data.
Adam Lesinski903a54c2016-04-11 14:49:52 -07007962 * @hide
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -07007963 */
7964 @Override
Adam Lesinski903a54c2016-04-11 14:49:52 -07007965 public void requestModemActivityInfo(ResultReceiver result) {
7966 enforceModifyPermission();
vagdeviaf9a5b92018-08-15 16:01:53 -07007967 WorkSource workSource = getWorkSource(Binder.getCallingUid());
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007968
7969 final long identity = Binder.clearCallingIdentity();
7970 try {
Shuo Qian8f4750a2020-02-20 17:12:10 -08007971 sendRequestAsync(CMD_GET_MODEM_ACTIVITY_INFO, result, null, workSource);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007972 } finally {
7973 Binder.restoreCallingIdentity(identity);
Chenjie Yu1ba97252018-01-11 18:16:20 -08007974 }
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -07007975 }
Jack Yu85bd38a2015-11-09 11:34:32 -08007976
Siddharth Rayb8114062018-06-17 15:02:38 -07007977 // Checks that ModemActivityInfo is valid. Sleep time, Idle time, Rx time and Tx time should be
7978 // less than total activity duration.
7979 private boolean isModemActivityInfoValid(ModemActivityInfo info) {
7980 if (info == null) {
7981 return false;
7982 }
7983 int activityDurationMs =
Hall Liu49656c02020-10-09 19:00:11 -07007984 (int) (info.getTimestampMillis() - mLastModemActivityInfo.getTimestampMillis());
7985 int totalTxTimeMs = Arrays.stream(info.getTransmitTimeMillis()).sum();
7986
Siddharth Rayb8114062018-06-17 15:02:38 -07007987 return (info.isValid()
7988 && (info.getSleepTimeMillis() <= activityDurationMs)
7989 && (info.getIdleTimeMillis() <= activityDurationMs)
Chen Xud78231e2019-09-10 18:49:52 -07007990 && (info.getReceiveTimeMillis() <= activityDurationMs)
Siddharth Rayb8114062018-06-17 15:02:38 -07007991 && (totalTxTimeMs <= activityDurationMs));
7992 }
7993
Jack Yu85bd38a2015-11-09 11:34:32 -08007994 /**
Jack Yu85bd38a2015-11-09 11:34:32 -08007995 * Returns the service state information on specified subscription.
7996 */
7997 @Override
Sooraj Sasindran2250dc02021-11-10 16:42:01 -08007998 public ServiceState getServiceStateForSubscriber(int subId,
7999 boolean renounceFineLocationAccess, boolean renounceCoarseLocationAccess,
8000 String callingPackage, String callingFeatureId) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08008001 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
Philip P. Moltmann700a9592019-10-03 11:53:50 -07008002 mApp, subId, callingPackage, callingFeatureId, "getServiceStateForSubscriber")) {
Jack Yu85bd38a2015-11-09 11:34:32 -08008003 return null;
8004 }
8005
Sooraj Sasindran2250dc02021-11-10 16:42:01 -08008006 boolean hasFinePermission = false;
8007 boolean hasCoarsePermission = false;
8008 if (!renounceFineLocationAccess) {
8009 LocationAccessPolicy.LocationPermissionResult fineLocationResult =
8010 LocationAccessPolicy.checkLocationPermission(mApp,
8011 new LocationAccessPolicy.LocationPermissionQuery.Builder()
8012 .setCallingPackage(callingPackage)
8013 .setCallingFeatureId(callingFeatureId)
8014 .setCallingPid(Binder.getCallingPid())
8015 .setCallingUid(Binder.getCallingUid())
8016 .setMethod("getServiceStateForSubscriber")
8017 .setLogAsInfo(true)
8018 .setMinSdkVersionForFine(Build.VERSION_CODES.Q)
8019 .setMinSdkVersionForCoarse(Build.VERSION_CODES.Q)
8020 .setMinSdkVersionForEnforcement(Build.VERSION_CODES.Q)
8021 .build());
8022 hasFinePermission =
8023 fineLocationResult == LocationAccessPolicy.LocationPermissionResult.ALLOWED;
8024 }
Hall Liuf19c44f2018-11-27 14:38:17 -08008025
Sooraj Sasindran2250dc02021-11-10 16:42:01 -08008026 if (!renounceCoarseLocationAccess) {
8027 LocationAccessPolicy.LocationPermissionResult coarseLocationResult =
8028 LocationAccessPolicy.checkLocationPermission(mApp,
8029 new LocationAccessPolicy.LocationPermissionQuery.Builder()
8030 .setCallingPackage(callingPackage)
8031 .setCallingFeatureId(callingFeatureId)
8032 .setCallingPid(Binder.getCallingPid())
8033 .setCallingUid(Binder.getCallingUid())
8034 .setMethod("getServiceStateForSubscriber")
8035 .setLogAsInfo(true)
8036 .setMinSdkVersionForCoarse(Build.VERSION_CODES.Q)
8037 .setMinSdkVersionForFine(Integer.MAX_VALUE)
8038 .setMinSdkVersionForEnforcement(Build.VERSION_CODES.Q)
8039 .build());
8040 hasCoarsePermission =
8041 coarseLocationResult == LocationAccessPolicy.LocationPermissionResult.ALLOWED;
8042 }
Hall Liuf19c44f2018-11-27 14:38:17 -08008043
Jack Yu479f40e2020-10-27 21:29:25 -07008044 final Phone phone = getPhone(subId);
8045 if (phone == null) {
8046 return null;
8047 }
8048
Jordan Liu0f2bc442020-11-18 16:47:37 -08008049 final long identity = Binder.clearCallingIdentity();
8050
Jack Yu479f40e2020-10-27 21:29:25 -07008051 boolean isCallingPackageDataService = phone.getDataServicePackages()
8052 .contains(callingPackage);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008053 try {
Jordan Liuc437b192020-08-17 10:59:12 -07008054 // isActiveSubId requires READ_PHONE_STATE, which we already check for above
8055 if (!mSubscriptionController.isActiveSubId(subId, callingPackage, callingFeatureId)) {
8056 Rlog.d(LOG_TAG,
8057 "getServiceStateForSubscriber returning null for inactive subId=" + subId);
8058 return null;
8059 }
8060
Hall Liuf19c44f2018-11-27 14:38:17 -08008061 ServiceState ss = phone.getServiceState();
8062
8063 // Scrub out the location info in ServiceState depending on what level of access
8064 // the caller has.
Jack Yu479f40e2020-10-27 21:29:25 -07008065 if (hasFinePermission || isCallingPackageDataService) return ss;
Malcolm Chen5052de62019-12-30 13:56:38 -08008066 if (hasCoarsePermission) return ss.createLocationInfoSanitizedCopy(false);
8067 return ss.createLocationInfoSanitizedCopy(true);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008068 } finally {
8069 Binder.restoreCallingIdentity(identity);
8070 }
Jack Yu85bd38a2015-11-09 11:34:32 -08008071 }
Nancy Chen31f9ba12016-01-06 11:42:12 -08008072
8073 /**
8074 * Returns the URI for the per-account voicemail ringtone set in Phone settings.
8075 *
8076 * @param accountHandle The handle for the {@link PhoneAccount} for which to retrieve the
8077 * voicemail ringtone.
8078 * @return The URI for the ringtone to play when receiving a voicemail from a specific
8079 * PhoneAccount.
8080 */
8081 @Override
8082 public Uri getVoicemailRingtoneUri(PhoneAccountHandle accountHandle) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008083 final long identity = Binder.clearCallingIdentity();
8084 try {
8085 Phone phone = PhoneUtils.getPhoneForPhoneAccountHandle(accountHandle);
8086 if (phone == null) {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08008087 phone = getDefaultPhone();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008088 }
Nancy Chen31f9ba12016-01-06 11:42:12 -08008089
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008090 return VoicemailNotificationSettingsUtil.getRingtoneUri(phone.getContext());
8091 } finally {
8092 Binder.restoreCallingIdentity(identity);
8093 }
Nancy Chen31f9ba12016-01-06 11:42:12 -08008094 }
8095
8096 /**
Ta-wei Yenc33877d2017-01-23 18:11:21 -08008097 * Sets the per-account voicemail ringtone.
8098 *
8099 * <p>Requires that the calling app is the default dialer, or has carrier privileges, or
8100 * has permission {@link android.Manifest.permission#MODIFY_PHONE_STATE MODIFY_PHONE_STATE}.
8101 *
8102 * @param phoneAccountHandle The handle for the {@link PhoneAccount} for which to set the
8103 * voicemail ringtone.
8104 * @param uri The URI for the ringtone to play when receiving a voicemail from a specific
8105 * PhoneAccount.
8106 */
8107 @Override
8108 public void setVoicemailRingtoneUri(String callingPackage,
8109 PhoneAccountHandle phoneAccountHandle, Uri uri) {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08008110 final Phone defaultPhone = getDefaultPhone();
Ta-wei Yenc33877d2017-01-23 18:11:21 -08008111 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
Tyler Gunn5ddfdc92019-10-31 13:08:23 -07008112 TelecomManager tm = defaultPhone.getContext().getSystemService(TelecomManager.class);
8113 if (!TextUtils.equals(callingPackage, tm.getDefaultDialerPackage())) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08008114 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
8115 mApp, PhoneUtils.getSubIdForPhoneAccountHandle(phoneAccountHandle),
8116 "setVoicemailRingtoneUri");
Ta-wei Yenc33877d2017-01-23 18:11:21 -08008117 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008118
8119 final long identity = Binder.clearCallingIdentity();
8120 try {
8121 Phone phone = PhoneUtils.getPhoneForPhoneAccountHandle(phoneAccountHandle);
8122 if (phone == null) {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08008123 phone = defaultPhone;
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008124 }
8125 VoicemailNotificationSettingsUtil.setRingtoneUri(phone.getContext(), uri);
8126 } finally {
8127 Binder.restoreCallingIdentity(identity);
Ta-wei Yenc33877d2017-01-23 18:11:21 -08008128 }
Ta-wei Yenc33877d2017-01-23 18:11:21 -08008129 }
8130
8131 /**
Nancy Chen31f9ba12016-01-06 11:42:12 -08008132 * Returns whether vibration is set for voicemail notification in Phone settings.
8133 *
8134 * @param accountHandle The handle for the {@link PhoneAccount} for which to retrieve the
8135 * voicemail vibration setting.
8136 * @return {@code true} if the vibration is set for this PhoneAccount, {@code false} otherwise.
8137 */
8138 @Override
8139 public boolean isVoicemailVibrationEnabled(PhoneAccountHandle accountHandle) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008140 final long identity = Binder.clearCallingIdentity();
8141 try {
8142 Phone phone = PhoneUtils.getPhoneForPhoneAccountHandle(accountHandle);
8143 if (phone == null) {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08008144 phone = getDefaultPhone();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008145 }
Nancy Chen31f9ba12016-01-06 11:42:12 -08008146
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008147 return VoicemailNotificationSettingsUtil.isVibrationEnabled(phone.getContext());
8148 } finally {
8149 Binder.restoreCallingIdentity(identity);
8150 }
Nancy Chen31f9ba12016-01-06 11:42:12 -08008151 }
8152
Youhan Wange64578a2016-05-02 15:32:42 -07008153 /**
Ta-wei Yenc33877d2017-01-23 18:11:21 -08008154 * Sets the per-account voicemail vibration.
8155 *
8156 * <p>Requires that the calling app is the default dialer, or has carrier privileges, or
8157 * has permission {@link android.Manifest.permission#MODIFY_PHONE_STATE MODIFY_PHONE_STATE}.
8158 *
8159 * @param phoneAccountHandle The handle for the {@link PhoneAccount} for which to set the
8160 * voicemail vibration setting.
8161 * @param enabled Whether to enable or disable vibration for voicemail notifications from a
8162 * specific PhoneAccount.
8163 */
8164 @Override
8165 public void setVoicemailVibrationEnabled(String callingPackage,
8166 PhoneAccountHandle phoneAccountHandle, boolean enabled) {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08008167 final Phone defaultPhone = getDefaultPhone();
Ta-wei Yenc33877d2017-01-23 18:11:21 -08008168 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
Tyler Gunn5ddfdc92019-10-31 13:08:23 -07008169 TelecomManager tm = defaultPhone.getContext().getSystemService(TelecomManager.class);
8170 if (!TextUtils.equals(callingPackage, tm.getDefaultDialerPackage())) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08008171 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
8172 mApp, PhoneUtils.getSubIdForPhoneAccountHandle(phoneAccountHandle),
8173 "setVoicemailVibrationEnabled");
Ta-wei Yenc33877d2017-01-23 18:11:21 -08008174 }
8175
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008176 final long identity = Binder.clearCallingIdentity();
8177 try {
8178 Phone phone = PhoneUtils.getPhoneForPhoneAccountHandle(phoneAccountHandle);
8179 if (phone == null) {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08008180 phone = defaultPhone;
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008181 }
8182 VoicemailNotificationSettingsUtil.setVibrationEnabled(phone.getContext(), enabled);
8183 } finally {
8184 Binder.restoreCallingIdentity(identity);
Ta-wei Yenc33877d2017-01-23 18:11:21 -08008185 }
Ta-wei Yenc33877d2017-01-23 18:11:21 -08008186 }
8187
8188 /**
Youhan Wange64578a2016-05-02 15:32:42 -07008189 * Make sure either called from same process as self (phone) or IPC caller has read privilege.
8190 *
8191 * @throws SecurityException if the caller does not have the required permission
8192 */
Brad Ebinger35c841c2018-10-01 10:40:55 -07008193 private void enforceReadPrivilegedPermission(String message) {
Youhan Wange64578a2016-05-02 15:32:42 -07008194 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.READ_PRIVILEGED_PHONE_STATE,
Brad Ebinger35c841c2018-10-01 10:40:55 -07008195 message);
Youhan Wange64578a2016-05-02 15:32:42 -07008196 }
8197
8198 /**
Ta-wei Yen30a69c82016-12-27 14:52:32 -08008199 * Make sure either called from same process as self (phone) or IPC caller has send SMS
8200 * permission.
8201 *
8202 * @throws SecurityException if the caller does not have the required permission
8203 */
8204 private void enforceSendSmsPermission() {
8205 mApp.enforceCallingOrSelfPermission(permission.SEND_SMS, null);
8206 }
8207
8208 /**
Ta-wei Yen527a9c02017-01-06 15:29:25 -08008209 * Make sure called from the package in charge of visual voicemail.
Ta-wei Yen30a69c82016-12-27 14:52:32 -08008210 *
Ta-wei Yen527a9c02017-01-06 15:29:25 -08008211 * @throws SecurityException if the caller is not the visual voicemail package.
Ta-wei Yen30a69c82016-12-27 14:52:32 -08008212 */
Ta-wei Yen527a9c02017-01-06 15:29:25 -08008213 private void enforceVisualVoicemailPackage(String callingPackage, int subId) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008214 final long identity = Binder.clearCallingIdentity();
8215 try {
8216 ComponentName componentName =
Nazanin Bakhshif782e562018-12-11 15:15:39 -08008217 RemoteVvmTaskManager.getRemotePackage(mApp, subId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008218 if (componentName == null) {
8219 throw new SecurityException(
8220 "Caller not current active visual voicemail package[null]");
8221 }
8222 String vvmPackage = componentName.getPackageName();
8223 if (!callingPackage.equals(vvmPackage)) {
8224 throw new SecurityException("Caller not current active visual voicemail package["
8225 + vvmPackage + "]");
8226 }
8227 } finally {
8228 Binder.restoreCallingIdentity(identity);
Ta-wei Yen30a69c82016-12-27 14:52:32 -08008229 }
8230 }
8231
8232 /**
Youhan Wange64578a2016-05-02 15:32:42 -07008233 * Return the application ID for the app type.
8234 *
8235 * @param subId the subscription ID that this request applies to.
8236 * @param appType the uicc app type.
8237 * @return Application ID for specificied app type, or null if no uicc.
8238 */
8239 @Override
8240 public String getAidForAppType(int subId, int appType) {
Brad Ebinger35c841c2018-10-01 10:40:55 -07008241 enforceReadPrivilegedPermission("getAidForAppType");
Youhan Wange64578a2016-05-02 15:32:42 -07008242 Phone phone = getPhone(subId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008243
8244 final long identity = Binder.clearCallingIdentity();
Youhan Wange64578a2016-05-02 15:32:42 -07008245 try {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008246 if (phone == null) {
8247 return null;
8248 }
8249 String aid = null;
8250 try {
Muralidhar Reddy472c2ae2021-09-29 19:38:40 +00008251 aid = UiccController.getInstance().getUiccPort(phone.getPhoneId())
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008252 .getApplicationByType(appType).getAid();
8253 } catch (Exception e) {
8254 Log.e(LOG_TAG, "Not getting aid. Exception ex=" + e);
8255 }
8256 return aid;
8257 } finally {
8258 Binder.restoreCallingIdentity(identity);
Youhan Wange64578a2016-05-02 15:32:42 -07008259 }
Youhan Wange64578a2016-05-02 15:32:42 -07008260 }
8261
Youhan Wang4001d252016-05-11 10:29:41 -07008262 /**
8263 * Return the Electronic Serial Number.
8264 *
8265 * @param subId the subscription ID that this request applies to.
8266 * @return ESN or null if error.
8267 */
8268 @Override
8269 public String getEsn(int subId) {
Brad Ebinger35c841c2018-10-01 10:40:55 -07008270 enforceReadPrivilegedPermission("getEsn");
Youhan Wang4001d252016-05-11 10:29:41 -07008271 Phone phone = getPhone(subId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008272
8273 final long identity = Binder.clearCallingIdentity();
Youhan Wang4001d252016-05-11 10:29:41 -07008274 try {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008275 if (phone == null) {
8276 return null;
8277 }
8278 String esn = null;
8279 try {
8280 esn = phone.getEsn();
8281 } catch (Exception e) {
8282 Log.e(LOG_TAG, "Not getting ESN. Exception ex=" + e);
8283 }
8284 return esn;
8285 } finally {
8286 Binder.restoreCallingIdentity(identity);
Youhan Wang4001d252016-05-11 10:29:41 -07008287 }
Youhan Wang4001d252016-05-11 10:29:41 -07008288 }
8289
Sanket Padawe99ef1e32016-05-18 16:12:33 -07008290 /**
Youhan Wang66ad5d72016-07-18 17:56:58 -07008291 * Return the Preferred Roaming List Version.
8292 *
8293 * @param subId the subscription ID that this request applies to.
8294 * @return PRLVersion or null if error.
8295 */
8296 @Override
8297 public String getCdmaPrlVersion(int subId) {
Brad Ebinger35c841c2018-10-01 10:40:55 -07008298 enforceReadPrivilegedPermission("getCdmaPrlVersion");
Youhan Wang66ad5d72016-07-18 17:56:58 -07008299 Phone phone = getPhone(subId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008300
8301 final long identity = Binder.clearCallingIdentity();
Youhan Wang66ad5d72016-07-18 17:56:58 -07008302 try {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008303 if (phone == null) {
8304 return null;
8305 }
8306 String cdmaPrlVersion = null;
8307 try {
8308 cdmaPrlVersion = phone.getCdmaPrlVersion();
8309 } catch (Exception e) {
8310 Log.e(LOG_TAG, "Not getting PRLVersion", e);
8311 }
8312 return cdmaPrlVersion;
8313 } finally {
8314 Binder.restoreCallingIdentity(identity);
Youhan Wang66ad5d72016-07-18 17:56:58 -07008315 }
Youhan Wang66ad5d72016-07-18 17:56:58 -07008316 }
8317
8318 /**
Sanket Padawe99ef1e32016-05-18 16:12:33 -07008319 * Get snapshot of Telephony histograms
8320 * @return List of Telephony histograms
8321 * @hide
8322 */
8323 @Override
8324 public List<TelephonyHistogram> getTelephonyHistograms() {
Jeff Davidson7e17e312018-02-13 18:17:36 -08008325 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
8326 mApp, getDefaultSubscription(), "getTelephonyHistograms");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008327
8328 final long identity = Binder.clearCallingIdentity();
8329 try {
8330 return RIL.getTelephonyRILTimingHistograms();
8331 } finally {
8332 Binder.restoreCallingIdentity(identity);
8333 }
Sanket Padawe99ef1e32016-05-18 16:12:33 -07008334 }
Meng Wang1a7c35a2016-05-05 20:56:15 -07008335
8336 /**
8337 * {@hide}
Michele Berionne482f8202018-11-27 18:57:59 -08008338 * Set the allowed carrier list and the excluded carrier list, indicating the priority between
8339 * the two lists.
Meng Wang1a7c35a2016-05-05 20:56:15 -07008340 * Require system privileges. In the future we may add this to carrier APIs.
8341 *
Michele Berionne482f8202018-11-27 18:57:59 -08008342 * @return Integer with the result of the operation, as defined in {@link TelephonyManager}.
Meng Wang1a7c35a2016-05-05 20:56:15 -07008343 */
8344 @Override
Michele Berionne482f8202018-11-27 18:57:59 -08008345 @TelephonyManager.SetCarrierRestrictionResult
8346 public int setAllowedCarriers(CarrierRestrictionRules carrierRestrictionRules) {
Meng Wang1a7c35a2016-05-05 20:56:15 -07008347 enforceModifyPermission();
vagdeviaf9a5b92018-08-15 16:01:53 -07008348 WorkSource workSource = getWorkSource(Binder.getCallingUid());
Sanket Padawe13bac7b2017-03-20 15:04:47 -07008349
Michele Berionne482f8202018-11-27 18:57:59 -08008350 if (carrierRestrictionRules == null) {
8351 throw new NullPointerException("carrier restriction cannot be null");
Meng Wang9b7c4e92017-02-17 11:41:27 -08008352 }
Sanket Padawe13bac7b2017-03-20 15:04:47 -07008353
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008354 final long identity = Binder.clearCallingIdentity();
8355 try {
Michele Berionne482f8202018-11-27 18:57:59 -08008356 return (int) sendRequest(CMD_SET_ALLOWED_CARRIERS, carrierRestrictionRules,
vagdeviaf9a5b92018-08-15 16:01:53 -07008357 workSource);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008358 } finally {
8359 Binder.restoreCallingIdentity(identity);
8360 }
Meng Wang1a7c35a2016-05-05 20:56:15 -07008361 }
8362
8363 /**
8364 * {@hide}
Michele Berionne482f8202018-11-27 18:57:59 -08008365 * Get the allowed carrier list and the excluded carrier list, including the priority between
8366 * the two lists.
Meng Wang1a7c35a2016-05-05 20:56:15 -07008367 * Require system privileges. In the future we may add this to carrier APIs.
8368 *
Michele Berionne482f8202018-11-27 18:57:59 -08008369 * @return {@link android.telephony.CarrierRestrictionRules}
Meng Wang1a7c35a2016-05-05 20:56:15 -07008370 */
8371 @Override
Michele Berionne482f8202018-11-27 18:57:59 -08008372 public CarrierRestrictionRules getAllowedCarriers() {
Brad Ebinger35c841c2018-10-01 10:40:55 -07008373 enforceReadPrivilegedPermission("getAllowedCarriers");
vagdeviaf9a5b92018-08-15 16:01:53 -07008374 WorkSource workSource = getWorkSource(Binder.getCallingUid());
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008375
8376 final long identity = Binder.clearCallingIdentity();
8377 try {
Michele Berionne482f8202018-11-27 18:57:59 -08008378 Object response = sendRequest(CMD_GET_ALLOWED_CARRIERS, null, workSource);
8379 if (response instanceof CarrierRestrictionRules) {
8380 return (CarrierRestrictionRules) response;
8381 }
8382 // Response is an Exception of some kind,
8383 // which is signalled to the user as a NULL retval
8384 return null;
8385 } catch (Exception e) {
8386 Log.e(LOG_TAG, "getAllowedCarriers. Exception ex=" + e);
8387 return null;
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008388 } finally {
8389 Binder.restoreCallingIdentity(identity);
8390 }
Meng Wang1a7c35a2016-05-05 20:56:15 -07008391 }
8392
fionaxu59545b42016-05-25 15:53:37 -07008393 /**
fionaxu59545b42016-05-25 15:53:37 -07008394 * Action set from carrier signalling broadcast receivers to enable/disable radio
8395 * @param subId the subscription ID that this action applies to.
8396 * @param enabled control enable or disable radio.
8397 * {@hide}
8398 */
8399 @Override
8400 public void carrierActionSetRadioEnabled(int subId, boolean enabled) {
8401 enforceModifyPermission();
8402 final Phone phone = getPhone(subId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008403
8404 final long identity = Binder.clearCallingIdentity();
fionaxu59545b42016-05-25 15:53:37 -07008405 if (phone == null) {
8406 loge("carrierAction: SetRadioEnabled fails with invalid sibId: " + subId);
8407 return;
8408 }
8409 try {
8410 phone.carrierActionSetRadioEnabled(enabled);
8411 } catch (Exception e) {
8412 Log.e(LOG_TAG, "carrierAction: SetRadioEnabled fails. Exception ex=" + e);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008413 } finally {
8414 Binder.restoreCallingIdentity(identity);
fionaxu59545b42016-05-25 15:53:37 -07008415 }
8416 }
8417
Ta-wei Yenc236d6b2016-06-21 13:33:12 -07008418 /**
Sooraj Sasindrandaf060f2021-06-15 14:52:55 -07008419 * Enable or disable Voice over NR (VoNR)
8420 * @param subId the subscription ID that this action applies to.
8421 * @param enabled enable or disable VoNR.
8422 * @return operation result.
8423 */
8424 @Override
8425 public int setVoNrEnabled(int subId, boolean enabled) {
8426 enforceModifyPermission();
8427 final Phone phone = getPhone(subId);
8428
8429 final long identity = Binder.clearCallingIdentity();
8430 if (phone == null) {
8431 loge("setVoNrEnabled fails with no phone object for subId: " + subId);
8432 return TelephonyManager.ENABLE_VONR_RADIO_NOT_AVAILABLE;
8433 }
8434
8435 WorkSource workSource = getWorkSource(Binder.getCallingUid());
8436 try {
8437 int result = (int) sendRequest(CMD_ENABLE_VONR, enabled, subId,
8438 workSource);
8439 if (DBG) log("setVoNrEnabled result: " + result);
Gary Jian8dd305f2021-10-14 16:31:35 +08008440
8441 if (result == TelephonyManager.ENABLE_VONR_SUCCESS) {
8442 if (DBG) {
8443 log("Set VoNR settings in siminfo db; subId=" + subId + ", value:" + enabled);
8444 }
8445 SubscriptionManager.setSubscriptionProperty(
8446 subId, SubscriptionManager.NR_ADVANCED_CALLING_ENABLED,
8447 (enabled ? "1" : "0"));
8448 }
8449
Sooraj Sasindrandaf060f2021-06-15 14:52:55 -07008450 return result;
8451 } finally {
8452 Binder.restoreCallingIdentity(identity);
8453 }
8454 }
8455
8456 /**
8457 * Is voice over NR enabled
8458 * @return true if VoNR is enabled else false
8459 */
8460 @Override
8461 public boolean isVoNrEnabled(int subId) {
8462 enforceReadPrivilegedPermission("isVoNrEnabled");
8463 WorkSource workSource = getWorkSource(Binder.getCallingUid());
8464 final long identity = Binder.clearCallingIdentity();
8465 try {
8466 boolean isEnabled = (boolean) sendRequest(CMD_IS_VONR_ENABLED,
8467 null, subId, workSource);
8468 if (DBG) log("isVoNrEnabled: " + isEnabled);
8469 return isEnabled;
8470 } finally {
8471 Binder.restoreCallingIdentity(identity);
8472 }
8473 }
8474
8475 /**
fionaxu8da9cb12017-05-23 15:02:46 -07008476 * Action set from carrier signalling broadcast receivers to start/stop reporting the default
8477 * network status based on which carrier apps could apply actions accordingly,
8478 * enable/disable default url handler for example.
8479 *
8480 * @param subId the subscription ID that this action applies to.
8481 * @param report control start/stop reporting the default network status.
8482 * {@hide}
8483 */
8484 @Override
8485 public void carrierActionReportDefaultNetworkStatus(int subId, boolean report) {
8486 enforceModifyPermission();
8487 final Phone phone = getPhone(subId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008488
8489 final long identity = Binder.clearCallingIdentity();
fionaxu8da9cb12017-05-23 15:02:46 -07008490 if (phone == null) {
8491 loge("carrierAction: ReportDefaultNetworkStatus fails with invalid sibId: " + subId);
8492 return;
8493 }
8494 try {
8495 phone.carrierActionReportDefaultNetworkStatus(report);
8496 } catch (Exception e) {
8497 Log.e(LOG_TAG, "carrierAction: ReportDefaultNetworkStatus fails. Exception ex=" + e);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008498 } finally {
8499 Binder.restoreCallingIdentity(identity);
fionaxu8da9cb12017-05-23 15:02:46 -07008500 }
8501 }
8502
8503 /**
fionaxud9622282017-07-17 17:51:30 -07008504 * Action set from carrier signalling broadcast receivers to reset all carrier actions
8505 * @param subId the subscription ID that this action applies to.
8506 * {@hide}
8507 */
8508 @Override
8509 public void carrierActionResetAll(int subId) {
8510 enforceModifyPermission();
8511 final Phone phone = getPhone(subId);
8512 if (phone == null) {
8513 loge("carrierAction: ResetAll fails with invalid sibId: " + subId);
8514 return;
8515 }
8516 try {
8517 phone.carrierActionResetAll();
8518 } catch (Exception e) {
8519 Log.e(LOG_TAG, "carrierAction: ResetAll fails. Exception ex=" + e);
8520 }
8521 }
8522
8523 /**
Ta-wei Yenc236d6b2016-06-21 13:33:12 -07008524 * Called when "adb shell dumpsys phone" is invoked. Dump is also automatically invoked when a
8525 * bug report is being generated.
8526 */
8527 @Override
Ta-wei Yen99282e02016-06-21 18:19:35 -07008528 protected void dump(FileDescriptor fd, PrintWriter writer, String[] args) {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08008529 if (mApp.checkCallingOrSelfPermission(android.Manifest.permission.DUMP)
8530 != PackageManager.PERMISSION_GRANTED) {
dcashman22b950d2016-06-27 11:39:02 -07008531 writer.println("Permission Denial: can't dump Phone from pid="
8532 + Binder.getCallingPid()
8533 + ", uid=" + Binder.getCallingUid()
8534 + "without permission "
8535 + android.Manifest.permission.DUMP);
8536 return;
8537 }
Nazanin Bakhshif782e562018-12-11 15:15:39 -08008538 DumpsysHandler.dump(mApp, fd, writer, args);
Ta-wei Yenc236d6b2016-06-21 13:33:12 -07008539 }
Jack Yueb89b242016-06-22 13:27:47 -07008540
Brad Ebingerdac2f002018-04-03 15:17:52 -07008541 @Override
Hall Liua1548bd2019-12-24 14:14:12 -08008542 public int handleShellCommand(@NonNull ParcelFileDescriptor in,
8543 @NonNull ParcelFileDescriptor out, @NonNull ParcelFileDescriptor err,
8544 @NonNull String[] args) {
8545 return new TelephonyShellCommand(this, getDefaultPhone().getContext()).exec(
8546 this, in.getFileDescriptor(), out.getFileDescriptor(),
8547 err.getFileDescriptor(), args);
Brad Ebingerdac2f002018-04-03 15:17:52 -07008548 }
8549
Jack Yueb89b242016-06-22 13:27:47 -07008550 /**
Sooraj Sasindranaf1b5132020-05-05 21:06:20 +00008551 * Policy control of data connection with reason {@@TelephonyManager.DataEnabledReason}
Greg Kaiser17f41752020-05-05 16:47:47 +00008552 * @param subId Subscription index
Sooraj Sasindranaf1b5132020-05-05 21:06:20 +00008553 * @param reason the reason the data enable change is taking place
8554 * @param enabled True if enabling the data, otherwise disabling.
8555 * @hide
Jack Yu75ab2952016-07-08 14:29:33 -07008556 */
8557 @Override
Sooraj Sasindran95c07a92020-07-15 01:35:24 -07008558 public void setDataEnabledForReason(int subId, @TelephonyManager.DataEnabledReason int reason,
Sooraj Sasindranaf1b5132020-05-05 21:06:20 +00008559 boolean enabled) {
8560 if (reason == TelephonyManager.DATA_ENABLED_REASON_USER
8561 || reason == TelephonyManager.DATA_ENABLED_REASON_CARRIER) {
8562 try {
8563 TelephonyPermissions.enforceCallingOrSelfCarrierPrivilege(
Sooraj Sasindran95c07a92020-07-15 01:35:24 -07008564 mApp, subId, "setDataEnabledForReason");
Sooraj Sasindranaf1b5132020-05-05 21:06:20 +00008565 } catch (SecurityException se) {
8566 enforceModifyPermission();
8567 }
8568 } else {
8569 enforceModifyPermission();
8570 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008571
8572 final long identity = Binder.clearCallingIdentity();
8573 try {
8574 Phone phone = getPhone(subId);
8575 if (phone != null) {
Sooraj Sasindranaf1b5132020-05-05 21:06:20 +00008576 if (reason == TelephonyManager.DATA_ENABLED_REASON_CARRIER) {
8577 phone.carrierActionSetMeteredApnsEnabled(enabled);
8578 } else {
Jack Yu99e87332021-12-17 23:14:15 -08008579 if (phone.isUsingNewDataStack()) {
8580 phone.getDataSettingsManager().setDataEnabled(reason, enabled);
8581 } else {
8582 phone.getDataEnabledSettings().setDataEnabled(reason, enabled);
8583 }
Sooraj Sasindranaf1b5132020-05-05 21:06:20 +00008584 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008585 }
8586 } finally {
8587 Binder.restoreCallingIdentity(identity);
Jack Yu75ab2952016-07-08 14:29:33 -07008588 }
8589 }
Sooraj Sasindran9a909312016-07-18 11:57:25 -07008590
8591 /**
8592 * Get Client request stats
8593 * @return List of Client Request Stats
8594 * @hide
8595 */
8596 @Override
Philip P. Moltmann700a9592019-10-03 11:53:50 -07008597 public List<ClientRequestStats> getClientRequestStats(String callingPackage,
8598 String callingFeatureId, int subId) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08008599 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
Philip P. Moltmann700a9592019-10-03 11:53:50 -07008600 mApp, subId, callingPackage, callingFeatureId, "getClientRequestStats")) {
Sooraj Sasindran9a909312016-07-18 11:57:25 -07008601 return null;
8602 }
Sooraj Sasindran9a909312016-07-18 11:57:25 -07008603 Phone phone = getPhone(subId);
Sooraj Sasindran9a909312016-07-18 11:57:25 -07008604
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008605 final long identity = Binder.clearCallingIdentity();
8606 try {
8607 if (phone != null) {
8608 return phone.getClientRequestStats();
8609 }
8610
8611 return null;
8612 } finally {
8613 Binder.restoreCallingIdentity(identity);
8614 }
Sooraj Sasindran9a909312016-07-18 11:57:25 -07008615 }
8616
Narayan Kamathf04b5a12018-01-09 11:47:15 +00008617 private WorkSource getWorkSource(int uid) {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08008618 String packageName = mApp.getPackageManager().getNameForUid(uid);
Narayan Kamathf04b5a12018-01-09 11:47:15 +00008619 return new WorkSource(uid, packageName);
Sooraj Sasindran9a909312016-07-18 11:57:25 -07008620 }
Jack Yueb4124c2017-02-16 15:32:43 -08008621
8622 /**
Grace Chen70990072017-03-24 17:21:30 -07008623 * Set SIM card power state.
Jack Yueb4124c2017-02-16 15:32:43 -08008624 *
Sanket Padawe13bac7b2017-03-20 15:04:47 -07008625 * @param slotIndex SIM slot id.
Grace Chen70990072017-03-24 17:21:30 -07008626 * @param state State of SIM (power down, power up, pass through)
8627 * - {@link android.telephony.TelephonyManager#CARD_POWER_DOWN}
8628 * - {@link android.telephony.TelephonyManager#CARD_POWER_UP}
8629 * - {@link android.telephony.TelephonyManager#CARD_POWER_UP_PASS_THROUGH}
Jack Yueb4124c2017-02-16 15:32:43 -08008630 *
8631 **/
8632 @Override
Grace Chen70990072017-03-24 17:21:30 -07008633 public void setSimPowerStateForSlot(int slotIndex, int state) {
Jack Yueb4124c2017-02-16 15:32:43 -08008634 enforceModifyPermission();
Sanket Padawe13bac7b2017-03-20 15:04:47 -07008635 Phone phone = PhoneFactory.getPhone(slotIndex);
8636
vagdeviaf9a5b92018-08-15 16:01:53 -07008637 WorkSource workSource = getWorkSource(Binder.getCallingUid());
8638
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008639 final long identity = Binder.clearCallingIdentity();
8640 try {
8641 if (phone != null) {
Jordan Liu109698e2020-11-24 14:50:34 -08008642 phone.setSimPowerState(state, null, workSource);
8643 }
8644 } finally {
8645 Binder.restoreCallingIdentity(identity);
8646 }
8647 }
8648
8649 /**
8650 * Set SIM card power state.
8651 *
8652 * @param slotIndex SIM slot id.
8653 * @param state State of SIM (power down, power up, pass through)
8654 * @param callback callback to trigger after success or failure
8655 * - {@link android.telephony.TelephonyManager#CARD_POWER_DOWN}
8656 * - {@link android.telephony.TelephonyManager#CARD_POWER_UP}
8657 * - {@link android.telephony.TelephonyManager#CARD_POWER_UP_PASS_THROUGH}
8658 *
8659 **/
8660 @Override
8661 public void setSimPowerStateForSlotWithCallback(int slotIndex, int state,
8662 IIntegerConsumer callback) {
8663 enforceModifyPermission();
8664 Phone phone = PhoneFactory.getPhone(slotIndex);
8665
8666 WorkSource workSource = getWorkSource(Binder.getCallingUid());
8667
8668 final long identity = Binder.clearCallingIdentity();
8669 try {
8670 if (phone != null) {
8671 Pair<Integer, IIntegerConsumer> arguments = Pair.create(state, callback);
8672 sendRequestAsync(CMD_SET_SIM_POWER, arguments, phone, workSource);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008673 }
8674 } finally {
8675 Binder.restoreCallingIdentity(identity);
Jack Yueb4124c2017-02-16 15:32:43 -08008676 }
8677 }
Shuo Qiandd210312017-04-12 22:11:33 +00008678
Tyler Gunn65d45c22017-06-05 11:22:26 -07008679 private boolean isUssdApiAllowed(int subId) {
8680 CarrierConfigManager configManager =
Nazanin Bakhshif782e562018-12-11 15:15:39 -08008681 (CarrierConfigManager) mApp.getSystemService(Context.CARRIER_CONFIG_SERVICE);
Tyler Gunn65d45c22017-06-05 11:22:26 -07008682 if (configManager == null) {
8683 return false;
8684 }
8685 PersistableBundle pb = configManager.getConfigForSubId(subId);
8686 if (pb == null) {
8687 return false;
8688 }
8689 return pb.getBoolean(
8690 CarrierConfigManager.KEY_ALLOW_USSD_REQUESTS_VIA_TELEPHONY_MANAGER_BOOL);
8691 }
8692
Shuo Qiandd210312017-04-12 22:11:33 +00008693 /**
8694 * Check if phone is in emergency callback mode
8695 * @return true if phone is in emergency callback mode
8696 * @param subId sub id
8697 */
goneil9c5f4872017-12-05 14:07:56 -08008698 @Override
Shuo Qiandd210312017-04-12 22:11:33 +00008699 public boolean getEmergencyCallbackMode(int subId) {
Brad Ebinger35c841c2018-10-01 10:40:55 -07008700 enforceReadPrivilegedPermission("getEmergencyCallbackMode");
Shuo Qiandd210312017-04-12 22:11:33 +00008701 final Phone phone = getPhone(subId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008702
8703 final long identity = Binder.clearCallingIdentity();
8704 try {
8705 if (phone != null) {
8706 return phone.isInEcm();
8707 } else {
8708 return false;
8709 }
8710 } finally {
8711 Binder.restoreCallingIdentity(identity);
Shuo Qiandd210312017-04-12 22:11:33 +00008712 }
8713 }
Nathan Harold46b42aa2017-03-10 19:38:22 -08008714
8715 /**
8716 * Get the current signal strength information for the given subscription.
8717 * Because this information is not updated when the device is in a low power state
8718 * it should not be relied-upon to be current.
8719 * @param subId Subscription index
8720 * @return the most recent cached signal strength info from the modem
8721 */
8722 @Override
8723 public SignalStrength getSignalStrength(int subId) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008724 final long identity = Binder.clearCallingIdentity();
8725 try {
8726 Phone p = getPhone(subId);
8727 if (p == null) {
8728 return null;
8729 }
Nathan Harold46b42aa2017-03-10 19:38:22 -08008730
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008731 return p.getSignalStrength();
8732 } finally {
8733 Binder.restoreCallingIdentity(identity);
8734 }
Nathan Harold46b42aa2017-03-10 19:38:22 -08008735 }
Holly Jiuyu Sun01c47ad2018-01-24 17:56:33 +00008736
Pengquan Meng77b7f132018-08-22 14:49:57 -07008737 /**
Chen Xuf792fd62018-10-17 17:54:36 +00008738 * Get the current modem radio state for the given slot.
8739 * @param slotIndex slot index.
8740 * @param callingPackage the name of the package making the call.
Philip P. Moltmann700a9592019-10-03 11:53:50 -07008741 * @param callingFeatureId The feature in the package.
Chen Xuf792fd62018-10-17 17:54:36 +00008742 * @return the current radio power state from the modem
8743 */
8744 @Override
Philip P. Moltmann700a9592019-10-03 11:53:50 -07008745 public int getRadioPowerState(int slotIndex, String callingPackage, String callingFeatureId) {
Chen Xuf792fd62018-10-17 17:54:36 +00008746 Phone phone = PhoneFactory.getPhone(slotIndex);
8747 if (phone != null) {
Philip P. Moltmann700a9592019-10-03 11:53:50 -07008748 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(mApp, phone.getSubId(),
8749 callingPackage, callingFeatureId, "getRadioPowerState")) {
Chen Xuf792fd62018-10-17 17:54:36 +00008750 return TelephonyManager.RADIO_POWER_UNAVAILABLE;
8751 }
8752
8753 final long identity = Binder.clearCallingIdentity();
8754 try {
8755 return phone.getRadioPowerState();
8756 } finally {
8757 Binder.restoreCallingIdentity(identity);
8758 }
8759 }
8760 return TelephonyManager.RADIO_POWER_UNAVAILABLE;
8761 }
8762
8763 /**
Pengquan Meng77b7f132018-08-22 14:49:57 -07008764 * Checks if data roaming is enabled on the subscription with id {@code subId}.
8765 *
8766 * <p>Requires one of the following permissions:
8767 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE},
Sooraj Sasindran5d051bf2021-11-05 13:14:15 -07008768 * {@link android.Manifest.permission#READ_BASIC_PHONE_STATE},
Pengquan Meng77b7f132018-08-22 14:49:57 -07008769 * {@link android.Manifest.permission#READ_PHONE_STATE} or that the calling app has carrier
8770 * privileges.
8771 *
8772 * @param subId subscription id
8773 * @return {@code true} if data roaming is enabled on this subscription, otherwise return
8774 * {@code false}.
8775 */
8776 @Override
8777 public boolean isDataRoamingEnabled(int subId) {
Sooraj Sasindran5d051bf2021-11-05 13:14:15 -07008778 String functionName = "isDataRoamingEnabled";
Shuo Qian093013d2020-08-13 15:42:55 -07008779 try {
Sooraj Sasindran5d051bf2021-11-05 13:14:15 -07008780 try {
8781 mApp.enforceCallingOrSelfPermission(
8782 android.Manifest.permission.ACCESS_NETWORK_STATE,
8783 functionName);
8784 } catch (Exception e) {
8785 mApp.enforceCallingOrSelfPermission(
8786 permission.READ_BASIC_PHONE_STATE, functionName);
8787 }
Shuo Qian093013d2020-08-13 15:42:55 -07008788 } catch (Exception e) {
Nathan Harold62c68512021-04-06 11:26:02 -07008789 TelephonyPermissions.enforceCallingOrSelfReadPhoneStatePermissionOrCarrierPrivilege(
Sooraj Sasindran5d051bf2021-11-05 13:14:15 -07008790 mApp, subId, functionName);
Shuo Qian093013d2020-08-13 15:42:55 -07008791 }
Pengquan Meng44e66f12019-04-01 10:48:20 -07008792
Pengquan Menga1bb6272018-09-06 09:59:22 -07008793 boolean isEnabled = false;
8794 final long identity = Binder.clearCallingIdentity();
Pengquan Meng77b7f132018-08-22 14:49:57 -07008795 try {
Pengquan Menga1bb6272018-09-06 09:59:22 -07008796 Phone phone = getPhone(subId);
8797 isEnabled = phone != null ? phone.getDataRoamingEnabled() : false;
Pengquan Menga1bb6272018-09-06 09:59:22 -07008798 } finally {
8799 Binder.restoreCallingIdentity(identity);
Pengquan Meng77b7f132018-08-22 14:49:57 -07008800 }
Pengquan Menga1bb6272018-09-06 09:59:22 -07008801 return isEnabled;
Pengquan Meng77b7f132018-08-22 14:49:57 -07008802 }
8803
8804
8805 /**
8806 * Enables/Disables the data roaming on the subscription with id {@code subId}.
8807 *
8808 * <p> Requires permission:
8809 * {@link android.Manifest.permission#MODIFY_PHONE_STATE} or that the calling app has carrier
8810 * privileges.
8811 *
8812 * @param subId subscription id
8813 * @param isEnabled {@code true} means enable, {@code false} means disable.
8814 */
8815 @Override
8816 public void setDataRoamingEnabled(int subId, boolean isEnabled) {
Pengquan Meng44e66f12019-04-01 10:48:20 -07008817 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
8818 mApp, subId, "setDataRoamingEnabled");
8819
Pengquan Menga1bb6272018-09-06 09:59:22 -07008820 final long identity = Binder.clearCallingIdentity();
8821 try {
Pengquan Menga1bb6272018-09-06 09:59:22 -07008822 Phone phone = getPhone(subId);
8823 if (phone != null) {
8824 phone.setDataRoamingEnabled(isEnabled);
8825 }
8826 } finally {
8827 Binder.restoreCallingIdentity(identity);
Pengquan Meng77b7f132018-08-22 14:49:57 -07008828 }
8829 }
8830
Holly Jiuyu Sun01c47ad2018-01-24 17:56:33 +00008831 @Override
Pengquan Meng6884a2c2018-10-03 12:19:13 -07008832 public boolean isManualNetworkSelectionAllowed(int subId) {
tom hsuc91afc72020-01-06 23:46:07 +08008833 TelephonyPermissions
Nathan Harold62c68512021-04-06 11:26:02 -07008834 .enforceCallingOrSelfReadPrivilegedPhoneStatePermissionOrCarrierPrivilege(
Pengquan Meng44e66f12019-04-01 10:48:20 -07008835 mApp, subId, "isManualNetworkSelectionAllowed");
8836
Pengquan Meng6884a2c2018-10-03 12:19:13 -07008837 boolean isAllowed = true;
8838 final long identity = Binder.clearCallingIdentity();
8839 try {
Pengquan Meng6884a2c2018-10-03 12:19:13 -07008840 Phone phone = getPhone(subId);
8841 if (phone != null) {
8842 isAllowed = phone.isCspPlmnEnabled();
8843 }
8844 } finally {
8845 Binder.restoreCallingIdentity(identity);
8846 }
8847 return isAllowed;
8848 }
8849
Muralidhar Reddyeb809e32021-11-19 03:07:54 +00008850 private boolean haveCarrierPrivilegeAccess(UiccPort port, String callingPackage) {
8851 UiccProfile profile = port.getUiccProfile();
8852 if (profile == null ||
8853 profile.getCarrierPrivilegeStatus(mApp.getPackageManager(), callingPackage)
8854 != TelephonyManager.CARRIER_PRIVILEGE_STATUS_HAS_ACCESS) {
8855 return false;
Muralidhar Reddy472c2ae2021-09-29 19:38:40 +00008856 }
8857 return true;
8858 }
8859
Pengquan Meng6884a2c2018-10-03 12:19:13 -07008860 @Override
Jordan Liu75f43ea2019-01-17 16:56:37 -08008861 public List<UiccCardInfo> getUiccCardsInfo(String callingPackage) {
sandeepjsa208e3b2021-11-17 04:05:58 +00008862 // Verify that the callingPackage belongs to the calling UID
Jordan Liu4cda4552020-03-23 11:55:07 -07008863 mApp.getSystemService(AppOpsManager.class)
8864 .checkPackage(Binder.getCallingUid(), callingPackage);
8865
Jordan Liu1e142fc2019-04-22 15:10:43 -07008866 boolean hasReadPermission = false;
sandeepjsb6c87872021-09-27 15:34:44 +00008867 boolean isIccIdAccessRestricted = false;
Jordan Liuc65bc952019-02-12 17:54:02 -08008868 try {
8869 enforceReadPrivilegedPermission("getUiccCardsInfo");
Jordan Liu1e142fc2019-04-22 15:10:43 -07008870 hasReadPermission = true;
Jordan Liuc65bc952019-02-12 17:54:02 -08008871 } catch (SecurityException e) {
8872 // even without READ_PRIVILEGED_PHONE_STATE, we allow the call to continue if the caller
8873 // has carrier privileges on an active UICC
8874 if (checkCarrierPrivilegesForPackageAnyPhone(callingPackage)
8875 != TelephonyManager.CARRIER_PRIVILEGE_STATUS_HAS_ACCESS) {
Jordan Liu1e142fc2019-04-22 15:10:43 -07008876 throw new SecurityException("Caller does not have permission.");
Jordan Liuc65bc952019-02-12 17:54:02 -08008877 }
Jordan Liu75f43ea2019-01-17 16:56:37 -08008878 }
sandeepjsb6c87872021-09-27 15:34:44 +00008879 // checking compatibility, if calling app's target SDK is T and beyond.
8880 if (CompatChanges.isChangeEnabled(GET_API_SIGNATURES_FROM_UICC_PORT_INFO,
8881 Binder.getCallingUid())) {
8882 isIccIdAccessRestricted = true;
8883 }
Jordan Liu5aa07002018-12-18 15:44:48 -08008884 final long identity = Binder.clearCallingIdentity();
8885 try {
Jordan Liu75f43ea2019-01-17 16:56:37 -08008886 UiccController uiccController = UiccController.getInstance();
8887 ArrayList<UiccCardInfo> cardInfos = uiccController.getAllUiccCardInfos();
Jordan Liu1e142fc2019-04-22 15:10:43 -07008888 if (hasReadPermission) {
8889 return cardInfos;
Jordan Liu75f43ea2019-01-17 16:56:37 -08008890 }
Jordan Liu1e142fc2019-04-22 15:10:43 -07008891
8892 // Remove private info if the caller doesn't have access
8893 ArrayList<UiccCardInfo> filteredInfos = new ArrayList<>();
8894 for (UiccCardInfo cardInfo : cardInfos) {
sandeepjsb6c87872021-09-27 15:34:44 +00008895 //setting the value after compatibility check
8896 cardInfo.setIccIdAccessRestricted(isIccIdAccessRestricted);
Jordan Liu1e142fc2019-04-22 15:10:43 -07008897 // For an inactive eUICC, the UiccCard will be null even though the UiccCardInfo
8898 // is available
sandeepjsb6c87872021-09-27 15:34:44 +00008899 UiccCard card = uiccController.getUiccCardForSlot(cardInfo.getPhysicalSlotIndex());
Muralidhar Reddyeb809e32021-11-19 03:07:54 +00008900 if (card == null) {
8901 // assume no access if the card is unavailable
sandeepjsb6c87872021-09-27 15:34:44 +00008902 filteredInfos.add(getUiccCardInfoUnPrivileged(cardInfo));
Jordan Liu1e142fc2019-04-22 15:10:43 -07008903 continue;
8904 }
Muralidhar Reddyeb809e32021-11-19 03:07:54 +00008905 Collection<UiccPortInfo> portInfos = cardInfo.getPorts();
8906 if (portInfos.isEmpty()) {
sandeepjsb6c87872021-09-27 15:34:44 +00008907 filteredInfos.add(getUiccCardInfoUnPrivileged(cardInfo));
Muralidhar Reddyeb809e32021-11-19 03:07:54 +00008908 continue;
Jordan Liu1e142fc2019-04-22 15:10:43 -07008909 }
Muralidhar Reddyeb809e32021-11-19 03:07:54 +00008910 List<UiccPortInfo> uiccPortInfos = new ArrayList<>();
8911 for (UiccPortInfo portInfo : portInfos) {
8912 UiccPort port = uiccController.getUiccPortForSlot(
8913 cardInfo.getPhysicalSlotIndex(), portInfo.getPortIndex());
8914 if (port == null) {
8915 // assume no access if port is null
8916 uiccPortInfos.add(getUiccPortInfoUnPrivileged(portInfo));
8917 continue;
8918 }
8919 if (haveCarrierPrivilegeAccess(port, callingPackage)) {
8920 uiccPortInfos.add(portInfo);
8921 } else {
8922 uiccPortInfos.add(getUiccPortInfoUnPrivileged(portInfo));
8923 }
8924 }
8925 filteredInfos.add(new UiccCardInfo(
8926 cardInfo.isEuicc(),
8927 cardInfo.getCardId(),
8928 null,
8929 cardInfo.getPhysicalSlotIndex(),
8930 cardInfo.isRemovable(),
8931 cardInfo.isMultipleEnabledProfilesSupported(),
8932 uiccPortInfos));
Jordan Liu1e142fc2019-04-22 15:10:43 -07008933 }
8934 return filteredInfos;
Jordan Liu5aa07002018-12-18 15:44:48 -08008935 } finally {
8936 Binder.restoreCallingIdentity(identity);
8937 }
8938 }
8939
sandeepjsb6c87872021-09-27 15:34:44 +00008940 /**
8941 * Returns a copy of the UiccCardinfo with the EID and ICCID set to null. These values are
8942 * generally private and require carrier privileges to view.
8943 *
8944 * @hide
8945 */
8946 @NonNull
8947 public UiccCardInfo getUiccCardInfoUnPrivileged(UiccCardInfo cardInfo) {
8948 List<UiccPortInfo> portinfo = new ArrayList<>();
8949 for (UiccPortInfo portinfos : cardInfo.getPorts()) {
8950 portinfo.add(getUiccPortInfoUnPrivileged(portinfos));
8951 }
8952 return new UiccCardInfo(
8953 cardInfo.isEuicc(),
8954 cardInfo.getCardId(),
8955 null,
8956 cardInfo.getPhysicalSlotIndex(),
8957 cardInfo.isRemovable(),
8958 cardInfo.isMultipleEnabledProfilesSupported(),
8959 portinfo
8960 );
8961 }
Holly Jiuyu Sun01c47ad2018-01-24 17:56:33 +00008962
sandeepjsb6c87872021-09-27 15:34:44 +00008963 /**
8964 * @hide
8965 * @return a copy of the UiccPortInfo with ICCID set to {@link UiccPortInfo#ICCID_REDACTED}.
8966 * These values are generally private and require carrier privileges to view.
8967 */
8968 @NonNull
8969 public UiccPortInfo getUiccPortInfoUnPrivileged(UiccPortInfo portInfo) {
8970 return new UiccPortInfo(
8971 UiccPortInfo.ICCID_REDACTED,
8972 portInfo.getPortIndex(),
8973 portInfo.getLogicalSlotIndex(),
8974 portInfo.isActive()
8975 );
8976 }
8977 @Override
8978 public UiccSlotInfo[] getUiccSlotsInfo(String callingPackage) {
sandeepjsa208e3b2021-11-17 04:05:58 +00008979 // Verify that the callingPackage belongs to the calling UID
sandeepjsb6c87872021-09-27 15:34:44 +00008980 mApp.getSystemService(AppOpsManager.class)
8981 .checkPackage(Binder.getCallingUid(), callingPackage);
8982
8983 boolean hasReadPermission = false;
8984 boolean isLogicalSlotAccessRestricted = false;
sandeepjsb6c87872021-09-27 15:34:44 +00008985
8986 try {
8987 enforceReadPrivilegedPermission("getUiccSlotsInfo");
8988 hasReadPermission = true;
8989 } catch (SecurityException e) {
8990 // even without READ_PRIVILEGED_PHONE_STATE, we allow the call to continue if the caller
8991 // has carrier privileges on an active UICC
8992 if (checkCarrierPrivilegesForPackageAnyPhone(callingPackage)
8993 == TelephonyManager.CARRIER_PRIVILEGE_STATUS_HAS_ACCESS) {
8994 hasReadPermission = true;
8995 }
8996 }
8997
8998 // checking compatibility, if calling app's target SDK is T and beyond.
8999 if (CompatChanges.isChangeEnabled(GET_API_SIGNATURES_FROM_UICC_PORT_INFO,
9000 Binder.getCallingUid())) {
9001 isLogicalSlotAccessRestricted = true;
9002 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08009003 final long identity = Binder.clearCallingIdentity();
9004 try {
9005 UiccSlot[] slots = UiccController.getInstance().getUiccSlots();
9006 if (slots == null) {
9007 Rlog.i(LOG_TAG, "slots is null.");
9008 return null;
9009 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08009010 UiccSlotInfo[] infos = new UiccSlotInfo[slots.length];
9011 for (int i = 0; i < slots.length; i++) {
9012 UiccSlot slot = slots[i];
9013 if (slot == null) {
9014 continue;
9015 }
9016
Jordan Liu7be7e652019-05-06 18:55:02 +00009017 String cardId;
Malcolm Chenaa4a8532018-02-28 15:00:40 -08009018 UiccCard card = slot.getUiccCard();
9019 if (card != null) {
9020 cardId = card.getCardId();
Jordan Liu7be7e652019-05-06 18:55:02 +00009021 } else {
Jordan Liu01bd00d2019-09-12 16:19:43 -07009022 cardId = slot.getEid();
9023 if (TextUtils.isEmpty(cardId)) {
Muralidhar Reddyeb809e32021-11-19 03:07:54 +00009024 // If cardId is null, use iccId of default port as cardId. Check if has
9025 // read permission otherwise set to null.(card is null which means no
9026 // carrier permission)
9027 cardId = hasReadPermission ? slot.getIccId(
9028 TelephonyManager.DEFAULT_PORT_INDEX) : null;
Jordan Liu01bd00d2019-09-12 16:19:43 -07009029 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08009030 }
9031
Jordan Liu857451f2019-05-09 16:35:35 -07009032 if (cardId != null) {
9033 // if cardId is an ICCID, strip off trailing Fs before exposing to user
9034 // if cardId is an EID, it's all digits so this is fine
9035 cardId = IccUtils.stripTrailingFs(cardId);
9036 }
9037
Malcolm Chenaa4a8532018-02-28 15:00:40 -08009038 int cardState = 0;
9039 switch (slot.getCardState()) {
9040 case CARDSTATE_ABSENT:
9041 cardState = UiccSlotInfo.CARD_STATE_INFO_ABSENT;
9042 break;
9043 case CARDSTATE_PRESENT:
9044 cardState = UiccSlotInfo.CARD_STATE_INFO_PRESENT;
9045 break;
9046 case CARDSTATE_ERROR:
9047 cardState = UiccSlotInfo.CARD_STATE_INFO_ERROR;
9048 break;
9049 case CARDSTATE_RESTRICTED:
9050 cardState = UiccSlotInfo.CARD_STATE_INFO_RESTRICTED;
9051 break;
9052 default:
9053 break;
9054
9055 }
Muralidhar Reddyeb809e32021-11-19 03:07:54 +00009056 List<UiccPortInfo> portInfos = new ArrayList<>();
9057 int[] portIndexes = slot.getPortList();
9058 for (int portIdx : portIndexes) {
9059 String iccId = IccUtils.stripTrailingFs(getIccId(slot, portIdx,
9060 callingPackage, hasReadPermission));
9061 if (slot.isPortActive(portIdx)) {
9062 UiccPort port = slot.getUiccCard().getUiccPort(portIdx);
9063 portInfos.add(new UiccPortInfo(iccId, port.getPortIdx(),
9064 port.getPhoneId(), true));
9065 } else {
9066 portInfos.add(new UiccPortInfo(iccId, portIdx, -1, false));
9067 }
9068 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08009069 infos[i] = new UiccSlotInfo(
Malcolm Chenaa4a8532018-02-28 15:00:40 -08009070 slot.isEuicc(),
9071 cardId,
9072 cardState,
Jordan Liua2619582019-02-14 12:56:40 -08009073 slot.isExtendedApduSupported(),
Muralidhar Reddyeb809e32021-11-19 03:07:54 +00009074 slot.isRemovable(), portInfos);
sandeepjsb6c87872021-09-27 15:34:44 +00009075 //setting the value after compatibility check
9076 infos[i].setLogicalSlotAccessRestricted(isLogicalSlotAccessRestricted);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08009077 }
9078 return infos;
9079 } finally {
9080 Binder.restoreCallingIdentity(identity);
Holly Jiuyu Sun1d957c52018-04-04 13:52:42 -07009081 }
Holly Jiuyu Sun01c47ad2018-01-24 17:56:33 +00009082 }
9083
Muralidhar Reddyeb809e32021-11-19 03:07:54 +00009084 /* Returns null if doesn't have read permission or carrier privilege access. */
9085 private String getIccId(UiccSlot slot, int portIndex, String callingPackage,
9086 boolean hasReadPermission) {
9087 String iccId = slot.getIccId(portIndex);
9088 if (hasReadPermission) { // if has read permission
9089 return iccId;
9090 } else {
9091 if (slot.getUiccCard() != null && slot.getUiccCard().getUiccPort(portIndex) != null) {
9092 UiccPort port = slot.getUiccCard().getUiccPort(portIndex);
9093 // if no read permission, checking carrier privilege access
9094 if (haveCarrierPrivilegeAccess(port, callingPackage)) {
9095 return iccId;
9096 }
9097 }
9098 }
9099 // No read permission or carrier privilege access.
9100 return UiccPortInfo.ICCID_REDACTED;
9101 }
9102
Holly Jiuyu Sun01c47ad2018-01-24 17:56:33 +00009103 @Override
sandeepjsb6c87872021-09-27 15:34:44 +00009104 @Deprecated
Holly Jiuyu Sun01c47ad2018-01-24 17:56:33 +00009105 public boolean switchSlots(int[] physicalSlots) {
9106 enforceModifyPermission();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08009107
9108 final long identity = Binder.clearCallingIdentity();
9109 try {
Muralidhar Reddyeb809e32021-11-19 03:07:54 +00009110 List<UiccSlotMapping> slotMappings = new ArrayList<>();
9111 for (int i = 0; i < physicalSlots.length; i++) {
9112 // Deprecated API, hence MEP is not supported. Adding default portIndex 0.
9113 slotMappings.add(new UiccSlotMapping(TelephonyManager.DEFAULT_PORT_INDEX,
9114 physicalSlots[i], i));
9115 }
9116 return (Boolean) sendRequest(CMD_SWITCH_SLOTS, slotMappings);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08009117 } finally {
9118 Binder.restoreCallingIdentity(identity);
9119 }
Holly Jiuyu Sun01c47ad2018-01-24 17:56:33 +00009120 }
Jack Yu4c988042018-02-27 15:30:01 -08009121
9122 @Override
sandeepjsb6c87872021-09-27 15:34:44 +00009123 @RequiresPermission(android.Manifest.permission.MODIFY_PHONE_STATE)
9124 public boolean setSimSlotMapping(@NonNull List<UiccSlotMapping> slotMapping) {
9125 enforceModifyPermission();
9126
9127 final long identity = Binder.clearCallingIdentity();
9128 try {
Muralidhar Reddyeb809e32021-11-19 03:07:54 +00009129 return (Boolean) sendRequest(CMD_SWITCH_SLOTS, slotMapping);
sandeepjsb6c87872021-09-27 15:34:44 +00009130 } finally {
9131 Binder.restoreCallingIdentity(identity);
9132 }
9133 }
9134
9135 @Override
Jordan Liu7de49fa2018-12-06 14:48:49 -08009136 public int getCardIdForDefaultEuicc(int subId, String callingPackage) {
Jordan Liu7de49fa2018-12-06 14:48:49 -08009137 final long identity = Binder.clearCallingIdentity();
9138 try {
9139 return UiccController.getInstance().getCardIdForDefaultEuicc();
9140 } finally {
9141 Binder.restoreCallingIdentity(identity);
9142 }
9143 }
9144
Pengquan Meng85728fb2018-03-12 16:31:21 -07009145 /**
goneil47ffb6e2018-04-06 15:40:58 -07009146 * A test API to reload the UICC profile.
9147 *
9148 * <p>Requires that the calling app has permission
9149 * {@link android.Manifest.permission#MODIFY_PHONE_STATE MODIFY_PHONE_STATE}.
9150 * @hide
9151 */
9152 @Override
9153 public void refreshUiccProfile(int subId) {
9154 enforceModifyPermission();
9155
9156 final long identity = Binder.clearCallingIdentity();
9157 try {
9158 Phone phone = getPhone(subId);
9159 if (phone == null) {
9160 return;
9161 }
Muralidhar Reddy472c2ae2021-09-29 19:38:40 +00009162 UiccPort uiccPort = phone.getUiccPort();
9163 if (uiccPort == null) {
goneil47ffb6e2018-04-06 15:40:58 -07009164 return;
9165 }
Muralidhar Reddy472c2ae2021-09-29 19:38:40 +00009166 UiccProfile uiccProfile = uiccPort.getUiccProfile();
goneil47ffb6e2018-04-06 15:40:58 -07009167 if (uiccProfile == null) {
9168 return;
9169 }
9170 uiccProfile.refresh();
9171 } finally {
9172 Binder.restoreCallingIdentity(identity);
9173 }
9174 }
9175
9176 /**
Pengquan Meng85728fb2018-03-12 16:31:21 -07009177 * Returns false if the mobile data is disabled by default, otherwise return true.
9178 */
9179 private boolean getDefaultDataEnabled() {
Inseob Kim14bb3d02018-12-13 17:11:34 +09009180 return TelephonyProperties.mobile_data().orElse(true);
Pengquan Meng85728fb2018-03-12 16:31:21 -07009181 }
9182
9183 /**
9184 * Returns true if the data roaming is enabled by default, i.e the system property
9185 * of {@link #DEFAULT_DATA_ROAMING_PROPERTY_NAME} is true or the config of
9186 * {@link CarrierConfigManager#KEY_CARRIER_DEFAULT_DATA_ROAMING_ENABLED_BOOL} is true.
9187 */
9188 private boolean getDefaultDataRoamingEnabled(int subId) {
9189 final CarrierConfigManager configMgr = (CarrierConfigManager)
Nazanin Bakhshif782e562018-12-11 15:15:39 -08009190 mApp.getSystemService(Context.CARRIER_CONFIG_SERVICE);
Shuo Qian1d84a0e2020-07-15 12:36:44 -07009191 boolean isDataRoamingEnabled = TelephonyProperties.data_roaming().orElse(false);
Pengquan Meng85728fb2018-03-12 16:31:21 -07009192 isDataRoamingEnabled |= configMgr.getConfigForSubId(subId).getBoolean(
9193 CarrierConfigManager.KEY_CARRIER_DEFAULT_DATA_ROAMING_ENABLED_BOOL);
9194 return isDataRoamingEnabled;
9195 }
9196
9197 /**
9198 * Returns the default network type for the given {@code subId}, if the default network type is
9199 * not set, return {@link Phone#PREFERRED_NT_MODE}.
9200 */
9201 private int getDefaultNetworkType(int subId) {
Inseob Kim14bb3d02018-12-13 17:11:34 +09009202 List<Integer> list = TelephonyProperties.default_network();
9203 int phoneId = mSubscriptionController.getPhoneId(subId);
9204 if (phoneId >= 0 && phoneId < list.size() && list.get(phoneId) != null) {
9205 return list.get(phoneId);
9206 }
9207 return Phone.PREFERRED_NT_MODE;
Pengquan Meng85728fb2018-03-12 16:31:21 -07009208 }
fionaxua13278b2018-03-21 00:08:13 -07009209
9210 @Override
9211 public void setCarrierTestOverride(int subId, String mccmnc, String imsi, String iccid, String
chen xueaba88a2019-03-15 13:15:10 -07009212 gid1, String gid2, String plmn, String spn, String carrierPrivilegeRules, String apn) {
fionaxua13278b2018-03-21 00:08:13 -07009213 enforceModifyPermission();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08009214
9215 final long identity = Binder.clearCallingIdentity();
9216 try {
9217 final Phone phone = getPhone(subId);
9218 if (phone == null) {
9219 loge("setCarrierTestOverride fails with invalid subId: " + subId);
9220 return;
9221 }
Rambo Wang9c9ffdd2022-01-13 21:51:44 -08009222 CarrierPrivilegesTracker cpt = phone.getCarrierPrivilegesTracker();
9223 if (cpt != null) {
9224 cpt.setTestOverrideCarrierPrivilegeRules(carrierPrivilegeRules);
9225 }
9226 // TODO(b/211796398): remove the legacy logic below once CPT migration is done.
chen xueaba88a2019-03-15 13:15:10 -07009227 phone.setCarrierTestOverride(mccmnc, imsi, iccid, gid1, gid2, plmn, spn,
9228 carrierPrivilegeRules, apn);
Jeff Davidson8ab02b22020-03-28 12:24:40 -07009229 if (carrierPrivilegeRules == null) {
9230 mCarrierPrivilegeTestOverrideSubIds.remove(subId);
9231 } else {
9232 mCarrierPrivilegeTestOverrideSubIds.add(subId);
9233 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08009234 } finally {
9235 Binder.restoreCallingIdentity(identity);
fionaxua13278b2018-03-21 00:08:13 -07009236 }
fionaxua13278b2018-03-21 00:08:13 -07009237 }
9238
9239 @Override
9240 public int getCarrierIdListVersion(int subId) {
Brad Ebinger35c841c2018-10-01 10:40:55 -07009241 enforceReadPrivilegedPermission("getCarrierIdListVersion");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08009242
9243 final long identity = Binder.clearCallingIdentity();
9244 try {
9245 final Phone phone = getPhone(subId);
9246 if (phone == null) {
9247 loge("getCarrierIdListVersion fails with invalid subId: " + subId);
9248 return TelephonyManager.UNKNOWN_CARRIER_ID_LIST_VERSION;
9249 }
9250 return phone.getCarrierIdListVersion();
9251 } finally {
9252 Binder.restoreCallingIdentity(identity);
fionaxua13278b2018-03-21 00:08:13 -07009253 }
fionaxua13278b2018-03-21 00:08:13 -07009254 }
Malcolm Chen2c63d402018-08-14 16:00:53 -07009255
9256 @Override
Philip P. Moltmann700a9592019-10-03 11:53:50 -07009257 public int getNumberOfModemsWithSimultaneousDataConnections(int subId, String callingPackage,
9258 String callingFeatureId) {
Malcolm Chen2c63d402018-08-14 16:00:53 -07009259 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
Philip P. Moltmann700a9592019-10-03 11:53:50 -07009260 mApp, subId, callingPackage, callingFeatureId,
9261 "getNumberOfModemsWithSimultaneousDataConnections")) {
Malcolm Chen2c63d402018-08-14 16:00:53 -07009262 return -1;
9263 }
9264
9265 final long identity = Binder.clearCallingIdentity();
9266 try {
9267 return mPhoneConfigurationManager.getNumberOfModemsWithSimultaneousDataConnections();
9268 } finally {
9269 Binder.restoreCallingIdentity(identity);
9270 }
9271 }
Pengquan Menga1bb6272018-09-06 09:59:22 -07009272
9273 @Override
9274 public int getCdmaRoamingMode(int subId) {
zoey chen7e6d4e52019-12-17 18:18:59 +08009275 TelephonyPermissions
Nathan Harold62c68512021-04-06 11:26:02 -07009276 .enforceCallingOrSelfReadPrivilegedPhoneStatePermissionOrCarrierPrivilege(
Pengquan Menga1bb6272018-09-06 09:59:22 -07009277 mApp, subId, "getCdmaRoamingMode");
9278
9279 final long identity = Binder.clearCallingIdentity();
9280 try {
9281 return (int) sendRequest(CMD_GET_CDMA_ROAMING_MODE, null /* argument */, subId);
9282 } finally {
9283 Binder.restoreCallingIdentity(identity);
9284 }
9285 }
9286
9287 @Override
9288 public boolean setCdmaRoamingMode(int subId, int mode) {
9289 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
9290 mApp, subId, "setCdmaRoamingMode");
9291
9292 final long identity = Binder.clearCallingIdentity();
9293 try {
9294 return (boolean) sendRequest(CMD_SET_CDMA_ROAMING_MODE, mode, subId);
9295 } finally {
9296 Binder.restoreCallingIdentity(identity);
9297 }
9298 }
9299
9300 @Override
Sarah Chinbaab1432020-10-28 13:46:24 -07009301 public int getCdmaSubscriptionMode(int subId) {
9302 TelephonyPermissions
Nathan Harold62c68512021-04-06 11:26:02 -07009303 .enforceCallingOrSelfReadPrivilegedPhoneStatePermissionOrCarrierPrivilege(
Sarah Chinbaab1432020-10-28 13:46:24 -07009304 mApp, subId, "getCdmaSubscriptionMode");
9305
9306 final long identity = Binder.clearCallingIdentity();
9307 try {
9308 return (int) sendRequest(CMD_GET_CDMA_SUBSCRIPTION_MODE, null /* argument */, subId);
9309 } finally {
9310 Binder.restoreCallingIdentity(identity);
9311 }
9312 }
9313
9314 @Override
Pengquan Menga1bb6272018-09-06 09:59:22 -07009315 public boolean setCdmaSubscriptionMode(int subId, int mode) {
9316 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
9317 mApp, subId, "setCdmaSubscriptionMode");
9318
9319 final long identity = Binder.clearCallingIdentity();
9320 try {
9321 return (boolean) sendRequest(CMD_SET_CDMA_SUBSCRIPTION_MODE, mode, subId);
9322 } finally {
9323 Binder.restoreCallingIdentity(identity);
9324 }
9325 }
Makoto Onukida3bf792018-09-18 16:06:29 -07009326
sqianc5eccab2018-10-19 18:46:41 -07009327 @Override
sqian8c685422019-02-22 15:55:18 -08009328 public Map<Integer, List<EmergencyNumber>> getEmergencyNumberList(
Philip P. Moltmann700a9592019-10-03 11:53:50 -07009329 String callingPackage, String callingFeatureId) {
sqian11b7a0e2018-12-05 18:48:28 -08009330 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
Philip P. Moltmann700a9592019-10-03 11:53:50 -07009331 mApp, getDefaultSubscription(), callingPackage, callingFeatureId,
9332 "getEmergencyNumberList")) {
sqian11b7a0e2018-12-05 18:48:28 -08009333 throw new SecurityException("Requires READ_PHONE_STATE permission.");
9334 }
9335 final long identity = Binder.clearCallingIdentity();
9336 try {
sqian854d44b2018-12-12 16:48:18 -08009337 Map<Integer, List<EmergencyNumber>> emergencyNumberListInternal = new HashMap<>();
9338 for (Phone phone: PhoneFactory.getPhones()) {
9339 if (phone.getEmergencyNumberTracker() != null
9340 && phone.getEmergencyNumberTracker().getEmergencyNumberList() != null) {
9341 emergencyNumberListInternal.put(
9342 phone.getSubId(),
9343 phone.getEmergencyNumberTracker().getEmergencyNumberList());
9344 }
sqian11b7a0e2018-12-05 18:48:28 -08009345 }
sqian854d44b2018-12-12 16:48:18 -08009346 return emergencyNumberListInternal;
sqian11b7a0e2018-12-05 18:48:28 -08009347 } finally {
9348 Binder.restoreCallingIdentity(identity);
9349 }
sqianc5eccab2018-10-19 18:46:41 -07009350 }
9351
9352 @Override
sqian8c685422019-02-22 15:55:18 -08009353 public boolean isEmergencyNumber(String number, boolean exactMatch) {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08009354 final Phone defaultPhone = getDefaultPhone();
sqian11b7a0e2018-12-05 18:48:28 -08009355 if (!exactMatch) {
9356 TelephonyPermissions
Nathan Harold62c68512021-04-06 11:26:02 -07009357 .enforceCallingOrSelfReadPrivilegedPhoneStatePermissionOrCarrierPrivilege(
sqian8c685422019-02-22 15:55:18 -08009358 mApp, defaultPhone.getSubId(), "isEmergencyNumber(Potential)");
sqian11b7a0e2018-12-05 18:48:28 -08009359 }
9360 final long identity = Binder.clearCallingIdentity();
9361 try {
sqian854d44b2018-12-12 16:48:18 -08009362 for (Phone phone: PhoneFactory.getPhones()) {
9363 if (phone.getEmergencyNumberTracker() != null
Taesu Leee050c002020-10-13 17:19:35 +09009364 && phone.getEmergencyNumberTracker()
9365 .isEmergencyNumber(number, exactMatch)) {
9366 return true;
sqian11b7a0e2018-12-05 18:48:28 -08009367 }
sqian11b7a0e2018-12-05 18:48:28 -08009368 }
9369 return false;
9370 } finally {
9371 Binder.restoreCallingIdentity(identity);
9372 }
9373 }
9374
sqianf4ca7ed2019-01-15 18:32:07 -08009375 /**
Shuo Qianccbaf742021-02-22 18:32:21 -08009376 * Start emergency callback mode for GsmCdmaPhone for testing.
9377 */
9378 @Override
9379 public void startEmergencyCallbackMode() {
9380 TelephonyPermissions.enforceShellOnly(Binder.getCallingUid(),
9381 "startEmergencyCallbackMode");
9382 enforceModifyPermission();
9383 final long identity = Binder.clearCallingIdentity();
9384 try {
9385 for (Phone phone : PhoneFactory.getPhones()) {
9386 Rlog.d(LOG_TAG, "startEmergencyCallbackMode phone type: " + phone.getPhoneType());
9387 if (phone != null && ((phone.getPhoneType() == PHONE_TYPE_GSM)
9388 || (phone.getPhoneType() == PHONE_TYPE_CDMA))) {
9389 GsmCdmaPhone gsmCdmaPhone = (GsmCdmaPhone) phone;
9390 gsmCdmaPhone.obtainMessage(
9391 GsmCdmaPhone.EVENT_EMERGENCY_CALLBACK_MODE_ENTER).sendToTarget();
9392 Rlog.d(LOG_TAG, "startEmergencyCallbackMode: triggered");
9393 }
9394 }
9395 } finally {
9396 Binder.restoreCallingIdentity(identity);
9397 }
9398 }
9399
9400 /**
sqianf4ca7ed2019-01-15 18:32:07 -08009401 * Update emergency number list for test mode.
9402 */
9403 @Override
9404 public void updateEmergencyNumberListTestMode(int action, EmergencyNumber num) {
9405 TelephonyPermissions.enforceShellOnly(Binder.getCallingUid(),
9406 "updateEmergencyNumberListTestMode");
9407
9408 final long identity = Binder.clearCallingIdentity();
9409 try {
9410 for (Phone phone: PhoneFactory.getPhones()) {
9411 EmergencyNumberTracker tracker = phone.getEmergencyNumberTracker();
9412 if (tracker != null) {
9413 tracker.executeEmergencyNumberTestModeCommand(action, num);
9414 }
9415 }
9416 } finally {
9417 Binder.restoreCallingIdentity(identity);
9418 }
9419 }
9420
9421 /**
9422 * Get the full emergency number list for test mode.
9423 */
9424 @Override
9425 public List<String> getEmergencyNumberListTestMode() {
9426 TelephonyPermissions.enforceShellOnly(Binder.getCallingUid(),
9427 "getEmergencyNumberListTestMode");
9428
9429 final long identity = Binder.clearCallingIdentity();
9430 try {
9431 Set<String> emergencyNumbers = new HashSet<>();
9432 for (Phone phone: PhoneFactory.getPhones()) {
9433 EmergencyNumberTracker tracker = phone.getEmergencyNumberTracker();
9434 if (tracker != null) {
9435 for (EmergencyNumber num : tracker.getEmergencyNumberList()) {
9436 emergencyNumbers.add(num.getNumber());
9437 }
9438 }
9439 }
9440 return new ArrayList<>(emergencyNumbers);
9441 } finally {
9442 Binder.restoreCallingIdentity(identity);
9443 }
9444 }
9445
chen xud6b45bd2018-10-30 22:27:10 -07009446 @Override
Shuo Qian3b6ee772019-11-13 17:43:31 -08009447 public int getEmergencyNumberDbVersion(int subId) {
9448 enforceReadPrivilegedPermission("getEmergencyNumberDbVersion");
9449
9450 final long identity = Binder.clearCallingIdentity();
9451 try {
9452 final Phone phone = getPhone(subId);
9453 if (phone == null) {
9454 loge("getEmergencyNumberDbVersion fails with invalid subId: " + subId);
9455 return TelephonyManager.INVALID_EMERGENCY_NUMBER_DB_VERSION;
9456 }
9457 return phone.getEmergencyNumberDbVersion();
9458 } finally {
9459 Binder.restoreCallingIdentity(identity);
9460 }
9461 }
9462
9463 @Override
9464 public void notifyOtaEmergencyNumberDbInstalled() {
9465 enforceModifyPermission();
9466
9467 final long identity = Binder.clearCallingIdentity();
9468 try {
9469 for (Phone phone: PhoneFactory.getPhones()) {
9470 EmergencyNumberTracker tracker = phone.getEmergencyNumberTracker();
9471 if (tracker != null) {
9472 tracker.updateOtaEmergencyNumberDatabase();
9473 }
9474 }
9475 } finally {
9476 Binder.restoreCallingIdentity(identity);
9477 }
9478 }
9479
9480 @Override
Shuo Qianc373f112020-03-05 17:55:34 -08009481 public void updateOtaEmergencyNumberDbFilePath(ParcelFileDescriptor otaParcelFileDescriptor) {
Shuo Qian3b6ee772019-11-13 17:43:31 -08009482 enforceActiveEmergencySessionPermission();
9483
9484 final long identity = Binder.clearCallingIdentity();
9485 try {
9486 for (Phone phone: PhoneFactory.getPhones()) {
9487 EmergencyNumberTracker tracker = phone.getEmergencyNumberTracker();
9488 if (tracker != null) {
Shuo Qianc373f112020-03-05 17:55:34 -08009489 tracker.updateOtaEmergencyNumberDbFilePath(otaParcelFileDescriptor);
9490 }
9491 }
9492 } finally {
9493 Binder.restoreCallingIdentity(identity);
9494 }
9495 }
9496
9497 @Override
9498 public void resetOtaEmergencyNumberDbFilePath() {
9499 enforceActiveEmergencySessionPermission();
9500
9501 final long identity = Binder.clearCallingIdentity();
9502 try {
9503 for (Phone phone: PhoneFactory.getPhones()) {
9504 EmergencyNumberTracker tracker = phone.getEmergencyNumberTracker();
9505 if (tracker != null) {
9506 tracker.resetOtaEmergencyNumberDbFilePath();
Shuo Qian3b6ee772019-11-13 17:43:31 -08009507 }
9508 }
9509 } finally {
9510 Binder.restoreCallingIdentity(identity);
9511 }
9512 }
9513
9514 @Override
chen xud6b45bd2018-10-30 22:27:10 -07009515 public List<String> getCertsFromCarrierPrivilegeAccessRules(int subId) {
9516 enforceReadPrivilegedPermission("getCertsFromCarrierPrivilegeAccessRules");
9517 Phone phone = getPhone(subId);
9518 if (phone == null) {
9519 return null;
9520 }
9521 final long identity = Binder.clearCallingIdentity();
9522 try {
9523 UiccProfile profile = UiccController.getInstance()
9524 .getUiccProfileForPhone(phone.getPhoneId());
9525 if (profile != null) {
9526 return profile.getCertsFromCarrierPrivilegeAccessRules();
9527 }
9528 } finally {
9529 Binder.restoreCallingIdentity(identity);
9530 }
9531 return null;
9532 }
Malcolm Chen8e4ed912019-01-15 20:22:16 -08009533
9534 /**
9535 * Enable or disable a modem stack.
9536 */
9537 @Override
9538 public boolean enableModemForSlot(int slotIndex, boolean enable) {
9539 enforceModifyPermission();
9540
9541 final long identity = Binder.clearCallingIdentity();
9542 try {
9543 Phone phone = PhoneFactory.getPhone(slotIndex);
9544 if (phone == null) {
9545 return false;
9546 } else {
9547 return (Boolean) sendRequest(CMD_REQUEST_ENABLE_MODEM, enable, phone, null);
9548 }
9549 } finally {
9550 Binder.restoreCallingIdentity(identity);
9551 }
9552 }
Michelecea4cf22018-12-21 15:00:11 -08009553
Malcolm Chen4bcd9822019-03-27 18:34:05 -07009554 /**
9555 * Whether a modem stack is enabled or not.
9556 */
9557 @Override
Philip P. Moltmann700a9592019-10-03 11:53:50 -07009558 public boolean isModemEnabledForSlot(int slotIndex, String callingPackage,
9559 String callingFeatureId) {
Malcolm Chen4bcd9822019-03-27 18:34:05 -07009560 Phone phone = PhoneFactory.getPhone(slotIndex);
9561 if (phone == null) return false;
9562
9563 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
Philip P. Moltmann700a9592019-10-03 11:53:50 -07009564 mApp, phone.getSubId(), callingPackage, callingFeatureId,
9565 "isModemEnabledForSlot")) {
Malcolm Chen4bcd9822019-03-27 18:34:05 -07009566 throw new SecurityException("Requires READ_PHONE_STATE permission.");
9567 }
9568
9569 final long identity = Binder.clearCallingIdentity();
9570 try {
Nazanin Bakhshif71371d2019-04-29 17:29:44 -07009571 try {
9572 return mPhoneConfigurationManager.getPhoneStatusFromCache(phone.getPhoneId());
9573 } catch (NoSuchElementException ex) {
9574 return (Boolean) sendRequest(CMD_GET_MODEM_STATUS, null, phone, null);
9575 }
Malcolm Chen4bcd9822019-03-27 18:34:05 -07009576 } finally {
9577 Binder.restoreCallingIdentity(identity);
9578 }
9579 }
9580
Michelecea4cf22018-12-21 15:00:11 -08009581 @Override
Michele0ea7d782019-03-19 14:58:42 -07009582 public void setMultiSimCarrierRestriction(boolean isMultiSimCarrierRestricted) {
Michelecea4cf22018-12-21 15:00:11 -08009583 enforceModifyPermission();
9584
9585 final long identity = Binder.clearCallingIdentity();
9586 try {
9587 mTelephonySharedPreferences.edit()
Michele0ea7d782019-03-19 14:58:42 -07009588 .putBoolean(PREF_MULTI_SIM_RESTRICTED, isMultiSimCarrierRestricted)
Michelecea4cf22018-12-21 15:00:11 -08009589 .commit();
9590 } finally {
9591 Binder.restoreCallingIdentity(identity);
9592 }
9593 }
9594
9595 @Override
Michele0ea7d782019-03-19 14:58:42 -07009596 @TelephonyManager.IsMultiSimSupportedResult
Philip P. Moltmann700a9592019-10-03 11:53:50 -07009597 public int isMultiSimSupported(String callingPackage, String callingFeatureId) {
Michele4245e952019-02-04 11:36:23 -08009598 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(mApp,
Philip P. Moltmann700a9592019-10-03 11:53:50 -07009599 getDefaultPhone().getSubId(), callingPackage, callingFeatureId,
9600 "isMultiSimSupported")) {
Michele0ea7d782019-03-19 14:58:42 -07009601 return TelephonyManager.MULTISIM_NOT_SUPPORTED_BY_HARDWARE;
Michele4245e952019-02-04 11:36:23 -08009602 }
Michelecea4cf22018-12-21 15:00:11 -08009603
9604 final long identity = Binder.clearCallingIdentity();
9605 try {
Michele0ea7d782019-03-19 14:58:42 -07009606 return isMultiSimSupportedInternal();
Michelecea4cf22018-12-21 15:00:11 -08009607 } finally {
9608 Binder.restoreCallingIdentity(identity);
9609 }
9610 }
Nazanin Bakhshi628473f2019-01-29 17:37:52 -08009611
Michele0ea7d782019-03-19 14:58:42 -07009612 @TelephonyManager.IsMultiSimSupportedResult
9613 private int isMultiSimSupportedInternal() {
Michele30b57b22019-03-01 12:01:14 -08009614 // If the device has less than 2 SIM cards, indicate that multisim is restricted.
9615 int numPhysicalSlots = UiccController.getInstance().getUiccSlots().length;
9616 if (numPhysicalSlots < 2) {
Michele0ea7d782019-03-19 14:58:42 -07009617 loge("isMultiSimSupportedInternal: requires at least 2 cards");
9618 return TelephonyManager.MULTISIM_NOT_SUPPORTED_BY_HARDWARE;
Michele30b57b22019-03-01 12:01:14 -08009619 }
9620 // Check if the hardware supports multisim functionality. If usage of multisim is not
9621 // supported by the modem, indicate that it is restricted.
9622 PhoneCapability staticCapability =
9623 mPhoneConfigurationManager.getStaticPhoneCapability();
9624 if (staticCapability == null) {
Michele0ea7d782019-03-19 14:58:42 -07009625 loge("isMultiSimSupportedInternal: no static configuration available");
9626 return TelephonyManager.MULTISIM_NOT_SUPPORTED_BY_HARDWARE;
Michele30b57b22019-03-01 12:01:14 -08009627 }
SongFerngWang8236caa2021-01-17 21:51:44 +08009628 if (staticCapability.getLogicalModemList().size() < 2) {
Michele0ea7d782019-03-19 14:58:42 -07009629 loge("isMultiSimSupportedInternal: maximum number of modem is < 2");
9630 return TelephonyManager.MULTISIM_NOT_SUPPORTED_BY_HARDWARE;
Michele30b57b22019-03-01 12:01:14 -08009631 }
9632 // Check if support of multiple SIMs is restricted by carrier
9633 if (mTelephonySharedPreferences.getBoolean(PREF_MULTI_SIM_RESTRICTED, false)) {
Michele0ea7d782019-03-19 14:58:42 -07009634 return TelephonyManager.MULTISIM_NOT_SUPPORTED_BY_CARRIER;
Michele30b57b22019-03-01 12:01:14 -08009635 }
9636
Michele0ea7d782019-03-19 14:58:42 -07009637 return TelephonyManager.MULTISIM_ALLOWED;
Michele30b57b22019-03-01 12:01:14 -08009638 }
9639
Nazanin Bakhshi628473f2019-01-29 17:37:52 -08009640 /**
9641 * Switch configs to enable multi-sim or switch back to single-sim
Nazanin Bakhshi17318782019-03-01 11:56:08 -08009642 * Note: Switch from multi-sim to single-sim is only possible with MODIFY_PHONE_STATE
9643 * permission, but the other way around is possible with either MODIFY_PHONE_STATE
9644 * or carrier privileges
Nazanin Bakhshi628473f2019-01-29 17:37:52 -08009645 * @param numOfSims number of active sims we want to switch to
9646 */
9647 @Override
9648 public void switchMultiSimConfig(int numOfSims) {
Nazanin Bakhshi17318782019-03-01 11:56:08 -08009649 if (numOfSims == 1) {
9650 enforceModifyPermission();
9651 } else {
9652 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
9653 mApp, SubscriptionManager.DEFAULT_SUBSCRIPTION_ID, "switchMultiSimConfig");
9654 }
Nazanin Bakhshi628473f2019-01-29 17:37:52 -08009655 final long identity = Binder.clearCallingIdentity();
Michele30b57b22019-03-01 12:01:14 -08009656
Nazanin Bakhshi628473f2019-01-29 17:37:52 -08009657 try {
Michele30b57b22019-03-01 12:01:14 -08009658 //only proceed if multi-sim is not restricted
Michele0ea7d782019-03-19 14:58:42 -07009659 if (isMultiSimSupportedInternal() != TelephonyManager.MULTISIM_ALLOWED) {
Michele30b57b22019-03-01 12:01:14 -08009660 loge("switchMultiSimConfig not possible. It is restricted or not supported.");
9661 return;
9662 }
Nazanin Bakhshi628473f2019-01-29 17:37:52 -08009663 mPhoneConfigurationManager.switchMultiSimConfig(numOfSims);
9664 } finally {
9665 Binder.restoreCallingIdentity(identity);
9666 }
9667 }
9668
Hyungjun Parkbb07fde2019-01-10 15:28:51 +09009669 @Override
9670 public boolean isApplicationOnUicc(int subId, int appType) {
9671 enforceReadPrivilegedPermission("isApplicationOnUicc");
9672 Phone phone = getPhone(subId);
9673 if (phone == null) {
9674 return false;
9675 }
9676 final long identity = Binder.clearCallingIdentity();
9677 try {
Muralidhar Reddy472c2ae2021-09-29 19:38:40 +00009678 UiccPort uiccPort = phone.getUiccPort();
9679 if (uiccPort == null) {
Hyungjun Parkbb07fde2019-01-10 15:28:51 +09009680 return false;
9681 }
Muralidhar Reddy472c2ae2021-09-29 19:38:40 +00009682 UiccProfile uiccProfile = uiccPort.getUiccProfile();
Hyungjun Parkbb07fde2019-01-10 15:28:51 +09009683 if (uiccProfile == null) {
9684 return false;
9685 }
9686 if (TelephonyManager.APPTYPE_SIM <= appType
9687 && appType <= TelephonyManager.APPTYPE_ISIM) {
9688 return uiccProfile.isApplicationOnIcc(AppType.values()[appType]);
9689 }
9690 return false;
9691 } finally {
9692 Binder.restoreCallingIdentity(identity);
9693 }
9694 }
9695
Nazanin Bakhshi628473f2019-01-29 17:37:52 -08009696 /**
chen xub4baa772019-04-03 10:23:41 -07009697 * Get whether making changes to modem configurations will trigger reboot.
9698 * Return value defaults to true.
Nazanin Bakhshi5fe5ef22019-01-30 10:52:09 -08009699 */
9700 @Override
Philip P. Moltmann700a9592019-10-03 11:53:50 -07009701 public boolean doesSwitchMultiSimConfigTriggerReboot(int subId, String callingPackage,
9702 String callingFeatureId) {
chen xub4baa772019-04-03 10:23:41 -07009703 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
Philip P. Moltmann700a9592019-10-03 11:53:50 -07009704 mApp, subId, callingPackage, callingFeatureId,
9705 "doesSwitchMultiSimConfigTriggerReboot")) {
chen xub4baa772019-04-03 10:23:41 -07009706 return false;
9707 }
Nazanin Bakhshi5fe5ef22019-01-30 10:52:09 -08009708 final long identity = Binder.clearCallingIdentity();
9709 try {
9710 return mPhoneConfigurationManager.isRebootRequiredForModemConfigChange();
9711 } finally {
9712 Binder.restoreCallingIdentity(identity);
9713 }
9714 }
9715
Nathan Harold29f5f052019-02-15 13:41:57 -08009716 private void updateModemStateMetrics() {
9717 TelephonyMetrics metrics = TelephonyMetrics.getInstance();
9718 // TODO: check the state for each modem if the api is ready.
9719 metrics.updateEnabledModemBitmap((1 << TelephonyManager.from(mApp).getPhoneCount()) - 1);
9720 }
9721
Pengquan Meng3889a572019-01-23 11:16:29 -08009722 @Override
sandeepjsa208e3b2021-11-17 04:05:58 +00009723 public List<UiccSlotMapping> getSlotsMapping(String callingPackage) {
Pengquan Meng3889a572019-01-23 11:16:29 -08009724 enforceReadPrivilegedPermission("getSlotsMapping");
sandeepjsa208e3b2021-11-17 04:05:58 +00009725 // Verify that the callingPackage belongs to the calling UID
9726 mApp.getSystemService(AppOpsManager.class)
9727 .checkPackage(Binder.getCallingUid(), callingPackage);
Pengquan Meng3889a572019-01-23 11:16:29 -08009728 final long identity = Binder.clearCallingIdentity();
sandeepjsa208e3b2021-11-17 04:05:58 +00009729 List<UiccSlotMapping> slotMap = new ArrayList<>();
Pengquan Meng3889a572019-01-23 11:16:29 -08009730 try {
sandeepjsa208e3b2021-11-17 04:05:58 +00009731 UiccSlotInfo[] slotInfos = getUiccSlotsInfo(mApp.getOpPackageName());
9732 if (slotInfos != null) {
9733 for (int i = 0; i < slotInfos.length; i++) {
9734 for (UiccPortInfo portInfo : slotInfos[i].getPorts()) {
9735 if (SubscriptionManager.isValidPhoneId(portInfo.getLogicalSlotIndex())) {
9736 slotMap.add(new UiccSlotMapping(portInfo.getPortIndex(), i,
9737 portInfo.getLogicalSlotIndex()));
9738 }
9739 }
Pengquan Meng3889a572019-01-23 11:16:29 -08009740 }
9741 }
sandeepjsa208e3b2021-11-17 04:05:58 +00009742 return slotMap;
Pengquan Meng3889a572019-01-23 11:16:29 -08009743 } finally {
9744 Binder.restoreCallingIdentity(identity);
9745 }
9746 }
Nathan Harold48d6fd52019-02-06 19:01:40 -08009747
9748 /**
9749 * Get the IRadio HAL Version
9750 */
9751 @Override
9752 public int getRadioHalVersion() {
9753 Phone phone = getDefaultPhone();
9754 if (phone == null) return -1;
9755 HalVersion hv = phone.getHalVersion();
9756 if (hv.equals(HalVersion.UNKNOWN)) return -1;
9757 return hv.major * 100 + hv.minor;
9758 }
Malcolm Chendc8c10e2019-04-10 18:25:07 -07009759
9760 /**
Shuo Qianda2d6ec2020-01-14 15:18:28 -08009761 * Get the current calling package name.
9762 * @return the current calling package name
9763 */
9764 @Override
9765 public String getCurrentPackageName() {
9766 return mApp.getPackageManager().getPackagesForUid(Binder.getCallingUid())[0];
9767 }
9768
9769 /**
Malcolm Chene5ad5792019-04-18 13:51:02 -07009770 * Return whether data is enabled for certain APN type. This will tell if framework will accept
9771 * corresponding network requests on a subId.
9772 *
9773 * Data is enabled if:
Malcolm Chendc8c10e2019-04-10 18:25:07 -07009774 * 1) user data is turned on, or
Malcolm Chene5ad5792019-04-18 13:51:02 -07009775 * 2) APN is un-metered for this subscription, or
9776 * 3) APN type is whitelisted. E.g. MMS is whitelisted if
Hall Liu746e03c2020-09-25 11:13:49 -07009777 * {@link TelephonyManager#MOBILE_DATA_POLICY_MMS_ALWAYS_ALLOWED} is enabled.
Malcolm Chene5ad5792019-04-18 13:51:02 -07009778 *
9779 * @return whether data is allowed for a apn type.
9780 *
9781 * @hide
Malcolm Chendc8c10e2019-04-10 18:25:07 -07009782 */
9783 @Override
Malcolm Chene5ad5792019-04-18 13:51:02 -07009784 public boolean isDataEnabledForApn(int apnType, int subId, String callingPackage) {
Amit Mahajan5d4e1922019-10-07 16:20:43 -07009785 enforceReadPrivilegedPermission("Needs READ_PRIVILEGED_PHONE_STATE for "
9786 + "isDataEnabledForApn");
Malcolm Chendc8c10e2019-04-10 18:25:07 -07009787
9788 // Now that all security checks passes, perform the operation as ourselves.
9789 final long identity = Binder.clearCallingIdentity();
9790 try {
9791 Phone phone = getPhone(subId);
9792 if (phone == null) return false;
9793
Jack Yu41407ee2019-05-13 16:54:09 -07009794 boolean isMetered = ApnSettingUtils.isMeteredApnType(apnType, phone);
Jack Yu99e87332021-12-17 23:14:15 -08009795 boolean isDataEnabled;
9796 if (phone.isUsingNewDataStack()) {
9797 isDataEnabled = phone.getDataSettingsManager().isDataEnabled(apnType);
9798 } else {
9799 isDataEnabled = phone.getDataEnabledSettings().isDataEnabled(apnType);
9800 }
9801 return !isMetered || isDataEnabled;
Malcolm Chene5ad5792019-04-18 13:51:02 -07009802 } finally {
9803 Binder.restoreCallingIdentity(identity);
9804 }
9805 }
9806
9807 @Override
Jack Yu41407ee2019-05-13 16:54:09 -07009808 public boolean isApnMetered(@ApnType int apnType, int subId) {
Malcolm Chene5ad5792019-04-18 13:51:02 -07009809 enforceReadPrivilegedPermission("isApnMetered");
9810
9811 // Now that all security checks passes, perform the operation as ourselves.
9812 final long identity = Binder.clearCallingIdentity();
9813 try {
9814 Phone phone = getPhone(subId);
9815 if (phone == null) return true; // By default return true.
9816
Jack Yu41407ee2019-05-13 16:54:09 -07009817 return ApnSettingUtils.isMeteredApnType(apnType, phone);
Malcolm Chendc8c10e2019-04-10 18:25:07 -07009818 } finally {
9819 Binder.restoreCallingIdentity(identity);
9820 }
9821 }
Brad Ebingera63db5f2019-04-23 16:31:13 -07009822
9823 @Override
Hall Liu73f5d362020-01-20 13:42:00 -08009824 public void setSystemSelectionChannels(List<RadioAccessSpecifier> specifiers,
9825 int subscriptionId, IBooleanConsumer resultCallback) {
9826 enforceModifyPermission();
9827 long token = Binder.clearCallingIdentity();
9828 try {
9829 Phone phone = getPhone(subscriptionId);
9830 if (phone == null) {
9831 try {
9832 if (resultCallback != null) {
9833 resultCallback.accept(false);
9834 }
9835 } catch (RemoteException e) {
9836 // ignore
9837 }
9838 return;
9839 }
9840 Pair<List<RadioAccessSpecifier>, Consumer<Boolean>> argument =
9841 Pair.create(specifiers, (x) -> {
9842 try {
9843 if (resultCallback != null) {
9844 resultCallback.accept(x);
9845 }
9846 } catch (RemoteException e) {
9847 // ignore
9848 }
9849 });
9850 sendRequestAsync(CMD_SET_SYSTEM_SELECTION_CHANNELS, argument, phone, null);
9851 } finally {
9852 Binder.restoreCallingIdentity(token);
9853 }
9854 }
9855
9856 @Override
Sarah Chin679c08a2020-11-18 13:39:35 -08009857 public List<RadioAccessSpecifier> getSystemSelectionChannels(int subId) {
9858 TelephonyPermissions
Nathan Harold62c68512021-04-06 11:26:02 -07009859 .enforceCallingOrSelfReadPrivilegedPhoneStatePermissionOrCarrierPrivilege(
Sarah Chin679c08a2020-11-18 13:39:35 -08009860 mApp, subId, "getSystemSelectionChannels");
9861 WorkSource workSource = getWorkSource(Binder.getCallingUid());
9862 final long identity = Binder.clearCallingIdentity();
9863 try {
Sarah Chin428d1d62021-03-13 03:17:40 -08009864 Object result = sendRequest(CMD_GET_SYSTEM_SELECTION_CHANNELS, null, subId, workSource);
9865 if (result instanceof IllegalStateException) {
9866 throw (IllegalStateException) result;
9867 }
9868 List<RadioAccessSpecifier> specifiers = (List<RadioAccessSpecifier>) result;
Sarah Chin679c08a2020-11-18 13:39:35 -08009869 if (DBG) log("getSystemSelectionChannels: " + specifiers);
9870 return specifiers;
9871 } finally {
9872 Binder.restoreCallingIdentity(identity);
9873 }
9874 }
9875
9876 @Override
changbetty7157e9e2019-12-06 18:16:37 +08009877 public boolean isMvnoMatched(int subId, int mvnoType, @NonNull String mvnoMatchData) {
changbettyca3d40d2020-03-03 16:27:31 +08009878 enforceReadPrivilegedPermission("isMvnoMatched");
changbetty7157e9e2019-12-06 18:16:37 +08009879 IccRecords iccRecords = UiccController.getInstance().getIccRecords(
9880 SubscriptionManager.getPhoneId(subId), UiccController.APP_FAM_3GPP);
9881 if (iccRecords == null) {
9882 Log.d(LOG_TAG, "isMvnoMatched# IccRecords is null");
9883 return false;
9884 }
9885 return ApnSettingUtils.mvnoMatches(iccRecords, mvnoType, mvnoMatchData);
9886 }
9887
9888 @Override
Philip P. Moltmannd02b7372020-03-18 17:06:12 -07009889 public void enqueueSmsPickResult(String callingPackage, String callingAttributionTag,
9890 IIntegerConsumer pendingSubIdResult) {
Shuo Qianda2d6ec2020-01-14 15:18:28 -08009891 if (callingPackage == null) {
9892 callingPackage = getCurrentPackageName();
9893 }
Brad Ebingera63db5f2019-04-23 16:31:13 -07009894 SmsPermissions permissions = new SmsPermissions(getDefaultPhone(), mApp,
9895 (AppOpsManager) mApp.getSystemService(Context.APP_OPS_SERVICE));
Philip P. Moltmannd02b7372020-03-18 17:06:12 -07009896 if (!permissions.checkCallingCanSendSms(callingPackage, callingAttributionTag,
9897 "Sending message")) {
Brad Ebingera63db5f2019-04-23 16:31:13 -07009898 throw new SecurityException("Requires SEND_SMS permission to perform this operation");
9899 }
9900 PickSmsSubscriptionActivity.addPendingResult(pendingSubIdResult);
9901 Intent intent = new Intent();
9902 intent.setClass(mApp, PickSmsSubscriptionActivity.class);
9903 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
9904 // Bring up choose default SMS subscription dialog right now
9905 intent.putExtra(PickSmsSubscriptionActivity.DIALOG_TYPE_KEY,
9906 PickSmsSubscriptionActivity.SMS_PICK_FOR_MESSAGE);
9907 mApp.startActivity(intent);
9908 }
chen xud5ca2d52019-05-28 15:20:57 -07009909
9910 @Override
9911 public String getMmsUAProfUrl(int subId) {
9912 //TODO investigate if this API should require proper permission check in R b/133791609
9913 final long identity = Binder.clearCallingIdentity();
9914 try {
Guoqiang.Qiu171383d2020-07-13 09:38:32 +08009915 String carrierUAProfUrl = mApp.getCarrierConfigForSubId(subId).getString(
9916 CarrierConfigManager.KEY_MMS_UA_PROF_URL_STRING);
9917 if (!TextUtils.isEmpty(carrierUAProfUrl)) {
9918 return carrierUAProfUrl;
9919 }
Daniel Brightebb4eb72020-02-18 15:16:33 -08009920 return SubscriptionManager.getResourcesForSubId(getDefaultPhone().getContext(), subId)
9921 .getString(com.android.internal.R.string.config_mms_user_agent_profile_url);
chen xud5ca2d52019-05-28 15:20:57 -07009922 } finally {
9923 Binder.restoreCallingIdentity(identity);
9924 }
9925 }
9926
9927 @Override
9928 public String getMmsUserAgent(int subId) {
9929 //TODO investigate if this API should require proper permission check in R b/133791609
9930 final long identity = Binder.clearCallingIdentity();
9931 try {
Guoqiang.Qiu171383d2020-07-13 09:38:32 +08009932 String carrierUserAgent = mApp.getCarrierConfigForSubId(subId).getString(
9933 CarrierConfigManager.KEY_MMS_USER_AGENT_STRING);
9934 if (!TextUtils.isEmpty(carrierUserAgent)) {
9935 return carrierUserAgent;
9936 }
Daniel Brightebb4eb72020-02-18 15:16:33 -08009937 return SubscriptionManager.getResourcesForSubId(getDefaultPhone().getContext(), subId)
9938 .getString(com.android.internal.R.string.config_mms_user_agent);
chen xud5ca2d52019-05-28 15:20:57 -07009939 } finally {
9940 Binder.restoreCallingIdentity(identity);
9941 }
9942 }
Jack Yub07d4972019-05-28 16:12:25 -07009943
9944 @Override
Hall Liua62f5da2020-09-25 10:42:19 -07009945 public boolean isMobileDataPolicyEnabled(int subscriptionId, int policy) {
9946 enforceReadPrivilegedPermission("isMobileDataPolicyEnabled");
Jack Yub07d4972019-05-28 16:12:25 -07009947
Jack Yub07d4972019-05-28 16:12:25 -07009948 final long identity = Binder.clearCallingIdentity();
9949 try {
Hall Liua62f5da2020-09-25 10:42:19 -07009950 Phone phone = getPhone(subscriptionId);
Jack Yub07d4972019-05-28 16:12:25 -07009951 if (phone == null) return false;
9952
Hall Liua62f5da2020-09-25 10:42:19 -07009953 switch (policy) {
9954 case TelephonyManager.MOBILE_DATA_POLICY_DATA_ON_NON_DEFAULT_DURING_VOICE_CALL:
Jack Yu99e87332021-12-17 23:14:15 -08009955 if (phone.isUsingNewDataStack()) {
9956 return phone.getDataSettingsManager().isDataAllowedInVoiceCall();
9957 } else {
9958 return phone.getDataEnabledSettings().isDataAllowedInVoiceCall();
9959 }
Hall Liua62f5da2020-09-25 10:42:19 -07009960 case TelephonyManager.MOBILE_DATA_POLICY_MMS_ALWAYS_ALLOWED:
Jack Yu99e87332021-12-17 23:14:15 -08009961 if (phone.isUsingNewDataStack()) {
9962 return phone.getDataSettingsManager().isMmsAlwaysAllowed();
9963 } else {
9964 return phone.getDataEnabledSettings().isMmsAlwaysAllowed();
9965 }
Hall Liua62f5da2020-09-25 10:42:19 -07009966 default:
9967 throw new IllegalArgumentException(policy + " is not a valid policy");
9968 }
Jack Yub07d4972019-05-28 16:12:25 -07009969 } finally {
9970 Binder.restoreCallingIdentity(identity);
9971 }
9972 }
9973
9974 @Override
Hall Liuc66bb112021-02-02 12:09:32 -08009975 public void setMobileDataPolicyEnabled(int subscriptionId, int policy,
Hall Liua62f5da2020-09-25 10:42:19 -07009976 boolean enabled) {
changbettyd5c246e2019-12-24 15:40:37 +08009977 enforceModifyPermission();
9978
changbettyd5c246e2019-12-24 15:40:37 +08009979 final long identity = Binder.clearCallingIdentity();
9980 try {
Hall Liua62f5da2020-09-25 10:42:19 -07009981 Phone phone = getPhone(subscriptionId);
9982 if (phone == null) return;
changbettyd5c246e2019-12-24 15:40:37 +08009983
Hall Liua62f5da2020-09-25 10:42:19 -07009984 switch (policy) {
9985 case TelephonyManager.MOBILE_DATA_POLICY_DATA_ON_NON_DEFAULT_DURING_VOICE_CALL:
Jack Yu99e87332021-12-17 23:14:15 -08009986 if (phone.isUsingNewDataStack()) {
9987 phone.getDataSettingsManager().setAllowDataDuringVoiceCall(enabled);
9988 } else {
9989 phone.getDataEnabledSettings().setAllowDataDuringVoiceCall(enabled);
9990 }
Hall Liua62f5da2020-09-25 10:42:19 -07009991 break;
9992 case TelephonyManager.MOBILE_DATA_POLICY_MMS_ALWAYS_ALLOWED:
Jack Yu99e87332021-12-17 23:14:15 -08009993 if (phone.isUsingNewDataStack()) {
9994 phone.getDataSettingsManager().setAlwaysAllowMmsData(enabled);
9995 } else {
9996 phone.getDataEnabledSettings().setAlwaysAllowMmsData(enabled);
9997 }
Hall Liua62f5da2020-09-25 10:42:19 -07009998 break;
9999 default:
10000 throw new IllegalArgumentException(policy + " is not a valid policy");
10001 }
changbettyd5c246e2019-12-24 15:40:37 +080010002 } finally {
10003 Binder.restoreCallingIdentity(identity);
10004 }
10005 }
10006
Tyler Gunn7bcdc742019-10-04 15:56:59 -070010007 /**
Hall Liu746e03c2020-09-25 11:13:49 -070010008 * Updates whether conference event package handling is enabled.
Tyler Gunn7bcdc742019-10-04 15:56:59 -070010009 * @param isCepEnabled {@code true} if CEP handling is enabled (default), or {@code false}
10010 * otherwise.
10011 */
10012 @Override
10013 public void setCepEnabled(boolean isCepEnabled) {
10014 TelephonyPermissions.enforceShellOnly(Binder.getCallingUid(), "setCepEnabled");
10015
10016 final long identity = Binder.clearCallingIdentity();
10017 try {
10018 Rlog.i(LOG_TAG, "setCepEnabled isCepEnabled=" + isCepEnabled);
10019 for (Phone phone : PhoneFactory.getPhones()) {
10020 Phone defaultPhone = phone.getImsPhone();
10021 if (defaultPhone != null && defaultPhone.getPhoneType() == PHONE_TYPE_IMS) {
10022 ImsPhone imsPhone = (ImsPhone) defaultPhone;
10023 ImsPhoneCallTracker imsPhoneCallTracker =
10024 (ImsPhoneCallTracker) imsPhone.getCallTracker();
10025 imsPhoneCallTracker.setConferenceEventPackageEnabled(isCepEnabled);
10026 Rlog.i(LOG_TAG, "setCepEnabled isCepEnabled=" + isCepEnabled + ", for imsPhone "
10027 + imsPhone.getMsisdn());
10028 }
10029 }
10030 } finally {
10031 Binder.restoreCallingIdentity(identity);
10032 }
10033 }
allenwtsu46dcc572020-01-08 18:24:03 +080010034
10035 /**
10036 * Notify that an RCS autoconfiguration XML file has been received for provisioning.
10037 *
10038 * @param config The XML file to be read. ASCII/UTF8 encoded text if not compressed.
10039 * @param isCompressed The XML file is compressed in gzip format and must be decompressed
10040 * before being read.
10041 */
10042 @Override
10043 public void notifyRcsAutoConfigurationReceived(int subId, @NonNull byte[] config, boolean
10044 isCompressed) {
10045 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
10046 mApp, subId, "notifyRcsAutoConfigurationReceived");
Hui Wang761a6682020-10-31 05:12:53 +000010047 if (!SubscriptionManager.isValidSubscriptionId(subId)) {
10048 throw new IllegalArgumentException("Invalid Subscription ID: " + subId);
10049 }
10050 if (!isImsAvailableOnDevice()) {
10051 throw new ServiceSpecificException(ImsException.CODE_ERROR_UNSUPPORTED_OPERATION,
10052 "IMS not available on device.");
10053 }
10054
10055 final long identity = Binder.clearCallingIdentity();
allenwtsu46dcc572020-01-08 18:24:03 +080010056 try {
Hui Wang761a6682020-10-31 05:12:53 +000010057 RcsProvisioningMonitor.getInstance().updateConfig(subId, config, isCompressed);
10058 } finally {
10059 Binder.restoreCallingIdentity(identity);
allenwtsu46dcc572020-01-08 18:24:03 +080010060 }
10061 }
zoey chene02881a2019-12-30 16:11:23 +080010062
10063 @Override
10064 public boolean isIccLockEnabled(int subId) {
10065 enforceReadPrivilegedPermission("isIccLockEnabled");
10066
10067 // Now that all security checks passes, perform the operation as ourselves.
10068 final long identity = Binder.clearCallingIdentity();
10069 try {
10070 Phone phone = getPhone(subId);
10071 if (phone != null && phone.getIccCard() != null) {
10072 return phone.getIccCard().getIccLockEnabled();
10073 } else {
10074 return false;
10075 }
10076 } finally {
10077 Binder.restoreCallingIdentity(identity);
10078 }
10079 }
10080
10081 /**
10082 * Set the ICC pin lock enabled or disabled.
10083 *
10084 * @return an integer representing the status of IccLock enabled or disabled in the following
10085 * three cases:
10086 * - {@link TelephonyManager#CHANGE_ICC_LOCK_SUCCESS} if enabled or disabled IccLock
10087 * successfully.
10088 * - Positive number and zero for remaining password attempts.
10089 * - Negative number for other failure cases (such like enabling/disabling PIN failed).
10090 *
10091 */
10092 @Override
10093 public int setIccLockEnabled(int subId, boolean enabled, String password) {
10094 enforceModifyPermission();
10095
10096 Phone phone = getPhone(subId);
10097 if (phone == null) {
10098 return 0;
10099 }
10100 // Now that all security checks passes, perform the operation as ourselves.
10101 final long identity = Binder.clearCallingIdentity();
10102 try {
10103 int attemptsRemaining = (int) sendRequest(CMD_SET_ICC_LOCK_ENABLED,
10104 new Pair<Boolean, String>(enabled, password), phone, null);
10105 return attemptsRemaining;
10106
10107 } catch (Exception e) {
10108 Log.e(LOG_TAG, "setIccLockEnabled. Exception e =" + e);
10109 } finally {
10110 Binder.restoreCallingIdentity(identity);
10111 }
10112 return 0;
10113 }
10114
10115 /**
10116 * Change the ICC password used in ICC pin lock.
10117 *
10118 * @return an integer representing the status of IccLock changed in the following three cases:
10119 * - {@link TelephonyManager#CHANGE_ICC_LOCK_SUCCESS} if changed IccLock successfully.
10120 * - Positive number and zero for remaining password attempts.
10121 * - Negative number for other failure cases (such like enabling/disabling PIN failed).
10122 *
10123 */
10124 @Override
10125 public int changeIccLockPassword(int subId, String oldPassword, String newPassword) {
10126 enforceModifyPermission();
10127
10128 Phone phone = getPhone(subId);
10129 if (phone == null) {
10130 return 0;
10131 }
10132 // Now that all security checks passes, perform the operation as ourselves.
10133 final long identity = Binder.clearCallingIdentity();
10134 try {
10135 int attemptsRemaining = (int) sendRequest(CMD_CHANGE_ICC_LOCK_PASSWORD,
10136 new Pair<String, String>(oldPassword, newPassword), phone, null);
10137 return attemptsRemaining;
10138
10139 } catch (Exception e) {
10140 Log.e(LOG_TAG, "changeIccLockPassword. Exception e =" + e);
10141 } finally {
10142 Binder.restoreCallingIdentity(identity);
10143 }
10144 return 0;
10145 }
Peter Wangdafb9ac2020-01-15 14:13:38 -080010146
10147 /**
10148 * Request for receiving user activity notification
10149 */
10150 @Override
10151 public void requestUserActivityNotification() {
10152 if (!mNotifyUserActivity.get()
10153 && !mMainThreadHandler.hasMessages(MSG_NOTIFY_USER_ACTIVITY)) {
10154 mNotifyUserActivity.set(true);
10155 }
10156 }
10157
10158 /**
10159 * Called when userActivity is signalled in the power manager.
10160 * This is safe to call from any thread, with any window manager locks held or not.
10161 */
10162 @Override
10163 public void userActivity() {
10164 // ***************************************
10165 // * Inherited from PhoneWindowManager *
10166 // ***************************************
10167 // THIS IS CALLED FROM DEEP IN THE POWER MANAGER
10168 // WITH ITS LOCKS HELD.
10169 //
10170 // This code must be VERY careful about the locks
10171 // it acquires.
10172 // In fact, the current code acquires way too many,
10173 // and probably has lurking deadlocks.
10174
10175 if (Binder.getCallingUid() != Process.SYSTEM_UID) {
10176 throw new SecurityException("Only the OS may call notifyUserActivity()");
10177 }
10178
10179 if (mNotifyUserActivity.getAndSet(false)) {
10180 mMainThreadHandler.sendEmptyMessageDelayed(MSG_NOTIFY_USER_ACTIVITY,
10181 USER_ACTIVITY_NOTIFICATION_DELAY);
10182 }
10183 }
Malcolm Chen4639c562020-04-13 11:59:40 -070010184
10185 @Override
10186 public boolean canConnectTo5GInDsdsMode() {
10187 return mApp.getResources().getBoolean(R.bool.config_5g_connection_in_dsds_mode);
10188 }
Jack Yud10cdd42020-09-28 20:28:01 -070010189
10190 @Override
10191 public @NonNull List<String> getEquivalentHomePlmns(int subId, String callingPackage,
10192 String callingFeatureId) {
10193 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
10194 mApp, subId, callingPackage, callingFeatureId, "getEquivalentHomePlmns")) {
10195 throw new SecurityException("Requires READ_PHONE_STATE permission.");
10196 }
10197
10198 Phone phone = getPhone(subId);
10199 if (phone == null) {
10200 throw new RuntimeException("phone is not available");
10201 }
10202 // Now that all security checks passes, perform the operation as ourselves.
10203 final long identity = Binder.clearCallingIdentity();
10204 try {
10205 return phone.getEquivalentHomePlmns();
10206 } finally {
10207 Binder.restoreCallingIdentity(identity);
10208 }
10209 }
Daniel Bright59e67312020-11-13 11:49:37 -080010210
10211 @Override
10212 public boolean isRadioInterfaceCapabilitySupported(
Daniel Bright95a4c1f2021-02-11 09:57:16 -080010213 final @NonNull @TelephonyManager.RadioInterfaceCapability String capability) {
10214 Set<String> radioInterfaceCapabilities =
Daniel Bright94f43662021-03-01 14:43:40 -080010215 mRadioInterfaceCapabilities.getCapabilities();
Daniel Bright59e67312020-11-13 11:49:37 -080010216 if (radioInterfaceCapabilities == null) {
10217 throw new RuntimeException("radio interface capabilities are not available");
Daniel Bright59e67312020-11-13 11:49:37 -080010218 }
Daniel Bright95a4c1f2021-02-11 09:57:16 -080010219 return radioInterfaceCapabilities.contains(capability);
Daniel Bright59e67312020-11-13 11:49:37 -080010220 }
Jack Nudelmanb0b87642020-11-12 15:04:39 -080010221
Hui Wang641e81c2020-10-12 12:14:23 -070010222 @Override
10223 public void bootstrapAuthenticationRequest(int subId, int appType, Uri nafUrl,
10224 UaSecurityProtocolIdentifier securityProtocol,
Brad Ebinger34c09a52021-02-17 23:23:21 +000010225 boolean forceBootStrapping, IBootstrapAuthenticationCallback callback) {
10226 TelephonyPermissions.enforceAnyPermissionGrantedOrCarrierPrivileges(mApp, subId,
10227 Binder.getCallingUid(), "bootstrapAuthenticationRequest",
10228 Manifest.permission.PERFORM_IMS_SINGLE_REGISTRATION,
10229 Manifest.permission.MODIFY_PHONE_STATE);
Hui Wang641e81c2020-10-12 12:14:23 -070010230 if (DBG) {
10231 log("bootstrapAuthenticationRequest, subId:" + subId + ", appType:"
10232 + appType + ", NAF:" + nafUrl + ", sp:" + securityProtocol
10233 + ", forceBootStrapping:" + forceBootStrapping + ", callback:" + callback);
10234 }
10235
10236 if (!SubscriptionManager.isValidSubscriptionId(subId)
10237 || appType < TelephonyManager.APPTYPE_UNKNOWN
10238 || appType > TelephonyManager.APPTYPE_ISIM
10239 || nafUrl == null || securityProtocol == null || callback == null) {
10240 Log.d(LOG_TAG, "bootstrapAuthenticationRequest failed due to invalid parameters");
10241 if (callback != null) {
10242 try {
10243 callback.onAuthenticationFailure(
10244 0, TelephonyManager.GBA_FAILURE_REASON_FEATURE_NOT_SUPPORTED);
10245 } catch (RemoteException exception) {
10246 log("Fail to notify onAuthenticationFailure due to " + exception);
10247 }
10248 return;
10249 }
10250 }
10251
10252 final long token = Binder.clearCallingIdentity();
10253 try {
10254 getGbaManager(subId).bootstrapAuthenticationRequest(
10255 new GbaAuthRequest(subId, appType, nafUrl, securityProtocol.toByteArray(),
10256 forceBootStrapping, callback));
10257 } finally {
10258 Binder.restoreCallingIdentity(token);
10259 }
10260 }
10261
Jack Nudelmanb0b87642020-11-12 15:04:39 -080010262 /**
Sooraj Sasindran72cff492021-07-29 09:42:42 -070010263 * Attempts to set the radio power state for all phones for thermal reason.
10264 * This does not guarantee that the
Jack Nudelmanb0b87642020-11-12 15:04:39 -080010265 * requested radio power state will actually be set. See {@link
10266 * PhoneInternalInterface#setRadioPowerForReason} for more details.
10267 *
Jack Nudelmanb0b87642020-11-12 15:04:39 -080010268 * @param enable {@code true} if trying to turn radio on.
10269 * @return {@code true} if phone setRadioPowerForReason was called. Otherwise, returns {@code
10270 * false}.
10271 */
Sooraj Sasindran72cff492021-07-29 09:42:42 -070010272 private boolean setRadioPowerForThermal(boolean enable) {
10273 boolean isPhoneAvailable = false;
10274 for (int i = 0; i < TelephonyManager.getDefault().getActiveModemCount(); i++) {
10275 Phone phone = PhoneFactory.getPhone(i);
10276 if (phone != null) {
10277 phone.setRadioPowerForReason(enable, Phone.RADIO_POWER_REASON_THERMAL);
10278 isPhoneAvailable = true;
10279 }
Jack Nudelmanb0b87642020-11-12 15:04:39 -080010280 }
Sooraj Sasindran72cff492021-07-29 09:42:42 -070010281
10282 // return true if successfully informed the phone object about the thermal radio power
10283 // request.
10284 return isPhoneAvailable;
Jack Nudelmanb0b87642020-11-12 15:04:39 -080010285 }
10286
10287 private int handleDataThrottlingRequest(int subId,
10288 DataThrottlingRequest dataThrottlingRequest) {
Jack Nudelman5d6a98b2021-03-04 14:26:25 -080010289 boolean isDataThrottlingSupported = isRadioInterfaceCapabilitySupported(
10290 TelephonyManager.CAPABILITY_THERMAL_MITIGATION_DATA_THROTTLING);
10291 if (!isDataThrottlingSupported && dataThrottlingRequest.getDataThrottlingAction()
10292 != DataThrottlingRequest.DATA_THROTTLING_ACTION_NO_DATA_THROTTLING) {
10293 throw new IllegalArgumentException("modem does not support data throttling");
10294 }
10295
Jack Nudelmanb0b87642020-11-12 15:04:39 -080010296 // Ensure that radio is on. If not able to power on due to phone being unavailable, return
10297 // THERMAL_MITIGATION_RESULT_MODEM_NOT_AVAILABLE.
Sooraj Sasindran72cff492021-07-29 09:42:42 -070010298 if (!setRadioPowerForThermal(true)) {
Jack Nudelmanb0b87642020-11-12 15:04:39 -080010299 return TelephonyManager.THERMAL_MITIGATION_RESULT_MODEM_NOT_AVAILABLE;
10300 }
10301
10302 setDataEnabledForReason(subId, TelephonyManager.DATA_ENABLED_REASON_THERMAL, true);
10303
Jack Nudelman5d6a98b2021-03-04 14:26:25 -080010304 if (isDataThrottlingSupported) {
10305 int thermalMitigationResult =
Jack Nudelmanb0b87642020-11-12 15:04:39 -080010306 (int) sendRequest(CMD_SET_DATA_THROTTLING, dataThrottlingRequest, subId);
Jack Nudelman5d6a98b2021-03-04 14:26:25 -080010307 if (thermalMitigationResult == SET_DATA_THROTTLING_MODEM_THREW_INVALID_PARAMS) {
10308 throw new IllegalArgumentException("modem returned INVALID_ARGUMENTS");
10309 } else if (thermalMitigationResult
10310 == MODEM_DOES_NOT_SUPPORT_DATA_THROTTLING_ERROR_CODE) {
Jack Nudelman760d0962021-05-20 13:57:30 -070010311 log("Modem likely does not support data throttling on secondary carrier. Data " +
10312 "throttling action = " + dataThrottlingRequest.getDataThrottlingAction());
10313 return TelephonyManager.THERMAL_MITIGATION_RESULT_MODEM_ERROR;
Jack Nudelman5d6a98b2021-03-04 14:26:25 -080010314 }
10315 return thermalMitigationResult;
Jack Nudelmanb0b87642020-11-12 15:04:39 -080010316 }
Jack Nudelman5d6a98b2021-03-04 14:26:25 -080010317
10318 return TelephonyManager.THERMAL_MITIGATION_RESULT_SUCCESS;
Jack Nudelmanb0b87642020-11-12 15:04:39 -080010319 }
10320
Jack Nudelman644b91a2021-03-12 14:09:48 -080010321 private static List<String> getThermalMitigationAllowlist(Context context) {
10322 if (sThermalMitigationAllowlistedPackages.isEmpty()) {
10323 for (String pckg : context.getResources()
10324 .getStringArray(R.array.thermal_mitigation_allowlisted_packages)) {
10325 sThermalMitigationAllowlistedPackages.add(pckg);
10326 }
10327 }
10328
10329 return sThermalMitigationAllowlistedPackages;
10330 }
10331
Jack Nudelmane69bbc82021-05-13 10:00:15 -070010332 private boolean isAnyPhoneInEmergencyState() {
10333 TelecomManager tm = mApp.getSystemService(TelecomManager.class);
10334 if (tm.isInEmergencyCall()) {
10335 Log.e(LOG_TAG , "Phone state is not valid. One of the phones is in an emergency call");
10336 return true;
10337 }
10338 for (Phone phone : PhoneFactory.getPhones()) {
10339 if (phone.isInEmergencySmsMode() || phone.isInEcm()) {
10340 Log.e(LOG_TAG, "Phone state is not valid. isInEmergencySmsMode = "
10341 + phone.isInEmergencySmsMode() + " isInEmergencyCallbackMode = "
10342 + phone.isInEcm());
10343 return true;
10344 }
10345 }
10346
10347 return false;
10348 }
10349
Jack Nudelman644b91a2021-03-12 14:09:48 -080010350 /**
10351 * Used by shell commands to add an authorized package name for thermal mitigation.
10352 * @param packageName name of package to be allowlisted
10353 * @param context
10354 */
10355 static void addPackageToThermalMitigationAllowlist(String packageName, Context context) {
10356 sThermalMitigationAllowlistedPackages = getThermalMitigationAllowlist(context);
10357 sThermalMitigationAllowlistedPackages.add(packageName);
10358 }
10359
10360 /**
10361 * Used by shell commands to remove an authorized package name for thermal mitigation.
10362 * @param packageName name of package to remove from allowlist
10363 * @param context
10364 */
10365 static void removePackageFromThermalMitigationAllowlist(String packageName, Context context) {
10366 sThermalMitigationAllowlistedPackages = getThermalMitigationAllowlist(context);
10367 sThermalMitigationAllowlistedPackages.remove(packageName);
10368 }
10369
Jack Nudelmanb0b87642020-11-12 15:04:39 -080010370 /**
10371 * Thermal mitigation request to control functionalities at modem.
10372 *
10373 * @param subId the id of the subscription.
10374 * @param thermalMitigationRequest holds all necessary information to be passed down to modem.
Jack Nudelman644b91a2021-03-12 14:09:48 -080010375 * @param callingPackage the package name of the calling package.
Jack Nudelmanb0b87642020-11-12 15:04:39 -080010376 *
10377 * @return thermalMitigationResult enum as defined in android.telephony.Annotation.
10378 */
10379 @Override
10380 @ThermalMitigationResult
10381 public int sendThermalMitigationRequest(
10382 int subId,
Jack Nudelman644b91a2021-03-12 14:09:48 -080010383 ThermalMitigationRequest thermalMitigationRequest,
10384 String callingPackage) throws IllegalArgumentException {
Jack Nudelmanb0b87642020-11-12 15:04:39 -080010385 enforceModifyPermission();
10386
Jack Nudelman644b91a2021-03-12 14:09:48 -080010387 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
10388 if (!getThermalMitigationAllowlist(getDefaultPhone().getContext())
10389 .contains(callingPackage)) {
10390 throw new SecurityException("Calling package must be configured in the device config. "
10391 + "calling package: " + callingPackage);
10392 }
10393
Jack Nudelmanb0b87642020-11-12 15:04:39 -080010394 WorkSource workSource = getWorkSource(Binder.getCallingUid());
10395 final long identity = Binder.clearCallingIdentity();
10396
10397 int thermalMitigationResult = TelephonyManager.THERMAL_MITIGATION_RESULT_UNKNOWN_ERROR;
10398 try {
10399 int thermalMitigationAction = thermalMitigationRequest.getThermalMitigationAction();
10400 switch (thermalMitigationAction) {
10401 case ThermalMitigationRequest.THERMAL_MITIGATION_ACTION_DATA_THROTTLING:
10402 thermalMitigationResult =
10403 handleDataThrottlingRequest(subId,
10404 thermalMitigationRequest.getDataThrottlingRequest());
10405 break;
10406 case ThermalMitigationRequest.THERMAL_MITIGATION_ACTION_VOICE_ONLY:
10407 if (thermalMitigationRequest.getDataThrottlingRequest() != null) {
10408 throw new IllegalArgumentException("dataThrottlingRequest must be null for "
10409 + "ThermalMitigationRequest.THERMAL_MITIGATION_ACTION_VOICE_ONLY");
10410 }
10411
10412 // Ensure that radio is on. If not able to power on due to phone being
10413 // unavailable, return THERMAL_MITIGATION_RESULT_MODEM_NOT_AVAILABLE.
Sooraj Sasindran72cff492021-07-29 09:42:42 -070010414 if (!setRadioPowerForThermal(true)) {
Jack Nudelmanb0b87642020-11-12 15:04:39 -080010415 thermalMitigationResult =
10416 TelephonyManager.THERMAL_MITIGATION_RESULT_MODEM_NOT_AVAILABLE;
10417 break;
10418 }
10419
10420 setDataEnabledForReason(subId, TelephonyManager.DATA_ENABLED_REASON_THERMAL,
10421 false);
10422 thermalMitigationResult = TelephonyManager.THERMAL_MITIGATION_RESULT_SUCCESS;
10423 break;
10424 case ThermalMitigationRequest.THERMAL_MITIGATION_ACTION_RADIO_OFF:
10425 if (thermalMitigationRequest.getDataThrottlingRequest() != null) {
10426 throw new IllegalArgumentException("dataThrottlingRequest must be null for"
10427 + " ThermalMitigationRequest.THERMAL_MITIGATION_ACTION_RADIO_OFF");
10428 }
10429
10430 TelecomAccountRegistry registry = TelecomAccountRegistry.getInstance(null);
10431 if (registry != null) {
Jack Nudelmanb0b87642020-11-12 15:04:39 -080010432 Phone phone = getPhone(subId);
10433 if (phone == null) {
10434 thermalMitigationResult =
10435 TelephonyManager.THERMAL_MITIGATION_RESULT_MODEM_NOT_AVAILABLE;
10436 break;
10437 }
10438
Jack Nudelmane69bbc82021-05-13 10:00:15 -070010439 TelephonyConnectionService service =
10440 registry.getTelephonyConnectionService();
Jack Nudelmanb30ac302021-06-17 15:39:58 -070010441 if (service != null && service.isEmergencyCallPending()) {
Jack Nudelmane69bbc82021-05-13 10:00:15 -070010442 Log.e(LOG_TAG, "An emergency call is pending");
10443 thermalMitigationResult =
10444 TelephonyManager.THERMAL_MITIGATION_RESULT_INVALID_STATE;
10445 break;
10446 } else if (isAnyPhoneInEmergencyState()) {
Jack Nudelmanb0b87642020-11-12 15:04:39 -080010447 thermalMitigationResult =
10448 TelephonyManager.THERMAL_MITIGATION_RESULT_INVALID_STATE;
10449 break;
10450 }
10451 } else {
10452 thermalMitigationResult =
10453 TelephonyManager.THERMAL_MITIGATION_RESULT_MODEM_NOT_AVAILABLE;
10454 break;
10455 }
10456
10457 // Turn radio off. If not able to power off due to phone being unavailable,
10458 // return THERMAL_MITIGATION_RESULT_MODEM_NOT_AVAILABLE.
Sooraj Sasindran72cff492021-07-29 09:42:42 -070010459 if (!setRadioPowerForThermal(false)) {
Jack Nudelmanb0b87642020-11-12 15:04:39 -080010460 thermalMitigationResult =
10461 TelephonyManager.THERMAL_MITIGATION_RESULT_MODEM_NOT_AVAILABLE;
10462 break;
10463 }
10464 thermalMitigationResult =
10465 TelephonyManager.THERMAL_MITIGATION_RESULT_SUCCESS;
10466 break;
10467 default:
10468 throw new IllegalArgumentException("the requested thermalMitigationAction does "
10469 + "not exist. Requested action: " + thermalMitigationAction);
10470 }
10471 } catch (IllegalArgumentException e) {
10472 throw e;
10473 } catch (Exception e) {
10474 Log.e(LOG_TAG, "thermalMitigationRequest. Exception e =" + e);
10475 thermalMitigationResult = TelephonyManager.THERMAL_MITIGATION_RESULT_MODEM_ERROR;
10476 } finally {
10477 Binder.restoreCallingIdentity(identity);
10478 }
10479
10480 if (DBG) {
10481 log("thermalMitigationRequest returning with thermalMitigationResult: "
10482 + thermalMitigationResult);
10483 }
10484
10485 return thermalMitigationResult;
10486 }
Hui Wang641e81c2020-10-12 12:14:23 -070010487
10488 /**
10489 * Set the GbaService Package Name that Telephony will bind to.
10490 *
10491 * @param subId The sim that the GbaService is associated with.
10492 * @param packageName The name of the package to be replaced with.
10493 * @return true if setting the GbaService to bind to succeeded, false if it did not.
10494 */
10495 @Override
10496 public boolean setBoundGbaServiceOverride(int subId, String packageName) {
10497 enforceModifyPermission();
10498
10499 final long identity = Binder.clearCallingIdentity();
10500 try {
10501 return getGbaManager(subId).overrideServicePackage(packageName);
10502 } finally {
10503 Binder.restoreCallingIdentity(identity);
10504 }
10505 }
10506
10507 /**
10508 * Return the package name of the currently bound GbaService.
10509 *
10510 * @param subId The sim that the GbaService is associated with.
10511 * @return the package name of the GbaService configuration, null if GBA is not supported.
10512 */
10513 @Override
10514 public String getBoundGbaService(int subId) {
10515 enforceReadPrivilegedPermission("getBoundGbaServicePackage");
10516
10517 final long identity = Binder.clearCallingIdentity();
10518 try {
10519 return getGbaManager(subId).getServicePackage();
10520 } finally {
10521 Binder.restoreCallingIdentity(identity);
10522 }
10523 }
10524
10525 /**
10526 * Set the release time for telephony to unbind GbaService.
10527 *
10528 * @param subId The sim that the GbaService is associated with.
10529 * @param interval The release time to unbind GbaService by millisecond.
10530 * @return true if setting the GbaService to bind to succeeded, false if it did not.
10531 */
10532 @Override
10533 public boolean setGbaReleaseTimeOverride(int subId, int interval) {
10534 enforceModifyPermission();
10535
10536 final long identity = Binder.clearCallingIdentity();
10537 try {
10538 return getGbaManager(subId).overrideReleaseTime(interval);
10539 } finally {
10540 Binder.restoreCallingIdentity(identity);
10541 }
10542 }
10543
10544 /**
10545 * Return the release time for telephony to unbind GbaService.
10546 *
10547 * @param subId The sim that the GbaService is associated with.
10548 * @return The release time to unbind GbaService by millisecond.
10549 */
10550 @Override
10551 public int getGbaReleaseTime(int subId) {
10552 enforceReadPrivilegedPermission("getGbaReleaseTime");
10553
10554 final long identity = Binder.clearCallingIdentity();
10555 try {
10556 return getGbaManager(subId).getReleaseTime();
10557 } finally {
10558 Binder.restoreCallingIdentity(identity);
10559 }
10560 }
10561
10562 private GbaManager getGbaManager(int subId) {
10563 GbaManager instance = GbaManager.getInstance(subId);
10564 if (instance == null) {
10565 String packageName = mApp.getResources().getString(R.string.config_gba_package);
10566 int releaseTime = mApp.getResources().getInteger(R.integer.config_gba_release_time);
10567 instance = GbaManager.make(mApp, subId, packageName, releaseTime);
10568 }
10569 return instance;
10570 }
Hui Wang761a6682020-10-31 05:12:53 +000010571
10572 /**
10573 * indicate whether the device and the carrier can support
10574 * RCS VoLTE single registration.
10575 */
10576 @Override
10577 public boolean isRcsVolteSingleRegistrationCapable(int subId) {
Brad Ebinger34c09a52021-02-17 23:23:21 +000010578 TelephonyPermissions.enforceAnyPermissionGrantedOrCarrierPrivileges(mApp, subId,
10579 Binder.getCallingUid(), "isRcsVolteSingleRegistrationCapable",
10580 Manifest.permission.PERFORM_IMS_SINGLE_REGISTRATION,
10581 permission.READ_PRIVILEGED_PHONE_STATE);
Hui Wang761a6682020-10-31 05:12:53 +000010582
10583 if (!SubscriptionManager.isValidSubscriptionId(subId)) {
10584 throw new IllegalArgumentException("Invalid Subscription ID: " + subId);
10585 }
10586
10587 final long identity = Binder.clearCallingIdentity();
10588 try {
10589 RcsProvisioningMonitor rpm = RcsProvisioningMonitor.getInstance();
10590 if (rpm != null) {
Hui Wang67af90e2021-06-04 16:57:15 -070010591 Boolean isCapable = rpm.isRcsVolteSingleRegistrationEnabled(subId);
10592 if (isCapable != null) {
10593 return isCapable;
10594 }
Hui Wang761a6682020-10-31 05:12:53 +000010595 }
Hui Wang67af90e2021-06-04 16:57:15 -070010596 throw new ServiceSpecificException(ImsException.CODE_ERROR_SERVICE_UNAVAILABLE,
10597 "service is temporarily unavailable.");
Hui Wang761a6682020-10-31 05:12:53 +000010598 } finally {
10599 Binder.restoreCallingIdentity(identity);
10600 }
10601 }
10602
10603 /**
10604 * Register RCS provisioning callback.
10605 */
10606 @Override
Hui Wang3cac7e52021-01-27 14:45:25 -080010607 public void registerRcsProvisioningCallback(int subId,
Hui Wang761a6682020-10-31 05:12:53 +000010608 IRcsConfigCallback callback) {
Brad Ebinger34c09a52021-02-17 23:23:21 +000010609 TelephonyPermissions.enforceAnyPermissionGrantedOrCarrierPrivileges(mApp, subId,
Hui Wang3cac7e52021-01-27 14:45:25 -080010610 Binder.getCallingUid(), "registerRcsProvisioningCallback",
Brad Ebinger34c09a52021-02-17 23:23:21 +000010611 Manifest.permission.PERFORM_IMS_SINGLE_REGISTRATION,
10612 permission.READ_PRIVILEGED_PHONE_STATE);
Hui Wang761a6682020-10-31 05:12:53 +000010613
10614 if (!SubscriptionManager.isValidSubscriptionId(subId)) {
10615 throw new IllegalArgumentException("Invalid Subscription ID: " + subId);
10616 }
10617 if (!isImsAvailableOnDevice()) {
10618 throw new ServiceSpecificException(ImsException.CODE_ERROR_UNSUPPORTED_OPERATION,
10619 "IMS not available on device.");
10620 }
10621
10622 final long identity = Binder.clearCallingIdentity();
10623 try {
Hui Wang68cd3722021-01-11 20:04:53 -080010624 if (!RcsProvisioningMonitor.getInstance()
Hui Wang3cac7e52021-01-27 14:45:25 -080010625 .registerRcsProvisioningCallback(subId, callback)) {
Brad Ebinger919631e2021-06-02 17:46:35 -070010626 throw new ServiceSpecificException(ImsException.CODE_ERROR_INVALID_SUBSCRIPTION,
10627 "Active subscription not found.");
Hui Wang68cd3722021-01-11 20:04:53 -080010628 }
Hui Wang761a6682020-10-31 05:12:53 +000010629 } finally {
10630 Binder.restoreCallingIdentity(identity);
10631 }
10632 }
10633
10634 /**
10635 * Unregister RCS provisioning callback.
10636 */
10637 @Override
Hui Wang3cac7e52021-01-27 14:45:25 -080010638 public void unregisterRcsProvisioningCallback(int subId,
Hui Wang761a6682020-10-31 05:12:53 +000010639 IRcsConfigCallback callback) {
Brad Ebinger34c09a52021-02-17 23:23:21 +000010640 TelephonyPermissions.enforceAnyPermissionGrantedOrCarrierPrivileges(mApp, subId,
Hui Wang3cac7e52021-01-27 14:45:25 -080010641 Binder.getCallingUid(), "unregisterRcsProvisioningCallback",
Brad Ebinger34c09a52021-02-17 23:23:21 +000010642 Manifest.permission.PERFORM_IMS_SINGLE_REGISTRATION,
10643 permission.READ_PRIVILEGED_PHONE_STATE);
Hui Wang761a6682020-10-31 05:12:53 +000010644
10645 if (!SubscriptionManager.isValidSubscriptionId(subId)) {
10646 throw new IllegalArgumentException("Invalid Subscription ID: " + subId);
10647 }
10648 if (!isImsAvailableOnDevice()) {
10649 throw new ServiceSpecificException(ImsException.CODE_ERROR_UNSUPPORTED_OPERATION,
10650 "IMS not available on device.");
10651 }
10652
10653 final long identity = Binder.clearCallingIdentity();
10654 try {
Hui Wang68cd3722021-01-11 20:04:53 -080010655 RcsProvisioningMonitor.getInstance()
Hui Wang3cac7e52021-01-27 14:45:25 -080010656 .unregisterRcsProvisioningCallback(subId, callback);
Hui Wang761a6682020-10-31 05:12:53 +000010657 } finally {
10658 Binder.restoreCallingIdentity(identity);
10659 }
10660 }
10661
10662 /**
10663 * trigger RCS reconfiguration.
10664 */
10665 public void triggerRcsReconfiguration(int subId) {
Brad Ebinger34c09a52021-02-17 23:23:21 +000010666 TelephonyPermissions.enforceAnyPermissionGranted(mApp, Binder.getCallingUid(),
10667 "triggerRcsReconfiguration",
10668 Manifest.permission.PERFORM_IMS_SINGLE_REGISTRATION);
Hui Wang761a6682020-10-31 05:12:53 +000010669
10670 if (!SubscriptionManager.isValidSubscriptionId(subId)) {
10671 throw new IllegalArgumentException("Invalid Subscription ID: " + subId);
10672 }
10673 if (!isImsAvailableOnDevice()) {
10674 throw new ServiceSpecificException(ImsException.CODE_ERROR_UNSUPPORTED_OPERATION,
10675 "IMS not available on device.");
10676 }
10677
10678 final long identity = Binder.clearCallingIdentity();
10679 try {
10680 RcsProvisioningMonitor.getInstance().requestReconfig(subId);
10681 } finally {
10682 Binder.restoreCallingIdentity(identity);
10683 }
10684 }
10685
10686 /**
10687 * Provide the client configuration parameters of the RCS application.
10688 */
10689 public void setRcsClientConfiguration(int subId, RcsClientConfiguration rcc) {
Brad Ebinger34c09a52021-02-17 23:23:21 +000010690 TelephonyPermissions.enforceAnyPermissionGranted(mApp, Binder.getCallingUid(),
10691 "setRcsClientConfiguration",
10692 Manifest.permission.PERFORM_IMS_SINGLE_REGISTRATION);
Hui Wang761a6682020-10-31 05:12:53 +000010693
10694 if (!SubscriptionManager.isValidSubscriptionId(subId)) {
10695 throw new IllegalArgumentException("Invalid Subscription ID: " + subId);
10696 }
10697 if (!isImsAvailableOnDevice()) {
10698 throw new ServiceSpecificException(ImsException.CODE_ERROR_UNSUPPORTED_OPERATION,
10699 "IMS not available on device.");
10700 }
10701
10702 final long identity = Binder.clearCallingIdentity();
10703
10704 try {
10705 IImsConfig configBinder = getImsConfig(getSlotIndex(subId), ImsFeature.FEATURE_RCS);
10706 if (configBinder == null) {
10707 Rlog.e(LOG_TAG, "null result for setRcsClientConfiguration");
Brad Ebinger919631e2021-06-02 17:46:35 -070010708 throw new ServiceSpecificException(ImsException.CODE_ERROR_INVALID_SUBSCRIPTION,
10709 "could not find the requested subscription");
Hui Wang761a6682020-10-31 05:12:53 +000010710 } else {
10711 configBinder.setRcsClientConfiguration(rcc);
10712 }
joonhunshin3e154242021-09-17 06:33:39 +000010713
10714 RcsStats.getInstance().onRcsClientProvisioningStats(subId,
10715 RCS_CLIENT_PROVISIONING_STATS__EVENT__CLIENT_PARAMS_SENT);
Hui Wang761a6682020-10-31 05:12:53 +000010716 } catch (RemoteException e) {
10717 Rlog.e(LOG_TAG, "fail to setRcsClientConfiguration " + e.getMessage());
Brad Ebinger919631e2021-06-02 17:46:35 -070010718 throw new ServiceSpecificException(ImsException.CODE_ERROR_SERVICE_UNAVAILABLE,
10719 "service is temporarily unavailable.");
Hui Wang761a6682020-10-31 05:12:53 +000010720 } finally {
10721 Binder.restoreCallingIdentity(identity);
10722 }
10723 }
10724
10725 /**
Hui Wangbaaee6a2021-02-19 20:45:36 -080010726 * Enables or disables the test mode for RCS VoLTE single registration.
10727 */
10728 @Override
10729 public void setRcsSingleRegistrationTestModeEnabled(boolean enabled) {
10730 TelephonyPermissions.enforceShellOnly(Binder.getCallingUid(),
10731 "setRcsSingleRegistrationTestModeEnabled");
10732
10733 RcsProvisioningMonitor.getInstance().setTestModeEnabled(enabled);
10734 }
10735
10736 /**
10737 * Gets the test mode for RCS VoLTE single registration.
10738 */
10739 @Override
10740 public boolean getRcsSingleRegistrationTestModeEnabled() {
10741 TelephonyPermissions.enforceShellOnly(Binder.getCallingUid(),
10742 "getRcsSingleRegistrationTestModeEnabled");
10743
10744 return RcsProvisioningMonitor.getInstance().getTestModeEnabled();
10745 }
10746
10747 /**
Hui Wang761a6682020-10-31 05:12:53 +000010748 * Overrides the config of RCS VoLTE single registration enabled for the device.
10749 */
10750 @Override
10751 public void setDeviceSingleRegistrationEnabledOverride(String enabledStr) {
10752 TelephonyPermissions.enforceShellOnly(Binder.getCallingUid(),
10753 "setDeviceSingleRegistrationEnabledOverride");
10754 enforceModifyPermission();
10755
10756 Boolean enabled = "NULL".equalsIgnoreCase(enabledStr) ? null
10757 : Boolean.parseBoolean(enabledStr);
10758 RcsProvisioningMonitor.getInstance().overrideDeviceSingleRegistrationEnabled(enabled);
Brad Ebinger49a72b42021-01-29 00:55:24 +000010759 mApp.imsRcsController.setDeviceSingleRegistrationSupportOverride(enabled);
Hui Wang761a6682020-10-31 05:12:53 +000010760 }
10761
10762 /**
Tyler Gunn92479152021-01-20 16:30:10 -080010763 * Sends a device to device communication message. Only usable via shell.
10764 * @param message message to send.
10765 * @param value message value.
10766 */
10767 @Override
10768 public void sendDeviceToDeviceMessage(int message, int value) {
10769 TelephonyPermissions.enforceShellOnly(Binder.getCallingUid(),
Tyler Gunnbabbda02021-02-10 11:05:02 -080010770 "sendDeviceToDeviceMessage");
Tyler Gunn92479152021-01-20 16:30:10 -080010771 enforceModifyPermission();
10772
10773 final long identity = Binder.clearCallingIdentity();
10774 try {
10775 TelephonyConnectionService service =
10776 TelecomAccountRegistry.getInstance(null).getTelephonyConnectionService();
10777 if (service == null) {
10778 Rlog.e(LOG_TAG, "sendDeviceToDeviceMessage: not in a call.");
10779 return;
10780 }
10781 service.sendTestDeviceToDeviceMessage(message, value);
10782 } finally {
10783 Binder.restoreCallingIdentity(identity);
10784 }
10785 }
10786
Tyler Gunnbabbda02021-02-10 11:05:02 -080010787 /**
10788 * Sets the specified device to device transport active.
10789 * @param transport The transport to set active.
10790 */
10791 @Override
10792 public void setActiveDeviceToDeviceTransport(@NonNull String transport) {
10793 TelephonyPermissions.enforceShellOnly(Binder.getCallingUid(),
10794 "setActiveDeviceToDeviceTransport");
10795 enforceModifyPermission();
10796
10797 final long identity = Binder.clearCallingIdentity();
10798 try {
10799 TelephonyConnectionService service =
10800 TelecomAccountRegistry.getInstance(null).getTelephonyConnectionService();
10801 if (service == null) {
10802 Rlog.e(LOG_TAG, "setActiveDeviceToDeviceTransport: not in a call.");
10803 return;
10804 }
10805 service.setActiveDeviceToDeviceTransport(transport);
10806 } finally {
10807 Binder.restoreCallingIdentity(identity);
10808 }
10809 }
Tyler Gunn92479152021-01-20 16:30:10 -080010810
Tyler Gunnd4339262021-05-03 14:46:49 -070010811 @Override
10812 public void setDeviceToDeviceForceEnabled(boolean isForceEnabled) {
10813 TelephonyPermissions.enforceShellOnly(Binder.getCallingUid(),
10814 "setDeviceToDeviceForceEnabled");
10815
10816 final long identity = Binder.clearCallingIdentity();
10817 try {
10818 Arrays.stream(PhoneFactory.getPhones()).forEach(
10819 p -> {
10820 Phone thePhone = p.getImsPhone();
10821 if (thePhone != null && thePhone instanceof ImsPhone) {
10822 ImsPhone imsPhone = (ImsPhone) thePhone;
10823 CallTracker tracker = imsPhone.getCallTracker();
10824 if (tracker != null && tracker instanceof ImsPhoneCallTracker) {
10825 ImsPhoneCallTracker imsPhoneCallTracker =
10826 (ImsPhoneCallTracker) tracker;
10827 imsPhoneCallTracker.setDeviceToDeviceForceEnabled(isForceEnabled);
10828 }
10829 }
10830 }
10831 );
10832 } finally {
10833 Binder.restoreCallingIdentity(identity);
10834 }
10835 }
10836
Tyler Gunn92479152021-01-20 16:30:10 -080010837 /**
Hui Wang761a6682020-10-31 05:12:53 +000010838 * Gets the config of RCS VoLTE single registration enabled for the device.
10839 */
10840 @Override
10841 public boolean getDeviceSingleRegistrationEnabled() {
10842 enforceReadPrivilegedPermission("getDeviceSingleRegistrationEnabled");
10843 return RcsProvisioningMonitor.getInstance().getDeviceSingleRegistrationEnabled();
10844 }
10845
10846 /**
10847 * Overrides the config of RCS VoLTE single registration enabled for the carrier/subscription.
10848 */
10849 @Override
10850 public boolean setCarrierSingleRegistrationEnabledOverride(int subId, String enabledStr) {
10851 TelephonyPermissions.enforceShellOnly(Binder.getCallingUid(),
10852 "setCarrierSingleRegistrationEnabledOverride");
10853 enforceModifyPermission();
10854
10855 Boolean enabled = "NULL".equalsIgnoreCase(enabledStr) ? null
10856 : Boolean.parseBoolean(enabledStr);
10857 return RcsProvisioningMonitor.getInstance().overrideCarrierSingleRegistrationEnabled(
10858 subId, enabled);
10859 }
10860
10861 /**
10862 * Gets the config of RCS VoLTE single registration enabled for the carrier/subscription.
10863 */
10864 @Override
10865 public boolean getCarrierSingleRegistrationEnabled(int subId) {
10866 enforceReadPrivilegedPermission("getCarrierSingleRegistrationEnabled");
10867 return RcsProvisioningMonitor.getInstance().getCarrierSingleRegistrationEnabled(subId);
10868 }
Chiachang Wangd6d34772020-12-22 11:38:27 +080010869
10870 /**
Hui Wangb647abe2021-02-26 09:33:38 -080010871 * Overrides the ims feature validation result
10872 */
10873 @Override
10874 public boolean setImsFeatureValidationOverride(int subId, String enabledStr) {
10875 TelephonyPermissions.enforceShellOnly(Binder.getCallingUid(),
10876 "setImsFeatureValidationOverride");
10877
10878 Boolean enabled = "NULL".equalsIgnoreCase(enabledStr) ? null
10879 : Boolean.parseBoolean(enabledStr);
10880 return RcsProvisioningMonitor.getInstance().overrideImsFeatureValidation(
10881 subId, enabled);
10882 }
10883
10884 /**
10885 * Gets the ims feature validation override value
10886 */
10887 @Override
10888 public boolean getImsFeatureValidationOverride(int subId) {
10889 TelephonyPermissions.enforceShellOnly(Binder.getCallingUid(),
10890 "getImsFeatureValidationOverride");
10891 return RcsProvisioningMonitor.getInstance().getImsFeatureValidationOverride(subId);
10892 }
10893
10894 /**
Chiachang Wangd6d34772020-12-22 11:38:27 +080010895 * Get the mobile provisioning url that is used to launch a browser to allow users to manage
10896 * their mobile plan.
10897 */
10898 @Override
10899 public String getMobileProvisioningUrl() {
10900 enforceReadPrivilegedPermission("getMobileProvisioningUrl");
10901 final long identity = Binder.clearCallingIdentity();
10902 try {
10903 return getDefaultPhone().getMobileProvisioningUrl();
10904 } finally {
10905 Binder.restoreCallingIdentity(identity);
10906 }
10907 }
Rambo Wanga5cc9b72021-01-07 10:51:54 -080010908
James.cf Linbcdf8b32021-01-14 16:44:13 +080010909 /**
calvinpane4a8a1d2021-01-25 13:51:18 +080010910 * Get the EAB contact from the EAB database.
10911 */
10912 @Override
10913 public String getContactFromEab(String contact) {
10914 TelephonyPermissions.enforceShellOnly(Binder.getCallingUid(), "getContactFromEab");
10915 enforceModifyPermission();
10916 final long identity = Binder.clearCallingIdentity();
10917 try {
10918 return EabUtil.getContactFromEab(getDefaultPhone().getContext(), contact);
10919 } finally {
10920 Binder.restoreCallingIdentity(identity);
10921 }
10922 }
10923
10924 /**
Calvin Pana1434322021-07-01 19:27:01 +080010925 * Get the EAB capability from the EAB database.
10926 */
10927 @Override
10928 public String getCapabilityFromEab(String contact) {
10929 TelephonyPermissions.enforceShellOnly(Binder.getCallingUid(), "getCapabilityFromEab");
10930 enforceModifyPermission();
10931 final long identity = Binder.clearCallingIdentity();
10932 try {
10933 return EabUtil.getCapabilityFromEab(getDefaultPhone().getContext(), contact);
10934 } finally {
10935 Binder.restoreCallingIdentity(identity);
10936 }
10937 }
10938
10939 /**
James.cf Linbcdf8b32021-01-14 16:44:13 +080010940 * Remove the EAB contacts from the EAB database.
10941 */
10942 @Override
10943 public int removeContactFromEab(int subId, String contacts) {
10944 TelephonyPermissions.enforceShellOnly(Binder.getCallingUid(), "removeCapabilitiesFromEab");
10945 enforceModifyPermission();
10946 final long identity = Binder.clearCallingIdentity();
10947 try {
10948 return EabUtil.removeContactFromEab(subId, contacts, getDefaultPhone().getContext());
10949 } finally {
10950 Binder.restoreCallingIdentity(identity);
10951 }
10952 }
10953
Rambo Wanga5cc9b72021-01-07 10:51:54 -080010954 @Override
James.cf Lin4b784aa2021-01-31 03:25:15 +080010955 public boolean getDeviceUceEnabled() {
10956 TelephonyPermissions.enforceShellOnly(Binder.getCallingUid(), "getDeviceUceEnabled");
10957 final long identity = Binder.clearCallingIdentity();
10958 try {
10959 return mApp.getDeviceUceEnabled();
10960 } finally {
10961 Binder.restoreCallingIdentity(identity);
10962 }
10963 }
10964
10965 @Override
10966 public void setDeviceUceEnabled(boolean isEnabled) {
10967 TelephonyPermissions.enforceShellOnly(Binder.getCallingUid(), "setDeviceUceEnabled");
10968 final long identity = Binder.clearCallingIdentity();
10969 try {
10970 mApp.setDeviceUceEnabled(isEnabled);
10971 } finally {
10972 Binder.restoreCallingIdentity(identity);
10973 }
10974 }
10975
Brad Ebinger14d467f2021-02-12 06:18:28 +000010976 /**
10977 * Add new feature tags to the Set used to calculate the capabilities in PUBLISH.
10978 * @return current RcsContactUceCapability instance that will be used for PUBLISH.
10979 */
10980 // Used for SHELL command only right now.
10981 @Override
10982 public RcsContactUceCapability addUceRegistrationOverrideShell(int subId,
10983 List<String> featureTags) {
10984 TelephonyPermissions.enforceShellOnly(Binder.getCallingUid(),
10985 "addUceRegistrationOverrideShell");
10986 final long identity = Binder.clearCallingIdentity();
10987 try {
10988 return mApp.imsRcsController.addUceRegistrationOverrideShell(subId,
10989 new ArraySet<>(featureTags));
10990 } catch (ImsException e) {
10991 throw new ServiceSpecificException(e.getCode(), e.getMessage());
10992 } finally {
10993 Binder.restoreCallingIdentity(identity);
10994 }
10995 }
10996
10997 /**
10998 * Remove existing feature tags to the Set used to calculate the capabilities in PUBLISH.
10999 * @return current RcsContactUceCapability instance that will be used for PUBLISH.
11000 */
11001 // Used for SHELL command only right now.
11002 @Override
11003 public RcsContactUceCapability removeUceRegistrationOverrideShell(int subId,
11004 List<String> featureTags) {
11005 TelephonyPermissions.enforceShellOnly(Binder.getCallingUid(),
11006 "removeUceRegistrationOverrideShell");
11007 final long identity = Binder.clearCallingIdentity();
11008 try {
11009 return mApp.imsRcsController.removeUceRegistrationOverrideShell(subId,
11010 new ArraySet<>(featureTags));
11011 } catch (ImsException e) {
11012 throw new ServiceSpecificException(e.getCode(), e.getMessage());
11013 } finally {
11014 Binder.restoreCallingIdentity(identity);
11015 }
11016 }
11017
11018 /**
11019 * Clear all overrides in the Set used to calculate the capabilities in PUBLISH.
11020 * @return current RcsContactUceCapability instance that will be used for PUBLISH.
11021 */
11022 // Used for SHELL command only right now.
11023 @Override
11024 public RcsContactUceCapability clearUceRegistrationOverrideShell(int subId) {
11025 TelephonyPermissions.enforceShellOnly(Binder.getCallingUid(),
11026 "clearUceRegistrationOverrideShell");
11027 final long identity = Binder.clearCallingIdentity();
11028 try {
11029 return mApp.imsRcsController.clearUceRegistrationOverrideShell(subId);
11030 } catch (ImsException e) {
11031 throw new ServiceSpecificException(e.getCode(), e.getMessage());
11032 } finally {
11033 Binder.restoreCallingIdentity(identity);
11034 }
11035 }
11036
11037 /**
11038 * @return current RcsContactUceCapability instance that will be used for PUBLISH.
11039 */
11040 // Used for SHELL command only right now.
11041 @Override
11042 public RcsContactUceCapability getLatestRcsContactUceCapabilityShell(int subId) {
11043 TelephonyPermissions.enforceShellOnly(Binder.getCallingUid(),
11044 "getLatestRcsContactUceCapabilityShell");
11045 final long identity = Binder.clearCallingIdentity();
11046 try {
11047 return mApp.imsRcsController.getLatestRcsContactUceCapabilityShell(subId);
11048 } catch (ImsException e) {
11049 throw new ServiceSpecificException(e.getCode(), e.getMessage());
11050 } finally {
11051 Binder.restoreCallingIdentity(identity);
11052 }
11053 }
11054
11055 /**
11056 * Returns the last PIDF XML sent to the network during the last PUBLISH or "none" if the
11057 * device does not have an active PUBLISH.
11058 */
11059 // Used for SHELL command only right now.
11060 @Override
11061 public String getLastUcePidfXmlShell(int subId) {
11062 TelephonyPermissions.enforceShellOnly(Binder.getCallingUid(), "uceGetLastPidfXml");
11063 final long identity = Binder.clearCallingIdentity();
11064 try {
11065 return mApp.imsRcsController.getLastUcePidfXmlShell(subId);
11066 } catch (ImsException e) {
11067 throw new ServiceSpecificException(e.getCode(), e.getMessage());
11068 } finally {
11069 Binder.restoreCallingIdentity(identity);
11070 }
11071 }
11072
James.cf Line8713a42021-04-29 16:04:26 +080011073 /**
11074 * Remove UCE requests cannot be sent to the network status.
11075 */
11076 // Used for SHELL command only right now.
11077 @Override
11078 public boolean removeUceRequestDisallowedStatus(int subId) {
11079 TelephonyPermissions.enforceShellOnly(Binder.getCallingUid(), "uceRemoveDisallowedStatus");
11080 final long identity = Binder.clearCallingIdentity();
11081 try {
11082 return mApp.imsRcsController.removeUceRequestDisallowedStatus(subId);
11083 } catch (ImsException e) {
11084 throw new ServiceSpecificException(e.getCode(), e.getMessage());
11085 } finally {
11086 Binder.restoreCallingIdentity(identity);
11087 }
11088 }
11089
James.cf Lin18bb9002021-05-25 01:37:38 +080011090 /**
11091 * Remove UCE requests cannot be sent to the network status.
11092 */
11093 // Used for SHELL command only.
11094 @Override
11095 public boolean setCapabilitiesRequestTimeout(int subId, long timeoutAfterMs) {
11096 TelephonyPermissions.enforceShellOnly(Binder.getCallingUid(), "setCapRequestTimeout");
11097 final long identity = Binder.clearCallingIdentity();
11098 try {
11099 return mApp.imsRcsController.setCapabilitiesRequestTimeout(subId, timeoutAfterMs);
11100 } catch (ImsException e) {
11101 throw new ServiceSpecificException(e.getCode(), e.getMessage());
11102 } finally {
11103 Binder.restoreCallingIdentity(identity);
11104 }
11105 }
Brad Ebinger14d467f2021-02-12 06:18:28 +000011106
James.cf Lin4b784aa2021-01-31 03:25:15 +080011107 @Override
Rambo Wanga5cc9b72021-01-07 10:51:54 -080011108 public void setSignalStrengthUpdateRequest(int subId, SignalStrengthUpdateRequest request,
11109 String callingPackage) {
11110 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
11111 mApp, subId, "setSignalStrengthUpdateRequest");
11112
11113 final int callingUid = Binder.getCallingUid();
11114 // Verify that tha callingPackage belongs to the calling UID
11115 mApp.getSystemService(AppOpsManager.class)
11116 .checkPackage(callingUid, callingPackage);
11117
Rambo Wang3607f502021-02-01 21:51:40 -080011118 validateSignalStrengthUpdateRequest(mApp, request, callingUid);
Rambo Wanga5cc9b72021-01-07 10:51:54 -080011119
11120 final long identity = Binder.clearCallingIdentity();
11121 try {
11122 Object result = sendRequest(CMD_SET_SIGNAL_STRENGTH_UPDATE_REQUEST,
11123 new Pair<Integer, SignalStrengthUpdateRequest>(callingUid, request), subId);
11124
11125 if (result instanceof IllegalStateException) {
11126 throw (IllegalStateException) result;
11127 }
11128 } finally {
11129 Binder.restoreCallingIdentity(identity);
11130 }
11131 }
11132
11133 @Override
11134 public void clearSignalStrengthUpdateRequest(int subId, SignalStrengthUpdateRequest request,
11135 String callingPackage) {
11136 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
11137 mApp, subId, "clearSignalStrengthUpdateRequest");
11138
11139 final int callingUid = Binder.getCallingUid();
11140 // Verify that tha callingPackage belongs to the calling UID
11141 mApp.getSystemService(AppOpsManager.class)
11142 .checkPackage(callingUid, callingPackage);
11143
11144 final long identity = Binder.clearCallingIdentity();
11145 try {
11146 Object result = sendRequest(CMD_CLEAR_SIGNAL_STRENGTH_UPDATE_REQUEST,
11147 new Pair<Integer, SignalStrengthUpdateRequest>(callingUid, request), subId);
11148
11149 if (result instanceof IllegalStateException) {
11150 throw (IllegalStateException) result;
11151 }
11152 } finally {
11153 Binder.restoreCallingIdentity(identity);
11154 }
11155 }
11156
Rambo Wang3607f502021-02-01 21:51:40 -080011157 private static void validateSignalStrengthUpdateRequest(Context context,
11158 SignalStrengthUpdateRequest request, int callingUid) {
Rambo Wanga5cc9b72021-01-07 10:51:54 -080011159 if (callingUid == Process.PHONE_UID || callingUid == Process.SYSTEM_UID) {
11160 // phone/system process do not have further restriction on request
11161 return;
11162 }
11163
11164 // Applications has restrictions on how to use the request:
Rambo Wang3607f502021-02-01 21:51:40 -080011165 // Non-system callers need permission to set mIsSystemThresholdReportingRequestedWhileIdle
Rambo Wanga5cc9b72021-01-07 10:51:54 -080011166 if (request.isSystemThresholdReportingRequestedWhileIdle()) {
Rambo Wang3607f502021-02-01 21:51:40 -080011167 context.enforceCallingOrSelfPermission(
11168 android.Manifest.permission.LISTEN_ALWAYS_REPORTED_SIGNAL_STRENGTH,
11169 "validateSignalStrengthUpdateRequest");
Rambo Wanga5cc9b72021-01-07 10:51:54 -080011170 }
11171
11172 for (SignalThresholdInfo info : request.getSignalThresholdInfos()) {
11173 // Only system caller can set mHysteresisMs/mHysteresisDb/mIsEnabled.
11174 if (info.getHysteresisMs() != SignalThresholdInfo.HYSTERESIS_MS_DISABLED
11175 || info.getHysteresisDb() != SignalThresholdInfo.HYSTERESIS_DB_DISABLED
11176 || info.isEnabled()) {
11177 throw new IllegalArgumentException(
11178 "Only system can set hide fields in SignalThresholdInfo");
11179 }
11180
11181 // Thresholds length for each RAN need in range. This has been validated in
11182 // SignalThresholdInfo#Builder#setThreshold. Here we prevent apps calling hide method
11183 // setThresholdUnlimited (e.g. through reflection) with too short or too long thresholds
11184 final int[] thresholds = info.getThresholds();
11185 Objects.requireNonNull(thresholds);
11186 if (thresholds.length < SignalThresholdInfo.getMinimumNumberOfThresholdsAllowed()
11187 || thresholds.length
11188 > SignalThresholdInfo.getMaximumNumberOfThresholdsAllowed()) {
11189 throw new IllegalArgumentException(
11190 "thresholds length is out of range: " + thresholds.length);
11191 }
11192 }
11193 }
SongFerngWang8236caa2021-01-17 21:51:44 +080011194
11195 /**
11196 * Gets the current phone capability.
11197 *
11198 * Requires carrier privileges or READ_PRECISE_PHONE_STATE permission.
11199 * @return the PhoneCapability which describes the data connection capability of modem.
11200 * It's used to evaluate possible phone config change, for example from single
11201 * SIM device to multi-SIM device.
11202 */
11203 @Override
11204 public PhoneCapability getPhoneCapability() {
11205 enforceReadPrivilegedPermission("getPhoneCapability");
11206 final long identity = Binder.clearCallingIdentity();
11207 try {
11208 return mPhoneConfigurationManager.getCurrentPhoneCapability();
11209 } finally {
11210 Binder.restoreCallingIdentity(identity);
11211 }
11212 }
Michele Berionne5e411512020-11-13 02:36:59 +000011213
11214 /**
11215 * Prepare TelephonyManager for an unattended reboot. The reboot is
11216 * required to be done shortly after the API is invoked.
11217 */
11218 @Override
11219 @TelephonyManager.PrepareUnattendedRebootResult
11220 public int prepareForUnattendedReboot() {
Rafael Higuera Silvad9630642021-09-20 15:32:01 +000011221 WorkSource workSource = getWorkSource(Binder.getCallingUid());
Michele Berionne5e411512020-11-13 02:36:59 +000011222 enforceRebootPermission();
11223
11224 final long identity = Binder.clearCallingIdentity();
11225 try {
Rafael Higuera Silvad9630642021-09-20 15:32:01 +000011226 return (int) sendRequest(CMD_PREPARE_UNATTENDED_REBOOT, null, workSource);
Michele Berionne5e411512020-11-13 02:36:59 +000011227 } finally {
11228 Binder.restoreCallingIdentity(identity);
11229 }
11230 }
Hongbo Zeng156aa4a2021-02-08 21:50:28 +080011231
11232 /**
11233 * Request to get the current slicing configuration including URSP rules and
11234 * NSSAIs (configured, allowed and rejected).
11235 *
11236 * Requires carrier privileges or READ_PRIVILEGED_PHONE_STATE permission.
11237 */
11238 @Override
11239 public void getSlicingConfig(ResultReceiver callback) {
11240 enforceReadPrivilegedPermission("getSlicingConfig");
11241
11242 final long identity = Binder.clearCallingIdentity();
11243 try {
11244 Phone phone = getDefaultPhone();
11245 sendRequestAsync(CMD_GET_SLICING_CONFIG, callback, phone, null);
11246 } finally {
11247 Binder.restoreCallingIdentity(identity);
11248 }
11249 }
Hunsuk Choi3b742d62021-10-25 19:48:34 +000011250
11251 /**
11252 * Register an IMS connection state callback
11253 */
11254 @Override
Hunsuk Choi89bd22c2021-11-01 13:04:54 +000011255 public void registerImsStateCallback(int subId, int feature, IImsStateCallback cb,
11256 String callingPackage) {
Hunsuk Choi3b742d62021-10-25 19:48:34 +000011257 if (feature == ImsFeature.FEATURE_MMTEL) {
11258 // ImsMmTelManager
11259 // The following also checks READ_PRIVILEGED_PHONE_STATE.
11260 TelephonyPermissions
11261 .enforceCallingOrSelfReadPrecisePhoneStatePermissionOrCarrierPrivilege(
11262 mApp, subId, "registerImsStateCallback");
11263 } else if (feature == ImsFeature.FEATURE_RCS) {
11264 // ImsRcsManager or SipDelegateManager
11265 TelephonyPermissions.enforceAnyPermissionGrantedOrCarrierPrivileges(mApp, subId,
11266 Binder.getCallingUid(), "registerImsStateCallback",
11267 Manifest.permission.READ_PRIVILEGED_PHONE_STATE,
11268 Manifest.permission.READ_PRECISE_PHONE_STATE,
11269 Manifest.permission.ACCESS_RCS_USER_CAPABILITY_EXCHANGE,
11270 Manifest.permission.PERFORM_IMS_SINGLE_REGISTRATION);
11271 }
11272
11273 if (!ImsManager.isImsSupportedOnDevice(mApp)) {
11274 throw new ServiceSpecificException(ImsException.CODE_ERROR_UNSUPPORTED_OPERATION,
11275 "IMS not available on device.");
11276 }
11277
11278 if (subId == SubscriptionManager.DEFAULT_SUBSCRIPTION_ID) {
11279 throw new ServiceSpecificException(ImsException.CODE_ERROR_INVALID_SUBSCRIPTION);
11280 }
11281
11282 ImsStateCallbackController controller = ImsStateCallbackController.getInstance();
11283 if (controller == null) {
11284 throw new ServiceSpecificException(ImsException.CODE_ERROR_UNSUPPORTED_OPERATION,
11285 "IMS not available on device.");
11286 }
11287
Hunsuk Choi89bd22c2021-11-01 13:04:54 +000011288 if (callingPackage == null) {
11289 callingPackage = getCurrentPackageName();
11290 }
11291
Hunsuk Choi3b742d62021-10-25 19:48:34 +000011292 final long token = Binder.clearCallingIdentity();
11293 try {
11294 int slotId = getSlotIndexOrException(subId);
Hunsuk Choi89bd22c2021-11-01 13:04:54 +000011295 controller.registerImsStateCallback(subId, feature, cb, callingPackage);
Hunsuk Choi3b742d62021-10-25 19:48:34 +000011296 } catch (ImsException e) {
11297 throw new ServiceSpecificException(e.getCode());
11298 } finally {
11299 Binder.restoreCallingIdentity(token);
11300 }
11301 }
11302
11303 /**
11304 * Unregister an IMS connection state callback
11305 */
11306 @Override
11307 public void unregisterImsStateCallback(IImsStateCallback cb) {
11308 final long token = Binder.clearCallingIdentity();
11309 ImsStateCallbackController controller = ImsStateCallbackController.getInstance();
11310 if (controller == null) {
11311 return;
11312 }
11313 try {
11314 controller.unregisterImsStateCallback(cb);
11315 } finally {
11316 Binder.restoreCallingIdentity(token);
11317 }
11318 }
Sooraj Sasindranfae41b32021-10-26 02:10:05 -070011319
11320 /**
11321 * @return {@CellIdentity} last known cell identity {@CellIdentity}.
11322 *
11323 * Require {@link android.Manifest.permission#ACCESS_FINE_LOCATION} and
11324 * com.android.phone.permission.ACCESS_LAST_KNOWN_CELL_ID, otherwise throws
11325 * SecurityException.
11326 * If there is current registered network this value will be same as the registered cell
11327 * identity. If the device goes out of service the previous cell identity is cached and
11328 * will be returned. If the cache age of the Cell identity is more than 24 hours
11329 * it will be cleared and null will be returned.
11330 *
11331 */
11332 @Override
11333 public @Nullable CellIdentity getLastKnownCellIdentity(int subId, String callingPackage,
11334 String callingFeatureId) {
11335 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
11336 LocationAccessPolicy.LocationPermissionResult fineLocationResult =
11337 LocationAccessPolicy.checkLocationPermission(mApp,
11338 new LocationAccessPolicy.LocationPermissionQuery.Builder()
11339 .setCallingPackage(callingPackage)
11340 .setCallingFeatureId(callingFeatureId)
11341 .setCallingPid(Binder.getCallingPid())
11342 .setCallingUid(Binder.getCallingUid())
11343 .setMethod("getLastKnownCellIdentity")
11344 .setLogAsInfo(true)
11345 .setMinSdkVersionForFine(Build.VERSION_CODES.Q)
11346 .setMinSdkVersionForCoarse(Build.VERSION_CODES.Q)
11347 .setMinSdkVersionForEnforcement(Build.VERSION_CODES.Q)
11348 .build());
11349
11350 boolean hasFinePermission =
11351 fineLocationResult == LocationAccessPolicy.LocationPermissionResult.ALLOWED;
11352 if (!hasFinePermission
11353 || !TelephonyPermissions.checkLastKnownCellIdAccessPermission(mApp)) {
11354 throw new SecurityException("getLastKnownCellIdentity need ACCESS_FINE_LOCATION "
11355 + "and BIND_CONNECTION_SERVICE permission.");
11356 }
11357
11358 final long identity = Binder.clearCallingIdentity();
11359 try {
11360 Phone phone = getPhone(subId);
11361 if (phone == null) return null;
11362 ServiceStateTracker sst = phone.getServiceStateTracker();
11363 if (sst == null) return null;
11364 return sst.getLastKnownCellIdentity();
11365 } finally {
11366 Binder.restoreCallingIdentity(identity);
11367 }
11368 }
Jack Yu4c0a5502021-12-03 23:58:26 -080011369
11370 @Override
11371 public boolean isUsingNewDataStack() {
11372 TelephonyPermissions.enforceShellOnly(Binder.getCallingUid(), "isUsingNewDataStack");
11373 return getDefaultPhone().isUsingNewDataStack();
11374 }
jimsun3b9ccac2021-10-26 15:01:23 +080011375
11376 /**
11377 * Sets the modem service class Name that Telephony will bind to.
11378 *
11379 * @param serviceName The class name of the modem service.
11380 * @return true if the operation is succeed, otherwise false.
11381 */
11382 public boolean setModemService(String serviceName) {
11383 Log.d(LOG_TAG, "setModemService - " + serviceName);
11384 TelephonyPermissions.enforceShellOnly(Binder.getCallingUid(), "setModemService");
11385 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(mApp,
11386 SubscriptionManager.INVALID_SUBSCRIPTION_ID,
11387 "setModemService");
11388 return mPhoneConfigurationManager.setModemService(serviceName);
11389 }
11390
11391 /**
11392 * Return the class name of the currently bounded modem service.
11393 *
11394 * @return the class name of the modem service.
11395 */
11396 public String getModemService() {
11397 String result;
11398 Log.d(LOG_TAG, "getModemService");
11399 TelephonyPermissions.enforceShellOnly(Binder.getCallingUid(), "getModemService");
11400 TelephonyPermissions
11401 .enforceCallingOrSelfReadPrivilegedPhoneStatePermissionOrCarrierPrivilege(
11402 mApp, SubscriptionManager.INVALID_SUBSCRIPTION_ID,
11403 "getModemService");
11404 result = mPhoneConfigurationManager.getModemService();
11405 Log.d(LOG_TAG, "result = " + result);
11406 return result;
11407 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -070011408}