blob: ebb2c51ac34433f564c4c30b3ba5aa22bdaec655 [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;
Tyler Gunn64144d92022-03-17 14:16:41 -070034import android.app.PropertyInvalidatedCache;
Brad Ebinger4f6208e2021-03-23 21:04:45 +000035import android.app.compat.CompatChanges;
Hall Liu82694d52020-12-11 18:22:04 -080036import android.app.role.RoleManager;
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;
Shishir Agrawal60f9c952014-06-23 12:00:43 -070045import android.content.pm.PackageManager;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070046import android.net.Uri;
47import android.os.AsyncResult;
48import android.os.Binder;
Hall Liuf19c44f2018-11-27 14:38:17 -080049import android.os.Build;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070050import android.os.Bundle;
51import android.os.Handler;
yinxu504e1392017-04-12 16:03:22 -070052import android.os.IBinder;
tom hsue9360cd2022-09-29 23:49:21 +080053import android.os.LocaleList;
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;
Gary Jian3aa9a762022-01-24 16:41:19 +080076import android.telephony.AccessNetworkConstants;
77import android.telephony.ActivityStatsTechSpecificInfo;
Chen Xu227e06f2019-09-26 22:48:11 -070078import android.telephony.Annotation.ApnType;
Jack Yu0eda6842022-04-18 00:34:46 -070079import android.telephony.Annotation.DataActivityType;
Jack Nudelmanb0b87642020-11-12 15:04:39 -080080import android.telephony.Annotation.ThermalMitigationResult;
Shuo Qian4a594052020-01-23 11:59:30 -080081import android.telephony.CallForwardingInfo;
Junda Liu12f7d802015-05-01 12:06:44 -070082import android.telephony.CarrierConfigManager;
Michele Berionne482f8202018-11-27 18:57:59 -080083import android.telephony.CarrierRestrictionRules;
yincheng zhao2737e882019-09-06 17:06:54 -070084import android.telephony.CellIdentity;
Meng Wanga10e89e2019-12-09 13:13:01 -080085import android.telephony.CellIdentityCdma;
86import android.telephony.CellIdentityGsm;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070087import android.telephony.CellInfo;
Nathan Haroldf180aac2018-06-01 18:43:55 -070088import android.telephony.CellInfoGsm;
89import android.telephony.CellInfoWcdma;
Sooraj Sasindran9a909312016-07-18 11:57:25 -070090import android.telephony.ClientRequestStats;
Jack Nudelmanb0b87642020-11-12 15:04:39 -080091import android.telephony.DataThrottlingRequest;
Hui Wang641e81c2020-10-12 12:14:23 -070092import android.telephony.IBootstrapAuthenticationCallback;
Nathan Haroldfa8da0f2018-09-27 18:51:29 -070093import android.telephony.ICellInfoCallback;
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -070094import android.telephony.IccOpenLogicalChannelResponse;
Hall Liu1aa510f2017-11-22 17:40:08 -080095import android.telephony.LocationAccessPolicy;
Ta-wei Yen87c49842016-05-13 21:19:52 -070096import android.telephony.ModemActivityInfo;
Jake Hambye994d462014-02-03 13:10:13 -080097import android.telephony.NeighboringCellInfo;
yinxu504e1392017-04-12 16:03:22 -070098import android.telephony.NetworkScanRequest;
Michele4245e952019-02-04 11:36:23 -080099import android.telephony.PhoneCapability;
Hall Liud892bec2018-11-30 14:51:45 -0800100import android.telephony.PhoneNumberRange;
Wink Saville5d475dd2014-10-17 15:00:58 -0700101import android.telephony.RadioAccessFamily;
Hall Liub2ac8ef2019-02-28 15:56:23 -0800102import android.telephony.RadioAccessSpecifier;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700103import android.telephony.ServiceState;
Nathan Harold46b42aa2017-03-10 19:38:22 -0800104import android.telephony.SignalStrength;
Rambo Wanga5cc9b72021-01-07 10:51:54 -0800105import android.telephony.SignalStrengthUpdateRequest;
106import android.telephony.SignalThresholdInfo;
Wink Saville0f3b5fc2014-11-11 08:40:49 -0800107import android.telephony.SubscriptionInfo;
Jeff Sharkey85190e62014-12-05 09:40:12 -0800108import android.telephony.SubscriptionManager;
Peter Wangc035ce42020-01-08 21:00:22 -0800109import android.telephony.TelephonyFrameworkInitializer;
Sanket Padawe99ef1e32016-05-18 16:12:33 -0700110import android.telephony.TelephonyHistogram;
Ta-wei Yenb6929602016-05-24 15:48:27 -0700111import android.telephony.TelephonyManager;
Ryan Savitskicab25c52023-03-14 14:23:49 +0000112import android.telephony.TelephonyManager.SimState;
Hall Liub2ac8ef2019-02-28 15:56:23 -0800113import android.telephony.TelephonyScanManager;
Jack Nudelmanb0b87642020-11-12 15:04:39 -0800114import android.telephony.ThermalMitigationRequest;
Jordan Liu5aa07002018-12-18 15:44:48 -0800115import android.telephony.UiccCardInfo;
sandeepjsb6c87872021-09-27 15:34:44 +0000116import android.telephony.UiccPortInfo;
Holly Jiuyu Sun01c47ad2018-01-24 17:56:33 +0000117import android.telephony.UiccSlotInfo;
sandeepjsb6c87872021-09-27 15:34:44 +0000118import android.telephony.UiccSlotMapping;
Tyler Gunn65d45c22017-06-05 11:22:26 -0700119import android.telephony.UssdResponse;
Ta-wei Yenb6929602016-05-24 15:48:27 -0700120import android.telephony.VisualVoicemailSmsFilterSettings;
Hongbo Zeng0e18b162021-04-07 16:52:18 +0800121import android.telephony.data.NetworkSlicingConfig;
Jack Yub5d8f642018-11-26 11:20:48 -0800122import android.telephony.emergency.EmergencyNumber;
Hui Wang641e81c2020-10-12 12:14:23 -0700123import android.telephony.gba.GbaAuthRequest;
124import android.telephony.gba.UaSecurityProtocolIdentifier;
Brad Ebinger1ce9c432019-07-16 13:19:44 -0700125import android.telephony.ims.ImsException;
Brad Ebinger1c8542e2019-01-14 13:43:14 -0800126import android.telephony.ims.ProvisioningManager;
Hui Wang761a6682020-10-31 05:12:53 +0000127import android.telephony.ims.RcsClientConfiguration;
Brad Ebinger14d467f2021-02-12 06:18:28 +0000128import android.telephony.ims.RcsContactUceCapability;
Brad Ebingera34a6c22019-10-22 17:36:18 -0700129import android.telephony.ims.RegistrationManager;
joonhunshincffb7fc2021-11-28 07:32:01 +0000130import android.telephony.ims.aidl.IFeatureProvisioningCallback;
Brad Ebinger35c841c2018-10-01 10:40:55 -0700131import android.telephony.ims.aidl.IImsCapabilityCallback;
Brad Ebinger22bc3e42018-01-16 09:39:35 -0800132import android.telephony.ims.aidl.IImsConfig;
Brad Ebingerdf5b4f02018-10-31 11:24:17 -0700133import android.telephony.ims.aidl.IImsConfigCallback;
Brad Ebinger22bc3e42018-01-16 09:39:35 -0800134import android.telephony.ims.aidl.IImsRegistration;
Brad Ebinger35c841c2018-10-01 10:40:55 -0700135import android.telephony.ims.aidl.IImsRegistrationCallback;
Hui Wang761a6682020-10-31 05:12:53 +0000136import android.telephony.ims.aidl.IRcsConfigCallback;
Brad Ebingerbc7dd582019-10-17 17:03:22 -0700137import android.telephony.ims.feature.ImsFeature;
Brad Ebinger1c8542e2019-01-14 13:43:14 -0800138import android.telephony.ims.stub.ImsConfigImplBase;
Brad Ebinger1f2b5082018-02-08 16:11:32 -0800139import android.telephony.ims.stub.ImsRegistrationImplBase;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700140import android.text.TextUtils;
Jeff Sharkey85190e62014-12-05 09:40:12 -0800141import android.util.ArraySet;
Hall Liud60acc92020-05-21 17:09:35 -0700142import android.util.EventLog;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700143import android.util.Log;
Jake Hambye994d462014-02-03 13:10:13 -0800144import android.util.Pair;
Ta-wei Yen30a69c82016-12-27 14:52:32 -0800145
Andrew Lee312e8172014-10-23 17:01:36 -0700146import com.android.ims.ImsManager;
Brad Ebinger34bef922017-11-09 10:27:08 -0800147import com.android.ims.internal.IImsServiceFeatureCallback;
James.cf Linbcdf8b32021-01-14 16:44:13 +0800148import com.android.ims.rcs.uce.eab.EabUtil;
SongFerngWangfd89b102021-05-27 22:44:54 +0800149import com.android.internal.annotations.VisibleForTesting;
Shuo Qian4a594052020-01-23 11:59:30 -0800150import com.android.internal.telephony.CallForwardInfo;
Shishir Agrawal566b7612013-10-28 14:41:00 -0700151import com.android.internal.telephony.CallManager;
Tyler Gunn52dcf772017-04-26 11:30:31 -0700152import com.android.internal.telephony.CallStateException;
Tyler Gunnd4339262021-05-03 14:46:49 -0700153import com.android.internal.telephony.CallTracker;
Rambo Wang9c9ffdd2022-01-13 21:51:44 -0800154import com.android.internal.telephony.CarrierPrivilegesTracker;
chen xu651eec72018-11-11 19:03:44 -0800155import com.android.internal.telephony.CarrierResolver;
Shishir Agrawal302c8692015-06-19 13:49:39 -0700156import com.android.internal.telephony.CellNetworkScanResult;
Shishir Agrawal566b7612013-10-28 14:41:00 -0700157import com.android.internal.telephony.CommandException;
Shuo Qian4a594052020-01-23 11:59:30 -0800158import com.android.internal.telephony.CommandsInterface;
Hui Wang641e81c2020-10-12 12:14:23 -0700159import com.android.internal.telephony.GbaManager;
Shuo Qianccbaf742021-02-22 18:32:21 -0800160import com.android.internal.telephony.GsmCdmaPhone;
Nathan Harold48d6fd52019-02-06 19:01:40 -0800161import com.android.internal.telephony.HalVersion;
Hall Liu73f5d362020-01-20 13:42:00 -0800162import com.android.internal.telephony.IBooleanConsumer;
Hall Liu27d24262020-09-18 19:04:59 -0700163import com.android.internal.telephony.ICallForwardingInfoCallback;
Hunsuk Choi3b742d62021-10-25 19:48:34 +0000164import com.android.internal.telephony.IImsStateCallback;
Brad Ebingera63db5f2019-04-23 16:31:13 -0700165import com.android.internal.telephony.IIntegerConsumer;
Hall Liud892bec2018-11-30 14:51:45 -0800166import com.android.internal.telephony.INumberVerificationCallback;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700167import com.android.internal.telephony.ITelephony;
Jake Hambye994d462014-02-03 13:10:13 -0800168import com.android.internal.telephony.IccCard;
Ryan Savitskicab25c52023-03-14 14:23:49 +0000169import com.android.internal.telephony.IccCardConstants;
Rambo Wanga1782702021-11-10 20:15:19 -0800170import com.android.internal.telephony.IccLogicalChannelRequest;
Jack Yu5f7092c2018-04-13 14:05:37 -0700171import com.android.internal.telephony.LocaleTracker;
yinxub1bed742017-04-17 11:45:04 -0700172import com.android.internal.telephony.NetworkScanRequestTracker;
Shishir Agrawal302c8692015-06-19 13:49:39 -0700173import com.android.internal.telephony.OperatorInfo;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700174import com.android.internal.telephony.Phone;
Malcolm Chen2c63d402018-08-14 16:00:53 -0700175import com.android.internal.telephony.PhoneConfigurationManager;
Nathan Harolda667c152016-12-14 11:27:20 -0800176import com.android.internal.telephony.PhoneConstantConversions;
Ta-wei Yen87c49842016-05-13 21:19:52 -0700177import com.android.internal.telephony.PhoneConstants;
Wink Saville36469e72014-06-11 15:17:00 -0700178import com.android.internal.telephony.PhoneFactory;
Wink Saville5d475dd2014-10-17 15:00:58 -0700179import com.android.internal.telephony.ProxyController;
Sanket Padawe99ef1e32016-05-18 16:12:33 -0700180import com.android.internal.telephony.RIL;
SongFerngWang8c6e82e2021-03-02 22:09:29 +0800181import com.android.internal.telephony.RILConstants;
Daniel Bright94f43662021-03-01 14:43:40 -0800182import com.android.internal.telephony.RadioInterfaceCapabilityController;
Jack Yu5f7092c2018-04-13 14:05:37 -0700183import com.android.internal.telephony.ServiceStateTracker;
Amit Mahajandccb3f12019-05-13 13:48:32 -0700184import com.android.internal.telephony.SmsController;
Brad Ebingera63db5f2019-04-23 16:31:13 -0700185import com.android.internal.telephony.SmsPermissions;
Wink Savilleac1bdfd2014-11-20 23:04:44 -0800186import com.android.internal.telephony.SubscriptionController;
Peter Wang59571be2020-01-27 12:35:15 +0800187import com.android.internal.telephony.TelephonyIntents;
Jeff Davidson7e17e312018-02-13 18:17:36 -0800188import com.android.internal.telephony.TelephonyPermissions;
Jack Yu27422a52022-03-21 10:38:05 -0700189import com.android.internal.telephony.data.DataUtils;
sqianf4ca7ed2019-01-15 18:32:07 -0800190import com.android.internal.telephony.emergency.EmergencyNumberTracker;
Derek Tan740e1672017-06-27 14:56:27 -0700191import com.android.internal.telephony.euicc.EuiccConnector;
Brad Ebinger9c0eb502019-01-23 15:06:19 -0800192import com.android.internal.telephony.ims.ImsResolver;
Tyler Gunn7bcdc742019-10-04 15:56:59 -0700193import com.android.internal.telephony.imsphone.ImsPhone;
194import com.android.internal.telephony.imsphone.ImsPhoneCallTracker;
joonhunshin3e154242021-09-17 06:33:39 +0000195import com.android.internal.telephony.metrics.RcsStats;
Pengquan Meng6c2dc9f2019-02-06 11:12:53 -0800196import com.android.internal.telephony.metrics.TelephonyMetrics;
Jack Yudf049d72022-12-02 22:48:35 -0800197import com.android.internal.telephony.subscription.SubscriptionInfoInternal;
198import com.android.internal.telephony.subscription.SubscriptionManagerService;
Meng Wangafbc5852019-09-19 17:37:13 -0700199import com.android.internal.telephony.uicc.IccCardApplicationStatus.AppType;
Shishir Agrawal566b7612013-10-28 14:41:00 -0700200import com.android.internal.telephony.uicc.IccIoResult;
201import com.android.internal.telephony.uicc.IccUtils;
Nathan Haroldb3014052017-01-25 15:57:32 -0800202import com.android.internal.telephony.uicc.SIMRecords;
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700203import com.android.internal.telephony.uicc.UiccCard;
Nathan Haroldb3014052017-01-25 15:57:32 -0800204import com.android.internal.telephony.uicc.UiccCardApplication;
Shishir Agrawal566b7612013-10-28 14:41:00 -0700205import com.android.internal.telephony.uicc.UiccController;
Muralidhar Reddy472c2ae2021-09-29 19:38:40 +0000206import com.android.internal.telephony.uicc.UiccPort;
Jeff Davidson7e17e312018-02-13 18:17:36 -0800207import com.android.internal.telephony.uicc.UiccProfile;
Holly Jiuyu Sun01c47ad2018-01-24 17:56:33 +0000208import com.android.internal.telephony.uicc.UiccSlot;
zoey chenc730df82019-12-18 17:07:20 +0800209import com.android.internal.telephony.util.LocaleUtils;
fionaxu7ed723d2017-05-30 18:58:54 -0700210import com.android.internal.telephony.util.VoicemailNotificationSettingsUtil;
Hall Liu27d24262020-09-18 19:04:59 -0700211import com.android.internal.util.FunctionalUtils;
Jake Hambye994d462014-02-03 13:10:13 -0800212import com.android.internal.util.HexDump;
Hall Liuaa4211e2021-01-20 15:43:39 -0800213import com.android.phone.callcomposer.CallComposerPictureManager;
214import com.android.phone.callcomposer.CallComposerPictureTransfer;
215import com.android.phone.callcomposer.ImageData;
Brad Ebingera63db5f2019-04-23 16:31:13 -0700216import com.android.phone.settings.PickSmsSubscriptionActivity;
Ta-wei Yenc9df0432017-04-17 17:09:07 -0700217import com.android.phone.vvm.PhoneAccountHandleConverter;
Ta-wei Yen527a9c02017-01-06 15:29:25 -0800218import com.android.phone.vvm.RemoteVvmTaskManager;
Ta-wei Yenc9df0432017-04-17 17:09:07 -0700219import com.android.phone.vvm.VisualVoicemailSettingsUtil;
Ta-wei Yenc8905312017-03-28 11:14:45 -0700220import com.android.phone.vvm.VisualVoicemailSmsFilterConfig;
Jack Nudelmanb0b87642020-11-12 15:04:39 -0800221import com.android.services.telephony.TelecomAccountRegistry;
222import com.android.services.telephony.TelephonyConnectionService;
Peter Wang44b186e2020-01-13 23:33:09 -0800223import com.android.telephony.Rlog;
Ta-wei Yen30a69c82016-12-27 14:52:32 -0800224
Hall Liu82694d52020-12-11 18:22:04 -0800225import java.io.ByteArrayOutputStream;
Ta-wei Yenc236d6b2016-06-21 13:33:12 -0700226import java.io.FileDescriptor;
Hall Liu82694d52020-12-11 18:22:04 -0800227import java.io.IOException;
228import java.io.InputStream;
Ta-wei Yenc236d6b2016-06-21 13:33:12 -0700229import java.io.PrintWriter;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700230import java.util.ArrayList;
Jeff Sharkey85190e62014-12-05 09:40:12 -0800231import java.util.Arrays;
Muralidhar Reddyeb809e32021-11-19 03:07:54 +0000232import java.util.Collection;
Hunter Knepshieldcad7f0b2021-12-13 15:08:35 -0800233import java.util.Collections;
sqian11b7a0e2018-12-05 18:48:28 -0800234import java.util.HashMap;
sqianf4ca7ed2019-01-15 18:32:07 -0800235import java.util.HashSet;
Jake Hambye994d462014-02-03 13:10:13 -0800236import java.util.List;
Narayan Kamath1c496c22015-04-16 14:40:19 +0100237import java.util.Locale;
Jeff Sharkey85190e62014-12-05 09:40:12 -0800238import java.util.Map;
Nazanin Bakhshif71371d2019-04-29 17:29:44 -0700239import java.util.NoSuchElementException;
Hall Liu82694d52020-12-11 18:22:04 -0800240import java.util.Objects;
sqianf4ca7ed2019-01-15 18:32:07 -0800241import java.util.Set;
Hall Liu82694d52020-12-11 18:22:04 -0800242import java.util.concurrent.Executors;
Peter Wangdafb9ac2020-01-15 14:13:38 -0800243import java.util.concurrent.atomic.AtomicBoolean;
Hall Liu73f5d362020-01-20 13:42:00 -0800244import java.util.function.Consumer;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700245
246/**
247 * Implementation of the ITelephony interface.
248 */
Santos Cordon117fee72014-05-16 17:56:12 -0700249public class PhoneInterfaceManager extends ITelephony.Stub {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700250 private static final String LOG_TAG = "PhoneInterfaceManager";
251 private static final boolean DBG = (PhoneGlobals.DBG_LEVEL >= 2);
252 private static final boolean DBG_LOC = false;
Jeff Sharkey85190e62014-12-05 09:40:12 -0800253 private static final boolean DBG_MERGE = false;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700254
255 // Message codes used with mMainThreadHandler
256 private static final int CMD_HANDLE_PIN_MMI = 1;
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700257 private static final int CMD_TRANSMIT_APDU_LOGICAL_CHANNEL = 7;
258 private static final int EVENT_TRANSMIT_APDU_LOGICAL_CHANNEL_DONE = 8;
Shishir Agrawal566b7612013-10-28 14:41:00 -0700259 private static final int CMD_OPEN_CHANNEL = 9;
260 private static final int EVENT_OPEN_CHANNEL_DONE = 10;
261 private static final int CMD_CLOSE_CHANNEL = 11;
262 private static final int EVENT_CLOSE_CHANNEL_DONE = 12;
Jake Hambye994d462014-02-03 13:10:13 -0800263 private static final int CMD_NV_READ_ITEM = 13;
264 private static final int EVENT_NV_READ_ITEM_DONE = 14;
265 private static final int CMD_NV_WRITE_ITEM = 15;
266 private static final int EVENT_NV_WRITE_ITEM_DONE = 16;
267 private static final int CMD_NV_WRITE_CDMA_PRL = 17;
268 private static final int EVENT_NV_WRITE_CDMA_PRL_DONE = 18;
chen xu6dac5ab2018-10-26 17:39:23 -0700269 private static final int CMD_RESET_MODEM_CONFIG = 19;
270 private static final int EVENT_RESET_MODEM_CONFIG_DONE = 20;
SongFerngWang3ef3e072020-12-21 16:41:52 +0800271 private static final int CMD_GET_ALLOWED_NETWORK_TYPES_BITMASK = 21;
272 private static final int EVENT_GET_ALLOWED_NETWORK_TYPES_BITMASK_DONE = 22;
Sailesh Nepal35b59452014-03-06 09:26:56 -0800273 private static final int CMD_SEND_ENVELOPE = 25;
274 private static final int EVENT_SEND_ENVELOPE_DONE = 26;
Shuo Qian850e4d6a2018-04-25 21:02:08 +0000275 private static final int CMD_INVOKE_OEM_RIL_REQUEST_RAW = 27;
276 private static final int EVENT_INVOKE_OEM_RIL_REQUEST_RAW_DONE = 28;
Derek Tan6b088ee2014-09-05 14:15:18 -0700277 private static final int CMD_TRANSMIT_APDU_BASIC_CHANNEL = 29;
278 private static final int EVENT_TRANSMIT_APDU_BASIC_CHANNEL_DONE = 30;
279 private static final int CMD_EXCHANGE_SIM_IO = 31;
280 private static final int EVENT_EXCHANGE_SIM_IO_DONE = 32;
Shishir Agrawal76d5da92014-11-09 16:17:25 -0800281 private static final int CMD_SET_VOICEMAIL_NUMBER = 33;
282 private static final int EVENT_SET_VOICEMAIL_NUMBER_DONE = 34;
Stuart Scott54788802015-03-30 13:18:01 -0700283 private static final int CMD_SET_NETWORK_SELECTION_MODE_AUTOMATIC = 35;
284 private static final int EVENT_SET_NETWORK_SELECTION_MODE_AUTOMATIC_DONE = 36;
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -0700285 private static final int CMD_GET_MODEM_ACTIVITY_INFO = 37;
286 private static final int EVENT_GET_MODEM_ACTIVITY_INFO_DONE = 38;
Shishir Agrawal302c8692015-06-19 13:49:39 -0700287 private static final int CMD_PERFORM_NETWORK_SCAN = 39;
288 private static final int EVENT_PERFORM_NETWORK_SCAN_DONE = 40;
289 private static final int CMD_SET_NETWORK_SELECTION_MODE_MANUAL = 41;
290 private static final int EVENT_SET_NETWORK_SELECTION_MODE_MANUAL_DONE = 42;
Meng Wang1a7c35a2016-05-05 20:56:15 -0700291 private static final int CMD_SET_ALLOWED_CARRIERS = 43;
292 private static final int EVENT_SET_ALLOWED_CARRIERS_DONE = 44;
293 private static final int CMD_GET_ALLOWED_CARRIERS = 45;
294 private static final int EVENT_GET_ALLOWED_CARRIERS_DONE = 46;
pkanwar32d516d2016-10-14 19:37:38 -0700295 private static final int CMD_HANDLE_USSD_REQUEST = 47;
Nathan Haroldb3014052017-01-25 15:57:32 -0800296 private static final int CMD_GET_FORBIDDEN_PLMNS = 48;
297 private static final int EVENT_GET_FORBIDDEN_PLMNS_DONE = 49;
Holly Jiuyu Sun01c47ad2018-01-24 17:56:33 +0000298 private static final int CMD_SWITCH_SLOTS = 50;
299 private static final int EVENT_SWITCH_SLOTS_DONE = 51;
Pengquan Menga1bb6272018-09-06 09:59:22 -0700300 private static final int CMD_GET_NETWORK_SELECTION_MODE = 52;
301 private static final int EVENT_GET_NETWORK_SELECTION_MODE_DONE = 53;
302 private static final int CMD_GET_CDMA_ROAMING_MODE = 54;
303 private static final int EVENT_GET_CDMA_ROAMING_MODE_DONE = 55;
304 private static final int CMD_SET_CDMA_ROAMING_MODE = 56;
305 private static final int EVENT_SET_CDMA_ROAMING_MODE_DONE = 57;
306 private static final int CMD_SET_CDMA_SUBSCRIPTION_MODE = 58;
307 private static final int EVENT_SET_CDMA_SUBSCRIPTION_MODE_DONE = 59;
Nathan Harold3ff88932018-08-14 10:19:49 -0700308 private static final int CMD_GET_ALL_CELL_INFO = 60;
309 private static final int EVENT_GET_ALL_CELL_INFO_DONE = 61;
310 private static final int CMD_GET_CELL_LOCATION = 62;
311 private static final int EVENT_GET_CELL_LOCATION_DONE = 63;
chen xu6dac5ab2018-10-26 17:39:23 -0700312 private static final int CMD_MODEM_REBOOT = 64;
313 private static final int EVENT_CMD_MODEM_REBOOT_DONE = 65;
Nathan Haroldfa8da0f2018-09-27 18:51:29 -0700314 private static final int CMD_REQUEST_CELL_INFO_UPDATE = 66;
315 private static final int EVENT_REQUEST_CELL_INFO_UPDATE_DONE = 67;
Malcolm Chen8e4ed912019-01-15 20:22:16 -0800316 private static final int CMD_REQUEST_ENABLE_MODEM = 68;
317 private static final int EVENT_ENABLE_MODEM_DONE = 69;
Nazanin Bakhshif71371d2019-04-29 17:29:44 -0700318 private static final int CMD_GET_MODEM_STATUS = 70;
319 private static final int EVENT_GET_MODEM_STATUS_DONE = 71;
yincheng zhao2737e882019-09-06 17:06:54 -0700320 private static final int CMD_SET_FORBIDDEN_PLMNS = 72;
321 private static final int EVENT_SET_FORBIDDEN_PLMNS_DONE = 73;
Naina Nallurid63128d2019-09-17 14:10:30 -0700322 private static final int CMD_ERASE_MODEM_CONFIG = 74;
323 private static final int EVENT_ERASE_MODEM_CONFIG_DONE = 75;
zoey chene02881a2019-12-30 16:11:23 +0800324 private static final int CMD_CHANGE_ICC_LOCK_PASSWORD = 76;
325 private static final int EVENT_CHANGE_ICC_LOCK_PASSWORD_DONE = 77;
326 private static final int CMD_SET_ICC_LOCK_ENABLED = 78;
327 private static final int EVENT_SET_ICC_LOCK_ENABLED_DONE = 79;
Hall Liu73f5d362020-01-20 13:42:00 -0800328 private static final int CMD_SET_SYSTEM_SELECTION_CHANNELS = 80;
329 private static final int EVENT_SET_SYSTEM_SELECTION_CHANNELS_DONE = 81;
Peter Wangdafb9ac2020-01-15 14:13:38 -0800330 private static final int MSG_NOTIFY_USER_ACTIVITY = 82;
Shuo Qian4a594052020-01-23 11:59:30 -0800331 private static final int CMD_GET_CALL_FORWARDING = 83;
332 private static final int EVENT_GET_CALL_FORWARDING_DONE = 84;
333 private static final int CMD_SET_CALL_FORWARDING = 85;
334 private static final int EVENT_SET_CALL_FORWARDING_DONE = 86;
335 private static final int CMD_GET_CALL_WAITING = 87;
336 private static final int EVENT_GET_CALL_WAITING_DONE = 88;
337 private static final int CMD_SET_CALL_WAITING = 89;
338 private static final int EVENT_SET_CALL_WAITING_DONE = 90;
Sooraj Sasindran37444802020-08-11 10:40:43 -0700339 private static final int CMD_ENABLE_NR_DUAL_CONNECTIVITY = 91;
340 private static final int EVENT_ENABLE_NR_DUAL_CONNECTIVITY_DONE = 92;
341 private static final int CMD_IS_NR_DUAL_CONNECTIVITY_ENABLED = 93;
342 private static final int EVENT_IS_NR_DUAL_CONNECTIVITY_ENABLED_DONE = 94;
Sarah Chinbaab1432020-10-28 13:46:24 -0700343 private static final int CMD_GET_CDMA_SUBSCRIPTION_MODE = 95;
344 private static final int EVENT_GET_CDMA_SUBSCRIPTION_MODE_DONE = 96;
Sarah Chin679c08a2020-11-18 13:39:35 -0800345 private static final int CMD_GET_SYSTEM_SELECTION_CHANNELS = 97;
346 private static final int EVENT_GET_SYSTEM_SELECTION_CHANNELS_DONE = 98;
Jack Nudelmanb0b87642020-11-12 15:04:39 -0800347 private static final int CMD_SET_DATA_THROTTLING = 99;
348 private static final int EVENT_SET_DATA_THROTTLING_DONE = 100;
Jordan Liu109698e2020-11-24 14:50:34 -0800349 private static final int CMD_SET_SIM_POWER = 101;
350 private static final int EVENT_SET_SIM_POWER_DONE = 102;
Rambo Wanga5cc9b72021-01-07 10:51:54 -0800351 private static final int CMD_SET_SIGNAL_STRENGTH_UPDATE_REQUEST = 103;
352 private static final int EVENT_SET_SIGNAL_STRENGTH_UPDATE_REQUEST_DONE = 104;
353 private static final int CMD_CLEAR_SIGNAL_STRENGTH_UPDATE_REQUEST = 105;
354 private static final int EVENT_CLEAR_SIGNAL_STRENGTH_UPDATE_REQUEST_DONE = 106;
SongFerngWang3ef3e072020-12-21 16:41:52 +0800355 private static final int CMD_SET_ALLOWED_NETWORK_TYPES_FOR_REASON = 107;
356 private static final int EVENT_SET_ALLOWED_NETWORK_TYPES_FOR_REASON_DONE = 108;
Michele Berionne5e411512020-11-13 02:36:59 +0000357 private static final int CMD_PREPARE_UNATTENDED_REBOOT = 109;
Hongbo Zeng156aa4a2021-02-08 21:50:28 +0800358 private static final int CMD_GET_SLICING_CONFIG = 110;
359 private static final int EVENT_GET_SLICING_CONFIG_DONE = 111;
Kai Shif70f46f2021-03-03 13:59:46 -0800360 private static final int CMD_ERASE_DATA_SHARED_PREFERENCES = 112;
Sooraj Sasindrandaf060f2021-06-15 14:52:55 -0700361 private static final int CMD_ENABLE_VONR = 113;
362 private static final int EVENT_ENABLE_VONR_DONE = 114;
363 private static final int CMD_IS_VONR_ENABLED = 115;
364 private static final int EVENT_IS_VONR_ENABLED_DONE = 116;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700365
Holly Jiuyu Sun1cc2d552018-01-26 15:51:16 -0800366 // Parameters of select command.
367 private static final int SELECT_COMMAND = 0xA4;
368 private static final int SELECT_P1 = 0x04;
369 private static final int SELECT_P2 = 0;
370 private static final int SELECT_P3 = 0x10;
371
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700372 /** The singleton instance. */
373 private static PhoneInterfaceManager sInstance;
Jack Nudelman644b91a2021-03-12 14:09:48 -0800374 private static List<String> sThermalMitigationAllowlistedPackages = new ArrayList<>();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700375
Wink Saville3ab207e2014-11-20 13:07:20 -0800376 private PhoneGlobals mApp;
Wink Saville3ab207e2014-11-20 13:07:20 -0800377 private CallManager mCM;
Brad Ebinger24c29992019-12-05 13:03:21 -0800378 private ImsResolver mImsResolver;
Stuart Scott981d8582015-04-21 14:09:50 -0700379 private UserManager mUserManager;
Wink Saville3ab207e2014-11-20 13:07:20 -0800380 private AppOpsManager mAppOps;
Grace Jia0ddb3612021-04-22 13:35:26 -0700381 private PackageManager mPm;
Wink Saville3ab207e2014-11-20 13:07:20 -0800382 private MainThreadHandler mMainThreadHandler;
Wink Savilleac1bdfd2014-11-20 23:04:44 -0800383 private SubscriptionController mSubscriptionController;
Wink Saville3ab207e2014-11-20 13:07:20 -0800384 private SharedPreferences mTelephonySharedPreferences;
Malcolm Chen2c63d402018-08-14 16:00:53 -0700385 private PhoneConfigurationManager mPhoneConfigurationManager;
Daniel Bright94f43662021-03-01 14:43:40 -0800386 private final RadioInterfaceCapabilityController mRadioInterfaceCapabilities;
Gil Cukierman6dac5eb2022-09-19 16:09:04 +0000387 private final Telephony2gUpdater mTelephony2gUpdater;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700388
Peter Wangdafb9ac2020-01-15 14:13:38 -0800389 /** User Activity */
390 private AtomicBoolean mNotifyUserActivity;
Peter Wangdafb9ac2020-01-15 14:13:38 -0800391 private static final int USER_ACTIVITY_NOTIFICATION_DELAY = 200;
392
Jeff Davidson8ab02b22020-03-28 12:24:40 -0700393 private Set<Integer> mCarrierPrivilegeTestOverrideSubIds = new ArraySet<>();
394
Derek Tan97ebb422014-09-05 16:55:38 -0700395 private static final String PREF_CARRIERS_ALPHATAG_PREFIX = "carrier_alphtag_";
396 private static final String PREF_CARRIERS_NUMBER_PREFIX = "carrier_number_";
Jeff Sharkey85190e62014-12-05 09:40:12 -0800397 private static final String PREF_CARRIERS_SUBSCRIBER_PREFIX = "carrier_subscriber_";
Brad Ebinger1c8542e2019-01-14 13:43:14 -0800398 private static final String PREF_PROVISION_IMS_MMTEL_PREFIX = "provision_ims_mmtel_";
Derek Tan89e89d42014-07-08 17:00:10 -0700399
Michelecea4cf22018-12-21 15:00:11 -0800400 // String to store multi SIM allowed
401 private static final String PREF_MULTI_SIM_RESTRICTED = "multisim_restricted";
402
Derek Tan740e1672017-06-27 14:56:27 -0700403 // The AID of ISD-R.
404 private static final String ISDR_AID = "A0000005591010FFFFFFFF8900000100";
405
yinxub1bed742017-04-17 11:45:04 -0700406 private NetworkScanRequestTracker mNetworkScanRequestTracker;
407
David Kelly5e06a7f2018-03-12 14:10:59 +0000408 private static final int TYPE_ALLOCATION_CODE_LENGTH = 8;
409 private static final int MANUFACTURER_CODE_LENGTH = 8;
410
Jack Nudelmanb0b87642020-11-12 15:04:39 -0800411 private static final int SET_DATA_THROTTLING_MODEM_THREW_INVALID_PARAMS = -1;
Jack Nudelman5d6a98b2021-03-04 14:26:25 -0800412 private static final int MODEM_DOES_NOT_SUPPORT_DATA_THROTTLING_ERROR_CODE = -2;
Jack Nudelmanb0b87642020-11-12 15:04:39 -0800413
Derek Tan89e89d42014-07-08 17:00:10 -0700414 /**
Naina Nallurid63128d2019-09-17 14:10:30 -0700415 * Experiment flag to enable erase modem config on reset network, default value is false
416 */
417 public static final String RESET_NETWORK_ERASE_MODEM_CONFIG_ENABLED =
418 "reset_network_erase_modem_config_enabled";
419
Rambo Wang0f050d82021-02-12 11:43:36 -0800420 private static final int SET_NETWORK_SELECTION_MODE_AUTOMATIC_TIMEOUT_MS = 2000; // 2 seconds
Chen Xu540470b2021-12-14 17:15:47 -0800421
sandeepjsb6c87872021-09-27 15:34:44 +0000422 /**
423 * With support for MEP(multiple enabled profile) in Android T, a SIM card can have more than
424 * one ICCID active at the same time.
425 * Apps should use below API signatures if targeting SDK is T and beyond.
426 *
427 * @hide
428 */
429 @ChangeId
430 @EnabledSince(targetSdkVersion = Build.VERSION_CODES.TIRAMISU)
431 public static final long GET_API_SIGNATURES_FROM_UICC_PORT_INFO = 202110963L;
Rambo Wang0f050d82021-02-12 11:43:36 -0800432
Naina Nallurid63128d2019-09-17 14:10:30 -0700433 /**
Chen Xu540470b2021-12-14 17:15:47 -0800434 * Apps targeting on Android T and beyond will get exception whenever icc close channel
435 * operation fails.
436 */
437 @ChangeId
438 @EnabledSince(targetSdkVersion = Build.VERSION_CODES.TIRAMISU)
439 public static final long ICC_CLOSE_CHANNEL_EXCEPTION_ON_FAILURE = 208739934L;
440
441 /**
Shishir Agrawal566b7612013-10-28 14:41:00 -0700442 * A request object to use for transmitting data to an ICC.
443 */
444 private static final class IccAPDUArgument {
445 public int channel, cla, command, p1, p2, p3;
446 public String data;
447
448 public IccAPDUArgument(int channel, int cla, int command,
449 int p1, int p2, int p3, String data) {
450 this.channel = channel;
451 this.cla = cla;
452 this.command = command;
453 this.p1 = p1;
454 this.p2 = p2;
455 this.p3 = p3;
456 this.data = data;
457 }
458 }
459
460 /**
Shishir Agrawal77ba3172015-09-10 14:50:19 -0700461 * A request object to use for transmitting data to an ICC.
462 */
463 private static final class ManualNetworkSelectionArgument {
464 public OperatorInfo operatorInfo;
465 public boolean persistSelection;
466
467 public ManualNetworkSelectionArgument(OperatorInfo operatorInfo, boolean persistSelection) {
468 this.operatorInfo = operatorInfo;
469 this.persistSelection = persistSelection;
470 }
471 }
472
473 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700474 * A request object for use with {@link MainThreadHandler}. Requesters should wait() on the
475 * request after sending. The main thread will notify the request when it is complete.
476 */
477 private static final class MainThreadRequest {
478 /** The argument to use for the request */
479 public Object argument;
480 /** The result of the request that is run on the main thread */
481 public Object result;
Sanket Padawe56e75a32016-02-08 12:18:19 -0800482 // The subscriber id that this request applies to. Defaults to
483 // SubscriptionManager.INVALID_SUBSCRIPTION_ID
484 public Integer subId = SubscriptionManager.INVALID_SUBSCRIPTION_ID;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700485
Nathan Harold92bed182018-10-12 18:16:49 -0700486 // In cases where subId is unavailable, the caller needs to specify the phone.
487 public Phone phone;
488
vagdeviaf9a5b92018-08-15 16:01:53 -0700489 public WorkSource workSource;
490
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700491 public MainThreadRequest(Object argument) {
492 this.argument = argument;
493 }
Shishir Agrawal76d5da92014-11-09 16:17:25 -0800494
Nathan Harold92bed182018-10-12 18:16:49 -0700495 MainThreadRequest(Object argument, Phone phone, WorkSource workSource) {
496 this.argument = argument;
497 if (phone != null) {
498 this.phone = phone;
499 }
500 this.workSource = workSource;
501 }
502
vagdeviaf9a5b92018-08-15 16:01:53 -0700503 MainThreadRequest(Object argument, Integer subId, WorkSource workSource) {
Shishir Agrawal76d5da92014-11-09 16:17:25 -0800504 this.argument = argument;
Sanket Padawe56e75a32016-02-08 12:18:19 -0800505 if (subId != null) {
506 this.subId = subId;
507 }
vagdeviaf9a5b92018-08-15 16:01:53 -0700508 this.workSource = workSource;
Shishir Agrawal76d5da92014-11-09 16:17:25 -0800509 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700510 }
511
Sailesh Nepalcc0375f2013-11-13 09:15:18 -0800512 private static final class IncomingThirdPartyCallArgs {
513 public final ComponentName component;
514 public final String callId;
515 public final String callerDisplayName;
516
517 public IncomingThirdPartyCallArgs(ComponentName component, String callId,
518 String callerDisplayName) {
519 this.component = component;
520 this.callId = callId;
521 this.callerDisplayName = callerDisplayName;
522 }
523 }
524
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700525 /**
526 * A handler that processes messages on the main thread in the phone process. Since many
527 * of the Phone calls are not thread safe this is needed to shuttle the requests from the
528 * inbound binder threads to the main thread in the phone process. The Binder thread
529 * may provide a {@link MainThreadRequest} object in the msg.obj field that they are waiting
530 * on, which will be notified when the operation completes and will contain the result of the
531 * request.
532 *
533 * <p>If a MainThreadRequest object is provided in the msg.obj field,
534 * note that request.result must be set to something non-null for the calling thread to
535 * unblock.
536 */
537 private final class MainThreadHandler extends Handler {
538 @Override
539 public void handleMessage(Message msg) {
540 MainThreadRequest request;
541 Message onCompleted;
542 AsyncResult ar;
Muralidhar Reddy472c2ae2021-09-29 19:38:40 +0000543 UiccPort uiccPort;
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700544 IccAPDUArgument iccArgument;
Nazanin Bakhshif782e562018-12-11 15:15:39 -0800545 final Phone defaultPhone = getDefaultPhone();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700546
547 switch (msg.what) {
Pengquan Menga1bb6272018-09-06 09:59:22 -0700548 case CMD_HANDLE_USSD_REQUEST: {
549 request = (MainThreadRequest) msg.obj;
550 final Phone phone = getPhoneFromRequest(request);
551 Pair<String, ResultReceiver> ussdObject = (Pair) request.argument;
Chen Xue9d737e2022-01-01 23:41:31 -0800552 String ussdRequest = ussdObject.first;
Pengquan Menga1bb6272018-09-06 09:59:22 -0700553 ResultReceiver wrappedCallback = ussdObject.second;
Tyler Gunn65d45c22017-06-05 11:22:26 -0700554
Pengquan Menga1bb6272018-09-06 09:59:22 -0700555 if (!isUssdApiAllowed(request.subId)) {
556 // Carrier does not support use of this API, return failure.
557 Rlog.w(LOG_TAG, "handleUssdRequest: carrier does not support USSD apis.");
558 UssdResponse response = new UssdResponse(ussdRequest, null);
559 Bundle returnData = new Bundle();
560 returnData.putParcelable(TelephonyManager.USSD_RESPONSE, response);
561 wrappedCallback.send(TelephonyManager.USSD_RETURN_FAILURE, returnData);
Tyler Gunn65d45c22017-06-05 11:22:26 -0700562
Pengquan Menga1bb6272018-09-06 09:59:22 -0700563 request.result = true;
564 notifyRequester(request);
565 return;
566 }
Tyler Gunn65d45c22017-06-05 11:22:26 -0700567
Pengquan Menga1bb6272018-09-06 09:59:22 -0700568 try {
569 request.result = phone != null
570 ? phone.handleUssdRequest(ussdRequest, wrappedCallback) : false;
571 } catch (CallStateException cse) {
572 request.result = false;
573 }
574 // Wake up the requesting thread
575 notifyRequester(request);
576 break;
pkanwar32d516d2016-10-14 19:37:38 -0700577 }
578
Yorke Lee716f67e2015-06-17 15:39:16 -0700579 case CMD_HANDLE_PIN_MMI: {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700580 request = (MainThreadRequest) msg.obj;
Yorke Lee716f67e2015-06-17 15:39:16 -0700581 final Phone phone = getPhoneFromRequest(request);
582 request.result = phone != null ?
583 getPhoneFromRequest(request).handlePinMmi((String) request.argument)
584 : false;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700585 // Wake up the requesting thread
Pengquan Menga1bb6272018-09-06 09:59:22 -0700586 notifyRequester(request);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700587 break;
Yorke Lee716f67e2015-06-17 15:39:16 -0700588 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700589
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700590 case CMD_TRANSMIT_APDU_LOGICAL_CHANNEL:
Shishir Agrawal566b7612013-10-28 14:41:00 -0700591 request = (MainThreadRequest) msg.obj;
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700592 iccArgument = (IccAPDUArgument) request.argument;
Muralidhar Reddy472c2ae2021-09-29 19:38:40 +0000593 uiccPort = getUiccPortFromRequest(request);
594 if (uiccPort == null) {
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700595 loge("iccTransmitApduLogicalChannel: No UICC");
Chen Xue9d737e2022-01-01 23:41:31 -0800596 request.result = new IccIoResult(0x6F, 0, (byte[]) null);
Pengquan Menga1bb6272018-09-06 09:59:22 -0700597 notifyRequester(request);
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700598 } else {
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700599 onCompleted = obtainMessage(EVENT_TRANSMIT_APDU_LOGICAL_CHANNEL_DONE,
Chen Xue9d737e2022-01-01 23:41:31 -0800600 request);
Muralidhar Reddy472c2ae2021-09-29 19:38:40 +0000601 uiccPort.iccTransmitApduLogicalChannel(
Chen Xue9d737e2022-01-01 23:41:31 -0800602 iccArgument.channel, iccArgument.cla, iccArgument.command,
603 iccArgument.p1, iccArgument.p2, iccArgument.p3, iccArgument.data,
604 onCompleted);
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700605 }
Shishir Agrawal566b7612013-10-28 14:41:00 -0700606 break;
607
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700608 case EVENT_TRANSMIT_APDU_LOGICAL_CHANNEL_DONE:
Shishir Agrawal566b7612013-10-28 14:41:00 -0700609 ar = (AsyncResult) msg.obj;
610 request = (MainThreadRequest) ar.userObj;
611 if (ar.exception == null && ar.result != null) {
612 request.result = ar.result;
613 } else {
Chen Xue9d737e2022-01-01 23:41:31 -0800614 request.result = new IccIoResult(0x6F, 0, (byte[]) null);
Shishir Agrawal566b7612013-10-28 14:41:00 -0700615 if (ar.result == null) {
616 loge("iccTransmitApduLogicalChannel: Empty response");
Jake Hambye994d462014-02-03 13:10:13 -0800617 } else if (ar.exception instanceof CommandException) {
Shishir Agrawal566b7612013-10-28 14:41:00 -0700618 loge("iccTransmitApduLogicalChannel: CommandException: " +
Jake Hambye994d462014-02-03 13:10:13 -0800619 ar.exception);
Shishir Agrawal566b7612013-10-28 14:41:00 -0700620 } else {
621 loge("iccTransmitApduLogicalChannel: Unknown exception");
622 }
623 }
Pengquan Menga1bb6272018-09-06 09:59:22 -0700624 notifyRequester(request);
Shishir Agrawal566b7612013-10-28 14:41:00 -0700625 break;
626
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700627 case CMD_TRANSMIT_APDU_BASIC_CHANNEL:
628 request = (MainThreadRequest) msg.obj;
629 iccArgument = (IccAPDUArgument) request.argument;
Muralidhar Reddy472c2ae2021-09-29 19:38:40 +0000630 uiccPort = getUiccPortFromRequest(request);
631 if (uiccPort == null) {
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700632 loge("iccTransmitApduBasicChannel: No UICC");
Chen Xue9d737e2022-01-01 23:41:31 -0800633 request.result = new IccIoResult(0x6F, 0, (byte[]) null);
Pengquan Menga1bb6272018-09-06 09:59:22 -0700634 notifyRequester(request);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700635 } else {
636 onCompleted = obtainMessage(EVENT_TRANSMIT_APDU_BASIC_CHANNEL_DONE,
Chen Xue9d737e2022-01-01 23:41:31 -0800637 request);
Muralidhar Reddy472c2ae2021-09-29 19:38:40 +0000638 uiccPort.iccTransmitApduBasicChannel(
Chen Xue9d737e2022-01-01 23:41:31 -0800639 iccArgument.cla, iccArgument.command, iccArgument.p1,
640 iccArgument.p2,
641 iccArgument.p3, iccArgument.data, onCompleted);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700642 }
643 break;
644
645 case EVENT_TRANSMIT_APDU_BASIC_CHANNEL_DONE:
646 ar = (AsyncResult) msg.obj;
647 request = (MainThreadRequest) ar.userObj;
648 if (ar.exception == null && ar.result != null) {
649 request.result = ar.result;
650 } else {
Chen Xue9d737e2022-01-01 23:41:31 -0800651 request.result = new IccIoResult(0x6F, 0, (byte[]) null);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700652 if (ar.result == null) {
653 loge("iccTransmitApduBasicChannel: Empty response");
654 } else if (ar.exception instanceof CommandException) {
655 loge("iccTransmitApduBasicChannel: CommandException: " +
656 ar.exception);
657 } else {
658 loge("iccTransmitApduBasicChannel: Unknown exception");
659 }
660 }
Pengquan Menga1bb6272018-09-06 09:59:22 -0700661 notifyRequester(request);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700662 break;
663
664 case CMD_EXCHANGE_SIM_IO:
665 request = (MainThreadRequest) msg.obj;
666 iccArgument = (IccAPDUArgument) request.argument;
Muralidhar Reddy472c2ae2021-09-29 19:38:40 +0000667 uiccPort = getUiccPortFromRequest(request);
668 if (uiccPort == null) {
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700669 loge("iccExchangeSimIO: No UICC");
Chen Xue9d737e2022-01-01 23:41:31 -0800670 request.result = new IccIoResult(0x6F, 0, (byte[]) null);
Pengquan Menga1bb6272018-09-06 09:59:22 -0700671 notifyRequester(request);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700672 } else {
673 onCompleted = obtainMessage(EVENT_EXCHANGE_SIM_IO_DONE,
674 request);
Muralidhar Reddy472c2ae2021-09-29 19:38:40 +0000675 uiccPort.iccExchangeSimIO(iccArgument.cla, /* fileID */
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700676 iccArgument.command, iccArgument.p1, iccArgument.p2, iccArgument.p3,
677 iccArgument.data, onCompleted);
678 }
679 break;
680
681 case EVENT_EXCHANGE_SIM_IO_DONE:
682 ar = (AsyncResult) msg.obj;
683 request = (MainThreadRequest) ar.userObj;
684 if (ar.exception == null && ar.result != null) {
685 request.result = ar.result;
686 } else {
Chen Xue9d737e2022-01-01 23:41:31 -0800687 request.result = new IccIoResult(0x6f, 0, (byte[]) null);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700688 }
Pengquan Menga1bb6272018-09-06 09:59:22 -0700689 notifyRequester(request);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700690 break;
691
Derek Tan4d5e5c12014-02-04 11:54:58 -0800692 case CMD_SEND_ENVELOPE:
693 request = (MainThreadRequest) msg.obj;
Muralidhar Reddy472c2ae2021-09-29 19:38:40 +0000694 uiccPort = getUiccPortFromRequest(request);
695 if (uiccPort == null) {
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700696 loge("sendEnvelopeWithStatus: No UICC");
Chen Xue9d737e2022-01-01 23:41:31 -0800697 request.result = new IccIoResult(0x6F, 0, (byte[]) null);
Pengquan Menga1bb6272018-09-06 09:59:22 -0700698 notifyRequester(request);
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700699 } else {
700 onCompleted = obtainMessage(EVENT_SEND_ENVELOPE_DONE, request);
Chen Xue9d737e2022-01-01 23:41:31 -0800701 uiccPort.sendEnvelopeWithStatus((String) request.argument, onCompleted);
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700702 }
Derek Tan4d5e5c12014-02-04 11:54:58 -0800703 break;
704
705 case EVENT_SEND_ENVELOPE_DONE:
706 ar = (AsyncResult) msg.obj;
707 request = (MainThreadRequest) ar.userObj;
Shishir Agrawal9f9877d2014-03-14 09:36:27 -0700708 if (ar.exception == null && ar.result != null) {
709 request.result = ar.result;
Derek Tan4d5e5c12014-02-04 11:54:58 -0800710 } else {
Chen Xue9d737e2022-01-01 23:41:31 -0800711 request.result = new IccIoResult(0x6F, 0, (byte[]) null);
Shishir Agrawal9f9877d2014-03-14 09:36:27 -0700712 if (ar.result == null) {
713 loge("sendEnvelopeWithStatus: Empty response");
714 } else if (ar.exception instanceof CommandException) {
715 loge("sendEnvelopeWithStatus: CommandException: " +
716 ar.exception);
717 } else {
718 loge("sendEnvelopeWithStatus: exception:" + ar.exception);
719 }
Derek Tan4d5e5c12014-02-04 11:54:58 -0800720 }
Pengquan Menga1bb6272018-09-06 09:59:22 -0700721 notifyRequester(request);
Derek Tan4d5e5c12014-02-04 11:54:58 -0800722 break;
723
Shishir Agrawal566b7612013-10-28 14:41:00 -0700724 case CMD_OPEN_CHANNEL:
725 request = (MainThreadRequest) msg.obj;
Muralidhar Reddy472c2ae2021-09-29 19:38:40 +0000726 uiccPort = getUiccPortFromRequest(request);
Rambo Wanga1782702021-11-10 20:15:19 -0800727 IccLogicalChannelRequest openChannelRequest =
728 (IccLogicalChannelRequest) request.argument;
Muralidhar Reddy472c2ae2021-09-29 19:38:40 +0000729 if (uiccPort == null) {
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700730 loge("iccOpenLogicalChannel: No UICC");
Shishir Agrawalfc0492a2016-02-17 11:15:33 -0800731 request.result = new IccOpenLogicalChannelResponse(-1,
Chen Xue9d737e2022-01-01 23:41:31 -0800732 IccOpenLogicalChannelResponse.STATUS_MISSING_RESOURCE, null);
Pengquan Menga1bb6272018-09-06 09:59:22 -0700733 notifyRequester(request);
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700734 } else {
735 onCompleted = obtainMessage(EVENT_OPEN_CHANNEL_DONE, request);
Rambo Wanga1782702021-11-10 20:15:19 -0800736 uiccPort.iccOpenLogicalChannel(openChannelRequest.aid,
737 openChannelRequest.p2, onCompleted);
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700738 }
Shishir Agrawal566b7612013-10-28 14:41:00 -0700739 break;
740
741 case EVENT_OPEN_CHANNEL_DONE:
742 ar = (AsyncResult) msg.obj;
743 request = (MainThreadRequest) ar.userObj;
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700744 IccOpenLogicalChannelResponse openChannelResp;
Shishir Agrawal566b7612013-10-28 14:41:00 -0700745 if (ar.exception == null && ar.result != null) {
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700746 int[] result = (int[]) ar.result;
747 int channelId = result[0];
748 byte[] selectResponse = null;
749 if (result.length > 1) {
750 selectResponse = new byte[result.length - 1];
751 for (int i = 1; i < result.length; ++i) {
752 selectResponse[i - 1] = (byte) result[i];
753 }
754 }
755 openChannelResp = new IccOpenLogicalChannelResponse(channelId,
Chen Xue9d737e2022-01-01 23:41:31 -0800756 IccOpenLogicalChannelResponse.STATUS_NO_ERROR, selectResponse);
Rambo Wang3b77c4c2021-11-10 20:15:19 -0800757
758 uiccPort = getUiccPortFromRequest(request);
Rambo Wange53e07d2022-05-10 13:01:13 -0700759 if (uiccPort == null) {
760 loge("EVENT_OPEN_CHANNEL_DONE: UiccPort is null");
761 } else {
762 IccLogicalChannelRequest channelRequest =
763 (IccLogicalChannelRequest) request.argument;
764 channelRequest.channel = channelId;
765 uiccPort.onLogicalChannelOpened(channelRequest);
766 }
Shishir Agrawal566b7612013-10-28 14:41:00 -0700767 } else {
Shishir Agrawal566b7612013-10-28 14:41:00 -0700768 if (ar.result == null) {
769 loge("iccOpenLogicalChannel: Empty response");
Shishir Agrawal566b7612013-10-28 14:41:00 -0700770 }
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700771 if (ar.exception != null) {
772 loge("iccOpenLogicalChannel: Exception: " + ar.exception);
773 }
774
Shishir Agrawal527e8bf2014-08-25 08:54:56 -0700775 int errorCode = IccOpenLogicalChannelResponse.STATUS_UNKNOWN_ERROR;
Junda Liua754ba12015-05-20 01:17:52 -0700776 if (ar.exception instanceof CommandException) {
777 CommandException.Error error =
Chen Xue9d737e2022-01-01 23:41:31 -0800778 ((CommandException) (ar.exception)).getCommandError();
Junda Liua754ba12015-05-20 01:17:52 -0700779 if (error == CommandException.Error.MISSING_RESOURCE) {
Shishir Agrawal527e8bf2014-08-25 08:54:56 -0700780 errorCode = IccOpenLogicalChannelResponse.STATUS_MISSING_RESOURCE;
Junda Liua754ba12015-05-20 01:17:52 -0700781 } else if (error == CommandException.Error.NO_SUCH_ELEMENT) {
Shishir Agrawal527e8bf2014-08-25 08:54:56 -0700782 errorCode = IccOpenLogicalChannelResponse.STATUS_NO_SUCH_ELEMENT;
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700783 }
784 }
785 openChannelResp = new IccOpenLogicalChannelResponse(
Chen Xue9d737e2022-01-01 23:41:31 -0800786 IccOpenLogicalChannelResponse.INVALID_CHANNEL, errorCode, null);
Shishir Agrawal566b7612013-10-28 14:41:00 -0700787 }
Shishir Agrawal82c8a462014-07-31 18:13:17 -0700788 request.result = openChannelResp;
Pengquan Menga1bb6272018-09-06 09:59:22 -0700789 notifyRequester(request);
Shishir Agrawal566b7612013-10-28 14:41:00 -0700790 break;
791
792 case CMD_CLOSE_CHANNEL:
793 request = (MainThreadRequest) msg.obj;
Muralidhar Reddy472c2ae2021-09-29 19:38:40 +0000794 uiccPort = getUiccPortFromRequest(request);
795 if (uiccPort == null) {
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700796 loge("iccCloseLogicalChannel: No UICC");
Chen Xua8f0dff2022-02-12 00:34:15 -0800797 request.result = new IllegalArgumentException(
Chen Xue9d737e2022-01-01 23:41:31 -0800798 "iccCloseLogicalChannel: No UICC");
Chen Xue9d737e2022-01-01 23:41:31 -0800799 notifyRequester(request);
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700800 } else {
801 onCompleted = obtainMessage(EVENT_CLOSE_CHANNEL_DONE, request);
Muralidhar Reddy472c2ae2021-09-29 19:38:40 +0000802 uiccPort.iccCloseLogicalChannel((Integer) request.argument, onCompleted);
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700803 }
Shishir Agrawal566b7612013-10-28 14:41:00 -0700804 break;
805
806 case EVENT_CLOSE_CHANNEL_DONE:
Chen Xu540470b2021-12-14 17:15:47 -0800807 ar = (AsyncResult) msg.obj;
808 request = (MainThreadRequest) ar.userObj;
809 if (ar.exception == null) {
810 request.result = true;
Rambo Wang3b77c4c2021-11-10 20:15:19 -0800811 uiccPort = getUiccPortFromRequest(request);
Rambo Wange53e07d2022-05-10 13:01:13 -0700812 if (uiccPort == null) {
813 loge("EVENT_CLOSE_CHANNEL_DONE: UiccPort is null");
814 } else {
815 final int channelId = (Integer) request.argument;
816 uiccPort.onLogicalChannelClosed(channelId);
817 }
Chen Xu540470b2021-12-14 17:15:47 -0800818 } else {
819 request.result = false;
Chen Xue9d737e2022-01-01 23:41:31 -0800820 Exception exception = null;
Chen Xu540470b2021-12-14 17:15:47 -0800821 if (ar.exception instanceof CommandException) {
822 loge("iccCloseLogicalChannel: CommandException: " + ar.exception);
823 CommandException.Error error =
824 ((CommandException) (ar.exception)).getCommandError();
Chen Xue9d737e2022-01-01 23:41:31 -0800825 if (error == CommandException.Error.INVALID_ARGUMENTS) {
826 // should only throw exceptions from the binder threads.
827 exception = new IllegalArgumentException(
Chen Xu540470b2021-12-14 17:15:47 -0800828 "iccCloseLogicalChannel: invalid argument ");
829 }
830 } else {
831 loge("iccCloseLogicalChannel: Unknown exception");
832 }
Chen Xua8f0dff2022-02-12 00:34:15 -0800833 request.result = (exception != null) ? exception :
834 new IllegalStateException(
835 "exception from modem to close iccLogical Channel");
Chen Xu540470b2021-12-14 17:15:47 -0800836 }
837 notifyRequester(request);
Jake Hambye994d462014-02-03 13:10:13 -0800838 break;
839
840 case CMD_NV_READ_ITEM:
841 request = (MainThreadRequest) msg.obj;
842 onCompleted = obtainMessage(EVENT_NV_READ_ITEM_DONE, request);
Nazanin Bakhshif782e562018-12-11 15:15:39 -0800843 defaultPhone.nvReadItem((Integer) request.argument, onCompleted,
844 request.workSource);
Jake Hambye994d462014-02-03 13:10:13 -0800845 break;
846
847 case EVENT_NV_READ_ITEM_DONE:
Shishir Agrawal566b7612013-10-28 14:41:00 -0700848 ar = (AsyncResult) msg.obj;
849 request = (MainThreadRequest) ar.userObj;
Jake Hambye994d462014-02-03 13:10:13 -0800850 if (ar.exception == null && ar.result != null) {
851 request.result = ar.result; // String
Shishir Agrawal566b7612013-10-28 14:41:00 -0700852 } else {
Jake Hambye994d462014-02-03 13:10:13 -0800853 request.result = "";
854 if (ar.result == null) {
855 loge("nvReadItem: Empty response");
856 } else if (ar.exception instanceof CommandException) {
857 loge("nvReadItem: CommandException: " +
858 ar.exception);
Shishir Agrawal566b7612013-10-28 14:41:00 -0700859 } else {
Jake Hambye994d462014-02-03 13:10:13 -0800860 loge("nvReadItem: Unknown exception");
Shishir Agrawal566b7612013-10-28 14:41:00 -0700861 }
862 }
Pengquan Menga1bb6272018-09-06 09:59:22 -0700863 notifyRequester(request);
Shishir Agrawal566b7612013-10-28 14:41:00 -0700864 break;
865
Jake Hambye994d462014-02-03 13:10:13 -0800866 case CMD_NV_WRITE_ITEM:
867 request = (MainThreadRequest) msg.obj;
868 onCompleted = obtainMessage(EVENT_NV_WRITE_ITEM_DONE, request);
869 Pair<Integer, String> idValue = (Pair<Integer, String>) request.argument;
Nazanin Bakhshif782e562018-12-11 15:15:39 -0800870 defaultPhone.nvWriteItem(idValue.first, idValue.second, onCompleted,
vagdeviaf9a5b92018-08-15 16:01:53 -0700871 request.workSource);
Jake Hambye994d462014-02-03 13:10:13 -0800872 break;
873
874 case EVENT_NV_WRITE_ITEM_DONE:
875 handleNullReturnEvent(msg, "nvWriteItem");
876 break;
877
878 case CMD_NV_WRITE_CDMA_PRL:
879 request = (MainThreadRequest) msg.obj;
880 onCompleted = obtainMessage(EVENT_NV_WRITE_CDMA_PRL_DONE, request);
Nazanin Bakhshif782e562018-12-11 15:15:39 -0800881 defaultPhone.nvWriteCdmaPrl((byte[]) request.argument, onCompleted);
Jake Hambye994d462014-02-03 13:10:13 -0800882 break;
883
884 case EVENT_NV_WRITE_CDMA_PRL_DONE:
885 handleNullReturnEvent(msg, "nvWriteCdmaPrl");
886 break;
887
chen xu6dac5ab2018-10-26 17:39:23 -0700888 case CMD_RESET_MODEM_CONFIG:
Jake Hambye994d462014-02-03 13:10:13 -0800889 request = (MainThreadRequest) msg.obj;
chen xu6dac5ab2018-10-26 17:39:23 -0700890 onCompleted = obtainMessage(EVENT_RESET_MODEM_CONFIG_DONE, request);
Nazanin Bakhshif782e562018-12-11 15:15:39 -0800891 defaultPhone.resetModemConfig(onCompleted);
Jake Hambye994d462014-02-03 13:10:13 -0800892 break;
893
chen xu6dac5ab2018-10-26 17:39:23 -0700894 case EVENT_RESET_MODEM_CONFIG_DONE:
895 handleNullReturnEvent(msg, "resetModemConfig");
Jake Hambye994d462014-02-03 13:10:13 -0800896 break;
897
Sooraj Sasindran37444802020-08-11 10:40:43 -0700898 case CMD_IS_NR_DUAL_CONNECTIVITY_ENABLED: {
899 request = (MainThreadRequest) msg.obj;
900 onCompleted = obtainMessage(EVENT_IS_NR_DUAL_CONNECTIVITY_ENABLED_DONE,
901 request);
902 Phone phone = getPhoneFromRequest(request);
903 if (phone != null) {
904 phone.isNrDualConnectivityEnabled(onCompleted, request.workSource);
905 } else {
906 loge("isNRDualConnectivityEnabled: No phone object");
907 request.result = false;
908 notifyRequester(request);
909 }
910 break;
911 }
912
913 case EVENT_IS_NR_DUAL_CONNECTIVITY_ENABLED_DONE:
914 ar = (AsyncResult) msg.obj;
915 request = (MainThreadRequest) ar.userObj;
916 if (ar.exception == null && ar.result != null) {
917 request.result = ar.result;
918 } else {
919 // request.result must be set to something non-null
920 // for the calling thread to unblock
Sooraj Sasindrandaf060f2021-06-15 14:52:55 -0700921 if (ar.result != null) {
Sooraj Sasindran37444802020-08-11 10:40:43 -0700922 request.result = ar.result;
923 } else {
924 request.result = false;
925 }
926 if (ar.result == null) {
927 loge("isNRDualConnectivityEnabled: Empty response");
928 } else if (ar.exception instanceof CommandException) {
929 loge("isNRDualConnectivityEnabled: CommandException: "
930 + ar.exception);
931 } else {
932 loge("isNRDualConnectivityEnabled: Unknown exception");
933 }
934 }
935 notifyRequester(request);
936 break;
937
Sooraj Sasindrandaf060f2021-06-15 14:52:55 -0700938 case CMD_IS_VONR_ENABLED: {
939 request = (MainThreadRequest) msg.obj;
940 onCompleted = obtainMessage(EVENT_IS_VONR_ENABLED_DONE,
941 request);
942 Phone phone = getPhoneFromRequest(request);
943 if (phone != null) {
944 phone.isVoNrEnabled(onCompleted, request.workSource);
945 } else {
946 loge("isVoNrEnabled: No phone object");
947 request.result = false;
948 notifyRequester(request);
949 }
950 break;
951 }
952
953 case EVENT_IS_VONR_ENABLED_DONE:
954 ar = (AsyncResult) msg.obj;
955 request = (MainThreadRequest) ar.userObj;
956 if (ar.exception == null && ar.result != null) {
957 request.result = ar.result;
958 } else {
959 // request.result must be set to something non-null
960 // for the calling thread to unblock
961 if (ar.result != null) {
962 request.result = ar.result;
963 } else {
964 request.result = false;
965 }
966 if (ar.result == null) {
967 loge("isVoNrEnabled: Empty response");
968 } else if (ar.exception instanceof CommandException) {
969 loge("isVoNrEnabled: CommandException: "
970 + ar.exception);
971 } else {
972 loge("isVoNrEnabled: Unknown exception");
973 }
974 }
975 notifyRequester(request);
976 break;
977
Sooraj Sasindran37444802020-08-11 10:40:43 -0700978 case CMD_ENABLE_NR_DUAL_CONNECTIVITY: {
979 request = (MainThreadRequest) msg.obj;
980 onCompleted = obtainMessage(EVENT_ENABLE_NR_DUAL_CONNECTIVITY_DONE, request);
981 Phone phone = getPhoneFromRequest(request);
982 if (phone != null) {
983 phone.setNrDualConnectivityState((int) request.argument, onCompleted,
984 request.workSource);
985 } else {
986 loge("enableNrDualConnectivity: No phone object");
987 request.result =
988 TelephonyManager.ENABLE_NR_DUAL_CONNECTIVITY_RADIO_NOT_AVAILABLE;
989 notifyRequester(request);
990 }
991 break;
992 }
993
994 case EVENT_ENABLE_NR_DUAL_CONNECTIVITY_DONE: {
995 ar = (AsyncResult) msg.obj;
996 request = (MainThreadRequest) ar.userObj;
997 if (ar.exception == null) {
998 request.result =
999 TelephonyManager.ENABLE_NR_DUAL_CONNECTIVITY_SUCCESS;
1000 } else {
1001 request.result =
1002 TelephonyManager
1003 .ENABLE_NR_DUAL_CONNECTIVITY_RADIO_ERROR;
1004 if (ar.exception instanceof CommandException) {
1005 CommandException.Error error =
1006 ((CommandException) (ar.exception)).getCommandError();
1007 if (error == CommandException.Error.RADIO_NOT_AVAILABLE) {
1008 request.result =
1009 TelephonyManager
1010 .ENABLE_NR_DUAL_CONNECTIVITY_RADIO_NOT_AVAILABLE;
Sooraj Sasindran29654162021-03-03 23:00:01 +00001011 } else if (error == CommandException.Error.REQUEST_NOT_SUPPORTED) {
1012 request.result =
1013 TelephonyManager
1014 .ENABLE_NR_DUAL_CONNECTIVITY_NOT_SUPPORTED;
Sooraj Sasindran37444802020-08-11 10:40:43 -07001015 }
1016 loge("enableNrDualConnectivity" + ": CommandException: "
1017 + ar.exception);
1018 } else {
1019 loge("enableNrDualConnectivity" + ": Unknown exception");
1020 }
1021 }
1022 notifyRequester(request);
1023 break;
1024 }
1025
Sooraj Sasindrandaf060f2021-06-15 14:52:55 -07001026 case CMD_ENABLE_VONR: {
1027 request = (MainThreadRequest) msg.obj;
1028 onCompleted = obtainMessage(EVENT_ENABLE_VONR_DONE, request);
1029 Phone phone = getPhoneFromRequest(request);
1030 if (phone != null) {
1031 phone.setVoNrEnabled((boolean) request.argument, onCompleted,
1032 request.workSource);
1033 } else {
1034 loge("setVoNrEnabled: No phone object");
1035 request.result =
1036 TelephonyManager.ENABLE_VONR_RADIO_NOT_AVAILABLE;
1037 notifyRequester(request);
1038 }
1039 break;
1040 }
1041
1042 case EVENT_ENABLE_VONR_DONE: {
1043 ar = (AsyncResult) msg.obj;
1044 request = (MainThreadRequest) ar.userObj;
1045 if (ar.exception == null) {
1046 request.result = TelephonyManager.ENABLE_VONR_SUCCESS;
1047 } else {
1048 request.result = TelephonyManager.ENABLE_VONR_RADIO_ERROR;
1049 if (ar.exception instanceof CommandException) {
1050 CommandException.Error error =
1051 ((CommandException) (ar.exception)).getCommandError();
1052 if (error == CommandException.Error.RADIO_NOT_AVAILABLE) {
1053 request.result = TelephonyManager.ENABLE_VONR_RADIO_NOT_AVAILABLE;
1054 } else if (error == CommandException.Error.REQUEST_NOT_SUPPORTED) {
1055 request.result = TelephonyManager.ENABLE_VONR_REQUEST_NOT_SUPPORTED;
1056 } else {
1057 request.result = TelephonyManager.ENABLE_VONR_RADIO_ERROR;
1058 }
1059 loge("setVoNrEnabled" + ": CommandException: "
1060 + ar.exception);
1061 } else {
1062 loge("setVoNrEnabled" + ": Unknown exception");
1063 }
1064 }
1065 notifyRequester(request);
1066 break;
1067 }
1068
SongFerngWang3ef3e072020-12-21 16:41:52 +08001069 case CMD_GET_ALLOWED_NETWORK_TYPES_BITMASK:
Jake Hamby7c27be32014-03-03 13:25:59 -08001070 request = (MainThreadRequest) msg.obj;
SongFerngWang3ef3e072020-12-21 16:41:52 +08001071 onCompleted = obtainMessage(EVENT_GET_ALLOWED_NETWORK_TYPES_BITMASK_DONE,
1072 request);
1073 getPhoneFromRequest(request).getAllowedNetworkTypesBitmask(onCompleted);
Jake Hamby7c27be32014-03-03 13:25:59 -08001074 break;
1075
SongFerngWang3ef3e072020-12-21 16:41:52 +08001076 case EVENT_GET_ALLOWED_NETWORK_TYPES_BITMASK_DONE:
Jake Hamby7c27be32014-03-03 13:25:59 -08001077 ar = (AsyncResult) msg.obj;
1078 request = (MainThreadRequest) ar.userObj;
1079 if (ar.exception == null && ar.result != null) {
1080 request.result = ar.result; // Integer
1081 } else {
Nazish Tabassume8ba43a2020-07-28 14:49:25 +05301082 // request.result must be set to something non-null
1083 // for the calling thread to unblock
1084 request.result = new int[]{-1};
Jake Hamby7c27be32014-03-03 13:25:59 -08001085 if (ar.result == null) {
SongFerngWang3ef3e072020-12-21 16:41:52 +08001086 loge("getAllowedNetworkTypesBitmask: Empty response");
Jake Hamby7c27be32014-03-03 13:25:59 -08001087 } else if (ar.exception instanceof CommandException) {
SongFerngWang3ef3e072020-12-21 16:41:52 +08001088 loge("getAllowedNetworkTypesBitmask: CommandException: "
1089 + ar.exception);
Jake Hamby7c27be32014-03-03 13:25:59 -08001090 } else {
SongFerngWang3ef3e072020-12-21 16:41:52 +08001091 loge("getAllowedNetworkTypesBitmask: Unknown exception");
Jake Hamby7c27be32014-03-03 13:25:59 -08001092 }
1093 }
Pengquan Menga1bb6272018-09-06 09:59:22 -07001094 notifyRequester(request);
Jake Hamby7c27be32014-03-03 13:25:59 -08001095 break;
1096
SongFerngWang3ef3e072020-12-21 16:41:52 +08001097 case CMD_SET_ALLOWED_NETWORK_TYPES_FOR_REASON:
Jake Hamby7c27be32014-03-03 13:25:59 -08001098 request = (MainThreadRequest) msg.obj;
SongFerngWang3ef3e072020-12-21 16:41:52 +08001099 onCompleted = obtainMessage(EVENT_SET_ALLOWED_NETWORK_TYPES_FOR_REASON_DONE,
1100 request);
1101 Pair<Integer, Long> reasonWithNetworkTypes =
1102 (Pair<Integer, Long>) request.argument;
1103 getPhoneFromRequest(request).setAllowedNetworkTypes(
1104 reasonWithNetworkTypes.first,
1105 reasonWithNetworkTypes.second,
1106 onCompleted);
Jake Hamby7c27be32014-03-03 13:25:59 -08001107 break;
1108
SongFerngWang3ef3e072020-12-21 16:41:52 +08001109 case EVENT_SET_ALLOWED_NETWORK_TYPES_FOR_REASON_DONE:
1110 handleNullReturnEvent(msg, "setAllowedNetworkTypesForReason");
Jake Hamby7c27be32014-03-03 13:25:59 -08001111 break;
1112
Shuo Qian850e4d6a2018-04-25 21:02:08 +00001113 case CMD_INVOKE_OEM_RIL_REQUEST_RAW:
1114 request = (MainThreadRequest)msg.obj;
1115 onCompleted = obtainMessage(EVENT_INVOKE_OEM_RIL_REQUEST_RAW_DONE, request);
Nazanin Bakhshif782e562018-12-11 15:15:39 -08001116 defaultPhone.invokeOemRilRequestRaw((byte[]) request.argument, onCompleted);
Shuo Qian850e4d6a2018-04-25 21:02:08 +00001117 break;
1118
1119 case EVENT_INVOKE_OEM_RIL_REQUEST_RAW_DONE:
1120 ar = (AsyncResult)msg.obj;
1121 request = (MainThreadRequest)ar.userObj;
1122 request.result = ar;
Pengquan Menga1bb6272018-09-06 09:59:22 -07001123 notifyRequester(request);
Shuo Qian850e4d6a2018-04-25 21:02:08 +00001124 break;
1125
Shishir Agrawal76d5da92014-11-09 16:17:25 -08001126 case CMD_SET_VOICEMAIL_NUMBER:
1127 request = (MainThreadRequest) msg.obj;
1128 onCompleted = obtainMessage(EVENT_SET_VOICEMAIL_NUMBER_DONE, request);
1129 Pair<String, String> tagNum = (Pair<String, String>) request.argument;
Stuart Scott584921c2015-01-15 17:10:34 -08001130 getPhoneFromRequest(request).setVoiceMailNumber(tagNum.first, tagNum.second,
1131 onCompleted);
Shishir Agrawal76d5da92014-11-09 16:17:25 -08001132 break;
1133
1134 case EVENT_SET_VOICEMAIL_NUMBER_DONE:
1135 handleNullReturnEvent(msg, "setVoicemailNumber");
1136 break;
1137
Stuart Scott54788802015-03-30 13:18:01 -07001138 case CMD_SET_NETWORK_SELECTION_MODE_AUTOMATIC:
1139 request = (MainThreadRequest) msg.obj;
1140 onCompleted = obtainMessage(EVENT_SET_NETWORK_SELECTION_MODE_AUTOMATIC_DONE,
1141 request);
1142 getPhoneFromRequest(request).setNetworkSelectionModeAutomatic(onCompleted);
1143 break;
1144
1145 case EVENT_SET_NETWORK_SELECTION_MODE_AUTOMATIC_DONE:
1146 handleNullReturnEvent(msg, "setNetworkSelectionModeAutomatic");
1147 break;
1148
Shishir Agrawal302c8692015-06-19 13:49:39 -07001149 case CMD_PERFORM_NETWORK_SCAN:
1150 request = (MainThreadRequest) msg.obj;
1151 onCompleted = obtainMessage(EVENT_PERFORM_NETWORK_SCAN_DONE, request);
1152 getPhoneFromRequest(request).getAvailableNetworks(onCompleted);
1153 break;
1154
Hall Liu27d24262020-09-18 19:04:59 -07001155 case CMD_GET_CALL_FORWARDING: {
Shuo Qian4a594052020-01-23 11:59:30 -08001156 request = (MainThreadRequest) msg.obj;
1157 onCompleted = obtainMessage(EVENT_GET_CALL_FORWARDING_DONE, request);
Hall Liu27d24262020-09-18 19:04:59 -07001158 Pair<Integer, TelephonyManager.CallForwardingInfoCallback> args =
1159 (Pair<Integer, TelephonyManager.CallForwardingInfoCallback>)
1160 request.argument;
1161 int callForwardingReason = args.first;
1162 request.phone.getCallForwardingOption(callForwardingReason, onCompleted);
Shuo Qian4a594052020-01-23 11:59:30 -08001163 break;
Hall Liu27d24262020-09-18 19:04:59 -07001164 }
1165 case EVENT_GET_CALL_FORWARDING_DONE: {
Shuo Qian4a594052020-01-23 11:59:30 -08001166 ar = (AsyncResult) msg.obj;
1167 request = (MainThreadRequest) ar.userObj;
Hall Liu27d24262020-09-18 19:04:59 -07001168 TelephonyManager.CallForwardingInfoCallback callback =
1169 ((Pair<Integer, TelephonyManager.CallForwardingInfoCallback>)
1170 request.argument).second;
Shuo Qian4a594052020-01-23 11:59:30 -08001171 if (ar.exception == null && ar.result != null) {
Hall Liu27d24262020-09-18 19:04:59 -07001172 CallForwardingInfo callForwardingInfo = null;
Shuo Qian4a594052020-01-23 11:59:30 -08001173 CallForwardInfo[] callForwardInfos = (CallForwardInfo[]) ar.result;
1174 for (CallForwardInfo callForwardInfo : callForwardInfos) {
1175 // Service Class is a bit mask per 3gpp 27.007. Search for
1176 // any service for voice call.
1177 if ((callForwardInfo.serviceClass
1178 & CommandsInterface.SERVICE_CLASS_VOICE) > 0) {
Yuchen Dong69cc1412021-09-27 20:27:01 +08001179 callForwardingInfo = new CallForwardingInfo(
1180 callForwardInfo.status
1181 == CommandsInterface.CF_ACTION_ENABLE,
Hall Liu27d24262020-09-18 19:04:59 -07001182 callForwardInfo.reason,
1183 callForwardInfo.number,
1184 callForwardInfo.timeSeconds);
Shuo Qian4a594052020-01-23 11:59:30 -08001185 break;
1186 }
1187 }
1188 // Didn't find a call forward info for voice call.
1189 if (callForwardingInfo == null) {
Hall Liu27d24262020-09-18 19:04:59 -07001190 callForwardingInfo = new CallForwardingInfo(false /* enabled */,
1191 0 /* reason */, null /* number */, 0 /* timeout */);
Shuo Qian4a594052020-01-23 11:59:30 -08001192 }
Hall Liu27d24262020-09-18 19:04:59 -07001193 callback.onCallForwardingInfoAvailable(callForwardingInfo);
Shuo Qian4a594052020-01-23 11:59:30 -08001194 } else {
1195 if (ar.result == null) {
1196 loge("EVENT_GET_CALL_FORWARDING_DONE: Empty response");
1197 }
1198 if (ar.exception != null) {
1199 loge("EVENT_GET_CALL_FORWARDING_DONE: Exception: " + ar.exception);
1200 }
Hall Liu940c4ca2020-09-29 17:10:18 -07001201 int errorCode = TelephonyManager
1202 .CallForwardingInfoCallback.RESULT_ERROR_UNKNOWN;
Shuo Qian4a594052020-01-23 11:59:30 -08001203 if (ar.exception instanceof CommandException) {
1204 CommandException.Error error =
1205 ((CommandException) (ar.exception)).getCommandError();
1206 if (error == CommandException.Error.FDN_CHECK_FAILURE) {
Hall Liu940c4ca2020-09-29 17:10:18 -07001207 errorCode = TelephonyManager
1208 .CallForwardingInfoCallback.RESULT_ERROR_FDN_CHECK_FAILURE;
Shuo Qian4a594052020-01-23 11:59:30 -08001209 } else if (error == CommandException.Error.REQUEST_NOT_SUPPORTED) {
Hall Liu940c4ca2020-09-29 17:10:18 -07001210 errorCode = TelephonyManager
1211 .CallForwardingInfoCallback.RESULT_ERROR_NOT_SUPPORTED;
Shuo Qian4a594052020-01-23 11:59:30 -08001212 }
1213 }
Hall Liu27d24262020-09-18 19:04:59 -07001214 callback.onError(errorCode);
Shuo Qian4a594052020-01-23 11:59:30 -08001215 }
Shuo Qian4a594052020-01-23 11:59:30 -08001216 break;
Hall Liu27d24262020-09-18 19:04:59 -07001217 }
Shuo Qian4a594052020-01-23 11:59:30 -08001218
Hall Liu27d24262020-09-18 19:04:59 -07001219 case CMD_SET_CALL_FORWARDING: {
Shuo Qian4a594052020-01-23 11:59:30 -08001220 request = (MainThreadRequest) msg.obj;
1221 onCompleted = obtainMessage(EVENT_SET_CALL_FORWARDING_DONE, request);
Hall Liu27d24262020-09-18 19:04:59 -07001222 request = (MainThreadRequest) msg.obj;
Shuo Qian4a594052020-01-23 11:59:30 -08001223 CallForwardingInfo callForwardingInfoToSet =
Hall Liu27d24262020-09-18 19:04:59 -07001224 ((Pair<CallForwardingInfo, Consumer<Integer>>)
1225 request.argument).first;
1226 request.phone.setCallForwardingOption(
1227 callForwardingInfoToSet.isEnabled()
Calvin Pan258f1f72021-07-28 21:46:56 +08001228 ? CommandsInterface.CF_ACTION_REGISTRATION
Hall Liu27d24262020-09-18 19:04:59 -07001229 : CommandsInterface.CF_ACTION_DISABLE,
Shuo Qian4a594052020-01-23 11:59:30 -08001230 callForwardingInfoToSet.getReason(),
1231 callForwardingInfoToSet.getNumber(),
1232 callForwardingInfoToSet.getTimeoutSeconds(), onCompleted);
1233 break;
Hall Liu27d24262020-09-18 19:04:59 -07001234 }
Shuo Qian4a594052020-01-23 11:59:30 -08001235
Hall Liu27d24262020-09-18 19:04:59 -07001236 case EVENT_SET_CALL_FORWARDING_DONE: {
Shuo Qian4a594052020-01-23 11:59:30 -08001237 ar = (AsyncResult) msg.obj;
1238 request = (MainThreadRequest) ar.userObj;
Hall Liu27d24262020-09-18 19:04:59 -07001239 Consumer<Integer> callback =
1240 ((Pair<CallForwardingInfo, Consumer<Integer>>)
1241 request.argument).second;
1242 if (ar.exception != null) {
Shuo Qian4a594052020-01-23 11:59:30 -08001243 loge("setCallForwarding exception: " + ar.exception);
Hall Liu940c4ca2020-09-29 17:10:18 -07001244 int errorCode = TelephonyManager.CallForwardingInfoCallback
1245 .RESULT_ERROR_UNKNOWN;
Hall Liu27d24262020-09-18 19:04:59 -07001246 if (ar.exception instanceof CommandException) {
1247 CommandException.Error error =
1248 ((CommandException) (ar.exception)).getCommandError();
1249 if (error == CommandException.Error.FDN_CHECK_FAILURE) {
Hall Liu940c4ca2020-09-29 17:10:18 -07001250 errorCode = TelephonyManager.CallForwardingInfoCallback
1251 .RESULT_ERROR_FDN_CHECK_FAILURE;
Hall Liu27d24262020-09-18 19:04:59 -07001252 } else if (error == CommandException.Error.REQUEST_NOT_SUPPORTED) {
Hall Liu940c4ca2020-09-29 17:10:18 -07001253 errorCode = TelephonyManager.CallForwardingInfoCallback
1254 .RESULT_ERROR_NOT_SUPPORTED;
Hall Liu27d24262020-09-18 19:04:59 -07001255 }
1256 }
1257 callback.accept(errorCode);
1258 } else {
Hall Liu940c4ca2020-09-29 17:10:18 -07001259 callback.accept(TelephonyManager.CallForwardingInfoCallback.RESULT_SUCCESS);
Shuo Qian4a594052020-01-23 11:59:30 -08001260 }
Shuo Qian4a594052020-01-23 11:59:30 -08001261 break;
Hall Liu27d24262020-09-18 19:04:59 -07001262 }
Shuo Qian4a594052020-01-23 11:59:30 -08001263
Hall Liu27d24262020-09-18 19:04:59 -07001264 case CMD_GET_CALL_WAITING: {
Shuo Qian4a594052020-01-23 11:59:30 -08001265 request = (MainThreadRequest) msg.obj;
1266 onCompleted = obtainMessage(EVENT_GET_CALL_WAITING_DONE, request);
1267 getPhoneFromRequest(request).getCallWaiting(onCompleted);
1268 break;
Hall Liu27d24262020-09-18 19:04:59 -07001269 }
Shuo Qian4a594052020-01-23 11:59:30 -08001270
Hall Liu27d24262020-09-18 19:04:59 -07001271 case EVENT_GET_CALL_WAITING_DONE: {
Shuo Qian4a594052020-01-23 11:59:30 -08001272 ar = (AsyncResult) msg.obj;
1273 request = (MainThreadRequest) ar.userObj;
Hall Liu27d24262020-09-18 19:04:59 -07001274 Consumer<Integer> callback = (Consumer<Integer>) request.argument;
SongFerngWangebda2c52022-01-11 15:28:38 +08001275 int callWaitingStatus = TelephonyManager.CALL_WAITING_STATUS_UNKNOWN_ERROR;
Shuo Qian4a594052020-01-23 11:59:30 -08001276 if (ar.exception == null && ar.result != null) {
Shuo Qiand6a0dba2020-02-18 18:13:49 -08001277 int[] callForwardResults = (int[]) ar.result;
Shuo Qian4a594052020-01-23 11:59:30 -08001278 // Service Class is a bit mask per 3gpp 27.007.
1279 // Search for any service for voice call.
Shuo Qiand6a0dba2020-02-18 18:13:49 -08001280 if (callForwardResults.length > 1
1281 && ((callForwardResults[1]
Hall Liu27d24262020-09-18 19:04:59 -07001282 & CommandsInterface.SERVICE_CLASS_VOICE) > 0)) {
SongFerngWangebda2c52022-01-11 15:28:38 +08001283 callWaitingStatus = callForwardResults[0] == 0
Hall Liu27d24262020-09-18 19:04:59 -07001284 ? TelephonyManager.CALL_WAITING_STATUS_DISABLED
1285 : TelephonyManager.CALL_WAITING_STATUS_ENABLED;
Shuo Qian4a594052020-01-23 11:59:30 -08001286 } else {
SongFerngWangebda2c52022-01-11 15:28:38 +08001287 callWaitingStatus = TelephonyManager.CALL_WAITING_STATUS_DISABLED;
Shuo Qian4a594052020-01-23 11:59:30 -08001288 }
1289 } else {
1290 if (ar.result == null) {
1291 loge("EVENT_GET_CALL_WAITING_DONE: Empty response");
1292 }
1293 if (ar.exception != null) {
1294 loge("EVENT_GET_CALL_WAITING_DONE: Exception: " + ar.exception);
1295 }
1296 if (ar.exception instanceof CommandException) {
1297 CommandException.Error error =
1298 ((CommandException) (ar.exception)).getCommandError();
1299 if (error == CommandException.Error.REQUEST_NOT_SUPPORTED) {
SongFerngWangebda2c52022-01-11 15:28:38 +08001300 callWaitingStatus =
Shuo Qian4a594052020-01-23 11:59:30 -08001301 TelephonyManager.CALL_WAITING_STATUS_NOT_SUPPORTED;
SongFerngWangebda2c52022-01-11 15:28:38 +08001302 } else if (error == CommandException.Error.FDN_CHECK_FAILURE) {
1303 callWaitingStatus =
1304 TelephonyManager.CALL_WAITING_STATUS_FDN_CHECK_FAILURE;
Shuo Qian4a594052020-01-23 11:59:30 -08001305 }
1306 }
1307 }
SongFerngWangebda2c52022-01-11 15:28:38 +08001308 callback.accept(callWaitingStatus);
Shuo Qian4a594052020-01-23 11:59:30 -08001309 break;
Hall Liu27d24262020-09-18 19:04:59 -07001310 }
Shuo Qian4a594052020-01-23 11:59:30 -08001311
Hall Liu27d24262020-09-18 19:04:59 -07001312 case CMD_SET_CALL_WAITING: {
Shuo Qian4a594052020-01-23 11:59:30 -08001313 request = (MainThreadRequest) msg.obj;
1314 onCompleted = obtainMessage(EVENT_SET_CALL_WAITING_DONE, request);
Hall Liu27d24262020-09-18 19:04:59 -07001315 boolean enable = ((Pair<Boolean, Consumer<Integer>>) request.argument).first;
1316 getPhoneFromRequest(request).setCallWaiting(enable, onCompleted);
Shuo Qian4a594052020-01-23 11:59:30 -08001317 break;
Hall Liu27d24262020-09-18 19:04:59 -07001318 }
Shuo Qian4a594052020-01-23 11:59:30 -08001319
Hall Liu27d24262020-09-18 19:04:59 -07001320 case EVENT_SET_CALL_WAITING_DONE: {
Shuo Qian4a594052020-01-23 11:59:30 -08001321 ar = (AsyncResult) msg.obj;
1322 request = (MainThreadRequest) ar.userObj;
Hall Liu27d24262020-09-18 19:04:59 -07001323 boolean enable = ((Pair<Boolean, Consumer<Integer>>) request.argument).first;
1324 Consumer<Integer> callback =
1325 ((Pair<Boolean, Consumer<Integer>>) request.argument).second;
1326 if (ar.exception != null) {
Shuo Qian4a594052020-01-23 11:59:30 -08001327 loge("setCallWaiting exception: " + ar.exception);
Hall Liu27d24262020-09-18 19:04:59 -07001328 if (ar.exception instanceof CommandException) {
1329 CommandException.Error error =
1330 ((CommandException) (ar.exception)).getCommandError();
1331 if (error == CommandException.Error.REQUEST_NOT_SUPPORTED) {
1332 callback.accept(TelephonyManager.CALL_WAITING_STATUS_NOT_SUPPORTED);
SongFerngWangebda2c52022-01-11 15:28:38 +08001333 } else if (error == CommandException.Error.FDN_CHECK_FAILURE) {
1334 callback.accept(
1335 TelephonyManager.CALL_WAITING_STATUS_FDN_CHECK_FAILURE);
Hall Liu27d24262020-09-18 19:04:59 -07001336 } else {
1337 callback.accept(TelephonyManager.CALL_WAITING_STATUS_UNKNOWN_ERROR);
1338 }
1339 } else {
1340 callback.accept(TelephonyManager.CALL_WAITING_STATUS_UNKNOWN_ERROR);
1341 }
1342 } else {
1343 callback.accept(enable ? TelephonyManager.CALL_WAITING_STATUS_ENABLED
1344 : TelephonyManager.CALL_WAITING_STATUS_DISABLED);
Shuo Qian4a594052020-01-23 11:59:30 -08001345 }
Shuo Qian4a594052020-01-23 11:59:30 -08001346 break;
Hall Liu27d24262020-09-18 19:04:59 -07001347 }
Shishir Agrawal302c8692015-06-19 13:49:39 -07001348 case EVENT_PERFORM_NETWORK_SCAN_DONE:
1349 ar = (AsyncResult) msg.obj;
1350 request = (MainThreadRequest) ar.userObj;
1351 CellNetworkScanResult cellScanResult;
1352 if (ar.exception == null && ar.result != null) {
1353 cellScanResult = new CellNetworkScanResult(
1354 CellNetworkScanResult.STATUS_SUCCESS,
1355 (List<OperatorInfo>) ar.result);
1356 } else {
1357 if (ar.result == null) {
1358 loge("getCellNetworkScanResults: Empty response");
1359 }
1360 if (ar.exception != null) {
1361 loge("getCellNetworkScanResults: Exception: " + ar.exception);
1362 }
1363 int errorCode = CellNetworkScanResult.STATUS_UNKNOWN_ERROR;
1364 if (ar.exception instanceof CommandException) {
1365 CommandException.Error error =
1366 ((CommandException) (ar.exception)).getCommandError();
1367 if (error == CommandException.Error.RADIO_NOT_AVAILABLE) {
1368 errorCode = CellNetworkScanResult.STATUS_RADIO_NOT_AVAILABLE;
1369 } else if (error == CommandException.Error.GENERIC_FAILURE) {
1370 errorCode = CellNetworkScanResult.STATUS_RADIO_GENERIC_FAILURE;
1371 }
1372 }
1373 cellScanResult = new CellNetworkScanResult(errorCode, null);
1374 }
1375 request.result = cellScanResult;
Pengquan Menga1bb6272018-09-06 09:59:22 -07001376 notifyRequester(request);
Shishir Agrawal302c8692015-06-19 13:49:39 -07001377 break;
1378
1379 case CMD_SET_NETWORK_SELECTION_MODE_MANUAL:
1380 request = (MainThreadRequest) msg.obj;
Shishir Agrawal77ba3172015-09-10 14:50:19 -07001381 ManualNetworkSelectionArgument selArg =
1382 (ManualNetworkSelectionArgument) request.argument;
Shishir Agrawal302c8692015-06-19 13:49:39 -07001383 onCompleted = obtainMessage(EVENT_SET_NETWORK_SELECTION_MODE_MANUAL_DONE,
1384 request);
Shishir Agrawal77ba3172015-09-10 14:50:19 -07001385 getPhoneFromRequest(request).selectNetworkManually(selArg.operatorInfo,
1386 selArg.persistSelection, onCompleted);
Shishir Agrawal302c8692015-06-19 13:49:39 -07001387 break;
1388
1389 case EVENT_SET_NETWORK_SELECTION_MODE_MANUAL_DONE:
Pengquan Menge3d01e22018-09-20 15:25:35 -07001390 ar = (AsyncResult) msg.obj;
1391 request = (MainThreadRequest) ar.userObj;
1392 if (ar.exception == null) {
1393 request.result = true;
1394 } else {
1395 request.result = false;
1396 loge("setNetworkSelectionModeManual " + ar.exception);
1397 }
1398 notifyRequester(request);
1399 mApp.onNetworkSelectionChanged(request.subId);
Shishir Agrawal302c8692015-06-19 13:49:39 -07001400 break;
1401
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -07001402 case CMD_GET_MODEM_ACTIVITY_INFO:
1403 request = (MainThreadRequest) msg.obj;
1404 onCompleted = obtainMessage(EVENT_GET_MODEM_ACTIVITY_INFO_DONE, request);
James Mattisab947702019-04-03 14:18:34 -07001405 if (defaultPhone != null) {
1406 defaultPhone.getModemActivityInfo(onCompleted, request.workSource);
Shuo Qian8f4750a2020-02-20 17:12:10 -08001407 } else {
1408 ResultReceiver result = (ResultReceiver) request.argument;
1409 Bundle bundle = new Bundle();
1410 bundle.putParcelable(TelephonyManager.MODEM_ACTIVITY_RESULT_KEY,
Hall Liu49656c02020-10-09 19:00:11 -07001411 new ModemActivityInfo(0, 0, 0,
1412 new int[ModemActivityInfo.getNumTxPowerLevels()], 0));
Shuo Qian8f4750a2020-02-20 17:12:10 -08001413 result.send(0, bundle);
James Mattisab947702019-04-03 14:18:34 -07001414 }
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -07001415 break;
1416
Hall Liud0f208c2020-10-14 16:54:44 -07001417 case EVENT_GET_MODEM_ACTIVITY_INFO_DONE: {
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -07001418 ar = (AsyncResult) msg.obj;
1419 request = (MainThreadRequest) ar.userObj;
Shuo Qian8f4750a2020-02-20 17:12:10 -08001420 ResultReceiver result = (ResultReceiver) request.argument;
Hall Liud0f208c2020-10-14 16:54:44 -07001421 int error = 0;
Kai Shi917fdc62022-11-28 14:01:02 -08001422 ModemActivityInfo ret = null;
Gary Jian3aa9a762022-01-24 16:41:19 +08001423 if (mLastModemActivityInfo == null) {
1424 mLastModemActivitySpecificInfo = new ActivityStatsTechSpecificInfo[1];
1425 mLastModemActivitySpecificInfo[0] =
1426 new ActivityStatsTechSpecificInfo(
1427 0,
1428 0,
1429 new int[ModemActivityInfo.getNumTxPowerLevels()],
1430 0);
1431 mLastModemActivityInfo =
1432 new ModemActivityInfo(0, 0, 0, mLastModemActivitySpecificInfo);
1433 }
1434
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -07001435 if (ar.exception == null && ar.result != null) {
Shuo Qian8f4750a2020-02-20 17:12:10 -08001436 // Update the last modem activity info and the result of the request.
1437 ModemActivityInfo info = (ModemActivityInfo) ar.result;
1438 if (isModemActivityInfoValid(info)) {
Gary Jian3aa9a762022-01-24 16:41:19 +08001439 mergeModemActivityInfo(info);
Shuo Qian8f4750a2020-02-20 17:12:10 -08001440 }
Kai Shi917fdc62022-11-28 14:01:02 -08001441 // This is needed to decouple ret from mLastModemActivityInfo
1442 // We don't want to return mLastModemActivityInfo which is updated
1443 // inside mergeModemActivityInfo()
1444 ret = new ModemActivityInfo(
1445 mLastModemActivityInfo.getTimestampMillis(),
1446 mLastModemActivityInfo.getSleepTimeMillis(),
1447 mLastModemActivityInfo.getIdleTimeMillis(),
1448 deepCopyModemActivitySpecificInfo(mLastModemActivitySpecificInfo));
Gary Jian3aa9a762022-01-24 16:41:19 +08001449
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -07001450 } else {
1451 if (ar.result == null) {
1452 loge("queryModemActivityInfo: Empty response");
Hall Liud0f208c2020-10-14 16:54:44 -07001453 error = TelephonyManager.ModemActivityInfoException
1454 .ERROR_INVALID_INFO_RECEIVED;
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -07001455 } else if (ar.exception instanceof CommandException) {
Gary Jian3aa9a762022-01-24 16:41:19 +08001456 loge("queryModemActivityInfo: CommandException: " + ar.exception);
Hall Liud0f208c2020-10-14 16:54:44 -07001457 error = TelephonyManager.ModemActivityInfoException
1458 .ERROR_MODEM_RESPONSE_ERROR;
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -07001459 } else {
1460 loge("queryModemActivityInfo: Unknown exception");
Hall Liud0f208c2020-10-14 16:54:44 -07001461 error = TelephonyManager.ModemActivityInfoException
1462 .ERROR_UNKNOWN;
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -07001463 }
1464 }
Shuo Qian8f4750a2020-02-20 17:12:10 -08001465 Bundle bundle = new Bundle();
Kai Shi917fdc62022-11-28 14:01:02 -08001466 if (ret != null) {
Gary Jian3aa9a762022-01-24 16:41:19 +08001467 bundle.putParcelable(
1468 TelephonyManager.MODEM_ACTIVITY_RESULT_KEY,
Kai Shi917fdc62022-11-28 14:01:02 -08001469 ret);
Hall Liud0f208c2020-10-14 16:54:44 -07001470 } else {
1471 bundle.putInt(TelephonyManager.EXCEPTION_RESULT_KEY, error);
1472 }
Shuo Qian8f4750a2020-02-20 17:12:10 -08001473 result.send(0, bundle);
Pengquan Menga1bb6272018-09-06 09:59:22 -07001474 notifyRequester(request);
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -07001475 break;
Hall Liud0f208c2020-10-14 16:54:44 -07001476 }
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -07001477
Meng Wang1a7c35a2016-05-05 20:56:15 -07001478 case CMD_SET_ALLOWED_CARRIERS:
1479 request = (MainThreadRequest) msg.obj;
Michele Berionne482f8202018-11-27 18:57:59 -08001480 CarrierRestrictionRules argument =
1481 (CarrierRestrictionRules) request.argument;
Meng Wang1a7c35a2016-05-05 20:56:15 -07001482 onCompleted = obtainMessage(EVENT_SET_ALLOWED_CARRIERS_DONE, request);
Michele Berionne482f8202018-11-27 18:57:59 -08001483 defaultPhone.setAllowedCarriers(argument, onCompleted, request.workSource);
Meng Wang1a7c35a2016-05-05 20:56:15 -07001484 break;
1485
1486 case EVENT_SET_ALLOWED_CARRIERS_DONE:
1487 ar = (AsyncResult) msg.obj;
1488 request = (MainThreadRequest) ar.userObj;
1489 if (ar.exception == null && ar.result != null) {
1490 request.result = ar.result;
1491 } else {
Michele Berionne482f8202018-11-27 18:57:59 -08001492 request.result = TelephonyManager.SET_CARRIER_RESTRICTION_ERROR;
1493 if (ar.exception instanceof CommandException) {
1494 loge("setAllowedCarriers: CommandException: " + ar.exception);
1495 CommandException.Error error =
1496 ((CommandException) (ar.exception)).getCommandError();
1497 if (error == CommandException.Error.REQUEST_NOT_SUPPORTED) {
1498 request.result =
1499 TelephonyManager.SET_CARRIER_RESTRICTION_NOT_SUPPORTED;
1500 }
Meng Wang1a7c35a2016-05-05 20:56:15 -07001501 } else {
1502 loge("setAllowedCarriers: Unknown exception");
1503 }
1504 }
Pengquan Menga1bb6272018-09-06 09:59:22 -07001505 notifyRequester(request);
Meng Wang1a7c35a2016-05-05 20:56:15 -07001506 break;
1507
1508 case CMD_GET_ALLOWED_CARRIERS:
1509 request = (MainThreadRequest) msg.obj;
1510 onCompleted = obtainMessage(EVENT_GET_ALLOWED_CARRIERS_DONE, request);
Nazanin Bakhshif782e562018-12-11 15:15:39 -08001511 defaultPhone.getAllowedCarriers(onCompleted, request.workSource);
Meng Wang1a7c35a2016-05-05 20:56:15 -07001512 break;
1513
1514 case EVENT_GET_ALLOWED_CARRIERS_DONE:
1515 ar = (AsyncResult) msg.obj;
1516 request = (MainThreadRequest) ar.userObj;
1517 if (ar.exception == null && ar.result != null) {
1518 request.result = ar.result;
1519 } else {
Michele Berionne482f8202018-11-27 18:57:59 -08001520 request.result = new IllegalStateException(
1521 "Failed to get carrier restrictions");
Meng Wang1a7c35a2016-05-05 20:56:15 -07001522 if (ar.result == null) {
1523 loge("getAllowedCarriers: Empty response");
1524 } else if (ar.exception instanceof CommandException) {
1525 loge("getAllowedCarriers: CommandException: " +
1526 ar.exception);
1527 } else {
1528 loge("getAllowedCarriers: Unknown exception");
1529 }
1530 }
Pengquan Menga1bb6272018-09-06 09:59:22 -07001531 notifyRequester(request);
Meng Wang1a7c35a2016-05-05 20:56:15 -07001532 break;
1533
Nathan Haroldb3014052017-01-25 15:57:32 -08001534 case EVENT_GET_FORBIDDEN_PLMNS_DONE:
1535 ar = (AsyncResult) msg.obj;
1536 request = (MainThreadRequest) ar.userObj;
1537 if (ar.exception == null && ar.result != null) {
1538 request.result = ar.result;
1539 } else {
1540 request.result = new IllegalArgumentException(
1541 "Failed to retrieve Forbidden Plmns");
1542 if (ar.result == null) {
1543 loge("getForbiddenPlmns: Empty response");
1544 } else {
1545 loge("getForbiddenPlmns: Unknown exception");
1546 }
1547 }
Pengquan Menga1bb6272018-09-06 09:59:22 -07001548 notifyRequester(request);
Nathan Haroldb3014052017-01-25 15:57:32 -08001549 break;
1550
1551 case CMD_GET_FORBIDDEN_PLMNS:
1552 request = (MainThreadRequest) msg.obj;
Muralidhar Reddy472c2ae2021-09-29 19:38:40 +00001553 uiccPort = getUiccPortFromRequest(request);
1554 if (uiccPort == null) {
1555 loge("getForbiddenPlmns() UiccPort is null");
Nathan Haroldb3014052017-01-25 15:57:32 -08001556 request.result = new IllegalArgumentException(
Muralidhar Reddy472c2ae2021-09-29 19:38:40 +00001557 "getForbiddenPlmns() UiccPort is null");
Pengquan Menga1bb6272018-09-06 09:59:22 -07001558 notifyRequester(request);
Nathan Haroldb3014052017-01-25 15:57:32 -08001559 break;
1560 }
1561 Integer appType = (Integer) request.argument;
Muralidhar Reddy472c2ae2021-09-29 19:38:40 +00001562 UiccCardApplication uiccApp = uiccPort.getApplicationByType(appType);
Nathan Haroldb3014052017-01-25 15:57:32 -08001563 if (uiccApp == null) {
1564 loge("getForbiddenPlmns() no app with specified type -- "
1565 + appType);
1566 request.result = new IllegalArgumentException("Failed to get UICC App");
Pengquan Menga1bb6272018-09-06 09:59:22 -07001567 notifyRequester(request);
Nathan Haroldb3014052017-01-25 15:57:32 -08001568 break;
1569 } else {
1570 if (DBG) logv("getForbiddenPlmns() found app " + uiccApp.getAid()
1571 + " specified type -- " + appType);
1572 }
1573 onCompleted = obtainMessage(EVENT_GET_FORBIDDEN_PLMNS_DONE, request);
1574 ((SIMRecords) uiccApp.getIccRecords()).getForbiddenPlmns(
1575 onCompleted);
1576 break;
1577
Holly Jiuyu Sun01c47ad2018-01-24 17:56:33 +00001578 case CMD_SWITCH_SLOTS:
1579 request = (MainThreadRequest) msg.obj;
Muralidhar Reddyeb809e32021-11-19 03:07:54 +00001580 List<UiccSlotMapping> slotMapping = (List<UiccSlotMapping>) request.argument;
Holly Jiuyu Sun01c47ad2018-01-24 17:56:33 +00001581 onCompleted = obtainMessage(EVENT_SWITCH_SLOTS_DONE, request);
Muralidhar Reddyeb809e32021-11-19 03:07:54 +00001582 UiccController.getInstance().switchSlots(slotMapping, onCompleted);
Holly Jiuyu Sun01c47ad2018-01-24 17:56:33 +00001583 break;
1584
1585 case EVENT_SWITCH_SLOTS_DONE:
1586 ar = (AsyncResult) msg.obj;
1587 request = (MainThreadRequest) ar.userObj;
1588 request.result = (ar.exception == null);
Pengquan Menga1bb6272018-09-06 09:59:22 -07001589 notifyRequester(request);
1590 break;
1591 case CMD_GET_NETWORK_SELECTION_MODE:
1592 request = (MainThreadRequest) msg.obj;
1593 onCompleted = obtainMessage(EVENT_GET_NETWORK_SELECTION_MODE_DONE, request);
1594 getPhoneFromRequest(request).getNetworkSelectionMode(onCompleted);
1595 break;
1596
1597 case EVENT_GET_NETWORK_SELECTION_MODE_DONE:
1598 ar = (AsyncResult) msg.obj;
1599 request = (MainThreadRequest) ar.userObj;
1600 if (ar.exception != null) {
1601 request.result = TelephonyManager.NETWORK_SELECTION_MODE_UNKNOWN;
1602 } else {
1603 int mode = ((int[]) ar.result)[0];
1604 if (mode == 0) {
1605 request.result = TelephonyManager.NETWORK_SELECTION_MODE_AUTO;
1606 } else {
1607 request.result = TelephonyManager.NETWORK_SELECTION_MODE_MANUAL;
1608 }
Holly Jiuyu Sun01c47ad2018-01-24 17:56:33 +00001609 }
Pengquan Menga1bb6272018-09-06 09:59:22 -07001610 notifyRequester(request);
1611 break;
1612 case CMD_GET_CDMA_ROAMING_MODE:
1613 request = (MainThreadRequest) msg.obj;
1614 onCompleted = obtainMessage(EVENT_GET_CDMA_ROAMING_MODE_DONE, request);
1615 getPhoneFromRequest(request).queryCdmaRoamingPreference(onCompleted);
1616 break;
1617 case EVENT_GET_CDMA_ROAMING_MODE_DONE:
1618 ar = (AsyncResult) msg.obj;
1619 request = (MainThreadRequest) ar.userObj;
1620 if (ar.exception != null) {
1621 request.result = TelephonyManager.CDMA_ROAMING_MODE_RADIO_DEFAULT;
1622 } else {
1623 request.result = ((int[]) ar.result)[0];
1624 }
1625 notifyRequester(request);
1626 break;
1627 case CMD_SET_CDMA_ROAMING_MODE:
1628 request = (MainThreadRequest) msg.obj;
1629 onCompleted = obtainMessage(EVENT_SET_CDMA_ROAMING_MODE_DONE, request);
1630 int mode = (int) request.argument;
1631 getPhoneFromRequest(request).setCdmaRoamingPreference(mode, onCompleted);
1632 break;
1633 case EVENT_SET_CDMA_ROAMING_MODE_DONE:
1634 ar = (AsyncResult) msg.obj;
1635 request = (MainThreadRequest) ar.userObj;
1636 request.result = ar.exception == null;
1637 notifyRequester(request);
1638 break;
Sarah Chinbaab1432020-10-28 13:46:24 -07001639 case CMD_GET_CDMA_SUBSCRIPTION_MODE:
1640 request = (MainThreadRequest) msg.obj;
1641 onCompleted = obtainMessage(EVENT_GET_CDMA_SUBSCRIPTION_MODE_DONE, request);
1642 getPhoneFromRequest(request).queryCdmaSubscriptionMode(onCompleted);
1643 break;
1644 case EVENT_GET_CDMA_SUBSCRIPTION_MODE_DONE:
1645 ar = (AsyncResult) msg.obj;
1646 request = (MainThreadRequest) ar.userObj;
1647 if (ar.exception != null) {
1648 request.result = TelephonyManager.CDMA_SUBSCRIPTION_RUIM_SIM;
1649 } else {
1650 request.result = ((int[]) ar.result)[0];
1651 }
1652 notifyRequester(request);
1653 break;
Pengquan Menga1bb6272018-09-06 09:59:22 -07001654 case CMD_SET_CDMA_SUBSCRIPTION_MODE:
1655 request = (MainThreadRequest) msg.obj;
1656 onCompleted = obtainMessage(EVENT_SET_CDMA_SUBSCRIPTION_MODE_DONE, request);
1657 int subscriptionMode = (int) request.argument;
Sarah Chinbaab1432020-10-28 13:46:24 -07001658 getPhoneFromRequest(request).setCdmaSubscriptionMode(
1659 subscriptionMode, onCompleted);
Pengquan Menga1bb6272018-09-06 09:59:22 -07001660 break;
1661 case EVENT_SET_CDMA_SUBSCRIPTION_MODE_DONE:
1662 ar = (AsyncResult) msg.obj;
1663 request = (MainThreadRequest) ar.userObj;
1664 request.result = ar.exception == null;
1665 notifyRequester(request);
Holly Jiuyu Sun01c47ad2018-01-24 17:56:33 +00001666 break;
Nathan Harold3ff88932018-08-14 10:19:49 -07001667 case CMD_GET_ALL_CELL_INFO:
1668 request = (MainThreadRequest) msg.obj;
Nathan Harold3ff88932018-08-14 10:19:49 -07001669 onCompleted = obtainMessage(EVENT_GET_ALL_CELL_INFO_DONE, request);
Nathan Harold92bed182018-10-12 18:16:49 -07001670 request.phone.requestCellInfoUpdate(request.workSource, onCompleted);
Nathan Harold3ff88932018-08-14 10:19:49 -07001671 break;
Nathan Harold3ff88932018-08-14 10:19:49 -07001672 case EVENT_GET_ALL_CELL_INFO_DONE:
1673 ar = (AsyncResult) msg.obj;
1674 request = (MainThreadRequest) ar.userObj;
Nathan Harold8d0f1742018-10-02 12:14:47 -07001675 // If a timeout occurs, the response will be null
1676 request.result = (ar.exception == null && ar.result != null)
1677 ? ar.result : new ArrayList<CellInfo>();
Nathan Harold3ff88932018-08-14 10:19:49 -07001678 synchronized (request) {
1679 request.notifyAll();
1680 }
1681 break;
Nathan Haroldfa8da0f2018-09-27 18:51:29 -07001682 case CMD_REQUEST_CELL_INFO_UPDATE:
1683 request = (MainThreadRequest) msg.obj;
1684 request.phone.requestCellInfoUpdate(request.workSource,
1685 obtainMessage(EVENT_REQUEST_CELL_INFO_UPDATE_DONE, request));
1686 break;
1687 case EVENT_REQUEST_CELL_INFO_UPDATE_DONE:
1688 ar = (AsyncResult) msg.obj;
1689 request = (MainThreadRequest) ar.userObj;
1690 ICellInfoCallback cb = (ICellInfoCallback) request.argument;
1691 try {
1692 if (ar.exception != null) {
Nathan Haroldfa8da0f2018-09-27 18:51:29 -07001693 Log.e(LOG_TAG, "Exception retrieving CellInfo=" + ar.exception);
Meng Wangd8921f42019-09-30 17:13:54 -07001694 cb.onError(
1695 TelephonyManager.CellInfoCallback.ERROR_MODEM_ERROR,
1696 ar.exception.getClass().getName(),
1697 ar.exception.toString());
Nathan Haroldfa8da0f2018-09-27 18:51:29 -07001698 } else if (ar.result == null) {
Nathan Haroldfa8da0f2018-09-27 18:51:29 -07001699 Log.w(LOG_TAG, "Timeout Waiting for CellInfo!");
Meng Wangd8921f42019-09-30 17:13:54 -07001700 cb.onError(TelephonyManager.CellInfoCallback.ERROR_TIMEOUT, null, null);
Nathan Haroldfa8da0f2018-09-27 18:51:29 -07001701 } else {
1702 // use the result as returned
1703 cb.onCellInfo((List<CellInfo>) ar.result);
1704 }
1705 } catch (RemoteException re) {
1706 Log.w(LOG_TAG, "Discarded CellInfo due to Callback RemoteException");
1707 }
1708 break;
Sarah Chin679c08a2020-11-18 13:39:35 -08001709 case CMD_GET_CELL_LOCATION: {
Nathan Harold3ff88932018-08-14 10:19:49 -07001710 request = (MainThreadRequest) msg.obj;
1711 WorkSource ws = (WorkSource) request.argument;
1712 Phone phone = getPhoneFromRequest(request);
Meng Wanga10e89e2019-12-09 13:13:01 -08001713 phone.getCellIdentity(ws, obtainMessage(EVENT_GET_CELL_LOCATION_DONE, request));
Nathan Harold3ff88932018-08-14 10:19:49 -07001714 break;
Sarah Chin679c08a2020-11-18 13:39:35 -08001715 }
1716 case EVENT_GET_CELL_LOCATION_DONE: {
Nathan Harold3ff88932018-08-14 10:19:49 -07001717 ar = (AsyncResult) msg.obj;
1718 request = (MainThreadRequest) ar.userObj;
1719 if (ar.exception == null) {
1720 request.result = ar.result;
1721 } else {
Sarah Chin679c08a2020-11-18 13:39:35 -08001722 Phone phone = getPhoneFromRequest(request);
Nathan Harold3ff88932018-08-14 10:19:49 -07001723 request.result = (phone.getPhoneType() == PhoneConstants.PHONE_TYPE_CDMA)
Meng Wanga10e89e2019-12-09 13:13:01 -08001724 ? new CellIdentityCdma() : new CellIdentityGsm();
Nathan Harold3ff88932018-08-14 10:19:49 -07001725 }
1726
1727 synchronized (request) {
1728 request.notifyAll();
1729 }
1730 break;
Sarah Chin679c08a2020-11-18 13:39:35 -08001731 }
chen xu6dac5ab2018-10-26 17:39:23 -07001732 case CMD_MODEM_REBOOT:
1733 request = (MainThreadRequest) msg.obj;
1734 onCompleted = obtainMessage(EVENT_RESET_MODEM_CONFIG_DONE, request);
Nazanin Bakhshif782e562018-12-11 15:15:39 -08001735 defaultPhone.rebootModem(onCompleted);
chen xu6dac5ab2018-10-26 17:39:23 -07001736 break;
chen xu6dac5ab2018-10-26 17:39:23 -07001737 case EVENT_CMD_MODEM_REBOOT_DONE:
1738 handleNullReturnEvent(msg, "rebootModem");
1739 break;
Malcolm Chen8e4ed912019-01-15 20:22:16 -08001740 case CMD_REQUEST_ENABLE_MODEM:
1741 request = (MainThreadRequest) msg.obj;
1742 boolean enable = (boolean) request.argument;
1743 onCompleted = obtainMessage(EVENT_ENABLE_MODEM_DONE, request);
Nazanin Bakhshi33d584b2019-02-27 10:44:32 -08001744 onCompleted.arg1 = enable ? 1 : 0;
Malcolm Chen8e4ed912019-01-15 20:22:16 -08001745 PhoneConfigurationManager.getInstance()
1746 .enablePhone(request.phone, enable, onCompleted);
1747 break;
Michele Berionne5e411512020-11-13 02:36:59 +00001748 case EVENT_ENABLE_MODEM_DONE: {
Malcolm Chen8e4ed912019-01-15 20:22:16 -08001749 ar = (AsyncResult) msg.obj;
1750 request = (MainThreadRequest) ar.userObj;
1751 request.result = (ar.exception == null);
Nazanin Bakhshif71371d2019-04-29 17:29:44 -07001752 int phoneId = request.phone.getPhoneId();
Nazanin Bakhshi33d584b2019-02-27 10:44:32 -08001753 //update the cache as modem status has changed
Nazanin Bakhshif71371d2019-04-29 17:29:44 -07001754 if ((boolean) request.result) {
1755 mPhoneConfigurationManager.addToPhoneStatusCache(phoneId, msg.arg1 == 1);
1756 updateModemStateMetrics();
1757 } else {
1758 Log.e(LOG_TAG, msg.what + " failure. Not updating modem status."
1759 + ar.exception);
1760 }
1761 notifyRequester(request);
1762 break;
Michele Berionne5e411512020-11-13 02:36:59 +00001763 }
Nazanin Bakhshif71371d2019-04-29 17:29:44 -07001764 case CMD_GET_MODEM_STATUS:
1765 request = (MainThreadRequest) msg.obj;
1766 onCompleted = obtainMessage(EVENT_GET_MODEM_STATUS_DONE, request);
1767 PhoneConfigurationManager.getInstance()
1768 .getPhoneStatusFromModem(request.phone, onCompleted);
1769 break;
1770 case EVENT_GET_MODEM_STATUS_DONE:
1771 ar = (AsyncResult) msg.obj;
1772 request = (MainThreadRequest) ar.userObj;
1773 int id = request.phone.getPhoneId();
1774 if (ar.exception == null && ar.result != null) {
1775 request.result = ar.result;
1776 //update the cache as modem status has changed
1777 mPhoneConfigurationManager.addToPhoneStatusCache(id,
1778 (boolean) request.result);
1779 } else {
1780 // Return true if modem status cannot be retrieved. For most cases,
1781 // modem status is on. And for older version modems, GET_MODEM_STATUS
1782 // and disable modem are not supported. Modem is always on.
1783 // TODO: this should be fixed in R to support a third
1784 // status UNKNOWN b/131631629
1785 request.result = true;
1786 Log.e(LOG_TAG, msg.what + " failure. Not updating modem status."
1787 + ar.exception);
1788 }
Malcolm Chen8e4ed912019-01-15 20:22:16 -08001789 notifyRequester(request);
1790 break;
Hall Liu73f5d362020-01-20 13:42:00 -08001791 case CMD_SET_SYSTEM_SELECTION_CHANNELS: {
1792 request = (MainThreadRequest) msg.obj;
1793 onCompleted = obtainMessage(EVENT_SET_SYSTEM_SELECTION_CHANNELS_DONE, request);
1794 Pair<List<RadioAccessSpecifier>, Consumer<Boolean>> args =
1795 (Pair<List<RadioAccessSpecifier>, Consumer<Boolean>>) request.argument;
1796 request.phone.setSystemSelectionChannels(args.first, onCompleted);
1797 break;
1798 }
1799 case EVENT_SET_SYSTEM_SELECTION_CHANNELS_DONE: {
1800 ar = (AsyncResult) msg.obj;
1801 request = (MainThreadRequest) ar.userObj;
1802 Pair<List<RadioAccessSpecifier>, Consumer<Boolean>> args =
1803 (Pair<List<RadioAccessSpecifier>, Consumer<Boolean>>) request.argument;
1804 args.second.accept(ar.exception == null);
1805 notifyRequester(request);
1806 break;
1807 }
Sarah Chin679c08a2020-11-18 13:39:35 -08001808 case CMD_GET_SYSTEM_SELECTION_CHANNELS: {
1809 request = (MainThreadRequest) msg.obj;
1810 onCompleted = obtainMessage(EVENT_GET_SYSTEM_SELECTION_CHANNELS_DONE, request);
1811 Phone phone = getPhoneFromRequest(request);
1812 if (phone != null) {
1813 phone.getSystemSelectionChannels(onCompleted);
1814 } else {
1815 loge("getSystemSelectionChannels: No phone object");
1816 request.result = new ArrayList<RadioAccessSpecifier>();
1817 notifyRequester(request);
1818 }
1819 break;
1820 }
1821 case EVENT_GET_SYSTEM_SELECTION_CHANNELS_DONE:
1822 ar = (AsyncResult) msg.obj;
1823 request = (MainThreadRequest) ar.userObj;
1824 if (ar.exception == null && ar.result != null) {
1825 request.result = ar.result;
1826 } else {
Sarah Chin428d1d62021-03-13 03:17:40 -08001827 request.result = new IllegalStateException(
1828 "Failed to retrieve system selecton channels");
Sarah Chin679c08a2020-11-18 13:39:35 -08001829 if (ar.result == null) {
1830 loge("getSystemSelectionChannels: Empty response");
1831 } else {
1832 loge("getSystemSelectionChannels: Unknown exception");
1833 }
1834 }
1835 notifyRequester(request);
1836 break;
yincheng zhao2737e882019-09-06 17:06:54 -07001837 case EVENT_SET_FORBIDDEN_PLMNS_DONE:
1838 ar = (AsyncResult) msg.obj;
1839 request = (MainThreadRequest) ar.userObj;
1840 if (ar.exception == null && ar.result != null) {
1841 request.result = ar.result;
1842 } else {
1843 request.result = -1;
1844 loge("Failed to set Forbidden Plmns");
1845 if (ar.result == null) {
1846 loge("setForbidenPlmns: Empty response");
1847 } else if (ar.exception != null) {
1848 loge("setForbiddenPlmns: Exception: " + ar.exception);
1849 request.result = -1;
1850 } else {
1851 loge("setForbiddenPlmns: Unknown exception");
1852 }
1853 }
1854 notifyRequester(request);
1855 break;
1856 case CMD_SET_FORBIDDEN_PLMNS:
1857 request = (MainThreadRequest) msg.obj;
Muralidhar Reddy472c2ae2021-09-29 19:38:40 +00001858 uiccPort = getUiccPortFromRequest(request);
1859 if (uiccPort == null) {
1860 loge("setForbiddenPlmns: UiccPort is null");
yincheng zhao2737e882019-09-06 17:06:54 -07001861 request.result = -1;
1862 notifyRequester(request);
1863 break;
1864 }
1865 Pair<Integer, List<String>> setFplmnsArgs =
1866 (Pair<Integer, List<String>>) request.argument;
1867 appType = setFplmnsArgs.first;
1868 List<String> fplmns = setFplmnsArgs.second;
Muralidhar Reddy472c2ae2021-09-29 19:38:40 +00001869 uiccApp = uiccPort.getApplicationByType(appType);
yincheng zhao2737e882019-09-06 17:06:54 -07001870 if (uiccApp == null) {
1871 loge("setForbiddenPlmns: no app with specified type -- " + appType);
1872 request.result = -1;
1873 loge("Failed to get UICC App");
1874 notifyRequester(request);
1875 } else {
1876 onCompleted = obtainMessage(EVENT_SET_FORBIDDEN_PLMNS_DONE, request);
1877 ((SIMRecords) uiccApp.getIccRecords())
1878 .setForbiddenPlmns(onCompleted, fplmns);
1879 }
yinchengzhao4d163c02019-12-12 15:21:47 -08001880 break;
Naina Nallurid63128d2019-09-17 14:10:30 -07001881 case CMD_ERASE_MODEM_CONFIG:
1882 request = (MainThreadRequest) msg.obj;
1883 onCompleted = obtainMessage(EVENT_ERASE_MODEM_CONFIG_DONE, request);
1884 defaultPhone.eraseModemConfig(onCompleted);
1885 break;
1886 case EVENT_ERASE_MODEM_CONFIG_DONE:
1887 handleNullReturnEvent(msg, "eraseModemConfig");
yincheng zhao2737e882019-09-06 17:06:54 -07001888 break;
zoey chene02881a2019-12-30 16:11:23 +08001889
Kai Shif70f46f2021-03-03 13:59:46 -08001890 case CMD_ERASE_DATA_SHARED_PREFERENCES:
1891 request = (MainThreadRequest) msg.obj;
1892 request.result = defaultPhone.eraseDataInSharedPreferences();
1893 notifyRequester(request);
1894 break;
1895
zoey chene02881a2019-12-30 16:11:23 +08001896 case CMD_CHANGE_ICC_LOCK_PASSWORD:
1897 request = (MainThreadRequest) msg.obj;
1898 onCompleted = obtainMessage(EVENT_CHANGE_ICC_LOCK_PASSWORD_DONE, request);
1899 Pair<String, String> changed = (Pair<String, String>) request.argument;
1900 getPhoneFromRequest(request).getIccCard().changeIccLockPassword(
1901 changed.first, changed.second, onCompleted);
1902 break;
1903 case EVENT_CHANGE_ICC_LOCK_PASSWORD_DONE:
1904 ar = (AsyncResult) msg.obj;
1905 request = (MainThreadRequest) ar.userObj;
1906 if (ar.exception == null) {
1907 request.result = TelephonyManager.CHANGE_ICC_LOCK_SUCCESS;
Michele Berionne5e411512020-11-13 02:36:59 +00001908 // If the operation is successful, update the PIN storage
1909 Pair<String, String> passwords = (Pair<String, String>) request.argument;
1910 int phoneId = getPhoneFromRequest(request).getPhoneId();
Jon Spivack9c3bc762021-10-06 20:53:09 +00001911 UiccController.getInstance().getPinStorage()
1912 .storePin(passwords.second, phoneId);
zoey chene02881a2019-12-30 16:11:23 +08001913 } else {
1914 request.result = msg.arg1;
1915 }
1916 notifyRequester(request);
1917 break;
1918
Michele Berionne5e411512020-11-13 02:36:59 +00001919 case CMD_SET_ICC_LOCK_ENABLED: {
zoey chene02881a2019-12-30 16:11:23 +08001920 request = (MainThreadRequest) msg.obj;
1921 onCompleted = obtainMessage(EVENT_SET_ICC_LOCK_ENABLED_DONE, request);
1922 Pair<Boolean, String> enabled = (Pair<Boolean, String>) request.argument;
1923 getPhoneFromRequest(request).getIccCard().setIccLockEnabled(
1924 enabled.first, enabled.second, onCompleted);
1925 break;
Michele Berionne5e411512020-11-13 02:36:59 +00001926 }
zoey chene02881a2019-12-30 16:11:23 +08001927 case EVENT_SET_ICC_LOCK_ENABLED_DONE:
1928 ar = (AsyncResult) msg.obj;
1929 request = (MainThreadRequest) ar.userObj;
1930 if (ar.exception == null) {
1931 request.result = TelephonyManager.CHANGE_ICC_LOCK_SUCCESS;
Michele Berionne5e411512020-11-13 02:36:59 +00001932 // If the operation is successful, update the PIN storage
1933 Pair<Boolean, String> enabled = (Pair<Boolean, String>) request.argument;
1934 int phoneId = getPhoneFromRequest(request).getPhoneId();
1935 if (enabled.first) {
Jon Spivack9c3bc762021-10-06 20:53:09 +00001936 UiccController.getInstance().getPinStorage()
1937 .storePin(enabled.second, phoneId);
Michele Berionne5e411512020-11-13 02:36:59 +00001938 } else {
1939 UiccController.getInstance().getPinStorage().clearPin(phoneId);
1940 }
zoey chene02881a2019-12-30 16:11:23 +08001941 } else {
1942 request.result = msg.arg1;
1943 }
Michele Berionne5e411512020-11-13 02:36:59 +00001944
1945
zoey chene02881a2019-12-30 16:11:23 +08001946 notifyRequester(request);
1947 break;
1948
Peter Wangdafb9ac2020-01-15 14:13:38 -08001949 case MSG_NOTIFY_USER_ACTIVITY:
1950 removeMessages(MSG_NOTIFY_USER_ACTIVITY);
Peter Wang59571be2020-01-27 12:35:15 +08001951 Intent intent = new Intent(TelephonyIntents.ACTION_USER_ACTIVITY_NOTIFICATION);
Peter Wangdafb9ac2020-01-15 14:13:38 -08001952 intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY);
1953 getDefaultPhone().getContext().sendBroadcastAsUser(
1954 intent, UserHandle.ALL, permission.USER_ACTIVITY);
1955 break;
Jack Nudelmanb0b87642020-11-12 15:04:39 -08001956
1957 case CMD_SET_DATA_THROTTLING: {
1958 request = (MainThreadRequest) msg.obj;
1959 onCompleted = obtainMessage(EVENT_SET_DATA_THROTTLING_DONE, request);
1960 DataThrottlingRequest dataThrottlingRequest =
1961 (DataThrottlingRequest) request.argument;
1962 Phone phone = getPhoneFromRequest(request);
1963 if (phone != null) {
1964 phone.setDataThrottling(onCompleted,
1965 request.workSource, dataThrottlingRequest.getDataThrottlingAction(),
1966 dataThrottlingRequest.getCompletionDurationMillis());
1967 } else {
1968 loge("setDataThrottling: No phone object");
1969 request.result =
1970 TelephonyManager.THERMAL_MITIGATION_RESULT_MODEM_NOT_AVAILABLE;
1971 notifyRequester(request);
1972 }
1973
1974 break;
1975 }
1976 case EVENT_SET_DATA_THROTTLING_DONE:
1977 ar = (AsyncResult) msg.obj;
1978 request = (MainThreadRequest) ar.userObj;
1979
1980 if (ar.exception == null) {
1981 request.result = TelephonyManager.THERMAL_MITIGATION_RESULT_SUCCESS;
1982 } else if (ar.exception instanceof CommandException) {
1983 loge("setDataThrottling: CommandException: " + ar.exception);
1984 CommandException.Error error =
1985 ((CommandException) (ar.exception)).getCommandError();
1986
1987 if (error == CommandException.Error.RADIO_NOT_AVAILABLE) {
1988 request.result = TelephonyManager
1989 .THERMAL_MITIGATION_RESULT_MODEM_NOT_AVAILABLE;
1990 } else if (error == CommandException.Error.INVALID_ARGUMENTS) {
1991 request.result = SET_DATA_THROTTLING_MODEM_THREW_INVALID_PARAMS;
Jack Nudelman5d6a98b2021-03-04 14:26:25 -08001992 } else if (error == CommandException.Error.REQUEST_NOT_SUPPORTED) {
1993 request.result = MODEM_DOES_NOT_SUPPORT_DATA_THROTTLING_ERROR_CODE;
Jack Nudelmanb0b87642020-11-12 15:04:39 -08001994 } else {
1995 request.result =
1996 TelephonyManager.THERMAL_MITIGATION_RESULT_MODEM_ERROR;
1997 }
1998 } else {
1999 request.result = TelephonyManager.THERMAL_MITIGATION_RESULT_MODEM_ERROR;
2000 }
2001 Log.w(LOG_TAG, "DataThrottlingResult = " + request.result);
2002 notifyRequester(request);
2003 break;
Jordan Liu109698e2020-11-24 14:50:34 -08002004
2005 case CMD_SET_SIM_POWER: {
2006 request = (MainThreadRequest) msg.obj;
2007 onCompleted = obtainMessage(EVENT_SET_SIM_POWER_DONE, request);
2008 request = (MainThreadRequest) msg.obj;
2009 int stateToSet =
2010 ((Pair<Integer, IIntegerConsumer>)
2011 request.argument).first;
2012 request.phone.setSimPowerState(stateToSet, onCompleted, request.workSource);
2013 break;
2014 }
2015 case EVENT_SET_SIM_POWER_DONE: {
2016 ar = (AsyncResult) msg.obj;
2017 request = (MainThreadRequest) ar.userObj;
2018 IIntegerConsumer callback =
2019 ((Pair<Integer, IIntegerConsumer>) request.argument).second;
2020 if (ar.exception != null) {
2021 loge("setSimPower exception: " + ar.exception);
2022 int errorCode = TelephonyManager.CallForwardingInfoCallback
2023 .RESULT_ERROR_UNKNOWN;
2024 if (ar.exception instanceof CommandException) {
2025 CommandException.Error error =
2026 ((CommandException) (ar.exception)).getCommandError();
2027 if (error == CommandException.Error.SIM_ERR) {
2028 errorCode = TelephonyManager.SET_SIM_POWER_STATE_SIM_ERROR;
2029 } else if (error == CommandException.Error.INVALID_ARGUMENTS) {
2030 errorCode = TelephonyManager.SET_SIM_POWER_STATE_ALREADY_IN_STATE;
2031 } else if (error == CommandException.Error.REQUEST_NOT_SUPPORTED) {
2032 errorCode = TelephonyManager.SET_SIM_POWER_STATE_NOT_SUPPORTED;
2033 } else {
2034 errorCode = TelephonyManager.SET_SIM_POWER_STATE_MODEM_ERROR;
2035 }
2036 }
2037 try {
2038 callback.accept(errorCode);
2039 } catch (RemoteException e) {
2040 // Ignore if the remote process is no longer available to call back.
2041 Log.w(LOG_TAG, "setSimPower: callback not available.");
2042 }
2043 } else {
2044 try {
2045 callback.accept(TelephonyManager.SET_SIM_POWER_STATE_SUCCESS);
2046 } catch (RemoteException e) {
2047 // Ignore if the remote process is no longer available to call back.
2048 Log.w(LOG_TAG, "setSimPower: callback not available.");
2049 }
2050 }
2051 break;
2052 }
Rambo Wanga5cc9b72021-01-07 10:51:54 -08002053 case CMD_SET_SIGNAL_STRENGTH_UPDATE_REQUEST: {
2054 request = (MainThreadRequest) msg.obj;
2055
2056 final Phone phone = getPhoneFromRequest(request);
2057 if (phone == null || phone.getServiceStateTracker() == null) {
2058 request.result = new IllegalStateException("Phone or SST is null");
2059 notifyRequester(request);
2060 break;
2061 }
2062
2063 Pair<Integer, SignalStrengthUpdateRequest> pair =
2064 (Pair<Integer, SignalStrengthUpdateRequest>) request.argument;
2065 onCompleted = obtainMessage(EVENT_SET_SIGNAL_STRENGTH_UPDATE_REQUEST_DONE,
2066 request);
Rambo Wang6568f172021-02-03 16:56:47 -08002067 phone.getSignalStrengthController().setSignalStrengthUpdateRequest(
Rambo Wanga5cc9b72021-01-07 10:51:54 -08002068 request.subId, pair.first /*callingUid*/,
2069 pair.second /*request*/, onCompleted);
2070 break;
2071 }
2072 case EVENT_SET_SIGNAL_STRENGTH_UPDATE_REQUEST_DONE: {
2073 ar = (AsyncResult) msg.obj;
2074 request = (MainThreadRequest) ar.userObj;
2075 // request.result will be the exception of ar if present, true otherwise.
2076 // Be cautious not to leave result null which will wait() forever
2077 request.result = ar.exception != null ? ar.exception : true;
2078 notifyRequester(request);
2079 break;
2080 }
2081 case CMD_CLEAR_SIGNAL_STRENGTH_UPDATE_REQUEST: {
2082 request = (MainThreadRequest) msg.obj;
2083
2084 Phone phone = getPhoneFromRequest(request);
2085 if (phone == null || phone.getServiceStateTracker() == null) {
2086 request.result = new IllegalStateException("Phone or SST is null");
2087 notifyRequester(request);
2088 break;
2089 }
2090
2091 Pair<Integer, SignalStrengthUpdateRequest> pair =
2092 (Pair<Integer, SignalStrengthUpdateRequest>) request.argument;
2093 onCompleted = obtainMessage(EVENT_CLEAR_SIGNAL_STRENGTH_UPDATE_REQUEST_DONE,
2094 request);
Rambo Wang6568f172021-02-03 16:56:47 -08002095 phone.getSignalStrengthController().clearSignalStrengthUpdateRequest(
Rambo Wanga5cc9b72021-01-07 10:51:54 -08002096 request.subId, pair.first /*callingUid*/,
2097 pair.second /*request*/, onCompleted);
2098 break;
2099 }
2100 case EVENT_CLEAR_SIGNAL_STRENGTH_UPDATE_REQUEST_DONE: {
2101 ar = (AsyncResult) msg.obj;
2102 request = (MainThreadRequest) ar.userObj;
2103 request.result = ar.exception != null ? ar.exception : true;
2104 notifyRequester(request);
2105 break;
2106 }
Jordan Liu109698e2020-11-24 14:50:34 -08002107
Hongbo Zeng156aa4a2021-02-08 21:50:28 +08002108 case CMD_GET_SLICING_CONFIG: {
2109 request = (MainThreadRequest) msg.obj;
2110 onCompleted = obtainMessage(EVENT_GET_SLICING_CONFIG_DONE, request);
2111 request.phone.getSlicingConfig(onCompleted);
2112 break;
2113 }
2114 case EVENT_GET_SLICING_CONFIG_DONE: {
2115 ar = (AsyncResult) msg.obj;
2116 request = (MainThreadRequest) ar.userObj;
2117 ResultReceiver result = (ResultReceiver) request.argument;
2118
Hongbo Zeng0e18b162021-04-07 16:52:18 +08002119 NetworkSlicingConfig slicingConfig = null;
Hongbo Zeng156aa4a2021-02-08 21:50:28 +08002120 Bundle bundle = new Bundle();
2121 int resultCode = 0;
2122 if (ar.exception != null) {
2123 Log.e(LOG_TAG, "Exception retrieving slicing configuration="
2124 + ar.exception);
Hongbo Zeng0e18b162021-04-07 16:52:18 +08002125 resultCode = TelephonyManager.NetworkSlicingException.ERROR_MODEM_ERROR;
Hongbo Zeng156aa4a2021-02-08 21:50:28 +08002126 } else if (ar.result == null) {
2127 Log.w(LOG_TAG, "Timeout Waiting for slicing configuration!");
Hongbo Zeng0e18b162021-04-07 16:52:18 +08002128 resultCode = TelephonyManager.NetworkSlicingException.ERROR_TIMEOUT;
Hongbo Zeng156aa4a2021-02-08 21:50:28 +08002129 } else {
2130 // use the result as returned
Hongbo Zeng0e18b162021-04-07 16:52:18 +08002131 resultCode = TelephonyManager.NetworkSlicingException.SUCCESS;
2132 slicingConfig = (NetworkSlicingConfig) ar.result;
Hongbo Zeng156aa4a2021-02-08 21:50:28 +08002133 }
2134
2135 if (slicingConfig == null) {
Hongbo Zeng0e18b162021-04-07 16:52:18 +08002136 slicingConfig = new NetworkSlicingConfig();
Hongbo Zeng156aa4a2021-02-08 21:50:28 +08002137 }
2138 bundle.putParcelable(TelephonyManager.KEY_SLICING_CONFIG_HANDLE, slicingConfig);
2139 result.send(resultCode, bundle);
2140 notifyRequester(request);
2141 break;
2142 }
2143
Michele Berionne5e411512020-11-13 02:36:59 +00002144 case CMD_PREPARE_UNATTENDED_REBOOT:
2145 request = (MainThreadRequest) msg.obj;
2146 request.result =
Rafael Higuera Silvad9630642021-09-20 15:32:01 +00002147 UiccController.getInstance().getPinStorage()
2148 .prepareUnattendedReboot(request.workSource);
Michele Berionne5e411512020-11-13 02:36:59 +00002149 notifyRequester(request);
2150 break;
2151
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002152 default:
2153 Log.w(LOG_TAG, "MainThreadHandler: unexpected message code: " + msg.what);
2154 break;
2155 }
2156 }
Jake Hambye994d462014-02-03 13:10:13 -08002157
Pengquan Menga1bb6272018-09-06 09:59:22 -07002158 private void notifyRequester(MainThreadRequest request) {
2159 synchronized (request) {
2160 request.notifyAll();
2161 }
2162 }
2163
Jake Hambye994d462014-02-03 13:10:13 -08002164 private void handleNullReturnEvent(Message msg, String command) {
2165 AsyncResult ar = (AsyncResult) msg.obj;
2166 MainThreadRequest request = (MainThreadRequest) ar.userObj;
2167 if (ar.exception == null) {
2168 request.result = true;
2169 } else {
2170 request.result = false;
2171 if (ar.exception instanceof CommandException) {
2172 loge(command + ": CommandException: " + ar.exception);
2173 } else {
2174 loge(command + ": Unknown exception");
2175 }
2176 }
Pengquan Menga1bb6272018-09-06 09:59:22 -07002177 notifyRequester(request);
Jake Hambye994d462014-02-03 13:10:13 -08002178 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002179 }
2180
2181 /**
2182 * Posts the specified command to be executed on the main thread,
2183 * waits for the request to complete, and returns the result.
2184 * @see #sendRequestAsync
2185 */
2186 private Object sendRequest(int command, Object argument) {
Rambo Wang0f050d82021-02-12 11:43:36 -08002187 return sendRequest(command, argument, SubscriptionManager.INVALID_SUBSCRIPTION_ID, null,
2188 null, -1 /*timeoutInMs*/);
vagdeviaf9a5b92018-08-15 16:01:53 -07002189 }
2190
2191 /**
2192 * Posts the specified command to be executed on the main thread,
2193 * waits for the request to complete, and returns the result.
2194 * @see #sendRequestAsync
2195 */
2196 private Object sendRequest(int command, Object argument, WorkSource workSource) {
2197 return sendRequest(command, argument, SubscriptionManager.INVALID_SUBSCRIPTION_ID,
Rambo Wang0f050d82021-02-12 11:43:36 -08002198 null, workSource, -1 /*timeoutInMs*/);
Wink Saville36469e72014-06-11 15:17:00 -07002199 }
2200
2201 /**
2202 * Posts the specified command to be executed on the main thread,
2203 * waits for the request to complete, and returns the result.
2204 * @see #sendRequestAsync
2205 */
Shishir Agrawal76d5da92014-11-09 16:17:25 -08002206 private Object sendRequest(int command, Object argument, Integer subId) {
Rambo Wang0f050d82021-02-12 11:43:36 -08002207 return sendRequest(command, argument, subId, null, null, -1 /*timeoutInMs*/);
2208 }
2209
2210 /**
2211 * Posts the specified command to be executed on the main thread,
2212 * waits for the request to complete for at most {@code timeoutInMs}, and returns the result
2213 * if not timeout or null otherwise.
2214 * @see #sendRequestAsync
2215 */
2216 private @Nullable Object sendRequest(int command, Object argument, Integer subId,
2217 long timeoutInMs) {
2218 return sendRequest(command, argument, subId, null, null, timeoutInMs);
vagdeviaf9a5b92018-08-15 16:01:53 -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 */
Nathan Harold92bed182018-10-12 18:16:49 -07002226 private Object sendRequest(int command, Object argument, int subId, WorkSource workSource) {
Rambo Wang0f050d82021-02-12 11:43:36 -08002227 return sendRequest(command, argument, subId, null, workSource, -1 /*timeoutInMs*/);
Nathan Harold92bed182018-10-12 18:16:49 -07002228 }
2229
2230 /**
2231 * Posts the specified command to be executed on the main thread,
2232 * waits for the request to complete, and returns the result.
2233 * @see #sendRequestAsync
2234 */
2235 private Object sendRequest(int command, Object argument, Phone phone, WorkSource workSource) {
Rambo Wang0f050d82021-02-12 11:43:36 -08002236 return sendRequest(command, argument, SubscriptionManager.INVALID_SUBSCRIPTION_ID, phone,
2237 workSource, -1 /*timeoutInMs*/);
Nathan Harold92bed182018-10-12 18:16:49 -07002238 }
2239
2240 /**
Rambo Wang0f050d82021-02-12 11:43:36 -08002241 * Posts the specified command to be executed on the main thread. If {@code timeoutInMs} is
2242 * negative, waits for the request to complete, and returns the result. Otherwise, wait for
2243 * maximum of {@code timeoutInMs} milliseconds, interrupt and return null.
Nathan Harold92bed182018-10-12 18:16:49 -07002244 * @see #sendRequestAsync
2245 */
Rambo Wang0f050d82021-02-12 11:43:36 -08002246 private @Nullable Object sendRequest(int command, Object argument, Integer subId, Phone phone,
2247 WorkSource workSource, long timeoutInMs) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002248 if (Looper.myLooper() == mMainThreadHandler.getLooper()) {
2249 throw new RuntimeException("This method will deadlock if called from the main thread.");
2250 }
2251
Nathan Harold92bed182018-10-12 18:16:49 -07002252 MainThreadRequest request = null;
2253 if (subId != SubscriptionManager.INVALID_SUBSCRIPTION_ID && phone != null) {
2254 throw new IllegalArgumentException("subId and phone cannot both be specified!");
2255 } else if (phone != null) {
2256 request = new MainThreadRequest(argument, phone, workSource);
2257 } else {
2258 request = new MainThreadRequest(argument, subId, workSource);
2259 }
2260
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002261 Message msg = mMainThreadHandler.obtainMessage(command, request);
2262 msg.sendToTarget();
2263
Rambo Wang0f050d82021-02-12 11:43:36 -08002264
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002265 synchronized (request) {
Rambo Wang0f050d82021-02-12 11:43:36 -08002266 if (timeoutInMs >= 0) {
2267 // Wait for at least timeoutInMs before returning null request result
2268 long now = SystemClock.elapsedRealtime();
2269 long deadline = now + timeoutInMs;
Grace Jia8a0a1e82021-05-23 22:59:52 -07002270 while (request.result == null && now < deadline) {
Rambo Wang0f050d82021-02-12 11:43:36 -08002271 try {
2272 request.wait(deadline - now);
2273 } catch (InterruptedException e) {
2274 // Do nothing, go back and check if request is completed or timeout
2275 } finally {
2276 now = SystemClock.elapsedRealtime();
2277 }
2278 }
2279 } else {
2280 // Wait for the request to complete
2281 while (request.result == null) {
2282 try {
2283 request.wait();
2284 } catch (InterruptedException e) {
2285 // Do nothing, go back and wait until the request is complete
2286 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002287 }
2288 }
2289 }
Rambo Wang0f050d82021-02-12 11:43:36 -08002290 if (request.result == null) {
2291 Log.wtf(LOG_TAG,
2292 "sendRequest: Blocking command timed out. Something has gone terribly wrong.");
2293 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002294 return request.result;
2295 }
2296
2297 /**
2298 * Asynchronous ("fire and forget") version of sendRequest():
2299 * Posts the specified command to be executed on the main thread, and
2300 * returns immediately.
2301 * @see #sendRequest
2302 */
2303 private void sendRequestAsync(int command) {
2304 mMainThreadHandler.sendEmptyMessage(command);
2305 }
2306
2307 /**
Sailesh Nepalbd76e4e2013-10-27 13:59:44 -07002308 * Same as {@link #sendRequestAsync(int)} except it takes an argument.
Nathan Haroldfa8da0f2018-09-27 18:51:29 -07002309 * @see {@link #sendRequest(int)}
Sailesh Nepalbd76e4e2013-10-27 13:59:44 -07002310 */
2311 private void sendRequestAsync(int command, Object argument) {
Nathan Haroldfa8da0f2018-09-27 18:51:29 -07002312 sendRequestAsync(command, argument, null, null);
2313 }
2314
2315 /**
2316 * Same as {@link #sendRequestAsync(int,Object)} except it takes a Phone and WorkSource.
2317 * @see {@link #sendRequest(int,Object)}
2318 */
2319 private void sendRequestAsync(
2320 int command, Object argument, Phone phone, WorkSource workSource) {
2321 MainThreadRequest request = new MainThreadRequest(argument, phone, workSource);
Sailesh Nepalbd76e4e2013-10-27 13:59:44 -07002322 Message msg = mMainThreadHandler.obtainMessage(command, request);
2323 msg.sendToTarget();
2324 }
2325
2326 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002327 * Initialize the singleton PhoneInterfaceManager instance.
2328 * This is only done once, at startup, from PhoneApp.onCreate().
2329 */
Nazanin Bakhshif782e562018-12-11 15:15:39 -08002330 /* package */ static PhoneInterfaceManager init(PhoneGlobals app) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002331 synchronized (PhoneInterfaceManager.class) {
2332 if (sInstance == null) {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08002333 sInstance = new PhoneInterfaceManager(app);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002334 } else {
2335 Log.wtf(LOG_TAG, "init() called multiple times! sInstance = " + sInstance);
2336 }
2337 return sInstance;
2338 }
2339 }
2340
2341 /** Private constructor; @see init() */
Jordan Liu1979a042020-03-20 21:39:35 +00002342 private PhoneInterfaceManager(PhoneGlobals app) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002343 mApp = app;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002344 mCM = PhoneGlobals.getInstance().mCM;
Brad Ebingerd1947d82021-05-17 20:54:49 +00002345 mImsResolver = ImsResolver.getInstance();
Stuart Scott981d8582015-04-21 14:09:50 -07002346 mUserManager = (UserManager) app.getSystemService(Context.USER_SERVICE);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002347 mAppOps = (AppOpsManager)app.getSystemService(Context.APP_OPS_SERVICE);
Grace Jia0ddb3612021-04-22 13:35:26 -07002348 mPm = app.getSystemService(PackageManager.class);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002349 mMainThreadHandler = new MainThreadHandler();
Tobias Thiererb19e1f12018-12-11 17:54:03 +00002350 mSubscriptionController = SubscriptionController.getInstance();
Nazanin Bakhshif782e562018-12-11 15:15:39 -08002351 mTelephonySharedPreferences =
2352 PreferenceManager.getDefaultSharedPreferences(mApp);
yinxub1bed742017-04-17 11:45:04 -07002353 mNetworkScanRequestTracker = new NetworkScanRequestTracker();
Malcolm Chen2c63d402018-08-14 16:00:53 -07002354 mPhoneConfigurationManager = PhoneConfigurationManager.getInstance();
Daniel Bright94f43662021-03-01 14:43:40 -08002355 mRadioInterfaceCapabilities = RadioInterfaceCapabilityController.getInstance();
Peter Wanga3cf4ac2020-01-27 09:39:46 +08002356 mNotifyUserActivity = new AtomicBoolean(false);
Tyler Gunn64144d92022-03-17 14:16:41 -07002357 PropertyInvalidatedCache.invalidateCache(TelephonyManager.CACHE_KEY_PHONE_ACCOUNT_TO_SUBID);
Gil Cukiermand6cae882022-12-29 12:02:31 -05002358 mTelephony2gUpdater = new Telephony2gUpdater(mApp);
Gil Cukierman6dac5eb2022-09-19 16:09:04 +00002359 mTelephony2gUpdater.init();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002360 publish();
2361 }
2362
Nazanin Bakhshif782e562018-12-11 15:15:39 -08002363 private Phone getDefaultPhone() {
2364 Phone thePhone = getPhone(getDefaultSubscription());
2365 return (thePhone != null) ? thePhone : PhoneFactory.getDefaultPhone();
2366 }
2367
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002368 private void publish() {
2369 if (DBG) log("publish: " + this);
2370
Peter Wangc035ce42020-01-08 21:00:22 -08002371 TelephonyFrameworkInitializer
2372 .getTelephonyServiceManager()
2373 .getTelephonyServiceRegisterer()
2374 .register(this);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002375 }
2376
Stuart Scott584921c2015-01-15 17:10:34 -08002377 private Phone getPhoneFromRequest(MainThreadRequest request) {
Jordan Liu4c733742019-02-28 12:03:40 -08002378 if (request.phone != null) {
2379 return request.phone;
2380 } else {
2381 return getPhoneFromSubId(request.subId);
2382 }
2383 }
2384
2385 private Phone getPhoneFromSubId(int subId) {
2386 return (subId == SubscriptionManager.INVALID_SUBSCRIPTION_ID)
2387 ? getDefaultPhone() : getPhone(subId);
Stuart Scott584921c2015-01-15 17:10:34 -08002388 }
2389
Rambo Wange53e07d2022-05-10 13:01:13 -07002390 @Nullable
2391 private UiccPort getUiccPortFromRequest(@NonNull MainThreadRequest request) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002392 Phone phone = getPhoneFromRequest(request);
2393 return phone == null ? null :
Muralidhar Reddy472c2ae2021-09-29 19:38:40 +00002394 UiccController.getInstance().getUiccPort(phone.getPhoneId());
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002395 }
2396
Wink Saville36469e72014-06-11 15:17:00 -07002397 // returns phone associated with the subId.
Wink Savilleb564aae2014-10-23 10:18:09 -07002398 private Phone getPhone(int subId) {
Jack Yudf049d72022-12-02 22:48:35 -08002399 return PhoneFactory.getPhone(SubscriptionManager.getPhoneId(subId));
Wink Saville36469e72014-06-11 15:17:00 -07002400 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002401
Kai Shif70f46f2021-03-03 13:59:46 -08002402 private void sendEraseModemConfig(@NonNull Phone phone) {
2403 Boolean success = (Boolean) sendRequest(CMD_ERASE_MODEM_CONFIG, null);
2404 if (DBG) log("eraseModemConfig:" + ' ' + (success ? "ok" : "fail"));
2405 }
2406
2407 private void sendEraseDataInSharedPreferences(@NonNull Phone phone) {
2408 Boolean success = (Boolean) sendRequest(CMD_ERASE_DATA_SHARED_PREFERENCES, null);
2409 if (DBG) log("eraseDataInSharedPreferences:" + ' ' + (success ? "ok" : "fail"));
Naina Nallurid63128d2019-09-17 14:10:30 -07002410 }
2411
Peter Wang44b186e2020-01-13 23:33:09 -08002412 private boolean isImsAvailableOnDevice() {
2413 PackageManager pm = getDefaultPhone().getContext().getPackageManager();
2414 if (pm == null) {
2415 // For some reason package manger is not available.. This will fail internally anyway,
2416 // so do not throw error and allow.
2417 return true;
2418 }
2419 return pm.hasSystemFeature(PackageManager.FEATURE_TELEPHONY_IMS, 0);
2420 }
2421
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002422 public void dial(String number) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07002423 dialForSubscriber(getPreferredVoiceSubscription(), number);
Wink Saville36469e72014-06-11 15:17:00 -07002424 }
2425
Wink Savilleb564aae2014-10-23 10:18:09 -07002426 public void dialForSubscriber(int subId, String number) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002427 if (DBG) log("dial: " + number);
2428 // No permission check needed here: This is just a wrapper around the
2429 // ACTION_DIAL intent, which is available to any app since it puts up
2430 // the UI before it does anything.
2431
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002432 final long identity = Binder.clearCallingIdentity();
2433 try {
2434 String url = createTelUrl(number);
2435 if (url == null) {
2436 return;
2437 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002438
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002439 // PENDING: should we just silently fail if phone is offhook or ringing?
2440 PhoneConstants.State state = mCM.getState(subId);
2441 if (state != PhoneConstants.State.OFFHOOK && state != PhoneConstants.State.RINGING) {
2442 Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse(url));
2443 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
2444 mApp.startActivity(intent);
2445 }
2446 } finally {
2447 Binder.restoreCallingIdentity(identity);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002448 }
2449 }
2450
2451 public void call(String callingPackage, String number) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07002452 callForSubscriber(getPreferredVoiceSubscription(), callingPackage, number);
Wink Saville36469e72014-06-11 15:17:00 -07002453 }
2454
Wink Savilleb564aae2014-10-23 10:18:09 -07002455 public void callForSubscriber(int subId, String callingPackage, String number) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002456 if (DBG) log("call: " + number);
2457
2458 // This is just a wrapper around the ACTION_CALL intent, but we still
2459 // need to do a permission check since we're calling startActivity()
2460 // from the context of the phone app.
2461 enforceCallPermission();
2462
Jordan Liu1617b712019-07-10 15:06:26 -07002463 if (mAppOps.noteOp(AppOpsManager.OPSTR_CALL_PHONE, Binder.getCallingUid(), callingPackage)
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002464 != AppOpsManager.MODE_ALLOWED) {
2465 return;
2466 }
2467
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002468 final long identity = Binder.clearCallingIdentity();
2469 try {
2470 String url = createTelUrl(number);
2471 if (url == null) {
2472 return;
2473 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002474
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002475 boolean isValid = false;
2476 final List<SubscriptionInfo> slist = getActiveSubscriptionInfoListPrivileged();
2477 if (slist != null) {
2478 for (SubscriptionInfo subInfoRecord : slist) {
2479 if (subInfoRecord.getSubscriptionId() == subId) {
2480 isValid = true;
2481 break;
2482 }
Wink Saville3ab207e2014-11-20 13:07:20 -08002483 }
Wink Saville08874612014-08-31 19:19:58 -07002484 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002485 if (!isValid) {
2486 return;
2487 }
Wink Saville08874612014-08-31 19:19:58 -07002488
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002489 Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse(url));
2490 intent.putExtra(SUBSCRIPTION_KEY, subId);
2491 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
2492 mApp.startActivity(intent);
2493 } finally {
2494 Binder.restoreCallingIdentity(identity);
2495 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002496 }
2497
Wink Savilleb564aae2014-10-23 10:18:09 -07002498 public boolean supplyPinForSubscriber(int subId, String pin) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07002499 int [] resultArray = supplyPinReportResultForSubscriber(subId, pin);
Wink Saville9de0f752013-10-22 19:04:03 -07002500 return (resultArray[0] == PhoneConstants.PIN_RESULT_SUCCESS) ? true : false;
2501 }
2502
Wink Savilleb564aae2014-10-23 10:18:09 -07002503 public boolean supplyPukForSubscriber(int subId, String puk, String pin) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07002504 int [] resultArray = supplyPukReportResultForSubscriber(subId, puk, pin);
Wink Saville9de0f752013-10-22 19:04:03 -07002505 return (resultArray[0] == PhoneConstants.PIN_RESULT_SUCCESS) ? true : false;
2506 }
2507
Wink Savilleb564aae2014-10-23 10:18:09 -07002508 public int[] supplyPinReportResultForSubscriber(int subId, String pin) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002509 enforceModifyPermission();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002510
2511 final long identity = Binder.clearCallingIdentity();
2512 try {
Michele Berionne5e411512020-11-13 02:36:59 +00002513 Phone phone = getPhone(subId);
2514 final UnlockSim checkSimPin = new UnlockSim(phone.getPhoneId(), phone.getIccCard());
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002515 checkSimPin.start();
2516 return checkSimPin.unlockSim(null, pin);
2517 } finally {
2518 Binder.restoreCallingIdentity(identity);
2519 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002520 }
2521
Wink Savilleb564aae2014-10-23 10:18:09 -07002522 public int[] supplyPukReportResultForSubscriber(int subId, String puk, String pin) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002523 enforceModifyPermission();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002524
2525 final long identity = Binder.clearCallingIdentity();
2526 try {
Michele Berionne5e411512020-11-13 02:36:59 +00002527 Phone phone = getPhone(subId);
2528 final UnlockSim checkSimPuk = new UnlockSim(phone.getPhoneId(), phone.getIccCard());
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002529 checkSimPuk.start();
2530 return checkSimPuk.unlockSim(puk, pin);
2531 } finally {
2532 Binder.restoreCallingIdentity(identity);
2533 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002534 }
2535
2536 /**
Wink Saville9de0f752013-10-22 19:04:03 -07002537 * Helper thread to turn async call to SimCard#supplyPin into
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002538 * a synchronous one.
2539 */
2540 private static class UnlockSim extends Thread {
2541
2542 private final IccCard mSimCard;
Michele Berionne5e411512020-11-13 02:36:59 +00002543 private final int mPhoneId;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002544
2545 private boolean mDone = false;
Wink Saville9de0f752013-10-22 19:04:03 -07002546 private int mResult = PhoneConstants.PIN_GENERAL_FAILURE;
2547 private int mRetryCount = -1;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002548
2549 // For replies from SimCard interface
2550 private Handler mHandler;
2551
2552 // For async handler to identify request type
2553 private static final int SUPPLY_PIN_COMPLETE = 100;
2554
Michele Berionne5e411512020-11-13 02:36:59 +00002555 UnlockSim(int phoneId, IccCard simCard) {
2556 mPhoneId = phoneId;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002557 mSimCard = simCard;
2558 }
2559
2560 @Override
2561 public void run() {
2562 Looper.prepare();
2563 synchronized (UnlockSim.this) {
2564 mHandler = new Handler() {
2565 @Override
2566 public void handleMessage(Message msg) {
2567 AsyncResult ar = (AsyncResult) msg.obj;
2568 switch (msg.what) {
2569 case SUPPLY_PIN_COMPLETE:
2570 Log.d(LOG_TAG, "SUPPLY_PIN_COMPLETE");
2571 synchronized (UnlockSim.this) {
Wink Saville9de0f752013-10-22 19:04:03 -07002572 mRetryCount = msg.arg1;
2573 if (ar.exception != null) {
2574 if (ar.exception instanceof CommandException &&
2575 ((CommandException)(ar.exception)).getCommandError()
2576 == CommandException.Error.PASSWORD_INCORRECT) {
2577 mResult = PhoneConstants.PIN_PASSWORD_INCORRECT;
vivi.lib5e9ada2019-09-12 16:04:24 +08002578 } //When UiccCardApp dispose,handle message and return exception
2579 else if (ar.exception instanceof CommandException &&
2580 ((CommandException) (ar.exception)).getCommandError()
2581 == CommandException.Error.ABORTED) {
2582 mResult = PhoneConstants.PIN_OPERATION_ABORTED;
Wink Saville9de0f752013-10-22 19:04:03 -07002583 } else {
2584 mResult = PhoneConstants.PIN_GENERAL_FAILURE;
2585 }
2586 } else {
2587 mResult = PhoneConstants.PIN_RESULT_SUCCESS;
2588 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002589 mDone = true;
2590 UnlockSim.this.notifyAll();
2591 }
2592 break;
2593 }
2594 }
2595 };
2596 UnlockSim.this.notifyAll();
2597 }
2598 Looper.loop();
2599 }
2600
2601 /*
2602 * Use PIN or PUK to unlock SIM card
2603 *
2604 * If PUK is null, unlock SIM card with PIN
2605 *
2606 * If PUK is not null, unlock SIM card with PUK and set PIN code
2607 */
Wink Saville9de0f752013-10-22 19:04:03 -07002608 synchronized int[] unlockSim(String puk, String pin) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002609
2610 while (mHandler == null) {
2611 try {
2612 wait();
2613 } catch (InterruptedException e) {
2614 Thread.currentThread().interrupt();
2615 }
2616 }
2617 Message callback = Message.obtain(mHandler, SUPPLY_PIN_COMPLETE);
2618
2619 if (puk == null) {
2620 mSimCard.supplyPin(pin, callback);
2621 } else {
2622 mSimCard.supplyPuk(puk, pin, callback);
2623 }
2624
2625 while (!mDone) {
2626 try {
2627 Log.d(LOG_TAG, "wait for done");
2628 wait();
2629 } catch (InterruptedException e) {
2630 // Restore the interrupted status
2631 Thread.currentThread().interrupt();
2632 }
2633 }
2634 Log.d(LOG_TAG, "done");
Wink Saville9de0f752013-10-22 19:04:03 -07002635 int[] resultArray = new int[2];
2636 resultArray[0] = mResult;
2637 resultArray[1] = mRetryCount;
Michele Berionne5e411512020-11-13 02:36:59 +00002638
2639 if (mResult == PhoneConstants.PIN_RESULT_SUCCESS && pin.length() > 0) {
Jon Spivack9c3bc762021-10-06 20:53:09 +00002640 UiccController.getInstance().getPinStorage().storePin(pin, mPhoneId);
Michele Berionne5e411512020-11-13 02:36:59 +00002641 }
2642
Wink Saville9de0f752013-10-22 19:04:03 -07002643 return resultArray;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002644 }
2645 }
2646
Nathan Harold7c8d0f12020-05-28 20:40:31 -07002647 /**
2648 * This method has been removed due to privacy and stability concerns.
2649 */
2650 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002651 public void updateServiceLocation() {
Nathan Harold7c8d0f12020-05-28 20:40:31 -07002652 Log.e(LOG_TAG, "Call to unsupported method updateServiceLocation()");
2653 return;
Wink Saville36469e72014-06-11 15:17:00 -07002654 }
2655
Nathan Harold1f889d82020-06-04 17:05:26 -07002656 @Override
2657 public void updateServiceLocationWithPackageName(String callingPackage) {
2658 mApp.getSystemService(AppOpsManager.class)
2659 .checkPackage(Binder.getCallingUid(), callingPackage);
2660
Nathan Haroldf096d982020-11-18 17:18:06 -08002661 final int targetSdk = TelephonyPermissions.getTargetSdk(mApp, callingPackage);
Nathan Harold1f889d82020-06-04 17:05:26 -07002662 if (targetSdk > android.os.Build.VERSION_CODES.R) {
2663 // Callers targeting S have no business invoking this method.
2664 return;
2665 }
2666
2667 LocationAccessPolicy.LocationPermissionResult locationResult =
2668 LocationAccessPolicy.checkLocationPermission(mApp,
2669 new LocationAccessPolicy.LocationPermissionQuery.Builder()
2670 .setCallingPackage(callingPackage)
2671 .setCallingFeatureId(null)
2672 .setCallingPid(Binder.getCallingPid())
2673 .setCallingUid(Binder.getCallingUid())
2674 .setMethod("updateServiceLocation")
2675 .setMinSdkVersionForCoarse(Build.VERSION_CODES.BASE)
2676 .setMinSdkVersionForFine(Build.VERSION_CODES.Q)
2677 .build());
2678 // Apps that lack location permission have no business calling this method;
2679 // however, because no permission was declared in the public API, denials must
2680 // all be "soft".
2681 switch (locationResult) {
2682 case DENIED_HARD: /* fall through */
2683 case DENIED_SOFT:
2684 return;
2685 }
2686
2687 WorkSource workSource = getWorkSource(Binder.getCallingUid());
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002688 final long identity = Binder.clearCallingIdentity();
2689 try {
Nathan Harold1f889d82020-06-04 17:05:26 -07002690 final Phone phone = getPhone(getDefaultSubscription());
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002691 if (phone != null) {
Nathan Harold1f889d82020-06-04 17:05:26 -07002692 phone.updateServiceLocation(workSource);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002693 }
2694 } finally {
2695 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07002696 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002697 }
2698
Philip P. Moltmann700a9592019-10-03 11:53:50 -07002699 @Deprecated
Robert Greenwalt36b23af2015-07-06 17:59:14 -07002700 @Override
2701 public boolean isRadioOn(String callingPackage) {
Philip P. Moltmann700a9592019-10-03 11:53:50 -07002702 return isRadioOnWithFeature(callingPackage, null);
2703 }
2704
2705
2706 @Override
2707 public boolean isRadioOnWithFeature(String callingPackage, String callingFeatureId) {
2708 return isRadioOnForSubscriberWithFeature(getDefaultSubscription(), callingPackage,
2709 callingFeatureId);
2710 }
2711
2712 @Deprecated
2713 @Override
2714 public boolean isRadioOnForSubscriber(int subId, String callingPackage) {
2715 return isRadioOnForSubscriberWithFeature(subId, callingPackage, null);
Wink Saville36469e72014-06-11 15:17:00 -07002716 }
2717
Robert Greenwalt36b23af2015-07-06 17:59:14 -07002718 @Override
Philip P. Moltmann700a9592019-10-03 11:53:50 -07002719 public boolean isRadioOnForSubscriberWithFeature(int subId, String callingPackage,
2720 String callingFeatureId) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08002721 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
Philip P. Moltmann700a9592019-10-03 11:53:50 -07002722 mApp, subId, callingPackage, callingFeatureId, "isRadioOnForSubscriber")) {
Robert Greenwalt36b23af2015-07-06 17:59:14 -07002723 return false;
2724 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002725
2726 final long identity = Binder.clearCallingIdentity();
2727 try {
2728 return isRadioOnForSubscriber(subId);
2729 } finally {
2730 Binder.restoreCallingIdentity(identity);
2731 }
Robert Greenwalt36b23af2015-07-06 17:59:14 -07002732 }
2733
2734 private boolean isRadioOnForSubscriber(int subId) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002735 final long identity = Binder.clearCallingIdentity();
2736 try {
2737 final Phone phone = getPhone(subId);
2738 if (phone != null) {
2739 return phone.getServiceState().getState() != ServiceState.STATE_POWER_OFF;
2740 } else {
2741 return false;
2742 }
2743 } finally {
2744 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07002745 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002746 }
2747
2748 public void toggleRadioOnOff() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07002749 toggleRadioOnOffForSubscriber(getDefaultSubscription());
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002750 }
Wink Saville36469e72014-06-11 15:17:00 -07002751
Wink Savilleb564aae2014-10-23 10:18:09 -07002752 public void toggleRadioOnOffForSubscriber(int subId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002753 enforceModifyPermission();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002754
2755 final long identity = Binder.clearCallingIdentity();
2756 try {
2757 final Phone phone = getPhone(subId);
2758 if (phone != null) {
2759 phone.setRadioPower(!isRadioOnForSubscriber(subId));
2760 }
2761 } finally {
2762 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07002763 }
Wink Saville36469e72014-06-11 15:17:00 -07002764 }
2765
2766 public boolean setRadio(boolean turnOn) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07002767 return setRadioForSubscriber(getDefaultSubscription(), turnOn);
Wink Saville36469e72014-06-11 15:17:00 -07002768 }
2769
Wink Savilleb564aae2014-10-23 10:18:09 -07002770 public boolean setRadioForSubscriber(int subId, boolean turnOn) {
Wink Saville36469e72014-06-11 15:17:00 -07002771 enforceModifyPermission();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002772
2773 final long identity = Binder.clearCallingIdentity();
2774 try {
2775 final Phone phone = getPhone(subId);
2776 if (phone == null) {
2777 return false;
2778 }
2779 if ((phone.getServiceState().getState() != ServiceState.STATE_POWER_OFF) != turnOn) {
2780 toggleRadioOnOffForSubscriber(subId);
2781 }
2782 return true;
2783 } finally {
2784 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07002785 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002786 }
Wink Saville36469e72014-06-11 15:17:00 -07002787
Naveen Kalla1fd79bd2014-08-08 00:48:59 -07002788 public boolean needMobileRadioShutdown() {
Shuo Qianfa7b6b32019-12-10 10:40:38 -08002789 enforceReadPrivilegedPermission("needMobileRadioShutdown");
Naveen Kalla1fd79bd2014-08-08 00:48:59 -07002790 /*
2791 * If any of the Radios are available, it will need to be
2792 * shutdown. So return true if any Radio is available.
2793 */
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002794 final long identity = Binder.clearCallingIdentity();
2795 try {
2796 for (int i = 0; i < TelephonyManager.getDefault().getPhoneCount(); i++) {
2797 Phone phone = PhoneFactory.getPhone(i);
2798 if (phone != null && phone.isRadioAvailable()) return true;
2799 }
2800 logv(TelephonyManager.getDefault().getPhoneCount() + " Phones are shutdown.");
2801 return false;
2802 } finally {
2803 Binder.restoreCallingIdentity(identity);
Naveen Kalla1fd79bd2014-08-08 00:48:59 -07002804 }
Naveen Kalla1fd79bd2014-08-08 00:48:59 -07002805 }
2806
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002807 @Override
Naveen Kalla1fd79bd2014-08-08 00:48:59 -07002808 public void shutdownMobileRadios() {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002809 enforceModifyPermission();
2810
2811 final long identity = Binder.clearCallingIdentity();
2812 try {
2813 for (int i = 0; i < TelephonyManager.getDefault().getPhoneCount(); i++) {
2814 logv("Shutting down Phone " + i);
2815 shutdownRadioUsingPhoneId(i);
2816 }
2817 } finally {
2818 Binder.restoreCallingIdentity(identity);
Naveen Kalla1fd79bd2014-08-08 00:48:59 -07002819 }
2820 }
2821
2822 private void shutdownRadioUsingPhoneId(int phoneId) {
Naveen Kalla1fd79bd2014-08-08 00:48:59 -07002823 Phone phone = PhoneFactory.getPhone(phoneId);
2824 if (phone != null && phone.isRadioAvailable()) {
2825 phone.shutdownRadio();
2826 }
2827 }
2828
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002829 public boolean setRadioPower(boolean turnOn) {
Jack Yub4e16162017-05-15 12:48:40 -07002830 enforceModifyPermission();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002831
2832 final long identity = Binder.clearCallingIdentity();
2833 try {
2834 final Phone defaultPhone = PhoneFactory.getDefaultPhone();
2835 if (defaultPhone != null) {
2836 defaultPhone.setRadioPower(turnOn);
2837 return true;
2838 } else {
2839 loge("There's no default phone.");
2840 return false;
2841 }
2842 } finally {
2843 Binder.restoreCallingIdentity(identity);
Wei Liu9ae2a062016-08-08 11:09:34 -07002844 }
Wink Saville36469e72014-06-11 15:17:00 -07002845 }
2846
Wink Savilleb564aae2014-10-23 10:18:09 -07002847 public boolean setRadioPowerForSubscriber(int subId, boolean turnOn) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002848 enforceModifyPermission();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002849
2850 final long identity = Binder.clearCallingIdentity();
2851 try {
2852 final Phone phone = getPhone(subId);
2853 if (phone != null) {
2854 phone.setRadioPower(turnOn);
2855 return true;
2856 } else {
2857 return false;
2858 }
2859 } finally {
2860 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07002861 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002862 }
2863
Wink Saville36469e72014-06-11 15:17:00 -07002864 // FIXME: subId version needed
Sanket Padawe356d7632015-06-22 14:03:32 -07002865 @Override
Sarah Chinecc78c42022-03-31 21:16:48 -07002866 public boolean enableDataConnectivity(String callingPackage) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002867 enforceModifyPermission();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002868
2869 final long identity = Binder.clearCallingIdentity();
2870 try {
Jack Yudf049d72022-12-02 22:48:35 -08002871 int subId = SubscriptionManager.getDefaultDataSubscriptionId();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002872 final Phone phone = getPhone(subId);
2873 if (phone != null) {
Jack Yuabb10902022-07-31 00:43:21 -07002874 phone.getDataSettingsManager().setDataEnabled(
2875 TelephonyManager.DATA_ENABLED_REASON_USER, true, callingPackage);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002876 return true;
2877 } else {
2878 return false;
2879 }
2880 } finally {
2881 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07002882 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002883 }
2884
Wink Saville36469e72014-06-11 15:17:00 -07002885 // FIXME: subId version needed
Sanket Padawe356d7632015-06-22 14:03:32 -07002886 @Override
Sarah Chinecc78c42022-03-31 21:16:48 -07002887 public boolean disableDataConnectivity(String callingPackage) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002888 enforceModifyPermission();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002889
2890 final long identity = Binder.clearCallingIdentity();
2891 try {
Jack Yudf049d72022-12-02 22:48:35 -08002892 int subId = SubscriptionManager.getDefaultDataSubscriptionId();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002893 final Phone phone = getPhone(subId);
2894 if (phone != null) {
Jack Yuabb10902022-07-31 00:43:21 -07002895 phone.getDataSettingsManager().setDataEnabled(
2896 TelephonyManager.DATA_ENABLED_REASON_USER, false, callingPackage);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002897 return true;
2898 } else {
2899 return false;
2900 }
2901 } finally {
2902 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07002903 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002904 }
2905
Sanket Padawe356d7632015-06-22 14:03:32 -07002906 @Override
Jack Yuacf8a132017-05-01 17:00:48 -07002907 public boolean isDataConnectivityPossible(int subId) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002908 final long identity = Binder.clearCallingIdentity();
2909 try {
2910 final Phone phone = getPhone(subId);
2911 if (phone != null) {
Jack Yu59824e12022-03-23 01:42:44 -07002912 return phone.isDataAllowed();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002913 } else {
2914 return false;
2915 }
2916 } finally {
2917 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07002918 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002919 }
2920
2921 public boolean handlePinMmi(String dialString) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07002922 return handlePinMmiForSubscriber(getDefaultSubscription(), dialString);
Wink Saville36469e72014-06-11 15:17:00 -07002923 }
2924
pkanwarae03a6b2016-11-06 20:37:09 -08002925 public void handleUssdRequest(int subId, String ussdRequest, ResultReceiver wrappedCallback) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002926 enforceCallPermission();
2927
2928 final long identity = Binder.clearCallingIdentity();
2929 try {
2930 if (!SubscriptionManager.isValidSubscriptionId(subId)) {
2931 return;
2932 }
2933 Pair<String, ResultReceiver> ussdObject = new Pair(ussdRequest, wrappedCallback);
2934 sendRequest(CMD_HANDLE_USSD_REQUEST, ussdObject, subId);
2935 } finally {
2936 Binder.restoreCallingIdentity(identity);
2937 }
pkanwar32d516d2016-10-14 19:37:38 -07002938 };
2939
Wink Savilleb564aae2014-10-23 10:18:09 -07002940 public boolean handlePinMmiForSubscriber(int subId, String dialString) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002941 enforceModifyPermission();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002942
2943 final long identity = Binder.clearCallingIdentity();
2944 try {
2945 if (!SubscriptionManager.isValidSubscriptionId(subId)) {
2946 return false;
2947 }
2948 return (Boolean) sendRequest(CMD_HANDLE_PIN_MMI, dialString, subId);
2949 } finally {
2950 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07002951 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002952 }
2953
Brad Ebinger4f6208e2021-03-23 21:04:45 +00002954 /**
2955 * @deprecated This method is deprecated and is only being kept due to an UnsupportedAppUsage
2956 * tag on getCallState Binder call.
2957 */
2958 @Deprecated
2959 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002960 public int getCallState() {
Brad Ebinger4f6208e2021-03-23 21:04:45 +00002961 if (CompatChanges.isChangeEnabled(
2962 TelecomManager.ENABLE_GET_CALL_STATE_PERMISSION_PROTECTION,
2963 Binder.getCallingUid())) {
2964 // Do not allow this API to be called on API version 31+, it should only be
2965 // called on old apps using this Binder call directly.
2966 throw new SecurityException("This method can only be used for applications "
2967 + "targeting API version 30 or less.");
2968 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002969 final long identity = Binder.clearCallingIdentity();
2970 try {
Brad Ebinger4f6208e2021-03-23 21:04:45 +00002971 Phone phone = getPhone(getDefaultSubscription());
2972 return phone == null ? TelephonyManager.CALL_STATE_IDLE :
2973 PhoneConstantConversions.convertCallState(phone.getState());
2974 } finally {
2975 Binder.restoreCallingIdentity(identity);
2976 }
2977 }
2978
2979 @Override
2980 public int getCallStateForSubscription(int subId, String callingPackage, String featureId) {
2981 if (CompatChanges.isChangeEnabled(
2982 TelecomManager.ENABLE_GET_CALL_STATE_PERMISSION_PROTECTION,
2983 Binder.getCallingUid())) {
2984 // Check READ_PHONE_STATE for API version 31+
2985 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(mApp, subId, callingPackage,
2986 featureId, "getCallStateForSubscription")) {
2987 throw new SecurityException("getCallState requires READ_PHONE_STATE for apps "
2988 + "targeting API level 31+.");
2989 }
2990 }
2991 final long identity = Binder.clearCallingIdentity();
2992 try {
2993 Phone phone = getPhone(subId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002994 return phone == null ? TelephonyManager.CALL_STATE_IDLE :
2995 PhoneConstantConversions.convertCallState(phone.getState());
2996 } finally {
2997 Binder.restoreCallingIdentity(identity);
2998 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002999 }
3000
Sanket Padawe356d7632015-06-22 14:03:32 -07003001 @Override
Nathan Harolde037c472019-06-26 00:41:07 +00003002 public int getDataState() {
Jack Yudf049d72022-12-02 22:48:35 -08003003 return getDataStateForSubId(SubscriptionManager.getDefaultDataSubscriptionId());
Nathan Haroldc4689b12019-06-14 16:58:30 -07003004 }
3005
3006 @Override
3007 public int getDataStateForSubId(int subId) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003008 final long identity = Binder.clearCallingIdentity();
3009 try {
Nathan Haroldc4689b12019-06-14 16:58:30 -07003010 final Phone phone = getPhone(subId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003011 if (phone != null) {
Jack Yuabb10902022-07-31 00:43:21 -07003012 return phone.getDataNetworkController().getInternetDataNetworkState();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003013 } else {
3014 return PhoneConstantConversions.convertDataState(
3015 PhoneConstants.DataState.DISCONNECTED);
3016 }
3017 } finally {
3018 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07003019 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003020 }
3021
Sanket Padawe356d7632015-06-22 14:03:32 -07003022 @Override
Jack Yu0eda6842022-04-18 00:34:46 -07003023 public @DataActivityType int getDataActivity() {
Jack Yudf049d72022-12-02 22:48:35 -08003024 return getDataActivityForSubId(SubscriptionManager.getDefaultDataSubscriptionId());
Nathan Haroldc4689b12019-06-14 16:58:30 -07003025 }
3026
3027 @Override
Jack Yu0eda6842022-04-18 00:34:46 -07003028 public @DataActivityType int getDataActivityForSubId(int subId) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003029 final long identity = Binder.clearCallingIdentity();
3030 try {
Nathan Haroldc4689b12019-06-14 16:58:30 -07003031 final Phone phone = getPhone(subId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003032 if (phone != null) {
Jack Yu0eda6842022-04-18 00:34:46 -07003033 return phone.getDataActivityState();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003034 } else {
3035 return TelephonyManager.DATA_ACTIVITY_NONE;
3036 }
3037 } finally {
3038 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07003039 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003040 }
3041
3042 @Override
Meng Wanga10e89e2019-12-09 13:13:01 -08003043 public CellIdentity getCellLocation(String callingPackage, String callingFeatureId) {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08003044 mApp.getSystemService(AppOpsManager.class)
Hall Liu1aa510f2017-11-22 17:40:08 -08003045 .checkPackage(Binder.getCallingUid(), callingPackage);
Hall Liuf19c44f2018-11-27 14:38:17 -08003046
3047 LocationAccessPolicy.LocationPermissionResult locationResult =
3048 LocationAccessPolicy.checkLocationPermission(mApp,
3049 new LocationAccessPolicy.LocationPermissionQuery.Builder()
3050 .setCallingPackage(callingPackage)
Philip P. Moltmann3a2772a2019-10-04 08:15:00 -07003051 .setCallingFeatureId(callingFeatureId)
Hall Liuf19c44f2018-11-27 14:38:17 -08003052 .setCallingPid(Binder.getCallingPid())
3053 .setCallingUid(Binder.getCallingUid())
3054 .setMethod("getCellLocation")
Hall Liu773ba022020-01-24 18:07:12 -08003055 .setMinSdkVersionForCoarse(Build.VERSION_CODES.BASE)
Hall Liuf19c44f2018-11-27 14:38:17 -08003056 .setMinSdkVersionForFine(Build.VERSION_CODES.Q)
3057 .build());
3058 switch (locationResult) {
3059 case DENIED_HARD:
3060 throw new SecurityException("Not allowed to access cell location");
3061 case DENIED_SOFT:
Meng Wanga10e89e2019-12-09 13:13:01 -08003062 return (getDefaultPhone().getPhoneType() == PhoneConstants.PHONE_TYPE_CDMA)
3063 ? new CellIdentityCdma() : new CellIdentityGsm();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003064 }
3065
Narayan Kamathf04b5a12018-01-09 11:47:15 +00003066 WorkSource workSource = getWorkSource(Binder.getCallingUid());
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003067 final long identity = Binder.clearCallingIdentity();
3068 try {
3069 if (DBG_LOC) log("getCellLocation: is active user");
Jack Yudf049d72022-12-02 22:48:35 -08003070 int subId = SubscriptionManager.getDefaultDataSubscriptionId();
Meng Wanga10e89e2019-12-09 13:13:01 -08003071 return (CellIdentity) sendRequest(CMD_GET_CELL_LOCATION, workSource, subId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003072 } finally {
3073 Binder.restoreCallingIdentity(identity);
3074 }
Svetoslav64fad262015-04-14 14:35:21 -07003075 }
3076
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003077 @Override
Jack Yueb1e7fe2020-02-22 19:38:58 -08003078 public String getNetworkCountryIsoForPhone(int phoneId) {
Jonathan Basseribf5362b2017-07-19 12:22:35 -07003079 // Reporting the correct network country is ambiguous when IWLAN could conflict with
3080 // registered cell info, so return a NULL country instead.
3081 final long identity = Binder.clearCallingIdentity();
3082 try {
Malcolm Chen3732c2b2018-07-18 20:15:24 -07003083 if (phoneId == SubscriptionManager.INVALID_PHONE_INDEX) {
3084 // Get default phone in this case.
3085 phoneId = SubscriptionManager.DEFAULT_PHONE_INDEX;
3086 }
Jack Yudf049d72022-12-02 22:48:35 -08003087 final int subId = SubscriptionManager.getSubscriptionId(phoneId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003088 Phone phone = PhoneFactory.getPhone(phoneId);
Nathan Harold532f51c2020-04-21 19:31:10 -07003089 if (phone == null) return "";
3090 ServiceStateTracker sst = phone.getServiceStateTracker();
3091 if (sst == null) return "";
3092 LocaleTracker lt = sst.getLocaleTracker();
3093 if (lt == null) return "";
Shuo Qian9418a922021-03-09 11:21:16 -08003094 return lt.getCurrentCountry();
Jonathan Basseribf5362b2017-07-19 12:22:35 -07003095 } finally {
3096 Binder.restoreCallingIdentity(identity);
3097 }
Jonathan Basseribf5362b2017-07-19 12:22:35 -07003098 }
3099
Nathan Harold7c8d0f12020-05-28 20:40:31 -07003100 /**
3101 * This method was removed due to potential issues caused by performing partial
3102 * updates of service state, and lack of a credible use case.
3103 *
3104 * This has the ability to break the telephony implementation by disabling notification of
3105 * changes in device connectivity. DO NOT USE THIS!
3106 */
Jonathan Basseribf5362b2017-07-19 12:22:35 -07003107 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003108 public void enableLocationUpdates() {
3109 mApp.enforceCallingOrSelfPermission(
3110 android.Manifest.permission.CONTROL_LOCATION_UPDATES, null);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003111 }
3112
Nathan Harold7c8d0f12020-05-28 20:40:31 -07003113 /**
3114 * This method was removed due to potential issues caused by performing partial
3115 * updates of service state, and lack of a credible use case.
3116 *
3117 * This has the ability to break the telephony implementation by disabling notification of
3118 * changes in device connectivity. DO NOT USE THIS!
3119 */
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003120 @Override
3121 public void disableLocationUpdates() {
3122 mApp.enforceCallingOrSelfPermission(
3123 android.Manifest.permission.CONTROL_LOCATION_UPDATES, null);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003124 }
3125
3126 @Override
3127 @SuppressWarnings("unchecked")
Philip P. Moltmann3a2772a2019-10-04 08:15:00 -07003128 public List<NeighboringCellInfo> getNeighboringCellInfo(String callingPackage,
3129 String callingFeatureId) {
Nathan Haroldb55f63b2021-07-27 11:27:38 -07003130 try {
3131 mApp.getSystemService(AppOpsManager.class)
3132 .checkPackage(Binder.getCallingUid(), callingPackage);
3133 } catch (SecurityException e) {
3134 EventLog.writeEvent(0x534e4554, "190619791", Binder.getCallingUid());
3135 throw e;
3136 }
3137
Nathan Haroldf096d982020-11-18 17:18:06 -08003138 final int targetSdk = TelephonyPermissions.getTargetSdk(mApp, callingPackage);
Nathan Harolddbea45a2018-08-30 14:35:07 -07003139 if (targetSdk >= android.os.Build.VERSION_CODES.Q) {
3140 throw new SecurityException(
3141 "getNeighboringCellInfo() is unavailable to callers targeting Q+ SDK levels.");
3142 }
Nathan Haroldb4d55612018-07-20 13:13:08 -07003143
Jordan Liu1617b712019-07-10 15:06:26 -07003144 if (mAppOps.noteOp(AppOpsManager.OPSTR_NEIGHBORING_CELLS, Binder.getCallingUid(),
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003145 callingPackage) != AppOpsManager.MODE_ALLOWED) {
3146 return null;
3147 }
Svetoslav64fad262015-04-14 14:35:21 -07003148
Svetoslav Ganov4a9d4482017-06-20 19:53:35 -07003149 if (DBG_LOC) log("getNeighboringCellInfo: is active user");
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003150
Philip P. Moltmann3a2772a2019-10-04 08:15:00 -07003151 List<CellInfo> info = getAllCellInfo(callingPackage, callingFeatureId);
Nathan Haroldf180aac2018-06-01 18:43:55 -07003152 if (info == null) return null;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003153
Nathan Haroldf180aac2018-06-01 18:43:55 -07003154 List<NeighboringCellInfo> neighbors = new ArrayList<NeighboringCellInfo>();
3155 for (CellInfo ci : info) {
3156 if (ci instanceof CellInfoGsm) {
3157 neighbors.add(new NeighboringCellInfo((CellInfoGsm) ci));
3158 } else if (ci instanceof CellInfoWcdma) {
3159 neighbors.add(new NeighboringCellInfo((CellInfoWcdma) ci));
3160 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003161 }
Nathan Haroldf180aac2018-06-01 18:43:55 -07003162 return (neighbors.size()) > 0 ? neighbors : null;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003163 }
3164
Nathan Haroldfa8da0f2018-09-27 18:51:29 -07003165 private List<CellInfo> getCachedCellInfo() {
3166 List<CellInfo> cellInfos = new ArrayList<CellInfo>();
3167 for (Phone phone : PhoneFactory.getPhones()) {
3168 List<CellInfo> info = phone.getAllCellInfo();
3169 if (info != null) cellInfos.addAll(info);
3170 }
3171 return cellInfos;
3172 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003173
3174 @Override
Philip P. Moltmann3a2772a2019-10-04 08:15:00 -07003175 public List<CellInfo> getAllCellInfo(String callingPackage, String callingFeatureId) {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08003176 mApp.getSystemService(AppOpsManager.class)
Hall Liu1aa510f2017-11-22 17:40:08 -08003177 .checkPackage(Binder.getCallingUid(), callingPackage);
Hall Liuf19c44f2018-11-27 14:38:17 -08003178
3179 LocationAccessPolicy.LocationPermissionResult locationResult =
3180 LocationAccessPolicy.checkLocationPermission(mApp,
3181 new LocationAccessPolicy.LocationPermissionQuery.Builder()
3182 .setCallingPackage(callingPackage)
Philip P. Moltmann3a2772a2019-10-04 08:15:00 -07003183 .setCallingFeatureId(callingFeatureId)
Hall Liuf19c44f2018-11-27 14:38:17 -08003184 .setCallingPid(Binder.getCallingPid())
3185 .setCallingUid(Binder.getCallingUid())
3186 .setMethod("getAllCellInfo")
Nathan Harold5ae50b52019-02-20 15:46:36 -08003187 .setMinSdkVersionForCoarse(Build.VERSION_CODES.BASE)
Hall Liuf19c44f2018-11-27 14:38:17 -08003188 .setMinSdkVersionForFine(Build.VERSION_CODES.Q)
3189 .build());
3190 switch (locationResult) {
3191 case DENIED_HARD:
3192 throw new SecurityException("Not allowed to access cell info");
3193 case DENIED_SOFT:
3194 return new ArrayList<>();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003195 }
3196
Nathan Haroldf096d982020-11-18 17:18:06 -08003197 final int targetSdk = TelephonyPermissions.getTargetSdk(mApp, callingPackage);
Nathan Haroldfa8da0f2018-09-27 18:51:29 -07003198 if (targetSdk >= android.os.Build.VERSION_CODES.Q) {
3199 return getCachedCellInfo();
3200 }
3201
Svetoslav Ganov4a9d4482017-06-20 19:53:35 -07003202 if (DBG_LOC) log("getAllCellInfo: is active user");
Narayan Kamathf04b5a12018-01-09 11:47:15 +00003203 WorkSource workSource = getWorkSource(Binder.getCallingUid());
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003204 final long identity = Binder.clearCallingIdentity();
3205 try {
3206 List<CellInfo> cellInfos = new ArrayList<CellInfo>();
3207 for (Phone phone : PhoneFactory.getPhones()) {
Nathan Harold3ff88932018-08-14 10:19:49 -07003208 final List<CellInfo> info = (List<CellInfo>) sendRequest(
Nathan Harold92bed182018-10-12 18:16:49 -07003209 CMD_GET_ALL_CELL_INFO, null, phone, workSource);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003210 if (info != null) cellInfos.addAll(info);
3211 }
3212 return cellInfos;
3213 } finally {
3214 Binder.restoreCallingIdentity(identity);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003215 }
3216 }
3217
Sailesh Nepalbd76e4e2013-10-27 13:59:44 -07003218 @Override
Philip P. Moltmann3a2772a2019-10-04 08:15:00 -07003219 public void requestCellInfoUpdate(int subId, ICellInfoCallback cb, String callingPackage,
3220 String callingFeatureId) {
3221 requestCellInfoUpdateInternal(subId, cb, callingPackage, callingFeatureId,
3222 getWorkSource(Binder.getCallingUid()));
Nathan Haroldfa8da0f2018-09-27 18:51:29 -07003223 }
3224
3225 @Override
Philip P. Moltmann3a2772a2019-10-04 08:15:00 -07003226 public void requestCellInfoUpdateWithWorkSource(int subId, ICellInfoCallback cb,
3227 String callingPackage, String callingFeatureId, WorkSource workSource) {
Nathan Haroldfa8da0f2018-09-27 18:51:29 -07003228 enforceModifyPermission();
Philip P. Moltmann3a2772a2019-10-04 08:15:00 -07003229 requestCellInfoUpdateInternal(subId, cb, callingPackage, callingFeatureId, workSource);
Nathan Haroldfa8da0f2018-09-27 18:51:29 -07003230 }
3231
Philip P. Moltmann3a2772a2019-10-04 08:15:00 -07003232 private void requestCellInfoUpdateInternal(int subId, ICellInfoCallback cb,
3233 String callingPackage, String callingFeatureId, WorkSource workSource) {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08003234 mApp.getSystemService(AppOpsManager.class)
Nathan Haroldfa8da0f2018-09-27 18:51:29 -07003235 .checkPackage(Binder.getCallingUid(), callingPackage);
Hall Liuf19c44f2018-11-27 14:38:17 -08003236
3237 LocationAccessPolicy.LocationPermissionResult locationResult =
3238 LocationAccessPolicy.checkLocationPermission(mApp,
3239 new LocationAccessPolicy.LocationPermissionQuery.Builder()
3240 .setCallingPackage(callingPackage)
Philip P. Moltmann3a2772a2019-10-04 08:15:00 -07003241 .setCallingFeatureId(callingFeatureId)
Hall Liuf19c44f2018-11-27 14:38:17 -08003242 .setCallingPid(Binder.getCallingPid())
3243 .setCallingUid(Binder.getCallingUid())
3244 .setMethod("requestCellInfoUpdate")
Hall Liud60acc92020-05-21 17:09:35 -07003245 .setMinSdkVersionForCoarse(Build.VERSION_CODES.BASE)
3246 .setMinSdkVersionForFine(Build.VERSION_CODES.BASE)
Hall Liuf19c44f2018-11-27 14:38:17 -08003247 .build());
3248 switch (locationResult) {
3249 case DENIED_HARD:
Nathan Haroldf096d982020-11-18 17:18:06 -08003250 if (TelephonyPermissions
3251 .getTargetSdk(mApp, callingPackage) < Build.VERSION_CODES.Q) {
Hall Liud60acc92020-05-21 17:09:35 -07003252 // Safetynet logging for b/154934934
3253 EventLog.writeEvent(0x534e4554, "154934934", Binder.getCallingUid());
3254 }
Hall Liuf19c44f2018-11-27 14:38:17 -08003255 throw new SecurityException("Not allowed to access cell info");
3256 case DENIED_SOFT:
Nathan Haroldf096d982020-11-18 17:18:06 -08003257 if (TelephonyPermissions
3258 .getTargetSdk(mApp, callingPackage) < Build.VERSION_CODES.Q) {
Hall Liud60acc92020-05-21 17:09:35 -07003259 // Safetynet logging for b/154934934
3260 EventLog.writeEvent(0x534e4554, "154934934", Binder.getCallingUid());
3261 }
Nathan Harold5320c422019-05-09 10:26:08 -07003262 try {
3263 cb.onCellInfo(new ArrayList<CellInfo>());
3264 } catch (RemoteException re) {
3265 // Drop without consequences
3266 }
Hall Liuf19c44f2018-11-27 14:38:17 -08003267 return;
Nathan Haroldfa8da0f2018-09-27 18:51:29 -07003268 }
3269
Nathan Harolda939a962019-05-09 10:13:47 -07003270
3271 final Phone phone = getPhoneFromSubId(subId);
Nathan Haroldfa8da0f2018-09-27 18:51:29 -07003272 if (phone == null) throw new IllegalArgumentException("Invalid Subscription Id: " + subId);
3273
3274 sendRequestAsync(CMD_REQUEST_CELL_INFO_UPDATE, cb, phone, workSource);
3275 }
3276
3277 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003278 public void setCellInfoListRate(int rateInMillis) {
Jack Yua8d8cb82017-01-16 10:15:34 -08003279 enforceModifyPermission();
Narayan Kamathf04b5a12018-01-09 11:47:15 +00003280 WorkSource workSource = getWorkSource(Binder.getCallingUid());
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003281
3282 final long identity = Binder.clearCallingIdentity();
3283 try {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08003284 getDefaultPhone().setCellInfoListRate(rateInMillis, workSource);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003285 } finally {
3286 Binder.restoreCallingIdentity(identity);
3287 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003288 }
3289
Shishir Agrawala9f32182016-04-12 12:00:16 -07003290 @Override
Philip P. Moltmann700a9592019-10-03 11:53:50 -07003291 public String getImeiForSlot(int slotIndex, String callingPackage, String callingFeatureId) {
Jeff Davidson913390f2018-02-23 17:11:49 -08003292 Phone phone = PhoneFactory.getPhone(slotIndex);
3293 if (phone == null) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08003294 return null;
3295 }
Jeff Davidson913390f2018-02-23 17:11:49 -08003296 int subId = phone.getSubId();
Grace Jia0ddb3612021-04-22 13:35:26 -07003297 enforceCallingPackage(callingPackage, Binder.getCallingUid(), "getImeiForSlot");
Michael Groover70af6dc2018-10-01 16:23:15 -07003298 if (!TelephonyPermissions.checkCallingOrSelfReadDeviceIdentifiers(mApp, subId,
Philip P. Moltmann700a9592019-10-03 11:53:50 -07003299 callingPackage, callingFeatureId, "getImeiForSlot")) {
Jeff Davidson913390f2018-02-23 17:11:49 -08003300 return null;
3301 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003302
3303 final long identity = Binder.clearCallingIdentity();
3304 try {
3305 return phone.getImei();
3306 } finally {
3307 Binder.restoreCallingIdentity(identity);
3308 }
Shishir Agrawala9f32182016-04-12 12:00:16 -07003309 }
3310
3311 @Override
David Kelly5e06a7f2018-03-12 14:10:59 +00003312 public String getTypeAllocationCodeForSlot(int slotIndex) {
3313 Phone phone = PhoneFactory.getPhone(slotIndex);
3314 String tac = null;
3315 if (phone != null) {
3316 String imei = phone.getImei();
Vala Zadehab005552021-09-21 15:54:29 -07003317 try {
3318 tac = imei == null ? null : imei.substring(0, TYPE_ALLOCATION_CODE_LENGTH);
3319 } catch (IndexOutOfBoundsException e) {
3320 Log.e(LOG_TAG, "IMEI length shorter than upper index.");
3321 return null;
3322 }
David Kelly5e06a7f2018-03-12 14:10:59 +00003323 }
3324 return tac;
3325 }
3326
3327 @Override
Philip P. Moltmann700a9592019-10-03 11:53:50 -07003328 public String getMeidForSlot(int slotIndex, String callingPackage, String callingFeatureId) {
Shuo Qian13d89152021-05-10 23:58:11 -07003329 try {
3330 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
3331 } catch (SecurityException se) {
3332 EventLog.writeEvent(0x534e4554, "186530496", Binder.getCallingUid());
3333 throw new SecurityException("Package " + callingPackage + " does not belong to "
3334 + Binder.getCallingUid());
3335 }
Jeff Davidson913390f2018-02-23 17:11:49 -08003336 Phone phone = PhoneFactory.getPhone(slotIndex);
3337 if (phone == null) {
Jack Yu2af8d712017-03-15 17:14:14 -07003338 return null;
3339 }
Nazanin Bakhshif782e562018-12-11 15:15:39 -08003340
Jeff Davidson913390f2018-02-23 17:11:49 -08003341 int subId = phone.getSubId();
Michael Groover70af6dc2018-10-01 16:23:15 -07003342 if (!TelephonyPermissions.checkCallingOrSelfReadDeviceIdentifiers(mApp, subId,
Philip P. Moltmann700a9592019-10-03 11:53:50 -07003343 callingPackage, callingFeatureId, "getMeidForSlot")) {
Jeff Davidson913390f2018-02-23 17:11:49 -08003344 return null;
3345 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003346
3347 final long identity = Binder.clearCallingIdentity();
3348 try {
3349 return phone.getMeid();
3350 } finally {
3351 Binder.restoreCallingIdentity(identity);
3352 }
Jack Yu2af8d712017-03-15 17:14:14 -07003353 }
3354
3355 @Override
David Kelly5e06a7f2018-03-12 14:10:59 +00003356 public String getManufacturerCodeForSlot(int slotIndex) {
3357 Phone phone = PhoneFactory.getPhone(slotIndex);
3358 String manufacturerCode = null;
3359 if (phone != null) {
3360 String meid = phone.getMeid();
Vala Zadehab005552021-09-21 15:54:29 -07003361 try {
3362 manufacturerCode =
3363 meid == null ? null : meid.substring(0, MANUFACTURER_CODE_LENGTH);
3364 } catch (IndexOutOfBoundsException e) {
3365 Log.e(LOG_TAG, "MEID length shorter than upper index.");
3366 return null;
3367 }
David Kelly5e06a7f2018-03-12 14:10:59 +00003368 }
3369 return manufacturerCode;
3370 }
3371
3372 @Override
Philip P. Moltmann700a9592019-10-03 11:53:50 -07003373 public String getDeviceSoftwareVersionForSlot(int slotIndex, String callingPackage,
3374 String callingFeatureId) {
Jeff Davidson913390f2018-02-23 17:11:49 -08003375 Phone phone = PhoneFactory.getPhone(slotIndex);
3376 if (phone == null) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08003377 return null;
3378 }
Jeff Davidson913390f2018-02-23 17:11:49 -08003379 int subId = phone.getSubId();
3380 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
Philip P. Moltmann700a9592019-10-03 11:53:50 -07003381 mApp, subId, callingPackage, callingFeatureId,
3382 "getDeviceSoftwareVersionForSlot")) {
Jeff Davidson913390f2018-02-23 17:11:49 -08003383 return null;
3384 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003385
3386 final long identity = Binder.clearCallingIdentity();
3387 try {
3388 return phone.getDeviceSvn();
3389 } finally {
3390 Binder.restoreCallingIdentity(identity);
3391 }
Shishir Agrawala9f32182016-04-12 12:00:16 -07003392 }
3393
fionaxu43304da2017-11-27 22:51:16 -08003394 @Override
3395 public int getSubscriptionCarrierId(int subId) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003396 final long identity = Binder.clearCallingIdentity();
3397 try {
3398 final Phone phone = getPhone(subId);
3399 return phone == null ? TelephonyManager.UNKNOWN_CARRIER_ID : phone.getCarrierId();
3400 } finally {
3401 Binder.restoreCallingIdentity(identity);
3402 }
fionaxu43304da2017-11-27 22:51:16 -08003403 }
3404
3405 @Override
3406 public String getSubscriptionCarrierName(int subId) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003407 final long identity = Binder.clearCallingIdentity();
3408 try {
3409 final Phone phone = getPhone(subId);
3410 return phone == null ? null : phone.getCarrierName();
3411 } finally {
3412 Binder.restoreCallingIdentity(identity);
3413 }
fionaxu43304da2017-11-27 22:51:16 -08003414 }
3415
calvinpanffe225e2018-11-01 19:43:06 +08003416 @Override
chen xu0026ca62019-03-06 15:28:50 -08003417 public int getSubscriptionSpecificCarrierId(int subId) {
chen xu25637222018-11-04 17:17:00 -08003418 final long identity = Binder.clearCallingIdentity();
3419 try {
3420 final Phone phone = getPhone(subId);
3421 return phone == null ? TelephonyManager.UNKNOWN_CARRIER_ID
chen xu0026ca62019-03-06 15:28:50 -08003422 : phone.getSpecificCarrierId();
chen xu25637222018-11-04 17:17:00 -08003423 } finally {
3424 Binder.restoreCallingIdentity(identity);
3425 }
3426 }
3427
3428 @Override
chen xu0026ca62019-03-06 15:28:50 -08003429 public String getSubscriptionSpecificCarrierName(int subId) {
chen xu25637222018-11-04 17:17:00 -08003430 final long identity = Binder.clearCallingIdentity();
3431 try {
3432 final Phone phone = getPhone(subId);
chen xu0026ca62019-03-06 15:28:50 -08003433 return phone == null ? null : phone.getSpecificCarrierName();
chen xu25637222018-11-04 17:17:00 -08003434 } finally {
3435 Binder.restoreCallingIdentity(identity);
3436 }
3437 }
3438
chen xu651eec72018-11-11 19:03:44 -08003439 @Override
chen xu864e11c2018-12-06 22:10:03 -08003440 public int getCarrierIdFromMccMnc(int slotIndex, String mccmnc, boolean isSubscriptionMccMnc) {
3441 if (!isSubscriptionMccMnc) {
3442 enforceReadPrivilegedPermission("getCarrierIdFromMccMnc");
3443 }
chen xu651eec72018-11-11 19:03:44 -08003444 final Phone phone = PhoneFactory.getPhone(slotIndex);
3445 if (phone == null) {
3446 return TelephonyManager.UNKNOWN_CARRIER_ID;
3447 }
3448 final long identity = Binder.clearCallingIdentity();
3449 try {
3450 return CarrierResolver.getCarrierIdFromMccMnc(phone.getContext(), mccmnc);
3451 } finally {
3452 Binder.restoreCallingIdentity(identity);
3453 }
3454 }
3455
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003456 //
3457 // Internal helper methods.
3458 //
3459
Sanket Padaweee13a9b2016-03-08 17:30:28 -08003460 /**
Grace Jia0ddb3612021-04-22 13:35:26 -07003461 * Make sure the caller is the calling package itself
3462 *
3463 * @throws SecurityException if the caller is not the calling package
3464 */
3465 private void enforceCallingPackage(String callingPackage, int callingUid, String message) {
3466 int packageUid = -1;
Grace Jiadbefca02021-04-26 15:13:31 -07003467 PackageManager pm = mApp.getBaseContext().createContextAsUser(
3468 UserHandle.getUserHandleForUid(callingUid), 0).getPackageManager();
Grace Jia0ddb3612021-04-22 13:35:26 -07003469 try {
Grace Jiadbefca02021-04-26 15:13:31 -07003470 packageUid = pm.getPackageUid(callingPackage, 0);
Grace Jia0ddb3612021-04-22 13:35:26 -07003471 } catch (PackageManager.NameNotFoundException e) {
3472 // packageUid is -1
3473 }
3474 if (packageUid != callingUid) {
3475 throw new SecurityException(message + ": Package " + callingPackage
3476 + " does not belong to " + callingUid);
3477 }
3478 }
3479
3480 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003481 * Make sure the caller has the MODIFY_PHONE_STATE permission.
3482 *
3483 * @throws SecurityException if the caller does not have the required permission
3484 */
3485 private void enforceModifyPermission() {
3486 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.MODIFY_PHONE_STATE, null);
3487 }
3488
Shuo Qian3b6ee772019-11-13 17:43:31 -08003489 private void enforceActiveEmergencySessionPermission() {
3490 mApp.enforceCallingOrSelfPermission(
3491 android.Manifest.permission.READ_ACTIVE_EMERGENCY_SESSION, null);
3492 }
3493
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003494 /**
3495 * Make sure the caller has the CALL_PHONE permission.
3496 *
3497 * @throws SecurityException if the caller does not have the required permission
3498 */
3499 private void enforceCallPermission() {
3500 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.CALL_PHONE, null);
3501 }
3502
paulhu5a773602019-08-23 19:17:33 +08003503 private void enforceSettingsPermission() {
3504 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.NETWORK_SETTINGS, null);
Stuart Scott8eef64f2015-04-08 15:13:54 -07003505 }
3506
Michele Berionne5e411512020-11-13 02:36:59 +00003507 private void enforceRebootPermission() {
3508 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.REBOOT, null);
3509 }
3510
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003511 private String createTelUrl(String number) {
3512 if (TextUtils.isEmpty(number)) {
3513 return null;
3514 }
3515
Jake Hambye994d462014-02-03 13:10:13 -08003516 return "tel:" + number;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003517 }
3518
Ihab Awadf9e92732013-12-05 18:02:52 -08003519 private static void log(String msg) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003520 Log.d(LOG_TAG, "[PhoneIntfMgr] " + msg);
3521 }
3522
Naveen Kalla1fd79bd2014-08-08 00:48:59 -07003523 private static void logv(String msg) {
3524 Log.v(LOG_TAG, "[PhoneIntfMgr] " + msg);
3525 }
3526
Ihab Awadf9e92732013-12-05 18:02:52 -08003527 private static void loge(String msg) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003528 Log.e(LOG_TAG, "[PhoneIntfMgr] " + msg);
3529 }
3530
Robert Greenwalt36b23af2015-07-06 17:59:14 -07003531 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003532 public int getActivePhoneType() {
Shishir Agrawala9f32182016-04-12 12:00:16 -07003533 return getActivePhoneTypeForSlot(getSlotForDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07003534 }
3535
Sanket Padawe356d7632015-06-22 14:03:32 -07003536 @Override
Sanket Padawe13bac7b2017-03-20 15:04:47 -07003537 public int getActivePhoneTypeForSlot(int slotIndex) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003538 final long identity = Binder.clearCallingIdentity();
3539 try {
3540 final Phone phone = PhoneFactory.getPhone(slotIndex);
3541 if (phone == null) {
3542 return PhoneConstants.PHONE_TYPE_NONE;
3543 } else {
3544 return phone.getPhoneType();
3545 }
3546 } finally {
3547 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07003548 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003549 }
3550
3551 /**
3552 * Returns the CDMA ERI icon index to display
3553 */
Robert Greenwalt36b23af2015-07-06 17:59:14 -07003554 @Override
Philip P. Moltmann700a9592019-10-03 11:53:50 -07003555 public int getCdmaEriIconIndex(String callingPackage, String callingFeatureId) {
3556 return getCdmaEriIconIndexForSubscriber(getDefaultSubscription(), callingPackage,
3557 callingFeatureId);
Wink Saville36469e72014-06-11 15:17:00 -07003558 }
3559
Sanket Padawe356d7632015-06-22 14:03:32 -07003560 @Override
Philip P. Moltmann700a9592019-10-03 11:53:50 -07003561 public int getCdmaEriIconIndexForSubscriber(int subId, String callingPackage,
3562 String callingFeatureId) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08003563 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
Philip P. Moltmann700a9592019-10-03 11:53:50 -07003564 mApp, subId, callingPackage, callingFeatureId,
3565 "getCdmaEriIconIndexForSubscriber")) {
Robert Greenwalt36b23af2015-07-06 17:59:14 -07003566 return -1;
3567 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003568
3569 final long identity = Binder.clearCallingIdentity();
3570 try {
3571 final Phone phone = getPhone(subId);
3572 if (phone != null) {
3573 return phone.getCdmaEriIconIndex();
3574 } else {
3575 return -1;
3576 }
3577 } finally {
3578 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07003579 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003580 }
3581
3582 /**
3583 * Returns the CDMA ERI icon mode,
3584 * 0 - ON
3585 * 1 - FLASHING
3586 */
Robert Greenwalt36b23af2015-07-06 17:59:14 -07003587 @Override
Philip P. Moltmann700a9592019-10-03 11:53:50 -07003588 public int getCdmaEriIconMode(String callingPackage, String callingFeatureId) {
3589 return getCdmaEriIconModeForSubscriber(getDefaultSubscription(), callingPackage,
3590 callingFeatureId);
Wink Saville36469e72014-06-11 15:17:00 -07003591 }
3592
Sanket Padawe356d7632015-06-22 14:03:32 -07003593 @Override
Philip P. Moltmann700a9592019-10-03 11:53:50 -07003594 public int getCdmaEriIconModeForSubscriber(int subId, String callingPackage,
3595 String callingFeatureId) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08003596 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
Philip P. Moltmann700a9592019-10-03 11:53:50 -07003597 mApp, subId, callingPackage, callingFeatureId,
3598 "getCdmaEriIconModeForSubscriber")) {
Robert Greenwalt36b23af2015-07-06 17:59:14 -07003599 return -1;
3600 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003601
3602 final long identity = Binder.clearCallingIdentity();
3603 try {
3604 final Phone phone = getPhone(subId);
3605 if (phone != null) {
3606 return phone.getCdmaEriIconMode();
3607 } else {
3608 return -1;
3609 }
3610 } finally {
3611 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07003612 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003613 }
3614
3615 /**
3616 * Returns the CDMA ERI text,
3617 */
Robert Greenwalt36b23af2015-07-06 17:59:14 -07003618 @Override
Philip P. Moltmann700a9592019-10-03 11:53:50 -07003619 public String getCdmaEriText(String callingPackage, String callingFeatureId) {
3620 return getCdmaEriTextForSubscriber(getDefaultSubscription(), callingPackage,
3621 callingFeatureId);
Wink Saville36469e72014-06-11 15:17:00 -07003622 }
3623
Sanket Padawe356d7632015-06-22 14:03:32 -07003624 @Override
Philip P. Moltmann700a9592019-10-03 11:53:50 -07003625 public String getCdmaEriTextForSubscriber(int subId, String callingPackage,
3626 String callingFeatureId) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08003627 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
Philip P. Moltmann700a9592019-10-03 11:53:50 -07003628 mApp, subId, callingPackage, callingFeatureId,
3629 "getCdmaEriIconTextForSubscriber")) {
Robert Greenwalt36b23af2015-07-06 17:59:14 -07003630 return null;
3631 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003632
3633 final long identity = Binder.clearCallingIdentity();
3634 try {
3635 final Phone phone = getPhone(subId);
3636 if (phone != null) {
3637 return phone.getCdmaEriText();
3638 } else {
3639 return null;
3640 }
3641 } finally {
3642 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07003643 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003644 }
3645
3646 /**
Junda Liuca05d5d2014-08-14 22:36:34 -07003647 * Returns the CDMA MDN.
3648 */
Sanket Padawe356d7632015-06-22 14:03:32 -07003649 @Override
Wink Savilleb564aae2014-10-23 10:18:09 -07003650 public String getCdmaMdn(int subId) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08003651 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
3652 mApp, subId, "getCdmaMdn");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003653
3654 final long identity = Binder.clearCallingIdentity();
3655 try {
3656 final Phone phone = getPhone(subId);
Nazanin Bakhshif782e562018-12-11 15:15:39 -08003657 if (phone != null && phone.getPhoneType() == PhoneConstants.PHONE_TYPE_CDMA) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003658 return phone.getLine1Number();
3659 } else {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08003660 loge("getCdmaMdn: no phone found. Invalid subId: " + subId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003661 return null;
3662 }
3663 } finally {
3664 Binder.restoreCallingIdentity(identity);
Junda Liuca05d5d2014-08-14 22:36:34 -07003665 }
3666 }
3667
3668 /**
3669 * Returns the CDMA MIN.
3670 */
Sanket Padawe356d7632015-06-22 14:03:32 -07003671 @Override
Wink Savilleb564aae2014-10-23 10:18:09 -07003672 public String getCdmaMin(int subId) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08003673 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
3674 mApp, subId, "getCdmaMin");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003675
3676 final long identity = Binder.clearCallingIdentity();
3677 try {
3678 final Phone phone = getPhone(subId);
3679 if (phone != null && phone.getPhoneType() == PhoneConstants.PHONE_TYPE_CDMA) {
3680 return phone.getCdmaMin();
3681 } else {
3682 return null;
3683 }
3684 } finally {
3685 Binder.restoreCallingIdentity(identity);
Junda Liuca05d5d2014-08-14 22:36:34 -07003686 }
3687 }
3688
Hall Liud892bec2018-11-30 14:51:45 -08003689 @Override
3690 public void requestNumberVerification(PhoneNumberRange range, long timeoutMillis,
3691 INumberVerificationCallback callback, String callingPackage) {
3692 if (mApp.checkCallingOrSelfPermission(android.Manifest.permission.MODIFY_PHONE_STATE)
3693 != PERMISSION_GRANTED) {
3694 throw new SecurityException("Caller must hold the MODIFY_PHONE_STATE permission");
3695 }
3696 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
3697
3698 String authorizedPackage = NumberVerificationManager.getAuthorizedPackage(mApp);
3699 if (!TextUtils.equals(callingPackage, authorizedPackage)) {
Hall Liub9d8feb2021-01-13 10:28:04 -08003700 throw new SecurityException("Calling package must be configured in the device config: "
3701 + "calling package: " + callingPackage
3702 + ", configured package: " + authorizedPackage);
Hall Liud892bec2018-11-30 14:51:45 -08003703 }
3704
3705 if (range == null) {
3706 throw new NullPointerException("Range must be non-null");
3707 }
3708
3709 timeoutMillis = Math.min(timeoutMillis,
Hall Liubd069e32019-02-28 18:56:30 -08003710 TelephonyManager.getMaxNumberVerificationTimeoutMillis());
Hall Liud892bec2018-11-30 14:51:45 -08003711
3712 NumberVerificationManager.getInstance().requestVerification(range, callback, timeoutMillis);
3713 }
3714
Junda Liuca05d5d2014-08-14 22:36:34 -07003715 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003716 * Returns true if CDMA provisioning needs to run.
3717 */
3718 public boolean needsOtaServiceProvisioning() {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003719 final long identity = Binder.clearCallingIdentity();
3720 try {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08003721 return getDefaultPhone().needsOtaServiceProvisioning();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003722 } finally {
3723 Binder.restoreCallingIdentity(identity);
3724 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003725 }
3726
3727 /**
Shishir Agrawal76d5da92014-11-09 16:17:25 -08003728 * Sets the voice mail number of a given subId.
3729 */
3730 @Override
3731 public boolean setVoiceMailNumber(int subId, String alphaTag, String number) {
Shuo Qian2c0ae432019-12-05 11:40:37 -08003732 TelephonyPermissions.enforceCallingOrSelfCarrierPrivilege(
3733 mApp, subId, "setVoiceMailNumber");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003734
3735 final long identity = Binder.clearCallingIdentity();
3736 try {
3737 Boolean success = (Boolean) sendRequest(CMD_SET_VOICEMAIL_NUMBER,
3738 new Pair<String, String>(alphaTag, number), new Integer(subId));
3739 return success;
3740 } finally {
3741 Binder.restoreCallingIdentity(identity);
3742 }
Shishir Agrawal76d5da92014-11-09 16:17:25 -08003743 }
3744
Ta-wei Yen87c49842016-05-13 21:19:52 -07003745 @Override
Ta-wei Yenc9df0432017-04-17 17:09:07 -07003746 public Bundle getVisualVoicemailSettings(String callingPackage, int subId) {
3747 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
Tyler Gunn5ddfdc92019-10-31 13:08:23 -07003748 TelecomManager tm = mApp.getSystemService(TelecomManager.class);
3749 String systemDialer = tm.getSystemDialerPackage();
Ta-wei Yenc9df0432017-04-17 17:09:07 -07003750 if (!TextUtils.equals(callingPackage, systemDialer)) {
3751 throw new SecurityException("caller must be system dialer");
3752 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003753
3754 final long identity = Binder.clearCallingIdentity();
3755 try {
3756 PhoneAccountHandle phoneAccountHandle = PhoneAccountHandleConverter.fromSubId(subId);
3757 if (phoneAccountHandle == null) {
3758 return null;
3759 }
Nazanin Bakhshif782e562018-12-11 15:15:39 -08003760 return VisualVoicemailSettingsUtil.dump(mApp, phoneAccountHandle);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003761 } finally {
3762 Binder.restoreCallingIdentity(identity);
Ta-wei Yenc9df0432017-04-17 17:09:07 -07003763 }
Ta-wei Yenc9df0432017-04-17 17:09:07 -07003764 }
3765
3766 @Override
Philip P. Moltmann700a9592019-10-03 11:53:50 -07003767 public String getVisualVoicemailPackageName(String callingPackage, String callingFeatureId,
3768 int subId) {
Ta-wei Yendca928f2017-01-10 16:17:08 -08003769 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
Jeff Davidson7e17e312018-02-13 18:17:36 -08003770 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
Philip P. Moltmann700a9592019-10-03 11:53:50 -07003771 mApp, subId, callingPackage, callingFeatureId,
3772 "getVisualVoicemailPackageName")) {
Ta-wei Yendca928f2017-01-10 16:17:08 -08003773 return null;
3774 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003775
Jeff Davidsona8e4e242018-03-15 17:16:18 -07003776 final long identity = Binder.clearCallingIdentity();
3777 try {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08003778 return RemoteVvmTaskManager.getRemotePackage(mApp, subId).getPackageName();
Jeff Davidsona8e4e242018-03-15 17:16:18 -07003779 } finally {
3780 Binder.restoreCallingIdentity(identity);
3781 }
Ta-wei Yendca928f2017-01-10 16:17:08 -08003782 }
3783
3784 @Override
Ta-wei Yenb6929602016-05-24 15:48:27 -07003785 public void enableVisualVoicemailSmsFilter(String callingPackage, int subId,
3786 VisualVoicemailSmsFilterSettings settings) {
3787 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003788
3789 final long identity = Binder.clearCallingIdentity();
3790 try {
3791 VisualVoicemailSmsFilterConfig.enableVisualVoicemailSmsFilter(
Nazanin Bakhshif782e562018-12-11 15:15:39 -08003792 mApp, callingPackage, subId, settings);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003793 } finally {
3794 Binder.restoreCallingIdentity(identity);
3795 }
Ta-wei Yen87c49842016-05-13 21:19:52 -07003796 }
3797
3798 @Override
Ta-wei Yenb6929602016-05-24 15:48:27 -07003799 public void disableVisualVoicemailSmsFilter(String callingPackage, int subId) {
3800 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003801
3802 final long identity = Binder.clearCallingIdentity();
3803 try {
3804 VisualVoicemailSmsFilterConfig.disableVisualVoicemailSmsFilter(
Nazanin Bakhshif782e562018-12-11 15:15:39 -08003805 mApp, callingPackage, subId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003806 } finally {
3807 Binder.restoreCallingIdentity(identity);
3808 }
Ta-wei Yen87c49842016-05-13 21:19:52 -07003809 }
3810
3811 @Override
Ta-wei Yenb6929602016-05-24 15:48:27 -07003812 public VisualVoicemailSmsFilterSettings getVisualVoicemailSmsFilterSettings(
3813 String callingPackage, int subId) {
3814 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003815
3816 final long identity = Binder.clearCallingIdentity();
3817 try {
3818 return VisualVoicemailSmsFilterConfig.getVisualVoicemailSmsFilterSettings(
Nazanin Bakhshif782e562018-12-11 15:15:39 -08003819 mApp, callingPackage, subId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003820 } finally {
3821 Binder.restoreCallingIdentity(identity);
3822 }
Ta-wei Yen87c49842016-05-13 21:19:52 -07003823 }
3824
3825 @Override
Ta-wei Yen30a69c82016-12-27 14:52:32 -08003826 public VisualVoicemailSmsFilterSettings getActiveVisualVoicemailSmsFilterSettings(int subId) {
Brad Ebinger35c841c2018-10-01 10:40:55 -07003827 enforceReadPrivilegedPermission("getActiveVisualVoicemailSmsFilterSettings");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003828
3829 final long identity = Binder.clearCallingIdentity();
3830 try {
3831 return VisualVoicemailSmsFilterConfig.getActiveVisualVoicemailSmsFilterSettings(
Nazanin Bakhshif782e562018-12-11 15:15:39 -08003832 mApp, subId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003833 } finally {
3834 Binder.restoreCallingIdentity(identity);
3835 }
Ta-wei Yen30a69c82016-12-27 14:52:32 -08003836 }
3837
3838 @Override
Philip P. Moltmann2f6f8ce2020-03-18 18:17:02 -07003839 public void sendVisualVoicemailSmsForSubscriber(String callingPackage,
3840 String callingAttributionTag, int subId, String number, int port, String text,
3841 PendingIntent sentIntent) {
Ta-wei Yen30a69c82016-12-27 14:52:32 -08003842 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
Ta-wei Yen527a9c02017-01-06 15:29:25 -08003843 enforceVisualVoicemailPackage(callingPackage, subId);
Ta-wei Yen30a69c82016-12-27 14:52:32 -08003844 enforceSendSmsPermission();
Amit Mahajandccb3f12019-05-13 13:48:32 -07003845 SmsController smsController = PhoneFactory.getSmsController();
Philip P. Moltmann2f6f8ce2020-03-18 18:17:02 -07003846 smsController.sendVisualVoicemailSmsForSubscriber(callingPackage, callingAttributionTag,
3847 subId, number, port, text, sentIntent);
Ta-wei Yen87c49842016-05-13 21:19:52 -07003848 }
Amit Mahajandccb3f12019-05-13 13:48:32 -07003849
Shishir Agrawal76d5da92014-11-09 16:17:25 -08003850 /**
fionaxu0152e512016-11-14 13:36:14 -08003851 * Sets the voice activation state of a given subId.
3852 */
3853 @Override
3854 public void setVoiceActivationState(int subId, int activationState) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08003855 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
3856 mApp, subId, "setVoiceActivationState");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003857
3858 final long identity = Binder.clearCallingIdentity();
3859 try {
3860 final Phone phone = getPhone(subId);
3861 if (phone != null) {
3862 phone.setVoiceActivationState(activationState);
3863 } else {
3864 loge("setVoiceActivationState fails with invalid subId: " + subId);
3865 }
3866 } finally {
3867 Binder.restoreCallingIdentity(identity);
fionaxu0152e512016-11-14 13:36:14 -08003868 }
3869 }
3870
3871 /**
3872 * Sets the data activation state of a given subId.
3873 */
3874 @Override
3875 public void setDataActivationState(int subId, int activationState) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08003876 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
3877 mApp, subId, "setDataActivationState");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003878
3879 final long identity = Binder.clearCallingIdentity();
3880 try {
3881 final Phone phone = getPhone(subId);
3882 if (phone != null) {
3883 phone.setDataActivationState(activationState);
3884 } else {
Taesu Leef8fbed92019-10-07 18:47:02 +09003885 loge("setDataActivationState fails with invalid subId: " + subId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003886 }
3887 } finally {
3888 Binder.restoreCallingIdentity(identity);
fionaxu0152e512016-11-14 13:36:14 -08003889 }
3890 }
3891
3892 /**
3893 * Returns the voice activation state of a given subId.
3894 */
3895 @Override
3896 public int getVoiceActivationState(int subId, String callingPackage) {
Brad Ebinger35c841c2018-10-01 10:40:55 -07003897 enforceReadPrivilegedPermission("getVoiceActivationState");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003898
fionaxu0152e512016-11-14 13:36:14 -08003899 final Phone phone = getPhone(subId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003900 final long identity = Binder.clearCallingIdentity();
3901 try {
3902 if (phone != null) {
3903 return phone.getVoiceActivationState();
3904 } else {
3905 return TelephonyManager.SIM_ACTIVATION_STATE_UNKNOWN;
3906 }
3907 } finally {
3908 Binder.restoreCallingIdentity(identity);
fionaxu0152e512016-11-14 13:36:14 -08003909 }
3910 }
3911
3912 /**
3913 * Returns the data activation state of a given subId.
3914 */
3915 @Override
3916 public int getDataActivationState(int subId, String callingPackage) {
Brad Ebinger35c841c2018-10-01 10:40:55 -07003917 enforceReadPrivilegedPermission("getDataActivationState");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003918
fionaxu0152e512016-11-14 13:36:14 -08003919 final Phone phone = getPhone(subId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003920 final long identity = Binder.clearCallingIdentity();
3921 try {
3922 if (phone != null) {
3923 return phone.getDataActivationState();
3924 } else {
3925 return TelephonyManager.SIM_ACTIVATION_STATE_UNKNOWN;
3926 }
3927 } finally {
3928 Binder.restoreCallingIdentity(identity);
fionaxu0152e512016-11-14 13:36:14 -08003929 }
3930 }
3931
3932 /**
Wink Saville36469e72014-06-11 15:17:00 -07003933 * Returns the unread count of voicemails for a subId
3934 */
Sanket Padawe356d7632015-06-22 14:03:32 -07003935 @Override
Philip P. Moltmann700a9592019-10-03 11:53:50 -07003936 public int getVoiceMessageCountForSubscriber(int subId, String callingPackage,
3937 String callingFeatureId) {
Brad Ebingerf7664ba2018-11-29 12:43:38 -08003938 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
Philip P. Moltmann700a9592019-10-03 11:53:50 -07003939 mApp, subId, callingPackage, callingFeatureId,
3940 "getVoiceMessageCountForSubscriber")) {
Brad Ebingerf7664ba2018-11-29 12:43:38 -08003941 return 0;
3942 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003943 final long identity = Binder.clearCallingIdentity();
3944 try {
3945 final Phone phone = getPhone(subId);
3946 if (phone != null) {
3947 return phone.getVoiceMessageCount();
3948 } else {
3949 return 0;
3950 }
3951 } finally {
3952 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07003953 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003954 }
3955
3956 /**
pkanwar8a4dcfb2017-01-19 13:43:16 -08003957 * returns true, if the device is in a state where both voice and data
3958 * are supported simultaneously. This can change based on location or network condition.
3959 */
3960 @Override
3961 public boolean isConcurrentVoiceAndDataAllowed(int subId) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003962 final long identity = Binder.clearCallingIdentity();
3963 try {
3964 final Phone phone = getPhone(subId);
3965 return (phone == null ? false : phone.isConcurrentVoiceAndDataAllowed());
3966 } finally {
3967 Binder.restoreCallingIdentity(identity);
3968 }
pkanwar8a4dcfb2017-01-19 13:43:16 -08003969 }
3970
3971 /**
fionaxu235cc5e2017-03-06 22:25:57 -08003972 * Send the dialer code if called from the current default dialer or the caller has
3973 * carrier privilege.
3974 * @param inputCode The dialer code to send
3975 */
3976 @Override
3977 public void sendDialerSpecialCode(String callingPackage, String inputCode) {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08003978 final Phone defaultPhone = getDefaultPhone();
fionaxu235cc5e2017-03-06 22:25:57 -08003979 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
Tyler Gunn5ddfdc92019-10-31 13:08:23 -07003980 TelecomManager tm = defaultPhone.getContext().getSystemService(TelecomManager.class);
3981 String defaultDialer = tm.getDefaultDialerPackage();
fionaxu235cc5e2017-03-06 22:25:57 -08003982 if (!TextUtils.equals(callingPackage, defaultDialer)) {
Shuo Qian2c0ae432019-12-05 11:40:37 -08003983 TelephonyPermissions.enforceCallingOrSelfCarrierPrivilege(mApp,
Jeff Davidson7e17e312018-02-13 18:17:36 -08003984 getDefaultSubscription(), "sendDialerSpecialCode");
fionaxu235cc5e2017-03-06 22:25:57 -08003985 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003986
3987 final long identity = Binder.clearCallingIdentity();
3988 try {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08003989 defaultPhone.sendDialerSpecialCode(inputCode);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003990 } finally {
3991 Binder.restoreCallingIdentity(identity);
3992 }
fionaxu235cc5e2017-03-06 22:25:57 -08003993 }
3994
Pengquan Menga1bb6272018-09-06 09:59:22 -07003995 @Override
3996 public int getNetworkSelectionMode(int subId) {
shilufc958392020-01-20 11:36:01 -08003997 TelephonyPermissions
Nathan Harold62c68512021-04-06 11:26:02 -07003998 .enforceCallingOrSelfReadPrecisePhoneStatePermissionOrCarrierPrivilege(
shilufc958392020-01-20 11:36:01 -08003999 mApp, subId, "getNetworkSelectionMode");
4000 final long identity = Binder.clearCallingIdentity();
4001 try {
4002 if (!isActiveSubscription(subId)) {
4003 return TelephonyManager.NETWORK_SELECTION_MODE_UNKNOWN;
4004 }
4005 return (int) sendRequest(CMD_GET_NETWORK_SELECTION_MODE, null /* argument */, subId);
4006 } finally {
4007 Binder.restoreCallingIdentity(identity);
Pengquan Menge92a50d2018-09-21 15:54:48 -07004008 }
Pengquan Menga1bb6272018-09-06 09:59:22 -07004009 }
4010
Brad Ebinger35c841c2018-10-01 10:40:55 -07004011 @Override
Brad Ebingerb2b65522019-03-15 13:48:47 -07004012 public boolean isInEmergencySmsMode() {
4013 enforceReadPrivilegedPermission("isInEmergencySmsMode");
4014 final long identity = Binder.clearCallingIdentity();
4015 try {
4016 for (Phone phone : PhoneFactory.getPhones()) {
4017 if (phone.isInEmergencySmsMode()) {
4018 return true;
4019 }
4020 }
4021 } finally {
4022 Binder.restoreCallingIdentity(identity);
4023 }
4024 return false;
4025 }
4026
shilu366312e2019-12-17 09:28:10 -08004027 /**
4028 * Requires carrier privileges or READ_PRECISE_PHONE_STATE permission.
4029 * @param subId The subscription to use to check the configuration.
4030 * @param c The callback that will be used to send the result.
4031 */
Brad Ebingerb2b65522019-03-15 13:48:47 -07004032 @Override
Brad Ebinger9878b0b2018-11-08 17:43:22 -08004033 public void registerImsRegistrationCallback(int subId, IImsRegistrationCallback c)
4034 throws RemoteException {
Nathan Harold62c68512021-04-06 11:26:02 -07004035 TelephonyPermissions.enforceCallingOrSelfReadPrecisePhoneStatePermissionOrCarrierPrivilege(
Rambo Wang37f9c242020-02-10 14:45:28 -08004036 mApp, subId, "registerImsRegistrationCallback");
Brad Ebingera2628302022-02-18 03:44:55 +00004037
4038 if (!ImsManager.isImsSupportedOnDevice(mApp)) {
4039 throw new ServiceSpecificException(ImsException.CODE_ERROR_UNSUPPORTED_OPERATION,
4040 "IMS not available on device.");
4041 }
Brad Ebinger35c841c2018-10-01 10:40:55 -07004042 final long token = Binder.clearCallingIdentity();
4043 try {
Brad Ebingera2628302022-02-18 03:44:55 +00004044 int slotId = getSlotIndexOrException(subId);
4045 verifyImsMmTelConfiguredOrThrow(slotId);
4046 ImsManager.getInstance(mApp, slotId).addRegistrationCallbackForSubscription(c, subId);
Brad Ebinger1ce9c432019-07-16 13:19:44 -07004047 } catch (ImsException e) {
4048 throw new ServiceSpecificException(e.getCode());
Brad Ebinger35c841c2018-10-01 10:40:55 -07004049 } finally {
4050 Binder.restoreCallingIdentity(token);
4051 }
4052 }
4053
shilu366312e2019-12-17 09:28:10 -08004054 /**
4055 * Requires carrier privileges or READ_PRECISE_PHONE_STATE permission.
4056 * @param subId The subscription to use to check the configuration.
4057 * @param c The callback that will be used to send the result.
4058 */
Brad Ebinger35c841c2018-10-01 10:40:55 -07004059 @Override
Brad Ebinger9878b0b2018-11-08 17:43:22 -08004060 public void unregisterImsRegistrationCallback(int subId, IImsRegistrationCallback c) {
Nathan Harold62c68512021-04-06 11:26:02 -07004061 TelephonyPermissions.enforceCallingOrSelfReadPrecisePhoneStatePermissionOrCarrierPrivilege(
Rambo Wang37f9c242020-02-10 14:45:28 -08004062 mApp, subId, "unregisterImsRegistrationCallback");
Brad Ebinger4ae57f92019-01-09 16:51:30 -08004063 if (!SubscriptionManager.isValidSubscriptionId(subId)) {
4064 throw new IllegalArgumentException("Invalid Subscription ID: " + subId);
4065 }
Meng Wangafbc5852019-09-19 17:37:13 -07004066 final long token = Binder.clearCallingIdentity();
4067 try {
Meng Wangafbc5852019-09-19 17:37:13 -07004068 ImsManager.getInstance(mApp, getSlotIndexOrException(subId))
4069 .removeRegistrationCallbackForSubscription(c, subId);
4070 } catch (ImsException e) {
4071 Log.i(LOG_TAG, "unregisterImsRegistrationCallback: " + subId
4072 + "is inactive, ignoring unregister.");
4073 // If the subscription is no longer active, just return, since the callback
4074 // will already have been removed internally.
4075 } finally {
4076 Binder.restoreCallingIdentity(token);
4077 }
Brad Ebinger35c841c2018-10-01 10:40:55 -07004078 }
4079
Brad Ebingera34a6c22019-10-22 17:36:18 -07004080 /**
4081 * Get the IMS service registration state for the MmTelFeature associated with this sub id.
4082 */
4083 @Override
4084 public void getImsMmTelRegistrationState(int subId, IIntegerConsumer consumer) {
4085 enforceReadPrivilegedPermission("getImsMmTelRegistrationState");
4086 if (!ImsManager.isImsSupportedOnDevice(mApp)) {
4087 throw new ServiceSpecificException(ImsException.CODE_ERROR_UNSUPPORTED_OPERATION,
4088 "IMS not available on device.");
4089 }
4090 final long token = Binder.clearCallingIdentity();
4091 try {
4092 Phone phone = getPhone(subId);
4093 if (phone == null) {
4094 Log.w(LOG_TAG, "getImsMmTelRegistrationState: called with an invalid subscription '"
4095 + subId + "'");
4096 throw new ServiceSpecificException(ImsException.CODE_ERROR_INVALID_SUBSCRIPTION);
4097 }
4098 phone.getImsRegistrationState(regState -> {
4099 try {
4100 consumer.accept((regState == null)
4101 ? RegistrationManager.REGISTRATION_STATE_NOT_REGISTERED : regState);
4102 } catch (RemoteException e) {
4103 // Ignore if the remote process is no longer available to call back.
4104 Log.w(LOG_TAG, "getImsMmTelRegistrationState: callback not available.");
4105 }
4106 });
4107 } finally {
4108 Binder.restoreCallingIdentity(token);
4109 }
4110 }
4111
4112 /**
4113 * Get the transport type for the IMS service registration state.
4114 */
4115 @Override
4116 public void getImsMmTelRegistrationTransportType(int subId, IIntegerConsumer consumer) {
Nathan Harold62c68512021-04-06 11:26:02 -07004117 TelephonyPermissions.enforceCallingOrSelfReadPrecisePhoneStatePermissionOrCarrierPrivilege(
Rambo Wang37f9c242020-02-10 14:45:28 -08004118 mApp, subId, "getImsMmTelRegistrationTransportType");
Brad Ebingera34a6c22019-10-22 17:36:18 -07004119 if (!ImsManager.isImsSupportedOnDevice(mApp)) {
4120 throw new ServiceSpecificException(ImsException.CODE_ERROR_UNSUPPORTED_OPERATION,
4121 "IMS not available on device.");
4122 }
4123 final long token = Binder.clearCallingIdentity();
4124 try {
4125 Phone phone = getPhone(subId);
4126 if (phone == null) {
4127 Log.w(LOG_TAG, "getImsMmTelRegistrationState: called with an invalid subscription '"
4128 + subId + "'");
4129 throw new ServiceSpecificException(ImsException.CODE_ERROR_INVALID_SUBSCRIPTION);
4130 }
4131 phone.getImsRegistrationTech(regTech -> {
4132 // Convert registration tech from ImsRegistrationImplBase -> RegistrationManager
4133 int regTechConverted = (regTech == null)
4134 ? ImsRegistrationImplBase.REGISTRATION_TECH_NONE : regTech;
4135 regTechConverted = RegistrationManager.IMS_REG_TO_ACCESS_TYPE_MAP.get(
4136 regTechConverted);
4137 try {
4138 consumer.accept(regTechConverted);
4139 } catch (RemoteException e) {
4140 // Ignore if the remote process is no longer available to call back.
4141 Log.w(LOG_TAG, "getImsMmTelRegistrationState: callback not available.");
4142 }
4143 });
4144 } finally {
4145 Binder.restoreCallingIdentity(token);
4146 }
4147 }
4148
shilu366312e2019-12-17 09:28:10 -08004149 /**
4150 * Requires carrier privileges or READ_PRECISE_PHONE_STATE permission.
4151 * @param subId The subscription to use to check the configuration.
4152 * @param c The callback that will be used to send the result.
4153 */
Brad Ebinger35c841c2018-10-01 10:40:55 -07004154 @Override
Brad Ebinger9878b0b2018-11-08 17:43:22 -08004155 public void registerMmTelCapabilityCallback(int subId, IImsCapabilityCallback c)
4156 throws RemoteException {
Nathan Harold62c68512021-04-06 11:26:02 -07004157 TelephonyPermissions.enforceCallingOrSelfReadPrecisePhoneStatePermissionOrCarrierPrivilege(
Rambo Wang37f9c242020-02-10 14:45:28 -08004158 mApp, subId, "registerMmTelCapabilityCallback");
Brad Ebingera2628302022-02-18 03:44:55 +00004159 if (!ImsManager.isImsSupportedOnDevice(mApp)) {
4160 throw new ServiceSpecificException(ImsException.CODE_ERROR_UNSUPPORTED_OPERATION,
4161 "IMS not available on device.");
4162 }
Brad Ebinger35c841c2018-10-01 10:40:55 -07004163 final long token = Binder.clearCallingIdentity();
4164 try {
Brad Ebingera2628302022-02-18 03:44:55 +00004165 int slotId = getSlotIndexOrException(subId);
4166 verifyImsMmTelConfiguredOrThrow(slotId);
4167 ImsManager.getInstance(mApp, slotId).addCapabilitiesCallbackForSubscription(c, subId);
Brad Ebinger1ce9c432019-07-16 13:19:44 -07004168 } catch (ImsException e) {
4169 throw new ServiceSpecificException(e.getCode());
Brad Ebinger35c841c2018-10-01 10:40:55 -07004170 } finally {
4171 Binder.restoreCallingIdentity(token);
4172 }
4173 }
4174
shilu366312e2019-12-17 09:28:10 -08004175 /**
4176 * Requires carrier privileges or READ_PRECISE_PHONE_STATE permission.
4177 * @param subId The subscription to use to check the configuration.
4178 * @param c The callback that will be used to send the result.
4179 */
Brad Ebinger35c841c2018-10-01 10:40:55 -07004180 @Override
Brad Ebinger9878b0b2018-11-08 17:43:22 -08004181 public void unregisterMmTelCapabilityCallback(int subId, IImsCapabilityCallback c) {
Nathan Harold62c68512021-04-06 11:26:02 -07004182 TelephonyPermissions.enforceCallingOrSelfReadPrecisePhoneStatePermissionOrCarrierPrivilege(
Rambo Wang37f9c242020-02-10 14:45:28 -08004183 mApp, subId, "unregisterMmTelCapabilityCallback");
Brad Ebinger4ae57f92019-01-09 16:51:30 -08004184 if (!SubscriptionManager.isValidSubscriptionId(subId)) {
4185 throw new IllegalArgumentException("Invalid Subscription ID: " + subId);
4186 }
Meng Wangafbc5852019-09-19 17:37:13 -07004187
4188 final long token = Binder.clearCallingIdentity();
4189 try {
Meng Wangafbc5852019-09-19 17:37:13 -07004190 ImsManager.getInstance(mApp, getSlotIndexOrException(subId))
Brad Ebinger4ae57f92019-01-09 16:51:30 -08004191 .removeCapabilitiesCallbackForSubscription(c, subId);
Meng Wangafbc5852019-09-19 17:37:13 -07004192 } catch (ImsException e) {
4193 Log.i(LOG_TAG, "unregisterMmTelCapabilityCallback: " + subId
4194 + "is inactive, ignoring unregister.");
4195 // If the subscription is no longer active, just return, since the callback
4196 // will already have been removed internally.
4197 } finally {
4198 Binder.restoreCallingIdentity(token);
4199 }
Brad Ebinger35c841c2018-10-01 10:40:55 -07004200 }
4201
4202 @Override
Brad Ebinger9878b0b2018-11-08 17:43:22 -08004203 public boolean isCapable(int subId, int capability, int regTech) {
4204 enforceReadPrivilegedPermission("isCapable");
Brad Ebinger35c841c2018-10-01 10:40:55 -07004205 final long token = Binder.clearCallingIdentity();
4206 try {
Brad Ebingera2628302022-02-18 03:44:55 +00004207 int slotId = getSlotIndexOrException(subId);
4208 verifyImsMmTelConfiguredOrThrow(slotId);
4209 return ImsManager.getInstance(mApp, slotId).queryMmTelCapability(capability, regTech);
4210 } catch (com.android.ims.ImsException e) {
4211 Log.w(LOG_TAG, "IMS isCapable - service unavailable: " + e.getMessage());
4212 return false;
Brad Ebinger1ce9c432019-07-16 13:19:44 -07004213 } catch (ImsException e) {
Brad Ebinger6b5ac222019-02-04 14:36:52 -08004214 Log.i(LOG_TAG, "isCapable: " + subId + " is inactive, returning false.");
4215 return false;
Brad Ebinger35c841c2018-10-01 10:40:55 -07004216 } finally {
4217 Binder.restoreCallingIdentity(token);
4218 }
4219 }
4220
4221 @Override
Brad Ebinger9878b0b2018-11-08 17:43:22 -08004222 public boolean isAvailable(int subId, int capability, int regTech) {
4223 enforceReadPrivilegedPermission("isAvailable");
Brad Ebinger35c841c2018-10-01 10:40:55 -07004224 final long token = Binder.clearCallingIdentity();
4225 try {
4226 Phone phone = getPhone(subId);
4227 if (phone == null) return false;
4228 return phone.isImsCapabilityAvailable(capability, regTech);
Daniel Bright5e40e4e2020-03-11 16:35:39 -07004229 } catch (com.android.ims.ImsException e) {
4230 Log.w(LOG_TAG, "IMS isAvailable - service unavailable: " + e.getMessage());
4231 return false;
Brad Ebinger35c841c2018-10-01 10:40:55 -07004232 } finally {
4233 Binder.restoreCallingIdentity(token);
4234 }
4235 }
4236
Brad Ebingerbc7dd582019-10-17 17:03:22 -07004237 /**
4238 * Determines if the MmTel feature capability is supported by the carrier configuration for this
4239 * subscription.
4240 * @param subId The subscription to use to check the configuration.
4241 * @param callback The callback that will be used to send the result.
4242 * @param capability The MmTelFeature capability that will be used to send the result.
4243 * @param transportType The transport type of the MmTelFeature capability.
4244 */
4245 @Override
4246 public void isMmTelCapabilitySupported(int subId, IIntegerConsumer callback, int capability,
4247 int transportType) {
4248 enforceReadPrivilegedPermission("isMmTelCapabilitySupported");
Brad Ebingera2628302022-02-18 03:44:55 +00004249 if (!ImsManager.isImsSupportedOnDevice(mApp)) {
4250 throw new ServiceSpecificException(ImsException.CODE_ERROR_UNSUPPORTED_OPERATION,
4251 "IMS not available on device.");
4252 }
Brad Ebingerbc7dd582019-10-17 17:03:22 -07004253 final long token = Binder.clearCallingIdentity();
4254 try {
Brad Ebingera2628302022-02-18 03:44:55 +00004255 int slotId = getSlotIndex(subId);
4256 if (slotId <= SubscriptionManager.INVALID_SIM_SLOT_INDEX) {
4257 Log.w(LOG_TAG, "isMmTelCapabilitySupported: called with an inactive subscription '"
4258 + subId + "'");
4259 throw new ServiceSpecificException(ImsException.CODE_ERROR_INVALID_SUBSCRIPTION);
4260 }
4261 verifyImsMmTelConfiguredOrThrow(slotId);
4262 ImsManager.getInstance(mApp, slotId).isSupported(capability,
4263 transportType, aBoolean -> {
4264 try {
4265 callback.accept((aBoolean == null) ? 0 : (aBoolean ? 1 : 0));
4266 } catch (RemoteException e) {
4267 Log.w(LOG_TAG, "isMmTelCapabilitySupported: remote caller is not "
4268 + "running. Ignore");
4269 }
4270 });
Brad Ebinger919631e2021-06-02 17:46:35 -07004271 } catch (ImsException e) {
4272 throw new ServiceSpecificException(e.getCode());
Brad Ebingerbc7dd582019-10-17 17:03:22 -07004273 } finally {
4274 Binder.restoreCallingIdentity(token);
4275 }
4276 }
4277
shilu366312e2019-12-17 09:28:10 -08004278 /**
4279 * Requires carrier privileges or READ_PRECISE_PHONE_STATE permission.
4280 * @param subId The subscription to use to check the configuration.
4281 */
Brad Ebinger35c841c2018-10-01 10:40:55 -07004282 @Override
4283 public boolean isAdvancedCallingSettingEnabled(int subId) {
Nathan Harold62c68512021-04-06 11:26:02 -07004284 TelephonyPermissions.enforceCallingOrSelfReadPrecisePhoneStatePermissionOrCarrierPrivilege(
Rambo Wang37f9c242020-02-10 14:45:28 -08004285 mApp, subId, "isAdvancedCallingSettingEnabled");
shilu366312e2019-12-17 09:28:10 -08004286
Brad Ebinger35c841c2018-10-01 10:40:55 -07004287 final long token = Binder.clearCallingIdentity();
4288 try {
Brad Ebinger919631e2021-06-02 17:46:35 -07004289 int slotId = getSlotIndexOrException(subId);
Brad Ebinger735c5ce2021-07-12 13:58:21 -07004290 // This setting doesn't require an active ImsService connection, so do not verify.
Brad Ebinger919631e2021-06-02 17:46:35 -07004291 return ImsManager.getInstance(mApp, slotId).isEnhanced4gLteModeSettingEnabledByUser();
Brad Ebinger1ce9c432019-07-16 13:19:44 -07004292 } catch (ImsException e) {
4293 throw new ServiceSpecificException(e.getCode());
Brad Ebinger35c841c2018-10-01 10:40:55 -07004294 } finally {
4295 Binder.restoreCallingIdentity(token);
4296 }
4297 }
4298
4299 @Override
Brad Ebinger1c162042019-02-21 14:49:10 -08004300 public void setAdvancedCallingSettingEnabled(int subId, boolean isEnabled) {
Brad Ebinger35c841c2018-10-01 10:40:55 -07004301 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(mApp, subId,
Brad Ebinger1c162042019-02-21 14:49:10 -08004302 "setAdvancedCallingSettingEnabled");
Brad Ebinger35c841c2018-10-01 10:40:55 -07004303 final long identity = Binder.clearCallingIdentity();
4304 try {
Brad Ebinger919631e2021-06-02 17:46:35 -07004305 int slotId = getSlotIndexOrException(subId);
Brad Ebinger735c5ce2021-07-12 13:58:21 -07004306 // This setting doesn't require an active ImsService connection, so do not verify. The
4307 // new setting will be picked up when the ImsService comes up next if it isn't up.
Brad Ebinger919631e2021-06-02 17:46:35 -07004308 ImsManager.getInstance(mApp, slotId).setEnhanced4gLteModeSetting(isEnabled);
Brad Ebinger1ce9c432019-07-16 13:19:44 -07004309 } catch (ImsException e) {
4310 throw new ServiceSpecificException(e.getCode());
Brad Ebinger35c841c2018-10-01 10:40:55 -07004311 } finally {
4312 Binder.restoreCallingIdentity(identity);
4313 }
4314 }
4315
shilu366312e2019-12-17 09:28:10 -08004316 /**
4317 * Requires carrier privileges or READ_PRECISE_PHONE_STATE permission.
4318 * @param subId The subscription to use to check the configuration.
4319 */
Brad Ebinger35c841c2018-10-01 10:40:55 -07004320 @Override
Brad Ebinger9878b0b2018-11-08 17:43:22 -08004321 public boolean isVtSettingEnabled(int subId) {
Nathan Harold62c68512021-04-06 11:26:02 -07004322 TelephonyPermissions.enforceCallingOrSelfReadPrecisePhoneStatePermissionOrCarrierPrivilege(
Rambo Wang37f9c242020-02-10 14:45:28 -08004323 mApp, subId, "isVtSettingEnabled");
Brad Ebinger35c841c2018-10-01 10:40:55 -07004324 final long identity = Binder.clearCallingIdentity();
4325 try {
Brad Ebinger919631e2021-06-02 17:46:35 -07004326 int slotId = getSlotIndexOrException(subId);
Brad Ebinger735c5ce2021-07-12 13:58:21 -07004327 // This setting doesn't require an active ImsService connection, so do not verify.
Brad Ebinger919631e2021-06-02 17:46:35 -07004328 return ImsManager.getInstance(mApp, slotId).isVtEnabledByUser();
Brad Ebinger1ce9c432019-07-16 13:19:44 -07004329 } catch (ImsException e) {
4330 throw new ServiceSpecificException(e.getCode());
Brad Ebinger35c841c2018-10-01 10:40:55 -07004331 } finally {
4332 Binder.restoreCallingIdentity(identity);
4333 }
4334 }
4335
4336 @Override
Brad Ebinger1c162042019-02-21 14:49:10 -08004337 public void setVtSettingEnabled(int subId, boolean isEnabled) {
Brad Ebinger35c841c2018-10-01 10:40:55 -07004338 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(mApp, subId,
Brad Ebinger1c162042019-02-21 14:49:10 -08004339 "setVtSettingEnabled");
Brad Ebinger35c841c2018-10-01 10:40:55 -07004340 final long identity = Binder.clearCallingIdentity();
4341 try {
Brad Ebinger919631e2021-06-02 17:46:35 -07004342 int slotId = getSlotIndexOrException(subId);
Brad Ebinger735c5ce2021-07-12 13:58:21 -07004343 // This setting doesn't require an active ImsService connection, so do not verify. The
4344 // new setting will be picked up when the ImsService comes up next if it isn't up.
Brad Ebinger919631e2021-06-02 17:46:35 -07004345 ImsManager.getInstance(mApp, slotId).setVtSetting(isEnabled);
Brad Ebinger1ce9c432019-07-16 13:19:44 -07004346 } catch (ImsException e) {
4347 throw new ServiceSpecificException(e.getCode());
Brad Ebinger35c841c2018-10-01 10:40:55 -07004348 } finally {
4349 Binder.restoreCallingIdentity(identity);
4350 }
4351 }
4352
shilu366312e2019-12-17 09:28:10 -08004353 /**
4354 * Requires carrier privileges or READ_PRECISE_PHONE_STATE permission.
4355 * @param subId The subscription to use to check the configuration.
4356 */
Brad Ebinger35c841c2018-10-01 10:40:55 -07004357 @Override
4358 public boolean isVoWiFiSettingEnabled(int subId) {
Nathan Harold62c68512021-04-06 11:26:02 -07004359 TelephonyPermissions.enforceCallingOrSelfReadPrecisePhoneStatePermissionOrCarrierPrivilege(
Rambo Wang37f9c242020-02-10 14:45:28 -08004360 mApp, subId, "isVoWiFiSettingEnabled");
Brad Ebinger35c841c2018-10-01 10:40:55 -07004361 final long identity = Binder.clearCallingIdentity();
4362 try {
Brad Ebinger919631e2021-06-02 17:46:35 -07004363 int slotId = getSlotIndexOrException(subId);
Brad Ebinger735c5ce2021-07-12 13:58:21 -07004364 // This setting doesn't require an active ImsService connection, so do not verify.
Brad Ebinger919631e2021-06-02 17:46:35 -07004365 return ImsManager.getInstance(mApp, slotId).isWfcEnabledByUser();
Brad Ebinger1ce9c432019-07-16 13:19:44 -07004366 } catch (ImsException e) {
4367 throw new ServiceSpecificException(e.getCode());
Brad Ebinger35c841c2018-10-01 10:40:55 -07004368 } finally {
4369 Binder.restoreCallingIdentity(identity);
4370 }
4371 }
4372
4373 @Override
Brad Ebinger1c162042019-02-21 14:49:10 -08004374 public void setVoWiFiSettingEnabled(int subId, boolean isEnabled) {
Brad Ebinger35c841c2018-10-01 10:40:55 -07004375 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(mApp, subId,
Brad Ebinger1c162042019-02-21 14:49:10 -08004376 "setVoWiFiSettingEnabled");
Brad Ebinger35c841c2018-10-01 10:40:55 -07004377 final long identity = Binder.clearCallingIdentity();
4378 try {
Brad Ebinger919631e2021-06-02 17:46:35 -07004379 int slotId = getSlotIndexOrException(subId);
Brad Ebinger735c5ce2021-07-12 13:58:21 -07004380 // This setting doesn't require an active ImsService connection, so do not verify. The
4381 // new setting will be picked up when the ImsService comes up next if it isn't up.
Brad Ebinger919631e2021-06-02 17:46:35 -07004382 ImsManager.getInstance(mApp, slotId).setWfcSetting(isEnabled);
Brad Ebinger1ce9c432019-07-16 13:19:44 -07004383 } catch (ImsException e) {
4384 throw new ServiceSpecificException(e.getCode());
Brad Ebinger35c841c2018-10-01 10:40:55 -07004385 } finally {
4386 Binder.restoreCallingIdentity(identity);
4387 }
4388 }
4389
shilu366312e2019-12-17 09:28:10 -08004390 /**
Sooraj Sasindrane655add2020-11-23 19:40:38 -08004391 * @return true if the user's setting for Voice over Cross SIM is enabled and false if it is not
4392 * Requires carrier privileges or READ_PRECISE_PHONE_STATE permission.
4393 * @param subId The subscription to use to check the configuration.
4394 */
4395 @Override
4396 public boolean isCrossSimCallingEnabledByUser(int subId) {
Nathan Harold62c68512021-04-06 11:26:02 -07004397 TelephonyPermissions.enforceCallingOrSelfReadPrecisePhoneStatePermissionOrCarrierPrivilege(
Sooraj Sasindrane655add2020-11-23 19:40:38 -08004398 mApp, subId, "isCrossSimCallingEnabledByUser");
4399 final long identity = Binder.clearCallingIdentity();
4400 try {
Brad Ebinger919631e2021-06-02 17:46:35 -07004401 int slotId = getSlotIndexOrException(subId);
Brad Ebinger735c5ce2021-07-12 13:58:21 -07004402 // This setting doesn't require an active ImsService connection, so do not verify.
Brad Ebinger919631e2021-06-02 17:46:35 -07004403 return ImsManager.getInstance(mApp, slotId).isCrossSimCallingEnabledByUser();
Sooraj Sasindrane655add2020-11-23 19:40:38 -08004404 } catch (ImsException e) {
4405 throw new ServiceSpecificException(e.getCode());
4406 } finally {
4407 Binder.restoreCallingIdentity(identity);
4408 }
4409 }
4410
4411 /**
4412 * Sets the user's setting for whether or not Voice over Cross SIM is enabled.
4413 * Requires MODIFY_PHONE_STATE permission.
4414 * @param subId The subscription to use to check the configuration.
4415 * @param isEnabled true if the user's setting for Voice over Cross SIM is enabled,
4416 * false otherwise
4417 */
4418 @Override
4419 public void setCrossSimCallingEnabled(int subId, boolean isEnabled) {
4420 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(mApp, subId,
4421 "setCrossSimCallingEnabled");
4422 final long identity = Binder.clearCallingIdentity();
4423 try {
Brad Ebinger919631e2021-06-02 17:46:35 -07004424 int slotId = getSlotIndexOrException(subId);
Brad Ebinger735c5ce2021-07-12 13:58:21 -07004425 // This setting doesn't require an active ImsService connection, so do not verify. The
4426 // new setting will be picked up when the ImsService comes up next if it isn't up.
Brad Ebinger919631e2021-06-02 17:46:35 -07004427 ImsManager.getInstance(mApp, slotId).setCrossSimCallingEnabled(isEnabled);
Sooraj Sasindrane655add2020-11-23 19:40:38 -08004428 } catch (ImsException e) {
4429 throw new ServiceSpecificException(e.getCode());
4430 } finally {
4431 Binder.restoreCallingIdentity(identity);
4432 }
4433 }
4434
4435 /**
shilu366312e2019-12-17 09:28:10 -08004436 * Requires carrier privileges or READ_PRECISE_PHONE_STATE permission.
4437 * @param subId The subscription to use to check the configuration.
4438 */
Brad Ebinger35c841c2018-10-01 10:40:55 -07004439 @Override
Nathan Harold62c68512021-04-06 11:26:02 -07004440
Brad Ebinger35c841c2018-10-01 10:40:55 -07004441 public boolean isVoWiFiRoamingSettingEnabled(int subId) {
Nathan Harold62c68512021-04-06 11:26:02 -07004442 TelephonyPermissions.enforceCallingOrSelfReadPrecisePhoneStatePermissionOrCarrierPrivilege(
Rambo Wang37f9c242020-02-10 14:45:28 -08004443 mApp, subId, "isVoWiFiRoamingSettingEnabled");
Brad Ebinger35c841c2018-10-01 10:40:55 -07004444 final long identity = Binder.clearCallingIdentity();
4445 try {
Brad Ebinger919631e2021-06-02 17:46:35 -07004446 int slotId = getSlotIndexOrException(subId);
Brad Ebinger735c5ce2021-07-12 13:58:21 -07004447 // This setting doesn't require an active ImsService connection, so do not verify.
Brad Ebinger919631e2021-06-02 17:46:35 -07004448 return ImsManager.getInstance(mApp, slotId).isWfcRoamingEnabledByUser();
Brad Ebinger1ce9c432019-07-16 13:19:44 -07004449 } catch (ImsException e) {
4450 throw new ServiceSpecificException(e.getCode());
Brad Ebinger35c841c2018-10-01 10:40:55 -07004451 } finally {
4452 Binder.restoreCallingIdentity(identity);
4453 }
4454 }
4455
4456 @Override
Brad Ebinger1c162042019-02-21 14:49:10 -08004457 public void setVoWiFiRoamingSettingEnabled(int subId, boolean isEnabled) {
Brad Ebinger35c841c2018-10-01 10:40:55 -07004458 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(mApp, subId,
Brad Ebinger1c162042019-02-21 14:49:10 -08004459 "setVoWiFiRoamingSettingEnabled");
Brad Ebinger35c841c2018-10-01 10:40:55 -07004460 final long identity = Binder.clearCallingIdentity();
4461 try {
Brad Ebinger919631e2021-06-02 17:46:35 -07004462 int slotId = getSlotIndexOrException(subId);
Brad Ebinger735c5ce2021-07-12 13:58:21 -07004463 // This setting doesn't require an active ImsService connection, so do not verify. The
4464 // new setting will be picked up when the ImsService comes up next if it isn't up.
Brad Ebinger919631e2021-06-02 17:46:35 -07004465 ImsManager.getInstance(mApp, slotId).setWfcRoamingSetting(isEnabled);
Brad Ebinger1ce9c432019-07-16 13:19:44 -07004466 } catch (ImsException e) {
4467 throw new ServiceSpecificException(e.getCode());
Brad Ebinger35c841c2018-10-01 10:40:55 -07004468 } finally {
4469 Binder.restoreCallingIdentity(identity);
4470 }
4471 }
4472
4473 @Override
4474 public void setVoWiFiNonPersistent(int subId, boolean isCapable, int mode) {
4475 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(mApp, subId,
4476 "setVoWiFiNonPersistent");
4477 final long identity = Binder.clearCallingIdentity();
4478 try {
Brad Ebinger919631e2021-06-02 17:46:35 -07004479 int slotId = getSlotIndexOrException(subId);
Brad Ebinger735c5ce2021-07-12 13:58:21 -07004480 // This setting will be ignored if the ImsService isn't up.
Brad Ebinger919631e2021-06-02 17:46:35 -07004481 ImsManager.getInstance(mApp, slotId).setWfcNonPersistent(isCapable, mode);
Brad Ebinger1ce9c432019-07-16 13:19:44 -07004482 } catch (ImsException e) {
4483 throw new ServiceSpecificException(e.getCode());
Brad Ebinger35c841c2018-10-01 10:40:55 -07004484 } finally {
4485 Binder.restoreCallingIdentity(identity);
4486 }
4487 }
4488
shilu366312e2019-12-17 09:28:10 -08004489 /**
4490 * Requires carrier privileges or READ_PRECISE_PHONE_STATE permission.
4491 * @param subId The subscription to use to check the configuration.
4492 */
Brad Ebinger35c841c2018-10-01 10:40:55 -07004493 @Override
4494 public int getVoWiFiModeSetting(int subId) {
Nathan Harold62c68512021-04-06 11:26:02 -07004495 TelephonyPermissions.enforceCallingOrSelfReadPrecisePhoneStatePermissionOrCarrierPrivilege(
Rambo Wang37f9c242020-02-10 14:45:28 -08004496 mApp, subId, "getVoWiFiModeSetting");
Brad Ebinger35c841c2018-10-01 10:40:55 -07004497 final long identity = Binder.clearCallingIdentity();
4498 try {
Brad Ebinger919631e2021-06-02 17:46:35 -07004499 int slotId = getSlotIndexOrException(subId);
Brad Ebinger735c5ce2021-07-12 13:58:21 -07004500 // This setting doesn't require an active ImsService connection, so do not verify.
Brad Ebinger919631e2021-06-02 17:46:35 -07004501 return ImsManager.getInstance(mApp, slotId).getWfcMode(false /*isRoaming*/);
Brad Ebinger1ce9c432019-07-16 13:19:44 -07004502 } catch (ImsException e) {
4503 throw new ServiceSpecificException(e.getCode());
Brad Ebinger35c841c2018-10-01 10:40:55 -07004504 } finally {
4505 Binder.restoreCallingIdentity(identity);
4506 }
4507 }
4508
4509 @Override
4510 public void setVoWiFiModeSetting(int subId, int mode) {
4511 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(mApp, subId,
4512 "setVoWiFiModeSetting");
4513 final long identity = Binder.clearCallingIdentity();
4514 try {
Brad Ebinger919631e2021-06-02 17:46:35 -07004515 int slotId = getSlotIndexOrException(subId);
Brad Ebinger735c5ce2021-07-12 13:58:21 -07004516 // This setting doesn't require an active ImsService connection, so do not verify. The
4517 // new setting will be picked up when the ImsService comes up next if it isn't up.
Brad Ebinger919631e2021-06-02 17:46:35 -07004518 ImsManager.getInstance(mApp, slotId).setWfcMode(mode, false /*isRoaming*/);
Brad Ebinger1ce9c432019-07-16 13:19:44 -07004519 } catch (ImsException e) {
4520 throw new ServiceSpecificException(e.getCode());
Brad Ebinger35c841c2018-10-01 10:40:55 -07004521 } finally {
4522 Binder.restoreCallingIdentity(identity);
4523 }
4524 }
4525
4526 @Override
4527 public int getVoWiFiRoamingModeSetting(int subId) {
4528 enforceReadPrivilegedPermission("getVoWiFiRoamingModeSetting");
4529 final long identity = Binder.clearCallingIdentity();
4530 try {
Brad Ebinger919631e2021-06-02 17:46:35 -07004531 int slotId = getSlotIndexOrException(subId);
Brad Ebinger735c5ce2021-07-12 13:58:21 -07004532 // This setting doesn't require an active ImsService connection, so do not verify.
Brad Ebinger919631e2021-06-02 17:46:35 -07004533 return ImsManager.getInstance(mApp, slotId).getWfcMode(true /*isRoaming*/);
Brad Ebinger1ce9c432019-07-16 13:19:44 -07004534 } catch (ImsException e) {
4535 throw new ServiceSpecificException(e.getCode());
Brad Ebinger35c841c2018-10-01 10:40:55 -07004536 } finally {
4537 Binder.restoreCallingIdentity(identity);
4538 }
4539 }
4540
4541 @Override
4542 public void setVoWiFiRoamingModeSetting(int subId, int mode) {
4543 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(mApp, subId,
4544 "setVoWiFiRoamingModeSetting");
4545 final long identity = Binder.clearCallingIdentity();
4546 try {
Brad Ebinger919631e2021-06-02 17:46:35 -07004547 int slotId = getSlotIndexOrException(subId);
Brad Ebinger735c5ce2021-07-12 13:58:21 -07004548 // This setting doesn't require an active ImsService connection, so do not verify. The
4549 // new setting will be picked up when the ImsService comes up next if it isn't up.
Brad Ebinger919631e2021-06-02 17:46:35 -07004550 ImsManager.getInstance(mApp, slotId).setWfcMode(mode, true /*isRoaming*/);
Brad Ebinger1ce9c432019-07-16 13:19:44 -07004551 } catch (ImsException e) {
4552 throw new ServiceSpecificException(e.getCode());
Brad Ebinger35c841c2018-10-01 10:40:55 -07004553 } finally {
4554 Binder.restoreCallingIdentity(identity);
4555 }
4556 }
4557
4558 @Override
4559 public void setRttCapabilitySetting(int subId, boolean isEnabled) {
4560 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(mApp, subId,
4561 "setRttCapabilityEnabled");
4562 final long identity = Binder.clearCallingIdentity();
4563 try {
Brad Ebinger919631e2021-06-02 17:46:35 -07004564 int slotId = getSlotIndexOrException(subId);
Brad Ebinger735c5ce2021-07-12 13:58:21 -07004565 // This setting doesn't require an active ImsService connection, so do not verify. The
4566 // new setting will be picked up when the ImsService comes up next if it isn't up.
Brad Ebinger919631e2021-06-02 17:46:35 -07004567 ImsManager.getInstance(mApp, slotId).setRttEnabled(isEnabled);
Brad Ebinger1ce9c432019-07-16 13:19:44 -07004568 } catch (ImsException e) {
4569 throw new ServiceSpecificException(e.getCode());
Brad Ebinger35c841c2018-10-01 10:40:55 -07004570 } finally {
4571 Binder.restoreCallingIdentity(identity);
4572 }
4573 }
4574
shilu366312e2019-12-17 09:28:10 -08004575 /**
4576 * Requires carrier privileges or READ_PRECISE_PHONE_STATE permission.
4577 * @param subId The subscription to use to check the configuration.
4578 */
Brad Ebinger35c841c2018-10-01 10:40:55 -07004579 @Override
4580 public boolean isTtyOverVolteEnabled(int subId) {
Nathan Harold62c68512021-04-06 11:26:02 -07004581 TelephonyPermissions.enforceCallingOrSelfReadPrecisePhoneStatePermissionOrCarrierPrivilege(
Rambo Wang37f9c242020-02-10 14:45:28 -08004582 mApp, subId, "isTtyOverVolteEnabled");
Brad Ebinger35c841c2018-10-01 10:40:55 -07004583 final long identity = Binder.clearCallingIdentity();
4584 try {
Brad Ebinger919631e2021-06-02 17:46:35 -07004585 int slotId = getSlotIndexOrException(subId);
Brad Ebinger735c5ce2021-07-12 13:58:21 -07004586 // This setting doesn't require an active ImsService connection, so do not verify.
Brad Ebinger919631e2021-06-02 17:46:35 -07004587 return ImsManager.getInstance(mApp, slotId).isTtyOnVoLteCapable();
Brad Ebinger1ce9c432019-07-16 13:19:44 -07004588 } catch (ImsException e) {
4589 throw new ServiceSpecificException(e.getCode());
Brad Ebinger35c841c2018-10-01 10:40:55 -07004590 } finally {
4591 Binder.restoreCallingIdentity(identity);
4592 }
4593 }
4594
Brad Ebingerdf5b4f02018-10-31 11:24:17 -07004595 @Override
4596 public void registerImsProvisioningChangedCallback(int subId, IImsConfigCallback callback) {
4597 enforceReadPrivilegedPermission("registerImsProvisioningChangedCallback");
joonhunshincffb7fc2021-11-28 07:32:01 +00004598
Brad Ebingerdf5b4f02018-10-31 11:24:17 -07004599 final long identity = Binder.clearCallingIdentity();
4600 try {
Brad Ebingera2628302022-02-18 03:44:55 +00004601 if (!isImsAvailableOnDevice()) {
4602 throw new ServiceSpecificException(ImsException.CODE_ERROR_UNSUPPORTED_OPERATION,
4603 "IMS not available on device.");
4604 }
4605 int slotId = getSlotIndexOrException(subId);
4606 verifyImsMmTelConfiguredOrThrow(slotId);
4607 ImsManager.getInstance(mApp, slotId)
4608 .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");
joonhunshincffb7fc2021-11-28 07:32:01 +00004619
Brad Ebingerdf5b4f02018-10-31 11:24:17 -07004620 final long identity = Binder.clearCallingIdentity();
Brad Ebinger4ae57f92019-01-09 16:51:30 -08004621 if (!SubscriptionManager.isValidSubscriptionId(subId)) {
4622 throw new IllegalArgumentException("Invalid Subscription ID: " + subId);
4623 }
Brad Ebingerdf5b4f02018-10-31 11:24:17 -07004624 try {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08004625 ImsManager.getInstance(mApp, getSlotIndexOrException(subId))
Brad Ebinger4ae57f92019-01-09 16:51:30 -08004626 .removeProvisioningCallbackForSubscription(callback, subId);
Brad Ebinger1ce9c432019-07-16 13:19:44 -07004627 } catch (ImsException e) {
Brad Ebinger4ae57f92019-01-09 16:51:30 -08004628 Log.i(LOG_TAG, "unregisterImsProvisioningChangedCallback: " + subId
4629 + "is inactive, ignoring unregister.");
4630 // If the subscription is no longer active, just return, since the callback will already
4631 // have been removed internally.
Brad Ebingerdf5b4f02018-10-31 11:24:17 -07004632 } finally {
4633 Binder.restoreCallingIdentity(identity);
4634 }
4635 }
4636
joonhunshincffb7fc2021-11-28 07:32:01 +00004637 @Override
4638 public void registerFeatureProvisioningChangedCallback(int subId,
4639 IFeatureProvisioningCallback callback) {
4640 TelephonyPermissions.enforceCallingOrSelfReadPrecisePhoneStatePermissionOrCarrierPrivilege(
4641 mApp, subId, "registerFeatureProvisioningChangedCallback");
4642
4643 final long identity = Binder.clearCallingIdentity();
4644 if (!SubscriptionManager.isValidSubscriptionId(subId)) {
4645 throw new IllegalArgumentException("Invalid Subscription ID: " + subId);
4646 }
4647
joonhunshin91bc1952022-04-29 08:47:15 +00004648 try {
4649 ImsProvisioningController controller = ImsProvisioningController.getInstance();
4650 if (controller == null) {
4651 throw new ServiceSpecificException(ImsException.CODE_ERROR_UNSUPPORTED_OPERATION,
4652 "Device does not support IMS");
4653 }
4654 controller.addFeatureProvisioningChangedCallback(subId, callback);
4655 } finally {
4656 Binder.restoreCallingIdentity(identity);
4657 }
joonhunshincffb7fc2021-11-28 07:32:01 +00004658 }
4659
4660 @Override
4661 public void unregisterFeatureProvisioningChangedCallback(int subId,
4662 IFeatureProvisioningCallback callback) {
4663 TelephonyPermissions.enforceCallingOrSelfReadPrecisePhoneStatePermissionOrCarrierPrivilege(
4664 mApp, subId, "unregisterFeatureProvisioningChangedCallback");
4665
4666 final long identity = Binder.clearCallingIdentity();
4667 if (!SubscriptionManager.isValidSubscriptionId(subId)) {
4668 throw new IllegalArgumentException("Invalid Subscription ID: " + subId);
4669 }
4670
joonhunshin91bc1952022-04-29 08:47:15 +00004671 try {
4672 ImsProvisioningController controller = ImsProvisioningController.getInstance();
4673 if (controller == null) {
4674 loge("unregisterFeatureProvisioningChangedCallback: Device does not support IMS");
4675 return;
4676 }
4677 controller.removeFeatureProvisioningChangedCallback(subId, callback);
4678 } finally {
4679 Binder.restoreCallingIdentity(identity);
4680 }
joonhunshincffb7fc2021-11-28 07:32:01 +00004681 }
allenwtsu99c623b2020-01-03 18:24:23 +08004682
4683 private void checkModifyPhoneStatePermission(int subId, String message) {
4684 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(mApp, subId,
4685 message);
4686 }
4687
allenwtsu99c623b2020-01-03 18:24:23 +08004688 @Override
joonhunshincffb7fc2021-11-28 07:32:01 +00004689 public void setRcsProvisioningStatusForCapability(int subId, int capability, int tech,
allenwtsu99c623b2020-01-03 18:24:23 +08004690 boolean isProvisioned) {
4691 checkModifyPhoneStatePermission(subId, "setRcsProvisioningStatusForCapability");
4692
4693 final long identity = Binder.clearCallingIdentity();
4694 try {
joonhunshin91bc1952022-04-29 08:47:15 +00004695 ImsProvisioningController controller = ImsProvisioningController.getInstance();
4696 if (controller == null) {
4697 loge("setRcsProvisioningStatusForCapability: Device does not support IMS");
4698 return;
4699 }
4700 controller.setRcsProvisioningStatusForCapability(
4701 subId, capability, tech, isProvisioned);
allenwtsu99c623b2020-01-03 18:24:23 +08004702 } finally {
4703 Binder.restoreCallingIdentity(identity);
4704 }
allenwtsu99c623b2020-01-03 18:24:23 +08004705 }
4706
4707
4708 @Override
joonhunshincffb7fc2021-11-28 07:32:01 +00004709 public boolean getRcsProvisioningStatusForCapability(int subId, int capability, int tech) {
4710 TelephonyPermissions.enforceCallingOrSelfReadPrecisePhoneStatePermissionOrCarrierPrivilege(
4711 mApp, subId, "getRcsProvisioningStatusForCapability");
4712
allenwtsu99c623b2020-01-03 18:24:23 +08004713 final long identity = Binder.clearCallingIdentity();
4714 try {
joonhunshin91bc1952022-04-29 08:47:15 +00004715 ImsProvisioningController controller = ImsProvisioningController.getInstance();
4716 if (controller == null) {
4717 loge("getRcsProvisioningStatusForCapability: Device does not support IMS");
4718
4719 // device does not support IMS, this method will return true always.
4720 return true;
4721 }
4722 return controller.getRcsProvisioningStatusForCapability(subId, capability, tech);
allenwtsu99c623b2020-01-03 18:24:23 +08004723 } finally {
4724 Binder.restoreCallingIdentity(identity);
4725 }
4726 }
4727
Brad Ebingerdf5b4f02018-10-31 11:24:17 -07004728 @Override
Brad Ebinger1c8542e2019-01-14 13:43:14 -08004729 public void setImsProvisioningStatusForCapability(int subId, int capability, int tech,
4730 boolean isProvisioned) {
allenwtsu99c623b2020-01-03 18:24:23 +08004731 checkModifyPhoneStatePermission(subId, "setImsProvisioningStatusForCapability");
joonhunshincffb7fc2021-11-28 07:32:01 +00004732
Brad Ebinger1c8542e2019-01-14 13:43:14 -08004733 final long identity = Binder.clearCallingIdentity();
4734 try {
joonhunshin91bc1952022-04-29 08:47:15 +00004735 ImsProvisioningController controller = ImsProvisioningController.getInstance();
4736 if (controller == null) {
4737 loge("setImsProvisioningStatusForCapability: Device does not support IMS");
4738 return;
4739 }
4740 controller.setImsProvisioningStatusForCapability(
4741 subId, capability, tech, isProvisioned);
Brad Ebinger1c8542e2019-01-14 13:43:14 -08004742 } finally {
4743 Binder.restoreCallingIdentity(identity);
4744 }
4745 }
4746
4747 @Override
4748 public boolean getImsProvisioningStatusForCapability(int subId, int capability, int tech) {
joonhunshincffb7fc2021-11-28 07:32:01 +00004749 TelephonyPermissions.enforceCallingOrSelfReadPrecisePhoneStatePermissionOrCarrierPrivilege(
4750 mApp, subId, "getProvisioningStatusForCapability");
4751
Brad Ebinger1c8542e2019-01-14 13:43:14 -08004752 final long identity = Binder.clearCallingIdentity();
4753 try {
joonhunshin91bc1952022-04-29 08:47:15 +00004754 ImsProvisioningController controller = ImsProvisioningController.getInstance();
4755 if (controller == null) {
4756 loge("getImsProvisioningStatusForCapability: Device does not support IMS");
Brad Ebinger1c8542e2019-01-14 13:43:14 -08004757
joonhunshin91bc1952022-04-29 08:47:15 +00004758 // device does not support IMS, this method will return true always.
4759 return true;
4760 }
4761 return controller.getImsProvisioningStatusForCapability(subId, capability, tech);
Brad Ebinger1c8542e2019-01-14 13:43:14 -08004762 } finally {
4763 Binder.restoreCallingIdentity(identity);
4764 }
4765 }
4766
4767 @Override
joonhunshincffb7fc2021-11-28 07:32:01 +00004768 public boolean isProvisioningRequiredForCapability(int subId, int capability, int tech) {
4769 TelephonyPermissions.enforceCallingOrSelfReadPrecisePhoneStatePermissionOrCarrierPrivilege(
4770 mApp, subId, "isProvisioningRequiredForCapability");
4771
4772 final long identity = Binder.clearCallingIdentity();
4773 try {
joonhunshin91bc1952022-04-29 08:47:15 +00004774 ImsProvisioningController controller = ImsProvisioningController.getInstance();
4775 if (controller == null) {
4776 loge("isProvisioningRequiredForCapability: Device does not support IMS");
4777
4778 // device does not support IMS, this method will return false
4779 return false;
4780 }
4781 return controller.isImsProvisioningRequiredForCapability(subId, capability, tech);
joonhunshincffb7fc2021-11-28 07:32:01 +00004782 } finally {
4783 Binder.restoreCallingIdentity(identity);
Brad Ebinger1c8542e2019-01-14 13:43:14 -08004784 }
Brad Ebinger1c8542e2019-01-14 13:43:14 -08004785 }
4786
4787 @Override
joonhunshincffb7fc2021-11-28 07:32:01 +00004788 public boolean isRcsProvisioningRequiredForCapability(int subId, int capability, int tech) {
4789 TelephonyPermissions.enforceCallingOrSelfReadPrecisePhoneStatePermissionOrCarrierPrivilege(
4790 mApp, subId, "isProvisioningRequiredForCapability");
Brad Ebinger1c8542e2019-01-14 13:43:14 -08004791
joonhunshincffb7fc2021-11-28 07:32:01 +00004792 final long identity = Binder.clearCallingIdentity();
4793 try {
joonhunshin91bc1952022-04-29 08:47:15 +00004794 ImsProvisioningController controller = ImsProvisioningController.getInstance();
4795 if (controller == null) {
4796 loge("isRcsProvisioningRequiredForCapability: Device does not support IMS");
4797
4798 // device does not support IMS, this method will return false
4799 return false;
4800 }
4801 return controller.isRcsProvisioningRequiredForCapability(subId, capability, tech);
joonhunshincffb7fc2021-11-28 07:32:01 +00004802 } finally {
4803 Binder.restoreCallingIdentity(identity);
4804 }
Brad Ebinger1c8542e2019-01-14 13:43:14 -08004805 }
4806
Brad Ebinger1c8542e2019-01-14 13:43:14 -08004807 @Override
Brad Ebingerdf5b4f02018-10-31 11:24:17 -07004808 public int getImsProvisioningInt(int subId, int key) {
Brad Ebinger1c8542e2019-01-14 13:43:14 -08004809 if (!SubscriptionManager.isValidSubscriptionId(subId)) {
4810 throw new IllegalArgumentException("Invalid Subscription id '" + subId + "'");
4811 }
joonhunshincffb7fc2021-11-28 07:32:01 +00004812 TelephonyPermissions.enforceCallingOrSelfReadPrecisePhoneStatePermissionOrCarrierPrivilege(
4813 mApp, subId, "getImsProvisioningInt");
4814
Brad Ebingerdf5b4f02018-10-31 11:24:17 -07004815 final long identity = Binder.clearCallingIdentity();
4816 try {
4817 // TODO: Refactor to remove ImsManager dependence and query through ImsPhone directly.
Brad Ebinger1c8542e2019-01-14 13:43:14 -08004818 int slotId = getSlotIndex(subId);
4819 if (slotId <= SubscriptionManager.INVALID_SIM_SLOT_INDEX) {
4820 Log.w(LOG_TAG, "getImsProvisioningInt: called with an inactive subscription '"
4821 + subId + "' for key:" + key);
4822 return ImsConfigImplBase.CONFIG_RESULT_UNKNOWN;
4823 }
joonhunshincffb7fc2021-11-28 07:32:01 +00004824
joonhunshin91bc1952022-04-29 08:47:15 +00004825 ImsProvisioningController controller = ImsProvisioningController.getInstance();
4826 if (controller == null) {
4827 loge("getImsProvisioningInt: Device does not support IMS");
4828
4829 // device does not support IMS, this method will return CONFIG_RESULT_UNKNOWN.
4830 return ImsConfigImplBase.CONFIG_RESULT_UNKNOWN;
4831 }
4832 int retVal = controller.getProvisioningValue(subId, key);
joonhunshincffb7fc2021-11-28 07:32:01 +00004833 if (retVal != ImsConfigImplBase.CONFIG_RESULT_UNKNOWN) {
4834 return retVal;
4835 }
4836
calvinpanb5a34062021-02-08 19:59:36 +08004837 return ImsManager.getInstance(mApp, slotId).getConfigInt(key);
Brad Ebinger1ce9c432019-07-16 13:19:44 -07004838 } catch (com.android.ims.ImsException e) {
Brad Ebinger1c8542e2019-01-14 13:43:14 -08004839 Log.w(LOG_TAG, "getImsProvisioningInt: ImsService is not available for subscription '"
4840 + subId + "' for key:" + key);
4841 return ImsConfigImplBase.CONFIG_RESULT_UNKNOWN;
Brad Ebingerdf5b4f02018-10-31 11:24:17 -07004842 } finally {
4843 Binder.restoreCallingIdentity(identity);
4844 }
4845 }
4846
4847 @Override
4848 public String getImsProvisioningString(int subId, int key) {
Brad Ebinger1c8542e2019-01-14 13:43:14 -08004849 if (!SubscriptionManager.isValidSubscriptionId(subId)) {
4850 throw new IllegalArgumentException("Invalid Subscription id '" + subId + "'");
4851 }
joonhunshincffb7fc2021-11-28 07:32:01 +00004852 TelephonyPermissions.enforceCallingOrSelfReadPrecisePhoneStatePermissionOrCarrierPrivilege(
4853 mApp, subId, "getImsProvisioningString");
4854
Brad Ebingerdf5b4f02018-10-31 11:24:17 -07004855 final long identity = Binder.clearCallingIdentity();
4856 try {
4857 // TODO: Refactor to remove ImsManager dependence and query through ImsPhone directly.
Brad Ebinger1c8542e2019-01-14 13:43:14 -08004858 int slotId = getSlotIndex(subId);
4859 if (slotId <= SubscriptionManager.INVALID_SIM_SLOT_INDEX) {
4860 Log.w(LOG_TAG, "getImsProvisioningString: called for an inactive subscription id '"
4861 + subId + "' for key:" + key);
4862 return ProvisioningManager.STRING_QUERY_RESULT_ERROR_GENERIC;
4863 }
calvinpanb5a34062021-02-08 19:59:36 +08004864 return ImsManager.getInstance(mApp, slotId).getConfigString(key);
Brad Ebinger1ce9c432019-07-16 13:19:44 -07004865 } catch (com.android.ims.ImsException e) {
Brad Ebinger1c8542e2019-01-14 13:43:14 -08004866 Log.w(LOG_TAG, "getImsProvisioningString: ImsService is not available for sub '"
4867 + subId + "' for key:" + key);
4868 return ProvisioningManager.STRING_QUERY_RESULT_ERROR_NOT_READY;
Brad Ebingerdf5b4f02018-10-31 11:24:17 -07004869 } finally {
4870 Binder.restoreCallingIdentity(identity);
4871 }
4872 }
4873
4874 @Override
4875 public int setImsProvisioningInt(int subId, int key, int value) {
Brad Ebinger1c8542e2019-01-14 13:43:14 -08004876 if (!SubscriptionManager.isValidSubscriptionId(subId)) {
4877 throw new IllegalArgumentException("Invalid Subscription id '" + subId + "'");
4878 }
Brad Ebinger3d0b34e2018-11-15 14:13:12 -08004879 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(mApp, subId,
4880 "setImsProvisioningInt");
joonhunshincffb7fc2021-11-28 07:32:01 +00004881
Brad Ebingerdf5b4f02018-10-31 11:24:17 -07004882 final long identity = Binder.clearCallingIdentity();
4883 try {
4884 // TODO: Refactor to remove ImsManager dependence and query through ImsPhone directly.
Brad Ebinger1c8542e2019-01-14 13:43:14 -08004885 int slotId = getSlotIndex(subId);
4886 if (slotId <= SubscriptionManager.INVALID_SIM_SLOT_INDEX) {
4887 Log.w(LOG_TAG, "setImsProvisioningInt: called with an inactive subscription id '"
4888 + subId + "' for key:" + key);
4889 return ImsConfigImplBase.CONFIG_RESULT_FAILED;
4890 }
joonhunshincffb7fc2021-11-28 07:32:01 +00004891
joonhunshin91bc1952022-04-29 08:47:15 +00004892 ImsProvisioningController controller = ImsProvisioningController.getInstance();
4893 if (controller == null) {
4894 loge("setImsProvisioningInt: Device does not support IMS");
4895
4896 // device does not support IMS, this method will return CONFIG_RESULT_FAILED.
4897 return ImsConfigImplBase.CONFIG_RESULT_FAILED;
4898 }
4899 int retVal = controller.setProvisioningValue(subId, key, value);
joonhunshincffb7fc2021-11-28 07:32:01 +00004900 if (retVal != ImsConfigImplBase.CONFIG_RESULT_UNKNOWN) {
4901 return retVal;
4902 }
4903
calvinpanb5a34062021-02-08 19:59:36 +08004904 return ImsManager.getInstance(mApp, slotId).setConfig(key, value);
4905 } catch (com.android.ims.ImsException | RemoteException e) {
Brad Ebinger1c8542e2019-01-14 13:43:14 -08004906 Log.w(LOG_TAG, "setImsProvisioningInt: ImsService unavailable for sub '" + subId
calvinpanb5a34062021-02-08 19:59:36 +08004907 + "' for key:" + key, e);
Brad Ebinger1c8542e2019-01-14 13:43:14 -08004908 return ImsConfigImplBase.CONFIG_RESULT_FAILED;
Brad Ebingerdf5b4f02018-10-31 11:24:17 -07004909 } finally {
4910 Binder.restoreCallingIdentity(identity);
4911 }
4912 }
4913
4914 @Override
4915 public int setImsProvisioningString(int subId, int key, String value) {
Brad Ebinger1c8542e2019-01-14 13:43:14 -08004916 if (!SubscriptionManager.isValidSubscriptionId(subId)) {
4917 throw new IllegalArgumentException("Invalid Subscription id '" + subId + "'");
4918 }
Brad Ebinger3d0b34e2018-11-15 14:13:12 -08004919 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(mApp, subId,
4920 "setImsProvisioningString");
Brad Ebingerdf5b4f02018-10-31 11:24:17 -07004921 final long identity = Binder.clearCallingIdentity();
4922 try {
4923 // TODO: Refactor to remove ImsManager dependence and query through ImsPhone directly.
Brad Ebinger1c8542e2019-01-14 13:43:14 -08004924 int slotId = getSlotIndex(subId);
4925 if (slotId <= SubscriptionManager.INVALID_SIM_SLOT_INDEX) {
4926 Log.w(LOG_TAG, "setImsProvisioningString: called with an inactive subscription id '"
4927 + subId + "' for key:" + key);
4928 return ImsConfigImplBase.CONFIG_RESULT_FAILED;
4929 }
calvinpanb5a34062021-02-08 19:59:36 +08004930 return ImsManager.getInstance(mApp, slotId).setConfig(key, value);
4931 } catch (com.android.ims.ImsException | RemoteException e) {
Brad Ebinger1c8542e2019-01-14 13:43:14 -08004932 Log.w(LOG_TAG, "setImsProvisioningString: ImsService unavailable for sub '" + subId
calvinpanb5a34062021-02-08 19:59:36 +08004933 + "' for key:" + key, e);
Brad Ebinger1c8542e2019-01-14 13:43:14 -08004934 return ImsConfigImplBase.CONFIG_RESULT_FAILED;
Brad Ebingerdf5b4f02018-10-31 11:24:17 -07004935 } finally {
4936 Binder.restoreCallingIdentity(identity);
4937 }
4938 }
4939
Brad Ebinger919631e2021-06-02 17:46:35 -07004940 /**
4941 * Throw an ImsException if the IMS resolver does not have an ImsService configured for MMTEL
4942 * for the given slot ID or no ImsResolver instance has been created.
4943 * @param slotId The slot ID that the IMS service is created for.
4944 * @throws ImsException If there is no ImsService configured for this slot.
4945 */
4946 private void verifyImsMmTelConfiguredOrThrow(int slotId) throws ImsException {
4947 if (mImsResolver == null || !mImsResolver.isImsServiceConfiguredForFeature(slotId,
4948 ImsFeature.FEATURE_MMTEL)) {
4949 throw new ImsException("This subscription does not support MMTEL over IMS",
4950 ImsException.CODE_ERROR_UNSUPPORTED_OPERATION);
4951 }
4952 }
4953
Brad Ebinger1ce9c432019-07-16 13:19:44 -07004954 private int getSlotIndexOrException(int subId) throws ImsException {
Brad Ebinger35c841c2018-10-01 10:40:55 -07004955 int slotId = SubscriptionManager.getSlotIndex(subId);
4956 if (!SubscriptionManager.isValidSlotIndex(slotId)) {
Brad Ebinger1ce9c432019-07-16 13:19:44 -07004957 throw new ImsException("Invalid Subscription Id, subId=" + subId,
4958 ImsException.CODE_ERROR_INVALID_SUBSCRIPTION);
Brad Ebinger35c841c2018-10-01 10:40:55 -07004959 }
4960 return slotId;
4961 }
4962
Brad Ebinger1c8542e2019-01-14 13:43:14 -08004963 private int getSlotIndex(int subId) {
4964 int slotId = SubscriptionManager.getSlotIndex(subId);
4965 if (!SubscriptionManager.isValidSlotIndex(slotId)) {
4966 return SubscriptionManager.INVALID_SIM_SLOT_INDEX;
4967 }
4968 return slotId;
4969 }
4970
Wink Saville36469e72014-06-11 15:17:00 -07004971 /**
Nathan Harold9042f0b2019-05-21 15:51:27 -07004972 * Returns the data network type for a subId; does not throw SecurityException.
Wink Saville36469e72014-06-11 15:17:00 -07004973 */
4974 @Override
Philip P. Moltmann700a9592019-10-03 11:53:50 -07004975 public int getNetworkTypeForSubscriber(int subId, String callingPackage,
4976 String callingFeatureId) {
Shuo Qian13d89152021-05-10 23:58:11 -07004977 try {
4978 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
4979 } catch (SecurityException se) {
4980 EventLog.writeEvent(0x534e4554, "186776740", Binder.getCallingUid());
4981 throw new SecurityException("Package " + callingPackage + " does not belong to "
4982 + Binder.getCallingUid());
4983 }
Nathan Haroldf096d982020-11-18 17:18:06 -08004984 final int targetSdk = TelephonyPermissions.getTargetSdk(mApp, callingPackage);
Nathan Haroldef60dba2019-05-22 13:55:14 -07004985 if (targetSdk > android.os.Build.VERSION_CODES.Q) {
Philip P. Moltmann700a9592019-10-03 11:53:50 -07004986 return getDataNetworkTypeForSubscriber(subId, callingPackage, callingFeatureId);
Nathan Haroldef60dba2019-05-22 13:55:14 -07004987 } else if (targetSdk == android.os.Build.VERSION_CODES.Q
Nathan Harold9042f0b2019-05-21 15:51:27 -07004988 && !TelephonyPermissions.checkCallingOrSelfReadPhoneStateNoThrow(
Philip P. Moltmann700a9592019-10-03 11:53:50 -07004989 mApp, subId, callingPackage, callingFeatureId,
4990 "getNetworkTypeForSubscriber")) {
Robert Greenwalt36b23af2015-07-06 17:59:14 -07004991 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
4992 }
Robert Greenwalta5dcfcb2015-07-10 09:06:29 -07004993
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004994 final long identity = Binder.clearCallingIdentity();
4995 try {
4996 final Phone phone = getPhone(subId);
4997 if (phone != null) {
4998 return phone.getServiceState().getDataNetworkType();
4999 } else {
5000 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
5001 }
5002 } finally {
5003 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07005004 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07005005 }
5006
5007 /**
5008 * Returns the data network type
5009 */
5010 @Override
Philip P. Moltmann700a9592019-10-03 11:53:50 -07005011 public int getDataNetworkType(String callingPackage, String callingFeatureId) {
Jack Yudf049d72022-12-02 22:48:35 -08005012 return getDataNetworkTypeForSubscriber(SubscriptionManager.getDefaultDataSubscriptionId(),
Zoey Chenfd61f7f2021-04-21 13:42:10 +08005013 callingPackage, callingFeatureId);
Wink Saville36469e72014-06-11 15:17:00 -07005014 }
5015
5016 /**
5017 * Returns the data network type for a subId
5018 */
5019 @Override
Philip P. Moltmann700a9592019-10-03 11:53:50 -07005020 public int getDataNetworkTypeForSubscriber(int subId, String callingPackage,
5021 String callingFeatureId) {
Sooraj Sasindran5d051bf2021-11-05 13:14:15 -07005022 String functionName = "getDataNetworkTypeForSubscriber";
5023 if (!TelephonyPermissions.checkCallingOrSelfReadNonDangerousPhoneStateNoThrow(
5024 mApp, functionName)) {
5025 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
5026 mApp, subId, callingPackage, callingFeatureId, functionName)) {
5027 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
5028 }
Robert Greenwalt36b23af2015-07-06 17:59:14 -07005029 }
5030
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005031 final long identity = Binder.clearCallingIdentity();
5032 try {
5033 final Phone phone = getPhone(subId);
5034 if (phone != null) {
5035 return phone.getServiceState().getDataNetworkType();
5036 } else {
5037 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
5038 }
5039 } finally {
5040 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07005041 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07005042 }
5043
5044 /**
Wink Saville36469e72014-06-11 15:17:00 -07005045 * Returns the Voice network type for a subId
5046 */
5047 @Override
Philip P. Moltmann700a9592019-10-03 11:53:50 -07005048 public int getVoiceNetworkTypeForSubscriber(int subId, String callingPackage,
5049 String callingFeatureId) {
Sooraj Sasindran5d051bf2021-11-05 13:14:15 -07005050 String functionName = "getVoiceNetworkTypeForSubscriber";
5051 if (!TelephonyPermissions.checkCallingOrSelfReadNonDangerousPhoneStateNoThrow(
5052 mApp, functionName)) {
5053 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
5054 mApp, subId, callingPackage, callingFeatureId, functionName)) {
5055 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
5056 }
Robert Greenwalta5dcfcb2015-07-10 09:06:29 -07005057 }
5058
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005059 final long identity = Binder.clearCallingIdentity();
5060 try {
5061 final Phone phone = getPhone(subId);
5062 if (phone != null) {
5063 return phone.getServiceState().getVoiceNetworkType();
5064 } else {
5065 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
5066 }
5067 } finally {
5068 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07005069 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07005070 }
5071
5072 /**
5073 * @return true if a ICC card is present
5074 */
5075 public boolean hasIccCard() {
Wink Saville36469e72014-06-11 15:17:00 -07005076 // FIXME Make changes to pass defaultSimId of type int
Jack Yudf049d72022-12-02 22:48:35 -08005077 return hasIccCardUsingSlotIndex(SubscriptionManager.getSlotIndex(
Sanket Padawe13bac7b2017-03-20 15:04:47 -07005078 getDefaultSubscription()));
Wink Saville36469e72014-06-11 15:17:00 -07005079 }
5080
5081 /**
Sanket Padawe13bac7b2017-03-20 15:04:47 -07005082 * @return true if a ICC card is present for a slotIndex
Wink Saville36469e72014-06-11 15:17:00 -07005083 */
Sanket Padawe356d7632015-06-22 14:03:32 -07005084 @Override
Sanket Padawe13bac7b2017-03-20 15:04:47 -07005085 public boolean hasIccCardUsingSlotIndex(int slotIndex) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005086 final long identity = Binder.clearCallingIdentity();
5087 try {
5088 final Phone phone = PhoneFactory.getPhone(slotIndex);
5089 if (phone != null) {
5090 return phone.getIccCard().hasIccCard();
5091 } else {
5092 return false;
5093 }
5094 } finally {
5095 Binder.restoreCallingIdentity(identity);
Amit Mahajana6fc2a82015-01-06 11:53:51 -08005096 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07005097 }
5098
5099 /**
5100 * Return if the current radio is LTE on CDMA. This
5101 * is a tri-state return value as for a period of time
5102 * the mode may be unknown.
5103 *
Robert Greenwalt36b23af2015-07-06 17:59:14 -07005104 * @param callingPackage the name of the package making the call.
Santos Cordon7d4ddf62013-07-10 11:58:08 -07005105 * @return {@link Phone#LTE_ON_CDMA_UNKNOWN}, {@link Phone#LTE_ON_CDMA_FALSE}
Jake Hambye994d462014-02-03 13:10:13 -08005106 * or {@link Phone#LTE_ON_CDMA_TRUE}
Santos Cordon7d4ddf62013-07-10 11:58:08 -07005107 */
Robert Greenwalt36b23af2015-07-06 17:59:14 -07005108 @Override
Philip P. Moltmann700a9592019-10-03 11:53:50 -07005109 public int getLteOnCdmaMode(String callingPackage, String callingFeatureId) {
5110 return getLteOnCdmaModeForSubscriber(getDefaultSubscription(), callingPackage,
5111 callingFeatureId);
Wink Saville36469e72014-06-11 15:17:00 -07005112 }
5113
Sanket Padawe356d7632015-06-22 14:03:32 -07005114 @Override
Philip P. Moltmann700a9592019-10-03 11:53:50 -07005115 public int getLteOnCdmaModeForSubscriber(int subId, String callingPackage,
5116 String callingFeatureId) {
Sarah Chin790d2922020-01-16 12:17:23 -08005117 try {
5118 enforceReadPrivilegedPermission("getLteOnCdmaModeForSubscriber");
5119 } catch (SecurityException e) {
Robert Greenwalt36b23af2015-07-06 17:59:14 -07005120 return PhoneConstants.LTE_ON_CDMA_UNKNOWN;
5121 }
5122
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005123 final long identity = Binder.clearCallingIdentity();
5124 try {
5125 final Phone phone = getPhone(subId);
5126 if (phone == null) {
5127 return PhoneConstants.LTE_ON_CDMA_UNKNOWN;
5128 } else {
Nathan Harold05ad6332020-07-10 11:54:36 -07005129 return TelephonyProperties.lte_on_cdma_device()
5130 .orElse(PhoneConstants.LTE_ON_CDMA_FALSE);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005131 }
5132 } finally {
5133 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07005134 }
Wink Saville36469e72014-06-11 15:17:00 -07005135 }
5136
Wink Saville36469e72014-06-11 15:17:00 -07005137 /**
5138 * {@hide}
5139 * Returns Default subId, 0 in the case of single standby.
5140 */
Wink Savilleb564aae2014-10-23 10:18:09 -07005141 private int getDefaultSubscription() {
Jack Yudf049d72022-12-02 22:48:35 -08005142 return SubscriptionManager.getDefaultSubscriptionId();
Wink Saville36469e72014-06-11 15:17:00 -07005143 }
5144
Shishir Agrawala9f32182016-04-12 12:00:16 -07005145 private int getSlotForDefaultSubscription() {
Jack Yudf049d72022-12-02 22:48:35 -08005146 return SubscriptionManager.getPhoneId(getDefaultSubscription());
Shishir Agrawala9f32182016-04-12 12:00:16 -07005147 }
5148
Wink Savilleb564aae2014-10-23 10:18:09 -07005149 private int getPreferredVoiceSubscription() {
Jack Yudf049d72022-12-02 22:48:35 -08005150 return SubscriptionManager.getDefaultVoiceSubscriptionId();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07005151 }
Ihab Awadf2177b72013-11-25 13:33:23 -08005152
Pengquan Menge92a50d2018-09-21 15:54:48 -07005153 private boolean isActiveSubscription(int subId) {
Jack Yudf049d72022-12-02 22:48:35 -08005154 if (PhoneFactory.isSubscriptionManagerServiceEnabled()) {
5155 return SubscriptionManagerService.getInstance().isActiveSubId(subId,
5156 mApp.getOpPackageName(), mApp.getFeatureId());
5157 }
Pengquan Menge92a50d2018-09-21 15:54:48 -07005158 return mSubscriptionController.isActiveSubId(subId);
5159 }
5160
Ihab Awadf2177b72013-11-25 13:33:23 -08005161 /**
5162 * @see android.telephony.TelephonyManager.WifiCallingChoices
5163 */
5164 public int getWhenToMakeWifiCalls() {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005165 final long identity = Binder.clearCallingIdentity();
5166 try {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08005167 return Settings.System.getInt(mApp.getContentResolver(),
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005168 Settings.System.WHEN_TO_MAKE_WIFI_CALLS,
5169 getWhenToMakeWifiCallsDefaultPreference());
5170 } finally {
5171 Binder.restoreCallingIdentity(identity);
5172 }
Ihab Awadf2177b72013-11-25 13:33:23 -08005173 }
5174
5175 /**
5176 * @see android.telephony.TelephonyManager.WifiCallingChoices
5177 */
5178 public void setWhenToMakeWifiCalls(int preference) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005179 final long identity = Binder.clearCallingIdentity();
5180 try {
5181 if (DBG) log("setWhenToMakeWifiCallsStr, storing setting = " + preference);
Nazanin Bakhshif782e562018-12-11 15:15:39 -08005182 Settings.System.putInt(mApp.getContentResolver(),
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005183 Settings.System.WHEN_TO_MAKE_WIFI_CALLS, preference);
5184 } finally {
5185 Binder.restoreCallingIdentity(identity);
5186 }
Ihab Awadf9e92732013-12-05 18:02:52 -08005187 }
5188
Sailesh Nepald1e68152013-12-12 19:08:02 -08005189 private static int getWhenToMakeWifiCallsDefaultPreference() {
Santos Cordonda120f42014-08-06 04:44:34 -07005190 // TODO: Use a build property to choose this value.
Evan Charlton9829e882013-12-19 15:30:38 -08005191 return TelephonyManager.WifiCallingChoices.ALWAYS_USE;
Ihab Awadf2177b72013-11-25 13:33:23 -08005192 }
Shishir Agrawal69f68122013-12-16 17:25:49 -08005193
Muralidhar Reddybd38d952021-12-02 21:04:16 +00005194 private Phone getPhoneFromSlotPortIndexOrThrowException(int slotIndex, int portIndex) {
5195 int phoneId = UiccController.getInstance().getPhoneIdFromSlotPortIndex(slotIndex,
5196 portIndex);
Jordan Liu4c733742019-02-28 12:03:40 -08005197 if (phoneId == -1) {
Muralidhar Reddybd38d952021-12-02 21:04:16 +00005198 throw new IllegalArgumentException("Given slot index: " + slotIndex + " port index: "
5199 + portIndex + " does not correspond to an active phone");
Jordan Liu4c733742019-02-28 12:03:40 -08005200 }
5201 return PhoneFactory.getPhone(phoneId);
5202 }
5203
Shishir Agrawal566b7612013-10-28 14:41:00 -07005204 @Override
Derek Tan740e1672017-06-27 14:56:27 -07005205 public IccOpenLogicalChannelResponse iccOpenLogicalChannel(
Rambo Wanga1782702021-11-10 20:15:19 -08005206 @NonNull IccLogicalChannelRequest request) {
5207 Phone phone = getPhoneFromValidIccLogicalChannelRequest(request,
5208 /*message=*/ "iccOpenLogicalChannel");
5209
5210 if (DBG) log("iccOpenLogicalChannel: request=" + request);
5211 // Verify that the callingPackage in the request belongs to the calling UID
5212 mAppOps.checkPackage(Binder.getCallingUid(), request.callingPackage);
5213
5214 return iccOpenLogicalChannelWithPermission(phone, request);
Jordan Liu4c733742019-02-28 12:03:40 -08005215 }
Shishir Agrawal566b7612013-10-28 14:41:00 -07005216
Rambo Wanga1782702021-11-10 20:15:19 -08005217 private Phone getPhoneFromValidIccLogicalChannelRequest(
5218 @NonNull IccLogicalChannelRequest request, String message) {
5219 Phone phone;
5220 if (request.subId != SubscriptionManager.INVALID_SUBSCRIPTION_ID) {
5221 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
5222 mApp, request.subId, message);
5223 phone = getPhoneFromSubId(request.subId);
5224 } else if (request.slotIndex != SubscriptionManager.INVALID_SIM_SLOT_INDEX) {
5225 enforceModifyPermission();
5226 phone = getPhoneFromSlotPortIndexOrThrowException(request.slotIndex, request.portIndex);
5227 } else {
5228 throw new IllegalArgumentException("Both subId and slotIndex in request are invalid.");
Jordan Liu4c733742019-02-28 12:03:40 -08005229 }
Rambo Wanga1782702021-11-10 20:15:19 -08005230 return phone;
Jordan Liu4c733742019-02-28 12:03:40 -08005231 }
5232
5233 private IccOpenLogicalChannelResponse iccOpenLogicalChannelWithPermission(Phone phone,
Rambo Wanga1782702021-11-10 20:15:19 -08005234 IccLogicalChannelRequest channelRequest) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005235 final long identity = Binder.clearCallingIdentity();
5236 try {
Rambo Wanga1782702021-11-10 20:15:19 -08005237 if (TextUtils.equals(ISDR_AID, channelRequest.aid)) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005238 // Only allows LPA to open logical channel to ISD-R.
Nazanin Bakhshif782e562018-12-11 15:15:39 -08005239 ComponentInfo bestComponent = EuiccConnector.findBestComponent(getDefaultPhone()
5240 .getContext().getPackageManager());
Rambo Wanga1782702021-11-10 20:15:19 -08005241 if (bestComponent == null || !TextUtils.equals(channelRequest.callingPackage,
5242 bestComponent.packageName)) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005243 loge("The calling package is not allowed to access ISD-R.");
5244 throw new SecurityException(
5245 "The calling package is not allowed to access ISD-R.");
5246 }
Derek Tan740e1672017-06-27 14:56:27 -07005247 }
Derek Tan740e1672017-06-27 14:56:27 -07005248
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005249 IccOpenLogicalChannelResponse response = (IccOpenLogicalChannelResponse) sendRequest(
Rambo Wanga1782702021-11-10 20:15:19 -08005250 CMD_OPEN_CHANNEL, channelRequest, phone, null /* workSource */);
5251 if (DBG) log("iccOpenLogicalChannelWithPermission: response=" + response);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005252 return response;
5253 } finally {
5254 Binder.restoreCallingIdentity(identity);
5255 }
Shishir Agrawal566b7612013-10-28 14:41:00 -07005256 }
5257
5258 @Override
Rambo Wanga1782702021-11-10 20:15:19 -08005259 public boolean iccCloseLogicalChannel(@NonNull IccLogicalChannelRequest request) {
5260 Phone phone = getPhoneFromValidIccLogicalChannelRequest(request,
5261 /*message=*/"iccCloseLogicalChannel");
5262
5263 if (DBG) log("iccCloseLogicalChannel: request=" + request);
5264
5265 return iccCloseLogicalChannelWithPermission(phone, request);
Jordan Liu4c733742019-02-28 12:03:40 -08005266 }
Shishir Agrawal566b7612013-10-28 14:41:00 -07005267
Rambo Wanga1782702021-11-10 20:15:19 -08005268 private boolean iccCloseLogicalChannelWithPermission(Phone phone,
5269 IccLogicalChannelRequest request) {
Chen Xua8f0dff2022-02-12 00:34:15 -08005270 // before this feature is enabled, this API should only return false if
5271 // the operation fails instead of throwing runtime exception for
5272 // backward-compatibility.
5273 final boolean shouldThrowExceptionOnFailure = CompatChanges.isChangeEnabled(
5274 ICC_CLOSE_CHANNEL_EXCEPTION_ON_FAILURE, Binder.getCallingUid());
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005275 final long identity = Binder.clearCallingIdentity();
5276 try {
Rambo Wanga1782702021-11-10 20:15:19 -08005277 if (request.channel < 0) {
Chen Xu540470b2021-12-14 17:15:47 -08005278 throw new IllegalArgumentException("request.channel is less than 0");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005279 }
Chen Xue9d737e2022-01-01 23:41:31 -08005280 Object result = sendRequest(CMD_CLOSE_CHANNEL, request.channel, phone,
Jordan Liu4c733742019-02-28 12:03:40 -08005281 null /* workSource */);
Chen Xue9d737e2022-01-01 23:41:31 -08005282 Boolean success = false;
5283 if (result instanceof RuntimeException) {
5284 // if there is an exception returned, throw from the binder thread here.
Chen Xua8f0dff2022-02-12 00:34:15 -08005285 if (shouldThrowExceptionOnFailure) {
5286 throw (RuntimeException) result;
5287 } else {
5288 return false;
5289 }
Chen Xue9d737e2022-01-01 23:41:31 -08005290 } else if (result instanceof Boolean) {
5291 success = (Boolean) result;
5292 } else {
5293 loge("iccCloseLogicalChannelWithPermission: supported return type " + result);
5294 }
Rambo Wanga1782702021-11-10 20:15:19 -08005295 if (DBG) log("iccCloseLogicalChannelWithPermission: success=" + success);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005296 return success;
5297 } finally {
5298 Binder.restoreCallingIdentity(identity);
Shishir Agrawal566b7612013-10-28 14:41:00 -07005299 }
Shishir Agrawal566b7612013-10-28 14:41:00 -07005300 }
5301
5302 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08005303 public String iccTransmitApduLogicalChannel(int subId, int channel, int cla,
Shishir Agrawal566b7612013-10-28 14:41:00 -07005304 int command, int p1, int p2, int p3, String data) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08005305 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
5306 mApp, subId, "iccTransmitApduLogicalChannel");
Jordan Liu4c733742019-02-28 12:03:40 -08005307 if (DBG) {
5308 log("iccTransmitApduLogicalChannel: subId=" + subId + " chnl=" + channel
5309 + " cla=" + cla + " cmd=" + command + " p1=" + p1 + " p2=" + p2 + " p3="
5310 + p3 + " data=" + data);
5311 }
5312 return iccTransmitApduLogicalChannelWithPermission(getPhoneFromSubId(subId), channel, cla,
5313 command, p1, p2, p3, data);
5314 }
Shishir Agrawal566b7612013-10-28 14:41:00 -07005315
Jordan Liu4c733742019-02-28 12:03:40 -08005316 @Override
Muralidhar Reddybd38d952021-12-02 21:04:16 +00005317 public String iccTransmitApduLogicalChannelByPort(int slotIndex, int portIndex, int channel,
5318 int cla, int command, int p1, int p2, int p3, String data) {
Jordan Liu4c733742019-02-28 12:03:40 -08005319 enforceModifyPermission();
5320 if (DBG) {
Muralidhar Reddybd38d952021-12-02 21:04:16 +00005321 log("iccTransmitApduLogicalChannelByPort: slotIndex=" + slotIndex + " portIndex="
5322 + portIndex + " chnl=" + channel + " cla=" + cla + " cmd=" + command + " p1="
5323 + p1 + " p2=" + p2 + " p3=" + p3 + " data=" + data);
Jordan Liu4c733742019-02-28 12:03:40 -08005324 }
5325 return iccTransmitApduLogicalChannelWithPermission(
Muralidhar Reddybd38d952021-12-02 21:04:16 +00005326 getPhoneFromSlotPortIndexOrThrowException(slotIndex, portIndex), channel, cla,
5327 command, p1, p2, p3, data);
Jordan Liu4c733742019-02-28 12:03:40 -08005328 }
5329
5330 private String iccTransmitApduLogicalChannelWithPermission(Phone phone, int channel, int cla,
5331 int command, int p1, int p2, int p3, String data) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005332 final long identity = Binder.clearCallingIdentity();
5333 try {
Hall Liu4fd771b2019-05-02 09:16:29 -07005334 if (channel <= 0) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005335 return "";
5336 }
Shishir Agrawal566b7612013-10-28 14:41:00 -07005337
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005338 IccIoResult response = (IccIoResult) sendRequest(CMD_TRANSMIT_APDU_LOGICAL_CHANNEL,
Jordan Liu4c733742019-02-28 12:03:40 -08005339 new IccAPDUArgument(channel, cla, command, p1, p2, p3, data), phone,
5340 null /* workSource */);
5341 if (DBG) log("iccTransmitApduLogicalChannelWithPermission: " + response);
Shishir Agrawal566b7612013-10-28 14:41:00 -07005342
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005343 // Append the returned status code to the end of the response payload.
5344 String s = Integer.toHexString(
5345 (response.sw1 << 8) + response.sw2 + 0x10000).substring(1);
5346 if (response.payload != null) {
5347 s = IccUtils.bytesToHexString(response.payload) + s;
5348 }
5349 return s;
5350 } finally {
5351 Binder.restoreCallingIdentity(identity);
Shishir Agrawal5ec14172014-08-05 17:05:45 -07005352 }
Shishir Agrawal566b7612013-10-28 14:41:00 -07005353 }
Jake Hambye994d462014-02-03 13:10:13 -08005354
Evan Charltonc66da362014-05-16 14:06:40 -07005355 @Override
Holly Jiuyu Sun1cc2d552018-01-26 15:51:16 -08005356 public String iccTransmitApduBasicChannel(int subId, String callingPackage, int cla,
5357 int command, int p1, int p2, int p3, String data) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08005358 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
5359 mApp, subId, "iccTransmitApduBasicChannel");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005360 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
Jordan Liu4c733742019-02-28 12:03:40 -08005361 if (DBG) {
5362 log("iccTransmitApduBasicChannel: subId=" + subId + " cla=" + cla + " cmd="
5363 + command + " p1=" + p1 + " p2=" + p2 + " p3=" + p3 + " data=" + data);
5364 }
5365 return iccTransmitApduBasicChannelWithPermission(getPhoneFromSubId(subId), callingPackage,
5366 cla, command, p1, p2, p3, data);
5367 }
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07005368
Jordan Liu4c733742019-02-28 12:03:40 -08005369 @Override
Muralidhar Reddybd38d952021-12-02 21:04:16 +00005370 public String iccTransmitApduBasicChannelByPort(int slotIndex, int portIndex,
5371 String callingPackage, int cla, int command, int p1, int p2, int p3, String data) {
Jordan Liu4c733742019-02-28 12:03:40 -08005372 enforceModifyPermission();
5373 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
5374 if (DBG) {
Muralidhar Reddybd38d952021-12-02 21:04:16 +00005375 log("iccTransmitApduBasicChannelByPort: slotIndex=" + slotIndex + " portIndex="
5376 + portIndex + " cla=" + cla + " cmd=" + command + " p1=" + p1 + " p2="
5377 + p2 + " p3=" + p3 + " data=" + data);
Jordan Liu4c733742019-02-28 12:03:40 -08005378 }
5379
5380 return iccTransmitApduBasicChannelWithPermission(
Muralidhar Reddybd38d952021-12-02 21:04:16 +00005381 getPhoneFromSlotPortIndexOrThrowException(slotIndex, portIndex), callingPackage,
5382 cla, command, p1, p2, p3, data);
Jordan Liu4c733742019-02-28 12:03:40 -08005383 }
5384
5385 // open APDU basic channel assuming the caller has sufficient permissions
5386 private String iccTransmitApduBasicChannelWithPermission(Phone phone, String callingPackage,
5387 int cla, int command, int p1, int p2, int p3, String data) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005388 final long identity = Binder.clearCallingIdentity();
5389 try {
5390 if (command == SELECT_COMMAND && p1 == SELECT_P1 && p2 == SELECT_P2 && p3 == SELECT_P3
5391 && TextUtils.equals(ISDR_AID, data)) {
5392 // Only allows LPA to select ISD-R.
Nazanin Bakhshif782e562018-12-11 15:15:39 -08005393 ComponentInfo bestComponent = EuiccConnector.findBestComponent(getDefaultPhone()
5394 .getContext().getPackageManager());
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005395 if (bestComponent == null
5396 || !TextUtils.equals(callingPackage, bestComponent.packageName)) {
5397 loge("The calling package is not allowed to select ISD-R.");
5398 throw new SecurityException(
5399 "The calling package is not allowed to select ISD-R.");
5400 }
Holly Jiuyu Sun1cc2d552018-01-26 15:51:16 -08005401 }
Holly Jiuyu Sun1cc2d552018-01-26 15:51:16 -08005402
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005403 IccIoResult response = (IccIoResult) sendRequest(CMD_TRANSMIT_APDU_BASIC_CHANNEL,
Jordan Liu4c733742019-02-28 12:03:40 -08005404 new IccAPDUArgument(0, cla, command, p1, p2, p3, data), phone,
5405 null /* workSource */);
5406 if (DBG) log("iccTransmitApduBasicChannelWithPermission: " + response);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07005407
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005408 // Append the returned status code to the end of the response payload.
5409 String s = Integer.toHexString(
5410 (response.sw1 << 8) + response.sw2 + 0x10000).substring(1);
5411 if (response.payload != null) {
5412 s = IccUtils.bytesToHexString(response.payload) + s;
5413 }
5414 return s;
5415 } finally {
5416 Binder.restoreCallingIdentity(identity);
Shishir Agrawal5ec14172014-08-05 17:05:45 -07005417 }
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07005418 }
5419
5420 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08005421 public byte[] iccExchangeSimIO(int subId, int fileID, int command, int p1, int p2, int p3,
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07005422 String filePath) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08005423 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
5424 mApp, subId, "iccExchangeSimIO");
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07005425
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005426 final long identity = Binder.clearCallingIdentity();
5427 try {
5428 if (DBG) {
5429 log("Exchange SIM_IO " + subId + ":" + fileID + ":" + command + " "
5430 + p1 + " " + p2 + " " + p3 + ":" + filePath);
5431 }
5432
5433 IccIoResult response =
5434 (IccIoResult) sendRequest(CMD_EXCHANGE_SIM_IO,
5435 new IccAPDUArgument(-1, fileID, command, p1, p2, p3, filePath),
5436 subId);
5437
5438 if (DBG) {
5439 log("Exchange SIM_IO [R]" + response);
5440 }
5441
5442 byte[] result = null;
5443 int length = 2;
5444 if (response.payload != null) {
5445 length = 2 + response.payload.length;
5446 result = new byte[length];
5447 System.arraycopy(response.payload, 0, result, 0, response.payload.length);
5448 } else {
5449 result = new byte[length];
5450 }
5451
5452 result[length - 1] = (byte) response.sw2;
5453 result[length - 2] = (byte) response.sw1;
5454 return result;
5455 } finally {
5456 Binder.restoreCallingIdentity(identity);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07005457 }
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07005458 }
5459
Nathan Haroldb3014052017-01-25 15:57:32 -08005460 /**
5461 * Get the forbidden PLMN List from the given app type (ex APPTYPE_USIM)
5462 * on a particular subscription
5463 */
Philip P. Moltmann700a9592019-10-03 11:53:50 -07005464 public String[] getForbiddenPlmns(int subId, int appType, String callingPackage,
5465 String callingFeatureId) {
sqianb6e41952018-03-12 14:54:01 -07005466 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
Philip P. Moltmann700a9592019-10-03 11:53:50 -07005467 mApp, subId, callingPackage, callingFeatureId, "getForbiddenPlmns")) {
sqianb6e41952018-03-12 14:54:01 -07005468 return null;
5469 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005470
5471 final long identity = Binder.clearCallingIdentity();
5472 try {
5473 if (appType != TelephonyManager.APPTYPE_USIM
5474 && appType != TelephonyManager.APPTYPE_SIM) {
5475 loge("getForbiddenPlmnList(): App Type must be USIM or SIM");
5476 return null;
5477 }
5478 Object response = sendRequest(
5479 CMD_GET_FORBIDDEN_PLMNS, new Integer(appType), subId);
5480 if (response instanceof String[]) {
5481 return (String[]) response;
5482 }
yincheng zhao2737e882019-09-06 17:06:54 -07005483 // Response is an Exception of some kind
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005484 // which is signalled to the user as a NULL retval
Nathan Haroldb3014052017-01-25 15:57:32 -08005485 return null;
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005486 } finally {
5487 Binder.restoreCallingIdentity(identity);
Nathan Haroldb3014052017-01-25 15:57:32 -08005488 }
Nathan Haroldb3014052017-01-25 15:57:32 -08005489 }
5490
yincheng zhao2737e882019-09-06 17:06:54 -07005491 /**
5492 * Set the forbidden PLMN list from the given app type (ex APPTYPE_USIM) on a particular
5493 * subscription.
5494 *
5495 * @param subId the id of the subscription.
5496 * @param appType the uicc app type, must be USIM or SIM.
5497 * @param fplmns the Forbiden plmns list that needed to be written to the SIM.
5498 * @param callingPackage the op Package name.
Philip P. Moltmann700a9592019-10-03 11:53:50 -07005499 * @param callingFeatureId the feature in the package.
yincheng zhao2737e882019-09-06 17:06:54 -07005500 * @return number of fplmns that is successfully written to the SIM.
5501 */
Philip P. Moltmann700a9592019-10-03 11:53:50 -07005502 public int setForbiddenPlmns(int subId, int appType, List<String> fplmns, String callingPackage,
5503 String callingFeatureId) {
Jayachandran C5b0d75a2021-10-21 22:15:27 -07005504 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
5505 mApp, subId, "setForbiddenPlmns");
5506
yincheng zhao2737e882019-09-06 17:06:54 -07005507 if (appType != TelephonyManager.APPTYPE_USIM && appType != TelephonyManager.APPTYPE_SIM) {
5508 loge("setForbiddenPlmnList(): App Type must be USIM or SIM");
5509 throw new IllegalArgumentException("Invalid appType: App Type must be USIM or SIM");
5510 }
5511 if (fplmns == null) {
5512 throw new IllegalArgumentException("Fplmn List provided is null");
5513 }
5514 for (String fplmn : fplmns) {
5515 if (!CellIdentity.isValidPlmn(fplmn)) {
5516 throw new IllegalArgumentException("Invalid fplmn provided: " + fplmn);
5517 }
5518 }
5519 final long identity = Binder.clearCallingIdentity();
5520 try {
5521 Object response = sendRequest(
5522 CMD_SET_FORBIDDEN_PLMNS,
5523 new Pair<Integer, List<String>>(new Integer(appType), fplmns),
5524 subId);
5525 return (int) response;
5526 } finally {
5527 Binder.restoreCallingIdentity(identity);
5528 }
5529 }
5530
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07005531 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08005532 public String sendEnvelopeWithStatus(int subId, String content) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08005533 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
5534 mApp, subId, "sendEnvelopeWithStatus");
Evan Charltonc66da362014-05-16 14:06:40 -07005535
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005536 final long identity = Binder.clearCallingIdentity();
5537 try {
5538 IccIoResult response = (IccIoResult) sendRequest(CMD_SEND_ENVELOPE, content, subId);
5539 if (response.payload == null) {
5540 return "";
5541 }
Evan Charltonc66da362014-05-16 14:06:40 -07005542
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005543 // Append the returned status code to the end of the response payload.
5544 String s = Integer.toHexString(
5545 (response.sw1 << 8) + response.sw2 + 0x10000).substring(1);
5546 s = IccUtils.bytesToHexString(response.payload) + s;
5547 return s;
5548 } finally {
5549 Binder.restoreCallingIdentity(identity);
5550 }
Evan Charltonc66da362014-05-16 14:06:40 -07005551 }
5552
Jake Hambye994d462014-02-03 13:10:13 -08005553 /**
5554 * Read one of the NV items defined in {@link com.android.internal.telephony.RadioNVItems}
5555 * and {@code ril_nv_items.h}. Used for device configuration by some CDMA operators.
5556 *
5557 * @param itemID the ID of the item to read
5558 * @return the NV item as a String, or null on error.
5559 */
5560 @Override
5561 public String nvReadItem(int itemID) {
vagdeviaf9a5b92018-08-15 16:01:53 -07005562 WorkSource workSource = getWorkSource(Binder.getCallingUid());
Jeff Davidson7e17e312018-02-13 18:17:36 -08005563 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
5564 mApp, getDefaultSubscription(), "nvReadItem");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005565
5566 final long identity = Binder.clearCallingIdentity();
5567 try {
5568 if (DBG) log("nvReadItem: item " + itemID);
vagdeviaf9a5b92018-08-15 16:01:53 -07005569 String value = (String) sendRequest(CMD_NV_READ_ITEM, itemID, workSource);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005570 if (DBG) log("nvReadItem: item " + itemID + " is \"" + value + '"');
5571 return value;
5572 } finally {
5573 Binder.restoreCallingIdentity(identity);
5574 }
Jake Hambye994d462014-02-03 13:10:13 -08005575 }
5576
5577 /**
5578 * Write one of the NV items defined in {@link com.android.internal.telephony.RadioNVItems}
5579 * and {@code ril_nv_items.h}. Used for device configuration by some CDMA operators.
5580 *
5581 * @param itemID the ID of the item to read
5582 * @param itemValue the value to write, as a String
5583 * @return true on success; false on any failure
5584 */
5585 @Override
5586 public boolean nvWriteItem(int itemID, String itemValue) {
vagdeviaf9a5b92018-08-15 16:01:53 -07005587 WorkSource workSource = getWorkSource(Binder.getCallingUid());
Jeff Davidson7e17e312018-02-13 18:17:36 -08005588 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
5589 mApp, getDefaultSubscription(), "nvWriteItem");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005590
5591 final long identity = Binder.clearCallingIdentity();
5592 try {
5593 if (DBG) log("nvWriteItem: item " + itemID + " value \"" + itemValue + '"');
5594 Boolean success = (Boolean) sendRequest(CMD_NV_WRITE_ITEM,
vagdeviaf9a5b92018-08-15 16:01:53 -07005595 new Pair<Integer, String>(itemID, itemValue), workSource);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005596 if (DBG) log("nvWriteItem: item " + itemID + ' ' + (success ? "ok" : "fail"));
5597 return success;
5598 } finally {
5599 Binder.restoreCallingIdentity(identity);
5600 }
Jake Hambye994d462014-02-03 13:10:13 -08005601 }
5602
5603 /**
5604 * Update the CDMA Preferred Roaming List (PRL) in the radio NV storage.
5605 * Used for device configuration by some CDMA operators.
5606 *
5607 * @param preferredRoamingList byte array containing the new PRL
5608 * @return true on success; false on any failure
5609 */
5610 @Override
5611 public boolean nvWriteCdmaPrl(byte[] preferredRoamingList) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08005612 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
5613 mApp, getDefaultSubscription(), "nvWriteCdmaPrl");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005614
5615 final long identity = Binder.clearCallingIdentity();
5616 try {
5617 if (DBG) log("nvWriteCdmaPrl: value: " + HexDump.toHexString(preferredRoamingList));
5618 Boolean success = (Boolean) sendRequest(CMD_NV_WRITE_CDMA_PRL, preferredRoamingList);
5619 if (DBG) log("nvWriteCdmaPrl: " + (success ? "ok" : "fail"));
5620 return success;
5621 } finally {
5622 Binder.restoreCallingIdentity(identity);
5623 }
Jake Hambye994d462014-02-03 13:10:13 -08005624 }
5625
5626 /**
chen xu6dac5ab2018-10-26 17:39:23 -07005627 * Rollback modem configurations to factory default except some config which are in whitelist.
Jake Hambye994d462014-02-03 13:10:13 -08005628 * Used for device configuration by some CDMA operators.
5629 *
chen xu6dac5ab2018-10-26 17:39:23 -07005630 * @param slotIndex - device slot.
5631 *
Jake Hambye994d462014-02-03 13:10:13 -08005632 * @return true on success; false on any failure
5633 */
5634 @Override
chen xu6dac5ab2018-10-26 17:39:23 -07005635 public boolean resetModemConfig(int slotIndex) {
5636 Phone phone = PhoneFactory.getPhone(slotIndex);
5637 if (phone != null) {
5638 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
5639 mApp, phone.getSubId(), "resetModemConfig");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005640
chen xu6dac5ab2018-10-26 17:39:23 -07005641 final long identity = Binder.clearCallingIdentity();
5642 try {
5643 Boolean success = (Boolean) sendRequest(CMD_RESET_MODEM_CONFIG, null);
5644 if (DBG) log("resetModemConfig:" + ' ' + (success ? "ok" : "fail"));
5645 return success;
5646 } finally {
5647 Binder.restoreCallingIdentity(identity);
5648 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005649 }
chen xu6dac5ab2018-10-26 17:39:23 -07005650 return false;
5651 }
5652
5653 /**
5654 * Generate a radio modem reset. Used for device configuration by some CDMA operators.
5655 *
5656 * @param slotIndex - device slot.
5657 *
5658 * @return true on success; false on any failure
5659 */
5660 @Override
5661 public boolean rebootModem(int slotIndex) {
5662 Phone phone = PhoneFactory.getPhone(slotIndex);
5663 if (phone != null) {
5664 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
5665 mApp, phone.getSubId(), "rebootModem");
5666
5667 final long identity = Binder.clearCallingIdentity();
5668 try {
5669 Boolean success = (Boolean) sendRequest(CMD_MODEM_REBOOT, null);
5670 if (DBG) log("rebootModem:" + ' ' + (success ? "ok" : "fail"));
5671 return success;
5672 } finally {
5673 Binder.restoreCallingIdentity(identity);
5674 }
5675 }
5676 return false;
Jake Hambye994d462014-02-03 13:10:13 -08005677 }
Jake Hamby7c27be32014-03-03 13:25:59 -08005678
Brad Ebinger51f743a2017-01-23 13:50:20 -08005679 /**
Grace Jiaaa2eb6b2020-01-09 16:26:08 -08005680 * Toggle IMS disable and enable for the framework to reset it. See {@link #enableIms(int)} and
5681 * {@link #disableIms(int)}.
5682 * @param slotIndex device slot.
5683 */
5684 public void resetIms(int slotIndex) {
5685 enforceModifyPermission();
5686
5687 final long identity = Binder.clearCallingIdentity();
5688 try {
5689 if (mImsResolver == null) {
5690 // may happen if the does not support IMS.
5691 return;
5692 }
5693 mImsResolver.disableIms(slotIndex);
5694 mImsResolver.enableIms(slotIndex);
5695 } finally {
5696 Binder.restoreCallingIdentity(identity);
5697 }
5698 }
5699
5700 /**
Brad Ebinger22bc3e42018-01-16 09:39:35 -08005701 * Enables IMS for the framework. This will trigger IMS registration and ImsFeature capability
5702 * status updates, if not already enabled.
Brad Ebinger51f743a2017-01-23 13:50:20 -08005703 */
Brad Ebinger22bc3e42018-01-16 09:39:35 -08005704 public void enableIms(int slotId) {
Brad Ebinger51f743a2017-01-23 13:50:20 -08005705 enforceModifyPermission();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005706
5707 final long identity = Binder.clearCallingIdentity();
5708 try {
Brad Ebinger24c29992019-12-05 13:03:21 -08005709 if (mImsResolver == null) {
Brad Ebinger9c0eb502019-01-23 15:06:19 -08005710 // may happen if the device does not support IMS.
5711 return;
5712 }
Brad Ebinger24c29992019-12-05 13:03:21 -08005713 mImsResolver.enableIms(slotId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005714 } finally {
5715 Binder.restoreCallingIdentity(identity);
5716 }
Brad Ebinger34bef922017-11-09 10:27:08 -08005717 }
5718
5719 /**
Brad Ebinger22bc3e42018-01-16 09:39:35 -08005720 * Disables IMS for the framework. This will trigger IMS de-registration and trigger ImsFeature
5721 * status updates to disabled.
Brad Ebinger34bef922017-11-09 10:27:08 -08005722 */
Brad Ebinger22bc3e42018-01-16 09:39:35 -08005723 public void disableIms(int slotId) {
5724 enforceModifyPermission();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005725
5726 final long identity = Binder.clearCallingIdentity();
5727 try {
Brad Ebinger24c29992019-12-05 13:03:21 -08005728 if (mImsResolver == null) {
Brad Ebinger9c0eb502019-01-23 15:06:19 -08005729 // may happen if the device does not support IMS.
5730 return;
5731 }
Brad Ebinger24c29992019-12-05 13:03:21 -08005732 mImsResolver.disableIms(slotId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005733 } finally {
5734 Binder.restoreCallingIdentity(identity);
5735 }
Brad Ebinger22bc3e42018-01-16 09:39:35 -08005736 }
5737
5738 /**
Brad Ebinger67b3e042020-09-11 12:45:11 -07005739 * Registers for updates to the MmTelFeature connection through the IImsServiceFeatureCallback
5740 * callback.
Brad Ebinger22bc3e42018-01-16 09:39:35 -08005741 */
Brad Ebinger67b3e042020-09-11 12:45:11 -07005742 @Override
Brad Ebingerf6aca002020-10-01 13:51:05 -07005743 public void registerMmTelFeatureCallback(int slotId, IImsServiceFeatureCallback callback) {
Brad Ebinger34bef922017-11-09 10:27:08 -08005744 enforceModifyPermission();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005745
5746 final long identity = Binder.clearCallingIdentity();
5747 try {
Brad Ebinger24c29992019-12-05 13:03:21 -08005748 if (mImsResolver == null) {
Brad Ebinger67b3e042020-09-11 12:45:11 -07005749 throw new ServiceSpecificException(ImsException.CODE_ERROR_UNSUPPORTED_OPERATION,
5750 "Device does not support IMS");
Brad Ebinger9c0eb502019-01-23 15:06:19 -08005751 }
Brad Ebingerf6aca002020-10-01 13:51:05 -07005752 mImsResolver.listenForFeature(slotId, ImsFeature.FEATURE_MMTEL, callback);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005753 } finally {
5754 Binder.restoreCallingIdentity(identity);
5755 }
Brad Ebinger34bef922017-11-09 10:27:08 -08005756 }
Brad Ebinger5f64b052017-12-14 14:26:15 -08005757 /**
Brad Ebinger075ff3a2020-05-18 17:52:58 -07005758 * Unregister a previously registered IImsServiceFeatureCallback associated with an ImsFeature.
5759 */
Brad Ebinger67b3e042020-09-11 12:45:11 -07005760 @Override
5761 public void unregisterImsFeatureCallback(IImsServiceFeatureCallback callback) {
Brad Ebinger075ff3a2020-05-18 17:52:58 -07005762 enforceModifyPermission();
5763
5764 final long identity = Binder.clearCallingIdentity();
5765 try {
5766 if (mImsResolver == null) return;
Brad Ebinger67b3e042020-09-11 12:45:11 -07005767 mImsResolver.unregisterImsFeatureCallback(callback);
Brad Ebinger075ff3a2020-05-18 17:52:58 -07005768 } finally {
5769 Binder.restoreCallingIdentity(identity);
5770 }
5771 }
5772
5773 /**
Brad Ebinger5f64b052017-12-14 14:26:15 -08005774 * Returns the {@link IImsRegistration} structure associated with the slotId and feature
Brad Ebinger9c0eb502019-01-23 15:06:19 -08005775 * specified or null if IMS is not supported on the slot specified.
Brad Ebinger5f64b052017-12-14 14:26:15 -08005776 */
5777 public IImsRegistration getImsRegistration(int slotId, int feature) throws RemoteException {
5778 enforceModifyPermission();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005779
5780 final long identity = Binder.clearCallingIdentity();
5781 try {
Brad Ebinger24c29992019-12-05 13:03:21 -08005782 if (mImsResolver == null) {
Brad Ebinger9c0eb502019-01-23 15:06:19 -08005783 // may happen if the device does not support IMS.
5784 return null;
5785 }
Brad Ebinger24c29992019-12-05 13:03:21 -08005786 return mImsResolver.getImsRegistration(slotId, feature);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005787 } finally {
5788 Binder.restoreCallingIdentity(identity);
5789 }
Brad Ebinger5f64b052017-12-14 14:26:15 -08005790 }
5791
Brad Ebinger22bc3e42018-01-16 09:39:35 -08005792 /**
5793 * Returns the {@link IImsConfig} structure associated with the slotId and feature
Brad Ebinger9c0eb502019-01-23 15:06:19 -08005794 * specified or null if IMS is not supported on the slot specified.
Brad Ebinger22bc3e42018-01-16 09:39:35 -08005795 */
5796 public IImsConfig getImsConfig(int slotId, int feature) throws RemoteException {
5797 enforceModifyPermission();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005798
5799 final long identity = Binder.clearCallingIdentity();
5800 try {
Brad Ebinger24c29992019-12-05 13:03:21 -08005801 if (mImsResolver == null) {
Brad Ebinger9c0eb502019-01-23 15:06:19 -08005802 // may happen if the device does not support IMS.
5803 return null;
5804 }
Brad Ebinger24c29992019-12-05 13:03:21 -08005805 return mImsResolver.getImsConfig(slotId, feature);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005806 } finally {
5807 Binder.restoreCallingIdentity(identity);
5808 }
Brad Ebinger22bc3e42018-01-16 09:39:35 -08005809 }
5810
Brad Ebinger884c07b2018-02-15 16:17:40 -08005811 /**
Brad Ebingerdac2f002018-04-03 15:17:52 -07005812 * Sets the ImsService Package Name that Telephony will bind to.
5813 *
Brad Ebinger24c29992019-12-05 13:03:21 -08005814 * @param slotIndex the slot ID that the ImsService should bind for.
5815 * @param isCarrierService true if the ImsService is the carrier override, false if the
Brad Ebingerdac2f002018-04-03 15:17:52 -07005816 * ImsService is the device default ImsService.
Brad Ebinger24c29992019-12-05 13:03:21 -08005817 * @param featureTypes An integer array of feature types associated with a packageName.
5818 * @param packageName The name of the package that the current configuration will be replaced
5819 * with.
Brad Ebingerdac2f002018-04-03 15:17:52 -07005820 * @return true if setting the ImsService to bind to succeeded, false if it did not.
Brad Ebingerdac2f002018-04-03 15:17:52 -07005821 */
Brad Ebinger24c29992019-12-05 13:03:21 -08005822 public boolean setBoundImsServiceOverride(int slotIndex, boolean isCarrierService,
5823 int[] featureTypes, String packageName) {
Brad Ebinger24c29992019-12-05 13:03:21 -08005824 TelephonyPermissions.enforceShellOnly(Binder.getCallingUid(), "setBoundImsServiceOverride");
Brad Ebingerde696de2018-04-06 09:56:40 -07005825 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(mApp,
Jack Yude40a3f2022-11-19 22:29:12 -08005826 SubscriptionManager.getSubscriptionId(slotIndex), "setBoundImsServiceOverride");
Brad Ebingerde696de2018-04-06 09:56:40 -07005827
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005828 final long identity = Binder.clearCallingIdentity();
5829 try {
Brad Ebinger24c29992019-12-05 13:03:21 -08005830 if (mImsResolver == null) {
Brad Ebinger9c0eb502019-01-23 15:06:19 -08005831 // may happen if the device does not support IMS.
5832 return false;
5833 }
Brad Ebinger24c29992019-12-05 13:03:21 -08005834 Map<Integer, String> featureConfig = new HashMap<>();
5835 for (int featureType : featureTypes) {
5836 featureConfig.put(featureType, packageName);
5837 }
5838 return mImsResolver.overrideImsServiceConfiguration(slotIndex, isCarrierService,
5839 featureConfig);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005840 } finally {
5841 Binder.restoreCallingIdentity(identity);
5842 }
Brad Ebingerdac2f002018-04-03 15:17:52 -07005843 }
5844
5845 /**
Brad Ebinger999d3302020-11-25 14:31:39 -08005846 * Clears any carrier ImsService overrides for the slot index specified that were previously
5847 * set with {@link #setBoundImsServiceOverride(int, boolean, int[], String)}.
5848 *
5849 * This should only be used for testing.
5850 *
5851 * @param slotIndex the slot ID that the ImsService should bind for.
5852 * @return true if clearing the carrier ImsService override succeeded or false if it did not.
5853 */
5854 @Override
5855 public boolean clearCarrierImsServiceOverride(int slotIndex) {
Brad Ebinger999d3302020-11-25 14:31:39 -08005856 TelephonyPermissions.enforceShellOnly(Binder.getCallingUid(),
5857 "clearCarrierImsServiceOverride");
5858 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(mApp,
Jack Yude40a3f2022-11-19 22:29:12 -08005859 SubscriptionManager.getSubscriptionId(slotIndex), "clearCarrierImsServiceOverride");
Brad Ebinger999d3302020-11-25 14:31:39 -08005860
5861 final long identity = Binder.clearCallingIdentity();
5862 try {
5863 if (mImsResolver == null) {
5864 // may happen if the device does not support IMS.
5865 return false;
5866 }
5867 return mImsResolver.clearCarrierImsServiceConfiguration(slotIndex);
5868 } finally {
5869 Binder.restoreCallingIdentity(identity);
5870 }
5871 }
5872
5873 /**
Brad Ebinger24c29992019-12-05 13:03:21 -08005874 * Return the package name of the currently bound ImsService.
Brad Ebingerdac2f002018-04-03 15:17:52 -07005875 *
5876 * @param slotId The slot that the ImsService is associated with.
5877 * @param isCarrierImsService true, if the ImsService is a carrier override, false if it is
5878 * the device default.
Brad Ebinger24c29992019-12-05 13:03:21 -08005879 * @param featureType The feature associated with the queried configuration.
Brad Ebingerdac2f002018-04-03 15:17:52 -07005880 * @return the package name of the ImsService configuration.
5881 */
Brad Ebinger24c29992019-12-05 13:03:21 -08005882 public String getBoundImsServicePackage(int slotId, boolean isCarrierImsService,
5883 @ImsFeature.FeatureType int featureType) {
Brad Ebinger24c29992019-12-05 13:03:21 -08005884 TelephonyPermissions
Jack Yude40a3f2022-11-19 22:29:12 -08005885 .enforceCallingOrSelfReadPrivilegedPhoneStatePermissionOrCarrierPrivilege(mApp,
5886 SubscriptionManager.getSubscriptionId(slotId), "getBoundImsServicePackage");
Brad Ebingerde696de2018-04-06 09:56:40 -07005887
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005888 final long identity = Binder.clearCallingIdentity();
5889 try {
Brad Ebinger24c29992019-12-05 13:03:21 -08005890 if (mImsResolver == null) {
Brad Ebinger9c0eb502019-01-23 15:06:19 -08005891 // may happen if the device does not support IMS.
5892 return "";
5893 }
Brad Ebingera80c3312019-12-02 10:59:39 -08005894 // TODO: change API to query RCS separately.
Brad Ebinger24c29992019-12-05 13:03:21 -08005895 return mImsResolver.getImsServiceConfiguration(slotId, isCarrierImsService,
5896 featureType);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005897 } finally {
5898 Binder.restoreCallingIdentity(identity);
5899 }
Brad Ebingerdac2f002018-04-03 15:17:52 -07005900 }
5901
Brad Ebingerbc7dd582019-10-17 17:03:22 -07005902 /**
5903 * Get the MmTelFeature state associated with the requested subscription id.
5904 * @param subId The subscription that the MmTelFeature is associated with.
5905 * @param callback A callback with an integer containing the
5906 * {@link android.telephony.ims.feature.ImsFeature.ImsState} associated with the MmTelFeature.
5907 */
5908 @Override
5909 public void getImsMmTelFeatureState(int subId, IIntegerConsumer callback) {
5910 enforceReadPrivilegedPermission("getImsMmTelFeatureState");
Brad Ebingera2628302022-02-18 03:44:55 +00005911 if (!ImsManager.isImsSupportedOnDevice(mApp)) {
5912 throw new ServiceSpecificException(ImsException.CODE_ERROR_UNSUPPORTED_OPERATION,
5913 "IMS not available on device.");
5914 }
Brad Ebingerbc7dd582019-10-17 17:03:22 -07005915 final long token = Binder.clearCallingIdentity();
5916 try {
Brad Ebingera2628302022-02-18 03:44:55 +00005917 int slotId = getSlotIndex(subId);
5918 if (slotId <= SubscriptionManager.INVALID_SIM_SLOT_INDEX) {
5919 Log.w(LOG_TAG, "getImsMmTelFeatureState: called with an inactive subscription '"
5920 + subId + "'");
5921 throw new ServiceSpecificException(ImsException.CODE_ERROR_INVALID_SUBSCRIPTION);
5922 }
5923 verifyImsMmTelConfiguredOrThrow(slotId);
5924 ImsManager.getInstance(mApp, slotId).getImsServiceState(anInteger -> {
5925 try {
5926 callback.accept(anInteger == null ? ImsFeature.STATE_UNAVAILABLE : anInteger);
5927 } catch (RemoteException e) {
5928 Log.w(LOG_TAG, "getImsMmTelFeatureState: remote caller is no longer running. "
5929 + "Ignore");
5930 }
Brad Ebingerbc7dd582019-10-17 17:03:22 -07005931 });
Brad Ebinger919631e2021-06-02 17:46:35 -07005932 } catch (ImsException e) {
5933 throw new ServiceSpecificException(e.getCode());
Brad Ebingerbc7dd582019-10-17 17:03:22 -07005934 } finally {
5935 Binder.restoreCallingIdentity(token);
5936 }
5937 }
5938
Daniel Brightbb5840b2021-01-12 15:48:18 -08005939 /**
5940 * Sets the ims registration state on all valid {@link Phone}s.
5941 */
5942 public void setImsRegistrationState(final boolean registered) {
Wink Saville36469e72014-06-11 15:17:00 -07005943 enforceModifyPermission();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005944
5945 final long identity = Binder.clearCallingIdentity();
5946 try {
Daniel Brightbb5840b2021-01-12 15:48:18 -08005947 // NOTE: Before S, this method only set the default phone.
5948 for (final Phone phone : PhoneFactory.getPhones()) {
5949 if (SubscriptionManager.isValidSubscriptionId(phone.getSubId())) {
5950 phone.setImsRegistrationState(registered);
5951 }
5952 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005953 } finally {
5954 Binder.restoreCallingIdentity(identity);
5955 }
Wink Saville36469e72014-06-11 15:17:00 -07005956 }
5957
5958 /**
Stuart Scott54788802015-03-30 13:18:01 -07005959 * Set the network selection mode to automatic.
5960 *
5961 */
5962 @Override
5963 public void setNetworkSelectionModeAutomatic(int subId) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08005964 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
5965 mApp, subId, "setNetworkSelectionModeAutomatic");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005966
5967 final long identity = Binder.clearCallingIdentity();
5968 try {
shilufc958392020-01-20 11:36:01 -08005969 if (!isActiveSubscription(subId)) {
5970 return;
5971 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005972 if (DBG) log("setNetworkSelectionModeAutomatic: subId " + subId);
Rambo Wang0f050d82021-02-12 11:43:36 -08005973 sendRequest(CMD_SET_NETWORK_SELECTION_MODE_AUTOMATIC, null, subId,
5974 SET_NETWORK_SELECTION_MODE_AUTOMATIC_TIMEOUT_MS);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005975 } finally {
5976 Binder.restoreCallingIdentity(identity);
5977 }
Stuart Scott54788802015-03-30 13:18:01 -07005978 }
5979
Jack Yud10cdd42020-09-28 20:28:01 -07005980 /**
Pengquan Mengea84e042018-09-20 14:57:26 -07005981 * Ask the radio to connect to the input network and change selection mode to manual.
5982 *
5983 * @param subId the id of the subscription.
5984 * @param operatorInfo the operator information, included the PLMN, long name and short name of
5985 * the operator to attach to.
5986 * @param persistSelection whether the selection will persist until reboot. If true, only allows
5987 * attaching to the selected PLMN until reboot; otherwise, attach to the chosen PLMN and resume
5988 * normal network selection next time.
5989 * @return {@code true} on success; {@code true} on any failure.
Shishir Agrawal302c8692015-06-19 13:49:39 -07005990 */
5991 @Override
Pengquan Mengea84e042018-09-20 14:57:26 -07005992 public boolean setNetworkSelectionModeManual(
5993 int subId, OperatorInfo operatorInfo, boolean persistSelection) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08005994 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
5995 mApp, subId, "setNetworkSelectionModeManual");
Pengquan Menge92a50d2018-09-21 15:54:48 -07005996
Jack Yudf049d72022-12-02 22:48:35 -08005997 final long identity = Binder.clearCallingIdentity();
Pengquan Menge92a50d2018-09-21 15:54:48 -07005998 if (!isActiveSubscription(subId)) {
5999 return false;
6000 }
6001
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006002 try {
Pengquan Mengea84e042018-09-20 14:57:26 -07006003 ManualNetworkSelectionArgument arg = new ManualNetworkSelectionArgument(operatorInfo,
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006004 persistSelection);
Pengquan Mengea84e042018-09-20 14:57:26 -07006005 if (DBG) {
6006 log("setNetworkSelectionModeManual: subId: " + subId
6007 + " operator: " + operatorInfo);
6008 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006009 return (Boolean) sendRequest(CMD_SET_NETWORK_SELECTION_MODE_MANUAL, arg, subId);
6010 } finally {
6011 Binder.restoreCallingIdentity(identity);
6012 }
Shishir Agrawal302c8692015-06-19 13:49:39 -07006013 }
shilu84f6e8b2019-12-19 13:58:01 -08006014 /**
6015 * Get the manual network selection
6016 *
6017 * @param subId the id of the subscription.
6018 *
6019 * @return the previously saved user selected PLMN
6020 */
6021 @Override
6022 public String getManualNetworkSelectionPlmn(int subId) {
6023 TelephonyPermissions
Nathan Harold62c68512021-04-06 11:26:02 -07006024 .enforceCallingOrSelfReadPrecisePhoneStatePermissionOrCarrierPrivilege(
shilu84f6e8b2019-12-19 13:58:01 -08006025 mApp, subId, "getManualNetworkSelectionPlmn");
6026
6027 final long identity = Binder.clearCallingIdentity();
6028 try {
6029 if (!isActiveSubscription(subId)) {
shilufa1c2592020-03-10 10:59:43 -07006030 throw new IllegalArgumentException("Invalid Subscription Id: " + subId);
shilu84f6e8b2019-12-19 13:58:01 -08006031 }
6032
6033 final Phone phone = getPhone(subId);
6034 if (phone == null) {
shilufa1c2592020-03-10 10:59:43 -07006035 throw new IllegalArgumentException("Invalid Subscription Id: " + subId);
shilu84f6e8b2019-12-19 13:58:01 -08006036 }
6037 OperatorInfo networkSelection = phone.getSavedNetworkSelection();
6038 return TextUtils.isEmpty(networkSelection.getOperatorNumeric())
6039 ? phone.getManualNetworkSelectionPlmn() : networkSelection.getOperatorNumeric();
6040 } finally {
6041 Binder.restoreCallingIdentity(identity);
6042 }
6043 }
Shishir Agrawal302c8692015-06-19 13:49:39 -07006044
6045 /**
6046 * Scans for available networks.
6047 */
6048 @Override
Philip P. Moltmann3a2772a2019-10-04 08:15:00 -07006049 public CellNetworkScanResult getCellNetworkScanResults(int subId, String callingPackage,
6050 String callingFeatureId) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08006051 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
6052 mApp, subId, "getCellNetworkScanResults");
Hall Liuf19c44f2018-11-27 14:38:17 -08006053 LocationAccessPolicy.LocationPermissionResult locationResult =
6054 LocationAccessPolicy.checkLocationPermission(mApp,
6055 new LocationAccessPolicy.LocationPermissionQuery.Builder()
6056 .setCallingPackage(callingPackage)
Philip P. Moltmann3a2772a2019-10-04 08:15:00 -07006057 .setCallingFeatureId(callingFeatureId)
Hall Liuf19c44f2018-11-27 14:38:17 -08006058 .setCallingPid(Binder.getCallingPid())
6059 .setCallingUid(Binder.getCallingUid())
6060 .setMethod("getCellNetworkScanResults")
6061 .setMinSdkVersionForFine(Build.VERSION_CODES.Q)
Hall Liuc4a3e422020-05-26 17:18:03 -07006062 .setMinSdkVersionForCoarse(Build.VERSION_CODES.Q)
6063 .setMinSdkVersionForEnforcement(Build.VERSION_CODES.Q)
Hall Liuf19c44f2018-11-27 14:38:17 -08006064 .build());
6065 switch (locationResult) {
6066 case DENIED_HARD:
6067 throw new SecurityException("Not allowed to access scan results -- location");
6068 case DENIED_SOFT:
6069 return null;
6070 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006071
Pengquan Menga1bb6272018-09-06 09:59:22 -07006072 long identity = Binder.clearCallingIdentity();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006073 try {
6074 if (DBG) log("getCellNetworkScanResults: subId " + subId);
Pengquan Menga1bb6272018-09-06 09:59:22 -07006075 return (CellNetworkScanResult) sendRequest(
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006076 CMD_PERFORM_NETWORK_SCAN, null, subId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006077 } finally {
6078 Binder.restoreCallingIdentity(identity);
6079 }
Shishir Agrawal302c8692015-06-19 13:49:39 -07006080 }
6081
6082 /**
Shuo Qian4a594052020-01-23 11:59:30 -08006083 * Get the call forwarding info, given the call forwarding reason.
6084 */
6085 @Override
Hall Liu27d24262020-09-18 19:04:59 -07006086 public void getCallForwarding(int subId, int callForwardingReason,
6087 ICallForwardingInfoCallback callback) {
Shuo Qian4a594052020-01-23 11:59:30 -08006088 enforceReadPrivilegedPermission("getCallForwarding");
6089 long identity = Binder.clearCallingIdentity();
6090 try {
6091 if (DBG) {
6092 log("getCallForwarding: subId " + subId
6093 + " callForwardingReason" + callForwardingReason);
6094 }
Hall Liu27d24262020-09-18 19:04:59 -07006095
6096 Phone phone = getPhone(subId);
6097 if (phone == null) {
6098 try {
Hall Liu940c4ca2020-09-29 17:10:18 -07006099 callback.onError(
6100 TelephonyManager.CallForwardingInfoCallback.RESULT_ERROR_UNKNOWN);
Hall Liu27d24262020-09-18 19:04:59 -07006101 } catch (RemoteException e) {
6102 // ignore
6103 }
6104 return;
6105 }
6106
6107 Pair<Integer, TelephonyManager.CallForwardingInfoCallback> argument = Pair.create(
6108 callForwardingReason, new TelephonyManager.CallForwardingInfoCallback() {
6109 @Override
6110 public void onCallForwardingInfoAvailable(CallForwardingInfo info) {
6111 try {
6112 callback.onCallForwardingInfoAvailable(info);
6113 } catch (RemoteException e) {
6114 // ignore
6115 }
6116 }
6117
6118 @Override
6119 public void onError(int error) {
6120 try {
6121 callback.onError(error);
6122 } catch (RemoteException e) {
6123 // ignore
6124 }
6125 }
6126 });
6127 sendRequestAsync(CMD_GET_CALL_FORWARDING, argument, phone, null);
Shuo Qian4a594052020-01-23 11:59:30 -08006128 } finally {
6129 Binder.restoreCallingIdentity(identity);
6130 }
6131 }
6132
6133 /**
6134 * Sets the voice call forwarding info including status (enable/disable), call forwarding
6135 * reason, the number to forward, and the timeout before the forwarding is attempted.
6136 */
6137 @Override
Hall Liu27d24262020-09-18 19:04:59 -07006138 public void setCallForwarding(int subId, CallForwardingInfo callForwardingInfo,
6139 IIntegerConsumer callback) {
Shuo Qian4a594052020-01-23 11:59:30 -08006140 enforceModifyPermission();
6141 long identity = Binder.clearCallingIdentity();
6142 try {
6143 if (DBG) {
6144 log("setCallForwarding: subId " + subId
6145 + " callForwardingInfo" + callForwardingInfo);
6146 }
Hall Liu27d24262020-09-18 19:04:59 -07006147
6148 Phone phone = getPhone(subId);
6149 if (phone == null) {
6150 try {
Hall Liu940c4ca2020-09-29 17:10:18 -07006151 callback.accept(
6152 TelephonyManager.CallForwardingInfoCallback.RESULT_ERROR_UNKNOWN);
Hall Liu27d24262020-09-18 19:04:59 -07006153 } catch (RemoteException e) {
6154 // ignore
6155 }
6156 return;
6157 }
6158
6159 Pair<CallForwardingInfo, Consumer<Integer>> arguments = Pair.create(callForwardingInfo,
6160 FunctionalUtils.ignoreRemoteException(callback::accept));
6161
6162 sendRequestAsync(CMD_SET_CALL_FORWARDING, arguments, phone, null);
Shuo Qian4a594052020-01-23 11:59:30 -08006163 } finally {
6164 Binder.restoreCallingIdentity(identity);
6165 }
6166 }
6167
6168 /**
Hall Liu27d24262020-09-18 19:04:59 -07006169 * Get the call waiting status for a subId.
Shuo Qian4a594052020-01-23 11:59:30 -08006170 */
6171 @Override
Hall Liu27d24262020-09-18 19:04:59 -07006172 public void getCallWaitingStatus(int subId, IIntegerConsumer callback) {
SongFerngWang0e767992021-03-31 22:08:45 +08006173 enforceReadPrivilegedPermission("getCallWaitingStatus");
Shuo Qian4a594052020-01-23 11:59:30 -08006174 long identity = Binder.clearCallingIdentity();
6175 try {
Hall Liu27d24262020-09-18 19:04:59 -07006176 Phone phone = getPhone(subId);
6177 if (phone == null) {
6178 try {
6179 callback.accept(TelephonyManager.CALL_WAITING_STATUS_UNKNOWN_ERROR);
6180 } catch (RemoteException e) {
6181 // ignore
6182 }
6183 return;
6184 }
SongFerngWang0e767992021-03-31 22:08:45 +08006185 CarrierConfigManager configManager = new CarrierConfigManager(phone.getContext());
6186 PersistableBundle c = configManager.getConfigForSubId(subId);
6187 boolean requireUssd = c.getBoolean(
6188 CarrierConfigManager.KEY_USE_CALL_WAITING_USSD_BOOL, false);
Hall Liu27d24262020-09-18 19:04:59 -07006189
Shuo Qian4a594052020-01-23 11:59:30 -08006190 if (DBG) log("getCallWaitingStatus: subId " + subId);
SongFerngWang0e767992021-03-31 22:08:45 +08006191 if (requireUssd) {
6192 CarrierXmlParser carrierXmlParser = new CarrierXmlParser(phone.getContext(),
6193 getSubscriptionCarrierId(subId));
6194 String newUssdCommand = "";
6195 try {
6196 newUssdCommand = carrierXmlParser.getFeature(
6197 CarrierXmlParser.FEATURE_CALL_WAITING)
6198 .makeCommand(CarrierXmlParser.SsEntry.SSAction.QUERY, null);
6199 } catch (NullPointerException e) {
6200 loge("Failed to generate USSD number" + e);
6201 }
6202 ResultReceiver wrappedCallback = new CallWaitingUssdResultReceiver(
6203 mMainThreadHandler, callback, carrierXmlParser,
6204 CarrierXmlParser.SsEntry.SSAction.QUERY);
6205 final String ussdCommand = newUssdCommand;
6206 Executors.newSingleThreadExecutor().execute(() -> {
6207 handleUssdRequest(subId, ussdCommand, wrappedCallback);
6208 });
6209 } else {
6210 Consumer<Integer> argument = FunctionalUtils.ignoreRemoteException(
6211 callback::accept);
6212 sendRequestAsync(CMD_GET_CALL_WAITING, argument, phone, null);
6213 }
Shuo Qian4a594052020-01-23 11:59:30 -08006214 } finally {
6215 Binder.restoreCallingIdentity(identity);
6216 }
6217 }
6218
6219 /**
Hall Liu27d24262020-09-18 19:04:59 -07006220 * Sets whether call waiting is enabled for a given subId.
Shuo Qian4a594052020-01-23 11:59:30 -08006221 */
6222 @Override
Hall Liu27d24262020-09-18 19:04:59 -07006223 public void setCallWaitingStatus(int subId, boolean enable, IIntegerConsumer callback) {
Shuo Qian4a594052020-01-23 11:59:30 -08006224 enforceModifyPermission();
6225 long identity = Binder.clearCallingIdentity();
6226 try {
Hall Liu27d24262020-09-18 19:04:59 -07006227 if (DBG) log("setCallWaitingStatus: subId " + subId + " enable: " + enable);
6228
6229 Phone phone = getPhone(subId);
6230 if (phone == null) {
6231 try {
6232 callback.accept(TelephonyManager.CALL_WAITING_STATUS_UNKNOWN_ERROR);
6233 } catch (RemoteException e) {
6234 // ignore
6235 }
6236 return;
6237 }
6238
SongFerngWang0e767992021-03-31 22:08:45 +08006239 CarrierConfigManager configManager = new CarrierConfigManager(phone.getContext());
6240 PersistableBundle c = configManager.getConfigForSubId(subId);
6241 boolean requireUssd = c.getBoolean(
6242 CarrierConfigManager.KEY_USE_CALL_WAITING_USSD_BOOL, false);
Hall Liu27d24262020-09-18 19:04:59 -07006243
SongFerngWang0e767992021-03-31 22:08:45 +08006244 if (DBG) log("getCallWaitingStatus: subId " + subId);
6245 if (requireUssd) {
6246 CarrierXmlParser carrierXmlParser = new CarrierXmlParser(phone.getContext(),
6247 getSubscriptionCarrierId(subId));
6248 CarrierXmlParser.SsEntry.SSAction ssAction =
6249 enable ? CarrierXmlParser.SsEntry.SSAction.UPDATE_ACTIVATE
6250 : CarrierXmlParser.SsEntry.SSAction.UPDATE_DEACTIVATE;
6251 String newUssdCommand = "";
6252 try {
6253 newUssdCommand = carrierXmlParser.getFeature(
6254 CarrierXmlParser.FEATURE_CALL_WAITING)
6255 .makeCommand(ssAction, null);
6256 } catch (NullPointerException e) {
6257 loge("Failed to generate USSD number" + e);
6258 }
6259 ResultReceiver wrappedCallback = new CallWaitingUssdResultReceiver(
6260 mMainThreadHandler, callback, carrierXmlParser, ssAction);
6261 final String ussdCommand = newUssdCommand;
6262 Executors.newSingleThreadExecutor().execute(() -> {
6263 handleUssdRequest(subId, ussdCommand, wrappedCallback);
6264 });
6265 } else {
6266 Pair<Boolean, Consumer<Integer>> arguments = Pair.create(enable,
6267 FunctionalUtils.ignoreRemoteException(callback::accept));
6268
6269 sendRequestAsync(CMD_SET_CALL_WAITING, arguments, phone, null);
6270 }
Shuo Qian4a594052020-01-23 11:59:30 -08006271 } finally {
6272 Binder.restoreCallingIdentity(identity);
6273 }
6274 }
6275
6276 /**
yinxub1bed742017-04-17 11:45:04 -07006277 * Starts a new network scan and returns the id of this scan.
yinxu504e1392017-04-12 16:03:22 -07006278 *
yinxub1bed742017-04-17 11:45:04 -07006279 * @param subId id of the subscription
Sooraj Sasindran2250dc02021-11-10 16:42:01 -08006280 * @param renounceFineLocationAccess Set this to true if the caller would not like to receive
6281 * location related information which will be sent if the caller already possess
6282 * {@android.Manifest.permission.ACCESS_FINE_LOCATION} and do not renounce the permission
yinxub1bed742017-04-17 11:45:04 -07006283 * @param request contains the radio access networks with bands/channels to scan
6284 * @param messenger callback messenger for scan results or errors
6285 * @param binder for the purpose of auto clean when the user thread crashes
yinxu504e1392017-04-12 16:03:22 -07006286 * @return the id of the requested scan which can be used to stop the scan.
6287 */
6288 @Override
Sooraj Sasindran2250dc02021-11-10 16:42:01 -08006289 public int requestNetworkScan(int subId, boolean renounceFineLocationAccess,
6290 NetworkScanRequest request, Messenger messenger,
Philip P. Moltmann3a2772a2019-10-04 08:15:00 -07006291 IBinder binder, String callingPackage, String callingFeatureId) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08006292 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
6293 mApp, subId, "requestNetworkScan");
Hall Liuf19c44f2018-11-27 14:38:17 -08006294 LocationAccessPolicy.LocationPermissionResult locationResult =
Sooraj Sasindran2250dc02021-11-10 16:42:01 -08006295 LocationAccessPolicy.LocationPermissionResult.DENIED_HARD;
6296 if (!renounceFineLocationAccess) {
6297 locationResult = LocationAccessPolicy.checkLocationPermission(mApp,
6298 new LocationAccessPolicy.LocationPermissionQuery.Builder()
6299 .setCallingPackage(callingPackage)
6300 .setCallingFeatureId(callingFeatureId)
6301 .setCallingPid(Binder.getCallingPid())
6302 .setCallingUid(Binder.getCallingUid())
6303 .setMethod("requestNetworkScan")
6304 .setMinSdkVersionForFine(Build.VERSION_CODES.Q)
6305 .setMinSdkVersionForCoarse(Build.VERSION_CODES.Q)
6306 .setMinSdkVersionForEnforcement(Build.VERSION_CODES.Q)
6307 .build());
6308 }
Hall Liub2ac8ef2019-02-28 15:56:23 -08006309 if (locationResult != LocationAccessPolicy.LocationPermissionResult.ALLOWED) {
Nathan Harold1c11dba2020-09-22 17:54:53 -07006310 SecurityException e = checkNetworkRequestForSanitizedLocationAccess(
6311 request, subId, callingPackage);
Hall Liub2ac8ef2019-02-28 15:56:23 -08006312 if (e != null) {
6313 if (locationResult == LocationAccessPolicy.LocationPermissionResult.DENIED_HARD) {
6314 throw e;
6315 } else {
Hall Liu0e5abaf2019-04-04 01:25:30 -07006316 loge(e.getMessage());
Hall Liub2ac8ef2019-02-28 15:56:23 -08006317 return TelephonyScanManager.INVALID_SCAN_ID;
6318 }
6319 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006320 }
Hall Liu912dfd32019-04-25 14:02:26 -07006321 int callingUid = Binder.getCallingUid();
6322 int callingPid = Binder.getCallingPid();
Ying Xu94a46582019-04-18 17:14:56 -07006323 final long identity = Binder.clearCallingIdentity();
6324 try {
6325 return mNetworkScanRequestTracker.startNetworkScan(
Sooraj Sasindran2250dc02021-11-10 16:42:01 -08006326 renounceFineLocationAccess, request, messenger, binder, getPhone(subId),
Hall Liu912dfd32019-04-25 14:02:26 -07006327 callingUid, callingPid, callingPackage);
Ying Xu94a46582019-04-18 17:14:56 -07006328 } finally {
6329 Binder.restoreCallingIdentity(identity);
6330 }
yinxu504e1392017-04-12 16:03:22 -07006331 }
6332
Hall Liub2ac8ef2019-02-28 15:56:23 -08006333 private SecurityException checkNetworkRequestForSanitizedLocationAccess(
Nathan Harold1c11dba2020-09-22 17:54:53 -07006334 NetworkScanRequest request, int subId, String callingPackage) {
Rambo Wangaccdabf2022-10-20 16:52:29 +00006335 boolean hasCarrierPriv;
6336 final long identity = Binder.clearCallingIdentity();
6337 try {
6338 hasCarrierPriv = checkCarrierPrivilegesForPackage(subId, callingPackage)
6339 == TelephonyManager.CARRIER_PRIVILEGE_STATUS_HAS_ACCESS;
6340 } finally {
6341 Binder.restoreCallingIdentity(identity);
6342 }
Hall Liu558027f2019-05-15 19:14:05 -07006343 boolean hasNetworkScanPermission =
6344 mApp.checkCallingOrSelfPermission(android.Manifest.permission.NETWORK_SCAN)
6345 == PERMISSION_GRANTED;
6346
6347 if (!hasCarrierPriv && !hasNetworkScanPermission) {
6348 return new SecurityException("permission.NETWORK_SCAN or carrier privileges is needed"
6349 + " for network scans without location access.");
Hall Liub2ac8ef2019-02-28 15:56:23 -08006350 }
6351
6352 if (request.getSpecifiers() != null && request.getSpecifiers().length > 0) {
6353 for (RadioAccessSpecifier ras : request.getSpecifiers()) {
Hall Liub2ac8ef2019-02-28 15:56:23 -08006354 if (ras.getChannels() != null && ras.getChannels().length > 0) {
6355 return new SecurityException("Specific channels must not be"
6356 + " scanned without location access.");
6357 }
6358 }
6359 }
6360
Hall Liub2ac8ef2019-02-28 15:56:23 -08006361 return null;
6362 }
6363
yinxu504e1392017-04-12 16:03:22 -07006364 /**
6365 * Stops an existing network scan with the given scanId.
yinxub1bed742017-04-17 11:45:04 -07006366 *
6367 * @param subId id of the subscription
6368 * @param scanId id of the scan that needs to be stopped
yinxu504e1392017-04-12 16:03:22 -07006369 */
6370 @Override
6371 public void stopNetworkScan(int subId, int scanId) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08006372 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
6373 mApp, subId, "stopNetworkScan");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006374
Hall Liu912dfd32019-04-25 14:02:26 -07006375 int callingUid = Binder.getCallingUid();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006376 final long identity = Binder.clearCallingIdentity();
6377 try {
Hall Liu912dfd32019-04-25 14:02:26 -07006378 mNetworkScanRequestTracker.stopNetworkScan(scanId, callingUid);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006379 } finally {
6380 Binder.restoreCallingIdentity(identity);
6381 }
yinxu504e1392017-04-12 16:03:22 -07006382 }
6383
6384 /**
SongFerngWang3ef3e072020-12-21 16:41:52 +08006385 * Get the allowed network types bitmask.
Junda Liu84d15a22014-07-02 11:21:04 -07006386 *
SongFerngWang3ef3e072020-12-21 16:41:52 +08006387 * @return the allowed network types bitmask, defined in RILConstants.java.
Junda Liu84d15a22014-07-02 11:21:04 -07006388 */
6389 @Override
SongFerngWang3ef3e072020-12-21 16:41:52 +08006390 public int getAllowedNetworkTypesBitmask(int subId) {
Pengquan Menga4009cb2018-12-20 11:00:24 -08006391 TelephonyPermissions
Nathan Harold62c68512021-04-06 11:26:02 -07006392 .enforceCallingOrSelfReadPrivilegedPhoneStatePermissionOrCarrierPrivilege(
SongFerngWang3ef3e072020-12-21 16:41:52 +08006393 mApp, subId, "getAllowedNetworkTypesBitmask");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006394
6395 final long identity = Binder.clearCallingIdentity();
6396 try {
SongFerngWang3ef3e072020-12-21 16:41:52 +08006397 if (DBG) log("getAllowedNetworkTypesBitmask");
6398 int[] result = (int[]) sendRequest(CMD_GET_ALLOWED_NETWORK_TYPES_BITMASK, null, subId);
6399 int networkTypesBitmask = (result != null ? result[0] : -1);
6400 if (DBG) log("getAllowedNetworkTypesBitmask: " + networkTypesBitmask);
6401 return networkTypesBitmask;
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006402 } finally {
6403 Binder.restoreCallingIdentity(identity);
6404 }
Jake Hamby7c27be32014-03-03 13:25:59 -08006405 }
6406
6407 /**
Sooraj Sasindranc46dfbd2020-06-03 01:06:00 -07006408 * Get the allowed network types for certain reason.
6409 *
6410 * @param subId the id of the subscription.
6411 * @param reason the reason the allowed network type change is taking place
6412 * @return the allowed network types.
6413 */
6414 @Override
6415 public long getAllowedNetworkTypesForReason(int subId,
6416 @TelephonyManager.AllowedNetworkTypesReason int reason) {
Nathan Harold62c68512021-04-06 11:26:02 -07006417 TelephonyPermissions.enforceCallingOrSelfReadPrecisePhoneStatePermissionOrCarrierPrivilege(
SongFerngWang8c6e82e2021-03-02 22:09:29 +08006418 mApp, subId, "getAllowedNetworkTypesForReason");
Sooraj Sasindranc46dfbd2020-06-03 01:06:00 -07006419 final long identity = Binder.clearCallingIdentity();
6420 try {
6421 return getPhoneFromSubId(subId).getAllowedNetworkTypes(reason);
6422 } finally {
6423 Binder.restoreCallingIdentity(identity);
6424 }
6425 }
6426
6427 /**
Sooraj Sasindran37444802020-08-11 10:40:43 -07006428 * Enable/Disable E-UTRA-NR Dual Connectivity
6429 * @param subId subscription id of the sim card
6430 * @param nrDualConnectivityState expected NR dual connectivity state
6431 * This can be passed following states
6432 * <ol>
6433 * <li>Enable NR dual connectivity {@link TelephonyManager#NR_DUAL_CONNECTIVITY_ENABLE}
6434 * <li>Disable NR dual connectivity {@link TelephonyManager#NR_DUAL_CONNECTIVITY_DISABLE}
6435 * <li>Disable NR dual connectivity and force secondary cell to be released
6436 * {@link TelephonyManager#NR_DUAL_CONNECTIVITY_DISABLE_IMMEDIATE}
6437 * </ol>
6438 * @return operation result.
6439 */
6440 @Override
6441 public int setNrDualConnectivityState(int subId,
6442 @TelephonyManager.NrDualConnectivityState int nrDualConnectivityState) {
6443 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
6444 mApp, subId, "enableNRDualConnectivity");
Sooraj Sasindran0e4e00a2021-03-16 18:02:32 -07006445 if (!isRadioInterfaceCapabilitySupported(
Sooraj Sasindrandfd595b2021-03-11 17:38:13 -08006446 TelephonyManager.CAPABILITY_NR_DUAL_CONNECTIVITY_CONFIGURATION_AVAILABLE)) {
6447 return TelephonyManager.ENABLE_NR_DUAL_CONNECTIVITY_NOT_SUPPORTED;
6448 }
6449
Sooraj Sasindran37444802020-08-11 10:40:43 -07006450 WorkSource workSource = getWorkSource(Binder.getCallingUid());
6451 final long identity = Binder.clearCallingIdentity();
6452 try {
6453 int result = (int) sendRequest(CMD_ENABLE_NR_DUAL_CONNECTIVITY,
6454 nrDualConnectivityState, subId,
6455 workSource);
6456 if (DBG) log("enableNRDualConnectivity result: " + result);
6457 return result;
6458 } finally {
6459 Binder.restoreCallingIdentity(identity);
6460 }
6461 }
6462
6463 /**
6464 * Is E-UTRA-NR Dual Connectivity enabled
6465 * @return true if dual connectivity is enabled else false
6466 */
6467 @Override
6468 public boolean isNrDualConnectivityEnabled(int subId) {
6469 TelephonyPermissions
Nathan Harold62c68512021-04-06 11:26:02 -07006470 .enforceCallingOrSelfReadPrivilegedPhoneStatePermissionOrCarrierPrivilege(
Sooraj Sasindran37444802020-08-11 10:40:43 -07006471 mApp, subId, "isNRDualConnectivityEnabled");
Sooraj Sasindran0e4e00a2021-03-16 18:02:32 -07006472 if (!isRadioInterfaceCapabilitySupported(
Sooraj Sasindrandfd595b2021-03-11 17:38:13 -08006473 TelephonyManager.CAPABILITY_NR_DUAL_CONNECTIVITY_CONFIGURATION_AVAILABLE)) {
6474 return false;
6475 }
Sooraj Sasindran37444802020-08-11 10:40:43 -07006476 WorkSource workSource = getWorkSource(Binder.getCallingUid());
6477 final long identity = Binder.clearCallingIdentity();
6478 try {
6479 boolean isEnabled = (boolean) sendRequest(CMD_IS_NR_DUAL_CONNECTIVITY_ENABLED,
6480 null, subId, workSource);
6481 if (DBG) log("isNRDualConnectivityEnabled: " + isEnabled);
6482 return isEnabled;
6483 } finally {
6484 Binder.restoreCallingIdentity(identity);
6485 }
6486 }
6487
6488 /**
Sooraj Sasindranc46dfbd2020-06-03 01:06:00 -07006489 * Set the allowed network types of the device and
6490 * provide the reason triggering the allowed network change.
6491 *
6492 * @param subId the id of the subscription.
6493 * @param reason the reason the allowed network type change is taking place
6494 * @param allowedNetworkTypes the allowed network types.
6495 * @return true on success; false on any failure.
6496 */
6497 @Override
6498 public boolean setAllowedNetworkTypesForReason(int subId,
SongFerngWang3ef3e072020-12-21 16:41:52 +08006499 @TelephonyManager.AllowedNetworkTypesReason int reason,
6500 @TelephonyManager.NetworkTypeBitMask long allowedNetworkTypes) {
Sooraj Sasindranc46dfbd2020-06-03 01:06:00 -07006501 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
6502 mApp, subId, "setAllowedNetworkTypesForReason");
Gil Cukierman1d3d3752022-10-03 21:31:33 +00006503 // If the caller only has carrier privileges, then they should not be able to override
6504 // any network types which were set for security reasons.
6505 if (mApp.checkCallingOrSelfPermission(Manifest.permission.MODIFY_PHONE_STATE)
6506 != PERMISSION_GRANTED
6507 && (reason == TelephonyManager.ALLOWED_NETWORK_TYPES_REASON_ENABLE_2G
6508 || reason == TelephonyManager.ALLOWED_NETWORK_TYPES_REASON_USER_RESTRICTIONS)) {
6509 throw new SecurityException(
6510 "setAllowedNetworkTypesForReason cannot be called with carrier privileges for"
6511 + " reason "
6512 + reason);
6513 }
SongFerngWang3ef3e072020-12-21 16:41:52 +08006514 if (!TelephonyManager.isValidAllowedNetworkTypesReason(reason)) {
SongFerngWang7ffc2732021-04-15 19:46:33 +08006515 loge("setAllowedNetworkTypesForReason: Invalid allowed network type reason: " + reason);
6516 return false;
6517 }
6518 if (!SubscriptionManager.isUsableSubscriptionId(subId)) {
6519 loge("setAllowedNetworkTypesForReason: Invalid subscriptionId:" + subId);
SongFerngWang3ef3e072020-12-21 16:41:52 +08006520 return false;
6521 }
6522
SongFerngWang8c6e82e2021-03-02 22:09:29 +08006523 log("setAllowedNetworkTypesForReason: " + reason + " value: "
6524 + TelephonyManager.convertNetworkTypeBitmaskToString(allowedNetworkTypes));
6525
6526
6527 if (allowedNetworkTypes == getPhoneFromSubId(subId).getAllowedNetworkTypes(reason)) {
6528 log("setAllowedNetworkTypesForReason: " + reason + "does not change value");
6529 return true;
SongFerngWang3ef3e072020-12-21 16:41:52 +08006530 }
SongFerngWang8c6e82e2021-03-02 22:09:29 +08006531
Sooraj Sasindranc46dfbd2020-06-03 01:06:00 -07006532 final long identity = Binder.clearCallingIdentity();
6533 try {
SongFerngWang3ef3e072020-12-21 16:41:52 +08006534 Boolean success = (Boolean) sendRequest(
6535 CMD_SET_ALLOWED_NETWORK_TYPES_FOR_REASON,
6536 new Pair<Integer, Long>(reason, allowedNetworkTypes), subId);
6537
6538 if (DBG) log("setAllowedNetworkTypesForReason: " + (success ? "ok" : "fail"));
6539 return success;
Sooraj Sasindranc46dfbd2020-06-03 01:06:00 -07006540 } finally {
6541 Binder.restoreCallingIdentity(identity);
6542 }
6543 }
6544
6545 /**
Miaoa84611c2019-03-15 09:21:10 +08006546 * Check whether DUN APN is required for tethering with subId.
Junda Liu475951f2014-11-07 16:45:03 -08006547 *
Miaoa84611c2019-03-15 09:21:10 +08006548 * @param subId the id of the subscription to require tethering.
Amit Mahajanfe58cdf2017-07-11 12:01:53 -07006549 * @return {@code true} if DUN APN is required for tethering.
Junda Liu475951f2014-11-07 16:45:03 -08006550 * @hide
6551 */
6552 @Override
SongFerngWangf08d8122019-11-15 14:58:44 +08006553 public boolean isTetheringApnRequiredForSubscriber(int subId) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08006554 enforceModifyPermission();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006555 final long identity = Binder.clearCallingIdentity();
Miaoa84611c2019-03-15 09:21:10 +08006556 final Phone phone = getPhone(subId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006557 try {
Miaoa84611c2019-03-15 09:21:10 +08006558 if (phone != null) {
6559 return phone.hasMatchedTetherApnSetting();
6560 } else {
6561 return false;
6562 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006563 } finally {
6564 Binder.restoreCallingIdentity(identity);
Junda Liu475951f2014-11-07 16:45:03 -08006565 }
Junda Liu475951f2014-11-07 16:45:03 -08006566 }
6567
6568 /**
Malcolm Chen964682d2017-11-28 16:20:07 -08006569 * Get the user enabled state of Mobile Data.
6570 *
6571 * TODO: remove and use isUserDataEnabled.
6572 * This can't be removed now because some vendor codes
6573 * calls through ITelephony directly while they should
6574 * use TelephonyManager.
6575 *
6576 * @return true on enabled
6577 */
6578 @Override
6579 public boolean getDataEnabled(int subId) {
6580 return isUserDataEnabled(subId);
6581 }
6582
6583 /**
6584 * Get whether mobile data is enabled per user setting.
6585 *
6586 * There are other factors deciding whether mobile data is actually enabled, but they are
6587 * not considered here. See {@link #isDataEnabled(int)} for more details.
Robert Greenwalt646120a2014-05-23 11:54:03 -07006588 *
Sooraj Sasindran5d051bf2021-11-05 13:14:15 -07006589 * Accepts either READ_BASIC_PHONE_STATE, ACCESS_NETWORK_STATE, MODIFY_PHONE_STATE
6590 * or carrier privileges.
Robert Greenwalted86e582014-05-21 20:03:20 -07006591 *
6592 * @return {@code true} if data is enabled else {@code false}
6593 */
6594 @Override
Malcolm Chen964682d2017-11-28 16:20:07 -08006595 public boolean isUserDataEnabled(int subId) {
Sooraj Sasindran5d051bf2021-11-05 13:14:15 -07006596 String functionName = "isUserDataEnabled";
Robert Greenwalt646120a2014-05-23 11:54:03 -07006597 try {
Sooraj Sasindran5d051bf2021-11-05 13:14:15 -07006598 try {
6599 mApp.enforceCallingOrSelfPermission(permission.READ_BASIC_PHONE_STATE,
6600 functionName);
6601 } catch (Exception e) {
6602 mApp.enforceCallingOrSelfPermission(permission.ACCESS_NETWORK_STATE, functionName);
6603 }
Robert Greenwalt646120a2014-05-23 11:54:03 -07006604 } catch (Exception e) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08006605 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
Sooraj Sasindran5d051bf2021-11-05 13:14:15 -07006606 mApp, subId, functionName);
6607
Robert Greenwalt646120a2014-05-23 11:54:03 -07006608 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006609
6610 final long identity = Binder.clearCallingIdentity();
6611 try {
Jack Yudf049d72022-12-02 22:48:35 -08006612 int phoneId = SubscriptionManager.getPhoneId(subId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006613 if (DBG) log("isUserDataEnabled: subId=" + subId + " phoneId=" + phoneId);
6614 Phone phone = PhoneFactory.getPhone(phoneId);
6615 if (phone != null) {
6616 boolean retVal = phone.isUserDataEnabled();
6617 if (DBG) log("isUserDataEnabled: subId=" + subId + " retVal=" + retVal);
6618 return retVal;
6619 } else {
6620 if (DBG) loge("isUserDataEnabled: no phone subId=" + subId + " retVal=false");
6621 return false;
6622 }
6623 } finally {
6624 Binder.restoreCallingIdentity(identity);
Malcolm Chen964682d2017-11-28 16:20:07 -08006625 }
6626 }
6627
6628 /**
Shuo Qian8ee4e882020-01-08 14:30:06 -08006629 * Checks if the device is capable of mobile data by considering whether whether the
6630 * user has enabled mobile data, whether the carrier has enabled mobile data, and
6631 * whether the network policy allows data connections.
Malcolm Chen964682d2017-11-28 16:20:07 -08006632 *
Shuo Qian8ee4e882020-01-08 14:30:06 -08006633 * @return {@code true} if the overall data connection is capable; {@code false} if not.
Malcolm Chen964682d2017-11-28 16:20:07 -08006634 */
6635 @Override
6636 public boolean isDataEnabled(int subId) {
Sooraj Sasindran5d051bf2021-11-05 13:14:15 -07006637 String functionName = "isDataEnabled";
Sooraj Sasindran95c07a92020-07-15 01:35:24 -07006638 try {
6639 try {
6640 mApp.enforceCallingOrSelfPermission(
6641 android.Manifest.permission.ACCESS_NETWORK_STATE,
Sooraj Sasindran5d051bf2021-11-05 13:14:15 -07006642 functionName);
Sooraj Sasindran95c07a92020-07-15 01:35:24 -07006643 } catch (Exception e) {
Sooraj Sasindran5d051bf2021-11-05 13:14:15 -07006644 try {
6645 mApp.enforceCallingOrSelfPermission(
6646 android.Manifest.permission.READ_PHONE_STATE,
6647 functionName);
6648 } catch (Exception e2) {
6649 mApp.enforceCallingOrSelfPermission(
6650 permission.READ_BASIC_PHONE_STATE, functionName);
6651 }
Sooraj Sasindran95c07a92020-07-15 01:35:24 -07006652 }
6653 } catch (Exception e) {
Sooraj Sasindran5d051bf2021-11-05 13:14:15 -07006654 enforceReadPrivilegedPermission(functionName);
Sooraj Sasindran95c07a92020-07-15 01:35:24 -07006655 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006656
6657 final long identity = Binder.clearCallingIdentity();
6658 try {
Jack Yudf049d72022-12-02 22:48:35 -08006659 int phoneId = SubscriptionManager.getPhoneId(subId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006660 Phone phone = PhoneFactory.getPhone(phoneId);
6661 if (phone != null) {
Jack Yu4ad64e52021-12-03 14:23:53 -08006662 boolean retVal;
Jack Yuabb10902022-07-31 00:43:21 -07006663 retVal = phone.getDataSettingsManager().isDataEnabled();
Jack Yu4ad64e52021-12-03 14:23:53 -08006664 if (DBG) log("isDataEnabled: " + retVal + ", subId=" + subId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006665 return retVal;
6666 } else {
6667 if (DBG) loge("isDataEnabled: no phone subId=" + subId + " retVal=false");
6668 return false;
6669 }
6670 } finally {
6671 Binder.restoreCallingIdentity(identity);
Wink Savillee7353bb2014-12-05 14:21:41 -08006672 }
Robert Greenwalted86e582014-05-21 20:03:20 -07006673 }
Shishir Agrawal60f9c952014-06-23 12:00:43 -07006674
Sooraj Sasindranaf1b5132020-05-05 21:06:20 +00006675 /**
6676 * Check if data is enabled for a specific reason
6677 * @param subId Subscription index
6678 * @param reason the reason the data enable change is taking place
6679 * @return {@code true} if the overall data is enabled; {@code false} if not.
6680 */
6681 @Override
Sooraj Sasindran95c07a92020-07-15 01:35:24 -07006682 public boolean isDataEnabledForReason(int subId,
Sooraj Sasindranaf1b5132020-05-05 21:06:20 +00006683 @TelephonyManager.DataEnabledReason int reason) {
Sooraj Sasindran5d051bf2021-11-05 13:14:15 -07006684 String functionName = "isDataEnabledForReason";
Sooraj Sasindranaf1b5132020-05-05 21:06:20 +00006685 try {
Sooraj Sasindran5d051bf2021-11-05 13:14:15 -07006686 try {
6687 mApp.enforceCallingOrSelfPermission(
6688 android.Manifest.permission.ACCESS_NETWORK_STATE,
6689 functionName);
6690 } catch (Exception e) {
6691 mApp.enforceCallingOrSelfPermission(permission.READ_BASIC_PHONE_STATE,
6692 functionName);
6693 }
Sooraj Sasindranaf1b5132020-05-05 21:06:20 +00006694 } catch (Exception e) {
Sooraj Sasindran0d909a02021-11-08 12:01:16 -08006695 try {
6696 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.READ_PHONE_STATE,
Sooraj Sasindran5d051bf2021-11-05 13:14:15 -07006697 functionName);
Sooraj Sasindran0d909a02021-11-08 12:01:16 -08006698 } catch (Exception e2) {
6699 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
Sooraj Sasindran5d051bf2021-11-05 13:14:15 -07006700 mApp, subId, functionName);
Sooraj Sasindran0d909a02021-11-08 12:01:16 -08006701 }
Sooraj Sasindranaf1b5132020-05-05 21:06:20 +00006702 }
6703
6704
6705 final long identity = Binder.clearCallingIdentity();
6706 try {
Jack Yudf049d72022-12-02 22:48:35 -08006707 int phoneId = SubscriptionManager.getPhoneId(subId);
Sooraj Sasindranaf1b5132020-05-05 21:06:20 +00006708 if (DBG) {
Sooraj Sasindran95c07a92020-07-15 01:35:24 -07006709 log("isDataEnabledForReason: subId=" + subId + " phoneId=" + phoneId
Sooraj Sasindranaf1b5132020-05-05 21:06:20 +00006710 + " reason=" + reason);
6711 }
6712 Phone phone = PhoneFactory.getPhone(phoneId);
6713 if (phone != null) {
6714 boolean retVal;
Jack Yuabb10902022-07-31 00:43:21 -07006715 retVal = phone.getDataSettingsManager().isDataEnabledForReason(reason);
Sooraj Sasindran95c07a92020-07-15 01:35:24 -07006716 if (DBG) log("isDataEnabledForReason: retVal=" + retVal);
Sooraj Sasindranaf1b5132020-05-05 21:06:20 +00006717 return retVal;
6718 } else {
6719 if (DBG) {
Sooraj Sasindran95c07a92020-07-15 01:35:24 -07006720 loge("isDataEnabledForReason: no phone subId="
Sooraj Sasindranaf1b5132020-05-05 21:06:20 +00006721 + subId + " retVal=false");
6722 }
6723 return false;
6724 }
6725 } finally {
6726 Binder.restoreCallingIdentity(identity);
6727 }
6728 }
6729
Shishir Agrawal60f9c952014-06-23 12:00:43 -07006730 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08006731 public int getCarrierPrivilegeStatus(int subId) {
Hunter Knepshieldcad7f0b2021-12-13 15:08:35 -08006732 // No permission needed; this only lets the caller inspect their own status.
6733 return getCarrierPrivilegeStatusForUidWithPermission(subId, Binder.getCallingUid());
Shishir Agrawal60f9c952014-06-23 12:00:43 -07006734 }
Junda Liu29340342014-07-10 15:23:27 -07006735
6736 @Override
Jeff Davidson7e17e312018-02-13 18:17:36 -08006737 public int getCarrierPrivilegeStatusForUid(int subId, int uid) {
Shuo Qian2c0ae432019-12-05 11:40:37 -08006738 enforceReadPrivilegedPermission("getCarrierPrivilegeStatusForUid");
Hunter Knepshieldcad7f0b2021-12-13 15:08:35 -08006739 return getCarrierPrivilegeStatusForUidWithPermission(subId, uid);
6740 }
6741
6742 private int getCarrierPrivilegeStatusForUidWithPermission(int subId, int uid) {
6743 Phone phone = getPhone(subId);
Jeff Davidson7e17e312018-02-13 18:17:36 -08006744 if (phone == null) {
Taesu Leef8fbed92019-10-07 18:47:02 +09006745 loge("getCarrierPrivilegeStatusForUid: Invalid subId");
Jeff Davidson7e17e312018-02-13 18:17:36 -08006746 return TelephonyManager.CARRIER_PRIVILEGE_STATUS_NO_ACCESS;
6747 }
Hunter Knepshieldcad7f0b2021-12-13 15:08:35 -08006748 CarrierPrivilegesTracker cpt = phone.getCarrierPrivilegesTracker();
6749 if (cpt == null) {
6750 loge("getCarrierPrivilegeStatusForUid: No CarrierPrivilegesTracker");
Jeff Davidson7e17e312018-02-13 18:17:36 -08006751 return TelephonyManager.CARRIER_PRIVILEGE_STATUS_RULES_NOT_LOADED;
6752 }
Hunter Knepshieldcad7f0b2021-12-13 15:08:35 -08006753 return cpt.getCarrierPrivilegeStatusForUid(uid);
Jeff Davidson7e17e312018-02-13 18:17:36 -08006754 }
6755
6756 @Override
chen xuf7e9fe82019-05-09 19:31:02 -07006757 public int checkCarrierPrivilegesForPackage(int subId, String pkgName) {
Nazanin1adf4562021-03-29 15:35:30 -07006758 enforceReadPrivilegedPermission("checkCarrierPrivilegesForPackage");
chen xuf7e9fe82019-05-09 19:31:02 -07006759 if (TextUtils.isEmpty(pkgName)) {
Junda Liu317d70b2016-03-08 09:33:53 -08006760 return TelephonyManager.CARRIER_PRIVILEGE_STATUS_NO_ACCESS;
chen xuf7e9fe82019-05-09 19:31:02 -07006761 }
Hunter Knepshieldcad7f0b2021-12-13 15:08:35 -08006762 Phone phone = getPhone(subId);
6763 if (phone == null) {
6764 loge("checkCarrierPrivilegesForPackage: Invalid subId");
6765 return TelephonyManager.CARRIER_PRIVILEGE_STATUS_NO_ACCESS;
6766 }
6767 CarrierPrivilegesTracker cpt = phone.getCarrierPrivilegesTracker();
6768 if (cpt == null) {
6769 loge("checkCarrierPrivilegesForPackage: No CarrierPrivilegesTracker");
Shishir Agrawaleb8771e2014-07-22 11:24:08 -07006770 return TelephonyManager.CARRIER_PRIVILEGE_STATUS_RULES_NOT_LOADED;
6771 }
Hunter Knepshieldcad7f0b2021-12-13 15:08:35 -08006772 return cpt.getCarrierPrivilegeStatusForPackage(pkgName);
Zach Johnson50ecba32015-05-19 00:24:21 -07006773 }
6774
6775 @Override
6776 public int checkCarrierPrivilegesForPackageAnyPhone(String pkgName) {
Hunter Knepshieldcad7f0b2021-12-13 15:08:35 -08006777 enforceReadPrivilegedPermission("checkCarrierPrivilegesForPackageAnyPhone");
Rambo Wange7209ce2022-02-23 13:41:02 -08006778 return checkCarrierPrivilegesForPackageAnyPhoneWithPermission(pkgName);
6779 }
6780
6781 private int checkCarrierPrivilegesForPackageAnyPhoneWithPermission(String pkgName) {
Hunter Knepshieldcad7f0b2021-12-13 15:08:35 -08006782 if (TextUtils.isEmpty(pkgName)) {
Junda Liu317d70b2016-03-08 09:33:53 -08006783 return TelephonyManager.CARRIER_PRIVILEGE_STATUS_NO_ACCESS;
Hunter Knepshieldcad7f0b2021-12-13 15:08:35 -08006784 }
Zach Johnson50ecba32015-05-19 00:24:21 -07006785 int result = TelephonyManager.CARRIER_PRIVILEGE_STATUS_RULES_NOT_LOADED;
Hunter Knepshieldcad7f0b2021-12-13 15:08:35 -08006786 for (int phoneId = 0; phoneId < TelephonyManager.getDefault().getPhoneCount(); phoneId++) {
6787 Phone phone = PhoneFactory.getPhone(phoneId);
6788 if (phone == null) {
6789 continue;
Zach Johnson50ecba32015-05-19 00:24:21 -07006790 }
Hunter Knepshieldcad7f0b2021-12-13 15:08:35 -08006791 CarrierPrivilegesTracker cpt = phone.getCarrierPrivilegesTracker();
6792 if (cpt == null) {
6793 continue;
6794 }
6795 result = cpt.getCarrierPrivilegeStatusForPackage(pkgName);
Zach Johnson50ecba32015-05-19 00:24:21 -07006796 if (result == TelephonyManager.CARRIER_PRIVILEGE_STATUS_HAS_ACCESS) {
6797 break;
6798 }
6799 }
Zach Johnson50ecba32015-05-19 00:24:21 -07006800 return result;
Junda Liu29340342014-07-10 15:23:27 -07006801 }
Derek Tan89e89d42014-07-08 17:00:10 -07006802
6803 @Override
Junda Liue64de782015-04-16 17:19:16 -07006804 public List<String> getCarrierPackageNamesForIntentAndPhone(Intent intent, int phoneId) {
Nazanin1adf4562021-03-29 15:35:30 -07006805 enforceReadPrivilegedPermission("getCarrierPackageNamesForIntentAndPhone");
Rambo Wang8a247eb2022-02-08 21:11:18 +00006806 Phone phone = PhoneFactory.getPhone(phoneId);
6807 if (phone == null) {
6808 return Collections.emptyList();
Junda Liue64de782015-04-16 17:19:16 -07006809 }
Rambo Wang8a247eb2022-02-08 21:11:18 +00006810 CarrierPrivilegesTracker cpt = phone.getCarrierPrivilegesTracker();
6811 if (cpt == null) {
6812 return Collections.emptyList();
Shishir Agrawaleb6439a2014-07-21 13:19:38 -07006813 }
Rambo Wang8a247eb2022-02-08 21:11:18 +00006814 return cpt.getCarrierPackageNamesForIntent(intent);
Shishir Agrawaleb6439a2014-07-21 13:19:38 -07006815 }
6816
Amith Yamasani6e118872016-02-19 12:53:51 -08006817 @Override
chen xuf7e9fe82019-05-09 19:31:02 -07006818 public List<String> getPackagesWithCarrierPrivileges(int phoneId) {
Nazanin1adf4562021-03-29 15:35:30 -07006819 enforceReadPrivilegedPermission("getPackagesWithCarrierPrivileges");
Hunter Knepshieldcad7f0b2021-12-13 15:08:35 -08006820 Phone phone = PhoneFactory.getPhone(phoneId);
6821 if (phone == null) {
6822 return Collections.emptyList();
Amith Yamasani6e118872016-02-19 12:53:51 -08006823 }
Hunter Knepshieldcad7f0b2021-12-13 15:08:35 -08006824 CarrierPrivilegesTracker cpt = phone.getCarrierPrivilegesTracker();
6825 if (cpt == null) {
6826 return Collections.emptyList();
6827 }
6828 return new ArrayList<>(cpt.getPackagesWithCarrierPrivileges());
Amith Yamasani6e118872016-02-19 12:53:51 -08006829 }
6830
chen xuf7e9fe82019-05-09 19:31:02 -07006831 @Override
6832 public List<String> getPackagesWithCarrierPrivilegesForAllPhones() {
Shuo Qian067a06d2019-12-03 23:40:18 +00006833 enforceReadPrivilegedPermission("getPackagesWithCarrierPrivilegesForAllPhones");
Hunter Knepshieldcad7f0b2021-12-13 15:08:35 -08006834 Set<String> privilegedPackages = new ArraySet<>();
Shuo Qian067a06d2019-12-03 23:40:18 +00006835 final long identity = Binder.clearCallingIdentity();
Shuo Qian067a06d2019-12-03 23:40:18 +00006836 try {
6837 for (int i = 0; i < TelephonyManager.getDefault().getPhoneCount(); i++) {
6838 privilegedPackages.addAll(getPackagesWithCarrierPrivileges(i));
6839 }
6840 } finally {
6841 Binder.restoreCallingIdentity(identity);
chen xuf7e9fe82019-05-09 19:31:02 -07006842 }
Hunter Knepshieldcad7f0b2021-12-13 15:08:35 -08006843 return new ArrayList<>(privilegedPackages);
chen xuf7e9fe82019-05-09 19:31:02 -07006844 }
6845
Rambo Wang6812ffb2022-03-15 16:54:17 -07006846 @Override
6847 public @Nullable String getCarrierServicePackageNameForLogicalSlot(int logicalSlotIndex) {
6848 enforceReadPrivilegedPermission("getCarrierServicePackageNameForLogicalSlot");
6849
6850 final Phone phone = PhoneFactory.getPhone(logicalSlotIndex);
6851 if (phone == null) {
6852 return null;
6853 }
6854 final CarrierPrivilegesTracker cpt = phone.getCarrierPrivilegesTracker();
6855 if (cpt == null) {
6856 return null;
6857 }
6858 return cpt.getCarrierServicePackageName();
6859 }
6860
Wink Savilleb564aae2014-10-23 10:18:09 -07006861 private String getIccId(int subId) {
Sanket Padawe356d7632015-06-22 14:03:32 -07006862 final Phone phone = getPhone(subId);
Muralidhar Reddy472c2ae2021-09-29 19:38:40 +00006863 UiccPort port = phone == null ? null : phone.getUiccPort();
6864 if (port == null) {
Derek Tan97ebb422014-09-05 16:55:38 -07006865 return null;
6866 }
Muralidhar Reddy472c2ae2021-09-29 19:38:40 +00006867 String iccId = port.getIccId();
Derek Tan97ebb422014-09-05 16:55:38 -07006868 if (TextUtils.isEmpty(iccId)) {
Derek Tan97ebb422014-09-05 16:55:38 -07006869 return null;
6870 }
6871 return iccId;
6872 }
6873
Shishir Agrawaleb6439a2014-07-21 13:19:38 -07006874 @Override
Shuo Qiane4e11672020-12-15 22:15:33 -08006875 public void setCallComposerStatus(int subId, int status) {
6876 enforceModifyPermission();
6877
6878 final long identity = Binder.clearCallingIdentity();
6879 try {
6880 Phone phone = getPhone(subId);
6881 if (phone != null) {
6882 Phone defaultPhone = phone.getImsPhone();
6883 if (defaultPhone != null && defaultPhone.getPhoneType() == PHONE_TYPE_IMS) {
6884 ImsPhone imsPhone = (ImsPhone) defaultPhone;
6885 imsPhone.setCallComposerStatus(status);
Shuo Qian284ae752020-12-22 19:10:14 -08006886 ImsManager.getInstance(mApp, getSlotIndexOrException(subId))
6887 .updateImsServiceConfig();
Shuo Qiane4e11672020-12-15 22:15:33 -08006888 }
6889 }
Shuo Qian284ae752020-12-22 19:10:14 -08006890 } catch (ImsException e) {
6891 throw new ServiceSpecificException(e.getCode());
6892 } finally {
Shuo Qiane4e11672020-12-15 22:15:33 -08006893 Binder.restoreCallingIdentity(identity);
6894 }
6895 }
6896
6897 @Override
6898 public int getCallComposerStatus(int subId) {
6899 enforceReadPrivilegedPermission("getCallComposerStatus");
6900
6901 final long identity = Binder.clearCallingIdentity();
6902 try {
6903 Phone phone = getPhone(subId);
6904 if (phone != null) {
6905 Phone defaultPhone = phone.getImsPhone();
6906 if (defaultPhone != null && defaultPhone.getPhoneType() == PHONE_TYPE_IMS) {
6907 ImsPhone imsPhone = (ImsPhone) defaultPhone;
6908 return imsPhone.getCallComposerStatus();
6909 }
6910 }
6911 } finally {
6912 Binder.restoreCallingIdentity(identity);
6913 }
6914 return TelephonyManager.CALL_COMPOSER_STATUS_OFF;
6915 }
6916
6917 @Override
Jeff Sharkey85190e62014-12-05 09:40:12 -08006918 public boolean setLine1NumberForDisplayForSubscriber(int subId, String alphaTag,
6919 String number) {
Shuo Qian2c0ae432019-12-05 11:40:37 -08006920 TelephonyPermissions.enforceCallingOrSelfCarrierPrivilege(mApp,
Jeff Davidson7e17e312018-02-13 18:17:36 -08006921 subId, "setLine1NumberForDisplayForSubscriber");
Derek Tan97ebb422014-09-05 16:55:38 -07006922
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006923 final long identity = Binder.clearCallingIdentity();
6924 try {
6925 final String iccId = getIccId(subId);
6926 final Phone phone = getPhone(subId);
6927 if (phone == null) {
6928 return false;
6929 }
6930 final String subscriberId = phone.getSubscriberId();
6931
6932 if (DBG_MERGE) {
Amit Mahajanb8f13202020-01-27 18:16:07 -08006933 Rlog.d(LOG_TAG, "Setting line number for ICC=" + iccId + ", subscriberId="
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006934 + subscriberId + " to " + number);
6935 }
6936
6937 if (TextUtils.isEmpty(iccId)) {
6938 return false;
6939 }
6940
6941 final SharedPreferences.Editor editor = mTelephonySharedPreferences.edit();
6942
6943 final String alphaTagPrefKey = PREF_CARRIERS_ALPHATAG_PREFIX + iccId;
6944 if (alphaTag == null) {
6945 editor.remove(alphaTagPrefKey);
6946 } else {
6947 editor.putString(alphaTagPrefKey, alphaTag);
6948 }
6949
6950 // Record both the line number and IMSI for this ICCID, since we need to
6951 // track all merged IMSIs based on line number
6952 final String numberPrefKey = PREF_CARRIERS_NUMBER_PREFIX + iccId;
6953 final String subscriberPrefKey = PREF_CARRIERS_SUBSCRIBER_PREFIX + iccId;
6954 if (number == null) {
6955 editor.remove(numberPrefKey);
6956 editor.remove(subscriberPrefKey);
6957 } else {
6958 editor.putString(numberPrefKey, number);
6959 editor.putString(subscriberPrefKey, subscriberId);
6960 }
6961
6962 editor.commit();
6963 return true;
6964 } finally {
6965 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07006966 }
Derek Tan7226c842014-07-02 17:42:23 -07006967 }
6968
6969 @Override
Philip P. Moltmann700a9592019-10-03 11:53:50 -07006970 public String getLine1NumberForDisplay(int subId, String callingPackage,
6971 String callingFeatureId) {
Makoto Onukifee69342015-06-29 14:44:50 -07006972 // This is open to apps with WRITE_SMS.
Jeff Davidson7e17e312018-02-13 18:17:36 -08006973 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneNumber(
Philip P. Moltmann700a9592019-10-03 11:53:50 -07006974 mApp, subId, callingPackage, callingFeatureId, "getLine1NumberForDisplay")) {
Amit Mahajan9cf11512015-11-09 11:40:48 -08006975 if (DBG_MERGE) log("getLine1NumberForDisplay returning null due to permission");
Svet Ganovb320e182015-04-16 12:30:10 -07006976 return null;
6977 }
Derek Tan97ebb422014-09-05 16:55:38 -07006978
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006979 final long identity = Binder.clearCallingIdentity();
6980 try {
6981 String iccId = getIccId(subId);
6982 if (iccId != null) {
6983 String numberPrefKey = PREF_CARRIERS_NUMBER_PREFIX + iccId;
6984 if (DBG_MERGE) {
6985 log("getLine1NumberForDisplay returning "
6986 + mTelephonySharedPreferences.getString(numberPrefKey, null));
6987 }
6988 return mTelephonySharedPreferences.getString(numberPrefKey, null);
Amit Mahajan9cf11512015-11-09 11:40:48 -08006989 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006990 if (DBG_MERGE) log("getLine1NumberForDisplay returning null as iccId is null");
6991 return null;
6992 } finally {
6993 Binder.restoreCallingIdentity(identity);
Derek Tan7226c842014-07-02 17:42:23 -07006994 }
Derek Tan7226c842014-07-02 17:42:23 -07006995 }
6996
6997 @Override
Philip P. Moltmann700a9592019-10-03 11:53:50 -07006998 public String getLine1AlphaTagForDisplay(int subId, String callingPackage,
6999 String callingFeatureId) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08007000 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
Philip P. Moltmann700a9592019-10-03 11:53:50 -07007001 mApp, subId, callingPackage, callingFeatureId, "getLine1AlphaTagForDisplay")) {
Svet Ganovb320e182015-04-16 12:30:10 -07007002 return null;
7003 }
Derek Tan97ebb422014-09-05 16:55:38 -07007004
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007005 final long identity = Binder.clearCallingIdentity();
7006 try {
7007 String iccId = getIccId(subId);
7008 if (iccId != null) {
7009 String alphaTagPrefKey = PREF_CARRIERS_ALPHATAG_PREFIX + iccId;
7010 return mTelephonySharedPreferences.getString(alphaTagPrefKey, null);
7011 }
7012 return null;
7013 } finally {
7014 Binder.restoreCallingIdentity(identity);
Derek Tan7226c842014-07-02 17:42:23 -07007015 }
Derek Tan7226c842014-07-02 17:42:23 -07007016 }
Shishir Agrawalb1ebf8c2014-07-17 16:32:41 -07007017
7018 @Override
Philip P. Moltmann700a9592019-10-03 11:53:50 -07007019 public String[] getMergedSubscriberIds(int subId, String callingPackage,
7020 String callingFeatureId) {
Jeff Davidson913390f2018-02-23 17:11:49 -08007021 // This API isn't public, so no need to provide a valid subscription ID - we're not worried
7022 // about carrier-privileged callers not having access.
Jeff Davidson7e17e312018-02-13 18:17:36 -08007023 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
Jeff Davidson913390f2018-02-23 17:11:49 -08007024 mApp, SubscriptionManager.INVALID_SUBSCRIPTION_ID, callingPackage,
Philip P. Moltmann700a9592019-10-03 11:53:50 -07007025 callingFeatureId, "getMergedSubscriberIds")) {
Fyodor Kupolov8e53b0b2015-06-17 13:17:50 -07007026 return null;
7027 }
Jeff Sharkey85190e62014-12-05 09:40:12 -08007028
Jordan Liub49b04b2019-05-06 14:45:15 -07007029 // Clear calling identity, when calling TelephonyManager, because callerUid must be
7030 // the process, where TelephonyManager was instantiated.
7031 // Otherwise AppOps check will fail.
Fyodor Kupolov8e53b0b2015-06-17 13:17:50 -07007032 final long identity = Binder.clearCallingIdentity();
7033 try {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08007034 final Context context = mApp;
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007035 final TelephonyManager tele = TelephonyManager.from(context);
7036 final SubscriptionManager sub = SubscriptionManager.from(context);
7037
7038 // Figure out what subscribers are currently active
7039 final ArraySet<String> activeSubscriberIds = new ArraySet<>();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007040
Jordan Liub49b04b2019-05-06 14:45:15 -07007041 // Only consider subs which match the current subId
7042 // This logic can be simplified. See b/131189269 for progress.
7043 if (isActiveSubscription(subId)) {
Fyodor Kupolov8e53b0b2015-06-17 13:17:50 -07007044 activeSubscriberIds.add(tele.getSubscriberId(subId));
7045 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007046
7047 // First pass, find a number override for an active subscriber
7048 String mergeNumber = null;
7049 final Map<String, ?> prefs = mTelephonySharedPreferences.getAll();
7050 for (String key : prefs.keySet()) {
7051 if (key.startsWith(PREF_CARRIERS_SUBSCRIBER_PREFIX)) {
7052 final String subscriberId = (String) prefs.get(key);
7053 if (activeSubscriberIds.contains(subscriberId)) {
7054 final String iccId = key.substring(
7055 PREF_CARRIERS_SUBSCRIBER_PREFIX.length());
7056 final String numberKey = PREF_CARRIERS_NUMBER_PREFIX + iccId;
7057 mergeNumber = (String) prefs.get(numberKey);
7058 if (DBG_MERGE) {
Amit Mahajanb8f13202020-01-27 18:16:07 -08007059 Rlog.d(LOG_TAG, "Found line number " + mergeNumber
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007060 + " for active subscriber " + subscriberId);
7061 }
7062 if (!TextUtils.isEmpty(mergeNumber)) {
7063 break;
7064 }
7065 }
7066 }
7067 }
7068
7069 // Shortcut when no active merged subscribers
7070 if (TextUtils.isEmpty(mergeNumber)) {
7071 return null;
7072 }
7073
7074 // Second pass, find all subscribers under that line override
7075 final ArraySet<String> result = new ArraySet<>();
7076 for (String key : prefs.keySet()) {
7077 if (key.startsWith(PREF_CARRIERS_NUMBER_PREFIX)) {
7078 final String number = (String) prefs.get(key);
7079 if (mergeNumber.equals(number)) {
7080 final String iccId = key.substring(PREF_CARRIERS_NUMBER_PREFIX.length());
7081 final String subscriberKey = PREF_CARRIERS_SUBSCRIBER_PREFIX + iccId;
7082 final String subscriberId = (String) prefs.get(subscriberKey);
7083 if (!TextUtils.isEmpty(subscriberId)) {
7084 result.add(subscriberId);
7085 }
7086 }
7087 }
7088 }
7089
7090 final String[] resultArray = result.toArray(new String[result.size()]);
7091 Arrays.sort(resultArray);
7092 if (DBG_MERGE) {
Amit Mahajanb8f13202020-01-27 18:16:07 -08007093 Rlog.d(LOG_TAG,
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007094 "Found subscribers " + Arrays.toString(resultArray) + " after merge");
7095 }
7096 return resultArray;
Fyodor Kupolov8e53b0b2015-06-17 13:17:50 -07007097 } finally {
7098 Binder.restoreCallingIdentity(identity);
Jeff Sharkey85190e62014-12-05 09:40:12 -08007099 }
Jeff Sharkey85190e62014-12-05 09:40:12 -08007100 }
7101
7102 @Override
zoey chen38003472019-12-13 17:16:31 +08007103 public String[] getMergedImsisFromGroup(int subId, String callingPackage) {
7104 enforceReadPrivilegedPermission("getMergedImsisFromGroup");
Malcolm Chen6ca97372019-07-01 16:28:21 -07007105
7106 final long identity = Binder.clearCallingIdentity();
7107 try {
7108 final TelephonyManager telephonyManager = mApp.getSystemService(
7109 TelephonyManager.class);
7110 String subscriberId = telephonyManager.getSubscriberId(subId);
7111 if (subscriberId == null) {
7112 if (DBG) {
zoey chen38003472019-12-13 17:16:31 +08007113 log("getMergedImsisFromGroup can't find subscriberId for subId "
Malcolm Chen6ca97372019-07-01 16:28:21 -07007114 + subId);
7115 }
7116 return null;
7117 }
7118
Jack Yudf049d72022-12-02 22:48:35 -08007119 ParcelUuid groupUuid;
7120 if (PhoneFactory.isSubscriptionManagerServiceEnabled()) {
7121 final SubscriptionInfo info = SubscriptionManagerService.getInstance()
7122 .getSubscriptionInfo(subId);
7123 groupUuid = info.getGroupUuid();
7124 } else {
7125 final SubscriptionInfo info = mSubscriptionController
7126 .getSubscriptionInfo(subId);
7127 groupUuid = info.getGroupUuid();
7128 }
Malcolm Chen6ca97372019-07-01 16:28:21 -07007129 // If it doesn't belong to any group, return just subscriberId of itself.
7130 if (groupUuid == null) {
7131 return new String[]{subscriberId};
7132 }
7133
7134 // Get all subscriberIds from the group.
7135 final List<String> mergedSubscriberIds = new ArrayList<>();
Jack Yudf049d72022-12-02 22:48:35 -08007136 List<SubscriptionInfo> groupInfos;
7137 if (PhoneFactory.isSubscriptionManagerServiceEnabled()) {
7138 groupInfos = SubscriptionManagerService.getInstance()
7139 .getSubscriptionsInGroup(groupUuid, mApp.getOpPackageName(),
7140 mApp.getAttributionTag());
7141 } else {
7142 groupInfos = mSubscriptionController
7143 .getSubscriptionsInGroup(groupUuid, mApp.getOpPackageName(),
7144 mApp.getAttributionTag());
7145 }
Malcolm Chen6ca97372019-07-01 16:28:21 -07007146 for (SubscriptionInfo subInfo : groupInfos) {
7147 subscriberId = telephonyManager.getSubscriberId(subInfo.getSubscriptionId());
7148 if (subscriberId != null) {
7149 mergedSubscriberIds.add(subscriberId);
7150 }
7151 }
7152
7153 return mergedSubscriberIds.toArray(new String[mergedSubscriberIds.size()]);
7154 } finally {
7155 Binder.restoreCallingIdentity(identity);
7156
7157 }
7158 }
7159
7160 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08007161 public boolean setOperatorBrandOverride(int subId, String brand) {
Shuo Qian2c0ae432019-12-05 11:40:37 -08007162 TelephonyPermissions.enforceCallingOrSelfCarrierPrivilege(mApp,
Jeff Davidson7e17e312018-02-13 18:17:36 -08007163 subId, "setOperatorBrandOverride");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007164
7165 final long identity = Binder.clearCallingIdentity();
7166 try {
7167 final Phone phone = getPhone(subId);
7168 return phone == null ? false : phone.setOperatorBrandOverride(brand);
7169 } finally {
7170 Binder.restoreCallingIdentity(identity);
7171 }
Shishir Agrawalb1ebf8c2014-07-17 16:32:41 -07007172 }
Steven Liu4bf01bc2014-07-17 11:05:29 -05007173
7174 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08007175 public boolean setRoamingOverride(int subId, List<String> gsmRoamingList,
Shishir Agrawal621a47c2014-12-01 10:25:09 -08007176 List<String> gsmNonRoamingList, List<String> cdmaRoamingList,
7177 List<String> cdmaNonRoamingList) {
Shuo Qian2c0ae432019-12-05 11:40:37 -08007178 TelephonyPermissions.enforceCallingOrSelfCarrierPrivilege(
7179 mApp, subId, "setRoamingOverride");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007180
7181 final long identity = Binder.clearCallingIdentity();
7182 try {
7183 final Phone phone = getPhone(subId);
7184 if (phone == null) {
7185 return false;
7186 }
7187 return phone.setRoamingOverride(gsmRoamingList, gsmNonRoamingList, cdmaRoamingList,
7188 cdmaNonRoamingList);
7189 } finally {
7190 Binder.restoreCallingIdentity(identity);
Shishir Agrawalc04d9752016-02-19 10:41:00 -08007191 }
Shishir Agrawal621a47c2014-12-01 10:25:09 -08007192 }
7193
7194 @Override
Shuo Qian850e4d6a2018-04-25 21:02:08 +00007195 @Deprecated
7196 public int invokeOemRilRequestRaw(byte[] oemReq, byte[] oemResp) {
7197 enforceModifyPermission();
7198
7199 int returnValue = 0;
7200 try {
vagdeviaf9a5b92018-08-15 16:01:53 -07007201 AsyncResult result = (AsyncResult) sendRequest(CMD_INVOKE_OEM_RIL_REQUEST_RAW, oemReq);
Shuo Qian850e4d6a2018-04-25 21:02:08 +00007202 if(result.exception == null) {
7203 if (result.result != null) {
7204 byte[] responseData = (byte[])(result.result);
7205 if(responseData.length > oemResp.length) {
7206 Log.w(LOG_TAG, "Buffer to copy response too small: Response length is " +
7207 responseData.length + "bytes. Buffer Size is " +
7208 oemResp.length + "bytes.");
7209 }
7210 System.arraycopy(responseData, 0, oemResp, 0, responseData.length);
7211 returnValue = responseData.length;
7212 }
7213 } else {
7214 CommandException ex = (CommandException) result.exception;
7215 returnValue = ex.getCommandError().ordinal();
7216 if(returnValue > 0) returnValue *= -1;
7217 }
7218 } catch (RuntimeException e) {
7219 Log.w(LOG_TAG, "sendOemRilRequestRaw: Runtime Exception");
7220 returnValue = (CommandException.Error.GENERIC_FAILURE.ordinal());
7221 if(returnValue > 0) returnValue *= -1;
7222 }
7223
7224 return returnValue;
7225 }
7226
7227 @Override
Robert Greenwalt36b23af2015-07-06 17:59:14 -07007228 public int getRadioAccessFamily(int phoneId, String callingPackage) {
Jeff Davidson913390f2018-02-23 17:11:49 -08007229 Phone phone = PhoneFactory.getPhone(phoneId);
Shuo Qiandee53402020-05-29 14:08:15 -07007230 try {
7231 TelephonyPermissions
Nathan Harold62c68512021-04-06 11:26:02 -07007232 .enforceCallingOrSelfReadPrivilegedPhoneStatePermissionOrCarrierPrivilege(
Shuo Qiandee53402020-05-29 14:08:15 -07007233 mApp, phone.getSubId(), "getRadioAccessFamily");
7234 } catch (SecurityException e) {
7235 EventLog.writeEvent(0x534e4554, "150857259", -1, "Missing Permission");
7236 throw e;
7237 }
chen xub97461a2018-10-26 14:17:57 -07007238 int raf = RadioAccessFamily.RAF_UNKNOWN;
Jeff Davidson913390f2018-02-23 17:11:49 -08007239 if (phone == null) {
chen xub97461a2018-10-26 14:17:57 -07007240 return raf;
Jeff Davidson913390f2018-02-23 17:11:49 -08007241 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007242 final long identity = Binder.clearCallingIdentity();
7243 try {
chen xub97461a2018-10-26 14:17:57 -07007244 TelephonyPermissions
Nathan Harold62c68512021-04-06 11:26:02 -07007245 .enforceCallingOrSelfReadPrivilegedPhoneStatePermissionOrCarrierPrivilege(
chen xub97461a2018-10-26 14:17:57 -07007246 mApp, phone.getSubId(), "getRadioAccessFamily");
7247 raf = ProxyController.getInstance().getRadioAccessFamily(phoneId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007248 } finally {
7249 Binder.restoreCallingIdentity(identity);
7250 }
chen xub97461a2018-10-26 14:17:57 -07007251 return raf;
Wink Saville5d475dd2014-10-17 15:00:58 -07007252 }
Andrew Leedf14ead2014-10-17 14:22:52 -07007253
7254 @Override
Hall Liu82694d52020-12-11 18:22:04 -08007255 public void uploadCallComposerPicture(int subscriptionId, String callingPackage,
Hall Liue31bac62020-12-23 19:16:10 -08007256 String contentType, ParcelFileDescriptor fd, ResultReceiver callback) {
Hall Liu82694d52020-12-11 18:22:04 -08007257 try {
7258 if (!Objects.equals(mApp.getPackageManager().getPackageUid(callingPackage, 0),
7259 Binder.getCallingUid())) {
Tyler Gunnb87925a2021-06-10 14:54:27 -07007260 throw new SecurityException("Invalid package:" + callingPackage);
Hall Liu82694d52020-12-11 18:22:04 -08007261 }
7262 } catch (PackageManager.NameNotFoundException e) {
Tyler Gunnb87925a2021-06-10 14:54:27 -07007263 throw new SecurityException("Invalid package:" + callingPackage);
Hall Liu82694d52020-12-11 18:22:04 -08007264 }
7265 RoleManager rm = mApp.getSystemService(RoleManager.class);
7266 List<String> dialerRoleHolders = rm.getRoleHolders(RoleManager.ROLE_DIALER);
7267 if (!dialerRoleHolders.contains(callingPackage)) {
7268 throw new SecurityException("App must be the dialer role holder to"
7269 + " upload a call composer pic");
7270 }
7271
7272 Executors.newSingleThreadExecutor().execute(() -> {
7273 ByteArrayOutputStream output = new ByteArrayOutputStream(
7274 (int) TelephonyManager.getMaximumCallComposerPictureSize());
7275 InputStream input = new ParcelFileDescriptor.AutoCloseInputStream(fd);
7276 boolean readUntilEnd = false;
7277 int totalBytesRead = 0;
7278 byte[] buffer = new byte[16 * 1024];
7279 while (true) {
7280 int numRead;
7281 try {
7282 numRead = input.read(buffer);
7283 } catch (IOException e) {
7284 try {
7285 fd.checkError();
7286 callback.send(TelephonyManager.CallComposerException.ERROR_INPUT_CLOSED,
7287 null);
7288 } catch (IOException e1) {
7289 // This means that the other side closed explicitly with an error. If this
7290 // happens, log and ignore.
7291 loge("Remote end of call composer picture pipe closed: " + e1);
7292 }
7293 break;
7294 }
7295 if (numRead == -1) {
7296 readUntilEnd = true;
7297 break;
7298 }
7299 totalBytesRead += numRead;
7300 if (totalBytesRead > TelephonyManager.getMaximumCallComposerPictureSize()) {
7301 loge("Too many bytes read for call composer picture: " + totalBytesRead);
7302 try {
7303 input.close();
7304 } catch (IOException e) {
7305 // ignore
7306 }
7307 break;
7308 }
7309 output.write(buffer, 0, numRead);
7310 }
7311 // Generally, the remote end will close the file descriptors. The only case where we
7312 // close is above, where the picture size is too big.
7313
7314 try {
7315 fd.checkError();
7316 } catch (IOException e) {
7317 loge("Remote end for call composer closed with an error: " + e);
7318 return;
7319 }
7320
Hall Liuaa4211e2021-01-20 15:43:39 -08007321 if (!readUntilEnd) {
7322 loge("Did not finish reading entire image; aborting");
7323 return;
7324 }
Hall Liu82694d52020-12-11 18:22:04 -08007325
Hall Liuaa4211e2021-01-20 15:43:39 -08007326 ImageData imageData = new ImageData(output.toByteArray(), contentType, null);
7327 CallComposerPictureManager.getInstance(mApp, subscriptionId).handleUploadToServer(
7328 new CallComposerPictureTransfer.Factory() {},
7329 imageData,
7330 (result) -> {
7331 if (result.first != null) {
7332 ParcelUuid parcelUuid = new ParcelUuid(result.first);
7333 Bundle outputResult = new Bundle();
7334 outputResult.putParcelable(
7335 TelephonyManager.KEY_CALL_COMPOSER_PICTURE_HANDLE, parcelUuid);
7336 callback.send(TelephonyManager.CallComposerException.SUCCESS,
7337 outputResult);
7338 } else {
7339 callback.send(result.second, null);
7340 }
7341 }
7342 );
Hall Liu82694d52020-12-11 18:22:04 -08007343 });
7344 }
7345
7346 @Override
Andrew Leedf14ead2014-10-17 14:22:52 -07007347 public void enableVideoCalling(boolean enable) {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08007348 final Phone defaultPhone = getDefaultPhone();
Andrew Leedf14ead2014-10-17 14:22:52 -07007349 enforceModifyPermission();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007350
7351 final long identity = Binder.clearCallingIdentity();
7352 try {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08007353 ImsManager.getInstance(defaultPhone.getContext(),
7354 defaultPhone.getPhoneId()).setVtSetting(enable);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007355 } finally {
7356 Binder.restoreCallingIdentity(identity);
7357 }
Andrew Leedf14ead2014-10-17 14:22:52 -07007358 }
7359
7360 @Override
Philip P. Moltmann700a9592019-10-03 11:53:50 -07007361 public boolean isVideoCallingEnabled(String callingPackage, String callingFeatureId) {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08007362 final Phone defaultPhone = getDefaultPhone();
Philip P. Moltmann700a9592019-10-03 11:53:50 -07007363 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(mApp, defaultPhone.getSubId(),
7364 callingPackage, callingFeatureId, "isVideoCallingEnabled")) {
Amit Mahajan578e53d2018-03-20 16:18:38 +00007365 return false;
7366 }
Svet Ganovb320e182015-04-16 12:30:10 -07007367
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007368 final long identity = Binder.clearCallingIdentity();
7369 try {
7370 // Check the user preference and the system-level IMS setting. Even if the user has
7371 // enabled video calling, if IMS is disabled we aren't able to support video calling.
7372 // In the long run, we may instead need to check if there exists a connection service
7373 // which can support video calling.
7374 ImsManager imsManager =
Nazanin Bakhshif782e562018-12-11 15:15:39 -08007375 ImsManager.getInstance(defaultPhone.getContext(), defaultPhone.getPhoneId());
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007376 return imsManager.isVtEnabledByPlatform()
7377 && imsManager.isEnhanced4gLteModeSettingEnabledByUser()
7378 && imsManager.isVtEnabledByUser();
7379 } finally {
7380 Binder.restoreCallingIdentity(identity);
7381 }
Andrew Leedf14ead2014-10-17 14:22:52 -07007382 }
Libin.Tang@motorola.comafe82642014-12-18 13:27:53 -06007383
Andrew Leea1239f22015-03-02 17:44:07 -08007384 @Override
Philip P. Moltmann700a9592019-10-03 11:53:50 -07007385 public boolean canChangeDtmfToneLength(int subId, String callingPackage,
7386 String callingFeatureId) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007387 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
Philip P. Moltmann700a9592019-10-03 11:53:50 -07007388 mApp, subId, callingPackage, callingFeatureId,
7389 "isVideoCallingEnabled")) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007390 return false;
7391 }
7392
7393 final long identity = Binder.clearCallingIdentity();
7394 try {
7395 CarrierConfigManager configManager =
7396 (CarrierConfigManager) mApp.getSystemService(Context.CARRIER_CONFIG_SERVICE);
Nazanin Bakhshif782e562018-12-11 15:15:39 -08007397 return configManager.getConfigForSubId(subId)
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007398 .getBoolean(CarrierConfigManager.KEY_DTMF_TYPE_ENABLED_BOOL);
7399 } finally {
7400 Binder.restoreCallingIdentity(identity);
7401 }
Andrew Leea1239f22015-03-02 17:44:07 -08007402 }
7403
7404 @Override
Philip P. Moltmann700a9592019-10-03 11:53:50 -07007405 public boolean isWorldPhone(int subId, String callingPackage, String callingFeatureId) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007406 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
Philip P. Moltmann700a9592019-10-03 11:53:50 -07007407 mApp, subId, callingPackage, callingFeatureId, "isVideoCallingEnabled")) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007408 return false;
7409 }
7410
7411 final long identity = Binder.clearCallingIdentity();
7412 try {
7413 CarrierConfigManager configManager =
7414 (CarrierConfigManager) mApp.getSystemService(Context.CARRIER_CONFIG_SERVICE);
Nazanin Bakhshif782e562018-12-11 15:15:39 -08007415 return configManager.getConfigForSubId(subId)
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007416 .getBoolean(CarrierConfigManager.KEY_WORLD_PHONE_BOOL);
7417 } finally {
7418 Binder.restoreCallingIdentity(identity);
7419 }
Andrew Leea1239f22015-03-02 17:44:07 -08007420 }
7421
Andrew Lee9431b832015-03-09 18:46:45 -07007422 @Override
7423 public boolean isTtyModeSupported() {
Tyler Gunn5ddfdc92019-10-31 13:08:23 -07007424 TelecomManager telecomManager = mApp.getSystemService(TelecomManager.class);
Wooki Wu1f82f7a2016-02-15 15:59:58 +08007425 return telecomManager.isTtySupported();
Andrew Lee9431b832015-03-09 18:46:45 -07007426 }
7427
7428 @Override
7429 public boolean isHearingAidCompatibilitySupported() {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007430 final long identity = Binder.clearCallingIdentity();
7431 try {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08007432 return mApp.getResources().getBoolean(R.bool.hac_enabled);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007433 } finally {
7434 Binder.restoreCallingIdentity(identity);
7435 }
Andrew Lee9431b832015-03-09 18:46:45 -07007436 }
7437
Hall Liuf6668912018-10-31 17:05:23 -07007438 /**
7439 * Determines whether the device currently supports RTT (Real-time text). Based both on carrier
7440 * support for the feature and device firmware support.
7441 *
7442 * @return {@code true} if the device and carrier both support RTT, {@code false} otherwise.
7443 */
7444 @Override
7445 public boolean isRttSupported(int subscriptionId) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007446 final long identity = Binder.clearCallingIdentity();
Nazanin Bakhshif782e562018-12-11 15:15:39 -08007447 final Phone phone = getPhone(subscriptionId);
7448 if (phone == null) {
7449 loge("isRttSupported: no Phone found. Invalid subId:" + subscriptionId);
7450 return false;
7451 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007452 try {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08007453 boolean isCarrierSupported = mApp.getCarrierConfigForSubId(subscriptionId).getBoolean(
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007454 CarrierConfigManager.KEY_RTT_SUPPORTED_BOOL);
7455 boolean isDeviceSupported =
Nazanin Bakhshif782e562018-12-11 15:15:39 -08007456 phone.getContext().getResources().getBoolean(R.bool.config_support_rtt);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007457 return isCarrierSupported && isDeviceSupported;
7458 } finally {
7459 Binder.restoreCallingIdentity(identity);
7460 }
Hall Liu98187582018-01-22 19:15:32 -08007461 }
7462
Hall Liuf6668912018-10-31 17:05:23 -07007463 /**
Hall Liuf2daa022019-07-23 18:39:00 -07007464 * Determines whether the user has turned on RTT. If the carrier wants to ignore the user-set
7465 * RTT setting, will return true if the device and carrier both support RTT.
7466 * Otherwise. only returns true if the device and carrier both also support RTT.
Hall Liuf6668912018-10-31 17:05:23 -07007467 */
7468 public boolean isRttEnabled(int subscriptionId) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007469 final long identity = Binder.clearCallingIdentity();
7470 try {
Hall Liu5bab75c2019-12-11 23:58:20 +00007471 boolean isRttSupported = isRttSupported(subscriptionId);
7472 boolean isUserRttSettingOn = Settings.Secure.getInt(
7473 mApp.getContentResolver(), Settings.Secure.RTT_CALLING_MODE, 0) != 0;
7474 boolean shouldIgnoreUserRttSetting = mApp.getCarrierConfigForSubId(subscriptionId)
7475 .getBoolean(CarrierConfigManager.KEY_IGNORE_RTT_MODE_SETTING_BOOL);
7476 return isRttSupported && (isUserRttSettingOn || shouldIgnoreUserRttSetting);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007477 } finally {
7478 Binder.restoreCallingIdentity(identity);
7479 }
Hall Liu3ad5f012018-04-06 16:23:39 -07007480 }
7481
Philip P. Moltmann700a9592019-10-03 11:53:50 -07007482 @Deprecated
7483 @Override
7484 public String getDeviceId(String callingPackage) {
7485 return getDeviceIdWithFeature(callingPackage, null);
7486 }
7487
Sanket Padawe7310cc72015-01-14 09:53:20 -08007488 /**
7489 * Returns the unique device ID of phone, for example, the IMEI for
7490 * GSM and the MEID for CDMA phones. Return null if device ID is not available.
7491 *
7492 * <p>Requires Permission:
7493 * {@link android.Manifest.permission#READ_PHONE_STATE READ_PHONE_STATE}
7494 */
7495 @Override
Philip P. Moltmann700a9592019-10-03 11:53:50 -07007496 public String getDeviceIdWithFeature(String callingPackage, String callingFeatureId) {
Shuo Qian13d89152021-05-10 23:58:11 -07007497 try {
7498 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
7499 } catch (SecurityException se) {
7500 EventLog.writeEvent(0x534e4554, "186530889", Binder.getCallingUid());
7501 throw new SecurityException("Package " + callingPackage + " does not belong to "
7502 + Binder.getCallingUid());
7503 }
Sanket Padawe7310cc72015-01-14 09:53:20 -08007504 final Phone phone = PhoneFactory.getPhone(0);
Jeff Davidson913390f2018-02-23 17:11:49 -08007505 if (phone == null) {
Sanket Padawe7310cc72015-01-14 09:53:20 -08007506 return null;
7507 }
Jeff Davidson913390f2018-02-23 17:11:49 -08007508 int subId = phone.getSubId();
Michael Groover70af6dc2018-10-01 16:23:15 -07007509 if (!TelephonyPermissions.checkCallingOrSelfReadDeviceIdentifiers(mApp, subId,
Philip P. Moltmann700a9592019-10-03 11:53:50 -07007510 callingPackage, callingFeatureId, "getDeviceId")) {
Jeff Davidson913390f2018-02-23 17:11:49 -08007511 return null;
7512 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007513
7514 final long identity = Binder.clearCallingIdentity();
7515 try {
7516 return phone.getDeviceId();
7517 } finally {
7518 Binder.restoreCallingIdentity(identity);
7519 }
Sanket Padawe7310cc72015-01-14 09:53:20 -08007520 }
7521
Ping Sunc67b7c22016-03-02 19:16:45 +08007522 /**
7523 * {@hide}
7524 * Returns the IMS Registration Status on a particular subid
7525 *
7526 * @param subId
7527 */
Brad Ebinger1f2b5082018-02-08 16:11:32 -08007528 public boolean isImsRegistered(int subId) {
Ping Sunc67b7c22016-03-02 19:16:45 +08007529 Phone phone = getPhone(subId);
7530 if (phone != null) {
7531 return phone.isImsRegistered();
7532 } else {
7533 return false;
7534 }
7535 }
7536
Santos Cordon7a1885b2015-02-03 11:15:19 -08007537 @Override
Shuo Qian6e6137d2019-10-30 16:33:31 -07007538 public int getSubIdForPhoneAccountHandle(
Philip P. Moltmann700a9592019-10-03 11:53:50 -07007539 PhoneAccountHandle phoneAccountHandle, String callingPackage, String callingFeatureId) {
Shuo Qian6e6137d2019-10-30 16:33:31 -07007540 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(mApp, getDefaultSubscription(),
Philip P. Moltmann700a9592019-10-03 11:53:50 -07007541 callingPackage, callingFeatureId, "getSubIdForPhoneAccountHandle")) {
Shuo Qian6e6137d2019-10-30 16:33:31 -07007542 throw new SecurityException("Requires READ_PHONE_STATE permission.");
7543 }
7544 final long identity = Binder.clearCallingIdentity();
7545 try {
7546 return PhoneUtils.getSubIdForPhoneAccountHandle(phoneAccountHandle);
7547 } finally {
7548 Binder.restoreCallingIdentity(identity);
7549 }
7550 }
7551
7552 @Override
Tyler Gunnf70ed162019-04-03 15:28:53 -07007553 public @Nullable PhoneAccountHandle getPhoneAccountHandleForSubscriptionId(int subscriptionId) {
Alireza Forouzan4ac4f982021-03-16 22:18:52 -07007554 TelephonyPermissions
Nathan Harold62c68512021-04-06 11:26:02 -07007555 .enforceCallingOrSelfReadPrivilegedPhoneStatePermissionOrCarrierPrivilege(
Alireza Forouzan4ac4f982021-03-16 22:18:52 -07007556 mApp,
7557 subscriptionId,
7558 "getPhoneAccountHandleForSubscriptionId, " + "subscriptionId: " + subscriptionId);
Tyler Gunnf70ed162019-04-03 15:28:53 -07007559 final long identity = Binder.clearCallingIdentity();
7560 try {
7561 Phone phone = getPhone(subscriptionId);
7562 if (phone == null) {
7563 return null;
7564 }
7565 return PhoneUtils.makePstnPhoneAccountHandle(phone);
7566 } finally {
7567 Binder.restoreCallingIdentity(identity);
7568 }
7569 }
7570
Brad Ebinger1f2b5082018-02-08 16:11:32 -08007571 /**
7572 * @return the VoWiFi calling availability.
Nathan Haroldc55097a2015-03-11 18:14:50 -07007573 */
Brad Ebinger1f2b5082018-02-08 16:11:32 -08007574 public boolean isWifiCallingAvailable(int subId) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007575 final long identity = Binder.clearCallingIdentity();
7576 try {
7577 Phone phone = getPhone(subId);
7578 if (phone != null) {
7579 return phone.isWifiCallingEnabled();
7580 } else {
7581 return false;
7582 }
7583 } finally {
7584 Binder.restoreCallingIdentity(identity);
Brad Ebinger1f2b5082018-02-08 16:11:32 -08007585 }
Nathan Haroldc55097a2015-03-11 18:14:50 -07007586 }
7587
Brad Ebinger1f2b5082018-02-08 16:11:32 -08007588 /**
Brad Ebinger1f2b5082018-02-08 16:11:32 -08007589 * @return the VT calling availability.
Etan Cohen3b7a1bc2015-05-28 15:57:13 -07007590 */
Brad Ebinger1f2b5082018-02-08 16:11:32 -08007591 public boolean isVideoTelephonyAvailable(int subId) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007592 final long identity = Binder.clearCallingIdentity();
7593 try {
7594 Phone phone = getPhone(subId);
7595 if (phone != null) {
7596 return phone.isVideoEnabled();
7597 } else {
7598 return false;
7599 }
7600 } finally {
7601 Binder.restoreCallingIdentity(identity);
Brad Ebinger1f2b5082018-02-08 16:11:32 -08007602 }
7603 }
7604
7605 /**
7606 * @return the IMS registration technology for the MMTEL feature. Valid return values are
7607 * defined in {@link ImsRegistrationImplBase}.
7608 */
7609 public @ImsRegistrationImplBase.ImsRegistrationTech int getImsRegTechnologyForMmTel(int subId) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007610 final long identity = Binder.clearCallingIdentity();
7611 try {
7612 Phone phone = getPhone(subId);
7613 if (phone != null) {
7614 return phone.getImsRegistrationTech();
7615 } else {
7616 return ImsRegistrationImplBase.REGISTRATION_TECH_NONE;
7617 }
7618 } finally {
7619 Binder.restoreCallingIdentity(identity);
Brad Ebinger1f2b5082018-02-08 16:11:32 -08007620 }
Etan Cohen3b7a1bc2015-05-28 15:57:13 -07007621 }
7622
Stuart Scott8eef64f2015-04-08 15:13:54 -07007623 @Override
Sarah Chinecc78c42022-03-31 21:16:48 -07007624 public void factoryReset(int subId, String callingPackage) {
paulhu5a773602019-08-23 19:17:33 +08007625 enforceSettingsPermission();
Stuart Scott981d8582015-04-21 14:09:50 -07007626 if (mUserManager.hasUserRestriction(UserManager.DISALLOW_NETWORK_RESET)) {
7627 return;
7628 }
Kai Shif70f46f2021-03-03 13:59:46 -08007629 Phone defaultPhone = getDefaultPhone();
7630 if (defaultPhone != null) {
7631 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
7632 mApp, getDefaultPhone().getSubId(), "factoryReset");
7633 }
Svet Ganovcc087f82015-05-12 20:35:54 -07007634 final long identity = Binder.clearCallingIdentity();
Nazanin Bakhshif782e562018-12-11 15:15:39 -08007635
Svet Ganovcc087f82015-05-12 20:35:54 -07007636 try {
Stuart Scott981d8582015-04-21 14:09:50 -07007637 if (SubscriptionManager.isUsableSubIdValue(subId) && !mUserManager.hasUserRestriction(
7638 UserManager.DISALLOW_CONFIG_MOBILE_NETWORKS)) {
Sooraj Sasindran95c07a92020-07-15 01:35:24 -07007639 setDataEnabledForReason(subId, TelephonyManager.DATA_ENABLED_REASON_USER,
Sarah Chinecc78c42022-03-31 21:16:48 -07007640 getDefaultDataEnabled(), callingPackage);
Svet Ganovcc087f82015-05-12 20:35:54 -07007641 setNetworkSelectionModeAutomatic(subId);
SongFerngWang8c6e82e2021-03-02 22:09:29 +08007642 Phone phone = getPhone(subId);
SongFerngWangfd89b102021-05-27 22:44:54 +08007643 cleanUpAllowedNetworkTypes(phone, subId);
Nazanin Bakhshif782e562018-12-11 15:15:39 -08007644 setDataRoamingEnabled(subId, getDefaultDataRoamingEnabled(subId));
Jordan Liu857e73a2021-03-05 16:24:04 -08007645 getPhone(subId).resetCarrierKeysForImsiEncryption();
Svet Ganovcc087f82015-05-12 20:35:54 -07007646 }
Amit Mahajan7dbbd822019-03-13 17:33:47 -07007647 // There has been issues when Sms raw table somehow stores orphan
7648 // fragments. They lead to garbled message when new fragments come
7649 // in and combined with those stale ones. In case this happens again,
7650 // user can reset all network settings which will clean up this table.
7651 cleanUpSmsRawTable(getDefaultPhone().getContext());
Brad Ebingerbc7dd582019-10-17 17:03:22 -07007652 // Clean up IMS settings as well here.
7653 int slotId = getSlotIndex(subId);
7654 if (slotId > SubscriptionManager.INVALID_SIM_SLOT_INDEX) {
7655 ImsManager.getInstance(mApp, slotId).factoryReset();
7656 }
Naina Nallurid63128d2019-09-17 14:10:30 -07007657
Kai Shif70f46f2021-03-03 13:59:46 -08007658 if (defaultPhone == null) {
7659 return;
7660 }
Naina Nallurid63128d2019-09-17 14:10:30 -07007661 // Erase modem config if erase modem on network setting is enabled.
7662 String configValue = DeviceConfig.getProperty(DeviceConfig.NAMESPACE_TELEPHONY,
7663 RESET_NETWORK_ERASE_MODEM_CONFIG_ENABLED);
7664 if (configValue != null && Boolean.parseBoolean(configValue)) {
Kai Shif70f46f2021-03-03 13:59:46 -08007665 sendEraseModemConfig(defaultPhone);
Naina Nallurid63128d2019-09-17 14:10:30 -07007666 }
Kai Shif70f46f2021-03-03 13:59:46 -08007667
7668 sendEraseDataInSharedPreferences(defaultPhone);
Svet Ganovcc087f82015-05-12 20:35:54 -07007669 } finally {
7670 Binder.restoreCallingIdentity(identity);
Stuart Scott8eef64f2015-04-08 15:13:54 -07007671 }
7672 }
Narayan Kamath1c496c22015-04-16 14:40:19 +01007673
SongFerngWangfd89b102021-05-27 22:44:54 +08007674 @VisibleForTesting
7675 void cleanUpAllowedNetworkTypes(Phone phone, int subId) {
7676 if (phone == null || !SubscriptionManager.isUsableSubscriptionId(subId)) {
7677 return;
7678 }
7679 long defaultNetworkType = RadioAccessFamily.getRafFromNetworkType(
7680 RILConstants.PREFERRED_NETWORK_MODE);
7681 SubscriptionManager.setSubscriptionProperty(subId,
7682 SubscriptionManager.ALLOWED_NETWORK_TYPES,
7683 "user=" + defaultNetworkType);
7684 phone.loadAllowedNetworksFromSubscriptionDatabase();
7685 phone.setAllowedNetworkTypes(TelephonyManager.ALLOWED_NETWORK_TYPES_REASON_USER,
7686 defaultNetworkType, null);
7687 }
7688
Amit Mahajan7dbbd822019-03-13 17:33:47 -07007689 private void cleanUpSmsRawTable(Context context) {
7690 ContentResolver resolver = context.getContentResolver();
7691 Uri uri = Uri.withAppendedPath(Telephony.Sms.CONTENT_URI, "raw/permanentDelete");
7692 resolver.delete(uri, null, null);
7693 }
7694
Narayan Kamath1c496c22015-04-16 14:40:19 +01007695 @Override
chen xu5d3637b2019-01-21 23:31:38 -08007696 public String getSimLocaleForSubscriber(int subId) {
7697 enforceReadPrivilegedPermission("getSimLocaleForSubscriber, subId: " + subId);
7698 final Phone phone = getPhone(subId);
7699 if (phone == null) {
7700 log("getSimLocaleForSubscriber, invalid subId");
chen xu2bb91e42019-01-24 14:35:54 -08007701 return null;
chen xu5d3637b2019-01-21 23:31:38 -08007702 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007703 final long identity = Binder.clearCallingIdentity();
7704 try {
Jack Yudf049d72022-12-02 22:48:35 -08007705 SubscriptionInfo info;
7706 if (PhoneFactory.isSubscriptionManagerServiceEnabled()) {
7707 info = SubscriptionManagerService.getInstance().getActiveSubscriptionInfo(subId,
7708 phone.getContext().getOpPackageName(),
7709 phone.getContext().getAttributionTag());
7710 if (info == null) {
7711 log("getSimLocaleForSubscriber, inactive subId: " + subId);
7712 return null;
7713 }
7714 } else {
7715 info = mSubscriptionController.getActiveSubscriptionInfo(subId,
7716 phone.getContext().getOpPackageName(),
7717 phone.getContext().getAttributionTag());
7718 if (info == null) {
7719 log("getSimLocaleForSubscriber, inactive subId: " + subId);
7720 return null;
7721 }
chen xu6291c472019-02-04 12:55:53 -08007722 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007723 // Try and fetch the locale from the carrier properties or from the SIM language
7724 // preferences (EF-PL and EF-LI)...
7725 final int mcc = info.getMcc();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007726 String simLanguage = null;
chen xu5d3637b2019-01-21 23:31:38 -08007727 final Locale localeFromDefaultSim = phone.getLocaleFromSimAndCarrierPrefs();
7728 if (localeFromDefaultSim != null) {
7729 if (!localeFromDefaultSim.getCountry().isEmpty()) {
7730 if (DBG) log("Using locale from subId: " + subId + " locale: "
7731 + localeFromDefaultSim);
tom hsu22b7c3e2022-10-27 00:10:04 +08007732 return matchLocaleFromSupportedLocaleList(phone, localeFromDefaultSim);
chen xu5d3637b2019-01-21 23:31:38 -08007733 } else {
7734 simLanguage = localeFromDefaultSim.getLanguage();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007735 }
7736 }
Narayan Kamath1c496c22015-04-16 14:40:19 +01007737
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007738 // The SIM language preferences only store a language (e.g. fr = French), not an
7739 // exact locale (e.g. fr_FR = French/France). So, if the locale returned from
7740 // the SIM and carrier preferences does not include a country we add the country
7741 // determined from the SIM MCC to provide an exact locale.
zoey chenc730df82019-12-18 17:07:20 +08007742 final Locale mccLocale = LocaleUtils.getLocaleFromMcc(mApp, mcc, simLanguage);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007743 if (mccLocale != null) {
chen xu5d3637b2019-01-21 23:31:38 -08007744 if (DBG) log("No locale from SIM, using mcc locale:" + mccLocale);
tom hsu22b7c3e2022-10-27 00:10:04 +08007745 return matchLocaleFromSupportedLocaleList(phone, mccLocale);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007746 }
7747
7748 if (DBG) log("No locale found - returning null");
7749 return null;
7750 } finally {
7751 Binder.restoreCallingIdentity(identity);
7752 }
Narayan Kamath1c496c22015-04-16 14:40:19 +01007753 }
7754
tom hsue9360cd2022-09-29 23:49:21 +08007755 @VisibleForTesting
tom hsu22b7c3e2022-10-27 00:10:04 +08007756 String matchLocaleFromSupportedLocaleList(Phone phone, @NonNull Locale inputLocale) {
tom hsue9360cd2022-09-29 23:49:21 +08007757 String[] supportedLocale = com.android.internal.app.LocalePicker.getSupportedLocales(
tom hsu22b7c3e2022-10-27 00:10:04 +08007758 phone.getContext());
tom hsue9360cd2022-09-29 23:49:21 +08007759 for (String localeTag : supportedLocale) {
tom hsu22b7c3e2022-10-27 00:10:04 +08007760 if (LocaleList.matchesLanguageAndScript(inputLocale, Locale.forLanguageTag(localeTag))
7761 && TextUtils.equals(inputLocale.getCountry(),
tom hsue9360cd2022-09-29 23:49:21 +08007762 Locale.forLanguageTag(localeTag).getCountry())) {
7763 return localeTag;
7764 }
7765 }
7766 return inputLocale.toLanguageTag();
7767 }
7768
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007769 /**
7770 * NOTE: this method assumes permission checks are done and caller identity has been cleared.
7771 */
7772 private List<SubscriptionInfo> getActiveSubscriptionInfoListPrivileged() {
Jack Yudf049d72022-12-02 22:48:35 -08007773 if (PhoneFactory.isSubscriptionManagerServiceEnabled()) {
7774 return SubscriptionManagerService.getInstance().getActiveSubscriptionInfoList(
7775 mApp.getOpPackageName(), mApp.getAttributionTag());
7776 }
Philip P. Moltmann700a9592019-10-03 11:53:50 -07007777 return mSubscriptionController.getActiveSubscriptionInfoList(mApp.getOpPackageName(),
Philip P. Moltmann8d34f0c2020-03-05 16:24:02 -08007778 mApp.getAttributionTag());
Narayan Kamath1c496c22015-04-16 14:40:19 +01007779 }
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -07007780
Gary Jian3aa9a762022-01-24 16:41:19 +08007781 private ActivityStatsTechSpecificInfo[] mLastModemActivitySpecificInfo = null;
7782 private ModemActivityInfo mLastModemActivityInfo = null;
Chenjie Yu1ba97252018-01-11 18:16:20 -08007783
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -07007784 /**
Adam Lesinski903a54c2016-04-11 14:49:52 -07007785 * Responds to the ResultReceiver with the {@link android.telephony.ModemActivityInfo} object
7786 * representing the state of the modem.
7787 *
Chenjie Yu1ba97252018-01-11 18:16:20 -08007788 * NOTE: The underlying implementation clears the modem state, so there should only ever be one
7789 * caller to it. Everyone should call this class to get cumulative data.
Adam Lesinski903a54c2016-04-11 14:49:52 -07007790 * @hide
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -07007791 */
7792 @Override
Adam Lesinski903a54c2016-04-11 14:49:52 -07007793 public void requestModemActivityInfo(ResultReceiver result) {
7794 enforceModifyPermission();
vagdeviaf9a5b92018-08-15 16:01:53 -07007795 WorkSource workSource = getWorkSource(Binder.getCallingUid());
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007796
7797 final long identity = Binder.clearCallingIdentity();
7798 try {
Shuo Qian8f4750a2020-02-20 17:12:10 -08007799 sendRequestAsync(CMD_GET_MODEM_ACTIVITY_INFO, result, null, workSource);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007800 } finally {
7801 Binder.restoreCallingIdentity(identity);
Chenjie Yu1ba97252018-01-11 18:16:20 -08007802 }
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -07007803 }
Jack Yu85bd38a2015-11-09 11:34:32 -08007804
Siddharth Rayb8114062018-06-17 15:02:38 -07007805 // Checks that ModemActivityInfo is valid. Sleep time, Idle time, Rx time and Tx time should be
7806 // less than total activity duration.
7807 private boolean isModemActivityInfoValid(ModemActivityInfo info) {
7808 if (info == null) {
7809 return false;
7810 }
7811 int activityDurationMs =
Hall Liu49656c02020-10-09 19:00:11 -07007812 (int) (info.getTimestampMillis() - mLastModemActivityInfo.getTimestampMillis());
7813 int totalTxTimeMs = Arrays.stream(info.getTransmitTimeMillis()).sum();
7814
Siddharth Rayb8114062018-06-17 15:02:38 -07007815 return (info.isValid()
7816 && (info.getSleepTimeMillis() <= activityDurationMs)
7817 && (info.getIdleTimeMillis() <= activityDurationMs)
Chen Xud78231e2019-09-10 18:49:52 -07007818 && (info.getReceiveTimeMillis() <= activityDurationMs)
Siddharth Rayb8114062018-06-17 15:02:38 -07007819 && (totalTxTimeMs <= activityDurationMs));
7820 }
7821
Gary Jian3aa9a762022-01-24 16:41:19 +08007822 private void updateLastModemActivityInfo(ModemActivityInfo info, int rat, int freq) {
7823 int[] mergedTxTimeMs = new int [ModemActivityInfo.getNumTxPowerLevels()];
7824 int[] txTimeMs = info.getTransmitTimeMillis(rat, freq);
7825 int[] lastModemTxTimeMs = mLastModemActivityInfo.getTransmitTimeMillis(rat, freq);
7826
7827 for (int lvl = 0; lvl < mergedTxTimeMs.length; lvl++) {
7828 mergedTxTimeMs[lvl] = txTimeMs[lvl] + lastModemTxTimeMs[lvl];
7829 }
7830
7831 mLastModemActivityInfo.setTransmitTimeMillis(rat, freq, mergedTxTimeMs);
7832 mLastModemActivityInfo.setReceiveTimeMillis(
7833 rat,
7834 freq,
7835 info.getReceiveTimeMillis(rat, freq)
7836 + mLastModemActivityInfo.getReceiveTimeMillis(rat, freq));
7837 }
7838
7839 private void updateLastModemActivityInfo(ModemActivityInfo info, int rat) {
7840 int[] mergedTxTimeMs = new int [ModemActivityInfo.getNumTxPowerLevels()];
7841 int[] txTimeMs = info.getTransmitTimeMillis(rat);
7842 int[] lastModemTxTimeMs = mLastModemActivityInfo.getTransmitTimeMillis(rat);
7843
7844 for (int lvl = 0; lvl < mergedTxTimeMs.length; lvl++) {
7845 mergedTxTimeMs[lvl] = txTimeMs[lvl] + lastModemTxTimeMs[lvl];
7846 }
7847 mLastModemActivityInfo.setTransmitTimeMillis(rat, mergedTxTimeMs);
7848 mLastModemActivityInfo.setReceiveTimeMillis(
7849 rat,
7850 info.getReceiveTimeMillis(rat) + mLastModemActivityInfo.getReceiveTimeMillis(rat));
7851 }
7852
7853 /**
7854 * Merge this ModemActivityInfo with mLastModemActivitySpecificInfo
7855 * @param info recent ModemActivityInfo
7856 */
7857 private void mergeModemActivityInfo(ModemActivityInfo info) {
7858 List<ActivityStatsTechSpecificInfo> merged = new ArrayList<>();
Kai Shi917fdc62022-11-28 14:01:02 -08007859 ActivityStatsTechSpecificInfo deltaSpecificInfo;
Gary Jian3aa9a762022-01-24 16:41:19 +08007860 boolean matched;
7861 for (int i = 0; i < info.getSpecificInfoLength(); i++) {
7862 matched = false;
7863 int rat = info.getSpecificInfoRat(i);
7864 int freq = info.getSpecificInfoFrequencyRange(i);
7865 //Check each ActivityStatsTechSpecificInfo in this ModemActivityInfo for new rat returns
7866 //Add a new ActivityStatsTechSpecificInfo if is a new rat, and merge with the original
7867 //if it already exists
7868 for (int j = 0; j < mLastModemActivitySpecificInfo.length; j++) {
7869 if (rat == mLastModemActivityInfo.getSpecificInfoRat(j) && !matched) {
7870 //Merged based on frequency range (MMWAVE vs SUB6) for 5G
7871 if (rat == AccessNetworkConstants.AccessNetworkType.NGRAN) {
7872 if (freq == mLastModemActivityInfo.getSpecificInfoFrequencyRange(j)) {
7873 updateLastModemActivityInfo(info, rat, freq);
7874 matched = true;
7875 }
7876 } else {
7877 updateLastModemActivityInfo(info, rat);
7878 matched = true;
7879 }
7880 }
7881 }
7882
7883 if (!matched) {
Kai Shi917fdc62022-11-28 14:01:02 -08007884 deltaSpecificInfo =
Gary Jian3aa9a762022-01-24 16:41:19 +08007885 new ActivityStatsTechSpecificInfo(
7886 rat,
7887 freq,
7888 info.getTransmitTimeMillis(rat, freq),
7889 (int) info.getReceiveTimeMillis(rat, freq));
Kai Shi917fdc62022-11-28 14:01:02 -08007890 merged.addAll(Arrays.asList(deltaSpecificInfo));
Gary Jian3aa9a762022-01-24 16:41:19 +08007891 }
7892 }
7893 merged.addAll(Arrays.asList(mLastModemActivitySpecificInfo));
7894 mLastModemActivitySpecificInfo =
7895 new ActivityStatsTechSpecificInfo[merged.size()];
7896 merged.toArray(mLastModemActivitySpecificInfo);
7897
7898 mLastModemActivityInfo.setTimestamp(info.getTimestampMillis());
7899 mLastModemActivityInfo.setSleepTimeMillis(
7900 info.getSleepTimeMillis()
7901 + mLastModemActivityInfo.getSleepTimeMillis());
7902 mLastModemActivityInfo.setIdleTimeMillis(
7903 info.getIdleTimeMillis()
7904 + mLastModemActivityInfo.getIdleTimeMillis());
Kai Shi917fdc62022-11-28 14:01:02 -08007905
7906 mLastModemActivityInfo =
7907 new ModemActivityInfo(
7908 mLastModemActivityInfo.getTimestampMillis(),
7909 mLastModemActivityInfo.getSleepTimeMillis(),
7910 mLastModemActivityInfo.getIdleTimeMillis(),
7911 mLastModemActivitySpecificInfo);
7912 }
7913
7914 private ActivityStatsTechSpecificInfo[] deepCopyModemActivitySpecificInfo(
7915 ActivityStatsTechSpecificInfo[] info) {
7916 int infoSize = info.length;
7917 ActivityStatsTechSpecificInfo[] ret = new ActivityStatsTechSpecificInfo[infoSize];
7918 for (int i = 0; i < infoSize; i++) {
7919 ret[i] = new ActivityStatsTechSpecificInfo(
7920 info[i].getRat(), info[i].getFrequencyRange(),
7921 info[i].getTransmitTimeMillis(),
7922 (int) info[i].getReceiveTimeMillis());
7923 }
7924 return ret;
Gary Jian3aa9a762022-01-24 16:41:19 +08007925 }
7926
Jack Yu85bd38a2015-11-09 11:34:32 -08007927 /**
Jack Yu85bd38a2015-11-09 11:34:32 -08007928 * Returns the service state information on specified subscription.
7929 */
7930 @Override
Sooraj Sasindran2250dc02021-11-10 16:42:01 -08007931 public ServiceState getServiceStateForSubscriber(int subId,
7932 boolean renounceFineLocationAccess, boolean renounceCoarseLocationAccess,
7933 String callingPackage, String callingFeatureId) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08007934 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
Philip P. Moltmann700a9592019-10-03 11:53:50 -07007935 mApp, subId, callingPackage, callingFeatureId, "getServiceStateForSubscriber")) {
Jack Yu85bd38a2015-11-09 11:34:32 -08007936 return null;
7937 }
7938
Sooraj Sasindran2250dc02021-11-10 16:42:01 -08007939 boolean hasFinePermission = false;
7940 boolean hasCoarsePermission = false;
7941 if (!renounceFineLocationAccess) {
7942 LocationAccessPolicy.LocationPermissionResult fineLocationResult =
7943 LocationAccessPolicy.checkLocationPermission(mApp,
7944 new LocationAccessPolicy.LocationPermissionQuery.Builder()
7945 .setCallingPackage(callingPackage)
7946 .setCallingFeatureId(callingFeatureId)
7947 .setCallingPid(Binder.getCallingPid())
7948 .setCallingUid(Binder.getCallingUid())
7949 .setMethod("getServiceStateForSubscriber")
7950 .setLogAsInfo(true)
7951 .setMinSdkVersionForFine(Build.VERSION_CODES.Q)
7952 .setMinSdkVersionForCoarse(Build.VERSION_CODES.Q)
7953 .setMinSdkVersionForEnforcement(Build.VERSION_CODES.Q)
7954 .build());
7955 hasFinePermission =
7956 fineLocationResult == LocationAccessPolicy.LocationPermissionResult.ALLOWED;
7957 }
Hall Liuf19c44f2018-11-27 14:38:17 -08007958
Sooraj Sasindran2250dc02021-11-10 16:42:01 -08007959 if (!renounceCoarseLocationAccess) {
7960 LocationAccessPolicy.LocationPermissionResult coarseLocationResult =
7961 LocationAccessPolicy.checkLocationPermission(mApp,
7962 new LocationAccessPolicy.LocationPermissionQuery.Builder()
7963 .setCallingPackage(callingPackage)
7964 .setCallingFeatureId(callingFeatureId)
7965 .setCallingPid(Binder.getCallingPid())
7966 .setCallingUid(Binder.getCallingUid())
7967 .setMethod("getServiceStateForSubscriber")
7968 .setLogAsInfo(true)
7969 .setMinSdkVersionForCoarse(Build.VERSION_CODES.Q)
7970 .setMinSdkVersionForFine(Integer.MAX_VALUE)
7971 .setMinSdkVersionForEnforcement(Build.VERSION_CODES.Q)
7972 .build());
7973 hasCoarsePermission =
7974 coarseLocationResult == LocationAccessPolicy.LocationPermissionResult.ALLOWED;
7975 }
Hall Liuf19c44f2018-11-27 14:38:17 -08007976
Jack Yu479f40e2020-10-27 21:29:25 -07007977 final Phone phone = getPhone(subId);
7978 if (phone == null) {
7979 return null;
7980 }
7981
Jordan Liu0f2bc442020-11-18 16:47:37 -08007982 final long identity = Binder.clearCallingIdentity();
7983
Jack Yu479f40e2020-10-27 21:29:25 -07007984 boolean isCallingPackageDataService = phone.getDataServicePackages()
7985 .contains(callingPackage);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007986 try {
Jordan Liuc437b192020-08-17 10:59:12 -07007987 // isActiveSubId requires READ_PHONE_STATE, which we already check for above
Jack Yudf049d72022-12-02 22:48:35 -08007988 if (PhoneFactory.isSubscriptionManagerServiceEnabled()) {
7989 SubscriptionInfoInternal subInfo = SubscriptionManagerService.getInstance()
7990 .getSubscriptionInfoInternal(subId);
7991 if (subInfo == null || !subInfo.isActive()) {
7992 Rlog.d(LOG_TAG, "getServiceStateForSubscriber returning null for inactive "
7993 + "subId=" + subId);
7994 return null;
7995 }
7996 } else {
7997 if (!mSubscriptionController.isActiveSubId(subId, callingPackage,
7998 callingFeatureId)) {
7999 Rlog.d(LOG_TAG, "getServiceStateForSubscriber returning null for inactive "
8000 + "subId=" + subId);
8001 return null;
8002 }
Jordan Liuc437b192020-08-17 10:59:12 -07008003 }
8004
Hall Liuf19c44f2018-11-27 14:38:17 -08008005 ServiceState ss = phone.getServiceState();
8006
8007 // Scrub out the location info in ServiceState depending on what level of access
8008 // the caller has.
Jack Yu479f40e2020-10-27 21:29:25 -07008009 if (hasFinePermission || isCallingPackageDataService) return ss;
Malcolm Chen5052de62019-12-30 13:56:38 -08008010 if (hasCoarsePermission) return ss.createLocationInfoSanitizedCopy(false);
8011 return ss.createLocationInfoSanitizedCopy(true);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008012 } finally {
8013 Binder.restoreCallingIdentity(identity);
8014 }
Jack Yu85bd38a2015-11-09 11:34:32 -08008015 }
Nancy Chen31f9ba12016-01-06 11:42:12 -08008016
8017 /**
8018 * Returns the URI for the per-account voicemail ringtone set in Phone settings.
8019 *
8020 * @param accountHandle The handle for the {@link PhoneAccount} for which to retrieve the
8021 * voicemail ringtone.
8022 * @return The URI for the ringtone to play when receiving a voicemail from a specific
8023 * PhoneAccount.
8024 */
8025 @Override
8026 public Uri getVoicemailRingtoneUri(PhoneAccountHandle accountHandle) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008027 final long identity = Binder.clearCallingIdentity();
8028 try {
8029 Phone phone = PhoneUtils.getPhoneForPhoneAccountHandle(accountHandle);
8030 if (phone == null) {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08008031 phone = getDefaultPhone();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008032 }
Nancy Chen31f9ba12016-01-06 11:42:12 -08008033
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008034 return VoicemailNotificationSettingsUtil.getRingtoneUri(phone.getContext());
8035 } finally {
8036 Binder.restoreCallingIdentity(identity);
8037 }
Nancy Chen31f9ba12016-01-06 11:42:12 -08008038 }
8039
8040 /**
Ta-wei Yenc33877d2017-01-23 18:11:21 -08008041 * Sets the per-account voicemail ringtone.
8042 *
8043 * <p>Requires that the calling app is the default dialer, or has carrier privileges, or
8044 * has permission {@link android.Manifest.permission#MODIFY_PHONE_STATE MODIFY_PHONE_STATE}.
8045 *
8046 * @param phoneAccountHandle The handle for the {@link PhoneAccount} for which to set the
8047 * voicemail ringtone.
8048 * @param uri The URI for the ringtone to play when receiving a voicemail from a specific
8049 * PhoneAccount.
8050 */
8051 @Override
8052 public void setVoicemailRingtoneUri(String callingPackage,
8053 PhoneAccountHandle phoneAccountHandle, Uri uri) {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08008054 final Phone defaultPhone = getDefaultPhone();
Ta-wei Yenc33877d2017-01-23 18:11:21 -08008055 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
Tyler Gunn5ddfdc92019-10-31 13:08:23 -07008056 TelecomManager tm = defaultPhone.getContext().getSystemService(TelecomManager.class);
8057 if (!TextUtils.equals(callingPackage, tm.getDefaultDialerPackage())) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08008058 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
8059 mApp, PhoneUtils.getSubIdForPhoneAccountHandle(phoneAccountHandle),
8060 "setVoicemailRingtoneUri");
Ta-wei Yenc33877d2017-01-23 18:11:21 -08008061 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008062
8063 final long identity = Binder.clearCallingIdentity();
8064 try {
8065 Phone phone = PhoneUtils.getPhoneForPhoneAccountHandle(phoneAccountHandle);
8066 if (phone == null) {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08008067 phone = defaultPhone;
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008068 }
8069 VoicemailNotificationSettingsUtil.setRingtoneUri(phone.getContext(), uri);
8070 } finally {
8071 Binder.restoreCallingIdentity(identity);
Ta-wei Yenc33877d2017-01-23 18:11:21 -08008072 }
Ta-wei Yenc33877d2017-01-23 18:11:21 -08008073 }
8074
8075 /**
Nancy Chen31f9ba12016-01-06 11:42:12 -08008076 * Returns whether vibration is set for voicemail notification in Phone settings.
8077 *
8078 * @param accountHandle The handle for the {@link PhoneAccount} for which to retrieve the
8079 * voicemail vibration setting.
8080 * @return {@code true} if the vibration is set for this PhoneAccount, {@code false} otherwise.
8081 */
8082 @Override
8083 public boolean isVoicemailVibrationEnabled(PhoneAccountHandle accountHandle) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008084 final long identity = Binder.clearCallingIdentity();
8085 try {
8086 Phone phone = PhoneUtils.getPhoneForPhoneAccountHandle(accountHandle);
8087 if (phone == null) {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08008088 phone = getDefaultPhone();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008089 }
Nancy Chen31f9ba12016-01-06 11:42:12 -08008090
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008091 return VoicemailNotificationSettingsUtil.isVibrationEnabled(phone.getContext());
8092 } finally {
8093 Binder.restoreCallingIdentity(identity);
8094 }
Nancy Chen31f9ba12016-01-06 11:42:12 -08008095 }
8096
Youhan Wange64578a2016-05-02 15:32:42 -07008097 /**
Ta-wei Yenc33877d2017-01-23 18:11:21 -08008098 * Sets the per-account voicemail vibration.
8099 *
8100 * <p>Requires that the calling app is the default dialer, or has carrier privileges, or
8101 * has permission {@link android.Manifest.permission#MODIFY_PHONE_STATE MODIFY_PHONE_STATE}.
8102 *
8103 * @param phoneAccountHandle The handle for the {@link PhoneAccount} for which to set the
8104 * voicemail vibration setting.
8105 * @param enabled Whether to enable or disable vibration for voicemail notifications from a
8106 * specific PhoneAccount.
8107 */
8108 @Override
8109 public void setVoicemailVibrationEnabled(String callingPackage,
8110 PhoneAccountHandle phoneAccountHandle, boolean enabled) {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08008111 final Phone defaultPhone = getDefaultPhone();
Ta-wei Yenc33877d2017-01-23 18:11:21 -08008112 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
Tyler Gunn5ddfdc92019-10-31 13:08:23 -07008113 TelecomManager tm = defaultPhone.getContext().getSystemService(TelecomManager.class);
8114 if (!TextUtils.equals(callingPackage, tm.getDefaultDialerPackage())) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08008115 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
8116 mApp, PhoneUtils.getSubIdForPhoneAccountHandle(phoneAccountHandle),
8117 "setVoicemailVibrationEnabled");
Ta-wei Yenc33877d2017-01-23 18:11:21 -08008118 }
8119
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008120 final long identity = Binder.clearCallingIdentity();
8121 try {
8122 Phone phone = PhoneUtils.getPhoneForPhoneAccountHandle(phoneAccountHandle);
8123 if (phone == null) {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08008124 phone = defaultPhone;
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008125 }
8126 VoicemailNotificationSettingsUtil.setVibrationEnabled(phone.getContext(), enabled);
8127 } finally {
8128 Binder.restoreCallingIdentity(identity);
Ta-wei Yenc33877d2017-01-23 18:11:21 -08008129 }
Ta-wei Yenc33877d2017-01-23 18:11:21 -08008130 }
8131
8132 /**
Youhan Wange64578a2016-05-02 15:32:42 -07008133 * Make sure either called from same process as self (phone) or IPC caller has read privilege.
8134 *
8135 * @throws SecurityException if the caller does not have the required permission
8136 */
Brad Ebinger35c841c2018-10-01 10:40:55 -07008137 private void enforceReadPrivilegedPermission(String message) {
Youhan Wange64578a2016-05-02 15:32:42 -07008138 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.READ_PRIVILEGED_PHONE_STATE,
Brad Ebinger35c841c2018-10-01 10:40:55 -07008139 message);
Youhan Wange64578a2016-05-02 15:32:42 -07008140 }
8141
8142 /**
Ta-wei Yen30a69c82016-12-27 14:52:32 -08008143 * Make sure either called from same process as self (phone) or IPC caller has send SMS
8144 * permission.
8145 *
8146 * @throws SecurityException if the caller does not have the required permission
8147 */
8148 private void enforceSendSmsPermission() {
8149 mApp.enforceCallingOrSelfPermission(permission.SEND_SMS, null);
8150 }
8151
8152 /**
Ta-wei Yen527a9c02017-01-06 15:29:25 -08008153 * Make sure called from the package in charge of visual voicemail.
Ta-wei Yen30a69c82016-12-27 14:52:32 -08008154 *
Ta-wei Yen527a9c02017-01-06 15:29:25 -08008155 * @throws SecurityException if the caller is not the visual voicemail package.
Ta-wei Yen30a69c82016-12-27 14:52:32 -08008156 */
Ta-wei Yen527a9c02017-01-06 15:29:25 -08008157 private void enforceVisualVoicemailPackage(String callingPackage, int subId) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008158 final long identity = Binder.clearCallingIdentity();
8159 try {
8160 ComponentName componentName =
Nazanin Bakhshif782e562018-12-11 15:15:39 -08008161 RemoteVvmTaskManager.getRemotePackage(mApp, subId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008162 if (componentName == null) {
8163 throw new SecurityException(
8164 "Caller not current active visual voicemail package[null]");
8165 }
8166 String vvmPackage = componentName.getPackageName();
8167 if (!callingPackage.equals(vvmPackage)) {
Hui Wang7f657552022-08-16 16:58:25 +00008168 throw new SecurityException("Caller not current active visual voicemail package");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008169 }
8170 } finally {
8171 Binder.restoreCallingIdentity(identity);
Ta-wei Yen30a69c82016-12-27 14:52:32 -08008172 }
8173 }
8174
8175 /**
Youhan Wange64578a2016-05-02 15:32:42 -07008176 * Return the application ID for the app type.
8177 *
8178 * @param subId the subscription ID that this request applies to.
8179 * @param appType the uicc app type.
8180 * @return Application ID for specificied app type, or null if no uicc.
8181 */
8182 @Override
8183 public String getAidForAppType(int subId, int appType) {
Brad Ebinger35c841c2018-10-01 10:40:55 -07008184 enforceReadPrivilegedPermission("getAidForAppType");
Youhan Wange64578a2016-05-02 15:32:42 -07008185 Phone phone = getPhone(subId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008186
8187 final long identity = Binder.clearCallingIdentity();
Youhan Wange64578a2016-05-02 15:32:42 -07008188 try {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008189 if (phone == null) {
8190 return null;
8191 }
8192 String aid = null;
8193 try {
Muralidhar Reddy472c2ae2021-09-29 19:38:40 +00008194 aid = UiccController.getInstance().getUiccPort(phone.getPhoneId())
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008195 .getApplicationByType(appType).getAid();
8196 } catch (Exception e) {
8197 Log.e(LOG_TAG, "Not getting aid. Exception ex=" + e);
8198 }
8199 return aid;
8200 } finally {
8201 Binder.restoreCallingIdentity(identity);
Youhan Wange64578a2016-05-02 15:32:42 -07008202 }
Youhan Wange64578a2016-05-02 15:32:42 -07008203 }
8204
Youhan Wang4001d252016-05-11 10:29:41 -07008205 /**
8206 * Return the Electronic Serial Number.
8207 *
8208 * @param subId the subscription ID that this request applies to.
8209 * @return ESN or null if error.
8210 */
8211 @Override
8212 public String getEsn(int subId) {
Brad Ebinger35c841c2018-10-01 10:40:55 -07008213 enforceReadPrivilegedPermission("getEsn");
Youhan Wang4001d252016-05-11 10:29:41 -07008214 Phone phone = getPhone(subId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008215
8216 final long identity = Binder.clearCallingIdentity();
Youhan Wang4001d252016-05-11 10:29:41 -07008217 try {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008218 if (phone == null) {
8219 return null;
8220 }
8221 String esn = null;
8222 try {
8223 esn = phone.getEsn();
8224 } catch (Exception e) {
8225 Log.e(LOG_TAG, "Not getting ESN. Exception ex=" + e);
8226 }
8227 return esn;
8228 } finally {
8229 Binder.restoreCallingIdentity(identity);
Youhan Wang4001d252016-05-11 10:29:41 -07008230 }
Youhan Wang4001d252016-05-11 10:29:41 -07008231 }
8232
Sanket Padawe99ef1e32016-05-18 16:12:33 -07008233 /**
Youhan Wang66ad5d72016-07-18 17:56:58 -07008234 * Return the Preferred Roaming List Version.
8235 *
8236 * @param subId the subscription ID that this request applies to.
8237 * @return PRLVersion or null if error.
8238 */
8239 @Override
8240 public String getCdmaPrlVersion(int subId) {
Brad Ebinger35c841c2018-10-01 10:40:55 -07008241 enforceReadPrivilegedPermission("getCdmaPrlVersion");
Youhan Wang66ad5d72016-07-18 17:56:58 -07008242 Phone phone = getPhone(subId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008243
8244 final long identity = Binder.clearCallingIdentity();
Youhan Wang66ad5d72016-07-18 17:56:58 -07008245 try {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008246 if (phone == null) {
8247 return null;
8248 }
8249 String cdmaPrlVersion = null;
8250 try {
8251 cdmaPrlVersion = phone.getCdmaPrlVersion();
8252 } catch (Exception e) {
8253 Log.e(LOG_TAG, "Not getting PRLVersion", e);
8254 }
8255 return cdmaPrlVersion;
8256 } finally {
8257 Binder.restoreCallingIdentity(identity);
Youhan Wang66ad5d72016-07-18 17:56:58 -07008258 }
Youhan Wang66ad5d72016-07-18 17:56:58 -07008259 }
8260
8261 /**
Sanket Padawe99ef1e32016-05-18 16:12:33 -07008262 * Get snapshot of Telephony histograms
8263 * @return List of Telephony histograms
8264 * @hide
8265 */
8266 @Override
8267 public List<TelephonyHistogram> getTelephonyHistograms() {
Jeff Davidson7e17e312018-02-13 18:17:36 -08008268 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
8269 mApp, getDefaultSubscription(), "getTelephonyHistograms");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008270
8271 final long identity = Binder.clearCallingIdentity();
8272 try {
8273 return RIL.getTelephonyRILTimingHistograms();
8274 } finally {
8275 Binder.restoreCallingIdentity(identity);
8276 }
Sanket Padawe99ef1e32016-05-18 16:12:33 -07008277 }
Meng Wang1a7c35a2016-05-05 20:56:15 -07008278
8279 /**
8280 * {@hide}
Michele Berionne482f8202018-11-27 18:57:59 -08008281 * Set the allowed carrier list and the excluded carrier list, indicating the priority between
8282 * the two lists.
Meng Wang1a7c35a2016-05-05 20:56:15 -07008283 * Require system privileges. In the future we may add this to carrier APIs.
8284 *
Michele Berionne482f8202018-11-27 18:57:59 -08008285 * @return Integer with the result of the operation, as defined in {@link TelephonyManager}.
Meng Wang1a7c35a2016-05-05 20:56:15 -07008286 */
8287 @Override
Michele Berionne482f8202018-11-27 18:57:59 -08008288 @TelephonyManager.SetCarrierRestrictionResult
8289 public int setAllowedCarriers(CarrierRestrictionRules carrierRestrictionRules) {
Meng Wang1a7c35a2016-05-05 20:56:15 -07008290 enforceModifyPermission();
vagdeviaf9a5b92018-08-15 16:01:53 -07008291 WorkSource workSource = getWorkSource(Binder.getCallingUid());
Sanket Padawe13bac7b2017-03-20 15:04:47 -07008292
Michele Berionne482f8202018-11-27 18:57:59 -08008293 if (carrierRestrictionRules == null) {
8294 throw new NullPointerException("carrier restriction cannot be null");
Meng Wang9b7c4e92017-02-17 11:41:27 -08008295 }
Sanket Padawe13bac7b2017-03-20 15:04:47 -07008296
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008297 final long identity = Binder.clearCallingIdentity();
8298 try {
Michele Berionne482f8202018-11-27 18:57:59 -08008299 return (int) sendRequest(CMD_SET_ALLOWED_CARRIERS, carrierRestrictionRules,
vagdeviaf9a5b92018-08-15 16:01:53 -07008300 workSource);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008301 } finally {
8302 Binder.restoreCallingIdentity(identity);
8303 }
Meng Wang1a7c35a2016-05-05 20:56:15 -07008304 }
8305
8306 /**
8307 * {@hide}
Michele Berionne482f8202018-11-27 18:57:59 -08008308 * Get the allowed carrier list and the excluded carrier list, including the priority between
8309 * the two lists.
Meng Wang1a7c35a2016-05-05 20:56:15 -07008310 * Require system privileges. In the future we may add this to carrier APIs.
8311 *
Michele Berionne482f8202018-11-27 18:57:59 -08008312 * @return {@link android.telephony.CarrierRestrictionRules}
Meng Wang1a7c35a2016-05-05 20:56:15 -07008313 */
8314 @Override
Michele Berionne482f8202018-11-27 18:57:59 -08008315 public CarrierRestrictionRules getAllowedCarriers() {
Brad Ebinger35c841c2018-10-01 10:40:55 -07008316 enforceReadPrivilegedPermission("getAllowedCarriers");
vagdeviaf9a5b92018-08-15 16:01:53 -07008317 WorkSource workSource = getWorkSource(Binder.getCallingUid());
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008318
8319 final long identity = Binder.clearCallingIdentity();
8320 try {
Michele Berionne482f8202018-11-27 18:57:59 -08008321 Object response = sendRequest(CMD_GET_ALLOWED_CARRIERS, null, workSource);
8322 if (response instanceof CarrierRestrictionRules) {
8323 return (CarrierRestrictionRules) response;
8324 }
8325 // Response is an Exception of some kind,
8326 // which is signalled to the user as a NULL retval
8327 return null;
8328 } catch (Exception e) {
8329 Log.e(LOG_TAG, "getAllowedCarriers. Exception ex=" + e);
8330 return null;
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008331 } finally {
8332 Binder.restoreCallingIdentity(identity);
8333 }
Meng Wang1a7c35a2016-05-05 20:56:15 -07008334 }
8335
fionaxu59545b42016-05-25 15:53:37 -07008336 /**
fionaxu59545b42016-05-25 15:53:37 -07008337 * Action set from carrier signalling broadcast receivers to enable/disable radio
8338 * @param subId the subscription ID that this action applies to.
8339 * @param enabled control enable or disable radio.
8340 * {@hide}
8341 */
8342 @Override
8343 public void carrierActionSetRadioEnabled(int subId, boolean enabled) {
8344 enforceModifyPermission();
8345 final Phone phone = getPhone(subId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008346
8347 final long identity = Binder.clearCallingIdentity();
fionaxu59545b42016-05-25 15:53:37 -07008348 if (phone == null) {
8349 loge("carrierAction: SetRadioEnabled fails with invalid sibId: " + subId);
8350 return;
8351 }
8352 try {
8353 phone.carrierActionSetRadioEnabled(enabled);
8354 } catch (Exception e) {
8355 Log.e(LOG_TAG, "carrierAction: SetRadioEnabled fails. Exception ex=" + e);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008356 } finally {
8357 Binder.restoreCallingIdentity(identity);
fionaxu59545b42016-05-25 15:53:37 -07008358 }
8359 }
8360
Ta-wei Yenc236d6b2016-06-21 13:33:12 -07008361 /**
Sooraj Sasindrandaf060f2021-06-15 14:52:55 -07008362 * Enable or disable Voice over NR (VoNR)
8363 * @param subId the subscription ID that this action applies to.
8364 * @param enabled enable or disable VoNR.
8365 * @return operation result.
8366 */
8367 @Override
8368 public int setVoNrEnabled(int subId, boolean enabled) {
8369 enforceModifyPermission();
8370 final Phone phone = getPhone(subId);
8371
8372 final long identity = Binder.clearCallingIdentity();
8373 if (phone == null) {
8374 loge("setVoNrEnabled fails with no phone object for subId: " + subId);
8375 return TelephonyManager.ENABLE_VONR_RADIO_NOT_AVAILABLE;
8376 }
8377
8378 WorkSource workSource = getWorkSource(Binder.getCallingUid());
8379 try {
8380 int result = (int) sendRequest(CMD_ENABLE_VONR, enabled, subId,
8381 workSource);
8382 if (DBG) log("setVoNrEnabled result: " + result);
Gary Jian8dd305f2021-10-14 16:31:35 +08008383
8384 if (result == TelephonyManager.ENABLE_VONR_SUCCESS) {
8385 if (DBG) {
8386 log("Set VoNR settings in siminfo db; subId=" + subId + ", value:" + enabled);
8387 }
8388 SubscriptionManager.setSubscriptionProperty(
8389 subId, SubscriptionManager.NR_ADVANCED_CALLING_ENABLED,
8390 (enabled ? "1" : "0"));
8391 }
8392
Sooraj Sasindrandaf060f2021-06-15 14:52:55 -07008393 return result;
8394 } finally {
8395 Binder.restoreCallingIdentity(identity);
8396 }
8397 }
8398
8399 /**
8400 * Is voice over NR enabled
8401 * @return true if VoNR is enabled else false
8402 */
8403 @Override
8404 public boolean isVoNrEnabled(int subId) {
8405 enforceReadPrivilegedPermission("isVoNrEnabled");
8406 WorkSource workSource = getWorkSource(Binder.getCallingUid());
8407 final long identity = Binder.clearCallingIdentity();
8408 try {
8409 boolean isEnabled = (boolean) sendRequest(CMD_IS_VONR_ENABLED,
8410 null, subId, workSource);
8411 if (DBG) log("isVoNrEnabled: " + isEnabled);
8412 return isEnabled;
8413 } finally {
8414 Binder.restoreCallingIdentity(identity);
8415 }
8416 }
8417
8418 /**
fionaxu8da9cb12017-05-23 15:02:46 -07008419 * Action set from carrier signalling broadcast receivers to start/stop reporting the default
8420 * network status based on which carrier apps could apply actions accordingly,
8421 * enable/disable default url handler for example.
8422 *
8423 * @param subId the subscription ID that this action applies to.
8424 * @param report control start/stop reporting the default network status.
8425 * {@hide}
8426 */
8427 @Override
8428 public void carrierActionReportDefaultNetworkStatus(int subId, boolean report) {
8429 enforceModifyPermission();
8430 final Phone phone = getPhone(subId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008431
8432 final long identity = Binder.clearCallingIdentity();
fionaxu8da9cb12017-05-23 15:02:46 -07008433 if (phone == null) {
8434 loge("carrierAction: ReportDefaultNetworkStatus fails with invalid sibId: " + subId);
8435 return;
8436 }
8437 try {
8438 phone.carrierActionReportDefaultNetworkStatus(report);
8439 } catch (Exception e) {
8440 Log.e(LOG_TAG, "carrierAction: ReportDefaultNetworkStatus fails. Exception ex=" + e);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008441 } finally {
8442 Binder.restoreCallingIdentity(identity);
fionaxu8da9cb12017-05-23 15:02:46 -07008443 }
8444 }
8445
8446 /**
fionaxud9622282017-07-17 17:51:30 -07008447 * Action set from carrier signalling broadcast receivers to reset all carrier actions
8448 * @param subId the subscription ID that this action applies to.
8449 * {@hide}
8450 */
8451 @Override
8452 public void carrierActionResetAll(int subId) {
8453 enforceModifyPermission();
8454 final Phone phone = getPhone(subId);
8455 if (phone == null) {
8456 loge("carrierAction: ResetAll fails with invalid sibId: " + subId);
8457 return;
8458 }
8459 try {
8460 phone.carrierActionResetAll();
8461 } catch (Exception e) {
8462 Log.e(LOG_TAG, "carrierAction: ResetAll fails. Exception ex=" + e);
8463 }
8464 }
8465
8466 /**
Ta-wei Yenc236d6b2016-06-21 13:33:12 -07008467 * Called when "adb shell dumpsys phone" is invoked. Dump is also automatically invoked when a
8468 * bug report is being generated.
8469 */
8470 @Override
Ta-wei Yen99282e02016-06-21 18:19:35 -07008471 protected void dump(FileDescriptor fd, PrintWriter writer, String[] args) {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08008472 if (mApp.checkCallingOrSelfPermission(android.Manifest.permission.DUMP)
8473 != PackageManager.PERMISSION_GRANTED) {
dcashman22b950d2016-06-27 11:39:02 -07008474 writer.println("Permission Denial: can't dump Phone from pid="
8475 + Binder.getCallingPid()
8476 + ", uid=" + Binder.getCallingUid()
8477 + "without permission "
8478 + android.Manifest.permission.DUMP);
8479 return;
8480 }
Nazanin Bakhshif782e562018-12-11 15:15:39 -08008481 DumpsysHandler.dump(mApp, fd, writer, args);
Ta-wei Yenc236d6b2016-06-21 13:33:12 -07008482 }
Jack Yueb89b242016-06-22 13:27:47 -07008483
Brad Ebingerdac2f002018-04-03 15:17:52 -07008484 @Override
Hall Liua1548bd2019-12-24 14:14:12 -08008485 public int handleShellCommand(@NonNull ParcelFileDescriptor in,
8486 @NonNull ParcelFileDescriptor out, @NonNull ParcelFileDescriptor err,
8487 @NonNull String[] args) {
8488 return new TelephonyShellCommand(this, getDefaultPhone().getContext()).exec(
8489 this, in.getFileDescriptor(), out.getFileDescriptor(),
8490 err.getFileDescriptor(), args);
Brad Ebingerdac2f002018-04-03 15:17:52 -07008491 }
8492
Jack Yueb89b242016-06-22 13:27:47 -07008493 /**
Sooraj Sasindranaf1b5132020-05-05 21:06:20 +00008494 * Policy control of data connection with reason {@@TelephonyManager.DataEnabledReason}
Greg Kaiser17f41752020-05-05 16:47:47 +00008495 * @param subId Subscription index
Sarah Chinecc78c42022-03-31 21:16:48 -07008496 * @param reason The reason the data enable change is taking place.
Sooraj Sasindranaf1b5132020-05-05 21:06:20 +00008497 * @param enabled True if enabling the data, otherwise disabling.
Sarah Chinecc78c42022-03-31 21:16:48 -07008498 * @param callingPackage The package that changed the data enabled state.
Sooraj Sasindranaf1b5132020-05-05 21:06:20 +00008499 * @hide
Jack Yu75ab2952016-07-08 14:29:33 -07008500 */
8501 @Override
Sooraj Sasindran95c07a92020-07-15 01:35:24 -07008502 public void setDataEnabledForReason(int subId, @TelephonyManager.DataEnabledReason int reason,
Sarah Chinecc78c42022-03-31 21:16:48 -07008503 boolean enabled, String callingPackage) {
Sooraj Sasindranaf1b5132020-05-05 21:06:20 +00008504 if (reason == TelephonyManager.DATA_ENABLED_REASON_USER
8505 || reason == TelephonyManager.DATA_ENABLED_REASON_CARRIER) {
8506 try {
8507 TelephonyPermissions.enforceCallingOrSelfCarrierPrivilege(
Sooraj Sasindran95c07a92020-07-15 01:35:24 -07008508 mApp, subId, "setDataEnabledForReason");
Sooraj Sasindranaf1b5132020-05-05 21:06:20 +00008509 } catch (SecurityException se) {
8510 enforceModifyPermission();
8511 }
8512 } else {
8513 enforceModifyPermission();
8514 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008515
8516 final long identity = Binder.clearCallingIdentity();
8517 try {
8518 Phone phone = getPhone(subId);
8519 if (phone != null) {
Sooraj Sasindranaf1b5132020-05-05 21:06:20 +00008520 if (reason == TelephonyManager.DATA_ENABLED_REASON_CARRIER) {
8521 phone.carrierActionSetMeteredApnsEnabled(enabled);
8522 } else {
Jack Yuabb10902022-07-31 00:43:21 -07008523 phone.getDataSettingsManager().setDataEnabled(
8524 reason, enabled, callingPackage);
Sooraj Sasindranaf1b5132020-05-05 21:06:20 +00008525 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008526 }
8527 } finally {
8528 Binder.restoreCallingIdentity(identity);
Jack Yu75ab2952016-07-08 14:29:33 -07008529 }
8530 }
Sooraj Sasindran9a909312016-07-18 11:57:25 -07008531
8532 /**
8533 * Get Client request stats
8534 * @return List of Client Request Stats
8535 * @hide
8536 */
8537 @Override
Philip P. Moltmann700a9592019-10-03 11:53:50 -07008538 public List<ClientRequestStats> getClientRequestStats(String callingPackage,
8539 String callingFeatureId, int subId) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08008540 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
Philip P. Moltmann700a9592019-10-03 11:53:50 -07008541 mApp, subId, callingPackage, callingFeatureId, "getClientRequestStats")) {
Sooraj Sasindran9a909312016-07-18 11:57:25 -07008542 return null;
8543 }
Sooraj Sasindran9a909312016-07-18 11:57:25 -07008544 Phone phone = getPhone(subId);
Sooraj Sasindran9a909312016-07-18 11:57:25 -07008545
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008546 final long identity = Binder.clearCallingIdentity();
8547 try {
8548 if (phone != null) {
8549 return phone.getClientRequestStats();
8550 }
8551
8552 return null;
8553 } finally {
8554 Binder.restoreCallingIdentity(identity);
8555 }
Sooraj Sasindran9a909312016-07-18 11:57:25 -07008556 }
8557
Narayan Kamathf04b5a12018-01-09 11:47:15 +00008558 private WorkSource getWorkSource(int uid) {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08008559 String packageName = mApp.getPackageManager().getNameForUid(uid);
Hunter Knepshieldd03383b2022-03-29 22:47:54 +00008560 if (uid == Process.ROOT_UID && packageName == null) {
8561 // Downstream WorkSource attribution inside the RIL requires both a UID and package name
8562 // to be set for wakelock tracking, otherwise RIL requests fail with a runtime
8563 // exception. ROOT_UID seems not to have a valid package name returned by
8564 // PackageManager, so just fake it here to avoid issues when running telephony shell
8565 // commands that plumb through the RIL as root, like so:
8566 // $ adb root
8567 // $ adb shell cmd phone ...
8568 packageName = "root";
8569 }
Narayan Kamathf04b5a12018-01-09 11:47:15 +00008570 return new WorkSource(uid, packageName);
Sooraj Sasindran9a909312016-07-18 11:57:25 -07008571 }
Jack Yueb4124c2017-02-16 15:32:43 -08008572
8573 /**
Grace Chen70990072017-03-24 17:21:30 -07008574 * Set SIM card power state.
Jack Yueb4124c2017-02-16 15:32:43 -08008575 *
Sanket Padawe13bac7b2017-03-20 15:04:47 -07008576 * @param slotIndex SIM slot id.
Grace Chen70990072017-03-24 17:21:30 -07008577 * @param state State of SIM (power down, power up, pass through)
8578 * - {@link android.telephony.TelephonyManager#CARD_POWER_DOWN}
8579 * - {@link android.telephony.TelephonyManager#CARD_POWER_UP}
8580 * - {@link android.telephony.TelephonyManager#CARD_POWER_UP_PASS_THROUGH}
Jack Yueb4124c2017-02-16 15:32:43 -08008581 *
8582 **/
8583 @Override
Grace Chen70990072017-03-24 17:21:30 -07008584 public void setSimPowerStateForSlot(int slotIndex, int state) {
Jack Yueb4124c2017-02-16 15:32:43 -08008585 enforceModifyPermission();
Sanket Padawe13bac7b2017-03-20 15:04:47 -07008586 Phone phone = PhoneFactory.getPhone(slotIndex);
8587
vagdeviaf9a5b92018-08-15 16:01:53 -07008588 WorkSource workSource = getWorkSource(Binder.getCallingUid());
8589
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008590 final long identity = Binder.clearCallingIdentity();
8591 try {
8592 if (phone != null) {
Jordan Liu109698e2020-11-24 14:50:34 -08008593 phone.setSimPowerState(state, null, workSource);
8594 }
8595 } finally {
8596 Binder.restoreCallingIdentity(identity);
8597 }
8598 }
8599
8600 /**
8601 * Set SIM card power state.
8602 *
8603 * @param slotIndex SIM slot id.
8604 * @param state State of SIM (power down, power up, pass through)
8605 * @param callback callback to trigger after success or failure
8606 * - {@link android.telephony.TelephonyManager#CARD_POWER_DOWN}
8607 * - {@link android.telephony.TelephonyManager#CARD_POWER_UP}
8608 * - {@link android.telephony.TelephonyManager#CARD_POWER_UP_PASS_THROUGH}
8609 *
8610 **/
8611 @Override
8612 public void setSimPowerStateForSlotWithCallback(int slotIndex, int state,
8613 IIntegerConsumer callback) {
8614 enforceModifyPermission();
8615 Phone phone = PhoneFactory.getPhone(slotIndex);
8616
8617 WorkSource workSource = getWorkSource(Binder.getCallingUid());
8618
8619 final long identity = Binder.clearCallingIdentity();
8620 try {
8621 if (phone != null) {
8622 Pair<Integer, IIntegerConsumer> arguments = Pair.create(state, callback);
8623 sendRequestAsync(CMD_SET_SIM_POWER, arguments, phone, workSource);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008624 }
8625 } finally {
8626 Binder.restoreCallingIdentity(identity);
Jack Yueb4124c2017-02-16 15:32:43 -08008627 }
8628 }
Shuo Qiandd210312017-04-12 22:11:33 +00008629
Tyler Gunn65d45c22017-06-05 11:22:26 -07008630 private boolean isUssdApiAllowed(int subId) {
8631 CarrierConfigManager configManager =
Nazanin Bakhshif782e562018-12-11 15:15:39 -08008632 (CarrierConfigManager) mApp.getSystemService(Context.CARRIER_CONFIG_SERVICE);
Tyler Gunn65d45c22017-06-05 11:22:26 -07008633 if (configManager == null) {
8634 return false;
8635 }
8636 PersistableBundle pb = configManager.getConfigForSubId(subId);
8637 if (pb == null) {
8638 return false;
8639 }
8640 return pb.getBoolean(
8641 CarrierConfigManager.KEY_ALLOW_USSD_REQUESTS_VIA_TELEPHONY_MANAGER_BOOL);
8642 }
8643
Shuo Qiandd210312017-04-12 22:11:33 +00008644 /**
8645 * Check if phone is in emergency callback mode
8646 * @return true if phone is in emergency callback mode
8647 * @param subId sub id
8648 */
goneil9c5f4872017-12-05 14:07:56 -08008649 @Override
Shuo Qiandd210312017-04-12 22:11:33 +00008650 public boolean getEmergencyCallbackMode(int subId) {
Brad Ebinger35c841c2018-10-01 10:40:55 -07008651 enforceReadPrivilegedPermission("getEmergencyCallbackMode");
Shuo Qiandd210312017-04-12 22:11:33 +00008652 final Phone phone = getPhone(subId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008653
8654 final long identity = Binder.clearCallingIdentity();
8655 try {
8656 if (phone != null) {
8657 return phone.isInEcm();
8658 } else {
8659 return false;
8660 }
8661 } finally {
8662 Binder.restoreCallingIdentity(identity);
Shuo Qiandd210312017-04-12 22:11:33 +00008663 }
8664 }
Nathan Harold46b42aa2017-03-10 19:38:22 -08008665
8666 /**
8667 * Get the current signal strength information for the given subscription.
8668 * Because this information is not updated when the device is in a low power state
8669 * it should not be relied-upon to be current.
8670 * @param subId Subscription index
8671 * @return the most recent cached signal strength info from the modem
8672 */
8673 @Override
8674 public SignalStrength getSignalStrength(int subId) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008675 final long identity = Binder.clearCallingIdentity();
8676 try {
8677 Phone p = getPhone(subId);
8678 if (p == null) {
8679 return null;
8680 }
Nathan Harold46b42aa2017-03-10 19:38:22 -08008681
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008682 return p.getSignalStrength();
8683 } finally {
8684 Binder.restoreCallingIdentity(identity);
8685 }
Nathan Harold46b42aa2017-03-10 19:38:22 -08008686 }
Holly Jiuyu Sun01c47ad2018-01-24 17:56:33 +00008687
Pengquan Meng77b7f132018-08-22 14:49:57 -07008688 /**
Chen Xuf792fd62018-10-17 17:54:36 +00008689 * Get the current modem radio state for the given slot.
8690 * @param slotIndex slot index.
8691 * @param callingPackage the name of the package making the call.
Philip P. Moltmann700a9592019-10-03 11:53:50 -07008692 * @param callingFeatureId The feature in the package.
Chen Xuf792fd62018-10-17 17:54:36 +00008693 * @return the current radio power state from the modem
8694 */
8695 @Override
Philip P. Moltmann700a9592019-10-03 11:53:50 -07008696 public int getRadioPowerState(int slotIndex, String callingPackage, String callingFeatureId) {
Chen Xuf792fd62018-10-17 17:54:36 +00008697 Phone phone = PhoneFactory.getPhone(slotIndex);
8698 if (phone != null) {
Philip P. Moltmann700a9592019-10-03 11:53:50 -07008699 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(mApp, phone.getSubId(),
8700 callingPackage, callingFeatureId, "getRadioPowerState")) {
Chen Xuf792fd62018-10-17 17:54:36 +00008701 return TelephonyManager.RADIO_POWER_UNAVAILABLE;
8702 }
8703
8704 final long identity = Binder.clearCallingIdentity();
8705 try {
8706 return phone.getRadioPowerState();
8707 } finally {
8708 Binder.restoreCallingIdentity(identity);
8709 }
8710 }
8711 return TelephonyManager.RADIO_POWER_UNAVAILABLE;
8712 }
8713
8714 /**
Pengquan Meng77b7f132018-08-22 14:49:57 -07008715 * Checks if data roaming is enabled on the subscription with id {@code subId}.
8716 *
8717 * <p>Requires one of the following permissions:
8718 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE},
Sooraj Sasindran5d051bf2021-11-05 13:14:15 -07008719 * {@link android.Manifest.permission#READ_BASIC_PHONE_STATE},
Pengquan Meng77b7f132018-08-22 14:49:57 -07008720 * {@link android.Manifest.permission#READ_PHONE_STATE} or that the calling app has carrier
8721 * privileges.
8722 *
8723 * @param subId subscription id
8724 * @return {@code true} if data roaming is enabled on this subscription, otherwise return
8725 * {@code false}.
8726 */
8727 @Override
8728 public boolean isDataRoamingEnabled(int subId) {
Sooraj Sasindran5d051bf2021-11-05 13:14:15 -07008729 String functionName = "isDataRoamingEnabled";
Shuo Qian093013d2020-08-13 15:42:55 -07008730 try {
Sooraj Sasindran5d051bf2021-11-05 13:14:15 -07008731 try {
8732 mApp.enforceCallingOrSelfPermission(
8733 android.Manifest.permission.ACCESS_NETWORK_STATE,
8734 functionName);
8735 } catch (Exception e) {
8736 mApp.enforceCallingOrSelfPermission(
8737 permission.READ_BASIC_PHONE_STATE, functionName);
8738 }
Shuo Qian093013d2020-08-13 15:42:55 -07008739 } catch (Exception e) {
Nathan Harold62c68512021-04-06 11:26:02 -07008740 TelephonyPermissions.enforceCallingOrSelfReadPhoneStatePermissionOrCarrierPrivilege(
Sooraj Sasindran5d051bf2021-11-05 13:14:15 -07008741 mApp, subId, functionName);
Shuo Qian093013d2020-08-13 15:42:55 -07008742 }
Pengquan Meng44e66f12019-04-01 10:48:20 -07008743
Pengquan Menga1bb6272018-09-06 09:59:22 -07008744 boolean isEnabled = false;
8745 final long identity = Binder.clearCallingIdentity();
Pengquan Meng77b7f132018-08-22 14:49:57 -07008746 try {
Pengquan Menga1bb6272018-09-06 09:59:22 -07008747 Phone phone = getPhone(subId);
8748 isEnabled = phone != null ? phone.getDataRoamingEnabled() : false;
Pengquan Menga1bb6272018-09-06 09:59:22 -07008749 } finally {
8750 Binder.restoreCallingIdentity(identity);
Pengquan Meng77b7f132018-08-22 14:49:57 -07008751 }
Pengquan Menga1bb6272018-09-06 09:59:22 -07008752 return isEnabled;
Pengquan Meng77b7f132018-08-22 14:49:57 -07008753 }
8754
8755
8756 /**
8757 * Enables/Disables the data roaming on the subscription with id {@code subId}.
8758 *
8759 * <p> Requires permission:
8760 * {@link android.Manifest.permission#MODIFY_PHONE_STATE} or that the calling app has carrier
8761 * privileges.
8762 *
8763 * @param subId subscription id
8764 * @param isEnabled {@code true} means enable, {@code false} means disable.
8765 */
8766 @Override
8767 public void setDataRoamingEnabled(int subId, boolean isEnabled) {
Pengquan Meng44e66f12019-04-01 10:48:20 -07008768 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
8769 mApp, subId, "setDataRoamingEnabled");
8770
Pengquan Menga1bb6272018-09-06 09:59:22 -07008771 final long identity = Binder.clearCallingIdentity();
8772 try {
Pengquan Menga1bb6272018-09-06 09:59:22 -07008773 Phone phone = getPhone(subId);
8774 if (phone != null) {
8775 phone.setDataRoamingEnabled(isEnabled);
8776 }
8777 } finally {
8778 Binder.restoreCallingIdentity(identity);
Pengquan Meng77b7f132018-08-22 14:49:57 -07008779 }
8780 }
8781
Holly Jiuyu Sun01c47ad2018-01-24 17:56:33 +00008782 @Override
Pengquan Meng6884a2c2018-10-03 12:19:13 -07008783 public boolean isManualNetworkSelectionAllowed(int subId) {
tom hsuc91afc72020-01-06 23:46:07 +08008784 TelephonyPermissions
Nathan Harold62c68512021-04-06 11:26:02 -07008785 .enforceCallingOrSelfReadPrivilegedPhoneStatePermissionOrCarrierPrivilege(
Pengquan Meng44e66f12019-04-01 10:48:20 -07008786 mApp, subId, "isManualNetworkSelectionAllowed");
8787
Pengquan Meng6884a2c2018-10-03 12:19:13 -07008788 boolean isAllowed = true;
8789 final long identity = Binder.clearCallingIdentity();
8790 try {
Pengquan Meng6884a2c2018-10-03 12:19:13 -07008791 Phone phone = getPhone(subId);
8792 if (phone != null) {
8793 isAllowed = phone.isCspPlmnEnabled();
8794 }
8795 } finally {
8796 Binder.restoreCallingIdentity(identity);
8797 }
8798 return isAllowed;
8799 }
8800
Muralidhar Reddyeb809e32021-11-19 03:07:54 +00008801 private boolean haveCarrierPrivilegeAccess(UiccPort port, String callingPackage) {
8802 UiccProfile profile = port.getUiccProfile();
Hunter Knepshieldcad7f0b2021-12-13 15:08:35 -08008803 if (profile == null) {
Muralidhar Reddyeb809e32021-11-19 03:07:54 +00008804 return false;
Muralidhar Reddy472c2ae2021-09-29 19:38:40 +00008805 }
Hunter Knepshieldcad7f0b2021-12-13 15:08:35 -08008806 Phone phone = PhoneFactory.getPhone(profile.getPhoneId());
8807 if (phone == null) {
8808 return false;
8809 }
8810 CarrierPrivilegesTracker cpt = phone.getCarrierPrivilegesTracker();
8811 return cpt != null && cpt.getCarrierPrivilegeStatusForPackage(callingPackage)
8812 == TelephonyManager.CARRIER_PRIVILEGE_STATUS_HAS_ACCESS;
Muralidhar Reddy472c2ae2021-09-29 19:38:40 +00008813 }
8814
Pengquan Meng6884a2c2018-10-03 12:19:13 -07008815 @Override
Jordan Liu75f43ea2019-01-17 16:56:37 -08008816 public List<UiccCardInfo> getUiccCardsInfo(String callingPackage) {
sandeepjsa208e3b2021-11-17 04:05:58 +00008817 // Verify that the callingPackage belongs to the calling UID
Jordan Liu4cda4552020-03-23 11:55:07 -07008818 mApp.getSystemService(AppOpsManager.class)
8819 .checkPackage(Binder.getCallingUid(), callingPackage);
8820
Jordan Liu1e142fc2019-04-22 15:10:43 -07008821 boolean hasReadPermission = false;
sandeepjsb6c87872021-09-27 15:34:44 +00008822 boolean isIccIdAccessRestricted = false;
Jordan Liuc65bc952019-02-12 17:54:02 -08008823 try {
8824 enforceReadPrivilegedPermission("getUiccCardsInfo");
Jordan Liu1e142fc2019-04-22 15:10:43 -07008825 hasReadPermission = true;
Jordan Liuc65bc952019-02-12 17:54:02 -08008826 } catch (SecurityException e) {
8827 // even without READ_PRIVILEGED_PHONE_STATE, we allow the call to continue if the caller
8828 // has carrier privileges on an active UICC
Rambo Wange7209ce2022-02-23 13:41:02 -08008829 if (checkCarrierPrivilegesForPackageAnyPhoneWithPermission(callingPackage)
Jordan Liuc65bc952019-02-12 17:54:02 -08008830 != TelephonyManager.CARRIER_PRIVILEGE_STATUS_HAS_ACCESS) {
Jordan Liu1e142fc2019-04-22 15:10:43 -07008831 throw new SecurityException("Caller does not have permission.");
Jordan Liuc65bc952019-02-12 17:54:02 -08008832 }
Jordan Liu75f43ea2019-01-17 16:56:37 -08008833 }
sandeepjsb6c87872021-09-27 15:34:44 +00008834 // checking compatibility, if calling app's target SDK is T and beyond.
8835 if (CompatChanges.isChangeEnabled(GET_API_SIGNATURES_FROM_UICC_PORT_INFO,
8836 Binder.getCallingUid())) {
8837 isIccIdAccessRestricted = true;
8838 }
Jordan Liu5aa07002018-12-18 15:44:48 -08008839 final long identity = Binder.clearCallingIdentity();
8840 try {
Jordan Liu75f43ea2019-01-17 16:56:37 -08008841 UiccController uiccController = UiccController.getInstance();
8842 ArrayList<UiccCardInfo> cardInfos = uiccController.getAllUiccCardInfos();
Jordan Liu1e142fc2019-04-22 15:10:43 -07008843 if (hasReadPermission) {
8844 return cardInfos;
Jordan Liu75f43ea2019-01-17 16:56:37 -08008845 }
Jordan Liu1e142fc2019-04-22 15:10:43 -07008846
8847 // Remove private info if the caller doesn't have access
8848 ArrayList<UiccCardInfo> filteredInfos = new ArrayList<>();
8849 for (UiccCardInfo cardInfo : cardInfos) {
sandeepjsb6c87872021-09-27 15:34:44 +00008850 //setting the value after compatibility check
8851 cardInfo.setIccIdAccessRestricted(isIccIdAccessRestricted);
Jordan Liu1e142fc2019-04-22 15:10:43 -07008852 // For an inactive eUICC, the UiccCard will be null even though the UiccCardInfo
8853 // is available
sandeepjsb6c87872021-09-27 15:34:44 +00008854 UiccCard card = uiccController.getUiccCardForSlot(cardInfo.getPhysicalSlotIndex());
Muralidhar Reddyeb809e32021-11-19 03:07:54 +00008855 if (card == null) {
8856 // assume no access if the card is unavailable
sandeepjsb6c87872021-09-27 15:34:44 +00008857 filteredInfos.add(getUiccCardInfoUnPrivileged(cardInfo));
Jordan Liu1e142fc2019-04-22 15:10:43 -07008858 continue;
8859 }
Muralidhar Reddyeb809e32021-11-19 03:07:54 +00008860 Collection<UiccPortInfo> portInfos = cardInfo.getPorts();
8861 if (portInfos.isEmpty()) {
sandeepjsb6c87872021-09-27 15:34:44 +00008862 filteredInfos.add(getUiccCardInfoUnPrivileged(cardInfo));
Muralidhar Reddyeb809e32021-11-19 03:07:54 +00008863 continue;
Jordan Liu1e142fc2019-04-22 15:10:43 -07008864 }
Muralidhar Reddyeb809e32021-11-19 03:07:54 +00008865 List<UiccPortInfo> uiccPortInfos = new ArrayList<>();
8866 for (UiccPortInfo portInfo : portInfos) {
8867 UiccPort port = uiccController.getUiccPortForSlot(
8868 cardInfo.getPhysicalSlotIndex(), portInfo.getPortIndex());
8869 if (port == null) {
8870 // assume no access if port is null
8871 uiccPortInfos.add(getUiccPortInfoUnPrivileged(portInfo));
8872 continue;
8873 }
8874 if (haveCarrierPrivilegeAccess(port, callingPackage)) {
8875 uiccPortInfos.add(portInfo);
8876 } else {
8877 uiccPortInfos.add(getUiccPortInfoUnPrivileged(portInfo));
8878 }
8879 }
8880 filteredInfos.add(new UiccCardInfo(
8881 cardInfo.isEuicc(),
8882 cardInfo.getCardId(),
8883 null,
8884 cardInfo.getPhysicalSlotIndex(),
8885 cardInfo.isRemovable(),
8886 cardInfo.isMultipleEnabledProfilesSupported(),
8887 uiccPortInfos));
Jordan Liu1e142fc2019-04-22 15:10:43 -07008888 }
8889 return filteredInfos;
Jordan Liu5aa07002018-12-18 15:44:48 -08008890 } finally {
8891 Binder.restoreCallingIdentity(identity);
8892 }
8893 }
8894
sandeepjsb6c87872021-09-27 15:34:44 +00008895 /**
8896 * Returns a copy of the UiccCardinfo with the EID and ICCID set to null. These values are
8897 * generally private and require carrier privileges to view.
8898 *
8899 * @hide
8900 */
8901 @NonNull
8902 public UiccCardInfo getUiccCardInfoUnPrivileged(UiccCardInfo cardInfo) {
8903 List<UiccPortInfo> portinfo = new ArrayList<>();
8904 for (UiccPortInfo portinfos : cardInfo.getPorts()) {
8905 portinfo.add(getUiccPortInfoUnPrivileged(portinfos));
8906 }
8907 return new UiccCardInfo(
8908 cardInfo.isEuicc(),
8909 cardInfo.getCardId(),
8910 null,
8911 cardInfo.getPhysicalSlotIndex(),
8912 cardInfo.isRemovable(),
8913 cardInfo.isMultipleEnabledProfilesSupported(),
8914 portinfo
8915 );
8916 }
Holly Jiuyu Sun01c47ad2018-01-24 17:56:33 +00008917
sandeepjsb6c87872021-09-27 15:34:44 +00008918 /**
8919 * @hide
8920 * @return a copy of the UiccPortInfo with ICCID set to {@link UiccPortInfo#ICCID_REDACTED}.
8921 * These values are generally private and require carrier privileges to view.
8922 */
8923 @NonNull
8924 public UiccPortInfo getUiccPortInfoUnPrivileged(UiccPortInfo portInfo) {
8925 return new UiccPortInfo(
8926 UiccPortInfo.ICCID_REDACTED,
8927 portInfo.getPortIndex(),
8928 portInfo.getLogicalSlotIndex(),
8929 portInfo.isActive()
8930 );
8931 }
8932 @Override
8933 public UiccSlotInfo[] getUiccSlotsInfo(String callingPackage) {
sandeepjsa208e3b2021-11-17 04:05:58 +00008934 // Verify that the callingPackage belongs to the calling UID
sandeepjsb6c87872021-09-27 15:34:44 +00008935 mApp.getSystemService(AppOpsManager.class)
8936 .checkPackage(Binder.getCallingUid(), callingPackage);
8937
sandeepjsb6c87872021-09-27 15:34:44 +00008938 boolean isLogicalSlotAccessRestricted = false;
sandeepjsb6c87872021-09-27 15:34:44 +00008939
Aman Guptaf3c90b32022-03-17 04:54:16 +00008940 // This will make sure caller has the READ_PRIVILEGED_PHONE_STATE. Do not remove this as
8941 // we are reading iccId which is PII data.
8942 enforceReadPrivilegedPermission("getUiccSlotsInfo");
sandeepjsb6c87872021-09-27 15:34:44 +00008943
8944 // checking compatibility, if calling app's target SDK is T and beyond.
8945 if (CompatChanges.isChangeEnabled(GET_API_SIGNATURES_FROM_UICC_PORT_INFO,
8946 Binder.getCallingUid())) {
8947 isLogicalSlotAccessRestricted = true;
8948 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008949 final long identity = Binder.clearCallingIdentity();
8950 try {
8951 UiccSlot[] slots = UiccController.getInstance().getUiccSlots();
Muralidhar Reddyd196bbf2022-01-17 17:56:30 +00008952 if (slots == null || slots.length == 0) {
8953 Rlog.i(LOG_TAG, "slots is null or empty.");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008954 return null;
8955 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008956 UiccSlotInfo[] infos = new UiccSlotInfo[slots.length];
8957 for (int i = 0; i < slots.length; i++) {
8958 UiccSlot slot = slots[i];
8959 if (slot == null) {
8960 continue;
8961 }
8962
Jordan Liu7be7e652019-05-06 18:55:02 +00008963 String cardId;
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008964 UiccCard card = slot.getUiccCard();
8965 if (card != null) {
8966 cardId = card.getCardId();
Jordan Liu7be7e652019-05-06 18:55:02 +00008967 } else {
Jordan Liu01bd00d2019-09-12 16:19:43 -07008968 cardId = slot.getEid();
8969 if (TextUtils.isEmpty(cardId)) {
Aman Guptaf3c90b32022-03-17 04:54:16 +00008970 // If cardId is null, use iccId of default port as cardId.
8971 cardId = slot.getIccId(TelephonyManager.DEFAULT_PORT_INDEX);
Jordan Liu01bd00d2019-09-12 16:19:43 -07008972 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008973 }
8974
Jordan Liu857451f2019-05-09 16:35:35 -07008975 if (cardId != null) {
8976 // if cardId is an ICCID, strip off trailing Fs before exposing to user
8977 // if cardId is an EID, it's all digits so this is fine
8978 cardId = IccUtils.stripTrailingFs(cardId);
8979 }
8980
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008981 int cardState = 0;
8982 switch (slot.getCardState()) {
8983 case CARDSTATE_ABSENT:
8984 cardState = UiccSlotInfo.CARD_STATE_INFO_ABSENT;
8985 break;
8986 case CARDSTATE_PRESENT:
8987 cardState = UiccSlotInfo.CARD_STATE_INFO_PRESENT;
8988 break;
8989 case CARDSTATE_ERROR:
8990 cardState = UiccSlotInfo.CARD_STATE_INFO_ERROR;
8991 break;
8992 case CARDSTATE_RESTRICTED:
8993 cardState = UiccSlotInfo.CARD_STATE_INFO_RESTRICTED;
8994 break;
8995 default:
8996 break;
8997
8998 }
Muralidhar Reddyeb809e32021-11-19 03:07:54 +00008999 List<UiccPortInfo> portInfos = new ArrayList<>();
9000 int[] portIndexes = slot.getPortList();
9001 for (int portIdx : portIndexes) {
9002 String iccId = IccUtils.stripTrailingFs(getIccId(slot, portIdx,
Aman Guptaf3c90b32022-03-17 04:54:16 +00009003 callingPackage, /* hasReadPermission= */ true));
Muralidhar Reddyfbcff0c2022-01-19 13:07:57 +00009004 portInfos.add(new UiccPortInfo(iccId, portIdx,
9005 slot.getPhoneIdFromPortIndex(portIdx), slot.isPortActive(portIdx)));
Muralidhar Reddyeb809e32021-11-19 03:07:54 +00009006 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08009007 infos[i] = new UiccSlotInfo(
Malcolm Chenaa4a8532018-02-28 15:00:40 -08009008 slot.isEuicc(),
9009 cardId,
9010 cardState,
Jordan Liua2619582019-02-14 12:56:40 -08009011 slot.isExtendedApduSupported(),
Muralidhar Reddyeb809e32021-11-19 03:07:54 +00009012 slot.isRemovable(), portInfos);
sandeepjsb6c87872021-09-27 15:34:44 +00009013 //setting the value after compatibility check
9014 infos[i].setLogicalSlotAccessRestricted(isLogicalSlotAccessRestricted);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08009015 }
9016 return infos;
9017 } finally {
9018 Binder.restoreCallingIdentity(identity);
Holly Jiuyu Sun1d957c52018-04-04 13:52:42 -07009019 }
Holly Jiuyu Sun01c47ad2018-01-24 17:56:33 +00009020 }
9021
Muralidhar Reddyeb809e32021-11-19 03:07:54 +00009022 /* Returns null if doesn't have read permission or carrier privilege access. */
9023 private String getIccId(UiccSlot slot, int portIndex, String callingPackage,
9024 boolean hasReadPermission) {
9025 String iccId = slot.getIccId(portIndex);
9026 if (hasReadPermission) { // if has read permission
9027 return iccId;
9028 } else {
9029 if (slot.getUiccCard() != null && slot.getUiccCard().getUiccPort(portIndex) != null) {
9030 UiccPort port = slot.getUiccCard().getUiccPort(portIndex);
9031 // if no read permission, checking carrier privilege access
9032 if (haveCarrierPrivilegeAccess(port, callingPackage)) {
9033 return iccId;
9034 }
9035 }
9036 }
9037 // No read permission or carrier privilege access.
9038 return UiccPortInfo.ICCID_REDACTED;
9039 }
9040
Holly Jiuyu Sun01c47ad2018-01-24 17:56:33 +00009041 @Override
sandeepjsb6c87872021-09-27 15:34:44 +00009042 @Deprecated
Holly Jiuyu Sun01c47ad2018-01-24 17:56:33 +00009043 public boolean switchSlots(int[] physicalSlots) {
9044 enforceModifyPermission();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08009045
9046 final long identity = Binder.clearCallingIdentity();
9047 try {
Muralidhar Reddyeb809e32021-11-19 03:07:54 +00009048 List<UiccSlotMapping> slotMappings = new ArrayList<>();
9049 for (int i = 0; i < physicalSlots.length; i++) {
9050 // Deprecated API, hence MEP is not supported. Adding default portIndex 0.
9051 slotMappings.add(new UiccSlotMapping(TelephonyManager.DEFAULT_PORT_INDEX,
9052 physicalSlots[i], i));
9053 }
9054 return (Boolean) sendRequest(CMD_SWITCH_SLOTS, slotMappings);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08009055 } finally {
9056 Binder.restoreCallingIdentity(identity);
9057 }
Holly Jiuyu Sun01c47ad2018-01-24 17:56:33 +00009058 }
Jack Yu4c988042018-02-27 15:30:01 -08009059
9060 @Override
sandeepjsb6c87872021-09-27 15:34:44 +00009061 @RequiresPermission(android.Manifest.permission.MODIFY_PHONE_STATE)
9062 public boolean setSimSlotMapping(@NonNull List<UiccSlotMapping> slotMapping) {
9063 enforceModifyPermission();
9064
9065 final long identity = Binder.clearCallingIdentity();
9066 try {
Muralidhar Reddyeb809e32021-11-19 03:07:54 +00009067 return (Boolean) sendRequest(CMD_SWITCH_SLOTS, slotMapping);
sandeepjsb6c87872021-09-27 15:34:44 +00009068 } finally {
9069 Binder.restoreCallingIdentity(identity);
9070 }
9071 }
9072
9073 @Override
Jordan Liu7de49fa2018-12-06 14:48:49 -08009074 public int getCardIdForDefaultEuicc(int subId, String callingPackage) {
Jordan Liu7de49fa2018-12-06 14:48:49 -08009075 final long identity = Binder.clearCallingIdentity();
9076 try {
9077 return UiccController.getInstance().getCardIdForDefaultEuicc();
9078 } finally {
9079 Binder.restoreCallingIdentity(identity);
9080 }
9081 }
9082
Pengquan Meng85728fb2018-03-12 16:31:21 -07009083 /**
goneil47ffb6e2018-04-06 15:40:58 -07009084 * A test API to reload the UICC profile.
9085 *
9086 * <p>Requires that the calling app has permission
9087 * {@link android.Manifest.permission#MODIFY_PHONE_STATE MODIFY_PHONE_STATE}.
9088 * @hide
9089 */
9090 @Override
9091 public void refreshUiccProfile(int subId) {
9092 enforceModifyPermission();
9093
9094 final long identity = Binder.clearCallingIdentity();
9095 try {
9096 Phone phone = getPhone(subId);
9097 if (phone == null) {
9098 return;
9099 }
Muralidhar Reddy472c2ae2021-09-29 19:38:40 +00009100 UiccPort uiccPort = phone.getUiccPort();
9101 if (uiccPort == null) {
goneil47ffb6e2018-04-06 15:40:58 -07009102 return;
9103 }
Muralidhar Reddy472c2ae2021-09-29 19:38:40 +00009104 UiccProfile uiccProfile = uiccPort.getUiccProfile();
goneil47ffb6e2018-04-06 15:40:58 -07009105 if (uiccProfile == null) {
9106 return;
9107 }
9108 uiccProfile.refresh();
9109 } finally {
9110 Binder.restoreCallingIdentity(identity);
9111 }
9112 }
9113
9114 /**
Pengquan Meng85728fb2018-03-12 16:31:21 -07009115 * Returns false if the mobile data is disabled by default, otherwise return true.
9116 */
9117 private boolean getDefaultDataEnabled() {
Inseob Kim14bb3d02018-12-13 17:11:34 +09009118 return TelephonyProperties.mobile_data().orElse(true);
Pengquan Meng85728fb2018-03-12 16:31:21 -07009119 }
9120
9121 /**
9122 * Returns true if the data roaming is enabled by default, i.e the system property
9123 * of {@link #DEFAULT_DATA_ROAMING_PROPERTY_NAME} is true or the config of
9124 * {@link CarrierConfigManager#KEY_CARRIER_DEFAULT_DATA_ROAMING_ENABLED_BOOL} is true.
9125 */
9126 private boolean getDefaultDataRoamingEnabled(int subId) {
9127 final CarrierConfigManager configMgr = (CarrierConfigManager)
Nazanin Bakhshif782e562018-12-11 15:15:39 -08009128 mApp.getSystemService(Context.CARRIER_CONFIG_SERVICE);
Shuo Qian1d84a0e2020-07-15 12:36:44 -07009129 boolean isDataRoamingEnabled = TelephonyProperties.data_roaming().orElse(false);
Pengquan Meng85728fb2018-03-12 16:31:21 -07009130 isDataRoamingEnabled |= configMgr.getConfigForSubId(subId).getBoolean(
9131 CarrierConfigManager.KEY_CARRIER_DEFAULT_DATA_ROAMING_ENABLED_BOOL);
9132 return isDataRoamingEnabled;
9133 }
9134
9135 /**
9136 * Returns the default network type for the given {@code subId}, if the default network type is
9137 * not set, return {@link Phone#PREFERRED_NT_MODE}.
9138 */
9139 private int getDefaultNetworkType(int subId) {
Inseob Kim14bb3d02018-12-13 17:11:34 +09009140 List<Integer> list = TelephonyProperties.default_network();
Jack Yudf049d72022-12-02 22:48:35 -08009141 int phoneId = SubscriptionManager.getPhoneId(subId);
Inseob Kim14bb3d02018-12-13 17:11:34 +09009142 if (phoneId >= 0 && phoneId < list.size() && list.get(phoneId) != null) {
9143 return list.get(phoneId);
9144 }
9145 return Phone.PREFERRED_NT_MODE;
Pengquan Meng85728fb2018-03-12 16:31:21 -07009146 }
fionaxua13278b2018-03-21 00:08:13 -07009147
9148 @Override
9149 public void setCarrierTestOverride(int subId, String mccmnc, String imsi, String iccid, String
chen xueaba88a2019-03-15 13:15:10 -07009150 gid1, String gid2, String plmn, String spn, String carrierPrivilegeRules, String apn) {
fionaxua13278b2018-03-21 00:08:13 -07009151 enforceModifyPermission();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08009152
9153 final long identity = Binder.clearCallingIdentity();
9154 try {
9155 final Phone phone = getPhone(subId);
9156 if (phone == null) {
9157 loge("setCarrierTestOverride fails with invalid subId: " + subId);
9158 return;
9159 }
Rambo Wang9c9ffdd2022-01-13 21:51:44 -08009160 CarrierPrivilegesTracker cpt = phone.getCarrierPrivilegesTracker();
9161 if (cpt != null) {
9162 cpt.setTestOverrideCarrierPrivilegeRules(carrierPrivilegeRules);
9163 }
9164 // TODO(b/211796398): remove the legacy logic below once CPT migration is done.
chen xueaba88a2019-03-15 13:15:10 -07009165 phone.setCarrierTestOverride(mccmnc, imsi, iccid, gid1, gid2, plmn, spn,
9166 carrierPrivilegeRules, apn);
Jeff Davidson8ab02b22020-03-28 12:24:40 -07009167 if (carrierPrivilegeRules == null) {
9168 mCarrierPrivilegeTestOverrideSubIds.remove(subId);
9169 } else {
9170 mCarrierPrivilegeTestOverrideSubIds.add(subId);
9171 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08009172 } finally {
9173 Binder.restoreCallingIdentity(identity);
fionaxua13278b2018-03-21 00:08:13 -07009174 }
fionaxua13278b2018-03-21 00:08:13 -07009175 }
9176
9177 @Override
9178 public int getCarrierIdListVersion(int subId) {
Brad Ebinger35c841c2018-10-01 10:40:55 -07009179 enforceReadPrivilegedPermission("getCarrierIdListVersion");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08009180
9181 final long identity = Binder.clearCallingIdentity();
9182 try {
9183 final Phone phone = getPhone(subId);
9184 if (phone == null) {
9185 loge("getCarrierIdListVersion fails with invalid subId: " + subId);
9186 return TelephonyManager.UNKNOWN_CARRIER_ID_LIST_VERSION;
9187 }
9188 return phone.getCarrierIdListVersion();
9189 } finally {
9190 Binder.restoreCallingIdentity(identity);
fionaxua13278b2018-03-21 00:08:13 -07009191 }
fionaxua13278b2018-03-21 00:08:13 -07009192 }
Malcolm Chen2c63d402018-08-14 16:00:53 -07009193
9194 @Override
Philip P. Moltmann700a9592019-10-03 11:53:50 -07009195 public int getNumberOfModemsWithSimultaneousDataConnections(int subId, String callingPackage,
9196 String callingFeatureId) {
Malcolm Chen2c63d402018-08-14 16:00:53 -07009197 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
Philip P. Moltmann700a9592019-10-03 11:53:50 -07009198 mApp, subId, callingPackage, callingFeatureId,
9199 "getNumberOfModemsWithSimultaneousDataConnections")) {
Malcolm Chen2c63d402018-08-14 16:00:53 -07009200 return -1;
9201 }
9202
9203 final long identity = Binder.clearCallingIdentity();
9204 try {
9205 return mPhoneConfigurationManager.getNumberOfModemsWithSimultaneousDataConnections();
9206 } finally {
9207 Binder.restoreCallingIdentity(identity);
9208 }
9209 }
Pengquan Menga1bb6272018-09-06 09:59:22 -07009210
9211 @Override
9212 public int getCdmaRoamingMode(int subId) {
zoey chen7e6d4e52019-12-17 18:18:59 +08009213 TelephonyPermissions
Nathan Harold62c68512021-04-06 11:26:02 -07009214 .enforceCallingOrSelfReadPrivilegedPhoneStatePermissionOrCarrierPrivilege(
Pengquan Menga1bb6272018-09-06 09:59:22 -07009215 mApp, subId, "getCdmaRoamingMode");
9216
9217 final long identity = Binder.clearCallingIdentity();
9218 try {
9219 return (int) sendRequest(CMD_GET_CDMA_ROAMING_MODE, null /* argument */, subId);
9220 } finally {
9221 Binder.restoreCallingIdentity(identity);
9222 }
9223 }
9224
9225 @Override
9226 public boolean setCdmaRoamingMode(int subId, int mode) {
9227 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
9228 mApp, subId, "setCdmaRoamingMode");
9229
9230 final long identity = Binder.clearCallingIdentity();
9231 try {
9232 return (boolean) sendRequest(CMD_SET_CDMA_ROAMING_MODE, mode, subId);
9233 } finally {
9234 Binder.restoreCallingIdentity(identity);
9235 }
9236 }
9237
9238 @Override
Sarah Chinbaab1432020-10-28 13:46:24 -07009239 public int getCdmaSubscriptionMode(int subId) {
9240 TelephonyPermissions
Nathan Harold62c68512021-04-06 11:26:02 -07009241 .enforceCallingOrSelfReadPrivilegedPhoneStatePermissionOrCarrierPrivilege(
Sarah Chinbaab1432020-10-28 13:46:24 -07009242 mApp, subId, "getCdmaSubscriptionMode");
9243
9244 final long identity = Binder.clearCallingIdentity();
9245 try {
9246 return (int) sendRequest(CMD_GET_CDMA_SUBSCRIPTION_MODE, null /* argument */, subId);
9247 } finally {
9248 Binder.restoreCallingIdentity(identity);
9249 }
9250 }
9251
9252 @Override
Pengquan Menga1bb6272018-09-06 09:59:22 -07009253 public boolean setCdmaSubscriptionMode(int subId, int mode) {
9254 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
9255 mApp, subId, "setCdmaSubscriptionMode");
9256
9257 final long identity = Binder.clearCallingIdentity();
9258 try {
9259 return (boolean) sendRequest(CMD_SET_CDMA_SUBSCRIPTION_MODE, mode, subId);
9260 } finally {
9261 Binder.restoreCallingIdentity(identity);
9262 }
9263 }
Makoto Onukida3bf792018-09-18 16:06:29 -07009264
sqianc5eccab2018-10-19 18:46:41 -07009265 @Override
sqian8c685422019-02-22 15:55:18 -08009266 public Map<Integer, List<EmergencyNumber>> getEmergencyNumberList(
Philip P. Moltmann700a9592019-10-03 11:53:50 -07009267 String callingPackage, String callingFeatureId) {
sqian11b7a0e2018-12-05 18:48:28 -08009268 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
Philip P. Moltmann700a9592019-10-03 11:53:50 -07009269 mApp, getDefaultSubscription(), callingPackage, callingFeatureId,
9270 "getEmergencyNumberList")) {
sqian11b7a0e2018-12-05 18:48:28 -08009271 throw new SecurityException("Requires READ_PHONE_STATE permission.");
9272 }
9273 final long identity = Binder.clearCallingIdentity();
9274 try {
sqian854d44b2018-12-12 16:48:18 -08009275 Map<Integer, List<EmergencyNumber>> emergencyNumberListInternal = new HashMap<>();
9276 for (Phone phone: PhoneFactory.getPhones()) {
9277 if (phone.getEmergencyNumberTracker() != null
9278 && phone.getEmergencyNumberTracker().getEmergencyNumberList() != null) {
9279 emergencyNumberListInternal.put(
9280 phone.getSubId(),
9281 phone.getEmergencyNumberTracker().getEmergencyNumberList());
9282 }
sqian11b7a0e2018-12-05 18:48:28 -08009283 }
sqian854d44b2018-12-12 16:48:18 -08009284 return emergencyNumberListInternal;
sqian11b7a0e2018-12-05 18:48:28 -08009285 } finally {
9286 Binder.restoreCallingIdentity(identity);
9287 }
sqianc5eccab2018-10-19 18:46:41 -07009288 }
9289
9290 @Override
sqian8c685422019-02-22 15:55:18 -08009291 public boolean isEmergencyNumber(String number, boolean exactMatch) {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08009292 final Phone defaultPhone = getDefaultPhone();
sqian11b7a0e2018-12-05 18:48:28 -08009293 if (!exactMatch) {
9294 TelephonyPermissions
Nathan Harold62c68512021-04-06 11:26:02 -07009295 .enforceCallingOrSelfReadPrivilegedPhoneStatePermissionOrCarrierPrivilege(
sqian8c685422019-02-22 15:55:18 -08009296 mApp, defaultPhone.getSubId(), "isEmergencyNumber(Potential)");
sqian11b7a0e2018-12-05 18:48:28 -08009297 }
9298 final long identity = Binder.clearCallingIdentity();
9299 try {
sqian854d44b2018-12-12 16:48:18 -08009300 for (Phone phone: PhoneFactory.getPhones()) {
9301 if (phone.getEmergencyNumberTracker() != null
Taesu Leee050c002020-10-13 17:19:35 +09009302 && phone.getEmergencyNumberTracker()
9303 .isEmergencyNumber(number, exactMatch)) {
9304 return true;
sqian11b7a0e2018-12-05 18:48:28 -08009305 }
sqian11b7a0e2018-12-05 18:48:28 -08009306 }
9307 return false;
9308 } finally {
9309 Binder.restoreCallingIdentity(identity);
9310 }
9311 }
9312
sqianf4ca7ed2019-01-15 18:32:07 -08009313 /**
Shuo Qianccbaf742021-02-22 18:32:21 -08009314 * Start emergency callback mode for GsmCdmaPhone for testing.
9315 */
9316 @Override
9317 public void startEmergencyCallbackMode() {
9318 TelephonyPermissions.enforceShellOnly(Binder.getCallingUid(),
9319 "startEmergencyCallbackMode");
9320 enforceModifyPermission();
9321 final long identity = Binder.clearCallingIdentity();
9322 try {
9323 for (Phone phone : PhoneFactory.getPhones()) {
9324 Rlog.d(LOG_TAG, "startEmergencyCallbackMode phone type: " + phone.getPhoneType());
9325 if (phone != null && ((phone.getPhoneType() == PHONE_TYPE_GSM)
9326 || (phone.getPhoneType() == PHONE_TYPE_CDMA))) {
9327 GsmCdmaPhone gsmCdmaPhone = (GsmCdmaPhone) phone;
9328 gsmCdmaPhone.obtainMessage(
9329 GsmCdmaPhone.EVENT_EMERGENCY_CALLBACK_MODE_ENTER).sendToTarget();
9330 Rlog.d(LOG_TAG, "startEmergencyCallbackMode: triggered");
9331 }
9332 }
9333 } finally {
9334 Binder.restoreCallingIdentity(identity);
9335 }
9336 }
9337
9338 /**
sqianf4ca7ed2019-01-15 18:32:07 -08009339 * Update emergency number list for test mode.
9340 */
9341 @Override
9342 public void updateEmergencyNumberListTestMode(int action, EmergencyNumber num) {
9343 TelephonyPermissions.enforceShellOnly(Binder.getCallingUid(),
9344 "updateEmergencyNumberListTestMode");
9345
9346 final long identity = Binder.clearCallingIdentity();
9347 try {
9348 for (Phone phone: PhoneFactory.getPhones()) {
9349 EmergencyNumberTracker tracker = phone.getEmergencyNumberTracker();
9350 if (tracker != null) {
9351 tracker.executeEmergencyNumberTestModeCommand(action, num);
9352 }
9353 }
9354 } finally {
9355 Binder.restoreCallingIdentity(identity);
9356 }
9357 }
9358
9359 /**
9360 * Get the full emergency number list for test mode.
9361 */
9362 @Override
9363 public List<String> getEmergencyNumberListTestMode() {
9364 TelephonyPermissions.enforceShellOnly(Binder.getCallingUid(),
9365 "getEmergencyNumberListTestMode");
9366
9367 final long identity = Binder.clearCallingIdentity();
9368 try {
9369 Set<String> emergencyNumbers = new HashSet<>();
9370 for (Phone phone: PhoneFactory.getPhones()) {
9371 EmergencyNumberTracker tracker = phone.getEmergencyNumberTracker();
9372 if (tracker != null) {
9373 for (EmergencyNumber num : tracker.getEmergencyNumberList()) {
9374 emergencyNumbers.add(num.getNumber());
9375 }
9376 }
9377 }
9378 return new ArrayList<>(emergencyNumbers);
9379 } finally {
9380 Binder.restoreCallingIdentity(identity);
9381 }
9382 }
9383
chen xud6b45bd2018-10-30 22:27:10 -07009384 @Override
Shuo Qian3b6ee772019-11-13 17:43:31 -08009385 public int getEmergencyNumberDbVersion(int subId) {
9386 enforceReadPrivilegedPermission("getEmergencyNumberDbVersion");
9387
9388 final long identity = Binder.clearCallingIdentity();
9389 try {
9390 final Phone phone = getPhone(subId);
9391 if (phone == null) {
9392 loge("getEmergencyNumberDbVersion fails with invalid subId: " + subId);
9393 return TelephonyManager.INVALID_EMERGENCY_NUMBER_DB_VERSION;
9394 }
9395 return phone.getEmergencyNumberDbVersion();
9396 } finally {
9397 Binder.restoreCallingIdentity(identity);
9398 }
9399 }
9400
9401 @Override
9402 public void notifyOtaEmergencyNumberDbInstalled() {
9403 enforceModifyPermission();
9404
9405 final long identity = Binder.clearCallingIdentity();
9406 try {
9407 for (Phone phone: PhoneFactory.getPhones()) {
9408 EmergencyNumberTracker tracker = phone.getEmergencyNumberTracker();
9409 if (tracker != null) {
9410 tracker.updateOtaEmergencyNumberDatabase();
9411 }
9412 }
9413 } finally {
9414 Binder.restoreCallingIdentity(identity);
9415 }
9416 }
9417
9418 @Override
Shuo Qianc373f112020-03-05 17:55:34 -08009419 public void updateOtaEmergencyNumberDbFilePath(ParcelFileDescriptor otaParcelFileDescriptor) {
Shuo Qian3b6ee772019-11-13 17:43:31 -08009420 enforceActiveEmergencySessionPermission();
9421
9422 final long identity = Binder.clearCallingIdentity();
9423 try {
9424 for (Phone phone: PhoneFactory.getPhones()) {
9425 EmergencyNumberTracker tracker = phone.getEmergencyNumberTracker();
9426 if (tracker != null) {
Shuo Qianc373f112020-03-05 17:55:34 -08009427 tracker.updateOtaEmergencyNumberDbFilePath(otaParcelFileDescriptor);
9428 }
9429 }
9430 } finally {
9431 Binder.restoreCallingIdentity(identity);
9432 }
9433 }
9434
9435 @Override
9436 public void resetOtaEmergencyNumberDbFilePath() {
9437 enforceActiveEmergencySessionPermission();
9438
9439 final long identity = Binder.clearCallingIdentity();
9440 try {
9441 for (Phone phone: PhoneFactory.getPhones()) {
9442 EmergencyNumberTracker tracker = phone.getEmergencyNumberTracker();
9443 if (tracker != null) {
9444 tracker.resetOtaEmergencyNumberDbFilePath();
Shuo Qian3b6ee772019-11-13 17:43:31 -08009445 }
9446 }
9447 } finally {
9448 Binder.restoreCallingIdentity(identity);
9449 }
9450 }
9451
9452 @Override
chen xud6b45bd2018-10-30 22:27:10 -07009453 public List<String> getCertsFromCarrierPrivilegeAccessRules(int subId) {
9454 enforceReadPrivilegedPermission("getCertsFromCarrierPrivilegeAccessRules");
9455 Phone phone = getPhone(subId);
9456 if (phone == null) {
9457 return null;
9458 }
9459 final long identity = Binder.clearCallingIdentity();
9460 try {
9461 UiccProfile profile = UiccController.getInstance()
9462 .getUiccProfileForPhone(phone.getPhoneId());
9463 if (profile != null) {
9464 return profile.getCertsFromCarrierPrivilegeAccessRules();
9465 }
9466 } finally {
9467 Binder.restoreCallingIdentity(identity);
9468 }
9469 return null;
9470 }
Malcolm Chen8e4ed912019-01-15 20:22:16 -08009471
9472 /**
9473 * Enable or disable a modem stack.
9474 */
9475 @Override
9476 public boolean enableModemForSlot(int slotIndex, boolean enable) {
9477 enforceModifyPermission();
9478
9479 final long identity = Binder.clearCallingIdentity();
9480 try {
9481 Phone phone = PhoneFactory.getPhone(slotIndex);
9482 if (phone == null) {
9483 return false;
9484 } else {
9485 return (Boolean) sendRequest(CMD_REQUEST_ENABLE_MODEM, enable, phone, null);
9486 }
9487 } finally {
9488 Binder.restoreCallingIdentity(identity);
9489 }
9490 }
Michelecea4cf22018-12-21 15:00:11 -08009491
Malcolm Chen4bcd9822019-03-27 18:34:05 -07009492 /**
9493 * Whether a modem stack is enabled or not.
9494 */
9495 @Override
Philip P. Moltmann700a9592019-10-03 11:53:50 -07009496 public boolean isModemEnabledForSlot(int slotIndex, String callingPackage,
9497 String callingFeatureId) {
Malcolm Chen4bcd9822019-03-27 18:34:05 -07009498 Phone phone = PhoneFactory.getPhone(slotIndex);
9499 if (phone == null) return false;
9500
9501 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
Philip P. Moltmann700a9592019-10-03 11:53:50 -07009502 mApp, phone.getSubId(), callingPackage, callingFeatureId,
9503 "isModemEnabledForSlot")) {
Malcolm Chen4bcd9822019-03-27 18:34:05 -07009504 throw new SecurityException("Requires READ_PHONE_STATE permission.");
9505 }
9506
9507 final long identity = Binder.clearCallingIdentity();
9508 try {
Nazanin Bakhshif71371d2019-04-29 17:29:44 -07009509 try {
9510 return mPhoneConfigurationManager.getPhoneStatusFromCache(phone.getPhoneId());
9511 } catch (NoSuchElementException ex) {
9512 return (Boolean) sendRequest(CMD_GET_MODEM_STATUS, null, phone, null);
9513 }
Malcolm Chen4bcd9822019-03-27 18:34:05 -07009514 } finally {
9515 Binder.restoreCallingIdentity(identity);
9516 }
9517 }
9518
Michelecea4cf22018-12-21 15:00:11 -08009519 @Override
Michele0ea7d782019-03-19 14:58:42 -07009520 public void setMultiSimCarrierRestriction(boolean isMultiSimCarrierRestricted) {
Michelecea4cf22018-12-21 15:00:11 -08009521 enforceModifyPermission();
9522
9523 final long identity = Binder.clearCallingIdentity();
9524 try {
9525 mTelephonySharedPreferences.edit()
Michele0ea7d782019-03-19 14:58:42 -07009526 .putBoolean(PREF_MULTI_SIM_RESTRICTED, isMultiSimCarrierRestricted)
Michelecea4cf22018-12-21 15:00:11 -08009527 .commit();
9528 } finally {
9529 Binder.restoreCallingIdentity(identity);
9530 }
9531 }
9532
9533 @Override
Michele0ea7d782019-03-19 14:58:42 -07009534 @TelephonyManager.IsMultiSimSupportedResult
Philip P. Moltmann700a9592019-10-03 11:53:50 -07009535 public int isMultiSimSupported(String callingPackage, String callingFeatureId) {
Michele4245e952019-02-04 11:36:23 -08009536 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(mApp,
Philip P. Moltmann700a9592019-10-03 11:53:50 -07009537 getDefaultPhone().getSubId(), callingPackage, callingFeatureId,
9538 "isMultiSimSupported")) {
Michele0ea7d782019-03-19 14:58:42 -07009539 return TelephonyManager.MULTISIM_NOT_SUPPORTED_BY_HARDWARE;
Michele4245e952019-02-04 11:36:23 -08009540 }
Michelecea4cf22018-12-21 15:00:11 -08009541
9542 final long identity = Binder.clearCallingIdentity();
9543 try {
Michele0ea7d782019-03-19 14:58:42 -07009544 return isMultiSimSupportedInternal();
Michelecea4cf22018-12-21 15:00:11 -08009545 } finally {
9546 Binder.restoreCallingIdentity(identity);
9547 }
9548 }
Nazanin Bakhshi628473f2019-01-29 17:37:52 -08009549
Michele0ea7d782019-03-19 14:58:42 -07009550 @TelephonyManager.IsMultiSimSupportedResult
9551 private int isMultiSimSupportedInternal() {
Michele30b57b22019-03-01 12:01:14 -08009552 // If the device has less than 2 SIM cards, indicate that multisim is restricted.
9553 int numPhysicalSlots = UiccController.getInstance().getUiccSlots().length;
9554 if (numPhysicalSlots < 2) {
Michele0ea7d782019-03-19 14:58:42 -07009555 loge("isMultiSimSupportedInternal: requires at least 2 cards");
9556 return TelephonyManager.MULTISIM_NOT_SUPPORTED_BY_HARDWARE;
Michele30b57b22019-03-01 12:01:14 -08009557 }
9558 // Check if the hardware supports multisim functionality. If usage of multisim is not
9559 // supported by the modem, indicate that it is restricted.
9560 PhoneCapability staticCapability =
9561 mPhoneConfigurationManager.getStaticPhoneCapability();
9562 if (staticCapability == null) {
Michele0ea7d782019-03-19 14:58:42 -07009563 loge("isMultiSimSupportedInternal: no static configuration available");
9564 return TelephonyManager.MULTISIM_NOT_SUPPORTED_BY_HARDWARE;
Michele30b57b22019-03-01 12:01:14 -08009565 }
SongFerngWang8236caa2021-01-17 21:51:44 +08009566 if (staticCapability.getLogicalModemList().size() < 2) {
Michele0ea7d782019-03-19 14:58:42 -07009567 loge("isMultiSimSupportedInternal: maximum number of modem is < 2");
9568 return TelephonyManager.MULTISIM_NOT_SUPPORTED_BY_HARDWARE;
Michele30b57b22019-03-01 12:01:14 -08009569 }
9570 // Check if support of multiple SIMs is restricted by carrier
9571 if (mTelephonySharedPreferences.getBoolean(PREF_MULTI_SIM_RESTRICTED, false)) {
Michele0ea7d782019-03-19 14:58:42 -07009572 return TelephonyManager.MULTISIM_NOT_SUPPORTED_BY_CARRIER;
Michele30b57b22019-03-01 12:01:14 -08009573 }
9574
Michele0ea7d782019-03-19 14:58:42 -07009575 return TelephonyManager.MULTISIM_ALLOWED;
Michele30b57b22019-03-01 12:01:14 -08009576 }
9577
Nazanin Bakhshi628473f2019-01-29 17:37:52 -08009578 /**
9579 * Switch configs to enable multi-sim or switch back to single-sim
Nazanin Bakhshi17318782019-03-01 11:56:08 -08009580 * Note: Switch from multi-sim to single-sim is only possible with MODIFY_PHONE_STATE
9581 * permission, but the other way around is possible with either MODIFY_PHONE_STATE
9582 * or carrier privileges
Nazanin Bakhshi628473f2019-01-29 17:37:52 -08009583 * @param numOfSims number of active sims we want to switch to
9584 */
9585 @Override
9586 public void switchMultiSimConfig(int numOfSims) {
Nazanin Bakhshi17318782019-03-01 11:56:08 -08009587 if (numOfSims == 1) {
9588 enforceModifyPermission();
9589 } else {
9590 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
9591 mApp, SubscriptionManager.DEFAULT_SUBSCRIPTION_ID, "switchMultiSimConfig");
9592 }
Nazanin Bakhshi628473f2019-01-29 17:37:52 -08009593 final long identity = Binder.clearCallingIdentity();
Michele30b57b22019-03-01 12:01:14 -08009594
Nazanin Bakhshi628473f2019-01-29 17:37:52 -08009595 try {
Michele30b57b22019-03-01 12:01:14 -08009596 //only proceed if multi-sim is not restricted
Michele0ea7d782019-03-19 14:58:42 -07009597 if (isMultiSimSupportedInternal() != TelephonyManager.MULTISIM_ALLOWED) {
Michele30b57b22019-03-01 12:01:14 -08009598 loge("switchMultiSimConfig not possible. It is restricted or not supported.");
9599 return;
9600 }
Nazanin Bakhshi628473f2019-01-29 17:37:52 -08009601 mPhoneConfigurationManager.switchMultiSimConfig(numOfSims);
9602 } finally {
9603 Binder.restoreCallingIdentity(identity);
9604 }
9605 }
9606
Hyungjun Parkbb07fde2019-01-10 15:28:51 +09009607 @Override
9608 public boolean isApplicationOnUicc(int subId, int appType) {
9609 enforceReadPrivilegedPermission("isApplicationOnUicc");
9610 Phone phone = getPhone(subId);
9611 if (phone == null) {
9612 return false;
9613 }
9614 final long identity = Binder.clearCallingIdentity();
9615 try {
Muralidhar Reddy472c2ae2021-09-29 19:38:40 +00009616 UiccPort uiccPort = phone.getUiccPort();
9617 if (uiccPort == null) {
Hyungjun Parkbb07fde2019-01-10 15:28:51 +09009618 return false;
9619 }
Muralidhar Reddy472c2ae2021-09-29 19:38:40 +00009620 UiccProfile uiccProfile = uiccPort.getUiccProfile();
Hyungjun Parkbb07fde2019-01-10 15:28:51 +09009621 if (uiccProfile == null) {
9622 return false;
9623 }
9624 if (TelephonyManager.APPTYPE_SIM <= appType
9625 && appType <= TelephonyManager.APPTYPE_ISIM) {
9626 return uiccProfile.isApplicationOnIcc(AppType.values()[appType]);
9627 }
9628 return false;
9629 } finally {
9630 Binder.restoreCallingIdentity(identity);
9631 }
9632 }
9633
Nazanin Bakhshi628473f2019-01-29 17:37:52 -08009634 /**
chen xub4baa772019-04-03 10:23:41 -07009635 * Get whether making changes to modem configurations will trigger reboot.
9636 * Return value defaults to true.
Nazanin Bakhshi5fe5ef22019-01-30 10:52:09 -08009637 */
9638 @Override
Philip P. Moltmann700a9592019-10-03 11:53:50 -07009639 public boolean doesSwitchMultiSimConfigTriggerReboot(int subId, String callingPackage,
9640 String callingFeatureId) {
chen xub4baa772019-04-03 10:23:41 -07009641 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
Philip P. Moltmann700a9592019-10-03 11:53:50 -07009642 mApp, subId, callingPackage, callingFeatureId,
9643 "doesSwitchMultiSimConfigTriggerReboot")) {
chen xub4baa772019-04-03 10:23:41 -07009644 return false;
9645 }
Nazanin Bakhshi5fe5ef22019-01-30 10:52:09 -08009646 final long identity = Binder.clearCallingIdentity();
9647 try {
9648 return mPhoneConfigurationManager.isRebootRequiredForModemConfigChange();
9649 } finally {
9650 Binder.restoreCallingIdentity(identity);
9651 }
9652 }
9653
Nathan Harold29f5f052019-02-15 13:41:57 -08009654 private void updateModemStateMetrics() {
9655 TelephonyMetrics metrics = TelephonyMetrics.getInstance();
9656 // TODO: check the state for each modem if the api is ready.
9657 metrics.updateEnabledModemBitmap((1 << TelephonyManager.from(mApp).getPhoneCount()) - 1);
9658 }
9659
Pengquan Meng3889a572019-01-23 11:16:29 -08009660 @Override
sandeepjsa208e3b2021-11-17 04:05:58 +00009661 public List<UiccSlotMapping> getSlotsMapping(String callingPackage) {
Pengquan Meng3889a572019-01-23 11:16:29 -08009662 enforceReadPrivilegedPermission("getSlotsMapping");
sandeepjsa208e3b2021-11-17 04:05:58 +00009663 // Verify that the callingPackage belongs to the calling UID
9664 mApp.getSystemService(AppOpsManager.class)
9665 .checkPackage(Binder.getCallingUid(), callingPackage);
Pengquan Meng3889a572019-01-23 11:16:29 -08009666 final long identity = Binder.clearCallingIdentity();
sandeepjsa208e3b2021-11-17 04:05:58 +00009667 List<UiccSlotMapping> slotMap = new ArrayList<>();
Pengquan Meng3889a572019-01-23 11:16:29 -08009668 try {
sandeepjsa208e3b2021-11-17 04:05:58 +00009669 UiccSlotInfo[] slotInfos = getUiccSlotsInfo(mApp.getOpPackageName());
9670 if (slotInfos != null) {
9671 for (int i = 0; i < slotInfos.length; i++) {
9672 for (UiccPortInfo portInfo : slotInfos[i].getPorts()) {
9673 if (SubscriptionManager.isValidPhoneId(portInfo.getLogicalSlotIndex())) {
9674 slotMap.add(new UiccSlotMapping(portInfo.getPortIndex(), i,
9675 portInfo.getLogicalSlotIndex()));
9676 }
9677 }
Pengquan Meng3889a572019-01-23 11:16:29 -08009678 }
9679 }
sandeepjsa208e3b2021-11-17 04:05:58 +00009680 return slotMap;
Pengquan Meng3889a572019-01-23 11:16:29 -08009681 } finally {
9682 Binder.restoreCallingIdentity(identity);
9683 }
9684 }
Nathan Harold48d6fd52019-02-06 19:01:40 -08009685
9686 /**
9687 * Get the IRadio HAL Version
9688 */
9689 @Override
9690 public int getRadioHalVersion() {
9691 Phone phone = getDefaultPhone();
9692 if (phone == null) return -1;
9693 HalVersion hv = phone.getHalVersion();
9694 if (hv.equals(HalVersion.UNKNOWN)) return -1;
9695 return hv.major * 100 + hv.minor;
9696 }
Malcolm Chendc8c10e2019-04-10 18:25:07 -07009697
9698 /**
Shuo Qianda2d6ec2020-01-14 15:18:28 -08009699 * Get the current calling package name.
9700 * @return the current calling package name
9701 */
9702 @Override
9703 public String getCurrentPackageName() {
9704 return mApp.getPackageManager().getPackagesForUid(Binder.getCallingUid())[0];
9705 }
9706
9707 /**
Malcolm Chene5ad5792019-04-18 13:51:02 -07009708 * Return whether data is enabled for certain APN type. This will tell if framework will accept
9709 * corresponding network requests on a subId.
9710 *
9711 * Data is enabled if:
Malcolm Chendc8c10e2019-04-10 18:25:07 -07009712 * 1) user data is turned on, or
Malcolm Chene5ad5792019-04-18 13:51:02 -07009713 * 2) APN is un-metered for this subscription, or
9714 * 3) APN type is whitelisted. E.g. MMS is whitelisted if
Hall Liu746e03c2020-09-25 11:13:49 -07009715 * {@link TelephonyManager#MOBILE_DATA_POLICY_MMS_ALWAYS_ALLOWED} is enabled.
Malcolm Chene5ad5792019-04-18 13:51:02 -07009716 *
9717 * @return whether data is allowed for a apn type.
9718 *
9719 * @hide
Malcolm Chendc8c10e2019-04-10 18:25:07 -07009720 */
9721 @Override
Malcolm Chene5ad5792019-04-18 13:51:02 -07009722 public boolean isDataEnabledForApn(int apnType, int subId, String callingPackage) {
Amit Mahajan5d4e1922019-10-07 16:20:43 -07009723 enforceReadPrivilegedPermission("Needs READ_PRIVILEGED_PHONE_STATE for "
9724 + "isDataEnabledForApn");
Malcolm Chendc8c10e2019-04-10 18:25:07 -07009725
9726 // Now that all security checks passes, perform the operation as ourselves.
9727 final long identity = Binder.clearCallingIdentity();
9728 try {
9729 Phone phone = getPhone(subId);
9730 if (phone == null) return false;
9731
Jack Yu27422a52022-03-21 10:38:05 -07009732 boolean isMetered;
Jack Yu99e87332021-12-17 23:14:15 -08009733 boolean isDataEnabled;
Jack Yuabb10902022-07-31 00:43:21 -07009734 isMetered = phone.getDataNetworkController().getDataConfigManager()
9735 .isMeteredCapability(DataUtils.apnTypeToNetworkCapability(apnType),
9736 phone.getServiceState().getDataRoaming());
9737 isDataEnabled = phone.getDataSettingsManager().isDataEnabled(apnType);
Jack Yu99e87332021-12-17 23:14:15 -08009738 return !isMetered || isDataEnabled;
Malcolm Chene5ad5792019-04-18 13:51:02 -07009739 } finally {
9740 Binder.restoreCallingIdentity(identity);
9741 }
9742 }
9743
9744 @Override
Jack Yu41407ee2019-05-13 16:54:09 -07009745 public boolean isApnMetered(@ApnType int apnType, int subId) {
Malcolm Chene5ad5792019-04-18 13:51:02 -07009746 enforceReadPrivilegedPermission("isApnMetered");
9747
9748 // Now that all security checks passes, perform the operation as ourselves.
9749 final long identity = Binder.clearCallingIdentity();
9750 try {
9751 Phone phone = getPhone(subId);
9752 if (phone == null) return true; // By default return true.
Jack Yuabb10902022-07-31 00:43:21 -07009753 return phone.getDataNetworkController().getDataConfigManager().isMeteredCapability(
9754 DataUtils.apnTypeToNetworkCapability(apnType),
9755 phone.getServiceState().getDataRoaming());
Malcolm Chendc8c10e2019-04-10 18:25:07 -07009756 } finally {
9757 Binder.restoreCallingIdentity(identity);
9758 }
9759 }
Brad Ebingera63db5f2019-04-23 16:31:13 -07009760
9761 @Override
Hall Liu73f5d362020-01-20 13:42:00 -08009762 public void setSystemSelectionChannels(List<RadioAccessSpecifier> specifiers,
9763 int subscriptionId, IBooleanConsumer resultCallback) {
9764 enforceModifyPermission();
9765 long token = Binder.clearCallingIdentity();
9766 try {
9767 Phone phone = getPhone(subscriptionId);
9768 if (phone == null) {
9769 try {
9770 if (resultCallback != null) {
9771 resultCallback.accept(false);
9772 }
9773 } catch (RemoteException e) {
9774 // ignore
9775 }
9776 return;
9777 }
9778 Pair<List<RadioAccessSpecifier>, Consumer<Boolean>> argument =
9779 Pair.create(specifiers, (x) -> {
9780 try {
9781 if (resultCallback != null) {
9782 resultCallback.accept(x);
9783 }
9784 } catch (RemoteException e) {
9785 // ignore
9786 }
9787 });
9788 sendRequestAsync(CMD_SET_SYSTEM_SELECTION_CHANNELS, argument, phone, null);
9789 } finally {
9790 Binder.restoreCallingIdentity(token);
9791 }
9792 }
9793
9794 @Override
Sarah Chin679c08a2020-11-18 13:39:35 -08009795 public List<RadioAccessSpecifier> getSystemSelectionChannels(int subId) {
9796 TelephonyPermissions
Nathan Harold62c68512021-04-06 11:26:02 -07009797 .enforceCallingOrSelfReadPrivilegedPhoneStatePermissionOrCarrierPrivilege(
Sarah Chin679c08a2020-11-18 13:39:35 -08009798 mApp, subId, "getSystemSelectionChannels");
9799 WorkSource workSource = getWorkSource(Binder.getCallingUid());
9800 final long identity = Binder.clearCallingIdentity();
9801 try {
Sarah Chin428d1d62021-03-13 03:17:40 -08009802 Object result = sendRequest(CMD_GET_SYSTEM_SELECTION_CHANNELS, null, subId, workSource);
9803 if (result instanceof IllegalStateException) {
9804 throw (IllegalStateException) result;
9805 }
9806 List<RadioAccessSpecifier> specifiers = (List<RadioAccessSpecifier>) result;
Sarah Chin679c08a2020-11-18 13:39:35 -08009807 if (DBG) log("getSystemSelectionChannels: " + specifiers);
9808 return specifiers;
9809 } finally {
9810 Binder.restoreCallingIdentity(identity);
9811 }
9812 }
9813
9814 @Override
Jack Yu8b766fc2022-03-21 09:42:33 -07009815 public boolean isMvnoMatched(int slotIndex, int mvnoType, @NonNull String mvnoMatchData) {
changbettyca3d40d2020-03-03 16:27:31 +08009816 enforceReadPrivilegedPermission("isMvnoMatched");
Jack Yu8b766fc2022-03-21 09:42:33 -07009817 return UiccController.getInstance().mvnoMatches(slotIndex, mvnoType, mvnoMatchData);
changbetty7157e9e2019-12-06 18:16:37 +08009818 }
9819
9820 @Override
Philip P. Moltmannd02b7372020-03-18 17:06:12 -07009821 public void enqueueSmsPickResult(String callingPackage, String callingAttributionTag,
9822 IIntegerConsumer pendingSubIdResult) {
Shuo Qianda2d6ec2020-01-14 15:18:28 -08009823 if (callingPackage == null) {
9824 callingPackage = getCurrentPackageName();
9825 }
Brad Ebingera63db5f2019-04-23 16:31:13 -07009826 SmsPermissions permissions = new SmsPermissions(getDefaultPhone(), mApp,
9827 (AppOpsManager) mApp.getSystemService(Context.APP_OPS_SERVICE));
Philip P. Moltmannd02b7372020-03-18 17:06:12 -07009828 if (!permissions.checkCallingCanSendSms(callingPackage, callingAttributionTag,
9829 "Sending message")) {
Brad Ebingera63db5f2019-04-23 16:31:13 -07009830 throw new SecurityException("Requires SEND_SMS permission to perform this operation");
9831 }
9832 PickSmsSubscriptionActivity.addPendingResult(pendingSubIdResult);
9833 Intent intent = new Intent();
9834 intent.setClass(mApp, PickSmsSubscriptionActivity.class);
9835 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
9836 // Bring up choose default SMS subscription dialog right now
9837 intent.putExtra(PickSmsSubscriptionActivity.DIALOG_TYPE_KEY,
9838 PickSmsSubscriptionActivity.SMS_PICK_FOR_MESSAGE);
9839 mApp.startActivity(intent);
9840 }
chen xud5ca2d52019-05-28 15:20:57 -07009841
9842 @Override
9843 public String getMmsUAProfUrl(int subId) {
9844 //TODO investigate if this API should require proper permission check in R b/133791609
9845 final long identity = Binder.clearCallingIdentity();
9846 try {
Guoqiang.Qiu171383d2020-07-13 09:38:32 +08009847 String carrierUAProfUrl = mApp.getCarrierConfigForSubId(subId).getString(
9848 CarrierConfigManager.KEY_MMS_UA_PROF_URL_STRING);
9849 if (!TextUtils.isEmpty(carrierUAProfUrl)) {
9850 return carrierUAProfUrl;
9851 }
Daniel Brightebb4eb72020-02-18 15:16:33 -08009852 return SubscriptionManager.getResourcesForSubId(getDefaultPhone().getContext(), subId)
9853 .getString(com.android.internal.R.string.config_mms_user_agent_profile_url);
chen xud5ca2d52019-05-28 15:20:57 -07009854 } finally {
9855 Binder.restoreCallingIdentity(identity);
9856 }
9857 }
9858
9859 @Override
9860 public String getMmsUserAgent(int subId) {
9861 //TODO investigate if this API should require proper permission check in R b/133791609
9862 final long identity = Binder.clearCallingIdentity();
9863 try {
Guoqiang.Qiu171383d2020-07-13 09:38:32 +08009864 String carrierUserAgent = mApp.getCarrierConfigForSubId(subId).getString(
9865 CarrierConfigManager.KEY_MMS_USER_AGENT_STRING);
9866 if (!TextUtils.isEmpty(carrierUserAgent)) {
9867 return carrierUserAgent;
9868 }
Daniel Brightebb4eb72020-02-18 15:16:33 -08009869 return SubscriptionManager.getResourcesForSubId(getDefaultPhone().getContext(), subId)
9870 .getString(com.android.internal.R.string.config_mms_user_agent);
chen xud5ca2d52019-05-28 15:20:57 -07009871 } finally {
9872 Binder.restoreCallingIdentity(identity);
9873 }
9874 }
Jack Yub07d4972019-05-28 16:12:25 -07009875
9876 @Override
Hall Liua62f5da2020-09-25 10:42:19 -07009877 public boolean isMobileDataPolicyEnabled(int subscriptionId, int policy) {
9878 enforceReadPrivilegedPermission("isMobileDataPolicyEnabled");
Jack Yub07d4972019-05-28 16:12:25 -07009879
Jack Yub07d4972019-05-28 16:12:25 -07009880 final long identity = Binder.clearCallingIdentity();
9881 try {
Hall Liua62f5da2020-09-25 10:42:19 -07009882 Phone phone = getPhone(subscriptionId);
Jack Yub07d4972019-05-28 16:12:25 -07009883 if (phone == null) return false;
9884
Ling Ma58448e42022-09-16 15:22:36 -07009885 return phone.getDataSettingsManager().isMobileDataPolicyEnabled(policy);
Jack Yub07d4972019-05-28 16:12:25 -07009886 } finally {
9887 Binder.restoreCallingIdentity(identity);
9888 }
9889 }
9890
9891 @Override
Hall Liuc66bb112021-02-02 12:09:32 -08009892 public void setMobileDataPolicyEnabled(int subscriptionId, int policy,
Hall Liua62f5da2020-09-25 10:42:19 -07009893 boolean enabled) {
changbettyd5c246e2019-12-24 15:40:37 +08009894 enforceModifyPermission();
9895
changbettyd5c246e2019-12-24 15:40:37 +08009896 final long identity = Binder.clearCallingIdentity();
9897 try {
Hall Liua62f5da2020-09-25 10:42:19 -07009898 Phone phone = getPhone(subscriptionId);
9899 if (phone == null) return;
changbettyd5c246e2019-12-24 15:40:37 +08009900
Ling Ma58448e42022-09-16 15:22:36 -07009901 phone.getDataSettingsManager().setMobileDataPolicy(policy, enabled);
changbettyd5c246e2019-12-24 15:40:37 +08009902 } finally {
9903 Binder.restoreCallingIdentity(identity);
9904 }
9905 }
9906
Tyler Gunn7bcdc742019-10-04 15:56:59 -07009907 /**
Hall Liu746e03c2020-09-25 11:13:49 -07009908 * Updates whether conference event package handling is enabled.
Tyler Gunn7bcdc742019-10-04 15:56:59 -07009909 * @param isCepEnabled {@code true} if CEP handling is enabled (default), or {@code false}
9910 * otherwise.
9911 */
9912 @Override
9913 public void setCepEnabled(boolean isCepEnabled) {
9914 TelephonyPermissions.enforceShellOnly(Binder.getCallingUid(), "setCepEnabled");
9915
9916 final long identity = Binder.clearCallingIdentity();
9917 try {
9918 Rlog.i(LOG_TAG, "setCepEnabled isCepEnabled=" + isCepEnabled);
9919 for (Phone phone : PhoneFactory.getPhones()) {
9920 Phone defaultPhone = phone.getImsPhone();
9921 if (defaultPhone != null && defaultPhone.getPhoneType() == PHONE_TYPE_IMS) {
9922 ImsPhone imsPhone = (ImsPhone) defaultPhone;
9923 ImsPhoneCallTracker imsPhoneCallTracker =
9924 (ImsPhoneCallTracker) imsPhone.getCallTracker();
9925 imsPhoneCallTracker.setConferenceEventPackageEnabled(isCepEnabled);
9926 Rlog.i(LOG_TAG, "setCepEnabled isCepEnabled=" + isCepEnabled + ", for imsPhone "
9927 + imsPhone.getMsisdn());
9928 }
9929 }
9930 } finally {
9931 Binder.restoreCallingIdentity(identity);
9932 }
9933 }
allenwtsu46dcc572020-01-08 18:24:03 +08009934
9935 /**
9936 * Notify that an RCS autoconfiguration XML file has been received for provisioning.
9937 *
9938 * @param config The XML file to be read. ASCII/UTF8 encoded text if not compressed.
9939 * @param isCompressed The XML file is compressed in gzip format and must be decompressed
9940 * before being read.
9941 */
9942 @Override
9943 public void notifyRcsAutoConfigurationReceived(int subId, @NonNull byte[] config, boolean
9944 isCompressed) {
9945 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
9946 mApp, subId, "notifyRcsAutoConfigurationReceived");
Hui Wang761a6682020-10-31 05:12:53 +00009947 if (!SubscriptionManager.isValidSubscriptionId(subId)) {
9948 throw new IllegalArgumentException("Invalid Subscription ID: " + subId);
9949 }
9950 if (!isImsAvailableOnDevice()) {
9951 throw new ServiceSpecificException(ImsException.CODE_ERROR_UNSUPPORTED_OPERATION,
9952 "IMS not available on device.");
9953 }
9954
9955 final long identity = Binder.clearCallingIdentity();
allenwtsu46dcc572020-01-08 18:24:03 +08009956 try {
Hui Wang761a6682020-10-31 05:12:53 +00009957 RcsProvisioningMonitor.getInstance().updateConfig(subId, config, isCompressed);
9958 } finally {
9959 Binder.restoreCallingIdentity(identity);
allenwtsu46dcc572020-01-08 18:24:03 +08009960 }
9961 }
zoey chene02881a2019-12-30 16:11:23 +08009962
9963 @Override
9964 public boolean isIccLockEnabled(int subId) {
9965 enforceReadPrivilegedPermission("isIccLockEnabled");
9966
9967 // Now that all security checks passes, perform the operation as ourselves.
9968 final long identity = Binder.clearCallingIdentity();
9969 try {
9970 Phone phone = getPhone(subId);
9971 if (phone != null && phone.getIccCard() != null) {
9972 return phone.getIccCard().getIccLockEnabled();
9973 } else {
9974 return false;
9975 }
9976 } finally {
9977 Binder.restoreCallingIdentity(identity);
9978 }
9979 }
9980
9981 /**
9982 * Set the ICC pin lock enabled or disabled.
9983 *
9984 * @return an integer representing the status of IccLock enabled or disabled in the following
9985 * three cases:
9986 * - {@link TelephonyManager#CHANGE_ICC_LOCK_SUCCESS} if enabled or disabled IccLock
9987 * successfully.
9988 * - Positive number and zero for remaining password attempts.
9989 * - Negative number for other failure cases (such like enabling/disabling PIN failed).
9990 *
9991 */
9992 @Override
9993 public int setIccLockEnabled(int subId, boolean enabled, String password) {
9994 enforceModifyPermission();
9995
9996 Phone phone = getPhone(subId);
9997 if (phone == null) {
9998 return 0;
9999 }
10000 // Now that all security checks passes, perform the operation as ourselves.
10001 final long identity = Binder.clearCallingIdentity();
10002 try {
10003 int attemptsRemaining = (int) sendRequest(CMD_SET_ICC_LOCK_ENABLED,
10004 new Pair<Boolean, String>(enabled, password), phone, null);
10005 return attemptsRemaining;
10006
10007 } catch (Exception e) {
10008 Log.e(LOG_TAG, "setIccLockEnabled. Exception e =" + e);
10009 } finally {
10010 Binder.restoreCallingIdentity(identity);
10011 }
10012 return 0;
10013 }
10014
10015 /**
10016 * Change the ICC password used in ICC pin lock.
10017 *
10018 * @return an integer representing the status of IccLock changed in the following three cases:
10019 * - {@link TelephonyManager#CHANGE_ICC_LOCK_SUCCESS} if changed IccLock successfully.
10020 * - Positive number and zero for remaining password attempts.
10021 * - Negative number for other failure cases (such like enabling/disabling PIN failed).
10022 *
10023 */
10024 @Override
10025 public int changeIccLockPassword(int subId, String oldPassword, String newPassword) {
10026 enforceModifyPermission();
10027
10028 Phone phone = getPhone(subId);
10029 if (phone == null) {
10030 return 0;
10031 }
10032 // Now that all security checks passes, perform the operation as ourselves.
10033 final long identity = Binder.clearCallingIdentity();
10034 try {
10035 int attemptsRemaining = (int) sendRequest(CMD_CHANGE_ICC_LOCK_PASSWORD,
10036 new Pair<String, String>(oldPassword, newPassword), phone, null);
10037 return attemptsRemaining;
10038
10039 } catch (Exception e) {
10040 Log.e(LOG_TAG, "changeIccLockPassword. Exception e =" + e);
10041 } finally {
10042 Binder.restoreCallingIdentity(identity);
10043 }
10044 return 0;
10045 }
Peter Wangdafb9ac2020-01-15 14:13:38 -080010046
10047 /**
10048 * Request for receiving user activity notification
10049 */
10050 @Override
10051 public void requestUserActivityNotification() {
10052 if (!mNotifyUserActivity.get()
10053 && !mMainThreadHandler.hasMessages(MSG_NOTIFY_USER_ACTIVITY)) {
10054 mNotifyUserActivity.set(true);
10055 }
10056 }
10057
10058 /**
10059 * Called when userActivity is signalled in the power manager.
10060 * This is safe to call from any thread, with any window manager locks held or not.
10061 */
10062 @Override
10063 public void userActivity() {
10064 // ***************************************
10065 // * Inherited from PhoneWindowManager *
10066 // ***************************************
10067 // THIS IS CALLED FROM DEEP IN THE POWER MANAGER
10068 // WITH ITS LOCKS HELD.
10069 //
10070 // This code must be VERY careful about the locks
10071 // it acquires.
10072 // In fact, the current code acquires way too many,
10073 // and probably has lurking deadlocks.
10074
10075 if (Binder.getCallingUid() != Process.SYSTEM_UID) {
10076 throw new SecurityException("Only the OS may call notifyUserActivity()");
10077 }
10078
10079 if (mNotifyUserActivity.getAndSet(false)) {
10080 mMainThreadHandler.sendEmptyMessageDelayed(MSG_NOTIFY_USER_ACTIVITY,
10081 USER_ACTIVITY_NOTIFICATION_DELAY);
10082 }
10083 }
Malcolm Chen4639c562020-04-13 11:59:40 -070010084
10085 @Override
10086 public boolean canConnectTo5GInDsdsMode() {
10087 return mApp.getResources().getBoolean(R.bool.config_5g_connection_in_dsds_mode);
10088 }
Jack Yud10cdd42020-09-28 20:28:01 -070010089
10090 @Override
10091 public @NonNull List<String> getEquivalentHomePlmns(int subId, String callingPackage,
10092 String callingFeatureId) {
10093 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
10094 mApp, subId, callingPackage, callingFeatureId, "getEquivalentHomePlmns")) {
10095 throw new SecurityException("Requires READ_PHONE_STATE permission.");
10096 }
10097
10098 Phone phone = getPhone(subId);
10099 if (phone == null) {
10100 throw new RuntimeException("phone is not available");
10101 }
10102 // Now that all security checks passes, perform the operation as ourselves.
10103 final long identity = Binder.clearCallingIdentity();
10104 try {
10105 return phone.getEquivalentHomePlmns();
10106 } finally {
10107 Binder.restoreCallingIdentity(identity);
10108 }
10109 }
Daniel Bright59e67312020-11-13 11:49:37 -080010110
10111 @Override
10112 public boolean isRadioInterfaceCapabilitySupported(
Daniel Bright95a4c1f2021-02-11 09:57:16 -080010113 final @NonNull @TelephonyManager.RadioInterfaceCapability String capability) {
10114 Set<String> radioInterfaceCapabilities =
Daniel Bright94f43662021-03-01 14:43:40 -080010115 mRadioInterfaceCapabilities.getCapabilities();
Daniel Bright59e67312020-11-13 11:49:37 -080010116 if (radioInterfaceCapabilities == null) {
10117 throw new RuntimeException("radio interface capabilities are not available");
Daniel Bright59e67312020-11-13 11:49:37 -080010118 }
Daniel Bright95a4c1f2021-02-11 09:57:16 -080010119 return radioInterfaceCapabilities.contains(capability);
Daniel Bright59e67312020-11-13 11:49:37 -080010120 }
Jack Nudelmanb0b87642020-11-12 15:04:39 -080010121
Hui Wang641e81c2020-10-12 12:14:23 -070010122 @Override
10123 public void bootstrapAuthenticationRequest(int subId, int appType, Uri nafUrl,
10124 UaSecurityProtocolIdentifier securityProtocol,
Brad Ebinger34c09a52021-02-17 23:23:21 +000010125 boolean forceBootStrapping, IBootstrapAuthenticationCallback callback) {
10126 TelephonyPermissions.enforceAnyPermissionGrantedOrCarrierPrivileges(mApp, subId,
10127 Binder.getCallingUid(), "bootstrapAuthenticationRequest",
10128 Manifest.permission.PERFORM_IMS_SINGLE_REGISTRATION,
10129 Manifest.permission.MODIFY_PHONE_STATE);
Hui Wang641e81c2020-10-12 12:14:23 -070010130 if (DBG) {
10131 log("bootstrapAuthenticationRequest, subId:" + subId + ", appType:"
10132 + appType + ", NAF:" + nafUrl + ", sp:" + securityProtocol
10133 + ", forceBootStrapping:" + forceBootStrapping + ", callback:" + callback);
10134 }
10135
10136 if (!SubscriptionManager.isValidSubscriptionId(subId)
10137 || appType < TelephonyManager.APPTYPE_UNKNOWN
10138 || appType > TelephonyManager.APPTYPE_ISIM
10139 || nafUrl == null || securityProtocol == null || callback == null) {
10140 Log.d(LOG_TAG, "bootstrapAuthenticationRequest failed due to invalid parameters");
10141 if (callback != null) {
10142 try {
10143 callback.onAuthenticationFailure(
10144 0, TelephonyManager.GBA_FAILURE_REASON_FEATURE_NOT_SUPPORTED);
10145 } catch (RemoteException exception) {
10146 log("Fail to notify onAuthenticationFailure due to " + exception);
10147 }
10148 return;
10149 }
10150 }
10151
10152 final long token = Binder.clearCallingIdentity();
10153 try {
10154 getGbaManager(subId).bootstrapAuthenticationRequest(
10155 new GbaAuthRequest(subId, appType, nafUrl, securityProtocol.toByteArray(),
10156 forceBootStrapping, callback));
10157 } finally {
10158 Binder.restoreCallingIdentity(token);
10159 }
10160 }
10161
Jack Nudelmanb0b87642020-11-12 15:04:39 -080010162 /**
Sooraj Sasindran72cff492021-07-29 09:42:42 -070010163 * Attempts to set the radio power state for all phones for thermal reason.
10164 * This does not guarantee that the
Jack Nudelmanb0b87642020-11-12 15:04:39 -080010165 * requested radio power state will actually be set. See {@link
10166 * PhoneInternalInterface#setRadioPowerForReason} for more details.
10167 *
Jack Nudelmanb0b87642020-11-12 15:04:39 -080010168 * @param enable {@code true} if trying to turn radio on.
10169 * @return {@code true} if phone setRadioPowerForReason was called. Otherwise, returns {@code
10170 * false}.
10171 */
Sooraj Sasindran72cff492021-07-29 09:42:42 -070010172 private boolean setRadioPowerForThermal(boolean enable) {
10173 boolean isPhoneAvailable = false;
10174 for (int i = 0; i < TelephonyManager.getDefault().getActiveModemCount(); i++) {
10175 Phone phone = PhoneFactory.getPhone(i);
10176 if (phone != null) {
10177 phone.setRadioPowerForReason(enable, Phone.RADIO_POWER_REASON_THERMAL);
10178 isPhoneAvailable = true;
10179 }
Jack Nudelmanb0b87642020-11-12 15:04:39 -080010180 }
Sooraj Sasindran72cff492021-07-29 09:42:42 -070010181
10182 // return true if successfully informed the phone object about the thermal radio power
10183 // request.
10184 return isPhoneAvailable;
Jack Nudelmanb0b87642020-11-12 15:04:39 -080010185 }
10186
10187 private int handleDataThrottlingRequest(int subId,
Sarah Chinecc78c42022-03-31 21:16:48 -070010188 DataThrottlingRequest dataThrottlingRequest, String callingPackage) {
Jack Nudelman5d6a98b2021-03-04 14:26:25 -080010189 boolean isDataThrottlingSupported = isRadioInterfaceCapabilitySupported(
10190 TelephonyManager.CAPABILITY_THERMAL_MITIGATION_DATA_THROTTLING);
10191 if (!isDataThrottlingSupported && dataThrottlingRequest.getDataThrottlingAction()
10192 != DataThrottlingRequest.DATA_THROTTLING_ACTION_NO_DATA_THROTTLING) {
10193 throw new IllegalArgumentException("modem does not support data throttling");
10194 }
10195
Jack Nudelmanb0b87642020-11-12 15:04:39 -080010196 // Ensure that radio is on. If not able to power on due to phone being unavailable, return
10197 // THERMAL_MITIGATION_RESULT_MODEM_NOT_AVAILABLE.
Sooraj Sasindran72cff492021-07-29 09:42:42 -070010198 if (!setRadioPowerForThermal(true)) {
Jack Nudelmanb0b87642020-11-12 15:04:39 -080010199 return TelephonyManager.THERMAL_MITIGATION_RESULT_MODEM_NOT_AVAILABLE;
10200 }
10201
Sarah Chinecc78c42022-03-31 21:16:48 -070010202 setDataEnabledForReason(
10203 subId, TelephonyManager.DATA_ENABLED_REASON_THERMAL, true, callingPackage);
Jack Nudelmanb0b87642020-11-12 15:04:39 -080010204
Jack Nudelman5d6a98b2021-03-04 14:26:25 -080010205 if (isDataThrottlingSupported) {
10206 int thermalMitigationResult =
Jack Nudelmanb0b87642020-11-12 15:04:39 -080010207 (int) sendRequest(CMD_SET_DATA_THROTTLING, dataThrottlingRequest, subId);
Jack Nudelman5d6a98b2021-03-04 14:26:25 -080010208 if (thermalMitigationResult == SET_DATA_THROTTLING_MODEM_THREW_INVALID_PARAMS) {
10209 throw new IllegalArgumentException("modem returned INVALID_ARGUMENTS");
10210 } else if (thermalMitigationResult
10211 == MODEM_DOES_NOT_SUPPORT_DATA_THROTTLING_ERROR_CODE) {
Jack Nudelman760d0962021-05-20 13:57:30 -070010212 log("Modem likely does not support data throttling on secondary carrier. Data " +
10213 "throttling action = " + dataThrottlingRequest.getDataThrottlingAction());
10214 return TelephonyManager.THERMAL_MITIGATION_RESULT_MODEM_ERROR;
Jack Nudelman5d6a98b2021-03-04 14:26:25 -080010215 }
10216 return thermalMitigationResult;
Jack Nudelmanb0b87642020-11-12 15:04:39 -080010217 }
Jack Nudelman5d6a98b2021-03-04 14:26:25 -080010218
10219 return TelephonyManager.THERMAL_MITIGATION_RESULT_SUCCESS;
Jack Nudelmanb0b87642020-11-12 15:04:39 -080010220 }
10221
Jack Nudelman644b91a2021-03-12 14:09:48 -080010222 private static List<String> getThermalMitigationAllowlist(Context context) {
10223 if (sThermalMitigationAllowlistedPackages.isEmpty()) {
10224 for (String pckg : context.getResources()
10225 .getStringArray(R.array.thermal_mitigation_allowlisted_packages)) {
10226 sThermalMitigationAllowlistedPackages.add(pckg);
10227 }
10228 }
10229
10230 return sThermalMitigationAllowlistedPackages;
10231 }
10232
Jack Nudelmane69bbc82021-05-13 10:00:15 -070010233 private boolean isAnyPhoneInEmergencyState() {
10234 TelecomManager tm = mApp.getSystemService(TelecomManager.class);
10235 if (tm.isInEmergencyCall()) {
10236 Log.e(LOG_TAG , "Phone state is not valid. One of the phones is in an emergency call");
10237 return true;
10238 }
10239 for (Phone phone : PhoneFactory.getPhones()) {
10240 if (phone.isInEmergencySmsMode() || phone.isInEcm()) {
10241 Log.e(LOG_TAG, "Phone state is not valid. isInEmergencySmsMode = "
10242 + phone.isInEmergencySmsMode() + " isInEmergencyCallbackMode = "
10243 + phone.isInEcm());
10244 return true;
10245 }
10246 }
10247
10248 return false;
10249 }
10250
Jack Nudelman644b91a2021-03-12 14:09:48 -080010251 /**
10252 * Used by shell commands to add an authorized package name for thermal mitigation.
10253 * @param packageName name of package to be allowlisted
10254 * @param context
10255 */
10256 static void addPackageToThermalMitigationAllowlist(String packageName, Context context) {
10257 sThermalMitigationAllowlistedPackages = getThermalMitigationAllowlist(context);
10258 sThermalMitigationAllowlistedPackages.add(packageName);
10259 }
10260
10261 /**
10262 * Used by shell commands to remove an authorized package name for thermal mitigation.
10263 * @param packageName name of package to remove from allowlist
10264 * @param context
10265 */
10266 static void removePackageFromThermalMitigationAllowlist(String packageName, Context context) {
10267 sThermalMitigationAllowlistedPackages = getThermalMitigationAllowlist(context);
10268 sThermalMitigationAllowlistedPackages.remove(packageName);
10269 }
10270
Jack Nudelmanb0b87642020-11-12 15:04:39 -080010271 /**
10272 * Thermal mitigation request to control functionalities at modem.
10273 *
10274 * @param subId the id of the subscription.
10275 * @param thermalMitigationRequest holds all necessary information to be passed down to modem.
Jack Nudelman644b91a2021-03-12 14:09:48 -080010276 * @param callingPackage the package name of the calling package.
Jack Nudelmanb0b87642020-11-12 15:04:39 -080010277 *
10278 * @return thermalMitigationResult enum as defined in android.telephony.Annotation.
10279 */
10280 @Override
10281 @ThermalMitigationResult
10282 public int sendThermalMitigationRequest(
10283 int subId,
Jack Nudelman644b91a2021-03-12 14:09:48 -080010284 ThermalMitigationRequest thermalMitigationRequest,
10285 String callingPackage) throws IllegalArgumentException {
Jack Nudelmanb0b87642020-11-12 15:04:39 -080010286 enforceModifyPermission();
10287
Jack Nudelman644b91a2021-03-12 14:09:48 -080010288 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
10289 if (!getThermalMitigationAllowlist(getDefaultPhone().getContext())
10290 .contains(callingPackage)) {
10291 throw new SecurityException("Calling package must be configured in the device config. "
10292 + "calling package: " + callingPackage);
10293 }
10294
Jack Nudelmanb0b87642020-11-12 15:04:39 -080010295 WorkSource workSource = getWorkSource(Binder.getCallingUid());
10296 final long identity = Binder.clearCallingIdentity();
10297
10298 int thermalMitigationResult = TelephonyManager.THERMAL_MITIGATION_RESULT_UNKNOWN_ERROR;
10299 try {
10300 int thermalMitigationAction = thermalMitigationRequest.getThermalMitigationAction();
10301 switch (thermalMitigationAction) {
10302 case ThermalMitigationRequest.THERMAL_MITIGATION_ACTION_DATA_THROTTLING:
10303 thermalMitigationResult =
10304 handleDataThrottlingRequest(subId,
Sarah Chinecc78c42022-03-31 21:16:48 -070010305 thermalMitigationRequest.getDataThrottlingRequest(),
10306 callingPackage);
Jack Nudelmanb0b87642020-11-12 15:04:39 -080010307 break;
10308 case ThermalMitigationRequest.THERMAL_MITIGATION_ACTION_VOICE_ONLY:
10309 if (thermalMitigationRequest.getDataThrottlingRequest() != null) {
10310 throw new IllegalArgumentException("dataThrottlingRequest must be null for "
10311 + "ThermalMitigationRequest.THERMAL_MITIGATION_ACTION_VOICE_ONLY");
10312 }
10313
10314 // Ensure that radio is on. If not able to power on due to phone being
10315 // unavailable, return THERMAL_MITIGATION_RESULT_MODEM_NOT_AVAILABLE.
Sooraj Sasindran72cff492021-07-29 09:42:42 -070010316 if (!setRadioPowerForThermal(true)) {
Jack Nudelmanb0b87642020-11-12 15:04:39 -080010317 thermalMitigationResult =
10318 TelephonyManager.THERMAL_MITIGATION_RESULT_MODEM_NOT_AVAILABLE;
10319 break;
10320 }
10321
10322 setDataEnabledForReason(subId, TelephonyManager.DATA_ENABLED_REASON_THERMAL,
Sarah Chinecc78c42022-03-31 21:16:48 -070010323 false, callingPackage);
Jack Nudelmanb0b87642020-11-12 15:04:39 -080010324 thermalMitigationResult = TelephonyManager.THERMAL_MITIGATION_RESULT_SUCCESS;
10325 break;
10326 case ThermalMitigationRequest.THERMAL_MITIGATION_ACTION_RADIO_OFF:
10327 if (thermalMitigationRequest.getDataThrottlingRequest() != null) {
10328 throw new IllegalArgumentException("dataThrottlingRequest must be null for"
10329 + " ThermalMitigationRequest.THERMAL_MITIGATION_ACTION_RADIO_OFF");
10330 }
10331
10332 TelecomAccountRegistry registry = TelecomAccountRegistry.getInstance(null);
10333 if (registry != null) {
Jack Nudelmanb0b87642020-11-12 15:04:39 -080010334 Phone phone = getPhone(subId);
10335 if (phone == null) {
10336 thermalMitigationResult =
10337 TelephonyManager.THERMAL_MITIGATION_RESULT_MODEM_NOT_AVAILABLE;
10338 break;
10339 }
10340
Jack Nudelmane69bbc82021-05-13 10:00:15 -070010341 TelephonyConnectionService service =
10342 registry.getTelephonyConnectionService();
Jack Nudelmanb30ac302021-06-17 15:39:58 -070010343 if (service != null && service.isEmergencyCallPending()) {
Jack Nudelmane69bbc82021-05-13 10:00:15 -070010344 Log.e(LOG_TAG, "An emergency call is pending");
10345 thermalMitigationResult =
10346 TelephonyManager.THERMAL_MITIGATION_RESULT_INVALID_STATE;
10347 break;
10348 } else if (isAnyPhoneInEmergencyState()) {
Jack Nudelmanb0b87642020-11-12 15:04:39 -080010349 thermalMitigationResult =
10350 TelephonyManager.THERMAL_MITIGATION_RESULT_INVALID_STATE;
10351 break;
10352 }
10353 } else {
10354 thermalMitigationResult =
10355 TelephonyManager.THERMAL_MITIGATION_RESULT_MODEM_NOT_AVAILABLE;
10356 break;
10357 }
10358
10359 // Turn radio off. If not able to power off due to phone being unavailable,
10360 // return THERMAL_MITIGATION_RESULT_MODEM_NOT_AVAILABLE.
Sooraj Sasindran72cff492021-07-29 09:42:42 -070010361 if (!setRadioPowerForThermal(false)) {
Jack Nudelmanb0b87642020-11-12 15:04:39 -080010362 thermalMitigationResult =
10363 TelephonyManager.THERMAL_MITIGATION_RESULT_MODEM_NOT_AVAILABLE;
10364 break;
10365 }
10366 thermalMitigationResult =
10367 TelephonyManager.THERMAL_MITIGATION_RESULT_SUCCESS;
10368 break;
10369 default:
10370 throw new IllegalArgumentException("the requested thermalMitigationAction does "
10371 + "not exist. Requested action: " + thermalMitigationAction);
10372 }
10373 } catch (IllegalArgumentException e) {
10374 throw e;
10375 } catch (Exception e) {
10376 Log.e(LOG_TAG, "thermalMitigationRequest. Exception e =" + e);
10377 thermalMitigationResult = TelephonyManager.THERMAL_MITIGATION_RESULT_MODEM_ERROR;
10378 } finally {
10379 Binder.restoreCallingIdentity(identity);
10380 }
10381
10382 if (DBG) {
10383 log("thermalMitigationRequest returning with thermalMitigationResult: "
10384 + thermalMitigationResult);
10385 }
10386
10387 return thermalMitigationResult;
10388 }
Hui Wang641e81c2020-10-12 12:14:23 -070010389
10390 /**
10391 * Set the GbaService Package Name that Telephony will bind to.
10392 *
10393 * @param subId The sim that the GbaService is associated with.
10394 * @param packageName The name of the package to be replaced with.
10395 * @return true if setting the GbaService to bind to succeeded, false if it did not.
10396 */
10397 @Override
10398 public boolean setBoundGbaServiceOverride(int subId, String packageName) {
10399 enforceModifyPermission();
10400
10401 final long identity = Binder.clearCallingIdentity();
10402 try {
10403 return getGbaManager(subId).overrideServicePackage(packageName);
10404 } finally {
10405 Binder.restoreCallingIdentity(identity);
10406 }
10407 }
10408
10409 /**
10410 * Return the package name of the currently bound GbaService.
10411 *
10412 * @param subId The sim that the GbaService is associated with.
10413 * @return the package name of the GbaService configuration, null if GBA is not supported.
10414 */
10415 @Override
10416 public String getBoundGbaService(int subId) {
10417 enforceReadPrivilegedPermission("getBoundGbaServicePackage");
10418
10419 final long identity = Binder.clearCallingIdentity();
10420 try {
10421 return getGbaManager(subId).getServicePackage();
10422 } finally {
10423 Binder.restoreCallingIdentity(identity);
10424 }
10425 }
10426
10427 /**
10428 * Set the release time for telephony to unbind GbaService.
10429 *
10430 * @param subId The sim that the GbaService is associated with.
10431 * @param interval The release time to unbind GbaService by millisecond.
10432 * @return true if setting the GbaService to bind to succeeded, false if it did not.
10433 */
10434 @Override
10435 public boolean setGbaReleaseTimeOverride(int subId, int interval) {
10436 enforceModifyPermission();
10437
10438 final long identity = Binder.clearCallingIdentity();
10439 try {
10440 return getGbaManager(subId).overrideReleaseTime(interval);
10441 } finally {
10442 Binder.restoreCallingIdentity(identity);
10443 }
10444 }
10445
10446 /**
10447 * Return the release time for telephony to unbind GbaService.
10448 *
10449 * @param subId The sim that the GbaService is associated with.
10450 * @return The release time to unbind GbaService by millisecond.
10451 */
10452 @Override
10453 public int getGbaReleaseTime(int subId) {
10454 enforceReadPrivilegedPermission("getGbaReleaseTime");
10455
10456 final long identity = Binder.clearCallingIdentity();
10457 try {
10458 return getGbaManager(subId).getReleaseTime();
10459 } finally {
10460 Binder.restoreCallingIdentity(identity);
10461 }
10462 }
10463
10464 private GbaManager getGbaManager(int subId) {
10465 GbaManager instance = GbaManager.getInstance(subId);
10466 if (instance == null) {
10467 String packageName = mApp.getResources().getString(R.string.config_gba_package);
10468 int releaseTime = mApp.getResources().getInteger(R.integer.config_gba_release_time);
10469 instance = GbaManager.make(mApp, subId, packageName, releaseTime);
10470 }
10471 return instance;
10472 }
Hui Wang761a6682020-10-31 05:12:53 +000010473
10474 /**
10475 * indicate whether the device and the carrier can support
10476 * RCS VoLTE single registration.
10477 */
10478 @Override
10479 public boolean isRcsVolteSingleRegistrationCapable(int subId) {
Brad Ebinger34c09a52021-02-17 23:23:21 +000010480 TelephonyPermissions.enforceAnyPermissionGrantedOrCarrierPrivileges(mApp, subId,
10481 Binder.getCallingUid(), "isRcsVolteSingleRegistrationCapable",
10482 Manifest.permission.PERFORM_IMS_SINGLE_REGISTRATION,
10483 permission.READ_PRIVILEGED_PHONE_STATE);
Hui Wang761a6682020-10-31 05:12:53 +000010484
10485 if (!SubscriptionManager.isValidSubscriptionId(subId)) {
10486 throw new IllegalArgumentException("Invalid Subscription ID: " + subId);
10487 }
10488
10489 final long identity = Binder.clearCallingIdentity();
10490 try {
10491 RcsProvisioningMonitor rpm = RcsProvisioningMonitor.getInstance();
10492 if (rpm != null) {
Hui Wang67af90e2021-06-04 16:57:15 -070010493 Boolean isCapable = rpm.isRcsVolteSingleRegistrationEnabled(subId);
10494 if (isCapable != null) {
10495 return isCapable;
10496 }
Hui Wang761a6682020-10-31 05:12:53 +000010497 }
Hui Wang67af90e2021-06-04 16:57:15 -070010498 throw new ServiceSpecificException(ImsException.CODE_ERROR_SERVICE_UNAVAILABLE,
10499 "service is temporarily unavailable.");
Hui Wang761a6682020-10-31 05:12:53 +000010500 } finally {
10501 Binder.restoreCallingIdentity(identity);
10502 }
10503 }
10504
10505 /**
10506 * Register RCS provisioning callback.
10507 */
10508 @Override
Hui Wang3cac7e52021-01-27 14:45:25 -080010509 public void registerRcsProvisioningCallback(int subId,
Hui Wang761a6682020-10-31 05:12:53 +000010510 IRcsConfigCallback callback) {
Brad Ebinger34c09a52021-02-17 23:23:21 +000010511 TelephonyPermissions.enforceAnyPermissionGrantedOrCarrierPrivileges(mApp, subId,
Hui Wang3cac7e52021-01-27 14:45:25 -080010512 Binder.getCallingUid(), "registerRcsProvisioningCallback",
Brad Ebinger34c09a52021-02-17 23:23:21 +000010513 Manifest.permission.PERFORM_IMS_SINGLE_REGISTRATION,
10514 permission.READ_PRIVILEGED_PHONE_STATE);
Hui Wang761a6682020-10-31 05:12:53 +000010515
10516 if (!SubscriptionManager.isValidSubscriptionId(subId)) {
10517 throw new IllegalArgumentException("Invalid Subscription ID: " + subId);
10518 }
10519 if (!isImsAvailableOnDevice()) {
10520 throw new ServiceSpecificException(ImsException.CODE_ERROR_UNSUPPORTED_OPERATION,
10521 "IMS not available on device.");
10522 }
10523
10524 final long identity = Binder.clearCallingIdentity();
10525 try {
Hui Wang68cd3722021-01-11 20:04:53 -080010526 if (!RcsProvisioningMonitor.getInstance()
Hui Wang3cac7e52021-01-27 14:45:25 -080010527 .registerRcsProvisioningCallback(subId, callback)) {
Brad Ebinger919631e2021-06-02 17:46:35 -070010528 throw new ServiceSpecificException(ImsException.CODE_ERROR_INVALID_SUBSCRIPTION,
10529 "Active subscription not found.");
Hui Wang68cd3722021-01-11 20:04:53 -080010530 }
Hui Wang761a6682020-10-31 05:12:53 +000010531 } finally {
10532 Binder.restoreCallingIdentity(identity);
10533 }
10534 }
10535
10536 /**
10537 * Unregister RCS provisioning callback.
10538 */
10539 @Override
Hui Wang3cac7e52021-01-27 14:45:25 -080010540 public void unregisterRcsProvisioningCallback(int subId,
Hui Wang761a6682020-10-31 05:12:53 +000010541 IRcsConfigCallback callback) {
Brad Ebinger34c09a52021-02-17 23:23:21 +000010542 TelephonyPermissions.enforceAnyPermissionGrantedOrCarrierPrivileges(mApp, subId,
Hui Wang3cac7e52021-01-27 14:45:25 -080010543 Binder.getCallingUid(), "unregisterRcsProvisioningCallback",
Brad Ebinger34c09a52021-02-17 23:23:21 +000010544 Manifest.permission.PERFORM_IMS_SINGLE_REGISTRATION,
10545 permission.READ_PRIVILEGED_PHONE_STATE);
Hui Wang761a6682020-10-31 05:12:53 +000010546
10547 if (!SubscriptionManager.isValidSubscriptionId(subId)) {
10548 throw new IllegalArgumentException("Invalid Subscription ID: " + subId);
10549 }
10550 if (!isImsAvailableOnDevice()) {
10551 throw new ServiceSpecificException(ImsException.CODE_ERROR_UNSUPPORTED_OPERATION,
10552 "IMS not available on device.");
10553 }
10554
10555 final long identity = Binder.clearCallingIdentity();
10556 try {
Hui Wang68cd3722021-01-11 20:04:53 -080010557 RcsProvisioningMonitor.getInstance()
Hui Wang3cac7e52021-01-27 14:45:25 -080010558 .unregisterRcsProvisioningCallback(subId, callback);
Hui Wang761a6682020-10-31 05:12:53 +000010559 } finally {
10560 Binder.restoreCallingIdentity(identity);
10561 }
10562 }
10563
10564 /**
10565 * trigger RCS reconfiguration.
10566 */
10567 public void triggerRcsReconfiguration(int subId) {
Brad Ebinger34c09a52021-02-17 23:23:21 +000010568 TelephonyPermissions.enforceAnyPermissionGranted(mApp, Binder.getCallingUid(),
10569 "triggerRcsReconfiguration",
10570 Manifest.permission.PERFORM_IMS_SINGLE_REGISTRATION);
Hui Wang761a6682020-10-31 05:12:53 +000010571
10572 if (!SubscriptionManager.isValidSubscriptionId(subId)) {
10573 throw new IllegalArgumentException("Invalid Subscription ID: " + subId);
10574 }
10575 if (!isImsAvailableOnDevice()) {
10576 throw new ServiceSpecificException(ImsException.CODE_ERROR_UNSUPPORTED_OPERATION,
10577 "IMS not available on device.");
10578 }
10579
10580 final long identity = Binder.clearCallingIdentity();
10581 try {
10582 RcsProvisioningMonitor.getInstance().requestReconfig(subId);
10583 } finally {
10584 Binder.restoreCallingIdentity(identity);
10585 }
10586 }
10587
10588 /**
10589 * Provide the client configuration parameters of the RCS application.
10590 */
10591 public void setRcsClientConfiguration(int subId, RcsClientConfiguration rcc) {
Brad Ebinger34c09a52021-02-17 23:23:21 +000010592 TelephonyPermissions.enforceAnyPermissionGranted(mApp, Binder.getCallingUid(),
10593 "setRcsClientConfiguration",
10594 Manifest.permission.PERFORM_IMS_SINGLE_REGISTRATION);
Hui Wang761a6682020-10-31 05:12:53 +000010595
10596 if (!SubscriptionManager.isValidSubscriptionId(subId)) {
10597 throw new IllegalArgumentException("Invalid Subscription ID: " + subId);
10598 }
10599 if (!isImsAvailableOnDevice()) {
10600 throw new ServiceSpecificException(ImsException.CODE_ERROR_UNSUPPORTED_OPERATION,
10601 "IMS not available on device.");
10602 }
10603
10604 final long identity = Binder.clearCallingIdentity();
10605
10606 try {
10607 IImsConfig configBinder = getImsConfig(getSlotIndex(subId), ImsFeature.FEATURE_RCS);
10608 if (configBinder == null) {
10609 Rlog.e(LOG_TAG, "null result for setRcsClientConfiguration");
Brad Ebinger919631e2021-06-02 17:46:35 -070010610 throw new ServiceSpecificException(ImsException.CODE_ERROR_INVALID_SUBSCRIPTION,
10611 "could not find the requested subscription");
Hui Wang761a6682020-10-31 05:12:53 +000010612 } else {
10613 configBinder.setRcsClientConfiguration(rcc);
10614 }
joonhunshin3e154242021-09-17 06:33:39 +000010615
10616 RcsStats.getInstance().onRcsClientProvisioningStats(subId,
10617 RCS_CLIENT_PROVISIONING_STATS__EVENT__CLIENT_PARAMS_SENT);
Hui Wang761a6682020-10-31 05:12:53 +000010618 } catch (RemoteException e) {
10619 Rlog.e(LOG_TAG, "fail to setRcsClientConfiguration " + e.getMessage());
Brad Ebinger919631e2021-06-02 17:46:35 -070010620 throw new ServiceSpecificException(ImsException.CODE_ERROR_SERVICE_UNAVAILABLE,
10621 "service is temporarily unavailable.");
Hui Wang761a6682020-10-31 05:12:53 +000010622 } finally {
10623 Binder.restoreCallingIdentity(identity);
10624 }
10625 }
10626
10627 /**
Hui Wangbaaee6a2021-02-19 20:45:36 -080010628 * Enables or disables the test mode for RCS VoLTE single registration.
10629 */
10630 @Override
10631 public void setRcsSingleRegistrationTestModeEnabled(boolean enabled) {
10632 TelephonyPermissions.enforceShellOnly(Binder.getCallingUid(),
10633 "setRcsSingleRegistrationTestModeEnabled");
10634
10635 RcsProvisioningMonitor.getInstance().setTestModeEnabled(enabled);
10636 }
10637
10638 /**
10639 * Gets the test mode for RCS VoLTE single registration.
10640 */
10641 @Override
10642 public boolean getRcsSingleRegistrationTestModeEnabled() {
10643 TelephonyPermissions.enforceShellOnly(Binder.getCallingUid(),
10644 "getRcsSingleRegistrationTestModeEnabled");
10645
10646 return RcsProvisioningMonitor.getInstance().getTestModeEnabled();
10647 }
10648
10649 /**
Hui Wang761a6682020-10-31 05:12:53 +000010650 * Overrides the config of RCS VoLTE single registration enabled for the device.
10651 */
10652 @Override
10653 public void setDeviceSingleRegistrationEnabledOverride(String enabledStr) {
10654 TelephonyPermissions.enforceShellOnly(Binder.getCallingUid(),
10655 "setDeviceSingleRegistrationEnabledOverride");
10656 enforceModifyPermission();
10657
10658 Boolean enabled = "NULL".equalsIgnoreCase(enabledStr) ? null
10659 : Boolean.parseBoolean(enabledStr);
10660 RcsProvisioningMonitor.getInstance().overrideDeviceSingleRegistrationEnabled(enabled);
Brad Ebinger49a72b42021-01-29 00:55:24 +000010661 mApp.imsRcsController.setDeviceSingleRegistrationSupportOverride(enabled);
Hui Wang761a6682020-10-31 05:12:53 +000010662 }
10663
10664 /**
Tyler Gunn92479152021-01-20 16:30:10 -080010665 * Sends a device to device communication message. Only usable via shell.
10666 * @param message message to send.
10667 * @param value message value.
10668 */
10669 @Override
10670 public void sendDeviceToDeviceMessage(int message, int value) {
10671 TelephonyPermissions.enforceShellOnly(Binder.getCallingUid(),
Tyler Gunnbabbda02021-02-10 11:05:02 -080010672 "sendDeviceToDeviceMessage");
Tyler Gunn92479152021-01-20 16:30:10 -080010673 enforceModifyPermission();
10674
10675 final long identity = Binder.clearCallingIdentity();
10676 try {
10677 TelephonyConnectionService service =
10678 TelecomAccountRegistry.getInstance(null).getTelephonyConnectionService();
10679 if (service == null) {
10680 Rlog.e(LOG_TAG, "sendDeviceToDeviceMessage: not in a call.");
10681 return;
10682 }
10683 service.sendTestDeviceToDeviceMessage(message, value);
10684 } finally {
10685 Binder.restoreCallingIdentity(identity);
10686 }
10687 }
10688
Tyler Gunnbabbda02021-02-10 11:05:02 -080010689 /**
10690 * Sets the specified device to device transport active.
10691 * @param transport The transport to set active.
10692 */
10693 @Override
10694 public void setActiveDeviceToDeviceTransport(@NonNull String transport) {
10695 TelephonyPermissions.enforceShellOnly(Binder.getCallingUid(),
10696 "setActiveDeviceToDeviceTransport");
10697 enforceModifyPermission();
10698
10699 final long identity = Binder.clearCallingIdentity();
10700 try {
10701 TelephonyConnectionService service =
10702 TelecomAccountRegistry.getInstance(null).getTelephonyConnectionService();
10703 if (service == null) {
10704 Rlog.e(LOG_TAG, "setActiveDeviceToDeviceTransport: not in a call.");
10705 return;
10706 }
10707 service.setActiveDeviceToDeviceTransport(transport);
10708 } finally {
10709 Binder.restoreCallingIdentity(identity);
10710 }
10711 }
Tyler Gunn92479152021-01-20 16:30:10 -080010712
Tyler Gunnd4339262021-05-03 14:46:49 -070010713 @Override
10714 public void setDeviceToDeviceForceEnabled(boolean isForceEnabled) {
10715 TelephonyPermissions.enforceShellOnly(Binder.getCallingUid(),
10716 "setDeviceToDeviceForceEnabled");
10717
10718 final long identity = Binder.clearCallingIdentity();
10719 try {
10720 Arrays.stream(PhoneFactory.getPhones()).forEach(
10721 p -> {
10722 Phone thePhone = p.getImsPhone();
10723 if (thePhone != null && thePhone instanceof ImsPhone) {
10724 ImsPhone imsPhone = (ImsPhone) thePhone;
10725 CallTracker tracker = imsPhone.getCallTracker();
10726 if (tracker != null && tracker instanceof ImsPhoneCallTracker) {
10727 ImsPhoneCallTracker imsPhoneCallTracker =
10728 (ImsPhoneCallTracker) tracker;
10729 imsPhoneCallTracker.setDeviceToDeviceForceEnabled(isForceEnabled);
10730 }
10731 }
10732 }
10733 );
10734 } finally {
10735 Binder.restoreCallingIdentity(identity);
10736 }
10737 }
10738
Tyler Gunn92479152021-01-20 16:30:10 -080010739 /**
Hui Wang761a6682020-10-31 05:12:53 +000010740 * Gets the config of RCS VoLTE single registration enabled for the device.
10741 */
10742 @Override
10743 public boolean getDeviceSingleRegistrationEnabled() {
10744 enforceReadPrivilegedPermission("getDeviceSingleRegistrationEnabled");
10745 return RcsProvisioningMonitor.getInstance().getDeviceSingleRegistrationEnabled();
10746 }
10747
10748 /**
10749 * Overrides the config of RCS VoLTE single registration enabled for the carrier/subscription.
10750 */
10751 @Override
10752 public boolean setCarrierSingleRegistrationEnabledOverride(int subId, String enabledStr) {
10753 TelephonyPermissions.enforceShellOnly(Binder.getCallingUid(),
10754 "setCarrierSingleRegistrationEnabledOverride");
10755 enforceModifyPermission();
10756
10757 Boolean enabled = "NULL".equalsIgnoreCase(enabledStr) ? null
10758 : Boolean.parseBoolean(enabledStr);
10759 return RcsProvisioningMonitor.getInstance().overrideCarrierSingleRegistrationEnabled(
10760 subId, enabled);
10761 }
10762
10763 /**
10764 * Gets the config of RCS VoLTE single registration enabled for the carrier/subscription.
10765 */
10766 @Override
10767 public boolean getCarrierSingleRegistrationEnabled(int subId) {
10768 enforceReadPrivilegedPermission("getCarrierSingleRegistrationEnabled");
10769 return RcsProvisioningMonitor.getInstance().getCarrierSingleRegistrationEnabled(subId);
10770 }
Chiachang Wangd6d34772020-12-22 11:38:27 +080010771
10772 /**
Hui Wangb647abe2021-02-26 09:33:38 -080010773 * Overrides the ims feature validation result
10774 */
10775 @Override
10776 public boolean setImsFeatureValidationOverride(int subId, String enabledStr) {
10777 TelephonyPermissions.enforceShellOnly(Binder.getCallingUid(),
10778 "setImsFeatureValidationOverride");
10779
10780 Boolean enabled = "NULL".equalsIgnoreCase(enabledStr) ? null
10781 : Boolean.parseBoolean(enabledStr);
10782 return RcsProvisioningMonitor.getInstance().overrideImsFeatureValidation(
10783 subId, enabled);
10784 }
10785
10786 /**
10787 * Gets the ims feature validation override value
10788 */
10789 @Override
10790 public boolean getImsFeatureValidationOverride(int subId) {
10791 TelephonyPermissions.enforceShellOnly(Binder.getCallingUid(),
10792 "getImsFeatureValidationOverride");
10793 return RcsProvisioningMonitor.getInstance().getImsFeatureValidationOverride(subId);
10794 }
10795
10796 /**
Chiachang Wangd6d34772020-12-22 11:38:27 +080010797 * Get the mobile provisioning url that is used to launch a browser to allow users to manage
10798 * their mobile plan.
10799 */
10800 @Override
10801 public String getMobileProvisioningUrl() {
10802 enforceReadPrivilegedPermission("getMobileProvisioningUrl");
10803 final long identity = Binder.clearCallingIdentity();
10804 try {
10805 return getDefaultPhone().getMobileProvisioningUrl();
10806 } finally {
10807 Binder.restoreCallingIdentity(identity);
10808 }
10809 }
Rambo Wanga5cc9b72021-01-07 10:51:54 -080010810
James.cf Linbcdf8b32021-01-14 16:44:13 +080010811 /**
calvinpane4a8a1d2021-01-25 13:51:18 +080010812 * Get the EAB contact from the EAB database.
10813 */
10814 @Override
10815 public String getContactFromEab(String contact) {
10816 TelephonyPermissions.enforceShellOnly(Binder.getCallingUid(), "getContactFromEab");
10817 enforceModifyPermission();
10818 final long identity = Binder.clearCallingIdentity();
10819 try {
10820 return EabUtil.getContactFromEab(getDefaultPhone().getContext(), contact);
10821 } finally {
10822 Binder.restoreCallingIdentity(identity);
10823 }
10824 }
10825
10826 /**
Calvin Pana1434322021-07-01 19:27:01 +080010827 * Get the EAB capability from the EAB database.
10828 */
10829 @Override
10830 public String getCapabilityFromEab(String contact) {
10831 TelephonyPermissions.enforceShellOnly(Binder.getCallingUid(), "getCapabilityFromEab");
10832 enforceModifyPermission();
10833 final long identity = Binder.clearCallingIdentity();
10834 try {
10835 return EabUtil.getCapabilityFromEab(getDefaultPhone().getContext(), contact);
10836 } finally {
10837 Binder.restoreCallingIdentity(identity);
10838 }
10839 }
10840
10841 /**
James.cf Linbcdf8b32021-01-14 16:44:13 +080010842 * Remove the EAB contacts from the EAB database.
10843 */
10844 @Override
10845 public int removeContactFromEab(int subId, String contacts) {
10846 TelephonyPermissions.enforceShellOnly(Binder.getCallingUid(), "removeCapabilitiesFromEab");
10847 enforceModifyPermission();
10848 final long identity = Binder.clearCallingIdentity();
10849 try {
10850 return EabUtil.removeContactFromEab(subId, contacts, getDefaultPhone().getContext());
10851 } finally {
10852 Binder.restoreCallingIdentity(identity);
10853 }
10854 }
10855
Rambo Wanga5cc9b72021-01-07 10:51:54 -080010856 @Override
James.cf Lin4b784aa2021-01-31 03:25:15 +080010857 public boolean getDeviceUceEnabled() {
10858 TelephonyPermissions.enforceShellOnly(Binder.getCallingUid(), "getDeviceUceEnabled");
10859 final long identity = Binder.clearCallingIdentity();
10860 try {
10861 return mApp.getDeviceUceEnabled();
10862 } finally {
10863 Binder.restoreCallingIdentity(identity);
10864 }
10865 }
10866
10867 @Override
10868 public void setDeviceUceEnabled(boolean isEnabled) {
10869 TelephonyPermissions.enforceShellOnly(Binder.getCallingUid(), "setDeviceUceEnabled");
10870 final long identity = Binder.clearCallingIdentity();
10871 try {
10872 mApp.setDeviceUceEnabled(isEnabled);
10873 } finally {
10874 Binder.restoreCallingIdentity(identity);
10875 }
10876 }
10877
Brad Ebinger14d467f2021-02-12 06:18:28 +000010878 /**
10879 * Add new feature tags to the Set used to calculate the capabilities in PUBLISH.
10880 * @return current RcsContactUceCapability instance that will be used for PUBLISH.
10881 */
10882 // Used for SHELL command only right now.
10883 @Override
10884 public RcsContactUceCapability addUceRegistrationOverrideShell(int subId,
10885 List<String> featureTags) {
10886 TelephonyPermissions.enforceShellOnly(Binder.getCallingUid(),
10887 "addUceRegistrationOverrideShell");
10888 final long identity = Binder.clearCallingIdentity();
10889 try {
10890 return mApp.imsRcsController.addUceRegistrationOverrideShell(subId,
10891 new ArraySet<>(featureTags));
10892 } catch (ImsException e) {
10893 throw new ServiceSpecificException(e.getCode(), e.getMessage());
10894 } finally {
10895 Binder.restoreCallingIdentity(identity);
10896 }
10897 }
10898
10899 /**
10900 * Remove existing feature tags to the Set used to calculate the capabilities in PUBLISH.
10901 * @return current RcsContactUceCapability instance that will be used for PUBLISH.
10902 */
10903 // Used for SHELL command only right now.
10904 @Override
10905 public RcsContactUceCapability removeUceRegistrationOverrideShell(int subId,
10906 List<String> featureTags) {
10907 TelephonyPermissions.enforceShellOnly(Binder.getCallingUid(),
10908 "removeUceRegistrationOverrideShell");
10909 final long identity = Binder.clearCallingIdentity();
10910 try {
10911 return mApp.imsRcsController.removeUceRegistrationOverrideShell(subId,
10912 new ArraySet<>(featureTags));
10913 } catch (ImsException e) {
10914 throw new ServiceSpecificException(e.getCode(), e.getMessage());
10915 } finally {
10916 Binder.restoreCallingIdentity(identity);
10917 }
10918 }
10919
10920 /**
10921 * Clear all overrides in the Set used to calculate the capabilities in PUBLISH.
10922 * @return current RcsContactUceCapability instance that will be used for PUBLISH.
10923 */
10924 // Used for SHELL command only right now.
10925 @Override
10926 public RcsContactUceCapability clearUceRegistrationOverrideShell(int subId) {
10927 TelephonyPermissions.enforceShellOnly(Binder.getCallingUid(),
10928 "clearUceRegistrationOverrideShell");
10929 final long identity = Binder.clearCallingIdentity();
10930 try {
10931 return mApp.imsRcsController.clearUceRegistrationOverrideShell(subId);
10932 } catch (ImsException e) {
10933 throw new ServiceSpecificException(e.getCode(), e.getMessage());
10934 } finally {
10935 Binder.restoreCallingIdentity(identity);
10936 }
10937 }
10938
10939 /**
10940 * @return current RcsContactUceCapability instance that will be used for PUBLISH.
10941 */
10942 // Used for SHELL command only right now.
10943 @Override
10944 public RcsContactUceCapability getLatestRcsContactUceCapabilityShell(int subId) {
10945 TelephonyPermissions.enforceShellOnly(Binder.getCallingUid(),
10946 "getLatestRcsContactUceCapabilityShell");
10947 final long identity = Binder.clearCallingIdentity();
10948 try {
10949 return mApp.imsRcsController.getLatestRcsContactUceCapabilityShell(subId);
10950 } catch (ImsException e) {
10951 throw new ServiceSpecificException(e.getCode(), e.getMessage());
10952 } finally {
10953 Binder.restoreCallingIdentity(identity);
10954 }
10955 }
10956
10957 /**
10958 * Returns the last PIDF XML sent to the network during the last PUBLISH or "none" if the
10959 * device does not have an active PUBLISH.
10960 */
10961 // Used for SHELL command only right now.
10962 @Override
10963 public String getLastUcePidfXmlShell(int subId) {
10964 TelephonyPermissions.enforceShellOnly(Binder.getCallingUid(), "uceGetLastPidfXml");
10965 final long identity = Binder.clearCallingIdentity();
10966 try {
10967 return mApp.imsRcsController.getLastUcePidfXmlShell(subId);
10968 } catch (ImsException e) {
10969 throw new ServiceSpecificException(e.getCode(), e.getMessage());
10970 } finally {
10971 Binder.restoreCallingIdentity(identity);
10972 }
10973 }
10974
James.cf Line8713a42021-04-29 16:04:26 +080010975 /**
10976 * Remove UCE requests cannot be sent to the network status.
10977 */
10978 // Used for SHELL command only right now.
10979 @Override
10980 public boolean removeUceRequestDisallowedStatus(int subId) {
10981 TelephonyPermissions.enforceShellOnly(Binder.getCallingUid(), "uceRemoveDisallowedStatus");
10982 final long identity = Binder.clearCallingIdentity();
10983 try {
10984 return mApp.imsRcsController.removeUceRequestDisallowedStatus(subId);
10985 } catch (ImsException e) {
10986 throw new ServiceSpecificException(e.getCode(), e.getMessage());
10987 } finally {
10988 Binder.restoreCallingIdentity(identity);
10989 }
10990 }
10991
James.cf Lin18bb9002021-05-25 01:37:38 +080010992 /**
10993 * Remove UCE requests cannot be sent to the network status.
10994 */
10995 // Used for SHELL command only.
10996 @Override
10997 public boolean setCapabilitiesRequestTimeout(int subId, long timeoutAfterMs) {
10998 TelephonyPermissions.enforceShellOnly(Binder.getCallingUid(), "setCapRequestTimeout");
10999 final long identity = Binder.clearCallingIdentity();
11000 try {
11001 return mApp.imsRcsController.setCapabilitiesRequestTimeout(subId, timeoutAfterMs);
11002 } catch (ImsException e) {
11003 throw new ServiceSpecificException(e.getCode(), e.getMessage());
11004 } finally {
11005 Binder.restoreCallingIdentity(identity);
11006 }
11007 }
Brad Ebinger14d467f2021-02-12 06:18:28 +000011008
James.cf Lin4b784aa2021-01-31 03:25:15 +080011009 @Override
Rambo Wanga5cc9b72021-01-07 10:51:54 -080011010 public void setSignalStrengthUpdateRequest(int subId, SignalStrengthUpdateRequest request,
11011 String callingPackage) {
11012 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
11013 mApp, subId, "setSignalStrengthUpdateRequest");
11014
11015 final int callingUid = Binder.getCallingUid();
11016 // Verify that tha callingPackage belongs to the calling UID
11017 mApp.getSystemService(AppOpsManager.class)
11018 .checkPackage(callingUid, callingPackage);
11019
Rambo Wang3607f502021-02-01 21:51:40 -080011020 validateSignalStrengthUpdateRequest(mApp, request, callingUid);
Rambo Wanga5cc9b72021-01-07 10:51:54 -080011021
11022 final long identity = Binder.clearCallingIdentity();
11023 try {
11024 Object result = sendRequest(CMD_SET_SIGNAL_STRENGTH_UPDATE_REQUEST,
11025 new Pair<Integer, SignalStrengthUpdateRequest>(callingUid, request), subId);
11026
11027 if (result instanceof IllegalStateException) {
11028 throw (IllegalStateException) result;
11029 }
11030 } finally {
11031 Binder.restoreCallingIdentity(identity);
11032 }
11033 }
11034
11035 @Override
11036 public void clearSignalStrengthUpdateRequest(int subId, SignalStrengthUpdateRequest request,
11037 String callingPackage) {
11038 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
11039 mApp, subId, "clearSignalStrengthUpdateRequest");
11040
11041 final int callingUid = Binder.getCallingUid();
11042 // Verify that tha callingPackage belongs to the calling UID
11043 mApp.getSystemService(AppOpsManager.class)
11044 .checkPackage(callingUid, callingPackage);
11045
11046 final long identity = Binder.clearCallingIdentity();
11047 try {
11048 Object result = sendRequest(CMD_CLEAR_SIGNAL_STRENGTH_UPDATE_REQUEST,
11049 new Pair<Integer, SignalStrengthUpdateRequest>(callingUid, request), subId);
11050
11051 if (result instanceof IllegalStateException) {
11052 throw (IllegalStateException) result;
11053 }
11054 } finally {
11055 Binder.restoreCallingIdentity(identity);
11056 }
11057 }
11058
Rambo Wang3607f502021-02-01 21:51:40 -080011059 private static void validateSignalStrengthUpdateRequest(Context context,
11060 SignalStrengthUpdateRequest request, int callingUid) {
Rambo Wanga5cc9b72021-01-07 10:51:54 -080011061 if (callingUid == Process.PHONE_UID || callingUid == Process.SYSTEM_UID) {
11062 // phone/system process do not have further restriction on request
11063 return;
11064 }
11065
11066 // Applications has restrictions on how to use the request:
Rambo Wang3607f502021-02-01 21:51:40 -080011067 // Non-system callers need permission to set mIsSystemThresholdReportingRequestedWhileIdle
Rambo Wanga5cc9b72021-01-07 10:51:54 -080011068 if (request.isSystemThresholdReportingRequestedWhileIdle()) {
Rambo Wang3607f502021-02-01 21:51:40 -080011069 context.enforceCallingOrSelfPermission(
11070 android.Manifest.permission.LISTEN_ALWAYS_REPORTED_SIGNAL_STRENGTH,
11071 "validateSignalStrengthUpdateRequest");
Rambo Wanga5cc9b72021-01-07 10:51:54 -080011072 }
11073
11074 for (SignalThresholdInfo info : request.getSignalThresholdInfos()) {
11075 // Only system caller can set mHysteresisMs/mHysteresisDb/mIsEnabled.
11076 if (info.getHysteresisMs() != SignalThresholdInfo.HYSTERESIS_MS_DISABLED
11077 || info.getHysteresisDb() != SignalThresholdInfo.HYSTERESIS_DB_DISABLED
11078 || info.isEnabled()) {
11079 throw new IllegalArgumentException(
11080 "Only system can set hide fields in SignalThresholdInfo");
11081 }
11082
11083 // Thresholds length for each RAN need in range. This has been validated in
11084 // SignalThresholdInfo#Builder#setThreshold. Here we prevent apps calling hide method
11085 // setThresholdUnlimited (e.g. through reflection) with too short or too long thresholds
11086 final int[] thresholds = info.getThresholds();
11087 Objects.requireNonNull(thresholds);
11088 if (thresholds.length < SignalThresholdInfo.getMinimumNumberOfThresholdsAllowed()
11089 || thresholds.length
11090 > SignalThresholdInfo.getMaximumNumberOfThresholdsAllowed()) {
11091 throw new IllegalArgumentException(
11092 "thresholds length is out of range: " + thresholds.length);
11093 }
11094 }
11095 }
SongFerngWang8236caa2021-01-17 21:51:44 +080011096
11097 /**
11098 * Gets the current phone capability.
11099 *
11100 * Requires carrier privileges or READ_PRECISE_PHONE_STATE permission.
11101 * @return the PhoneCapability which describes the data connection capability of modem.
11102 * It's used to evaluate possible phone config change, for example from single
11103 * SIM device to multi-SIM device.
11104 */
11105 @Override
11106 public PhoneCapability getPhoneCapability() {
11107 enforceReadPrivilegedPermission("getPhoneCapability");
11108 final long identity = Binder.clearCallingIdentity();
11109 try {
11110 return mPhoneConfigurationManager.getCurrentPhoneCapability();
11111 } finally {
11112 Binder.restoreCallingIdentity(identity);
11113 }
11114 }
Michele Berionne5e411512020-11-13 02:36:59 +000011115
11116 /**
11117 * Prepare TelephonyManager for an unattended reboot. The reboot is
11118 * required to be done shortly after the API is invoked.
11119 */
11120 @Override
11121 @TelephonyManager.PrepareUnattendedRebootResult
11122 public int prepareForUnattendedReboot() {
Rafael Higuera Silvad9630642021-09-20 15:32:01 +000011123 WorkSource workSource = getWorkSource(Binder.getCallingUid());
Michele Berionne5e411512020-11-13 02:36:59 +000011124 enforceRebootPermission();
11125
11126 final long identity = Binder.clearCallingIdentity();
11127 try {
Rafael Higuera Silvad9630642021-09-20 15:32:01 +000011128 return (int) sendRequest(CMD_PREPARE_UNATTENDED_REBOOT, null, workSource);
Michele Berionne5e411512020-11-13 02:36:59 +000011129 } finally {
11130 Binder.restoreCallingIdentity(identity);
11131 }
11132 }
Hongbo Zeng156aa4a2021-02-08 21:50:28 +080011133
11134 /**
11135 * Request to get the current slicing configuration including URSP rules and
11136 * NSSAIs (configured, allowed and rejected).
11137 *
11138 * Requires carrier privileges or READ_PRIVILEGED_PHONE_STATE permission.
11139 */
11140 @Override
11141 public void getSlicingConfig(ResultReceiver callback) {
Hongbo Zeng1b2063d2022-02-21 01:33:03 +000011142 TelephonyPermissions
11143 .enforceCallingOrSelfReadPrivilegedPhoneStatePermissionOrCarrierPrivilege(
11144 mApp, SubscriptionManager.INVALID_SUBSCRIPTION_ID, "getSlicingConfig");
Hongbo Zeng156aa4a2021-02-08 21:50:28 +080011145
11146 final long identity = Binder.clearCallingIdentity();
11147 try {
11148 Phone phone = getDefaultPhone();
11149 sendRequestAsync(CMD_GET_SLICING_CONFIG, callback, phone, null);
11150 } finally {
11151 Binder.restoreCallingIdentity(identity);
11152 }
11153 }
Hunsuk Choi3b742d62021-10-25 19:48:34 +000011154
11155 /**
11156 * Register an IMS connection state callback
11157 */
11158 @Override
Hunsuk Choi89bd22c2021-11-01 13:04:54 +000011159 public void registerImsStateCallback(int subId, int feature, IImsStateCallback cb,
11160 String callingPackage) {
Hunsuk Choi3b742d62021-10-25 19:48:34 +000011161 if (feature == ImsFeature.FEATURE_MMTEL) {
11162 // ImsMmTelManager
11163 // The following also checks READ_PRIVILEGED_PHONE_STATE.
11164 TelephonyPermissions
11165 .enforceCallingOrSelfReadPrecisePhoneStatePermissionOrCarrierPrivilege(
11166 mApp, subId, "registerImsStateCallback");
11167 } else if (feature == ImsFeature.FEATURE_RCS) {
11168 // ImsRcsManager or SipDelegateManager
11169 TelephonyPermissions.enforceAnyPermissionGrantedOrCarrierPrivileges(mApp, subId,
11170 Binder.getCallingUid(), "registerImsStateCallback",
11171 Manifest.permission.READ_PRIVILEGED_PHONE_STATE,
11172 Manifest.permission.READ_PRECISE_PHONE_STATE,
11173 Manifest.permission.ACCESS_RCS_USER_CAPABILITY_EXCHANGE,
11174 Manifest.permission.PERFORM_IMS_SINGLE_REGISTRATION);
11175 }
11176
11177 if (!ImsManager.isImsSupportedOnDevice(mApp)) {
11178 throw new ServiceSpecificException(ImsException.CODE_ERROR_UNSUPPORTED_OPERATION,
11179 "IMS not available on device.");
11180 }
11181
11182 if (subId == SubscriptionManager.DEFAULT_SUBSCRIPTION_ID) {
11183 throw new ServiceSpecificException(ImsException.CODE_ERROR_INVALID_SUBSCRIPTION);
11184 }
11185
11186 ImsStateCallbackController controller = ImsStateCallbackController.getInstance();
11187 if (controller == null) {
11188 throw new ServiceSpecificException(ImsException.CODE_ERROR_UNSUPPORTED_OPERATION,
11189 "IMS not available on device.");
11190 }
11191
Hunsuk Choi89bd22c2021-11-01 13:04:54 +000011192 if (callingPackage == null) {
11193 callingPackage = getCurrentPackageName();
11194 }
11195
Hunsuk Choi3b742d62021-10-25 19:48:34 +000011196 final long token = Binder.clearCallingIdentity();
11197 try {
11198 int slotId = getSlotIndexOrException(subId);
Hunsuk Choi89bd22c2021-11-01 13:04:54 +000011199 controller.registerImsStateCallback(subId, feature, cb, callingPackage);
Hunsuk Choi3b742d62021-10-25 19:48:34 +000011200 } catch (ImsException e) {
11201 throw new ServiceSpecificException(e.getCode());
11202 } finally {
11203 Binder.restoreCallingIdentity(token);
11204 }
11205 }
11206
11207 /**
11208 * Unregister an IMS connection state callback
11209 */
11210 @Override
11211 public void unregisterImsStateCallback(IImsStateCallback cb) {
11212 final long token = Binder.clearCallingIdentity();
11213 ImsStateCallbackController controller = ImsStateCallbackController.getInstance();
11214 if (controller == null) {
11215 return;
11216 }
11217 try {
11218 controller.unregisterImsStateCallback(cb);
11219 } finally {
11220 Binder.restoreCallingIdentity(token);
11221 }
11222 }
Sooraj Sasindranfae41b32021-10-26 02:10:05 -070011223
11224 /**
11225 * @return {@CellIdentity} last known cell identity {@CellIdentity}.
11226 *
11227 * Require {@link android.Manifest.permission#ACCESS_FINE_LOCATION} and
11228 * com.android.phone.permission.ACCESS_LAST_KNOWN_CELL_ID, otherwise throws
11229 * SecurityException.
11230 * If there is current registered network this value will be same as the registered cell
11231 * identity. If the device goes out of service the previous cell identity is cached and
11232 * will be returned. If the cache age of the Cell identity is more than 24 hours
11233 * it will be cleared and null will be returned.
11234 *
11235 */
11236 @Override
11237 public @Nullable CellIdentity getLastKnownCellIdentity(int subId, String callingPackage,
11238 String callingFeatureId) {
11239 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
11240 LocationAccessPolicy.LocationPermissionResult fineLocationResult =
11241 LocationAccessPolicy.checkLocationPermission(mApp,
11242 new LocationAccessPolicy.LocationPermissionQuery.Builder()
11243 .setCallingPackage(callingPackage)
11244 .setCallingFeatureId(callingFeatureId)
11245 .setCallingPid(Binder.getCallingPid())
11246 .setCallingUid(Binder.getCallingUid())
11247 .setMethod("getLastKnownCellIdentity")
11248 .setLogAsInfo(true)
11249 .setMinSdkVersionForFine(Build.VERSION_CODES.Q)
11250 .setMinSdkVersionForCoarse(Build.VERSION_CODES.Q)
11251 .setMinSdkVersionForEnforcement(Build.VERSION_CODES.Q)
11252 .build());
11253
11254 boolean hasFinePermission =
11255 fineLocationResult == LocationAccessPolicy.LocationPermissionResult.ALLOWED;
11256 if (!hasFinePermission
11257 || !TelephonyPermissions.checkLastKnownCellIdAccessPermission(mApp)) {
11258 throw new SecurityException("getLastKnownCellIdentity need ACCESS_FINE_LOCATION "
Rambo Wang918993a2022-04-27 09:08:36 -070011259 + "and ACCESS_LAST_KNOWN_CELL_ID permission.");
Sooraj Sasindranfae41b32021-10-26 02:10:05 -070011260 }
11261
11262 final long identity = Binder.clearCallingIdentity();
11263 try {
11264 Phone phone = getPhone(subId);
11265 if (phone == null) return null;
11266 ServiceStateTracker sst = phone.getServiceStateTracker();
11267 if (sst == null) return null;
11268 return sst.getLastKnownCellIdentity();
11269 } finally {
11270 Binder.restoreCallingIdentity(identity);
11271 }
11272 }
Jack Yu4c0a5502021-12-03 23:58:26 -080011273
jimsun3b9ccac2021-10-26 15:01:23 +080011274 /**
11275 * Sets the modem service class Name that Telephony will bind to.
11276 *
11277 * @param serviceName The class name of the modem service.
11278 * @return true if the operation is succeed, otherwise false.
11279 */
11280 public boolean setModemService(String serviceName) {
11281 Log.d(LOG_TAG, "setModemService - " + serviceName);
11282 TelephonyPermissions.enforceShellOnly(Binder.getCallingUid(), "setModemService");
11283 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(mApp,
11284 SubscriptionManager.INVALID_SUBSCRIPTION_ID,
11285 "setModemService");
11286 return mPhoneConfigurationManager.setModemService(serviceName);
11287 }
11288
11289 /**
11290 * Return the class name of the currently bounded modem service.
11291 *
11292 * @return the class name of the modem service.
11293 */
11294 public String getModemService() {
11295 String result;
11296 Log.d(LOG_TAG, "getModemService");
11297 TelephonyPermissions.enforceShellOnly(Binder.getCallingUid(), "getModemService");
11298 TelephonyPermissions
11299 .enforceCallingOrSelfReadPrivilegedPhoneStatePermissionOrCarrierPrivilege(
11300 mApp, SubscriptionManager.INVALID_SUBSCRIPTION_ID,
11301 "getModemService");
11302 result = mPhoneConfigurationManager.getModemService();
11303 Log.d(LOG_TAG, "result = " + result);
11304 return result;
11305 }
Hunter Knepshield2b076fa2022-01-19 02:26:22 -080011306
11307 @Override
11308 public void setVoiceServiceStateOverride(int subId, boolean hasService, String callingPackage) {
11309 // Only telecom (and shell, for CTS purposes) is allowed to call this method.
11310 mApp.enforceCallingOrSelfPermission(
11311 permission.BIND_TELECOM_CONNECTION_SERVICE, "setVoiceServiceStateOverride");
11312 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
11313
11314 final long identity = Binder.clearCallingIdentity();
11315 try {
11316 Phone phone = getPhone(subId);
11317 if (phone == null) return;
11318 phone.setVoiceServiceStateOverride(hasService);
11319 } finally {
11320 Binder.restoreCallingIdentity(identity);
11321 }
11322 }
Muralidhar Reddy4e5a8012022-05-11 14:49:00 +000011323
11324 /**
11325 * set removable eSIM as default eUICC.
11326 *
11327 * @hide
11328 */
11329 @Override
11330 public void setRemovableEsimAsDefaultEuicc(boolean isDefault, String callingPackage) {
11331 enforceModifyPermission();
11332 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
11333
11334 final long identity = Binder.clearCallingIdentity();
11335 try {
11336 UiccController.getInstance().setRemovableEsimAsDefaultEuicc(isDefault);
11337 } finally {
11338 Binder.restoreCallingIdentity(identity);
11339 }
11340 }
11341
11342 /**
11343 * Returns whether the removable eSIM is default eUICC or not.
11344 *
11345 * @hide
11346 */
11347 @Override
11348 public boolean isRemovableEsimDefaultEuicc(String callingPackage) {
11349 enforceReadPrivilegedPermission("isRemovableEsimDefaultEuicc");
11350 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
11351
11352 final long identity = Binder.clearCallingIdentity();
11353 try {
11354 return UiccController.getInstance().isRemovableEsimDefaultEuicc();
11355 } finally {
11356 Binder.restoreCallingIdentity(identity);
11357 }
11358 }
11359
Ryan Savitskicab25c52023-03-14 14:23:49 +000011360 /**
11361 * Get the SIM state for the slot index.
11362 * For Remote-SIMs, this method returns {@link IccCardConstants.State#UNKNOWN}
11363 *
11364 * @return SIM state as the ordinal of {@link IccCardConstants.State}
11365 */
11366 @Override
11367 @SimState
11368 public int getSimStateForSlotIndex(int slotIndex) {
11369 IccCardConstants.State simState;
11370 if (slotIndex < 0) {
11371 simState = IccCardConstants.State.UNKNOWN;
11372 } else {
11373 Phone phone = null;
11374 try {
11375 phone = PhoneFactory.getPhone(slotIndex);
11376 } catch (IllegalStateException e) {
11377 // ignore
11378 }
11379 if (phone == null) {
11380 simState = IccCardConstants.State.UNKNOWN;
11381 } else {
11382 IccCard icc = phone.getIccCard();
11383 if (icc == null) {
11384 simState = IccCardConstants.State.UNKNOWN;
11385 } else {
11386 simState = icc.getState();
11387 }
11388 }
11389 }
11390 return simState.ordinal();
11391 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -070011392}