blob: 71cebfdb510b5a3ff3a44eff979cde4ac886c548 [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
Tyler Gunn7bcdc742019-10-04 15:56:59 -070021import static com.android.internal.telephony.PhoneConstants.PHONE_TYPE_IMS;
Ta-wei Yen87c49842016-05-13 21:19:52 -070022import static com.android.internal.telephony.PhoneConstants.SUBSCRIPTION_KEY;
23
Ta-wei Yen30a69c82016-12-27 14:52:32 -080024import android.Manifest.permission;
changbetty363e8ac2019-12-06 18:16:37 +080025import android.annotation.NonNull;
Tyler Gunnf70ed162019-04-03 15:28:53 -070026import android.annotation.Nullable;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070027import android.app.AppOpsManager;
Ta-wei Yen30a69c82016-12-27 14:52:32 -080028import android.app.PendingIntent;
Sailesh Nepalbd76e4e2013-10-27 13:59:44 -070029import android.content.ComponentName;
Amit Mahajan7dbbd822019-03-13 17:33:47 -070030import android.content.ContentResolver;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070031import android.content.Context;
32import android.content.Intent;
Derek Tan97ebb422014-09-05 16:55:38 -070033import android.content.SharedPreferences;
Nathan Harold31d7ff32018-10-15 20:20:30 -070034import android.content.pm.ApplicationInfo;
Derek Tan740e1672017-06-27 14:56:27 -070035import android.content.pm.ComponentInfo;
Amith Yamasani6e118872016-02-19 12:53:51 -080036import android.content.pm.PackageInfo;
Shishir Agrawal60f9c952014-06-23 12:00:43 -070037import android.content.pm.PackageManager;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070038import android.net.Uri;
39import android.os.AsyncResult;
40import android.os.Binder;
Hall Liuf19c44f2018-11-27 14:38:17 -080041import android.os.Build;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070042import android.os.Bundle;
43import android.os.Handler;
yinxu504e1392017-04-12 16:03:22 -070044import android.os.IBinder;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070045import android.os.Looper;
46import android.os.Message;
yinxu504e1392017-04-12 16:03:22 -070047import android.os.Messenger;
Shuo Qianb15c6be2020-03-05 17:55:34 -080048import android.os.ParcelFileDescriptor;
Malcolm Chen6ca97372019-07-01 16:28:21 -070049import android.os.ParcelUuid;
Tyler Gunn65d45c22017-06-05 11:22:26 -070050import android.os.PersistableBundle;
Shuo Qian5bac1ee2020-01-16 20:51:11 -080051import android.os.Process;
Brad Ebinger5f64b052017-12-14 14:26:15 -080052import android.os.RemoteException;
Adam Lesinski903a54c2016-04-11 14:49:52 -070053import android.os.ResultReceiver;
Brad Ebinger1ce9c432019-07-16 13:19:44 -070054import android.os.ServiceSpecificException;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070055import android.os.UserHandle;
Stuart Scott981d8582015-04-21 14:09:50 -070056import android.os.UserManager;
Sooraj Sasindran9a909312016-07-18 11:57:25 -070057import android.os.WorkSource;
Derek Tan97ebb422014-09-05 16:55:38 -070058import android.preference.PreferenceManager;
Naina Nalluri8ff344d2019-09-17 14:10:30 -070059import android.provider.DeviceConfig;
Ihab Awadf2177b72013-11-25 13:33:23 -080060import android.provider.Settings;
Amit Mahajan7dbbd822019-03-13 17:33:47 -070061import android.provider.Telephony;
Inseob Kim8d298d42018-12-13 17:11:34 +090062import android.sysprop.TelephonyProperties;
Santos Cordon7a1885b2015-02-03 11:15:19 -080063import android.telecom.PhoneAccount;
Nancy Chen31f9ba12016-01-06 11:42:12 -080064import android.telecom.PhoneAccountHandle;
Andrew Lee9431b832015-03-09 18:46:45 -070065import android.telecom.TelecomManager;
Chen Xuc7b18ec2019-09-26 22:48:11 -070066import android.telephony.Annotation.ApnType;
Jack Nudelman24d51a52020-11-24 12:08:04 -080067import android.telephony.Annotation.ThermalMitigationResult;
sqian80370722020-01-29 15:02:51 -080068import android.telephony.CallForwardingInfo;
Sooraj Sasindran1f812e02020-10-30 13:17:53 -070069import android.telephony.CarrierBandwidth;
Junda Liu12f7d802015-05-01 12:06:44 -070070import android.telephony.CarrierConfigManager;
Michele Berionne482f8202018-11-27 18:57:59 -080071import android.telephony.CarrierRestrictionRules;
yincheng zhaod698b842019-09-06 17:06:54 -070072import android.telephony.CellIdentity;
Meng Wangd64acad2019-12-09 13:13:01 -080073import android.telephony.CellIdentityCdma;
74import android.telephony.CellIdentityGsm;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070075import android.telephony.CellInfo;
Nathan Haroldf180aac2018-06-01 18:43:55 -070076import android.telephony.CellInfoGsm;
77import android.telephony.CellInfoWcdma;
Sooraj Sasindran9a909312016-07-18 11:57:25 -070078import android.telephony.ClientRequestStats;
Jack Nudelman24d51a52020-11-24 12:08:04 -080079import android.telephony.DataThrottlingRequest;
Nathan Haroldfa8da0f2018-09-27 18:51:29 -070080import android.telephony.ICellInfoCallback;
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -070081import android.telephony.IccOpenLogicalChannelResponse;
Hall Liu1aa510f2017-11-22 17:40:08 -080082import android.telephony.LocationAccessPolicy;
Ta-wei Yen87c49842016-05-13 21:19:52 -070083import android.telephony.ModemActivityInfo;
Jake Hambye994d462014-02-03 13:10:13 -080084import android.telephony.NeighboringCellInfo;
yinxu504e1392017-04-12 16:03:22 -070085import android.telephony.NetworkScanRequest;
Michele4245e952019-02-04 11:36:23 -080086import android.telephony.PhoneCapability;
Hall Liud892bec2018-11-30 14:51:45 -080087import android.telephony.PhoneNumberRange;
Wink Saville5d475dd2014-10-17 15:00:58 -070088import android.telephony.RadioAccessFamily;
Hall Liub2ac8ef2019-02-28 15:56:23 -080089import android.telephony.RadioAccessSpecifier;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070090import android.telephony.ServiceState;
Nathan Harold46b42aa2017-03-10 19:38:22 -080091import android.telephony.SignalStrength;
Wink Saville0f3b5fc2014-11-11 08:40:49 -080092import android.telephony.SubscriptionInfo;
Jeff Sharkey85190e62014-12-05 09:40:12 -080093import android.telephony.SubscriptionManager;
Peter Wangc035ce42020-01-08 21:00:22 -080094import android.telephony.TelephonyFrameworkInitializer;
Sanket Padawe99ef1e32016-05-18 16:12:33 -070095import android.telephony.TelephonyHistogram;
Ta-wei Yenb6929602016-05-24 15:48:27 -070096import android.telephony.TelephonyManager;
Hall Liub2ac8ef2019-02-28 15:56:23 -080097import android.telephony.TelephonyScanManager;
Jack Nudelman24d51a52020-11-24 12:08:04 -080098import android.telephony.ThermalMitigationRequest;
Jordan Liu5aa07002018-12-18 15:44:48 -080099import android.telephony.UiccCardInfo;
Holly Jiuyu Sun01c47ad2018-01-24 17:56:33 +0000100import android.telephony.UiccSlotInfo;
Tyler Gunn65d45c22017-06-05 11:22:26 -0700101import android.telephony.UssdResponse;
Ta-wei Yenb6929602016-05-24 15:48:27 -0700102import android.telephony.VisualVoicemailSmsFilterSettings;
Jack Yub5d8f642018-11-26 11:20:48 -0800103import android.telephony.data.ApnSetting;
104import android.telephony.emergency.EmergencyNumber;
Brad Ebinger1ce9c432019-07-16 13:19:44 -0700105import android.telephony.ims.ImsException;
Brad Ebinger1c8542e2019-01-14 13:43:14 -0800106import android.telephony.ims.ProvisioningManager;
Brad Ebinger774ba362019-10-22 17:36:18 -0700107import android.telephony.ims.RegistrationManager;
Brad Ebinger35c841c2018-10-01 10:40:55 -0700108import android.telephony.ims.aidl.IImsCapabilityCallback;
Brad Ebinger22bc3e42018-01-16 09:39:35 -0800109import android.telephony.ims.aidl.IImsConfig;
Brad Ebingerdf5b4f02018-10-31 11:24:17 -0700110import android.telephony.ims.aidl.IImsConfigCallback;
Brad Ebinger22bc3e42018-01-16 09:39:35 -0800111import android.telephony.ims.aidl.IImsRegistration;
Brad Ebinger35c841c2018-10-01 10:40:55 -0700112import android.telephony.ims.aidl.IImsRegistrationCallback;
Brad Ebinger77b832e2019-10-17 17:03:22 -0700113import android.telephony.ims.feature.ImsFeature;
Brad Ebinger1c8542e2019-01-14 13:43:14 -0800114import android.telephony.ims.feature.MmTelFeature;
allenwtsu99c623b2020-01-03 18:24:23 +0800115import android.telephony.ims.feature.RcsFeature;
Brad Ebinger1c8542e2019-01-14 13:43:14 -0800116import android.telephony.ims.stub.ImsConfigImplBase;
Brad Ebinger1f2b5082018-02-08 16:11:32 -0800117import android.telephony.ims.stub.ImsRegistrationImplBase;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700118import android.text.TextUtils;
Jeff Sharkey85190e62014-12-05 09:40:12 -0800119import android.util.ArraySet;
Hall Liuaa4283b2020-05-21 17:09:35 -0700120import android.util.EventLog;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700121import android.util.Log;
Jake Hambye994d462014-02-03 13:10:13 -0800122import android.util.Pair;
Ta-wei Yen30a69c82016-12-27 14:52:32 -0800123
Andrew Lee312e8172014-10-23 17:01:36 -0700124import com.android.ims.ImsManager;
Brad Ebinger34bef922017-11-09 10:27:08 -0800125import com.android.ims.internal.IImsServiceFeatureCallback;
sqian80370722020-01-29 15:02:51 -0800126import com.android.internal.telephony.CallForwardInfo;
Shishir Agrawal566b7612013-10-28 14:41:00 -0700127import com.android.internal.telephony.CallManager;
Tyler Gunn52dcf772017-04-26 11:30:31 -0700128import com.android.internal.telephony.CallStateException;
pkanwar79ec0542017-07-31 14:10:01 -0700129import com.android.internal.telephony.CarrierInfoManager;
chen xu651eec72018-11-11 19:03:44 -0800130import com.android.internal.telephony.CarrierResolver;
Shishir Agrawal302c8692015-06-19 13:49:39 -0700131import com.android.internal.telephony.CellNetworkScanResult;
Shishir Agrawal566b7612013-10-28 14:41:00 -0700132import com.android.internal.telephony.CommandException;
sqian80370722020-01-29 15:02:51 -0800133import com.android.internal.telephony.CommandsInterface;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700134import com.android.internal.telephony.DefaultPhoneNotifier;
Nathan Harold48d6fd52019-02-06 19:01:40 -0800135import com.android.internal.telephony.HalVersion;
Hall Liud0d1dc92020-01-20 13:42:00 -0800136import com.android.internal.telephony.IBooleanConsumer;
Hall Liua1acea22020-09-18 19:04:59 -0700137import com.android.internal.telephony.ICallForwardingInfoCallback;
Brad Ebingera63db5f2019-04-23 16:31:13 -0700138import com.android.internal.telephony.IIntegerConsumer;
Hall Liud892bec2018-11-30 14:51:45 -0800139import com.android.internal.telephony.INumberVerificationCallback;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700140import com.android.internal.telephony.ITelephony;
Jake Hambye994d462014-02-03 13:10:13 -0800141import com.android.internal.telephony.IccCard;
Jack Yu5f7092c2018-04-13 14:05:37 -0700142import com.android.internal.telephony.LocaleTracker;
yinxub1bed742017-04-17 11:45:04 -0700143import com.android.internal.telephony.NetworkScanRequestTracker;
Shishir Agrawal302c8692015-06-19 13:49:39 -0700144import com.android.internal.telephony.OperatorInfo;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700145import com.android.internal.telephony.Phone;
Malcolm Chen2c63d402018-08-14 16:00:53 -0700146import com.android.internal.telephony.PhoneConfigurationManager;
Nathan Harolda667c152016-12-14 11:27:20 -0800147import com.android.internal.telephony.PhoneConstantConversions;
Ta-wei Yen87c49842016-05-13 21:19:52 -0700148import com.android.internal.telephony.PhoneConstants;
Wink Saville36469e72014-06-11 15:17:00 -0700149import com.android.internal.telephony.PhoneFactory;
Wink Saville5d475dd2014-10-17 15:00:58 -0700150import com.android.internal.telephony.ProxyController;
Sanket Padawe99ef1e32016-05-18 16:12:33 -0700151import com.android.internal.telephony.RIL;
Svet Ganovb320e182015-04-16 12:30:10 -0700152import com.android.internal.telephony.RILConstants;
Jack Yu5f7092c2018-04-13 14:05:37 -0700153import com.android.internal.telephony.ServiceStateTracker;
Amit Mahajandccb3f12019-05-13 13:48:32 -0700154import com.android.internal.telephony.SmsController;
Brad Ebingera63db5f2019-04-23 16:31:13 -0700155import com.android.internal.telephony.SmsPermissions;
Wink Savilleac1bdfd2014-11-20 23:04:44 -0800156import com.android.internal.telephony.SubscriptionController;
Peter Wang59571be2020-01-27 12:35:15 +0800157import com.android.internal.telephony.TelephonyIntents;
Jeff Davidson7e17e312018-02-13 18:17:36 -0800158import com.android.internal.telephony.TelephonyPermissions;
Malcolm Chendc8c10e2019-04-10 18:25:07 -0700159import com.android.internal.telephony.dataconnection.ApnSettingUtils;
sqianf4ca7ed2019-01-15 18:32:07 -0800160import com.android.internal.telephony.emergency.EmergencyNumberTracker;
Derek Tan740e1672017-06-27 14:56:27 -0700161import com.android.internal.telephony.euicc.EuiccConnector;
Brad Ebinger9c0eb502019-01-23 15:06:19 -0800162import com.android.internal.telephony.ims.ImsResolver;
Tyler Gunn7bcdc742019-10-04 15:56:59 -0700163import com.android.internal.telephony.imsphone.ImsPhone;
164import com.android.internal.telephony.imsphone.ImsPhoneCallTracker;
Pengquan Meng6c2dc9f2019-02-06 11:12:53 -0800165import com.android.internal.telephony.metrics.TelephonyMetrics;
Taesu Leef8fbed92019-10-07 18:47:02 +0900166import com.android.internal.telephony.uicc.IccCardApplicationStatus.AppType;
Shishir Agrawal566b7612013-10-28 14:41:00 -0700167import com.android.internal.telephony.uicc.IccIoResult;
changbetty363e8ac2019-12-06 18:16:37 +0800168import com.android.internal.telephony.uicc.IccRecords;
Shishir Agrawal566b7612013-10-28 14:41:00 -0700169import com.android.internal.telephony.uicc.IccUtils;
Nathan Haroldb3014052017-01-25 15:57:32 -0800170import com.android.internal.telephony.uicc.SIMRecords;
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700171import com.android.internal.telephony.uicc.UiccCard;
Nathan Haroldb3014052017-01-25 15:57:32 -0800172import com.android.internal.telephony.uicc.UiccCardApplication;
Shishir Agrawal566b7612013-10-28 14:41:00 -0700173import com.android.internal.telephony.uicc.UiccController;
Jeff Davidson7e17e312018-02-13 18:17:36 -0800174import com.android.internal.telephony.uicc.UiccProfile;
Holly Jiuyu Sun01c47ad2018-01-24 17:56:33 +0000175import com.android.internal.telephony.uicc.UiccSlot;
zoey chen84e2b212019-12-18 17:07:20 +0800176import com.android.internal.telephony.util.LocaleUtils;
fionaxu7ed723d2017-05-30 18:58:54 -0700177import com.android.internal.telephony.util.VoicemailNotificationSettingsUtil;
Hall Liua1acea22020-09-18 19:04:59 -0700178import com.android.internal.util.FunctionalUtils;
Jake Hambye994d462014-02-03 13:10:13 -0800179import com.android.internal.util.HexDump;
Brad Ebingera63db5f2019-04-23 16:31:13 -0700180import com.android.phone.settings.PickSmsSubscriptionActivity;
Ta-wei Yenc9df0432017-04-17 17:09:07 -0700181import com.android.phone.vvm.PhoneAccountHandleConverter;
Ta-wei Yen527a9c02017-01-06 15:29:25 -0800182import com.android.phone.vvm.RemoteVvmTaskManager;
Ta-wei Yenc9df0432017-04-17 17:09:07 -0700183import com.android.phone.vvm.VisualVoicemailSettingsUtil;
Ta-wei Yenc8905312017-03-28 11:14:45 -0700184import com.android.phone.vvm.VisualVoicemailSmsFilterConfig;
Jack Nudelman24d51a52020-11-24 12:08:04 -0800185import com.android.services.telephony.TelecomAccountRegistry;
186import com.android.services.telephony.TelephonyConnectionService;
Peter Wang050bb052020-01-13 23:33:09 -0800187import com.android.telephony.Rlog;
Ta-wei Yen30a69c82016-12-27 14:52:32 -0800188
Ta-wei Yenc236d6b2016-06-21 13:33:12 -0700189import java.io.FileDescriptor;
190import java.io.PrintWriter;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700191import java.util.ArrayList;
Jeff Sharkey85190e62014-12-05 09:40:12 -0800192import java.util.Arrays;
sqian11b7a0e2018-12-05 18:48:28 -0800193import java.util.HashMap;
sqianf4ca7ed2019-01-15 18:32:07 -0800194import java.util.HashSet;
Jake Hambye994d462014-02-03 13:10:13 -0800195import java.util.List;
Narayan Kamath1c496c22015-04-16 14:40:19 +0100196import java.util.Locale;
Jeff Sharkey85190e62014-12-05 09:40:12 -0800197import java.util.Map;
Nazanin Bakhshif71371d2019-04-29 17:29:44 -0700198import java.util.NoSuchElementException;
sqianf4ca7ed2019-01-15 18:32:07 -0800199import java.util.Set;
Peter Wangdafb9ac2020-01-15 14:13:38 -0800200import java.util.concurrent.atomic.AtomicBoolean;
Hall Liud0d1dc92020-01-20 13:42:00 -0800201import java.util.function.Consumer;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700202
203/**
204 * Implementation of the ITelephony interface.
205 */
Santos Cordon117fee72014-05-16 17:56:12 -0700206public class PhoneInterfaceManager extends ITelephony.Stub {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700207 private static final String LOG_TAG = "PhoneInterfaceManager";
208 private static final boolean DBG = (PhoneGlobals.DBG_LEVEL >= 2);
209 private static final boolean DBG_LOC = false;
Jeff Sharkey85190e62014-12-05 09:40:12 -0800210 private static final boolean DBG_MERGE = false;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700211
212 // Message codes used with mMainThreadHandler
213 private static final int CMD_HANDLE_PIN_MMI = 1;
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700214 private static final int CMD_TRANSMIT_APDU_LOGICAL_CHANNEL = 7;
215 private static final int EVENT_TRANSMIT_APDU_LOGICAL_CHANNEL_DONE = 8;
Shishir Agrawal566b7612013-10-28 14:41:00 -0700216 private static final int CMD_OPEN_CHANNEL = 9;
217 private static final int EVENT_OPEN_CHANNEL_DONE = 10;
218 private static final int CMD_CLOSE_CHANNEL = 11;
219 private static final int EVENT_CLOSE_CHANNEL_DONE = 12;
Jake Hambye994d462014-02-03 13:10:13 -0800220 private static final int CMD_NV_READ_ITEM = 13;
221 private static final int EVENT_NV_READ_ITEM_DONE = 14;
222 private static final int CMD_NV_WRITE_ITEM = 15;
223 private static final int EVENT_NV_WRITE_ITEM_DONE = 16;
224 private static final int CMD_NV_WRITE_CDMA_PRL = 17;
225 private static final int EVENT_NV_WRITE_CDMA_PRL_DONE = 18;
chen xu6dac5ab2018-10-26 17:39:23 -0700226 private static final int CMD_RESET_MODEM_CONFIG = 19;
227 private static final int EVENT_RESET_MODEM_CONFIG_DONE = 20;
Jake Hamby7c27be32014-03-03 13:25:59 -0800228 private static final int CMD_GET_PREFERRED_NETWORK_TYPE = 21;
229 private static final int EVENT_GET_PREFERRED_NETWORK_TYPE_DONE = 22;
230 private static final int CMD_SET_PREFERRED_NETWORK_TYPE = 23;
231 private static final int EVENT_SET_PREFERRED_NETWORK_TYPE_DONE = 24;
Sailesh Nepal35b59452014-03-06 09:26:56 -0800232 private static final int CMD_SEND_ENVELOPE = 25;
233 private static final int EVENT_SEND_ENVELOPE_DONE = 26;
Shuo Qian850e4d6a2018-04-25 21:02:08 +0000234 private static final int CMD_INVOKE_OEM_RIL_REQUEST_RAW = 27;
235 private static final int EVENT_INVOKE_OEM_RIL_REQUEST_RAW_DONE = 28;
Derek Tan6b088ee2014-09-05 14:15:18 -0700236 private static final int CMD_TRANSMIT_APDU_BASIC_CHANNEL = 29;
237 private static final int EVENT_TRANSMIT_APDU_BASIC_CHANNEL_DONE = 30;
238 private static final int CMD_EXCHANGE_SIM_IO = 31;
239 private static final int EVENT_EXCHANGE_SIM_IO_DONE = 32;
Shishir Agrawal76d5da92014-11-09 16:17:25 -0800240 private static final int CMD_SET_VOICEMAIL_NUMBER = 33;
241 private static final int EVENT_SET_VOICEMAIL_NUMBER_DONE = 34;
Stuart Scott54788802015-03-30 13:18:01 -0700242 private static final int CMD_SET_NETWORK_SELECTION_MODE_AUTOMATIC = 35;
243 private static final int EVENT_SET_NETWORK_SELECTION_MODE_AUTOMATIC_DONE = 36;
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -0700244 private static final int CMD_GET_MODEM_ACTIVITY_INFO = 37;
245 private static final int EVENT_GET_MODEM_ACTIVITY_INFO_DONE = 38;
Shishir Agrawal302c8692015-06-19 13:49:39 -0700246 private static final int CMD_PERFORM_NETWORK_SCAN = 39;
247 private static final int EVENT_PERFORM_NETWORK_SCAN_DONE = 40;
248 private static final int CMD_SET_NETWORK_SELECTION_MODE_MANUAL = 41;
249 private static final int EVENT_SET_NETWORK_SELECTION_MODE_MANUAL_DONE = 42;
Meng Wang1a7c35a2016-05-05 20:56:15 -0700250 private static final int CMD_SET_ALLOWED_CARRIERS = 43;
251 private static final int EVENT_SET_ALLOWED_CARRIERS_DONE = 44;
252 private static final int CMD_GET_ALLOWED_CARRIERS = 45;
253 private static final int EVENT_GET_ALLOWED_CARRIERS_DONE = 46;
pkanwar32d516d2016-10-14 19:37:38 -0700254 private static final int CMD_HANDLE_USSD_REQUEST = 47;
Nathan Haroldb3014052017-01-25 15:57:32 -0800255 private static final int CMD_GET_FORBIDDEN_PLMNS = 48;
256 private static final int EVENT_GET_FORBIDDEN_PLMNS_DONE = 49;
Holly Jiuyu Sun01c47ad2018-01-24 17:56:33 +0000257 private static final int CMD_SWITCH_SLOTS = 50;
258 private static final int EVENT_SWITCH_SLOTS_DONE = 51;
Pengquan Menga1bb6272018-09-06 09:59:22 -0700259 private static final int CMD_GET_NETWORK_SELECTION_MODE = 52;
260 private static final int EVENT_GET_NETWORK_SELECTION_MODE_DONE = 53;
261 private static final int CMD_GET_CDMA_ROAMING_MODE = 54;
262 private static final int EVENT_GET_CDMA_ROAMING_MODE_DONE = 55;
263 private static final int CMD_SET_CDMA_ROAMING_MODE = 56;
264 private static final int EVENT_SET_CDMA_ROAMING_MODE_DONE = 57;
265 private static final int CMD_SET_CDMA_SUBSCRIPTION_MODE = 58;
266 private static final int EVENT_SET_CDMA_SUBSCRIPTION_MODE_DONE = 59;
Nathan Harold3ff88932018-08-14 10:19:49 -0700267 private static final int CMD_GET_ALL_CELL_INFO = 60;
268 private static final int EVENT_GET_ALL_CELL_INFO_DONE = 61;
269 private static final int CMD_GET_CELL_LOCATION = 62;
270 private static final int EVENT_GET_CELL_LOCATION_DONE = 63;
chen xu6dac5ab2018-10-26 17:39:23 -0700271 private static final int CMD_MODEM_REBOOT = 64;
272 private static final int EVENT_CMD_MODEM_REBOOT_DONE = 65;
Nathan Haroldfa8da0f2018-09-27 18:51:29 -0700273 private static final int CMD_REQUEST_CELL_INFO_UPDATE = 66;
274 private static final int EVENT_REQUEST_CELL_INFO_UPDATE_DONE = 67;
Malcolm Chen8e4ed912019-01-15 20:22:16 -0800275 private static final int CMD_REQUEST_ENABLE_MODEM = 68;
276 private static final int EVENT_ENABLE_MODEM_DONE = 69;
Nazanin Bakhshif71371d2019-04-29 17:29:44 -0700277 private static final int CMD_GET_MODEM_STATUS = 70;
278 private static final int EVENT_GET_MODEM_STATUS_DONE = 71;
yincheng zhaod698b842019-09-06 17:06:54 -0700279 private static final int CMD_SET_FORBIDDEN_PLMNS = 72;
280 private static final int EVENT_SET_FORBIDDEN_PLMNS_DONE = 73;
Naina Nalluri8ff344d2019-09-17 14:10:30 -0700281 private static final int CMD_ERASE_MODEM_CONFIG = 74;
282 private static final int EVENT_ERASE_MODEM_CONFIG_DONE = 75;
zoey chenf95ca592019-12-30 16:11:23 +0800283 private static final int CMD_CHANGE_ICC_LOCK_PASSWORD = 76;
284 private static final int EVENT_CHANGE_ICC_LOCK_PASSWORD_DONE = 77;
285 private static final int CMD_SET_ICC_LOCK_ENABLED = 78;
286 private static final int EVENT_SET_ICC_LOCK_ENABLED_DONE = 79;
Hall Liud0d1dc92020-01-20 13:42:00 -0800287 private static final int CMD_SET_SYSTEM_SELECTION_CHANNELS = 80;
288 private static final int EVENT_SET_SYSTEM_SELECTION_CHANNELS_DONE = 81;
Peter Wangdafb9ac2020-01-15 14:13:38 -0800289 private static final int MSG_NOTIFY_USER_ACTIVITY = 82;
sqian80370722020-01-29 15:02:51 -0800290 private static final int CMD_GET_CALL_FORWARDING = 83;
291 private static final int EVENT_GET_CALL_FORWARDING_DONE = 84;
292 private static final int CMD_SET_CALL_FORWARDING = 85;
293 private static final int EVENT_SET_CALL_FORWARDING_DONE = 86;
294 private static final int CMD_GET_CALL_WAITING = 87;
295 private static final int EVENT_GET_CALL_WAITING_DONE = 88;
296 private static final int CMD_SET_CALL_WAITING = 89;
297 private static final int EVENT_SET_CALL_WAITING_DONE = 90;
Sooraj Sasindranb4a99602020-08-11 10:40:43 -0700298 private static final int CMD_ENABLE_NR_DUAL_CONNECTIVITY = 91;
299 private static final int EVENT_ENABLE_NR_DUAL_CONNECTIVITY_DONE = 92;
300 private static final int CMD_IS_NR_DUAL_CONNECTIVITY_ENABLED = 93;
301 private static final int EVENT_IS_NR_DUAL_CONNECTIVITY_ENABLED_DONE = 94;
Sarah Chin49f22af2020-10-28 13:46:24 -0700302 private static final int CMD_GET_CDMA_SUBSCRIPTION_MODE = 95;
303 private static final int EVENT_GET_CDMA_SUBSCRIPTION_MODE_DONE = 96;
Sarah Chincc055732020-11-18 13:39:35 -0800304 private static final int CMD_GET_SYSTEM_SELECTION_CHANNELS = 97;
305 private static final int EVENT_GET_SYSTEM_SELECTION_CHANNELS_DONE = 98;
Jack Nudelman24d51a52020-11-24 12:08:04 -0800306 private static final int CMD_SET_DATA_THROTTLING = 99;
307 private static final int EVENT_SET_DATA_THROTTLING_DONE = 100;
Jordan Liud5366d92020-11-24 14:50:34 -0800308 private static final int CMD_SET_SIM_POWER = 101;
309 private static final int EVENT_SET_SIM_POWER_DONE = 102;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700310
Holly Jiuyu Sun1cc2d552018-01-26 15:51:16 -0800311 // Parameters of select command.
312 private static final int SELECT_COMMAND = 0xA4;
313 private static final int SELECT_P1 = 0x04;
314 private static final int SELECT_P2 = 0;
315 private static final int SELECT_P3 = 0x10;
316
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700317 /** The singleton instance. */
318 private static PhoneInterfaceManager sInstance;
319
Wink Saville3ab207e2014-11-20 13:07:20 -0800320 private PhoneGlobals mApp;
Wink Saville3ab207e2014-11-20 13:07:20 -0800321 private CallManager mCM;
Brad Ebinger05f52c22019-12-05 13:03:21 -0800322 private ImsResolver mImsResolver;
Stuart Scott981d8582015-04-21 14:09:50 -0700323 private UserManager mUserManager;
Wink Saville3ab207e2014-11-20 13:07:20 -0800324 private AppOpsManager mAppOps;
325 private MainThreadHandler mMainThreadHandler;
Wink Savilleac1bdfd2014-11-20 23:04:44 -0800326 private SubscriptionController mSubscriptionController;
Wink Saville3ab207e2014-11-20 13:07:20 -0800327 private SharedPreferences mTelephonySharedPreferences;
Malcolm Chen2c63d402018-08-14 16:00:53 -0700328 private PhoneConfigurationManager mPhoneConfigurationManager;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700329
Peter Wangdafb9ac2020-01-15 14:13:38 -0800330 /** User Activity */
331 private AtomicBoolean mNotifyUserActivity;
Peter Wangdafb9ac2020-01-15 14:13:38 -0800332 private static final int USER_ACTIVITY_NOTIFICATION_DELAY = 200;
333
Jeff Davidson0103e8c2020-03-28 12:24:40 -0700334 private Set<Integer> mCarrierPrivilegeTestOverrideSubIds = new ArraySet<>();
335
Derek Tan97ebb422014-09-05 16:55:38 -0700336 private static final String PREF_CARRIERS_ALPHATAG_PREFIX = "carrier_alphtag_";
337 private static final String PREF_CARRIERS_NUMBER_PREFIX = "carrier_number_";
Jeff Sharkey85190e62014-12-05 09:40:12 -0800338 private static final String PREF_CARRIERS_SUBSCRIBER_PREFIX = "carrier_subscriber_";
Brad Ebinger1c8542e2019-01-14 13:43:14 -0800339 private static final String PREF_PROVISION_IMS_MMTEL_PREFIX = "provision_ims_mmtel_";
Derek Tan89e89d42014-07-08 17:00:10 -0700340
Michelecea4cf22018-12-21 15:00:11 -0800341 // String to store multi SIM allowed
342 private static final String PREF_MULTI_SIM_RESTRICTED = "multisim_restricted";
343
Derek Tan740e1672017-06-27 14:56:27 -0700344 // The AID of ISD-R.
345 private static final String ISDR_AID = "A0000005591010FFFFFFFF8900000100";
346
yinxub1bed742017-04-17 11:45:04 -0700347 private NetworkScanRequestTracker mNetworkScanRequestTracker;
348
David Kelly5e06a7f2018-03-12 14:10:59 +0000349 private static final int TYPE_ALLOCATION_CODE_LENGTH = 8;
350 private static final int MANUFACTURER_CODE_LENGTH = 8;
351
Jack Nudelman24d51a52020-11-24 12:08:04 -0800352 private static final int SET_DATA_THROTTLING_MODEM_THREW_INVALID_PARAMS = -1;
353
Derek Tan89e89d42014-07-08 17:00:10 -0700354 /**
Naina Nalluri8ff344d2019-09-17 14:10:30 -0700355 * Experiment flag to enable erase modem config on reset network, default value is false
356 */
357 public static final String RESET_NETWORK_ERASE_MODEM_CONFIG_ENABLED =
358 "reset_network_erase_modem_config_enabled";
359
360 /**
Shishir Agrawal566b7612013-10-28 14:41:00 -0700361 * A request object to use for transmitting data to an ICC.
362 */
363 private static final class IccAPDUArgument {
364 public int channel, cla, command, p1, p2, p3;
365 public String data;
366
367 public IccAPDUArgument(int channel, int cla, int command,
368 int p1, int p2, int p3, String data) {
369 this.channel = channel;
370 this.cla = cla;
371 this.command = command;
372 this.p1 = p1;
373 this.p2 = p2;
374 this.p3 = p3;
375 this.data = data;
376 }
377 }
378
379 /**
Shishir Agrawal77ba3172015-09-10 14:50:19 -0700380 * A request object to use for transmitting data to an ICC.
381 */
382 private static final class ManualNetworkSelectionArgument {
383 public OperatorInfo operatorInfo;
384 public boolean persistSelection;
385
386 public ManualNetworkSelectionArgument(OperatorInfo operatorInfo, boolean persistSelection) {
387 this.operatorInfo = operatorInfo;
388 this.persistSelection = persistSelection;
389 }
390 }
391
392 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700393 * A request object for use with {@link MainThreadHandler}. Requesters should wait() on the
394 * request after sending. The main thread will notify the request when it is complete.
395 */
396 private static final class MainThreadRequest {
397 /** The argument to use for the request */
398 public Object argument;
399 /** The result of the request that is run on the main thread */
400 public Object result;
Sanket Padawe56e75a32016-02-08 12:18:19 -0800401 // The subscriber id that this request applies to. Defaults to
402 // SubscriptionManager.INVALID_SUBSCRIPTION_ID
403 public Integer subId = SubscriptionManager.INVALID_SUBSCRIPTION_ID;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700404
Nathan Harold92bed182018-10-12 18:16:49 -0700405 // In cases where subId is unavailable, the caller needs to specify the phone.
406 public Phone phone;
407
vagdeviaf9a5b92018-08-15 16:01:53 -0700408 public WorkSource workSource;
409
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700410 public MainThreadRequest(Object argument) {
411 this.argument = argument;
412 }
Shishir Agrawal76d5da92014-11-09 16:17:25 -0800413
Nathan Harold92bed182018-10-12 18:16:49 -0700414 MainThreadRequest(Object argument, Phone phone, WorkSource workSource) {
415 this.argument = argument;
416 if (phone != null) {
417 this.phone = phone;
418 }
419 this.workSource = workSource;
420 }
421
vagdeviaf9a5b92018-08-15 16:01:53 -0700422 MainThreadRequest(Object argument, Integer subId, WorkSource workSource) {
Shishir Agrawal76d5da92014-11-09 16:17:25 -0800423 this.argument = argument;
Sanket Padawe56e75a32016-02-08 12:18:19 -0800424 if (subId != null) {
425 this.subId = subId;
426 }
vagdeviaf9a5b92018-08-15 16:01:53 -0700427 this.workSource = workSource;
Shishir Agrawal76d5da92014-11-09 16:17:25 -0800428 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700429 }
430
Sailesh Nepalcc0375f2013-11-13 09:15:18 -0800431 private static final class IncomingThirdPartyCallArgs {
432 public final ComponentName component;
433 public final String callId;
434 public final String callerDisplayName;
435
436 public IncomingThirdPartyCallArgs(ComponentName component, String callId,
437 String callerDisplayName) {
438 this.component = component;
439 this.callId = callId;
440 this.callerDisplayName = callerDisplayName;
441 }
442 }
443
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700444 /**
445 * A handler that processes messages on the main thread in the phone process. Since many
446 * of the Phone calls are not thread safe this is needed to shuttle the requests from the
447 * inbound binder threads to the main thread in the phone process. The Binder thread
448 * may provide a {@link MainThreadRequest} object in the msg.obj field that they are waiting
449 * on, which will be notified when the operation completes and will contain the result of the
450 * request.
451 *
452 * <p>If a MainThreadRequest object is provided in the msg.obj field,
453 * note that request.result must be set to something non-null for the calling thread to
454 * unblock.
455 */
456 private final class MainThreadHandler extends Handler {
457 @Override
458 public void handleMessage(Message msg) {
459 MainThreadRequest request;
460 Message onCompleted;
461 AsyncResult ar;
Shishir Agrawalc04d9752016-02-19 10:41:00 -0800462 UiccCard uiccCard;
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700463 IccAPDUArgument iccArgument;
Nazanin Bakhshif782e562018-12-11 15:15:39 -0800464 final Phone defaultPhone = getDefaultPhone();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700465
466 switch (msg.what) {
Pengquan Menga1bb6272018-09-06 09:59:22 -0700467 case CMD_HANDLE_USSD_REQUEST: {
468 request = (MainThreadRequest) msg.obj;
469 final Phone phone = getPhoneFromRequest(request);
470 Pair<String, ResultReceiver> ussdObject = (Pair) request.argument;
471 String ussdRequest = ussdObject.first;
472 ResultReceiver wrappedCallback = ussdObject.second;
Tyler Gunn65d45c22017-06-05 11:22:26 -0700473
Pengquan Menga1bb6272018-09-06 09:59:22 -0700474 if (!isUssdApiAllowed(request.subId)) {
475 // Carrier does not support use of this API, return failure.
476 Rlog.w(LOG_TAG, "handleUssdRequest: carrier does not support USSD apis.");
477 UssdResponse response = new UssdResponse(ussdRequest, null);
478 Bundle returnData = new Bundle();
479 returnData.putParcelable(TelephonyManager.USSD_RESPONSE, response);
480 wrappedCallback.send(TelephonyManager.USSD_RETURN_FAILURE, returnData);
Tyler Gunn65d45c22017-06-05 11:22:26 -0700481
Pengquan Menga1bb6272018-09-06 09:59:22 -0700482 request.result = true;
483 notifyRequester(request);
484 return;
485 }
Tyler Gunn65d45c22017-06-05 11:22:26 -0700486
Pengquan Menga1bb6272018-09-06 09:59:22 -0700487 try {
488 request.result = phone != null
489 ? phone.handleUssdRequest(ussdRequest, wrappedCallback) : false;
490 } catch (CallStateException cse) {
491 request.result = false;
492 }
493 // Wake up the requesting thread
494 notifyRequester(request);
495 break;
pkanwar32d516d2016-10-14 19:37:38 -0700496 }
497
Yorke Lee716f67e2015-06-17 15:39:16 -0700498 case CMD_HANDLE_PIN_MMI: {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700499 request = (MainThreadRequest) msg.obj;
Yorke Lee716f67e2015-06-17 15:39:16 -0700500 final Phone phone = getPhoneFromRequest(request);
501 request.result = phone != null ?
502 getPhoneFromRequest(request).handlePinMmi((String) request.argument)
503 : false;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700504 // Wake up the requesting thread
Pengquan Menga1bb6272018-09-06 09:59:22 -0700505 notifyRequester(request);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700506 break;
Yorke Lee716f67e2015-06-17 15:39:16 -0700507 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700508
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700509 case CMD_TRANSMIT_APDU_LOGICAL_CHANNEL:
Shishir Agrawal566b7612013-10-28 14:41:00 -0700510 request = (MainThreadRequest) msg.obj;
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700511 iccArgument = (IccAPDUArgument) request.argument;
Shishir Agrawalc04d9752016-02-19 10:41:00 -0800512 uiccCard = getUiccCardFromRequest(request);
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700513 if (uiccCard == null) {
514 loge("iccTransmitApduLogicalChannel: No UICC");
515 request.result = new IccIoResult(0x6F, 0, (byte[])null);
Pengquan Menga1bb6272018-09-06 09:59:22 -0700516 notifyRequester(request);
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700517 } else {
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700518 onCompleted = obtainMessage(EVENT_TRANSMIT_APDU_LOGICAL_CHANNEL_DONE,
519 request);
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700520 uiccCard.iccTransmitApduLogicalChannel(
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700521 iccArgument.channel, iccArgument.cla, iccArgument.command,
522 iccArgument.p1, iccArgument.p2, iccArgument.p3, iccArgument.data,
Shishir Agrawal566b7612013-10-28 14:41:00 -0700523 onCompleted);
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700524 }
Shishir Agrawal566b7612013-10-28 14:41:00 -0700525 break;
526
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700527 case EVENT_TRANSMIT_APDU_LOGICAL_CHANNEL_DONE:
Shishir Agrawal566b7612013-10-28 14:41:00 -0700528 ar = (AsyncResult) msg.obj;
529 request = (MainThreadRequest) ar.userObj;
530 if (ar.exception == null && ar.result != null) {
531 request.result = ar.result;
532 } else {
533 request.result = new IccIoResult(0x6F, 0, (byte[])null);
534 if (ar.result == null) {
535 loge("iccTransmitApduLogicalChannel: Empty response");
Jake Hambye994d462014-02-03 13:10:13 -0800536 } else if (ar.exception instanceof CommandException) {
Shishir Agrawal566b7612013-10-28 14:41:00 -0700537 loge("iccTransmitApduLogicalChannel: CommandException: " +
Jake Hambye994d462014-02-03 13:10:13 -0800538 ar.exception);
Shishir Agrawal566b7612013-10-28 14:41:00 -0700539 } else {
540 loge("iccTransmitApduLogicalChannel: Unknown exception");
541 }
542 }
Pengquan Menga1bb6272018-09-06 09:59:22 -0700543 notifyRequester(request);
Shishir Agrawal566b7612013-10-28 14:41:00 -0700544 break;
545
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700546 case CMD_TRANSMIT_APDU_BASIC_CHANNEL:
547 request = (MainThreadRequest) msg.obj;
548 iccArgument = (IccAPDUArgument) request.argument;
Shishir Agrawalc04d9752016-02-19 10:41:00 -0800549 uiccCard = getUiccCardFromRequest(request);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700550 if (uiccCard == null) {
551 loge("iccTransmitApduBasicChannel: No UICC");
552 request.result = new IccIoResult(0x6F, 0, (byte[])null);
Pengquan Menga1bb6272018-09-06 09:59:22 -0700553 notifyRequester(request);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700554 } else {
555 onCompleted = obtainMessage(EVENT_TRANSMIT_APDU_BASIC_CHANNEL_DONE,
556 request);
557 uiccCard.iccTransmitApduBasicChannel(
558 iccArgument.cla, iccArgument.command, iccArgument.p1, iccArgument.p2,
559 iccArgument.p3, iccArgument.data, onCompleted);
560 }
561 break;
562
563 case EVENT_TRANSMIT_APDU_BASIC_CHANNEL_DONE:
564 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("iccTransmitApduBasicChannel: Empty response");
572 } else if (ar.exception instanceof CommandException) {
573 loge("iccTransmitApduBasicChannel: CommandException: " +
574 ar.exception);
575 } else {
576 loge("iccTransmitApduBasicChannel: Unknown exception");
577 }
578 }
Pengquan Menga1bb6272018-09-06 09:59:22 -0700579 notifyRequester(request);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700580 break;
581
582 case CMD_EXCHANGE_SIM_IO:
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("iccExchangeSimIO: 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_EXCHANGE_SIM_IO_DONE,
592 request);
593 uiccCard.iccExchangeSimIO(iccArgument.cla, /* fileID */
594 iccArgument.command, iccArgument.p1, iccArgument.p2, iccArgument.p3,
595 iccArgument.data, onCompleted);
596 }
597 break;
598
599 case EVENT_EXCHANGE_SIM_IO_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 }
Pengquan Menga1bb6272018-09-06 09:59:22 -0700607 notifyRequester(request);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700608 break;
609
Derek Tan4d5e5c12014-02-04 11:54:58 -0800610 case CMD_SEND_ENVELOPE:
611 request = (MainThreadRequest) msg.obj;
Shishir Agrawalc04d9752016-02-19 10:41:00 -0800612 uiccCard = getUiccCardFromRequest(request);
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700613 if (uiccCard == null) {
614 loge("sendEnvelopeWithStatus: No UICC");
615 request.result = new IccIoResult(0x6F, 0, (byte[])null);
Pengquan Menga1bb6272018-09-06 09:59:22 -0700616 notifyRequester(request);
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700617 } else {
618 onCompleted = obtainMessage(EVENT_SEND_ENVELOPE_DONE, request);
619 uiccCard.sendEnvelopeWithStatus((String)request.argument, onCompleted);
620 }
Derek Tan4d5e5c12014-02-04 11:54:58 -0800621 break;
622
623 case EVENT_SEND_ENVELOPE_DONE:
624 ar = (AsyncResult) msg.obj;
625 request = (MainThreadRequest) ar.userObj;
Shishir Agrawal9f9877d2014-03-14 09:36:27 -0700626 if (ar.exception == null && ar.result != null) {
627 request.result = ar.result;
Derek Tan4d5e5c12014-02-04 11:54:58 -0800628 } else {
Shishir Agrawal9f9877d2014-03-14 09:36:27 -0700629 request.result = new IccIoResult(0x6F, 0, (byte[])null);
630 if (ar.result == null) {
631 loge("sendEnvelopeWithStatus: Empty response");
632 } else if (ar.exception instanceof CommandException) {
633 loge("sendEnvelopeWithStatus: CommandException: " +
634 ar.exception);
635 } else {
636 loge("sendEnvelopeWithStatus: exception:" + ar.exception);
637 }
Derek Tan4d5e5c12014-02-04 11:54:58 -0800638 }
Pengquan Menga1bb6272018-09-06 09:59:22 -0700639 notifyRequester(request);
Derek Tan4d5e5c12014-02-04 11:54:58 -0800640 break;
641
Shishir Agrawal566b7612013-10-28 14:41:00 -0700642 case CMD_OPEN_CHANNEL:
643 request = (MainThreadRequest) msg.obj;
Shishir Agrawalc04d9752016-02-19 10:41:00 -0800644 uiccCard = getUiccCardFromRequest(request);
Ajay Nambid7454d32015-12-03 13:50:00 -0800645 Pair<String, Integer> openChannelArgs = (Pair<String, Integer>) request.argument;
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700646 if (uiccCard == null) {
647 loge("iccOpenLogicalChannel: No UICC");
Shishir Agrawalfc0492a2016-02-17 11:15:33 -0800648 request.result = new IccOpenLogicalChannelResponse(-1,
649 IccOpenLogicalChannelResponse.STATUS_MISSING_RESOURCE, null);
Pengquan Menga1bb6272018-09-06 09:59:22 -0700650 notifyRequester(request);
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700651 } else {
652 onCompleted = obtainMessage(EVENT_OPEN_CHANNEL_DONE, request);
Ajay Nambid7454d32015-12-03 13:50:00 -0800653 uiccCard.iccOpenLogicalChannel(openChannelArgs.first,
654 openChannelArgs.second, onCompleted);
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700655 }
Shishir Agrawal566b7612013-10-28 14:41:00 -0700656 break;
657
658 case EVENT_OPEN_CHANNEL_DONE:
659 ar = (AsyncResult) msg.obj;
660 request = (MainThreadRequest) ar.userObj;
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700661 IccOpenLogicalChannelResponse openChannelResp;
Shishir Agrawal566b7612013-10-28 14:41:00 -0700662 if (ar.exception == null && ar.result != null) {
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700663 int[] result = (int[]) ar.result;
664 int channelId = result[0];
665 byte[] selectResponse = null;
666 if (result.length > 1) {
667 selectResponse = new byte[result.length - 1];
668 for (int i = 1; i < result.length; ++i) {
669 selectResponse[i - 1] = (byte) result[i];
670 }
671 }
672 openChannelResp = new IccOpenLogicalChannelResponse(channelId,
Shishir Agrawal527e8bf2014-08-25 08:54:56 -0700673 IccOpenLogicalChannelResponse.STATUS_NO_ERROR, selectResponse);
Shishir Agrawal566b7612013-10-28 14:41:00 -0700674 } else {
Shishir Agrawal566b7612013-10-28 14:41:00 -0700675 if (ar.result == null) {
676 loge("iccOpenLogicalChannel: Empty response");
Shishir Agrawal566b7612013-10-28 14:41:00 -0700677 }
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700678 if (ar.exception != null) {
679 loge("iccOpenLogicalChannel: Exception: " + ar.exception);
680 }
681
Shishir Agrawal527e8bf2014-08-25 08:54:56 -0700682 int errorCode = IccOpenLogicalChannelResponse.STATUS_UNKNOWN_ERROR;
Junda Liua754ba12015-05-20 01:17:52 -0700683 if (ar.exception instanceof CommandException) {
684 CommandException.Error error =
685 ((CommandException) (ar.exception)).getCommandError();
686 if (error == CommandException.Error.MISSING_RESOURCE) {
Shishir Agrawal527e8bf2014-08-25 08:54:56 -0700687 errorCode = IccOpenLogicalChannelResponse.STATUS_MISSING_RESOURCE;
Junda Liua754ba12015-05-20 01:17:52 -0700688 } else if (error == CommandException.Error.NO_SUCH_ELEMENT) {
Shishir Agrawal527e8bf2014-08-25 08:54:56 -0700689 errorCode = IccOpenLogicalChannelResponse.STATUS_NO_SUCH_ELEMENT;
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700690 }
691 }
692 openChannelResp = new IccOpenLogicalChannelResponse(
693 IccOpenLogicalChannelResponse.INVALID_CHANNEL, errorCode, null);
Shishir Agrawal566b7612013-10-28 14:41:00 -0700694 }
Shishir Agrawal82c8a462014-07-31 18:13:17 -0700695 request.result = openChannelResp;
Pengquan Menga1bb6272018-09-06 09:59:22 -0700696 notifyRequester(request);
Shishir Agrawal566b7612013-10-28 14:41:00 -0700697 break;
698
699 case CMD_CLOSE_CHANNEL:
700 request = (MainThreadRequest) msg.obj;
Shishir Agrawalc04d9752016-02-19 10:41:00 -0800701 uiccCard = getUiccCardFromRequest(request);
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700702 if (uiccCard == null) {
703 loge("iccCloseLogicalChannel: No UICC");
Yoshiaki Naka2e29d822016-09-02 19:27:39 +0900704 request.result = false;
Pengquan Menga1bb6272018-09-06 09:59:22 -0700705 notifyRequester(request);
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700706 } else {
707 onCompleted = obtainMessage(EVENT_CLOSE_CHANNEL_DONE, request);
708 uiccCard.iccCloseLogicalChannel((Integer) request.argument, onCompleted);
709 }
Shishir Agrawal566b7612013-10-28 14:41:00 -0700710 break;
711
712 case EVENT_CLOSE_CHANNEL_DONE:
Jake Hambye994d462014-02-03 13:10:13 -0800713 handleNullReturnEvent(msg, "iccCloseLogicalChannel");
714 break;
715
716 case CMD_NV_READ_ITEM:
717 request = (MainThreadRequest) msg.obj;
718 onCompleted = obtainMessage(EVENT_NV_READ_ITEM_DONE, request);
Nazanin Bakhshif782e562018-12-11 15:15:39 -0800719 defaultPhone.nvReadItem((Integer) request.argument, onCompleted,
720 request.workSource);
Jake Hambye994d462014-02-03 13:10:13 -0800721 break;
722
723 case EVENT_NV_READ_ITEM_DONE:
Shishir Agrawal566b7612013-10-28 14:41:00 -0700724 ar = (AsyncResult) msg.obj;
725 request = (MainThreadRequest) ar.userObj;
Jake Hambye994d462014-02-03 13:10:13 -0800726 if (ar.exception == null && ar.result != null) {
727 request.result = ar.result; // String
Shishir Agrawal566b7612013-10-28 14:41:00 -0700728 } else {
Jake Hambye994d462014-02-03 13:10:13 -0800729 request.result = "";
730 if (ar.result == null) {
731 loge("nvReadItem: Empty response");
732 } else if (ar.exception instanceof CommandException) {
733 loge("nvReadItem: CommandException: " +
734 ar.exception);
Shishir Agrawal566b7612013-10-28 14:41:00 -0700735 } else {
Jake Hambye994d462014-02-03 13:10:13 -0800736 loge("nvReadItem: Unknown exception");
Shishir Agrawal566b7612013-10-28 14:41:00 -0700737 }
738 }
Pengquan Menga1bb6272018-09-06 09:59:22 -0700739 notifyRequester(request);
Shishir Agrawal566b7612013-10-28 14:41:00 -0700740 break;
741
Jake Hambye994d462014-02-03 13:10:13 -0800742 case CMD_NV_WRITE_ITEM:
743 request = (MainThreadRequest) msg.obj;
744 onCompleted = obtainMessage(EVENT_NV_WRITE_ITEM_DONE, request);
745 Pair<Integer, String> idValue = (Pair<Integer, String>) request.argument;
Nazanin Bakhshif782e562018-12-11 15:15:39 -0800746 defaultPhone.nvWriteItem(idValue.first, idValue.second, onCompleted,
vagdeviaf9a5b92018-08-15 16:01:53 -0700747 request.workSource);
Jake Hambye994d462014-02-03 13:10:13 -0800748 break;
749
750 case EVENT_NV_WRITE_ITEM_DONE:
751 handleNullReturnEvent(msg, "nvWriteItem");
752 break;
753
754 case CMD_NV_WRITE_CDMA_PRL:
755 request = (MainThreadRequest) msg.obj;
756 onCompleted = obtainMessage(EVENT_NV_WRITE_CDMA_PRL_DONE, request);
Nazanin Bakhshif782e562018-12-11 15:15:39 -0800757 defaultPhone.nvWriteCdmaPrl((byte[]) request.argument, onCompleted);
Jake Hambye994d462014-02-03 13:10:13 -0800758 break;
759
760 case EVENT_NV_WRITE_CDMA_PRL_DONE:
761 handleNullReturnEvent(msg, "nvWriteCdmaPrl");
762 break;
763
chen xu6dac5ab2018-10-26 17:39:23 -0700764 case CMD_RESET_MODEM_CONFIG:
Jake Hambye994d462014-02-03 13:10:13 -0800765 request = (MainThreadRequest) msg.obj;
chen xu6dac5ab2018-10-26 17:39:23 -0700766 onCompleted = obtainMessage(EVENT_RESET_MODEM_CONFIG_DONE, request);
Nazanin Bakhshif782e562018-12-11 15:15:39 -0800767 defaultPhone.resetModemConfig(onCompleted);
Jake Hambye994d462014-02-03 13:10:13 -0800768 break;
769
chen xu6dac5ab2018-10-26 17:39:23 -0700770 case EVENT_RESET_MODEM_CONFIG_DONE:
771 handleNullReturnEvent(msg, "resetModemConfig");
Jake Hambye994d462014-02-03 13:10:13 -0800772 break;
773
Sooraj Sasindranb4a99602020-08-11 10:40:43 -0700774 case CMD_IS_NR_DUAL_CONNECTIVITY_ENABLED: {
775 request = (MainThreadRequest) msg.obj;
776 onCompleted = obtainMessage(EVENT_IS_NR_DUAL_CONNECTIVITY_ENABLED_DONE,
777 request);
778 Phone phone = getPhoneFromRequest(request);
779 if (phone != null) {
780 phone.isNrDualConnectivityEnabled(onCompleted, request.workSource);
781 } else {
782 loge("isNRDualConnectivityEnabled: No phone object");
783 request.result = false;
784 notifyRequester(request);
785 }
786 break;
787 }
788
789 case EVENT_IS_NR_DUAL_CONNECTIVITY_ENABLED_DONE:
790 ar = (AsyncResult) msg.obj;
791 request = (MainThreadRequest) ar.userObj;
792 if (ar.exception == null && ar.result != null) {
793 request.result = ar.result;
794 } else {
795 // request.result must be set to something non-null
796 // for the calling thread to unblock
797 if (request.result != null) {
798 request.result = ar.result;
799 } else {
800 request.result = false;
801 }
802 if (ar.result == null) {
803 loge("isNRDualConnectivityEnabled: Empty response");
804 } else if (ar.exception instanceof CommandException) {
805 loge("isNRDualConnectivityEnabled: CommandException: "
806 + ar.exception);
807 } else {
808 loge("isNRDualConnectivityEnabled: Unknown exception");
809 }
810 }
811 notifyRequester(request);
812 break;
813
814 case CMD_ENABLE_NR_DUAL_CONNECTIVITY: {
815 request = (MainThreadRequest) msg.obj;
816 onCompleted = obtainMessage(EVENT_ENABLE_NR_DUAL_CONNECTIVITY_DONE, request);
817 Phone phone = getPhoneFromRequest(request);
818 if (phone != null) {
819 phone.setNrDualConnectivityState((int) request.argument, onCompleted,
820 request.workSource);
821 } else {
822 loge("enableNrDualConnectivity: No phone object");
823 request.result =
824 TelephonyManager.ENABLE_NR_DUAL_CONNECTIVITY_RADIO_NOT_AVAILABLE;
825 notifyRequester(request);
826 }
827 break;
828 }
829
830 case EVENT_ENABLE_NR_DUAL_CONNECTIVITY_DONE: {
831 ar = (AsyncResult) msg.obj;
832 request = (MainThreadRequest) ar.userObj;
833 if (ar.exception == null) {
834 request.result =
835 TelephonyManager.ENABLE_NR_DUAL_CONNECTIVITY_SUCCESS;
836 } else {
837 request.result =
838 TelephonyManager
839 .ENABLE_NR_DUAL_CONNECTIVITY_RADIO_ERROR;
840 if (ar.exception instanceof CommandException) {
841 CommandException.Error error =
842 ((CommandException) (ar.exception)).getCommandError();
843 if (error == CommandException.Error.RADIO_NOT_AVAILABLE) {
844 request.result =
845 TelephonyManager
846 .ENABLE_NR_DUAL_CONNECTIVITY_RADIO_NOT_AVAILABLE;
847 }
848 loge("enableNrDualConnectivity" + ": CommandException: "
849 + ar.exception);
850 } else {
851 loge("enableNrDualConnectivity" + ": Unknown exception");
852 }
853 }
854 notifyRequester(request);
855 break;
856 }
857
Jake Hamby7c27be32014-03-03 13:25:59 -0800858 case CMD_GET_PREFERRED_NETWORK_TYPE:
859 request = (MainThreadRequest) msg.obj;
860 onCompleted = obtainMessage(EVENT_GET_PREFERRED_NETWORK_TYPE_DONE, request);
Stuart Scott54788802015-03-30 13:18:01 -0700861 getPhoneFromRequest(request).getPreferredNetworkType(onCompleted);
Jake Hamby7c27be32014-03-03 13:25:59 -0800862 break;
863
864 case EVENT_GET_PREFERRED_NETWORK_TYPE_DONE:
865 ar = (AsyncResult) msg.obj;
866 request = (MainThreadRequest) ar.userObj;
867 if (ar.exception == null && ar.result != null) {
868 request.result = ar.result; // Integer
869 } else {
Nazish Tabassume8ba43a2020-07-28 14:49:25 +0530870 // request.result must be set to something non-null
871 // for the calling thread to unblock
872 request.result = new int[]{-1};
Jake Hamby7c27be32014-03-03 13:25:59 -0800873 if (ar.result == null) {
874 loge("getPreferredNetworkType: Empty response");
875 } else if (ar.exception instanceof CommandException) {
876 loge("getPreferredNetworkType: CommandException: " +
877 ar.exception);
878 } else {
879 loge("getPreferredNetworkType: Unknown exception");
880 }
881 }
Pengquan Menga1bb6272018-09-06 09:59:22 -0700882 notifyRequester(request);
Jake Hamby7c27be32014-03-03 13:25:59 -0800883 break;
884
885 case CMD_SET_PREFERRED_NETWORK_TYPE:
886 request = (MainThreadRequest) msg.obj;
887 onCompleted = obtainMessage(EVENT_SET_PREFERRED_NETWORK_TYPE_DONE, request);
888 int networkType = (Integer) request.argument;
Stuart Scott54788802015-03-30 13:18:01 -0700889 getPhoneFromRequest(request).setPreferredNetworkType(networkType, onCompleted);
Jake Hamby7c27be32014-03-03 13:25:59 -0800890 break;
891
892 case EVENT_SET_PREFERRED_NETWORK_TYPE_DONE:
893 handleNullReturnEvent(msg, "setPreferredNetworkType");
894 break;
895
Shuo Qian850e4d6a2018-04-25 21:02:08 +0000896 case CMD_INVOKE_OEM_RIL_REQUEST_RAW:
897 request = (MainThreadRequest)msg.obj;
898 onCompleted = obtainMessage(EVENT_INVOKE_OEM_RIL_REQUEST_RAW_DONE, request);
Nazanin Bakhshif782e562018-12-11 15:15:39 -0800899 defaultPhone.invokeOemRilRequestRaw((byte[]) request.argument, onCompleted);
Shuo Qian850e4d6a2018-04-25 21:02:08 +0000900 break;
901
902 case EVENT_INVOKE_OEM_RIL_REQUEST_RAW_DONE:
903 ar = (AsyncResult)msg.obj;
904 request = (MainThreadRequest)ar.userObj;
905 request.result = ar;
Pengquan Menga1bb6272018-09-06 09:59:22 -0700906 notifyRequester(request);
Shuo Qian850e4d6a2018-04-25 21:02:08 +0000907 break;
908
Shishir Agrawal76d5da92014-11-09 16:17:25 -0800909 case CMD_SET_VOICEMAIL_NUMBER:
910 request = (MainThreadRequest) msg.obj;
911 onCompleted = obtainMessage(EVENT_SET_VOICEMAIL_NUMBER_DONE, request);
912 Pair<String, String> tagNum = (Pair<String, String>) request.argument;
Stuart Scott584921c2015-01-15 17:10:34 -0800913 getPhoneFromRequest(request).setVoiceMailNumber(tagNum.first, tagNum.second,
914 onCompleted);
Shishir Agrawal76d5da92014-11-09 16:17:25 -0800915 break;
916
917 case EVENT_SET_VOICEMAIL_NUMBER_DONE:
918 handleNullReturnEvent(msg, "setVoicemailNumber");
919 break;
920
Stuart Scott54788802015-03-30 13:18:01 -0700921 case CMD_SET_NETWORK_SELECTION_MODE_AUTOMATIC:
922 request = (MainThreadRequest) msg.obj;
923 onCompleted = obtainMessage(EVENT_SET_NETWORK_SELECTION_MODE_AUTOMATIC_DONE,
924 request);
925 getPhoneFromRequest(request).setNetworkSelectionModeAutomatic(onCompleted);
926 break;
927
928 case EVENT_SET_NETWORK_SELECTION_MODE_AUTOMATIC_DONE:
929 handleNullReturnEvent(msg, "setNetworkSelectionModeAutomatic");
930 break;
931
Shishir Agrawal302c8692015-06-19 13:49:39 -0700932 case CMD_PERFORM_NETWORK_SCAN:
933 request = (MainThreadRequest) msg.obj;
934 onCompleted = obtainMessage(EVENT_PERFORM_NETWORK_SCAN_DONE, request);
935 getPhoneFromRequest(request).getAvailableNetworks(onCompleted);
936 break;
937
Hall Liua1acea22020-09-18 19:04:59 -0700938 case CMD_GET_CALL_FORWARDING: {
sqian80370722020-01-29 15:02:51 -0800939 request = (MainThreadRequest) msg.obj;
940 onCompleted = obtainMessage(EVENT_GET_CALL_FORWARDING_DONE, request);
Hall Liua1acea22020-09-18 19:04:59 -0700941 Pair<Integer, TelephonyManager.CallForwardingInfoCallback> args =
942 (Pair<Integer, TelephonyManager.CallForwardingInfoCallback>)
943 request.argument;
944 int callForwardingReason = args.first;
945 request.phone.getCallForwardingOption(callForwardingReason, onCompleted);
sqian80370722020-01-29 15:02:51 -0800946 break;
Hall Liua1acea22020-09-18 19:04:59 -0700947 }
948 case EVENT_GET_CALL_FORWARDING_DONE: {
sqian80370722020-01-29 15:02:51 -0800949 ar = (AsyncResult) msg.obj;
950 request = (MainThreadRequest) ar.userObj;
Hall Liua1acea22020-09-18 19:04:59 -0700951 TelephonyManager.CallForwardingInfoCallback callback =
952 ((Pair<Integer, TelephonyManager.CallForwardingInfoCallback>)
953 request.argument).second;
sqian80370722020-01-29 15:02:51 -0800954 if (ar.exception == null && ar.result != null) {
Hall Liua1acea22020-09-18 19:04:59 -0700955 CallForwardingInfo callForwardingInfo = null;
sqian80370722020-01-29 15:02:51 -0800956 CallForwardInfo[] callForwardInfos = (CallForwardInfo[]) ar.result;
957 for (CallForwardInfo callForwardInfo : callForwardInfos) {
958 // Service Class is a bit mask per 3gpp 27.007. Search for
959 // any service for voice call.
960 if ((callForwardInfo.serviceClass
961 & CommandsInterface.SERVICE_CLASS_VOICE) > 0) {
Hall Liua1acea22020-09-18 19:04:59 -0700962 callForwardingInfo = new CallForwardingInfo(true,
963 callForwardInfo.reason,
964 callForwardInfo.number,
965 callForwardInfo.timeSeconds);
sqian80370722020-01-29 15:02:51 -0800966 break;
967 }
968 }
969 // Didn't find a call forward info for voice call.
970 if (callForwardingInfo == null) {
Hall Liua1acea22020-09-18 19:04:59 -0700971 callForwardingInfo = new CallForwardingInfo(false /* enabled */,
972 0 /* reason */, null /* number */, 0 /* timeout */);
sqian80370722020-01-29 15:02:51 -0800973 }
Hall Liua1acea22020-09-18 19:04:59 -0700974 callback.onCallForwardingInfoAvailable(callForwardingInfo);
sqian80370722020-01-29 15:02:51 -0800975 } else {
976 if (ar.result == null) {
977 loge("EVENT_GET_CALL_FORWARDING_DONE: Empty response");
978 }
979 if (ar.exception != null) {
980 loge("EVENT_GET_CALL_FORWARDING_DONE: Exception: " + ar.exception);
981 }
Hall Liuae527aa2020-09-29 17:10:18 -0700982 int errorCode = TelephonyManager
983 .CallForwardingInfoCallback.RESULT_ERROR_UNKNOWN;
sqian80370722020-01-29 15:02:51 -0800984 if (ar.exception instanceof CommandException) {
985 CommandException.Error error =
986 ((CommandException) (ar.exception)).getCommandError();
987 if (error == CommandException.Error.FDN_CHECK_FAILURE) {
Hall Liuae527aa2020-09-29 17:10:18 -0700988 errorCode = TelephonyManager
989 .CallForwardingInfoCallback.RESULT_ERROR_FDN_CHECK_FAILURE;
sqian80370722020-01-29 15:02:51 -0800990 } else if (error == CommandException.Error.REQUEST_NOT_SUPPORTED) {
Hall Liuae527aa2020-09-29 17:10:18 -0700991 errorCode = TelephonyManager
992 .CallForwardingInfoCallback.RESULT_ERROR_NOT_SUPPORTED;
sqian80370722020-01-29 15:02:51 -0800993 }
994 }
Hall Liua1acea22020-09-18 19:04:59 -0700995 callback.onError(errorCode);
sqian80370722020-01-29 15:02:51 -0800996 }
sqian80370722020-01-29 15:02:51 -0800997 break;
Hall Liua1acea22020-09-18 19:04:59 -0700998 }
sqian80370722020-01-29 15:02:51 -0800999
Hall Liua1acea22020-09-18 19:04:59 -07001000 case CMD_SET_CALL_FORWARDING: {
sqian80370722020-01-29 15:02:51 -08001001 request = (MainThreadRequest) msg.obj;
1002 onCompleted = obtainMessage(EVENT_SET_CALL_FORWARDING_DONE, request);
Hall Liua1acea22020-09-18 19:04:59 -07001003 request = (MainThreadRequest) msg.obj;
sqian80370722020-01-29 15:02:51 -08001004 CallForwardingInfo callForwardingInfoToSet =
Hall Liua1acea22020-09-18 19:04:59 -07001005 ((Pair<CallForwardingInfo, Consumer<Integer>>)
1006 request.argument).first;
1007 request.phone.setCallForwardingOption(
1008 callForwardingInfoToSet.isEnabled()
1009 ? CommandsInterface.CF_ACTION_ENABLE
1010 : CommandsInterface.CF_ACTION_DISABLE,
sqian80370722020-01-29 15:02:51 -08001011 callForwardingInfoToSet.getReason(),
1012 callForwardingInfoToSet.getNumber(),
1013 callForwardingInfoToSet.getTimeoutSeconds(), onCompleted);
1014 break;
Hall Liua1acea22020-09-18 19:04:59 -07001015 }
sqian80370722020-01-29 15:02:51 -08001016
Hall Liua1acea22020-09-18 19:04:59 -07001017 case EVENT_SET_CALL_FORWARDING_DONE: {
sqian80370722020-01-29 15:02:51 -08001018 ar = (AsyncResult) msg.obj;
1019 request = (MainThreadRequest) ar.userObj;
Hall Liua1acea22020-09-18 19:04:59 -07001020 Consumer<Integer> callback =
1021 ((Pair<CallForwardingInfo, Consumer<Integer>>)
1022 request.argument).second;
1023 if (ar.exception != null) {
sqian80370722020-01-29 15:02:51 -08001024 loge("setCallForwarding exception: " + ar.exception);
Hall Liuae527aa2020-09-29 17:10:18 -07001025 int errorCode = TelephonyManager.CallForwardingInfoCallback
1026 .RESULT_ERROR_UNKNOWN;
Hall Liua1acea22020-09-18 19:04:59 -07001027 if (ar.exception instanceof CommandException) {
1028 CommandException.Error error =
1029 ((CommandException) (ar.exception)).getCommandError();
1030 if (error == CommandException.Error.FDN_CHECK_FAILURE) {
Hall Liuae527aa2020-09-29 17:10:18 -07001031 errorCode = TelephonyManager.CallForwardingInfoCallback
1032 .RESULT_ERROR_FDN_CHECK_FAILURE;
Hall Liua1acea22020-09-18 19:04:59 -07001033 } else if (error == CommandException.Error.REQUEST_NOT_SUPPORTED) {
Hall Liuae527aa2020-09-29 17:10:18 -07001034 errorCode = TelephonyManager.CallForwardingInfoCallback
1035 .RESULT_ERROR_NOT_SUPPORTED;
Hall Liua1acea22020-09-18 19:04:59 -07001036 }
1037 }
1038 callback.accept(errorCode);
1039 } else {
Hall Liuae527aa2020-09-29 17:10:18 -07001040 callback.accept(TelephonyManager.CallForwardingInfoCallback.RESULT_SUCCESS);
sqian80370722020-01-29 15:02:51 -08001041 }
sqian80370722020-01-29 15:02:51 -08001042 break;
Hall Liua1acea22020-09-18 19:04:59 -07001043 }
sqian80370722020-01-29 15:02:51 -08001044
Hall Liua1acea22020-09-18 19:04:59 -07001045 case CMD_GET_CALL_WAITING: {
sqian80370722020-01-29 15:02:51 -08001046 request = (MainThreadRequest) msg.obj;
1047 onCompleted = obtainMessage(EVENT_GET_CALL_WAITING_DONE, request);
1048 getPhoneFromRequest(request).getCallWaiting(onCompleted);
1049 break;
Hall Liua1acea22020-09-18 19:04:59 -07001050 }
sqian80370722020-01-29 15:02:51 -08001051
Hall Liua1acea22020-09-18 19:04:59 -07001052 case EVENT_GET_CALL_WAITING_DONE: {
sqian80370722020-01-29 15:02:51 -08001053 ar = (AsyncResult) msg.obj;
1054 request = (MainThreadRequest) ar.userObj;
Hall Liua1acea22020-09-18 19:04:59 -07001055 Consumer<Integer> callback = (Consumer<Integer>) request.argument;
sqian80370722020-01-29 15:02:51 -08001056 int callForwardingStatus = TelephonyManager.CALL_WAITING_STATUS_UNKNOWN_ERROR;
1057 if (ar.exception == null && ar.result != null) {
Shuo Qiand6a0dba2020-02-18 18:13:49 -08001058 int[] callForwardResults = (int[]) ar.result;
sqian80370722020-01-29 15:02:51 -08001059 // Service Class is a bit mask per 3gpp 27.007.
1060 // Search for any service for voice call.
Shuo Qiand6a0dba2020-02-18 18:13:49 -08001061 if (callForwardResults.length > 1
1062 && ((callForwardResults[1]
Hall Liua1acea22020-09-18 19:04:59 -07001063 & CommandsInterface.SERVICE_CLASS_VOICE) > 0)) {
Shuo Qiand6a0dba2020-02-18 18:13:49 -08001064 callForwardingStatus = callForwardResults[0] == 0
Hall Liua1acea22020-09-18 19:04:59 -07001065 ? TelephonyManager.CALL_WAITING_STATUS_DISABLED
1066 : TelephonyManager.CALL_WAITING_STATUS_ENABLED;
sqian80370722020-01-29 15:02:51 -08001067 } else {
Hall Liua1acea22020-09-18 19:04:59 -07001068 callForwardingStatus = TelephonyManager.CALL_WAITING_STATUS_DISABLED;
sqian80370722020-01-29 15:02:51 -08001069 }
1070 } else {
1071 if (ar.result == null) {
1072 loge("EVENT_GET_CALL_WAITING_DONE: Empty response");
1073 }
1074 if (ar.exception != null) {
1075 loge("EVENT_GET_CALL_WAITING_DONE: Exception: " + ar.exception);
1076 }
1077 if (ar.exception instanceof CommandException) {
1078 CommandException.Error error =
1079 ((CommandException) (ar.exception)).getCommandError();
1080 if (error == CommandException.Error.REQUEST_NOT_SUPPORTED) {
1081 callForwardingStatus =
1082 TelephonyManager.CALL_WAITING_STATUS_NOT_SUPPORTED;
1083 }
1084 }
1085 }
Hall Liua1acea22020-09-18 19:04:59 -07001086 callback.accept(callForwardingStatus);
sqian80370722020-01-29 15:02:51 -08001087 break;
Hall Liua1acea22020-09-18 19:04:59 -07001088 }
sqian80370722020-01-29 15:02:51 -08001089
Hall Liua1acea22020-09-18 19:04:59 -07001090 case CMD_SET_CALL_WAITING: {
sqian80370722020-01-29 15:02:51 -08001091 request = (MainThreadRequest) msg.obj;
1092 onCompleted = obtainMessage(EVENT_SET_CALL_WAITING_DONE, request);
Hall Liua1acea22020-09-18 19:04:59 -07001093 boolean enable = ((Pair<Boolean, Consumer<Integer>>) request.argument).first;
1094 getPhoneFromRequest(request).setCallWaiting(enable, onCompleted);
sqian80370722020-01-29 15:02:51 -08001095 break;
Hall Liua1acea22020-09-18 19:04:59 -07001096 }
sqian80370722020-01-29 15:02:51 -08001097
Hall Liua1acea22020-09-18 19:04:59 -07001098 case EVENT_SET_CALL_WAITING_DONE: {
sqian80370722020-01-29 15:02:51 -08001099 ar = (AsyncResult) msg.obj;
1100 request = (MainThreadRequest) ar.userObj;
Hall Liua1acea22020-09-18 19:04:59 -07001101 boolean enable = ((Pair<Boolean, Consumer<Integer>>) request.argument).first;
1102 Consumer<Integer> callback =
1103 ((Pair<Boolean, Consumer<Integer>>) request.argument).second;
1104 if (ar.exception != null) {
sqian80370722020-01-29 15:02:51 -08001105 loge("setCallWaiting exception: " + ar.exception);
Hall Liua1acea22020-09-18 19:04:59 -07001106 if (ar.exception instanceof CommandException) {
1107 CommandException.Error error =
1108 ((CommandException) (ar.exception)).getCommandError();
1109 if (error == CommandException.Error.REQUEST_NOT_SUPPORTED) {
1110 callback.accept(TelephonyManager.CALL_WAITING_STATUS_NOT_SUPPORTED);
1111 } else {
1112 callback.accept(TelephonyManager.CALL_WAITING_STATUS_UNKNOWN_ERROR);
1113 }
1114 } else {
1115 callback.accept(TelephonyManager.CALL_WAITING_STATUS_UNKNOWN_ERROR);
1116 }
1117 } else {
1118 callback.accept(enable ? TelephonyManager.CALL_WAITING_STATUS_ENABLED
1119 : TelephonyManager.CALL_WAITING_STATUS_DISABLED);
sqian80370722020-01-29 15:02:51 -08001120 }
sqian80370722020-01-29 15:02:51 -08001121 break;
Hall Liua1acea22020-09-18 19:04:59 -07001122 }
sqian80370722020-01-29 15:02:51 -08001123
Shishir Agrawal302c8692015-06-19 13:49:39 -07001124 case EVENT_PERFORM_NETWORK_SCAN_DONE:
1125 ar = (AsyncResult) msg.obj;
1126 request = (MainThreadRequest) ar.userObj;
1127 CellNetworkScanResult cellScanResult;
1128 if (ar.exception == null && ar.result != null) {
1129 cellScanResult = new CellNetworkScanResult(
1130 CellNetworkScanResult.STATUS_SUCCESS,
1131 (List<OperatorInfo>) ar.result);
1132 } else {
1133 if (ar.result == null) {
1134 loge("getCellNetworkScanResults: Empty response");
1135 }
1136 if (ar.exception != null) {
1137 loge("getCellNetworkScanResults: Exception: " + ar.exception);
1138 }
1139 int errorCode = CellNetworkScanResult.STATUS_UNKNOWN_ERROR;
1140 if (ar.exception instanceof CommandException) {
1141 CommandException.Error error =
1142 ((CommandException) (ar.exception)).getCommandError();
1143 if (error == CommandException.Error.RADIO_NOT_AVAILABLE) {
1144 errorCode = CellNetworkScanResult.STATUS_RADIO_NOT_AVAILABLE;
1145 } else if (error == CommandException.Error.GENERIC_FAILURE) {
1146 errorCode = CellNetworkScanResult.STATUS_RADIO_GENERIC_FAILURE;
1147 }
1148 }
1149 cellScanResult = new CellNetworkScanResult(errorCode, null);
1150 }
1151 request.result = cellScanResult;
Pengquan Menga1bb6272018-09-06 09:59:22 -07001152 notifyRequester(request);
Shishir Agrawal302c8692015-06-19 13:49:39 -07001153 break;
1154
1155 case CMD_SET_NETWORK_SELECTION_MODE_MANUAL:
1156 request = (MainThreadRequest) msg.obj;
Shishir Agrawal77ba3172015-09-10 14:50:19 -07001157 ManualNetworkSelectionArgument selArg =
1158 (ManualNetworkSelectionArgument) request.argument;
Shishir Agrawal302c8692015-06-19 13:49:39 -07001159 onCompleted = obtainMessage(EVENT_SET_NETWORK_SELECTION_MODE_MANUAL_DONE,
1160 request);
Shishir Agrawal77ba3172015-09-10 14:50:19 -07001161 getPhoneFromRequest(request).selectNetworkManually(selArg.operatorInfo,
1162 selArg.persistSelection, onCompleted);
Shishir Agrawal302c8692015-06-19 13:49:39 -07001163 break;
1164
1165 case EVENT_SET_NETWORK_SELECTION_MODE_MANUAL_DONE:
Pengquan Menge3d01e22018-09-20 15:25:35 -07001166 ar = (AsyncResult) msg.obj;
1167 request = (MainThreadRequest) ar.userObj;
1168 if (ar.exception == null) {
1169 request.result = true;
1170 } else {
1171 request.result = false;
1172 loge("setNetworkSelectionModeManual " + ar.exception);
1173 }
1174 notifyRequester(request);
1175 mApp.onNetworkSelectionChanged(request.subId);
Shishir Agrawal302c8692015-06-19 13:49:39 -07001176 break;
1177
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -07001178 case CMD_GET_MODEM_ACTIVITY_INFO:
1179 request = (MainThreadRequest) msg.obj;
1180 onCompleted = obtainMessage(EVENT_GET_MODEM_ACTIVITY_INFO_DONE, request);
James Mattisab947702019-04-03 14:18:34 -07001181 if (defaultPhone != null) {
1182 defaultPhone.getModemActivityInfo(onCompleted, request.workSource);
sqian1a1be542020-03-05 11:37:28 -08001183 } else {
1184 ResultReceiver result = (ResultReceiver) request.argument;
1185 Bundle bundle = new Bundle();
1186 bundle.putParcelable(TelephonyManager.MODEM_ACTIVITY_RESULT_KEY,
1187 new ModemActivityInfo(0, 0, 0, new int[0], 0));
1188 result.send(0, bundle);
James Mattisab947702019-04-03 14:18:34 -07001189 }
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -07001190 break;
1191
1192 case EVENT_GET_MODEM_ACTIVITY_INFO_DONE:
1193 ar = (AsyncResult) msg.obj;
1194 request = (MainThreadRequest) ar.userObj;
sqian1a1be542020-03-05 11:37:28 -08001195 ResultReceiver result = (ResultReceiver) request.argument;
1196
1197 ModemActivityInfo ret = new ModemActivityInfo(0, 0, 0, new int[0], 0);
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -07001198 if (ar.exception == null && ar.result != null) {
sqian1a1be542020-03-05 11:37:28 -08001199 // Update the last modem activity info and the result of the request.
1200 ModemActivityInfo info = (ModemActivityInfo) ar.result;
1201 if (isModemActivityInfoValid(info)) {
1202 int[] mergedTxTimeMs = new int[ModemActivityInfo.TX_POWER_LEVELS];
1203 int[] txTimeMs = info.getTransmitTimeMillis();
1204 int[] lastModemTxTimeMs = mLastModemActivityInfo
1205 .getTransmitTimeMillis();
1206 for (int i = 0; i < mergedTxTimeMs.length; i++) {
1207 mergedTxTimeMs[i] = txTimeMs[i] + lastModemTxTimeMs[i];
1208 }
1209 mLastModemActivityInfo.setTimestamp(info.getTimestamp());
1210 mLastModemActivityInfo.setSleepTimeMillis(info.getSleepTimeMillis()
1211 + mLastModemActivityInfo.getSleepTimeMillis());
1212 mLastModemActivityInfo.setIdleTimeMillis(info.getIdleTimeMillis()
1213 + mLastModemActivityInfo.getIdleTimeMillis());
1214 mLastModemActivityInfo.setTransmitTimeMillis(mergedTxTimeMs);
1215 mLastModemActivityInfo.setReceiveTimeMillis(
1216 info.getReceiveTimeMillis()
1217 + mLastModemActivityInfo.getReceiveTimeMillis());
1218 }
1219 ret = new ModemActivityInfo(mLastModemActivityInfo.getTimestamp(),
1220 mLastModemActivityInfo.getSleepTimeMillis(),
1221 mLastModemActivityInfo.getIdleTimeMillis(),
1222 mLastModemActivityInfo.getTransmitTimeMillis(),
1223 mLastModemActivityInfo.getReceiveTimeMillis());
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -07001224 } else {
1225 if (ar.result == null) {
1226 loge("queryModemActivityInfo: Empty response");
1227 } else if (ar.exception instanceof CommandException) {
1228 loge("queryModemActivityInfo: CommandException: " +
1229 ar.exception);
1230 } else {
1231 loge("queryModemActivityInfo: Unknown exception");
1232 }
1233 }
sqian1a1be542020-03-05 11:37:28 -08001234 Bundle bundle = new Bundle();
1235 bundle.putParcelable(TelephonyManager.MODEM_ACTIVITY_RESULT_KEY, ret);
1236 result.send(0, bundle);
Pengquan Menga1bb6272018-09-06 09:59:22 -07001237 notifyRequester(request);
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -07001238 break;
1239
Meng Wang1a7c35a2016-05-05 20:56:15 -07001240 case CMD_SET_ALLOWED_CARRIERS:
1241 request = (MainThreadRequest) msg.obj;
Michele Berionne482f8202018-11-27 18:57:59 -08001242 CarrierRestrictionRules argument =
1243 (CarrierRestrictionRules) request.argument;
Meng Wang1a7c35a2016-05-05 20:56:15 -07001244 onCompleted = obtainMessage(EVENT_SET_ALLOWED_CARRIERS_DONE, request);
Michele Berionne482f8202018-11-27 18:57:59 -08001245 defaultPhone.setAllowedCarriers(argument, onCompleted, request.workSource);
Meng Wang1a7c35a2016-05-05 20:56:15 -07001246 break;
1247
1248 case EVENT_SET_ALLOWED_CARRIERS_DONE:
1249 ar = (AsyncResult) msg.obj;
1250 request = (MainThreadRequest) ar.userObj;
1251 if (ar.exception == null && ar.result != null) {
1252 request.result = ar.result;
1253 } else {
Michele Berionne482f8202018-11-27 18:57:59 -08001254 request.result = TelephonyManager.SET_CARRIER_RESTRICTION_ERROR;
1255 if (ar.exception instanceof CommandException) {
1256 loge("setAllowedCarriers: CommandException: " + ar.exception);
1257 CommandException.Error error =
1258 ((CommandException) (ar.exception)).getCommandError();
1259 if (error == CommandException.Error.REQUEST_NOT_SUPPORTED) {
1260 request.result =
1261 TelephonyManager.SET_CARRIER_RESTRICTION_NOT_SUPPORTED;
1262 }
Meng Wang1a7c35a2016-05-05 20:56:15 -07001263 } else {
1264 loge("setAllowedCarriers: Unknown exception");
1265 }
1266 }
Pengquan Menga1bb6272018-09-06 09:59:22 -07001267 notifyRequester(request);
Meng Wang1a7c35a2016-05-05 20:56:15 -07001268 break;
1269
1270 case CMD_GET_ALLOWED_CARRIERS:
1271 request = (MainThreadRequest) msg.obj;
1272 onCompleted = obtainMessage(EVENT_GET_ALLOWED_CARRIERS_DONE, request);
Nazanin Bakhshif782e562018-12-11 15:15:39 -08001273 defaultPhone.getAllowedCarriers(onCompleted, request.workSource);
Meng Wang1a7c35a2016-05-05 20:56:15 -07001274 break;
1275
1276 case EVENT_GET_ALLOWED_CARRIERS_DONE:
1277 ar = (AsyncResult) msg.obj;
1278 request = (MainThreadRequest) ar.userObj;
1279 if (ar.exception == null && ar.result != null) {
1280 request.result = ar.result;
1281 } else {
Michele Berionne482f8202018-11-27 18:57:59 -08001282 request.result = new IllegalStateException(
1283 "Failed to get carrier restrictions");
Meng Wang1a7c35a2016-05-05 20:56:15 -07001284 if (ar.result == null) {
1285 loge("getAllowedCarriers: Empty response");
1286 } else if (ar.exception instanceof CommandException) {
1287 loge("getAllowedCarriers: CommandException: " +
1288 ar.exception);
1289 } else {
1290 loge("getAllowedCarriers: Unknown exception");
1291 }
1292 }
Pengquan Menga1bb6272018-09-06 09:59:22 -07001293 notifyRequester(request);
Meng Wang1a7c35a2016-05-05 20:56:15 -07001294 break;
1295
Nathan Haroldb3014052017-01-25 15:57:32 -08001296 case EVENT_GET_FORBIDDEN_PLMNS_DONE:
1297 ar = (AsyncResult) msg.obj;
1298 request = (MainThreadRequest) ar.userObj;
1299 if (ar.exception == null && ar.result != null) {
1300 request.result = ar.result;
1301 } else {
1302 request.result = new IllegalArgumentException(
1303 "Failed to retrieve Forbidden Plmns");
1304 if (ar.result == null) {
1305 loge("getForbiddenPlmns: Empty response");
1306 } else {
1307 loge("getForbiddenPlmns: Unknown exception");
1308 }
1309 }
Pengquan Menga1bb6272018-09-06 09:59:22 -07001310 notifyRequester(request);
Nathan Haroldb3014052017-01-25 15:57:32 -08001311 break;
1312
1313 case CMD_GET_FORBIDDEN_PLMNS:
1314 request = (MainThreadRequest) msg.obj;
1315 uiccCard = getUiccCardFromRequest(request);
1316 if (uiccCard == null) {
1317 loge("getForbiddenPlmns() UiccCard is null");
1318 request.result = new IllegalArgumentException(
1319 "getForbiddenPlmns() UiccCard is null");
Pengquan Menga1bb6272018-09-06 09:59:22 -07001320 notifyRequester(request);
Nathan Haroldb3014052017-01-25 15:57:32 -08001321 break;
1322 }
1323 Integer appType = (Integer) request.argument;
1324 UiccCardApplication uiccApp = uiccCard.getApplicationByType(appType);
1325 if (uiccApp == null) {
1326 loge("getForbiddenPlmns() no app with specified type -- "
1327 + appType);
1328 request.result = new IllegalArgumentException("Failed to get UICC App");
Pengquan Menga1bb6272018-09-06 09:59:22 -07001329 notifyRequester(request);
Nathan Haroldb3014052017-01-25 15:57:32 -08001330 break;
1331 } else {
1332 if (DBG) logv("getForbiddenPlmns() found app " + uiccApp.getAid()
1333 + " specified type -- " + appType);
1334 }
1335 onCompleted = obtainMessage(EVENT_GET_FORBIDDEN_PLMNS_DONE, request);
1336 ((SIMRecords) uiccApp.getIccRecords()).getForbiddenPlmns(
1337 onCompleted);
1338 break;
1339
Holly Jiuyu Sun01c47ad2018-01-24 17:56:33 +00001340 case CMD_SWITCH_SLOTS:
1341 request = (MainThreadRequest) msg.obj;
1342 int[] physicalSlots = (int[]) request.argument;
1343 onCompleted = obtainMessage(EVENT_SWITCH_SLOTS_DONE, request);
1344 UiccController.getInstance().switchSlots(physicalSlots, onCompleted);
1345 break;
1346
1347 case EVENT_SWITCH_SLOTS_DONE:
1348 ar = (AsyncResult) msg.obj;
1349 request = (MainThreadRequest) ar.userObj;
1350 request.result = (ar.exception == null);
Pengquan Menga1bb6272018-09-06 09:59:22 -07001351 notifyRequester(request);
1352 break;
1353 case CMD_GET_NETWORK_SELECTION_MODE:
1354 request = (MainThreadRequest) msg.obj;
1355 onCompleted = obtainMessage(EVENT_GET_NETWORK_SELECTION_MODE_DONE, request);
1356 getPhoneFromRequest(request).getNetworkSelectionMode(onCompleted);
1357 break;
1358
1359 case EVENT_GET_NETWORK_SELECTION_MODE_DONE:
1360 ar = (AsyncResult) msg.obj;
1361 request = (MainThreadRequest) ar.userObj;
1362 if (ar.exception != null) {
1363 request.result = TelephonyManager.NETWORK_SELECTION_MODE_UNKNOWN;
1364 } else {
1365 int mode = ((int[]) ar.result)[0];
1366 if (mode == 0) {
1367 request.result = TelephonyManager.NETWORK_SELECTION_MODE_AUTO;
1368 } else {
1369 request.result = TelephonyManager.NETWORK_SELECTION_MODE_MANUAL;
1370 }
Holly Jiuyu Sun01c47ad2018-01-24 17:56:33 +00001371 }
Pengquan Menga1bb6272018-09-06 09:59:22 -07001372 notifyRequester(request);
1373 break;
1374 case CMD_GET_CDMA_ROAMING_MODE:
1375 request = (MainThreadRequest) msg.obj;
1376 onCompleted = obtainMessage(EVENT_GET_CDMA_ROAMING_MODE_DONE, request);
1377 getPhoneFromRequest(request).queryCdmaRoamingPreference(onCompleted);
1378 break;
1379 case EVENT_GET_CDMA_ROAMING_MODE_DONE:
1380 ar = (AsyncResult) msg.obj;
1381 request = (MainThreadRequest) ar.userObj;
1382 if (ar.exception != null) {
1383 request.result = TelephonyManager.CDMA_ROAMING_MODE_RADIO_DEFAULT;
1384 } else {
1385 request.result = ((int[]) ar.result)[0];
1386 }
1387 notifyRequester(request);
1388 break;
1389 case CMD_SET_CDMA_ROAMING_MODE:
1390 request = (MainThreadRequest) msg.obj;
1391 onCompleted = obtainMessage(EVENT_SET_CDMA_ROAMING_MODE_DONE, request);
1392 int mode = (int) request.argument;
1393 getPhoneFromRequest(request).setCdmaRoamingPreference(mode, onCompleted);
1394 break;
1395 case EVENT_SET_CDMA_ROAMING_MODE_DONE:
1396 ar = (AsyncResult) msg.obj;
1397 request = (MainThreadRequest) ar.userObj;
1398 request.result = ar.exception == null;
1399 notifyRequester(request);
1400 break;
Sarah Chin49f22af2020-10-28 13:46:24 -07001401 case CMD_GET_CDMA_SUBSCRIPTION_MODE:
1402 request = (MainThreadRequest) msg.obj;
1403 onCompleted = obtainMessage(EVENT_GET_CDMA_SUBSCRIPTION_MODE_DONE, request);
1404 getPhoneFromRequest(request).queryCdmaSubscriptionMode(onCompleted);
1405 break;
1406 case EVENT_GET_CDMA_SUBSCRIPTION_MODE_DONE:
1407 ar = (AsyncResult) msg.obj;
1408 request = (MainThreadRequest) ar.userObj;
1409 if (ar.exception != null) {
1410 request.result = TelephonyManager.CDMA_SUBSCRIPTION_RUIM_SIM;
1411 } else {
1412 request.result = ((int[]) ar.result)[0];
1413 }
1414 notifyRequester(request);
1415 break;
Pengquan Menga1bb6272018-09-06 09:59:22 -07001416 case CMD_SET_CDMA_SUBSCRIPTION_MODE:
1417 request = (MainThreadRequest) msg.obj;
1418 onCompleted = obtainMessage(EVENT_SET_CDMA_SUBSCRIPTION_MODE_DONE, request);
1419 int subscriptionMode = (int) request.argument;
Sarah Chin49f22af2020-10-28 13:46:24 -07001420 getPhoneFromRequest(request).setCdmaSubscriptionMode(
1421 subscriptionMode, onCompleted);
Pengquan Menga1bb6272018-09-06 09:59:22 -07001422 break;
1423 case EVENT_SET_CDMA_SUBSCRIPTION_MODE_DONE:
1424 ar = (AsyncResult) msg.obj;
1425 request = (MainThreadRequest) ar.userObj;
1426 request.result = ar.exception == null;
1427 notifyRequester(request);
Holly Jiuyu Sun01c47ad2018-01-24 17:56:33 +00001428 break;
Nathan Harold3ff88932018-08-14 10:19:49 -07001429 case CMD_GET_ALL_CELL_INFO:
1430 request = (MainThreadRequest) msg.obj;
Nathan Harold3ff88932018-08-14 10:19:49 -07001431 onCompleted = obtainMessage(EVENT_GET_ALL_CELL_INFO_DONE, request);
Nathan Harold92bed182018-10-12 18:16:49 -07001432 request.phone.requestCellInfoUpdate(request.workSource, onCompleted);
Nathan Harold3ff88932018-08-14 10:19:49 -07001433 break;
Nathan Harold3ff88932018-08-14 10:19:49 -07001434 case EVENT_GET_ALL_CELL_INFO_DONE:
1435 ar = (AsyncResult) msg.obj;
1436 request = (MainThreadRequest) ar.userObj;
Nathan Harold8d0f1742018-10-02 12:14:47 -07001437 // If a timeout occurs, the response will be null
1438 request.result = (ar.exception == null && ar.result != null)
1439 ? ar.result : new ArrayList<CellInfo>();
Nathan Harold3ff88932018-08-14 10:19:49 -07001440 synchronized (request) {
1441 request.notifyAll();
1442 }
1443 break;
Nathan Haroldfa8da0f2018-09-27 18:51:29 -07001444 case CMD_REQUEST_CELL_INFO_UPDATE:
1445 request = (MainThreadRequest) msg.obj;
1446 request.phone.requestCellInfoUpdate(request.workSource,
1447 obtainMessage(EVENT_REQUEST_CELL_INFO_UPDATE_DONE, request));
1448 break;
1449 case EVENT_REQUEST_CELL_INFO_UPDATE_DONE:
1450 ar = (AsyncResult) msg.obj;
1451 request = (MainThreadRequest) ar.userObj;
1452 ICellInfoCallback cb = (ICellInfoCallback) request.argument;
1453 try {
1454 if (ar.exception != null) {
Nathan Haroldfa8da0f2018-09-27 18:51:29 -07001455 Log.e(LOG_TAG, "Exception retrieving CellInfo=" + ar.exception);
Meng Wang6c08ecd2019-09-30 17:13:54 -07001456 cb.onError(
1457 TelephonyManager.CellInfoCallback.ERROR_MODEM_ERROR,
1458 ar.exception.getClass().getName(),
1459 ar.exception.toString());
Nathan Haroldfa8da0f2018-09-27 18:51:29 -07001460 } else if (ar.result == null) {
Nathan Haroldfa8da0f2018-09-27 18:51:29 -07001461 Log.w(LOG_TAG, "Timeout Waiting for CellInfo!");
Meng Wang6c08ecd2019-09-30 17:13:54 -07001462 cb.onError(TelephonyManager.CellInfoCallback.ERROR_TIMEOUT, null, null);
Nathan Haroldfa8da0f2018-09-27 18:51:29 -07001463 } else {
1464 // use the result as returned
1465 cb.onCellInfo((List<CellInfo>) ar.result);
1466 }
1467 } catch (RemoteException re) {
1468 Log.w(LOG_TAG, "Discarded CellInfo due to Callback RemoteException");
1469 }
1470 break;
Sarah Chincc055732020-11-18 13:39:35 -08001471 case CMD_GET_CELL_LOCATION: {
Nathan Harold3ff88932018-08-14 10:19:49 -07001472 request = (MainThreadRequest) msg.obj;
1473 WorkSource ws = (WorkSource) request.argument;
1474 Phone phone = getPhoneFromRequest(request);
Meng Wangd64acad2019-12-09 13:13:01 -08001475 phone.getCellIdentity(ws, obtainMessage(EVENT_GET_CELL_LOCATION_DONE, request));
Nathan Harold3ff88932018-08-14 10:19:49 -07001476 break;
Sarah Chincc055732020-11-18 13:39:35 -08001477 }
1478 case EVENT_GET_CELL_LOCATION_DONE: {
Nathan Harold3ff88932018-08-14 10:19:49 -07001479 ar = (AsyncResult) msg.obj;
1480 request = (MainThreadRequest) ar.userObj;
1481 if (ar.exception == null) {
1482 request.result = ar.result;
1483 } else {
Sarah Chincc055732020-11-18 13:39:35 -08001484 Phone phone = getPhoneFromRequest(request);
Nathan Harold3ff88932018-08-14 10:19:49 -07001485 request.result = (phone.getPhoneType() == PhoneConstants.PHONE_TYPE_CDMA)
Meng Wangd64acad2019-12-09 13:13:01 -08001486 ? new CellIdentityCdma() : new CellIdentityGsm();
Nathan Harold3ff88932018-08-14 10:19:49 -07001487 }
1488
1489 synchronized (request) {
1490 request.notifyAll();
1491 }
1492 break;
Sarah Chincc055732020-11-18 13:39:35 -08001493 }
chen xu6dac5ab2018-10-26 17:39:23 -07001494 case CMD_MODEM_REBOOT:
1495 request = (MainThreadRequest) msg.obj;
1496 onCompleted = obtainMessage(EVENT_RESET_MODEM_CONFIG_DONE, request);
Nazanin Bakhshif782e562018-12-11 15:15:39 -08001497 defaultPhone.rebootModem(onCompleted);
chen xu6dac5ab2018-10-26 17:39:23 -07001498 break;
chen xu6dac5ab2018-10-26 17:39:23 -07001499 case EVENT_CMD_MODEM_REBOOT_DONE:
1500 handleNullReturnEvent(msg, "rebootModem");
1501 break;
Malcolm Chen8e4ed912019-01-15 20:22:16 -08001502 case CMD_REQUEST_ENABLE_MODEM:
1503 request = (MainThreadRequest) msg.obj;
1504 boolean enable = (boolean) request.argument;
1505 onCompleted = obtainMessage(EVENT_ENABLE_MODEM_DONE, request);
Nazanin Bakhshi33d584b2019-02-27 10:44:32 -08001506 onCompleted.arg1 = enable ? 1 : 0;
Malcolm Chen8e4ed912019-01-15 20:22:16 -08001507 PhoneConfigurationManager.getInstance()
1508 .enablePhone(request.phone, enable, onCompleted);
1509 break;
1510 case EVENT_ENABLE_MODEM_DONE:
1511 ar = (AsyncResult) msg.obj;
1512 request = (MainThreadRequest) ar.userObj;
1513 request.result = (ar.exception == null);
Nazanin Bakhshif71371d2019-04-29 17:29:44 -07001514 int phoneId = request.phone.getPhoneId();
Nazanin Bakhshi33d584b2019-02-27 10:44:32 -08001515 //update the cache as modem status has changed
Nazanin Bakhshif71371d2019-04-29 17:29:44 -07001516 if ((boolean) request.result) {
1517 mPhoneConfigurationManager.addToPhoneStatusCache(phoneId, msg.arg1 == 1);
1518 updateModemStateMetrics();
1519 } else {
1520 Log.e(LOG_TAG, msg.what + " failure. Not updating modem status."
1521 + ar.exception);
1522 }
1523 notifyRequester(request);
1524 break;
1525 case CMD_GET_MODEM_STATUS:
1526 request = (MainThreadRequest) msg.obj;
1527 onCompleted = obtainMessage(EVENT_GET_MODEM_STATUS_DONE, request);
1528 PhoneConfigurationManager.getInstance()
1529 .getPhoneStatusFromModem(request.phone, onCompleted);
1530 break;
1531 case EVENT_GET_MODEM_STATUS_DONE:
1532 ar = (AsyncResult) msg.obj;
1533 request = (MainThreadRequest) ar.userObj;
1534 int id = request.phone.getPhoneId();
1535 if (ar.exception == null && ar.result != null) {
1536 request.result = ar.result;
1537 //update the cache as modem status has changed
1538 mPhoneConfigurationManager.addToPhoneStatusCache(id,
1539 (boolean) request.result);
1540 } else {
1541 // Return true if modem status cannot be retrieved. For most cases,
1542 // modem status is on. And for older version modems, GET_MODEM_STATUS
1543 // and disable modem are not supported. Modem is always on.
1544 // TODO: this should be fixed in R to support a third
1545 // status UNKNOWN b/131631629
1546 request.result = true;
1547 Log.e(LOG_TAG, msg.what + " failure. Not updating modem status."
1548 + ar.exception);
1549 }
Malcolm Chen8e4ed912019-01-15 20:22:16 -08001550 notifyRequester(request);
1551 break;
Hall Liud0d1dc92020-01-20 13:42:00 -08001552 case CMD_SET_SYSTEM_SELECTION_CHANNELS: {
1553 request = (MainThreadRequest) msg.obj;
1554 onCompleted = obtainMessage(EVENT_SET_SYSTEM_SELECTION_CHANNELS_DONE, request);
1555 Pair<List<RadioAccessSpecifier>, Consumer<Boolean>> args =
1556 (Pair<List<RadioAccessSpecifier>, Consumer<Boolean>>) request.argument;
1557 request.phone.setSystemSelectionChannels(args.first, onCompleted);
1558 break;
1559 }
1560 case EVENT_SET_SYSTEM_SELECTION_CHANNELS_DONE: {
1561 ar = (AsyncResult) msg.obj;
1562 request = (MainThreadRequest) ar.userObj;
1563 Pair<List<RadioAccessSpecifier>, Consumer<Boolean>> args =
1564 (Pair<List<RadioAccessSpecifier>, Consumer<Boolean>>) request.argument;
1565 args.second.accept(ar.exception == null);
1566 notifyRequester(request);
1567 break;
1568 }
Sarah Chincc055732020-11-18 13:39:35 -08001569 case CMD_GET_SYSTEM_SELECTION_CHANNELS: {
1570 request = (MainThreadRequest) msg.obj;
1571 onCompleted = obtainMessage(EVENT_GET_SYSTEM_SELECTION_CHANNELS_DONE, request);
1572 Phone phone = getPhoneFromRequest(request);
1573 if (phone != null) {
1574 phone.getSystemSelectionChannels(onCompleted);
1575 } else {
1576 loge("getSystemSelectionChannels: No phone object");
1577 request.result = new ArrayList<RadioAccessSpecifier>();
1578 notifyRequester(request);
1579 }
1580 break;
1581 }
1582 case EVENT_GET_SYSTEM_SELECTION_CHANNELS_DONE:
1583 ar = (AsyncResult) msg.obj;
1584 request = (MainThreadRequest) ar.userObj;
1585 if (ar.exception == null && ar.result != null) {
1586 request.result = ar.result;
1587 } else {
1588 request.result = new IllegalArgumentException(
1589 "Failed to retrieve system selection channels");
1590 if (ar.result == null) {
1591 loge("getSystemSelectionChannels: Empty response");
1592 } else {
1593 loge("getSystemSelectionChannels: Unknown exception");
1594 }
1595 }
1596 notifyRequester(request);
1597 break;
yincheng zhaod698b842019-09-06 17:06:54 -07001598 case EVENT_SET_FORBIDDEN_PLMNS_DONE:
1599 ar = (AsyncResult) msg.obj;
1600 request = (MainThreadRequest) ar.userObj;
1601 if (ar.exception == null && ar.result != null) {
1602 request.result = ar.result;
1603 } else {
1604 request.result = -1;
1605 loge("Failed to set Forbidden Plmns");
1606 if (ar.result == null) {
1607 loge("setForbidenPlmns: Empty response");
1608 } else if (ar.exception != null) {
1609 loge("setForbiddenPlmns: Exception: " + ar.exception);
1610 request.result = -1;
1611 } else {
1612 loge("setForbiddenPlmns: Unknown exception");
1613 }
1614 }
1615 notifyRequester(request);
1616 break;
1617 case CMD_SET_FORBIDDEN_PLMNS:
1618 request = (MainThreadRequest) msg.obj;
1619 uiccCard = getUiccCardFromRequest(request);
1620 if (uiccCard == null) {
1621 loge("setForbiddenPlmns: UiccCard is null");
1622 request.result = -1;
1623 notifyRequester(request);
1624 break;
1625 }
1626 Pair<Integer, List<String>> setFplmnsArgs =
1627 (Pair<Integer, List<String>>) request.argument;
1628 appType = setFplmnsArgs.first;
1629 List<String> fplmns = setFplmnsArgs.second;
1630 uiccApp = uiccCard.getApplicationByType(appType);
1631 if (uiccApp == null) {
1632 loge("setForbiddenPlmns: no app with specified type -- " + appType);
1633 request.result = -1;
1634 loge("Failed to get UICC App");
1635 notifyRequester(request);
1636 } else {
1637 onCompleted = obtainMessage(EVENT_SET_FORBIDDEN_PLMNS_DONE, request);
1638 ((SIMRecords) uiccApp.getIccRecords())
1639 .setForbiddenPlmns(onCompleted, fplmns);
1640 }
yinchengzhao4d163c02019-12-12 15:21:47 -08001641 break;
Naina Nalluri8ff344d2019-09-17 14:10:30 -07001642 case CMD_ERASE_MODEM_CONFIG:
1643 request = (MainThreadRequest) msg.obj;
1644 onCompleted = obtainMessage(EVENT_ERASE_MODEM_CONFIG_DONE, request);
1645 defaultPhone.eraseModemConfig(onCompleted);
1646 break;
1647 case EVENT_ERASE_MODEM_CONFIG_DONE:
1648 handleNullReturnEvent(msg, "eraseModemConfig");
yincheng zhaod698b842019-09-06 17:06:54 -07001649 break;
zoey chenf95ca592019-12-30 16:11:23 +08001650
1651 case CMD_CHANGE_ICC_LOCK_PASSWORD:
1652 request = (MainThreadRequest) msg.obj;
1653 onCompleted = obtainMessage(EVENT_CHANGE_ICC_LOCK_PASSWORD_DONE, request);
1654 Pair<String, String> changed = (Pair<String, String>) request.argument;
1655 getPhoneFromRequest(request).getIccCard().changeIccLockPassword(
1656 changed.first, changed.second, onCompleted);
1657 break;
1658 case EVENT_CHANGE_ICC_LOCK_PASSWORD_DONE:
1659 ar = (AsyncResult) msg.obj;
1660 request = (MainThreadRequest) ar.userObj;
1661 if (ar.exception == null) {
1662 request.result = TelephonyManager.CHANGE_ICC_LOCK_SUCCESS;
1663 } else {
1664 request.result = msg.arg1;
1665 }
1666 notifyRequester(request);
1667 break;
1668
1669 case CMD_SET_ICC_LOCK_ENABLED:
1670 request = (MainThreadRequest) msg.obj;
1671 onCompleted = obtainMessage(EVENT_SET_ICC_LOCK_ENABLED_DONE, request);
1672 Pair<Boolean, String> enabled = (Pair<Boolean, String>) request.argument;
1673 getPhoneFromRequest(request).getIccCard().setIccLockEnabled(
1674 enabled.first, enabled.second, onCompleted);
1675 break;
1676 case EVENT_SET_ICC_LOCK_ENABLED_DONE:
1677 ar = (AsyncResult) msg.obj;
1678 request = (MainThreadRequest) ar.userObj;
1679 if (ar.exception == null) {
1680 request.result = TelephonyManager.CHANGE_ICC_LOCK_SUCCESS;
1681 } else {
1682 request.result = msg.arg1;
1683 }
1684 notifyRequester(request);
1685 break;
1686
Peter Wangdafb9ac2020-01-15 14:13:38 -08001687 case MSG_NOTIFY_USER_ACTIVITY:
1688 removeMessages(MSG_NOTIFY_USER_ACTIVITY);
Peter Wang59571be2020-01-27 12:35:15 +08001689 Intent intent = new Intent(TelephonyIntents.ACTION_USER_ACTIVITY_NOTIFICATION);
Peter Wangdafb9ac2020-01-15 14:13:38 -08001690 intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY);
1691 getDefaultPhone().getContext().sendBroadcastAsUser(
1692 intent, UserHandle.ALL, permission.USER_ACTIVITY);
1693 break;
Jack Nudelman24d51a52020-11-24 12:08:04 -08001694
1695 case CMD_SET_DATA_THROTTLING: {
1696 request = (MainThreadRequest) msg.obj;
1697 onCompleted = obtainMessage(EVENT_SET_DATA_THROTTLING_DONE, request);
1698 DataThrottlingRequest dataThrottlingRequest =
1699 (DataThrottlingRequest) request.argument;
1700 Phone phone = getPhoneFromRequest(request);
1701 if (phone != null) {
1702 phone.setDataThrottling(onCompleted,
1703 request.workSource, dataThrottlingRequest.getDataThrottlingAction(),
1704 dataThrottlingRequest.getCompletionDurationMillis());
1705 } else {
1706 loge("setDataThrottling: No phone object");
1707 request.result =
1708 TelephonyManager.THERMAL_MITIGATION_RESULT_MODEM_NOT_AVAILABLE;
1709 notifyRequester(request);
1710 }
1711
1712 break;
1713 }
1714 case EVENT_SET_DATA_THROTTLING_DONE:
1715 ar = (AsyncResult) msg.obj;
1716 request = (MainThreadRequest) ar.userObj;
1717
1718 if (ar.exception == null) {
1719 request.result = TelephonyManager.THERMAL_MITIGATION_RESULT_SUCCESS;
1720 } else if (ar.exception instanceof CommandException) {
1721 loge("setDataThrottling: CommandException: " + ar.exception);
1722 CommandException.Error error =
1723 ((CommandException) (ar.exception)).getCommandError();
1724
1725 if (error == CommandException.Error.RADIO_NOT_AVAILABLE) {
1726 request.result = TelephonyManager
1727 .THERMAL_MITIGATION_RESULT_MODEM_NOT_AVAILABLE;
1728 } else if (error == CommandException.Error.INVALID_ARGUMENTS) {
1729 request.result = SET_DATA_THROTTLING_MODEM_THREW_INVALID_PARAMS;
1730 } else {
1731 request.result =
1732 TelephonyManager.THERMAL_MITIGATION_RESULT_MODEM_ERROR;
1733 }
1734 } else {
1735 request.result = TelephonyManager.THERMAL_MITIGATION_RESULT_MODEM_ERROR;
1736 }
1737 Log.w(LOG_TAG, "DataThrottlingResult = " + request.result);
1738 notifyRequester(request);
1739 break;
Jordan Liud5366d92020-11-24 14:50:34 -08001740
1741 case CMD_SET_SIM_POWER: {
1742 request = (MainThreadRequest) msg.obj;
1743 onCompleted = obtainMessage(EVENT_SET_SIM_POWER_DONE, request);
1744 request = (MainThreadRequest) msg.obj;
1745 int stateToSet =
1746 ((Pair<Integer, IIntegerConsumer>)
1747 request.argument).first;
1748 request.phone.setSimPowerState(stateToSet, onCompleted, request.workSource);
1749 break;
1750 }
1751 case EVENT_SET_SIM_POWER_DONE: {
1752 ar = (AsyncResult) msg.obj;
1753 request = (MainThreadRequest) ar.userObj;
1754 IIntegerConsumer callback =
1755 ((Pair<Integer, IIntegerConsumer>) request.argument).second;
1756 if (ar.exception != null) {
1757 loge("setSimPower exception: " + ar.exception);
1758 int errorCode = TelephonyManager.CallForwardingInfoCallback
1759 .RESULT_ERROR_UNKNOWN;
1760 if (ar.exception instanceof CommandException) {
1761 CommandException.Error error =
1762 ((CommandException) (ar.exception)).getCommandError();
1763 if (error == CommandException.Error.SIM_ERR) {
1764 errorCode = TelephonyManager.SET_SIM_POWER_STATE_SIM_ERROR;
1765 } else if (error == CommandException.Error.INVALID_ARGUMENTS) {
1766 errorCode = TelephonyManager.SET_SIM_POWER_STATE_ALREADY_IN_STATE;
1767 } else if (error == CommandException.Error.REQUEST_NOT_SUPPORTED) {
1768 errorCode = TelephonyManager.SET_SIM_POWER_STATE_NOT_SUPPORTED;
1769 } else {
1770 errorCode = TelephonyManager.SET_SIM_POWER_STATE_MODEM_ERROR;
1771 }
1772 }
1773 try {
1774 callback.accept(errorCode);
1775 } catch (RemoteException e) {
1776 // Ignore if the remote process is no longer available to call back.
1777 Log.w(LOG_TAG, "setSimPower: callback not available.");
1778 }
1779 } else {
1780 try {
1781 callback.accept(TelephonyManager.SET_SIM_POWER_STATE_SUCCESS);
1782 } catch (RemoteException e) {
1783 // Ignore if the remote process is no longer available to call back.
1784 Log.w(LOG_TAG, "setSimPower: callback not available.");
1785 }
1786 }
1787 break;
1788 }
1789
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001790 default:
1791 Log.w(LOG_TAG, "MainThreadHandler: unexpected message code: " + msg.what);
1792 break;
1793 }
1794 }
Jake Hambye994d462014-02-03 13:10:13 -08001795
Pengquan Menga1bb6272018-09-06 09:59:22 -07001796 private void notifyRequester(MainThreadRequest request) {
1797 synchronized (request) {
1798 request.notifyAll();
1799 }
1800 }
1801
Jake Hambye994d462014-02-03 13:10:13 -08001802 private void handleNullReturnEvent(Message msg, String command) {
1803 AsyncResult ar = (AsyncResult) msg.obj;
1804 MainThreadRequest request = (MainThreadRequest) ar.userObj;
1805 if (ar.exception == null) {
1806 request.result = true;
1807 } else {
1808 request.result = false;
1809 if (ar.exception instanceof CommandException) {
1810 loge(command + ": CommandException: " + ar.exception);
1811 } else {
1812 loge(command + ": Unknown exception");
1813 }
1814 }
Pengquan Menga1bb6272018-09-06 09:59:22 -07001815 notifyRequester(request);
Jake Hambye994d462014-02-03 13:10:13 -08001816 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001817 }
1818
1819 /**
1820 * Posts the specified command to be executed on the main thread,
1821 * waits for the request to complete, and returns the result.
1822 * @see #sendRequestAsync
1823 */
1824 private Object sendRequest(int command, Object argument) {
Nathan Harold92bed182018-10-12 18:16:49 -07001825 return sendRequest(
1826 command, argument, SubscriptionManager.INVALID_SUBSCRIPTION_ID, null, null);
vagdeviaf9a5b92018-08-15 16:01:53 -07001827 }
1828
1829 /**
1830 * Posts the specified command to be executed on the main thread,
1831 * waits for the request to complete, and returns the result.
1832 * @see #sendRequestAsync
1833 */
1834 private Object sendRequest(int command, Object argument, WorkSource workSource) {
1835 return sendRequest(command, argument, SubscriptionManager.INVALID_SUBSCRIPTION_ID,
Nathan Harold92bed182018-10-12 18:16:49 -07001836 null, workSource);
Wink Saville36469e72014-06-11 15:17:00 -07001837 }
1838
1839 /**
1840 * Posts the specified command to be executed on the main thread,
1841 * waits for the request to complete, and returns the result.
1842 * @see #sendRequestAsync
1843 */
Shishir Agrawal76d5da92014-11-09 16:17:25 -08001844 private Object sendRequest(int command, Object argument, Integer subId) {
Nathan Harold92bed182018-10-12 18:16:49 -07001845 return sendRequest(command, argument, subId, null, null);
vagdeviaf9a5b92018-08-15 16:01:53 -07001846 }
1847
1848 /**
1849 * Posts the specified command to be executed on the main thread,
1850 * waits for the request to complete, and returns the result.
1851 * @see #sendRequestAsync
1852 */
Nathan Harold92bed182018-10-12 18:16:49 -07001853 private Object sendRequest(int command, Object argument, int subId, WorkSource workSource) {
1854 return sendRequest(command, argument, subId, null, workSource);
1855 }
1856
1857 /**
1858 * Posts the specified command to be executed on the main thread,
1859 * waits for the request to complete, and returns the result.
1860 * @see #sendRequestAsync
1861 */
1862 private Object sendRequest(int command, Object argument, Phone phone, WorkSource workSource) {
1863 return sendRequest(
1864 command, argument, SubscriptionManager.INVALID_SUBSCRIPTION_ID, phone, workSource);
1865 }
1866
1867 /**
1868 * Posts the specified command to be executed on the main thread,
1869 * waits for the request to complete, and returns the result.
1870 * @see #sendRequestAsync
1871 */
1872 private Object sendRequest(
1873 int command, Object argument, Integer subId, Phone phone, WorkSource workSource) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001874 if (Looper.myLooper() == mMainThreadHandler.getLooper()) {
1875 throw new RuntimeException("This method will deadlock if called from the main thread.");
1876 }
1877
Nathan Harold92bed182018-10-12 18:16:49 -07001878 MainThreadRequest request = null;
1879 if (subId != SubscriptionManager.INVALID_SUBSCRIPTION_ID && phone != null) {
1880 throw new IllegalArgumentException("subId and phone cannot both be specified!");
1881 } else if (phone != null) {
1882 request = new MainThreadRequest(argument, phone, workSource);
1883 } else {
1884 request = new MainThreadRequest(argument, subId, workSource);
1885 }
1886
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001887 Message msg = mMainThreadHandler.obtainMessage(command, request);
1888 msg.sendToTarget();
1889
1890 // Wait for the request to complete
1891 synchronized (request) {
1892 while (request.result == null) {
1893 try {
1894 request.wait();
1895 } catch (InterruptedException e) {
1896 // Do nothing, go back and wait until the request is complete
1897 }
1898 }
1899 }
1900 return request.result;
1901 }
1902
1903 /**
1904 * Asynchronous ("fire and forget") version of sendRequest():
1905 * Posts the specified command to be executed on the main thread, and
1906 * returns immediately.
1907 * @see #sendRequest
1908 */
1909 private void sendRequestAsync(int command) {
1910 mMainThreadHandler.sendEmptyMessage(command);
1911 }
1912
1913 /**
Sailesh Nepalbd76e4e2013-10-27 13:59:44 -07001914 * Same as {@link #sendRequestAsync(int)} except it takes an argument.
Nathan Haroldfa8da0f2018-09-27 18:51:29 -07001915 * @see {@link #sendRequest(int)}
Sailesh Nepalbd76e4e2013-10-27 13:59:44 -07001916 */
1917 private void sendRequestAsync(int command, Object argument) {
Nathan Haroldfa8da0f2018-09-27 18:51:29 -07001918 sendRequestAsync(command, argument, null, null);
1919 }
1920
1921 /**
1922 * Same as {@link #sendRequestAsync(int,Object)} except it takes a Phone and WorkSource.
1923 * @see {@link #sendRequest(int,Object)}
1924 */
1925 private void sendRequestAsync(
1926 int command, Object argument, Phone phone, WorkSource workSource) {
1927 MainThreadRequest request = new MainThreadRequest(argument, phone, workSource);
Sailesh Nepalbd76e4e2013-10-27 13:59:44 -07001928 Message msg = mMainThreadHandler.obtainMessage(command, request);
1929 msg.sendToTarget();
1930 }
1931
1932 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001933 * Initialize the singleton PhoneInterfaceManager instance.
1934 * This is only done once, at startup, from PhoneApp.onCreate().
1935 */
Nazanin Bakhshif782e562018-12-11 15:15:39 -08001936 /* package */ static PhoneInterfaceManager init(PhoneGlobals app) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001937 synchronized (PhoneInterfaceManager.class) {
1938 if (sInstance == null) {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08001939 sInstance = new PhoneInterfaceManager(app);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001940 } else {
1941 Log.wtf(LOG_TAG, "init() called multiple times! sInstance = " + sInstance);
1942 }
1943 return sInstance;
1944 }
1945 }
1946
1947 /** Private constructor; @see init() */
Jordan Liu1979a042020-03-20 21:39:35 +00001948 private PhoneInterfaceManager(PhoneGlobals app) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001949 mApp = app;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001950 mCM = PhoneGlobals.getInstance().mCM;
Brad Ebinger05f52c22019-12-05 13:03:21 -08001951 mImsResolver = PhoneGlobals.getInstance().getImsResolver();
Stuart Scott981d8582015-04-21 14:09:50 -07001952 mUserManager = (UserManager) app.getSystemService(Context.USER_SERVICE);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001953 mAppOps = (AppOpsManager)app.getSystemService(Context.APP_OPS_SERVICE);
1954 mMainThreadHandler = new MainThreadHandler();
Tobias Thiererb19e1f12018-12-11 17:54:03 +00001955 mSubscriptionController = SubscriptionController.getInstance();
Nazanin Bakhshif782e562018-12-11 15:15:39 -08001956 mTelephonySharedPreferences =
1957 PreferenceManager.getDefaultSharedPreferences(mApp);
yinxub1bed742017-04-17 11:45:04 -07001958 mNetworkScanRequestTracker = new NetworkScanRequestTracker();
Malcolm Chen2c63d402018-08-14 16:00:53 -07001959 mPhoneConfigurationManager = PhoneConfigurationManager.getInstance();
Peter Wanga3cf4ac2020-01-27 09:39:46 +08001960 mNotifyUserActivity = new AtomicBoolean(false);
Wink Saville3ab207e2014-11-20 13:07:20 -08001961
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001962 publish();
1963 }
1964
Nazanin Bakhshif782e562018-12-11 15:15:39 -08001965 private Phone getDefaultPhone() {
1966 Phone thePhone = getPhone(getDefaultSubscription());
1967 return (thePhone != null) ? thePhone : PhoneFactory.getDefaultPhone();
1968 }
1969
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001970 private void publish() {
1971 if (DBG) log("publish: " + this);
1972
Peter Wangc035ce42020-01-08 21:00:22 -08001973 TelephonyFrameworkInitializer
1974 .getTelephonyServiceManager()
1975 .getTelephonyServiceRegisterer()
1976 .register(this);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001977 }
1978
Stuart Scott584921c2015-01-15 17:10:34 -08001979 private Phone getPhoneFromRequest(MainThreadRequest request) {
Jordan Liu4c733742019-02-28 12:03:40 -08001980 if (request.phone != null) {
1981 return request.phone;
1982 } else {
1983 return getPhoneFromSubId(request.subId);
1984 }
1985 }
1986
1987 private Phone getPhoneFromSubId(int subId) {
1988 return (subId == SubscriptionManager.INVALID_SUBSCRIPTION_ID)
1989 ? getDefaultPhone() : getPhone(subId);
Stuart Scott584921c2015-01-15 17:10:34 -08001990 }
1991
Shishir Agrawalc04d9752016-02-19 10:41:00 -08001992 private UiccCard getUiccCardFromRequest(MainThreadRequest request) {
1993 Phone phone = getPhoneFromRequest(request);
1994 return phone == null ? null :
1995 UiccController.getInstance().getUiccCard(phone.getPhoneId());
1996 }
1997
Wink Saville36469e72014-06-11 15:17:00 -07001998 // returns phone associated with the subId.
Wink Savilleb564aae2014-10-23 10:18:09 -07001999 private Phone getPhone(int subId) {
Wink Savilleac1bdfd2014-11-20 23:04:44 -08002000 return PhoneFactory.getPhone(mSubscriptionController.getPhoneId(subId));
Wink Saville36469e72014-06-11 15:17:00 -07002001 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002002
Naina Nalluri8ff344d2019-09-17 14:10:30 -07002003 private void sendEraseModemConfig(Phone phone) {
2004 if (phone != null) {
2005 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
2006 mApp, phone.getSubId(), "eraseModemConfig");
2007 final long identity = Binder.clearCallingIdentity();
2008 try {
2009 Boolean success = (Boolean) sendRequest(CMD_ERASE_MODEM_CONFIG, null);
2010 if (DBG) log("eraseModemConfig:" + ' ' + (success ? "ok" : "fail"));
2011 } finally {
2012 Binder.restoreCallingIdentity(identity);
2013 }
2014 }
2015 }
2016
Peter Wang050bb052020-01-13 23:33:09 -08002017 private boolean isImsAvailableOnDevice() {
2018 PackageManager pm = getDefaultPhone().getContext().getPackageManager();
2019 if (pm == null) {
2020 // For some reason package manger is not available.. This will fail internally anyway,
2021 // so do not throw error and allow.
2022 return true;
2023 }
2024 return pm.hasSystemFeature(PackageManager.FEATURE_TELEPHONY_IMS, 0);
2025 }
2026
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002027 public void dial(String number) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07002028 dialForSubscriber(getPreferredVoiceSubscription(), number);
Wink Saville36469e72014-06-11 15:17:00 -07002029 }
2030
Wink Savilleb564aae2014-10-23 10:18:09 -07002031 public void dialForSubscriber(int subId, String number) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002032 if (DBG) log("dial: " + number);
2033 // No permission check needed here: This is just a wrapper around the
2034 // ACTION_DIAL intent, which is available to any app since it puts up
2035 // the UI before it does anything.
2036
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002037 final long identity = Binder.clearCallingIdentity();
2038 try {
2039 String url = createTelUrl(number);
2040 if (url == null) {
2041 return;
2042 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002043
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002044 // PENDING: should we just silently fail if phone is offhook or ringing?
2045 PhoneConstants.State state = mCM.getState(subId);
2046 if (state != PhoneConstants.State.OFFHOOK && state != PhoneConstants.State.RINGING) {
2047 Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse(url));
2048 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
2049 mApp.startActivity(intent);
2050 }
2051 } finally {
2052 Binder.restoreCallingIdentity(identity);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002053 }
2054 }
2055
2056 public void call(String callingPackage, String number) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07002057 callForSubscriber(getPreferredVoiceSubscription(), callingPackage, number);
Wink Saville36469e72014-06-11 15:17:00 -07002058 }
2059
Wink Savilleb564aae2014-10-23 10:18:09 -07002060 public void callForSubscriber(int subId, String callingPackage, String number) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002061 if (DBG) log("call: " + number);
2062
2063 // This is just a wrapper around the ACTION_CALL intent, but we still
2064 // need to do a permission check since we're calling startActivity()
2065 // from the context of the phone app.
2066 enforceCallPermission();
2067
Jordan Liu1617b712019-07-10 15:06:26 -07002068 if (mAppOps.noteOp(AppOpsManager.OPSTR_CALL_PHONE, Binder.getCallingUid(), callingPackage)
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002069 != AppOpsManager.MODE_ALLOWED) {
2070 return;
2071 }
2072
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002073 final long identity = Binder.clearCallingIdentity();
2074 try {
2075 String url = createTelUrl(number);
2076 if (url == null) {
2077 return;
2078 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002079
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002080 boolean isValid = false;
2081 final List<SubscriptionInfo> slist = getActiveSubscriptionInfoListPrivileged();
2082 if (slist != null) {
2083 for (SubscriptionInfo subInfoRecord : slist) {
2084 if (subInfoRecord.getSubscriptionId() == subId) {
2085 isValid = true;
2086 break;
2087 }
Wink Saville3ab207e2014-11-20 13:07:20 -08002088 }
Wink Saville08874612014-08-31 19:19:58 -07002089 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002090 if (!isValid) {
2091 return;
2092 }
Wink Saville08874612014-08-31 19:19:58 -07002093
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002094 Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse(url));
2095 intent.putExtra(SUBSCRIPTION_KEY, subId);
2096 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
2097 mApp.startActivity(intent);
2098 } finally {
2099 Binder.restoreCallingIdentity(identity);
2100 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002101 }
2102
Wink Savilleb564aae2014-10-23 10:18:09 -07002103 public boolean supplyPinForSubscriber(int subId, String pin) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07002104 int [] resultArray = supplyPinReportResultForSubscriber(subId, pin);
Wink Saville9de0f752013-10-22 19:04:03 -07002105 return (resultArray[0] == PhoneConstants.PIN_RESULT_SUCCESS) ? true : false;
2106 }
2107
Wink Savilleb564aae2014-10-23 10:18:09 -07002108 public boolean supplyPukForSubscriber(int subId, String puk, String pin) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07002109 int [] resultArray = supplyPukReportResultForSubscriber(subId, puk, pin);
Wink Saville9de0f752013-10-22 19:04:03 -07002110 return (resultArray[0] == PhoneConstants.PIN_RESULT_SUCCESS) ? true : false;
2111 }
2112
Wink Savilleb564aae2014-10-23 10:18:09 -07002113 public int[] supplyPinReportResultForSubscriber(int subId, String pin) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002114 enforceModifyPermission();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002115
2116 final long identity = Binder.clearCallingIdentity();
2117 try {
2118 final UnlockSim checkSimPin = new UnlockSim(getPhone(subId).getIccCard());
2119 checkSimPin.start();
2120 return checkSimPin.unlockSim(null, pin);
2121 } finally {
2122 Binder.restoreCallingIdentity(identity);
2123 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002124 }
2125
Wink Savilleb564aae2014-10-23 10:18:09 -07002126 public int[] supplyPukReportResultForSubscriber(int subId, String puk, String pin) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002127 enforceModifyPermission();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002128
2129 final long identity = Binder.clearCallingIdentity();
2130 try {
2131 final UnlockSim checkSimPuk = new UnlockSim(getPhone(subId).getIccCard());
2132 checkSimPuk.start();
2133 return checkSimPuk.unlockSim(puk, pin);
2134 } finally {
2135 Binder.restoreCallingIdentity(identity);
2136 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002137 }
2138
2139 /**
Wink Saville9de0f752013-10-22 19:04:03 -07002140 * Helper thread to turn async call to SimCard#supplyPin into
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002141 * a synchronous one.
2142 */
2143 private static class UnlockSim extends Thread {
2144
2145 private final IccCard mSimCard;
2146
2147 private boolean mDone = false;
Wink Saville9de0f752013-10-22 19:04:03 -07002148 private int mResult = PhoneConstants.PIN_GENERAL_FAILURE;
2149 private int mRetryCount = -1;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002150
2151 // For replies from SimCard interface
2152 private Handler mHandler;
2153
2154 // For async handler to identify request type
2155 private static final int SUPPLY_PIN_COMPLETE = 100;
2156
2157 public UnlockSim(IccCard simCard) {
2158 mSimCard = simCard;
2159 }
2160
2161 @Override
2162 public void run() {
2163 Looper.prepare();
2164 synchronized (UnlockSim.this) {
2165 mHandler = new Handler() {
2166 @Override
2167 public void handleMessage(Message msg) {
2168 AsyncResult ar = (AsyncResult) msg.obj;
2169 switch (msg.what) {
2170 case SUPPLY_PIN_COMPLETE:
2171 Log.d(LOG_TAG, "SUPPLY_PIN_COMPLETE");
2172 synchronized (UnlockSim.this) {
Wink Saville9de0f752013-10-22 19:04:03 -07002173 mRetryCount = msg.arg1;
2174 if (ar.exception != null) {
2175 if (ar.exception instanceof CommandException &&
2176 ((CommandException)(ar.exception)).getCommandError()
2177 == CommandException.Error.PASSWORD_INCORRECT) {
2178 mResult = PhoneConstants.PIN_PASSWORD_INCORRECT;
vivi.lib5e9ada2019-09-12 16:04:24 +08002179 } //When UiccCardApp dispose,handle message and return exception
2180 else if (ar.exception instanceof CommandException &&
2181 ((CommandException) (ar.exception)).getCommandError()
2182 == CommandException.Error.ABORTED) {
2183 mResult = PhoneConstants.PIN_OPERATION_ABORTED;
Wink Saville9de0f752013-10-22 19:04:03 -07002184 } else {
2185 mResult = PhoneConstants.PIN_GENERAL_FAILURE;
2186 }
2187 } else {
2188 mResult = PhoneConstants.PIN_RESULT_SUCCESS;
2189 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002190 mDone = true;
2191 UnlockSim.this.notifyAll();
2192 }
2193 break;
2194 }
2195 }
2196 };
2197 UnlockSim.this.notifyAll();
2198 }
2199 Looper.loop();
2200 }
2201
2202 /*
2203 * Use PIN or PUK to unlock SIM card
2204 *
2205 * If PUK is null, unlock SIM card with PIN
2206 *
2207 * If PUK is not null, unlock SIM card with PUK and set PIN code
2208 */
Wink Saville9de0f752013-10-22 19:04:03 -07002209 synchronized int[] unlockSim(String puk, String pin) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002210
2211 while (mHandler == null) {
2212 try {
2213 wait();
2214 } catch (InterruptedException e) {
2215 Thread.currentThread().interrupt();
2216 }
2217 }
2218 Message callback = Message.obtain(mHandler, SUPPLY_PIN_COMPLETE);
2219
2220 if (puk == null) {
2221 mSimCard.supplyPin(pin, callback);
2222 } else {
2223 mSimCard.supplyPuk(puk, pin, callback);
2224 }
2225
2226 while (!mDone) {
2227 try {
2228 Log.d(LOG_TAG, "wait for done");
2229 wait();
2230 } catch (InterruptedException e) {
2231 // Restore the interrupted status
2232 Thread.currentThread().interrupt();
2233 }
2234 }
2235 Log.d(LOG_TAG, "done");
Wink Saville9de0f752013-10-22 19:04:03 -07002236 int[] resultArray = new int[2];
2237 resultArray[0] = mResult;
2238 resultArray[1] = mRetryCount;
2239 return resultArray;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002240 }
2241 }
2242
Nathan Harold7c8d0f12020-05-28 20:40:31 -07002243 /**
2244 * This method has been removed due to privacy and stability concerns.
2245 */
2246 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002247 public void updateServiceLocation() {
Nathan Harold7c8d0f12020-05-28 20:40:31 -07002248 Log.e(LOG_TAG, "Call to unsupported method updateServiceLocation()");
2249 return;
Wink Saville36469e72014-06-11 15:17:00 -07002250 }
2251
Nathan Harold1f889d82020-06-04 17:05:26 -07002252 @Override
2253 public void updateServiceLocationWithPackageName(String callingPackage) {
2254 mApp.getSystemService(AppOpsManager.class)
2255 .checkPackage(Binder.getCallingUid(), callingPackage);
2256
2257 final int targetSdk = getTargetSdk(callingPackage);
2258 if (targetSdk > android.os.Build.VERSION_CODES.R) {
2259 // Callers targeting S have no business invoking this method.
2260 return;
2261 }
2262
2263 LocationAccessPolicy.LocationPermissionResult locationResult =
2264 LocationAccessPolicy.checkLocationPermission(mApp,
2265 new LocationAccessPolicy.LocationPermissionQuery.Builder()
2266 .setCallingPackage(callingPackage)
2267 .setCallingFeatureId(null)
2268 .setCallingPid(Binder.getCallingPid())
2269 .setCallingUid(Binder.getCallingUid())
2270 .setMethod("updateServiceLocation")
2271 .setMinSdkVersionForCoarse(Build.VERSION_CODES.BASE)
2272 .setMinSdkVersionForFine(Build.VERSION_CODES.Q)
2273 .build());
2274 // Apps that lack location permission have no business calling this method;
2275 // however, because no permission was declared in the public API, denials must
2276 // all be "soft".
2277 switch (locationResult) {
2278 case DENIED_HARD: /* fall through */
2279 case DENIED_SOFT:
2280 return;
2281 }
2282
2283 WorkSource workSource = getWorkSource(Binder.getCallingUid());
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002284 final long identity = Binder.clearCallingIdentity();
2285 try {
Nathan Harold1f889d82020-06-04 17:05:26 -07002286 final Phone phone = getPhone(getDefaultSubscription());
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002287 if (phone != null) {
Nathan Harold1f889d82020-06-04 17:05:26 -07002288 phone.updateServiceLocation(workSource);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002289 }
2290 } finally {
2291 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07002292 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002293 }
2294
Philip P. Moltmann9797cbb2020-01-07 13:45:00 -08002295 @Deprecated
Robert Greenwalt36b23af2015-07-06 17:59:14 -07002296 @Override
2297 public boolean isRadioOn(String callingPackage) {
Philip P. Moltmann9797cbb2020-01-07 13:45:00 -08002298 return isRadioOnWithFeature(callingPackage, null);
Wink Saville36469e72014-06-11 15:17:00 -07002299 }
2300
Philip P. Moltmann700a9592019-10-03 11:53:50 -07002301
Robert Greenwalt36b23af2015-07-06 17:59:14 -07002302 @Override
Philip P. Moltmann9797cbb2020-01-07 13:45:00 -08002303 public boolean isRadioOnWithFeature(String callingPackage, String callingFeatureId) {
2304 return isRadioOnForSubscriberWithFeature(getDefaultSubscription(), callingPackage,
2305 callingFeatureId);
2306 }
2307
2308 @Deprecated
2309 @Override
Robert Greenwalt36b23af2015-07-06 17:59:14 -07002310 public boolean isRadioOnForSubscriber(int subId, String callingPackage) {
Philip P. Moltmann9797cbb2020-01-07 13:45:00 -08002311 return isRadioOnForSubscriberWithFeature(subId, callingPackage, null);
2312 }
2313
2314 @Override
2315 public boolean isRadioOnForSubscriberWithFeature(int subId, String callingPackage,
2316 String callingFeatureId) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08002317 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
Philip P. Moltmann9797cbb2020-01-07 13:45:00 -08002318 mApp, subId, callingPackage, callingFeatureId, "isRadioOnForSubscriber")) {
Robert Greenwalt36b23af2015-07-06 17:59:14 -07002319 return false;
2320 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002321
2322 final long identity = Binder.clearCallingIdentity();
2323 try {
2324 return isRadioOnForSubscriber(subId);
2325 } finally {
2326 Binder.restoreCallingIdentity(identity);
2327 }
Robert Greenwalt36b23af2015-07-06 17:59:14 -07002328 }
2329
2330 private boolean isRadioOnForSubscriber(int subId) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002331 final long identity = Binder.clearCallingIdentity();
2332 try {
2333 final Phone phone = getPhone(subId);
2334 if (phone != null) {
2335 return phone.getServiceState().getState() != ServiceState.STATE_POWER_OFF;
2336 } else {
2337 return false;
2338 }
2339 } finally {
2340 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07002341 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002342 }
2343
2344 public void toggleRadioOnOff() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07002345 toggleRadioOnOffForSubscriber(getDefaultSubscription());
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002346 }
Wink Saville36469e72014-06-11 15:17:00 -07002347
Wink Savilleb564aae2014-10-23 10:18:09 -07002348 public void toggleRadioOnOffForSubscriber(int subId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002349 enforceModifyPermission();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002350
2351 final long identity = Binder.clearCallingIdentity();
2352 try {
2353 final Phone phone = getPhone(subId);
2354 if (phone != null) {
2355 phone.setRadioPower(!isRadioOnForSubscriber(subId));
2356 }
2357 } finally {
2358 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07002359 }
Wink Saville36469e72014-06-11 15:17:00 -07002360 }
2361
2362 public boolean setRadio(boolean turnOn) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07002363 return setRadioForSubscriber(getDefaultSubscription(), turnOn);
Wink Saville36469e72014-06-11 15:17:00 -07002364 }
2365
Wink Savilleb564aae2014-10-23 10:18:09 -07002366 public boolean setRadioForSubscriber(int subId, boolean turnOn) {
Wink Saville36469e72014-06-11 15:17:00 -07002367 enforceModifyPermission();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002368
2369 final long identity = Binder.clearCallingIdentity();
2370 try {
2371 final Phone phone = getPhone(subId);
2372 if (phone == null) {
2373 return false;
2374 }
2375 if ((phone.getServiceState().getState() != ServiceState.STATE_POWER_OFF) != turnOn) {
2376 toggleRadioOnOffForSubscriber(subId);
2377 }
2378 return true;
2379 } finally {
2380 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07002381 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002382 }
Wink Saville36469e72014-06-11 15:17:00 -07002383
Naveen Kalla1fd79bd2014-08-08 00:48:59 -07002384 public boolean needMobileRadioShutdown() {
Shuo Qianafeaf7d2019-12-10 10:40:38 -08002385 enforceReadPrivilegedPermission("needMobileRadioShutdown");
Naveen Kalla1fd79bd2014-08-08 00:48:59 -07002386 /*
2387 * If any of the Radios are available, it will need to be
2388 * shutdown. So return true if any Radio is available.
2389 */
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002390 final long identity = Binder.clearCallingIdentity();
2391 try {
2392 for (int i = 0; i < TelephonyManager.getDefault().getPhoneCount(); i++) {
2393 Phone phone = PhoneFactory.getPhone(i);
2394 if (phone != null && phone.isRadioAvailable()) return true;
2395 }
2396 logv(TelephonyManager.getDefault().getPhoneCount() + " Phones are shutdown.");
2397 return false;
2398 } finally {
2399 Binder.restoreCallingIdentity(identity);
Naveen Kalla1fd79bd2014-08-08 00:48:59 -07002400 }
Naveen Kalla1fd79bd2014-08-08 00:48:59 -07002401 }
2402
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002403 @Override
Naveen Kalla1fd79bd2014-08-08 00:48:59 -07002404 public void shutdownMobileRadios() {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002405 enforceModifyPermission();
2406
2407 final long identity = Binder.clearCallingIdentity();
2408 try {
2409 for (int i = 0; i < TelephonyManager.getDefault().getPhoneCount(); i++) {
2410 logv("Shutting down Phone " + i);
2411 shutdownRadioUsingPhoneId(i);
2412 }
2413 } finally {
2414 Binder.restoreCallingIdentity(identity);
Naveen Kalla1fd79bd2014-08-08 00:48:59 -07002415 }
2416 }
2417
2418 private void shutdownRadioUsingPhoneId(int phoneId) {
Naveen Kalla1fd79bd2014-08-08 00:48:59 -07002419 Phone phone = PhoneFactory.getPhone(phoneId);
2420 if (phone != null && phone.isRadioAvailable()) {
2421 phone.shutdownRadio();
2422 }
2423 }
2424
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002425 public boolean setRadioPower(boolean turnOn) {
Jack Yub4e16162017-05-15 12:48:40 -07002426 enforceModifyPermission();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002427
2428 final long identity = Binder.clearCallingIdentity();
2429 try {
2430 final Phone defaultPhone = PhoneFactory.getDefaultPhone();
2431 if (defaultPhone != null) {
2432 defaultPhone.setRadioPower(turnOn);
2433 return true;
2434 } else {
2435 loge("There's no default phone.");
2436 return false;
2437 }
2438 } finally {
2439 Binder.restoreCallingIdentity(identity);
Wei Liu9ae2a062016-08-08 11:09:34 -07002440 }
Wink Saville36469e72014-06-11 15:17:00 -07002441 }
2442
Wink Savilleb564aae2014-10-23 10:18:09 -07002443 public boolean setRadioPowerForSubscriber(int subId, boolean turnOn) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002444 enforceModifyPermission();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002445
2446 final long identity = Binder.clearCallingIdentity();
2447 try {
2448 final Phone phone = getPhone(subId);
2449 if (phone != null) {
2450 phone.setRadioPower(turnOn);
2451 return true;
2452 } else {
2453 return false;
2454 }
2455 } finally {
2456 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07002457 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002458 }
2459
Wink Saville36469e72014-06-11 15:17:00 -07002460 // FIXME: subId version needed
Sanket Padawe356d7632015-06-22 14:03:32 -07002461 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002462 public boolean enableDataConnectivity() {
2463 enforceModifyPermission();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002464
2465 final long identity = Binder.clearCallingIdentity();
2466 try {
2467 int subId = mSubscriptionController.getDefaultDataSubId();
2468 final Phone phone = getPhone(subId);
2469 if (phone != null) {
Sooraj Sasindran2675c0b2020-04-19 18:14:03 -07002470 phone.getDataEnabledSettings().setDataEnabled(
2471 TelephonyManager.DATA_ENABLED_REASON_USER, true);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002472 return true;
2473 } else {
2474 return false;
2475 }
2476 } finally {
2477 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07002478 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002479 }
2480
Wink Saville36469e72014-06-11 15:17:00 -07002481 // FIXME: subId version needed
Sanket Padawe356d7632015-06-22 14:03:32 -07002482 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002483 public boolean disableDataConnectivity() {
2484 enforceModifyPermission();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002485
2486 final long identity = Binder.clearCallingIdentity();
2487 try {
2488 int subId = mSubscriptionController.getDefaultDataSubId();
2489 final Phone phone = getPhone(subId);
2490 if (phone != null) {
Sooraj Sasindran2675c0b2020-04-19 18:14:03 -07002491 phone.getDataEnabledSettings().setDataEnabled(
2492 TelephonyManager.DATA_ENABLED_REASON_USER, false);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002493 return true;
2494 } else {
2495 return false;
2496 }
2497 } finally {
2498 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07002499 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002500 }
2501
Sanket Padawe356d7632015-06-22 14:03:32 -07002502 @Override
Jack Yuacf8a132017-05-01 17:00:48 -07002503 public boolean isDataConnectivityPossible(int subId) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002504 final long identity = Binder.clearCallingIdentity();
2505 try {
2506 final Phone phone = getPhone(subId);
2507 if (phone != null) {
Jack Yub5d8f642018-11-26 11:20:48 -08002508 return phone.isDataAllowed(ApnSetting.TYPE_DEFAULT);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002509 } else {
2510 return false;
2511 }
2512 } finally {
2513 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07002514 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002515 }
2516
2517 public boolean handlePinMmi(String dialString) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07002518 return handlePinMmiForSubscriber(getDefaultSubscription(), dialString);
Wink Saville36469e72014-06-11 15:17:00 -07002519 }
2520
pkanwarae03a6b2016-11-06 20:37:09 -08002521 public void handleUssdRequest(int subId, String ussdRequest, ResultReceiver wrappedCallback) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002522 enforceCallPermission();
2523
2524 final long identity = Binder.clearCallingIdentity();
2525 try {
2526 if (!SubscriptionManager.isValidSubscriptionId(subId)) {
2527 return;
2528 }
2529 Pair<String, ResultReceiver> ussdObject = new Pair(ussdRequest, wrappedCallback);
2530 sendRequest(CMD_HANDLE_USSD_REQUEST, ussdObject, subId);
2531 } finally {
2532 Binder.restoreCallingIdentity(identity);
2533 }
pkanwar32d516d2016-10-14 19:37:38 -07002534 };
2535
Wink Savilleb564aae2014-10-23 10:18:09 -07002536 public boolean handlePinMmiForSubscriber(int subId, String dialString) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002537 enforceModifyPermission();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002538
2539 final long identity = Binder.clearCallingIdentity();
2540 try {
2541 if (!SubscriptionManager.isValidSubscriptionId(subId)) {
2542 return false;
2543 }
2544 return (Boolean) sendRequest(CMD_HANDLE_PIN_MMI, dialString, subId);
2545 } finally {
2546 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07002547 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002548 }
2549
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002550 public int getCallState() {
Shishir Agrawala9f32182016-04-12 12:00:16 -07002551 return getCallStateForSlot(getSlotForDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07002552 }
2553
Sanket Padawe13bac7b2017-03-20 15:04:47 -07002554 public int getCallStateForSlot(int slotIndex) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002555 final long identity = Binder.clearCallingIdentity();
2556 try {
2557 Phone phone = PhoneFactory.getPhone(slotIndex);
2558 return phone == null ? TelephonyManager.CALL_STATE_IDLE :
2559 PhoneConstantConversions.convertCallState(phone.getState());
2560 } finally {
2561 Binder.restoreCallingIdentity(identity);
2562 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002563 }
2564
Sanket Padawe356d7632015-06-22 14:03:32 -07002565 @Override
Nathan Harolde037c472019-06-26 00:41:07 +00002566 public int getDataState() {
Nathan Haroldc4689b12019-06-14 16:58:30 -07002567 return getDataStateForSubId(mSubscriptionController.getDefaultDataSubId());
2568 }
2569
2570 @Override
2571 public int getDataStateForSubId(int subId) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002572 final long identity = Binder.clearCallingIdentity();
2573 try {
Nathan Haroldc4689b12019-06-14 16:58:30 -07002574 final Phone phone = getPhone(subId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002575 if (phone != null) {
2576 return PhoneConstantConversions.convertDataState(phone.getDataConnectionState());
2577 } else {
2578 return PhoneConstantConversions.convertDataState(
2579 PhoneConstants.DataState.DISCONNECTED);
2580 }
2581 } finally {
2582 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07002583 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002584 }
2585
Sanket Padawe356d7632015-06-22 14:03:32 -07002586 @Override
Nathan Harolde037c472019-06-26 00:41:07 +00002587 public int getDataActivity() {
Nathan Haroldc4689b12019-06-14 16:58:30 -07002588 return getDataActivityForSubId(mSubscriptionController.getDefaultDataSubId());
2589 }
2590
2591 @Override
2592 public int getDataActivityForSubId(int subId) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002593 final long identity = Binder.clearCallingIdentity();
2594 try {
Nathan Haroldc4689b12019-06-14 16:58:30 -07002595 final Phone phone = getPhone(subId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002596 if (phone != null) {
2597 return DefaultPhoneNotifier.convertDataActivityState(phone.getDataActivityState());
2598 } else {
2599 return TelephonyManager.DATA_ACTIVITY_NONE;
2600 }
2601 } finally {
2602 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07002603 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002604 }
2605
2606 @Override
Meng Wangd64acad2019-12-09 13:13:01 -08002607 public CellIdentity getCellLocation(String callingPackage, String callingFeatureId) {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08002608 mApp.getSystemService(AppOpsManager.class)
Hall Liu1aa510f2017-11-22 17:40:08 -08002609 .checkPackage(Binder.getCallingUid(), callingPackage);
Hall Liuf19c44f2018-11-27 14:38:17 -08002610
2611 LocationAccessPolicy.LocationPermissionResult locationResult =
2612 LocationAccessPolicy.checkLocationPermission(mApp,
2613 new LocationAccessPolicy.LocationPermissionQuery.Builder()
2614 .setCallingPackage(callingPackage)
Philip P. Moltmann9797cbb2020-01-07 13:45:00 -08002615 .setCallingFeatureId(callingFeatureId)
Hall Liuf19c44f2018-11-27 14:38:17 -08002616 .setCallingPid(Binder.getCallingPid())
2617 .setCallingUid(Binder.getCallingUid())
2618 .setMethod("getCellLocation")
Hall Liuc3f8eb62020-01-24 18:07:12 -08002619 .setMinSdkVersionForCoarse(Build.VERSION_CODES.BASE)
Hall Liuf19c44f2018-11-27 14:38:17 -08002620 .setMinSdkVersionForFine(Build.VERSION_CODES.Q)
2621 .build());
2622 switch (locationResult) {
2623 case DENIED_HARD:
2624 throw new SecurityException("Not allowed to access cell location");
2625 case DENIED_SOFT:
Meng Wangd64acad2019-12-09 13:13:01 -08002626 return (getDefaultPhone().getPhoneType() == PhoneConstants.PHONE_TYPE_CDMA)
2627 ? new CellIdentityCdma() : new CellIdentityGsm();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002628 }
2629
Narayan Kamathf04b5a12018-01-09 11:47:15 +00002630 WorkSource workSource = getWorkSource(Binder.getCallingUid());
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002631 final long identity = Binder.clearCallingIdentity();
2632 try {
2633 if (DBG_LOC) log("getCellLocation: is active user");
Nathan Harold3ff88932018-08-14 10:19:49 -07002634 int subId = mSubscriptionController.getDefaultDataSubId();
Meng Wangd64acad2019-12-09 13:13:01 -08002635 return (CellIdentity) sendRequest(CMD_GET_CELL_LOCATION, workSource, subId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002636 } finally {
2637 Binder.restoreCallingIdentity(identity);
2638 }
Svetoslav64fad262015-04-14 14:35:21 -07002639 }
2640
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002641 @Override
Jack Yu01425032020-02-22 19:38:58 -08002642 public String getNetworkCountryIsoForPhone(int phoneId) {
Jonathan Basseribf5362b2017-07-19 12:22:35 -07002643 // Reporting the correct network country is ambiguous when IWLAN could conflict with
2644 // registered cell info, so return a NULL country instead.
2645 final long identity = Binder.clearCallingIdentity();
2646 try {
Malcolm Chen3732c2b2018-07-18 20:15:24 -07002647 if (phoneId == SubscriptionManager.INVALID_PHONE_INDEX) {
2648 // Get default phone in this case.
2649 phoneId = SubscriptionManager.DEFAULT_PHONE_INDEX;
2650 }
Jonathan Basseribf5362b2017-07-19 12:22:35 -07002651 final int subId = mSubscriptionController.getSubIdUsingPhoneId(phoneId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002652 Phone phone = PhoneFactory.getPhone(phoneId);
Nathan Haroldcf38ed92020-04-21 19:31:10 -07002653 if (phone == null) return "";
2654 ServiceStateTracker sst = phone.getServiceStateTracker();
2655 if (sst == null) return "";
2656 LocaleTracker lt = sst.getLocaleTracker();
2657 if (lt == null) return "";
2658 if (!TextUtils.isEmpty(lt.getCurrentCountry())) return lt.getCurrentCountry();
2659 EmergencyNumberTracker ent = phone.getEmergencyNumberTracker();
2660 return (ent == null) ? "" : ent.getEmergencyCountryIso();
Jonathan Basseribf5362b2017-07-19 12:22:35 -07002661 } finally {
2662 Binder.restoreCallingIdentity(identity);
2663 }
Jonathan Basseribf5362b2017-07-19 12:22:35 -07002664 }
2665
Nathan Harold7c8d0f12020-05-28 20:40:31 -07002666 /**
2667 * This method was removed due to potential issues caused by performing partial
2668 * updates of service state, and lack of a credible use case.
2669 *
2670 * This has the ability to break the telephony implementation by disabling notification of
2671 * changes in device connectivity. DO NOT USE THIS!
2672 */
Jonathan Basseribf5362b2017-07-19 12:22:35 -07002673 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002674 public void enableLocationUpdates() {
2675 mApp.enforceCallingOrSelfPermission(
2676 android.Manifest.permission.CONTROL_LOCATION_UPDATES, null);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002677 }
2678
Nathan Harold7c8d0f12020-05-28 20:40:31 -07002679 /**
2680 * This method was removed due to potential issues caused by performing partial
2681 * updates of service state, and lack of a credible use case.
2682 *
2683 * This has the ability to break the telephony implementation by disabling notification of
2684 * changes in device connectivity. DO NOT USE THIS!
2685 */
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002686 @Override
2687 public void disableLocationUpdates() {
2688 mApp.enforceCallingOrSelfPermission(
2689 android.Manifest.permission.CONTROL_LOCATION_UPDATES, null);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002690 }
2691
Nathan Harold31d7ff32018-10-15 20:20:30 -07002692 /**
2693 * Returns the target SDK version number for a given package name.
2694 *
Nathan Haroldec184742019-07-10 17:04:16 -07002695 * This call MUST be invoked before clearing the calling UID.
2696 *
Nathan Harold31d7ff32018-10-15 20:20:30 -07002697 * @return target SDK if the package is found or INT_MAX.
2698 */
2699 private int getTargetSdk(String packageName) {
2700 try {
Nathan Haroldec184742019-07-10 17:04:16 -07002701 final ApplicationInfo ai = mApp.getPackageManager().getApplicationInfoAsUser(
Chen Xu54d20302019-07-30 15:12:06 -07002702 packageName, 0, UserHandle.getUserHandleForUid(Binder.getCallingUid()));
Nathan Harold31d7ff32018-10-15 20:20:30 -07002703 if (ai != null) return ai.targetSdkVersion;
2704 } catch (PackageManager.NameNotFoundException unexpected) {
Nathan Haroldec184742019-07-10 17:04:16 -07002705 loge("Failed to get package info for pkg="
2706 + packageName + ", uid=" + Binder.getCallingUid());
Nathan Harold31d7ff32018-10-15 20:20:30 -07002707 }
2708 return Integer.MAX_VALUE;
2709 }
2710
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002711 @Override
2712 @SuppressWarnings("unchecked")
Philip P. Moltmann9797cbb2020-01-07 13:45:00 -08002713 public List<NeighboringCellInfo> getNeighboringCellInfo(String callingPackage,
2714 String callingFeatureId) {
Nathan Harold31d7ff32018-10-15 20:20:30 -07002715 final int targetSdk = getTargetSdk(callingPackage);
Nathan Harolddbea45a2018-08-30 14:35:07 -07002716 if (targetSdk >= android.os.Build.VERSION_CODES.Q) {
2717 throw new SecurityException(
2718 "getNeighboringCellInfo() is unavailable to callers targeting Q+ SDK levels.");
2719 }
Nathan Haroldb4d55612018-07-20 13:13:08 -07002720
Jordan Liu1617b712019-07-10 15:06:26 -07002721 if (mAppOps.noteOp(AppOpsManager.OPSTR_NEIGHBORING_CELLS, Binder.getCallingUid(),
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002722 callingPackage) != AppOpsManager.MODE_ALLOWED) {
2723 return null;
2724 }
Svetoslav64fad262015-04-14 14:35:21 -07002725
Svetoslav Ganov4a9d4482017-06-20 19:53:35 -07002726 if (DBG_LOC) log("getNeighboringCellInfo: is active user");
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002727
Philip P. Moltmann9797cbb2020-01-07 13:45:00 -08002728 List<CellInfo> info = getAllCellInfo(callingPackage, callingFeatureId);
Nathan Haroldf180aac2018-06-01 18:43:55 -07002729 if (info == null) return null;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002730
Nathan Haroldf180aac2018-06-01 18:43:55 -07002731 List<NeighboringCellInfo> neighbors = new ArrayList<NeighboringCellInfo>();
2732 for (CellInfo ci : info) {
2733 if (ci instanceof CellInfoGsm) {
2734 neighbors.add(new NeighboringCellInfo((CellInfoGsm) ci));
2735 } else if (ci instanceof CellInfoWcdma) {
2736 neighbors.add(new NeighboringCellInfo((CellInfoWcdma) ci));
2737 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002738 }
Nathan Haroldf180aac2018-06-01 18:43:55 -07002739 return (neighbors.size()) > 0 ? neighbors : null;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002740 }
2741
Nathan Haroldfa8da0f2018-09-27 18:51:29 -07002742 private List<CellInfo> getCachedCellInfo() {
2743 List<CellInfo> cellInfos = new ArrayList<CellInfo>();
2744 for (Phone phone : PhoneFactory.getPhones()) {
2745 List<CellInfo> info = phone.getAllCellInfo();
2746 if (info != null) cellInfos.addAll(info);
2747 }
2748 return cellInfos;
2749 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002750
2751 @Override
Philip P. Moltmann9797cbb2020-01-07 13:45:00 -08002752 public List<CellInfo> getAllCellInfo(String callingPackage, String callingFeatureId) {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08002753 mApp.getSystemService(AppOpsManager.class)
Hall Liu1aa510f2017-11-22 17:40:08 -08002754 .checkPackage(Binder.getCallingUid(), callingPackage);
Hall Liuf19c44f2018-11-27 14:38:17 -08002755
2756 LocationAccessPolicy.LocationPermissionResult locationResult =
2757 LocationAccessPolicy.checkLocationPermission(mApp,
2758 new LocationAccessPolicy.LocationPermissionQuery.Builder()
2759 .setCallingPackage(callingPackage)
Philip P. Moltmann9797cbb2020-01-07 13:45:00 -08002760 .setCallingFeatureId(callingFeatureId)
Hall Liuf19c44f2018-11-27 14:38:17 -08002761 .setCallingPid(Binder.getCallingPid())
2762 .setCallingUid(Binder.getCallingUid())
2763 .setMethod("getAllCellInfo")
Nathan Harold5ae50b52019-02-20 15:46:36 -08002764 .setMinSdkVersionForCoarse(Build.VERSION_CODES.BASE)
Hall Liuf19c44f2018-11-27 14:38:17 -08002765 .setMinSdkVersionForFine(Build.VERSION_CODES.Q)
2766 .build());
2767 switch (locationResult) {
2768 case DENIED_HARD:
2769 throw new SecurityException("Not allowed to access cell info");
2770 case DENIED_SOFT:
2771 return new ArrayList<>();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002772 }
2773
Nathan Haroldfa8da0f2018-09-27 18:51:29 -07002774 final int targetSdk = getTargetSdk(callingPackage);
2775 if (targetSdk >= android.os.Build.VERSION_CODES.Q) {
2776 return getCachedCellInfo();
2777 }
2778
Svetoslav Ganov4a9d4482017-06-20 19:53:35 -07002779 if (DBG_LOC) log("getAllCellInfo: is active user");
Narayan Kamathf04b5a12018-01-09 11:47:15 +00002780 WorkSource workSource = getWorkSource(Binder.getCallingUid());
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002781 final long identity = Binder.clearCallingIdentity();
2782 try {
2783 List<CellInfo> cellInfos = new ArrayList<CellInfo>();
2784 for (Phone phone : PhoneFactory.getPhones()) {
Nathan Harold3ff88932018-08-14 10:19:49 -07002785 final List<CellInfo> info = (List<CellInfo>) sendRequest(
Nathan Harold92bed182018-10-12 18:16:49 -07002786 CMD_GET_ALL_CELL_INFO, null, phone, workSource);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002787 if (info != null) cellInfos.addAll(info);
2788 }
2789 return cellInfos;
2790 } finally {
2791 Binder.restoreCallingIdentity(identity);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002792 }
2793 }
2794
Sailesh Nepalbd76e4e2013-10-27 13:59:44 -07002795 @Override
Philip P. Moltmann9797cbb2020-01-07 13:45:00 -08002796 public void requestCellInfoUpdate(int subId, ICellInfoCallback cb, String callingPackage,
2797 String callingFeatureId) {
2798 requestCellInfoUpdateInternal(subId, cb, callingPackage, callingFeatureId,
2799 getWorkSource(Binder.getCallingUid()));
Nathan Haroldfa8da0f2018-09-27 18:51:29 -07002800 }
2801
2802 @Override
Philip P. Moltmann9797cbb2020-01-07 13:45:00 -08002803 public void requestCellInfoUpdateWithWorkSource(int subId, ICellInfoCallback cb,
2804 String callingPackage, String callingFeatureId, WorkSource workSource) {
Nathan Haroldfa8da0f2018-09-27 18:51:29 -07002805 enforceModifyPermission();
Philip P. Moltmann9797cbb2020-01-07 13:45:00 -08002806 requestCellInfoUpdateInternal(subId, cb, callingPackage, callingFeatureId, workSource);
Nathan Haroldfa8da0f2018-09-27 18:51:29 -07002807 }
2808
Philip P. Moltmann9797cbb2020-01-07 13:45:00 -08002809 private void requestCellInfoUpdateInternal(int subId, ICellInfoCallback cb,
2810 String callingPackage, String callingFeatureId, WorkSource workSource) {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08002811 mApp.getSystemService(AppOpsManager.class)
Nathan Haroldfa8da0f2018-09-27 18:51:29 -07002812 .checkPackage(Binder.getCallingUid(), callingPackage);
Hall Liuf19c44f2018-11-27 14:38:17 -08002813
2814 LocationAccessPolicy.LocationPermissionResult locationResult =
2815 LocationAccessPolicy.checkLocationPermission(mApp,
2816 new LocationAccessPolicy.LocationPermissionQuery.Builder()
2817 .setCallingPackage(callingPackage)
Philip P. Moltmann9797cbb2020-01-07 13:45:00 -08002818 .setCallingFeatureId(callingFeatureId)
Hall Liuf19c44f2018-11-27 14:38:17 -08002819 .setCallingPid(Binder.getCallingPid())
2820 .setCallingUid(Binder.getCallingUid())
2821 .setMethod("requestCellInfoUpdate")
Hall Liuaa4283b2020-05-21 17:09:35 -07002822 .setMinSdkVersionForCoarse(Build.VERSION_CODES.BASE)
2823 .setMinSdkVersionForFine(Build.VERSION_CODES.BASE)
Hall Liuf19c44f2018-11-27 14:38:17 -08002824 .build());
2825 switch (locationResult) {
2826 case DENIED_HARD:
Hall Liuaa4283b2020-05-21 17:09:35 -07002827 if (getTargetSdk(callingPackage) < Build.VERSION_CODES.Q) {
2828 // Safetynet logging for b/154934934
2829 EventLog.writeEvent(0x534e4554, "154934934", Binder.getCallingUid());
2830 }
Hall Liuf19c44f2018-11-27 14:38:17 -08002831 throw new SecurityException("Not allowed to access cell info");
2832 case DENIED_SOFT:
Hall Liuaa4283b2020-05-21 17:09:35 -07002833 if (getTargetSdk(callingPackage) < Build.VERSION_CODES.Q) {
2834 // Safetynet logging for b/154934934
2835 EventLog.writeEvent(0x534e4554, "154934934", Binder.getCallingUid());
2836 }
Nathan Harold5320c422019-05-09 10:26:08 -07002837 try {
2838 cb.onCellInfo(new ArrayList<CellInfo>());
2839 } catch (RemoteException re) {
2840 // Drop without consequences
2841 }
Hall Liuf19c44f2018-11-27 14:38:17 -08002842 return;
Nathan Haroldfa8da0f2018-09-27 18:51:29 -07002843 }
2844
Nathan Harolda939a962019-05-09 10:13:47 -07002845
2846 final Phone phone = getPhoneFromSubId(subId);
Nathan Haroldfa8da0f2018-09-27 18:51:29 -07002847 if (phone == null) throw new IllegalArgumentException("Invalid Subscription Id: " + subId);
2848
2849 sendRequestAsync(CMD_REQUEST_CELL_INFO_UPDATE, cb, phone, workSource);
2850 }
2851
2852 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002853 public void setCellInfoListRate(int rateInMillis) {
Jack Yua8d8cb82017-01-16 10:15:34 -08002854 enforceModifyPermission();
Narayan Kamathf04b5a12018-01-09 11:47:15 +00002855 WorkSource workSource = getWorkSource(Binder.getCallingUid());
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002856
2857 final long identity = Binder.clearCallingIdentity();
2858 try {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08002859 getDefaultPhone().setCellInfoListRate(rateInMillis, workSource);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002860 } finally {
2861 Binder.restoreCallingIdentity(identity);
2862 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002863 }
2864
Shishir Agrawala9f32182016-04-12 12:00:16 -07002865 @Override
Philip P. Moltmann9797cbb2020-01-07 13:45:00 -08002866 public String getImeiForSlot(int slotIndex, String callingPackage, String callingFeatureId) {
Jeff Davidson913390f2018-02-23 17:11:49 -08002867 Phone phone = PhoneFactory.getPhone(slotIndex);
2868 if (phone == null) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08002869 return null;
2870 }
Jeff Davidson913390f2018-02-23 17:11:49 -08002871 int subId = phone.getSubId();
Michael Groover70af6dc2018-10-01 16:23:15 -07002872 if (!TelephonyPermissions.checkCallingOrSelfReadDeviceIdentifiers(mApp, subId,
Philip P. Moltmann9797cbb2020-01-07 13:45:00 -08002873 callingPackage, callingFeatureId, "getImeiForSlot")) {
Jeff Davidson913390f2018-02-23 17:11:49 -08002874 return null;
2875 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002876
2877 final long identity = Binder.clearCallingIdentity();
2878 try {
2879 return phone.getImei();
2880 } finally {
2881 Binder.restoreCallingIdentity(identity);
2882 }
Shishir Agrawala9f32182016-04-12 12:00:16 -07002883 }
2884
2885 @Override
David Kelly5e06a7f2018-03-12 14:10:59 +00002886 public String getTypeAllocationCodeForSlot(int slotIndex) {
2887 Phone phone = PhoneFactory.getPhone(slotIndex);
2888 String tac = null;
2889 if (phone != null) {
2890 String imei = phone.getImei();
2891 tac = imei == null ? null : imei.substring(0, TYPE_ALLOCATION_CODE_LENGTH);
2892 }
2893 return tac;
2894 }
2895
2896 @Override
Philip P. Moltmann9797cbb2020-01-07 13:45:00 -08002897 public String getMeidForSlot(int slotIndex, String callingPackage, String callingFeatureId) {
Jeff Davidson913390f2018-02-23 17:11:49 -08002898 Phone phone = PhoneFactory.getPhone(slotIndex);
2899 if (phone == null) {
Jack Yu2af8d712017-03-15 17:14:14 -07002900 return null;
2901 }
Nazanin Bakhshif782e562018-12-11 15:15:39 -08002902
Jeff Davidson913390f2018-02-23 17:11:49 -08002903 int subId = phone.getSubId();
Michael Groover70af6dc2018-10-01 16:23:15 -07002904 if (!TelephonyPermissions.checkCallingOrSelfReadDeviceIdentifiers(mApp, subId,
Philip P. Moltmann9797cbb2020-01-07 13:45:00 -08002905 callingPackage, callingFeatureId, "getMeidForSlot")) {
Jeff Davidson913390f2018-02-23 17:11:49 -08002906 return null;
2907 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002908
2909 final long identity = Binder.clearCallingIdentity();
2910 try {
2911 return phone.getMeid();
2912 } finally {
2913 Binder.restoreCallingIdentity(identity);
2914 }
Jack Yu2af8d712017-03-15 17:14:14 -07002915 }
2916
2917 @Override
David Kelly5e06a7f2018-03-12 14:10:59 +00002918 public String getManufacturerCodeForSlot(int slotIndex) {
2919 Phone phone = PhoneFactory.getPhone(slotIndex);
2920 String manufacturerCode = null;
2921 if (phone != null) {
2922 String meid = phone.getMeid();
2923 manufacturerCode = meid == null ? null : meid.substring(0, MANUFACTURER_CODE_LENGTH);
2924 }
2925 return manufacturerCode;
2926 }
2927
2928 @Override
Philip P. Moltmann9797cbb2020-01-07 13:45:00 -08002929 public String getDeviceSoftwareVersionForSlot(int slotIndex, String callingPackage,
2930 String callingFeatureId) {
Jeff Davidson913390f2018-02-23 17:11:49 -08002931 Phone phone = PhoneFactory.getPhone(slotIndex);
2932 if (phone == null) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08002933 return null;
2934 }
Jeff Davidson913390f2018-02-23 17:11:49 -08002935 int subId = phone.getSubId();
2936 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
Philip P. Moltmann9797cbb2020-01-07 13:45:00 -08002937 mApp, subId, callingPackage, callingFeatureId,
2938 "getDeviceSoftwareVersionForSlot")) {
Jeff Davidson913390f2018-02-23 17:11:49 -08002939 return null;
2940 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002941
2942 final long identity = Binder.clearCallingIdentity();
2943 try {
2944 return phone.getDeviceSvn();
2945 } finally {
2946 Binder.restoreCallingIdentity(identity);
2947 }
Shishir Agrawala9f32182016-04-12 12:00:16 -07002948 }
2949
fionaxu43304da2017-11-27 22:51:16 -08002950 @Override
2951 public int getSubscriptionCarrierId(int subId) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002952 final long identity = Binder.clearCallingIdentity();
2953 try {
2954 final Phone phone = getPhone(subId);
2955 return phone == null ? TelephonyManager.UNKNOWN_CARRIER_ID : phone.getCarrierId();
2956 } finally {
2957 Binder.restoreCallingIdentity(identity);
2958 }
fionaxu43304da2017-11-27 22:51:16 -08002959 }
2960
2961 @Override
2962 public String getSubscriptionCarrierName(int subId) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002963 final long identity = Binder.clearCallingIdentity();
2964 try {
2965 final Phone phone = getPhone(subId);
2966 return phone == null ? null : phone.getCarrierName();
2967 } finally {
2968 Binder.restoreCallingIdentity(identity);
2969 }
fionaxu43304da2017-11-27 22:51:16 -08002970 }
2971
calvinpanffe225e2018-11-01 19:43:06 +08002972 @Override
chen xu0026ca62019-03-06 15:28:50 -08002973 public int getSubscriptionSpecificCarrierId(int subId) {
chen xu25637222018-11-04 17:17:00 -08002974 final long identity = Binder.clearCallingIdentity();
2975 try {
2976 final Phone phone = getPhone(subId);
2977 return phone == null ? TelephonyManager.UNKNOWN_CARRIER_ID
chen xu0026ca62019-03-06 15:28:50 -08002978 : phone.getSpecificCarrierId();
chen xu25637222018-11-04 17:17:00 -08002979 } finally {
2980 Binder.restoreCallingIdentity(identity);
2981 }
2982 }
2983
2984 @Override
chen xu0026ca62019-03-06 15:28:50 -08002985 public String getSubscriptionSpecificCarrierName(int subId) {
chen xu25637222018-11-04 17:17:00 -08002986 final long identity = Binder.clearCallingIdentity();
2987 try {
2988 final Phone phone = getPhone(subId);
chen xu0026ca62019-03-06 15:28:50 -08002989 return phone == null ? null : phone.getSpecificCarrierName();
chen xu25637222018-11-04 17:17:00 -08002990 } finally {
2991 Binder.restoreCallingIdentity(identity);
2992 }
2993 }
2994
chen xu651eec72018-11-11 19:03:44 -08002995 @Override
chen xu864e11c2018-12-06 22:10:03 -08002996 public int getCarrierIdFromMccMnc(int slotIndex, String mccmnc, boolean isSubscriptionMccMnc) {
2997 if (!isSubscriptionMccMnc) {
2998 enforceReadPrivilegedPermission("getCarrierIdFromMccMnc");
2999 }
chen xu651eec72018-11-11 19:03:44 -08003000 final Phone phone = PhoneFactory.getPhone(slotIndex);
3001 if (phone == null) {
3002 return TelephonyManager.UNKNOWN_CARRIER_ID;
3003 }
3004 final long identity = Binder.clearCallingIdentity();
3005 try {
3006 return CarrierResolver.getCarrierIdFromMccMnc(phone.getContext(), mccmnc);
3007 } finally {
3008 Binder.restoreCallingIdentity(identity);
3009 }
3010 }
3011
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003012 //
3013 // Internal helper methods.
3014 //
3015
Sanket Padaweee13a9b2016-03-08 17:30:28 -08003016 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003017 * Make sure the caller has the MODIFY_PHONE_STATE permission.
3018 *
3019 * @throws SecurityException if the caller does not have the required permission
3020 */
3021 private void enforceModifyPermission() {
3022 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.MODIFY_PHONE_STATE, null);
3023 }
3024
Shuo Qian5bac1ee2020-01-16 20:51:11 -08003025 /**
3026 * Make sure the caller is system.
3027 *
3028 * @throws SecurityException if the caller is not system.
3029 */
3030 private void enforceSystemCaller() {
3031 if (Binder.getCallingUid() != Process.SYSTEM_UID) {
3032 throw new SecurityException("Caller must be system");
3033 }
3034 }
3035
Shuo Qianf2b2df42019-11-13 17:43:31 -08003036 private void enforceActiveEmergencySessionPermission() {
3037 mApp.enforceCallingOrSelfPermission(
3038 android.Manifest.permission.READ_ACTIVE_EMERGENCY_SESSION, null);
3039 }
3040
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003041 /**
3042 * Make sure the caller has the CALL_PHONE permission.
3043 *
3044 * @throws SecurityException if the caller does not have the required permission
3045 */
3046 private void enforceCallPermission() {
3047 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.CALL_PHONE, null);
3048 }
3049
paulhu423b5f22019-08-23 19:17:33 +08003050 private void enforceSettingsPermission() {
3051 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.NETWORK_SETTINGS, null);
Stuart Scott8eef64f2015-04-08 15:13:54 -07003052 }
3053
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003054 private String createTelUrl(String number) {
3055 if (TextUtils.isEmpty(number)) {
3056 return null;
3057 }
3058
Jake Hambye994d462014-02-03 13:10:13 -08003059 return "tel:" + number;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003060 }
3061
Ihab Awadf9e92732013-12-05 18:02:52 -08003062 private static void log(String msg) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003063 Log.d(LOG_TAG, "[PhoneIntfMgr] " + msg);
3064 }
3065
Naveen Kalla1fd79bd2014-08-08 00:48:59 -07003066 private static void logv(String msg) {
3067 Log.v(LOG_TAG, "[PhoneIntfMgr] " + msg);
3068 }
3069
Ihab Awadf9e92732013-12-05 18:02:52 -08003070 private static void loge(String msg) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003071 Log.e(LOG_TAG, "[PhoneIntfMgr] " + msg);
3072 }
3073
Robert Greenwalt36b23af2015-07-06 17:59:14 -07003074 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003075 public int getActivePhoneType() {
Shishir Agrawala9f32182016-04-12 12:00:16 -07003076 return getActivePhoneTypeForSlot(getSlotForDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07003077 }
3078
Sanket Padawe356d7632015-06-22 14:03:32 -07003079 @Override
Sanket Padawe13bac7b2017-03-20 15:04:47 -07003080 public int getActivePhoneTypeForSlot(int slotIndex) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003081 final long identity = Binder.clearCallingIdentity();
3082 try {
3083 final Phone phone = PhoneFactory.getPhone(slotIndex);
3084 if (phone == null) {
3085 return PhoneConstants.PHONE_TYPE_NONE;
3086 } else {
3087 return phone.getPhoneType();
3088 }
3089 } finally {
3090 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07003091 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003092 }
3093
3094 /**
3095 * Returns the CDMA ERI icon index to display
3096 */
Robert Greenwalt36b23af2015-07-06 17:59:14 -07003097 @Override
Philip P. Moltmann9797cbb2020-01-07 13:45:00 -08003098 public int getCdmaEriIconIndex(String callingPackage, String callingFeatureId) {
3099 return getCdmaEriIconIndexForSubscriber(getDefaultSubscription(), callingPackage,
3100 callingFeatureId);
Wink Saville36469e72014-06-11 15:17:00 -07003101 }
3102
Sanket Padawe356d7632015-06-22 14:03:32 -07003103 @Override
Philip P. Moltmann9797cbb2020-01-07 13:45:00 -08003104 public int getCdmaEriIconIndexForSubscriber(int subId, String callingPackage,
3105 String callingFeatureId) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08003106 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
Philip P. Moltmann9797cbb2020-01-07 13:45:00 -08003107 mApp, subId, callingPackage, callingFeatureId,
3108 "getCdmaEriIconIndexForSubscriber")) {
Robert Greenwalt36b23af2015-07-06 17:59:14 -07003109 return -1;
3110 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003111
3112 final long identity = Binder.clearCallingIdentity();
3113 try {
3114 final Phone phone = getPhone(subId);
3115 if (phone != null) {
3116 return phone.getCdmaEriIconIndex();
3117 } else {
3118 return -1;
3119 }
3120 } finally {
3121 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07003122 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003123 }
3124
3125 /**
3126 * Returns the CDMA ERI icon mode,
3127 * 0 - ON
3128 * 1 - FLASHING
3129 */
Robert Greenwalt36b23af2015-07-06 17:59:14 -07003130 @Override
Philip P. Moltmann9797cbb2020-01-07 13:45:00 -08003131 public int getCdmaEriIconMode(String callingPackage, String callingFeatureId) {
3132 return getCdmaEriIconModeForSubscriber(getDefaultSubscription(), callingPackage,
3133 callingFeatureId);
Wink Saville36469e72014-06-11 15:17:00 -07003134 }
3135
Sanket Padawe356d7632015-06-22 14:03:32 -07003136 @Override
Philip P. Moltmann9797cbb2020-01-07 13:45:00 -08003137 public int getCdmaEriIconModeForSubscriber(int subId, String callingPackage,
3138 String callingFeatureId) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08003139 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
Philip P. Moltmann9797cbb2020-01-07 13:45:00 -08003140 mApp, subId, callingPackage, callingFeatureId,
3141 "getCdmaEriIconModeForSubscriber")) {
Robert Greenwalt36b23af2015-07-06 17:59:14 -07003142 return -1;
3143 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003144
3145 final long identity = Binder.clearCallingIdentity();
3146 try {
3147 final Phone phone = getPhone(subId);
3148 if (phone != null) {
3149 return phone.getCdmaEriIconMode();
3150 } else {
3151 return -1;
3152 }
3153 } finally {
3154 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07003155 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003156 }
3157
3158 /**
3159 * Returns the CDMA ERI text,
3160 */
Robert Greenwalt36b23af2015-07-06 17:59:14 -07003161 @Override
Philip P. Moltmann9797cbb2020-01-07 13:45:00 -08003162 public String getCdmaEriText(String callingPackage, String callingFeatureId) {
3163 return getCdmaEriTextForSubscriber(getDefaultSubscription(), callingPackage,
3164 callingFeatureId);
Wink Saville36469e72014-06-11 15:17:00 -07003165 }
3166
Sanket Padawe356d7632015-06-22 14:03:32 -07003167 @Override
Philip P. Moltmann9797cbb2020-01-07 13:45:00 -08003168 public String getCdmaEriTextForSubscriber(int subId, String callingPackage,
3169 String callingFeatureId) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08003170 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
Philip P. Moltmann9797cbb2020-01-07 13:45:00 -08003171 mApp, subId, callingPackage, callingFeatureId,
3172 "getCdmaEriIconTextForSubscriber")) {
Robert Greenwalt36b23af2015-07-06 17:59:14 -07003173 return null;
3174 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003175
3176 final long identity = Binder.clearCallingIdentity();
3177 try {
3178 final Phone phone = getPhone(subId);
3179 if (phone != null) {
3180 return phone.getCdmaEriText();
3181 } else {
3182 return null;
3183 }
3184 } finally {
3185 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07003186 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003187 }
3188
3189 /**
Junda Liuca05d5d2014-08-14 22:36:34 -07003190 * Returns the CDMA MDN.
3191 */
Sanket Padawe356d7632015-06-22 14:03:32 -07003192 @Override
Wink Savilleb564aae2014-10-23 10:18:09 -07003193 public String getCdmaMdn(int subId) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08003194 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
3195 mApp, subId, "getCdmaMdn");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003196
3197 final long identity = Binder.clearCallingIdentity();
3198 try {
3199 final Phone phone = getPhone(subId);
Nazanin Bakhshif782e562018-12-11 15:15:39 -08003200 if (phone != null && phone.getPhoneType() == PhoneConstants.PHONE_TYPE_CDMA) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003201 return phone.getLine1Number();
3202 } else {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08003203 loge("getCdmaMdn: no phone found. Invalid subId: " + subId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003204 return null;
3205 }
3206 } finally {
3207 Binder.restoreCallingIdentity(identity);
Junda Liuca05d5d2014-08-14 22:36:34 -07003208 }
3209 }
3210
3211 /**
3212 * Returns the CDMA MIN.
3213 */
Sanket Padawe356d7632015-06-22 14:03:32 -07003214 @Override
Wink Savilleb564aae2014-10-23 10:18:09 -07003215 public String getCdmaMin(int subId) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08003216 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
3217 mApp, subId, "getCdmaMin");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003218
3219 final long identity = Binder.clearCallingIdentity();
3220 try {
3221 final Phone phone = getPhone(subId);
3222 if (phone != null && phone.getPhoneType() == PhoneConstants.PHONE_TYPE_CDMA) {
3223 return phone.getCdmaMin();
3224 } else {
3225 return null;
3226 }
3227 } finally {
3228 Binder.restoreCallingIdentity(identity);
Junda Liuca05d5d2014-08-14 22:36:34 -07003229 }
3230 }
3231
Hall Liud892bec2018-11-30 14:51:45 -08003232 @Override
3233 public void requestNumberVerification(PhoneNumberRange range, long timeoutMillis,
3234 INumberVerificationCallback callback, String callingPackage) {
3235 if (mApp.checkCallingOrSelfPermission(android.Manifest.permission.MODIFY_PHONE_STATE)
3236 != PERMISSION_GRANTED) {
3237 throw new SecurityException("Caller must hold the MODIFY_PHONE_STATE permission");
3238 }
3239 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
3240
3241 String authorizedPackage = NumberVerificationManager.getAuthorizedPackage(mApp);
3242 if (!TextUtils.equals(callingPackage, authorizedPackage)) {
3243 throw new SecurityException("Calling package must be configured in the device config");
3244 }
3245
3246 if (range == null) {
3247 throw new NullPointerException("Range must be non-null");
3248 }
3249
3250 timeoutMillis = Math.min(timeoutMillis,
Hall Liubd069e32019-02-28 18:56:30 -08003251 TelephonyManager.getMaxNumberVerificationTimeoutMillis());
Hall Liud892bec2018-11-30 14:51:45 -08003252
3253 NumberVerificationManager.getInstance().requestVerification(range, callback, timeoutMillis);
3254 }
3255
Junda Liuca05d5d2014-08-14 22:36:34 -07003256 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003257 * Returns true if CDMA provisioning needs to run.
3258 */
3259 public boolean needsOtaServiceProvisioning() {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003260 final long identity = Binder.clearCallingIdentity();
3261 try {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08003262 return getDefaultPhone().needsOtaServiceProvisioning();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003263 } finally {
3264 Binder.restoreCallingIdentity(identity);
3265 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003266 }
3267
3268 /**
Shishir Agrawal76d5da92014-11-09 16:17:25 -08003269 * Sets the voice mail number of a given subId.
3270 */
3271 @Override
3272 public boolean setVoiceMailNumber(int subId, String alphaTag, String number) {
Shuo Qian6d927452019-12-05 11:40:37 -08003273 TelephonyPermissions.enforceCallingOrSelfCarrierPrivilege(
3274 mApp, subId, "setVoiceMailNumber");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003275
3276 final long identity = Binder.clearCallingIdentity();
3277 try {
3278 Boolean success = (Boolean) sendRequest(CMD_SET_VOICEMAIL_NUMBER,
3279 new Pair<String, String>(alphaTag, number), new Integer(subId));
3280 return success;
3281 } finally {
3282 Binder.restoreCallingIdentity(identity);
3283 }
Shishir Agrawal76d5da92014-11-09 16:17:25 -08003284 }
3285
Ta-wei Yen87c49842016-05-13 21:19:52 -07003286 @Override
Ta-wei Yenc9df0432017-04-17 17:09:07 -07003287 public Bundle getVisualVoicemailSettings(String callingPackage, int subId) {
3288 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
Tyler Gunn77ee9382019-10-31 13:08:23 -07003289 TelecomManager tm = mApp.getSystemService(TelecomManager.class);
3290 String systemDialer = tm.getSystemDialerPackage();
Ta-wei Yenc9df0432017-04-17 17:09:07 -07003291 if (!TextUtils.equals(callingPackage, systemDialer)) {
3292 throw new SecurityException("caller must be system dialer");
3293 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003294
3295 final long identity = Binder.clearCallingIdentity();
3296 try {
3297 PhoneAccountHandle phoneAccountHandle = PhoneAccountHandleConverter.fromSubId(subId);
3298 if (phoneAccountHandle == null) {
3299 return null;
3300 }
Nazanin Bakhshif782e562018-12-11 15:15:39 -08003301 return VisualVoicemailSettingsUtil.dump(mApp, phoneAccountHandle);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003302 } finally {
3303 Binder.restoreCallingIdentity(identity);
Ta-wei Yenc9df0432017-04-17 17:09:07 -07003304 }
Ta-wei Yenc9df0432017-04-17 17:09:07 -07003305 }
3306
3307 @Override
Philip P. Moltmann9797cbb2020-01-07 13:45:00 -08003308 public String getVisualVoicemailPackageName(String callingPackage, String callingFeatureId,
3309 int subId) {
Ta-wei Yendca928f2017-01-10 16:17:08 -08003310 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
Jeff Davidson7e17e312018-02-13 18:17:36 -08003311 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
Philip P. Moltmann9797cbb2020-01-07 13:45:00 -08003312 mApp, subId, callingPackage, callingFeatureId,
3313 "getVisualVoicemailPackageName")) {
Ta-wei Yendca928f2017-01-10 16:17:08 -08003314 return null;
3315 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003316
Jeff Davidsona8e4e242018-03-15 17:16:18 -07003317 final long identity = Binder.clearCallingIdentity();
3318 try {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08003319 return RemoteVvmTaskManager.getRemotePackage(mApp, subId).getPackageName();
Jeff Davidsona8e4e242018-03-15 17:16:18 -07003320 } finally {
3321 Binder.restoreCallingIdentity(identity);
3322 }
Ta-wei Yendca928f2017-01-10 16:17:08 -08003323 }
3324
3325 @Override
Ta-wei Yenb6929602016-05-24 15:48:27 -07003326 public void enableVisualVoicemailSmsFilter(String callingPackage, int subId,
3327 VisualVoicemailSmsFilterSettings settings) {
3328 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003329
3330 final long identity = Binder.clearCallingIdentity();
3331 try {
3332 VisualVoicemailSmsFilterConfig.enableVisualVoicemailSmsFilter(
Nazanin Bakhshif782e562018-12-11 15:15:39 -08003333 mApp, callingPackage, subId, settings);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003334 } finally {
3335 Binder.restoreCallingIdentity(identity);
3336 }
Ta-wei Yen87c49842016-05-13 21:19:52 -07003337 }
3338
3339 @Override
Ta-wei Yenb6929602016-05-24 15:48:27 -07003340 public void disableVisualVoicemailSmsFilter(String callingPackage, int subId) {
3341 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003342
3343 final long identity = Binder.clearCallingIdentity();
3344 try {
3345 VisualVoicemailSmsFilterConfig.disableVisualVoicemailSmsFilter(
Nazanin Bakhshif782e562018-12-11 15:15:39 -08003346 mApp, callingPackage, subId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003347 } finally {
3348 Binder.restoreCallingIdentity(identity);
3349 }
Ta-wei Yen87c49842016-05-13 21:19:52 -07003350 }
3351
3352 @Override
Ta-wei Yenb6929602016-05-24 15:48:27 -07003353 public VisualVoicemailSmsFilterSettings getVisualVoicemailSmsFilterSettings(
3354 String callingPackage, int subId) {
3355 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003356
3357 final long identity = Binder.clearCallingIdentity();
3358 try {
3359 return VisualVoicemailSmsFilterConfig.getVisualVoicemailSmsFilterSettings(
Nazanin Bakhshif782e562018-12-11 15:15:39 -08003360 mApp, callingPackage, subId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003361 } finally {
3362 Binder.restoreCallingIdentity(identity);
3363 }
Ta-wei Yen87c49842016-05-13 21:19:52 -07003364 }
3365
3366 @Override
Ta-wei Yen30a69c82016-12-27 14:52:32 -08003367 public VisualVoicemailSmsFilterSettings getActiveVisualVoicemailSmsFilterSettings(int subId) {
Brad Ebinger35c841c2018-10-01 10:40:55 -07003368 enforceReadPrivilegedPermission("getActiveVisualVoicemailSmsFilterSettings");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003369
3370 final long identity = Binder.clearCallingIdentity();
3371 try {
3372 return VisualVoicemailSmsFilterConfig.getActiveVisualVoicemailSmsFilterSettings(
Nazanin Bakhshif782e562018-12-11 15:15:39 -08003373 mApp, subId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003374 } finally {
3375 Binder.restoreCallingIdentity(identity);
3376 }
Ta-wei Yen30a69c82016-12-27 14:52:32 -08003377 }
3378
3379 @Override
Philip P. Moltmann2f6f8ce2020-03-18 18:17:02 -07003380 public void sendVisualVoicemailSmsForSubscriber(String callingPackage,
3381 String callingAttributionTag, int subId, String number, int port, String text,
3382 PendingIntent sentIntent) {
Ta-wei Yen30a69c82016-12-27 14:52:32 -08003383 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
Ta-wei Yen527a9c02017-01-06 15:29:25 -08003384 enforceVisualVoicemailPackage(callingPackage, subId);
Ta-wei Yen30a69c82016-12-27 14:52:32 -08003385 enforceSendSmsPermission();
Amit Mahajandccb3f12019-05-13 13:48:32 -07003386 SmsController smsController = PhoneFactory.getSmsController();
Philip P. Moltmann2f6f8ce2020-03-18 18:17:02 -07003387 smsController.sendVisualVoicemailSmsForSubscriber(callingPackage, callingAttributionTag,
3388 subId, number, port, text, sentIntent);
Ta-wei Yen87c49842016-05-13 21:19:52 -07003389 }
Amit Mahajandccb3f12019-05-13 13:48:32 -07003390
Shishir Agrawal76d5da92014-11-09 16:17:25 -08003391 /**
fionaxu0152e512016-11-14 13:36:14 -08003392 * Sets the voice activation state of a given subId.
3393 */
3394 @Override
3395 public void setVoiceActivationState(int subId, int activationState) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08003396 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
3397 mApp, subId, "setVoiceActivationState");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003398
3399 final long identity = Binder.clearCallingIdentity();
3400 try {
3401 final Phone phone = getPhone(subId);
3402 if (phone != null) {
3403 phone.setVoiceActivationState(activationState);
3404 } else {
3405 loge("setVoiceActivationState fails with invalid subId: " + subId);
3406 }
3407 } finally {
3408 Binder.restoreCallingIdentity(identity);
fionaxu0152e512016-11-14 13:36:14 -08003409 }
3410 }
3411
3412 /**
3413 * Sets the data activation state of a given subId.
3414 */
3415 @Override
3416 public void setDataActivationState(int subId, int activationState) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08003417 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
3418 mApp, subId, "setDataActivationState");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003419
3420 final long identity = Binder.clearCallingIdentity();
3421 try {
3422 final Phone phone = getPhone(subId);
3423 if (phone != null) {
3424 phone.setDataActivationState(activationState);
3425 } else {
Taesu Leef8fbed92019-10-07 18:47:02 +09003426 loge("setDataActivationState fails with invalid subId: " + subId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003427 }
3428 } finally {
3429 Binder.restoreCallingIdentity(identity);
fionaxu0152e512016-11-14 13:36:14 -08003430 }
3431 }
3432
3433 /**
3434 * Returns the voice activation state of a given subId.
3435 */
3436 @Override
3437 public int getVoiceActivationState(int subId, String callingPackage) {
Brad Ebinger35c841c2018-10-01 10:40:55 -07003438 enforceReadPrivilegedPermission("getVoiceActivationState");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003439
fionaxu0152e512016-11-14 13:36:14 -08003440 final Phone phone = getPhone(subId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003441 final long identity = Binder.clearCallingIdentity();
3442 try {
3443 if (phone != null) {
3444 return phone.getVoiceActivationState();
3445 } else {
3446 return TelephonyManager.SIM_ACTIVATION_STATE_UNKNOWN;
3447 }
3448 } finally {
3449 Binder.restoreCallingIdentity(identity);
fionaxu0152e512016-11-14 13:36:14 -08003450 }
3451 }
3452
3453 /**
3454 * Returns the data activation state of a given subId.
3455 */
3456 @Override
3457 public int getDataActivationState(int subId, String callingPackage) {
Brad Ebinger35c841c2018-10-01 10:40:55 -07003458 enforceReadPrivilegedPermission("getDataActivationState");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003459
fionaxu0152e512016-11-14 13:36:14 -08003460 final Phone phone = getPhone(subId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003461 final long identity = Binder.clearCallingIdentity();
3462 try {
3463 if (phone != null) {
3464 return phone.getDataActivationState();
3465 } else {
3466 return TelephonyManager.SIM_ACTIVATION_STATE_UNKNOWN;
3467 }
3468 } finally {
3469 Binder.restoreCallingIdentity(identity);
fionaxu0152e512016-11-14 13:36:14 -08003470 }
3471 }
3472
3473 /**
Wink Saville36469e72014-06-11 15:17:00 -07003474 * Returns the unread count of voicemails for a subId
3475 */
Sanket Padawe356d7632015-06-22 14:03:32 -07003476 @Override
Philip P. Moltmann9797cbb2020-01-07 13:45:00 -08003477 public int getVoiceMessageCountForSubscriber(int subId, String callingPackage,
3478 String callingFeatureId) {
Brad Ebingerf7664ba2018-11-29 12:43:38 -08003479 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
Philip P. Moltmann9797cbb2020-01-07 13:45:00 -08003480 mApp, subId, callingPackage, callingFeatureId,
3481 "getVoiceMessageCountForSubscriber")) {
Brad Ebingerf7664ba2018-11-29 12:43:38 -08003482 return 0;
3483 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003484 final long identity = Binder.clearCallingIdentity();
3485 try {
3486 final Phone phone = getPhone(subId);
3487 if (phone != null) {
3488 return phone.getVoiceMessageCount();
3489 } else {
3490 return 0;
3491 }
3492 } finally {
3493 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07003494 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003495 }
3496
3497 /**
pkanwar8a4dcfb2017-01-19 13:43:16 -08003498 * returns true, if the device is in a state where both voice and data
3499 * are supported simultaneously. This can change based on location or network condition.
3500 */
3501 @Override
3502 public boolean isConcurrentVoiceAndDataAllowed(int subId) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003503 final long identity = Binder.clearCallingIdentity();
3504 try {
3505 final Phone phone = getPhone(subId);
3506 return (phone == null ? false : phone.isConcurrentVoiceAndDataAllowed());
3507 } finally {
3508 Binder.restoreCallingIdentity(identity);
3509 }
pkanwar8a4dcfb2017-01-19 13:43:16 -08003510 }
3511
3512 /**
fionaxu235cc5e2017-03-06 22:25:57 -08003513 * Send the dialer code if called from the current default dialer or the caller has
3514 * carrier privilege.
3515 * @param inputCode The dialer code to send
3516 */
3517 @Override
3518 public void sendDialerSpecialCode(String callingPackage, String inputCode) {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08003519 final Phone defaultPhone = getDefaultPhone();
fionaxu235cc5e2017-03-06 22:25:57 -08003520 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
Tyler Gunn77ee9382019-10-31 13:08:23 -07003521 TelecomManager tm = defaultPhone.getContext().getSystemService(TelecomManager.class);
3522 String defaultDialer = tm.getDefaultDialerPackage();
fionaxu235cc5e2017-03-06 22:25:57 -08003523 if (!TextUtils.equals(callingPackage, defaultDialer)) {
Shuo Qian6d927452019-12-05 11:40:37 -08003524 TelephonyPermissions.enforceCallingOrSelfCarrierPrivilege(mApp,
Jeff Davidson7e17e312018-02-13 18:17:36 -08003525 getDefaultSubscription(), "sendDialerSpecialCode");
fionaxu235cc5e2017-03-06 22:25:57 -08003526 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003527
3528 final long identity = Binder.clearCallingIdentity();
3529 try {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08003530 defaultPhone.sendDialerSpecialCode(inputCode);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003531 } finally {
3532 Binder.restoreCallingIdentity(identity);
3533 }
fionaxu235cc5e2017-03-06 22:25:57 -08003534 }
3535
Pengquan Menga1bb6272018-09-06 09:59:22 -07003536 @Override
3537 public int getNetworkSelectionMode(int subId) {
shilufc958392020-01-20 11:36:01 -08003538 TelephonyPermissions
3539 .enforeceCallingOrSelfReadPrecisePhoneStatePermissionOrCarrierPrivilege(
3540 mApp, subId, "getNetworkSelectionMode");
3541 final long identity = Binder.clearCallingIdentity();
3542 try {
3543 if (!isActiveSubscription(subId)) {
3544 return TelephonyManager.NETWORK_SELECTION_MODE_UNKNOWN;
3545 }
3546 return (int) sendRequest(CMD_GET_NETWORK_SELECTION_MODE, null /* argument */, subId);
3547 } finally {
3548 Binder.restoreCallingIdentity(identity);
Pengquan Menge92a50d2018-09-21 15:54:48 -07003549 }
Pengquan Menga1bb6272018-09-06 09:59:22 -07003550 }
3551
Brad Ebinger35c841c2018-10-01 10:40:55 -07003552 @Override
Brad Ebingerb2b65522019-03-15 13:48:47 -07003553 public boolean isInEmergencySmsMode() {
3554 enforceReadPrivilegedPermission("isInEmergencySmsMode");
3555 final long identity = Binder.clearCallingIdentity();
3556 try {
3557 for (Phone phone : PhoneFactory.getPhones()) {
3558 if (phone.isInEmergencySmsMode()) {
3559 return true;
3560 }
3561 }
3562 } finally {
3563 Binder.restoreCallingIdentity(identity);
3564 }
3565 return false;
3566 }
3567
shilu366312e2019-12-17 09:28:10 -08003568 /**
3569 * Requires carrier privileges or READ_PRECISE_PHONE_STATE permission.
3570 * @param subId The subscription to use to check the configuration.
3571 * @param c The callback that will be used to send the result.
3572 */
Brad Ebingerb2b65522019-03-15 13:48:47 -07003573 @Override
Brad Ebinger9878b0b2018-11-08 17:43:22 -08003574 public void registerImsRegistrationCallback(int subId, IImsRegistrationCallback c)
3575 throws RemoteException {
Rambo Wang37f9c242020-02-10 14:45:28 -08003576 TelephonyPermissions.enforeceCallingOrSelfReadPrecisePhoneStatePermissionOrCarrierPrivilege(
3577 mApp, subId, "registerImsRegistrationCallback");
shilu366312e2019-12-17 09:28:10 -08003578
Brad Ebinger77b832e2019-10-17 17:03:22 -07003579 if (!ImsManager.isImsSupportedOnDevice(mApp)) {
3580 throw new ServiceSpecificException(ImsException.CODE_ERROR_UNSUPPORTED_OPERATION,
3581 "IMS not available on device.");
3582 }
Brad Ebinger35c841c2018-10-01 10:40:55 -07003583 final long token = Binder.clearCallingIdentity();
3584 try {
Brad Ebinger4ae57f92019-01-09 16:51:30 -08003585 // TODO: Refactor to remove ImsManager dependence and query through ImsPhone directly.
Nazanin Bakhshif782e562018-12-11 15:15:39 -08003586 ImsManager.getInstance(mApp, getSlotIndexOrException(subId))
Brad Ebinger35c841c2018-10-01 10:40:55 -07003587 .addRegistrationCallbackForSubscription(c, subId);
Brad Ebinger1ce9c432019-07-16 13:19:44 -07003588 } catch (ImsException e) {
3589 throw new ServiceSpecificException(e.getCode());
Brad Ebinger35c841c2018-10-01 10:40:55 -07003590 } finally {
3591 Binder.restoreCallingIdentity(token);
3592 }
3593 }
3594
shilu366312e2019-12-17 09:28:10 -08003595 /**
3596 * Requires carrier privileges or READ_PRECISE_PHONE_STATE permission.
3597 * @param subId The subscription to use to check the configuration.
3598 * @param c The callback that will be used to send the result.
3599 */
Brad Ebinger35c841c2018-10-01 10:40:55 -07003600 @Override
Brad Ebinger9878b0b2018-11-08 17:43:22 -08003601 public void unregisterImsRegistrationCallback(int subId, IImsRegistrationCallback c) {
Rambo Wang37f9c242020-02-10 14:45:28 -08003602 TelephonyPermissions.enforeceCallingOrSelfReadPrecisePhoneStatePermissionOrCarrierPrivilege(
3603 mApp, subId, "unregisterImsRegistrationCallback");
Brad Ebinger4ae57f92019-01-09 16:51:30 -08003604 if (!SubscriptionManager.isValidSubscriptionId(subId)) {
3605 throw new IllegalArgumentException("Invalid Subscription ID: " + subId);
3606 }
Meng Wangd20ad6b2019-09-19 17:37:13 -07003607 final long token = Binder.clearCallingIdentity();
3608 try {
3609 // TODO: Refactor to remove ImsManager dependence and query through ImsPhone.
3610 ImsManager.getInstance(mApp, getSlotIndexOrException(subId))
3611 .removeRegistrationCallbackForSubscription(c, subId);
3612 } catch (ImsException e) {
3613 Log.i(LOG_TAG, "unregisterImsRegistrationCallback: " + subId
3614 + "is inactive, ignoring unregister.");
3615 // If the subscription is no longer active, just return, since the callback
3616 // will already have been removed internally.
3617 } finally {
3618 Binder.restoreCallingIdentity(token);
3619 }
Brad Ebinger35c841c2018-10-01 10:40:55 -07003620 }
3621
Brad Ebinger774ba362019-10-22 17:36:18 -07003622 /**
3623 * Get the IMS service registration state for the MmTelFeature associated with this sub id.
3624 */
3625 @Override
3626 public void getImsMmTelRegistrationState(int subId, IIntegerConsumer consumer) {
3627 enforceReadPrivilegedPermission("getImsMmTelRegistrationState");
3628 if (!ImsManager.isImsSupportedOnDevice(mApp)) {
3629 throw new ServiceSpecificException(ImsException.CODE_ERROR_UNSUPPORTED_OPERATION,
3630 "IMS not available on device.");
3631 }
3632 final long token = Binder.clearCallingIdentity();
3633 try {
3634 Phone phone = getPhone(subId);
3635 if (phone == null) {
3636 Log.w(LOG_TAG, "getImsMmTelRegistrationState: called with an invalid subscription '"
3637 + subId + "'");
3638 throw new ServiceSpecificException(ImsException.CODE_ERROR_INVALID_SUBSCRIPTION);
3639 }
3640 phone.getImsRegistrationState(regState -> {
3641 try {
3642 consumer.accept((regState == null)
3643 ? RegistrationManager.REGISTRATION_STATE_NOT_REGISTERED : regState);
3644 } catch (RemoteException e) {
3645 // Ignore if the remote process is no longer available to call back.
3646 Log.w(LOG_TAG, "getImsMmTelRegistrationState: callback not available.");
3647 }
3648 });
3649 } finally {
3650 Binder.restoreCallingIdentity(token);
3651 }
3652 }
3653
3654 /**
3655 * Get the transport type for the IMS service registration state.
3656 */
3657 @Override
3658 public void getImsMmTelRegistrationTransportType(int subId, IIntegerConsumer consumer) {
Rambo Wang37f9c242020-02-10 14:45:28 -08003659 TelephonyPermissions.enforeceCallingOrSelfReadPrecisePhoneStatePermissionOrCarrierPrivilege(
3660 mApp, subId, "getImsMmTelRegistrationTransportType");
Brad Ebinger774ba362019-10-22 17:36:18 -07003661 if (!ImsManager.isImsSupportedOnDevice(mApp)) {
3662 throw new ServiceSpecificException(ImsException.CODE_ERROR_UNSUPPORTED_OPERATION,
3663 "IMS not available on device.");
3664 }
3665 final long token = Binder.clearCallingIdentity();
3666 try {
3667 Phone phone = getPhone(subId);
3668 if (phone == null) {
3669 Log.w(LOG_TAG, "getImsMmTelRegistrationState: called with an invalid subscription '"
3670 + subId + "'");
3671 throw new ServiceSpecificException(ImsException.CODE_ERROR_INVALID_SUBSCRIPTION);
3672 }
3673 phone.getImsRegistrationTech(regTech -> {
3674 // Convert registration tech from ImsRegistrationImplBase -> RegistrationManager
3675 int regTechConverted = (regTech == null)
3676 ? ImsRegistrationImplBase.REGISTRATION_TECH_NONE : regTech;
3677 regTechConverted = RegistrationManager.IMS_REG_TO_ACCESS_TYPE_MAP.get(
3678 regTechConverted);
3679 try {
3680 consumer.accept(regTechConverted);
3681 } catch (RemoteException e) {
3682 // Ignore if the remote process is no longer available to call back.
3683 Log.w(LOG_TAG, "getImsMmTelRegistrationState: callback not available.");
3684 }
3685 });
3686 } finally {
3687 Binder.restoreCallingIdentity(token);
3688 }
3689 }
3690
shilu366312e2019-12-17 09:28:10 -08003691 /**
3692 * Requires carrier privileges or READ_PRECISE_PHONE_STATE permission.
3693 * @param subId The subscription to use to check the configuration.
3694 * @param c The callback that will be used to send the result.
3695 */
Brad Ebinger35c841c2018-10-01 10:40:55 -07003696 @Override
Brad Ebinger9878b0b2018-11-08 17:43:22 -08003697 public void registerMmTelCapabilityCallback(int subId, IImsCapabilityCallback c)
3698 throws RemoteException {
Rambo Wang37f9c242020-02-10 14:45:28 -08003699 TelephonyPermissions.enforeceCallingOrSelfReadPrecisePhoneStatePermissionOrCarrierPrivilege(
3700 mApp, subId, "registerMmTelCapabilityCallback");
Brad Ebinger77b832e2019-10-17 17:03:22 -07003701 if (!ImsManager.isImsSupportedOnDevice(mApp)) {
3702 throw new ServiceSpecificException(ImsException.CODE_ERROR_UNSUPPORTED_OPERATION,
3703 "IMS not available on device.");
3704 }
Brad Ebinger35c841c2018-10-01 10:40:55 -07003705 // TODO: Refactor to remove ImsManager dependence and query through ImsPhone directly.
3706 final long token = Binder.clearCallingIdentity();
3707 try {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08003708 ImsManager.getInstance(mApp, getSlotIndexOrException(subId))
Brad Ebinger35c841c2018-10-01 10:40:55 -07003709 .addCapabilitiesCallbackForSubscription(c, subId);
Brad Ebinger1ce9c432019-07-16 13:19:44 -07003710 } catch (ImsException e) {
3711 throw new ServiceSpecificException(e.getCode());
Brad Ebinger35c841c2018-10-01 10:40:55 -07003712 } finally {
3713 Binder.restoreCallingIdentity(token);
3714 }
3715 }
3716
shilu366312e2019-12-17 09:28:10 -08003717 /**
3718 * Requires carrier privileges or READ_PRECISE_PHONE_STATE permission.
3719 * @param subId The subscription to use to check the configuration.
3720 * @param c The callback that will be used to send the result.
3721 */
Brad Ebinger35c841c2018-10-01 10:40:55 -07003722 @Override
Brad Ebinger9878b0b2018-11-08 17:43:22 -08003723 public void unregisterMmTelCapabilityCallback(int subId, IImsCapabilityCallback c) {
Rambo Wang37f9c242020-02-10 14:45:28 -08003724 TelephonyPermissions.enforeceCallingOrSelfReadPrecisePhoneStatePermissionOrCarrierPrivilege(
3725 mApp, subId, "unregisterMmTelCapabilityCallback");
Brad Ebinger4ae57f92019-01-09 16:51:30 -08003726 if (!SubscriptionManager.isValidSubscriptionId(subId)) {
3727 throw new IllegalArgumentException("Invalid Subscription ID: " + subId);
3728 }
Meng Wangd20ad6b2019-09-19 17:37:13 -07003729
3730 final long token = Binder.clearCallingIdentity();
3731 try {
3732 // TODO: Refactor to remove ImsManager dependence and query through ImsPhone.
3733 ImsManager.getInstance(mApp, getSlotIndexOrException(subId))
Brad Ebinger4ae57f92019-01-09 16:51:30 -08003734 .removeCapabilitiesCallbackForSubscription(c, subId);
Meng Wangd20ad6b2019-09-19 17:37:13 -07003735 } catch (ImsException e) {
3736 Log.i(LOG_TAG, "unregisterMmTelCapabilityCallback: " + subId
3737 + "is inactive, ignoring unregister.");
3738 // If the subscription is no longer active, just return, since the callback
3739 // will already have been removed internally.
3740 } finally {
3741 Binder.restoreCallingIdentity(token);
3742 }
Brad Ebinger35c841c2018-10-01 10:40:55 -07003743 }
3744
3745 @Override
Brad Ebinger9878b0b2018-11-08 17:43:22 -08003746 public boolean isCapable(int subId, int capability, int regTech) {
3747 enforceReadPrivilegedPermission("isCapable");
Brad Ebinger35c841c2018-10-01 10:40:55 -07003748 // TODO: Refactor to remove ImsManager dependence and query through ImsPhone directly.
3749 final long token = Binder.clearCallingIdentity();
3750 try {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08003751 return ImsManager.getInstance(mApp,
Brad Ebinger35c841c2018-10-01 10:40:55 -07003752 getSlotIndexOrException(subId)).queryMmTelCapability(capability, regTech);
Brad Ebinger1ce9c432019-07-16 13:19:44 -07003753 } catch (com.android.ims.ImsException e) {
Brad Ebinger35c841c2018-10-01 10:40:55 -07003754 Log.w(LOG_TAG, "IMS isCapable - service unavailable: " + e.getMessage());
3755 return false;
Brad Ebinger1ce9c432019-07-16 13:19:44 -07003756 } catch (ImsException e) {
Brad Ebinger6b5ac222019-02-04 14:36:52 -08003757 Log.i(LOG_TAG, "isCapable: " + subId + " is inactive, returning false.");
3758 return false;
Brad Ebinger35c841c2018-10-01 10:40:55 -07003759 } finally {
3760 Binder.restoreCallingIdentity(token);
3761 }
3762 }
3763
3764 @Override
Brad Ebinger9878b0b2018-11-08 17:43:22 -08003765 public boolean isAvailable(int subId, int capability, int regTech) {
3766 enforceReadPrivilegedPermission("isAvailable");
Brad Ebinger35c841c2018-10-01 10:40:55 -07003767 final long token = Binder.clearCallingIdentity();
3768 try {
3769 Phone phone = getPhone(subId);
3770 if (phone == null) return false;
3771 return phone.isImsCapabilityAvailable(capability, regTech);
Daniel Bright32706d92020-03-11 16:35:39 -07003772 } catch (com.android.ims.ImsException e) {
3773 Log.w(LOG_TAG, "IMS isAvailable - service unavailable: " + e.getMessage());
3774 return false;
Brad Ebinger35c841c2018-10-01 10:40:55 -07003775 } finally {
3776 Binder.restoreCallingIdentity(token);
3777 }
3778 }
3779
Brad Ebinger77b832e2019-10-17 17:03:22 -07003780 /**
3781 * Determines if the MmTel feature capability is supported by the carrier configuration for this
3782 * subscription.
3783 * @param subId The subscription to use to check the configuration.
3784 * @param callback The callback that will be used to send the result.
3785 * @param capability The MmTelFeature capability that will be used to send the result.
3786 * @param transportType The transport type of the MmTelFeature capability.
3787 */
3788 @Override
3789 public void isMmTelCapabilitySupported(int subId, IIntegerConsumer callback, int capability,
3790 int transportType) {
3791 enforceReadPrivilegedPermission("isMmTelCapabilitySupported");
3792 if (!ImsManager.isImsSupportedOnDevice(mApp)) {
3793 throw new ServiceSpecificException(ImsException.CODE_ERROR_UNSUPPORTED_OPERATION,
3794 "IMS not available on device.");
3795 }
3796 final long token = Binder.clearCallingIdentity();
3797 try {
3798 int slotId = getSlotIndex(subId);
3799 if (slotId <= SubscriptionManager.INVALID_SIM_SLOT_INDEX) {
3800 Log.w(LOG_TAG, "isMmTelCapabilitySupported: called with an inactive subscription '"
3801 + subId + "'");
3802 throw new ServiceSpecificException(ImsException.CODE_ERROR_INVALID_SUBSCRIPTION);
3803 }
3804 ImsManager.getInstance(mApp, slotId).isSupported(capability,
3805 transportType, aBoolean -> {
3806 try {
3807 callback.accept((aBoolean == null) ? 0 : (aBoolean ? 1 : 0));
3808 } catch (RemoteException e) {
3809 Log.w(LOG_TAG, "isMmTelCapabilitySupported: remote caller is not "
3810 + "running. Ignore");
3811 }
3812 });
3813 } finally {
3814 Binder.restoreCallingIdentity(token);
3815 }
3816 }
3817
shilu366312e2019-12-17 09:28:10 -08003818 /**
3819 * Requires carrier privileges or READ_PRECISE_PHONE_STATE permission.
3820 * @param subId The subscription to use to check the configuration.
3821 */
Brad Ebinger35c841c2018-10-01 10:40:55 -07003822 @Override
3823 public boolean isAdvancedCallingSettingEnabled(int subId) {
Rambo Wang37f9c242020-02-10 14:45:28 -08003824 TelephonyPermissions.enforeceCallingOrSelfReadPrecisePhoneStatePermissionOrCarrierPrivilege(
3825 mApp, subId, "isAdvancedCallingSettingEnabled");
shilu366312e2019-12-17 09:28:10 -08003826
Brad Ebinger35c841c2018-10-01 10:40:55 -07003827 // TODO: Refactor to remove ImsManager dependence and query through ImsPhone directly.
3828 final long token = Binder.clearCallingIdentity();
3829 try {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08003830 return ImsManager.getInstance(mApp,
Brad Ebinger35c841c2018-10-01 10:40:55 -07003831 getSlotIndexOrException(subId)).isEnhanced4gLteModeSettingEnabledByUser();
Brad Ebinger1ce9c432019-07-16 13:19:44 -07003832 } catch (ImsException e) {
3833 throw new ServiceSpecificException(e.getCode());
Brad Ebinger35c841c2018-10-01 10:40:55 -07003834 } finally {
3835 Binder.restoreCallingIdentity(token);
3836 }
3837 }
3838
3839 @Override
Brad Ebinger1c162042019-02-21 14:49:10 -08003840 public void setAdvancedCallingSettingEnabled(int subId, boolean isEnabled) {
Brad Ebinger35c841c2018-10-01 10:40:55 -07003841 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(mApp, subId,
Brad Ebinger1c162042019-02-21 14:49:10 -08003842 "setAdvancedCallingSettingEnabled");
Brad Ebinger35c841c2018-10-01 10:40:55 -07003843 final long identity = Binder.clearCallingIdentity();
3844 try {
3845 // TODO: Refactor to remove ImsManager dependence and query through ImsPhone directly.
Nazanin Bakhshif782e562018-12-11 15:15:39 -08003846 ImsManager.getInstance(mApp,
Brad Ebinger35c841c2018-10-01 10:40:55 -07003847 getSlotIndexOrException(subId)).setEnhanced4gLteModeSetting(isEnabled);
Brad Ebinger1ce9c432019-07-16 13:19:44 -07003848 } catch (ImsException e) {
3849 throw new ServiceSpecificException(e.getCode());
Brad Ebinger35c841c2018-10-01 10:40:55 -07003850 } finally {
3851 Binder.restoreCallingIdentity(identity);
3852 }
3853 }
3854
shilu366312e2019-12-17 09:28:10 -08003855 /**
3856 * Requires carrier privileges or READ_PRECISE_PHONE_STATE permission.
3857 * @param subId The subscription to use to check the configuration.
3858 */
Brad Ebinger35c841c2018-10-01 10:40:55 -07003859 @Override
Brad Ebinger9878b0b2018-11-08 17:43:22 -08003860 public boolean isVtSettingEnabled(int subId) {
Rambo Wang37f9c242020-02-10 14:45:28 -08003861 TelephonyPermissions.enforeceCallingOrSelfReadPrecisePhoneStatePermissionOrCarrierPrivilege(
3862 mApp, subId, "isVtSettingEnabled");
Brad Ebinger35c841c2018-10-01 10:40:55 -07003863 final long identity = Binder.clearCallingIdentity();
3864 try {
3865 // TODO: Refactor to remove ImsManager dependence and query through ImsPhone directly.
Brad Ebinger1ce9c432019-07-16 13:19:44 -07003866 return ImsManager.getInstance(mApp, getSlotIndexOrException(subId)).isVtEnabledByUser();
3867 } catch (ImsException e) {
3868 throw new ServiceSpecificException(e.getCode());
Brad Ebinger35c841c2018-10-01 10:40:55 -07003869 } finally {
3870 Binder.restoreCallingIdentity(identity);
3871 }
3872 }
3873
3874 @Override
Brad Ebinger1c162042019-02-21 14:49:10 -08003875 public void setVtSettingEnabled(int subId, boolean isEnabled) {
Brad Ebinger35c841c2018-10-01 10:40:55 -07003876 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(mApp, subId,
Brad Ebinger1c162042019-02-21 14:49:10 -08003877 "setVtSettingEnabled");
Brad Ebinger35c841c2018-10-01 10:40:55 -07003878 final long identity = Binder.clearCallingIdentity();
3879 try {
3880 // TODO: Refactor to remove ImsManager dependence and query through ImsPhone directly.
Nazanin Bakhshif782e562018-12-11 15:15:39 -08003881 ImsManager.getInstance(mApp, getSlotIndexOrException(subId)).setVtSetting(isEnabled);
Brad Ebinger1ce9c432019-07-16 13:19:44 -07003882 } catch (ImsException e) {
3883 throw new ServiceSpecificException(e.getCode());
Brad Ebinger35c841c2018-10-01 10:40:55 -07003884 } finally {
3885 Binder.restoreCallingIdentity(identity);
3886 }
3887 }
3888
shilu366312e2019-12-17 09:28:10 -08003889 /**
3890 * Requires carrier privileges or READ_PRECISE_PHONE_STATE permission.
3891 * @param subId The subscription to use to check the configuration.
3892 */
Brad Ebinger35c841c2018-10-01 10:40:55 -07003893 @Override
3894 public boolean isVoWiFiSettingEnabled(int subId) {
Rambo Wang37f9c242020-02-10 14:45:28 -08003895 TelephonyPermissions.enforeceCallingOrSelfReadPrecisePhoneStatePermissionOrCarrierPrivilege(
3896 mApp, subId, "isVoWiFiSettingEnabled");
Brad Ebinger35c841c2018-10-01 10:40:55 -07003897 final long identity = Binder.clearCallingIdentity();
3898 try {
3899 // TODO: Refactor to remove ImsManager dependence and query through ImsPhone directly.
Nazanin Bakhshif782e562018-12-11 15:15:39 -08003900 return ImsManager.getInstance(mApp,
Brad Ebinger35c841c2018-10-01 10:40:55 -07003901 getSlotIndexOrException(subId)).isWfcEnabledByUser();
Brad Ebinger1ce9c432019-07-16 13:19:44 -07003902 } catch (ImsException e) {
3903 throw new ServiceSpecificException(e.getCode());
Brad Ebinger35c841c2018-10-01 10:40:55 -07003904 } finally {
3905 Binder.restoreCallingIdentity(identity);
3906 }
3907 }
3908
3909 @Override
Brad Ebinger1c162042019-02-21 14:49:10 -08003910 public void setVoWiFiSettingEnabled(int subId, boolean isEnabled) {
Brad Ebinger35c841c2018-10-01 10:40:55 -07003911 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(mApp, subId,
Brad Ebinger1c162042019-02-21 14:49:10 -08003912 "setVoWiFiSettingEnabled");
Brad Ebinger35c841c2018-10-01 10:40:55 -07003913 final long identity = Binder.clearCallingIdentity();
3914 try {
3915 // TODO: Refactor to remove ImsManager dependence and query through ImsPhone directly.
Nazanin Bakhshif782e562018-12-11 15:15:39 -08003916 ImsManager.getInstance(mApp, getSlotIndexOrException(subId)).setWfcSetting(isEnabled);
Brad Ebinger1ce9c432019-07-16 13:19:44 -07003917 } catch (ImsException e) {
3918 throw new ServiceSpecificException(e.getCode());
Brad Ebinger35c841c2018-10-01 10:40:55 -07003919 } finally {
3920 Binder.restoreCallingIdentity(identity);
3921 }
3922 }
3923
shilu366312e2019-12-17 09:28:10 -08003924 /**
3925 * Requires carrier privileges or READ_PRECISE_PHONE_STATE permission.
3926 * @param subId The subscription to use to check the configuration.
3927 */
Brad Ebinger35c841c2018-10-01 10:40:55 -07003928 @Override
3929 public boolean isVoWiFiRoamingSettingEnabled(int subId) {
Rambo Wang37f9c242020-02-10 14:45:28 -08003930 TelephonyPermissions.enforeceCallingOrSelfReadPrecisePhoneStatePermissionOrCarrierPrivilege(
3931 mApp, subId, "isVoWiFiRoamingSettingEnabled");
Brad Ebinger35c841c2018-10-01 10:40:55 -07003932 final long identity = Binder.clearCallingIdentity();
3933 try {
3934 // TODO: Refactor to remove ImsManager dependence and query through ImsPhone directly.
Nazanin Bakhshif782e562018-12-11 15:15:39 -08003935 return ImsManager.getInstance(mApp,
Brad Ebinger35c841c2018-10-01 10:40:55 -07003936 getSlotIndexOrException(subId)).isWfcRoamingEnabledByUser();
Brad Ebinger1ce9c432019-07-16 13:19:44 -07003937 } catch (ImsException e) {
3938 throw new ServiceSpecificException(e.getCode());
Brad Ebinger35c841c2018-10-01 10:40:55 -07003939 } finally {
3940 Binder.restoreCallingIdentity(identity);
3941 }
3942 }
3943
3944 @Override
Brad Ebinger1c162042019-02-21 14:49:10 -08003945 public void setVoWiFiRoamingSettingEnabled(int subId, boolean isEnabled) {
Brad Ebinger35c841c2018-10-01 10:40:55 -07003946 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(mApp, subId,
Brad Ebinger1c162042019-02-21 14:49:10 -08003947 "setVoWiFiRoamingSettingEnabled");
Brad Ebinger35c841c2018-10-01 10:40:55 -07003948 final long identity = Binder.clearCallingIdentity();
3949 try {
3950 // TODO: Refactor to remove ImsManager dependence and query through ImsPhone directly.
Nazanin Bakhshif782e562018-12-11 15:15:39 -08003951 ImsManager.getInstance(mApp,
Brad Ebinger35c841c2018-10-01 10:40:55 -07003952 getSlotIndexOrException(subId)).setWfcRoamingSetting(isEnabled);
Brad Ebinger1ce9c432019-07-16 13:19:44 -07003953 } catch (ImsException e) {
3954 throw new ServiceSpecificException(e.getCode());
Brad Ebinger35c841c2018-10-01 10:40:55 -07003955 } finally {
3956 Binder.restoreCallingIdentity(identity);
3957 }
3958 }
3959
3960 @Override
3961 public void setVoWiFiNonPersistent(int subId, boolean isCapable, int mode) {
3962 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(mApp, subId,
3963 "setVoWiFiNonPersistent");
3964 final long identity = Binder.clearCallingIdentity();
3965 try {
3966 // TODO: Refactor to remove ImsManager dependence and query through ImsPhone directly.
Nazanin Bakhshif782e562018-12-11 15:15:39 -08003967 ImsManager.getInstance(mApp,
Brad Ebinger2d29c012019-05-07 18:33:46 -07003968 getSlotIndexOrException(subId)).setWfcNonPersistent(isCapable, mode);
Brad Ebinger1ce9c432019-07-16 13:19:44 -07003969 } catch (ImsException e) {
3970 throw new ServiceSpecificException(e.getCode());
Brad Ebinger35c841c2018-10-01 10:40:55 -07003971 } finally {
3972 Binder.restoreCallingIdentity(identity);
3973 }
3974 }
3975
shilu366312e2019-12-17 09:28:10 -08003976 /**
3977 * Requires carrier privileges or READ_PRECISE_PHONE_STATE permission.
3978 * @param subId The subscription to use to check the configuration.
3979 */
Brad Ebinger35c841c2018-10-01 10:40:55 -07003980 @Override
3981 public int getVoWiFiModeSetting(int subId) {
Rambo Wang37f9c242020-02-10 14:45:28 -08003982 TelephonyPermissions.enforeceCallingOrSelfReadPrecisePhoneStatePermissionOrCarrierPrivilege(
3983 mApp, subId, "getVoWiFiModeSetting");
Brad Ebinger35c841c2018-10-01 10:40:55 -07003984 final long identity = Binder.clearCallingIdentity();
3985 try {
3986 // TODO: Refactor to remove ImsManager dependence and query through ImsPhone directly.
Nazanin Bakhshif782e562018-12-11 15:15:39 -08003987 return ImsManager.getInstance(mApp,
Brad Ebinger35c841c2018-10-01 10:40:55 -07003988 getSlotIndexOrException(subId)).getWfcMode(false /*isRoaming*/);
Brad Ebinger1ce9c432019-07-16 13:19:44 -07003989 } catch (ImsException e) {
3990 throw new ServiceSpecificException(e.getCode());
Brad Ebinger35c841c2018-10-01 10:40:55 -07003991 } finally {
3992 Binder.restoreCallingIdentity(identity);
3993 }
3994 }
3995
3996 @Override
3997 public void setVoWiFiModeSetting(int subId, int mode) {
3998 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(mApp, subId,
3999 "setVoWiFiModeSetting");
4000 final long identity = Binder.clearCallingIdentity();
4001 try {
4002 // TODO: Refactor to remove ImsManager dependence and query through ImsPhone directly.
Nazanin Bakhshif782e562018-12-11 15:15:39 -08004003 ImsManager.getInstance(mApp,
Brad Ebinger35c841c2018-10-01 10:40:55 -07004004 getSlotIndexOrException(subId)).setWfcMode(mode, false /*isRoaming*/);
Brad Ebinger1ce9c432019-07-16 13:19:44 -07004005 } catch (ImsException e) {
4006 throw new ServiceSpecificException(e.getCode());
Brad Ebinger35c841c2018-10-01 10:40:55 -07004007 } finally {
4008 Binder.restoreCallingIdentity(identity);
4009 }
4010 }
4011
4012 @Override
4013 public int getVoWiFiRoamingModeSetting(int subId) {
4014 enforceReadPrivilegedPermission("getVoWiFiRoamingModeSetting");
4015 final long identity = Binder.clearCallingIdentity();
4016 try {
4017 // TODO: Refactor to remove ImsManager dependence and query through ImsPhone directly.
Nazanin Bakhshif782e562018-12-11 15:15:39 -08004018 return ImsManager.getInstance(mApp,
Brad Ebinger35c841c2018-10-01 10:40:55 -07004019 getSlotIndexOrException(subId)).getWfcMode(true /*isRoaming*/);
Brad Ebinger1ce9c432019-07-16 13:19:44 -07004020 } catch (ImsException e) {
4021 throw new ServiceSpecificException(e.getCode());
Brad Ebinger35c841c2018-10-01 10:40:55 -07004022 } finally {
4023 Binder.restoreCallingIdentity(identity);
4024 }
4025 }
4026
4027 @Override
4028 public void setVoWiFiRoamingModeSetting(int subId, int mode) {
4029 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(mApp, subId,
4030 "setVoWiFiRoamingModeSetting");
4031 final long identity = Binder.clearCallingIdentity();
4032 try {
4033 // TODO: Refactor to remove ImsManager dependence and query through ImsPhone directly.
Nazanin Bakhshif782e562018-12-11 15:15:39 -08004034 ImsManager.getInstance(mApp,
Brad Ebinger35c841c2018-10-01 10:40:55 -07004035 getSlotIndexOrException(subId)).setWfcMode(mode, true /*isRoaming*/);
Brad Ebinger1ce9c432019-07-16 13:19:44 -07004036 } catch (ImsException e) {
4037 throw new ServiceSpecificException(e.getCode());
Brad Ebinger35c841c2018-10-01 10:40:55 -07004038 } finally {
4039 Binder.restoreCallingIdentity(identity);
4040 }
4041 }
4042
4043 @Override
4044 public void setRttCapabilitySetting(int subId, boolean isEnabled) {
4045 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(mApp, subId,
4046 "setRttCapabilityEnabled");
4047 final long identity = Binder.clearCallingIdentity();
4048 try {
4049 // TODO: Refactor to remove ImsManager dependence and query through ImsPhone directly.
Brad Ebinger1ce9c432019-07-16 13:19:44 -07004050 ImsManager.getInstance(mApp, getSlotIndexOrException(subId)).setRttEnabled(isEnabled);
4051 } catch (ImsException e) {
4052 throw new ServiceSpecificException(e.getCode());
Brad Ebinger35c841c2018-10-01 10:40:55 -07004053 } finally {
4054 Binder.restoreCallingIdentity(identity);
4055 }
4056 }
4057
shilu366312e2019-12-17 09:28:10 -08004058 /**
4059 * Requires carrier privileges or READ_PRECISE_PHONE_STATE permission.
4060 * @param subId The subscription to use to check the configuration.
4061 */
Brad Ebinger35c841c2018-10-01 10:40:55 -07004062 @Override
4063 public boolean isTtyOverVolteEnabled(int subId) {
Rambo Wang37f9c242020-02-10 14:45:28 -08004064 TelephonyPermissions.enforeceCallingOrSelfReadPrecisePhoneStatePermissionOrCarrierPrivilege(
4065 mApp, subId, "isTtyOverVolteEnabled");
Brad Ebinger35c841c2018-10-01 10:40:55 -07004066 final long identity = Binder.clearCallingIdentity();
4067 try {
4068 // TODO: Refactor to remove ImsManager dependence and query through ImsPhone directly.
Nazanin Bakhshif782e562018-12-11 15:15:39 -08004069 return ImsManager.getInstance(mApp,
Brad Ebinger35c841c2018-10-01 10:40:55 -07004070 getSlotIndexOrException(subId)).isTtyOnVoLteCapable();
Brad Ebinger1ce9c432019-07-16 13:19:44 -07004071 } catch (ImsException e) {
4072 throw new ServiceSpecificException(e.getCode());
Brad Ebinger35c841c2018-10-01 10:40:55 -07004073 } finally {
4074 Binder.restoreCallingIdentity(identity);
4075 }
4076 }
4077
Brad Ebingerdf5b4f02018-10-31 11:24:17 -07004078 @Override
4079 public void registerImsProvisioningChangedCallback(int subId, IImsConfigCallback callback) {
4080 enforceReadPrivilegedPermission("registerImsProvisioningChangedCallback");
4081 final long identity = Binder.clearCallingIdentity();
4082 try {
Brad Ebinger6cf0f652020-01-16 11:21:18 -08004083 if (!isImsAvailableOnDevice()) {
4084 throw new ServiceSpecificException(ImsException.CODE_ERROR_UNSUPPORTED_OPERATION,
4085 "IMS not available on device.");
Peter Wang050bb052020-01-13 23:33:09 -08004086 }
Brad Ebingerdf5b4f02018-10-31 11:24:17 -07004087 // TODO: Refactor to remove ImsManager dependence and query through ImsPhone directly.
Nazanin Bakhshif782e562018-12-11 15:15:39 -08004088 ImsManager.getInstance(mApp, getSlotIndexOrException(subId))
Brad Ebinger4ae57f92019-01-09 16:51:30 -08004089 .addProvisioningCallbackForSubscription(callback, subId);
Brad Ebinger1ce9c432019-07-16 13:19:44 -07004090 } catch (ImsException e) {
4091 throw new ServiceSpecificException(e.getCode());
Brad Ebingerdf5b4f02018-10-31 11:24:17 -07004092 } finally {
4093 Binder.restoreCallingIdentity(identity);
4094 }
4095 }
4096
4097 @Override
4098 public void unregisterImsProvisioningChangedCallback(int subId, IImsConfigCallback callback) {
4099 enforceReadPrivilegedPermission("unregisterImsProvisioningChangedCallback");
4100 final long identity = Binder.clearCallingIdentity();
Brad Ebinger4ae57f92019-01-09 16:51:30 -08004101 if (!SubscriptionManager.isValidSubscriptionId(subId)) {
4102 throw new IllegalArgumentException("Invalid Subscription ID: " + subId);
4103 }
Brad Ebingerdf5b4f02018-10-31 11:24:17 -07004104 try {
4105 // TODO: Refactor to remove ImsManager dependence and query through ImsPhone directly.
Nazanin Bakhshif782e562018-12-11 15:15:39 -08004106 ImsManager.getInstance(mApp, getSlotIndexOrException(subId))
Brad Ebinger4ae57f92019-01-09 16:51:30 -08004107 .removeProvisioningCallbackForSubscription(callback, subId);
Brad Ebinger1ce9c432019-07-16 13:19:44 -07004108 } catch (ImsException e) {
Brad Ebinger4ae57f92019-01-09 16:51:30 -08004109 Log.i(LOG_TAG, "unregisterImsProvisioningChangedCallback: " + subId
4110 + "is inactive, ignoring unregister.");
4111 // If the subscription is no longer active, just return, since the callback will already
4112 // have been removed internally.
Brad Ebingerdf5b4f02018-10-31 11:24:17 -07004113 } finally {
4114 Binder.restoreCallingIdentity(identity);
4115 }
4116 }
4117
allenwtsu99c623b2020-01-03 18:24:23 +08004118
4119 private void checkModifyPhoneStatePermission(int subId, String message) {
4120 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(mApp, subId,
4121 message);
4122 }
4123
4124 private boolean isImsProvisioningRequired(int subId, int capability,
4125 boolean isMmtelCapability) {
4126 Phone phone = getPhone(subId);
4127 if (phone == null) {
4128 loge("phone instance null for subid " + subId);
4129 return false;
4130 }
4131 if (isMmtelCapability) {
4132 if (!doesImsCapabilityRequireProvisioning(phone.getContext(), subId, capability)) {
4133 return false;
4134 }
4135 } else {
4136 if (!doesRcsCapabilityRequireProvisioning(phone.getContext(), subId, capability)) {
4137 return false;
4138 }
4139 }
4140 return true;
4141 }
4142
4143 @Override
4144 public void setRcsProvisioningStatusForCapability(int subId, int capability,
4145 boolean isProvisioned) {
4146 checkModifyPhoneStatePermission(subId, "setRcsProvisioningStatusForCapability");
4147
4148 final long identity = Binder.clearCallingIdentity();
4149 try {
4150 // TODO: Refactor to remove ImsManager dependence and query through ImsPhone directly.
4151 if (!isImsProvisioningRequired(subId, capability, false)) {
4152 return;
4153 }
4154
4155 // this capability requires provisioning, route to the correct API.
4156 ImsManager ims = ImsManager.getInstance(mApp, getSlotIndex(subId));
4157 switch (capability) {
4158 case RcsFeature.RcsImsCapabilities.CAPABILITY_TYPE_OPTIONS_UCE:
4159 case RcsFeature.RcsImsCapabilities.CAPABILITY_TYPE_PRESENCE_UCE:
4160 ims.setEabProvisioned(isProvisioned);
4161 break;
4162 default: {
4163 throw new IllegalArgumentException("Tried to set provisioning for "
4164 + "rcs capability '" + capability + "', which does not require "
4165 + "provisioning.");
4166 }
4167 }
4168 } finally {
4169 Binder.restoreCallingIdentity(identity);
4170 }
4171
4172 }
4173
4174
4175 @Override
4176 public boolean getRcsProvisioningStatusForCapability(int subId, int capability) {
4177 enforceReadPrivilegedPermission("getRcsProvisioningStatusForCapability");
4178 final long identity = Binder.clearCallingIdentity();
4179 try {
4180 // TODO: Refactor to remove ImsManager dependence and query through ImsPhone directly.
4181 if (!isImsProvisioningRequired(subId, capability, false)) {
4182 return true;
4183 }
4184
4185 ImsManager ims = ImsManager.getInstance(mApp, getSlotIndex(subId));
4186 switch (capability) {
4187 case RcsFeature.RcsImsCapabilities.CAPABILITY_TYPE_OPTIONS_UCE:
4188 case RcsFeature.RcsImsCapabilities.CAPABILITY_TYPE_PRESENCE_UCE:
4189 return ims.isEabProvisionedOnDevice();
4190
4191 default: {
4192 throw new IllegalArgumentException("Tried to get rcs provisioning for "
4193 + "capability '" + capability + "', which does not require "
4194 + "provisioning.");
4195 }
4196 }
4197
4198 } finally {
4199 Binder.restoreCallingIdentity(identity);
4200 }
4201 }
4202
Brad Ebingerdf5b4f02018-10-31 11:24:17 -07004203 @Override
Brad Ebinger1c8542e2019-01-14 13:43:14 -08004204 public void setImsProvisioningStatusForCapability(int subId, int capability, int tech,
4205 boolean isProvisioned) {
4206 if (tech != ImsRegistrationImplBase.REGISTRATION_TECH_IWLAN
4207 && tech != ImsRegistrationImplBase.REGISTRATION_TECH_LTE) {
4208 throw new IllegalArgumentException("Registration technology '" + tech + "' is invalid");
4209 }
allenwtsu99c623b2020-01-03 18:24:23 +08004210 checkModifyPhoneStatePermission(subId, "setImsProvisioningStatusForCapability");
Brad Ebinger1c8542e2019-01-14 13:43:14 -08004211 final long identity = Binder.clearCallingIdentity();
4212 try {
4213 // TODO: Refactor to remove ImsManager dependence and query through ImsPhone directly.
allenwtsu99c623b2020-01-03 18:24:23 +08004214 if (!isImsProvisioningRequired(subId, capability, true)) {
Brad Ebinger1c8542e2019-01-14 13:43:14 -08004215 return;
4216 }
4217
4218 // this capability requires provisioning, route to the correct API.
4219 ImsManager ims = ImsManager.getInstance(mApp, getSlotIndex(subId));
4220 switch (capability) {
4221 case MmTelFeature.MmTelCapabilities.CAPABILITY_TYPE_VOICE: {
4222 if (tech == ImsRegistrationImplBase.REGISTRATION_TECH_LTE) {
4223 ims.setVolteProvisioned(isProvisioned);
4224 } else if (tech == ImsRegistrationImplBase.REGISTRATION_TECH_IWLAN) {
4225 ims.setWfcProvisioned(isProvisioned);
4226 }
4227 break;
4228 }
4229 case MmTelFeature.MmTelCapabilities.CAPABILITY_TYPE_VIDEO: {
4230 // There is currently no difference in VT provisioning type.
4231 ims.setVtProvisioned(isProvisioned);
4232 break;
4233 }
4234 case MmTelFeature.MmTelCapabilities.CAPABILITY_TYPE_UT: {
4235 // There is no "deprecated" UT provisioning mechanism through ImsConfig, so
4236 // change the capability of the feature instead if needed.
4237 if (isMmTelCapabilityProvisionedInCache(subId, capability, tech)
4238 == isProvisioned) {
4239 // No change in provisioning.
4240 return;
4241 }
4242 cacheMmTelCapabilityProvisioning(subId, capability, tech, isProvisioned);
4243 try {
4244 ims.changeMmTelCapability(capability, tech, isProvisioned);
Brad Ebinger1ce9c432019-07-16 13:19:44 -07004245 } catch (com.android.ims.ImsException e) {
Brad Ebinger1c8542e2019-01-14 13:43:14 -08004246 loge("setImsProvisioningStatusForCapability: couldn't change UT capability"
4247 + ", Exception" + e.getMessage());
4248 }
4249 break;
4250 }
4251 default: {
allenwtsu99c623b2020-01-03 18:24:23 +08004252 throw new IllegalArgumentException("Tried to set provisioning for "
4253 + "MmTel capability '" + capability + "', which does not require "
4254 + "provisioning. ");
Brad Ebinger1c8542e2019-01-14 13:43:14 -08004255 }
4256 }
4257
4258 } finally {
4259 Binder.restoreCallingIdentity(identity);
4260 }
4261 }
4262
4263 @Override
4264 public boolean getImsProvisioningStatusForCapability(int subId, int capability, int tech) {
4265 if (tech != ImsRegistrationImplBase.REGISTRATION_TECH_IWLAN
4266 && tech != ImsRegistrationImplBase.REGISTRATION_TECH_LTE) {
4267 throw new IllegalArgumentException("Registration technology '" + tech + "' is invalid");
4268 }
4269 enforceReadPrivilegedPermission("getProvisioningStatusForCapability");
4270 final long identity = Binder.clearCallingIdentity();
4271 try {
4272 // TODO: Refactor to remove ImsManager dependence and query through ImsPhone directly.
allenwtsu99c623b2020-01-03 18:24:23 +08004273 if (!isImsProvisioningRequired(subId, capability, true)) {
Brad Ebinger1c8542e2019-01-14 13:43:14 -08004274 return true;
4275 }
4276
4277 ImsManager ims = ImsManager.getInstance(mApp, getSlotIndex(subId));
4278 switch (capability) {
4279 case MmTelFeature.MmTelCapabilities.CAPABILITY_TYPE_VOICE: {
4280 if (tech == ImsRegistrationImplBase.REGISTRATION_TECH_LTE) {
4281 return ims.isVolteProvisionedOnDevice();
4282 } else if (tech == ImsRegistrationImplBase.REGISTRATION_TECH_IWLAN) {
4283 return ims.isWfcProvisionedOnDevice();
4284 }
4285 // This should never happen, since we are checking tech above to make sure it
4286 // is either LTE or IWLAN.
4287 throw new IllegalArgumentException("Invalid radio technology for voice "
4288 + "capability.");
4289 }
4290 case MmTelFeature.MmTelCapabilities.CAPABILITY_TYPE_VIDEO: {
4291 // There is currently no difference in VT provisioning type.
4292 return ims.isVtProvisionedOnDevice();
4293 }
4294 case MmTelFeature.MmTelCapabilities.CAPABILITY_TYPE_UT: {
4295 // There is no "deprecated" UT provisioning mechanism, so get from shared prefs.
4296 return isMmTelCapabilityProvisionedInCache(subId, capability, tech);
4297 }
4298 default: {
allenwtsu99c623b2020-01-03 18:24:23 +08004299 throw new IllegalArgumentException(
4300 "Tried to get provisioning for MmTel capability '" + capability
4301 + "', which does not require provisioning.");
Brad Ebinger1c8542e2019-01-14 13:43:14 -08004302 }
4303 }
4304
4305 } finally {
4306 Binder.restoreCallingIdentity(identity);
4307 }
4308 }
4309
4310 @Override
4311 public boolean isMmTelCapabilityProvisionedInCache(int subId, int capability, int tech) {
4312 if (tech != ImsRegistrationImplBase.REGISTRATION_TECH_IWLAN
4313 && tech != ImsRegistrationImplBase.REGISTRATION_TECH_LTE) {
4314 throw new IllegalArgumentException("Registration technology '" + tech + "' is invalid");
4315 }
4316 enforceReadPrivilegedPermission("isMmTelCapabilityProvisionedInCache");
4317 int provisionedBits = getMmTelCapabilityProvisioningBitfield(subId, tech);
4318 return (provisionedBits & capability) > 0;
4319 }
4320
4321 @Override
4322 public void cacheMmTelCapabilityProvisioning(int subId, int capability, int tech,
4323 boolean isProvisioned) {
4324 if (tech != ImsRegistrationImplBase.REGISTRATION_TECH_IWLAN
4325 && tech != ImsRegistrationImplBase.REGISTRATION_TECH_LTE) {
4326 throw new IllegalArgumentException("Registration technology '" + tech + "' is invalid");
4327 }
4328 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(mApp, subId,
4329 "setProvisioningStatusForCapability");
4330 int provisionedBits = getMmTelCapabilityProvisioningBitfield(subId, tech);
4331 // If the current provisioning status for capability already matches isProvisioned,
4332 // do nothing.
4333 if (((provisionedBits & capability) > 0) == isProvisioned) {
4334 return;
4335 }
4336 if (isProvisioned) {
4337 setMmTelCapabilityProvisioningBitfield(subId, tech, (provisionedBits | capability));
4338 } else {
4339 setMmTelCapabilityProvisioningBitfield(subId, tech, (provisionedBits & ~capability));
4340 }
4341 }
4342
4343 /**
4344 * @return the bitfield containing the MmTel provisioning for the provided subscription and
4345 * technology. The bitfield should mirror the bitfield defined by
4346 * {@link MmTelFeature.MmTelCapabilities.MmTelCapability}.
4347 */
4348 private int getMmTelCapabilityProvisioningBitfield(int subId, int tech) {
4349 String key = getMmTelProvisioningKey(subId, tech);
4350 // Default is no capabilities are provisioned.
4351 return mTelephonySharedPreferences.getInt(key, 0 /*default*/);
4352 }
4353
4354 /**
4355 * Sets the MmTel capability provisioning bitfield (defined by
4356 * {@link MmTelFeature.MmTelCapabilities.MmTelCapability}) for the subscription and
4357 * technology specified.
4358 *
4359 * Note: This is a synchronous command and should not be called on UI thread.
4360 */
4361 private void setMmTelCapabilityProvisioningBitfield(int subId, int tech, int newField) {
4362 final SharedPreferences.Editor editor = mTelephonySharedPreferences.edit();
4363 String key = getMmTelProvisioningKey(subId, tech);
4364 editor.putInt(key, newField);
4365 editor.commit();
4366 }
4367
4368 private static String getMmTelProvisioningKey(int subId, int tech) {
4369 // resulting key is provision_ims_mmtel_{subId}_{tech}
4370 return PREF_PROVISION_IMS_MMTEL_PREFIX + subId + "_" + tech;
4371 }
4372
4373 /**
4374 * Query CarrierConfig to see if the specified capability requires provisioning for the
4375 * carrier associated with the subscription id.
4376 */
4377 private boolean doesImsCapabilityRequireProvisioning(Context context, int subId,
4378 int capability) {
4379 CarrierConfigManager configManager = new CarrierConfigManager(context);
4380 PersistableBundle c = configManager.getConfigForSubId(subId);
4381 boolean requireUtProvisioning = c.getBoolean(
Brad Ebinger076903f2019-05-13 10:00:22 -07004382 CarrierConfigManager.KEY_CARRIER_SUPPORTS_SS_OVER_UT_BOOL, false)
Brad Ebinger1c8542e2019-01-14 13:43:14 -08004383 && c.getBoolean(CarrierConfigManager.KEY_CARRIER_UT_PROVISIONING_REQUIRED_BOOL,
4384 false);
4385 boolean requireVoiceVtProvisioning = c.getBoolean(
4386 CarrierConfigManager.KEY_CARRIER_VOLTE_PROVISIONING_REQUIRED_BOOL, false);
4387
4388 // First check to make sure that the capability requires provisioning.
4389 switch (capability) {
4390 case MmTelFeature.MmTelCapabilities.CAPABILITY_TYPE_VOICE:
4391 // intentional fallthrough
4392 case MmTelFeature.MmTelCapabilities.CAPABILITY_TYPE_VIDEO: {
4393 if (requireVoiceVtProvisioning) {
4394 // Voice and Video requires provisioning
4395 return true;
4396 }
4397 break;
4398 }
4399 case MmTelFeature.MmTelCapabilities.CAPABILITY_TYPE_UT: {
4400 if (requireUtProvisioning) {
4401 // UT requires provisioning
4402 return true;
4403 }
4404 break;
4405 }
4406 }
4407 return false;
4408 }
4409
allenwtsu99c623b2020-01-03 18:24:23 +08004410 private boolean doesRcsCapabilityRequireProvisioning(Context context, int subId,
4411 int capability) {
4412 CarrierConfigManager configManager = new CarrierConfigManager(context);
4413 PersistableBundle c = configManager.getConfigForSubId(subId);
4414
4415 boolean requireRcsProvisioning = c.getBoolean(
4416 CarrierConfigManager.KEY_CARRIER_RCS_PROVISIONING_REQUIRED_BOOL, false);
4417
4418 // First check to make sure that the capability requires provisioning.
4419 switch (capability) {
4420 case RcsFeature.RcsImsCapabilities.CAPABILITY_TYPE_PRESENCE_UCE:
4421 // intentional fallthrough
4422 case RcsFeature.RcsImsCapabilities.CAPABILITY_TYPE_OPTIONS_UCE: {
4423 if (requireRcsProvisioning) {
4424 // OPTION or PRESENCE requires provisioning
4425 return true;
4426 }
4427 break;
4428 }
4429 }
4430 return false;
4431 }
4432
Brad Ebinger1c8542e2019-01-14 13:43:14 -08004433 @Override
Brad Ebingerdf5b4f02018-10-31 11:24:17 -07004434 public int getImsProvisioningInt(int subId, int key) {
Brad Ebinger1c8542e2019-01-14 13:43:14 -08004435 if (!SubscriptionManager.isValidSubscriptionId(subId)) {
4436 throw new IllegalArgumentException("Invalid Subscription id '" + subId + "'");
4437 }
Brad Ebingerdf5b4f02018-10-31 11:24:17 -07004438 enforceReadPrivilegedPermission("getImsProvisioningInt");
4439 final long identity = Binder.clearCallingIdentity();
4440 try {
4441 // TODO: Refactor to remove ImsManager dependence and query through ImsPhone directly.
Brad Ebinger1c8542e2019-01-14 13:43:14 -08004442 int slotId = getSlotIndex(subId);
4443 if (slotId <= SubscriptionManager.INVALID_SIM_SLOT_INDEX) {
4444 Log.w(LOG_TAG, "getImsProvisioningInt: called with an inactive subscription '"
4445 + subId + "' for key:" + key);
4446 return ImsConfigImplBase.CONFIG_RESULT_UNKNOWN;
4447 }
4448 return ImsManager.getInstance(mApp, slotId).getConfigInterface().getConfigInt(key);
Brad Ebinger1ce9c432019-07-16 13:19:44 -07004449 } catch (com.android.ims.ImsException e) {
Brad Ebinger1c8542e2019-01-14 13:43:14 -08004450 Log.w(LOG_TAG, "getImsProvisioningInt: ImsService is not available for subscription '"
4451 + subId + "' for key:" + key);
4452 return ImsConfigImplBase.CONFIG_RESULT_UNKNOWN;
Brad Ebingerdf5b4f02018-10-31 11:24:17 -07004453 } finally {
4454 Binder.restoreCallingIdentity(identity);
4455 }
4456 }
4457
4458 @Override
4459 public String getImsProvisioningString(int subId, int key) {
Brad Ebinger1c8542e2019-01-14 13:43:14 -08004460 if (!SubscriptionManager.isValidSubscriptionId(subId)) {
4461 throw new IllegalArgumentException("Invalid Subscription id '" + subId + "'");
4462 }
Brad Ebingerdf5b4f02018-10-31 11:24:17 -07004463 enforceReadPrivilegedPermission("getImsProvisioningString");
4464 final long identity = Binder.clearCallingIdentity();
4465 try {
4466 // TODO: Refactor to remove ImsManager dependence and query through ImsPhone directly.
Brad Ebinger1c8542e2019-01-14 13:43:14 -08004467 int slotId = getSlotIndex(subId);
4468 if (slotId <= SubscriptionManager.INVALID_SIM_SLOT_INDEX) {
4469 Log.w(LOG_TAG, "getImsProvisioningString: called for an inactive subscription id '"
4470 + subId + "' for key:" + key);
4471 return ProvisioningManager.STRING_QUERY_RESULT_ERROR_GENERIC;
4472 }
4473 return ImsManager.getInstance(mApp, slotId).getConfigInterface().getConfigString(key);
Brad Ebinger1ce9c432019-07-16 13:19:44 -07004474 } catch (com.android.ims.ImsException e) {
Brad Ebinger1c8542e2019-01-14 13:43:14 -08004475 Log.w(LOG_TAG, "getImsProvisioningString: ImsService is not available for sub '"
4476 + subId + "' for key:" + key);
4477 return ProvisioningManager.STRING_QUERY_RESULT_ERROR_NOT_READY;
Brad Ebingerdf5b4f02018-10-31 11:24:17 -07004478 } finally {
4479 Binder.restoreCallingIdentity(identity);
4480 }
4481 }
4482
4483 @Override
4484 public int setImsProvisioningInt(int subId, int key, int value) {
Brad Ebinger1c8542e2019-01-14 13:43:14 -08004485 if (!SubscriptionManager.isValidSubscriptionId(subId)) {
4486 throw new IllegalArgumentException("Invalid Subscription id '" + subId + "'");
4487 }
Brad Ebinger3d0b34e2018-11-15 14:13:12 -08004488 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(mApp, subId,
4489 "setImsProvisioningInt");
Brad Ebingerdf5b4f02018-10-31 11:24:17 -07004490 final long identity = Binder.clearCallingIdentity();
4491 try {
4492 // TODO: Refactor to remove ImsManager dependence and query through ImsPhone directly.
Brad Ebinger1c8542e2019-01-14 13:43:14 -08004493 int slotId = getSlotIndex(subId);
4494 if (slotId <= SubscriptionManager.INVALID_SIM_SLOT_INDEX) {
4495 Log.w(LOG_TAG, "setImsProvisioningInt: called with an inactive subscription id '"
4496 + subId + "' for key:" + key);
4497 return ImsConfigImplBase.CONFIG_RESULT_FAILED;
4498 }
4499 return ImsManager.getInstance(mApp, slotId).getConfigInterface().setConfig(key, value);
Brad Ebinger1ce9c432019-07-16 13:19:44 -07004500 } catch (com.android.ims.ImsException e) {
Brad Ebinger1c8542e2019-01-14 13:43:14 -08004501 Log.w(LOG_TAG, "setImsProvisioningInt: ImsService unavailable for sub '" + subId
4502 + "' for key:" + key);
4503 return ImsConfigImplBase.CONFIG_RESULT_FAILED;
Brad Ebingerdf5b4f02018-10-31 11:24:17 -07004504 } finally {
4505 Binder.restoreCallingIdentity(identity);
4506 }
4507 }
4508
4509 @Override
4510 public int setImsProvisioningString(int subId, int key, String value) {
Brad Ebinger1c8542e2019-01-14 13:43:14 -08004511 if (!SubscriptionManager.isValidSubscriptionId(subId)) {
4512 throw new IllegalArgumentException("Invalid Subscription id '" + subId + "'");
4513 }
Brad Ebinger3d0b34e2018-11-15 14:13:12 -08004514 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(mApp, subId,
4515 "setImsProvisioningString");
Brad Ebingerdf5b4f02018-10-31 11:24:17 -07004516 final long identity = Binder.clearCallingIdentity();
4517 try {
4518 // TODO: Refactor to remove ImsManager dependence and query through ImsPhone directly.
Brad Ebinger1c8542e2019-01-14 13:43:14 -08004519 int slotId = getSlotIndex(subId);
4520 if (slotId <= SubscriptionManager.INVALID_SIM_SLOT_INDEX) {
4521 Log.w(LOG_TAG, "setImsProvisioningString: called with an inactive subscription id '"
4522 + subId + "' for key:" + key);
4523 return ImsConfigImplBase.CONFIG_RESULT_FAILED;
4524 }
4525 return ImsManager.getInstance(mApp, slotId).getConfigInterface().setConfig(key, value);
Brad Ebinger1ce9c432019-07-16 13:19:44 -07004526 } catch (com.android.ims.ImsException e) {
Brad Ebinger1c8542e2019-01-14 13:43:14 -08004527 Log.w(LOG_TAG, "setImsProvisioningString: ImsService unavailable for sub '" + subId
4528 + "' for key:" + key);
4529 return ImsConfigImplBase.CONFIG_RESULT_FAILED;
Brad Ebingerdf5b4f02018-10-31 11:24:17 -07004530 } finally {
4531 Binder.restoreCallingIdentity(identity);
4532 }
4533 }
4534
Brad Ebinger1ce9c432019-07-16 13:19:44 -07004535 private int getSlotIndexOrException(int subId) throws ImsException {
Brad Ebinger35c841c2018-10-01 10:40:55 -07004536 int slotId = SubscriptionManager.getSlotIndex(subId);
4537 if (!SubscriptionManager.isValidSlotIndex(slotId)) {
Brad Ebinger1ce9c432019-07-16 13:19:44 -07004538 throw new ImsException("Invalid Subscription Id, subId=" + subId,
4539 ImsException.CODE_ERROR_INVALID_SUBSCRIPTION);
Brad Ebinger35c841c2018-10-01 10:40:55 -07004540 }
4541 return slotId;
4542 }
4543
Brad Ebinger1c8542e2019-01-14 13:43:14 -08004544 private int getSlotIndex(int subId) {
4545 int slotId = SubscriptionManager.getSlotIndex(subId);
4546 if (!SubscriptionManager.isValidSlotIndex(slotId)) {
4547 return SubscriptionManager.INVALID_SIM_SLOT_INDEX;
4548 }
4549 return slotId;
4550 }
4551
Wink Saville36469e72014-06-11 15:17:00 -07004552 /**
Nathan Harold9042f0b2019-05-21 15:51:27 -07004553 * Returns the data network type for a subId; does not throw SecurityException.
Wink Saville36469e72014-06-11 15:17:00 -07004554 */
4555 @Override
Philip P. Moltmann9797cbb2020-01-07 13:45:00 -08004556 public int getNetworkTypeForSubscriber(int subId, String callingPackage,
4557 String callingFeatureId) {
Nathan Haroldef60dba2019-05-22 13:55:14 -07004558 final int targetSdk = getTargetSdk(callingPackage);
4559 if (targetSdk > android.os.Build.VERSION_CODES.Q) {
Philip P. Moltmann9797cbb2020-01-07 13:45:00 -08004560 return getDataNetworkTypeForSubscriber(subId, callingPackage, callingFeatureId);
Nathan Haroldef60dba2019-05-22 13:55:14 -07004561 } else if (targetSdk == android.os.Build.VERSION_CODES.Q
Nathan Harold9042f0b2019-05-21 15:51:27 -07004562 && !TelephonyPermissions.checkCallingOrSelfReadPhoneStateNoThrow(
Philip P. Moltmann9797cbb2020-01-07 13:45:00 -08004563 mApp, subId, callingPackage, callingFeatureId,
4564 "getNetworkTypeForSubscriber")) {
Robert Greenwalt36b23af2015-07-06 17:59:14 -07004565 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
4566 }
Robert Greenwalta5dcfcb2015-07-10 09:06:29 -07004567
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004568 final long identity = Binder.clearCallingIdentity();
4569 try {
4570 final Phone phone = getPhone(subId);
4571 if (phone != null) {
4572 return phone.getServiceState().getDataNetworkType();
4573 } else {
4574 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
4575 }
4576 } finally {
4577 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07004578 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07004579 }
4580
4581 /**
4582 * Returns the data network type
4583 */
4584 @Override
Philip P. Moltmann9797cbb2020-01-07 13:45:00 -08004585 public int getDataNetworkType(String callingPackage, String callingFeatureId) {
4586 return getDataNetworkTypeForSubscriber(getDefaultSubscription(), callingPackage,
4587 callingFeatureId);
Wink Saville36469e72014-06-11 15:17:00 -07004588 }
4589
4590 /**
4591 * Returns the data network type for a subId
4592 */
4593 @Override
Philip P. Moltmann9797cbb2020-01-07 13:45:00 -08004594 public int getDataNetworkTypeForSubscriber(int subId, String callingPackage,
4595 String callingFeatureId) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08004596 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
Philip P. Moltmann9797cbb2020-01-07 13:45:00 -08004597 mApp, subId, callingPackage, callingFeatureId,
4598 "getDataNetworkTypeForSubscriber")) {
Robert Greenwalt36b23af2015-07-06 17:59:14 -07004599 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
4600 }
4601
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004602 final long identity = Binder.clearCallingIdentity();
4603 try {
4604 final Phone phone = getPhone(subId);
4605 if (phone != null) {
4606 return phone.getServiceState().getDataNetworkType();
4607 } else {
4608 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
4609 }
4610 } finally {
4611 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07004612 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07004613 }
4614
4615 /**
Wink Saville36469e72014-06-11 15:17:00 -07004616 * Returns the Voice network type for a subId
4617 */
4618 @Override
Philip P. Moltmann9797cbb2020-01-07 13:45:00 -08004619 public int getVoiceNetworkTypeForSubscriber(int subId, String callingPackage,
4620 String callingFeatureId) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08004621 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
Philip P. Moltmann9797cbb2020-01-07 13:45:00 -08004622 mApp, subId, callingPackage, callingFeatureId,
4623 "getDataNetworkTypeForSubscriber")) {
Robert Greenwalta5dcfcb2015-07-10 09:06:29 -07004624 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
4625 }
4626
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004627 final long identity = Binder.clearCallingIdentity();
4628 try {
4629 final Phone phone = getPhone(subId);
4630 if (phone != null) {
4631 return phone.getServiceState().getVoiceNetworkType();
4632 } else {
4633 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
4634 }
4635 } finally {
4636 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07004637 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07004638 }
4639
4640 /**
4641 * @return true if a ICC card is present
4642 */
4643 public boolean hasIccCard() {
Wink Saville36469e72014-06-11 15:17:00 -07004644 // FIXME Make changes to pass defaultSimId of type int
Sanket Padawe13bac7b2017-03-20 15:04:47 -07004645 return hasIccCardUsingSlotIndex(mSubscriptionController.getSlotIndex(
4646 getDefaultSubscription()));
Wink Saville36469e72014-06-11 15:17:00 -07004647 }
4648
4649 /**
Sanket Padawe13bac7b2017-03-20 15:04:47 -07004650 * @return true if a ICC card is present for a slotIndex
Wink Saville36469e72014-06-11 15:17:00 -07004651 */
Sanket Padawe356d7632015-06-22 14:03:32 -07004652 @Override
Sanket Padawe13bac7b2017-03-20 15:04:47 -07004653 public boolean hasIccCardUsingSlotIndex(int slotIndex) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004654 final long identity = Binder.clearCallingIdentity();
4655 try {
4656 final Phone phone = PhoneFactory.getPhone(slotIndex);
4657 if (phone != null) {
4658 return phone.getIccCard().hasIccCard();
4659 } else {
4660 return false;
4661 }
4662 } finally {
4663 Binder.restoreCallingIdentity(identity);
Amit Mahajana6fc2a82015-01-06 11:53:51 -08004664 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07004665 }
4666
4667 /**
4668 * Return if the current radio is LTE on CDMA. This
4669 * is a tri-state return value as for a period of time
4670 * the mode may be unknown.
4671 *
Robert Greenwalt36b23af2015-07-06 17:59:14 -07004672 * @param callingPackage the name of the package making the call.
Santos Cordon7d4ddf62013-07-10 11:58:08 -07004673 * @return {@link Phone#LTE_ON_CDMA_UNKNOWN}, {@link Phone#LTE_ON_CDMA_FALSE}
Jake Hambye994d462014-02-03 13:10:13 -08004674 * or {@link Phone#LTE_ON_CDMA_TRUE}
Santos Cordon7d4ddf62013-07-10 11:58:08 -07004675 */
Robert Greenwalt36b23af2015-07-06 17:59:14 -07004676 @Override
Philip P. Moltmann9797cbb2020-01-07 13:45:00 -08004677 public int getLteOnCdmaMode(String callingPackage, String callingFeatureId) {
4678 return getLteOnCdmaModeForSubscriber(getDefaultSubscription(), callingPackage,
4679 callingFeatureId);
Wink Saville36469e72014-06-11 15:17:00 -07004680 }
4681
Sanket Padawe356d7632015-06-22 14:03:32 -07004682 @Override
Philip P. Moltmann9797cbb2020-01-07 13:45:00 -08004683 public int getLteOnCdmaModeForSubscriber(int subId, String callingPackage,
4684 String callingFeatureId) {
Sarah Chinf6656a62020-01-16 12:17:23 -08004685 try {
4686 enforceReadPrivilegedPermission("getLteOnCdmaModeForSubscriber");
4687 } catch (SecurityException e) {
Robert Greenwalt36b23af2015-07-06 17:59:14 -07004688 return PhoneConstants.LTE_ON_CDMA_UNKNOWN;
4689 }
4690
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004691 final long identity = Binder.clearCallingIdentity();
4692 try {
4693 final Phone phone = getPhone(subId);
4694 if (phone == null) {
4695 return PhoneConstants.LTE_ON_CDMA_UNKNOWN;
4696 } else {
Nathan Harold05ad6332020-07-10 11:54:36 -07004697 return TelephonyProperties.lte_on_cdma_device()
4698 .orElse(PhoneConstants.LTE_ON_CDMA_FALSE);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004699 }
4700 } finally {
4701 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07004702 }
Wink Saville36469e72014-06-11 15:17:00 -07004703 }
4704
Wink Saville36469e72014-06-11 15:17:00 -07004705 /**
4706 * {@hide}
4707 * Returns Default subId, 0 in the case of single standby.
4708 */
Wink Savilleb564aae2014-10-23 10:18:09 -07004709 private int getDefaultSubscription() {
Wink Savilleac1bdfd2014-11-20 23:04:44 -08004710 return mSubscriptionController.getDefaultSubId();
Wink Saville36469e72014-06-11 15:17:00 -07004711 }
4712
Shishir Agrawala9f32182016-04-12 12:00:16 -07004713 private int getSlotForDefaultSubscription() {
4714 return mSubscriptionController.getPhoneId(getDefaultSubscription());
4715 }
4716
Wink Savilleb564aae2014-10-23 10:18:09 -07004717 private int getPreferredVoiceSubscription() {
Wink Savilleac1bdfd2014-11-20 23:04:44 -08004718 return mSubscriptionController.getDefaultVoiceSubId();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07004719 }
Ihab Awadf2177b72013-11-25 13:33:23 -08004720
Pengquan Menge92a50d2018-09-21 15:54:48 -07004721 private boolean isActiveSubscription(int subId) {
4722 return mSubscriptionController.isActiveSubId(subId);
4723 }
4724
Ihab Awadf2177b72013-11-25 13:33:23 -08004725 /**
4726 * @see android.telephony.TelephonyManager.WifiCallingChoices
4727 */
4728 public int getWhenToMakeWifiCalls() {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004729 final long identity = Binder.clearCallingIdentity();
4730 try {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08004731 return Settings.System.getInt(mApp.getContentResolver(),
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004732 Settings.System.WHEN_TO_MAKE_WIFI_CALLS,
4733 getWhenToMakeWifiCallsDefaultPreference());
4734 } finally {
4735 Binder.restoreCallingIdentity(identity);
4736 }
Ihab Awadf2177b72013-11-25 13:33:23 -08004737 }
4738
4739 /**
4740 * @see android.telephony.TelephonyManager.WifiCallingChoices
4741 */
4742 public void setWhenToMakeWifiCalls(int preference) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004743 final long identity = Binder.clearCallingIdentity();
4744 try {
4745 if (DBG) log("setWhenToMakeWifiCallsStr, storing setting = " + preference);
Nazanin Bakhshif782e562018-12-11 15:15:39 -08004746 Settings.System.putInt(mApp.getContentResolver(),
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004747 Settings.System.WHEN_TO_MAKE_WIFI_CALLS, preference);
4748 } finally {
4749 Binder.restoreCallingIdentity(identity);
4750 }
Ihab Awadf9e92732013-12-05 18:02:52 -08004751 }
4752
Sailesh Nepald1e68152013-12-12 19:08:02 -08004753 private static int getWhenToMakeWifiCallsDefaultPreference() {
Santos Cordonda120f42014-08-06 04:44:34 -07004754 // TODO: Use a build property to choose this value.
Evan Charlton9829e882013-12-19 15:30:38 -08004755 return TelephonyManager.WifiCallingChoices.ALWAYS_USE;
Ihab Awadf2177b72013-11-25 13:33:23 -08004756 }
Shishir Agrawal69f68122013-12-16 17:25:49 -08004757
Jordan Liu4c733742019-02-28 12:03:40 -08004758 private Phone getPhoneFromSlotIdOrThrowException(int slotIndex) {
4759 int phoneId = UiccController.getInstance().getPhoneIdFromSlotId(slotIndex);
4760 if (phoneId == -1) {
4761 throw new IllegalArgumentException("Given slot index: " + slotIndex
4762 + " does not correspond to an active phone");
4763 }
4764 return PhoneFactory.getPhone(phoneId);
4765 }
4766
Shishir Agrawal566b7612013-10-28 14:41:00 -07004767 @Override
Derek Tan740e1672017-06-27 14:56:27 -07004768 public IccOpenLogicalChannelResponse iccOpenLogicalChannel(
4769 int subId, String callingPackage, String aid, int p2) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08004770 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
4771 mApp, subId, "iccOpenLogicalChannel");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004772 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
Jordan Liu4c733742019-02-28 12:03:40 -08004773 if (DBG) {
4774 log("iccOpenLogicalChannel: subId=" + subId + " aid=" + aid + " p2=" + p2);
4775 }
4776 return iccOpenLogicalChannelWithPermission(getPhoneFromSubId(subId), callingPackage, aid,
4777 p2);
4778 }
Shishir Agrawal566b7612013-10-28 14:41:00 -07004779
Jordan Liu4c733742019-02-28 12:03:40 -08004780
4781 @Override
4782 public IccOpenLogicalChannelResponse iccOpenLogicalChannelBySlot(
4783 int slotIndex, String callingPackage, String aid, int p2) {
4784 enforceModifyPermission();
4785 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
4786 if (DBG) {
4787 log("iccOpenLogicalChannelBySlot: slot=" + slotIndex + " aid=" + aid + " p2=" + p2);
4788 }
4789 return iccOpenLogicalChannelWithPermission(getPhoneFromSlotIdOrThrowException(slotIndex),
4790 callingPackage, aid, p2);
4791 }
4792
4793 private IccOpenLogicalChannelResponse iccOpenLogicalChannelWithPermission(Phone phone,
4794 String callingPackage, String aid, int p2) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004795 final long identity = Binder.clearCallingIdentity();
4796 try {
4797 if (TextUtils.equals(ISDR_AID, aid)) {
4798 // Only allows LPA to open logical channel to ISD-R.
Nazanin Bakhshif782e562018-12-11 15:15:39 -08004799 ComponentInfo bestComponent = EuiccConnector.findBestComponent(getDefaultPhone()
4800 .getContext().getPackageManager());
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004801 if (bestComponent == null
4802 || !TextUtils.equals(callingPackage, bestComponent.packageName)) {
4803 loge("The calling package is not allowed to access ISD-R.");
4804 throw new SecurityException(
4805 "The calling package is not allowed to access ISD-R.");
4806 }
Derek Tan740e1672017-06-27 14:56:27 -07004807 }
Derek Tan740e1672017-06-27 14:56:27 -07004808
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004809 IccOpenLogicalChannelResponse response = (IccOpenLogicalChannelResponse) sendRequest(
Jordan Liu4c733742019-02-28 12:03:40 -08004810 CMD_OPEN_CHANNEL, new Pair<String, Integer>(aid, p2), phone,
4811 null /* workSource */);
4812 if (DBG) log("iccOpenLogicalChannelWithPermission: " + response);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004813 return response;
4814 } finally {
4815 Binder.restoreCallingIdentity(identity);
4816 }
Shishir Agrawal566b7612013-10-28 14:41:00 -07004817 }
4818
4819 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08004820 public boolean iccCloseLogicalChannel(int subId, int channel) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08004821 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
4822 mApp, subId, "iccCloseLogicalChannel");
Jordan Liu4c733742019-02-28 12:03:40 -08004823 if (DBG) log("iccCloseLogicalChannel: subId=" + subId + " chnl=" + channel);
4824 return iccCloseLogicalChannelWithPermission(getPhoneFromSubId(subId), channel);
4825 }
Shishir Agrawal566b7612013-10-28 14:41:00 -07004826
Jordan Liu4c733742019-02-28 12:03:40 -08004827 @Override
4828 public boolean iccCloseLogicalChannelBySlot(int slotIndex, int channel) {
4829 enforceModifyPermission();
4830 if (DBG) log("iccCloseLogicalChannelBySlot: slotIndex=" + slotIndex + " chnl=" + channel);
4831 return iccCloseLogicalChannelWithPermission(getPhoneFromSlotIdOrThrowException(slotIndex),
4832 channel);
4833 }
4834
4835 private boolean iccCloseLogicalChannelWithPermission(Phone phone, int channel) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004836 final long identity = Binder.clearCallingIdentity();
4837 try {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004838 if (channel < 0) {
4839 return false;
4840 }
Jordan Liu4c733742019-02-28 12:03:40 -08004841 Boolean success = (Boolean) sendRequest(CMD_CLOSE_CHANNEL, channel, phone,
4842 null /* workSource */);
4843 if (DBG) log("iccCloseLogicalChannelWithPermission: " + success);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004844 return success;
4845 } finally {
4846 Binder.restoreCallingIdentity(identity);
Shishir Agrawal566b7612013-10-28 14:41:00 -07004847 }
Shishir Agrawal566b7612013-10-28 14:41:00 -07004848 }
4849
4850 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08004851 public String iccTransmitApduLogicalChannel(int subId, int channel, int cla,
Shishir Agrawal566b7612013-10-28 14:41:00 -07004852 int command, int p1, int p2, int p3, String data) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08004853 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
4854 mApp, subId, "iccTransmitApduLogicalChannel");
Jordan Liu4c733742019-02-28 12:03:40 -08004855 if (DBG) {
4856 log("iccTransmitApduLogicalChannel: subId=" + subId + " chnl=" + channel
4857 + " cla=" + cla + " cmd=" + command + " p1=" + p1 + " p2=" + p2 + " p3="
4858 + p3 + " data=" + data);
4859 }
4860 return iccTransmitApduLogicalChannelWithPermission(getPhoneFromSubId(subId), channel, cla,
4861 command, p1, p2, p3, data);
4862 }
Shishir Agrawal566b7612013-10-28 14:41:00 -07004863
Jordan Liu4c733742019-02-28 12:03:40 -08004864 @Override
4865 public String iccTransmitApduLogicalChannelBySlot(int slotIndex, int channel, int cla,
4866 int command, int p1, int p2, int p3, String data) {
4867 enforceModifyPermission();
4868 if (DBG) {
4869 log("iccTransmitApduLogicalChannelBySlot: slotIndex=" + slotIndex + " chnl=" + channel
4870 + " cla=" + cla + " cmd=" + command + " p1=" + p1 + " p2=" + p2 + " p3="
4871 + p3 + " data=" + data);
4872 }
4873 return iccTransmitApduLogicalChannelWithPermission(
4874 getPhoneFromSlotIdOrThrowException(slotIndex), channel, cla, command, p1, p2, p3,
4875 data);
4876 }
4877
4878 private String iccTransmitApduLogicalChannelWithPermission(Phone phone, int channel, int cla,
4879 int command, int p1, int p2, int p3, String data) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004880 final long identity = Binder.clearCallingIdentity();
4881 try {
Hall Liu4fd771b2019-05-02 09:16:29 -07004882 if (channel <= 0) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004883 return "";
4884 }
Shishir Agrawal566b7612013-10-28 14:41:00 -07004885
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004886 IccIoResult response = (IccIoResult) sendRequest(CMD_TRANSMIT_APDU_LOGICAL_CHANNEL,
Jordan Liu4c733742019-02-28 12:03:40 -08004887 new IccAPDUArgument(channel, cla, command, p1, p2, p3, data), phone,
4888 null /* workSource */);
4889 if (DBG) log("iccTransmitApduLogicalChannelWithPermission: " + response);
Shishir Agrawal566b7612013-10-28 14:41:00 -07004890
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004891 // Append the returned status code to the end of the response payload.
4892 String s = Integer.toHexString(
4893 (response.sw1 << 8) + response.sw2 + 0x10000).substring(1);
4894 if (response.payload != null) {
4895 s = IccUtils.bytesToHexString(response.payload) + s;
4896 }
4897 return s;
4898 } finally {
4899 Binder.restoreCallingIdentity(identity);
Shishir Agrawal5ec14172014-08-05 17:05:45 -07004900 }
Shishir Agrawal566b7612013-10-28 14:41:00 -07004901 }
Jake Hambye994d462014-02-03 13:10:13 -08004902
Evan Charltonc66da362014-05-16 14:06:40 -07004903 @Override
Holly Jiuyu Sun1cc2d552018-01-26 15:51:16 -08004904 public String iccTransmitApduBasicChannel(int subId, String callingPackage, int cla,
4905 int command, int p1, int p2, int p3, String data) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08004906 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
4907 mApp, subId, "iccTransmitApduBasicChannel");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004908 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
Jordan Liu4c733742019-02-28 12:03:40 -08004909 if (DBG) {
4910 log("iccTransmitApduBasicChannel: subId=" + subId + " cla=" + cla + " cmd="
4911 + command + " p1=" + p1 + " p2=" + p2 + " p3=" + p3 + " data=" + data);
4912 }
4913 return iccTransmitApduBasicChannelWithPermission(getPhoneFromSubId(subId), callingPackage,
4914 cla, command, p1, p2, p3, data);
4915 }
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07004916
Jordan Liu4c733742019-02-28 12:03:40 -08004917 @Override
4918 public String iccTransmitApduBasicChannelBySlot(int slotIndex, String callingPackage, int cla,
4919 int command, int p1, int p2, int p3, String data) {
4920 enforceModifyPermission();
4921 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
4922 if (DBG) {
4923 log("iccTransmitApduBasicChannelBySlot: slotIndex=" + slotIndex + " cla=" + cla
4924 + " cmd=" + command + " p1=" + p1 + " p2=" + p2 + " p3=" + p3
4925 + " data=" + data);
4926 }
4927
4928 return iccTransmitApduBasicChannelWithPermission(
4929 getPhoneFromSlotIdOrThrowException(slotIndex), callingPackage, cla, command, p1,
4930 p2, p3, data);
4931 }
4932
4933 // open APDU basic channel assuming the caller has sufficient permissions
4934 private String iccTransmitApduBasicChannelWithPermission(Phone phone, String callingPackage,
4935 int cla, int command, int p1, int p2, int p3, String data) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004936 final long identity = Binder.clearCallingIdentity();
4937 try {
4938 if (command == SELECT_COMMAND && p1 == SELECT_P1 && p2 == SELECT_P2 && p3 == SELECT_P3
4939 && TextUtils.equals(ISDR_AID, data)) {
4940 // Only allows LPA to select ISD-R.
Nazanin Bakhshif782e562018-12-11 15:15:39 -08004941 ComponentInfo bestComponent = EuiccConnector.findBestComponent(getDefaultPhone()
4942 .getContext().getPackageManager());
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004943 if (bestComponent == null
4944 || !TextUtils.equals(callingPackage, bestComponent.packageName)) {
4945 loge("The calling package is not allowed to select ISD-R.");
4946 throw new SecurityException(
4947 "The calling package is not allowed to select ISD-R.");
4948 }
Holly Jiuyu Sun1cc2d552018-01-26 15:51:16 -08004949 }
Holly Jiuyu Sun1cc2d552018-01-26 15:51:16 -08004950
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004951 IccIoResult response = (IccIoResult) sendRequest(CMD_TRANSMIT_APDU_BASIC_CHANNEL,
Jordan Liu4c733742019-02-28 12:03:40 -08004952 new IccAPDUArgument(0, cla, command, p1, p2, p3, data), phone,
4953 null /* workSource */);
4954 if (DBG) log("iccTransmitApduBasicChannelWithPermission: " + response);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07004955
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004956 // Append the returned status code to the end of the response payload.
4957 String s = Integer.toHexString(
4958 (response.sw1 << 8) + response.sw2 + 0x10000).substring(1);
4959 if (response.payload != null) {
4960 s = IccUtils.bytesToHexString(response.payload) + s;
4961 }
4962 return s;
4963 } finally {
4964 Binder.restoreCallingIdentity(identity);
Shishir Agrawal5ec14172014-08-05 17:05:45 -07004965 }
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07004966 }
4967
4968 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08004969 public byte[] iccExchangeSimIO(int subId, int fileID, int command, int p1, int p2, int p3,
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07004970 String filePath) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08004971 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
4972 mApp, subId, "iccExchangeSimIO");
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07004973
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004974 final long identity = Binder.clearCallingIdentity();
4975 try {
4976 if (DBG) {
4977 log("Exchange SIM_IO " + subId + ":" + fileID + ":" + command + " "
4978 + p1 + " " + p2 + " " + p3 + ":" + filePath);
4979 }
4980
4981 IccIoResult response =
4982 (IccIoResult) sendRequest(CMD_EXCHANGE_SIM_IO,
4983 new IccAPDUArgument(-1, fileID, command, p1, p2, p3, filePath),
4984 subId);
4985
4986 if (DBG) {
4987 log("Exchange SIM_IO [R]" + response);
4988 }
4989
4990 byte[] result = null;
4991 int length = 2;
4992 if (response.payload != null) {
4993 length = 2 + response.payload.length;
4994 result = new byte[length];
4995 System.arraycopy(response.payload, 0, result, 0, response.payload.length);
4996 } else {
4997 result = new byte[length];
4998 }
4999
5000 result[length - 1] = (byte) response.sw2;
5001 result[length - 2] = (byte) response.sw1;
5002 return result;
5003 } finally {
5004 Binder.restoreCallingIdentity(identity);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07005005 }
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07005006 }
5007
Nathan Haroldb3014052017-01-25 15:57:32 -08005008 /**
5009 * Get the forbidden PLMN List from the given app type (ex APPTYPE_USIM)
5010 * on a particular subscription
5011 */
Philip P. Moltmann9797cbb2020-01-07 13:45:00 -08005012 public String[] getForbiddenPlmns(int subId, int appType, String callingPackage,
5013 String callingFeatureId) {
sqianb6e41952018-03-12 14:54:01 -07005014 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
Philip P. Moltmann9797cbb2020-01-07 13:45:00 -08005015 mApp, subId, callingPackage, callingFeatureId, "getForbiddenPlmns")) {
sqianb6e41952018-03-12 14:54:01 -07005016 return null;
5017 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005018
5019 final long identity = Binder.clearCallingIdentity();
5020 try {
5021 if (appType != TelephonyManager.APPTYPE_USIM
5022 && appType != TelephonyManager.APPTYPE_SIM) {
5023 loge("getForbiddenPlmnList(): App Type must be USIM or SIM");
5024 return null;
5025 }
5026 Object response = sendRequest(
5027 CMD_GET_FORBIDDEN_PLMNS, new Integer(appType), subId);
5028 if (response instanceof String[]) {
5029 return (String[]) response;
5030 }
yincheng zhaod698b842019-09-06 17:06:54 -07005031 // Response is an Exception of some kind
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005032 // which is signalled to the user as a NULL retval
Nathan Haroldb3014052017-01-25 15:57:32 -08005033 return null;
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005034 } finally {
5035 Binder.restoreCallingIdentity(identity);
Nathan Haroldb3014052017-01-25 15:57:32 -08005036 }
Nathan Haroldb3014052017-01-25 15:57:32 -08005037 }
5038
yincheng zhaod698b842019-09-06 17:06:54 -07005039 /**
5040 * Set the forbidden PLMN list from the given app type (ex APPTYPE_USIM) on a particular
5041 * subscription.
5042 *
5043 * @param subId the id of the subscription.
5044 * @param appType the uicc app type, must be USIM or SIM.
5045 * @param fplmns the Forbiden plmns list that needed to be written to the SIM.
5046 * @param callingPackage the op Package name.
Philip P. Moltmann9797cbb2020-01-07 13:45:00 -08005047 * @param callingFeatureId the feature in the package.
yincheng zhaod698b842019-09-06 17:06:54 -07005048 * @return number of fplmns that is successfully written to the SIM.
5049 */
Philip P. Moltmann9797cbb2020-01-07 13:45:00 -08005050 public int setForbiddenPlmns(int subId, int appType, List<String> fplmns, String callingPackage,
5051 String callingFeatureId) {
5052 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(mApp, subId, callingPackage,
5053 callingFeatureId, "setForbiddenPlmns")) {
yincheng zhaod698b842019-09-06 17:06:54 -07005054 if (DBG) logv("no permissions for setForbiddenplmns");
5055 throw new IllegalStateException("No Permissions for setForbiddenPlmns");
5056 }
5057 if (appType != TelephonyManager.APPTYPE_USIM && appType != TelephonyManager.APPTYPE_SIM) {
5058 loge("setForbiddenPlmnList(): App Type must be USIM or SIM");
5059 throw new IllegalArgumentException("Invalid appType: App Type must be USIM or SIM");
5060 }
5061 if (fplmns == null) {
5062 throw new IllegalArgumentException("Fplmn List provided is null");
5063 }
5064 for (String fplmn : fplmns) {
5065 if (!CellIdentity.isValidPlmn(fplmn)) {
5066 throw new IllegalArgumentException("Invalid fplmn provided: " + fplmn);
5067 }
5068 }
5069 final long identity = Binder.clearCallingIdentity();
5070 try {
5071 Object response = sendRequest(
5072 CMD_SET_FORBIDDEN_PLMNS,
5073 new Pair<Integer, List<String>>(new Integer(appType), fplmns),
5074 subId);
5075 return (int) response;
5076 } finally {
5077 Binder.restoreCallingIdentity(identity);
5078 }
5079 }
5080
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07005081 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08005082 public String sendEnvelopeWithStatus(int subId, String content) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08005083 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
5084 mApp, subId, "sendEnvelopeWithStatus");
Evan Charltonc66da362014-05-16 14:06:40 -07005085
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005086 final long identity = Binder.clearCallingIdentity();
5087 try {
5088 IccIoResult response = (IccIoResult) sendRequest(CMD_SEND_ENVELOPE, content, subId);
5089 if (response.payload == null) {
5090 return "";
5091 }
Evan Charltonc66da362014-05-16 14:06:40 -07005092
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005093 // Append the returned status code to the end of the response payload.
5094 String s = Integer.toHexString(
5095 (response.sw1 << 8) + response.sw2 + 0x10000).substring(1);
5096 s = IccUtils.bytesToHexString(response.payload) + s;
5097 return s;
5098 } finally {
5099 Binder.restoreCallingIdentity(identity);
5100 }
Evan Charltonc66da362014-05-16 14:06:40 -07005101 }
5102
Jake Hambye994d462014-02-03 13:10:13 -08005103 /**
5104 * Read one of the NV items defined in {@link com.android.internal.telephony.RadioNVItems}
5105 * and {@code ril_nv_items.h}. Used for device configuration by some CDMA operators.
5106 *
5107 * @param itemID the ID of the item to read
5108 * @return the NV item as a String, or null on error.
5109 */
5110 @Override
5111 public String nvReadItem(int itemID) {
vagdeviaf9a5b92018-08-15 16:01:53 -07005112 WorkSource workSource = getWorkSource(Binder.getCallingUid());
Jeff Davidson7e17e312018-02-13 18:17:36 -08005113 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
5114 mApp, getDefaultSubscription(), "nvReadItem");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005115
5116 final long identity = Binder.clearCallingIdentity();
5117 try {
5118 if (DBG) log("nvReadItem: item " + itemID);
vagdeviaf9a5b92018-08-15 16:01:53 -07005119 String value = (String) sendRequest(CMD_NV_READ_ITEM, itemID, workSource);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005120 if (DBG) log("nvReadItem: item " + itemID + " is \"" + value + '"');
5121 return value;
5122 } finally {
5123 Binder.restoreCallingIdentity(identity);
5124 }
Jake Hambye994d462014-02-03 13:10:13 -08005125 }
5126
5127 /**
5128 * Write one of the NV items defined in {@link com.android.internal.telephony.RadioNVItems}
5129 * and {@code ril_nv_items.h}. Used for device configuration by some CDMA operators.
5130 *
5131 * @param itemID the ID of the item to read
5132 * @param itemValue the value to write, as a String
5133 * @return true on success; false on any failure
5134 */
5135 @Override
5136 public boolean nvWriteItem(int itemID, String itemValue) {
vagdeviaf9a5b92018-08-15 16:01:53 -07005137 WorkSource workSource = getWorkSource(Binder.getCallingUid());
Jeff Davidson7e17e312018-02-13 18:17:36 -08005138 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
5139 mApp, getDefaultSubscription(), "nvWriteItem");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005140
5141 final long identity = Binder.clearCallingIdentity();
5142 try {
5143 if (DBG) log("nvWriteItem: item " + itemID + " value \"" + itemValue + '"');
5144 Boolean success = (Boolean) sendRequest(CMD_NV_WRITE_ITEM,
vagdeviaf9a5b92018-08-15 16:01:53 -07005145 new Pair<Integer, String>(itemID, itemValue), workSource);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005146 if (DBG) log("nvWriteItem: item " + itemID + ' ' + (success ? "ok" : "fail"));
5147 return success;
5148 } finally {
5149 Binder.restoreCallingIdentity(identity);
5150 }
Jake Hambye994d462014-02-03 13:10:13 -08005151 }
5152
5153 /**
5154 * Update the CDMA Preferred Roaming List (PRL) in the radio NV storage.
5155 * Used for device configuration by some CDMA operators.
5156 *
5157 * @param preferredRoamingList byte array containing the new PRL
5158 * @return true on success; false on any failure
5159 */
5160 @Override
5161 public boolean nvWriteCdmaPrl(byte[] preferredRoamingList) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08005162 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
5163 mApp, getDefaultSubscription(), "nvWriteCdmaPrl");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005164
5165 final long identity = Binder.clearCallingIdentity();
5166 try {
5167 if (DBG) log("nvWriteCdmaPrl: value: " + HexDump.toHexString(preferredRoamingList));
5168 Boolean success = (Boolean) sendRequest(CMD_NV_WRITE_CDMA_PRL, preferredRoamingList);
5169 if (DBG) log("nvWriteCdmaPrl: " + (success ? "ok" : "fail"));
5170 return success;
5171 } finally {
5172 Binder.restoreCallingIdentity(identity);
5173 }
Jake Hambye994d462014-02-03 13:10:13 -08005174 }
5175
5176 /**
chen xu6dac5ab2018-10-26 17:39:23 -07005177 * Rollback modem configurations to factory default except some config which are in whitelist.
Jake Hambye994d462014-02-03 13:10:13 -08005178 * Used for device configuration by some CDMA operators.
5179 *
chen xu6dac5ab2018-10-26 17:39:23 -07005180 * @param slotIndex - device slot.
5181 *
Jake Hambye994d462014-02-03 13:10:13 -08005182 * @return true on success; false on any failure
5183 */
5184 @Override
chen xu6dac5ab2018-10-26 17:39:23 -07005185 public boolean resetModemConfig(int slotIndex) {
5186 Phone phone = PhoneFactory.getPhone(slotIndex);
5187 if (phone != null) {
5188 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
5189 mApp, phone.getSubId(), "resetModemConfig");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005190
chen xu6dac5ab2018-10-26 17:39:23 -07005191 final long identity = Binder.clearCallingIdentity();
5192 try {
5193 Boolean success = (Boolean) sendRequest(CMD_RESET_MODEM_CONFIG, null);
5194 if (DBG) log("resetModemConfig:" + ' ' + (success ? "ok" : "fail"));
5195 return success;
5196 } finally {
5197 Binder.restoreCallingIdentity(identity);
5198 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005199 }
chen xu6dac5ab2018-10-26 17:39:23 -07005200 return false;
5201 }
5202
5203 /**
5204 * Generate a radio modem reset. Used for device configuration by some CDMA operators.
5205 *
5206 * @param slotIndex - device slot.
5207 *
5208 * @return true on success; false on any failure
5209 */
5210 @Override
5211 public boolean rebootModem(int slotIndex) {
5212 Phone phone = PhoneFactory.getPhone(slotIndex);
5213 if (phone != null) {
5214 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
5215 mApp, phone.getSubId(), "rebootModem");
5216
5217 final long identity = Binder.clearCallingIdentity();
5218 try {
5219 Boolean success = (Boolean) sendRequest(CMD_MODEM_REBOOT, null);
5220 if (DBG) log("rebootModem:" + ' ' + (success ? "ok" : "fail"));
5221 return success;
5222 } finally {
5223 Binder.restoreCallingIdentity(identity);
5224 }
5225 }
5226 return false;
Jake Hambye994d462014-02-03 13:10:13 -08005227 }
Jake Hamby7c27be32014-03-03 13:25:59 -08005228
Philip P. Moltmann9797cbb2020-01-07 13:45:00 -08005229 public String[] getPcscfAddress(String apnType, String callingPackage,
5230 String callingFeatureId) {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08005231 final Phone defaultPhone = getDefaultPhone();
Philip P. Moltmann9797cbb2020-01-07 13:45:00 -08005232 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(mApp, defaultPhone.getSubId(),
5233 callingPackage, callingFeatureId, "getPcscfAddress")) {
Svet Ganovb320e182015-04-16 12:30:10 -07005234 return new String[0];
5235 }
5236
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005237 final long identity = Binder.clearCallingIdentity();
5238 try {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08005239 return defaultPhone.getPcscfAddress(apnType);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005240 } finally {
5241 Binder.restoreCallingIdentity(identity);
5242 }
Wink Saville36469e72014-06-11 15:17:00 -07005243 }
5244
Brad Ebinger51f743a2017-01-23 13:50:20 -08005245 /**
Grace Jiaaa2eb6b2020-01-09 16:26:08 -08005246 * Toggle IMS disable and enable for the framework to reset it. See {@link #enableIms(int)} and
5247 * {@link #disableIms(int)}.
5248 * @param slotIndex device slot.
5249 */
5250 public void resetIms(int slotIndex) {
5251 enforceModifyPermission();
5252
5253 final long identity = Binder.clearCallingIdentity();
5254 try {
5255 if (mImsResolver == null) {
5256 // may happen if the does not support IMS.
5257 return;
5258 }
5259 mImsResolver.disableIms(slotIndex);
5260 mImsResolver.enableIms(slotIndex);
5261 } finally {
5262 Binder.restoreCallingIdentity(identity);
5263 }
5264 }
5265
5266 /**
Brad Ebinger22bc3e42018-01-16 09:39:35 -08005267 * Enables IMS for the framework. This will trigger IMS registration and ImsFeature capability
5268 * status updates, if not already enabled.
Brad Ebinger51f743a2017-01-23 13:50:20 -08005269 */
Brad Ebinger22bc3e42018-01-16 09:39:35 -08005270 public void enableIms(int slotId) {
Brad Ebinger51f743a2017-01-23 13:50:20 -08005271 enforceModifyPermission();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005272
5273 final long identity = Binder.clearCallingIdentity();
5274 try {
Brad Ebinger05f52c22019-12-05 13:03:21 -08005275 if (mImsResolver == null) {
Brad Ebinger9c0eb502019-01-23 15:06:19 -08005276 // may happen if the device does not support IMS.
5277 return;
5278 }
Brad Ebinger05f52c22019-12-05 13:03:21 -08005279 mImsResolver.enableIms(slotId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005280 } finally {
5281 Binder.restoreCallingIdentity(identity);
5282 }
Brad Ebinger34bef922017-11-09 10:27:08 -08005283 }
5284
5285 /**
Brad Ebinger22bc3e42018-01-16 09:39:35 -08005286 * Disables IMS for the framework. This will trigger IMS de-registration and trigger ImsFeature
5287 * status updates to disabled.
Brad Ebinger34bef922017-11-09 10:27:08 -08005288 */
Brad Ebinger22bc3e42018-01-16 09:39:35 -08005289 public void disableIms(int slotId) {
5290 enforceModifyPermission();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005291
5292 final long identity = Binder.clearCallingIdentity();
5293 try {
Brad Ebinger05f52c22019-12-05 13:03:21 -08005294 if (mImsResolver == null) {
Brad Ebinger9c0eb502019-01-23 15:06:19 -08005295 // may happen if the device does not support IMS.
5296 return;
5297 }
Brad Ebinger05f52c22019-12-05 13:03:21 -08005298 mImsResolver.disableIms(slotId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005299 } finally {
5300 Binder.restoreCallingIdentity(identity);
5301 }
Brad Ebinger22bc3e42018-01-16 09:39:35 -08005302 }
5303
5304 /**
Brad Ebingere3ae65a2020-09-11 12:45:11 -07005305 * Registers for updates to the MmTelFeature connection through the IImsServiceFeatureCallback
5306 * callback.
Brad Ebinger22bc3e42018-01-16 09:39:35 -08005307 */
Brad Ebingere3ae65a2020-09-11 12:45:11 -07005308 @Override
Brad Ebinger6366ce92020-10-01 13:51:05 -07005309 public void registerMmTelFeatureCallback(int slotId, IImsServiceFeatureCallback callback) {
Brad Ebinger34bef922017-11-09 10:27:08 -08005310 enforceModifyPermission();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005311
5312 final long identity = Binder.clearCallingIdentity();
5313 try {
Brad Ebinger05f52c22019-12-05 13:03:21 -08005314 if (mImsResolver == null) {
Brad Ebingere3ae65a2020-09-11 12:45:11 -07005315 throw new ServiceSpecificException(ImsException.CODE_ERROR_UNSUPPORTED_OPERATION,
5316 "Device does not support IMS");
Brad Ebinger9c0eb502019-01-23 15:06:19 -08005317 }
Brad Ebinger6366ce92020-10-01 13:51:05 -07005318 mImsResolver.listenForFeature(slotId, ImsFeature.FEATURE_MMTEL, callback);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005319 } finally {
5320 Binder.restoreCallingIdentity(identity);
5321 }
Brad Ebinger34bef922017-11-09 10:27:08 -08005322 }
Brad Ebinger5f64b052017-12-14 14:26:15 -08005323 /**
Brad Ebingerab47dd42020-05-18 17:52:58 -07005324 * Unregister a previously registered IImsServiceFeatureCallback associated with an ImsFeature.
5325 */
Brad Ebingere3ae65a2020-09-11 12:45:11 -07005326 @Override
5327 public void unregisterImsFeatureCallback(IImsServiceFeatureCallback callback) {
Brad Ebingerab47dd42020-05-18 17:52:58 -07005328 enforceModifyPermission();
5329
5330 final long identity = Binder.clearCallingIdentity();
5331 try {
5332 if (mImsResolver == null) return;
Brad Ebingere3ae65a2020-09-11 12:45:11 -07005333 mImsResolver.unregisterImsFeatureCallback(callback);
Brad Ebingerab47dd42020-05-18 17:52:58 -07005334 } finally {
5335 Binder.restoreCallingIdentity(identity);
5336 }
5337 }
5338
5339 /**
Brad Ebinger5f64b052017-12-14 14:26:15 -08005340 * Returns the {@link IImsRegistration} structure associated with the slotId and feature
Brad Ebinger9c0eb502019-01-23 15:06:19 -08005341 * specified or null if IMS is not supported on the slot specified.
Brad Ebinger5f64b052017-12-14 14:26:15 -08005342 */
5343 public IImsRegistration getImsRegistration(int slotId, int feature) throws RemoteException {
5344 enforceModifyPermission();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005345
5346 final long identity = Binder.clearCallingIdentity();
5347 try {
Brad Ebinger05f52c22019-12-05 13:03:21 -08005348 if (mImsResolver == null) {
Brad Ebinger9c0eb502019-01-23 15:06:19 -08005349 // may happen if the device does not support IMS.
5350 return null;
5351 }
Brad Ebinger05f52c22019-12-05 13:03:21 -08005352 return mImsResolver.getImsRegistration(slotId, feature);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005353 } finally {
5354 Binder.restoreCallingIdentity(identity);
5355 }
Brad Ebinger5f64b052017-12-14 14:26:15 -08005356 }
5357
Brad Ebinger22bc3e42018-01-16 09:39:35 -08005358 /**
5359 * Returns the {@link IImsConfig} structure associated with the slotId and feature
Brad Ebinger9c0eb502019-01-23 15:06:19 -08005360 * specified or null if IMS is not supported on the slot specified.
Brad Ebinger22bc3e42018-01-16 09:39:35 -08005361 */
5362 public IImsConfig getImsConfig(int slotId, int feature) throws RemoteException {
5363 enforceModifyPermission();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005364
5365 final long identity = Binder.clearCallingIdentity();
5366 try {
Brad Ebinger05f52c22019-12-05 13:03:21 -08005367 if (mImsResolver == null) {
Brad Ebinger9c0eb502019-01-23 15:06:19 -08005368 // may happen if the device does not support IMS.
5369 return null;
5370 }
Brad Ebinger05f52c22019-12-05 13:03:21 -08005371 return mImsResolver.getImsConfig(slotId, feature);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005372 } finally {
5373 Binder.restoreCallingIdentity(identity);
5374 }
Brad Ebinger22bc3e42018-01-16 09:39:35 -08005375 }
5376
Brad Ebinger884c07b2018-02-15 16:17:40 -08005377 /**
Brad Ebingerdac2f002018-04-03 15:17:52 -07005378 * Sets the ImsService Package Name that Telephony will bind to.
5379 *
Brad Ebinger05f52c22019-12-05 13:03:21 -08005380 * @param slotIndex the slot ID that the ImsService should bind for.
5381 * @param isCarrierService true if the ImsService is the carrier override, false if the
Brad Ebingerdac2f002018-04-03 15:17:52 -07005382 * ImsService is the device default ImsService.
Brad Ebinger05f52c22019-12-05 13:03:21 -08005383 * @param featureTypes An integer array of feature types associated with a packageName.
5384 * @param packageName The name of the package that the current configuration will be replaced
5385 * with.
Brad Ebingerdac2f002018-04-03 15:17:52 -07005386 * @return true if setting the ImsService to bind to succeeded, false if it did not.
Brad Ebingerdac2f002018-04-03 15:17:52 -07005387 */
Brad Ebinger05f52c22019-12-05 13:03:21 -08005388 public boolean setBoundImsServiceOverride(int slotIndex, boolean isCarrierService,
5389 int[] featureTypes, String packageName) {
5390 int[] subIds = SubscriptionManager.getSubId(slotIndex);
5391 TelephonyPermissions.enforceShellOnly(Binder.getCallingUid(), "setBoundImsServiceOverride");
Brad Ebingerde696de2018-04-06 09:56:40 -07005392 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(mApp,
5393 (subIds != null ? subIds[0] : SubscriptionManager.INVALID_SUBSCRIPTION_ID),
Brad Ebinger05f52c22019-12-05 13:03:21 -08005394 "setBoundImsServiceOverride");
Brad Ebingerde696de2018-04-06 09:56:40 -07005395
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005396 final long identity = Binder.clearCallingIdentity();
5397 try {
Brad Ebinger05f52c22019-12-05 13:03:21 -08005398 if (mImsResolver == null) {
Brad Ebinger9c0eb502019-01-23 15:06:19 -08005399 // may happen if the device does not support IMS.
5400 return false;
5401 }
Brad Ebinger05f52c22019-12-05 13:03:21 -08005402 Map<Integer, String> featureConfig = new HashMap<>();
5403 for (int featureType : featureTypes) {
5404 featureConfig.put(featureType, packageName);
5405 }
5406 return mImsResolver.overrideImsServiceConfiguration(slotIndex, isCarrierService,
5407 featureConfig);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005408 } finally {
5409 Binder.restoreCallingIdentity(identity);
5410 }
Brad Ebingerdac2f002018-04-03 15:17:52 -07005411 }
5412
5413 /**
Brad Ebinger999d3302020-11-25 14:31:39 -08005414 * Clears any carrier ImsService overrides for the slot index specified that were previously
5415 * set with {@link #setBoundImsServiceOverride(int, boolean, int[], String)}.
5416 *
5417 * This should only be used for testing.
5418 *
5419 * @param slotIndex the slot ID that the ImsService should bind for.
5420 * @return true if clearing the carrier ImsService override succeeded or false if it did not.
5421 */
5422 @Override
5423 public boolean clearCarrierImsServiceOverride(int slotIndex) {
5424 int[] subIds = SubscriptionManager.getSubId(slotIndex);
5425 TelephonyPermissions.enforceShellOnly(Binder.getCallingUid(),
5426 "clearCarrierImsServiceOverride");
5427 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(mApp,
5428 (subIds != null ? subIds[0] : SubscriptionManager.INVALID_SUBSCRIPTION_ID),
5429 "clearCarrierImsServiceOverride");
5430
5431 final long identity = Binder.clearCallingIdentity();
5432 try {
5433 if (mImsResolver == null) {
5434 // may happen if the device does not support IMS.
5435 return false;
5436 }
5437 return mImsResolver.clearCarrierImsServiceConfiguration(slotIndex);
5438 } finally {
5439 Binder.restoreCallingIdentity(identity);
5440 }
5441 }
5442
5443 /**
Brad Ebinger05f52c22019-12-05 13:03:21 -08005444 * Return the package name of the currently bound ImsService.
Brad Ebingerdac2f002018-04-03 15:17:52 -07005445 *
5446 * @param slotId The slot that the ImsService is associated with.
5447 * @param isCarrierImsService true, if the ImsService is a carrier override, false if it is
5448 * the device default.
Brad Ebinger05f52c22019-12-05 13:03:21 -08005449 * @param featureType The feature associated with the queried configuration.
Brad Ebingerdac2f002018-04-03 15:17:52 -07005450 * @return the package name of the ImsService configuration.
5451 */
Brad Ebinger05f52c22019-12-05 13:03:21 -08005452 public String getBoundImsServicePackage(int slotId, boolean isCarrierImsService,
5453 @ImsFeature.FeatureType int featureType) {
Brad Ebingerde696de2018-04-06 09:56:40 -07005454 int[] subIds = SubscriptionManager.getSubId(slotId);
Brad Ebinger05f52c22019-12-05 13:03:21 -08005455 TelephonyPermissions
5456 .enforeceCallingOrSelfReadPrivilegedPhoneStatePermissionOrCarrierPrivilege(
5457 mApp, (subIds != null ? subIds[0] : SubscriptionManager.INVALID_SUBSCRIPTION_ID),
5458 "getBoundImsServicePackage");
Brad Ebingerde696de2018-04-06 09:56:40 -07005459
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005460 final long identity = Binder.clearCallingIdentity();
5461 try {
Brad Ebinger05f52c22019-12-05 13:03:21 -08005462 if (mImsResolver == null) {
Brad Ebinger9c0eb502019-01-23 15:06:19 -08005463 // may happen if the device does not support IMS.
5464 return "";
5465 }
Brad Ebingera80c3312019-12-02 10:59:39 -08005466 // TODO: change API to query RCS separately.
Brad Ebinger05f52c22019-12-05 13:03:21 -08005467 return mImsResolver.getImsServiceConfiguration(slotId, isCarrierImsService,
5468 featureType);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005469 } finally {
5470 Binder.restoreCallingIdentity(identity);
5471 }
Brad Ebingerdac2f002018-04-03 15:17:52 -07005472 }
5473
Brad Ebinger77b832e2019-10-17 17:03:22 -07005474 /**
5475 * Get the MmTelFeature state associated with the requested subscription id.
5476 * @param subId The subscription that the MmTelFeature is associated with.
5477 * @param callback A callback with an integer containing the
5478 * {@link android.telephony.ims.feature.ImsFeature.ImsState} associated with the MmTelFeature.
5479 */
5480 @Override
5481 public void getImsMmTelFeatureState(int subId, IIntegerConsumer callback) {
5482 enforceReadPrivilegedPermission("getImsMmTelFeatureState");
5483 if (!ImsManager.isImsSupportedOnDevice(mApp)) {
5484 throw new ServiceSpecificException(ImsException.CODE_ERROR_UNSUPPORTED_OPERATION,
5485 "IMS not available on device.");
5486 }
5487 final long token = Binder.clearCallingIdentity();
5488 try {
5489 int slotId = getSlotIndex(subId);
5490 if (slotId <= SubscriptionManager.INVALID_SIM_SLOT_INDEX) {
5491 Log.w(LOG_TAG, "getImsMmTelFeatureState: called with an inactive subscription '"
5492 + subId + "'");
5493 throw new ServiceSpecificException(ImsException.CODE_ERROR_INVALID_SUBSCRIPTION);
5494 }
5495 ImsManager.getInstance(mApp, slotId).getImsServiceState(anInteger -> {
5496 try {
5497 callback.accept(anInteger == null ? ImsFeature.STATE_UNAVAILABLE : anInteger);
5498 } catch (RemoteException e) {
5499 Log.w(LOG_TAG, "getImsMmTelFeatureState: remote caller is no longer running. "
5500 + "Ignore");
5501 }
5502 });
5503 } finally {
5504 Binder.restoreCallingIdentity(token);
5505 }
5506 }
5507
Wink Saville36469e72014-06-11 15:17:00 -07005508 public void setImsRegistrationState(boolean registered) {
5509 enforceModifyPermission();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005510
5511 final long identity = Binder.clearCallingIdentity();
5512 try {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08005513 getDefaultPhone().setImsRegistrationState(registered);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005514 } finally {
5515 Binder.restoreCallingIdentity(identity);
5516 }
Wink Saville36469e72014-06-11 15:17:00 -07005517 }
5518
5519 /**
Stuart Scott54788802015-03-30 13:18:01 -07005520 * Set the network selection mode to automatic.
5521 *
5522 */
5523 @Override
5524 public void setNetworkSelectionModeAutomatic(int subId) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08005525 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
5526 mApp, subId, "setNetworkSelectionModeAutomatic");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005527
5528 final long identity = Binder.clearCallingIdentity();
5529 try {
shilufc958392020-01-20 11:36:01 -08005530 if (!isActiveSubscription(subId)) {
5531 return;
5532 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005533 if (DBG) log("setNetworkSelectionModeAutomatic: subId " + subId);
5534 sendRequest(CMD_SET_NETWORK_SELECTION_MODE_AUTOMATIC, null, subId);
5535 } finally {
5536 Binder.restoreCallingIdentity(identity);
5537 }
Stuart Scott54788802015-03-30 13:18:01 -07005538 }
5539
Jack Yud10cdd42020-09-28 20:28:01 -07005540 /**
Pengquan Mengea84e042018-09-20 14:57:26 -07005541 * Ask the radio to connect to the input network and change selection mode to manual.
5542 *
5543 * @param subId the id of the subscription.
5544 * @param operatorInfo the operator information, included the PLMN, long name and short name of
5545 * the operator to attach to.
5546 * @param persistSelection whether the selection will persist until reboot. If true, only allows
5547 * attaching to the selected PLMN until reboot; otherwise, attach to the chosen PLMN and resume
5548 * normal network selection next time.
5549 * @return {@code true} on success; {@code true} on any failure.
Shishir Agrawal302c8692015-06-19 13:49:39 -07005550 */
5551 @Override
Pengquan Mengea84e042018-09-20 14:57:26 -07005552 public boolean setNetworkSelectionModeManual(
5553 int subId, OperatorInfo operatorInfo, boolean persistSelection) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08005554 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
5555 mApp, subId, "setNetworkSelectionModeManual");
Pengquan Menge92a50d2018-09-21 15:54:48 -07005556
5557 if (!isActiveSubscription(subId)) {
5558 return false;
5559 }
5560
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005561 final long identity = Binder.clearCallingIdentity();
5562 try {
Pengquan Mengea84e042018-09-20 14:57:26 -07005563 ManualNetworkSelectionArgument arg = new ManualNetworkSelectionArgument(operatorInfo,
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005564 persistSelection);
Pengquan Mengea84e042018-09-20 14:57:26 -07005565 if (DBG) {
5566 log("setNetworkSelectionModeManual: subId: " + subId
5567 + " operator: " + operatorInfo);
5568 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005569 return (Boolean) sendRequest(CMD_SET_NETWORK_SELECTION_MODE_MANUAL, arg, subId);
5570 } finally {
5571 Binder.restoreCallingIdentity(identity);
5572 }
Shishir Agrawal302c8692015-06-19 13:49:39 -07005573 }
shilu84f6e8b2019-12-19 13:58:01 -08005574 /**
5575 * Get the manual network selection
5576 *
5577 * @param subId the id of the subscription.
5578 *
5579 * @return the previously saved user selected PLMN
5580 */
5581 @Override
5582 public String getManualNetworkSelectionPlmn(int subId) {
5583 TelephonyPermissions
5584 .enforeceCallingOrSelfReadPrecisePhoneStatePermissionOrCarrierPrivilege(
5585 mApp, subId, "getManualNetworkSelectionPlmn");
5586
5587 final long identity = Binder.clearCallingIdentity();
5588 try {
5589 if (!isActiveSubscription(subId)) {
5590 return "";
5591 }
5592
5593 final Phone phone = getPhone(subId);
5594 if (phone == null) {
5595 return "";
5596 }
5597 OperatorInfo networkSelection = phone.getSavedNetworkSelection();
5598 return TextUtils.isEmpty(networkSelection.getOperatorNumeric())
5599 ? phone.getManualNetworkSelectionPlmn() : networkSelection.getOperatorNumeric();
5600 } finally {
5601 Binder.restoreCallingIdentity(identity);
5602 }
5603 }
Shishir Agrawal302c8692015-06-19 13:49:39 -07005604
5605 /**
5606 * Scans for available networks.
5607 */
5608 @Override
Philip P. Moltmann9797cbb2020-01-07 13:45:00 -08005609 public CellNetworkScanResult getCellNetworkScanResults(int subId, String callingPackage,
5610 String callingFeatureId) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08005611 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
5612 mApp, subId, "getCellNetworkScanResults");
Hall Liuf19c44f2018-11-27 14:38:17 -08005613 LocationAccessPolicy.LocationPermissionResult locationResult =
5614 LocationAccessPolicy.checkLocationPermission(mApp,
5615 new LocationAccessPolicy.LocationPermissionQuery.Builder()
5616 .setCallingPackage(callingPackage)
Philip P. Moltmann9797cbb2020-01-07 13:45:00 -08005617 .setCallingFeatureId(callingFeatureId)
Hall Liuf19c44f2018-11-27 14:38:17 -08005618 .setCallingPid(Binder.getCallingPid())
5619 .setCallingUid(Binder.getCallingUid())
5620 .setMethod("getCellNetworkScanResults")
5621 .setMinSdkVersionForFine(Build.VERSION_CODES.Q)
5622 .build());
5623 switch (locationResult) {
5624 case DENIED_HARD:
5625 throw new SecurityException("Not allowed to access scan results -- location");
5626 case DENIED_SOFT:
5627 return null;
5628 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005629
Pengquan Menga1bb6272018-09-06 09:59:22 -07005630 long identity = Binder.clearCallingIdentity();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005631 try {
5632 if (DBG) log("getCellNetworkScanResults: subId " + subId);
Pengquan Menga1bb6272018-09-06 09:59:22 -07005633 return (CellNetworkScanResult) sendRequest(
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005634 CMD_PERFORM_NETWORK_SCAN, null, subId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005635 } finally {
5636 Binder.restoreCallingIdentity(identity);
5637 }
Shishir Agrawal302c8692015-06-19 13:49:39 -07005638 }
5639
5640 /**
sqian80370722020-01-29 15:02:51 -08005641 * Get the call forwarding info, given the call forwarding reason.
5642 */
5643 @Override
Hall Liua1acea22020-09-18 19:04:59 -07005644 public void getCallForwarding(int subId, int callForwardingReason,
5645 ICallForwardingInfoCallback callback) {
sqian80370722020-01-29 15:02:51 -08005646 enforceReadPrivilegedPermission("getCallForwarding");
5647 long identity = Binder.clearCallingIdentity();
5648 try {
5649 if (DBG) {
5650 log("getCallForwarding: subId " + subId
5651 + " callForwardingReason" + callForwardingReason);
5652 }
Hall Liua1acea22020-09-18 19:04:59 -07005653
5654 Phone phone = getPhone(subId);
5655 if (phone == null) {
5656 try {
Hall Liuae527aa2020-09-29 17:10:18 -07005657 callback.onError(
5658 TelephonyManager.CallForwardingInfoCallback.RESULT_ERROR_UNKNOWN);
Hall Liua1acea22020-09-18 19:04:59 -07005659 } catch (RemoteException e) {
5660 // ignore
5661 }
5662 return;
5663 }
5664
5665 Pair<Integer, TelephonyManager.CallForwardingInfoCallback> argument = Pair.create(
5666 callForwardingReason, new TelephonyManager.CallForwardingInfoCallback() {
5667 @Override
5668 public void onCallForwardingInfoAvailable(CallForwardingInfo info) {
5669 try {
5670 callback.onCallForwardingInfoAvailable(info);
5671 } catch (RemoteException e) {
5672 // ignore
5673 }
5674 }
5675
5676 @Override
5677 public void onError(int error) {
5678 try {
5679 callback.onError(error);
5680 } catch (RemoteException e) {
5681 // ignore
5682 }
5683 }
5684 });
5685 sendRequestAsync(CMD_GET_CALL_FORWARDING, argument, phone, null);
sqian80370722020-01-29 15:02:51 -08005686 } finally {
5687 Binder.restoreCallingIdentity(identity);
5688 }
5689 }
5690
5691 /**
5692 * Sets the voice call forwarding info including status (enable/disable), call forwarding
5693 * reason, the number to forward, and the timeout before the forwarding is attempted.
5694 */
5695 @Override
Hall Liua1acea22020-09-18 19:04:59 -07005696 public void setCallForwarding(int subId, CallForwardingInfo callForwardingInfo,
5697 IIntegerConsumer callback) {
sqian80370722020-01-29 15:02:51 -08005698 enforceModifyPermission();
5699 long identity = Binder.clearCallingIdentity();
5700 try {
5701 if (DBG) {
5702 log("setCallForwarding: subId " + subId
5703 + " callForwardingInfo" + callForwardingInfo);
5704 }
Hall Liua1acea22020-09-18 19:04:59 -07005705
5706 Phone phone = getPhone(subId);
5707 if (phone == null) {
5708 try {
Hall Liuae527aa2020-09-29 17:10:18 -07005709 callback.accept(
5710 TelephonyManager.CallForwardingInfoCallback.RESULT_ERROR_UNKNOWN);
Hall Liua1acea22020-09-18 19:04:59 -07005711 } catch (RemoteException e) {
5712 // ignore
5713 }
5714 return;
5715 }
5716
5717 Pair<CallForwardingInfo, Consumer<Integer>> arguments = Pair.create(callForwardingInfo,
5718 FunctionalUtils.ignoreRemoteException(callback::accept));
5719
5720 sendRequestAsync(CMD_SET_CALL_FORWARDING, arguments, phone, null);
sqian80370722020-01-29 15:02:51 -08005721 } finally {
5722 Binder.restoreCallingIdentity(identity);
5723 }
5724 }
5725
5726 /**
Hall Liua1acea22020-09-18 19:04:59 -07005727 * Get the call waiting status for a subId.
sqian80370722020-01-29 15:02:51 -08005728 */
5729 @Override
Hall Liua1acea22020-09-18 19:04:59 -07005730 public void getCallWaitingStatus(int subId, IIntegerConsumer callback) {
sqian80370722020-01-29 15:02:51 -08005731 enforceReadPrivilegedPermission("getCallForwarding");
5732 long identity = Binder.clearCallingIdentity();
5733 try {
Hall Liua1acea22020-09-18 19:04:59 -07005734
5735 Phone phone = getPhone(subId);
5736 if (phone == null) {
5737 try {
5738 callback.accept(TelephonyManager.CALL_WAITING_STATUS_UNKNOWN_ERROR);
5739 } catch (RemoteException e) {
5740 // ignore
5741 }
5742 return;
5743 }
5744
5745 Consumer<Integer> argument = FunctionalUtils.ignoreRemoteException(callback::accept);
5746
sqian80370722020-01-29 15:02:51 -08005747 if (DBG) log("getCallWaitingStatus: subId " + subId);
Hall Liua1acea22020-09-18 19:04:59 -07005748 sendRequestAsync(CMD_GET_CALL_WAITING, argument, phone, null);
sqian80370722020-01-29 15:02:51 -08005749 } finally {
5750 Binder.restoreCallingIdentity(identity);
5751 }
5752 }
5753
5754 /**
Hall Liua1acea22020-09-18 19:04:59 -07005755 * Sets whether call waiting is enabled for a given subId.
sqian80370722020-01-29 15:02:51 -08005756 */
5757 @Override
Hall Liua1acea22020-09-18 19:04:59 -07005758 public void setCallWaitingStatus(int subId, boolean enable, IIntegerConsumer callback) {
sqian80370722020-01-29 15:02:51 -08005759 enforceModifyPermission();
5760 long identity = Binder.clearCallingIdentity();
5761 try {
Hall Liua1acea22020-09-18 19:04:59 -07005762 if (DBG) log("setCallWaitingStatus: subId " + subId + " enable: " + enable);
5763
5764 Phone phone = getPhone(subId);
5765 if (phone == null) {
5766 try {
5767 callback.accept(TelephonyManager.CALL_WAITING_STATUS_UNKNOWN_ERROR);
5768 } catch (RemoteException e) {
5769 // ignore
5770 }
5771 return;
5772 }
5773
5774 Pair<Boolean, Consumer<Integer>> arguments = Pair.create(enable,
5775 FunctionalUtils.ignoreRemoteException(callback::accept));
5776
5777 sendRequestAsync(CMD_SET_CALL_WAITING, arguments, phone, null);
sqian80370722020-01-29 15:02:51 -08005778 } finally {
5779 Binder.restoreCallingIdentity(identity);
5780 }
5781 }
5782
5783 /**
yinxub1bed742017-04-17 11:45:04 -07005784 * Starts a new network scan and returns the id of this scan.
yinxu504e1392017-04-12 16:03:22 -07005785 *
yinxub1bed742017-04-17 11:45:04 -07005786 * @param subId id of the subscription
5787 * @param request contains the radio access networks with bands/channels to scan
5788 * @param messenger callback messenger for scan results or errors
5789 * @param binder for the purpose of auto clean when the user thread crashes
yinxu504e1392017-04-12 16:03:22 -07005790 * @return the id of the requested scan which can be used to stop the scan.
5791 */
5792 @Override
5793 public int requestNetworkScan(int subId, NetworkScanRequest request, Messenger messenger,
Philip P. Moltmann9797cbb2020-01-07 13:45:00 -08005794 IBinder binder, String callingPackage, String callingFeatureId) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08005795 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
5796 mApp, subId, "requestNetworkScan");
Hall Liuf19c44f2018-11-27 14:38:17 -08005797 LocationAccessPolicy.LocationPermissionResult locationResult =
5798 LocationAccessPolicy.checkLocationPermission(mApp,
5799 new LocationAccessPolicy.LocationPermissionQuery.Builder()
5800 .setCallingPackage(callingPackage)
Philip P. Moltmann9797cbb2020-01-07 13:45:00 -08005801 .setCallingFeatureId(callingFeatureId)
Hall Liuf19c44f2018-11-27 14:38:17 -08005802 .setCallingPid(Binder.getCallingPid())
5803 .setCallingUid(Binder.getCallingUid())
5804 .setMethod("requestNetworkScan")
5805 .setMinSdkVersionForFine(Build.VERSION_CODES.Q)
5806 .build());
Hall Liub2ac8ef2019-02-28 15:56:23 -08005807 if (locationResult != LocationAccessPolicy.LocationPermissionResult.ALLOWED) {
Nathan Harold284aa042020-09-22 17:54:53 -07005808 SecurityException e = checkNetworkRequestForSanitizedLocationAccess(
5809 request, subId, callingPackage);
Hall Liub2ac8ef2019-02-28 15:56:23 -08005810 if (e != null) {
5811 if (locationResult == LocationAccessPolicy.LocationPermissionResult.DENIED_HARD) {
5812 throw e;
5813 } else {
Hall Liu0e5abaf2019-04-04 01:25:30 -07005814 loge(e.getMessage());
Hall Liub2ac8ef2019-02-28 15:56:23 -08005815 return TelephonyScanManager.INVALID_SCAN_ID;
5816 }
5817 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005818 }
Hall Liu912dfd32019-04-25 14:02:26 -07005819 int callingUid = Binder.getCallingUid();
5820 int callingPid = Binder.getCallingPid();
Ying Xu94a46582019-04-18 17:14:56 -07005821 final long identity = Binder.clearCallingIdentity();
5822 try {
5823 return mNetworkScanRequestTracker.startNetworkScan(
5824 request, messenger, binder, getPhone(subId),
Hall Liu912dfd32019-04-25 14:02:26 -07005825 callingUid, callingPid, callingPackage);
Ying Xu94a46582019-04-18 17:14:56 -07005826 } finally {
5827 Binder.restoreCallingIdentity(identity);
5828 }
yinxu504e1392017-04-12 16:03:22 -07005829 }
5830
Hall Liub2ac8ef2019-02-28 15:56:23 -08005831 private SecurityException checkNetworkRequestForSanitizedLocationAccess(
Nathan Harold284aa042020-09-22 17:54:53 -07005832 NetworkScanRequest request, int subId, String callingPackage) {
5833 boolean hasCarrierPriv = checkCarrierPrivilegesForPackage(subId, callingPackage)
Hall Liu558027f2019-05-15 19:14:05 -07005834 == TelephonyManager.CARRIER_PRIVILEGE_STATUS_HAS_ACCESS;
5835 boolean hasNetworkScanPermission =
5836 mApp.checkCallingOrSelfPermission(android.Manifest.permission.NETWORK_SCAN)
5837 == PERMISSION_GRANTED;
5838
5839 if (!hasCarrierPriv && !hasNetworkScanPermission) {
5840 return new SecurityException("permission.NETWORK_SCAN or carrier privileges is needed"
5841 + " for network scans without location access.");
Hall Liub2ac8ef2019-02-28 15:56:23 -08005842 }
5843
5844 if (request.getSpecifiers() != null && request.getSpecifiers().length > 0) {
5845 for (RadioAccessSpecifier ras : request.getSpecifiers()) {
Hall Liub2ac8ef2019-02-28 15:56:23 -08005846 if (ras.getChannels() != null && ras.getChannels().length > 0) {
5847 return new SecurityException("Specific channels must not be"
5848 + " scanned without location access.");
5849 }
5850 }
5851 }
5852
Hall Liub2ac8ef2019-02-28 15:56:23 -08005853 return null;
5854 }
5855
yinxu504e1392017-04-12 16:03:22 -07005856 /**
5857 * Stops an existing network scan with the given scanId.
yinxub1bed742017-04-17 11:45:04 -07005858 *
5859 * @param subId id of the subscription
5860 * @param scanId id of the scan that needs to be stopped
yinxu504e1392017-04-12 16:03:22 -07005861 */
5862 @Override
5863 public void stopNetworkScan(int subId, int scanId) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08005864 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
5865 mApp, subId, "stopNetworkScan");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005866
Hall Liu912dfd32019-04-25 14:02:26 -07005867 int callingUid = Binder.getCallingUid();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005868 final long identity = Binder.clearCallingIdentity();
5869 try {
Hall Liu912dfd32019-04-25 14:02:26 -07005870 mNetworkScanRequestTracker.stopNetworkScan(scanId, callingUid);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005871 } finally {
5872 Binder.restoreCallingIdentity(identity);
5873 }
yinxu504e1392017-04-12 16:03:22 -07005874 }
5875
5876 /**
Junda Liu84d15a22014-07-02 11:21:04 -07005877 * Get the calculated preferred network type.
5878 * Used for debugging incorrect network type.
5879 *
5880 * @return the preferred network type, defined in RILConstants.java.
5881 */
5882 @Override
Philip P. Moltmann9797cbb2020-01-07 13:45:00 -08005883 public int getCalculatedPreferredNetworkType(String callingPackage, String callingFeatureId) {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08005884 final Phone defaultPhone = getDefaultPhone();
5885 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(mApp, defaultPhone.getSubId(),
Philip P. Moltmann9797cbb2020-01-07 13:45:00 -08005886 callingPackage, callingFeatureId, "getCalculatedPreferredNetworkType")) {
Svet Ganovb320e182015-04-16 12:30:10 -07005887 return RILConstants.PREFERRED_NETWORK_MODE;
5888 }
5889
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005890 final long identity = Binder.clearCallingIdentity();
5891 try {
5892 // FIXME: need to get SubId from somewhere.
Nazanin Bakhshif782e562018-12-11 15:15:39 -08005893 return PhoneFactory.calculatePreferredNetworkType(defaultPhone.getContext(), 0);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005894 } finally {
5895 Binder.restoreCallingIdentity(identity);
5896 }
Junda Liu84d15a22014-07-02 11:21:04 -07005897 }
5898
5899 /**
Jake Hamby7c27be32014-03-03 13:25:59 -08005900 * Get the preferred network type.
5901 * Used for device configuration by some CDMA operators.
5902 *
5903 * @return the preferred network type, defined in RILConstants.java.
5904 */
5905 @Override
Stuart Scott54788802015-03-30 13:18:01 -07005906 public int getPreferredNetworkType(int subId) {
Pengquan Menga4009cb2018-12-20 11:00:24 -08005907 TelephonyPermissions
5908 .enforeceCallingOrSelfReadPrivilegedPhoneStatePermissionOrCarrierPrivilege(
5909 mApp, subId, "getPreferredNetworkType");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005910
5911 final long identity = Binder.clearCallingIdentity();
5912 try {
5913 if (DBG) log("getPreferredNetworkType");
5914 int[] result = (int[]) sendRequest(CMD_GET_PREFERRED_NETWORK_TYPE, null, subId);
5915 int networkType = (result != null ? result[0] : -1);
5916 if (DBG) log("getPreferredNetworkType: " + networkType);
5917 return networkType;
5918 } finally {
5919 Binder.restoreCallingIdentity(identity);
5920 }
Jake Hamby7c27be32014-03-03 13:25:59 -08005921 }
5922
5923 /**
5924 * Set the preferred network type.
Jake Hamby7c27be32014-03-03 13:25:59 -08005925 *
5926 * @param networkType the preferred network type, defined in RILConstants.java.
5927 * @return true on success; false on any failure.
5928 */
5929 @Override
Stuart Scott54788802015-03-30 13:18:01 -07005930 public boolean setPreferredNetworkType(int subId, int networkType) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08005931 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
5932 mApp, subId, "setPreferredNetworkType");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005933
5934 final long identity = Binder.clearCallingIdentity();
5935 try {
calvinpan677fc2b2020-01-14 20:42:55 +08005936 Settings.Global.putInt(mApp.getContentResolver(),
5937 Settings.Global.PREFERRED_NETWORK_MODE + subId, networkType);
calvinpan089c2a62020-03-12 14:17:55 +08005938
5939 Boolean success = (Boolean) sendRequest(
5940 CMD_SET_PREFERRED_NETWORK_TYPE, networkType, subId);
5941 if (DBG) log("setPreferredNetworkType: " + (success ? "ok" : "fail"));
5942 return success;
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005943 } finally {
5944 Binder.restoreCallingIdentity(identity);
Junda Liu80bc0d12014-07-14 16:36:44 -07005945 }
Jake Hamby7c27be32014-03-03 13:25:59 -08005946 }
Robert Greenwalted86e582014-05-21 20:03:20 -07005947
5948 /**
calvinpan677fc2b2020-01-14 20:42:55 +08005949 * Get the allowed network types that store in the telephony provider.
5950 *
5951 * @param subId the id of the subscription.
5952 * @return allowedNetworkTypes the allowed network types.
5953 */
5954 @Override
5955 public long getAllowedNetworkTypes(int subId) {
5956 TelephonyPermissions
5957 .enforeceCallingOrSelfReadPrivilegedPhoneStatePermissionOrCarrierPrivilege(
5958 mApp, subId, "getAllowedNetworkTypes");
5959
5960 final long identity = Binder.clearCallingIdentity();
5961 try {
5962 return SubscriptionManager.getLongSubscriptionProperty(
5963 subId, SubscriptionManager.ALLOWED_NETWORK_TYPES, -1, mApp);
5964 } finally {
5965 Binder.restoreCallingIdentity(identity);
5966 }
5967 }
5968
5969 /**
5970 * Set the allowed network types.
5971 *
5972 * @param subId the id of the subscription.
5973 * @param allowedNetworkTypes the allowed network types.
5974 * @return true on success; false on any failure.
5975 */
5976 @Override
5977 public boolean setAllowedNetworkTypes(int subId, long allowedNetworkTypes) {
5978 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
5979 mApp, subId, "setAllowedNetworkTypes");
calvinpan677fc2b2020-01-14 20:42:55 +08005980
calvinpan089c2a62020-03-12 14:17:55 +08005981 SubscriptionManager.setSubscriptionProperty(subId,
5982 SubscriptionManager.ALLOWED_NETWORK_TYPES,
5983 String.valueOf(allowedNetworkTypes));
calvinpan677fc2b2020-01-14 20:42:55 +08005984
calvinpan089c2a62020-03-12 14:17:55 +08005985 int preferredNetworkMode = Settings.Global.getInt(mApp.getContentResolver(),
5986 Settings.Global.PREFERRED_NETWORK_MODE + subId,
5987 RILConstants.PREFERRED_NETWORK_MODE);
5988 return setPreferredNetworkType(subId, preferredNetworkMode);
calvinpan677fc2b2020-01-14 20:42:55 +08005989 }
5990
5991 /**
Sooraj Sasindran9e2be4e2020-06-03 01:06:00 -07005992 * Get the allowed network types for certain reason.
5993 *
5994 * @param subId the id of the subscription.
5995 * @param reason the reason the allowed network type change is taking place
5996 * @return the allowed network types.
5997 */
5998 @Override
5999 public long getAllowedNetworkTypesForReason(int subId,
6000 @TelephonyManager.AllowedNetworkTypesReason int reason) {
6001 TelephonyPermissions
6002 .enforeceCallingOrSelfReadPrivilegedPhoneStatePermissionOrCarrierPrivilege(
6003 mApp, subId, "getAllowedNetworkTypesForReason");
6004 final long identity = Binder.clearCallingIdentity();
6005 try {
6006 return getPhoneFromSubId(subId).getAllowedNetworkTypes(reason);
6007 } finally {
6008 Binder.restoreCallingIdentity(identity);
6009 }
6010 }
6011
6012 /**
Sooraj Sasindranb4a99602020-08-11 10:40:43 -07006013 * Enable/Disable E-UTRA-NR Dual Connectivity
6014 * @param subId subscription id of the sim card
6015 * @param nrDualConnectivityState expected NR dual connectivity state
6016 * This can be passed following states
6017 * <ol>
6018 * <li>Enable NR dual connectivity {@link TelephonyManager#NR_DUAL_CONNECTIVITY_ENABLE}
6019 * <li>Disable NR dual connectivity {@link TelephonyManager#NR_DUAL_CONNECTIVITY_DISABLE}
6020 * <li>Disable NR dual connectivity and force secondary cell to be released
6021 * {@link TelephonyManager#NR_DUAL_CONNECTIVITY_DISABLE_IMMEDIATE}
6022 * </ol>
6023 * @return operation result.
6024 */
6025 @Override
6026 public int setNrDualConnectivityState(int subId,
6027 @TelephonyManager.NrDualConnectivityState int nrDualConnectivityState) {
6028 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
6029 mApp, subId, "enableNRDualConnectivity");
6030 WorkSource workSource = getWorkSource(Binder.getCallingUid());
6031 final long identity = Binder.clearCallingIdentity();
6032 try {
6033 int result = (int) sendRequest(CMD_ENABLE_NR_DUAL_CONNECTIVITY,
6034 nrDualConnectivityState, subId,
6035 workSource);
6036 if (DBG) log("enableNRDualConnectivity result: " + result);
6037 return result;
6038 } finally {
6039 Binder.restoreCallingIdentity(identity);
6040 }
6041 }
6042
6043 /**
6044 * Is E-UTRA-NR Dual Connectivity enabled
6045 * @return true if dual connectivity is enabled else false
6046 */
6047 @Override
6048 public boolean isNrDualConnectivityEnabled(int subId) {
6049 TelephonyPermissions
6050 .enforeceCallingOrSelfReadPrivilegedPhoneStatePermissionOrCarrierPrivilege(
6051 mApp, subId, "isNRDualConnectivityEnabled");
6052 WorkSource workSource = getWorkSource(Binder.getCallingUid());
6053 final long identity = Binder.clearCallingIdentity();
6054 try {
6055 boolean isEnabled = (boolean) sendRequest(CMD_IS_NR_DUAL_CONNECTIVITY_ENABLED,
6056 null, subId, workSource);
6057 if (DBG) log("isNRDualConnectivityEnabled: " + isEnabled);
6058 return isEnabled;
6059 } finally {
6060 Binder.restoreCallingIdentity(identity);
6061 }
6062 }
6063
6064 /**
Sooraj Sasindran1f812e02020-10-30 13:17:53 -07006065 * get carrier bandwidth per primary and secondary carrier
6066 * @param subId subscription id of the sim card
6067 * @return CarrierBandwidth with bandwidth of both primary and secondary carrier..
6068 */
6069 @Override
6070 public CarrierBandwidth getCarrierBandwidth(int subId) {
6071 TelephonyPermissions
6072 .enforeceCallingOrSelfReadPrivilegedPhoneStatePermissionOrCarrierPrivilege(
6073 mApp, subId, "isNRDualConnectivityEnabled");
6074 WorkSource workSource = getWorkSource(Binder.getCallingUid());
6075 final long identity = Binder.clearCallingIdentity();
6076 try {
6077 CarrierBandwidth carrierBandwidth =
6078 getPhoneFromSubId(subId).getCarrierBandwidth();
6079 if (DBG) log("getCarrierBandwidth: " + carrierBandwidth);
6080 return carrierBandwidth;
6081 } finally {
6082 Binder.restoreCallingIdentity(identity);
6083 }
6084 }
6085
6086 /**
Sooraj Sasindran9e2be4e2020-06-03 01:06:00 -07006087 * Get the effective allowed network types on the device.
6088 * This API will return an intersection of allowed network types for all reasons,
6089 * including the configuration done through setAllowedNetworkTypes
6090 *
6091 * @param subId the id of the subscription.
6092 * @return the allowed network types
6093 */
6094 @Override
6095 public long getEffectiveAllowedNetworkTypes(int subId) {
6096 TelephonyPermissions
6097 .enforeceCallingOrSelfReadPrivilegedPhoneStatePermissionOrCarrierPrivilege(
6098 mApp, subId, "getEffectiveAllowedNetworkTypes");
6099 final long identity = Binder.clearCallingIdentity();
6100 try {
6101 return getPhoneFromSubId(subId).getEffectiveAllowedNetworkTypes();
6102 } finally {
6103 Binder.restoreCallingIdentity(identity);
6104 }
6105 }
6106
6107 /**
6108 * Set the allowed network types of the device and
6109 * provide the reason triggering the allowed network change.
6110 *
6111 * @param subId the id of the subscription.
6112 * @param reason the reason the allowed network type change is taking place
6113 * @param allowedNetworkTypes the allowed network types.
6114 * @return true on success; false on any failure.
6115 */
6116 @Override
6117 public boolean setAllowedNetworkTypesForReason(int subId,
6118 @TelephonyManager.AllowedNetworkTypesReason int reason, long allowedNetworkTypes) {
6119 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
6120 mApp, subId, "setAllowedNetworkTypesForReason");
6121 final long identity = Binder.clearCallingIdentity();
6122 try {
6123 getPhoneFromSubId(subId).setAllowedNetworkTypes(reason, allowedNetworkTypes);
6124 int preferredNetworkMode = Settings.Global.getInt(mApp.getContentResolver(),
6125 Settings.Global.PREFERRED_NETWORK_MODE + subId,
6126 RILConstants.PREFERRED_NETWORK_MODE);
6127 return setPreferredNetworkType(subId, preferredNetworkMode);
6128 } finally {
6129 Binder.restoreCallingIdentity(identity);
6130 }
6131 }
6132
6133 /**
Miaoa84611c2019-03-15 09:21:10 +08006134 * Check whether DUN APN is required for tethering with subId.
Junda Liu475951f2014-11-07 16:45:03 -08006135 *
Miaoa84611c2019-03-15 09:21:10 +08006136 * @param subId the id of the subscription to require tethering.
Amit Mahajanfe58cdf2017-07-11 12:01:53 -07006137 * @return {@code true} if DUN APN is required for tethering.
Junda Liu475951f2014-11-07 16:45:03 -08006138 * @hide
6139 */
6140 @Override
SongFerngWangf08d8122019-11-15 14:58:44 +08006141 public boolean isTetheringApnRequiredForSubscriber(int subId) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08006142 enforceModifyPermission();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006143 final long identity = Binder.clearCallingIdentity();
Miaoa84611c2019-03-15 09:21:10 +08006144 final Phone phone = getPhone(subId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006145 try {
Miaoa84611c2019-03-15 09:21:10 +08006146 if (phone != null) {
6147 return phone.hasMatchedTetherApnSetting();
6148 } else {
6149 return false;
6150 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006151 } finally {
6152 Binder.restoreCallingIdentity(identity);
Junda Liu475951f2014-11-07 16:45:03 -08006153 }
Junda Liu475951f2014-11-07 16:45:03 -08006154 }
6155
6156 /**
Shuo Qian5bac1ee2020-01-16 20:51:11 -08006157 * Enable or disable always reporting signal strength changes from radio.
6158 *
6159 * @param isEnable {@code true} for enabling; {@code false} for disabling.
6160 */
6161 @Override
6162 public void setAlwaysReportSignalStrength(int subId, boolean isEnable) {
6163 enforceModifyPermission();
6164 enforceSystemCaller();
6165
6166 final long identity = Binder.clearCallingIdentity();
6167 final Phone phone = getPhone(subId);
6168 try {
6169 if (phone != null) {
6170 if (DBG) {
6171 log("setAlwaysReportSignalStrength: subId=" + subId
6172 + " isEnable=" + isEnable);
6173 }
6174 phone.setAlwaysReportSignalStrength(isEnable);
6175 } else {
6176 loge("setAlwaysReportSignalStrength: no phone found for subId="
6177 + subId);
6178 }
6179 } finally {
6180 Binder.restoreCallingIdentity(identity);
6181 }
6182 }
6183
6184 /**
Malcolm Chen964682d2017-11-28 16:20:07 -08006185 * Get the user enabled state of Mobile Data.
6186 *
6187 * TODO: remove and use isUserDataEnabled.
6188 * This can't be removed now because some vendor codes
6189 * calls through ITelephony directly while they should
6190 * use TelephonyManager.
6191 *
6192 * @return true on enabled
6193 */
6194 @Override
6195 public boolean getDataEnabled(int subId) {
6196 return isUserDataEnabled(subId);
6197 }
6198
6199 /**
6200 * Get whether mobile data is enabled per user setting.
6201 *
6202 * There are other factors deciding whether mobile data is actually enabled, but they are
6203 * not considered here. See {@link #isDataEnabled(int)} for more details.
Robert Greenwalt646120a2014-05-23 11:54:03 -07006204 *
Jeff Davidsona1920712016-11-18 17:05:56 -08006205 * Accepts either ACCESS_NETWORK_STATE, MODIFY_PHONE_STATE or carrier privileges.
Robert Greenwalted86e582014-05-21 20:03:20 -07006206 *
6207 * @return {@code true} if data is enabled else {@code false}
6208 */
6209 @Override
Malcolm Chen964682d2017-11-28 16:20:07 -08006210 public boolean isUserDataEnabled(int subId) {
Robert Greenwalt646120a2014-05-23 11:54:03 -07006211 try {
6212 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.ACCESS_NETWORK_STATE,
6213 null);
6214 } catch (Exception e) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08006215 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
6216 mApp, subId, "isUserDataEnabled");
Robert Greenwalt646120a2014-05-23 11:54:03 -07006217 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006218
6219 final long identity = Binder.clearCallingIdentity();
6220 try {
6221 int phoneId = mSubscriptionController.getPhoneId(subId);
6222 if (DBG) log("isUserDataEnabled: subId=" + subId + " phoneId=" + phoneId);
6223 Phone phone = PhoneFactory.getPhone(phoneId);
6224 if (phone != null) {
6225 boolean retVal = phone.isUserDataEnabled();
6226 if (DBG) log("isUserDataEnabled: subId=" + subId + " retVal=" + retVal);
6227 return retVal;
6228 } else {
6229 if (DBG) loge("isUserDataEnabled: no phone subId=" + subId + " retVal=false");
6230 return false;
6231 }
6232 } finally {
6233 Binder.restoreCallingIdentity(identity);
Malcolm Chen964682d2017-11-28 16:20:07 -08006234 }
6235 }
6236
6237 /**
Shuo Qian985d1232020-01-08 14:30:06 -08006238 * Checks if the device is capable of mobile data by considering whether whether the
6239 * user has enabled mobile data, whether the carrier has enabled mobile data, and
6240 * whether the network policy allows data connections.
Malcolm Chen964682d2017-11-28 16:20:07 -08006241 *
Shuo Qian985d1232020-01-08 14:30:06 -08006242 * @return {@code true} if the overall data connection is capable; {@code false} if not.
Malcolm Chen964682d2017-11-28 16:20:07 -08006243 */
6244 @Override
6245 public boolean isDataEnabled(int subId) {
Sooraj Sasindranff75de62020-07-15 01:35:24 -07006246 try {
6247 try {
6248 mApp.enforceCallingOrSelfPermission(
6249 android.Manifest.permission.ACCESS_NETWORK_STATE,
6250 null);
6251 } catch (Exception e) {
6252 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.READ_PHONE_STATE,
6253 "isDataEnabled");
6254 }
6255 } catch (Exception e) {
6256 enforceReadPrivilegedPermission("isDataEnabled");
6257 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006258
6259 final long identity = Binder.clearCallingIdentity();
6260 try {
6261 int phoneId = mSubscriptionController.getPhoneId(subId);
6262 if (DBG) log("isDataEnabled: subId=" + subId + " phoneId=" + phoneId);
6263 Phone phone = PhoneFactory.getPhone(phoneId);
6264 if (phone != null) {
Jack Yud79fba22018-12-13 11:51:28 -08006265 boolean retVal = phone.getDataEnabledSettings().isDataEnabled();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006266 if (DBG) log("isDataEnabled: subId=" + subId + " retVal=" + retVal);
6267 return retVal;
6268 } else {
6269 if (DBG) loge("isDataEnabled: no phone subId=" + subId + " retVal=false");
6270 return false;
6271 }
6272 } finally {
6273 Binder.restoreCallingIdentity(identity);
Wink Savillee7353bb2014-12-05 14:21:41 -08006274 }
Robert Greenwalted86e582014-05-21 20:03:20 -07006275 }
Shishir Agrawal60f9c952014-06-23 12:00:43 -07006276
Sooraj Sasindran2675c0b2020-04-19 18:14:03 -07006277 /**
6278 * Check if data is enabled for a specific reason
6279 * @param subId Subscription index
6280 * @param reason the reason the data enable change is taking place
6281 * @return {@code true} if the overall data is enabled; {@code false} if not.
6282 */
6283 @Override
Sooraj Sasindranff75de62020-07-15 01:35:24 -07006284 public boolean isDataEnabledForReason(int subId,
Sooraj Sasindran2675c0b2020-04-19 18:14:03 -07006285 @TelephonyManager.DataEnabledReason int reason) {
6286 try {
6287 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.ACCESS_NETWORK_STATE,
6288 null);
6289 } catch (Exception e) {
6290 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.READ_PHONE_STATE,
Sooraj Sasindranff75de62020-07-15 01:35:24 -07006291 "isDataEnabledForReason");
Sooraj Sasindran2675c0b2020-04-19 18:14:03 -07006292 }
6293
6294
6295 final long identity = Binder.clearCallingIdentity();
6296 try {
6297 int phoneId = mSubscriptionController.getPhoneId(subId);
6298 if (DBG) {
Sooraj Sasindranff75de62020-07-15 01:35:24 -07006299 log("isDataEnabledForReason: subId=" + subId + " phoneId=" + phoneId
Sooraj Sasindran2675c0b2020-04-19 18:14:03 -07006300 + " reason=" + reason);
6301 }
6302 Phone phone = PhoneFactory.getPhone(phoneId);
6303 if (phone != null) {
6304 boolean retVal;
6305 if (reason == TelephonyManager.DATA_ENABLED_REASON_USER) {
6306 retVal = phone.isUserDataEnabled();
6307 } else {
Sooraj Sasindranff75de62020-07-15 01:35:24 -07006308 retVal = phone.getDataEnabledSettings().isDataEnabledForReason(reason);
Sooraj Sasindran2675c0b2020-04-19 18:14:03 -07006309 }
Sooraj Sasindranff75de62020-07-15 01:35:24 -07006310 if (DBG) log("isDataEnabledForReason: retVal=" + retVal);
Sooraj Sasindran2675c0b2020-04-19 18:14:03 -07006311 return retVal;
6312 } else {
6313 if (DBG) {
Sooraj Sasindranff75de62020-07-15 01:35:24 -07006314 loge("isDataEnabledForReason: no phone subId="
Sooraj Sasindran2675c0b2020-04-19 18:14:03 -07006315 + subId + " retVal=false");
6316 }
6317 return false;
6318 }
6319 } finally {
6320 Binder.restoreCallingIdentity(identity);
6321 }
6322 }
6323
Malcolm Chendf0b4cc2020-02-24 15:12:43 -08006324 private int getCarrierPrivilegeStatusFromCarrierConfigRules(int privilegeFromSim, int uid,
Nazanin Bakhshi5d0636e2019-08-19 16:29:37 -07006325 Phone phone) {
Hall Liuce478d22020-07-13 12:13:03 -07006326 if (uid == Process.SYSTEM_UID || uid == Process.PHONE_UID) {
6327 // Skip the check if it's one of these special uids
6328 return TelephonyManager.CARRIER_PRIVILEGE_STATUS_HAS_ACCESS;
6329 }
6330
Nazanin Bakhshi5d0636e2019-08-19 16:29:37 -07006331 //load access rules from carrier configs, and check those as well: b/139133814
6332 SubscriptionController subController = SubscriptionController.getInstance();
6333 if (privilegeFromSim == TelephonyManager.CARRIER_PRIVILEGE_STATUS_HAS_ACCESS
6334 || subController == null) return privilegeFromSim;
6335
Nazanin Bakhshi5d0636e2019-08-19 16:29:37 -07006336 PackageManager pkgMgr = phone.getContext().getPackageManager();
6337 String[] packages = pkgMgr.getPackagesForUid(uid);
6338
6339 final long identity = Binder.clearCallingIdentity();
6340 try {
Jeff Davidson0103e8c2020-03-28 12:24:40 -07006341 int subId = phone.getSubId();
6342 if (mCarrierPrivilegeTestOverrideSubIds.contains(subId)) {
6343 // A test override is in place for the privileges for this subId, so don't try to
6344 // read the subscription privileges.
6345 return privilegeFromSim;
6346 }
6347 SubscriptionInfo subInfo = subController.getSubscriptionInfo(subId);
Nazanin Bakhshi5d0636e2019-08-19 16:29:37 -07006348 SubscriptionManager subManager = (SubscriptionManager)
6349 phone.getContext().getSystemService(Context.TELEPHONY_SUBSCRIPTION_SERVICE);
6350 for (String pkg : packages) {
6351 if (subManager.canManageSubscription(subInfo, pkg)) {
6352 return TelephonyManager.CARRIER_PRIVILEGE_STATUS_HAS_ACCESS;
6353 }
6354 }
6355 return privilegeFromSim;
6356 } finally {
6357 Binder.restoreCallingIdentity(identity);
6358 }
6359 }
6360
6361 private int getCarrierPrivilegeStatusFromCarrierConfigRules(int privilegeFromSim, Phone phone,
6362 String pkgName) {
6363 //load access rules from carrier configs, and check those as well: b/139133814
6364 SubscriptionController subController = SubscriptionController.getInstance();
6365 if (privilegeFromSim == TelephonyManager.CARRIER_PRIVILEGE_STATUS_HAS_ACCESS
6366 || subController == null) return privilegeFromSim;
6367
6368 final long identity = Binder.clearCallingIdentity();
6369 try {
Jeff Davidson0103e8c2020-03-28 12:24:40 -07006370 int subId = phone.getSubId();
6371 if (mCarrierPrivilegeTestOverrideSubIds.contains(subId)) {
6372 // A test override is in place for the privileges for this subId, so don't try to
6373 // read the subscription privileges.
6374 return privilegeFromSim;
6375 }
6376 SubscriptionInfo subInfo = subController.getSubscriptionInfo(subId);
Nazanin Bakhshi5d0636e2019-08-19 16:29:37 -07006377 SubscriptionManager subManager = (SubscriptionManager)
6378 phone.getContext().getSystemService(Context.TELEPHONY_SUBSCRIPTION_SERVICE);
6379 return subManager.canManageSubscription(subInfo, pkgName)
6380 ? TelephonyManager.CARRIER_PRIVILEGE_STATUS_HAS_ACCESS : privilegeFromSim;
6381 } finally {
6382 Binder.restoreCallingIdentity(identity);
6383 }
6384 }
6385
Shishir Agrawal60f9c952014-06-23 12:00:43 -07006386 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08006387 public int getCarrierPrivilegeStatus(int subId) {
6388 final Phone phone = getPhone(subId);
6389 if (phone == null) {
6390 loge("getCarrierPrivilegeStatus: Invalid subId");
6391 return TelephonyManager.CARRIER_PRIVILEGE_STATUS_NO_ACCESS;
6392 }
6393 UiccCard card = UiccController.getInstance().getUiccCard(phone.getPhoneId());
Shishir Agrawaleb8771e2014-07-22 11:24:08 -07006394 if (card == null) {
Shishir Agrawal5e5becd2014-11-18 11:38:23 -08006395 loge("getCarrierPrivilegeStatus: No UICC");
Shishir Agrawaleb8771e2014-07-22 11:24:08 -07006396 return TelephonyManager.CARRIER_PRIVILEGE_STATUS_RULES_NOT_LOADED;
6397 }
Nazanin Bakhshi5d0636e2019-08-19 16:29:37 -07006398
6399 return getCarrierPrivilegeStatusFromCarrierConfigRules(
6400 card.getCarrierPrivilegeStatusForCurrentTransaction(
Malcolm Chendf0b4cc2020-02-24 15:12:43 -08006401 phone.getContext().getPackageManager()), Binder.getCallingUid(), phone);
Shishir Agrawal60f9c952014-06-23 12:00:43 -07006402 }
Junda Liu29340342014-07-10 15:23:27 -07006403
6404 @Override
Jeff Davidson7e17e312018-02-13 18:17:36 -08006405 public int getCarrierPrivilegeStatusForUid(int subId, int uid) {
Shuo Qian6d927452019-12-05 11:40:37 -08006406 enforceReadPrivilegedPermission("getCarrierPrivilegeStatusForUid");
Jeff Davidson7e17e312018-02-13 18:17:36 -08006407 final Phone phone = getPhone(subId);
6408 if (phone == null) {
Taesu Leef8fbed92019-10-07 18:47:02 +09006409 loge("getCarrierPrivilegeStatusForUid: Invalid subId");
Jeff Davidson7e17e312018-02-13 18:17:36 -08006410 return TelephonyManager.CARRIER_PRIVILEGE_STATUS_NO_ACCESS;
6411 }
6412 UiccProfile profile =
6413 UiccController.getInstance().getUiccProfileForPhone(phone.getPhoneId());
6414 if (profile == null) {
Taesu Leef8fbed92019-10-07 18:47:02 +09006415 loge("getCarrierPrivilegeStatusForUid: No UICC");
Jeff Davidson7e17e312018-02-13 18:17:36 -08006416 return TelephonyManager.CARRIER_PRIVILEGE_STATUS_RULES_NOT_LOADED;
6417 }
Nazanin Bakhshi5d0636e2019-08-19 16:29:37 -07006418 return getCarrierPrivilegeStatusFromCarrierConfigRules(
Shuo Qian6d927452019-12-05 11:40:37 -08006419 profile.getCarrierPrivilegeStatusForUid(
Malcolm Chendf0b4cc2020-02-24 15:12:43 -08006420 phone.getContext().getPackageManager(), uid), uid, phone);
Jeff Davidson7e17e312018-02-13 18:17:36 -08006421 }
6422
6423 @Override
chen xuf7e9fe82019-05-09 19:31:02 -07006424 public int checkCarrierPrivilegesForPackage(int subId, String pkgName) {
6425 if (TextUtils.isEmpty(pkgName)) {
Junda Liu317d70b2016-03-08 09:33:53 -08006426 return TelephonyManager.CARRIER_PRIVILEGE_STATUS_NO_ACCESS;
chen xuf7e9fe82019-05-09 19:31:02 -07006427 }
6428
6429 int phoneId = SubscriptionManager.getPhoneId(subId);
6430 UiccCard card = UiccController.getInstance().getUiccCard(phoneId);
Shishir Agrawaleb8771e2014-07-22 11:24:08 -07006431 if (card == null) {
chen xuf7e9fe82019-05-09 19:31:02 -07006432 loge("checkCarrierPrivilegesForPackage: No UICC on subId " + subId);
Shishir Agrawaleb8771e2014-07-22 11:24:08 -07006433 return TelephonyManager.CARRIER_PRIVILEGE_STATUS_RULES_NOT_LOADED;
6434 }
Nazanin Bakhshi5d0636e2019-08-19 16:29:37 -07006435 return getCarrierPrivilegeStatusFromCarrierConfigRules(
6436 card.getCarrierPrivilegeStatus(mApp.getPackageManager(), pkgName),
6437 getPhone(phoneId), pkgName);
Zach Johnson50ecba32015-05-19 00:24:21 -07006438 }
6439
6440 @Override
6441 public int checkCarrierPrivilegesForPackageAnyPhone(String pkgName) {
Junda Liu317d70b2016-03-08 09:33:53 -08006442 if (TextUtils.isEmpty(pkgName))
6443 return TelephonyManager.CARRIER_PRIVILEGE_STATUS_NO_ACCESS;
Zach Johnson50ecba32015-05-19 00:24:21 -07006444 int result = TelephonyManager.CARRIER_PRIVILEGE_STATUS_RULES_NOT_LOADED;
6445 for (int i = 0; i < TelephonyManager.getDefault().getPhoneCount(); i++) {
6446 UiccCard card = UiccController.getInstance().getUiccCard(i);
6447 if (card == null) {
Jonathan Basseri7d320df2015-06-16 12:17:08 -07006448 // No UICC in that slot.
Zach Johnson50ecba32015-05-19 00:24:21 -07006449 continue;
6450 }
6451
Nazanin Bakhshi5d0636e2019-08-19 16:29:37 -07006452 result = getCarrierPrivilegeStatusFromCarrierConfigRules(
6453 card.getCarrierPrivilegeStatus(mApp.getPackageManager(), pkgName),
6454 getPhone(i), pkgName);
Zach Johnson50ecba32015-05-19 00:24:21 -07006455 if (result == TelephonyManager.CARRIER_PRIVILEGE_STATUS_HAS_ACCESS) {
6456 break;
6457 }
6458 }
6459
6460 return result;
Junda Liu29340342014-07-10 15:23:27 -07006461 }
Derek Tan89e89d42014-07-08 17:00:10 -07006462
6463 @Override
Junda Liue64de782015-04-16 17:19:16 -07006464 public List<String> getCarrierPackageNamesForIntentAndPhone(Intent intent, int phoneId) {
6465 if (!SubscriptionManager.isValidPhoneId(phoneId)) {
6466 loge("phoneId " + phoneId + " is not valid.");
6467 return null;
6468 }
6469 UiccCard card = UiccController.getInstance().getUiccCard(phoneId);
Shishir Agrawaleb6439a2014-07-21 13:19:38 -07006470 if (card == null) {
Taesu Leef8fbed92019-10-07 18:47:02 +09006471 loge("getCarrierPackageNamesForIntentAndPhone: No UICC");
Shishir Agrawaleb6439a2014-07-21 13:19:38 -07006472 return null ;
6473 }
Nazanin Bakhshif782e562018-12-11 15:15:39 -08006474 return card.getCarrierPackageNamesForIntent(mApp.getPackageManager(), intent);
Shishir Agrawaleb6439a2014-07-21 13:19:38 -07006475 }
6476
Amith Yamasani6e118872016-02-19 12:53:51 -08006477 @Override
chen xuf7e9fe82019-05-09 19:31:02 -07006478 public List<String> getPackagesWithCarrierPrivileges(int phoneId) {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08006479 PackageManager pm = mApp.getPackageManager();
Amith Yamasani6e118872016-02-19 12:53:51 -08006480 List<String> privilegedPackages = new ArrayList<>();
6481 List<PackageInfo> packages = null;
chen xuf7e9fe82019-05-09 19:31:02 -07006482 UiccCard card = UiccController.getInstance().getUiccCard(phoneId);
6483 // has UICC in that slot.
6484 if (card != null) {
Amith Yamasani6e118872016-02-19 12:53:51 -08006485 if (card.hasCarrierPrivilegeRules()) {
6486 if (packages == null) {
6487 // Only check packages in user 0 for now
6488 packages = pm.getInstalledPackagesAsUser(
Nazanin Bakhshi5d0636e2019-08-19 16:29:37 -07006489 PackageManager.MATCH_DISABLED_COMPONENTS
6490 | PackageManager.MATCH_DISABLED_UNTIL_USED_COMPONENTS
Cheonho Park17089c62019-08-01 15:23:12 +09006491 | PackageManager.GET_SIGNING_CERTIFICATES,
Amit Mahajan98bbd6e2020-01-27 18:16:07 -08006492 UserHandle.SYSTEM.getIdentifier());
Amith Yamasani6e118872016-02-19 12:53:51 -08006493 }
6494 for (int p = packages.size() - 1; p >= 0; p--) {
6495 PackageInfo pkgInfo = packages.get(p);
6496 if (pkgInfo != null && pkgInfo.packageName != null
6497 && card.getCarrierPrivilegeStatus(pkgInfo)
chen xuf7e9fe82019-05-09 19:31:02 -07006498 == TelephonyManager.CARRIER_PRIVILEGE_STATUS_HAS_ACCESS) {
Amith Yamasani6e118872016-02-19 12:53:51 -08006499 privilegedPackages.add(pkgInfo.packageName);
6500 }
6501 }
6502 }
6503 }
6504 return privilegedPackages;
6505 }
6506
chen xuf7e9fe82019-05-09 19:31:02 -07006507 @Override
6508 public List<String> getPackagesWithCarrierPrivilegesForAllPhones() {
Shuo Qiand54f9f32019-12-03 23:40:18 +00006509 enforceReadPrivilegedPermission("getPackagesWithCarrierPrivilegesForAllPhones");
6510
6511 final long identity = Binder.clearCallingIdentity();
6512
chen xuf7e9fe82019-05-09 19:31:02 -07006513 List<String> privilegedPackages = new ArrayList<>();
Shuo Qiand54f9f32019-12-03 23:40:18 +00006514 try {
6515 for (int i = 0; i < TelephonyManager.getDefault().getPhoneCount(); i++) {
6516 privilegedPackages.addAll(getPackagesWithCarrierPrivileges(i));
6517 }
6518 } finally {
6519 Binder.restoreCallingIdentity(identity);
chen xuf7e9fe82019-05-09 19:31:02 -07006520 }
6521 return privilegedPackages;
6522 }
6523
Wink Savilleb564aae2014-10-23 10:18:09 -07006524 private String getIccId(int subId) {
Sanket Padawe356d7632015-06-22 14:03:32 -07006525 final Phone phone = getPhone(subId);
6526 UiccCard card = phone == null ? null : phone.getUiccCard();
Derek Tan97ebb422014-09-05 16:55:38 -07006527 if (card == null) {
Derek Tan97ebb422014-09-05 16:55:38 -07006528 return null;
6529 }
6530 String iccId = card.getIccId();
6531 if (TextUtils.isEmpty(iccId)) {
Derek Tan97ebb422014-09-05 16:55:38 -07006532 return null;
6533 }
6534 return iccId;
6535 }
6536
Shishir Agrawaleb6439a2014-07-21 13:19:38 -07006537 @Override
Jeff Sharkey85190e62014-12-05 09:40:12 -08006538 public boolean setLine1NumberForDisplayForSubscriber(int subId, String alphaTag,
6539 String number) {
Shuo Qian6d927452019-12-05 11:40:37 -08006540 TelephonyPermissions.enforceCallingOrSelfCarrierPrivilege(mApp,
Jeff Davidson7e17e312018-02-13 18:17:36 -08006541 subId, "setLine1NumberForDisplayForSubscriber");
Derek Tan97ebb422014-09-05 16:55:38 -07006542
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006543 final long identity = Binder.clearCallingIdentity();
6544 try {
6545 final String iccId = getIccId(subId);
6546 final Phone phone = getPhone(subId);
6547 if (phone == null) {
6548 return false;
6549 }
6550 final String subscriberId = phone.getSubscriberId();
6551
6552 if (DBG_MERGE) {
Amit Mahajan98bbd6e2020-01-27 18:16:07 -08006553 Rlog.d(LOG_TAG, "Setting line number for ICC=" + iccId + ", subscriberId="
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006554 + subscriberId + " to " + number);
6555 }
6556
6557 if (TextUtils.isEmpty(iccId)) {
6558 return false;
6559 }
6560
6561 final SharedPreferences.Editor editor = mTelephonySharedPreferences.edit();
6562
6563 final String alphaTagPrefKey = PREF_CARRIERS_ALPHATAG_PREFIX + iccId;
6564 if (alphaTag == null) {
6565 editor.remove(alphaTagPrefKey);
6566 } else {
6567 editor.putString(alphaTagPrefKey, alphaTag);
6568 }
6569
6570 // Record both the line number and IMSI for this ICCID, since we need to
6571 // track all merged IMSIs based on line number
6572 final String numberPrefKey = PREF_CARRIERS_NUMBER_PREFIX + iccId;
6573 final String subscriberPrefKey = PREF_CARRIERS_SUBSCRIBER_PREFIX + iccId;
6574 if (number == null) {
6575 editor.remove(numberPrefKey);
6576 editor.remove(subscriberPrefKey);
6577 } else {
6578 editor.putString(numberPrefKey, number);
6579 editor.putString(subscriberPrefKey, subscriberId);
6580 }
6581
6582 editor.commit();
6583 return true;
6584 } finally {
6585 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07006586 }
Derek Tan7226c842014-07-02 17:42:23 -07006587 }
6588
6589 @Override
Philip P. Moltmann9797cbb2020-01-07 13:45:00 -08006590 public String getLine1NumberForDisplay(int subId, String callingPackage,
6591 String callingFeatureId) {
Makoto Onukifee69342015-06-29 14:44:50 -07006592 // This is open to apps with WRITE_SMS.
Jeff Davidson7e17e312018-02-13 18:17:36 -08006593 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneNumber(
Philip P. Moltmann9797cbb2020-01-07 13:45:00 -08006594 mApp, subId, callingPackage, callingFeatureId, "getLine1NumberForDisplay")) {
Amit Mahajan9cf11512015-11-09 11:40:48 -08006595 if (DBG_MERGE) log("getLine1NumberForDisplay returning null due to permission");
Svet Ganovb320e182015-04-16 12:30:10 -07006596 return null;
6597 }
Derek Tan97ebb422014-09-05 16:55:38 -07006598
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006599 final long identity = Binder.clearCallingIdentity();
6600 try {
6601 String iccId = getIccId(subId);
6602 if (iccId != null) {
6603 String numberPrefKey = PREF_CARRIERS_NUMBER_PREFIX + iccId;
6604 if (DBG_MERGE) {
6605 log("getLine1NumberForDisplay returning "
6606 + mTelephonySharedPreferences.getString(numberPrefKey, null));
6607 }
6608 return mTelephonySharedPreferences.getString(numberPrefKey, null);
Amit Mahajan9cf11512015-11-09 11:40:48 -08006609 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006610 if (DBG_MERGE) log("getLine1NumberForDisplay returning null as iccId is null");
6611 return null;
6612 } finally {
6613 Binder.restoreCallingIdentity(identity);
Derek Tan7226c842014-07-02 17:42:23 -07006614 }
Derek Tan7226c842014-07-02 17:42:23 -07006615 }
6616
6617 @Override
Philip P. Moltmann9797cbb2020-01-07 13:45:00 -08006618 public String getLine1AlphaTagForDisplay(int subId, String callingPackage,
6619 String callingFeatureId) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08006620 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
Philip P. Moltmann9797cbb2020-01-07 13:45:00 -08006621 mApp, subId, callingPackage, callingFeatureId, "getLine1AlphaTagForDisplay")) {
Svet Ganovb320e182015-04-16 12:30:10 -07006622 return null;
6623 }
Derek Tan97ebb422014-09-05 16:55:38 -07006624
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006625 final long identity = Binder.clearCallingIdentity();
6626 try {
6627 String iccId = getIccId(subId);
6628 if (iccId != null) {
6629 String alphaTagPrefKey = PREF_CARRIERS_ALPHATAG_PREFIX + iccId;
6630 return mTelephonySharedPreferences.getString(alphaTagPrefKey, null);
6631 }
6632 return null;
6633 } finally {
6634 Binder.restoreCallingIdentity(identity);
Derek Tan7226c842014-07-02 17:42:23 -07006635 }
Derek Tan7226c842014-07-02 17:42:23 -07006636 }
Shishir Agrawalb1ebf8c2014-07-17 16:32:41 -07006637
6638 @Override
Philip P. Moltmann9797cbb2020-01-07 13:45:00 -08006639 public String[] getMergedSubscriberIds(int subId, String callingPackage,
6640 String callingFeatureId) {
Jeff Davidson913390f2018-02-23 17:11:49 -08006641 // This API isn't public, so no need to provide a valid subscription ID - we're not worried
6642 // about carrier-privileged callers not having access.
Jeff Davidson7e17e312018-02-13 18:17:36 -08006643 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
Jeff Davidson913390f2018-02-23 17:11:49 -08006644 mApp, SubscriptionManager.INVALID_SUBSCRIPTION_ID, callingPackage,
Philip P. Moltmann9797cbb2020-01-07 13:45:00 -08006645 callingFeatureId, "getMergedSubscriberIds")) {
Fyodor Kupolov8e53b0b2015-06-17 13:17:50 -07006646 return null;
6647 }
Jeff Sharkey85190e62014-12-05 09:40:12 -08006648
Jordan Liub49b04b2019-05-06 14:45:15 -07006649 // Clear calling identity, when calling TelephonyManager, because callerUid must be
6650 // the process, where TelephonyManager was instantiated.
6651 // Otherwise AppOps check will fail.
Fyodor Kupolov8e53b0b2015-06-17 13:17:50 -07006652 final long identity = Binder.clearCallingIdentity();
6653 try {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08006654 final Context context = mApp;
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006655 final TelephonyManager tele = TelephonyManager.from(context);
6656 final SubscriptionManager sub = SubscriptionManager.from(context);
6657
6658 // Figure out what subscribers are currently active
6659 final ArraySet<String> activeSubscriberIds = new ArraySet<>();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006660
Jordan Liub49b04b2019-05-06 14:45:15 -07006661 // Only consider subs which match the current subId
6662 // This logic can be simplified. See b/131189269 for progress.
6663 if (isActiveSubscription(subId)) {
Fyodor Kupolov8e53b0b2015-06-17 13:17:50 -07006664 activeSubscriberIds.add(tele.getSubscriberId(subId));
6665 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006666
6667 // First pass, find a number override for an active subscriber
6668 String mergeNumber = null;
6669 final Map<String, ?> prefs = mTelephonySharedPreferences.getAll();
6670 for (String key : prefs.keySet()) {
6671 if (key.startsWith(PREF_CARRIERS_SUBSCRIBER_PREFIX)) {
6672 final String subscriberId = (String) prefs.get(key);
6673 if (activeSubscriberIds.contains(subscriberId)) {
6674 final String iccId = key.substring(
6675 PREF_CARRIERS_SUBSCRIBER_PREFIX.length());
6676 final String numberKey = PREF_CARRIERS_NUMBER_PREFIX + iccId;
6677 mergeNumber = (String) prefs.get(numberKey);
6678 if (DBG_MERGE) {
Amit Mahajan98bbd6e2020-01-27 18:16:07 -08006679 Rlog.d(LOG_TAG, "Found line number " + mergeNumber
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006680 + " for active subscriber " + subscriberId);
6681 }
6682 if (!TextUtils.isEmpty(mergeNumber)) {
6683 break;
6684 }
6685 }
6686 }
6687 }
6688
6689 // Shortcut when no active merged subscribers
6690 if (TextUtils.isEmpty(mergeNumber)) {
6691 return null;
6692 }
6693
6694 // Second pass, find all subscribers under that line override
6695 final ArraySet<String> result = new ArraySet<>();
6696 for (String key : prefs.keySet()) {
6697 if (key.startsWith(PREF_CARRIERS_NUMBER_PREFIX)) {
6698 final String number = (String) prefs.get(key);
6699 if (mergeNumber.equals(number)) {
6700 final String iccId = key.substring(PREF_CARRIERS_NUMBER_PREFIX.length());
6701 final String subscriberKey = PREF_CARRIERS_SUBSCRIBER_PREFIX + iccId;
6702 final String subscriberId = (String) prefs.get(subscriberKey);
6703 if (!TextUtils.isEmpty(subscriberId)) {
6704 result.add(subscriberId);
6705 }
6706 }
6707 }
6708 }
6709
6710 final String[] resultArray = result.toArray(new String[result.size()]);
6711 Arrays.sort(resultArray);
6712 if (DBG_MERGE) {
Amit Mahajan98bbd6e2020-01-27 18:16:07 -08006713 Rlog.d(LOG_TAG,
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006714 "Found subscribers " + Arrays.toString(resultArray) + " after merge");
6715 }
6716 return resultArray;
Fyodor Kupolov8e53b0b2015-06-17 13:17:50 -07006717 } finally {
6718 Binder.restoreCallingIdentity(identity);
Jeff Sharkey85190e62014-12-05 09:40:12 -08006719 }
Jeff Sharkey85190e62014-12-05 09:40:12 -08006720 }
6721
6722 @Override
zoey chen38003472019-12-13 17:16:31 +08006723 public String[] getMergedImsisFromGroup(int subId, String callingPackage) {
6724 enforceReadPrivilegedPermission("getMergedImsisFromGroup");
Malcolm Chen6ca97372019-07-01 16:28:21 -07006725
6726 final long identity = Binder.clearCallingIdentity();
6727 try {
6728 final TelephonyManager telephonyManager = mApp.getSystemService(
6729 TelephonyManager.class);
6730 String subscriberId = telephonyManager.getSubscriberId(subId);
6731 if (subscriberId == null) {
6732 if (DBG) {
zoey chen38003472019-12-13 17:16:31 +08006733 log("getMergedImsisFromGroup can't find subscriberId for subId "
Malcolm Chen6ca97372019-07-01 16:28:21 -07006734 + subId);
6735 }
6736 return null;
6737 }
6738
6739 final SubscriptionInfo info = SubscriptionController.getInstance()
6740 .getSubscriptionInfo(subId);
6741 final ParcelUuid groupUuid = info.getGroupUuid();
6742 // If it doesn't belong to any group, return just subscriberId of itself.
6743 if (groupUuid == null) {
6744 return new String[]{subscriberId};
6745 }
6746
6747 // Get all subscriberIds from the group.
6748 final List<String> mergedSubscriberIds = new ArrayList<>();
6749 final List<SubscriptionInfo> groupInfos = SubscriptionController.getInstance()
Philip P. Moltmann9797cbb2020-01-07 13:45:00 -08006750 .getSubscriptionsInGroup(groupUuid, mApp.getOpPackageName(),
Philip P. Moltmann0079aae2020-03-05 16:24:02 -08006751 mApp.getAttributionTag());
Malcolm Chen6ca97372019-07-01 16:28:21 -07006752 for (SubscriptionInfo subInfo : groupInfos) {
6753 subscriberId = telephonyManager.getSubscriberId(subInfo.getSubscriptionId());
6754 if (subscriberId != null) {
6755 mergedSubscriberIds.add(subscriberId);
6756 }
6757 }
6758
6759 return mergedSubscriberIds.toArray(new String[mergedSubscriberIds.size()]);
6760 } finally {
6761 Binder.restoreCallingIdentity(identity);
6762
6763 }
6764 }
6765
6766 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08006767 public boolean setOperatorBrandOverride(int subId, String brand) {
Shuo Qian6d927452019-12-05 11:40:37 -08006768 TelephonyPermissions.enforceCallingOrSelfCarrierPrivilege(mApp,
Jeff Davidson7e17e312018-02-13 18:17:36 -08006769 subId, "setOperatorBrandOverride");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006770
6771 final long identity = Binder.clearCallingIdentity();
6772 try {
6773 final Phone phone = getPhone(subId);
6774 return phone == null ? false : phone.setOperatorBrandOverride(brand);
6775 } finally {
6776 Binder.restoreCallingIdentity(identity);
6777 }
Shishir Agrawalb1ebf8c2014-07-17 16:32:41 -07006778 }
Steven Liu4bf01bc2014-07-17 11:05:29 -05006779
6780 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08006781 public boolean setRoamingOverride(int subId, List<String> gsmRoamingList,
Shishir Agrawal621a47c2014-12-01 10:25:09 -08006782 List<String> gsmNonRoamingList, List<String> cdmaRoamingList,
6783 List<String> cdmaNonRoamingList) {
Shuo Qian6d927452019-12-05 11:40:37 -08006784 TelephonyPermissions.enforceCallingOrSelfCarrierPrivilege(
6785 mApp, subId, "setRoamingOverride");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006786
6787 final long identity = Binder.clearCallingIdentity();
6788 try {
6789 final Phone phone = getPhone(subId);
6790 if (phone == null) {
6791 return false;
6792 }
6793 return phone.setRoamingOverride(gsmRoamingList, gsmNonRoamingList, cdmaRoamingList,
6794 cdmaNonRoamingList);
6795 } finally {
6796 Binder.restoreCallingIdentity(identity);
Shishir Agrawalc04d9752016-02-19 10:41:00 -08006797 }
Shishir Agrawal621a47c2014-12-01 10:25:09 -08006798 }
6799
6800 @Override
Shuo Qian850e4d6a2018-04-25 21:02:08 +00006801 @Deprecated
6802 public int invokeOemRilRequestRaw(byte[] oemReq, byte[] oemResp) {
6803 enforceModifyPermission();
6804
6805 int returnValue = 0;
6806 try {
vagdeviaf9a5b92018-08-15 16:01:53 -07006807 AsyncResult result = (AsyncResult) sendRequest(CMD_INVOKE_OEM_RIL_REQUEST_RAW, oemReq);
Shuo Qian850e4d6a2018-04-25 21:02:08 +00006808 if(result.exception == null) {
6809 if (result.result != null) {
6810 byte[] responseData = (byte[])(result.result);
6811 if(responseData.length > oemResp.length) {
6812 Log.w(LOG_TAG, "Buffer to copy response too small: Response length is " +
6813 responseData.length + "bytes. Buffer Size is " +
6814 oemResp.length + "bytes.");
6815 }
6816 System.arraycopy(responseData, 0, oemResp, 0, responseData.length);
6817 returnValue = responseData.length;
6818 }
6819 } else {
6820 CommandException ex = (CommandException) result.exception;
6821 returnValue = ex.getCommandError().ordinal();
6822 if(returnValue > 0) returnValue *= -1;
6823 }
6824 } catch (RuntimeException e) {
6825 Log.w(LOG_TAG, "sendOemRilRequestRaw: Runtime Exception");
6826 returnValue = (CommandException.Error.GENERIC_FAILURE.ordinal());
6827 if(returnValue > 0) returnValue *= -1;
6828 }
6829
6830 return returnValue;
6831 }
6832
6833 @Override
Wink Saville5d475dd2014-10-17 15:00:58 -07006834 public void setRadioCapability(RadioAccessFamily[] rafs) {
6835 try {
6836 ProxyController.getInstance().setRadioCapability(rafs);
6837 } catch (RuntimeException e) {
6838 Log.w(LOG_TAG, "setRadioCapability: Runtime Exception");
6839 }
6840 }
6841
6842 @Override
Robert Greenwalt36b23af2015-07-06 17:59:14 -07006843 public int getRadioAccessFamily(int phoneId, String callingPackage) {
Jeff Davidson913390f2018-02-23 17:11:49 -08006844 Phone phone = PhoneFactory.getPhone(phoneId);
chen xub97461a2018-10-26 14:17:57 -07006845 int raf = RadioAccessFamily.RAF_UNKNOWN;
Jeff Davidson913390f2018-02-23 17:11:49 -08006846 if (phone == null) {
chen xub97461a2018-10-26 14:17:57 -07006847 return raf;
Jeff Davidson913390f2018-02-23 17:11:49 -08006848 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006849 final long identity = Binder.clearCallingIdentity();
6850 try {
chen xub97461a2018-10-26 14:17:57 -07006851 TelephonyPermissions
6852 .enforeceCallingOrSelfReadPrivilegedPhoneStatePermissionOrCarrierPrivilege(
6853 mApp, phone.getSubId(), "getRadioAccessFamily");
6854 raf = ProxyController.getInstance().getRadioAccessFamily(phoneId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006855 } finally {
6856 Binder.restoreCallingIdentity(identity);
6857 }
chen xub97461a2018-10-26 14:17:57 -07006858 return raf;
Wink Saville5d475dd2014-10-17 15:00:58 -07006859 }
Andrew Leedf14ead2014-10-17 14:22:52 -07006860
6861 @Override
6862 public void enableVideoCalling(boolean enable) {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08006863 final Phone defaultPhone = getDefaultPhone();
Andrew Leedf14ead2014-10-17 14:22:52 -07006864 enforceModifyPermission();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006865
6866 final long identity = Binder.clearCallingIdentity();
6867 try {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08006868 ImsManager.getInstance(defaultPhone.getContext(),
6869 defaultPhone.getPhoneId()).setVtSetting(enable);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006870 } finally {
6871 Binder.restoreCallingIdentity(identity);
6872 }
Andrew Leedf14ead2014-10-17 14:22:52 -07006873 }
6874
6875 @Override
Philip P. Moltmann9797cbb2020-01-07 13:45:00 -08006876 public boolean isVideoCallingEnabled(String callingPackage, String callingFeatureId) {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08006877 final Phone defaultPhone = getDefaultPhone();
Philip P. Moltmann9797cbb2020-01-07 13:45:00 -08006878 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(mApp, defaultPhone.getSubId(),
6879 callingPackage, callingFeatureId, "isVideoCallingEnabled")) {
Amit Mahajan578e53d2018-03-20 16:18:38 +00006880 return false;
6881 }
Svet Ganovb320e182015-04-16 12:30:10 -07006882
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006883 final long identity = Binder.clearCallingIdentity();
6884 try {
6885 // Check the user preference and the system-level IMS setting. Even if the user has
6886 // enabled video calling, if IMS is disabled we aren't able to support video calling.
6887 // In the long run, we may instead need to check if there exists a connection service
6888 // which can support video calling.
6889 ImsManager imsManager =
Nazanin Bakhshif782e562018-12-11 15:15:39 -08006890 ImsManager.getInstance(defaultPhone.getContext(), defaultPhone.getPhoneId());
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006891 return imsManager.isVtEnabledByPlatform()
6892 && imsManager.isEnhanced4gLteModeSettingEnabledByUser()
6893 && imsManager.isVtEnabledByUser();
6894 } finally {
6895 Binder.restoreCallingIdentity(identity);
6896 }
Andrew Leedf14ead2014-10-17 14:22:52 -07006897 }
Libin.Tang@motorola.comafe82642014-12-18 13:27:53 -06006898
Andrew Leea1239f22015-03-02 17:44:07 -08006899 @Override
Philip P. Moltmann9797cbb2020-01-07 13:45:00 -08006900 public boolean canChangeDtmfToneLength(int subId, String callingPackage,
6901 String callingFeatureId) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006902 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
Philip P. Moltmann9797cbb2020-01-07 13:45:00 -08006903 mApp, subId, callingPackage, callingFeatureId,
6904 "isVideoCallingEnabled")) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006905 return false;
6906 }
6907
6908 final long identity = Binder.clearCallingIdentity();
6909 try {
6910 CarrierConfigManager configManager =
6911 (CarrierConfigManager) mApp.getSystemService(Context.CARRIER_CONFIG_SERVICE);
Nazanin Bakhshif782e562018-12-11 15:15:39 -08006912 return configManager.getConfigForSubId(subId)
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006913 .getBoolean(CarrierConfigManager.KEY_DTMF_TYPE_ENABLED_BOOL);
6914 } finally {
6915 Binder.restoreCallingIdentity(identity);
6916 }
Andrew Leea1239f22015-03-02 17:44:07 -08006917 }
6918
6919 @Override
Philip P. Moltmann9797cbb2020-01-07 13:45:00 -08006920 public boolean isWorldPhone(int subId, String callingPackage, String callingFeatureId) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006921 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
Philip P. Moltmann9797cbb2020-01-07 13:45:00 -08006922 mApp, subId, callingPackage, callingFeatureId, "isVideoCallingEnabled")) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006923 return false;
6924 }
6925
6926 final long identity = Binder.clearCallingIdentity();
6927 try {
6928 CarrierConfigManager configManager =
6929 (CarrierConfigManager) mApp.getSystemService(Context.CARRIER_CONFIG_SERVICE);
Nazanin Bakhshif782e562018-12-11 15:15:39 -08006930 return configManager.getConfigForSubId(subId)
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006931 .getBoolean(CarrierConfigManager.KEY_WORLD_PHONE_BOOL);
6932 } finally {
6933 Binder.restoreCallingIdentity(identity);
6934 }
Andrew Leea1239f22015-03-02 17:44:07 -08006935 }
6936
Andrew Lee9431b832015-03-09 18:46:45 -07006937 @Override
6938 public boolean isTtyModeSupported() {
Tyler Gunn77ee9382019-10-31 13:08:23 -07006939 TelecomManager telecomManager = mApp.getSystemService(TelecomManager.class);
Wooki Wu1f82f7a2016-02-15 15:59:58 +08006940 return telecomManager.isTtySupported();
Andrew Lee9431b832015-03-09 18:46:45 -07006941 }
6942
6943 @Override
6944 public boolean isHearingAidCompatibilitySupported() {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006945 final long identity = Binder.clearCallingIdentity();
6946 try {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08006947 return mApp.getResources().getBoolean(R.bool.hac_enabled);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006948 } finally {
6949 Binder.restoreCallingIdentity(identity);
6950 }
Andrew Lee9431b832015-03-09 18:46:45 -07006951 }
6952
Hall Liuf6668912018-10-31 17:05:23 -07006953 /**
6954 * Determines whether the device currently supports RTT (Real-time text). Based both on carrier
6955 * support for the feature and device firmware support.
6956 *
6957 * @return {@code true} if the device and carrier both support RTT, {@code false} otherwise.
6958 */
6959 @Override
6960 public boolean isRttSupported(int subscriptionId) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006961 final long identity = Binder.clearCallingIdentity();
Nazanin Bakhshif782e562018-12-11 15:15:39 -08006962 final Phone phone = getPhone(subscriptionId);
6963 if (phone == null) {
6964 loge("isRttSupported: no Phone found. Invalid subId:" + subscriptionId);
6965 return false;
6966 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006967 try {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08006968 boolean isCarrierSupported = mApp.getCarrierConfigForSubId(subscriptionId).getBoolean(
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006969 CarrierConfigManager.KEY_RTT_SUPPORTED_BOOL);
6970 boolean isDeviceSupported =
Nazanin Bakhshif782e562018-12-11 15:15:39 -08006971 phone.getContext().getResources().getBoolean(R.bool.config_support_rtt);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006972 return isCarrierSupported && isDeviceSupported;
6973 } finally {
6974 Binder.restoreCallingIdentity(identity);
6975 }
Hall Liu98187582018-01-22 19:15:32 -08006976 }
6977
Hall Liuf6668912018-10-31 17:05:23 -07006978 /**
Hall Liu6a06be62019-07-23 18:39:00 -07006979 * Determines whether the user has turned on RTT. If the carrier wants to ignore the user-set
6980 * RTT setting, will return true if the device and carrier both support RTT.
6981 * Otherwise. only returns true if the device and carrier both also support RTT.
Hall Liuf6668912018-10-31 17:05:23 -07006982 */
6983 public boolean isRttEnabled(int subscriptionId) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006984 final long identity = Binder.clearCallingIdentity();
6985 try {
Hall Liu63767ec2019-12-11 23:58:20 +00006986 boolean isRttSupported = isRttSupported(subscriptionId);
6987 boolean isUserRttSettingOn = Settings.Secure.getInt(
6988 mApp.getContentResolver(), Settings.Secure.RTT_CALLING_MODE, 0) != 0;
6989 boolean shouldIgnoreUserRttSetting = mApp.getCarrierConfigForSubId(subscriptionId)
6990 .getBoolean(CarrierConfigManager.KEY_IGNORE_RTT_MODE_SETTING_BOOL);
6991 return isRttSupported && (isUserRttSettingOn || shouldIgnoreUserRttSetting);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006992 } finally {
6993 Binder.restoreCallingIdentity(identity);
6994 }
Hall Liu3ad5f012018-04-06 16:23:39 -07006995 }
6996
Philip P. Moltmann9797cbb2020-01-07 13:45:00 -08006997 @Deprecated
6998 @Override
6999 public String getDeviceId(String callingPackage) {
7000 return getDeviceIdWithFeature(callingPackage, null);
7001 }
7002
Sanket Padawe7310cc72015-01-14 09:53:20 -08007003 /**
7004 * Returns the unique device ID of phone, for example, the IMEI for
7005 * GSM and the MEID for CDMA phones. Return null if device ID is not available.
7006 *
7007 * <p>Requires Permission:
7008 * {@link android.Manifest.permission#READ_PHONE_STATE READ_PHONE_STATE}
7009 */
7010 @Override
Philip P. Moltmann9797cbb2020-01-07 13:45:00 -08007011 public String getDeviceIdWithFeature(String callingPackage, String callingFeatureId) {
Sanket Padawe7310cc72015-01-14 09:53:20 -08007012 final Phone phone = PhoneFactory.getPhone(0);
Jeff Davidson913390f2018-02-23 17:11:49 -08007013 if (phone == null) {
Sanket Padawe7310cc72015-01-14 09:53:20 -08007014 return null;
7015 }
Jeff Davidson913390f2018-02-23 17:11:49 -08007016 int subId = phone.getSubId();
Michael Groover70af6dc2018-10-01 16:23:15 -07007017 if (!TelephonyPermissions.checkCallingOrSelfReadDeviceIdentifiers(mApp, subId,
Philip P. Moltmann9797cbb2020-01-07 13:45:00 -08007018 callingPackage, callingFeatureId, "getDeviceId")) {
Jeff Davidson913390f2018-02-23 17:11:49 -08007019 return null;
7020 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007021
7022 final long identity = Binder.clearCallingIdentity();
7023 try {
7024 return phone.getDeviceId();
7025 } finally {
7026 Binder.restoreCallingIdentity(identity);
7027 }
Sanket Padawe7310cc72015-01-14 09:53:20 -08007028 }
7029
Ping Sunc67b7c22016-03-02 19:16:45 +08007030 /**
7031 * {@hide}
7032 * Returns the IMS Registration Status on a particular subid
7033 *
7034 * @param subId
7035 */
Brad Ebinger1f2b5082018-02-08 16:11:32 -08007036 public boolean isImsRegistered(int subId) {
Ping Sunc67b7c22016-03-02 19:16:45 +08007037 Phone phone = getPhone(subId);
7038 if (phone != null) {
7039 return phone.isImsRegistered();
7040 } else {
7041 return false;
7042 }
7043 }
7044
Santos Cordon7a1885b2015-02-03 11:15:19 -08007045 @Override
7046 public int getSubIdForPhoneAccount(PhoneAccount phoneAccount) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007047 final long identity = Binder.clearCallingIdentity();
7048 try {
7049 return PhoneUtils.getSubIdForPhoneAccount(phoneAccount);
7050 } finally {
7051 Binder.restoreCallingIdentity(identity);
7052 }
Santos Cordon7a1885b2015-02-03 11:15:19 -08007053 }
Nathan Harolddcfc7932015-03-18 10:01:20 -07007054
Tyler Gunnf70ed162019-04-03 15:28:53 -07007055 @Override
Shuo Qian0762a782019-10-30 16:33:31 -07007056 public int getSubIdForPhoneAccountHandle(
Philip P. Moltmann9797cbb2020-01-07 13:45:00 -08007057 PhoneAccountHandle phoneAccountHandle, String callingPackage, String callingFeatureId) {
Shuo Qian0762a782019-10-30 16:33:31 -07007058 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(mApp, getDefaultSubscription(),
Philip P. Moltmann9797cbb2020-01-07 13:45:00 -08007059 callingPackage, callingFeatureId, "getSubIdForPhoneAccountHandle")) {
Shuo Qian0762a782019-10-30 16:33:31 -07007060 throw new SecurityException("Requires READ_PHONE_STATE permission.");
7061 }
7062 final long identity = Binder.clearCallingIdentity();
7063 try {
7064 return PhoneUtils.getSubIdForPhoneAccountHandle(phoneAccountHandle);
7065 } finally {
7066 Binder.restoreCallingIdentity(identity);
7067 }
7068 }
7069
7070 @Override
Tyler Gunnf70ed162019-04-03 15:28:53 -07007071 public @Nullable PhoneAccountHandle getPhoneAccountHandleForSubscriptionId(int subscriptionId) {
duki.hongfd96bde2020-07-22 17:32:19 +09007072 enforceReadPrivilegedPermission("getPhoneAccountHandleForSubscriptionId, "
7073 + "subscriptionId: " + subscriptionId);
Tyler Gunnf70ed162019-04-03 15:28:53 -07007074 final long identity = Binder.clearCallingIdentity();
7075 try {
7076 Phone phone = getPhone(subscriptionId);
7077 if (phone == null) {
7078 return null;
7079 }
7080 return PhoneUtils.makePstnPhoneAccountHandle(phone);
7081 } finally {
7082 Binder.restoreCallingIdentity(identity);
7083 }
7084 }
7085
Brad Ebinger1f2b5082018-02-08 16:11:32 -08007086 /**
7087 * @return the VoWiFi calling availability.
Nathan Haroldc55097a2015-03-11 18:14:50 -07007088 */
Brad Ebinger1f2b5082018-02-08 16:11:32 -08007089 public boolean isWifiCallingAvailable(int subId) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007090 final long identity = Binder.clearCallingIdentity();
7091 try {
7092 Phone phone = getPhone(subId);
7093 if (phone != null) {
7094 return phone.isWifiCallingEnabled();
7095 } else {
7096 return false;
7097 }
7098 } finally {
7099 Binder.restoreCallingIdentity(identity);
Brad Ebinger1f2b5082018-02-08 16:11:32 -08007100 }
Nathan Haroldc55097a2015-03-11 18:14:50 -07007101 }
7102
Brad Ebinger1f2b5082018-02-08 16:11:32 -08007103 /**
Brad Ebinger1f2b5082018-02-08 16:11:32 -08007104 * @return the VT calling availability.
Etan Cohen3b7a1bc2015-05-28 15:57:13 -07007105 */
Brad Ebinger1f2b5082018-02-08 16:11:32 -08007106 public boolean isVideoTelephonyAvailable(int subId) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007107 final long identity = Binder.clearCallingIdentity();
7108 try {
7109 Phone phone = getPhone(subId);
7110 if (phone != null) {
7111 return phone.isVideoEnabled();
7112 } else {
7113 return false;
7114 }
7115 } finally {
7116 Binder.restoreCallingIdentity(identity);
Brad Ebinger1f2b5082018-02-08 16:11:32 -08007117 }
7118 }
7119
7120 /**
7121 * @return the IMS registration technology for the MMTEL feature. Valid return values are
7122 * defined in {@link ImsRegistrationImplBase}.
7123 */
7124 public @ImsRegistrationImplBase.ImsRegistrationTech int getImsRegTechnologyForMmTel(int subId) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007125 final long identity = Binder.clearCallingIdentity();
7126 try {
7127 Phone phone = getPhone(subId);
7128 if (phone != null) {
7129 return phone.getImsRegistrationTech();
7130 } else {
7131 return ImsRegistrationImplBase.REGISTRATION_TECH_NONE;
7132 }
7133 } finally {
7134 Binder.restoreCallingIdentity(identity);
Brad Ebinger1f2b5082018-02-08 16:11:32 -08007135 }
Etan Cohen3b7a1bc2015-05-28 15:57:13 -07007136 }
7137
Stuart Scott8eef64f2015-04-08 15:13:54 -07007138 @Override
7139 public void factoryReset(int subId) {
paulhu423b5f22019-08-23 19:17:33 +08007140 enforceSettingsPermission();
Stuart Scott981d8582015-04-21 14:09:50 -07007141 if (mUserManager.hasUserRestriction(UserManager.DISALLOW_NETWORK_RESET)) {
7142 return;
7143 }
7144
Svet Ganovcc087f82015-05-12 20:35:54 -07007145 final long identity = Binder.clearCallingIdentity();
Nazanin Bakhshif782e562018-12-11 15:15:39 -08007146
Svet Ganovcc087f82015-05-12 20:35:54 -07007147 try {
Stuart Scott981d8582015-04-21 14:09:50 -07007148 if (SubscriptionManager.isUsableSubIdValue(subId) && !mUserManager.hasUserRestriction(
7149 UserManager.DISALLOW_CONFIG_MOBILE_NETWORKS)) {
Sooraj Sasindranff75de62020-07-15 01:35:24 -07007150 setDataEnabledForReason(subId, TelephonyManager.DATA_ENABLED_REASON_USER,
Sooraj Sasindran2675c0b2020-04-19 18:14:03 -07007151 getDefaultDataEnabled());
Svet Ganovcc087f82015-05-12 20:35:54 -07007152 setNetworkSelectionModeAutomatic(subId);
Pengquan Meng85728fb2018-03-12 16:31:21 -07007153 setPreferredNetworkType(subId, getDefaultNetworkType(subId));
Nazanin Bakhshif782e562018-12-11 15:15:39 -08007154 setDataRoamingEnabled(subId, getDefaultDataRoamingEnabled(subId));
7155 CarrierInfoManager.deleteAllCarrierKeysForImsiEncryption(mApp);
Svet Ganovcc087f82015-05-12 20:35:54 -07007156 }
Amit Mahajan7dbbd822019-03-13 17:33:47 -07007157 // There has been issues when Sms raw table somehow stores orphan
7158 // fragments. They lead to garbled message when new fragments come
7159 // in and combined with those stale ones. In case this happens again,
7160 // user can reset all network settings which will clean up this table.
7161 cleanUpSmsRawTable(getDefaultPhone().getContext());
Brad Ebinger77b832e2019-10-17 17:03:22 -07007162 // Clean up IMS settings as well here.
7163 int slotId = getSlotIndex(subId);
7164 if (slotId > SubscriptionManager.INVALID_SIM_SLOT_INDEX) {
7165 ImsManager.getInstance(mApp, slotId).factoryReset();
7166 }
Naina Nalluri8ff344d2019-09-17 14:10:30 -07007167
7168 // Erase modem config if erase modem on network setting is enabled.
7169 String configValue = DeviceConfig.getProperty(DeviceConfig.NAMESPACE_TELEPHONY,
7170 RESET_NETWORK_ERASE_MODEM_CONFIG_ENABLED);
7171 if (configValue != null && Boolean.parseBoolean(configValue)) {
7172 sendEraseModemConfig(getDefaultPhone());
7173 }
Svet Ganovcc087f82015-05-12 20:35:54 -07007174 } finally {
7175 Binder.restoreCallingIdentity(identity);
Stuart Scott8eef64f2015-04-08 15:13:54 -07007176 }
7177 }
Narayan Kamath1c496c22015-04-16 14:40:19 +01007178
Amit Mahajan7dbbd822019-03-13 17:33:47 -07007179 private void cleanUpSmsRawTable(Context context) {
7180 ContentResolver resolver = context.getContentResolver();
7181 Uri uri = Uri.withAppendedPath(Telephony.Sms.CONTENT_URI, "raw/permanentDelete");
7182 resolver.delete(uri, null, null);
7183 }
7184
Narayan Kamath1c496c22015-04-16 14:40:19 +01007185 @Override
chen xu5d3637b2019-01-21 23:31:38 -08007186 public String getSimLocaleForSubscriber(int subId) {
7187 enforceReadPrivilegedPermission("getSimLocaleForSubscriber, subId: " + subId);
7188 final Phone phone = getPhone(subId);
7189 if (phone == null) {
7190 log("getSimLocaleForSubscriber, invalid subId");
chen xu2bb91e42019-01-24 14:35:54 -08007191 return null;
chen xu5d3637b2019-01-21 23:31:38 -08007192 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007193 final long identity = Binder.clearCallingIdentity();
7194 try {
chen xu5d3637b2019-01-21 23:31:38 -08007195 final SubscriptionInfo info = mSubscriptionController.getActiveSubscriptionInfo(subId,
Philip P. Moltmann0079aae2020-03-05 16:24:02 -08007196 phone.getContext().getOpPackageName(), phone.getContext().getAttributionTag());
chen xu6291c472019-02-04 12:55:53 -08007197 if (info == null) {
7198 log("getSimLocaleForSubscriber, inactive subId: " + subId);
7199 return null;
7200 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007201 // Try and fetch the locale from the carrier properties or from the SIM language
7202 // preferences (EF-PL and EF-LI)...
7203 final int mcc = info.getMcc();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007204 String simLanguage = null;
chen xu5d3637b2019-01-21 23:31:38 -08007205 final Locale localeFromDefaultSim = phone.getLocaleFromSimAndCarrierPrefs();
7206 if (localeFromDefaultSim != null) {
7207 if (!localeFromDefaultSim.getCountry().isEmpty()) {
7208 if (DBG) log("Using locale from subId: " + subId + " locale: "
7209 + localeFromDefaultSim);
7210 return localeFromDefaultSim.toLanguageTag();
7211 } else {
7212 simLanguage = localeFromDefaultSim.getLanguage();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007213 }
7214 }
Narayan Kamath1c496c22015-04-16 14:40:19 +01007215
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007216 // The SIM language preferences only store a language (e.g. fr = French), not an
7217 // exact locale (e.g. fr_FR = French/France). So, if the locale returned from
7218 // the SIM and carrier preferences does not include a country we add the country
7219 // determined from the SIM MCC to provide an exact locale.
zoey chen84e2b212019-12-18 17:07:20 +08007220 final Locale mccLocale = LocaleUtils.getLocaleFromMcc(mApp, mcc, simLanguage);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007221 if (mccLocale != null) {
chen xu5d3637b2019-01-21 23:31:38 -08007222 if (DBG) log("No locale from SIM, using mcc locale:" + mccLocale);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007223 return mccLocale.toLanguageTag();
7224 }
7225
7226 if (DBG) log("No locale found - returning null");
7227 return null;
7228 } finally {
7229 Binder.restoreCallingIdentity(identity);
7230 }
Narayan Kamath1c496c22015-04-16 14:40:19 +01007231 }
7232
7233 private List<SubscriptionInfo> getAllSubscriptionInfoList() {
Philip P. Moltmann9797cbb2020-01-07 13:45:00 -08007234 return mSubscriptionController.getAllSubInfoList(mApp.getOpPackageName(),
Philip P. Moltmann0079aae2020-03-05 16:24:02 -08007235 mApp.getAttributionTag());
Narayan Kamath1c496c22015-04-16 14:40:19 +01007236 }
7237
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007238 /**
7239 * NOTE: this method assumes permission checks are done and caller identity has been cleared.
7240 */
7241 private List<SubscriptionInfo> getActiveSubscriptionInfoListPrivileged() {
Philip P. Moltmann9797cbb2020-01-07 13:45:00 -08007242 return mSubscriptionController.getActiveSubscriptionInfoList(mApp.getOpPackageName(),
Philip P. Moltmann0079aae2020-03-05 16:24:02 -08007243 mApp.getAttributionTag());
Narayan Kamath1c496c22015-04-16 14:40:19 +01007244 }
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -07007245
Chenjie Yu1ba97252018-01-11 18:16:20 -08007246 private final ModemActivityInfo mLastModemActivityInfo =
Chen Xu13851032019-09-10 18:49:52 -07007247 new ModemActivityInfo(0, 0, 0, new int[0], 0);
Chenjie Yu1ba97252018-01-11 18:16:20 -08007248
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -07007249 /**
Adam Lesinski903a54c2016-04-11 14:49:52 -07007250 * Responds to the ResultReceiver with the {@link android.telephony.ModemActivityInfo} object
7251 * representing the state of the modem.
7252 *
Chenjie Yu1ba97252018-01-11 18:16:20 -08007253 * NOTE: The underlying implementation clears the modem state, so there should only ever be one
7254 * caller to it. Everyone should call this class to get cumulative data.
Adam Lesinski903a54c2016-04-11 14:49:52 -07007255 * @hide
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -07007256 */
7257 @Override
Adam Lesinski903a54c2016-04-11 14:49:52 -07007258 public void requestModemActivityInfo(ResultReceiver result) {
7259 enforceModifyPermission();
vagdeviaf9a5b92018-08-15 16:01:53 -07007260 WorkSource workSource = getWorkSource(Binder.getCallingUid());
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007261
7262 final long identity = Binder.clearCallingIdentity();
7263 try {
sqian1a1be542020-03-05 11:37:28 -08007264 sendRequestAsync(CMD_GET_MODEM_ACTIVITY_INFO, result, null, workSource);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007265 } finally {
7266 Binder.restoreCallingIdentity(identity);
Chenjie Yu1ba97252018-01-11 18:16:20 -08007267 }
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -07007268 }
Jack Yu85bd38a2015-11-09 11:34:32 -08007269
Siddharth Rayb8114062018-06-17 15:02:38 -07007270 // Checks that ModemActivityInfo is valid. Sleep time, Idle time, Rx time and Tx time should be
7271 // less than total activity duration.
7272 private boolean isModemActivityInfoValid(ModemActivityInfo info) {
7273 if (info == null) {
7274 return false;
7275 }
7276 int activityDurationMs =
7277 (int) (info.getTimestamp() - mLastModemActivityInfo.getTimestamp());
7278 int totalTxTimeMs = 0;
Chen Xu13851032019-09-10 18:49:52 -07007279 int[] txTimeMs = info.getTransmitTimeMillis();
7280 for (int i = 0; i < info.getTransmitPowerInfo().size(); i++) {
7281 totalTxTimeMs += txTimeMs[i];
Siddharth Rayb8114062018-06-17 15:02:38 -07007282 }
7283 return (info.isValid()
7284 && (info.getSleepTimeMillis() <= activityDurationMs)
7285 && (info.getIdleTimeMillis() <= activityDurationMs)
Chen Xu13851032019-09-10 18:49:52 -07007286 && (info.getReceiveTimeMillis() <= activityDurationMs)
Siddharth Rayb8114062018-06-17 15:02:38 -07007287 && (totalTxTimeMs <= activityDurationMs));
7288 }
7289
Jack Yu85bd38a2015-11-09 11:34:32 -08007290 /**
7291 * {@hide}
7292 * Returns the service state information on specified subscription.
7293 */
7294 @Override
Philip P. Moltmann9797cbb2020-01-07 13:45:00 -08007295 public ServiceState getServiceStateForSubscriber(int subId, String callingPackage,
7296 String callingFeatureId) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08007297 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
Philip P. Moltmann9797cbb2020-01-07 13:45:00 -08007298 mApp, subId, callingPackage, callingFeatureId, "getServiceStateForSubscriber")) {
Jack Yu85bd38a2015-11-09 11:34:32 -08007299 return null;
7300 }
7301
Hall Liuf19c44f2018-11-27 14:38:17 -08007302 LocationAccessPolicy.LocationPermissionResult fineLocationResult =
7303 LocationAccessPolicy.checkLocationPermission(mApp,
7304 new LocationAccessPolicy.LocationPermissionQuery.Builder()
7305 .setCallingPackage(callingPackage)
Philip P. Moltmann9797cbb2020-01-07 13:45:00 -08007306 .setCallingFeatureId(callingFeatureId)
Hall Liuf19c44f2018-11-27 14:38:17 -08007307 .setCallingPid(Binder.getCallingPid())
7308 .setCallingUid(Binder.getCallingUid())
7309 .setMethod("getServiceStateForSubscriber")
Hall Liuf18a0cf2019-04-17 13:37:11 -07007310 .setLogAsInfo(true)
Hall Liuf19c44f2018-11-27 14:38:17 -08007311 .setMinSdkVersionForFine(Build.VERSION_CODES.Q)
7312 .build());
7313
7314 LocationAccessPolicy.LocationPermissionResult coarseLocationResult =
7315 LocationAccessPolicy.checkLocationPermission(mApp,
7316 new LocationAccessPolicy.LocationPermissionQuery.Builder()
7317 .setCallingPackage(callingPackage)
Philip P. Moltmann9797cbb2020-01-07 13:45:00 -08007318 .setCallingFeatureId(callingFeatureId)
Hall Liuf19c44f2018-11-27 14:38:17 -08007319 .setCallingPid(Binder.getCallingPid())
7320 .setCallingUid(Binder.getCallingUid())
7321 .setMethod("getServiceStateForSubscriber")
Hall Liuf18a0cf2019-04-17 13:37:11 -07007322 .setLogAsInfo(true)
Hall Liuf19c44f2018-11-27 14:38:17 -08007323 .setMinSdkVersionForCoarse(Build.VERSION_CODES.Q)
7324 .build());
7325 // We don't care about hard or soft here -- all we need to know is how much info to scrub.
7326 boolean hasFinePermission =
7327 fineLocationResult == LocationAccessPolicy.LocationPermissionResult.ALLOWED;
7328 boolean hasCoarsePermission =
7329 coarseLocationResult == LocationAccessPolicy.LocationPermissionResult.ALLOWED;
7330
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007331 final long identity = Binder.clearCallingIdentity();
7332 try {
7333 final Phone phone = getPhone(subId);
7334 if (phone == null) {
7335 return null;
7336 }
Jack Yu85bd38a2015-11-09 11:34:32 -08007337
Hall Liuf19c44f2018-11-27 14:38:17 -08007338 ServiceState ss = phone.getServiceState();
7339
7340 // Scrub out the location info in ServiceState depending on what level of access
7341 // the caller has.
7342 if (hasFinePermission) return ss;
Malcolm Chendb337972019-12-30 13:56:38 -08007343 if (hasCoarsePermission) return ss.createLocationInfoSanitizedCopy(false);
7344 return ss.createLocationInfoSanitizedCopy(true);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007345 } finally {
7346 Binder.restoreCallingIdentity(identity);
7347 }
Jack Yu85bd38a2015-11-09 11:34:32 -08007348 }
Nancy Chen31f9ba12016-01-06 11:42:12 -08007349
7350 /**
7351 * Returns the URI for the per-account voicemail ringtone set in Phone settings.
7352 *
7353 * @param accountHandle The handle for the {@link PhoneAccount} for which to retrieve the
7354 * voicemail ringtone.
7355 * @return The URI for the ringtone to play when receiving a voicemail from a specific
7356 * PhoneAccount.
7357 */
7358 @Override
7359 public Uri getVoicemailRingtoneUri(PhoneAccountHandle accountHandle) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007360 final long identity = Binder.clearCallingIdentity();
7361 try {
7362 Phone phone = PhoneUtils.getPhoneForPhoneAccountHandle(accountHandle);
7363 if (phone == null) {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08007364 phone = getDefaultPhone();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007365 }
Nancy Chen31f9ba12016-01-06 11:42:12 -08007366
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007367 return VoicemailNotificationSettingsUtil.getRingtoneUri(phone.getContext());
7368 } finally {
7369 Binder.restoreCallingIdentity(identity);
7370 }
Nancy Chen31f9ba12016-01-06 11:42:12 -08007371 }
7372
7373 /**
Ta-wei Yenc33877d2017-01-23 18:11:21 -08007374 * Sets the per-account voicemail ringtone.
7375 *
7376 * <p>Requires that the calling app is the default dialer, or has carrier privileges, or
7377 * has permission {@link android.Manifest.permission#MODIFY_PHONE_STATE MODIFY_PHONE_STATE}.
7378 *
7379 * @param phoneAccountHandle The handle for the {@link PhoneAccount} for which to set the
7380 * voicemail ringtone.
7381 * @param uri The URI for the ringtone to play when receiving a voicemail from a specific
7382 * PhoneAccount.
7383 */
7384 @Override
7385 public void setVoicemailRingtoneUri(String callingPackage,
7386 PhoneAccountHandle phoneAccountHandle, Uri uri) {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08007387 final Phone defaultPhone = getDefaultPhone();
Ta-wei Yenc33877d2017-01-23 18:11:21 -08007388 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
Tyler Gunn77ee9382019-10-31 13:08:23 -07007389 TelecomManager tm = defaultPhone.getContext().getSystemService(TelecomManager.class);
7390 if (!TextUtils.equals(callingPackage, tm.getDefaultDialerPackage())) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08007391 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
7392 mApp, PhoneUtils.getSubIdForPhoneAccountHandle(phoneAccountHandle),
7393 "setVoicemailRingtoneUri");
Ta-wei Yenc33877d2017-01-23 18:11:21 -08007394 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007395
7396 final long identity = Binder.clearCallingIdentity();
7397 try {
7398 Phone phone = PhoneUtils.getPhoneForPhoneAccountHandle(phoneAccountHandle);
7399 if (phone == null) {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08007400 phone = defaultPhone;
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007401 }
7402 VoicemailNotificationSettingsUtil.setRingtoneUri(phone.getContext(), uri);
7403 } finally {
7404 Binder.restoreCallingIdentity(identity);
Ta-wei Yenc33877d2017-01-23 18:11:21 -08007405 }
Ta-wei Yenc33877d2017-01-23 18:11:21 -08007406 }
7407
7408 /**
Nancy Chen31f9ba12016-01-06 11:42:12 -08007409 * Returns whether vibration is set for voicemail notification in Phone settings.
7410 *
7411 * @param accountHandle The handle for the {@link PhoneAccount} for which to retrieve the
7412 * voicemail vibration setting.
7413 * @return {@code true} if the vibration is set for this PhoneAccount, {@code false} otherwise.
7414 */
7415 @Override
7416 public boolean isVoicemailVibrationEnabled(PhoneAccountHandle accountHandle) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007417 final long identity = Binder.clearCallingIdentity();
7418 try {
7419 Phone phone = PhoneUtils.getPhoneForPhoneAccountHandle(accountHandle);
7420 if (phone == null) {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08007421 phone = getDefaultPhone();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007422 }
Nancy Chen31f9ba12016-01-06 11:42:12 -08007423
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007424 return VoicemailNotificationSettingsUtil.isVibrationEnabled(phone.getContext());
7425 } finally {
7426 Binder.restoreCallingIdentity(identity);
7427 }
Nancy Chen31f9ba12016-01-06 11:42:12 -08007428 }
7429
Youhan Wange64578a2016-05-02 15:32:42 -07007430 /**
Ta-wei Yenc33877d2017-01-23 18:11:21 -08007431 * Sets the per-account voicemail vibration.
7432 *
7433 * <p>Requires that the calling app is the default dialer, or has carrier privileges, or
7434 * has permission {@link android.Manifest.permission#MODIFY_PHONE_STATE MODIFY_PHONE_STATE}.
7435 *
7436 * @param phoneAccountHandle The handle for the {@link PhoneAccount} for which to set the
7437 * voicemail vibration setting.
7438 * @param enabled Whether to enable or disable vibration for voicemail notifications from a
7439 * specific PhoneAccount.
7440 */
7441 @Override
7442 public void setVoicemailVibrationEnabled(String callingPackage,
7443 PhoneAccountHandle phoneAccountHandle, boolean enabled) {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08007444 final Phone defaultPhone = getDefaultPhone();
Ta-wei Yenc33877d2017-01-23 18:11:21 -08007445 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
Tyler Gunn77ee9382019-10-31 13:08:23 -07007446 TelecomManager tm = defaultPhone.getContext().getSystemService(TelecomManager.class);
7447 if (!TextUtils.equals(callingPackage, tm.getDefaultDialerPackage())) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08007448 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
7449 mApp, PhoneUtils.getSubIdForPhoneAccountHandle(phoneAccountHandle),
7450 "setVoicemailVibrationEnabled");
Ta-wei Yenc33877d2017-01-23 18:11:21 -08007451 }
7452
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007453 final long identity = Binder.clearCallingIdentity();
7454 try {
7455 Phone phone = PhoneUtils.getPhoneForPhoneAccountHandle(phoneAccountHandle);
7456 if (phone == null) {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08007457 phone = defaultPhone;
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007458 }
7459 VoicemailNotificationSettingsUtil.setVibrationEnabled(phone.getContext(), enabled);
7460 } finally {
7461 Binder.restoreCallingIdentity(identity);
Ta-wei Yenc33877d2017-01-23 18:11:21 -08007462 }
Ta-wei Yenc33877d2017-01-23 18:11:21 -08007463 }
7464
7465 /**
Youhan Wange64578a2016-05-02 15:32:42 -07007466 * Make sure either called from same process as self (phone) or IPC caller has read privilege.
7467 *
7468 * @throws SecurityException if the caller does not have the required permission
7469 */
Brad Ebinger35c841c2018-10-01 10:40:55 -07007470 private void enforceReadPrivilegedPermission(String message) {
Youhan Wange64578a2016-05-02 15:32:42 -07007471 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.READ_PRIVILEGED_PHONE_STATE,
Brad Ebinger35c841c2018-10-01 10:40:55 -07007472 message);
Youhan Wange64578a2016-05-02 15:32:42 -07007473 }
7474
7475 /**
Ta-wei Yen30a69c82016-12-27 14:52:32 -08007476 * Make sure either called from same process as self (phone) or IPC caller has send SMS
7477 * permission.
7478 *
7479 * @throws SecurityException if the caller does not have the required permission
7480 */
7481 private void enforceSendSmsPermission() {
7482 mApp.enforceCallingOrSelfPermission(permission.SEND_SMS, null);
7483 }
7484
7485 /**
Ta-wei Yen527a9c02017-01-06 15:29:25 -08007486 * Make sure called from the package in charge of visual voicemail.
Ta-wei Yen30a69c82016-12-27 14:52:32 -08007487 *
Ta-wei Yen527a9c02017-01-06 15:29:25 -08007488 * @throws SecurityException if the caller is not the visual voicemail package.
Ta-wei Yen30a69c82016-12-27 14:52:32 -08007489 */
Ta-wei Yen527a9c02017-01-06 15:29:25 -08007490 private void enforceVisualVoicemailPackage(String callingPackage, int subId) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007491 final long identity = Binder.clearCallingIdentity();
7492 try {
7493 ComponentName componentName =
Nazanin Bakhshif782e562018-12-11 15:15:39 -08007494 RemoteVvmTaskManager.getRemotePackage(mApp, subId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007495 if (componentName == null) {
7496 throw new SecurityException(
7497 "Caller not current active visual voicemail package[null]");
7498 }
7499 String vvmPackage = componentName.getPackageName();
7500 if (!callingPackage.equals(vvmPackage)) {
7501 throw new SecurityException("Caller not current active visual voicemail package["
7502 + vvmPackage + "]");
7503 }
7504 } finally {
7505 Binder.restoreCallingIdentity(identity);
Ta-wei Yen30a69c82016-12-27 14:52:32 -08007506 }
7507 }
7508
7509 /**
Youhan Wange64578a2016-05-02 15:32:42 -07007510 * Return the application ID for the app type.
7511 *
7512 * @param subId the subscription ID that this request applies to.
7513 * @param appType the uicc app type.
7514 * @return Application ID for specificied app type, or null if no uicc.
7515 */
7516 @Override
7517 public String getAidForAppType(int subId, int appType) {
Brad Ebinger35c841c2018-10-01 10:40:55 -07007518 enforceReadPrivilegedPermission("getAidForAppType");
Youhan Wange64578a2016-05-02 15:32:42 -07007519 Phone phone = getPhone(subId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007520
7521 final long identity = Binder.clearCallingIdentity();
Youhan Wange64578a2016-05-02 15:32:42 -07007522 try {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007523 if (phone == null) {
7524 return null;
7525 }
7526 String aid = null;
7527 try {
7528 aid = UiccController.getInstance().getUiccCard(phone.getPhoneId())
7529 .getApplicationByType(appType).getAid();
7530 } catch (Exception e) {
7531 Log.e(LOG_TAG, "Not getting aid. Exception ex=" + e);
7532 }
7533 return aid;
7534 } finally {
7535 Binder.restoreCallingIdentity(identity);
Youhan Wange64578a2016-05-02 15:32:42 -07007536 }
Youhan Wange64578a2016-05-02 15:32:42 -07007537 }
7538
Youhan Wang4001d252016-05-11 10:29:41 -07007539 /**
7540 * Return the Electronic Serial Number.
7541 *
7542 * @param subId the subscription ID that this request applies to.
7543 * @return ESN or null if error.
7544 */
7545 @Override
7546 public String getEsn(int subId) {
Brad Ebinger35c841c2018-10-01 10:40:55 -07007547 enforceReadPrivilegedPermission("getEsn");
Youhan Wang4001d252016-05-11 10:29:41 -07007548 Phone phone = getPhone(subId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007549
7550 final long identity = Binder.clearCallingIdentity();
Youhan Wang4001d252016-05-11 10:29:41 -07007551 try {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007552 if (phone == null) {
7553 return null;
7554 }
7555 String esn = null;
7556 try {
7557 esn = phone.getEsn();
7558 } catch (Exception e) {
7559 Log.e(LOG_TAG, "Not getting ESN. Exception ex=" + e);
7560 }
7561 return esn;
7562 } finally {
7563 Binder.restoreCallingIdentity(identity);
Youhan Wang4001d252016-05-11 10:29:41 -07007564 }
Youhan Wang4001d252016-05-11 10:29:41 -07007565 }
7566
Sanket Padawe99ef1e32016-05-18 16:12:33 -07007567 /**
Youhan Wang66ad5d72016-07-18 17:56:58 -07007568 * Return the Preferred Roaming List Version.
7569 *
7570 * @param subId the subscription ID that this request applies to.
7571 * @return PRLVersion or null if error.
7572 */
7573 @Override
7574 public String getCdmaPrlVersion(int subId) {
Brad Ebinger35c841c2018-10-01 10:40:55 -07007575 enforceReadPrivilegedPermission("getCdmaPrlVersion");
Youhan Wang66ad5d72016-07-18 17:56:58 -07007576 Phone phone = getPhone(subId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007577
7578 final long identity = Binder.clearCallingIdentity();
Youhan Wang66ad5d72016-07-18 17:56:58 -07007579 try {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007580 if (phone == null) {
7581 return null;
7582 }
7583 String cdmaPrlVersion = null;
7584 try {
7585 cdmaPrlVersion = phone.getCdmaPrlVersion();
7586 } catch (Exception e) {
7587 Log.e(LOG_TAG, "Not getting PRLVersion", e);
7588 }
7589 return cdmaPrlVersion;
7590 } finally {
7591 Binder.restoreCallingIdentity(identity);
Youhan Wang66ad5d72016-07-18 17:56:58 -07007592 }
Youhan Wang66ad5d72016-07-18 17:56:58 -07007593 }
7594
7595 /**
Sanket Padawe99ef1e32016-05-18 16:12:33 -07007596 * Get snapshot of Telephony histograms
7597 * @return List of Telephony histograms
7598 * @hide
7599 */
7600 @Override
7601 public List<TelephonyHistogram> getTelephonyHistograms() {
Jeff Davidson7e17e312018-02-13 18:17:36 -08007602 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
7603 mApp, getDefaultSubscription(), "getTelephonyHistograms");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007604
7605 final long identity = Binder.clearCallingIdentity();
7606 try {
7607 return RIL.getTelephonyRILTimingHistograms();
7608 } finally {
7609 Binder.restoreCallingIdentity(identity);
7610 }
Sanket Padawe99ef1e32016-05-18 16:12:33 -07007611 }
Meng Wang1a7c35a2016-05-05 20:56:15 -07007612
7613 /**
7614 * {@hide}
Michele Berionne482f8202018-11-27 18:57:59 -08007615 * Set the allowed carrier list and the excluded carrier list, indicating the priority between
7616 * the two lists.
Meng Wang1a7c35a2016-05-05 20:56:15 -07007617 * Require system privileges. In the future we may add this to carrier APIs.
7618 *
Michele Berionne482f8202018-11-27 18:57:59 -08007619 * @return Integer with the result of the operation, as defined in {@link TelephonyManager}.
Meng Wang1a7c35a2016-05-05 20:56:15 -07007620 */
7621 @Override
Michele Berionne482f8202018-11-27 18:57:59 -08007622 @TelephonyManager.SetCarrierRestrictionResult
7623 public int setAllowedCarriers(CarrierRestrictionRules carrierRestrictionRules) {
Meng Wang1a7c35a2016-05-05 20:56:15 -07007624 enforceModifyPermission();
vagdeviaf9a5b92018-08-15 16:01:53 -07007625 WorkSource workSource = getWorkSource(Binder.getCallingUid());
Sanket Padawe13bac7b2017-03-20 15:04:47 -07007626
Michele Berionne482f8202018-11-27 18:57:59 -08007627 if (carrierRestrictionRules == null) {
7628 throw new NullPointerException("carrier restriction cannot be null");
Meng Wang9b7c4e92017-02-17 11:41:27 -08007629 }
Sanket Padawe13bac7b2017-03-20 15:04:47 -07007630
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007631 final long identity = Binder.clearCallingIdentity();
7632 try {
Michele Berionne482f8202018-11-27 18:57:59 -08007633 return (int) sendRequest(CMD_SET_ALLOWED_CARRIERS, carrierRestrictionRules,
vagdeviaf9a5b92018-08-15 16:01:53 -07007634 workSource);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007635 } finally {
7636 Binder.restoreCallingIdentity(identity);
7637 }
Meng Wang1a7c35a2016-05-05 20:56:15 -07007638 }
7639
7640 /**
7641 * {@hide}
Michele Berionne482f8202018-11-27 18:57:59 -08007642 * Get the allowed carrier list and the excluded carrier list, including the priority between
7643 * the two lists.
Meng Wang1a7c35a2016-05-05 20:56:15 -07007644 * Require system privileges. In the future we may add this to carrier APIs.
7645 *
Michele Berionne482f8202018-11-27 18:57:59 -08007646 * @return {@link android.telephony.CarrierRestrictionRules}
Meng Wang1a7c35a2016-05-05 20:56:15 -07007647 */
7648 @Override
Michele Berionne482f8202018-11-27 18:57:59 -08007649 public CarrierRestrictionRules getAllowedCarriers() {
Brad Ebinger35c841c2018-10-01 10:40:55 -07007650 enforceReadPrivilegedPermission("getAllowedCarriers");
vagdeviaf9a5b92018-08-15 16:01:53 -07007651 WorkSource workSource = getWorkSource(Binder.getCallingUid());
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007652
7653 final long identity = Binder.clearCallingIdentity();
7654 try {
Michele Berionne482f8202018-11-27 18:57:59 -08007655 Object response = sendRequest(CMD_GET_ALLOWED_CARRIERS, null, workSource);
7656 if (response instanceof CarrierRestrictionRules) {
7657 return (CarrierRestrictionRules) response;
7658 }
7659 // Response is an Exception of some kind,
7660 // which is signalled to the user as a NULL retval
7661 return null;
7662 } catch (Exception e) {
7663 Log.e(LOG_TAG, "getAllowedCarriers. Exception ex=" + e);
7664 return null;
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007665 } finally {
7666 Binder.restoreCallingIdentity(identity);
7667 }
Meng Wang1a7c35a2016-05-05 20:56:15 -07007668 }
7669
fionaxu59545b42016-05-25 15:53:37 -07007670 /**
fionaxu59545b42016-05-25 15:53:37 -07007671 * Action set from carrier signalling broadcast receivers to enable/disable radio
7672 * @param subId the subscription ID that this action applies to.
7673 * @param enabled control enable or disable radio.
7674 * {@hide}
7675 */
7676 @Override
7677 public void carrierActionSetRadioEnabled(int subId, boolean enabled) {
7678 enforceModifyPermission();
7679 final Phone phone = getPhone(subId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007680
7681 final long identity = Binder.clearCallingIdentity();
fionaxu59545b42016-05-25 15:53:37 -07007682 if (phone == null) {
7683 loge("carrierAction: SetRadioEnabled fails with invalid sibId: " + subId);
7684 return;
7685 }
7686 try {
7687 phone.carrierActionSetRadioEnabled(enabled);
7688 } catch (Exception e) {
7689 Log.e(LOG_TAG, "carrierAction: SetRadioEnabled fails. Exception ex=" + e);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007690 } finally {
7691 Binder.restoreCallingIdentity(identity);
fionaxu59545b42016-05-25 15:53:37 -07007692 }
7693 }
7694
Ta-wei Yenc236d6b2016-06-21 13:33:12 -07007695 /**
fionaxu8da9cb12017-05-23 15:02:46 -07007696 * Action set from carrier signalling broadcast receivers to start/stop reporting the default
7697 * network status based on which carrier apps could apply actions accordingly,
7698 * enable/disable default url handler for example.
7699 *
7700 * @param subId the subscription ID that this action applies to.
7701 * @param report control start/stop reporting the default network status.
7702 * {@hide}
7703 */
7704 @Override
7705 public void carrierActionReportDefaultNetworkStatus(int subId, boolean report) {
7706 enforceModifyPermission();
7707 final Phone phone = getPhone(subId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007708
7709 final long identity = Binder.clearCallingIdentity();
fionaxu8da9cb12017-05-23 15:02:46 -07007710 if (phone == null) {
7711 loge("carrierAction: ReportDefaultNetworkStatus fails with invalid sibId: " + subId);
7712 return;
7713 }
7714 try {
7715 phone.carrierActionReportDefaultNetworkStatus(report);
7716 } catch (Exception e) {
7717 Log.e(LOG_TAG, "carrierAction: ReportDefaultNetworkStatus fails. Exception ex=" + e);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007718 } finally {
7719 Binder.restoreCallingIdentity(identity);
fionaxu8da9cb12017-05-23 15:02:46 -07007720 }
7721 }
7722
7723 /**
fionaxud9622282017-07-17 17:51:30 -07007724 * Action set from carrier signalling broadcast receivers to reset all carrier actions
7725 * @param subId the subscription ID that this action applies to.
7726 * {@hide}
7727 */
7728 @Override
7729 public void carrierActionResetAll(int subId) {
7730 enforceModifyPermission();
7731 final Phone phone = getPhone(subId);
7732 if (phone == null) {
7733 loge("carrierAction: ResetAll fails with invalid sibId: " + subId);
7734 return;
7735 }
7736 try {
7737 phone.carrierActionResetAll();
7738 } catch (Exception e) {
7739 Log.e(LOG_TAG, "carrierAction: ResetAll fails. Exception ex=" + e);
7740 }
7741 }
7742
7743 /**
Ta-wei Yenc236d6b2016-06-21 13:33:12 -07007744 * Called when "adb shell dumpsys phone" is invoked. Dump is also automatically invoked when a
7745 * bug report is being generated.
7746 */
7747 @Override
Ta-wei Yen99282e02016-06-21 18:19:35 -07007748 protected void dump(FileDescriptor fd, PrintWriter writer, String[] args) {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08007749 if (mApp.checkCallingOrSelfPermission(android.Manifest.permission.DUMP)
7750 != PackageManager.PERMISSION_GRANTED) {
dcashman22b950d2016-06-27 11:39:02 -07007751 writer.println("Permission Denial: can't dump Phone from pid="
7752 + Binder.getCallingPid()
7753 + ", uid=" + Binder.getCallingUid()
7754 + "without permission "
7755 + android.Manifest.permission.DUMP);
7756 return;
7757 }
Nazanin Bakhshif782e562018-12-11 15:15:39 -08007758 DumpsysHandler.dump(mApp, fd, writer, args);
Ta-wei Yenc236d6b2016-06-21 13:33:12 -07007759 }
Jack Yueb89b242016-06-22 13:27:47 -07007760
Brad Ebingerdac2f002018-04-03 15:17:52 -07007761 @Override
Hall Liua1548bd2019-12-24 14:14:12 -08007762 public int handleShellCommand(@NonNull ParcelFileDescriptor in,
7763 @NonNull ParcelFileDescriptor out, @NonNull ParcelFileDescriptor err,
7764 @NonNull String[] args) {
7765 return new TelephonyShellCommand(this, getDefaultPhone().getContext()).exec(
7766 this, in.getFileDescriptor(), out.getFileDescriptor(),
7767 err.getFileDescriptor(), args);
Brad Ebingerdac2f002018-04-03 15:17:52 -07007768 }
7769
Jack Yueb89b242016-06-22 13:27:47 -07007770 /**
Sooraj Sasindran2675c0b2020-04-19 18:14:03 -07007771 * Policy control of data connection with reason {@@TelephonyManager.DataEnabledReason}
Jack Yu75ab2952016-07-08 14:29:33 -07007772 * @param subId Subscription index
Sooraj Sasindran2675c0b2020-04-19 18:14:03 -07007773 * @param reason the reason the data enable change is taking place
7774 * @param enabled True if enabling the data, otherwise disabling.
7775 * @hide
Jack Yu75ab2952016-07-08 14:29:33 -07007776 */
7777 @Override
Sooraj Sasindranff75de62020-07-15 01:35:24 -07007778 public void setDataEnabledForReason(int subId, @TelephonyManager.DataEnabledReason int reason,
Sooraj Sasindran2675c0b2020-04-19 18:14:03 -07007779 boolean enabled) {
7780 if (reason == TelephonyManager.DATA_ENABLED_REASON_USER
7781 || reason == TelephonyManager.DATA_ENABLED_REASON_CARRIER) {
7782 try {
7783 TelephonyPermissions.enforceCallingOrSelfCarrierPrivilege(
Sooraj Sasindranff75de62020-07-15 01:35:24 -07007784 mApp, subId, "setDataEnabledForReason");
Sooraj Sasindran2675c0b2020-04-19 18:14:03 -07007785 } catch (SecurityException se) {
7786 enforceModifyPermission();
7787 }
7788 } else {
7789 enforceModifyPermission();
7790 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007791
7792 final long identity = Binder.clearCallingIdentity();
7793 try {
7794 Phone phone = getPhone(subId);
7795 if (phone != null) {
Sooraj Sasindran2675c0b2020-04-19 18:14:03 -07007796 if (reason == TelephonyManager.DATA_ENABLED_REASON_CARRIER) {
7797 phone.carrierActionSetMeteredApnsEnabled(enabled);
7798 } else {
7799 phone.getDataEnabledSettings().setDataEnabled(reason, enabled);
7800 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007801 }
7802 } finally {
7803 Binder.restoreCallingIdentity(identity);
Jack Yu75ab2952016-07-08 14:29:33 -07007804 }
7805 }
Sooraj Sasindran9a909312016-07-18 11:57:25 -07007806
7807 /**
7808 * Get Client request stats
7809 * @return List of Client Request Stats
7810 * @hide
7811 */
7812 @Override
Philip P. Moltmann9797cbb2020-01-07 13:45:00 -08007813 public List<ClientRequestStats> getClientRequestStats(String callingPackage,
7814 String callingFeatureId, int subId) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08007815 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
Philip P. Moltmann9797cbb2020-01-07 13:45:00 -08007816 mApp, subId, callingPackage, callingFeatureId, "getClientRequestStats")) {
Sooraj Sasindran9a909312016-07-18 11:57:25 -07007817 return null;
7818 }
Sooraj Sasindran9a909312016-07-18 11:57:25 -07007819 Phone phone = getPhone(subId);
Sooraj Sasindran9a909312016-07-18 11:57:25 -07007820
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007821 final long identity = Binder.clearCallingIdentity();
7822 try {
7823 if (phone != null) {
7824 return phone.getClientRequestStats();
7825 }
7826
7827 return null;
7828 } finally {
7829 Binder.restoreCallingIdentity(identity);
7830 }
Sooraj Sasindran9a909312016-07-18 11:57:25 -07007831 }
7832
Narayan Kamathf04b5a12018-01-09 11:47:15 +00007833 private WorkSource getWorkSource(int uid) {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08007834 String packageName = mApp.getPackageManager().getNameForUid(uid);
Narayan Kamathf04b5a12018-01-09 11:47:15 +00007835 return new WorkSource(uid, packageName);
Sooraj Sasindran9a909312016-07-18 11:57:25 -07007836 }
Jack Yueb4124c2017-02-16 15:32:43 -08007837
7838 /**
Grace Chen70990072017-03-24 17:21:30 -07007839 * Set SIM card power state.
Jack Yueb4124c2017-02-16 15:32:43 -08007840 *
Sanket Padawe13bac7b2017-03-20 15:04:47 -07007841 * @param slotIndex SIM slot id.
Grace Chen70990072017-03-24 17:21:30 -07007842 * @param state State of SIM (power down, power up, pass through)
7843 * - {@link android.telephony.TelephonyManager#CARD_POWER_DOWN}
7844 * - {@link android.telephony.TelephonyManager#CARD_POWER_UP}
7845 * - {@link android.telephony.TelephonyManager#CARD_POWER_UP_PASS_THROUGH}
Jack Yueb4124c2017-02-16 15:32:43 -08007846 *
7847 **/
7848 @Override
Grace Chen70990072017-03-24 17:21:30 -07007849 public void setSimPowerStateForSlot(int slotIndex, int state) {
Jack Yueb4124c2017-02-16 15:32:43 -08007850 enforceModifyPermission();
Sanket Padawe13bac7b2017-03-20 15:04:47 -07007851 Phone phone = PhoneFactory.getPhone(slotIndex);
7852
vagdeviaf9a5b92018-08-15 16:01:53 -07007853 WorkSource workSource = getWorkSource(Binder.getCallingUid());
7854
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007855 final long identity = Binder.clearCallingIdentity();
7856 try {
7857 if (phone != null) {
Jordan Liud5366d92020-11-24 14:50:34 -08007858 phone.setSimPowerState(state, null, workSource);
7859 }
7860 } finally {
7861 Binder.restoreCallingIdentity(identity);
7862 }
7863 }
7864
7865 /**
7866 * Set SIM card power state.
7867 *
7868 * @param slotIndex SIM slot id.
7869 * @param state State of SIM (power down, power up, pass through)
7870 * @param callback callback to trigger after success or failure
7871 * - {@link android.telephony.TelephonyManager#CARD_POWER_DOWN}
7872 * - {@link android.telephony.TelephonyManager#CARD_POWER_UP}
7873 * - {@link android.telephony.TelephonyManager#CARD_POWER_UP_PASS_THROUGH}
7874 *
7875 **/
7876 @Override
7877 public void setSimPowerStateForSlotWithCallback(int slotIndex, int state,
7878 IIntegerConsumer callback) {
7879 enforceModifyPermission();
7880 Phone phone = PhoneFactory.getPhone(slotIndex);
7881
7882 WorkSource workSource = getWorkSource(Binder.getCallingUid());
7883
7884 final long identity = Binder.clearCallingIdentity();
7885 try {
7886 if (phone != null) {
7887 Pair<Integer, IIntegerConsumer> arguments = Pair.create(state, callback);
7888 sendRequestAsync(CMD_SET_SIM_POWER, arguments, phone, workSource);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007889 }
7890 } finally {
7891 Binder.restoreCallingIdentity(identity);
Jack Yueb4124c2017-02-16 15:32:43 -08007892 }
7893 }
Shuo Qiandd210312017-04-12 22:11:33 +00007894
Tyler Gunn65d45c22017-06-05 11:22:26 -07007895 private boolean isUssdApiAllowed(int subId) {
7896 CarrierConfigManager configManager =
Nazanin Bakhshif782e562018-12-11 15:15:39 -08007897 (CarrierConfigManager) mApp.getSystemService(Context.CARRIER_CONFIG_SERVICE);
Tyler Gunn65d45c22017-06-05 11:22:26 -07007898 if (configManager == null) {
7899 return false;
7900 }
7901 PersistableBundle pb = configManager.getConfigForSubId(subId);
7902 if (pb == null) {
7903 return false;
7904 }
7905 return pb.getBoolean(
7906 CarrierConfigManager.KEY_ALLOW_USSD_REQUESTS_VIA_TELEPHONY_MANAGER_BOOL);
7907 }
7908
Shuo Qiandd210312017-04-12 22:11:33 +00007909 /**
7910 * Check if phone is in emergency callback mode
7911 * @return true if phone is in emergency callback mode
7912 * @param subId sub id
7913 */
goneil9c5f4872017-12-05 14:07:56 -08007914 @Override
Shuo Qiandd210312017-04-12 22:11:33 +00007915 public boolean getEmergencyCallbackMode(int subId) {
Brad Ebinger35c841c2018-10-01 10:40:55 -07007916 enforceReadPrivilegedPermission("getEmergencyCallbackMode");
Shuo Qiandd210312017-04-12 22:11:33 +00007917 final Phone phone = getPhone(subId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007918
7919 final long identity = Binder.clearCallingIdentity();
7920 try {
7921 if (phone != null) {
7922 return phone.isInEcm();
7923 } else {
7924 return false;
7925 }
7926 } finally {
7927 Binder.restoreCallingIdentity(identity);
Shuo Qiandd210312017-04-12 22:11:33 +00007928 }
7929 }
Nathan Harold46b42aa2017-03-10 19:38:22 -08007930
7931 /**
7932 * Get the current signal strength information for the given subscription.
7933 * Because this information is not updated when the device is in a low power state
7934 * it should not be relied-upon to be current.
7935 * @param subId Subscription index
7936 * @return the most recent cached signal strength info from the modem
7937 */
7938 @Override
7939 public SignalStrength getSignalStrength(int subId) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007940 final long identity = Binder.clearCallingIdentity();
7941 try {
7942 Phone p = getPhone(subId);
7943 if (p == null) {
7944 return null;
7945 }
Nathan Harold46b42aa2017-03-10 19:38:22 -08007946
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007947 return p.getSignalStrength();
7948 } finally {
7949 Binder.restoreCallingIdentity(identity);
7950 }
Nathan Harold46b42aa2017-03-10 19:38:22 -08007951 }
Holly Jiuyu Sun01c47ad2018-01-24 17:56:33 +00007952
Pengquan Meng77b7f132018-08-22 14:49:57 -07007953 /**
Chen Xuf792fd62018-10-17 17:54:36 +00007954 * Get the current modem radio state for the given slot.
7955 * @param slotIndex slot index.
7956 * @param callingPackage the name of the package making the call.
Philip P. Moltmann9797cbb2020-01-07 13:45:00 -08007957 * @param callingFeatureId The feature in the package.
Chen Xuf792fd62018-10-17 17:54:36 +00007958 * @return the current radio power state from the modem
7959 */
7960 @Override
Philip P. Moltmann9797cbb2020-01-07 13:45:00 -08007961 public int getRadioPowerState(int slotIndex, String callingPackage, String callingFeatureId) {
Chen Xuf792fd62018-10-17 17:54:36 +00007962 Phone phone = PhoneFactory.getPhone(slotIndex);
7963 if (phone != null) {
Philip P. Moltmann9797cbb2020-01-07 13:45:00 -08007964 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(mApp, phone.getSubId(),
7965 callingPackage, callingFeatureId, "getRadioPowerState")) {
Chen Xuf792fd62018-10-17 17:54:36 +00007966 return TelephonyManager.RADIO_POWER_UNAVAILABLE;
7967 }
7968
7969 final long identity = Binder.clearCallingIdentity();
7970 try {
7971 return phone.getRadioPowerState();
7972 } finally {
7973 Binder.restoreCallingIdentity(identity);
7974 }
7975 }
7976 return TelephonyManager.RADIO_POWER_UNAVAILABLE;
7977 }
7978
7979 /**
Pengquan Meng77b7f132018-08-22 14:49:57 -07007980 * Checks if data roaming is enabled on the subscription with id {@code subId}.
7981 *
7982 * <p>Requires one of the following permissions:
7983 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE},
7984 * {@link android.Manifest.permission#READ_PHONE_STATE} or that the calling app has carrier
7985 * privileges.
7986 *
7987 * @param subId subscription id
7988 * @return {@code true} if data roaming is enabled on this subscription, otherwise return
7989 * {@code false}.
7990 */
7991 @Override
7992 public boolean isDataRoamingEnabled(int subId) {
Shuo Qian11263f32020-08-13 15:42:55 -07007993 try {
7994 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.ACCESS_NETWORK_STATE,
7995 null);
7996 } catch (Exception e) {
7997 TelephonyPermissions.enforeceCallingOrSelfReadPhoneStatePermissionOrCarrierPrivilege(
7998 mApp, subId, "isDataRoamingEnabled");
7999 }
Pengquan Meng44e66f12019-04-01 10:48:20 -07008000
Pengquan Menga1bb6272018-09-06 09:59:22 -07008001 boolean isEnabled = false;
8002 final long identity = Binder.clearCallingIdentity();
Pengquan Meng77b7f132018-08-22 14:49:57 -07008003 try {
Pengquan Menga1bb6272018-09-06 09:59:22 -07008004 Phone phone = getPhone(subId);
8005 isEnabled = phone != null ? phone.getDataRoamingEnabled() : false;
Pengquan Menga1bb6272018-09-06 09:59:22 -07008006 } finally {
8007 Binder.restoreCallingIdentity(identity);
Pengquan Meng77b7f132018-08-22 14:49:57 -07008008 }
Pengquan Menga1bb6272018-09-06 09:59:22 -07008009 return isEnabled;
Pengquan Meng77b7f132018-08-22 14:49:57 -07008010 }
8011
8012
8013 /**
8014 * Enables/Disables the data roaming on the subscription with id {@code subId}.
8015 *
8016 * <p> Requires permission:
8017 * {@link android.Manifest.permission#MODIFY_PHONE_STATE} or that the calling app has carrier
8018 * privileges.
8019 *
8020 * @param subId subscription id
8021 * @param isEnabled {@code true} means enable, {@code false} means disable.
8022 */
8023 @Override
8024 public void setDataRoamingEnabled(int subId, boolean isEnabled) {
Pengquan Meng44e66f12019-04-01 10:48:20 -07008025 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
8026 mApp, subId, "setDataRoamingEnabled");
8027
Pengquan Menga1bb6272018-09-06 09:59:22 -07008028 final long identity = Binder.clearCallingIdentity();
8029 try {
Pengquan Menga1bb6272018-09-06 09:59:22 -07008030 Phone phone = getPhone(subId);
8031 if (phone != null) {
8032 phone.setDataRoamingEnabled(isEnabled);
8033 }
8034 } finally {
8035 Binder.restoreCallingIdentity(identity);
Pengquan Meng77b7f132018-08-22 14:49:57 -07008036 }
8037 }
8038
Holly Jiuyu Sun01c47ad2018-01-24 17:56:33 +00008039 @Override
Pengquan Meng6884a2c2018-10-03 12:19:13 -07008040 public boolean isManualNetworkSelectionAllowed(int subId) {
tom hsuc91afc72020-01-06 23:46:07 +08008041 TelephonyPermissions
8042 .enforeceCallingOrSelfReadPrivilegedPhoneStatePermissionOrCarrierPrivilege(
Pengquan Meng44e66f12019-04-01 10:48:20 -07008043 mApp, subId, "isManualNetworkSelectionAllowed");
8044
Pengquan Meng6884a2c2018-10-03 12:19:13 -07008045 boolean isAllowed = true;
8046 final long identity = Binder.clearCallingIdentity();
8047 try {
Pengquan Meng6884a2c2018-10-03 12:19:13 -07008048 Phone phone = getPhone(subId);
8049 if (phone != null) {
8050 isAllowed = phone.isCspPlmnEnabled();
8051 }
8052 } finally {
8053 Binder.restoreCallingIdentity(identity);
8054 }
8055 return isAllowed;
8056 }
8057
8058 @Override
Jordan Liu75f43ea2019-01-17 16:56:37 -08008059 public List<UiccCardInfo> getUiccCardsInfo(String callingPackage) {
Jordan Liucfdfe3a2020-03-23 11:55:07 -07008060 // Verify that tha callingPackage belongs to the calling UID
8061 mApp.getSystemService(AppOpsManager.class)
8062 .checkPackage(Binder.getCallingUid(), callingPackage);
8063
Jordan Liu1e142fc2019-04-22 15:10:43 -07008064 boolean hasReadPermission = false;
Jordan Liuc65bc952019-02-12 17:54:02 -08008065 try {
8066 enforceReadPrivilegedPermission("getUiccCardsInfo");
Jordan Liu1e142fc2019-04-22 15:10:43 -07008067 hasReadPermission = true;
Jordan Liuc65bc952019-02-12 17:54:02 -08008068 } catch (SecurityException e) {
8069 // even without READ_PRIVILEGED_PHONE_STATE, we allow the call to continue if the caller
8070 // has carrier privileges on an active UICC
8071 if (checkCarrierPrivilegesForPackageAnyPhone(callingPackage)
8072 != TelephonyManager.CARRIER_PRIVILEGE_STATUS_HAS_ACCESS) {
Jordan Liu1e142fc2019-04-22 15:10:43 -07008073 throw new SecurityException("Caller does not have permission.");
Jordan Liuc65bc952019-02-12 17:54:02 -08008074 }
Jordan Liu75f43ea2019-01-17 16:56:37 -08008075 }
Jordan Liu5aa07002018-12-18 15:44:48 -08008076
8077 final long identity = Binder.clearCallingIdentity();
8078 try {
Jordan Liu75f43ea2019-01-17 16:56:37 -08008079 UiccController uiccController = UiccController.getInstance();
8080 ArrayList<UiccCardInfo> cardInfos = uiccController.getAllUiccCardInfos();
Jordan Liu1e142fc2019-04-22 15:10:43 -07008081 if (hasReadPermission) {
8082 return cardInfos;
Jordan Liu75f43ea2019-01-17 16:56:37 -08008083 }
Jordan Liu1e142fc2019-04-22 15:10:43 -07008084
8085 // Remove private info if the caller doesn't have access
8086 ArrayList<UiccCardInfo> filteredInfos = new ArrayList<>();
8087 for (UiccCardInfo cardInfo : cardInfos) {
8088 // For an inactive eUICC, the UiccCard will be null even though the UiccCardInfo
8089 // is available
8090 UiccCard card = uiccController.getUiccCardForSlot(cardInfo.getSlotIndex());
8091 if (card == null || card.getUiccProfile() == null) {
8092 // assume no access if the card or profile is unavailable
8093 filteredInfos.add(cardInfo.getUnprivileged());
8094 continue;
8095 }
8096 UiccProfile profile = card.getUiccProfile();
8097 if (profile.getCarrierPrivilegeStatus(mApp.getPackageManager(), callingPackage)
8098 == TelephonyManager.CARRIER_PRIVILEGE_STATUS_HAS_ACCESS) {
8099 filteredInfos.add(cardInfo);
8100 } else {
8101 filteredInfos.add(cardInfo.getUnprivileged());
8102 }
8103 }
8104 return filteredInfos;
Jordan Liu5aa07002018-12-18 15:44:48 -08008105 } finally {
8106 Binder.restoreCallingIdentity(identity);
8107 }
8108 }
8109
8110 @Override
Holly Jiuyu Sun01c47ad2018-01-24 17:56:33 +00008111 public UiccSlotInfo[] getUiccSlotsInfo() {
Brad Ebinger35c841c2018-10-01 10:40:55 -07008112 enforceReadPrivilegedPermission("getUiccSlotsInfo");
Holly Jiuyu Sun01c47ad2018-01-24 17:56:33 +00008113
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008114 final long identity = Binder.clearCallingIdentity();
8115 try {
8116 UiccSlot[] slots = UiccController.getInstance().getUiccSlots();
8117 if (slots == null) {
8118 Rlog.i(LOG_TAG, "slots is null.");
8119 return null;
8120 }
8121
8122 UiccSlotInfo[] infos = new UiccSlotInfo[slots.length];
8123 for (int i = 0; i < slots.length; i++) {
8124 UiccSlot slot = slots[i];
8125 if (slot == null) {
8126 continue;
8127 }
8128
Jordan Liu7be7e652019-05-06 18:55:02 +00008129 String cardId;
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008130 UiccCard card = slot.getUiccCard();
8131 if (card != null) {
8132 cardId = card.getCardId();
Jordan Liu7be7e652019-05-06 18:55:02 +00008133 } else {
Jordan Liud96b5292019-09-12 16:19:43 -07008134 cardId = slot.getEid();
8135 if (TextUtils.isEmpty(cardId)) {
8136 cardId = slot.getIccId();
8137 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008138 }
8139
Jordan Liu857451f2019-05-09 16:35:35 -07008140 if (cardId != null) {
8141 // if cardId is an ICCID, strip off trailing Fs before exposing to user
8142 // if cardId is an EID, it's all digits so this is fine
8143 cardId = IccUtils.stripTrailingFs(cardId);
8144 }
8145
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008146 int cardState = 0;
8147 switch (slot.getCardState()) {
8148 case CARDSTATE_ABSENT:
8149 cardState = UiccSlotInfo.CARD_STATE_INFO_ABSENT;
8150 break;
8151 case CARDSTATE_PRESENT:
8152 cardState = UiccSlotInfo.CARD_STATE_INFO_PRESENT;
8153 break;
8154 case CARDSTATE_ERROR:
8155 cardState = UiccSlotInfo.CARD_STATE_INFO_ERROR;
8156 break;
8157 case CARDSTATE_RESTRICTED:
8158 cardState = UiccSlotInfo.CARD_STATE_INFO_RESTRICTED;
8159 break;
8160 default:
8161 break;
8162
8163 }
8164
8165 infos[i] = new UiccSlotInfo(
8166 slot.isActive(),
8167 slot.isEuicc(),
8168 cardId,
8169 cardState,
8170 slot.getPhoneId(),
Jordan Liua2619582019-02-14 12:56:40 -08008171 slot.isExtendedApduSupported(),
8172 slot.isRemovable());
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008173 }
8174 return infos;
8175 } finally {
8176 Binder.restoreCallingIdentity(identity);
Holly Jiuyu Sun1d957c52018-04-04 13:52:42 -07008177 }
Holly Jiuyu Sun01c47ad2018-01-24 17:56:33 +00008178 }
8179
8180 @Override
8181 public boolean switchSlots(int[] physicalSlots) {
8182 enforceModifyPermission();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008183
8184 final long identity = Binder.clearCallingIdentity();
8185 try {
8186 return (Boolean) sendRequest(CMD_SWITCH_SLOTS, physicalSlots);
8187 } finally {
8188 Binder.restoreCallingIdentity(identity);
8189 }
Holly Jiuyu Sun01c47ad2018-01-24 17:56:33 +00008190 }
Jack Yu4c988042018-02-27 15:30:01 -08008191
8192 @Override
Jordan Liu7de49fa2018-12-06 14:48:49 -08008193 public int getCardIdForDefaultEuicc(int subId, String callingPackage) {
Jordan Liu7de49fa2018-12-06 14:48:49 -08008194 final long identity = Binder.clearCallingIdentity();
8195 try {
8196 return UiccController.getInstance().getCardIdForDefaultEuicc();
8197 } finally {
8198 Binder.restoreCallingIdentity(identity);
8199 }
8200 }
8201
Pengquan Meng85728fb2018-03-12 16:31:21 -07008202 /**
goneil47ffb6e2018-04-06 15:40:58 -07008203 * A test API to reload the UICC profile.
8204 *
8205 * <p>Requires that the calling app has permission
8206 * {@link android.Manifest.permission#MODIFY_PHONE_STATE MODIFY_PHONE_STATE}.
8207 * @hide
8208 */
8209 @Override
8210 public void refreshUiccProfile(int subId) {
8211 enforceModifyPermission();
8212
8213 final long identity = Binder.clearCallingIdentity();
8214 try {
8215 Phone phone = getPhone(subId);
8216 if (phone == null) {
8217 return;
8218 }
8219 UiccCard uiccCard = phone.getUiccCard();
8220 if (uiccCard == null) {
8221 return;
8222 }
8223 UiccProfile uiccProfile = uiccCard.getUiccProfile();
8224 if (uiccProfile == null) {
8225 return;
8226 }
8227 uiccProfile.refresh();
8228 } finally {
8229 Binder.restoreCallingIdentity(identity);
8230 }
8231 }
8232
8233 /**
Pengquan Meng85728fb2018-03-12 16:31:21 -07008234 * Returns false if the mobile data is disabled by default, otherwise return true.
8235 */
8236 private boolean getDefaultDataEnabled() {
Inseob Kim8d298d42018-12-13 17:11:34 +09008237 return TelephonyProperties.mobile_data().orElse(true);
Pengquan Meng85728fb2018-03-12 16:31:21 -07008238 }
8239
8240 /**
8241 * Returns true if the data roaming is enabled by default, i.e the system property
8242 * of {@link #DEFAULT_DATA_ROAMING_PROPERTY_NAME} is true or the config of
8243 * {@link CarrierConfigManager#KEY_CARRIER_DEFAULT_DATA_ROAMING_ENABLED_BOOL} is true.
8244 */
8245 private boolean getDefaultDataRoamingEnabled(int subId) {
8246 final CarrierConfigManager configMgr = (CarrierConfigManager)
Nazanin Bakhshif782e562018-12-11 15:15:39 -08008247 mApp.getSystemService(Context.CARRIER_CONFIG_SERVICE);
Shuo Qianfaaa63d2020-07-15 12:36:44 -07008248 boolean isDataRoamingEnabled = TelephonyProperties.data_roaming().orElse(false);
Pengquan Meng85728fb2018-03-12 16:31:21 -07008249 isDataRoamingEnabled |= configMgr.getConfigForSubId(subId).getBoolean(
8250 CarrierConfigManager.KEY_CARRIER_DEFAULT_DATA_ROAMING_ENABLED_BOOL);
8251 return isDataRoamingEnabled;
8252 }
8253
8254 /**
8255 * Returns the default network type for the given {@code subId}, if the default network type is
8256 * not set, return {@link Phone#PREFERRED_NT_MODE}.
8257 */
8258 private int getDefaultNetworkType(int subId) {
Inseob Kim8d298d42018-12-13 17:11:34 +09008259 List<Integer> list = TelephonyProperties.default_network();
8260 int phoneId = mSubscriptionController.getPhoneId(subId);
8261 if (phoneId >= 0 && phoneId < list.size() && list.get(phoneId) != null) {
8262 return list.get(phoneId);
8263 }
8264 return Phone.PREFERRED_NT_MODE;
Pengquan Meng85728fb2018-03-12 16:31:21 -07008265 }
fionaxua13278b2018-03-21 00:08:13 -07008266
8267 @Override
8268 public void setCarrierTestOverride(int subId, String mccmnc, String imsi, String iccid, String
chen xueaba88a2019-03-15 13:15:10 -07008269 gid1, String gid2, String plmn, String spn, String carrierPrivilegeRules, String apn) {
fionaxua13278b2018-03-21 00:08:13 -07008270 enforceModifyPermission();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008271
8272 final long identity = Binder.clearCallingIdentity();
8273 try {
8274 final Phone phone = getPhone(subId);
8275 if (phone == null) {
8276 loge("setCarrierTestOverride fails with invalid subId: " + subId);
8277 return;
8278 }
chen xueaba88a2019-03-15 13:15:10 -07008279 phone.setCarrierTestOverride(mccmnc, imsi, iccid, gid1, gid2, plmn, spn,
8280 carrierPrivilegeRules, apn);
Jeff Davidson0103e8c2020-03-28 12:24:40 -07008281 if (carrierPrivilegeRules == null) {
8282 mCarrierPrivilegeTestOverrideSubIds.remove(subId);
8283 } else {
8284 mCarrierPrivilegeTestOverrideSubIds.add(subId);
8285 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008286 } finally {
8287 Binder.restoreCallingIdentity(identity);
fionaxua13278b2018-03-21 00:08:13 -07008288 }
fionaxua13278b2018-03-21 00:08:13 -07008289 }
8290
8291 @Override
8292 public int getCarrierIdListVersion(int subId) {
Brad Ebinger35c841c2018-10-01 10:40:55 -07008293 enforceReadPrivilegedPermission("getCarrierIdListVersion");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008294
8295 final long identity = Binder.clearCallingIdentity();
8296 try {
8297 final Phone phone = getPhone(subId);
8298 if (phone == null) {
8299 loge("getCarrierIdListVersion fails with invalid subId: " + subId);
8300 return TelephonyManager.UNKNOWN_CARRIER_ID_LIST_VERSION;
8301 }
8302 return phone.getCarrierIdListVersion();
8303 } finally {
8304 Binder.restoreCallingIdentity(identity);
fionaxua13278b2018-03-21 00:08:13 -07008305 }
fionaxua13278b2018-03-21 00:08:13 -07008306 }
Malcolm Chen2c63d402018-08-14 16:00:53 -07008307
8308 @Override
Philip P. Moltmann9797cbb2020-01-07 13:45:00 -08008309 public int getNumberOfModemsWithSimultaneousDataConnections(int subId, String callingPackage,
8310 String callingFeatureId) {
Malcolm Chen2c63d402018-08-14 16:00:53 -07008311 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
Philip P. Moltmann9797cbb2020-01-07 13:45:00 -08008312 mApp, subId, callingPackage, callingFeatureId,
8313 "getNumberOfModemsWithSimultaneousDataConnections")) {
Malcolm Chen2c63d402018-08-14 16:00:53 -07008314 return -1;
8315 }
8316
8317 final long identity = Binder.clearCallingIdentity();
8318 try {
8319 return mPhoneConfigurationManager.getNumberOfModemsWithSimultaneousDataConnections();
8320 } finally {
8321 Binder.restoreCallingIdentity(identity);
8322 }
8323 }
Pengquan Menga1bb6272018-09-06 09:59:22 -07008324
8325 @Override
8326 public int getCdmaRoamingMode(int subId) {
zoey chen07238702019-12-17 18:18:59 +08008327 TelephonyPermissions
8328 .enforeceCallingOrSelfReadPrivilegedPhoneStatePermissionOrCarrierPrivilege(
Pengquan Menga1bb6272018-09-06 09:59:22 -07008329 mApp, subId, "getCdmaRoamingMode");
8330
8331 final long identity = Binder.clearCallingIdentity();
8332 try {
8333 return (int) sendRequest(CMD_GET_CDMA_ROAMING_MODE, null /* argument */, subId);
8334 } finally {
8335 Binder.restoreCallingIdentity(identity);
8336 }
8337 }
8338
8339 @Override
8340 public boolean setCdmaRoamingMode(int subId, int mode) {
8341 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
8342 mApp, subId, "setCdmaRoamingMode");
8343
8344 final long identity = Binder.clearCallingIdentity();
8345 try {
8346 return (boolean) sendRequest(CMD_SET_CDMA_ROAMING_MODE, mode, subId);
8347 } finally {
8348 Binder.restoreCallingIdentity(identity);
8349 }
8350 }
8351
8352 @Override
Sarah Chin49f22af2020-10-28 13:46:24 -07008353 public int getCdmaSubscriptionMode(int subId) {
8354 TelephonyPermissions
8355 .enforeceCallingOrSelfReadPrivilegedPhoneStatePermissionOrCarrierPrivilege(
8356 mApp, subId, "getCdmaSubscriptionMode");
8357
8358 final long identity = Binder.clearCallingIdentity();
8359 try {
8360 return (int) sendRequest(CMD_GET_CDMA_SUBSCRIPTION_MODE, null /* argument */, subId);
8361 } finally {
8362 Binder.restoreCallingIdentity(identity);
8363 }
8364 }
8365
8366 @Override
Pengquan Menga1bb6272018-09-06 09:59:22 -07008367 public boolean setCdmaSubscriptionMode(int subId, int mode) {
8368 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
8369 mApp, subId, "setCdmaSubscriptionMode");
8370
8371 final long identity = Binder.clearCallingIdentity();
8372 try {
8373 return (boolean) sendRequest(CMD_SET_CDMA_SUBSCRIPTION_MODE, mode, subId);
8374 } finally {
8375 Binder.restoreCallingIdentity(identity);
8376 }
8377 }
Makoto Onukida3bf792018-09-18 16:06:29 -07008378
sqianc5eccab2018-10-19 18:46:41 -07008379 @Override
sqian8c685422019-02-22 15:55:18 -08008380 public Map<Integer, List<EmergencyNumber>> getEmergencyNumberList(
Philip P. Moltmann9797cbb2020-01-07 13:45:00 -08008381 String callingPackage, String callingFeatureId) {
sqian11b7a0e2018-12-05 18:48:28 -08008382 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
Philip P. Moltmann9797cbb2020-01-07 13:45:00 -08008383 mApp, getDefaultSubscription(), callingPackage, callingFeatureId,
8384 "getEmergencyNumberList")) {
sqian11b7a0e2018-12-05 18:48:28 -08008385 throw new SecurityException("Requires READ_PHONE_STATE permission.");
8386 }
8387 final long identity = Binder.clearCallingIdentity();
8388 try {
sqian854d44b2018-12-12 16:48:18 -08008389 Map<Integer, List<EmergencyNumber>> emergencyNumberListInternal = new HashMap<>();
8390 for (Phone phone: PhoneFactory.getPhones()) {
8391 if (phone.getEmergencyNumberTracker() != null
8392 && phone.getEmergencyNumberTracker().getEmergencyNumberList() != null) {
8393 emergencyNumberListInternal.put(
8394 phone.getSubId(),
8395 phone.getEmergencyNumberTracker().getEmergencyNumberList());
8396 }
sqian11b7a0e2018-12-05 18:48:28 -08008397 }
sqian854d44b2018-12-12 16:48:18 -08008398 return emergencyNumberListInternal;
sqian11b7a0e2018-12-05 18:48:28 -08008399 } finally {
8400 Binder.restoreCallingIdentity(identity);
8401 }
sqianc5eccab2018-10-19 18:46:41 -07008402 }
8403
8404 @Override
sqian8c685422019-02-22 15:55:18 -08008405 public boolean isEmergencyNumber(String number, boolean exactMatch) {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08008406 final Phone defaultPhone = getDefaultPhone();
sqian11b7a0e2018-12-05 18:48:28 -08008407 if (!exactMatch) {
8408 TelephonyPermissions
8409 .enforeceCallingOrSelfReadPrivilegedPhoneStatePermissionOrCarrierPrivilege(
sqian8c685422019-02-22 15:55:18 -08008410 mApp, defaultPhone.getSubId(), "isEmergencyNumber(Potential)");
sqian11b7a0e2018-12-05 18:48:28 -08008411 }
8412 final long identity = Binder.clearCallingIdentity();
8413 try {
sqian854d44b2018-12-12 16:48:18 -08008414 for (Phone phone: PhoneFactory.getPhones()) {
8415 if (phone.getEmergencyNumberTracker() != null
Taesu Leee050c002020-10-13 17:19:35 +09008416 && phone.getEmergencyNumberTracker()
8417 .isEmergencyNumber(number, exactMatch)) {
8418 return true;
sqian11b7a0e2018-12-05 18:48:28 -08008419 }
sqian11b7a0e2018-12-05 18:48:28 -08008420 }
8421 return false;
8422 } finally {
8423 Binder.restoreCallingIdentity(identity);
8424 }
8425 }
8426
sqianf4ca7ed2019-01-15 18:32:07 -08008427 /**
8428 * Update emergency number list for test mode.
8429 */
8430 @Override
8431 public void updateEmergencyNumberListTestMode(int action, EmergencyNumber num) {
8432 TelephonyPermissions.enforceShellOnly(Binder.getCallingUid(),
8433 "updateEmergencyNumberListTestMode");
8434
8435 final long identity = Binder.clearCallingIdentity();
8436 try {
8437 for (Phone phone: PhoneFactory.getPhones()) {
8438 EmergencyNumberTracker tracker = phone.getEmergencyNumberTracker();
8439 if (tracker != null) {
8440 tracker.executeEmergencyNumberTestModeCommand(action, num);
8441 }
8442 }
8443 } finally {
8444 Binder.restoreCallingIdentity(identity);
8445 }
8446 }
8447
8448 /**
8449 * Get the full emergency number list for test mode.
8450 */
8451 @Override
8452 public List<String> getEmergencyNumberListTestMode() {
8453 TelephonyPermissions.enforceShellOnly(Binder.getCallingUid(),
8454 "getEmergencyNumberListTestMode");
8455
8456 final long identity = Binder.clearCallingIdentity();
8457 try {
8458 Set<String> emergencyNumbers = new HashSet<>();
8459 for (Phone phone: PhoneFactory.getPhones()) {
8460 EmergencyNumberTracker tracker = phone.getEmergencyNumberTracker();
8461 if (tracker != null) {
8462 for (EmergencyNumber num : tracker.getEmergencyNumberList()) {
8463 emergencyNumbers.add(num.getNumber());
8464 }
8465 }
8466 }
8467 return new ArrayList<>(emergencyNumbers);
8468 } finally {
8469 Binder.restoreCallingIdentity(identity);
8470 }
8471 }
8472
chen xud6b45bd2018-10-30 22:27:10 -07008473 @Override
Shuo Qianf2b2df42019-11-13 17:43:31 -08008474 public int getEmergencyNumberDbVersion(int subId) {
8475 enforceReadPrivilegedPermission("getEmergencyNumberDbVersion");
8476
8477 final long identity = Binder.clearCallingIdentity();
8478 try {
8479 final Phone phone = getPhone(subId);
8480 if (phone == null) {
8481 loge("getEmergencyNumberDbVersion fails with invalid subId: " + subId);
8482 return TelephonyManager.INVALID_EMERGENCY_NUMBER_DB_VERSION;
8483 }
8484 return phone.getEmergencyNumberDbVersion();
8485 } finally {
8486 Binder.restoreCallingIdentity(identity);
8487 }
8488 }
8489
8490 @Override
8491 public void notifyOtaEmergencyNumberDbInstalled() {
8492 enforceModifyPermission();
8493
8494 final long identity = Binder.clearCallingIdentity();
8495 try {
8496 for (Phone phone: PhoneFactory.getPhones()) {
8497 EmergencyNumberTracker tracker = phone.getEmergencyNumberTracker();
8498 if (tracker != null) {
8499 tracker.updateOtaEmergencyNumberDatabase();
8500 }
8501 }
8502 } finally {
8503 Binder.restoreCallingIdentity(identity);
8504 }
8505 }
8506
8507 @Override
Shuo Qianb15c6be2020-03-05 17:55:34 -08008508 public void updateOtaEmergencyNumberDbFilePath(ParcelFileDescriptor otaParcelFileDescriptor) {
Shuo Qianf2b2df42019-11-13 17:43:31 -08008509 enforceActiveEmergencySessionPermission();
8510
8511 final long identity = Binder.clearCallingIdentity();
8512 try {
8513 for (Phone phone: PhoneFactory.getPhones()) {
8514 EmergencyNumberTracker tracker = phone.getEmergencyNumberTracker();
8515 if (tracker != null) {
Shuo Qianb15c6be2020-03-05 17:55:34 -08008516 tracker.updateOtaEmergencyNumberDbFilePath(otaParcelFileDescriptor);
8517 }
8518 }
8519 } finally {
8520 Binder.restoreCallingIdentity(identity);
8521 }
8522 }
8523
8524 @Override
8525 public void resetOtaEmergencyNumberDbFilePath() {
8526 enforceActiveEmergencySessionPermission();
8527
8528 final long identity = Binder.clearCallingIdentity();
8529 try {
8530 for (Phone phone: PhoneFactory.getPhones()) {
8531 EmergencyNumberTracker tracker = phone.getEmergencyNumberTracker();
8532 if (tracker != null) {
8533 tracker.resetOtaEmergencyNumberDbFilePath();
Shuo Qianf2b2df42019-11-13 17:43:31 -08008534 }
8535 }
8536 } finally {
8537 Binder.restoreCallingIdentity(identity);
8538 }
8539 }
8540
8541 @Override
chen xud6b45bd2018-10-30 22:27:10 -07008542 public List<String> getCertsFromCarrierPrivilegeAccessRules(int subId) {
8543 enforceReadPrivilegedPermission("getCertsFromCarrierPrivilegeAccessRules");
8544 Phone phone = getPhone(subId);
8545 if (phone == null) {
8546 return null;
8547 }
8548 final long identity = Binder.clearCallingIdentity();
8549 try {
8550 UiccProfile profile = UiccController.getInstance()
8551 .getUiccProfileForPhone(phone.getPhoneId());
8552 if (profile != null) {
8553 return profile.getCertsFromCarrierPrivilegeAccessRules();
8554 }
8555 } finally {
8556 Binder.restoreCallingIdentity(identity);
8557 }
8558 return null;
8559 }
Malcolm Chen8e4ed912019-01-15 20:22:16 -08008560
8561 /**
8562 * Enable or disable a modem stack.
8563 */
8564 @Override
8565 public boolean enableModemForSlot(int slotIndex, boolean enable) {
8566 enforceModifyPermission();
8567
8568 final long identity = Binder.clearCallingIdentity();
8569 try {
8570 Phone phone = PhoneFactory.getPhone(slotIndex);
8571 if (phone == null) {
8572 return false;
8573 } else {
8574 return (Boolean) sendRequest(CMD_REQUEST_ENABLE_MODEM, enable, phone, null);
8575 }
8576 } finally {
8577 Binder.restoreCallingIdentity(identity);
8578 }
8579 }
Michelecea4cf22018-12-21 15:00:11 -08008580
Malcolm Chen4bcd9822019-03-27 18:34:05 -07008581 /**
8582 * Whether a modem stack is enabled or not.
8583 */
8584 @Override
Philip P. Moltmann9797cbb2020-01-07 13:45:00 -08008585 public boolean isModemEnabledForSlot(int slotIndex, String callingPackage,
8586 String callingFeatureId) {
Malcolm Chen4bcd9822019-03-27 18:34:05 -07008587 Phone phone = PhoneFactory.getPhone(slotIndex);
8588 if (phone == null) return false;
8589
8590 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
Philip P. Moltmann9797cbb2020-01-07 13:45:00 -08008591 mApp, phone.getSubId(), callingPackage, callingFeatureId,
8592 "isModemEnabledForSlot")) {
Malcolm Chen4bcd9822019-03-27 18:34:05 -07008593 throw new SecurityException("Requires READ_PHONE_STATE permission.");
8594 }
8595
8596 final long identity = Binder.clearCallingIdentity();
8597 try {
Nazanin Bakhshif71371d2019-04-29 17:29:44 -07008598 try {
8599 return mPhoneConfigurationManager.getPhoneStatusFromCache(phone.getPhoneId());
8600 } catch (NoSuchElementException ex) {
8601 return (Boolean) sendRequest(CMD_GET_MODEM_STATUS, null, phone, null);
8602 }
Malcolm Chen4bcd9822019-03-27 18:34:05 -07008603 } finally {
8604 Binder.restoreCallingIdentity(identity);
8605 }
8606 }
8607
Michelecea4cf22018-12-21 15:00:11 -08008608 @Override
Michele0ea7d782019-03-19 14:58:42 -07008609 public void setMultiSimCarrierRestriction(boolean isMultiSimCarrierRestricted) {
Michelecea4cf22018-12-21 15:00:11 -08008610 enforceModifyPermission();
8611
8612 final long identity = Binder.clearCallingIdentity();
8613 try {
8614 mTelephonySharedPreferences.edit()
Michele0ea7d782019-03-19 14:58:42 -07008615 .putBoolean(PREF_MULTI_SIM_RESTRICTED, isMultiSimCarrierRestricted)
Michelecea4cf22018-12-21 15:00:11 -08008616 .commit();
8617 } finally {
8618 Binder.restoreCallingIdentity(identity);
8619 }
8620 }
8621
8622 @Override
Michele0ea7d782019-03-19 14:58:42 -07008623 @TelephonyManager.IsMultiSimSupportedResult
Philip P. Moltmann9797cbb2020-01-07 13:45:00 -08008624 public int isMultiSimSupported(String callingPackage, String callingFeatureId) {
Michele4245e952019-02-04 11:36:23 -08008625 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(mApp,
Philip P. Moltmann9797cbb2020-01-07 13:45:00 -08008626 getDefaultPhone().getSubId(), callingPackage, callingFeatureId,
8627 "isMultiSimSupported")) {
Michele0ea7d782019-03-19 14:58:42 -07008628 return TelephonyManager.MULTISIM_NOT_SUPPORTED_BY_HARDWARE;
Michele4245e952019-02-04 11:36:23 -08008629 }
Michelecea4cf22018-12-21 15:00:11 -08008630
8631 final long identity = Binder.clearCallingIdentity();
8632 try {
Michele0ea7d782019-03-19 14:58:42 -07008633 return isMultiSimSupportedInternal();
Michelecea4cf22018-12-21 15:00:11 -08008634 } finally {
8635 Binder.restoreCallingIdentity(identity);
8636 }
8637 }
Nazanin Bakhshi628473f2019-01-29 17:37:52 -08008638
Michele0ea7d782019-03-19 14:58:42 -07008639 @TelephonyManager.IsMultiSimSupportedResult
8640 private int isMultiSimSupportedInternal() {
Michele30b57b22019-03-01 12:01:14 -08008641 // If the device has less than 2 SIM cards, indicate that multisim is restricted.
8642 int numPhysicalSlots = UiccController.getInstance().getUiccSlots().length;
8643 if (numPhysicalSlots < 2) {
Michele0ea7d782019-03-19 14:58:42 -07008644 loge("isMultiSimSupportedInternal: requires at least 2 cards");
8645 return TelephonyManager.MULTISIM_NOT_SUPPORTED_BY_HARDWARE;
Michele30b57b22019-03-01 12:01:14 -08008646 }
8647 // Check if the hardware supports multisim functionality. If usage of multisim is not
8648 // supported by the modem, indicate that it is restricted.
8649 PhoneCapability staticCapability =
8650 mPhoneConfigurationManager.getStaticPhoneCapability();
8651 if (staticCapability == null) {
Michele0ea7d782019-03-19 14:58:42 -07008652 loge("isMultiSimSupportedInternal: no static configuration available");
8653 return TelephonyManager.MULTISIM_NOT_SUPPORTED_BY_HARDWARE;
Michele30b57b22019-03-01 12:01:14 -08008654 }
Sarah Chin09f38ee2020-02-25 00:13:10 +00008655 if (staticCapability.logicalModemList.size() < 2) {
Michele0ea7d782019-03-19 14:58:42 -07008656 loge("isMultiSimSupportedInternal: maximum number of modem is < 2");
8657 return TelephonyManager.MULTISIM_NOT_SUPPORTED_BY_HARDWARE;
Michele30b57b22019-03-01 12:01:14 -08008658 }
8659 // Check if support of multiple SIMs is restricted by carrier
8660 if (mTelephonySharedPreferences.getBoolean(PREF_MULTI_SIM_RESTRICTED, false)) {
Michele0ea7d782019-03-19 14:58:42 -07008661 return TelephonyManager.MULTISIM_NOT_SUPPORTED_BY_CARRIER;
Michele30b57b22019-03-01 12:01:14 -08008662 }
8663
Michele0ea7d782019-03-19 14:58:42 -07008664 return TelephonyManager.MULTISIM_ALLOWED;
Michele30b57b22019-03-01 12:01:14 -08008665 }
8666
Nazanin Bakhshi628473f2019-01-29 17:37:52 -08008667 /**
8668 * Switch configs to enable multi-sim or switch back to single-sim
Nazanin Bakhshi17318782019-03-01 11:56:08 -08008669 * Note: Switch from multi-sim to single-sim is only possible with MODIFY_PHONE_STATE
8670 * permission, but the other way around is possible with either MODIFY_PHONE_STATE
8671 * or carrier privileges
Nazanin Bakhshi628473f2019-01-29 17:37:52 -08008672 * @param numOfSims number of active sims we want to switch to
8673 */
8674 @Override
8675 public void switchMultiSimConfig(int numOfSims) {
Nazanin Bakhshi17318782019-03-01 11:56:08 -08008676 if (numOfSims == 1) {
8677 enforceModifyPermission();
8678 } else {
8679 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
8680 mApp, SubscriptionManager.DEFAULT_SUBSCRIPTION_ID, "switchMultiSimConfig");
8681 }
Nazanin Bakhshi628473f2019-01-29 17:37:52 -08008682 final long identity = Binder.clearCallingIdentity();
Michele30b57b22019-03-01 12:01:14 -08008683
Nazanin Bakhshi628473f2019-01-29 17:37:52 -08008684 try {
Michele30b57b22019-03-01 12:01:14 -08008685 //only proceed if multi-sim is not restricted
Michele0ea7d782019-03-19 14:58:42 -07008686 if (isMultiSimSupportedInternal() != TelephonyManager.MULTISIM_ALLOWED) {
Michele30b57b22019-03-01 12:01:14 -08008687 loge("switchMultiSimConfig not possible. It is restricted or not supported.");
8688 return;
8689 }
Nazanin Bakhshi628473f2019-01-29 17:37:52 -08008690 mPhoneConfigurationManager.switchMultiSimConfig(numOfSims);
8691 } finally {
8692 Binder.restoreCallingIdentity(identity);
8693 }
8694 }
8695
Hyungjun Parkbb07fde2019-01-10 15:28:51 +09008696 @Override
8697 public boolean isApplicationOnUicc(int subId, int appType) {
8698 enforceReadPrivilegedPermission("isApplicationOnUicc");
8699 Phone phone = getPhone(subId);
8700 if (phone == null) {
8701 return false;
8702 }
8703 final long identity = Binder.clearCallingIdentity();
8704 try {
8705 UiccCard uiccCard = phone.getUiccCard();
8706 if (uiccCard == null) {
8707 return false;
8708 }
8709 UiccProfile uiccProfile = uiccCard.getUiccProfile();
8710 if (uiccProfile == null) {
8711 return false;
8712 }
8713 if (TelephonyManager.APPTYPE_SIM <= appType
8714 && appType <= TelephonyManager.APPTYPE_ISIM) {
8715 return uiccProfile.isApplicationOnIcc(AppType.values()[appType]);
8716 }
8717 return false;
8718 } finally {
8719 Binder.restoreCallingIdentity(identity);
8720 }
8721 }
8722
Nazanin Bakhshi628473f2019-01-29 17:37:52 -08008723 /**
chen xub4baa772019-04-03 10:23:41 -07008724 * Get whether making changes to modem configurations will trigger reboot.
8725 * Return value defaults to true.
Nazanin Bakhshi5fe5ef22019-01-30 10:52:09 -08008726 */
8727 @Override
Philip P. Moltmann9797cbb2020-01-07 13:45:00 -08008728 public boolean doesSwitchMultiSimConfigTriggerReboot(int subId, String callingPackage,
8729 String callingFeatureId) {
chen xub4baa772019-04-03 10:23:41 -07008730 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
Philip P. Moltmann9797cbb2020-01-07 13:45:00 -08008731 mApp, subId, callingPackage, callingFeatureId,
8732 "doesSwitchMultiSimConfigTriggerReboot")) {
chen xub4baa772019-04-03 10:23:41 -07008733 return false;
8734 }
Nazanin Bakhshi5fe5ef22019-01-30 10:52:09 -08008735 final long identity = Binder.clearCallingIdentity();
8736 try {
8737 return mPhoneConfigurationManager.isRebootRequiredForModemConfigChange();
8738 } finally {
8739 Binder.restoreCallingIdentity(identity);
8740 }
8741 }
8742
Nathan Harold29f5f052019-02-15 13:41:57 -08008743 private void updateModemStateMetrics() {
8744 TelephonyMetrics metrics = TelephonyMetrics.getInstance();
8745 // TODO: check the state for each modem if the api is ready.
8746 metrics.updateEnabledModemBitmap((1 << TelephonyManager.from(mApp).getPhoneCount()) - 1);
8747 }
8748
Pengquan Meng3889a572019-01-23 11:16:29 -08008749 @Override
8750 public int[] getSlotsMapping() {
8751 enforceReadPrivilegedPermission("getSlotsMapping");
8752
8753 final long identity = Binder.clearCallingIdentity();
8754 try {
8755 int phoneCount = TelephonyManager.getDefault().getPhoneCount();
8756 // All logical slots should have a mapping to a physical slot.
8757 int[] logicalSlotsMapping = new int[phoneCount];
8758 UiccSlotInfo[] slotInfos = getUiccSlotsInfo();
8759 for (int i = 0; i < slotInfos.length; i++) {
8760 if (SubscriptionManager.isValidPhoneId(slotInfos[i].getLogicalSlotIdx())) {
8761 logicalSlotsMapping[slotInfos[i].getLogicalSlotIdx()] = i;
8762 }
8763 }
8764 return logicalSlotsMapping;
8765 } finally {
8766 Binder.restoreCallingIdentity(identity);
8767 }
8768 }
Nathan Harold48d6fd52019-02-06 19:01:40 -08008769
8770 /**
8771 * Get the IRadio HAL Version
8772 */
8773 @Override
8774 public int getRadioHalVersion() {
8775 Phone phone = getDefaultPhone();
8776 if (phone == null) return -1;
8777 HalVersion hv = phone.getHalVersion();
8778 if (hv.equals(HalVersion.UNKNOWN)) return -1;
8779 return hv.major * 100 + hv.minor;
8780 }
Malcolm Chendc8c10e2019-04-10 18:25:07 -07008781
8782 /**
Shuo Qianaf42a312020-01-14 15:18:28 -08008783 * Get the current calling package name.
8784 * @return the current calling package name
8785 */
8786 @Override
8787 public String getCurrentPackageName() {
8788 return mApp.getPackageManager().getPackagesForUid(Binder.getCallingUid())[0];
8789 }
8790
8791 /**
Malcolm Chene5ad5792019-04-18 13:51:02 -07008792 * Return whether data is enabled for certain APN type. This will tell if framework will accept
8793 * corresponding network requests on a subId.
8794 *
8795 * Data is enabled if:
Malcolm Chendc8c10e2019-04-10 18:25:07 -07008796 * 1) user data is turned on, or
Malcolm Chene5ad5792019-04-18 13:51:02 -07008797 * 2) APN is un-metered for this subscription, or
8798 * 3) APN type is whitelisted. E.g. MMS is whitelisted if
Hall Liub48cf452020-09-25 11:13:49 -07008799 * {@link TelephonyManager#MOBILE_DATA_POLICY_MMS_ALWAYS_ALLOWED} is enabled.
Malcolm Chene5ad5792019-04-18 13:51:02 -07008800 *
8801 * @return whether data is allowed for a apn type.
8802 *
8803 * @hide
Malcolm Chendc8c10e2019-04-10 18:25:07 -07008804 */
8805 @Override
Malcolm Chene5ad5792019-04-18 13:51:02 -07008806 public boolean isDataEnabledForApn(int apnType, int subId, String callingPackage) {
Amit Mahajanf2509742019-10-07 16:20:43 -07008807 enforceReadPrivilegedPermission("Needs READ_PRIVILEGED_PHONE_STATE for "
8808 + "isDataEnabledForApn");
Malcolm Chendc8c10e2019-04-10 18:25:07 -07008809
8810 // Now that all security checks passes, perform the operation as ourselves.
8811 final long identity = Binder.clearCallingIdentity();
8812 try {
8813 Phone phone = getPhone(subId);
8814 if (phone == null) return false;
8815
Jack Yu41407ee2019-05-13 16:54:09 -07008816 boolean isMetered = ApnSettingUtils.isMeteredApnType(apnType, phone);
Malcolm Chene5ad5792019-04-18 13:51:02 -07008817 return !isMetered || phone.getDataEnabledSettings().isDataEnabled(apnType);
8818 } finally {
8819 Binder.restoreCallingIdentity(identity);
8820 }
8821 }
8822
8823 @Override
Jack Yu41407ee2019-05-13 16:54:09 -07008824 public boolean isApnMetered(@ApnType int apnType, int subId) {
Malcolm Chene5ad5792019-04-18 13:51:02 -07008825 enforceReadPrivilegedPermission("isApnMetered");
8826
8827 // Now that all security checks passes, perform the operation as ourselves.
8828 final long identity = Binder.clearCallingIdentity();
8829 try {
8830 Phone phone = getPhone(subId);
8831 if (phone == null) return true; // By default return true.
8832
Jack Yu41407ee2019-05-13 16:54:09 -07008833 return ApnSettingUtils.isMeteredApnType(apnType, phone);
Malcolm Chendc8c10e2019-04-10 18:25:07 -07008834 } finally {
8835 Binder.restoreCallingIdentity(identity);
8836 }
8837 }
Brad Ebingera63db5f2019-04-23 16:31:13 -07008838
8839 @Override
Hall Liud0d1dc92020-01-20 13:42:00 -08008840 public void setSystemSelectionChannels(List<RadioAccessSpecifier> specifiers,
8841 int subscriptionId, IBooleanConsumer resultCallback) {
8842 enforceModifyPermission();
8843 long token = Binder.clearCallingIdentity();
8844 try {
8845 Phone phone = getPhone(subscriptionId);
8846 if (phone == null) {
8847 try {
8848 if (resultCallback != null) {
8849 resultCallback.accept(false);
8850 }
8851 } catch (RemoteException e) {
8852 // ignore
8853 }
8854 return;
8855 }
8856 Pair<List<RadioAccessSpecifier>, Consumer<Boolean>> argument =
8857 Pair.create(specifiers, (x) -> {
8858 try {
8859 if (resultCallback != null) {
8860 resultCallback.accept(x);
8861 }
8862 } catch (RemoteException e) {
8863 // ignore
8864 }
8865 });
8866 sendRequestAsync(CMD_SET_SYSTEM_SELECTION_CHANNELS, argument, phone, null);
8867 } finally {
8868 Binder.restoreCallingIdentity(token);
8869 }
8870 }
8871
8872 @Override
Sarah Chincc055732020-11-18 13:39:35 -08008873 public List<RadioAccessSpecifier> getSystemSelectionChannels(int subId) {
8874 TelephonyPermissions
8875 .enforeceCallingOrSelfReadPrivilegedPhoneStatePermissionOrCarrierPrivilege(
8876 mApp, subId, "getSystemSelectionChannels");
8877 WorkSource workSource = getWorkSource(Binder.getCallingUid());
8878 final long identity = Binder.clearCallingIdentity();
8879 try {
8880 List<RadioAccessSpecifier> specifiers =
8881 (List<RadioAccessSpecifier>) sendRequest(CMD_GET_SYSTEM_SELECTION_CHANNELS,
8882 null, subId, workSource);
8883 if (DBG) log("getSystemSelectionChannels: " + specifiers);
8884 return specifiers;
8885 } finally {
8886 Binder.restoreCallingIdentity(identity);
8887 }
8888 }
8889
8890 @Override
changbetty363e8ac2019-12-06 18:16:37 +08008891 public boolean isMvnoMatched(int subId, int mvnoType, @NonNull String mvnoMatchData) {
changbettyca3d40d2020-03-03 16:27:31 +08008892 enforceReadPrivilegedPermission("isMvnoMatched");
changbetty363e8ac2019-12-06 18:16:37 +08008893 IccRecords iccRecords = UiccController.getInstance().getIccRecords(
8894 SubscriptionManager.getPhoneId(subId), UiccController.APP_FAM_3GPP);
8895 if (iccRecords == null) {
8896 Log.d(LOG_TAG, "isMvnoMatched# IccRecords is null");
8897 return false;
8898 }
8899 return ApnSettingUtils.mvnoMatches(iccRecords, mvnoType, mvnoMatchData);
8900 }
8901
8902 @Override
Philip P. Moltmann295beed2020-03-18 17:06:12 -07008903 public void enqueueSmsPickResult(String callingPackage, String callingAttributionTag,
8904 IIntegerConsumer pendingSubIdResult) {
Shuo Qianaf42a312020-01-14 15:18:28 -08008905 if (callingPackage == null) {
8906 callingPackage = getCurrentPackageName();
8907 }
Brad Ebingera63db5f2019-04-23 16:31:13 -07008908 SmsPermissions permissions = new SmsPermissions(getDefaultPhone(), mApp,
8909 (AppOpsManager) mApp.getSystemService(Context.APP_OPS_SERVICE));
Philip P. Moltmann295beed2020-03-18 17:06:12 -07008910 if (!permissions.checkCallingCanSendSms(callingPackage, callingAttributionTag,
8911 "Sending message")) {
Brad Ebingera63db5f2019-04-23 16:31:13 -07008912 throw new SecurityException("Requires SEND_SMS permission to perform this operation");
8913 }
8914 PickSmsSubscriptionActivity.addPendingResult(pendingSubIdResult);
8915 Intent intent = new Intent();
8916 intent.setClass(mApp, PickSmsSubscriptionActivity.class);
8917 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
8918 // Bring up choose default SMS subscription dialog right now
8919 intent.putExtra(PickSmsSubscriptionActivity.DIALOG_TYPE_KEY,
8920 PickSmsSubscriptionActivity.SMS_PICK_FOR_MESSAGE);
8921 mApp.startActivity(intent);
8922 }
chen xud5ca2d52019-05-28 15:20:57 -07008923
8924 @Override
8925 public String getMmsUAProfUrl(int subId) {
8926 //TODO investigate if this API should require proper permission check in R b/133791609
8927 final long identity = Binder.clearCallingIdentity();
8928 try {
Guoqiang.Qiu171383d2020-07-13 09:38:32 +08008929 String carrierUAProfUrl = mApp.getCarrierConfigForSubId(subId).getString(
8930 CarrierConfigManager.KEY_MMS_UA_PROF_URL_STRING);
8931 if (!TextUtils.isEmpty(carrierUAProfUrl)) {
8932 return carrierUAProfUrl;
8933 }
chen xud5ca2d52019-05-28 15:20:57 -07008934 return SubscriptionManager.getResourcesForSubId(getDefaultPhone().getContext(), subId)
8935 .getString(com.android.internal.R.string.config_mms_user_agent_profile_url);
8936 } finally {
8937 Binder.restoreCallingIdentity(identity);
8938 }
8939 }
8940
8941 @Override
8942 public String getMmsUserAgent(int subId) {
8943 //TODO investigate if this API should require proper permission check in R b/133791609
8944 final long identity = Binder.clearCallingIdentity();
8945 try {
Guoqiang.Qiu171383d2020-07-13 09:38:32 +08008946 String carrierUserAgent = mApp.getCarrierConfigForSubId(subId).getString(
8947 CarrierConfigManager.KEY_MMS_USER_AGENT_STRING);
8948 if (!TextUtils.isEmpty(carrierUserAgent)) {
8949 return carrierUserAgent;
8950 }
chen xud5ca2d52019-05-28 15:20:57 -07008951 return SubscriptionManager.getResourcesForSubId(getDefaultPhone().getContext(), subId)
8952 .getString(com.android.internal.R.string.config_mms_user_agent);
8953 } finally {
8954 Binder.restoreCallingIdentity(identity);
8955 }
8956 }
Jack Yub07d4972019-05-28 16:12:25 -07008957
8958 @Override
Hall Liuc041a552020-09-25 10:42:19 -07008959 public boolean isMobileDataPolicyEnabled(int subscriptionId, int policy) {
8960 enforceReadPrivilegedPermission("isMobileDataPolicyEnabled");
8961
8962 final long identity = Binder.clearCallingIdentity();
8963 try {
8964 Phone phone = getPhone(subscriptionId);
8965 if (phone == null) return false;
8966
8967 switch (policy) {
8968 case TelephonyManager.MOBILE_DATA_POLICY_DATA_ON_NON_DEFAULT_DURING_VOICE_CALL:
8969 return phone.getDataEnabledSettings().isDataAllowedInVoiceCall();
8970 case TelephonyManager.MOBILE_DATA_POLICY_MMS_ALWAYS_ALLOWED:
8971 return phone.getDataEnabledSettings().isMmsAlwaysAllowed();
8972 default:
8973 throw new IllegalArgumentException(policy + " is not a valid policy");
8974 }
8975 } finally {
8976 Binder.restoreCallingIdentity(identity);
8977 }
8978 }
8979
8980 @Override
8981 public void setMobileDataPolicyEnabledStatus(int subscriptionId, int policy,
8982 boolean enabled) {
8983 enforceModifyPermission();
8984
8985 final long identity = Binder.clearCallingIdentity();
8986 try {
8987 Phone phone = getPhone(subscriptionId);
8988 if (phone == null) return;
8989
8990 switch (policy) {
8991 case TelephonyManager.MOBILE_DATA_POLICY_DATA_ON_NON_DEFAULT_DURING_VOICE_CALL:
8992 phone.getDataEnabledSettings().setAllowDataDuringVoiceCall(enabled);
8993 break;
8994 case TelephonyManager.MOBILE_DATA_POLICY_MMS_ALWAYS_ALLOWED:
8995 phone.getDataEnabledSettings().setAlwaysAllowMmsData(enabled);
8996 break;
8997 default:
8998 throw new IllegalArgumentException(policy + " is not a valid policy");
8999 }
9000 } finally {
9001 Binder.restoreCallingIdentity(identity);
9002 }
9003 }
9004
Tyler Gunn7bcdc742019-10-04 15:56:59 -07009005 /**
Hall Liub48cf452020-09-25 11:13:49 -07009006 * Updates whether conference event package handling is enabled.
Tyler Gunn7bcdc742019-10-04 15:56:59 -07009007 * @param isCepEnabled {@code true} if CEP handling is enabled (default), or {@code false}
9008 * otherwise.
9009 */
9010 @Override
9011 public void setCepEnabled(boolean isCepEnabled) {
9012 TelephonyPermissions.enforceShellOnly(Binder.getCallingUid(), "setCepEnabled");
9013
9014 final long identity = Binder.clearCallingIdentity();
9015 try {
9016 Rlog.i(LOG_TAG, "setCepEnabled isCepEnabled=" + isCepEnabled);
9017 for (Phone phone : PhoneFactory.getPhones()) {
9018 Phone defaultPhone = phone.getImsPhone();
9019 if (defaultPhone != null && defaultPhone.getPhoneType() == PHONE_TYPE_IMS) {
9020 ImsPhone imsPhone = (ImsPhone) defaultPhone;
9021 ImsPhoneCallTracker imsPhoneCallTracker =
9022 (ImsPhoneCallTracker) imsPhone.getCallTracker();
9023 imsPhoneCallTracker.setConferenceEventPackageEnabled(isCepEnabled);
9024 Rlog.i(LOG_TAG, "setCepEnabled isCepEnabled=" + isCepEnabled + ", for imsPhone "
9025 + imsPhone.getMsisdn());
9026 }
9027 }
9028 } finally {
9029 Binder.restoreCallingIdentity(identity);
9030 }
9031 }
allenwtsu46dcc572020-01-08 18:24:03 +08009032
9033 /**
9034 * Notify that an RCS autoconfiguration XML file has been received for provisioning.
9035 *
9036 * @param config The XML file to be read. ASCII/UTF8 encoded text if not compressed.
9037 * @param isCompressed The XML file is compressed in gzip format and must be decompressed
9038 * before being read.
9039 */
9040 @Override
9041 public void notifyRcsAutoConfigurationReceived(int subId, @NonNull byte[] config, boolean
9042 isCompressed) {
9043 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
9044 mApp, subId, "notifyRcsAutoConfigurationReceived");
9045 try {
9046 IImsConfig configBinder = getImsConfig(getSlotIndex(subId), ImsFeature.FEATURE_RCS);
9047 if (configBinder == null) {
9048 Rlog.e(LOG_TAG, "null result for getImsConfig");
9049 } else {
9050 configBinder.notifyRcsAutoConfigurationReceived(config, isCompressed);
9051 }
9052 } catch (RemoteException e) {
9053 Rlog.e(LOG_TAG, "fail to getImsConfig " + e.getMessage());
9054 }
9055 }
zoey chenf95ca592019-12-30 16:11:23 +08009056
9057 @Override
9058 public boolean isIccLockEnabled(int subId) {
9059 enforceReadPrivilegedPermission("isIccLockEnabled");
9060
9061 // Now that all security checks passes, perform the operation as ourselves.
9062 final long identity = Binder.clearCallingIdentity();
9063 try {
9064 Phone phone = getPhone(subId);
9065 if (phone != null && phone.getIccCard() != null) {
9066 return phone.getIccCard().getIccLockEnabled();
9067 } else {
9068 return false;
9069 }
9070 } finally {
9071 Binder.restoreCallingIdentity(identity);
9072 }
9073 }
9074
9075 /**
zoey chene02881a2019-12-30 16:11:23 +08009076 * Set the ICC pin lock enabled or disabled.
zoey chenf95ca592019-12-30 16:11:23 +08009077 *
zoey chene02881a2019-12-30 16:11:23 +08009078 * @return an integer representing the status of IccLock enabled or disabled in the following
9079 * three cases:
9080 * - {@link TelephonyManager#CHANGE_ICC_LOCK_SUCCESS} if enabled or disabled IccLock
9081 * successfully.
9082 * - Positive number and zero for remaining password attempts.
9083 * - Negative number for other failure cases (such like enabling/disabling PIN failed).
zoey chenf95ca592019-12-30 16:11:23 +08009084 *
9085 */
9086 @Override
9087 public int setIccLockEnabled(int subId, boolean enabled, String password) {
9088 enforceModifyPermission();
9089
9090 Phone phone = getPhone(subId);
9091 if (phone == null) {
9092 return 0;
9093 }
9094 // Now that all security checks passes, perform the operation as ourselves.
9095 final long identity = Binder.clearCallingIdentity();
9096 try {
9097 int attemptsRemaining = (int) sendRequest(CMD_SET_ICC_LOCK_ENABLED,
9098 new Pair<Boolean, String>(enabled, password), phone, null);
9099 return attemptsRemaining;
9100
9101 } catch (Exception e) {
9102 Log.e(LOG_TAG, "setIccLockEnabled. Exception e =" + e);
9103 } finally {
9104 Binder.restoreCallingIdentity(identity);
9105 }
9106 return 0;
9107 }
9108
9109 /**
9110 * Change the ICC password used in ICC pin lock.
9111 *
zoey chene02881a2019-12-30 16:11:23 +08009112 * @return an integer representing the status of IccLock changed in the following three cases:
9113 * - {@link TelephonyManager#CHANGE_ICC_LOCK_SUCCESS} if changed IccLock successfully.
9114 * - Positive number and zero for remaining password attempts.
9115 * - Negative number for other failure cases (such like enabling/disabling PIN failed).
zoey chenf95ca592019-12-30 16:11:23 +08009116 *
9117 */
9118 @Override
9119 public int changeIccLockPassword(int subId, String oldPassword, String newPassword) {
9120 enforceModifyPermission();
9121
9122 Phone phone = getPhone(subId);
9123 if (phone == null) {
9124 return 0;
9125 }
9126 // Now that all security checks passes, perform the operation as ourselves.
9127 final long identity = Binder.clearCallingIdentity();
9128 try {
9129 int attemptsRemaining = (int) sendRequest(CMD_CHANGE_ICC_LOCK_PASSWORD,
9130 new Pair<String, String>(oldPassword, newPassword), phone, null);
9131 return attemptsRemaining;
9132
9133 } catch (Exception e) {
9134 Log.e(LOG_TAG, "changeIccLockPassword. Exception e =" + e);
9135 } finally {
9136 Binder.restoreCallingIdentity(identity);
9137 }
9138 return 0;
9139 }
Malcolm Chen884180b2020-04-13 11:59:40 -07009140
Peter Wangdafb9ac2020-01-15 14:13:38 -08009141 /**
9142 * Request for receiving user activity notification
9143 */
9144 @Override
9145 public void requestUserActivityNotification() {
9146 if (!mNotifyUserActivity.get()
9147 && !mMainThreadHandler.hasMessages(MSG_NOTIFY_USER_ACTIVITY)) {
9148 mNotifyUserActivity.set(true);
9149 }
9150 }
9151
9152 /**
9153 * Called when userActivity is signalled in the power manager.
9154 * This is safe to call from any thread, with any window manager locks held or not.
9155 */
9156 @Override
9157 public void userActivity() {
9158 // ***************************************
9159 // * Inherited from PhoneWindowManager *
9160 // ***************************************
9161 // THIS IS CALLED FROM DEEP IN THE POWER MANAGER
9162 // WITH ITS LOCKS HELD.
9163 //
9164 // This code must be VERY careful about the locks
9165 // it acquires.
9166 // In fact, the current code acquires way too many,
9167 // and probably has lurking deadlocks.
9168
9169 if (Binder.getCallingUid() != Process.SYSTEM_UID) {
9170 throw new SecurityException("Only the OS may call notifyUserActivity()");
9171 }
9172
9173 if (mNotifyUserActivity.getAndSet(false)) {
9174 mMainThreadHandler.sendEmptyMessageDelayed(MSG_NOTIFY_USER_ACTIVITY,
9175 USER_ACTIVITY_NOTIFICATION_DELAY);
9176 }
9177 }
Malcolm Chen4639c562020-04-13 11:59:40 -07009178
Malcolm Chen884180b2020-04-13 11:59:40 -07009179 @Override
9180 public boolean canConnectTo5GInDsdsMode() {
9181 return mApp.getResources().getBoolean(R.bool.config_5g_connection_in_dsds_mode);
9182 }
Jack Yud10cdd42020-09-28 20:28:01 -07009183
9184 @Override
9185 public @NonNull List<String> getEquivalentHomePlmns(int subId, String callingPackage,
9186 String callingFeatureId) {
9187 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
9188 mApp, subId, callingPackage, callingFeatureId, "getEquivalentHomePlmns")) {
9189 throw new SecurityException("Requires READ_PHONE_STATE permission.");
9190 }
9191
9192 Phone phone = getPhone(subId);
9193 if (phone == null) {
9194 throw new RuntimeException("phone is not available");
9195 }
9196 // Now that all security checks passes, perform the operation as ourselves.
9197 final long identity = Binder.clearCallingIdentity();
9198 try {
9199 return phone.getEquivalentHomePlmns();
9200 } finally {
9201 Binder.restoreCallingIdentity(identity);
9202 }
9203 }
Jack Nudelman24d51a52020-11-24 12:08:04 -08009204
9205 /**
9206 * Attempts to set the radio power state for thermal reason. This does not guarantee that the
9207 * requested radio power state will actually be set. See {@link
9208 * PhoneInternalInterface#setRadioPowerForReason} for more details.
9209 *
9210 * @param subId the subscription ID of the phone requesting to set the radio power state.
9211 * @param enable {@code true} if trying to turn radio on.
9212 * @return {@code true} if phone setRadioPowerForReason was called. Otherwise, returns {@code
9213 * false}.
9214 */
9215 private boolean setRadioPowerForThermal(int subId, boolean enable) {
9216 Phone phone = getPhone(subId);
9217 if (phone != null) {
9218 phone.setRadioPowerForReason(enable, Phone.RADIO_POWER_REASON_THERMAL);
9219 return true;
9220 }
9221 return false;
9222 }
9223
9224 private int handleDataThrottlingRequest(int subId,
9225 DataThrottlingRequest dataThrottlingRequest) {
9226 // Ensure that radio is on. If not able to power on due to phone being unavailable, return
9227 // THERMAL_MITIGATION_RESULT_MODEM_NOT_AVAILABLE.
9228 if (!setRadioPowerForThermal(subId, true)) {
9229 return TelephonyManager.THERMAL_MITIGATION_RESULT_MODEM_NOT_AVAILABLE;
9230 }
9231
9232 setDataEnabledForReason(subId, TelephonyManager.DATA_ENABLED_REASON_THERMAL, true);
9233
9234 int thermalMitigationResult =
9235 (int) sendRequest(CMD_SET_DATA_THROTTLING, dataThrottlingRequest, subId);
9236 if (thermalMitigationResult == SET_DATA_THROTTLING_MODEM_THREW_INVALID_PARAMS) {
9237 throw new IllegalArgumentException("modem returned INVALID_ARGUMENTS");
9238 }
9239 return thermalMitigationResult;
9240 }
9241
9242 /**
9243 * Thermal mitigation request to control functionalities at modem.
9244 *
9245 * @param subId the id of the subscription.
9246 * @param thermalMitigationRequest holds all necessary information to be passed down to modem.
9247 *
9248 * @return thermalMitigationResult enum as defined in android.telephony.Annotation.
9249 */
9250 @Override
9251 @ThermalMitigationResult
9252 public int sendThermalMitigationRequest(
9253 int subId,
9254 ThermalMitigationRequest thermalMitigationRequest) throws IllegalArgumentException {
9255 enforceModifyPermission();
9256
9257 WorkSource workSource = getWorkSource(Binder.getCallingUid());
9258 final long identity = Binder.clearCallingIdentity();
9259
9260 int thermalMitigationResult = TelephonyManager.THERMAL_MITIGATION_RESULT_UNKNOWN_ERROR;
9261 try {
9262 int thermalMitigationAction = thermalMitigationRequest.getThermalMitigationAction();
9263 switch (thermalMitigationAction) {
9264 case ThermalMitigationRequest.THERMAL_MITIGATION_ACTION_DATA_THROTTLING:
9265 thermalMitigationResult =
9266 handleDataThrottlingRequest(subId,
9267 thermalMitigationRequest.getDataThrottlingRequest());
9268 break;
9269 case ThermalMitigationRequest.THERMAL_MITIGATION_ACTION_VOICE_ONLY:
9270 if (thermalMitigationRequest.getDataThrottlingRequest() != null) {
9271 throw new IllegalArgumentException("dataThrottlingRequest must be null for "
9272 + "ThermalMitigationRequest.THERMAL_MITIGATION_ACTION_VOICE_ONLY");
9273 }
9274
9275 // Ensure that radio is on. If not able to power on due to phone being
9276 // unavailable, return THERMAL_MITIGATION_RESULT_MODEM_NOT_AVAILABLE.
9277 if (!setRadioPowerForThermal(subId, true)) {
9278 thermalMitigationResult =
9279 TelephonyManager.THERMAL_MITIGATION_RESULT_MODEM_NOT_AVAILABLE;
9280 break;
9281 }
9282
9283 setDataEnabledForReason(subId, TelephonyManager.DATA_ENABLED_REASON_THERMAL,
9284 false);
9285 thermalMitigationResult = TelephonyManager.THERMAL_MITIGATION_RESULT_SUCCESS;
9286 break;
9287 case ThermalMitigationRequest.THERMAL_MITIGATION_ACTION_RADIO_OFF:
9288 if (thermalMitigationRequest.getDataThrottlingRequest() != null) {
9289 throw new IllegalArgumentException("dataThrottlingRequest must be null for"
9290 + " ThermalMitigationRequest.THERMAL_MITIGATION_ACTION_RADIO_OFF");
9291 }
9292
9293 TelecomAccountRegistry registry = TelecomAccountRegistry.getInstance(null);
9294 if (registry != null) {
9295 TelephonyConnectionService service =
9296 registry.getTelephonyConnectionService();
9297 Phone phone = getPhone(subId);
9298 if (phone == null) {
9299 thermalMitigationResult =
9300 TelephonyManager.THERMAL_MITIGATION_RESULT_MODEM_NOT_AVAILABLE;
9301 break;
9302 }
9303
9304 if (PhoneConstantConversions.convertCallState(phone.getState())
9305 != TelephonyManager.CALL_STATE_IDLE
9306 || phone.isInEmergencySmsMode() || phone.isInEcm()
9307 || (service != null && service.isEmergencyCallPending())) {
9308 String errorMessage = "Phone state is not valid. call state = "
9309 + PhoneConstantConversions.convertCallState(phone.getState())
9310 + " isInEmergencySmsMode = " + phone.isInEmergencySmsMode()
9311 + " isInEmergencyCallbackMode = " + phone.isInEcm();
9312 errorMessage += service == null
9313 ? " TelephonyConnectionService is null"
9314 : " isEmergencyCallPending = "
9315 + service.isEmergencyCallPending();
9316 Log.e(LOG_TAG, errorMessage);
9317 thermalMitigationResult =
9318 TelephonyManager.THERMAL_MITIGATION_RESULT_INVALID_STATE;
9319 break;
9320 }
9321 } else {
9322 thermalMitigationResult =
9323 TelephonyManager.THERMAL_MITIGATION_RESULT_MODEM_NOT_AVAILABLE;
9324 break;
9325 }
9326
9327 // Turn radio off. If not able to power off due to phone being unavailable,
9328 // return THERMAL_MITIGATION_RESULT_MODEM_NOT_AVAILABLE.
9329 if (!setRadioPowerForThermal(subId, false)) {
9330 thermalMitigationResult =
9331 TelephonyManager.THERMAL_MITIGATION_RESULT_MODEM_NOT_AVAILABLE;
9332 break;
9333 }
9334 thermalMitigationResult =
9335 TelephonyManager.THERMAL_MITIGATION_RESULT_SUCCESS;
9336 break;
9337 default:
9338 throw new IllegalArgumentException("the requested thermalMitigationAction does "
9339 + "not exist. Requested action: " + thermalMitigationAction);
9340 }
9341 } catch (IllegalArgumentException e) {
9342 throw e;
9343 } catch (Exception e) {
9344 Log.e(LOG_TAG, "thermalMitigationRequest. Exception e =" + e);
9345 thermalMitigationResult = TelephonyManager.THERMAL_MITIGATION_RESULT_MODEM_ERROR;
9346 } finally {
9347 Binder.restoreCallingIdentity(identity);
9348 }
9349
9350 if (DBG) {
9351 log("thermalMitigationRequest returning with thermalMitigationResult: "
9352 + thermalMitigationResult);
9353 }
9354
9355 return thermalMitigationResult;
9356 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07009357}