blob: 013d3020bc7405b7319fe50ce34885f6f032ff14 [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;
25
Brad Ebinger34c09a52021-02-17 23:23:21 +000026import android.Manifest;
Ta-wei Yen30a69c82016-12-27 14:52:32 -080027import android.Manifest.permission;
Hall Liua1548bd2019-12-24 14:14:12 -080028import android.annotation.NonNull;
Tyler Gunnf70ed162019-04-03 15:28:53 -070029import android.annotation.Nullable;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070030import android.app.AppOpsManager;
Ta-wei Yen30a69c82016-12-27 14:52:32 -080031import android.app.PendingIntent;
Hall Liu82694d52020-12-11 18:22:04 -080032import android.app.role.RoleManager;
Sailesh Nepalbd76e4e2013-10-27 13:59:44 -070033import android.content.ComponentName;
Amit Mahajan7dbbd822019-03-13 17:33:47 -070034import android.content.ContentResolver;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070035import android.content.Context;
36import android.content.Intent;
Derek Tan97ebb422014-09-05 16:55:38 -070037import android.content.SharedPreferences;
Derek Tan740e1672017-06-27 14:56:27 -070038import android.content.pm.ComponentInfo;
Amith Yamasani6e118872016-02-19 12:53:51 -080039import android.content.pm.PackageInfo;
Shishir Agrawal60f9c952014-06-23 12:00:43 -070040import android.content.pm.PackageManager;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070041import android.net.Uri;
42import android.os.AsyncResult;
43import android.os.Binder;
Hall Liuf19c44f2018-11-27 14:38:17 -080044import android.os.Build;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070045import android.os.Bundle;
46import android.os.Handler;
yinxu504e1392017-04-12 16:03:22 -070047import android.os.IBinder;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070048import android.os.Looper;
49import android.os.Message;
yinxu504e1392017-04-12 16:03:22 -070050import android.os.Messenger;
Hall Liua1548bd2019-12-24 14:14:12 -080051import android.os.ParcelFileDescriptor;
Malcolm Chen6ca97372019-07-01 16:28:21 -070052import android.os.ParcelUuid;
Tyler Gunn65d45c22017-06-05 11:22:26 -070053import android.os.PersistableBundle;
Shuo Qiancd19c462020-01-16 20:51:11 -080054import android.os.Process;
Brad Ebinger5f64b052017-12-14 14:26:15 -080055import android.os.RemoteException;
Adam Lesinski903a54c2016-04-11 14:49:52 -070056import android.os.ResultReceiver;
Brad Ebinger1ce9c432019-07-16 13:19:44 -070057import android.os.ServiceSpecificException;
Rambo Wang0f050d82021-02-12 11:43:36 -080058import android.os.SystemClock;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070059import android.os.UserHandle;
Stuart Scott981d8582015-04-21 14:09:50 -070060import android.os.UserManager;
Sooraj Sasindran9a909312016-07-18 11:57:25 -070061import android.os.WorkSource;
Derek Tan97ebb422014-09-05 16:55:38 -070062import android.preference.PreferenceManager;
Naina Nallurid63128d2019-09-17 14:10:30 -070063import android.provider.DeviceConfig;
Ihab Awadf2177b72013-11-25 13:33:23 -080064import android.provider.Settings;
Amit Mahajan7dbbd822019-03-13 17:33:47 -070065import android.provider.Telephony;
Inseob Kim14bb3d02018-12-13 17:11:34 +090066import android.sysprop.TelephonyProperties;
Santos Cordon7a1885b2015-02-03 11:15:19 -080067import android.telecom.PhoneAccount;
Nancy Chen31f9ba12016-01-06 11:42:12 -080068import android.telecom.PhoneAccountHandle;
Andrew Lee9431b832015-03-09 18:46:45 -070069import android.telecom.TelecomManager;
Chen Xu227e06f2019-09-26 22:48:11 -070070import android.telephony.Annotation.ApnType;
Jack Nudelmanb0b87642020-11-12 15:04:39 -080071import android.telephony.Annotation.ThermalMitigationResult;
Shuo Qian4a594052020-01-23 11:59:30 -080072import android.telephony.CallForwardingInfo;
Junda Liu12f7d802015-05-01 12:06:44 -070073import android.telephony.CarrierConfigManager;
Michele Berionne482f8202018-11-27 18:57:59 -080074import android.telephony.CarrierRestrictionRules;
yincheng zhao2737e882019-09-06 17:06:54 -070075import android.telephony.CellIdentity;
Meng Wanga10e89e2019-12-09 13:13:01 -080076import android.telephony.CellIdentityCdma;
77import android.telephony.CellIdentityGsm;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070078import android.telephony.CellInfo;
Nathan Haroldf180aac2018-06-01 18:43:55 -070079import android.telephony.CellInfoGsm;
80import android.telephony.CellInfoWcdma;
Sooraj Sasindran9a909312016-07-18 11:57:25 -070081import android.telephony.ClientRequestStats;
Jack Nudelmanb0b87642020-11-12 15:04:39 -080082import android.telephony.DataThrottlingRequest;
Hui Wang641e81c2020-10-12 12:14:23 -070083import android.telephony.IBootstrapAuthenticationCallback;
Nathan Haroldfa8da0f2018-09-27 18:51:29 -070084import android.telephony.ICellInfoCallback;
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -070085import android.telephony.IccOpenLogicalChannelResponse;
Hall Liu1aa510f2017-11-22 17:40:08 -080086import android.telephony.LocationAccessPolicy;
Ta-wei Yen87c49842016-05-13 21:19:52 -070087import android.telephony.ModemActivityInfo;
Jake Hambye994d462014-02-03 13:10:13 -080088import android.telephony.NeighboringCellInfo;
yinxu504e1392017-04-12 16:03:22 -070089import android.telephony.NetworkScanRequest;
Michele4245e952019-02-04 11:36:23 -080090import android.telephony.PhoneCapability;
Hall Liud892bec2018-11-30 14:51:45 -080091import android.telephony.PhoneNumberRange;
Wink Saville5d475dd2014-10-17 15:00:58 -070092import android.telephony.RadioAccessFamily;
Hall Liub2ac8ef2019-02-28 15:56:23 -080093import android.telephony.RadioAccessSpecifier;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070094import android.telephony.ServiceState;
Nathan Harold46b42aa2017-03-10 19:38:22 -080095import android.telephony.SignalStrength;
Rambo Wanga5cc9b72021-01-07 10:51:54 -080096import android.telephony.SignalStrengthUpdateRequest;
97import android.telephony.SignalThresholdInfo;
Wink Saville0f3b5fc2014-11-11 08:40:49 -080098import android.telephony.SubscriptionInfo;
Jeff Sharkey85190e62014-12-05 09:40:12 -080099import android.telephony.SubscriptionManager;
Peter Wangc035ce42020-01-08 21:00:22 -0800100import android.telephony.TelephonyFrameworkInitializer;
Sanket Padawe99ef1e32016-05-18 16:12:33 -0700101import android.telephony.TelephonyHistogram;
Ta-wei Yenb6929602016-05-24 15:48:27 -0700102import android.telephony.TelephonyManager;
Hall Liub2ac8ef2019-02-28 15:56:23 -0800103import android.telephony.TelephonyScanManager;
Jack Nudelmanb0b87642020-11-12 15:04:39 -0800104import android.telephony.ThermalMitigationRequest;
Jordan Liu5aa07002018-12-18 15:44:48 -0800105import android.telephony.UiccCardInfo;
Holly Jiuyu Sun01c47ad2018-01-24 17:56:33 +0000106import android.telephony.UiccSlotInfo;
Tyler Gunn65d45c22017-06-05 11:22:26 -0700107import android.telephony.UssdResponse;
Ta-wei Yenb6929602016-05-24 15:48:27 -0700108import android.telephony.VisualVoicemailSmsFilterSettings;
Jack Yub5d8f642018-11-26 11:20:48 -0800109import android.telephony.data.ApnSetting;
Hongbo Zeng156aa4a2021-02-08 21:50:28 +0800110import android.telephony.data.SlicingConfig;
Jack Yub5d8f642018-11-26 11:20:48 -0800111import android.telephony.emergency.EmergencyNumber;
Hui Wang641e81c2020-10-12 12:14:23 -0700112import android.telephony.gba.GbaAuthRequest;
113import android.telephony.gba.UaSecurityProtocolIdentifier;
Brad Ebinger1ce9c432019-07-16 13:19:44 -0700114import android.telephony.ims.ImsException;
Brad Ebinger1c8542e2019-01-14 13:43:14 -0800115import android.telephony.ims.ProvisioningManager;
Hui Wang761a6682020-10-31 05:12:53 +0000116import android.telephony.ims.RcsClientConfiguration;
Brad Ebinger14d467f2021-02-12 06:18:28 +0000117import android.telephony.ims.RcsContactUceCapability;
Brad Ebingera34a6c22019-10-22 17:36:18 -0700118import android.telephony.ims.RegistrationManager;
Brad Ebinger35c841c2018-10-01 10:40:55 -0700119import android.telephony.ims.aidl.IImsCapabilityCallback;
Brad Ebinger22bc3e42018-01-16 09:39:35 -0800120import android.telephony.ims.aidl.IImsConfig;
Brad Ebingerdf5b4f02018-10-31 11:24:17 -0700121import android.telephony.ims.aidl.IImsConfigCallback;
Brad Ebinger22bc3e42018-01-16 09:39:35 -0800122import android.telephony.ims.aidl.IImsRegistration;
Brad Ebinger35c841c2018-10-01 10:40:55 -0700123import android.telephony.ims.aidl.IImsRegistrationCallback;
Hui Wang761a6682020-10-31 05:12:53 +0000124import android.telephony.ims.aidl.IRcsConfigCallback;
Brad Ebingerbc7dd582019-10-17 17:03:22 -0700125import android.telephony.ims.feature.ImsFeature;
Brad Ebinger1c8542e2019-01-14 13:43:14 -0800126import android.telephony.ims.feature.MmTelFeature;
allenwtsu99c623b2020-01-03 18:24:23 +0800127import android.telephony.ims.feature.RcsFeature;
Brad Ebinger1c8542e2019-01-14 13:43:14 -0800128import android.telephony.ims.stub.ImsConfigImplBase;
Brad Ebinger1f2b5082018-02-08 16:11:32 -0800129import android.telephony.ims.stub.ImsRegistrationImplBase;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700130import android.text.TextUtils;
Jeff Sharkey85190e62014-12-05 09:40:12 -0800131import android.util.ArraySet;
Hall Liud60acc92020-05-21 17:09:35 -0700132import android.util.EventLog;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700133import android.util.Log;
Jake Hambye994d462014-02-03 13:10:13 -0800134import android.util.Pair;
Ta-wei Yen30a69c82016-12-27 14:52:32 -0800135
Andrew Lee312e8172014-10-23 17:01:36 -0700136import com.android.ims.ImsManager;
Brad Ebinger34bef922017-11-09 10:27:08 -0800137import com.android.ims.internal.IImsServiceFeatureCallback;
James.cf Linbcdf8b32021-01-14 16:44:13 +0800138import com.android.ims.rcs.uce.eab.EabUtil;
Shuo Qian4a594052020-01-23 11:59:30 -0800139import com.android.internal.telephony.CallForwardInfo;
Shishir Agrawal566b7612013-10-28 14:41:00 -0700140import com.android.internal.telephony.CallManager;
Tyler Gunn52dcf772017-04-26 11:30:31 -0700141import com.android.internal.telephony.CallStateException;
chen xu651eec72018-11-11 19:03:44 -0800142import com.android.internal.telephony.CarrierResolver;
Shishir Agrawal302c8692015-06-19 13:49:39 -0700143import com.android.internal.telephony.CellNetworkScanResult;
Shishir Agrawal566b7612013-10-28 14:41:00 -0700144import com.android.internal.telephony.CommandException;
Shuo Qian4a594052020-01-23 11:59:30 -0800145import com.android.internal.telephony.CommandsInterface;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700146import com.android.internal.telephony.DefaultPhoneNotifier;
Hui Wang641e81c2020-10-12 12:14:23 -0700147import com.android.internal.telephony.GbaManager;
Shuo Qianccbaf742021-02-22 18:32:21 -0800148import com.android.internal.telephony.GsmCdmaPhone;
Nathan Harold48d6fd52019-02-06 19:01:40 -0800149import com.android.internal.telephony.HalVersion;
Hall Liu73f5d362020-01-20 13:42:00 -0800150import com.android.internal.telephony.IBooleanConsumer;
Hall Liu27d24262020-09-18 19:04:59 -0700151import com.android.internal.telephony.ICallForwardingInfoCallback;
Brad Ebingera63db5f2019-04-23 16:31:13 -0700152import com.android.internal.telephony.IIntegerConsumer;
Hall Liud892bec2018-11-30 14:51:45 -0800153import com.android.internal.telephony.INumberVerificationCallback;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700154import com.android.internal.telephony.ITelephony;
Jake Hambye994d462014-02-03 13:10:13 -0800155import com.android.internal.telephony.IccCard;
Jack Yu5f7092c2018-04-13 14:05:37 -0700156import com.android.internal.telephony.LocaleTracker;
yinxub1bed742017-04-17 11:45:04 -0700157import com.android.internal.telephony.NetworkScanRequestTracker;
Shishir Agrawal302c8692015-06-19 13:49:39 -0700158import com.android.internal.telephony.OperatorInfo;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700159import com.android.internal.telephony.Phone;
Malcolm Chen2c63d402018-08-14 16:00:53 -0700160import com.android.internal.telephony.PhoneConfigurationManager;
Nathan Harolda667c152016-12-14 11:27:20 -0800161import com.android.internal.telephony.PhoneConstantConversions;
Ta-wei Yen87c49842016-05-13 21:19:52 -0700162import com.android.internal.telephony.PhoneConstants;
Wink Saville36469e72014-06-11 15:17:00 -0700163import com.android.internal.telephony.PhoneFactory;
Wink Saville5d475dd2014-10-17 15:00:58 -0700164import com.android.internal.telephony.ProxyController;
Sanket Padawe99ef1e32016-05-18 16:12:33 -0700165import com.android.internal.telephony.RIL;
SongFerngWang8c6e82e2021-03-02 22:09:29 +0800166import com.android.internal.telephony.RILConstants;
Daniel Bright94f43662021-03-01 14:43:40 -0800167import com.android.internal.telephony.RadioInterfaceCapabilityController;
Jack Yu5f7092c2018-04-13 14:05:37 -0700168import com.android.internal.telephony.ServiceStateTracker;
Amit Mahajandccb3f12019-05-13 13:48:32 -0700169import com.android.internal.telephony.SmsController;
Brad Ebingera63db5f2019-04-23 16:31:13 -0700170import com.android.internal.telephony.SmsPermissions;
Wink Savilleac1bdfd2014-11-20 23:04:44 -0800171import com.android.internal.telephony.SubscriptionController;
Peter Wang59571be2020-01-27 12:35:15 +0800172import com.android.internal.telephony.TelephonyIntents;
Jeff Davidson7e17e312018-02-13 18:17:36 -0800173import com.android.internal.telephony.TelephonyPermissions;
Malcolm Chendc8c10e2019-04-10 18:25:07 -0700174import com.android.internal.telephony.dataconnection.ApnSettingUtils;
sqianf4ca7ed2019-01-15 18:32:07 -0800175import com.android.internal.telephony.emergency.EmergencyNumberTracker;
Derek Tan740e1672017-06-27 14:56:27 -0700176import com.android.internal.telephony.euicc.EuiccConnector;
Brad Ebinger9c0eb502019-01-23 15:06:19 -0800177import com.android.internal.telephony.ims.ImsResolver;
Tyler Gunn7bcdc742019-10-04 15:56:59 -0700178import com.android.internal.telephony.imsphone.ImsPhone;
179import com.android.internal.telephony.imsphone.ImsPhoneCallTracker;
Pengquan Meng6c2dc9f2019-02-06 11:12:53 -0800180import com.android.internal.telephony.metrics.TelephonyMetrics;
Meng Wangafbc5852019-09-19 17:37:13 -0700181import com.android.internal.telephony.uicc.IccCardApplicationStatus.AppType;
Shishir Agrawal566b7612013-10-28 14:41:00 -0700182import com.android.internal.telephony.uicc.IccIoResult;
changbetty7157e9e2019-12-06 18:16:37 +0800183import com.android.internal.telephony.uicc.IccRecords;
Shishir Agrawal566b7612013-10-28 14:41:00 -0700184import com.android.internal.telephony.uicc.IccUtils;
Nathan Haroldb3014052017-01-25 15:57:32 -0800185import com.android.internal.telephony.uicc.SIMRecords;
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700186import com.android.internal.telephony.uicc.UiccCard;
Nathan Haroldb3014052017-01-25 15:57:32 -0800187import com.android.internal.telephony.uicc.UiccCardApplication;
Shishir Agrawal566b7612013-10-28 14:41:00 -0700188import com.android.internal.telephony.uicc.UiccController;
Jeff Davidson7e17e312018-02-13 18:17:36 -0800189import com.android.internal.telephony.uicc.UiccProfile;
Holly Jiuyu Sun01c47ad2018-01-24 17:56:33 +0000190import com.android.internal.telephony.uicc.UiccSlot;
zoey chenc730df82019-12-18 17:07:20 +0800191import com.android.internal.telephony.util.LocaleUtils;
fionaxu7ed723d2017-05-30 18:58:54 -0700192import com.android.internal.telephony.util.VoicemailNotificationSettingsUtil;
Hall Liu27d24262020-09-18 19:04:59 -0700193import com.android.internal.util.FunctionalUtils;
Jake Hambye994d462014-02-03 13:10:13 -0800194import com.android.internal.util.HexDump;
Hall Liuaa4211e2021-01-20 15:43:39 -0800195import com.android.phone.callcomposer.CallComposerPictureManager;
196import com.android.phone.callcomposer.CallComposerPictureTransfer;
197import com.android.phone.callcomposer.ImageData;
Brad Ebingera63db5f2019-04-23 16:31:13 -0700198import com.android.phone.settings.PickSmsSubscriptionActivity;
Ta-wei Yenc9df0432017-04-17 17:09:07 -0700199import com.android.phone.vvm.PhoneAccountHandleConverter;
Ta-wei Yen527a9c02017-01-06 15:29:25 -0800200import com.android.phone.vvm.RemoteVvmTaskManager;
Ta-wei Yenc9df0432017-04-17 17:09:07 -0700201import com.android.phone.vvm.VisualVoicemailSettingsUtil;
Ta-wei Yenc8905312017-03-28 11:14:45 -0700202import com.android.phone.vvm.VisualVoicemailSmsFilterConfig;
Jack Nudelmanb0b87642020-11-12 15:04:39 -0800203import com.android.services.telephony.TelecomAccountRegistry;
204import com.android.services.telephony.TelephonyConnectionService;
Peter Wang44b186e2020-01-13 23:33:09 -0800205import com.android.telephony.Rlog;
Ta-wei Yen30a69c82016-12-27 14:52:32 -0800206
Hall Liu82694d52020-12-11 18:22:04 -0800207import java.io.ByteArrayOutputStream;
Ta-wei Yenc236d6b2016-06-21 13:33:12 -0700208import java.io.FileDescriptor;
Hall Liu82694d52020-12-11 18:22:04 -0800209import java.io.IOException;
210import java.io.InputStream;
Ta-wei Yenc236d6b2016-06-21 13:33:12 -0700211import java.io.PrintWriter;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700212import java.util.ArrayList;
Jeff Sharkey85190e62014-12-05 09:40:12 -0800213import java.util.Arrays;
sqian11b7a0e2018-12-05 18:48:28 -0800214import java.util.HashMap;
sqianf4ca7ed2019-01-15 18:32:07 -0800215import java.util.HashSet;
Jake Hambye994d462014-02-03 13:10:13 -0800216import java.util.List;
Narayan Kamath1c496c22015-04-16 14:40:19 +0100217import java.util.Locale;
Jeff Sharkey85190e62014-12-05 09:40:12 -0800218import java.util.Map;
Nazanin Bakhshif71371d2019-04-29 17:29:44 -0700219import java.util.NoSuchElementException;
Hall Liu82694d52020-12-11 18:22:04 -0800220import java.util.Objects;
sqianf4ca7ed2019-01-15 18:32:07 -0800221import java.util.Set;
Hall Liu82694d52020-12-11 18:22:04 -0800222import java.util.concurrent.Executors;
Peter Wangdafb9ac2020-01-15 14:13:38 -0800223import java.util.concurrent.atomic.AtomicBoolean;
Hall Liu73f5d362020-01-20 13:42:00 -0800224import java.util.function.Consumer;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700225
226/**
227 * Implementation of the ITelephony interface.
228 */
Santos Cordon117fee72014-05-16 17:56:12 -0700229public class PhoneInterfaceManager extends ITelephony.Stub {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700230 private static final String LOG_TAG = "PhoneInterfaceManager";
231 private static final boolean DBG = (PhoneGlobals.DBG_LEVEL >= 2);
232 private static final boolean DBG_LOC = false;
Jeff Sharkey85190e62014-12-05 09:40:12 -0800233 private static final boolean DBG_MERGE = false;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700234
235 // Message codes used with mMainThreadHandler
236 private static final int CMD_HANDLE_PIN_MMI = 1;
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700237 private static final int CMD_TRANSMIT_APDU_LOGICAL_CHANNEL = 7;
238 private static final int EVENT_TRANSMIT_APDU_LOGICAL_CHANNEL_DONE = 8;
Shishir Agrawal566b7612013-10-28 14:41:00 -0700239 private static final int CMD_OPEN_CHANNEL = 9;
240 private static final int EVENT_OPEN_CHANNEL_DONE = 10;
241 private static final int CMD_CLOSE_CHANNEL = 11;
242 private static final int EVENT_CLOSE_CHANNEL_DONE = 12;
Jake Hambye994d462014-02-03 13:10:13 -0800243 private static final int CMD_NV_READ_ITEM = 13;
244 private static final int EVENT_NV_READ_ITEM_DONE = 14;
245 private static final int CMD_NV_WRITE_ITEM = 15;
246 private static final int EVENT_NV_WRITE_ITEM_DONE = 16;
247 private static final int CMD_NV_WRITE_CDMA_PRL = 17;
248 private static final int EVENT_NV_WRITE_CDMA_PRL_DONE = 18;
chen xu6dac5ab2018-10-26 17:39:23 -0700249 private static final int CMD_RESET_MODEM_CONFIG = 19;
250 private static final int EVENT_RESET_MODEM_CONFIG_DONE = 20;
SongFerngWang3ef3e072020-12-21 16:41:52 +0800251 private static final int CMD_GET_ALLOWED_NETWORK_TYPES_BITMASK = 21;
252 private static final int EVENT_GET_ALLOWED_NETWORK_TYPES_BITMASK_DONE = 22;
Sailesh Nepal35b59452014-03-06 09:26:56 -0800253 private static final int CMD_SEND_ENVELOPE = 25;
254 private static final int EVENT_SEND_ENVELOPE_DONE = 26;
Shuo Qian850e4d6a2018-04-25 21:02:08 +0000255 private static final int CMD_INVOKE_OEM_RIL_REQUEST_RAW = 27;
256 private static final int EVENT_INVOKE_OEM_RIL_REQUEST_RAW_DONE = 28;
Derek Tan6b088ee2014-09-05 14:15:18 -0700257 private static final int CMD_TRANSMIT_APDU_BASIC_CHANNEL = 29;
258 private static final int EVENT_TRANSMIT_APDU_BASIC_CHANNEL_DONE = 30;
259 private static final int CMD_EXCHANGE_SIM_IO = 31;
260 private static final int EVENT_EXCHANGE_SIM_IO_DONE = 32;
Shishir Agrawal76d5da92014-11-09 16:17:25 -0800261 private static final int CMD_SET_VOICEMAIL_NUMBER = 33;
262 private static final int EVENT_SET_VOICEMAIL_NUMBER_DONE = 34;
Stuart Scott54788802015-03-30 13:18:01 -0700263 private static final int CMD_SET_NETWORK_SELECTION_MODE_AUTOMATIC = 35;
264 private static final int EVENT_SET_NETWORK_SELECTION_MODE_AUTOMATIC_DONE = 36;
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -0700265 private static final int CMD_GET_MODEM_ACTIVITY_INFO = 37;
266 private static final int EVENT_GET_MODEM_ACTIVITY_INFO_DONE = 38;
Shishir Agrawal302c8692015-06-19 13:49:39 -0700267 private static final int CMD_PERFORM_NETWORK_SCAN = 39;
268 private static final int EVENT_PERFORM_NETWORK_SCAN_DONE = 40;
269 private static final int CMD_SET_NETWORK_SELECTION_MODE_MANUAL = 41;
270 private static final int EVENT_SET_NETWORK_SELECTION_MODE_MANUAL_DONE = 42;
Meng Wang1a7c35a2016-05-05 20:56:15 -0700271 private static final int CMD_SET_ALLOWED_CARRIERS = 43;
272 private static final int EVENT_SET_ALLOWED_CARRIERS_DONE = 44;
273 private static final int CMD_GET_ALLOWED_CARRIERS = 45;
274 private static final int EVENT_GET_ALLOWED_CARRIERS_DONE = 46;
pkanwar32d516d2016-10-14 19:37:38 -0700275 private static final int CMD_HANDLE_USSD_REQUEST = 47;
Nathan Haroldb3014052017-01-25 15:57:32 -0800276 private static final int CMD_GET_FORBIDDEN_PLMNS = 48;
277 private static final int EVENT_GET_FORBIDDEN_PLMNS_DONE = 49;
Holly Jiuyu Sun01c47ad2018-01-24 17:56:33 +0000278 private static final int CMD_SWITCH_SLOTS = 50;
279 private static final int EVENT_SWITCH_SLOTS_DONE = 51;
Pengquan Menga1bb6272018-09-06 09:59:22 -0700280 private static final int CMD_GET_NETWORK_SELECTION_MODE = 52;
281 private static final int EVENT_GET_NETWORK_SELECTION_MODE_DONE = 53;
282 private static final int CMD_GET_CDMA_ROAMING_MODE = 54;
283 private static final int EVENT_GET_CDMA_ROAMING_MODE_DONE = 55;
284 private static final int CMD_SET_CDMA_ROAMING_MODE = 56;
285 private static final int EVENT_SET_CDMA_ROAMING_MODE_DONE = 57;
286 private static final int CMD_SET_CDMA_SUBSCRIPTION_MODE = 58;
287 private static final int EVENT_SET_CDMA_SUBSCRIPTION_MODE_DONE = 59;
Nathan Harold3ff88932018-08-14 10:19:49 -0700288 private static final int CMD_GET_ALL_CELL_INFO = 60;
289 private static final int EVENT_GET_ALL_CELL_INFO_DONE = 61;
290 private static final int CMD_GET_CELL_LOCATION = 62;
291 private static final int EVENT_GET_CELL_LOCATION_DONE = 63;
chen xu6dac5ab2018-10-26 17:39:23 -0700292 private static final int CMD_MODEM_REBOOT = 64;
293 private static final int EVENT_CMD_MODEM_REBOOT_DONE = 65;
Nathan Haroldfa8da0f2018-09-27 18:51:29 -0700294 private static final int CMD_REQUEST_CELL_INFO_UPDATE = 66;
295 private static final int EVENT_REQUEST_CELL_INFO_UPDATE_DONE = 67;
Malcolm Chen8e4ed912019-01-15 20:22:16 -0800296 private static final int CMD_REQUEST_ENABLE_MODEM = 68;
297 private static final int EVENT_ENABLE_MODEM_DONE = 69;
Nazanin Bakhshif71371d2019-04-29 17:29:44 -0700298 private static final int CMD_GET_MODEM_STATUS = 70;
299 private static final int EVENT_GET_MODEM_STATUS_DONE = 71;
yincheng zhao2737e882019-09-06 17:06:54 -0700300 private static final int CMD_SET_FORBIDDEN_PLMNS = 72;
301 private static final int EVENT_SET_FORBIDDEN_PLMNS_DONE = 73;
Naina Nallurid63128d2019-09-17 14:10:30 -0700302 private static final int CMD_ERASE_MODEM_CONFIG = 74;
303 private static final int EVENT_ERASE_MODEM_CONFIG_DONE = 75;
zoey chene02881a2019-12-30 16:11:23 +0800304 private static final int CMD_CHANGE_ICC_LOCK_PASSWORD = 76;
305 private static final int EVENT_CHANGE_ICC_LOCK_PASSWORD_DONE = 77;
306 private static final int CMD_SET_ICC_LOCK_ENABLED = 78;
307 private static final int EVENT_SET_ICC_LOCK_ENABLED_DONE = 79;
Hall Liu73f5d362020-01-20 13:42:00 -0800308 private static final int CMD_SET_SYSTEM_SELECTION_CHANNELS = 80;
309 private static final int EVENT_SET_SYSTEM_SELECTION_CHANNELS_DONE = 81;
Peter Wangdafb9ac2020-01-15 14:13:38 -0800310 private static final int MSG_NOTIFY_USER_ACTIVITY = 82;
Shuo Qian4a594052020-01-23 11:59:30 -0800311 private static final int CMD_GET_CALL_FORWARDING = 83;
312 private static final int EVENT_GET_CALL_FORWARDING_DONE = 84;
313 private static final int CMD_SET_CALL_FORWARDING = 85;
314 private static final int EVENT_SET_CALL_FORWARDING_DONE = 86;
315 private static final int CMD_GET_CALL_WAITING = 87;
316 private static final int EVENT_GET_CALL_WAITING_DONE = 88;
317 private static final int CMD_SET_CALL_WAITING = 89;
318 private static final int EVENT_SET_CALL_WAITING_DONE = 90;
Sooraj Sasindran37444802020-08-11 10:40:43 -0700319 private static final int CMD_ENABLE_NR_DUAL_CONNECTIVITY = 91;
320 private static final int EVENT_ENABLE_NR_DUAL_CONNECTIVITY_DONE = 92;
321 private static final int CMD_IS_NR_DUAL_CONNECTIVITY_ENABLED = 93;
322 private static final int EVENT_IS_NR_DUAL_CONNECTIVITY_ENABLED_DONE = 94;
Sarah Chinbaab1432020-10-28 13:46:24 -0700323 private static final int CMD_GET_CDMA_SUBSCRIPTION_MODE = 95;
324 private static final int EVENT_GET_CDMA_SUBSCRIPTION_MODE_DONE = 96;
Sarah Chin679c08a2020-11-18 13:39:35 -0800325 private static final int CMD_GET_SYSTEM_SELECTION_CHANNELS = 97;
326 private static final int EVENT_GET_SYSTEM_SELECTION_CHANNELS_DONE = 98;
Jack Nudelmanb0b87642020-11-12 15:04:39 -0800327 private static final int CMD_SET_DATA_THROTTLING = 99;
328 private static final int EVENT_SET_DATA_THROTTLING_DONE = 100;
Jordan Liu109698e2020-11-24 14:50:34 -0800329 private static final int CMD_SET_SIM_POWER = 101;
330 private static final int EVENT_SET_SIM_POWER_DONE = 102;
Rambo Wanga5cc9b72021-01-07 10:51:54 -0800331 private static final int CMD_SET_SIGNAL_STRENGTH_UPDATE_REQUEST = 103;
332 private static final int EVENT_SET_SIGNAL_STRENGTH_UPDATE_REQUEST_DONE = 104;
333 private static final int CMD_CLEAR_SIGNAL_STRENGTH_UPDATE_REQUEST = 105;
334 private static final int EVENT_CLEAR_SIGNAL_STRENGTH_UPDATE_REQUEST_DONE = 106;
SongFerngWang3ef3e072020-12-21 16:41:52 +0800335 private static final int CMD_SET_ALLOWED_NETWORK_TYPES_FOR_REASON = 107;
336 private static final int EVENT_SET_ALLOWED_NETWORK_TYPES_FOR_REASON_DONE = 108;
Michele Berionne5e411512020-11-13 02:36:59 +0000337 private static final int CMD_PREPARE_UNATTENDED_REBOOT = 109;
Hongbo Zeng156aa4a2021-02-08 21:50:28 +0800338 private static final int CMD_GET_SLICING_CONFIG = 110;
339 private static final int EVENT_GET_SLICING_CONFIG_DONE = 111;
Kai Shif70f46f2021-03-03 13:59:46 -0800340 private static final int CMD_ERASE_DATA_SHARED_PREFERENCES = 112;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700341
Holly Jiuyu Sun1cc2d552018-01-26 15:51:16 -0800342 // Parameters of select command.
343 private static final int SELECT_COMMAND = 0xA4;
344 private static final int SELECT_P1 = 0x04;
345 private static final int SELECT_P2 = 0;
346 private static final int SELECT_P3 = 0x10;
347
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700348 /** The singleton instance. */
349 private static PhoneInterfaceManager sInstance;
Jack Nudelman644b91a2021-03-12 14:09:48 -0800350 private static List<String> sThermalMitigationAllowlistedPackages = new ArrayList<>();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700351
Wink Saville3ab207e2014-11-20 13:07:20 -0800352 private PhoneGlobals mApp;
Wink Saville3ab207e2014-11-20 13:07:20 -0800353 private CallManager mCM;
Brad Ebinger24c29992019-12-05 13:03:21 -0800354 private ImsResolver mImsResolver;
Stuart Scott981d8582015-04-21 14:09:50 -0700355 private UserManager mUserManager;
Wink Saville3ab207e2014-11-20 13:07:20 -0800356 private AppOpsManager mAppOps;
357 private MainThreadHandler mMainThreadHandler;
Wink Savilleac1bdfd2014-11-20 23:04:44 -0800358 private SubscriptionController mSubscriptionController;
Wink Saville3ab207e2014-11-20 13:07:20 -0800359 private SharedPreferences mTelephonySharedPreferences;
Malcolm Chen2c63d402018-08-14 16:00:53 -0700360 private PhoneConfigurationManager mPhoneConfigurationManager;
Daniel Bright94f43662021-03-01 14:43:40 -0800361 private final RadioInterfaceCapabilityController mRadioInterfaceCapabilities;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700362
Peter Wangdafb9ac2020-01-15 14:13:38 -0800363 /** User Activity */
364 private AtomicBoolean mNotifyUserActivity;
Peter Wangdafb9ac2020-01-15 14:13:38 -0800365 private static final int USER_ACTIVITY_NOTIFICATION_DELAY = 200;
366
Jeff Davidson8ab02b22020-03-28 12:24:40 -0700367 private Set<Integer> mCarrierPrivilegeTestOverrideSubIds = new ArraySet<>();
368
Derek Tan97ebb422014-09-05 16:55:38 -0700369 private static final String PREF_CARRIERS_ALPHATAG_PREFIX = "carrier_alphtag_";
370 private static final String PREF_CARRIERS_NUMBER_PREFIX = "carrier_number_";
Jeff Sharkey85190e62014-12-05 09:40:12 -0800371 private static final String PREF_CARRIERS_SUBSCRIBER_PREFIX = "carrier_subscriber_";
Brad Ebinger1c8542e2019-01-14 13:43:14 -0800372 private static final String PREF_PROVISION_IMS_MMTEL_PREFIX = "provision_ims_mmtel_";
Derek Tan89e89d42014-07-08 17:00:10 -0700373
Michelecea4cf22018-12-21 15:00:11 -0800374 // String to store multi SIM allowed
375 private static final String PREF_MULTI_SIM_RESTRICTED = "multisim_restricted";
376
Derek Tan740e1672017-06-27 14:56:27 -0700377 // The AID of ISD-R.
378 private static final String ISDR_AID = "A0000005591010FFFFFFFF8900000100";
379
yinxub1bed742017-04-17 11:45:04 -0700380 private NetworkScanRequestTracker mNetworkScanRequestTracker;
381
David Kelly5e06a7f2018-03-12 14:10:59 +0000382 private static final int TYPE_ALLOCATION_CODE_LENGTH = 8;
383 private static final int MANUFACTURER_CODE_LENGTH = 8;
384
Jack Nudelmanb0b87642020-11-12 15:04:39 -0800385 private static final int SET_DATA_THROTTLING_MODEM_THREW_INVALID_PARAMS = -1;
Jack Nudelman5d6a98b2021-03-04 14:26:25 -0800386 private static final int MODEM_DOES_NOT_SUPPORT_DATA_THROTTLING_ERROR_CODE = -2;
Jack Nudelmanb0b87642020-11-12 15:04:39 -0800387
Derek Tan89e89d42014-07-08 17:00:10 -0700388 /**
Naina Nallurid63128d2019-09-17 14:10:30 -0700389 * Experiment flag to enable erase modem config on reset network, default value is false
390 */
391 public static final String RESET_NETWORK_ERASE_MODEM_CONFIG_ENABLED =
392 "reset_network_erase_modem_config_enabled";
393
Rambo Wang0f050d82021-02-12 11:43:36 -0800394 private static final int SET_NETWORK_SELECTION_MODE_AUTOMATIC_TIMEOUT_MS = 2000; // 2 seconds
395
Naina Nallurid63128d2019-09-17 14:10:30 -0700396 /**
Shishir Agrawal566b7612013-10-28 14:41:00 -0700397 * A request object to use for transmitting data to an ICC.
398 */
399 private static final class IccAPDUArgument {
400 public int channel, cla, command, p1, p2, p3;
401 public String data;
402
403 public IccAPDUArgument(int channel, int cla, int command,
404 int p1, int p2, int p3, String data) {
405 this.channel = channel;
406 this.cla = cla;
407 this.command = command;
408 this.p1 = p1;
409 this.p2 = p2;
410 this.p3 = p3;
411 this.data = data;
412 }
413 }
414
415 /**
Shishir Agrawal77ba3172015-09-10 14:50:19 -0700416 * A request object to use for transmitting data to an ICC.
417 */
418 private static final class ManualNetworkSelectionArgument {
419 public OperatorInfo operatorInfo;
420 public boolean persistSelection;
421
422 public ManualNetworkSelectionArgument(OperatorInfo operatorInfo, boolean persistSelection) {
423 this.operatorInfo = operatorInfo;
424 this.persistSelection = persistSelection;
425 }
426 }
427
428 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700429 * A request object for use with {@link MainThreadHandler}. Requesters should wait() on the
430 * request after sending. The main thread will notify the request when it is complete.
431 */
432 private static final class MainThreadRequest {
433 /** The argument to use for the request */
434 public Object argument;
435 /** The result of the request that is run on the main thread */
436 public Object result;
Sanket Padawe56e75a32016-02-08 12:18:19 -0800437 // The subscriber id that this request applies to. Defaults to
438 // SubscriptionManager.INVALID_SUBSCRIPTION_ID
439 public Integer subId = SubscriptionManager.INVALID_SUBSCRIPTION_ID;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700440
Nathan Harold92bed182018-10-12 18:16:49 -0700441 // In cases where subId is unavailable, the caller needs to specify the phone.
442 public Phone phone;
443
vagdeviaf9a5b92018-08-15 16:01:53 -0700444 public WorkSource workSource;
445
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700446 public MainThreadRequest(Object argument) {
447 this.argument = argument;
448 }
Shishir Agrawal76d5da92014-11-09 16:17:25 -0800449
Nathan Harold92bed182018-10-12 18:16:49 -0700450 MainThreadRequest(Object argument, Phone phone, WorkSource workSource) {
451 this.argument = argument;
452 if (phone != null) {
453 this.phone = phone;
454 }
455 this.workSource = workSource;
456 }
457
vagdeviaf9a5b92018-08-15 16:01:53 -0700458 MainThreadRequest(Object argument, Integer subId, WorkSource workSource) {
Shishir Agrawal76d5da92014-11-09 16:17:25 -0800459 this.argument = argument;
Sanket Padawe56e75a32016-02-08 12:18:19 -0800460 if (subId != null) {
461 this.subId = subId;
462 }
vagdeviaf9a5b92018-08-15 16:01:53 -0700463 this.workSource = workSource;
Shishir Agrawal76d5da92014-11-09 16:17:25 -0800464 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700465 }
466
Sailesh Nepalcc0375f2013-11-13 09:15:18 -0800467 private static final class IncomingThirdPartyCallArgs {
468 public final ComponentName component;
469 public final String callId;
470 public final String callerDisplayName;
471
472 public IncomingThirdPartyCallArgs(ComponentName component, String callId,
473 String callerDisplayName) {
474 this.component = component;
475 this.callId = callId;
476 this.callerDisplayName = callerDisplayName;
477 }
478 }
479
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700480 /**
481 * A handler that processes messages on the main thread in the phone process. Since many
482 * of the Phone calls are not thread safe this is needed to shuttle the requests from the
483 * inbound binder threads to the main thread in the phone process. The Binder thread
484 * may provide a {@link MainThreadRequest} object in the msg.obj field that they are waiting
485 * on, which will be notified when the operation completes and will contain the result of the
486 * request.
487 *
488 * <p>If a MainThreadRequest object is provided in the msg.obj field,
489 * note that request.result must be set to something non-null for the calling thread to
490 * unblock.
491 */
492 private final class MainThreadHandler extends Handler {
493 @Override
494 public void handleMessage(Message msg) {
495 MainThreadRequest request;
496 Message onCompleted;
497 AsyncResult ar;
Shishir Agrawalc04d9752016-02-19 10:41:00 -0800498 UiccCard uiccCard;
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700499 IccAPDUArgument iccArgument;
Nazanin Bakhshif782e562018-12-11 15:15:39 -0800500 final Phone defaultPhone = getDefaultPhone();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700501
502 switch (msg.what) {
Pengquan Menga1bb6272018-09-06 09:59:22 -0700503 case CMD_HANDLE_USSD_REQUEST: {
504 request = (MainThreadRequest) msg.obj;
505 final Phone phone = getPhoneFromRequest(request);
506 Pair<String, ResultReceiver> ussdObject = (Pair) request.argument;
507 String ussdRequest = ussdObject.first;
508 ResultReceiver wrappedCallback = ussdObject.second;
Tyler Gunn65d45c22017-06-05 11:22:26 -0700509
Pengquan Menga1bb6272018-09-06 09:59:22 -0700510 if (!isUssdApiAllowed(request.subId)) {
511 // Carrier does not support use of this API, return failure.
512 Rlog.w(LOG_TAG, "handleUssdRequest: carrier does not support USSD apis.");
513 UssdResponse response = new UssdResponse(ussdRequest, null);
514 Bundle returnData = new Bundle();
515 returnData.putParcelable(TelephonyManager.USSD_RESPONSE, response);
516 wrappedCallback.send(TelephonyManager.USSD_RETURN_FAILURE, returnData);
Tyler Gunn65d45c22017-06-05 11:22:26 -0700517
Pengquan Menga1bb6272018-09-06 09:59:22 -0700518 request.result = true;
519 notifyRequester(request);
520 return;
521 }
Tyler Gunn65d45c22017-06-05 11:22:26 -0700522
Pengquan Menga1bb6272018-09-06 09:59:22 -0700523 try {
524 request.result = phone != null
525 ? phone.handleUssdRequest(ussdRequest, wrappedCallback) : false;
526 } catch (CallStateException cse) {
527 request.result = false;
528 }
529 // Wake up the requesting thread
530 notifyRequester(request);
531 break;
pkanwar32d516d2016-10-14 19:37:38 -0700532 }
533
Yorke Lee716f67e2015-06-17 15:39:16 -0700534 case CMD_HANDLE_PIN_MMI: {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700535 request = (MainThreadRequest) msg.obj;
Yorke Lee716f67e2015-06-17 15:39:16 -0700536 final Phone phone = getPhoneFromRequest(request);
537 request.result = phone != null ?
538 getPhoneFromRequest(request).handlePinMmi((String) request.argument)
539 : false;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700540 // Wake up the requesting thread
Pengquan Menga1bb6272018-09-06 09:59:22 -0700541 notifyRequester(request);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700542 break;
Yorke Lee716f67e2015-06-17 15:39:16 -0700543 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700544
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700545 case CMD_TRANSMIT_APDU_LOGICAL_CHANNEL:
Shishir Agrawal566b7612013-10-28 14:41:00 -0700546 request = (MainThreadRequest) msg.obj;
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700547 iccArgument = (IccAPDUArgument) request.argument;
Shishir Agrawalc04d9752016-02-19 10:41:00 -0800548 uiccCard = getUiccCardFromRequest(request);
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700549 if (uiccCard == null) {
550 loge("iccTransmitApduLogicalChannel: No UICC");
551 request.result = new IccIoResult(0x6F, 0, (byte[])null);
Pengquan Menga1bb6272018-09-06 09:59:22 -0700552 notifyRequester(request);
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700553 } else {
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700554 onCompleted = obtainMessage(EVENT_TRANSMIT_APDU_LOGICAL_CHANNEL_DONE,
555 request);
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700556 uiccCard.iccTransmitApduLogicalChannel(
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700557 iccArgument.channel, iccArgument.cla, iccArgument.command,
558 iccArgument.p1, iccArgument.p2, iccArgument.p3, iccArgument.data,
Shishir Agrawal566b7612013-10-28 14:41:00 -0700559 onCompleted);
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700560 }
Shishir Agrawal566b7612013-10-28 14:41:00 -0700561 break;
562
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700563 case EVENT_TRANSMIT_APDU_LOGICAL_CHANNEL_DONE:
Shishir Agrawal566b7612013-10-28 14:41:00 -0700564 ar = (AsyncResult) msg.obj;
565 request = (MainThreadRequest) ar.userObj;
566 if (ar.exception == null && ar.result != null) {
567 request.result = ar.result;
568 } else {
569 request.result = new IccIoResult(0x6F, 0, (byte[])null);
570 if (ar.result == null) {
571 loge("iccTransmitApduLogicalChannel: Empty response");
Jake Hambye994d462014-02-03 13:10:13 -0800572 } else if (ar.exception instanceof CommandException) {
Shishir Agrawal566b7612013-10-28 14:41:00 -0700573 loge("iccTransmitApduLogicalChannel: CommandException: " +
Jake Hambye994d462014-02-03 13:10:13 -0800574 ar.exception);
Shishir Agrawal566b7612013-10-28 14:41:00 -0700575 } else {
576 loge("iccTransmitApduLogicalChannel: Unknown exception");
577 }
578 }
Pengquan Menga1bb6272018-09-06 09:59:22 -0700579 notifyRequester(request);
Shishir Agrawal566b7612013-10-28 14:41:00 -0700580 break;
581
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700582 case CMD_TRANSMIT_APDU_BASIC_CHANNEL:
583 request = (MainThreadRequest) msg.obj;
584 iccArgument = (IccAPDUArgument) request.argument;
Shishir Agrawalc04d9752016-02-19 10:41:00 -0800585 uiccCard = getUiccCardFromRequest(request);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700586 if (uiccCard == null) {
587 loge("iccTransmitApduBasicChannel: No UICC");
588 request.result = new IccIoResult(0x6F, 0, (byte[])null);
Pengquan Menga1bb6272018-09-06 09:59:22 -0700589 notifyRequester(request);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700590 } else {
591 onCompleted = obtainMessage(EVENT_TRANSMIT_APDU_BASIC_CHANNEL_DONE,
592 request);
593 uiccCard.iccTransmitApduBasicChannel(
594 iccArgument.cla, iccArgument.command, iccArgument.p1, iccArgument.p2,
595 iccArgument.p3, iccArgument.data, onCompleted);
596 }
597 break;
598
599 case EVENT_TRANSMIT_APDU_BASIC_CHANNEL_DONE:
600 ar = (AsyncResult) msg.obj;
601 request = (MainThreadRequest) ar.userObj;
602 if (ar.exception == null && ar.result != null) {
603 request.result = ar.result;
604 } else {
605 request.result = new IccIoResult(0x6F, 0, (byte[])null);
606 if (ar.result == null) {
607 loge("iccTransmitApduBasicChannel: Empty response");
608 } else if (ar.exception instanceof CommandException) {
609 loge("iccTransmitApduBasicChannel: CommandException: " +
610 ar.exception);
611 } else {
612 loge("iccTransmitApduBasicChannel: Unknown exception");
613 }
614 }
Pengquan Menga1bb6272018-09-06 09:59:22 -0700615 notifyRequester(request);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700616 break;
617
618 case CMD_EXCHANGE_SIM_IO:
619 request = (MainThreadRequest) msg.obj;
620 iccArgument = (IccAPDUArgument) request.argument;
Shishir Agrawalc04d9752016-02-19 10:41:00 -0800621 uiccCard = getUiccCardFromRequest(request);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700622 if (uiccCard == null) {
623 loge("iccExchangeSimIO: No UICC");
624 request.result = new IccIoResult(0x6F, 0, (byte[])null);
Pengquan Menga1bb6272018-09-06 09:59:22 -0700625 notifyRequester(request);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700626 } else {
627 onCompleted = obtainMessage(EVENT_EXCHANGE_SIM_IO_DONE,
628 request);
629 uiccCard.iccExchangeSimIO(iccArgument.cla, /* fileID */
630 iccArgument.command, iccArgument.p1, iccArgument.p2, iccArgument.p3,
631 iccArgument.data, onCompleted);
632 }
633 break;
634
635 case EVENT_EXCHANGE_SIM_IO_DONE:
636 ar = (AsyncResult) msg.obj;
637 request = (MainThreadRequest) ar.userObj;
638 if (ar.exception == null && ar.result != null) {
639 request.result = ar.result;
640 } else {
641 request.result = new IccIoResult(0x6f, 0, (byte[])null);
642 }
Pengquan Menga1bb6272018-09-06 09:59:22 -0700643 notifyRequester(request);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700644 break;
645
Derek Tan4d5e5c12014-02-04 11:54:58 -0800646 case CMD_SEND_ENVELOPE:
647 request = (MainThreadRequest) msg.obj;
Shishir Agrawalc04d9752016-02-19 10:41:00 -0800648 uiccCard = getUiccCardFromRequest(request);
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700649 if (uiccCard == null) {
650 loge("sendEnvelopeWithStatus: No UICC");
651 request.result = new IccIoResult(0x6F, 0, (byte[])null);
Pengquan Menga1bb6272018-09-06 09:59:22 -0700652 notifyRequester(request);
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700653 } else {
654 onCompleted = obtainMessage(EVENT_SEND_ENVELOPE_DONE, request);
655 uiccCard.sendEnvelopeWithStatus((String)request.argument, onCompleted);
656 }
Derek Tan4d5e5c12014-02-04 11:54:58 -0800657 break;
658
659 case EVENT_SEND_ENVELOPE_DONE:
660 ar = (AsyncResult) msg.obj;
661 request = (MainThreadRequest) ar.userObj;
Shishir Agrawal9f9877d2014-03-14 09:36:27 -0700662 if (ar.exception == null && ar.result != null) {
663 request.result = ar.result;
Derek Tan4d5e5c12014-02-04 11:54:58 -0800664 } else {
Shishir Agrawal9f9877d2014-03-14 09:36:27 -0700665 request.result = new IccIoResult(0x6F, 0, (byte[])null);
666 if (ar.result == null) {
667 loge("sendEnvelopeWithStatus: Empty response");
668 } else if (ar.exception instanceof CommandException) {
669 loge("sendEnvelopeWithStatus: CommandException: " +
670 ar.exception);
671 } else {
672 loge("sendEnvelopeWithStatus: exception:" + ar.exception);
673 }
Derek Tan4d5e5c12014-02-04 11:54:58 -0800674 }
Pengquan Menga1bb6272018-09-06 09:59:22 -0700675 notifyRequester(request);
Derek Tan4d5e5c12014-02-04 11:54:58 -0800676 break;
677
Shishir Agrawal566b7612013-10-28 14:41:00 -0700678 case CMD_OPEN_CHANNEL:
679 request = (MainThreadRequest) msg.obj;
Shishir Agrawalc04d9752016-02-19 10:41:00 -0800680 uiccCard = getUiccCardFromRequest(request);
Ajay Nambid7454d32015-12-03 13:50:00 -0800681 Pair<String, Integer> openChannelArgs = (Pair<String, Integer>) request.argument;
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700682 if (uiccCard == null) {
683 loge("iccOpenLogicalChannel: No UICC");
Shishir Agrawalfc0492a2016-02-17 11:15:33 -0800684 request.result = new IccOpenLogicalChannelResponse(-1,
685 IccOpenLogicalChannelResponse.STATUS_MISSING_RESOURCE, null);
Pengquan Menga1bb6272018-09-06 09:59:22 -0700686 notifyRequester(request);
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700687 } else {
688 onCompleted = obtainMessage(EVENT_OPEN_CHANNEL_DONE, request);
Ajay Nambid7454d32015-12-03 13:50:00 -0800689 uiccCard.iccOpenLogicalChannel(openChannelArgs.first,
690 openChannelArgs.second, onCompleted);
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700691 }
Shishir Agrawal566b7612013-10-28 14:41:00 -0700692 break;
693
694 case EVENT_OPEN_CHANNEL_DONE:
695 ar = (AsyncResult) msg.obj;
696 request = (MainThreadRequest) ar.userObj;
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700697 IccOpenLogicalChannelResponse openChannelResp;
Shishir Agrawal566b7612013-10-28 14:41:00 -0700698 if (ar.exception == null && ar.result != null) {
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700699 int[] result = (int[]) ar.result;
700 int channelId = result[0];
701 byte[] selectResponse = null;
702 if (result.length > 1) {
703 selectResponse = new byte[result.length - 1];
704 for (int i = 1; i < result.length; ++i) {
705 selectResponse[i - 1] = (byte) result[i];
706 }
707 }
708 openChannelResp = new IccOpenLogicalChannelResponse(channelId,
Shishir Agrawal527e8bf2014-08-25 08:54:56 -0700709 IccOpenLogicalChannelResponse.STATUS_NO_ERROR, selectResponse);
Shishir Agrawal566b7612013-10-28 14:41:00 -0700710 } else {
Shishir Agrawal566b7612013-10-28 14:41:00 -0700711 if (ar.result == null) {
712 loge("iccOpenLogicalChannel: Empty response");
Shishir Agrawal566b7612013-10-28 14:41:00 -0700713 }
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700714 if (ar.exception != null) {
715 loge("iccOpenLogicalChannel: Exception: " + ar.exception);
716 }
717
Shishir Agrawal527e8bf2014-08-25 08:54:56 -0700718 int errorCode = IccOpenLogicalChannelResponse.STATUS_UNKNOWN_ERROR;
Junda Liua754ba12015-05-20 01:17:52 -0700719 if (ar.exception instanceof CommandException) {
720 CommandException.Error error =
721 ((CommandException) (ar.exception)).getCommandError();
722 if (error == CommandException.Error.MISSING_RESOURCE) {
Shishir Agrawal527e8bf2014-08-25 08:54:56 -0700723 errorCode = IccOpenLogicalChannelResponse.STATUS_MISSING_RESOURCE;
Junda Liua754ba12015-05-20 01:17:52 -0700724 } else if (error == CommandException.Error.NO_SUCH_ELEMENT) {
Shishir Agrawal527e8bf2014-08-25 08:54:56 -0700725 errorCode = IccOpenLogicalChannelResponse.STATUS_NO_SUCH_ELEMENT;
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700726 }
727 }
728 openChannelResp = new IccOpenLogicalChannelResponse(
729 IccOpenLogicalChannelResponse.INVALID_CHANNEL, errorCode, null);
Shishir Agrawal566b7612013-10-28 14:41:00 -0700730 }
Shishir Agrawal82c8a462014-07-31 18:13:17 -0700731 request.result = openChannelResp;
Pengquan Menga1bb6272018-09-06 09:59:22 -0700732 notifyRequester(request);
Shishir Agrawal566b7612013-10-28 14:41:00 -0700733 break;
734
735 case CMD_CLOSE_CHANNEL:
736 request = (MainThreadRequest) msg.obj;
Shishir Agrawalc04d9752016-02-19 10:41:00 -0800737 uiccCard = getUiccCardFromRequest(request);
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700738 if (uiccCard == null) {
739 loge("iccCloseLogicalChannel: No UICC");
Yoshiaki Naka2e29d822016-09-02 19:27:39 +0900740 request.result = false;
Pengquan Menga1bb6272018-09-06 09:59:22 -0700741 notifyRequester(request);
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700742 } else {
743 onCompleted = obtainMessage(EVENT_CLOSE_CHANNEL_DONE, request);
744 uiccCard.iccCloseLogicalChannel((Integer) request.argument, onCompleted);
745 }
Shishir Agrawal566b7612013-10-28 14:41:00 -0700746 break;
747
748 case EVENT_CLOSE_CHANNEL_DONE:
Jake Hambye994d462014-02-03 13:10:13 -0800749 handleNullReturnEvent(msg, "iccCloseLogicalChannel");
750 break;
751
752 case CMD_NV_READ_ITEM:
753 request = (MainThreadRequest) msg.obj;
754 onCompleted = obtainMessage(EVENT_NV_READ_ITEM_DONE, request);
Nazanin Bakhshif782e562018-12-11 15:15:39 -0800755 defaultPhone.nvReadItem((Integer) request.argument, onCompleted,
756 request.workSource);
Jake Hambye994d462014-02-03 13:10:13 -0800757 break;
758
759 case EVENT_NV_READ_ITEM_DONE:
Shishir Agrawal566b7612013-10-28 14:41:00 -0700760 ar = (AsyncResult) msg.obj;
761 request = (MainThreadRequest) ar.userObj;
Jake Hambye994d462014-02-03 13:10:13 -0800762 if (ar.exception == null && ar.result != null) {
763 request.result = ar.result; // String
Shishir Agrawal566b7612013-10-28 14:41:00 -0700764 } else {
Jake Hambye994d462014-02-03 13:10:13 -0800765 request.result = "";
766 if (ar.result == null) {
767 loge("nvReadItem: Empty response");
768 } else if (ar.exception instanceof CommandException) {
769 loge("nvReadItem: CommandException: " +
770 ar.exception);
Shishir Agrawal566b7612013-10-28 14:41:00 -0700771 } else {
Jake Hambye994d462014-02-03 13:10:13 -0800772 loge("nvReadItem: Unknown exception");
Shishir Agrawal566b7612013-10-28 14:41:00 -0700773 }
774 }
Pengquan Menga1bb6272018-09-06 09:59:22 -0700775 notifyRequester(request);
Shishir Agrawal566b7612013-10-28 14:41:00 -0700776 break;
777
Jake Hambye994d462014-02-03 13:10:13 -0800778 case CMD_NV_WRITE_ITEM:
779 request = (MainThreadRequest) msg.obj;
780 onCompleted = obtainMessage(EVENT_NV_WRITE_ITEM_DONE, request);
781 Pair<Integer, String> idValue = (Pair<Integer, String>) request.argument;
Nazanin Bakhshif782e562018-12-11 15:15:39 -0800782 defaultPhone.nvWriteItem(idValue.first, idValue.second, onCompleted,
vagdeviaf9a5b92018-08-15 16:01:53 -0700783 request.workSource);
Jake Hambye994d462014-02-03 13:10:13 -0800784 break;
785
786 case EVENT_NV_WRITE_ITEM_DONE:
787 handleNullReturnEvent(msg, "nvWriteItem");
788 break;
789
790 case CMD_NV_WRITE_CDMA_PRL:
791 request = (MainThreadRequest) msg.obj;
792 onCompleted = obtainMessage(EVENT_NV_WRITE_CDMA_PRL_DONE, request);
Nazanin Bakhshif782e562018-12-11 15:15:39 -0800793 defaultPhone.nvWriteCdmaPrl((byte[]) request.argument, onCompleted);
Jake Hambye994d462014-02-03 13:10:13 -0800794 break;
795
796 case EVENT_NV_WRITE_CDMA_PRL_DONE:
797 handleNullReturnEvent(msg, "nvWriteCdmaPrl");
798 break;
799
chen xu6dac5ab2018-10-26 17:39:23 -0700800 case CMD_RESET_MODEM_CONFIG:
Jake Hambye994d462014-02-03 13:10:13 -0800801 request = (MainThreadRequest) msg.obj;
chen xu6dac5ab2018-10-26 17:39:23 -0700802 onCompleted = obtainMessage(EVENT_RESET_MODEM_CONFIG_DONE, request);
Nazanin Bakhshif782e562018-12-11 15:15:39 -0800803 defaultPhone.resetModemConfig(onCompleted);
Jake Hambye994d462014-02-03 13:10:13 -0800804 break;
805
chen xu6dac5ab2018-10-26 17:39:23 -0700806 case EVENT_RESET_MODEM_CONFIG_DONE:
807 handleNullReturnEvent(msg, "resetModemConfig");
Jake Hambye994d462014-02-03 13:10:13 -0800808 break;
809
Sooraj Sasindran37444802020-08-11 10:40:43 -0700810 case CMD_IS_NR_DUAL_CONNECTIVITY_ENABLED: {
811 request = (MainThreadRequest) msg.obj;
812 onCompleted = obtainMessage(EVENT_IS_NR_DUAL_CONNECTIVITY_ENABLED_DONE,
813 request);
814 Phone phone = getPhoneFromRequest(request);
815 if (phone != null) {
816 phone.isNrDualConnectivityEnabled(onCompleted, request.workSource);
817 } else {
818 loge("isNRDualConnectivityEnabled: No phone object");
819 request.result = false;
820 notifyRequester(request);
821 }
822 break;
823 }
824
825 case EVENT_IS_NR_DUAL_CONNECTIVITY_ENABLED_DONE:
826 ar = (AsyncResult) msg.obj;
827 request = (MainThreadRequest) ar.userObj;
828 if (ar.exception == null && ar.result != null) {
829 request.result = ar.result;
830 } else {
831 // request.result must be set to something non-null
832 // for the calling thread to unblock
833 if (request.result != null) {
834 request.result = ar.result;
835 } else {
836 request.result = false;
837 }
838 if (ar.result == null) {
839 loge("isNRDualConnectivityEnabled: Empty response");
840 } else if (ar.exception instanceof CommandException) {
841 loge("isNRDualConnectivityEnabled: CommandException: "
842 + ar.exception);
843 } else {
844 loge("isNRDualConnectivityEnabled: Unknown exception");
845 }
846 }
847 notifyRequester(request);
848 break;
849
850 case CMD_ENABLE_NR_DUAL_CONNECTIVITY: {
851 request = (MainThreadRequest) msg.obj;
852 onCompleted = obtainMessage(EVENT_ENABLE_NR_DUAL_CONNECTIVITY_DONE, request);
853 Phone phone = getPhoneFromRequest(request);
854 if (phone != null) {
855 phone.setNrDualConnectivityState((int) request.argument, onCompleted,
856 request.workSource);
857 } else {
858 loge("enableNrDualConnectivity: No phone object");
859 request.result =
860 TelephonyManager.ENABLE_NR_DUAL_CONNECTIVITY_RADIO_NOT_AVAILABLE;
861 notifyRequester(request);
862 }
863 break;
864 }
865
866 case EVENT_ENABLE_NR_DUAL_CONNECTIVITY_DONE: {
867 ar = (AsyncResult) msg.obj;
868 request = (MainThreadRequest) ar.userObj;
869 if (ar.exception == null) {
870 request.result =
871 TelephonyManager.ENABLE_NR_DUAL_CONNECTIVITY_SUCCESS;
872 } else {
873 request.result =
874 TelephonyManager
875 .ENABLE_NR_DUAL_CONNECTIVITY_RADIO_ERROR;
876 if (ar.exception instanceof CommandException) {
877 CommandException.Error error =
878 ((CommandException) (ar.exception)).getCommandError();
879 if (error == CommandException.Error.RADIO_NOT_AVAILABLE) {
880 request.result =
881 TelephonyManager
882 .ENABLE_NR_DUAL_CONNECTIVITY_RADIO_NOT_AVAILABLE;
Sooraj Sasindran29654162021-03-03 23:00:01 +0000883 } else if (error == CommandException.Error.REQUEST_NOT_SUPPORTED) {
884 request.result =
885 TelephonyManager
886 .ENABLE_NR_DUAL_CONNECTIVITY_NOT_SUPPORTED;
Sooraj Sasindran37444802020-08-11 10:40:43 -0700887 }
888 loge("enableNrDualConnectivity" + ": CommandException: "
889 + ar.exception);
890 } else {
891 loge("enableNrDualConnectivity" + ": Unknown exception");
892 }
893 }
894 notifyRequester(request);
895 break;
896 }
897
SongFerngWang3ef3e072020-12-21 16:41:52 +0800898 case CMD_GET_ALLOWED_NETWORK_TYPES_BITMASK:
Jake Hamby7c27be32014-03-03 13:25:59 -0800899 request = (MainThreadRequest) msg.obj;
SongFerngWang3ef3e072020-12-21 16:41:52 +0800900 onCompleted = obtainMessage(EVENT_GET_ALLOWED_NETWORK_TYPES_BITMASK_DONE,
901 request);
902 getPhoneFromRequest(request).getAllowedNetworkTypesBitmask(onCompleted);
Jake Hamby7c27be32014-03-03 13:25:59 -0800903 break;
904
SongFerngWang3ef3e072020-12-21 16:41:52 +0800905 case EVENT_GET_ALLOWED_NETWORK_TYPES_BITMASK_DONE:
Jake Hamby7c27be32014-03-03 13:25:59 -0800906 ar = (AsyncResult) msg.obj;
907 request = (MainThreadRequest) ar.userObj;
908 if (ar.exception == null && ar.result != null) {
909 request.result = ar.result; // Integer
910 } else {
Nazish Tabassume8ba43a2020-07-28 14:49:25 +0530911 // request.result must be set to something non-null
912 // for the calling thread to unblock
913 request.result = new int[]{-1};
Jake Hamby7c27be32014-03-03 13:25:59 -0800914 if (ar.result == null) {
SongFerngWang3ef3e072020-12-21 16:41:52 +0800915 loge("getAllowedNetworkTypesBitmask: Empty response");
Jake Hamby7c27be32014-03-03 13:25:59 -0800916 } else if (ar.exception instanceof CommandException) {
SongFerngWang3ef3e072020-12-21 16:41:52 +0800917 loge("getAllowedNetworkTypesBitmask: CommandException: "
918 + ar.exception);
Jake Hamby7c27be32014-03-03 13:25:59 -0800919 } else {
SongFerngWang3ef3e072020-12-21 16:41:52 +0800920 loge("getAllowedNetworkTypesBitmask: Unknown exception");
Jake Hamby7c27be32014-03-03 13:25:59 -0800921 }
922 }
Pengquan Menga1bb6272018-09-06 09:59:22 -0700923 notifyRequester(request);
Jake Hamby7c27be32014-03-03 13:25:59 -0800924 break;
925
SongFerngWang3ef3e072020-12-21 16:41:52 +0800926 case CMD_SET_ALLOWED_NETWORK_TYPES_FOR_REASON:
Jake Hamby7c27be32014-03-03 13:25:59 -0800927 request = (MainThreadRequest) msg.obj;
SongFerngWang3ef3e072020-12-21 16:41:52 +0800928 onCompleted = obtainMessage(EVENT_SET_ALLOWED_NETWORK_TYPES_FOR_REASON_DONE,
929 request);
930 Pair<Integer, Long> reasonWithNetworkTypes =
931 (Pair<Integer, Long>) request.argument;
932 getPhoneFromRequest(request).setAllowedNetworkTypes(
933 reasonWithNetworkTypes.first,
934 reasonWithNetworkTypes.second,
935 onCompleted);
Jake Hamby7c27be32014-03-03 13:25:59 -0800936 break;
937
SongFerngWang3ef3e072020-12-21 16:41:52 +0800938 case EVENT_SET_ALLOWED_NETWORK_TYPES_FOR_REASON_DONE:
939 handleNullReturnEvent(msg, "setAllowedNetworkTypesForReason");
Jake Hamby7c27be32014-03-03 13:25:59 -0800940 break;
941
Shuo Qian850e4d6a2018-04-25 21:02:08 +0000942 case CMD_INVOKE_OEM_RIL_REQUEST_RAW:
943 request = (MainThreadRequest)msg.obj;
944 onCompleted = obtainMessage(EVENT_INVOKE_OEM_RIL_REQUEST_RAW_DONE, request);
Nazanin Bakhshif782e562018-12-11 15:15:39 -0800945 defaultPhone.invokeOemRilRequestRaw((byte[]) request.argument, onCompleted);
Shuo Qian850e4d6a2018-04-25 21:02:08 +0000946 break;
947
948 case EVENT_INVOKE_OEM_RIL_REQUEST_RAW_DONE:
949 ar = (AsyncResult)msg.obj;
950 request = (MainThreadRequest)ar.userObj;
951 request.result = ar;
Pengquan Menga1bb6272018-09-06 09:59:22 -0700952 notifyRequester(request);
Shuo Qian850e4d6a2018-04-25 21:02:08 +0000953 break;
954
Shishir Agrawal76d5da92014-11-09 16:17:25 -0800955 case CMD_SET_VOICEMAIL_NUMBER:
956 request = (MainThreadRequest) msg.obj;
957 onCompleted = obtainMessage(EVENT_SET_VOICEMAIL_NUMBER_DONE, request);
958 Pair<String, String> tagNum = (Pair<String, String>) request.argument;
Stuart Scott584921c2015-01-15 17:10:34 -0800959 getPhoneFromRequest(request).setVoiceMailNumber(tagNum.first, tagNum.second,
960 onCompleted);
Shishir Agrawal76d5da92014-11-09 16:17:25 -0800961 break;
962
963 case EVENT_SET_VOICEMAIL_NUMBER_DONE:
964 handleNullReturnEvent(msg, "setVoicemailNumber");
965 break;
966
Stuart Scott54788802015-03-30 13:18:01 -0700967 case CMD_SET_NETWORK_SELECTION_MODE_AUTOMATIC:
968 request = (MainThreadRequest) msg.obj;
969 onCompleted = obtainMessage(EVENT_SET_NETWORK_SELECTION_MODE_AUTOMATIC_DONE,
970 request);
971 getPhoneFromRequest(request).setNetworkSelectionModeAutomatic(onCompleted);
972 break;
973
974 case EVENT_SET_NETWORK_SELECTION_MODE_AUTOMATIC_DONE:
975 handleNullReturnEvent(msg, "setNetworkSelectionModeAutomatic");
976 break;
977
Shishir Agrawal302c8692015-06-19 13:49:39 -0700978 case CMD_PERFORM_NETWORK_SCAN:
979 request = (MainThreadRequest) msg.obj;
980 onCompleted = obtainMessage(EVENT_PERFORM_NETWORK_SCAN_DONE, request);
981 getPhoneFromRequest(request).getAvailableNetworks(onCompleted);
982 break;
983
Hall Liu27d24262020-09-18 19:04:59 -0700984 case CMD_GET_CALL_FORWARDING: {
Shuo Qian4a594052020-01-23 11:59:30 -0800985 request = (MainThreadRequest) msg.obj;
986 onCompleted = obtainMessage(EVENT_GET_CALL_FORWARDING_DONE, request);
Hall Liu27d24262020-09-18 19:04:59 -0700987 Pair<Integer, TelephonyManager.CallForwardingInfoCallback> args =
988 (Pair<Integer, TelephonyManager.CallForwardingInfoCallback>)
989 request.argument;
990 int callForwardingReason = args.first;
991 request.phone.getCallForwardingOption(callForwardingReason, onCompleted);
Shuo Qian4a594052020-01-23 11:59:30 -0800992 break;
Hall Liu27d24262020-09-18 19:04:59 -0700993 }
994 case EVENT_GET_CALL_FORWARDING_DONE: {
Shuo Qian4a594052020-01-23 11:59:30 -0800995 ar = (AsyncResult) msg.obj;
996 request = (MainThreadRequest) ar.userObj;
Hall Liu27d24262020-09-18 19:04:59 -0700997 TelephonyManager.CallForwardingInfoCallback callback =
998 ((Pair<Integer, TelephonyManager.CallForwardingInfoCallback>)
999 request.argument).second;
Shuo Qian4a594052020-01-23 11:59:30 -08001000 if (ar.exception == null && ar.result != null) {
Hall Liu27d24262020-09-18 19:04:59 -07001001 CallForwardingInfo callForwardingInfo = null;
Shuo Qian4a594052020-01-23 11:59:30 -08001002 CallForwardInfo[] callForwardInfos = (CallForwardInfo[]) ar.result;
1003 for (CallForwardInfo callForwardInfo : callForwardInfos) {
1004 // Service Class is a bit mask per 3gpp 27.007. Search for
1005 // any service for voice call.
1006 if ((callForwardInfo.serviceClass
1007 & CommandsInterface.SERVICE_CLASS_VOICE) > 0) {
Hall Liu27d24262020-09-18 19:04:59 -07001008 callForwardingInfo = new CallForwardingInfo(true,
1009 callForwardInfo.reason,
1010 callForwardInfo.number,
1011 callForwardInfo.timeSeconds);
Shuo Qian4a594052020-01-23 11:59:30 -08001012 break;
1013 }
1014 }
1015 // Didn't find a call forward info for voice call.
1016 if (callForwardingInfo == null) {
Hall Liu27d24262020-09-18 19:04:59 -07001017 callForwardingInfo = new CallForwardingInfo(false /* enabled */,
1018 0 /* reason */, null /* number */, 0 /* timeout */);
Shuo Qian4a594052020-01-23 11:59:30 -08001019 }
Hall Liu27d24262020-09-18 19:04:59 -07001020 callback.onCallForwardingInfoAvailable(callForwardingInfo);
Shuo Qian4a594052020-01-23 11:59:30 -08001021 } else {
1022 if (ar.result == null) {
1023 loge("EVENT_GET_CALL_FORWARDING_DONE: Empty response");
1024 }
1025 if (ar.exception != null) {
1026 loge("EVENT_GET_CALL_FORWARDING_DONE: Exception: " + ar.exception);
1027 }
Hall Liu940c4ca2020-09-29 17:10:18 -07001028 int errorCode = TelephonyManager
1029 .CallForwardingInfoCallback.RESULT_ERROR_UNKNOWN;
Shuo Qian4a594052020-01-23 11:59:30 -08001030 if (ar.exception instanceof CommandException) {
1031 CommandException.Error error =
1032 ((CommandException) (ar.exception)).getCommandError();
1033 if (error == CommandException.Error.FDN_CHECK_FAILURE) {
Hall Liu940c4ca2020-09-29 17:10:18 -07001034 errorCode = TelephonyManager
1035 .CallForwardingInfoCallback.RESULT_ERROR_FDN_CHECK_FAILURE;
Shuo Qian4a594052020-01-23 11:59:30 -08001036 } else if (error == CommandException.Error.REQUEST_NOT_SUPPORTED) {
Hall Liu940c4ca2020-09-29 17:10:18 -07001037 errorCode = TelephonyManager
1038 .CallForwardingInfoCallback.RESULT_ERROR_NOT_SUPPORTED;
Shuo Qian4a594052020-01-23 11:59:30 -08001039 }
1040 }
Hall Liu27d24262020-09-18 19:04:59 -07001041 callback.onError(errorCode);
Shuo Qian4a594052020-01-23 11:59:30 -08001042 }
Shuo Qian4a594052020-01-23 11:59:30 -08001043 break;
Hall Liu27d24262020-09-18 19:04:59 -07001044 }
Shuo Qian4a594052020-01-23 11:59:30 -08001045
Hall Liu27d24262020-09-18 19:04:59 -07001046 case CMD_SET_CALL_FORWARDING: {
Shuo Qian4a594052020-01-23 11:59:30 -08001047 request = (MainThreadRequest) msg.obj;
1048 onCompleted = obtainMessage(EVENT_SET_CALL_FORWARDING_DONE, request);
Hall Liu27d24262020-09-18 19:04:59 -07001049 request = (MainThreadRequest) msg.obj;
Shuo Qian4a594052020-01-23 11:59:30 -08001050 CallForwardingInfo callForwardingInfoToSet =
Hall Liu27d24262020-09-18 19:04:59 -07001051 ((Pair<CallForwardingInfo, Consumer<Integer>>)
1052 request.argument).first;
1053 request.phone.setCallForwardingOption(
1054 callForwardingInfoToSet.isEnabled()
1055 ? CommandsInterface.CF_ACTION_ENABLE
1056 : CommandsInterface.CF_ACTION_DISABLE,
Shuo Qian4a594052020-01-23 11:59:30 -08001057 callForwardingInfoToSet.getReason(),
1058 callForwardingInfoToSet.getNumber(),
1059 callForwardingInfoToSet.getTimeoutSeconds(), onCompleted);
1060 break;
Hall Liu27d24262020-09-18 19:04:59 -07001061 }
Shuo Qian4a594052020-01-23 11:59:30 -08001062
Hall Liu27d24262020-09-18 19:04:59 -07001063 case EVENT_SET_CALL_FORWARDING_DONE: {
Shuo Qian4a594052020-01-23 11:59:30 -08001064 ar = (AsyncResult) msg.obj;
1065 request = (MainThreadRequest) ar.userObj;
Hall Liu27d24262020-09-18 19:04:59 -07001066 Consumer<Integer> callback =
1067 ((Pair<CallForwardingInfo, Consumer<Integer>>)
1068 request.argument).second;
1069 if (ar.exception != null) {
Shuo Qian4a594052020-01-23 11:59:30 -08001070 loge("setCallForwarding exception: " + ar.exception);
Hall Liu940c4ca2020-09-29 17:10:18 -07001071 int errorCode = TelephonyManager.CallForwardingInfoCallback
1072 .RESULT_ERROR_UNKNOWN;
Hall Liu27d24262020-09-18 19:04:59 -07001073 if (ar.exception instanceof CommandException) {
1074 CommandException.Error error =
1075 ((CommandException) (ar.exception)).getCommandError();
1076 if (error == CommandException.Error.FDN_CHECK_FAILURE) {
Hall Liu940c4ca2020-09-29 17:10:18 -07001077 errorCode = TelephonyManager.CallForwardingInfoCallback
1078 .RESULT_ERROR_FDN_CHECK_FAILURE;
Hall Liu27d24262020-09-18 19:04:59 -07001079 } else if (error == CommandException.Error.REQUEST_NOT_SUPPORTED) {
Hall Liu940c4ca2020-09-29 17:10:18 -07001080 errorCode = TelephonyManager.CallForwardingInfoCallback
1081 .RESULT_ERROR_NOT_SUPPORTED;
Hall Liu27d24262020-09-18 19:04:59 -07001082 }
1083 }
1084 callback.accept(errorCode);
1085 } else {
Hall Liu940c4ca2020-09-29 17:10:18 -07001086 callback.accept(TelephonyManager.CallForwardingInfoCallback.RESULT_SUCCESS);
Shuo Qian4a594052020-01-23 11:59:30 -08001087 }
Shuo Qian4a594052020-01-23 11:59:30 -08001088 break;
Hall Liu27d24262020-09-18 19:04:59 -07001089 }
Shuo Qian4a594052020-01-23 11:59:30 -08001090
Hall Liu27d24262020-09-18 19:04:59 -07001091 case CMD_GET_CALL_WAITING: {
Shuo Qian4a594052020-01-23 11:59:30 -08001092 request = (MainThreadRequest) msg.obj;
1093 onCompleted = obtainMessage(EVENT_GET_CALL_WAITING_DONE, request);
1094 getPhoneFromRequest(request).getCallWaiting(onCompleted);
1095 break;
Hall Liu27d24262020-09-18 19:04:59 -07001096 }
Shuo Qian4a594052020-01-23 11:59:30 -08001097
Hall Liu27d24262020-09-18 19:04:59 -07001098 case EVENT_GET_CALL_WAITING_DONE: {
Shuo Qian4a594052020-01-23 11:59:30 -08001099 ar = (AsyncResult) msg.obj;
1100 request = (MainThreadRequest) ar.userObj;
Hall Liu27d24262020-09-18 19:04:59 -07001101 Consumer<Integer> callback = (Consumer<Integer>) request.argument;
Shuo Qian4a594052020-01-23 11:59:30 -08001102 int callForwardingStatus = TelephonyManager.CALL_WAITING_STATUS_UNKNOWN_ERROR;
1103 if (ar.exception == null && ar.result != null) {
Shuo Qiand6a0dba2020-02-18 18:13:49 -08001104 int[] callForwardResults = (int[]) ar.result;
Shuo Qian4a594052020-01-23 11:59:30 -08001105 // Service Class is a bit mask per 3gpp 27.007.
1106 // Search for any service for voice call.
Shuo Qiand6a0dba2020-02-18 18:13:49 -08001107 if (callForwardResults.length > 1
1108 && ((callForwardResults[1]
Hall Liu27d24262020-09-18 19:04:59 -07001109 & CommandsInterface.SERVICE_CLASS_VOICE) > 0)) {
Shuo Qiand6a0dba2020-02-18 18:13:49 -08001110 callForwardingStatus = callForwardResults[0] == 0
Hall Liu27d24262020-09-18 19:04:59 -07001111 ? TelephonyManager.CALL_WAITING_STATUS_DISABLED
1112 : TelephonyManager.CALL_WAITING_STATUS_ENABLED;
Shuo Qian4a594052020-01-23 11:59:30 -08001113 } else {
Hall Liu27d24262020-09-18 19:04:59 -07001114 callForwardingStatus = TelephonyManager.CALL_WAITING_STATUS_DISABLED;
Shuo Qian4a594052020-01-23 11:59:30 -08001115 }
1116 } else {
1117 if (ar.result == null) {
1118 loge("EVENT_GET_CALL_WAITING_DONE: Empty response");
1119 }
1120 if (ar.exception != null) {
1121 loge("EVENT_GET_CALL_WAITING_DONE: Exception: " + ar.exception);
1122 }
1123 if (ar.exception instanceof CommandException) {
1124 CommandException.Error error =
1125 ((CommandException) (ar.exception)).getCommandError();
1126 if (error == CommandException.Error.REQUEST_NOT_SUPPORTED) {
1127 callForwardingStatus =
1128 TelephonyManager.CALL_WAITING_STATUS_NOT_SUPPORTED;
1129 }
1130 }
1131 }
Hall Liu27d24262020-09-18 19:04:59 -07001132 callback.accept(callForwardingStatus);
Shuo Qian4a594052020-01-23 11:59:30 -08001133 break;
Hall Liu27d24262020-09-18 19:04:59 -07001134 }
Shuo Qian4a594052020-01-23 11:59:30 -08001135
Hall Liu27d24262020-09-18 19:04:59 -07001136 case CMD_SET_CALL_WAITING: {
Shuo Qian4a594052020-01-23 11:59:30 -08001137 request = (MainThreadRequest) msg.obj;
1138 onCompleted = obtainMessage(EVENT_SET_CALL_WAITING_DONE, request);
Hall Liu27d24262020-09-18 19:04:59 -07001139 boolean enable = ((Pair<Boolean, Consumer<Integer>>) request.argument).first;
1140 getPhoneFromRequest(request).setCallWaiting(enable, onCompleted);
Shuo Qian4a594052020-01-23 11:59:30 -08001141 break;
Hall Liu27d24262020-09-18 19:04:59 -07001142 }
Shuo Qian4a594052020-01-23 11:59:30 -08001143
Hall Liu27d24262020-09-18 19:04:59 -07001144 case EVENT_SET_CALL_WAITING_DONE: {
Shuo Qian4a594052020-01-23 11:59:30 -08001145 ar = (AsyncResult) msg.obj;
1146 request = (MainThreadRequest) ar.userObj;
Hall Liu27d24262020-09-18 19:04:59 -07001147 boolean enable = ((Pair<Boolean, Consumer<Integer>>) request.argument).first;
1148 Consumer<Integer> callback =
1149 ((Pair<Boolean, Consumer<Integer>>) request.argument).second;
1150 if (ar.exception != null) {
Shuo Qian4a594052020-01-23 11:59:30 -08001151 loge("setCallWaiting exception: " + ar.exception);
Hall Liu27d24262020-09-18 19:04:59 -07001152 if (ar.exception instanceof CommandException) {
1153 CommandException.Error error =
1154 ((CommandException) (ar.exception)).getCommandError();
1155 if (error == CommandException.Error.REQUEST_NOT_SUPPORTED) {
1156 callback.accept(TelephonyManager.CALL_WAITING_STATUS_NOT_SUPPORTED);
1157 } else {
1158 callback.accept(TelephonyManager.CALL_WAITING_STATUS_UNKNOWN_ERROR);
1159 }
1160 } else {
1161 callback.accept(TelephonyManager.CALL_WAITING_STATUS_UNKNOWN_ERROR);
1162 }
1163 } else {
1164 callback.accept(enable ? TelephonyManager.CALL_WAITING_STATUS_ENABLED
1165 : TelephonyManager.CALL_WAITING_STATUS_DISABLED);
Shuo Qian4a594052020-01-23 11:59:30 -08001166 }
Shuo Qian4a594052020-01-23 11:59:30 -08001167 break;
Hall Liu27d24262020-09-18 19:04:59 -07001168 }
Shishir Agrawal302c8692015-06-19 13:49:39 -07001169 case EVENT_PERFORM_NETWORK_SCAN_DONE:
1170 ar = (AsyncResult) msg.obj;
1171 request = (MainThreadRequest) ar.userObj;
1172 CellNetworkScanResult cellScanResult;
1173 if (ar.exception == null && ar.result != null) {
1174 cellScanResult = new CellNetworkScanResult(
1175 CellNetworkScanResult.STATUS_SUCCESS,
1176 (List<OperatorInfo>) ar.result);
1177 } else {
1178 if (ar.result == null) {
1179 loge("getCellNetworkScanResults: Empty response");
1180 }
1181 if (ar.exception != null) {
1182 loge("getCellNetworkScanResults: Exception: " + ar.exception);
1183 }
1184 int errorCode = CellNetworkScanResult.STATUS_UNKNOWN_ERROR;
1185 if (ar.exception instanceof CommandException) {
1186 CommandException.Error error =
1187 ((CommandException) (ar.exception)).getCommandError();
1188 if (error == CommandException.Error.RADIO_NOT_AVAILABLE) {
1189 errorCode = CellNetworkScanResult.STATUS_RADIO_NOT_AVAILABLE;
1190 } else if (error == CommandException.Error.GENERIC_FAILURE) {
1191 errorCode = CellNetworkScanResult.STATUS_RADIO_GENERIC_FAILURE;
1192 }
1193 }
1194 cellScanResult = new CellNetworkScanResult(errorCode, null);
1195 }
1196 request.result = cellScanResult;
Pengquan Menga1bb6272018-09-06 09:59:22 -07001197 notifyRequester(request);
Shishir Agrawal302c8692015-06-19 13:49:39 -07001198 break;
1199
1200 case CMD_SET_NETWORK_SELECTION_MODE_MANUAL:
1201 request = (MainThreadRequest) msg.obj;
Shishir Agrawal77ba3172015-09-10 14:50:19 -07001202 ManualNetworkSelectionArgument selArg =
1203 (ManualNetworkSelectionArgument) request.argument;
Shishir Agrawal302c8692015-06-19 13:49:39 -07001204 onCompleted = obtainMessage(EVENT_SET_NETWORK_SELECTION_MODE_MANUAL_DONE,
1205 request);
Shishir Agrawal77ba3172015-09-10 14:50:19 -07001206 getPhoneFromRequest(request).selectNetworkManually(selArg.operatorInfo,
1207 selArg.persistSelection, onCompleted);
Shishir Agrawal302c8692015-06-19 13:49:39 -07001208 break;
1209
1210 case EVENT_SET_NETWORK_SELECTION_MODE_MANUAL_DONE:
Pengquan Menge3d01e22018-09-20 15:25:35 -07001211 ar = (AsyncResult) msg.obj;
1212 request = (MainThreadRequest) ar.userObj;
1213 if (ar.exception == null) {
1214 request.result = true;
1215 } else {
1216 request.result = false;
1217 loge("setNetworkSelectionModeManual " + ar.exception);
1218 }
1219 notifyRequester(request);
1220 mApp.onNetworkSelectionChanged(request.subId);
Shishir Agrawal302c8692015-06-19 13:49:39 -07001221 break;
1222
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -07001223 case CMD_GET_MODEM_ACTIVITY_INFO:
1224 request = (MainThreadRequest) msg.obj;
1225 onCompleted = obtainMessage(EVENT_GET_MODEM_ACTIVITY_INFO_DONE, request);
James Mattisab947702019-04-03 14:18:34 -07001226 if (defaultPhone != null) {
1227 defaultPhone.getModemActivityInfo(onCompleted, request.workSource);
Shuo Qian8f4750a2020-02-20 17:12:10 -08001228 } else {
1229 ResultReceiver result = (ResultReceiver) request.argument;
1230 Bundle bundle = new Bundle();
1231 bundle.putParcelable(TelephonyManager.MODEM_ACTIVITY_RESULT_KEY,
Hall Liu49656c02020-10-09 19:00:11 -07001232 new ModemActivityInfo(0, 0, 0,
1233 new int[ModemActivityInfo.getNumTxPowerLevels()], 0));
Shuo Qian8f4750a2020-02-20 17:12:10 -08001234 result.send(0, bundle);
James Mattisab947702019-04-03 14:18:34 -07001235 }
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -07001236 break;
1237
Hall Liud0f208c2020-10-14 16:54:44 -07001238 case EVENT_GET_MODEM_ACTIVITY_INFO_DONE: {
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -07001239 ar = (AsyncResult) msg.obj;
1240 request = (MainThreadRequest) ar.userObj;
Shuo Qian8f4750a2020-02-20 17:12:10 -08001241 ResultReceiver result = (ResultReceiver) request.argument;
1242
Hall Liud0f208c2020-10-14 16:54:44 -07001243 ModemActivityInfo ret = null;
1244 int error = 0;
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -07001245 if (ar.exception == null && ar.result != null) {
Shuo Qian8f4750a2020-02-20 17:12:10 -08001246 // Update the last modem activity info and the result of the request.
1247 ModemActivityInfo info = (ModemActivityInfo) ar.result;
1248 if (isModemActivityInfoValid(info)) {
Hall Liu49656c02020-10-09 19:00:11 -07001249 int[] mergedTxTimeMs = new int[ModemActivityInfo.getNumTxPowerLevels()];
Shuo Qian8f4750a2020-02-20 17:12:10 -08001250 int[] txTimeMs = info.getTransmitTimeMillis();
1251 int[] lastModemTxTimeMs = mLastModemActivityInfo
1252 .getTransmitTimeMillis();
1253 for (int i = 0; i < mergedTxTimeMs.length; i++) {
1254 mergedTxTimeMs[i] = txTimeMs[i] + lastModemTxTimeMs[i];
1255 }
Hall Liu49656c02020-10-09 19:00:11 -07001256 mLastModemActivityInfo.setTimestamp(info.getTimestampMillis());
Shuo Qian8f4750a2020-02-20 17:12:10 -08001257 mLastModemActivityInfo.setSleepTimeMillis(info.getSleepTimeMillis()
1258 + mLastModemActivityInfo.getSleepTimeMillis());
1259 mLastModemActivityInfo.setIdleTimeMillis(info.getIdleTimeMillis()
1260 + mLastModemActivityInfo.getIdleTimeMillis());
1261 mLastModemActivityInfo.setTransmitTimeMillis(mergedTxTimeMs);
1262 mLastModemActivityInfo.setReceiveTimeMillis(
1263 info.getReceiveTimeMillis()
1264 + mLastModemActivityInfo.getReceiveTimeMillis());
1265 }
Hall Liu49656c02020-10-09 19:00:11 -07001266 ret = new ModemActivityInfo(mLastModemActivityInfo.getTimestampMillis(),
Shuo Qian8f4750a2020-02-20 17:12:10 -08001267 mLastModemActivityInfo.getSleepTimeMillis(),
1268 mLastModemActivityInfo.getIdleTimeMillis(),
1269 mLastModemActivityInfo.getTransmitTimeMillis(),
1270 mLastModemActivityInfo.getReceiveTimeMillis());
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -07001271 } else {
1272 if (ar.result == null) {
1273 loge("queryModemActivityInfo: Empty response");
Hall Liud0f208c2020-10-14 16:54:44 -07001274 error = TelephonyManager.ModemActivityInfoException
1275 .ERROR_INVALID_INFO_RECEIVED;
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -07001276 } else if (ar.exception instanceof CommandException) {
1277 loge("queryModemActivityInfo: CommandException: " +
1278 ar.exception);
Hall Liud0f208c2020-10-14 16:54:44 -07001279 error = TelephonyManager.ModemActivityInfoException
1280 .ERROR_MODEM_RESPONSE_ERROR;
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -07001281 } else {
1282 loge("queryModemActivityInfo: Unknown exception");
Hall Liud0f208c2020-10-14 16:54:44 -07001283 error = TelephonyManager.ModemActivityInfoException
1284 .ERROR_UNKNOWN;
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -07001285 }
1286 }
Shuo Qian8f4750a2020-02-20 17:12:10 -08001287 Bundle bundle = new Bundle();
Hall Liud0f208c2020-10-14 16:54:44 -07001288 if (ret != null) {
1289 bundle.putParcelable(TelephonyManager.MODEM_ACTIVITY_RESULT_KEY, ret);
1290 } else {
1291 bundle.putInt(TelephonyManager.EXCEPTION_RESULT_KEY, error);
1292 }
Shuo Qian8f4750a2020-02-20 17:12:10 -08001293 result.send(0, bundle);
Pengquan Menga1bb6272018-09-06 09:59:22 -07001294 notifyRequester(request);
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -07001295 break;
Hall Liud0f208c2020-10-14 16:54:44 -07001296 }
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -07001297
Meng Wang1a7c35a2016-05-05 20:56:15 -07001298 case CMD_SET_ALLOWED_CARRIERS:
1299 request = (MainThreadRequest) msg.obj;
Michele Berionne482f8202018-11-27 18:57:59 -08001300 CarrierRestrictionRules argument =
1301 (CarrierRestrictionRules) request.argument;
Meng Wang1a7c35a2016-05-05 20:56:15 -07001302 onCompleted = obtainMessage(EVENT_SET_ALLOWED_CARRIERS_DONE, request);
Michele Berionne482f8202018-11-27 18:57:59 -08001303 defaultPhone.setAllowedCarriers(argument, onCompleted, request.workSource);
Meng Wang1a7c35a2016-05-05 20:56:15 -07001304 break;
1305
1306 case EVENT_SET_ALLOWED_CARRIERS_DONE:
1307 ar = (AsyncResult) msg.obj;
1308 request = (MainThreadRequest) ar.userObj;
1309 if (ar.exception == null && ar.result != null) {
1310 request.result = ar.result;
1311 } else {
Michele Berionne482f8202018-11-27 18:57:59 -08001312 request.result = TelephonyManager.SET_CARRIER_RESTRICTION_ERROR;
1313 if (ar.exception instanceof CommandException) {
1314 loge("setAllowedCarriers: CommandException: " + ar.exception);
1315 CommandException.Error error =
1316 ((CommandException) (ar.exception)).getCommandError();
1317 if (error == CommandException.Error.REQUEST_NOT_SUPPORTED) {
1318 request.result =
1319 TelephonyManager.SET_CARRIER_RESTRICTION_NOT_SUPPORTED;
1320 }
Meng Wang1a7c35a2016-05-05 20:56:15 -07001321 } else {
1322 loge("setAllowedCarriers: Unknown exception");
1323 }
1324 }
Pengquan Menga1bb6272018-09-06 09:59:22 -07001325 notifyRequester(request);
Meng Wang1a7c35a2016-05-05 20:56:15 -07001326 break;
1327
1328 case CMD_GET_ALLOWED_CARRIERS:
1329 request = (MainThreadRequest) msg.obj;
1330 onCompleted = obtainMessage(EVENT_GET_ALLOWED_CARRIERS_DONE, request);
Nazanin Bakhshif782e562018-12-11 15:15:39 -08001331 defaultPhone.getAllowedCarriers(onCompleted, request.workSource);
Meng Wang1a7c35a2016-05-05 20:56:15 -07001332 break;
1333
1334 case EVENT_GET_ALLOWED_CARRIERS_DONE:
1335 ar = (AsyncResult) msg.obj;
1336 request = (MainThreadRequest) ar.userObj;
1337 if (ar.exception == null && ar.result != null) {
1338 request.result = ar.result;
1339 } else {
Michele Berionne482f8202018-11-27 18:57:59 -08001340 request.result = new IllegalStateException(
1341 "Failed to get carrier restrictions");
Meng Wang1a7c35a2016-05-05 20:56:15 -07001342 if (ar.result == null) {
1343 loge("getAllowedCarriers: Empty response");
1344 } else if (ar.exception instanceof CommandException) {
1345 loge("getAllowedCarriers: CommandException: " +
1346 ar.exception);
1347 } else {
1348 loge("getAllowedCarriers: Unknown exception");
1349 }
1350 }
Pengquan Menga1bb6272018-09-06 09:59:22 -07001351 notifyRequester(request);
Meng Wang1a7c35a2016-05-05 20:56:15 -07001352 break;
1353
Nathan Haroldb3014052017-01-25 15:57:32 -08001354 case EVENT_GET_FORBIDDEN_PLMNS_DONE:
1355 ar = (AsyncResult) msg.obj;
1356 request = (MainThreadRequest) ar.userObj;
1357 if (ar.exception == null && ar.result != null) {
1358 request.result = ar.result;
1359 } else {
1360 request.result = new IllegalArgumentException(
1361 "Failed to retrieve Forbidden Plmns");
1362 if (ar.result == null) {
1363 loge("getForbiddenPlmns: Empty response");
1364 } else {
1365 loge("getForbiddenPlmns: Unknown exception");
1366 }
1367 }
Pengquan Menga1bb6272018-09-06 09:59:22 -07001368 notifyRequester(request);
Nathan Haroldb3014052017-01-25 15:57:32 -08001369 break;
1370
1371 case CMD_GET_FORBIDDEN_PLMNS:
1372 request = (MainThreadRequest) msg.obj;
1373 uiccCard = getUiccCardFromRequest(request);
1374 if (uiccCard == null) {
1375 loge("getForbiddenPlmns() UiccCard is null");
1376 request.result = new IllegalArgumentException(
1377 "getForbiddenPlmns() UiccCard is null");
Pengquan Menga1bb6272018-09-06 09:59:22 -07001378 notifyRequester(request);
Nathan Haroldb3014052017-01-25 15:57:32 -08001379 break;
1380 }
1381 Integer appType = (Integer) request.argument;
1382 UiccCardApplication uiccApp = uiccCard.getApplicationByType(appType);
1383 if (uiccApp == null) {
1384 loge("getForbiddenPlmns() no app with specified type -- "
1385 + appType);
1386 request.result = new IllegalArgumentException("Failed to get UICC App");
Pengquan Menga1bb6272018-09-06 09:59:22 -07001387 notifyRequester(request);
Nathan Haroldb3014052017-01-25 15:57:32 -08001388 break;
1389 } else {
1390 if (DBG) logv("getForbiddenPlmns() found app " + uiccApp.getAid()
1391 + " specified type -- " + appType);
1392 }
1393 onCompleted = obtainMessage(EVENT_GET_FORBIDDEN_PLMNS_DONE, request);
1394 ((SIMRecords) uiccApp.getIccRecords()).getForbiddenPlmns(
1395 onCompleted);
1396 break;
1397
Holly Jiuyu Sun01c47ad2018-01-24 17:56:33 +00001398 case CMD_SWITCH_SLOTS:
1399 request = (MainThreadRequest) msg.obj;
1400 int[] physicalSlots = (int[]) request.argument;
1401 onCompleted = obtainMessage(EVENT_SWITCH_SLOTS_DONE, request);
1402 UiccController.getInstance().switchSlots(physicalSlots, onCompleted);
1403 break;
1404
1405 case EVENT_SWITCH_SLOTS_DONE:
1406 ar = (AsyncResult) msg.obj;
1407 request = (MainThreadRequest) ar.userObj;
1408 request.result = (ar.exception == null);
Pengquan Menga1bb6272018-09-06 09:59:22 -07001409 notifyRequester(request);
1410 break;
1411 case CMD_GET_NETWORK_SELECTION_MODE:
1412 request = (MainThreadRequest) msg.obj;
1413 onCompleted = obtainMessage(EVENT_GET_NETWORK_SELECTION_MODE_DONE, request);
1414 getPhoneFromRequest(request).getNetworkSelectionMode(onCompleted);
1415 break;
1416
1417 case EVENT_GET_NETWORK_SELECTION_MODE_DONE:
1418 ar = (AsyncResult) msg.obj;
1419 request = (MainThreadRequest) ar.userObj;
1420 if (ar.exception != null) {
1421 request.result = TelephonyManager.NETWORK_SELECTION_MODE_UNKNOWN;
1422 } else {
1423 int mode = ((int[]) ar.result)[0];
1424 if (mode == 0) {
1425 request.result = TelephonyManager.NETWORK_SELECTION_MODE_AUTO;
1426 } else {
1427 request.result = TelephonyManager.NETWORK_SELECTION_MODE_MANUAL;
1428 }
Holly Jiuyu Sun01c47ad2018-01-24 17:56:33 +00001429 }
Pengquan Menga1bb6272018-09-06 09:59:22 -07001430 notifyRequester(request);
1431 break;
1432 case CMD_GET_CDMA_ROAMING_MODE:
1433 request = (MainThreadRequest) msg.obj;
1434 onCompleted = obtainMessage(EVENT_GET_CDMA_ROAMING_MODE_DONE, request);
1435 getPhoneFromRequest(request).queryCdmaRoamingPreference(onCompleted);
1436 break;
1437 case EVENT_GET_CDMA_ROAMING_MODE_DONE:
1438 ar = (AsyncResult) msg.obj;
1439 request = (MainThreadRequest) ar.userObj;
1440 if (ar.exception != null) {
1441 request.result = TelephonyManager.CDMA_ROAMING_MODE_RADIO_DEFAULT;
1442 } else {
1443 request.result = ((int[]) ar.result)[0];
1444 }
1445 notifyRequester(request);
1446 break;
1447 case CMD_SET_CDMA_ROAMING_MODE:
1448 request = (MainThreadRequest) msg.obj;
1449 onCompleted = obtainMessage(EVENT_SET_CDMA_ROAMING_MODE_DONE, request);
1450 int mode = (int) request.argument;
1451 getPhoneFromRequest(request).setCdmaRoamingPreference(mode, onCompleted);
1452 break;
1453 case EVENT_SET_CDMA_ROAMING_MODE_DONE:
1454 ar = (AsyncResult) msg.obj;
1455 request = (MainThreadRequest) ar.userObj;
1456 request.result = ar.exception == null;
1457 notifyRequester(request);
1458 break;
Sarah Chinbaab1432020-10-28 13:46:24 -07001459 case CMD_GET_CDMA_SUBSCRIPTION_MODE:
1460 request = (MainThreadRequest) msg.obj;
1461 onCompleted = obtainMessage(EVENT_GET_CDMA_SUBSCRIPTION_MODE_DONE, request);
1462 getPhoneFromRequest(request).queryCdmaSubscriptionMode(onCompleted);
1463 break;
1464 case EVENT_GET_CDMA_SUBSCRIPTION_MODE_DONE:
1465 ar = (AsyncResult) msg.obj;
1466 request = (MainThreadRequest) ar.userObj;
1467 if (ar.exception != null) {
1468 request.result = TelephonyManager.CDMA_SUBSCRIPTION_RUIM_SIM;
1469 } else {
1470 request.result = ((int[]) ar.result)[0];
1471 }
1472 notifyRequester(request);
1473 break;
Pengquan Menga1bb6272018-09-06 09:59:22 -07001474 case CMD_SET_CDMA_SUBSCRIPTION_MODE:
1475 request = (MainThreadRequest) msg.obj;
1476 onCompleted = obtainMessage(EVENT_SET_CDMA_SUBSCRIPTION_MODE_DONE, request);
1477 int subscriptionMode = (int) request.argument;
Sarah Chinbaab1432020-10-28 13:46:24 -07001478 getPhoneFromRequest(request).setCdmaSubscriptionMode(
1479 subscriptionMode, onCompleted);
Pengquan Menga1bb6272018-09-06 09:59:22 -07001480 break;
1481 case EVENT_SET_CDMA_SUBSCRIPTION_MODE_DONE:
1482 ar = (AsyncResult) msg.obj;
1483 request = (MainThreadRequest) ar.userObj;
1484 request.result = ar.exception == null;
1485 notifyRequester(request);
Holly Jiuyu Sun01c47ad2018-01-24 17:56:33 +00001486 break;
Nathan Harold3ff88932018-08-14 10:19:49 -07001487 case CMD_GET_ALL_CELL_INFO:
1488 request = (MainThreadRequest) msg.obj;
Nathan Harold3ff88932018-08-14 10:19:49 -07001489 onCompleted = obtainMessage(EVENT_GET_ALL_CELL_INFO_DONE, request);
Nathan Harold92bed182018-10-12 18:16:49 -07001490 request.phone.requestCellInfoUpdate(request.workSource, onCompleted);
Nathan Harold3ff88932018-08-14 10:19:49 -07001491 break;
Nathan Harold3ff88932018-08-14 10:19:49 -07001492 case EVENT_GET_ALL_CELL_INFO_DONE:
1493 ar = (AsyncResult) msg.obj;
1494 request = (MainThreadRequest) ar.userObj;
Nathan Harold8d0f1742018-10-02 12:14:47 -07001495 // If a timeout occurs, the response will be null
1496 request.result = (ar.exception == null && ar.result != null)
1497 ? ar.result : new ArrayList<CellInfo>();
Nathan Harold3ff88932018-08-14 10:19:49 -07001498 synchronized (request) {
1499 request.notifyAll();
1500 }
1501 break;
Nathan Haroldfa8da0f2018-09-27 18:51:29 -07001502 case CMD_REQUEST_CELL_INFO_UPDATE:
1503 request = (MainThreadRequest) msg.obj;
1504 request.phone.requestCellInfoUpdate(request.workSource,
1505 obtainMessage(EVENT_REQUEST_CELL_INFO_UPDATE_DONE, request));
1506 break;
1507 case EVENT_REQUEST_CELL_INFO_UPDATE_DONE:
1508 ar = (AsyncResult) msg.obj;
1509 request = (MainThreadRequest) ar.userObj;
1510 ICellInfoCallback cb = (ICellInfoCallback) request.argument;
1511 try {
1512 if (ar.exception != null) {
Nathan Haroldfa8da0f2018-09-27 18:51:29 -07001513 Log.e(LOG_TAG, "Exception retrieving CellInfo=" + ar.exception);
Meng Wangd8921f42019-09-30 17:13:54 -07001514 cb.onError(
1515 TelephonyManager.CellInfoCallback.ERROR_MODEM_ERROR,
1516 ar.exception.getClass().getName(),
1517 ar.exception.toString());
Nathan Haroldfa8da0f2018-09-27 18:51:29 -07001518 } else if (ar.result == null) {
Nathan Haroldfa8da0f2018-09-27 18:51:29 -07001519 Log.w(LOG_TAG, "Timeout Waiting for CellInfo!");
Meng Wangd8921f42019-09-30 17:13:54 -07001520 cb.onError(TelephonyManager.CellInfoCallback.ERROR_TIMEOUT, null, null);
Nathan Haroldfa8da0f2018-09-27 18:51:29 -07001521 } else {
1522 // use the result as returned
1523 cb.onCellInfo((List<CellInfo>) ar.result);
1524 }
1525 } catch (RemoteException re) {
1526 Log.w(LOG_TAG, "Discarded CellInfo due to Callback RemoteException");
1527 }
1528 break;
Sarah Chin679c08a2020-11-18 13:39:35 -08001529 case CMD_GET_CELL_LOCATION: {
Nathan Harold3ff88932018-08-14 10:19:49 -07001530 request = (MainThreadRequest) msg.obj;
1531 WorkSource ws = (WorkSource) request.argument;
1532 Phone phone = getPhoneFromRequest(request);
Meng Wanga10e89e2019-12-09 13:13:01 -08001533 phone.getCellIdentity(ws, obtainMessage(EVENT_GET_CELL_LOCATION_DONE, request));
Nathan Harold3ff88932018-08-14 10:19:49 -07001534 break;
Sarah Chin679c08a2020-11-18 13:39:35 -08001535 }
1536 case EVENT_GET_CELL_LOCATION_DONE: {
Nathan Harold3ff88932018-08-14 10:19:49 -07001537 ar = (AsyncResult) msg.obj;
1538 request = (MainThreadRequest) ar.userObj;
1539 if (ar.exception == null) {
1540 request.result = ar.result;
1541 } else {
Sarah Chin679c08a2020-11-18 13:39:35 -08001542 Phone phone = getPhoneFromRequest(request);
Nathan Harold3ff88932018-08-14 10:19:49 -07001543 request.result = (phone.getPhoneType() == PhoneConstants.PHONE_TYPE_CDMA)
Meng Wanga10e89e2019-12-09 13:13:01 -08001544 ? new CellIdentityCdma() : new CellIdentityGsm();
Nathan Harold3ff88932018-08-14 10:19:49 -07001545 }
1546
1547 synchronized (request) {
1548 request.notifyAll();
1549 }
1550 break;
Sarah Chin679c08a2020-11-18 13:39:35 -08001551 }
chen xu6dac5ab2018-10-26 17:39:23 -07001552 case CMD_MODEM_REBOOT:
1553 request = (MainThreadRequest) msg.obj;
1554 onCompleted = obtainMessage(EVENT_RESET_MODEM_CONFIG_DONE, request);
Nazanin Bakhshif782e562018-12-11 15:15:39 -08001555 defaultPhone.rebootModem(onCompleted);
chen xu6dac5ab2018-10-26 17:39:23 -07001556 break;
chen xu6dac5ab2018-10-26 17:39:23 -07001557 case EVENT_CMD_MODEM_REBOOT_DONE:
1558 handleNullReturnEvent(msg, "rebootModem");
1559 break;
Malcolm Chen8e4ed912019-01-15 20:22:16 -08001560 case CMD_REQUEST_ENABLE_MODEM:
1561 request = (MainThreadRequest) msg.obj;
1562 boolean enable = (boolean) request.argument;
1563 onCompleted = obtainMessage(EVENT_ENABLE_MODEM_DONE, request);
Nazanin Bakhshi33d584b2019-02-27 10:44:32 -08001564 onCompleted.arg1 = enable ? 1 : 0;
Malcolm Chen8e4ed912019-01-15 20:22:16 -08001565 PhoneConfigurationManager.getInstance()
1566 .enablePhone(request.phone, enable, onCompleted);
1567 break;
Michele Berionne5e411512020-11-13 02:36:59 +00001568 case EVENT_ENABLE_MODEM_DONE: {
Malcolm Chen8e4ed912019-01-15 20:22:16 -08001569 ar = (AsyncResult) msg.obj;
1570 request = (MainThreadRequest) ar.userObj;
1571 request.result = (ar.exception == null);
Nazanin Bakhshif71371d2019-04-29 17:29:44 -07001572 int phoneId = request.phone.getPhoneId();
Nazanin Bakhshi33d584b2019-02-27 10:44:32 -08001573 //update the cache as modem status has changed
Nazanin Bakhshif71371d2019-04-29 17:29:44 -07001574 if ((boolean) request.result) {
1575 mPhoneConfigurationManager.addToPhoneStatusCache(phoneId, msg.arg1 == 1);
1576 updateModemStateMetrics();
1577 } else {
1578 Log.e(LOG_TAG, msg.what + " failure. Not updating modem status."
1579 + ar.exception);
1580 }
1581 notifyRequester(request);
1582 break;
Michele Berionne5e411512020-11-13 02:36:59 +00001583 }
Nazanin Bakhshif71371d2019-04-29 17:29:44 -07001584 case CMD_GET_MODEM_STATUS:
1585 request = (MainThreadRequest) msg.obj;
1586 onCompleted = obtainMessage(EVENT_GET_MODEM_STATUS_DONE, request);
1587 PhoneConfigurationManager.getInstance()
1588 .getPhoneStatusFromModem(request.phone, onCompleted);
1589 break;
1590 case EVENT_GET_MODEM_STATUS_DONE:
1591 ar = (AsyncResult) msg.obj;
1592 request = (MainThreadRequest) ar.userObj;
1593 int id = request.phone.getPhoneId();
1594 if (ar.exception == null && ar.result != null) {
1595 request.result = ar.result;
1596 //update the cache as modem status has changed
1597 mPhoneConfigurationManager.addToPhoneStatusCache(id,
1598 (boolean) request.result);
1599 } else {
1600 // Return true if modem status cannot be retrieved. For most cases,
1601 // modem status is on. And for older version modems, GET_MODEM_STATUS
1602 // and disable modem are not supported. Modem is always on.
1603 // TODO: this should be fixed in R to support a third
1604 // status UNKNOWN b/131631629
1605 request.result = true;
1606 Log.e(LOG_TAG, msg.what + " failure. Not updating modem status."
1607 + ar.exception);
1608 }
Malcolm Chen8e4ed912019-01-15 20:22:16 -08001609 notifyRequester(request);
1610 break;
Hall Liu73f5d362020-01-20 13:42:00 -08001611 case CMD_SET_SYSTEM_SELECTION_CHANNELS: {
1612 request = (MainThreadRequest) msg.obj;
1613 onCompleted = obtainMessage(EVENT_SET_SYSTEM_SELECTION_CHANNELS_DONE, request);
1614 Pair<List<RadioAccessSpecifier>, Consumer<Boolean>> args =
1615 (Pair<List<RadioAccessSpecifier>, Consumer<Boolean>>) request.argument;
1616 request.phone.setSystemSelectionChannels(args.first, onCompleted);
1617 break;
1618 }
1619 case EVENT_SET_SYSTEM_SELECTION_CHANNELS_DONE: {
1620 ar = (AsyncResult) msg.obj;
1621 request = (MainThreadRequest) ar.userObj;
1622 Pair<List<RadioAccessSpecifier>, Consumer<Boolean>> args =
1623 (Pair<List<RadioAccessSpecifier>, Consumer<Boolean>>) request.argument;
1624 args.second.accept(ar.exception == null);
1625 notifyRequester(request);
1626 break;
1627 }
Sarah Chin679c08a2020-11-18 13:39:35 -08001628 case CMD_GET_SYSTEM_SELECTION_CHANNELS: {
1629 request = (MainThreadRequest) msg.obj;
1630 onCompleted = obtainMessage(EVENT_GET_SYSTEM_SELECTION_CHANNELS_DONE, request);
1631 Phone phone = getPhoneFromRequest(request);
1632 if (phone != null) {
1633 phone.getSystemSelectionChannels(onCompleted);
1634 } else {
1635 loge("getSystemSelectionChannels: No phone object");
1636 request.result = new ArrayList<RadioAccessSpecifier>();
1637 notifyRequester(request);
1638 }
1639 break;
1640 }
1641 case EVENT_GET_SYSTEM_SELECTION_CHANNELS_DONE:
1642 ar = (AsyncResult) msg.obj;
1643 request = (MainThreadRequest) ar.userObj;
1644 if (ar.exception == null && ar.result != null) {
1645 request.result = ar.result;
1646 } else {
1647 request.result = new IllegalArgumentException(
1648 "Failed to retrieve system selection channels");
1649 if (ar.result == null) {
1650 loge("getSystemSelectionChannels: Empty response");
1651 } else {
1652 loge("getSystemSelectionChannels: Unknown exception");
1653 }
1654 }
1655 notifyRequester(request);
1656 break;
yincheng zhao2737e882019-09-06 17:06:54 -07001657 case EVENT_SET_FORBIDDEN_PLMNS_DONE:
1658 ar = (AsyncResult) msg.obj;
1659 request = (MainThreadRequest) ar.userObj;
1660 if (ar.exception == null && ar.result != null) {
1661 request.result = ar.result;
1662 } else {
1663 request.result = -1;
1664 loge("Failed to set Forbidden Plmns");
1665 if (ar.result == null) {
1666 loge("setForbidenPlmns: Empty response");
1667 } else if (ar.exception != null) {
1668 loge("setForbiddenPlmns: Exception: " + ar.exception);
1669 request.result = -1;
1670 } else {
1671 loge("setForbiddenPlmns: Unknown exception");
1672 }
1673 }
1674 notifyRequester(request);
1675 break;
1676 case CMD_SET_FORBIDDEN_PLMNS:
1677 request = (MainThreadRequest) msg.obj;
1678 uiccCard = getUiccCardFromRequest(request);
1679 if (uiccCard == null) {
1680 loge("setForbiddenPlmns: UiccCard is null");
1681 request.result = -1;
1682 notifyRequester(request);
1683 break;
1684 }
1685 Pair<Integer, List<String>> setFplmnsArgs =
1686 (Pair<Integer, List<String>>) request.argument;
1687 appType = setFplmnsArgs.first;
1688 List<String> fplmns = setFplmnsArgs.second;
1689 uiccApp = uiccCard.getApplicationByType(appType);
1690 if (uiccApp == null) {
1691 loge("setForbiddenPlmns: no app with specified type -- " + appType);
1692 request.result = -1;
1693 loge("Failed to get UICC App");
1694 notifyRequester(request);
1695 } else {
1696 onCompleted = obtainMessage(EVENT_SET_FORBIDDEN_PLMNS_DONE, request);
1697 ((SIMRecords) uiccApp.getIccRecords())
1698 .setForbiddenPlmns(onCompleted, fplmns);
1699 }
yinchengzhao4d163c02019-12-12 15:21:47 -08001700 break;
Naina Nallurid63128d2019-09-17 14:10:30 -07001701 case CMD_ERASE_MODEM_CONFIG:
1702 request = (MainThreadRequest) msg.obj;
1703 onCompleted = obtainMessage(EVENT_ERASE_MODEM_CONFIG_DONE, request);
1704 defaultPhone.eraseModemConfig(onCompleted);
1705 break;
1706 case EVENT_ERASE_MODEM_CONFIG_DONE:
1707 handleNullReturnEvent(msg, "eraseModemConfig");
yincheng zhao2737e882019-09-06 17:06:54 -07001708 break;
zoey chene02881a2019-12-30 16:11:23 +08001709
Kai Shif70f46f2021-03-03 13:59:46 -08001710 case CMD_ERASE_DATA_SHARED_PREFERENCES:
1711 request = (MainThreadRequest) msg.obj;
1712 request.result = defaultPhone.eraseDataInSharedPreferences();
1713 notifyRequester(request);
1714 break;
1715
zoey chene02881a2019-12-30 16:11:23 +08001716 case CMD_CHANGE_ICC_LOCK_PASSWORD:
1717 request = (MainThreadRequest) msg.obj;
1718 onCompleted = obtainMessage(EVENT_CHANGE_ICC_LOCK_PASSWORD_DONE, request);
1719 Pair<String, String> changed = (Pair<String, String>) request.argument;
1720 getPhoneFromRequest(request).getIccCard().changeIccLockPassword(
1721 changed.first, changed.second, onCompleted);
1722 break;
1723 case EVENT_CHANGE_ICC_LOCK_PASSWORD_DONE:
1724 ar = (AsyncResult) msg.obj;
1725 request = (MainThreadRequest) ar.userObj;
1726 if (ar.exception == null) {
1727 request.result = TelephonyManager.CHANGE_ICC_LOCK_SUCCESS;
Michele Berionne5e411512020-11-13 02:36:59 +00001728 // If the operation is successful, update the PIN storage
1729 Pair<String, String> passwords = (Pair<String, String>) request.argument;
1730 int phoneId = getPhoneFromRequest(request).getPhoneId();
1731 UiccController.getInstance().getPinStorage()
1732 .storePin(passwords.second, phoneId);
zoey chene02881a2019-12-30 16:11:23 +08001733 } else {
1734 request.result = msg.arg1;
1735 }
1736 notifyRequester(request);
1737 break;
1738
Michele Berionne5e411512020-11-13 02:36:59 +00001739 case CMD_SET_ICC_LOCK_ENABLED: {
zoey chene02881a2019-12-30 16:11:23 +08001740 request = (MainThreadRequest) msg.obj;
1741 onCompleted = obtainMessage(EVENT_SET_ICC_LOCK_ENABLED_DONE, request);
1742 Pair<Boolean, String> enabled = (Pair<Boolean, String>) request.argument;
1743 getPhoneFromRequest(request).getIccCard().setIccLockEnabled(
1744 enabled.first, enabled.second, onCompleted);
1745 break;
Michele Berionne5e411512020-11-13 02:36:59 +00001746 }
zoey chene02881a2019-12-30 16:11:23 +08001747 case EVENT_SET_ICC_LOCK_ENABLED_DONE:
1748 ar = (AsyncResult) msg.obj;
1749 request = (MainThreadRequest) ar.userObj;
1750 if (ar.exception == null) {
1751 request.result = TelephonyManager.CHANGE_ICC_LOCK_SUCCESS;
Michele Berionne5e411512020-11-13 02:36:59 +00001752 // If the operation is successful, update the PIN storage
1753 Pair<Boolean, String> enabled = (Pair<Boolean, String>) request.argument;
1754 int phoneId = getPhoneFromRequest(request).getPhoneId();
1755 if (enabled.first) {
1756 UiccController.getInstance().getPinStorage()
1757 .storePin(enabled.second, phoneId);
1758 } else {
1759 UiccController.getInstance().getPinStorage().clearPin(phoneId);
1760 }
zoey chene02881a2019-12-30 16:11:23 +08001761 } else {
1762 request.result = msg.arg1;
1763 }
Michele Berionne5e411512020-11-13 02:36:59 +00001764
1765
zoey chene02881a2019-12-30 16:11:23 +08001766 notifyRequester(request);
1767 break;
1768
Peter Wangdafb9ac2020-01-15 14:13:38 -08001769 case MSG_NOTIFY_USER_ACTIVITY:
1770 removeMessages(MSG_NOTIFY_USER_ACTIVITY);
Peter Wang59571be2020-01-27 12:35:15 +08001771 Intent intent = new Intent(TelephonyIntents.ACTION_USER_ACTIVITY_NOTIFICATION);
Peter Wangdafb9ac2020-01-15 14:13:38 -08001772 intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY);
1773 getDefaultPhone().getContext().sendBroadcastAsUser(
1774 intent, UserHandle.ALL, permission.USER_ACTIVITY);
1775 break;
Jack Nudelmanb0b87642020-11-12 15:04:39 -08001776
1777 case CMD_SET_DATA_THROTTLING: {
1778 request = (MainThreadRequest) msg.obj;
1779 onCompleted = obtainMessage(EVENT_SET_DATA_THROTTLING_DONE, request);
1780 DataThrottlingRequest dataThrottlingRequest =
1781 (DataThrottlingRequest) request.argument;
1782 Phone phone = getPhoneFromRequest(request);
1783 if (phone != null) {
1784 phone.setDataThrottling(onCompleted,
1785 request.workSource, dataThrottlingRequest.getDataThrottlingAction(),
1786 dataThrottlingRequest.getCompletionDurationMillis());
1787 } else {
1788 loge("setDataThrottling: No phone object");
1789 request.result =
1790 TelephonyManager.THERMAL_MITIGATION_RESULT_MODEM_NOT_AVAILABLE;
1791 notifyRequester(request);
1792 }
1793
1794 break;
1795 }
1796 case EVENT_SET_DATA_THROTTLING_DONE:
1797 ar = (AsyncResult) msg.obj;
1798 request = (MainThreadRequest) ar.userObj;
1799
1800 if (ar.exception == null) {
1801 request.result = TelephonyManager.THERMAL_MITIGATION_RESULT_SUCCESS;
1802 } else if (ar.exception instanceof CommandException) {
1803 loge("setDataThrottling: CommandException: " + ar.exception);
1804 CommandException.Error error =
1805 ((CommandException) (ar.exception)).getCommandError();
1806
1807 if (error == CommandException.Error.RADIO_NOT_AVAILABLE) {
1808 request.result = TelephonyManager
1809 .THERMAL_MITIGATION_RESULT_MODEM_NOT_AVAILABLE;
1810 } else if (error == CommandException.Error.INVALID_ARGUMENTS) {
1811 request.result = SET_DATA_THROTTLING_MODEM_THREW_INVALID_PARAMS;
Jack Nudelman5d6a98b2021-03-04 14:26:25 -08001812 } else if (error == CommandException.Error.REQUEST_NOT_SUPPORTED) {
1813 request.result = MODEM_DOES_NOT_SUPPORT_DATA_THROTTLING_ERROR_CODE;
Jack Nudelmanb0b87642020-11-12 15:04:39 -08001814 } else {
1815 request.result =
1816 TelephonyManager.THERMAL_MITIGATION_RESULT_MODEM_ERROR;
1817 }
1818 } else {
1819 request.result = TelephonyManager.THERMAL_MITIGATION_RESULT_MODEM_ERROR;
1820 }
1821 Log.w(LOG_TAG, "DataThrottlingResult = " + request.result);
1822 notifyRequester(request);
1823 break;
Jordan Liu109698e2020-11-24 14:50:34 -08001824
1825 case CMD_SET_SIM_POWER: {
1826 request = (MainThreadRequest) msg.obj;
1827 onCompleted = obtainMessage(EVENT_SET_SIM_POWER_DONE, request);
1828 request = (MainThreadRequest) msg.obj;
1829 int stateToSet =
1830 ((Pair<Integer, IIntegerConsumer>)
1831 request.argument).first;
1832 request.phone.setSimPowerState(stateToSet, onCompleted, request.workSource);
1833 break;
1834 }
1835 case EVENT_SET_SIM_POWER_DONE: {
1836 ar = (AsyncResult) msg.obj;
1837 request = (MainThreadRequest) ar.userObj;
1838 IIntegerConsumer callback =
1839 ((Pair<Integer, IIntegerConsumer>) request.argument).second;
1840 if (ar.exception != null) {
1841 loge("setSimPower exception: " + ar.exception);
1842 int errorCode = TelephonyManager.CallForwardingInfoCallback
1843 .RESULT_ERROR_UNKNOWN;
1844 if (ar.exception instanceof CommandException) {
1845 CommandException.Error error =
1846 ((CommandException) (ar.exception)).getCommandError();
1847 if (error == CommandException.Error.SIM_ERR) {
1848 errorCode = TelephonyManager.SET_SIM_POWER_STATE_SIM_ERROR;
1849 } else if (error == CommandException.Error.INVALID_ARGUMENTS) {
1850 errorCode = TelephonyManager.SET_SIM_POWER_STATE_ALREADY_IN_STATE;
1851 } else if (error == CommandException.Error.REQUEST_NOT_SUPPORTED) {
1852 errorCode = TelephonyManager.SET_SIM_POWER_STATE_NOT_SUPPORTED;
1853 } else {
1854 errorCode = TelephonyManager.SET_SIM_POWER_STATE_MODEM_ERROR;
1855 }
1856 }
1857 try {
1858 callback.accept(errorCode);
1859 } catch (RemoteException e) {
1860 // Ignore if the remote process is no longer available to call back.
1861 Log.w(LOG_TAG, "setSimPower: callback not available.");
1862 }
1863 } else {
1864 try {
1865 callback.accept(TelephonyManager.SET_SIM_POWER_STATE_SUCCESS);
1866 } catch (RemoteException e) {
1867 // Ignore if the remote process is no longer available to call back.
1868 Log.w(LOG_TAG, "setSimPower: callback not available.");
1869 }
1870 }
1871 break;
1872 }
Rambo Wanga5cc9b72021-01-07 10:51:54 -08001873 case CMD_SET_SIGNAL_STRENGTH_UPDATE_REQUEST: {
1874 request = (MainThreadRequest) msg.obj;
1875
1876 final Phone phone = getPhoneFromRequest(request);
1877 if (phone == null || phone.getServiceStateTracker() == null) {
1878 request.result = new IllegalStateException("Phone or SST is null");
1879 notifyRequester(request);
1880 break;
1881 }
1882
1883 Pair<Integer, SignalStrengthUpdateRequest> pair =
1884 (Pair<Integer, SignalStrengthUpdateRequest>) request.argument;
1885 onCompleted = obtainMessage(EVENT_SET_SIGNAL_STRENGTH_UPDATE_REQUEST_DONE,
1886 request);
1887 phone.getServiceStateTracker().setSignalStrengthUpdateRequest(
1888 request.subId, pair.first /*callingUid*/,
1889 pair.second /*request*/, onCompleted);
1890 break;
1891 }
1892 case EVENT_SET_SIGNAL_STRENGTH_UPDATE_REQUEST_DONE: {
1893 ar = (AsyncResult) msg.obj;
1894 request = (MainThreadRequest) ar.userObj;
1895 // request.result will be the exception of ar if present, true otherwise.
1896 // Be cautious not to leave result null which will wait() forever
1897 request.result = ar.exception != null ? ar.exception : true;
1898 notifyRequester(request);
1899 break;
1900 }
1901 case CMD_CLEAR_SIGNAL_STRENGTH_UPDATE_REQUEST: {
1902 request = (MainThreadRequest) msg.obj;
1903
1904 Phone phone = getPhoneFromRequest(request);
1905 if (phone == null || phone.getServiceStateTracker() == null) {
1906 request.result = new IllegalStateException("Phone or SST is null");
1907 notifyRequester(request);
1908 break;
1909 }
1910
1911 Pair<Integer, SignalStrengthUpdateRequest> pair =
1912 (Pair<Integer, SignalStrengthUpdateRequest>) request.argument;
1913 onCompleted = obtainMessage(EVENT_CLEAR_SIGNAL_STRENGTH_UPDATE_REQUEST_DONE,
1914 request);
1915 phone.getServiceStateTracker().clearSignalStrengthUpdateRequest(
1916 request.subId, pair.first /*callingUid*/,
1917 pair.second /*request*/, onCompleted);
1918 break;
1919 }
1920 case EVENT_CLEAR_SIGNAL_STRENGTH_UPDATE_REQUEST_DONE: {
1921 ar = (AsyncResult) msg.obj;
1922 request = (MainThreadRequest) ar.userObj;
1923 request.result = ar.exception != null ? ar.exception : true;
1924 notifyRequester(request);
1925 break;
1926 }
Jordan Liu109698e2020-11-24 14:50:34 -08001927
Hongbo Zeng156aa4a2021-02-08 21:50:28 +08001928 case CMD_GET_SLICING_CONFIG: {
1929 request = (MainThreadRequest) msg.obj;
1930 onCompleted = obtainMessage(EVENT_GET_SLICING_CONFIG_DONE, request);
1931 request.phone.getSlicingConfig(onCompleted);
1932 break;
1933 }
1934 case EVENT_GET_SLICING_CONFIG_DONE: {
1935 ar = (AsyncResult) msg.obj;
1936 request = (MainThreadRequest) ar.userObj;
1937 ResultReceiver result = (ResultReceiver) request.argument;
1938
1939 SlicingConfig slicingConfig = null;
1940 Bundle bundle = new Bundle();
1941 int resultCode = 0;
1942 if (ar.exception != null) {
1943 Log.e(LOG_TAG, "Exception retrieving slicing configuration="
1944 + ar.exception);
1945 resultCode = TelephonyManager.SlicingException.ERROR_MODEM_ERROR;
1946 } else if (ar.result == null) {
1947 Log.w(LOG_TAG, "Timeout Waiting for slicing configuration!");
1948 resultCode = TelephonyManager.SlicingException.ERROR_TIMEOUT;
1949 } else {
1950 // use the result as returned
1951 resultCode = TelephonyManager.SlicingException.SUCCESS;
1952 slicingConfig = (SlicingConfig) ar.result;
1953 }
1954
1955 if (slicingConfig == null) {
1956 slicingConfig = new SlicingConfig();
1957 }
1958 bundle.putParcelable(TelephonyManager.KEY_SLICING_CONFIG_HANDLE, slicingConfig);
1959 result.send(resultCode, bundle);
1960 notifyRequester(request);
1961 break;
1962 }
1963
Michele Berionne5e411512020-11-13 02:36:59 +00001964 case CMD_PREPARE_UNATTENDED_REBOOT:
1965 request = (MainThreadRequest) msg.obj;
1966 request.result =
1967 UiccController.getInstance().getPinStorage().prepareUnattendedReboot();
1968 notifyRequester(request);
1969 break;
1970
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001971 default:
1972 Log.w(LOG_TAG, "MainThreadHandler: unexpected message code: " + msg.what);
1973 break;
1974 }
1975 }
Jake Hambye994d462014-02-03 13:10:13 -08001976
Pengquan Menga1bb6272018-09-06 09:59:22 -07001977 private void notifyRequester(MainThreadRequest request) {
1978 synchronized (request) {
1979 request.notifyAll();
1980 }
1981 }
1982
Jake Hambye994d462014-02-03 13:10:13 -08001983 private void handleNullReturnEvent(Message msg, String command) {
1984 AsyncResult ar = (AsyncResult) msg.obj;
1985 MainThreadRequest request = (MainThreadRequest) ar.userObj;
1986 if (ar.exception == null) {
1987 request.result = true;
1988 } else {
1989 request.result = false;
1990 if (ar.exception instanceof CommandException) {
1991 loge(command + ": CommandException: " + ar.exception);
1992 } else {
1993 loge(command + ": Unknown exception");
1994 }
1995 }
Pengquan Menga1bb6272018-09-06 09:59:22 -07001996 notifyRequester(request);
Jake Hambye994d462014-02-03 13:10:13 -08001997 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001998 }
1999
2000 /**
2001 * Posts the specified command to be executed on the main thread,
2002 * waits for the request to complete, and returns the result.
2003 * @see #sendRequestAsync
2004 */
2005 private Object sendRequest(int command, Object argument) {
Rambo Wang0f050d82021-02-12 11:43:36 -08002006 return sendRequest(command, argument, SubscriptionManager.INVALID_SUBSCRIPTION_ID, null,
2007 null, -1 /*timeoutInMs*/);
vagdeviaf9a5b92018-08-15 16:01:53 -07002008 }
2009
2010 /**
2011 * Posts the specified command to be executed on the main thread,
2012 * waits for the request to complete, and returns the result.
2013 * @see #sendRequestAsync
2014 */
2015 private Object sendRequest(int command, Object argument, WorkSource workSource) {
2016 return sendRequest(command, argument, SubscriptionManager.INVALID_SUBSCRIPTION_ID,
Rambo Wang0f050d82021-02-12 11:43:36 -08002017 null, workSource, -1 /*timeoutInMs*/);
Wink Saville36469e72014-06-11 15:17:00 -07002018 }
2019
2020 /**
2021 * Posts the specified command to be executed on the main thread,
2022 * waits for the request to complete, and returns the result.
2023 * @see #sendRequestAsync
2024 */
Shishir Agrawal76d5da92014-11-09 16:17:25 -08002025 private Object sendRequest(int command, Object argument, Integer subId) {
Rambo Wang0f050d82021-02-12 11:43:36 -08002026 return sendRequest(command, argument, subId, null, null, -1 /*timeoutInMs*/);
2027 }
2028
2029 /**
2030 * Posts the specified command to be executed on the main thread,
2031 * waits for the request to complete for at most {@code timeoutInMs}, and returns the result
2032 * if not timeout or null otherwise.
2033 * @see #sendRequestAsync
2034 */
2035 private @Nullable Object sendRequest(int command, Object argument, Integer subId,
2036 long timeoutInMs) {
2037 return sendRequest(command, argument, subId, null, null, timeoutInMs);
vagdeviaf9a5b92018-08-15 16:01:53 -07002038 }
2039
2040 /**
2041 * Posts the specified command to be executed on the main thread,
2042 * waits for the request to complete, and returns the result.
2043 * @see #sendRequestAsync
2044 */
Nathan Harold92bed182018-10-12 18:16:49 -07002045 private Object sendRequest(int command, Object argument, int subId, WorkSource workSource) {
Rambo Wang0f050d82021-02-12 11:43:36 -08002046 return sendRequest(command, argument, subId, null, workSource, -1 /*timeoutInMs*/);
Nathan Harold92bed182018-10-12 18:16:49 -07002047 }
2048
2049 /**
2050 * Posts the specified command to be executed on the main thread,
2051 * waits for the request to complete, and returns the result.
2052 * @see #sendRequestAsync
2053 */
2054 private Object sendRequest(int command, Object argument, Phone phone, WorkSource workSource) {
Rambo Wang0f050d82021-02-12 11:43:36 -08002055 return sendRequest(command, argument, SubscriptionManager.INVALID_SUBSCRIPTION_ID, phone,
2056 workSource, -1 /*timeoutInMs*/);
Nathan Harold92bed182018-10-12 18:16:49 -07002057 }
2058
2059 /**
Rambo Wang0f050d82021-02-12 11:43:36 -08002060 * Posts the specified command to be executed on the main thread. If {@code timeoutInMs} is
2061 * negative, waits for the request to complete, and returns the result. Otherwise, wait for
2062 * maximum of {@code timeoutInMs} milliseconds, interrupt and return null.
Nathan Harold92bed182018-10-12 18:16:49 -07002063 * @see #sendRequestAsync
2064 */
Rambo Wang0f050d82021-02-12 11:43:36 -08002065 private @Nullable Object sendRequest(int command, Object argument, Integer subId, Phone phone,
2066 WorkSource workSource, long timeoutInMs) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002067 if (Looper.myLooper() == mMainThreadHandler.getLooper()) {
2068 throw new RuntimeException("This method will deadlock if called from the main thread.");
2069 }
2070
Nathan Harold92bed182018-10-12 18:16:49 -07002071 MainThreadRequest request = null;
2072 if (subId != SubscriptionManager.INVALID_SUBSCRIPTION_ID && phone != null) {
2073 throw new IllegalArgumentException("subId and phone cannot both be specified!");
2074 } else if (phone != null) {
2075 request = new MainThreadRequest(argument, phone, workSource);
2076 } else {
2077 request = new MainThreadRequest(argument, subId, workSource);
2078 }
2079
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002080 Message msg = mMainThreadHandler.obtainMessage(command, request);
2081 msg.sendToTarget();
2082
Rambo Wang0f050d82021-02-12 11:43:36 -08002083
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002084 synchronized (request) {
Rambo Wang0f050d82021-02-12 11:43:36 -08002085 if (timeoutInMs >= 0) {
2086 // Wait for at least timeoutInMs before returning null request result
2087 long now = SystemClock.elapsedRealtime();
2088 long deadline = now + timeoutInMs;
2089 while (request == null && now < deadline) {
2090 try {
2091 request.wait(deadline - now);
2092 } catch (InterruptedException e) {
2093 // Do nothing, go back and check if request is completed or timeout
2094 } finally {
2095 now = SystemClock.elapsedRealtime();
2096 }
2097 }
2098 } else {
2099 // Wait for the request to complete
2100 while (request.result == null) {
2101 try {
2102 request.wait();
2103 } catch (InterruptedException e) {
2104 // Do nothing, go back and wait until the request is complete
2105 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002106 }
2107 }
2108 }
Rambo Wang0f050d82021-02-12 11:43:36 -08002109 if (request.result == null) {
2110 Log.wtf(LOG_TAG,
2111 "sendRequest: Blocking command timed out. Something has gone terribly wrong.");
2112 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002113 return request.result;
2114 }
2115
2116 /**
2117 * Asynchronous ("fire and forget") version of sendRequest():
2118 * Posts the specified command to be executed on the main thread, and
2119 * returns immediately.
2120 * @see #sendRequest
2121 */
2122 private void sendRequestAsync(int command) {
2123 mMainThreadHandler.sendEmptyMessage(command);
2124 }
2125
2126 /**
Sailesh Nepalbd76e4e2013-10-27 13:59:44 -07002127 * Same as {@link #sendRequestAsync(int)} except it takes an argument.
Nathan Haroldfa8da0f2018-09-27 18:51:29 -07002128 * @see {@link #sendRequest(int)}
Sailesh Nepalbd76e4e2013-10-27 13:59:44 -07002129 */
2130 private void sendRequestAsync(int command, Object argument) {
Nathan Haroldfa8da0f2018-09-27 18:51:29 -07002131 sendRequestAsync(command, argument, null, null);
2132 }
2133
2134 /**
2135 * Same as {@link #sendRequestAsync(int,Object)} except it takes a Phone and WorkSource.
2136 * @see {@link #sendRequest(int,Object)}
2137 */
2138 private void sendRequestAsync(
2139 int command, Object argument, Phone phone, WorkSource workSource) {
2140 MainThreadRequest request = new MainThreadRequest(argument, phone, workSource);
Sailesh Nepalbd76e4e2013-10-27 13:59:44 -07002141 Message msg = mMainThreadHandler.obtainMessage(command, request);
2142 msg.sendToTarget();
2143 }
2144
2145 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002146 * Initialize the singleton PhoneInterfaceManager instance.
2147 * This is only done once, at startup, from PhoneApp.onCreate().
2148 */
Nazanin Bakhshif782e562018-12-11 15:15:39 -08002149 /* package */ static PhoneInterfaceManager init(PhoneGlobals app) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002150 synchronized (PhoneInterfaceManager.class) {
2151 if (sInstance == null) {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08002152 sInstance = new PhoneInterfaceManager(app);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002153 } else {
2154 Log.wtf(LOG_TAG, "init() called multiple times! sInstance = " + sInstance);
2155 }
2156 return sInstance;
2157 }
2158 }
2159
2160 /** Private constructor; @see init() */
Jordan Liu1979a042020-03-20 21:39:35 +00002161 private PhoneInterfaceManager(PhoneGlobals app) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002162 mApp = app;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002163 mCM = PhoneGlobals.getInstance().mCM;
Brad Ebinger24c29992019-12-05 13:03:21 -08002164 mImsResolver = PhoneGlobals.getInstance().getImsResolver();
Stuart Scott981d8582015-04-21 14:09:50 -07002165 mUserManager = (UserManager) app.getSystemService(Context.USER_SERVICE);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002166 mAppOps = (AppOpsManager)app.getSystemService(Context.APP_OPS_SERVICE);
2167 mMainThreadHandler = new MainThreadHandler();
Tobias Thiererb19e1f12018-12-11 17:54:03 +00002168 mSubscriptionController = SubscriptionController.getInstance();
Nazanin Bakhshif782e562018-12-11 15:15:39 -08002169 mTelephonySharedPreferences =
2170 PreferenceManager.getDefaultSharedPreferences(mApp);
yinxub1bed742017-04-17 11:45:04 -07002171 mNetworkScanRequestTracker = new NetworkScanRequestTracker();
Malcolm Chen2c63d402018-08-14 16:00:53 -07002172 mPhoneConfigurationManager = PhoneConfigurationManager.getInstance();
Daniel Bright94f43662021-03-01 14:43:40 -08002173 mRadioInterfaceCapabilities = RadioInterfaceCapabilityController.getInstance();
Peter Wanga3cf4ac2020-01-27 09:39:46 +08002174 mNotifyUserActivity = new AtomicBoolean(false);
Wink Saville3ab207e2014-11-20 13:07:20 -08002175
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002176 publish();
2177 }
2178
Nazanin Bakhshif782e562018-12-11 15:15:39 -08002179 private Phone getDefaultPhone() {
2180 Phone thePhone = getPhone(getDefaultSubscription());
2181 return (thePhone != null) ? thePhone : PhoneFactory.getDefaultPhone();
2182 }
2183
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002184 private void publish() {
2185 if (DBG) log("publish: " + this);
2186
Peter Wangc035ce42020-01-08 21:00:22 -08002187 TelephonyFrameworkInitializer
2188 .getTelephonyServiceManager()
2189 .getTelephonyServiceRegisterer()
2190 .register(this);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002191 }
2192
Stuart Scott584921c2015-01-15 17:10:34 -08002193 private Phone getPhoneFromRequest(MainThreadRequest request) {
Jordan Liu4c733742019-02-28 12:03:40 -08002194 if (request.phone != null) {
2195 return request.phone;
2196 } else {
2197 return getPhoneFromSubId(request.subId);
2198 }
2199 }
2200
2201 private Phone getPhoneFromSubId(int subId) {
2202 return (subId == SubscriptionManager.INVALID_SUBSCRIPTION_ID)
2203 ? getDefaultPhone() : getPhone(subId);
Stuart Scott584921c2015-01-15 17:10:34 -08002204 }
2205
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002206 private UiccCard getUiccCardFromRequest(MainThreadRequest request) {
2207 Phone phone = getPhoneFromRequest(request);
2208 return phone == null ? null :
2209 UiccController.getInstance().getUiccCard(phone.getPhoneId());
2210 }
2211
Wink Saville36469e72014-06-11 15:17:00 -07002212 // returns phone associated with the subId.
Wink Savilleb564aae2014-10-23 10:18:09 -07002213 private Phone getPhone(int subId) {
Wink Savilleac1bdfd2014-11-20 23:04:44 -08002214 return PhoneFactory.getPhone(mSubscriptionController.getPhoneId(subId));
Wink Saville36469e72014-06-11 15:17:00 -07002215 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002216
Kai Shif70f46f2021-03-03 13:59:46 -08002217 private void sendEraseModemConfig(@NonNull Phone phone) {
2218 Boolean success = (Boolean) sendRequest(CMD_ERASE_MODEM_CONFIG, null);
2219 if (DBG) log("eraseModemConfig:" + ' ' + (success ? "ok" : "fail"));
2220 }
2221
2222 private void sendEraseDataInSharedPreferences(@NonNull Phone phone) {
2223 Boolean success = (Boolean) sendRequest(CMD_ERASE_DATA_SHARED_PREFERENCES, null);
2224 if (DBG) log("eraseDataInSharedPreferences:" + ' ' + (success ? "ok" : "fail"));
Naina Nallurid63128d2019-09-17 14:10:30 -07002225 }
2226
Peter Wang44b186e2020-01-13 23:33:09 -08002227 private boolean isImsAvailableOnDevice() {
2228 PackageManager pm = getDefaultPhone().getContext().getPackageManager();
2229 if (pm == null) {
2230 // For some reason package manger is not available.. This will fail internally anyway,
2231 // so do not throw error and allow.
2232 return true;
2233 }
2234 return pm.hasSystemFeature(PackageManager.FEATURE_TELEPHONY_IMS, 0);
2235 }
2236
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002237 public void dial(String number) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07002238 dialForSubscriber(getPreferredVoiceSubscription(), number);
Wink Saville36469e72014-06-11 15:17:00 -07002239 }
2240
Wink Savilleb564aae2014-10-23 10:18:09 -07002241 public void dialForSubscriber(int subId, String number) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002242 if (DBG) log("dial: " + number);
2243 // No permission check needed here: This is just a wrapper around the
2244 // ACTION_DIAL intent, which is available to any app since it puts up
2245 // the UI before it does anything.
2246
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002247 final long identity = Binder.clearCallingIdentity();
2248 try {
2249 String url = createTelUrl(number);
2250 if (url == null) {
2251 return;
2252 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002253
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002254 // PENDING: should we just silently fail if phone is offhook or ringing?
2255 PhoneConstants.State state = mCM.getState(subId);
2256 if (state != PhoneConstants.State.OFFHOOK && state != PhoneConstants.State.RINGING) {
2257 Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse(url));
2258 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
2259 mApp.startActivity(intent);
2260 }
2261 } finally {
2262 Binder.restoreCallingIdentity(identity);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002263 }
2264 }
2265
2266 public void call(String callingPackage, String number) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07002267 callForSubscriber(getPreferredVoiceSubscription(), callingPackage, number);
Wink Saville36469e72014-06-11 15:17:00 -07002268 }
2269
Wink Savilleb564aae2014-10-23 10:18:09 -07002270 public void callForSubscriber(int subId, String callingPackage, String number) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002271 if (DBG) log("call: " + number);
2272
2273 // This is just a wrapper around the ACTION_CALL intent, but we still
2274 // need to do a permission check since we're calling startActivity()
2275 // from the context of the phone app.
2276 enforceCallPermission();
2277
Jordan Liu1617b712019-07-10 15:06:26 -07002278 if (mAppOps.noteOp(AppOpsManager.OPSTR_CALL_PHONE, Binder.getCallingUid(), callingPackage)
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002279 != AppOpsManager.MODE_ALLOWED) {
2280 return;
2281 }
2282
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002283 final long identity = Binder.clearCallingIdentity();
2284 try {
2285 String url = createTelUrl(number);
2286 if (url == null) {
2287 return;
2288 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002289
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002290 boolean isValid = false;
2291 final List<SubscriptionInfo> slist = getActiveSubscriptionInfoListPrivileged();
2292 if (slist != null) {
2293 for (SubscriptionInfo subInfoRecord : slist) {
2294 if (subInfoRecord.getSubscriptionId() == subId) {
2295 isValid = true;
2296 break;
2297 }
Wink Saville3ab207e2014-11-20 13:07:20 -08002298 }
Wink Saville08874612014-08-31 19:19:58 -07002299 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002300 if (!isValid) {
2301 return;
2302 }
Wink Saville08874612014-08-31 19:19:58 -07002303
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002304 Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse(url));
2305 intent.putExtra(SUBSCRIPTION_KEY, subId);
2306 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
2307 mApp.startActivity(intent);
2308 } finally {
2309 Binder.restoreCallingIdentity(identity);
2310 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002311 }
2312
Wink Savilleb564aae2014-10-23 10:18:09 -07002313 public boolean supplyPinForSubscriber(int subId, String pin) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07002314 int [] resultArray = supplyPinReportResultForSubscriber(subId, pin);
Wink Saville9de0f752013-10-22 19:04:03 -07002315 return (resultArray[0] == PhoneConstants.PIN_RESULT_SUCCESS) ? true : false;
2316 }
2317
Wink Savilleb564aae2014-10-23 10:18:09 -07002318 public boolean supplyPukForSubscriber(int subId, String puk, String pin) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07002319 int [] resultArray = supplyPukReportResultForSubscriber(subId, puk, pin);
Wink Saville9de0f752013-10-22 19:04:03 -07002320 return (resultArray[0] == PhoneConstants.PIN_RESULT_SUCCESS) ? true : false;
2321 }
2322
Wink Savilleb564aae2014-10-23 10:18:09 -07002323 public int[] supplyPinReportResultForSubscriber(int subId, String pin) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002324 enforceModifyPermission();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002325
2326 final long identity = Binder.clearCallingIdentity();
2327 try {
Michele Berionne5e411512020-11-13 02:36:59 +00002328 Phone phone = getPhone(subId);
2329 final UnlockSim checkSimPin = new UnlockSim(phone.getPhoneId(), phone.getIccCard());
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002330 checkSimPin.start();
2331 return checkSimPin.unlockSim(null, pin);
2332 } finally {
2333 Binder.restoreCallingIdentity(identity);
2334 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002335 }
2336
Wink Savilleb564aae2014-10-23 10:18:09 -07002337 public int[] supplyPukReportResultForSubscriber(int subId, String puk, String pin) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002338 enforceModifyPermission();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002339
2340 final long identity = Binder.clearCallingIdentity();
2341 try {
Michele Berionne5e411512020-11-13 02:36:59 +00002342 Phone phone = getPhone(subId);
2343 final UnlockSim checkSimPuk = new UnlockSim(phone.getPhoneId(), phone.getIccCard());
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002344 checkSimPuk.start();
2345 return checkSimPuk.unlockSim(puk, pin);
2346 } finally {
2347 Binder.restoreCallingIdentity(identity);
2348 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002349 }
2350
2351 /**
Wink Saville9de0f752013-10-22 19:04:03 -07002352 * Helper thread to turn async call to SimCard#supplyPin into
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002353 * a synchronous one.
2354 */
2355 private static class UnlockSim extends Thread {
2356
2357 private final IccCard mSimCard;
Michele Berionne5e411512020-11-13 02:36:59 +00002358 private final int mPhoneId;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002359
2360 private boolean mDone = false;
Wink Saville9de0f752013-10-22 19:04:03 -07002361 private int mResult = PhoneConstants.PIN_GENERAL_FAILURE;
2362 private int mRetryCount = -1;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002363
2364 // For replies from SimCard interface
2365 private Handler mHandler;
2366
2367 // For async handler to identify request type
2368 private static final int SUPPLY_PIN_COMPLETE = 100;
2369
Michele Berionne5e411512020-11-13 02:36:59 +00002370 UnlockSim(int phoneId, IccCard simCard) {
2371 mPhoneId = phoneId;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002372 mSimCard = simCard;
2373 }
2374
2375 @Override
2376 public void run() {
2377 Looper.prepare();
2378 synchronized (UnlockSim.this) {
2379 mHandler = new Handler() {
2380 @Override
2381 public void handleMessage(Message msg) {
2382 AsyncResult ar = (AsyncResult) msg.obj;
2383 switch (msg.what) {
2384 case SUPPLY_PIN_COMPLETE:
2385 Log.d(LOG_TAG, "SUPPLY_PIN_COMPLETE");
2386 synchronized (UnlockSim.this) {
Wink Saville9de0f752013-10-22 19:04:03 -07002387 mRetryCount = msg.arg1;
2388 if (ar.exception != null) {
2389 if (ar.exception instanceof CommandException &&
2390 ((CommandException)(ar.exception)).getCommandError()
2391 == CommandException.Error.PASSWORD_INCORRECT) {
2392 mResult = PhoneConstants.PIN_PASSWORD_INCORRECT;
vivi.lib5e9ada2019-09-12 16:04:24 +08002393 } //When UiccCardApp dispose,handle message and return exception
2394 else if (ar.exception instanceof CommandException &&
2395 ((CommandException) (ar.exception)).getCommandError()
2396 == CommandException.Error.ABORTED) {
2397 mResult = PhoneConstants.PIN_OPERATION_ABORTED;
Wink Saville9de0f752013-10-22 19:04:03 -07002398 } else {
2399 mResult = PhoneConstants.PIN_GENERAL_FAILURE;
2400 }
2401 } else {
2402 mResult = PhoneConstants.PIN_RESULT_SUCCESS;
2403 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002404 mDone = true;
2405 UnlockSim.this.notifyAll();
2406 }
2407 break;
2408 }
2409 }
2410 };
2411 UnlockSim.this.notifyAll();
2412 }
2413 Looper.loop();
2414 }
2415
2416 /*
2417 * Use PIN or PUK to unlock SIM card
2418 *
2419 * If PUK is null, unlock SIM card with PIN
2420 *
2421 * If PUK is not null, unlock SIM card with PUK and set PIN code
2422 */
Wink Saville9de0f752013-10-22 19:04:03 -07002423 synchronized int[] unlockSim(String puk, String pin) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002424
2425 while (mHandler == null) {
2426 try {
2427 wait();
2428 } catch (InterruptedException e) {
2429 Thread.currentThread().interrupt();
2430 }
2431 }
2432 Message callback = Message.obtain(mHandler, SUPPLY_PIN_COMPLETE);
2433
2434 if (puk == null) {
2435 mSimCard.supplyPin(pin, callback);
2436 } else {
2437 mSimCard.supplyPuk(puk, pin, callback);
2438 }
2439
2440 while (!mDone) {
2441 try {
2442 Log.d(LOG_TAG, "wait for done");
2443 wait();
2444 } catch (InterruptedException e) {
2445 // Restore the interrupted status
2446 Thread.currentThread().interrupt();
2447 }
2448 }
2449 Log.d(LOG_TAG, "done");
Wink Saville9de0f752013-10-22 19:04:03 -07002450 int[] resultArray = new int[2];
2451 resultArray[0] = mResult;
2452 resultArray[1] = mRetryCount;
Michele Berionne5e411512020-11-13 02:36:59 +00002453
2454 if (mResult == PhoneConstants.PIN_RESULT_SUCCESS && pin.length() > 0) {
2455 UiccController.getInstance().getPinStorage().storePin(pin, mPhoneId);
2456 }
2457
Wink Saville9de0f752013-10-22 19:04:03 -07002458 return resultArray;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002459 }
2460 }
2461
Nathan Harold7c8d0f12020-05-28 20:40:31 -07002462 /**
2463 * This method has been removed due to privacy and stability concerns.
2464 */
2465 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002466 public void updateServiceLocation() {
Nathan Harold7c8d0f12020-05-28 20:40:31 -07002467 Log.e(LOG_TAG, "Call to unsupported method updateServiceLocation()");
2468 return;
Wink Saville36469e72014-06-11 15:17:00 -07002469 }
2470
Nathan Harold1f889d82020-06-04 17:05:26 -07002471 @Override
2472 public void updateServiceLocationWithPackageName(String callingPackage) {
2473 mApp.getSystemService(AppOpsManager.class)
2474 .checkPackage(Binder.getCallingUid(), callingPackage);
2475
Nathan Haroldf096d982020-11-18 17:18:06 -08002476 final int targetSdk = TelephonyPermissions.getTargetSdk(mApp, callingPackage);
Nathan Harold1f889d82020-06-04 17:05:26 -07002477 if (targetSdk > android.os.Build.VERSION_CODES.R) {
2478 // Callers targeting S have no business invoking this method.
2479 return;
2480 }
2481
2482 LocationAccessPolicy.LocationPermissionResult locationResult =
2483 LocationAccessPolicy.checkLocationPermission(mApp,
2484 new LocationAccessPolicy.LocationPermissionQuery.Builder()
2485 .setCallingPackage(callingPackage)
2486 .setCallingFeatureId(null)
2487 .setCallingPid(Binder.getCallingPid())
2488 .setCallingUid(Binder.getCallingUid())
2489 .setMethod("updateServiceLocation")
2490 .setMinSdkVersionForCoarse(Build.VERSION_CODES.BASE)
2491 .setMinSdkVersionForFine(Build.VERSION_CODES.Q)
2492 .build());
2493 // Apps that lack location permission have no business calling this method;
2494 // however, because no permission was declared in the public API, denials must
2495 // all be "soft".
2496 switch (locationResult) {
2497 case DENIED_HARD: /* fall through */
2498 case DENIED_SOFT:
2499 return;
2500 }
2501
2502 WorkSource workSource = getWorkSource(Binder.getCallingUid());
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002503 final long identity = Binder.clearCallingIdentity();
2504 try {
Nathan Harold1f889d82020-06-04 17:05:26 -07002505 final Phone phone = getPhone(getDefaultSubscription());
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002506 if (phone != null) {
Nathan Harold1f889d82020-06-04 17:05:26 -07002507 phone.updateServiceLocation(workSource);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002508 }
2509 } finally {
2510 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07002511 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002512 }
2513
Philip P. Moltmann700a9592019-10-03 11:53:50 -07002514 @Deprecated
Robert Greenwalt36b23af2015-07-06 17:59:14 -07002515 @Override
2516 public boolean isRadioOn(String callingPackage) {
Philip P. Moltmann700a9592019-10-03 11:53:50 -07002517 return isRadioOnWithFeature(callingPackage, null);
2518 }
2519
2520
2521 @Override
2522 public boolean isRadioOnWithFeature(String callingPackage, String callingFeatureId) {
2523 return isRadioOnForSubscriberWithFeature(getDefaultSubscription(), callingPackage,
2524 callingFeatureId);
2525 }
2526
2527 @Deprecated
2528 @Override
2529 public boolean isRadioOnForSubscriber(int subId, String callingPackage) {
2530 return isRadioOnForSubscriberWithFeature(subId, callingPackage, null);
Wink Saville36469e72014-06-11 15:17:00 -07002531 }
2532
Robert Greenwalt36b23af2015-07-06 17:59:14 -07002533 @Override
Philip P. Moltmann700a9592019-10-03 11:53:50 -07002534 public boolean isRadioOnForSubscriberWithFeature(int subId, String callingPackage,
2535 String callingFeatureId) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08002536 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
Philip P. Moltmann700a9592019-10-03 11:53:50 -07002537 mApp, subId, callingPackage, callingFeatureId, "isRadioOnForSubscriber")) {
Robert Greenwalt36b23af2015-07-06 17:59:14 -07002538 return false;
2539 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002540
2541 final long identity = Binder.clearCallingIdentity();
2542 try {
2543 return isRadioOnForSubscriber(subId);
2544 } finally {
2545 Binder.restoreCallingIdentity(identity);
2546 }
Robert Greenwalt36b23af2015-07-06 17:59:14 -07002547 }
2548
2549 private boolean isRadioOnForSubscriber(int subId) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002550 final long identity = Binder.clearCallingIdentity();
2551 try {
2552 final Phone phone = getPhone(subId);
2553 if (phone != null) {
2554 return phone.getServiceState().getState() != ServiceState.STATE_POWER_OFF;
2555 } else {
2556 return false;
2557 }
2558 } finally {
2559 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07002560 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002561 }
2562
2563 public void toggleRadioOnOff() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07002564 toggleRadioOnOffForSubscriber(getDefaultSubscription());
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002565 }
Wink Saville36469e72014-06-11 15:17:00 -07002566
Wink Savilleb564aae2014-10-23 10:18:09 -07002567 public void toggleRadioOnOffForSubscriber(int subId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002568 enforceModifyPermission();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002569
2570 final long identity = Binder.clearCallingIdentity();
2571 try {
2572 final Phone phone = getPhone(subId);
2573 if (phone != null) {
2574 phone.setRadioPower(!isRadioOnForSubscriber(subId));
2575 }
2576 } finally {
2577 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07002578 }
Wink Saville36469e72014-06-11 15:17:00 -07002579 }
2580
2581 public boolean setRadio(boolean turnOn) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07002582 return setRadioForSubscriber(getDefaultSubscription(), turnOn);
Wink Saville36469e72014-06-11 15:17:00 -07002583 }
2584
Wink Savilleb564aae2014-10-23 10:18:09 -07002585 public boolean setRadioForSubscriber(int subId, boolean turnOn) {
Wink Saville36469e72014-06-11 15:17:00 -07002586 enforceModifyPermission();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002587
2588 final long identity = Binder.clearCallingIdentity();
2589 try {
2590 final Phone phone = getPhone(subId);
2591 if (phone == null) {
2592 return false;
2593 }
2594 if ((phone.getServiceState().getState() != ServiceState.STATE_POWER_OFF) != turnOn) {
2595 toggleRadioOnOffForSubscriber(subId);
2596 }
2597 return true;
2598 } finally {
2599 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07002600 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002601 }
Wink Saville36469e72014-06-11 15:17:00 -07002602
Naveen Kalla1fd79bd2014-08-08 00:48:59 -07002603 public boolean needMobileRadioShutdown() {
Shuo Qianfa7b6b32019-12-10 10:40:38 -08002604 enforceReadPrivilegedPermission("needMobileRadioShutdown");
Naveen Kalla1fd79bd2014-08-08 00:48:59 -07002605 /*
2606 * If any of the Radios are available, it will need to be
2607 * shutdown. So return true if any Radio is available.
2608 */
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002609 final long identity = Binder.clearCallingIdentity();
2610 try {
2611 for (int i = 0; i < TelephonyManager.getDefault().getPhoneCount(); i++) {
2612 Phone phone = PhoneFactory.getPhone(i);
2613 if (phone != null && phone.isRadioAvailable()) return true;
2614 }
2615 logv(TelephonyManager.getDefault().getPhoneCount() + " Phones are shutdown.");
2616 return false;
2617 } finally {
2618 Binder.restoreCallingIdentity(identity);
Naveen Kalla1fd79bd2014-08-08 00:48:59 -07002619 }
Naveen Kalla1fd79bd2014-08-08 00:48:59 -07002620 }
2621
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002622 @Override
Naveen Kalla1fd79bd2014-08-08 00:48:59 -07002623 public void shutdownMobileRadios() {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002624 enforceModifyPermission();
2625
2626 final long identity = Binder.clearCallingIdentity();
2627 try {
2628 for (int i = 0; i < TelephonyManager.getDefault().getPhoneCount(); i++) {
2629 logv("Shutting down Phone " + i);
2630 shutdownRadioUsingPhoneId(i);
2631 }
2632 } finally {
2633 Binder.restoreCallingIdentity(identity);
Naveen Kalla1fd79bd2014-08-08 00:48:59 -07002634 }
2635 }
2636
2637 private void shutdownRadioUsingPhoneId(int phoneId) {
Naveen Kalla1fd79bd2014-08-08 00:48:59 -07002638 Phone phone = PhoneFactory.getPhone(phoneId);
2639 if (phone != null && phone.isRadioAvailable()) {
2640 phone.shutdownRadio();
2641 }
2642 }
2643
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002644 public boolean setRadioPower(boolean turnOn) {
Jack Yub4e16162017-05-15 12:48:40 -07002645 enforceModifyPermission();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002646
2647 final long identity = Binder.clearCallingIdentity();
2648 try {
2649 final Phone defaultPhone = PhoneFactory.getDefaultPhone();
2650 if (defaultPhone != null) {
2651 defaultPhone.setRadioPower(turnOn);
2652 return true;
2653 } else {
2654 loge("There's no default phone.");
2655 return false;
2656 }
2657 } finally {
2658 Binder.restoreCallingIdentity(identity);
Wei Liu9ae2a062016-08-08 11:09:34 -07002659 }
Wink Saville36469e72014-06-11 15:17:00 -07002660 }
2661
Wink Savilleb564aae2014-10-23 10:18:09 -07002662 public boolean setRadioPowerForSubscriber(int subId, boolean turnOn) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002663 enforceModifyPermission();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002664
2665 final long identity = Binder.clearCallingIdentity();
2666 try {
2667 final Phone phone = getPhone(subId);
2668 if (phone != null) {
2669 phone.setRadioPower(turnOn);
2670 return true;
2671 } else {
2672 return false;
2673 }
2674 } finally {
2675 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07002676 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002677 }
2678
Wink Saville36469e72014-06-11 15:17:00 -07002679 // FIXME: subId version needed
Sanket Padawe356d7632015-06-22 14:03:32 -07002680 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002681 public boolean enableDataConnectivity() {
2682 enforceModifyPermission();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002683
2684 final long identity = Binder.clearCallingIdentity();
2685 try {
2686 int subId = mSubscriptionController.getDefaultDataSubId();
2687 final Phone phone = getPhone(subId);
2688 if (phone != null) {
Sooraj Sasindranaf1b5132020-05-05 21:06:20 +00002689 phone.getDataEnabledSettings().setDataEnabled(
2690 TelephonyManager.DATA_ENABLED_REASON_USER, true);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002691 return true;
2692 } else {
2693 return false;
2694 }
2695 } finally {
2696 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07002697 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002698 }
2699
Wink Saville36469e72014-06-11 15:17:00 -07002700 // FIXME: subId version needed
Sanket Padawe356d7632015-06-22 14:03:32 -07002701 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002702 public boolean disableDataConnectivity() {
2703 enforceModifyPermission();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002704
2705 final long identity = Binder.clearCallingIdentity();
2706 try {
2707 int subId = mSubscriptionController.getDefaultDataSubId();
2708 final Phone phone = getPhone(subId);
2709 if (phone != null) {
Sooraj Sasindranaf1b5132020-05-05 21:06:20 +00002710 phone.getDataEnabledSettings().setDataEnabled(
2711 TelephonyManager.DATA_ENABLED_REASON_USER, false);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002712 return true;
2713 } else {
2714 return false;
2715 }
2716 } finally {
2717 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07002718 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002719 }
2720
Sanket Padawe356d7632015-06-22 14:03:32 -07002721 @Override
Jack Yuacf8a132017-05-01 17:00:48 -07002722 public boolean isDataConnectivityPossible(int subId) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002723 final long identity = Binder.clearCallingIdentity();
2724 try {
2725 final Phone phone = getPhone(subId);
2726 if (phone != null) {
Jack Yub5d8f642018-11-26 11:20:48 -08002727 return phone.isDataAllowed(ApnSetting.TYPE_DEFAULT);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002728 } else {
2729 return false;
2730 }
2731 } finally {
2732 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07002733 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002734 }
2735
2736 public boolean handlePinMmi(String dialString) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07002737 return handlePinMmiForSubscriber(getDefaultSubscription(), dialString);
Wink Saville36469e72014-06-11 15:17:00 -07002738 }
2739
pkanwarae03a6b2016-11-06 20:37:09 -08002740 public void handleUssdRequest(int subId, String ussdRequest, ResultReceiver wrappedCallback) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002741 enforceCallPermission();
2742
2743 final long identity = Binder.clearCallingIdentity();
2744 try {
2745 if (!SubscriptionManager.isValidSubscriptionId(subId)) {
2746 return;
2747 }
2748 Pair<String, ResultReceiver> ussdObject = new Pair(ussdRequest, wrappedCallback);
2749 sendRequest(CMD_HANDLE_USSD_REQUEST, ussdObject, subId);
2750 } finally {
2751 Binder.restoreCallingIdentity(identity);
2752 }
pkanwar32d516d2016-10-14 19:37:38 -07002753 };
2754
Wink Savilleb564aae2014-10-23 10:18:09 -07002755 public boolean handlePinMmiForSubscriber(int subId, String dialString) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002756 enforceModifyPermission();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002757
2758 final long identity = Binder.clearCallingIdentity();
2759 try {
2760 if (!SubscriptionManager.isValidSubscriptionId(subId)) {
2761 return false;
2762 }
2763 return (Boolean) sendRequest(CMD_HANDLE_PIN_MMI, dialString, subId);
2764 } finally {
2765 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07002766 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002767 }
2768
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002769 public int getCallState() {
Shishir Agrawala9f32182016-04-12 12:00:16 -07002770 return getCallStateForSlot(getSlotForDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07002771 }
2772
Sanket Padawe13bac7b2017-03-20 15:04:47 -07002773 public int getCallStateForSlot(int slotIndex) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002774 final long identity = Binder.clearCallingIdentity();
2775 try {
2776 Phone phone = PhoneFactory.getPhone(slotIndex);
2777 return phone == null ? TelephonyManager.CALL_STATE_IDLE :
2778 PhoneConstantConversions.convertCallState(phone.getState());
2779 } finally {
2780 Binder.restoreCallingIdentity(identity);
2781 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002782 }
2783
Sanket Padawe356d7632015-06-22 14:03:32 -07002784 @Override
Nathan Harolde037c472019-06-26 00:41:07 +00002785 public int getDataState() {
Nathan Haroldc4689b12019-06-14 16:58:30 -07002786 return getDataStateForSubId(mSubscriptionController.getDefaultDataSubId());
2787 }
2788
2789 @Override
2790 public int getDataStateForSubId(int subId) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002791 final long identity = Binder.clearCallingIdentity();
2792 try {
Nathan Haroldc4689b12019-06-14 16:58:30 -07002793 final Phone phone = getPhone(subId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002794 if (phone != null) {
2795 return PhoneConstantConversions.convertDataState(phone.getDataConnectionState());
2796 } else {
2797 return PhoneConstantConversions.convertDataState(
2798 PhoneConstants.DataState.DISCONNECTED);
2799 }
2800 } finally {
2801 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07002802 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002803 }
2804
Sanket Padawe356d7632015-06-22 14:03:32 -07002805 @Override
Nathan Harolde037c472019-06-26 00:41:07 +00002806 public int getDataActivity() {
Nathan Haroldc4689b12019-06-14 16:58:30 -07002807 return getDataActivityForSubId(mSubscriptionController.getDefaultDataSubId());
2808 }
2809
2810 @Override
2811 public int getDataActivityForSubId(int subId) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002812 final long identity = Binder.clearCallingIdentity();
2813 try {
Nathan Haroldc4689b12019-06-14 16:58:30 -07002814 final Phone phone = getPhone(subId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002815 if (phone != null) {
2816 return DefaultPhoneNotifier.convertDataActivityState(phone.getDataActivityState());
2817 } else {
2818 return TelephonyManager.DATA_ACTIVITY_NONE;
2819 }
2820 } finally {
2821 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07002822 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002823 }
2824
2825 @Override
Meng Wanga10e89e2019-12-09 13:13:01 -08002826 public CellIdentity getCellLocation(String callingPackage, String callingFeatureId) {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08002827 mApp.getSystemService(AppOpsManager.class)
Hall Liu1aa510f2017-11-22 17:40:08 -08002828 .checkPackage(Binder.getCallingUid(), callingPackage);
Hall Liuf19c44f2018-11-27 14:38:17 -08002829
2830 LocationAccessPolicy.LocationPermissionResult locationResult =
2831 LocationAccessPolicy.checkLocationPermission(mApp,
2832 new LocationAccessPolicy.LocationPermissionQuery.Builder()
2833 .setCallingPackage(callingPackage)
Philip P. Moltmann3a2772a2019-10-04 08:15:00 -07002834 .setCallingFeatureId(callingFeatureId)
Hall Liuf19c44f2018-11-27 14:38:17 -08002835 .setCallingPid(Binder.getCallingPid())
2836 .setCallingUid(Binder.getCallingUid())
2837 .setMethod("getCellLocation")
Hall Liu773ba022020-01-24 18:07:12 -08002838 .setMinSdkVersionForCoarse(Build.VERSION_CODES.BASE)
Hall Liuf19c44f2018-11-27 14:38:17 -08002839 .setMinSdkVersionForFine(Build.VERSION_CODES.Q)
2840 .build());
2841 switch (locationResult) {
2842 case DENIED_HARD:
2843 throw new SecurityException("Not allowed to access cell location");
2844 case DENIED_SOFT:
Meng Wanga10e89e2019-12-09 13:13:01 -08002845 return (getDefaultPhone().getPhoneType() == PhoneConstants.PHONE_TYPE_CDMA)
2846 ? new CellIdentityCdma() : new CellIdentityGsm();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002847 }
2848
Narayan Kamathf04b5a12018-01-09 11:47:15 +00002849 WorkSource workSource = getWorkSource(Binder.getCallingUid());
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002850 final long identity = Binder.clearCallingIdentity();
2851 try {
2852 if (DBG_LOC) log("getCellLocation: is active user");
Nathan Harold3ff88932018-08-14 10:19:49 -07002853 int subId = mSubscriptionController.getDefaultDataSubId();
Meng Wanga10e89e2019-12-09 13:13:01 -08002854 return (CellIdentity) sendRequest(CMD_GET_CELL_LOCATION, workSource, subId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002855 } finally {
2856 Binder.restoreCallingIdentity(identity);
2857 }
Svetoslav64fad262015-04-14 14:35:21 -07002858 }
2859
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002860 @Override
Jack Yueb1e7fe2020-02-22 19:38:58 -08002861 public String getNetworkCountryIsoForPhone(int phoneId) {
Jonathan Basseribf5362b2017-07-19 12:22:35 -07002862 // Reporting the correct network country is ambiguous when IWLAN could conflict with
2863 // registered cell info, so return a NULL country instead.
2864 final long identity = Binder.clearCallingIdentity();
2865 try {
Malcolm Chen3732c2b2018-07-18 20:15:24 -07002866 if (phoneId == SubscriptionManager.INVALID_PHONE_INDEX) {
2867 // Get default phone in this case.
2868 phoneId = SubscriptionManager.DEFAULT_PHONE_INDEX;
2869 }
Jonathan Basseribf5362b2017-07-19 12:22:35 -07002870 final int subId = mSubscriptionController.getSubIdUsingPhoneId(phoneId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002871 Phone phone = PhoneFactory.getPhone(phoneId);
Nathan Harold532f51c2020-04-21 19:31:10 -07002872 if (phone == null) return "";
2873 ServiceStateTracker sst = phone.getServiceStateTracker();
2874 if (sst == null) return "";
2875 LocaleTracker lt = sst.getLocaleTracker();
2876 if (lt == null) return "";
Shuo Qian9418a922021-03-09 11:21:16 -08002877 return lt.getCurrentCountry();
Jonathan Basseribf5362b2017-07-19 12:22:35 -07002878 } finally {
2879 Binder.restoreCallingIdentity(identity);
2880 }
Jonathan Basseribf5362b2017-07-19 12:22:35 -07002881 }
2882
Nathan Harold7c8d0f12020-05-28 20:40:31 -07002883 /**
2884 * This method was removed due to potential issues caused by performing partial
2885 * updates of service state, and lack of a credible use case.
2886 *
2887 * This has the ability to break the telephony implementation by disabling notification of
2888 * changes in device connectivity. DO NOT USE THIS!
2889 */
Jonathan Basseribf5362b2017-07-19 12:22:35 -07002890 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002891 public void enableLocationUpdates() {
2892 mApp.enforceCallingOrSelfPermission(
2893 android.Manifest.permission.CONTROL_LOCATION_UPDATES, null);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002894 }
2895
Nathan Harold7c8d0f12020-05-28 20:40:31 -07002896 /**
2897 * This method was removed due to potential issues caused by performing partial
2898 * updates of service state, and lack of a credible use case.
2899 *
2900 * This has the ability to break the telephony implementation by disabling notification of
2901 * changes in device connectivity. DO NOT USE THIS!
2902 */
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002903 @Override
2904 public void disableLocationUpdates() {
2905 mApp.enforceCallingOrSelfPermission(
2906 android.Manifest.permission.CONTROL_LOCATION_UPDATES, null);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002907 }
2908
2909 @Override
2910 @SuppressWarnings("unchecked")
Philip P. Moltmann3a2772a2019-10-04 08:15:00 -07002911 public List<NeighboringCellInfo> getNeighboringCellInfo(String callingPackage,
2912 String callingFeatureId) {
Nathan Haroldf096d982020-11-18 17:18:06 -08002913 final int targetSdk = TelephonyPermissions.getTargetSdk(mApp, callingPackage);
Nathan Harolddbea45a2018-08-30 14:35:07 -07002914 if (targetSdk >= android.os.Build.VERSION_CODES.Q) {
2915 throw new SecurityException(
2916 "getNeighboringCellInfo() is unavailable to callers targeting Q+ SDK levels.");
2917 }
Nathan Haroldb4d55612018-07-20 13:13:08 -07002918
Jordan Liu1617b712019-07-10 15:06:26 -07002919 if (mAppOps.noteOp(AppOpsManager.OPSTR_NEIGHBORING_CELLS, Binder.getCallingUid(),
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002920 callingPackage) != AppOpsManager.MODE_ALLOWED) {
2921 return null;
2922 }
Svetoslav64fad262015-04-14 14:35:21 -07002923
Svetoslav Ganov4a9d4482017-06-20 19:53:35 -07002924 if (DBG_LOC) log("getNeighboringCellInfo: is active user");
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002925
Philip P. Moltmann3a2772a2019-10-04 08:15:00 -07002926 List<CellInfo> info = getAllCellInfo(callingPackage, callingFeatureId);
Nathan Haroldf180aac2018-06-01 18:43:55 -07002927 if (info == null) return null;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002928
Nathan Haroldf180aac2018-06-01 18:43:55 -07002929 List<NeighboringCellInfo> neighbors = new ArrayList<NeighboringCellInfo>();
2930 for (CellInfo ci : info) {
2931 if (ci instanceof CellInfoGsm) {
2932 neighbors.add(new NeighboringCellInfo((CellInfoGsm) ci));
2933 } else if (ci instanceof CellInfoWcdma) {
2934 neighbors.add(new NeighboringCellInfo((CellInfoWcdma) ci));
2935 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002936 }
Nathan Haroldf180aac2018-06-01 18:43:55 -07002937 return (neighbors.size()) > 0 ? neighbors : null;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002938 }
2939
Nathan Haroldfa8da0f2018-09-27 18:51:29 -07002940 private List<CellInfo> getCachedCellInfo() {
2941 List<CellInfo> cellInfos = new ArrayList<CellInfo>();
2942 for (Phone phone : PhoneFactory.getPhones()) {
2943 List<CellInfo> info = phone.getAllCellInfo();
2944 if (info != null) cellInfos.addAll(info);
2945 }
2946 return cellInfos;
2947 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002948
2949 @Override
Philip P. Moltmann3a2772a2019-10-04 08:15:00 -07002950 public List<CellInfo> getAllCellInfo(String callingPackage, String callingFeatureId) {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08002951 mApp.getSystemService(AppOpsManager.class)
Hall Liu1aa510f2017-11-22 17:40:08 -08002952 .checkPackage(Binder.getCallingUid(), callingPackage);
Hall Liuf19c44f2018-11-27 14:38:17 -08002953
2954 LocationAccessPolicy.LocationPermissionResult locationResult =
2955 LocationAccessPolicy.checkLocationPermission(mApp,
2956 new LocationAccessPolicy.LocationPermissionQuery.Builder()
2957 .setCallingPackage(callingPackage)
Philip P. Moltmann3a2772a2019-10-04 08:15:00 -07002958 .setCallingFeatureId(callingFeatureId)
Hall Liuf19c44f2018-11-27 14:38:17 -08002959 .setCallingPid(Binder.getCallingPid())
2960 .setCallingUid(Binder.getCallingUid())
2961 .setMethod("getAllCellInfo")
Nathan Harold5ae50b52019-02-20 15:46:36 -08002962 .setMinSdkVersionForCoarse(Build.VERSION_CODES.BASE)
Hall Liuf19c44f2018-11-27 14:38:17 -08002963 .setMinSdkVersionForFine(Build.VERSION_CODES.Q)
2964 .build());
2965 switch (locationResult) {
2966 case DENIED_HARD:
2967 throw new SecurityException("Not allowed to access cell info");
2968 case DENIED_SOFT:
2969 return new ArrayList<>();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002970 }
2971
Nathan Haroldf096d982020-11-18 17:18:06 -08002972 final int targetSdk = TelephonyPermissions.getTargetSdk(mApp, callingPackage);
Nathan Haroldfa8da0f2018-09-27 18:51:29 -07002973 if (targetSdk >= android.os.Build.VERSION_CODES.Q) {
2974 return getCachedCellInfo();
2975 }
2976
Svetoslav Ganov4a9d4482017-06-20 19:53:35 -07002977 if (DBG_LOC) log("getAllCellInfo: is active user");
Narayan Kamathf04b5a12018-01-09 11:47:15 +00002978 WorkSource workSource = getWorkSource(Binder.getCallingUid());
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002979 final long identity = Binder.clearCallingIdentity();
2980 try {
2981 List<CellInfo> cellInfos = new ArrayList<CellInfo>();
2982 for (Phone phone : PhoneFactory.getPhones()) {
Nathan Harold3ff88932018-08-14 10:19:49 -07002983 final List<CellInfo> info = (List<CellInfo>) sendRequest(
Nathan Harold92bed182018-10-12 18:16:49 -07002984 CMD_GET_ALL_CELL_INFO, null, phone, workSource);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002985 if (info != null) cellInfos.addAll(info);
2986 }
2987 return cellInfos;
2988 } finally {
2989 Binder.restoreCallingIdentity(identity);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002990 }
2991 }
2992
Sailesh Nepalbd76e4e2013-10-27 13:59:44 -07002993 @Override
Philip P. Moltmann3a2772a2019-10-04 08:15:00 -07002994 public void requestCellInfoUpdate(int subId, ICellInfoCallback cb, String callingPackage,
2995 String callingFeatureId) {
2996 requestCellInfoUpdateInternal(subId, cb, callingPackage, callingFeatureId,
2997 getWorkSource(Binder.getCallingUid()));
Nathan Haroldfa8da0f2018-09-27 18:51:29 -07002998 }
2999
3000 @Override
Philip P. Moltmann3a2772a2019-10-04 08:15:00 -07003001 public void requestCellInfoUpdateWithWorkSource(int subId, ICellInfoCallback cb,
3002 String callingPackage, String callingFeatureId, WorkSource workSource) {
Nathan Haroldfa8da0f2018-09-27 18:51:29 -07003003 enforceModifyPermission();
Philip P. Moltmann3a2772a2019-10-04 08:15:00 -07003004 requestCellInfoUpdateInternal(subId, cb, callingPackage, callingFeatureId, workSource);
Nathan Haroldfa8da0f2018-09-27 18:51:29 -07003005 }
3006
Philip P. Moltmann3a2772a2019-10-04 08:15:00 -07003007 private void requestCellInfoUpdateInternal(int subId, ICellInfoCallback cb,
3008 String callingPackage, String callingFeatureId, WorkSource workSource) {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08003009 mApp.getSystemService(AppOpsManager.class)
Nathan Haroldfa8da0f2018-09-27 18:51:29 -07003010 .checkPackage(Binder.getCallingUid(), callingPackage);
Hall Liuf19c44f2018-11-27 14:38:17 -08003011
3012 LocationAccessPolicy.LocationPermissionResult locationResult =
3013 LocationAccessPolicy.checkLocationPermission(mApp,
3014 new LocationAccessPolicy.LocationPermissionQuery.Builder()
3015 .setCallingPackage(callingPackage)
Philip P. Moltmann3a2772a2019-10-04 08:15:00 -07003016 .setCallingFeatureId(callingFeatureId)
Hall Liuf19c44f2018-11-27 14:38:17 -08003017 .setCallingPid(Binder.getCallingPid())
3018 .setCallingUid(Binder.getCallingUid())
3019 .setMethod("requestCellInfoUpdate")
Hall Liud60acc92020-05-21 17:09:35 -07003020 .setMinSdkVersionForCoarse(Build.VERSION_CODES.BASE)
3021 .setMinSdkVersionForFine(Build.VERSION_CODES.BASE)
Hall Liuf19c44f2018-11-27 14:38:17 -08003022 .build());
3023 switch (locationResult) {
3024 case DENIED_HARD:
Nathan Haroldf096d982020-11-18 17:18:06 -08003025 if (TelephonyPermissions
3026 .getTargetSdk(mApp, callingPackage) < Build.VERSION_CODES.Q) {
Hall Liud60acc92020-05-21 17:09:35 -07003027 // Safetynet logging for b/154934934
3028 EventLog.writeEvent(0x534e4554, "154934934", Binder.getCallingUid());
3029 }
Hall Liuf19c44f2018-11-27 14:38:17 -08003030 throw new SecurityException("Not allowed to access cell info");
3031 case DENIED_SOFT:
Nathan Haroldf096d982020-11-18 17:18:06 -08003032 if (TelephonyPermissions
3033 .getTargetSdk(mApp, callingPackage) < Build.VERSION_CODES.Q) {
Hall Liud60acc92020-05-21 17:09:35 -07003034 // Safetynet logging for b/154934934
3035 EventLog.writeEvent(0x534e4554, "154934934", Binder.getCallingUid());
3036 }
Nathan Harold5320c422019-05-09 10:26:08 -07003037 try {
3038 cb.onCellInfo(new ArrayList<CellInfo>());
3039 } catch (RemoteException re) {
3040 // Drop without consequences
3041 }
Hall Liuf19c44f2018-11-27 14:38:17 -08003042 return;
Nathan Haroldfa8da0f2018-09-27 18:51:29 -07003043 }
3044
Nathan Harolda939a962019-05-09 10:13:47 -07003045
3046 final Phone phone = getPhoneFromSubId(subId);
Nathan Haroldfa8da0f2018-09-27 18:51:29 -07003047 if (phone == null) throw new IllegalArgumentException("Invalid Subscription Id: " + subId);
3048
3049 sendRequestAsync(CMD_REQUEST_CELL_INFO_UPDATE, cb, phone, workSource);
3050 }
3051
3052 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003053 public void setCellInfoListRate(int rateInMillis) {
Jack Yua8d8cb82017-01-16 10:15:34 -08003054 enforceModifyPermission();
Narayan Kamathf04b5a12018-01-09 11:47:15 +00003055 WorkSource workSource = getWorkSource(Binder.getCallingUid());
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003056
3057 final long identity = Binder.clearCallingIdentity();
3058 try {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08003059 getDefaultPhone().setCellInfoListRate(rateInMillis, workSource);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003060 } finally {
3061 Binder.restoreCallingIdentity(identity);
3062 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003063 }
3064
Shishir Agrawala9f32182016-04-12 12:00:16 -07003065 @Override
Philip P. Moltmann700a9592019-10-03 11:53:50 -07003066 public String getImeiForSlot(int slotIndex, String callingPackage, String callingFeatureId) {
Jeff Davidson913390f2018-02-23 17:11:49 -08003067 Phone phone = PhoneFactory.getPhone(slotIndex);
3068 if (phone == null) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08003069 return null;
3070 }
Jeff Davidson913390f2018-02-23 17:11:49 -08003071 int subId = phone.getSubId();
Michael Groover70af6dc2018-10-01 16:23:15 -07003072 if (!TelephonyPermissions.checkCallingOrSelfReadDeviceIdentifiers(mApp, subId,
Philip P. Moltmann700a9592019-10-03 11:53:50 -07003073 callingPackage, callingFeatureId, "getImeiForSlot")) {
Jeff Davidson913390f2018-02-23 17:11:49 -08003074 return null;
3075 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003076
3077 final long identity = Binder.clearCallingIdentity();
3078 try {
3079 return phone.getImei();
3080 } finally {
3081 Binder.restoreCallingIdentity(identity);
3082 }
Shishir Agrawala9f32182016-04-12 12:00:16 -07003083 }
3084
3085 @Override
David Kelly5e06a7f2018-03-12 14:10:59 +00003086 public String getTypeAllocationCodeForSlot(int slotIndex) {
3087 Phone phone = PhoneFactory.getPhone(slotIndex);
3088 String tac = null;
3089 if (phone != null) {
3090 String imei = phone.getImei();
3091 tac = imei == null ? null : imei.substring(0, TYPE_ALLOCATION_CODE_LENGTH);
3092 }
3093 return tac;
3094 }
3095
3096 @Override
Philip P. Moltmann700a9592019-10-03 11:53:50 -07003097 public String getMeidForSlot(int slotIndex, String callingPackage, String callingFeatureId) {
Jeff Davidson913390f2018-02-23 17:11:49 -08003098 Phone phone = PhoneFactory.getPhone(slotIndex);
3099 if (phone == null) {
Jack Yu2af8d712017-03-15 17:14:14 -07003100 return null;
3101 }
Nazanin Bakhshif782e562018-12-11 15:15:39 -08003102
Jeff Davidson913390f2018-02-23 17:11:49 -08003103 int subId = phone.getSubId();
Michael Groover70af6dc2018-10-01 16:23:15 -07003104 if (!TelephonyPermissions.checkCallingOrSelfReadDeviceIdentifiers(mApp, subId,
Philip P. Moltmann700a9592019-10-03 11:53:50 -07003105 callingPackage, callingFeatureId, "getMeidForSlot")) {
Jeff Davidson913390f2018-02-23 17:11:49 -08003106 return null;
3107 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003108
3109 final long identity = Binder.clearCallingIdentity();
3110 try {
3111 return phone.getMeid();
3112 } finally {
3113 Binder.restoreCallingIdentity(identity);
3114 }
Jack Yu2af8d712017-03-15 17:14:14 -07003115 }
3116
3117 @Override
David Kelly5e06a7f2018-03-12 14:10:59 +00003118 public String getManufacturerCodeForSlot(int slotIndex) {
3119 Phone phone = PhoneFactory.getPhone(slotIndex);
3120 String manufacturerCode = null;
3121 if (phone != null) {
3122 String meid = phone.getMeid();
3123 manufacturerCode = meid == null ? null : meid.substring(0, MANUFACTURER_CODE_LENGTH);
3124 }
3125 return manufacturerCode;
3126 }
3127
3128 @Override
Philip P. Moltmann700a9592019-10-03 11:53:50 -07003129 public String getDeviceSoftwareVersionForSlot(int slotIndex, String callingPackage,
3130 String callingFeatureId) {
Jeff Davidson913390f2018-02-23 17:11:49 -08003131 Phone phone = PhoneFactory.getPhone(slotIndex);
3132 if (phone == null) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08003133 return null;
3134 }
Jeff Davidson913390f2018-02-23 17:11:49 -08003135 int subId = phone.getSubId();
3136 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
Philip P. Moltmann700a9592019-10-03 11:53:50 -07003137 mApp, subId, callingPackage, callingFeatureId,
3138 "getDeviceSoftwareVersionForSlot")) {
Jeff Davidson913390f2018-02-23 17:11:49 -08003139 return null;
3140 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003141
3142 final long identity = Binder.clearCallingIdentity();
3143 try {
3144 return phone.getDeviceSvn();
3145 } finally {
3146 Binder.restoreCallingIdentity(identity);
3147 }
Shishir Agrawala9f32182016-04-12 12:00:16 -07003148 }
3149
fionaxu43304da2017-11-27 22:51:16 -08003150 @Override
3151 public int getSubscriptionCarrierId(int subId) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003152 final long identity = Binder.clearCallingIdentity();
3153 try {
3154 final Phone phone = getPhone(subId);
3155 return phone == null ? TelephonyManager.UNKNOWN_CARRIER_ID : phone.getCarrierId();
3156 } finally {
3157 Binder.restoreCallingIdentity(identity);
3158 }
fionaxu43304da2017-11-27 22:51:16 -08003159 }
3160
3161 @Override
3162 public String getSubscriptionCarrierName(int subId) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003163 final long identity = Binder.clearCallingIdentity();
3164 try {
3165 final Phone phone = getPhone(subId);
3166 return phone == null ? null : phone.getCarrierName();
3167 } finally {
3168 Binder.restoreCallingIdentity(identity);
3169 }
fionaxu43304da2017-11-27 22:51:16 -08003170 }
3171
calvinpanffe225e2018-11-01 19:43:06 +08003172 @Override
chen xu0026ca62019-03-06 15:28:50 -08003173 public int getSubscriptionSpecificCarrierId(int subId) {
chen xu25637222018-11-04 17:17:00 -08003174 final long identity = Binder.clearCallingIdentity();
3175 try {
3176 final Phone phone = getPhone(subId);
3177 return phone == null ? TelephonyManager.UNKNOWN_CARRIER_ID
chen xu0026ca62019-03-06 15:28:50 -08003178 : phone.getSpecificCarrierId();
chen xu25637222018-11-04 17:17:00 -08003179 } finally {
3180 Binder.restoreCallingIdentity(identity);
3181 }
3182 }
3183
3184 @Override
chen xu0026ca62019-03-06 15:28:50 -08003185 public String getSubscriptionSpecificCarrierName(int subId) {
chen xu25637222018-11-04 17:17:00 -08003186 final long identity = Binder.clearCallingIdentity();
3187 try {
3188 final Phone phone = getPhone(subId);
chen xu0026ca62019-03-06 15:28:50 -08003189 return phone == null ? null : phone.getSpecificCarrierName();
chen xu25637222018-11-04 17:17:00 -08003190 } finally {
3191 Binder.restoreCallingIdentity(identity);
3192 }
3193 }
3194
chen xu651eec72018-11-11 19:03:44 -08003195 @Override
chen xu864e11c2018-12-06 22:10:03 -08003196 public int getCarrierIdFromMccMnc(int slotIndex, String mccmnc, boolean isSubscriptionMccMnc) {
3197 if (!isSubscriptionMccMnc) {
3198 enforceReadPrivilegedPermission("getCarrierIdFromMccMnc");
3199 }
chen xu651eec72018-11-11 19:03:44 -08003200 final Phone phone = PhoneFactory.getPhone(slotIndex);
3201 if (phone == null) {
3202 return TelephonyManager.UNKNOWN_CARRIER_ID;
3203 }
3204 final long identity = Binder.clearCallingIdentity();
3205 try {
3206 return CarrierResolver.getCarrierIdFromMccMnc(phone.getContext(), mccmnc);
3207 } finally {
3208 Binder.restoreCallingIdentity(identity);
3209 }
3210 }
3211
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003212 //
3213 // Internal helper methods.
3214 //
3215
Sanket Padaweee13a9b2016-03-08 17:30:28 -08003216 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003217 * Make sure the caller has the MODIFY_PHONE_STATE permission.
3218 *
3219 * @throws SecurityException if the caller does not have the required permission
3220 */
3221 private void enforceModifyPermission() {
3222 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.MODIFY_PHONE_STATE, null);
3223 }
3224
Shuo Qiancd19c462020-01-16 20:51:11 -08003225 /**
3226 * Make sure the caller is system.
3227 *
3228 * @throws SecurityException if the caller is not system.
3229 */
Rambo Wanga5cc9b72021-01-07 10:51:54 -08003230 private static void enforceSystemCaller() {
Shuo Qiancd19c462020-01-16 20:51:11 -08003231 if (Binder.getCallingUid() != Process.SYSTEM_UID) {
3232 throw new SecurityException("Caller must be system");
3233 }
3234 }
3235
Shuo Qian3b6ee772019-11-13 17:43:31 -08003236 private void enforceActiveEmergencySessionPermission() {
3237 mApp.enforceCallingOrSelfPermission(
3238 android.Manifest.permission.READ_ACTIVE_EMERGENCY_SESSION, null);
3239 }
3240
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003241 /**
3242 * Make sure the caller has the CALL_PHONE permission.
3243 *
3244 * @throws SecurityException if the caller does not have the required permission
3245 */
3246 private void enforceCallPermission() {
3247 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.CALL_PHONE, null);
3248 }
3249
paulhu5a773602019-08-23 19:17:33 +08003250 private void enforceSettingsPermission() {
3251 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.NETWORK_SETTINGS, null);
Stuart Scott8eef64f2015-04-08 15:13:54 -07003252 }
3253
Michele Berionne5e411512020-11-13 02:36:59 +00003254 private void enforceRebootPermission() {
3255 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.REBOOT, null);
3256 }
3257
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003258 private String createTelUrl(String number) {
3259 if (TextUtils.isEmpty(number)) {
3260 return null;
3261 }
3262
Jake Hambye994d462014-02-03 13:10:13 -08003263 return "tel:" + number;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003264 }
3265
Ihab Awadf9e92732013-12-05 18:02:52 -08003266 private static void log(String msg) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003267 Log.d(LOG_TAG, "[PhoneIntfMgr] " + msg);
3268 }
3269
Naveen Kalla1fd79bd2014-08-08 00:48:59 -07003270 private static void logv(String msg) {
3271 Log.v(LOG_TAG, "[PhoneIntfMgr] " + msg);
3272 }
3273
Ihab Awadf9e92732013-12-05 18:02:52 -08003274 private static void loge(String msg) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003275 Log.e(LOG_TAG, "[PhoneIntfMgr] " + msg);
3276 }
3277
Robert Greenwalt36b23af2015-07-06 17:59:14 -07003278 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003279 public int getActivePhoneType() {
Shishir Agrawala9f32182016-04-12 12:00:16 -07003280 return getActivePhoneTypeForSlot(getSlotForDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07003281 }
3282
Sanket Padawe356d7632015-06-22 14:03:32 -07003283 @Override
Sanket Padawe13bac7b2017-03-20 15:04:47 -07003284 public int getActivePhoneTypeForSlot(int slotIndex) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003285 final long identity = Binder.clearCallingIdentity();
3286 try {
3287 final Phone phone = PhoneFactory.getPhone(slotIndex);
3288 if (phone == null) {
3289 return PhoneConstants.PHONE_TYPE_NONE;
3290 } else {
3291 return phone.getPhoneType();
3292 }
3293 } finally {
3294 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07003295 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003296 }
3297
3298 /**
3299 * Returns the CDMA ERI icon index to display
3300 */
Robert Greenwalt36b23af2015-07-06 17:59:14 -07003301 @Override
Philip P. Moltmann700a9592019-10-03 11:53:50 -07003302 public int getCdmaEriIconIndex(String callingPackage, String callingFeatureId) {
3303 return getCdmaEriIconIndexForSubscriber(getDefaultSubscription(), callingPackage,
3304 callingFeatureId);
Wink Saville36469e72014-06-11 15:17:00 -07003305 }
3306
Sanket Padawe356d7632015-06-22 14:03:32 -07003307 @Override
Philip P. Moltmann700a9592019-10-03 11:53:50 -07003308 public int getCdmaEriIconIndexForSubscriber(int subId, String callingPackage,
3309 String callingFeatureId) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08003310 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
Philip P. Moltmann700a9592019-10-03 11:53:50 -07003311 mApp, subId, callingPackage, callingFeatureId,
3312 "getCdmaEriIconIndexForSubscriber")) {
Robert Greenwalt36b23af2015-07-06 17:59:14 -07003313 return -1;
3314 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003315
3316 final long identity = Binder.clearCallingIdentity();
3317 try {
3318 final Phone phone = getPhone(subId);
3319 if (phone != null) {
3320 return phone.getCdmaEriIconIndex();
3321 } else {
3322 return -1;
3323 }
3324 } finally {
3325 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07003326 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003327 }
3328
3329 /**
3330 * Returns the CDMA ERI icon mode,
3331 * 0 - ON
3332 * 1 - FLASHING
3333 */
Robert Greenwalt36b23af2015-07-06 17:59:14 -07003334 @Override
Philip P. Moltmann700a9592019-10-03 11:53:50 -07003335 public int getCdmaEriIconMode(String callingPackage, String callingFeatureId) {
3336 return getCdmaEriIconModeForSubscriber(getDefaultSubscription(), callingPackage,
3337 callingFeatureId);
Wink Saville36469e72014-06-11 15:17:00 -07003338 }
3339
Sanket Padawe356d7632015-06-22 14:03:32 -07003340 @Override
Philip P. Moltmann700a9592019-10-03 11:53:50 -07003341 public int getCdmaEriIconModeForSubscriber(int subId, String callingPackage,
3342 String callingFeatureId) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08003343 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
Philip P. Moltmann700a9592019-10-03 11:53:50 -07003344 mApp, subId, callingPackage, callingFeatureId,
3345 "getCdmaEriIconModeForSubscriber")) {
Robert Greenwalt36b23af2015-07-06 17:59:14 -07003346 return -1;
3347 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003348
3349 final long identity = Binder.clearCallingIdentity();
3350 try {
3351 final Phone phone = getPhone(subId);
3352 if (phone != null) {
3353 return phone.getCdmaEriIconMode();
3354 } else {
3355 return -1;
3356 }
3357 } finally {
3358 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07003359 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003360 }
3361
3362 /**
3363 * Returns the CDMA ERI text,
3364 */
Robert Greenwalt36b23af2015-07-06 17:59:14 -07003365 @Override
Philip P. Moltmann700a9592019-10-03 11:53:50 -07003366 public String getCdmaEriText(String callingPackage, String callingFeatureId) {
3367 return getCdmaEriTextForSubscriber(getDefaultSubscription(), callingPackage,
3368 callingFeatureId);
Wink Saville36469e72014-06-11 15:17:00 -07003369 }
3370
Sanket Padawe356d7632015-06-22 14:03:32 -07003371 @Override
Philip P. Moltmann700a9592019-10-03 11:53:50 -07003372 public String getCdmaEriTextForSubscriber(int subId, String callingPackage,
3373 String callingFeatureId) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08003374 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
Philip P. Moltmann700a9592019-10-03 11:53:50 -07003375 mApp, subId, callingPackage, callingFeatureId,
3376 "getCdmaEriIconTextForSubscriber")) {
Robert Greenwalt36b23af2015-07-06 17:59:14 -07003377 return null;
3378 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003379
3380 final long identity = Binder.clearCallingIdentity();
3381 try {
3382 final Phone phone = getPhone(subId);
3383 if (phone != null) {
3384 return phone.getCdmaEriText();
3385 } else {
3386 return null;
3387 }
3388 } finally {
3389 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07003390 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003391 }
3392
3393 /**
Junda Liuca05d5d2014-08-14 22:36:34 -07003394 * Returns the CDMA MDN.
3395 */
Sanket Padawe356d7632015-06-22 14:03:32 -07003396 @Override
Wink Savilleb564aae2014-10-23 10:18:09 -07003397 public String getCdmaMdn(int subId) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08003398 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
3399 mApp, subId, "getCdmaMdn");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003400
3401 final long identity = Binder.clearCallingIdentity();
3402 try {
3403 final Phone phone = getPhone(subId);
Nazanin Bakhshif782e562018-12-11 15:15:39 -08003404 if (phone != null && phone.getPhoneType() == PhoneConstants.PHONE_TYPE_CDMA) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003405 return phone.getLine1Number();
3406 } else {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08003407 loge("getCdmaMdn: no phone found. Invalid subId: " + subId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003408 return null;
3409 }
3410 } finally {
3411 Binder.restoreCallingIdentity(identity);
Junda Liuca05d5d2014-08-14 22:36:34 -07003412 }
3413 }
3414
3415 /**
3416 * Returns the CDMA MIN.
3417 */
Sanket Padawe356d7632015-06-22 14:03:32 -07003418 @Override
Wink Savilleb564aae2014-10-23 10:18:09 -07003419 public String getCdmaMin(int subId) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08003420 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
3421 mApp, subId, "getCdmaMin");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003422
3423 final long identity = Binder.clearCallingIdentity();
3424 try {
3425 final Phone phone = getPhone(subId);
3426 if (phone != null && phone.getPhoneType() == PhoneConstants.PHONE_TYPE_CDMA) {
3427 return phone.getCdmaMin();
3428 } else {
3429 return null;
3430 }
3431 } finally {
3432 Binder.restoreCallingIdentity(identity);
Junda Liuca05d5d2014-08-14 22:36:34 -07003433 }
3434 }
3435
Hall Liud892bec2018-11-30 14:51:45 -08003436 @Override
3437 public void requestNumberVerification(PhoneNumberRange range, long timeoutMillis,
3438 INumberVerificationCallback callback, String callingPackage) {
3439 if (mApp.checkCallingOrSelfPermission(android.Manifest.permission.MODIFY_PHONE_STATE)
3440 != PERMISSION_GRANTED) {
3441 throw new SecurityException("Caller must hold the MODIFY_PHONE_STATE permission");
3442 }
3443 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
3444
3445 String authorizedPackage = NumberVerificationManager.getAuthorizedPackage(mApp);
3446 if (!TextUtils.equals(callingPackage, authorizedPackage)) {
Hall Liub9d8feb2021-01-13 10:28:04 -08003447 throw new SecurityException("Calling package must be configured in the device config: "
3448 + "calling package: " + callingPackage
3449 + ", configured package: " + authorizedPackage);
Hall Liud892bec2018-11-30 14:51:45 -08003450 }
3451
3452 if (range == null) {
3453 throw new NullPointerException("Range must be non-null");
3454 }
3455
3456 timeoutMillis = Math.min(timeoutMillis,
Hall Liubd069e32019-02-28 18:56:30 -08003457 TelephonyManager.getMaxNumberVerificationTimeoutMillis());
Hall Liud892bec2018-11-30 14:51:45 -08003458
3459 NumberVerificationManager.getInstance().requestVerification(range, callback, timeoutMillis);
3460 }
3461
Junda Liuca05d5d2014-08-14 22:36:34 -07003462 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003463 * Returns true if CDMA provisioning needs to run.
3464 */
3465 public boolean needsOtaServiceProvisioning() {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003466 final long identity = Binder.clearCallingIdentity();
3467 try {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08003468 return getDefaultPhone().needsOtaServiceProvisioning();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003469 } finally {
3470 Binder.restoreCallingIdentity(identity);
3471 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003472 }
3473
3474 /**
Shishir Agrawal76d5da92014-11-09 16:17:25 -08003475 * Sets the voice mail number of a given subId.
3476 */
3477 @Override
3478 public boolean setVoiceMailNumber(int subId, String alphaTag, String number) {
Shuo Qian2c0ae432019-12-05 11:40:37 -08003479 TelephonyPermissions.enforceCallingOrSelfCarrierPrivilege(
3480 mApp, subId, "setVoiceMailNumber");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003481
3482 final long identity = Binder.clearCallingIdentity();
3483 try {
3484 Boolean success = (Boolean) sendRequest(CMD_SET_VOICEMAIL_NUMBER,
3485 new Pair<String, String>(alphaTag, number), new Integer(subId));
3486 return success;
3487 } finally {
3488 Binder.restoreCallingIdentity(identity);
3489 }
Shishir Agrawal76d5da92014-11-09 16:17:25 -08003490 }
3491
Ta-wei Yen87c49842016-05-13 21:19:52 -07003492 @Override
Ta-wei Yenc9df0432017-04-17 17:09:07 -07003493 public Bundle getVisualVoicemailSettings(String callingPackage, int subId) {
3494 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
Tyler Gunn5ddfdc92019-10-31 13:08:23 -07003495 TelecomManager tm = mApp.getSystemService(TelecomManager.class);
3496 String systemDialer = tm.getSystemDialerPackage();
Ta-wei Yenc9df0432017-04-17 17:09:07 -07003497 if (!TextUtils.equals(callingPackage, systemDialer)) {
3498 throw new SecurityException("caller must be system dialer");
3499 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003500
3501 final long identity = Binder.clearCallingIdentity();
3502 try {
3503 PhoneAccountHandle phoneAccountHandle = PhoneAccountHandleConverter.fromSubId(subId);
3504 if (phoneAccountHandle == null) {
3505 return null;
3506 }
Nazanin Bakhshif782e562018-12-11 15:15:39 -08003507 return VisualVoicemailSettingsUtil.dump(mApp, phoneAccountHandle);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003508 } finally {
3509 Binder.restoreCallingIdentity(identity);
Ta-wei Yenc9df0432017-04-17 17:09:07 -07003510 }
Ta-wei Yenc9df0432017-04-17 17:09:07 -07003511 }
3512
3513 @Override
Philip P. Moltmann700a9592019-10-03 11:53:50 -07003514 public String getVisualVoicemailPackageName(String callingPackage, String callingFeatureId,
3515 int subId) {
Ta-wei Yendca928f2017-01-10 16:17:08 -08003516 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
Jeff Davidson7e17e312018-02-13 18:17:36 -08003517 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
Philip P. Moltmann700a9592019-10-03 11:53:50 -07003518 mApp, subId, callingPackage, callingFeatureId,
3519 "getVisualVoicemailPackageName")) {
Ta-wei Yendca928f2017-01-10 16:17:08 -08003520 return null;
3521 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003522
Jeff Davidsona8e4e242018-03-15 17:16:18 -07003523 final long identity = Binder.clearCallingIdentity();
3524 try {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08003525 return RemoteVvmTaskManager.getRemotePackage(mApp, subId).getPackageName();
Jeff Davidsona8e4e242018-03-15 17:16:18 -07003526 } finally {
3527 Binder.restoreCallingIdentity(identity);
3528 }
Ta-wei Yendca928f2017-01-10 16:17:08 -08003529 }
3530
3531 @Override
Ta-wei Yenb6929602016-05-24 15:48:27 -07003532 public void enableVisualVoicemailSmsFilter(String callingPackage, int subId,
3533 VisualVoicemailSmsFilterSettings settings) {
3534 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003535
3536 final long identity = Binder.clearCallingIdentity();
3537 try {
3538 VisualVoicemailSmsFilterConfig.enableVisualVoicemailSmsFilter(
Nazanin Bakhshif782e562018-12-11 15:15:39 -08003539 mApp, callingPackage, subId, settings);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003540 } finally {
3541 Binder.restoreCallingIdentity(identity);
3542 }
Ta-wei Yen87c49842016-05-13 21:19:52 -07003543 }
3544
3545 @Override
Ta-wei Yenb6929602016-05-24 15:48:27 -07003546 public void disableVisualVoicemailSmsFilter(String callingPackage, int subId) {
3547 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003548
3549 final long identity = Binder.clearCallingIdentity();
3550 try {
3551 VisualVoicemailSmsFilterConfig.disableVisualVoicemailSmsFilter(
Nazanin Bakhshif782e562018-12-11 15:15:39 -08003552 mApp, callingPackage, subId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003553 } finally {
3554 Binder.restoreCallingIdentity(identity);
3555 }
Ta-wei Yen87c49842016-05-13 21:19:52 -07003556 }
3557
3558 @Override
Ta-wei Yenb6929602016-05-24 15:48:27 -07003559 public VisualVoicemailSmsFilterSettings getVisualVoicemailSmsFilterSettings(
3560 String callingPackage, int subId) {
3561 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003562
3563 final long identity = Binder.clearCallingIdentity();
3564 try {
3565 return VisualVoicemailSmsFilterConfig.getVisualVoicemailSmsFilterSettings(
Nazanin Bakhshif782e562018-12-11 15:15:39 -08003566 mApp, callingPackage, subId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003567 } finally {
3568 Binder.restoreCallingIdentity(identity);
3569 }
Ta-wei Yen87c49842016-05-13 21:19:52 -07003570 }
3571
3572 @Override
Ta-wei Yen30a69c82016-12-27 14:52:32 -08003573 public VisualVoicemailSmsFilterSettings getActiveVisualVoicemailSmsFilterSettings(int subId) {
Brad Ebinger35c841c2018-10-01 10:40:55 -07003574 enforceReadPrivilegedPermission("getActiveVisualVoicemailSmsFilterSettings");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003575
3576 final long identity = Binder.clearCallingIdentity();
3577 try {
3578 return VisualVoicemailSmsFilterConfig.getActiveVisualVoicemailSmsFilterSettings(
Nazanin Bakhshif782e562018-12-11 15:15:39 -08003579 mApp, subId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003580 } finally {
3581 Binder.restoreCallingIdentity(identity);
3582 }
Ta-wei Yen30a69c82016-12-27 14:52:32 -08003583 }
3584
3585 @Override
Philip P. Moltmann2f6f8ce2020-03-18 18:17:02 -07003586 public void sendVisualVoicemailSmsForSubscriber(String callingPackage,
3587 String callingAttributionTag, int subId, String number, int port, String text,
3588 PendingIntent sentIntent) {
Ta-wei Yen30a69c82016-12-27 14:52:32 -08003589 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
Ta-wei Yen527a9c02017-01-06 15:29:25 -08003590 enforceVisualVoicemailPackage(callingPackage, subId);
Ta-wei Yen30a69c82016-12-27 14:52:32 -08003591 enforceSendSmsPermission();
Amit Mahajandccb3f12019-05-13 13:48:32 -07003592 SmsController smsController = PhoneFactory.getSmsController();
Philip P. Moltmann2f6f8ce2020-03-18 18:17:02 -07003593 smsController.sendVisualVoicemailSmsForSubscriber(callingPackage, callingAttributionTag,
3594 subId, number, port, text, sentIntent);
Ta-wei Yen87c49842016-05-13 21:19:52 -07003595 }
Amit Mahajandccb3f12019-05-13 13:48:32 -07003596
Shishir Agrawal76d5da92014-11-09 16:17:25 -08003597 /**
fionaxu0152e512016-11-14 13:36:14 -08003598 * Sets the voice activation state of a given subId.
3599 */
3600 @Override
3601 public void setVoiceActivationState(int subId, int activationState) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08003602 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
3603 mApp, subId, "setVoiceActivationState");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003604
3605 final long identity = Binder.clearCallingIdentity();
3606 try {
3607 final Phone phone = getPhone(subId);
3608 if (phone != null) {
3609 phone.setVoiceActivationState(activationState);
3610 } else {
3611 loge("setVoiceActivationState fails with invalid subId: " + subId);
3612 }
3613 } finally {
3614 Binder.restoreCallingIdentity(identity);
fionaxu0152e512016-11-14 13:36:14 -08003615 }
3616 }
3617
3618 /**
3619 * Sets the data activation state of a given subId.
3620 */
3621 @Override
3622 public void setDataActivationState(int subId, int activationState) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08003623 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
3624 mApp, subId, "setDataActivationState");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003625
3626 final long identity = Binder.clearCallingIdentity();
3627 try {
3628 final Phone phone = getPhone(subId);
3629 if (phone != null) {
3630 phone.setDataActivationState(activationState);
3631 } else {
Taesu Leef8fbed92019-10-07 18:47:02 +09003632 loge("setDataActivationState fails with invalid subId: " + subId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003633 }
3634 } finally {
3635 Binder.restoreCallingIdentity(identity);
fionaxu0152e512016-11-14 13:36:14 -08003636 }
3637 }
3638
3639 /**
3640 * Returns the voice activation state of a given subId.
3641 */
3642 @Override
3643 public int getVoiceActivationState(int subId, String callingPackage) {
Brad Ebinger35c841c2018-10-01 10:40:55 -07003644 enforceReadPrivilegedPermission("getVoiceActivationState");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003645
fionaxu0152e512016-11-14 13:36:14 -08003646 final Phone phone = getPhone(subId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003647 final long identity = Binder.clearCallingIdentity();
3648 try {
3649 if (phone != null) {
3650 return phone.getVoiceActivationState();
3651 } else {
3652 return TelephonyManager.SIM_ACTIVATION_STATE_UNKNOWN;
3653 }
3654 } finally {
3655 Binder.restoreCallingIdentity(identity);
fionaxu0152e512016-11-14 13:36:14 -08003656 }
3657 }
3658
3659 /**
3660 * Returns the data activation state of a given subId.
3661 */
3662 @Override
3663 public int getDataActivationState(int subId, String callingPackage) {
Brad Ebinger35c841c2018-10-01 10:40:55 -07003664 enforceReadPrivilegedPermission("getDataActivationState");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003665
fionaxu0152e512016-11-14 13:36:14 -08003666 final Phone phone = getPhone(subId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003667 final long identity = Binder.clearCallingIdentity();
3668 try {
3669 if (phone != null) {
3670 return phone.getDataActivationState();
3671 } else {
3672 return TelephonyManager.SIM_ACTIVATION_STATE_UNKNOWN;
3673 }
3674 } finally {
3675 Binder.restoreCallingIdentity(identity);
fionaxu0152e512016-11-14 13:36:14 -08003676 }
3677 }
3678
3679 /**
Wink Saville36469e72014-06-11 15:17:00 -07003680 * Returns the unread count of voicemails for a subId
3681 */
Sanket Padawe356d7632015-06-22 14:03:32 -07003682 @Override
Philip P. Moltmann700a9592019-10-03 11:53:50 -07003683 public int getVoiceMessageCountForSubscriber(int subId, String callingPackage,
3684 String callingFeatureId) {
Brad Ebingerf7664ba2018-11-29 12:43:38 -08003685 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
Philip P. Moltmann700a9592019-10-03 11:53:50 -07003686 mApp, subId, callingPackage, callingFeatureId,
3687 "getVoiceMessageCountForSubscriber")) {
Brad Ebingerf7664ba2018-11-29 12:43:38 -08003688 return 0;
3689 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003690 final long identity = Binder.clearCallingIdentity();
3691 try {
3692 final Phone phone = getPhone(subId);
3693 if (phone != null) {
3694 return phone.getVoiceMessageCount();
3695 } else {
3696 return 0;
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 /**
pkanwar8a4dcfb2017-01-19 13:43:16 -08003704 * returns true, if the device is in a state where both voice and data
3705 * are supported simultaneously. This can change based on location or network condition.
3706 */
3707 @Override
3708 public boolean isConcurrentVoiceAndDataAllowed(int subId) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003709 final long identity = Binder.clearCallingIdentity();
3710 try {
3711 final Phone phone = getPhone(subId);
3712 return (phone == null ? false : phone.isConcurrentVoiceAndDataAllowed());
3713 } finally {
3714 Binder.restoreCallingIdentity(identity);
3715 }
pkanwar8a4dcfb2017-01-19 13:43:16 -08003716 }
3717
3718 /**
fionaxu235cc5e2017-03-06 22:25:57 -08003719 * Send the dialer code if called from the current default dialer or the caller has
3720 * carrier privilege.
3721 * @param inputCode The dialer code to send
3722 */
3723 @Override
3724 public void sendDialerSpecialCode(String callingPackage, String inputCode) {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08003725 final Phone defaultPhone = getDefaultPhone();
fionaxu235cc5e2017-03-06 22:25:57 -08003726 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
Tyler Gunn5ddfdc92019-10-31 13:08:23 -07003727 TelecomManager tm = defaultPhone.getContext().getSystemService(TelecomManager.class);
3728 String defaultDialer = tm.getDefaultDialerPackage();
fionaxu235cc5e2017-03-06 22:25:57 -08003729 if (!TextUtils.equals(callingPackage, defaultDialer)) {
Shuo Qian2c0ae432019-12-05 11:40:37 -08003730 TelephonyPermissions.enforceCallingOrSelfCarrierPrivilege(mApp,
Jeff Davidson7e17e312018-02-13 18:17:36 -08003731 getDefaultSubscription(), "sendDialerSpecialCode");
fionaxu235cc5e2017-03-06 22:25:57 -08003732 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003733
3734 final long identity = Binder.clearCallingIdentity();
3735 try {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08003736 defaultPhone.sendDialerSpecialCode(inputCode);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003737 } finally {
3738 Binder.restoreCallingIdentity(identity);
3739 }
fionaxu235cc5e2017-03-06 22:25:57 -08003740 }
3741
Pengquan Menga1bb6272018-09-06 09:59:22 -07003742 @Override
3743 public int getNetworkSelectionMode(int subId) {
shilufc958392020-01-20 11:36:01 -08003744 TelephonyPermissions
3745 .enforeceCallingOrSelfReadPrecisePhoneStatePermissionOrCarrierPrivilege(
3746 mApp, subId, "getNetworkSelectionMode");
3747 final long identity = Binder.clearCallingIdentity();
3748 try {
3749 if (!isActiveSubscription(subId)) {
3750 return TelephonyManager.NETWORK_SELECTION_MODE_UNKNOWN;
3751 }
3752 return (int) sendRequest(CMD_GET_NETWORK_SELECTION_MODE, null /* argument */, subId);
3753 } finally {
3754 Binder.restoreCallingIdentity(identity);
Pengquan Menge92a50d2018-09-21 15:54:48 -07003755 }
Pengquan Menga1bb6272018-09-06 09:59:22 -07003756 }
3757
Brad Ebinger35c841c2018-10-01 10:40:55 -07003758 @Override
Brad Ebingerb2b65522019-03-15 13:48:47 -07003759 public boolean isInEmergencySmsMode() {
3760 enforceReadPrivilegedPermission("isInEmergencySmsMode");
3761 final long identity = Binder.clearCallingIdentity();
3762 try {
3763 for (Phone phone : PhoneFactory.getPhones()) {
3764 if (phone.isInEmergencySmsMode()) {
3765 return true;
3766 }
3767 }
3768 } finally {
3769 Binder.restoreCallingIdentity(identity);
3770 }
3771 return false;
3772 }
3773
shilu366312e2019-12-17 09:28:10 -08003774 /**
3775 * Requires carrier privileges or READ_PRECISE_PHONE_STATE permission.
3776 * @param subId The subscription to use to check the configuration.
3777 * @param c The callback that will be used to send the result.
3778 */
Brad Ebingerb2b65522019-03-15 13:48:47 -07003779 @Override
Brad Ebinger9878b0b2018-11-08 17:43:22 -08003780 public void registerImsRegistrationCallback(int subId, IImsRegistrationCallback c)
3781 throws RemoteException {
Rambo Wang37f9c242020-02-10 14:45:28 -08003782 TelephonyPermissions.enforeceCallingOrSelfReadPrecisePhoneStatePermissionOrCarrierPrivilege(
3783 mApp, subId, "registerImsRegistrationCallback");
shilu366312e2019-12-17 09:28:10 -08003784
Brad Ebingerbc7dd582019-10-17 17:03:22 -07003785 if (!ImsManager.isImsSupportedOnDevice(mApp)) {
3786 throw new ServiceSpecificException(ImsException.CODE_ERROR_UNSUPPORTED_OPERATION,
3787 "IMS not available on device.");
3788 }
Brad Ebinger35c841c2018-10-01 10:40:55 -07003789 final long token = Binder.clearCallingIdentity();
3790 try {
Brad Ebinger4ae57f92019-01-09 16:51:30 -08003791 // TODO: Refactor to remove ImsManager dependence and query through ImsPhone directly.
Nazanin Bakhshif782e562018-12-11 15:15:39 -08003792 ImsManager.getInstance(mApp, getSlotIndexOrException(subId))
Brad Ebinger35c841c2018-10-01 10:40:55 -07003793 .addRegistrationCallbackForSubscription(c, subId);
Brad Ebinger1ce9c432019-07-16 13:19:44 -07003794 } catch (ImsException e) {
3795 throw new ServiceSpecificException(e.getCode());
Brad Ebinger35c841c2018-10-01 10:40:55 -07003796 } finally {
3797 Binder.restoreCallingIdentity(token);
3798 }
3799 }
3800
shilu366312e2019-12-17 09:28:10 -08003801 /**
3802 * Requires carrier privileges or READ_PRECISE_PHONE_STATE permission.
3803 * @param subId The subscription to use to check the configuration.
3804 * @param c The callback that will be used to send the result.
3805 */
Brad Ebinger35c841c2018-10-01 10:40:55 -07003806 @Override
Brad Ebinger9878b0b2018-11-08 17:43:22 -08003807 public void unregisterImsRegistrationCallback(int subId, IImsRegistrationCallback c) {
Rambo Wang37f9c242020-02-10 14:45:28 -08003808 TelephonyPermissions.enforeceCallingOrSelfReadPrecisePhoneStatePermissionOrCarrierPrivilege(
3809 mApp, subId, "unregisterImsRegistrationCallback");
Brad Ebinger4ae57f92019-01-09 16:51:30 -08003810 if (!SubscriptionManager.isValidSubscriptionId(subId)) {
3811 throw new IllegalArgumentException("Invalid Subscription ID: " + subId);
3812 }
Meng Wangafbc5852019-09-19 17:37:13 -07003813 final long token = Binder.clearCallingIdentity();
3814 try {
Meng Wang8ea57e32020-06-25 11:03:56 -07003815 // TODO(b/159910732): Remove ImsManager dependence and query through ImsPhone directly.
Meng Wangafbc5852019-09-19 17:37:13 -07003816 ImsManager.getInstance(mApp, getSlotIndexOrException(subId))
3817 .removeRegistrationCallbackForSubscription(c, subId);
3818 } catch (ImsException e) {
3819 Log.i(LOG_TAG, "unregisterImsRegistrationCallback: " + subId
3820 + "is inactive, ignoring unregister.");
3821 // If the subscription is no longer active, just return, since the callback
3822 // will already have been removed internally.
3823 } finally {
3824 Binder.restoreCallingIdentity(token);
3825 }
Brad Ebinger35c841c2018-10-01 10:40:55 -07003826 }
3827
Brad Ebingera34a6c22019-10-22 17:36:18 -07003828 /**
3829 * Get the IMS service registration state for the MmTelFeature associated with this sub id.
3830 */
3831 @Override
3832 public void getImsMmTelRegistrationState(int subId, IIntegerConsumer consumer) {
3833 enforceReadPrivilegedPermission("getImsMmTelRegistrationState");
3834 if (!ImsManager.isImsSupportedOnDevice(mApp)) {
3835 throw new ServiceSpecificException(ImsException.CODE_ERROR_UNSUPPORTED_OPERATION,
3836 "IMS not available on device.");
3837 }
3838 final long token = Binder.clearCallingIdentity();
3839 try {
3840 Phone phone = getPhone(subId);
3841 if (phone == null) {
3842 Log.w(LOG_TAG, "getImsMmTelRegistrationState: called with an invalid subscription '"
3843 + subId + "'");
3844 throw new ServiceSpecificException(ImsException.CODE_ERROR_INVALID_SUBSCRIPTION);
3845 }
3846 phone.getImsRegistrationState(regState -> {
3847 try {
3848 consumer.accept((regState == null)
3849 ? RegistrationManager.REGISTRATION_STATE_NOT_REGISTERED : regState);
3850 } catch (RemoteException e) {
3851 // Ignore if the remote process is no longer available to call back.
3852 Log.w(LOG_TAG, "getImsMmTelRegistrationState: callback not available.");
3853 }
3854 });
3855 } finally {
3856 Binder.restoreCallingIdentity(token);
3857 }
3858 }
3859
3860 /**
3861 * Get the transport type for the IMS service registration state.
3862 */
3863 @Override
3864 public void getImsMmTelRegistrationTransportType(int subId, IIntegerConsumer consumer) {
Rambo Wang37f9c242020-02-10 14:45:28 -08003865 TelephonyPermissions.enforeceCallingOrSelfReadPrecisePhoneStatePermissionOrCarrierPrivilege(
3866 mApp, subId, "getImsMmTelRegistrationTransportType");
Brad Ebingera34a6c22019-10-22 17:36:18 -07003867 if (!ImsManager.isImsSupportedOnDevice(mApp)) {
3868 throw new ServiceSpecificException(ImsException.CODE_ERROR_UNSUPPORTED_OPERATION,
3869 "IMS not available on device.");
3870 }
3871 final long token = Binder.clearCallingIdentity();
3872 try {
3873 Phone phone = getPhone(subId);
3874 if (phone == null) {
3875 Log.w(LOG_TAG, "getImsMmTelRegistrationState: called with an invalid subscription '"
3876 + subId + "'");
3877 throw new ServiceSpecificException(ImsException.CODE_ERROR_INVALID_SUBSCRIPTION);
3878 }
3879 phone.getImsRegistrationTech(regTech -> {
3880 // Convert registration tech from ImsRegistrationImplBase -> RegistrationManager
3881 int regTechConverted = (regTech == null)
3882 ? ImsRegistrationImplBase.REGISTRATION_TECH_NONE : regTech;
3883 regTechConverted = RegistrationManager.IMS_REG_TO_ACCESS_TYPE_MAP.get(
3884 regTechConverted);
3885 try {
3886 consumer.accept(regTechConverted);
3887 } catch (RemoteException e) {
3888 // Ignore if the remote process is no longer available to call back.
3889 Log.w(LOG_TAG, "getImsMmTelRegistrationState: callback not available.");
3890 }
3891 });
3892 } finally {
3893 Binder.restoreCallingIdentity(token);
3894 }
3895 }
3896
shilu366312e2019-12-17 09:28:10 -08003897 /**
3898 * Requires carrier privileges or READ_PRECISE_PHONE_STATE permission.
3899 * @param subId The subscription to use to check the configuration.
3900 * @param c The callback that will be used to send the result.
3901 */
Brad Ebinger35c841c2018-10-01 10:40:55 -07003902 @Override
Brad Ebinger9878b0b2018-11-08 17:43:22 -08003903 public void registerMmTelCapabilityCallback(int subId, IImsCapabilityCallback c)
3904 throws RemoteException {
Rambo Wang37f9c242020-02-10 14:45:28 -08003905 TelephonyPermissions.enforeceCallingOrSelfReadPrecisePhoneStatePermissionOrCarrierPrivilege(
3906 mApp, subId, "registerMmTelCapabilityCallback");
Brad Ebingerbc7dd582019-10-17 17:03:22 -07003907 if (!ImsManager.isImsSupportedOnDevice(mApp)) {
3908 throw new ServiceSpecificException(ImsException.CODE_ERROR_UNSUPPORTED_OPERATION,
3909 "IMS not available on device.");
3910 }
Brad Ebinger35c841c2018-10-01 10:40:55 -07003911 // TODO: Refactor to remove ImsManager dependence and query through ImsPhone directly.
3912 final long token = Binder.clearCallingIdentity();
3913 try {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08003914 ImsManager.getInstance(mApp, getSlotIndexOrException(subId))
Brad Ebinger35c841c2018-10-01 10:40:55 -07003915 .addCapabilitiesCallbackForSubscription(c, subId);
Brad Ebinger1ce9c432019-07-16 13:19:44 -07003916 } catch (ImsException e) {
3917 throw new ServiceSpecificException(e.getCode());
Brad Ebinger35c841c2018-10-01 10:40:55 -07003918 } finally {
3919 Binder.restoreCallingIdentity(token);
3920 }
3921 }
3922
shilu366312e2019-12-17 09:28:10 -08003923 /**
3924 * Requires carrier privileges or READ_PRECISE_PHONE_STATE permission.
3925 * @param subId The subscription to use to check the configuration.
3926 * @param c The callback that will be used to send the result.
3927 */
Brad Ebinger35c841c2018-10-01 10:40:55 -07003928 @Override
Brad Ebinger9878b0b2018-11-08 17:43:22 -08003929 public void unregisterMmTelCapabilityCallback(int subId, IImsCapabilityCallback c) {
Rambo Wang37f9c242020-02-10 14:45:28 -08003930 TelephonyPermissions.enforeceCallingOrSelfReadPrecisePhoneStatePermissionOrCarrierPrivilege(
3931 mApp, subId, "unregisterMmTelCapabilityCallback");
Brad Ebinger4ae57f92019-01-09 16:51:30 -08003932 if (!SubscriptionManager.isValidSubscriptionId(subId)) {
3933 throw new IllegalArgumentException("Invalid Subscription ID: " + subId);
3934 }
Meng Wangafbc5852019-09-19 17:37:13 -07003935
3936 final long token = Binder.clearCallingIdentity();
3937 try {
Meng Wang8ea57e32020-06-25 11:03:56 -07003938 // TODO(b/159910732): Remove ImsManager dependence and query through ImsPhone directly.
Meng Wangafbc5852019-09-19 17:37:13 -07003939 ImsManager.getInstance(mApp, getSlotIndexOrException(subId))
Brad Ebinger4ae57f92019-01-09 16:51:30 -08003940 .removeCapabilitiesCallbackForSubscription(c, subId);
Meng Wangafbc5852019-09-19 17:37:13 -07003941 } catch (ImsException e) {
3942 Log.i(LOG_TAG, "unregisterMmTelCapabilityCallback: " + subId
3943 + "is inactive, ignoring unregister.");
3944 // If the subscription is no longer active, just return, since the callback
3945 // will already have been removed internally.
3946 } finally {
3947 Binder.restoreCallingIdentity(token);
3948 }
Brad Ebinger35c841c2018-10-01 10:40:55 -07003949 }
3950
3951 @Override
Brad Ebinger9878b0b2018-11-08 17:43:22 -08003952 public boolean isCapable(int subId, int capability, int regTech) {
3953 enforceReadPrivilegedPermission("isCapable");
Brad Ebinger35c841c2018-10-01 10:40:55 -07003954 // TODO: Refactor to remove ImsManager dependence and query through ImsPhone directly.
3955 final long token = Binder.clearCallingIdentity();
3956 try {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08003957 return ImsManager.getInstance(mApp,
Brad Ebinger35c841c2018-10-01 10:40:55 -07003958 getSlotIndexOrException(subId)).queryMmTelCapability(capability, regTech);
Brad Ebinger1ce9c432019-07-16 13:19:44 -07003959 } catch (com.android.ims.ImsException e) {
Brad Ebinger35c841c2018-10-01 10:40:55 -07003960 Log.w(LOG_TAG, "IMS isCapable - service unavailable: " + e.getMessage());
3961 return false;
Brad Ebinger1ce9c432019-07-16 13:19:44 -07003962 } catch (ImsException e) {
Brad Ebinger6b5ac222019-02-04 14:36:52 -08003963 Log.i(LOG_TAG, "isCapable: " + subId + " is inactive, returning false.");
3964 return false;
Brad Ebinger35c841c2018-10-01 10:40:55 -07003965 } finally {
3966 Binder.restoreCallingIdentity(token);
3967 }
3968 }
3969
3970 @Override
Brad Ebinger9878b0b2018-11-08 17:43:22 -08003971 public boolean isAvailable(int subId, int capability, int regTech) {
3972 enforceReadPrivilegedPermission("isAvailable");
Brad Ebinger35c841c2018-10-01 10:40:55 -07003973 final long token = Binder.clearCallingIdentity();
3974 try {
3975 Phone phone = getPhone(subId);
3976 if (phone == null) return false;
3977 return phone.isImsCapabilityAvailable(capability, regTech);
Daniel Bright5e40e4e2020-03-11 16:35:39 -07003978 } catch (com.android.ims.ImsException e) {
3979 Log.w(LOG_TAG, "IMS isAvailable - service unavailable: " + e.getMessage());
3980 return false;
Brad Ebinger35c841c2018-10-01 10:40:55 -07003981 } finally {
3982 Binder.restoreCallingIdentity(token);
3983 }
3984 }
3985
Brad Ebingerbc7dd582019-10-17 17:03:22 -07003986 /**
3987 * Determines if the MmTel feature capability is supported by the carrier configuration for this
3988 * subscription.
3989 * @param subId The subscription to use to check the configuration.
3990 * @param callback The callback that will be used to send the result.
3991 * @param capability The MmTelFeature capability that will be used to send the result.
3992 * @param transportType The transport type of the MmTelFeature capability.
3993 */
3994 @Override
3995 public void isMmTelCapabilitySupported(int subId, IIntegerConsumer callback, int capability,
3996 int transportType) {
3997 enforceReadPrivilegedPermission("isMmTelCapabilitySupported");
3998 if (!ImsManager.isImsSupportedOnDevice(mApp)) {
3999 throw new ServiceSpecificException(ImsException.CODE_ERROR_UNSUPPORTED_OPERATION,
4000 "IMS not available on device.");
4001 }
4002 final long token = Binder.clearCallingIdentity();
4003 try {
4004 int slotId = getSlotIndex(subId);
4005 if (slotId <= SubscriptionManager.INVALID_SIM_SLOT_INDEX) {
4006 Log.w(LOG_TAG, "isMmTelCapabilitySupported: called with an inactive subscription '"
4007 + subId + "'");
4008 throw new ServiceSpecificException(ImsException.CODE_ERROR_INVALID_SUBSCRIPTION);
4009 }
4010 ImsManager.getInstance(mApp, slotId).isSupported(capability,
4011 transportType, aBoolean -> {
4012 try {
4013 callback.accept((aBoolean == null) ? 0 : (aBoolean ? 1 : 0));
4014 } catch (RemoteException e) {
4015 Log.w(LOG_TAG, "isMmTelCapabilitySupported: remote caller is not "
4016 + "running. Ignore");
4017 }
4018 });
4019 } finally {
4020 Binder.restoreCallingIdentity(token);
4021 }
4022 }
4023
shilu366312e2019-12-17 09:28:10 -08004024 /**
4025 * Requires carrier privileges or READ_PRECISE_PHONE_STATE permission.
4026 * @param subId The subscription to use to check the configuration.
4027 */
Brad Ebinger35c841c2018-10-01 10:40:55 -07004028 @Override
4029 public boolean isAdvancedCallingSettingEnabled(int subId) {
Rambo Wang37f9c242020-02-10 14:45:28 -08004030 TelephonyPermissions.enforeceCallingOrSelfReadPrecisePhoneStatePermissionOrCarrierPrivilege(
4031 mApp, subId, "isAdvancedCallingSettingEnabled");
shilu366312e2019-12-17 09:28:10 -08004032
Brad Ebinger35c841c2018-10-01 10:40:55 -07004033 // TODO: Refactor to remove ImsManager dependence and query through ImsPhone directly.
4034 final long token = Binder.clearCallingIdentity();
4035 try {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08004036 return ImsManager.getInstance(mApp,
Brad Ebinger35c841c2018-10-01 10:40:55 -07004037 getSlotIndexOrException(subId)).isEnhanced4gLteModeSettingEnabledByUser();
Brad Ebinger1ce9c432019-07-16 13:19:44 -07004038 } catch (ImsException e) {
4039 throw new ServiceSpecificException(e.getCode());
Brad Ebinger35c841c2018-10-01 10:40:55 -07004040 } finally {
4041 Binder.restoreCallingIdentity(token);
4042 }
4043 }
4044
4045 @Override
Brad Ebinger1c162042019-02-21 14:49:10 -08004046 public void setAdvancedCallingSettingEnabled(int subId, boolean isEnabled) {
Brad Ebinger35c841c2018-10-01 10:40:55 -07004047 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(mApp, subId,
Brad Ebinger1c162042019-02-21 14:49:10 -08004048 "setAdvancedCallingSettingEnabled");
Brad Ebinger35c841c2018-10-01 10:40:55 -07004049 final long identity = Binder.clearCallingIdentity();
4050 try {
4051 // TODO: Refactor to remove ImsManager dependence and query through ImsPhone directly.
Nazanin Bakhshif782e562018-12-11 15:15:39 -08004052 ImsManager.getInstance(mApp,
Brad Ebinger35c841c2018-10-01 10:40:55 -07004053 getSlotIndexOrException(subId)).setEnhanced4gLteModeSetting(isEnabled);
Brad Ebinger1ce9c432019-07-16 13:19:44 -07004054 } catch (ImsException e) {
4055 throw new ServiceSpecificException(e.getCode());
Brad Ebinger35c841c2018-10-01 10:40:55 -07004056 } finally {
4057 Binder.restoreCallingIdentity(identity);
4058 }
4059 }
4060
shilu366312e2019-12-17 09:28:10 -08004061 /**
4062 * Requires carrier privileges or READ_PRECISE_PHONE_STATE permission.
4063 * @param subId The subscription to use to check the configuration.
4064 */
Brad Ebinger35c841c2018-10-01 10:40:55 -07004065 @Override
Brad Ebinger9878b0b2018-11-08 17:43:22 -08004066 public boolean isVtSettingEnabled(int subId) {
Rambo Wang37f9c242020-02-10 14:45:28 -08004067 TelephonyPermissions.enforeceCallingOrSelfReadPrecisePhoneStatePermissionOrCarrierPrivilege(
4068 mApp, subId, "isVtSettingEnabled");
Brad Ebinger35c841c2018-10-01 10:40:55 -07004069 final long identity = Binder.clearCallingIdentity();
4070 try {
4071 // TODO: Refactor to remove ImsManager dependence and query through ImsPhone directly.
Brad Ebinger1ce9c432019-07-16 13:19:44 -07004072 return ImsManager.getInstance(mApp, getSlotIndexOrException(subId)).isVtEnabledByUser();
4073 } catch (ImsException e) {
4074 throw new ServiceSpecificException(e.getCode());
Brad Ebinger35c841c2018-10-01 10:40:55 -07004075 } finally {
4076 Binder.restoreCallingIdentity(identity);
4077 }
4078 }
4079
4080 @Override
Brad Ebinger1c162042019-02-21 14:49:10 -08004081 public void setVtSettingEnabled(int subId, boolean isEnabled) {
Brad Ebinger35c841c2018-10-01 10:40:55 -07004082 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(mApp, subId,
Brad Ebinger1c162042019-02-21 14:49:10 -08004083 "setVtSettingEnabled");
Brad Ebinger35c841c2018-10-01 10:40:55 -07004084 final long identity = Binder.clearCallingIdentity();
4085 try {
4086 // TODO: Refactor to remove ImsManager dependence and query through ImsPhone directly.
Nazanin Bakhshif782e562018-12-11 15:15:39 -08004087 ImsManager.getInstance(mApp, getSlotIndexOrException(subId)).setVtSetting(isEnabled);
Brad Ebinger1ce9c432019-07-16 13:19:44 -07004088 } catch (ImsException e) {
4089 throw new ServiceSpecificException(e.getCode());
Brad Ebinger35c841c2018-10-01 10:40:55 -07004090 } finally {
4091 Binder.restoreCallingIdentity(identity);
4092 }
4093 }
4094
shilu366312e2019-12-17 09:28:10 -08004095 /**
4096 * Requires carrier privileges or READ_PRECISE_PHONE_STATE permission.
4097 * @param subId The subscription to use to check the configuration.
4098 */
Brad Ebinger35c841c2018-10-01 10:40:55 -07004099 @Override
4100 public boolean isVoWiFiSettingEnabled(int subId) {
Rambo Wang37f9c242020-02-10 14:45:28 -08004101 TelephonyPermissions.enforeceCallingOrSelfReadPrecisePhoneStatePermissionOrCarrierPrivilege(
4102 mApp, subId, "isVoWiFiSettingEnabled");
Brad Ebinger35c841c2018-10-01 10:40:55 -07004103 final long identity = Binder.clearCallingIdentity();
4104 try {
4105 // TODO: Refactor to remove ImsManager dependence and query through ImsPhone directly.
Nazanin Bakhshif782e562018-12-11 15:15:39 -08004106 return ImsManager.getInstance(mApp,
Brad Ebinger35c841c2018-10-01 10:40:55 -07004107 getSlotIndexOrException(subId)).isWfcEnabledByUser();
Brad Ebinger1ce9c432019-07-16 13:19:44 -07004108 } catch (ImsException e) {
4109 throw new ServiceSpecificException(e.getCode());
Brad Ebinger35c841c2018-10-01 10:40:55 -07004110 } finally {
4111 Binder.restoreCallingIdentity(identity);
4112 }
4113 }
4114
4115 @Override
Brad Ebinger1c162042019-02-21 14:49:10 -08004116 public void setVoWiFiSettingEnabled(int subId, boolean isEnabled) {
Brad Ebinger35c841c2018-10-01 10:40:55 -07004117 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(mApp, subId,
Brad Ebinger1c162042019-02-21 14:49:10 -08004118 "setVoWiFiSettingEnabled");
Brad Ebinger35c841c2018-10-01 10:40:55 -07004119 final long identity = Binder.clearCallingIdentity();
4120 try {
4121 // TODO: Refactor to remove ImsManager dependence and query through ImsPhone directly.
Nazanin Bakhshif782e562018-12-11 15:15:39 -08004122 ImsManager.getInstance(mApp, getSlotIndexOrException(subId)).setWfcSetting(isEnabled);
Brad Ebinger1ce9c432019-07-16 13:19:44 -07004123 } catch (ImsException e) {
4124 throw new ServiceSpecificException(e.getCode());
Brad Ebinger35c841c2018-10-01 10:40:55 -07004125 } finally {
4126 Binder.restoreCallingIdentity(identity);
4127 }
4128 }
4129
shilu366312e2019-12-17 09:28:10 -08004130 /**
Sooraj Sasindrane655add2020-11-23 19:40:38 -08004131 * @return true if the user's setting for Voice over Cross SIM is enabled and false if it is not
4132 * Requires carrier privileges or READ_PRECISE_PHONE_STATE permission.
4133 * @param subId The subscription to use to check the configuration.
4134 */
4135 @Override
4136 public boolean isCrossSimCallingEnabledByUser(int subId) {
4137 TelephonyPermissions.enforeceCallingOrSelfReadPrecisePhoneStatePermissionOrCarrierPrivilege(
4138 mApp, subId, "isCrossSimCallingEnabledByUser");
4139 final long identity = Binder.clearCallingIdentity();
4140 try {
4141 // TODO: Refactor to remove ImsManager dependence and query through ImsPhone directly.
4142 return ImsManager.getInstance(mApp,
4143 getSlotIndexOrException(subId)).isCrossSimCallingEnabledByUser();
4144 } catch (ImsException e) {
4145 throw new ServiceSpecificException(e.getCode());
4146 } finally {
4147 Binder.restoreCallingIdentity(identity);
4148 }
4149 }
4150
4151 /**
4152 * Sets the user's setting for whether or not Voice over Cross SIM is enabled.
4153 * Requires MODIFY_PHONE_STATE permission.
4154 * @param subId The subscription to use to check the configuration.
4155 * @param isEnabled true if the user's setting for Voice over Cross SIM is enabled,
4156 * false otherwise
4157 */
4158 @Override
4159 public void setCrossSimCallingEnabled(int subId, boolean isEnabled) {
4160 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(mApp, subId,
4161 "setCrossSimCallingEnabled");
4162 final long identity = Binder.clearCallingIdentity();
4163 try {
4164 // TODO: Refactor to remove ImsManager dependence and query through ImsPhone directly.
4165 ImsManager.getInstance(mApp, getSlotIndexOrException(subId))
4166 .setCrossSimCallingEnabled(isEnabled);
4167 } catch (ImsException e) {
4168 throw new ServiceSpecificException(e.getCode());
4169 } finally {
4170 Binder.restoreCallingIdentity(identity);
4171 }
4172 }
4173
4174 /**
shilu366312e2019-12-17 09:28:10 -08004175 * Requires carrier privileges or READ_PRECISE_PHONE_STATE permission.
4176 * @param subId The subscription to use to check the configuration.
4177 */
Brad Ebinger35c841c2018-10-01 10:40:55 -07004178 @Override
4179 public boolean isVoWiFiRoamingSettingEnabled(int subId) {
Rambo Wang37f9c242020-02-10 14:45:28 -08004180 TelephonyPermissions.enforeceCallingOrSelfReadPrecisePhoneStatePermissionOrCarrierPrivilege(
4181 mApp, subId, "isVoWiFiRoamingSettingEnabled");
Brad Ebinger35c841c2018-10-01 10:40:55 -07004182 final long identity = Binder.clearCallingIdentity();
4183 try {
4184 // TODO: Refactor to remove ImsManager dependence and query through ImsPhone directly.
Nazanin Bakhshif782e562018-12-11 15:15:39 -08004185 return ImsManager.getInstance(mApp,
Brad Ebinger35c841c2018-10-01 10:40:55 -07004186 getSlotIndexOrException(subId)).isWfcRoamingEnabledByUser();
Brad Ebinger1ce9c432019-07-16 13:19:44 -07004187 } catch (ImsException e) {
4188 throw new ServiceSpecificException(e.getCode());
Brad Ebinger35c841c2018-10-01 10:40:55 -07004189 } finally {
4190 Binder.restoreCallingIdentity(identity);
4191 }
4192 }
4193
4194 @Override
Brad Ebinger1c162042019-02-21 14:49:10 -08004195 public void setVoWiFiRoamingSettingEnabled(int subId, boolean isEnabled) {
Brad Ebinger35c841c2018-10-01 10:40:55 -07004196 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(mApp, subId,
Brad Ebinger1c162042019-02-21 14:49:10 -08004197 "setVoWiFiRoamingSettingEnabled");
Brad Ebinger35c841c2018-10-01 10:40:55 -07004198 final long identity = Binder.clearCallingIdentity();
4199 try {
4200 // TODO: Refactor to remove ImsManager dependence and query through ImsPhone directly.
Nazanin Bakhshif782e562018-12-11 15:15:39 -08004201 ImsManager.getInstance(mApp,
Brad Ebinger35c841c2018-10-01 10:40:55 -07004202 getSlotIndexOrException(subId)).setWfcRoamingSetting(isEnabled);
Brad Ebinger1ce9c432019-07-16 13:19:44 -07004203 } catch (ImsException e) {
4204 throw new ServiceSpecificException(e.getCode());
Brad Ebinger35c841c2018-10-01 10:40:55 -07004205 } finally {
4206 Binder.restoreCallingIdentity(identity);
4207 }
4208 }
4209
4210 @Override
4211 public void setVoWiFiNonPersistent(int subId, boolean isCapable, int mode) {
4212 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(mApp, subId,
4213 "setVoWiFiNonPersistent");
4214 final long identity = Binder.clearCallingIdentity();
4215 try {
4216 // TODO: Refactor to remove ImsManager dependence and query through ImsPhone directly.
Nazanin Bakhshif782e562018-12-11 15:15:39 -08004217 ImsManager.getInstance(mApp,
Brad Ebinger2d29c012019-05-07 18:33:46 -07004218 getSlotIndexOrException(subId)).setWfcNonPersistent(isCapable, mode);
Brad Ebinger1ce9c432019-07-16 13:19:44 -07004219 } catch (ImsException e) {
4220 throw new ServiceSpecificException(e.getCode());
Brad Ebinger35c841c2018-10-01 10:40:55 -07004221 } finally {
4222 Binder.restoreCallingIdentity(identity);
4223 }
4224 }
4225
shilu366312e2019-12-17 09:28:10 -08004226 /**
4227 * Requires carrier privileges or READ_PRECISE_PHONE_STATE permission.
4228 * @param subId The subscription to use to check the configuration.
4229 */
Brad Ebinger35c841c2018-10-01 10:40:55 -07004230 @Override
4231 public int getVoWiFiModeSetting(int subId) {
Rambo Wang37f9c242020-02-10 14:45:28 -08004232 TelephonyPermissions.enforeceCallingOrSelfReadPrecisePhoneStatePermissionOrCarrierPrivilege(
4233 mApp, subId, "getVoWiFiModeSetting");
Brad Ebinger35c841c2018-10-01 10:40:55 -07004234 final long identity = Binder.clearCallingIdentity();
4235 try {
4236 // TODO: Refactor to remove ImsManager dependence and query through ImsPhone directly.
Nazanin Bakhshif782e562018-12-11 15:15:39 -08004237 return ImsManager.getInstance(mApp,
Brad Ebinger35c841c2018-10-01 10:40:55 -07004238 getSlotIndexOrException(subId)).getWfcMode(false /*isRoaming*/);
Brad Ebinger1ce9c432019-07-16 13:19:44 -07004239 } catch (ImsException e) {
4240 throw new ServiceSpecificException(e.getCode());
Brad Ebinger35c841c2018-10-01 10:40:55 -07004241 } finally {
4242 Binder.restoreCallingIdentity(identity);
4243 }
4244 }
4245
4246 @Override
4247 public void setVoWiFiModeSetting(int subId, int mode) {
4248 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(mApp, subId,
4249 "setVoWiFiModeSetting");
4250 final long identity = Binder.clearCallingIdentity();
4251 try {
4252 // TODO: Refactor to remove ImsManager dependence and query through ImsPhone directly.
Nazanin Bakhshif782e562018-12-11 15:15:39 -08004253 ImsManager.getInstance(mApp,
Brad Ebinger35c841c2018-10-01 10:40:55 -07004254 getSlotIndexOrException(subId)).setWfcMode(mode, false /*isRoaming*/);
Brad Ebinger1ce9c432019-07-16 13:19:44 -07004255 } catch (ImsException e) {
4256 throw new ServiceSpecificException(e.getCode());
Brad Ebinger35c841c2018-10-01 10:40:55 -07004257 } finally {
4258 Binder.restoreCallingIdentity(identity);
4259 }
4260 }
4261
4262 @Override
4263 public int getVoWiFiRoamingModeSetting(int subId) {
4264 enforceReadPrivilegedPermission("getVoWiFiRoamingModeSetting");
4265 final long identity = Binder.clearCallingIdentity();
4266 try {
4267 // TODO: Refactor to remove ImsManager dependence and query through ImsPhone directly.
Nazanin Bakhshif782e562018-12-11 15:15:39 -08004268 return ImsManager.getInstance(mApp,
Brad Ebinger35c841c2018-10-01 10:40:55 -07004269 getSlotIndexOrException(subId)).getWfcMode(true /*isRoaming*/);
Brad Ebinger1ce9c432019-07-16 13:19:44 -07004270 } catch (ImsException e) {
4271 throw new ServiceSpecificException(e.getCode());
Brad Ebinger35c841c2018-10-01 10:40:55 -07004272 } finally {
4273 Binder.restoreCallingIdentity(identity);
4274 }
4275 }
4276
4277 @Override
4278 public void setVoWiFiRoamingModeSetting(int subId, int mode) {
4279 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(mApp, subId,
4280 "setVoWiFiRoamingModeSetting");
4281 final long identity = Binder.clearCallingIdentity();
4282 try {
4283 // TODO: Refactor to remove ImsManager dependence and query through ImsPhone directly.
Nazanin Bakhshif782e562018-12-11 15:15:39 -08004284 ImsManager.getInstance(mApp,
Brad Ebinger35c841c2018-10-01 10:40:55 -07004285 getSlotIndexOrException(subId)).setWfcMode(mode, true /*isRoaming*/);
Brad Ebinger1ce9c432019-07-16 13:19:44 -07004286 } catch (ImsException e) {
4287 throw new ServiceSpecificException(e.getCode());
Brad Ebinger35c841c2018-10-01 10:40:55 -07004288 } finally {
4289 Binder.restoreCallingIdentity(identity);
4290 }
4291 }
4292
4293 @Override
4294 public void setRttCapabilitySetting(int subId, boolean isEnabled) {
4295 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(mApp, subId,
4296 "setRttCapabilityEnabled");
4297 final long identity = Binder.clearCallingIdentity();
4298 try {
4299 // TODO: Refactor to remove ImsManager dependence and query through ImsPhone directly.
Brad Ebinger1ce9c432019-07-16 13:19:44 -07004300 ImsManager.getInstance(mApp, getSlotIndexOrException(subId)).setRttEnabled(isEnabled);
4301 } catch (ImsException e) {
4302 throw new ServiceSpecificException(e.getCode());
Brad Ebinger35c841c2018-10-01 10:40:55 -07004303 } finally {
4304 Binder.restoreCallingIdentity(identity);
4305 }
4306 }
4307
shilu366312e2019-12-17 09:28:10 -08004308 /**
4309 * Requires carrier privileges or READ_PRECISE_PHONE_STATE permission.
4310 * @param subId The subscription to use to check the configuration.
4311 */
Brad Ebinger35c841c2018-10-01 10:40:55 -07004312 @Override
4313 public boolean isTtyOverVolteEnabled(int subId) {
Rambo Wang37f9c242020-02-10 14:45:28 -08004314 TelephonyPermissions.enforeceCallingOrSelfReadPrecisePhoneStatePermissionOrCarrierPrivilege(
4315 mApp, subId, "isTtyOverVolteEnabled");
Brad Ebinger35c841c2018-10-01 10:40:55 -07004316 final long identity = Binder.clearCallingIdentity();
4317 try {
4318 // TODO: Refactor to remove ImsManager dependence and query through ImsPhone directly.
Nazanin Bakhshif782e562018-12-11 15:15:39 -08004319 return ImsManager.getInstance(mApp,
Brad Ebinger35c841c2018-10-01 10:40:55 -07004320 getSlotIndexOrException(subId)).isTtyOnVoLteCapable();
Brad Ebinger1ce9c432019-07-16 13:19:44 -07004321 } catch (ImsException e) {
4322 throw new ServiceSpecificException(e.getCode());
Brad Ebinger35c841c2018-10-01 10:40:55 -07004323 } finally {
4324 Binder.restoreCallingIdentity(identity);
4325 }
4326 }
4327
Brad Ebingerdf5b4f02018-10-31 11:24:17 -07004328 @Override
4329 public void registerImsProvisioningChangedCallback(int subId, IImsConfigCallback callback) {
4330 enforceReadPrivilegedPermission("registerImsProvisioningChangedCallback");
4331 final long identity = Binder.clearCallingIdentity();
4332 try {
Brad Ebingerd0331732020-01-16 11:21:18 -08004333 if (!isImsAvailableOnDevice()) {
4334 throw new ServiceSpecificException(ImsException.CODE_ERROR_UNSUPPORTED_OPERATION,
4335 "IMS not available on device.");
Peter Wang44b186e2020-01-13 23:33:09 -08004336 }
Brad Ebingerdf5b4f02018-10-31 11:24:17 -07004337 // TODO: Refactor to remove ImsManager dependence and query through ImsPhone directly.
Nazanin Bakhshif782e562018-12-11 15:15:39 -08004338 ImsManager.getInstance(mApp, getSlotIndexOrException(subId))
Brad Ebinger4ae57f92019-01-09 16:51:30 -08004339 .addProvisioningCallbackForSubscription(callback, subId);
Brad Ebinger1ce9c432019-07-16 13:19:44 -07004340 } catch (ImsException e) {
4341 throw new ServiceSpecificException(e.getCode());
Brad Ebingerdf5b4f02018-10-31 11:24:17 -07004342 } finally {
4343 Binder.restoreCallingIdentity(identity);
4344 }
4345 }
4346
4347 @Override
4348 public void unregisterImsProvisioningChangedCallback(int subId, IImsConfigCallback callback) {
4349 enforceReadPrivilegedPermission("unregisterImsProvisioningChangedCallback");
4350 final long identity = Binder.clearCallingIdentity();
Brad Ebinger4ae57f92019-01-09 16:51:30 -08004351 if (!SubscriptionManager.isValidSubscriptionId(subId)) {
4352 throw new IllegalArgumentException("Invalid Subscription ID: " + subId);
4353 }
Brad Ebingerdf5b4f02018-10-31 11:24:17 -07004354 try {
4355 // TODO: Refactor to remove ImsManager dependence and query through ImsPhone directly.
Nazanin Bakhshif782e562018-12-11 15:15:39 -08004356 ImsManager.getInstance(mApp, getSlotIndexOrException(subId))
Brad Ebinger4ae57f92019-01-09 16:51:30 -08004357 .removeProvisioningCallbackForSubscription(callback, subId);
Brad Ebinger1ce9c432019-07-16 13:19:44 -07004358 } catch (ImsException e) {
Brad Ebinger4ae57f92019-01-09 16:51:30 -08004359 Log.i(LOG_TAG, "unregisterImsProvisioningChangedCallback: " + subId
4360 + "is inactive, ignoring unregister.");
4361 // If the subscription is no longer active, just return, since the callback will already
4362 // have been removed internally.
Brad Ebingerdf5b4f02018-10-31 11:24:17 -07004363 } finally {
4364 Binder.restoreCallingIdentity(identity);
4365 }
4366 }
4367
allenwtsu99c623b2020-01-03 18:24:23 +08004368
4369 private void checkModifyPhoneStatePermission(int subId, String message) {
4370 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(mApp, subId,
4371 message);
4372 }
4373
4374 private boolean isImsProvisioningRequired(int subId, int capability,
4375 boolean isMmtelCapability) {
4376 Phone phone = getPhone(subId);
4377 if (phone == null) {
4378 loge("phone instance null for subid " + subId);
4379 return false;
4380 }
4381 if (isMmtelCapability) {
4382 if (!doesImsCapabilityRequireProvisioning(phone.getContext(), subId, capability)) {
4383 return false;
4384 }
4385 } else {
4386 if (!doesRcsCapabilityRequireProvisioning(phone.getContext(), subId, capability)) {
4387 return false;
4388 }
4389 }
4390 return true;
4391 }
4392
4393 @Override
4394 public void setRcsProvisioningStatusForCapability(int subId, int capability,
4395 boolean isProvisioned) {
4396 checkModifyPhoneStatePermission(subId, "setRcsProvisioningStatusForCapability");
4397
4398 final long identity = Binder.clearCallingIdentity();
4399 try {
4400 // TODO: Refactor to remove ImsManager dependence and query through ImsPhone directly.
4401 if (!isImsProvisioningRequired(subId, capability, false)) {
4402 return;
4403 }
4404
4405 // this capability requires provisioning, route to the correct API.
4406 ImsManager ims = ImsManager.getInstance(mApp, getSlotIndex(subId));
4407 switch (capability) {
4408 case RcsFeature.RcsImsCapabilities.CAPABILITY_TYPE_OPTIONS_UCE:
4409 case RcsFeature.RcsImsCapabilities.CAPABILITY_TYPE_PRESENCE_UCE:
4410 ims.setEabProvisioned(isProvisioned);
4411 break;
4412 default: {
4413 throw new IllegalArgumentException("Tried to set provisioning for "
4414 + "rcs capability '" + capability + "', which does not require "
4415 + "provisioning.");
4416 }
4417 }
4418 } finally {
4419 Binder.restoreCallingIdentity(identity);
4420 }
4421
4422 }
4423
4424
4425 @Override
4426 public boolean getRcsProvisioningStatusForCapability(int subId, int capability) {
4427 enforceReadPrivilegedPermission("getRcsProvisioningStatusForCapability");
4428 final long identity = Binder.clearCallingIdentity();
4429 try {
4430 // TODO: Refactor to remove ImsManager dependence and query through ImsPhone directly.
4431 if (!isImsProvisioningRequired(subId, capability, false)) {
4432 return true;
4433 }
4434
4435 ImsManager ims = ImsManager.getInstance(mApp, getSlotIndex(subId));
4436 switch (capability) {
4437 case RcsFeature.RcsImsCapabilities.CAPABILITY_TYPE_OPTIONS_UCE:
4438 case RcsFeature.RcsImsCapabilities.CAPABILITY_TYPE_PRESENCE_UCE:
4439 return ims.isEabProvisionedOnDevice();
4440
4441 default: {
4442 throw new IllegalArgumentException("Tried to get rcs provisioning for "
4443 + "capability '" + capability + "', which does not require "
4444 + "provisioning.");
4445 }
4446 }
4447
4448 } finally {
4449 Binder.restoreCallingIdentity(identity);
4450 }
4451 }
4452
Brad Ebingerdf5b4f02018-10-31 11:24:17 -07004453 @Override
Brad Ebinger1c8542e2019-01-14 13:43:14 -08004454 public void setImsProvisioningStatusForCapability(int subId, int capability, int tech,
4455 boolean isProvisioned) {
4456 if (tech != ImsRegistrationImplBase.REGISTRATION_TECH_IWLAN
4457 && tech != ImsRegistrationImplBase.REGISTRATION_TECH_LTE) {
4458 throw new IllegalArgumentException("Registration technology '" + tech + "' is invalid");
4459 }
allenwtsu99c623b2020-01-03 18:24:23 +08004460 checkModifyPhoneStatePermission(subId, "setImsProvisioningStatusForCapability");
Brad Ebinger1c8542e2019-01-14 13:43:14 -08004461 final long identity = Binder.clearCallingIdentity();
4462 try {
4463 // TODO: Refactor to remove ImsManager dependence and query through ImsPhone directly.
allenwtsu99c623b2020-01-03 18:24:23 +08004464 if (!isImsProvisioningRequired(subId, capability, true)) {
Brad Ebinger1c8542e2019-01-14 13:43:14 -08004465 return;
4466 }
4467
4468 // this capability requires provisioning, route to the correct API.
4469 ImsManager ims = ImsManager.getInstance(mApp, getSlotIndex(subId));
4470 switch (capability) {
4471 case MmTelFeature.MmTelCapabilities.CAPABILITY_TYPE_VOICE: {
4472 if (tech == ImsRegistrationImplBase.REGISTRATION_TECH_LTE) {
4473 ims.setVolteProvisioned(isProvisioned);
4474 } else if (tech == ImsRegistrationImplBase.REGISTRATION_TECH_IWLAN) {
4475 ims.setWfcProvisioned(isProvisioned);
4476 }
4477 break;
4478 }
4479 case MmTelFeature.MmTelCapabilities.CAPABILITY_TYPE_VIDEO: {
4480 // There is currently no difference in VT provisioning type.
4481 ims.setVtProvisioned(isProvisioned);
4482 break;
4483 }
4484 case MmTelFeature.MmTelCapabilities.CAPABILITY_TYPE_UT: {
4485 // There is no "deprecated" UT provisioning mechanism through ImsConfig, so
4486 // change the capability of the feature instead if needed.
4487 if (isMmTelCapabilityProvisionedInCache(subId, capability, tech)
4488 == isProvisioned) {
4489 // No change in provisioning.
4490 return;
4491 }
4492 cacheMmTelCapabilityProvisioning(subId, capability, tech, isProvisioned);
4493 try {
4494 ims.changeMmTelCapability(capability, tech, isProvisioned);
Brad Ebinger1ce9c432019-07-16 13:19:44 -07004495 } catch (com.android.ims.ImsException e) {
Brad Ebinger1c8542e2019-01-14 13:43:14 -08004496 loge("setImsProvisioningStatusForCapability: couldn't change UT capability"
4497 + ", Exception" + e.getMessage());
4498 }
4499 break;
4500 }
4501 default: {
allenwtsu99c623b2020-01-03 18:24:23 +08004502 throw new IllegalArgumentException("Tried to set provisioning for "
4503 + "MmTel capability '" + capability + "', which does not require "
4504 + "provisioning. ");
Brad Ebinger1c8542e2019-01-14 13:43:14 -08004505 }
4506 }
4507
4508 } finally {
4509 Binder.restoreCallingIdentity(identity);
4510 }
4511 }
4512
4513 @Override
4514 public boolean getImsProvisioningStatusForCapability(int subId, int capability, int tech) {
4515 if (tech != ImsRegistrationImplBase.REGISTRATION_TECH_IWLAN
4516 && tech != ImsRegistrationImplBase.REGISTRATION_TECH_LTE) {
4517 throw new IllegalArgumentException("Registration technology '" + tech + "' is invalid");
4518 }
4519 enforceReadPrivilegedPermission("getProvisioningStatusForCapability");
4520 final long identity = Binder.clearCallingIdentity();
4521 try {
4522 // TODO: Refactor to remove ImsManager dependence and query through ImsPhone directly.
allenwtsu99c623b2020-01-03 18:24:23 +08004523 if (!isImsProvisioningRequired(subId, capability, true)) {
Brad Ebinger1c8542e2019-01-14 13:43:14 -08004524 return true;
4525 }
4526
4527 ImsManager ims = ImsManager.getInstance(mApp, getSlotIndex(subId));
4528 switch (capability) {
4529 case MmTelFeature.MmTelCapabilities.CAPABILITY_TYPE_VOICE: {
4530 if (tech == ImsRegistrationImplBase.REGISTRATION_TECH_LTE) {
4531 return ims.isVolteProvisionedOnDevice();
4532 } else if (tech == ImsRegistrationImplBase.REGISTRATION_TECH_IWLAN) {
4533 return ims.isWfcProvisionedOnDevice();
4534 }
4535 // This should never happen, since we are checking tech above to make sure it
4536 // is either LTE or IWLAN.
4537 throw new IllegalArgumentException("Invalid radio technology for voice "
4538 + "capability.");
4539 }
4540 case MmTelFeature.MmTelCapabilities.CAPABILITY_TYPE_VIDEO: {
4541 // There is currently no difference in VT provisioning type.
4542 return ims.isVtProvisionedOnDevice();
4543 }
4544 case MmTelFeature.MmTelCapabilities.CAPABILITY_TYPE_UT: {
4545 // There is no "deprecated" UT provisioning mechanism, so get from shared prefs.
4546 return isMmTelCapabilityProvisionedInCache(subId, capability, tech);
4547 }
4548 default: {
allenwtsu99c623b2020-01-03 18:24:23 +08004549 throw new IllegalArgumentException(
4550 "Tried to get provisioning for MmTel capability '" + capability
4551 + "', which does not require provisioning.");
Brad Ebinger1c8542e2019-01-14 13:43:14 -08004552 }
4553 }
4554
4555 } finally {
4556 Binder.restoreCallingIdentity(identity);
4557 }
4558 }
4559
4560 @Override
4561 public boolean isMmTelCapabilityProvisionedInCache(int subId, int capability, int tech) {
4562 if (tech != ImsRegistrationImplBase.REGISTRATION_TECH_IWLAN
4563 && tech != ImsRegistrationImplBase.REGISTRATION_TECH_LTE) {
4564 throw new IllegalArgumentException("Registration technology '" + tech + "' is invalid");
4565 }
4566 enforceReadPrivilegedPermission("isMmTelCapabilityProvisionedInCache");
4567 int provisionedBits = getMmTelCapabilityProvisioningBitfield(subId, tech);
4568 return (provisionedBits & capability) > 0;
4569 }
4570
4571 @Override
4572 public void cacheMmTelCapabilityProvisioning(int subId, int capability, int tech,
4573 boolean isProvisioned) {
4574 if (tech != ImsRegistrationImplBase.REGISTRATION_TECH_IWLAN
4575 && tech != ImsRegistrationImplBase.REGISTRATION_TECH_LTE) {
4576 throw new IllegalArgumentException("Registration technology '" + tech + "' is invalid");
4577 }
4578 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(mApp, subId,
4579 "setProvisioningStatusForCapability");
4580 int provisionedBits = getMmTelCapabilityProvisioningBitfield(subId, tech);
4581 // If the current provisioning status for capability already matches isProvisioned,
4582 // do nothing.
4583 if (((provisionedBits & capability) > 0) == isProvisioned) {
4584 return;
4585 }
4586 if (isProvisioned) {
4587 setMmTelCapabilityProvisioningBitfield(subId, tech, (provisionedBits | capability));
4588 } else {
4589 setMmTelCapabilityProvisioningBitfield(subId, tech, (provisionedBits & ~capability));
4590 }
4591 }
4592
4593 /**
4594 * @return the bitfield containing the MmTel provisioning for the provided subscription and
4595 * technology. The bitfield should mirror the bitfield defined by
4596 * {@link MmTelFeature.MmTelCapabilities.MmTelCapability}.
4597 */
4598 private int getMmTelCapabilityProvisioningBitfield(int subId, int tech) {
4599 String key = getMmTelProvisioningKey(subId, tech);
4600 // Default is no capabilities are provisioned.
4601 return mTelephonySharedPreferences.getInt(key, 0 /*default*/);
4602 }
4603
4604 /**
4605 * Sets the MmTel capability provisioning bitfield (defined by
4606 * {@link MmTelFeature.MmTelCapabilities.MmTelCapability}) for the subscription and
4607 * technology specified.
4608 *
4609 * Note: This is a synchronous command and should not be called on UI thread.
4610 */
4611 private void setMmTelCapabilityProvisioningBitfield(int subId, int tech, int newField) {
4612 final SharedPreferences.Editor editor = mTelephonySharedPreferences.edit();
4613 String key = getMmTelProvisioningKey(subId, tech);
4614 editor.putInt(key, newField);
4615 editor.commit();
4616 }
4617
4618 private static String getMmTelProvisioningKey(int subId, int tech) {
4619 // resulting key is provision_ims_mmtel_{subId}_{tech}
4620 return PREF_PROVISION_IMS_MMTEL_PREFIX + subId + "_" + tech;
4621 }
4622
4623 /**
4624 * Query CarrierConfig to see if the specified capability requires provisioning for the
4625 * carrier associated with the subscription id.
4626 */
4627 private boolean doesImsCapabilityRequireProvisioning(Context context, int subId,
4628 int capability) {
4629 CarrierConfigManager configManager = new CarrierConfigManager(context);
4630 PersistableBundle c = configManager.getConfigForSubId(subId);
4631 boolean requireUtProvisioning = c.getBoolean(
Brad Ebinger076903f2019-05-13 10:00:22 -07004632 CarrierConfigManager.KEY_CARRIER_SUPPORTS_SS_OVER_UT_BOOL, false)
Brad Ebinger1c8542e2019-01-14 13:43:14 -08004633 && c.getBoolean(CarrierConfigManager.KEY_CARRIER_UT_PROVISIONING_REQUIRED_BOOL,
4634 false);
4635 boolean requireVoiceVtProvisioning = c.getBoolean(
4636 CarrierConfigManager.KEY_CARRIER_VOLTE_PROVISIONING_REQUIRED_BOOL, false);
4637
4638 // First check to make sure that the capability requires provisioning.
4639 switch (capability) {
4640 case MmTelFeature.MmTelCapabilities.CAPABILITY_TYPE_VOICE:
4641 // intentional fallthrough
4642 case MmTelFeature.MmTelCapabilities.CAPABILITY_TYPE_VIDEO: {
4643 if (requireVoiceVtProvisioning) {
4644 // Voice and Video requires provisioning
4645 return true;
4646 }
4647 break;
4648 }
4649 case MmTelFeature.MmTelCapabilities.CAPABILITY_TYPE_UT: {
4650 if (requireUtProvisioning) {
4651 // UT requires provisioning
4652 return true;
4653 }
4654 break;
4655 }
4656 }
4657 return false;
4658 }
4659
allenwtsu99c623b2020-01-03 18:24:23 +08004660 private boolean doesRcsCapabilityRequireProvisioning(Context context, int subId,
4661 int capability) {
4662 CarrierConfigManager configManager = new CarrierConfigManager(context);
4663 PersistableBundle c = configManager.getConfigForSubId(subId);
4664
4665 boolean requireRcsProvisioning = c.getBoolean(
4666 CarrierConfigManager.KEY_CARRIER_RCS_PROVISIONING_REQUIRED_BOOL, false);
4667
4668 // First check to make sure that the capability requires provisioning.
4669 switch (capability) {
4670 case RcsFeature.RcsImsCapabilities.CAPABILITY_TYPE_PRESENCE_UCE:
4671 // intentional fallthrough
4672 case RcsFeature.RcsImsCapabilities.CAPABILITY_TYPE_OPTIONS_UCE: {
4673 if (requireRcsProvisioning) {
4674 // OPTION or PRESENCE requires provisioning
4675 return true;
4676 }
4677 break;
4678 }
4679 }
4680 return false;
4681 }
4682
Brad Ebinger1c8542e2019-01-14 13:43:14 -08004683 @Override
Brad Ebingerdf5b4f02018-10-31 11:24:17 -07004684 public int getImsProvisioningInt(int subId, int key) {
Brad Ebinger1c8542e2019-01-14 13:43:14 -08004685 if (!SubscriptionManager.isValidSubscriptionId(subId)) {
4686 throw new IllegalArgumentException("Invalid Subscription id '" + subId + "'");
4687 }
Brad Ebingerdf5b4f02018-10-31 11:24:17 -07004688 enforceReadPrivilegedPermission("getImsProvisioningInt");
4689 final long identity = Binder.clearCallingIdentity();
4690 try {
4691 // TODO: Refactor to remove ImsManager dependence and query through ImsPhone directly.
Brad Ebinger1c8542e2019-01-14 13:43:14 -08004692 int slotId = getSlotIndex(subId);
4693 if (slotId <= SubscriptionManager.INVALID_SIM_SLOT_INDEX) {
4694 Log.w(LOG_TAG, "getImsProvisioningInt: called with an inactive subscription '"
4695 + subId + "' for key:" + key);
4696 return ImsConfigImplBase.CONFIG_RESULT_UNKNOWN;
4697 }
calvinpanb5a34062021-02-08 19:59:36 +08004698 return ImsManager.getInstance(mApp, slotId).getConfigInt(key);
Brad Ebinger1ce9c432019-07-16 13:19:44 -07004699 } catch (com.android.ims.ImsException e) {
Brad Ebinger1c8542e2019-01-14 13:43:14 -08004700 Log.w(LOG_TAG, "getImsProvisioningInt: ImsService is not available for subscription '"
4701 + subId + "' for key:" + key);
4702 return ImsConfigImplBase.CONFIG_RESULT_UNKNOWN;
Brad Ebingerdf5b4f02018-10-31 11:24:17 -07004703 } finally {
4704 Binder.restoreCallingIdentity(identity);
4705 }
4706 }
4707
4708 @Override
4709 public String getImsProvisioningString(int subId, int key) {
Brad Ebinger1c8542e2019-01-14 13:43:14 -08004710 if (!SubscriptionManager.isValidSubscriptionId(subId)) {
4711 throw new IllegalArgumentException("Invalid Subscription id '" + subId + "'");
4712 }
Brad Ebingerdf5b4f02018-10-31 11:24:17 -07004713 enforceReadPrivilegedPermission("getImsProvisioningString");
4714 final long identity = Binder.clearCallingIdentity();
4715 try {
4716 // TODO: Refactor to remove ImsManager dependence and query through ImsPhone directly.
Brad Ebinger1c8542e2019-01-14 13:43:14 -08004717 int slotId = getSlotIndex(subId);
4718 if (slotId <= SubscriptionManager.INVALID_SIM_SLOT_INDEX) {
4719 Log.w(LOG_TAG, "getImsProvisioningString: called for an inactive subscription id '"
4720 + subId + "' for key:" + key);
4721 return ProvisioningManager.STRING_QUERY_RESULT_ERROR_GENERIC;
4722 }
calvinpanb5a34062021-02-08 19:59:36 +08004723 return ImsManager.getInstance(mApp, slotId).getConfigString(key);
Brad Ebinger1ce9c432019-07-16 13:19:44 -07004724 } catch (com.android.ims.ImsException e) {
Brad Ebinger1c8542e2019-01-14 13:43:14 -08004725 Log.w(LOG_TAG, "getImsProvisioningString: ImsService is not available for sub '"
4726 + subId + "' for key:" + key);
4727 return ProvisioningManager.STRING_QUERY_RESULT_ERROR_NOT_READY;
Brad Ebingerdf5b4f02018-10-31 11:24:17 -07004728 } finally {
4729 Binder.restoreCallingIdentity(identity);
4730 }
4731 }
4732
4733 @Override
4734 public int setImsProvisioningInt(int subId, int key, int value) {
Brad Ebinger1c8542e2019-01-14 13:43:14 -08004735 if (!SubscriptionManager.isValidSubscriptionId(subId)) {
4736 throw new IllegalArgumentException("Invalid Subscription id '" + subId + "'");
4737 }
Brad Ebinger3d0b34e2018-11-15 14:13:12 -08004738 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(mApp, subId,
4739 "setImsProvisioningInt");
Brad Ebingerdf5b4f02018-10-31 11:24:17 -07004740 final long identity = Binder.clearCallingIdentity();
4741 try {
4742 // TODO: Refactor to remove ImsManager dependence and query through ImsPhone directly.
Brad Ebinger1c8542e2019-01-14 13:43:14 -08004743 int slotId = getSlotIndex(subId);
4744 if (slotId <= SubscriptionManager.INVALID_SIM_SLOT_INDEX) {
4745 Log.w(LOG_TAG, "setImsProvisioningInt: called with an inactive subscription id '"
4746 + subId + "' for key:" + key);
4747 return ImsConfigImplBase.CONFIG_RESULT_FAILED;
4748 }
calvinpanb5a34062021-02-08 19:59:36 +08004749 return ImsManager.getInstance(mApp, slotId).setConfig(key, value);
4750 } catch (com.android.ims.ImsException | RemoteException e) {
Brad Ebinger1c8542e2019-01-14 13:43:14 -08004751 Log.w(LOG_TAG, "setImsProvisioningInt: ImsService unavailable for sub '" + subId
calvinpanb5a34062021-02-08 19:59:36 +08004752 + "' for key:" + key, e);
Brad Ebinger1c8542e2019-01-14 13:43:14 -08004753 return ImsConfigImplBase.CONFIG_RESULT_FAILED;
Brad Ebingerdf5b4f02018-10-31 11:24:17 -07004754 } finally {
4755 Binder.restoreCallingIdentity(identity);
4756 }
4757 }
4758
4759 @Override
4760 public int setImsProvisioningString(int subId, int key, String value) {
Brad Ebinger1c8542e2019-01-14 13:43:14 -08004761 if (!SubscriptionManager.isValidSubscriptionId(subId)) {
4762 throw new IllegalArgumentException("Invalid Subscription id '" + subId + "'");
4763 }
Brad Ebinger3d0b34e2018-11-15 14:13:12 -08004764 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(mApp, subId,
4765 "setImsProvisioningString");
Brad Ebingerdf5b4f02018-10-31 11:24:17 -07004766 final long identity = Binder.clearCallingIdentity();
4767 try {
4768 // TODO: Refactor to remove ImsManager dependence and query through ImsPhone directly.
Brad Ebinger1c8542e2019-01-14 13:43:14 -08004769 int slotId = getSlotIndex(subId);
4770 if (slotId <= SubscriptionManager.INVALID_SIM_SLOT_INDEX) {
4771 Log.w(LOG_TAG, "setImsProvisioningString: called with an inactive subscription id '"
4772 + subId + "' for key:" + key);
4773 return ImsConfigImplBase.CONFIG_RESULT_FAILED;
4774 }
calvinpanb5a34062021-02-08 19:59:36 +08004775 return ImsManager.getInstance(mApp, slotId).setConfig(key, value);
4776 } catch (com.android.ims.ImsException | RemoteException e) {
Brad Ebinger1c8542e2019-01-14 13:43:14 -08004777 Log.w(LOG_TAG, "setImsProvisioningString: ImsService unavailable for sub '" + subId
calvinpanb5a34062021-02-08 19:59:36 +08004778 + "' for key:" + key, e);
Brad Ebinger1c8542e2019-01-14 13:43:14 -08004779 return ImsConfigImplBase.CONFIG_RESULT_FAILED;
Brad Ebingerdf5b4f02018-10-31 11:24:17 -07004780 } finally {
4781 Binder.restoreCallingIdentity(identity);
4782 }
4783 }
4784
Brad Ebinger1ce9c432019-07-16 13:19:44 -07004785 private int getSlotIndexOrException(int subId) throws ImsException {
Brad Ebinger35c841c2018-10-01 10:40:55 -07004786 int slotId = SubscriptionManager.getSlotIndex(subId);
4787 if (!SubscriptionManager.isValidSlotIndex(slotId)) {
Brad Ebinger1ce9c432019-07-16 13:19:44 -07004788 throw new ImsException("Invalid Subscription Id, subId=" + subId,
4789 ImsException.CODE_ERROR_INVALID_SUBSCRIPTION);
Brad Ebinger35c841c2018-10-01 10:40:55 -07004790 }
4791 return slotId;
4792 }
4793
Brad Ebinger1c8542e2019-01-14 13:43:14 -08004794 private int getSlotIndex(int subId) {
4795 int slotId = SubscriptionManager.getSlotIndex(subId);
4796 if (!SubscriptionManager.isValidSlotIndex(slotId)) {
4797 return SubscriptionManager.INVALID_SIM_SLOT_INDEX;
4798 }
4799 return slotId;
4800 }
4801
Wink Saville36469e72014-06-11 15:17:00 -07004802 /**
Nathan Harold9042f0b2019-05-21 15:51:27 -07004803 * Returns the data network type for a subId; does not throw SecurityException.
Wink Saville36469e72014-06-11 15:17:00 -07004804 */
4805 @Override
Philip P. Moltmann700a9592019-10-03 11:53:50 -07004806 public int getNetworkTypeForSubscriber(int subId, String callingPackage,
4807 String callingFeatureId) {
Nathan Haroldf096d982020-11-18 17:18:06 -08004808 final int targetSdk = TelephonyPermissions.getTargetSdk(mApp, callingPackage);
Nathan Haroldef60dba2019-05-22 13:55:14 -07004809 if (targetSdk > android.os.Build.VERSION_CODES.Q) {
Philip P. Moltmann700a9592019-10-03 11:53:50 -07004810 return getDataNetworkTypeForSubscriber(subId, callingPackage, callingFeatureId);
Nathan Haroldef60dba2019-05-22 13:55:14 -07004811 } else if (targetSdk == android.os.Build.VERSION_CODES.Q
Nathan Harold9042f0b2019-05-21 15:51:27 -07004812 && !TelephonyPermissions.checkCallingOrSelfReadPhoneStateNoThrow(
Philip P. Moltmann700a9592019-10-03 11:53:50 -07004813 mApp, subId, callingPackage, callingFeatureId,
4814 "getNetworkTypeForSubscriber")) {
Robert Greenwalt36b23af2015-07-06 17:59:14 -07004815 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
4816 }
Robert Greenwalta5dcfcb2015-07-10 09:06:29 -07004817
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004818 final long identity = Binder.clearCallingIdentity();
4819 try {
4820 final Phone phone = getPhone(subId);
4821 if (phone != null) {
4822 return phone.getServiceState().getDataNetworkType();
4823 } else {
4824 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
4825 }
4826 } finally {
4827 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07004828 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07004829 }
4830
4831 /**
4832 * Returns the data network type
4833 */
4834 @Override
Philip P. Moltmann700a9592019-10-03 11:53:50 -07004835 public int getDataNetworkType(String callingPackage, String callingFeatureId) {
4836 return getDataNetworkTypeForSubscriber(getDefaultSubscription(), callingPackage,
4837 callingFeatureId);
Wink Saville36469e72014-06-11 15:17:00 -07004838 }
4839
4840 /**
4841 * Returns the data network type for a subId
4842 */
4843 @Override
Philip P. Moltmann700a9592019-10-03 11:53:50 -07004844 public int getDataNetworkTypeForSubscriber(int subId, String callingPackage,
4845 String callingFeatureId) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08004846 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
Philip P. Moltmann700a9592019-10-03 11:53:50 -07004847 mApp, subId, callingPackage, callingFeatureId,
4848 "getDataNetworkTypeForSubscriber")) {
Robert Greenwalt36b23af2015-07-06 17:59:14 -07004849 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
4850 }
4851
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004852 final long identity = Binder.clearCallingIdentity();
4853 try {
4854 final Phone phone = getPhone(subId);
4855 if (phone != null) {
4856 return phone.getServiceState().getDataNetworkType();
4857 } else {
4858 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
4859 }
4860 } finally {
4861 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07004862 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07004863 }
4864
4865 /**
Wink Saville36469e72014-06-11 15:17:00 -07004866 * Returns the Voice network type for a subId
4867 */
4868 @Override
Philip P. Moltmann700a9592019-10-03 11:53:50 -07004869 public int getVoiceNetworkTypeForSubscriber(int subId, String callingPackage,
4870 String callingFeatureId) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08004871 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
Philip P. Moltmann700a9592019-10-03 11:53:50 -07004872 mApp, subId, callingPackage, callingFeatureId,
4873 "getDataNetworkTypeForSubscriber")) {
Robert Greenwalta5dcfcb2015-07-10 09:06:29 -07004874 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
4875 }
4876
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004877 final long identity = Binder.clearCallingIdentity();
4878 try {
4879 final Phone phone = getPhone(subId);
4880 if (phone != null) {
4881 return phone.getServiceState().getVoiceNetworkType();
4882 } else {
4883 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
4884 }
4885 } finally {
4886 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07004887 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07004888 }
4889
4890 /**
4891 * @return true if a ICC card is present
4892 */
4893 public boolean hasIccCard() {
Wink Saville36469e72014-06-11 15:17:00 -07004894 // FIXME Make changes to pass defaultSimId of type int
Sanket Padawe13bac7b2017-03-20 15:04:47 -07004895 return hasIccCardUsingSlotIndex(mSubscriptionController.getSlotIndex(
4896 getDefaultSubscription()));
Wink Saville36469e72014-06-11 15:17:00 -07004897 }
4898
4899 /**
Sanket Padawe13bac7b2017-03-20 15:04:47 -07004900 * @return true if a ICC card is present for a slotIndex
Wink Saville36469e72014-06-11 15:17:00 -07004901 */
Sanket Padawe356d7632015-06-22 14:03:32 -07004902 @Override
Sanket Padawe13bac7b2017-03-20 15:04:47 -07004903 public boolean hasIccCardUsingSlotIndex(int slotIndex) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004904 final long identity = Binder.clearCallingIdentity();
4905 try {
4906 final Phone phone = PhoneFactory.getPhone(slotIndex);
4907 if (phone != null) {
4908 return phone.getIccCard().hasIccCard();
4909 } else {
4910 return false;
4911 }
4912 } finally {
4913 Binder.restoreCallingIdentity(identity);
Amit Mahajana6fc2a82015-01-06 11:53:51 -08004914 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07004915 }
4916
4917 /**
4918 * Return if the current radio is LTE on CDMA. This
4919 * is a tri-state return value as for a period of time
4920 * the mode may be unknown.
4921 *
Robert Greenwalt36b23af2015-07-06 17:59:14 -07004922 * @param callingPackage the name of the package making the call.
Santos Cordon7d4ddf62013-07-10 11:58:08 -07004923 * @return {@link Phone#LTE_ON_CDMA_UNKNOWN}, {@link Phone#LTE_ON_CDMA_FALSE}
Jake Hambye994d462014-02-03 13:10:13 -08004924 * or {@link Phone#LTE_ON_CDMA_TRUE}
Santos Cordon7d4ddf62013-07-10 11:58:08 -07004925 */
Robert Greenwalt36b23af2015-07-06 17:59:14 -07004926 @Override
Philip P. Moltmann700a9592019-10-03 11:53:50 -07004927 public int getLteOnCdmaMode(String callingPackage, String callingFeatureId) {
4928 return getLteOnCdmaModeForSubscriber(getDefaultSubscription(), callingPackage,
4929 callingFeatureId);
Wink Saville36469e72014-06-11 15:17:00 -07004930 }
4931
Sanket Padawe356d7632015-06-22 14:03:32 -07004932 @Override
Philip P. Moltmann700a9592019-10-03 11:53:50 -07004933 public int getLteOnCdmaModeForSubscriber(int subId, String callingPackage,
4934 String callingFeatureId) {
Sarah Chin790d2922020-01-16 12:17:23 -08004935 try {
4936 enforceReadPrivilegedPermission("getLteOnCdmaModeForSubscriber");
4937 } catch (SecurityException e) {
Robert Greenwalt36b23af2015-07-06 17:59:14 -07004938 return PhoneConstants.LTE_ON_CDMA_UNKNOWN;
4939 }
4940
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004941 final long identity = Binder.clearCallingIdentity();
4942 try {
4943 final Phone phone = getPhone(subId);
4944 if (phone == null) {
4945 return PhoneConstants.LTE_ON_CDMA_UNKNOWN;
4946 } else {
Nathan Harold05ad6332020-07-10 11:54:36 -07004947 return TelephonyProperties.lte_on_cdma_device()
4948 .orElse(PhoneConstants.LTE_ON_CDMA_FALSE);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004949 }
4950 } finally {
4951 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07004952 }
Wink Saville36469e72014-06-11 15:17:00 -07004953 }
4954
Wink Saville36469e72014-06-11 15:17:00 -07004955 /**
4956 * {@hide}
4957 * Returns Default subId, 0 in the case of single standby.
4958 */
Wink Savilleb564aae2014-10-23 10:18:09 -07004959 private int getDefaultSubscription() {
Wink Savilleac1bdfd2014-11-20 23:04:44 -08004960 return mSubscriptionController.getDefaultSubId();
Wink Saville36469e72014-06-11 15:17:00 -07004961 }
4962
Shishir Agrawala9f32182016-04-12 12:00:16 -07004963 private int getSlotForDefaultSubscription() {
4964 return mSubscriptionController.getPhoneId(getDefaultSubscription());
4965 }
4966
Wink Savilleb564aae2014-10-23 10:18:09 -07004967 private int getPreferredVoiceSubscription() {
Wink Savilleac1bdfd2014-11-20 23:04:44 -08004968 return mSubscriptionController.getDefaultVoiceSubId();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07004969 }
Ihab Awadf2177b72013-11-25 13:33:23 -08004970
Pengquan Menge92a50d2018-09-21 15:54:48 -07004971 private boolean isActiveSubscription(int subId) {
4972 return mSubscriptionController.isActiveSubId(subId);
4973 }
4974
Ihab Awadf2177b72013-11-25 13:33:23 -08004975 /**
4976 * @see android.telephony.TelephonyManager.WifiCallingChoices
4977 */
4978 public int getWhenToMakeWifiCalls() {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004979 final long identity = Binder.clearCallingIdentity();
4980 try {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08004981 return Settings.System.getInt(mApp.getContentResolver(),
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004982 Settings.System.WHEN_TO_MAKE_WIFI_CALLS,
4983 getWhenToMakeWifiCallsDefaultPreference());
4984 } finally {
4985 Binder.restoreCallingIdentity(identity);
4986 }
Ihab Awadf2177b72013-11-25 13:33:23 -08004987 }
4988
4989 /**
4990 * @see android.telephony.TelephonyManager.WifiCallingChoices
4991 */
4992 public void setWhenToMakeWifiCalls(int preference) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004993 final long identity = Binder.clearCallingIdentity();
4994 try {
4995 if (DBG) log("setWhenToMakeWifiCallsStr, storing setting = " + preference);
Nazanin Bakhshif782e562018-12-11 15:15:39 -08004996 Settings.System.putInt(mApp.getContentResolver(),
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004997 Settings.System.WHEN_TO_MAKE_WIFI_CALLS, preference);
4998 } finally {
4999 Binder.restoreCallingIdentity(identity);
5000 }
Ihab Awadf9e92732013-12-05 18:02:52 -08005001 }
5002
Sailesh Nepald1e68152013-12-12 19:08:02 -08005003 private static int getWhenToMakeWifiCallsDefaultPreference() {
Santos Cordonda120f42014-08-06 04:44:34 -07005004 // TODO: Use a build property to choose this value.
Evan Charlton9829e882013-12-19 15:30:38 -08005005 return TelephonyManager.WifiCallingChoices.ALWAYS_USE;
Ihab Awadf2177b72013-11-25 13:33:23 -08005006 }
Shishir Agrawal69f68122013-12-16 17:25:49 -08005007
Jordan Liu4c733742019-02-28 12:03:40 -08005008 private Phone getPhoneFromSlotIdOrThrowException(int slotIndex) {
5009 int phoneId = UiccController.getInstance().getPhoneIdFromSlotId(slotIndex);
5010 if (phoneId == -1) {
5011 throw new IllegalArgumentException("Given slot index: " + slotIndex
5012 + " does not correspond to an active phone");
5013 }
5014 return PhoneFactory.getPhone(phoneId);
5015 }
5016
Shishir Agrawal566b7612013-10-28 14:41:00 -07005017 @Override
Derek Tan740e1672017-06-27 14:56:27 -07005018 public IccOpenLogicalChannelResponse iccOpenLogicalChannel(
5019 int subId, String callingPackage, String aid, int p2) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08005020 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
5021 mApp, subId, "iccOpenLogicalChannel");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005022 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
Jordan Liu4c733742019-02-28 12:03:40 -08005023 if (DBG) {
5024 log("iccOpenLogicalChannel: subId=" + subId + " aid=" + aid + " p2=" + p2);
5025 }
5026 return iccOpenLogicalChannelWithPermission(getPhoneFromSubId(subId), callingPackage, aid,
5027 p2);
5028 }
Shishir Agrawal566b7612013-10-28 14:41:00 -07005029
Jordan Liu4c733742019-02-28 12:03:40 -08005030
5031 @Override
5032 public IccOpenLogicalChannelResponse iccOpenLogicalChannelBySlot(
5033 int slotIndex, String callingPackage, String aid, int p2) {
5034 enforceModifyPermission();
5035 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
5036 if (DBG) {
5037 log("iccOpenLogicalChannelBySlot: slot=" + slotIndex + " aid=" + aid + " p2=" + p2);
5038 }
5039 return iccOpenLogicalChannelWithPermission(getPhoneFromSlotIdOrThrowException(slotIndex),
5040 callingPackage, aid, p2);
5041 }
5042
5043 private IccOpenLogicalChannelResponse iccOpenLogicalChannelWithPermission(Phone phone,
5044 String callingPackage, String aid, int p2) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005045 final long identity = Binder.clearCallingIdentity();
5046 try {
5047 if (TextUtils.equals(ISDR_AID, aid)) {
5048 // Only allows LPA to open logical channel to ISD-R.
Nazanin Bakhshif782e562018-12-11 15:15:39 -08005049 ComponentInfo bestComponent = EuiccConnector.findBestComponent(getDefaultPhone()
5050 .getContext().getPackageManager());
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005051 if (bestComponent == null
5052 || !TextUtils.equals(callingPackage, bestComponent.packageName)) {
5053 loge("The calling package is not allowed to access ISD-R.");
5054 throw new SecurityException(
5055 "The calling package is not allowed to access ISD-R.");
5056 }
Derek Tan740e1672017-06-27 14:56:27 -07005057 }
Derek Tan740e1672017-06-27 14:56:27 -07005058
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005059 IccOpenLogicalChannelResponse response = (IccOpenLogicalChannelResponse) sendRequest(
Jordan Liu4c733742019-02-28 12:03:40 -08005060 CMD_OPEN_CHANNEL, new Pair<String, Integer>(aid, p2), phone,
5061 null /* workSource */);
5062 if (DBG) log("iccOpenLogicalChannelWithPermission: " + response);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005063 return response;
5064 } finally {
5065 Binder.restoreCallingIdentity(identity);
5066 }
Shishir Agrawal566b7612013-10-28 14:41:00 -07005067 }
5068
5069 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08005070 public boolean iccCloseLogicalChannel(int subId, int channel) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08005071 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
5072 mApp, subId, "iccCloseLogicalChannel");
Jordan Liu4c733742019-02-28 12:03:40 -08005073 if (DBG) log("iccCloseLogicalChannel: subId=" + subId + " chnl=" + channel);
5074 return iccCloseLogicalChannelWithPermission(getPhoneFromSubId(subId), channel);
5075 }
Shishir Agrawal566b7612013-10-28 14:41:00 -07005076
Jordan Liu4c733742019-02-28 12:03:40 -08005077 @Override
5078 public boolean iccCloseLogicalChannelBySlot(int slotIndex, int channel) {
5079 enforceModifyPermission();
5080 if (DBG) log("iccCloseLogicalChannelBySlot: slotIndex=" + slotIndex + " chnl=" + channel);
5081 return iccCloseLogicalChannelWithPermission(getPhoneFromSlotIdOrThrowException(slotIndex),
5082 channel);
5083 }
5084
5085 private boolean iccCloseLogicalChannelWithPermission(Phone phone, int channel) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005086 final long identity = Binder.clearCallingIdentity();
5087 try {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005088 if (channel < 0) {
5089 return false;
5090 }
Jordan Liu4c733742019-02-28 12:03:40 -08005091 Boolean success = (Boolean) sendRequest(CMD_CLOSE_CHANNEL, channel, phone,
5092 null /* workSource */);
5093 if (DBG) log("iccCloseLogicalChannelWithPermission: " + success);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005094 return success;
5095 } finally {
5096 Binder.restoreCallingIdentity(identity);
Shishir Agrawal566b7612013-10-28 14:41:00 -07005097 }
Shishir Agrawal566b7612013-10-28 14:41:00 -07005098 }
5099
5100 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08005101 public String iccTransmitApduLogicalChannel(int subId, int channel, int cla,
Shishir Agrawal566b7612013-10-28 14:41:00 -07005102 int command, int p1, int p2, int p3, String data) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08005103 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
5104 mApp, subId, "iccTransmitApduLogicalChannel");
Jordan Liu4c733742019-02-28 12:03:40 -08005105 if (DBG) {
5106 log("iccTransmitApduLogicalChannel: subId=" + subId + " chnl=" + channel
5107 + " cla=" + cla + " cmd=" + command + " p1=" + p1 + " p2=" + p2 + " p3="
5108 + p3 + " data=" + data);
5109 }
5110 return iccTransmitApduLogicalChannelWithPermission(getPhoneFromSubId(subId), channel, cla,
5111 command, p1, p2, p3, data);
5112 }
Shishir Agrawal566b7612013-10-28 14:41:00 -07005113
Jordan Liu4c733742019-02-28 12:03:40 -08005114 @Override
5115 public String iccTransmitApduLogicalChannelBySlot(int slotIndex, int channel, int cla,
5116 int command, int p1, int p2, int p3, String data) {
5117 enforceModifyPermission();
5118 if (DBG) {
5119 log("iccTransmitApduLogicalChannelBySlot: slotIndex=" + slotIndex + " chnl=" + channel
5120 + " cla=" + cla + " cmd=" + command + " p1=" + p1 + " p2=" + p2 + " p3="
5121 + p3 + " data=" + data);
5122 }
5123 return iccTransmitApduLogicalChannelWithPermission(
5124 getPhoneFromSlotIdOrThrowException(slotIndex), channel, cla, command, p1, p2, p3,
5125 data);
5126 }
5127
5128 private String iccTransmitApduLogicalChannelWithPermission(Phone phone, int channel, int cla,
5129 int command, int p1, int p2, int p3, String data) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005130 final long identity = Binder.clearCallingIdentity();
5131 try {
Hall Liu4fd771b2019-05-02 09:16:29 -07005132 if (channel <= 0) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005133 return "";
5134 }
Shishir Agrawal566b7612013-10-28 14:41:00 -07005135
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005136 IccIoResult response = (IccIoResult) sendRequest(CMD_TRANSMIT_APDU_LOGICAL_CHANNEL,
Jordan Liu4c733742019-02-28 12:03:40 -08005137 new IccAPDUArgument(channel, cla, command, p1, p2, p3, data), phone,
5138 null /* workSource */);
5139 if (DBG) log("iccTransmitApduLogicalChannelWithPermission: " + response);
Shishir Agrawal566b7612013-10-28 14:41:00 -07005140
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005141 // Append the returned status code to the end of the response payload.
5142 String s = Integer.toHexString(
5143 (response.sw1 << 8) + response.sw2 + 0x10000).substring(1);
5144 if (response.payload != null) {
5145 s = IccUtils.bytesToHexString(response.payload) + s;
5146 }
5147 return s;
5148 } finally {
5149 Binder.restoreCallingIdentity(identity);
Shishir Agrawal5ec14172014-08-05 17:05:45 -07005150 }
Shishir Agrawal566b7612013-10-28 14:41:00 -07005151 }
Jake Hambye994d462014-02-03 13:10:13 -08005152
Evan Charltonc66da362014-05-16 14:06:40 -07005153 @Override
Holly Jiuyu Sun1cc2d552018-01-26 15:51:16 -08005154 public String iccTransmitApduBasicChannel(int subId, String callingPackage, int cla,
5155 int command, int p1, int p2, int p3, String data) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08005156 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
5157 mApp, subId, "iccTransmitApduBasicChannel");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005158 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
Jordan Liu4c733742019-02-28 12:03:40 -08005159 if (DBG) {
5160 log("iccTransmitApduBasicChannel: subId=" + subId + " cla=" + cla + " cmd="
5161 + command + " p1=" + p1 + " p2=" + p2 + " p3=" + p3 + " data=" + data);
5162 }
5163 return iccTransmitApduBasicChannelWithPermission(getPhoneFromSubId(subId), callingPackage,
5164 cla, command, p1, p2, p3, data);
5165 }
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07005166
Jordan Liu4c733742019-02-28 12:03:40 -08005167 @Override
5168 public String iccTransmitApduBasicChannelBySlot(int slotIndex, String callingPackage, int cla,
5169 int command, int p1, int p2, int p3, String data) {
5170 enforceModifyPermission();
5171 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
5172 if (DBG) {
5173 log("iccTransmitApduBasicChannelBySlot: slotIndex=" + slotIndex + " cla=" + cla
5174 + " cmd=" + command + " p1=" + p1 + " p2=" + p2 + " p3=" + p3
5175 + " data=" + data);
5176 }
5177
5178 return iccTransmitApduBasicChannelWithPermission(
5179 getPhoneFromSlotIdOrThrowException(slotIndex), callingPackage, cla, command, p1,
5180 p2, p3, data);
5181 }
5182
5183 // open APDU basic channel assuming the caller has sufficient permissions
5184 private String iccTransmitApduBasicChannelWithPermission(Phone phone, String callingPackage,
5185 int cla, int command, int p1, int p2, int p3, String data) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005186 final long identity = Binder.clearCallingIdentity();
5187 try {
5188 if (command == SELECT_COMMAND && p1 == SELECT_P1 && p2 == SELECT_P2 && p3 == SELECT_P3
5189 && TextUtils.equals(ISDR_AID, data)) {
5190 // Only allows LPA to select ISD-R.
Nazanin Bakhshif782e562018-12-11 15:15:39 -08005191 ComponentInfo bestComponent = EuiccConnector.findBestComponent(getDefaultPhone()
5192 .getContext().getPackageManager());
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005193 if (bestComponent == null
5194 || !TextUtils.equals(callingPackage, bestComponent.packageName)) {
5195 loge("The calling package is not allowed to select ISD-R.");
5196 throw new SecurityException(
5197 "The calling package is not allowed to select ISD-R.");
5198 }
Holly Jiuyu Sun1cc2d552018-01-26 15:51:16 -08005199 }
Holly Jiuyu Sun1cc2d552018-01-26 15:51:16 -08005200
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005201 IccIoResult response = (IccIoResult) sendRequest(CMD_TRANSMIT_APDU_BASIC_CHANNEL,
Jordan Liu4c733742019-02-28 12:03:40 -08005202 new IccAPDUArgument(0, cla, command, p1, p2, p3, data), phone,
5203 null /* workSource */);
5204 if (DBG) log("iccTransmitApduBasicChannelWithPermission: " + response);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07005205
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005206 // Append the returned status code to the end of the response payload.
5207 String s = Integer.toHexString(
5208 (response.sw1 << 8) + response.sw2 + 0x10000).substring(1);
5209 if (response.payload != null) {
5210 s = IccUtils.bytesToHexString(response.payload) + s;
5211 }
5212 return s;
5213 } finally {
5214 Binder.restoreCallingIdentity(identity);
Shishir Agrawal5ec14172014-08-05 17:05:45 -07005215 }
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07005216 }
5217
5218 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08005219 public byte[] iccExchangeSimIO(int subId, int fileID, int command, int p1, int p2, int p3,
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07005220 String filePath) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08005221 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
5222 mApp, subId, "iccExchangeSimIO");
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07005223
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005224 final long identity = Binder.clearCallingIdentity();
5225 try {
5226 if (DBG) {
5227 log("Exchange SIM_IO " + subId + ":" + fileID + ":" + command + " "
5228 + p1 + " " + p2 + " " + p3 + ":" + filePath);
5229 }
5230
5231 IccIoResult response =
5232 (IccIoResult) sendRequest(CMD_EXCHANGE_SIM_IO,
5233 new IccAPDUArgument(-1, fileID, command, p1, p2, p3, filePath),
5234 subId);
5235
5236 if (DBG) {
5237 log("Exchange SIM_IO [R]" + response);
5238 }
5239
5240 byte[] result = null;
5241 int length = 2;
5242 if (response.payload != null) {
5243 length = 2 + response.payload.length;
5244 result = new byte[length];
5245 System.arraycopy(response.payload, 0, result, 0, response.payload.length);
5246 } else {
5247 result = new byte[length];
5248 }
5249
5250 result[length - 1] = (byte) response.sw2;
5251 result[length - 2] = (byte) response.sw1;
5252 return result;
5253 } finally {
5254 Binder.restoreCallingIdentity(identity);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07005255 }
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07005256 }
5257
Nathan Haroldb3014052017-01-25 15:57:32 -08005258 /**
5259 * Get the forbidden PLMN List from the given app type (ex APPTYPE_USIM)
5260 * on a particular subscription
5261 */
Philip P. Moltmann700a9592019-10-03 11:53:50 -07005262 public String[] getForbiddenPlmns(int subId, int appType, String callingPackage,
5263 String callingFeatureId) {
sqianb6e41952018-03-12 14:54:01 -07005264 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
Philip P. Moltmann700a9592019-10-03 11:53:50 -07005265 mApp, subId, callingPackage, callingFeatureId, "getForbiddenPlmns")) {
sqianb6e41952018-03-12 14:54:01 -07005266 return null;
5267 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005268
5269 final long identity = Binder.clearCallingIdentity();
5270 try {
5271 if (appType != TelephonyManager.APPTYPE_USIM
5272 && appType != TelephonyManager.APPTYPE_SIM) {
5273 loge("getForbiddenPlmnList(): App Type must be USIM or SIM");
5274 return null;
5275 }
5276 Object response = sendRequest(
5277 CMD_GET_FORBIDDEN_PLMNS, new Integer(appType), subId);
5278 if (response instanceof String[]) {
5279 return (String[]) response;
5280 }
yincheng zhao2737e882019-09-06 17:06:54 -07005281 // Response is an Exception of some kind
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005282 // which is signalled to the user as a NULL retval
Nathan Haroldb3014052017-01-25 15:57:32 -08005283 return null;
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005284 } finally {
5285 Binder.restoreCallingIdentity(identity);
Nathan Haroldb3014052017-01-25 15:57:32 -08005286 }
Nathan Haroldb3014052017-01-25 15:57:32 -08005287 }
5288
yincheng zhao2737e882019-09-06 17:06:54 -07005289 /**
5290 * Set the forbidden PLMN list from the given app type (ex APPTYPE_USIM) on a particular
5291 * subscription.
5292 *
5293 * @param subId the id of the subscription.
5294 * @param appType the uicc app type, must be USIM or SIM.
5295 * @param fplmns the Forbiden plmns list that needed to be written to the SIM.
5296 * @param callingPackage the op Package name.
Philip P. Moltmann700a9592019-10-03 11:53:50 -07005297 * @param callingFeatureId the feature in the package.
yincheng zhao2737e882019-09-06 17:06:54 -07005298 * @return number of fplmns that is successfully written to the SIM.
5299 */
Philip P. Moltmann700a9592019-10-03 11:53:50 -07005300 public int setForbiddenPlmns(int subId, int appType, List<String> fplmns, String callingPackage,
5301 String callingFeatureId) {
5302 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(mApp, subId, callingPackage,
5303 callingFeatureId, "setForbiddenPlmns")) {
yincheng zhao2737e882019-09-06 17:06:54 -07005304 if (DBG) logv("no permissions for setForbiddenplmns");
5305 throw new IllegalStateException("No Permissions for setForbiddenPlmns");
5306 }
5307 if (appType != TelephonyManager.APPTYPE_USIM && appType != TelephonyManager.APPTYPE_SIM) {
5308 loge("setForbiddenPlmnList(): App Type must be USIM or SIM");
5309 throw new IllegalArgumentException("Invalid appType: App Type must be USIM or SIM");
5310 }
5311 if (fplmns == null) {
5312 throw new IllegalArgumentException("Fplmn List provided is null");
5313 }
5314 for (String fplmn : fplmns) {
5315 if (!CellIdentity.isValidPlmn(fplmn)) {
5316 throw new IllegalArgumentException("Invalid fplmn provided: " + fplmn);
5317 }
5318 }
5319 final long identity = Binder.clearCallingIdentity();
5320 try {
5321 Object response = sendRequest(
5322 CMD_SET_FORBIDDEN_PLMNS,
5323 new Pair<Integer, List<String>>(new Integer(appType), fplmns),
5324 subId);
5325 return (int) response;
5326 } finally {
5327 Binder.restoreCallingIdentity(identity);
5328 }
5329 }
5330
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07005331 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08005332 public String sendEnvelopeWithStatus(int subId, String content) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08005333 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
5334 mApp, subId, "sendEnvelopeWithStatus");
Evan Charltonc66da362014-05-16 14:06:40 -07005335
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005336 final long identity = Binder.clearCallingIdentity();
5337 try {
5338 IccIoResult response = (IccIoResult) sendRequest(CMD_SEND_ENVELOPE, content, subId);
5339 if (response.payload == null) {
5340 return "";
5341 }
Evan Charltonc66da362014-05-16 14:06:40 -07005342
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005343 // Append the returned status code to the end of the response payload.
5344 String s = Integer.toHexString(
5345 (response.sw1 << 8) + response.sw2 + 0x10000).substring(1);
5346 s = IccUtils.bytesToHexString(response.payload) + s;
5347 return s;
5348 } finally {
5349 Binder.restoreCallingIdentity(identity);
5350 }
Evan Charltonc66da362014-05-16 14:06:40 -07005351 }
5352
Jake Hambye994d462014-02-03 13:10:13 -08005353 /**
5354 * Read one of the NV items defined in {@link com.android.internal.telephony.RadioNVItems}
5355 * and {@code ril_nv_items.h}. Used for device configuration by some CDMA operators.
5356 *
5357 * @param itemID the ID of the item to read
5358 * @return the NV item as a String, or null on error.
5359 */
5360 @Override
5361 public String nvReadItem(int itemID) {
vagdeviaf9a5b92018-08-15 16:01:53 -07005362 WorkSource workSource = getWorkSource(Binder.getCallingUid());
Jeff Davidson7e17e312018-02-13 18:17:36 -08005363 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
5364 mApp, getDefaultSubscription(), "nvReadItem");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005365
5366 final long identity = Binder.clearCallingIdentity();
5367 try {
5368 if (DBG) log("nvReadItem: item " + itemID);
vagdeviaf9a5b92018-08-15 16:01:53 -07005369 String value = (String) sendRequest(CMD_NV_READ_ITEM, itemID, workSource);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005370 if (DBG) log("nvReadItem: item " + itemID + " is \"" + value + '"');
5371 return value;
5372 } finally {
5373 Binder.restoreCallingIdentity(identity);
5374 }
Jake Hambye994d462014-02-03 13:10:13 -08005375 }
5376
5377 /**
5378 * Write one of the NV items defined in {@link com.android.internal.telephony.RadioNVItems}
5379 * and {@code ril_nv_items.h}. Used for device configuration by some CDMA operators.
5380 *
5381 * @param itemID the ID of the item to read
5382 * @param itemValue the value to write, as a String
5383 * @return true on success; false on any failure
5384 */
5385 @Override
5386 public boolean nvWriteItem(int itemID, String itemValue) {
vagdeviaf9a5b92018-08-15 16:01:53 -07005387 WorkSource workSource = getWorkSource(Binder.getCallingUid());
Jeff Davidson7e17e312018-02-13 18:17:36 -08005388 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
5389 mApp, getDefaultSubscription(), "nvWriteItem");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005390
5391 final long identity = Binder.clearCallingIdentity();
5392 try {
5393 if (DBG) log("nvWriteItem: item " + itemID + " value \"" + itemValue + '"');
5394 Boolean success = (Boolean) sendRequest(CMD_NV_WRITE_ITEM,
vagdeviaf9a5b92018-08-15 16:01:53 -07005395 new Pair<Integer, String>(itemID, itemValue), workSource);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005396 if (DBG) log("nvWriteItem: item " + itemID + ' ' + (success ? "ok" : "fail"));
5397 return success;
5398 } finally {
5399 Binder.restoreCallingIdentity(identity);
5400 }
Jake Hambye994d462014-02-03 13:10:13 -08005401 }
5402
5403 /**
5404 * Update the CDMA Preferred Roaming List (PRL) in the radio NV storage.
5405 * Used for device configuration by some CDMA operators.
5406 *
5407 * @param preferredRoamingList byte array containing the new PRL
5408 * @return true on success; false on any failure
5409 */
5410 @Override
5411 public boolean nvWriteCdmaPrl(byte[] preferredRoamingList) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08005412 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
5413 mApp, getDefaultSubscription(), "nvWriteCdmaPrl");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005414
5415 final long identity = Binder.clearCallingIdentity();
5416 try {
5417 if (DBG) log("nvWriteCdmaPrl: value: " + HexDump.toHexString(preferredRoamingList));
5418 Boolean success = (Boolean) sendRequest(CMD_NV_WRITE_CDMA_PRL, preferredRoamingList);
5419 if (DBG) log("nvWriteCdmaPrl: " + (success ? "ok" : "fail"));
5420 return success;
5421 } finally {
5422 Binder.restoreCallingIdentity(identity);
5423 }
Jake Hambye994d462014-02-03 13:10:13 -08005424 }
5425
5426 /**
chen xu6dac5ab2018-10-26 17:39:23 -07005427 * Rollback modem configurations to factory default except some config which are in whitelist.
Jake Hambye994d462014-02-03 13:10:13 -08005428 * Used for device configuration by some CDMA operators.
5429 *
chen xu6dac5ab2018-10-26 17:39:23 -07005430 * @param slotIndex - device slot.
5431 *
Jake Hambye994d462014-02-03 13:10:13 -08005432 * @return true on success; false on any failure
5433 */
5434 @Override
chen xu6dac5ab2018-10-26 17:39:23 -07005435 public boolean resetModemConfig(int slotIndex) {
5436 Phone phone = PhoneFactory.getPhone(slotIndex);
5437 if (phone != null) {
5438 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
5439 mApp, phone.getSubId(), "resetModemConfig");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005440
chen xu6dac5ab2018-10-26 17:39:23 -07005441 final long identity = Binder.clearCallingIdentity();
5442 try {
5443 Boolean success = (Boolean) sendRequest(CMD_RESET_MODEM_CONFIG, null);
5444 if (DBG) log("resetModemConfig:" + ' ' + (success ? "ok" : "fail"));
5445 return success;
5446 } finally {
5447 Binder.restoreCallingIdentity(identity);
5448 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005449 }
chen xu6dac5ab2018-10-26 17:39:23 -07005450 return false;
5451 }
5452
5453 /**
5454 * Generate a radio modem reset. Used for device configuration by some CDMA operators.
5455 *
5456 * @param slotIndex - device slot.
5457 *
5458 * @return true on success; false on any failure
5459 */
5460 @Override
5461 public boolean rebootModem(int slotIndex) {
5462 Phone phone = PhoneFactory.getPhone(slotIndex);
5463 if (phone != null) {
5464 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
5465 mApp, phone.getSubId(), "rebootModem");
5466
5467 final long identity = Binder.clearCallingIdentity();
5468 try {
5469 Boolean success = (Boolean) sendRequest(CMD_MODEM_REBOOT, null);
5470 if (DBG) log("rebootModem:" + ' ' + (success ? "ok" : "fail"));
5471 return success;
5472 } finally {
5473 Binder.restoreCallingIdentity(identity);
5474 }
5475 }
5476 return false;
Jake Hambye994d462014-02-03 13:10:13 -08005477 }
Jake Hamby7c27be32014-03-03 13:25:59 -08005478
Philip P. Moltmann700a9592019-10-03 11:53:50 -07005479 public String[] getPcscfAddress(String apnType, String callingPackage,
5480 String callingFeatureId) {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08005481 final Phone defaultPhone = getDefaultPhone();
Philip P. Moltmann700a9592019-10-03 11:53:50 -07005482 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(mApp, defaultPhone.getSubId(),
5483 callingPackage, callingFeatureId, "getPcscfAddress")) {
Svet Ganovb320e182015-04-16 12:30:10 -07005484 return new String[0];
5485 }
5486
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005487 final long identity = Binder.clearCallingIdentity();
5488 try {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08005489 return defaultPhone.getPcscfAddress(apnType);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005490 } finally {
5491 Binder.restoreCallingIdentity(identity);
5492 }
Wink Saville36469e72014-06-11 15:17:00 -07005493 }
5494
Brad Ebinger51f743a2017-01-23 13:50:20 -08005495 /**
Grace Jiaaa2eb6b2020-01-09 16:26:08 -08005496 * Toggle IMS disable and enable for the framework to reset it. See {@link #enableIms(int)} and
5497 * {@link #disableIms(int)}.
5498 * @param slotIndex device slot.
5499 */
5500 public void resetIms(int slotIndex) {
5501 enforceModifyPermission();
5502
5503 final long identity = Binder.clearCallingIdentity();
5504 try {
5505 if (mImsResolver == null) {
5506 // may happen if the does not support IMS.
5507 return;
5508 }
5509 mImsResolver.disableIms(slotIndex);
5510 mImsResolver.enableIms(slotIndex);
5511 } finally {
5512 Binder.restoreCallingIdentity(identity);
5513 }
5514 }
5515
5516 /**
Brad Ebinger22bc3e42018-01-16 09:39:35 -08005517 * Enables IMS for the framework. This will trigger IMS registration and ImsFeature capability
5518 * status updates, if not already enabled.
Brad Ebinger51f743a2017-01-23 13:50:20 -08005519 */
Brad Ebinger22bc3e42018-01-16 09:39:35 -08005520 public void enableIms(int slotId) {
Brad Ebinger51f743a2017-01-23 13:50:20 -08005521 enforceModifyPermission();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005522
5523 final long identity = Binder.clearCallingIdentity();
5524 try {
Brad Ebinger24c29992019-12-05 13:03:21 -08005525 if (mImsResolver == null) {
Brad Ebinger9c0eb502019-01-23 15:06:19 -08005526 // may happen if the device does not support IMS.
5527 return;
5528 }
Brad Ebinger24c29992019-12-05 13:03:21 -08005529 mImsResolver.enableIms(slotId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005530 } finally {
5531 Binder.restoreCallingIdentity(identity);
5532 }
Brad Ebinger34bef922017-11-09 10:27:08 -08005533 }
5534
5535 /**
Brad Ebinger22bc3e42018-01-16 09:39:35 -08005536 * Disables IMS for the framework. This will trigger IMS de-registration and trigger ImsFeature
5537 * status updates to disabled.
Brad Ebinger34bef922017-11-09 10:27:08 -08005538 */
Brad Ebinger22bc3e42018-01-16 09:39:35 -08005539 public void disableIms(int slotId) {
5540 enforceModifyPermission();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005541
5542 final long identity = Binder.clearCallingIdentity();
5543 try {
Brad Ebinger24c29992019-12-05 13:03:21 -08005544 if (mImsResolver == null) {
Brad Ebinger9c0eb502019-01-23 15:06:19 -08005545 // may happen if the device does not support IMS.
5546 return;
5547 }
Brad Ebinger24c29992019-12-05 13:03:21 -08005548 mImsResolver.disableIms(slotId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005549 } finally {
5550 Binder.restoreCallingIdentity(identity);
5551 }
Brad Ebinger22bc3e42018-01-16 09:39:35 -08005552 }
5553
5554 /**
Brad Ebinger67b3e042020-09-11 12:45:11 -07005555 * Registers for updates to the MmTelFeature connection through the IImsServiceFeatureCallback
5556 * callback.
Brad Ebinger22bc3e42018-01-16 09:39:35 -08005557 */
Brad Ebinger67b3e042020-09-11 12:45:11 -07005558 @Override
Brad Ebingerf6aca002020-10-01 13:51:05 -07005559 public void registerMmTelFeatureCallback(int slotId, IImsServiceFeatureCallback callback) {
Brad Ebinger34bef922017-11-09 10:27:08 -08005560 enforceModifyPermission();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005561
5562 final long identity = Binder.clearCallingIdentity();
5563 try {
Brad Ebinger24c29992019-12-05 13:03:21 -08005564 if (mImsResolver == null) {
Brad Ebinger67b3e042020-09-11 12:45:11 -07005565 throw new ServiceSpecificException(ImsException.CODE_ERROR_UNSUPPORTED_OPERATION,
5566 "Device does not support IMS");
Brad Ebinger9c0eb502019-01-23 15:06:19 -08005567 }
Brad Ebingerf6aca002020-10-01 13:51:05 -07005568 mImsResolver.listenForFeature(slotId, ImsFeature.FEATURE_MMTEL, callback);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005569 } finally {
5570 Binder.restoreCallingIdentity(identity);
5571 }
Brad Ebinger34bef922017-11-09 10:27:08 -08005572 }
Brad Ebinger5f64b052017-12-14 14:26:15 -08005573 /**
Brad Ebinger075ff3a2020-05-18 17:52:58 -07005574 * Unregister a previously registered IImsServiceFeatureCallback associated with an ImsFeature.
5575 */
Brad Ebinger67b3e042020-09-11 12:45:11 -07005576 @Override
5577 public void unregisterImsFeatureCallback(IImsServiceFeatureCallback callback) {
Brad Ebinger075ff3a2020-05-18 17:52:58 -07005578 enforceModifyPermission();
5579
5580 final long identity = Binder.clearCallingIdentity();
5581 try {
5582 if (mImsResolver == null) return;
Brad Ebinger67b3e042020-09-11 12:45:11 -07005583 mImsResolver.unregisterImsFeatureCallback(callback);
Brad Ebinger075ff3a2020-05-18 17:52:58 -07005584 } finally {
5585 Binder.restoreCallingIdentity(identity);
5586 }
5587 }
5588
5589 /**
Brad Ebinger5f64b052017-12-14 14:26:15 -08005590 * Returns the {@link IImsRegistration} structure associated with the slotId and feature
Brad Ebinger9c0eb502019-01-23 15:06:19 -08005591 * specified or null if IMS is not supported on the slot specified.
Brad Ebinger5f64b052017-12-14 14:26:15 -08005592 */
5593 public IImsRegistration getImsRegistration(int slotId, int feature) throws RemoteException {
5594 enforceModifyPermission();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005595
5596 final long identity = Binder.clearCallingIdentity();
5597 try {
Brad Ebinger24c29992019-12-05 13:03:21 -08005598 if (mImsResolver == null) {
Brad Ebinger9c0eb502019-01-23 15:06:19 -08005599 // may happen if the device does not support IMS.
5600 return null;
5601 }
Brad Ebinger24c29992019-12-05 13:03:21 -08005602 return mImsResolver.getImsRegistration(slotId, feature);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005603 } finally {
5604 Binder.restoreCallingIdentity(identity);
5605 }
Brad Ebinger5f64b052017-12-14 14:26:15 -08005606 }
5607
Brad Ebinger22bc3e42018-01-16 09:39:35 -08005608 /**
5609 * Returns the {@link IImsConfig} structure associated with the slotId and feature
Brad Ebinger9c0eb502019-01-23 15:06:19 -08005610 * specified or null if IMS is not supported on the slot specified.
Brad Ebinger22bc3e42018-01-16 09:39:35 -08005611 */
5612 public IImsConfig getImsConfig(int slotId, int feature) throws RemoteException {
5613 enforceModifyPermission();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005614
5615 final long identity = Binder.clearCallingIdentity();
5616 try {
Brad Ebinger24c29992019-12-05 13:03:21 -08005617 if (mImsResolver == null) {
Brad Ebinger9c0eb502019-01-23 15:06:19 -08005618 // may happen if the device does not support IMS.
5619 return null;
5620 }
Brad Ebinger24c29992019-12-05 13:03:21 -08005621 return mImsResolver.getImsConfig(slotId, feature);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005622 } finally {
5623 Binder.restoreCallingIdentity(identity);
5624 }
Brad Ebinger22bc3e42018-01-16 09:39:35 -08005625 }
5626
Brad Ebinger884c07b2018-02-15 16:17:40 -08005627 /**
Brad Ebingerdac2f002018-04-03 15:17:52 -07005628 * Sets the ImsService Package Name that Telephony will bind to.
5629 *
Brad Ebinger24c29992019-12-05 13:03:21 -08005630 * @param slotIndex the slot ID that the ImsService should bind for.
5631 * @param isCarrierService true if the ImsService is the carrier override, false if the
Brad Ebingerdac2f002018-04-03 15:17:52 -07005632 * ImsService is the device default ImsService.
Brad Ebinger24c29992019-12-05 13:03:21 -08005633 * @param featureTypes An integer array of feature types associated with a packageName.
5634 * @param packageName The name of the package that the current configuration will be replaced
5635 * with.
Brad Ebingerdac2f002018-04-03 15:17:52 -07005636 * @return true if setting the ImsService to bind to succeeded, false if it did not.
Brad Ebingerdac2f002018-04-03 15:17:52 -07005637 */
Brad Ebinger24c29992019-12-05 13:03:21 -08005638 public boolean setBoundImsServiceOverride(int slotIndex, boolean isCarrierService,
5639 int[] featureTypes, String packageName) {
5640 int[] subIds = SubscriptionManager.getSubId(slotIndex);
5641 TelephonyPermissions.enforceShellOnly(Binder.getCallingUid(), "setBoundImsServiceOverride");
Brad Ebingerde696de2018-04-06 09:56:40 -07005642 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(mApp,
5643 (subIds != null ? subIds[0] : SubscriptionManager.INVALID_SUBSCRIPTION_ID),
Brad Ebinger24c29992019-12-05 13:03:21 -08005644 "setBoundImsServiceOverride");
Brad Ebingerde696de2018-04-06 09:56:40 -07005645
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005646 final long identity = Binder.clearCallingIdentity();
5647 try {
Brad Ebinger24c29992019-12-05 13:03:21 -08005648 if (mImsResolver == null) {
Brad Ebinger9c0eb502019-01-23 15:06:19 -08005649 // may happen if the device does not support IMS.
5650 return false;
5651 }
Brad Ebinger24c29992019-12-05 13:03:21 -08005652 Map<Integer, String> featureConfig = new HashMap<>();
5653 for (int featureType : featureTypes) {
5654 featureConfig.put(featureType, packageName);
5655 }
5656 return mImsResolver.overrideImsServiceConfiguration(slotIndex, isCarrierService,
5657 featureConfig);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005658 } finally {
5659 Binder.restoreCallingIdentity(identity);
5660 }
Brad Ebingerdac2f002018-04-03 15:17:52 -07005661 }
5662
5663 /**
Brad Ebinger999d3302020-11-25 14:31:39 -08005664 * Clears any carrier ImsService overrides for the slot index specified that were previously
5665 * set with {@link #setBoundImsServiceOverride(int, boolean, int[], String)}.
5666 *
5667 * This should only be used for testing.
5668 *
5669 * @param slotIndex the slot ID that the ImsService should bind for.
5670 * @return true if clearing the carrier ImsService override succeeded or false if it did not.
5671 */
5672 @Override
5673 public boolean clearCarrierImsServiceOverride(int slotIndex) {
5674 int[] subIds = SubscriptionManager.getSubId(slotIndex);
5675 TelephonyPermissions.enforceShellOnly(Binder.getCallingUid(),
5676 "clearCarrierImsServiceOverride");
5677 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(mApp,
5678 (subIds != null ? subIds[0] : SubscriptionManager.INVALID_SUBSCRIPTION_ID),
5679 "clearCarrierImsServiceOverride");
5680
5681 final long identity = Binder.clearCallingIdentity();
5682 try {
5683 if (mImsResolver == null) {
5684 // may happen if the device does not support IMS.
5685 return false;
5686 }
5687 return mImsResolver.clearCarrierImsServiceConfiguration(slotIndex);
5688 } finally {
5689 Binder.restoreCallingIdentity(identity);
5690 }
5691 }
5692
5693 /**
Brad Ebinger24c29992019-12-05 13:03:21 -08005694 * Return the package name of the currently bound ImsService.
Brad Ebingerdac2f002018-04-03 15:17:52 -07005695 *
5696 * @param slotId The slot that the ImsService is associated with.
5697 * @param isCarrierImsService true, if the ImsService is a carrier override, false if it is
5698 * the device default.
Brad Ebinger24c29992019-12-05 13:03:21 -08005699 * @param featureType The feature associated with the queried configuration.
Brad Ebingerdac2f002018-04-03 15:17:52 -07005700 * @return the package name of the ImsService configuration.
5701 */
Brad Ebinger24c29992019-12-05 13:03:21 -08005702 public String getBoundImsServicePackage(int slotId, boolean isCarrierImsService,
5703 @ImsFeature.FeatureType int featureType) {
Brad Ebingerde696de2018-04-06 09:56:40 -07005704 int[] subIds = SubscriptionManager.getSubId(slotId);
Brad Ebinger24c29992019-12-05 13:03:21 -08005705 TelephonyPermissions
5706 .enforeceCallingOrSelfReadPrivilegedPhoneStatePermissionOrCarrierPrivilege(
5707 mApp, (subIds != null ? subIds[0] : SubscriptionManager.INVALID_SUBSCRIPTION_ID),
5708 "getBoundImsServicePackage");
Brad Ebingerde696de2018-04-06 09:56:40 -07005709
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005710 final long identity = Binder.clearCallingIdentity();
5711 try {
Brad Ebinger24c29992019-12-05 13:03:21 -08005712 if (mImsResolver == null) {
Brad Ebinger9c0eb502019-01-23 15:06:19 -08005713 // may happen if the device does not support IMS.
5714 return "";
5715 }
Brad Ebingera80c3312019-12-02 10:59:39 -08005716 // TODO: change API to query RCS separately.
Brad Ebinger24c29992019-12-05 13:03:21 -08005717 return mImsResolver.getImsServiceConfiguration(slotId, isCarrierImsService,
5718 featureType);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005719 } finally {
5720 Binder.restoreCallingIdentity(identity);
5721 }
Brad Ebingerdac2f002018-04-03 15:17:52 -07005722 }
5723
Brad Ebingerbc7dd582019-10-17 17:03:22 -07005724 /**
5725 * Get the MmTelFeature state associated with the requested subscription id.
5726 * @param subId The subscription that the MmTelFeature is associated with.
5727 * @param callback A callback with an integer containing the
5728 * {@link android.telephony.ims.feature.ImsFeature.ImsState} associated with the MmTelFeature.
5729 */
5730 @Override
5731 public void getImsMmTelFeatureState(int subId, IIntegerConsumer callback) {
5732 enforceReadPrivilegedPermission("getImsMmTelFeatureState");
5733 if (!ImsManager.isImsSupportedOnDevice(mApp)) {
5734 throw new ServiceSpecificException(ImsException.CODE_ERROR_UNSUPPORTED_OPERATION,
5735 "IMS not available on device.");
5736 }
5737 final long token = Binder.clearCallingIdentity();
5738 try {
5739 int slotId = getSlotIndex(subId);
5740 if (slotId <= SubscriptionManager.INVALID_SIM_SLOT_INDEX) {
5741 Log.w(LOG_TAG, "getImsMmTelFeatureState: called with an inactive subscription '"
5742 + subId + "'");
5743 throw new ServiceSpecificException(ImsException.CODE_ERROR_INVALID_SUBSCRIPTION);
5744 }
5745 ImsManager.getInstance(mApp, slotId).getImsServiceState(anInteger -> {
5746 try {
5747 callback.accept(anInteger == null ? ImsFeature.STATE_UNAVAILABLE : anInteger);
5748 } catch (RemoteException e) {
5749 Log.w(LOG_TAG, "getImsMmTelFeatureState: remote caller is no longer running. "
5750 + "Ignore");
5751 }
5752 });
5753 } finally {
5754 Binder.restoreCallingIdentity(token);
5755 }
5756 }
5757
Daniel Brightbb5840b2021-01-12 15:48:18 -08005758 /**
5759 * Sets the ims registration state on all valid {@link Phone}s.
5760 */
5761 public void setImsRegistrationState(final boolean registered) {
Wink Saville36469e72014-06-11 15:17:00 -07005762 enforceModifyPermission();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005763
5764 final long identity = Binder.clearCallingIdentity();
5765 try {
Daniel Brightbb5840b2021-01-12 15:48:18 -08005766 // NOTE: Before S, this method only set the default phone.
5767 for (final Phone phone : PhoneFactory.getPhones()) {
5768 if (SubscriptionManager.isValidSubscriptionId(phone.getSubId())) {
5769 phone.setImsRegistrationState(registered);
5770 }
5771 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005772 } finally {
5773 Binder.restoreCallingIdentity(identity);
5774 }
Wink Saville36469e72014-06-11 15:17:00 -07005775 }
5776
5777 /**
Stuart Scott54788802015-03-30 13:18:01 -07005778 * Set the network selection mode to automatic.
5779 *
5780 */
5781 @Override
5782 public void setNetworkSelectionModeAutomatic(int subId) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08005783 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
5784 mApp, subId, "setNetworkSelectionModeAutomatic");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005785
5786 final long identity = Binder.clearCallingIdentity();
5787 try {
shilufc958392020-01-20 11:36:01 -08005788 if (!isActiveSubscription(subId)) {
5789 return;
5790 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005791 if (DBG) log("setNetworkSelectionModeAutomatic: subId " + subId);
Rambo Wang0f050d82021-02-12 11:43:36 -08005792 sendRequest(CMD_SET_NETWORK_SELECTION_MODE_AUTOMATIC, null, subId,
5793 SET_NETWORK_SELECTION_MODE_AUTOMATIC_TIMEOUT_MS);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005794 } finally {
5795 Binder.restoreCallingIdentity(identity);
5796 }
Stuart Scott54788802015-03-30 13:18:01 -07005797 }
5798
Jack Yud10cdd42020-09-28 20:28:01 -07005799 /**
Pengquan Mengea84e042018-09-20 14:57:26 -07005800 * Ask the radio to connect to the input network and change selection mode to manual.
5801 *
5802 * @param subId the id of the subscription.
5803 * @param operatorInfo the operator information, included the PLMN, long name and short name of
5804 * the operator to attach to.
5805 * @param persistSelection whether the selection will persist until reboot. If true, only allows
5806 * attaching to the selected PLMN until reboot; otherwise, attach to the chosen PLMN and resume
5807 * normal network selection next time.
5808 * @return {@code true} on success; {@code true} on any failure.
Shishir Agrawal302c8692015-06-19 13:49:39 -07005809 */
5810 @Override
Pengquan Mengea84e042018-09-20 14:57:26 -07005811 public boolean setNetworkSelectionModeManual(
5812 int subId, OperatorInfo operatorInfo, boolean persistSelection) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08005813 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
5814 mApp, subId, "setNetworkSelectionModeManual");
Pengquan Menge92a50d2018-09-21 15:54:48 -07005815
5816 if (!isActiveSubscription(subId)) {
5817 return false;
5818 }
5819
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005820 final long identity = Binder.clearCallingIdentity();
5821 try {
Pengquan Mengea84e042018-09-20 14:57:26 -07005822 ManualNetworkSelectionArgument arg = new ManualNetworkSelectionArgument(operatorInfo,
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005823 persistSelection);
Pengquan Mengea84e042018-09-20 14:57:26 -07005824 if (DBG) {
5825 log("setNetworkSelectionModeManual: subId: " + subId
5826 + " operator: " + operatorInfo);
5827 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005828 return (Boolean) sendRequest(CMD_SET_NETWORK_SELECTION_MODE_MANUAL, arg, subId);
5829 } finally {
5830 Binder.restoreCallingIdentity(identity);
5831 }
Shishir Agrawal302c8692015-06-19 13:49:39 -07005832 }
shilu84f6e8b2019-12-19 13:58:01 -08005833 /**
5834 * Get the manual network selection
5835 *
5836 * @param subId the id of the subscription.
5837 *
5838 * @return the previously saved user selected PLMN
5839 */
5840 @Override
5841 public String getManualNetworkSelectionPlmn(int subId) {
5842 TelephonyPermissions
5843 .enforeceCallingOrSelfReadPrecisePhoneStatePermissionOrCarrierPrivilege(
5844 mApp, subId, "getManualNetworkSelectionPlmn");
5845
5846 final long identity = Binder.clearCallingIdentity();
5847 try {
5848 if (!isActiveSubscription(subId)) {
shilufa1c2592020-03-10 10:59:43 -07005849 throw new IllegalArgumentException("Invalid Subscription Id: " + subId);
shilu84f6e8b2019-12-19 13:58:01 -08005850 }
5851
5852 final Phone phone = getPhone(subId);
5853 if (phone == null) {
shilufa1c2592020-03-10 10:59:43 -07005854 throw new IllegalArgumentException("Invalid Subscription Id: " + subId);
shilu84f6e8b2019-12-19 13:58:01 -08005855 }
5856 OperatorInfo networkSelection = phone.getSavedNetworkSelection();
5857 return TextUtils.isEmpty(networkSelection.getOperatorNumeric())
5858 ? phone.getManualNetworkSelectionPlmn() : networkSelection.getOperatorNumeric();
5859 } finally {
5860 Binder.restoreCallingIdentity(identity);
5861 }
5862 }
Shishir Agrawal302c8692015-06-19 13:49:39 -07005863
5864 /**
5865 * Scans for available networks.
5866 */
5867 @Override
Philip P. Moltmann3a2772a2019-10-04 08:15:00 -07005868 public CellNetworkScanResult getCellNetworkScanResults(int subId, String callingPackage,
5869 String callingFeatureId) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08005870 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
5871 mApp, subId, "getCellNetworkScanResults");
Hall Liuf19c44f2018-11-27 14:38:17 -08005872 LocationAccessPolicy.LocationPermissionResult locationResult =
5873 LocationAccessPolicy.checkLocationPermission(mApp,
5874 new LocationAccessPolicy.LocationPermissionQuery.Builder()
5875 .setCallingPackage(callingPackage)
Philip P. Moltmann3a2772a2019-10-04 08:15:00 -07005876 .setCallingFeatureId(callingFeatureId)
Hall Liuf19c44f2018-11-27 14:38:17 -08005877 .setCallingPid(Binder.getCallingPid())
5878 .setCallingUid(Binder.getCallingUid())
5879 .setMethod("getCellNetworkScanResults")
5880 .setMinSdkVersionForFine(Build.VERSION_CODES.Q)
Hall Liuc4a3e422020-05-26 17:18:03 -07005881 .setMinSdkVersionForCoarse(Build.VERSION_CODES.Q)
5882 .setMinSdkVersionForEnforcement(Build.VERSION_CODES.Q)
Hall Liuf19c44f2018-11-27 14:38:17 -08005883 .build());
5884 switch (locationResult) {
5885 case DENIED_HARD:
5886 throw new SecurityException("Not allowed to access scan results -- location");
5887 case DENIED_SOFT:
5888 return null;
5889 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005890
Pengquan Menga1bb6272018-09-06 09:59:22 -07005891 long identity = Binder.clearCallingIdentity();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005892 try {
5893 if (DBG) log("getCellNetworkScanResults: subId " + subId);
Pengquan Menga1bb6272018-09-06 09:59:22 -07005894 return (CellNetworkScanResult) sendRequest(
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005895 CMD_PERFORM_NETWORK_SCAN, null, subId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005896 } finally {
5897 Binder.restoreCallingIdentity(identity);
5898 }
Shishir Agrawal302c8692015-06-19 13:49:39 -07005899 }
5900
5901 /**
Shuo Qian4a594052020-01-23 11:59:30 -08005902 * Get the call forwarding info, given the call forwarding reason.
5903 */
5904 @Override
Hall Liu27d24262020-09-18 19:04:59 -07005905 public void getCallForwarding(int subId, int callForwardingReason,
5906 ICallForwardingInfoCallback callback) {
Shuo Qian4a594052020-01-23 11:59:30 -08005907 enforceReadPrivilegedPermission("getCallForwarding");
5908 long identity = Binder.clearCallingIdentity();
5909 try {
5910 if (DBG) {
5911 log("getCallForwarding: subId " + subId
5912 + " callForwardingReason" + callForwardingReason);
5913 }
Hall Liu27d24262020-09-18 19:04:59 -07005914
5915 Phone phone = getPhone(subId);
5916 if (phone == null) {
5917 try {
Hall Liu940c4ca2020-09-29 17:10:18 -07005918 callback.onError(
5919 TelephonyManager.CallForwardingInfoCallback.RESULT_ERROR_UNKNOWN);
Hall Liu27d24262020-09-18 19:04:59 -07005920 } catch (RemoteException e) {
5921 // ignore
5922 }
5923 return;
5924 }
5925
5926 Pair<Integer, TelephonyManager.CallForwardingInfoCallback> argument = Pair.create(
5927 callForwardingReason, new TelephonyManager.CallForwardingInfoCallback() {
5928 @Override
5929 public void onCallForwardingInfoAvailable(CallForwardingInfo info) {
5930 try {
5931 callback.onCallForwardingInfoAvailable(info);
5932 } catch (RemoteException e) {
5933 // ignore
5934 }
5935 }
5936
5937 @Override
5938 public void onError(int error) {
5939 try {
5940 callback.onError(error);
5941 } catch (RemoteException e) {
5942 // ignore
5943 }
5944 }
5945 });
5946 sendRequestAsync(CMD_GET_CALL_FORWARDING, argument, phone, null);
Shuo Qian4a594052020-01-23 11:59:30 -08005947 } finally {
5948 Binder.restoreCallingIdentity(identity);
5949 }
5950 }
5951
5952 /**
5953 * Sets the voice call forwarding info including status (enable/disable), call forwarding
5954 * reason, the number to forward, and the timeout before the forwarding is attempted.
5955 */
5956 @Override
Hall Liu27d24262020-09-18 19:04:59 -07005957 public void setCallForwarding(int subId, CallForwardingInfo callForwardingInfo,
5958 IIntegerConsumer callback) {
Shuo Qian4a594052020-01-23 11:59:30 -08005959 enforceModifyPermission();
5960 long identity = Binder.clearCallingIdentity();
5961 try {
5962 if (DBG) {
5963 log("setCallForwarding: subId " + subId
5964 + " callForwardingInfo" + callForwardingInfo);
5965 }
Hall Liu27d24262020-09-18 19:04:59 -07005966
5967 Phone phone = getPhone(subId);
5968 if (phone == null) {
5969 try {
Hall Liu940c4ca2020-09-29 17:10:18 -07005970 callback.accept(
5971 TelephonyManager.CallForwardingInfoCallback.RESULT_ERROR_UNKNOWN);
Hall Liu27d24262020-09-18 19:04:59 -07005972 } catch (RemoteException e) {
5973 // ignore
5974 }
5975 return;
5976 }
5977
5978 Pair<CallForwardingInfo, Consumer<Integer>> arguments = Pair.create(callForwardingInfo,
5979 FunctionalUtils.ignoreRemoteException(callback::accept));
5980
5981 sendRequestAsync(CMD_SET_CALL_FORWARDING, arguments, phone, null);
Shuo Qian4a594052020-01-23 11:59:30 -08005982 } finally {
5983 Binder.restoreCallingIdentity(identity);
5984 }
5985 }
5986
5987 /**
Hall Liu27d24262020-09-18 19:04:59 -07005988 * Get the call waiting status for a subId.
Shuo Qian4a594052020-01-23 11:59:30 -08005989 */
5990 @Override
Hall Liu27d24262020-09-18 19:04:59 -07005991 public void getCallWaitingStatus(int subId, IIntegerConsumer callback) {
SongFerngWang0e767992021-03-31 22:08:45 +08005992 enforceReadPrivilegedPermission("getCallWaitingStatus");
Shuo Qian4a594052020-01-23 11:59:30 -08005993 long identity = Binder.clearCallingIdentity();
5994 try {
Hall Liu27d24262020-09-18 19:04:59 -07005995 Phone phone = getPhone(subId);
5996 if (phone == null) {
5997 try {
5998 callback.accept(TelephonyManager.CALL_WAITING_STATUS_UNKNOWN_ERROR);
5999 } catch (RemoteException e) {
6000 // ignore
6001 }
6002 return;
6003 }
SongFerngWang0e767992021-03-31 22:08:45 +08006004 CarrierConfigManager configManager = new CarrierConfigManager(phone.getContext());
6005 PersistableBundle c = configManager.getConfigForSubId(subId);
6006 boolean requireUssd = c.getBoolean(
6007 CarrierConfigManager.KEY_USE_CALL_WAITING_USSD_BOOL, false);
Hall Liu27d24262020-09-18 19:04:59 -07006008
Shuo Qian4a594052020-01-23 11:59:30 -08006009 if (DBG) log("getCallWaitingStatus: subId " + subId);
SongFerngWang0e767992021-03-31 22:08:45 +08006010 if (requireUssd) {
6011 CarrierXmlParser carrierXmlParser = new CarrierXmlParser(phone.getContext(),
6012 getSubscriptionCarrierId(subId));
6013 String newUssdCommand = "";
6014 try {
6015 newUssdCommand = carrierXmlParser.getFeature(
6016 CarrierXmlParser.FEATURE_CALL_WAITING)
6017 .makeCommand(CarrierXmlParser.SsEntry.SSAction.QUERY, null);
6018 } catch (NullPointerException e) {
6019 loge("Failed to generate USSD number" + e);
6020 }
6021 ResultReceiver wrappedCallback = new CallWaitingUssdResultReceiver(
6022 mMainThreadHandler, callback, carrierXmlParser,
6023 CarrierXmlParser.SsEntry.SSAction.QUERY);
6024 final String ussdCommand = newUssdCommand;
6025 Executors.newSingleThreadExecutor().execute(() -> {
6026 handleUssdRequest(subId, ussdCommand, wrappedCallback);
6027 });
6028 } else {
6029 Consumer<Integer> argument = FunctionalUtils.ignoreRemoteException(
6030 callback::accept);
6031 sendRequestAsync(CMD_GET_CALL_WAITING, argument, phone, null);
6032 }
Shuo Qian4a594052020-01-23 11:59:30 -08006033 } finally {
6034 Binder.restoreCallingIdentity(identity);
6035 }
6036 }
6037
6038 /**
Hall Liu27d24262020-09-18 19:04:59 -07006039 * Sets whether call waiting is enabled for a given subId.
Shuo Qian4a594052020-01-23 11:59:30 -08006040 */
6041 @Override
Hall Liu27d24262020-09-18 19:04:59 -07006042 public void setCallWaitingStatus(int subId, boolean enable, IIntegerConsumer callback) {
Shuo Qian4a594052020-01-23 11:59:30 -08006043 enforceModifyPermission();
6044 long identity = Binder.clearCallingIdentity();
6045 try {
Hall Liu27d24262020-09-18 19:04:59 -07006046 if (DBG) log("setCallWaitingStatus: subId " + subId + " enable: " + enable);
6047
6048 Phone phone = getPhone(subId);
6049 if (phone == null) {
6050 try {
6051 callback.accept(TelephonyManager.CALL_WAITING_STATUS_UNKNOWN_ERROR);
6052 } catch (RemoteException e) {
6053 // ignore
6054 }
6055 return;
6056 }
6057
SongFerngWang0e767992021-03-31 22:08:45 +08006058 CarrierConfigManager configManager = new CarrierConfigManager(phone.getContext());
6059 PersistableBundle c = configManager.getConfigForSubId(subId);
6060 boolean requireUssd = c.getBoolean(
6061 CarrierConfigManager.KEY_USE_CALL_WAITING_USSD_BOOL, false);
Hall Liu27d24262020-09-18 19:04:59 -07006062
SongFerngWang0e767992021-03-31 22:08:45 +08006063 if (DBG) log("getCallWaitingStatus: subId " + subId);
6064 if (requireUssd) {
6065 CarrierXmlParser carrierXmlParser = new CarrierXmlParser(phone.getContext(),
6066 getSubscriptionCarrierId(subId));
6067 CarrierXmlParser.SsEntry.SSAction ssAction =
6068 enable ? CarrierXmlParser.SsEntry.SSAction.UPDATE_ACTIVATE
6069 : CarrierXmlParser.SsEntry.SSAction.UPDATE_DEACTIVATE;
6070 String newUssdCommand = "";
6071 try {
6072 newUssdCommand = carrierXmlParser.getFeature(
6073 CarrierXmlParser.FEATURE_CALL_WAITING)
6074 .makeCommand(ssAction, null);
6075 } catch (NullPointerException e) {
6076 loge("Failed to generate USSD number" + e);
6077 }
6078 ResultReceiver wrappedCallback = new CallWaitingUssdResultReceiver(
6079 mMainThreadHandler, callback, carrierXmlParser, ssAction);
6080 final String ussdCommand = newUssdCommand;
6081 Executors.newSingleThreadExecutor().execute(() -> {
6082 handleUssdRequest(subId, ussdCommand, wrappedCallback);
6083 });
6084 } else {
6085 Pair<Boolean, Consumer<Integer>> arguments = Pair.create(enable,
6086 FunctionalUtils.ignoreRemoteException(callback::accept));
6087
6088 sendRequestAsync(CMD_SET_CALL_WAITING, arguments, phone, null);
6089 }
Shuo Qian4a594052020-01-23 11:59:30 -08006090 } finally {
6091 Binder.restoreCallingIdentity(identity);
6092 }
6093 }
6094
6095 /**
yinxub1bed742017-04-17 11:45:04 -07006096 * Starts a new network scan and returns the id of this scan.
yinxu504e1392017-04-12 16:03:22 -07006097 *
yinxub1bed742017-04-17 11:45:04 -07006098 * @param subId id of the subscription
6099 * @param request contains the radio access networks with bands/channels to scan
6100 * @param messenger callback messenger for scan results or errors
6101 * @param binder for the purpose of auto clean when the user thread crashes
yinxu504e1392017-04-12 16:03:22 -07006102 * @return the id of the requested scan which can be used to stop the scan.
6103 */
6104 @Override
6105 public int requestNetworkScan(int subId, NetworkScanRequest request, Messenger messenger,
Philip P. Moltmann3a2772a2019-10-04 08:15:00 -07006106 IBinder binder, String callingPackage, String callingFeatureId) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08006107 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
6108 mApp, subId, "requestNetworkScan");
Hall Liuf19c44f2018-11-27 14:38:17 -08006109 LocationAccessPolicy.LocationPermissionResult locationResult =
6110 LocationAccessPolicy.checkLocationPermission(mApp,
6111 new LocationAccessPolicy.LocationPermissionQuery.Builder()
6112 .setCallingPackage(callingPackage)
Philip P. Moltmann3a2772a2019-10-04 08:15:00 -07006113 .setCallingFeatureId(callingFeatureId)
Hall Liuf19c44f2018-11-27 14:38:17 -08006114 .setCallingPid(Binder.getCallingPid())
6115 .setCallingUid(Binder.getCallingUid())
6116 .setMethod("requestNetworkScan")
6117 .setMinSdkVersionForFine(Build.VERSION_CODES.Q)
James.cf Lin1d4d7392020-07-03 18:22:53 +08006118 .setMinSdkVersionForCoarse(Build.VERSION_CODES.Q)
6119 .setMinSdkVersionForEnforcement(Build.VERSION_CODES.Q)
Hall Liuf19c44f2018-11-27 14:38:17 -08006120 .build());
Hall Liub2ac8ef2019-02-28 15:56:23 -08006121 if (locationResult != LocationAccessPolicy.LocationPermissionResult.ALLOWED) {
Nathan Harold1c11dba2020-09-22 17:54:53 -07006122 SecurityException e = checkNetworkRequestForSanitizedLocationAccess(
6123 request, subId, callingPackage);
Hall Liub2ac8ef2019-02-28 15:56:23 -08006124 if (e != null) {
6125 if (locationResult == LocationAccessPolicy.LocationPermissionResult.DENIED_HARD) {
6126 throw e;
6127 } else {
Hall Liu0e5abaf2019-04-04 01:25:30 -07006128 loge(e.getMessage());
Hall Liub2ac8ef2019-02-28 15:56:23 -08006129 return TelephonyScanManager.INVALID_SCAN_ID;
6130 }
6131 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006132 }
Hall Liu912dfd32019-04-25 14:02:26 -07006133 int callingUid = Binder.getCallingUid();
6134 int callingPid = Binder.getCallingPid();
Ying Xu94a46582019-04-18 17:14:56 -07006135 final long identity = Binder.clearCallingIdentity();
6136 try {
6137 return mNetworkScanRequestTracker.startNetworkScan(
6138 request, messenger, binder, getPhone(subId),
Hall Liu912dfd32019-04-25 14:02:26 -07006139 callingUid, callingPid, callingPackage);
Ying Xu94a46582019-04-18 17:14:56 -07006140 } finally {
6141 Binder.restoreCallingIdentity(identity);
6142 }
yinxu504e1392017-04-12 16:03:22 -07006143 }
6144
Hall Liub2ac8ef2019-02-28 15:56:23 -08006145 private SecurityException checkNetworkRequestForSanitizedLocationAccess(
Nathan Harold1c11dba2020-09-22 17:54:53 -07006146 NetworkScanRequest request, int subId, String callingPackage) {
6147 boolean hasCarrierPriv = checkCarrierPrivilegesForPackage(subId, callingPackage)
Hall Liu558027f2019-05-15 19:14:05 -07006148 == TelephonyManager.CARRIER_PRIVILEGE_STATUS_HAS_ACCESS;
6149 boolean hasNetworkScanPermission =
6150 mApp.checkCallingOrSelfPermission(android.Manifest.permission.NETWORK_SCAN)
6151 == PERMISSION_GRANTED;
6152
6153 if (!hasCarrierPriv && !hasNetworkScanPermission) {
6154 return new SecurityException("permission.NETWORK_SCAN or carrier privileges is needed"
6155 + " for network scans without location access.");
Hall Liub2ac8ef2019-02-28 15:56:23 -08006156 }
6157
6158 if (request.getSpecifiers() != null && request.getSpecifiers().length > 0) {
6159 for (RadioAccessSpecifier ras : request.getSpecifiers()) {
Hall Liub2ac8ef2019-02-28 15:56:23 -08006160 if (ras.getChannels() != null && ras.getChannels().length > 0) {
6161 return new SecurityException("Specific channels must not be"
6162 + " scanned without location access.");
6163 }
6164 }
6165 }
6166
Hall Liub2ac8ef2019-02-28 15:56:23 -08006167 return null;
6168 }
6169
yinxu504e1392017-04-12 16:03:22 -07006170 /**
6171 * Stops an existing network scan with the given scanId.
yinxub1bed742017-04-17 11:45:04 -07006172 *
6173 * @param subId id of the subscription
6174 * @param scanId id of the scan that needs to be stopped
yinxu504e1392017-04-12 16:03:22 -07006175 */
6176 @Override
6177 public void stopNetworkScan(int subId, int scanId) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08006178 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
6179 mApp, subId, "stopNetworkScan");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006180
Hall Liu912dfd32019-04-25 14:02:26 -07006181 int callingUid = Binder.getCallingUid();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006182 final long identity = Binder.clearCallingIdentity();
6183 try {
Hall Liu912dfd32019-04-25 14:02:26 -07006184 mNetworkScanRequestTracker.stopNetworkScan(scanId, callingUid);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006185 } finally {
6186 Binder.restoreCallingIdentity(identity);
6187 }
yinxu504e1392017-04-12 16:03:22 -07006188 }
6189
6190 /**
SongFerngWang3ef3e072020-12-21 16:41:52 +08006191 * Get the allowed network types bitmask.
Junda Liu84d15a22014-07-02 11:21:04 -07006192 *
SongFerngWang3ef3e072020-12-21 16:41:52 +08006193 * @return the allowed network types bitmask, defined in RILConstants.java.
Junda Liu84d15a22014-07-02 11:21:04 -07006194 */
6195 @Override
SongFerngWang3ef3e072020-12-21 16:41:52 +08006196 public int getAllowedNetworkTypesBitmask(int subId) {
Pengquan Menga4009cb2018-12-20 11:00:24 -08006197 TelephonyPermissions
6198 .enforeceCallingOrSelfReadPrivilegedPhoneStatePermissionOrCarrierPrivilege(
SongFerngWang3ef3e072020-12-21 16:41:52 +08006199 mApp, subId, "getAllowedNetworkTypesBitmask");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006200
6201 final long identity = Binder.clearCallingIdentity();
6202 try {
SongFerngWang3ef3e072020-12-21 16:41:52 +08006203 if (DBG) log("getAllowedNetworkTypesBitmask");
6204 int[] result = (int[]) sendRequest(CMD_GET_ALLOWED_NETWORK_TYPES_BITMASK, null, subId);
6205 int networkTypesBitmask = (result != null ? result[0] : -1);
6206 if (DBG) log("getAllowedNetworkTypesBitmask: " + networkTypesBitmask);
6207 return networkTypesBitmask;
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006208 } finally {
6209 Binder.restoreCallingIdentity(identity);
6210 }
Jake Hamby7c27be32014-03-03 13:25:59 -08006211 }
6212
6213 /**
Sooraj Sasindranc46dfbd2020-06-03 01:06:00 -07006214 * Get the allowed network types for certain reason.
6215 *
6216 * @param subId the id of the subscription.
6217 * @param reason the reason the allowed network type change is taking place
6218 * @return the allowed network types.
6219 */
6220 @Override
6221 public long getAllowedNetworkTypesForReason(int subId,
6222 @TelephonyManager.AllowedNetworkTypesReason int reason) {
SongFerngWang8c6e82e2021-03-02 22:09:29 +08006223 TelephonyPermissions.enforeceCallingOrSelfReadPrecisePhoneStatePermissionOrCarrierPrivilege(
6224 mApp, subId, "getAllowedNetworkTypesForReason");
Sooraj Sasindranc46dfbd2020-06-03 01:06:00 -07006225 final long identity = Binder.clearCallingIdentity();
6226 try {
6227 return getPhoneFromSubId(subId).getAllowedNetworkTypes(reason);
6228 } finally {
6229 Binder.restoreCallingIdentity(identity);
6230 }
6231 }
6232
6233 /**
Sooraj Sasindran37444802020-08-11 10:40:43 -07006234 * Enable/Disable E-UTRA-NR Dual Connectivity
6235 * @param subId subscription id of the sim card
6236 * @param nrDualConnectivityState expected NR dual connectivity state
6237 * This can be passed following states
6238 * <ol>
6239 * <li>Enable NR dual connectivity {@link TelephonyManager#NR_DUAL_CONNECTIVITY_ENABLE}
6240 * <li>Disable NR dual connectivity {@link TelephonyManager#NR_DUAL_CONNECTIVITY_DISABLE}
6241 * <li>Disable NR dual connectivity and force secondary cell to be released
6242 * {@link TelephonyManager#NR_DUAL_CONNECTIVITY_DISABLE_IMMEDIATE}
6243 * </ol>
6244 * @return operation result.
6245 */
6246 @Override
6247 public int setNrDualConnectivityState(int subId,
6248 @TelephonyManager.NrDualConnectivityState int nrDualConnectivityState) {
6249 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
6250 mApp, subId, "enableNRDualConnectivity");
Sooraj Sasindran0e4e00a2021-03-16 18:02:32 -07006251 if (!isRadioInterfaceCapabilitySupported(
Sooraj Sasindrandfd595b2021-03-11 17:38:13 -08006252 TelephonyManager.CAPABILITY_NR_DUAL_CONNECTIVITY_CONFIGURATION_AVAILABLE)) {
6253 return TelephonyManager.ENABLE_NR_DUAL_CONNECTIVITY_NOT_SUPPORTED;
6254 }
6255
Sooraj Sasindran37444802020-08-11 10:40:43 -07006256 WorkSource workSource = getWorkSource(Binder.getCallingUid());
6257 final long identity = Binder.clearCallingIdentity();
6258 try {
6259 int result = (int) sendRequest(CMD_ENABLE_NR_DUAL_CONNECTIVITY,
6260 nrDualConnectivityState, subId,
6261 workSource);
6262 if (DBG) log("enableNRDualConnectivity result: " + result);
6263 return result;
6264 } finally {
6265 Binder.restoreCallingIdentity(identity);
6266 }
6267 }
6268
6269 /**
6270 * Is E-UTRA-NR Dual Connectivity enabled
6271 * @return true if dual connectivity is enabled else false
6272 */
6273 @Override
6274 public boolean isNrDualConnectivityEnabled(int subId) {
6275 TelephonyPermissions
6276 .enforeceCallingOrSelfReadPrivilegedPhoneStatePermissionOrCarrierPrivilege(
6277 mApp, subId, "isNRDualConnectivityEnabled");
Sooraj Sasindran0e4e00a2021-03-16 18:02:32 -07006278 if (!isRadioInterfaceCapabilitySupported(
Sooraj Sasindrandfd595b2021-03-11 17:38:13 -08006279 TelephonyManager.CAPABILITY_NR_DUAL_CONNECTIVITY_CONFIGURATION_AVAILABLE)) {
6280 return false;
6281 }
Sooraj Sasindran37444802020-08-11 10:40:43 -07006282 WorkSource workSource = getWorkSource(Binder.getCallingUid());
6283 final long identity = Binder.clearCallingIdentity();
6284 try {
6285 boolean isEnabled = (boolean) sendRequest(CMD_IS_NR_DUAL_CONNECTIVITY_ENABLED,
6286 null, subId, workSource);
6287 if (DBG) log("isNRDualConnectivityEnabled: " + isEnabled);
6288 return isEnabled;
6289 } finally {
6290 Binder.restoreCallingIdentity(identity);
6291 }
6292 }
6293
6294 /**
Sooraj Sasindranc46dfbd2020-06-03 01:06:00 -07006295 * Set the allowed network types of the device and
6296 * provide the reason triggering the allowed network change.
6297 *
6298 * @param subId the id of the subscription.
6299 * @param reason the reason the allowed network type change is taking place
6300 * @param allowedNetworkTypes the allowed network types.
6301 * @return true on success; false on any failure.
6302 */
6303 @Override
6304 public boolean setAllowedNetworkTypesForReason(int subId,
SongFerngWang3ef3e072020-12-21 16:41:52 +08006305 @TelephonyManager.AllowedNetworkTypesReason int reason,
6306 @TelephonyManager.NetworkTypeBitMask long allowedNetworkTypes) {
Sooraj Sasindranc46dfbd2020-06-03 01:06:00 -07006307 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
6308 mApp, subId, "setAllowedNetworkTypesForReason");
SongFerngWang3ef3e072020-12-21 16:41:52 +08006309 if (!TelephonyManager.isValidAllowedNetworkTypesReason(reason)) {
6310 Rlog.e(LOG_TAG, "Invalid allowed network type reason: " + reason);
6311 return false;
6312 }
6313
SongFerngWang8c6e82e2021-03-02 22:09:29 +08006314 log("setAllowedNetworkTypesForReason: " + reason + " value: "
6315 + TelephonyManager.convertNetworkTypeBitmaskToString(allowedNetworkTypes));
6316
6317
6318 if (allowedNetworkTypes == getPhoneFromSubId(subId).getAllowedNetworkTypes(reason)) {
6319 log("setAllowedNetworkTypesForReason: " + reason + "does not change value");
6320 return true;
SongFerngWang3ef3e072020-12-21 16:41:52 +08006321 }
SongFerngWang8c6e82e2021-03-02 22:09:29 +08006322
Sooraj Sasindranc46dfbd2020-06-03 01:06:00 -07006323 final long identity = Binder.clearCallingIdentity();
6324 try {
SongFerngWang3ef3e072020-12-21 16:41:52 +08006325 Boolean success = (Boolean) sendRequest(
6326 CMD_SET_ALLOWED_NETWORK_TYPES_FOR_REASON,
6327 new Pair<Integer, Long>(reason, allowedNetworkTypes), subId);
6328
6329 if (DBG) log("setAllowedNetworkTypesForReason: " + (success ? "ok" : "fail"));
6330 return success;
Sooraj Sasindranc46dfbd2020-06-03 01:06:00 -07006331 } finally {
6332 Binder.restoreCallingIdentity(identity);
6333 }
6334 }
6335
6336 /**
Miaoa84611c2019-03-15 09:21:10 +08006337 * Check whether DUN APN is required for tethering with subId.
Junda Liu475951f2014-11-07 16:45:03 -08006338 *
Miaoa84611c2019-03-15 09:21:10 +08006339 * @param subId the id of the subscription to require tethering.
Amit Mahajanfe58cdf2017-07-11 12:01:53 -07006340 * @return {@code true} if DUN APN is required for tethering.
Junda Liu475951f2014-11-07 16:45:03 -08006341 * @hide
6342 */
6343 @Override
SongFerngWangf08d8122019-11-15 14:58:44 +08006344 public boolean isTetheringApnRequiredForSubscriber(int subId) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08006345 enforceModifyPermission();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006346 final long identity = Binder.clearCallingIdentity();
Miaoa84611c2019-03-15 09:21:10 +08006347 final Phone phone = getPhone(subId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006348 try {
Miaoa84611c2019-03-15 09:21:10 +08006349 if (phone != null) {
6350 return phone.hasMatchedTetherApnSetting();
6351 } else {
6352 return false;
6353 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006354 } finally {
6355 Binder.restoreCallingIdentity(identity);
Junda Liu475951f2014-11-07 16:45:03 -08006356 }
Junda Liu475951f2014-11-07 16:45:03 -08006357 }
6358
6359 /**
Shuo Qiancd19c462020-01-16 20:51:11 -08006360 * Enable or disable always reporting signal strength changes from radio.
6361 *
6362 * @param isEnable {@code true} for enabling; {@code false} for disabling.
6363 */
6364 @Override
6365 public void setAlwaysReportSignalStrength(int subId, boolean isEnable) {
6366 enforceModifyPermission();
6367 enforceSystemCaller();
6368
6369 final long identity = Binder.clearCallingIdentity();
6370 final Phone phone = getPhone(subId);
6371 try {
6372 if (phone != null) {
6373 if (DBG) {
6374 log("setAlwaysReportSignalStrength: subId=" + subId
6375 + " isEnable=" + isEnable);
6376 }
6377 phone.setAlwaysReportSignalStrength(isEnable);
6378 } else {
6379 loge("setAlwaysReportSignalStrength: no phone found for subId="
6380 + subId);
6381 }
6382 } finally {
6383 Binder.restoreCallingIdentity(identity);
6384 }
6385 }
6386
6387 /**
Malcolm Chen964682d2017-11-28 16:20:07 -08006388 * Get the user enabled state of Mobile Data.
6389 *
6390 * TODO: remove and use isUserDataEnabled.
6391 * This can't be removed now because some vendor codes
6392 * calls through ITelephony directly while they should
6393 * use TelephonyManager.
6394 *
6395 * @return true on enabled
6396 */
6397 @Override
6398 public boolean getDataEnabled(int subId) {
6399 return isUserDataEnabled(subId);
6400 }
6401
6402 /**
6403 * Get whether mobile data is enabled per user setting.
6404 *
6405 * There are other factors deciding whether mobile data is actually enabled, but they are
6406 * not considered here. See {@link #isDataEnabled(int)} for more details.
Robert Greenwalt646120a2014-05-23 11:54:03 -07006407 *
Jeff Davidsona1920712016-11-18 17:05:56 -08006408 * Accepts either ACCESS_NETWORK_STATE, MODIFY_PHONE_STATE or carrier privileges.
Robert Greenwalted86e582014-05-21 20:03:20 -07006409 *
6410 * @return {@code true} if data is enabled else {@code false}
6411 */
6412 @Override
Malcolm Chen964682d2017-11-28 16:20:07 -08006413 public boolean isUserDataEnabled(int subId) {
Robert Greenwalt646120a2014-05-23 11:54:03 -07006414 try {
6415 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.ACCESS_NETWORK_STATE,
6416 null);
6417 } catch (Exception e) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08006418 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
6419 mApp, subId, "isUserDataEnabled");
Robert Greenwalt646120a2014-05-23 11:54:03 -07006420 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006421
6422 final long identity = Binder.clearCallingIdentity();
6423 try {
6424 int phoneId = mSubscriptionController.getPhoneId(subId);
6425 if (DBG) log("isUserDataEnabled: subId=" + subId + " phoneId=" + phoneId);
6426 Phone phone = PhoneFactory.getPhone(phoneId);
6427 if (phone != null) {
6428 boolean retVal = phone.isUserDataEnabled();
6429 if (DBG) log("isUserDataEnabled: subId=" + subId + " retVal=" + retVal);
6430 return retVal;
6431 } else {
6432 if (DBG) loge("isUserDataEnabled: no phone subId=" + subId + " retVal=false");
6433 return false;
6434 }
6435 } finally {
6436 Binder.restoreCallingIdentity(identity);
Malcolm Chen964682d2017-11-28 16:20:07 -08006437 }
6438 }
6439
6440 /**
Shuo Qian8ee4e882020-01-08 14:30:06 -08006441 * Checks if the device is capable of mobile data by considering whether whether the
6442 * user has enabled mobile data, whether the carrier has enabled mobile data, and
6443 * whether the network policy allows data connections.
Malcolm Chen964682d2017-11-28 16:20:07 -08006444 *
Shuo Qian8ee4e882020-01-08 14:30:06 -08006445 * @return {@code true} if the overall data connection is capable; {@code false} if not.
Malcolm Chen964682d2017-11-28 16:20:07 -08006446 */
6447 @Override
6448 public boolean isDataEnabled(int subId) {
Sooraj Sasindran95c07a92020-07-15 01:35:24 -07006449 try {
6450 try {
6451 mApp.enforceCallingOrSelfPermission(
6452 android.Manifest.permission.ACCESS_NETWORK_STATE,
6453 null);
6454 } catch (Exception e) {
6455 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.READ_PHONE_STATE,
6456 "isDataEnabled");
6457 }
6458 } catch (Exception e) {
6459 enforceReadPrivilegedPermission("isDataEnabled");
6460 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006461
6462 final long identity = Binder.clearCallingIdentity();
6463 try {
6464 int phoneId = mSubscriptionController.getPhoneId(subId);
6465 if (DBG) log("isDataEnabled: subId=" + subId + " phoneId=" + phoneId);
6466 Phone phone = PhoneFactory.getPhone(phoneId);
6467 if (phone != null) {
Jack Yud79fba22018-12-13 11:51:28 -08006468 boolean retVal = phone.getDataEnabledSettings().isDataEnabled();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006469 if (DBG) log("isDataEnabled: subId=" + subId + " retVal=" + retVal);
6470 return retVal;
6471 } else {
6472 if (DBG) loge("isDataEnabled: no phone subId=" + subId + " retVal=false");
6473 return false;
6474 }
6475 } finally {
6476 Binder.restoreCallingIdentity(identity);
Wink Savillee7353bb2014-12-05 14:21:41 -08006477 }
Robert Greenwalted86e582014-05-21 20:03:20 -07006478 }
Shishir Agrawal60f9c952014-06-23 12:00:43 -07006479
Sooraj Sasindranaf1b5132020-05-05 21:06:20 +00006480 /**
6481 * Check if data is enabled for a specific reason
6482 * @param subId Subscription index
6483 * @param reason the reason the data enable change is taking place
6484 * @return {@code true} if the overall data is enabled; {@code false} if not.
6485 */
6486 @Override
Sooraj Sasindran95c07a92020-07-15 01:35:24 -07006487 public boolean isDataEnabledForReason(int subId,
Sooraj Sasindranaf1b5132020-05-05 21:06:20 +00006488 @TelephonyManager.DataEnabledReason int reason) {
6489 try {
6490 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.ACCESS_NETWORK_STATE,
6491 null);
6492 } catch (Exception e) {
6493 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.READ_PHONE_STATE,
Sooraj Sasindran95c07a92020-07-15 01:35:24 -07006494 "isDataEnabledForReason");
Sooraj Sasindranaf1b5132020-05-05 21:06:20 +00006495 }
6496
6497
6498 final long identity = Binder.clearCallingIdentity();
6499 try {
6500 int phoneId = mSubscriptionController.getPhoneId(subId);
6501 if (DBG) {
Sooraj Sasindran95c07a92020-07-15 01:35:24 -07006502 log("isDataEnabledForReason: subId=" + subId + " phoneId=" + phoneId
Sooraj Sasindranaf1b5132020-05-05 21:06:20 +00006503 + " reason=" + reason);
6504 }
6505 Phone phone = PhoneFactory.getPhone(phoneId);
6506 if (phone != null) {
6507 boolean retVal;
6508 if (reason == TelephonyManager.DATA_ENABLED_REASON_USER) {
6509 retVal = phone.isUserDataEnabled();
6510 } else {
Sooraj Sasindran95c07a92020-07-15 01:35:24 -07006511 retVal = phone.getDataEnabledSettings().isDataEnabledForReason(reason);
Sooraj Sasindranaf1b5132020-05-05 21:06:20 +00006512 }
Sooraj Sasindran95c07a92020-07-15 01:35:24 -07006513 if (DBG) log("isDataEnabledForReason: retVal=" + retVal);
Sooraj Sasindranaf1b5132020-05-05 21:06:20 +00006514 return retVal;
6515 } else {
6516 if (DBG) {
Sooraj Sasindran95c07a92020-07-15 01:35:24 -07006517 loge("isDataEnabledForReason: no phone subId="
Sooraj Sasindranaf1b5132020-05-05 21:06:20 +00006518 + subId + " retVal=false");
6519 }
6520 return false;
6521 }
6522 } finally {
6523 Binder.restoreCallingIdentity(identity);
6524 }
6525 }
6526
Malcolm Chendf0b4cc2020-02-24 15:12:43 -08006527 private int getCarrierPrivilegeStatusFromCarrierConfigRules(int privilegeFromSim, int uid,
Nazanin Bakhshi5d0636e2019-08-19 16:29:37 -07006528 Phone phone) {
Hall Liu54a2a0c2020-07-13 12:13:03 -07006529 if (uid == Process.SYSTEM_UID || uid == Process.PHONE_UID) {
6530 // Skip the check if it's one of these special uids
6531 return TelephonyManager.CARRIER_PRIVILEGE_STATUS_HAS_ACCESS;
6532 }
6533
Nazanin Bakhshi5d0636e2019-08-19 16:29:37 -07006534 //load access rules from carrier configs, and check those as well: b/139133814
6535 SubscriptionController subController = SubscriptionController.getInstance();
6536 if (privilegeFromSim == TelephonyManager.CARRIER_PRIVILEGE_STATUS_HAS_ACCESS
6537 || subController == null) return privilegeFromSim;
6538
Nazanin Bakhshi5d0636e2019-08-19 16:29:37 -07006539 PackageManager pkgMgr = phone.getContext().getPackageManager();
6540 String[] packages = pkgMgr.getPackagesForUid(uid);
6541
6542 final long identity = Binder.clearCallingIdentity();
6543 try {
Jeff Davidson8ab02b22020-03-28 12:24:40 -07006544 int subId = phone.getSubId();
6545 if (mCarrierPrivilegeTestOverrideSubIds.contains(subId)) {
6546 // A test override is in place for the privileges for this subId, so don't try to
6547 // read the subscription privileges.
6548 return privilegeFromSim;
6549 }
6550 SubscriptionInfo subInfo = subController.getSubscriptionInfo(subId);
Nazanin Bakhshi5d0636e2019-08-19 16:29:37 -07006551 SubscriptionManager subManager = (SubscriptionManager)
6552 phone.getContext().getSystemService(Context.TELEPHONY_SUBSCRIPTION_SERVICE);
6553 for (String pkg : packages) {
6554 if (subManager.canManageSubscription(subInfo, pkg)) {
6555 return TelephonyManager.CARRIER_PRIVILEGE_STATUS_HAS_ACCESS;
6556 }
6557 }
6558 return privilegeFromSim;
6559 } finally {
6560 Binder.restoreCallingIdentity(identity);
6561 }
6562 }
6563
6564 private int getCarrierPrivilegeStatusFromCarrierConfigRules(int privilegeFromSim, Phone phone,
6565 String pkgName) {
6566 //load access rules from carrier configs, and check those as well: b/139133814
6567 SubscriptionController subController = SubscriptionController.getInstance();
6568 if (privilegeFromSim == TelephonyManager.CARRIER_PRIVILEGE_STATUS_HAS_ACCESS
6569 || subController == null) return privilegeFromSim;
6570
6571 final long identity = Binder.clearCallingIdentity();
6572 try {
Jeff Davidson8ab02b22020-03-28 12:24:40 -07006573 int subId = phone.getSubId();
6574 if (mCarrierPrivilegeTestOverrideSubIds.contains(subId)) {
6575 // A test override is in place for the privileges for this subId, so don't try to
6576 // read the subscription privileges.
6577 return privilegeFromSim;
6578 }
6579 SubscriptionInfo subInfo = subController.getSubscriptionInfo(subId);
Nazanin Bakhshi5d0636e2019-08-19 16:29:37 -07006580 SubscriptionManager subManager = (SubscriptionManager)
6581 phone.getContext().getSystemService(Context.TELEPHONY_SUBSCRIPTION_SERVICE);
6582 return subManager.canManageSubscription(subInfo, pkgName)
6583 ? TelephonyManager.CARRIER_PRIVILEGE_STATUS_HAS_ACCESS : privilegeFromSim;
6584 } finally {
6585 Binder.restoreCallingIdentity(identity);
6586 }
6587 }
6588
Shishir Agrawal60f9c952014-06-23 12:00:43 -07006589 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08006590 public int getCarrierPrivilegeStatus(int subId) {
6591 final Phone phone = getPhone(subId);
6592 if (phone == null) {
6593 loge("getCarrierPrivilegeStatus: Invalid subId");
6594 return TelephonyManager.CARRIER_PRIVILEGE_STATUS_NO_ACCESS;
6595 }
6596 UiccCard card = UiccController.getInstance().getUiccCard(phone.getPhoneId());
Shishir Agrawaleb8771e2014-07-22 11:24:08 -07006597 if (card == null) {
Shishir Agrawal5e5becd2014-11-18 11:38:23 -08006598 loge("getCarrierPrivilegeStatus: No UICC");
Shishir Agrawaleb8771e2014-07-22 11:24:08 -07006599 return TelephonyManager.CARRIER_PRIVILEGE_STATUS_RULES_NOT_LOADED;
6600 }
Nazanin Bakhshi5d0636e2019-08-19 16:29:37 -07006601
6602 return getCarrierPrivilegeStatusFromCarrierConfigRules(
6603 card.getCarrierPrivilegeStatusForCurrentTransaction(
Malcolm Chendf0b4cc2020-02-24 15:12:43 -08006604 phone.getContext().getPackageManager()), Binder.getCallingUid(), phone);
Shishir Agrawal60f9c952014-06-23 12:00:43 -07006605 }
Junda Liu29340342014-07-10 15:23:27 -07006606
6607 @Override
Jeff Davidson7e17e312018-02-13 18:17:36 -08006608 public int getCarrierPrivilegeStatusForUid(int subId, int uid) {
Shuo Qian2c0ae432019-12-05 11:40:37 -08006609 enforceReadPrivilegedPermission("getCarrierPrivilegeStatusForUid");
Jeff Davidson7e17e312018-02-13 18:17:36 -08006610 final Phone phone = getPhone(subId);
6611 if (phone == null) {
Taesu Leef8fbed92019-10-07 18:47:02 +09006612 loge("getCarrierPrivilegeStatusForUid: Invalid subId");
Jeff Davidson7e17e312018-02-13 18:17:36 -08006613 return TelephonyManager.CARRIER_PRIVILEGE_STATUS_NO_ACCESS;
6614 }
6615 UiccProfile profile =
6616 UiccController.getInstance().getUiccProfileForPhone(phone.getPhoneId());
6617 if (profile == null) {
Taesu Leef8fbed92019-10-07 18:47:02 +09006618 loge("getCarrierPrivilegeStatusForUid: No UICC");
Jeff Davidson7e17e312018-02-13 18:17:36 -08006619 return TelephonyManager.CARRIER_PRIVILEGE_STATUS_RULES_NOT_LOADED;
6620 }
Nazanin Bakhshi5d0636e2019-08-19 16:29:37 -07006621 return getCarrierPrivilegeStatusFromCarrierConfigRules(
Shuo Qian2c0ae432019-12-05 11:40:37 -08006622 profile.getCarrierPrivilegeStatusForUid(
Malcolm Chendf0b4cc2020-02-24 15:12:43 -08006623 phone.getContext().getPackageManager(), uid), uid, phone);
Jeff Davidson7e17e312018-02-13 18:17:36 -08006624 }
6625
6626 @Override
chen xuf7e9fe82019-05-09 19:31:02 -07006627 public int checkCarrierPrivilegesForPackage(int subId, String pkgName) {
6628 if (TextUtils.isEmpty(pkgName)) {
Junda Liu317d70b2016-03-08 09:33:53 -08006629 return TelephonyManager.CARRIER_PRIVILEGE_STATUS_NO_ACCESS;
chen xuf7e9fe82019-05-09 19:31:02 -07006630 }
6631
6632 int phoneId = SubscriptionManager.getPhoneId(subId);
6633 UiccCard card = UiccController.getInstance().getUiccCard(phoneId);
Shishir Agrawaleb8771e2014-07-22 11:24:08 -07006634 if (card == null) {
chen xuf7e9fe82019-05-09 19:31:02 -07006635 loge("checkCarrierPrivilegesForPackage: No UICC on subId " + subId);
Shishir Agrawaleb8771e2014-07-22 11:24:08 -07006636 return TelephonyManager.CARRIER_PRIVILEGE_STATUS_RULES_NOT_LOADED;
6637 }
Nazanin Bakhshi5d0636e2019-08-19 16:29:37 -07006638 return getCarrierPrivilegeStatusFromCarrierConfigRules(
6639 card.getCarrierPrivilegeStatus(mApp.getPackageManager(), pkgName),
6640 getPhone(phoneId), pkgName);
Zach Johnson50ecba32015-05-19 00:24:21 -07006641 }
6642
6643 @Override
6644 public int checkCarrierPrivilegesForPackageAnyPhone(String pkgName) {
Junda Liu317d70b2016-03-08 09:33:53 -08006645 if (TextUtils.isEmpty(pkgName))
6646 return TelephonyManager.CARRIER_PRIVILEGE_STATUS_NO_ACCESS;
Zach Johnson50ecba32015-05-19 00:24:21 -07006647 int result = TelephonyManager.CARRIER_PRIVILEGE_STATUS_RULES_NOT_LOADED;
6648 for (int i = 0; i < TelephonyManager.getDefault().getPhoneCount(); i++) {
6649 UiccCard card = UiccController.getInstance().getUiccCard(i);
6650 if (card == null) {
Jonathan Basseri7d320df2015-06-16 12:17:08 -07006651 // No UICC in that slot.
Zach Johnson50ecba32015-05-19 00:24:21 -07006652 continue;
6653 }
6654
Nazanin Bakhshi5d0636e2019-08-19 16:29:37 -07006655 result = getCarrierPrivilegeStatusFromCarrierConfigRules(
6656 card.getCarrierPrivilegeStatus(mApp.getPackageManager(), pkgName),
6657 getPhone(i), pkgName);
Zach Johnson50ecba32015-05-19 00:24:21 -07006658 if (result == TelephonyManager.CARRIER_PRIVILEGE_STATUS_HAS_ACCESS) {
6659 break;
6660 }
6661 }
6662
6663 return result;
Junda Liu29340342014-07-10 15:23:27 -07006664 }
Derek Tan89e89d42014-07-08 17:00:10 -07006665
6666 @Override
Junda Liue64de782015-04-16 17:19:16 -07006667 public List<String> getCarrierPackageNamesForIntentAndPhone(Intent intent, int phoneId) {
6668 if (!SubscriptionManager.isValidPhoneId(phoneId)) {
6669 loge("phoneId " + phoneId + " is not valid.");
6670 return null;
6671 }
6672 UiccCard card = UiccController.getInstance().getUiccCard(phoneId);
Shishir Agrawaleb6439a2014-07-21 13:19:38 -07006673 if (card == null) {
Taesu Leef8fbed92019-10-07 18:47:02 +09006674 loge("getCarrierPackageNamesForIntentAndPhone: No UICC");
Shishir Agrawaleb6439a2014-07-21 13:19:38 -07006675 return null ;
6676 }
Nazanin Bakhshif782e562018-12-11 15:15:39 -08006677 return card.getCarrierPackageNamesForIntent(mApp.getPackageManager(), intent);
Shishir Agrawaleb6439a2014-07-21 13:19:38 -07006678 }
6679
Amith Yamasani6e118872016-02-19 12:53:51 -08006680 @Override
chen xuf7e9fe82019-05-09 19:31:02 -07006681 public List<String> getPackagesWithCarrierPrivileges(int phoneId) {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08006682 PackageManager pm = mApp.getPackageManager();
Amith Yamasani6e118872016-02-19 12:53:51 -08006683 List<String> privilegedPackages = new ArrayList<>();
6684 List<PackageInfo> packages = null;
chen xuf7e9fe82019-05-09 19:31:02 -07006685 UiccCard card = UiccController.getInstance().getUiccCard(phoneId);
6686 // has UICC in that slot.
6687 if (card != null) {
Amith Yamasani6e118872016-02-19 12:53:51 -08006688 if (card.hasCarrierPrivilegeRules()) {
6689 if (packages == null) {
6690 // Only check packages in user 0 for now
6691 packages = pm.getInstalledPackagesAsUser(
Nazanin Bakhshi5d0636e2019-08-19 16:29:37 -07006692 PackageManager.MATCH_DISABLED_COMPONENTS
6693 | PackageManager.MATCH_DISABLED_UNTIL_USED_COMPONENTS
Cheonho Park17089c62019-08-01 15:23:12 +09006694 | PackageManager.GET_SIGNING_CERTIFICATES,
Amit Mahajanb8f13202020-01-27 18:16:07 -08006695 UserHandle.SYSTEM.getIdentifier());
Amith Yamasani6e118872016-02-19 12:53:51 -08006696 }
6697 for (int p = packages.size() - 1; p >= 0; p--) {
6698 PackageInfo pkgInfo = packages.get(p);
6699 if (pkgInfo != null && pkgInfo.packageName != null
6700 && card.getCarrierPrivilegeStatus(pkgInfo)
chen xuf7e9fe82019-05-09 19:31:02 -07006701 == TelephonyManager.CARRIER_PRIVILEGE_STATUS_HAS_ACCESS) {
Amith Yamasani6e118872016-02-19 12:53:51 -08006702 privilegedPackages.add(pkgInfo.packageName);
6703 }
6704 }
6705 }
6706 }
6707 return privilegedPackages;
6708 }
6709
chen xuf7e9fe82019-05-09 19:31:02 -07006710 @Override
6711 public List<String> getPackagesWithCarrierPrivilegesForAllPhones() {
Shuo Qian067a06d2019-12-03 23:40:18 +00006712 enforceReadPrivilegedPermission("getPackagesWithCarrierPrivilegesForAllPhones");
6713
6714 final long identity = Binder.clearCallingIdentity();
6715
chen xuf7e9fe82019-05-09 19:31:02 -07006716 List<String> privilegedPackages = new ArrayList<>();
Shuo Qian067a06d2019-12-03 23:40:18 +00006717 try {
6718 for (int i = 0; i < TelephonyManager.getDefault().getPhoneCount(); i++) {
6719 privilegedPackages.addAll(getPackagesWithCarrierPrivileges(i));
6720 }
6721 } finally {
6722 Binder.restoreCallingIdentity(identity);
chen xuf7e9fe82019-05-09 19:31:02 -07006723 }
6724 return privilegedPackages;
6725 }
6726
Wink Savilleb564aae2014-10-23 10:18:09 -07006727 private String getIccId(int subId) {
Sanket Padawe356d7632015-06-22 14:03:32 -07006728 final Phone phone = getPhone(subId);
6729 UiccCard card = phone == null ? null : phone.getUiccCard();
Derek Tan97ebb422014-09-05 16:55:38 -07006730 if (card == null) {
Derek Tan97ebb422014-09-05 16:55:38 -07006731 return null;
6732 }
6733 String iccId = card.getIccId();
6734 if (TextUtils.isEmpty(iccId)) {
Derek Tan97ebb422014-09-05 16:55:38 -07006735 return null;
6736 }
6737 return iccId;
6738 }
6739
Shishir Agrawaleb6439a2014-07-21 13:19:38 -07006740 @Override
Shuo Qiane4e11672020-12-15 22:15:33 -08006741 public void setCallComposerStatus(int subId, int status) {
6742 enforceModifyPermission();
6743
6744 final long identity = Binder.clearCallingIdentity();
6745 try {
6746 Phone phone = getPhone(subId);
6747 if (phone != null) {
6748 Phone defaultPhone = phone.getImsPhone();
6749 if (defaultPhone != null && defaultPhone.getPhoneType() == PHONE_TYPE_IMS) {
6750 ImsPhone imsPhone = (ImsPhone) defaultPhone;
6751 imsPhone.setCallComposerStatus(status);
Shuo Qian284ae752020-12-22 19:10:14 -08006752 ImsManager.getInstance(mApp, getSlotIndexOrException(subId))
6753 .updateImsServiceConfig();
Shuo Qiane4e11672020-12-15 22:15:33 -08006754 }
6755 }
Shuo Qian284ae752020-12-22 19:10:14 -08006756 } catch (ImsException e) {
6757 throw new ServiceSpecificException(e.getCode());
6758 } finally {
Shuo Qiane4e11672020-12-15 22:15:33 -08006759 Binder.restoreCallingIdentity(identity);
6760 }
6761 }
6762
6763 @Override
6764 public int getCallComposerStatus(int subId) {
6765 enforceReadPrivilegedPermission("getCallComposerStatus");
6766
6767 final long identity = Binder.clearCallingIdentity();
6768 try {
6769 Phone phone = getPhone(subId);
6770 if (phone != null) {
6771 Phone defaultPhone = phone.getImsPhone();
6772 if (defaultPhone != null && defaultPhone.getPhoneType() == PHONE_TYPE_IMS) {
6773 ImsPhone imsPhone = (ImsPhone) defaultPhone;
6774 return imsPhone.getCallComposerStatus();
6775 }
6776 }
6777 } finally {
6778 Binder.restoreCallingIdentity(identity);
6779 }
6780 return TelephonyManager.CALL_COMPOSER_STATUS_OFF;
6781 }
6782
6783 @Override
Jeff Sharkey85190e62014-12-05 09:40:12 -08006784 public boolean setLine1NumberForDisplayForSubscriber(int subId, String alphaTag,
6785 String number) {
Shuo Qian2c0ae432019-12-05 11:40:37 -08006786 TelephonyPermissions.enforceCallingOrSelfCarrierPrivilege(mApp,
Jeff Davidson7e17e312018-02-13 18:17:36 -08006787 subId, "setLine1NumberForDisplayForSubscriber");
Derek Tan97ebb422014-09-05 16:55:38 -07006788
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006789 final long identity = Binder.clearCallingIdentity();
6790 try {
6791 final String iccId = getIccId(subId);
6792 final Phone phone = getPhone(subId);
6793 if (phone == null) {
6794 return false;
6795 }
6796 final String subscriberId = phone.getSubscriberId();
6797
6798 if (DBG_MERGE) {
Amit Mahajanb8f13202020-01-27 18:16:07 -08006799 Rlog.d(LOG_TAG, "Setting line number for ICC=" + iccId + ", subscriberId="
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006800 + subscriberId + " to " + number);
6801 }
6802
6803 if (TextUtils.isEmpty(iccId)) {
6804 return false;
6805 }
6806
6807 final SharedPreferences.Editor editor = mTelephonySharedPreferences.edit();
6808
6809 final String alphaTagPrefKey = PREF_CARRIERS_ALPHATAG_PREFIX + iccId;
6810 if (alphaTag == null) {
6811 editor.remove(alphaTagPrefKey);
6812 } else {
6813 editor.putString(alphaTagPrefKey, alphaTag);
6814 }
6815
6816 // Record both the line number and IMSI for this ICCID, since we need to
6817 // track all merged IMSIs based on line number
6818 final String numberPrefKey = PREF_CARRIERS_NUMBER_PREFIX + iccId;
6819 final String subscriberPrefKey = PREF_CARRIERS_SUBSCRIBER_PREFIX + iccId;
6820 if (number == null) {
6821 editor.remove(numberPrefKey);
6822 editor.remove(subscriberPrefKey);
6823 } else {
6824 editor.putString(numberPrefKey, number);
6825 editor.putString(subscriberPrefKey, subscriberId);
6826 }
6827
6828 editor.commit();
6829 return true;
6830 } finally {
6831 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07006832 }
Derek Tan7226c842014-07-02 17:42:23 -07006833 }
6834
6835 @Override
Philip P. Moltmann700a9592019-10-03 11:53:50 -07006836 public String getLine1NumberForDisplay(int subId, String callingPackage,
6837 String callingFeatureId) {
Makoto Onukifee69342015-06-29 14:44:50 -07006838 // This is open to apps with WRITE_SMS.
Jeff Davidson7e17e312018-02-13 18:17:36 -08006839 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneNumber(
Philip P. Moltmann700a9592019-10-03 11:53:50 -07006840 mApp, subId, callingPackage, callingFeatureId, "getLine1NumberForDisplay")) {
Amit Mahajan9cf11512015-11-09 11:40:48 -08006841 if (DBG_MERGE) log("getLine1NumberForDisplay returning null due to permission");
Svet Ganovb320e182015-04-16 12:30:10 -07006842 return null;
6843 }
Derek Tan97ebb422014-09-05 16:55:38 -07006844
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006845 final long identity = Binder.clearCallingIdentity();
6846 try {
6847 String iccId = getIccId(subId);
6848 if (iccId != null) {
6849 String numberPrefKey = PREF_CARRIERS_NUMBER_PREFIX + iccId;
6850 if (DBG_MERGE) {
6851 log("getLine1NumberForDisplay returning "
6852 + mTelephonySharedPreferences.getString(numberPrefKey, null));
6853 }
6854 return mTelephonySharedPreferences.getString(numberPrefKey, null);
Amit Mahajan9cf11512015-11-09 11:40:48 -08006855 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006856 if (DBG_MERGE) log("getLine1NumberForDisplay returning null as iccId is null");
6857 return null;
6858 } finally {
6859 Binder.restoreCallingIdentity(identity);
Derek Tan7226c842014-07-02 17:42:23 -07006860 }
Derek Tan7226c842014-07-02 17:42:23 -07006861 }
6862
6863 @Override
Philip P. Moltmann700a9592019-10-03 11:53:50 -07006864 public String getLine1AlphaTagForDisplay(int subId, String callingPackage,
6865 String callingFeatureId) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08006866 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
Philip P. Moltmann700a9592019-10-03 11:53:50 -07006867 mApp, subId, callingPackage, callingFeatureId, "getLine1AlphaTagForDisplay")) {
Svet Ganovb320e182015-04-16 12:30:10 -07006868 return null;
6869 }
Derek Tan97ebb422014-09-05 16:55:38 -07006870
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006871 final long identity = Binder.clearCallingIdentity();
6872 try {
6873 String iccId = getIccId(subId);
6874 if (iccId != null) {
6875 String alphaTagPrefKey = PREF_CARRIERS_ALPHATAG_PREFIX + iccId;
6876 return mTelephonySharedPreferences.getString(alphaTagPrefKey, null);
6877 }
6878 return null;
6879 } finally {
6880 Binder.restoreCallingIdentity(identity);
Derek Tan7226c842014-07-02 17:42:23 -07006881 }
Derek Tan7226c842014-07-02 17:42:23 -07006882 }
Shishir Agrawalb1ebf8c2014-07-17 16:32:41 -07006883
6884 @Override
Philip P. Moltmann700a9592019-10-03 11:53:50 -07006885 public String[] getMergedSubscriberIds(int subId, String callingPackage,
6886 String callingFeatureId) {
Jeff Davidson913390f2018-02-23 17:11:49 -08006887 // This API isn't public, so no need to provide a valid subscription ID - we're not worried
6888 // about carrier-privileged callers not having access.
Jeff Davidson7e17e312018-02-13 18:17:36 -08006889 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
Jeff Davidson913390f2018-02-23 17:11:49 -08006890 mApp, SubscriptionManager.INVALID_SUBSCRIPTION_ID, callingPackage,
Philip P. Moltmann700a9592019-10-03 11:53:50 -07006891 callingFeatureId, "getMergedSubscriberIds")) {
Fyodor Kupolov8e53b0b2015-06-17 13:17:50 -07006892 return null;
6893 }
Jeff Sharkey85190e62014-12-05 09:40:12 -08006894
Jordan Liub49b04b2019-05-06 14:45:15 -07006895 // Clear calling identity, when calling TelephonyManager, because callerUid must be
6896 // the process, where TelephonyManager was instantiated.
6897 // Otherwise AppOps check will fail.
Fyodor Kupolov8e53b0b2015-06-17 13:17:50 -07006898 final long identity = Binder.clearCallingIdentity();
6899 try {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08006900 final Context context = mApp;
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006901 final TelephonyManager tele = TelephonyManager.from(context);
6902 final SubscriptionManager sub = SubscriptionManager.from(context);
6903
6904 // Figure out what subscribers are currently active
6905 final ArraySet<String> activeSubscriberIds = new ArraySet<>();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006906
Jordan Liub49b04b2019-05-06 14:45:15 -07006907 // Only consider subs which match the current subId
6908 // This logic can be simplified. See b/131189269 for progress.
6909 if (isActiveSubscription(subId)) {
Fyodor Kupolov8e53b0b2015-06-17 13:17:50 -07006910 activeSubscriberIds.add(tele.getSubscriberId(subId));
6911 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006912
6913 // First pass, find a number override for an active subscriber
6914 String mergeNumber = null;
6915 final Map<String, ?> prefs = mTelephonySharedPreferences.getAll();
6916 for (String key : prefs.keySet()) {
6917 if (key.startsWith(PREF_CARRIERS_SUBSCRIBER_PREFIX)) {
6918 final String subscriberId = (String) prefs.get(key);
6919 if (activeSubscriberIds.contains(subscriberId)) {
6920 final String iccId = key.substring(
6921 PREF_CARRIERS_SUBSCRIBER_PREFIX.length());
6922 final String numberKey = PREF_CARRIERS_NUMBER_PREFIX + iccId;
6923 mergeNumber = (String) prefs.get(numberKey);
6924 if (DBG_MERGE) {
Amit Mahajanb8f13202020-01-27 18:16:07 -08006925 Rlog.d(LOG_TAG, "Found line number " + mergeNumber
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006926 + " for active subscriber " + subscriberId);
6927 }
6928 if (!TextUtils.isEmpty(mergeNumber)) {
6929 break;
6930 }
6931 }
6932 }
6933 }
6934
6935 // Shortcut when no active merged subscribers
6936 if (TextUtils.isEmpty(mergeNumber)) {
6937 return null;
6938 }
6939
6940 // Second pass, find all subscribers under that line override
6941 final ArraySet<String> result = new ArraySet<>();
6942 for (String key : prefs.keySet()) {
6943 if (key.startsWith(PREF_CARRIERS_NUMBER_PREFIX)) {
6944 final String number = (String) prefs.get(key);
6945 if (mergeNumber.equals(number)) {
6946 final String iccId = key.substring(PREF_CARRIERS_NUMBER_PREFIX.length());
6947 final String subscriberKey = PREF_CARRIERS_SUBSCRIBER_PREFIX + iccId;
6948 final String subscriberId = (String) prefs.get(subscriberKey);
6949 if (!TextUtils.isEmpty(subscriberId)) {
6950 result.add(subscriberId);
6951 }
6952 }
6953 }
6954 }
6955
6956 final String[] resultArray = result.toArray(new String[result.size()]);
6957 Arrays.sort(resultArray);
6958 if (DBG_MERGE) {
Amit Mahajanb8f13202020-01-27 18:16:07 -08006959 Rlog.d(LOG_TAG,
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006960 "Found subscribers " + Arrays.toString(resultArray) + " after merge");
6961 }
6962 return resultArray;
Fyodor Kupolov8e53b0b2015-06-17 13:17:50 -07006963 } finally {
6964 Binder.restoreCallingIdentity(identity);
Jeff Sharkey85190e62014-12-05 09:40:12 -08006965 }
Jeff Sharkey85190e62014-12-05 09:40:12 -08006966 }
6967
6968 @Override
zoey chen38003472019-12-13 17:16:31 +08006969 public String[] getMergedImsisFromGroup(int subId, String callingPackage) {
6970 enforceReadPrivilegedPermission("getMergedImsisFromGroup");
Malcolm Chen6ca97372019-07-01 16:28:21 -07006971
6972 final long identity = Binder.clearCallingIdentity();
6973 try {
6974 final TelephonyManager telephonyManager = mApp.getSystemService(
6975 TelephonyManager.class);
6976 String subscriberId = telephonyManager.getSubscriberId(subId);
6977 if (subscriberId == null) {
6978 if (DBG) {
zoey chen38003472019-12-13 17:16:31 +08006979 log("getMergedImsisFromGroup can't find subscriberId for subId "
Malcolm Chen6ca97372019-07-01 16:28:21 -07006980 + subId);
6981 }
6982 return null;
6983 }
6984
6985 final SubscriptionInfo info = SubscriptionController.getInstance()
6986 .getSubscriptionInfo(subId);
6987 final ParcelUuid groupUuid = info.getGroupUuid();
6988 // If it doesn't belong to any group, return just subscriberId of itself.
6989 if (groupUuid == null) {
6990 return new String[]{subscriberId};
6991 }
6992
6993 // Get all subscriberIds from the group.
6994 final List<String> mergedSubscriberIds = new ArrayList<>();
6995 final List<SubscriptionInfo> groupInfos = SubscriptionController.getInstance()
Philip P. Moltmann700a9592019-10-03 11:53:50 -07006996 .getSubscriptionsInGroup(groupUuid, mApp.getOpPackageName(),
Philip P. Moltmann8d34f0c2020-03-05 16:24:02 -08006997 mApp.getAttributionTag());
Malcolm Chen6ca97372019-07-01 16:28:21 -07006998 for (SubscriptionInfo subInfo : groupInfos) {
6999 subscriberId = telephonyManager.getSubscriberId(subInfo.getSubscriptionId());
7000 if (subscriberId != null) {
7001 mergedSubscriberIds.add(subscriberId);
7002 }
7003 }
7004
7005 return mergedSubscriberIds.toArray(new String[mergedSubscriberIds.size()]);
7006 } finally {
7007 Binder.restoreCallingIdentity(identity);
7008
7009 }
7010 }
7011
7012 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08007013 public boolean setOperatorBrandOverride(int subId, String brand) {
Shuo Qian2c0ae432019-12-05 11:40:37 -08007014 TelephonyPermissions.enforceCallingOrSelfCarrierPrivilege(mApp,
Jeff Davidson7e17e312018-02-13 18:17:36 -08007015 subId, "setOperatorBrandOverride");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007016
7017 final long identity = Binder.clearCallingIdentity();
7018 try {
7019 final Phone phone = getPhone(subId);
7020 return phone == null ? false : phone.setOperatorBrandOverride(brand);
7021 } finally {
7022 Binder.restoreCallingIdentity(identity);
7023 }
Shishir Agrawalb1ebf8c2014-07-17 16:32:41 -07007024 }
Steven Liu4bf01bc2014-07-17 11:05:29 -05007025
7026 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08007027 public boolean setRoamingOverride(int subId, List<String> gsmRoamingList,
Shishir Agrawal621a47c2014-12-01 10:25:09 -08007028 List<String> gsmNonRoamingList, List<String> cdmaRoamingList,
7029 List<String> cdmaNonRoamingList) {
Shuo Qian2c0ae432019-12-05 11:40:37 -08007030 TelephonyPermissions.enforceCallingOrSelfCarrierPrivilege(
7031 mApp, subId, "setRoamingOverride");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007032
7033 final long identity = Binder.clearCallingIdentity();
7034 try {
7035 final Phone phone = getPhone(subId);
7036 if (phone == null) {
7037 return false;
7038 }
7039 return phone.setRoamingOverride(gsmRoamingList, gsmNonRoamingList, cdmaRoamingList,
7040 cdmaNonRoamingList);
7041 } finally {
7042 Binder.restoreCallingIdentity(identity);
Shishir Agrawalc04d9752016-02-19 10:41:00 -08007043 }
Shishir Agrawal621a47c2014-12-01 10:25:09 -08007044 }
7045
7046 @Override
Shuo Qian850e4d6a2018-04-25 21:02:08 +00007047 @Deprecated
7048 public int invokeOemRilRequestRaw(byte[] oemReq, byte[] oemResp) {
7049 enforceModifyPermission();
7050
7051 int returnValue = 0;
7052 try {
vagdeviaf9a5b92018-08-15 16:01:53 -07007053 AsyncResult result = (AsyncResult) sendRequest(CMD_INVOKE_OEM_RIL_REQUEST_RAW, oemReq);
Shuo Qian850e4d6a2018-04-25 21:02:08 +00007054 if(result.exception == null) {
7055 if (result.result != null) {
7056 byte[] responseData = (byte[])(result.result);
7057 if(responseData.length > oemResp.length) {
7058 Log.w(LOG_TAG, "Buffer to copy response too small: Response length is " +
7059 responseData.length + "bytes. Buffer Size is " +
7060 oemResp.length + "bytes.");
7061 }
7062 System.arraycopy(responseData, 0, oemResp, 0, responseData.length);
7063 returnValue = responseData.length;
7064 }
7065 } else {
7066 CommandException ex = (CommandException) result.exception;
7067 returnValue = ex.getCommandError().ordinal();
7068 if(returnValue > 0) returnValue *= -1;
7069 }
7070 } catch (RuntimeException e) {
7071 Log.w(LOG_TAG, "sendOemRilRequestRaw: Runtime Exception");
7072 returnValue = (CommandException.Error.GENERIC_FAILURE.ordinal());
7073 if(returnValue > 0) returnValue *= -1;
7074 }
7075
7076 return returnValue;
7077 }
7078
7079 @Override
Robert Greenwalt36b23af2015-07-06 17:59:14 -07007080 public int getRadioAccessFamily(int phoneId, String callingPackage) {
Jeff Davidson913390f2018-02-23 17:11:49 -08007081 Phone phone = PhoneFactory.getPhone(phoneId);
Shuo Qiandee53402020-05-29 14:08:15 -07007082 try {
7083 TelephonyPermissions
7084 .enforeceCallingOrSelfReadPrivilegedPhoneStatePermissionOrCarrierPrivilege(
7085 mApp, phone.getSubId(), "getRadioAccessFamily");
7086 } catch (SecurityException e) {
7087 EventLog.writeEvent(0x534e4554, "150857259", -1, "Missing Permission");
7088 throw e;
7089 }
chen xub97461a2018-10-26 14:17:57 -07007090 int raf = RadioAccessFamily.RAF_UNKNOWN;
Jeff Davidson913390f2018-02-23 17:11:49 -08007091 if (phone == null) {
chen xub97461a2018-10-26 14:17:57 -07007092 return raf;
Jeff Davidson913390f2018-02-23 17:11:49 -08007093 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007094 final long identity = Binder.clearCallingIdentity();
7095 try {
chen xub97461a2018-10-26 14:17:57 -07007096 TelephonyPermissions
7097 .enforeceCallingOrSelfReadPrivilegedPhoneStatePermissionOrCarrierPrivilege(
7098 mApp, phone.getSubId(), "getRadioAccessFamily");
7099 raf = ProxyController.getInstance().getRadioAccessFamily(phoneId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007100 } finally {
7101 Binder.restoreCallingIdentity(identity);
7102 }
chen xub97461a2018-10-26 14:17:57 -07007103 return raf;
Wink Saville5d475dd2014-10-17 15:00:58 -07007104 }
Andrew Leedf14ead2014-10-17 14:22:52 -07007105
7106 @Override
Hall Liu82694d52020-12-11 18:22:04 -08007107 public void uploadCallComposerPicture(int subscriptionId, String callingPackage,
Hall Liue31bac62020-12-23 19:16:10 -08007108 String contentType, ParcelFileDescriptor fd, ResultReceiver callback) {
Hall Liu82694d52020-12-11 18:22:04 -08007109 try {
7110 if (!Objects.equals(mApp.getPackageManager().getPackageUid(callingPackage, 0),
7111 Binder.getCallingUid())) {
7112 throw new SecurityException("Package uid and package name do not match: "
7113 + "uid=" + Binder.getCallingUid() + ", packageName=" + callingPackage);
7114 }
7115 } catch (PackageManager.NameNotFoundException e) {
7116 throw new SecurityException("Package name invalid:" + callingPackage);
7117 }
7118 RoleManager rm = mApp.getSystemService(RoleManager.class);
7119 List<String> dialerRoleHolders = rm.getRoleHolders(RoleManager.ROLE_DIALER);
7120 if (!dialerRoleHolders.contains(callingPackage)) {
7121 throw new SecurityException("App must be the dialer role holder to"
7122 + " upload a call composer pic");
7123 }
7124
7125 Executors.newSingleThreadExecutor().execute(() -> {
7126 ByteArrayOutputStream output = new ByteArrayOutputStream(
7127 (int) TelephonyManager.getMaximumCallComposerPictureSize());
7128 InputStream input = new ParcelFileDescriptor.AutoCloseInputStream(fd);
7129 boolean readUntilEnd = false;
7130 int totalBytesRead = 0;
7131 byte[] buffer = new byte[16 * 1024];
7132 while (true) {
7133 int numRead;
7134 try {
7135 numRead = input.read(buffer);
7136 } catch (IOException e) {
7137 try {
7138 fd.checkError();
7139 callback.send(TelephonyManager.CallComposerException.ERROR_INPUT_CLOSED,
7140 null);
7141 } catch (IOException e1) {
7142 // This means that the other side closed explicitly with an error. If this
7143 // happens, log and ignore.
7144 loge("Remote end of call composer picture pipe closed: " + e1);
7145 }
7146 break;
7147 }
7148 if (numRead == -1) {
7149 readUntilEnd = true;
7150 break;
7151 }
7152 totalBytesRead += numRead;
7153 if (totalBytesRead > TelephonyManager.getMaximumCallComposerPictureSize()) {
7154 loge("Too many bytes read for call composer picture: " + totalBytesRead);
7155 try {
7156 input.close();
7157 } catch (IOException e) {
7158 // ignore
7159 }
7160 break;
7161 }
7162 output.write(buffer, 0, numRead);
7163 }
7164 // Generally, the remote end will close the file descriptors. The only case where we
7165 // close is above, where the picture size is too big.
7166
7167 try {
7168 fd.checkError();
7169 } catch (IOException e) {
7170 loge("Remote end for call composer closed with an error: " + e);
7171 return;
7172 }
7173
Hall Liuaa4211e2021-01-20 15:43:39 -08007174 if (!readUntilEnd) {
7175 loge("Did not finish reading entire image; aborting");
7176 return;
7177 }
Hall Liu82694d52020-12-11 18:22:04 -08007178
Hall Liuaa4211e2021-01-20 15:43:39 -08007179 ImageData imageData = new ImageData(output.toByteArray(), contentType, null);
7180 CallComposerPictureManager.getInstance(mApp, subscriptionId).handleUploadToServer(
7181 new CallComposerPictureTransfer.Factory() {},
7182 imageData,
7183 (result) -> {
7184 if (result.first != null) {
7185 ParcelUuid parcelUuid = new ParcelUuid(result.first);
7186 Bundle outputResult = new Bundle();
7187 outputResult.putParcelable(
7188 TelephonyManager.KEY_CALL_COMPOSER_PICTURE_HANDLE, parcelUuid);
7189 callback.send(TelephonyManager.CallComposerException.SUCCESS,
7190 outputResult);
7191 } else {
7192 callback.send(result.second, null);
7193 }
7194 }
7195 );
Hall Liu82694d52020-12-11 18:22:04 -08007196 });
7197 }
7198
7199 @Override
Andrew Leedf14ead2014-10-17 14:22:52 -07007200 public void enableVideoCalling(boolean enable) {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08007201 final Phone defaultPhone = getDefaultPhone();
Andrew Leedf14ead2014-10-17 14:22:52 -07007202 enforceModifyPermission();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007203
7204 final long identity = Binder.clearCallingIdentity();
7205 try {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08007206 ImsManager.getInstance(defaultPhone.getContext(),
7207 defaultPhone.getPhoneId()).setVtSetting(enable);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007208 } finally {
7209 Binder.restoreCallingIdentity(identity);
7210 }
Andrew Leedf14ead2014-10-17 14:22:52 -07007211 }
7212
7213 @Override
Philip P. Moltmann700a9592019-10-03 11:53:50 -07007214 public boolean isVideoCallingEnabled(String callingPackage, String callingFeatureId) {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08007215 final Phone defaultPhone = getDefaultPhone();
Philip P. Moltmann700a9592019-10-03 11:53:50 -07007216 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(mApp, defaultPhone.getSubId(),
7217 callingPackage, callingFeatureId, "isVideoCallingEnabled")) {
Amit Mahajan578e53d2018-03-20 16:18:38 +00007218 return false;
7219 }
Svet Ganovb320e182015-04-16 12:30:10 -07007220
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007221 final long identity = Binder.clearCallingIdentity();
7222 try {
7223 // Check the user preference and the system-level IMS setting. Even if the user has
7224 // enabled video calling, if IMS is disabled we aren't able to support video calling.
7225 // In the long run, we may instead need to check if there exists a connection service
7226 // which can support video calling.
7227 ImsManager imsManager =
Nazanin Bakhshif782e562018-12-11 15:15:39 -08007228 ImsManager.getInstance(defaultPhone.getContext(), defaultPhone.getPhoneId());
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007229 return imsManager.isVtEnabledByPlatform()
7230 && imsManager.isEnhanced4gLteModeSettingEnabledByUser()
7231 && imsManager.isVtEnabledByUser();
7232 } finally {
7233 Binder.restoreCallingIdentity(identity);
7234 }
Andrew Leedf14ead2014-10-17 14:22:52 -07007235 }
Libin.Tang@motorola.comafe82642014-12-18 13:27:53 -06007236
Andrew Leea1239f22015-03-02 17:44:07 -08007237 @Override
Philip P. Moltmann700a9592019-10-03 11:53:50 -07007238 public boolean canChangeDtmfToneLength(int subId, String callingPackage,
7239 String callingFeatureId) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007240 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
Philip P. Moltmann700a9592019-10-03 11:53:50 -07007241 mApp, subId, callingPackage, callingFeatureId,
7242 "isVideoCallingEnabled")) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007243 return false;
7244 }
7245
7246 final long identity = Binder.clearCallingIdentity();
7247 try {
7248 CarrierConfigManager configManager =
7249 (CarrierConfigManager) mApp.getSystemService(Context.CARRIER_CONFIG_SERVICE);
Nazanin Bakhshif782e562018-12-11 15:15:39 -08007250 return configManager.getConfigForSubId(subId)
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007251 .getBoolean(CarrierConfigManager.KEY_DTMF_TYPE_ENABLED_BOOL);
7252 } finally {
7253 Binder.restoreCallingIdentity(identity);
7254 }
Andrew Leea1239f22015-03-02 17:44:07 -08007255 }
7256
7257 @Override
Philip P. Moltmann700a9592019-10-03 11:53:50 -07007258 public boolean isWorldPhone(int subId, String callingPackage, String callingFeatureId) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007259 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
Philip P. Moltmann700a9592019-10-03 11:53:50 -07007260 mApp, subId, callingPackage, callingFeatureId, "isVideoCallingEnabled")) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007261 return false;
7262 }
7263
7264 final long identity = Binder.clearCallingIdentity();
7265 try {
7266 CarrierConfigManager configManager =
7267 (CarrierConfigManager) mApp.getSystemService(Context.CARRIER_CONFIG_SERVICE);
Nazanin Bakhshif782e562018-12-11 15:15:39 -08007268 return configManager.getConfigForSubId(subId)
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007269 .getBoolean(CarrierConfigManager.KEY_WORLD_PHONE_BOOL);
7270 } finally {
7271 Binder.restoreCallingIdentity(identity);
7272 }
Andrew Leea1239f22015-03-02 17:44:07 -08007273 }
7274
Andrew Lee9431b832015-03-09 18:46:45 -07007275 @Override
7276 public boolean isTtyModeSupported() {
Tyler Gunn5ddfdc92019-10-31 13:08:23 -07007277 TelecomManager telecomManager = mApp.getSystemService(TelecomManager.class);
Wooki Wu1f82f7a2016-02-15 15:59:58 +08007278 return telecomManager.isTtySupported();
Andrew Lee9431b832015-03-09 18:46:45 -07007279 }
7280
7281 @Override
7282 public boolean isHearingAidCompatibilitySupported() {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007283 final long identity = Binder.clearCallingIdentity();
7284 try {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08007285 return mApp.getResources().getBoolean(R.bool.hac_enabled);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007286 } finally {
7287 Binder.restoreCallingIdentity(identity);
7288 }
Andrew Lee9431b832015-03-09 18:46:45 -07007289 }
7290
Hall Liuf6668912018-10-31 17:05:23 -07007291 /**
7292 * Determines whether the device currently supports RTT (Real-time text). Based both on carrier
7293 * support for the feature and device firmware support.
7294 *
7295 * @return {@code true} if the device and carrier both support RTT, {@code false} otherwise.
7296 */
7297 @Override
7298 public boolean isRttSupported(int subscriptionId) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007299 final long identity = Binder.clearCallingIdentity();
Nazanin Bakhshif782e562018-12-11 15:15:39 -08007300 final Phone phone = getPhone(subscriptionId);
7301 if (phone == null) {
7302 loge("isRttSupported: no Phone found. Invalid subId:" + subscriptionId);
7303 return false;
7304 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007305 try {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08007306 boolean isCarrierSupported = mApp.getCarrierConfigForSubId(subscriptionId).getBoolean(
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007307 CarrierConfigManager.KEY_RTT_SUPPORTED_BOOL);
7308 boolean isDeviceSupported =
Nazanin Bakhshif782e562018-12-11 15:15:39 -08007309 phone.getContext().getResources().getBoolean(R.bool.config_support_rtt);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007310 return isCarrierSupported && isDeviceSupported;
7311 } finally {
7312 Binder.restoreCallingIdentity(identity);
7313 }
Hall Liu98187582018-01-22 19:15:32 -08007314 }
7315
Hall Liuf6668912018-10-31 17:05:23 -07007316 /**
Hall Liuf2daa022019-07-23 18:39:00 -07007317 * Determines whether the user has turned on RTT. If the carrier wants to ignore the user-set
7318 * RTT setting, will return true if the device and carrier both support RTT.
7319 * Otherwise. only returns true if the device and carrier both also support RTT.
Hall Liuf6668912018-10-31 17:05:23 -07007320 */
7321 public boolean isRttEnabled(int subscriptionId) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007322 final long identity = Binder.clearCallingIdentity();
7323 try {
Hall Liu5bab75c2019-12-11 23:58:20 +00007324 boolean isRttSupported = isRttSupported(subscriptionId);
7325 boolean isUserRttSettingOn = Settings.Secure.getInt(
7326 mApp.getContentResolver(), Settings.Secure.RTT_CALLING_MODE, 0) != 0;
7327 boolean shouldIgnoreUserRttSetting = mApp.getCarrierConfigForSubId(subscriptionId)
7328 .getBoolean(CarrierConfigManager.KEY_IGNORE_RTT_MODE_SETTING_BOOL);
7329 return isRttSupported && (isUserRttSettingOn || shouldIgnoreUserRttSetting);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007330 } finally {
7331 Binder.restoreCallingIdentity(identity);
7332 }
Hall Liu3ad5f012018-04-06 16:23:39 -07007333 }
7334
Philip P. Moltmann700a9592019-10-03 11:53:50 -07007335 @Deprecated
7336 @Override
7337 public String getDeviceId(String callingPackage) {
7338 return getDeviceIdWithFeature(callingPackage, null);
7339 }
7340
Sanket Padawe7310cc72015-01-14 09:53:20 -08007341 /**
7342 * Returns the unique device ID of phone, for example, the IMEI for
7343 * GSM and the MEID for CDMA phones. Return null if device ID is not available.
7344 *
7345 * <p>Requires Permission:
7346 * {@link android.Manifest.permission#READ_PHONE_STATE READ_PHONE_STATE}
7347 */
7348 @Override
Philip P. Moltmann700a9592019-10-03 11:53:50 -07007349 public String getDeviceIdWithFeature(String callingPackage, String callingFeatureId) {
Sanket Padawe7310cc72015-01-14 09:53:20 -08007350 final Phone phone = PhoneFactory.getPhone(0);
Jeff Davidson913390f2018-02-23 17:11:49 -08007351 if (phone == null) {
Sanket Padawe7310cc72015-01-14 09:53:20 -08007352 return null;
7353 }
Jeff Davidson913390f2018-02-23 17:11:49 -08007354 int subId = phone.getSubId();
Michael Groover70af6dc2018-10-01 16:23:15 -07007355 if (!TelephonyPermissions.checkCallingOrSelfReadDeviceIdentifiers(mApp, subId,
Philip P. Moltmann700a9592019-10-03 11:53:50 -07007356 callingPackage, callingFeatureId, "getDeviceId")) {
Jeff Davidson913390f2018-02-23 17:11:49 -08007357 return null;
7358 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007359
7360 final long identity = Binder.clearCallingIdentity();
7361 try {
7362 return phone.getDeviceId();
7363 } finally {
7364 Binder.restoreCallingIdentity(identity);
7365 }
Sanket Padawe7310cc72015-01-14 09:53:20 -08007366 }
7367
Ping Sunc67b7c22016-03-02 19:16:45 +08007368 /**
7369 * {@hide}
7370 * Returns the IMS Registration Status on a particular subid
7371 *
7372 * @param subId
7373 */
Brad Ebinger1f2b5082018-02-08 16:11:32 -08007374 public boolean isImsRegistered(int subId) {
Ping Sunc67b7c22016-03-02 19:16:45 +08007375 Phone phone = getPhone(subId);
7376 if (phone != null) {
7377 return phone.isImsRegistered();
7378 } else {
7379 return false;
7380 }
7381 }
7382
Santos Cordon7a1885b2015-02-03 11:15:19 -08007383 @Override
7384 public int getSubIdForPhoneAccount(PhoneAccount phoneAccount) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007385 final long identity = Binder.clearCallingIdentity();
7386 try {
7387 return PhoneUtils.getSubIdForPhoneAccount(phoneAccount);
7388 } finally {
7389 Binder.restoreCallingIdentity(identity);
7390 }
Santos Cordon7a1885b2015-02-03 11:15:19 -08007391 }
Nathan Harolddcfc7932015-03-18 10:01:20 -07007392
Tyler Gunnf70ed162019-04-03 15:28:53 -07007393 @Override
Shuo Qian6e6137d2019-10-30 16:33:31 -07007394 public int getSubIdForPhoneAccountHandle(
Philip P. Moltmann700a9592019-10-03 11:53:50 -07007395 PhoneAccountHandle phoneAccountHandle, String callingPackage, String callingFeatureId) {
Shuo Qian6e6137d2019-10-30 16:33:31 -07007396 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(mApp, getDefaultSubscription(),
Philip P. Moltmann700a9592019-10-03 11:53:50 -07007397 callingPackage, callingFeatureId, "getSubIdForPhoneAccountHandle")) {
Shuo Qian6e6137d2019-10-30 16:33:31 -07007398 throw new SecurityException("Requires READ_PHONE_STATE permission.");
7399 }
7400 final long identity = Binder.clearCallingIdentity();
7401 try {
7402 return PhoneUtils.getSubIdForPhoneAccountHandle(phoneAccountHandle);
7403 } finally {
7404 Binder.restoreCallingIdentity(identity);
7405 }
7406 }
7407
7408 @Override
Tyler Gunnf70ed162019-04-03 15:28:53 -07007409 public @Nullable PhoneAccountHandle getPhoneAccountHandleForSubscriptionId(int subscriptionId) {
Alireza Forouzan4ac4f982021-03-16 22:18:52 -07007410 TelephonyPermissions
7411 .enforeceCallingOrSelfReadPrivilegedPhoneStatePermissionOrCarrierPrivilege(
7412 mApp,
7413 subscriptionId,
7414 "getPhoneAccountHandleForSubscriptionId, " + "subscriptionId: " + subscriptionId);
Tyler Gunnf70ed162019-04-03 15:28:53 -07007415 final long identity = Binder.clearCallingIdentity();
7416 try {
7417 Phone phone = getPhone(subscriptionId);
7418 if (phone == null) {
7419 return null;
7420 }
7421 return PhoneUtils.makePstnPhoneAccountHandle(phone);
7422 } finally {
7423 Binder.restoreCallingIdentity(identity);
7424 }
7425 }
7426
Brad Ebinger1f2b5082018-02-08 16:11:32 -08007427 /**
7428 * @return the VoWiFi calling availability.
Nathan Haroldc55097a2015-03-11 18:14:50 -07007429 */
Brad Ebinger1f2b5082018-02-08 16:11:32 -08007430 public boolean isWifiCallingAvailable(int subId) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007431 final long identity = Binder.clearCallingIdentity();
7432 try {
7433 Phone phone = getPhone(subId);
7434 if (phone != null) {
7435 return phone.isWifiCallingEnabled();
7436 } else {
7437 return false;
7438 }
7439 } finally {
7440 Binder.restoreCallingIdentity(identity);
Brad Ebinger1f2b5082018-02-08 16:11:32 -08007441 }
Nathan Haroldc55097a2015-03-11 18:14:50 -07007442 }
7443
Brad Ebinger1f2b5082018-02-08 16:11:32 -08007444 /**
Brad Ebinger1f2b5082018-02-08 16:11:32 -08007445 * @return the VT calling availability.
Etan Cohen3b7a1bc2015-05-28 15:57:13 -07007446 */
Brad Ebinger1f2b5082018-02-08 16:11:32 -08007447 public boolean isVideoTelephonyAvailable(int subId) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007448 final long identity = Binder.clearCallingIdentity();
7449 try {
7450 Phone phone = getPhone(subId);
7451 if (phone != null) {
7452 return phone.isVideoEnabled();
7453 } else {
7454 return false;
7455 }
7456 } finally {
7457 Binder.restoreCallingIdentity(identity);
Brad Ebinger1f2b5082018-02-08 16:11:32 -08007458 }
7459 }
7460
7461 /**
7462 * @return the IMS registration technology for the MMTEL feature. Valid return values are
7463 * defined in {@link ImsRegistrationImplBase}.
7464 */
7465 public @ImsRegistrationImplBase.ImsRegistrationTech int getImsRegTechnologyForMmTel(int subId) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007466 final long identity = Binder.clearCallingIdentity();
7467 try {
7468 Phone phone = getPhone(subId);
7469 if (phone != null) {
7470 return phone.getImsRegistrationTech();
7471 } else {
7472 return ImsRegistrationImplBase.REGISTRATION_TECH_NONE;
7473 }
7474 } finally {
7475 Binder.restoreCallingIdentity(identity);
Brad Ebinger1f2b5082018-02-08 16:11:32 -08007476 }
Etan Cohen3b7a1bc2015-05-28 15:57:13 -07007477 }
7478
Stuart Scott8eef64f2015-04-08 15:13:54 -07007479 @Override
7480 public void factoryReset(int subId) {
paulhu5a773602019-08-23 19:17:33 +08007481 enforceSettingsPermission();
Stuart Scott981d8582015-04-21 14:09:50 -07007482 if (mUserManager.hasUserRestriction(UserManager.DISALLOW_NETWORK_RESET)) {
7483 return;
7484 }
Kai Shif70f46f2021-03-03 13:59:46 -08007485 Phone defaultPhone = getDefaultPhone();
7486 if (defaultPhone != null) {
7487 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
7488 mApp, getDefaultPhone().getSubId(), "factoryReset");
7489 }
Svet Ganovcc087f82015-05-12 20:35:54 -07007490 final long identity = Binder.clearCallingIdentity();
Nazanin Bakhshif782e562018-12-11 15:15:39 -08007491
Svet Ganovcc087f82015-05-12 20:35:54 -07007492 try {
Stuart Scott981d8582015-04-21 14:09:50 -07007493 if (SubscriptionManager.isUsableSubIdValue(subId) && !mUserManager.hasUserRestriction(
7494 UserManager.DISALLOW_CONFIG_MOBILE_NETWORKS)) {
Sooraj Sasindran95c07a92020-07-15 01:35:24 -07007495 setDataEnabledForReason(subId, TelephonyManager.DATA_ENABLED_REASON_USER,
Sooraj Sasindranaf1b5132020-05-05 21:06:20 +00007496 getDefaultDataEnabled());
Svet Ganovcc087f82015-05-12 20:35:54 -07007497 setNetworkSelectionModeAutomatic(subId);
SongFerngWang8c6e82e2021-03-02 22:09:29 +08007498 Phone phone = getPhone(subId);
7499 if (phone != null) {
7500 SubscriptionManager.setSubscriptionProperty(subId,
7501 SubscriptionManager.ALLOWED_NETWORK_TYPES,
7502 "user=" + RadioAccessFamily.getRafFromNetworkType(
7503 RILConstants.PREFERRED_NETWORK_MODE));
7504 phone.loadAllowedNetworksFromSubscriptionDatabase();
7505 }
Nazanin Bakhshif782e562018-12-11 15:15:39 -08007506 setDataRoamingEnabled(subId, getDefaultDataRoamingEnabled(subId));
Jordan Liu857e73a2021-03-05 16:24:04 -08007507 getPhone(subId).resetCarrierKeysForImsiEncryption();
Svet Ganovcc087f82015-05-12 20:35:54 -07007508 }
Amit Mahajan7dbbd822019-03-13 17:33:47 -07007509 // There has been issues when Sms raw table somehow stores orphan
7510 // fragments. They lead to garbled message when new fragments come
7511 // in and combined with those stale ones. In case this happens again,
7512 // user can reset all network settings which will clean up this table.
7513 cleanUpSmsRawTable(getDefaultPhone().getContext());
Brad Ebingerbc7dd582019-10-17 17:03:22 -07007514 // Clean up IMS settings as well here.
7515 int slotId = getSlotIndex(subId);
7516 if (slotId > SubscriptionManager.INVALID_SIM_SLOT_INDEX) {
7517 ImsManager.getInstance(mApp, slotId).factoryReset();
7518 }
Naina Nallurid63128d2019-09-17 14:10:30 -07007519
Kai Shif70f46f2021-03-03 13:59:46 -08007520 if (defaultPhone == null) {
7521 return;
7522 }
Naina Nallurid63128d2019-09-17 14:10:30 -07007523 // Erase modem config if erase modem on network setting is enabled.
7524 String configValue = DeviceConfig.getProperty(DeviceConfig.NAMESPACE_TELEPHONY,
7525 RESET_NETWORK_ERASE_MODEM_CONFIG_ENABLED);
7526 if (configValue != null && Boolean.parseBoolean(configValue)) {
Kai Shif70f46f2021-03-03 13:59:46 -08007527 sendEraseModemConfig(defaultPhone);
Naina Nallurid63128d2019-09-17 14:10:30 -07007528 }
Kai Shif70f46f2021-03-03 13:59:46 -08007529
7530 sendEraseDataInSharedPreferences(defaultPhone);
Svet Ganovcc087f82015-05-12 20:35:54 -07007531 } finally {
7532 Binder.restoreCallingIdentity(identity);
Stuart Scott8eef64f2015-04-08 15:13:54 -07007533 }
7534 }
Narayan Kamath1c496c22015-04-16 14:40:19 +01007535
Amit Mahajan7dbbd822019-03-13 17:33:47 -07007536 private void cleanUpSmsRawTable(Context context) {
7537 ContentResolver resolver = context.getContentResolver();
7538 Uri uri = Uri.withAppendedPath(Telephony.Sms.CONTENT_URI, "raw/permanentDelete");
7539 resolver.delete(uri, null, null);
7540 }
7541
Narayan Kamath1c496c22015-04-16 14:40:19 +01007542 @Override
chen xu5d3637b2019-01-21 23:31:38 -08007543 public String getSimLocaleForSubscriber(int subId) {
7544 enforceReadPrivilegedPermission("getSimLocaleForSubscriber, subId: " + subId);
7545 final Phone phone = getPhone(subId);
7546 if (phone == null) {
7547 log("getSimLocaleForSubscriber, invalid subId");
chen xu2bb91e42019-01-24 14:35:54 -08007548 return null;
chen xu5d3637b2019-01-21 23:31:38 -08007549 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007550 final long identity = Binder.clearCallingIdentity();
7551 try {
chen xu5d3637b2019-01-21 23:31:38 -08007552 final SubscriptionInfo info = mSubscriptionController.getActiveSubscriptionInfo(subId,
Philip P. Moltmann8d34f0c2020-03-05 16:24:02 -08007553 phone.getContext().getOpPackageName(), phone.getContext().getAttributionTag());
chen xu6291c472019-02-04 12:55:53 -08007554 if (info == null) {
7555 log("getSimLocaleForSubscriber, inactive subId: " + subId);
7556 return null;
7557 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007558 // Try and fetch the locale from the carrier properties or from the SIM language
7559 // preferences (EF-PL and EF-LI)...
7560 final int mcc = info.getMcc();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007561 String simLanguage = null;
chen xu5d3637b2019-01-21 23:31:38 -08007562 final Locale localeFromDefaultSim = phone.getLocaleFromSimAndCarrierPrefs();
7563 if (localeFromDefaultSim != null) {
7564 if (!localeFromDefaultSim.getCountry().isEmpty()) {
7565 if (DBG) log("Using locale from subId: " + subId + " locale: "
7566 + localeFromDefaultSim);
7567 return localeFromDefaultSim.toLanguageTag();
7568 } else {
7569 simLanguage = localeFromDefaultSim.getLanguage();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007570 }
7571 }
Narayan Kamath1c496c22015-04-16 14:40:19 +01007572
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007573 // The SIM language preferences only store a language (e.g. fr = French), not an
7574 // exact locale (e.g. fr_FR = French/France). So, if the locale returned from
7575 // the SIM and carrier preferences does not include a country we add the country
7576 // determined from the SIM MCC to provide an exact locale.
zoey chenc730df82019-12-18 17:07:20 +08007577 final Locale mccLocale = LocaleUtils.getLocaleFromMcc(mApp, mcc, simLanguage);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007578 if (mccLocale != null) {
chen xu5d3637b2019-01-21 23:31:38 -08007579 if (DBG) log("No locale from SIM, using mcc locale:" + mccLocale);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007580 return mccLocale.toLanguageTag();
7581 }
7582
7583 if (DBG) log("No locale found - returning null");
7584 return null;
7585 } finally {
7586 Binder.restoreCallingIdentity(identity);
7587 }
Narayan Kamath1c496c22015-04-16 14:40:19 +01007588 }
7589
7590 private List<SubscriptionInfo> getAllSubscriptionInfoList() {
Philip P. Moltmann700a9592019-10-03 11:53:50 -07007591 return mSubscriptionController.getAllSubInfoList(mApp.getOpPackageName(),
Philip P. Moltmann8d34f0c2020-03-05 16:24:02 -08007592 mApp.getAttributionTag());
Narayan Kamath1c496c22015-04-16 14:40:19 +01007593 }
7594
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007595 /**
7596 * NOTE: this method assumes permission checks are done and caller identity has been cleared.
7597 */
7598 private List<SubscriptionInfo> getActiveSubscriptionInfoListPrivileged() {
Philip P. Moltmann700a9592019-10-03 11:53:50 -07007599 return mSubscriptionController.getActiveSubscriptionInfoList(mApp.getOpPackageName(),
Philip P. Moltmann8d34f0c2020-03-05 16:24:02 -08007600 mApp.getAttributionTag());
Narayan Kamath1c496c22015-04-16 14:40:19 +01007601 }
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -07007602
Chenjie Yu1ba97252018-01-11 18:16:20 -08007603 private final ModemActivityInfo mLastModemActivityInfo =
Hall Liu49656c02020-10-09 19:00:11 -07007604 new ModemActivityInfo(0, 0, 0, new int[ModemActivityInfo.getNumTxPowerLevels()], 0);
Chenjie Yu1ba97252018-01-11 18:16:20 -08007605
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -07007606 /**
Adam Lesinski903a54c2016-04-11 14:49:52 -07007607 * Responds to the ResultReceiver with the {@link android.telephony.ModemActivityInfo} object
7608 * representing the state of the modem.
7609 *
Chenjie Yu1ba97252018-01-11 18:16:20 -08007610 * NOTE: The underlying implementation clears the modem state, so there should only ever be one
7611 * caller to it. Everyone should call this class to get cumulative data.
Adam Lesinski903a54c2016-04-11 14:49:52 -07007612 * @hide
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -07007613 */
7614 @Override
Adam Lesinski903a54c2016-04-11 14:49:52 -07007615 public void requestModemActivityInfo(ResultReceiver result) {
7616 enforceModifyPermission();
vagdeviaf9a5b92018-08-15 16:01:53 -07007617 WorkSource workSource = getWorkSource(Binder.getCallingUid());
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007618
7619 final long identity = Binder.clearCallingIdentity();
7620 try {
Shuo Qian8f4750a2020-02-20 17:12:10 -08007621 sendRequestAsync(CMD_GET_MODEM_ACTIVITY_INFO, result, null, workSource);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007622 } finally {
7623 Binder.restoreCallingIdentity(identity);
Chenjie Yu1ba97252018-01-11 18:16:20 -08007624 }
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -07007625 }
Jack Yu85bd38a2015-11-09 11:34:32 -08007626
Siddharth Rayb8114062018-06-17 15:02:38 -07007627 // Checks that ModemActivityInfo is valid. Sleep time, Idle time, Rx time and Tx time should be
7628 // less than total activity duration.
7629 private boolean isModemActivityInfoValid(ModemActivityInfo info) {
7630 if (info == null) {
7631 return false;
7632 }
7633 int activityDurationMs =
Hall Liu49656c02020-10-09 19:00:11 -07007634 (int) (info.getTimestampMillis() - mLastModemActivityInfo.getTimestampMillis());
7635 int totalTxTimeMs = Arrays.stream(info.getTransmitTimeMillis()).sum();
7636
Siddharth Rayb8114062018-06-17 15:02:38 -07007637 return (info.isValid()
7638 && (info.getSleepTimeMillis() <= activityDurationMs)
7639 && (info.getIdleTimeMillis() <= activityDurationMs)
Chen Xud78231e2019-09-10 18:49:52 -07007640 && (info.getReceiveTimeMillis() <= activityDurationMs)
Siddharth Rayb8114062018-06-17 15:02:38 -07007641 && (totalTxTimeMs <= activityDurationMs));
7642 }
7643
Jack Yu85bd38a2015-11-09 11:34:32 -08007644 /**
7645 * {@hide}
7646 * Returns the service state information on specified subscription.
7647 */
7648 @Override
Philip P. Moltmann3a2772a2019-10-04 08:15:00 -07007649 public ServiceState getServiceStateForSubscriber(int subId, String callingPackage,
7650 String callingFeatureId) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08007651 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
Philip P. Moltmann700a9592019-10-03 11:53:50 -07007652 mApp, subId, callingPackage, callingFeatureId, "getServiceStateForSubscriber")) {
Jack Yu85bd38a2015-11-09 11:34:32 -08007653 return null;
7654 }
7655
Hall Liuf19c44f2018-11-27 14:38:17 -08007656 LocationAccessPolicy.LocationPermissionResult fineLocationResult =
7657 LocationAccessPolicy.checkLocationPermission(mApp,
7658 new LocationAccessPolicy.LocationPermissionQuery.Builder()
7659 .setCallingPackage(callingPackage)
Philip P. Moltmann3a2772a2019-10-04 08:15:00 -07007660 .setCallingFeatureId(callingFeatureId)
Hall Liuf19c44f2018-11-27 14:38:17 -08007661 .setCallingPid(Binder.getCallingPid())
7662 .setCallingUid(Binder.getCallingUid())
7663 .setMethod("getServiceStateForSubscriber")
Hall Liuf18a0cf2019-04-17 13:37:11 -07007664 .setLogAsInfo(true)
Hall Liuf19c44f2018-11-27 14:38:17 -08007665 .setMinSdkVersionForFine(Build.VERSION_CODES.Q)
Hall Liuc4a3e422020-05-26 17:18:03 -07007666 .setMinSdkVersionForCoarse(Build.VERSION_CODES.Q)
7667 .setMinSdkVersionForEnforcement(Build.VERSION_CODES.Q)
Hall Liuf19c44f2018-11-27 14:38:17 -08007668 .build());
7669
7670 LocationAccessPolicy.LocationPermissionResult coarseLocationResult =
7671 LocationAccessPolicy.checkLocationPermission(mApp,
7672 new LocationAccessPolicy.LocationPermissionQuery.Builder()
7673 .setCallingPackage(callingPackage)
Philip P. Moltmann3a2772a2019-10-04 08:15:00 -07007674 .setCallingFeatureId(callingFeatureId)
Hall Liuf19c44f2018-11-27 14:38:17 -08007675 .setCallingPid(Binder.getCallingPid())
7676 .setCallingUid(Binder.getCallingUid())
7677 .setMethod("getServiceStateForSubscriber")
Hall Liuf18a0cf2019-04-17 13:37:11 -07007678 .setLogAsInfo(true)
Hall Liuf19c44f2018-11-27 14:38:17 -08007679 .setMinSdkVersionForCoarse(Build.VERSION_CODES.Q)
Hall Liuc4a3e422020-05-26 17:18:03 -07007680 .setMinSdkVersionForFine(Integer.MAX_VALUE)
7681 .setMinSdkVersionForEnforcement(Build.VERSION_CODES.Q)
Hall Liuf19c44f2018-11-27 14:38:17 -08007682 .build());
7683 // We don't care about hard or soft here -- all we need to know is how much info to scrub.
7684 boolean hasFinePermission =
7685 fineLocationResult == LocationAccessPolicy.LocationPermissionResult.ALLOWED;
7686 boolean hasCoarsePermission =
7687 coarseLocationResult == LocationAccessPolicy.LocationPermissionResult.ALLOWED;
7688
Jack Yu479f40e2020-10-27 21:29:25 -07007689 final Phone phone = getPhone(subId);
7690 if (phone == null) {
7691 return null;
7692 }
7693
Jordan Liu0f2bc442020-11-18 16:47:37 -08007694 final long identity = Binder.clearCallingIdentity();
7695
Jack Yu479f40e2020-10-27 21:29:25 -07007696 boolean isCallingPackageDataService = phone.getDataServicePackages()
7697 .contains(callingPackage);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007698 try {
Jordan Liuc437b192020-08-17 10:59:12 -07007699 // isActiveSubId requires READ_PHONE_STATE, which we already check for above
7700 if (!mSubscriptionController.isActiveSubId(subId, callingPackage, callingFeatureId)) {
7701 Rlog.d(LOG_TAG,
7702 "getServiceStateForSubscriber returning null for inactive subId=" + subId);
7703 return null;
7704 }
7705
Hall Liuf19c44f2018-11-27 14:38:17 -08007706 ServiceState ss = phone.getServiceState();
7707
7708 // Scrub out the location info in ServiceState depending on what level of access
7709 // the caller has.
Jack Yu479f40e2020-10-27 21:29:25 -07007710 if (hasFinePermission || isCallingPackageDataService) return ss;
Malcolm Chen5052de62019-12-30 13:56:38 -08007711 if (hasCoarsePermission) return ss.createLocationInfoSanitizedCopy(false);
7712 return ss.createLocationInfoSanitizedCopy(true);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007713 } finally {
7714 Binder.restoreCallingIdentity(identity);
7715 }
Jack Yu85bd38a2015-11-09 11:34:32 -08007716 }
Nancy Chen31f9ba12016-01-06 11:42:12 -08007717
7718 /**
7719 * Returns the URI for the per-account voicemail ringtone set in Phone settings.
7720 *
7721 * @param accountHandle The handle for the {@link PhoneAccount} for which to retrieve the
7722 * voicemail ringtone.
7723 * @return The URI for the ringtone to play when receiving a voicemail from a specific
7724 * PhoneAccount.
7725 */
7726 @Override
7727 public Uri getVoicemailRingtoneUri(PhoneAccountHandle accountHandle) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007728 final long identity = Binder.clearCallingIdentity();
7729 try {
7730 Phone phone = PhoneUtils.getPhoneForPhoneAccountHandle(accountHandle);
7731 if (phone == null) {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08007732 phone = getDefaultPhone();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007733 }
Nancy Chen31f9ba12016-01-06 11:42:12 -08007734
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007735 return VoicemailNotificationSettingsUtil.getRingtoneUri(phone.getContext());
7736 } finally {
7737 Binder.restoreCallingIdentity(identity);
7738 }
Nancy Chen31f9ba12016-01-06 11:42:12 -08007739 }
7740
7741 /**
Ta-wei Yenc33877d2017-01-23 18:11:21 -08007742 * Sets the per-account voicemail ringtone.
7743 *
7744 * <p>Requires that the calling app is the default dialer, or has carrier privileges, or
7745 * has permission {@link android.Manifest.permission#MODIFY_PHONE_STATE MODIFY_PHONE_STATE}.
7746 *
7747 * @param phoneAccountHandle The handle for the {@link PhoneAccount} for which to set the
7748 * voicemail ringtone.
7749 * @param uri The URI for the ringtone to play when receiving a voicemail from a specific
7750 * PhoneAccount.
7751 */
7752 @Override
7753 public void setVoicemailRingtoneUri(String callingPackage,
7754 PhoneAccountHandle phoneAccountHandle, Uri uri) {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08007755 final Phone defaultPhone = getDefaultPhone();
Ta-wei Yenc33877d2017-01-23 18:11:21 -08007756 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
Tyler Gunn5ddfdc92019-10-31 13:08:23 -07007757 TelecomManager tm = defaultPhone.getContext().getSystemService(TelecomManager.class);
7758 if (!TextUtils.equals(callingPackage, tm.getDefaultDialerPackage())) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08007759 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
7760 mApp, PhoneUtils.getSubIdForPhoneAccountHandle(phoneAccountHandle),
7761 "setVoicemailRingtoneUri");
Ta-wei Yenc33877d2017-01-23 18:11:21 -08007762 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007763
7764 final long identity = Binder.clearCallingIdentity();
7765 try {
7766 Phone phone = PhoneUtils.getPhoneForPhoneAccountHandle(phoneAccountHandle);
7767 if (phone == null) {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08007768 phone = defaultPhone;
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007769 }
7770 VoicemailNotificationSettingsUtil.setRingtoneUri(phone.getContext(), uri);
7771 } finally {
7772 Binder.restoreCallingIdentity(identity);
Ta-wei Yenc33877d2017-01-23 18:11:21 -08007773 }
Ta-wei Yenc33877d2017-01-23 18:11:21 -08007774 }
7775
7776 /**
Nancy Chen31f9ba12016-01-06 11:42:12 -08007777 * Returns whether vibration is set for voicemail notification in Phone settings.
7778 *
7779 * @param accountHandle The handle for the {@link PhoneAccount} for which to retrieve the
7780 * voicemail vibration setting.
7781 * @return {@code true} if the vibration is set for this PhoneAccount, {@code false} otherwise.
7782 */
7783 @Override
7784 public boolean isVoicemailVibrationEnabled(PhoneAccountHandle accountHandle) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007785 final long identity = Binder.clearCallingIdentity();
7786 try {
7787 Phone phone = PhoneUtils.getPhoneForPhoneAccountHandle(accountHandle);
7788 if (phone == null) {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08007789 phone = getDefaultPhone();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007790 }
Nancy Chen31f9ba12016-01-06 11:42:12 -08007791
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007792 return VoicemailNotificationSettingsUtil.isVibrationEnabled(phone.getContext());
7793 } finally {
7794 Binder.restoreCallingIdentity(identity);
7795 }
Nancy Chen31f9ba12016-01-06 11:42:12 -08007796 }
7797
Youhan Wange64578a2016-05-02 15:32:42 -07007798 /**
Ta-wei Yenc33877d2017-01-23 18:11:21 -08007799 * Sets the per-account voicemail vibration.
7800 *
7801 * <p>Requires that the calling app is the default dialer, or has carrier privileges, or
7802 * has permission {@link android.Manifest.permission#MODIFY_PHONE_STATE MODIFY_PHONE_STATE}.
7803 *
7804 * @param phoneAccountHandle The handle for the {@link PhoneAccount} for which to set the
7805 * voicemail vibration setting.
7806 * @param enabled Whether to enable or disable vibration for voicemail notifications from a
7807 * specific PhoneAccount.
7808 */
7809 @Override
7810 public void setVoicemailVibrationEnabled(String callingPackage,
7811 PhoneAccountHandle phoneAccountHandle, boolean enabled) {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08007812 final Phone defaultPhone = getDefaultPhone();
Ta-wei Yenc33877d2017-01-23 18:11:21 -08007813 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
Tyler Gunn5ddfdc92019-10-31 13:08:23 -07007814 TelecomManager tm = defaultPhone.getContext().getSystemService(TelecomManager.class);
7815 if (!TextUtils.equals(callingPackage, tm.getDefaultDialerPackage())) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08007816 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
7817 mApp, PhoneUtils.getSubIdForPhoneAccountHandle(phoneAccountHandle),
7818 "setVoicemailVibrationEnabled");
Ta-wei Yenc33877d2017-01-23 18:11:21 -08007819 }
7820
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007821 final long identity = Binder.clearCallingIdentity();
7822 try {
7823 Phone phone = PhoneUtils.getPhoneForPhoneAccountHandle(phoneAccountHandle);
7824 if (phone == null) {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08007825 phone = defaultPhone;
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007826 }
7827 VoicemailNotificationSettingsUtil.setVibrationEnabled(phone.getContext(), enabled);
7828 } finally {
7829 Binder.restoreCallingIdentity(identity);
Ta-wei Yenc33877d2017-01-23 18:11:21 -08007830 }
Ta-wei Yenc33877d2017-01-23 18:11:21 -08007831 }
7832
7833 /**
Youhan Wange64578a2016-05-02 15:32:42 -07007834 * Make sure either called from same process as self (phone) or IPC caller has read privilege.
7835 *
7836 * @throws SecurityException if the caller does not have the required permission
7837 */
Brad Ebinger35c841c2018-10-01 10:40:55 -07007838 private void enforceReadPrivilegedPermission(String message) {
Youhan Wange64578a2016-05-02 15:32:42 -07007839 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.READ_PRIVILEGED_PHONE_STATE,
Brad Ebinger35c841c2018-10-01 10:40:55 -07007840 message);
Youhan Wange64578a2016-05-02 15:32:42 -07007841 }
7842
7843 /**
Ta-wei Yen30a69c82016-12-27 14:52:32 -08007844 * Make sure either called from same process as self (phone) or IPC caller has send SMS
7845 * permission.
7846 *
7847 * @throws SecurityException if the caller does not have the required permission
7848 */
7849 private void enforceSendSmsPermission() {
7850 mApp.enforceCallingOrSelfPermission(permission.SEND_SMS, null);
7851 }
7852
7853 /**
Ta-wei Yen527a9c02017-01-06 15:29:25 -08007854 * Make sure called from the package in charge of visual voicemail.
Ta-wei Yen30a69c82016-12-27 14:52:32 -08007855 *
Ta-wei Yen527a9c02017-01-06 15:29:25 -08007856 * @throws SecurityException if the caller is not the visual voicemail package.
Ta-wei Yen30a69c82016-12-27 14:52:32 -08007857 */
Ta-wei Yen527a9c02017-01-06 15:29:25 -08007858 private void enforceVisualVoicemailPackage(String callingPackage, int subId) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007859 final long identity = Binder.clearCallingIdentity();
7860 try {
7861 ComponentName componentName =
Nazanin Bakhshif782e562018-12-11 15:15:39 -08007862 RemoteVvmTaskManager.getRemotePackage(mApp, subId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007863 if (componentName == null) {
7864 throw new SecurityException(
7865 "Caller not current active visual voicemail package[null]");
7866 }
7867 String vvmPackage = componentName.getPackageName();
7868 if (!callingPackage.equals(vvmPackage)) {
7869 throw new SecurityException("Caller not current active visual voicemail package["
7870 + vvmPackage + "]");
7871 }
7872 } finally {
7873 Binder.restoreCallingIdentity(identity);
Ta-wei Yen30a69c82016-12-27 14:52:32 -08007874 }
7875 }
7876
7877 /**
Youhan Wange64578a2016-05-02 15:32:42 -07007878 * Return the application ID for the app type.
7879 *
7880 * @param subId the subscription ID that this request applies to.
7881 * @param appType the uicc app type.
7882 * @return Application ID for specificied app type, or null if no uicc.
7883 */
7884 @Override
7885 public String getAidForAppType(int subId, int appType) {
Brad Ebinger35c841c2018-10-01 10:40:55 -07007886 enforceReadPrivilegedPermission("getAidForAppType");
Youhan Wange64578a2016-05-02 15:32:42 -07007887 Phone phone = getPhone(subId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007888
7889 final long identity = Binder.clearCallingIdentity();
Youhan Wange64578a2016-05-02 15:32:42 -07007890 try {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007891 if (phone == null) {
7892 return null;
7893 }
7894 String aid = null;
7895 try {
7896 aid = UiccController.getInstance().getUiccCard(phone.getPhoneId())
7897 .getApplicationByType(appType).getAid();
7898 } catch (Exception e) {
7899 Log.e(LOG_TAG, "Not getting aid. Exception ex=" + e);
7900 }
7901 return aid;
7902 } finally {
7903 Binder.restoreCallingIdentity(identity);
Youhan Wange64578a2016-05-02 15:32:42 -07007904 }
Youhan Wange64578a2016-05-02 15:32:42 -07007905 }
7906
Youhan Wang4001d252016-05-11 10:29:41 -07007907 /**
7908 * Return the Electronic Serial Number.
7909 *
7910 * @param subId the subscription ID that this request applies to.
7911 * @return ESN or null if error.
7912 */
7913 @Override
7914 public String getEsn(int subId) {
Brad Ebinger35c841c2018-10-01 10:40:55 -07007915 enforceReadPrivilegedPermission("getEsn");
Youhan Wang4001d252016-05-11 10:29:41 -07007916 Phone phone = getPhone(subId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007917
7918 final long identity = Binder.clearCallingIdentity();
Youhan Wang4001d252016-05-11 10:29:41 -07007919 try {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007920 if (phone == null) {
7921 return null;
7922 }
7923 String esn = null;
7924 try {
7925 esn = phone.getEsn();
7926 } catch (Exception e) {
7927 Log.e(LOG_TAG, "Not getting ESN. Exception ex=" + e);
7928 }
7929 return esn;
7930 } finally {
7931 Binder.restoreCallingIdentity(identity);
Youhan Wang4001d252016-05-11 10:29:41 -07007932 }
Youhan Wang4001d252016-05-11 10:29:41 -07007933 }
7934
Sanket Padawe99ef1e32016-05-18 16:12:33 -07007935 /**
Youhan Wang66ad5d72016-07-18 17:56:58 -07007936 * Return the Preferred Roaming List Version.
7937 *
7938 * @param subId the subscription ID that this request applies to.
7939 * @return PRLVersion or null if error.
7940 */
7941 @Override
7942 public String getCdmaPrlVersion(int subId) {
Brad Ebinger35c841c2018-10-01 10:40:55 -07007943 enforceReadPrivilegedPermission("getCdmaPrlVersion");
Youhan Wang66ad5d72016-07-18 17:56:58 -07007944 Phone phone = getPhone(subId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007945
7946 final long identity = Binder.clearCallingIdentity();
Youhan Wang66ad5d72016-07-18 17:56:58 -07007947 try {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007948 if (phone == null) {
7949 return null;
7950 }
7951 String cdmaPrlVersion = null;
7952 try {
7953 cdmaPrlVersion = phone.getCdmaPrlVersion();
7954 } catch (Exception e) {
7955 Log.e(LOG_TAG, "Not getting PRLVersion", e);
7956 }
7957 return cdmaPrlVersion;
7958 } finally {
7959 Binder.restoreCallingIdentity(identity);
Youhan Wang66ad5d72016-07-18 17:56:58 -07007960 }
Youhan Wang66ad5d72016-07-18 17:56:58 -07007961 }
7962
7963 /**
Sanket Padawe99ef1e32016-05-18 16:12:33 -07007964 * Get snapshot of Telephony histograms
7965 * @return List of Telephony histograms
7966 * @hide
7967 */
7968 @Override
7969 public List<TelephonyHistogram> getTelephonyHistograms() {
Jeff Davidson7e17e312018-02-13 18:17:36 -08007970 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
7971 mApp, getDefaultSubscription(), "getTelephonyHistograms");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007972
7973 final long identity = Binder.clearCallingIdentity();
7974 try {
7975 return RIL.getTelephonyRILTimingHistograms();
7976 } finally {
7977 Binder.restoreCallingIdentity(identity);
7978 }
Sanket Padawe99ef1e32016-05-18 16:12:33 -07007979 }
Meng Wang1a7c35a2016-05-05 20:56:15 -07007980
7981 /**
7982 * {@hide}
Michele Berionne482f8202018-11-27 18:57:59 -08007983 * Set the allowed carrier list and the excluded carrier list, indicating the priority between
7984 * the two lists.
Meng Wang1a7c35a2016-05-05 20:56:15 -07007985 * Require system privileges. In the future we may add this to carrier APIs.
7986 *
Michele Berionne482f8202018-11-27 18:57:59 -08007987 * @return Integer with the result of the operation, as defined in {@link TelephonyManager}.
Meng Wang1a7c35a2016-05-05 20:56:15 -07007988 */
7989 @Override
Michele Berionne482f8202018-11-27 18:57:59 -08007990 @TelephonyManager.SetCarrierRestrictionResult
7991 public int setAllowedCarriers(CarrierRestrictionRules carrierRestrictionRules) {
Meng Wang1a7c35a2016-05-05 20:56:15 -07007992 enforceModifyPermission();
vagdeviaf9a5b92018-08-15 16:01:53 -07007993 WorkSource workSource = getWorkSource(Binder.getCallingUid());
Sanket Padawe13bac7b2017-03-20 15:04:47 -07007994
Michele Berionne482f8202018-11-27 18:57:59 -08007995 if (carrierRestrictionRules == null) {
7996 throw new NullPointerException("carrier restriction cannot be null");
Meng Wang9b7c4e92017-02-17 11:41:27 -08007997 }
Sanket Padawe13bac7b2017-03-20 15:04:47 -07007998
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007999 final long identity = Binder.clearCallingIdentity();
8000 try {
Michele Berionne482f8202018-11-27 18:57:59 -08008001 return (int) sendRequest(CMD_SET_ALLOWED_CARRIERS, carrierRestrictionRules,
vagdeviaf9a5b92018-08-15 16:01:53 -07008002 workSource);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008003 } finally {
8004 Binder.restoreCallingIdentity(identity);
8005 }
Meng Wang1a7c35a2016-05-05 20:56:15 -07008006 }
8007
8008 /**
8009 * {@hide}
Michele Berionne482f8202018-11-27 18:57:59 -08008010 * Get the allowed carrier list and the excluded carrier list, including the priority between
8011 * the two lists.
Meng Wang1a7c35a2016-05-05 20:56:15 -07008012 * Require system privileges. In the future we may add this to carrier APIs.
8013 *
Michele Berionne482f8202018-11-27 18:57:59 -08008014 * @return {@link android.telephony.CarrierRestrictionRules}
Meng Wang1a7c35a2016-05-05 20:56:15 -07008015 */
8016 @Override
Michele Berionne482f8202018-11-27 18:57:59 -08008017 public CarrierRestrictionRules getAllowedCarriers() {
Brad Ebinger35c841c2018-10-01 10:40:55 -07008018 enforceReadPrivilegedPermission("getAllowedCarriers");
vagdeviaf9a5b92018-08-15 16:01:53 -07008019 WorkSource workSource = getWorkSource(Binder.getCallingUid());
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008020
8021 final long identity = Binder.clearCallingIdentity();
8022 try {
Michele Berionne482f8202018-11-27 18:57:59 -08008023 Object response = sendRequest(CMD_GET_ALLOWED_CARRIERS, null, workSource);
8024 if (response instanceof CarrierRestrictionRules) {
8025 return (CarrierRestrictionRules) response;
8026 }
8027 // Response is an Exception of some kind,
8028 // which is signalled to the user as a NULL retval
8029 return null;
8030 } catch (Exception e) {
8031 Log.e(LOG_TAG, "getAllowedCarriers. Exception ex=" + e);
8032 return null;
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008033 } finally {
8034 Binder.restoreCallingIdentity(identity);
8035 }
Meng Wang1a7c35a2016-05-05 20:56:15 -07008036 }
8037
fionaxu59545b42016-05-25 15:53:37 -07008038 /**
fionaxu59545b42016-05-25 15:53:37 -07008039 * Action set from carrier signalling broadcast receivers to enable/disable radio
8040 * @param subId the subscription ID that this action applies to.
8041 * @param enabled control enable or disable radio.
8042 * {@hide}
8043 */
8044 @Override
8045 public void carrierActionSetRadioEnabled(int subId, boolean enabled) {
8046 enforceModifyPermission();
8047 final Phone phone = getPhone(subId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008048
8049 final long identity = Binder.clearCallingIdentity();
fionaxu59545b42016-05-25 15:53:37 -07008050 if (phone == null) {
8051 loge("carrierAction: SetRadioEnabled fails with invalid sibId: " + subId);
8052 return;
8053 }
8054 try {
8055 phone.carrierActionSetRadioEnabled(enabled);
8056 } catch (Exception e) {
8057 Log.e(LOG_TAG, "carrierAction: SetRadioEnabled fails. Exception ex=" + e);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008058 } finally {
8059 Binder.restoreCallingIdentity(identity);
fionaxu59545b42016-05-25 15:53:37 -07008060 }
8061 }
8062
Ta-wei Yenc236d6b2016-06-21 13:33:12 -07008063 /**
fionaxu8da9cb12017-05-23 15:02:46 -07008064 * Action set from carrier signalling broadcast receivers to start/stop reporting the default
8065 * network status based on which carrier apps could apply actions accordingly,
8066 * enable/disable default url handler for example.
8067 *
8068 * @param subId the subscription ID that this action applies to.
8069 * @param report control start/stop reporting the default network status.
8070 * {@hide}
8071 */
8072 @Override
8073 public void carrierActionReportDefaultNetworkStatus(int subId, boolean report) {
8074 enforceModifyPermission();
8075 final Phone phone = getPhone(subId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008076
8077 final long identity = Binder.clearCallingIdentity();
fionaxu8da9cb12017-05-23 15:02:46 -07008078 if (phone == null) {
8079 loge("carrierAction: ReportDefaultNetworkStatus fails with invalid sibId: " + subId);
8080 return;
8081 }
8082 try {
8083 phone.carrierActionReportDefaultNetworkStatus(report);
8084 } catch (Exception e) {
8085 Log.e(LOG_TAG, "carrierAction: ReportDefaultNetworkStatus fails. Exception ex=" + e);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008086 } finally {
8087 Binder.restoreCallingIdentity(identity);
fionaxu8da9cb12017-05-23 15:02:46 -07008088 }
8089 }
8090
8091 /**
fionaxud9622282017-07-17 17:51:30 -07008092 * Action set from carrier signalling broadcast receivers to reset all carrier actions
8093 * @param subId the subscription ID that this action applies to.
8094 * {@hide}
8095 */
8096 @Override
8097 public void carrierActionResetAll(int subId) {
8098 enforceModifyPermission();
8099 final Phone phone = getPhone(subId);
8100 if (phone == null) {
8101 loge("carrierAction: ResetAll fails with invalid sibId: " + subId);
8102 return;
8103 }
8104 try {
8105 phone.carrierActionResetAll();
8106 } catch (Exception e) {
8107 Log.e(LOG_TAG, "carrierAction: ResetAll fails. Exception ex=" + e);
8108 }
8109 }
8110
8111 /**
Ta-wei Yenc236d6b2016-06-21 13:33:12 -07008112 * Called when "adb shell dumpsys phone" is invoked. Dump is also automatically invoked when a
8113 * bug report is being generated.
8114 */
8115 @Override
Ta-wei Yen99282e02016-06-21 18:19:35 -07008116 protected void dump(FileDescriptor fd, PrintWriter writer, String[] args) {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08008117 if (mApp.checkCallingOrSelfPermission(android.Manifest.permission.DUMP)
8118 != PackageManager.PERMISSION_GRANTED) {
dcashman22b950d2016-06-27 11:39:02 -07008119 writer.println("Permission Denial: can't dump Phone from pid="
8120 + Binder.getCallingPid()
8121 + ", uid=" + Binder.getCallingUid()
8122 + "without permission "
8123 + android.Manifest.permission.DUMP);
8124 return;
8125 }
Nazanin Bakhshif782e562018-12-11 15:15:39 -08008126 DumpsysHandler.dump(mApp, fd, writer, args);
Ta-wei Yenc236d6b2016-06-21 13:33:12 -07008127 }
Jack Yueb89b242016-06-22 13:27:47 -07008128
Brad Ebingerdac2f002018-04-03 15:17:52 -07008129 @Override
Hall Liua1548bd2019-12-24 14:14:12 -08008130 public int handleShellCommand(@NonNull ParcelFileDescriptor in,
8131 @NonNull ParcelFileDescriptor out, @NonNull ParcelFileDescriptor err,
8132 @NonNull String[] args) {
8133 return new TelephonyShellCommand(this, getDefaultPhone().getContext()).exec(
8134 this, in.getFileDescriptor(), out.getFileDescriptor(),
8135 err.getFileDescriptor(), args);
Brad Ebingerdac2f002018-04-03 15:17:52 -07008136 }
8137
Jack Yueb89b242016-06-22 13:27:47 -07008138 /**
Sooraj Sasindranaf1b5132020-05-05 21:06:20 +00008139 * Policy control of data connection with reason {@@TelephonyManager.DataEnabledReason}
Greg Kaiser17f41752020-05-05 16:47:47 +00008140 * @param subId Subscription index
Sooraj Sasindranaf1b5132020-05-05 21:06:20 +00008141 * @param reason the reason the data enable change is taking place
8142 * @param enabled True if enabling the data, otherwise disabling.
8143 * @hide
Jack Yu75ab2952016-07-08 14:29:33 -07008144 */
8145 @Override
Sooraj Sasindran95c07a92020-07-15 01:35:24 -07008146 public void setDataEnabledForReason(int subId, @TelephonyManager.DataEnabledReason int reason,
Sooraj Sasindranaf1b5132020-05-05 21:06:20 +00008147 boolean enabled) {
8148 if (reason == TelephonyManager.DATA_ENABLED_REASON_USER
8149 || reason == TelephonyManager.DATA_ENABLED_REASON_CARRIER) {
8150 try {
8151 TelephonyPermissions.enforceCallingOrSelfCarrierPrivilege(
Sooraj Sasindran95c07a92020-07-15 01:35:24 -07008152 mApp, subId, "setDataEnabledForReason");
Sooraj Sasindranaf1b5132020-05-05 21:06:20 +00008153 } catch (SecurityException se) {
8154 enforceModifyPermission();
8155 }
8156 } else {
8157 enforceModifyPermission();
8158 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008159
8160 final long identity = Binder.clearCallingIdentity();
8161 try {
8162 Phone phone = getPhone(subId);
8163 if (phone != null) {
Sooraj Sasindranaf1b5132020-05-05 21:06:20 +00008164 if (reason == TelephonyManager.DATA_ENABLED_REASON_CARRIER) {
8165 phone.carrierActionSetMeteredApnsEnabled(enabled);
8166 } else {
8167 phone.getDataEnabledSettings().setDataEnabled(reason, enabled);
8168 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008169 }
8170 } finally {
8171 Binder.restoreCallingIdentity(identity);
Jack Yu75ab2952016-07-08 14:29:33 -07008172 }
8173 }
Sooraj Sasindran9a909312016-07-18 11:57:25 -07008174
8175 /**
8176 * Get Client request stats
8177 * @return List of Client Request Stats
8178 * @hide
8179 */
8180 @Override
Philip P. Moltmann700a9592019-10-03 11:53:50 -07008181 public List<ClientRequestStats> getClientRequestStats(String callingPackage,
8182 String callingFeatureId, int subId) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08008183 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
Philip P. Moltmann700a9592019-10-03 11:53:50 -07008184 mApp, subId, callingPackage, callingFeatureId, "getClientRequestStats")) {
Sooraj Sasindran9a909312016-07-18 11:57:25 -07008185 return null;
8186 }
Sooraj Sasindran9a909312016-07-18 11:57:25 -07008187 Phone phone = getPhone(subId);
Sooraj Sasindran9a909312016-07-18 11:57:25 -07008188
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008189 final long identity = Binder.clearCallingIdentity();
8190 try {
8191 if (phone != null) {
8192 return phone.getClientRequestStats();
8193 }
8194
8195 return null;
8196 } finally {
8197 Binder.restoreCallingIdentity(identity);
8198 }
Sooraj Sasindran9a909312016-07-18 11:57:25 -07008199 }
8200
Narayan Kamathf04b5a12018-01-09 11:47:15 +00008201 private WorkSource getWorkSource(int uid) {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08008202 String packageName = mApp.getPackageManager().getNameForUid(uid);
Narayan Kamathf04b5a12018-01-09 11:47:15 +00008203 return new WorkSource(uid, packageName);
Sooraj Sasindran9a909312016-07-18 11:57:25 -07008204 }
Jack Yueb4124c2017-02-16 15:32:43 -08008205
8206 /**
Grace Chen70990072017-03-24 17:21:30 -07008207 * Set SIM card power state.
Jack Yueb4124c2017-02-16 15:32:43 -08008208 *
Sanket Padawe13bac7b2017-03-20 15:04:47 -07008209 * @param slotIndex SIM slot id.
Grace Chen70990072017-03-24 17:21:30 -07008210 * @param state State of SIM (power down, power up, pass through)
8211 * - {@link android.telephony.TelephonyManager#CARD_POWER_DOWN}
8212 * - {@link android.telephony.TelephonyManager#CARD_POWER_UP}
8213 * - {@link android.telephony.TelephonyManager#CARD_POWER_UP_PASS_THROUGH}
Jack Yueb4124c2017-02-16 15:32:43 -08008214 *
8215 **/
8216 @Override
Grace Chen70990072017-03-24 17:21:30 -07008217 public void setSimPowerStateForSlot(int slotIndex, int state) {
Jack Yueb4124c2017-02-16 15:32:43 -08008218 enforceModifyPermission();
Sanket Padawe13bac7b2017-03-20 15:04:47 -07008219 Phone phone = PhoneFactory.getPhone(slotIndex);
8220
vagdeviaf9a5b92018-08-15 16:01:53 -07008221 WorkSource workSource = getWorkSource(Binder.getCallingUid());
8222
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008223 final long identity = Binder.clearCallingIdentity();
8224 try {
8225 if (phone != null) {
Jordan Liu109698e2020-11-24 14:50:34 -08008226 phone.setSimPowerState(state, null, workSource);
8227 }
8228 } finally {
8229 Binder.restoreCallingIdentity(identity);
8230 }
8231 }
8232
8233 /**
8234 * Set SIM card power state.
8235 *
8236 * @param slotIndex SIM slot id.
8237 * @param state State of SIM (power down, power up, pass through)
8238 * @param callback callback to trigger after success or failure
8239 * - {@link android.telephony.TelephonyManager#CARD_POWER_DOWN}
8240 * - {@link android.telephony.TelephonyManager#CARD_POWER_UP}
8241 * - {@link android.telephony.TelephonyManager#CARD_POWER_UP_PASS_THROUGH}
8242 *
8243 **/
8244 @Override
8245 public void setSimPowerStateForSlotWithCallback(int slotIndex, int state,
8246 IIntegerConsumer callback) {
8247 enforceModifyPermission();
8248 Phone phone = PhoneFactory.getPhone(slotIndex);
8249
8250 WorkSource workSource = getWorkSource(Binder.getCallingUid());
8251
8252 final long identity = Binder.clearCallingIdentity();
8253 try {
8254 if (phone != null) {
8255 Pair<Integer, IIntegerConsumer> arguments = Pair.create(state, callback);
8256 sendRequestAsync(CMD_SET_SIM_POWER, arguments, phone, workSource);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008257 }
8258 } finally {
8259 Binder.restoreCallingIdentity(identity);
Jack Yueb4124c2017-02-16 15:32:43 -08008260 }
8261 }
Shuo Qiandd210312017-04-12 22:11:33 +00008262
Tyler Gunn65d45c22017-06-05 11:22:26 -07008263 private boolean isUssdApiAllowed(int subId) {
8264 CarrierConfigManager configManager =
Nazanin Bakhshif782e562018-12-11 15:15:39 -08008265 (CarrierConfigManager) mApp.getSystemService(Context.CARRIER_CONFIG_SERVICE);
Tyler Gunn65d45c22017-06-05 11:22:26 -07008266 if (configManager == null) {
8267 return false;
8268 }
8269 PersistableBundle pb = configManager.getConfigForSubId(subId);
8270 if (pb == null) {
8271 return false;
8272 }
8273 return pb.getBoolean(
8274 CarrierConfigManager.KEY_ALLOW_USSD_REQUESTS_VIA_TELEPHONY_MANAGER_BOOL);
8275 }
8276
Shuo Qiandd210312017-04-12 22:11:33 +00008277 /**
8278 * Check if phone is in emergency callback mode
8279 * @return true if phone is in emergency callback mode
8280 * @param subId sub id
8281 */
goneil9c5f4872017-12-05 14:07:56 -08008282 @Override
Shuo Qiandd210312017-04-12 22:11:33 +00008283 public boolean getEmergencyCallbackMode(int subId) {
Brad Ebinger35c841c2018-10-01 10:40:55 -07008284 enforceReadPrivilegedPermission("getEmergencyCallbackMode");
Shuo Qiandd210312017-04-12 22:11:33 +00008285 final Phone phone = getPhone(subId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008286
8287 final long identity = Binder.clearCallingIdentity();
8288 try {
8289 if (phone != null) {
8290 return phone.isInEcm();
8291 } else {
8292 return false;
8293 }
8294 } finally {
8295 Binder.restoreCallingIdentity(identity);
Shuo Qiandd210312017-04-12 22:11:33 +00008296 }
8297 }
Nathan Harold46b42aa2017-03-10 19:38:22 -08008298
8299 /**
8300 * Get the current signal strength information for the given subscription.
8301 * Because this information is not updated when the device is in a low power state
8302 * it should not be relied-upon to be current.
8303 * @param subId Subscription index
8304 * @return the most recent cached signal strength info from the modem
8305 */
8306 @Override
8307 public SignalStrength getSignalStrength(int subId) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008308 final long identity = Binder.clearCallingIdentity();
8309 try {
8310 Phone p = getPhone(subId);
8311 if (p == null) {
8312 return null;
8313 }
Nathan Harold46b42aa2017-03-10 19:38:22 -08008314
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008315 return p.getSignalStrength();
8316 } finally {
8317 Binder.restoreCallingIdentity(identity);
8318 }
Nathan Harold46b42aa2017-03-10 19:38:22 -08008319 }
Holly Jiuyu Sun01c47ad2018-01-24 17:56:33 +00008320
Pengquan Meng77b7f132018-08-22 14:49:57 -07008321 /**
Chen Xuf792fd62018-10-17 17:54:36 +00008322 * Get the current modem radio state for the given slot.
8323 * @param slotIndex slot index.
8324 * @param callingPackage the name of the package making the call.
Philip P. Moltmann700a9592019-10-03 11:53:50 -07008325 * @param callingFeatureId The feature in the package.
Chen Xuf792fd62018-10-17 17:54:36 +00008326 * @return the current radio power state from the modem
8327 */
8328 @Override
Philip P. Moltmann700a9592019-10-03 11:53:50 -07008329 public int getRadioPowerState(int slotIndex, String callingPackage, String callingFeatureId) {
Chen Xuf792fd62018-10-17 17:54:36 +00008330 Phone phone = PhoneFactory.getPhone(slotIndex);
8331 if (phone != null) {
Philip P. Moltmann700a9592019-10-03 11:53:50 -07008332 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(mApp, phone.getSubId(),
8333 callingPackage, callingFeatureId, "getRadioPowerState")) {
Chen Xuf792fd62018-10-17 17:54:36 +00008334 return TelephonyManager.RADIO_POWER_UNAVAILABLE;
8335 }
8336
8337 final long identity = Binder.clearCallingIdentity();
8338 try {
8339 return phone.getRadioPowerState();
8340 } finally {
8341 Binder.restoreCallingIdentity(identity);
8342 }
8343 }
8344 return TelephonyManager.RADIO_POWER_UNAVAILABLE;
8345 }
8346
8347 /**
Pengquan Meng77b7f132018-08-22 14:49:57 -07008348 * Checks if data roaming is enabled on the subscription with id {@code subId}.
8349 *
8350 * <p>Requires one of the following permissions:
8351 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE},
8352 * {@link android.Manifest.permission#READ_PHONE_STATE} or that the calling app has carrier
8353 * privileges.
8354 *
8355 * @param subId subscription id
8356 * @return {@code true} if data roaming is enabled on this subscription, otherwise return
8357 * {@code false}.
8358 */
8359 @Override
8360 public boolean isDataRoamingEnabled(int subId) {
Shuo Qian093013d2020-08-13 15:42:55 -07008361 try {
8362 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.ACCESS_NETWORK_STATE,
8363 null);
8364 } catch (Exception e) {
8365 TelephonyPermissions.enforeceCallingOrSelfReadPhoneStatePermissionOrCarrierPrivilege(
8366 mApp, subId, "isDataRoamingEnabled");
8367 }
Pengquan Meng44e66f12019-04-01 10:48:20 -07008368
Pengquan Menga1bb6272018-09-06 09:59:22 -07008369 boolean isEnabled = false;
8370 final long identity = Binder.clearCallingIdentity();
Pengquan Meng77b7f132018-08-22 14:49:57 -07008371 try {
Pengquan Menga1bb6272018-09-06 09:59:22 -07008372 Phone phone = getPhone(subId);
8373 isEnabled = phone != null ? phone.getDataRoamingEnabled() : false;
Pengquan Menga1bb6272018-09-06 09:59:22 -07008374 } finally {
8375 Binder.restoreCallingIdentity(identity);
Pengquan Meng77b7f132018-08-22 14:49:57 -07008376 }
Pengquan Menga1bb6272018-09-06 09:59:22 -07008377 return isEnabled;
Pengquan Meng77b7f132018-08-22 14:49:57 -07008378 }
8379
8380
8381 /**
8382 * Enables/Disables the data roaming on the subscription with id {@code subId}.
8383 *
8384 * <p> Requires permission:
8385 * {@link android.Manifest.permission#MODIFY_PHONE_STATE} or that the calling app has carrier
8386 * privileges.
8387 *
8388 * @param subId subscription id
8389 * @param isEnabled {@code true} means enable, {@code false} means disable.
8390 */
8391 @Override
8392 public void setDataRoamingEnabled(int subId, boolean isEnabled) {
Pengquan Meng44e66f12019-04-01 10:48:20 -07008393 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
8394 mApp, subId, "setDataRoamingEnabled");
8395
Pengquan Menga1bb6272018-09-06 09:59:22 -07008396 final long identity = Binder.clearCallingIdentity();
8397 try {
Pengquan Menga1bb6272018-09-06 09:59:22 -07008398 Phone phone = getPhone(subId);
8399 if (phone != null) {
8400 phone.setDataRoamingEnabled(isEnabled);
8401 }
8402 } finally {
8403 Binder.restoreCallingIdentity(identity);
Pengquan Meng77b7f132018-08-22 14:49:57 -07008404 }
8405 }
8406
Holly Jiuyu Sun01c47ad2018-01-24 17:56:33 +00008407 @Override
Pengquan Meng6884a2c2018-10-03 12:19:13 -07008408 public boolean isManualNetworkSelectionAllowed(int subId) {
tom hsuc91afc72020-01-06 23:46:07 +08008409 TelephonyPermissions
8410 .enforeceCallingOrSelfReadPrivilegedPhoneStatePermissionOrCarrierPrivilege(
Pengquan Meng44e66f12019-04-01 10:48:20 -07008411 mApp, subId, "isManualNetworkSelectionAllowed");
8412
Pengquan Meng6884a2c2018-10-03 12:19:13 -07008413 boolean isAllowed = true;
8414 final long identity = Binder.clearCallingIdentity();
8415 try {
Pengquan Meng6884a2c2018-10-03 12:19:13 -07008416 Phone phone = getPhone(subId);
8417 if (phone != null) {
8418 isAllowed = phone.isCspPlmnEnabled();
8419 }
8420 } finally {
8421 Binder.restoreCallingIdentity(identity);
8422 }
8423 return isAllowed;
8424 }
8425
8426 @Override
Jordan Liu75f43ea2019-01-17 16:56:37 -08008427 public List<UiccCardInfo> getUiccCardsInfo(String callingPackage) {
Jordan Liu4cda4552020-03-23 11:55:07 -07008428 // Verify that tha callingPackage belongs to the calling UID
8429 mApp.getSystemService(AppOpsManager.class)
8430 .checkPackage(Binder.getCallingUid(), callingPackage);
8431
Jordan Liu1e142fc2019-04-22 15:10:43 -07008432 boolean hasReadPermission = false;
Jordan Liuc65bc952019-02-12 17:54:02 -08008433 try {
8434 enforceReadPrivilegedPermission("getUiccCardsInfo");
Jordan Liu1e142fc2019-04-22 15:10:43 -07008435 hasReadPermission = true;
Jordan Liuc65bc952019-02-12 17:54:02 -08008436 } catch (SecurityException e) {
8437 // even without READ_PRIVILEGED_PHONE_STATE, we allow the call to continue if the caller
8438 // has carrier privileges on an active UICC
8439 if (checkCarrierPrivilegesForPackageAnyPhone(callingPackage)
8440 != TelephonyManager.CARRIER_PRIVILEGE_STATUS_HAS_ACCESS) {
Jordan Liu1e142fc2019-04-22 15:10:43 -07008441 throw new SecurityException("Caller does not have permission.");
Jordan Liuc65bc952019-02-12 17:54:02 -08008442 }
Jordan Liu75f43ea2019-01-17 16:56:37 -08008443 }
Jordan Liu5aa07002018-12-18 15:44:48 -08008444
8445 final long identity = Binder.clearCallingIdentity();
8446 try {
Jordan Liu75f43ea2019-01-17 16:56:37 -08008447 UiccController uiccController = UiccController.getInstance();
8448 ArrayList<UiccCardInfo> cardInfos = uiccController.getAllUiccCardInfos();
Jordan Liu1e142fc2019-04-22 15:10:43 -07008449 if (hasReadPermission) {
8450 return cardInfos;
Jordan Liu75f43ea2019-01-17 16:56:37 -08008451 }
Jordan Liu1e142fc2019-04-22 15:10:43 -07008452
8453 // Remove private info if the caller doesn't have access
8454 ArrayList<UiccCardInfo> filteredInfos = new ArrayList<>();
8455 for (UiccCardInfo cardInfo : cardInfos) {
8456 // For an inactive eUICC, the UiccCard will be null even though the UiccCardInfo
8457 // is available
8458 UiccCard card = uiccController.getUiccCardForSlot(cardInfo.getSlotIndex());
8459 if (card == null || card.getUiccProfile() == null) {
8460 // assume no access if the card or profile is unavailable
8461 filteredInfos.add(cardInfo.getUnprivileged());
8462 continue;
8463 }
8464 UiccProfile profile = card.getUiccProfile();
8465 if (profile.getCarrierPrivilegeStatus(mApp.getPackageManager(), callingPackage)
8466 == TelephonyManager.CARRIER_PRIVILEGE_STATUS_HAS_ACCESS) {
8467 filteredInfos.add(cardInfo);
8468 } else {
8469 filteredInfos.add(cardInfo.getUnprivileged());
8470 }
8471 }
8472 return filteredInfos;
Jordan Liu5aa07002018-12-18 15:44:48 -08008473 } finally {
8474 Binder.restoreCallingIdentity(identity);
8475 }
8476 }
8477
8478 @Override
Holly Jiuyu Sun01c47ad2018-01-24 17:56:33 +00008479 public UiccSlotInfo[] getUiccSlotsInfo() {
Brad Ebinger35c841c2018-10-01 10:40:55 -07008480 enforceReadPrivilegedPermission("getUiccSlotsInfo");
Holly Jiuyu Sun01c47ad2018-01-24 17:56:33 +00008481
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008482 final long identity = Binder.clearCallingIdentity();
8483 try {
8484 UiccSlot[] slots = UiccController.getInstance().getUiccSlots();
8485 if (slots == null) {
8486 Rlog.i(LOG_TAG, "slots is null.");
8487 return null;
8488 }
8489
8490 UiccSlotInfo[] infos = new UiccSlotInfo[slots.length];
8491 for (int i = 0; i < slots.length; i++) {
8492 UiccSlot slot = slots[i];
8493 if (slot == null) {
8494 continue;
8495 }
8496
Jordan Liu7be7e652019-05-06 18:55:02 +00008497 String cardId;
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008498 UiccCard card = slot.getUiccCard();
8499 if (card != null) {
8500 cardId = card.getCardId();
Jordan Liu7be7e652019-05-06 18:55:02 +00008501 } else {
Jordan Liu01bd00d2019-09-12 16:19:43 -07008502 cardId = slot.getEid();
8503 if (TextUtils.isEmpty(cardId)) {
8504 cardId = slot.getIccId();
8505 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008506 }
8507
Jordan Liu857451f2019-05-09 16:35:35 -07008508 if (cardId != null) {
8509 // if cardId is an ICCID, strip off trailing Fs before exposing to user
8510 // if cardId is an EID, it's all digits so this is fine
8511 cardId = IccUtils.stripTrailingFs(cardId);
8512 }
8513
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008514 int cardState = 0;
8515 switch (slot.getCardState()) {
8516 case CARDSTATE_ABSENT:
8517 cardState = UiccSlotInfo.CARD_STATE_INFO_ABSENT;
8518 break;
8519 case CARDSTATE_PRESENT:
8520 cardState = UiccSlotInfo.CARD_STATE_INFO_PRESENT;
8521 break;
8522 case CARDSTATE_ERROR:
8523 cardState = UiccSlotInfo.CARD_STATE_INFO_ERROR;
8524 break;
8525 case CARDSTATE_RESTRICTED:
8526 cardState = UiccSlotInfo.CARD_STATE_INFO_RESTRICTED;
8527 break;
8528 default:
8529 break;
8530
8531 }
8532
8533 infos[i] = new UiccSlotInfo(
8534 slot.isActive(),
8535 slot.isEuicc(),
8536 cardId,
8537 cardState,
8538 slot.getPhoneId(),
Jordan Liua2619582019-02-14 12:56:40 -08008539 slot.isExtendedApduSupported(),
8540 slot.isRemovable());
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008541 }
8542 return infos;
8543 } finally {
8544 Binder.restoreCallingIdentity(identity);
Holly Jiuyu Sun1d957c52018-04-04 13:52:42 -07008545 }
Holly Jiuyu Sun01c47ad2018-01-24 17:56:33 +00008546 }
8547
8548 @Override
8549 public boolean switchSlots(int[] physicalSlots) {
8550 enforceModifyPermission();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008551
8552 final long identity = Binder.clearCallingIdentity();
8553 try {
8554 return (Boolean) sendRequest(CMD_SWITCH_SLOTS, physicalSlots);
8555 } finally {
8556 Binder.restoreCallingIdentity(identity);
8557 }
Holly Jiuyu Sun01c47ad2018-01-24 17:56:33 +00008558 }
Jack Yu4c988042018-02-27 15:30:01 -08008559
8560 @Override
Jordan Liu7de49fa2018-12-06 14:48:49 -08008561 public int getCardIdForDefaultEuicc(int subId, String callingPackage) {
Jordan Liu7de49fa2018-12-06 14:48:49 -08008562 final long identity = Binder.clearCallingIdentity();
8563 try {
8564 return UiccController.getInstance().getCardIdForDefaultEuicc();
8565 } finally {
8566 Binder.restoreCallingIdentity(identity);
8567 }
8568 }
8569
Pengquan Meng85728fb2018-03-12 16:31:21 -07008570 /**
goneil47ffb6e2018-04-06 15:40:58 -07008571 * A test API to reload the UICC profile.
8572 *
8573 * <p>Requires that the calling app has permission
8574 * {@link android.Manifest.permission#MODIFY_PHONE_STATE MODIFY_PHONE_STATE}.
8575 * @hide
8576 */
8577 @Override
8578 public void refreshUiccProfile(int subId) {
8579 enforceModifyPermission();
8580
8581 final long identity = Binder.clearCallingIdentity();
8582 try {
8583 Phone phone = getPhone(subId);
8584 if (phone == null) {
8585 return;
8586 }
8587 UiccCard uiccCard = phone.getUiccCard();
8588 if (uiccCard == null) {
8589 return;
8590 }
8591 UiccProfile uiccProfile = uiccCard.getUiccProfile();
8592 if (uiccProfile == null) {
8593 return;
8594 }
8595 uiccProfile.refresh();
8596 } finally {
8597 Binder.restoreCallingIdentity(identity);
8598 }
8599 }
8600
8601 /**
Pengquan Meng85728fb2018-03-12 16:31:21 -07008602 * Returns false if the mobile data is disabled by default, otherwise return true.
8603 */
8604 private boolean getDefaultDataEnabled() {
Inseob Kim14bb3d02018-12-13 17:11:34 +09008605 return TelephonyProperties.mobile_data().orElse(true);
Pengquan Meng85728fb2018-03-12 16:31:21 -07008606 }
8607
8608 /**
8609 * Returns true if the data roaming is enabled by default, i.e the system property
8610 * of {@link #DEFAULT_DATA_ROAMING_PROPERTY_NAME} is true or the config of
8611 * {@link CarrierConfigManager#KEY_CARRIER_DEFAULT_DATA_ROAMING_ENABLED_BOOL} is true.
8612 */
8613 private boolean getDefaultDataRoamingEnabled(int subId) {
8614 final CarrierConfigManager configMgr = (CarrierConfigManager)
Nazanin Bakhshif782e562018-12-11 15:15:39 -08008615 mApp.getSystemService(Context.CARRIER_CONFIG_SERVICE);
Shuo Qian1d84a0e2020-07-15 12:36:44 -07008616 boolean isDataRoamingEnabled = TelephonyProperties.data_roaming().orElse(false);
Pengquan Meng85728fb2018-03-12 16:31:21 -07008617 isDataRoamingEnabled |= configMgr.getConfigForSubId(subId).getBoolean(
8618 CarrierConfigManager.KEY_CARRIER_DEFAULT_DATA_ROAMING_ENABLED_BOOL);
8619 return isDataRoamingEnabled;
8620 }
8621
8622 /**
8623 * Returns the default network type for the given {@code subId}, if the default network type is
8624 * not set, return {@link Phone#PREFERRED_NT_MODE}.
8625 */
8626 private int getDefaultNetworkType(int subId) {
Inseob Kim14bb3d02018-12-13 17:11:34 +09008627 List<Integer> list = TelephonyProperties.default_network();
8628 int phoneId = mSubscriptionController.getPhoneId(subId);
8629 if (phoneId >= 0 && phoneId < list.size() && list.get(phoneId) != null) {
8630 return list.get(phoneId);
8631 }
8632 return Phone.PREFERRED_NT_MODE;
Pengquan Meng85728fb2018-03-12 16:31:21 -07008633 }
fionaxua13278b2018-03-21 00:08:13 -07008634
8635 @Override
8636 public void setCarrierTestOverride(int subId, String mccmnc, String imsi, String iccid, String
chen xueaba88a2019-03-15 13:15:10 -07008637 gid1, String gid2, String plmn, String spn, String carrierPrivilegeRules, String apn) {
fionaxua13278b2018-03-21 00:08:13 -07008638 enforceModifyPermission();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008639
8640 final long identity = Binder.clearCallingIdentity();
8641 try {
8642 final Phone phone = getPhone(subId);
8643 if (phone == null) {
8644 loge("setCarrierTestOverride fails with invalid subId: " + subId);
8645 return;
8646 }
chen xueaba88a2019-03-15 13:15:10 -07008647 phone.setCarrierTestOverride(mccmnc, imsi, iccid, gid1, gid2, plmn, spn,
8648 carrierPrivilegeRules, apn);
Jeff Davidson8ab02b22020-03-28 12:24:40 -07008649 if (carrierPrivilegeRules == null) {
8650 mCarrierPrivilegeTestOverrideSubIds.remove(subId);
8651 } else {
8652 mCarrierPrivilegeTestOverrideSubIds.add(subId);
8653 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008654 } finally {
8655 Binder.restoreCallingIdentity(identity);
fionaxua13278b2018-03-21 00:08:13 -07008656 }
fionaxua13278b2018-03-21 00:08:13 -07008657 }
8658
8659 @Override
8660 public int getCarrierIdListVersion(int subId) {
Brad Ebinger35c841c2018-10-01 10:40:55 -07008661 enforceReadPrivilegedPermission("getCarrierIdListVersion");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008662
8663 final long identity = Binder.clearCallingIdentity();
8664 try {
8665 final Phone phone = getPhone(subId);
8666 if (phone == null) {
8667 loge("getCarrierIdListVersion fails with invalid subId: " + subId);
8668 return TelephonyManager.UNKNOWN_CARRIER_ID_LIST_VERSION;
8669 }
8670 return phone.getCarrierIdListVersion();
8671 } finally {
8672 Binder.restoreCallingIdentity(identity);
fionaxua13278b2018-03-21 00:08:13 -07008673 }
fionaxua13278b2018-03-21 00:08:13 -07008674 }
Malcolm Chen2c63d402018-08-14 16:00:53 -07008675
8676 @Override
Philip P. Moltmann700a9592019-10-03 11:53:50 -07008677 public int getNumberOfModemsWithSimultaneousDataConnections(int subId, String callingPackage,
8678 String callingFeatureId) {
Malcolm Chen2c63d402018-08-14 16:00:53 -07008679 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
Philip P. Moltmann700a9592019-10-03 11:53:50 -07008680 mApp, subId, callingPackage, callingFeatureId,
8681 "getNumberOfModemsWithSimultaneousDataConnections")) {
Malcolm Chen2c63d402018-08-14 16:00:53 -07008682 return -1;
8683 }
8684
8685 final long identity = Binder.clearCallingIdentity();
8686 try {
8687 return mPhoneConfigurationManager.getNumberOfModemsWithSimultaneousDataConnections();
8688 } finally {
8689 Binder.restoreCallingIdentity(identity);
8690 }
8691 }
Pengquan Menga1bb6272018-09-06 09:59:22 -07008692
8693 @Override
8694 public int getCdmaRoamingMode(int subId) {
zoey chen7e6d4e52019-12-17 18:18:59 +08008695 TelephonyPermissions
8696 .enforeceCallingOrSelfReadPrivilegedPhoneStatePermissionOrCarrierPrivilege(
Pengquan Menga1bb6272018-09-06 09:59:22 -07008697 mApp, subId, "getCdmaRoamingMode");
8698
8699 final long identity = Binder.clearCallingIdentity();
8700 try {
8701 return (int) sendRequest(CMD_GET_CDMA_ROAMING_MODE, null /* argument */, subId);
8702 } finally {
8703 Binder.restoreCallingIdentity(identity);
8704 }
8705 }
8706
8707 @Override
8708 public boolean setCdmaRoamingMode(int subId, int mode) {
8709 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
8710 mApp, subId, "setCdmaRoamingMode");
8711
8712 final long identity = Binder.clearCallingIdentity();
8713 try {
8714 return (boolean) sendRequest(CMD_SET_CDMA_ROAMING_MODE, mode, subId);
8715 } finally {
8716 Binder.restoreCallingIdentity(identity);
8717 }
8718 }
8719
8720 @Override
Sarah Chinbaab1432020-10-28 13:46:24 -07008721 public int getCdmaSubscriptionMode(int subId) {
8722 TelephonyPermissions
8723 .enforeceCallingOrSelfReadPrivilegedPhoneStatePermissionOrCarrierPrivilege(
8724 mApp, subId, "getCdmaSubscriptionMode");
8725
8726 final long identity = Binder.clearCallingIdentity();
8727 try {
8728 return (int) sendRequest(CMD_GET_CDMA_SUBSCRIPTION_MODE, null /* argument */, subId);
8729 } finally {
8730 Binder.restoreCallingIdentity(identity);
8731 }
8732 }
8733
8734 @Override
Pengquan Menga1bb6272018-09-06 09:59:22 -07008735 public boolean setCdmaSubscriptionMode(int subId, int mode) {
8736 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
8737 mApp, subId, "setCdmaSubscriptionMode");
8738
8739 final long identity = Binder.clearCallingIdentity();
8740 try {
8741 return (boolean) sendRequest(CMD_SET_CDMA_SUBSCRIPTION_MODE, mode, subId);
8742 } finally {
8743 Binder.restoreCallingIdentity(identity);
8744 }
8745 }
Makoto Onukida3bf792018-09-18 16:06:29 -07008746
sqianc5eccab2018-10-19 18:46:41 -07008747 @Override
sqian8c685422019-02-22 15:55:18 -08008748 public Map<Integer, List<EmergencyNumber>> getEmergencyNumberList(
Philip P. Moltmann700a9592019-10-03 11:53:50 -07008749 String callingPackage, String callingFeatureId) {
sqian11b7a0e2018-12-05 18:48:28 -08008750 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
Philip P. Moltmann700a9592019-10-03 11:53:50 -07008751 mApp, getDefaultSubscription(), callingPackage, callingFeatureId,
8752 "getEmergencyNumberList")) {
sqian11b7a0e2018-12-05 18:48:28 -08008753 throw new SecurityException("Requires READ_PHONE_STATE permission.");
8754 }
8755 final long identity = Binder.clearCallingIdentity();
8756 try {
sqian854d44b2018-12-12 16:48:18 -08008757 Map<Integer, List<EmergencyNumber>> emergencyNumberListInternal = new HashMap<>();
8758 for (Phone phone: PhoneFactory.getPhones()) {
8759 if (phone.getEmergencyNumberTracker() != null
8760 && phone.getEmergencyNumberTracker().getEmergencyNumberList() != null) {
8761 emergencyNumberListInternal.put(
8762 phone.getSubId(),
8763 phone.getEmergencyNumberTracker().getEmergencyNumberList());
8764 }
sqian11b7a0e2018-12-05 18:48:28 -08008765 }
sqian854d44b2018-12-12 16:48:18 -08008766 return emergencyNumberListInternal;
sqian11b7a0e2018-12-05 18:48:28 -08008767 } finally {
8768 Binder.restoreCallingIdentity(identity);
8769 }
sqianc5eccab2018-10-19 18:46:41 -07008770 }
8771
8772 @Override
sqian8c685422019-02-22 15:55:18 -08008773 public boolean isEmergencyNumber(String number, boolean exactMatch) {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08008774 final Phone defaultPhone = getDefaultPhone();
sqian11b7a0e2018-12-05 18:48:28 -08008775 if (!exactMatch) {
8776 TelephonyPermissions
8777 .enforeceCallingOrSelfReadPrivilegedPhoneStatePermissionOrCarrierPrivilege(
sqian8c685422019-02-22 15:55:18 -08008778 mApp, defaultPhone.getSubId(), "isEmergencyNumber(Potential)");
sqian11b7a0e2018-12-05 18:48:28 -08008779 }
8780 final long identity = Binder.clearCallingIdentity();
8781 try {
sqian854d44b2018-12-12 16:48:18 -08008782 for (Phone phone: PhoneFactory.getPhones()) {
8783 if (phone.getEmergencyNumberTracker() != null
Taesu Leee050c002020-10-13 17:19:35 +09008784 && phone.getEmergencyNumberTracker()
8785 .isEmergencyNumber(number, exactMatch)) {
8786 return true;
sqian11b7a0e2018-12-05 18:48:28 -08008787 }
sqian11b7a0e2018-12-05 18:48:28 -08008788 }
8789 return false;
8790 } finally {
8791 Binder.restoreCallingIdentity(identity);
8792 }
8793 }
8794
sqianf4ca7ed2019-01-15 18:32:07 -08008795 /**
Shuo Qianccbaf742021-02-22 18:32:21 -08008796 * Start emergency callback mode for GsmCdmaPhone for testing.
8797 */
8798 @Override
8799 public void startEmergencyCallbackMode() {
8800 TelephonyPermissions.enforceShellOnly(Binder.getCallingUid(),
8801 "startEmergencyCallbackMode");
8802 enforceModifyPermission();
8803 final long identity = Binder.clearCallingIdentity();
8804 try {
8805 for (Phone phone : PhoneFactory.getPhones()) {
8806 Rlog.d(LOG_TAG, "startEmergencyCallbackMode phone type: " + phone.getPhoneType());
8807 if (phone != null && ((phone.getPhoneType() == PHONE_TYPE_GSM)
8808 || (phone.getPhoneType() == PHONE_TYPE_CDMA))) {
8809 GsmCdmaPhone gsmCdmaPhone = (GsmCdmaPhone) phone;
8810 gsmCdmaPhone.obtainMessage(
8811 GsmCdmaPhone.EVENT_EMERGENCY_CALLBACK_MODE_ENTER).sendToTarget();
8812 Rlog.d(LOG_TAG, "startEmergencyCallbackMode: triggered");
8813 }
8814 }
8815 } finally {
8816 Binder.restoreCallingIdentity(identity);
8817 }
8818 }
8819
8820 /**
sqianf4ca7ed2019-01-15 18:32:07 -08008821 * Update emergency number list for test mode.
8822 */
8823 @Override
8824 public void updateEmergencyNumberListTestMode(int action, EmergencyNumber num) {
8825 TelephonyPermissions.enforceShellOnly(Binder.getCallingUid(),
8826 "updateEmergencyNumberListTestMode");
8827
8828 final long identity = Binder.clearCallingIdentity();
8829 try {
8830 for (Phone phone: PhoneFactory.getPhones()) {
8831 EmergencyNumberTracker tracker = phone.getEmergencyNumberTracker();
8832 if (tracker != null) {
8833 tracker.executeEmergencyNumberTestModeCommand(action, num);
8834 }
8835 }
8836 } finally {
8837 Binder.restoreCallingIdentity(identity);
8838 }
8839 }
8840
8841 /**
8842 * Get the full emergency number list for test mode.
8843 */
8844 @Override
8845 public List<String> getEmergencyNumberListTestMode() {
8846 TelephonyPermissions.enforceShellOnly(Binder.getCallingUid(),
8847 "getEmergencyNumberListTestMode");
8848
8849 final long identity = Binder.clearCallingIdentity();
8850 try {
8851 Set<String> emergencyNumbers = new HashSet<>();
8852 for (Phone phone: PhoneFactory.getPhones()) {
8853 EmergencyNumberTracker tracker = phone.getEmergencyNumberTracker();
8854 if (tracker != null) {
8855 for (EmergencyNumber num : tracker.getEmergencyNumberList()) {
8856 emergencyNumbers.add(num.getNumber());
8857 }
8858 }
8859 }
8860 return new ArrayList<>(emergencyNumbers);
8861 } finally {
8862 Binder.restoreCallingIdentity(identity);
8863 }
8864 }
8865
chen xud6b45bd2018-10-30 22:27:10 -07008866 @Override
Shuo Qian3b6ee772019-11-13 17:43:31 -08008867 public int getEmergencyNumberDbVersion(int subId) {
8868 enforceReadPrivilegedPermission("getEmergencyNumberDbVersion");
8869
8870 final long identity = Binder.clearCallingIdentity();
8871 try {
8872 final Phone phone = getPhone(subId);
8873 if (phone == null) {
8874 loge("getEmergencyNumberDbVersion fails with invalid subId: " + subId);
8875 return TelephonyManager.INVALID_EMERGENCY_NUMBER_DB_VERSION;
8876 }
8877 return phone.getEmergencyNumberDbVersion();
8878 } finally {
8879 Binder.restoreCallingIdentity(identity);
8880 }
8881 }
8882
8883 @Override
8884 public void notifyOtaEmergencyNumberDbInstalled() {
8885 enforceModifyPermission();
8886
8887 final long identity = Binder.clearCallingIdentity();
8888 try {
8889 for (Phone phone: PhoneFactory.getPhones()) {
8890 EmergencyNumberTracker tracker = phone.getEmergencyNumberTracker();
8891 if (tracker != null) {
8892 tracker.updateOtaEmergencyNumberDatabase();
8893 }
8894 }
8895 } finally {
8896 Binder.restoreCallingIdentity(identity);
8897 }
8898 }
8899
8900 @Override
Shuo Qianc373f112020-03-05 17:55:34 -08008901 public void updateOtaEmergencyNumberDbFilePath(ParcelFileDescriptor otaParcelFileDescriptor) {
Shuo Qian3b6ee772019-11-13 17:43:31 -08008902 enforceActiveEmergencySessionPermission();
8903
8904 final long identity = Binder.clearCallingIdentity();
8905 try {
8906 for (Phone phone: PhoneFactory.getPhones()) {
8907 EmergencyNumberTracker tracker = phone.getEmergencyNumberTracker();
8908 if (tracker != null) {
Shuo Qianc373f112020-03-05 17:55:34 -08008909 tracker.updateOtaEmergencyNumberDbFilePath(otaParcelFileDescriptor);
8910 }
8911 }
8912 } finally {
8913 Binder.restoreCallingIdentity(identity);
8914 }
8915 }
8916
8917 @Override
8918 public void resetOtaEmergencyNumberDbFilePath() {
8919 enforceActiveEmergencySessionPermission();
8920
8921 final long identity = Binder.clearCallingIdentity();
8922 try {
8923 for (Phone phone: PhoneFactory.getPhones()) {
8924 EmergencyNumberTracker tracker = phone.getEmergencyNumberTracker();
8925 if (tracker != null) {
8926 tracker.resetOtaEmergencyNumberDbFilePath();
Shuo Qian3b6ee772019-11-13 17:43:31 -08008927 }
8928 }
8929 } finally {
8930 Binder.restoreCallingIdentity(identity);
8931 }
8932 }
8933
8934 @Override
chen xud6b45bd2018-10-30 22:27:10 -07008935 public List<String> getCertsFromCarrierPrivilegeAccessRules(int subId) {
8936 enforceReadPrivilegedPermission("getCertsFromCarrierPrivilegeAccessRules");
8937 Phone phone = getPhone(subId);
8938 if (phone == null) {
8939 return null;
8940 }
8941 final long identity = Binder.clearCallingIdentity();
8942 try {
8943 UiccProfile profile = UiccController.getInstance()
8944 .getUiccProfileForPhone(phone.getPhoneId());
8945 if (profile != null) {
8946 return profile.getCertsFromCarrierPrivilegeAccessRules();
8947 }
8948 } finally {
8949 Binder.restoreCallingIdentity(identity);
8950 }
8951 return null;
8952 }
Malcolm Chen8e4ed912019-01-15 20:22:16 -08008953
8954 /**
8955 * Enable or disable a modem stack.
8956 */
8957 @Override
8958 public boolean enableModemForSlot(int slotIndex, boolean enable) {
8959 enforceModifyPermission();
8960
8961 final long identity = Binder.clearCallingIdentity();
8962 try {
8963 Phone phone = PhoneFactory.getPhone(slotIndex);
8964 if (phone == null) {
8965 return false;
8966 } else {
8967 return (Boolean) sendRequest(CMD_REQUEST_ENABLE_MODEM, enable, phone, null);
8968 }
8969 } finally {
8970 Binder.restoreCallingIdentity(identity);
8971 }
8972 }
Michelecea4cf22018-12-21 15:00:11 -08008973
Malcolm Chen4bcd9822019-03-27 18:34:05 -07008974 /**
8975 * Whether a modem stack is enabled or not.
8976 */
8977 @Override
Philip P. Moltmann700a9592019-10-03 11:53:50 -07008978 public boolean isModemEnabledForSlot(int slotIndex, String callingPackage,
8979 String callingFeatureId) {
Malcolm Chen4bcd9822019-03-27 18:34:05 -07008980 Phone phone = PhoneFactory.getPhone(slotIndex);
8981 if (phone == null) return false;
8982
8983 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
Philip P. Moltmann700a9592019-10-03 11:53:50 -07008984 mApp, phone.getSubId(), callingPackage, callingFeatureId,
8985 "isModemEnabledForSlot")) {
Malcolm Chen4bcd9822019-03-27 18:34:05 -07008986 throw new SecurityException("Requires READ_PHONE_STATE permission.");
8987 }
8988
8989 final long identity = Binder.clearCallingIdentity();
8990 try {
Nazanin Bakhshif71371d2019-04-29 17:29:44 -07008991 try {
8992 return mPhoneConfigurationManager.getPhoneStatusFromCache(phone.getPhoneId());
8993 } catch (NoSuchElementException ex) {
8994 return (Boolean) sendRequest(CMD_GET_MODEM_STATUS, null, phone, null);
8995 }
Malcolm Chen4bcd9822019-03-27 18:34:05 -07008996 } finally {
8997 Binder.restoreCallingIdentity(identity);
8998 }
8999 }
9000
Michelecea4cf22018-12-21 15:00:11 -08009001 @Override
Michele0ea7d782019-03-19 14:58:42 -07009002 public void setMultiSimCarrierRestriction(boolean isMultiSimCarrierRestricted) {
Michelecea4cf22018-12-21 15:00:11 -08009003 enforceModifyPermission();
9004
9005 final long identity = Binder.clearCallingIdentity();
9006 try {
9007 mTelephonySharedPreferences.edit()
Michele0ea7d782019-03-19 14:58:42 -07009008 .putBoolean(PREF_MULTI_SIM_RESTRICTED, isMultiSimCarrierRestricted)
Michelecea4cf22018-12-21 15:00:11 -08009009 .commit();
9010 } finally {
9011 Binder.restoreCallingIdentity(identity);
9012 }
9013 }
9014
9015 @Override
Michele0ea7d782019-03-19 14:58:42 -07009016 @TelephonyManager.IsMultiSimSupportedResult
Philip P. Moltmann700a9592019-10-03 11:53:50 -07009017 public int isMultiSimSupported(String callingPackage, String callingFeatureId) {
Michele4245e952019-02-04 11:36:23 -08009018 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(mApp,
Philip P. Moltmann700a9592019-10-03 11:53:50 -07009019 getDefaultPhone().getSubId(), callingPackage, callingFeatureId,
9020 "isMultiSimSupported")) {
Michele0ea7d782019-03-19 14:58:42 -07009021 return TelephonyManager.MULTISIM_NOT_SUPPORTED_BY_HARDWARE;
Michele4245e952019-02-04 11:36:23 -08009022 }
Michelecea4cf22018-12-21 15:00:11 -08009023
9024 final long identity = Binder.clearCallingIdentity();
9025 try {
Michele0ea7d782019-03-19 14:58:42 -07009026 return isMultiSimSupportedInternal();
Michelecea4cf22018-12-21 15:00:11 -08009027 } finally {
9028 Binder.restoreCallingIdentity(identity);
9029 }
9030 }
Nazanin Bakhshi628473f2019-01-29 17:37:52 -08009031
Michele0ea7d782019-03-19 14:58:42 -07009032 @TelephonyManager.IsMultiSimSupportedResult
9033 private int isMultiSimSupportedInternal() {
Michele30b57b22019-03-01 12:01:14 -08009034 // If the device has less than 2 SIM cards, indicate that multisim is restricted.
9035 int numPhysicalSlots = UiccController.getInstance().getUiccSlots().length;
9036 if (numPhysicalSlots < 2) {
Michele0ea7d782019-03-19 14:58:42 -07009037 loge("isMultiSimSupportedInternal: requires at least 2 cards");
9038 return TelephonyManager.MULTISIM_NOT_SUPPORTED_BY_HARDWARE;
Michele30b57b22019-03-01 12:01:14 -08009039 }
9040 // Check if the hardware supports multisim functionality. If usage of multisim is not
9041 // supported by the modem, indicate that it is restricted.
9042 PhoneCapability staticCapability =
9043 mPhoneConfigurationManager.getStaticPhoneCapability();
9044 if (staticCapability == null) {
Michele0ea7d782019-03-19 14:58:42 -07009045 loge("isMultiSimSupportedInternal: no static configuration available");
9046 return TelephonyManager.MULTISIM_NOT_SUPPORTED_BY_HARDWARE;
Michele30b57b22019-03-01 12:01:14 -08009047 }
SongFerngWang8236caa2021-01-17 21:51:44 +08009048 if (staticCapability.getLogicalModemList().size() < 2) {
Michele0ea7d782019-03-19 14:58:42 -07009049 loge("isMultiSimSupportedInternal: maximum number of modem is < 2");
9050 return TelephonyManager.MULTISIM_NOT_SUPPORTED_BY_HARDWARE;
Michele30b57b22019-03-01 12:01:14 -08009051 }
9052 // Check if support of multiple SIMs is restricted by carrier
9053 if (mTelephonySharedPreferences.getBoolean(PREF_MULTI_SIM_RESTRICTED, false)) {
Michele0ea7d782019-03-19 14:58:42 -07009054 return TelephonyManager.MULTISIM_NOT_SUPPORTED_BY_CARRIER;
Michele30b57b22019-03-01 12:01:14 -08009055 }
9056
Michele0ea7d782019-03-19 14:58:42 -07009057 return TelephonyManager.MULTISIM_ALLOWED;
Michele30b57b22019-03-01 12:01:14 -08009058 }
9059
Nazanin Bakhshi628473f2019-01-29 17:37:52 -08009060 /**
9061 * Switch configs to enable multi-sim or switch back to single-sim
Nazanin Bakhshi17318782019-03-01 11:56:08 -08009062 * Note: Switch from multi-sim to single-sim is only possible with MODIFY_PHONE_STATE
9063 * permission, but the other way around is possible with either MODIFY_PHONE_STATE
9064 * or carrier privileges
Nazanin Bakhshi628473f2019-01-29 17:37:52 -08009065 * @param numOfSims number of active sims we want to switch to
9066 */
9067 @Override
9068 public void switchMultiSimConfig(int numOfSims) {
Nazanin Bakhshi17318782019-03-01 11:56:08 -08009069 if (numOfSims == 1) {
9070 enforceModifyPermission();
9071 } else {
9072 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
9073 mApp, SubscriptionManager.DEFAULT_SUBSCRIPTION_ID, "switchMultiSimConfig");
9074 }
Nazanin Bakhshi628473f2019-01-29 17:37:52 -08009075 final long identity = Binder.clearCallingIdentity();
Michele30b57b22019-03-01 12:01:14 -08009076
Nazanin Bakhshi628473f2019-01-29 17:37:52 -08009077 try {
Michele30b57b22019-03-01 12:01:14 -08009078 //only proceed if multi-sim is not restricted
Michele0ea7d782019-03-19 14:58:42 -07009079 if (isMultiSimSupportedInternal() != TelephonyManager.MULTISIM_ALLOWED) {
Michele30b57b22019-03-01 12:01:14 -08009080 loge("switchMultiSimConfig not possible. It is restricted or not supported.");
9081 return;
9082 }
Nazanin Bakhshi628473f2019-01-29 17:37:52 -08009083 mPhoneConfigurationManager.switchMultiSimConfig(numOfSims);
9084 } finally {
9085 Binder.restoreCallingIdentity(identity);
9086 }
9087 }
9088
Hyungjun Parkbb07fde2019-01-10 15:28:51 +09009089 @Override
9090 public boolean isApplicationOnUicc(int subId, int appType) {
9091 enforceReadPrivilegedPermission("isApplicationOnUicc");
9092 Phone phone = getPhone(subId);
9093 if (phone == null) {
9094 return false;
9095 }
9096 final long identity = Binder.clearCallingIdentity();
9097 try {
9098 UiccCard uiccCard = phone.getUiccCard();
9099 if (uiccCard == null) {
9100 return false;
9101 }
9102 UiccProfile uiccProfile = uiccCard.getUiccProfile();
9103 if (uiccProfile == null) {
9104 return false;
9105 }
9106 if (TelephonyManager.APPTYPE_SIM <= appType
9107 && appType <= TelephonyManager.APPTYPE_ISIM) {
9108 return uiccProfile.isApplicationOnIcc(AppType.values()[appType]);
9109 }
9110 return false;
9111 } finally {
9112 Binder.restoreCallingIdentity(identity);
9113 }
9114 }
9115
Nazanin Bakhshi628473f2019-01-29 17:37:52 -08009116 /**
chen xub4baa772019-04-03 10:23:41 -07009117 * Get whether making changes to modem configurations will trigger reboot.
9118 * Return value defaults to true.
Nazanin Bakhshi5fe5ef22019-01-30 10:52:09 -08009119 */
9120 @Override
Philip P. Moltmann700a9592019-10-03 11:53:50 -07009121 public boolean doesSwitchMultiSimConfigTriggerReboot(int subId, String callingPackage,
9122 String callingFeatureId) {
chen xub4baa772019-04-03 10:23:41 -07009123 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
Philip P. Moltmann700a9592019-10-03 11:53:50 -07009124 mApp, subId, callingPackage, callingFeatureId,
9125 "doesSwitchMultiSimConfigTriggerReboot")) {
chen xub4baa772019-04-03 10:23:41 -07009126 return false;
9127 }
Nazanin Bakhshi5fe5ef22019-01-30 10:52:09 -08009128 final long identity = Binder.clearCallingIdentity();
9129 try {
9130 return mPhoneConfigurationManager.isRebootRequiredForModemConfigChange();
9131 } finally {
9132 Binder.restoreCallingIdentity(identity);
9133 }
9134 }
9135
Nathan Harold29f5f052019-02-15 13:41:57 -08009136 private void updateModemStateMetrics() {
9137 TelephonyMetrics metrics = TelephonyMetrics.getInstance();
9138 // TODO: check the state for each modem if the api is ready.
9139 metrics.updateEnabledModemBitmap((1 << TelephonyManager.from(mApp).getPhoneCount()) - 1);
9140 }
9141
Pengquan Meng3889a572019-01-23 11:16:29 -08009142 @Override
9143 public int[] getSlotsMapping() {
9144 enforceReadPrivilegedPermission("getSlotsMapping");
9145
9146 final long identity = Binder.clearCallingIdentity();
9147 try {
9148 int phoneCount = TelephonyManager.getDefault().getPhoneCount();
9149 // All logical slots should have a mapping to a physical slot.
9150 int[] logicalSlotsMapping = new int[phoneCount];
9151 UiccSlotInfo[] slotInfos = getUiccSlotsInfo();
9152 for (int i = 0; i < slotInfos.length; i++) {
9153 if (SubscriptionManager.isValidPhoneId(slotInfos[i].getLogicalSlotIdx())) {
9154 logicalSlotsMapping[slotInfos[i].getLogicalSlotIdx()] = i;
9155 }
9156 }
9157 return logicalSlotsMapping;
9158 } finally {
9159 Binder.restoreCallingIdentity(identity);
9160 }
9161 }
Nathan Harold48d6fd52019-02-06 19:01:40 -08009162
9163 /**
9164 * Get the IRadio HAL Version
9165 */
9166 @Override
9167 public int getRadioHalVersion() {
9168 Phone phone = getDefaultPhone();
9169 if (phone == null) return -1;
9170 HalVersion hv = phone.getHalVersion();
9171 if (hv.equals(HalVersion.UNKNOWN)) return -1;
9172 return hv.major * 100 + hv.minor;
9173 }
Malcolm Chendc8c10e2019-04-10 18:25:07 -07009174
9175 /**
Shuo Qianda2d6ec2020-01-14 15:18:28 -08009176 * Get the current calling package name.
9177 * @return the current calling package name
9178 */
9179 @Override
9180 public String getCurrentPackageName() {
9181 return mApp.getPackageManager().getPackagesForUid(Binder.getCallingUid())[0];
9182 }
9183
9184 /**
Malcolm Chene5ad5792019-04-18 13:51:02 -07009185 * Return whether data is enabled for certain APN type. This will tell if framework will accept
9186 * corresponding network requests on a subId.
9187 *
9188 * Data is enabled if:
Malcolm Chendc8c10e2019-04-10 18:25:07 -07009189 * 1) user data is turned on, or
Malcolm Chene5ad5792019-04-18 13:51:02 -07009190 * 2) APN is un-metered for this subscription, or
9191 * 3) APN type is whitelisted. E.g. MMS is whitelisted if
Hall Liu746e03c2020-09-25 11:13:49 -07009192 * {@link TelephonyManager#MOBILE_DATA_POLICY_MMS_ALWAYS_ALLOWED} is enabled.
Malcolm Chene5ad5792019-04-18 13:51:02 -07009193 *
9194 * @return whether data is allowed for a apn type.
9195 *
9196 * @hide
Malcolm Chendc8c10e2019-04-10 18:25:07 -07009197 */
9198 @Override
Malcolm Chene5ad5792019-04-18 13:51:02 -07009199 public boolean isDataEnabledForApn(int apnType, int subId, String callingPackage) {
Amit Mahajan5d4e1922019-10-07 16:20:43 -07009200 enforceReadPrivilegedPermission("Needs READ_PRIVILEGED_PHONE_STATE for "
9201 + "isDataEnabledForApn");
Malcolm Chendc8c10e2019-04-10 18:25:07 -07009202
9203 // Now that all security checks passes, perform the operation as ourselves.
9204 final long identity = Binder.clearCallingIdentity();
9205 try {
9206 Phone phone = getPhone(subId);
9207 if (phone == null) return false;
9208
Jack Yu41407ee2019-05-13 16:54:09 -07009209 boolean isMetered = ApnSettingUtils.isMeteredApnType(apnType, phone);
Malcolm Chene5ad5792019-04-18 13:51:02 -07009210 return !isMetered || phone.getDataEnabledSettings().isDataEnabled(apnType);
9211 } finally {
9212 Binder.restoreCallingIdentity(identity);
9213 }
9214 }
9215
9216 @Override
Jack Yu41407ee2019-05-13 16:54:09 -07009217 public boolean isApnMetered(@ApnType int apnType, int subId) {
Malcolm Chene5ad5792019-04-18 13:51:02 -07009218 enforceReadPrivilegedPermission("isApnMetered");
9219
9220 // Now that all security checks passes, perform the operation as ourselves.
9221 final long identity = Binder.clearCallingIdentity();
9222 try {
9223 Phone phone = getPhone(subId);
9224 if (phone == null) return true; // By default return true.
9225
Jack Yu41407ee2019-05-13 16:54:09 -07009226 return ApnSettingUtils.isMeteredApnType(apnType, phone);
Malcolm Chendc8c10e2019-04-10 18:25:07 -07009227 } finally {
9228 Binder.restoreCallingIdentity(identity);
9229 }
9230 }
Brad Ebingera63db5f2019-04-23 16:31:13 -07009231
9232 @Override
Hall Liu73f5d362020-01-20 13:42:00 -08009233 public void setSystemSelectionChannels(List<RadioAccessSpecifier> specifiers,
9234 int subscriptionId, IBooleanConsumer resultCallback) {
9235 enforceModifyPermission();
9236 long token = Binder.clearCallingIdentity();
9237 try {
9238 Phone phone = getPhone(subscriptionId);
9239 if (phone == null) {
9240 try {
9241 if (resultCallback != null) {
9242 resultCallback.accept(false);
9243 }
9244 } catch (RemoteException e) {
9245 // ignore
9246 }
9247 return;
9248 }
9249 Pair<List<RadioAccessSpecifier>, Consumer<Boolean>> argument =
9250 Pair.create(specifiers, (x) -> {
9251 try {
9252 if (resultCallback != null) {
9253 resultCallback.accept(x);
9254 }
9255 } catch (RemoteException e) {
9256 // ignore
9257 }
9258 });
9259 sendRequestAsync(CMD_SET_SYSTEM_SELECTION_CHANNELS, argument, phone, null);
9260 } finally {
9261 Binder.restoreCallingIdentity(token);
9262 }
9263 }
9264
9265 @Override
Sarah Chin679c08a2020-11-18 13:39:35 -08009266 public List<RadioAccessSpecifier> getSystemSelectionChannels(int subId) {
9267 TelephonyPermissions
9268 .enforeceCallingOrSelfReadPrivilegedPhoneStatePermissionOrCarrierPrivilege(
9269 mApp, subId, "getSystemSelectionChannels");
9270 WorkSource workSource = getWorkSource(Binder.getCallingUid());
9271 final long identity = Binder.clearCallingIdentity();
9272 try {
9273 List<RadioAccessSpecifier> specifiers =
9274 (List<RadioAccessSpecifier>) sendRequest(CMD_GET_SYSTEM_SELECTION_CHANNELS,
9275 null, subId, workSource);
9276 if (DBG) log("getSystemSelectionChannels: " + specifiers);
9277 return specifiers;
9278 } finally {
9279 Binder.restoreCallingIdentity(identity);
9280 }
9281 }
9282
9283 @Override
changbetty7157e9e2019-12-06 18:16:37 +08009284 public boolean isMvnoMatched(int subId, int mvnoType, @NonNull String mvnoMatchData) {
changbettyca3d40d2020-03-03 16:27:31 +08009285 enforceReadPrivilegedPermission("isMvnoMatched");
changbetty7157e9e2019-12-06 18:16:37 +08009286 IccRecords iccRecords = UiccController.getInstance().getIccRecords(
9287 SubscriptionManager.getPhoneId(subId), UiccController.APP_FAM_3GPP);
9288 if (iccRecords == null) {
9289 Log.d(LOG_TAG, "isMvnoMatched# IccRecords is null");
9290 return false;
9291 }
9292 return ApnSettingUtils.mvnoMatches(iccRecords, mvnoType, mvnoMatchData);
9293 }
9294
9295 @Override
Philip P. Moltmannd02b7372020-03-18 17:06:12 -07009296 public void enqueueSmsPickResult(String callingPackage, String callingAttributionTag,
9297 IIntegerConsumer pendingSubIdResult) {
Shuo Qianda2d6ec2020-01-14 15:18:28 -08009298 if (callingPackage == null) {
9299 callingPackage = getCurrentPackageName();
9300 }
Brad Ebingera63db5f2019-04-23 16:31:13 -07009301 SmsPermissions permissions = new SmsPermissions(getDefaultPhone(), mApp,
9302 (AppOpsManager) mApp.getSystemService(Context.APP_OPS_SERVICE));
Philip P. Moltmannd02b7372020-03-18 17:06:12 -07009303 if (!permissions.checkCallingCanSendSms(callingPackage, callingAttributionTag,
9304 "Sending message")) {
Brad Ebingera63db5f2019-04-23 16:31:13 -07009305 throw new SecurityException("Requires SEND_SMS permission to perform this operation");
9306 }
9307 PickSmsSubscriptionActivity.addPendingResult(pendingSubIdResult);
9308 Intent intent = new Intent();
9309 intent.setClass(mApp, PickSmsSubscriptionActivity.class);
9310 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
9311 // Bring up choose default SMS subscription dialog right now
9312 intent.putExtra(PickSmsSubscriptionActivity.DIALOG_TYPE_KEY,
9313 PickSmsSubscriptionActivity.SMS_PICK_FOR_MESSAGE);
9314 mApp.startActivity(intent);
9315 }
chen xud5ca2d52019-05-28 15:20:57 -07009316
9317 @Override
9318 public String getMmsUAProfUrl(int subId) {
9319 //TODO investigate if this API should require proper permission check in R b/133791609
9320 final long identity = Binder.clearCallingIdentity();
9321 try {
Guoqiang.Qiu171383d2020-07-13 09:38:32 +08009322 String carrierUAProfUrl = mApp.getCarrierConfigForSubId(subId).getString(
9323 CarrierConfigManager.KEY_MMS_UA_PROF_URL_STRING);
9324 if (!TextUtils.isEmpty(carrierUAProfUrl)) {
9325 return carrierUAProfUrl;
9326 }
Daniel Brightebb4eb72020-02-18 15:16:33 -08009327 return SubscriptionManager.getResourcesForSubId(getDefaultPhone().getContext(), subId)
9328 .getString(com.android.internal.R.string.config_mms_user_agent_profile_url);
chen xud5ca2d52019-05-28 15:20:57 -07009329 } finally {
9330 Binder.restoreCallingIdentity(identity);
9331 }
9332 }
9333
9334 @Override
9335 public String getMmsUserAgent(int subId) {
9336 //TODO investigate if this API should require proper permission check in R b/133791609
9337 final long identity = Binder.clearCallingIdentity();
9338 try {
Guoqiang.Qiu171383d2020-07-13 09:38:32 +08009339 String carrierUserAgent = mApp.getCarrierConfigForSubId(subId).getString(
9340 CarrierConfigManager.KEY_MMS_USER_AGENT_STRING);
9341 if (!TextUtils.isEmpty(carrierUserAgent)) {
9342 return carrierUserAgent;
9343 }
Daniel Brightebb4eb72020-02-18 15:16:33 -08009344 return SubscriptionManager.getResourcesForSubId(getDefaultPhone().getContext(), subId)
9345 .getString(com.android.internal.R.string.config_mms_user_agent);
chen xud5ca2d52019-05-28 15:20:57 -07009346 } finally {
9347 Binder.restoreCallingIdentity(identity);
9348 }
9349 }
Jack Yub07d4972019-05-28 16:12:25 -07009350
9351 @Override
Hall Liua62f5da2020-09-25 10:42:19 -07009352 public boolean isMobileDataPolicyEnabled(int subscriptionId, int policy) {
9353 enforceReadPrivilegedPermission("isMobileDataPolicyEnabled");
Jack Yub07d4972019-05-28 16:12:25 -07009354
Jack Yub07d4972019-05-28 16:12:25 -07009355 final long identity = Binder.clearCallingIdentity();
9356 try {
Hall Liua62f5da2020-09-25 10:42:19 -07009357 Phone phone = getPhone(subscriptionId);
Jack Yub07d4972019-05-28 16:12:25 -07009358 if (phone == null) return false;
9359
Hall Liua62f5da2020-09-25 10:42:19 -07009360 switch (policy) {
9361 case TelephonyManager.MOBILE_DATA_POLICY_DATA_ON_NON_DEFAULT_DURING_VOICE_CALL:
9362 return phone.getDataEnabledSettings().isDataAllowedInVoiceCall();
9363 case TelephonyManager.MOBILE_DATA_POLICY_MMS_ALWAYS_ALLOWED:
9364 return phone.getDataEnabledSettings().isMmsAlwaysAllowed();
9365 default:
9366 throw new IllegalArgumentException(policy + " is not a valid policy");
9367 }
Jack Yub07d4972019-05-28 16:12:25 -07009368 } finally {
9369 Binder.restoreCallingIdentity(identity);
9370 }
9371 }
9372
9373 @Override
Hall Liuc66bb112021-02-02 12:09:32 -08009374 public void setMobileDataPolicyEnabled(int subscriptionId, int policy,
Hall Liua62f5da2020-09-25 10:42:19 -07009375 boolean enabled) {
changbettyd5c246e2019-12-24 15:40:37 +08009376 enforceModifyPermission();
9377
changbettyd5c246e2019-12-24 15:40:37 +08009378 final long identity = Binder.clearCallingIdentity();
9379 try {
Hall Liua62f5da2020-09-25 10:42:19 -07009380 Phone phone = getPhone(subscriptionId);
9381 if (phone == null) return;
changbettyd5c246e2019-12-24 15:40:37 +08009382
Hall Liua62f5da2020-09-25 10:42:19 -07009383 switch (policy) {
9384 case TelephonyManager.MOBILE_DATA_POLICY_DATA_ON_NON_DEFAULT_DURING_VOICE_CALL:
9385 phone.getDataEnabledSettings().setAllowDataDuringVoiceCall(enabled);
9386 break;
9387 case TelephonyManager.MOBILE_DATA_POLICY_MMS_ALWAYS_ALLOWED:
9388 phone.getDataEnabledSettings().setAlwaysAllowMmsData(enabled);
9389 break;
9390 default:
9391 throw new IllegalArgumentException(policy + " is not a valid policy");
9392 }
changbettyd5c246e2019-12-24 15:40:37 +08009393 } finally {
9394 Binder.restoreCallingIdentity(identity);
9395 }
9396 }
9397
Tyler Gunn7bcdc742019-10-04 15:56:59 -07009398 /**
Hall Liu746e03c2020-09-25 11:13:49 -07009399 * Updates whether conference event package handling is enabled.
Tyler Gunn7bcdc742019-10-04 15:56:59 -07009400 * @param isCepEnabled {@code true} if CEP handling is enabled (default), or {@code false}
9401 * otherwise.
9402 */
9403 @Override
9404 public void setCepEnabled(boolean isCepEnabled) {
9405 TelephonyPermissions.enforceShellOnly(Binder.getCallingUid(), "setCepEnabled");
9406
9407 final long identity = Binder.clearCallingIdentity();
9408 try {
9409 Rlog.i(LOG_TAG, "setCepEnabled isCepEnabled=" + isCepEnabled);
9410 for (Phone phone : PhoneFactory.getPhones()) {
9411 Phone defaultPhone = phone.getImsPhone();
9412 if (defaultPhone != null && defaultPhone.getPhoneType() == PHONE_TYPE_IMS) {
9413 ImsPhone imsPhone = (ImsPhone) defaultPhone;
9414 ImsPhoneCallTracker imsPhoneCallTracker =
9415 (ImsPhoneCallTracker) imsPhone.getCallTracker();
9416 imsPhoneCallTracker.setConferenceEventPackageEnabled(isCepEnabled);
9417 Rlog.i(LOG_TAG, "setCepEnabled isCepEnabled=" + isCepEnabled + ", for imsPhone "
9418 + imsPhone.getMsisdn());
9419 }
9420 }
9421 } finally {
9422 Binder.restoreCallingIdentity(identity);
9423 }
9424 }
allenwtsu46dcc572020-01-08 18:24:03 +08009425
9426 /**
9427 * Notify that an RCS autoconfiguration XML file has been received for provisioning.
9428 *
9429 * @param config The XML file to be read. ASCII/UTF8 encoded text if not compressed.
9430 * @param isCompressed The XML file is compressed in gzip format and must be decompressed
9431 * before being read.
9432 */
9433 @Override
9434 public void notifyRcsAutoConfigurationReceived(int subId, @NonNull byte[] config, boolean
9435 isCompressed) {
9436 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
9437 mApp, subId, "notifyRcsAutoConfigurationReceived");
Hui Wang761a6682020-10-31 05:12:53 +00009438 if (!SubscriptionManager.isValidSubscriptionId(subId)) {
9439 throw new IllegalArgumentException("Invalid Subscription ID: " + subId);
9440 }
9441 if (!isImsAvailableOnDevice()) {
9442 throw new ServiceSpecificException(ImsException.CODE_ERROR_UNSUPPORTED_OPERATION,
9443 "IMS not available on device.");
9444 }
9445
9446 final long identity = Binder.clearCallingIdentity();
allenwtsu46dcc572020-01-08 18:24:03 +08009447 try {
Hui Wang761a6682020-10-31 05:12:53 +00009448 RcsProvisioningMonitor.getInstance().updateConfig(subId, config, isCompressed);
9449 } finally {
9450 Binder.restoreCallingIdentity(identity);
allenwtsu46dcc572020-01-08 18:24:03 +08009451 }
9452 }
zoey chene02881a2019-12-30 16:11:23 +08009453
9454 @Override
9455 public boolean isIccLockEnabled(int subId) {
9456 enforceReadPrivilegedPermission("isIccLockEnabled");
9457
9458 // Now that all security checks passes, perform the operation as ourselves.
9459 final long identity = Binder.clearCallingIdentity();
9460 try {
9461 Phone phone = getPhone(subId);
9462 if (phone != null && phone.getIccCard() != null) {
9463 return phone.getIccCard().getIccLockEnabled();
9464 } else {
9465 return false;
9466 }
9467 } finally {
9468 Binder.restoreCallingIdentity(identity);
9469 }
9470 }
9471
9472 /**
9473 * Set the ICC pin lock enabled or disabled.
9474 *
9475 * @return an integer representing the status of IccLock enabled or disabled in the following
9476 * three cases:
9477 * - {@link TelephonyManager#CHANGE_ICC_LOCK_SUCCESS} if enabled or disabled IccLock
9478 * successfully.
9479 * - Positive number and zero for remaining password attempts.
9480 * - Negative number for other failure cases (such like enabling/disabling PIN failed).
9481 *
9482 */
9483 @Override
9484 public int setIccLockEnabled(int subId, boolean enabled, String password) {
9485 enforceModifyPermission();
9486
9487 Phone phone = getPhone(subId);
9488 if (phone == null) {
9489 return 0;
9490 }
9491 // Now that all security checks passes, perform the operation as ourselves.
9492 final long identity = Binder.clearCallingIdentity();
9493 try {
9494 int attemptsRemaining = (int) sendRequest(CMD_SET_ICC_LOCK_ENABLED,
9495 new Pair<Boolean, String>(enabled, password), phone, null);
9496 return attemptsRemaining;
9497
9498 } catch (Exception e) {
9499 Log.e(LOG_TAG, "setIccLockEnabled. Exception e =" + e);
9500 } finally {
9501 Binder.restoreCallingIdentity(identity);
9502 }
9503 return 0;
9504 }
9505
9506 /**
9507 * Change the ICC password used in ICC pin lock.
9508 *
9509 * @return an integer representing the status of IccLock changed in the following three cases:
9510 * - {@link TelephonyManager#CHANGE_ICC_LOCK_SUCCESS} if changed IccLock successfully.
9511 * - Positive number and zero for remaining password attempts.
9512 * - Negative number for other failure cases (such like enabling/disabling PIN failed).
9513 *
9514 */
9515 @Override
9516 public int changeIccLockPassword(int subId, String oldPassword, String newPassword) {
9517 enforceModifyPermission();
9518
9519 Phone phone = getPhone(subId);
9520 if (phone == null) {
9521 return 0;
9522 }
9523 // Now that all security checks passes, perform the operation as ourselves.
9524 final long identity = Binder.clearCallingIdentity();
9525 try {
9526 int attemptsRemaining = (int) sendRequest(CMD_CHANGE_ICC_LOCK_PASSWORD,
9527 new Pair<String, String>(oldPassword, newPassword), phone, null);
9528 return attemptsRemaining;
9529
9530 } catch (Exception e) {
9531 Log.e(LOG_TAG, "changeIccLockPassword. Exception e =" + e);
9532 } finally {
9533 Binder.restoreCallingIdentity(identity);
9534 }
9535 return 0;
9536 }
Peter Wangdafb9ac2020-01-15 14:13:38 -08009537
9538 /**
9539 * Request for receiving user activity notification
9540 */
9541 @Override
9542 public void requestUserActivityNotification() {
9543 if (!mNotifyUserActivity.get()
9544 && !mMainThreadHandler.hasMessages(MSG_NOTIFY_USER_ACTIVITY)) {
9545 mNotifyUserActivity.set(true);
9546 }
9547 }
9548
9549 /**
9550 * Called when userActivity is signalled in the power manager.
9551 * This is safe to call from any thread, with any window manager locks held or not.
9552 */
9553 @Override
9554 public void userActivity() {
9555 // ***************************************
9556 // * Inherited from PhoneWindowManager *
9557 // ***************************************
9558 // THIS IS CALLED FROM DEEP IN THE POWER MANAGER
9559 // WITH ITS LOCKS HELD.
9560 //
9561 // This code must be VERY careful about the locks
9562 // it acquires.
9563 // In fact, the current code acquires way too many,
9564 // and probably has lurking deadlocks.
9565
9566 if (Binder.getCallingUid() != Process.SYSTEM_UID) {
9567 throw new SecurityException("Only the OS may call notifyUserActivity()");
9568 }
9569
9570 if (mNotifyUserActivity.getAndSet(false)) {
9571 mMainThreadHandler.sendEmptyMessageDelayed(MSG_NOTIFY_USER_ACTIVITY,
9572 USER_ACTIVITY_NOTIFICATION_DELAY);
9573 }
9574 }
Malcolm Chen4639c562020-04-13 11:59:40 -07009575
9576 @Override
9577 public boolean canConnectTo5GInDsdsMode() {
9578 return mApp.getResources().getBoolean(R.bool.config_5g_connection_in_dsds_mode);
9579 }
Jack Yud10cdd42020-09-28 20:28:01 -07009580
9581 @Override
9582 public @NonNull List<String> getEquivalentHomePlmns(int subId, String callingPackage,
9583 String callingFeatureId) {
9584 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
9585 mApp, subId, callingPackage, callingFeatureId, "getEquivalentHomePlmns")) {
9586 throw new SecurityException("Requires READ_PHONE_STATE permission.");
9587 }
9588
9589 Phone phone = getPhone(subId);
9590 if (phone == null) {
9591 throw new RuntimeException("phone is not available");
9592 }
9593 // Now that all security checks passes, perform the operation as ourselves.
9594 final long identity = Binder.clearCallingIdentity();
9595 try {
9596 return phone.getEquivalentHomePlmns();
9597 } finally {
9598 Binder.restoreCallingIdentity(identity);
9599 }
9600 }
Daniel Bright59e67312020-11-13 11:49:37 -08009601
9602 @Override
9603 public boolean isRadioInterfaceCapabilitySupported(
Daniel Bright95a4c1f2021-02-11 09:57:16 -08009604 final @NonNull @TelephonyManager.RadioInterfaceCapability String capability) {
9605 Set<String> radioInterfaceCapabilities =
Daniel Bright94f43662021-03-01 14:43:40 -08009606 mRadioInterfaceCapabilities.getCapabilities();
Daniel Bright59e67312020-11-13 11:49:37 -08009607 if (radioInterfaceCapabilities == null) {
9608 throw new RuntimeException("radio interface capabilities are not available");
Daniel Bright59e67312020-11-13 11:49:37 -08009609 }
Daniel Bright95a4c1f2021-02-11 09:57:16 -08009610 return radioInterfaceCapabilities.contains(capability);
Daniel Bright59e67312020-11-13 11:49:37 -08009611 }
Jack Nudelmanb0b87642020-11-12 15:04:39 -08009612
Hui Wang641e81c2020-10-12 12:14:23 -07009613 @Override
9614 public void bootstrapAuthenticationRequest(int subId, int appType, Uri nafUrl,
9615 UaSecurityProtocolIdentifier securityProtocol,
Brad Ebinger34c09a52021-02-17 23:23:21 +00009616 boolean forceBootStrapping, IBootstrapAuthenticationCallback callback) {
9617 TelephonyPermissions.enforceAnyPermissionGrantedOrCarrierPrivileges(mApp, subId,
9618 Binder.getCallingUid(), "bootstrapAuthenticationRequest",
9619 Manifest.permission.PERFORM_IMS_SINGLE_REGISTRATION,
9620 Manifest.permission.MODIFY_PHONE_STATE);
Hui Wang641e81c2020-10-12 12:14:23 -07009621 if (DBG) {
9622 log("bootstrapAuthenticationRequest, subId:" + subId + ", appType:"
9623 + appType + ", NAF:" + nafUrl + ", sp:" + securityProtocol
9624 + ", forceBootStrapping:" + forceBootStrapping + ", callback:" + callback);
9625 }
9626
9627 if (!SubscriptionManager.isValidSubscriptionId(subId)
9628 || appType < TelephonyManager.APPTYPE_UNKNOWN
9629 || appType > TelephonyManager.APPTYPE_ISIM
9630 || nafUrl == null || securityProtocol == null || callback == null) {
9631 Log.d(LOG_TAG, "bootstrapAuthenticationRequest failed due to invalid parameters");
9632 if (callback != null) {
9633 try {
9634 callback.onAuthenticationFailure(
9635 0, TelephonyManager.GBA_FAILURE_REASON_FEATURE_NOT_SUPPORTED);
9636 } catch (RemoteException exception) {
9637 log("Fail to notify onAuthenticationFailure due to " + exception);
9638 }
9639 return;
9640 }
9641 }
9642
9643 final long token = Binder.clearCallingIdentity();
9644 try {
9645 getGbaManager(subId).bootstrapAuthenticationRequest(
9646 new GbaAuthRequest(subId, appType, nafUrl, securityProtocol.toByteArray(),
9647 forceBootStrapping, callback));
9648 } finally {
9649 Binder.restoreCallingIdentity(token);
9650 }
9651 }
9652
Jack Nudelmanb0b87642020-11-12 15:04:39 -08009653 /**
9654 * Attempts to set the radio power state for thermal reason. This does not guarantee that the
9655 * requested radio power state will actually be set. See {@link
9656 * PhoneInternalInterface#setRadioPowerForReason} for more details.
9657 *
9658 * @param subId the subscription ID of the phone requesting to set the radio power state.
9659 * @param enable {@code true} if trying to turn radio on.
9660 * @return {@code true} if phone setRadioPowerForReason was called. Otherwise, returns {@code
9661 * false}.
9662 */
9663 private boolean setRadioPowerForThermal(int subId, boolean enable) {
9664 Phone phone = getPhone(subId);
9665 if (phone != null) {
9666 phone.setRadioPowerForReason(enable, Phone.RADIO_POWER_REASON_THERMAL);
9667 return true;
9668 }
9669 return false;
9670 }
9671
9672 private int handleDataThrottlingRequest(int subId,
9673 DataThrottlingRequest dataThrottlingRequest) {
Jack Nudelman5d6a98b2021-03-04 14:26:25 -08009674 boolean isDataThrottlingSupported = isRadioInterfaceCapabilitySupported(
9675 TelephonyManager.CAPABILITY_THERMAL_MITIGATION_DATA_THROTTLING);
9676 if (!isDataThrottlingSupported && dataThrottlingRequest.getDataThrottlingAction()
9677 != DataThrottlingRequest.DATA_THROTTLING_ACTION_NO_DATA_THROTTLING) {
9678 throw new IllegalArgumentException("modem does not support data throttling");
9679 }
9680
Jack Nudelmanb0b87642020-11-12 15:04:39 -08009681 // Ensure that radio is on. If not able to power on due to phone being unavailable, return
9682 // THERMAL_MITIGATION_RESULT_MODEM_NOT_AVAILABLE.
9683 if (!setRadioPowerForThermal(subId, true)) {
9684 return TelephonyManager.THERMAL_MITIGATION_RESULT_MODEM_NOT_AVAILABLE;
9685 }
9686
9687 setDataEnabledForReason(subId, TelephonyManager.DATA_ENABLED_REASON_THERMAL, true);
9688
Jack Nudelman5d6a98b2021-03-04 14:26:25 -08009689 if (isDataThrottlingSupported) {
9690 int thermalMitigationResult =
Jack Nudelmanb0b87642020-11-12 15:04:39 -08009691 (int) sendRequest(CMD_SET_DATA_THROTTLING, dataThrottlingRequest, subId);
Jack Nudelman5d6a98b2021-03-04 14:26:25 -08009692 if (thermalMitigationResult == SET_DATA_THROTTLING_MODEM_THREW_INVALID_PARAMS) {
9693 throw new IllegalArgumentException("modem returned INVALID_ARGUMENTS");
9694 } else if (thermalMitigationResult
9695 == MODEM_DOES_NOT_SUPPORT_DATA_THROTTLING_ERROR_CODE) {
9696 throw new IllegalArgumentException("modem does not support data throttling");
9697 }
9698 return thermalMitigationResult;
Jack Nudelmanb0b87642020-11-12 15:04:39 -08009699 }
Jack Nudelman5d6a98b2021-03-04 14:26:25 -08009700
9701 return TelephonyManager.THERMAL_MITIGATION_RESULT_SUCCESS;
Jack Nudelmanb0b87642020-11-12 15:04:39 -08009702 }
9703
Jack Nudelman644b91a2021-03-12 14:09:48 -08009704 private static List<String> getThermalMitigationAllowlist(Context context) {
9705 if (sThermalMitigationAllowlistedPackages.isEmpty()) {
9706 for (String pckg : context.getResources()
9707 .getStringArray(R.array.thermal_mitigation_allowlisted_packages)) {
9708 sThermalMitigationAllowlistedPackages.add(pckg);
9709 }
9710 }
9711
9712 return sThermalMitigationAllowlistedPackages;
9713 }
9714
9715 /**
9716 * Used by shell commands to add an authorized package name for thermal mitigation.
9717 * @param packageName name of package to be allowlisted
9718 * @param context
9719 */
9720 static void addPackageToThermalMitigationAllowlist(String packageName, Context context) {
9721 sThermalMitigationAllowlistedPackages = getThermalMitigationAllowlist(context);
9722 sThermalMitigationAllowlistedPackages.add(packageName);
9723 }
9724
9725 /**
9726 * Used by shell commands to remove an authorized package name for thermal mitigation.
9727 * @param packageName name of package to remove from allowlist
9728 * @param context
9729 */
9730 static void removePackageFromThermalMitigationAllowlist(String packageName, Context context) {
9731 sThermalMitigationAllowlistedPackages = getThermalMitigationAllowlist(context);
9732 sThermalMitigationAllowlistedPackages.remove(packageName);
9733 }
9734
Jack Nudelmanb0b87642020-11-12 15:04:39 -08009735 /**
9736 * Thermal mitigation request to control functionalities at modem.
9737 *
9738 * @param subId the id of the subscription.
9739 * @param thermalMitigationRequest holds all necessary information to be passed down to modem.
Jack Nudelman644b91a2021-03-12 14:09:48 -08009740 * @param callingPackage the package name of the calling package.
Jack Nudelmanb0b87642020-11-12 15:04:39 -08009741 *
9742 * @return thermalMitigationResult enum as defined in android.telephony.Annotation.
9743 */
9744 @Override
9745 @ThermalMitigationResult
9746 public int sendThermalMitigationRequest(
9747 int subId,
Jack Nudelman644b91a2021-03-12 14:09:48 -08009748 ThermalMitigationRequest thermalMitigationRequest,
9749 String callingPackage) throws IllegalArgumentException {
Jack Nudelmanb0b87642020-11-12 15:04:39 -08009750 enforceModifyPermission();
9751
Jack Nudelman644b91a2021-03-12 14:09:48 -08009752 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
9753 if (!getThermalMitigationAllowlist(getDefaultPhone().getContext())
9754 .contains(callingPackage)) {
9755 throw new SecurityException("Calling package must be configured in the device config. "
9756 + "calling package: " + callingPackage);
9757 }
9758
Jack Nudelmanb0b87642020-11-12 15:04:39 -08009759 WorkSource workSource = getWorkSource(Binder.getCallingUid());
9760 final long identity = Binder.clearCallingIdentity();
9761
9762 int thermalMitigationResult = TelephonyManager.THERMAL_MITIGATION_RESULT_UNKNOWN_ERROR;
9763 try {
9764 int thermalMitigationAction = thermalMitigationRequest.getThermalMitigationAction();
9765 switch (thermalMitigationAction) {
9766 case ThermalMitigationRequest.THERMAL_MITIGATION_ACTION_DATA_THROTTLING:
9767 thermalMitigationResult =
9768 handleDataThrottlingRequest(subId,
9769 thermalMitigationRequest.getDataThrottlingRequest());
9770 break;
9771 case ThermalMitigationRequest.THERMAL_MITIGATION_ACTION_VOICE_ONLY:
9772 if (thermalMitigationRequest.getDataThrottlingRequest() != null) {
9773 throw new IllegalArgumentException("dataThrottlingRequest must be null for "
9774 + "ThermalMitigationRequest.THERMAL_MITIGATION_ACTION_VOICE_ONLY");
9775 }
9776
9777 // Ensure that radio is on. If not able to power on due to phone being
9778 // unavailable, return THERMAL_MITIGATION_RESULT_MODEM_NOT_AVAILABLE.
9779 if (!setRadioPowerForThermal(subId, true)) {
9780 thermalMitigationResult =
9781 TelephonyManager.THERMAL_MITIGATION_RESULT_MODEM_NOT_AVAILABLE;
9782 break;
9783 }
9784
9785 setDataEnabledForReason(subId, TelephonyManager.DATA_ENABLED_REASON_THERMAL,
9786 false);
9787 thermalMitigationResult = TelephonyManager.THERMAL_MITIGATION_RESULT_SUCCESS;
9788 break;
9789 case ThermalMitigationRequest.THERMAL_MITIGATION_ACTION_RADIO_OFF:
9790 if (thermalMitigationRequest.getDataThrottlingRequest() != null) {
9791 throw new IllegalArgumentException("dataThrottlingRequest must be null for"
9792 + " ThermalMitigationRequest.THERMAL_MITIGATION_ACTION_RADIO_OFF");
9793 }
9794
9795 TelecomAccountRegistry registry = TelecomAccountRegistry.getInstance(null);
9796 if (registry != null) {
9797 TelephonyConnectionService service =
9798 registry.getTelephonyConnectionService();
9799 Phone phone = getPhone(subId);
9800 if (phone == null) {
9801 thermalMitigationResult =
9802 TelephonyManager.THERMAL_MITIGATION_RESULT_MODEM_NOT_AVAILABLE;
9803 break;
9804 }
9805
9806 if (PhoneConstantConversions.convertCallState(phone.getState())
9807 != TelephonyManager.CALL_STATE_IDLE
9808 || phone.isInEmergencySmsMode() || phone.isInEcm()
9809 || (service != null && service.isEmergencyCallPending())) {
9810 String errorMessage = "Phone state is not valid. call state = "
9811 + PhoneConstantConversions.convertCallState(phone.getState())
9812 + " isInEmergencySmsMode = " + phone.isInEmergencySmsMode()
9813 + " isInEmergencyCallbackMode = " + phone.isInEcm();
9814 errorMessage += service == null
9815 ? " TelephonyConnectionService is null"
9816 : " isEmergencyCallPending = "
9817 + service.isEmergencyCallPending();
9818 Log.e(LOG_TAG, errorMessage);
9819 thermalMitigationResult =
9820 TelephonyManager.THERMAL_MITIGATION_RESULT_INVALID_STATE;
9821 break;
9822 }
9823 } else {
9824 thermalMitigationResult =
9825 TelephonyManager.THERMAL_MITIGATION_RESULT_MODEM_NOT_AVAILABLE;
9826 break;
9827 }
9828
9829 // Turn radio off. If not able to power off due to phone being unavailable,
9830 // return THERMAL_MITIGATION_RESULT_MODEM_NOT_AVAILABLE.
9831 if (!setRadioPowerForThermal(subId, false)) {
9832 thermalMitigationResult =
9833 TelephonyManager.THERMAL_MITIGATION_RESULT_MODEM_NOT_AVAILABLE;
9834 break;
9835 }
9836 thermalMitigationResult =
9837 TelephonyManager.THERMAL_MITIGATION_RESULT_SUCCESS;
9838 break;
9839 default:
9840 throw new IllegalArgumentException("the requested thermalMitigationAction does "
9841 + "not exist. Requested action: " + thermalMitigationAction);
9842 }
9843 } catch (IllegalArgumentException e) {
9844 throw e;
9845 } catch (Exception e) {
9846 Log.e(LOG_TAG, "thermalMitigationRequest. Exception e =" + e);
9847 thermalMitigationResult = TelephonyManager.THERMAL_MITIGATION_RESULT_MODEM_ERROR;
9848 } finally {
9849 Binder.restoreCallingIdentity(identity);
9850 }
9851
9852 if (DBG) {
9853 log("thermalMitigationRequest returning with thermalMitigationResult: "
9854 + thermalMitigationResult);
9855 }
9856
9857 return thermalMitigationResult;
9858 }
Hui Wang641e81c2020-10-12 12:14:23 -07009859
9860 /**
9861 * Set the GbaService Package Name that Telephony will bind to.
9862 *
9863 * @param subId The sim that the GbaService is associated with.
9864 * @param packageName The name of the package to be replaced with.
9865 * @return true if setting the GbaService to bind to succeeded, false if it did not.
9866 */
9867 @Override
9868 public boolean setBoundGbaServiceOverride(int subId, String packageName) {
9869 enforceModifyPermission();
9870
9871 final long identity = Binder.clearCallingIdentity();
9872 try {
9873 return getGbaManager(subId).overrideServicePackage(packageName);
9874 } finally {
9875 Binder.restoreCallingIdentity(identity);
9876 }
9877 }
9878
9879 /**
9880 * Return the package name of the currently bound GbaService.
9881 *
9882 * @param subId The sim that the GbaService is associated with.
9883 * @return the package name of the GbaService configuration, null if GBA is not supported.
9884 */
9885 @Override
9886 public String getBoundGbaService(int subId) {
9887 enforceReadPrivilegedPermission("getBoundGbaServicePackage");
9888
9889 final long identity = Binder.clearCallingIdentity();
9890 try {
9891 return getGbaManager(subId).getServicePackage();
9892 } finally {
9893 Binder.restoreCallingIdentity(identity);
9894 }
9895 }
9896
9897 /**
9898 * Set the release time for telephony to unbind GbaService.
9899 *
9900 * @param subId The sim that the GbaService is associated with.
9901 * @param interval The release time to unbind GbaService by millisecond.
9902 * @return true if setting the GbaService to bind to succeeded, false if it did not.
9903 */
9904 @Override
9905 public boolean setGbaReleaseTimeOverride(int subId, int interval) {
9906 enforceModifyPermission();
9907
9908 final long identity = Binder.clearCallingIdentity();
9909 try {
9910 return getGbaManager(subId).overrideReleaseTime(interval);
9911 } finally {
9912 Binder.restoreCallingIdentity(identity);
9913 }
9914 }
9915
9916 /**
9917 * Return the release time for telephony to unbind GbaService.
9918 *
9919 * @param subId The sim that the GbaService is associated with.
9920 * @return The release time to unbind GbaService by millisecond.
9921 */
9922 @Override
9923 public int getGbaReleaseTime(int subId) {
9924 enforceReadPrivilegedPermission("getGbaReleaseTime");
9925
9926 final long identity = Binder.clearCallingIdentity();
9927 try {
9928 return getGbaManager(subId).getReleaseTime();
9929 } finally {
9930 Binder.restoreCallingIdentity(identity);
9931 }
9932 }
9933
9934 private GbaManager getGbaManager(int subId) {
9935 GbaManager instance = GbaManager.getInstance(subId);
9936 if (instance == null) {
9937 String packageName = mApp.getResources().getString(R.string.config_gba_package);
9938 int releaseTime = mApp.getResources().getInteger(R.integer.config_gba_release_time);
9939 instance = GbaManager.make(mApp, subId, packageName, releaseTime);
9940 }
9941 return instance;
9942 }
Hui Wang761a6682020-10-31 05:12:53 +00009943
9944 /**
9945 * indicate whether the device and the carrier can support
9946 * RCS VoLTE single registration.
9947 */
9948 @Override
9949 public boolean isRcsVolteSingleRegistrationCapable(int subId) {
Brad Ebinger34c09a52021-02-17 23:23:21 +00009950 TelephonyPermissions.enforceAnyPermissionGrantedOrCarrierPrivileges(mApp, subId,
9951 Binder.getCallingUid(), "isRcsVolteSingleRegistrationCapable",
9952 Manifest.permission.PERFORM_IMS_SINGLE_REGISTRATION,
9953 permission.READ_PRIVILEGED_PHONE_STATE);
Hui Wang761a6682020-10-31 05:12:53 +00009954
9955 if (!SubscriptionManager.isValidSubscriptionId(subId)) {
9956 throw new IllegalArgumentException("Invalid Subscription ID: " + subId);
9957 }
9958
9959 final long identity = Binder.clearCallingIdentity();
9960 try {
9961 RcsProvisioningMonitor rpm = RcsProvisioningMonitor.getInstance();
9962 if (rpm != null) {
9963 return rpm.isRcsVolteSingleRegistrationEnabled(subId);
9964 }
9965 return false;
9966 } finally {
9967 Binder.restoreCallingIdentity(identity);
9968 }
9969 }
9970
9971 /**
9972 * Register RCS provisioning callback.
9973 */
9974 @Override
Hui Wang3cac7e52021-01-27 14:45:25 -08009975 public void registerRcsProvisioningCallback(int subId,
Hui Wang761a6682020-10-31 05:12:53 +00009976 IRcsConfigCallback callback) {
Brad Ebinger34c09a52021-02-17 23:23:21 +00009977 TelephonyPermissions.enforceAnyPermissionGrantedOrCarrierPrivileges(mApp, subId,
Hui Wang3cac7e52021-01-27 14:45:25 -08009978 Binder.getCallingUid(), "registerRcsProvisioningCallback",
Brad Ebinger34c09a52021-02-17 23:23:21 +00009979 Manifest.permission.PERFORM_IMS_SINGLE_REGISTRATION,
9980 permission.READ_PRIVILEGED_PHONE_STATE);
Hui Wang761a6682020-10-31 05:12:53 +00009981
9982 if (!SubscriptionManager.isValidSubscriptionId(subId)) {
9983 throw new IllegalArgumentException("Invalid Subscription ID: " + subId);
9984 }
9985 if (!isImsAvailableOnDevice()) {
9986 throw new ServiceSpecificException(ImsException.CODE_ERROR_UNSUPPORTED_OPERATION,
9987 "IMS not available on device.");
9988 }
9989
9990 final long identity = Binder.clearCallingIdentity();
9991 try {
Hui Wang68cd3722021-01-11 20:04:53 -08009992 if (!RcsProvisioningMonitor.getInstance()
Hui Wang3cac7e52021-01-27 14:45:25 -08009993 .registerRcsProvisioningCallback(subId, callback)) {
Hui Wang68cd3722021-01-11 20:04:53 -08009994 throw new ServiceSpecificException(ImsException.CODE_ERROR_UNSUPPORTED_OPERATION,
9995 "Service not available for the subscription.");
9996 }
Hui Wang761a6682020-10-31 05:12:53 +00009997 } finally {
9998 Binder.restoreCallingIdentity(identity);
9999 }
10000 }
10001
10002 /**
10003 * Unregister RCS provisioning callback.
10004 */
10005 @Override
Hui Wang3cac7e52021-01-27 14:45:25 -080010006 public void unregisterRcsProvisioningCallback(int subId,
Hui Wang761a6682020-10-31 05:12:53 +000010007 IRcsConfigCallback callback) {
Brad Ebinger34c09a52021-02-17 23:23:21 +000010008 TelephonyPermissions.enforceAnyPermissionGrantedOrCarrierPrivileges(mApp, subId,
Hui Wang3cac7e52021-01-27 14:45:25 -080010009 Binder.getCallingUid(), "unregisterRcsProvisioningCallback",
Brad Ebinger34c09a52021-02-17 23:23:21 +000010010 Manifest.permission.PERFORM_IMS_SINGLE_REGISTRATION,
10011 permission.READ_PRIVILEGED_PHONE_STATE);
Hui Wang761a6682020-10-31 05:12:53 +000010012
10013 if (!SubscriptionManager.isValidSubscriptionId(subId)) {
10014 throw new IllegalArgumentException("Invalid Subscription ID: " + subId);
10015 }
10016 if (!isImsAvailableOnDevice()) {
10017 throw new ServiceSpecificException(ImsException.CODE_ERROR_UNSUPPORTED_OPERATION,
10018 "IMS not available on device.");
10019 }
10020
10021 final long identity = Binder.clearCallingIdentity();
10022 try {
Hui Wang68cd3722021-01-11 20:04:53 -080010023 RcsProvisioningMonitor.getInstance()
Hui Wang3cac7e52021-01-27 14:45:25 -080010024 .unregisterRcsProvisioningCallback(subId, callback);
Hui Wang761a6682020-10-31 05:12:53 +000010025 } finally {
10026 Binder.restoreCallingIdentity(identity);
10027 }
10028 }
10029
10030 /**
10031 * trigger RCS reconfiguration.
10032 */
10033 public void triggerRcsReconfiguration(int subId) {
Brad Ebinger34c09a52021-02-17 23:23:21 +000010034 TelephonyPermissions.enforceAnyPermissionGranted(mApp, Binder.getCallingUid(),
10035 "triggerRcsReconfiguration",
10036 Manifest.permission.PERFORM_IMS_SINGLE_REGISTRATION);
Hui Wang761a6682020-10-31 05:12:53 +000010037
10038 if (!SubscriptionManager.isValidSubscriptionId(subId)) {
10039 throw new IllegalArgumentException("Invalid Subscription ID: " + subId);
10040 }
10041 if (!isImsAvailableOnDevice()) {
10042 throw new ServiceSpecificException(ImsException.CODE_ERROR_UNSUPPORTED_OPERATION,
10043 "IMS not available on device.");
10044 }
10045
10046 final long identity = Binder.clearCallingIdentity();
10047 try {
10048 RcsProvisioningMonitor.getInstance().requestReconfig(subId);
10049 } finally {
10050 Binder.restoreCallingIdentity(identity);
10051 }
10052 }
10053
10054 /**
10055 * Provide the client configuration parameters of the RCS application.
10056 */
10057 public void setRcsClientConfiguration(int subId, RcsClientConfiguration rcc) {
Brad Ebinger34c09a52021-02-17 23:23:21 +000010058 TelephonyPermissions.enforceAnyPermissionGranted(mApp, Binder.getCallingUid(),
10059 "setRcsClientConfiguration",
10060 Manifest.permission.PERFORM_IMS_SINGLE_REGISTRATION);
Hui Wang761a6682020-10-31 05:12:53 +000010061
10062 if (!SubscriptionManager.isValidSubscriptionId(subId)) {
10063 throw new IllegalArgumentException("Invalid Subscription ID: " + subId);
10064 }
10065 if (!isImsAvailableOnDevice()) {
10066 throw new ServiceSpecificException(ImsException.CODE_ERROR_UNSUPPORTED_OPERATION,
10067 "IMS not available on device.");
10068 }
10069
10070 final long identity = Binder.clearCallingIdentity();
10071
10072 try {
10073 IImsConfig configBinder = getImsConfig(getSlotIndex(subId), ImsFeature.FEATURE_RCS);
10074 if (configBinder == null) {
10075 Rlog.e(LOG_TAG, "null result for setRcsClientConfiguration");
10076 } else {
10077 configBinder.setRcsClientConfiguration(rcc);
10078 }
10079 } catch (RemoteException e) {
10080 Rlog.e(LOG_TAG, "fail to setRcsClientConfiguration " + e.getMessage());
10081 } finally {
10082 Binder.restoreCallingIdentity(identity);
10083 }
10084 }
10085
10086 /**
Hui Wangbaaee6a2021-02-19 20:45:36 -080010087 * Enables or disables the test mode for RCS VoLTE single registration.
10088 */
10089 @Override
10090 public void setRcsSingleRegistrationTestModeEnabled(boolean enabled) {
10091 TelephonyPermissions.enforceShellOnly(Binder.getCallingUid(),
10092 "setRcsSingleRegistrationTestModeEnabled");
10093
10094 RcsProvisioningMonitor.getInstance().setTestModeEnabled(enabled);
10095 }
10096
10097 /**
10098 * Gets the test mode for RCS VoLTE single registration.
10099 */
10100 @Override
10101 public boolean getRcsSingleRegistrationTestModeEnabled() {
10102 TelephonyPermissions.enforceShellOnly(Binder.getCallingUid(),
10103 "getRcsSingleRegistrationTestModeEnabled");
10104
10105 return RcsProvisioningMonitor.getInstance().getTestModeEnabled();
10106 }
10107
10108 /**
Hui Wang761a6682020-10-31 05:12:53 +000010109 * Overrides the config of RCS VoLTE single registration enabled for the device.
10110 */
10111 @Override
10112 public void setDeviceSingleRegistrationEnabledOverride(String enabledStr) {
10113 TelephonyPermissions.enforceShellOnly(Binder.getCallingUid(),
10114 "setDeviceSingleRegistrationEnabledOverride");
10115 enforceModifyPermission();
10116
10117 Boolean enabled = "NULL".equalsIgnoreCase(enabledStr) ? null
10118 : Boolean.parseBoolean(enabledStr);
10119 RcsProvisioningMonitor.getInstance().overrideDeviceSingleRegistrationEnabled(enabled);
Brad Ebinger49a72b42021-01-29 00:55:24 +000010120 mApp.imsRcsController.setDeviceSingleRegistrationSupportOverride(enabled);
Hui Wang761a6682020-10-31 05:12:53 +000010121 }
10122
10123 /**
Tyler Gunn92479152021-01-20 16:30:10 -080010124 * Sends a device to device communication message. Only usable via shell.
10125 * @param message message to send.
10126 * @param value message value.
10127 */
10128 @Override
10129 public void sendDeviceToDeviceMessage(int message, int value) {
10130 TelephonyPermissions.enforceShellOnly(Binder.getCallingUid(),
Tyler Gunnbabbda02021-02-10 11:05:02 -080010131 "sendDeviceToDeviceMessage");
Tyler Gunn92479152021-01-20 16:30:10 -080010132 enforceModifyPermission();
10133
10134 final long identity = Binder.clearCallingIdentity();
10135 try {
10136 TelephonyConnectionService service =
10137 TelecomAccountRegistry.getInstance(null).getTelephonyConnectionService();
10138 if (service == null) {
10139 Rlog.e(LOG_TAG, "sendDeviceToDeviceMessage: not in a call.");
10140 return;
10141 }
10142 service.sendTestDeviceToDeviceMessage(message, value);
10143 } finally {
10144 Binder.restoreCallingIdentity(identity);
10145 }
10146 }
10147
Tyler Gunnbabbda02021-02-10 11:05:02 -080010148 /**
10149 * Sets the specified device to device transport active.
10150 * @param transport The transport to set active.
10151 */
10152 @Override
10153 public void setActiveDeviceToDeviceTransport(@NonNull String transport) {
10154 TelephonyPermissions.enforceShellOnly(Binder.getCallingUid(),
10155 "setActiveDeviceToDeviceTransport");
10156 enforceModifyPermission();
10157
10158 final long identity = Binder.clearCallingIdentity();
10159 try {
10160 TelephonyConnectionService service =
10161 TelecomAccountRegistry.getInstance(null).getTelephonyConnectionService();
10162 if (service == null) {
10163 Rlog.e(LOG_TAG, "setActiveDeviceToDeviceTransport: not in a call.");
10164 return;
10165 }
10166 service.setActiveDeviceToDeviceTransport(transport);
10167 } finally {
10168 Binder.restoreCallingIdentity(identity);
10169 }
10170 }
Tyler Gunn92479152021-01-20 16:30:10 -080010171
10172 /**
Hui Wang761a6682020-10-31 05:12:53 +000010173 * Gets the config of RCS VoLTE single registration enabled for the device.
10174 */
10175 @Override
10176 public boolean getDeviceSingleRegistrationEnabled() {
10177 enforceReadPrivilegedPermission("getDeviceSingleRegistrationEnabled");
10178 return RcsProvisioningMonitor.getInstance().getDeviceSingleRegistrationEnabled();
10179 }
10180
10181 /**
10182 * Overrides the config of RCS VoLTE single registration enabled for the carrier/subscription.
10183 */
10184 @Override
10185 public boolean setCarrierSingleRegistrationEnabledOverride(int subId, String enabledStr) {
10186 TelephonyPermissions.enforceShellOnly(Binder.getCallingUid(),
10187 "setCarrierSingleRegistrationEnabledOverride");
10188 enforceModifyPermission();
10189
10190 Boolean enabled = "NULL".equalsIgnoreCase(enabledStr) ? null
10191 : Boolean.parseBoolean(enabledStr);
10192 return RcsProvisioningMonitor.getInstance().overrideCarrierSingleRegistrationEnabled(
10193 subId, enabled);
10194 }
10195
10196 /**
10197 * Gets the config of RCS VoLTE single registration enabled for the carrier/subscription.
10198 */
10199 @Override
10200 public boolean getCarrierSingleRegistrationEnabled(int subId) {
10201 enforceReadPrivilegedPermission("getCarrierSingleRegistrationEnabled");
10202 return RcsProvisioningMonitor.getInstance().getCarrierSingleRegistrationEnabled(subId);
10203 }
Chiachang Wangd6d34772020-12-22 11:38:27 +080010204
10205 /**
Hui Wangb647abe2021-02-26 09:33:38 -080010206 * Overrides the ims feature validation result
10207 */
10208 @Override
10209 public boolean setImsFeatureValidationOverride(int subId, String enabledStr) {
10210 TelephonyPermissions.enforceShellOnly(Binder.getCallingUid(),
10211 "setImsFeatureValidationOverride");
10212
10213 Boolean enabled = "NULL".equalsIgnoreCase(enabledStr) ? null
10214 : Boolean.parseBoolean(enabledStr);
10215 return RcsProvisioningMonitor.getInstance().overrideImsFeatureValidation(
10216 subId, enabled);
10217 }
10218
10219 /**
10220 * Gets the ims feature validation override value
10221 */
10222 @Override
10223 public boolean getImsFeatureValidationOverride(int subId) {
10224 TelephonyPermissions.enforceShellOnly(Binder.getCallingUid(),
10225 "getImsFeatureValidationOverride");
10226 return RcsProvisioningMonitor.getInstance().getImsFeatureValidationOverride(subId);
10227 }
10228
10229 /**
Chiachang Wangd6d34772020-12-22 11:38:27 +080010230 * Get the mobile provisioning url that is used to launch a browser to allow users to manage
10231 * their mobile plan.
10232 */
10233 @Override
10234 public String getMobileProvisioningUrl() {
10235 enforceReadPrivilegedPermission("getMobileProvisioningUrl");
10236 final long identity = Binder.clearCallingIdentity();
10237 try {
10238 return getDefaultPhone().getMobileProvisioningUrl();
10239 } finally {
10240 Binder.restoreCallingIdentity(identity);
10241 }
10242 }
Rambo Wanga5cc9b72021-01-07 10:51:54 -080010243
James.cf Linbcdf8b32021-01-14 16:44:13 +080010244 /**
calvinpane4a8a1d2021-01-25 13:51:18 +080010245 * Get the EAB contact from the EAB database.
10246 */
10247 @Override
10248 public String getContactFromEab(String contact) {
10249 TelephonyPermissions.enforceShellOnly(Binder.getCallingUid(), "getContactFromEab");
10250 enforceModifyPermission();
10251 final long identity = Binder.clearCallingIdentity();
10252 try {
10253 return EabUtil.getContactFromEab(getDefaultPhone().getContext(), contact);
10254 } finally {
10255 Binder.restoreCallingIdentity(identity);
10256 }
10257 }
10258
10259 /**
James.cf Linbcdf8b32021-01-14 16:44:13 +080010260 * Remove the EAB contacts from the EAB database.
10261 */
10262 @Override
10263 public int removeContactFromEab(int subId, String contacts) {
10264 TelephonyPermissions.enforceShellOnly(Binder.getCallingUid(), "removeCapabilitiesFromEab");
10265 enforceModifyPermission();
10266 final long identity = Binder.clearCallingIdentity();
10267 try {
10268 return EabUtil.removeContactFromEab(subId, contacts, getDefaultPhone().getContext());
10269 } finally {
10270 Binder.restoreCallingIdentity(identity);
10271 }
10272 }
10273
Rambo Wanga5cc9b72021-01-07 10:51:54 -080010274 @Override
James.cf Lin4b784aa2021-01-31 03:25:15 +080010275 public boolean getDeviceUceEnabled() {
10276 TelephonyPermissions.enforceShellOnly(Binder.getCallingUid(), "getDeviceUceEnabled");
10277 final long identity = Binder.clearCallingIdentity();
10278 try {
10279 return mApp.getDeviceUceEnabled();
10280 } finally {
10281 Binder.restoreCallingIdentity(identity);
10282 }
10283 }
10284
10285 @Override
10286 public void setDeviceUceEnabled(boolean isEnabled) {
10287 TelephonyPermissions.enforceShellOnly(Binder.getCallingUid(), "setDeviceUceEnabled");
10288 final long identity = Binder.clearCallingIdentity();
10289 try {
10290 mApp.setDeviceUceEnabled(isEnabled);
10291 } finally {
10292 Binder.restoreCallingIdentity(identity);
10293 }
10294 }
10295
Brad Ebinger14d467f2021-02-12 06:18:28 +000010296 /**
10297 * Add new feature tags to the Set used to calculate the capabilities in PUBLISH.
10298 * @return current RcsContactUceCapability instance that will be used for PUBLISH.
10299 */
10300 // Used for SHELL command only right now.
10301 @Override
10302 public RcsContactUceCapability addUceRegistrationOverrideShell(int subId,
10303 List<String> featureTags) {
10304 TelephonyPermissions.enforceShellOnly(Binder.getCallingUid(),
10305 "addUceRegistrationOverrideShell");
10306 final long identity = Binder.clearCallingIdentity();
10307 try {
10308 return mApp.imsRcsController.addUceRegistrationOverrideShell(subId,
10309 new ArraySet<>(featureTags));
10310 } catch (ImsException e) {
10311 throw new ServiceSpecificException(e.getCode(), e.getMessage());
10312 } finally {
10313 Binder.restoreCallingIdentity(identity);
10314 }
10315 }
10316
10317 /**
10318 * Remove existing feature tags to the Set used to calculate the capabilities in PUBLISH.
10319 * @return current RcsContactUceCapability instance that will be used for PUBLISH.
10320 */
10321 // Used for SHELL command only right now.
10322 @Override
10323 public RcsContactUceCapability removeUceRegistrationOverrideShell(int subId,
10324 List<String> featureTags) {
10325 TelephonyPermissions.enforceShellOnly(Binder.getCallingUid(),
10326 "removeUceRegistrationOverrideShell");
10327 final long identity = Binder.clearCallingIdentity();
10328 try {
10329 return mApp.imsRcsController.removeUceRegistrationOverrideShell(subId,
10330 new ArraySet<>(featureTags));
10331 } catch (ImsException e) {
10332 throw new ServiceSpecificException(e.getCode(), e.getMessage());
10333 } finally {
10334 Binder.restoreCallingIdentity(identity);
10335 }
10336 }
10337
10338 /**
10339 * Clear all overrides in the Set used to calculate the capabilities in PUBLISH.
10340 * @return current RcsContactUceCapability instance that will be used for PUBLISH.
10341 */
10342 // Used for SHELL command only right now.
10343 @Override
10344 public RcsContactUceCapability clearUceRegistrationOverrideShell(int subId) {
10345 TelephonyPermissions.enforceShellOnly(Binder.getCallingUid(),
10346 "clearUceRegistrationOverrideShell");
10347 final long identity = Binder.clearCallingIdentity();
10348 try {
10349 return mApp.imsRcsController.clearUceRegistrationOverrideShell(subId);
10350 } catch (ImsException e) {
10351 throw new ServiceSpecificException(e.getCode(), e.getMessage());
10352 } finally {
10353 Binder.restoreCallingIdentity(identity);
10354 }
10355 }
10356
10357 /**
10358 * @return current RcsContactUceCapability instance that will be used for PUBLISH.
10359 */
10360 // Used for SHELL command only right now.
10361 @Override
10362 public RcsContactUceCapability getLatestRcsContactUceCapabilityShell(int subId) {
10363 TelephonyPermissions.enforceShellOnly(Binder.getCallingUid(),
10364 "getLatestRcsContactUceCapabilityShell");
10365 final long identity = Binder.clearCallingIdentity();
10366 try {
10367 return mApp.imsRcsController.getLatestRcsContactUceCapabilityShell(subId);
10368 } catch (ImsException e) {
10369 throw new ServiceSpecificException(e.getCode(), e.getMessage());
10370 } finally {
10371 Binder.restoreCallingIdentity(identity);
10372 }
10373 }
10374
10375 /**
10376 * Returns the last PIDF XML sent to the network during the last PUBLISH or "none" if the
10377 * device does not have an active PUBLISH.
10378 */
10379 // Used for SHELL command only right now.
10380 @Override
10381 public String getLastUcePidfXmlShell(int subId) {
10382 TelephonyPermissions.enforceShellOnly(Binder.getCallingUid(), "uceGetLastPidfXml");
10383 final long identity = Binder.clearCallingIdentity();
10384 try {
10385 return mApp.imsRcsController.getLastUcePidfXmlShell(subId);
10386 } catch (ImsException e) {
10387 throw new ServiceSpecificException(e.getCode(), e.getMessage());
10388 } finally {
10389 Binder.restoreCallingIdentity(identity);
10390 }
10391 }
10392
10393
James.cf Lin4b784aa2021-01-31 03:25:15 +080010394 @Override
Rambo Wanga5cc9b72021-01-07 10:51:54 -080010395 public void setSignalStrengthUpdateRequest(int subId, SignalStrengthUpdateRequest request,
10396 String callingPackage) {
10397 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
10398 mApp, subId, "setSignalStrengthUpdateRequest");
10399
10400 final int callingUid = Binder.getCallingUid();
10401 // Verify that tha callingPackage belongs to the calling UID
10402 mApp.getSystemService(AppOpsManager.class)
10403 .checkPackage(callingUid, callingPackage);
10404
10405 validateSignalStrengthUpdateRequest(request, callingUid);
10406
10407 final long identity = Binder.clearCallingIdentity();
10408 try {
10409 Object result = sendRequest(CMD_SET_SIGNAL_STRENGTH_UPDATE_REQUEST,
10410 new Pair<Integer, SignalStrengthUpdateRequest>(callingUid, request), subId);
10411
10412 if (result instanceof IllegalStateException) {
10413 throw (IllegalStateException) result;
10414 }
10415 } finally {
10416 Binder.restoreCallingIdentity(identity);
10417 }
10418 }
10419
10420 @Override
10421 public void clearSignalStrengthUpdateRequest(int subId, SignalStrengthUpdateRequest request,
10422 String callingPackage) {
10423 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
10424 mApp, subId, "clearSignalStrengthUpdateRequest");
10425
10426 final int callingUid = Binder.getCallingUid();
10427 // Verify that tha callingPackage belongs to the calling UID
10428 mApp.getSystemService(AppOpsManager.class)
10429 .checkPackage(callingUid, callingPackage);
10430
10431 final long identity = Binder.clearCallingIdentity();
10432 try {
10433 Object result = sendRequest(CMD_CLEAR_SIGNAL_STRENGTH_UPDATE_REQUEST,
10434 new Pair<Integer, SignalStrengthUpdateRequest>(callingUid, request), subId);
10435
10436 if (result instanceof IllegalStateException) {
10437 throw (IllegalStateException) result;
10438 }
10439 } finally {
10440 Binder.restoreCallingIdentity(identity);
10441 }
10442 }
10443
10444 private static void validateSignalStrengthUpdateRequest(SignalStrengthUpdateRequest request,
10445 int callingUid) {
10446 if (callingUid == Process.PHONE_UID || callingUid == Process.SYSTEM_UID) {
10447 // phone/system process do not have further restriction on request
10448 return;
10449 }
10450
10451 // Applications has restrictions on how to use the request:
10452 // Only system caller can set mIsSystemThresholdReportingRequestedWhileIdle
10453 if (request.isSystemThresholdReportingRequestedWhileIdle()) {
10454 // This is not system caller which has been checked above
10455 throw new IllegalArgumentException(
10456 "Only system can set isSystemThresholdReportingRequestedWhileIdle");
10457 }
10458
10459 for (SignalThresholdInfo info : request.getSignalThresholdInfos()) {
10460 // Only system caller can set mHysteresisMs/mHysteresisDb/mIsEnabled.
10461 if (info.getHysteresisMs() != SignalThresholdInfo.HYSTERESIS_MS_DISABLED
10462 || info.getHysteresisDb() != SignalThresholdInfo.HYSTERESIS_DB_DISABLED
10463 || info.isEnabled()) {
10464 throw new IllegalArgumentException(
10465 "Only system can set hide fields in SignalThresholdInfo");
10466 }
10467
10468 // Thresholds length for each RAN need in range. This has been validated in
10469 // SignalThresholdInfo#Builder#setThreshold. Here we prevent apps calling hide method
10470 // setThresholdUnlimited (e.g. through reflection) with too short or too long thresholds
10471 final int[] thresholds = info.getThresholds();
10472 Objects.requireNonNull(thresholds);
10473 if (thresholds.length < SignalThresholdInfo.getMinimumNumberOfThresholdsAllowed()
10474 || thresholds.length
10475 > SignalThresholdInfo.getMaximumNumberOfThresholdsAllowed()) {
10476 throw new IllegalArgumentException(
10477 "thresholds length is out of range: " + thresholds.length);
10478 }
10479 }
10480 }
SongFerngWang8236caa2021-01-17 21:51:44 +080010481
10482 /**
10483 * Gets the current phone capability.
10484 *
10485 * Requires carrier privileges or READ_PRECISE_PHONE_STATE permission.
10486 * @return the PhoneCapability which describes the data connection capability of modem.
10487 * It's used to evaluate possible phone config change, for example from single
10488 * SIM device to multi-SIM device.
10489 */
10490 @Override
10491 public PhoneCapability getPhoneCapability() {
10492 enforceReadPrivilegedPermission("getPhoneCapability");
10493 final long identity = Binder.clearCallingIdentity();
10494 try {
10495 return mPhoneConfigurationManager.getCurrentPhoneCapability();
10496 } finally {
10497 Binder.restoreCallingIdentity(identity);
10498 }
10499 }
Michele Berionne5e411512020-11-13 02:36:59 +000010500
10501 /**
10502 * Prepare TelephonyManager for an unattended reboot. The reboot is
10503 * required to be done shortly after the API is invoked.
10504 */
10505 @Override
10506 @TelephonyManager.PrepareUnattendedRebootResult
10507 public int prepareForUnattendedReboot() {
10508 enforceRebootPermission();
10509
10510 final long identity = Binder.clearCallingIdentity();
10511 try {
10512 return (int) sendRequest(CMD_PREPARE_UNATTENDED_REBOOT, null);
10513 } finally {
10514 Binder.restoreCallingIdentity(identity);
10515 }
10516 }
Hongbo Zeng156aa4a2021-02-08 21:50:28 +080010517
10518 /**
10519 * Request to get the current slicing configuration including URSP rules and
10520 * NSSAIs (configured, allowed and rejected).
10521 *
10522 * Requires carrier privileges or READ_PRIVILEGED_PHONE_STATE permission.
10523 */
10524 @Override
10525 public void getSlicingConfig(ResultReceiver callback) {
10526 enforceReadPrivilegedPermission("getSlicingConfig");
10527
10528 final long identity = Binder.clearCallingIdentity();
10529 try {
10530 Phone phone = getDefaultPhone();
10531 sendRequestAsync(CMD_GET_SLICING_CONFIG, callback, phone, null);
10532 } finally {
10533 Binder.restoreCallingIdentity(identity);
10534 }
10535 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -070010536}