blob: 5699ca8640785867edd5ef2da1d7acaa40c23316 [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;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070053import android.os.Looper;
54import android.os.Message;
yinxu504e1392017-04-12 16:03:22 -070055import android.os.Messenger;
Hall Liua1548bd2019-12-24 14:14:12 -080056import android.os.ParcelFileDescriptor;
Malcolm Chen6ca97372019-07-01 16:28:21 -070057import android.os.ParcelUuid;
Tyler Gunn65d45c22017-06-05 11:22:26 -070058import android.os.PersistableBundle;
Shuo Qiancd19c462020-01-16 20:51:11 -080059import android.os.Process;
Brad Ebinger5f64b052017-12-14 14:26:15 -080060import android.os.RemoteException;
Adam Lesinski903a54c2016-04-11 14:49:52 -070061import android.os.ResultReceiver;
Brad Ebinger1ce9c432019-07-16 13:19:44 -070062import android.os.ServiceSpecificException;
Rambo Wang0f050d82021-02-12 11:43:36 -080063import android.os.SystemClock;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070064import android.os.UserHandle;
Stuart Scott981d8582015-04-21 14:09:50 -070065import android.os.UserManager;
Sooraj Sasindran9a909312016-07-18 11:57:25 -070066import android.os.WorkSource;
Derek Tan97ebb422014-09-05 16:55:38 -070067import android.preference.PreferenceManager;
Naina Nallurid63128d2019-09-17 14:10:30 -070068import android.provider.DeviceConfig;
Ihab Awadf2177b72013-11-25 13:33:23 -080069import android.provider.Settings;
Amit Mahajan7dbbd822019-03-13 17:33:47 -070070import android.provider.Telephony;
Inseob Kim14bb3d02018-12-13 17:11:34 +090071import android.sysprop.TelephonyProperties;
Santos Cordon7a1885b2015-02-03 11:15:19 -080072import android.telecom.PhoneAccount;
Nancy Chen31f9ba12016-01-06 11:42:12 -080073import android.telecom.PhoneAccountHandle;
Andrew Lee9431b832015-03-09 18:46:45 -070074import android.telecom.TelecomManager;
Gary Jian3aa9a762022-01-24 16:41:19 +080075import android.telephony.AccessNetworkConstants;
76import android.telephony.ActivityStatsTechSpecificInfo;
Chen Xu227e06f2019-09-26 22:48:11 -070077import android.telephony.Annotation.ApnType;
Jack Yu0eda6842022-04-18 00:34:46 -070078import android.telephony.Annotation.DataActivityType;
Jack Nudelmanb0b87642020-11-12 15:04:39 -080079import android.telephony.Annotation.ThermalMitigationResult;
Sarah Chin2ec39f62022-08-31 17:03:26 -070080import android.telephony.AnomalyReporter;
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;
Hall Liub2ac8ef2019-02-28 15:56:23 -0800112import android.telephony.TelephonyScanManager;
Jack Nudelmanb0b87642020-11-12 15:04:39 -0800113import android.telephony.ThermalMitigationRequest;
Jordan Liu5aa07002018-12-18 15:44:48 -0800114import android.telephony.UiccCardInfo;
sandeepjsb6c87872021-09-27 15:34:44 +0000115import android.telephony.UiccPortInfo;
Holly Jiuyu Sun01c47ad2018-01-24 17:56:33 +0000116import android.telephony.UiccSlotInfo;
sandeepjsb6c87872021-09-27 15:34:44 +0000117import android.telephony.UiccSlotMapping;
Tyler Gunn65d45c22017-06-05 11:22:26 -0700118import android.telephony.UssdResponse;
Ta-wei Yenb6929602016-05-24 15:48:27 -0700119import android.telephony.VisualVoicemailSmsFilterSettings;
Hongbo Zeng0e18b162021-04-07 16:52:18 +0800120import android.telephony.data.NetworkSlicingConfig;
Jack Yub5d8f642018-11-26 11:20:48 -0800121import android.telephony.emergency.EmergencyNumber;
Hui Wang641e81c2020-10-12 12:14:23 -0700122import android.telephony.gba.GbaAuthRequest;
123import android.telephony.gba.UaSecurityProtocolIdentifier;
Brad Ebinger1ce9c432019-07-16 13:19:44 -0700124import android.telephony.ims.ImsException;
Brad Ebinger1c8542e2019-01-14 13:43:14 -0800125import android.telephony.ims.ProvisioningManager;
Hui Wang761a6682020-10-31 05:12:53 +0000126import android.telephony.ims.RcsClientConfiguration;
Brad Ebinger14d467f2021-02-12 06:18:28 +0000127import android.telephony.ims.RcsContactUceCapability;
Brad Ebingera34a6c22019-10-22 17:36:18 -0700128import android.telephony.ims.RegistrationManager;
joonhunshincffb7fc2021-11-28 07:32:01 +0000129import android.telephony.ims.aidl.IFeatureProvisioningCallback;
Brad Ebinger35c841c2018-10-01 10:40:55 -0700130import android.telephony.ims.aidl.IImsCapabilityCallback;
Brad Ebinger22bc3e42018-01-16 09:39:35 -0800131import android.telephony.ims.aidl.IImsConfig;
Brad Ebingerdf5b4f02018-10-31 11:24:17 -0700132import android.telephony.ims.aidl.IImsConfigCallback;
Brad Ebinger22bc3e42018-01-16 09:39:35 -0800133import android.telephony.ims.aidl.IImsRegistration;
Brad Ebinger35c841c2018-10-01 10:40:55 -0700134import android.telephony.ims.aidl.IImsRegistrationCallback;
Hui Wang761a6682020-10-31 05:12:53 +0000135import android.telephony.ims.aidl.IRcsConfigCallback;
Brad Ebingerbc7dd582019-10-17 17:03:22 -0700136import android.telephony.ims.feature.ImsFeature;
Brad Ebinger1c8542e2019-01-14 13:43:14 -0800137import android.telephony.ims.stub.ImsConfigImplBase;
Brad Ebinger1f2b5082018-02-08 16:11:32 -0800138import android.telephony.ims.stub.ImsRegistrationImplBase;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700139import android.text.TextUtils;
Jeff Sharkey85190e62014-12-05 09:40:12 -0800140import android.util.ArraySet;
Hall Liud60acc92020-05-21 17:09:35 -0700141import android.util.EventLog;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700142import android.util.Log;
Jake Hambye994d462014-02-03 13:10:13 -0800143import android.util.Pair;
Ta-wei Yen30a69c82016-12-27 14:52:32 -0800144
Andrew Lee312e8172014-10-23 17:01:36 -0700145import com.android.ims.ImsManager;
Brad Ebinger34bef922017-11-09 10:27:08 -0800146import com.android.ims.internal.IImsServiceFeatureCallback;
James.cf Linbcdf8b32021-01-14 16:44:13 +0800147import com.android.ims.rcs.uce.eab.EabUtil;
SongFerngWangfd89b102021-05-27 22:44:54 +0800148import com.android.internal.annotations.VisibleForTesting;
Shuo Qian4a594052020-01-23 11:59:30 -0800149import com.android.internal.telephony.CallForwardInfo;
Shishir Agrawal566b7612013-10-28 14:41:00 -0700150import com.android.internal.telephony.CallManager;
Tyler Gunn52dcf772017-04-26 11:30:31 -0700151import com.android.internal.telephony.CallStateException;
Tyler Gunnd4339262021-05-03 14:46:49 -0700152import com.android.internal.telephony.CallTracker;
Rambo Wang9c9ffdd2022-01-13 21:51:44 -0800153import com.android.internal.telephony.CarrierPrivilegesTracker;
chen xu651eec72018-11-11 19:03:44 -0800154import com.android.internal.telephony.CarrierResolver;
Shishir Agrawal302c8692015-06-19 13:49:39 -0700155import com.android.internal.telephony.CellNetworkScanResult;
Shishir Agrawal566b7612013-10-28 14:41:00 -0700156import com.android.internal.telephony.CommandException;
Shuo Qian4a594052020-01-23 11:59:30 -0800157import com.android.internal.telephony.CommandsInterface;
Hui Wang641e81c2020-10-12 12:14:23 -0700158import com.android.internal.telephony.GbaManager;
Shuo Qianccbaf742021-02-22 18:32:21 -0800159import com.android.internal.telephony.GsmCdmaPhone;
Nathan Harold48d6fd52019-02-06 19:01:40 -0800160import com.android.internal.telephony.HalVersion;
Hall Liu73f5d362020-01-20 13:42:00 -0800161import com.android.internal.telephony.IBooleanConsumer;
Hall Liu27d24262020-09-18 19:04:59 -0700162import com.android.internal.telephony.ICallForwardingInfoCallback;
Hunsuk Choi3b742d62021-10-25 19:48:34 +0000163import com.android.internal.telephony.IImsStateCallback;
Brad Ebingera63db5f2019-04-23 16:31:13 -0700164import com.android.internal.telephony.IIntegerConsumer;
Hall Liud892bec2018-11-30 14:51:45 -0800165import com.android.internal.telephony.INumberVerificationCallback;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700166import com.android.internal.telephony.ITelephony;
Jake Hambye994d462014-02-03 13:10:13 -0800167import com.android.internal.telephony.IccCard;
Rambo Wanga1782702021-11-10 20:15:19 -0800168import com.android.internal.telephony.IccLogicalChannelRequest;
Jack Yu5f7092c2018-04-13 14:05:37 -0700169import com.android.internal.telephony.LocaleTracker;
yinxub1bed742017-04-17 11:45:04 -0700170import com.android.internal.telephony.NetworkScanRequestTracker;
Shishir Agrawal302c8692015-06-19 13:49:39 -0700171import com.android.internal.telephony.OperatorInfo;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700172import com.android.internal.telephony.Phone;
Malcolm Chen2c63d402018-08-14 16:00:53 -0700173import com.android.internal.telephony.PhoneConfigurationManager;
Nathan Harolda667c152016-12-14 11:27:20 -0800174import com.android.internal.telephony.PhoneConstantConversions;
Ta-wei Yen87c49842016-05-13 21:19:52 -0700175import com.android.internal.telephony.PhoneConstants;
Wink Saville36469e72014-06-11 15:17:00 -0700176import com.android.internal.telephony.PhoneFactory;
Wink Saville5d475dd2014-10-17 15:00:58 -0700177import com.android.internal.telephony.ProxyController;
Sanket Padawe99ef1e32016-05-18 16:12:33 -0700178import com.android.internal.telephony.RIL;
SongFerngWang8c6e82e2021-03-02 22:09:29 +0800179import com.android.internal.telephony.RILConstants;
Daniel Bright94f43662021-03-01 14:43:40 -0800180import com.android.internal.telephony.RadioInterfaceCapabilityController;
Jack Yu5f7092c2018-04-13 14:05:37 -0700181import com.android.internal.telephony.ServiceStateTracker;
Amit Mahajandccb3f12019-05-13 13:48:32 -0700182import com.android.internal.telephony.SmsController;
Brad Ebingera63db5f2019-04-23 16:31:13 -0700183import com.android.internal.telephony.SmsPermissions;
Wink Savilleac1bdfd2014-11-20 23:04:44 -0800184import com.android.internal.telephony.SubscriptionController;
Peter Wang59571be2020-01-27 12:35:15 +0800185import com.android.internal.telephony.TelephonyIntents;
Jeff Davidson7e17e312018-02-13 18:17:36 -0800186import com.android.internal.telephony.TelephonyPermissions;
Jack Yu27422a52022-03-21 10:38:05 -0700187import com.android.internal.telephony.data.DataUtils;
sqianf4ca7ed2019-01-15 18:32:07 -0800188import com.android.internal.telephony.emergency.EmergencyNumberTracker;
Derek Tan740e1672017-06-27 14:56:27 -0700189import com.android.internal.telephony.euicc.EuiccConnector;
Brad Ebinger9c0eb502019-01-23 15:06:19 -0800190import com.android.internal.telephony.ims.ImsResolver;
Tyler Gunn7bcdc742019-10-04 15:56:59 -0700191import com.android.internal.telephony.imsphone.ImsPhone;
192import com.android.internal.telephony.imsphone.ImsPhoneCallTracker;
joonhunshin3e154242021-09-17 06:33:39 +0000193import com.android.internal.telephony.metrics.RcsStats;
Pengquan Meng6c2dc9f2019-02-06 11:12:53 -0800194import com.android.internal.telephony.metrics.TelephonyMetrics;
Meng Wangafbc5852019-09-19 17:37:13 -0700195import com.android.internal.telephony.uicc.IccCardApplicationStatus.AppType;
Shishir Agrawal566b7612013-10-28 14:41:00 -0700196import com.android.internal.telephony.uicc.IccIoResult;
197import com.android.internal.telephony.uicc.IccUtils;
Nathan Haroldb3014052017-01-25 15:57:32 -0800198import com.android.internal.telephony.uicc.SIMRecords;
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700199import com.android.internal.telephony.uicc.UiccCard;
Nathan Haroldb3014052017-01-25 15:57:32 -0800200import com.android.internal.telephony.uicc.UiccCardApplication;
Shishir Agrawal566b7612013-10-28 14:41:00 -0700201import com.android.internal.telephony.uicc.UiccController;
Muralidhar Reddy472c2ae2021-09-29 19:38:40 +0000202import com.android.internal.telephony.uicc.UiccPort;
Jeff Davidson7e17e312018-02-13 18:17:36 -0800203import com.android.internal.telephony.uicc.UiccProfile;
Holly Jiuyu Sun01c47ad2018-01-24 17:56:33 +0000204import com.android.internal.telephony.uicc.UiccSlot;
zoey chenc730df82019-12-18 17:07:20 +0800205import com.android.internal.telephony.util.LocaleUtils;
fionaxu7ed723d2017-05-30 18:58:54 -0700206import com.android.internal.telephony.util.VoicemailNotificationSettingsUtil;
Hall Liu27d24262020-09-18 19:04:59 -0700207import com.android.internal.util.FunctionalUtils;
Jake Hambye994d462014-02-03 13:10:13 -0800208import com.android.internal.util.HexDump;
Hall Liuaa4211e2021-01-20 15:43:39 -0800209import com.android.phone.callcomposer.CallComposerPictureManager;
210import com.android.phone.callcomposer.CallComposerPictureTransfer;
211import com.android.phone.callcomposer.ImageData;
Brad Ebingera63db5f2019-04-23 16:31:13 -0700212import com.android.phone.settings.PickSmsSubscriptionActivity;
Sarah Chin2ec39f62022-08-31 17:03:26 -0700213import com.android.phone.slicestore.SliceStore;
Ta-wei Yenc9df0432017-04-17 17:09:07 -0700214import com.android.phone.vvm.PhoneAccountHandleConverter;
Ta-wei Yen527a9c02017-01-06 15:29:25 -0800215import com.android.phone.vvm.RemoteVvmTaskManager;
Ta-wei Yenc9df0432017-04-17 17:09:07 -0700216import com.android.phone.vvm.VisualVoicemailSettingsUtil;
Ta-wei Yenc8905312017-03-28 11:14:45 -0700217import com.android.phone.vvm.VisualVoicemailSmsFilterConfig;
Jack Nudelmanb0b87642020-11-12 15:04:39 -0800218import com.android.services.telephony.TelecomAccountRegistry;
219import com.android.services.telephony.TelephonyConnectionService;
Peter Wang44b186e2020-01-13 23:33:09 -0800220import com.android.telephony.Rlog;
Ta-wei Yen30a69c82016-12-27 14:52:32 -0800221
Hall Liu82694d52020-12-11 18:22:04 -0800222import java.io.ByteArrayOutputStream;
Ta-wei Yenc236d6b2016-06-21 13:33:12 -0700223import java.io.FileDescriptor;
Hall Liu82694d52020-12-11 18:22:04 -0800224import java.io.IOException;
225import java.io.InputStream;
Ta-wei Yenc236d6b2016-06-21 13:33:12 -0700226import java.io.PrintWriter;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700227import java.util.ArrayList;
Jeff Sharkey85190e62014-12-05 09:40:12 -0800228import java.util.Arrays;
Muralidhar Reddyeb809e32021-11-19 03:07:54 +0000229import java.util.Collection;
Hunter Knepshieldcad7f0b2021-12-13 15:08:35 -0800230import java.util.Collections;
sqian11b7a0e2018-12-05 18:48:28 -0800231import java.util.HashMap;
sqianf4ca7ed2019-01-15 18:32:07 -0800232import java.util.HashSet;
Jake Hambye994d462014-02-03 13:10:13 -0800233import java.util.List;
Narayan Kamath1c496c22015-04-16 14:40:19 +0100234import java.util.Locale;
Jeff Sharkey85190e62014-12-05 09:40:12 -0800235import java.util.Map;
Nazanin Bakhshif71371d2019-04-29 17:29:44 -0700236import java.util.NoSuchElementException;
Hall Liu82694d52020-12-11 18:22:04 -0800237import java.util.Objects;
sqianf4ca7ed2019-01-15 18:32:07 -0800238import java.util.Set;
Sarah Chin2ec39f62022-08-31 17:03:26 -0700239import java.util.UUID;
Hall Liu82694d52020-12-11 18:22:04 -0800240import java.util.concurrent.Executors;
Peter Wangdafb9ac2020-01-15 14:13:38 -0800241import java.util.concurrent.atomic.AtomicBoolean;
Hall Liu73f5d362020-01-20 13:42:00 -0800242import java.util.function.Consumer;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700243
244/**
245 * Implementation of the ITelephony interface.
246 */
Santos Cordon117fee72014-05-16 17:56:12 -0700247public class PhoneInterfaceManager extends ITelephony.Stub {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700248 private static final String LOG_TAG = "PhoneInterfaceManager";
249 private static final boolean DBG = (PhoneGlobals.DBG_LEVEL >= 2);
250 private static final boolean DBG_LOC = false;
Jeff Sharkey85190e62014-12-05 09:40:12 -0800251 private static final boolean DBG_MERGE = false;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700252
253 // Message codes used with mMainThreadHandler
254 private static final int CMD_HANDLE_PIN_MMI = 1;
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700255 private static final int CMD_TRANSMIT_APDU_LOGICAL_CHANNEL = 7;
256 private static final int EVENT_TRANSMIT_APDU_LOGICAL_CHANNEL_DONE = 8;
Shishir Agrawal566b7612013-10-28 14:41:00 -0700257 private static final int CMD_OPEN_CHANNEL = 9;
258 private static final int EVENT_OPEN_CHANNEL_DONE = 10;
259 private static final int CMD_CLOSE_CHANNEL = 11;
260 private static final int EVENT_CLOSE_CHANNEL_DONE = 12;
Jake Hambye994d462014-02-03 13:10:13 -0800261 private static final int CMD_NV_READ_ITEM = 13;
262 private static final int EVENT_NV_READ_ITEM_DONE = 14;
263 private static final int CMD_NV_WRITE_ITEM = 15;
264 private static final int EVENT_NV_WRITE_ITEM_DONE = 16;
265 private static final int CMD_NV_WRITE_CDMA_PRL = 17;
266 private static final int EVENT_NV_WRITE_CDMA_PRL_DONE = 18;
chen xu6dac5ab2018-10-26 17:39:23 -0700267 private static final int CMD_RESET_MODEM_CONFIG = 19;
268 private static final int EVENT_RESET_MODEM_CONFIG_DONE = 20;
SongFerngWang3ef3e072020-12-21 16:41:52 +0800269 private static final int CMD_GET_ALLOWED_NETWORK_TYPES_BITMASK = 21;
270 private static final int EVENT_GET_ALLOWED_NETWORK_TYPES_BITMASK_DONE = 22;
Sailesh Nepal35b59452014-03-06 09:26:56 -0800271 private static final int CMD_SEND_ENVELOPE = 25;
272 private static final int EVENT_SEND_ENVELOPE_DONE = 26;
Shuo Qian850e4d6a2018-04-25 21:02:08 +0000273 private static final int CMD_INVOKE_OEM_RIL_REQUEST_RAW = 27;
274 private static final int EVENT_INVOKE_OEM_RIL_REQUEST_RAW_DONE = 28;
Derek Tan6b088ee2014-09-05 14:15:18 -0700275 private static final int CMD_TRANSMIT_APDU_BASIC_CHANNEL = 29;
276 private static final int EVENT_TRANSMIT_APDU_BASIC_CHANNEL_DONE = 30;
277 private static final int CMD_EXCHANGE_SIM_IO = 31;
278 private static final int EVENT_EXCHANGE_SIM_IO_DONE = 32;
Shishir Agrawal76d5da92014-11-09 16:17:25 -0800279 private static final int CMD_SET_VOICEMAIL_NUMBER = 33;
280 private static final int EVENT_SET_VOICEMAIL_NUMBER_DONE = 34;
Stuart Scott54788802015-03-30 13:18:01 -0700281 private static final int CMD_SET_NETWORK_SELECTION_MODE_AUTOMATIC = 35;
282 private static final int EVENT_SET_NETWORK_SELECTION_MODE_AUTOMATIC_DONE = 36;
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -0700283 private static final int CMD_GET_MODEM_ACTIVITY_INFO = 37;
284 private static final int EVENT_GET_MODEM_ACTIVITY_INFO_DONE = 38;
Shishir Agrawal302c8692015-06-19 13:49:39 -0700285 private static final int CMD_PERFORM_NETWORK_SCAN = 39;
286 private static final int EVENT_PERFORM_NETWORK_SCAN_DONE = 40;
287 private static final int CMD_SET_NETWORK_SELECTION_MODE_MANUAL = 41;
288 private static final int EVENT_SET_NETWORK_SELECTION_MODE_MANUAL_DONE = 42;
Meng Wang1a7c35a2016-05-05 20:56:15 -0700289 private static final int CMD_SET_ALLOWED_CARRIERS = 43;
290 private static final int EVENT_SET_ALLOWED_CARRIERS_DONE = 44;
291 private static final int CMD_GET_ALLOWED_CARRIERS = 45;
292 private static final int EVENT_GET_ALLOWED_CARRIERS_DONE = 46;
pkanwar32d516d2016-10-14 19:37:38 -0700293 private static final int CMD_HANDLE_USSD_REQUEST = 47;
Nathan Haroldb3014052017-01-25 15:57:32 -0800294 private static final int CMD_GET_FORBIDDEN_PLMNS = 48;
295 private static final int EVENT_GET_FORBIDDEN_PLMNS_DONE = 49;
Holly Jiuyu Sun01c47ad2018-01-24 17:56:33 +0000296 private static final int CMD_SWITCH_SLOTS = 50;
297 private static final int EVENT_SWITCH_SLOTS_DONE = 51;
Pengquan Menga1bb6272018-09-06 09:59:22 -0700298 private static final int CMD_GET_NETWORK_SELECTION_MODE = 52;
299 private static final int EVENT_GET_NETWORK_SELECTION_MODE_DONE = 53;
300 private static final int CMD_GET_CDMA_ROAMING_MODE = 54;
301 private static final int EVENT_GET_CDMA_ROAMING_MODE_DONE = 55;
302 private static final int CMD_SET_CDMA_ROAMING_MODE = 56;
303 private static final int EVENT_SET_CDMA_ROAMING_MODE_DONE = 57;
304 private static final int CMD_SET_CDMA_SUBSCRIPTION_MODE = 58;
305 private static final int EVENT_SET_CDMA_SUBSCRIPTION_MODE_DONE = 59;
Nathan Harold3ff88932018-08-14 10:19:49 -0700306 private static final int CMD_GET_ALL_CELL_INFO = 60;
307 private static final int EVENT_GET_ALL_CELL_INFO_DONE = 61;
308 private static final int CMD_GET_CELL_LOCATION = 62;
309 private static final int EVENT_GET_CELL_LOCATION_DONE = 63;
chen xu6dac5ab2018-10-26 17:39:23 -0700310 private static final int CMD_MODEM_REBOOT = 64;
311 private static final int EVENT_CMD_MODEM_REBOOT_DONE = 65;
Nathan Haroldfa8da0f2018-09-27 18:51:29 -0700312 private static final int CMD_REQUEST_CELL_INFO_UPDATE = 66;
313 private static final int EVENT_REQUEST_CELL_INFO_UPDATE_DONE = 67;
Malcolm Chen8e4ed912019-01-15 20:22:16 -0800314 private static final int CMD_REQUEST_ENABLE_MODEM = 68;
315 private static final int EVENT_ENABLE_MODEM_DONE = 69;
Nazanin Bakhshif71371d2019-04-29 17:29:44 -0700316 private static final int CMD_GET_MODEM_STATUS = 70;
317 private static final int EVENT_GET_MODEM_STATUS_DONE = 71;
yincheng zhao2737e882019-09-06 17:06:54 -0700318 private static final int CMD_SET_FORBIDDEN_PLMNS = 72;
319 private static final int EVENT_SET_FORBIDDEN_PLMNS_DONE = 73;
Naina Nallurid63128d2019-09-17 14:10:30 -0700320 private static final int CMD_ERASE_MODEM_CONFIG = 74;
321 private static final int EVENT_ERASE_MODEM_CONFIG_DONE = 75;
zoey chene02881a2019-12-30 16:11:23 +0800322 private static final int CMD_CHANGE_ICC_LOCK_PASSWORD = 76;
323 private static final int EVENT_CHANGE_ICC_LOCK_PASSWORD_DONE = 77;
324 private static final int CMD_SET_ICC_LOCK_ENABLED = 78;
325 private static final int EVENT_SET_ICC_LOCK_ENABLED_DONE = 79;
Hall Liu73f5d362020-01-20 13:42:00 -0800326 private static final int CMD_SET_SYSTEM_SELECTION_CHANNELS = 80;
327 private static final int EVENT_SET_SYSTEM_SELECTION_CHANNELS_DONE = 81;
Peter Wangdafb9ac2020-01-15 14:13:38 -0800328 private static final int MSG_NOTIFY_USER_ACTIVITY = 82;
Shuo Qian4a594052020-01-23 11:59:30 -0800329 private static final int CMD_GET_CALL_FORWARDING = 83;
330 private static final int EVENT_GET_CALL_FORWARDING_DONE = 84;
331 private static final int CMD_SET_CALL_FORWARDING = 85;
332 private static final int EVENT_SET_CALL_FORWARDING_DONE = 86;
333 private static final int CMD_GET_CALL_WAITING = 87;
334 private static final int EVENT_GET_CALL_WAITING_DONE = 88;
335 private static final int CMD_SET_CALL_WAITING = 89;
336 private static final int EVENT_SET_CALL_WAITING_DONE = 90;
Sooraj Sasindran37444802020-08-11 10:40:43 -0700337 private static final int CMD_ENABLE_NR_DUAL_CONNECTIVITY = 91;
338 private static final int EVENT_ENABLE_NR_DUAL_CONNECTIVITY_DONE = 92;
339 private static final int CMD_IS_NR_DUAL_CONNECTIVITY_ENABLED = 93;
340 private static final int EVENT_IS_NR_DUAL_CONNECTIVITY_ENABLED_DONE = 94;
Sarah Chinbaab1432020-10-28 13:46:24 -0700341 private static final int CMD_GET_CDMA_SUBSCRIPTION_MODE = 95;
342 private static final int EVENT_GET_CDMA_SUBSCRIPTION_MODE_DONE = 96;
Sarah Chin679c08a2020-11-18 13:39:35 -0800343 private static final int CMD_GET_SYSTEM_SELECTION_CHANNELS = 97;
344 private static final int EVENT_GET_SYSTEM_SELECTION_CHANNELS_DONE = 98;
Jack Nudelmanb0b87642020-11-12 15:04:39 -0800345 private static final int CMD_SET_DATA_THROTTLING = 99;
346 private static final int EVENT_SET_DATA_THROTTLING_DONE = 100;
Jordan Liu109698e2020-11-24 14:50:34 -0800347 private static final int CMD_SET_SIM_POWER = 101;
348 private static final int EVENT_SET_SIM_POWER_DONE = 102;
Rambo Wanga5cc9b72021-01-07 10:51:54 -0800349 private static final int CMD_SET_SIGNAL_STRENGTH_UPDATE_REQUEST = 103;
350 private static final int EVENT_SET_SIGNAL_STRENGTH_UPDATE_REQUEST_DONE = 104;
351 private static final int CMD_CLEAR_SIGNAL_STRENGTH_UPDATE_REQUEST = 105;
352 private static final int EVENT_CLEAR_SIGNAL_STRENGTH_UPDATE_REQUEST_DONE = 106;
SongFerngWang3ef3e072020-12-21 16:41:52 +0800353 private static final int CMD_SET_ALLOWED_NETWORK_TYPES_FOR_REASON = 107;
354 private static final int EVENT_SET_ALLOWED_NETWORK_TYPES_FOR_REASON_DONE = 108;
Michele Berionne5e411512020-11-13 02:36:59 +0000355 private static final int CMD_PREPARE_UNATTENDED_REBOOT = 109;
Hongbo Zeng156aa4a2021-02-08 21:50:28 +0800356 private static final int CMD_GET_SLICING_CONFIG = 110;
357 private static final int EVENT_GET_SLICING_CONFIG_DONE = 111;
Kai Shif70f46f2021-03-03 13:59:46 -0800358 private static final int CMD_ERASE_DATA_SHARED_PREFERENCES = 112;
Sooraj Sasindrandaf060f2021-06-15 14:52:55 -0700359 private static final int CMD_ENABLE_VONR = 113;
360 private static final int EVENT_ENABLE_VONR_DONE = 114;
361 private static final int CMD_IS_VONR_ENABLED = 115;
362 private static final int EVENT_IS_VONR_ENABLED_DONE = 116;
Sarah Chin2ec39f62022-08-31 17:03:26 -0700363 private static final int CMD_PURCHASE_PREMIUM_CAPABILITY = 117;
364 private static final int EVENT_PURCHASE_PREMIUM_CAPABILITY_DONE = 118;
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;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700387
Peter Wangdafb9ac2020-01-15 14:13:38 -0800388 /** User Activity */
389 private AtomicBoolean mNotifyUserActivity;
Peter Wangdafb9ac2020-01-15 14:13:38 -0800390 private static final int USER_ACTIVITY_NOTIFICATION_DELAY = 200;
391
Jeff Davidson8ab02b22020-03-28 12:24:40 -0700392 private Set<Integer> mCarrierPrivilegeTestOverrideSubIds = new ArraySet<>();
393
Derek Tan97ebb422014-09-05 16:55:38 -0700394 private static final String PREF_CARRIERS_ALPHATAG_PREFIX = "carrier_alphtag_";
395 private static final String PREF_CARRIERS_NUMBER_PREFIX = "carrier_number_";
Jeff Sharkey85190e62014-12-05 09:40:12 -0800396 private static final String PREF_CARRIERS_SUBSCRIBER_PREFIX = "carrier_subscriber_";
Brad Ebinger1c8542e2019-01-14 13:43:14 -0800397 private static final String PREF_PROVISION_IMS_MMTEL_PREFIX = "provision_ims_mmtel_";
Derek Tan89e89d42014-07-08 17:00:10 -0700398
Michelecea4cf22018-12-21 15:00:11 -0800399 // String to store multi SIM allowed
400 private static final String PREF_MULTI_SIM_RESTRICTED = "multisim_restricted";
401
Derek Tan740e1672017-06-27 14:56:27 -0700402 // The AID of ISD-R.
403 private static final String ISDR_AID = "A0000005591010FFFFFFFF8900000100";
404
yinxub1bed742017-04-17 11:45:04 -0700405 private NetworkScanRequestTracker mNetworkScanRequestTracker;
406
David Kelly5e06a7f2018-03-12 14:10:59 +0000407 private static final int TYPE_ALLOCATION_CODE_LENGTH = 8;
408 private static final int MANUFACTURER_CODE_LENGTH = 8;
409
Jack Nudelmanb0b87642020-11-12 15:04:39 -0800410 private static final int SET_DATA_THROTTLING_MODEM_THREW_INVALID_PARAMS = -1;
Jack Nudelman5d6a98b2021-03-04 14:26:25 -0800411 private static final int MODEM_DOES_NOT_SUPPORT_DATA_THROTTLING_ERROR_CODE = -2;
Jack Nudelmanb0b87642020-11-12 15:04:39 -0800412
Sarah Chin2ec39f62022-08-31 17:03:26 -0700413 private static final String PURCHASE_PREMIUM_CAPABILITY_ERROR_UUID =
414 "24bf97a6-e8a6-44d8-a6a4-255d7548733c";
415
Derek Tan89e89d42014-07-08 17:00:10 -0700416 /**
Naina Nallurid63128d2019-09-17 14:10:30 -0700417 * Experiment flag to enable erase modem config on reset network, default value is false
418 */
419 public static final String RESET_NETWORK_ERASE_MODEM_CONFIG_ENABLED =
420 "reset_network_erase_modem_config_enabled";
421
Rambo Wang0f050d82021-02-12 11:43:36 -0800422 private static final int SET_NETWORK_SELECTION_MODE_AUTOMATIC_TIMEOUT_MS = 2000; // 2 seconds
Chen Xu540470b2021-12-14 17:15:47 -0800423
sandeepjsb6c87872021-09-27 15:34:44 +0000424 /**
425 * With support for MEP(multiple enabled profile) in Android T, a SIM card can have more than
426 * one ICCID active at the same time.
427 * Apps should use below API signatures if targeting SDK is T and beyond.
428 *
429 * @hide
430 */
431 @ChangeId
432 @EnabledSince(targetSdkVersion = Build.VERSION_CODES.TIRAMISU)
433 public static final long GET_API_SIGNATURES_FROM_UICC_PORT_INFO = 202110963L;
Rambo Wang0f050d82021-02-12 11:43:36 -0800434
Naina Nallurid63128d2019-09-17 14:10:30 -0700435 /**
Chen Xu540470b2021-12-14 17:15:47 -0800436 * Apps targeting on Android T and beyond will get exception whenever icc close channel
437 * operation fails.
438 */
439 @ChangeId
440 @EnabledSince(targetSdkVersion = Build.VERSION_CODES.TIRAMISU)
441 public static final long ICC_CLOSE_CHANNEL_EXCEPTION_ON_FAILURE = 208739934L;
442
443 /**
Shishir Agrawal566b7612013-10-28 14:41:00 -0700444 * A request object to use for transmitting data to an ICC.
445 */
446 private static final class IccAPDUArgument {
447 public int channel, cla, command, p1, p2, p3;
448 public String data;
449
450 public IccAPDUArgument(int channel, int cla, int command,
451 int p1, int p2, int p3, String data) {
452 this.channel = channel;
453 this.cla = cla;
454 this.command = command;
455 this.p1 = p1;
456 this.p2 = p2;
457 this.p3 = p3;
458 this.data = data;
459 }
460 }
461
462 /**
Shishir Agrawal77ba3172015-09-10 14:50:19 -0700463 * A request object to use for transmitting data to an ICC.
464 */
465 private static final class ManualNetworkSelectionArgument {
466 public OperatorInfo operatorInfo;
467 public boolean persistSelection;
468
469 public ManualNetworkSelectionArgument(OperatorInfo operatorInfo, boolean persistSelection) {
470 this.operatorInfo = operatorInfo;
471 this.persistSelection = persistSelection;
472 }
473 }
474
475 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700476 * A request object for use with {@link MainThreadHandler}. Requesters should wait() on the
477 * request after sending. The main thread will notify the request when it is complete.
478 */
479 private static final class MainThreadRequest {
480 /** The argument to use for the request */
481 public Object argument;
482 /** The result of the request that is run on the main thread */
483 public Object result;
Sanket Padawe56e75a32016-02-08 12:18:19 -0800484 // The subscriber id that this request applies to. Defaults to
485 // SubscriptionManager.INVALID_SUBSCRIPTION_ID
486 public Integer subId = SubscriptionManager.INVALID_SUBSCRIPTION_ID;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700487
Nathan Harold92bed182018-10-12 18:16:49 -0700488 // In cases where subId is unavailable, the caller needs to specify the phone.
489 public Phone phone;
490
vagdeviaf9a5b92018-08-15 16:01:53 -0700491 public WorkSource workSource;
492
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700493 public MainThreadRequest(Object argument) {
494 this.argument = argument;
495 }
Shishir Agrawal76d5da92014-11-09 16:17:25 -0800496
Nathan Harold92bed182018-10-12 18:16:49 -0700497 MainThreadRequest(Object argument, Phone phone, WorkSource workSource) {
498 this.argument = argument;
499 if (phone != null) {
500 this.phone = phone;
501 }
502 this.workSource = workSource;
503 }
504
vagdeviaf9a5b92018-08-15 16:01:53 -0700505 MainThreadRequest(Object argument, Integer subId, WorkSource workSource) {
Shishir Agrawal76d5da92014-11-09 16:17:25 -0800506 this.argument = argument;
Sanket Padawe56e75a32016-02-08 12:18:19 -0800507 if (subId != null) {
508 this.subId = subId;
509 }
vagdeviaf9a5b92018-08-15 16:01:53 -0700510 this.workSource = workSource;
Shishir Agrawal76d5da92014-11-09 16:17:25 -0800511 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700512 }
513
Sailesh Nepalcc0375f2013-11-13 09:15:18 -0800514 private static final class IncomingThirdPartyCallArgs {
515 public final ComponentName component;
516 public final String callId;
517 public final String callerDisplayName;
518
519 public IncomingThirdPartyCallArgs(ComponentName component, String callId,
520 String callerDisplayName) {
521 this.component = component;
522 this.callId = callId;
523 this.callerDisplayName = callerDisplayName;
524 }
525 }
526
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700527 /**
528 * A handler that processes messages on the main thread in the phone process. Since many
529 * of the Phone calls are not thread safe this is needed to shuttle the requests from the
530 * inbound binder threads to the main thread in the phone process. The Binder thread
531 * may provide a {@link MainThreadRequest} object in the msg.obj field that they are waiting
532 * on, which will be notified when the operation completes and will contain the result of the
533 * request.
534 *
535 * <p>If a MainThreadRequest object is provided in the msg.obj field,
536 * note that request.result must be set to something non-null for the calling thread to
537 * unblock.
538 */
539 private final class MainThreadHandler extends Handler {
540 @Override
541 public void handleMessage(Message msg) {
542 MainThreadRequest request;
543 Message onCompleted;
544 AsyncResult ar;
Muralidhar Reddy472c2ae2021-09-29 19:38:40 +0000545 UiccPort uiccPort;
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700546 IccAPDUArgument iccArgument;
Nazanin Bakhshif782e562018-12-11 15:15:39 -0800547 final Phone defaultPhone = getDefaultPhone();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700548
549 switch (msg.what) {
Pengquan Menga1bb6272018-09-06 09:59:22 -0700550 case CMD_HANDLE_USSD_REQUEST: {
551 request = (MainThreadRequest) msg.obj;
552 final Phone phone = getPhoneFromRequest(request);
553 Pair<String, ResultReceiver> ussdObject = (Pair) request.argument;
Chen Xue9d737e2022-01-01 23:41:31 -0800554 String ussdRequest = ussdObject.first;
Pengquan Menga1bb6272018-09-06 09:59:22 -0700555 ResultReceiver wrappedCallback = ussdObject.second;
Tyler Gunn65d45c22017-06-05 11:22:26 -0700556
Pengquan Menga1bb6272018-09-06 09:59:22 -0700557 if (!isUssdApiAllowed(request.subId)) {
558 // Carrier does not support use of this API, return failure.
559 Rlog.w(LOG_TAG, "handleUssdRequest: carrier does not support USSD apis.");
560 UssdResponse response = new UssdResponse(ussdRequest, null);
561 Bundle returnData = new Bundle();
562 returnData.putParcelable(TelephonyManager.USSD_RESPONSE, response);
563 wrappedCallback.send(TelephonyManager.USSD_RETURN_FAILURE, returnData);
Tyler Gunn65d45c22017-06-05 11:22:26 -0700564
Pengquan Menga1bb6272018-09-06 09:59:22 -0700565 request.result = true;
566 notifyRequester(request);
567 return;
568 }
Tyler Gunn65d45c22017-06-05 11:22:26 -0700569
Pengquan Menga1bb6272018-09-06 09:59:22 -0700570 try {
571 request.result = phone != null
572 ? phone.handleUssdRequest(ussdRequest, wrappedCallback) : false;
573 } catch (CallStateException cse) {
574 request.result = false;
575 }
576 // Wake up the requesting thread
577 notifyRequester(request);
578 break;
pkanwar32d516d2016-10-14 19:37:38 -0700579 }
580
Yorke Lee716f67e2015-06-17 15:39:16 -0700581 case CMD_HANDLE_PIN_MMI: {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700582 request = (MainThreadRequest) msg.obj;
Yorke Lee716f67e2015-06-17 15:39:16 -0700583 final Phone phone = getPhoneFromRequest(request);
584 request.result = phone != null ?
585 getPhoneFromRequest(request).handlePinMmi((String) request.argument)
586 : false;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700587 // Wake up the requesting thread
Pengquan Menga1bb6272018-09-06 09:59:22 -0700588 notifyRequester(request);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700589 break;
Yorke Lee716f67e2015-06-17 15:39:16 -0700590 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700591
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700592 case CMD_TRANSMIT_APDU_LOGICAL_CHANNEL:
Shishir Agrawal566b7612013-10-28 14:41:00 -0700593 request = (MainThreadRequest) msg.obj;
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700594 iccArgument = (IccAPDUArgument) request.argument;
Muralidhar Reddy472c2ae2021-09-29 19:38:40 +0000595 uiccPort = getUiccPortFromRequest(request);
596 if (uiccPort == null) {
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700597 loge("iccTransmitApduLogicalChannel: No UICC");
Chen Xue9d737e2022-01-01 23:41:31 -0800598 request.result = new IccIoResult(0x6F, 0, (byte[]) null);
Pengquan Menga1bb6272018-09-06 09:59:22 -0700599 notifyRequester(request);
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700600 } else {
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700601 onCompleted = obtainMessage(EVENT_TRANSMIT_APDU_LOGICAL_CHANNEL_DONE,
Chen Xue9d737e2022-01-01 23:41:31 -0800602 request);
Muralidhar Reddy472c2ae2021-09-29 19:38:40 +0000603 uiccPort.iccTransmitApduLogicalChannel(
Chen Xue9d737e2022-01-01 23:41:31 -0800604 iccArgument.channel, iccArgument.cla, iccArgument.command,
605 iccArgument.p1, iccArgument.p2, iccArgument.p3, iccArgument.data,
606 onCompleted);
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700607 }
Shishir Agrawal566b7612013-10-28 14:41:00 -0700608 break;
609
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700610 case EVENT_TRANSMIT_APDU_LOGICAL_CHANNEL_DONE:
Shishir Agrawal566b7612013-10-28 14:41:00 -0700611 ar = (AsyncResult) msg.obj;
612 request = (MainThreadRequest) ar.userObj;
613 if (ar.exception == null && ar.result != null) {
614 request.result = ar.result;
615 } else {
Chen Xue9d737e2022-01-01 23:41:31 -0800616 request.result = new IccIoResult(0x6F, 0, (byte[]) null);
Shishir Agrawal566b7612013-10-28 14:41:00 -0700617 if (ar.result == null) {
618 loge("iccTransmitApduLogicalChannel: Empty response");
Jake Hambye994d462014-02-03 13:10:13 -0800619 } else if (ar.exception instanceof CommandException) {
Shishir Agrawal566b7612013-10-28 14:41:00 -0700620 loge("iccTransmitApduLogicalChannel: CommandException: " +
Jake Hambye994d462014-02-03 13:10:13 -0800621 ar.exception);
Shishir Agrawal566b7612013-10-28 14:41:00 -0700622 } else {
623 loge("iccTransmitApduLogicalChannel: Unknown exception");
624 }
625 }
Pengquan Menga1bb6272018-09-06 09:59:22 -0700626 notifyRequester(request);
Shishir Agrawal566b7612013-10-28 14:41:00 -0700627 break;
628
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700629 case CMD_TRANSMIT_APDU_BASIC_CHANNEL:
630 request = (MainThreadRequest) msg.obj;
631 iccArgument = (IccAPDUArgument) request.argument;
Muralidhar Reddy472c2ae2021-09-29 19:38:40 +0000632 uiccPort = getUiccPortFromRequest(request);
633 if (uiccPort == null) {
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700634 loge("iccTransmitApduBasicChannel: No UICC");
Chen Xue9d737e2022-01-01 23:41:31 -0800635 request.result = new IccIoResult(0x6F, 0, (byte[]) null);
Pengquan Menga1bb6272018-09-06 09:59:22 -0700636 notifyRequester(request);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700637 } else {
638 onCompleted = obtainMessage(EVENT_TRANSMIT_APDU_BASIC_CHANNEL_DONE,
Chen Xue9d737e2022-01-01 23:41:31 -0800639 request);
Muralidhar Reddy472c2ae2021-09-29 19:38:40 +0000640 uiccPort.iccTransmitApduBasicChannel(
Chen Xue9d737e2022-01-01 23:41:31 -0800641 iccArgument.cla, iccArgument.command, iccArgument.p1,
642 iccArgument.p2,
643 iccArgument.p3, iccArgument.data, onCompleted);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700644 }
645 break;
646
647 case EVENT_TRANSMIT_APDU_BASIC_CHANNEL_DONE:
648 ar = (AsyncResult) msg.obj;
649 request = (MainThreadRequest) ar.userObj;
650 if (ar.exception == null && ar.result != null) {
651 request.result = ar.result;
652 } else {
Chen Xue9d737e2022-01-01 23:41:31 -0800653 request.result = new IccIoResult(0x6F, 0, (byte[]) null);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700654 if (ar.result == null) {
655 loge("iccTransmitApduBasicChannel: Empty response");
656 } else if (ar.exception instanceof CommandException) {
657 loge("iccTransmitApduBasicChannel: CommandException: " +
658 ar.exception);
659 } else {
660 loge("iccTransmitApduBasicChannel: Unknown exception");
661 }
662 }
Pengquan Menga1bb6272018-09-06 09:59:22 -0700663 notifyRequester(request);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700664 break;
665
666 case CMD_EXCHANGE_SIM_IO:
667 request = (MainThreadRequest) msg.obj;
668 iccArgument = (IccAPDUArgument) request.argument;
Muralidhar Reddy472c2ae2021-09-29 19:38:40 +0000669 uiccPort = getUiccPortFromRequest(request);
670 if (uiccPort == null) {
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700671 loge("iccExchangeSimIO: No UICC");
Chen Xue9d737e2022-01-01 23:41:31 -0800672 request.result = new IccIoResult(0x6F, 0, (byte[]) null);
Pengquan Menga1bb6272018-09-06 09:59:22 -0700673 notifyRequester(request);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700674 } else {
675 onCompleted = obtainMessage(EVENT_EXCHANGE_SIM_IO_DONE,
676 request);
Muralidhar Reddy472c2ae2021-09-29 19:38:40 +0000677 uiccPort.iccExchangeSimIO(iccArgument.cla, /* fileID */
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700678 iccArgument.command, iccArgument.p1, iccArgument.p2, iccArgument.p3,
679 iccArgument.data, onCompleted);
680 }
681 break;
682
683 case EVENT_EXCHANGE_SIM_IO_DONE:
684 ar = (AsyncResult) msg.obj;
685 request = (MainThreadRequest) ar.userObj;
686 if (ar.exception == null && ar.result != null) {
687 request.result = ar.result;
688 } else {
Chen Xue9d737e2022-01-01 23:41:31 -0800689 request.result = new IccIoResult(0x6f, 0, (byte[]) null);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700690 }
Pengquan Menga1bb6272018-09-06 09:59:22 -0700691 notifyRequester(request);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700692 break;
693
Derek Tan4d5e5c12014-02-04 11:54:58 -0800694 case CMD_SEND_ENVELOPE:
695 request = (MainThreadRequest) msg.obj;
Muralidhar Reddy472c2ae2021-09-29 19:38:40 +0000696 uiccPort = getUiccPortFromRequest(request);
697 if (uiccPort == null) {
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700698 loge("sendEnvelopeWithStatus: No UICC");
Chen Xue9d737e2022-01-01 23:41:31 -0800699 request.result = new IccIoResult(0x6F, 0, (byte[]) null);
Pengquan Menga1bb6272018-09-06 09:59:22 -0700700 notifyRequester(request);
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700701 } else {
702 onCompleted = obtainMessage(EVENT_SEND_ENVELOPE_DONE, request);
Chen Xue9d737e2022-01-01 23:41:31 -0800703 uiccPort.sendEnvelopeWithStatus((String) request.argument, onCompleted);
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700704 }
Derek Tan4d5e5c12014-02-04 11:54:58 -0800705 break;
706
707 case EVENT_SEND_ENVELOPE_DONE:
708 ar = (AsyncResult) msg.obj;
709 request = (MainThreadRequest) ar.userObj;
Shishir Agrawal9f9877d2014-03-14 09:36:27 -0700710 if (ar.exception == null && ar.result != null) {
711 request.result = ar.result;
Derek Tan4d5e5c12014-02-04 11:54:58 -0800712 } else {
Chen Xue9d737e2022-01-01 23:41:31 -0800713 request.result = new IccIoResult(0x6F, 0, (byte[]) null);
Shishir Agrawal9f9877d2014-03-14 09:36:27 -0700714 if (ar.result == null) {
715 loge("sendEnvelopeWithStatus: Empty response");
716 } else if (ar.exception instanceof CommandException) {
717 loge("sendEnvelopeWithStatus: CommandException: " +
718 ar.exception);
719 } else {
720 loge("sendEnvelopeWithStatus: exception:" + ar.exception);
721 }
Derek Tan4d5e5c12014-02-04 11:54:58 -0800722 }
Pengquan Menga1bb6272018-09-06 09:59:22 -0700723 notifyRequester(request);
Derek Tan4d5e5c12014-02-04 11:54:58 -0800724 break;
725
Shishir Agrawal566b7612013-10-28 14:41:00 -0700726 case CMD_OPEN_CHANNEL:
727 request = (MainThreadRequest) msg.obj;
Muralidhar Reddy472c2ae2021-09-29 19:38:40 +0000728 uiccPort = getUiccPortFromRequest(request);
Rambo Wanga1782702021-11-10 20:15:19 -0800729 IccLogicalChannelRequest openChannelRequest =
730 (IccLogicalChannelRequest) request.argument;
Muralidhar Reddy472c2ae2021-09-29 19:38:40 +0000731 if (uiccPort == null) {
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700732 loge("iccOpenLogicalChannel: No UICC");
Shishir Agrawalfc0492a2016-02-17 11:15:33 -0800733 request.result = new IccOpenLogicalChannelResponse(-1,
Chen Xue9d737e2022-01-01 23:41:31 -0800734 IccOpenLogicalChannelResponse.STATUS_MISSING_RESOURCE, null);
Pengquan Menga1bb6272018-09-06 09:59:22 -0700735 notifyRequester(request);
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700736 } else {
737 onCompleted = obtainMessage(EVENT_OPEN_CHANNEL_DONE, request);
Rambo Wanga1782702021-11-10 20:15:19 -0800738 uiccPort.iccOpenLogicalChannel(openChannelRequest.aid,
739 openChannelRequest.p2, onCompleted);
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700740 }
Shishir Agrawal566b7612013-10-28 14:41:00 -0700741 break;
742
743 case EVENT_OPEN_CHANNEL_DONE:
744 ar = (AsyncResult) msg.obj;
745 request = (MainThreadRequest) ar.userObj;
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700746 IccOpenLogicalChannelResponse openChannelResp;
Shishir Agrawal566b7612013-10-28 14:41:00 -0700747 if (ar.exception == null && ar.result != null) {
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700748 int[] result = (int[]) ar.result;
749 int channelId = result[0];
750 byte[] selectResponse = null;
751 if (result.length > 1) {
752 selectResponse = new byte[result.length - 1];
753 for (int i = 1; i < result.length; ++i) {
754 selectResponse[i - 1] = (byte) result[i];
755 }
756 }
757 openChannelResp = new IccOpenLogicalChannelResponse(channelId,
Chen Xue9d737e2022-01-01 23:41:31 -0800758 IccOpenLogicalChannelResponse.STATUS_NO_ERROR, selectResponse);
Rambo Wang3b77c4c2021-11-10 20:15:19 -0800759
760 uiccPort = getUiccPortFromRequest(request);
Rambo Wange53e07d2022-05-10 13:01:13 -0700761 if (uiccPort == null) {
762 loge("EVENT_OPEN_CHANNEL_DONE: UiccPort is null");
763 } else {
764 IccLogicalChannelRequest channelRequest =
765 (IccLogicalChannelRequest) request.argument;
766 channelRequest.channel = channelId;
767 uiccPort.onLogicalChannelOpened(channelRequest);
768 }
Shishir Agrawal566b7612013-10-28 14:41:00 -0700769 } else {
Shishir Agrawal566b7612013-10-28 14:41:00 -0700770 if (ar.result == null) {
771 loge("iccOpenLogicalChannel: Empty response");
Shishir Agrawal566b7612013-10-28 14:41:00 -0700772 }
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700773 if (ar.exception != null) {
774 loge("iccOpenLogicalChannel: Exception: " + ar.exception);
775 }
776
Shishir Agrawal527e8bf2014-08-25 08:54:56 -0700777 int errorCode = IccOpenLogicalChannelResponse.STATUS_UNKNOWN_ERROR;
Junda Liua754ba12015-05-20 01:17:52 -0700778 if (ar.exception instanceof CommandException) {
779 CommandException.Error error =
Chen Xue9d737e2022-01-01 23:41:31 -0800780 ((CommandException) (ar.exception)).getCommandError();
Junda Liua754ba12015-05-20 01:17:52 -0700781 if (error == CommandException.Error.MISSING_RESOURCE) {
Shishir Agrawal527e8bf2014-08-25 08:54:56 -0700782 errorCode = IccOpenLogicalChannelResponse.STATUS_MISSING_RESOURCE;
Junda Liua754ba12015-05-20 01:17:52 -0700783 } else if (error == CommandException.Error.NO_SUCH_ELEMENT) {
Shishir Agrawal527e8bf2014-08-25 08:54:56 -0700784 errorCode = IccOpenLogicalChannelResponse.STATUS_NO_SUCH_ELEMENT;
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700785 }
786 }
787 openChannelResp = new IccOpenLogicalChannelResponse(
Chen Xue9d737e2022-01-01 23:41:31 -0800788 IccOpenLogicalChannelResponse.INVALID_CHANNEL, errorCode, null);
Shishir Agrawal566b7612013-10-28 14:41:00 -0700789 }
Shishir Agrawal82c8a462014-07-31 18:13:17 -0700790 request.result = openChannelResp;
Pengquan Menga1bb6272018-09-06 09:59:22 -0700791 notifyRequester(request);
Shishir Agrawal566b7612013-10-28 14:41:00 -0700792 break;
793
794 case CMD_CLOSE_CHANNEL:
795 request = (MainThreadRequest) msg.obj;
Muralidhar Reddy472c2ae2021-09-29 19:38:40 +0000796 uiccPort = getUiccPortFromRequest(request);
797 if (uiccPort == null) {
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700798 loge("iccCloseLogicalChannel: No UICC");
Chen Xua8f0dff2022-02-12 00:34:15 -0800799 request.result = new IllegalArgumentException(
Chen Xue9d737e2022-01-01 23:41:31 -0800800 "iccCloseLogicalChannel: No UICC");
Chen Xue9d737e2022-01-01 23:41:31 -0800801 notifyRequester(request);
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700802 } else {
803 onCompleted = obtainMessage(EVENT_CLOSE_CHANNEL_DONE, request);
Muralidhar Reddy472c2ae2021-09-29 19:38:40 +0000804 uiccPort.iccCloseLogicalChannel((Integer) request.argument, onCompleted);
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700805 }
Shishir Agrawal566b7612013-10-28 14:41:00 -0700806 break;
807
808 case EVENT_CLOSE_CHANNEL_DONE:
Chen Xu540470b2021-12-14 17:15:47 -0800809 ar = (AsyncResult) msg.obj;
810 request = (MainThreadRequest) ar.userObj;
811 if (ar.exception == null) {
812 request.result = true;
Rambo Wang3b77c4c2021-11-10 20:15:19 -0800813 uiccPort = getUiccPortFromRequest(request);
Rambo Wange53e07d2022-05-10 13:01:13 -0700814 if (uiccPort == null) {
815 loge("EVENT_CLOSE_CHANNEL_DONE: UiccPort is null");
816 } else {
817 final int channelId = (Integer) request.argument;
818 uiccPort.onLogicalChannelClosed(channelId);
819 }
Chen Xu540470b2021-12-14 17:15:47 -0800820 } else {
821 request.result = false;
Chen Xue9d737e2022-01-01 23:41:31 -0800822 Exception exception = null;
Chen Xu540470b2021-12-14 17:15:47 -0800823 if (ar.exception instanceof CommandException) {
824 loge("iccCloseLogicalChannel: CommandException: " + ar.exception);
825 CommandException.Error error =
826 ((CommandException) (ar.exception)).getCommandError();
Chen Xue9d737e2022-01-01 23:41:31 -0800827 if (error == CommandException.Error.INVALID_ARGUMENTS) {
828 // should only throw exceptions from the binder threads.
829 exception = new IllegalArgumentException(
Chen Xu540470b2021-12-14 17:15:47 -0800830 "iccCloseLogicalChannel: invalid argument ");
831 }
832 } else {
833 loge("iccCloseLogicalChannel: Unknown exception");
834 }
Chen Xua8f0dff2022-02-12 00:34:15 -0800835 request.result = (exception != null) ? exception :
836 new IllegalStateException(
837 "exception from modem to close iccLogical Channel");
Chen Xu540470b2021-12-14 17:15:47 -0800838 }
839 notifyRequester(request);
Jake Hambye994d462014-02-03 13:10:13 -0800840 break;
841
842 case CMD_NV_READ_ITEM:
843 request = (MainThreadRequest) msg.obj;
844 onCompleted = obtainMessage(EVENT_NV_READ_ITEM_DONE, request);
Nazanin Bakhshif782e562018-12-11 15:15:39 -0800845 defaultPhone.nvReadItem((Integer) request.argument, onCompleted,
846 request.workSource);
Jake Hambye994d462014-02-03 13:10:13 -0800847 break;
848
849 case EVENT_NV_READ_ITEM_DONE:
Shishir Agrawal566b7612013-10-28 14:41:00 -0700850 ar = (AsyncResult) msg.obj;
851 request = (MainThreadRequest) ar.userObj;
Jake Hambye994d462014-02-03 13:10:13 -0800852 if (ar.exception == null && ar.result != null) {
853 request.result = ar.result; // String
Shishir Agrawal566b7612013-10-28 14:41:00 -0700854 } else {
Jake Hambye994d462014-02-03 13:10:13 -0800855 request.result = "";
856 if (ar.result == null) {
857 loge("nvReadItem: Empty response");
858 } else if (ar.exception instanceof CommandException) {
859 loge("nvReadItem: CommandException: " +
860 ar.exception);
Shishir Agrawal566b7612013-10-28 14:41:00 -0700861 } else {
Jake Hambye994d462014-02-03 13:10:13 -0800862 loge("nvReadItem: Unknown exception");
Shishir Agrawal566b7612013-10-28 14:41:00 -0700863 }
864 }
Pengquan Menga1bb6272018-09-06 09:59:22 -0700865 notifyRequester(request);
Shishir Agrawal566b7612013-10-28 14:41:00 -0700866 break;
867
Jake Hambye994d462014-02-03 13:10:13 -0800868 case CMD_NV_WRITE_ITEM:
869 request = (MainThreadRequest) msg.obj;
870 onCompleted = obtainMessage(EVENT_NV_WRITE_ITEM_DONE, request);
871 Pair<Integer, String> idValue = (Pair<Integer, String>) request.argument;
Nazanin Bakhshif782e562018-12-11 15:15:39 -0800872 defaultPhone.nvWriteItem(idValue.first, idValue.second, onCompleted,
vagdeviaf9a5b92018-08-15 16:01:53 -0700873 request.workSource);
Jake Hambye994d462014-02-03 13:10:13 -0800874 break;
875
876 case EVENT_NV_WRITE_ITEM_DONE:
877 handleNullReturnEvent(msg, "nvWriteItem");
878 break;
879
880 case CMD_NV_WRITE_CDMA_PRL:
881 request = (MainThreadRequest) msg.obj;
882 onCompleted = obtainMessage(EVENT_NV_WRITE_CDMA_PRL_DONE, request);
Nazanin Bakhshif782e562018-12-11 15:15:39 -0800883 defaultPhone.nvWriteCdmaPrl((byte[]) request.argument, onCompleted);
Jake Hambye994d462014-02-03 13:10:13 -0800884 break;
885
886 case EVENT_NV_WRITE_CDMA_PRL_DONE:
887 handleNullReturnEvent(msg, "nvWriteCdmaPrl");
888 break;
889
chen xu6dac5ab2018-10-26 17:39:23 -0700890 case CMD_RESET_MODEM_CONFIG:
Jake Hambye994d462014-02-03 13:10:13 -0800891 request = (MainThreadRequest) msg.obj;
chen xu6dac5ab2018-10-26 17:39:23 -0700892 onCompleted = obtainMessage(EVENT_RESET_MODEM_CONFIG_DONE, request);
Nazanin Bakhshif782e562018-12-11 15:15:39 -0800893 defaultPhone.resetModemConfig(onCompleted);
Jake Hambye994d462014-02-03 13:10:13 -0800894 break;
895
chen xu6dac5ab2018-10-26 17:39:23 -0700896 case EVENT_RESET_MODEM_CONFIG_DONE:
897 handleNullReturnEvent(msg, "resetModemConfig");
Jake Hambye994d462014-02-03 13:10:13 -0800898 break;
899
Sooraj Sasindran37444802020-08-11 10:40:43 -0700900 case CMD_IS_NR_DUAL_CONNECTIVITY_ENABLED: {
901 request = (MainThreadRequest) msg.obj;
902 onCompleted = obtainMessage(EVENT_IS_NR_DUAL_CONNECTIVITY_ENABLED_DONE,
903 request);
904 Phone phone = getPhoneFromRequest(request);
905 if (phone != null) {
906 phone.isNrDualConnectivityEnabled(onCompleted, request.workSource);
907 } else {
908 loge("isNRDualConnectivityEnabled: No phone object");
909 request.result = false;
910 notifyRequester(request);
911 }
912 break;
913 }
914
915 case EVENT_IS_NR_DUAL_CONNECTIVITY_ENABLED_DONE:
916 ar = (AsyncResult) msg.obj;
917 request = (MainThreadRequest) ar.userObj;
918 if (ar.exception == null && ar.result != null) {
919 request.result = ar.result;
920 } else {
921 // request.result must be set to something non-null
922 // for the calling thread to unblock
Sooraj Sasindrandaf060f2021-06-15 14:52:55 -0700923 if (ar.result != null) {
Sooraj Sasindran37444802020-08-11 10:40:43 -0700924 request.result = ar.result;
925 } else {
926 request.result = false;
927 }
928 if (ar.result == null) {
929 loge("isNRDualConnectivityEnabled: Empty response");
930 } else if (ar.exception instanceof CommandException) {
931 loge("isNRDualConnectivityEnabled: CommandException: "
932 + ar.exception);
933 } else {
934 loge("isNRDualConnectivityEnabled: Unknown exception");
935 }
936 }
937 notifyRequester(request);
938 break;
939
Sooraj Sasindrandaf060f2021-06-15 14:52:55 -0700940 case CMD_IS_VONR_ENABLED: {
941 request = (MainThreadRequest) msg.obj;
942 onCompleted = obtainMessage(EVENT_IS_VONR_ENABLED_DONE,
943 request);
944 Phone phone = getPhoneFromRequest(request);
945 if (phone != null) {
946 phone.isVoNrEnabled(onCompleted, request.workSource);
947 } else {
948 loge("isVoNrEnabled: No phone object");
949 request.result = false;
950 notifyRequester(request);
951 }
952 break;
953 }
954
955 case EVENT_IS_VONR_ENABLED_DONE:
956 ar = (AsyncResult) msg.obj;
957 request = (MainThreadRequest) ar.userObj;
958 if (ar.exception == null && ar.result != null) {
959 request.result = ar.result;
960 } else {
961 // request.result must be set to something non-null
962 // for the calling thread to unblock
963 if (ar.result != null) {
964 request.result = ar.result;
965 } else {
966 request.result = false;
967 }
968 if (ar.result == null) {
969 loge("isVoNrEnabled: Empty response");
970 } else if (ar.exception instanceof CommandException) {
971 loge("isVoNrEnabled: CommandException: "
972 + ar.exception);
973 } else {
974 loge("isVoNrEnabled: Unknown exception");
975 }
976 }
977 notifyRequester(request);
978 break;
979
Sooraj Sasindran37444802020-08-11 10:40:43 -0700980 case CMD_ENABLE_NR_DUAL_CONNECTIVITY: {
981 request = (MainThreadRequest) msg.obj;
982 onCompleted = obtainMessage(EVENT_ENABLE_NR_DUAL_CONNECTIVITY_DONE, request);
983 Phone phone = getPhoneFromRequest(request);
984 if (phone != null) {
985 phone.setNrDualConnectivityState((int) request.argument, onCompleted,
986 request.workSource);
987 } else {
988 loge("enableNrDualConnectivity: No phone object");
989 request.result =
990 TelephonyManager.ENABLE_NR_DUAL_CONNECTIVITY_RADIO_NOT_AVAILABLE;
991 notifyRequester(request);
992 }
993 break;
994 }
995
996 case EVENT_ENABLE_NR_DUAL_CONNECTIVITY_DONE: {
997 ar = (AsyncResult) msg.obj;
998 request = (MainThreadRequest) ar.userObj;
999 if (ar.exception == null) {
1000 request.result =
1001 TelephonyManager.ENABLE_NR_DUAL_CONNECTIVITY_SUCCESS;
1002 } else {
1003 request.result =
1004 TelephonyManager
1005 .ENABLE_NR_DUAL_CONNECTIVITY_RADIO_ERROR;
1006 if (ar.exception instanceof CommandException) {
1007 CommandException.Error error =
1008 ((CommandException) (ar.exception)).getCommandError();
1009 if (error == CommandException.Error.RADIO_NOT_AVAILABLE) {
1010 request.result =
1011 TelephonyManager
1012 .ENABLE_NR_DUAL_CONNECTIVITY_RADIO_NOT_AVAILABLE;
Sooraj Sasindran29654162021-03-03 23:00:01 +00001013 } else if (error == CommandException.Error.REQUEST_NOT_SUPPORTED) {
1014 request.result =
1015 TelephonyManager
1016 .ENABLE_NR_DUAL_CONNECTIVITY_NOT_SUPPORTED;
Sooraj Sasindran37444802020-08-11 10:40:43 -07001017 }
1018 loge("enableNrDualConnectivity" + ": CommandException: "
1019 + ar.exception);
1020 } else {
1021 loge("enableNrDualConnectivity" + ": Unknown exception");
1022 }
1023 }
1024 notifyRequester(request);
1025 break;
1026 }
1027
Sooraj Sasindrandaf060f2021-06-15 14:52:55 -07001028 case CMD_ENABLE_VONR: {
1029 request = (MainThreadRequest) msg.obj;
1030 onCompleted = obtainMessage(EVENT_ENABLE_VONR_DONE, request);
1031 Phone phone = getPhoneFromRequest(request);
1032 if (phone != null) {
1033 phone.setVoNrEnabled((boolean) request.argument, onCompleted,
1034 request.workSource);
1035 } else {
1036 loge("setVoNrEnabled: No phone object");
1037 request.result =
1038 TelephonyManager.ENABLE_VONR_RADIO_NOT_AVAILABLE;
1039 notifyRequester(request);
1040 }
1041 break;
1042 }
1043
1044 case EVENT_ENABLE_VONR_DONE: {
1045 ar = (AsyncResult) msg.obj;
1046 request = (MainThreadRequest) ar.userObj;
1047 if (ar.exception == null) {
1048 request.result = TelephonyManager.ENABLE_VONR_SUCCESS;
1049 } else {
1050 request.result = TelephonyManager.ENABLE_VONR_RADIO_ERROR;
1051 if (ar.exception instanceof CommandException) {
1052 CommandException.Error error =
1053 ((CommandException) (ar.exception)).getCommandError();
1054 if (error == CommandException.Error.RADIO_NOT_AVAILABLE) {
1055 request.result = TelephonyManager.ENABLE_VONR_RADIO_NOT_AVAILABLE;
1056 } else if (error == CommandException.Error.REQUEST_NOT_SUPPORTED) {
1057 request.result = TelephonyManager.ENABLE_VONR_REQUEST_NOT_SUPPORTED;
1058 } else {
1059 request.result = TelephonyManager.ENABLE_VONR_RADIO_ERROR;
1060 }
1061 loge("setVoNrEnabled" + ": CommandException: "
1062 + ar.exception);
1063 } else {
1064 loge("setVoNrEnabled" + ": Unknown exception");
1065 }
1066 }
1067 notifyRequester(request);
1068 break;
1069 }
1070
SongFerngWang3ef3e072020-12-21 16:41:52 +08001071 case CMD_GET_ALLOWED_NETWORK_TYPES_BITMASK:
Jake Hamby7c27be32014-03-03 13:25:59 -08001072 request = (MainThreadRequest) msg.obj;
SongFerngWang3ef3e072020-12-21 16:41:52 +08001073 onCompleted = obtainMessage(EVENT_GET_ALLOWED_NETWORK_TYPES_BITMASK_DONE,
1074 request);
1075 getPhoneFromRequest(request).getAllowedNetworkTypesBitmask(onCompleted);
Jake Hamby7c27be32014-03-03 13:25:59 -08001076 break;
1077
SongFerngWang3ef3e072020-12-21 16:41:52 +08001078 case EVENT_GET_ALLOWED_NETWORK_TYPES_BITMASK_DONE:
Jake Hamby7c27be32014-03-03 13:25:59 -08001079 ar = (AsyncResult) msg.obj;
1080 request = (MainThreadRequest) ar.userObj;
1081 if (ar.exception == null && ar.result != null) {
1082 request.result = ar.result; // Integer
1083 } else {
Nazish Tabassume8ba43a2020-07-28 14:49:25 +05301084 // request.result must be set to something non-null
1085 // for the calling thread to unblock
1086 request.result = new int[]{-1};
Jake Hamby7c27be32014-03-03 13:25:59 -08001087 if (ar.result == null) {
SongFerngWang3ef3e072020-12-21 16:41:52 +08001088 loge("getAllowedNetworkTypesBitmask: Empty response");
Jake Hamby7c27be32014-03-03 13:25:59 -08001089 } else if (ar.exception instanceof CommandException) {
SongFerngWang3ef3e072020-12-21 16:41:52 +08001090 loge("getAllowedNetworkTypesBitmask: CommandException: "
1091 + ar.exception);
Jake Hamby7c27be32014-03-03 13:25:59 -08001092 } else {
SongFerngWang3ef3e072020-12-21 16:41:52 +08001093 loge("getAllowedNetworkTypesBitmask: Unknown exception");
Jake Hamby7c27be32014-03-03 13:25:59 -08001094 }
1095 }
Pengquan Menga1bb6272018-09-06 09:59:22 -07001096 notifyRequester(request);
Jake Hamby7c27be32014-03-03 13:25:59 -08001097 break;
1098
SongFerngWang3ef3e072020-12-21 16:41:52 +08001099 case CMD_SET_ALLOWED_NETWORK_TYPES_FOR_REASON:
Jake Hamby7c27be32014-03-03 13:25:59 -08001100 request = (MainThreadRequest) msg.obj;
SongFerngWang3ef3e072020-12-21 16:41:52 +08001101 onCompleted = obtainMessage(EVENT_SET_ALLOWED_NETWORK_TYPES_FOR_REASON_DONE,
1102 request);
1103 Pair<Integer, Long> reasonWithNetworkTypes =
1104 (Pair<Integer, Long>) request.argument;
1105 getPhoneFromRequest(request).setAllowedNetworkTypes(
1106 reasonWithNetworkTypes.first,
1107 reasonWithNetworkTypes.second,
1108 onCompleted);
Jake Hamby7c27be32014-03-03 13:25:59 -08001109 break;
1110
SongFerngWang3ef3e072020-12-21 16:41:52 +08001111 case EVENT_SET_ALLOWED_NETWORK_TYPES_FOR_REASON_DONE:
1112 handleNullReturnEvent(msg, "setAllowedNetworkTypesForReason");
Jake Hamby7c27be32014-03-03 13:25:59 -08001113 break;
1114
Shuo Qian850e4d6a2018-04-25 21:02:08 +00001115 case CMD_INVOKE_OEM_RIL_REQUEST_RAW:
1116 request = (MainThreadRequest)msg.obj;
1117 onCompleted = obtainMessage(EVENT_INVOKE_OEM_RIL_REQUEST_RAW_DONE, request);
Nazanin Bakhshif782e562018-12-11 15:15:39 -08001118 defaultPhone.invokeOemRilRequestRaw((byte[]) request.argument, onCompleted);
Shuo Qian850e4d6a2018-04-25 21:02:08 +00001119 break;
1120
1121 case EVENT_INVOKE_OEM_RIL_REQUEST_RAW_DONE:
1122 ar = (AsyncResult)msg.obj;
1123 request = (MainThreadRequest)ar.userObj;
1124 request.result = ar;
Pengquan Menga1bb6272018-09-06 09:59:22 -07001125 notifyRequester(request);
Shuo Qian850e4d6a2018-04-25 21:02:08 +00001126 break;
1127
Shishir Agrawal76d5da92014-11-09 16:17:25 -08001128 case CMD_SET_VOICEMAIL_NUMBER:
1129 request = (MainThreadRequest) msg.obj;
1130 onCompleted = obtainMessage(EVENT_SET_VOICEMAIL_NUMBER_DONE, request);
1131 Pair<String, String> tagNum = (Pair<String, String>) request.argument;
Stuart Scott584921c2015-01-15 17:10:34 -08001132 getPhoneFromRequest(request).setVoiceMailNumber(tagNum.first, tagNum.second,
1133 onCompleted);
Shishir Agrawal76d5da92014-11-09 16:17:25 -08001134 break;
1135
1136 case EVENT_SET_VOICEMAIL_NUMBER_DONE:
1137 handleNullReturnEvent(msg, "setVoicemailNumber");
1138 break;
1139
Stuart Scott54788802015-03-30 13:18:01 -07001140 case CMD_SET_NETWORK_SELECTION_MODE_AUTOMATIC:
1141 request = (MainThreadRequest) msg.obj;
1142 onCompleted = obtainMessage(EVENT_SET_NETWORK_SELECTION_MODE_AUTOMATIC_DONE,
1143 request);
1144 getPhoneFromRequest(request).setNetworkSelectionModeAutomatic(onCompleted);
1145 break;
1146
1147 case EVENT_SET_NETWORK_SELECTION_MODE_AUTOMATIC_DONE:
1148 handleNullReturnEvent(msg, "setNetworkSelectionModeAutomatic");
1149 break;
1150
Shishir Agrawal302c8692015-06-19 13:49:39 -07001151 case CMD_PERFORM_NETWORK_SCAN:
1152 request = (MainThreadRequest) msg.obj;
1153 onCompleted = obtainMessage(EVENT_PERFORM_NETWORK_SCAN_DONE, request);
1154 getPhoneFromRequest(request).getAvailableNetworks(onCompleted);
1155 break;
1156
Hall Liu27d24262020-09-18 19:04:59 -07001157 case CMD_GET_CALL_FORWARDING: {
Shuo Qian4a594052020-01-23 11:59:30 -08001158 request = (MainThreadRequest) msg.obj;
1159 onCompleted = obtainMessage(EVENT_GET_CALL_FORWARDING_DONE, request);
Hall Liu27d24262020-09-18 19:04:59 -07001160 Pair<Integer, TelephonyManager.CallForwardingInfoCallback> args =
1161 (Pair<Integer, TelephonyManager.CallForwardingInfoCallback>)
1162 request.argument;
1163 int callForwardingReason = args.first;
1164 request.phone.getCallForwardingOption(callForwardingReason, onCompleted);
Shuo Qian4a594052020-01-23 11:59:30 -08001165 break;
Hall Liu27d24262020-09-18 19:04:59 -07001166 }
1167 case EVENT_GET_CALL_FORWARDING_DONE: {
Shuo Qian4a594052020-01-23 11:59:30 -08001168 ar = (AsyncResult) msg.obj;
1169 request = (MainThreadRequest) ar.userObj;
Hall Liu27d24262020-09-18 19:04:59 -07001170 TelephonyManager.CallForwardingInfoCallback callback =
1171 ((Pair<Integer, TelephonyManager.CallForwardingInfoCallback>)
1172 request.argument).second;
Shuo Qian4a594052020-01-23 11:59:30 -08001173 if (ar.exception == null && ar.result != null) {
Hall Liu27d24262020-09-18 19:04:59 -07001174 CallForwardingInfo callForwardingInfo = null;
Shuo Qian4a594052020-01-23 11:59:30 -08001175 CallForwardInfo[] callForwardInfos = (CallForwardInfo[]) ar.result;
1176 for (CallForwardInfo callForwardInfo : callForwardInfos) {
1177 // Service Class is a bit mask per 3gpp 27.007. Search for
1178 // any service for voice call.
1179 if ((callForwardInfo.serviceClass
1180 & CommandsInterface.SERVICE_CLASS_VOICE) > 0) {
Yuchen Dong69cc1412021-09-27 20:27:01 +08001181 callForwardingInfo = new CallForwardingInfo(
1182 callForwardInfo.status
1183 == CommandsInterface.CF_ACTION_ENABLE,
Hall Liu27d24262020-09-18 19:04:59 -07001184 callForwardInfo.reason,
1185 callForwardInfo.number,
1186 callForwardInfo.timeSeconds);
Shuo Qian4a594052020-01-23 11:59:30 -08001187 break;
1188 }
1189 }
1190 // Didn't find a call forward info for voice call.
1191 if (callForwardingInfo == null) {
Hall Liu27d24262020-09-18 19:04:59 -07001192 callForwardingInfo = new CallForwardingInfo(false /* enabled */,
1193 0 /* reason */, null /* number */, 0 /* timeout */);
Shuo Qian4a594052020-01-23 11:59:30 -08001194 }
Hall Liu27d24262020-09-18 19:04:59 -07001195 callback.onCallForwardingInfoAvailable(callForwardingInfo);
Shuo Qian4a594052020-01-23 11:59:30 -08001196 } else {
1197 if (ar.result == null) {
1198 loge("EVENT_GET_CALL_FORWARDING_DONE: Empty response");
1199 }
1200 if (ar.exception != null) {
1201 loge("EVENT_GET_CALL_FORWARDING_DONE: Exception: " + ar.exception);
1202 }
Hall Liu940c4ca2020-09-29 17:10:18 -07001203 int errorCode = TelephonyManager
1204 .CallForwardingInfoCallback.RESULT_ERROR_UNKNOWN;
Shuo Qian4a594052020-01-23 11:59:30 -08001205 if (ar.exception instanceof CommandException) {
1206 CommandException.Error error =
1207 ((CommandException) (ar.exception)).getCommandError();
1208 if (error == CommandException.Error.FDN_CHECK_FAILURE) {
Hall Liu940c4ca2020-09-29 17:10:18 -07001209 errorCode = TelephonyManager
1210 .CallForwardingInfoCallback.RESULT_ERROR_FDN_CHECK_FAILURE;
Shuo Qian4a594052020-01-23 11:59:30 -08001211 } else if (error == CommandException.Error.REQUEST_NOT_SUPPORTED) {
Hall Liu940c4ca2020-09-29 17:10:18 -07001212 errorCode = TelephonyManager
1213 .CallForwardingInfoCallback.RESULT_ERROR_NOT_SUPPORTED;
Shuo Qian4a594052020-01-23 11:59:30 -08001214 }
1215 }
Hall Liu27d24262020-09-18 19:04:59 -07001216 callback.onError(errorCode);
Shuo Qian4a594052020-01-23 11:59:30 -08001217 }
Shuo Qian4a594052020-01-23 11:59:30 -08001218 break;
Hall Liu27d24262020-09-18 19:04:59 -07001219 }
Shuo Qian4a594052020-01-23 11:59:30 -08001220
Hall Liu27d24262020-09-18 19:04:59 -07001221 case CMD_SET_CALL_FORWARDING: {
Shuo Qian4a594052020-01-23 11:59:30 -08001222 request = (MainThreadRequest) msg.obj;
1223 onCompleted = obtainMessage(EVENT_SET_CALL_FORWARDING_DONE, request);
Hall Liu27d24262020-09-18 19:04:59 -07001224 request = (MainThreadRequest) msg.obj;
Shuo Qian4a594052020-01-23 11:59:30 -08001225 CallForwardingInfo callForwardingInfoToSet =
Hall Liu27d24262020-09-18 19:04:59 -07001226 ((Pair<CallForwardingInfo, Consumer<Integer>>)
1227 request.argument).first;
1228 request.phone.setCallForwardingOption(
1229 callForwardingInfoToSet.isEnabled()
Calvin Pan258f1f72021-07-28 21:46:56 +08001230 ? CommandsInterface.CF_ACTION_REGISTRATION
Hall Liu27d24262020-09-18 19:04:59 -07001231 : CommandsInterface.CF_ACTION_DISABLE,
Shuo Qian4a594052020-01-23 11:59:30 -08001232 callForwardingInfoToSet.getReason(),
1233 callForwardingInfoToSet.getNumber(),
1234 callForwardingInfoToSet.getTimeoutSeconds(), onCompleted);
1235 break;
Hall Liu27d24262020-09-18 19:04:59 -07001236 }
Shuo Qian4a594052020-01-23 11:59:30 -08001237
Hall Liu27d24262020-09-18 19:04:59 -07001238 case EVENT_SET_CALL_FORWARDING_DONE: {
Shuo Qian4a594052020-01-23 11:59:30 -08001239 ar = (AsyncResult) msg.obj;
1240 request = (MainThreadRequest) ar.userObj;
Hall Liu27d24262020-09-18 19:04:59 -07001241 Consumer<Integer> callback =
1242 ((Pair<CallForwardingInfo, Consumer<Integer>>)
1243 request.argument).second;
1244 if (ar.exception != null) {
Shuo Qian4a594052020-01-23 11:59:30 -08001245 loge("setCallForwarding exception: " + ar.exception);
Hall Liu940c4ca2020-09-29 17:10:18 -07001246 int errorCode = TelephonyManager.CallForwardingInfoCallback
1247 .RESULT_ERROR_UNKNOWN;
Hall Liu27d24262020-09-18 19:04:59 -07001248 if (ar.exception instanceof CommandException) {
1249 CommandException.Error error =
1250 ((CommandException) (ar.exception)).getCommandError();
1251 if (error == CommandException.Error.FDN_CHECK_FAILURE) {
Hall Liu940c4ca2020-09-29 17:10:18 -07001252 errorCode = TelephonyManager.CallForwardingInfoCallback
1253 .RESULT_ERROR_FDN_CHECK_FAILURE;
Hall Liu27d24262020-09-18 19:04:59 -07001254 } else if (error == CommandException.Error.REQUEST_NOT_SUPPORTED) {
Hall Liu940c4ca2020-09-29 17:10:18 -07001255 errorCode = TelephonyManager.CallForwardingInfoCallback
1256 .RESULT_ERROR_NOT_SUPPORTED;
Hall Liu27d24262020-09-18 19:04:59 -07001257 }
1258 }
1259 callback.accept(errorCode);
1260 } else {
Hall Liu940c4ca2020-09-29 17:10:18 -07001261 callback.accept(TelephonyManager.CallForwardingInfoCallback.RESULT_SUCCESS);
Shuo Qian4a594052020-01-23 11:59:30 -08001262 }
Shuo Qian4a594052020-01-23 11:59:30 -08001263 break;
Hall Liu27d24262020-09-18 19:04:59 -07001264 }
Shuo Qian4a594052020-01-23 11:59:30 -08001265
Hall Liu27d24262020-09-18 19:04:59 -07001266 case CMD_GET_CALL_WAITING: {
Shuo Qian4a594052020-01-23 11:59:30 -08001267 request = (MainThreadRequest) msg.obj;
1268 onCompleted = obtainMessage(EVENT_GET_CALL_WAITING_DONE, request);
1269 getPhoneFromRequest(request).getCallWaiting(onCompleted);
1270 break;
Hall Liu27d24262020-09-18 19:04:59 -07001271 }
Shuo Qian4a594052020-01-23 11:59:30 -08001272
Hall Liu27d24262020-09-18 19:04:59 -07001273 case EVENT_GET_CALL_WAITING_DONE: {
Shuo Qian4a594052020-01-23 11:59:30 -08001274 ar = (AsyncResult) msg.obj;
1275 request = (MainThreadRequest) ar.userObj;
Hall Liu27d24262020-09-18 19:04:59 -07001276 Consumer<Integer> callback = (Consumer<Integer>) request.argument;
SongFerngWangebda2c52022-01-11 15:28:38 +08001277 int callWaitingStatus = TelephonyManager.CALL_WAITING_STATUS_UNKNOWN_ERROR;
Shuo Qian4a594052020-01-23 11:59:30 -08001278 if (ar.exception == null && ar.result != null) {
Shuo Qiand6a0dba2020-02-18 18:13:49 -08001279 int[] callForwardResults = (int[]) ar.result;
Shuo Qian4a594052020-01-23 11:59:30 -08001280 // Service Class is a bit mask per 3gpp 27.007.
1281 // Search for any service for voice call.
Shuo Qiand6a0dba2020-02-18 18:13:49 -08001282 if (callForwardResults.length > 1
1283 && ((callForwardResults[1]
Hall Liu27d24262020-09-18 19:04:59 -07001284 & CommandsInterface.SERVICE_CLASS_VOICE) > 0)) {
SongFerngWangebda2c52022-01-11 15:28:38 +08001285 callWaitingStatus = callForwardResults[0] == 0
Hall Liu27d24262020-09-18 19:04:59 -07001286 ? TelephonyManager.CALL_WAITING_STATUS_DISABLED
1287 : TelephonyManager.CALL_WAITING_STATUS_ENABLED;
Shuo Qian4a594052020-01-23 11:59:30 -08001288 } else {
SongFerngWangebda2c52022-01-11 15:28:38 +08001289 callWaitingStatus = TelephonyManager.CALL_WAITING_STATUS_DISABLED;
Shuo Qian4a594052020-01-23 11:59:30 -08001290 }
1291 } else {
1292 if (ar.result == null) {
1293 loge("EVENT_GET_CALL_WAITING_DONE: Empty response");
1294 }
1295 if (ar.exception != null) {
1296 loge("EVENT_GET_CALL_WAITING_DONE: Exception: " + ar.exception);
1297 }
1298 if (ar.exception instanceof CommandException) {
1299 CommandException.Error error =
1300 ((CommandException) (ar.exception)).getCommandError();
1301 if (error == CommandException.Error.REQUEST_NOT_SUPPORTED) {
SongFerngWangebda2c52022-01-11 15:28:38 +08001302 callWaitingStatus =
Shuo Qian4a594052020-01-23 11:59:30 -08001303 TelephonyManager.CALL_WAITING_STATUS_NOT_SUPPORTED;
SongFerngWangebda2c52022-01-11 15:28:38 +08001304 } else if (error == CommandException.Error.FDN_CHECK_FAILURE) {
1305 callWaitingStatus =
1306 TelephonyManager.CALL_WAITING_STATUS_FDN_CHECK_FAILURE;
Shuo Qian4a594052020-01-23 11:59:30 -08001307 }
1308 }
1309 }
SongFerngWangebda2c52022-01-11 15:28:38 +08001310 callback.accept(callWaitingStatus);
Shuo Qian4a594052020-01-23 11:59:30 -08001311 break;
Hall Liu27d24262020-09-18 19:04:59 -07001312 }
Shuo Qian4a594052020-01-23 11:59:30 -08001313
Hall Liu27d24262020-09-18 19:04:59 -07001314 case CMD_SET_CALL_WAITING: {
Shuo Qian4a594052020-01-23 11:59:30 -08001315 request = (MainThreadRequest) msg.obj;
1316 onCompleted = obtainMessage(EVENT_SET_CALL_WAITING_DONE, request);
Hall Liu27d24262020-09-18 19:04:59 -07001317 boolean enable = ((Pair<Boolean, Consumer<Integer>>) request.argument).first;
1318 getPhoneFromRequest(request).setCallWaiting(enable, onCompleted);
Shuo Qian4a594052020-01-23 11:59:30 -08001319 break;
Hall Liu27d24262020-09-18 19:04:59 -07001320 }
Shuo Qian4a594052020-01-23 11:59:30 -08001321
Hall Liu27d24262020-09-18 19:04:59 -07001322 case EVENT_SET_CALL_WAITING_DONE: {
Shuo Qian4a594052020-01-23 11:59:30 -08001323 ar = (AsyncResult) msg.obj;
1324 request = (MainThreadRequest) ar.userObj;
Hall Liu27d24262020-09-18 19:04:59 -07001325 boolean enable = ((Pair<Boolean, Consumer<Integer>>) request.argument).first;
1326 Consumer<Integer> callback =
1327 ((Pair<Boolean, Consumer<Integer>>) request.argument).second;
1328 if (ar.exception != null) {
Shuo Qian4a594052020-01-23 11:59:30 -08001329 loge("setCallWaiting exception: " + ar.exception);
Hall Liu27d24262020-09-18 19:04:59 -07001330 if (ar.exception instanceof CommandException) {
1331 CommandException.Error error =
1332 ((CommandException) (ar.exception)).getCommandError();
1333 if (error == CommandException.Error.REQUEST_NOT_SUPPORTED) {
1334 callback.accept(TelephonyManager.CALL_WAITING_STATUS_NOT_SUPPORTED);
SongFerngWangebda2c52022-01-11 15:28:38 +08001335 } else if (error == CommandException.Error.FDN_CHECK_FAILURE) {
1336 callback.accept(
1337 TelephonyManager.CALL_WAITING_STATUS_FDN_CHECK_FAILURE);
Hall Liu27d24262020-09-18 19:04:59 -07001338 } else {
1339 callback.accept(TelephonyManager.CALL_WAITING_STATUS_UNKNOWN_ERROR);
1340 }
1341 } else {
1342 callback.accept(TelephonyManager.CALL_WAITING_STATUS_UNKNOWN_ERROR);
1343 }
1344 } else {
1345 callback.accept(enable ? TelephonyManager.CALL_WAITING_STATUS_ENABLED
1346 : TelephonyManager.CALL_WAITING_STATUS_DISABLED);
Shuo Qian4a594052020-01-23 11:59:30 -08001347 }
Shuo Qian4a594052020-01-23 11:59:30 -08001348 break;
Hall Liu27d24262020-09-18 19:04:59 -07001349 }
Shishir Agrawal302c8692015-06-19 13:49:39 -07001350 case EVENT_PERFORM_NETWORK_SCAN_DONE:
1351 ar = (AsyncResult) msg.obj;
1352 request = (MainThreadRequest) ar.userObj;
1353 CellNetworkScanResult cellScanResult;
1354 if (ar.exception == null && ar.result != null) {
1355 cellScanResult = new CellNetworkScanResult(
1356 CellNetworkScanResult.STATUS_SUCCESS,
1357 (List<OperatorInfo>) ar.result);
1358 } else {
1359 if (ar.result == null) {
1360 loge("getCellNetworkScanResults: Empty response");
1361 }
1362 if (ar.exception != null) {
1363 loge("getCellNetworkScanResults: Exception: " + ar.exception);
1364 }
1365 int errorCode = CellNetworkScanResult.STATUS_UNKNOWN_ERROR;
1366 if (ar.exception instanceof CommandException) {
1367 CommandException.Error error =
1368 ((CommandException) (ar.exception)).getCommandError();
1369 if (error == CommandException.Error.RADIO_NOT_AVAILABLE) {
1370 errorCode = CellNetworkScanResult.STATUS_RADIO_NOT_AVAILABLE;
1371 } else if (error == CommandException.Error.GENERIC_FAILURE) {
1372 errorCode = CellNetworkScanResult.STATUS_RADIO_GENERIC_FAILURE;
1373 }
1374 }
1375 cellScanResult = new CellNetworkScanResult(errorCode, null);
1376 }
1377 request.result = cellScanResult;
Pengquan Menga1bb6272018-09-06 09:59:22 -07001378 notifyRequester(request);
Shishir Agrawal302c8692015-06-19 13:49:39 -07001379 break;
1380
1381 case CMD_SET_NETWORK_SELECTION_MODE_MANUAL:
1382 request = (MainThreadRequest) msg.obj;
Shishir Agrawal77ba3172015-09-10 14:50:19 -07001383 ManualNetworkSelectionArgument selArg =
1384 (ManualNetworkSelectionArgument) request.argument;
Shishir Agrawal302c8692015-06-19 13:49:39 -07001385 onCompleted = obtainMessage(EVENT_SET_NETWORK_SELECTION_MODE_MANUAL_DONE,
1386 request);
Shishir Agrawal77ba3172015-09-10 14:50:19 -07001387 getPhoneFromRequest(request).selectNetworkManually(selArg.operatorInfo,
1388 selArg.persistSelection, onCompleted);
Shishir Agrawal302c8692015-06-19 13:49:39 -07001389 break;
1390
1391 case EVENT_SET_NETWORK_SELECTION_MODE_MANUAL_DONE:
Pengquan Menge3d01e22018-09-20 15:25:35 -07001392 ar = (AsyncResult) msg.obj;
1393 request = (MainThreadRequest) ar.userObj;
1394 if (ar.exception == null) {
1395 request.result = true;
1396 } else {
1397 request.result = false;
1398 loge("setNetworkSelectionModeManual " + ar.exception);
1399 }
1400 notifyRequester(request);
1401 mApp.onNetworkSelectionChanged(request.subId);
Shishir Agrawal302c8692015-06-19 13:49:39 -07001402 break;
1403
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -07001404 case CMD_GET_MODEM_ACTIVITY_INFO:
1405 request = (MainThreadRequest) msg.obj;
1406 onCompleted = obtainMessage(EVENT_GET_MODEM_ACTIVITY_INFO_DONE, request);
James Mattisab947702019-04-03 14:18:34 -07001407 if (defaultPhone != null) {
1408 defaultPhone.getModemActivityInfo(onCompleted, request.workSource);
Shuo Qian8f4750a2020-02-20 17:12:10 -08001409 } else {
1410 ResultReceiver result = (ResultReceiver) request.argument;
1411 Bundle bundle = new Bundle();
1412 bundle.putParcelable(TelephonyManager.MODEM_ACTIVITY_RESULT_KEY,
Hall Liu49656c02020-10-09 19:00:11 -07001413 new ModemActivityInfo(0, 0, 0,
1414 new int[ModemActivityInfo.getNumTxPowerLevels()], 0));
Shuo Qian8f4750a2020-02-20 17:12:10 -08001415 result.send(0, bundle);
James Mattisab947702019-04-03 14:18:34 -07001416 }
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -07001417 break;
1418
Hall Liud0f208c2020-10-14 16:54:44 -07001419 case EVENT_GET_MODEM_ACTIVITY_INFO_DONE: {
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -07001420 ar = (AsyncResult) msg.obj;
1421 request = (MainThreadRequest) ar.userObj;
Shuo Qian8f4750a2020-02-20 17:12:10 -08001422 ResultReceiver result = (ResultReceiver) request.argument;
Hall Liud0f208c2020-10-14 16:54:44 -07001423 int error = 0;
Gary Jian3aa9a762022-01-24 16:41:19 +08001424 if (mLastModemActivityInfo == null) {
1425 mLastModemActivitySpecificInfo = new ActivityStatsTechSpecificInfo[1];
1426 mLastModemActivitySpecificInfo[0] =
1427 new ActivityStatsTechSpecificInfo(
1428 0,
1429 0,
1430 new int[ModemActivityInfo.getNumTxPowerLevels()],
1431 0);
1432 mLastModemActivityInfo =
1433 new ModemActivityInfo(0, 0, 0, mLastModemActivitySpecificInfo);
1434 }
1435
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -07001436 if (ar.exception == null && ar.result != null) {
Shuo Qian8f4750a2020-02-20 17:12:10 -08001437 // Update the last modem activity info and the result of the request.
1438 ModemActivityInfo info = (ModemActivityInfo) ar.result;
1439 if (isModemActivityInfoValid(info)) {
Gary Jian3aa9a762022-01-24 16:41:19 +08001440 mergeModemActivityInfo(info);
Shuo Qian8f4750a2020-02-20 17:12:10 -08001441 }
Gary Jian3aa9a762022-01-24 16:41:19 +08001442 mLastModemActivityInfo =
1443 new ModemActivityInfo(
1444 mLastModemActivityInfo.getTimestampMillis(),
1445 mLastModemActivityInfo.getSleepTimeMillis(),
1446 mLastModemActivityInfo.getIdleTimeMillis(),
1447 mLastModemActivitySpecificInfo);
1448
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -07001449 } else {
1450 if (ar.result == null) {
1451 loge("queryModemActivityInfo: Empty response");
Hall Liud0f208c2020-10-14 16:54:44 -07001452 error = TelephonyManager.ModemActivityInfoException
1453 .ERROR_INVALID_INFO_RECEIVED;
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -07001454 } else if (ar.exception instanceof CommandException) {
Gary Jian3aa9a762022-01-24 16:41:19 +08001455 loge("queryModemActivityInfo: CommandException: " + ar.exception);
Hall Liud0f208c2020-10-14 16:54:44 -07001456 error = TelephonyManager.ModemActivityInfoException
1457 .ERROR_MODEM_RESPONSE_ERROR;
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -07001458 } else {
1459 loge("queryModemActivityInfo: Unknown exception");
Hall Liud0f208c2020-10-14 16:54:44 -07001460 error = TelephonyManager.ModemActivityInfoException
1461 .ERROR_UNKNOWN;
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -07001462 }
1463 }
Shuo Qian8f4750a2020-02-20 17:12:10 -08001464 Bundle bundle = new Bundle();
Gary Jian3aa9a762022-01-24 16:41:19 +08001465 if (mLastModemActivityInfo != null) {
1466 bundle.putParcelable(
1467 TelephonyManager.MODEM_ACTIVITY_RESULT_KEY,
1468 mLastModemActivityInfo);
Hall Liud0f208c2020-10-14 16:54:44 -07001469 } else {
1470 bundle.putInt(TelephonyManager.EXCEPTION_RESULT_KEY, error);
1471 }
Shuo Qian8f4750a2020-02-20 17:12:10 -08001472 result.send(0, bundle);
Pengquan Menga1bb6272018-09-06 09:59:22 -07001473 notifyRequester(request);
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -07001474 break;
Hall Liud0f208c2020-10-14 16:54:44 -07001475 }
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -07001476
Meng Wang1a7c35a2016-05-05 20:56:15 -07001477 case CMD_SET_ALLOWED_CARRIERS:
1478 request = (MainThreadRequest) msg.obj;
Michele Berionne482f8202018-11-27 18:57:59 -08001479 CarrierRestrictionRules argument =
1480 (CarrierRestrictionRules) request.argument;
Meng Wang1a7c35a2016-05-05 20:56:15 -07001481 onCompleted = obtainMessage(EVENT_SET_ALLOWED_CARRIERS_DONE, request);
Michele Berionne482f8202018-11-27 18:57:59 -08001482 defaultPhone.setAllowedCarriers(argument, onCompleted, request.workSource);
Meng Wang1a7c35a2016-05-05 20:56:15 -07001483 break;
1484
1485 case EVENT_SET_ALLOWED_CARRIERS_DONE:
1486 ar = (AsyncResult) msg.obj;
1487 request = (MainThreadRequest) ar.userObj;
1488 if (ar.exception == null && ar.result != null) {
1489 request.result = ar.result;
1490 } else {
Michele Berionne482f8202018-11-27 18:57:59 -08001491 request.result = TelephonyManager.SET_CARRIER_RESTRICTION_ERROR;
1492 if (ar.exception instanceof CommandException) {
1493 loge("setAllowedCarriers: CommandException: " + ar.exception);
1494 CommandException.Error error =
1495 ((CommandException) (ar.exception)).getCommandError();
1496 if (error == CommandException.Error.REQUEST_NOT_SUPPORTED) {
1497 request.result =
1498 TelephonyManager.SET_CARRIER_RESTRICTION_NOT_SUPPORTED;
1499 }
Meng Wang1a7c35a2016-05-05 20:56:15 -07001500 } else {
1501 loge("setAllowedCarriers: Unknown exception");
1502 }
1503 }
Pengquan Menga1bb6272018-09-06 09:59:22 -07001504 notifyRequester(request);
Meng Wang1a7c35a2016-05-05 20:56:15 -07001505 break;
1506
1507 case CMD_GET_ALLOWED_CARRIERS:
1508 request = (MainThreadRequest) msg.obj;
1509 onCompleted = obtainMessage(EVENT_GET_ALLOWED_CARRIERS_DONE, request);
Nazanin Bakhshif782e562018-12-11 15:15:39 -08001510 defaultPhone.getAllowedCarriers(onCompleted, request.workSource);
Meng Wang1a7c35a2016-05-05 20:56:15 -07001511 break;
1512
1513 case EVENT_GET_ALLOWED_CARRIERS_DONE:
1514 ar = (AsyncResult) msg.obj;
1515 request = (MainThreadRequest) ar.userObj;
1516 if (ar.exception == null && ar.result != null) {
1517 request.result = ar.result;
1518 } else {
Michele Berionne482f8202018-11-27 18:57:59 -08001519 request.result = new IllegalStateException(
1520 "Failed to get carrier restrictions");
Meng Wang1a7c35a2016-05-05 20:56:15 -07001521 if (ar.result == null) {
1522 loge("getAllowedCarriers: Empty response");
1523 } else if (ar.exception instanceof CommandException) {
1524 loge("getAllowedCarriers: CommandException: " +
1525 ar.exception);
1526 } else {
1527 loge("getAllowedCarriers: Unknown exception");
1528 }
1529 }
Pengquan Menga1bb6272018-09-06 09:59:22 -07001530 notifyRequester(request);
Meng Wang1a7c35a2016-05-05 20:56:15 -07001531 break;
1532
Nathan Haroldb3014052017-01-25 15:57:32 -08001533 case EVENT_GET_FORBIDDEN_PLMNS_DONE:
1534 ar = (AsyncResult) msg.obj;
1535 request = (MainThreadRequest) ar.userObj;
1536 if (ar.exception == null && ar.result != null) {
1537 request.result = ar.result;
1538 } else {
1539 request.result = new IllegalArgumentException(
1540 "Failed to retrieve Forbidden Plmns");
1541 if (ar.result == null) {
1542 loge("getForbiddenPlmns: Empty response");
1543 } else {
1544 loge("getForbiddenPlmns: Unknown exception");
1545 }
1546 }
Pengquan Menga1bb6272018-09-06 09:59:22 -07001547 notifyRequester(request);
Nathan Haroldb3014052017-01-25 15:57:32 -08001548 break;
1549
1550 case CMD_GET_FORBIDDEN_PLMNS:
1551 request = (MainThreadRequest) msg.obj;
Muralidhar Reddy472c2ae2021-09-29 19:38:40 +00001552 uiccPort = getUiccPortFromRequest(request);
1553 if (uiccPort == null) {
1554 loge("getForbiddenPlmns() UiccPort is null");
Nathan Haroldb3014052017-01-25 15:57:32 -08001555 request.result = new IllegalArgumentException(
Muralidhar Reddy472c2ae2021-09-29 19:38:40 +00001556 "getForbiddenPlmns() UiccPort is null");
Pengquan Menga1bb6272018-09-06 09:59:22 -07001557 notifyRequester(request);
Nathan Haroldb3014052017-01-25 15:57:32 -08001558 break;
1559 }
1560 Integer appType = (Integer) request.argument;
Muralidhar Reddy472c2ae2021-09-29 19:38:40 +00001561 UiccCardApplication uiccApp = uiccPort.getApplicationByType(appType);
Nathan Haroldb3014052017-01-25 15:57:32 -08001562 if (uiccApp == null) {
1563 loge("getForbiddenPlmns() no app with specified type -- "
1564 + appType);
1565 request.result = new IllegalArgumentException("Failed to get UICC App");
Pengquan Menga1bb6272018-09-06 09:59:22 -07001566 notifyRequester(request);
Nathan Haroldb3014052017-01-25 15:57:32 -08001567 break;
1568 } else {
1569 if (DBG) logv("getForbiddenPlmns() found app " + uiccApp.getAid()
1570 + " specified type -- " + appType);
1571 }
1572 onCompleted = obtainMessage(EVENT_GET_FORBIDDEN_PLMNS_DONE, request);
1573 ((SIMRecords) uiccApp.getIccRecords()).getForbiddenPlmns(
1574 onCompleted);
1575 break;
1576
Holly Jiuyu Sun01c47ad2018-01-24 17:56:33 +00001577 case CMD_SWITCH_SLOTS:
1578 request = (MainThreadRequest) msg.obj;
Muralidhar Reddyeb809e32021-11-19 03:07:54 +00001579 List<UiccSlotMapping> slotMapping = (List<UiccSlotMapping>) request.argument;
Holly Jiuyu Sun01c47ad2018-01-24 17:56:33 +00001580 onCompleted = obtainMessage(EVENT_SWITCH_SLOTS_DONE, request);
Muralidhar Reddyeb809e32021-11-19 03:07:54 +00001581 UiccController.getInstance().switchSlots(slotMapping, onCompleted);
Holly Jiuyu Sun01c47ad2018-01-24 17:56:33 +00001582 break;
1583
1584 case EVENT_SWITCH_SLOTS_DONE:
1585 ar = (AsyncResult) msg.obj;
1586 request = (MainThreadRequest) ar.userObj;
1587 request.result = (ar.exception == null);
Pengquan Menga1bb6272018-09-06 09:59:22 -07001588 notifyRequester(request);
1589 break;
1590 case CMD_GET_NETWORK_SELECTION_MODE:
1591 request = (MainThreadRequest) msg.obj;
1592 onCompleted = obtainMessage(EVENT_GET_NETWORK_SELECTION_MODE_DONE, request);
1593 getPhoneFromRequest(request).getNetworkSelectionMode(onCompleted);
1594 break;
1595
1596 case EVENT_GET_NETWORK_SELECTION_MODE_DONE:
1597 ar = (AsyncResult) msg.obj;
1598 request = (MainThreadRequest) ar.userObj;
1599 if (ar.exception != null) {
1600 request.result = TelephonyManager.NETWORK_SELECTION_MODE_UNKNOWN;
1601 } else {
1602 int mode = ((int[]) ar.result)[0];
1603 if (mode == 0) {
1604 request.result = TelephonyManager.NETWORK_SELECTION_MODE_AUTO;
1605 } else {
1606 request.result = TelephonyManager.NETWORK_SELECTION_MODE_MANUAL;
1607 }
Holly Jiuyu Sun01c47ad2018-01-24 17:56:33 +00001608 }
Pengquan Menga1bb6272018-09-06 09:59:22 -07001609 notifyRequester(request);
1610 break;
1611 case CMD_GET_CDMA_ROAMING_MODE:
1612 request = (MainThreadRequest) msg.obj;
1613 onCompleted = obtainMessage(EVENT_GET_CDMA_ROAMING_MODE_DONE, request);
1614 getPhoneFromRequest(request).queryCdmaRoamingPreference(onCompleted);
1615 break;
1616 case EVENT_GET_CDMA_ROAMING_MODE_DONE:
1617 ar = (AsyncResult) msg.obj;
1618 request = (MainThreadRequest) ar.userObj;
1619 if (ar.exception != null) {
1620 request.result = TelephonyManager.CDMA_ROAMING_MODE_RADIO_DEFAULT;
1621 } else {
1622 request.result = ((int[]) ar.result)[0];
1623 }
1624 notifyRequester(request);
1625 break;
1626 case CMD_SET_CDMA_ROAMING_MODE:
1627 request = (MainThreadRequest) msg.obj;
1628 onCompleted = obtainMessage(EVENT_SET_CDMA_ROAMING_MODE_DONE, request);
1629 int mode = (int) request.argument;
1630 getPhoneFromRequest(request).setCdmaRoamingPreference(mode, onCompleted);
1631 break;
1632 case EVENT_SET_CDMA_ROAMING_MODE_DONE:
1633 ar = (AsyncResult) msg.obj;
1634 request = (MainThreadRequest) ar.userObj;
1635 request.result = ar.exception == null;
1636 notifyRequester(request);
1637 break;
Sarah Chinbaab1432020-10-28 13:46:24 -07001638 case CMD_GET_CDMA_SUBSCRIPTION_MODE:
1639 request = (MainThreadRequest) msg.obj;
1640 onCompleted = obtainMessage(EVENT_GET_CDMA_SUBSCRIPTION_MODE_DONE, request);
1641 getPhoneFromRequest(request).queryCdmaSubscriptionMode(onCompleted);
1642 break;
1643 case EVENT_GET_CDMA_SUBSCRIPTION_MODE_DONE:
1644 ar = (AsyncResult) msg.obj;
1645 request = (MainThreadRequest) ar.userObj;
1646 if (ar.exception != null) {
1647 request.result = TelephonyManager.CDMA_SUBSCRIPTION_RUIM_SIM;
1648 } else {
1649 request.result = ((int[]) ar.result)[0];
1650 }
1651 notifyRequester(request);
1652 break;
Pengquan Menga1bb6272018-09-06 09:59:22 -07001653 case CMD_SET_CDMA_SUBSCRIPTION_MODE:
1654 request = (MainThreadRequest) msg.obj;
1655 onCompleted = obtainMessage(EVENT_SET_CDMA_SUBSCRIPTION_MODE_DONE, request);
1656 int subscriptionMode = (int) request.argument;
Sarah Chinbaab1432020-10-28 13:46:24 -07001657 getPhoneFromRequest(request).setCdmaSubscriptionMode(
1658 subscriptionMode, onCompleted);
Pengquan Menga1bb6272018-09-06 09:59:22 -07001659 break;
1660 case EVENT_SET_CDMA_SUBSCRIPTION_MODE_DONE:
1661 ar = (AsyncResult) msg.obj;
1662 request = (MainThreadRequest) ar.userObj;
1663 request.result = ar.exception == null;
1664 notifyRequester(request);
Holly Jiuyu Sun01c47ad2018-01-24 17:56:33 +00001665 break;
Nathan Harold3ff88932018-08-14 10:19:49 -07001666 case CMD_GET_ALL_CELL_INFO:
1667 request = (MainThreadRequest) msg.obj;
Nathan Harold3ff88932018-08-14 10:19:49 -07001668 onCompleted = obtainMessage(EVENT_GET_ALL_CELL_INFO_DONE, request);
Nathan Harold92bed182018-10-12 18:16:49 -07001669 request.phone.requestCellInfoUpdate(request.workSource, onCompleted);
Nathan Harold3ff88932018-08-14 10:19:49 -07001670 break;
Nathan Harold3ff88932018-08-14 10:19:49 -07001671 case EVENT_GET_ALL_CELL_INFO_DONE:
1672 ar = (AsyncResult) msg.obj;
1673 request = (MainThreadRequest) ar.userObj;
Nathan Harold8d0f1742018-10-02 12:14:47 -07001674 // If a timeout occurs, the response will be null
1675 request.result = (ar.exception == null && ar.result != null)
1676 ? ar.result : new ArrayList<CellInfo>();
Nathan Harold3ff88932018-08-14 10:19:49 -07001677 synchronized (request) {
1678 request.notifyAll();
1679 }
1680 break;
Nathan Haroldfa8da0f2018-09-27 18:51:29 -07001681 case CMD_REQUEST_CELL_INFO_UPDATE:
1682 request = (MainThreadRequest) msg.obj;
1683 request.phone.requestCellInfoUpdate(request.workSource,
1684 obtainMessage(EVENT_REQUEST_CELL_INFO_UPDATE_DONE, request));
1685 break;
1686 case EVENT_REQUEST_CELL_INFO_UPDATE_DONE:
1687 ar = (AsyncResult) msg.obj;
1688 request = (MainThreadRequest) ar.userObj;
1689 ICellInfoCallback cb = (ICellInfoCallback) request.argument;
1690 try {
1691 if (ar.exception != null) {
Nathan Haroldfa8da0f2018-09-27 18:51:29 -07001692 Log.e(LOG_TAG, "Exception retrieving CellInfo=" + ar.exception);
Meng Wangd8921f42019-09-30 17:13:54 -07001693 cb.onError(
1694 TelephonyManager.CellInfoCallback.ERROR_MODEM_ERROR,
1695 ar.exception.getClass().getName(),
1696 ar.exception.toString());
Nathan Haroldfa8da0f2018-09-27 18:51:29 -07001697 } else if (ar.result == null) {
Nathan Haroldfa8da0f2018-09-27 18:51:29 -07001698 Log.w(LOG_TAG, "Timeout Waiting for CellInfo!");
Meng Wangd8921f42019-09-30 17:13:54 -07001699 cb.onError(TelephonyManager.CellInfoCallback.ERROR_TIMEOUT, null, null);
Nathan Haroldfa8da0f2018-09-27 18:51:29 -07001700 } else {
1701 // use the result as returned
1702 cb.onCellInfo((List<CellInfo>) ar.result);
1703 }
1704 } catch (RemoteException re) {
1705 Log.w(LOG_TAG, "Discarded CellInfo due to Callback RemoteException");
1706 }
1707 break;
Sarah Chin679c08a2020-11-18 13:39:35 -08001708 case CMD_GET_CELL_LOCATION: {
Nathan Harold3ff88932018-08-14 10:19:49 -07001709 request = (MainThreadRequest) msg.obj;
1710 WorkSource ws = (WorkSource) request.argument;
1711 Phone phone = getPhoneFromRequest(request);
Meng Wanga10e89e2019-12-09 13:13:01 -08001712 phone.getCellIdentity(ws, obtainMessage(EVENT_GET_CELL_LOCATION_DONE, request));
Nathan Harold3ff88932018-08-14 10:19:49 -07001713 break;
Sarah Chin679c08a2020-11-18 13:39:35 -08001714 }
1715 case EVENT_GET_CELL_LOCATION_DONE: {
Nathan Harold3ff88932018-08-14 10:19:49 -07001716 ar = (AsyncResult) msg.obj;
1717 request = (MainThreadRequest) ar.userObj;
1718 if (ar.exception == null) {
1719 request.result = ar.result;
1720 } else {
Sarah Chin679c08a2020-11-18 13:39:35 -08001721 Phone phone = getPhoneFromRequest(request);
Nathan Harold3ff88932018-08-14 10:19:49 -07001722 request.result = (phone.getPhoneType() == PhoneConstants.PHONE_TYPE_CDMA)
Meng Wanga10e89e2019-12-09 13:13:01 -08001723 ? new CellIdentityCdma() : new CellIdentityGsm();
Nathan Harold3ff88932018-08-14 10:19:49 -07001724 }
1725
1726 synchronized (request) {
1727 request.notifyAll();
1728 }
1729 break;
Sarah Chin679c08a2020-11-18 13:39:35 -08001730 }
chen xu6dac5ab2018-10-26 17:39:23 -07001731 case CMD_MODEM_REBOOT:
1732 request = (MainThreadRequest) msg.obj;
1733 onCompleted = obtainMessage(EVENT_RESET_MODEM_CONFIG_DONE, request);
Nazanin Bakhshif782e562018-12-11 15:15:39 -08001734 defaultPhone.rebootModem(onCompleted);
chen xu6dac5ab2018-10-26 17:39:23 -07001735 break;
chen xu6dac5ab2018-10-26 17:39:23 -07001736 case EVENT_CMD_MODEM_REBOOT_DONE:
1737 handleNullReturnEvent(msg, "rebootModem");
1738 break;
Malcolm Chen8e4ed912019-01-15 20:22:16 -08001739 case CMD_REQUEST_ENABLE_MODEM:
1740 request = (MainThreadRequest) msg.obj;
1741 boolean enable = (boolean) request.argument;
1742 onCompleted = obtainMessage(EVENT_ENABLE_MODEM_DONE, request);
Nazanin Bakhshi33d584b2019-02-27 10:44:32 -08001743 onCompleted.arg1 = enable ? 1 : 0;
Malcolm Chen8e4ed912019-01-15 20:22:16 -08001744 PhoneConfigurationManager.getInstance()
1745 .enablePhone(request.phone, enable, onCompleted);
1746 break;
Michele Berionne5e411512020-11-13 02:36:59 +00001747 case EVENT_ENABLE_MODEM_DONE: {
Malcolm Chen8e4ed912019-01-15 20:22:16 -08001748 ar = (AsyncResult) msg.obj;
1749 request = (MainThreadRequest) ar.userObj;
1750 request.result = (ar.exception == null);
Nazanin Bakhshif71371d2019-04-29 17:29:44 -07001751 int phoneId = request.phone.getPhoneId();
Nazanin Bakhshi33d584b2019-02-27 10:44:32 -08001752 //update the cache as modem status has changed
Nazanin Bakhshif71371d2019-04-29 17:29:44 -07001753 if ((boolean) request.result) {
1754 mPhoneConfigurationManager.addToPhoneStatusCache(phoneId, msg.arg1 == 1);
1755 updateModemStateMetrics();
1756 } else {
1757 Log.e(LOG_TAG, msg.what + " failure. Not updating modem status."
1758 + ar.exception);
1759 }
1760 notifyRequester(request);
1761 break;
Michele Berionne5e411512020-11-13 02:36:59 +00001762 }
Nazanin Bakhshif71371d2019-04-29 17:29:44 -07001763 case CMD_GET_MODEM_STATUS:
1764 request = (MainThreadRequest) msg.obj;
1765 onCompleted = obtainMessage(EVENT_GET_MODEM_STATUS_DONE, request);
1766 PhoneConfigurationManager.getInstance()
1767 .getPhoneStatusFromModem(request.phone, onCompleted);
1768 break;
1769 case EVENT_GET_MODEM_STATUS_DONE:
1770 ar = (AsyncResult) msg.obj;
1771 request = (MainThreadRequest) ar.userObj;
1772 int id = request.phone.getPhoneId();
1773 if (ar.exception == null && ar.result != null) {
1774 request.result = ar.result;
1775 //update the cache as modem status has changed
1776 mPhoneConfigurationManager.addToPhoneStatusCache(id,
1777 (boolean) request.result);
1778 } else {
1779 // Return true if modem status cannot be retrieved. For most cases,
1780 // modem status is on. And for older version modems, GET_MODEM_STATUS
1781 // and disable modem are not supported. Modem is always on.
1782 // TODO: this should be fixed in R to support a third
1783 // status UNKNOWN b/131631629
1784 request.result = true;
1785 Log.e(LOG_TAG, msg.what + " failure. Not updating modem status."
1786 + ar.exception);
1787 }
Malcolm Chen8e4ed912019-01-15 20:22:16 -08001788 notifyRequester(request);
1789 break;
Hall Liu73f5d362020-01-20 13:42:00 -08001790 case CMD_SET_SYSTEM_SELECTION_CHANNELS: {
1791 request = (MainThreadRequest) msg.obj;
1792 onCompleted = obtainMessage(EVENT_SET_SYSTEM_SELECTION_CHANNELS_DONE, request);
1793 Pair<List<RadioAccessSpecifier>, Consumer<Boolean>> args =
1794 (Pair<List<RadioAccessSpecifier>, Consumer<Boolean>>) request.argument;
1795 request.phone.setSystemSelectionChannels(args.first, onCompleted);
1796 break;
1797 }
1798 case EVENT_SET_SYSTEM_SELECTION_CHANNELS_DONE: {
1799 ar = (AsyncResult) msg.obj;
1800 request = (MainThreadRequest) ar.userObj;
1801 Pair<List<RadioAccessSpecifier>, Consumer<Boolean>> args =
1802 (Pair<List<RadioAccessSpecifier>, Consumer<Boolean>>) request.argument;
1803 args.second.accept(ar.exception == null);
1804 notifyRequester(request);
1805 break;
1806 }
Sarah Chin679c08a2020-11-18 13:39:35 -08001807 case CMD_GET_SYSTEM_SELECTION_CHANNELS: {
1808 request = (MainThreadRequest) msg.obj;
1809 onCompleted = obtainMessage(EVENT_GET_SYSTEM_SELECTION_CHANNELS_DONE, request);
1810 Phone phone = getPhoneFromRequest(request);
1811 if (phone != null) {
1812 phone.getSystemSelectionChannels(onCompleted);
1813 } else {
1814 loge("getSystemSelectionChannels: No phone object");
1815 request.result = new ArrayList<RadioAccessSpecifier>();
1816 notifyRequester(request);
1817 }
1818 break;
1819 }
1820 case EVENT_GET_SYSTEM_SELECTION_CHANNELS_DONE:
1821 ar = (AsyncResult) msg.obj;
1822 request = (MainThreadRequest) ar.userObj;
1823 if (ar.exception == null && ar.result != null) {
1824 request.result = ar.result;
1825 } else {
Sarah Chin428d1d62021-03-13 03:17:40 -08001826 request.result = new IllegalStateException(
1827 "Failed to retrieve system selecton channels");
Sarah Chin679c08a2020-11-18 13:39:35 -08001828 if (ar.result == null) {
1829 loge("getSystemSelectionChannels: Empty response");
1830 } else {
1831 loge("getSystemSelectionChannels: Unknown exception");
1832 }
1833 }
1834 notifyRequester(request);
1835 break;
yincheng zhao2737e882019-09-06 17:06:54 -07001836 case EVENT_SET_FORBIDDEN_PLMNS_DONE:
1837 ar = (AsyncResult) msg.obj;
1838 request = (MainThreadRequest) ar.userObj;
1839 if (ar.exception == null && ar.result != null) {
1840 request.result = ar.result;
1841 } else {
1842 request.result = -1;
1843 loge("Failed to set Forbidden Plmns");
1844 if (ar.result == null) {
1845 loge("setForbidenPlmns: Empty response");
1846 } else if (ar.exception != null) {
1847 loge("setForbiddenPlmns: Exception: " + ar.exception);
1848 request.result = -1;
1849 } else {
1850 loge("setForbiddenPlmns: Unknown exception");
1851 }
1852 }
1853 notifyRequester(request);
1854 break;
1855 case CMD_SET_FORBIDDEN_PLMNS:
1856 request = (MainThreadRequest) msg.obj;
Muralidhar Reddy472c2ae2021-09-29 19:38:40 +00001857 uiccPort = getUiccPortFromRequest(request);
1858 if (uiccPort == null) {
1859 loge("setForbiddenPlmns: UiccPort is null");
yincheng zhao2737e882019-09-06 17:06:54 -07001860 request.result = -1;
1861 notifyRequester(request);
1862 break;
1863 }
1864 Pair<Integer, List<String>> setFplmnsArgs =
1865 (Pair<Integer, List<String>>) request.argument;
1866 appType = setFplmnsArgs.first;
1867 List<String> fplmns = setFplmnsArgs.second;
Muralidhar Reddy472c2ae2021-09-29 19:38:40 +00001868 uiccApp = uiccPort.getApplicationByType(appType);
yincheng zhao2737e882019-09-06 17:06:54 -07001869 if (uiccApp == null) {
1870 loge("setForbiddenPlmns: no app with specified type -- " + appType);
1871 request.result = -1;
1872 loge("Failed to get UICC App");
1873 notifyRequester(request);
1874 } else {
1875 onCompleted = obtainMessage(EVENT_SET_FORBIDDEN_PLMNS_DONE, request);
1876 ((SIMRecords) uiccApp.getIccRecords())
1877 .setForbiddenPlmns(onCompleted, fplmns);
1878 }
yinchengzhao4d163c02019-12-12 15:21:47 -08001879 break;
Naina Nallurid63128d2019-09-17 14:10:30 -07001880 case CMD_ERASE_MODEM_CONFIG:
1881 request = (MainThreadRequest) msg.obj;
1882 onCompleted = obtainMessage(EVENT_ERASE_MODEM_CONFIG_DONE, request);
1883 defaultPhone.eraseModemConfig(onCompleted);
1884 break;
1885 case EVENT_ERASE_MODEM_CONFIG_DONE:
1886 handleNullReturnEvent(msg, "eraseModemConfig");
yincheng zhao2737e882019-09-06 17:06:54 -07001887 break;
zoey chene02881a2019-12-30 16:11:23 +08001888
Kai Shif70f46f2021-03-03 13:59:46 -08001889 case CMD_ERASE_DATA_SHARED_PREFERENCES:
1890 request = (MainThreadRequest) msg.obj;
1891 request.result = defaultPhone.eraseDataInSharedPreferences();
1892 notifyRequester(request);
1893 break;
1894
zoey chene02881a2019-12-30 16:11:23 +08001895 case CMD_CHANGE_ICC_LOCK_PASSWORD:
1896 request = (MainThreadRequest) msg.obj;
1897 onCompleted = obtainMessage(EVENT_CHANGE_ICC_LOCK_PASSWORD_DONE, request);
1898 Pair<String, String> changed = (Pair<String, String>) request.argument;
1899 getPhoneFromRequest(request).getIccCard().changeIccLockPassword(
1900 changed.first, changed.second, onCompleted);
1901 break;
1902 case EVENT_CHANGE_ICC_LOCK_PASSWORD_DONE:
1903 ar = (AsyncResult) msg.obj;
1904 request = (MainThreadRequest) ar.userObj;
1905 if (ar.exception == null) {
1906 request.result = TelephonyManager.CHANGE_ICC_LOCK_SUCCESS;
Michele Berionne5e411512020-11-13 02:36:59 +00001907 // If the operation is successful, update the PIN storage
1908 Pair<String, String> passwords = (Pair<String, String>) request.argument;
1909 int phoneId = getPhoneFromRequest(request).getPhoneId();
Jon Spivack9c3bc762021-10-06 20:53:09 +00001910 UiccController.getInstance().getPinStorage()
1911 .storePin(passwords.second, phoneId);
zoey chene02881a2019-12-30 16:11:23 +08001912 } else {
1913 request.result = msg.arg1;
1914 }
1915 notifyRequester(request);
1916 break;
1917
Michele Berionne5e411512020-11-13 02:36:59 +00001918 case CMD_SET_ICC_LOCK_ENABLED: {
zoey chene02881a2019-12-30 16:11:23 +08001919 request = (MainThreadRequest) msg.obj;
1920 onCompleted = obtainMessage(EVENT_SET_ICC_LOCK_ENABLED_DONE, request);
1921 Pair<Boolean, String> enabled = (Pair<Boolean, String>) request.argument;
1922 getPhoneFromRequest(request).getIccCard().setIccLockEnabled(
1923 enabled.first, enabled.second, onCompleted);
1924 break;
Michele Berionne5e411512020-11-13 02:36:59 +00001925 }
zoey chene02881a2019-12-30 16:11:23 +08001926 case EVENT_SET_ICC_LOCK_ENABLED_DONE:
1927 ar = (AsyncResult) msg.obj;
1928 request = (MainThreadRequest) ar.userObj;
1929 if (ar.exception == null) {
1930 request.result = TelephonyManager.CHANGE_ICC_LOCK_SUCCESS;
Michele Berionne5e411512020-11-13 02:36:59 +00001931 // If the operation is successful, update the PIN storage
1932 Pair<Boolean, String> enabled = (Pair<Boolean, String>) request.argument;
1933 int phoneId = getPhoneFromRequest(request).getPhoneId();
1934 if (enabled.first) {
Jon Spivack9c3bc762021-10-06 20:53:09 +00001935 UiccController.getInstance().getPinStorage()
1936 .storePin(enabled.second, phoneId);
Michele Berionne5e411512020-11-13 02:36:59 +00001937 } else {
1938 UiccController.getInstance().getPinStorage().clearPin(phoneId);
1939 }
zoey chene02881a2019-12-30 16:11:23 +08001940 } else {
1941 request.result = msg.arg1;
1942 }
Michele Berionne5e411512020-11-13 02:36:59 +00001943
1944
zoey chene02881a2019-12-30 16:11:23 +08001945 notifyRequester(request);
1946 break;
1947
Peter Wangdafb9ac2020-01-15 14:13:38 -08001948 case MSG_NOTIFY_USER_ACTIVITY:
1949 removeMessages(MSG_NOTIFY_USER_ACTIVITY);
Peter Wang59571be2020-01-27 12:35:15 +08001950 Intent intent = new Intent(TelephonyIntents.ACTION_USER_ACTIVITY_NOTIFICATION);
Peter Wangdafb9ac2020-01-15 14:13:38 -08001951 intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY);
1952 getDefaultPhone().getContext().sendBroadcastAsUser(
1953 intent, UserHandle.ALL, permission.USER_ACTIVITY);
1954 break;
Jack Nudelmanb0b87642020-11-12 15:04:39 -08001955
1956 case CMD_SET_DATA_THROTTLING: {
1957 request = (MainThreadRequest) msg.obj;
1958 onCompleted = obtainMessage(EVENT_SET_DATA_THROTTLING_DONE, request);
1959 DataThrottlingRequest dataThrottlingRequest =
1960 (DataThrottlingRequest) request.argument;
1961 Phone phone = getPhoneFromRequest(request);
1962 if (phone != null) {
1963 phone.setDataThrottling(onCompleted,
1964 request.workSource, dataThrottlingRequest.getDataThrottlingAction(),
1965 dataThrottlingRequest.getCompletionDurationMillis());
1966 } else {
1967 loge("setDataThrottling: No phone object");
1968 request.result =
1969 TelephonyManager.THERMAL_MITIGATION_RESULT_MODEM_NOT_AVAILABLE;
1970 notifyRequester(request);
1971 }
1972
1973 break;
1974 }
1975 case EVENT_SET_DATA_THROTTLING_DONE:
1976 ar = (AsyncResult) msg.obj;
1977 request = (MainThreadRequest) ar.userObj;
1978
1979 if (ar.exception == null) {
1980 request.result = TelephonyManager.THERMAL_MITIGATION_RESULT_SUCCESS;
1981 } else if (ar.exception instanceof CommandException) {
1982 loge("setDataThrottling: CommandException: " + ar.exception);
1983 CommandException.Error error =
1984 ((CommandException) (ar.exception)).getCommandError();
1985
1986 if (error == CommandException.Error.RADIO_NOT_AVAILABLE) {
1987 request.result = TelephonyManager
1988 .THERMAL_MITIGATION_RESULT_MODEM_NOT_AVAILABLE;
1989 } else if (error == CommandException.Error.INVALID_ARGUMENTS) {
1990 request.result = SET_DATA_THROTTLING_MODEM_THREW_INVALID_PARAMS;
Jack Nudelman5d6a98b2021-03-04 14:26:25 -08001991 } else if (error == CommandException.Error.REQUEST_NOT_SUPPORTED) {
1992 request.result = MODEM_DOES_NOT_SUPPORT_DATA_THROTTLING_ERROR_CODE;
Jack Nudelmanb0b87642020-11-12 15:04:39 -08001993 } else {
1994 request.result =
1995 TelephonyManager.THERMAL_MITIGATION_RESULT_MODEM_ERROR;
1996 }
1997 } else {
1998 request.result = TelephonyManager.THERMAL_MITIGATION_RESULT_MODEM_ERROR;
1999 }
2000 Log.w(LOG_TAG, "DataThrottlingResult = " + request.result);
2001 notifyRequester(request);
2002 break;
Jordan Liu109698e2020-11-24 14:50:34 -08002003
2004 case CMD_SET_SIM_POWER: {
2005 request = (MainThreadRequest) msg.obj;
2006 onCompleted = obtainMessage(EVENT_SET_SIM_POWER_DONE, request);
2007 request = (MainThreadRequest) msg.obj;
2008 int stateToSet =
2009 ((Pair<Integer, IIntegerConsumer>)
2010 request.argument).first;
2011 request.phone.setSimPowerState(stateToSet, onCompleted, request.workSource);
2012 break;
2013 }
2014 case EVENT_SET_SIM_POWER_DONE: {
2015 ar = (AsyncResult) msg.obj;
2016 request = (MainThreadRequest) ar.userObj;
2017 IIntegerConsumer callback =
2018 ((Pair<Integer, IIntegerConsumer>) request.argument).second;
2019 if (ar.exception != null) {
2020 loge("setSimPower exception: " + ar.exception);
2021 int errorCode = TelephonyManager.CallForwardingInfoCallback
2022 .RESULT_ERROR_UNKNOWN;
2023 if (ar.exception instanceof CommandException) {
2024 CommandException.Error error =
2025 ((CommandException) (ar.exception)).getCommandError();
2026 if (error == CommandException.Error.SIM_ERR) {
2027 errorCode = TelephonyManager.SET_SIM_POWER_STATE_SIM_ERROR;
2028 } else if (error == CommandException.Error.INVALID_ARGUMENTS) {
2029 errorCode = TelephonyManager.SET_SIM_POWER_STATE_ALREADY_IN_STATE;
2030 } else if (error == CommandException.Error.REQUEST_NOT_SUPPORTED) {
2031 errorCode = TelephonyManager.SET_SIM_POWER_STATE_NOT_SUPPORTED;
2032 } else {
2033 errorCode = TelephonyManager.SET_SIM_POWER_STATE_MODEM_ERROR;
2034 }
2035 }
2036 try {
2037 callback.accept(errorCode);
2038 } catch (RemoteException e) {
2039 // Ignore if the remote process is no longer available to call back.
2040 Log.w(LOG_TAG, "setSimPower: callback not available.");
2041 }
2042 } else {
2043 try {
2044 callback.accept(TelephonyManager.SET_SIM_POWER_STATE_SUCCESS);
2045 } catch (RemoteException e) {
2046 // Ignore if the remote process is no longer available to call back.
2047 Log.w(LOG_TAG, "setSimPower: callback not available.");
2048 }
2049 }
2050 break;
2051 }
Rambo Wanga5cc9b72021-01-07 10:51:54 -08002052 case CMD_SET_SIGNAL_STRENGTH_UPDATE_REQUEST: {
2053 request = (MainThreadRequest) msg.obj;
2054
2055 final Phone phone = getPhoneFromRequest(request);
2056 if (phone == null || phone.getServiceStateTracker() == null) {
2057 request.result = new IllegalStateException("Phone or SST is null");
2058 notifyRequester(request);
2059 break;
2060 }
2061
2062 Pair<Integer, SignalStrengthUpdateRequest> pair =
2063 (Pair<Integer, SignalStrengthUpdateRequest>) request.argument;
2064 onCompleted = obtainMessage(EVENT_SET_SIGNAL_STRENGTH_UPDATE_REQUEST_DONE,
2065 request);
Rambo Wang6568f172021-02-03 16:56:47 -08002066 phone.getSignalStrengthController().setSignalStrengthUpdateRequest(
Rambo Wanga5cc9b72021-01-07 10:51:54 -08002067 request.subId, pair.first /*callingUid*/,
2068 pair.second /*request*/, onCompleted);
2069 break;
2070 }
2071 case EVENT_SET_SIGNAL_STRENGTH_UPDATE_REQUEST_DONE: {
2072 ar = (AsyncResult) msg.obj;
2073 request = (MainThreadRequest) ar.userObj;
2074 // request.result will be the exception of ar if present, true otherwise.
2075 // Be cautious not to leave result null which will wait() forever
2076 request.result = ar.exception != null ? ar.exception : true;
2077 notifyRequester(request);
2078 break;
2079 }
2080 case CMD_CLEAR_SIGNAL_STRENGTH_UPDATE_REQUEST: {
2081 request = (MainThreadRequest) msg.obj;
2082
2083 Phone phone = getPhoneFromRequest(request);
2084 if (phone == null || phone.getServiceStateTracker() == null) {
2085 request.result = new IllegalStateException("Phone or SST is null");
2086 notifyRequester(request);
2087 break;
2088 }
2089
2090 Pair<Integer, SignalStrengthUpdateRequest> pair =
2091 (Pair<Integer, SignalStrengthUpdateRequest>) request.argument;
2092 onCompleted = obtainMessage(EVENT_CLEAR_SIGNAL_STRENGTH_UPDATE_REQUEST_DONE,
2093 request);
Rambo Wang6568f172021-02-03 16:56:47 -08002094 phone.getSignalStrengthController().clearSignalStrengthUpdateRequest(
Rambo Wanga5cc9b72021-01-07 10:51:54 -08002095 request.subId, pair.first /*callingUid*/,
2096 pair.second /*request*/, onCompleted);
2097 break;
2098 }
2099 case EVENT_CLEAR_SIGNAL_STRENGTH_UPDATE_REQUEST_DONE: {
2100 ar = (AsyncResult) msg.obj;
2101 request = (MainThreadRequest) ar.userObj;
2102 request.result = ar.exception != null ? ar.exception : true;
2103 notifyRequester(request);
2104 break;
2105 }
Jordan Liu109698e2020-11-24 14:50:34 -08002106
Hongbo Zeng156aa4a2021-02-08 21:50:28 +08002107 case CMD_GET_SLICING_CONFIG: {
2108 request = (MainThreadRequest) msg.obj;
2109 onCompleted = obtainMessage(EVENT_GET_SLICING_CONFIG_DONE, request);
2110 request.phone.getSlicingConfig(onCompleted);
2111 break;
2112 }
2113 case EVENT_GET_SLICING_CONFIG_DONE: {
2114 ar = (AsyncResult) msg.obj;
2115 request = (MainThreadRequest) ar.userObj;
2116 ResultReceiver result = (ResultReceiver) request.argument;
2117
Hongbo Zeng0e18b162021-04-07 16:52:18 +08002118 NetworkSlicingConfig slicingConfig = null;
Hongbo Zeng156aa4a2021-02-08 21:50:28 +08002119 Bundle bundle = new Bundle();
2120 int resultCode = 0;
2121 if (ar.exception != null) {
2122 Log.e(LOG_TAG, "Exception retrieving slicing configuration="
2123 + ar.exception);
Hongbo Zeng0e18b162021-04-07 16:52:18 +08002124 resultCode = TelephonyManager.NetworkSlicingException.ERROR_MODEM_ERROR;
Hongbo Zeng156aa4a2021-02-08 21:50:28 +08002125 } else if (ar.result == null) {
2126 Log.w(LOG_TAG, "Timeout Waiting for slicing configuration!");
Hongbo Zeng0e18b162021-04-07 16:52:18 +08002127 resultCode = TelephonyManager.NetworkSlicingException.ERROR_TIMEOUT;
Hongbo Zeng156aa4a2021-02-08 21:50:28 +08002128 } else {
2129 // use the result as returned
Hongbo Zeng0e18b162021-04-07 16:52:18 +08002130 resultCode = TelephonyManager.NetworkSlicingException.SUCCESS;
2131 slicingConfig = (NetworkSlicingConfig) ar.result;
Hongbo Zeng156aa4a2021-02-08 21:50:28 +08002132 }
2133
2134 if (slicingConfig == null) {
Hongbo Zeng0e18b162021-04-07 16:52:18 +08002135 slicingConfig = new NetworkSlicingConfig();
Hongbo Zeng156aa4a2021-02-08 21:50:28 +08002136 }
2137 bundle.putParcelable(TelephonyManager.KEY_SLICING_CONFIG_HANDLE, slicingConfig);
2138 result.send(resultCode, bundle);
2139 notifyRequester(request);
2140 break;
2141 }
2142
Sarah Chin2ec39f62022-08-31 17:03:26 -07002143 case CMD_PURCHASE_PREMIUM_CAPABILITY:
2144 request = (MainThreadRequest) msg.obj;
2145 onCompleted = obtainMessage(EVENT_PURCHASE_PREMIUM_CAPABILITY_DONE, request);
2146 SliceStore.getInstance(request.phone).purchasePremiumCapability(
2147 ((Pair<Integer, IIntegerConsumer>) request.argument).first,
2148 onCompleted);
2149 break;
2150
2151 case EVENT_PURCHASE_PREMIUM_CAPABILITY_DONE:
2152 ar = (AsyncResult) msg.obj;
2153 request = (MainThreadRequest) ar.userObj;
2154 Pair<Integer, IIntegerConsumer> pair =
2155 (Pair<Integer, IIntegerConsumer>) request.argument;
2156 try {
2157 int result = (int) ar.result;
2158 pair.second.accept(result);
2159 log("purchasePremiumCapability: capability="
2160 + TelephonyManager.convertPremiumCapabilityToString(pair.first)
2161 + ", result= "
2162 + TelephonyManager.convertPurchaseResultToString(result));
2163 } catch (RemoteException e) {
2164 String logStr = "Purchase premium capability "
2165 + TelephonyManager.convertPremiumCapabilityToString(pair.first)
2166 + " failed: " + e;
2167 if (DBG) log(logStr);
2168 AnomalyReporter.reportAnomaly(
2169 UUID.fromString(PURCHASE_PREMIUM_CAPABILITY_ERROR_UUID), logStr);
2170 }
2171 break;
2172
Michele Berionne5e411512020-11-13 02:36:59 +00002173 case CMD_PREPARE_UNATTENDED_REBOOT:
2174 request = (MainThreadRequest) msg.obj;
2175 request.result =
Rafael Higuera Silvad9630642021-09-20 15:32:01 +00002176 UiccController.getInstance().getPinStorage()
2177 .prepareUnattendedReboot(request.workSource);
Michele Berionne5e411512020-11-13 02:36:59 +00002178 notifyRequester(request);
2179 break;
2180
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002181 default:
2182 Log.w(LOG_TAG, "MainThreadHandler: unexpected message code: " + msg.what);
2183 break;
2184 }
2185 }
Jake Hambye994d462014-02-03 13:10:13 -08002186
Pengquan Menga1bb6272018-09-06 09:59:22 -07002187 private void notifyRequester(MainThreadRequest request) {
2188 synchronized (request) {
2189 request.notifyAll();
2190 }
2191 }
2192
Jake Hambye994d462014-02-03 13:10:13 -08002193 private void handleNullReturnEvent(Message msg, String command) {
2194 AsyncResult ar = (AsyncResult) msg.obj;
2195 MainThreadRequest request = (MainThreadRequest) ar.userObj;
2196 if (ar.exception == null) {
2197 request.result = true;
2198 } else {
2199 request.result = false;
2200 if (ar.exception instanceof CommandException) {
2201 loge(command + ": CommandException: " + ar.exception);
2202 } else {
2203 loge(command + ": Unknown exception");
2204 }
2205 }
Pengquan Menga1bb6272018-09-06 09:59:22 -07002206 notifyRequester(request);
Jake Hambye994d462014-02-03 13:10:13 -08002207 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002208 }
2209
2210 /**
2211 * Posts the specified command to be executed on the main thread,
2212 * waits for the request to complete, and returns the result.
2213 * @see #sendRequestAsync
2214 */
2215 private Object sendRequest(int command, Object argument) {
Rambo Wang0f050d82021-02-12 11:43:36 -08002216 return sendRequest(command, argument, SubscriptionManager.INVALID_SUBSCRIPTION_ID, null,
2217 null, -1 /*timeoutInMs*/);
vagdeviaf9a5b92018-08-15 16:01:53 -07002218 }
2219
2220 /**
2221 * Posts the specified command to be executed on the main thread,
2222 * waits for the request to complete, and returns the result.
2223 * @see #sendRequestAsync
2224 */
2225 private Object sendRequest(int command, Object argument, WorkSource workSource) {
2226 return sendRequest(command, argument, SubscriptionManager.INVALID_SUBSCRIPTION_ID,
Rambo Wang0f050d82021-02-12 11:43:36 -08002227 null, workSource, -1 /*timeoutInMs*/);
Wink Saville36469e72014-06-11 15:17:00 -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 */
Shishir Agrawal76d5da92014-11-09 16:17:25 -08002235 private Object sendRequest(int command, Object argument, Integer subId) {
Rambo Wang0f050d82021-02-12 11:43:36 -08002236 return sendRequest(command, argument, subId, null, null, -1 /*timeoutInMs*/);
2237 }
2238
2239 /**
2240 * Posts the specified command to be executed on the main thread,
2241 * waits for the request to complete for at most {@code timeoutInMs}, and returns the result
2242 * if not timeout or null otherwise.
2243 * @see #sendRequestAsync
2244 */
2245 private @Nullable Object sendRequest(int command, Object argument, Integer subId,
2246 long timeoutInMs) {
2247 return sendRequest(command, argument, subId, null, null, timeoutInMs);
vagdeviaf9a5b92018-08-15 16:01:53 -07002248 }
2249
2250 /**
2251 * Posts the specified command to be executed on the main thread,
2252 * waits for the request to complete, and returns the result.
2253 * @see #sendRequestAsync
2254 */
Nathan Harold92bed182018-10-12 18:16:49 -07002255 private Object sendRequest(int command, Object argument, int subId, WorkSource workSource) {
Rambo Wang0f050d82021-02-12 11:43:36 -08002256 return sendRequest(command, argument, subId, null, workSource, -1 /*timeoutInMs*/);
Nathan Harold92bed182018-10-12 18:16:49 -07002257 }
2258
2259 /**
2260 * Posts the specified command to be executed on the main thread,
2261 * waits for the request to complete, and returns the result.
2262 * @see #sendRequestAsync
2263 */
2264 private Object sendRequest(int command, Object argument, Phone phone, WorkSource workSource) {
Rambo Wang0f050d82021-02-12 11:43:36 -08002265 return sendRequest(command, argument, SubscriptionManager.INVALID_SUBSCRIPTION_ID, phone,
2266 workSource, -1 /*timeoutInMs*/);
Nathan Harold92bed182018-10-12 18:16:49 -07002267 }
2268
2269 /**
Rambo Wang0f050d82021-02-12 11:43:36 -08002270 * Posts the specified command to be executed on the main thread. If {@code timeoutInMs} is
2271 * negative, waits for the request to complete, and returns the result. Otherwise, wait for
2272 * maximum of {@code timeoutInMs} milliseconds, interrupt and return null.
Nathan Harold92bed182018-10-12 18:16:49 -07002273 * @see #sendRequestAsync
2274 */
Rambo Wang0f050d82021-02-12 11:43:36 -08002275 private @Nullable Object sendRequest(int command, Object argument, Integer subId, Phone phone,
2276 WorkSource workSource, long timeoutInMs) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002277 if (Looper.myLooper() == mMainThreadHandler.getLooper()) {
2278 throw new RuntimeException("This method will deadlock if called from the main thread.");
2279 }
2280
Nathan Harold92bed182018-10-12 18:16:49 -07002281 MainThreadRequest request = null;
2282 if (subId != SubscriptionManager.INVALID_SUBSCRIPTION_ID && phone != null) {
2283 throw new IllegalArgumentException("subId and phone cannot both be specified!");
2284 } else if (phone != null) {
2285 request = new MainThreadRequest(argument, phone, workSource);
2286 } else {
2287 request = new MainThreadRequest(argument, subId, workSource);
2288 }
2289
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002290 Message msg = mMainThreadHandler.obtainMessage(command, request);
2291 msg.sendToTarget();
2292
Rambo Wang0f050d82021-02-12 11:43:36 -08002293
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002294 synchronized (request) {
Rambo Wang0f050d82021-02-12 11:43:36 -08002295 if (timeoutInMs >= 0) {
2296 // Wait for at least timeoutInMs before returning null request result
2297 long now = SystemClock.elapsedRealtime();
2298 long deadline = now + timeoutInMs;
Grace Jia8a0a1e82021-05-23 22:59:52 -07002299 while (request.result == null && now < deadline) {
Rambo Wang0f050d82021-02-12 11:43:36 -08002300 try {
2301 request.wait(deadline - now);
2302 } catch (InterruptedException e) {
2303 // Do nothing, go back and check if request is completed or timeout
2304 } finally {
2305 now = SystemClock.elapsedRealtime();
2306 }
2307 }
2308 } else {
2309 // Wait for the request to complete
2310 while (request.result == null) {
2311 try {
2312 request.wait();
2313 } catch (InterruptedException e) {
2314 // Do nothing, go back and wait until the request is complete
2315 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002316 }
2317 }
2318 }
Rambo Wang0f050d82021-02-12 11:43:36 -08002319 if (request.result == null) {
2320 Log.wtf(LOG_TAG,
2321 "sendRequest: Blocking command timed out. Something has gone terribly wrong.");
2322 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002323 return request.result;
2324 }
2325
2326 /**
2327 * Asynchronous ("fire and forget") version of sendRequest():
2328 * Posts the specified command to be executed on the main thread, and
2329 * returns immediately.
2330 * @see #sendRequest
2331 */
2332 private void sendRequestAsync(int command) {
2333 mMainThreadHandler.sendEmptyMessage(command);
2334 }
2335
2336 /**
Sailesh Nepalbd76e4e2013-10-27 13:59:44 -07002337 * Same as {@link #sendRequestAsync(int)} except it takes an argument.
Nathan Haroldfa8da0f2018-09-27 18:51:29 -07002338 * @see {@link #sendRequest(int)}
Sailesh Nepalbd76e4e2013-10-27 13:59:44 -07002339 */
2340 private void sendRequestAsync(int command, Object argument) {
Nathan Haroldfa8da0f2018-09-27 18:51:29 -07002341 sendRequestAsync(command, argument, null, null);
2342 }
2343
2344 /**
2345 * Same as {@link #sendRequestAsync(int,Object)} except it takes a Phone and WorkSource.
2346 * @see {@link #sendRequest(int,Object)}
2347 */
2348 private void sendRequestAsync(
2349 int command, Object argument, Phone phone, WorkSource workSource) {
2350 MainThreadRequest request = new MainThreadRequest(argument, phone, workSource);
Sailesh Nepalbd76e4e2013-10-27 13:59:44 -07002351 Message msg = mMainThreadHandler.obtainMessage(command, request);
2352 msg.sendToTarget();
2353 }
2354
2355 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002356 * Initialize the singleton PhoneInterfaceManager instance.
2357 * This is only done once, at startup, from PhoneApp.onCreate().
2358 */
Nazanin Bakhshif782e562018-12-11 15:15:39 -08002359 /* package */ static PhoneInterfaceManager init(PhoneGlobals app) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002360 synchronized (PhoneInterfaceManager.class) {
2361 if (sInstance == null) {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08002362 sInstance = new PhoneInterfaceManager(app);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002363 } else {
2364 Log.wtf(LOG_TAG, "init() called multiple times! sInstance = " + sInstance);
2365 }
2366 return sInstance;
2367 }
2368 }
2369
2370 /** Private constructor; @see init() */
Jordan Liu1979a042020-03-20 21:39:35 +00002371 private PhoneInterfaceManager(PhoneGlobals app) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002372 mApp = app;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002373 mCM = PhoneGlobals.getInstance().mCM;
Brad Ebingerd1947d82021-05-17 20:54:49 +00002374 mImsResolver = ImsResolver.getInstance();
Stuart Scott981d8582015-04-21 14:09:50 -07002375 mUserManager = (UserManager) app.getSystemService(Context.USER_SERVICE);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002376 mAppOps = (AppOpsManager)app.getSystemService(Context.APP_OPS_SERVICE);
Grace Jia0ddb3612021-04-22 13:35:26 -07002377 mPm = app.getSystemService(PackageManager.class);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002378 mMainThreadHandler = new MainThreadHandler();
Tobias Thiererb19e1f12018-12-11 17:54:03 +00002379 mSubscriptionController = SubscriptionController.getInstance();
Nazanin Bakhshif782e562018-12-11 15:15:39 -08002380 mTelephonySharedPreferences =
2381 PreferenceManager.getDefaultSharedPreferences(mApp);
yinxub1bed742017-04-17 11:45:04 -07002382 mNetworkScanRequestTracker = new NetworkScanRequestTracker();
Malcolm Chen2c63d402018-08-14 16:00:53 -07002383 mPhoneConfigurationManager = PhoneConfigurationManager.getInstance();
Daniel Bright94f43662021-03-01 14:43:40 -08002384 mRadioInterfaceCapabilities = RadioInterfaceCapabilityController.getInstance();
Peter Wanga3cf4ac2020-01-27 09:39:46 +08002385 mNotifyUserActivity = new AtomicBoolean(false);
Tyler Gunn64144d92022-03-17 14:16:41 -07002386 PropertyInvalidatedCache.invalidateCache(TelephonyManager.CACHE_KEY_PHONE_ACCOUNT_TO_SUBID);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002387 publish();
2388 }
2389
Nazanin Bakhshif782e562018-12-11 15:15:39 -08002390 private Phone getDefaultPhone() {
2391 Phone thePhone = getPhone(getDefaultSubscription());
2392 return (thePhone != null) ? thePhone : PhoneFactory.getDefaultPhone();
2393 }
2394
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002395 private void publish() {
2396 if (DBG) log("publish: " + this);
2397
Peter Wangc035ce42020-01-08 21:00:22 -08002398 TelephonyFrameworkInitializer
2399 .getTelephonyServiceManager()
2400 .getTelephonyServiceRegisterer()
2401 .register(this);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002402 }
2403
Stuart Scott584921c2015-01-15 17:10:34 -08002404 private Phone getPhoneFromRequest(MainThreadRequest request) {
Jordan Liu4c733742019-02-28 12:03:40 -08002405 if (request.phone != null) {
2406 return request.phone;
2407 } else {
2408 return getPhoneFromSubId(request.subId);
2409 }
2410 }
2411
2412 private Phone getPhoneFromSubId(int subId) {
2413 return (subId == SubscriptionManager.INVALID_SUBSCRIPTION_ID)
2414 ? getDefaultPhone() : getPhone(subId);
Stuart Scott584921c2015-01-15 17:10:34 -08002415 }
2416
Rambo Wange53e07d2022-05-10 13:01:13 -07002417 @Nullable
2418 private UiccPort getUiccPortFromRequest(@NonNull MainThreadRequest request) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002419 Phone phone = getPhoneFromRequest(request);
2420 return phone == null ? null :
Muralidhar Reddy472c2ae2021-09-29 19:38:40 +00002421 UiccController.getInstance().getUiccPort(phone.getPhoneId());
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002422 }
2423
Wink Saville36469e72014-06-11 15:17:00 -07002424 // returns phone associated with the subId.
Wink Savilleb564aae2014-10-23 10:18:09 -07002425 private Phone getPhone(int subId) {
Wink Savilleac1bdfd2014-11-20 23:04:44 -08002426 return PhoneFactory.getPhone(mSubscriptionController.getPhoneId(subId));
Wink Saville36469e72014-06-11 15:17:00 -07002427 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002428
Kai Shif70f46f2021-03-03 13:59:46 -08002429 private void sendEraseModemConfig(@NonNull Phone phone) {
2430 Boolean success = (Boolean) sendRequest(CMD_ERASE_MODEM_CONFIG, null);
2431 if (DBG) log("eraseModemConfig:" + ' ' + (success ? "ok" : "fail"));
2432 }
2433
2434 private void sendEraseDataInSharedPreferences(@NonNull Phone phone) {
2435 Boolean success = (Boolean) sendRequest(CMD_ERASE_DATA_SHARED_PREFERENCES, null);
2436 if (DBG) log("eraseDataInSharedPreferences:" + ' ' + (success ? "ok" : "fail"));
Naina Nallurid63128d2019-09-17 14:10:30 -07002437 }
2438
Peter Wang44b186e2020-01-13 23:33:09 -08002439 private boolean isImsAvailableOnDevice() {
2440 PackageManager pm = getDefaultPhone().getContext().getPackageManager();
2441 if (pm == null) {
2442 // For some reason package manger is not available.. This will fail internally anyway,
2443 // so do not throw error and allow.
2444 return true;
2445 }
2446 return pm.hasSystemFeature(PackageManager.FEATURE_TELEPHONY_IMS, 0);
2447 }
2448
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002449 public void dial(String number) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07002450 dialForSubscriber(getPreferredVoiceSubscription(), number);
Wink Saville36469e72014-06-11 15:17:00 -07002451 }
2452
Wink Savilleb564aae2014-10-23 10:18:09 -07002453 public void dialForSubscriber(int subId, String number) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002454 if (DBG) log("dial: " + number);
2455 // No permission check needed here: This is just a wrapper around the
2456 // ACTION_DIAL intent, which is available to any app since it puts up
2457 // the UI before it does anything.
2458
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002459 final long identity = Binder.clearCallingIdentity();
2460 try {
2461 String url = createTelUrl(number);
2462 if (url == null) {
2463 return;
2464 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002465
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002466 // PENDING: should we just silently fail if phone is offhook or ringing?
2467 PhoneConstants.State state = mCM.getState(subId);
2468 if (state != PhoneConstants.State.OFFHOOK && state != PhoneConstants.State.RINGING) {
2469 Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse(url));
2470 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
2471 mApp.startActivity(intent);
2472 }
2473 } finally {
2474 Binder.restoreCallingIdentity(identity);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002475 }
2476 }
2477
2478 public void call(String callingPackage, String number) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07002479 callForSubscriber(getPreferredVoiceSubscription(), callingPackage, number);
Wink Saville36469e72014-06-11 15:17:00 -07002480 }
2481
Wink Savilleb564aae2014-10-23 10:18:09 -07002482 public void callForSubscriber(int subId, String callingPackage, String number) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002483 if (DBG) log("call: " + number);
2484
2485 // This is just a wrapper around the ACTION_CALL intent, but we still
2486 // need to do a permission check since we're calling startActivity()
2487 // from the context of the phone app.
2488 enforceCallPermission();
2489
Jordan Liu1617b712019-07-10 15:06:26 -07002490 if (mAppOps.noteOp(AppOpsManager.OPSTR_CALL_PHONE, Binder.getCallingUid(), callingPackage)
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002491 != AppOpsManager.MODE_ALLOWED) {
2492 return;
2493 }
2494
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002495 final long identity = Binder.clearCallingIdentity();
2496 try {
2497 String url = createTelUrl(number);
2498 if (url == null) {
2499 return;
2500 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002501
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002502 boolean isValid = false;
2503 final List<SubscriptionInfo> slist = getActiveSubscriptionInfoListPrivileged();
2504 if (slist != null) {
2505 for (SubscriptionInfo subInfoRecord : slist) {
2506 if (subInfoRecord.getSubscriptionId() == subId) {
2507 isValid = true;
2508 break;
2509 }
Wink Saville3ab207e2014-11-20 13:07:20 -08002510 }
Wink Saville08874612014-08-31 19:19:58 -07002511 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002512 if (!isValid) {
2513 return;
2514 }
Wink Saville08874612014-08-31 19:19:58 -07002515
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002516 Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse(url));
2517 intent.putExtra(SUBSCRIPTION_KEY, subId);
2518 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
2519 mApp.startActivity(intent);
2520 } finally {
2521 Binder.restoreCallingIdentity(identity);
2522 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002523 }
2524
Wink Savilleb564aae2014-10-23 10:18:09 -07002525 public boolean supplyPinForSubscriber(int subId, String pin) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07002526 int [] resultArray = supplyPinReportResultForSubscriber(subId, pin);
Wink Saville9de0f752013-10-22 19:04:03 -07002527 return (resultArray[0] == PhoneConstants.PIN_RESULT_SUCCESS) ? true : false;
2528 }
2529
Wink Savilleb564aae2014-10-23 10:18:09 -07002530 public boolean supplyPukForSubscriber(int subId, String puk, String pin) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07002531 int [] resultArray = supplyPukReportResultForSubscriber(subId, puk, pin);
Wink Saville9de0f752013-10-22 19:04:03 -07002532 return (resultArray[0] == PhoneConstants.PIN_RESULT_SUCCESS) ? true : false;
2533 }
2534
Wink Savilleb564aae2014-10-23 10:18:09 -07002535 public int[] supplyPinReportResultForSubscriber(int subId, String pin) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002536 enforceModifyPermission();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002537
2538 final long identity = Binder.clearCallingIdentity();
2539 try {
Michele Berionne5e411512020-11-13 02:36:59 +00002540 Phone phone = getPhone(subId);
2541 final UnlockSim checkSimPin = new UnlockSim(phone.getPhoneId(), phone.getIccCard());
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002542 checkSimPin.start();
2543 return checkSimPin.unlockSim(null, pin);
2544 } finally {
2545 Binder.restoreCallingIdentity(identity);
2546 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002547 }
2548
Wink Savilleb564aae2014-10-23 10:18:09 -07002549 public int[] supplyPukReportResultForSubscriber(int subId, String puk, String pin) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002550 enforceModifyPermission();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002551
2552 final long identity = Binder.clearCallingIdentity();
2553 try {
Michele Berionne5e411512020-11-13 02:36:59 +00002554 Phone phone = getPhone(subId);
2555 final UnlockSim checkSimPuk = new UnlockSim(phone.getPhoneId(), phone.getIccCard());
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002556 checkSimPuk.start();
2557 return checkSimPuk.unlockSim(puk, pin);
2558 } finally {
2559 Binder.restoreCallingIdentity(identity);
2560 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002561 }
2562
2563 /**
Wink Saville9de0f752013-10-22 19:04:03 -07002564 * Helper thread to turn async call to SimCard#supplyPin into
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002565 * a synchronous one.
2566 */
2567 private static class UnlockSim extends Thread {
2568
2569 private final IccCard mSimCard;
Michele Berionne5e411512020-11-13 02:36:59 +00002570 private final int mPhoneId;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002571
2572 private boolean mDone = false;
Wink Saville9de0f752013-10-22 19:04:03 -07002573 private int mResult = PhoneConstants.PIN_GENERAL_FAILURE;
2574 private int mRetryCount = -1;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002575
2576 // For replies from SimCard interface
2577 private Handler mHandler;
2578
2579 // For async handler to identify request type
2580 private static final int SUPPLY_PIN_COMPLETE = 100;
2581
Michele Berionne5e411512020-11-13 02:36:59 +00002582 UnlockSim(int phoneId, IccCard simCard) {
2583 mPhoneId = phoneId;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002584 mSimCard = simCard;
2585 }
2586
2587 @Override
2588 public void run() {
2589 Looper.prepare();
2590 synchronized (UnlockSim.this) {
2591 mHandler = new Handler() {
2592 @Override
2593 public void handleMessage(Message msg) {
2594 AsyncResult ar = (AsyncResult) msg.obj;
2595 switch (msg.what) {
2596 case SUPPLY_PIN_COMPLETE:
2597 Log.d(LOG_TAG, "SUPPLY_PIN_COMPLETE");
2598 synchronized (UnlockSim.this) {
Wink Saville9de0f752013-10-22 19:04:03 -07002599 mRetryCount = msg.arg1;
2600 if (ar.exception != null) {
2601 if (ar.exception instanceof CommandException &&
2602 ((CommandException)(ar.exception)).getCommandError()
2603 == CommandException.Error.PASSWORD_INCORRECT) {
2604 mResult = PhoneConstants.PIN_PASSWORD_INCORRECT;
vivi.lib5e9ada2019-09-12 16:04:24 +08002605 } //When UiccCardApp dispose,handle message and return exception
2606 else if (ar.exception instanceof CommandException &&
2607 ((CommandException) (ar.exception)).getCommandError()
2608 == CommandException.Error.ABORTED) {
2609 mResult = PhoneConstants.PIN_OPERATION_ABORTED;
Wink Saville9de0f752013-10-22 19:04:03 -07002610 } else {
2611 mResult = PhoneConstants.PIN_GENERAL_FAILURE;
2612 }
2613 } else {
2614 mResult = PhoneConstants.PIN_RESULT_SUCCESS;
2615 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002616 mDone = true;
2617 UnlockSim.this.notifyAll();
2618 }
2619 break;
2620 }
2621 }
2622 };
2623 UnlockSim.this.notifyAll();
2624 }
2625 Looper.loop();
2626 }
2627
2628 /*
2629 * Use PIN or PUK to unlock SIM card
2630 *
2631 * If PUK is null, unlock SIM card with PIN
2632 *
2633 * If PUK is not null, unlock SIM card with PUK and set PIN code
2634 */
Wink Saville9de0f752013-10-22 19:04:03 -07002635 synchronized int[] unlockSim(String puk, String pin) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002636
2637 while (mHandler == null) {
2638 try {
2639 wait();
2640 } catch (InterruptedException e) {
2641 Thread.currentThread().interrupt();
2642 }
2643 }
2644 Message callback = Message.obtain(mHandler, SUPPLY_PIN_COMPLETE);
2645
2646 if (puk == null) {
2647 mSimCard.supplyPin(pin, callback);
2648 } else {
2649 mSimCard.supplyPuk(puk, pin, callback);
2650 }
2651
2652 while (!mDone) {
2653 try {
2654 Log.d(LOG_TAG, "wait for done");
2655 wait();
2656 } catch (InterruptedException e) {
2657 // Restore the interrupted status
2658 Thread.currentThread().interrupt();
2659 }
2660 }
2661 Log.d(LOG_TAG, "done");
Wink Saville9de0f752013-10-22 19:04:03 -07002662 int[] resultArray = new int[2];
2663 resultArray[0] = mResult;
2664 resultArray[1] = mRetryCount;
Michele Berionne5e411512020-11-13 02:36:59 +00002665
2666 if (mResult == PhoneConstants.PIN_RESULT_SUCCESS && pin.length() > 0) {
Jon Spivack9c3bc762021-10-06 20:53:09 +00002667 UiccController.getInstance().getPinStorage().storePin(pin, mPhoneId);
Michele Berionne5e411512020-11-13 02:36:59 +00002668 }
2669
Wink Saville9de0f752013-10-22 19:04:03 -07002670 return resultArray;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002671 }
2672 }
2673
Nathan Harold7c8d0f12020-05-28 20:40:31 -07002674 /**
2675 * This method has been removed due to privacy and stability concerns.
2676 */
2677 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002678 public void updateServiceLocation() {
Nathan Harold7c8d0f12020-05-28 20:40:31 -07002679 Log.e(LOG_TAG, "Call to unsupported method updateServiceLocation()");
2680 return;
Wink Saville36469e72014-06-11 15:17:00 -07002681 }
2682
Nathan Harold1f889d82020-06-04 17:05:26 -07002683 @Override
2684 public void updateServiceLocationWithPackageName(String callingPackage) {
2685 mApp.getSystemService(AppOpsManager.class)
2686 .checkPackage(Binder.getCallingUid(), callingPackage);
2687
Nathan Haroldf096d982020-11-18 17:18:06 -08002688 final int targetSdk = TelephonyPermissions.getTargetSdk(mApp, callingPackage);
Nathan Harold1f889d82020-06-04 17:05:26 -07002689 if (targetSdk > android.os.Build.VERSION_CODES.R) {
2690 // Callers targeting S have no business invoking this method.
2691 return;
2692 }
2693
2694 LocationAccessPolicy.LocationPermissionResult locationResult =
2695 LocationAccessPolicy.checkLocationPermission(mApp,
2696 new LocationAccessPolicy.LocationPermissionQuery.Builder()
2697 .setCallingPackage(callingPackage)
2698 .setCallingFeatureId(null)
2699 .setCallingPid(Binder.getCallingPid())
2700 .setCallingUid(Binder.getCallingUid())
2701 .setMethod("updateServiceLocation")
2702 .setMinSdkVersionForCoarse(Build.VERSION_CODES.BASE)
2703 .setMinSdkVersionForFine(Build.VERSION_CODES.Q)
2704 .build());
2705 // Apps that lack location permission have no business calling this method;
2706 // however, because no permission was declared in the public API, denials must
2707 // all be "soft".
2708 switch (locationResult) {
2709 case DENIED_HARD: /* fall through */
2710 case DENIED_SOFT:
2711 return;
2712 }
2713
2714 WorkSource workSource = getWorkSource(Binder.getCallingUid());
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002715 final long identity = Binder.clearCallingIdentity();
2716 try {
Nathan Harold1f889d82020-06-04 17:05:26 -07002717 final Phone phone = getPhone(getDefaultSubscription());
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002718 if (phone != null) {
Nathan Harold1f889d82020-06-04 17:05:26 -07002719 phone.updateServiceLocation(workSource);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002720 }
2721 } finally {
2722 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07002723 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002724 }
2725
Philip P. Moltmann700a9592019-10-03 11:53:50 -07002726 @Deprecated
Robert Greenwalt36b23af2015-07-06 17:59:14 -07002727 @Override
2728 public boolean isRadioOn(String callingPackage) {
Philip P. Moltmann700a9592019-10-03 11:53:50 -07002729 return isRadioOnWithFeature(callingPackage, null);
2730 }
2731
2732
2733 @Override
2734 public boolean isRadioOnWithFeature(String callingPackage, String callingFeatureId) {
2735 return isRadioOnForSubscriberWithFeature(getDefaultSubscription(), callingPackage,
2736 callingFeatureId);
2737 }
2738
2739 @Deprecated
2740 @Override
2741 public boolean isRadioOnForSubscriber(int subId, String callingPackage) {
2742 return isRadioOnForSubscriberWithFeature(subId, callingPackage, null);
Wink Saville36469e72014-06-11 15:17:00 -07002743 }
2744
Robert Greenwalt36b23af2015-07-06 17:59:14 -07002745 @Override
Philip P. Moltmann700a9592019-10-03 11:53:50 -07002746 public boolean isRadioOnForSubscriberWithFeature(int subId, String callingPackage,
2747 String callingFeatureId) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08002748 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
Philip P. Moltmann700a9592019-10-03 11:53:50 -07002749 mApp, subId, callingPackage, callingFeatureId, "isRadioOnForSubscriber")) {
Robert Greenwalt36b23af2015-07-06 17:59:14 -07002750 return false;
2751 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002752
2753 final long identity = Binder.clearCallingIdentity();
2754 try {
2755 return isRadioOnForSubscriber(subId);
2756 } finally {
2757 Binder.restoreCallingIdentity(identity);
2758 }
Robert Greenwalt36b23af2015-07-06 17:59:14 -07002759 }
2760
2761 private boolean isRadioOnForSubscriber(int subId) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002762 final long identity = Binder.clearCallingIdentity();
2763 try {
2764 final Phone phone = getPhone(subId);
2765 if (phone != null) {
2766 return phone.getServiceState().getState() != ServiceState.STATE_POWER_OFF;
2767 } else {
2768 return false;
2769 }
2770 } finally {
2771 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07002772 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002773 }
2774
2775 public void toggleRadioOnOff() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07002776 toggleRadioOnOffForSubscriber(getDefaultSubscription());
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002777 }
Wink Saville36469e72014-06-11 15:17:00 -07002778
Wink Savilleb564aae2014-10-23 10:18:09 -07002779 public void toggleRadioOnOffForSubscriber(int subId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002780 enforceModifyPermission();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002781
2782 final long identity = Binder.clearCallingIdentity();
2783 try {
2784 final Phone phone = getPhone(subId);
2785 if (phone != null) {
2786 phone.setRadioPower(!isRadioOnForSubscriber(subId));
2787 }
2788 } finally {
2789 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07002790 }
Wink Saville36469e72014-06-11 15:17:00 -07002791 }
2792
2793 public boolean setRadio(boolean turnOn) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07002794 return setRadioForSubscriber(getDefaultSubscription(), turnOn);
Wink Saville36469e72014-06-11 15:17:00 -07002795 }
2796
Wink Savilleb564aae2014-10-23 10:18:09 -07002797 public boolean setRadioForSubscriber(int subId, boolean turnOn) {
Wink Saville36469e72014-06-11 15:17:00 -07002798 enforceModifyPermission();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002799
2800 final long identity = Binder.clearCallingIdentity();
2801 try {
2802 final Phone phone = getPhone(subId);
2803 if (phone == null) {
2804 return false;
2805 }
2806 if ((phone.getServiceState().getState() != ServiceState.STATE_POWER_OFF) != turnOn) {
2807 toggleRadioOnOffForSubscriber(subId);
2808 }
2809 return true;
2810 } finally {
2811 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07002812 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002813 }
Wink Saville36469e72014-06-11 15:17:00 -07002814
Naveen Kalla1fd79bd2014-08-08 00:48:59 -07002815 public boolean needMobileRadioShutdown() {
Shuo Qianfa7b6b32019-12-10 10:40:38 -08002816 enforceReadPrivilegedPermission("needMobileRadioShutdown");
Naveen Kalla1fd79bd2014-08-08 00:48:59 -07002817 /*
2818 * If any of the Radios are available, it will need to be
2819 * shutdown. So return true if any Radio is available.
2820 */
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002821 final long identity = Binder.clearCallingIdentity();
2822 try {
2823 for (int i = 0; i < TelephonyManager.getDefault().getPhoneCount(); i++) {
2824 Phone phone = PhoneFactory.getPhone(i);
2825 if (phone != null && phone.isRadioAvailable()) return true;
2826 }
2827 logv(TelephonyManager.getDefault().getPhoneCount() + " Phones are shutdown.");
2828 return false;
2829 } finally {
2830 Binder.restoreCallingIdentity(identity);
Naveen Kalla1fd79bd2014-08-08 00:48:59 -07002831 }
Naveen Kalla1fd79bd2014-08-08 00:48:59 -07002832 }
2833
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002834 @Override
Naveen Kalla1fd79bd2014-08-08 00:48:59 -07002835 public void shutdownMobileRadios() {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002836 enforceModifyPermission();
2837
2838 final long identity = Binder.clearCallingIdentity();
2839 try {
2840 for (int i = 0; i < TelephonyManager.getDefault().getPhoneCount(); i++) {
2841 logv("Shutting down Phone " + i);
2842 shutdownRadioUsingPhoneId(i);
2843 }
2844 } finally {
2845 Binder.restoreCallingIdentity(identity);
Naveen Kalla1fd79bd2014-08-08 00:48:59 -07002846 }
2847 }
2848
2849 private void shutdownRadioUsingPhoneId(int phoneId) {
Naveen Kalla1fd79bd2014-08-08 00:48:59 -07002850 Phone phone = PhoneFactory.getPhone(phoneId);
2851 if (phone != null && phone.isRadioAvailable()) {
2852 phone.shutdownRadio();
2853 }
2854 }
2855
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002856 public boolean setRadioPower(boolean turnOn) {
Jack Yub4e16162017-05-15 12:48:40 -07002857 enforceModifyPermission();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002858
2859 final long identity = Binder.clearCallingIdentity();
2860 try {
2861 final Phone defaultPhone = PhoneFactory.getDefaultPhone();
2862 if (defaultPhone != null) {
2863 defaultPhone.setRadioPower(turnOn);
2864 return true;
2865 } else {
2866 loge("There's no default phone.");
2867 return false;
2868 }
2869 } finally {
2870 Binder.restoreCallingIdentity(identity);
Wei Liu9ae2a062016-08-08 11:09:34 -07002871 }
Wink Saville36469e72014-06-11 15:17:00 -07002872 }
2873
Wink Savilleb564aae2014-10-23 10:18:09 -07002874 public boolean setRadioPowerForSubscriber(int subId, boolean turnOn) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002875 enforceModifyPermission();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002876
2877 final long identity = Binder.clearCallingIdentity();
2878 try {
2879 final Phone phone = getPhone(subId);
2880 if (phone != null) {
2881 phone.setRadioPower(turnOn);
2882 return true;
2883 } else {
2884 return false;
2885 }
2886 } finally {
2887 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07002888 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002889 }
2890
Thomas Nguyenfd0572f2022-07-15 22:28:49 +00002891 /**
2892 * Vote on powering off the radio for a reason. The radio will be turned on only when there is
2893 * no reason to power it off. When any of the voters want to power it off, it will be turned
2894 * off. In case of emergency, the radio will be turned on even if there are some reasons for
2895 * powering it off, and these radio off votes will be cleared.
2896 * Multiple apps can vote for the same reason and the last vote will take effect. Each app is
2897 * responsible for its vote. A powering-off vote of a reason will be maintained until it is
2898 * cleared by calling {@link clearRadioPowerOffForReason} for that reason, or an emergency call
2899 * is made, or the device is rebooted. When an app comes backup from a crash, it needs to make
2900 * sure if its vote is as expected. An app can use the API {@link getRadioPowerOffReasons} to
2901 * check its vote.
2902 *
2903 * @param subId The subscription ID.
2904 * @param reason The reason for powering off radio.
2905 * @return true on success and false on failure.
2906 */
2907 public boolean requestRadioPowerOffForReason(int subId,
2908 @TelephonyManager.RadioPowerReason int reason) {
2909 enforceModifyPermission();
2910
2911 final long identity = Binder.clearCallingIdentity();
2912 try {
2913 final Phone phone = getPhone(subId);
2914 if (phone != null) {
2915 phone.setRadioPowerForReason(false, reason);
2916 return true;
2917 } else {
2918 return false;
2919 }
2920 } finally {
2921 Binder.restoreCallingIdentity(identity);
2922 }
2923 }
2924
2925 /**
2926 * Remove the vote on powering off the radio for a reason, as requested by
2927 * {@link requestRadioPowerOffForReason}.
2928 *
2929 * @param subId The subscription ID.
2930 * @param reason The reason for powering off radio.
2931 * @return true on success and false on failure.
2932 */
2933 public boolean clearRadioPowerOffForReason(int subId,
2934 @TelephonyManager.RadioPowerReason int reason) {
2935 enforceModifyPermission();
2936
2937 final long identity = Binder.clearCallingIdentity();
2938 try {
2939 final Phone phone = getPhone(subId);
2940 if (phone != null) {
2941 phone.setRadioPowerForReason(true, reason);
2942 return true;
2943 } else {
2944 return false;
2945 }
2946 } finally {
2947 Binder.restoreCallingIdentity(identity);
2948 }
2949 }
2950
2951 /**
2952 * Get reasons for powering off radio, as requested by {@link requestRadioPowerOffForReason}.
2953 *
2954 * @param subId The subscription ID.
2955 * @param callingPackage The package making the call.
2956 * @param callingFeatureId The feature in the package.
2957 * @return List of reasons for powering off radio.
2958 */
2959 public List getRadioPowerOffReasons(int subId, String callingPackage, String callingFeatureId) {
2960 enforceReadPrivilegedPermission("getRadioPowerOffReasons");
2961
2962 final long identity = Binder.clearCallingIdentity();
2963 List result = new ArrayList();
2964 try {
2965 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(mApp, subId,
2966 callingPackage, callingFeatureId, "getRadioPowerOffReasons")) {
2967 return result;
2968 }
2969
2970 final Phone phone = getPhone(subId);
2971 if (phone != null) {
2972 result.addAll(phone.getRadioPowerOffReasons());
2973 }
2974 } finally {
2975 Binder.restoreCallingIdentity(identity);
2976 }
2977 return result;
2978 }
2979
Wink Saville36469e72014-06-11 15:17:00 -07002980 // FIXME: subId version needed
Sanket Padawe356d7632015-06-22 14:03:32 -07002981 @Override
Sarah Chinecc78c42022-03-31 21:16:48 -07002982 public boolean enableDataConnectivity(String callingPackage) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002983 enforceModifyPermission();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002984
2985 final long identity = Binder.clearCallingIdentity();
2986 try {
2987 int subId = mSubscriptionController.getDefaultDataSubId();
2988 final Phone phone = getPhone(subId);
2989 if (phone != null) {
Jack Yu7968c6d2022-07-31 00:43:21 -07002990 phone.getDataSettingsManager().setDataEnabled(
2991 TelephonyManager.DATA_ENABLED_REASON_USER, true, callingPackage);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002992 return true;
2993 } else {
2994 return false;
2995 }
2996 } finally {
2997 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07002998 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002999 }
3000
Wink Saville36469e72014-06-11 15:17:00 -07003001 // FIXME: subId version needed
Sanket Padawe356d7632015-06-22 14:03:32 -07003002 @Override
Sarah Chinecc78c42022-03-31 21:16:48 -07003003 public boolean disableDataConnectivity(String callingPackage) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003004 enforceModifyPermission();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003005
3006 final long identity = Binder.clearCallingIdentity();
3007 try {
3008 int subId = mSubscriptionController.getDefaultDataSubId();
3009 final Phone phone = getPhone(subId);
3010 if (phone != null) {
Jack Yu7968c6d2022-07-31 00:43:21 -07003011 phone.getDataSettingsManager().setDataEnabled(
3012 TelephonyManager.DATA_ENABLED_REASON_USER, false, callingPackage);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003013 return true;
3014 } else {
3015 return false;
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 Yuacf8a132017-05-01 17:00:48 -07003023 public boolean isDataConnectivityPossible(int subId) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003024 final long identity = Binder.clearCallingIdentity();
3025 try {
3026 final Phone phone = getPhone(subId);
3027 if (phone != null) {
Jack Yu59824e12022-03-23 01:42:44 -07003028 return phone.isDataAllowed();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003029 } else {
3030 return false;
3031 }
3032 } finally {
3033 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07003034 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003035 }
3036
3037 public boolean handlePinMmi(String dialString) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07003038 return handlePinMmiForSubscriber(getDefaultSubscription(), dialString);
Wink Saville36469e72014-06-11 15:17:00 -07003039 }
3040
pkanwarae03a6b2016-11-06 20:37:09 -08003041 public void handleUssdRequest(int subId, String ussdRequest, ResultReceiver wrappedCallback) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003042 enforceCallPermission();
3043
3044 final long identity = Binder.clearCallingIdentity();
3045 try {
3046 if (!SubscriptionManager.isValidSubscriptionId(subId)) {
3047 return;
3048 }
3049 Pair<String, ResultReceiver> ussdObject = new Pair(ussdRequest, wrappedCallback);
3050 sendRequest(CMD_HANDLE_USSD_REQUEST, ussdObject, subId);
3051 } finally {
3052 Binder.restoreCallingIdentity(identity);
3053 }
pkanwar32d516d2016-10-14 19:37:38 -07003054 };
3055
Wink Savilleb564aae2014-10-23 10:18:09 -07003056 public boolean handlePinMmiForSubscriber(int subId, String dialString) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003057 enforceModifyPermission();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003058
3059 final long identity = Binder.clearCallingIdentity();
3060 try {
3061 if (!SubscriptionManager.isValidSubscriptionId(subId)) {
3062 return false;
3063 }
3064 return (Boolean) sendRequest(CMD_HANDLE_PIN_MMI, dialString, subId);
3065 } finally {
3066 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07003067 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003068 }
3069
Brad Ebinger4f6208e2021-03-23 21:04:45 +00003070 /**
3071 * @deprecated This method is deprecated and is only being kept due to an UnsupportedAppUsage
3072 * tag on getCallState Binder call.
3073 */
3074 @Deprecated
3075 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003076 public int getCallState() {
Brad Ebinger4f6208e2021-03-23 21:04:45 +00003077 if (CompatChanges.isChangeEnabled(
3078 TelecomManager.ENABLE_GET_CALL_STATE_PERMISSION_PROTECTION,
3079 Binder.getCallingUid())) {
3080 // Do not allow this API to be called on API version 31+, it should only be
3081 // called on old apps using this Binder call directly.
3082 throw new SecurityException("This method can only be used for applications "
3083 + "targeting API version 30 or less.");
3084 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003085 final long identity = Binder.clearCallingIdentity();
3086 try {
Brad Ebinger4f6208e2021-03-23 21:04:45 +00003087 Phone phone = getPhone(getDefaultSubscription());
3088 return phone == null ? TelephonyManager.CALL_STATE_IDLE :
3089 PhoneConstantConversions.convertCallState(phone.getState());
3090 } finally {
3091 Binder.restoreCallingIdentity(identity);
3092 }
3093 }
3094
3095 @Override
3096 public int getCallStateForSubscription(int subId, String callingPackage, String featureId) {
3097 if (CompatChanges.isChangeEnabled(
3098 TelecomManager.ENABLE_GET_CALL_STATE_PERMISSION_PROTECTION,
3099 Binder.getCallingUid())) {
3100 // Check READ_PHONE_STATE for API version 31+
3101 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(mApp, subId, callingPackage,
3102 featureId, "getCallStateForSubscription")) {
3103 throw new SecurityException("getCallState requires READ_PHONE_STATE for apps "
3104 + "targeting API level 31+.");
3105 }
3106 }
3107 final long identity = Binder.clearCallingIdentity();
3108 try {
3109 Phone phone = getPhone(subId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003110 return phone == null ? TelephonyManager.CALL_STATE_IDLE :
3111 PhoneConstantConversions.convertCallState(phone.getState());
3112 } finally {
3113 Binder.restoreCallingIdentity(identity);
3114 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003115 }
3116
Sanket Padawe356d7632015-06-22 14:03:32 -07003117 @Override
Nathan Harolde037c472019-06-26 00:41:07 +00003118 public int getDataState() {
Nathan Haroldc4689b12019-06-14 16:58:30 -07003119 return getDataStateForSubId(mSubscriptionController.getDefaultDataSubId());
3120 }
3121
3122 @Override
3123 public int getDataStateForSubId(int subId) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003124 final long identity = Binder.clearCallingIdentity();
3125 try {
Nathan Haroldc4689b12019-06-14 16:58:30 -07003126 final Phone phone = getPhone(subId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003127 if (phone != null) {
Jack Yu7968c6d2022-07-31 00:43:21 -07003128 return phone.getDataNetworkController().getInternetDataNetworkState();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003129 } else {
3130 return PhoneConstantConversions.convertDataState(
3131 PhoneConstants.DataState.DISCONNECTED);
3132 }
3133 } finally {
3134 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07003135 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003136 }
3137
Sanket Padawe356d7632015-06-22 14:03:32 -07003138 @Override
Jack Yu0eda6842022-04-18 00:34:46 -07003139 public @DataActivityType int getDataActivity() {
Nathan Haroldc4689b12019-06-14 16:58:30 -07003140 return getDataActivityForSubId(mSubscriptionController.getDefaultDataSubId());
3141 }
3142
3143 @Override
Jack Yu0eda6842022-04-18 00:34:46 -07003144 public @DataActivityType int getDataActivityForSubId(int subId) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003145 final long identity = Binder.clearCallingIdentity();
3146 try {
Nathan Haroldc4689b12019-06-14 16:58:30 -07003147 final Phone phone = getPhone(subId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003148 if (phone != null) {
Jack Yu0eda6842022-04-18 00:34:46 -07003149 return phone.getDataActivityState();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003150 } else {
3151 return TelephonyManager.DATA_ACTIVITY_NONE;
3152 }
3153 } finally {
3154 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07003155 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003156 }
3157
3158 @Override
Meng Wanga10e89e2019-12-09 13:13:01 -08003159 public CellIdentity getCellLocation(String callingPackage, String callingFeatureId) {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08003160 mApp.getSystemService(AppOpsManager.class)
Hall Liu1aa510f2017-11-22 17:40:08 -08003161 .checkPackage(Binder.getCallingUid(), callingPackage);
Hall Liuf19c44f2018-11-27 14:38:17 -08003162
3163 LocationAccessPolicy.LocationPermissionResult locationResult =
3164 LocationAccessPolicy.checkLocationPermission(mApp,
3165 new LocationAccessPolicy.LocationPermissionQuery.Builder()
3166 .setCallingPackage(callingPackage)
Philip P. Moltmann3a2772a2019-10-04 08:15:00 -07003167 .setCallingFeatureId(callingFeatureId)
Hall Liuf19c44f2018-11-27 14:38:17 -08003168 .setCallingPid(Binder.getCallingPid())
3169 .setCallingUid(Binder.getCallingUid())
3170 .setMethod("getCellLocation")
Hall Liu773ba022020-01-24 18:07:12 -08003171 .setMinSdkVersionForCoarse(Build.VERSION_CODES.BASE)
Hall Liuf19c44f2018-11-27 14:38:17 -08003172 .setMinSdkVersionForFine(Build.VERSION_CODES.Q)
3173 .build());
3174 switch (locationResult) {
3175 case DENIED_HARD:
3176 throw new SecurityException("Not allowed to access cell location");
3177 case DENIED_SOFT:
Meng Wanga10e89e2019-12-09 13:13:01 -08003178 return (getDefaultPhone().getPhoneType() == PhoneConstants.PHONE_TYPE_CDMA)
3179 ? new CellIdentityCdma() : new CellIdentityGsm();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003180 }
3181
Narayan Kamathf04b5a12018-01-09 11:47:15 +00003182 WorkSource workSource = getWorkSource(Binder.getCallingUid());
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003183 final long identity = Binder.clearCallingIdentity();
3184 try {
3185 if (DBG_LOC) log("getCellLocation: is active user");
Nathan Harold3ff88932018-08-14 10:19:49 -07003186 int subId = mSubscriptionController.getDefaultDataSubId();
Meng Wanga10e89e2019-12-09 13:13:01 -08003187 return (CellIdentity) sendRequest(CMD_GET_CELL_LOCATION, workSource, subId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003188 } finally {
3189 Binder.restoreCallingIdentity(identity);
3190 }
Svetoslav64fad262015-04-14 14:35:21 -07003191 }
3192
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003193 @Override
Jack Yueb1e7fe2020-02-22 19:38:58 -08003194 public String getNetworkCountryIsoForPhone(int phoneId) {
Jonathan Basseribf5362b2017-07-19 12:22:35 -07003195 // Reporting the correct network country is ambiguous when IWLAN could conflict with
3196 // registered cell info, so return a NULL country instead.
3197 final long identity = Binder.clearCallingIdentity();
3198 try {
Malcolm Chen3732c2b2018-07-18 20:15:24 -07003199 if (phoneId == SubscriptionManager.INVALID_PHONE_INDEX) {
3200 // Get default phone in this case.
3201 phoneId = SubscriptionManager.DEFAULT_PHONE_INDEX;
3202 }
Jonathan Basseribf5362b2017-07-19 12:22:35 -07003203 final int subId = mSubscriptionController.getSubIdUsingPhoneId(phoneId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003204 Phone phone = PhoneFactory.getPhone(phoneId);
Nathan Harold532f51c2020-04-21 19:31:10 -07003205 if (phone == null) return "";
3206 ServiceStateTracker sst = phone.getServiceStateTracker();
3207 if (sst == null) return "";
3208 LocaleTracker lt = sst.getLocaleTracker();
3209 if (lt == null) return "";
Shuo Qian9418a922021-03-09 11:21:16 -08003210 return lt.getCurrentCountry();
Jonathan Basseribf5362b2017-07-19 12:22:35 -07003211 } finally {
3212 Binder.restoreCallingIdentity(identity);
3213 }
Jonathan Basseribf5362b2017-07-19 12:22:35 -07003214 }
3215
Nathan Harold7c8d0f12020-05-28 20:40:31 -07003216 /**
3217 * This method was removed due to potential issues caused by performing partial
3218 * updates of service state, and lack of a credible use case.
3219 *
3220 * This has the ability to break the telephony implementation by disabling notification of
3221 * changes in device connectivity. DO NOT USE THIS!
3222 */
Jonathan Basseribf5362b2017-07-19 12:22:35 -07003223 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003224 public void enableLocationUpdates() {
3225 mApp.enforceCallingOrSelfPermission(
3226 android.Manifest.permission.CONTROL_LOCATION_UPDATES, null);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003227 }
3228
Nathan Harold7c8d0f12020-05-28 20:40:31 -07003229 /**
3230 * This method was removed due to potential issues caused by performing partial
3231 * updates of service state, and lack of a credible use case.
3232 *
3233 * This has the ability to break the telephony implementation by disabling notification of
3234 * changes in device connectivity. DO NOT USE THIS!
3235 */
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003236 @Override
3237 public void disableLocationUpdates() {
3238 mApp.enforceCallingOrSelfPermission(
3239 android.Manifest.permission.CONTROL_LOCATION_UPDATES, null);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003240 }
3241
3242 @Override
3243 @SuppressWarnings("unchecked")
Philip P. Moltmann3a2772a2019-10-04 08:15:00 -07003244 public List<NeighboringCellInfo> getNeighboringCellInfo(String callingPackage,
3245 String callingFeatureId) {
Nathan Haroldb55f63b2021-07-27 11:27:38 -07003246 try {
3247 mApp.getSystemService(AppOpsManager.class)
3248 .checkPackage(Binder.getCallingUid(), callingPackage);
3249 } catch (SecurityException e) {
3250 EventLog.writeEvent(0x534e4554, "190619791", Binder.getCallingUid());
3251 throw e;
3252 }
3253
Nathan Haroldf096d982020-11-18 17:18:06 -08003254 final int targetSdk = TelephonyPermissions.getTargetSdk(mApp, callingPackage);
Nathan Harolddbea45a2018-08-30 14:35:07 -07003255 if (targetSdk >= android.os.Build.VERSION_CODES.Q) {
3256 throw new SecurityException(
3257 "getNeighboringCellInfo() is unavailable to callers targeting Q+ SDK levels.");
3258 }
Nathan Haroldb4d55612018-07-20 13:13:08 -07003259
Jordan Liu1617b712019-07-10 15:06:26 -07003260 if (mAppOps.noteOp(AppOpsManager.OPSTR_NEIGHBORING_CELLS, Binder.getCallingUid(),
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003261 callingPackage) != AppOpsManager.MODE_ALLOWED) {
3262 return null;
3263 }
Svetoslav64fad262015-04-14 14:35:21 -07003264
Svetoslav Ganov4a9d4482017-06-20 19:53:35 -07003265 if (DBG_LOC) log("getNeighboringCellInfo: is active user");
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003266
Philip P. Moltmann3a2772a2019-10-04 08:15:00 -07003267 List<CellInfo> info = getAllCellInfo(callingPackage, callingFeatureId);
Nathan Haroldf180aac2018-06-01 18:43:55 -07003268 if (info == null) return null;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003269
Nathan Haroldf180aac2018-06-01 18:43:55 -07003270 List<NeighboringCellInfo> neighbors = new ArrayList<NeighboringCellInfo>();
3271 for (CellInfo ci : info) {
3272 if (ci instanceof CellInfoGsm) {
3273 neighbors.add(new NeighboringCellInfo((CellInfoGsm) ci));
3274 } else if (ci instanceof CellInfoWcdma) {
3275 neighbors.add(new NeighboringCellInfo((CellInfoWcdma) ci));
3276 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003277 }
Nathan Haroldf180aac2018-06-01 18:43:55 -07003278 return (neighbors.size()) > 0 ? neighbors : null;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003279 }
3280
Nathan Haroldfa8da0f2018-09-27 18:51:29 -07003281 private List<CellInfo> getCachedCellInfo() {
3282 List<CellInfo> cellInfos = new ArrayList<CellInfo>();
3283 for (Phone phone : PhoneFactory.getPhones()) {
3284 List<CellInfo> info = phone.getAllCellInfo();
3285 if (info != null) cellInfos.addAll(info);
3286 }
3287 return cellInfos;
3288 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003289
3290 @Override
Philip P. Moltmann3a2772a2019-10-04 08:15:00 -07003291 public List<CellInfo> getAllCellInfo(String callingPackage, String callingFeatureId) {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08003292 mApp.getSystemService(AppOpsManager.class)
Hall Liu1aa510f2017-11-22 17:40:08 -08003293 .checkPackage(Binder.getCallingUid(), callingPackage);
Hall Liuf19c44f2018-11-27 14:38:17 -08003294
3295 LocationAccessPolicy.LocationPermissionResult locationResult =
3296 LocationAccessPolicy.checkLocationPermission(mApp,
3297 new LocationAccessPolicy.LocationPermissionQuery.Builder()
3298 .setCallingPackage(callingPackage)
Philip P. Moltmann3a2772a2019-10-04 08:15:00 -07003299 .setCallingFeatureId(callingFeatureId)
Hall Liuf19c44f2018-11-27 14:38:17 -08003300 .setCallingPid(Binder.getCallingPid())
3301 .setCallingUid(Binder.getCallingUid())
3302 .setMethod("getAllCellInfo")
Nathan Harold5ae50b52019-02-20 15:46:36 -08003303 .setMinSdkVersionForCoarse(Build.VERSION_CODES.BASE)
Hall Liuf19c44f2018-11-27 14:38:17 -08003304 .setMinSdkVersionForFine(Build.VERSION_CODES.Q)
3305 .build());
3306 switch (locationResult) {
3307 case DENIED_HARD:
3308 throw new SecurityException("Not allowed to access cell info");
3309 case DENIED_SOFT:
3310 return new ArrayList<>();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003311 }
3312
Nathan Haroldf096d982020-11-18 17:18:06 -08003313 final int targetSdk = TelephonyPermissions.getTargetSdk(mApp, callingPackage);
Nathan Haroldfa8da0f2018-09-27 18:51:29 -07003314 if (targetSdk >= android.os.Build.VERSION_CODES.Q) {
3315 return getCachedCellInfo();
3316 }
3317
Svetoslav Ganov4a9d4482017-06-20 19:53:35 -07003318 if (DBG_LOC) log("getAllCellInfo: is active user");
Narayan Kamathf04b5a12018-01-09 11:47:15 +00003319 WorkSource workSource = getWorkSource(Binder.getCallingUid());
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003320 final long identity = Binder.clearCallingIdentity();
3321 try {
3322 List<CellInfo> cellInfos = new ArrayList<CellInfo>();
3323 for (Phone phone : PhoneFactory.getPhones()) {
Nathan Harold3ff88932018-08-14 10:19:49 -07003324 final List<CellInfo> info = (List<CellInfo>) sendRequest(
Nathan Harold92bed182018-10-12 18:16:49 -07003325 CMD_GET_ALL_CELL_INFO, null, phone, workSource);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003326 if (info != null) cellInfos.addAll(info);
3327 }
3328 return cellInfos;
3329 } finally {
3330 Binder.restoreCallingIdentity(identity);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003331 }
3332 }
3333
Sailesh Nepalbd76e4e2013-10-27 13:59:44 -07003334 @Override
Philip P. Moltmann3a2772a2019-10-04 08:15:00 -07003335 public void requestCellInfoUpdate(int subId, ICellInfoCallback cb, String callingPackage,
3336 String callingFeatureId) {
3337 requestCellInfoUpdateInternal(subId, cb, callingPackage, callingFeatureId,
3338 getWorkSource(Binder.getCallingUid()));
Nathan Haroldfa8da0f2018-09-27 18:51:29 -07003339 }
3340
3341 @Override
Philip P. Moltmann3a2772a2019-10-04 08:15:00 -07003342 public void requestCellInfoUpdateWithWorkSource(int subId, ICellInfoCallback cb,
3343 String callingPackage, String callingFeatureId, WorkSource workSource) {
Nathan Haroldfa8da0f2018-09-27 18:51:29 -07003344 enforceModifyPermission();
Philip P. Moltmann3a2772a2019-10-04 08:15:00 -07003345 requestCellInfoUpdateInternal(subId, cb, callingPackage, callingFeatureId, workSource);
Nathan Haroldfa8da0f2018-09-27 18:51:29 -07003346 }
3347
Philip P. Moltmann3a2772a2019-10-04 08:15:00 -07003348 private void requestCellInfoUpdateInternal(int subId, ICellInfoCallback cb,
3349 String callingPackage, String callingFeatureId, WorkSource workSource) {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08003350 mApp.getSystemService(AppOpsManager.class)
Nathan Haroldfa8da0f2018-09-27 18:51:29 -07003351 .checkPackage(Binder.getCallingUid(), callingPackage);
Hall Liuf19c44f2018-11-27 14:38:17 -08003352
3353 LocationAccessPolicy.LocationPermissionResult locationResult =
3354 LocationAccessPolicy.checkLocationPermission(mApp,
3355 new LocationAccessPolicy.LocationPermissionQuery.Builder()
3356 .setCallingPackage(callingPackage)
Philip P. Moltmann3a2772a2019-10-04 08:15:00 -07003357 .setCallingFeatureId(callingFeatureId)
Hall Liuf19c44f2018-11-27 14:38:17 -08003358 .setCallingPid(Binder.getCallingPid())
3359 .setCallingUid(Binder.getCallingUid())
3360 .setMethod("requestCellInfoUpdate")
Hall Liud60acc92020-05-21 17:09:35 -07003361 .setMinSdkVersionForCoarse(Build.VERSION_CODES.BASE)
3362 .setMinSdkVersionForFine(Build.VERSION_CODES.BASE)
Hall Liuf19c44f2018-11-27 14:38:17 -08003363 .build());
3364 switch (locationResult) {
3365 case DENIED_HARD:
Nathan Haroldf096d982020-11-18 17:18:06 -08003366 if (TelephonyPermissions
3367 .getTargetSdk(mApp, callingPackage) < Build.VERSION_CODES.Q) {
Hall Liud60acc92020-05-21 17:09:35 -07003368 // Safetynet logging for b/154934934
3369 EventLog.writeEvent(0x534e4554, "154934934", Binder.getCallingUid());
3370 }
Hall Liuf19c44f2018-11-27 14:38:17 -08003371 throw new SecurityException("Not allowed to access cell info");
3372 case DENIED_SOFT:
Nathan Haroldf096d982020-11-18 17:18:06 -08003373 if (TelephonyPermissions
3374 .getTargetSdk(mApp, callingPackage) < Build.VERSION_CODES.Q) {
Hall Liud60acc92020-05-21 17:09:35 -07003375 // Safetynet logging for b/154934934
3376 EventLog.writeEvent(0x534e4554, "154934934", Binder.getCallingUid());
3377 }
Nathan Harold5320c422019-05-09 10:26:08 -07003378 try {
3379 cb.onCellInfo(new ArrayList<CellInfo>());
3380 } catch (RemoteException re) {
3381 // Drop without consequences
3382 }
Hall Liuf19c44f2018-11-27 14:38:17 -08003383 return;
Nathan Haroldfa8da0f2018-09-27 18:51:29 -07003384 }
3385
Nathan Harolda939a962019-05-09 10:13:47 -07003386
3387 final Phone phone = getPhoneFromSubId(subId);
Nathan Haroldfa8da0f2018-09-27 18:51:29 -07003388 if (phone == null) throw new IllegalArgumentException("Invalid Subscription Id: " + subId);
3389
3390 sendRequestAsync(CMD_REQUEST_CELL_INFO_UPDATE, cb, phone, workSource);
3391 }
3392
3393 @Override
Aishwarya Mallampati0603fb12022-08-24 21:16:56 +00003394 public void setCellInfoListRate(int rateInMillis, int subId) {
Jack Yua8d8cb82017-01-16 10:15:34 -08003395 enforceModifyPermission();
Narayan Kamathf04b5a12018-01-09 11:47:15 +00003396 WorkSource workSource = getWorkSource(Binder.getCallingUid());
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003397
3398 final long identity = Binder.clearCallingIdentity();
3399 try {
Aishwarya Mallampati0603fb12022-08-24 21:16:56 +00003400 Phone phone = getPhone(subId);
3401 if (phone == null) {
3402 getDefaultPhone().setCellInfoListRate(rateInMillis, workSource);
3403 } else {
3404 phone.setCellInfoListRate(rateInMillis, workSource);
3405 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003406 } finally {
3407 Binder.restoreCallingIdentity(identity);
3408 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003409 }
3410
Shishir Agrawala9f32182016-04-12 12:00:16 -07003411 @Override
Philip P. Moltmann700a9592019-10-03 11:53:50 -07003412 public String getImeiForSlot(int slotIndex, String callingPackage, String callingFeatureId) {
Jeff Davidson913390f2018-02-23 17:11:49 -08003413 Phone phone = PhoneFactory.getPhone(slotIndex);
3414 if (phone == null) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08003415 return null;
3416 }
Jeff Davidson913390f2018-02-23 17:11:49 -08003417 int subId = phone.getSubId();
Grace Jia0ddb3612021-04-22 13:35:26 -07003418 enforceCallingPackage(callingPackage, Binder.getCallingUid(), "getImeiForSlot");
Michael Groover70af6dc2018-10-01 16:23:15 -07003419 if (!TelephonyPermissions.checkCallingOrSelfReadDeviceIdentifiers(mApp, subId,
Philip P. Moltmann700a9592019-10-03 11:53:50 -07003420 callingPackage, callingFeatureId, "getImeiForSlot")) {
Jeff Davidson913390f2018-02-23 17:11:49 -08003421 return null;
3422 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003423
3424 final long identity = Binder.clearCallingIdentity();
3425 try {
3426 return phone.getImei();
3427 } finally {
3428 Binder.restoreCallingIdentity(identity);
3429 }
Shishir Agrawala9f32182016-04-12 12:00:16 -07003430 }
3431
3432 @Override
David Kelly5e06a7f2018-03-12 14:10:59 +00003433 public String getTypeAllocationCodeForSlot(int slotIndex) {
3434 Phone phone = PhoneFactory.getPhone(slotIndex);
3435 String tac = null;
3436 if (phone != null) {
3437 String imei = phone.getImei();
Vala Zadehab005552021-09-21 15:54:29 -07003438 try {
3439 tac = imei == null ? null : imei.substring(0, TYPE_ALLOCATION_CODE_LENGTH);
3440 } catch (IndexOutOfBoundsException e) {
3441 Log.e(LOG_TAG, "IMEI length shorter than upper index.");
3442 return null;
3443 }
David Kelly5e06a7f2018-03-12 14:10:59 +00003444 }
3445 return tac;
3446 }
3447
3448 @Override
Philip P. Moltmann700a9592019-10-03 11:53:50 -07003449 public String getMeidForSlot(int slotIndex, String callingPackage, String callingFeatureId) {
Shuo Qian13d89152021-05-10 23:58:11 -07003450 try {
3451 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
3452 } catch (SecurityException se) {
3453 EventLog.writeEvent(0x534e4554, "186530496", Binder.getCallingUid());
3454 throw new SecurityException("Package " + callingPackage + " does not belong to "
3455 + Binder.getCallingUid());
3456 }
Jeff Davidson913390f2018-02-23 17:11:49 -08003457 Phone phone = PhoneFactory.getPhone(slotIndex);
3458 if (phone == null) {
Jack Yu2af8d712017-03-15 17:14:14 -07003459 return null;
3460 }
Nazanin Bakhshif782e562018-12-11 15:15:39 -08003461
Jeff Davidson913390f2018-02-23 17:11:49 -08003462 int subId = phone.getSubId();
Michael Groover70af6dc2018-10-01 16:23:15 -07003463 if (!TelephonyPermissions.checkCallingOrSelfReadDeviceIdentifiers(mApp, subId,
Philip P. Moltmann700a9592019-10-03 11:53:50 -07003464 callingPackage, callingFeatureId, "getMeidForSlot")) {
Jeff Davidson913390f2018-02-23 17:11:49 -08003465 return null;
3466 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003467
3468 final long identity = Binder.clearCallingIdentity();
3469 try {
3470 return phone.getMeid();
3471 } finally {
3472 Binder.restoreCallingIdentity(identity);
3473 }
Jack Yu2af8d712017-03-15 17:14:14 -07003474 }
3475
3476 @Override
David Kelly5e06a7f2018-03-12 14:10:59 +00003477 public String getManufacturerCodeForSlot(int slotIndex) {
3478 Phone phone = PhoneFactory.getPhone(slotIndex);
3479 String manufacturerCode = null;
3480 if (phone != null) {
3481 String meid = phone.getMeid();
Vala Zadehab005552021-09-21 15:54:29 -07003482 try {
3483 manufacturerCode =
3484 meid == null ? null : meid.substring(0, MANUFACTURER_CODE_LENGTH);
3485 } catch (IndexOutOfBoundsException e) {
3486 Log.e(LOG_TAG, "MEID length shorter than upper index.");
3487 return null;
3488 }
David Kelly5e06a7f2018-03-12 14:10:59 +00003489 }
3490 return manufacturerCode;
3491 }
3492
3493 @Override
Philip P. Moltmann700a9592019-10-03 11:53:50 -07003494 public String getDeviceSoftwareVersionForSlot(int slotIndex, String callingPackage,
3495 String callingFeatureId) {
Jeff Davidson913390f2018-02-23 17:11:49 -08003496 Phone phone = PhoneFactory.getPhone(slotIndex);
3497 if (phone == null) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08003498 return null;
3499 }
Jeff Davidson913390f2018-02-23 17:11:49 -08003500 int subId = phone.getSubId();
3501 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
Philip P. Moltmann700a9592019-10-03 11:53:50 -07003502 mApp, subId, callingPackage, callingFeatureId,
3503 "getDeviceSoftwareVersionForSlot")) {
Jeff Davidson913390f2018-02-23 17:11:49 -08003504 return null;
3505 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003506
3507 final long identity = Binder.clearCallingIdentity();
3508 try {
3509 return phone.getDeviceSvn();
3510 } finally {
3511 Binder.restoreCallingIdentity(identity);
3512 }
Shishir Agrawala9f32182016-04-12 12:00:16 -07003513 }
3514
fionaxu43304da2017-11-27 22:51:16 -08003515 @Override
3516 public int getSubscriptionCarrierId(int subId) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003517 final long identity = Binder.clearCallingIdentity();
3518 try {
3519 final Phone phone = getPhone(subId);
3520 return phone == null ? TelephonyManager.UNKNOWN_CARRIER_ID : phone.getCarrierId();
3521 } finally {
3522 Binder.restoreCallingIdentity(identity);
3523 }
fionaxu43304da2017-11-27 22:51:16 -08003524 }
3525
3526 @Override
3527 public String getSubscriptionCarrierName(int subId) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003528 final long identity = Binder.clearCallingIdentity();
3529 try {
3530 final Phone phone = getPhone(subId);
3531 return phone == null ? null : phone.getCarrierName();
3532 } finally {
3533 Binder.restoreCallingIdentity(identity);
3534 }
fionaxu43304da2017-11-27 22:51:16 -08003535 }
3536
calvinpanffe225e2018-11-01 19:43:06 +08003537 @Override
chen xu0026ca62019-03-06 15:28:50 -08003538 public int getSubscriptionSpecificCarrierId(int subId) {
chen xu25637222018-11-04 17:17:00 -08003539 final long identity = Binder.clearCallingIdentity();
3540 try {
3541 final Phone phone = getPhone(subId);
3542 return phone == null ? TelephonyManager.UNKNOWN_CARRIER_ID
chen xu0026ca62019-03-06 15:28:50 -08003543 : phone.getSpecificCarrierId();
chen xu25637222018-11-04 17:17:00 -08003544 } finally {
3545 Binder.restoreCallingIdentity(identity);
3546 }
3547 }
3548
3549 @Override
chen xu0026ca62019-03-06 15:28:50 -08003550 public String getSubscriptionSpecificCarrierName(int subId) {
chen xu25637222018-11-04 17:17:00 -08003551 final long identity = Binder.clearCallingIdentity();
3552 try {
3553 final Phone phone = getPhone(subId);
chen xu0026ca62019-03-06 15:28:50 -08003554 return phone == null ? null : phone.getSpecificCarrierName();
chen xu25637222018-11-04 17:17:00 -08003555 } finally {
3556 Binder.restoreCallingIdentity(identity);
3557 }
3558 }
3559
chen xu651eec72018-11-11 19:03:44 -08003560 @Override
chen xu864e11c2018-12-06 22:10:03 -08003561 public int getCarrierIdFromMccMnc(int slotIndex, String mccmnc, boolean isSubscriptionMccMnc) {
3562 if (!isSubscriptionMccMnc) {
3563 enforceReadPrivilegedPermission("getCarrierIdFromMccMnc");
3564 }
chen xu651eec72018-11-11 19:03:44 -08003565 final Phone phone = PhoneFactory.getPhone(slotIndex);
3566 if (phone == null) {
3567 return TelephonyManager.UNKNOWN_CARRIER_ID;
3568 }
3569 final long identity = Binder.clearCallingIdentity();
3570 try {
3571 return CarrierResolver.getCarrierIdFromMccMnc(phone.getContext(), mccmnc);
3572 } finally {
3573 Binder.restoreCallingIdentity(identity);
3574 }
3575 }
3576
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003577 //
3578 // Internal helper methods.
3579 //
3580
Sanket Padaweee13a9b2016-03-08 17:30:28 -08003581 /**
Grace Jia0ddb3612021-04-22 13:35:26 -07003582 * Make sure the caller is the calling package itself
3583 *
3584 * @throws SecurityException if the caller is not the calling package
3585 */
3586 private void enforceCallingPackage(String callingPackage, int callingUid, String message) {
3587 int packageUid = -1;
Grace Jiadbefca02021-04-26 15:13:31 -07003588 PackageManager pm = mApp.getBaseContext().createContextAsUser(
3589 UserHandle.getUserHandleForUid(callingUid), 0).getPackageManager();
Grace Jia0ddb3612021-04-22 13:35:26 -07003590 try {
Grace Jiadbefca02021-04-26 15:13:31 -07003591 packageUid = pm.getPackageUid(callingPackage, 0);
Grace Jia0ddb3612021-04-22 13:35:26 -07003592 } catch (PackageManager.NameNotFoundException e) {
3593 // packageUid is -1
3594 }
3595 if (packageUid != callingUid) {
3596 throw new SecurityException(message + ": Package " + callingPackage
3597 + " does not belong to " + callingUid);
3598 }
3599 }
3600
3601 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003602 * Make sure the caller has the MODIFY_PHONE_STATE permission.
3603 *
3604 * @throws SecurityException if the caller does not have the required permission
3605 */
3606 private void enforceModifyPermission() {
3607 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.MODIFY_PHONE_STATE, null);
3608 }
3609
Shuo Qian3b6ee772019-11-13 17:43:31 -08003610 private void enforceActiveEmergencySessionPermission() {
3611 mApp.enforceCallingOrSelfPermission(
3612 android.Manifest.permission.READ_ACTIVE_EMERGENCY_SESSION, null);
3613 }
3614
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003615 /**
3616 * Make sure the caller has the CALL_PHONE permission.
3617 *
3618 * @throws SecurityException if the caller does not have the required permission
3619 */
3620 private void enforceCallPermission() {
3621 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.CALL_PHONE, null);
3622 }
3623
paulhu5a773602019-08-23 19:17:33 +08003624 private void enforceSettingsPermission() {
3625 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.NETWORK_SETTINGS, null);
Stuart Scott8eef64f2015-04-08 15:13:54 -07003626 }
3627
Michele Berionne5e411512020-11-13 02:36:59 +00003628 private void enforceRebootPermission() {
3629 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.REBOOT, null);
3630 }
3631
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003632 private String createTelUrl(String number) {
3633 if (TextUtils.isEmpty(number)) {
3634 return null;
3635 }
3636
Jake Hambye994d462014-02-03 13:10:13 -08003637 return "tel:" + number;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003638 }
3639
Ihab Awadf9e92732013-12-05 18:02:52 -08003640 private static void log(String msg) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003641 Log.d(LOG_TAG, "[PhoneIntfMgr] " + msg);
3642 }
3643
Naveen Kalla1fd79bd2014-08-08 00:48:59 -07003644 private static void logv(String msg) {
3645 Log.v(LOG_TAG, "[PhoneIntfMgr] " + msg);
3646 }
3647
Ihab Awadf9e92732013-12-05 18:02:52 -08003648 private static void loge(String msg) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003649 Log.e(LOG_TAG, "[PhoneIntfMgr] " + msg);
3650 }
3651
Robert Greenwalt36b23af2015-07-06 17:59:14 -07003652 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003653 public int getActivePhoneType() {
Shishir Agrawala9f32182016-04-12 12:00:16 -07003654 return getActivePhoneTypeForSlot(getSlotForDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07003655 }
3656
Sanket Padawe356d7632015-06-22 14:03:32 -07003657 @Override
Sanket Padawe13bac7b2017-03-20 15:04:47 -07003658 public int getActivePhoneTypeForSlot(int slotIndex) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003659 final long identity = Binder.clearCallingIdentity();
3660 try {
3661 final Phone phone = PhoneFactory.getPhone(slotIndex);
3662 if (phone == null) {
3663 return PhoneConstants.PHONE_TYPE_NONE;
3664 } else {
3665 return phone.getPhoneType();
3666 }
3667 } finally {
3668 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07003669 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003670 }
3671
3672 /**
3673 * Returns the CDMA ERI icon index to display
3674 */
Robert Greenwalt36b23af2015-07-06 17:59:14 -07003675 @Override
Philip P. Moltmann700a9592019-10-03 11:53:50 -07003676 public int getCdmaEriIconIndex(String callingPackage, String callingFeatureId) {
3677 return getCdmaEriIconIndexForSubscriber(getDefaultSubscription(), callingPackage,
3678 callingFeatureId);
Wink Saville36469e72014-06-11 15:17:00 -07003679 }
3680
Sanket Padawe356d7632015-06-22 14:03:32 -07003681 @Override
Philip P. Moltmann700a9592019-10-03 11:53:50 -07003682 public int getCdmaEriIconIndexForSubscriber(int subId, String callingPackage,
3683 String callingFeatureId) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08003684 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
Philip P. Moltmann700a9592019-10-03 11:53:50 -07003685 mApp, subId, callingPackage, callingFeatureId,
3686 "getCdmaEriIconIndexForSubscriber")) {
Robert Greenwalt36b23af2015-07-06 17:59:14 -07003687 return -1;
3688 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003689
3690 final long identity = Binder.clearCallingIdentity();
3691 try {
3692 final Phone phone = getPhone(subId);
3693 if (phone != null) {
3694 return phone.getCdmaEriIconIndex();
3695 } else {
3696 return -1;
3697 }
3698 } finally {
3699 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07003700 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003701 }
3702
3703 /**
3704 * Returns the CDMA ERI icon mode,
3705 * 0 - ON
3706 * 1 - FLASHING
3707 */
Robert Greenwalt36b23af2015-07-06 17:59:14 -07003708 @Override
Philip P. Moltmann700a9592019-10-03 11:53:50 -07003709 public int getCdmaEriIconMode(String callingPackage, String callingFeatureId) {
3710 return getCdmaEriIconModeForSubscriber(getDefaultSubscription(), callingPackage,
3711 callingFeatureId);
Wink Saville36469e72014-06-11 15:17:00 -07003712 }
3713
Sanket Padawe356d7632015-06-22 14:03:32 -07003714 @Override
Philip P. Moltmann700a9592019-10-03 11:53:50 -07003715 public int getCdmaEriIconModeForSubscriber(int subId, String callingPackage,
3716 String callingFeatureId) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08003717 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
Philip P. Moltmann700a9592019-10-03 11:53:50 -07003718 mApp, subId, callingPackage, callingFeatureId,
3719 "getCdmaEriIconModeForSubscriber")) {
Robert Greenwalt36b23af2015-07-06 17:59:14 -07003720 return -1;
3721 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003722
3723 final long identity = Binder.clearCallingIdentity();
3724 try {
3725 final Phone phone = getPhone(subId);
3726 if (phone != null) {
3727 return phone.getCdmaEriIconMode();
3728 } else {
3729 return -1;
3730 }
3731 } finally {
3732 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07003733 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003734 }
3735
3736 /**
3737 * Returns the CDMA ERI text,
3738 */
Robert Greenwalt36b23af2015-07-06 17:59:14 -07003739 @Override
Philip P. Moltmann700a9592019-10-03 11:53:50 -07003740 public String getCdmaEriText(String callingPackage, String callingFeatureId) {
3741 return getCdmaEriTextForSubscriber(getDefaultSubscription(), callingPackage,
3742 callingFeatureId);
Wink Saville36469e72014-06-11 15:17:00 -07003743 }
3744
Sanket Padawe356d7632015-06-22 14:03:32 -07003745 @Override
Philip P. Moltmann700a9592019-10-03 11:53:50 -07003746 public String getCdmaEriTextForSubscriber(int subId, String callingPackage,
3747 String callingFeatureId) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08003748 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
Philip P. Moltmann700a9592019-10-03 11:53:50 -07003749 mApp, subId, callingPackage, callingFeatureId,
3750 "getCdmaEriIconTextForSubscriber")) {
Robert Greenwalt36b23af2015-07-06 17:59:14 -07003751 return null;
3752 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003753
3754 final long identity = Binder.clearCallingIdentity();
3755 try {
3756 final Phone phone = getPhone(subId);
3757 if (phone != null) {
3758 return phone.getCdmaEriText();
3759 } else {
3760 return null;
3761 }
3762 } finally {
3763 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07003764 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003765 }
3766
3767 /**
Junda Liuca05d5d2014-08-14 22:36:34 -07003768 * Returns the CDMA MDN.
3769 */
Sanket Padawe356d7632015-06-22 14:03:32 -07003770 @Override
Wink Savilleb564aae2014-10-23 10:18:09 -07003771 public String getCdmaMdn(int subId) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08003772 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
3773 mApp, subId, "getCdmaMdn");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003774
3775 final long identity = Binder.clearCallingIdentity();
3776 try {
3777 final Phone phone = getPhone(subId);
Nazanin Bakhshif782e562018-12-11 15:15:39 -08003778 if (phone != null && phone.getPhoneType() == PhoneConstants.PHONE_TYPE_CDMA) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003779 return phone.getLine1Number();
3780 } else {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08003781 loge("getCdmaMdn: no phone found. Invalid subId: " + subId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003782 return null;
3783 }
3784 } finally {
3785 Binder.restoreCallingIdentity(identity);
Junda Liuca05d5d2014-08-14 22:36:34 -07003786 }
3787 }
3788
3789 /**
3790 * Returns the CDMA MIN.
3791 */
Sanket Padawe356d7632015-06-22 14:03:32 -07003792 @Override
Wink Savilleb564aae2014-10-23 10:18:09 -07003793 public String getCdmaMin(int subId) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08003794 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
3795 mApp, subId, "getCdmaMin");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003796
3797 final long identity = Binder.clearCallingIdentity();
3798 try {
3799 final Phone phone = getPhone(subId);
3800 if (phone != null && phone.getPhoneType() == PhoneConstants.PHONE_TYPE_CDMA) {
3801 return phone.getCdmaMin();
3802 } else {
3803 return null;
3804 }
3805 } finally {
3806 Binder.restoreCallingIdentity(identity);
Junda Liuca05d5d2014-08-14 22:36:34 -07003807 }
3808 }
3809
Hall Liud892bec2018-11-30 14:51:45 -08003810 @Override
3811 public void requestNumberVerification(PhoneNumberRange range, long timeoutMillis,
3812 INumberVerificationCallback callback, String callingPackage) {
3813 if (mApp.checkCallingOrSelfPermission(android.Manifest.permission.MODIFY_PHONE_STATE)
3814 != PERMISSION_GRANTED) {
3815 throw new SecurityException("Caller must hold the MODIFY_PHONE_STATE permission");
3816 }
3817 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
3818
3819 String authorizedPackage = NumberVerificationManager.getAuthorizedPackage(mApp);
3820 if (!TextUtils.equals(callingPackage, authorizedPackage)) {
Hall Liub9d8feb2021-01-13 10:28:04 -08003821 throw new SecurityException("Calling package must be configured in the device config: "
3822 + "calling package: " + callingPackage
3823 + ", configured package: " + authorizedPackage);
Hall Liud892bec2018-11-30 14:51:45 -08003824 }
3825
3826 if (range == null) {
3827 throw new NullPointerException("Range must be non-null");
3828 }
3829
3830 timeoutMillis = Math.min(timeoutMillis,
Hall Liubd069e32019-02-28 18:56:30 -08003831 TelephonyManager.getMaxNumberVerificationTimeoutMillis());
Hall Liud892bec2018-11-30 14:51:45 -08003832
3833 NumberVerificationManager.getInstance().requestVerification(range, callback, timeoutMillis);
3834 }
3835
Junda Liuca05d5d2014-08-14 22:36:34 -07003836 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003837 * Returns true if CDMA provisioning needs to run.
3838 */
3839 public boolean needsOtaServiceProvisioning() {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003840 final long identity = Binder.clearCallingIdentity();
3841 try {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08003842 return getDefaultPhone().needsOtaServiceProvisioning();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003843 } finally {
3844 Binder.restoreCallingIdentity(identity);
3845 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003846 }
3847
3848 /**
Shishir Agrawal76d5da92014-11-09 16:17:25 -08003849 * Sets the voice mail number of a given subId.
3850 */
3851 @Override
3852 public boolean setVoiceMailNumber(int subId, String alphaTag, String number) {
Shuo Qian2c0ae432019-12-05 11:40:37 -08003853 TelephonyPermissions.enforceCallingOrSelfCarrierPrivilege(
3854 mApp, subId, "setVoiceMailNumber");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003855
3856 final long identity = Binder.clearCallingIdentity();
3857 try {
3858 Boolean success = (Boolean) sendRequest(CMD_SET_VOICEMAIL_NUMBER,
3859 new Pair<String, String>(alphaTag, number), new Integer(subId));
3860 return success;
3861 } finally {
3862 Binder.restoreCallingIdentity(identity);
3863 }
Shishir Agrawal76d5da92014-11-09 16:17:25 -08003864 }
3865
Ta-wei Yen87c49842016-05-13 21:19:52 -07003866 @Override
Ta-wei Yenc9df0432017-04-17 17:09:07 -07003867 public Bundle getVisualVoicemailSettings(String callingPackage, int subId) {
3868 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
Tyler Gunn5ddfdc92019-10-31 13:08:23 -07003869 TelecomManager tm = mApp.getSystemService(TelecomManager.class);
3870 String systemDialer = tm.getSystemDialerPackage();
Ta-wei Yenc9df0432017-04-17 17:09:07 -07003871 if (!TextUtils.equals(callingPackage, systemDialer)) {
3872 throw new SecurityException("caller must be system dialer");
3873 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003874
3875 final long identity = Binder.clearCallingIdentity();
3876 try {
3877 PhoneAccountHandle phoneAccountHandle = PhoneAccountHandleConverter.fromSubId(subId);
3878 if (phoneAccountHandle == null) {
3879 return null;
3880 }
Nazanin Bakhshif782e562018-12-11 15:15:39 -08003881 return VisualVoicemailSettingsUtil.dump(mApp, phoneAccountHandle);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003882 } finally {
3883 Binder.restoreCallingIdentity(identity);
Ta-wei Yenc9df0432017-04-17 17:09:07 -07003884 }
Ta-wei Yenc9df0432017-04-17 17:09:07 -07003885 }
3886
3887 @Override
Philip P. Moltmann700a9592019-10-03 11:53:50 -07003888 public String getVisualVoicemailPackageName(String callingPackage, String callingFeatureId,
3889 int subId) {
Ta-wei Yendca928f2017-01-10 16:17:08 -08003890 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
Jeff Davidson7e17e312018-02-13 18:17:36 -08003891 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
Philip P. Moltmann700a9592019-10-03 11:53:50 -07003892 mApp, subId, callingPackage, callingFeatureId,
3893 "getVisualVoicemailPackageName")) {
Ta-wei Yendca928f2017-01-10 16:17:08 -08003894 return null;
3895 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003896
Jeff Davidsona8e4e242018-03-15 17:16:18 -07003897 final long identity = Binder.clearCallingIdentity();
3898 try {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08003899 return RemoteVvmTaskManager.getRemotePackage(mApp, subId).getPackageName();
Jeff Davidsona8e4e242018-03-15 17:16:18 -07003900 } finally {
3901 Binder.restoreCallingIdentity(identity);
3902 }
Ta-wei Yendca928f2017-01-10 16:17:08 -08003903 }
3904
3905 @Override
Ta-wei Yenb6929602016-05-24 15:48:27 -07003906 public void enableVisualVoicemailSmsFilter(String callingPackage, int subId,
3907 VisualVoicemailSmsFilterSettings settings) {
3908 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003909
3910 final long identity = Binder.clearCallingIdentity();
3911 try {
3912 VisualVoicemailSmsFilterConfig.enableVisualVoicemailSmsFilter(
Nazanin Bakhshif782e562018-12-11 15:15:39 -08003913 mApp, callingPackage, subId, settings);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003914 } finally {
3915 Binder.restoreCallingIdentity(identity);
3916 }
Ta-wei Yen87c49842016-05-13 21:19:52 -07003917 }
3918
3919 @Override
Ta-wei Yenb6929602016-05-24 15:48:27 -07003920 public void disableVisualVoicemailSmsFilter(String callingPackage, int subId) {
3921 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003922
3923 final long identity = Binder.clearCallingIdentity();
3924 try {
3925 VisualVoicemailSmsFilterConfig.disableVisualVoicemailSmsFilter(
Nazanin Bakhshif782e562018-12-11 15:15:39 -08003926 mApp, callingPackage, subId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003927 } finally {
3928 Binder.restoreCallingIdentity(identity);
3929 }
Ta-wei Yen87c49842016-05-13 21:19:52 -07003930 }
3931
3932 @Override
Ta-wei Yenb6929602016-05-24 15:48:27 -07003933 public VisualVoicemailSmsFilterSettings getVisualVoicemailSmsFilterSettings(
3934 String callingPackage, int subId) {
3935 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003936
3937 final long identity = Binder.clearCallingIdentity();
3938 try {
3939 return VisualVoicemailSmsFilterConfig.getVisualVoicemailSmsFilterSettings(
Nazanin Bakhshif782e562018-12-11 15:15:39 -08003940 mApp, callingPackage, subId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003941 } finally {
3942 Binder.restoreCallingIdentity(identity);
3943 }
Ta-wei Yen87c49842016-05-13 21:19:52 -07003944 }
3945
3946 @Override
Ta-wei Yen30a69c82016-12-27 14:52:32 -08003947 public VisualVoicemailSmsFilterSettings getActiveVisualVoicemailSmsFilterSettings(int subId) {
Brad Ebinger35c841c2018-10-01 10:40:55 -07003948 enforceReadPrivilegedPermission("getActiveVisualVoicemailSmsFilterSettings");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003949
3950 final long identity = Binder.clearCallingIdentity();
3951 try {
3952 return VisualVoicemailSmsFilterConfig.getActiveVisualVoicemailSmsFilterSettings(
Nazanin Bakhshif782e562018-12-11 15:15:39 -08003953 mApp, subId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003954 } finally {
3955 Binder.restoreCallingIdentity(identity);
3956 }
Ta-wei Yen30a69c82016-12-27 14:52:32 -08003957 }
3958
3959 @Override
Philip P. Moltmann2f6f8ce2020-03-18 18:17:02 -07003960 public void sendVisualVoicemailSmsForSubscriber(String callingPackage,
3961 String callingAttributionTag, int subId, String number, int port, String text,
3962 PendingIntent sentIntent) {
Ta-wei Yen30a69c82016-12-27 14:52:32 -08003963 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
Ta-wei Yen527a9c02017-01-06 15:29:25 -08003964 enforceVisualVoicemailPackage(callingPackage, subId);
Ta-wei Yen30a69c82016-12-27 14:52:32 -08003965 enforceSendSmsPermission();
Amit Mahajandccb3f12019-05-13 13:48:32 -07003966 SmsController smsController = PhoneFactory.getSmsController();
Philip P. Moltmann2f6f8ce2020-03-18 18:17:02 -07003967 smsController.sendVisualVoicemailSmsForSubscriber(callingPackage, callingAttributionTag,
3968 subId, number, port, text, sentIntent);
Ta-wei Yen87c49842016-05-13 21:19:52 -07003969 }
Amit Mahajandccb3f12019-05-13 13:48:32 -07003970
Shishir Agrawal76d5da92014-11-09 16:17:25 -08003971 /**
fionaxu0152e512016-11-14 13:36:14 -08003972 * Sets the voice activation state of a given subId.
3973 */
3974 @Override
3975 public void setVoiceActivationState(int subId, int activationState) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08003976 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
3977 mApp, subId, "setVoiceActivationState");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003978
3979 final long identity = Binder.clearCallingIdentity();
3980 try {
3981 final Phone phone = getPhone(subId);
3982 if (phone != null) {
3983 phone.setVoiceActivationState(activationState);
3984 } else {
3985 loge("setVoiceActivationState fails with invalid subId: " + subId);
3986 }
3987 } finally {
3988 Binder.restoreCallingIdentity(identity);
fionaxu0152e512016-11-14 13:36:14 -08003989 }
3990 }
3991
3992 /**
3993 * Sets the data activation state of a given subId.
3994 */
3995 @Override
3996 public void setDataActivationState(int subId, int activationState) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08003997 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
3998 mApp, subId, "setDataActivationState");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003999
4000 final long identity = Binder.clearCallingIdentity();
4001 try {
4002 final Phone phone = getPhone(subId);
4003 if (phone != null) {
4004 phone.setDataActivationState(activationState);
4005 } else {
Taesu Leef8fbed92019-10-07 18:47:02 +09004006 loge("setDataActivationState fails with invalid subId: " + subId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004007 }
4008 } finally {
4009 Binder.restoreCallingIdentity(identity);
fionaxu0152e512016-11-14 13:36:14 -08004010 }
4011 }
4012
4013 /**
4014 * Returns the voice activation state of a given subId.
4015 */
4016 @Override
4017 public int getVoiceActivationState(int subId, String callingPackage) {
Brad Ebinger35c841c2018-10-01 10:40:55 -07004018 enforceReadPrivilegedPermission("getVoiceActivationState");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004019
fionaxu0152e512016-11-14 13:36:14 -08004020 final Phone phone = getPhone(subId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004021 final long identity = Binder.clearCallingIdentity();
4022 try {
4023 if (phone != null) {
4024 return phone.getVoiceActivationState();
4025 } else {
4026 return TelephonyManager.SIM_ACTIVATION_STATE_UNKNOWN;
4027 }
4028 } finally {
4029 Binder.restoreCallingIdentity(identity);
fionaxu0152e512016-11-14 13:36:14 -08004030 }
4031 }
4032
4033 /**
4034 * Returns the data activation state of a given subId.
4035 */
4036 @Override
4037 public int getDataActivationState(int subId, String callingPackage) {
Brad Ebinger35c841c2018-10-01 10:40:55 -07004038 enforceReadPrivilegedPermission("getDataActivationState");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004039
fionaxu0152e512016-11-14 13:36:14 -08004040 final Phone phone = getPhone(subId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004041 final long identity = Binder.clearCallingIdentity();
4042 try {
4043 if (phone != null) {
4044 return phone.getDataActivationState();
4045 } else {
4046 return TelephonyManager.SIM_ACTIVATION_STATE_UNKNOWN;
4047 }
4048 } finally {
4049 Binder.restoreCallingIdentity(identity);
fionaxu0152e512016-11-14 13:36:14 -08004050 }
4051 }
4052
4053 /**
Wink Saville36469e72014-06-11 15:17:00 -07004054 * Returns the unread count of voicemails for a subId
4055 */
Sanket Padawe356d7632015-06-22 14:03:32 -07004056 @Override
Philip P. Moltmann700a9592019-10-03 11:53:50 -07004057 public int getVoiceMessageCountForSubscriber(int subId, String callingPackage,
4058 String callingFeatureId) {
Brad Ebingerf7664ba2018-11-29 12:43:38 -08004059 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
Philip P. Moltmann700a9592019-10-03 11:53:50 -07004060 mApp, subId, callingPackage, callingFeatureId,
4061 "getVoiceMessageCountForSubscriber")) {
Brad Ebingerf7664ba2018-11-29 12:43:38 -08004062 return 0;
4063 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004064 final long identity = Binder.clearCallingIdentity();
4065 try {
4066 final Phone phone = getPhone(subId);
4067 if (phone != null) {
4068 return phone.getVoiceMessageCount();
4069 } else {
4070 return 0;
4071 }
4072 } finally {
4073 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07004074 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07004075 }
4076
4077 /**
pkanwar8a4dcfb2017-01-19 13:43:16 -08004078 * returns true, if the device is in a state where both voice and data
4079 * are supported simultaneously. This can change based on location or network condition.
4080 */
4081 @Override
4082 public boolean isConcurrentVoiceAndDataAllowed(int subId) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004083 final long identity = Binder.clearCallingIdentity();
4084 try {
4085 final Phone phone = getPhone(subId);
4086 return (phone == null ? false : phone.isConcurrentVoiceAndDataAllowed());
4087 } finally {
4088 Binder.restoreCallingIdentity(identity);
4089 }
pkanwar8a4dcfb2017-01-19 13:43:16 -08004090 }
4091
4092 /**
fionaxu235cc5e2017-03-06 22:25:57 -08004093 * Send the dialer code if called from the current default dialer or the caller has
4094 * carrier privilege.
4095 * @param inputCode The dialer code to send
4096 */
4097 @Override
4098 public void sendDialerSpecialCode(String callingPackage, String inputCode) {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08004099 final Phone defaultPhone = getDefaultPhone();
fionaxu235cc5e2017-03-06 22:25:57 -08004100 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
Tyler Gunn5ddfdc92019-10-31 13:08:23 -07004101 TelecomManager tm = defaultPhone.getContext().getSystemService(TelecomManager.class);
4102 String defaultDialer = tm.getDefaultDialerPackage();
fionaxu235cc5e2017-03-06 22:25:57 -08004103 if (!TextUtils.equals(callingPackage, defaultDialer)) {
Shuo Qian2c0ae432019-12-05 11:40:37 -08004104 TelephonyPermissions.enforceCallingOrSelfCarrierPrivilege(mApp,
Jeff Davidson7e17e312018-02-13 18:17:36 -08004105 getDefaultSubscription(), "sendDialerSpecialCode");
fionaxu235cc5e2017-03-06 22:25:57 -08004106 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004107
4108 final long identity = Binder.clearCallingIdentity();
4109 try {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08004110 defaultPhone.sendDialerSpecialCode(inputCode);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004111 } finally {
4112 Binder.restoreCallingIdentity(identity);
4113 }
fionaxu235cc5e2017-03-06 22:25:57 -08004114 }
4115
Pengquan Menga1bb6272018-09-06 09:59:22 -07004116 @Override
4117 public int getNetworkSelectionMode(int subId) {
shilufc958392020-01-20 11:36:01 -08004118 TelephonyPermissions
Nathan Harold62c68512021-04-06 11:26:02 -07004119 .enforceCallingOrSelfReadPrecisePhoneStatePermissionOrCarrierPrivilege(
shilufc958392020-01-20 11:36:01 -08004120 mApp, subId, "getNetworkSelectionMode");
4121 final long identity = Binder.clearCallingIdentity();
4122 try {
4123 if (!isActiveSubscription(subId)) {
4124 return TelephonyManager.NETWORK_SELECTION_MODE_UNKNOWN;
4125 }
4126 return (int) sendRequest(CMD_GET_NETWORK_SELECTION_MODE, null /* argument */, subId);
4127 } finally {
4128 Binder.restoreCallingIdentity(identity);
Pengquan Menge92a50d2018-09-21 15:54:48 -07004129 }
Pengquan Menga1bb6272018-09-06 09:59:22 -07004130 }
4131
Brad Ebinger35c841c2018-10-01 10:40:55 -07004132 @Override
Brad Ebingerb2b65522019-03-15 13:48:47 -07004133 public boolean isInEmergencySmsMode() {
4134 enforceReadPrivilegedPermission("isInEmergencySmsMode");
4135 final long identity = Binder.clearCallingIdentity();
4136 try {
4137 for (Phone phone : PhoneFactory.getPhones()) {
4138 if (phone.isInEmergencySmsMode()) {
4139 return true;
4140 }
4141 }
4142 } finally {
4143 Binder.restoreCallingIdentity(identity);
4144 }
4145 return false;
4146 }
4147
shilu366312e2019-12-17 09:28:10 -08004148 /**
4149 * Requires carrier privileges or READ_PRECISE_PHONE_STATE permission.
4150 * @param subId The subscription to use to check the configuration.
4151 * @param c The callback that will be used to send the result.
4152 */
Brad Ebingerb2b65522019-03-15 13:48:47 -07004153 @Override
Brad Ebinger9878b0b2018-11-08 17:43:22 -08004154 public void registerImsRegistrationCallback(int subId, IImsRegistrationCallback c)
4155 throws RemoteException {
Nathan Harold62c68512021-04-06 11:26:02 -07004156 TelephonyPermissions.enforceCallingOrSelfReadPrecisePhoneStatePermissionOrCarrierPrivilege(
Rambo Wang37f9c242020-02-10 14:45:28 -08004157 mApp, subId, "registerImsRegistrationCallback");
Brad Ebingera2628302022-02-18 03:44:55 +00004158
4159 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);
joonhunshin49f0aed2022-08-05 08:33:05 +00004167
4168 ImsStateCallbackController controller = ImsStateCallbackController.getInstance();
4169 if (controller != null) {
4170 ImsManager imsManager = controller.getImsManager(subId);
4171 if (imsManager != null) {
4172 imsManager.addRegistrationCallbackForSubscription(c, subId);
4173 } else {
4174 throw new ServiceSpecificException(ImsException.CODE_ERROR_SERVICE_UNAVAILABLE);
4175 }
4176 } else {
4177 throw new ServiceSpecificException(ImsException.CODE_ERROR_UNSUPPORTED_OPERATION);
4178 }
Brad Ebinger1ce9c432019-07-16 13:19:44 -07004179 } catch (ImsException e) {
4180 throw new ServiceSpecificException(e.getCode());
Brad Ebinger35c841c2018-10-01 10:40:55 -07004181 } finally {
4182 Binder.restoreCallingIdentity(token);
4183 }
4184 }
4185
shilu366312e2019-12-17 09:28:10 -08004186 /**
4187 * Requires carrier privileges or READ_PRECISE_PHONE_STATE permission.
4188 * @param subId The subscription to use to check the configuration.
4189 * @param c The callback that will be used to send the result.
4190 */
Brad Ebinger35c841c2018-10-01 10:40:55 -07004191 @Override
Brad Ebinger9878b0b2018-11-08 17:43:22 -08004192 public void unregisterImsRegistrationCallback(int subId, IImsRegistrationCallback c) {
Nathan Harold62c68512021-04-06 11:26:02 -07004193 TelephonyPermissions.enforceCallingOrSelfReadPrecisePhoneStatePermissionOrCarrierPrivilege(
Rambo Wang37f9c242020-02-10 14:45:28 -08004194 mApp, subId, "unregisterImsRegistrationCallback");
Brad Ebinger4ae57f92019-01-09 16:51:30 -08004195 if (!SubscriptionManager.isValidSubscriptionId(subId)) {
4196 throw new IllegalArgumentException("Invalid Subscription ID: " + subId);
4197 }
Meng Wangafbc5852019-09-19 17:37:13 -07004198 final long token = Binder.clearCallingIdentity();
joonhunshin49f0aed2022-08-05 08:33:05 +00004199
Meng Wangafbc5852019-09-19 17:37:13 -07004200 try {
joonhunshin49f0aed2022-08-05 08:33:05 +00004201 ImsStateCallbackController controller = ImsStateCallbackController.getInstance();
4202 if (controller != null) {
4203 ImsManager imsManager = controller.getImsManager(subId);
4204 if (imsManager != null) {
4205 imsManager.removeRegistrationCallbackForSubscription(c, subId);
4206 } else {
4207 Log.i(LOG_TAG, "unregisterImsRegistrationCallback: " + subId
4208 + "is inactive, ignoring unregister.");
4209 // If the ImsManager is not valid, just return, since the callback
4210 // will already have been removed internally.
4211 }
4212 }
Meng Wangafbc5852019-09-19 17:37:13 -07004213 } finally {
4214 Binder.restoreCallingIdentity(token);
4215 }
Brad Ebinger35c841c2018-10-01 10:40:55 -07004216 }
4217
Brad Ebingera34a6c22019-10-22 17:36:18 -07004218 /**
4219 * Get the IMS service registration state for the MmTelFeature associated with this sub id.
4220 */
4221 @Override
4222 public void getImsMmTelRegistrationState(int subId, IIntegerConsumer consumer) {
4223 enforceReadPrivilegedPermission("getImsMmTelRegistrationState");
4224 if (!ImsManager.isImsSupportedOnDevice(mApp)) {
4225 throw new ServiceSpecificException(ImsException.CODE_ERROR_UNSUPPORTED_OPERATION,
4226 "IMS not available on device.");
4227 }
4228 final long token = Binder.clearCallingIdentity();
4229 try {
4230 Phone phone = getPhone(subId);
4231 if (phone == null) {
4232 Log.w(LOG_TAG, "getImsMmTelRegistrationState: called with an invalid subscription '"
4233 + subId + "'");
4234 throw new ServiceSpecificException(ImsException.CODE_ERROR_INVALID_SUBSCRIPTION);
4235 }
4236 phone.getImsRegistrationState(regState -> {
4237 try {
4238 consumer.accept((regState == null)
4239 ? RegistrationManager.REGISTRATION_STATE_NOT_REGISTERED : regState);
4240 } catch (RemoteException e) {
4241 // Ignore if the remote process is no longer available to call back.
4242 Log.w(LOG_TAG, "getImsMmTelRegistrationState: callback not available.");
4243 }
4244 });
4245 } finally {
4246 Binder.restoreCallingIdentity(token);
4247 }
4248 }
4249
4250 /**
4251 * Get the transport type for the IMS service registration state.
4252 */
4253 @Override
4254 public void getImsMmTelRegistrationTransportType(int subId, IIntegerConsumer consumer) {
Nathan Harold62c68512021-04-06 11:26:02 -07004255 TelephonyPermissions.enforceCallingOrSelfReadPrecisePhoneStatePermissionOrCarrierPrivilege(
Rambo Wang37f9c242020-02-10 14:45:28 -08004256 mApp, subId, "getImsMmTelRegistrationTransportType");
Brad Ebingera34a6c22019-10-22 17:36:18 -07004257 if (!ImsManager.isImsSupportedOnDevice(mApp)) {
4258 throw new ServiceSpecificException(ImsException.CODE_ERROR_UNSUPPORTED_OPERATION,
4259 "IMS not available on device.");
4260 }
4261 final long token = Binder.clearCallingIdentity();
4262 try {
4263 Phone phone = getPhone(subId);
4264 if (phone == null) {
4265 Log.w(LOG_TAG, "getImsMmTelRegistrationState: called with an invalid subscription '"
4266 + subId + "'");
4267 throw new ServiceSpecificException(ImsException.CODE_ERROR_INVALID_SUBSCRIPTION);
4268 }
4269 phone.getImsRegistrationTech(regTech -> {
4270 // Convert registration tech from ImsRegistrationImplBase -> RegistrationManager
4271 int regTechConverted = (regTech == null)
4272 ? ImsRegistrationImplBase.REGISTRATION_TECH_NONE : regTech;
4273 regTechConverted = RegistrationManager.IMS_REG_TO_ACCESS_TYPE_MAP.get(
4274 regTechConverted);
4275 try {
4276 consumer.accept(regTechConverted);
4277 } catch (RemoteException e) {
4278 // Ignore if the remote process is no longer available to call back.
4279 Log.w(LOG_TAG, "getImsMmTelRegistrationState: callback not available.");
4280 }
4281 });
4282 } finally {
4283 Binder.restoreCallingIdentity(token);
4284 }
4285 }
4286
shilu366312e2019-12-17 09:28:10 -08004287 /**
4288 * Requires carrier privileges or READ_PRECISE_PHONE_STATE permission.
4289 * @param subId The subscription to use to check the configuration.
4290 * @param c The callback that will be used to send the result.
4291 */
Brad Ebinger35c841c2018-10-01 10:40:55 -07004292 @Override
Brad Ebinger9878b0b2018-11-08 17:43:22 -08004293 public void registerMmTelCapabilityCallback(int subId, IImsCapabilityCallback c)
4294 throws RemoteException {
Nathan Harold62c68512021-04-06 11:26:02 -07004295 TelephonyPermissions.enforceCallingOrSelfReadPrecisePhoneStatePermissionOrCarrierPrivilege(
Rambo Wang37f9c242020-02-10 14:45:28 -08004296 mApp, subId, "registerMmTelCapabilityCallback");
Brad Ebingera2628302022-02-18 03:44:55 +00004297 if (!ImsManager.isImsSupportedOnDevice(mApp)) {
4298 throw new ServiceSpecificException(ImsException.CODE_ERROR_UNSUPPORTED_OPERATION,
4299 "IMS not available on device.");
4300 }
Brad Ebinger35c841c2018-10-01 10:40:55 -07004301 final long token = Binder.clearCallingIdentity();
4302 try {
Brad Ebingera2628302022-02-18 03:44:55 +00004303 int slotId = getSlotIndexOrException(subId);
4304 verifyImsMmTelConfiguredOrThrow(slotId);
4305 ImsManager.getInstance(mApp, slotId).addCapabilitiesCallbackForSubscription(c, subId);
Brad Ebinger1ce9c432019-07-16 13:19:44 -07004306 } catch (ImsException e) {
4307 throw new ServiceSpecificException(e.getCode());
Brad Ebinger35c841c2018-10-01 10:40:55 -07004308 } finally {
4309 Binder.restoreCallingIdentity(token);
4310 }
4311 }
4312
shilu366312e2019-12-17 09:28:10 -08004313 /**
4314 * Requires carrier privileges or READ_PRECISE_PHONE_STATE permission.
4315 * @param subId The subscription to use to check the configuration.
4316 * @param c The callback that will be used to send the result.
4317 */
Brad Ebinger35c841c2018-10-01 10:40:55 -07004318 @Override
Brad Ebinger9878b0b2018-11-08 17:43:22 -08004319 public void unregisterMmTelCapabilityCallback(int subId, IImsCapabilityCallback c) {
Nathan Harold62c68512021-04-06 11:26:02 -07004320 TelephonyPermissions.enforceCallingOrSelfReadPrecisePhoneStatePermissionOrCarrierPrivilege(
Rambo Wang37f9c242020-02-10 14:45:28 -08004321 mApp, subId, "unregisterMmTelCapabilityCallback");
Brad Ebinger4ae57f92019-01-09 16:51:30 -08004322 if (!SubscriptionManager.isValidSubscriptionId(subId)) {
4323 throw new IllegalArgumentException("Invalid Subscription ID: " + subId);
4324 }
Meng Wangafbc5852019-09-19 17:37:13 -07004325
4326 final long token = Binder.clearCallingIdentity();
4327 try {
Meng Wangafbc5852019-09-19 17:37:13 -07004328 ImsManager.getInstance(mApp, getSlotIndexOrException(subId))
Brad Ebinger4ae57f92019-01-09 16:51:30 -08004329 .removeCapabilitiesCallbackForSubscription(c, subId);
Meng Wangafbc5852019-09-19 17:37:13 -07004330 } catch (ImsException e) {
4331 Log.i(LOG_TAG, "unregisterMmTelCapabilityCallback: " + subId
4332 + "is inactive, ignoring unregister.");
4333 // If the subscription is no longer active, just return, since the callback
4334 // will already have been removed internally.
4335 } finally {
4336 Binder.restoreCallingIdentity(token);
4337 }
Brad Ebinger35c841c2018-10-01 10:40:55 -07004338 }
4339
4340 @Override
Brad Ebinger9878b0b2018-11-08 17:43:22 -08004341 public boolean isCapable(int subId, int capability, int regTech) {
4342 enforceReadPrivilegedPermission("isCapable");
Brad Ebinger35c841c2018-10-01 10:40:55 -07004343 final long token = Binder.clearCallingIdentity();
4344 try {
Brad Ebingera2628302022-02-18 03:44:55 +00004345 int slotId = getSlotIndexOrException(subId);
4346 verifyImsMmTelConfiguredOrThrow(slotId);
4347 return ImsManager.getInstance(mApp, slotId).queryMmTelCapability(capability, regTech);
4348 } catch (com.android.ims.ImsException e) {
4349 Log.w(LOG_TAG, "IMS isCapable - service unavailable: " + e.getMessage());
4350 return false;
Brad Ebinger1ce9c432019-07-16 13:19:44 -07004351 } catch (ImsException e) {
Brad Ebinger6b5ac222019-02-04 14:36:52 -08004352 Log.i(LOG_TAG, "isCapable: " + subId + " is inactive, returning false.");
4353 return false;
Brad Ebinger35c841c2018-10-01 10:40:55 -07004354 } finally {
4355 Binder.restoreCallingIdentity(token);
4356 }
4357 }
4358
4359 @Override
Brad Ebinger9878b0b2018-11-08 17:43:22 -08004360 public boolean isAvailable(int subId, int capability, int regTech) {
4361 enforceReadPrivilegedPermission("isAvailable");
Brad Ebinger35c841c2018-10-01 10:40:55 -07004362 final long token = Binder.clearCallingIdentity();
4363 try {
4364 Phone phone = getPhone(subId);
4365 if (phone == null) return false;
4366 return phone.isImsCapabilityAvailable(capability, regTech);
Daniel Bright5e40e4e2020-03-11 16:35:39 -07004367 } catch (com.android.ims.ImsException e) {
4368 Log.w(LOG_TAG, "IMS isAvailable - service unavailable: " + e.getMessage());
4369 return false;
Brad Ebinger35c841c2018-10-01 10:40:55 -07004370 } finally {
4371 Binder.restoreCallingIdentity(token);
4372 }
4373 }
4374
Brad Ebingerbc7dd582019-10-17 17:03:22 -07004375 /**
4376 * Determines if the MmTel feature capability is supported by the carrier configuration for this
4377 * subscription.
4378 * @param subId The subscription to use to check the configuration.
4379 * @param callback The callback that will be used to send the result.
4380 * @param capability The MmTelFeature capability that will be used to send the result.
4381 * @param transportType The transport type of the MmTelFeature capability.
4382 */
4383 @Override
4384 public void isMmTelCapabilitySupported(int subId, IIntegerConsumer callback, int capability,
4385 int transportType) {
4386 enforceReadPrivilegedPermission("isMmTelCapabilitySupported");
Brad Ebingera2628302022-02-18 03:44:55 +00004387 if (!ImsManager.isImsSupportedOnDevice(mApp)) {
4388 throw new ServiceSpecificException(ImsException.CODE_ERROR_UNSUPPORTED_OPERATION,
4389 "IMS not available on device.");
4390 }
Brad Ebingerbc7dd582019-10-17 17:03:22 -07004391 final long token = Binder.clearCallingIdentity();
4392 try {
Brad Ebingera2628302022-02-18 03:44:55 +00004393 int slotId = getSlotIndex(subId);
4394 if (slotId <= SubscriptionManager.INVALID_SIM_SLOT_INDEX) {
4395 Log.w(LOG_TAG, "isMmTelCapabilitySupported: called with an inactive subscription '"
4396 + subId + "'");
4397 throw new ServiceSpecificException(ImsException.CODE_ERROR_INVALID_SUBSCRIPTION);
4398 }
4399 verifyImsMmTelConfiguredOrThrow(slotId);
4400 ImsManager.getInstance(mApp, slotId).isSupported(capability,
4401 transportType, aBoolean -> {
4402 try {
4403 callback.accept((aBoolean == null) ? 0 : (aBoolean ? 1 : 0));
4404 } catch (RemoteException e) {
4405 Log.w(LOG_TAG, "isMmTelCapabilitySupported: remote caller is not "
4406 + "running. Ignore");
4407 }
4408 });
Brad Ebinger919631e2021-06-02 17:46:35 -07004409 } catch (ImsException e) {
4410 throw new ServiceSpecificException(e.getCode());
Brad Ebingerbc7dd582019-10-17 17:03:22 -07004411 } finally {
4412 Binder.restoreCallingIdentity(token);
4413 }
4414 }
4415
shilu366312e2019-12-17 09:28:10 -08004416 /**
4417 * Requires carrier privileges or READ_PRECISE_PHONE_STATE permission.
4418 * @param subId The subscription to use to check the configuration.
4419 */
Brad Ebinger35c841c2018-10-01 10:40:55 -07004420 @Override
4421 public boolean isAdvancedCallingSettingEnabled(int subId) {
Nathan Harold62c68512021-04-06 11:26:02 -07004422 TelephonyPermissions.enforceCallingOrSelfReadPrecisePhoneStatePermissionOrCarrierPrivilege(
Rambo Wang37f9c242020-02-10 14:45:28 -08004423 mApp, subId, "isAdvancedCallingSettingEnabled");
shilu366312e2019-12-17 09:28:10 -08004424
Brad Ebinger35c841c2018-10-01 10:40:55 -07004425 final long token = Binder.clearCallingIdentity();
4426 try {
Brad Ebinger919631e2021-06-02 17:46:35 -07004427 int slotId = getSlotIndexOrException(subId);
Brad Ebinger735c5ce2021-07-12 13:58:21 -07004428 // This setting doesn't require an active ImsService connection, so do not verify.
Brad Ebinger919631e2021-06-02 17:46:35 -07004429 return ImsManager.getInstance(mApp, slotId).isEnhanced4gLteModeSettingEnabledByUser();
Brad Ebinger1ce9c432019-07-16 13:19:44 -07004430 } catch (ImsException e) {
4431 throw new ServiceSpecificException(e.getCode());
Brad Ebinger35c841c2018-10-01 10:40:55 -07004432 } finally {
4433 Binder.restoreCallingIdentity(token);
4434 }
4435 }
4436
4437 @Override
Brad Ebinger1c162042019-02-21 14:49:10 -08004438 public void setAdvancedCallingSettingEnabled(int subId, boolean isEnabled) {
Brad Ebinger35c841c2018-10-01 10:40:55 -07004439 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(mApp, subId,
Brad Ebinger1c162042019-02-21 14:49:10 -08004440 "setAdvancedCallingSettingEnabled");
Brad Ebinger35c841c2018-10-01 10:40:55 -07004441 final long identity = Binder.clearCallingIdentity();
4442 try {
Brad Ebinger919631e2021-06-02 17:46:35 -07004443 int slotId = getSlotIndexOrException(subId);
Brad Ebinger735c5ce2021-07-12 13:58:21 -07004444 // This setting doesn't require an active ImsService connection, so do not verify. The
4445 // new setting will be picked up when the ImsService comes up next if it isn't up.
Brad Ebinger919631e2021-06-02 17:46:35 -07004446 ImsManager.getInstance(mApp, slotId).setEnhanced4gLteModeSetting(isEnabled);
Brad Ebinger1ce9c432019-07-16 13:19:44 -07004447 } catch (ImsException e) {
4448 throw new ServiceSpecificException(e.getCode());
Brad Ebinger35c841c2018-10-01 10:40:55 -07004449 } finally {
4450 Binder.restoreCallingIdentity(identity);
4451 }
4452 }
4453
shilu366312e2019-12-17 09:28:10 -08004454 /**
4455 * Requires carrier privileges or READ_PRECISE_PHONE_STATE permission.
4456 * @param subId The subscription to use to check the configuration.
4457 */
Brad Ebinger35c841c2018-10-01 10:40:55 -07004458 @Override
Brad Ebinger9878b0b2018-11-08 17:43:22 -08004459 public boolean isVtSettingEnabled(int subId) {
Nathan Harold62c68512021-04-06 11:26:02 -07004460 TelephonyPermissions.enforceCallingOrSelfReadPrecisePhoneStatePermissionOrCarrierPrivilege(
Rambo Wang37f9c242020-02-10 14:45:28 -08004461 mApp, subId, "isVtSettingEnabled");
Brad Ebinger35c841c2018-10-01 10:40:55 -07004462 final long identity = Binder.clearCallingIdentity();
4463 try {
Brad Ebinger919631e2021-06-02 17:46:35 -07004464 int slotId = getSlotIndexOrException(subId);
Brad Ebinger735c5ce2021-07-12 13:58:21 -07004465 // This setting doesn't require an active ImsService connection, so do not verify.
Brad Ebinger919631e2021-06-02 17:46:35 -07004466 return ImsManager.getInstance(mApp, slotId).isVtEnabledByUser();
Brad Ebinger1ce9c432019-07-16 13:19:44 -07004467 } catch (ImsException e) {
4468 throw new ServiceSpecificException(e.getCode());
Brad Ebinger35c841c2018-10-01 10:40:55 -07004469 } finally {
4470 Binder.restoreCallingIdentity(identity);
4471 }
4472 }
4473
4474 @Override
Brad Ebinger1c162042019-02-21 14:49:10 -08004475 public void setVtSettingEnabled(int subId, boolean isEnabled) {
Brad Ebinger35c841c2018-10-01 10:40:55 -07004476 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(mApp, subId,
Brad Ebinger1c162042019-02-21 14:49:10 -08004477 "setVtSettingEnabled");
Brad Ebinger35c841c2018-10-01 10:40:55 -07004478 final long identity = Binder.clearCallingIdentity();
4479 try {
Brad Ebinger919631e2021-06-02 17:46:35 -07004480 int slotId = getSlotIndexOrException(subId);
Brad Ebinger735c5ce2021-07-12 13:58:21 -07004481 // This setting doesn't require an active ImsService connection, so do not verify. The
4482 // new setting will be picked up when the ImsService comes up next if it isn't up.
Brad Ebinger919631e2021-06-02 17:46:35 -07004483 ImsManager.getInstance(mApp, slotId).setVtSetting(isEnabled);
Brad Ebinger1ce9c432019-07-16 13:19:44 -07004484 } catch (ImsException e) {
4485 throw new ServiceSpecificException(e.getCode());
Brad Ebinger35c841c2018-10-01 10:40:55 -07004486 } finally {
4487 Binder.restoreCallingIdentity(identity);
4488 }
4489 }
4490
shilu366312e2019-12-17 09:28:10 -08004491 /**
4492 * Requires carrier privileges or READ_PRECISE_PHONE_STATE permission.
4493 * @param subId The subscription to use to check the configuration.
4494 */
Brad Ebinger35c841c2018-10-01 10:40:55 -07004495 @Override
4496 public boolean isVoWiFiSettingEnabled(int subId) {
Nathan Harold62c68512021-04-06 11:26:02 -07004497 TelephonyPermissions.enforceCallingOrSelfReadPrecisePhoneStatePermissionOrCarrierPrivilege(
Rambo Wang37f9c242020-02-10 14:45:28 -08004498 mApp, subId, "isVoWiFiSettingEnabled");
Brad Ebinger35c841c2018-10-01 10:40:55 -07004499 final long identity = Binder.clearCallingIdentity();
4500 try {
Brad Ebinger919631e2021-06-02 17:46:35 -07004501 int slotId = getSlotIndexOrException(subId);
Brad Ebinger735c5ce2021-07-12 13:58:21 -07004502 // This setting doesn't require an active ImsService connection, so do not verify.
Brad Ebinger919631e2021-06-02 17:46:35 -07004503 return ImsManager.getInstance(mApp, slotId).isWfcEnabledByUser();
Brad Ebinger1ce9c432019-07-16 13:19:44 -07004504 } catch (ImsException e) {
4505 throw new ServiceSpecificException(e.getCode());
Brad Ebinger35c841c2018-10-01 10:40:55 -07004506 } finally {
4507 Binder.restoreCallingIdentity(identity);
4508 }
4509 }
4510
4511 @Override
Brad Ebinger1c162042019-02-21 14:49:10 -08004512 public void setVoWiFiSettingEnabled(int subId, boolean isEnabled) {
Brad Ebinger35c841c2018-10-01 10:40:55 -07004513 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(mApp, subId,
Brad Ebinger1c162042019-02-21 14:49:10 -08004514 "setVoWiFiSettingEnabled");
Brad Ebinger35c841c2018-10-01 10:40:55 -07004515 final long identity = Binder.clearCallingIdentity();
4516 try {
Brad Ebinger919631e2021-06-02 17:46:35 -07004517 int slotId = getSlotIndexOrException(subId);
Brad Ebinger735c5ce2021-07-12 13:58:21 -07004518 // This setting doesn't require an active ImsService connection, so do not verify. The
4519 // new setting will be picked up when the ImsService comes up next if it isn't up.
Brad Ebinger919631e2021-06-02 17:46:35 -07004520 ImsManager.getInstance(mApp, slotId).setWfcSetting(isEnabled);
Brad Ebinger1ce9c432019-07-16 13:19:44 -07004521 } catch (ImsException e) {
4522 throw new ServiceSpecificException(e.getCode());
Brad Ebinger35c841c2018-10-01 10:40:55 -07004523 } finally {
4524 Binder.restoreCallingIdentity(identity);
4525 }
4526 }
4527
shilu366312e2019-12-17 09:28:10 -08004528 /**
Sooraj Sasindrane655add2020-11-23 19:40:38 -08004529 * @return true if the user's setting for Voice over Cross SIM is enabled and false if it is not
4530 * Requires carrier privileges or READ_PRECISE_PHONE_STATE permission.
4531 * @param subId The subscription to use to check the configuration.
4532 */
4533 @Override
4534 public boolean isCrossSimCallingEnabledByUser(int subId) {
Nathan Harold62c68512021-04-06 11:26:02 -07004535 TelephonyPermissions.enforceCallingOrSelfReadPrecisePhoneStatePermissionOrCarrierPrivilege(
Sooraj Sasindrane655add2020-11-23 19:40:38 -08004536 mApp, subId, "isCrossSimCallingEnabledByUser");
4537 final long identity = Binder.clearCallingIdentity();
4538 try {
Brad Ebinger919631e2021-06-02 17:46:35 -07004539 int slotId = getSlotIndexOrException(subId);
Brad Ebinger735c5ce2021-07-12 13:58:21 -07004540 // This setting doesn't require an active ImsService connection, so do not verify.
Brad Ebinger919631e2021-06-02 17:46:35 -07004541 return ImsManager.getInstance(mApp, slotId).isCrossSimCallingEnabledByUser();
Sooraj Sasindrane655add2020-11-23 19:40:38 -08004542 } catch (ImsException e) {
4543 throw new ServiceSpecificException(e.getCode());
4544 } finally {
4545 Binder.restoreCallingIdentity(identity);
4546 }
4547 }
4548
4549 /**
4550 * Sets the user's setting for whether or not Voice over Cross SIM is enabled.
4551 * Requires MODIFY_PHONE_STATE permission.
4552 * @param subId The subscription to use to check the configuration.
4553 * @param isEnabled true if the user's setting for Voice over Cross SIM is enabled,
4554 * false otherwise
4555 */
4556 @Override
4557 public void setCrossSimCallingEnabled(int subId, boolean isEnabled) {
4558 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(mApp, subId,
4559 "setCrossSimCallingEnabled");
4560 final long identity = Binder.clearCallingIdentity();
4561 try {
Brad Ebinger919631e2021-06-02 17:46:35 -07004562 int slotId = getSlotIndexOrException(subId);
Brad Ebinger735c5ce2021-07-12 13:58:21 -07004563 // This setting doesn't require an active ImsService connection, so do not verify. The
4564 // new setting will be picked up when the ImsService comes up next if it isn't up.
Brad Ebinger919631e2021-06-02 17:46:35 -07004565 ImsManager.getInstance(mApp, slotId).setCrossSimCallingEnabled(isEnabled);
Sooraj Sasindrane655add2020-11-23 19:40:38 -08004566 } catch (ImsException e) {
4567 throw new ServiceSpecificException(e.getCode());
4568 } finally {
4569 Binder.restoreCallingIdentity(identity);
4570 }
4571 }
4572
4573 /**
shilu366312e2019-12-17 09:28:10 -08004574 * Requires carrier privileges or READ_PRECISE_PHONE_STATE permission.
4575 * @param subId The subscription to use to check the configuration.
4576 */
Brad Ebinger35c841c2018-10-01 10:40:55 -07004577 @Override
Nathan Harold62c68512021-04-06 11:26:02 -07004578
Brad Ebinger35c841c2018-10-01 10:40:55 -07004579 public boolean isVoWiFiRoamingSettingEnabled(int subId) {
Nathan Harold62c68512021-04-06 11:26:02 -07004580 TelephonyPermissions.enforceCallingOrSelfReadPrecisePhoneStatePermissionOrCarrierPrivilege(
Rambo Wang37f9c242020-02-10 14:45:28 -08004581 mApp, subId, "isVoWiFiRoamingSettingEnabled");
Brad Ebinger35c841c2018-10-01 10:40:55 -07004582 final long identity = Binder.clearCallingIdentity();
4583 try {
Brad Ebinger919631e2021-06-02 17:46:35 -07004584 int slotId = getSlotIndexOrException(subId);
Brad Ebinger735c5ce2021-07-12 13:58:21 -07004585 // This setting doesn't require an active ImsService connection, so do not verify.
Brad Ebinger919631e2021-06-02 17:46:35 -07004586 return ImsManager.getInstance(mApp, slotId).isWfcRoamingEnabledByUser();
Brad Ebinger1ce9c432019-07-16 13:19:44 -07004587 } catch (ImsException e) {
4588 throw new ServiceSpecificException(e.getCode());
Brad Ebinger35c841c2018-10-01 10:40:55 -07004589 } finally {
4590 Binder.restoreCallingIdentity(identity);
4591 }
4592 }
4593
4594 @Override
Brad Ebinger1c162042019-02-21 14:49:10 -08004595 public void setVoWiFiRoamingSettingEnabled(int subId, boolean isEnabled) {
Brad Ebinger35c841c2018-10-01 10:40:55 -07004596 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(mApp, subId,
Brad Ebinger1c162042019-02-21 14:49:10 -08004597 "setVoWiFiRoamingSettingEnabled");
Brad Ebinger35c841c2018-10-01 10:40:55 -07004598 final long identity = Binder.clearCallingIdentity();
4599 try {
Brad Ebinger919631e2021-06-02 17:46:35 -07004600 int slotId = getSlotIndexOrException(subId);
Brad Ebinger735c5ce2021-07-12 13:58:21 -07004601 // This setting doesn't require an active ImsService connection, so do not verify. The
4602 // new setting will be picked up when the ImsService comes up next if it isn't up.
Brad Ebinger919631e2021-06-02 17:46:35 -07004603 ImsManager.getInstance(mApp, slotId).setWfcRoamingSetting(isEnabled);
Brad Ebinger1ce9c432019-07-16 13:19:44 -07004604 } catch (ImsException e) {
4605 throw new ServiceSpecificException(e.getCode());
Brad Ebinger35c841c2018-10-01 10:40:55 -07004606 } finally {
4607 Binder.restoreCallingIdentity(identity);
4608 }
4609 }
4610
4611 @Override
4612 public void setVoWiFiNonPersistent(int subId, boolean isCapable, int mode) {
4613 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(mApp, subId,
4614 "setVoWiFiNonPersistent");
4615 final long identity = Binder.clearCallingIdentity();
4616 try {
Brad Ebinger919631e2021-06-02 17:46:35 -07004617 int slotId = getSlotIndexOrException(subId);
Brad Ebinger735c5ce2021-07-12 13:58:21 -07004618 // This setting will be ignored if the ImsService isn't up.
Brad Ebinger919631e2021-06-02 17:46:35 -07004619 ImsManager.getInstance(mApp, slotId).setWfcNonPersistent(isCapable, mode);
Brad Ebinger1ce9c432019-07-16 13:19:44 -07004620 } catch (ImsException e) {
4621 throw new ServiceSpecificException(e.getCode());
Brad Ebinger35c841c2018-10-01 10:40:55 -07004622 } finally {
4623 Binder.restoreCallingIdentity(identity);
4624 }
4625 }
4626
shilu366312e2019-12-17 09:28:10 -08004627 /**
4628 * Requires carrier privileges or READ_PRECISE_PHONE_STATE permission.
4629 * @param subId The subscription to use to check the configuration.
4630 */
Brad Ebinger35c841c2018-10-01 10:40:55 -07004631 @Override
4632 public int getVoWiFiModeSetting(int subId) {
Nathan Harold62c68512021-04-06 11:26:02 -07004633 TelephonyPermissions.enforceCallingOrSelfReadPrecisePhoneStatePermissionOrCarrierPrivilege(
Rambo Wang37f9c242020-02-10 14:45:28 -08004634 mApp, subId, "getVoWiFiModeSetting");
Brad Ebinger35c841c2018-10-01 10:40:55 -07004635 final long identity = Binder.clearCallingIdentity();
4636 try {
Brad Ebinger919631e2021-06-02 17:46:35 -07004637 int slotId = getSlotIndexOrException(subId);
Brad Ebinger735c5ce2021-07-12 13:58:21 -07004638 // This setting doesn't require an active ImsService connection, so do not verify.
Brad Ebinger919631e2021-06-02 17:46:35 -07004639 return ImsManager.getInstance(mApp, slotId).getWfcMode(false /*isRoaming*/);
Brad Ebinger1ce9c432019-07-16 13:19:44 -07004640 } catch (ImsException e) {
4641 throw new ServiceSpecificException(e.getCode());
Brad Ebinger35c841c2018-10-01 10:40:55 -07004642 } finally {
4643 Binder.restoreCallingIdentity(identity);
4644 }
4645 }
4646
4647 @Override
4648 public void setVoWiFiModeSetting(int subId, int mode) {
4649 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(mApp, subId,
4650 "setVoWiFiModeSetting");
4651 final long identity = Binder.clearCallingIdentity();
4652 try {
Brad Ebinger919631e2021-06-02 17:46:35 -07004653 int slotId = getSlotIndexOrException(subId);
Brad Ebinger735c5ce2021-07-12 13:58:21 -07004654 // This setting doesn't require an active ImsService connection, so do not verify. The
4655 // new setting will be picked up when the ImsService comes up next if it isn't up.
Brad Ebinger919631e2021-06-02 17:46:35 -07004656 ImsManager.getInstance(mApp, slotId).setWfcMode(mode, false /*isRoaming*/);
Brad Ebinger1ce9c432019-07-16 13:19:44 -07004657 } catch (ImsException e) {
4658 throw new ServiceSpecificException(e.getCode());
Brad Ebinger35c841c2018-10-01 10:40:55 -07004659 } finally {
4660 Binder.restoreCallingIdentity(identity);
4661 }
4662 }
4663
4664 @Override
4665 public int getVoWiFiRoamingModeSetting(int subId) {
4666 enforceReadPrivilegedPermission("getVoWiFiRoamingModeSetting");
4667 final long identity = Binder.clearCallingIdentity();
4668 try {
Brad Ebinger919631e2021-06-02 17:46:35 -07004669 int slotId = getSlotIndexOrException(subId);
Brad Ebinger735c5ce2021-07-12 13:58:21 -07004670 // This setting doesn't require an active ImsService connection, so do not verify.
Brad Ebinger919631e2021-06-02 17:46:35 -07004671 return ImsManager.getInstance(mApp, slotId).getWfcMode(true /*isRoaming*/);
Brad Ebinger1ce9c432019-07-16 13:19:44 -07004672 } catch (ImsException e) {
4673 throw new ServiceSpecificException(e.getCode());
Brad Ebinger35c841c2018-10-01 10:40:55 -07004674 } finally {
4675 Binder.restoreCallingIdentity(identity);
4676 }
4677 }
4678
4679 @Override
4680 public void setVoWiFiRoamingModeSetting(int subId, int mode) {
4681 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(mApp, subId,
4682 "setVoWiFiRoamingModeSetting");
4683 final long identity = Binder.clearCallingIdentity();
4684 try {
Brad Ebinger919631e2021-06-02 17:46:35 -07004685 int slotId = getSlotIndexOrException(subId);
Brad Ebinger735c5ce2021-07-12 13:58:21 -07004686 // This setting doesn't require an active ImsService connection, so do not verify. The
4687 // new setting will be picked up when the ImsService comes up next if it isn't up.
Brad Ebinger919631e2021-06-02 17:46:35 -07004688 ImsManager.getInstance(mApp, slotId).setWfcMode(mode, true /*isRoaming*/);
Brad Ebinger1ce9c432019-07-16 13:19:44 -07004689 } catch (ImsException e) {
4690 throw new ServiceSpecificException(e.getCode());
Brad Ebinger35c841c2018-10-01 10:40:55 -07004691 } finally {
4692 Binder.restoreCallingIdentity(identity);
4693 }
4694 }
4695
4696 @Override
4697 public void setRttCapabilitySetting(int subId, boolean isEnabled) {
4698 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(mApp, subId,
4699 "setRttCapabilityEnabled");
4700 final long identity = Binder.clearCallingIdentity();
4701 try {
Brad Ebinger919631e2021-06-02 17:46:35 -07004702 int slotId = getSlotIndexOrException(subId);
Brad Ebinger735c5ce2021-07-12 13:58:21 -07004703 // This setting doesn't require an active ImsService connection, so do not verify. The
4704 // new setting will be picked up when the ImsService comes up next if it isn't up.
Brad Ebinger919631e2021-06-02 17:46:35 -07004705 ImsManager.getInstance(mApp, slotId).setRttEnabled(isEnabled);
Brad Ebinger1ce9c432019-07-16 13:19:44 -07004706 } catch (ImsException e) {
4707 throw new ServiceSpecificException(e.getCode());
Brad Ebinger35c841c2018-10-01 10:40:55 -07004708 } finally {
4709 Binder.restoreCallingIdentity(identity);
4710 }
4711 }
4712
shilu366312e2019-12-17 09:28:10 -08004713 /**
4714 * Requires carrier privileges or READ_PRECISE_PHONE_STATE permission.
4715 * @param subId The subscription to use to check the configuration.
4716 */
Brad Ebinger35c841c2018-10-01 10:40:55 -07004717 @Override
4718 public boolean isTtyOverVolteEnabled(int subId) {
Nathan Harold62c68512021-04-06 11:26:02 -07004719 TelephonyPermissions.enforceCallingOrSelfReadPrecisePhoneStatePermissionOrCarrierPrivilege(
Rambo Wang37f9c242020-02-10 14:45:28 -08004720 mApp, subId, "isTtyOverVolteEnabled");
Brad Ebinger35c841c2018-10-01 10:40:55 -07004721 final long identity = Binder.clearCallingIdentity();
4722 try {
Brad Ebinger919631e2021-06-02 17:46:35 -07004723 int slotId = getSlotIndexOrException(subId);
Brad Ebinger735c5ce2021-07-12 13:58:21 -07004724 // This setting doesn't require an active ImsService connection, so do not verify.
Brad Ebinger919631e2021-06-02 17:46:35 -07004725 return ImsManager.getInstance(mApp, slotId).isTtyOnVoLteCapable();
Brad Ebinger1ce9c432019-07-16 13:19:44 -07004726 } catch (ImsException e) {
4727 throw new ServiceSpecificException(e.getCode());
Brad Ebinger35c841c2018-10-01 10:40:55 -07004728 } finally {
4729 Binder.restoreCallingIdentity(identity);
4730 }
4731 }
4732
Brad Ebingerdf5b4f02018-10-31 11:24:17 -07004733 @Override
4734 public void registerImsProvisioningChangedCallback(int subId, IImsConfigCallback callback) {
4735 enforceReadPrivilegedPermission("registerImsProvisioningChangedCallback");
joonhunshincffb7fc2021-11-28 07:32:01 +00004736
Brad Ebingerdf5b4f02018-10-31 11:24:17 -07004737 final long identity = Binder.clearCallingIdentity();
4738 try {
Brad Ebingera2628302022-02-18 03:44:55 +00004739 if (!isImsAvailableOnDevice()) {
4740 throw new ServiceSpecificException(ImsException.CODE_ERROR_UNSUPPORTED_OPERATION,
4741 "IMS not available on device.");
4742 }
4743 int slotId = getSlotIndexOrException(subId);
4744 verifyImsMmTelConfiguredOrThrow(slotId);
4745 ImsManager.getInstance(mApp, slotId)
4746 .addProvisioningCallbackForSubscription(callback, subId);
Brad Ebinger1ce9c432019-07-16 13:19:44 -07004747 } catch (ImsException e) {
4748 throw new ServiceSpecificException(e.getCode());
Brad Ebingerdf5b4f02018-10-31 11:24:17 -07004749 } finally {
4750 Binder.restoreCallingIdentity(identity);
4751 }
4752 }
4753
4754 @Override
4755 public void unregisterImsProvisioningChangedCallback(int subId, IImsConfigCallback callback) {
4756 enforceReadPrivilegedPermission("unregisterImsProvisioningChangedCallback");
joonhunshincffb7fc2021-11-28 07:32:01 +00004757
Brad Ebingerdf5b4f02018-10-31 11:24:17 -07004758 final long identity = Binder.clearCallingIdentity();
Brad Ebinger4ae57f92019-01-09 16:51:30 -08004759 if (!SubscriptionManager.isValidSubscriptionId(subId)) {
4760 throw new IllegalArgumentException("Invalid Subscription ID: " + subId);
4761 }
Brad Ebingerdf5b4f02018-10-31 11:24:17 -07004762 try {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08004763 ImsManager.getInstance(mApp, getSlotIndexOrException(subId))
Brad Ebinger4ae57f92019-01-09 16:51:30 -08004764 .removeProvisioningCallbackForSubscription(callback, subId);
Brad Ebinger1ce9c432019-07-16 13:19:44 -07004765 } catch (ImsException e) {
Brad Ebinger4ae57f92019-01-09 16:51:30 -08004766 Log.i(LOG_TAG, "unregisterImsProvisioningChangedCallback: " + subId
4767 + "is inactive, ignoring unregister.");
4768 // If the subscription is no longer active, just return, since the callback will already
4769 // have been removed internally.
Brad Ebingerdf5b4f02018-10-31 11:24:17 -07004770 } finally {
4771 Binder.restoreCallingIdentity(identity);
4772 }
4773 }
4774
joonhunshincffb7fc2021-11-28 07:32:01 +00004775 @Override
4776 public void registerFeatureProvisioningChangedCallback(int subId,
4777 IFeatureProvisioningCallback callback) {
4778 TelephonyPermissions.enforceCallingOrSelfReadPrecisePhoneStatePermissionOrCarrierPrivilege(
4779 mApp, subId, "registerFeatureProvisioningChangedCallback");
4780
4781 final long identity = Binder.clearCallingIdentity();
4782 if (!SubscriptionManager.isValidSubscriptionId(subId)) {
4783 throw new IllegalArgumentException("Invalid Subscription ID: " + subId);
4784 }
4785
joonhunshin91bc1952022-04-29 08:47:15 +00004786 try {
4787 ImsProvisioningController controller = ImsProvisioningController.getInstance();
4788 if (controller == null) {
4789 throw new ServiceSpecificException(ImsException.CODE_ERROR_UNSUPPORTED_OPERATION,
4790 "Device does not support IMS");
4791 }
4792 controller.addFeatureProvisioningChangedCallback(subId, callback);
4793 } finally {
4794 Binder.restoreCallingIdentity(identity);
4795 }
joonhunshincffb7fc2021-11-28 07:32:01 +00004796 }
4797
4798 @Override
4799 public void unregisterFeatureProvisioningChangedCallback(int subId,
4800 IFeatureProvisioningCallback callback) {
4801 TelephonyPermissions.enforceCallingOrSelfReadPrecisePhoneStatePermissionOrCarrierPrivilege(
4802 mApp, subId, "unregisterFeatureProvisioningChangedCallback");
4803
4804 final long identity = Binder.clearCallingIdentity();
4805 if (!SubscriptionManager.isValidSubscriptionId(subId)) {
4806 throw new IllegalArgumentException("Invalid Subscription ID: " + subId);
4807 }
4808
joonhunshin91bc1952022-04-29 08:47:15 +00004809 try {
4810 ImsProvisioningController controller = ImsProvisioningController.getInstance();
4811 if (controller == null) {
4812 loge("unregisterFeatureProvisioningChangedCallback: Device does not support IMS");
4813 return;
4814 }
4815 controller.removeFeatureProvisioningChangedCallback(subId, callback);
4816 } finally {
4817 Binder.restoreCallingIdentity(identity);
4818 }
joonhunshincffb7fc2021-11-28 07:32:01 +00004819 }
allenwtsu99c623b2020-01-03 18:24:23 +08004820
4821 private void checkModifyPhoneStatePermission(int subId, String message) {
4822 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(mApp, subId,
4823 message);
4824 }
4825
allenwtsu99c623b2020-01-03 18:24:23 +08004826 @Override
joonhunshincffb7fc2021-11-28 07:32:01 +00004827 public void setRcsProvisioningStatusForCapability(int subId, int capability, int tech,
allenwtsu99c623b2020-01-03 18:24:23 +08004828 boolean isProvisioned) {
4829 checkModifyPhoneStatePermission(subId, "setRcsProvisioningStatusForCapability");
4830
4831 final long identity = Binder.clearCallingIdentity();
4832 try {
joonhunshin91bc1952022-04-29 08:47:15 +00004833 ImsProvisioningController controller = ImsProvisioningController.getInstance();
4834 if (controller == null) {
4835 loge("setRcsProvisioningStatusForCapability: Device does not support IMS");
4836 return;
4837 }
4838 controller.setRcsProvisioningStatusForCapability(
4839 subId, capability, tech, isProvisioned);
allenwtsu99c623b2020-01-03 18:24:23 +08004840 } finally {
4841 Binder.restoreCallingIdentity(identity);
4842 }
allenwtsu99c623b2020-01-03 18:24:23 +08004843 }
4844
4845
4846 @Override
joonhunshincffb7fc2021-11-28 07:32:01 +00004847 public boolean getRcsProvisioningStatusForCapability(int subId, int capability, int tech) {
4848 TelephonyPermissions.enforceCallingOrSelfReadPrecisePhoneStatePermissionOrCarrierPrivilege(
4849 mApp, subId, "getRcsProvisioningStatusForCapability");
4850
allenwtsu99c623b2020-01-03 18:24:23 +08004851 final long identity = Binder.clearCallingIdentity();
4852 try {
joonhunshin91bc1952022-04-29 08:47:15 +00004853 ImsProvisioningController controller = ImsProvisioningController.getInstance();
4854 if (controller == null) {
4855 loge("getRcsProvisioningStatusForCapability: Device does not support IMS");
4856
4857 // device does not support IMS, this method will return true always.
4858 return true;
4859 }
4860 return controller.getRcsProvisioningStatusForCapability(subId, capability, tech);
allenwtsu99c623b2020-01-03 18:24:23 +08004861 } finally {
4862 Binder.restoreCallingIdentity(identity);
4863 }
4864 }
4865
Brad Ebingerdf5b4f02018-10-31 11:24:17 -07004866 @Override
Brad Ebinger1c8542e2019-01-14 13:43:14 -08004867 public void setImsProvisioningStatusForCapability(int subId, int capability, int tech,
4868 boolean isProvisioned) {
allenwtsu99c623b2020-01-03 18:24:23 +08004869 checkModifyPhoneStatePermission(subId, "setImsProvisioningStatusForCapability");
joonhunshincffb7fc2021-11-28 07:32:01 +00004870
Brad Ebinger1c8542e2019-01-14 13:43:14 -08004871 final long identity = Binder.clearCallingIdentity();
4872 try {
joonhunshin91bc1952022-04-29 08:47:15 +00004873 ImsProvisioningController controller = ImsProvisioningController.getInstance();
4874 if (controller == null) {
4875 loge("setImsProvisioningStatusForCapability: Device does not support IMS");
4876 return;
4877 }
4878 controller.setImsProvisioningStatusForCapability(
4879 subId, capability, tech, isProvisioned);
Brad Ebinger1c8542e2019-01-14 13:43:14 -08004880 } finally {
4881 Binder.restoreCallingIdentity(identity);
4882 }
4883 }
4884
4885 @Override
4886 public boolean getImsProvisioningStatusForCapability(int subId, int capability, int tech) {
joonhunshincffb7fc2021-11-28 07:32:01 +00004887 TelephonyPermissions.enforceCallingOrSelfReadPrecisePhoneStatePermissionOrCarrierPrivilege(
4888 mApp, subId, "getProvisioningStatusForCapability");
4889
Brad Ebinger1c8542e2019-01-14 13:43:14 -08004890 final long identity = Binder.clearCallingIdentity();
4891 try {
joonhunshin91bc1952022-04-29 08:47:15 +00004892 ImsProvisioningController controller = ImsProvisioningController.getInstance();
4893 if (controller == null) {
4894 loge("getImsProvisioningStatusForCapability: Device does not support IMS");
Brad Ebinger1c8542e2019-01-14 13:43:14 -08004895
joonhunshin91bc1952022-04-29 08:47:15 +00004896 // device does not support IMS, this method will return true always.
4897 return true;
4898 }
4899 return controller.getImsProvisioningStatusForCapability(subId, capability, tech);
Brad Ebinger1c8542e2019-01-14 13:43:14 -08004900 } finally {
4901 Binder.restoreCallingIdentity(identity);
4902 }
4903 }
4904
4905 @Override
joonhunshincffb7fc2021-11-28 07:32:01 +00004906 public boolean isProvisioningRequiredForCapability(int subId, int capability, int tech) {
4907 TelephonyPermissions.enforceCallingOrSelfReadPrecisePhoneStatePermissionOrCarrierPrivilege(
4908 mApp, subId, "isProvisioningRequiredForCapability");
4909
4910 final long identity = Binder.clearCallingIdentity();
4911 try {
joonhunshin91bc1952022-04-29 08:47:15 +00004912 ImsProvisioningController controller = ImsProvisioningController.getInstance();
4913 if (controller == null) {
4914 loge("isProvisioningRequiredForCapability: Device does not support IMS");
4915
4916 // device does not support IMS, this method will return false
4917 return false;
4918 }
4919 return controller.isImsProvisioningRequiredForCapability(subId, capability, tech);
joonhunshincffb7fc2021-11-28 07:32:01 +00004920 } finally {
4921 Binder.restoreCallingIdentity(identity);
Brad Ebinger1c8542e2019-01-14 13:43:14 -08004922 }
Brad Ebinger1c8542e2019-01-14 13:43:14 -08004923 }
4924
4925 @Override
joonhunshincffb7fc2021-11-28 07:32:01 +00004926 public boolean isRcsProvisioningRequiredForCapability(int subId, int capability, int tech) {
4927 TelephonyPermissions.enforceCallingOrSelfReadPrecisePhoneStatePermissionOrCarrierPrivilege(
4928 mApp, subId, "isProvisioningRequiredForCapability");
Brad Ebinger1c8542e2019-01-14 13:43:14 -08004929
joonhunshincffb7fc2021-11-28 07:32:01 +00004930 final long identity = Binder.clearCallingIdentity();
4931 try {
joonhunshin91bc1952022-04-29 08:47:15 +00004932 ImsProvisioningController controller = ImsProvisioningController.getInstance();
4933 if (controller == null) {
4934 loge("isRcsProvisioningRequiredForCapability: Device does not support IMS");
4935
4936 // device does not support IMS, this method will return false
4937 return false;
4938 }
4939 return controller.isRcsProvisioningRequiredForCapability(subId, capability, tech);
joonhunshincffb7fc2021-11-28 07:32:01 +00004940 } finally {
4941 Binder.restoreCallingIdentity(identity);
4942 }
Brad Ebinger1c8542e2019-01-14 13:43:14 -08004943 }
4944
Brad Ebinger1c8542e2019-01-14 13:43:14 -08004945 @Override
Brad Ebingerdf5b4f02018-10-31 11:24:17 -07004946 public int getImsProvisioningInt(int subId, int key) {
Brad Ebinger1c8542e2019-01-14 13:43:14 -08004947 if (!SubscriptionManager.isValidSubscriptionId(subId)) {
4948 throw new IllegalArgumentException("Invalid Subscription id '" + subId + "'");
4949 }
joonhunshincffb7fc2021-11-28 07:32:01 +00004950 TelephonyPermissions.enforceCallingOrSelfReadPrecisePhoneStatePermissionOrCarrierPrivilege(
4951 mApp, subId, "getImsProvisioningInt");
4952
Brad Ebingerdf5b4f02018-10-31 11:24:17 -07004953 final long identity = Binder.clearCallingIdentity();
4954 try {
4955 // TODO: Refactor to remove ImsManager dependence and query through ImsPhone directly.
Brad Ebinger1c8542e2019-01-14 13:43:14 -08004956 int slotId = getSlotIndex(subId);
4957 if (slotId <= SubscriptionManager.INVALID_SIM_SLOT_INDEX) {
4958 Log.w(LOG_TAG, "getImsProvisioningInt: called with an inactive subscription '"
4959 + subId + "' for key:" + key);
4960 return ImsConfigImplBase.CONFIG_RESULT_UNKNOWN;
4961 }
joonhunshincffb7fc2021-11-28 07:32:01 +00004962
joonhunshin91bc1952022-04-29 08:47:15 +00004963 ImsProvisioningController controller = ImsProvisioningController.getInstance();
4964 if (controller == null) {
4965 loge("getImsProvisioningInt: Device does not support IMS");
4966
4967 // device does not support IMS, this method will return CONFIG_RESULT_UNKNOWN.
4968 return ImsConfigImplBase.CONFIG_RESULT_UNKNOWN;
4969 }
4970 int retVal = controller.getProvisioningValue(subId, key);
joonhunshincffb7fc2021-11-28 07:32:01 +00004971 if (retVal != ImsConfigImplBase.CONFIG_RESULT_UNKNOWN) {
4972 return retVal;
4973 }
4974
calvinpanb5a34062021-02-08 19:59:36 +08004975 return ImsManager.getInstance(mApp, slotId).getConfigInt(key);
Brad Ebinger1ce9c432019-07-16 13:19:44 -07004976 } catch (com.android.ims.ImsException e) {
Brad Ebinger1c8542e2019-01-14 13:43:14 -08004977 Log.w(LOG_TAG, "getImsProvisioningInt: ImsService is not available for subscription '"
4978 + subId + "' for key:" + key);
4979 return ImsConfigImplBase.CONFIG_RESULT_UNKNOWN;
Brad Ebingerdf5b4f02018-10-31 11:24:17 -07004980 } finally {
4981 Binder.restoreCallingIdentity(identity);
4982 }
4983 }
4984
4985 @Override
4986 public String getImsProvisioningString(int subId, int key) {
Brad Ebinger1c8542e2019-01-14 13:43:14 -08004987 if (!SubscriptionManager.isValidSubscriptionId(subId)) {
4988 throw new IllegalArgumentException("Invalid Subscription id '" + subId + "'");
4989 }
joonhunshincffb7fc2021-11-28 07:32:01 +00004990 TelephonyPermissions.enforceCallingOrSelfReadPrecisePhoneStatePermissionOrCarrierPrivilege(
4991 mApp, subId, "getImsProvisioningString");
4992
Brad Ebingerdf5b4f02018-10-31 11:24:17 -07004993 final long identity = Binder.clearCallingIdentity();
4994 try {
4995 // TODO: Refactor to remove ImsManager dependence and query through ImsPhone directly.
Brad Ebinger1c8542e2019-01-14 13:43:14 -08004996 int slotId = getSlotIndex(subId);
4997 if (slotId <= SubscriptionManager.INVALID_SIM_SLOT_INDEX) {
4998 Log.w(LOG_TAG, "getImsProvisioningString: called for an inactive subscription id '"
4999 + subId + "' for key:" + key);
5000 return ProvisioningManager.STRING_QUERY_RESULT_ERROR_GENERIC;
5001 }
calvinpanb5a34062021-02-08 19:59:36 +08005002 return ImsManager.getInstance(mApp, slotId).getConfigString(key);
Brad Ebinger1ce9c432019-07-16 13:19:44 -07005003 } catch (com.android.ims.ImsException e) {
Brad Ebinger1c8542e2019-01-14 13:43:14 -08005004 Log.w(LOG_TAG, "getImsProvisioningString: ImsService is not available for sub '"
5005 + subId + "' for key:" + key);
5006 return ProvisioningManager.STRING_QUERY_RESULT_ERROR_NOT_READY;
Brad Ebingerdf5b4f02018-10-31 11:24:17 -07005007 } finally {
5008 Binder.restoreCallingIdentity(identity);
5009 }
5010 }
5011
5012 @Override
5013 public int setImsProvisioningInt(int subId, int key, int value) {
Brad Ebinger1c8542e2019-01-14 13:43:14 -08005014 if (!SubscriptionManager.isValidSubscriptionId(subId)) {
5015 throw new IllegalArgumentException("Invalid Subscription id '" + subId + "'");
5016 }
Brad Ebinger3d0b34e2018-11-15 14:13:12 -08005017 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(mApp, subId,
5018 "setImsProvisioningInt");
joonhunshincffb7fc2021-11-28 07:32:01 +00005019
Brad Ebingerdf5b4f02018-10-31 11:24:17 -07005020 final long identity = Binder.clearCallingIdentity();
5021 try {
5022 // TODO: Refactor to remove ImsManager dependence and query through ImsPhone directly.
Brad Ebinger1c8542e2019-01-14 13:43:14 -08005023 int slotId = getSlotIndex(subId);
5024 if (slotId <= SubscriptionManager.INVALID_SIM_SLOT_INDEX) {
5025 Log.w(LOG_TAG, "setImsProvisioningInt: called with an inactive subscription id '"
5026 + subId + "' for key:" + key);
5027 return ImsConfigImplBase.CONFIG_RESULT_FAILED;
5028 }
joonhunshincffb7fc2021-11-28 07:32:01 +00005029
joonhunshin91bc1952022-04-29 08:47:15 +00005030 ImsProvisioningController controller = ImsProvisioningController.getInstance();
5031 if (controller == null) {
5032 loge("setImsProvisioningInt: Device does not support IMS");
5033
5034 // device does not support IMS, this method will return CONFIG_RESULT_FAILED.
5035 return ImsConfigImplBase.CONFIG_RESULT_FAILED;
5036 }
5037 int retVal = controller.setProvisioningValue(subId, key, value);
joonhunshincffb7fc2021-11-28 07:32:01 +00005038 if (retVal != ImsConfigImplBase.CONFIG_RESULT_UNKNOWN) {
5039 return retVal;
5040 }
5041
calvinpanb5a34062021-02-08 19:59:36 +08005042 return ImsManager.getInstance(mApp, slotId).setConfig(key, value);
5043 } catch (com.android.ims.ImsException | RemoteException e) {
Brad Ebinger1c8542e2019-01-14 13:43:14 -08005044 Log.w(LOG_TAG, "setImsProvisioningInt: ImsService unavailable for sub '" + subId
calvinpanb5a34062021-02-08 19:59:36 +08005045 + "' for key:" + key, e);
Brad Ebinger1c8542e2019-01-14 13:43:14 -08005046 return ImsConfigImplBase.CONFIG_RESULT_FAILED;
Brad Ebingerdf5b4f02018-10-31 11:24:17 -07005047 } finally {
5048 Binder.restoreCallingIdentity(identity);
5049 }
5050 }
5051
5052 @Override
5053 public int setImsProvisioningString(int subId, int key, String value) {
Brad Ebinger1c8542e2019-01-14 13:43:14 -08005054 if (!SubscriptionManager.isValidSubscriptionId(subId)) {
5055 throw new IllegalArgumentException("Invalid Subscription id '" + subId + "'");
5056 }
Brad Ebinger3d0b34e2018-11-15 14:13:12 -08005057 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(mApp, subId,
5058 "setImsProvisioningString");
Brad Ebingerdf5b4f02018-10-31 11:24:17 -07005059 final long identity = Binder.clearCallingIdentity();
5060 try {
5061 // TODO: Refactor to remove ImsManager dependence and query through ImsPhone directly.
Brad Ebinger1c8542e2019-01-14 13:43:14 -08005062 int slotId = getSlotIndex(subId);
5063 if (slotId <= SubscriptionManager.INVALID_SIM_SLOT_INDEX) {
5064 Log.w(LOG_TAG, "setImsProvisioningString: called with an inactive subscription id '"
5065 + subId + "' for key:" + key);
5066 return ImsConfigImplBase.CONFIG_RESULT_FAILED;
5067 }
calvinpanb5a34062021-02-08 19:59:36 +08005068 return ImsManager.getInstance(mApp, slotId).setConfig(key, value);
5069 } catch (com.android.ims.ImsException | RemoteException e) {
Brad Ebinger1c8542e2019-01-14 13:43:14 -08005070 Log.w(LOG_TAG, "setImsProvisioningString: ImsService unavailable for sub '" + subId
calvinpanb5a34062021-02-08 19:59:36 +08005071 + "' for key:" + key, e);
Brad Ebinger1c8542e2019-01-14 13:43:14 -08005072 return ImsConfigImplBase.CONFIG_RESULT_FAILED;
Brad Ebingerdf5b4f02018-10-31 11:24:17 -07005073 } finally {
5074 Binder.restoreCallingIdentity(identity);
5075 }
5076 }
5077
Brad Ebinger919631e2021-06-02 17:46:35 -07005078 /**
5079 * Throw an ImsException if the IMS resolver does not have an ImsService configured for MMTEL
5080 * for the given slot ID or no ImsResolver instance has been created.
5081 * @param slotId The slot ID that the IMS service is created for.
5082 * @throws ImsException If there is no ImsService configured for this slot.
5083 */
5084 private void verifyImsMmTelConfiguredOrThrow(int slotId) throws ImsException {
5085 if (mImsResolver == null || !mImsResolver.isImsServiceConfiguredForFeature(slotId,
5086 ImsFeature.FEATURE_MMTEL)) {
5087 throw new ImsException("This subscription does not support MMTEL over IMS",
5088 ImsException.CODE_ERROR_UNSUPPORTED_OPERATION);
5089 }
5090 }
5091
Brad Ebinger1ce9c432019-07-16 13:19:44 -07005092 private int getSlotIndexOrException(int subId) throws ImsException {
Brad Ebinger35c841c2018-10-01 10:40:55 -07005093 int slotId = SubscriptionManager.getSlotIndex(subId);
5094 if (!SubscriptionManager.isValidSlotIndex(slotId)) {
Brad Ebinger1ce9c432019-07-16 13:19:44 -07005095 throw new ImsException("Invalid Subscription Id, subId=" + subId,
5096 ImsException.CODE_ERROR_INVALID_SUBSCRIPTION);
Brad Ebinger35c841c2018-10-01 10:40:55 -07005097 }
5098 return slotId;
5099 }
5100
Brad Ebinger1c8542e2019-01-14 13:43:14 -08005101 private int getSlotIndex(int subId) {
5102 int slotId = SubscriptionManager.getSlotIndex(subId);
5103 if (!SubscriptionManager.isValidSlotIndex(slotId)) {
5104 return SubscriptionManager.INVALID_SIM_SLOT_INDEX;
5105 }
5106 return slotId;
5107 }
5108
Wink Saville36469e72014-06-11 15:17:00 -07005109 /**
Nathan Harold9042f0b2019-05-21 15:51:27 -07005110 * Returns the data network type for a subId; does not throw SecurityException.
Wink Saville36469e72014-06-11 15:17:00 -07005111 */
5112 @Override
Philip P. Moltmann700a9592019-10-03 11:53:50 -07005113 public int getNetworkTypeForSubscriber(int subId, String callingPackage,
5114 String callingFeatureId) {
Shuo Qian13d89152021-05-10 23:58:11 -07005115 try {
5116 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
5117 } catch (SecurityException se) {
5118 EventLog.writeEvent(0x534e4554, "186776740", Binder.getCallingUid());
5119 throw new SecurityException("Package " + callingPackage + " does not belong to "
5120 + Binder.getCallingUid());
5121 }
Nathan Haroldf096d982020-11-18 17:18:06 -08005122 final int targetSdk = TelephonyPermissions.getTargetSdk(mApp, callingPackage);
Nathan Haroldef60dba2019-05-22 13:55:14 -07005123 if (targetSdk > android.os.Build.VERSION_CODES.Q) {
Philip P. Moltmann700a9592019-10-03 11:53:50 -07005124 return getDataNetworkTypeForSubscriber(subId, callingPackage, callingFeatureId);
Nathan Haroldef60dba2019-05-22 13:55:14 -07005125 } else if (targetSdk == android.os.Build.VERSION_CODES.Q
Nathan Harold9042f0b2019-05-21 15:51:27 -07005126 && !TelephonyPermissions.checkCallingOrSelfReadPhoneStateNoThrow(
Philip P. Moltmann700a9592019-10-03 11:53:50 -07005127 mApp, subId, callingPackage, callingFeatureId,
5128 "getNetworkTypeForSubscriber")) {
Robert Greenwalt36b23af2015-07-06 17:59:14 -07005129 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
5130 }
Robert Greenwalta5dcfcb2015-07-10 09:06:29 -07005131
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005132 final long identity = Binder.clearCallingIdentity();
5133 try {
5134 final Phone phone = getPhone(subId);
5135 if (phone != null) {
5136 return phone.getServiceState().getDataNetworkType();
5137 } else {
5138 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
5139 }
5140 } finally {
5141 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07005142 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07005143 }
5144
5145 /**
5146 * Returns the data network type
5147 */
5148 @Override
Philip P. Moltmann700a9592019-10-03 11:53:50 -07005149 public int getDataNetworkType(String callingPackage, String callingFeatureId) {
Zoey Chenfd61f7f2021-04-21 13:42:10 +08005150 return getDataNetworkTypeForSubscriber(mSubscriptionController.getDefaultDataSubId(),
5151 callingPackage, callingFeatureId);
Wink Saville36469e72014-06-11 15:17:00 -07005152 }
5153
5154 /**
5155 * Returns the data network type for a subId
5156 */
5157 @Override
Philip P. Moltmann700a9592019-10-03 11:53:50 -07005158 public int getDataNetworkTypeForSubscriber(int subId, String callingPackage,
5159 String callingFeatureId) {
Sooraj Sasindran5d051bf2021-11-05 13:14:15 -07005160 String functionName = "getDataNetworkTypeForSubscriber";
5161 if (!TelephonyPermissions.checkCallingOrSelfReadNonDangerousPhoneStateNoThrow(
5162 mApp, functionName)) {
5163 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
5164 mApp, subId, callingPackage, callingFeatureId, functionName)) {
5165 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
5166 }
Robert Greenwalt36b23af2015-07-06 17:59:14 -07005167 }
5168
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005169 final long identity = Binder.clearCallingIdentity();
5170 try {
5171 final Phone phone = getPhone(subId);
5172 if (phone != null) {
5173 return phone.getServiceState().getDataNetworkType();
5174 } else {
5175 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
5176 }
5177 } finally {
5178 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07005179 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07005180 }
5181
5182 /**
Wink Saville36469e72014-06-11 15:17:00 -07005183 * Returns the Voice network type for a subId
5184 */
5185 @Override
Philip P. Moltmann700a9592019-10-03 11:53:50 -07005186 public int getVoiceNetworkTypeForSubscriber(int subId, String callingPackage,
5187 String callingFeatureId) {
Sooraj Sasindran5d051bf2021-11-05 13:14:15 -07005188 String functionName = "getVoiceNetworkTypeForSubscriber";
5189 if (!TelephonyPermissions.checkCallingOrSelfReadNonDangerousPhoneStateNoThrow(
5190 mApp, functionName)) {
5191 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
5192 mApp, subId, callingPackage, callingFeatureId, functionName)) {
5193 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
5194 }
Robert Greenwalta5dcfcb2015-07-10 09:06:29 -07005195 }
5196
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005197 final long identity = Binder.clearCallingIdentity();
5198 try {
5199 final Phone phone = getPhone(subId);
5200 if (phone != null) {
5201 return phone.getServiceState().getVoiceNetworkType();
5202 } else {
5203 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
5204 }
5205 } finally {
5206 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07005207 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07005208 }
5209
5210 /**
5211 * @return true if a ICC card is present
5212 */
5213 public boolean hasIccCard() {
Wink Saville36469e72014-06-11 15:17:00 -07005214 // FIXME Make changes to pass defaultSimId of type int
Sanket Padawe13bac7b2017-03-20 15:04:47 -07005215 return hasIccCardUsingSlotIndex(mSubscriptionController.getSlotIndex(
5216 getDefaultSubscription()));
Wink Saville36469e72014-06-11 15:17:00 -07005217 }
5218
5219 /**
Sanket Padawe13bac7b2017-03-20 15:04:47 -07005220 * @return true if a ICC card is present for a slotIndex
Wink Saville36469e72014-06-11 15:17:00 -07005221 */
Sanket Padawe356d7632015-06-22 14:03:32 -07005222 @Override
Sanket Padawe13bac7b2017-03-20 15:04:47 -07005223 public boolean hasIccCardUsingSlotIndex(int slotIndex) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005224 final long identity = Binder.clearCallingIdentity();
5225 try {
5226 final Phone phone = PhoneFactory.getPhone(slotIndex);
5227 if (phone != null) {
5228 return phone.getIccCard().hasIccCard();
5229 } else {
5230 return false;
5231 }
5232 } finally {
5233 Binder.restoreCallingIdentity(identity);
Amit Mahajana6fc2a82015-01-06 11:53:51 -08005234 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07005235 }
5236
5237 /**
5238 * Return if the current radio is LTE on CDMA. This
5239 * is a tri-state return value as for a period of time
5240 * the mode may be unknown.
5241 *
Robert Greenwalt36b23af2015-07-06 17:59:14 -07005242 * @param callingPackage the name of the package making the call.
Santos Cordon7d4ddf62013-07-10 11:58:08 -07005243 * @return {@link Phone#LTE_ON_CDMA_UNKNOWN}, {@link Phone#LTE_ON_CDMA_FALSE}
Jake Hambye994d462014-02-03 13:10:13 -08005244 * or {@link Phone#LTE_ON_CDMA_TRUE}
Santos Cordon7d4ddf62013-07-10 11:58:08 -07005245 */
Robert Greenwalt36b23af2015-07-06 17:59:14 -07005246 @Override
Philip P. Moltmann700a9592019-10-03 11:53:50 -07005247 public int getLteOnCdmaMode(String callingPackage, String callingFeatureId) {
5248 return getLteOnCdmaModeForSubscriber(getDefaultSubscription(), callingPackage,
5249 callingFeatureId);
Wink Saville36469e72014-06-11 15:17:00 -07005250 }
5251
Sanket Padawe356d7632015-06-22 14:03:32 -07005252 @Override
Philip P. Moltmann700a9592019-10-03 11:53:50 -07005253 public int getLteOnCdmaModeForSubscriber(int subId, String callingPackage,
5254 String callingFeatureId) {
Sarah Chin790d2922020-01-16 12:17:23 -08005255 try {
5256 enforceReadPrivilegedPermission("getLteOnCdmaModeForSubscriber");
5257 } catch (SecurityException e) {
Robert Greenwalt36b23af2015-07-06 17:59:14 -07005258 return PhoneConstants.LTE_ON_CDMA_UNKNOWN;
5259 }
5260
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005261 final long identity = Binder.clearCallingIdentity();
5262 try {
5263 final Phone phone = getPhone(subId);
5264 if (phone == null) {
5265 return PhoneConstants.LTE_ON_CDMA_UNKNOWN;
5266 } else {
Nathan Harold05ad6332020-07-10 11:54:36 -07005267 return TelephonyProperties.lte_on_cdma_device()
5268 .orElse(PhoneConstants.LTE_ON_CDMA_FALSE);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005269 }
5270 } finally {
5271 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07005272 }
Wink Saville36469e72014-06-11 15:17:00 -07005273 }
5274
Wink Saville36469e72014-06-11 15:17:00 -07005275 /**
5276 * {@hide}
5277 * Returns Default subId, 0 in the case of single standby.
5278 */
Wink Savilleb564aae2014-10-23 10:18:09 -07005279 private int getDefaultSubscription() {
Wink Savilleac1bdfd2014-11-20 23:04:44 -08005280 return mSubscriptionController.getDefaultSubId();
Wink Saville36469e72014-06-11 15:17:00 -07005281 }
5282
Shishir Agrawala9f32182016-04-12 12:00:16 -07005283 private int getSlotForDefaultSubscription() {
5284 return mSubscriptionController.getPhoneId(getDefaultSubscription());
5285 }
5286
Wink Savilleb564aae2014-10-23 10:18:09 -07005287 private int getPreferredVoiceSubscription() {
Wink Savilleac1bdfd2014-11-20 23:04:44 -08005288 return mSubscriptionController.getDefaultVoiceSubId();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07005289 }
Ihab Awadf2177b72013-11-25 13:33:23 -08005290
Pengquan Menge92a50d2018-09-21 15:54:48 -07005291 private boolean isActiveSubscription(int subId) {
5292 return mSubscriptionController.isActiveSubId(subId);
5293 }
5294
Ihab Awadf2177b72013-11-25 13:33:23 -08005295 /**
5296 * @see android.telephony.TelephonyManager.WifiCallingChoices
5297 */
5298 public int getWhenToMakeWifiCalls() {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005299 final long identity = Binder.clearCallingIdentity();
5300 try {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08005301 return Settings.System.getInt(mApp.getContentResolver(),
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005302 Settings.System.WHEN_TO_MAKE_WIFI_CALLS,
5303 getWhenToMakeWifiCallsDefaultPreference());
5304 } finally {
5305 Binder.restoreCallingIdentity(identity);
5306 }
Ihab Awadf2177b72013-11-25 13:33:23 -08005307 }
5308
5309 /**
5310 * @see android.telephony.TelephonyManager.WifiCallingChoices
5311 */
5312 public void setWhenToMakeWifiCalls(int preference) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005313 final long identity = Binder.clearCallingIdentity();
5314 try {
5315 if (DBG) log("setWhenToMakeWifiCallsStr, storing setting = " + preference);
Nazanin Bakhshif782e562018-12-11 15:15:39 -08005316 Settings.System.putInt(mApp.getContentResolver(),
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005317 Settings.System.WHEN_TO_MAKE_WIFI_CALLS, preference);
5318 } finally {
5319 Binder.restoreCallingIdentity(identity);
5320 }
Ihab Awadf9e92732013-12-05 18:02:52 -08005321 }
5322
Sailesh Nepald1e68152013-12-12 19:08:02 -08005323 private static int getWhenToMakeWifiCallsDefaultPreference() {
Santos Cordonda120f42014-08-06 04:44:34 -07005324 // TODO: Use a build property to choose this value.
Evan Charlton9829e882013-12-19 15:30:38 -08005325 return TelephonyManager.WifiCallingChoices.ALWAYS_USE;
Ihab Awadf2177b72013-11-25 13:33:23 -08005326 }
Shishir Agrawal69f68122013-12-16 17:25:49 -08005327
Muralidhar Reddybd38d952021-12-02 21:04:16 +00005328 private Phone getPhoneFromSlotPortIndexOrThrowException(int slotIndex, int portIndex) {
5329 int phoneId = UiccController.getInstance().getPhoneIdFromSlotPortIndex(slotIndex,
5330 portIndex);
Jordan Liu4c733742019-02-28 12:03:40 -08005331 if (phoneId == -1) {
Muralidhar Reddybd38d952021-12-02 21:04:16 +00005332 throw new IllegalArgumentException("Given slot index: " + slotIndex + " port index: "
5333 + portIndex + " does not correspond to an active phone");
Jordan Liu4c733742019-02-28 12:03:40 -08005334 }
5335 return PhoneFactory.getPhone(phoneId);
5336 }
5337
Shishir Agrawal566b7612013-10-28 14:41:00 -07005338 @Override
Derek Tan740e1672017-06-27 14:56:27 -07005339 public IccOpenLogicalChannelResponse iccOpenLogicalChannel(
Rambo Wanga1782702021-11-10 20:15:19 -08005340 @NonNull IccLogicalChannelRequest request) {
5341 Phone phone = getPhoneFromValidIccLogicalChannelRequest(request,
5342 /*message=*/ "iccOpenLogicalChannel");
5343
5344 if (DBG) log("iccOpenLogicalChannel: request=" + request);
5345 // Verify that the callingPackage in the request belongs to the calling UID
5346 mAppOps.checkPackage(Binder.getCallingUid(), request.callingPackage);
5347
5348 return iccOpenLogicalChannelWithPermission(phone, request);
Jordan Liu4c733742019-02-28 12:03:40 -08005349 }
Shishir Agrawal566b7612013-10-28 14:41:00 -07005350
Rambo Wanga1782702021-11-10 20:15:19 -08005351 private Phone getPhoneFromValidIccLogicalChannelRequest(
5352 @NonNull IccLogicalChannelRequest request, String message) {
5353 Phone phone;
5354 if (request.subId != SubscriptionManager.INVALID_SUBSCRIPTION_ID) {
5355 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
5356 mApp, request.subId, message);
5357 phone = getPhoneFromSubId(request.subId);
5358 } else if (request.slotIndex != SubscriptionManager.INVALID_SIM_SLOT_INDEX) {
5359 enforceModifyPermission();
5360 phone = getPhoneFromSlotPortIndexOrThrowException(request.slotIndex, request.portIndex);
5361 } else {
5362 throw new IllegalArgumentException("Both subId and slotIndex in request are invalid.");
Jordan Liu4c733742019-02-28 12:03:40 -08005363 }
Rambo Wanga1782702021-11-10 20:15:19 -08005364 return phone;
Jordan Liu4c733742019-02-28 12:03:40 -08005365 }
5366
5367 private IccOpenLogicalChannelResponse iccOpenLogicalChannelWithPermission(Phone phone,
Rambo Wanga1782702021-11-10 20:15:19 -08005368 IccLogicalChannelRequest channelRequest) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005369 final long identity = Binder.clearCallingIdentity();
5370 try {
Rambo Wanga1782702021-11-10 20:15:19 -08005371 if (TextUtils.equals(ISDR_AID, channelRequest.aid)) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005372 // Only allows LPA to open logical channel to ISD-R.
Nazanin Bakhshif782e562018-12-11 15:15:39 -08005373 ComponentInfo bestComponent = EuiccConnector.findBestComponent(getDefaultPhone()
5374 .getContext().getPackageManager());
Rambo Wanga1782702021-11-10 20:15:19 -08005375 if (bestComponent == null || !TextUtils.equals(channelRequest.callingPackage,
5376 bestComponent.packageName)) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005377 loge("The calling package is not allowed to access ISD-R.");
5378 throw new SecurityException(
5379 "The calling package is not allowed to access ISD-R.");
5380 }
Derek Tan740e1672017-06-27 14:56:27 -07005381 }
Derek Tan740e1672017-06-27 14:56:27 -07005382
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005383 IccOpenLogicalChannelResponse response = (IccOpenLogicalChannelResponse) sendRequest(
Rambo Wanga1782702021-11-10 20:15:19 -08005384 CMD_OPEN_CHANNEL, channelRequest, phone, null /* workSource */);
5385 if (DBG) log("iccOpenLogicalChannelWithPermission: response=" + response);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005386 return response;
5387 } finally {
5388 Binder.restoreCallingIdentity(identity);
5389 }
Shishir Agrawal566b7612013-10-28 14:41:00 -07005390 }
5391
5392 @Override
Rambo Wanga1782702021-11-10 20:15:19 -08005393 public boolean iccCloseLogicalChannel(@NonNull IccLogicalChannelRequest request) {
5394 Phone phone = getPhoneFromValidIccLogicalChannelRequest(request,
5395 /*message=*/"iccCloseLogicalChannel");
5396
5397 if (DBG) log("iccCloseLogicalChannel: request=" + request);
5398
5399 return iccCloseLogicalChannelWithPermission(phone, request);
Jordan Liu4c733742019-02-28 12:03:40 -08005400 }
Shishir Agrawal566b7612013-10-28 14:41:00 -07005401
Rambo Wanga1782702021-11-10 20:15:19 -08005402 private boolean iccCloseLogicalChannelWithPermission(Phone phone,
5403 IccLogicalChannelRequest request) {
Chen Xua8f0dff2022-02-12 00:34:15 -08005404 // before this feature is enabled, this API should only return false if
5405 // the operation fails instead of throwing runtime exception for
5406 // backward-compatibility.
5407 final boolean shouldThrowExceptionOnFailure = CompatChanges.isChangeEnabled(
5408 ICC_CLOSE_CHANNEL_EXCEPTION_ON_FAILURE, Binder.getCallingUid());
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005409 final long identity = Binder.clearCallingIdentity();
5410 try {
Rambo Wanga1782702021-11-10 20:15:19 -08005411 if (request.channel < 0) {
Chen Xu540470b2021-12-14 17:15:47 -08005412 throw new IllegalArgumentException("request.channel is less than 0");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005413 }
Chen Xue9d737e2022-01-01 23:41:31 -08005414 Object result = sendRequest(CMD_CLOSE_CHANNEL, request.channel, phone,
Jordan Liu4c733742019-02-28 12:03:40 -08005415 null /* workSource */);
Chen Xue9d737e2022-01-01 23:41:31 -08005416 Boolean success = false;
5417 if (result instanceof RuntimeException) {
5418 // if there is an exception returned, throw from the binder thread here.
Chen Xua8f0dff2022-02-12 00:34:15 -08005419 if (shouldThrowExceptionOnFailure) {
5420 throw (RuntimeException) result;
5421 } else {
5422 return false;
5423 }
Chen Xue9d737e2022-01-01 23:41:31 -08005424 } else if (result instanceof Boolean) {
5425 success = (Boolean) result;
5426 } else {
5427 loge("iccCloseLogicalChannelWithPermission: supported return type " + result);
5428 }
Rambo Wanga1782702021-11-10 20:15:19 -08005429 if (DBG) log("iccCloseLogicalChannelWithPermission: success=" + success);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005430 return success;
5431 } finally {
5432 Binder.restoreCallingIdentity(identity);
Shishir Agrawal566b7612013-10-28 14:41:00 -07005433 }
Shishir Agrawal566b7612013-10-28 14:41:00 -07005434 }
5435
5436 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08005437 public String iccTransmitApduLogicalChannel(int subId, int channel, int cla,
Shishir Agrawal566b7612013-10-28 14:41:00 -07005438 int command, int p1, int p2, int p3, String data) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08005439 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
5440 mApp, subId, "iccTransmitApduLogicalChannel");
Jordan Liu4c733742019-02-28 12:03:40 -08005441 if (DBG) {
5442 log("iccTransmitApduLogicalChannel: subId=" + subId + " chnl=" + channel
5443 + " cla=" + cla + " cmd=" + command + " p1=" + p1 + " p2=" + p2 + " p3="
5444 + p3 + " data=" + data);
5445 }
5446 return iccTransmitApduLogicalChannelWithPermission(getPhoneFromSubId(subId), channel, cla,
5447 command, p1, p2, p3, data);
5448 }
Shishir Agrawal566b7612013-10-28 14:41:00 -07005449
Jordan Liu4c733742019-02-28 12:03:40 -08005450 @Override
Muralidhar Reddybd38d952021-12-02 21:04:16 +00005451 public String iccTransmitApduLogicalChannelByPort(int slotIndex, int portIndex, int channel,
5452 int cla, int command, int p1, int p2, int p3, String data) {
Jordan Liu4c733742019-02-28 12:03:40 -08005453 enforceModifyPermission();
5454 if (DBG) {
Muralidhar Reddybd38d952021-12-02 21:04:16 +00005455 log("iccTransmitApduLogicalChannelByPort: slotIndex=" + slotIndex + " portIndex="
5456 + portIndex + " chnl=" + channel + " cla=" + cla + " cmd=" + command + " p1="
5457 + p1 + " p2=" + p2 + " p3=" + p3 + " data=" + data);
Jordan Liu4c733742019-02-28 12:03:40 -08005458 }
5459 return iccTransmitApduLogicalChannelWithPermission(
Muralidhar Reddybd38d952021-12-02 21:04:16 +00005460 getPhoneFromSlotPortIndexOrThrowException(slotIndex, portIndex), channel, cla,
5461 command, p1, p2, p3, data);
Jordan Liu4c733742019-02-28 12:03:40 -08005462 }
5463
5464 private String iccTransmitApduLogicalChannelWithPermission(Phone phone, int channel, int cla,
5465 int command, int p1, int p2, int p3, String data) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005466 final long identity = Binder.clearCallingIdentity();
5467 try {
Hall Liu4fd771b2019-05-02 09:16:29 -07005468 if (channel <= 0) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005469 return "";
5470 }
Shishir Agrawal566b7612013-10-28 14:41:00 -07005471
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005472 IccIoResult response = (IccIoResult) sendRequest(CMD_TRANSMIT_APDU_LOGICAL_CHANNEL,
Jordan Liu4c733742019-02-28 12:03:40 -08005473 new IccAPDUArgument(channel, cla, command, p1, p2, p3, data), phone,
5474 null /* workSource */);
5475 if (DBG) log("iccTransmitApduLogicalChannelWithPermission: " + response);
Shishir Agrawal566b7612013-10-28 14:41:00 -07005476
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005477 // Append the returned status code to the end of the response payload.
5478 String s = Integer.toHexString(
5479 (response.sw1 << 8) + response.sw2 + 0x10000).substring(1);
5480 if (response.payload != null) {
5481 s = IccUtils.bytesToHexString(response.payload) + s;
5482 }
5483 return s;
5484 } finally {
5485 Binder.restoreCallingIdentity(identity);
Shishir Agrawal5ec14172014-08-05 17:05:45 -07005486 }
Shishir Agrawal566b7612013-10-28 14:41:00 -07005487 }
Jake Hambye994d462014-02-03 13:10:13 -08005488
Evan Charltonc66da362014-05-16 14:06:40 -07005489 @Override
Holly Jiuyu Sun1cc2d552018-01-26 15:51:16 -08005490 public String iccTransmitApduBasicChannel(int subId, String callingPackage, int cla,
5491 int command, int p1, int p2, int p3, String data) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08005492 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
5493 mApp, subId, "iccTransmitApduBasicChannel");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005494 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
Jordan Liu4c733742019-02-28 12:03:40 -08005495 if (DBG) {
5496 log("iccTransmitApduBasicChannel: subId=" + subId + " cla=" + cla + " cmd="
5497 + command + " p1=" + p1 + " p2=" + p2 + " p3=" + p3 + " data=" + data);
5498 }
5499 return iccTransmitApduBasicChannelWithPermission(getPhoneFromSubId(subId), callingPackage,
5500 cla, command, p1, p2, p3, data);
5501 }
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07005502
Jordan Liu4c733742019-02-28 12:03:40 -08005503 @Override
Muralidhar Reddybd38d952021-12-02 21:04:16 +00005504 public String iccTransmitApduBasicChannelByPort(int slotIndex, int portIndex,
5505 String callingPackage, int cla, int command, int p1, int p2, int p3, String data) {
Jordan Liu4c733742019-02-28 12:03:40 -08005506 enforceModifyPermission();
5507 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
5508 if (DBG) {
Muralidhar Reddybd38d952021-12-02 21:04:16 +00005509 log("iccTransmitApduBasicChannelByPort: slotIndex=" + slotIndex + " portIndex="
5510 + portIndex + " cla=" + cla + " cmd=" + command + " p1=" + p1 + " p2="
5511 + p2 + " p3=" + p3 + " data=" + data);
Jordan Liu4c733742019-02-28 12:03:40 -08005512 }
5513
5514 return iccTransmitApduBasicChannelWithPermission(
Muralidhar Reddybd38d952021-12-02 21:04:16 +00005515 getPhoneFromSlotPortIndexOrThrowException(slotIndex, portIndex), callingPackage,
5516 cla, command, p1, p2, p3, data);
Jordan Liu4c733742019-02-28 12:03:40 -08005517 }
5518
5519 // open APDU basic channel assuming the caller has sufficient permissions
5520 private String iccTransmitApduBasicChannelWithPermission(Phone phone, String callingPackage,
5521 int cla, int command, int p1, int p2, int p3, String data) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005522 final long identity = Binder.clearCallingIdentity();
5523 try {
5524 if (command == SELECT_COMMAND && p1 == SELECT_P1 && p2 == SELECT_P2 && p3 == SELECT_P3
5525 && TextUtils.equals(ISDR_AID, data)) {
5526 // Only allows LPA to select ISD-R.
Nazanin Bakhshif782e562018-12-11 15:15:39 -08005527 ComponentInfo bestComponent = EuiccConnector.findBestComponent(getDefaultPhone()
5528 .getContext().getPackageManager());
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005529 if (bestComponent == null
5530 || !TextUtils.equals(callingPackage, bestComponent.packageName)) {
5531 loge("The calling package is not allowed to select ISD-R.");
5532 throw new SecurityException(
5533 "The calling package is not allowed to select ISD-R.");
5534 }
Holly Jiuyu Sun1cc2d552018-01-26 15:51:16 -08005535 }
Holly Jiuyu Sun1cc2d552018-01-26 15:51:16 -08005536
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005537 IccIoResult response = (IccIoResult) sendRequest(CMD_TRANSMIT_APDU_BASIC_CHANNEL,
Jordan Liu4c733742019-02-28 12:03:40 -08005538 new IccAPDUArgument(0, cla, command, p1, p2, p3, data), phone,
5539 null /* workSource */);
5540 if (DBG) log("iccTransmitApduBasicChannelWithPermission: " + response);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07005541
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005542 // Append the returned status code to the end of the response payload.
5543 String s = Integer.toHexString(
5544 (response.sw1 << 8) + response.sw2 + 0x10000).substring(1);
5545 if (response.payload != null) {
5546 s = IccUtils.bytesToHexString(response.payload) + s;
5547 }
5548 return s;
5549 } finally {
5550 Binder.restoreCallingIdentity(identity);
Shishir Agrawal5ec14172014-08-05 17:05:45 -07005551 }
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07005552 }
5553
5554 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08005555 public byte[] iccExchangeSimIO(int subId, int fileID, int command, int p1, int p2, int p3,
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07005556 String filePath) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08005557 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
5558 mApp, subId, "iccExchangeSimIO");
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07005559
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005560 final long identity = Binder.clearCallingIdentity();
5561 try {
5562 if (DBG) {
5563 log("Exchange SIM_IO " + subId + ":" + fileID + ":" + command + " "
5564 + p1 + " " + p2 + " " + p3 + ":" + filePath);
5565 }
5566
5567 IccIoResult response =
5568 (IccIoResult) sendRequest(CMD_EXCHANGE_SIM_IO,
5569 new IccAPDUArgument(-1, fileID, command, p1, p2, p3, filePath),
5570 subId);
5571
5572 if (DBG) {
5573 log("Exchange SIM_IO [R]" + response);
5574 }
5575
5576 byte[] result = null;
5577 int length = 2;
5578 if (response.payload != null) {
5579 length = 2 + response.payload.length;
5580 result = new byte[length];
5581 System.arraycopy(response.payload, 0, result, 0, response.payload.length);
5582 } else {
5583 result = new byte[length];
5584 }
5585
5586 result[length - 1] = (byte) response.sw2;
5587 result[length - 2] = (byte) response.sw1;
5588 return result;
5589 } finally {
5590 Binder.restoreCallingIdentity(identity);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07005591 }
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07005592 }
5593
Nathan Haroldb3014052017-01-25 15:57:32 -08005594 /**
5595 * Get the forbidden PLMN List from the given app type (ex APPTYPE_USIM)
5596 * on a particular subscription
5597 */
Philip P. Moltmann700a9592019-10-03 11:53:50 -07005598 public String[] getForbiddenPlmns(int subId, int appType, String callingPackage,
5599 String callingFeatureId) {
sqianb6e41952018-03-12 14:54:01 -07005600 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
Philip P. Moltmann700a9592019-10-03 11:53:50 -07005601 mApp, subId, callingPackage, callingFeatureId, "getForbiddenPlmns")) {
sqianb6e41952018-03-12 14:54:01 -07005602 return null;
5603 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005604
5605 final long identity = Binder.clearCallingIdentity();
5606 try {
5607 if (appType != TelephonyManager.APPTYPE_USIM
5608 && appType != TelephonyManager.APPTYPE_SIM) {
5609 loge("getForbiddenPlmnList(): App Type must be USIM or SIM");
5610 return null;
5611 }
5612 Object response = sendRequest(
5613 CMD_GET_FORBIDDEN_PLMNS, new Integer(appType), subId);
5614 if (response instanceof String[]) {
5615 return (String[]) response;
5616 }
yincheng zhao2737e882019-09-06 17:06:54 -07005617 // Response is an Exception of some kind
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005618 // which is signalled to the user as a NULL retval
Nathan Haroldb3014052017-01-25 15:57:32 -08005619 return null;
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005620 } finally {
5621 Binder.restoreCallingIdentity(identity);
Nathan Haroldb3014052017-01-25 15:57:32 -08005622 }
Nathan Haroldb3014052017-01-25 15:57:32 -08005623 }
5624
yincheng zhao2737e882019-09-06 17:06:54 -07005625 /**
5626 * Set the forbidden PLMN list from the given app type (ex APPTYPE_USIM) on a particular
5627 * subscription.
5628 *
5629 * @param subId the id of the subscription.
5630 * @param appType the uicc app type, must be USIM or SIM.
5631 * @param fplmns the Forbiden plmns list that needed to be written to the SIM.
5632 * @param callingPackage the op Package name.
Philip P. Moltmann700a9592019-10-03 11:53:50 -07005633 * @param callingFeatureId the feature in the package.
yincheng zhao2737e882019-09-06 17:06:54 -07005634 * @return number of fplmns that is successfully written to the SIM.
5635 */
Philip P. Moltmann700a9592019-10-03 11:53:50 -07005636 public int setForbiddenPlmns(int subId, int appType, List<String> fplmns, String callingPackage,
5637 String callingFeatureId) {
Jayachandran C5b0d75a2021-10-21 22:15:27 -07005638 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
5639 mApp, subId, "setForbiddenPlmns");
5640
yincheng zhao2737e882019-09-06 17:06:54 -07005641 if (appType != TelephonyManager.APPTYPE_USIM && appType != TelephonyManager.APPTYPE_SIM) {
5642 loge("setForbiddenPlmnList(): App Type must be USIM or SIM");
5643 throw new IllegalArgumentException("Invalid appType: App Type must be USIM or SIM");
5644 }
5645 if (fplmns == null) {
5646 throw new IllegalArgumentException("Fplmn List provided is null");
5647 }
5648 for (String fplmn : fplmns) {
5649 if (!CellIdentity.isValidPlmn(fplmn)) {
5650 throw new IllegalArgumentException("Invalid fplmn provided: " + fplmn);
5651 }
5652 }
5653 final long identity = Binder.clearCallingIdentity();
5654 try {
5655 Object response = sendRequest(
5656 CMD_SET_FORBIDDEN_PLMNS,
5657 new Pair<Integer, List<String>>(new Integer(appType), fplmns),
5658 subId);
5659 return (int) response;
5660 } finally {
5661 Binder.restoreCallingIdentity(identity);
5662 }
5663 }
5664
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07005665 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08005666 public String sendEnvelopeWithStatus(int subId, String content) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08005667 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
5668 mApp, subId, "sendEnvelopeWithStatus");
Evan Charltonc66da362014-05-16 14:06:40 -07005669
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005670 final long identity = Binder.clearCallingIdentity();
5671 try {
5672 IccIoResult response = (IccIoResult) sendRequest(CMD_SEND_ENVELOPE, content, subId);
5673 if (response.payload == null) {
5674 return "";
5675 }
Evan Charltonc66da362014-05-16 14:06:40 -07005676
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005677 // Append the returned status code to the end of the response payload.
5678 String s = Integer.toHexString(
5679 (response.sw1 << 8) + response.sw2 + 0x10000).substring(1);
5680 s = IccUtils.bytesToHexString(response.payload) + s;
5681 return s;
5682 } finally {
5683 Binder.restoreCallingIdentity(identity);
5684 }
Evan Charltonc66da362014-05-16 14:06:40 -07005685 }
5686
Jake Hambye994d462014-02-03 13:10:13 -08005687 /**
5688 * Read one of the NV items defined in {@link com.android.internal.telephony.RadioNVItems}
5689 * and {@code ril_nv_items.h}. Used for device configuration by some CDMA operators.
5690 *
5691 * @param itemID the ID of the item to read
5692 * @return the NV item as a String, or null on error.
5693 */
5694 @Override
5695 public String nvReadItem(int itemID) {
vagdeviaf9a5b92018-08-15 16:01:53 -07005696 WorkSource workSource = getWorkSource(Binder.getCallingUid());
Jeff Davidson7e17e312018-02-13 18:17:36 -08005697 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
5698 mApp, getDefaultSubscription(), "nvReadItem");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005699
5700 final long identity = Binder.clearCallingIdentity();
5701 try {
5702 if (DBG) log("nvReadItem: item " + itemID);
vagdeviaf9a5b92018-08-15 16:01:53 -07005703 String value = (String) sendRequest(CMD_NV_READ_ITEM, itemID, workSource);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005704 if (DBG) log("nvReadItem: item " + itemID + " is \"" + value + '"');
5705 return value;
5706 } finally {
5707 Binder.restoreCallingIdentity(identity);
5708 }
Jake Hambye994d462014-02-03 13:10:13 -08005709 }
5710
5711 /**
5712 * Write one of the NV items defined in {@link com.android.internal.telephony.RadioNVItems}
5713 * and {@code ril_nv_items.h}. Used for device configuration by some CDMA operators.
5714 *
5715 * @param itemID the ID of the item to read
5716 * @param itemValue the value to write, as a String
5717 * @return true on success; false on any failure
5718 */
5719 @Override
5720 public boolean nvWriteItem(int itemID, String itemValue) {
vagdeviaf9a5b92018-08-15 16:01:53 -07005721 WorkSource workSource = getWorkSource(Binder.getCallingUid());
Jeff Davidson7e17e312018-02-13 18:17:36 -08005722 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
5723 mApp, getDefaultSubscription(), "nvWriteItem");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005724
5725 final long identity = Binder.clearCallingIdentity();
5726 try {
5727 if (DBG) log("nvWriteItem: item " + itemID + " value \"" + itemValue + '"');
5728 Boolean success = (Boolean) sendRequest(CMD_NV_WRITE_ITEM,
vagdeviaf9a5b92018-08-15 16:01:53 -07005729 new Pair<Integer, String>(itemID, itemValue), workSource);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005730 if (DBG) log("nvWriteItem: item " + itemID + ' ' + (success ? "ok" : "fail"));
5731 return success;
5732 } finally {
5733 Binder.restoreCallingIdentity(identity);
5734 }
Jake Hambye994d462014-02-03 13:10:13 -08005735 }
5736
5737 /**
5738 * Update the CDMA Preferred Roaming List (PRL) in the radio NV storage.
5739 * Used for device configuration by some CDMA operators.
5740 *
5741 * @param preferredRoamingList byte array containing the new PRL
5742 * @return true on success; false on any failure
5743 */
5744 @Override
5745 public boolean nvWriteCdmaPrl(byte[] preferredRoamingList) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08005746 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
5747 mApp, getDefaultSubscription(), "nvWriteCdmaPrl");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005748
5749 final long identity = Binder.clearCallingIdentity();
5750 try {
5751 if (DBG) log("nvWriteCdmaPrl: value: " + HexDump.toHexString(preferredRoamingList));
5752 Boolean success = (Boolean) sendRequest(CMD_NV_WRITE_CDMA_PRL, preferredRoamingList);
5753 if (DBG) log("nvWriteCdmaPrl: " + (success ? "ok" : "fail"));
5754 return success;
5755 } finally {
5756 Binder.restoreCallingIdentity(identity);
5757 }
Jake Hambye994d462014-02-03 13:10:13 -08005758 }
5759
5760 /**
chen xu6dac5ab2018-10-26 17:39:23 -07005761 * Rollback modem configurations to factory default except some config which are in whitelist.
Jake Hambye994d462014-02-03 13:10:13 -08005762 * Used for device configuration by some CDMA operators.
5763 *
chen xu6dac5ab2018-10-26 17:39:23 -07005764 * @param slotIndex - device slot.
5765 *
Jake Hambye994d462014-02-03 13:10:13 -08005766 * @return true on success; false on any failure
5767 */
5768 @Override
chen xu6dac5ab2018-10-26 17:39:23 -07005769 public boolean resetModemConfig(int slotIndex) {
5770 Phone phone = PhoneFactory.getPhone(slotIndex);
5771 if (phone != null) {
5772 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
5773 mApp, phone.getSubId(), "resetModemConfig");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005774
chen xu6dac5ab2018-10-26 17:39:23 -07005775 final long identity = Binder.clearCallingIdentity();
5776 try {
5777 Boolean success = (Boolean) sendRequest(CMD_RESET_MODEM_CONFIG, null);
5778 if (DBG) log("resetModemConfig:" + ' ' + (success ? "ok" : "fail"));
5779 return success;
5780 } finally {
5781 Binder.restoreCallingIdentity(identity);
5782 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005783 }
chen xu6dac5ab2018-10-26 17:39:23 -07005784 return false;
5785 }
5786
5787 /**
5788 * Generate a radio modem reset. Used for device configuration by some CDMA operators.
5789 *
5790 * @param slotIndex - device slot.
5791 *
5792 * @return true on success; false on any failure
5793 */
5794 @Override
5795 public boolean rebootModem(int slotIndex) {
5796 Phone phone = PhoneFactory.getPhone(slotIndex);
5797 if (phone != null) {
5798 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
5799 mApp, phone.getSubId(), "rebootModem");
5800
5801 final long identity = Binder.clearCallingIdentity();
5802 try {
5803 Boolean success = (Boolean) sendRequest(CMD_MODEM_REBOOT, null);
5804 if (DBG) log("rebootModem:" + ' ' + (success ? "ok" : "fail"));
5805 return success;
5806 } finally {
5807 Binder.restoreCallingIdentity(identity);
5808 }
5809 }
5810 return false;
Jake Hambye994d462014-02-03 13:10:13 -08005811 }
Jake Hamby7c27be32014-03-03 13:25:59 -08005812
Brad Ebinger51f743a2017-01-23 13:50:20 -08005813 /**
Grace Jiaaa2eb6b2020-01-09 16:26:08 -08005814 * Toggle IMS disable and enable for the framework to reset it. See {@link #enableIms(int)} and
5815 * {@link #disableIms(int)}.
5816 * @param slotIndex device slot.
5817 */
5818 public void resetIms(int slotIndex) {
5819 enforceModifyPermission();
5820
5821 final long identity = Binder.clearCallingIdentity();
5822 try {
5823 if (mImsResolver == null) {
5824 // may happen if the does not support IMS.
5825 return;
5826 }
Hyunhoa17ac7c2022-08-30 12:03:04 +00005827 mImsResolver.resetIms(slotIndex);
Grace Jiaaa2eb6b2020-01-09 16:26:08 -08005828 } finally {
5829 Binder.restoreCallingIdentity(identity);
5830 }
5831 }
5832
5833 /**
Brad Ebinger22bc3e42018-01-16 09:39:35 -08005834 * Enables IMS for the framework. This will trigger IMS registration and ImsFeature capability
5835 * status updates, if not already enabled.
Brad Ebinger51f743a2017-01-23 13:50:20 -08005836 */
Brad Ebinger22bc3e42018-01-16 09:39:35 -08005837 public void enableIms(int slotId) {
Brad Ebinger51f743a2017-01-23 13:50:20 -08005838 enforceModifyPermission();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005839
5840 final long identity = Binder.clearCallingIdentity();
5841 try {
Brad Ebinger24c29992019-12-05 13:03:21 -08005842 if (mImsResolver == null) {
Brad Ebinger9c0eb502019-01-23 15:06:19 -08005843 // may happen if the device does not support IMS.
5844 return;
5845 }
Brad Ebinger24c29992019-12-05 13:03:21 -08005846 mImsResolver.enableIms(slotId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005847 } finally {
5848 Binder.restoreCallingIdentity(identity);
5849 }
Brad Ebinger34bef922017-11-09 10:27:08 -08005850 }
5851
5852 /**
Brad Ebinger22bc3e42018-01-16 09:39:35 -08005853 * Disables IMS for the framework. This will trigger IMS de-registration and trigger ImsFeature
5854 * status updates to disabled.
Brad Ebinger34bef922017-11-09 10:27:08 -08005855 */
Brad Ebinger22bc3e42018-01-16 09:39:35 -08005856 public void disableIms(int slotId) {
5857 enforceModifyPermission();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005858
5859 final long identity = Binder.clearCallingIdentity();
5860 try {
Brad Ebinger24c29992019-12-05 13:03:21 -08005861 if (mImsResolver == null) {
Brad Ebinger9c0eb502019-01-23 15:06:19 -08005862 // may happen if the device does not support IMS.
5863 return;
5864 }
Brad Ebinger24c29992019-12-05 13:03:21 -08005865 mImsResolver.disableIms(slotId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005866 } finally {
5867 Binder.restoreCallingIdentity(identity);
5868 }
Brad Ebinger22bc3e42018-01-16 09:39:35 -08005869 }
5870
5871 /**
Brad Ebinger67b3e042020-09-11 12:45:11 -07005872 * Registers for updates to the MmTelFeature connection through the IImsServiceFeatureCallback
5873 * callback.
Brad Ebinger22bc3e42018-01-16 09:39:35 -08005874 */
Brad Ebinger67b3e042020-09-11 12:45:11 -07005875 @Override
Brad Ebingerf6aca002020-10-01 13:51:05 -07005876 public void registerMmTelFeatureCallback(int slotId, IImsServiceFeatureCallback callback) {
Brad Ebinger34bef922017-11-09 10:27:08 -08005877 enforceModifyPermission();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005878
5879 final long identity = Binder.clearCallingIdentity();
5880 try {
Brad Ebinger24c29992019-12-05 13:03:21 -08005881 if (mImsResolver == null) {
Brad Ebinger67b3e042020-09-11 12:45:11 -07005882 throw new ServiceSpecificException(ImsException.CODE_ERROR_UNSUPPORTED_OPERATION,
5883 "Device does not support IMS");
Brad Ebinger9c0eb502019-01-23 15:06:19 -08005884 }
Brad Ebingerf6aca002020-10-01 13:51:05 -07005885 mImsResolver.listenForFeature(slotId, ImsFeature.FEATURE_MMTEL, callback);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005886 } finally {
5887 Binder.restoreCallingIdentity(identity);
5888 }
Brad Ebinger34bef922017-11-09 10:27:08 -08005889 }
Brad Ebinger5f64b052017-12-14 14:26:15 -08005890 /**
Brad Ebinger075ff3a2020-05-18 17:52:58 -07005891 * Unregister a previously registered IImsServiceFeatureCallback associated with an ImsFeature.
5892 */
Brad Ebinger67b3e042020-09-11 12:45:11 -07005893 @Override
5894 public void unregisterImsFeatureCallback(IImsServiceFeatureCallback callback) {
Brad Ebinger075ff3a2020-05-18 17:52:58 -07005895 enforceModifyPermission();
5896
5897 final long identity = Binder.clearCallingIdentity();
5898 try {
5899 if (mImsResolver == null) return;
Brad Ebinger67b3e042020-09-11 12:45:11 -07005900 mImsResolver.unregisterImsFeatureCallback(callback);
Brad Ebinger075ff3a2020-05-18 17:52:58 -07005901 } finally {
5902 Binder.restoreCallingIdentity(identity);
5903 }
5904 }
5905
5906 /**
Brad Ebinger5f64b052017-12-14 14:26:15 -08005907 * Returns the {@link IImsRegistration} structure associated with the slotId and feature
Brad Ebinger9c0eb502019-01-23 15:06:19 -08005908 * specified or null if IMS is not supported on the slot specified.
Brad Ebinger5f64b052017-12-14 14:26:15 -08005909 */
5910 public IImsRegistration getImsRegistration(int slotId, int feature) throws RemoteException {
5911 enforceModifyPermission();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005912
5913 final long identity = Binder.clearCallingIdentity();
5914 try {
Brad Ebinger24c29992019-12-05 13:03:21 -08005915 if (mImsResolver == null) {
Brad Ebinger9c0eb502019-01-23 15:06:19 -08005916 // may happen if the device does not support IMS.
5917 return null;
5918 }
Brad Ebinger24c29992019-12-05 13:03:21 -08005919 return mImsResolver.getImsRegistration(slotId, feature);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005920 } finally {
5921 Binder.restoreCallingIdentity(identity);
5922 }
Brad Ebinger5f64b052017-12-14 14:26:15 -08005923 }
5924
Brad Ebinger22bc3e42018-01-16 09:39:35 -08005925 /**
5926 * Returns the {@link IImsConfig} structure associated with the slotId and feature
Brad Ebinger9c0eb502019-01-23 15:06:19 -08005927 * specified or null if IMS is not supported on the slot specified.
Brad Ebinger22bc3e42018-01-16 09:39:35 -08005928 */
5929 public IImsConfig getImsConfig(int slotId, int feature) throws RemoteException {
5930 enforceModifyPermission();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005931
5932 final long identity = Binder.clearCallingIdentity();
5933 try {
Brad Ebinger24c29992019-12-05 13:03:21 -08005934 if (mImsResolver == null) {
Brad Ebinger9c0eb502019-01-23 15:06:19 -08005935 // may happen if the device does not support IMS.
5936 return null;
5937 }
Brad Ebinger24c29992019-12-05 13:03:21 -08005938 return mImsResolver.getImsConfig(slotId, feature);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005939 } finally {
5940 Binder.restoreCallingIdentity(identity);
5941 }
Brad Ebinger22bc3e42018-01-16 09:39:35 -08005942 }
5943
Brad Ebinger884c07b2018-02-15 16:17:40 -08005944 /**
Brad Ebingerdac2f002018-04-03 15:17:52 -07005945 * Sets the ImsService Package Name that Telephony will bind to.
5946 *
Brad Ebinger24c29992019-12-05 13:03:21 -08005947 * @param slotIndex the slot ID that the ImsService should bind for.
5948 * @param isCarrierService true if the ImsService is the carrier override, false if the
Brad Ebingerdac2f002018-04-03 15:17:52 -07005949 * ImsService is the device default ImsService.
Brad Ebinger24c29992019-12-05 13:03:21 -08005950 * @param featureTypes An integer array of feature types associated with a packageName.
5951 * @param packageName The name of the package that the current configuration will be replaced
5952 * with.
Brad Ebingerdac2f002018-04-03 15:17:52 -07005953 * @return true if setting the ImsService to bind to succeeded, false if it did not.
Brad Ebingerdac2f002018-04-03 15:17:52 -07005954 */
Brad Ebinger24c29992019-12-05 13:03:21 -08005955 public boolean setBoundImsServiceOverride(int slotIndex, boolean isCarrierService,
5956 int[] featureTypes, String packageName) {
5957 int[] subIds = SubscriptionManager.getSubId(slotIndex);
5958 TelephonyPermissions.enforceShellOnly(Binder.getCallingUid(), "setBoundImsServiceOverride");
Brad Ebingerde696de2018-04-06 09:56:40 -07005959 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(mApp,
5960 (subIds != null ? subIds[0] : SubscriptionManager.INVALID_SUBSCRIPTION_ID),
Brad Ebinger24c29992019-12-05 13:03:21 -08005961 "setBoundImsServiceOverride");
Brad Ebingerde696de2018-04-06 09:56:40 -07005962
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005963 final long identity = Binder.clearCallingIdentity();
5964 try {
Brad Ebinger24c29992019-12-05 13:03:21 -08005965 if (mImsResolver == null) {
Brad Ebinger9c0eb502019-01-23 15:06:19 -08005966 // may happen if the device does not support IMS.
5967 return false;
5968 }
Brad Ebinger24c29992019-12-05 13:03:21 -08005969 Map<Integer, String> featureConfig = new HashMap<>();
5970 for (int featureType : featureTypes) {
5971 featureConfig.put(featureType, packageName);
5972 }
5973 return mImsResolver.overrideImsServiceConfiguration(slotIndex, isCarrierService,
5974 featureConfig);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005975 } finally {
5976 Binder.restoreCallingIdentity(identity);
5977 }
Brad Ebingerdac2f002018-04-03 15:17:52 -07005978 }
5979
5980 /**
Brad Ebinger999d3302020-11-25 14:31:39 -08005981 * Clears any carrier ImsService overrides for the slot index specified that were previously
5982 * set with {@link #setBoundImsServiceOverride(int, boolean, int[], String)}.
5983 *
5984 * This should only be used for testing.
5985 *
5986 * @param slotIndex the slot ID that the ImsService should bind for.
5987 * @return true if clearing the carrier ImsService override succeeded or false if it did not.
5988 */
5989 @Override
5990 public boolean clearCarrierImsServiceOverride(int slotIndex) {
5991 int[] subIds = SubscriptionManager.getSubId(slotIndex);
5992 TelephonyPermissions.enforceShellOnly(Binder.getCallingUid(),
5993 "clearCarrierImsServiceOverride");
5994 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(mApp,
5995 (subIds != null ? subIds[0] : SubscriptionManager.INVALID_SUBSCRIPTION_ID),
5996 "clearCarrierImsServiceOverride");
5997
5998 final long identity = Binder.clearCallingIdentity();
5999 try {
6000 if (mImsResolver == null) {
6001 // may happen if the device does not support IMS.
6002 return false;
6003 }
6004 return mImsResolver.clearCarrierImsServiceConfiguration(slotIndex);
6005 } finally {
6006 Binder.restoreCallingIdentity(identity);
6007 }
6008 }
6009
6010 /**
Brad Ebinger24c29992019-12-05 13:03:21 -08006011 * Return the package name of the currently bound ImsService.
Brad Ebingerdac2f002018-04-03 15:17:52 -07006012 *
6013 * @param slotId The slot that the ImsService is associated with.
6014 * @param isCarrierImsService true, if the ImsService is a carrier override, false if it is
6015 * the device default.
Brad Ebinger24c29992019-12-05 13:03:21 -08006016 * @param featureType The feature associated with the queried configuration.
Brad Ebingerdac2f002018-04-03 15:17:52 -07006017 * @return the package name of the ImsService configuration.
6018 */
Brad Ebinger24c29992019-12-05 13:03:21 -08006019 public String getBoundImsServicePackage(int slotId, boolean isCarrierImsService,
6020 @ImsFeature.FeatureType int featureType) {
Brad Ebingerde696de2018-04-06 09:56:40 -07006021 int[] subIds = SubscriptionManager.getSubId(slotId);
Brad Ebinger24c29992019-12-05 13:03:21 -08006022 TelephonyPermissions
Nathan Harold62c68512021-04-06 11:26:02 -07006023 .enforceCallingOrSelfReadPrivilegedPhoneStatePermissionOrCarrierPrivilege(
Brad Ebinger24c29992019-12-05 13:03:21 -08006024 mApp, (subIds != null ? subIds[0] : SubscriptionManager.INVALID_SUBSCRIPTION_ID),
6025 "getBoundImsServicePackage");
Brad Ebingerde696de2018-04-06 09:56:40 -07006026
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006027 final long identity = Binder.clearCallingIdentity();
6028 try {
Brad Ebinger24c29992019-12-05 13:03:21 -08006029 if (mImsResolver == null) {
Brad Ebinger9c0eb502019-01-23 15:06:19 -08006030 // may happen if the device does not support IMS.
6031 return "";
6032 }
Brad Ebingera80c3312019-12-02 10:59:39 -08006033 // TODO: change API to query RCS separately.
Brad Ebinger24c29992019-12-05 13:03:21 -08006034 return mImsResolver.getImsServiceConfiguration(slotId, isCarrierImsService,
6035 featureType);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006036 } finally {
6037 Binder.restoreCallingIdentity(identity);
6038 }
Brad Ebingerdac2f002018-04-03 15:17:52 -07006039 }
6040
Brad Ebingerbc7dd582019-10-17 17:03:22 -07006041 /**
6042 * Get the MmTelFeature state associated with the requested subscription id.
6043 * @param subId The subscription that the MmTelFeature is associated with.
6044 * @param callback A callback with an integer containing the
6045 * {@link android.telephony.ims.feature.ImsFeature.ImsState} associated with the MmTelFeature.
6046 */
6047 @Override
6048 public void getImsMmTelFeatureState(int subId, IIntegerConsumer callback) {
6049 enforceReadPrivilegedPermission("getImsMmTelFeatureState");
Brad Ebingera2628302022-02-18 03:44:55 +00006050 if (!ImsManager.isImsSupportedOnDevice(mApp)) {
6051 throw new ServiceSpecificException(ImsException.CODE_ERROR_UNSUPPORTED_OPERATION,
6052 "IMS not available on device.");
6053 }
Brad Ebingerbc7dd582019-10-17 17:03:22 -07006054 final long token = Binder.clearCallingIdentity();
6055 try {
Brad Ebingera2628302022-02-18 03:44:55 +00006056 int slotId = getSlotIndex(subId);
6057 if (slotId <= SubscriptionManager.INVALID_SIM_SLOT_INDEX) {
6058 Log.w(LOG_TAG, "getImsMmTelFeatureState: called with an inactive subscription '"
6059 + subId + "'");
6060 throw new ServiceSpecificException(ImsException.CODE_ERROR_INVALID_SUBSCRIPTION);
6061 }
6062 verifyImsMmTelConfiguredOrThrow(slotId);
6063 ImsManager.getInstance(mApp, slotId).getImsServiceState(anInteger -> {
6064 try {
6065 callback.accept(anInteger == null ? ImsFeature.STATE_UNAVAILABLE : anInteger);
6066 } catch (RemoteException e) {
6067 Log.w(LOG_TAG, "getImsMmTelFeatureState: remote caller is no longer running. "
6068 + "Ignore");
6069 }
Brad Ebingerbc7dd582019-10-17 17:03:22 -07006070 });
Brad Ebinger919631e2021-06-02 17:46:35 -07006071 } catch (ImsException e) {
6072 throw new ServiceSpecificException(e.getCode());
Brad Ebingerbc7dd582019-10-17 17:03:22 -07006073 } finally {
6074 Binder.restoreCallingIdentity(token);
6075 }
6076 }
6077
Daniel Brightbb5840b2021-01-12 15:48:18 -08006078 /**
6079 * Sets the ims registration state on all valid {@link Phone}s.
6080 */
6081 public void setImsRegistrationState(final boolean registered) {
Wink Saville36469e72014-06-11 15:17:00 -07006082 enforceModifyPermission();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006083
6084 final long identity = Binder.clearCallingIdentity();
6085 try {
Daniel Brightbb5840b2021-01-12 15:48:18 -08006086 // NOTE: Before S, this method only set the default phone.
6087 for (final Phone phone : PhoneFactory.getPhones()) {
6088 if (SubscriptionManager.isValidSubscriptionId(phone.getSubId())) {
6089 phone.setImsRegistrationState(registered);
6090 }
6091 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006092 } finally {
6093 Binder.restoreCallingIdentity(identity);
6094 }
Wink Saville36469e72014-06-11 15:17:00 -07006095 }
6096
6097 /**
Stuart Scott54788802015-03-30 13:18:01 -07006098 * Set the network selection mode to automatic.
6099 *
6100 */
6101 @Override
6102 public void setNetworkSelectionModeAutomatic(int subId) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08006103 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
6104 mApp, subId, "setNetworkSelectionModeAutomatic");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006105
6106 final long identity = Binder.clearCallingIdentity();
6107 try {
shilufc958392020-01-20 11:36:01 -08006108 if (!isActiveSubscription(subId)) {
6109 return;
6110 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006111 if (DBG) log("setNetworkSelectionModeAutomatic: subId " + subId);
Rambo Wang0f050d82021-02-12 11:43:36 -08006112 sendRequest(CMD_SET_NETWORK_SELECTION_MODE_AUTOMATIC, null, subId,
6113 SET_NETWORK_SELECTION_MODE_AUTOMATIC_TIMEOUT_MS);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006114 } finally {
6115 Binder.restoreCallingIdentity(identity);
6116 }
Stuart Scott54788802015-03-30 13:18:01 -07006117 }
6118
Jack Yud10cdd42020-09-28 20:28:01 -07006119 /**
Pengquan Mengea84e042018-09-20 14:57:26 -07006120 * Ask the radio to connect to the input network and change selection mode to manual.
6121 *
6122 * @param subId the id of the subscription.
6123 * @param operatorInfo the operator information, included the PLMN, long name and short name of
6124 * the operator to attach to.
6125 * @param persistSelection whether the selection will persist until reboot. If true, only allows
6126 * attaching to the selected PLMN until reboot; otherwise, attach to the chosen PLMN and resume
6127 * normal network selection next time.
6128 * @return {@code true} on success; {@code true} on any failure.
Shishir Agrawal302c8692015-06-19 13:49:39 -07006129 */
6130 @Override
Pengquan Mengea84e042018-09-20 14:57:26 -07006131 public boolean setNetworkSelectionModeManual(
6132 int subId, OperatorInfo operatorInfo, boolean persistSelection) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08006133 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
6134 mApp, subId, "setNetworkSelectionModeManual");
Pengquan Menge92a50d2018-09-21 15:54:48 -07006135
6136 if (!isActiveSubscription(subId)) {
6137 return false;
6138 }
6139
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006140 final long identity = Binder.clearCallingIdentity();
6141 try {
Pengquan Mengea84e042018-09-20 14:57:26 -07006142 ManualNetworkSelectionArgument arg = new ManualNetworkSelectionArgument(operatorInfo,
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006143 persistSelection);
Pengquan Mengea84e042018-09-20 14:57:26 -07006144 if (DBG) {
6145 log("setNetworkSelectionModeManual: subId: " + subId
6146 + " operator: " + operatorInfo);
6147 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006148 return (Boolean) sendRequest(CMD_SET_NETWORK_SELECTION_MODE_MANUAL, arg, subId);
6149 } finally {
6150 Binder.restoreCallingIdentity(identity);
6151 }
Shishir Agrawal302c8692015-06-19 13:49:39 -07006152 }
shilu84f6e8b2019-12-19 13:58:01 -08006153 /**
6154 * Get the manual network selection
6155 *
6156 * @param subId the id of the subscription.
6157 *
6158 * @return the previously saved user selected PLMN
6159 */
6160 @Override
6161 public String getManualNetworkSelectionPlmn(int subId) {
6162 TelephonyPermissions
Nathan Harold62c68512021-04-06 11:26:02 -07006163 .enforceCallingOrSelfReadPrecisePhoneStatePermissionOrCarrierPrivilege(
shilu84f6e8b2019-12-19 13:58:01 -08006164 mApp, subId, "getManualNetworkSelectionPlmn");
6165
6166 final long identity = Binder.clearCallingIdentity();
6167 try {
6168 if (!isActiveSubscription(subId)) {
shilufa1c2592020-03-10 10:59:43 -07006169 throw new IllegalArgumentException("Invalid Subscription Id: " + subId);
shilu84f6e8b2019-12-19 13:58:01 -08006170 }
6171
6172 final Phone phone = getPhone(subId);
6173 if (phone == null) {
shilufa1c2592020-03-10 10:59:43 -07006174 throw new IllegalArgumentException("Invalid Subscription Id: " + subId);
shilu84f6e8b2019-12-19 13:58:01 -08006175 }
6176 OperatorInfo networkSelection = phone.getSavedNetworkSelection();
6177 return TextUtils.isEmpty(networkSelection.getOperatorNumeric())
6178 ? phone.getManualNetworkSelectionPlmn() : networkSelection.getOperatorNumeric();
6179 } finally {
6180 Binder.restoreCallingIdentity(identity);
6181 }
6182 }
Shishir Agrawal302c8692015-06-19 13:49:39 -07006183
6184 /**
6185 * Scans for available networks.
6186 */
6187 @Override
Philip P. Moltmann3a2772a2019-10-04 08:15:00 -07006188 public CellNetworkScanResult getCellNetworkScanResults(int subId, String callingPackage,
6189 String callingFeatureId) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08006190 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
6191 mApp, subId, "getCellNetworkScanResults");
Hall Liuf19c44f2018-11-27 14:38:17 -08006192 LocationAccessPolicy.LocationPermissionResult locationResult =
6193 LocationAccessPolicy.checkLocationPermission(mApp,
6194 new LocationAccessPolicy.LocationPermissionQuery.Builder()
6195 .setCallingPackage(callingPackage)
Philip P. Moltmann3a2772a2019-10-04 08:15:00 -07006196 .setCallingFeatureId(callingFeatureId)
Hall Liuf19c44f2018-11-27 14:38:17 -08006197 .setCallingPid(Binder.getCallingPid())
6198 .setCallingUid(Binder.getCallingUid())
6199 .setMethod("getCellNetworkScanResults")
6200 .setMinSdkVersionForFine(Build.VERSION_CODES.Q)
Hall Liuc4a3e422020-05-26 17:18:03 -07006201 .setMinSdkVersionForCoarse(Build.VERSION_CODES.Q)
6202 .setMinSdkVersionForEnforcement(Build.VERSION_CODES.Q)
Hall Liuf19c44f2018-11-27 14:38:17 -08006203 .build());
6204 switch (locationResult) {
6205 case DENIED_HARD:
6206 throw new SecurityException("Not allowed to access scan results -- location");
6207 case DENIED_SOFT:
6208 return null;
6209 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006210
Pengquan Menga1bb6272018-09-06 09:59:22 -07006211 long identity = Binder.clearCallingIdentity();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006212 try {
6213 if (DBG) log("getCellNetworkScanResults: subId " + subId);
Pengquan Menga1bb6272018-09-06 09:59:22 -07006214 return (CellNetworkScanResult) sendRequest(
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006215 CMD_PERFORM_NETWORK_SCAN, null, subId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006216 } finally {
6217 Binder.restoreCallingIdentity(identity);
6218 }
Shishir Agrawal302c8692015-06-19 13:49:39 -07006219 }
6220
6221 /**
Shuo Qian4a594052020-01-23 11:59:30 -08006222 * Get the call forwarding info, given the call forwarding reason.
6223 */
6224 @Override
Hall Liu27d24262020-09-18 19:04:59 -07006225 public void getCallForwarding(int subId, int callForwardingReason,
6226 ICallForwardingInfoCallback callback) {
Shuo Qian4a594052020-01-23 11:59:30 -08006227 enforceReadPrivilegedPermission("getCallForwarding");
6228 long identity = Binder.clearCallingIdentity();
6229 try {
6230 if (DBG) {
6231 log("getCallForwarding: subId " + subId
6232 + " callForwardingReason" + callForwardingReason);
6233 }
Hall Liu27d24262020-09-18 19:04:59 -07006234
6235 Phone phone = getPhone(subId);
6236 if (phone == null) {
6237 try {
Hall Liu940c4ca2020-09-29 17:10:18 -07006238 callback.onError(
6239 TelephonyManager.CallForwardingInfoCallback.RESULT_ERROR_UNKNOWN);
Hall Liu27d24262020-09-18 19:04:59 -07006240 } catch (RemoteException e) {
6241 // ignore
6242 }
6243 return;
6244 }
6245
6246 Pair<Integer, TelephonyManager.CallForwardingInfoCallback> argument = Pair.create(
6247 callForwardingReason, new TelephonyManager.CallForwardingInfoCallback() {
6248 @Override
6249 public void onCallForwardingInfoAvailable(CallForwardingInfo info) {
6250 try {
6251 callback.onCallForwardingInfoAvailable(info);
6252 } catch (RemoteException e) {
6253 // ignore
6254 }
6255 }
6256
6257 @Override
6258 public void onError(int error) {
6259 try {
6260 callback.onError(error);
6261 } catch (RemoteException e) {
6262 // ignore
6263 }
6264 }
6265 });
6266 sendRequestAsync(CMD_GET_CALL_FORWARDING, argument, phone, null);
Shuo Qian4a594052020-01-23 11:59:30 -08006267 } finally {
6268 Binder.restoreCallingIdentity(identity);
6269 }
6270 }
6271
6272 /**
6273 * Sets the voice call forwarding info including status (enable/disable), call forwarding
6274 * reason, the number to forward, and the timeout before the forwarding is attempted.
6275 */
6276 @Override
Hall Liu27d24262020-09-18 19:04:59 -07006277 public void setCallForwarding(int subId, CallForwardingInfo callForwardingInfo,
6278 IIntegerConsumer callback) {
Shuo Qian4a594052020-01-23 11:59:30 -08006279 enforceModifyPermission();
6280 long identity = Binder.clearCallingIdentity();
6281 try {
6282 if (DBG) {
6283 log("setCallForwarding: subId " + subId
6284 + " callForwardingInfo" + callForwardingInfo);
6285 }
Hall Liu27d24262020-09-18 19:04:59 -07006286
6287 Phone phone = getPhone(subId);
6288 if (phone == null) {
6289 try {
Hall Liu940c4ca2020-09-29 17:10:18 -07006290 callback.accept(
6291 TelephonyManager.CallForwardingInfoCallback.RESULT_ERROR_UNKNOWN);
Hall Liu27d24262020-09-18 19:04:59 -07006292 } catch (RemoteException e) {
6293 // ignore
6294 }
6295 return;
6296 }
6297
6298 Pair<CallForwardingInfo, Consumer<Integer>> arguments = Pair.create(callForwardingInfo,
6299 FunctionalUtils.ignoreRemoteException(callback::accept));
6300
6301 sendRequestAsync(CMD_SET_CALL_FORWARDING, arguments, phone, null);
Shuo Qian4a594052020-01-23 11:59:30 -08006302 } finally {
6303 Binder.restoreCallingIdentity(identity);
6304 }
6305 }
6306
6307 /**
Hall Liu27d24262020-09-18 19:04:59 -07006308 * Get the call waiting status for a subId.
Shuo Qian4a594052020-01-23 11:59:30 -08006309 */
6310 @Override
Hall Liu27d24262020-09-18 19:04:59 -07006311 public void getCallWaitingStatus(int subId, IIntegerConsumer callback) {
SongFerngWang0e767992021-03-31 22:08:45 +08006312 enforceReadPrivilegedPermission("getCallWaitingStatus");
Shuo Qian4a594052020-01-23 11:59:30 -08006313 long identity = Binder.clearCallingIdentity();
6314 try {
Hall Liu27d24262020-09-18 19:04:59 -07006315 Phone phone = getPhone(subId);
6316 if (phone == null) {
6317 try {
6318 callback.accept(TelephonyManager.CALL_WAITING_STATUS_UNKNOWN_ERROR);
6319 } catch (RemoteException e) {
6320 // ignore
6321 }
6322 return;
6323 }
SongFerngWang0e767992021-03-31 22:08:45 +08006324 CarrierConfigManager configManager = new CarrierConfigManager(phone.getContext());
6325 PersistableBundle c = configManager.getConfigForSubId(subId);
6326 boolean requireUssd = c.getBoolean(
6327 CarrierConfigManager.KEY_USE_CALL_WAITING_USSD_BOOL, false);
Hall Liu27d24262020-09-18 19:04:59 -07006328
Shuo Qian4a594052020-01-23 11:59:30 -08006329 if (DBG) log("getCallWaitingStatus: subId " + subId);
SongFerngWang0e767992021-03-31 22:08:45 +08006330 if (requireUssd) {
6331 CarrierXmlParser carrierXmlParser = new CarrierXmlParser(phone.getContext(),
6332 getSubscriptionCarrierId(subId));
6333 String newUssdCommand = "";
6334 try {
6335 newUssdCommand = carrierXmlParser.getFeature(
6336 CarrierXmlParser.FEATURE_CALL_WAITING)
6337 .makeCommand(CarrierXmlParser.SsEntry.SSAction.QUERY, null);
6338 } catch (NullPointerException e) {
6339 loge("Failed to generate USSD number" + e);
6340 }
6341 ResultReceiver wrappedCallback = new CallWaitingUssdResultReceiver(
6342 mMainThreadHandler, callback, carrierXmlParser,
6343 CarrierXmlParser.SsEntry.SSAction.QUERY);
6344 final String ussdCommand = newUssdCommand;
6345 Executors.newSingleThreadExecutor().execute(() -> {
6346 handleUssdRequest(subId, ussdCommand, wrappedCallback);
6347 });
6348 } else {
6349 Consumer<Integer> argument = FunctionalUtils.ignoreRemoteException(
6350 callback::accept);
6351 sendRequestAsync(CMD_GET_CALL_WAITING, argument, phone, null);
6352 }
Shuo Qian4a594052020-01-23 11:59:30 -08006353 } finally {
6354 Binder.restoreCallingIdentity(identity);
6355 }
6356 }
6357
6358 /**
Hall Liu27d24262020-09-18 19:04:59 -07006359 * Sets whether call waiting is enabled for a given subId.
Shuo Qian4a594052020-01-23 11:59:30 -08006360 */
6361 @Override
Hall Liu27d24262020-09-18 19:04:59 -07006362 public void setCallWaitingStatus(int subId, boolean enable, IIntegerConsumer callback) {
Shuo Qian4a594052020-01-23 11:59:30 -08006363 enforceModifyPermission();
6364 long identity = Binder.clearCallingIdentity();
6365 try {
Hall Liu27d24262020-09-18 19:04:59 -07006366 if (DBG) log("setCallWaitingStatus: subId " + subId + " enable: " + enable);
6367
6368 Phone phone = getPhone(subId);
6369 if (phone == null) {
6370 try {
6371 callback.accept(TelephonyManager.CALL_WAITING_STATUS_UNKNOWN_ERROR);
6372 } catch (RemoteException e) {
6373 // ignore
6374 }
6375 return;
6376 }
6377
SongFerngWang0e767992021-03-31 22:08:45 +08006378 CarrierConfigManager configManager = new CarrierConfigManager(phone.getContext());
6379 PersistableBundle c = configManager.getConfigForSubId(subId);
6380 boolean requireUssd = c.getBoolean(
6381 CarrierConfigManager.KEY_USE_CALL_WAITING_USSD_BOOL, false);
Hall Liu27d24262020-09-18 19:04:59 -07006382
SongFerngWang0e767992021-03-31 22:08:45 +08006383 if (DBG) log("getCallWaitingStatus: subId " + subId);
6384 if (requireUssd) {
6385 CarrierXmlParser carrierXmlParser = new CarrierXmlParser(phone.getContext(),
6386 getSubscriptionCarrierId(subId));
6387 CarrierXmlParser.SsEntry.SSAction ssAction =
6388 enable ? CarrierXmlParser.SsEntry.SSAction.UPDATE_ACTIVATE
6389 : CarrierXmlParser.SsEntry.SSAction.UPDATE_DEACTIVATE;
6390 String newUssdCommand = "";
6391 try {
6392 newUssdCommand = carrierXmlParser.getFeature(
6393 CarrierXmlParser.FEATURE_CALL_WAITING)
6394 .makeCommand(ssAction, null);
6395 } catch (NullPointerException e) {
6396 loge("Failed to generate USSD number" + e);
6397 }
6398 ResultReceiver wrappedCallback = new CallWaitingUssdResultReceiver(
6399 mMainThreadHandler, callback, carrierXmlParser, ssAction);
6400 final String ussdCommand = newUssdCommand;
6401 Executors.newSingleThreadExecutor().execute(() -> {
6402 handleUssdRequest(subId, ussdCommand, wrappedCallback);
6403 });
6404 } else {
6405 Pair<Boolean, Consumer<Integer>> arguments = Pair.create(enable,
6406 FunctionalUtils.ignoreRemoteException(callback::accept));
6407
6408 sendRequestAsync(CMD_SET_CALL_WAITING, arguments, phone, null);
6409 }
Shuo Qian4a594052020-01-23 11:59:30 -08006410 } finally {
6411 Binder.restoreCallingIdentity(identity);
6412 }
6413 }
6414
6415 /**
yinxub1bed742017-04-17 11:45:04 -07006416 * Starts a new network scan and returns the id of this scan.
yinxu504e1392017-04-12 16:03:22 -07006417 *
yinxub1bed742017-04-17 11:45:04 -07006418 * @param subId id of the subscription
Sooraj Sasindran2250dc02021-11-10 16:42:01 -08006419 * @param renounceFineLocationAccess Set this to true if the caller would not like to receive
6420 * location related information which will be sent if the caller already possess
6421 * {@android.Manifest.permission.ACCESS_FINE_LOCATION} and do not renounce the permission
yinxub1bed742017-04-17 11:45:04 -07006422 * @param request contains the radio access networks with bands/channels to scan
6423 * @param messenger callback messenger for scan results or errors
6424 * @param binder for the purpose of auto clean when the user thread crashes
yinxu504e1392017-04-12 16:03:22 -07006425 * @return the id of the requested scan which can be used to stop the scan.
6426 */
6427 @Override
Sooraj Sasindran2250dc02021-11-10 16:42:01 -08006428 public int requestNetworkScan(int subId, boolean renounceFineLocationAccess,
6429 NetworkScanRequest request, Messenger messenger,
Philip P. Moltmann3a2772a2019-10-04 08:15:00 -07006430 IBinder binder, String callingPackage, String callingFeatureId) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08006431 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
6432 mApp, subId, "requestNetworkScan");
Hall Liuf19c44f2018-11-27 14:38:17 -08006433 LocationAccessPolicy.LocationPermissionResult locationResult =
Sooraj Sasindran2250dc02021-11-10 16:42:01 -08006434 LocationAccessPolicy.LocationPermissionResult.DENIED_HARD;
6435 if (!renounceFineLocationAccess) {
6436 locationResult = LocationAccessPolicy.checkLocationPermission(mApp,
6437 new LocationAccessPolicy.LocationPermissionQuery.Builder()
6438 .setCallingPackage(callingPackage)
6439 .setCallingFeatureId(callingFeatureId)
6440 .setCallingPid(Binder.getCallingPid())
6441 .setCallingUid(Binder.getCallingUid())
6442 .setMethod("requestNetworkScan")
6443 .setMinSdkVersionForFine(Build.VERSION_CODES.Q)
6444 .setMinSdkVersionForCoarse(Build.VERSION_CODES.Q)
6445 .setMinSdkVersionForEnforcement(Build.VERSION_CODES.Q)
6446 .build());
6447 }
Hall Liub2ac8ef2019-02-28 15:56:23 -08006448 if (locationResult != LocationAccessPolicy.LocationPermissionResult.ALLOWED) {
Nathan Harold1c11dba2020-09-22 17:54:53 -07006449 SecurityException e = checkNetworkRequestForSanitizedLocationAccess(
6450 request, subId, callingPackage);
Hall Liub2ac8ef2019-02-28 15:56:23 -08006451 if (e != null) {
6452 if (locationResult == LocationAccessPolicy.LocationPermissionResult.DENIED_HARD) {
6453 throw e;
6454 } else {
Hall Liu0e5abaf2019-04-04 01:25:30 -07006455 loge(e.getMessage());
Hall Liub2ac8ef2019-02-28 15:56:23 -08006456 return TelephonyScanManager.INVALID_SCAN_ID;
6457 }
6458 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006459 }
Hall Liu912dfd32019-04-25 14:02:26 -07006460 int callingUid = Binder.getCallingUid();
6461 int callingPid = Binder.getCallingPid();
Ying Xu94a46582019-04-18 17:14:56 -07006462 final long identity = Binder.clearCallingIdentity();
6463 try {
6464 return mNetworkScanRequestTracker.startNetworkScan(
Sooraj Sasindran2250dc02021-11-10 16:42:01 -08006465 renounceFineLocationAccess, request, messenger, binder, getPhone(subId),
Hall Liu912dfd32019-04-25 14:02:26 -07006466 callingUid, callingPid, callingPackage);
Ying Xu94a46582019-04-18 17:14:56 -07006467 } finally {
6468 Binder.restoreCallingIdentity(identity);
6469 }
yinxu504e1392017-04-12 16:03:22 -07006470 }
6471
Hall Liub2ac8ef2019-02-28 15:56:23 -08006472 private SecurityException checkNetworkRequestForSanitizedLocationAccess(
Nathan Harold1c11dba2020-09-22 17:54:53 -07006473 NetworkScanRequest request, int subId, String callingPackage) {
6474 boolean hasCarrierPriv = checkCarrierPrivilegesForPackage(subId, callingPackage)
Hall Liu558027f2019-05-15 19:14:05 -07006475 == TelephonyManager.CARRIER_PRIVILEGE_STATUS_HAS_ACCESS;
6476 boolean hasNetworkScanPermission =
6477 mApp.checkCallingOrSelfPermission(android.Manifest.permission.NETWORK_SCAN)
6478 == PERMISSION_GRANTED;
6479
6480 if (!hasCarrierPriv && !hasNetworkScanPermission) {
6481 return new SecurityException("permission.NETWORK_SCAN or carrier privileges is needed"
6482 + " for network scans without location access.");
Hall Liub2ac8ef2019-02-28 15:56:23 -08006483 }
6484
6485 if (request.getSpecifiers() != null && request.getSpecifiers().length > 0) {
6486 for (RadioAccessSpecifier ras : request.getSpecifiers()) {
Hall Liub2ac8ef2019-02-28 15:56:23 -08006487 if (ras.getChannels() != null && ras.getChannels().length > 0) {
6488 return new SecurityException("Specific channels must not be"
6489 + " scanned without location access.");
6490 }
6491 }
6492 }
6493
Hall Liub2ac8ef2019-02-28 15:56:23 -08006494 return null;
6495 }
6496
yinxu504e1392017-04-12 16:03:22 -07006497 /**
6498 * Stops an existing network scan with the given scanId.
yinxub1bed742017-04-17 11:45:04 -07006499 *
6500 * @param subId id of the subscription
6501 * @param scanId id of the scan that needs to be stopped
yinxu504e1392017-04-12 16:03:22 -07006502 */
6503 @Override
6504 public void stopNetworkScan(int subId, int scanId) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08006505 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
6506 mApp, subId, "stopNetworkScan");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006507
Hall Liu912dfd32019-04-25 14:02:26 -07006508 int callingUid = Binder.getCallingUid();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006509 final long identity = Binder.clearCallingIdentity();
6510 try {
Hall Liu912dfd32019-04-25 14:02:26 -07006511 mNetworkScanRequestTracker.stopNetworkScan(scanId, callingUid);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006512 } finally {
6513 Binder.restoreCallingIdentity(identity);
6514 }
yinxu504e1392017-04-12 16:03:22 -07006515 }
6516
6517 /**
SongFerngWang3ef3e072020-12-21 16:41:52 +08006518 * Get the allowed network types bitmask.
Junda Liu84d15a22014-07-02 11:21:04 -07006519 *
SongFerngWang3ef3e072020-12-21 16:41:52 +08006520 * @return the allowed network types bitmask, defined in RILConstants.java.
Junda Liu84d15a22014-07-02 11:21:04 -07006521 */
6522 @Override
SongFerngWang3ef3e072020-12-21 16:41:52 +08006523 public int getAllowedNetworkTypesBitmask(int subId) {
Pengquan Menga4009cb2018-12-20 11:00:24 -08006524 TelephonyPermissions
Nathan Harold62c68512021-04-06 11:26:02 -07006525 .enforceCallingOrSelfReadPrivilegedPhoneStatePermissionOrCarrierPrivilege(
SongFerngWang3ef3e072020-12-21 16:41:52 +08006526 mApp, subId, "getAllowedNetworkTypesBitmask");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006527
6528 final long identity = Binder.clearCallingIdentity();
6529 try {
SongFerngWang3ef3e072020-12-21 16:41:52 +08006530 if (DBG) log("getAllowedNetworkTypesBitmask");
6531 int[] result = (int[]) sendRequest(CMD_GET_ALLOWED_NETWORK_TYPES_BITMASK, null, subId);
6532 int networkTypesBitmask = (result != null ? result[0] : -1);
6533 if (DBG) log("getAllowedNetworkTypesBitmask: " + networkTypesBitmask);
6534 return networkTypesBitmask;
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006535 } finally {
6536 Binder.restoreCallingIdentity(identity);
6537 }
Jake Hamby7c27be32014-03-03 13:25:59 -08006538 }
6539
6540 /**
Sooraj Sasindranc46dfbd2020-06-03 01:06:00 -07006541 * Get the allowed network types for certain reason.
6542 *
6543 * @param subId the id of the subscription.
6544 * @param reason the reason the allowed network type change is taking place
6545 * @return the allowed network types.
6546 */
6547 @Override
6548 public long getAllowedNetworkTypesForReason(int subId,
6549 @TelephonyManager.AllowedNetworkTypesReason int reason) {
Nathan Harold62c68512021-04-06 11:26:02 -07006550 TelephonyPermissions.enforceCallingOrSelfReadPrecisePhoneStatePermissionOrCarrierPrivilege(
SongFerngWang8c6e82e2021-03-02 22:09:29 +08006551 mApp, subId, "getAllowedNetworkTypesForReason");
Sooraj Sasindranc46dfbd2020-06-03 01:06:00 -07006552 final long identity = Binder.clearCallingIdentity();
6553 try {
6554 return getPhoneFromSubId(subId).getAllowedNetworkTypes(reason);
6555 } finally {
6556 Binder.restoreCallingIdentity(identity);
6557 }
6558 }
6559
6560 /**
Sooraj Sasindran37444802020-08-11 10:40:43 -07006561 * Enable/Disable E-UTRA-NR Dual Connectivity
6562 * @param subId subscription id of the sim card
6563 * @param nrDualConnectivityState expected NR dual connectivity state
6564 * This can be passed following states
6565 * <ol>
6566 * <li>Enable NR dual connectivity {@link TelephonyManager#NR_DUAL_CONNECTIVITY_ENABLE}
6567 * <li>Disable NR dual connectivity {@link TelephonyManager#NR_DUAL_CONNECTIVITY_DISABLE}
6568 * <li>Disable NR dual connectivity and force secondary cell to be released
6569 * {@link TelephonyManager#NR_DUAL_CONNECTIVITY_DISABLE_IMMEDIATE}
6570 * </ol>
6571 * @return operation result.
6572 */
6573 @Override
6574 public int setNrDualConnectivityState(int subId,
6575 @TelephonyManager.NrDualConnectivityState int nrDualConnectivityState) {
6576 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
6577 mApp, subId, "enableNRDualConnectivity");
Sooraj Sasindran0e4e00a2021-03-16 18:02:32 -07006578 if (!isRadioInterfaceCapabilitySupported(
Sooraj Sasindrandfd595b2021-03-11 17:38:13 -08006579 TelephonyManager.CAPABILITY_NR_DUAL_CONNECTIVITY_CONFIGURATION_AVAILABLE)) {
6580 return TelephonyManager.ENABLE_NR_DUAL_CONNECTIVITY_NOT_SUPPORTED;
6581 }
6582
Sooraj Sasindran37444802020-08-11 10:40:43 -07006583 WorkSource workSource = getWorkSource(Binder.getCallingUid());
6584 final long identity = Binder.clearCallingIdentity();
6585 try {
6586 int result = (int) sendRequest(CMD_ENABLE_NR_DUAL_CONNECTIVITY,
6587 nrDualConnectivityState, subId,
6588 workSource);
6589 if (DBG) log("enableNRDualConnectivity result: " + result);
6590 return result;
6591 } finally {
6592 Binder.restoreCallingIdentity(identity);
6593 }
6594 }
6595
6596 /**
6597 * Is E-UTRA-NR Dual Connectivity enabled
6598 * @return true if dual connectivity is enabled else false
6599 */
6600 @Override
6601 public boolean isNrDualConnectivityEnabled(int subId) {
6602 TelephonyPermissions
Nathan Harold62c68512021-04-06 11:26:02 -07006603 .enforceCallingOrSelfReadPrivilegedPhoneStatePermissionOrCarrierPrivilege(
Sooraj Sasindran37444802020-08-11 10:40:43 -07006604 mApp, subId, "isNRDualConnectivityEnabled");
Sooraj Sasindran0e4e00a2021-03-16 18:02:32 -07006605 if (!isRadioInterfaceCapabilitySupported(
Sooraj Sasindrandfd595b2021-03-11 17:38:13 -08006606 TelephonyManager.CAPABILITY_NR_DUAL_CONNECTIVITY_CONFIGURATION_AVAILABLE)) {
6607 return false;
6608 }
Sooraj Sasindran37444802020-08-11 10:40:43 -07006609 WorkSource workSource = getWorkSource(Binder.getCallingUid());
6610 final long identity = Binder.clearCallingIdentity();
6611 try {
6612 boolean isEnabled = (boolean) sendRequest(CMD_IS_NR_DUAL_CONNECTIVITY_ENABLED,
6613 null, subId, workSource);
6614 if (DBG) log("isNRDualConnectivityEnabled: " + isEnabled);
6615 return isEnabled;
6616 } finally {
6617 Binder.restoreCallingIdentity(identity);
6618 }
6619 }
6620
6621 /**
Sooraj Sasindranc46dfbd2020-06-03 01:06:00 -07006622 * Set the allowed network types of the device and
6623 * provide the reason triggering the allowed network change.
6624 *
6625 * @param subId the id of the subscription.
6626 * @param reason the reason the allowed network type change is taking place
6627 * @param allowedNetworkTypes the allowed network types.
6628 * @return true on success; false on any failure.
6629 */
6630 @Override
6631 public boolean setAllowedNetworkTypesForReason(int subId,
SongFerngWang3ef3e072020-12-21 16:41:52 +08006632 @TelephonyManager.AllowedNetworkTypesReason int reason,
6633 @TelephonyManager.NetworkTypeBitMask long allowedNetworkTypes) {
Sooraj Sasindranc46dfbd2020-06-03 01:06:00 -07006634 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
6635 mApp, subId, "setAllowedNetworkTypesForReason");
SongFerngWang3ef3e072020-12-21 16:41:52 +08006636 if (!TelephonyManager.isValidAllowedNetworkTypesReason(reason)) {
SongFerngWang7ffc2732021-04-15 19:46:33 +08006637 loge("setAllowedNetworkTypesForReason: Invalid allowed network type reason: " + reason);
6638 return false;
6639 }
6640 if (!SubscriptionManager.isUsableSubscriptionId(subId)) {
6641 loge("setAllowedNetworkTypesForReason: Invalid subscriptionId:" + subId);
SongFerngWang3ef3e072020-12-21 16:41:52 +08006642 return false;
6643 }
6644
SongFerngWang8c6e82e2021-03-02 22:09:29 +08006645 log("setAllowedNetworkTypesForReason: " + reason + " value: "
6646 + TelephonyManager.convertNetworkTypeBitmaskToString(allowedNetworkTypes));
6647
6648
6649 if (allowedNetworkTypes == getPhoneFromSubId(subId).getAllowedNetworkTypes(reason)) {
6650 log("setAllowedNetworkTypesForReason: " + reason + "does not change value");
6651 return true;
SongFerngWang3ef3e072020-12-21 16:41:52 +08006652 }
SongFerngWang8c6e82e2021-03-02 22:09:29 +08006653
Sooraj Sasindranc46dfbd2020-06-03 01:06:00 -07006654 final long identity = Binder.clearCallingIdentity();
6655 try {
SongFerngWang3ef3e072020-12-21 16:41:52 +08006656 Boolean success = (Boolean) sendRequest(
6657 CMD_SET_ALLOWED_NETWORK_TYPES_FOR_REASON,
6658 new Pair<Integer, Long>(reason, allowedNetworkTypes), subId);
6659
6660 if (DBG) log("setAllowedNetworkTypesForReason: " + (success ? "ok" : "fail"));
6661 return success;
Sooraj Sasindranc46dfbd2020-06-03 01:06:00 -07006662 } finally {
6663 Binder.restoreCallingIdentity(identity);
6664 }
6665 }
6666
6667 /**
Miaoa84611c2019-03-15 09:21:10 +08006668 * Check whether DUN APN is required for tethering with subId.
Junda Liu475951f2014-11-07 16:45:03 -08006669 *
Miaoa84611c2019-03-15 09:21:10 +08006670 * @param subId the id of the subscription to require tethering.
Amit Mahajanfe58cdf2017-07-11 12:01:53 -07006671 * @return {@code true} if DUN APN is required for tethering.
Junda Liu475951f2014-11-07 16:45:03 -08006672 * @hide
6673 */
6674 @Override
SongFerngWangf08d8122019-11-15 14:58:44 +08006675 public boolean isTetheringApnRequiredForSubscriber(int subId) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08006676 enforceModifyPermission();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006677 final long identity = Binder.clearCallingIdentity();
Miaoa84611c2019-03-15 09:21:10 +08006678 final Phone phone = getPhone(subId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006679 try {
Miaoa84611c2019-03-15 09:21:10 +08006680 if (phone != null) {
6681 return phone.hasMatchedTetherApnSetting();
6682 } else {
6683 return false;
6684 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006685 } finally {
6686 Binder.restoreCallingIdentity(identity);
Junda Liu475951f2014-11-07 16:45:03 -08006687 }
Junda Liu475951f2014-11-07 16:45:03 -08006688 }
6689
6690 /**
Malcolm Chen964682d2017-11-28 16:20:07 -08006691 * Get the user enabled state of Mobile Data.
6692 *
6693 * TODO: remove and use isUserDataEnabled.
6694 * This can't be removed now because some vendor codes
6695 * calls through ITelephony directly while they should
6696 * use TelephonyManager.
6697 *
6698 * @return true on enabled
6699 */
6700 @Override
6701 public boolean getDataEnabled(int subId) {
6702 return isUserDataEnabled(subId);
6703 }
6704
6705 /**
6706 * Get whether mobile data is enabled per user setting.
6707 *
6708 * There are other factors deciding whether mobile data is actually enabled, but they are
6709 * not considered here. See {@link #isDataEnabled(int)} for more details.
Robert Greenwalt646120a2014-05-23 11:54:03 -07006710 *
Sooraj Sasindran5d051bf2021-11-05 13:14:15 -07006711 * Accepts either READ_BASIC_PHONE_STATE, ACCESS_NETWORK_STATE, MODIFY_PHONE_STATE
6712 * or carrier privileges.
Robert Greenwalted86e582014-05-21 20:03:20 -07006713 *
6714 * @return {@code true} if data is enabled else {@code false}
6715 */
6716 @Override
Malcolm Chen964682d2017-11-28 16:20:07 -08006717 public boolean isUserDataEnabled(int subId) {
Sooraj Sasindran5d051bf2021-11-05 13:14:15 -07006718 String functionName = "isUserDataEnabled";
Robert Greenwalt646120a2014-05-23 11:54:03 -07006719 try {
Sooraj Sasindran5d051bf2021-11-05 13:14:15 -07006720 try {
6721 mApp.enforceCallingOrSelfPermission(permission.READ_BASIC_PHONE_STATE,
6722 functionName);
Sooraj Sasindran12545cc2022-08-22 14:30:25 -07006723 } catch (SecurityException e) {
Sooraj Sasindran5d051bf2021-11-05 13:14:15 -07006724 mApp.enforceCallingOrSelfPermission(permission.ACCESS_NETWORK_STATE, functionName);
6725 }
Sooraj Sasindran12545cc2022-08-22 14:30:25 -07006726 } catch (SecurityException e) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08006727 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
Sooraj Sasindran5d051bf2021-11-05 13:14:15 -07006728 mApp, subId, functionName);
6729
Robert Greenwalt646120a2014-05-23 11:54:03 -07006730 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006731
6732 final long identity = Binder.clearCallingIdentity();
6733 try {
6734 int phoneId = mSubscriptionController.getPhoneId(subId);
6735 if (DBG) log("isUserDataEnabled: subId=" + subId + " phoneId=" + phoneId);
6736 Phone phone = PhoneFactory.getPhone(phoneId);
6737 if (phone != null) {
6738 boolean retVal = phone.isUserDataEnabled();
6739 if (DBG) log("isUserDataEnabled: subId=" + subId + " retVal=" + retVal);
6740 return retVal;
6741 } else {
6742 if (DBG) loge("isUserDataEnabled: no phone subId=" + subId + " retVal=false");
6743 return false;
6744 }
6745 } finally {
6746 Binder.restoreCallingIdentity(identity);
Malcolm Chen964682d2017-11-28 16:20:07 -08006747 }
6748 }
6749
6750 /**
Shuo Qian8ee4e882020-01-08 14:30:06 -08006751 * Checks if the device is capable of mobile data by considering whether whether the
6752 * user has enabled mobile data, whether the carrier has enabled mobile data, and
6753 * whether the network policy allows data connections.
Malcolm Chen964682d2017-11-28 16:20:07 -08006754 *
Shuo Qian8ee4e882020-01-08 14:30:06 -08006755 * @return {@code true} if the overall data connection is capable; {@code false} if not.
Malcolm Chen964682d2017-11-28 16:20:07 -08006756 */
6757 @Override
6758 public boolean isDataEnabled(int subId) {
Sooraj Sasindran5d051bf2021-11-05 13:14:15 -07006759 String functionName = "isDataEnabled";
Sooraj Sasindran95c07a92020-07-15 01:35:24 -07006760 try {
6761 try {
6762 mApp.enforceCallingOrSelfPermission(
6763 android.Manifest.permission.ACCESS_NETWORK_STATE,
Sooraj Sasindran5d051bf2021-11-05 13:14:15 -07006764 functionName);
Sooraj Sasindran12545cc2022-08-22 14:30:25 -07006765 } catch (SecurityException e) {
Sooraj Sasindran5d051bf2021-11-05 13:14:15 -07006766 try {
6767 mApp.enforceCallingOrSelfPermission(
6768 android.Manifest.permission.READ_PHONE_STATE,
6769 functionName);
Sooraj Sasindran12545cc2022-08-22 14:30:25 -07006770 } catch (SecurityException e2) {
Sooraj Sasindran5d051bf2021-11-05 13:14:15 -07006771 mApp.enforceCallingOrSelfPermission(
6772 permission.READ_BASIC_PHONE_STATE, functionName);
6773 }
Sooraj Sasindran95c07a92020-07-15 01:35:24 -07006774 }
Sooraj Sasindran12545cc2022-08-22 14:30:25 -07006775 } catch (SecurityException e) {
Sooraj Sasindran5d051bf2021-11-05 13:14:15 -07006776 enforceReadPrivilegedPermission(functionName);
Sooraj Sasindran95c07a92020-07-15 01:35:24 -07006777 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006778
6779 final long identity = Binder.clearCallingIdentity();
6780 try {
6781 int phoneId = mSubscriptionController.getPhoneId(subId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006782 Phone phone = PhoneFactory.getPhone(phoneId);
6783 if (phone != null) {
Jack Yu4ad64e52021-12-03 14:23:53 -08006784 boolean retVal;
Jack Yu7968c6d2022-07-31 00:43:21 -07006785 retVal = phone.getDataSettingsManager().isDataEnabled();
Jack Yu4ad64e52021-12-03 14:23:53 -08006786 if (DBG) log("isDataEnabled: " + retVal + ", subId=" + subId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006787 return retVal;
6788 } else {
6789 if (DBG) loge("isDataEnabled: no phone subId=" + subId + " retVal=false");
6790 return false;
6791 }
6792 } finally {
6793 Binder.restoreCallingIdentity(identity);
Wink Savillee7353bb2014-12-05 14:21:41 -08006794 }
Robert Greenwalted86e582014-05-21 20:03:20 -07006795 }
Shishir Agrawal60f9c952014-06-23 12:00:43 -07006796
Sooraj Sasindranaf1b5132020-05-05 21:06:20 +00006797 /**
6798 * Check if data is enabled for a specific reason
6799 * @param subId Subscription index
6800 * @param reason the reason the data enable change is taking place
6801 * @return {@code true} if the overall data is enabled; {@code false} if not.
6802 */
6803 @Override
Sooraj Sasindran95c07a92020-07-15 01:35:24 -07006804 public boolean isDataEnabledForReason(int subId,
Sooraj Sasindranaf1b5132020-05-05 21:06:20 +00006805 @TelephonyManager.DataEnabledReason int reason) {
Sooraj Sasindran5d051bf2021-11-05 13:14:15 -07006806 String functionName = "isDataEnabledForReason";
Sooraj Sasindranaf1b5132020-05-05 21:06:20 +00006807 try {
Sooraj Sasindran5d051bf2021-11-05 13:14:15 -07006808 try {
6809 mApp.enforceCallingOrSelfPermission(
6810 android.Manifest.permission.ACCESS_NETWORK_STATE,
6811 functionName);
Sooraj Sasindran12545cc2022-08-22 14:30:25 -07006812 } catch (SecurityException e) {
Sooraj Sasindran5d051bf2021-11-05 13:14:15 -07006813 mApp.enforceCallingOrSelfPermission(permission.READ_BASIC_PHONE_STATE,
6814 functionName);
6815 }
Sooraj Sasindran12545cc2022-08-22 14:30:25 -07006816 } catch (SecurityException e) {
Sooraj Sasindran0d909a02021-11-08 12:01:16 -08006817 try {
6818 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.READ_PHONE_STATE,
Sooraj Sasindran5d051bf2021-11-05 13:14:15 -07006819 functionName);
Sooraj Sasindran12545cc2022-08-22 14:30:25 -07006820 } catch (SecurityException e2) {
Sooraj Sasindran0d909a02021-11-08 12:01:16 -08006821 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
Sooraj Sasindran5d051bf2021-11-05 13:14:15 -07006822 mApp, subId, functionName);
Sooraj Sasindran0d909a02021-11-08 12:01:16 -08006823 }
Sooraj Sasindranaf1b5132020-05-05 21:06:20 +00006824 }
6825
6826
6827 final long identity = Binder.clearCallingIdentity();
6828 try {
6829 int phoneId = mSubscriptionController.getPhoneId(subId);
6830 if (DBG) {
Sooraj Sasindran95c07a92020-07-15 01:35:24 -07006831 log("isDataEnabledForReason: subId=" + subId + " phoneId=" + phoneId
Sooraj Sasindranaf1b5132020-05-05 21:06:20 +00006832 + " reason=" + reason);
6833 }
6834 Phone phone = PhoneFactory.getPhone(phoneId);
6835 if (phone != null) {
6836 boolean retVal;
Jack Yu7968c6d2022-07-31 00:43:21 -07006837 retVal = phone.getDataSettingsManager().isDataEnabledForReason(reason);
Sooraj Sasindran95c07a92020-07-15 01:35:24 -07006838 if (DBG) log("isDataEnabledForReason: retVal=" + retVal);
Sooraj Sasindranaf1b5132020-05-05 21:06:20 +00006839 return retVal;
6840 } else {
6841 if (DBG) {
Sooraj Sasindran95c07a92020-07-15 01:35:24 -07006842 loge("isDataEnabledForReason: no phone subId="
Sooraj Sasindranaf1b5132020-05-05 21:06:20 +00006843 + subId + " retVal=false");
6844 }
6845 return false;
6846 }
6847 } finally {
6848 Binder.restoreCallingIdentity(identity);
6849 }
6850 }
6851
Shishir Agrawal60f9c952014-06-23 12:00:43 -07006852 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08006853 public int getCarrierPrivilegeStatus(int subId) {
Hunter Knepshieldcad7f0b2021-12-13 15:08:35 -08006854 // No permission needed; this only lets the caller inspect their own status.
6855 return getCarrierPrivilegeStatusForUidWithPermission(subId, Binder.getCallingUid());
Shishir Agrawal60f9c952014-06-23 12:00:43 -07006856 }
Junda Liu29340342014-07-10 15:23:27 -07006857
6858 @Override
Jeff Davidson7e17e312018-02-13 18:17:36 -08006859 public int getCarrierPrivilegeStatusForUid(int subId, int uid) {
Shuo Qian2c0ae432019-12-05 11:40:37 -08006860 enforceReadPrivilegedPermission("getCarrierPrivilegeStatusForUid");
Hunter Knepshieldcad7f0b2021-12-13 15:08:35 -08006861 return getCarrierPrivilegeStatusForUidWithPermission(subId, uid);
6862 }
6863
6864 private int getCarrierPrivilegeStatusForUidWithPermission(int subId, int uid) {
6865 Phone phone = getPhone(subId);
Jeff Davidson7e17e312018-02-13 18:17:36 -08006866 if (phone == null) {
Taesu Leef8fbed92019-10-07 18:47:02 +09006867 loge("getCarrierPrivilegeStatusForUid: Invalid subId");
Jeff Davidson7e17e312018-02-13 18:17:36 -08006868 return TelephonyManager.CARRIER_PRIVILEGE_STATUS_NO_ACCESS;
6869 }
Hunter Knepshieldcad7f0b2021-12-13 15:08:35 -08006870 CarrierPrivilegesTracker cpt = phone.getCarrierPrivilegesTracker();
6871 if (cpt == null) {
6872 loge("getCarrierPrivilegeStatusForUid: No CarrierPrivilegesTracker");
Jeff Davidson7e17e312018-02-13 18:17:36 -08006873 return TelephonyManager.CARRIER_PRIVILEGE_STATUS_RULES_NOT_LOADED;
6874 }
Hunter Knepshieldcad7f0b2021-12-13 15:08:35 -08006875 return cpt.getCarrierPrivilegeStatusForUid(uid);
Jeff Davidson7e17e312018-02-13 18:17:36 -08006876 }
6877
6878 @Override
chen xuf7e9fe82019-05-09 19:31:02 -07006879 public int checkCarrierPrivilegesForPackage(int subId, String pkgName) {
Nazanin1adf4562021-03-29 15:35:30 -07006880 enforceReadPrivilegedPermission("checkCarrierPrivilegesForPackage");
chen xuf7e9fe82019-05-09 19:31:02 -07006881 if (TextUtils.isEmpty(pkgName)) {
Junda Liu317d70b2016-03-08 09:33:53 -08006882 return TelephonyManager.CARRIER_PRIVILEGE_STATUS_NO_ACCESS;
chen xuf7e9fe82019-05-09 19:31:02 -07006883 }
Hunter Knepshieldcad7f0b2021-12-13 15:08:35 -08006884 Phone phone = getPhone(subId);
6885 if (phone == null) {
6886 loge("checkCarrierPrivilegesForPackage: Invalid subId");
6887 return TelephonyManager.CARRIER_PRIVILEGE_STATUS_NO_ACCESS;
6888 }
6889 CarrierPrivilegesTracker cpt = phone.getCarrierPrivilegesTracker();
6890 if (cpt == null) {
6891 loge("checkCarrierPrivilegesForPackage: No CarrierPrivilegesTracker");
Shishir Agrawaleb8771e2014-07-22 11:24:08 -07006892 return TelephonyManager.CARRIER_PRIVILEGE_STATUS_RULES_NOT_LOADED;
6893 }
Hunter Knepshieldcad7f0b2021-12-13 15:08:35 -08006894 return cpt.getCarrierPrivilegeStatusForPackage(pkgName);
Zach Johnson50ecba32015-05-19 00:24:21 -07006895 }
6896
6897 @Override
6898 public int checkCarrierPrivilegesForPackageAnyPhone(String pkgName) {
Hunter Knepshieldcad7f0b2021-12-13 15:08:35 -08006899 enforceReadPrivilegedPermission("checkCarrierPrivilegesForPackageAnyPhone");
Rambo Wange7209ce2022-02-23 13:41:02 -08006900 return checkCarrierPrivilegesForPackageAnyPhoneWithPermission(pkgName);
6901 }
6902
6903 private int checkCarrierPrivilegesForPackageAnyPhoneWithPermission(String pkgName) {
Hunter Knepshieldcad7f0b2021-12-13 15:08:35 -08006904 if (TextUtils.isEmpty(pkgName)) {
Junda Liu317d70b2016-03-08 09:33:53 -08006905 return TelephonyManager.CARRIER_PRIVILEGE_STATUS_NO_ACCESS;
Hunter Knepshieldcad7f0b2021-12-13 15:08:35 -08006906 }
Zach Johnson50ecba32015-05-19 00:24:21 -07006907 int result = TelephonyManager.CARRIER_PRIVILEGE_STATUS_RULES_NOT_LOADED;
Hunter Knepshieldcad7f0b2021-12-13 15:08:35 -08006908 for (int phoneId = 0; phoneId < TelephonyManager.getDefault().getPhoneCount(); phoneId++) {
6909 Phone phone = PhoneFactory.getPhone(phoneId);
6910 if (phone == null) {
6911 continue;
Zach Johnson50ecba32015-05-19 00:24:21 -07006912 }
Hunter Knepshieldcad7f0b2021-12-13 15:08:35 -08006913 CarrierPrivilegesTracker cpt = phone.getCarrierPrivilegesTracker();
6914 if (cpt == null) {
6915 continue;
6916 }
6917 result = cpt.getCarrierPrivilegeStatusForPackage(pkgName);
Zach Johnson50ecba32015-05-19 00:24:21 -07006918 if (result == TelephonyManager.CARRIER_PRIVILEGE_STATUS_HAS_ACCESS) {
6919 break;
6920 }
6921 }
Zach Johnson50ecba32015-05-19 00:24:21 -07006922 return result;
Junda Liu29340342014-07-10 15:23:27 -07006923 }
Derek Tan89e89d42014-07-08 17:00:10 -07006924
6925 @Override
Junda Liue64de782015-04-16 17:19:16 -07006926 public List<String> getCarrierPackageNamesForIntentAndPhone(Intent intent, int phoneId) {
Nazanin1adf4562021-03-29 15:35:30 -07006927 enforceReadPrivilegedPermission("getCarrierPackageNamesForIntentAndPhone");
Rambo Wang8a247eb2022-02-08 21:11:18 +00006928 Phone phone = PhoneFactory.getPhone(phoneId);
6929 if (phone == null) {
6930 return Collections.emptyList();
Junda Liue64de782015-04-16 17:19:16 -07006931 }
Rambo Wang8a247eb2022-02-08 21:11:18 +00006932 CarrierPrivilegesTracker cpt = phone.getCarrierPrivilegesTracker();
6933 if (cpt == null) {
6934 return Collections.emptyList();
Shishir Agrawaleb6439a2014-07-21 13:19:38 -07006935 }
Rambo Wang8a247eb2022-02-08 21:11:18 +00006936 return cpt.getCarrierPackageNamesForIntent(intent);
Shishir Agrawaleb6439a2014-07-21 13:19:38 -07006937 }
6938
Amith Yamasani6e118872016-02-19 12:53:51 -08006939 @Override
chen xuf7e9fe82019-05-09 19:31:02 -07006940 public List<String> getPackagesWithCarrierPrivileges(int phoneId) {
Nazanin1adf4562021-03-29 15:35:30 -07006941 enforceReadPrivilegedPermission("getPackagesWithCarrierPrivileges");
Hunter Knepshieldcad7f0b2021-12-13 15:08:35 -08006942 Phone phone = PhoneFactory.getPhone(phoneId);
6943 if (phone == null) {
6944 return Collections.emptyList();
Amith Yamasani6e118872016-02-19 12:53:51 -08006945 }
Hunter Knepshieldcad7f0b2021-12-13 15:08:35 -08006946 CarrierPrivilegesTracker cpt = phone.getCarrierPrivilegesTracker();
6947 if (cpt == null) {
6948 return Collections.emptyList();
6949 }
6950 return new ArrayList<>(cpt.getPackagesWithCarrierPrivileges());
Amith Yamasani6e118872016-02-19 12:53:51 -08006951 }
6952
chen xuf7e9fe82019-05-09 19:31:02 -07006953 @Override
6954 public List<String> getPackagesWithCarrierPrivilegesForAllPhones() {
Shuo Qian067a06d2019-12-03 23:40:18 +00006955 enforceReadPrivilegedPermission("getPackagesWithCarrierPrivilegesForAllPhones");
Hunter Knepshieldcad7f0b2021-12-13 15:08:35 -08006956 Set<String> privilegedPackages = new ArraySet<>();
Shuo Qian067a06d2019-12-03 23:40:18 +00006957 final long identity = Binder.clearCallingIdentity();
Shuo Qian067a06d2019-12-03 23:40:18 +00006958 try {
6959 for (int i = 0; i < TelephonyManager.getDefault().getPhoneCount(); i++) {
6960 privilegedPackages.addAll(getPackagesWithCarrierPrivileges(i));
6961 }
6962 } finally {
6963 Binder.restoreCallingIdentity(identity);
chen xuf7e9fe82019-05-09 19:31:02 -07006964 }
Hunter Knepshieldcad7f0b2021-12-13 15:08:35 -08006965 return new ArrayList<>(privilegedPackages);
chen xuf7e9fe82019-05-09 19:31:02 -07006966 }
6967
Rambo Wang6812ffb2022-03-15 16:54:17 -07006968 @Override
6969 public @Nullable String getCarrierServicePackageNameForLogicalSlot(int logicalSlotIndex) {
6970 enforceReadPrivilegedPermission("getCarrierServicePackageNameForLogicalSlot");
6971
6972 final Phone phone = PhoneFactory.getPhone(logicalSlotIndex);
6973 if (phone == null) {
6974 return null;
6975 }
6976 final CarrierPrivilegesTracker cpt = phone.getCarrierPrivilegesTracker();
6977 if (cpt == null) {
6978 return null;
6979 }
6980 return cpt.getCarrierServicePackageName();
6981 }
6982
Wink Savilleb564aae2014-10-23 10:18:09 -07006983 private String getIccId(int subId) {
Sanket Padawe356d7632015-06-22 14:03:32 -07006984 final Phone phone = getPhone(subId);
Muralidhar Reddy472c2ae2021-09-29 19:38:40 +00006985 UiccPort port = phone == null ? null : phone.getUiccPort();
6986 if (port == null) {
Derek Tan97ebb422014-09-05 16:55:38 -07006987 return null;
6988 }
Muralidhar Reddy472c2ae2021-09-29 19:38:40 +00006989 String iccId = port.getIccId();
Derek Tan97ebb422014-09-05 16:55:38 -07006990 if (TextUtils.isEmpty(iccId)) {
Derek Tan97ebb422014-09-05 16:55:38 -07006991 return null;
6992 }
6993 return iccId;
6994 }
6995
Shishir Agrawaleb6439a2014-07-21 13:19:38 -07006996 @Override
Shuo Qiane4e11672020-12-15 22:15:33 -08006997 public void setCallComposerStatus(int subId, int status) {
6998 enforceModifyPermission();
6999
7000 final long identity = Binder.clearCallingIdentity();
7001 try {
7002 Phone phone = getPhone(subId);
7003 if (phone != null) {
7004 Phone defaultPhone = phone.getImsPhone();
7005 if (defaultPhone != null && defaultPhone.getPhoneType() == PHONE_TYPE_IMS) {
7006 ImsPhone imsPhone = (ImsPhone) defaultPhone;
7007 imsPhone.setCallComposerStatus(status);
Shuo Qian284ae752020-12-22 19:10:14 -08007008 ImsManager.getInstance(mApp, getSlotIndexOrException(subId))
7009 .updateImsServiceConfig();
Shuo Qiane4e11672020-12-15 22:15:33 -08007010 }
7011 }
Shuo Qian284ae752020-12-22 19:10:14 -08007012 } catch (ImsException e) {
7013 throw new ServiceSpecificException(e.getCode());
7014 } finally {
Shuo Qiane4e11672020-12-15 22:15:33 -08007015 Binder.restoreCallingIdentity(identity);
7016 }
7017 }
7018
7019 @Override
7020 public int getCallComposerStatus(int subId) {
7021 enforceReadPrivilegedPermission("getCallComposerStatus");
7022
7023 final long identity = Binder.clearCallingIdentity();
7024 try {
7025 Phone phone = getPhone(subId);
7026 if (phone != null) {
7027 Phone defaultPhone = phone.getImsPhone();
7028 if (defaultPhone != null && defaultPhone.getPhoneType() == PHONE_TYPE_IMS) {
7029 ImsPhone imsPhone = (ImsPhone) defaultPhone;
7030 return imsPhone.getCallComposerStatus();
7031 }
7032 }
7033 } finally {
7034 Binder.restoreCallingIdentity(identity);
7035 }
7036 return TelephonyManager.CALL_COMPOSER_STATUS_OFF;
7037 }
7038
7039 @Override
Jeff Sharkey85190e62014-12-05 09:40:12 -08007040 public boolean setLine1NumberForDisplayForSubscriber(int subId, String alphaTag,
7041 String number) {
Shuo Qian2c0ae432019-12-05 11:40:37 -08007042 TelephonyPermissions.enforceCallingOrSelfCarrierPrivilege(mApp,
Jeff Davidson7e17e312018-02-13 18:17:36 -08007043 subId, "setLine1NumberForDisplayForSubscriber");
Derek Tan97ebb422014-09-05 16:55:38 -07007044
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007045 final long identity = Binder.clearCallingIdentity();
7046 try {
7047 final String iccId = getIccId(subId);
7048 final Phone phone = getPhone(subId);
7049 if (phone == null) {
7050 return false;
7051 }
7052 final String subscriberId = phone.getSubscriberId();
7053
7054 if (DBG_MERGE) {
Amit Mahajanb8f13202020-01-27 18:16:07 -08007055 Rlog.d(LOG_TAG, "Setting line number for ICC=" + iccId + ", subscriberId="
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007056 + subscriberId + " to " + number);
7057 }
7058
7059 if (TextUtils.isEmpty(iccId)) {
7060 return false;
7061 }
7062
7063 final SharedPreferences.Editor editor = mTelephonySharedPreferences.edit();
7064
7065 final String alphaTagPrefKey = PREF_CARRIERS_ALPHATAG_PREFIX + iccId;
7066 if (alphaTag == null) {
7067 editor.remove(alphaTagPrefKey);
7068 } else {
7069 editor.putString(alphaTagPrefKey, alphaTag);
7070 }
7071
7072 // Record both the line number and IMSI for this ICCID, since we need to
7073 // track all merged IMSIs based on line number
7074 final String numberPrefKey = PREF_CARRIERS_NUMBER_PREFIX + iccId;
7075 final String subscriberPrefKey = PREF_CARRIERS_SUBSCRIBER_PREFIX + iccId;
7076 if (number == null) {
7077 editor.remove(numberPrefKey);
7078 editor.remove(subscriberPrefKey);
7079 } else {
7080 editor.putString(numberPrefKey, number);
7081 editor.putString(subscriberPrefKey, subscriberId);
7082 }
7083
7084 editor.commit();
7085 return true;
7086 } finally {
7087 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07007088 }
Derek Tan7226c842014-07-02 17:42:23 -07007089 }
7090
7091 @Override
Philip P. Moltmann700a9592019-10-03 11:53:50 -07007092 public String getLine1NumberForDisplay(int subId, String callingPackage,
7093 String callingFeatureId) {
Makoto Onukifee69342015-06-29 14:44:50 -07007094 // This is open to apps with WRITE_SMS.
Jeff Davidson7e17e312018-02-13 18:17:36 -08007095 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneNumber(
Philip P. Moltmann700a9592019-10-03 11:53:50 -07007096 mApp, subId, callingPackage, callingFeatureId, "getLine1NumberForDisplay")) {
Amit Mahajan9cf11512015-11-09 11:40:48 -08007097 if (DBG_MERGE) log("getLine1NumberForDisplay returning null due to permission");
Svet Ganovb320e182015-04-16 12:30:10 -07007098 return null;
7099 }
Derek Tan97ebb422014-09-05 16:55:38 -07007100
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007101 final long identity = Binder.clearCallingIdentity();
7102 try {
7103 String iccId = getIccId(subId);
7104 if (iccId != null) {
7105 String numberPrefKey = PREF_CARRIERS_NUMBER_PREFIX + iccId;
7106 if (DBG_MERGE) {
7107 log("getLine1NumberForDisplay returning "
7108 + mTelephonySharedPreferences.getString(numberPrefKey, null));
7109 }
7110 return mTelephonySharedPreferences.getString(numberPrefKey, null);
Amit Mahajan9cf11512015-11-09 11:40:48 -08007111 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007112 if (DBG_MERGE) log("getLine1NumberForDisplay returning null as iccId is null");
7113 return null;
7114 } finally {
7115 Binder.restoreCallingIdentity(identity);
Derek Tan7226c842014-07-02 17:42:23 -07007116 }
Derek Tan7226c842014-07-02 17:42:23 -07007117 }
7118
7119 @Override
Philip P. Moltmann700a9592019-10-03 11:53:50 -07007120 public String getLine1AlphaTagForDisplay(int subId, String callingPackage,
7121 String callingFeatureId) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08007122 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
Philip P. Moltmann700a9592019-10-03 11:53:50 -07007123 mApp, subId, callingPackage, callingFeatureId, "getLine1AlphaTagForDisplay")) {
Svet Ganovb320e182015-04-16 12:30:10 -07007124 return null;
7125 }
Derek Tan97ebb422014-09-05 16:55:38 -07007126
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007127 final long identity = Binder.clearCallingIdentity();
7128 try {
7129 String iccId = getIccId(subId);
7130 if (iccId != null) {
7131 String alphaTagPrefKey = PREF_CARRIERS_ALPHATAG_PREFIX + iccId;
7132 return mTelephonySharedPreferences.getString(alphaTagPrefKey, null);
7133 }
7134 return null;
7135 } finally {
7136 Binder.restoreCallingIdentity(identity);
Derek Tan7226c842014-07-02 17:42:23 -07007137 }
Derek Tan7226c842014-07-02 17:42:23 -07007138 }
Shishir Agrawalb1ebf8c2014-07-17 16:32:41 -07007139
7140 @Override
Philip P. Moltmann700a9592019-10-03 11:53:50 -07007141 public String[] getMergedSubscriberIds(int subId, String callingPackage,
7142 String callingFeatureId) {
Jeff Davidson913390f2018-02-23 17:11:49 -08007143 // This API isn't public, so no need to provide a valid subscription ID - we're not worried
7144 // about carrier-privileged callers not having access.
Jeff Davidson7e17e312018-02-13 18:17:36 -08007145 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
Jeff Davidson913390f2018-02-23 17:11:49 -08007146 mApp, SubscriptionManager.INVALID_SUBSCRIPTION_ID, callingPackage,
Philip P. Moltmann700a9592019-10-03 11:53:50 -07007147 callingFeatureId, "getMergedSubscriberIds")) {
Fyodor Kupolov8e53b0b2015-06-17 13:17:50 -07007148 return null;
7149 }
Jeff Sharkey85190e62014-12-05 09:40:12 -08007150
Jordan Liub49b04b2019-05-06 14:45:15 -07007151 // Clear calling identity, when calling TelephonyManager, because callerUid must be
7152 // the process, where TelephonyManager was instantiated.
7153 // Otherwise AppOps check will fail.
Fyodor Kupolov8e53b0b2015-06-17 13:17:50 -07007154 final long identity = Binder.clearCallingIdentity();
7155 try {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08007156 final Context context = mApp;
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007157 final TelephonyManager tele = TelephonyManager.from(context);
7158 final SubscriptionManager sub = SubscriptionManager.from(context);
7159
7160 // Figure out what subscribers are currently active
7161 final ArraySet<String> activeSubscriberIds = new ArraySet<>();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007162
Jordan Liub49b04b2019-05-06 14:45:15 -07007163 // Only consider subs which match the current subId
7164 // This logic can be simplified. See b/131189269 for progress.
7165 if (isActiveSubscription(subId)) {
Fyodor Kupolov8e53b0b2015-06-17 13:17:50 -07007166 activeSubscriberIds.add(tele.getSubscriberId(subId));
7167 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007168
7169 // First pass, find a number override for an active subscriber
7170 String mergeNumber = null;
7171 final Map<String, ?> prefs = mTelephonySharedPreferences.getAll();
7172 for (String key : prefs.keySet()) {
7173 if (key.startsWith(PREF_CARRIERS_SUBSCRIBER_PREFIX)) {
7174 final String subscriberId = (String) prefs.get(key);
7175 if (activeSubscriberIds.contains(subscriberId)) {
7176 final String iccId = key.substring(
7177 PREF_CARRIERS_SUBSCRIBER_PREFIX.length());
7178 final String numberKey = PREF_CARRIERS_NUMBER_PREFIX + iccId;
7179 mergeNumber = (String) prefs.get(numberKey);
7180 if (DBG_MERGE) {
Amit Mahajanb8f13202020-01-27 18:16:07 -08007181 Rlog.d(LOG_TAG, "Found line number " + mergeNumber
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007182 + " for active subscriber " + subscriberId);
7183 }
7184 if (!TextUtils.isEmpty(mergeNumber)) {
7185 break;
7186 }
7187 }
7188 }
7189 }
7190
7191 // Shortcut when no active merged subscribers
7192 if (TextUtils.isEmpty(mergeNumber)) {
7193 return null;
7194 }
7195
7196 // Second pass, find all subscribers under that line override
7197 final ArraySet<String> result = new ArraySet<>();
7198 for (String key : prefs.keySet()) {
7199 if (key.startsWith(PREF_CARRIERS_NUMBER_PREFIX)) {
7200 final String number = (String) prefs.get(key);
7201 if (mergeNumber.equals(number)) {
7202 final String iccId = key.substring(PREF_CARRIERS_NUMBER_PREFIX.length());
7203 final String subscriberKey = PREF_CARRIERS_SUBSCRIBER_PREFIX + iccId;
7204 final String subscriberId = (String) prefs.get(subscriberKey);
7205 if (!TextUtils.isEmpty(subscriberId)) {
7206 result.add(subscriberId);
7207 }
7208 }
7209 }
7210 }
7211
7212 final String[] resultArray = result.toArray(new String[result.size()]);
7213 Arrays.sort(resultArray);
7214 if (DBG_MERGE) {
Amit Mahajanb8f13202020-01-27 18:16:07 -08007215 Rlog.d(LOG_TAG,
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007216 "Found subscribers " + Arrays.toString(resultArray) + " after merge");
7217 }
7218 return resultArray;
Fyodor Kupolov8e53b0b2015-06-17 13:17:50 -07007219 } finally {
7220 Binder.restoreCallingIdentity(identity);
Jeff Sharkey85190e62014-12-05 09:40:12 -08007221 }
Jeff Sharkey85190e62014-12-05 09:40:12 -08007222 }
7223
7224 @Override
zoey chen38003472019-12-13 17:16:31 +08007225 public String[] getMergedImsisFromGroup(int subId, String callingPackage) {
7226 enforceReadPrivilegedPermission("getMergedImsisFromGroup");
Malcolm Chen6ca97372019-07-01 16:28:21 -07007227
7228 final long identity = Binder.clearCallingIdentity();
7229 try {
7230 final TelephonyManager telephonyManager = mApp.getSystemService(
7231 TelephonyManager.class);
7232 String subscriberId = telephonyManager.getSubscriberId(subId);
7233 if (subscriberId == null) {
7234 if (DBG) {
zoey chen38003472019-12-13 17:16:31 +08007235 log("getMergedImsisFromGroup can't find subscriberId for subId "
Malcolm Chen6ca97372019-07-01 16:28:21 -07007236 + subId);
7237 }
7238 return null;
7239 }
7240
7241 final SubscriptionInfo info = SubscriptionController.getInstance()
7242 .getSubscriptionInfo(subId);
7243 final ParcelUuid groupUuid = info.getGroupUuid();
7244 // If it doesn't belong to any group, return just subscriberId of itself.
7245 if (groupUuid == null) {
7246 return new String[]{subscriberId};
7247 }
7248
7249 // Get all subscriberIds from the group.
7250 final List<String> mergedSubscriberIds = new ArrayList<>();
7251 final List<SubscriptionInfo> groupInfos = SubscriptionController.getInstance()
Philip P. Moltmann700a9592019-10-03 11:53:50 -07007252 .getSubscriptionsInGroup(groupUuid, mApp.getOpPackageName(),
Philip P. Moltmann8d34f0c2020-03-05 16:24:02 -08007253 mApp.getAttributionTag());
Malcolm Chen6ca97372019-07-01 16:28:21 -07007254 for (SubscriptionInfo subInfo : groupInfos) {
7255 subscriberId = telephonyManager.getSubscriberId(subInfo.getSubscriptionId());
7256 if (subscriberId != null) {
7257 mergedSubscriberIds.add(subscriberId);
7258 }
7259 }
7260
7261 return mergedSubscriberIds.toArray(new String[mergedSubscriberIds.size()]);
7262 } finally {
7263 Binder.restoreCallingIdentity(identity);
7264
7265 }
7266 }
7267
7268 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08007269 public boolean setOperatorBrandOverride(int subId, String brand) {
Shuo Qian2c0ae432019-12-05 11:40:37 -08007270 TelephonyPermissions.enforceCallingOrSelfCarrierPrivilege(mApp,
Jeff Davidson7e17e312018-02-13 18:17:36 -08007271 subId, "setOperatorBrandOverride");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007272
7273 final long identity = Binder.clearCallingIdentity();
7274 try {
7275 final Phone phone = getPhone(subId);
7276 return phone == null ? false : phone.setOperatorBrandOverride(brand);
7277 } finally {
7278 Binder.restoreCallingIdentity(identity);
7279 }
Shishir Agrawalb1ebf8c2014-07-17 16:32:41 -07007280 }
Steven Liu4bf01bc2014-07-17 11:05:29 -05007281
7282 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08007283 public boolean setRoamingOverride(int subId, List<String> gsmRoamingList,
Shishir Agrawal621a47c2014-12-01 10:25:09 -08007284 List<String> gsmNonRoamingList, List<String> cdmaRoamingList,
7285 List<String> cdmaNonRoamingList) {
Shuo Qian2c0ae432019-12-05 11:40:37 -08007286 TelephonyPermissions.enforceCallingOrSelfCarrierPrivilege(
7287 mApp, subId, "setRoamingOverride");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007288
7289 final long identity = Binder.clearCallingIdentity();
7290 try {
7291 final Phone phone = getPhone(subId);
7292 if (phone == null) {
7293 return false;
7294 }
7295 return phone.setRoamingOverride(gsmRoamingList, gsmNonRoamingList, cdmaRoamingList,
7296 cdmaNonRoamingList);
7297 } finally {
7298 Binder.restoreCallingIdentity(identity);
Shishir Agrawalc04d9752016-02-19 10:41:00 -08007299 }
Shishir Agrawal621a47c2014-12-01 10:25:09 -08007300 }
7301
7302 @Override
Shuo Qian850e4d6a2018-04-25 21:02:08 +00007303 @Deprecated
7304 public int invokeOemRilRequestRaw(byte[] oemReq, byte[] oemResp) {
7305 enforceModifyPermission();
7306
7307 int returnValue = 0;
7308 try {
vagdeviaf9a5b92018-08-15 16:01:53 -07007309 AsyncResult result = (AsyncResult) sendRequest(CMD_INVOKE_OEM_RIL_REQUEST_RAW, oemReq);
Shuo Qian850e4d6a2018-04-25 21:02:08 +00007310 if(result.exception == null) {
7311 if (result.result != null) {
7312 byte[] responseData = (byte[])(result.result);
7313 if(responseData.length > oemResp.length) {
7314 Log.w(LOG_TAG, "Buffer to copy response too small: Response length is " +
7315 responseData.length + "bytes. Buffer Size is " +
7316 oemResp.length + "bytes.");
7317 }
7318 System.arraycopy(responseData, 0, oemResp, 0, responseData.length);
7319 returnValue = responseData.length;
7320 }
7321 } else {
7322 CommandException ex = (CommandException) result.exception;
7323 returnValue = ex.getCommandError().ordinal();
7324 if(returnValue > 0) returnValue *= -1;
7325 }
7326 } catch (RuntimeException e) {
7327 Log.w(LOG_TAG, "sendOemRilRequestRaw: Runtime Exception");
7328 returnValue = (CommandException.Error.GENERIC_FAILURE.ordinal());
7329 if(returnValue > 0) returnValue *= -1;
7330 }
7331
7332 return returnValue;
7333 }
7334
7335 @Override
Robert Greenwalt36b23af2015-07-06 17:59:14 -07007336 public int getRadioAccessFamily(int phoneId, String callingPackage) {
Jeff Davidson913390f2018-02-23 17:11:49 -08007337 Phone phone = PhoneFactory.getPhone(phoneId);
Shuo Qiandee53402020-05-29 14:08:15 -07007338 try {
7339 TelephonyPermissions
Nathan Harold62c68512021-04-06 11:26:02 -07007340 .enforceCallingOrSelfReadPrivilegedPhoneStatePermissionOrCarrierPrivilege(
Shuo Qiandee53402020-05-29 14:08:15 -07007341 mApp, phone.getSubId(), "getRadioAccessFamily");
7342 } catch (SecurityException e) {
7343 EventLog.writeEvent(0x534e4554, "150857259", -1, "Missing Permission");
7344 throw e;
7345 }
chen xub97461a2018-10-26 14:17:57 -07007346 int raf = RadioAccessFamily.RAF_UNKNOWN;
Jeff Davidson913390f2018-02-23 17:11:49 -08007347 if (phone == null) {
chen xub97461a2018-10-26 14:17:57 -07007348 return raf;
Jeff Davidson913390f2018-02-23 17:11:49 -08007349 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007350 final long identity = Binder.clearCallingIdentity();
7351 try {
chen xub97461a2018-10-26 14:17:57 -07007352 TelephonyPermissions
Nathan Harold62c68512021-04-06 11:26:02 -07007353 .enforceCallingOrSelfReadPrivilegedPhoneStatePermissionOrCarrierPrivilege(
chen xub97461a2018-10-26 14:17:57 -07007354 mApp, phone.getSubId(), "getRadioAccessFamily");
7355 raf = ProxyController.getInstance().getRadioAccessFamily(phoneId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007356 } finally {
7357 Binder.restoreCallingIdentity(identity);
7358 }
chen xub97461a2018-10-26 14:17:57 -07007359 return raf;
Wink Saville5d475dd2014-10-17 15:00:58 -07007360 }
Andrew Leedf14ead2014-10-17 14:22:52 -07007361
7362 @Override
Hall Liu82694d52020-12-11 18:22:04 -08007363 public void uploadCallComposerPicture(int subscriptionId, String callingPackage,
Hall Liue31bac62020-12-23 19:16:10 -08007364 String contentType, ParcelFileDescriptor fd, ResultReceiver callback) {
Hall Liu82694d52020-12-11 18:22:04 -08007365 try {
7366 if (!Objects.equals(mApp.getPackageManager().getPackageUid(callingPackage, 0),
7367 Binder.getCallingUid())) {
Tyler Gunnb87925a2021-06-10 14:54:27 -07007368 throw new SecurityException("Invalid package:" + callingPackage);
Hall Liu82694d52020-12-11 18:22:04 -08007369 }
7370 } catch (PackageManager.NameNotFoundException e) {
Tyler Gunnb87925a2021-06-10 14:54:27 -07007371 throw new SecurityException("Invalid package:" + callingPackage);
Hall Liu82694d52020-12-11 18:22:04 -08007372 }
7373 RoleManager rm = mApp.getSystemService(RoleManager.class);
7374 List<String> dialerRoleHolders = rm.getRoleHolders(RoleManager.ROLE_DIALER);
7375 if (!dialerRoleHolders.contains(callingPackage)) {
7376 throw new SecurityException("App must be the dialer role holder to"
7377 + " upload a call composer pic");
7378 }
7379
7380 Executors.newSingleThreadExecutor().execute(() -> {
7381 ByteArrayOutputStream output = new ByteArrayOutputStream(
7382 (int) TelephonyManager.getMaximumCallComposerPictureSize());
7383 InputStream input = new ParcelFileDescriptor.AutoCloseInputStream(fd);
7384 boolean readUntilEnd = false;
7385 int totalBytesRead = 0;
7386 byte[] buffer = new byte[16 * 1024];
7387 while (true) {
7388 int numRead;
7389 try {
7390 numRead = input.read(buffer);
7391 } catch (IOException e) {
7392 try {
7393 fd.checkError();
7394 callback.send(TelephonyManager.CallComposerException.ERROR_INPUT_CLOSED,
7395 null);
7396 } catch (IOException e1) {
7397 // This means that the other side closed explicitly with an error. If this
7398 // happens, log and ignore.
7399 loge("Remote end of call composer picture pipe closed: " + e1);
7400 }
7401 break;
7402 }
7403 if (numRead == -1) {
7404 readUntilEnd = true;
7405 break;
7406 }
7407 totalBytesRead += numRead;
7408 if (totalBytesRead > TelephonyManager.getMaximumCallComposerPictureSize()) {
7409 loge("Too many bytes read for call composer picture: " + totalBytesRead);
7410 try {
7411 input.close();
7412 } catch (IOException e) {
7413 // ignore
7414 }
7415 break;
7416 }
7417 output.write(buffer, 0, numRead);
7418 }
7419 // Generally, the remote end will close the file descriptors. The only case where we
7420 // close is above, where the picture size is too big.
7421
7422 try {
7423 fd.checkError();
7424 } catch (IOException e) {
7425 loge("Remote end for call composer closed with an error: " + e);
7426 return;
7427 }
7428
Hall Liuaa4211e2021-01-20 15:43:39 -08007429 if (!readUntilEnd) {
7430 loge("Did not finish reading entire image; aborting");
7431 return;
7432 }
Hall Liu82694d52020-12-11 18:22:04 -08007433
Hall Liuaa4211e2021-01-20 15:43:39 -08007434 ImageData imageData = new ImageData(output.toByteArray(), contentType, null);
7435 CallComposerPictureManager.getInstance(mApp, subscriptionId).handleUploadToServer(
7436 new CallComposerPictureTransfer.Factory() {},
7437 imageData,
7438 (result) -> {
7439 if (result.first != null) {
7440 ParcelUuid parcelUuid = new ParcelUuid(result.first);
7441 Bundle outputResult = new Bundle();
7442 outputResult.putParcelable(
7443 TelephonyManager.KEY_CALL_COMPOSER_PICTURE_HANDLE, parcelUuid);
7444 callback.send(TelephonyManager.CallComposerException.SUCCESS,
7445 outputResult);
7446 } else {
7447 callback.send(result.second, null);
7448 }
7449 }
7450 );
Hall Liu82694d52020-12-11 18:22:04 -08007451 });
7452 }
7453
7454 @Override
Andrew Leedf14ead2014-10-17 14:22:52 -07007455 public void enableVideoCalling(boolean enable) {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08007456 final Phone defaultPhone = getDefaultPhone();
Andrew Leedf14ead2014-10-17 14:22:52 -07007457 enforceModifyPermission();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007458
7459 final long identity = Binder.clearCallingIdentity();
7460 try {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08007461 ImsManager.getInstance(defaultPhone.getContext(),
7462 defaultPhone.getPhoneId()).setVtSetting(enable);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007463 } finally {
7464 Binder.restoreCallingIdentity(identity);
7465 }
Andrew Leedf14ead2014-10-17 14:22:52 -07007466 }
7467
7468 @Override
Philip P. Moltmann700a9592019-10-03 11:53:50 -07007469 public boolean isVideoCallingEnabled(String callingPackage, String callingFeatureId) {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08007470 final Phone defaultPhone = getDefaultPhone();
Philip P. Moltmann700a9592019-10-03 11:53:50 -07007471 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(mApp, defaultPhone.getSubId(),
7472 callingPackage, callingFeatureId, "isVideoCallingEnabled")) {
Amit Mahajan578e53d2018-03-20 16:18:38 +00007473 return false;
7474 }
Svet Ganovb320e182015-04-16 12:30:10 -07007475
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007476 final long identity = Binder.clearCallingIdentity();
7477 try {
7478 // Check the user preference and the system-level IMS setting. Even if the user has
7479 // enabled video calling, if IMS is disabled we aren't able to support video calling.
7480 // In the long run, we may instead need to check if there exists a connection service
7481 // which can support video calling.
7482 ImsManager imsManager =
Nazanin Bakhshif782e562018-12-11 15:15:39 -08007483 ImsManager.getInstance(defaultPhone.getContext(), defaultPhone.getPhoneId());
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007484 return imsManager.isVtEnabledByPlatform()
7485 && imsManager.isEnhanced4gLteModeSettingEnabledByUser()
7486 && imsManager.isVtEnabledByUser();
7487 } finally {
7488 Binder.restoreCallingIdentity(identity);
7489 }
Andrew Leedf14ead2014-10-17 14:22:52 -07007490 }
Libin.Tang@motorola.comafe82642014-12-18 13:27:53 -06007491
Andrew Leea1239f22015-03-02 17:44:07 -08007492 @Override
Philip P. Moltmann700a9592019-10-03 11:53:50 -07007493 public boolean canChangeDtmfToneLength(int subId, String callingPackage,
7494 String callingFeatureId) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007495 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
Philip P. Moltmann700a9592019-10-03 11:53:50 -07007496 mApp, subId, callingPackage, callingFeatureId,
7497 "isVideoCallingEnabled")) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007498 return false;
7499 }
7500
7501 final long identity = Binder.clearCallingIdentity();
7502 try {
7503 CarrierConfigManager configManager =
7504 (CarrierConfigManager) mApp.getSystemService(Context.CARRIER_CONFIG_SERVICE);
Nazanin Bakhshif782e562018-12-11 15:15:39 -08007505 return configManager.getConfigForSubId(subId)
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007506 .getBoolean(CarrierConfigManager.KEY_DTMF_TYPE_ENABLED_BOOL);
7507 } finally {
7508 Binder.restoreCallingIdentity(identity);
7509 }
Andrew Leea1239f22015-03-02 17:44:07 -08007510 }
7511
7512 @Override
Philip P. Moltmann700a9592019-10-03 11:53:50 -07007513 public boolean isWorldPhone(int subId, String callingPackage, String callingFeatureId) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007514 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
Philip P. Moltmann700a9592019-10-03 11:53:50 -07007515 mApp, subId, callingPackage, callingFeatureId, "isVideoCallingEnabled")) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007516 return false;
7517 }
7518
7519 final long identity = Binder.clearCallingIdentity();
7520 try {
7521 CarrierConfigManager configManager =
7522 (CarrierConfigManager) mApp.getSystemService(Context.CARRIER_CONFIG_SERVICE);
Nazanin Bakhshif782e562018-12-11 15:15:39 -08007523 return configManager.getConfigForSubId(subId)
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007524 .getBoolean(CarrierConfigManager.KEY_WORLD_PHONE_BOOL);
7525 } finally {
7526 Binder.restoreCallingIdentity(identity);
7527 }
Andrew Leea1239f22015-03-02 17:44:07 -08007528 }
7529
Andrew Lee9431b832015-03-09 18:46:45 -07007530 @Override
7531 public boolean isTtyModeSupported() {
Tyler Gunn5ddfdc92019-10-31 13:08:23 -07007532 TelecomManager telecomManager = mApp.getSystemService(TelecomManager.class);
Wooki Wu1f82f7a2016-02-15 15:59:58 +08007533 return telecomManager.isTtySupported();
Andrew Lee9431b832015-03-09 18:46:45 -07007534 }
7535
7536 @Override
7537 public boolean isHearingAidCompatibilitySupported() {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007538 final long identity = Binder.clearCallingIdentity();
7539 try {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08007540 return mApp.getResources().getBoolean(R.bool.hac_enabled);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007541 } finally {
7542 Binder.restoreCallingIdentity(identity);
7543 }
Andrew Lee9431b832015-03-09 18:46:45 -07007544 }
7545
Hall Liuf6668912018-10-31 17:05:23 -07007546 /**
7547 * Determines whether the device currently supports RTT (Real-time text). Based both on carrier
7548 * support for the feature and device firmware support.
7549 *
7550 * @return {@code true} if the device and carrier both support RTT, {@code false} otherwise.
7551 */
7552 @Override
7553 public boolean isRttSupported(int subscriptionId) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007554 final long identity = Binder.clearCallingIdentity();
Nazanin Bakhshif782e562018-12-11 15:15:39 -08007555 final Phone phone = getPhone(subscriptionId);
7556 if (phone == null) {
7557 loge("isRttSupported: no Phone found. Invalid subId:" + subscriptionId);
7558 return false;
7559 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007560 try {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08007561 boolean isCarrierSupported = mApp.getCarrierConfigForSubId(subscriptionId).getBoolean(
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007562 CarrierConfigManager.KEY_RTT_SUPPORTED_BOOL);
7563 boolean isDeviceSupported =
Nazanin Bakhshif782e562018-12-11 15:15:39 -08007564 phone.getContext().getResources().getBoolean(R.bool.config_support_rtt);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007565 return isCarrierSupported && isDeviceSupported;
7566 } finally {
7567 Binder.restoreCallingIdentity(identity);
7568 }
Hall Liu98187582018-01-22 19:15:32 -08007569 }
7570
Hall Liuf6668912018-10-31 17:05:23 -07007571 /**
Hall Liuf2daa022019-07-23 18:39:00 -07007572 * Determines whether the user has turned on RTT. If the carrier wants to ignore the user-set
7573 * RTT setting, will return true if the device and carrier both support RTT.
7574 * Otherwise. only returns true if the device and carrier both also support RTT.
Hall Liuf6668912018-10-31 17:05:23 -07007575 */
7576 public boolean isRttEnabled(int subscriptionId) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007577 final long identity = Binder.clearCallingIdentity();
7578 try {
Hall Liu5bab75c2019-12-11 23:58:20 +00007579 boolean isRttSupported = isRttSupported(subscriptionId);
7580 boolean isUserRttSettingOn = Settings.Secure.getInt(
7581 mApp.getContentResolver(), Settings.Secure.RTT_CALLING_MODE, 0) != 0;
7582 boolean shouldIgnoreUserRttSetting = mApp.getCarrierConfigForSubId(subscriptionId)
7583 .getBoolean(CarrierConfigManager.KEY_IGNORE_RTT_MODE_SETTING_BOOL);
7584 return isRttSupported && (isUserRttSettingOn || shouldIgnoreUserRttSetting);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007585 } finally {
7586 Binder.restoreCallingIdentity(identity);
7587 }
Hall Liu3ad5f012018-04-06 16:23:39 -07007588 }
7589
Philip P. Moltmann700a9592019-10-03 11:53:50 -07007590 @Deprecated
7591 @Override
7592 public String getDeviceId(String callingPackage) {
7593 return getDeviceIdWithFeature(callingPackage, null);
7594 }
7595
Sanket Padawe7310cc72015-01-14 09:53:20 -08007596 /**
7597 * Returns the unique device ID of phone, for example, the IMEI for
7598 * GSM and the MEID for CDMA phones. Return null if device ID is not available.
7599 *
7600 * <p>Requires Permission:
7601 * {@link android.Manifest.permission#READ_PHONE_STATE READ_PHONE_STATE}
7602 */
7603 @Override
Philip P. Moltmann700a9592019-10-03 11:53:50 -07007604 public String getDeviceIdWithFeature(String callingPackage, String callingFeatureId) {
Shuo Qian13d89152021-05-10 23:58:11 -07007605 try {
7606 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
7607 } catch (SecurityException se) {
7608 EventLog.writeEvent(0x534e4554, "186530889", Binder.getCallingUid());
7609 throw new SecurityException("Package " + callingPackage + " does not belong to "
7610 + Binder.getCallingUid());
7611 }
Sanket Padawe7310cc72015-01-14 09:53:20 -08007612 final Phone phone = PhoneFactory.getPhone(0);
Jeff Davidson913390f2018-02-23 17:11:49 -08007613 if (phone == null) {
Sanket Padawe7310cc72015-01-14 09:53:20 -08007614 return null;
7615 }
Jeff Davidson913390f2018-02-23 17:11:49 -08007616 int subId = phone.getSubId();
Michael Groover70af6dc2018-10-01 16:23:15 -07007617 if (!TelephonyPermissions.checkCallingOrSelfReadDeviceIdentifiers(mApp, subId,
Philip P. Moltmann700a9592019-10-03 11:53:50 -07007618 callingPackage, callingFeatureId, "getDeviceId")) {
Jeff Davidson913390f2018-02-23 17:11:49 -08007619 return null;
7620 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007621
7622 final long identity = Binder.clearCallingIdentity();
7623 try {
7624 return phone.getDeviceId();
7625 } finally {
7626 Binder.restoreCallingIdentity(identity);
7627 }
Sanket Padawe7310cc72015-01-14 09:53:20 -08007628 }
7629
Ping Sunc67b7c22016-03-02 19:16:45 +08007630 /**
7631 * {@hide}
7632 * Returns the IMS Registration Status on a particular subid
7633 *
7634 * @param subId
7635 */
Brad Ebinger1f2b5082018-02-08 16:11:32 -08007636 public boolean isImsRegistered(int subId) {
Ping Sunc67b7c22016-03-02 19:16:45 +08007637 Phone phone = getPhone(subId);
7638 if (phone != null) {
7639 return phone.isImsRegistered();
7640 } else {
7641 return false;
7642 }
7643 }
7644
Santos Cordon7a1885b2015-02-03 11:15:19 -08007645 @Override
Shuo Qian6e6137d2019-10-30 16:33:31 -07007646 public int getSubIdForPhoneAccountHandle(
Philip P. Moltmann700a9592019-10-03 11:53:50 -07007647 PhoneAccountHandle phoneAccountHandle, String callingPackage, String callingFeatureId) {
Shuo Qian6e6137d2019-10-30 16:33:31 -07007648 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(mApp, getDefaultSubscription(),
Philip P. Moltmann700a9592019-10-03 11:53:50 -07007649 callingPackage, callingFeatureId, "getSubIdForPhoneAccountHandle")) {
Shuo Qian6e6137d2019-10-30 16:33:31 -07007650 throw new SecurityException("Requires READ_PHONE_STATE permission.");
7651 }
7652 final long identity = Binder.clearCallingIdentity();
7653 try {
7654 return PhoneUtils.getSubIdForPhoneAccountHandle(phoneAccountHandle);
7655 } finally {
7656 Binder.restoreCallingIdentity(identity);
7657 }
7658 }
7659
7660 @Override
Tyler Gunnf70ed162019-04-03 15:28:53 -07007661 public @Nullable PhoneAccountHandle getPhoneAccountHandleForSubscriptionId(int subscriptionId) {
Alireza Forouzan4ac4f982021-03-16 22:18:52 -07007662 TelephonyPermissions
Nathan Harold62c68512021-04-06 11:26:02 -07007663 .enforceCallingOrSelfReadPrivilegedPhoneStatePermissionOrCarrierPrivilege(
Alireza Forouzan4ac4f982021-03-16 22:18:52 -07007664 mApp,
7665 subscriptionId,
7666 "getPhoneAccountHandleForSubscriptionId, " + "subscriptionId: " + subscriptionId);
Tyler Gunnf70ed162019-04-03 15:28:53 -07007667 final long identity = Binder.clearCallingIdentity();
7668 try {
7669 Phone phone = getPhone(subscriptionId);
7670 if (phone == null) {
7671 return null;
7672 }
7673 return PhoneUtils.makePstnPhoneAccountHandle(phone);
7674 } finally {
7675 Binder.restoreCallingIdentity(identity);
7676 }
7677 }
7678
Brad Ebinger1f2b5082018-02-08 16:11:32 -08007679 /**
7680 * @return the VoWiFi calling availability.
Nathan Haroldc55097a2015-03-11 18:14:50 -07007681 */
Brad Ebinger1f2b5082018-02-08 16:11:32 -08007682 public boolean isWifiCallingAvailable(int subId) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007683 final long identity = Binder.clearCallingIdentity();
7684 try {
7685 Phone phone = getPhone(subId);
7686 if (phone != null) {
7687 return phone.isWifiCallingEnabled();
7688 } else {
7689 return false;
7690 }
7691 } finally {
7692 Binder.restoreCallingIdentity(identity);
Brad Ebinger1f2b5082018-02-08 16:11:32 -08007693 }
Nathan Haroldc55097a2015-03-11 18:14:50 -07007694 }
7695
Brad Ebinger1f2b5082018-02-08 16:11:32 -08007696 /**
Brad Ebinger1f2b5082018-02-08 16:11:32 -08007697 * @return the VT calling availability.
Etan Cohen3b7a1bc2015-05-28 15:57:13 -07007698 */
Brad Ebinger1f2b5082018-02-08 16:11:32 -08007699 public boolean isVideoTelephonyAvailable(int subId) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007700 final long identity = Binder.clearCallingIdentity();
7701 try {
7702 Phone phone = getPhone(subId);
7703 if (phone != null) {
7704 return phone.isVideoEnabled();
7705 } else {
7706 return false;
7707 }
7708 } finally {
7709 Binder.restoreCallingIdentity(identity);
Brad Ebinger1f2b5082018-02-08 16:11:32 -08007710 }
7711 }
7712
7713 /**
7714 * @return the IMS registration technology for the MMTEL feature. Valid return values are
7715 * defined in {@link ImsRegistrationImplBase}.
7716 */
7717 public @ImsRegistrationImplBase.ImsRegistrationTech int getImsRegTechnologyForMmTel(int subId) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007718 final long identity = Binder.clearCallingIdentity();
7719 try {
7720 Phone phone = getPhone(subId);
7721 if (phone != null) {
7722 return phone.getImsRegistrationTech();
7723 } else {
7724 return ImsRegistrationImplBase.REGISTRATION_TECH_NONE;
7725 }
7726 } finally {
7727 Binder.restoreCallingIdentity(identity);
Brad Ebinger1f2b5082018-02-08 16:11:32 -08007728 }
Etan Cohen3b7a1bc2015-05-28 15:57:13 -07007729 }
7730
Stuart Scott8eef64f2015-04-08 15:13:54 -07007731 @Override
Sarah Chinecc78c42022-03-31 21:16:48 -07007732 public void factoryReset(int subId, String callingPackage) {
paulhu5a773602019-08-23 19:17:33 +08007733 enforceSettingsPermission();
Stuart Scott981d8582015-04-21 14:09:50 -07007734 if (mUserManager.hasUserRestriction(UserManager.DISALLOW_NETWORK_RESET)) {
7735 return;
7736 }
Kai Shif70f46f2021-03-03 13:59:46 -08007737 Phone defaultPhone = getDefaultPhone();
7738 if (defaultPhone != null) {
7739 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
7740 mApp, getDefaultPhone().getSubId(), "factoryReset");
7741 }
Svet Ganovcc087f82015-05-12 20:35:54 -07007742 final long identity = Binder.clearCallingIdentity();
Nazanin Bakhshif782e562018-12-11 15:15:39 -08007743
Svet Ganovcc087f82015-05-12 20:35:54 -07007744 try {
Stuart Scott981d8582015-04-21 14:09:50 -07007745 if (SubscriptionManager.isUsableSubIdValue(subId) && !mUserManager.hasUserRestriction(
7746 UserManager.DISALLOW_CONFIG_MOBILE_NETWORKS)) {
Sooraj Sasindran95c07a92020-07-15 01:35:24 -07007747 setDataEnabledForReason(subId, TelephonyManager.DATA_ENABLED_REASON_USER,
Sarah Chinecc78c42022-03-31 21:16:48 -07007748 getDefaultDataEnabled(), callingPackage);
Svet Ganovcc087f82015-05-12 20:35:54 -07007749 setNetworkSelectionModeAutomatic(subId);
SongFerngWang8c6e82e2021-03-02 22:09:29 +08007750 Phone phone = getPhone(subId);
SongFerngWangfd89b102021-05-27 22:44:54 +08007751 cleanUpAllowedNetworkTypes(phone, subId);
Nazanin Bakhshif782e562018-12-11 15:15:39 -08007752 setDataRoamingEnabled(subId, getDefaultDataRoamingEnabled(subId));
Jordan Liu857e73a2021-03-05 16:24:04 -08007753 getPhone(subId).resetCarrierKeysForImsiEncryption();
Svet Ganovcc087f82015-05-12 20:35:54 -07007754 }
Amit Mahajan7dbbd822019-03-13 17:33:47 -07007755 // There has been issues when Sms raw table somehow stores orphan
7756 // fragments. They lead to garbled message when new fragments come
7757 // in and combined with those stale ones. In case this happens again,
7758 // user can reset all network settings which will clean up this table.
7759 cleanUpSmsRawTable(getDefaultPhone().getContext());
Brad Ebingerbc7dd582019-10-17 17:03:22 -07007760 // Clean up IMS settings as well here.
7761 int slotId = getSlotIndex(subId);
7762 if (slotId > SubscriptionManager.INVALID_SIM_SLOT_INDEX) {
7763 ImsManager.getInstance(mApp, slotId).factoryReset();
7764 }
Naina Nallurid63128d2019-09-17 14:10:30 -07007765
Kai Shif70f46f2021-03-03 13:59:46 -08007766 if (defaultPhone == null) {
7767 return;
7768 }
Naina Nallurid63128d2019-09-17 14:10:30 -07007769 // Erase modem config if erase modem on network setting is enabled.
7770 String configValue = DeviceConfig.getProperty(DeviceConfig.NAMESPACE_TELEPHONY,
7771 RESET_NETWORK_ERASE_MODEM_CONFIG_ENABLED);
7772 if (configValue != null && Boolean.parseBoolean(configValue)) {
Kai Shif70f46f2021-03-03 13:59:46 -08007773 sendEraseModemConfig(defaultPhone);
Naina Nallurid63128d2019-09-17 14:10:30 -07007774 }
Kai Shif70f46f2021-03-03 13:59:46 -08007775
7776 sendEraseDataInSharedPreferences(defaultPhone);
Svet Ganovcc087f82015-05-12 20:35:54 -07007777 } finally {
7778 Binder.restoreCallingIdentity(identity);
Stuart Scott8eef64f2015-04-08 15:13:54 -07007779 }
7780 }
Narayan Kamath1c496c22015-04-16 14:40:19 +01007781
SongFerngWangfd89b102021-05-27 22:44:54 +08007782 @VisibleForTesting
7783 void cleanUpAllowedNetworkTypes(Phone phone, int subId) {
7784 if (phone == null || !SubscriptionManager.isUsableSubscriptionId(subId)) {
7785 return;
7786 }
7787 long defaultNetworkType = RadioAccessFamily.getRafFromNetworkType(
7788 RILConstants.PREFERRED_NETWORK_MODE);
7789 SubscriptionManager.setSubscriptionProperty(subId,
7790 SubscriptionManager.ALLOWED_NETWORK_TYPES,
7791 "user=" + defaultNetworkType);
7792 phone.loadAllowedNetworksFromSubscriptionDatabase();
7793 phone.setAllowedNetworkTypes(TelephonyManager.ALLOWED_NETWORK_TYPES_REASON_USER,
7794 defaultNetworkType, null);
7795 }
7796
Amit Mahajan7dbbd822019-03-13 17:33:47 -07007797 private void cleanUpSmsRawTable(Context context) {
7798 ContentResolver resolver = context.getContentResolver();
7799 Uri uri = Uri.withAppendedPath(Telephony.Sms.CONTENT_URI, "raw/permanentDelete");
7800 resolver.delete(uri, null, null);
7801 }
7802
Narayan Kamath1c496c22015-04-16 14:40:19 +01007803 @Override
chen xu5d3637b2019-01-21 23:31:38 -08007804 public String getSimLocaleForSubscriber(int subId) {
7805 enforceReadPrivilegedPermission("getSimLocaleForSubscriber, subId: " + subId);
7806 final Phone phone = getPhone(subId);
7807 if (phone == null) {
7808 log("getSimLocaleForSubscriber, invalid subId");
chen xu2bb91e42019-01-24 14:35:54 -08007809 return null;
chen xu5d3637b2019-01-21 23:31:38 -08007810 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007811 final long identity = Binder.clearCallingIdentity();
7812 try {
chen xu5d3637b2019-01-21 23:31:38 -08007813 final SubscriptionInfo info = mSubscriptionController.getActiveSubscriptionInfo(subId,
Philip P. Moltmann8d34f0c2020-03-05 16:24:02 -08007814 phone.getContext().getOpPackageName(), phone.getContext().getAttributionTag());
chen xu6291c472019-02-04 12:55:53 -08007815 if (info == null) {
7816 log("getSimLocaleForSubscriber, inactive subId: " + subId);
7817 return null;
7818 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007819 // Try and fetch the locale from the carrier properties or from the SIM language
7820 // preferences (EF-PL and EF-LI)...
7821 final int mcc = info.getMcc();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007822 String simLanguage = null;
chen xu5d3637b2019-01-21 23:31:38 -08007823 final Locale localeFromDefaultSim = phone.getLocaleFromSimAndCarrierPrefs();
7824 if (localeFromDefaultSim != null) {
7825 if (!localeFromDefaultSim.getCountry().isEmpty()) {
7826 if (DBG) log("Using locale from subId: " + subId + " locale: "
7827 + localeFromDefaultSim);
7828 return localeFromDefaultSim.toLanguageTag();
7829 } else {
7830 simLanguage = localeFromDefaultSim.getLanguage();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007831 }
7832 }
Narayan Kamath1c496c22015-04-16 14:40:19 +01007833
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007834 // The SIM language preferences only store a language (e.g. fr = French), not an
7835 // exact locale (e.g. fr_FR = French/France). So, if the locale returned from
7836 // the SIM and carrier preferences does not include a country we add the country
7837 // determined from the SIM MCC to provide an exact locale.
zoey chenc730df82019-12-18 17:07:20 +08007838 final Locale mccLocale = LocaleUtils.getLocaleFromMcc(mApp, mcc, simLanguage);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007839 if (mccLocale != null) {
chen xu5d3637b2019-01-21 23:31:38 -08007840 if (DBG) log("No locale from SIM, using mcc locale:" + mccLocale);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007841 return mccLocale.toLanguageTag();
7842 }
7843
7844 if (DBG) log("No locale found - returning null");
7845 return null;
7846 } finally {
7847 Binder.restoreCallingIdentity(identity);
7848 }
Narayan Kamath1c496c22015-04-16 14:40:19 +01007849 }
7850
7851 private List<SubscriptionInfo> getAllSubscriptionInfoList() {
Philip P. Moltmann700a9592019-10-03 11:53:50 -07007852 return mSubscriptionController.getAllSubInfoList(mApp.getOpPackageName(),
Philip P. Moltmann8d34f0c2020-03-05 16:24:02 -08007853 mApp.getAttributionTag());
Narayan Kamath1c496c22015-04-16 14:40:19 +01007854 }
7855
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007856 /**
7857 * NOTE: this method assumes permission checks are done and caller identity has been cleared.
7858 */
7859 private List<SubscriptionInfo> getActiveSubscriptionInfoListPrivileged() {
Philip P. Moltmann700a9592019-10-03 11:53:50 -07007860 return mSubscriptionController.getActiveSubscriptionInfoList(mApp.getOpPackageName(),
Philip P. Moltmann8d34f0c2020-03-05 16:24:02 -08007861 mApp.getAttributionTag());
Narayan Kamath1c496c22015-04-16 14:40:19 +01007862 }
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -07007863
Gary Jian3aa9a762022-01-24 16:41:19 +08007864 private ActivityStatsTechSpecificInfo[] mLastModemActivitySpecificInfo = null;
7865 private ModemActivityInfo mLastModemActivityInfo = null;
Chenjie Yu1ba97252018-01-11 18:16:20 -08007866
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -07007867 /**
Adam Lesinski903a54c2016-04-11 14:49:52 -07007868 * Responds to the ResultReceiver with the {@link android.telephony.ModemActivityInfo} object
7869 * representing the state of the modem.
7870 *
Chenjie Yu1ba97252018-01-11 18:16:20 -08007871 * NOTE: The underlying implementation clears the modem state, so there should only ever be one
7872 * caller to it. Everyone should call this class to get cumulative data.
Adam Lesinski903a54c2016-04-11 14:49:52 -07007873 * @hide
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -07007874 */
7875 @Override
Adam Lesinski903a54c2016-04-11 14:49:52 -07007876 public void requestModemActivityInfo(ResultReceiver result) {
7877 enforceModifyPermission();
vagdeviaf9a5b92018-08-15 16:01:53 -07007878 WorkSource workSource = getWorkSource(Binder.getCallingUid());
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007879
7880 final long identity = Binder.clearCallingIdentity();
7881 try {
Shuo Qian8f4750a2020-02-20 17:12:10 -08007882 sendRequestAsync(CMD_GET_MODEM_ACTIVITY_INFO, result, null, workSource);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007883 } finally {
7884 Binder.restoreCallingIdentity(identity);
Chenjie Yu1ba97252018-01-11 18:16:20 -08007885 }
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -07007886 }
Jack Yu85bd38a2015-11-09 11:34:32 -08007887
Siddharth Rayb8114062018-06-17 15:02:38 -07007888 // Checks that ModemActivityInfo is valid. Sleep time, Idle time, Rx time and Tx time should be
7889 // less than total activity duration.
7890 private boolean isModemActivityInfoValid(ModemActivityInfo info) {
7891 if (info == null) {
7892 return false;
7893 }
7894 int activityDurationMs =
Hall Liu49656c02020-10-09 19:00:11 -07007895 (int) (info.getTimestampMillis() - mLastModemActivityInfo.getTimestampMillis());
7896 int totalTxTimeMs = Arrays.stream(info.getTransmitTimeMillis()).sum();
7897
Siddharth Rayb8114062018-06-17 15:02:38 -07007898 return (info.isValid()
7899 && (info.getSleepTimeMillis() <= activityDurationMs)
7900 && (info.getIdleTimeMillis() <= activityDurationMs)
Chen Xud78231e2019-09-10 18:49:52 -07007901 && (info.getReceiveTimeMillis() <= activityDurationMs)
Siddharth Rayb8114062018-06-17 15:02:38 -07007902 && (totalTxTimeMs <= activityDurationMs));
7903 }
7904
Gary Jian3aa9a762022-01-24 16:41:19 +08007905 private void updateLastModemActivityInfo(ModemActivityInfo info, int rat, int freq) {
7906 int[] mergedTxTimeMs = new int [ModemActivityInfo.getNumTxPowerLevels()];
7907 int[] txTimeMs = info.getTransmitTimeMillis(rat, freq);
7908 int[] lastModemTxTimeMs = mLastModemActivityInfo.getTransmitTimeMillis(rat, freq);
7909
7910 for (int lvl = 0; lvl < mergedTxTimeMs.length; lvl++) {
7911 mergedTxTimeMs[lvl] = txTimeMs[lvl] + lastModemTxTimeMs[lvl];
7912 }
7913
7914 mLastModemActivityInfo.setTransmitTimeMillis(rat, freq, mergedTxTimeMs);
7915 mLastModemActivityInfo.setReceiveTimeMillis(
7916 rat,
7917 freq,
7918 info.getReceiveTimeMillis(rat, freq)
7919 + mLastModemActivityInfo.getReceiveTimeMillis(rat, freq));
7920 }
7921
7922 private void updateLastModemActivityInfo(ModemActivityInfo info, int rat) {
7923 int[] mergedTxTimeMs = new int [ModemActivityInfo.getNumTxPowerLevels()];
7924 int[] txTimeMs = info.getTransmitTimeMillis(rat);
7925 int[] lastModemTxTimeMs = mLastModemActivityInfo.getTransmitTimeMillis(rat);
7926
7927 for (int lvl = 0; lvl < mergedTxTimeMs.length; lvl++) {
7928 mergedTxTimeMs[lvl] = txTimeMs[lvl] + lastModemTxTimeMs[lvl];
7929 }
7930 mLastModemActivityInfo.setTransmitTimeMillis(rat, mergedTxTimeMs);
7931 mLastModemActivityInfo.setReceiveTimeMillis(
7932 rat,
7933 info.getReceiveTimeMillis(rat) + mLastModemActivityInfo.getReceiveTimeMillis(rat));
7934 }
7935
7936 /**
7937 * Merge this ModemActivityInfo with mLastModemActivitySpecificInfo
7938 * @param info recent ModemActivityInfo
7939 */
7940 private void mergeModemActivityInfo(ModemActivityInfo info) {
7941 List<ActivityStatsTechSpecificInfo> merged = new ArrayList<>();
7942 ActivityStatsTechSpecificInfo mDeltaSpecificInfo;
7943 boolean matched;
7944 for (int i = 0; i < info.getSpecificInfoLength(); i++) {
7945 matched = false;
7946 int rat = info.getSpecificInfoRat(i);
7947 int freq = info.getSpecificInfoFrequencyRange(i);
7948 //Check each ActivityStatsTechSpecificInfo in this ModemActivityInfo for new rat returns
7949 //Add a new ActivityStatsTechSpecificInfo if is a new rat, and merge with the original
7950 //if it already exists
7951 for (int j = 0; j < mLastModemActivitySpecificInfo.length; j++) {
7952 if (rat == mLastModemActivityInfo.getSpecificInfoRat(j) && !matched) {
7953 //Merged based on frequency range (MMWAVE vs SUB6) for 5G
7954 if (rat == AccessNetworkConstants.AccessNetworkType.NGRAN) {
7955 if (freq == mLastModemActivityInfo.getSpecificInfoFrequencyRange(j)) {
7956 updateLastModemActivityInfo(info, rat, freq);
7957 matched = true;
7958 }
7959 } else {
7960 updateLastModemActivityInfo(info, rat);
7961 matched = true;
7962 }
7963 }
7964 }
7965
7966 if (!matched) {
7967 mDeltaSpecificInfo =
7968 new ActivityStatsTechSpecificInfo(
7969 rat,
7970 freq,
7971 info.getTransmitTimeMillis(rat, freq),
7972 (int) info.getReceiveTimeMillis(rat, freq));
7973 merged.addAll(Arrays.asList(mDeltaSpecificInfo));
7974 }
7975 }
7976 merged.addAll(Arrays.asList(mLastModemActivitySpecificInfo));
7977 mLastModemActivitySpecificInfo =
7978 new ActivityStatsTechSpecificInfo[merged.size()];
7979 merged.toArray(mLastModemActivitySpecificInfo);
7980
7981 mLastModemActivityInfo.setTimestamp(info.getTimestampMillis());
7982 mLastModemActivityInfo.setSleepTimeMillis(
7983 info.getSleepTimeMillis()
7984 + mLastModemActivityInfo.getSleepTimeMillis());
7985 mLastModemActivityInfo.setIdleTimeMillis(
7986 info.getIdleTimeMillis()
7987 + mLastModemActivityInfo.getIdleTimeMillis());
7988 }
7989
Jack Yu85bd38a2015-11-09 11:34:32 -08007990 /**
Jack Yu85bd38a2015-11-09 11:34:32 -08007991 * Returns the service state information on specified subscription.
7992 */
7993 @Override
Sooraj Sasindran2250dc02021-11-10 16:42:01 -08007994 public ServiceState getServiceStateForSubscriber(int subId,
7995 boolean renounceFineLocationAccess, boolean renounceCoarseLocationAccess,
7996 String callingPackage, String callingFeatureId) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08007997 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
Philip P. Moltmann700a9592019-10-03 11:53:50 -07007998 mApp, subId, callingPackage, callingFeatureId, "getServiceStateForSubscriber")) {
Jack Yu85bd38a2015-11-09 11:34:32 -08007999 return null;
8000 }
8001
Sooraj Sasindran2250dc02021-11-10 16:42:01 -08008002 boolean hasFinePermission = false;
8003 boolean hasCoarsePermission = false;
8004 if (!renounceFineLocationAccess) {
8005 LocationAccessPolicy.LocationPermissionResult fineLocationResult =
8006 LocationAccessPolicy.checkLocationPermission(mApp,
8007 new LocationAccessPolicy.LocationPermissionQuery.Builder()
8008 .setCallingPackage(callingPackage)
8009 .setCallingFeatureId(callingFeatureId)
8010 .setCallingPid(Binder.getCallingPid())
8011 .setCallingUid(Binder.getCallingUid())
8012 .setMethod("getServiceStateForSubscriber")
8013 .setLogAsInfo(true)
8014 .setMinSdkVersionForFine(Build.VERSION_CODES.Q)
8015 .setMinSdkVersionForCoarse(Build.VERSION_CODES.Q)
8016 .setMinSdkVersionForEnforcement(Build.VERSION_CODES.Q)
8017 .build());
8018 hasFinePermission =
8019 fineLocationResult == LocationAccessPolicy.LocationPermissionResult.ALLOWED;
8020 }
Hall Liuf19c44f2018-11-27 14:38:17 -08008021
Sooraj Sasindran2250dc02021-11-10 16:42:01 -08008022 if (!renounceCoarseLocationAccess) {
8023 LocationAccessPolicy.LocationPermissionResult coarseLocationResult =
8024 LocationAccessPolicy.checkLocationPermission(mApp,
8025 new LocationAccessPolicy.LocationPermissionQuery.Builder()
8026 .setCallingPackage(callingPackage)
8027 .setCallingFeatureId(callingFeatureId)
8028 .setCallingPid(Binder.getCallingPid())
8029 .setCallingUid(Binder.getCallingUid())
8030 .setMethod("getServiceStateForSubscriber")
8031 .setLogAsInfo(true)
8032 .setMinSdkVersionForCoarse(Build.VERSION_CODES.Q)
8033 .setMinSdkVersionForFine(Integer.MAX_VALUE)
8034 .setMinSdkVersionForEnforcement(Build.VERSION_CODES.Q)
8035 .build());
8036 hasCoarsePermission =
8037 coarseLocationResult == LocationAccessPolicy.LocationPermissionResult.ALLOWED;
8038 }
Hall Liuf19c44f2018-11-27 14:38:17 -08008039
Jack Yu479f40e2020-10-27 21:29:25 -07008040 final Phone phone = getPhone(subId);
8041 if (phone == null) {
8042 return null;
8043 }
8044
Jordan Liu0f2bc442020-11-18 16:47:37 -08008045 final long identity = Binder.clearCallingIdentity();
8046
Jack Yu479f40e2020-10-27 21:29:25 -07008047 boolean isCallingPackageDataService = phone.getDataServicePackages()
8048 .contains(callingPackage);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008049 try {
Jordan Liuc437b192020-08-17 10:59:12 -07008050 // isActiveSubId requires READ_PHONE_STATE, which we already check for above
8051 if (!mSubscriptionController.isActiveSubId(subId, callingPackage, callingFeatureId)) {
8052 Rlog.d(LOG_TAG,
8053 "getServiceStateForSubscriber returning null for inactive subId=" + subId);
8054 return null;
8055 }
8056
Hall Liuf19c44f2018-11-27 14:38:17 -08008057 ServiceState ss = phone.getServiceState();
8058
8059 // Scrub out the location info in ServiceState depending on what level of access
8060 // the caller has.
Jack Yu479f40e2020-10-27 21:29:25 -07008061 if (hasFinePermission || isCallingPackageDataService) return ss;
Malcolm Chen5052de62019-12-30 13:56:38 -08008062 if (hasCoarsePermission) return ss.createLocationInfoSanitizedCopy(false);
8063 return ss.createLocationInfoSanitizedCopy(true);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008064 } finally {
8065 Binder.restoreCallingIdentity(identity);
8066 }
Jack Yu85bd38a2015-11-09 11:34:32 -08008067 }
Nancy Chen31f9ba12016-01-06 11:42:12 -08008068
8069 /**
8070 * Returns the URI for the per-account voicemail ringtone set in Phone settings.
8071 *
8072 * @param accountHandle The handle for the {@link PhoneAccount} for which to retrieve the
8073 * voicemail ringtone.
8074 * @return The URI for the ringtone to play when receiving a voicemail from a specific
8075 * PhoneAccount.
8076 */
8077 @Override
8078 public Uri getVoicemailRingtoneUri(PhoneAccountHandle accountHandle) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008079 final long identity = Binder.clearCallingIdentity();
8080 try {
8081 Phone phone = PhoneUtils.getPhoneForPhoneAccountHandle(accountHandle);
8082 if (phone == null) {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08008083 phone = getDefaultPhone();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008084 }
Nancy Chen31f9ba12016-01-06 11:42:12 -08008085
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008086 return VoicemailNotificationSettingsUtil.getRingtoneUri(phone.getContext());
8087 } finally {
8088 Binder.restoreCallingIdentity(identity);
8089 }
Nancy Chen31f9ba12016-01-06 11:42:12 -08008090 }
8091
8092 /**
Ta-wei Yenc33877d2017-01-23 18:11:21 -08008093 * Sets the per-account voicemail ringtone.
8094 *
8095 * <p>Requires that the calling app is the default dialer, or has carrier privileges, or
8096 * has permission {@link android.Manifest.permission#MODIFY_PHONE_STATE MODIFY_PHONE_STATE}.
8097 *
8098 * @param phoneAccountHandle The handle for the {@link PhoneAccount} for which to set the
8099 * voicemail ringtone.
8100 * @param uri The URI for the ringtone to play when receiving a voicemail from a specific
8101 * PhoneAccount.
8102 */
8103 @Override
8104 public void setVoicemailRingtoneUri(String callingPackage,
8105 PhoneAccountHandle phoneAccountHandle, Uri uri) {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08008106 final Phone defaultPhone = getDefaultPhone();
Ta-wei Yenc33877d2017-01-23 18:11:21 -08008107 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
Tyler Gunn5ddfdc92019-10-31 13:08:23 -07008108 TelecomManager tm = defaultPhone.getContext().getSystemService(TelecomManager.class);
8109 if (!TextUtils.equals(callingPackage, tm.getDefaultDialerPackage())) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08008110 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
8111 mApp, PhoneUtils.getSubIdForPhoneAccountHandle(phoneAccountHandle),
8112 "setVoicemailRingtoneUri");
Ta-wei Yenc33877d2017-01-23 18:11:21 -08008113 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008114
8115 final long identity = Binder.clearCallingIdentity();
8116 try {
8117 Phone phone = PhoneUtils.getPhoneForPhoneAccountHandle(phoneAccountHandle);
8118 if (phone == null) {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08008119 phone = defaultPhone;
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008120 }
8121 VoicemailNotificationSettingsUtil.setRingtoneUri(phone.getContext(), uri);
8122 } finally {
8123 Binder.restoreCallingIdentity(identity);
Ta-wei Yenc33877d2017-01-23 18:11:21 -08008124 }
Ta-wei Yenc33877d2017-01-23 18:11:21 -08008125 }
8126
8127 /**
Nancy Chen31f9ba12016-01-06 11:42:12 -08008128 * Returns whether vibration is set for voicemail notification in Phone settings.
8129 *
8130 * @param accountHandle The handle for the {@link PhoneAccount} for which to retrieve the
8131 * voicemail vibration setting.
8132 * @return {@code true} if the vibration is set for this PhoneAccount, {@code false} otherwise.
8133 */
8134 @Override
8135 public boolean isVoicemailVibrationEnabled(PhoneAccountHandle accountHandle) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008136 final long identity = Binder.clearCallingIdentity();
8137 try {
8138 Phone phone = PhoneUtils.getPhoneForPhoneAccountHandle(accountHandle);
8139 if (phone == null) {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08008140 phone = getDefaultPhone();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008141 }
Nancy Chen31f9ba12016-01-06 11:42:12 -08008142
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008143 return VoicemailNotificationSettingsUtil.isVibrationEnabled(phone.getContext());
8144 } finally {
8145 Binder.restoreCallingIdentity(identity);
8146 }
Nancy Chen31f9ba12016-01-06 11:42:12 -08008147 }
8148
Youhan Wange64578a2016-05-02 15:32:42 -07008149 /**
Ta-wei Yenc33877d2017-01-23 18:11:21 -08008150 * Sets the per-account voicemail vibration.
8151 *
8152 * <p>Requires that the calling app is the default dialer, or has carrier privileges, or
8153 * has permission {@link android.Manifest.permission#MODIFY_PHONE_STATE MODIFY_PHONE_STATE}.
8154 *
8155 * @param phoneAccountHandle The handle for the {@link PhoneAccount} for which to set the
8156 * voicemail vibration setting.
8157 * @param enabled Whether to enable or disable vibration for voicemail notifications from a
8158 * specific PhoneAccount.
8159 */
8160 @Override
8161 public void setVoicemailVibrationEnabled(String callingPackage,
8162 PhoneAccountHandle phoneAccountHandle, boolean enabled) {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08008163 final Phone defaultPhone = getDefaultPhone();
Ta-wei Yenc33877d2017-01-23 18:11:21 -08008164 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
Tyler Gunn5ddfdc92019-10-31 13:08:23 -07008165 TelecomManager tm = defaultPhone.getContext().getSystemService(TelecomManager.class);
8166 if (!TextUtils.equals(callingPackage, tm.getDefaultDialerPackage())) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08008167 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
8168 mApp, PhoneUtils.getSubIdForPhoneAccountHandle(phoneAccountHandle),
8169 "setVoicemailVibrationEnabled");
Ta-wei Yenc33877d2017-01-23 18:11:21 -08008170 }
8171
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008172 final long identity = Binder.clearCallingIdentity();
8173 try {
8174 Phone phone = PhoneUtils.getPhoneForPhoneAccountHandle(phoneAccountHandle);
8175 if (phone == null) {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08008176 phone = defaultPhone;
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008177 }
8178 VoicemailNotificationSettingsUtil.setVibrationEnabled(phone.getContext(), enabled);
8179 } finally {
8180 Binder.restoreCallingIdentity(identity);
Ta-wei Yenc33877d2017-01-23 18:11:21 -08008181 }
Ta-wei Yenc33877d2017-01-23 18:11:21 -08008182 }
8183
8184 /**
Youhan Wange64578a2016-05-02 15:32:42 -07008185 * Make sure either called from same process as self (phone) or IPC caller has read privilege.
8186 *
8187 * @throws SecurityException if the caller does not have the required permission
8188 */
Brad Ebinger35c841c2018-10-01 10:40:55 -07008189 private void enforceReadPrivilegedPermission(String message) {
Youhan Wange64578a2016-05-02 15:32:42 -07008190 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.READ_PRIVILEGED_PHONE_STATE,
Brad Ebinger35c841c2018-10-01 10:40:55 -07008191 message);
Youhan Wange64578a2016-05-02 15:32:42 -07008192 }
8193
8194 /**
Ta-wei Yen30a69c82016-12-27 14:52:32 -08008195 * Make sure either called from same process as self (phone) or IPC caller has send SMS
8196 * permission.
8197 *
8198 * @throws SecurityException if the caller does not have the required permission
8199 */
8200 private void enforceSendSmsPermission() {
8201 mApp.enforceCallingOrSelfPermission(permission.SEND_SMS, null);
8202 }
8203
8204 /**
Ta-wei Yen527a9c02017-01-06 15:29:25 -08008205 * Make sure called from the package in charge of visual voicemail.
Ta-wei Yen30a69c82016-12-27 14:52:32 -08008206 *
Ta-wei Yen527a9c02017-01-06 15:29:25 -08008207 * @throws SecurityException if the caller is not the visual voicemail package.
Ta-wei Yen30a69c82016-12-27 14:52:32 -08008208 */
Ta-wei Yen527a9c02017-01-06 15:29:25 -08008209 private void enforceVisualVoicemailPackage(String callingPackage, int subId) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008210 final long identity = Binder.clearCallingIdentity();
8211 try {
8212 ComponentName componentName =
Nazanin Bakhshif782e562018-12-11 15:15:39 -08008213 RemoteVvmTaskManager.getRemotePackage(mApp, subId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008214 if (componentName == null) {
8215 throw new SecurityException(
8216 "Caller not current active visual voicemail package[null]");
8217 }
8218 String vvmPackage = componentName.getPackageName();
8219 if (!callingPackage.equals(vvmPackage)) {
Hui Wang7f657552022-08-16 16:58:25 +00008220 throw new SecurityException("Caller not current active visual voicemail package");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008221 }
8222 } finally {
8223 Binder.restoreCallingIdentity(identity);
Ta-wei Yen30a69c82016-12-27 14:52:32 -08008224 }
8225 }
8226
8227 /**
Youhan Wange64578a2016-05-02 15:32:42 -07008228 * Return the application ID for the app type.
8229 *
8230 * @param subId the subscription ID that this request applies to.
8231 * @param appType the uicc app type.
8232 * @return Application ID for specificied app type, or null if no uicc.
8233 */
8234 @Override
8235 public String getAidForAppType(int subId, int appType) {
Brad Ebinger35c841c2018-10-01 10:40:55 -07008236 enforceReadPrivilegedPermission("getAidForAppType");
Youhan Wange64578a2016-05-02 15:32:42 -07008237 Phone phone = getPhone(subId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008238
8239 final long identity = Binder.clearCallingIdentity();
Youhan Wange64578a2016-05-02 15:32:42 -07008240 try {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008241 if (phone == null) {
8242 return null;
8243 }
8244 String aid = null;
8245 try {
Muralidhar Reddy472c2ae2021-09-29 19:38:40 +00008246 aid = UiccController.getInstance().getUiccPort(phone.getPhoneId())
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008247 .getApplicationByType(appType).getAid();
8248 } catch (Exception e) {
8249 Log.e(LOG_TAG, "Not getting aid. Exception ex=" + e);
8250 }
8251 return aid;
8252 } finally {
8253 Binder.restoreCallingIdentity(identity);
Youhan Wange64578a2016-05-02 15:32:42 -07008254 }
Youhan Wange64578a2016-05-02 15:32:42 -07008255 }
8256
Youhan Wang4001d252016-05-11 10:29:41 -07008257 /**
8258 * Return the Electronic Serial Number.
8259 *
8260 * @param subId the subscription ID that this request applies to.
8261 * @return ESN or null if error.
8262 */
8263 @Override
8264 public String getEsn(int subId) {
Brad Ebinger35c841c2018-10-01 10:40:55 -07008265 enforceReadPrivilegedPermission("getEsn");
Youhan Wang4001d252016-05-11 10:29:41 -07008266 Phone phone = getPhone(subId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008267
8268 final long identity = Binder.clearCallingIdentity();
Youhan Wang4001d252016-05-11 10:29:41 -07008269 try {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008270 if (phone == null) {
8271 return null;
8272 }
8273 String esn = null;
8274 try {
8275 esn = phone.getEsn();
8276 } catch (Exception e) {
8277 Log.e(LOG_TAG, "Not getting ESN. Exception ex=" + e);
8278 }
8279 return esn;
8280 } finally {
8281 Binder.restoreCallingIdentity(identity);
Youhan Wang4001d252016-05-11 10:29:41 -07008282 }
Youhan Wang4001d252016-05-11 10:29:41 -07008283 }
8284
Sanket Padawe99ef1e32016-05-18 16:12:33 -07008285 /**
Youhan Wang66ad5d72016-07-18 17:56:58 -07008286 * Return the Preferred Roaming List Version.
8287 *
8288 * @param subId the subscription ID that this request applies to.
8289 * @return PRLVersion or null if error.
8290 */
8291 @Override
8292 public String getCdmaPrlVersion(int subId) {
Brad Ebinger35c841c2018-10-01 10:40:55 -07008293 enforceReadPrivilegedPermission("getCdmaPrlVersion");
Youhan Wang66ad5d72016-07-18 17:56:58 -07008294 Phone phone = getPhone(subId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008295
8296 final long identity = Binder.clearCallingIdentity();
Youhan Wang66ad5d72016-07-18 17:56:58 -07008297 try {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008298 if (phone == null) {
8299 return null;
8300 }
8301 String cdmaPrlVersion = null;
8302 try {
8303 cdmaPrlVersion = phone.getCdmaPrlVersion();
8304 } catch (Exception e) {
8305 Log.e(LOG_TAG, "Not getting PRLVersion", e);
8306 }
8307 return cdmaPrlVersion;
8308 } finally {
8309 Binder.restoreCallingIdentity(identity);
Youhan Wang66ad5d72016-07-18 17:56:58 -07008310 }
Youhan Wang66ad5d72016-07-18 17:56:58 -07008311 }
8312
8313 /**
Sanket Padawe99ef1e32016-05-18 16:12:33 -07008314 * Get snapshot of Telephony histograms
8315 * @return List of Telephony histograms
8316 * @hide
8317 */
8318 @Override
8319 public List<TelephonyHistogram> getTelephonyHistograms() {
Jeff Davidson7e17e312018-02-13 18:17:36 -08008320 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
8321 mApp, getDefaultSubscription(), "getTelephonyHistograms");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008322
8323 final long identity = Binder.clearCallingIdentity();
8324 try {
8325 return RIL.getTelephonyRILTimingHistograms();
8326 } finally {
8327 Binder.restoreCallingIdentity(identity);
8328 }
Sanket Padawe99ef1e32016-05-18 16:12:33 -07008329 }
Meng Wang1a7c35a2016-05-05 20:56:15 -07008330
8331 /**
8332 * {@hide}
Michele Berionne482f8202018-11-27 18:57:59 -08008333 * Set the allowed carrier list and the excluded carrier list, indicating the priority between
8334 * the two lists.
Meng Wang1a7c35a2016-05-05 20:56:15 -07008335 * Require system privileges. In the future we may add this to carrier APIs.
8336 *
Michele Berionne482f8202018-11-27 18:57:59 -08008337 * @return Integer with the result of the operation, as defined in {@link TelephonyManager}.
Meng Wang1a7c35a2016-05-05 20:56:15 -07008338 */
8339 @Override
Michele Berionne482f8202018-11-27 18:57:59 -08008340 @TelephonyManager.SetCarrierRestrictionResult
8341 public int setAllowedCarriers(CarrierRestrictionRules carrierRestrictionRules) {
Meng Wang1a7c35a2016-05-05 20:56:15 -07008342 enforceModifyPermission();
vagdeviaf9a5b92018-08-15 16:01:53 -07008343 WorkSource workSource = getWorkSource(Binder.getCallingUid());
Sanket Padawe13bac7b2017-03-20 15:04:47 -07008344
Michele Berionne482f8202018-11-27 18:57:59 -08008345 if (carrierRestrictionRules == null) {
8346 throw new NullPointerException("carrier restriction cannot be null");
Meng Wang9b7c4e92017-02-17 11:41:27 -08008347 }
Sanket Padawe13bac7b2017-03-20 15:04:47 -07008348
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008349 final long identity = Binder.clearCallingIdentity();
8350 try {
Michele Berionne482f8202018-11-27 18:57:59 -08008351 return (int) sendRequest(CMD_SET_ALLOWED_CARRIERS, carrierRestrictionRules,
vagdeviaf9a5b92018-08-15 16:01:53 -07008352 workSource);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008353 } finally {
8354 Binder.restoreCallingIdentity(identity);
8355 }
Meng Wang1a7c35a2016-05-05 20:56:15 -07008356 }
8357
8358 /**
8359 * {@hide}
Michele Berionne482f8202018-11-27 18:57:59 -08008360 * Get the allowed carrier list and the excluded carrier list, including the priority between
8361 * the two lists.
Meng Wang1a7c35a2016-05-05 20:56:15 -07008362 * Require system privileges. In the future we may add this to carrier APIs.
8363 *
Michele Berionne482f8202018-11-27 18:57:59 -08008364 * @return {@link android.telephony.CarrierRestrictionRules}
Meng Wang1a7c35a2016-05-05 20:56:15 -07008365 */
8366 @Override
Michele Berionne482f8202018-11-27 18:57:59 -08008367 public CarrierRestrictionRules getAllowedCarriers() {
Brad Ebinger35c841c2018-10-01 10:40:55 -07008368 enforceReadPrivilegedPermission("getAllowedCarriers");
vagdeviaf9a5b92018-08-15 16:01:53 -07008369 WorkSource workSource = getWorkSource(Binder.getCallingUid());
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008370
8371 final long identity = Binder.clearCallingIdentity();
8372 try {
Michele Berionne482f8202018-11-27 18:57:59 -08008373 Object response = sendRequest(CMD_GET_ALLOWED_CARRIERS, null, workSource);
8374 if (response instanceof CarrierRestrictionRules) {
8375 return (CarrierRestrictionRules) response;
8376 }
8377 // Response is an Exception of some kind,
8378 // which is signalled to the user as a NULL retval
8379 return null;
8380 } catch (Exception e) {
8381 Log.e(LOG_TAG, "getAllowedCarriers. Exception ex=" + e);
8382 return null;
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008383 } finally {
8384 Binder.restoreCallingIdentity(identity);
8385 }
Meng Wang1a7c35a2016-05-05 20:56:15 -07008386 }
8387
fionaxu59545b42016-05-25 15:53:37 -07008388 /**
fionaxu59545b42016-05-25 15:53:37 -07008389 * Action set from carrier signalling broadcast receivers to enable/disable radio
8390 * @param subId the subscription ID that this action applies to.
8391 * @param enabled control enable or disable radio.
8392 * {@hide}
8393 */
8394 @Override
8395 public void carrierActionSetRadioEnabled(int subId, boolean enabled) {
8396 enforceModifyPermission();
8397 final Phone phone = getPhone(subId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008398
8399 final long identity = Binder.clearCallingIdentity();
fionaxu59545b42016-05-25 15:53:37 -07008400 if (phone == null) {
8401 loge("carrierAction: SetRadioEnabled fails with invalid sibId: " + subId);
8402 return;
8403 }
8404 try {
8405 phone.carrierActionSetRadioEnabled(enabled);
8406 } catch (Exception e) {
8407 Log.e(LOG_TAG, "carrierAction: SetRadioEnabled fails. Exception ex=" + e);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008408 } finally {
8409 Binder.restoreCallingIdentity(identity);
fionaxu59545b42016-05-25 15:53:37 -07008410 }
8411 }
8412
Ta-wei Yenc236d6b2016-06-21 13:33:12 -07008413 /**
Sooraj Sasindrandaf060f2021-06-15 14:52:55 -07008414 * Enable or disable Voice over NR (VoNR)
8415 * @param subId the subscription ID that this action applies to.
8416 * @param enabled enable or disable VoNR.
8417 * @return operation result.
8418 */
8419 @Override
8420 public int setVoNrEnabled(int subId, boolean enabled) {
8421 enforceModifyPermission();
8422 final Phone phone = getPhone(subId);
8423
8424 final long identity = Binder.clearCallingIdentity();
8425 if (phone == null) {
8426 loge("setVoNrEnabled fails with no phone object for subId: " + subId);
8427 return TelephonyManager.ENABLE_VONR_RADIO_NOT_AVAILABLE;
8428 }
8429
8430 WorkSource workSource = getWorkSource(Binder.getCallingUid());
8431 try {
8432 int result = (int) sendRequest(CMD_ENABLE_VONR, enabled, subId,
8433 workSource);
8434 if (DBG) log("setVoNrEnabled result: " + result);
Gary Jian8dd305f2021-10-14 16:31:35 +08008435
8436 if (result == TelephonyManager.ENABLE_VONR_SUCCESS) {
8437 if (DBG) {
8438 log("Set VoNR settings in siminfo db; subId=" + subId + ", value:" + enabled);
8439 }
8440 SubscriptionManager.setSubscriptionProperty(
8441 subId, SubscriptionManager.NR_ADVANCED_CALLING_ENABLED,
8442 (enabled ? "1" : "0"));
8443 }
8444
Sooraj Sasindrandaf060f2021-06-15 14:52:55 -07008445 return result;
8446 } finally {
8447 Binder.restoreCallingIdentity(identity);
8448 }
8449 }
8450
8451 /**
8452 * Is voice over NR enabled
8453 * @return true if VoNR is enabled else false
8454 */
8455 @Override
8456 public boolean isVoNrEnabled(int subId) {
8457 enforceReadPrivilegedPermission("isVoNrEnabled");
8458 WorkSource workSource = getWorkSource(Binder.getCallingUid());
8459 final long identity = Binder.clearCallingIdentity();
8460 try {
8461 boolean isEnabled = (boolean) sendRequest(CMD_IS_VONR_ENABLED,
8462 null, subId, workSource);
8463 if (DBG) log("isVoNrEnabled: " + isEnabled);
8464 return isEnabled;
8465 } finally {
8466 Binder.restoreCallingIdentity(identity);
8467 }
8468 }
8469
8470 /**
fionaxu8da9cb12017-05-23 15:02:46 -07008471 * Action set from carrier signalling broadcast receivers to start/stop reporting the default
8472 * network status based on which carrier apps could apply actions accordingly,
8473 * enable/disable default url handler for example.
8474 *
8475 * @param subId the subscription ID that this action applies to.
8476 * @param report control start/stop reporting the default network status.
8477 * {@hide}
8478 */
8479 @Override
8480 public void carrierActionReportDefaultNetworkStatus(int subId, boolean report) {
8481 enforceModifyPermission();
8482 final Phone phone = getPhone(subId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008483
8484 final long identity = Binder.clearCallingIdentity();
fionaxu8da9cb12017-05-23 15:02:46 -07008485 if (phone == null) {
8486 loge("carrierAction: ReportDefaultNetworkStatus fails with invalid sibId: " + subId);
8487 return;
8488 }
8489 try {
8490 phone.carrierActionReportDefaultNetworkStatus(report);
8491 } catch (Exception e) {
8492 Log.e(LOG_TAG, "carrierAction: ReportDefaultNetworkStatus fails. Exception ex=" + e);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008493 } finally {
8494 Binder.restoreCallingIdentity(identity);
fionaxu8da9cb12017-05-23 15:02:46 -07008495 }
8496 }
8497
8498 /**
fionaxud9622282017-07-17 17:51:30 -07008499 * Action set from carrier signalling broadcast receivers to reset all carrier actions
8500 * @param subId the subscription ID that this action applies to.
8501 * {@hide}
8502 */
8503 @Override
8504 public void carrierActionResetAll(int subId) {
8505 enforceModifyPermission();
8506 final Phone phone = getPhone(subId);
8507 if (phone == null) {
8508 loge("carrierAction: ResetAll fails with invalid sibId: " + subId);
8509 return;
8510 }
8511 try {
8512 phone.carrierActionResetAll();
8513 } catch (Exception e) {
8514 Log.e(LOG_TAG, "carrierAction: ResetAll fails. Exception ex=" + e);
8515 }
8516 }
8517
8518 /**
Ta-wei Yenc236d6b2016-06-21 13:33:12 -07008519 * Called when "adb shell dumpsys phone" is invoked. Dump is also automatically invoked when a
8520 * bug report is being generated.
8521 */
8522 @Override
Ta-wei Yen99282e02016-06-21 18:19:35 -07008523 protected void dump(FileDescriptor fd, PrintWriter writer, String[] args) {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08008524 if (mApp.checkCallingOrSelfPermission(android.Manifest.permission.DUMP)
8525 != PackageManager.PERMISSION_GRANTED) {
dcashman22b950d2016-06-27 11:39:02 -07008526 writer.println("Permission Denial: can't dump Phone from pid="
8527 + Binder.getCallingPid()
8528 + ", uid=" + Binder.getCallingUid()
8529 + "without permission "
8530 + android.Manifest.permission.DUMP);
8531 return;
8532 }
Nazanin Bakhshif782e562018-12-11 15:15:39 -08008533 DumpsysHandler.dump(mApp, fd, writer, args);
Ta-wei Yenc236d6b2016-06-21 13:33:12 -07008534 }
Jack Yueb89b242016-06-22 13:27:47 -07008535
Brad Ebingerdac2f002018-04-03 15:17:52 -07008536 @Override
Hall Liua1548bd2019-12-24 14:14:12 -08008537 public int handleShellCommand(@NonNull ParcelFileDescriptor in,
8538 @NonNull ParcelFileDescriptor out, @NonNull ParcelFileDescriptor err,
8539 @NonNull String[] args) {
8540 return new TelephonyShellCommand(this, getDefaultPhone().getContext()).exec(
8541 this, in.getFileDescriptor(), out.getFileDescriptor(),
8542 err.getFileDescriptor(), args);
Brad Ebingerdac2f002018-04-03 15:17:52 -07008543 }
8544
Jack Yueb89b242016-06-22 13:27:47 -07008545 /**
Sooraj Sasindranaf1b5132020-05-05 21:06:20 +00008546 * Policy control of data connection with reason {@@TelephonyManager.DataEnabledReason}
Greg Kaiser17f41752020-05-05 16:47:47 +00008547 * @param subId Subscription index
Sarah Chinecc78c42022-03-31 21:16:48 -07008548 * @param reason The reason the data enable change is taking place.
Sooraj Sasindranaf1b5132020-05-05 21:06:20 +00008549 * @param enabled True if enabling the data, otherwise disabling.
Sarah Chinecc78c42022-03-31 21:16:48 -07008550 * @param callingPackage The package that changed the data enabled state.
Sooraj Sasindranaf1b5132020-05-05 21:06:20 +00008551 * @hide
Jack Yu75ab2952016-07-08 14:29:33 -07008552 */
8553 @Override
Sooraj Sasindran95c07a92020-07-15 01:35:24 -07008554 public void setDataEnabledForReason(int subId, @TelephonyManager.DataEnabledReason int reason,
Sarah Chinecc78c42022-03-31 21:16:48 -07008555 boolean enabled, String callingPackage) {
Sooraj Sasindranaf1b5132020-05-05 21:06:20 +00008556 if (reason == TelephonyManager.DATA_ENABLED_REASON_USER
8557 || reason == TelephonyManager.DATA_ENABLED_REASON_CARRIER) {
8558 try {
8559 TelephonyPermissions.enforceCallingOrSelfCarrierPrivilege(
Sooraj Sasindran95c07a92020-07-15 01:35:24 -07008560 mApp, subId, "setDataEnabledForReason");
Sooraj Sasindranaf1b5132020-05-05 21:06:20 +00008561 } catch (SecurityException se) {
8562 enforceModifyPermission();
8563 }
8564 } else {
8565 enforceModifyPermission();
8566 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008567
8568 final long identity = Binder.clearCallingIdentity();
8569 try {
8570 Phone phone = getPhone(subId);
8571 if (phone != null) {
Sooraj Sasindranaf1b5132020-05-05 21:06:20 +00008572 if (reason == TelephonyManager.DATA_ENABLED_REASON_CARRIER) {
8573 phone.carrierActionSetMeteredApnsEnabled(enabled);
8574 } else {
Jack Yu7968c6d2022-07-31 00:43:21 -07008575 phone.getDataSettingsManager().setDataEnabled(
8576 reason, enabled, callingPackage);
Sooraj Sasindranaf1b5132020-05-05 21:06:20 +00008577 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008578 }
8579 } finally {
8580 Binder.restoreCallingIdentity(identity);
Jack Yu75ab2952016-07-08 14:29:33 -07008581 }
8582 }
Sooraj Sasindran9a909312016-07-18 11:57:25 -07008583
8584 /**
8585 * Get Client request stats
8586 * @return List of Client Request Stats
8587 * @hide
8588 */
8589 @Override
Philip P. Moltmann700a9592019-10-03 11:53:50 -07008590 public List<ClientRequestStats> getClientRequestStats(String callingPackage,
8591 String callingFeatureId, int subId) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08008592 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
Philip P. Moltmann700a9592019-10-03 11:53:50 -07008593 mApp, subId, callingPackage, callingFeatureId, "getClientRequestStats")) {
Sooraj Sasindran9a909312016-07-18 11:57:25 -07008594 return null;
8595 }
Sooraj Sasindran9a909312016-07-18 11:57:25 -07008596 Phone phone = getPhone(subId);
Sooraj Sasindran9a909312016-07-18 11:57:25 -07008597
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008598 final long identity = Binder.clearCallingIdentity();
8599 try {
8600 if (phone != null) {
8601 return phone.getClientRequestStats();
8602 }
8603
8604 return null;
8605 } finally {
8606 Binder.restoreCallingIdentity(identity);
8607 }
Sooraj Sasindran9a909312016-07-18 11:57:25 -07008608 }
8609
Narayan Kamathf04b5a12018-01-09 11:47:15 +00008610 private WorkSource getWorkSource(int uid) {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08008611 String packageName = mApp.getPackageManager().getNameForUid(uid);
Hunter Knepshieldd03383b2022-03-29 22:47:54 +00008612 if (uid == Process.ROOT_UID && packageName == null) {
8613 // Downstream WorkSource attribution inside the RIL requires both a UID and package name
8614 // to be set for wakelock tracking, otherwise RIL requests fail with a runtime
8615 // exception. ROOT_UID seems not to have a valid package name returned by
8616 // PackageManager, so just fake it here to avoid issues when running telephony shell
8617 // commands that plumb through the RIL as root, like so:
8618 // $ adb root
8619 // $ adb shell cmd phone ...
8620 packageName = "root";
8621 }
Narayan Kamathf04b5a12018-01-09 11:47:15 +00008622 return new WorkSource(uid, packageName);
Sooraj Sasindran9a909312016-07-18 11:57:25 -07008623 }
Jack Yueb4124c2017-02-16 15:32:43 -08008624
8625 /**
Grace Chen70990072017-03-24 17:21:30 -07008626 * Set SIM card power state.
Jack Yueb4124c2017-02-16 15:32:43 -08008627 *
Sanket Padawe13bac7b2017-03-20 15:04:47 -07008628 * @param slotIndex SIM slot id.
Grace Chen70990072017-03-24 17:21:30 -07008629 * @param state State of SIM (power down, power up, pass through)
8630 * - {@link android.telephony.TelephonyManager#CARD_POWER_DOWN}
8631 * - {@link android.telephony.TelephonyManager#CARD_POWER_UP}
8632 * - {@link android.telephony.TelephonyManager#CARD_POWER_UP_PASS_THROUGH}
Jack Yueb4124c2017-02-16 15:32:43 -08008633 *
8634 **/
8635 @Override
Grace Chen70990072017-03-24 17:21:30 -07008636 public void setSimPowerStateForSlot(int slotIndex, int state) {
Jack Yueb4124c2017-02-16 15:32:43 -08008637 enforceModifyPermission();
Sanket Padawe13bac7b2017-03-20 15:04:47 -07008638 Phone phone = PhoneFactory.getPhone(slotIndex);
8639
vagdeviaf9a5b92018-08-15 16:01:53 -07008640 WorkSource workSource = getWorkSource(Binder.getCallingUid());
8641
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008642 final long identity = Binder.clearCallingIdentity();
8643 try {
8644 if (phone != null) {
Jordan Liu109698e2020-11-24 14:50:34 -08008645 phone.setSimPowerState(state, null, workSource);
8646 }
8647 } finally {
8648 Binder.restoreCallingIdentity(identity);
8649 }
8650 }
8651
8652 /**
8653 * Set SIM card power state.
8654 *
8655 * @param slotIndex SIM slot id.
8656 * @param state State of SIM (power down, power up, pass through)
8657 * @param callback callback to trigger after success or failure
8658 * - {@link android.telephony.TelephonyManager#CARD_POWER_DOWN}
8659 * - {@link android.telephony.TelephonyManager#CARD_POWER_UP}
8660 * - {@link android.telephony.TelephonyManager#CARD_POWER_UP_PASS_THROUGH}
8661 *
8662 **/
8663 @Override
8664 public void setSimPowerStateForSlotWithCallback(int slotIndex, int state,
8665 IIntegerConsumer callback) {
8666 enforceModifyPermission();
8667 Phone phone = PhoneFactory.getPhone(slotIndex);
8668
8669 WorkSource workSource = getWorkSource(Binder.getCallingUid());
8670
8671 final long identity = Binder.clearCallingIdentity();
8672 try {
8673 if (phone != null) {
8674 Pair<Integer, IIntegerConsumer> arguments = Pair.create(state, callback);
8675 sendRequestAsync(CMD_SET_SIM_POWER, arguments, phone, workSource);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008676 }
8677 } finally {
8678 Binder.restoreCallingIdentity(identity);
Jack Yueb4124c2017-02-16 15:32:43 -08008679 }
8680 }
Shuo Qiandd210312017-04-12 22:11:33 +00008681
Tyler Gunn65d45c22017-06-05 11:22:26 -07008682 private boolean isUssdApiAllowed(int subId) {
8683 CarrierConfigManager configManager =
Nazanin Bakhshif782e562018-12-11 15:15:39 -08008684 (CarrierConfigManager) mApp.getSystemService(Context.CARRIER_CONFIG_SERVICE);
Tyler Gunn65d45c22017-06-05 11:22:26 -07008685 if (configManager == null) {
8686 return false;
8687 }
8688 PersistableBundle pb = configManager.getConfigForSubId(subId);
8689 if (pb == null) {
8690 return false;
8691 }
8692 return pb.getBoolean(
8693 CarrierConfigManager.KEY_ALLOW_USSD_REQUESTS_VIA_TELEPHONY_MANAGER_BOOL);
8694 }
8695
Shuo Qiandd210312017-04-12 22:11:33 +00008696 /**
8697 * Check if phone is in emergency callback mode
8698 * @return true if phone is in emergency callback mode
8699 * @param subId sub id
8700 */
goneil9c5f4872017-12-05 14:07:56 -08008701 @Override
Shuo Qiandd210312017-04-12 22:11:33 +00008702 public boolean getEmergencyCallbackMode(int subId) {
Brad Ebinger35c841c2018-10-01 10:40:55 -07008703 enforceReadPrivilegedPermission("getEmergencyCallbackMode");
Shuo Qiandd210312017-04-12 22:11:33 +00008704 final Phone phone = getPhone(subId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008705
8706 final long identity = Binder.clearCallingIdentity();
8707 try {
8708 if (phone != null) {
8709 return phone.isInEcm();
8710 } else {
8711 return false;
8712 }
8713 } finally {
8714 Binder.restoreCallingIdentity(identity);
Shuo Qiandd210312017-04-12 22:11:33 +00008715 }
8716 }
Nathan Harold46b42aa2017-03-10 19:38:22 -08008717
8718 /**
8719 * Get the current signal strength information for the given subscription.
8720 * Because this information is not updated when the device is in a low power state
8721 * it should not be relied-upon to be current.
8722 * @param subId Subscription index
8723 * @return the most recent cached signal strength info from the modem
8724 */
8725 @Override
8726 public SignalStrength getSignalStrength(int subId) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008727 final long identity = Binder.clearCallingIdentity();
8728 try {
8729 Phone p = getPhone(subId);
8730 if (p == null) {
8731 return null;
8732 }
Nathan Harold46b42aa2017-03-10 19:38:22 -08008733
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008734 return p.getSignalStrength();
8735 } finally {
8736 Binder.restoreCallingIdentity(identity);
8737 }
Nathan Harold46b42aa2017-03-10 19:38:22 -08008738 }
Holly Jiuyu Sun01c47ad2018-01-24 17:56:33 +00008739
Pengquan Meng77b7f132018-08-22 14:49:57 -07008740 /**
Chen Xuf792fd62018-10-17 17:54:36 +00008741 * Get the current modem radio state for the given slot.
8742 * @param slotIndex slot index.
8743 * @param callingPackage the name of the package making the call.
Philip P. Moltmann700a9592019-10-03 11:53:50 -07008744 * @param callingFeatureId The feature in the package.
Chen Xuf792fd62018-10-17 17:54:36 +00008745 * @return the current radio power state from the modem
8746 */
8747 @Override
Philip P. Moltmann700a9592019-10-03 11:53:50 -07008748 public int getRadioPowerState(int slotIndex, String callingPackage, String callingFeatureId) {
Chen Xuf792fd62018-10-17 17:54:36 +00008749 Phone phone = PhoneFactory.getPhone(slotIndex);
8750 if (phone != null) {
Philip P. Moltmann700a9592019-10-03 11:53:50 -07008751 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(mApp, phone.getSubId(),
8752 callingPackage, callingFeatureId, "getRadioPowerState")) {
Chen Xuf792fd62018-10-17 17:54:36 +00008753 return TelephonyManager.RADIO_POWER_UNAVAILABLE;
8754 }
8755
8756 final long identity = Binder.clearCallingIdentity();
8757 try {
8758 return phone.getRadioPowerState();
8759 } finally {
8760 Binder.restoreCallingIdentity(identity);
8761 }
8762 }
8763 return TelephonyManager.RADIO_POWER_UNAVAILABLE;
8764 }
8765
8766 /**
Pengquan Meng77b7f132018-08-22 14:49:57 -07008767 * Checks if data roaming is enabled on the subscription with id {@code subId}.
8768 *
8769 * <p>Requires one of the following permissions:
8770 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE},
Sooraj Sasindran5d051bf2021-11-05 13:14:15 -07008771 * {@link android.Manifest.permission#READ_BASIC_PHONE_STATE},
Pengquan Meng77b7f132018-08-22 14:49:57 -07008772 * {@link android.Manifest.permission#READ_PHONE_STATE} or that the calling app has carrier
8773 * privileges.
8774 *
8775 * @param subId subscription id
8776 * @return {@code true} if data roaming is enabled on this subscription, otherwise return
8777 * {@code false}.
8778 */
8779 @Override
8780 public boolean isDataRoamingEnabled(int subId) {
Sooraj Sasindran5d051bf2021-11-05 13:14:15 -07008781 String functionName = "isDataRoamingEnabled";
Shuo Qian093013d2020-08-13 15:42:55 -07008782 try {
Sooraj Sasindran5d051bf2021-11-05 13:14:15 -07008783 try {
8784 mApp.enforceCallingOrSelfPermission(
8785 android.Manifest.permission.ACCESS_NETWORK_STATE,
8786 functionName);
Sooraj Sasindran12545cc2022-08-22 14:30:25 -07008787 } catch (SecurityException e) {
Sooraj Sasindran5d051bf2021-11-05 13:14:15 -07008788 mApp.enforceCallingOrSelfPermission(
8789 permission.READ_BASIC_PHONE_STATE, functionName);
8790 }
Sooraj Sasindran12545cc2022-08-22 14:30:25 -07008791 } catch (SecurityException e) {
Nathan Harold62c68512021-04-06 11:26:02 -07008792 TelephonyPermissions.enforceCallingOrSelfReadPhoneStatePermissionOrCarrierPrivilege(
Sooraj Sasindran5d051bf2021-11-05 13:14:15 -07008793 mApp, subId, functionName);
Shuo Qian093013d2020-08-13 15:42:55 -07008794 }
Pengquan Meng44e66f12019-04-01 10:48:20 -07008795
Pengquan Menga1bb6272018-09-06 09:59:22 -07008796 boolean isEnabled = false;
8797 final long identity = Binder.clearCallingIdentity();
Pengquan Meng77b7f132018-08-22 14:49:57 -07008798 try {
Pengquan Menga1bb6272018-09-06 09:59:22 -07008799 Phone phone = getPhone(subId);
8800 isEnabled = phone != null ? phone.getDataRoamingEnabled() : false;
Pengquan Menga1bb6272018-09-06 09:59:22 -07008801 } finally {
8802 Binder.restoreCallingIdentity(identity);
Pengquan Meng77b7f132018-08-22 14:49:57 -07008803 }
Pengquan Menga1bb6272018-09-06 09:59:22 -07008804 return isEnabled;
Pengquan Meng77b7f132018-08-22 14:49:57 -07008805 }
8806
8807
8808 /**
8809 * Enables/Disables the data roaming on the subscription with id {@code subId}.
8810 *
8811 * <p> Requires permission:
8812 * {@link android.Manifest.permission#MODIFY_PHONE_STATE} or that the calling app has carrier
8813 * privileges.
8814 *
8815 * @param subId subscription id
8816 * @param isEnabled {@code true} means enable, {@code false} means disable.
8817 */
8818 @Override
8819 public void setDataRoamingEnabled(int subId, boolean isEnabled) {
Pengquan Meng44e66f12019-04-01 10:48:20 -07008820 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
8821 mApp, subId, "setDataRoamingEnabled");
8822
Pengquan Menga1bb6272018-09-06 09:59:22 -07008823 final long identity = Binder.clearCallingIdentity();
8824 try {
Pengquan Menga1bb6272018-09-06 09:59:22 -07008825 Phone phone = getPhone(subId);
8826 if (phone != null) {
8827 phone.setDataRoamingEnabled(isEnabled);
8828 }
8829 } finally {
8830 Binder.restoreCallingIdentity(identity);
Pengquan Meng77b7f132018-08-22 14:49:57 -07008831 }
8832 }
8833
Holly Jiuyu Sun01c47ad2018-01-24 17:56:33 +00008834 @Override
Pengquan Meng6884a2c2018-10-03 12:19:13 -07008835 public boolean isManualNetworkSelectionAllowed(int subId) {
tom hsuc91afc72020-01-06 23:46:07 +08008836 TelephonyPermissions
Nathan Harold62c68512021-04-06 11:26:02 -07008837 .enforceCallingOrSelfReadPrivilegedPhoneStatePermissionOrCarrierPrivilege(
Pengquan Meng44e66f12019-04-01 10:48:20 -07008838 mApp, subId, "isManualNetworkSelectionAllowed");
8839
Pengquan Meng6884a2c2018-10-03 12:19:13 -07008840 boolean isAllowed = true;
8841 final long identity = Binder.clearCallingIdentity();
8842 try {
Pengquan Meng6884a2c2018-10-03 12:19:13 -07008843 Phone phone = getPhone(subId);
8844 if (phone != null) {
8845 isAllowed = phone.isCspPlmnEnabled();
8846 }
8847 } finally {
8848 Binder.restoreCallingIdentity(identity);
8849 }
8850 return isAllowed;
8851 }
8852
Muralidhar Reddyeb809e32021-11-19 03:07:54 +00008853 private boolean haveCarrierPrivilegeAccess(UiccPort port, String callingPackage) {
8854 UiccProfile profile = port.getUiccProfile();
Hunter Knepshieldcad7f0b2021-12-13 15:08:35 -08008855 if (profile == null) {
Muralidhar Reddyeb809e32021-11-19 03:07:54 +00008856 return false;
Muralidhar Reddy472c2ae2021-09-29 19:38:40 +00008857 }
Hunter Knepshieldcad7f0b2021-12-13 15:08:35 -08008858 Phone phone = PhoneFactory.getPhone(profile.getPhoneId());
8859 if (phone == null) {
8860 return false;
8861 }
8862 CarrierPrivilegesTracker cpt = phone.getCarrierPrivilegesTracker();
8863 return cpt != null && cpt.getCarrierPrivilegeStatusForPackage(callingPackage)
8864 == TelephonyManager.CARRIER_PRIVILEGE_STATUS_HAS_ACCESS;
Muralidhar Reddy472c2ae2021-09-29 19:38:40 +00008865 }
8866
Pengquan Meng6884a2c2018-10-03 12:19:13 -07008867 @Override
Jordan Liu75f43ea2019-01-17 16:56:37 -08008868 public List<UiccCardInfo> getUiccCardsInfo(String callingPackage) {
sandeepjsa208e3b2021-11-17 04:05:58 +00008869 // Verify that the callingPackage belongs to the calling UID
Jordan Liu4cda4552020-03-23 11:55:07 -07008870 mApp.getSystemService(AppOpsManager.class)
8871 .checkPackage(Binder.getCallingUid(), callingPackage);
8872
Jordan Liu1e142fc2019-04-22 15:10:43 -07008873 boolean hasReadPermission = false;
sandeepjsb6c87872021-09-27 15:34:44 +00008874 boolean isIccIdAccessRestricted = false;
Jordan Liuc65bc952019-02-12 17:54:02 -08008875 try {
8876 enforceReadPrivilegedPermission("getUiccCardsInfo");
Jordan Liu1e142fc2019-04-22 15:10:43 -07008877 hasReadPermission = true;
Jordan Liuc65bc952019-02-12 17:54:02 -08008878 } catch (SecurityException e) {
8879 // even without READ_PRIVILEGED_PHONE_STATE, we allow the call to continue if the caller
8880 // has carrier privileges on an active UICC
Rambo Wange7209ce2022-02-23 13:41:02 -08008881 if (checkCarrierPrivilegesForPackageAnyPhoneWithPermission(callingPackage)
Jordan Liuc65bc952019-02-12 17:54:02 -08008882 != TelephonyManager.CARRIER_PRIVILEGE_STATUS_HAS_ACCESS) {
Jordan Liu1e142fc2019-04-22 15:10:43 -07008883 throw new SecurityException("Caller does not have permission.");
Jordan Liuc65bc952019-02-12 17:54:02 -08008884 }
Jordan Liu75f43ea2019-01-17 16:56:37 -08008885 }
sandeepjsb6c87872021-09-27 15:34:44 +00008886 // checking compatibility, if calling app's target SDK is T and beyond.
8887 if (CompatChanges.isChangeEnabled(GET_API_SIGNATURES_FROM_UICC_PORT_INFO,
8888 Binder.getCallingUid())) {
8889 isIccIdAccessRestricted = true;
8890 }
Jordan Liu5aa07002018-12-18 15:44:48 -08008891 final long identity = Binder.clearCallingIdentity();
8892 try {
Jordan Liu75f43ea2019-01-17 16:56:37 -08008893 UiccController uiccController = UiccController.getInstance();
8894 ArrayList<UiccCardInfo> cardInfos = uiccController.getAllUiccCardInfos();
Jordan Liu1e142fc2019-04-22 15:10:43 -07008895 if (hasReadPermission) {
8896 return cardInfos;
Jordan Liu75f43ea2019-01-17 16:56:37 -08008897 }
Jordan Liu1e142fc2019-04-22 15:10:43 -07008898
8899 // Remove private info if the caller doesn't have access
8900 ArrayList<UiccCardInfo> filteredInfos = new ArrayList<>();
8901 for (UiccCardInfo cardInfo : cardInfos) {
sandeepjsb6c87872021-09-27 15:34:44 +00008902 //setting the value after compatibility check
8903 cardInfo.setIccIdAccessRestricted(isIccIdAccessRestricted);
Jordan Liu1e142fc2019-04-22 15:10:43 -07008904 // For an inactive eUICC, the UiccCard will be null even though the UiccCardInfo
8905 // is available
sandeepjsb6c87872021-09-27 15:34:44 +00008906 UiccCard card = uiccController.getUiccCardForSlot(cardInfo.getPhysicalSlotIndex());
Muralidhar Reddyeb809e32021-11-19 03:07:54 +00008907 if (card == null) {
8908 // assume no access if the card is unavailable
sandeepjsb6c87872021-09-27 15:34:44 +00008909 filteredInfos.add(getUiccCardInfoUnPrivileged(cardInfo));
Jordan Liu1e142fc2019-04-22 15:10:43 -07008910 continue;
8911 }
Muralidhar Reddyeb809e32021-11-19 03:07:54 +00008912 Collection<UiccPortInfo> portInfos = cardInfo.getPorts();
8913 if (portInfos.isEmpty()) {
sandeepjsb6c87872021-09-27 15:34:44 +00008914 filteredInfos.add(getUiccCardInfoUnPrivileged(cardInfo));
Muralidhar Reddyeb809e32021-11-19 03:07:54 +00008915 continue;
Jordan Liu1e142fc2019-04-22 15:10:43 -07008916 }
Muralidhar Reddyeb809e32021-11-19 03:07:54 +00008917 List<UiccPortInfo> uiccPortInfos = new ArrayList<>();
8918 for (UiccPortInfo portInfo : portInfos) {
8919 UiccPort port = uiccController.getUiccPortForSlot(
8920 cardInfo.getPhysicalSlotIndex(), portInfo.getPortIndex());
8921 if (port == null) {
8922 // assume no access if port is null
8923 uiccPortInfos.add(getUiccPortInfoUnPrivileged(portInfo));
8924 continue;
8925 }
8926 if (haveCarrierPrivilegeAccess(port, callingPackage)) {
8927 uiccPortInfos.add(portInfo);
8928 } else {
8929 uiccPortInfos.add(getUiccPortInfoUnPrivileged(portInfo));
8930 }
8931 }
8932 filteredInfos.add(new UiccCardInfo(
8933 cardInfo.isEuicc(),
8934 cardInfo.getCardId(),
8935 null,
8936 cardInfo.getPhysicalSlotIndex(),
8937 cardInfo.isRemovable(),
8938 cardInfo.isMultipleEnabledProfilesSupported(),
8939 uiccPortInfos));
Jordan Liu1e142fc2019-04-22 15:10:43 -07008940 }
8941 return filteredInfos;
Jordan Liu5aa07002018-12-18 15:44:48 -08008942 } finally {
8943 Binder.restoreCallingIdentity(identity);
8944 }
8945 }
8946
sandeepjsb6c87872021-09-27 15:34:44 +00008947 /**
8948 * Returns a copy of the UiccCardinfo with the EID and ICCID set to null. These values are
8949 * generally private and require carrier privileges to view.
8950 *
8951 * @hide
8952 */
8953 @NonNull
8954 public UiccCardInfo getUiccCardInfoUnPrivileged(UiccCardInfo cardInfo) {
8955 List<UiccPortInfo> portinfo = new ArrayList<>();
8956 for (UiccPortInfo portinfos : cardInfo.getPorts()) {
8957 portinfo.add(getUiccPortInfoUnPrivileged(portinfos));
8958 }
8959 return new UiccCardInfo(
8960 cardInfo.isEuicc(),
8961 cardInfo.getCardId(),
8962 null,
8963 cardInfo.getPhysicalSlotIndex(),
8964 cardInfo.isRemovable(),
8965 cardInfo.isMultipleEnabledProfilesSupported(),
8966 portinfo
8967 );
8968 }
Holly Jiuyu Sun01c47ad2018-01-24 17:56:33 +00008969
sandeepjsb6c87872021-09-27 15:34:44 +00008970 /**
8971 * @hide
8972 * @return a copy of the UiccPortInfo with ICCID set to {@link UiccPortInfo#ICCID_REDACTED}.
8973 * These values are generally private and require carrier privileges to view.
8974 */
8975 @NonNull
8976 public UiccPortInfo getUiccPortInfoUnPrivileged(UiccPortInfo portInfo) {
8977 return new UiccPortInfo(
8978 UiccPortInfo.ICCID_REDACTED,
8979 portInfo.getPortIndex(),
8980 portInfo.getLogicalSlotIndex(),
8981 portInfo.isActive()
8982 );
8983 }
8984 @Override
8985 public UiccSlotInfo[] getUiccSlotsInfo(String callingPackage) {
sandeepjsa208e3b2021-11-17 04:05:58 +00008986 // Verify that the callingPackage belongs to the calling UID
sandeepjsb6c87872021-09-27 15:34:44 +00008987 mApp.getSystemService(AppOpsManager.class)
8988 .checkPackage(Binder.getCallingUid(), callingPackage);
8989
sandeepjsb6c87872021-09-27 15:34:44 +00008990 boolean isLogicalSlotAccessRestricted = false;
sandeepjsb6c87872021-09-27 15:34:44 +00008991
Aman Guptaf3c90b32022-03-17 04:54:16 +00008992 // This will make sure caller has the READ_PRIVILEGED_PHONE_STATE. Do not remove this as
8993 // we are reading iccId which is PII data.
8994 enforceReadPrivilegedPermission("getUiccSlotsInfo");
sandeepjsb6c87872021-09-27 15:34:44 +00008995
8996 // checking compatibility, if calling app's target SDK is T and beyond.
8997 if (CompatChanges.isChangeEnabled(GET_API_SIGNATURES_FROM_UICC_PORT_INFO,
8998 Binder.getCallingUid())) {
8999 isLogicalSlotAccessRestricted = true;
9000 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08009001 final long identity = Binder.clearCallingIdentity();
9002 try {
9003 UiccSlot[] slots = UiccController.getInstance().getUiccSlots();
Muralidhar Reddyd196bbf2022-01-17 17:56:30 +00009004 if (slots == null || slots.length == 0) {
9005 Rlog.i(LOG_TAG, "slots is null or empty.");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08009006 return null;
9007 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08009008 UiccSlotInfo[] infos = new UiccSlotInfo[slots.length];
9009 for (int i = 0; i < slots.length; i++) {
9010 UiccSlot slot = slots[i];
9011 if (slot == null) {
9012 continue;
9013 }
9014
Jordan Liu7be7e652019-05-06 18:55:02 +00009015 String cardId;
Malcolm Chenaa4a8532018-02-28 15:00:40 -08009016 UiccCard card = slot.getUiccCard();
9017 if (card != null) {
9018 cardId = card.getCardId();
Jordan Liu7be7e652019-05-06 18:55:02 +00009019 } else {
Jordan Liu01bd00d2019-09-12 16:19:43 -07009020 cardId = slot.getEid();
9021 if (TextUtils.isEmpty(cardId)) {
Aman Guptaf3c90b32022-03-17 04:54:16 +00009022 // If cardId is null, use iccId of default port as cardId.
9023 cardId = slot.getIccId(TelephonyManager.DEFAULT_PORT_INDEX);
Jordan Liu01bd00d2019-09-12 16:19:43 -07009024 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08009025 }
9026
Jordan Liu857451f2019-05-09 16:35:35 -07009027 if (cardId != null) {
9028 // if cardId is an ICCID, strip off trailing Fs before exposing to user
9029 // if cardId is an EID, it's all digits so this is fine
9030 cardId = IccUtils.stripTrailingFs(cardId);
9031 }
9032
Malcolm Chenaa4a8532018-02-28 15:00:40 -08009033 int cardState = 0;
9034 switch (slot.getCardState()) {
9035 case CARDSTATE_ABSENT:
9036 cardState = UiccSlotInfo.CARD_STATE_INFO_ABSENT;
9037 break;
9038 case CARDSTATE_PRESENT:
9039 cardState = UiccSlotInfo.CARD_STATE_INFO_PRESENT;
9040 break;
9041 case CARDSTATE_ERROR:
9042 cardState = UiccSlotInfo.CARD_STATE_INFO_ERROR;
9043 break;
9044 case CARDSTATE_RESTRICTED:
9045 cardState = UiccSlotInfo.CARD_STATE_INFO_RESTRICTED;
9046 break;
9047 default:
9048 break;
9049
9050 }
Muralidhar Reddyeb809e32021-11-19 03:07:54 +00009051 List<UiccPortInfo> portInfos = new ArrayList<>();
9052 int[] portIndexes = slot.getPortList();
9053 for (int portIdx : portIndexes) {
9054 String iccId = IccUtils.stripTrailingFs(getIccId(slot, portIdx,
Aman Guptaf3c90b32022-03-17 04:54:16 +00009055 callingPackage, /* hasReadPermission= */ true));
Muralidhar Reddyfbcff0c2022-01-19 13:07:57 +00009056 portInfos.add(new UiccPortInfo(iccId, portIdx,
9057 slot.getPhoneIdFromPortIndex(portIdx), slot.isPortActive(portIdx)));
Muralidhar Reddyeb809e32021-11-19 03:07:54 +00009058 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08009059 infos[i] = new UiccSlotInfo(
Malcolm Chenaa4a8532018-02-28 15:00:40 -08009060 slot.isEuicc(),
9061 cardId,
9062 cardState,
Jordan Liua2619582019-02-14 12:56:40 -08009063 slot.isExtendedApduSupported(),
Muralidhar Reddyeb809e32021-11-19 03:07:54 +00009064 slot.isRemovable(), portInfos);
sandeepjsb6c87872021-09-27 15:34:44 +00009065 //setting the value after compatibility check
9066 infos[i].setLogicalSlotAccessRestricted(isLogicalSlotAccessRestricted);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08009067 }
9068 return infos;
9069 } finally {
9070 Binder.restoreCallingIdentity(identity);
Holly Jiuyu Sun1d957c52018-04-04 13:52:42 -07009071 }
Holly Jiuyu Sun01c47ad2018-01-24 17:56:33 +00009072 }
9073
Muralidhar Reddyeb809e32021-11-19 03:07:54 +00009074 /* Returns null if doesn't have read permission or carrier privilege access. */
9075 private String getIccId(UiccSlot slot, int portIndex, String callingPackage,
9076 boolean hasReadPermission) {
9077 String iccId = slot.getIccId(portIndex);
9078 if (hasReadPermission) { // if has read permission
9079 return iccId;
9080 } else {
9081 if (slot.getUiccCard() != null && slot.getUiccCard().getUiccPort(portIndex) != null) {
9082 UiccPort port = slot.getUiccCard().getUiccPort(portIndex);
9083 // if no read permission, checking carrier privilege access
9084 if (haveCarrierPrivilegeAccess(port, callingPackage)) {
9085 return iccId;
9086 }
9087 }
9088 }
9089 // No read permission or carrier privilege access.
9090 return UiccPortInfo.ICCID_REDACTED;
9091 }
9092
Holly Jiuyu Sun01c47ad2018-01-24 17:56:33 +00009093 @Override
sandeepjsb6c87872021-09-27 15:34:44 +00009094 @Deprecated
Holly Jiuyu Sun01c47ad2018-01-24 17:56:33 +00009095 public boolean switchSlots(int[] physicalSlots) {
9096 enforceModifyPermission();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08009097
9098 final long identity = Binder.clearCallingIdentity();
9099 try {
Muralidhar Reddyeb809e32021-11-19 03:07:54 +00009100 List<UiccSlotMapping> slotMappings = new ArrayList<>();
9101 for (int i = 0; i < physicalSlots.length; i++) {
9102 // Deprecated API, hence MEP is not supported. Adding default portIndex 0.
9103 slotMappings.add(new UiccSlotMapping(TelephonyManager.DEFAULT_PORT_INDEX,
9104 physicalSlots[i], i));
9105 }
9106 return (Boolean) sendRequest(CMD_SWITCH_SLOTS, slotMappings);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08009107 } finally {
9108 Binder.restoreCallingIdentity(identity);
9109 }
Holly Jiuyu Sun01c47ad2018-01-24 17:56:33 +00009110 }
Jack Yu4c988042018-02-27 15:30:01 -08009111
9112 @Override
sandeepjsb6c87872021-09-27 15:34:44 +00009113 @RequiresPermission(android.Manifest.permission.MODIFY_PHONE_STATE)
9114 public boolean setSimSlotMapping(@NonNull List<UiccSlotMapping> slotMapping) {
9115 enforceModifyPermission();
9116
9117 final long identity = Binder.clearCallingIdentity();
9118 try {
Muralidhar Reddyeb809e32021-11-19 03:07:54 +00009119 return (Boolean) sendRequest(CMD_SWITCH_SLOTS, slotMapping);
sandeepjsb6c87872021-09-27 15:34:44 +00009120 } finally {
9121 Binder.restoreCallingIdentity(identity);
9122 }
9123 }
9124
9125 @Override
Jordan Liu7de49fa2018-12-06 14:48:49 -08009126 public int getCardIdForDefaultEuicc(int subId, String callingPackage) {
Jordan Liu7de49fa2018-12-06 14:48:49 -08009127 final long identity = Binder.clearCallingIdentity();
9128 try {
9129 return UiccController.getInstance().getCardIdForDefaultEuicc();
9130 } finally {
9131 Binder.restoreCallingIdentity(identity);
9132 }
9133 }
9134
Pengquan Meng85728fb2018-03-12 16:31:21 -07009135 /**
goneil47ffb6e2018-04-06 15:40:58 -07009136 * A test API to reload the UICC profile.
9137 *
9138 * <p>Requires that the calling app has permission
9139 * {@link android.Manifest.permission#MODIFY_PHONE_STATE MODIFY_PHONE_STATE}.
9140 * @hide
9141 */
9142 @Override
9143 public void refreshUiccProfile(int subId) {
9144 enforceModifyPermission();
9145
9146 final long identity = Binder.clearCallingIdentity();
9147 try {
9148 Phone phone = getPhone(subId);
9149 if (phone == null) {
9150 return;
9151 }
Muralidhar Reddy472c2ae2021-09-29 19:38:40 +00009152 UiccPort uiccPort = phone.getUiccPort();
9153 if (uiccPort == null) {
goneil47ffb6e2018-04-06 15:40:58 -07009154 return;
9155 }
Muralidhar Reddy472c2ae2021-09-29 19:38:40 +00009156 UiccProfile uiccProfile = uiccPort.getUiccProfile();
goneil47ffb6e2018-04-06 15:40:58 -07009157 if (uiccProfile == null) {
9158 return;
9159 }
9160 uiccProfile.refresh();
9161 } finally {
9162 Binder.restoreCallingIdentity(identity);
9163 }
9164 }
9165
9166 /**
Pengquan Meng85728fb2018-03-12 16:31:21 -07009167 * Returns false if the mobile data is disabled by default, otherwise return true.
9168 */
9169 private boolean getDefaultDataEnabled() {
Inseob Kim14bb3d02018-12-13 17:11:34 +09009170 return TelephonyProperties.mobile_data().orElse(true);
Pengquan Meng85728fb2018-03-12 16:31:21 -07009171 }
9172
9173 /**
9174 * Returns true if the data roaming is enabled by default, i.e the system property
9175 * of {@link #DEFAULT_DATA_ROAMING_PROPERTY_NAME} is true or the config of
9176 * {@link CarrierConfigManager#KEY_CARRIER_DEFAULT_DATA_ROAMING_ENABLED_BOOL} is true.
9177 */
9178 private boolean getDefaultDataRoamingEnabled(int subId) {
9179 final CarrierConfigManager configMgr = (CarrierConfigManager)
Nazanin Bakhshif782e562018-12-11 15:15:39 -08009180 mApp.getSystemService(Context.CARRIER_CONFIG_SERVICE);
Shuo Qian1d84a0e2020-07-15 12:36:44 -07009181 boolean isDataRoamingEnabled = TelephonyProperties.data_roaming().orElse(false);
Pengquan Meng85728fb2018-03-12 16:31:21 -07009182 isDataRoamingEnabled |= configMgr.getConfigForSubId(subId).getBoolean(
9183 CarrierConfigManager.KEY_CARRIER_DEFAULT_DATA_ROAMING_ENABLED_BOOL);
9184 return isDataRoamingEnabled;
9185 }
9186
9187 /**
9188 * Returns the default network type for the given {@code subId}, if the default network type is
9189 * not set, return {@link Phone#PREFERRED_NT_MODE}.
9190 */
9191 private int getDefaultNetworkType(int subId) {
Inseob Kim14bb3d02018-12-13 17:11:34 +09009192 List<Integer> list = TelephonyProperties.default_network();
9193 int phoneId = mSubscriptionController.getPhoneId(subId);
9194 if (phoneId >= 0 && phoneId < list.size() && list.get(phoneId) != null) {
9195 return list.get(phoneId);
9196 }
9197 return Phone.PREFERRED_NT_MODE;
Pengquan Meng85728fb2018-03-12 16:31:21 -07009198 }
fionaxua13278b2018-03-21 00:08:13 -07009199
9200 @Override
9201 public void setCarrierTestOverride(int subId, String mccmnc, String imsi, String iccid, String
chen xueaba88a2019-03-15 13:15:10 -07009202 gid1, String gid2, String plmn, String spn, String carrierPrivilegeRules, String apn) {
fionaxua13278b2018-03-21 00:08:13 -07009203 enforceModifyPermission();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08009204
9205 final long identity = Binder.clearCallingIdentity();
9206 try {
9207 final Phone phone = getPhone(subId);
9208 if (phone == null) {
9209 loge("setCarrierTestOverride fails with invalid subId: " + subId);
9210 return;
9211 }
Rambo Wang9c9ffdd2022-01-13 21:51:44 -08009212 CarrierPrivilegesTracker cpt = phone.getCarrierPrivilegesTracker();
9213 if (cpt != null) {
9214 cpt.setTestOverrideCarrierPrivilegeRules(carrierPrivilegeRules);
9215 }
9216 // TODO(b/211796398): remove the legacy logic below once CPT migration is done.
chen xueaba88a2019-03-15 13:15:10 -07009217 phone.setCarrierTestOverride(mccmnc, imsi, iccid, gid1, gid2, plmn, spn,
9218 carrierPrivilegeRules, apn);
Jeff Davidson8ab02b22020-03-28 12:24:40 -07009219 if (carrierPrivilegeRules == null) {
9220 mCarrierPrivilegeTestOverrideSubIds.remove(subId);
9221 } else {
9222 mCarrierPrivilegeTestOverrideSubIds.add(subId);
9223 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08009224 } finally {
9225 Binder.restoreCallingIdentity(identity);
fionaxua13278b2018-03-21 00:08:13 -07009226 }
fionaxua13278b2018-03-21 00:08:13 -07009227 }
9228
9229 @Override
9230 public int getCarrierIdListVersion(int subId) {
Brad Ebinger35c841c2018-10-01 10:40:55 -07009231 enforceReadPrivilegedPermission("getCarrierIdListVersion");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08009232
9233 final long identity = Binder.clearCallingIdentity();
9234 try {
9235 final Phone phone = getPhone(subId);
9236 if (phone == null) {
9237 loge("getCarrierIdListVersion fails with invalid subId: " + subId);
9238 return TelephonyManager.UNKNOWN_CARRIER_ID_LIST_VERSION;
9239 }
9240 return phone.getCarrierIdListVersion();
9241 } finally {
9242 Binder.restoreCallingIdentity(identity);
fionaxua13278b2018-03-21 00:08:13 -07009243 }
fionaxua13278b2018-03-21 00:08:13 -07009244 }
Malcolm Chen2c63d402018-08-14 16:00:53 -07009245
9246 @Override
Philip P. Moltmann700a9592019-10-03 11:53:50 -07009247 public int getNumberOfModemsWithSimultaneousDataConnections(int subId, String callingPackage,
9248 String callingFeatureId) {
Malcolm Chen2c63d402018-08-14 16:00:53 -07009249 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
Philip P. Moltmann700a9592019-10-03 11:53:50 -07009250 mApp, subId, callingPackage, callingFeatureId,
9251 "getNumberOfModemsWithSimultaneousDataConnections")) {
Malcolm Chen2c63d402018-08-14 16:00:53 -07009252 return -1;
9253 }
9254
9255 final long identity = Binder.clearCallingIdentity();
9256 try {
9257 return mPhoneConfigurationManager.getNumberOfModemsWithSimultaneousDataConnections();
9258 } finally {
9259 Binder.restoreCallingIdentity(identity);
9260 }
9261 }
Pengquan Menga1bb6272018-09-06 09:59:22 -07009262
9263 @Override
9264 public int getCdmaRoamingMode(int subId) {
zoey chen7e6d4e52019-12-17 18:18:59 +08009265 TelephonyPermissions
Nathan Harold62c68512021-04-06 11:26:02 -07009266 .enforceCallingOrSelfReadPrivilegedPhoneStatePermissionOrCarrierPrivilege(
Pengquan Menga1bb6272018-09-06 09:59:22 -07009267 mApp, subId, "getCdmaRoamingMode");
9268
9269 final long identity = Binder.clearCallingIdentity();
9270 try {
9271 return (int) sendRequest(CMD_GET_CDMA_ROAMING_MODE, null /* argument */, subId);
9272 } finally {
9273 Binder.restoreCallingIdentity(identity);
9274 }
9275 }
9276
9277 @Override
9278 public boolean setCdmaRoamingMode(int subId, int mode) {
9279 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
9280 mApp, subId, "setCdmaRoamingMode");
9281
9282 final long identity = Binder.clearCallingIdentity();
9283 try {
9284 return (boolean) sendRequest(CMD_SET_CDMA_ROAMING_MODE, mode, subId);
9285 } finally {
9286 Binder.restoreCallingIdentity(identity);
9287 }
9288 }
9289
9290 @Override
Sarah Chinbaab1432020-10-28 13:46:24 -07009291 public int getCdmaSubscriptionMode(int subId) {
9292 TelephonyPermissions
Nathan Harold62c68512021-04-06 11:26:02 -07009293 .enforceCallingOrSelfReadPrivilegedPhoneStatePermissionOrCarrierPrivilege(
Sarah Chinbaab1432020-10-28 13:46:24 -07009294 mApp, subId, "getCdmaSubscriptionMode");
9295
9296 final long identity = Binder.clearCallingIdentity();
9297 try {
9298 return (int) sendRequest(CMD_GET_CDMA_SUBSCRIPTION_MODE, null /* argument */, subId);
9299 } finally {
9300 Binder.restoreCallingIdentity(identity);
9301 }
9302 }
9303
9304 @Override
Pengquan Menga1bb6272018-09-06 09:59:22 -07009305 public boolean setCdmaSubscriptionMode(int subId, int mode) {
9306 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
9307 mApp, subId, "setCdmaSubscriptionMode");
9308
9309 final long identity = Binder.clearCallingIdentity();
9310 try {
9311 return (boolean) sendRequest(CMD_SET_CDMA_SUBSCRIPTION_MODE, mode, subId);
9312 } finally {
9313 Binder.restoreCallingIdentity(identity);
9314 }
9315 }
Makoto Onukida3bf792018-09-18 16:06:29 -07009316
sqianc5eccab2018-10-19 18:46:41 -07009317 @Override
sqian8c685422019-02-22 15:55:18 -08009318 public Map<Integer, List<EmergencyNumber>> getEmergencyNumberList(
Philip P. Moltmann700a9592019-10-03 11:53:50 -07009319 String callingPackage, String callingFeatureId) {
sqian11b7a0e2018-12-05 18:48:28 -08009320 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
Philip P. Moltmann700a9592019-10-03 11:53:50 -07009321 mApp, getDefaultSubscription(), callingPackage, callingFeatureId,
9322 "getEmergencyNumberList")) {
sqian11b7a0e2018-12-05 18:48:28 -08009323 throw new SecurityException("Requires READ_PHONE_STATE permission.");
9324 }
9325 final long identity = Binder.clearCallingIdentity();
9326 try {
sqian854d44b2018-12-12 16:48:18 -08009327 Map<Integer, List<EmergencyNumber>> emergencyNumberListInternal = new HashMap<>();
9328 for (Phone phone: PhoneFactory.getPhones()) {
9329 if (phone.getEmergencyNumberTracker() != null
9330 && phone.getEmergencyNumberTracker().getEmergencyNumberList() != null) {
9331 emergencyNumberListInternal.put(
9332 phone.getSubId(),
9333 phone.getEmergencyNumberTracker().getEmergencyNumberList());
9334 }
sqian11b7a0e2018-12-05 18:48:28 -08009335 }
sqian854d44b2018-12-12 16:48:18 -08009336 return emergencyNumberListInternal;
sqian11b7a0e2018-12-05 18:48:28 -08009337 } finally {
9338 Binder.restoreCallingIdentity(identity);
9339 }
sqianc5eccab2018-10-19 18:46:41 -07009340 }
9341
9342 @Override
sqian8c685422019-02-22 15:55:18 -08009343 public boolean isEmergencyNumber(String number, boolean exactMatch) {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08009344 final Phone defaultPhone = getDefaultPhone();
sqian11b7a0e2018-12-05 18:48:28 -08009345 if (!exactMatch) {
9346 TelephonyPermissions
Nathan Harold62c68512021-04-06 11:26:02 -07009347 .enforceCallingOrSelfReadPrivilegedPhoneStatePermissionOrCarrierPrivilege(
sqian8c685422019-02-22 15:55:18 -08009348 mApp, defaultPhone.getSubId(), "isEmergencyNumber(Potential)");
sqian11b7a0e2018-12-05 18:48:28 -08009349 }
9350 final long identity = Binder.clearCallingIdentity();
9351 try {
sqian854d44b2018-12-12 16:48:18 -08009352 for (Phone phone: PhoneFactory.getPhones()) {
Chinmay Dhodapkard521bb12022-08-16 15:49:54 -07009353 //Note: we ignore passed in param exactMatch. We can remove it once
9354 // TelephonyManager#isPotentialEmergencyNumber is removed completely
sqian854d44b2018-12-12 16:48:18 -08009355 if (phone.getEmergencyNumberTracker() != null
Taesu Leee050c002020-10-13 17:19:35 +09009356 && phone.getEmergencyNumberTracker()
Chinmay Dhodapkard521bb12022-08-16 15:49:54 -07009357 .isEmergencyNumber(number)) {
Taesu Leee050c002020-10-13 17:19:35 +09009358 return true;
sqian11b7a0e2018-12-05 18:48:28 -08009359 }
sqian11b7a0e2018-12-05 18:48:28 -08009360 }
9361 return false;
9362 } finally {
9363 Binder.restoreCallingIdentity(identity);
9364 }
9365 }
9366
sqianf4ca7ed2019-01-15 18:32:07 -08009367 /**
Shuo Qianccbaf742021-02-22 18:32:21 -08009368 * Start emergency callback mode for GsmCdmaPhone for testing.
9369 */
9370 @Override
9371 public void startEmergencyCallbackMode() {
9372 TelephonyPermissions.enforceShellOnly(Binder.getCallingUid(),
9373 "startEmergencyCallbackMode");
9374 enforceModifyPermission();
9375 final long identity = Binder.clearCallingIdentity();
9376 try {
9377 for (Phone phone : PhoneFactory.getPhones()) {
9378 Rlog.d(LOG_TAG, "startEmergencyCallbackMode phone type: " + phone.getPhoneType());
9379 if (phone != null && ((phone.getPhoneType() == PHONE_TYPE_GSM)
9380 || (phone.getPhoneType() == PHONE_TYPE_CDMA))) {
9381 GsmCdmaPhone gsmCdmaPhone = (GsmCdmaPhone) phone;
9382 gsmCdmaPhone.obtainMessage(
9383 GsmCdmaPhone.EVENT_EMERGENCY_CALLBACK_MODE_ENTER).sendToTarget();
9384 Rlog.d(LOG_TAG, "startEmergencyCallbackMode: triggered");
9385 }
9386 }
9387 } finally {
9388 Binder.restoreCallingIdentity(identity);
9389 }
9390 }
9391
9392 /**
sqianf4ca7ed2019-01-15 18:32:07 -08009393 * Update emergency number list for test mode.
9394 */
9395 @Override
9396 public void updateEmergencyNumberListTestMode(int action, EmergencyNumber num) {
9397 TelephonyPermissions.enforceShellOnly(Binder.getCallingUid(),
9398 "updateEmergencyNumberListTestMode");
9399
9400 final long identity = Binder.clearCallingIdentity();
9401 try {
9402 for (Phone phone: PhoneFactory.getPhones()) {
9403 EmergencyNumberTracker tracker = phone.getEmergencyNumberTracker();
9404 if (tracker != null) {
9405 tracker.executeEmergencyNumberTestModeCommand(action, num);
9406 }
9407 }
9408 } finally {
9409 Binder.restoreCallingIdentity(identity);
9410 }
9411 }
9412
9413 /**
9414 * Get the full emergency number list for test mode.
9415 */
9416 @Override
9417 public List<String> getEmergencyNumberListTestMode() {
9418 TelephonyPermissions.enforceShellOnly(Binder.getCallingUid(),
9419 "getEmergencyNumberListTestMode");
9420
9421 final long identity = Binder.clearCallingIdentity();
9422 try {
9423 Set<String> emergencyNumbers = new HashSet<>();
9424 for (Phone phone: PhoneFactory.getPhones()) {
9425 EmergencyNumberTracker tracker = phone.getEmergencyNumberTracker();
9426 if (tracker != null) {
9427 for (EmergencyNumber num : tracker.getEmergencyNumberList()) {
9428 emergencyNumbers.add(num.getNumber());
9429 }
9430 }
9431 }
9432 return new ArrayList<>(emergencyNumbers);
9433 } finally {
9434 Binder.restoreCallingIdentity(identity);
9435 }
9436 }
9437
chen xud6b45bd2018-10-30 22:27:10 -07009438 @Override
Shuo Qian3b6ee772019-11-13 17:43:31 -08009439 public int getEmergencyNumberDbVersion(int subId) {
9440 enforceReadPrivilegedPermission("getEmergencyNumberDbVersion");
9441
9442 final long identity = Binder.clearCallingIdentity();
9443 try {
9444 final Phone phone = getPhone(subId);
9445 if (phone == null) {
9446 loge("getEmergencyNumberDbVersion fails with invalid subId: " + subId);
9447 return TelephonyManager.INVALID_EMERGENCY_NUMBER_DB_VERSION;
9448 }
9449 return phone.getEmergencyNumberDbVersion();
9450 } finally {
9451 Binder.restoreCallingIdentity(identity);
9452 }
9453 }
9454
9455 @Override
9456 public void notifyOtaEmergencyNumberDbInstalled() {
9457 enforceModifyPermission();
9458
9459 final long identity = Binder.clearCallingIdentity();
9460 try {
9461 for (Phone phone: PhoneFactory.getPhones()) {
9462 EmergencyNumberTracker tracker = phone.getEmergencyNumberTracker();
9463 if (tracker != null) {
9464 tracker.updateOtaEmergencyNumberDatabase();
9465 }
9466 }
9467 } finally {
9468 Binder.restoreCallingIdentity(identity);
9469 }
9470 }
9471
9472 @Override
Shuo Qianc373f112020-03-05 17:55:34 -08009473 public void updateOtaEmergencyNumberDbFilePath(ParcelFileDescriptor otaParcelFileDescriptor) {
Shuo Qian3b6ee772019-11-13 17:43:31 -08009474 enforceActiveEmergencySessionPermission();
9475
9476 final long identity = Binder.clearCallingIdentity();
9477 try {
9478 for (Phone phone: PhoneFactory.getPhones()) {
9479 EmergencyNumberTracker tracker = phone.getEmergencyNumberTracker();
9480 if (tracker != null) {
Shuo Qianc373f112020-03-05 17:55:34 -08009481 tracker.updateOtaEmergencyNumberDbFilePath(otaParcelFileDescriptor);
9482 }
9483 }
9484 } finally {
9485 Binder.restoreCallingIdentity(identity);
9486 }
9487 }
9488
9489 @Override
9490 public void resetOtaEmergencyNumberDbFilePath() {
9491 enforceActiveEmergencySessionPermission();
9492
9493 final long identity = Binder.clearCallingIdentity();
9494 try {
9495 for (Phone phone: PhoneFactory.getPhones()) {
9496 EmergencyNumberTracker tracker = phone.getEmergencyNumberTracker();
9497 if (tracker != null) {
9498 tracker.resetOtaEmergencyNumberDbFilePath();
Shuo Qian3b6ee772019-11-13 17:43:31 -08009499 }
9500 }
9501 } finally {
9502 Binder.restoreCallingIdentity(identity);
9503 }
9504 }
9505
9506 @Override
chen xud6b45bd2018-10-30 22:27:10 -07009507 public List<String> getCertsFromCarrierPrivilegeAccessRules(int subId) {
9508 enforceReadPrivilegedPermission("getCertsFromCarrierPrivilegeAccessRules");
9509 Phone phone = getPhone(subId);
9510 if (phone == null) {
9511 return null;
9512 }
9513 final long identity = Binder.clearCallingIdentity();
9514 try {
9515 UiccProfile profile = UiccController.getInstance()
9516 .getUiccProfileForPhone(phone.getPhoneId());
9517 if (profile != null) {
9518 return profile.getCertsFromCarrierPrivilegeAccessRules();
9519 }
9520 } finally {
9521 Binder.restoreCallingIdentity(identity);
9522 }
9523 return null;
9524 }
Malcolm Chen8e4ed912019-01-15 20:22:16 -08009525
9526 /**
9527 * Enable or disable a modem stack.
9528 */
9529 @Override
9530 public boolean enableModemForSlot(int slotIndex, boolean enable) {
9531 enforceModifyPermission();
9532
9533 final long identity = Binder.clearCallingIdentity();
9534 try {
9535 Phone phone = PhoneFactory.getPhone(slotIndex);
9536 if (phone == null) {
9537 return false;
9538 } else {
9539 return (Boolean) sendRequest(CMD_REQUEST_ENABLE_MODEM, enable, phone, null);
9540 }
9541 } finally {
9542 Binder.restoreCallingIdentity(identity);
9543 }
9544 }
Michelecea4cf22018-12-21 15:00:11 -08009545
Malcolm Chen4bcd9822019-03-27 18:34:05 -07009546 /**
9547 * Whether a modem stack is enabled or not.
9548 */
9549 @Override
Philip P. Moltmann700a9592019-10-03 11:53:50 -07009550 public boolean isModemEnabledForSlot(int slotIndex, String callingPackage,
9551 String callingFeatureId) {
Malcolm Chen4bcd9822019-03-27 18:34:05 -07009552 Phone phone = PhoneFactory.getPhone(slotIndex);
9553 if (phone == null) return false;
9554
9555 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
Philip P. Moltmann700a9592019-10-03 11:53:50 -07009556 mApp, phone.getSubId(), callingPackage, callingFeatureId,
9557 "isModemEnabledForSlot")) {
Malcolm Chen4bcd9822019-03-27 18:34:05 -07009558 throw new SecurityException("Requires READ_PHONE_STATE permission.");
9559 }
9560
9561 final long identity = Binder.clearCallingIdentity();
9562 try {
Nazanin Bakhshif71371d2019-04-29 17:29:44 -07009563 try {
9564 return mPhoneConfigurationManager.getPhoneStatusFromCache(phone.getPhoneId());
9565 } catch (NoSuchElementException ex) {
9566 return (Boolean) sendRequest(CMD_GET_MODEM_STATUS, null, phone, null);
9567 }
Malcolm Chen4bcd9822019-03-27 18:34:05 -07009568 } finally {
9569 Binder.restoreCallingIdentity(identity);
9570 }
9571 }
9572
Michelecea4cf22018-12-21 15:00:11 -08009573 @Override
Michele0ea7d782019-03-19 14:58:42 -07009574 public void setMultiSimCarrierRestriction(boolean isMultiSimCarrierRestricted) {
Michelecea4cf22018-12-21 15:00:11 -08009575 enforceModifyPermission();
9576
9577 final long identity = Binder.clearCallingIdentity();
9578 try {
9579 mTelephonySharedPreferences.edit()
Michele0ea7d782019-03-19 14:58:42 -07009580 .putBoolean(PREF_MULTI_SIM_RESTRICTED, isMultiSimCarrierRestricted)
Michelecea4cf22018-12-21 15:00:11 -08009581 .commit();
9582 } finally {
9583 Binder.restoreCallingIdentity(identity);
9584 }
9585 }
9586
9587 @Override
Michele0ea7d782019-03-19 14:58:42 -07009588 @TelephonyManager.IsMultiSimSupportedResult
Philip P. Moltmann700a9592019-10-03 11:53:50 -07009589 public int isMultiSimSupported(String callingPackage, String callingFeatureId) {
Michele4245e952019-02-04 11:36:23 -08009590 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(mApp,
Philip P. Moltmann700a9592019-10-03 11:53:50 -07009591 getDefaultPhone().getSubId(), callingPackage, callingFeatureId,
9592 "isMultiSimSupported")) {
Michele0ea7d782019-03-19 14:58:42 -07009593 return TelephonyManager.MULTISIM_NOT_SUPPORTED_BY_HARDWARE;
Michele4245e952019-02-04 11:36:23 -08009594 }
Michelecea4cf22018-12-21 15:00:11 -08009595
9596 final long identity = Binder.clearCallingIdentity();
9597 try {
Michele0ea7d782019-03-19 14:58:42 -07009598 return isMultiSimSupportedInternal();
Michelecea4cf22018-12-21 15:00:11 -08009599 } finally {
9600 Binder.restoreCallingIdentity(identity);
9601 }
9602 }
Nazanin Bakhshi628473f2019-01-29 17:37:52 -08009603
Michele0ea7d782019-03-19 14:58:42 -07009604 @TelephonyManager.IsMultiSimSupportedResult
9605 private int isMultiSimSupportedInternal() {
Michele30b57b22019-03-01 12:01:14 -08009606 // If the device has less than 2 SIM cards, indicate that multisim is restricted.
9607 int numPhysicalSlots = UiccController.getInstance().getUiccSlots().length;
9608 if (numPhysicalSlots < 2) {
Michele0ea7d782019-03-19 14:58:42 -07009609 loge("isMultiSimSupportedInternal: requires at least 2 cards");
9610 return TelephonyManager.MULTISIM_NOT_SUPPORTED_BY_HARDWARE;
Michele30b57b22019-03-01 12:01:14 -08009611 }
9612 // Check if the hardware supports multisim functionality. If usage of multisim is not
9613 // supported by the modem, indicate that it is restricted.
9614 PhoneCapability staticCapability =
9615 mPhoneConfigurationManager.getStaticPhoneCapability();
9616 if (staticCapability == null) {
Michele0ea7d782019-03-19 14:58:42 -07009617 loge("isMultiSimSupportedInternal: no static configuration available");
9618 return TelephonyManager.MULTISIM_NOT_SUPPORTED_BY_HARDWARE;
Michele30b57b22019-03-01 12:01:14 -08009619 }
SongFerngWang8236caa2021-01-17 21:51:44 +08009620 if (staticCapability.getLogicalModemList().size() < 2) {
Michele0ea7d782019-03-19 14:58:42 -07009621 loge("isMultiSimSupportedInternal: maximum number of modem is < 2");
9622 return TelephonyManager.MULTISIM_NOT_SUPPORTED_BY_HARDWARE;
Michele30b57b22019-03-01 12:01:14 -08009623 }
9624 // Check if support of multiple SIMs is restricted by carrier
9625 if (mTelephonySharedPreferences.getBoolean(PREF_MULTI_SIM_RESTRICTED, false)) {
Michele0ea7d782019-03-19 14:58:42 -07009626 return TelephonyManager.MULTISIM_NOT_SUPPORTED_BY_CARRIER;
Michele30b57b22019-03-01 12:01:14 -08009627 }
9628
Michele0ea7d782019-03-19 14:58:42 -07009629 return TelephonyManager.MULTISIM_ALLOWED;
Michele30b57b22019-03-01 12:01:14 -08009630 }
9631
Nazanin Bakhshi628473f2019-01-29 17:37:52 -08009632 /**
9633 * Switch configs to enable multi-sim or switch back to single-sim
Nazanin Bakhshi17318782019-03-01 11:56:08 -08009634 * Note: Switch from multi-sim to single-sim is only possible with MODIFY_PHONE_STATE
9635 * permission, but the other way around is possible with either MODIFY_PHONE_STATE
9636 * or carrier privileges
Nazanin Bakhshi628473f2019-01-29 17:37:52 -08009637 * @param numOfSims number of active sims we want to switch to
9638 */
9639 @Override
9640 public void switchMultiSimConfig(int numOfSims) {
Nazanin Bakhshi17318782019-03-01 11:56:08 -08009641 if (numOfSims == 1) {
9642 enforceModifyPermission();
9643 } else {
9644 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
9645 mApp, SubscriptionManager.DEFAULT_SUBSCRIPTION_ID, "switchMultiSimConfig");
9646 }
Nazanin Bakhshi628473f2019-01-29 17:37:52 -08009647 final long identity = Binder.clearCallingIdentity();
Michele30b57b22019-03-01 12:01:14 -08009648
Nazanin Bakhshi628473f2019-01-29 17:37:52 -08009649 try {
Michele30b57b22019-03-01 12:01:14 -08009650 //only proceed if multi-sim is not restricted
Michele0ea7d782019-03-19 14:58:42 -07009651 if (isMultiSimSupportedInternal() != TelephonyManager.MULTISIM_ALLOWED) {
Michele30b57b22019-03-01 12:01:14 -08009652 loge("switchMultiSimConfig not possible. It is restricted or not supported.");
9653 return;
9654 }
Nazanin Bakhshi628473f2019-01-29 17:37:52 -08009655 mPhoneConfigurationManager.switchMultiSimConfig(numOfSims);
9656 } finally {
9657 Binder.restoreCallingIdentity(identity);
9658 }
9659 }
9660
Hyungjun Parkbb07fde2019-01-10 15:28:51 +09009661 @Override
9662 public boolean isApplicationOnUicc(int subId, int appType) {
9663 enforceReadPrivilegedPermission("isApplicationOnUicc");
9664 Phone phone = getPhone(subId);
9665 if (phone == null) {
9666 return false;
9667 }
9668 final long identity = Binder.clearCallingIdentity();
9669 try {
Muralidhar Reddy472c2ae2021-09-29 19:38:40 +00009670 UiccPort uiccPort = phone.getUiccPort();
9671 if (uiccPort == null) {
Hyungjun Parkbb07fde2019-01-10 15:28:51 +09009672 return false;
9673 }
Muralidhar Reddy472c2ae2021-09-29 19:38:40 +00009674 UiccProfile uiccProfile = uiccPort.getUiccProfile();
Hyungjun Parkbb07fde2019-01-10 15:28:51 +09009675 if (uiccProfile == null) {
9676 return false;
9677 }
9678 if (TelephonyManager.APPTYPE_SIM <= appType
9679 && appType <= TelephonyManager.APPTYPE_ISIM) {
9680 return uiccProfile.isApplicationOnIcc(AppType.values()[appType]);
9681 }
9682 return false;
9683 } finally {
9684 Binder.restoreCallingIdentity(identity);
9685 }
9686 }
9687
Nazanin Bakhshi628473f2019-01-29 17:37:52 -08009688 /**
chen xub4baa772019-04-03 10:23:41 -07009689 * Get whether making changes to modem configurations will trigger reboot.
9690 * Return value defaults to true.
Nazanin Bakhshi5fe5ef22019-01-30 10:52:09 -08009691 */
9692 @Override
Philip P. Moltmann700a9592019-10-03 11:53:50 -07009693 public boolean doesSwitchMultiSimConfigTriggerReboot(int subId, String callingPackage,
9694 String callingFeatureId) {
chen xub4baa772019-04-03 10:23:41 -07009695 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
Philip P. Moltmann700a9592019-10-03 11:53:50 -07009696 mApp, subId, callingPackage, callingFeatureId,
9697 "doesSwitchMultiSimConfigTriggerReboot")) {
chen xub4baa772019-04-03 10:23:41 -07009698 return false;
9699 }
Nazanin Bakhshi5fe5ef22019-01-30 10:52:09 -08009700 final long identity = Binder.clearCallingIdentity();
9701 try {
9702 return mPhoneConfigurationManager.isRebootRequiredForModemConfigChange();
9703 } finally {
9704 Binder.restoreCallingIdentity(identity);
9705 }
9706 }
9707
Nathan Harold29f5f052019-02-15 13:41:57 -08009708 private void updateModemStateMetrics() {
9709 TelephonyMetrics metrics = TelephonyMetrics.getInstance();
9710 // TODO: check the state for each modem if the api is ready.
9711 metrics.updateEnabledModemBitmap((1 << TelephonyManager.from(mApp).getPhoneCount()) - 1);
9712 }
9713
Pengquan Meng3889a572019-01-23 11:16:29 -08009714 @Override
sandeepjsa208e3b2021-11-17 04:05:58 +00009715 public List<UiccSlotMapping> getSlotsMapping(String callingPackage) {
Pengquan Meng3889a572019-01-23 11:16:29 -08009716 enforceReadPrivilegedPermission("getSlotsMapping");
sandeepjsa208e3b2021-11-17 04:05:58 +00009717 // Verify that the callingPackage belongs to the calling UID
9718 mApp.getSystemService(AppOpsManager.class)
9719 .checkPackage(Binder.getCallingUid(), callingPackage);
Pengquan Meng3889a572019-01-23 11:16:29 -08009720 final long identity = Binder.clearCallingIdentity();
sandeepjsa208e3b2021-11-17 04:05:58 +00009721 List<UiccSlotMapping> slotMap = new ArrayList<>();
Pengquan Meng3889a572019-01-23 11:16:29 -08009722 try {
sandeepjsa208e3b2021-11-17 04:05:58 +00009723 UiccSlotInfo[] slotInfos = getUiccSlotsInfo(mApp.getOpPackageName());
9724 if (slotInfos != null) {
9725 for (int i = 0; i < slotInfos.length; i++) {
9726 for (UiccPortInfo portInfo : slotInfos[i].getPorts()) {
9727 if (SubscriptionManager.isValidPhoneId(portInfo.getLogicalSlotIndex())) {
9728 slotMap.add(new UiccSlotMapping(portInfo.getPortIndex(), i,
9729 portInfo.getLogicalSlotIndex()));
9730 }
9731 }
Pengquan Meng3889a572019-01-23 11:16:29 -08009732 }
9733 }
sandeepjsa208e3b2021-11-17 04:05:58 +00009734 return slotMap;
Pengquan Meng3889a572019-01-23 11:16:29 -08009735 } finally {
9736 Binder.restoreCallingIdentity(identity);
9737 }
9738 }
Nathan Harold48d6fd52019-02-06 19:01:40 -08009739
9740 /**
9741 * Get the IRadio HAL Version
9742 */
9743 @Override
9744 public int getRadioHalVersion() {
9745 Phone phone = getDefaultPhone();
9746 if (phone == null) return -1;
9747 HalVersion hv = phone.getHalVersion();
9748 if (hv.equals(HalVersion.UNKNOWN)) return -1;
9749 return hv.major * 100 + hv.minor;
9750 }
Malcolm Chendc8c10e2019-04-10 18:25:07 -07009751
9752 /**
Shuo Qianda2d6ec2020-01-14 15:18:28 -08009753 * Get the current calling package name.
9754 * @return the current calling package name
9755 */
9756 @Override
9757 public String getCurrentPackageName() {
9758 return mApp.getPackageManager().getPackagesForUid(Binder.getCallingUid())[0];
9759 }
9760
9761 /**
Malcolm Chene5ad5792019-04-18 13:51:02 -07009762 * Return whether data is enabled for certain APN type. This will tell if framework will accept
9763 * corresponding network requests on a subId.
9764 *
9765 * Data is enabled if:
Malcolm Chendc8c10e2019-04-10 18:25:07 -07009766 * 1) user data is turned on, or
Malcolm Chene5ad5792019-04-18 13:51:02 -07009767 * 2) APN is un-metered for this subscription, or
9768 * 3) APN type is whitelisted. E.g. MMS is whitelisted if
Hall Liu746e03c2020-09-25 11:13:49 -07009769 * {@link TelephonyManager#MOBILE_DATA_POLICY_MMS_ALWAYS_ALLOWED} is enabled.
Malcolm Chene5ad5792019-04-18 13:51:02 -07009770 *
9771 * @return whether data is allowed for a apn type.
9772 *
9773 * @hide
Malcolm Chendc8c10e2019-04-10 18:25:07 -07009774 */
9775 @Override
Malcolm Chene5ad5792019-04-18 13:51:02 -07009776 public boolean isDataEnabledForApn(int apnType, int subId, String callingPackage) {
Amit Mahajan5d4e1922019-10-07 16:20:43 -07009777 enforceReadPrivilegedPermission("Needs READ_PRIVILEGED_PHONE_STATE for "
9778 + "isDataEnabledForApn");
Malcolm Chendc8c10e2019-04-10 18:25:07 -07009779
9780 // Now that all security checks passes, perform the operation as ourselves.
9781 final long identity = Binder.clearCallingIdentity();
9782 try {
9783 Phone phone = getPhone(subId);
9784 if (phone == null) return false;
9785
Jack Yu27422a52022-03-21 10:38:05 -07009786 boolean isMetered;
Jack Yu99e87332021-12-17 23:14:15 -08009787 boolean isDataEnabled;
Jack Yu7968c6d2022-07-31 00:43:21 -07009788 isMetered = phone.getDataNetworkController().getDataConfigManager()
9789 .isMeteredCapability(DataUtils.apnTypeToNetworkCapability(apnType),
9790 phone.getServiceState().getDataRoaming());
9791 isDataEnabled = phone.getDataSettingsManager().isDataEnabled(apnType);
Jack Yu99e87332021-12-17 23:14:15 -08009792 return !isMetered || isDataEnabled;
Malcolm Chene5ad5792019-04-18 13:51:02 -07009793 } finally {
9794 Binder.restoreCallingIdentity(identity);
9795 }
9796 }
9797
9798 @Override
Jack Yu41407ee2019-05-13 16:54:09 -07009799 public boolean isApnMetered(@ApnType int apnType, int subId) {
Malcolm Chene5ad5792019-04-18 13:51:02 -07009800 enforceReadPrivilegedPermission("isApnMetered");
9801
9802 // Now that all security checks passes, perform the operation as ourselves.
9803 final long identity = Binder.clearCallingIdentity();
9804 try {
9805 Phone phone = getPhone(subId);
9806 if (phone == null) return true; // By default return true.
Jack Yu7968c6d2022-07-31 00:43:21 -07009807 return phone.getDataNetworkController().getDataConfigManager().isMeteredCapability(
9808 DataUtils.apnTypeToNetworkCapability(apnType),
9809 phone.getServiceState().getDataRoaming());
Malcolm Chendc8c10e2019-04-10 18:25:07 -07009810 } finally {
9811 Binder.restoreCallingIdentity(identity);
9812 }
9813 }
Brad Ebingera63db5f2019-04-23 16:31:13 -07009814
9815 @Override
Hall Liu73f5d362020-01-20 13:42:00 -08009816 public void setSystemSelectionChannels(List<RadioAccessSpecifier> specifiers,
9817 int subscriptionId, IBooleanConsumer resultCallback) {
9818 enforceModifyPermission();
9819 long token = Binder.clearCallingIdentity();
9820 try {
9821 Phone phone = getPhone(subscriptionId);
9822 if (phone == null) {
9823 try {
9824 if (resultCallback != null) {
9825 resultCallback.accept(false);
9826 }
9827 } catch (RemoteException e) {
9828 // ignore
9829 }
9830 return;
9831 }
9832 Pair<List<RadioAccessSpecifier>, Consumer<Boolean>> argument =
9833 Pair.create(specifiers, (x) -> {
9834 try {
9835 if (resultCallback != null) {
9836 resultCallback.accept(x);
9837 }
9838 } catch (RemoteException e) {
9839 // ignore
9840 }
9841 });
9842 sendRequestAsync(CMD_SET_SYSTEM_SELECTION_CHANNELS, argument, phone, null);
9843 } finally {
9844 Binder.restoreCallingIdentity(token);
9845 }
9846 }
9847
9848 @Override
Sarah Chin679c08a2020-11-18 13:39:35 -08009849 public List<RadioAccessSpecifier> getSystemSelectionChannels(int subId) {
9850 TelephonyPermissions
Nathan Harold62c68512021-04-06 11:26:02 -07009851 .enforceCallingOrSelfReadPrivilegedPhoneStatePermissionOrCarrierPrivilege(
Sarah Chin679c08a2020-11-18 13:39:35 -08009852 mApp, subId, "getSystemSelectionChannels");
9853 WorkSource workSource = getWorkSource(Binder.getCallingUid());
9854 final long identity = Binder.clearCallingIdentity();
9855 try {
Sarah Chin428d1d62021-03-13 03:17:40 -08009856 Object result = sendRequest(CMD_GET_SYSTEM_SELECTION_CHANNELS, null, subId, workSource);
9857 if (result instanceof IllegalStateException) {
9858 throw (IllegalStateException) result;
9859 }
9860 List<RadioAccessSpecifier> specifiers = (List<RadioAccessSpecifier>) result;
Sarah Chin679c08a2020-11-18 13:39:35 -08009861 if (DBG) log("getSystemSelectionChannels: " + specifiers);
9862 return specifiers;
9863 } finally {
9864 Binder.restoreCallingIdentity(identity);
9865 }
9866 }
9867
9868 @Override
Jack Yu8b766fc2022-03-21 09:42:33 -07009869 public boolean isMvnoMatched(int slotIndex, int mvnoType, @NonNull String mvnoMatchData) {
changbettyca3d40d2020-03-03 16:27:31 +08009870 enforceReadPrivilegedPermission("isMvnoMatched");
Jack Yu8b766fc2022-03-21 09:42:33 -07009871 return UiccController.getInstance().mvnoMatches(slotIndex, mvnoType, mvnoMatchData);
changbetty7157e9e2019-12-06 18:16:37 +08009872 }
9873
9874 @Override
Philip P. Moltmannd02b7372020-03-18 17:06:12 -07009875 public void enqueueSmsPickResult(String callingPackage, String callingAttributionTag,
9876 IIntegerConsumer pendingSubIdResult) {
Shuo Qianda2d6ec2020-01-14 15:18:28 -08009877 if (callingPackage == null) {
9878 callingPackage = getCurrentPackageName();
9879 }
Brad Ebingera63db5f2019-04-23 16:31:13 -07009880 SmsPermissions permissions = new SmsPermissions(getDefaultPhone(), mApp,
9881 (AppOpsManager) mApp.getSystemService(Context.APP_OPS_SERVICE));
Philip P. Moltmannd02b7372020-03-18 17:06:12 -07009882 if (!permissions.checkCallingCanSendSms(callingPackage, callingAttributionTag,
9883 "Sending message")) {
Brad Ebingera63db5f2019-04-23 16:31:13 -07009884 throw new SecurityException("Requires SEND_SMS permission to perform this operation");
9885 }
9886 PickSmsSubscriptionActivity.addPendingResult(pendingSubIdResult);
9887 Intent intent = new Intent();
9888 intent.setClass(mApp, PickSmsSubscriptionActivity.class);
9889 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
9890 // Bring up choose default SMS subscription dialog right now
9891 intent.putExtra(PickSmsSubscriptionActivity.DIALOG_TYPE_KEY,
9892 PickSmsSubscriptionActivity.SMS_PICK_FOR_MESSAGE);
9893 mApp.startActivity(intent);
9894 }
chen xud5ca2d52019-05-28 15:20:57 -07009895
9896 @Override
9897 public String getMmsUAProfUrl(int subId) {
9898 //TODO investigate if this API should require proper permission check in R b/133791609
9899 final long identity = Binder.clearCallingIdentity();
9900 try {
Guoqiang.Qiu171383d2020-07-13 09:38:32 +08009901 String carrierUAProfUrl = mApp.getCarrierConfigForSubId(subId).getString(
9902 CarrierConfigManager.KEY_MMS_UA_PROF_URL_STRING);
9903 if (!TextUtils.isEmpty(carrierUAProfUrl)) {
9904 return carrierUAProfUrl;
9905 }
Daniel Brightebb4eb72020-02-18 15:16:33 -08009906 return SubscriptionManager.getResourcesForSubId(getDefaultPhone().getContext(), subId)
9907 .getString(com.android.internal.R.string.config_mms_user_agent_profile_url);
chen xud5ca2d52019-05-28 15:20:57 -07009908 } finally {
9909 Binder.restoreCallingIdentity(identity);
9910 }
9911 }
9912
9913 @Override
9914 public String getMmsUserAgent(int subId) {
9915 //TODO investigate if this API should require proper permission check in R b/133791609
9916 final long identity = Binder.clearCallingIdentity();
9917 try {
Guoqiang.Qiu171383d2020-07-13 09:38:32 +08009918 String carrierUserAgent = mApp.getCarrierConfigForSubId(subId).getString(
9919 CarrierConfigManager.KEY_MMS_USER_AGENT_STRING);
9920 if (!TextUtils.isEmpty(carrierUserAgent)) {
9921 return carrierUserAgent;
9922 }
Daniel Brightebb4eb72020-02-18 15:16:33 -08009923 return SubscriptionManager.getResourcesForSubId(getDefaultPhone().getContext(), subId)
9924 .getString(com.android.internal.R.string.config_mms_user_agent);
chen xud5ca2d52019-05-28 15:20:57 -07009925 } finally {
9926 Binder.restoreCallingIdentity(identity);
9927 }
9928 }
Jack Yub07d4972019-05-28 16:12:25 -07009929
9930 @Override
Hall Liua62f5da2020-09-25 10:42:19 -07009931 public boolean isMobileDataPolicyEnabled(int subscriptionId, int policy) {
9932 enforceReadPrivilegedPermission("isMobileDataPolicyEnabled");
Jack Yub07d4972019-05-28 16:12:25 -07009933
Jack Yub07d4972019-05-28 16:12:25 -07009934 final long identity = Binder.clearCallingIdentity();
9935 try {
Hall Liua62f5da2020-09-25 10:42:19 -07009936 Phone phone = getPhone(subscriptionId);
Jack Yub07d4972019-05-28 16:12:25 -07009937 if (phone == null) return false;
9938
Hall Liua62f5da2020-09-25 10:42:19 -07009939 switch (policy) {
9940 case TelephonyManager.MOBILE_DATA_POLICY_DATA_ON_NON_DEFAULT_DURING_VOICE_CALL:
Jack Yu7968c6d2022-07-31 00:43:21 -07009941 return phone.getDataSettingsManager().isDataAllowedInVoiceCall();
Hall Liua62f5da2020-09-25 10:42:19 -07009942 case TelephonyManager.MOBILE_DATA_POLICY_MMS_ALWAYS_ALLOWED:
Jack Yu7968c6d2022-07-31 00:43:21 -07009943 return phone.getDataSettingsManager().isMmsAlwaysAllowed();
Hall Liua62f5da2020-09-25 10:42:19 -07009944 default:
9945 throw new IllegalArgumentException(policy + " is not a valid policy");
9946 }
Jack Yub07d4972019-05-28 16:12:25 -07009947 } finally {
9948 Binder.restoreCallingIdentity(identity);
9949 }
9950 }
9951
9952 @Override
Hall Liuc66bb112021-02-02 12:09:32 -08009953 public void setMobileDataPolicyEnabled(int subscriptionId, int policy,
Hall Liua62f5da2020-09-25 10:42:19 -07009954 boolean enabled) {
changbettyd5c246e2019-12-24 15:40:37 +08009955 enforceModifyPermission();
9956
changbettyd5c246e2019-12-24 15:40:37 +08009957 final long identity = Binder.clearCallingIdentity();
9958 try {
Hall Liua62f5da2020-09-25 10:42:19 -07009959 Phone phone = getPhone(subscriptionId);
9960 if (phone == null) return;
changbettyd5c246e2019-12-24 15:40:37 +08009961
Hall Liua62f5da2020-09-25 10:42:19 -07009962 switch (policy) {
9963 case TelephonyManager.MOBILE_DATA_POLICY_DATA_ON_NON_DEFAULT_DURING_VOICE_CALL:
Jack Yu7968c6d2022-07-31 00:43:21 -07009964 phone.getDataSettingsManager().setAllowDataDuringVoiceCall(enabled);
Hall Liua62f5da2020-09-25 10:42:19 -07009965 break;
9966 case TelephonyManager.MOBILE_DATA_POLICY_MMS_ALWAYS_ALLOWED:
Jack Yu7968c6d2022-07-31 00:43:21 -07009967 phone.getDataSettingsManager().setAlwaysAllowMmsData(enabled);
Hall Liua62f5da2020-09-25 10:42:19 -07009968 break;
9969 default:
9970 throw new IllegalArgumentException(policy + " is not a valid policy");
9971 }
changbettyd5c246e2019-12-24 15:40:37 +08009972 } finally {
9973 Binder.restoreCallingIdentity(identity);
9974 }
9975 }
9976
Tyler Gunn7bcdc742019-10-04 15:56:59 -07009977 /**
Hall Liu746e03c2020-09-25 11:13:49 -07009978 * Updates whether conference event package handling is enabled.
Tyler Gunn7bcdc742019-10-04 15:56:59 -07009979 * @param isCepEnabled {@code true} if CEP handling is enabled (default), or {@code false}
9980 * otherwise.
9981 */
9982 @Override
9983 public void setCepEnabled(boolean isCepEnabled) {
9984 TelephonyPermissions.enforceShellOnly(Binder.getCallingUid(), "setCepEnabled");
9985
9986 final long identity = Binder.clearCallingIdentity();
9987 try {
9988 Rlog.i(LOG_TAG, "setCepEnabled isCepEnabled=" + isCepEnabled);
9989 for (Phone phone : PhoneFactory.getPhones()) {
9990 Phone defaultPhone = phone.getImsPhone();
9991 if (defaultPhone != null && defaultPhone.getPhoneType() == PHONE_TYPE_IMS) {
9992 ImsPhone imsPhone = (ImsPhone) defaultPhone;
9993 ImsPhoneCallTracker imsPhoneCallTracker =
9994 (ImsPhoneCallTracker) imsPhone.getCallTracker();
9995 imsPhoneCallTracker.setConferenceEventPackageEnabled(isCepEnabled);
9996 Rlog.i(LOG_TAG, "setCepEnabled isCepEnabled=" + isCepEnabled + ", for imsPhone "
9997 + imsPhone.getMsisdn());
9998 }
9999 }
10000 } finally {
10001 Binder.restoreCallingIdentity(identity);
10002 }
10003 }
allenwtsu46dcc572020-01-08 18:24:03 +080010004
10005 /**
10006 * Notify that an RCS autoconfiguration XML file has been received for provisioning.
10007 *
10008 * @param config The XML file to be read. ASCII/UTF8 encoded text if not compressed.
10009 * @param isCompressed The XML file is compressed in gzip format and must be decompressed
10010 * before being read.
10011 */
10012 @Override
10013 public void notifyRcsAutoConfigurationReceived(int subId, @NonNull byte[] config, boolean
10014 isCompressed) {
10015 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
10016 mApp, subId, "notifyRcsAutoConfigurationReceived");
Hui Wang761a6682020-10-31 05:12:53 +000010017 if (!SubscriptionManager.isValidSubscriptionId(subId)) {
10018 throw new IllegalArgumentException("Invalid Subscription ID: " + subId);
10019 }
10020 if (!isImsAvailableOnDevice()) {
10021 throw new ServiceSpecificException(ImsException.CODE_ERROR_UNSUPPORTED_OPERATION,
10022 "IMS not available on device.");
10023 }
10024
10025 final long identity = Binder.clearCallingIdentity();
allenwtsu46dcc572020-01-08 18:24:03 +080010026 try {
Hui Wang761a6682020-10-31 05:12:53 +000010027 RcsProvisioningMonitor.getInstance().updateConfig(subId, config, isCompressed);
10028 } finally {
10029 Binder.restoreCallingIdentity(identity);
allenwtsu46dcc572020-01-08 18:24:03 +080010030 }
10031 }
zoey chene02881a2019-12-30 16:11:23 +080010032
10033 @Override
10034 public boolean isIccLockEnabled(int subId) {
10035 enforceReadPrivilegedPermission("isIccLockEnabled");
10036
10037 // Now that all security checks passes, perform the operation as ourselves.
10038 final long identity = Binder.clearCallingIdentity();
10039 try {
10040 Phone phone = getPhone(subId);
10041 if (phone != null && phone.getIccCard() != null) {
10042 return phone.getIccCard().getIccLockEnabled();
10043 } else {
10044 return false;
10045 }
10046 } finally {
10047 Binder.restoreCallingIdentity(identity);
10048 }
10049 }
10050
10051 /**
10052 * Set the ICC pin lock enabled or disabled.
10053 *
10054 * @return an integer representing the status of IccLock enabled or disabled in the following
10055 * three cases:
10056 * - {@link TelephonyManager#CHANGE_ICC_LOCK_SUCCESS} if enabled or disabled IccLock
10057 * successfully.
10058 * - Positive number and zero for remaining password attempts.
10059 * - Negative number for other failure cases (such like enabling/disabling PIN failed).
10060 *
10061 */
10062 @Override
10063 public int setIccLockEnabled(int subId, boolean enabled, String password) {
10064 enforceModifyPermission();
10065
10066 Phone phone = getPhone(subId);
10067 if (phone == null) {
10068 return 0;
10069 }
10070 // Now that all security checks passes, perform the operation as ourselves.
10071 final long identity = Binder.clearCallingIdentity();
10072 try {
10073 int attemptsRemaining = (int) sendRequest(CMD_SET_ICC_LOCK_ENABLED,
10074 new Pair<Boolean, String>(enabled, password), phone, null);
10075 return attemptsRemaining;
10076
10077 } catch (Exception e) {
10078 Log.e(LOG_TAG, "setIccLockEnabled. Exception e =" + e);
10079 } finally {
10080 Binder.restoreCallingIdentity(identity);
10081 }
10082 return 0;
10083 }
10084
10085 /**
10086 * Change the ICC password used in ICC pin lock.
10087 *
10088 * @return an integer representing the status of IccLock changed in the following three cases:
10089 * - {@link TelephonyManager#CHANGE_ICC_LOCK_SUCCESS} if changed IccLock successfully.
10090 * - Positive number and zero for remaining password attempts.
10091 * - Negative number for other failure cases (such like enabling/disabling PIN failed).
10092 *
10093 */
10094 @Override
10095 public int changeIccLockPassword(int subId, String oldPassword, String newPassword) {
10096 enforceModifyPermission();
10097
10098 Phone phone = getPhone(subId);
10099 if (phone == null) {
10100 return 0;
10101 }
10102 // Now that all security checks passes, perform the operation as ourselves.
10103 final long identity = Binder.clearCallingIdentity();
10104 try {
10105 int attemptsRemaining = (int) sendRequest(CMD_CHANGE_ICC_LOCK_PASSWORD,
10106 new Pair<String, String>(oldPassword, newPassword), phone, null);
10107 return attemptsRemaining;
10108
10109 } catch (Exception e) {
10110 Log.e(LOG_TAG, "changeIccLockPassword. Exception e =" + e);
10111 } finally {
10112 Binder.restoreCallingIdentity(identity);
10113 }
10114 return 0;
10115 }
Peter Wangdafb9ac2020-01-15 14:13:38 -080010116
10117 /**
10118 * Request for receiving user activity notification
10119 */
10120 @Override
10121 public void requestUserActivityNotification() {
10122 if (!mNotifyUserActivity.get()
10123 && !mMainThreadHandler.hasMessages(MSG_NOTIFY_USER_ACTIVITY)) {
10124 mNotifyUserActivity.set(true);
10125 }
10126 }
10127
10128 /**
10129 * Called when userActivity is signalled in the power manager.
10130 * This is safe to call from any thread, with any window manager locks held or not.
10131 */
10132 @Override
10133 public void userActivity() {
10134 // ***************************************
10135 // * Inherited from PhoneWindowManager *
10136 // ***************************************
10137 // THIS IS CALLED FROM DEEP IN THE POWER MANAGER
10138 // WITH ITS LOCKS HELD.
10139 //
10140 // This code must be VERY careful about the locks
10141 // it acquires.
10142 // In fact, the current code acquires way too many,
10143 // and probably has lurking deadlocks.
10144
10145 if (Binder.getCallingUid() != Process.SYSTEM_UID) {
10146 throw new SecurityException("Only the OS may call notifyUserActivity()");
10147 }
10148
10149 if (mNotifyUserActivity.getAndSet(false)) {
10150 mMainThreadHandler.sendEmptyMessageDelayed(MSG_NOTIFY_USER_ACTIVITY,
10151 USER_ACTIVITY_NOTIFICATION_DELAY);
10152 }
10153 }
Malcolm Chen4639c562020-04-13 11:59:40 -070010154
10155 @Override
10156 public boolean canConnectTo5GInDsdsMode() {
10157 return mApp.getResources().getBoolean(R.bool.config_5g_connection_in_dsds_mode);
10158 }
Jack Yud10cdd42020-09-28 20:28:01 -070010159
10160 @Override
10161 public @NonNull List<String> getEquivalentHomePlmns(int subId, String callingPackage,
10162 String callingFeatureId) {
10163 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
10164 mApp, subId, callingPackage, callingFeatureId, "getEquivalentHomePlmns")) {
10165 throw new SecurityException("Requires READ_PHONE_STATE permission.");
10166 }
10167
10168 Phone phone = getPhone(subId);
10169 if (phone == null) {
10170 throw new RuntimeException("phone is not available");
10171 }
10172 // Now that all security checks passes, perform the operation as ourselves.
10173 final long identity = Binder.clearCallingIdentity();
10174 try {
10175 return phone.getEquivalentHomePlmns();
10176 } finally {
10177 Binder.restoreCallingIdentity(identity);
10178 }
10179 }
Daniel Bright59e67312020-11-13 11:49:37 -080010180
10181 @Override
10182 public boolean isRadioInterfaceCapabilitySupported(
Daniel Bright95a4c1f2021-02-11 09:57:16 -080010183 final @NonNull @TelephonyManager.RadioInterfaceCapability String capability) {
10184 Set<String> radioInterfaceCapabilities =
Daniel Bright94f43662021-03-01 14:43:40 -080010185 mRadioInterfaceCapabilities.getCapabilities();
Daniel Bright59e67312020-11-13 11:49:37 -080010186 if (radioInterfaceCapabilities == null) {
10187 throw new RuntimeException("radio interface capabilities are not available");
Daniel Bright59e67312020-11-13 11:49:37 -080010188 }
Daniel Bright95a4c1f2021-02-11 09:57:16 -080010189 return radioInterfaceCapabilities.contains(capability);
Daniel Bright59e67312020-11-13 11:49:37 -080010190 }
Jack Nudelmanb0b87642020-11-12 15:04:39 -080010191
Hui Wang641e81c2020-10-12 12:14:23 -070010192 @Override
10193 public void bootstrapAuthenticationRequest(int subId, int appType, Uri nafUrl,
10194 UaSecurityProtocolIdentifier securityProtocol,
Brad Ebinger34c09a52021-02-17 23:23:21 +000010195 boolean forceBootStrapping, IBootstrapAuthenticationCallback callback) {
10196 TelephonyPermissions.enforceAnyPermissionGrantedOrCarrierPrivileges(mApp, subId,
10197 Binder.getCallingUid(), "bootstrapAuthenticationRequest",
10198 Manifest.permission.PERFORM_IMS_SINGLE_REGISTRATION,
10199 Manifest.permission.MODIFY_PHONE_STATE);
Hui Wang641e81c2020-10-12 12:14:23 -070010200 if (DBG) {
10201 log("bootstrapAuthenticationRequest, subId:" + subId + ", appType:"
10202 + appType + ", NAF:" + nafUrl + ", sp:" + securityProtocol
10203 + ", forceBootStrapping:" + forceBootStrapping + ", callback:" + callback);
10204 }
10205
10206 if (!SubscriptionManager.isValidSubscriptionId(subId)
10207 || appType < TelephonyManager.APPTYPE_UNKNOWN
10208 || appType > TelephonyManager.APPTYPE_ISIM
10209 || nafUrl == null || securityProtocol == null || callback == null) {
10210 Log.d(LOG_TAG, "bootstrapAuthenticationRequest failed due to invalid parameters");
10211 if (callback != null) {
10212 try {
10213 callback.onAuthenticationFailure(
10214 0, TelephonyManager.GBA_FAILURE_REASON_FEATURE_NOT_SUPPORTED);
10215 } catch (RemoteException exception) {
10216 log("Fail to notify onAuthenticationFailure due to " + exception);
10217 }
10218 return;
10219 }
10220 }
10221
10222 final long token = Binder.clearCallingIdentity();
10223 try {
10224 getGbaManager(subId).bootstrapAuthenticationRequest(
10225 new GbaAuthRequest(subId, appType, nafUrl, securityProtocol.toByteArray(),
10226 forceBootStrapping, callback));
10227 } finally {
10228 Binder.restoreCallingIdentity(token);
10229 }
10230 }
10231
Jack Nudelmanb0b87642020-11-12 15:04:39 -080010232 /**
Sooraj Sasindran72cff492021-07-29 09:42:42 -070010233 * Attempts to set the radio power state for all phones for thermal reason.
10234 * This does not guarantee that the
Jack Nudelmanb0b87642020-11-12 15:04:39 -080010235 * requested radio power state will actually be set. See {@link
10236 * PhoneInternalInterface#setRadioPowerForReason} for more details.
10237 *
Jack Nudelmanb0b87642020-11-12 15:04:39 -080010238 * @param enable {@code true} if trying to turn radio on.
10239 * @return {@code true} if phone setRadioPowerForReason was called. Otherwise, returns {@code
10240 * false}.
10241 */
Sooraj Sasindran72cff492021-07-29 09:42:42 -070010242 private boolean setRadioPowerForThermal(boolean enable) {
10243 boolean isPhoneAvailable = false;
10244 for (int i = 0; i < TelephonyManager.getDefault().getActiveModemCount(); i++) {
10245 Phone phone = PhoneFactory.getPhone(i);
10246 if (phone != null) {
Thomas Nguyenfd0572f2022-07-15 22:28:49 +000010247 phone.setRadioPowerForReason(enable, TelephonyManager.RADIO_POWER_REASON_THERMAL);
Sooraj Sasindran72cff492021-07-29 09:42:42 -070010248 isPhoneAvailable = true;
10249 }
Jack Nudelmanb0b87642020-11-12 15:04:39 -080010250 }
Sooraj Sasindran72cff492021-07-29 09:42:42 -070010251
10252 // return true if successfully informed the phone object about the thermal radio power
10253 // request.
10254 return isPhoneAvailable;
Jack Nudelmanb0b87642020-11-12 15:04:39 -080010255 }
10256
10257 private int handleDataThrottlingRequest(int subId,
Sarah Chinecc78c42022-03-31 21:16:48 -070010258 DataThrottlingRequest dataThrottlingRequest, String callingPackage) {
Jack Nudelman5d6a98b2021-03-04 14:26:25 -080010259 boolean isDataThrottlingSupported = isRadioInterfaceCapabilitySupported(
10260 TelephonyManager.CAPABILITY_THERMAL_MITIGATION_DATA_THROTTLING);
10261 if (!isDataThrottlingSupported && dataThrottlingRequest.getDataThrottlingAction()
10262 != DataThrottlingRequest.DATA_THROTTLING_ACTION_NO_DATA_THROTTLING) {
10263 throw new IllegalArgumentException("modem does not support data throttling");
10264 }
10265
Jack Nudelmanb0b87642020-11-12 15:04:39 -080010266 // Ensure that radio is on. If not able to power on due to phone being unavailable, return
10267 // THERMAL_MITIGATION_RESULT_MODEM_NOT_AVAILABLE.
Sooraj Sasindran72cff492021-07-29 09:42:42 -070010268 if (!setRadioPowerForThermal(true)) {
Jack Nudelmanb0b87642020-11-12 15:04:39 -080010269 return TelephonyManager.THERMAL_MITIGATION_RESULT_MODEM_NOT_AVAILABLE;
10270 }
10271
Sarah Chinecc78c42022-03-31 21:16:48 -070010272 setDataEnabledForReason(
10273 subId, TelephonyManager.DATA_ENABLED_REASON_THERMAL, true, callingPackage);
Jack Nudelmanb0b87642020-11-12 15:04:39 -080010274
Jack Nudelman5d6a98b2021-03-04 14:26:25 -080010275 if (isDataThrottlingSupported) {
10276 int thermalMitigationResult =
Jack Nudelmanb0b87642020-11-12 15:04:39 -080010277 (int) sendRequest(CMD_SET_DATA_THROTTLING, dataThrottlingRequest, subId);
Jack Nudelman5d6a98b2021-03-04 14:26:25 -080010278 if (thermalMitigationResult == SET_DATA_THROTTLING_MODEM_THREW_INVALID_PARAMS) {
10279 throw new IllegalArgumentException("modem returned INVALID_ARGUMENTS");
10280 } else if (thermalMitigationResult
10281 == MODEM_DOES_NOT_SUPPORT_DATA_THROTTLING_ERROR_CODE) {
Jack Nudelman760d0962021-05-20 13:57:30 -070010282 log("Modem likely does not support data throttling on secondary carrier. Data " +
10283 "throttling action = " + dataThrottlingRequest.getDataThrottlingAction());
10284 return TelephonyManager.THERMAL_MITIGATION_RESULT_MODEM_ERROR;
Jack Nudelman5d6a98b2021-03-04 14:26:25 -080010285 }
10286 return thermalMitigationResult;
Jack Nudelmanb0b87642020-11-12 15:04:39 -080010287 }
Jack Nudelman5d6a98b2021-03-04 14:26:25 -080010288
10289 return TelephonyManager.THERMAL_MITIGATION_RESULT_SUCCESS;
Jack Nudelmanb0b87642020-11-12 15:04:39 -080010290 }
10291
Jack Nudelman644b91a2021-03-12 14:09:48 -080010292 private static List<String> getThermalMitigationAllowlist(Context context) {
10293 if (sThermalMitigationAllowlistedPackages.isEmpty()) {
10294 for (String pckg : context.getResources()
10295 .getStringArray(R.array.thermal_mitigation_allowlisted_packages)) {
10296 sThermalMitigationAllowlistedPackages.add(pckg);
10297 }
10298 }
10299
10300 return sThermalMitigationAllowlistedPackages;
10301 }
10302
Jack Nudelmane69bbc82021-05-13 10:00:15 -070010303 private boolean isAnyPhoneInEmergencyState() {
10304 TelecomManager tm = mApp.getSystemService(TelecomManager.class);
10305 if (tm.isInEmergencyCall()) {
10306 Log.e(LOG_TAG , "Phone state is not valid. One of the phones is in an emergency call");
10307 return true;
10308 }
10309 for (Phone phone : PhoneFactory.getPhones()) {
10310 if (phone.isInEmergencySmsMode() || phone.isInEcm()) {
10311 Log.e(LOG_TAG, "Phone state is not valid. isInEmergencySmsMode = "
10312 + phone.isInEmergencySmsMode() + " isInEmergencyCallbackMode = "
10313 + phone.isInEcm());
10314 return true;
10315 }
10316 }
10317
10318 return false;
10319 }
10320
Jack Nudelman644b91a2021-03-12 14:09:48 -080010321 /**
10322 * Used by shell commands to add an authorized package name for thermal mitigation.
10323 * @param packageName name of package to be allowlisted
10324 * @param context
10325 */
10326 static void addPackageToThermalMitigationAllowlist(String packageName, Context context) {
10327 sThermalMitigationAllowlistedPackages = getThermalMitigationAllowlist(context);
10328 sThermalMitigationAllowlistedPackages.add(packageName);
10329 }
10330
10331 /**
10332 * Used by shell commands to remove an authorized package name for thermal mitigation.
10333 * @param packageName name of package to remove from allowlist
10334 * @param context
10335 */
10336 static void removePackageFromThermalMitigationAllowlist(String packageName, Context context) {
10337 sThermalMitigationAllowlistedPackages = getThermalMitigationAllowlist(context);
10338 sThermalMitigationAllowlistedPackages.remove(packageName);
10339 }
10340
Jack Nudelmanb0b87642020-11-12 15:04:39 -080010341 /**
10342 * Thermal mitigation request to control functionalities at modem.
10343 *
10344 * @param subId the id of the subscription.
10345 * @param thermalMitigationRequest holds all necessary information to be passed down to modem.
Jack Nudelman644b91a2021-03-12 14:09:48 -080010346 * @param callingPackage the package name of the calling package.
Jack Nudelmanb0b87642020-11-12 15:04:39 -080010347 *
10348 * @return thermalMitigationResult enum as defined in android.telephony.Annotation.
10349 */
10350 @Override
10351 @ThermalMitigationResult
10352 public int sendThermalMitigationRequest(
10353 int subId,
Jack Nudelman644b91a2021-03-12 14:09:48 -080010354 ThermalMitigationRequest thermalMitigationRequest,
10355 String callingPackage) throws IllegalArgumentException {
Jack Nudelmanb0b87642020-11-12 15:04:39 -080010356 enforceModifyPermission();
10357
Jack Nudelman644b91a2021-03-12 14:09:48 -080010358 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
10359 if (!getThermalMitigationAllowlist(getDefaultPhone().getContext())
10360 .contains(callingPackage)) {
10361 throw new SecurityException("Calling package must be configured in the device config. "
10362 + "calling package: " + callingPackage);
10363 }
10364
Jack Nudelmanb0b87642020-11-12 15:04:39 -080010365 WorkSource workSource = getWorkSource(Binder.getCallingUid());
10366 final long identity = Binder.clearCallingIdentity();
10367
10368 int thermalMitigationResult = TelephonyManager.THERMAL_MITIGATION_RESULT_UNKNOWN_ERROR;
10369 try {
10370 int thermalMitigationAction = thermalMitigationRequest.getThermalMitigationAction();
10371 switch (thermalMitigationAction) {
10372 case ThermalMitigationRequest.THERMAL_MITIGATION_ACTION_DATA_THROTTLING:
10373 thermalMitigationResult =
10374 handleDataThrottlingRequest(subId,
Sarah Chinecc78c42022-03-31 21:16:48 -070010375 thermalMitigationRequest.getDataThrottlingRequest(),
10376 callingPackage);
Jack Nudelmanb0b87642020-11-12 15:04:39 -080010377 break;
10378 case ThermalMitigationRequest.THERMAL_MITIGATION_ACTION_VOICE_ONLY:
10379 if (thermalMitigationRequest.getDataThrottlingRequest() != null) {
10380 throw new IllegalArgumentException("dataThrottlingRequest must be null for "
10381 + "ThermalMitigationRequest.THERMAL_MITIGATION_ACTION_VOICE_ONLY");
10382 }
10383
10384 // Ensure that radio is on. If not able to power on due to phone being
10385 // unavailable, return THERMAL_MITIGATION_RESULT_MODEM_NOT_AVAILABLE.
Sooraj Sasindran72cff492021-07-29 09:42:42 -070010386 if (!setRadioPowerForThermal(true)) {
Jack Nudelmanb0b87642020-11-12 15:04:39 -080010387 thermalMitigationResult =
10388 TelephonyManager.THERMAL_MITIGATION_RESULT_MODEM_NOT_AVAILABLE;
10389 break;
10390 }
10391
10392 setDataEnabledForReason(subId, TelephonyManager.DATA_ENABLED_REASON_THERMAL,
Sarah Chinecc78c42022-03-31 21:16:48 -070010393 false, callingPackage);
Jack Nudelmanb0b87642020-11-12 15:04:39 -080010394 thermalMitigationResult = TelephonyManager.THERMAL_MITIGATION_RESULT_SUCCESS;
10395 break;
10396 case ThermalMitigationRequest.THERMAL_MITIGATION_ACTION_RADIO_OFF:
10397 if (thermalMitigationRequest.getDataThrottlingRequest() != null) {
10398 throw new IllegalArgumentException("dataThrottlingRequest must be null for"
10399 + " ThermalMitigationRequest.THERMAL_MITIGATION_ACTION_RADIO_OFF");
10400 }
10401
10402 TelecomAccountRegistry registry = TelecomAccountRegistry.getInstance(null);
10403 if (registry != null) {
Jack Nudelmanb0b87642020-11-12 15:04:39 -080010404 Phone phone = getPhone(subId);
10405 if (phone == null) {
10406 thermalMitigationResult =
10407 TelephonyManager.THERMAL_MITIGATION_RESULT_MODEM_NOT_AVAILABLE;
10408 break;
10409 }
10410
Jack Nudelmane69bbc82021-05-13 10:00:15 -070010411 TelephonyConnectionService service =
10412 registry.getTelephonyConnectionService();
Jack Nudelmanb30ac302021-06-17 15:39:58 -070010413 if (service != null && service.isEmergencyCallPending()) {
Jack Nudelmane69bbc82021-05-13 10:00:15 -070010414 Log.e(LOG_TAG, "An emergency call is pending");
10415 thermalMitigationResult =
10416 TelephonyManager.THERMAL_MITIGATION_RESULT_INVALID_STATE;
10417 break;
10418 } else if (isAnyPhoneInEmergencyState()) {
Jack Nudelmanb0b87642020-11-12 15:04:39 -080010419 thermalMitigationResult =
10420 TelephonyManager.THERMAL_MITIGATION_RESULT_INVALID_STATE;
10421 break;
10422 }
10423 } else {
10424 thermalMitigationResult =
10425 TelephonyManager.THERMAL_MITIGATION_RESULT_MODEM_NOT_AVAILABLE;
10426 break;
10427 }
10428
10429 // Turn radio off. If not able to power off due to phone being unavailable,
10430 // return THERMAL_MITIGATION_RESULT_MODEM_NOT_AVAILABLE.
Sooraj Sasindran72cff492021-07-29 09:42:42 -070010431 if (!setRadioPowerForThermal(false)) {
Jack Nudelmanb0b87642020-11-12 15:04:39 -080010432 thermalMitigationResult =
10433 TelephonyManager.THERMAL_MITIGATION_RESULT_MODEM_NOT_AVAILABLE;
10434 break;
10435 }
10436 thermalMitigationResult =
10437 TelephonyManager.THERMAL_MITIGATION_RESULT_SUCCESS;
10438 break;
10439 default:
10440 throw new IllegalArgumentException("the requested thermalMitigationAction does "
10441 + "not exist. Requested action: " + thermalMitigationAction);
10442 }
10443 } catch (IllegalArgumentException e) {
10444 throw e;
10445 } catch (Exception e) {
10446 Log.e(LOG_TAG, "thermalMitigationRequest. Exception e =" + e);
10447 thermalMitigationResult = TelephonyManager.THERMAL_MITIGATION_RESULT_MODEM_ERROR;
10448 } finally {
10449 Binder.restoreCallingIdentity(identity);
10450 }
10451
10452 if (DBG) {
10453 log("thermalMitigationRequest returning with thermalMitigationResult: "
10454 + thermalMitigationResult);
10455 }
10456
10457 return thermalMitigationResult;
10458 }
Hui Wang641e81c2020-10-12 12:14:23 -070010459
10460 /**
10461 * Set the GbaService Package Name that Telephony will bind to.
10462 *
10463 * @param subId The sim that the GbaService is associated with.
10464 * @param packageName The name of the package to be replaced with.
10465 * @return true if setting the GbaService to bind to succeeded, false if it did not.
10466 */
10467 @Override
10468 public boolean setBoundGbaServiceOverride(int subId, String packageName) {
10469 enforceModifyPermission();
10470
10471 final long identity = Binder.clearCallingIdentity();
10472 try {
10473 return getGbaManager(subId).overrideServicePackage(packageName);
10474 } finally {
10475 Binder.restoreCallingIdentity(identity);
10476 }
10477 }
10478
10479 /**
10480 * Return the package name of the currently bound GbaService.
10481 *
10482 * @param subId The sim that the GbaService is associated with.
10483 * @return the package name of the GbaService configuration, null if GBA is not supported.
10484 */
10485 @Override
10486 public String getBoundGbaService(int subId) {
10487 enforceReadPrivilegedPermission("getBoundGbaServicePackage");
10488
10489 final long identity = Binder.clearCallingIdentity();
10490 try {
10491 return getGbaManager(subId).getServicePackage();
10492 } finally {
10493 Binder.restoreCallingIdentity(identity);
10494 }
10495 }
10496
10497 /**
10498 * Set the release time for telephony to unbind GbaService.
10499 *
10500 * @param subId The sim that the GbaService is associated with.
10501 * @param interval The release time to unbind GbaService by millisecond.
10502 * @return true if setting the GbaService to bind to succeeded, false if it did not.
10503 */
10504 @Override
10505 public boolean setGbaReleaseTimeOverride(int subId, int interval) {
10506 enforceModifyPermission();
10507
10508 final long identity = Binder.clearCallingIdentity();
10509 try {
10510 return getGbaManager(subId).overrideReleaseTime(interval);
10511 } finally {
10512 Binder.restoreCallingIdentity(identity);
10513 }
10514 }
10515
10516 /**
10517 * Return the release time for telephony to unbind GbaService.
10518 *
10519 * @param subId The sim that the GbaService is associated with.
10520 * @return The release time to unbind GbaService by millisecond.
10521 */
10522 @Override
10523 public int getGbaReleaseTime(int subId) {
10524 enforceReadPrivilegedPermission("getGbaReleaseTime");
10525
10526 final long identity = Binder.clearCallingIdentity();
10527 try {
10528 return getGbaManager(subId).getReleaseTime();
10529 } finally {
10530 Binder.restoreCallingIdentity(identity);
10531 }
10532 }
10533
10534 private GbaManager getGbaManager(int subId) {
10535 GbaManager instance = GbaManager.getInstance(subId);
10536 if (instance == null) {
10537 String packageName = mApp.getResources().getString(R.string.config_gba_package);
10538 int releaseTime = mApp.getResources().getInteger(R.integer.config_gba_release_time);
10539 instance = GbaManager.make(mApp, subId, packageName, releaseTime);
10540 }
10541 return instance;
10542 }
Hui Wang761a6682020-10-31 05:12:53 +000010543
10544 /**
10545 * indicate whether the device and the carrier can support
10546 * RCS VoLTE single registration.
10547 */
10548 @Override
10549 public boolean isRcsVolteSingleRegistrationCapable(int subId) {
Brad Ebinger34c09a52021-02-17 23:23:21 +000010550 TelephonyPermissions.enforceAnyPermissionGrantedOrCarrierPrivileges(mApp, subId,
10551 Binder.getCallingUid(), "isRcsVolteSingleRegistrationCapable",
10552 Manifest.permission.PERFORM_IMS_SINGLE_REGISTRATION,
10553 permission.READ_PRIVILEGED_PHONE_STATE);
Hui Wang761a6682020-10-31 05:12:53 +000010554
10555 if (!SubscriptionManager.isValidSubscriptionId(subId)) {
10556 throw new IllegalArgumentException("Invalid Subscription ID: " + subId);
10557 }
10558
10559 final long identity = Binder.clearCallingIdentity();
10560 try {
10561 RcsProvisioningMonitor rpm = RcsProvisioningMonitor.getInstance();
10562 if (rpm != null) {
Hui Wang67af90e2021-06-04 16:57:15 -070010563 Boolean isCapable = rpm.isRcsVolteSingleRegistrationEnabled(subId);
10564 if (isCapable != null) {
10565 return isCapable;
10566 }
Hui Wang761a6682020-10-31 05:12:53 +000010567 }
Hui Wang67af90e2021-06-04 16:57:15 -070010568 throw new ServiceSpecificException(ImsException.CODE_ERROR_SERVICE_UNAVAILABLE,
10569 "service is temporarily unavailable.");
Hui Wang761a6682020-10-31 05:12:53 +000010570 } finally {
10571 Binder.restoreCallingIdentity(identity);
10572 }
10573 }
10574
10575 /**
10576 * Register RCS provisioning callback.
10577 */
10578 @Override
Hui Wang3cac7e52021-01-27 14:45:25 -080010579 public void registerRcsProvisioningCallback(int subId,
Hui Wang761a6682020-10-31 05:12:53 +000010580 IRcsConfigCallback callback) {
Brad Ebinger34c09a52021-02-17 23:23:21 +000010581 TelephonyPermissions.enforceAnyPermissionGrantedOrCarrierPrivileges(mApp, subId,
Hui Wang3cac7e52021-01-27 14:45:25 -080010582 Binder.getCallingUid(), "registerRcsProvisioningCallback",
Brad Ebinger34c09a52021-02-17 23:23:21 +000010583 Manifest.permission.PERFORM_IMS_SINGLE_REGISTRATION,
10584 permission.READ_PRIVILEGED_PHONE_STATE);
Hui Wang761a6682020-10-31 05:12:53 +000010585
10586 if (!SubscriptionManager.isValidSubscriptionId(subId)) {
10587 throw new IllegalArgumentException("Invalid Subscription ID: " + subId);
10588 }
10589 if (!isImsAvailableOnDevice()) {
10590 throw new ServiceSpecificException(ImsException.CODE_ERROR_UNSUPPORTED_OPERATION,
10591 "IMS not available on device.");
10592 }
10593
10594 final long identity = Binder.clearCallingIdentity();
10595 try {
Hui Wang68cd3722021-01-11 20:04:53 -080010596 if (!RcsProvisioningMonitor.getInstance()
Hui Wang3cac7e52021-01-27 14:45:25 -080010597 .registerRcsProvisioningCallback(subId, callback)) {
Brad Ebinger919631e2021-06-02 17:46:35 -070010598 throw new ServiceSpecificException(ImsException.CODE_ERROR_INVALID_SUBSCRIPTION,
10599 "Active subscription not found.");
Hui Wang68cd3722021-01-11 20:04:53 -080010600 }
Hui Wang761a6682020-10-31 05:12:53 +000010601 } finally {
10602 Binder.restoreCallingIdentity(identity);
10603 }
10604 }
10605
10606 /**
10607 * Unregister RCS provisioning callback.
10608 */
10609 @Override
Hui Wang3cac7e52021-01-27 14:45:25 -080010610 public void unregisterRcsProvisioningCallback(int subId,
Hui Wang761a6682020-10-31 05:12:53 +000010611 IRcsConfigCallback callback) {
Brad Ebinger34c09a52021-02-17 23:23:21 +000010612 TelephonyPermissions.enforceAnyPermissionGrantedOrCarrierPrivileges(mApp, subId,
Hui Wang3cac7e52021-01-27 14:45:25 -080010613 Binder.getCallingUid(), "unregisterRcsProvisioningCallback",
Brad Ebinger34c09a52021-02-17 23:23:21 +000010614 Manifest.permission.PERFORM_IMS_SINGLE_REGISTRATION,
10615 permission.READ_PRIVILEGED_PHONE_STATE);
Hui Wang761a6682020-10-31 05:12:53 +000010616
10617 if (!SubscriptionManager.isValidSubscriptionId(subId)) {
10618 throw new IllegalArgumentException("Invalid Subscription ID: " + subId);
10619 }
10620 if (!isImsAvailableOnDevice()) {
10621 throw new ServiceSpecificException(ImsException.CODE_ERROR_UNSUPPORTED_OPERATION,
10622 "IMS not available on device.");
10623 }
10624
10625 final long identity = Binder.clearCallingIdentity();
10626 try {
Hui Wang68cd3722021-01-11 20:04:53 -080010627 RcsProvisioningMonitor.getInstance()
Hui Wang3cac7e52021-01-27 14:45:25 -080010628 .unregisterRcsProvisioningCallback(subId, callback);
Hui Wang761a6682020-10-31 05:12:53 +000010629 } finally {
10630 Binder.restoreCallingIdentity(identity);
10631 }
10632 }
10633
10634 /**
10635 * trigger RCS reconfiguration.
10636 */
10637 public void triggerRcsReconfiguration(int subId) {
Brad Ebinger34c09a52021-02-17 23:23:21 +000010638 TelephonyPermissions.enforceAnyPermissionGranted(mApp, Binder.getCallingUid(),
10639 "triggerRcsReconfiguration",
10640 Manifest.permission.PERFORM_IMS_SINGLE_REGISTRATION);
Hui Wang761a6682020-10-31 05:12:53 +000010641
10642 if (!SubscriptionManager.isValidSubscriptionId(subId)) {
10643 throw new IllegalArgumentException("Invalid Subscription ID: " + subId);
10644 }
10645 if (!isImsAvailableOnDevice()) {
10646 throw new ServiceSpecificException(ImsException.CODE_ERROR_UNSUPPORTED_OPERATION,
10647 "IMS not available on device.");
10648 }
10649
10650 final long identity = Binder.clearCallingIdentity();
10651 try {
10652 RcsProvisioningMonitor.getInstance().requestReconfig(subId);
10653 } finally {
10654 Binder.restoreCallingIdentity(identity);
10655 }
10656 }
10657
10658 /**
10659 * Provide the client configuration parameters of the RCS application.
10660 */
10661 public void setRcsClientConfiguration(int subId, RcsClientConfiguration rcc) {
Brad Ebinger34c09a52021-02-17 23:23:21 +000010662 TelephonyPermissions.enforceAnyPermissionGranted(mApp, Binder.getCallingUid(),
10663 "setRcsClientConfiguration",
10664 Manifest.permission.PERFORM_IMS_SINGLE_REGISTRATION);
Hui Wang761a6682020-10-31 05:12:53 +000010665
10666 if (!SubscriptionManager.isValidSubscriptionId(subId)) {
10667 throw new IllegalArgumentException("Invalid Subscription ID: " + subId);
10668 }
10669 if (!isImsAvailableOnDevice()) {
10670 throw new ServiceSpecificException(ImsException.CODE_ERROR_UNSUPPORTED_OPERATION,
10671 "IMS not available on device.");
10672 }
10673
10674 final long identity = Binder.clearCallingIdentity();
10675
10676 try {
10677 IImsConfig configBinder = getImsConfig(getSlotIndex(subId), ImsFeature.FEATURE_RCS);
10678 if (configBinder == null) {
10679 Rlog.e(LOG_TAG, "null result for setRcsClientConfiguration");
Brad Ebinger919631e2021-06-02 17:46:35 -070010680 throw new ServiceSpecificException(ImsException.CODE_ERROR_INVALID_SUBSCRIPTION,
10681 "could not find the requested subscription");
Hui Wang761a6682020-10-31 05:12:53 +000010682 } else {
10683 configBinder.setRcsClientConfiguration(rcc);
10684 }
joonhunshin3e154242021-09-17 06:33:39 +000010685
10686 RcsStats.getInstance().onRcsClientProvisioningStats(subId,
10687 RCS_CLIENT_PROVISIONING_STATS__EVENT__CLIENT_PARAMS_SENT);
Hui Wang761a6682020-10-31 05:12:53 +000010688 } catch (RemoteException e) {
10689 Rlog.e(LOG_TAG, "fail to setRcsClientConfiguration " + e.getMessage());
Brad Ebinger919631e2021-06-02 17:46:35 -070010690 throw new ServiceSpecificException(ImsException.CODE_ERROR_SERVICE_UNAVAILABLE,
10691 "service is temporarily unavailable.");
Hui Wang761a6682020-10-31 05:12:53 +000010692 } finally {
10693 Binder.restoreCallingIdentity(identity);
10694 }
10695 }
10696
10697 /**
Hui Wangbaaee6a2021-02-19 20:45:36 -080010698 * Enables or disables the test mode for RCS VoLTE single registration.
10699 */
10700 @Override
10701 public void setRcsSingleRegistrationTestModeEnabled(boolean enabled) {
10702 TelephonyPermissions.enforceShellOnly(Binder.getCallingUid(),
10703 "setRcsSingleRegistrationTestModeEnabled");
10704
10705 RcsProvisioningMonitor.getInstance().setTestModeEnabled(enabled);
10706 }
10707
10708 /**
10709 * Gets the test mode for RCS VoLTE single registration.
10710 */
10711 @Override
10712 public boolean getRcsSingleRegistrationTestModeEnabled() {
10713 TelephonyPermissions.enforceShellOnly(Binder.getCallingUid(),
10714 "getRcsSingleRegistrationTestModeEnabled");
10715
10716 return RcsProvisioningMonitor.getInstance().getTestModeEnabled();
10717 }
10718
10719 /**
Hui Wang761a6682020-10-31 05:12:53 +000010720 * Overrides the config of RCS VoLTE single registration enabled for the device.
10721 */
10722 @Override
10723 public void setDeviceSingleRegistrationEnabledOverride(String enabledStr) {
10724 TelephonyPermissions.enforceShellOnly(Binder.getCallingUid(),
10725 "setDeviceSingleRegistrationEnabledOverride");
10726 enforceModifyPermission();
10727
10728 Boolean enabled = "NULL".equalsIgnoreCase(enabledStr) ? null
10729 : Boolean.parseBoolean(enabledStr);
10730 RcsProvisioningMonitor.getInstance().overrideDeviceSingleRegistrationEnabled(enabled);
Brad Ebinger49a72b42021-01-29 00:55:24 +000010731 mApp.imsRcsController.setDeviceSingleRegistrationSupportOverride(enabled);
Hui Wang761a6682020-10-31 05:12:53 +000010732 }
10733
10734 /**
Tyler Gunn92479152021-01-20 16:30:10 -080010735 * Sends a device to device communication message. Only usable via shell.
10736 * @param message message to send.
10737 * @param value message value.
10738 */
10739 @Override
10740 public void sendDeviceToDeviceMessage(int message, int value) {
10741 TelephonyPermissions.enforceShellOnly(Binder.getCallingUid(),
Tyler Gunnbabbda02021-02-10 11:05:02 -080010742 "sendDeviceToDeviceMessage");
Tyler Gunn92479152021-01-20 16:30:10 -080010743 enforceModifyPermission();
10744
10745 final long identity = Binder.clearCallingIdentity();
10746 try {
10747 TelephonyConnectionService service =
10748 TelecomAccountRegistry.getInstance(null).getTelephonyConnectionService();
10749 if (service == null) {
10750 Rlog.e(LOG_TAG, "sendDeviceToDeviceMessage: not in a call.");
10751 return;
10752 }
10753 service.sendTestDeviceToDeviceMessage(message, value);
10754 } finally {
10755 Binder.restoreCallingIdentity(identity);
10756 }
10757 }
10758
Tyler Gunnbabbda02021-02-10 11:05:02 -080010759 /**
10760 * Sets the specified device to device transport active.
10761 * @param transport The transport to set active.
10762 */
10763 @Override
10764 public void setActiveDeviceToDeviceTransport(@NonNull String transport) {
10765 TelephonyPermissions.enforceShellOnly(Binder.getCallingUid(),
10766 "setActiveDeviceToDeviceTransport");
10767 enforceModifyPermission();
10768
10769 final long identity = Binder.clearCallingIdentity();
10770 try {
10771 TelephonyConnectionService service =
10772 TelecomAccountRegistry.getInstance(null).getTelephonyConnectionService();
10773 if (service == null) {
10774 Rlog.e(LOG_TAG, "setActiveDeviceToDeviceTransport: not in a call.");
10775 return;
10776 }
10777 service.setActiveDeviceToDeviceTransport(transport);
10778 } finally {
10779 Binder.restoreCallingIdentity(identity);
10780 }
10781 }
Tyler Gunn92479152021-01-20 16:30:10 -080010782
Tyler Gunnd4339262021-05-03 14:46:49 -070010783 @Override
10784 public void setDeviceToDeviceForceEnabled(boolean isForceEnabled) {
10785 TelephonyPermissions.enforceShellOnly(Binder.getCallingUid(),
10786 "setDeviceToDeviceForceEnabled");
10787
10788 final long identity = Binder.clearCallingIdentity();
10789 try {
10790 Arrays.stream(PhoneFactory.getPhones()).forEach(
10791 p -> {
10792 Phone thePhone = p.getImsPhone();
10793 if (thePhone != null && thePhone instanceof ImsPhone) {
10794 ImsPhone imsPhone = (ImsPhone) thePhone;
10795 CallTracker tracker = imsPhone.getCallTracker();
10796 if (tracker != null && tracker instanceof ImsPhoneCallTracker) {
10797 ImsPhoneCallTracker imsPhoneCallTracker =
10798 (ImsPhoneCallTracker) tracker;
10799 imsPhoneCallTracker.setDeviceToDeviceForceEnabled(isForceEnabled);
10800 }
10801 }
10802 }
10803 );
10804 } finally {
10805 Binder.restoreCallingIdentity(identity);
10806 }
10807 }
10808
Tyler Gunn92479152021-01-20 16:30:10 -080010809 /**
Hui Wang761a6682020-10-31 05:12:53 +000010810 * Gets the config of RCS VoLTE single registration enabled for the device.
10811 */
10812 @Override
10813 public boolean getDeviceSingleRegistrationEnabled() {
10814 enforceReadPrivilegedPermission("getDeviceSingleRegistrationEnabled");
10815 return RcsProvisioningMonitor.getInstance().getDeviceSingleRegistrationEnabled();
10816 }
10817
10818 /**
10819 * Overrides the config of RCS VoLTE single registration enabled for the carrier/subscription.
10820 */
10821 @Override
10822 public boolean setCarrierSingleRegistrationEnabledOverride(int subId, String enabledStr) {
10823 TelephonyPermissions.enforceShellOnly(Binder.getCallingUid(),
10824 "setCarrierSingleRegistrationEnabledOverride");
10825 enforceModifyPermission();
10826
10827 Boolean enabled = "NULL".equalsIgnoreCase(enabledStr) ? null
10828 : Boolean.parseBoolean(enabledStr);
10829 return RcsProvisioningMonitor.getInstance().overrideCarrierSingleRegistrationEnabled(
10830 subId, enabled);
10831 }
10832
10833 /**
10834 * Gets the config of RCS VoLTE single registration enabled for the carrier/subscription.
10835 */
10836 @Override
10837 public boolean getCarrierSingleRegistrationEnabled(int subId) {
10838 enforceReadPrivilegedPermission("getCarrierSingleRegistrationEnabled");
10839 return RcsProvisioningMonitor.getInstance().getCarrierSingleRegistrationEnabled(subId);
10840 }
Chiachang Wangd6d34772020-12-22 11:38:27 +080010841
10842 /**
Hui Wangb647abe2021-02-26 09:33:38 -080010843 * Overrides the ims feature validation result
10844 */
10845 @Override
10846 public boolean setImsFeatureValidationOverride(int subId, String enabledStr) {
10847 TelephonyPermissions.enforceShellOnly(Binder.getCallingUid(),
10848 "setImsFeatureValidationOverride");
10849
10850 Boolean enabled = "NULL".equalsIgnoreCase(enabledStr) ? null
10851 : Boolean.parseBoolean(enabledStr);
10852 return RcsProvisioningMonitor.getInstance().overrideImsFeatureValidation(
10853 subId, enabled);
10854 }
10855
10856 /**
10857 * Gets the ims feature validation override value
10858 */
10859 @Override
10860 public boolean getImsFeatureValidationOverride(int subId) {
10861 TelephonyPermissions.enforceShellOnly(Binder.getCallingUid(),
10862 "getImsFeatureValidationOverride");
10863 return RcsProvisioningMonitor.getInstance().getImsFeatureValidationOverride(subId);
10864 }
10865
10866 /**
Chiachang Wangd6d34772020-12-22 11:38:27 +080010867 * Get the mobile provisioning url that is used to launch a browser to allow users to manage
10868 * their mobile plan.
10869 */
10870 @Override
10871 public String getMobileProvisioningUrl() {
10872 enforceReadPrivilegedPermission("getMobileProvisioningUrl");
10873 final long identity = Binder.clearCallingIdentity();
10874 try {
10875 return getDefaultPhone().getMobileProvisioningUrl();
10876 } finally {
10877 Binder.restoreCallingIdentity(identity);
10878 }
10879 }
Rambo Wanga5cc9b72021-01-07 10:51:54 -080010880
James.cf Linbcdf8b32021-01-14 16:44:13 +080010881 /**
calvinpane4a8a1d2021-01-25 13:51:18 +080010882 * Get the EAB contact from the EAB database.
10883 */
10884 @Override
10885 public String getContactFromEab(String contact) {
10886 TelephonyPermissions.enforceShellOnly(Binder.getCallingUid(), "getContactFromEab");
10887 enforceModifyPermission();
10888 final long identity = Binder.clearCallingIdentity();
10889 try {
10890 return EabUtil.getContactFromEab(getDefaultPhone().getContext(), contact);
10891 } finally {
10892 Binder.restoreCallingIdentity(identity);
10893 }
10894 }
10895
10896 /**
Calvin Pana1434322021-07-01 19:27:01 +080010897 * Get the EAB capability from the EAB database.
10898 */
10899 @Override
10900 public String getCapabilityFromEab(String contact) {
10901 TelephonyPermissions.enforceShellOnly(Binder.getCallingUid(), "getCapabilityFromEab");
10902 enforceModifyPermission();
10903 final long identity = Binder.clearCallingIdentity();
10904 try {
10905 return EabUtil.getCapabilityFromEab(getDefaultPhone().getContext(), contact);
10906 } finally {
10907 Binder.restoreCallingIdentity(identity);
10908 }
10909 }
10910
10911 /**
James.cf Linbcdf8b32021-01-14 16:44:13 +080010912 * Remove the EAB contacts from the EAB database.
10913 */
10914 @Override
10915 public int removeContactFromEab(int subId, String contacts) {
10916 TelephonyPermissions.enforceShellOnly(Binder.getCallingUid(), "removeCapabilitiesFromEab");
10917 enforceModifyPermission();
10918 final long identity = Binder.clearCallingIdentity();
10919 try {
10920 return EabUtil.removeContactFromEab(subId, contacts, getDefaultPhone().getContext());
10921 } finally {
10922 Binder.restoreCallingIdentity(identity);
10923 }
10924 }
10925
Rambo Wanga5cc9b72021-01-07 10:51:54 -080010926 @Override
James.cf Lin4b784aa2021-01-31 03:25:15 +080010927 public boolean getDeviceUceEnabled() {
10928 TelephonyPermissions.enforceShellOnly(Binder.getCallingUid(), "getDeviceUceEnabled");
10929 final long identity = Binder.clearCallingIdentity();
10930 try {
10931 return mApp.getDeviceUceEnabled();
10932 } finally {
10933 Binder.restoreCallingIdentity(identity);
10934 }
10935 }
10936
10937 @Override
10938 public void setDeviceUceEnabled(boolean isEnabled) {
10939 TelephonyPermissions.enforceShellOnly(Binder.getCallingUid(), "setDeviceUceEnabled");
10940 final long identity = Binder.clearCallingIdentity();
10941 try {
10942 mApp.setDeviceUceEnabled(isEnabled);
10943 } finally {
10944 Binder.restoreCallingIdentity(identity);
10945 }
10946 }
10947
Brad Ebinger14d467f2021-02-12 06:18:28 +000010948 /**
10949 * Add new feature tags to the Set used to calculate the capabilities in PUBLISH.
10950 * @return current RcsContactUceCapability instance that will be used for PUBLISH.
10951 */
10952 // Used for SHELL command only right now.
10953 @Override
10954 public RcsContactUceCapability addUceRegistrationOverrideShell(int subId,
10955 List<String> featureTags) {
10956 TelephonyPermissions.enforceShellOnly(Binder.getCallingUid(),
10957 "addUceRegistrationOverrideShell");
10958 final long identity = Binder.clearCallingIdentity();
10959 try {
10960 return mApp.imsRcsController.addUceRegistrationOverrideShell(subId,
10961 new ArraySet<>(featureTags));
10962 } catch (ImsException e) {
10963 throw new ServiceSpecificException(e.getCode(), e.getMessage());
10964 } finally {
10965 Binder.restoreCallingIdentity(identity);
10966 }
10967 }
10968
10969 /**
10970 * Remove existing feature tags to the Set used to calculate the capabilities in PUBLISH.
10971 * @return current RcsContactUceCapability instance that will be used for PUBLISH.
10972 */
10973 // Used for SHELL command only right now.
10974 @Override
10975 public RcsContactUceCapability removeUceRegistrationOverrideShell(int subId,
10976 List<String> featureTags) {
10977 TelephonyPermissions.enforceShellOnly(Binder.getCallingUid(),
10978 "removeUceRegistrationOverrideShell");
10979 final long identity = Binder.clearCallingIdentity();
10980 try {
10981 return mApp.imsRcsController.removeUceRegistrationOverrideShell(subId,
10982 new ArraySet<>(featureTags));
10983 } catch (ImsException e) {
10984 throw new ServiceSpecificException(e.getCode(), e.getMessage());
10985 } finally {
10986 Binder.restoreCallingIdentity(identity);
10987 }
10988 }
10989
10990 /**
10991 * Clear all overrides in the Set used to calculate the capabilities in PUBLISH.
10992 * @return current RcsContactUceCapability instance that will be used for PUBLISH.
10993 */
10994 // Used for SHELL command only right now.
10995 @Override
10996 public RcsContactUceCapability clearUceRegistrationOverrideShell(int subId) {
10997 TelephonyPermissions.enforceShellOnly(Binder.getCallingUid(),
10998 "clearUceRegistrationOverrideShell");
10999 final long identity = Binder.clearCallingIdentity();
11000 try {
11001 return mApp.imsRcsController.clearUceRegistrationOverrideShell(subId);
11002 } catch (ImsException e) {
11003 throw new ServiceSpecificException(e.getCode(), e.getMessage());
11004 } finally {
11005 Binder.restoreCallingIdentity(identity);
11006 }
11007 }
11008
11009 /**
11010 * @return current RcsContactUceCapability instance that will be used for PUBLISH.
11011 */
11012 // Used for SHELL command only right now.
11013 @Override
11014 public RcsContactUceCapability getLatestRcsContactUceCapabilityShell(int subId) {
11015 TelephonyPermissions.enforceShellOnly(Binder.getCallingUid(),
11016 "getLatestRcsContactUceCapabilityShell");
11017 final long identity = Binder.clearCallingIdentity();
11018 try {
11019 return mApp.imsRcsController.getLatestRcsContactUceCapabilityShell(subId);
11020 } catch (ImsException e) {
11021 throw new ServiceSpecificException(e.getCode(), e.getMessage());
11022 } finally {
11023 Binder.restoreCallingIdentity(identity);
11024 }
11025 }
11026
11027 /**
11028 * Returns the last PIDF XML sent to the network during the last PUBLISH or "none" if the
11029 * device does not have an active PUBLISH.
11030 */
11031 // Used for SHELL command only right now.
11032 @Override
11033 public String getLastUcePidfXmlShell(int subId) {
11034 TelephonyPermissions.enforceShellOnly(Binder.getCallingUid(), "uceGetLastPidfXml");
11035 final long identity = Binder.clearCallingIdentity();
11036 try {
11037 return mApp.imsRcsController.getLastUcePidfXmlShell(subId);
11038 } catch (ImsException e) {
11039 throw new ServiceSpecificException(e.getCode(), e.getMessage());
11040 } finally {
11041 Binder.restoreCallingIdentity(identity);
11042 }
11043 }
11044
James.cf Line8713a42021-04-29 16:04:26 +080011045 /**
11046 * Remove UCE requests cannot be sent to the network status.
11047 */
11048 // Used for SHELL command only right now.
11049 @Override
11050 public boolean removeUceRequestDisallowedStatus(int subId) {
11051 TelephonyPermissions.enforceShellOnly(Binder.getCallingUid(), "uceRemoveDisallowedStatus");
11052 final long identity = Binder.clearCallingIdentity();
11053 try {
11054 return mApp.imsRcsController.removeUceRequestDisallowedStatus(subId);
11055 } catch (ImsException e) {
11056 throw new ServiceSpecificException(e.getCode(), e.getMessage());
11057 } finally {
11058 Binder.restoreCallingIdentity(identity);
11059 }
11060 }
11061
James.cf Lin18bb9002021-05-25 01:37:38 +080011062 /**
11063 * Remove UCE requests cannot be sent to the network status.
11064 */
11065 // Used for SHELL command only.
11066 @Override
11067 public boolean setCapabilitiesRequestTimeout(int subId, long timeoutAfterMs) {
11068 TelephonyPermissions.enforceShellOnly(Binder.getCallingUid(), "setCapRequestTimeout");
11069 final long identity = Binder.clearCallingIdentity();
11070 try {
11071 return mApp.imsRcsController.setCapabilitiesRequestTimeout(subId, timeoutAfterMs);
11072 } catch (ImsException e) {
11073 throw new ServiceSpecificException(e.getCode(), e.getMessage());
11074 } finally {
11075 Binder.restoreCallingIdentity(identity);
11076 }
11077 }
Brad Ebinger14d467f2021-02-12 06:18:28 +000011078
James.cf Lin4b784aa2021-01-31 03:25:15 +080011079 @Override
Rambo Wanga5cc9b72021-01-07 10:51:54 -080011080 public void setSignalStrengthUpdateRequest(int subId, SignalStrengthUpdateRequest request,
11081 String callingPackage) {
11082 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
11083 mApp, subId, "setSignalStrengthUpdateRequest");
11084
11085 final int callingUid = Binder.getCallingUid();
11086 // Verify that tha callingPackage belongs to the calling UID
11087 mApp.getSystemService(AppOpsManager.class)
11088 .checkPackage(callingUid, callingPackage);
11089
Rambo Wang3607f502021-02-01 21:51:40 -080011090 validateSignalStrengthUpdateRequest(mApp, request, callingUid);
Rambo Wanga5cc9b72021-01-07 10:51:54 -080011091
11092 final long identity = Binder.clearCallingIdentity();
11093 try {
11094 Object result = sendRequest(CMD_SET_SIGNAL_STRENGTH_UPDATE_REQUEST,
11095 new Pair<Integer, SignalStrengthUpdateRequest>(callingUid, request), subId);
11096
11097 if (result instanceof IllegalStateException) {
11098 throw (IllegalStateException) result;
11099 }
11100 } finally {
11101 Binder.restoreCallingIdentity(identity);
11102 }
11103 }
11104
11105 @Override
11106 public void clearSignalStrengthUpdateRequest(int subId, SignalStrengthUpdateRequest request,
11107 String callingPackage) {
11108 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
11109 mApp, subId, "clearSignalStrengthUpdateRequest");
11110
11111 final int callingUid = Binder.getCallingUid();
11112 // Verify that tha callingPackage belongs to the calling UID
11113 mApp.getSystemService(AppOpsManager.class)
11114 .checkPackage(callingUid, callingPackage);
11115
11116 final long identity = Binder.clearCallingIdentity();
11117 try {
11118 Object result = sendRequest(CMD_CLEAR_SIGNAL_STRENGTH_UPDATE_REQUEST,
11119 new Pair<Integer, SignalStrengthUpdateRequest>(callingUid, request), subId);
11120
11121 if (result instanceof IllegalStateException) {
11122 throw (IllegalStateException) result;
11123 }
11124 } finally {
11125 Binder.restoreCallingIdentity(identity);
11126 }
11127 }
11128
Rambo Wang3607f502021-02-01 21:51:40 -080011129 private static void validateSignalStrengthUpdateRequest(Context context,
11130 SignalStrengthUpdateRequest request, int callingUid) {
Rambo Wanga5cc9b72021-01-07 10:51:54 -080011131 if (callingUid == Process.PHONE_UID || callingUid == Process.SYSTEM_UID) {
11132 // phone/system process do not have further restriction on request
11133 return;
11134 }
11135
11136 // Applications has restrictions on how to use the request:
Rambo Wang3607f502021-02-01 21:51:40 -080011137 // Non-system callers need permission to set mIsSystemThresholdReportingRequestedWhileIdle
Rambo Wanga5cc9b72021-01-07 10:51:54 -080011138 if (request.isSystemThresholdReportingRequestedWhileIdle()) {
Rambo Wang3607f502021-02-01 21:51:40 -080011139 context.enforceCallingOrSelfPermission(
11140 android.Manifest.permission.LISTEN_ALWAYS_REPORTED_SIGNAL_STRENGTH,
11141 "validateSignalStrengthUpdateRequest");
Rambo Wanga5cc9b72021-01-07 10:51:54 -080011142 }
11143
11144 for (SignalThresholdInfo info : request.getSignalThresholdInfos()) {
11145 // Only system caller can set mHysteresisMs/mHysteresisDb/mIsEnabled.
11146 if (info.getHysteresisMs() != SignalThresholdInfo.HYSTERESIS_MS_DISABLED
11147 || info.getHysteresisDb() != SignalThresholdInfo.HYSTERESIS_DB_DISABLED
11148 || info.isEnabled()) {
11149 throw new IllegalArgumentException(
11150 "Only system can set hide fields in SignalThresholdInfo");
11151 }
11152
11153 // Thresholds length for each RAN need in range. This has been validated in
11154 // SignalThresholdInfo#Builder#setThreshold. Here we prevent apps calling hide method
11155 // setThresholdUnlimited (e.g. through reflection) with too short or too long thresholds
11156 final int[] thresholds = info.getThresholds();
11157 Objects.requireNonNull(thresholds);
11158 if (thresholds.length < SignalThresholdInfo.getMinimumNumberOfThresholdsAllowed()
11159 || thresholds.length
11160 > SignalThresholdInfo.getMaximumNumberOfThresholdsAllowed()) {
11161 throw new IllegalArgumentException(
11162 "thresholds length is out of range: " + thresholds.length);
11163 }
11164 }
11165 }
SongFerngWang8236caa2021-01-17 21:51:44 +080011166
11167 /**
11168 * Gets the current phone capability.
11169 *
11170 * Requires carrier privileges or READ_PRECISE_PHONE_STATE permission.
11171 * @return the PhoneCapability which describes the data connection capability of modem.
11172 * It's used to evaluate possible phone config change, for example from single
11173 * SIM device to multi-SIM device.
11174 */
11175 @Override
11176 public PhoneCapability getPhoneCapability() {
11177 enforceReadPrivilegedPermission("getPhoneCapability");
11178 final long identity = Binder.clearCallingIdentity();
11179 try {
11180 return mPhoneConfigurationManager.getCurrentPhoneCapability();
11181 } finally {
11182 Binder.restoreCallingIdentity(identity);
11183 }
11184 }
Michele Berionne5e411512020-11-13 02:36:59 +000011185
11186 /**
11187 * Prepare TelephonyManager for an unattended reboot. The reboot is
11188 * required to be done shortly after the API is invoked.
11189 */
11190 @Override
11191 @TelephonyManager.PrepareUnattendedRebootResult
11192 public int prepareForUnattendedReboot() {
Rafael Higuera Silvad9630642021-09-20 15:32:01 +000011193 WorkSource workSource = getWorkSource(Binder.getCallingUid());
Michele Berionne5e411512020-11-13 02:36:59 +000011194 enforceRebootPermission();
11195
11196 final long identity = Binder.clearCallingIdentity();
11197 try {
Rafael Higuera Silvad9630642021-09-20 15:32:01 +000011198 return (int) sendRequest(CMD_PREPARE_UNATTENDED_REBOOT, null, workSource);
Michele Berionne5e411512020-11-13 02:36:59 +000011199 } finally {
11200 Binder.restoreCallingIdentity(identity);
11201 }
11202 }
Hongbo Zeng156aa4a2021-02-08 21:50:28 +080011203
11204 /**
11205 * Request to get the current slicing configuration including URSP rules and
11206 * NSSAIs (configured, allowed and rejected).
11207 *
11208 * Requires carrier privileges or READ_PRIVILEGED_PHONE_STATE permission.
11209 */
11210 @Override
11211 public void getSlicingConfig(ResultReceiver callback) {
Hongbo Zeng1b2063d2022-02-21 01:33:03 +000011212 TelephonyPermissions
11213 .enforceCallingOrSelfReadPrivilegedPhoneStatePermissionOrCarrierPrivilege(
11214 mApp, SubscriptionManager.INVALID_SUBSCRIPTION_ID, "getSlicingConfig");
Hongbo Zeng156aa4a2021-02-08 21:50:28 +080011215
11216 final long identity = Binder.clearCallingIdentity();
11217 try {
11218 Phone phone = getDefaultPhone();
11219 sendRequestAsync(CMD_GET_SLICING_CONFIG, callback, phone, null);
11220 } finally {
11221 Binder.restoreCallingIdentity(identity);
11222 }
11223 }
Hunsuk Choi3b742d62021-10-25 19:48:34 +000011224
11225 /**
Sarah Chin2ec39f62022-08-31 17:03:26 -070011226 * Check whether the given premium capability is available for purchase from the carrier.
11227 *
11228 * @param capability The premium capability to check.
11229 * @param subId The subId to check the premium capability for.
11230 *
11231 * @return Whether the given premium capability is available to purchase.
11232 */
11233 @Override
11234 public boolean isPremiumCapabilityAvailableForPurchase(int capability, int subId) {
11235 if (!TelephonyPermissions.checkCallingOrSelfReadNonDangerousPhoneStateNoThrow(
11236 mApp, "isPremiumCapabilityAvailableForPurchase")) {
11237 log("Premium capability "
11238 + TelephonyManager.convertPremiumCapabilityToString(capability)
11239 + " is not available for purchase due to missing permissions.");
11240 throw new SecurityException("isPremiumCapabilityAvailableForPurchase requires "
11241 + "permission READ_BASIC_PHONE_STATE.");
11242 }
11243
11244 Phone phone = getPhone(subId);
11245 final long identity = Binder.clearCallingIdentity();
11246 try {
11247 return SliceStore.getInstance(phone)
11248 .isPremiumCapabilityAvailableForPurchase(capability);
11249 } finally {
11250 Binder.restoreCallingIdentity(identity);
11251 }
11252 }
11253
11254 /**
11255 * Purchase the given premium capability from the carrier.
11256 *
11257 * @param capability The premium capability to purchase.
11258 * @param callback The result of the purchase request.
11259 * @param subId The subId to purchase the premium capability for.
11260 */
11261 @Override
11262 public void purchasePremiumCapability(int capability, IIntegerConsumer callback, int subId) {
11263 log("purchasePremiumCapability: capability="
11264 + TelephonyManager.convertPremiumCapabilityToString(capability) + ", caller="
11265 + getCurrentPackageName());
11266
11267 if (!TelephonyPermissions.checkCallingOrSelfReadNonDangerousPhoneStateNoThrow(
11268 mApp, "purchasePremiumCapability")) {
11269 log("purchasePremiumCapability "
11270 + TelephonyManager.convertPremiumCapabilityToString(capability)
11271 + " failed due to missing permissions.");
11272 throw new SecurityException("purchasePremiumCapability requires permission "
11273 + "READ_BASIC_PHONE_STATE.");
11274 }
11275
11276 Phone phone = getPhone(subId);
11277 Pair<Integer, IIntegerConsumer> argument = new Pair<>(capability, callback);
11278 sendRequestAsync(CMD_PURCHASE_PREMIUM_CAPABILITY, argument, phone, null);
11279 }
11280
11281 /**
Hunsuk Choi3b742d62021-10-25 19:48:34 +000011282 * Register an IMS connection state callback
11283 */
11284 @Override
Hunsuk Choi89bd22c2021-11-01 13:04:54 +000011285 public void registerImsStateCallback(int subId, int feature, IImsStateCallback cb,
11286 String callingPackage) {
Hunsuk Choi3b742d62021-10-25 19:48:34 +000011287 if (feature == ImsFeature.FEATURE_MMTEL) {
11288 // ImsMmTelManager
11289 // The following also checks READ_PRIVILEGED_PHONE_STATE.
11290 TelephonyPermissions
11291 .enforceCallingOrSelfReadPrecisePhoneStatePermissionOrCarrierPrivilege(
11292 mApp, subId, "registerImsStateCallback");
11293 } else if (feature == ImsFeature.FEATURE_RCS) {
11294 // ImsRcsManager or SipDelegateManager
11295 TelephonyPermissions.enforceAnyPermissionGrantedOrCarrierPrivileges(mApp, subId,
11296 Binder.getCallingUid(), "registerImsStateCallback",
11297 Manifest.permission.READ_PRIVILEGED_PHONE_STATE,
11298 Manifest.permission.READ_PRECISE_PHONE_STATE,
11299 Manifest.permission.ACCESS_RCS_USER_CAPABILITY_EXCHANGE,
11300 Manifest.permission.PERFORM_IMS_SINGLE_REGISTRATION);
11301 }
11302
11303 if (!ImsManager.isImsSupportedOnDevice(mApp)) {
11304 throw new ServiceSpecificException(ImsException.CODE_ERROR_UNSUPPORTED_OPERATION,
11305 "IMS not available on device.");
11306 }
11307
11308 if (subId == SubscriptionManager.DEFAULT_SUBSCRIPTION_ID) {
11309 throw new ServiceSpecificException(ImsException.CODE_ERROR_INVALID_SUBSCRIPTION);
11310 }
11311
11312 ImsStateCallbackController controller = ImsStateCallbackController.getInstance();
11313 if (controller == null) {
11314 throw new ServiceSpecificException(ImsException.CODE_ERROR_UNSUPPORTED_OPERATION,
11315 "IMS not available on device.");
11316 }
11317
Hunsuk Choi89bd22c2021-11-01 13:04:54 +000011318 if (callingPackage == null) {
11319 callingPackage = getCurrentPackageName();
11320 }
11321
Hunsuk Choi3b742d62021-10-25 19:48:34 +000011322 final long token = Binder.clearCallingIdentity();
11323 try {
11324 int slotId = getSlotIndexOrException(subId);
Hunsuk Choi89bd22c2021-11-01 13:04:54 +000011325 controller.registerImsStateCallback(subId, feature, cb, callingPackage);
Hunsuk Choi3b742d62021-10-25 19:48:34 +000011326 } catch (ImsException e) {
11327 throw new ServiceSpecificException(e.getCode());
11328 } finally {
11329 Binder.restoreCallingIdentity(token);
11330 }
11331 }
11332
11333 /**
11334 * Unregister an IMS connection state callback
11335 */
11336 @Override
11337 public void unregisterImsStateCallback(IImsStateCallback cb) {
11338 final long token = Binder.clearCallingIdentity();
11339 ImsStateCallbackController controller = ImsStateCallbackController.getInstance();
11340 if (controller == null) {
11341 return;
11342 }
11343 try {
11344 controller.unregisterImsStateCallback(cb);
11345 } finally {
11346 Binder.restoreCallingIdentity(token);
11347 }
11348 }
Sooraj Sasindranfae41b32021-10-26 02:10:05 -070011349
11350 /**
11351 * @return {@CellIdentity} last known cell identity {@CellIdentity}.
11352 *
11353 * Require {@link android.Manifest.permission#ACCESS_FINE_LOCATION} and
11354 * com.android.phone.permission.ACCESS_LAST_KNOWN_CELL_ID, otherwise throws
11355 * SecurityException.
11356 * If there is current registered network this value will be same as the registered cell
11357 * identity. If the device goes out of service the previous cell identity is cached and
11358 * will be returned. If the cache age of the Cell identity is more than 24 hours
11359 * it will be cleared and null will be returned.
11360 *
11361 */
11362 @Override
11363 public @Nullable CellIdentity getLastKnownCellIdentity(int subId, String callingPackage,
11364 String callingFeatureId) {
11365 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
11366 LocationAccessPolicy.LocationPermissionResult fineLocationResult =
11367 LocationAccessPolicy.checkLocationPermission(mApp,
11368 new LocationAccessPolicy.LocationPermissionQuery.Builder()
11369 .setCallingPackage(callingPackage)
11370 .setCallingFeatureId(callingFeatureId)
11371 .setCallingPid(Binder.getCallingPid())
11372 .setCallingUid(Binder.getCallingUid())
11373 .setMethod("getLastKnownCellIdentity")
11374 .setLogAsInfo(true)
11375 .setMinSdkVersionForFine(Build.VERSION_CODES.Q)
11376 .setMinSdkVersionForCoarse(Build.VERSION_CODES.Q)
11377 .setMinSdkVersionForEnforcement(Build.VERSION_CODES.Q)
11378 .build());
11379
11380 boolean hasFinePermission =
11381 fineLocationResult == LocationAccessPolicy.LocationPermissionResult.ALLOWED;
11382 if (!hasFinePermission
11383 || !TelephonyPermissions.checkLastKnownCellIdAccessPermission(mApp)) {
11384 throw new SecurityException("getLastKnownCellIdentity need ACCESS_FINE_LOCATION "
Rambo Wang918993a2022-04-27 09:08:36 -070011385 + "and ACCESS_LAST_KNOWN_CELL_ID permission.");
Sooraj Sasindranfae41b32021-10-26 02:10:05 -070011386 }
11387
11388 final long identity = Binder.clearCallingIdentity();
11389 try {
11390 Phone phone = getPhone(subId);
11391 if (phone == null) return null;
11392 ServiceStateTracker sst = phone.getServiceStateTracker();
11393 if (sst == null) return null;
11394 return sst.getLastKnownCellIdentity();
11395 } finally {
11396 Binder.restoreCallingIdentity(identity);
11397 }
11398 }
Jack Yu4c0a5502021-12-03 23:58:26 -080011399
jimsun3b9ccac2021-10-26 15:01:23 +080011400 /**
11401 * Sets the modem service class Name that Telephony will bind to.
11402 *
11403 * @param serviceName The class name of the modem service.
11404 * @return true if the operation is succeed, otherwise false.
11405 */
11406 public boolean setModemService(String serviceName) {
11407 Log.d(LOG_TAG, "setModemService - " + serviceName);
11408 TelephonyPermissions.enforceShellOnly(Binder.getCallingUid(), "setModemService");
11409 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(mApp,
11410 SubscriptionManager.INVALID_SUBSCRIPTION_ID,
11411 "setModemService");
11412 return mPhoneConfigurationManager.setModemService(serviceName);
11413 }
11414
11415 /**
11416 * Return the class name of the currently bounded modem service.
11417 *
11418 * @return the class name of the modem service.
11419 */
11420 public String getModemService() {
11421 String result;
11422 Log.d(LOG_TAG, "getModemService");
11423 TelephonyPermissions.enforceShellOnly(Binder.getCallingUid(), "getModemService");
11424 TelephonyPermissions
11425 .enforceCallingOrSelfReadPrivilegedPhoneStatePermissionOrCarrierPrivilege(
11426 mApp, SubscriptionManager.INVALID_SUBSCRIPTION_ID,
11427 "getModemService");
11428 result = mPhoneConfigurationManager.getModemService();
11429 Log.d(LOG_TAG, "result = " + result);
11430 return result;
11431 }
Hunter Knepshield2b076fa2022-01-19 02:26:22 -080011432
11433 @Override
11434 public void setVoiceServiceStateOverride(int subId, boolean hasService, String callingPackage) {
11435 // Only telecom (and shell, for CTS purposes) is allowed to call this method.
11436 mApp.enforceCallingOrSelfPermission(
11437 permission.BIND_TELECOM_CONNECTION_SERVICE, "setVoiceServiceStateOverride");
11438 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
11439
11440 final long identity = Binder.clearCallingIdentity();
11441 try {
11442 Phone phone = getPhone(subId);
11443 if (phone == null) return;
11444 phone.setVoiceServiceStateOverride(hasService);
11445 } finally {
11446 Binder.restoreCallingIdentity(identity);
11447 }
11448 }
Muralidhar Reddy4e5a8012022-05-11 14:49:00 +000011449
11450 /**
11451 * set removable eSIM as default eUICC.
11452 *
11453 * @hide
11454 */
11455 @Override
11456 public void setRemovableEsimAsDefaultEuicc(boolean isDefault, String callingPackage) {
11457 enforceModifyPermission();
11458 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
11459
11460 final long identity = Binder.clearCallingIdentity();
11461 try {
11462 UiccController.getInstance().setRemovableEsimAsDefaultEuicc(isDefault);
11463 } finally {
11464 Binder.restoreCallingIdentity(identity);
11465 }
11466 }
11467
11468 /**
11469 * Returns whether the removable eSIM is default eUICC or not.
11470 *
11471 * @hide
11472 */
11473 @Override
11474 public boolean isRemovableEsimDefaultEuicc(String callingPackage) {
11475 enforceReadPrivilegedPermission("isRemovableEsimDefaultEuicc");
11476 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
11477
11478 final long identity = Binder.clearCallingIdentity();
11479 try {
11480 return UiccController.getInstance().isRemovableEsimDefaultEuicc();
11481 } finally {
11482 Binder.restoreCallingIdentity(identity);
11483 }
11484 }
11485
11486
Santos Cordon7d4ddf62013-07-10 11:58:08 -070011487}