blob: 67c8d926239016495d465d201681b46b668d31cb [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;
Hall Liua1548bd2019-12-24 14:14:12 -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;
Hall Liua1548bd2019-12-24 14:14:12 -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 Qiancd19c462020-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 Nallurid63128d2019-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 Kim14bb3d02018-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 Xu227e06f2019-09-26 22:48:11 -070066import android.telephony.Annotation.ApnType;
Shuo Qian4a594052020-01-23 11:59:30 -080067import android.telephony.CallForwardingInfo;
Junda Liu12f7d802015-05-01 12:06:44 -070068import android.telephony.CarrierConfigManager;
Michele Berionne482f8202018-11-27 18:57:59 -080069import android.telephony.CarrierRestrictionRules;
yincheng zhao2737e882019-09-06 17:06:54 -070070import android.telephony.CellIdentity;
Meng Wanga10e89e2019-12-09 13:13:01 -080071import android.telephony.CellIdentityCdma;
72import android.telephony.CellIdentityGsm;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070073import android.telephony.CellInfo;
Nathan Haroldf180aac2018-06-01 18:43:55 -070074import android.telephony.CellInfoGsm;
75import android.telephony.CellInfoWcdma;
Sooraj Sasindran9a909312016-07-18 11:57:25 -070076import android.telephony.ClientRequestStats;
Nathan Haroldfa8da0f2018-09-27 18:51:29 -070077import android.telephony.ICellInfoCallback;
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -070078import android.telephony.IccOpenLogicalChannelResponse;
Hall Liu1aa510f2017-11-22 17:40:08 -080079import android.telephony.LocationAccessPolicy;
Ta-wei Yen87c49842016-05-13 21:19:52 -070080import android.telephony.ModemActivityInfo;
Jake Hambye994d462014-02-03 13:10:13 -080081import android.telephony.NeighboringCellInfo;
yinxu504e1392017-04-12 16:03:22 -070082import android.telephony.NetworkScanRequest;
Michele4245e952019-02-04 11:36:23 -080083import android.telephony.PhoneCapability;
Hall Liud892bec2018-11-30 14:51:45 -080084import android.telephony.PhoneNumberRange;
Wink Saville5d475dd2014-10-17 15:00:58 -070085import android.telephony.RadioAccessFamily;
Hall Liub2ac8ef2019-02-28 15:56:23 -080086import android.telephony.RadioAccessSpecifier;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070087import android.telephony.ServiceState;
Nathan Harold46b42aa2017-03-10 19:38:22 -080088import android.telephony.SignalStrength;
Wink Saville0f3b5fc2014-11-11 08:40:49 -080089import android.telephony.SubscriptionInfo;
Jeff Sharkey85190e62014-12-05 09:40:12 -080090import android.telephony.SubscriptionManager;
Peter Wangc035ce42020-01-08 21:00:22 -080091import android.telephony.TelephonyFrameworkInitializer;
Sanket Padawe99ef1e32016-05-18 16:12:33 -070092import android.telephony.TelephonyHistogram;
Ta-wei Yenb6929602016-05-24 15:48:27 -070093import android.telephony.TelephonyManager;
Hall Liub2ac8ef2019-02-28 15:56:23 -080094import android.telephony.TelephonyScanManager;
Jordan Liu5aa07002018-12-18 15:44:48 -080095import android.telephony.UiccCardInfo;
Holly Jiuyu Sun01c47ad2018-01-24 17:56:33 +000096import android.telephony.UiccSlotInfo;
Tyler Gunn65d45c22017-06-05 11:22:26 -070097import android.telephony.UssdResponse;
Ta-wei Yenb6929602016-05-24 15:48:27 -070098import android.telephony.VisualVoicemailSmsFilterSettings;
Jack Yub5d8f642018-11-26 11:20:48 -080099import android.telephony.data.ApnSetting;
100import android.telephony.emergency.EmergencyNumber;
Brad Ebinger1ce9c432019-07-16 13:19:44 -0700101import android.telephony.ims.ImsException;
Brad Ebinger1c8542e2019-01-14 13:43:14 -0800102import android.telephony.ims.ProvisioningManager;
Brad Ebingera34a6c22019-10-22 17:36:18 -0700103import android.telephony.ims.RegistrationManager;
Brad Ebinger35c841c2018-10-01 10:40:55 -0700104import android.telephony.ims.aidl.IImsCapabilityCallback;
Brad Ebinger22bc3e42018-01-16 09:39:35 -0800105import android.telephony.ims.aidl.IImsConfig;
Brad Ebingerdf5b4f02018-10-31 11:24:17 -0700106import android.telephony.ims.aidl.IImsConfigCallback;
Brad Ebinger22bc3e42018-01-16 09:39:35 -0800107import android.telephony.ims.aidl.IImsMmTelFeature;
108import android.telephony.ims.aidl.IImsRcsFeature;
109import android.telephony.ims.aidl.IImsRegistration;
Brad Ebinger35c841c2018-10-01 10:40:55 -0700110import android.telephony.ims.aidl.IImsRegistrationCallback;
Brad Ebingerbc7dd582019-10-17 17:03:22 -0700111import android.telephony.ims.feature.ImsFeature;
Brad Ebinger1c8542e2019-01-14 13:43:14 -0800112import android.telephony.ims.feature.MmTelFeature;
allenwtsu99c623b2020-01-03 18:24:23 +0800113import android.telephony.ims.feature.RcsFeature;
Brad Ebinger1c8542e2019-01-14 13:43:14 -0800114import android.telephony.ims.stub.ImsConfigImplBase;
Brad Ebinger1f2b5082018-02-08 16:11:32 -0800115import android.telephony.ims.stub.ImsRegistrationImplBase;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700116import android.text.TextUtils;
Jeff Sharkey85190e62014-12-05 09:40:12 -0800117import android.util.ArraySet;
Hall Liud60acc92020-05-21 17:09:35 -0700118import android.util.EventLog;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700119import android.util.Log;
Jake Hambye994d462014-02-03 13:10:13 -0800120import android.util.Pair;
Ta-wei Yen30a69c82016-12-27 14:52:32 -0800121
Andrew Lee312e8172014-10-23 17:01:36 -0700122import com.android.ims.ImsManager;
Brad Ebinger34bef922017-11-09 10:27:08 -0800123import com.android.ims.internal.IImsServiceFeatureCallback;
Shuo Qian4a594052020-01-23 11:59:30 -0800124import com.android.internal.telephony.CallForwardInfo;
Shishir Agrawal566b7612013-10-28 14:41:00 -0700125import com.android.internal.telephony.CallManager;
Tyler Gunn52dcf772017-04-26 11:30:31 -0700126import com.android.internal.telephony.CallStateException;
pkanwar79ec0542017-07-31 14:10:01 -0700127import com.android.internal.telephony.CarrierInfoManager;
chen xu651eec72018-11-11 19:03:44 -0800128import com.android.internal.telephony.CarrierResolver;
Shishir Agrawal302c8692015-06-19 13:49:39 -0700129import com.android.internal.telephony.CellNetworkScanResult;
Shishir Agrawal566b7612013-10-28 14:41:00 -0700130import com.android.internal.telephony.CommandException;
Shuo Qian4a594052020-01-23 11:59:30 -0800131import com.android.internal.telephony.CommandsInterface;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700132import com.android.internal.telephony.DefaultPhoneNotifier;
Nathan Harold48d6fd52019-02-06 19:01:40 -0800133import com.android.internal.telephony.HalVersion;
Hall Liu73f5d362020-01-20 13:42:00 -0800134import com.android.internal.telephony.IBooleanConsumer;
Hall Liu27d24262020-09-18 19:04:59 -0700135import com.android.internal.telephony.ICallForwardingInfoCallback;
Brad Ebingera63db5f2019-04-23 16:31:13 -0700136import com.android.internal.telephony.IIntegerConsumer;
Hall Liud892bec2018-11-30 14:51:45 -0800137import com.android.internal.telephony.INumberVerificationCallback;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700138import com.android.internal.telephony.ITelephony;
Jake Hambye994d462014-02-03 13:10:13 -0800139import com.android.internal.telephony.IccCard;
Jack Yu5f7092c2018-04-13 14:05:37 -0700140import com.android.internal.telephony.LocaleTracker;
yinxub1bed742017-04-17 11:45:04 -0700141import com.android.internal.telephony.NetworkScanRequestTracker;
Shishir Agrawal302c8692015-06-19 13:49:39 -0700142import com.android.internal.telephony.OperatorInfo;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700143import com.android.internal.telephony.Phone;
Malcolm Chen2c63d402018-08-14 16:00:53 -0700144import com.android.internal.telephony.PhoneConfigurationManager;
Nathan Harolda667c152016-12-14 11:27:20 -0800145import com.android.internal.telephony.PhoneConstantConversions;
Ta-wei Yen87c49842016-05-13 21:19:52 -0700146import com.android.internal.telephony.PhoneConstants;
Wink Saville36469e72014-06-11 15:17:00 -0700147import com.android.internal.telephony.PhoneFactory;
Wink Saville5d475dd2014-10-17 15:00:58 -0700148import com.android.internal.telephony.ProxyController;
Sanket Padawe99ef1e32016-05-18 16:12:33 -0700149import com.android.internal.telephony.RIL;
Svet Ganovb320e182015-04-16 12:30:10 -0700150import com.android.internal.telephony.RILConstants;
Jack Yu5f7092c2018-04-13 14:05:37 -0700151import com.android.internal.telephony.ServiceStateTracker;
Amit Mahajandccb3f12019-05-13 13:48:32 -0700152import com.android.internal.telephony.SmsController;
Brad Ebingera63db5f2019-04-23 16:31:13 -0700153import com.android.internal.telephony.SmsPermissions;
Wink Savilleac1bdfd2014-11-20 23:04:44 -0800154import com.android.internal.telephony.SubscriptionController;
Peter Wang59571be2020-01-27 12:35:15 +0800155import com.android.internal.telephony.TelephonyIntents;
Jeff Davidson7e17e312018-02-13 18:17:36 -0800156import com.android.internal.telephony.TelephonyPermissions;
Malcolm Chendc8c10e2019-04-10 18:25:07 -0700157import com.android.internal.telephony.dataconnection.ApnSettingUtils;
sqianf4ca7ed2019-01-15 18:32:07 -0800158import com.android.internal.telephony.emergency.EmergencyNumberTracker;
Derek Tan740e1672017-06-27 14:56:27 -0700159import com.android.internal.telephony.euicc.EuiccConnector;
Brad Ebinger9c0eb502019-01-23 15:06:19 -0800160import com.android.internal.telephony.ims.ImsResolver;
Tyler Gunn7bcdc742019-10-04 15:56:59 -0700161import com.android.internal.telephony.imsphone.ImsPhone;
162import com.android.internal.telephony.imsphone.ImsPhoneCallTracker;
Pengquan Meng6c2dc9f2019-02-06 11:12:53 -0800163import com.android.internal.telephony.metrics.TelephonyMetrics;
Meng Wangafbc5852019-09-19 17:37:13 -0700164import com.android.internal.telephony.uicc.IccCardApplicationStatus.AppType;
Shishir Agrawal566b7612013-10-28 14:41:00 -0700165import com.android.internal.telephony.uicc.IccIoResult;
changbetty7157e9e2019-12-06 18:16:37 +0800166import com.android.internal.telephony.uicc.IccRecords;
Shishir Agrawal566b7612013-10-28 14:41:00 -0700167import com.android.internal.telephony.uicc.IccUtils;
Nathan Haroldb3014052017-01-25 15:57:32 -0800168import com.android.internal.telephony.uicc.SIMRecords;
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700169import com.android.internal.telephony.uicc.UiccCard;
Nathan Haroldb3014052017-01-25 15:57:32 -0800170import com.android.internal.telephony.uicc.UiccCardApplication;
Shishir Agrawal566b7612013-10-28 14:41:00 -0700171import com.android.internal.telephony.uicc.UiccController;
Jeff Davidson7e17e312018-02-13 18:17:36 -0800172import com.android.internal.telephony.uicc.UiccProfile;
Holly Jiuyu Sun01c47ad2018-01-24 17:56:33 +0000173import com.android.internal.telephony.uicc.UiccSlot;
zoey chenc730df82019-12-18 17:07:20 +0800174import com.android.internal.telephony.util.LocaleUtils;
fionaxu7ed723d2017-05-30 18:58:54 -0700175import com.android.internal.telephony.util.VoicemailNotificationSettingsUtil;
Hall Liu27d24262020-09-18 19:04:59 -0700176import com.android.internal.util.FunctionalUtils;
Jake Hambye994d462014-02-03 13:10:13 -0800177import com.android.internal.util.HexDump;
Brad Ebingera63db5f2019-04-23 16:31:13 -0700178import com.android.phone.settings.PickSmsSubscriptionActivity;
Ta-wei Yenc9df0432017-04-17 17:09:07 -0700179import com.android.phone.vvm.PhoneAccountHandleConverter;
Ta-wei Yen527a9c02017-01-06 15:29:25 -0800180import com.android.phone.vvm.RemoteVvmTaskManager;
Ta-wei Yenc9df0432017-04-17 17:09:07 -0700181import com.android.phone.vvm.VisualVoicemailSettingsUtil;
Ta-wei Yenc8905312017-03-28 11:14:45 -0700182import com.android.phone.vvm.VisualVoicemailSmsFilterConfig;
Peter Wang44b186e2020-01-13 23:33:09 -0800183import com.android.telephony.Rlog;
Ta-wei Yen30a69c82016-12-27 14:52:32 -0800184
Ta-wei Yenc236d6b2016-06-21 13:33:12 -0700185import java.io.FileDescriptor;
186import java.io.PrintWriter;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700187import java.util.ArrayList;
Jeff Sharkey85190e62014-12-05 09:40:12 -0800188import java.util.Arrays;
sqian11b7a0e2018-12-05 18:48:28 -0800189import java.util.HashMap;
sqianf4ca7ed2019-01-15 18:32:07 -0800190import java.util.HashSet;
Jake Hambye994d462014-02-03 13:10:13 -0800191import java.util.List;
Narayan Kamath1c496c22015-04-16 14:40:19 +0100192import java.util.Locale;
Jeff Sharkey85190e62014-12-05 09:40:12 -0800193import java.util.Map;
Nazanin Bakhshif71371d2019-04-29 17:29:44 -0700194import java.util.NoSuchElementException;
sqianf4ca7ed2019-01-15 18:32:07 -0800195import java.util.Set;
Peter Wangdafb9ac2020-01-15 14:13:38 -0800196import java.util.concurrent.atomic.AtomicBoolean;
Hall Liu73f5d362020-01-20 13:42:00 -0800197import java.util.function.Consumer;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700198
199/**
200 * Implementation of the ITelephony interface.
201 */
Santos Cordon117fee72014-05-16 17:56:12 -0700202public class PhoneInterfaceManager extends ITelephony.Stub {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700203 private static final String LOG_TAG = "PhoneInterfaceManager";
204 private static final boolean DBG = (PhoneGlobals.DBG_LEVEL >= 2);
205 private static final boolean DBG_LOC = false;
Jeff Sharkey85190e62014-12-05 09:40:12 -0800206 private static final boolean DBG_MERGE = false;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700207
208 // Message codes used with mMainThreadHandler
209 private static final int CMD_HANDLE_PIN_MMI = 1;
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700210 private static final int CMD_TRANSMIT_APDU_LOGICAL_CHANNEL = 7;
211 private static final int EVENT_TRANSMIT_APDU_LOGICAL_CHANNEL_DONE = 8;
Shishir Agrawal566b7612013-10-28 14:41:00 -0700212 private static final int CMD_OPEN_CHANNEL = 9;
213 private static final int EVENT_OPEN_CHANNEL_DONE = 10;
214 private static final int CMD_CLOSE_CHANNEL = 11;
215 private static final int EVENT_CLOSE_CHANNEL_DONE = 12;
Jake Hambye994d462014-02-03 13:10:13 -0800216 private static final int CMD_NV_READ_ITEM = 13;
217 private static final int EVENT_NV_READ_ITEM_DONE = 14;
218 private static final int CMD_NV_WRITE_ITEM = 15;
219 private static final int EVENT_NV_WRITE_ITEM_DONE = 16;
220 private static final int CMD_NV_WRITE_CDMA_PRL = 17;
221 private static final int EVENT_NV_WRITE_CDMA_PRL_DONE = 18;
chen xu6dac5ab2018-10-26 17:39:23 -0700222 private static final int CMD_RESET_MODEM_CONFIG = 19;
223 private static final int EVENT_RESET_MODEM_CONFIG_DONE = 20;
Jake Hamby7c27be32014-03-03 13:25:59 -0800224 private static final int CMD_GET_PREFERRED_NETWORK_TYPE = 21;
225 private static final int EVENT_GET_PREFERRED_NETWORK_TYPE_DONE = 22;
226 private static final int CMD_SET_PREFERRED_NETWORK_TYPE = 23;
227 private static final int EVENT_SET_PREFERRED_NETWORK_TYPE_DONE = 24;
Sailesh Nepal35b59452014-03-06 09:26:56 -0800228 private static final int CMD_SEND_ENVELOPE = 25;
229 private static final int EVENT_SEND_ENVELOPE_DONE = 26;
Shuo Qian850e4d6a2018-04-25 21:02:08 +0000230 private static final int CMD_INVOKE_OEM_RIL_REQUEST_RAW = 27;
231 private static final int EVENT_INVOKE_OEM_RIL_REQUEST_RAW_DONE = 28;
Derek Tan6b088ee2014-09-05 14:15:18 -0700232 private static final int CMD_TRANSMIT_APDU_BASIC_CHANNEL = 29;
233 private static final int EVENT_TRANSMIT_APDU_BASIC_CHANNEL_DONE = 30;
234 private static final int CMD_EXCHANGE_SIM_IO = 31;
235 private static final int EVENT_EXCHANGE_SIM_IO_DONE = 32;
Shishir Agrawal76d5da92014-11-09 16:17:25 -0800236 private static final int CMD_SET_VOICEMAIL_NUMBER = 33;
237 private static final int EVENT_SET_VOICEMAIL_NUMBER_DONE = 34;
Stuart Scott54788802015-03-30 13:18:01 -0700238 private static final int CMD_SET_NETWORK_SELECTION_MODE_AUTOMATIC = 35;
239 private static final int EVENT_SET_NETWORK_SELECTION_MODE_AUTOMATIC_DONE = 36;
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -0700240 private static final int CMD_GET_MODEM_ACTIVITY_INFO = 37;
241 private static final int EVENT_GET_MODEM_ACTIVITY_INFO_DONE = 38;
Shishir Agrawal302c8692015-06-19 13:49:39 -0700242 private static final int CMD_PERFORM_NETWORK_SCAN = 39;
243 private static final int EVENT_PERFORM_NETWORK_SCAN_DONE = 40;
244 private static final int CMD_SET_NETWORK_SELECTION_MODE_MANUAL = 41;
245 private static final int EVENT_SET_NETWORK_SELECTION_MODE_MANUAL_DONE = 42;
Meng Wang1a7c35a2016-05-05 20:56:15 -0700246 private static final int CMD_SET_ALLOWED_CARRIERS = 43;
247 private static final int EVENT_SET_ALLOWED_CARRIERS_DONE = 44;
248 private static final int CMD_GET_ALLOWED_CARRIERS = 45;
249 private static final int EVENT_GET_ALLOWED_CARRIERS_DONE = 46;
pkanwar32d516d2016-10-14 19:37:38 -0700250 private static final int CMD_HANDLE_USSD_REQUEST = 47;
Nathan Haroldb3014052017-01-25 15:57:32 -0800251 private static final int CMD_GET_FORBIDDEN_PLMNS = 48;
252 private static final int EVENT_GET_FORBIDDEN_PLMNS_DONE = 49;
Holly Jiuyu Sun01c47ad2018-01-24 17:56:33 +0000253 private static final int CMD_SWITCH_SLOTS = 50;
254 private static final int EVENT_SWITCH_SLOTS_DONE = 51;
Pengquan Menga1bb6272018-09-06 09:59:22 -0700255 private static final int CMD_GET_NETWORK_SELECTION_MODE = 52;
256 private static final int EVENT_GET_NETWORK_SELECTION_MODE_DONE = 53;
257 private static final int CMD_GET_CDMA_ROAMING_MODE = 54;
258 private static final int EVENT_GET_CDMA_ROAMING_MODE_DONE = 55;
259 private static final int CMD_SET_CDMA_ROAMING_MODE = 56;
260 private static final int EVENT_SET_CDMA_ROAMING_MODE_DONE = 57;
261 private static final int CMD_SET_CDMA_SUBSCRIPTION_MODE = 58;
262 private static final int EVENT_SET_CDMA_SUBSCRIPTION_MODE_DONE = 59;
Nathan Harold3ff88932018-08-14 10:19:49 -0700263 private static final int CMD_GET_ALL_CELL_INFO = 60;
264 private static final int EVENT_GET_ALL_CELL_INFO_DONE = 61;
265 private static final int CMD_GET_CELL_LOCATION = 62;
266 private static final int EVENT_GET_CELL_LOCATION_DONE = 63;
chen xu6dac5ab2018-10-26 17:39:23 -0700267 private static final int CMD_MODEM_REBOOT = 64;
268 private static final int EVENT_CMD_MODEM_REBOOT_DONE = 65;
Nathan Haroldfa8da0f2018-09-27 18:51:29 -0700269 private static final int CMD_REQUEST_CELL_INFO_UPDATE = 66;
270 private static final int EVENT_REQUEST_CELL_INFO_UPDATE_DONE = 67;
Malcolm Chen8e4ed912019-01-15 20:22:16 -0800271 private static final int CMD_REQUEST_ENABLE_MODEM = 68;
272 private static final int EVENT_ENABLE_MODEM_DONE = 69;
Nazanin Bakhshif71371d2019-04-29 17:29:44 -0700273 private static final int CMD_GET_MODEM_STATUS = 70;
274 private static final int EVENT_GET_MODEM_STATUS_DONE = 71;
yincheng zhao2737e882019-09-06 17:06:54 -0700275 private static final int CMD_SET_FORBIDDEN_PLMNS = 72;
276 private static final int EVENT_SET_FORBIDDEN_PLMNS_DONE = 73;
Naina Nallurid63128d2019-09-17 14:10:30 -0700277 private static final int CMD_ERASE_MODEM_CONFIG = 74;
278 private static final int EVENT_ERASE_MODEM_CONFIG_DONE = 75;
zoey chene02881a2019-12-30 16:11:23 +0800279 private static final int CMD_CHANGE_ICC_LOCK_PASSWORD = 76;
280 private static final int EVENT_CHANGE_ICC_LOCK_PASSWORD_DONE = 77;
281 private static final int CMD_SET_ICC_LOCK_ENABLED = 78;
282 private static final int EVENT_SET_ICC_LOCK_ENABLED_DONE = 79;
Hall Liu73f5d362020-01-20 13:42:00 -0800283 private static final int CMD_SET_SYSTEM_SELECTION_CHANNELS = 80;
284 private static final int EVENT_SET_SYSTEM_SELECTION_CHANNELS_DONE = 81;
Peter Wangdafb9ac2020-01-15 14:13:38 -0800285 private static final int MSG_NOTIFY_USER_ACTIVITY = 82;
Shuo Qian4a594052020-01-23 11:59:30 -0800286 private static final int CMD_GET_CALL_FORWARDING = 83;
287 private static final int EVENT_GET_CALL_FORWARDING_DONE = 84;
288 private static final int CMD_SET_CALL_FORWARDING = 85;
289 private static final int EVENT_SET_CALL_FORWARDING_DONE = 86;
290 private static final int CMD_GET_CALL_WAITING = 87;
291 private static final int EVENT_GET_CALL_WAITING_DONE = 88;
292 private static final int CMD_SET_CALL_WAITING = 89;
293 private static final int EVENT_SET_CALL_WAITING_DONE = 90;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700294
Holly Jiuyu Sun1cc2d552018-01-26 15:51:16 -0800295 // Parameters of select command.
296 private static final int SELECT_COMMAND = 0xA4;
297 private static final int SELECT_P1 = 0x04;
298 private static final int SELECT_P2 = 0;
299 private static final int SELECT_P3 = 0x10;
300
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700301 /** The singleton instance. */
302 private static PhoneInterfaceManager sInstance;
303
Wink Saville3ab207e2014-11-20 13:07:20 -0800304 private PhoneGlobals mApp;
Wink Saville3ab207e2014-11-20 13:07:20 -0800305 private CallManager mCM;
Brad Ebinger24c29992019-12-05 13:03:21 -0800306 private ImsResolver mImsResolver;
Stuart Scott981d8582015-04-21 14:09:50 -0700307 private UserManager mUserManager;
Wink Saville3ab207e2014-11-20 13:07:20 -0800308 private AppOpsManager mAppOps;
309 private MainThreadHandler mMainThreadHandler;
Wink Savilleac1bdfd2014-11-20 23:04:44 -0800310 private SubscriptionController mSubscriptionController;
Wink Saville3ab207e2014-11-20 13:07:20 -0800311 private SharedPreferences mTelephonySharedPreferences;
Malcolm Chen2c63d402018-08-14 16:00:53 -0700312 private PhoneConfigurationManager mPhoneConfigurationManager;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700313
Peter Wangdafb9ac2020-01-15 14:13:38 -0800314 /** User Activity */
315 private AtomicBoolean mNotifyUserActivity;
Peter Wangdafb9ac2020-01-15 14:13:38 -0800316 private static final int USER_ACTIVITY_NOTIFICATION_DELAY = 200;
317
Jeff Davidson8ab02b22020-03-28 12:24:40 -0700318 private Set<Integer> mCarrierPrivilegeTestOverrideSubIds = new ArraySet<>();
319
Derek Tan97ebb422014-09-05 16:55:38 -0700320 private static final String PREF_CARRIERS_ALPHATAG_PREFIX = "carrier_alphtag_";
321 private static final String PREF_CARRIERS_NUMBER_PREFIX = "carrier_number_";
Jeff Sharkey85190e62014-12-05 09:40:12 -0800322 private static final String PREF_CARRIERS_SUBSCRIBER_PREFIX = "carrier_subscriber_";
Brad Ebinger1c8542e2019-01-14 13:43:14 -0800323 private static final String PREF_PROVISION_IMS_MMTEL_PREFIX = "provision_ims_mmtel_";
Derek Tan89e89d42014-07-08 17:00:10 -0700324
Michelecea4cf22018-12-21 15:00:11 -0800325 // String to store multi SIM allowed
326 private static final String PREF_MULTI_SIM_RESTRICTED = "multisim_restricted";
327
Derek Tan740e1672017-06-27 14:56:27 -0700328 // The AID of ISD-R.
329 private static final String ISDR_AID = "A0000005591010FFFFFFFF8900000100";
330
yinxub1bed742017-04-17 11:45:04 -0700331 private NetworkScanRequestTracker mNetworkScanRequestTracker;
332
David Kelly5e06a7f2018-03-12 14:10:59 +0000333 private static final int TYPE_ALLOCATION_CODE_LENGTH = 8;
334 private static final int MANUFACTURER_CODE_LENGTH = 8;
335
Derek Tan89e89d42014-07-08 17:00:10 -0700336 /**
Naina Nallurid63128d2019-09-17 14:10:30 -0700337 * Experiment flag to enable erase modem config on reset network, default value is false
338 */
339 public static final String RESET_NETWORK_ERASE_MODEM_CONFIG_ENABLED =
340 "reset_network_erase_modem_config_enabled";
341
342 /**
Shishir Agrawal566b7612013-10-28 14:41:00 -0700343 * A request object to use for transmitting data to an ICC.
344 */
345 private static final class IccAPDUArgument {
346 public int channel, cla, command, p1, p2, p3;
347 public String data;
348
349 public IccAPDUArgument(int channel, int cla, int command,
350 int p1, int p2, int p3, String data) {
351 this.channel = channel;
352 this.cla = cla;
353 this.command = command;
354 this.p1 = p1;
355 this.p2 = p2;
356 this.p3 = p3;
357 this.data = data;
358 }
359 }
360
361 /**
Shishir Agrawal77ba3172015-09-10 14:50:19 -0700362 * A request object to use for transmitting data to an ICC.
363 */
364 private static final class ManualNetworkSelectionArgument {
365 public OperatorInfo operatorInfo;
366 public boolean persistSelection;
367
368 public ManualNetworkSelectionArgument(OperatorInfo operatorInfo, boolean persistSelection) {
369 this.operatorInfo = operatorInfo;
370 this.persistSelection = persistSelection;
371 }
372 }
373
374 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700375 * A request object for use with {@link MainThreadHandler}. Requesters should wait() on the
376 * request after sending. The main thread will notify the request when it is complete.
377 */
378 private static final class MainThreadRequest {
379 /** The argument to use for the request */
380 public Object argument;
381 /** The result of the request that is run on the main thread */
382 public Object result;
Sanket Padawe56e75a32016-02-08 12:18:19 -0800383 // The subscriber id that this request applies to. Defaults to
384 // SubscriptionManager.INVALID_SUBSCRIPTION_ID
385 public Integer subId = SubscriptionManager.INVALID_SUBSCRIPTION_ID;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700386
Nathan Harold92bed182018-10-12 18:16:49 -0700387 // In cases where subId is unavailable, the caller needs to specify the phone.
388 public Phone phone;
389
vagdeviaf9a5b92018-08-15 16:01:53 -0700390 public WorkSource workSource;
391
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700392 public MainThreadRequest(Object argument) {
393 this.argument = argument;
394 }
Shishir Agrawal76d5da92014-11-09 16:17:25 -0800395
Nathan Harold92bed182018-10-12 18:16:49 -0700396 MainThreadRequest(Object argument, Phone phone, WorkSource workSource) {
397 this.argument = argument;
398 if (phone != null) {
399 this.phone = phone;
400 }
401 this.workSource = workSource;
402 }
403
vagdeviaf9a5b92018-08-15 16:01:53 -0700404 MainThreadRequest(Object argument, Integer subId, WorkSource workSource) {
Shishir Agrawal76d5da92014-11-09 16:17:25 -0800405 this.argument = argument;
Sanket Padawe56e75a32016-02-08 12:18:19 -0800406 if (subId != null) {
407 this.subId = subId;
408 }
vagdeviaf9a5b92018-08-15 16:01:53 -0700409 this.workSource = workSource;
Shishir Agrawal76d5da92014-11-09 16:17:25 -0800410 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700411 }
412
Sailesh Nepalcc0375f2013-11-13 09:15:18 -0800413 private static final class IncomingThirdPartyCallArgs {
414 public final ComponentName component;
415 public final String callId;
416 public final String callerDisplayName;
417
418 public IncomingThirdPartyCallArgs(ComponentName component, String callId,
419 String callerDisplayName) {
420 this.component = component;
421 this.callId = callId;
422 this.callerDisplayName = callerDisplayName;
423 }
424 }
425
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700426 /**
427 * A handler that processes messages on the main thread in the phone process. Since many
428 * of the Phone calls are not thread safe this is needed to shuttle the requests from the
429 * inbound binder threads to the main thread in the phone process. The Binder thread
430 * may provide a {@link MainThreadRequest} object in the msg.obj field that they are waiting
431 * on, which will be notified when the operation completes and will contain the result of the
432 * request.
433 *
434 * <p>If a MainThreadRequest object is provided in the msg.obj field,
435 * note that request.result must be set to something non-null for the calling thread to
436 * unblock.
437 */
438 private final class MainThreadHandler extends Handler {
439 @Override
440 public void handleMessage(Message msg) {
441 MainThreadRequest request;
442 Message onCompleted;
443 AsyncResult ar;
Shishir Agrawalc04d9752016-02-19 10:41:00 -0800444 UiccCard uiccCard;
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700445 IccAPDUArgument iccArgument;
Nazanin Bakhshif782e562018-12-11 15:15:39 -0800446 final Phone defaultPhone = getDefaultPhone();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700447
448 switch (msg.what) {
Pengquan Menga1bb6272018-09-06 09:59:22 -0700449 case CMD_HANDLE_USSD_REQUEST: {
450 request = (MainThreadRequest) msg.obj;
451 final Phone phone = getPhoneFromRequest(request);
452 Pair<String, ResultReceiver> ussdObject = (Pair) request.argument;
453 String ussdRequest = ussdObject.first;
454 ResultReceiver wrappedCallback = ussdObject.second;
Tyler Gunn65d45c22017-06-05 11:22:26 -0700455
Pengquan Menga1bb6272018-09-06 09:59:22 -0700456 if (!isUssdApiAllowed(request.subId)) {
457 // Carrier does not support use of this API, return failure.
458 Rlog.w(LOG_TAG, "handleUssdRequest: carrier does not support USSD apis.");
459 UssdResponse response = new UssdResponse(ussdRequest, null);
460 Bundle returnData = new Bundle();
461 returnData.putParcelable(TelephonyManager.USSD_RESPONSE, response);
462 wrappedCallback.send(TelephonyManager.USSD_RETURN_FAILURE, returnData);
Tyler Gunn65d45c22017-06-05 11:22:26 -0700463
Pengquan Menga1bb6272018-09-06 09:59:22 -0700464 request.result = true;
465 notifyRequester(request);
466 return;
467 }
Tyler Gunn65d45c22017-06-05 11:22:26 -0700468
Pengquan Menga1bb6272018-09-06 09:59:22 -0700469 try {
470 request.result = phone != null
471 ? phone.handleUssdRequest(ussdRequest, wrappedCallback) : false;
472 } catch (CallStateException cse) {
473 request.result = false;
474 }
475 // Wake up the requesting thread
476 notifyRequester(request);
477 break;
pkanwar32d516d2016-10-14 19:37:38 -0700478 }
479
Yorke Lee716f67e2015-06-17 15:39:16 -0700480 case CMD_HANDLE_PIN_MMI: {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700481 request = (MainThreadRequest) msg.obj;
Yorke Lee716f67e2015-06-17 15:39:16 -0700482 final Phone phone = getPhoneFromRequest(request);
483 request.result = phone != null ?
484 getPhoneFromRequest(request).handlePinMmi((String) request.argument)
485 : false;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700486 // Wake up the requesting thread
Pengquan Menga1bb6272018-09-06 09:59:22 -0700487 notifyRequester(request);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700488 break;
Yorke Lee716f67e2015-06-17 15:39:16 -0700489 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700490
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700491 case CMD_TRANSMIT_APDU_LOGICAL_CHANNEL:
Shishir Agrawal566b7612013-10-28 14:41:00 -0700492 request = (MainThreadRequest) msg.obj;
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700493 iccArgument = (IccAPDUArgument) request.argument;
Shishir Agrawalc04d9752016-02-19 10:41:00 -0800494 uiccCard = getUiccCardFromRequest(request);
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700495 if (uiccCard == null) {
496 loge("iccTransmitApduLogicalChannel: No UICC");
497 request.result = new IccIoResult(0x6F, 0, (byte[])null);
Pengquan Menga1bb6272018-09-06 09:59:22 -0700498 notifyRequester(request);
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700499 } else {
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700500 onCompleted = obtainMessage(EVENT_TRANSMIT_APDU_LOGICAL_CHANNEL_DONE,
501 request);
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700502 uiccCard.iccTransmitApduLogicalChannel(
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700503 iccArgument.channel, iccArgument.cla, iccArgument.command,
504 iccArgument.p1, iccArgument.p2, iccArgument.p3, iccArgument.data,
Shishir Agrawal566b7612013-10-28 14:41:00 -0700505 onCompleted);
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700506 }
Shishir Agrawal566b7612013-10-28 14:41:00 -0700507 break;
508
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700509 case EVENT_TRANSMIT_APDU_LOGICAL_CHANNEL_DONE:
Shishir Agrawal566b7612013-10-28 14:41:00 -0700510 ar = (AsyncResult) msg.obj;
511 request = (MainThreadRequest) ar.userObj;
512 if (ar.exception == null && ar.result != null) {
513 request.result = ar.result;
514 } else {
515 request.result = new IccIoResult(0x6F, 0, (byte[])null);
516 if (ar.result == null) {
517 loge("iccTransmitApduLogicalChannel: Empty response");
Jake Hambye994d462014-02-03 13:10:13 -0800518 } else if (ar.exception instanceof CommandException) {
Shishir Agrawal566b7612013-10-28 14:41:00 -0700519 loge("iccTransmitApduLogicalChannel: CommandException: " +
Jake Hambye994d462014-02-03 13:10:13 -0800520 ar.exception);
Shishir Agrawal566b7612013-10-28 14:41:00 -0700521 } else {
522 loge("iccTransmitApduLogicalChannel: Unknown exception");
523 }
524 }
Pengquan Menga1bb6272018-09-06 09:59:22 -0700525 notifyRequester(request);
Shishir Agrawal566b7612013-10-28 14:41:00 -0700526 break;
527
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700528 case CMD_TRANSMIT_APDU_BASIC_CHANNEL:
529 request = (MainThreadRequest) msg.obj;
530 iccArgument = (IccAPDUArgument) request.argument;
Shishir Agrawalc04d9752016-02-19 10:41:00 -0800531 uiccCard = getUiccCardFromRequest(request);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700532 if (uiccCard == null) {
533 loge("iccTransmitApduBasicChannel: No UICC");
534 request.result = new IccIoResult(0x6F, 0, (byte[])null);
Pengquan Menga1bb6272018-09-06 09:59:22 -0700535 notifyRequester(request);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700536 } else {
537 onCompleted = obtainMessage(EVENT_TRANSMIT_APDU_BASIC_CHANNEL_DONE,
538 request);
539 uiccCard.iccTransmitApduBasicChannel(
540 iccArgument.cla, iccArgument.command, iccArgument.p1, iccArgument.p2,
541 iccArgument.p3, iccArgument.data, onCompleted);
542 }
543 break;
544
545 case EVENT_TRANSMIT_APDU_BASIC_CHANNEL_DONE:
546 ar = (AsyncResult) msg.obj;
547 request = (MainThreadRequest) ar.userObj;
548 if (ar.exception == null && ar.result != null) {
549 request.result = ar.result;
550 } else {
551 request.result = new IccIoResult(0x6F, 0, (byte[])null);
552 if (ar.result == null) {
553 loge("iccTransmitApduBasicChannel: Empty response");
554 } else if (ar.exception instanceof CommandException) {
555 loge("iccTransmitApduBasicChannel: CommandException: " +
556 ar.exception);
557 } else {
558 loge("iccTransmitApduBasicChannel: Unknown exception");
559 }
560 }
Pengquan Menga1bb6272018-09-06 09:59:22 -0700561 notifyRequester(request);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700562 break;
563
564 case CMD_EXCHANGE_SIM_IO:
565 request = (MainThreadRequest) msg.obj;
566 iccArgument = (IccAPDUArgument) request.argument;
Shishir Agrawalc04d9752016-02-19 10:41:00 -0800567 uiccCard = getUiccCardFromRequest(request);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700568 if (uiccCard == null) {
569 loge("iccExchangeSimIO: No UICC");
570 request.result = new IccIoResult(0x6F, 0, (byte[])null);
Pengquan Menga1bb6272018-09-06 09:59:22 -0700571 notifyRequester(request);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700572 } else {
573 onCompleted = obtainMessage(EVENT_EXCHANGE_SIM_IO_DONE,
574 request);
575 uiccCard.iccExchangeSimIO(iccArgument.cla, /* fileID */
576 iccArgument.command, iccArgument.p1, iccArgument.p2, iccArgument.p3,
577 iccArgument.data, onCompleted);
578 }
579 break;
580
581 case EVENT_EXCHANGE_SIM_IO_DONE:
582 ar = (AsyncResult) msg.obj;
583 request = (MainThreadRequest) ar.userObj;
584 if (ar.exception == null && ar.result != null) {
585 request.result = ar.result;
586 } else {
587 request.result = new IccIoResult(0x6f, 0, (byte[])null);
588 }
Pengquan Menga1bb6272018-09-06 09:59:22 -0700589 notifyRequester(request);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700590 break;
591
Derek Tan4d5e5c12014-02-04 11:54:58 -0800592 case CMD_SEND_ENVELOPE:
593 request = (MainThreadRequest) msg.obj;
Shishir Agrawalc04d9752016-02-19 10:41:00 -0800594 uiccCard = getUiccCardFromRequest(request);
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700595 if (uiccCard == null) {
596 loge("sendEnvelopeWithStatus: No UICC");
597 request.result = new IccIoResult(0x6F, 0, (byte[])null);
Pengquan Menga1bb6272018-09-06 09:59:22 -0700598 notifyRequester(request);
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700599 } else {
600 onCompleted = obtainMessage(EVENT_SEND_ENVELOPE_DONE, request);
601 uiccCard.sendEnvelopeWithStatus((String)request.argument, onCompleted);
602 }
Derek Tan4d5e5c12014-02-04 11:54:58 -0800603 break;
604
605 case EVENT_SEND_ENVELOPE_DONE:
606 ar = (AsyncResult) msg.obj;
607 request = (MainThreadRequest) ar.userObj;
Shishir Agrawal9f9877d2014-03-14 09:36:27 -0700608 if (ar.exception == null && ar.result != null) {
609 request.result = ar.result;
Derek Tan4d5e5c12014-02-04 11:54:58 -0800610 } else {
Shishir Agrawal9f9877d2014-03-14 09:36:27 -0700611 request.result = new IccIoResult(0x6F, 0, (byte[])null);
612 if (ar.result == null) {
613 loge("sendEnvelopeWithStatus: Empty response");
614 } else if (ar.exception instanceof CommandException) {
615 loge("sendEnvelopeWithStatus: CommandException: " +
616 ar.exception);
617 } else {
618 loge("sendEnvelopeWithStatus: exception:" + ar.exception);
619 }
Derek Tan4d5e5c12014-02-04 11:54:58 -0800620 }
Pengquan Menga1bb6272018-09-06 09:59:22 -0700621 notifyRequester(request);
Derek Tan4d5e5c12014-02-04 11:54:58 -0800622 break;
623
Shishir Agrawal566b7612013-10-28 14:41:00 -0700624 case CMD_OPEN_CHANNEL:
625 request = (MainThreadRequest) msg.obj;
Shishir Agrawalc04d9752016-02-19 10:41:00 -0800626 uiccCard = getUiccCardFromRequest(request);
Ajay Nambid7454d32015-12-03 13:50:00 -0800627 Pair<String, Integer> openChannelArgs = (Pair<String, Integer>) request.argument;
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700628 if (uiccCard == null) {
629 loge("iccOpenLogicalChannel: No UICC");
Shishir Agrawalfc0492a2016-02-17 11:15:33 -0800630 request.result = new IccOpenLogicalChannelResponse(-1,
631 IccOpenLogicalChannelResponse.STATUS_MISSING_RESOURCE, null);
Pengquan Menga1bb6272018-09-06 09:59:22 -0700632 notifyRequester(request);
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700633 } else {
634 onCompleted = obtainMessage(EVENT_OPEN_CHANNEL_DONE, request);
Ajay Nambid7454d32015-12-03 13:50:00 -0800635 uiccCard.iccOpenLogicalChannel(openChannelArgs.first,
636 openChannelArgs.second, onCompleted);
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700637 }
Shishir Agrawal566b7612013-10-28 14:41:00 -0700638 break;
639
640 case EVENT_OPEN_CHANNEL_DONE:
641 ar = (AsyncResult) msg.obj;
642 request = (MainThreadRequest) ar.userObj;
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700643 IccOpenLogicalChannelResponse openChannelResp;
Shishir Agrawal566b7612013-10-28 14:41:00 -0700644 if (ar.exception == null && ar.result != null) {
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700645 int[] result = (int[]) ar.result;
646 int channelId = result[0];
647 byte[] selectResponse = null;
648 if (result.length > 1) {
649 selectResponse = new byte[result.length - 1];
650 for (int i = 1; i < result.length; ++i) {
651 selectResponse[i - 1] = (byte) result[i];
652 }
653 }
654 openChannelResp = new IccOpenLogicalChannelResponse(channelId,
Shishir Agrawal527e8bf2014-08-25 08:54:56 -0700655 IccOpenLogicalChannelResponse.STATUS_NO_ERROR, selectResponse);
Shishir Agrawal566b7612013-10-28 14:41:00 -0700656 } else {
Shishir Agrawal566b7612013-10-28 14:41:00 -0700657 if (ar.result == null) {
658 loge("iccOpenLogicalChannel: Empty response");
Shishir Agrawal566b7612013-10-28 14:41:00 -0700659 }
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700660 if (ar.exception != null) {
661 loge("iccOpenLogicalChannel: Exception: " + ar.exception);
662 }
663
Shishir Agrawal527e8bf2014-08-25 08:54:56 -0700664 int errorCode = IccOpenLogicalChannelResponse.STATUS_UNKNOWN_ERROR;
Junda Liua754ba12015-05-20 01:17:52 -0700665 if (ar.exception instanceof CommandException) {
666 CommandException.Error error =
667 ((CommandException) (ar.exception)).getCommandError();
668 if (error == CommandException.Error.MISSING_RESOURCE) {
Shishir Agrawal527e8bf2014-08-25 08:54:56 -0700669 errorCode = IccOpenLogicalChannelResponse.STATUS_MISSING_RESOURCE;
Junda Liua754ba12015-05-20 01:17:52 -0700670 } else if (error == CommandException.Error.NO_SUCH_ELEMENT) {
Shishir Agrawal527e8bf2014-08-25 08:54:56 -0700671 errorCode = IccOpenLogicalChannelResponse.STATUS_NO_SUCH_ELEMENT;
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700672 }
673 }
674 openChannelResp = new IccOpenLogicalChannelResponse(
675 IccOpenLogicalChannelResponse.INVALID_CHANNEL, errorCode, null);
Shishir Agrawal566b7612013-10-28 14:41:00 -0700676 }
Shishir Agrawal82c8a462014-07-31 18:13:17 -0700677 request.result = openChannelResp;
Pengquan Menga1bb6272018-09-06 09:59:22 -0700678 notifyRequester(request);
Shishir Agrawal566b7612013-10-28 14:41:00 -0700679 break;
680
681 case CMD_CLOSE_CHANNEL:
682 request = (MainThreadRequest) msg.obj;
Shishir Agrawalc04d9752016-02-19 10:41:00 -0800683 uiccCard = getUiccCardFromRequest(request);
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700684 if (uiccCard == null) {
685 loge("iccCloseLogicalChannel: No UICC");
Yoshiaki Naka2e29d822016-09-02 19:27:39 +0900686 request.result = false;
Pengquan Menga1bb6272018-09-06 09:59:22 -0700687 notifyRequester(request);
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700688 } else {
689 onCompleted = obtainMessage(EVENT_CLOSE_CHANNEL_DONE, request);
690 uiccCard.iccCloseLogicalChannel((Integer) request.argument, onCompleted);
691 }
Shishir Agrawal566b7612013-10-28 14:41:00 -0700692 break;
693
694 case EVENT_CLOSE_CHANNEL_DONE:
Jake Hambye994d462014-02-03 13:10:13 -0800695 handleNullReturnEvent(msg, "iccCloseLogicalChannel");
696 break;
697
698 case CMD_NV_READ_ITEM:
699 request = (MainThreadRequest) msg.obj;
700 onCompleted = obtainMessage(EVENT_NV_READ_ITEM_DONE, request);
Nazanin Bakhshif782e562018-12-11 15:15:39 -0800701 defaultPhone.nvReadItem((Integer) request.argument, onCompleted,
702 request.workSource);
Jake Hambye994d462014-02-03 13:10:13 -0800703 break;
704
705 case EVENT_NV_READ_ITEM_DONE:
Shishir Agrawal566b7612013-10-28 14:41:00 -0700706 ar = (AsyncResult) msg.obj;
707 request = (MainThreadRequest) ar.userObj;
Jake Hambye994d462014-02-03 13:10:13 -0800708 if (ar.exception == null && ar.result != null) {
709 request.result = ar.result; // String
Shishir Agrawal566b7612013-10-28 14:41:00 -0700710 } else {
Jake Hambye994d462014-02-03 13:10:13 -0800711 request.result = "";
712 if (ar.result == null) {
713 loge("nvReadItem: Empty response");
714 } else if (ar.exception instanceof CommandException) {
715 loge("nvReadItem: CommandException: " +
716 ar.exception);
Shishir Agrawal566b7612013-10-28 14:41:00 -0700717 } else {
Jake Hambye994d462014-02-03 13:10:13 -0800718 loge("nvReadItem: Unknown exception");
Shishir Agrawal566b7612013-10-28 14:41:00 -0700719 }
720 }
Pengquan Menga1bb6272018-09-06 09:59:22 -0700721 notifyRequester(request);
Shishir Agrawal566b7612013-10-28 14:41:00 -0700722 break;
723
Jake Hambye994d462014-02-03 13:10:13 -0800724 case CMD_NV_WRITE_ITEM:
725 request = (MainThreadRequest) msg.obj;
726 onCompleted = obtainMessage(EVENT_NV_WRITE_ITEM_DONE, request);
727 Pair<Integer, String> idValue = (Pair<Integer, String>) request.argument;
Nazanin Bakhshif782e562018-12-11 15:15:39 -0800728 defaultPhone.nvWriteItem(idValue.first, idValue.second, onCompleted,
vagdeviaf9a5b92018-08-15 16:01:53 -0700729 request.workSource);
Jake Hambye994d462014-02-03 13:10:13 -0800730 break;
731
732 case EVENT_NV_WRITE_ITEM_DONE:
733 handleNullReturnEvent(msg, "nvWriteItem");
734 break;
735
736 case CMD_NV_WRITE_CDMA_PRL:
737 request = (MainThreadRequest) msg.obj;
738 onCompleted = obtainMessage(EVENT_NV_WRITE_CDMA_PRL_DONE, request);
Nazanin Bakhshif782e562018-12-11 15:15:39 -0800739 defaultPhone.nvWriteCdmaPrl((byte[]) request.argument, onCompleted);
Jake Hambye994d462014-02-03 13:10:13 -0800740 break;
741
742 case EVENT_NV_WRITE_CDMA_PRL_DONE:
743 handleNullReturnEvent(msg, "nvWriteCdmaPrl");
744 break;
745
chen xu6dac5ab2018-10-26 17:39:23 -0700746 case CMD_RESET_MODEM_CONFIG:
Jake Hambye994d462014-02-03 13:10:13 -0800747 request = (MainThreadRequest) msg.obj;
chen xu6dac5ab2018-10-26 17:39:23 -0700748 onCompleted = obtainMessage(EVENT_RESET_MODEM_CONFIG_DONE, request);
Nazanin Bakhshif782e562018-12-11 15:15:39 -0800749 defaultPhone.resetModemConfig(onCompleted);
Jake Hambye994d462014-02-03 13:10:13 -0800750 break;
751
chen xu6dac5ab2018-10-26 17:39:23 -0700752 case EVENT_RESET_MODEM_CONFIG_DONE:
753 handleNullReturnEvent(msg, "resetModemConfig");
Jake Hambye994d462014-02-03 13:10:13 -0800754 break;
755
Jake Hamby7c27be32014-03-03 13:25:59 -0800756 case CMD_GET_PREFERRED_NETWORK_TYPE:
757 request = (MainThreadRequest) msg.obj;
758 onCompleted = obtainMessage(EVENT_GET_PREFERRED_NETWORK_TYPE_DONE, request);
Stuart Scott54788802015-03-30 13:18:01 -0700759 getPhoneFromRequest(request).getPreferredNetworkType(onCompleted);
Jake Hamby7c27be32014-03-03 13:25:59 -0800760 break;
761
762 case EVENT_GET_PREFERRED_NETWORK_TYPE_DONE:
763 ar = (AsyncResult) msg.obj;
764 request = (MainThreadRequest) ar.userObj;
765 if (ar.exception == null && ar.result != null) {
766 request.result = ar.result; // Integer
767 } else {
Nazish Tabassume8ba43a2020-07-28 14:49:25 +0530768 // request.result must be set to something non-null
769 // for the calling thread to unblock
770 request.result = new int[]{-1};
Jake Hamby7c27be32014-03-03 13:25:59 -0800771 if (ar.result == null) {
772 loge("getPreferredNetworkType: Empty response");
773 } else if (ar.exception instanceof CommandException) {
774 loge("getPreferredNetworkType: CommandException: " +
775 ar.exception);
776 } else {
777 loge("getPreferredNetworkType: Unknown exception");
778 }
779 }
Pengquan Menga1bb6272018-09-06 09:59:22 -0700780 notifyRequester(request);
Jake Hamby7c27be32014-03-03 13:25:59 -0800781 break;
782
783 case CMD_SET_PREFERRED_NETWORK_TYPE:
784 request = (MainThreadRequest) msg.obj;
785 onCompleted = obtainMessage(EVENT_SET_PREFERRED_NETWORK_TYPE_DONE, request);
786 int networkType = (Integer) request.argument;
Stuart Scott54788802015-03-30 13:18:01 -0700787 getPhoneFromRequest(request).setPreferredNetworkType(networkType, onCompleted);
Jake Hamby7c27be32014-03-03 13:25:59 -0800788 break;
789
790 case EVENT_SET_PREFERRED_NETWORK_TYPE_DONE:
791 handleNullReturnEvent(msg, "setPreferredNetworkType");
792 break;
793
Shuo Qian850e4d6a2018-04-25 21:02:08 +0000794 case CMD_INVOKE_OEM_RIL_REQUEST_RAW:
795 request = (MainThreadRequest)msg.obj;
796 onCompleted = obtainMessage(EVENT_INVOKE_OEM_RIL_REQUEST_RAW_DONE, request);
Nazanin Bakhshif782e562018-12-11 15:15:39 -0800797 defaultPhone.invokeOemRilRequestRaw((byte[]) request.argument, onCompleted);
Shuo Qian850e4d6a2018-04-25 21:02:08 +0000798 break;
799
800 case EVENT_INVOKE_OEM_RIL_REQUEST_RAW_DONE:
801 ar = (AsyncResult)msg.obj;
802 request = (MainThreadRequest)ar.userObj;
803 request.result = ar;
Pengquan Menga1bb6272018-09-06 09:59:22 -0700804 notifyRequester(request);
Shuo Qian850e4d6a2018-04-25 21:02:08 +0000805 break;
806
Shishir Agrawal76d5da92014-11-09 16:17:25 -0800807 case CMD_SET_VOICEMAIL_NUMBER:
808 request = (MainThreadRequest) msg.obj;
809 onCompleted = obtainMessage(EVENT_SET_VOICEMAIL_NUMBER_DONE, request);
810 Pair<String, String> tagNum = (Pair<String, String>) request.argument;
Stuart Scott584921c2015-01-15 17:10:34 -0800811 getPhoneFromRequest(request).setVoiceMailNumber(tagNum.first, tagNum.second,
812 onCompleted);
Shishir Agrawal76d5da92014-11-09 16:17:25 -0800813 break;
814
815 case EVENT_SET_VOICEMAIL_NUMBER_DONE:
816 handleNullReturnEvent(msg, "setVoicemailNumber");
817 break;
818
Stuart Scott54788802015-03-30 13:18:01 -0700819 case CMD_SET_NETWORK_SELECTION_MODE_AUTOMATIC:
820 request = (MainThreadRequest) msg.obj;
821 onCompleted = obtainMessage(EVENT_SET_NETWORK_SELECTION_MODE_AUTOMATIC_DONE,
822 request);
823 getPhoneFromRequest(request).setNetworkSelectionModeAutomatic(onCompleted);
824 break;
825
826 case EVENT_SET_NETWORK_SELECTION_MODE_AUTOMATIC_DONE:
827 handleNullReturnEvent(msg, "setNetworkSelectionModeAutomatic");
828 break;
829
Shishir Agrawal302c8692015-06-19 13:49:39 -0700830 case CMD_PERFORM_NETWORK_SCAN:
831 request = (MainThreadRequest) msg.obj;
832 onCompleted = obtainMessage(EVENT_PERFORM_NETWORK_SCAN_DONE, request);
833 getPhoneFromRequest(request).getAvailableNetworks(onCompleted);
834 break;
835
Hall Liu27d24262020-09-18 19:04:59 -0700836 case CMD_GET_CALL_FORWARDING: {
Shuo Qian4a594052020-01-23 11:59:30 -0800837 request = (MainThreadRequest) msg.obj;
838 onCompleted = obtainMessage(EVENT_GET_CALL_FORWARDING_DONE, request);
Hall Liu27d24262020-09-18 19:04:59 -0700839 Pair<Integer, TelephonyManager.CallForwardingInfoCallback> args =
840 (Pair<Integer, TelephonyManager.CallForwardingInfoCallback>)
841 request.argument;
842 int callForwardingReason = args.first;
843 request.phone.getCallForwardingOption(callForwardingReason, onCompleted);
Shuo Qian4a594052020-01-23 11:59:30 -0800844 break;
Hall Liu27d24262020-09-18 19:04:59 -0700845 }
846 case EVENT_GET_CALL_FORWARDING_DONE: {
Shuo Qian4a594052020-01-23 11:59:30 -0800847 ar = (AsyncResult) msg.obj;
848 request = (MainThreadRequest) ar.userObj;
Hall Liu27d24262020-09-18 19:04:59 -0700849 TelephonyManager.CallForwardingInfoCallback callback =
850 ((Pair<Integer, TelephonyManager.CallForwardingInfoCallback>)
851 request.argument).second;
Shuo Qian4a594052020-01-23 11:59:30 -0800852 if (ar.exception == null && ar.result != null) {
Hall Liu27d24262020-09-18 19:04:59 -0700853 CallForwardingInfo callForwardingInfo = null;
Shuo Qian4a594052020-01-23 11:59:30 -0800854 CallForwardInfo[] callForwardInfos = (CallForwardInfo[]) ar.result;
855 for (CallForwardInfo callForwardInfo : callForwardInfos) {
856 // Service Class is a bit mask per 3gpp 27.007. Search for
857 // any service for voice call.
858 if ((callForwardInfo.serviceClass
859 & CommandsInterface.SERVICE_CLASS_VOICE) > 0) {
Hall Liu27d24262020-09-18 19:04:59 -0700860 callForwardingInfo = new CallForwardingInfo(true,
861 callForwardInfo.reason,
862 callForwardInfo.number,
863 callForwardInfo.timeSeconds);
Shuo Qian4a594052020-01-23 11:59:30 -0800864 break;
865 }
866 }
867 // Didn't find a call forward info for voice call.
868 if (callForwardingInfo == null) {
Hall Liu27d24262020-09-18 19:04:59 -0700869 callForwardingInfo = new CallForwardingInfo(false /* enabled */,
870 0 /* reason */, null /* number */, 0 /* timeout */);
Shuo Qian4a594052020-01-23 11:59:30 -0800871 }
Hall Liu27d24262020-09-18 19:04:59 -0700872 callback.onCallForwardingInfoAvailable(callForwardingInfo);
Shuo Qian4a594052020-01-23 11:59:30 -0800873 } else {
874 if (ar.result == null) {
875 loge("EVENT_GET_CALL_FORWARDING_DONE: Empty response");
876 }
877 if (ar.exception != null) {
878 loge("EVENT_GET_CALL_FORWARDING_DONE: Exception: " + ar.exception);
879 }
Hall Liu940c4ca2020-09-29 17:10:18 -0700880 int errorCode = TelephonyManager
881 .CallForwardingInfoCallback.RESULT_ERROR_UNKNOWN;
Shuo Qian4a594052020-01-23 11:59:30 -0800882 if (ar.exception instanceof CommandException) {
883 CommandException.Error error =
884 ((CommandException) (ar.exception)).getCommandError();
885 if (error == CommandException.Error.FDN_CHECK_FAILURE) {
Hall Liu940c4ca2020-09-29 17:10:18 -0700886 errorCode = TelephonyManager
887 .CallForwardingInfoCallback.RESULT_ERROR_FDN_CHECK_FAILURE;
Shuo Qian4a594052020-01-23 11:59:30 -0800888 } else if (error == CommandException.Error.REQUEST_NOT_SUPPORTED) {
Hall Liu940c4ca2020-09-29 17:10:18 -0700889 errorCode = TelephonyManager
890 .CallForwardingInfoCallback.RESULT_ERROR_NOT_SUPPORTED;
Shuo Qian4a594052020-01-23 11:59:30 -0800891 }
892 }
Hall Liu27d24262020-09-18 19:04:59 -0700893 callback.onError(errorCode);
Shuo Qian4a594052020-01-23 11:59:30 -0800894 }
Shuo Qian4a594052020-01-23 11:59:30 -0800895 break;
Hall Liu27d24262020-09-18 19:04:59 -0700896 }
Shuo Qian4a594052020-01-23 11:59:30 -0800897
Hall Liu27d24262020-09-18 19:04:59 -0700898 case CMD_SET_CALL_FORWARDING: {
Shuo Qian4a594052020-01-23 11:59:30 -0800899 request = (MainThreadRequest) msg.obj;
900 onCompleted = obtainMessage(EVENT_SET_CALL_FORWARDING_DONE, request);
Hall Liu27d24262020-09-18 19:04:59 -0700901 request = (MainThreadRequest) msg.obj;
Shuo Qian4a594052020-01-23 11:59:30 -0800902 CallForwardingInfo callForwardingInfoToSet =
Hall Liu27d24262020-09-18 19:04:59 -0700903 ((Pair<CallForwardingInfo, Consumer<Integer>>)
904 request.argument).first;
905 request.phone.setCallForwardingOption(
906 callForwardingInfoToSet.isEnabled()
907 ? CommandsInterface.CF_ACTION_ENABLE
908 : CommandsInterface.CF_ACTION_DISABLE,
Shuo Qian4a594052020-01-23 11:59:30 -0800909 callForwardingInfoToSet.getReason(),
910 callForwardingInfoToSet.getNumber(),
911 callForwardingInfoToSet.getTimeoutSeconds(), onCompleted);
912 break;
Hall Liu27d24262020-09-18 19:04:59 -0700913 }
Shuo Qian4a594052020-01-23 11:59:30 -0800914
Hall Liu27d24262020-09-18 19:04:59 -0700915 case EVENT_SET_CALL_FORWARDING_DONE: {
Shuo Qian4a594052020-01-23 11:59:30 -0800916 ar = (AsyncResult) msg.obj;
917 request = (MainThreadRequest) ar.userObj;
Hall Liu27d24262020-09-18 19:04:59 -0700918 Consumer<Integer> callback =
919 ((Pair<CallForwardingInfo, Consumer<Integer>>)
920 request.argument).second;
921 if (ar.exception != null) {
Shuo Qian4a594052020-01-23 11:59:30 -0800922 loge("setCallForwarding exception: " + ar.exception);
Hall Liu940c4ca2020-09-29 17:10:18 -0700923 int errorCode = TelephonyManager.CallForwardingInfoCallback
924 .RESULT_ERROR_UNKNOWN;
Hall Liu27d24262020-09-18 19:04:59 -0700925 if (ar.exception instanceof CommandException) {
926 CommandException.Error error =
927 ((CommandException) (ar.exception)).getCommandError();
928 if (error == CommandException.Error.FDN_CHECK_FAILURE) {
Hall Liu940c4ca2020-09-29 17:10:18 -0700929 errorCode = TelephonyManager.CallForwardingInfoCallback
930 .RESULT_ERROR_FDN_CHECK_FAILURE;
Hall Liu27d24262020-09-18 19:04:59 -0700931 } else if (error == CommandException.Error.REQUEST_NOT_SUPPORTED) {
Hall Liu940c4ca2020-09-29 17:10:18 -0700932 errorCode = TelephonyManager.CallForwardingInfoCallback
933 .RESULT_ERROR_NOT_SUPPORTED;
Hall Liu27d24262020-09-18 19:04:59 -0700934 }
935 }
936 callback.accept(errorCode);
937 } else {
Hall Liu940c4ca2020-09-29 17:10:18 -0700938 callback.accept(TelephonyManager.CallForwardingInfoCallback.RESULT_SUCCESS);
Shuo Qian4a594052020-01-23 11:59:30 -0800939 }
Shuo Qian4a594052020-01-23 11:59:30 -0800940 break;
Hall Liu27d24262020-09-18 19:04:59 -0700941 }
Shuo Qian4a594052020-01-23 11:59:30 -0800942
Hall Liu27d24262020-09-18 19:04:59 -0700943 case CMD_GET_CALL_WAITING: {
Shuo Qian4a594052020-01-23 11:59:30 -0800944 request = (MainThreadRequest) msg.obj;
945 onCompleted = obtainMessage(EVENT_GET_CALL_WAITING_DONE, request);
946 getPhoneFromRequest(request).getCallWaiting(onCompleted);
947 break;
Hall Liu27d24262020-09-18 19:04:59 -0700948 }
Shuo Qian4a594052020-01-23 11:59:30 -0800949
Hall Liu27d24262020-09-18 19:04:59 -0700950 case EVENT_GET_CALL_WAITING_DONE: {
Shuo Qian4a594052020-01-23 11:59:30 -0800951 ar = (AsyncResult) msg.obj;
952 request = (MainThreadRequest) ar.userObj;
Hall Liu27d24262020-09-18 19:04:59 -0700953 Consumer<Integer> callback = (Consumer<Integer>) request.argument;
Shuo Qian4a594052020-01-23 11:59:30 -0800954 int callForwardingStatus = TelephonyManager.CALL_WAITING_STATUS_UNKNOWN_ERROR;
955 if (ar.exception == null && ar.result != null) {
Shuo Qiand6a0dba2020-02-18 18:13:49 -0800956 int[] callForwardResults = (int[]) ar.result;
Shuo Qian4a594052020-01-23 11:59:30 -0800957 // Service Class is a bit mask per 3gpp 27.007.
958 // Search for any service for voice call.
Shuo Qiand6a0dba2020-02-18 18:13:49 -0800959 if (callForwardResults.length > 1
960 && ((callForwardResults[1]
Hall Liu27d24262020-09-18 19:04:59 -0700961 & CommandsInterface.SERVICE_CLASS_VOICE) > 0)) {
Shuo Qiand6a0dba2020-02-18 18:13:49 -0800962 callForwardingStatus = callForwardResults[0] == 0
Hall Liu27d24262020-09-18 19:04:59 -0700963 ? TelephonyManager.CALL_WAITING_STATUS_DISABLED
964 : TelephonyManager.CALL_WAITING_STATUS_ENABLED;
Shuo Qian4a594052020-01-23 11:59:30 -0800965 } else {
Hall Liu27d24262020-09-18 19:04:59 -0700966 callForwardingStatus = TelephonyManager.CALL_WAITING_STATUS_DISABLED;
Shuo Qian4a594052020-01-23 11:59:30 -0800967 }
968 } else {
969 if (ar.result == null) {
970 loge("EVENT_GET_CALL_WAITING_DONE: Empty response");
971 }
972 if (ar.exception != null) {
973 loge("EVENT_GET_CALL_WAITING_DONE: Exception: " + ar.exception);
974 }
975 if (ar.exception instanceof CommandException) {
976 CommandException.Error error =
977 ((CommandException) (ar.exception)).getCommandError();
978 if (error == CommandException.Error.REQUEST_NOT_SUPPORTED) {
979 callForwardingStatus =
980 TelephonyManager.CALL_WAITING_STATUS_NOT_SUPPORTED;
981 }
982 }
983 }
Hall Liu27d24262020-09-18 19:04:59 -0700984 callback.accept(callForwardingStatus);
Shuo Qian4a594052020-01-23 11:59:30 -0800985 break;
Hall Liu27d24262020-09-18 19:04:59 -0700986 }
Shuo Qian4a594052020-01-23 11:59:30 -0800987
Hall Liu27d24262020-09-18 19:04:59 -0700988 case CMD_SET_CALL_WAITING: {
Shuo Qian4a594052020-01-23 11:59:30 -0800989 request = (MainThreadRequest) msg.obj;
990 onCompleted = obtainMessage(EVENT_SET_CALL_WAITING_DONE, request);
Hall Liu27d24262020-09-18 19:04:59 -0700991 boolean enable = ((Pair<Boolean, Consumer<Integer>>) request.argument).first;
992 getPhoneFromRequest(request).setCallWaiting(enable, onCompleted);
Shuo Qian4a594052020-01-23 11:59:30 -0800993 break;
Hall Liu27d24262020-09-18 19:04:59 -0700994 }
Shuo Qian4a594052020-01-23 11:59:30 -0800995
Hall Liu27d24262020-09-18 19:04:59 -0700996 case EVENT_SET_CALL_WAITING_DONE: {
Shuo Qian4a594052020-01-23 11:59:30 -0800997 ar = (AsyncResult) msg.obj;
998 request = (MainThreadRequest) ar.userObj;
Hall Liu27d24262020-09-18 19:04:59 -0700999 boolean enable = ((Pair<Boolean, Consumer<Integer>>) request.argument).first;
1000 Consumer<Integer> callback =
1001 ((Pair<Boolean, Consumer<Integer>>) request.argument).second;
1002 if (ar.exception != null) {
Shuo Qian4a594052020-01-23 11:59:30 -08001003 loge("setCallWaiting exception: " + ar.exception);
Hall Liu27d24262020-09-18 19:04:59 -07001004 if (ar.exception instanceof CommandException) {
1005 CommandException.Error error =
1006 ((CommandException) (ar.exception)).getCommandError();
1007 if (error == CommandException.Error.REQUEST_NOT_SUPPORTED) {
1008 callback.accept(TelephonyManager.CALL_WAITING_STATUS_NOT_SUPPORTED);
1009 } else {
1010 callback.accept(TelephonyManager.CALL_WAITING_STATUS_UNKNOWN_ERROR);
1011 }
1012 } else {
1013 callback.accept(TelephonyManager.CALL_WAITING_STATUS_UNKNOWN_ERROR);
1014 }
1015 } else {
1016 callback.accept(enable ? TelephonyManager.CALL_WAITING_STATUS_ENABLED
1017 : TelephonyManager.CALL_WAITING_STATUS_DISABLED);
Shuo Qian4a594052020-01-23 11:59:30 -08001018 }
Shuo Qian4a594052020-01-23 11:59:30 -08001019 break;
Hall Liu27d24262020-09-18 19:04:59 -07001020 }
Shuo Qian4a594052020-01-23 11:59:30 -08001021
Shishir Agrawal302c8692015-06-19 13:49:39 -07001022 case EVENT_PERFORM_NETWORK_SCAN_DONE:
1023 ar = (AsyncResult) msg.obj;
1024 request = (MainThreadRequest) ar.userObj;
1025 CellNetworkScanResult cellScanResult;
1026 if (ar.exception == null && ar.result != null) {
1027 cellScanResult = new CellNetworkScanResult(
1028 CellNetworkScanResult.STATUS_SUCCESS,
1029 (List<OperatorInfo>) ar.result);
1030 } else {
1031 if (ar.result == null) {
1032 loge("getCellNetworkScanResults: Empty response");
1033 }
1034 if (ar.exception != null) {
1035 loge("getCellNetworkScanResults: Exception: " + ar.exception);
1036 }
1037 int errorCode = CellNetworkScanResult.STATUS_UNKNOWN_ERROR;
1038 if (ar.exception instanceof CommandException) {
1039 CommandException.Error error =
1040 ((CommandException) (ar.exception)).getCommandError();
1041 if (error == CommandException.Error.RADIO_NOT_AVAILABLE) {
1042 errorCode = CellNetworkScanResult.STATUS_RADIO_NOT_AVAILABLE;
1043 } else if (error == CommandException.Error.GENERIC_FAILURE) {
1044 errorCode = CellNetworkScanResult.STATUS_RADIO_GENERIC_FAILURE;
1045 }
1046 }
1047 cellScanResult = new CellNetworkScanResult(errorCode, null);
1048 }
1049 request.result = cellScanResult;
Pengquan Menga1bb6272018-09-06 09:59:22 -07001050 notifyRequester(request);
Shishir Agrawal302c8692015-06-19 13:49:39 -07001051 break;
1052
1053 case CMD_SET_NETWORK_SELECTION_MODE_MANUAL:
1054 request = (MainThreadRequest) msg.obj;
Shishir Agrawal77ba3172015-09-10 14:50:19 -07001055 ManualNetworkSelectionArgument selArg =
1056 (ManualNetworkSelectionArgument) request.argument;
Shishir Agrawal302c8692015-06-19 13:49:39 -07001057 onCompleted = obtainMessage(EVENT_SET_NETWORK_SELECTION_MODE_MANUAL_DONE,
1058 request);
Shishir Agrawal77ba3172015-09-10 14:50:19 -07001059 getPhoneFromRequest(request).selectNetworkManually(selArg.operatorInfo,
1060 selArg.persistSelection, onCompleted);
Shishir Agrawal302c8692015-06-19 13:49:39 -07001061 break;
1062
1063 case EVENT_SET_NETWORK_SELECTION_MODE_MANUAL_DONE:
Pengquan Menge3d01e22018-09-20 15:25:35 -07001064 ar = (AsyncResult) msg.obj;
1065 request = (MainThreadRequest) ar.userObj;
1066 if (ar.exception == null) {
1067 request.result = true;
1068 } else {
1069 request.result = false;
1070 loge("setNetworkSelectionModeManual " + ar.exception);
1071 }
1072 notifyRequester(request);
1073 mApp.onNetworkSelectionChanged(request.subId);
Shishir Agrawal302c8692015-06-19 13:49:39 -07001074 break;
1075
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -07001076 case CMD_GET_MODEM_ACTIVITY_INFO:
1077 request = (MainThreadRequest) msg.obj;
1078 onCompleted = obtainMessage(EVENT_GET_MODEM_ACTIVITY_INFO_DONE, request);
James Mattisab947702019-04-03 14:18:34 -07001079 if (defaultPhone != null) {
1080 defaultPhone.getModemActivityInfo(onCompleted, request.workSource);
Shuo Qian8f4750a2020-02-20 17:12:10 -08001081 } else {
1082 ResultReceiver result = (ResultReceiver) request.argument;
1083 Bundle bundle = new Bundle();
1084 bundle.putParcelable(TelephonyManager.MODEM_ACTIVITY_RESULT_KEY,
1085 new ModemActivityInfo(0, 0, 0, new int[0], 0));
1086 result.send(0, bundle);
James Mattisab947702019-04-03 14:18:34 -07001087 }
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -07001088 break;
1089
1090 case EVENT_GET_MODEM_ACTIVITY_INFO_DONE:
1091 ar = (AsyncResult) msg.obj;
1092 request = (MainThreadRequest) ar.userObj;
Shuo Qian8f4750a2020-02-20 17:12:10 -08001093 ResultReceiver result = (ResultReceiver) request.argument;
1094
1095 ModemActivityInfo ret = new ModemActivityInfo(0, 0, 0, new int[0], 0);
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -07001096 if (ar.exception == null && ar.result != null) {
Shuo Qian8f4750a2020-02-20 17:12:10 -08001097 // Update the last modem activity info and the result of the request.
1098 ModemActivityInfo info = (ModemActivityInfo) ar.result;
1099 if (isModemActivityInfoValid(info)) {
1100 int[] mergedTxTimeMs = new int[ModemActivityInfo.TX_POWER_LEVELS];
1101 int[] txTimeMs = info.getTransmitTimeMillis();
1102 int[] lastModemTxTimeMs = mLastModemActivityInfo
1103 .getTransmitTimeMillis();
1104 for (int i = 0; i < mergedTxTimeMs.length; i++) {
1105 mergedTxTimeMs[i] = txTimeMs[i] + lastModemTxTimeMs[i];
1106 }
1107 mLastModemActivityInfo.setTimestamp(info.getTimestamp());
1108 mLastModemActivityInfo.setSleepTimeMillis(info.getSleepTimeMillis()
1109 + mLastModemActivityInfo.getSleepTimeMillis());
1110 mLastModemActivityInfo.setIdleTimeMillis(info.getIdleTimeMillis()
1111 + mLastModemActivityInfo.getIdleTimeMillis());
1112 mLastModemActivityInfo.setTransmitTimeMillis(mergedTxTimeMs);
1113 mLastModemActivityInfo.setReceiveTimeMillis(
1114 info.getReceiveTimeMillis()
1115 + mLastModemActivityInfo.getReceiveTimeMillis());
1116 }
1117 ret = new ModemActivityInfo(mLastModemActivityInfo.getTimestamp(),
1118 mLastModemActivityInfo.getSleepTimeMillis(),
1119 mLastModemActivityInfo.getIdleTimeMillis(),
1120 mLastModemActivityInfo.getTransmitTimeMillis(),
1121 mLastModemActivityInfo.getReceiveTimeMillis());
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -07001122 } else {
1123 if (ar.result == null) {
1124 loge("queryModemActivityInfo: Empty response");
1125 } else if (ar.exception instanceof CommandException) {
1126 loge("queryModemActivityInfo: CommandException: " +
1127 ar.exception);
1128 } else {
1129 loge("queryModemActivityInfo: Unknown exception");
1130 }
1131 }
Shuo Qian8f4750a2020-02-20 17:12:10 -08001132 Bundle bundle = new Bundle();
1133 bundle.putParcelable(TelephonyManager.MODEM_ACTIVITY_RESULT_KEY, ret);
1134 result.send(0, bundle);
Pengquan Menga1bb6272018-09-06 09:59:22 -07001135 notifyRequester(request);
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -07001136 break;
1137
Meng Wang1a7c35a2016-05-05 20:56:15 -07001138 case CMD_SET_ALLOWED_CARRIERS:
1139 request = (MainThreadRequest) msg.obj;
Michele Berionne482f8202018-11-27 18:57:59 -08001140 CarrierRestrictionRules argument =
1141 (CarrierRestrictionRules) request.argument;
Meng Wang1a7c35a2016-05-05 20:56:15 -07001142 onCompleted = obtainMessage(EVENT_SET_ALLOWED_CARRIERS_DONE, request);
Michele Berionne482f8202018-11-27 18:57:59 -08001143 defaultPhone.setAllowedCarriers(argument, onCompleted, request.workSource);
Meng Wang1a7c35a2016-05-05 20:56:15 -07001144 break;
1145
1146 case EVENT_SET_ALLOWED_CARRIERS_DONE:
1147 ar = (AsyncResult) msg.obj;
1148 request = (MainThreadRequest) ar.userObj;
1149 if (ar.exception == null && ar.result != null) {
1150 request.result = ar.result;
1151 } else {
Michele Berionne482f8202018-11-27 18:57:59 -08001152 request.result = TelephonyManager.SET_CARRIER_RESTRICTION_ERROR;
1153 if (ar.exception instanceof CommandException) {
1154 loge("setAllowedCarriers: CommandException: " + ar.exception);
1155 CommandException.Error error =
1156 ((CommandException) (ar.exception)).getCommandError();
1157 if (error == CommandException.Error.REQUEST_NOT_SUPPORTED) {
1158 request.result =
1159 TelephonyManager.SET_CARRIER_RESTRICTION_NOT_SUPPORTED;
1160 }
Meng Wang1a7c35a2016-05-05 20:56:15 -07001161 } else {
1162 loge("setAllowedCarriers: Unknown exception");
1163 }
1164 }
Pengquan Menga1bb6272018-09-06 09:59:22 -07001165 notifyRequester(request);
Meng Wang1a7c35a2016-05-05 20:56:15 -07001166 break;
1167
1168 case CMD_GET_ALLOWED_CARRIERS:
1169 request = (MainThreadRequest) msg.obj;
1170 onCompleted = obtainMessage(EVENT_GET_ALLOWED_CARRIERS_DONE, request);
Nazanin Bakhshif782e562018-12-11 15:15:39 -08001171 defaultPhone.getAllowedCarriers(onCompleted, request.workSource);
Meng Wang1a7c35a2016-05-05 20:56:15 -07001172 break;
1173
1174 case EVENT_GET_ALLOWED_CARRIERS_DONE:
1175 ar = (AsyncResult) msg.obj;
1176 request = (MainThreadRequest) ar.userObj;
1177 if (ar.exception == null && ar.result != null) {
1178 request.result = ar.result;
1179 } else {
Michele Berionne482f8202018-11-27 18:57:59 -08001180 request.result = new IllegalStateException(
1181 "Failed to get carrier restrictions");
Meng Wang1a7c35a2016-05-05 20:56:15 -07001182 if (ar.result == null) {
1183 loge("getAllowedCarriers: Empty response");
1184 } else if (ar.exception instanceof CommandException) {
1185 loge("getAllowedCarriers: CommandException: " +
1186 ar.exception);
1187 } else {
1188 loge("getAllowedCarriers: Unknown exception");
1189 }
1190 }
Pengquan Menga1bb6272018-09-06 09:59:22 -07001191 notifyRequester(request);
Meng Wang1a7c35a2016-05-05 20:56:15 -07001192 break;
1193
Nathan Haroldb3014052017-01-25 15:57:32 -08001194 case EVENT_GET_FORBIDDEN_PLMNS_DONE:
1195 ar = (AsyncResult) msg.obj;
1196 request = (MainThreadRequest) ar.userObj;
1197 if (ar.exception == null && ar.result != null) {
1198 request.result = ar.result;
1199 } else {
1200 request.result = new IllegalArgumentException(
1201 "Failed to retrieve Forbidden Plmns");
1202 if (ar.result == null) {
1203 loge("getForbiddenPlmns: Empty response");
1204 } else {
1205 loge("getForbiddenPlmns: Unknown exception");
1206 }
1207 }
Pengquan Menga1bb6272018-09-06 09:59:22 -07001208 notifyRequester(request);
Nathan Haroldb3014052017-01-25 15:57:32 -08001209 break;
1210
1211 case CMD_GET_FORBIDDEN_PLMNS:
1212 request = (MainThreadRequest) msg.obj;
1213 uiccCard = getUiccCardFromRequest(request);
1214 if (uiccCard == null) {
1215 loge("getForbiddenPlmns() UiccCard is null");
1216 request.result = new IllegalArgumentException(
1217 "getForbiddenPlmns() UiccCard is null");
Pengquan Menga1bb6272018-09-06 09:59:22 -07001218 notifyRequester(request);
Nathan Haroldb3014052017-01-25 15:57:32 -08001219 break;
1220 }
1221 Integer appType = (Integer) request.argument;
1222 UiccCardApplication uiccApp = uiccCard.getApplicationByType(appType);
1223 if (uiccApp == null) {
1224 loge("getForbiddenPlmns() no app with specified type -- "
1225 + appType);
1226 request.result = new IllegalArgumentException("Failed to get UICC App");
Pengquan Menga1bb6272018-09-06 09:59:22 -07001227 notifyRequester(request);
Nathan Haroldb3014052017-01-25 15:57:32 -08001228 break;
1229 } else {
1230 if (DBG) logv("getForbiddenPlmns() found app " + uiccApp.getAid()
1231 + " specified type -- " + appType);
1232 }
1233 onCompleted = obtainMessage(EVENT_GET_FORBIDDEN_PLMNS_DONE, request);
1234 ((SIMRecords) uiccApp.getIccRecords()).getForbiddenPlmns(
1235 onCompleted);
1236 break;
1237
Holly Jiuyu Sun01c47ad2018-01-24 17:56:33 +00001238 case CMD_SWITCH_SLOTS:
1239 request = (MainThreadRequest) msg.obj;
1240 int[] physicalSlots = (int[]) request.argument;
1241 onCompleted = obtainMessage(EVENT_SWITCH_SLOTS_DONE, request);
1242 UiccController.getInstance().switchSlots(physicalSlots, onCompleted);
1243 break;
1244
1245 case EVENT_SWITCH_SLOTS_DONE:
1246 ar = (AsyncResult) msg.obj;
1247 request = (MainThreadRequest) ar.userObj;
1248 request.result = (ar.exception == null);
Pengquan Menga1bb6272018-09-06 09:59:22 -07001249 notifyRequester(request);
1250 break;
1251 case CMD_GET_NETWORK_SELECTION_MODE:
1252 request = (MainThreadRequest) msg.obj;
1253 onCompleted = obtainMessage(EVENT_GET_NETWORK_SELECTION_MODE_DONE, request);
1254 getPhoneFromRequest(request).getNetworkSelectionMode(onCompleted);
1255 break;
1256
1257 case EVENT_GET_NETWORK_SELECTION_MODE_DONE:
1258 ar = (AsyncResult) msg.obj;
1259 request = (MainThreadRequest) ar.userObj;
1260 if (ar.exception != null) {
1261 request.result = TelephonyManager.NETWORK_SELECTION_MODE_UNKNOWN;
1262 } else {
1263 int mode = ((int[]) ar.result)[0];
1264 if (mode == 0) {
1265 request.result = TelephonyManager.NETWORK_SELECTION_MODE_AUTO;
1266 } else {
1267 request.result = TelephonyManager.NETWORK_SELECTION_MODE_MANUAL;
1268 }
Holly Jiuyu Sun01c47ad2018-01-24 17:56:33 +00001269 }
Pengquan Menga1bb6272018-09-06 09:59:22 -07001270 notifyRequester(request);
1271 break;
1272 case CMD_GET_CDMA_ROAMING_MODE:
1273 request = (MainThreadRequest) msg.obj;
1274 onCompleted = obtainMessage(EVENT_GET_CDMA_ROAMING_MODE_DONE, request);
1275 getPhoneFromRequest(request).queryCdmaRoamingPreference(onCompleted);
1276 break;
1277 case EVENT_GET_CDMA_ROAMING_MODE_DONE:
1278 ar = (AsyncResult) msg.obj;
1279 request = (MainThreadRequest) ar.userObj;
1280 if (ar.exception != null) {
1281 request.result = TelephonyManager.CDMA_ROAMING_MODE_RADIO_DEFAULT;
1282 } else {
1283 request.result = ((int[]) ar.result)[0];
1284 }
1285 notifyRequester(request);
1286 break;
1287 case CMD_SET_CDMA_ROAMING_MODE:
1288 request = (MainThreadRequest) msg.obj;
1289 onCompleted = obtainMessage(EVENT_SET_CDMA_ROAMING_MODE_DONE, request);
1290 int mode = (int) request.argument;
1291 getPhoneFromRequest(request).setCdmaRoamingPreference(mode, onCompleted);
1292 break;
1293 case EVENT_SET_CDMA_ROAMING_MODE_DONE:
1294 ar = (AsyncResult) msg.obj;
1295 request = (MainThreadRequest) ar.userObj;
1296 request.result = ar.exception == null;
1297 notifyRequester(request);
1298 break;
1299 case CMD_SET_CDMA_SUBSCRIPTION_MODE:
1300 request = (MainThreadRequest) msg.obj;
1301 onCompleted = obtainMessage(EVENT_SET_CDMA_SUBSCRIPTION_MODE_DONE, request);
1302 int subscriptionMode = (int) request.argument;
1303 getPhoneFromRequest(request).setCdmaSubscription(subscriptionMode, onCompleted);
1304 break;
1305 case EVENT_SET_CDMA_SUBSCRIPTION_MODE_DONE:
1306 ar = (AsyncResult) msg.obj;
1307 request = (MainThreadRequest) ar.userObj;
1308 request.result = ar.exception == null;
1309 notifyRequester(request);
Holly Jiuyu Sun01c47ad2018-01-24 17:56:33 +00001310 break;
Nathan Harold3ff88932018-08-14 10:19:49 -07001311 case CMD_GET_ALL_CELL_INFO:
1312 request = (MainThreadRequest) msg.obj;
Nathan Harold3ff88932018-08-14 10:19:49 -07001313 onCompleted = obtainMessage(EVENT_GET_ALL_CELL_INFO_DONE, request);
Nathan Harold92bed182018-10-12 18:16:49 -07001314 request.phone.requestCellInfoUpdate(request.workSource, onCompleted);
Nathan Harold3ff88932018-08-14 10:19:49 -07001315 break;
Nathan Harold3ff88932018-08-14 10:19:49 -07001316 case EVENT_GET_ALL_CELL_INFO_DONE:
1317 ar = (AsyncResult) msg.obj;
1318 request = (MainThreadRequest) ar.userObj;
Nathan Harold8d0f1742018-10-02 12:14:47 -07001319 // If a timeout occurs, the response will be null
1320 request.result = (ar.exception == null && ar.result != null)
1321 ? ar.result : new ArrayList<CellInfo>();
Nathan Harold3ff88932018-08-14 10:19:49 -07001322 synchronized (request) {
1323 request.notifyAll();
1324 }
1325 break;
Nathan Haroldfa8da0f2018-09-27 18:51:29 -07001326 case CMD_REQUEST_CELL_INFO_UPDATE:
1327 request = (MainThreadRequest) msg.obj;
1328 request.phone.requestCellInfoUpdate(request.workSource,
1329 obtainMessage(EVENT_REQUEST_CELL_INFO_UPDATE_DONE, request));
1330 break;
1331 case EVENT_REQUEST_CELL_INFO_UPDATE_DONE:
1332 ar = (AsyncResult) msg.obj;
1333 request = (MainThreadRequest) ar.userObj;
1334 ICellInfoCallback cb = (ICellInfoCallback) request.argument;
1335 try {
1336 if (ar.exception != null) {
Nathan Haroldfa8da0f2018-09-27 18:51:29 -07001337 Log.e(LOG_TAG, "Exception retrieving CellInfo=" + ar.exception);
Meng Wangd8921f42019-09-30 17:13:54 -07001338 cb.onError(
1339 TelephonyManager.CellInfoCallback.ERROR_MODEM_ERROR,
1340 ar.exception.getClass().getName(),
1341 ar.exception.toString());
Nathan Haroldfa8da0f2018-09-27 18:51:29 -07001342 } else if (ar.result == null) {
Nathan Haroldfa8da0f2018-09-27 18:51:29 -07001343 Log.w(LOG_TAG, "Timeout Waiting for CellInfo!");
Meng Wangd8921f42019-09-30 17:13:54 -07001344 cb.onError(TelephonyManager.CellInfoCallback.ERROR_TIMEOUT, null, null);
Nathan Haroldfa8da0f2018-09-27 18:51:29 -07001345 } else {
1346 // use the result as returned
1347 cb.onCellInfo((List<CellInfo>) ar.result);
1348 }
1349 } catch (RemoteException re) {
1350 Log.w(LOG_TAG, "Discarded CellInfo due to Callback RemoteException");
1351 }
1352 break;
1353 case CMD_GET_CELL_LOCATION:
Nathan Harold3ff88932018-08-14 10:19:49 -07001354 request = (MainThreadRequest) msg.obj;
1355 WorkSource ws = (WorkSource) request.argument;
1356 Phone phone = getPhoneFromRequest(request);
Meng Wanga10e89e2019-12-09 13:13:01 -08001357 phone.getCellIdentity(ws, obtainMessage(EVENT_GET_CELL_LOCATION_DONE, request));
Nathan Harold3ff88932018-08-14 10:19:49 -07001358 break;
Nathan Haroldfa8da0f2018-09-27 18:51:29 -07001359 case EVENT_GET_CELL_LOCATION_DONE:
Nathan Harold3ff88932018-08-14 10:19:49 -07001360 ar = (AsyncResult) msg.obj;
1361 request = (MainThreadRequest) ar.userObj;
1362 if (ar.exception == null) {
1363 request.result = ar.result;
1364 } else {
Nathan Haroldfa8da0f2018-09-27 18:51:29 -07001365 phone = getPhoneFromRequest(request);
Nathan Harold3ff88932018-08-14 10:19:49 -07001366 request.result = (phone.getPhoneType() == PhoneConstants.PHONE_TYPE_CDMA)
Meng Wanga10e89e2019-12-09 13:13:01 -08001367 ? new CellIdentityCdma() : new CellIdentityGsm();
Nathan Harold3ff88932018-08-14 10:19:49 -07001368 }
1369
1370 synchronized (request) {
1371 request.notifyAll();
1372 }
1373 break;
chen xu6dac5ab2018-10-26 17:39:23 -07001374 case CMD_MODEM_REBOOT:
1375 request = (MainThreadRequest) msg.obj;
1376 onCompleted = obtainMessage(EVENT_RESET_MODEM_CONFIG_DONE, request);
Nazanin Bakhshif782e562018-12-11 15:15:39 -08001377 defaultPhone.rebootModem(onCompleted);
chen xu6dac5ab2018-10-26 17:39:23 -07001378 break;
chen xu6dac5ab2018-10-26 17:39:23 -07001379 case EVENT_CMD_MODEM_REBOOT_DONE:
1380 handleNullReturnEvent(msg, "rebootModem");
1381 break;
Malcolm Chen8e4ed912019-01-15 20:22:16 -08001382 case CMD_REQUEST_ENABLE_MODEM:
1383 request = (MainThreadRequest) msg.obj;
1384 boolean enable = (boolean) request.argument;
1385 onCompleted = obtainMessage(EVENT_ENABLE_MODEM_DONE, request);
Nazanin Bakhshi33d584b2019-02-27 10:44:32 -08001386 onCompleted.arg1 = enable ? 1 : 0;
Malcolm Chen8e4ed912019-01-15 20:22:16 -08001387 PhoneConfigurationManager.getInstance()
1388 .enablePhone(request.phone, enable, onCompleted);
1389 break;
1390 case EVENT_ENABLE_MODEM_DONE:
1391 ar = (AsyncResult) msg.obj;
1392 request = (MainThreadRequest) ar.userObj;
1393 request.result = (ar.exception == null);
Nazanin Bakhshif71371d2019-04-29 17:29:44 -07001394 int phoneId = request.phone.getPhoneId();
Nazanin Bakhshi33d584b2019-02-27 10:44:32 -08001395 //update the cache as modem status has changed
Nazanin Bakhshif71371d2019-04-29 17:29:44 -07001396 if ((boolean) request.result) {
1397 mPhoneConfigurationManager.addToPhoneStatusCache(phoneId, msg.arg1 == 1);
1398 updateModemStateMetrics();
1399 } else {
1400 Log.e(LOG_TAG, msg.what + " failure. Not updating modem status."
1401 + ar.exception);
1402 }
1403 notifyRequester(request);
1404 break;
1405 case CMD_GET_MODEM_STATUS:
1406 request = (MainThreadRequest) msg.obj;
1407 onCompleted = obtainMessage(EVENT_GET_MODEM_STATUS_DONE, request);
1408 PhoneConfigurationManager.getInstance()
1409 .getPhoneStatusFromModem(request.phone, onCompleted);
1410 break;
1411 case EVENT_GET_MODEM_STATUS_DONE:
1412 ar = (AsyncResult) msg.obj;
1413 request = (MainThreadRequest) ar.userObj;
1414 int id = request.phone.getPhoneId();
1415 if (ar.exception == null && ar.result != null) {
1416 request.result = ar.result;
1417 //update the cache as modem status has changed
1418 mPhoneConfigurationManager.addToPhoneStatusCache(id,
1419 (boolean) request.result);
1420 } else {
1421 // Return true if modem status cannot be retrieved. For most cases,
1422 // modem status is on. And for older version modems, GET_MODEM_STATUS
1423 // and disable modem are not supported. Modem is always on.
1424 // TODO: this should be fixed in R to support a third
1425 // status UNKNOWN b/131631629
1426 request.result = true;
1427 Log.e(LOG_TAG, msg.what + " failure. Not updating modem status."
1428 + ar.exception);
1429 }
Malcolm Chen8e4ed912019-01-15 20:22:16 -08001430 notifyRequester(request);
1431 break;
Hall Liu73f5d362020-01-20 13:42:00 -08001432 case CMD_SET_SYSTEM_SELECTION_CHANNELS: {
1433 request = (MainThreadRequest) msg.obj;
1434 onCompleted = obtainMessage(EVENT_SET_SYSTEM_SELECTION_CHANNELS_DONE, request);
1435 Pair<List<RadioAccessSpecifier>, Consumer<Boolean>> args =
1436 (Pair<List<RadioAccessSpecifier>, Consumer<Boolean>>) request.argument;
1437 request.phone.setSystemSelectionChannels(args.first, onCompleted);
1438 break;
1439 }
1440 case EVENT_SET_SYSTEM_SELECTION_CHANNELS_DONE: {
1441 ar = (AsyncResult) msg.obj;
1442 request = (MainThreadRequest) ar.userObj;
1443 Pair<List<RadioAccessSpecifier>, Consumer<Boolean>> args =
1444 (Pair<List<RadioAccessSpecifier>, Consumer<Boolean>>) request.argument;
1445 args.second.accept(ar.exception == null);
1446 notifyRequester(request);
1447 break;
1448 }
yincheng zhao2737e882019-09-06 17:06:54 -07001449 case EVENT_SET_FORBIDDEN_PLMNS_DONE:
1450 ar = (AsyncResult) msg.obj;
1451 request = (MainThreadRequest) ar.userObj;
1452 if (ar.exception == null && ar.result != null) {
1453 request.result = ar.result;
1454 } else {
1455 request.result = -1;
1456 loge("Failed to set Forbidden Plmns");
1457 if (ar.result == null) {
1458 loge("setForbidenPlmns: Empty response");
1459 } else if (ar.exception != null) {
1460 loge("setForbiddenPlmns: Exception: " + ar.exception);
1461 request.result = -1;
1462 } else {
1463 loge("setForbiddenPlmns: Unknown exception");
1464 }
1465 }
1466 notifyRequester(request);
1467 break;
1468 case CMD_SET_FORBIDDEN_PLMNS:
1469 request = (MainThreadRequest) msg.obj;
1470 uiccCard = getUiccCardFromRequest(request);
1471 if (uiccCard == null) {
1472 loge("setForbiddenPlmns: UiccCard is null");
1473 request.result = -1;
1474 notifyRequester(request);
1475 break;
1476 }
1477 Pair<Integer, List<String>> setFplmnsArgs =
1478 (Pair<Integer, List<String>>) request.argument;
1479 appType = setFplmnsArgs.first;
1480 List<String> fplmns = setFplmnsArgs.second;
1481 uiccApp = uiccCard.getApplicationByType(appType);
1482 if (uiccApp == null) {
1483 loge("setForbiddenPlmns: no app with specified type -- " + appType);
1484 request.result = -1;
1485 loge("Failed to get UICC App");
1486 notifyRequester(request);
1487 } else {
1488 onCompleted = obtainMessage(EVENT_SET_FORBIDDEN_PLMNS_DONE, request);
1489 ((SIMRecords) uiccApp.getIccRecords())
1490 .setForbiddenPlmns(onCompleted, fplmns);
1491 }
yinchengzhao4d163c02019-12-12 15:21:47 -08001492 break;
Naina Nallurid63128d2019-09-17 14:10:30 -07001493 case CMD_ERASE_MODEM_CONFIG:
1494 request = (MainThreadRequest) msg.obj;
1495 onCompleted = obtainMessage(EVENT_ERASE_MODEM_CONFIG_DONE, request);
1496 defaultPhone.eraseModemConfig(onCompleted);
1497 break;
1498 case EVENT_ERASE_MODEM_CONFIG_DONE:
1499 handleNullReturnEvent(msg, "eraseModemConfig");
yincheng zhao2737e882019-09-06 17:06:54 -07001500 break;
zoey chene02881a2019-12-30 16:11:23 +08001501
1502 case CMD_CHANGE_ICC_LOCK_PASSWORD:
1503 request = (MainThreadRequest) msg.obj;
1504 onCompleted = obtainMessage(EVENT_CHANGE_ICC_LOCK_PASSWORD_DONE, request);
1505 Pair<String, String> changed = (Pair<String, String>) request.argument;
1506 getPhoneFromRequest(request).getIccCard().changeIccLockPassword(
1507 changed.first, changed.second, onCompleted);
1508 break;
1509 case EVENT_CHANGE_ICC_LOCK_PASSWORD_DONE:
1510 ar = (AsyncResult) msg.obj;
1511 request = (MainThreadRequest) ar.userObj;
1512 if (ar.exception == null) {
1513 request.result = TelephonyManager.CHANGE_ICC_LOCK_SUCCESS;
1514 } else {
1515 request.result = msg.arg1;
1516 }
1517 notifyRequester(request);
1518 break;
1519
1520 case CMD_SET_ICC_LOCK_ENABLED:
1521 request = (MainThreadRequest) msg.obj;
1522 onCompleted = obtainMessage(EVENT_SET_ICC_LOCK_ENABLED_DONE, request);
1523 Pair<Boolean, String> enabled = (Pair<Boolean, String>) request.argument;
1524 getPhoneFromRequest(request).getIccCard().setIccLockEnabled(
1525 enabled.first, enabled.second, onCompleted);
1526 break;
1527 case EVENT_SET_ICC_LOCK_ENABLED_DONE:
1528 ar = (AsyncResult) msg.obj;
1529 request = (MainThreadRequest) ar.userObj;
1530 if (ar.exception == null) {
1531 request.result = TelephonyManager.CHANGE_ICC_LOCK_SUCCESS;
1532 } else {
1533 request.result = msg.arg1;
1534 }
1535 notifyRequester(request);
1536 break;
1537
Peter Wangdafb9ac2020-01-15 14:13:38 -08001538 case MSG_NOTIFY_USER_ACTIVITY:
1539 removeMessages(MSG_NOTIFY_USER_ACTIVITY);
Peter Wang59571be2020-01-27 12:35:15 +08001540 Intent intent = new Intent(TelephonyIntents.ACTION_USER_ACTIVITY_NOTIFICATION);
Peter Wangdafb9ac2020-01-15 14:13:38 -08001541 intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY);
1542 getDefaultPhone().getContext().sendBroadcastAsUser(
1543 intent, UserHandle.ALL, permission.USER_ACTIVITY);
1544 break;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001545 default:
1546 Log.w(LOG_TAG, "MainThreadHandler: unexpected message code: " + msg.what);
1547 break;
1548 }
1549 }
Jake Hambye994d462014-02-03 13:10:13 -08001550
Pengquan Menga1bb6272018-09-06 09:59:22 -07001551 private void notifyRequester(MainThreadRequest request) {
1552 synchronized (request) {
1553 request.notifyAll();
1554 }
1555 }
1556
Jake Hambye994d462014-02-03 13:10:13 -08001557 private void handleNullReturnEvent(Message msg, String command) {
1558 AsyncResult ar = (AsyncResult) msg.obj;
1559 MainThreadRequest request = (MainThreadRequest) ar.userObj;
1560 if (ar.exception == null) {
1561 request.result = true;
1562 } else {
1563 request.result = false;
1564 if (ar.exception instanceof CommandException) {
1565 loge(command + ": CommandException: " + ar.exception);
1566 } else {
1567 loge(command + ": Unknown exception");
1568 }
1569 }
Pengquan Menga1bb6272018-09-06 09:59:22 -07001570 notifyRequester(request);
Jake Hambye994d462014-02-03 13:10:13 -08001571 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001572 }
1573
1574 /**
1575 * Posts the specified command to be executed on the main thread,
1576 * waits for the request to complete, and returns the result.
1577 * @see #sendRequestAsync
1578 */
1579 private Object sendRequest(int command, Object argument) {
Nathan Harold92bed182018-10-12 18:16:49 -07001580 return sendRequest(
1581 command, argument, SubscriptionManager.INVALID_SUBSCRIPTION_ID, null, null);
vagdeviaf9a5b92018-08-15 16:01:53 -07001582 }
1583
1584 /**
1585 * Posts the specified command to be executed on the main thread,
1586 * waits for the request to complete, and returns the result.
1587 * @see #sendRequestAsync
1588 */
1589 private Object sendRequest(int command, Object argument, WorkSource workSource) {
1590 return sendRequest(command, argument, SubscriptionManager.INVALID_SUBSCRIPTION_ID,
Nathan Harold92bed182018-10-12 18:16:49 -07001591 null, workSource);
Wink Saville36469e72014-06-11 15:17:00 -07001592 }
1593
1594 /**
1595 * Posts the specified command to be executed on the main thread,
1596 * waits for the request to complete, and returns the result.
1597 * @see #sendRequestAsync
1598 */
Shishir Agrawal76d5da92014-11-09 16:17:25 -08001599 private Object sendRequest(int command, Object argument, Integer subId) {
Nathan Harold92bed182018-10-12 18:16:49 -07001600 return sendRequest(command, argument, subId, null, null);
vagdeviaf9a5b92018-08-15 16:01:53 -07001601 }
1602
1603 /**
1604 * Posts the specified command to be executed on the main thread,
1605 * waits for the request to complete, and returns the result.
1606 * @see #sendRequestAsync
1607 */
Nathan Harold92bed182018-10-12 18:16:49 -07001608 private Object sendRequest(int command, Object argument, int subId, WorkSource workSource) {
1609 return sendRequest(command, argument, subId, null, workSource);
1610 }
1611
1612 /**
1613 * Posts the specified command to be executed on the main thread,
1614 * waits for the request to complete, and returns the result.
1615 * @see #sendRequestAsync
1616 */
1617 private Object sendRequest(int command, Object argument, Phone phone, WorkSource workSource) {
1618 return sendRequest(
1619 command, argument, SubscriptionManager.INVALID_SUBSCRIPTION_ID, phone, workSource);
1620 }
1621
1622 /**
1623 * Posts the specified command to be executed on the main thread,
1624 * waits for the request to complete, and returns the result.
1625 * @see #sendRequestAsync
1626 */
1627 private Object sendRequest(
1628 int command, Object argument, Integer subId, Phone phone, WorkSource workSource) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001629 if (Looper.myLooper() == mMainThreadHandler.getLooper()) {
1630 throw new RuntimeException("This method will deadlock if called from the main thread.");
1631 }
1632
Nathan Harold92bed182018-10-12 18:16:49 -07001633 MainThreadRequest request = null;
1634 if (subId != SubscriptionManager.INVALID_SUBSCRIPTION_ID && phone != null) {
1635 throw new IllegalArgumentException("subId and phone cannot both be specified!");
1636 } else if (phone != null) {
1637 request = new MainThreadRequest(argument, phone, workSource);
1638 } else {
1639 request = new MainThreadRequest(argument, subId, workSource);
1640 }
1641
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001642 Message msg = mMainThreadHandler.obtainMessage(command, request);
1643 msg.sendToTarget();
1644
1645 // Wait for the request to complete
1646 synchronized (request) {
1647 while (request.result == null) {
1648 try {
1649 request.wait();
1650 } catch (InterruptedException e) {
1651 // Do nothing, go back and wait until the request is complete
1652 }
1653 }
1654 }
1655 return request.result;
1656 }
1657
1658 /**
1659 * Asynchronous ("fire and forget") version of sendRequest():
1660 * Posts the specified command to be executed on the main thread, and
1661 * returns immediately.
1662 * @see #sendRequest
1663 */
1664 private void sendRequestAsync(int command) {
1665 mMainThreadHandler.sendEmptyMessage(command);
1666 }
1667
1668 /**
Sailesh Nepalbd76e4e2013-10-27 13:59:44 -07001669 * Same as {@link #sendRequestAsync(int)} except it takes an argument.
Nathan Haroldfa8da0f2018-09-27 18:51:29 -07001670 * @see {@link #sendRequest(int)}
Sailesh Nepalbd76e4e2013-10-27 13:59:44 -07001671 */
1672 private void sendRequestAsync(int command, Object argument) {
Nathan Haroldfa8da0f2018-09-27 18:51:29 -07001673 sendRequestAsync(command, argument, null, null);
1674 }
1675
1676 /**
1677 * Same as {@link #sendRequestAsync(int,Object)} except it takes a Phone and WorkSource.
1678 * @see {@link #sendRequest(int,Object)}
1679 */
1680 private void sendRequestAsync(
1681 int command, Object argument, Phone phone, WorkSource workSource) {
1682 MainThreadRequest request = new MainThreadRequest(argument, phone, workSource);
Sailesh Nepalbd76e4e2013-10-27 13:59:44 -07001683 Message msg = mMainThreadHandler.obtainMessage(command, request);
1684 msg.sendToTarget();
1685 }
1686
1687 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001688 * Initialize the singleton PhoneInterfaceManager instance.
1689 * This is only done once, at startup, from PhoneApp.onCreate().
1690 */
Nazanin Bakhshif782e562018-12-11 15:15:39 -08001691 /* package */ static PhoneInterfaceManager init(PhoneGlobals app) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001692 synchronized (PhoneInterfaceManager.class) {
1693 if (sInstance == null) {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08001694 sInstance = new PhoneInterfaceManager(app);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001695 } else {
1696 Log.wtf(LOG_TAG, "init() called multiple times! sInstance = " + sInstance);
1697 }
1698 return sInstance;
1699 }
1700 }
1701
1702 /** Private constructor; @see init() */
Jordan Liu1979a042020-03-20 21:39:35 +00001703 private PhoneInterfaceManager(PhoneGlobals app) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001704 mApp = app;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001705 mCM = PhoneGlobals.getInstance().mCM;
Brad Ebinger24c29992019-12-05 13:03:21 -08001706 mImsResolver = PhoneGlobals.getInstance().getImsResolver();
Stuart Scott981d8582015-04-21 14:09:50 -07001707 mUserManager = (UserManager) app.getSystemService(Context.USER_SERVICE);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001708 mAppOps = (AppOpsManager)app.getSystemService(Context.APP_OPS_SERVICE);
1709 mMainThreadHandler = new MainThreadHandler();
Tobias Thiererb19e1f12018-12-11 17:54:03 +00001710 mSubscriptionController = SubscriptionController.getInstance();
Nazanin Bakhshif782e562018-12-11 15:15:39 -08001711 mTelephonySharedPreferences =
1712 PreferenceManager.getDefaultSharedPreferences(mApp);
yinxub1bed742017-04-17 11:45:04 -07001713 mNetworkScanRequestTracker = new NetworkScanRequestTracker();
Malcolm Chen2c63d402018-08-14 16:00:53 -07001714 mPhoneConfigurationManager = PhoneConfigurationManager.getInstance();
Peter Wanga3cf4ac2020-01-27 09:39:46 +08001715 mNotifyUserActivity = new AtomicBoolean(false);
Wink Saville3ab207e2014-11-20 13:07:20 -08001716
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001717 publish();
1718 }
1719
Nazanin Bakhshif782e562018-12-11 15:15:39 -08001720 private Phone getDefaultPhone() {
1721 Phone thePhone = getPhone(getDefaultSubscription());
1722 return (thePhone != null) ? thePhone : PhoneFactory.getDefaultPhone();
1723 }
1724
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001725 private void publish() {
1726 if (DBG) log("publish: " + this);
1727
Peter Wangc035ce42020-01-08 21:00:22 -08001728 TelephonyFrameworkInitializer
1729 .getTelephonyServiceManager()
1730 .getTelephonyServiceRegisterer()
1731 .register(this);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001732 }
1733
Stuart Scott584921c2015-01-15 17:10:34 -08001734 private Phone getPhoneFromRequest(MainThreadRequest request) {
Jordan Liu4c733742019-02-28 12:03:40 -08001735 if (request.phone != null) {
1736 return request.phone;
1737 } else {
1738 return getPhoneFromSubId(request.subId);
1739 }
1740 }
1741
1742 private Phone getPhoneFromSubId(int subId) {
1743 return (subId == SubscriptionManager.INVALID_SUBSCRIPTION_ID)
1744 ? getDefaultPhone() : getPhone(subId);
Stuart Scott584921c2015-01-15 17:10:34 -08001745 }
1746
Shishir Agrawalc04d9752016-02-19 10:41:00 -08001747 private UiccCard getUiccCardFromRequest(MainThreadRequest request) {
1748 Phone phone = getPhoneFromRequest(request);
1749 return phone == null ? null :
1750 UiccController.getInstance().getUiccCard(phone.getPhoneId());
1751 }
1752
Wink Saville36469e72014-06-11 15:17:00 -07001753 // returns phone associated with the subId.
Wink Savilleb564aae2014-10-23 10:18:09 -07001754 private Phone getPhone(int subId) {
Wink Savilleac1bdfd2014-11-20 23:04:44 -08001755 return PhoneFactory.getPhone(mSubscriptionController.getPhoneId(subId));
Wink Saville36469e72014-06-11 15:17:00 -07001756 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001757
Naina Nallurid63128d2019-09-17 14:10:30 -07001758 private void sendEraseModemConfig(Phone phone) {
1759 if (phone != null) {
1760 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
1761 mApp, phone.getSubId(), "eraseModemConfig");
1762 final long identity = Binder.clearCallingIdentity();
1763 try {
1764 Boolean success = (Boolean) sendRequest(CMD_ERASE_MODEM_CONFIG, null);
1765 if (DBG) log("eraseModemConfig:" + ' ' + (success ? "ok" : "fail"));
1766 } finally {
1767 Binder.restoreCallingIdentity(identity);
1768 }
1769 }
1770 }
1771
Peter Wang44b186e2020-01-13 23:33:09 -08001772 private boolean isImsAvailableOnDevice() {
1773 PackageManager pm = getDefaultPhone().getContext().getPackageManager();
1774 if (pm == null) {
1775 // For some reason package manger is not available.. This will fail internally anyway,
1776 // so do not throw error and allow.
1777 return true;
1778 }
1779 return pm.hasSystemFeature(PackageManager.FEATURE_TELEPHONY_IMS, 0);
1780 }
1781
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001782 public void dial(String number) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001783 dialForSubscriber(getPreferredVoiceSubscription(), number);
Wink Saville36469e72014-06-11 15:17:00 -07001784 }
1785
Wink Savilleb564aae2014-10-23 10:18:09 -07001786 public void dialForSubscriber(int subId, String number) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001787 if (DBG) log("dial: " + number);
1788 // No permission check needed here: This is just a wrapper around the
1789 // ACTION_DIAL intent, which is available to any app since it puts up
1790 // the UI before it does anything.
1791
Malcolm Chenaa4a8532018-02-28 15:00:40 -08001792 final long identity = Binder.clearCallingIdentity();
1793 try {
1794 String url = createTelUrl(number);
1795 if (url == null) {
1796 return;
1797 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001798
Malcolm Chenaa4a8532018-02-28 15:00:40 -08001799 // PENDING: should we just silently fail if phone is offhook or ringing?
1800 PhoneConstants.State state = mCM.getState(subId);
1801 if (state != PhoneConstants.State.OFFHOOK && state != PhoneConstants.State.RINGING) {
1802 Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse(url));
1803 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
1804 mApp.startActivity(intent);
1805 }
1806 } finally {
1807 Binder.restoreCallingIdentity(identity);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001808 }
1809 }
1810
1811 public void call(String callingPackage, String number) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001812 callForSubscriber(getPreferredVoiceSubscription(), callingPackage, number);
Wink Saville36469e72014-06-11 15:17:00 -07001813 }
1814
Wink Savilleb564aae2014-10-23 10:18:09 -07001815 public void callForSubscriber(int subId, String callingPackage, String number) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001816 if (DBG) log("call: " + number);
1817
1818 // This is just a wrapper around the ACTION_CALL intent, but we still
1819 // need to do a permission check since we're calling startActivity()
1820 // from the context of the phone app.
1821 enforceCallPermission();
1822
Jordan Liu1617b712019-07-10 15:06:26 -07001823 if (mAppOps.noteOp(AppOpsManager.OPSTR_CALL_PHONE, Binder.getCallingUid(), callingPackage)
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001824 != AppOpsManager.MODE_ALLOWED) {
1825 return;
1826 }
1827
Malcolm Chenaa4a8532018-02-28 15:00:40 -08001828 final long identity = Binder.clearCallingIdentity();
1829 try {
1830 String url = createTelUrl(number);
1831 if (url == null) {
1832 return;
1833 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001834
Malcolm Chenaa4a8532018-02-28 15:00:40 -08001835 boolean isValid = false;
1836 final List<SubscriptionInfo> slist = getActiveSubscriptionInfoListPrivileged();
1837 if (slist != null) {
1838 for (SubscriptionInfo subInfoRecord : slist) {
1839 if (subInfoRecord.getSubscriptionId() == subId) {
1840 isValid = true;
1841 break;
1842 }
Wink Saville3ab207e2014-11-20 13:07:20 -08001843 }
Wink Saville08874612014-08-31 19:19:58 -07001844 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08001845 if (!isValid) {
1846 return;
1847 }
Wink Saville08874612014-08-31 19:19:58 -07001848
Malcolm Chenaa4a8532018-02-28 15:00:40 -08001849 Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse(url));
1850 intent.putExtra(SUBSCRIPTION_KEY, subId);
1851 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
1852 mApp.startActivity(intent);
1853 } finally {
1854 Binder.restoreCallingIdentity(identity);
1855 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001856 }
1857
Wink Savilleb564aae2014-10-23 10:18:09 -07001858 public boolean supplyPinForSubscriber(int subId, String pin) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001859 int [] resultArray = supplyPinReportResultForSubscriber(subId, pin);
Wink Saville9de0f752013-10-22 19:04:03 -07001860 return (resultArray[0] == PhoneConstants.PIN_RESULT_SUCCESS) ? true : false;
1861 }
1862
Wink Savilleb564aae2014-10-23 10:18:09 -07001863 public boolean supplyPukForSubscriber(int subId, String puk, String pin) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001864 int [] resultArray = supplyPukReportResultForSubscriber(subId, puk, pin);
Wink Saville9de0f752013-10-22 19:04:03 -07001865 return (resultArray[0] == PhoneConstants.PIN_RESULT_SUCCESS) ? true : false;
1866 }
1867
Wink Savilleb564aae2014-10-23 10:18:09 -07001868 public int[] supplyPinReportResultForSubscriber(int subId, String pin) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001869 enforceModifyPermission();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08001870
1871 final long identity = Binder.clearCallingIdentity();
1872 try {
1873 final UnlockSim checkSimPin = new UnlockSim(getPhone(subId).getIccCard());
1874 checkSimPin.start();
1875 return checkSimPin.unlockSim(null, pin);
1876 } finally {
1877 Binder.restoreCallingIdentity(identity);
1878 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001879 }
1880
Wink Savilleb564aae2014-10-23 10:18:09 -07001881 public int[] supplyPukReportResultForSubscriber(int subId, String puk, String pin) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001882 enforceModifyPermission();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08001883
1884 final long identity = Binder.clearCallingIdentity();
1885 try {
1886 final UnlockSim checkSimPuk = new UnlockSim(getPhone(subId).getIccCard());
1887 checkSimPuk.start();
1888 return checkSimPuk.unlockSim(puk, pin);
1889 } finally {
1890 Binder.restoreCallingIdentity(identity);
1891 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001892 }
1893
1894 /**
Wink Saville9de0f752013-10-22 19:04:03 -07001895 * Helper thread to turn async call to SimCard#supplyPin into
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001896 * a synchronous one.
1897 */
1898 private static class UnlockSim extends Thread {
1899
1900 private final IccCard mSimCard;
1901
1902 private boolean mDone = false;
Wink Saville9de0f752013-10-22 19:04:03 -07001903 private int mResult = PhoneConstants.PIN_GENERAL_FAILURE;
1904 private int mRetryCount = -1;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001905
1906 // For replies from SimCard interface
1907 private Handler mHandler;
1908
1909 // For async handler to identify request type
1910 private static final int SUPPLY_PIN_COMPLETE = 100;
1911
1912 public UnlockSim(IccCard simCard) {
1913 mSimCard = simCard;
1914 }
1915
1916 @Override
1917 public void run() {
1918 Looper.prepare();
1919 synchronized (UnlockSim.this) {
1920 mHandler = new Handler() {
1921 @Override
1922 public void handleMessage(Message msg) {
1923 AsyncResult ar = (AsyncResult) msg.obj;
1924 switch (msg.what) {
1925 case SUPPLY_PIN_COMPLETE:
1926 Log.d(LOG_TAG, "SUPPLY_PIN_COMPLETE");
1927 synchronized (UnlockSim.this) {
Wink Saville9de0f752013-10-22 19:04:03 -07001928 mRetryCount = msg.arg1;
1929 if (ar.exception != null) {
1930 if (ar.exception instanceof CommandException &&
1931 ((CommandException)(ar.exception)).getCommandError()
1932 == CommandException.Error.PASSWORD_INCORRECT) {
1933 mResult = PhoneConstants.PIN_PASSWORD_INCORRECT;
vivi.lib5e9ada2019-09-12 16:04:24 +08001934 } //When UiccCardApp dispose,handle message and return exception
1935 else if (ar.exception instanceof CommandException &&
1936 ((CommandException) (ar.exception)).getCommandError()
1937 == CommandException.Error.ABORTED) {
1938 mResult = PhoneConstants.PIN_OPERATION_ABORTED;
Wink Saville9de0f752013-10-22 19:04:03 -07001939 } else {
1940 mResult = PhoneConstants.PIN_GENERAL_FAILURE;
1941 }
1942 } else {
1943 mResult = PhoneConstants.PIN_RESULT_SUCCESS;
1944 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001945 mDone = true;
1946 UnlockSim.this.notifyAll();
1947 }
1948 break;
1949 }
1950 }
1951 };
1952 UnlockSim.this.notifyAll();
1953 }
1954 Looper.loop();
1955 }
1956
1957 /*
1958 * Use PIN or PUK to unlock SIM card
1959 *
1960 * If PUK is null, unlock SIM card with PIN
1961 *
1962 * If PUK is not null, unlock SIM card with PUK and set PIN code
1963 */
Wink Saville9de0f752013-10-22 19:04:03 -07001964 synchronized int[] unlockSim(String puk, String pin) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001965
1966 while (mHandler == null) {
1967 try {
1968 wait();
1969 } catch (InterruptedException e) {
1970 Thread.currentThread().interrupt();
1971 }
1972 }
1973 Message callback = Message.obtain(mHandler, SUPPLY_PIN_COMPLETE);
1974
1975 if (puk == null) {
1976 mSimCard.supplyPin(pin, callback);
1977 } else {
1978 mSimCard.supplyPuk(puk, pin, callback);
1979 }
1980
1981 while (!mDone) {
1982 try {
1983 Log.d(LOG_TAG, "wait for done");
1984 wait();
1985 } catch (InterruptedException e) {
1986 // Restore the interrupted status
1987 Thread.currentThread().interrupt();
1988 }
1989 }
1990 Log.d(LOG_TAG, "done");
Wink Saville9de0f752013-10-22 19:04:03 -07001991 int[] resultArray = new int[2];
1992 resultArray[0] = mResult;
1993 resultArray[1] = mRetryCount;
1994 return resultArray;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001995 }
1996 }
1997
Nathan Harold7c8d0f12020-05-28 20:40:31 -07001998 /**
1999 * This method has been removed due to privacy and stability concerns.
2000 */
2001 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002002 public void updateServiceLocation() {
Nathan Harold7c8d0f12020-05-28 20:40:31 -07002003 Log.e(LOG_TAG, "Call to unsupported method updateServiceLocation()");
2004 return;
Wink Saville36469e72014-06-11 15:17:00 -07002005 }
2006
Nathan Harold1f889d82020-06-04 17:05:26 -07002007 @Override
2008 public void updateServiceLocationWithPackageName(String callingPackage) {
2009 mApp.getSystemService(AppOpsManager.class)
2010 .checkPackage(Binder.getCallingUid(), callingPackage);
2011
2012 final int targetSdk = getTargetSdk(callingPackage);
2013 if (targetSdk > android.os.Build.VERSION_CODES.R) {
2014 // Callers targeting S have no business invoking this method.
2015 return;
2016 }
2017
2018 LocationAccessPolicy.LocationPermissionResult locationResult =
2019 LocationAccessPolicy.checkLocationPermission(mApp,
2020 new LocationAccessPolicy.LocationPermissionQuery.Builder()
2021 .setCallingPackage(callingPackage)
2022 .setCallingFeatureId(null)
2023 .setCallingPid(Binder.getCallingPid())
2024 .setCallingUid(Binder.getCallingUid())
2025 .setMethod("updateServiceLocation")
2026 .setMinSdkVersionForCoarse(Build.VERSION_CODES.BASE)
2027 .setMinSdkVersionForFine(Build.VERSION_CODES.Q)
2028 .build());
2029 // Apps that lack location permission have no business calling this method;
2030 // however, because no permission was declared in the public API, denials must
2031 // all be "soft".
2032 switch (locationResult) {
2033 case DENIED_HARD: /* fall through */
2034 case DENIED_SOFT:
2035 return;
2036 }
2037
2038 WorkSource workSource = getWorkSource(Binder.getCallingUid());
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002039 final long identity = Binder.clearCallingIdentity();
2040 try {
Nathan Harold1f889d82020-06-04 17:05:26 -07002041 final Phone phone = getPhone(getDefaultSubscription());
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002042 if (phone != null) {
Nathan Harold1f889d82020-06-04 17:05:26 -07002043 phone.updateServiceLocation(workSource);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002044 }
2045 } finally {
2046 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07002047 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002048 }
2049
Philip P. Moltmann700a9592019-10-03 11:53:50 -07002050 @Deprecated
Robert Greenwalt36b23af2015-07-06 17:59:14 -07002051 @Override
2052 public boolean isRadioOn(String callingPackage) {
Philip P. Moltmann700a9592019-10-03 11:53:50 -07002053 return isRadioOnWithFeature(callingPackage, null);
2054 }
2055
2056
2057 @Override
2058 public boolean isRadioOnWithFeature(String callingPackage, String callingFeatureId) {
2059 return isRadioOnForSubscriberWithFeature(getDefaultSubscription(), callingPackage,
2060 callingFeatureId);
2061 }
2062
2063 @Deprecated
2064 @Override
2065 public boolean isRadioOnForSubscriber(int subId, String callingPackage) {
2066 return isRadioOnForSubscriberWithFeature(subId, callingPackage, null);
Wink Saville36469e72014-06-11 15:17:00 -07002067 }
2068
Robert Greenwalt36b23af2015-07-06 17:59:14 -07002069 @Override
Philip P. Moltmann700a9592019-10-03 11:53:50 -07002070 public boolean isRadioOnForSubscriberWithFeature(int subId, String callingPackage,
2071 String callingFeatureId) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08002072 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
Philip P. Moltmann700a9592019-10-03 11:53:50 -07002073 mApp, subId, callingPackage, callingFeatureId, "isRadioOnForSubscriber")) {
Robert Greenwalt36b23af2015-07-06 17:59:14 -07002074 return false;
2075 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002076
2077 final long identity = Binder.clearCallingIdentity();
2078 try {
2079 return isRadioOnForSubscriber(subId);
2080 } finally {
2081 Binder.restoreCallingIdentity(identity);
2082 }
Robert Greenwalt36b23af2015-07-06 17:59:14 -07002083 }
2084
2085 private boolean isRadioOnForSubscriber(int subId) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002086 final long identity = Binder.clearCallingIdentity();
2087 try {
2088 final Phone phone = getPhone(subId);
2089 if (phone != null) {
2090 return phone.getServiceState().getState() != ServiceState.STATE_POWER_OFF;
2091 } else {
2092 return false;
2093 }
2094 } finally {
2095 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07002096 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002097 }
2098
2099 public void toggleRadioOnOff() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07002100 toggleRadioOnOffForSubscriber(getDefaultSubscription());
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002101 }
Wink Saville36469e72014-06-11 15:17:00 -07002102
Wink Savilleb564aae2014-10-23 10:18:09 -07002103 public void toggleRadioOnOffForSubscriber(int subId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002104 enforceModifyPermission();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002105
2106 final long identity = Binder.clearCallingIdentity();
2107 try {
2108 final Phone phone = getPhone(subId);
2109 if (phone != null) {
2110 phone.setRadioPower(!isRadioOnForSubscriber(subId));
2111 }
2112 } finally {
2113 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07002114 }
Wink Saville36469e72014-06-11 15:17:00 -07002115 }
2116
2117 public boolean setRadio(boolean turnOn) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07002118 return setRadioForSubscriber(getDefaultSubscription(), turnOn);
Wink Saville36469e72014-06-11 15:17:00 -07002119 }
2120
Wink Savilleb564aae2014-10-23 10:18:09 -07002121 public boolean setRadioForSubscriber(int subId, boolean turnOn) {
Wink Saville36469e72014-06-11 15:17:00 -07002122 enforceModifyPermission();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002123
2124 final long identity = Binder.clearCallingIdentity();
2125 try {
2126 final Phone phone = getPhone(subId);
2127 if (phone == null) {
2128 return false;
2129 }
2130 if ((phone.getServiceState().getState() != ServiceState.STATE_POWER_OFF) != turnOn) {
2131 toggleRadioOnOffForSubscriber(subId);
2132 }
2133 return true;
2134 } finally {
2135 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07002136 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002137 }
Wink Saville36469e72014-06-11 15:17:00 -07002138
Naveen Kalla1fd79bd2014-08-08 00:48:59 -07002139 public boolean needMobileRadioShutdown() {
Shuo Qianfa7b6b32019-12-10 10:40:38 -08002140 enforceReadPrivilegedPermission("needMobileRadioShutdown");
Naveen Kalla1fd79bd2014-08-08 00:48:59 -07002141 /*
2142 * If any of the Radios are available, it will need to be
2143 * shutdown. So return true if any Radio is available.
2144 */
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002145 final long identity = Binder.clearCallingIdentity();
2146 try {
2147 for (int i = 0; i < TelephonyManager.getDefault().getPhoneCount(); i++) {
2148 Phone phone = PhoneFactory.getPhone(i);
2149 if (phone != null && phone.isRadioAvailable()) return true;
2150 }
2151 logv(TelephonyManager.getDefault().getPhoneCount() + " Phones are shutdown.");
2152 return false;
2153 } finally {
2154 Binder.restoreCallingIdentity(identity);
Naveen Kalla1fd79bd2014-08-08 00:48:59 -07002155 }
Naveen Kalla1fd79bd2014-08-08 00:48:59 -07002156 }
2157
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002158 @Override
Naveen Kalla1fd79bd2014-08-08 00:48:59 -07002159 public void shutdownMobileRadios() {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002160 enforceModifyPermission();
2161
2162 final long identity = Binder.clearCallingIdentity();
2163 try {
2164 for (int i = 0; i < TelephonyManager.getDefault().getPhoneCount(); i++) {
2165 logv("Shutting down Phone " + i);
2166 shutdownRadioUsingPhoneId(i);
2167 }
2168 } finally {
2169 Binder.restoreCallingIdentity(identity);
Naveen Kalla1fd79bd2014-08-08 00:48:59 -07002170 }
2171 }
2172
2173 private void shutdownRadioUsingPhoneId(int phoneId) {
Naveen Kalla1fd79bd2014-08-08 00:48:59 -07002174 Phone phone = PhoneFactory.getPhone(phoneId);
2175 if (phone != null && phone.isRadioAvailable()) {
2176 phone.shutdownRadio();
2177 }
2178 }
2179
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002180 public boolean setRadioPower(boolean turnOn) {
Jack Yub4e16162017-05-15 12:48:40 -07002181 enforceModifyPermission();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002182
2183 final long identity = Binder.clearCallingIdentity();
2184 try {
2185 final Phone defaultPhone = PhoneFactory.getDefaultPhone();
2186 if (defaultPhone != null) {
2187 defaultPhone.setRadioPower(turnOn);
2188 return true;
2189 } else {
2190 loge("There's no default phone.");
2191 return false;
2192 }
2193 } finally {
2194 Binder.restoreCallingIdentity(identity);
Wei Liu9ae2a062016-08-08 11:09:34 -07002195 }
Wink Saville36469e72014-06-11 15:17:00 -07002196 }
2197
Wink Savilleb564aae2014-10-23 10:18:09 -07002198 public boolean setRadioPowerForSubscriber(int subId, boolean turnOn) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002199 enforceModifyPermission();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002200
2201 final long identity = Binder.clearCallingIdentity();
2202 try {
2203 final Phone phone = getPhone(subId);
2204 if (phone != null) {
2205 phone.setRadioPower(turnOn);
2206 return true;
2207 } else {
2208 return false;
2209 }
2210 } finally {
2211 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07002212 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002213 }
2214
Wink Saville36469e72014-06-11 15:17:00 -07002215 // FIXME: subId version needed
Sanket Padawe356d7632015-06-22 14:03:32 -07002216 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002217 public boolean enableDataConnectivity() {
2218 enforceModifyPermission();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002219
2220 final long identity = Binder.clearCallingIdentity();
2221 try {
2222 int subId = mSubscriptionController.getDefaultDataSubId();
2223 final Phone phone = getPhone(subId);
2224 if (phone != null) {
Sooraj Sasindranaf1b5132020-05-05 21:06:20 +00002225 phone.getDataEnabledSettings().setDataEnabled(
2226 TelephonyManager.DATA_ENABLED_REASON_USER, true);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002227 return true;
2228 } else {
2229 return false;
2230 }
2231 } finally {
2232 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07002233 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002234 }
2235
Wink Saville36469e72014-06-11 15:17:00 -07002236 // FIXME: subId version needed
Sanket Padawe356d7632015-06-22 14:03:32 -07002237 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002238 public boolean disableDataConnectivity() {
2239 enforceModifyPermission();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002240
2241 final long identity = Binder.clearCallingIdentity();
2242 try {
2243 int subId = mSubscriptionController.getDefaultDataSubId();
2244 final Phone phone = getPhone(subId);
2245 if (phone != null) {
Sooraj Sasindranaf1b5132020-05-05 21:06:20 +00002246 phone.getDataEnabledSettings().setDataEnabled(
2247 TelephonyManager.DATA_ENABLED_REASON_USER, false);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002248 return true;
2249 } else {
2250 return false;
2251 }
2252 } finally {
2253 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07002254 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002255 }
2256
Sanket Padawe356d7632015-06-22 14:03:32 -07002257 @Override
Jack Yuacf8a132017-05-01 17:00:48 -07002258 public boolean isDataConnectivityPossible(int subId) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002259 final long identity = Binder.clearCallingIdentity();
2260 try {
2261 final Phone phone = getPhone(subId);
2262 if (phone != null) {
Jack Yub5d8f642018-11-26 11:20:48 -08002263 return phone.isDataAllowed(ApnSetting.TYPE_DEFAULT);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002264 } else {
2265 return false;
2266 }
2267 } finally {
2268 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07002269 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002270 }
2271
2272 public boolean handlePinMmi(String dialString) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07002273 return handlePinMmiForSubscriber(getDefaultSubscription(), dialString);
Wink Saville36469e72014-06-11 15:17:00 -07002274 }
2275
pkanwarae03a6b2016-11-06 20:37:09 -08002276 public void handleUssdRequest(int subId, String ussdRequest, ResultReceiver wrappedCallback) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002277 enforceCallPermission();
2278
2279 final long identity = Binder.clearCallingIdentity();
2280 try {
2281 if (!SubscriptionManager.isValidSubscriptionId(subId)) {
2282 return;
2283 }
2284 Pair<String, ResultReceiver> ussdObject = new Pair(ussdRequest, wrappedCallback);
2285 sendRequest(CMD_HANDLE_USSD_REQUEST, ussdObject, subId);
2286 } finally {
2287 Binder.restoreCallingIdentity(identity);
2288 }
pkanwar32d516d2016-10-14 19:37:38 -07002289 };
2290
Wink Savilleb564aae2014-10-23 10:18:09 -07002291 public boolean handlePinMmiForSubscriber(int subId, String dialString) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002292 enforceModifyPermission();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002293
2294 final long identity = Binder.clearCallingIdentity();
2295 try {
2296 if (!SubscriptionManager.isValidSubscriptionId(subId)) {
2297 return false;
2298 }
2299 return (Boolean) sendRequest(CMD_HANDLE_PIN_MMI, dialString, subId);
2300 } finally {
2301 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07002302 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002303 }
2304
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002305 public int getCallState() {
Shishir Agrawala9f32182016-04-12 12:00:16 -07002306 return getCallStateForSlot(getSlotForDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07002307 }
2308
Sanket Padawe13bac7b2017-03-20 15:04:47 -07002309 public int getCallStateForSlot(int slotIndex) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002310 final long identity = Binder.clearCallingIdentity();
2311 try {
2312 Phone phone = PhoneFactory.getPhone(slotIndex);
2313 return phone == null ? TelephonyManager.CALL_STATE_IDLE :
2314 PhoneConstantConversions.convertCallState(phone.getState());
2315 } finally {
2316 Binder.restoreCallingIdentity(identity);
2317 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002318 }
2319
Sanket Padawe356d7632015-06-22 14:03:32 -07002320 @Override
Nathan Harolde037c472019-06-26 00:41:07 +00002321 public int getDataState() {
Nathan Haroldc4689b12019-06-14 16:58:30 -07002322 return getDataStateForSubId(mSubscriptionController.getDefaultDataSubId());
2323 }
2324
2325 @Override
2326 public int getDataStateForSubId(int subId) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002327 final long identity = Binder.clearCallingIdentity();
2328 try {
Nathan Haroldc4689b12019-06-14 16:58:30 -07002329 final Phone phone = getPhone(subId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002330 if (phone != null) {
2331 return PhoneConstantConversions.convertDataState(phone.getDataConnectionState());
2332 } else {
2333 return PhoneConstantConversions.convertDataState(
2334 PhoneConstants.DataState.DISCONNECTED);
2335 }
2336 } finally {
2337 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07002338 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002339 }
2340
Sanket Padawe356d7632015-06-22 14:03:32 -07002341 @Override
Nathan Harolde037c472019-06-26 00:41:07 +00002342 public int getDataActivity() {
Nathan Haroldc4689b12019-06-14 16:58:30 -07002343 return getDataActivityForSubId(mSubscriptionController.getDefaultDataSubId());
2344 }
2345
2346 @Override
2347 public int getDataActivityForSubId(int subId) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002348 final long identity = Binder.clearCallingIdentity();
2349 try {
Nathan Haroldc4689b12019-06-14 16:58:30 -07002350 final Phone phone = getPhone(subId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002351 if (phone != null) {
2352 return DefaultPhoneNotifier.convertDataActivityState(phone.getDataActivityState());
2353 } else {
2354 return TelephonyManager.DATA_ACTIVITY_NONE;
2355 }
2356 } finally {
2357 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07002358 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002359 }
2360
2361 @Override
Meng Wanga10e89e2019-12-09 13:13:01 -08002362 public CellIdentity getCellLocation(String callingPackage, String callingFeatureId) {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08002363 mApp.getSystemService(AppOpsManager.class)
Hall Liu1aa510f2017-11-22 17:40:08 -08002364 .checkPackage(Binder.getCallingUid(), callingPackage);
Hall Liuf19c44f2018-11-27 14:38:17 -08002365
2366 LocationAccessPolicy.LocationPermissionResult locationResult =
2367 LocationAccessPolicy.checkLocationPermission(mApp,
2368 new LocationAccessPolicy.LocationPermissionQuery.Builder()
2369 .setCallingPackage(callingPackage)
Philip P. Moltmann3a2772a2019-10-04 08:15:00 -07002370 .setCallingFeatureId(callingFeatureId)
Hall Liuf19c44f2018-11-27 14:38:17 -08002371 .setCallingPid(Binder.getCallingPid())
2372 .setCallingUid(Binder.getCallingUid())
2373 .setMethod("getCellLocation")
Hall Liu773ba022020-01-24 18:07:12 -08002374 .setMinSdkVersionForCoarse(Build.VERSION_CODES.BASE)
Hall Liuf19c44f2018-11-27 14:38:17 -08002375 .setMinSdkVersionForFine(Build.VERSION_CODES.Q)
2376 .build());
2377 switch (locationResult) {
2378 case DENIED_HARD:
2379 throw new SecurityException("Not allowed to access cell location");
2380 case DENIED_SOFT:
Meng Wanga10e89e2019-12-09 13:13:01 -08002381 return (getDefaultPhone().getPhoneType() == PhoneConstants.PHONE_TYPE_CDMA)
2382 ? new CellIdentityCdma() : new CellIdentityGsm();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002383 }
2384
Narayan Kamathf04b5a12018-01-09 11:47:15 +00002385 WorkSource workSource = getWorkSource(Binder.getCallingUid());
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002386 final long identity = Binder.clearCallingIdentity();
2387 try {
2388 if (DBG_LOC) log("getCellLocation: is active user");
Nathan Harold3ff88932018-08-14 10:19:49 -07002389 int subId = mSubscriptionController.getDefaultDataSubId();
Meng Wanga10e89e2019-12-09 13:13:01 -08002390 return (CellIdentity) sendRequest(CMD_GET_CELL_LOCATION, workSource, subId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002391 } finally {
2392 Binder.restoreCallingIdentity(identity);
2393 }
Svetoslav64fad262015-04-14 14:35:21 -07002394 }
2395
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002396 @Override
Jack Yueb1e7fe2020-02-22 19:38:58 -08002397 public String getNetworkCountryIsoForPhone(int phoneId) {
Jonathan Basseribf5362b2017-07-19 12:22:35 -07002398 // Reporting the correct network country is ambiguous when IWLAN could conflict with
2399 // registered cell info, so return a NULL country instead.
2400 final long identity = Binder.clearCallingIdentity();
2401 try {
Malcolm Chen3732c2b2018-07-18 20:15:24 -07002402 if (phoneId == SubscriptionManager.INVALID_PHONE_INDEX) {
2403 // Get default phone in this case.
2404 phoneId = SubscriptionManager.DEFAULT_PHONE_INDEX;
2405 }
Jonathan Basseribf5362b2017-07-19 12:22:35 -07002406 final int subId = mSubscriptionController.getSubIdUsingPhoneId(phoneId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002407 Phone phone = PhoneFactory.getPhone(phoneId);
Nathan Harold532f51c2020-04-21 19:31:10 -07002408 if (phone == null) return "";
2409 ServiceStateTracker sst = phone.getServiceStateTracker();
2410 if (sst == null) return "";
2411 LocaleTracker lt = sst.getLocaleTracker();
2412 if (lt == null) return "";
2413 if (!TextUtils.isEmpty(lt.getCurrentCountry())) return lt.getCurrentCountry();
2414 EmergencyNumberTracker ent = phone.getEmergencyNumberTracker();
2415 return (ent == null) ? "" : ent.getEmergencyCountryIso();
Jonathan Basseribf5362b2017-07-19 12:22:35 -07002416 } finally {
2417 Binder.restoreCallingIdentity(identity);
2418 }
Jonathan Basseribf5362b2017-07-19 12:22:35 -07002419 }
2420
Nathan Harold7c8d0f12020-05-28 20:40:31 -07002421 /**
2422 * This method was removed due to potential issues caused by performing partial
2423 * updates of service state, and lack of a credible use case.
2424 *
2425 * This has the ability to break the telephony implementation by disabling notification of
2426 * changes in device connectivity. DO NOT USE THIS!
2427 */
Jonathan Basseribf5362b2017-07-19 12:22:35 -07002428 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002429 public void enableLocationUpdates() {
2430 mApp.enforceCallingOrSelfPermission(
2431 android.Manifest.permission.CONTROL_LOCATION_UPDATES, null);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002432 }
2433
Nathan Harold7c8d0f12020-05-28 20:40:31 -07002434 /**
2435 * This method was removed due to potential issues caused by performing partial
2436 * updates of service state, and lack of a credible use case.
2437 *
2438 * This has the ability to break the telephony implementation by disabling notification of
2439 * changes in device connectivity. DO NOT USE THIS!
2440 */
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002441 @Override
2442 public void disableLocationUpdates() {
2443 mApp.enforceCallingOrSelfPermission(
2444 android.Manifest.permission.CONTROL_LOCATION_UPDATES, null);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002445 }
2446
Nathan Harold31d7ff32018-10-15 20:20:30 -07002447 /**
2448 * Returns the target SDK version number for a given package name.
2449 *
Nathan Haroldec184742019-07-10 17:04:16 -07002450 * This call MUST be invoked before clearing the calling UID.
2451 *
Nathan Harold31d7ff32018-10-15 20:20:30 -07002452 * @return target SDK if the package is found or INT_MAX.
2453 */
2454 private int getTargetSdk(String packageName) {
2455 try {
Nathan Haroldec184742019-07-10 17:04:16 -07002456 final ApplicationInfo ai = mApp.getPackageManager().getApplicationInfoAsUser(
Chen Xu0150f0e2019-07-30 15:12:06 -07002457 packageName, 0, UserHandle.getUserHandleForUid(Binder.getCallingUid()));
Nathan Harold31d7ff32018-10-15 20:20:30 -07002458 if (ai != null) return ai.targetSdkVersion;
2459 } catch (PackageManager.NameNotFoundException unexpected) {
Nathan Haroldec184742019-07-10 17:04:16 -07002460 loge("Failed to get package info for pkg="
2461 + packageName + ", uid=" + Binder.getCallingUid());
Nathan Harold31d7ff32018-10-15 20:20:30 -07002462 }
2463 return Integer.MAX_VALUE;
2464 }
2465
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002466 @Override
2467 @SuppressWarnings("unchecked")
Philip P. Moltmann3a2772a2019-10-04 08:15:00 -07002468 public List<NeighboringCellInfo> getNeighboringCellInfo(String callingPackage,
2469 String callingFeatureId) {
Nathan Harold31d7ff32018-10-15 20:20:30 -07002470 final int targetSdk = getTargetSdk(callingPackage);
Nathan Harolddbea45a2018-08-30 14:35:07 -07002471 if (targetSdk >= android.os.Build.VERSION_CODES.Q) {
2472 throw new SecurityException(
2473 "getNeighboringCellInfo() is unavailable to callers targeting Q+ SDK levels.");
2474 }
Nathan Haroldb4d55612018-07-20 13:13:08 -07002475
Jordan Liu1617b712019-07-10 15:06:26 -07002476 if (mAppOps.noteOp(AppOpsManager.OPSTR_NEIGHBORING_CELLS, Binder.getCallingUid(),
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002477 callingPackage) != AppOpsManager.MODE_ALLOWED) {
2478 return null;
2479 }
Svetoslav64fad262015-04-14 14:35:21 -07002480
Svetoslav Ganov4a9d4482017-06-20 19:53:35 -07002481 if (DBG_LOC) log("getNeighboringCellInfo: is active user");
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002482
Philip P. Moltmann3a2772a2019-10-04 08:15:00 -07002483 List<CellInfo> info = getAllCellInfo(callingPackage, callingFeatureId);
Nathan Haroldf180aac2018-06-01 18:43:55 -07002484 if (info == null) return null;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002485
Nathan Haroldf180aac2018-06-01 18:43:55 -07002486 List<NeighboringCellInfo> neighbors = new ArrayList<NeighboringCellInfo>();
2487 for (CellInfo ci : info) {
2488 if (ci instanceof CellInfoGsm) {
2489 neighbors.add(new NeighboringCellInfo((CellInfoGsm) ci));
2490 } else if (ci instanceof CellInfoWcdma) {
2491 neighbors.add(new NeighboringCellInfo((CellInfoWcdma) ci));
2492 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002493 }
Nathan Haroldf180aac2018-06-01 18:43:55 -07002494 return (neighbors.size()) > 0 ? neighbors : null;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002495 }
2496
Nathan Haroldfa8da0f2018-09-27 18:51:29 -07002497 private List<CellInfo> getCachedCellInfo() {
2498 List<CellInfo> cellInfos = new ArrayList<CellInfo>();
2499 for (Phone phone : PhoneFactory.getPhones()) {
2500 List<CellInfo> info = phone.getAllCellInfo();
2501 if (info != null) cellInfos.addAll(info);
2502 }
2503 return cellInfos;
2504 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002505
2506 @Override
Philip P. Moltmann3a2772a2019-10-04 08:15:00 -07002507 public List<CellInfo> getAllCellInfo(String callingPackage, String callingFeatureId) {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08002508 mApp.getSystemService(AppOpsManager.class)
Hall Liu1aa510f2017-11-22 17:40:08 -08002509 .checkPackage(Binder.getCallingUid(), callingPackage);
Hall Liuf19c44f2018-11-27 14:38:17 -08002510
2511 LocationAccessPolicy.LocationPermissionResult locationResult =
2512 LocationAccessPolicy.checkLocationPermission(mApp,
2513 new LocationAccessPolicy.LocationPermissionQuery.Builder()
2514 .setCallingPackage(callingPackage)
Philip P. Moltmann3a2772a2019-10-04 08:15:00 -07002515 .setCallingFeatureId(callingFeatureId)
Hall Liuf19c44f2018-11-27 14:38:17 -08002516 .setCallingPid(Binder.getCallingPid())
2517 .setCallingUid(Binder.getCallingUid())
2518 .setMethod("getAllCellInfo")
Nathan Harold5ae50b52019-02-20 15:46:36 -08002519 .setMinSdkVersionForCoarse(Build.VERSION_CODES.BASE)
Hall Liuf19c44f2018-11-27 14:38:17 -08002520 .setMinSdkVersionForFine(Build.VERSION_CODES.Q)
2521 .build());
2522 switch (locationResult) {
2523 case DENIED_HARD:
2524 throw new SecurityException("Not allowed to access cell info");
2525 case DENIED_SOFT:
2526 return new ArrayList<>();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002527 }
2528
Nathan Haroldfa8da0f2018-09-27 18:51:29 -07002529 final int targetSdk = getTargetSdk(callingPackage);
2530 if (targetSdk >= android.os.Build.VERSION_CODES.Q) {
2531 return getCachedCellInfo();
2532 }
2533
Svetoslav Ganov4a9d4482017-06-20 19:53:35 -07002534 if (DBG_LOC) log("getAllCellInfo: is active user");
Narayan Kamathf04b5a12018-01-09 11:47:15 +00002535 WorkSource workSource = getWorkSource(Binder.getCallingUid());
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002536 final long identity = Binder.clearCallingIdentity();
2537 try {
2538 List<CellInfo> cellInfos = new ArrayList<CellInfo>();
2539 for (Phone phone : PhoneFactory.getPhones()) {
Nathan Harold3ff88932018-08-14 10:19:49 -07002540 final List<CellInfo> info = (List<CellInfo>) sendRequest(
Nathan Harold92bed182018-10-12 18:16:49 -07002541 CMD_GET_ALL_CELL_INFO, null, phone, workSource);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002542 if (info != null) cellInfos.addAll(info);
2543 }
2544 return cellInfos;
2545 } finally {
2546 Binder.restoreCallingIdentity(identity);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002547 }
2548 }
2549
Sailesh Nepalbd76e4e2013-10-27 13:59:44 -07002550 @Override
Philip P. Moltmann3a2772a2019-10-04 08:15:00 -07002551 public void requestCellInfoUpdate(int subId, ICellInfoCallback cb, String callingPackage,
2552 String callingFeatureId) {
2553 requestCellInfoUpdateInternal(subId, cb, callingPackage, callingFeatureId,
2554 getWorkSource(Binder.getCallingUid()));
Nathan Haroldfa8da0f2018-09-27 18:51:29 -07002555 }
2556
2557 @Override
Philip P. Moltmann3a2772a2019-10-04 08:15:00 -07002558 public void requestCellInfoUpdateWithWorkSource(int subId, ICellInfoCallback cb,
2559 String callingPackage, String callingFeatureId, WorkSource workSource) {
Nathan Haroldfa8da0f2018-09-27 18:51:29 -07002560 enforceModifyPermission();
Philip P. Moltmann3a2772a2019-10-04 08:15:00 -07002561 requestCellInfoUpdateInternal(subId, cb, callingPackage, callingFeatureId, workSource);
Nathan Haroldfa8da0f2018-09-27 18:51:29 -07002562 }
2563
Philip P. Moltmann3a2772a2019-10-04 08:15:00 -07002564 private void requestCellInfoUpdateInternal(int subId, ICellInfoCallback cb,
2565 String callingPackage, String callingFeatureId, WorkSource workSource) {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08002566 mApp.getSystemService(AppOpsManager.class)
Nathan Haroldfa8da0f2018-09-27 18:51:29 -07002567 .checkPackage(Binder.getCallingUid(), callingPackage);
Hall Liuf19c44f2018-11-27 14:38:17 -08002568
2569 LocationAccessPolicy.LocationPermissionResult locationResult =
2570 LocationAccessPolicy.checkLocationPermission(mApp,
2571 new LocationAccessPolicy.LocationPermissionQuery.Builder()
2572 .setCallingPackage(callingPackage)
Philip P. Moltmann3a2772a2019-10-04 08:15:00 -07002573 .setCallingFeatureId(callingFeatureId)
Hall Liuf19c44f2018-11-27 14:38:17 -08002574 .setCallingPid(Binder.getCallingPid())
2575 .setCallingUid(Binder.getCallingUid())
2576 .setMethod("requestCellInfoUpdate")
Hall Liud60acc92020-05-21 17:09:35 -07002577 .setMinSdkVersionForCoarse(Build.VERSION_CODES.BASE)
2578 .setMinSdkVersionForFine(Build.VERSION_CODES.BASE)
Hall Liuf19c44f2018-11-27 14:38:17 -08002579 .build());
2580 switch (locationResult) {
2581 case DENIED_HARD:
Hall Liud60acc92020-05-21 17:09:35 -07002582 if (getTargetSdk(callingPackage) < Build.VERSION_CODES.Q) {
2583 // Safetynet logging for b/154934934
2584 EventLog.writeEvent(0x534e4554, "154934934", Binder.getCallingUid());
2585 }
Hall Liuf19c44f2018-11-27 14:38:17 -08002586 throw new SecurityException("Not allowed to access cell info");
2587 case DENIED_SOFT:
Hall Liud60acc92020-05-21 17:09:35 -07002588 if (getTargetSdk(callingPackage) < Build.VERSION_CODES.Q) {
2589 // Safetynet logging for b/154934934
2590 EventLog.writeEvent(0x534e4554, "154934934", Binder.getCallingUid());
2591 }
Nathan Harold5320c422019-05-09 10:26:08 -07002592 try {
2593 cb.onCellInfo(new ArrayList<CellInfo>());
2594 } catch (RemoteException re) {
2595 // Drop without consequences
2596 }
Hall Liuf19c44f2018-11-27 14:38:17 -08002597 return;
Nathan Haroldfa8da0f2018-09-27 18:51:29 -07002598 }
2599
Nathan Harolda939a962019-05-09 10:13:47 -07002600
2601 final Phone phone = getPhoneFromSubId(subId);
Nathan Haroldfa8da0f2018-09-27 18:51:29 -07002602 if (phone == null) throw new IllegalArgumentException("Invalid Subscription Id: " + subId);
2603
2604 sendRequestAsync(CMD_REQUEST_CELL_INFO_UPDATE, cb, phone, workSource);
2605 }
2606
2607 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002608 public void setCellInfoListRate(int rateInMillis) {
Jack Yua8d8cb82017-01-16 10:15:34 -08002609 enforceModifyPermission();
Narayan Kamathf04b5a12018-01-09 11:47:15 +00002610 WorkSource workSource = getWorkSource(Binder.getCallingUid());
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002611
2612 final long identity = Binder.clearCallingIdentity();
2613 try {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08002614 getDefaultPhone().setCellInfoListRate(rateInMillis, workSource);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002615 } finally {
2616 Binder.restoreCallingIdentity(identity);
2617 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002618 }
2619
Shishir Agrawala9f32182016-04-12 12:00:16 -07002620 @Override
Philip P. Moltmann700a9592019-10-03 11:53:50 -07002621 public String getImeiForSlot(int slotIndex, String callingPackage, String callingFeatureId) {
Jeff Davidson913390f2018-02-23 17:11:49 -08002622 Phone phone = PhoneFactory.getPhone(slotIndex);
2623 if (phone == null) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08002624 return null;
2625 }
Jeff Davidson913390f2018-02-23 17:11:49 -08002626 int subId = phone.getSubId();
Michael Groover70af6dc2018-10-01 16:23:15 -07002627 if (!TelephonyPermissions.checkCallingOrSelfReadDeviceIdentifiers(mApp, subId,
Philip P. Moltmann700a9592019-10-03 11:53:50 -07002628 callingPackage, callingFeatureId, "getImeiForSlot")) {
Jeff Davidson913390f2018-02-23 17:11:49 -08002629 return null;
2630 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002631
2632 final long identity = Binder.clearCallingIdentity();
2633 try {
2634 return phone.getImei();
2635 } finally {
2636 Binder.restoreCallingIdentity(identity);
2637 }
Shishir Agrawala9f32182016-04-12 12:00:16 -07002638 }
2639
2640 @Override
David Kelly5e06a7f2018-03-12 14:10:59 +00002641 public String getTypeAllocationCodeForSlot(int slotIndex) {
2642 Phone phone = PhoneFactory.getPhone(slotIndex);
2643 String tac = null;
2644 if (phone != null) {
2645 String imei = phone.getImei();
2646 tac = imei == null ? null : imei.substring(0, TYPE_ALLOCATION_CODE_LENGTH);
2647 }
2648 return tac;
2649 }
2650
2651 @Override
Philip P. Moltmann700a9592019-10-03 11:53:50 -07002652 public String getMeidForSlot(int slotIndex, String callingPackage, String callingFeatureId) {
Jeff Davidson913390f2018-02-23 17:11:49 -08002653 Phone phone = PhoneFactory.getPhone(slotIndex);
2654 if (phone == null) {
Jack Yu2af8d712017-03-15 17:14:14 -07002655 return null;
2656 }
Nazanin Bakhshif782e562018-12-11 15:15:39 -08002657
Jeff Davidson913390f2018-02-23 17:11:49 -08002658 int subId = phone.getSubId();
Michael Groover70af6dc2018-10-01 16:23:15 -07002659 if (!TelephonyPermissions.checkCallingOrSelfReadDeviceIdentifiers(mApp, subId,
Philip P. Moltmann700a9592019-10-03 11:53:50 -07002660 callingPackage, callingFeatureId, "getMeidForSlot")) {
Jeff Davidson913390f2018-02-23 17:11:49 -08002661 return null;
2662 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002663
2664 final long identity = Binder.clearCallingIdentity();
2665 try {
2666 return phone.getMeid();
2667 } finally {
2668 Binder.restoreCallingIdentity(identity);
2669 }
Jack Yu2af8d712017-03-15 17:14:14 -07002670 }
2671
2672 @Override
David Kelly5e06a7f2018-03-12 14:10:59 +00002673 public String getManufacturerCodeForSlot(int slotIndex) {
2674 Phone phone = PhoneFactory.getPhone(slotIndex);
2675 String manufacturerCode = null;
2676 if (phone != null) {
2677 String meid = phone.getMeid();
2678 manufacturerCode = meid == null ? null : meid.substring(0, MANUFACTURER_CODE_LENGTH);
2679 }
2680 return manufacturerCode;
2681 }
2682
2683 @Override
Philip P. Moltmann700a9592019-10-03 11:53:50 -07002684 public String getDeviceSoftwareVersionForSlot(int slotIndex, String callingPackage,
2685 String callingFeatureId) {
Jeff Davidson913390f2018-02-23 17:11:49 -08002686 Phone phone = PhoneFactory.getPhone(slotIndex);
2687 if (phone == null) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08002688 return null;
2689 }
Jeff Davidson913390f2018-02-23 17:11:49 -08002690 int subId = phone.getSubId();
2691 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
Philip P. Moltmann700a9592019-10-03 11:53:50 -07002692 mApp, subId, callingPackage, callingFeatureId,
2693 "getDeviceSoftwareVersionForSlot")) {
Jeff Davidson913390f2018-02-23 17:11:49 -08002694 return null;
2695 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002696
2697 final long identity = Binder.clearCallingIdentity();
2698 try {
2699 return phone.getDeviceSvn();
2700 } finally {
2701 Binder.restoreCallingIdentity(identity);
2702 }
Shishir Agrawala9f32182016-04-12 12:00:16 -07002703 }
2704
fionaxu43304da2017-11-27 22:51:16 -08002705 @Override
2706 public int getSubscriptionCarrierId(int subId) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002707 final long identity = Binder.clearCallingIdentity();
2708 try {
2709 final Phone phone = getPhone(subId);
2710 return phone == null ? TelephonyManager.UNKNOWN_CARRIER_ID : phone.getCarrierId();
2711 } finally {
2712 Binder.restoreCallingIdentity(identity);
2713 }
fionaxu43304da2017-11-27 22:51:16 -08002714 }
2715
2716 @Override
2717 public String getSubscriptionCarrierName(int subId) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002718 final long identity = Binder.clearCallingIdentity();
2719 try {
2720 final Phone phone = getPhone(subId);
2721 return phone == null ? null : phone.getCarrierName();
2722 } finally {
2723 Binder.restoreCallingIdentity(identity);
2724 }
fionaxu43304da2017-11-27 22:51:16 -08002725 }
2726
calvinpanffe225e2018-11-01 19:43:06 +08002727 @Override
chen xu0026ca62019-03-06 15:28:50 -08002728 public int getSubscriptionSpecificCarrierId(int subId) {
chen xu25637222018-11-04 17:17:00 -08002729 final long identity = Binder.clearCallingIdentity();
2730 try {
2731 final Phone phone = getPhone(subId);
2732 return phone == null ? TelephonyManager.UNKNOWN_CARRIER_ID
chen xu0026ca62019-03-06 15:28:50 -08002733 : phone.getSpecificCarrierId();
chen xu25637222018-11-04 17:17:00 -08002734 } finally {
2735 Binder.restoreCallingIdentity(identity);
2736 }
2737 }
2738
2739 @Override
chen xu0026ca62019-03-06 15:28:50 -08002740 public String getSubscriptionSpecificCarrierName(int subId) {
chen xu25637222018-11-04 17:17:00 -08002741 final long identity = Binder.clearCallingIdentity();
2742 try {
2743 final Phone phone = getPhone(subId);
chen xu0026ca62019-03-06 15:28:50 -08002744 return phone == null ? null : phone.getSpecificCarrierName();
chen xu25637222018-11-04 17:17:00 -08002745 } finally {
2746 Binder.restoreCallingIdentity(identity);
2747 }
2748 }
2749
chen xu651eec72018-11-11 19:03:44 -08002750 @Override
chen xu864e11c2018-12-06 22:10:03 -08002751 public int getCarrierIdFromMccMnc(int slotIndex, String mccmnc, boolean isSubscriptionMccMnc) {
2752 if (!isSubscriptionMccMnc) {
2753 enforceReadPrivilegedPermission("getCarrierIdFromMccMnc");
2754 }
chen xu651eec72018-11-11 19:03:44 -08002755 final Phone phone = PhoneFactory.getPhone(slotIndex);
2756 if (phone == null) {
2757 return TelephonyManager.UNKNOWN_CARRIER_ID;
2758 }
2759 final long identity = Binder.clearCallingIdentity();
2760 try {
2761 return CarrierResolver.getCarrierIdFromMccMnc(phone.getContext(), mccmnc);
2762 } finally {
2763 Binder.restoreCallingIdentity(identity);
2764 }
2765 }
2766
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002767 //
2768 // Internal helper methods.
2769 //
2770
Sanket Padaweee13a9b2016-03-08 17:30:28 -08002771 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002772 * Make sure the caller has the MODIFY_PHONE_STATE permission.
2773 *
2774 * @throws SecurityException if the caller does not have the required permission
2775 */
2776 private void enforceModifyPermission() {
2777 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.MODIFY_PHONE_STATE, null);
2778 }
2779
Shuo Qiancd19c462020-01-16 20:51:11 -08002780 /**
2781 * Make sure the caller is system.
2782 *
2783 * @throws SecurityException if the caller is not system.
2784 */
2785 private void enforceSystemCaller() {
2786 if (Binder.getCallingUid() != Process.SYSTEM_UID) {
2787 throw new SecurityException("Caller must be system");
2788 }
2789 }
2790
Shuo Qian3b6ee772019-11-13 17:43:31 -08002791 private void enforceActiveEmergencySessionPermission() {
2792 mApp.enforceCallingOrSelfPermission(
2793 android.Manifest.permission.READ_ACTIVE_EMERGENCY_SESSION, null);
2794 }
2795
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002796 /**
2797 * Make sure the caller has the CALL_PHONE permission.
2798 *
2799 * @throws SecurityException if the caller does not have the required permission
2800 */
2801 private void enforceCallPermission() {
2802 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.CALL_PHONE, null);
2803 }
2804
paulhu5a773602019-08-23 19:17:33 +08002805 private void enforceSettingsPermission() {
2806 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.NETWORK_SETTINGS, null);
Stuart Scott8eef64f2015-04-08 15:13:54 -07002807 }
2808
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002809 private String createTelUrl(String number) {
2810 if (TextUtils.isEmpty(number)) {
2811 return null;
2812 }
2813
Jake Hambye994d462014-02-03 13:10:13 -08002814 return "tel:" + number;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002815 }
2816
Ihab Awadf9e92732013-12-05 18:02:52 -08002817 private static void log(String msg) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002818 Log.d(LOG_TAG, "[PhoneIntfMgr] " + msg);
2819 }
2820
Naveen Kalla1fd79bd2014-08-08 00:48:59 -07002821 private static void logv(String msg) {
2822 Log.v(LOG_TAG, "[PhoneIntfMgr] " + msg);
2823 }
2824
Ihab Awadf9e92732013-12-05 18:02:52 -08002825 private static void loge(String msg) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002826 Log.e(LOG_TAG, "[PhoneIntfMgr] " + msg);
2827 }
2828
Robert Greenwalt36b23af2015-07-06 17:59:14 -07002829 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002830 public int getActivePhoneType() {
Shishir Agrawala9f32182016-04-12 12:00:16 -07002831 return getActivePhoneTypeForSlot(getSlotForDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07002832 }
2833
Sanket Padawe356d7632015-06-22 14:03:32 -07002834 @Override
Sanket Padawe13bac7b2017-03-20 15:04:47 -07002835 public int getActivePhoneTypeForSlot(int slotIndex) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002836 final long identity = Binder.clearCallingIdentity();
2837 try {
2838 final Phone phone = PhoneFactory.getPhone(slotIndex);
2839 if (phone == null) {
2840 return PhoneConstants.PHONE_TYPE_NONE;
2841 } else {
2842 return phone.getPhoneType();
2843 }
2844 } finally {
2845 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07002846 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002847 }
2848
2849 /**
2850 * Returns the CDMA ERI icon index to display
2851 */
Robert Greenwalt36b23af2015-07-06 17:59:14 -07002852 @Override
Philip P. Moltmann700a9592019-10-03 11:53:50 -07002853 public int getCdmaEriIconIndex(String callingPackage, String callingFeatureId) {
2854 return getCdmaEriIconIndexForSubscriber(getDefaultSubscription(), callingPackage,
2855 callingFeatureId);
Wink Saville36469e72014-06-11 15:17:00 -07002856 }
2857
Sanket Padawe356d7632015-06-22 14:03:32 -07002858 @Override
Philip P. Moltmann700a9592019-10-03 11:53:50 -07002859 public int getCdmaEriIconIndexForSubscriber(int subId, String callingPackage,
2860 String callingFeatureId) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08002861 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
Philip P. Moltmann700a9592019-10-03 11:53:50 -07002862 mApp, subId, callingPackage, callingFeatureId,
2863 "getCdmaEriIconIndexForSubscriber")) {
Robert Greenwalt36b23af2015-07-06 17:59:14 -07002864 return -1;
2865 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002866
2867 final long identity = Binder.clearCallingIdentity();
2868 try {
2869 final Phone phone = getPhone(subId);
2870 if (phone != null) {
2871 return phone.getCdmaEriIconIndex();
2872 } else {
2873 return -1;
2874 }
2875 } finally {
2876 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07002877 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002878 }
2879
2880 /**
2881 * Returns the CDMA ERI icon mode,
2882 * 0 - ON
2883 * 1 - FLASHING
2884 */
Robert Greenwalt36b23af2015-07-06 17:59:14 -07002885 @Override
Philip P. Moltmann700a9592019-10-03 11:53:50 -07002886 public int getCdmaEriIconMode(String callingPackage, String callingFeatureId) {
2887 return getCdmaEriIconModeForSubscriber(getDefaultSubscription(), callingPackage,
2888 callingFeatureId);
Wink Saville36469e72014-06-11 15:17:00 -07002889 }
2890
Sanket Padawe356d7632015-06-22 14:03:32 -07002891 @Override
Philip P. Moltmann700a9592019-10-03 11:53:50 -07002892 public int getCdmaEriIconModeForSubscriber(int subId, String callingPackage,
2893 String callingFeatureId) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08002894 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
Philip P. Moltmann700a9592019-10-03 11:53:50 -07002895 mApp, subId, callingPackage, callingFeatureId,
2896 "getCdmaEriIconModeForSubscriber")) {
Robert Greenwalt36b23af2015-07-06 17:59:14 -07002897 return -1;
2898 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002899
2900 final long identity = Binder.clearCallingIdentity();
2901 try {
2902 final Phone phone = getPhone(subId);
2903 if (phone != null) {
2904 return phone.getCdmaEriIconMode();
2905 } else {
2906 return -1;
2907 }
2908 } finally {
2909 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07002910 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002911 }
2912
2913 /**
2914 * Returns the CDMA ERI text,
2915 */
Robert Greenwalt36b23af2015-07-06 17:59:14 -07002916 @Override
Philip P. Moltmann700a9592019-10-03 11:53:50 -07002917 public String getCdmaEriText(String callingPackage, String callingFeatureId) {
2918 return getCdmaEriTextForSubscriber(getDefaultSubscription(), callingPackage,
2919 callingFeatureId);
Wink Saville36469e72014-06-11 15:17:00 -07002920 }
2921
Sanket Padawe356d7632015-06-22 14:03:32 -07002922 @Override
Philip P. Moltmann700a9592019-10-03 11:53:50 -07002923 public String getCdmaEriTextForSubscriber(int subId, String callingPackage,
2924 String callingFeatureId) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08002925 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
Philip P. Moltmann700a9592019-10-03 11:53:50 -07002926 mApp, subId, callingPackage, callingFeatureId,
2927 "getCdmaEriIconTextForSubscriber")) {
Robert Greenwalt36b23af2015-07-06 17:59:14 -07002928 return null;
2929 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002930
2931 final long identity = Binder.clearCallingIdentity();
2932 try {
2933 final Phone phone = getPhone(subId);
2934 if (phone != null) {
2935 return phone.getCdmaEriText();
2936 } else {
2937 return null;
2938 }
2939 } finally {
2940 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07002941 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002942 }
2943
2944 /**
Junda Liuca05d5d2014-08-14 22:36:34 -07002945 * Returns the CDMA MDN.
2946 */
Sanket Padawe356d7632015-06-22 14:03:32 -07002947 @Override
Wink Savilleb564aae2014-10-23 10:18:09 -07002948 public String getCdmaMdn(int subId) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08002949 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
2950 mApp, subId, "getCdmaMdn");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002951
2952 final long identity = Binder.clearCallingIdentity();
2953 try {
2954 final Phone phone = getPhone(subId);
Nazanin Bakhshif782e562018-12-11 15:15:39 -08002955 if (phone != null && phone.getPhoneType() == PhoneConstants.PHONE_TYPE_CDMA) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002956 return phone.getLine1Number();
2957 } else {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08002958 loge("getCdmaMdn: no phone found. Invalid subId: " + subId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002959 return null;
2960 }
2961 } finally {
2962 Binder.restoreCallingIdentity(identity);
Junda Liuca05d5d2014-08-14 22:36:34 -07002963 }
2964 }
2965
2966 /**
2967 * Returns the CDMA MIN.
2968 */
Sanket Padawe356d7632015-06-22 14:03:32 -07002969 @Override
Wink Savilleb564aae2014-10-23 10:18:09 -07002970 public String getCdmaMin(int subId) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08002971 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
2972 mApp, subId, "getCdmaMin");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002973
2974 final long identity = Binder.clearCallingIdentity();
2975 try {
2976 final Phone phone = getPhone(subId);
2977 if (phone != null && phone.getPhoneType() == PhoneConstants.PHONE_TYPE_CDMA) {
2978 return phone.getCdmaMin();
2979 } else {
2980 return null;
2981 }
2982 } finally {
2983 Binder.restoreCallingIdentity(identity);
Junda Liuca05d5d2014-08-14 22:36:34 -07002984 }
2985 }
2986
Hall Liud892bec2018-11-30 14:51:45 -08002987 @Override
2988 public void requestNumberVerification(PhoneNumberRange range, long timeoutMillis,
2989 INumberVerificationCallback callback, String callingPackage) {
2990 if (mApp.checkCallingOrSelfPermission(android.Manifest.permission.MODIFY_PHONE_STATE)
2991 != PERMISSION_GRANTED) {
2992 throw new SecurityException("Caller must hold the MODIFY_PHONE_STATE permission");
2993 }
2994 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
2995
2996 String authorizedPackage = NumberVerificationManager.getAuthorizedPackage(mApp);
2997 if (!TextUtils.equals(callingPackage, authorizedPackage)) {
2998 throw new SecurityException("Calling package must be configured in the device config");
2999 }
3000
3001 if (range == null) {
3002 throw new NullPointerException("Range must be non-null");
3003 }
3004
3005 timeoutMillis = Math.min(timeoutMillis,
Hall Liubd069e32019-02-28 18:56:30 -08003006 TelephonyManager.getMaxNumberVerificationTimeoutMillis());
Hall Liud892bec2018-11-30 14:51:45 -08003007
3008 NumberVerificationManager.getInstance().requestVerification(range, callback, timeoutMillis);
3009 }
3010
Junda Liuca05d5d2014-08-14 22:36:34 -07003011 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003012 * Returns true if CDMA provisioning needs to run.
3013 */
3014 public boolean needsOtaServiceProvisioning() {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003015 final long identity = Binder.clearCallingIdentity();
3016 try {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08003017 return getDefaultPhone().needsOtaServiceProvisioning();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003018 } finally {
3019 Binder.restoreCallingIdentity(identity);
3020 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003021 }
3022
3023 /**
Shishir Agrawal76d5da92014-11-09 16:17:25 -08003024 * Sets the voice mail number of a given subId.
3025 */
3026 @Override
3027 public boolean setVoiceMailNumber(int subId, String alphaTag, String number) {
Shuo Qian2c0ae432019-12-05 11:40:37 -08003028 TelephonyPermissions.enforceCallingOrSelfCarrierPrivilege(
3029 mApp, subId, "setVoiceMailNumber");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003030
3031 final long identity = Binder.clearCallingIdentity();
3032 try {
3033 Boolean success = (Boolean) sendRequest(CMD_SET_VOICEMAIL_NUMBER,
3034 new Pair<String, String>(alphaTag, number), new Integer(subId));
3035 return success;
3036 } finally {
3037 Binder.restoreCallingIdentity(identity);
3038 }
Shishir Agrawal76d5da92014-11-09 16:17:25 -08003039 }
3040
Ta-wei Yen87c49842016-05-13 21:19:52 -07003041 @Override
Ta-wei Yenc9df0432017-04-17 17:09:07 -07003042 public Bundle getVisualVoicemailSettings(String callingPackage, int subId) {
3043 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
Tyler Gunn5ddfdc92019-10-31 13:08:23 -07003044 TelecomManager tm = mApp.getSystemService(TelecomManager.class);
3045 String systemDialer = tm.getSystemDialerPackage();
Ta-wei Yenc9df0432017-04-17 17:09:07 -07003046 if (!TextUtils.equals(callingPackage, systemDialer)) {
3047 throw new SecurityException("caller must be system dialer");
3048 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003049
3050 final long identity = Binder.clearCallingIdentity();
3051 try {
3052 PhoneAccountHandle phoneAccountHandle = PhoneAccountHandleConverter.fromSubId(subId);
3053 if (phoneAccountHandle == null) {
3054 return null;
3055 }
Nazanin Bakhshif782e562018-12-11 15:15:39 -08003056 return VisualVoicemailSettingsUtil.dump(mApp, phoneAccountHandle);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003057 } finally {
3058 Binder.restoreCallingIdentity(identity);
Ta-wei Yenc9df0432017-04-17 17:09:07 -07003059 }
Ta-wei Yenc9df0432017-04-17 17:09:07 -07003060 }
3061
3062 @Override
Philip P. Moltmann700a9592019-10-03 11:53:50 -07003063 public String getVisualVoicemailPackageName(String callingPackage, String callingFeatureId,
3064 int subId) {
Ta-wei Yendca928f2017-01-10 16:17:08 -08003065 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
Jeff Davidson7e17e312018-02-13 18:17:36 -08003066 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
Philip P. Moltmann700a9592019-10-03 11:53:50 -07003067 mApp, subId, callingPackage, callingFeatureId,
3068 "getVisualVoicemailPackageName")) {
Ta-wei Yendca928f2017-01-10 16:17:08 -08003069 return null;
3070 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003071
Jeff Davidsona8e4e242018-03-15 17:16:18 -07003072 final long identity = Binder.clearCallingIdentity();
3073 try {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08003074 return RemoteVvmTaskManager.getRemotePackage(mApp, subId).getPackageName();
Jeff Davidsona8e4e242018-03-15 17:16:18 -07003075 } finally {
3076 Binder.restoreCallingIdentity(identity);
3077 }
Ta-wei Yendca928f2017-01-10 16:17:08 -08003078 }
3079
3080 @Override
Ta-wei Yenb6929602016-05-24 15:48:27 -07003081 public void enableVisualVoicemailSmsFilter(String callingPackage, int subId,
3082 VisualVoicemailSmsFilterSettings settings) {
3083 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003084
3085 final long identity = Binder.clearCallingIdentity();
3086 try {
3087 VisualVoicemailSmsFilterConfig.enableVisualVoicemailSmsFilter(
Nazanin Bakhshif782e562018-12-11 15:15:39 -08003088 mApp, callingPackage, subId, settings);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003089 } finally {
3090 Binder.restoreCallingIdentity(identity);
3091 }
Ta-wei Yen87c49842016-05-13 21:19:52 -07003092 }
3093
3094 @Override
Ta-wei Yenb6929602016-05-24 15:48:27 -07003095 public void disableVisualVoicemailSmsFilter(String callingPackage, int subId) {
3096 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003097
3098 final long identity = Binder.clearCallingIdentity();
3099 try {
3100 VisualVoicemailSmsFilterConfig.disableVisualVoicemailSmsFilter(
Nazanin Bakhshif782e562018-12-11 15:15:39 -08003101 mApp, callingPackage, subId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003102 } finally {
3103 Binder.restoreCallingIdentity(identity);
3104 }
Ta-wei Yen87c49842016-05-13 21:19:52 -07003105 }
3106
3107 @Override
Ta-wei Yenb6929602016-05-24 15:48:27 -07003108 public VisualVoicemailSmsFilterSettings getVisualVoicemailSmsFilterSettings(
3109 String callingPackage, int subId) {
3110 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003111
3112 final long identity = Binder.clearCallingIdentity();
3113 try {
3114 return VisualVoicemailSmsFilterConfig.getVisualVoicemailSmsFilterSettings(
Nazanin Bakhshif782e562018-12-11 15:15:39 -08003115 mApp, callingPackage, subId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003116 } finally {
3117 Binder.restoreCallingIdentity(identity);
3118 }
Ta-wei Yen87c49842016-05-13 21:19:52 -07003119 }
3120
3121 @Override
Ta-wei Yen30a69c82016-12-27 14:52:32 -08003122 public VisualVoicemailSmsFilterSettings getActiveVisualVoicemailSmsFilterSettings(int subId) {
Brad Ebinger35c841c2018-10-01 10:40:55 -07003123 enforceReadPrivilegedPermission("getActiveVisualVoicemailSmsFilterSettings");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003124
3125 final long identity = Binder.clearCallingIdentity();
3126 try {
3127 return VisualVoicemailSmsFilterConfig.getActiveVisualVoicemailSmsFilterSettings(
Nazanin Bakhshif782e562018-12-11 15:15:39 -08003128 mApp, subId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003129 } finally {
3130 Binder.restoreCallingIdentity(identity);
3131 }
Ta-wei Yen30a69c82016-12-27 14:52:32 -08003132 }
3133
3134 @Override
Philip P. Moltmann2f6f8ce2020-03-18 18:17:02 -07003135 public void sendVisualVoicemailSmsForSubscriber(String callingPackage,
3136 String callingAttributionTag, int subId, String number, int port, String text,
3137 PendingIntent sentIntent) {
Ta-wei Yen30a69c82016-12-27 14:52:32 -08003138 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
Ta-wei Yen527a9c02017-01-06 15:29:25 -08003139 enforceVisualVoicemailPackage(callingPackage, subId);
Ta-wei Yen30a69c82016-12-27 14:52:32 -08003140 enforceSendSmsPermission();
Amit Mahajandccb3f12019-05-13 13:48:32 -07003141 SmsController smsController = PhoneFactory.getSmsController();
Philip P. Moltmann2f6f8ce2020-03-18 18:17:02 -07003142 smsController.sendVisualVoicemailSmsForSubscriber(callingPackage, callingAttributionTag,
3143 subId, number, port, text, sentIntent);
Ta-wei Yen87c49842016-05-13 21:19:52 -07003144 }
Amit Mahajandccb3f12019-05-13 13:48:32 -07003145
Shishir Agrawal76d5da92014-11-09 16:17:25 -08003146 /**
fionaxu0152e512016-11-14 13:36:14 -08003147 * Sets the voice activation state of a given subId.
3148 */
3149 @Override
3150 public void setVoiceActivationState(int subId, int activationState) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08003151 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
3152 mApp, subId, "setVoiceActivationState");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003153
3154 final long identity = Binder.clearCallingIdentity();
3155 try {
3156 final Phone phone = getPhone(subId);
3157 if (phone != null) {
3158 phone.setVoiceActivationState(activationState);
3159 } else {
3160 loge("setVoiceActivationState fails with invalid subId: " + subId);
3161 }
3162 } finally {
3163 Binder.restoreCallingIdentity(identity);
fionaxu0152e512016-11-14 13:36:14 -08003164 }
3165 }
3166
3167 /**
3168 * Sets the data activation state of a given subId.
3169 */
3170 @Override
3171 public void setDataActivationState(int subId, int activationState) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08003172 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
3173 mApp, subId, "setDataActivationState");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003174
3175 final long identity = Binder.clearCallingIdentity();
3176 try {
3177 final Phone phone = getPhone(subId);
3178 if (phone != null) {
3179 phone.setDataActivationState(activationState);
3180 } else {
Taesu Leef8fbed92019-10-07 18:47:02 +09003181 loge("setDataActivationState fails with invalid subId: " + subId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003182 }
3183 } finally {
3184 Binder.restoreCallingIdentity(identity);
fionaxu0152e512016-11-14 13:36:14 -08003185 }
3186 }
3187
3188 /**
3189 * Returns the voice activation state of a given subId.
3190 */
3191 @Override
3192 public int getVoiceActivationState(int subId, String callingPackage) {
Brad Ebinger35c841c2018-10-01 10:40:55 -07003193 enforceReadPrivilegedPermission("getVoiceActivationState");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003194
fionaxu0152e512016-11-14 13:36:14 -08003195 final Phone phone = getPhone(subId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003196 final long identity = Binder.clearCallingIdentity();
3197 try {
3198 if (phone != null) {
3199 return phone.getVoiceActivationState();
3200 } else {
3201 return TelephonyManager.SIM_ACTIVATION_STATE_UNKNOWN;
3202 }
3203 } finally {
3204 Binder.restoreCallingIdentity(identity);
fionaxu0152e512016-11-14 13:36:14 -08003205 }
3206 }
3207
3208 /**
3209 * Returns the data activation state of a given subId.
3210 */
3211 @Override
3212 public int getDataActivationState(int subId, String callingPackage) {
Brad Ebinger35c841c2018-10-01 10:40:55 -07003213 enforceReadPrivilegedPermission("getDataActivationState");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003214
fionaxu0152e512016-11-14 13:36:14 -08003215 final Phone phone = getPhone(subId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003216 final long identity = Binder.clearCallingIdentity();
3217 try {
3218 if (phone != null) {
3219 return phone.getDataActivationState();
3220 } else {
3221 return TelephonyManager.SIM_ACTIVATION_STATE_UNKNOWN;
3222 }
3223 } finally {
3224 Binder.restoreCallingIdentity(identity);
fionaxu0152e512016-11-14 13:36:14 -08003225 }
3226 }
3227
3228 /**
Wink Saville36469e72014-06-11 15:17:00 -07003229 * Returns the unread count of voicemails for a subId
3230 */
Sanket Padawe356d7632015-06-22 14:03:32 -07003231 @Override
Philip P. Moltmann700a9592019-10-03 11:53:50 -07003232 public int getVoiceMessageCountForSubscriber(int subId, String callingPackage,
3233 String callingFeatureId) {
Brad Ebingerf7664ba2018-11-29 12:43:38 -08003234 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
Philip P. Moltmann700a9592019-10-03 11:53:50 -07003235 mApp, subId, callingPackage, callingFeatureId,
3236 "getVoiceMessageCountForSubscriber")) {
Brad Ebingerf7664ba2018-11-29 12:43:38 -08003237 return 0;
3238 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003239 final long identity = Binder.clearCallingIdentity();
3240 try {
3241 final Phone phone = getPhone(subId);
3242 if (phone != null) {
3243 return phone.getVoiceMessageCount();
3244 } else {
3245 return 0;
3246 }
3247 } finally {
3248 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07003249 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003250 }
3251
3252 /**
pkanwar8a4dcfb2017-01-19 13:43:16 -08003253 * returns true, if the device is in a state where both voice and data
3254 * are supported simultaneously. This can change based on location or network condition.
3255 */
3256 @Override
3257 public boolean isConcurrentVoiceAndDataAllowed(int subId) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003258 final long identity = Binder.clearCallingIdentity();
3259 try {
3260 final Phone phone = getPhone(subId);
3261 return (phone == null ? false : phone.isConcurrentVoiceAndDataAllowed());
3262 } finally {
3263 Binder.restoreCallingIdentity(identity);
3264 }
pkanwar8a4dcfb2017-01-19 13:43:16 -08003265 }
3266
3267 /**
fionaxu235cc5e2017-03-06 22:25:57 -08003268 * Send the dialer code if called from the current default dialer or the caller has
3269 * carrier privilege.
3270 * @param inputCode The dialer code to send
3271 */
3272 @Override
3273 public void sendDialerSpecialCode(String callingPackage, String inputCode) {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08003274 final Phone defaultPhone = getDefaultPhone();
fionaxu235cc5e2017-03-06 22:25:57 -08003275 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
Tyler Gunn5ddfdc92019-10-31 13:08:23 -07003276 TelecomManager tm = defaultPhone.getContext().getSystemService(TelecomManager.class);
3277 String defaultDialer = tm.getDefaultDialerPackage();
fionaxu235cc5e2017-03-06 22:25:57 -08003278 if (!TextUtils.equals(callingPackage, defaultDialer)) {
Shuo Qian2c0ae432019-12-05 11:40:37 -08003279 TelephonyPermissions.enforceCallingOrSelfCarrierPrivilege(mApp,
Jeff Davidson7e17e312018-02-13 18:17:36 -08003280 getDefaultSubscription(), "sendDialerSpecialCode");
fionaxu235cc5e2017-03-06 22:25:57 -08003281 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003282
3283 final long identity = Binder.clearCallingIdentity();
3284 try {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08003285 defaultPhone.sendDialerSpecialCode(inputCode);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003286 } finally {
3287 Binder.restoreCallingIdentity(identity);
3288 }
fionaxu235cc5e2017-03-06 22:25:57 -08003289 }
3290
Pengquan Menga1bb6272018-09-06 09:59:22 -07003291 @Override
3292 public int getNetworkSelectionMode(int subId) {
shilufc958392020-01-20 11:36:01 -08003293 TelephonyPermissions
3294 .enforeceCallingOrSelfReadPrecisePhoneStatePermissionOrCarrierPrivilege(
3295 mApp, subId, "getNetworkSelectionMode");
3296 final long identity = Binder.clearCallingIdentity();
3297 try {
3298 if (!isActiveSubscription(subId)) {
3299 return TelephonyManager.NETWORK_SELECTION_MODE_UNKNOWN;
3300 }
3301 return (int) sendRequest(CMD_GET_NETWORK_SELECTION_MODE, null /* argument */, subId);
3302 } finally {
3303 Binder.restoreCallingIdentity(identity);
Pengquan Menge92a50d2018-09-21 15:54:48 -07003304 }
Pengquan Menga1bb6272018-09-06 09:59:22 -07003305 }
3306
Brad Ebinger35c841c2018-10-01 10:40:55 -07003307 @Override
Brad Ebingerb2b65522019-03-15 13:48:47 -07003308 public boolean isInEmergencySmsMode() {
3309 enforceReadPrivilegedPermission("isInEmergencySmsMode");
3310 final long identity = Binder.clearCallingIdentity();
3311 try {
3312 for (Phone phone : PhoneFactory.getPhones()) {
3313 if (phone.isInEmergencySmsMode()) {
3314 return true;
3315 }
3316 }
3317 } finally {
3318 Binder.restoreCallingIdentity(identity);
3319 }
3320 return false;
3321 }
3322
shilu366312e2019-12-17 09:28:10 -08003323 /**
3324 * Requires carrier privileges or READ_PRECISE_PHONE_STATE permission.
3325 * @param subId The subscription to use to check the configuration.
3326 * @param c The callback that will be used to send the result.
3327 */
Brad Ebingerb2b65522019-03-15 13:48:47 -07003328 @Override
Brad Ebinger9878b0b2018-11-08 17:43:22 -08003329 public void registerImsRegistrationCallback(int subId, IImsRegistrationCallback c)
3330 throws RemoteException {
Rambo Wang37f9c242020-02-10 14:45:28 -08003331 TelephonyPermissions.enforeceCallingOrSelfReadPrecisePhoneStatePermissionOrCarrierPrivilege(
3332 mApp, subId, "registerImsRegistrationCallback");
shilu366312e2019-12-17 09:28:10 -08003333
Brad Ebingerbc7dd582019-10-17 17:03:22 -07003334 if (!ImsManager.isImsSupportedOnDevice(mApp)) {
3335 throw new ServiceSpecificException(ImsException.CODE_ERROR_UNSUPPORTED_OPERATION,
3336 "IMS not available on device.");
3337 }
Brad Ebinger35c841c2018-10-01 10:40:55 -07003338 final long token = Binder.clearCallingIdentity();
3339 try {
Brad Ebinger4ae57f92019-01-09 16:51:30 -08003340 // TODO: Refactor to remove ImsManager dependence and query through ImsPhone directly.
Nazanin Bakhshif782e562018-12-11 15:15:39 -08003341 ImsManager.getInstance(mApp, getSlotIndexOrException(subId))
Brad Ebinger35c841c2018-10-01 10:40:55 -07003342 .addRegistrationCallbackForSubscription(c, subId);
Brad Ebinger1ce9c432019-07-16 13:19:44 -07003343 } catch (ImsException e) {
3344 throw new ServiceSpecificException(e.getCode());
Brad Ebinger35c841c2018-10-01 10:40:55 -07003345 } finally {
3346 Binder.restoreCallingIdentity(token);
3347 }
3348 }
3349
shilu366312e2019-12-17 09:28:10 -08003350 /**
3351 * Requires carrier privileges or READ_PRECISE_PHONE_STATE permission.
3352 * @param subId The subscription to use to check the configuration.
3353 * @param c The callback that will be used to send the result.
3354 */
Brad Ebinger35c841c2018-10-01 10:40:55 -07003355 @Override
Brad Ebinger9878b0b2018-11-08 17:43:22 -08003356 public void unregisterImsRegistrationCallback(int subId, IImsRegistrationCallback c) {
Rambo Wang37f9c242020-02-10 14:45:28 -08003357 TelephonyPermissions.enforeceCallingOrSelfReadPrecisePhoneStatePermissionOrCarrierPrivilege(
3358 mApp, subId, "unregisterImsRegistrationCallback");
Brad Ebinger4ae57f92019-01-09 16:51:30 -08003359 if (!SubscriptionManager.isValidSubscriptionId(subId)) {
3360 throw new IllegalArgumentException("Invalid Subscription ID: " + subId);
3361 }
Meng Wangafbc5852019-09-19 17:37:13 -07003362 final long token = Binder.clearCallingIdentity();
3363 try {
Meng Wang8ea57e32020-06-25 11:03:56 -07003364 // TODO(b/159910732): Remove ImsManager dependence and query through ImsPhone directly.
Meng Wangafbc5852019-09-19 17:37:13 -07003365 ImsManager.getInstance(mApp, getSlotIndexOrException(subId))
3366 .removeRegistrationCallbackForSubscription(c, subId);
3367 } catch (ImsException e) {
3368 Log.i(LOG_TAG, "unregisterImsRegistrationCallback: " + subId
3369 + "is inactive, ignoring unregister.");
3370 // If the subscription is no longer active, just return, since the callback
3371 // will already have been removed internally.
3372 } finally {
3373 Binder.restoreCallingIdentity(token);
3374 }
Brad Ebinger35c841c2018-10-01 10:40:55 -07003375 }
3376
Brad Ebingera34a6c22019-10-22 17:36:18 -07003377 /**
3378 * Get the IMS service registration state for the MmTelFeature associated with this sub id.
3379 */
3380 @Override
3381 public void getImsMmTelRegistrationState(int subId, IIntegerConsumer consumer) {
3382 enforceReadPrivilegedPermission("getImsMmTelRegistrationState");
3383 if (!ImsManager.isImsSupportedOnDevice(mApp)) {
3384 throw new ServiceSpecificException(ImsException.CODE_ERROR_UNSUPPORTED_OPERATION,
3385 "IMS not available on device.");
3386 }
3387 final long token = Binder.clearCallingIdentity();
3388 try {
3389 Phone phone = getPhone(subId);
3390 if (phone == null) {
3391 Log.w(LOG_TAG, "getImsMmTelRegistrationState: called with an invalid subscription '"
3392 + subId + "'");
3393 throw new ServiceSpecificException(ImsException.CODE_ERROR_INVALID_SUBSCRIPTION);
3394 }
3395 phone.getImsRegistrationState(regState -> {
3396 try {
3397 consumer.accept((regState == null)
3398 ? RegistrationManager.REGISTRATION_STATE_NOT_REGISTERED : regState);
3399 } catch (RemoteException e) {
3400 // Ignore if the remote process is no longer available to call back.
3401 Log.w(LOG_TAG, "getImsMmTelRegistrationState: callback not available.");
3402 }
3403 });
3404 } finally {
3405 Binder.restoreCallingIdentity(token);
3406 }
3407 }
3408
3409 /**
3410 * Get the transport type for the IMS service registration state.
3411 */
3412 @Override
3413 public void getImsMmTelRegistrationTransportType(int subId, IIntegerConsumer consumer) {
Rambo Wang37f9c242020-02-10 14:45:28 -08003414 TelephonyPermissions.enforeceCallingOrSelfReadPrecisePhoneStatePermissionOrCarrierPrivilege(
3415 mApp, subId, "getImsMmTelRegistrationTransportType");
Brad Ebingera34a6c22019-10-22 17:36:18 -07003416 if (!ImsManager.isImsSupportedOnDevice(mApp)) {
3417 throw new ServiceSpecificException(ImsException.CODE_ERROR_UNSUPPORTED_OPERATION,
3418 "IMS not available on device.");
3419 }
3420 final long token = Binder.clearCallingIdentity();
3421 try {
3422 Phone phone = getPhone(subId);
3423 if (phone == null) {
3424 Log.w(LOG_TAG, "getImsMmTelRegistrationState: called with an invalid subscription '"
3425 + subId + "'");
3426 throw new ServiceSpecificException(ImsException.CODE_ERROR_INVALID_SUBSCRIPTION);
3427 }
3428 phone.getImsRegistrationTech(regTech -> {
3429 // Convert registration tech from ImsRegistrationImplBase -> RegistrationManager
3430 int regTechConverted = (regTech == null)
3431 ? ImsRegistrationImplBase.REGISTRATION_TECH_NONE : regTech;
3432 regTechConverted = RegistrationManager.IMS_REG_TO_ACCESS_TYPE_MAP.get(
3433 regTechConverted);
3434 try {
3435 consumer.accept(regTechConverted);
3436 } catch (RemoteException e) {
3437 // Ignore if the remote process is no longer available to call back.
3438 Log.w(LOG_TAG, "getImsMmTelRegistrationState: callback not available.");
3439 }
3440 });
3441 } finally {
3442 Binder.restoreCallingIdentity(token);
3443 }
3444 }
3445
shilu366312e2019-12-17 09:28:10 -08003446 /**
3447 * Requires carrier privileges or READ_PRECISE_PHONE_STATE permission.
3448 * @param subId The subscription to use to check the configuration.
3449 * @param c The callback that will be used to send the result.
3450 */
Brad Ebinger35c841c2018-10-01 10:40:55 -07003451 @Override
Brad Ebinger9878b0b2018-11-08 17:43:22 -08003452 public void registerMmTelCapabilityCallback(int subId, IImsCapabilityCallback c)
3453 throws RemoteException {
Rambo Wang37f9c242020-02-10 14:45:28 -08003454 TelephonyPermissions.enforeceCallingOrSelfReadPrecisePhoneStatePermissionOrCarrierPrivilege(
3455 mApp, subId, "registerMmTelCapabilityCallback");
Brad Ebingerbc7dd582019-10-17 17:03:22 -07003456 if (!ImsManager.isImsSupportedOnDevice(mApp)) {
3457 throw new ServiceSpecificException(ImsException.CODE_ERROR_UNSUPPORTED_OPERATION,
3458 "IMS not available on device.");
3459 }
Brad Ebinger35c841c2018-10-01 10:40:55 -07003460 // TODO: Refactor to remove ImsManager dependence and query through ImsPhone directly.
3461 final long token = Binder.clearCallingIdentity();
3462 try {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08003463 ImsManager.getInstance(mApp, getSlotIndexOrException(subId))
Brad Ebinger35c841c2018-10-01 10:40:55 -07003464 .addCapabilitiesCallbackForSubscription(c, subId);
Brad Ebinger1ce9c432019-07-16 13:19:44 -07003465 } catch (ImsException e) {
3466 throw new ServiceSpecificException(e.getCode());
Brad Ebinger35c841c2018-10-01 10:40:55 -07003467 } finally {
3468 Binder.restoreCallingIdentity(token);
3469 }
3470 }
3471
shilu366312e2019-12-17 09:28:10 -08003472 /**
3473 * Requires carrier privileges or READ_PRECISE_PHONE_STATE permission.
3474 * @param subId The subscription to use to check the configuration.
3475 * @param c The callback that will be used to send the result.
3476 */
Brad Ebinger35c841c2018-10-01 10:40:55 -07003477 @Override
Brad Ebinger9878b0b2018-11-08 17:43:22 -08003478 public void unregisterMmTelCapabilityCallback(int subId, IImsCapabilityCallback c) {
Rambo Wang37f9c242020-02-10 14:45:28 -08003479 TelephonyPermissions.enforeceCallingOrSelfReadPrecisePhoneStatePermissionOrCarrierPrivilege(
3480 mApp, subId, "unregisterMmTelCapabilityCallback");
Brad Ebinger4ae57f92019-01-09 16:51:30 -08003481 if (!SubscriptionManager.isValidSubscriptionId(subId)) {
3482 throw new IllegalArgumentException("Invalid Subscription ID: " + subId);
3483 }
Meng Wangafbc5852019-09-19 17:37:13 -07003484
3485 final long token = Binder.clearCallingIdentity();
3486 try {
Meng Wang8ea57e32020-06-25 11:03:56 -07003487 // TODO(b/159910732): Remove ImsManager dependence and query through ImsPhone directly.
Meng Wangafbc5852019-09-19 17:37:13 -07003488 ImsManager.getInstance(mApp, getSlotIndexOrException(subId))
Brad Ebinger4ae57f92019-01-09 16:51:30 -08003489 .removeCapabilitiesCallbackForSubscription(c, subId);
Meng Wangafbc5852019-09-19 17:37:13 -07003490 } catch (ImsException e) {
3491 Log.i(LOG_TAG, "unregisterMmTelCapabilityCallback: " + subId
3492 + "is inactive, ignoring unregister.");
3493 // If the subscription is no longer active, just return, since the callback
3494 // will already have been removed internally.
3495 } finally {
3496 Binder.restoreCallingIdentity(token);
3497 }
Brad Ebinger35c841c2018-10-01 10:40:55 -07003498 }
3499
3500 @Override
Brad Ebinger9878b0b2018-11-08 17:43:22 -08003501 public boolean isCapable(int subId, int capability, int regTech) {
3502 enforceReadPrivilegedPermission("isCapable");
Brad Ebinger35c841c2018-10-01 10:40:55 -07003503 // TODO: Refactor to remove ImsManager dependence and query through ImsPhone directly.
3504 final long token = Binder.clearCallingIdentity();
3505 try {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08003506 return ImsManager.getInstance(mApp,
Brad Ebinger35c841c2018-10-01 10:40:55 -07003507 getSlotIndexOrException(subId)).queryMmTelCapability(capability, regTech);
Brad Ebinger1ce9c432019-07-16 13:19:44 -07003508 } catch (com.android.ims.ImsException e) {
Brad Ebinger35c841c2018-10-01 10:40:55 -07003509 Log.w(LOG_TAG, "IMS isCapable - service unavailable: " + e.getMessage());
3510 return false;
Brad Ebinger1ce9c432019-07-16 13:19:44 -07003511 } catch (ImsException e) {
Brad Ebinger6b5ac222019-02-04 14:36:52 -08003512 Log.i(LOG_TAG, "isCapable: " + subId + " is inactive, returning false.");
3513 return false;
Brad Ebinger35c841c2018-10-01 10:40:55 -07003514 } finally {
3515 Binder.restoreCallingIdentity(token);
3516 }
3517 }
3518
3519 @Override
Brad Ebinger9878b0b2018-11-08 17:43:22 -08003520 public boolean isAvailable(int subId, int capability, int regTech) {
3521 enforceReadPrivilegedPermission("isAvailable");
Brad Ebinger35c841c2018-10-01 10:40:55 -07003522 final long token = Binder.clearCallingIdentity();
3523 try {
3524 Phone phone = getPhone(subId);
3525 if (phone == null) return false;
3526 return phone.isImsCapabilityAvailable(capability, regTech);
Daniel Bright5e40e4e2020-03-11 16:35:39 -07003527 } catch (com.android.ims.ImsException e) {
3528 Log.w(LOG_TAG, "IMS isAvailable - service unavailable: " + e.getMessage());
3529 return false;
Brad Ebinger35c841c2018-10-01 10:40:55 -07003530 } finally {
3531 Binder.restoreCallingIdentity(token);
3532 }
3533 }
3534
Brad Ebingerbc7dd582019-10-17 17:03:22 -07003535 /**
3536 * Determines if the MmTel feature capability is supported by the carrier configuration for this
3537 * subscription.
3538 * @param subId The subscription to use to check the configuration.
3539 * @param callback The callback that will be used to send the result.
3540 * @param capability The MmTelFeature capability that will be used to send the result.
3541 * @param transportType The transport type of the MmTelFeature capability.
3542 */
3543 @Override
3544 public void isMmTelCapabilitySupported(int subId, IIntegerConsumer callback, int capability,
3545 int transportType) {
3546 enforceReadPrivilegedPermission("isMmTelCapabilitySupported");
3547 if (!ImsManager.isImsSupportedOnDevice(mApp)) {
3548 throw new ServiceSpecificException(ImsException.CODE_ERROR_UNSUPPORTED_OPERATION,
3549 "IMS not available on device.");
3550 }
3551 final long token = Binder.clearCallingIdentity();
3552 try {
3553 int slotId = getSlotIndex(subId);
3554 if (slotId <= SubscriptionManager.INVALID_SIM_SLOT_INDEX) {
3555 Log.w(LOG_TAG, "isMmTelCapabilitySupported: called with an inactive subscription '"
3556 + subId + "'");
3557 throw new ServiceSpecificException(ImsException.CODE_ERROR_INVALID_SUBSCRIPTION);
3558 }
3559 ImsManager.getInstance(mApp, slotId).isSupported(capability,
3560 transportType, aBoolean -> {
3561 try {
3562 callback.accept((aBoolean == null) ? 0 : (aBoolean ? 1 : 0));
3563 } catch (RemoteException e) {
3564 Log.w(LOG_TAG, "isMmTelCapabilitySupported: remote caller is not "
3565 + "running. Ignore");
3566 }
3567 });
3568 } finally {
3569 Binder.restoreCallingIdentity(token);
3570 }
3571 }
3572
shilu366312e2019-12-17 09:28:10 -08003573 /**
3574 * Requires carrier privileges or READ_PRECISE_PHONE_STATE permission.
3575 * @param subId The subscription to use to check the configuration.
3576 */
Brad Ebinger35c841c2018-10-01 10:40:55 -07003577 @Override
3578 public boolean isAdvancedCallingSettingEnabled(int subId) {
Rambo Wang37f9c242020-02-10 14:45:28 -08003579 TelephonyPermissions.enforeceCallingOrSelfReadPrecisePhoneStatePermissionOrCarrierPrivilege(
3580 mApp, subId, "isAdvancedCallingSettingEnabled");
shilu366312e2019-12-17 09:28:10 -08003581
Brad Ebinger35c841c2018-10-01 10:40:55 -07003582 // TODO: Refactor to remove ImsManager dependence and query through ImsPhone directly.
3583 final long token = Binder.clearCallingIdentity();
3584 try {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08003585 return ImsManager.getInstance(mApp,
Brad Ebinger35c841c2018-10-01 10:40:55 -07003586 getSlotIndexOrException(subId)).isEnhanced4gLteModeSettingEnabledByUser();
Brad Ebinger1ce9c432019-07-16 13:19:44 -07003587 } catch (ImsException e) {
3588 throw new ServiceSpecificException(e.getCode());
Brad Ebinger35c841c2018-10-01 10:40:55 -07003589 } finally {
3590 Binder.restoreCallingIdentity(token);
3591 }
3592 }
3593
3594 @Override
Brad Ebinger1c162042019-02-21 14:49:10 -08003595 public void setAdvancedCallingSettingEnabled(int subId, boolean isEnabled) {
Brad Ebinger35c841c2018-10-01 10:40:55 -07003596 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(mApp, subId,
Brad Ebinger1c162042019-02-21 14:49:10 -08003597 "setAdvancedCallingSettingEnabled");
Brad Ebinger35c841c2018-10-01 10:40:55 -07003598 final long identity = Binder.clearCallingIdentity();
3599 try {
3600 // TODO: Refactor to remove ImsManager dependence and query through ImsPhone directly.
Nazanin Bakhshif782e562018-12-11 15:15:39 -08003601 ImsManager.getInstance(mApp,
Brad Ebinger35c841c2018-10-01 10:40:55 -07003602 getSlotIndexOrException(subId)).setEnhanced4gLteModeSetting(isEnabled);
Brad Ebinger1ce9c432019-07-16 13:19:44 -07003603 } catch (ImsException e) {
3604 throw new ServiceSpecificException(e.getCode());
Brad Ebinger35c841c2018-10-01 10:40:55 -07003605 } finally {
3606 Binder.restoreCallingIdentity(identity);
3607 }
3608 }
3609
shilu366312e2019-12-17 09:28:10 -08003610 /**
3611 * Requires carrier privileges or READ_PRECISE_PHONE_STATE permission.
3612 * @param subId The subscription to use to check the configuration.
3613 */
Brad Ebinger35c841c2018-10-01 10:40:55 -07003614 @Override
Brad Ebinger9878b0b2018-11-08 17:43:22 -08003615 public boolean isVtSettingEnabled(int subId) {
Rambo Wang37f9c242020-02-10 14:45:28 -08003616 TelephonyPermissions.enforeceCallingOrSelfReadPrecisePhoneStatePermissionOrCarrierPrivilege(
3617 mApp, subId, "isVtSettingEnabled");
Brad Ebinger35c841c2018-10-01 10:40:55 -07003618 final long identity = Binder.clearCallingIdentity();
3619 try {
3620 // TODO: Refactor to remove ImsManager dependence and query through ImsPhone directly.
Brad Ebinger1ce9c432019-07-16 13:19:44 -07003621 return ImsManager.getInstance(mApp, getSlotIndexOrException(subId)).isVtEnabledByUser();
3622 } catch (ImsException e) {
3623 throw new ServiceSpecificException(e.getCode());
Brad Ebinger35c841c2018-10-01 10:40:55 -07003624 } finally {
3625 Binder.restoreCallingIdentity(identity);
3626 }
3627 }
3628
3629 @Override
Brad Ebinger1c162042019-02-21 14:49:10 -08003630 public void setVtSettingEnabled(int subId, boolean isEnabled) {
Brad Ebinger35c841c2018-10-01 10:40:55 -07003631 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(mApp, subId,
Brad Ebinger1c162042019-02-21 14:49:10 -08003632 "setVtSettingEnabled");
Brad Ebinger35c841c2018-10-01 10:40:55 -07003633 final long identity = Binder.clearCallingIdentity();
3634 try {
3635 // TODO: Refactor to remove ImsManager dependence and query through ImsPhone directly.
Nazanin Bakhshif782e562018-12-11 15:15:39 -08003636 ImsManager.getInstance(mApp, getSlotIndexOrException(subId)).setVtSetting(isEnabled);
Brad Ebinger1ce9c432019-07-16 13:19:44 -07003637 } catch (ImsException e) {
3638 throw new ServiceSpecificException(e.getCode());
Brad Ebinger35c841c2018-10-01 10:40:55 -07003639 } finally {
3640 Binder.restoreCallingIdentity(identity);
3641 }
3642 }
3643
shilu366312e2019-12-17 09:28:10 -08003644 /**
3645 * Requires carrier privileges or READ_PRECISE_PHONE_STATE permission.
3646 * @param subId The subscription to use to check the configuration.
3647 */
Brad Ebinger35c841c2018-10-01 10:40:55 -07003648 @Override
3649 public boolean isVoWiFiSettingEnabled(int subId) {
Rambo Wang37f9c242020-02-10 14:45:28 -08003650 TelephonyPermissions.enforeceCallingOrSelfReadPrecisePhoneStatePermissionOrCarrierPrivilege(
3651 mApp, subId, "isVoWiFiSettingEnabled");
Brad Ebinger35c841c2018-10-01 10:40:55 -07003652 final long identity = Binder.clearCallingIdentity();
3653 try {
3654 // TODO: Refactor to remove ImsManager dependence and query through ImsPhone directly.
Nazanin Bakhshif782e562018-12-11 15:15:39 -08003655 return ImsManager.getInstance(mApp,
Brad Ebinger35c841c2018-10-01 10:40:55 -07003656 getSlotIndexOrException(subId)).isWfcEnabledByUser();
Brad Ebinger1ce9c432019-07-16 13:19:44 -07003657 } catch (ImsException e) {
3658 throw new ServiceSpecificException(e.getCode());
Brad Ebinger35c841c2018-10-01 10:40:55 -07003659 } finally {
3660 Binder.restoreCallingIdentity(identity);
3661 }
3662 }
3663
3664 @Override
Brad Ebinger1c162042019-02-21 14:49:10 -08003665 public void setVoWiFiSettingEnabled(int subId, boolean isEnabled) {
Brad Ebinger35c841c2018-10-01 10:40:55 -07003666 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(mApp, subId,
Brad Ebinger1c162042019-02-21 14:49:10 -08003667 "setVoWiFiSettingEnabled");
Brad Ebinger35c841c2018-10-01 10:40:55 -07003668 final long identity = Binder.clearCallingIdentity();
3669 try {
3670 // TODO: Refactor to remove ImsManager dependence and query through ImsPhone directly.
Nazanin Bakhshif782e562018-12-11 15:15:39 -08003671 ImsManager.getInstance(mApp, getSlotIndexOrException(subId)).setWfcSetting(isEnabled);
Brad Ebinger1ce9c432019-07-16 13:19:44 -07003672 } catch (ImsException e) {
3673 throw new ServiceSpecificException(e.getCode());
Brad Ebinger35c841c2018-10-01 10:40:55 -07003674 } finally {
3675 Binder.restoreCallingIdentity(identity);
3676 }
3677 }
3678
shilu366312e2019-12-17 09:28:10 -08003679 /**
3680 * Requires carrier privileges or READ_PRECISE_PHONE_STATE permission.
3681 * @param subId The subscription to use to check the configuration.
3682 */
Brad Ebinger35c841c2018-10-01 10:40:55 -07003683 @Override
3684 public boolean isVoWiFiRoamingSettingEnabled(int subId) {
Rambo Wang37f9c242020-02-10 14:45:28 -08003685 TelephonyPermissions.enforeceCallingOrSelfReadPrecisePhoneStatePermissionOrCarrierPrivilege(
3686 mApp, subId, "isVoWiFiRoamingSettingEnabled");
Brad Ebinger35c841c2018-10-01 10:40:55 -07003687 final long identity = Binder.clearCallingIdentity();
3688 try {
3689 // TODO: Refactor to remove ImsManager dependence and query through ImsPhone directly.
Nazanin Bakhshif782e562018-12-11 15:15:39 -08003690 return ImsManager.getInstance(mApp,
Brad Ebinger35c841c2018-10-01 10:40:55 -07003691 getSlotIndexOrException(subId)).isWfcRoamingEnabledByUser();
Brad Ebinger1ce9c432019-07-16 13:19:44 -07003692 } catch (ImsException e) {
3693 throw new ServiceSpecificException(e.getCode());
Brad Ebinger35c841c2018-10-01 10:40:55 -07003694 } finally {
3695 Binder.restoreCallingIdentity(identity);
3696 }
3697 }
3698
3699 @Override
Brad Ebinger1c162042019-02-21 14:49:10 -08003700 public void setVoWiFiRoamingSettingEnabled(int subId, boolean isEnabled) {
Brad Ebinger35c841c2018-10-01 10:40:55 -07003701 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(mApp, subId,
Brad Ebinger1c162042019-02-21 14:49:10 -08003702 "setVoWiFiRoamingSettingEnabled");
Brad Ebinger35c841c2018-10-01 10:40:55 -07003703 final long identity = Binder.clearCallingIdentity();
3704 try {
3705 // TODO: Refactor to remove ImsManager dependence and query through ImsPhone directly.
Nazanin Bakhshif782e562018-12-11 15:15:39 -08003706 ImsManager.getInstance(mApp,
Brad Ebinger35c841c2018-10-01 10:40:55 -07003707 getSlotIndexOrException(subId)).setWfcRoamingSetting(isEnabled);
Brad Ebinger1ce9c432019-07-16 13:19:44 -07003708 } catch (ImsException e) {
3709 throw new ServiceSpecificException(e.getCode());
Brad Ebinger35c841c2018-10-01 10:40:55 -07003710 } finally {
3711 Binder.restoreCallingIdentity(identity);
3712 }
3713 }
3714
3715 @Override
3716 public void setVoWiFiNonPersistent(int subId, boolean isCapable, int mode) {
3717 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(mApp, subId,
3718 "setVoWiFiNonPersistent");
3719 final long identity = Binder.clearCallingIdentity();
3720 try {
3721 // TODO: Refactor to remove ImsManager dependence and query through ImsPhone directly.
Nazanin Bakhshif782e562018-12-11 15:15:39 -08003722 ImsManager.getInstance(mApp,
Brad Ebinger2d29c012019-05-07 18:33:46 -07003723 getSlotIndexOrException(subId)).setWfcNonPersistent(isCapable, mode);
Brad Ebinger1ce9c432019-07-16 13:19:44 -07003724 } catch (ImsException e) {
3725 throw new ServiceSpecificException(e.getCode());
Brad Ebinger35c841c2018-10-01 10:40:55 -07003726 } finally {
3727 Binder.restoreCallingIdentity(identity);
3728 }
3729 }
3730
shilu366312e2019-12-17 09:28:10 -08003731 /**
3732 * Requires carrier privileges or READ_PRECISE_PHONE_STATE permission.
3733 * @param subId The subscription to use to check the configuration.
3734 */
Brad Ebinger35c841c2018-10-01 10:40:55 -07003735 @Override
3736 public int getVoWiFiModeSetting(int subId) {
Rambo Wang37f9c242020-02-10 14:45:28 -08003737 TelephonyPermissions.enforeceCallingOrSelfReadPrecisePhoneStatePermissionOrCarrierPrivilege(
3738 mApp, subId, "getVoWiFiModeSetting");
Brad Ebinger35c841c2018-10-01 10:40:55 -07003739 final long identity = Binder.clearCallingIdentity();
3740 try {
3741 // TODO: Refactor to remove ImsManager dependence and query through ImsPhone directly.
Nazanin Bakhshif782e562018-12-11 15:15:39 -08003742 return ImsManager.getInstance(mApp,
Brad Ebinger35c841c2018-10-01 10:40:55 -07003743 getSlotIndexOrException(subId)).getWfcMode(false /*isRoaming*/);
Brad Ebinger1ce9c432019-07-16 13:19:44 -07003744 } catch (ImsException e) {
3745 throw new ServiceSpecificException(e.getCode());
Brad Ebinger35c841c2018-10-01 10:40:55 -07003746 } finally {
3747 Binder.restoreCallingIdentity(identity);
3748 }
3749 }
3750
3751 @Override
3752 public void setVoWiFiModeSetting(int subId, int mode) {
3753 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(mApp, subId,
3754 "setVoWiFiModeSetting");
3755 final long identity = Binder.clearCallingIdentity();
3756 try {
3757 // TODO: Refactor to remove ImsManager dependence and query through ImsPhone directly.
Nazanin Bakhshif782e562018-12-11 15:15:39 -08003758 ImsManager.getInstance(mApp,
Brad Ebinger35c841c2018-10-01 10:40:55 -07003759 getSlotIndexOrException(subId)).setWfcMode(mode, false /*isRoaming*/);
Brad Ebinger1ce9c432019-07-16 13:19:44 -07003760 } catch (ImsException e) {
3761 throw new ServiceSpecificException(e.getCode());
Brad Ebinger35c841c2018-10-01 10:40:55 -07003762 } finally {
3763 Binder.restoreCallingIdentity(identity);
3764 }
3765 }
3766
3767 @Override
3768 public int getVoWiFiRoamingModeSetting(int subId) {
3769 enforceReadPrivilegedPermission("getVoWiFiRoamingModeSetting");
3770 final long identity = Binder.clearCallingIdentity();
3771 try {
3772 // TODO: Refactor to remove ImsManager dependence and query through ImsPhone directly.
Nazanin Bakhshif782e562018-12-11 15:15:39 -08003773 return ImsManager.getInstance(mApp,
Brad Ebinger35c841c2018-10-01 10:40:55 -07003774 getSlotIndexOrException(subId)).getWfcMode(true /*isRoaming*/);
Brad Ebinger1ce9c432019-07-16 13:19:44 -07003775 } catch (ImsException e) {
3776 throw new ServiceSpecificException(e.getCode());
Brad Ebinger35c841c2018-10-01 10:40:55 -07003777 } finally {
3778 Binder.restoreCallingIdentity(identity);
3779 }
3780 }
3781
3782 @Override
3783 public void setVoWiFiRoamingModeSetting(int subId, int mode) {
3784 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(mApp, subId,
3785 "setVoWiFiRoamingModeSetting");
3786 final long identity = Binder.clearCallingIdentity();
3787 try {
3788 // TODO: Refactor to remove ImsManager dependence and query through ImsPhone directly.
Nazanin Bakhshif782e562018-12-11 15:15:39 -08003789 ImsManager.getInstance(mApp,
Brad Ebinger35c841c2018-10-01 10:40:55 -07003790 getSlotIndexOrException(subId)).setWfcMode(mode, true /*isRoaming*/);
Brad Ebinger1ce9c432019-07-16 13:19:44 -07003791 } catch (ImsException e) {
3792 throw new ServiceSpecificException(e.getCode());
Brad Ebinger35c841c2018-10-01 10:40:55 -07003793 } finally {
3794 Binder.restoreCallingIdentity(identity);
3795 }
3796 }
3797
3798 @Override
3799 public void setRttCapabilitySetting(int subId, boolean isEnabled) {
3800 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(mApp, subId,
3801 "setRttCapabilityEnabled");
3802 final long identity = Binder.clearCallingIdentity();
3803 try {
3804 // TODO: Refactor to remove ImsManager dependence and query through ImsPhone directly.
Brad Ebinger1ce9c432019-07-16 13:19:44 -07003805 ImsManager.getInstance(mApp, getSlotIndexOrException(subId)).setRttEnabled(isEnabled);
3806 } catch (ImsException e) {
3807 throw new ServiceSpecificException(e.getCode());
Brad Ebinger35c841c2018-10-01 10:40:55 -07003808 } finally {
3809 Binder.restoreCallingIdentity(identity);
3810 }
3811 }
3812
shilu366312e2019-12-17 09:28:10 -08003813 /**
3814 * Requires carrier privileges or READ_PRECISE_PHONE_STATE permission.
3815 * @param subId The subscription to use to check the configuration.
3816 */
Brad Ebinger35c841c2018-10-01 10:40:55 -07003817 @Override
3818 public boolean isTtyOverVolteEnabled(int subId) {
Rambo Wang37f9c242020-02-10 14:45:28 -08003819 TelephonyPermissions.enforeceCallingOrSelfReadPrecisePhoneStatePermissionOrCarrierPrivilege(
3820 mApp, subId, "isTtyOverVolteEnabled");
Brad Ebinger35c841c2018-10-01 10:40:55 -07003821 final long identity = Binder.clearCallingIdentity();
3822 try {
3823 // TODO: Refactor to remove ImsManager dependence and query through ImsPhone directly.
Nazanin Bakhshif782e562018-12-11 15:15:39 -08003824 return ImsManager.getInstance(mApp,
Brad Ebinger35c841c2018-10-01 10:40:55 -07003825 getSlotIndexOrException(subId)).isTtyOnVoLteCapable();
Brad Ebinger1ce9c432019-07-16 13:19:44 -07003826 } catch (ImsException e) {
3827 throw new ServiceSpecificException(e.getCode());
Brad Ebinger35c841c2018-10-01 10:40:55 -07003828 } finally {
3829 Binder.restoreCallingIdentity(identity);
3830 }
3831 }
3832
Brad Ebingerdf5b4f02018-10-31 11:24:17 -07003833 @Override
3834 public void registerImsProvisioningChangedCallback(int subId, IImsConfigCallback callback) {
3835 enforceReadPrivilegedPermission("registerImsProvisioningChangedCallback");
3836 final long identity = Binder.clearCallingIdentity();
3837 try {
Brad Ebingerd0331732020-01-16 11:21:18 -08003838 if (!isImsAvailableOnDevice()) {
3839 throw new ServiceSpecificException(ImsException.CODE_ERROR_UNSUPPORTED_OPERATION,
3840 "IMS not available on device.");
Peter Wang44b186e2020-01-13 23:33:09 -08003841 }
Brad Ebingerdf5b4f02018-10-31 11:24:17 -07003842 // TODO: Refactor to remove ImsManager dependence and query through ImsPhone directly.
Nazanin Bakhshif782e562018-12-11 15:15:39 -08003843 ImsManager.getInstance(mApp, getSlotIndexOrException(subId))
Brad Ebinger4ae57f92019-01-09 16:51:30 -08003844 .addProvisioningCallbackForSubscription(callback, subId);
Brad Ebinger1ce9c432019-07-16 13:19:44 -07003845 } catch (ImsException e) {
3846 throw new ServiceSpecificException(e.getCode());
Brad Ebingerdf5b4f02018-10-31 11:24:17 -07003847 } finally {
3848 Binder.restoreCallingIdentity(identity);
3849 }
3850 }
3851
3852 @Override
3853 public void unregisterImsProvisioningChangedCallback(int subId, IImsConfigCallback callback) {
3854 enforceReadPrivilegedPermission("unregisterImsProvisioningChangedCallback");
3855 final long identity = Binder.clearCallingIdentity();
Brad Ebinger4ae57f92019-01-09 16:51:30 -08003856 if (!SubscriptionManager.isValidSubscriptionId(subId)) {
3857 throw new IllegalArgumentException("Invalid Subscription ID: " + subId);
3858 }
Brad Ebingerdf5b4f02018-10-31 11:24:17 -07003859 try {
3860 // TODO: Refactor to remove ImsManager dependence and query through ImsPhone directly.
Nazanin Bakhshif782e562018-12-11 15:15:39 -08003861 ImsManager.getInstance(mApp, getSlotIndexOrException(subId))
Brad Ebinger4ae57f92019-01-09 16:51:30 -08003862 .removeProvisioningCallbackForSubscription(callback, subId);
Brad Ebinger1ce9c432019-07-16 13:19:44 -07003863 } catch (ImsException e) {
Brad Ebinger4ae57f92019-01-09 16:51:30 -08003864 Log.i(LOG_TAG, "unregisterImsProvisioningChangedCallback: " + subId
3865 + "is inactive, ignoring unregister.");
3866 // If the subscription is no longer active, just return, since the callback will already
3867 // have been removed internally.
Brad Ebingerdf5b4f02018-10-31 11:24:17 -07003868 } finally {
3869 Binder.restoreCallingIdentity(identity);
3870 }
3871 }
3872
allenwtsu99c623b2020-01-03 18:24:23 +08003873
3874 private void checkModifyPhoneStatePermission(int subId, String message) {
3875 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(mApp, subId,
3876 message);
3877 }
3878
3879 private boolean isImsProvisioningRequired(int subId, int capability,
3880 boolean isMmtelCapability) {
3881 Phone phone = getPhone(subId);
3882 if (phone == null) {
3883 loge("phone instance null for subid " + subId);
3884 return false;
3885 }
3886 if (isMmtelCapability) {
3887 if (!doesImsCapabilityRequireProvisioning(phone.getContext(), subId, capability)) {
3888 return false;
3889 }
3890 } else {
3891 if (!doesRcsCapabilityRequireProvisioning(phone.getContext(), subId, capability)) {
3892 return false;
3893 }
3894 }
3895 return true;
3896 }
3897
3898 @Override
3899 public void setRcsProvisioningStatusForCapability(int subId, int capability,
3900 boolean isProvisioned) {
3901 checkModifyPhoneStatePermission(subId, "setRcsProvisioningStatusForCapability");
3902
3903 final long identity = Binder.clearCallingIdentity();
3904 try {
3905 // TODO: Refactor to remove ImsManager dependence and query through ImsPhone directly.
3906 if (!isImsProvisioningRequired(subId, capability, false)) {
3907 return;
3908 }
3909
3910 // this capability requires provisioning, route to the correct API.
3911 ImsManager ims = ImsManager.getInstance(mApp, getSlotIndex(subId));
3912 switch (capability) {
3913 case RcsFeature.RcsImsCapabilities.CAPABILITY_TYPE_OPTIONS_UCE:
3914 case RcsFeature.RcsImsCapabilities.CAPABILITY_TYPE_PRESENCE_UCE:
3915 ims.setEabProvisioned(isProvisioned);
3916 break;
3917 default: {
3918 throw new IllegalArgumentException("Tried to set provisioning for "
3919 + "rcs capability '" + capability + "', which does not require "
3920 + "provisioning.");
3921 }
3922 }
3923 } finally {
3924 Binder.restoreCallingIdentity(identity);
3925 }
3926
3927 }
3928
3929
3930 @Override
3931 public boolean getRcsProvisioningStatusForCapability(int subId, int capability) {
3932 enforceReadPrivilegedPermission("getRcsProvisioningStatusForCapability");
3933 final long identity = Binder.clearCallingIdentity();
3934 try {
3935 // TODO: Refactor to remove ImsManager dependence and query through ImsPhone directly.
3936 if (!isImsProvisioningRequired(subId, capability, false)) {
3937 return true;
3938 }
3939
3940 ImsManager ims = ImsManager.getInstance(mApp, getSlotIndex(subId));
3941 switch (capability) {
3942 case RcsFeature.RcsImsCapabilities.CAPABILITY_TYPE_OPTIONS_UCE:
3943 case RcsFeature.RcsImsCapabilities.CAPABILITY_TYPE_PRESENCE_UCE:
3944 return ims.isEabProvisionedOnDevice();
3945
3946 default: {
3947 throw new IllegalArgumentException("Tried to get rcs provisioning for "
3948 + "capability '" + capability + "', which does not require "
3949 + "provisioning.");
3950 }
3951 }
3952
3953 } finally {
3954 Binder.restoreCallingIdentity(identity);
3955 }
3956 }
3957
Brad Ebingerdf5b4f02018-10-31 11:24:17 -07003958 @Override
Brad Ebinger1c8542e2019-01-14 13:43:14 -08003959 public void setImsProvisioningStatusForCapability(int subId, int capability, int tech,
3960 boolean isProvisioned) {
3961 if (tech != ImsRegistrationImplBase.REGISTRATION_TECH_IWLAN
3962 && tech != ImsRegistrationImplBase.REGISTRATION_TECH_LTE) {
3963 throw new IllegalArgumentException("Registration technology '" + tech + "' is invalid");
3964 }
allenwtsu99c623b2020-01-03 18:24:23 +08003965 checkModifyPhoneStatePermission(subId, "setImsProvisioningStatusForCapability");
Brad Ebinger1c8542e2019-01-14 13:43:14 -08003966 final long identity = Binder.clearCallingIdentity();
3967 try {
3968 // TODO: Refactor to remove ImsManager dependence and query through ImsPhone directly.
allenwtsu99c623b2020-01-03 18:24:23 +08003969 if (!isImsProvisioningRequired(subId, capability, true)) {
Brad Ebinger1c8542e2019-01-14 13:43:14 -08003970 return;
3971 }
3972
3973 // this capability requires provisioning, route to the correct API.
3974 ImsManager ims = ImsManager.getInstance(mApp, getSlotIndex(subId));
3975 switch (capability) {
3976 case MmTelFeature.MmTelCapabilities.CAPABILITY_TYPE_VOICE: {
3977 if (tech == ImsRegistrationImplBase.REGISTRATION_TECH_LTE) {
3978 ims.setVolteProvisioned(isProvisioned);
3979 } else if (tech == ImsRegistrationImplBase.REGISTRATION_TECH_IWLAN) {
3980 ims.setWfcProvisioned(isProvisioned);
3981 }
3982 break;
3983 }
3984 case MmTelFeature.MmTelCapabilities.CAPABILITY_TYPE_VIDEO: {
3985 // There is currently no difference in VT provisioning type.
3986 ims.setVtProvisioned(isProvisioned);
3987 break;
3988 }
3989 case MmTelFeature.MmTelCapabilities.CAPABILITY_TYPE_UT: {
3990 // There is no "deprecated" UT provisioning mechanism through ImsConfig, so
3991 // change the capability of the feature instead if needed.
3992 if (isMmTelCapabilityProvisionedInCache(subId, capability, tech)
3993 == isProvisioned) {
3994 // No change in provisioning.
3995 return;
3996 }
3997 cacheMmTelCapabilityProvisioning(subId, capability, tech, isProvisioned);
3998 try {
3999 ims.changeMmTelCapability(capability, tech, isProvisioned);
Brad Ebinger1ce9c432019-07-16 13:19:44 -07004000 } catch (com.android.ims.ImsException e) {
Brad Ebinger1c8542e2019-01-14 13:43:14 -08004001 loge("setImsProvisioningStatusForCapability: couldn't change UT capability"
4002 + ", Exception" + e.getMessage());
4003 }
4004 break;
4005 }
4006 default: {
allenwtsu99c623b2020-01-03 18:24:23 +08004007 throw new IllegalArgumentException("Tried to set provisioning for "
4008 + "MmTel capability '" + capability + "', which does not require "
4009 + "provisioning. ");
Brad Ebinger1c8542e2019-01-14 13:43:14 -08004010 }
4011 }
4012
4013 } finally {
4014 Binder.restoreCallingIdentity(identity);
4015 }
4016 }
4017
4018 @Override
4019 public boolean getImsProvisioningStatusForCapability(int subId, int capability, int tech) {
4020 if (tech != ImsRegistrationImplBase.REGISTRATION_TECH_IWLAN
4021 && tech != ImsRegistrationImplBase.REGISTRATION_TECH_LTE) {
4022 throw new IllegalArgumentException("Registration technology '" + tech + "' is invalid");
4023 }
4024 enforceReadPrivilegedPermission("getProvisioningStatusForCapability");
4025 final long identity = Binder.clearCallingIdentity();
4026 try {
4027 // TODO: Refactor to remove ImsManager dependence and query through ImsPhone directly.
allenwtsu99c623b2020-01-03 18:24:23 +08004028 if (!isImsProvisioningRequired(subId, capability, true)) {
Brad Ebinger1c8542e2019-01-14 13:43:14 -08004029 return true;
4030 }
4031
4032 ImsManager ims = ImsManager.getInstance(mApp, getSlotIndex(subId));
4033 switch (capability) {
4034 case MmTelFeature.MmTelCapabilities.CAPABILITY_TYPE_VOICE: {
4035 if (tech == ImsRegistrationImplBase.REGISTRATION_TECH_LTE) {
4036 return ims.isVolteProvisionedOnDevice();
4037 } else if (tech == ImsRegistrationImplBase.REGISTRATION_TECH_IWLAN) {
4038 return ims.isWfcProvisionedOnDevice();
4039 }
4040 // This should never happen, since we are checking tech above to make sure it
4041 // is either LTE or IWLAN.
4042 throw new IllegalArgumentException("Invalid radio technology for voice "
4043 + "capability.");
4044 }
4045 case MmTelFeature.MmTelCapabilities.CAPABILITY_TYPE_VIDEO: {
4046 // There is currently no difference in VT provisioning type.
4047 return ims.isVtProvisionedOnDevice();
4048 }
4049 case MmTelFeature.MmTelCapabilities.CAPABILITY_TYPE_UT: {
4050 // There is no "deprecated" UT provisioning mechanism, so get from shared prefs.
4051 return isMmTelCapabilityProvisionedInCache(subId, capability, tech);
4052 }
4053 default: {
allenwtsu99c623b2020-01-03 18:24:23 +08004054 throw new IllegalArgumentException(
4055 "Tried to get provisioning for MmTel capability '" + capability
4056 + "', which does not require provisioning.");
Brad Ebinger1c8542e2019-01-14 13:43:14 -08004057 }
4058 }
4059
4060 } finally {
4061 Binder.restoreCallingIdentity(identity);
4062 }
4063 }
4064
4065 @Override
4066 public boolean isMmTelCapabilityProvisionedInCache(int subId, int capability, int tech) {
4067 if (tech != ImsRegistrationImplBase.REGISTRATION_TECH_IWLAN
4068 && tech != ImsRegistrationImplBase.REGISTRATION_TECH_LTE) {
4069 throw new IllegalArgumentException("Registration technology '" + tech + "' is invalid");
4070 }
4071 enforceReadPrivilegedPermission("isMmTelCapabilityProvisionedInCache");
4072 int provisionedBits = getMmTelCapabilityProvisioningBitfield(subId, tech);
4073 return (provisionedBits & capability) > 0;
4074 }
4075
4076 @Override
4077 public void cacheMmTelCapabilityProvisioning(int subId, int capability, int tech,
4078 boolean isProvisioned) {
4079 if (tech != ImsRegistrationImplBase.REGISTRATION_TECH_IWLAN
4080 && tech != ImsRegistrationImplBase.REGISTRATION_TECH_LTE) {
4081 throw new IllegalArgumentException("Registration technology '" + tech + "' is invalid");
4082 }
4083 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(mApp, subId,
4084 "setProvisioningStatusForCapability");
4085 int provisionedBits = getMmTelCapabilityProvisioningBitfield(subId, tech);
4086 // If the current provisioning status for capability already matches isProvisioned,
4087 // do nothing.
4088 if (((provisionedBits & capability) > 0) == isProvisioned) {
4089 return;
4090 }
4091 if (isProvisioned) {
4092 setMmTelCapabilityProvisioningBitfield(subId, tech, (provisionedBits | capability));
4093 } else {
4094 setMmTelCapabilityProvisioningBitfield(subId, tech, (provisionedBits & ~capability));
4095 }
4096 }
4097
4098 /**
4099 * @return the bitfield containing the MmTel provisioning for the provided subscription and
4100 * technology. The bitfield should mirror the bitfield defined by
4101 * {@link MmTelFeature.MmTelCapabilities.MmTelCapability}.
4102 */
4103 private int getMmTelCapabilityProvisioningBitfield(int subId, int tech) {
4104 String key = getMmTelProvisioningKey(subId, tech);
4105 // Default is no capabilities are provisioned.
4106 return mTelephonySharedPreferences.getInt(key, 0 /*default*/);
4107 }
4108
4109 /**
4110 * Sets the MmTel capability provisioning bitfield (defined by
4111 * {@link MmTelFeature.MmTelCapabilities.MmTelCapability}) for the subscription and
4112 * technology specified.
4113 *
4114 * Note: This is a synchronous command and should not be called on UI thread.
4115 */
4116 private void setMmTelCapabilityProvisioningBitfield(int subId, int tech, int newField) {
4117 final SharedPreferences.Editor editor = mTelephonySharedPreferences.edit();
4118 String key = getMmTelProvisioningKey(subId, tech);
4119 editor.putInt(key, newField);
4120 editor.commit();
4121 }
4122
4123 private static String getMmTelProvisioningKey(int subId, int tech) {
4124 // resulting key is provision_ims_mmtel_{subId}_{tech}
4125 return PREF_PROVISION_IMS_MMTEL_PREFIX + subId + "_" + tech;
4126 }
4127
4128 /**
4129 * Query CarrierConfig to see if the specified capability requires provisioning for the
4130 * carrier associated with the subscription id.
4131 */
4132 private boolean doesImsCapabilityRequireProvisioning(Context context, int subId,
4133 int capability) {
4134 CarrierConfigManager configManager = new CarrierConfigManager(context);
4135 PersistableBundle c = configManager.getConfigForSubId(subId);
4136 boolean requireUtProvisioning = c.getBoolean(
Brad Ebinger076903f2019-05-13 10:00:22 -07004137 CarrierConfigManager.KEY_CARRIER_SUPPORTS_SS_OVER_UT_BOOL, false)
Brad Ebinger1c8542e2019-01-14 13:43:14 -08004138 && c.getBoolean(CarrierConfigManager.KEY_CARRIER_UT_PROVISIONING_REQUIRED_BOOL,
4139 false);
4140 boolean requireVoiceVtProvisioning = c.getBoolean(
4141 CarrierConfigManager.KEY_CARRIER_VOLTE_PROVISIONING_REQUIRED_BOOL, false);
4142
4143 // First check to make sure that the capability requires provisioning.
4144 switch (capability) {
4145 case MmTelFeature.MmTelCapabilities.CAPABILITY_TYPE_VOICE:
4146 // intentional fallthrough
4147 case MmTelFeature.MmTelCapabilities.CAPABILITY_TYPE_VIDEO: {
4148 if (requireVoiceVtProvisioning) {
4149 // Voice and Video requires provisioning
4150 return true;
4151 }
4152 break;
4153 }
4154 case MmTelFeature.MmTelCapabilities.CAPABILITY_TYPE_UT: {
4155 if (requireUtProvisioning) {
4156 // UT requires provisioning
4157 return true;
4158 }
4159 break;
4160 }
4161 }
4162 return false;
4163 }
4164
allenwtsu99c623b2020-01-03 18:24:23 +08004165 private boolean doesRcsCapabilityRequireProvisioning(Context context, int subId,
4166 int capability) {
4167 CarrierConfigManager configManager = new CarrierConfigManager(context);
4168 PersistableBundle c = configManager.getConfigForSubId(subId);
4169
4170 boolean requireRcsProvisioning = c.getBoolean(
4171 CarrierConfigManager.KEY_CARRIER_RCS_PROVISIONING_REQUIRED_BOOL, false);
4172
4173 // First check to make sure that the capability requires provisioning.
4174 switch (capability) {
4175 case RcsFeature.RcsImsCapabilities.CAPABILITY_TYPE_PRESENCE_UCE:
4176 // intentional fallthrough
4177 case RcsFeature.RcsImsCapabilities.CAPABILITY_TYPE_OPTIONS_UCE: {
4178 if (requireRcsProvisioning) {
4179 // OPTION or PRESENCE requires provisioning
4180 return true;
4181 }
4182 break;
4183 }
4184 }
4185 return false;
4186 }
4187
Brad Ebinger1c8542e2019-01-14 13:43:14 -08004188 @Override
Brad Ebingerdf5b4f02018-10-31 11:24:17 -07004189 public int getImsProvisioningInt(int subId, int key) {
Brad Ebinger1c8542e2019-01-14 13:43:14 -08004190 if (!SubscriptionManager.isValidSubscriptionId(subId)) {
4191 throw new IllegalArgumentException("Invalid Subscription id '" + subId + "'");
4192 }
Brad Ebingerdf5b4f02018-10-31 11:24:17 -07004193 enforceReadPrivilegedPermission("getImsProvisioningInt");
4194 final long identity = Binder.clearCallingIdentity();
4195 try {
4196 // TODO: Refactor to remove ImsManager dependence and query through ImsPhone directly.
Brad Ebinger1c8542e2019-01-14 13:43:14 -08004197 int slotId = getSlotIndex(subId);
4198 if (slotId <= SubscriptionManager.INVALID_SIM_SLOT_INDEX) {
4199 Log.w(LOG_TAG, "getImsProvisioningInt: called with an inactive subscription '"
4200 + subId + "' for key:" + key);
4201 return ImsConfigImplBase.CONFIG_RESULT_UNKNOWN;
4202 }
4203 return ImsManager.getInstance(mApp, slotId).getConfigInterface().getConfigInt(key);
Brad Ebinger1ce9c432019-07-16 13:19:44 -07004204 } catch (com.android.ims.ImsException e) {
Brad Ebinger1c8542e2019-01-14 13:43:14 -08004205 Log.w(LOG_TAG, "getImsProvisioningInt: ImsService is not available for subscription '"
4206 + subId + "' for key:" + key);
4207 return ImsConfigImplBase.CONFIG_RESULT_UNKNOWN;
Brad Ebingerdf5b4f02018-10-31 11:24:17 -07004208 } finally {
4209 Binder.restoreCallingIdentity(identity);
4210 }
4211 }
4212
4213 @Override
4214 public String getImsProvisioningString(int subId, int key) {
Brad Ebinger1c8542e2019-01-14 13:43:14 -08004215 if (!SubscriptionManager.isValidSubscriptionId(subId)) {
4216 throw new IllegalArgumentException("Invalid Subscription id '" + subId + "'");
4217 }
Brad Ebingerdf5b4f02018-10-31 11:24:17 -07004218 enforceReadPrivilegedPermission("getImsProvisioningString");
4219 final long identity = Binder.clearCallingIdentity();
4220 try {
4221 // TODO: Refactor to remove ImsManager dependence and query through ImsPhone directly.
Brad Ebinger1c8542e2019-01-14 13:43:14 -08004222 int slotId = getSlotIndex(subId);
4223 if (slotId <= SubscriptionManager.INVALID_SIM_SLOT_INDEX) {
4224 Log.w(LOG_TAG, "getImsProvisioningString: called for an inactive subscription id '"
4225 + subId + "' for key:" + key);
4226 return ProvisioningManager.STRING_QUERY_RESULT_ERROR_GENERIC;
4227 }
4228 return ImsManager.getInstance(mApp, slotId).getConfigInterface().getConfigString(key);
Brad Ebinger1ce9c432019-07-16 13:19:44 -07004229 } catch (com.android.ims.ImsException e) {
Brad Ebinger1c8542e2019-01-14 13:43:14 -08004230 Log.w(LOG_TAG, "getImsProvisioningString: ImsService is not available for sub '"
4231 + subId + "' for key:" + key);
4232 return ProvisioningManager.STRING_QUERY_RESULT_ERROR_NOT_READY;
Brad Ebingerdf5b4f02018-10-31 11:24:17 -07004233 } finally {
4234 Binder.restoreCallingIdentity(identity);
4235 }
4236 }
4237
4238 @Override
4239 public int setImsProvisioningInt(int subId, int key, int value) {
Brad Ebinger1c8542e2019-01-14 13:43:14 -08004240 if (!SubscriptionManager.isValidSubscriptionId(subId)) {
4241 throw new IllegalArgumentException("Invalid Subscription id '" + subId + "'");
4242 }
Brad Ebinger3d0b34e2018-11-15 14:13:12 -08004243 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(mApp, subId,
4244 "setImsProvisioningInt");
Brad Ebingerdf5b4f02018-10-31 11:24:17 -07004245 final long identity = Binder.clearCallingIdentity();
4246 try {
4247 // TODO: Refactor to remove ImsManager dependence and query through ImsPhone directly.
Brad Ebinger1c8542e2019-01-14 13:43:14 -08004248 int slotId = getSlotIndex(subId);
4249 if (slotId <= SubscriptionManager.INVALID_SIM_SLOT_INDEX) {
4250 Log.w(LOG_TAG, "setImsProvisioningInt: called with an inactive subscription id '"
4251 + subId + "' for key:" + key);
4252 return ImsConfigImplBase.CONFIG_RESULT_FAILED;
4253 }
4254 return ImsManager.getInstance(mApp, slotId).getConfigInterface().setConfig(key, value);
Brad Ebinger1ce9c432019-07-16 13:19:44 -07004255 } catch (com.android.ims.ImsException e) {
Brad Ebinger1c8542e2019-01-14 13:43:14 -08004256 Log.w(LOG_TAG, "setImsProvisioningInt: ImsService unavailable for sub '" + subId
4257 + "' for key:" + key);
4258 return ImsConfigImplBase.CONFIG_RESULT_FAILED;
Brad Ebingerdf5b4f02018-10-31 11:24:17 -07004259 } finally {
4260 Binder.restoreCallingIdentity(identity);
4261 }
4262 }
4263
4264 @Override
4265 public int setImsProvisioningString(int subId, int key, String value) {
Brad Ebinger1c8542e2019-01-14 13:43:14 -08004266 if (!SubscriptionManager.isValidSubscriptionId(subId)) {
4267 throw new IllegalArgumentException("Invalid Subscription id '" + subId + "'");
4268 }
Brad Ebinger3d0b34e2018-11-15 14:13:12 -08004269 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(mApp, subId,
4270 "setImsProvisioningString");
Brad Ebingerdf5b4f02018-10-31 11:24:17 -07004271 final long identity = Binder.clearCallingIdentity();
4272 try {
4273 // TODO: Refactor to remove ImsManager dependence and query through ImsPhone directly.
Brad Ebinger1c8542e2019-01-14 13:43:14 -08004274 int slotId = getSlotIndex(subId);
4275 if (slotId <= SubscriptionManager.INVALID_SIM_SLOT_INDEX) {
4276 Log.w(LOG_TAG, "setImsProvisioningString: called with an inactive subscription id '"
4277 + subId + "' for key:" + key);
4278 return ImsConfigImplBase.CONFIG_RESULT_FAILED;
4279 }
4280 return ImsManager.getInstance(mApp, slotId).getConfigInterface().setConfig(key, value);
Brad Ebinger1ce9c432019-07-16 13:19:44 -07004281 } catch (com.android.ims.ImsException e) {
Brad Ebinger1c8542e2019-01-14 13:43:14 -08004282 Log.w(LOG_TAG, "setImsProvisioningString: ImsService unavailable for sub '" + subId
4283 + "' for key:" + key);
4284 return ImsConfigImplBase.CONFIG_RESULT_FAILED;
Brad Ebingerdf5b4f02018-10-31 11:24:17 -07004285 } finally {
4286 Binder.restoreCallingIdentity(identity);
4287 }
4288 }
4289
Brad Ebinger1ce9c432019-07-16 13:19:44 -07004290 private int getSlotIndexOrException(int subId) throws ImsException {
Brad Ebinger35c841c2018-10-01 10:40:55 -07004291 int slotId = SubscriptionManager.getSlotIndex(subId);
4292 if (!SubscriptionManager.isValidSlotIndex(slotId)) {
Brad Ebinger1ce9c432019-07-16 13:19:44 -07004293 throw new ImsException("Invalid Subscription Id, subId=" + subId,
4294 ImsException.CODE_ERROR_INVALID_SUBSCRIPTION);
Brad Ebinger35c841c2018-10-01 10:40:55 -07004295 }
4296 return slotId;
4297 }
4298
Brad Ebinger1c8542e2019-01-14 13:43:14 -08004299 private int getSlotIndex(int subId) {
4300 int slotId = SubscriptionManager.getSlotIndex(subId);
4301 if (!SubscriptionManager.isValidSlotIndex(slotId)) {
4302 return SubscriptionManager.INVALID_SIM_SLOT_INDEX;
4303 }
4304 return slotId;
4305 }
4306
Wink Saville36469e72014-06-11 15:17:00 -07004307 /**
Nathan Harold9042f0b2019-05-21 15:51:27 -07004308 * Returns the data network type for a subId; does not throw SecurityException.
Wink Saville36469e72014-06-11 15:17:00 -07004309 */
4310 @Override
Philip P. Moltmann700a9592019-10-03 11:53:50 -07004311 public int getNetworkTypeForSubscriber(int subId, String callingPackage,
4312 String callingFeatureId) {
Nathan Haroldef60dba2019-05-22 13:55:14 -07004313 final int targetSdk = getTargetSdk(callingPackage);
4314 if (targetSdk > android.os.Build.VERSION_CODES.Q) {
Philip P. Moltmann700a9592019-10-03 11:53:50 -07004315 return getDataNetworkTypeForSubscriber(subId, callingPackage, callingFeatureId);
Nathan Haroldef60dba2019-05-22 13:55:14 -07004316 } else if (targetSdk == android.os.Build.VERSION_CODES.Q
Nathan Harold9042f0b2019-05-21 15:51:27 -07004317 && !TelephonyPermissions.checkCallingOrSelfReadPhoneStateNoThrow(
Philip P. Moltmann700a9592019-10-03 11:53:50 -07004318 mApp, subId, callingPackage, callingFeatureId,
4319 "getNetworkTypeForSubscriber")) {
Robert Greenwalt36b23af2015-07-06 17:59:14 -07004320 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
4321 }
Robert Greenwalta5dcfcb2015-07-10 09:06:29 -07004322
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004323 final long identity = Binder.clearCallingIdentity();
4324 try {
4325 final Phone phone = getPhone(subId);
4326 if (phone != null) {
4327 return phone.getServiceState().getDataNetworkType();
4328 } else {
4329 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
4330 }
4331 } finally {
4332 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07004333 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07004334 }
4335
4336 /**
4337 * Returns the data network type
4338 */
4339 @Override
Philip P. Moltmann700a9592019-10-03 11:53:50 -07004340 public int getDataNetworkType(String callingPackage, String callingFeatureId) {
4341 return getDataNetworkTypeForSubscriber(getDefaultSubscription(), callingPackage,
4342 callingFeatureId);
Wink Saville36469e72014-06-11 15:17:00 -07004343 }
4344
4345 /**
4346 * Returns the data network type for a subId
4347 */
4348 @Override
Philip P. Moltmann700a9592019-10-03 11:53:50 -07004349 public int getDataNetworkTypeForSubscriber(int subId, String callingPackage,
4350 String callingFeatureId) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08004351 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
Philip P. Moltmann700a9592019-10-03 11:53:50 -07004352 mApp, subId, callingPackage, callingFeatureId,
4353 "getDataNetworkTypeForSubscriber")) {
Robert Greenwalt36b23af2015-07-06 17:59:14 -07004354 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
4355 }
4356
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004357 final long identity = Binder.clearCallingIdentity();
4358 try {
4359 final Phone phone = getPhone(subId);
4360 if (phone != null) {
4361 return phone.getServiceState().getDataNetworkType();
4362 } else {
4363 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
4364 }
4365 } finally {
4366 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07004367 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07004368 }
4369
4370 /**
Wink Saville36469e72014-06-11 15:17:00 -07004371 * Returns the Voice network type for a subId
4372 */
4373 @Override
Philip P. Moltmann700a9592019-10-03 11:53:50 -07004374 public int getVoiceNetworkTypeForSubscriber(int subId, String callingPackage,
4375 String callingFeatureId) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08004376 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
Philip P. Moltmann700a9592019-10-03 11:53:50 -07004377 mApp, subId, callingPackage, callingFeatureId,
4378 "getDataNetworkTypeForSubscriber")) {
Robert Greenwalta5dcfcb2015-07-10 09:06:29 -07004379 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
4380 }
4381
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004382 final long identity = Binder.clearCallingIdentity();
4383 try {
4384 final Phone phone = getPhone(subId);
4385 if (phone != null) {
4386 return phone.getServiceState().getVoiceNetworkType();
4387 } else {
4388 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
4389 }
4390 } finally {
4391 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07004392 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07004393 }
4394
4395 /**
4396 * @return true if a ICC card is present
4397 */
4398 public boolean hasIccCard() {
Wink Saville36469e72014-06-11 15:17:00 -07004399 // FIXME Make changes to pass defaultSimId of type int
Sanket Padawe13bac7b2017-03-20 15:04:47 -07004400 return hasIccCardUsingSlotIndex(mSubscriptionController.getSlotIndex(
4401 getDefaultSubscription()));
Wink Saville36469e72014-06-11 15:17:00 -07004402 }
4403
4404 /**
Sanket Padawe13bac7b2017-03-20 15:04:47 -07004405 * @return true if a ICC card is present for a slotIndex
Wink Saville36469e72014-06-11 15:17:00 -07004406 */
Sanket Padawe356d7632015-06-22 14:03:32 -07004407 @Override
Sanket Padawe13bac7b2017-03-20 15:04:47 -07004408 public boolean hasIccCardUsingSlotIndex(int slotIndex) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004409 final long identity = Binder.clearCallingIdentity();
4410 try {
4411 final Phone phone = PhoneFactory.getPhone(slotIndex);
4412 if (phone != null) {
4413 return phone.getIccCard().hasIccCard();
4414 } else {
4415 return false;
4416 }
4417 } finally {
4418 Binder.restoreCallingIdentity(identity);
Amit Mahajana6fc2a82015-01-06 11:53:51 -08004419 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07004420 }
4421
4422 /**
4423 * Return if the current radio is LTE on CDMA. This
4424 * is a tri-state return value as for a period of time
4425 * the mode may be unknown.
4426 *
Robert Greenwalt36b23af2015-07-06 17:59:14 -07004427 * @param callingPackage the name of the package making the call.
Santos Cordon7d4ddf62013-07-10 11:58:08 -07004428 * @return {@link Phone#LTE_ON_CDMA_UNKNOWN}, {@link Phone#LTE_ON_CDMA_FALSE}
Jake Hambye994d462014-02-03 13:10:13 -08004429 * or {@link Phone#LTE_ON_CDMA_TRUE}
Santos Cordon7d4ddf62013-07-10 11:58:08 -07004430 */
Robert Greenwalt36b23af2015-07-06 17:59:14 -07004431 @Override
Philip P. Moltmann700a9592019-10-03 11:53:50 -07004432 public int getLteOnCdmaMode(String callingPackage, String callingFeatureId) {
4433 return getLteOnCdmaModeForSubscriber(getDefaultSubscription(), callingPackage,
4434 callingFeatureId);
Wink Saville36469e72014-06-11 15:17:00 -07004435 }
4436
Sanket Padawe356d7632015-06-22 14:03:32 -07004437 @Override
Philip P. Moltmann700a9592019-10-03 11:53:50 -07004438 public int getLteOnCdmaModeForSubscriber(int subId, String callingPackage,
4439 String callingFeatureId) {
Sarah Chin790d2922020-01-16 12:17:23 -08004440 try {
4441 enforceReadPrivilegedPermission("getLteOnCdmaModeForSubscriber");
4442 } catch (SecurityException e) {
Robert Greenwalt36b23af2015-07-06 17:59:14 -07004443 return PhoneConstants.LTE_ON_CDMA_UNKNOWN;
4444 }
4445
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004446 final long identity = Binder.clearCallingIdentity();
4447 try {
4448 final Phone phone = getPhone(subId);
4449 if (phone == null) {
4450 return PhoneConstants.LTE_ON_CDMA_UNKNOWN;
4451 } else {
Nathan Harold05ad6332020-07-10 11:54:36 -07004452 return TelephonyProperties.lte_on_cdma_device()
4453 .orElse(PhoneConstants.LTE_ON_CDMA_FALSE);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004454 }
4455 } finally {
4456 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07004457 }
Wink Saville36469e72014-06-11 15:17:00 -07004458 }
4459
Wink Saville36469e72014-06-11 15:17:00 -07004460 /**
4461 * {@hide}
4462 * Returns Default subId, 0 in the case of single standby.
4463 */
Wink Savilleb564aae2014-10-23 10:18:09 -07004464 private int getDefaultSubscription() {
Wink Savilleac1bdfd2014-11-20 23:04:44 -08004465 return mSubscriptionController.getDefaultSubId();
Wink Saville36469e72014-06-11 15:17:00 -07004466 }
4467
Shishir Agrawala9f32182016-04-12 12:00:16 -07004468 private int getSlotForDefaultSubscription() {
4469 return mSubscriptionController.getPhoneId(getDefaultSubscription());
4470 }
4471
Wink Savilleb564aae2014-10-23 10:18:09 -07004472 private int getPreferredVoiceSubscription() {
Wink Savilleac1bdfd2014-11-20 23:04:44 -08004473 return mSubscriptionController.getDefaultVoiceSubId();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07004474 }
Ihab Awadf2177b72013-11-25 13:33:23 -08004475
Pengquan Menge92a50d2018-09-21 15:54:48 -07004476 private boolean isActiveSubscription(int subId) {
4477 return mSubscriptionController.isActiveSubId(subId);
4478 }
4479
Ihab Awadf2177b72013-11-25 13:33:23 -08004480 /**
4481 * @see android.telephony.TelephonyManager.WifiCallingChoices
4482 */
4483 public int getWhenToMakeWifiCalls() {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004484 final long identity = Binder.clearCallingIdentity();
4485 try {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08004486 return Settings.System.getInt(mApp.getContentResolver(),
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004487 Settings.System.WHEN_TO_MAKE_WIFI_CALLS,
4488 getWhenToMakeWifiCallsDefaultPreference());
4489 } finally {
4490 Binder.restoreCallingIdentity(identity);
4491 }
Ihab Awadf2177b72013-11-25 13:33:23 -08004492 }
4493
4494 /**
4495 * @see android.telephony.TelephonyManager.WifiCallingChoices
4496 */
4497 public void setWhenToMakeWifiCalls(int preference) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004498 final long identity = Binder.clearCallingIdentity();
4499 try {
4500 if (DBG) log("setWhenToMakeWifiCallsStr, storing setting = " + preference);
Nazanin Bakhshif782e562018-12-11 15:15:39 -08004501 Settings.System.putInt(mApp.getContentResolver(),
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004502 Settings.System.WHEN_TO_MAKE_WIFI_CALLS, preference);
4503 } finally {
4504 Binder.restoreCallingIdentity(identity);
4505 }
Ihab Awadf9e92732013-12-05 18:02:52 -08004506 }
4507
Sailesh Nepald1e68152013-12-12 19:08:02 -08004508 private static int getWhenToMakeWifiCallsDefaultPreference() {
Santos Cordonda120f42014-08-06 04:44:34 -07004509 // TODO: Use a build property to choose this value.
Evan Charlton9829e882013-12-19 15:30:38 -08004510 return TelephonyManager.WifiCallingChoices.ALWAYS_USE;
Ihab Awadf2177b72013-11-25 13:33:23 -08004511 }
Shishir Agrawal69f68122013-12-16 17:25:49 -08004512
Jordan Liu4c733742019-02-28 12:03:40 -08004513 private Phone getPhoneFromSlotIdOrThrowException(int slotIndex) {
4514 int phoneId = UiccController.getInstance().getPhoneIdFromSlotId(slotIndex);
4515 if (phoneId == -1) {
4516 throw new IllegalArgumentException("Given slot index: " + slotIndex
4517 + " does not correspond to an active phone");
4518 }
4519 return PhoneFactory.getPhone(phoneId);
4520 }
4521
Shishir Agrawal566b7612013-10-28 14:41:00 -07004522 @Override
Derek Tan740e1672017-06-27 14:56:27 -07004523 public IccOpenLogicalChannelResponse iccOpenLogicalChannel(
4524 int subId, String callingPackage, String aid, int p2) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08004525 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
4526 mApp, subId, "iccOpenLogicalChannel");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004527 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
Jordan Liu4c733742019-02-28 12:03:40 -08004528 if (DBG) {
4529 log("iccOpenLogicalChannel: subId=" + subId + " aid=" + aid + " p2=" + p2);
4530 }
4531 return iccOpenLogicalChannelWithPermission(getPhoneFromSubId(subId), callingPackage, aid,
4532 p2);
4533 }
Shishir Agrawal566b7612013-10-28 14:41:00 -07004534
Jordan Liu4c733742019-02-28 12:03:40 -08004535
4536 @Override
4537 public IccOpenLogicalChannelResponse iccOpenLogicalChannelBySlot(
4538 int slotIndex, String callingPackage, String aid, int p2) {
4539 enforceModifyPermission();
4540 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
4541 if (DBG) {
4542 log("iccOpenLogicalChannelBySlot: slot=" + slotIndex + " aid=" + aid + " p2=" + p2);
4543 }
4544 return iccOpenLogicalChannelWithPermission(getPhoneFromSlotIdOrThrowException(slotIndex),
4545 callingPackage, aid, p2);
4546 }
4547
4548 private IccOpenLogicalChannelResponse iccOpenLogicalChannelWithPermission(Phone phone,
4549 String callingPackage, String aid, int p2) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004550 final long identity = Binder.clearCallingIdentity();
4551 try {
4552 if (TextUtils.equals(ISDR_AID, aid)) {
4553 // Only allows LPA to open logical channel to ISD-R.
Nazanin Bakhshif782e562018-12-11 15:15:39 -08004554 ComponentInfo bestComponent = EuiccConnector.findBestComponent(getDefaultPhone()
4555 .getContext().getPackageManager());
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004556 if (bestComponent == null
4557 || !TextUtils.equals(callingPackage, bestComponent.packageName)) {
4558 loge("The calling package is not allowed to access ISD-R.");
4559 throw new SecurityException(
4560 "The calling package is not allowed to access ISD-R.");
4561 }
Derek Tan740e1672017-06-27 14:56:27 -07004562 }
Derek Tan740e1672017-06-27 14:56:27 -07004563
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004564 IccOpenLogicalChannelResponse response = (IccOpenLogicalChannelResponse) sendRequest(
Jordan Liu4c733742019-02-28 12:03:40 -08004565 CMD_OPEN_CHANNEL, new Pair<String, Integer>(aid, p2), phone,
4566 null /* workSource */);
4567 if (DBG) log("iccOpenLogicalChannelWithPermission: " + response);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004568 return response;
4569 } finally {
4570 Binder.restoreCallingIdentity(identity);
4571 }
Shishir Agrawal566b7612013-10-28 14:41:00 -07004572 }
4573
4574 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08004575 public boolean iccCloseLogicalChannel(int subId, int channel) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08004576 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
4577 mApp, subId, "iccCloseLogicalChannel");
Jordan Liu4c733742019-02-28 12:03:40 -08004578 if (DBG) log("iccCloseLogicalChannel: subId=" + subId + " chnl=" + channel);
4579 return iccCloseLogicalChannelWithPermission(getPhoneFromSubId(subId), channel);
4580 }
Shishir Agrawal566b7612013-10-28 14:41:00 -07004581
Jordan Liu4c733742019-02-28 12:03:40 -08004582 @Override
4583 public boolean iccCloseLogicalChannelBySlot(int slotIndex, int channel) {
4584 enforceModifyPermission();
4585 if (DBG) log("iccCloseLogicalChannelBySlot: slotIndex=" + slotIndex + " chnl=" + channel);
4586 return iccCloseLogicalChannelWithPermission(getPhoneFromSlotIdOrThrowException(slotIndex),
4587 channel);
4588 }
4589
4590 private boolean iccCloseLogicalChannelWithPermission(Phone phone, int channel) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004591 final long identity = Binder.clearCallingIdentity();
4592 try {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004593 if (channel < 0) {
4594 return false;
4595 }
Jordan Liu4c733742019-02-28 12:03:40 -08004596 Boolean success = (Boolean) sendRequest(CMD_CLOSE_CHANNEL, channel, phone,
4597 null /* workSource */);
4598 if (DBG) log("iccCloseLogicalChannelWithPermission: " + success);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004599 return success;
4600 } finally {
4601 Binder.restoreCallingIdentity(identity);
Shishir Agrawal566b7612013-10-28 14:41:00 -07004602 }
Shishir Agrawal566b7612013-10-28 14:41:00 -07004603 }
4604
4605 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08004606 public String iccTransmitApduLogicalChannel(int subId, int channel, int cla,
Shishir Agrawal566b7612013-10-28 14:41:00 -07004607 int command, int p1, int p2, int p3, String data) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08004608 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
4609 mApp, subId, "iccTransmitApduLogicalChannel");
Jordan Liu4c733742019-02-28 12:03:40 -08004610 if (DBG) {
4611 log("iccTransmitApduLogicalChannel: subId=" + subId + " chnl=" + channel
4612 + " cla=" + cla + " cmd=" + command + " p1=" + p1 + " p2=" + p2 + " p3="
4613 + p3 + " data=" + data);
4614 }
4615 return iccTransmitApduLogicalChannelWithPermission(getPhoneFromSubId(subId), channel, cla,
4616 command, p1, p2, p3, data);
4617 }
Shishir Agrawal566b7612013-10-28 14:41:00 -07004618
Jordan Liu4c733742019-02-28 12:03:40 -08004619 @Override
4620 public String iccTransmitApduLogicalChannelBySlot(int slotIndex, int channel, int cla,
4621 int command, int p1, int p2, int p3, String data) {
4622 enforceModifyPermission();
4623 if (DBG) {
4624 log("iccTransmitApduLogicalChannelBySlot: slotIndex=" + slotIndex + " chnl=" + channel
4625 + " cla=" + cla + " cmd=" + command + " p1=" + p1 + " p2=" + p2 + " p3="
4626 + p3 + " data=" + data);
4627 }
4628 return iccTransmitApduLogicalChannelWithPermission(
4629 getPhoneFromSlotIdOrThrowException(slotIndex), channel, cla, command, p1, p2, p3,
4630 data);
4631 }
4632
4633 private String iccTransmitApduLogicalChannelWithPermission(Phone phone, int channel, int cla,
4634 int command, int p1, int p2, int p3, String data) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004635 final long identity = Binder.clearCallingIdentity();
4636 try {
Hall Liu4fd771b2019-05-02 09:16:29 -07004637 if (channel <= 0) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004638 return "";
4639 }
Shishir Agrawal566b7612013-10-28 14:41:00 -07004640
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004641 IccIoResult response = (IccIoResult) sendRequest(CMD_TRANSMIT_APDU_LOGICAL_CHANNEL,
Jordan Liu4c733742019-02-28 12:03:40 -08004642 new IccAPDUArgument(channel, cla, command, p1, p2, p3, data), phone,
4643 null /* workSource */);
4644 if (DBG) log("iccTransmitApduLogicalChannelWithPermission: " + response);
Shishir Agrawal566b7612013-10-28 14:41:00 -07004645
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004646 // Append the returned status code to the end of the response payload.
4647 String s = Integer.toHexString(
4648 (response.sw1 << 8) + response.sw2 + 0x10000).substring(1);
4649 if (response.payload != null) {
4650 s = IccUtils.bytesToHexString(response.payload) + s;
4651 }
4652 return s;
4653 } finally {
4654 Binder.restoreCallingIdentity(identity);
Shishir Agrawal5ec14172014-08-05 17:05:45 -07004655 }
Shishir Agrawal566b7612013-10-28 14:41:00 -07004656 }
Jake Hambye994d462014-02-03 13:10:13 -08004657
Evan Charltonc66da362014-05-16 14:06:40 -07004658 @Override
Holly Jiuyu Sun1cc2d552018-01-26 15:51:16 -08004659 public String iccTransmitApduBasicChannel(int subId, String callingPackage, int cla,
4660 int command, int p1, int p2, int p3, String data) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08004661 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
4662 mApp, subId, "iccTransmitApduBasicChannel");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004663 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
Jordan Liu4c733742019-02-28 12:03:40 -08004664 if (DBG) {
4665 log("iccTransmitApduBasicChannel: subId=" + subId + " cla=" + cla + " cmd="
4666 + command + " p1=" + p1 + " p2=" + p2 + " p3=" + p3 + " data=" + data);
4667 }
4668 return iccTransmitApduBasicChannelWithPermission(getPhoneFromSubId(subId), callingPackage,
4669 cla, command, p1, p2, p3, data);
4670 }
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07004671
Jordan Liu4c733742019-02-28 12:03:40 -08004672 @Override
4673 public String iccTransmitApduBasicChannelBySlot(int slotIndex, String callingPackage, int cla,
4674 int command, int p1, int p2, int p3, String data) {
4675 enforceModifyPermission();
4676 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
4677 if (DBG) {
4678 log("iccTransmitApduBasicChannelBySlot: slotIndex=" + slotIndex + " cla=" + cla
4679 + " cmd=" + command + " p1=" + p1 + " p2=" + p2 + " p3=" + p3
4680 + " data=" + data);
4681 }
4682
4683 return iccTransmitApduBasicChannelWithPermission(
4684 getPhoneFromSlotIdOrThrowException(slotIndex), callingPackage, cla, command, p1,
4685 p2, p3, data);
4686 }
4687
4688 // open APDU basic channel assuming the caller has sufficient permissions
4689 private String iccTransmitApduBasicChannelWithPermission(Phone phone, String callingPackage,
4690 int cla, int command, int p1, int p2, int p3, String data) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004691 final long identity = Binder.clearCallingIdentity();
4692 try {
4693 if (command == SELECT_COMMAND && p1 == SELECT_P1 && p2 == SELECT_P2 && p3 == SELECT_P3
4694 && TextUtils.equals(ISDR_AID, data)) {
4695 // Only allows LPA to select ISD-R.
Nazanin Bakhshif782e562018-12-11 15:15:39 -08004696 ComponentInfo bestComponent = EuiccConnector.findBestComponent(getDefaultPhone()
4697 .getContext().getPackageManager());
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004698 if (bestComponent == null
4699 || !TextUtils.equals(callingPackage, bestComponent.packageName)) {
4700 loge("The calling package is not allowed to select ISD-R.");
4701 throw new SecurityException(
4702 "The calling package is not allowed to select ISD-R.");
4703 }
Holly Jiuyu Sun1cc2d552018-01-26 15:51:16 -08004704 }
Holly Jiuyu Sun1cc2d552018-01-26 15:51:16 -08004705
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004706 IccIoResult response = (IccIoResult) sendRequest(CMD_TRANSMIT_APDU_BASIC_CHANNEL,
Jordan Liu4c733742019-02-28 12:03:40 -08004707 new IccAPDUArgument(0, cla, command, p1, p2, p3, data), phone,
4708 null /* workSource */);
4709 if (DBG) log("iccTransmitApduBasicChannelWithPermission: " + response);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07004710
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004711 // Append the returned status code to the end of the response payload.
4712 String s = Integer.toHexString(
4713 (response.sw1 << 8) + response.sw2 + 0x10000).substring(1);
4714 if (response.payload != null) {
4715 s = IccUtils.bytesToHexString(response.payload) + s;
4716 }
4717 return s;
4718 } finally {
4719 Binder.restoreCallingIdentity(identity);
Shishir Agrawal5ec14172014-08-05 17:05:45 -07004720 }
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07004721 }
4722
4723 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08004724 public byte[] iccExchangeSimIO(int subId, int fileID, int command, int p1, int p2, int p3,
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07004725 String filePath) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08004726 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
4727 mApp, subId, "iccExchangeSimIO");
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07004728
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004729 final long identity = Binder.clearCallingIdentity();
4730 try {
4731 if (DBG) {
4732 log("Exchange SIM_IO " + subId + ":" + fileID + ":" + command + " "
4733 + p1 + " " + p2 + " " + p3 + ":" + filePath);
4734 }
4735
4736 IccIoResult response =
4737 (IccIoResult) sendRequest(CMD_EXCHANGE_SIM_IO,
4738 new IccAPDUArgument(-1, fileID, command, p1, p2, p3, filePath),
4739 subId);
4740
4741 if (DBG) {
4742 log("Exchange SIM_IO [R]" + response);
4743 }
4744
4745 byte[] result = null;
4746 int length = 2;
4747 if (response.payload != null) {
4748 length = 2 + response.payload.length;
4749 result = new byte[length];
4750 System.arraycopy(response.payload, 0, result, 0, response.payload.length);
4751 } else {
4752 result = new byte[length];
4753 }
4754
4755 result[length - 1] = (byte) response.sw2;
4756 result[length - 2] = (byte) response.sw1;
4757 return result;
4758 } finally {
4759 Binder.restoreCallingIdentity(identity);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07004760 }
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07004761 }
4762
Nathan Haroldb3014052017-01-25 15:57:32 -08004763 /**
4764 * Get the forbidden PLMN List from the given app type (ex APPTYPE_USIM)
4765 * on a particular subscription
4766 */
Philip P. Moltmann700a9592019-10-03 11:53:50 -07004767 public String[] getForbiddenPlmns(int subId, int appType, String callingPackage,
4768 String callingFeatureId) {
sqianb6e41952018-03-12 14:54:01 -07004769 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
Philip P. Moltmann700a9592019-10-03 11:53:50 -07004770 mApp, subId, callingPackage, callingFeatureId, "getForbiddenPlmns")) {
sqianb6e41952018-03-12 14:54:01 -07004771 return null;
4772 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004773
4774 final long identity = Binder.clearCallingIdentity();
4775 try {
4776 if (appType != TelephonyManager.APPTYPE_USIM
4777 && appType != TelephonyManager.APPTYPE_SIM) {
4778 loge("getForbiddenPlmnList(): App Type must be USIM or SIM");
4779 return null;
4780 }
4781 Object response = sendRequest(
4782 CMD_GET_FORBIDDEN_PLMNS, new Integer(appType), subId);
4783 if (response instanceof String[]) {
4784 return (String[]) response;
4785 }
yincheng zhao2737e882019-09-06 17:06:54 -07004786 // Response is an Exception of some kind
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004787 // which is signalled to the user as a NULL retval
Nathan Haroldb3014052017-01-25 15:57:32 -08004788 return null;
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004789 } finally {
4790 Binder.restoreCallingIdentity(identity);
Nathan Haroldb3014052017-01-25 15:57:32 -08004791 }
Nathan Haroldb3014052017-01-25 15:57:32 -08004792 }
4793
yincheng zhao2737e882019-09-06 17:06:54 -07004794 /**
4795 * Set the forbidden PLMN list from the given app type (ex APPTYPE_USIM) on a particular
4796 * subscription.
4797 *
4798 * @param subId the id of the subscription.
4799 * @param appType the uicc app type, must be USIM or SIM.
4800 * @param fplmns the Forbiden plmns list that needed to be written to the SIM.
4801 * @param callingPackage the op Package name.
Philip P. Moltmann700a9592019-10-03 11:53:50 -07004802 * @param callingFeatureId the feature in the package.
yincheng zhao2737e882019-09-06 17:06:54 -07004803 * @return number of fplmns that is successfully written to the SIM.
4804 */
Philip P. Moltmann700a9592019-10-03 11:53:50 -07004805 public int setForbiddenPlmns(int subId, int appType, List<String> fplmns, String callingPackage,
4806 String callingFeatureId) {
4807 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(mApp, subId, callingPackage,
4808 callingFeatureId, "setForbiddenPlmns")) {
yincheng zhao2737e882019-09-06 17:06:54 -07004809 if (DBG) logv("no permissions for setForbiddenplmns");
4810 throw new IllegalStateException("No Permissions for setForbiddenPlmns");
4811 }
4812 if (appType != TelephonyManager.APPTYPE_USIM && appType != TelephonyManager.APPTYPE_SIM) {
4813 loge("setForbiddenPlmnList(): App Type must be USIM or SIM");
4814 throw new IllegalArgumentException("Invalid appType: App Type must be USIM or SIM");
4815 }
4816 if (fplmns == null) {
4817 throw new IllegalArgumentException("Fplmn List provided is null");
4818 }
4819 for (String fplmn : fplmns) {
4820 if (!CellIdentity.isValidPlmn(fplmn)) {
4821 throw new IllegalArgumentException("Invalid fplmn provided: " + fplmn);
4822 }
4823 }
4824 final long identity = Binder.clearCallingIdentity();
4825 try {
4826 Object response = sendRequest(
4827 CMD_SET_FORBIDDEN_PLMNS,
4828 new Pair<Integer, List<String>>(new Integer(appType), fplmns),
4829 subId);
4830 return (int) response;
4831 } finally {
4832 Binder.restoreCallingIdentity(identity);
4833 }
4834 }
4835
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07004836 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08004837 public String sendEnvelopeWithStatus(int subId, String content) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08004838 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
4839 mApp, subId, "sendEnvelopeWithStatus");
Evan Charltonc66da362014-05-16 14:06:40 -07004840
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004841 final long identity = Binder.clearCallingIdentity();
4842 try {
4843 IccIoResult response = (IccIoResult) sendRequest(CMD_SEND_ENVELOPE, content, subId);
4844 if (response.payload == null) {
4845 return "";
4846 }
Evan Charltonc66da362014-05-16 14:06:40 -07004847
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004848 // Append the returned status code to the end of the response payload.
4849 String s = Integer.toHexString(
4850 (response.sw1 << 8) + response.sw2 + 0x10000).substring(1);
4851 s = IccUtils.bytesToHexString(response.payload) + s;
4852 return s;
4853 } finally {
4854 Binder.restoreCallingIdentity(identity);
4855 }
Evan Charltonc66da362014-05-16 14:06:40 -07004856 }
4857
Jake Hambye994d462014-02-03 13:10:13 -08004858 /**
4859 * Read one of the NV items defined in {@link com.android.internal.telephony.RadioNVItems}
4860 * and {@code ril_nv_items.h}. Used for device configuration by some CDMA operators.
4861 *
4862 * @param itemID the ID of the item to read
4863 * @return the NV item as a String, or null on error.
4864 */
4865 @Override
4866 public String nvReadItem(int itemID) {
vagdeviaf9a5b92018-08-15 16:01:53 -07004867 WorkSource workSource = getWorkSource(Binder.getCallingUid());
Jeff Davidson7e17e312018-02-13 18:17:36 -08004868 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
4869 mApp, getDefaultSubscription(), "nvReadItem");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004870
4871 final long identity = Binder.clearCallingIdentity();
4872 try {
4873 if (DBG) log("nvReadItem: item " + itemID);
vagdeviaf9a5b92018-08-15 16:01:53 -07004874 String value = (String) sendRequest(CMD_NV_READ_ITEM, itemID, workSource);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004875 if (DBG) log("nvReadItem: item " + itemID + " is \"" + value + '"');
4876 return value;
4877 } finally {
4878 Binder.restoreCallingIdentity(identity);
4879 }
Jake Hambye994d462014-02-03 13:10:13 -08004880 }
4881
4882 /**
4883 * Write one of the NV items defined in {@link com.android.internal.telephony.RadioNVItems}
4884 * and {@code ril_nv_items.h}. Used for device configuration by some CDMA operators.
4885 *
4886 * @param itemID the ID of the item to read
4887 * @param itemValue the value to write, as a String
4888 * @return true on success; false on any failure
4889 */
4890 @Override
4891 public boolean nvWriteItem(int itemID, String itemValue) {
vagdeviaf9a5b92018-08-15 16:01:53 -07004892 WorkSource workSource = getWorkSource(Binder.getCallingUid());
Jeff Davidson7e17e312018-02-13 18:17:36 -08004893 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
4894 mApp, getDefaultSubscription(), "nvWriteItem");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004895
4896 final long identity = Binder.clearCallingIdentity();
4897 try {
4898 if (DBG) log("nvWriteItem: item " + itemID + " value \"" + itemValue + '"');
4899 Boolean success = (Boolean) sendRequest(CMD_NV_WRITE_ITEM,
vagdeviaf9a5b92018-08-15 16:01:53 -07004900 new Pair<Integer, String>(itemID, itemValue), workSource);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004901 if (DBG) log("nvWriteItem: item " + itemID + ' ' + (success ? "ok" : "fail"));
4902 return success;
4903 } finally {
4904 Binder.restoreCallingIdentity(identity);
4905 }
Jake Hambye994d462014-02-03 13:10:13 -08004906 }
4907
4908 /**
4909 * Update the CDMA Preferred Roaming List (PRL) in the radio NV storage.
4910 * Used for device configuration by some CDMA operators.
4911 *
4912 * @param preferredRoamingList byte array containing the new PRL
4913 * @return true on success; false on any failure
4914 */
4915 @Override
4916 public boolean nvWriteCdmaPrl(byte[] preferredRoamingList) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08004917 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
4918 mApp, getDefaultSubscription(), "nvWriteCdmaPrl");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004919
4920 final long identity = Binder.clearCallingIdentity();
4921 try {
4922 if (DBG) log("nvWriteCdmaPrl: value: " + HexDump.toHexString(preferredRoamingList));
4923 Boolean success = (Boolean) sendRequest(CMD_NV_WRITE_CDMA_PRL, preferredRoamingList);
4924 if (DBG) log("nvWriteCdmaPrl: " + (success ? "ok" : "fail"));
4925 return success;
4926 } finally {
4927 Binder.restoreCallingIdentity(identity);
4928 }
Jake Hambye994d462014-02-03 13:10:13 -08004929 }
4930
4931 /**
chen xu6dac5ab2018-10-26 17:39:23 -07004932 * Rollback modem configurations to factory default except some config which are in whitelist.
Jake Hambye994d462014-02-03 13:10:13 -08004933 * Used for device configuration by some CDMA operators.
4934 *
chen xu6dac5ab2018-10-26 17:39:23 -07004935 * @param slotIndex - device slot.
4936 *
Jake Hambye994d462014-02-03 13:10:13 -08004937 * @return true on success; false on any failure
4938 */
4939 @Override
chen xu6dac5ab2018-10-26 17:39:23 -07004940 public boolean resetModemConfig(int slotIndex) {
4941 Phone phone = PhoneFactory.getPhone(slotIndex);
4942 if (phone != null) {
4943 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
4944 mApp, phone.getSubId(), "resetModemConfig");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004945
chen xu6dac5ab2018-10-26 17:39:23 -07004946 final long identity = Binder.clearCallingIdentity();
4947 try {
4948 Boolean success = (Boolean) sendRequest(CMD_RESET_MODEM_CONFIG, null);
4949 if (DBG) log("resetModemConfig:" + ' ' + (success ? "ok" : "fail"));
4950 return success;
4951 } finally {
4952 Binder.restoreCallingIdentity(identity);
4953 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004954 }
chen xu6dac5ab2018-10-26 17:39:23 -07004955 return false;
4956 }
4957
4958 /**
4959 * Generate a radio modem reset. Used for device configuration by some CDMA operators.
4960 *
4961 * @param slotIndex - device slot.
4962 *
4963 * @return true on success; false on any failure
4964 */
4965 @Override
4966 public boolean rebootModem(int slotIndex) {
4967 Phone phone = PhoneFactory.getPhone(slotIndex);
4968 if (phone != null) {
4969 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
4970 mApp, phone.getSubId(), "rebootModem");
4971
4972 final long identity = Binder.clearCallingIdentity();
4973 try {
4974 Boolean success = (Boolean) sendRequest(CMD_MODEM_REBOOT, null);
4975 if (DBG) log("rebootModem:" + ' ' + (success ? "ok" : "fail"));
4976 return success;
4977 } finally {
4978 Binder.restoreCallingIdentity(identity);
4979 }
4980 }
4981 return false;
Jake Hambye994d462014-02-03 13:10:13 -08004982 }
Jake Hamby7c27be32014-03-03 13:25:59 -08004983
Philip P. Moltmann700a9592019-10-03 11:53:50 -07004984 public String[] getPcscfAddress(String apnType, String callingPackage,
4985 String callingFeatureId) {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08004986 final Phone defaultPhone = getDefaultPhone();
Philip P. Moltmann700a9592019-10-03 11:53:50 -07004987 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(mApp, defaultPhone.getSubId(),
4988 callingPackage, callingFeatureId, "getPcscfAddress")) {
Svet Ganovb320e182015-04-16 12:30:10 -07004989 return new String[0];
4990 }
4991
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004992 final long identity = Binder.clearCallingIdentity();
4993 try {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08004994 return defaultPhone.getPcscfAddress(apnType);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004995 } finally {
4996 Binder.restoreCallingIdentity(identity);
4997 }
Wink Saville36469e72014-06-11 15:17:00 -07004998 }
4999
Brad Ebinger51f743a2017-01-23 13:50:20 -08005000 /**
Grace Jiaaa2eb6b2020-01-09 16:26:08 -08005001 * Toggle IMS disable and enable for the framework to reset it. See {@link #enableIms(int)} and
5002 * {@link #disableIms(int)}.
5003 * @param slotIndex device slot.
5004 */
5005 public void resetIms(int slotIndex) {
5006 enforceModifyPermission();
5007
5008 final long identity = Binder.clearCallingIdentity();
5009 try {
5010 if (mImsResolver == null) {
5011 // may happen if the does not support IMS.
5012 return;
5013 }
5014 mImsResolver.disableIms(slotIndex);
5015 mImsResolver.enableIms(slotIndex);
5016 } finally {
5017 Binder.restoreCallingIdentity(identity);
5018 }
5019 }
5020
5021 /**
Brad Ebinger22bc3e42018-01-16 09:39:35 -08005022 * Enables IMS for the framework. This will trigger IMS registration and ImsFeature capability
5023 * status updates, if not already enabled.
Brad Ebinger51f743a2017-01-23 13:50:20 -08005024 */
Brad Ebinger22bc3e42018-01-16 09:39:35 -08005025 public void enableIms(int slotId) {
Brad Ebinger51f743a2017-01-23 13:50:20 -08005026 enforceModifyPermission();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005027
5028 final long identity = Binder.clearCallingIdentity();
5029 try {
Brad Ebinger24c29992019-12-05 13:03:21 -08005030 if (mImsResolver == null) {
Brad Ebinger9c0eb502019-01-23 15:06:19 -08005031 // may happen if the device does not support IMS.
5032 return;
5033 }
Brad Ebinger24c29992019-12-05 13:03:21 -08005034 mImsResolver.enableIms(slotId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005035 } finally {
5036 Binder.restoreCallingIdentity(identity);
5037 }
Brad Ebinger34bef922017-11-09 10:27:08 -08005038 }
5039
5040 /**
Brad Ebinger22bc3e42018-01-16 09:39:35 -08005041 * Disables IMS for the framework. This will trigger IMS de-registration and trigger ImsFeature
5042 * status updates to disabled.
Brad Ebinger34bef922017-11-09 10:27:08 -08005043 */
Brad Ebinger22bc3e42018-01-16 09:39:35 -08005044 public void disableIms(int slotId) {
5045 enforceModifyPermission();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005046
5047 final long identity = Binder.clearCallingIdentity();
5048 try {
Brad Ebinger24c29992019-12-05 13:03:21 -08005049 if (mImsResolver == null) {
Brad Ebinger9c0eb502019-01-23 15:06:19 -08005050 // may happen if the device does not support IMS.
5051 return;
5052 }
Brad Ebinger24c29992019-12-05 13:03:21 -08005053 mImsResolver.disableIms(slotId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005054 } finally {
5055 Binder.restoreCallingIdentity(identity);
5056 }
Brad Ebinger22bc3e42018-01-16 09:39:35 -08005057 }
5058
5059 /**
5060 * Returns the {@link IImsMmTelFeature} that corresponds to the given slot Id for the MMTel
5061 * feature or {@link null} if the service is not available. If the feature is available, the
5062 * {@link IImsServiceFeatureCallback} callback is registered as a listener for feature updates.
5063 */
5064 public IImsMmTelFeature getMmTelFeatureAndListen(int slotId,
Brad Ebinger34bef922017-11-09 10:27:08 -08005065 IImsServiceFeatureCallback callback) {
5066 enforceModifyPermission();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005067
5068 final long identity = Binder.clearCallingIdentity();
5069 try {
Brad Ebinger24c29992019-12-05 13:03:21 -08005070 if (mImsResolver == null) {
Brad Ebinger9c0eb502019-01-23 15:06:19 -08005071 // may happen if the device does not support IMS.
5072 return null;
5073 }
Brad Ebinger24c29992019-12-05 13:03:21 -08005074 return mImsResolver.getMmTelFeatureAndListen(slotId, callback);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005075 } finally {
5076 Binder.restoreCallingIdentity(identity);
5077 }
Brad Ebinger34bef922017-11-09 10:27:08 -08005078 }
5079
5080 /**
5081 * Returns the {@link IImsRcsFeature} that corresponds to the given slot Id for the RCS
5082 * feature during emergency calling or {@link null} if the service is not available. If the
5083 * feature is available, the {@link IImsServiceFeatureCallback} callback is registered as a
5084 * listener for feature updates.
5085 */
5086 public IImsRcsFeature getRcsFeatureAndListen(int slotId, IImsServiceFeatureCallback callback) {
5087 enforceModifyPermission();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005088
5089 final long identity = Binder.clearCallingIdentity();
5090 try {
Brad Ebinger24c29992019-12-05 13:03:21 -08005091 if (mImsResolver == null) {
Brad Ebinger9c0eb502019-01-23 15:06:19 -08005092 // may happen if the device does not support IMS.
5093 return null;
5094 }
Brad Ebinger24c29992019-12-05 13:03:21 -08005095 return mImsResolver.getRcsFeatureAndListen(slotId, callback);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005096 } finally {
5097 Binder.restoreCallingIdentity(identity);
5098 }
Brad Ebinger51f743a2017-01-23 13:50:20 -08005099 }
5100
Brad Ebinger5f64b052017-12-14 14:26:15 -08005101 /**
Brad Ebinger075ff3a2020-05-18 17:52:58 -07005102 * Unregister a previously registered IImsServiceFeatureCallback associated with an ImsFeature.
5103 */
5104 public void unregisterImsFeatureCallback(int slotId, int featureType,
5105 IImsServiceFeatureCallback callback) {
5106 enforceModifyPermission();
5107
5108 final long identity = Binder.clearCallingIdentity();
5109 try {
5110 if (mImsResolver == null) return;
5111 mImsResolver.unregisterImsFeatureCallback(slotId, featureType, callback);
5112 } finally {
5113 Binder.restoreCallingIdentity(identity);
5114 }
5115 }
5116
5117 /**
Brad Ebinger5f64b052017-12-14 14:26:15 -08005118 * Returns the {@link IImsRegistration} structure associated with the slotId and feature
Brad Ebinger9c0eb502019-01-23 15:06:19 -08005119 * specified or null if IMS is not supported on the slot specified.
Brad Ebinger5f64b052017-12-14 14:26:15 -08005120 */
5121 public IImsRegistration getImsRegistration(int slotId, int feature) throws RemoteException {
5122 enforceModifyPermission();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005123
5124 final long identity = Binder.clearCallingIdentity();
5125 try {
Brad Ebinger24c29992019-12-05 13:03:21 -08005126 if (mImsResolver == null) {
Brad Ebinger9c0eb502019-01-23 15:06:19 -08005127 // may happen if the device does not support IMS.
5128 return null;
5129 }
Brad Ebinger24c29992019-12-05 13:03:21 -08005130 return mImsResolver.getImsRegistration(slotId, feature);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005131 } finally {
5132 Binder.restoreCallingIdentity(identity);
5133 }
Brad Ebinger5f64b052017-12-14 14:26:15 -08005134 }
5135
Brad Ebinger22bc3e42018-01-16 09:39:35 -08005136 /**
5137 * Returns the {@link IImsConfig} structure associated with the slotId and feature
Brad Ebinger9c0eb502019-01-23 15:06:19 -08005138 * specified or null if IMS is not supported on the slot specified.
Brad Ebinger22bc3e42018-01-16 09:39:35 -08005139 */
5140 public IImsConfig getImsConfig(int slotId, int feature) throws RemoteException {
5141 enforceModifyPermission();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005142
5143 final long identity = Binder.clearCallingIdentity();
5144 try {
Brad Ebinger24c29992019-12-05 13:03:21 -08005145 if (mImsResolver == null) {
Brad Ebinger9c0eb502019-01-23 15:06:19 -08005146 // may happen if the device does not support IMS.
5147 return null;
5148 }
Brad Ebinger24c29992019-12-05 13:03:21 -08005149 return mImsResolver.getImsConfig(slotId, feature);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005150 } finally {
5151 Binder.restoreCallingIdentity(identity);
5152 }
Brad Ebinger22bc3e42018-01-16 09:39:35 -08005153 }
5154
Brad Ebinger884c07b2018-02-15 16:17:40 -08005155 /**
Brad Ebingerdac2f002018-04-03 15:17:52 -07005156 * Sets the ImsService Package Name that Telephony will bind to.
5157 *
Brad Ebinger24c29992019-12-05 13:03:21 -08005158 * @param slotIndex the slot ID that the ImsService should bind for.
5159 * @param isCarrierService true if the ImsService is the carrier override, false if the
Brad Ebingerdac2f002018-04-03 15:17:52 -07005160 * ImsService is the device default ImsService.
Brad Ebinger24c29992019-12-05 13:03:21 -08005161 * @param featureTypes An integer array of feature types associated with a packageName.
5162 * @param packageName The name of the package that the current configuration will be replaced
5163 * with.
Brad Ebingerdac2f002018-04-03 15:17:52 -07005164 * @return true if setting the ImsService to bind to succeeded, false if it did not.
Brad Ebingerdac2f002018-04-03 15:17:52 -07005165 */
Brad Ebinger24c29992019-12-05 13:03:21 -08005166 public boolean setBoundImsServiceOverride(int slotIndex, boolean isCarrierService,
5167 int[] featureTypes, String packageName) {
5168 int[] subIds = SubscriptionManager.getSubId(slotIndex);
5169 TelephonyPermissions.enforceShellOnly(Binder.getCallingUid(), "setBoundImsServiceOverride");
Brad Ebingerde696de2018-04-06 09:56:40 -07005170 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(mApp,
5171 (subIds != null ? subIds[0] : SubscriptionManager.INVALID_SUBSCRIPTION_ID),
Brad Ebinger24c29992019-12-05 13:03:21 -08005172 "setBoundImsServiceOverride");
Brad Ebingerde696de2018-04-06 09:56:40 -07005173
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005174 final long identity = Binder.clearCallingIdentity();
5175 try {
Brad Ebinger24c29992019-12-05 13:03:21 -08005176 if (mImsResolver == null) {
Brad Ebinger9c0eb502019-01-23 15:06:19 -08005177 // may happen if the device does not support IMS.
5178 return false;
5179 }
Brad Ebinger24c29992019-12-05 13:03:21 -08005180 Map<Integer, String> featureConfig = new HashMap<>();
5181 for (int featureType : featureTypes) {
5182 featureConfig.put(featureType, packageName);
5183 }
5184 return mImsResolver.overrideImsServiceConfiguration(slotIndex, isCarrierService,
5185 featureConfig);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005186 } finally {
5187 Binder.restoreCallingIdentity(identity);
5188 }
Brad Ebingerdac2f002018-04-03 15:17:52 -07005189 }
5190
5191 /**
Brad Ebinger24c29992019-12-05 13:03:21 -08005192 * Return the package name of the currently bound ImsService.
Brad Ebingerdac2f002018-04-03 15:17:52 -07005193 *
5194 * @param slotId The slot that the ImsService is associated with.
5195 * @param isCarrierImsService true, if the ImsService is a carrier override, false if it is
5196 * the device default.
Brad Ebinger24c29992019-12-05 13:03:21 -08005197 * @param featureType The feature associated with the queried configuration.
Brad Ebingerdac2f002018-04-03 15:17:52 -07005198 * @return the package name of the ImsService configuration.
5199 */
Brad Ebinger24c29992019-12-05 13:03:21 -08005200 public String getBoundImsServicePackage(int slotId, boolean isCarrierImsService,
5201 @ImsFeature.FeatureType int featureType) {
Brad Ebingerde696de2018-04-06 09:56:40 -07005202 int[] subIds = SubscriptionManager.getSubId(slotId);
Brad Ebinger24c29992019-12-05 13:03:21 -08005203 TelephonyPermissions
5204 .enforeceCallingOrSelfReadPrivilegedPhoneStatePermissionOrCarrierPrivilege(
5205 mApp, (subIds != null ? subIds[0] : SubscriptionManager.INVALID_SUBSCRIPTION_ID),
5206 "getBoundImsServicePackage");
Brad Ebingerde696de2018-04-06 09:56:40 -07005207
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005208 final long identity = Binder.clearCallingIdentity();
5209 try {
Brad Ebinger24c29992019-12-05 13:03:21 -08005210 if (mImsResolver == null) {
Brad Ebinger9c0eb502019-01-23 15:06:19 -08005211 // may happen if the device does not support IMS.
5212 return "";
5213 }
Brad Ebingera80c3312019-12-02 10:59:39 -08005214 // TODO: change API to query RCS separately.
Brad Ebinger24c29992019-12-05 13:03:21 -08005215 return mImsResolver.getImsServiceConfiguration(slotId, isCarrierImsService,
5216 featureType);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005217 } finally {
5218 Binder.restoreCallingIdentity(identity);
5219 }
Brad Ebingerdac2f002018-04-03 15:17:52 -07005220 }
5221
Brad Ebingerbc7dd582019-10-17 17:03:22 -07005222 /**
5223 * Get the MmTelFeature state associated with the requested subscription id.
5224 * @param subId The subscription that the MmTelFeature is associated with.
5225 * @param callback A callback with an integer containing the
5226 * {@link android.telephony.ims.feature.ImsFeature.ImsState} associated with the MmTelFeature.
5227 */
5228 @Override
5229 public void getImsMmTelFeatureState(int subId, IIntegerConsumer callback) {
5230 enforceReadPrivilegedPermission("getImsMmTelFeatureState");
5231 if (!ImsManager.isImsSupportedOnDevice(mApp)) {
5232 throw new ServiceSpecificException(ImsException.CODE_ERROR_UNSUPPORTED_OPERATION,
5233 "IMS not available on device.");
5234 }
5235 final long token = Binder.clearCallingIdentity();
5236 try {
5237 int slotId = getSlotIndex(subId);
5238 if (slotId <= SubscriptionManager.INVALID_SIM_SLOT_INDEX) {
5239 Log.w(LOG_TAG, "getImsMmTelFeatureState: called with an inactive subscription '"
5240 + subId + "'");
5241 throw new ServiceSpecificException(ImsException.CODE_ERROR_INVALID_SUBSCRIPTION);
5242 }
5243 ImsManager.getInstance(mApp, slotId).getImsServiceState(anInteger -> {
5244 try {
5245 callback.accept(anInteger == null ? ImsFeature.STATE_UNAVAILABLE : anInteger);
5246 } catch (RemoteException e) {
5247 Log.w(LOG_TAG, "getImsMmTelFeatureState: remote caller is no longer running. "
5248 + "Ignore");
5249 }
5250 });
5251 } finally {
5252 Binder.restoreCallingIdentity(token);
5253 }
5254 }
5255
Wink Saville36469e72014-06-11 15:17:00 -07005256 public void setImsRegistrationState(boolean registered) {
5257 enforceModifyPermission();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005258
5259 final long identity = Binder.clearCallingIdentity();
5260 try {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08005261 getDefaultPhone().setImsRegistrationState(registered);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005262 } finally {
5263 Binder.restoreCallingIdentity(identity);
5264 }
Wink Saville36469e72014-06-11 15:17:00 -07005265 }
5266
5267 /**
Stuart Scott54788802015-03-30 13:18:01 -07005268 * Set the network selection mode to automatic.
5269 *
5270 */
5271 @Override
5272 public void setNetworkSelectionModeAutomatic(int subId) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08005273 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
5274 mApp, subId, "setNetworkSelectionModeAutomatic");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005275
5276 final long identity = Binder.clearCallingIdentity();
5277 try {
shilufc958392020-01-20 11:36:01 -08005278 if (!isActiveSubscription(subId)) {
5279 return;
5280 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005281 if (DBG) log("setNetworkSelectionModeAutomatic: subId " + subId);
5282 sendRequest(CMD_SET_NETWORK_SELECTION_MODE_AUTOMATIC, null, subId);
5283 } finally {
5284 Binder.restoreCallingIdentity(identity);
5285 }
Stuart Scott54788802015-03-30 13:18:01 -07005286 }
5287
Pengquan Mengea84e042018-09-20 14:57:26 -07005288 /**
5289 * Ask the radio to connect to the input network and change selection mode to manual.
5290 *
5291 * @param subId the id of the subscription.
5292 * @param operatorInfo the operator information, included the PLMN, long name and short name of
5293 * the operator to attach to.
5294 * @param persistSelection whether the selection will persist until reboot. If true, only allows
5295 * attaching to the selected PLMN until reboot; otherwise, attach to the chosen PLMN and resume
5296 * normal network selection next time.
5297 * @return {@code true} on success; {@code true} on any failure.
Shishir Agrawal302c8692015-06-19 13:49:39 -07005298 */
5299 @Override
Pengquan Mengea84e042018-09-20 14:57:26 -07005300 public boolean setNetworkSelectionModeManual(
5301 int subId, OperatorInfo operatorInfo, boolean persistSelection) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08005302 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
5303 mApp, subId, "setNetworkSelectionModeManual");
Pengquan Menge92a50d2018-09-21 15:54:48 -07005304
5305 if (!isActiveSubscription(subId)) {
5306 return false;
5307 }
5308
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005309 final long identity = Binder.clearCallingIdentity();
5310 try {
Pengquan Mengea84e042018-09-20 14:57:26 -07005311 ManualNetworkSelectionArgument arg = new ManualNetworkSelectionArgument(operatorInfo,
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005312 persistSelection);
Pengquan Mengea84e042018-09-20 14:57:26 -07005313 if (DBG) {
5314 log("setNetworkSelectionModeManual: subId: " + subId
5315 + " operator: " + operatorInfo);
5316 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005317 return (Boolean) sendRequest(CMD_SET_NETWORK_SELECTION_MODE_MANUAL, arg, subId);
5318 } finally {
5319 Binder.restoreCallingIdentity(identity);
5320 }
Shishir Agrawal302c8692015-06-19 13:49:39 -07005321 }
shilu84f6e8b2019-12-19 13:58:01 -08005322 /**
5323 * Get the manual network selection
5324 *
5325 * @param subId the id of the subscription.
5326 *
5327 * @return the previously saved user selected PLMN
5328 */
5329 @Override
5330 public String getManualNetworkSelectionPlmn(int subId) {
5331 TelephonyPermissions
5332 .enforeceCallingOrSelfReadPrecisePhoneStatePermissionOrCarrierPrivilege(
5333 mApp, subId, "getManualNetworkSelectionPlmn");
5334
5335 final long identity = Binder.clearCallingIdentity();
5336 try {
5337 if (!isActiveSubscription(subId)) {
shilufa1c2592020-03-10 10:59:43 -07005338 throw new IllegalArgumentException("Invalid Subscription Id: " + subId);
shilu84f6e8b2019-12-19 13:58:01 -08005339 }
5340
5341 final Phone phone = getPhone(subId);
5342 if (phone == null) {
shilufa1c2592020-03-10 10:59:43 -07005343 throw new IllegalArgumentException("Invalid Subscription Id: " + subId);
shilu84f6e8b2019-12-19 13:58:01 -08005344 }
5345 OperatorInfo networkSelection = phone.getSavedNetworkSelection();
5346 return TextUtils.isEmpty(networkSelection.getOperatorNumeric())
5347 ? phone.getManualNetworkSelectionPlmn() : networkSelection.getOperatorNumeric();
5348 } finally {
5349 Binder.restoreCallingIdentity(identity);
5350 }
5351 }
Shishir Agrawal302c8692015-06-19 13:49:39 -07005352
5353 /**
5354 * Scans for available networks.
5355 */
5356 @Override
Philip P. Moltmann3a2772a2019-10-04 08:15:00 -07005357 public CellNetworkScanResult getCellNetworkScanResults(int subId, String callingPackage,
5358 String callingFeatureId) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08005359 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
5360 mApp, subId, "getCellNetworkScanResults");
Hall Liuf19c44f2018-11-27 14:38:17 -08005361 LocationAccessPolicy.LocationPermissionResult locationResult =
5362 LocationAccessPolicy.checkLocationPermission(mApp,
5363 new LocationAccessPolicy.LocationPermissionQuery.Builder()
5364 .setCallingPackage(callingPackage)
Philip P. Moltmann3a2772a2019-10-04 08:15:00 -07005365 .setCallingFeatureId(callingFeatureId)
Hall Liuf19c44f2018-11-27 14:38:17 -08005366 .setCallingPid(Binder.getCallingPid())
5367 .setCallingUid(Binder.getCallingUid())
5368 .setMethod("getCellNetworkScanResults")
5369 .setMinSdkVersionForFine(Build.VERSION_CODES.Q)
Hall Liuc4a3e422020-05-26 17:18:03 -07005370 .setMinSdkVersionForCoarse(Build.VERSION_CODES.Q)
5371 .setMinSdkVersionForEnforcement(Build.VERSION_CODES.Q)
Hall Liuf19c44f2018-11-27 14:38:17 -08005372 .build());
5373 switch (locationResult) {
5374 case DENIED_HARD:
5375 throw new SecurityException("Not allowed to access scan results -- location");
5376 case DENIED_SOFT:
5377 return null;
5378 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005379
Pengquan Menga1bb6272018-09-06 09:59:22 -07005380 long identity = Binder.clearCallingIdentity();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005381 try {
5382 if (DBG) log("getCellNetworkScanResults: subId " + subId);
Pengquan Menga1bb6272018-09-06 09:59:22 -07005383 return (CellNetworkScanResult) sendRequest(
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005384 CMD_PERFORM_NETWORK_SCAN, null, subId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005385 } finally {
5386 Binder.restoreCallingIdentity(identity);
5387 }
Shishir Agrawal302c8692015-06-19 13:49:39 -07005388 }
5389
5390 /**
Shuo Qian4a594052020-01-23 11:59:30 -08005391 * Get the call forwarding info, given the call forwarding reason.
5392 */
5393 @Override
Hall Liu27d24262020-09-18 19:04:59 -07005394 public void getCallForwarding(int subId, int callForwardingReason,
5395 ICallForwardingInfoCallback callback) {
Shuo Qian4a594052020-01-23 11:59:30 -08005396 enforceReadPrivilegedPermission("getCallForwarding");
5397 long identity = Binder.clearCallingIdentity();
5398 try {
5399 if (DBG) {
5400 log("getCallForwarding: subId " + subId
5401 + " callForwardingReason" + callForwardingReason);
5402 }
Hall Liu27d24262020-09-18 19:04:59 -07005403
5404 Phone phone = getPhone(subId);
5405 if (phone == null) {
5406 try {
Hall Liu940c4ca2020-09-29 17:10:18 -07005407 callback.onError(
5408 TelephonyManager.CallForwardingInfoCallback.RESULT_ERROR_UNKNOWN);
Hall Liu27d24262020-09-18 19:04:59 -07005409 } catch (RemoteException e) {
5410 // ignore
5411 }
5412 return;
5413 }
5414
5415 Pair<Integer, TelephonyManager.CallForwardingInfoCallback> argument = Pair.create(
5416 callForwardingReason, new TelephonyManager.CallForwardingInfoCallback() {
5417 @Override
5418 public void onCallForwardingInfoAvailable(CallForwardingInfo info) {
5419 try {
5420 callback.onCallForwardingInfoAvailable(info);
5421 } catch (RemoteException e) {
5422 // ignore
5423 }
5424 }
5425
5426 @Override
5427 public void onError(int error) {
5428 try {
5429 callback.onError(error);
5430 } catch (RemoteException e) {
5431 // ignore
5432 }
5433 }
5434 });
5435 sendRequestAsync(CMD_GET_CALL_FORWARDING, argument, phone, null);
Shuo Qian4a594052020-01-23 11:59:30 -08005436 } finally {
5437 Binder.restoreCallingIdentity(identity);
5438 }
5439 }
5440
5441 /**
5442 * Sets the voice call forwarding info including status (enable/disable), call forwarding
5443 * reason, the number to forward, and the timeout before the forwarding is attempted.
5444 */
5445 @Override
Hall Liu27d24262020-09-18 19:04:59 -07005446 public void setCallForwarding(int subId, CallForwardingInfo callForwardingInfo,
5447 IIntegerConsumer callback) {
Shuo Qian4a594052020-01-23 11:59:30 -08005448 enforceModifyPermission();
5449 long identity = Binder.clearCallingIdentity();
5450 try {
5451 if (DBG) {
5452 log("setCallForwarding: subId " + subId
5453 + " callForwardingInfo" + callForwardingInfo);
5454 }
Hall Liu27d24262020-09-18 19:04:59 -07005455
5456 Phone phone = getPhone(subId);
5457 if (phone == null) {
5458 try {
Hall Liu940c4ca2020-09-29 17:10:18 -07005459 callback.accept(
5460 TelephonyManager.CallForwardingInfoCallback.RESULT_ERROR_UNKNOWN);
Hall Liu27d24262020-09-18 19:04:59 -07005461 } catch (RemoteException e) {
5462 // ignore
5463 }
5464 return;
5465 }
5466
5467 Pair<CallForwardingInfo, Consumer<Integer>> arguments = Pair.create(callForwardingInfo,
5468 FunctionalUtils.ignoreRemoteException(callback::accept));
5469
5470 sendRequestAsync(CMD_SET_CALL_FORWARDING, arguments, phone, null);
Shuo Qian4a594052020-01-23 11:59:30 -08005471 } finally {
5472 Binder.restoreCallingIdentity(identity);
5473 }
5474 }
5475
5476 /**
Hall Liu27d24262020-09-18 19:04:59 -07005477 * Get the call waiting status for a subId.
Shuo Qian4a594052020-01-23 11:59:30 -08005478 */
5479 @Override
Hall Liu27d24262020-09-18 19:04:59 -07005480 public void getCallWaitingStatus(int subId, IIntegerConsumer callback) {
Shuo Qian4a594052020-01-23 11:59:30 -08005481 enforceReadPrivilegedPermission("getCallForwarding");
5482 long identity = Binder.clearCallingIdentity();
5483 try {
Hall Liu27d24262020-09-18 19:04:59 -07005484
5485 Phone phone = getPhone(subId);
5486 if (phone == null) {
5487 try {
5488 callback.accept(TelephonyManager.CALL_WAITING_STATUS_UNKNOWN_ERROR);
5489 } catch (RemoteException e) {
5490 // ignore
5491 }
5492 return;
5493 }
5494
5495 Consumer<Integer> argument = FunctionalUtils.ignoreRemoteException(callback::accept);
5496
Shuo Qian4a594052020-01-23 11:59:30 -08005497 if (DBG) log("getCallWaitingStatus: subId " + subId);
Hall Liu27d24262020-09-18 19:04:59 -07005498 sendRequestAsync(CMD_GET_CALL_WAITING, argument, phone, null);
Shuo Qian4a594052020-01-23 11:59:30 -08005499 } finally {
5500 Binder.restoreCallingIdentity(identity);
5501 }
5502 }
5503
5504 /**
Hall Liu27d24262020-09-18 19:04:59 -07005505 * Sets whether call waiting is enabled for a given subId.
Shuo Qian4a594052020-01-23 11:59:30 -08005506 */
5507 @Override
Hall Liu27d24262020-09-18 19:04:59 -07005508 public void setCallWaitingStatus(int subId, boolean enable, IIntegerConsumer callback) {
Shuo Qian4a594052020-01-23 11:59:30 -08005509 enforceModifyPermission();
5510 long identity = Binder.clearCallingIdentity();
5511 try {
Hall Liu27d24262020-09-18 19:04:59 -07005512 if (DBG) log("setCallWaitingStatus: subId " + subId + " enable: " + enable);
5513
5514 Phone phone = getPhone(subId);
5515 if (phone == null) {
5516 try {
5517 callback.accept(TelephonyManager.CALL_WAITING_STATUS_UNKNOWN_ERROR);
5518 } catch (RemoteException e) {
5519 // ignore
5520 }
5521 return;
5522 }
5523
5524 Pair<Boolean, Consumer<Integer>> arguments = Pair.create(enable,
5525 FunctionalUtils.ignoreRemoteException(callback::accept));
5526
5527 sendRequestAsync(CMD_SET_CALL_WAITING, arguments, phone, null);
Shuo Qian4a594052020-01-23 11:59:30 -08005528 } finally {
5529 Binder.restoreCallingIdentity(identity);
5530 }
5531 }
5532
5533 /**
yinxub1bed742017-04-17 11:45:04 -07005534 * Starts a new network scan and returns the id of this scan.
yinxu504e1392017-04-12 16:03:22 -07005535 *
yinxub1bed742017-04-17 11:45:04 -07005536 * @param subId id of the subscription
5537 * @param request contains the radio access networks with bands/channels to scan
5538 * @param messenger callback messenger for scan results or errors
5539 * @param binder for the purpose of auto clean when the user thread crashes
yinxu504e1392017-04-12 16:03:22 -07005540 * @return the id of the requested scan which can be used to stop the scan.
5541 */
5542 @Override
5543 public int requestNetworkScan(int subId, NetworkScanRequest request, Messenger messenger,
Philip P. Moltmann3a2772a2019-10-04 08:15:00 -07005544 IBinder binder, String callingPackage, String callingFeatureId) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08005545 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
5546 mApp, subId, "requestNetworkScan");
Hall Liuf19c44f2018-11-27 14:38:17 -08005547 LocationAccessPolicy.LocationPermissionResult locationResult =
5548 LocationAccessPolicy.checkLocationPermission(mApp,
5549 new LocationAccessPolicy.LocationPermissionQuery.Builder()
5550 .setCallingPackage(callingPackage)
Philip P. Moltmann3a2772a2019-10-04 08:15:00 -07005551 .setCallingFeatureId(callingFeatureId)
Hall Liuf19c44f2018-11-27 14:38:17 -08005552 .setCallingPid(Binder.getCallingPid())
5553 .setCallingUid(Binder.getCallingUid())
5554 .setMethod("requestNetworkScan")
5555 .setMinSdkVersionForFine(Build.VERSION_CODES.Q)
James.cf Lin1d4d7392020-07-03 18:22:53 +08005556 .setMinSdkVersionForCoarse(Build.VERSION_CODES.Q)
5557 .setMinSdkVersionForEnforcement(Build.VERSION_CODES.Q)
Hall Liuf19c44f2018-11-27 14:38:17 -08005558 .build());
Hall Liub2ac8ef2019-02-28 15:56:23 -08005559 if (locationResult != LocationAccessPolicy.LocationPermissionResult.ALLOWED) {
Nathan Harold1c11dba2020-09-22 17:54:53 -07005560 SecurityException e = checkNetworkRequestForSanitizedLocationAccess(
5561 request, subId, callingPackage);
Hall Liub2ac8ef2019-02-28 15:56:23 -08005562 if (e != null) {
5563 if (locationResult == LocationAccessPolicy.LocationPermissionResult.DENIED_HARD) {
5564 throw e;
5565 } else {
Hall Liu0e5abaf2019-04-04 01:25:30 -07005566 loge(e.getMessage());
Hall Liub2ac8ef2019-02-28 15:56:23 -08005567 return TelephonyScanManager.INVALID_SCAN_ID;
5568 }
5569 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005570 }
Hall Liu912dfd32019-04-25 14:02:26 -07005571 int callingUid = Binder.getCallingUid();
5572 int callingPid = Binder.getCallingPid();
Ying Xu94a46582019-04-18 17:14:56 -07005573 final long identity = Binder.clearCallingIdentity();
5574 try {
5575 return mNetworkScanRequestTracker.startNetworkScan(
5576 request, messenger, binder, getPhone(subId),
Hall Liu912dfd32019-04-25 14:02:26 -07005577 callingUid, callingPid, callingPackage);
Ying Xu94a46582019-04-18 17:14:56 -07005578 } finally {
5579 Binder.restoreCallingIdentity(identity);
5580 }
yinxu504e1392017-04-12 16:03:22 -07005581 }
5582
Hall Liub2ac8ef2019-02-28 15:56:23 -08005583 private SecurityException checkNetworkRequestForSanitizedLocationAccess(
Nathan Harold1c11dba2020-09-22 17:54:53 -07005584 NetworkScanRequest request, int subId, String callingPackage) {
5585 boolean hasCarrierPriv = checkCarrierPrivilegesForPackage(subId, callingPackage)
Hall Liu558027f2019-05-15 19:14:05 -07005586 == TelephonyManager.CARRIER_PRIVILEGE_STATUS_HAS_ACCESS;
5587 boolean hasNetworkScanPermission =
5588 mApp.checkCallingOrSelfPermission(android.Manifest.permission.NETWORK_SCAN)
5589 == PERMISSION_GRANTED;
5590
5591 if (!hasCarrierPriv && !hasNetworkScanPermission) {
5592 return new SecurityException("permission.NETWORK_SCAN or carrier privileges is needed"
5593 + " for network scans without location access.");
Hall Liub2ac8ef2019-02-28 15:56:23 -08005594 }
5595
5596 if (request.getSpecifiers() != null && request.getSpecifiers().length > 0) {
5597 for (RadioAccessSpecifier ras : request.getSpecifiers()) {
Hall Liub2ac8ef2019-02-28 15:56:23 -08005598 if (ras.getChannels() != null && ras.getChannels().length > 0) {
5599 return new SecurityException("Specific channels must not be"
5600 + " scanned without location access.");
5601 }
5602 }
5603 }
5604
Hall Liub2ac8ef2019-02-28 15:56:23 -08005605 return null;
5606 }
5607
yinxu504e1392017-04-12 16:03:22 -07005608 /**
5609 * Stops an existing network scan with the given scanId.
yinxub1bed742017-04-17 11:45:04 -07005610 *
5611 * @param subId id of the subscription
5612 * @param scanId id of the scan that needs to be stopped
yinxu504e1392017-04-12 16:03:22 -07005613 */
5614 @Override
5615 public void stopNetworkScan(int subId, int scanId) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08005616 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
5617 mApp, subId, "stopNetworkScan");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005618
Hall Liu912dfd32019-04-25 14:02:26 -07005619 int callingUid = Binder.getCallingUid();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005620 final long identity = Binder.clearCallingIdentity();
5621 try {
Hall Liu912dfd32019-04-25 14:02:26 -07005622 mNetworkScanRequestTracker.stopNetworkScan(scanId, callingUid);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005623 } finally {
5624 Binder.restoreCallingIdentity(identity);
5625 }
yinxu504e1392017-04-12 16:03:22 -07005626 }
5627
5628 /**
Junda Liu84d15a22014-07-02 11:21:04 -07005629 * Get the calculated preferred network type.
5630 * Used for debugging incorrect network type.
5631 *
5632 * @return the preferred network type, defined in RILConstants.java.
5633 */
5634 @Override
Philip P. Moltmann700a9592019-10-03 11:53:50 -07005635 public int getCalculatedPreferredNetworkType(String callingPackage, String callingFeatureId) {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08005636 final Phone defaultPhone = getDefaultPhone();
5637 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(mApp, defaultPhone.getSubId(),
Philip P. Moltmann700a9592019-10-03 11:53:50 -07005638 callingPackage, callingFeatureId, "getCalculatedPreferredNetworkType")) {
Svet Ganovb320e182015-04-16 12:30:10 -07005639 return RILConstants.PREFERRED_NETWORK_MODE;
5640 }
5641
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005642 final long identity = Binder.clearCallingIdentity();
5643 try {
5644 // FIXME: need to get SubId from somewhere.
Nazanin Bakhshif782e562018-12-11 15:15:39 -08005645 return PhoneFactory.calculatePreferredNetworkType(defaultPhone.getContext(), 0);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005646 } finally {
5647 Binder.restoreCallingIdentity(identity);
5648 }
Junda Liu84d15a22014-07-02 11:21:04 -07005649 }
5650
5651 /**
Jake Hamby7c27be32014-03-03 13:25:59 -08005652 * Get the preferred network type.
5653 * Used for device configuration by some CDMA operators.
5654 *
5655 * @return the preferred network type, defined in RILConstants.java.
5656 */
5657 @Override
Stuart Scott54788802015-03-30 13:18:01 -07005658 public int getPreferredNetworkType(int subId) {
Pengquan Menga4009cb2018-12-20 11:00:24 -08005659 TelephonyPermissions
5660 .enforeceCallingOrSelfReadPrivilegedPhoneStatePermissionOrCarrierPrivilege(
5661 mApp, subId, "getPreferredNetworkType");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005662
5663 final long identity = Binder.clearCallingIdentity();
5664 try {
5665 if (DBG) log("getPreferredNetworkType");
5666 int[] result = (int[]) sendRequest(CMD_GET_PREFERRED_NETWORK_TYPE, null, subId);
5667 int networkType = (result != null ? result[0] : -1);
5668 if (DBG) log("getPreferredNetworkType: " + networkType);
5669 return networkType;
5670 } finally {
5671 Binder.restoreCallingIdentity(identity);
5672 }
Jake Hamby7c27be32014-03-03 13:25:59 -08005673 }
5674
5675 /**
5676 * Set the preferred network type.
Jake Hamby7c27be32014-03-03 13:25:59 -08005677 *
5678 * @param networkType the preferred network type, defined in RILConstants.java.
5679 * @return true on success; false on any failure.
5680 */
5681 @Override
Stuart Scott54788802015-03-30 13:18:01 -07005682 public boolean setPreferredNetworkType(int subId, int networkType) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08005683 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
5684 mApp, subId, "setPreferredNetworkType");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005685
5686 final long identity = Binder.clearCallingIdentity();
5687 try {
calvinpan8ed33732020-03-12 14:17:55 +08005688 Boolean success = (Boolean) sendRequest(
5689 CMD_SET_PREFERRED_NETWORK_TYPE, networkType, subId);
calvinpan03641a72020-08-18 16:53:59 +08005690
5691 if (success) {
5692 Settings.Global.putInt(mApp.getContentResolver(),
5693 Settings.Global.PREFERRED_NETWORK_MODE + subId, networkType);
5694 }
calvinpan8ed33732020-03-12 14:17:55 +08005695 if (DBG) log("setPreferredNetworkType: " + (success ? "ok" : "fail"));
5696 return success;
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005697 } finally {
5698 Binder.restoreCallingIdentity(identity);
Junda Liu80bc0d12014-07-14 16:36:44 -07005699 }
Jake Hamby7c27be32014-03-03 13:25:59 -08005700 }
Robert Greenwalted86e582014-05-21 20:03:20 -07005701
5702 /**
calvinpan0ac9c1a2020-01-14 20:42:55 +08005703 * Get the allowed network types that store in the telephony provider.
5704 *
5705 * @param subId the id of the subscription.
5706 * @return allowedNetworkTypes the allowed network types.
5707 */
5708 @Override
5709 public long getAllowedNetworkTypes(int subId) {
5710 TelephonyPermissions
5711 .enforeceCallingOrSelfReadPrivilegedPhoneStatePermissionOrCarrierPrivilege(
5712 mApp, subId, "getAllowedNetworkTypes");
5713
5714 final long identity = Binder.clearCallingIdentity();
5715 try {
5716 return SubscriptionManager.getLongSubscriptionProperty(
5717 subId, SubscriptionManager.ALLOWED_NETWORK_TYPES, -1, mApp);
5718 } finally {
5719 Binder.restoreCallingIdentity(identity);
5720 }
5721 }
5722
5723 /**
5724 * Set the allowed network types.
5725 *
5726 * @param subId the id of the subscription.
5727 * @param allowedNetworkTypes the allowed network types.
5728 * @return true on success; false on any failure.
5729 */
5730 @Override
5731 public boolean setAllowedNetworkTypes(int subId, long allowedNetworkTypes) {
5732 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
5733 mApp, subId, "setAllowedNetworkTypes");
calvinpan0ac9c1a2020-01-14 20:42:55 +08005734
calvinpan8ed33732020-03-12 14:17:55 +08005735 SubscriptionManager.setSubscriptionProperty(subId,
5736 SubscriptionManager.ALLOWED_NETWORK_TYPES,
5737 String.valueOf(allowedNetworkTypes));
calvinpan0ac9c1a2020-01-14 20:42:55 +08005738
calvinpan8ed33732020-03-12 14:17:55 +08005739 int preferredNetworkMode = Settings.Global.getInt(mApp.getContentResolver(),
5740 Settings.Global.PREFERRED_NETWORK_MODE + subId,
5741 RILConstants.PREFERRED_NETWORK_MODE);
5742 return setPreferredNetworkType(subId, preferredNetworkMode);
calvinpan0ac9c1a2020-01-14 20:42:55 +08005743 }
5744
5745 /**
Sooraj Sasindranc46dfbd2020-06-03 01:06:00 -07005746 * Get the allowed network types for certain reason.
5747 *
5748 * @param subId the id of the subscription.
5749 * @param reason the reason the allowed network type change is taking place
5750 * @return the allowed network types.
5751 */
5752 @Override
5753 public long getAllowedNetworkTypesForReason(int subId,
5754 @TelephonyManager.AllowedNetworkTypesReason int reason) {
5755 TelephonyPermissions
5756 .enforeceCallingOrSelfReadPrivilegedPhoneStatePermissionOrCarrierPrivilege(
5757 mApp, subId, "getAllowedNetworkTypesForReason");
5758 final long identity = Binder.clearCallingIdentity();
5759 try {
5760 return getPhoneFromSubId(subId).getAllowedNetworkTypes(reason);
5761 } finally {
5762 Binder.restoreCallingIdentity(identity);
5763 }
5764 }
5765
5766 /**
5767 * Get the effective allowed network types on the device.
5768 * This API will return an intersection of allowed network types for all reasons,
5769 * including the configuration done through setAllowedNetworkTypes
5770 *
5771 * @param subId the id of the subscription.
5772 * @return the allowed network types
5773 */
5774 @Override
5775 public long getEffectiveAllowedNetworkTypes(int subId) {
5776 TelephonyPermissions
5777 .enforeceCallingOrSelfReadPrivilegedPhoneStatePermissionOrCarrierPrivilege(
5778 mApp, subId, "getEffectiveAllowedNetworkTypes");
5779 final long identity = Binder.clearCallingIdentity();
5780 try {
5781 return getPhoneFromSubId(subId).getEffectiveAllowedNetworkTypes();
5782 } finally {
5783 Binder.restoreCallingIdentity(identity);
5784 }
5785 }
5786
5787 /**
5788 * Set the allowed network types of the device and
5789 * provide the reason triggering the allowed network change.
5790 *
5791 * @param subId the id of the subscription.
5792 * @param reason the reason the allowed network type change is taking place
5793 * @param allowedNetworkTypes the allowed network types.
5794 * @return true on success; false on any failure.
5795 */
5796 @Override
5797 public boolean setAllowedNetworkTypesForReason(int subId,
5798 @TelephonyManager.AllowedNetworkTypesReason int reason, long allowedNetworkTypes) {
5799 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
5800 mApp, subId, "setAllowedNetworkTypesForReason");
5801 final long identity = Binder.clearCallingIdentity();
5802 try {
5803 getPhoneFromSubId(subId).setAllowedNetworkTypes(reason, allowedNetworkTypes);
5804 int preferredNetworkMode = Settings.Global.getInt(mApp.getContentResolver(),
5805 Settings.Global.PREFERRED_NETWORK_MODE + subId,
5806 RILConstants.PREFERRED_NETWORK_MODE);
5807 return setPreferredNetworkType(subId, preferredNetworkMode);
5808 } finally {
5809 Binder.restoreCallingIdentity(identity);
5810 }
5811 }
5812
5813 /**
Miaoa84611c2019-03-15 09:21:10 +08005814 * Check whether DUN APN is required for tethering with subId.
Junda Liu475951f2014-11-07 16:45:03 -08005815 *
Miaoa84611c2019-03-15 09:21:10 +08005816 * @param subId the id of the subscription to require tethering.
Amit Mahajanfe58cdf2017-07-11 12:01:53 -07005817 * @return {@code true} if DUN APN is required for tethering.
Junda Liu475951f2014-11-07 16:45:03 -08005818 * @hide
5819 */
5820 @Override
SongFerngWangf08d8122019-11-15 14:58:44 +08005821 public boolean isTetheringApnRequiredForSubscriber(int subId) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08005822 enforceModifyPermission();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005823 final long identity = Binder.clearCallingIdentity();
Miaoa84611c2019-03-15 09:21:10 +08005824 final Phone phone = getPhone(subId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005825 try {
Miaoa84611c2019-03-15 09:21:10 +08005826 if (phone != null) {
5827 return phone.hasMatchedTetherApnSetting();
5828 } else {
5829 return false;
5830 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005831 } finally {
5832 Binder.restoreCallingIdentity(identity);
Junda Liu475951f2014-11-07 16:45:03 -08005833 }
Junda Liu475951f2014-11-07 16:45:03 -08005834 }
5835
5836 /**
Shuo Qiancd19c462020-01-16 20:51:11 -08005837 * Enable or disable always reporting signal strength changes from radio.
5838 *
5839 * @param isEnable {@code true} for enabling; {@code false} for disabling.
5840 */
5841 @Override
5842 public void setAlwaysReportSignalStrength(int subId, boolean isEnable) {
5843 enforceModifyPermission();
5844 enforceSystemCaller();
5845
5846 final long identity = Binder.clearCallingIdentity();
5847 final Phone phone = getPhone(subId);
5848 try {
5849 if (phone != null) {
5850 if (DBG) {
5851 log("setAlwaysReportSignalStrength: subId=" + subId
5852 + " isEnable=" + isEnable);
5853 }
5854 phone.setAlwaysReportSignalStrength(isEnable);
5855 } else {
5856 loge("setAlwaysReportSignalStrength: no phone found for subId="
5857 + subId);
5858 }
5859 } finally {
5860 Binder.restoreCallingIdentity(identity);
5861 }
5862 }
5863
5864 /**
Malcolm Chen964682d2017-11-28 16:20:07 -08005865 * Get the user enabled state of Mobile Data.
5866 *
5867 * TODO: remove and use isUserDataEnabled.
5868 * This can't be removed now because some vendor codes
5869 * calls through ITelephony directly while they should
5870 * use TelephonyManager.
5871 *
5872 * @return true on enabled
5873 */
5874 @Override
5875 public boolean getDataEnabled(int subId) {
5876 return isUserDataEnabled(subId);
5877 }
5878
5879 /**
5880 * Get whether mobile data is enabled per user setting.
5881 *
5882 * There are other factors deciding whether mobile data is actually enabled, but they are
5883 * not considered here. See {@link #isDataEnabled(int)} for more details.
Robert Greenwalt646120a2014-05-23 11:54:03 -07005884 *
Jeff Davidsona1920712016-11-18 17:05:56 -08005885 * Accepts either ACCESS_NETWORK_STATE, MODIFY_PHONE_STATE or carrier privileges.
Robert Greenwalted86e582014-05-21 20:03:20 -07005886 *
5887 * @return {@code true} if data is enabled else {@code false}
5888 */
5889 @Override
Malcolm Chen964682d2017-11-28 16:20:07 -08005890 public boolean isUserDataEnabled(int subId) {
Robert Greenwalt646120a2014-05-23 11:54:03 -07005891 try {
5892 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.ACCESS_NETWORK_STATE,
5893 null);
5894 } catch (Exception e) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08005895 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
5896 mApp, subId, "isUserDataEnabled");
Robert Greenwalt646120a2014-05-23 11:54:03 -07005897 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005898
5899 final long identity = Binder.clearCallingIdentity();
5900 try {
5901 int phoneId = mSubscriptionController.getPhoneId(subId);
5902 if (DBG) log("isUserDataEnabled: subId=" + subId + " phoneId=" + phoneId);
5903 Phone phone = PhoneFactory.getPhone(phoneId);
5904 if (phone != null) {
5905 boolean retVal = phone.isUserDataEnabled();
5906 if (DBG) log("isUserDataEnabled: subId=" + subId + " retVal=" + retVal);
5907 return retVal;
5908 } else {
5909 if (DBG) loge("isUserDataEnabled: no phone subId=" + subId + " retVal=false");
5910 return false;
5911 }
5912 } finally {
5913 Binder.restoreCallingIdentity(identity);
Malcolm Chen964682d2017-11-28 16:20:07 -08005914 }
5915 }
5916
5917 /**
Shuo Qian8ee4e882020-01-08 14:30:06 -08005918 * Checks if the device is capable of mobile data by considering whether whether the
5919 * user has enabled mobile data, whether the carrier has enabled mobile data, and
5920 * whether the network policy allows data connections.
Malcolm Chen964682d2017-11-28 16:20:07 -08005921 *
Shuo Qian8ee4e882020-01-08 14:30:06 -08005922 * @return {@code true} if the overall data connection is capable; {@code false} if not.
Malcolm Chen964682d2017-11-28 16:20:07 -08005923 */
5924 @Override
5925 public boolean isDataEnabled(int subId) {
Sooraj Sasindran95c07a92020-07-15 01:35:24 -07005926 try {
5927 try {
5928 mApp.enforceCallingOrSelfPermission(
5929 android.Manifest.permission.ACCESS_NETWORK_STATE,
5930 null);
5931 } catch (Exception e) {
5932 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.READ_PHONE_STATE,
5933 "isDataEnabled");
5934 }
5935 } catch (Exception e) {
5936 enforceReadPrivilegedPermission("isDataEnabled");
5937 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005938
5939 final long identity = Binder.clearCallingIdentity();
5940 try {
5941 int phoneId = mSubscriptionController.getPhoneId(subId);
5942 if (DBG) log("isDataEnabled: subId=" + subId + " phoneId=" + phoneId);
5943 Phone phone = PhoneFactory.getPhone(phoneId);
5944 if (phone != null) {
Jack Yud79fba22018-12-13 11:51:28 -08005945 boolean retVal = phone.getDataEnabledSettings().isDataEnabled();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005946 if (DBG) log("isDataEnabled: subId=" + subId + " retVal=" + retVal);
5947 return retVal;
5948 } else {
5949 if (DBG) loge("isDataEnabled: no phone subId=" + subId + " retVal=false");
5950 return false;
5951 }
5952 } finally {
5953 Binder.restoreCallingIdentity(identity);
Wink Savillee7353bb2014-12-05 14:21:41 -08005954 }
Robert Greenwalted86e582014-05-21 20:03:20 -07005955 }
Shishir Agrawal60f9c952014-06-23 12:00:43 -07005956
Sooraj Sasindranaf1b5132020-05-05 21:06:20 +00005957 /**
5958 * Check if data is enabled for a specific reason
5959 * @param subId Subscription index
5960 * @param reason the reason the data enable change is taking place
5961 * @return {@code true} if the overall data is enabled; {@code false} if not.
5962 */
5963 @Override
Sooraj Sasindran95c07a92020-07-15 01:35:24 -07005964 public boolean isDataEnabledForReason(int subId,
Sooraj Sasindranaf1b5132020-05-05 21:06:20 +00005965 @TelephonyManager.DataEnabledReason int reason) {
5966 try {
5967 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.ACCESS_NETWORK_STATE,
5968 null);
5969 } catch (Exception e) {
5970 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.READ_PHONE_STATE,
Sooraj Sasindran95c07a92020-07-15 01:35:24 -07005971 "isDataEnabledForReason");
Sooraj Sasindranaf1b5132020-05-05 21:06:20 +00005972 }
5973
5974
5975 final long identity = Binder.clearCallingIdentity();
5976 try {
5977 int phoneId = mSubscriptionController.getPhoneId(subId);
5978 if (DBG) {
Sooraj Sasindran95c07a92020-07-15 01:35:24 -07005979 log("isDataEnabledForReason: subId=" + subId + " phoneId=" + phoneId
Sooraj Sasindranaf1b5132020-05-05 21:06:20 +00005980 + " reason=" + reason);
5981 }
5982 Phone phone = PhoneFactory.getPhone(phoneId);
5983 if (phone != null) {
5984 boolean retVal;
5985 if (reason == TelephonyManager.DATA_ENABLED_REASON_USER) {
5986 retVal = phone.isUserDataEnabled();
5987 } else {
Sooraj Sasindran95c07a92020-07-15 01:35:24 -07005988 retVal = phone.getDataEnabledSettings().isDataEnabledForReason(reason);
Sooraj Sasindranaf1b5132020-05-05 21:06:20 +00005989 }
Sooraj Sasindran95c07a92020-07-15 01:35:24 -07005990 if (DBG) log("isDataEnabledForReason: retVal=" + retVal);
Sooraj Sasindranaf1b5132020-05-05 21:06:20 +00005991 return retVal;
5992 } else {
5993 if (DBG) {
Sooraj Sasindran95c07a92020-07-15 01:35:24 -07005994 loge("isDataEnabledForReason: no phone subId="
Sooraj Sasindranaf1b5132020-05-05 21:06:20 +00005995 + subId + " retVal=false");
5996 }
5997 return false;
5998 }
5999 } finally {
6000 Binder.restoreCallingIdentity(identity);
6001 }
6002 }
6003
Malcolm Chendf0b4cc2020-02-24 15:12:43 -08006004 private int getCarrierPrivilegeStatusFromCarrierConfigRules(int privilegeFromSim, int uid,
Nazanin Bakhshi5d0636e2019-08-19 16:29:37 -07006005 Phone phone) {
Hall Liu54a2a0c2020-07-13 12:13:03 -07006006 if (uid == Process.SYSTEM_UID || uid == Process.PHONE_UID) {
6007 // Skip the check if it's one of these special uids
6008 return TelephonyManager.CARRIER_PRIVILEGE_STATUS_HAS_ACCESS;
6009 }
6010
Nazanin Bakhshi5d0636e2019-08-19 16:29:37 -07006011 //load access rules from carrier configs, and check those as well: b/139133814
6012 SubscriptionController subController = SubscriptionController.getInstance();
6013 if (privilegeFromSim == TelephonyManager.CARRIER_PRIVILEGE_STATUS_HAS_ACCESS
6014 || subController == null) return privilegeFromSim;
6015
Nazanin Bakhshi5d0636e2019-08-19 16:29:37 -07006016 PackageManager pkgMgr = phone.getContext().getPackageManager();
6017 String[] packages = pkgMgr.getPackagesForUid(uid);
6018
6019 final long identity = Binder.clearCallingIdentity();
6020 try {
Jeff Davidson8ab02b22020-03-28 12:24:40 -07006021 int subId = phone.getSubId();
6022 if (mCarrierPrivilegeTestOverrideSubIds.contains(subId)) {
6023 // A test override is in place for the privileges for this subId, so don't try to
6024 // read the subscription privileges.
6025 return privilegeFromSim;
6026 }
6027 SubscriptionInfo subInfo = subController.getSubscriptionInfo(subId);
Nazanin Bakhshi5d0636e2019-08-19 16:29:37 -07006028 SubscriptionManager subManager = (SubscriptionManager)
6029 phone.getContext().getSystemService(Context.TELEPHONY_SUBSCRIPTION_SERVICE);
6030 for (String pkg : packages) {
6031 if (subManager.canManageSubscription(subInfo, pkg)) {
6032 return TelephonyManager.CARRIER_PRIVILEGE_STATUS_HAS_ACCESS;
6033 }
6034 }
6035 return privilegeFromSim;
6036 } finally {
6037 Binder.restoreCallingIdentity(identity);
6038 }
6039 }
6040
6041 private int getCarrierPrivilegeStatusFromCarrierConfigRules(int privilegeFromSim, Phone phone,
6042 String pkgName) {
6043 //load access rules from carrier configs, and check those as well: b/139133814
6044 SubscriptionController subController = SubscriptionController.getInstance();
6045 if (privilegeFromSim == TelephonyManager.CARRIER_PRIVILEGE_STATUS_HAS_ACCESS
6046 || subController == null) return privilegeFromSim;
6047
6048 final long identity = Binder.clearCallingIdentity();
6049 try {
Jeff Davidson8ab02b22020-03-28 12:24:40 -07006050 int subId = phone.getSubId();
6051 if (mCarrierPrivilegeTestOverrideSubIds.contains(subId)) {
6052 // A test override is in place for the privileges for this subId, so don't try to
6053 // read the subscription privileges.
6054 return privilegeFromSim;
6055 }
6056 SubscriptionInfo subInfo = subController.getSubscriptionInfo(subId);
Nazanin Bakhshi5d0636e2019-08-19 16:29:37 -07006057 SubscriptionManager subManager = (SubscriptionManager)
6058 phone.getContext().getSystemService(Context.TELEPHONY_SUBSCRIPTION_SERVICE);
6059 return subManager.canManageSubscription(subInfo, pkgName)
6060 ? TelephonyManager.CARRIER_PRIVILEGE_STATUS_HAS_ACCESS : privilegeFromSim;
6061 } finally {
6062 Binder.restoreCallingIdentity(identity);
6063 }
6064 }
6065
Shishir Agrawal60f9c952014-06-23 12:00:43 -07006066 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08006067 public int getCarrierPrivilegeStatus(int subId) {
6068 final Phone phone = getPhone(subId);
6069 if (phone == null) {
6070 loge("getCarrierPrivilegeStatus: Invalid subId");
6071 return TelephonyManager.CARRIER_PRIVILEGE_STATUS_NO_ACCESS;
6072 }
6073 UiccCard card = UiccController.getInstance().getUiccCard(phone.getPhoneId());
Shishir Agrawaleb8771e2014-07-22 11:24:08 -07006074 if (card == null) {
Shishir Agrawal5e5becd2014-11-18 11:38:23 -08006075 loge("getCarrierPrivilegeStatus: No UICC");
Shishir Agrawaleb8771e2014-07-22 11:24:08 -07006076 return TelephonyManager.CARRIER_PRIVILEGE_STATUS_RULES_NOT_LOADED;
6077 }
Nazanin Bakhshi5d0636e2019-08-19 16:29:37 -07006078
6079 return getCarrierPrivilegeStatusFromCarrierConfigRules(
6080 card.getCarrierPrivilegeStatusForCurrentTransaction(
Malcolm Chendf0b4cc2020-02-24 15:12:43 -08006081 phone.getContext().getPackageManager()), Binder.getCallingUid(), phone);
Shishir Agrawal60f9c952014-06-23 12:00:43 -07006082 }
Junda Liu29340342014-07-10 15:23:27 -07006083
6084 @Override
Jeff Davidson7e17e312018-02-13 18:17:36 -08006085 public int getCarrierPrivilegeStatusForUid(int subId, int uid) {
Shuo Qian2c0ae432019-12-05 11:40:37 -08006086 enforceReadPrivilegedPermission("getCarrierPrivilegeStatusForUid");
Jeff Davidson7e17e312018-02-13 18:17:36 -08006087 final Phone phone = getPhone(subId);
6088 if (phone == null) {
Taesu Leef8fbed92019-10-07 18:47:02 +09006089 loge("getCarrierPrivilegeStatusForUid: Invalid subId");
Jeff Davidson7e17e312018-02-13 18:17:36 -08006090 return TelephonyManager.CARRIER_PRIVILEGE_STATUS_NO_ACCESS;
6091 }
6092 UiccProfile profile =
6093 UiccController.getInstance().getUiccProfileForPhone(phone.getPhoneId());
6094 if (profile == null) {
Taesu Leef8fbed92019-10-07 18:47:02 +09006095 loge("getCarrierPrivilegeStatusForUid: No UICC");
Jeff Davidson7e17e312018-02-13 18:17:36 -08006096 return TelephonyManager.CARRIER_PRIVILEGE_STATUS_RULES_NOT_LOADED;
6097 }
Nazanin Bakhshi5d0636e2019-08-19 16:29:37 -07006098 return getCarrierPrivilegeStatusFromCarrierConfigRules(
Shuo Qian2c0ae432019-12-05 11:40:37 -08006099 profile.getCarrierPrivilegeStatusForUid(
Malcolm Chendf0b4cc2020-02-24 15:12:43 -08006100 phone.getContext().getPackageManager(), uid), uid, phone);
Jeff Davidson7e17e312018-02-13 18:17:36 -08006101 }
6102
6103 @Override
chen xuf7e9fe82019-05-09 19:31:02 -07006104 public int checkCarrierPrivilegesForPackage(int subId, String pkgName) {
6105 if (TextUtils.isEmpty(pkgName)) {
Junda Liu317d70b2016-03-08 09:33:53 -08006106 return TelephonyManager.CARRIER_PRIVILEGE_STATUS_NO_ACCESS;
chen xuf7e9fe82019-05-09 19:31:02 -07006107 }
6108
6109 int phoneId = SubscriptionManager.getPhoneId(subId);
6110 UiccCard card = UiccController.getInstance().getUiccCard(phoneId);
Shishir Agrawaleb8771e2014-07-22 11:24:08 -07006111 if (card == null) {
chen xuf7e9fe82019-05-09 19:31:02 -07006112 loge("checkCarrierPrivilegesForPackage: No UICC on subId " + subId);
Shishir Agrawaleb8771e2014-07-22 11:24:08 -07006113 return TelephonyManager.CARRIER_PRIVILEGE_STATUS_RULES_NOT_LOADED;
6114 }
Nazanin Bakhshi5d0636e2019-08-19 16:29:37 -07006115 return getCarrierPrivilegeStatusFromCarrierConfigRules(
6116 card.getCarrierPrivilegeStatus(mApp.getPackageManager(), pkgName),
6117 getPhone(phoneId), pkgName);
Zach Johnson50ecba32015-05-19 00:24:21 -07006118 }
6119
6120 @Override
6121 public int checkCarrierPrivilegesForPackageAnyPhone(String pkgName) {
Junda Liu317d70b2016-03-08 09:33:53 -08006122 if (TextUtils.isEmpty(pkgName))
6123 return TelephonyManager.CARRIER_PRIVILEGE_STATUS_NO_ACCESS;
Zach Johnson50ecba32015-05-19 00:24:21 -07006124 int result = TelephonyManager.CARRIER_PRIVILEGE_STATUS_RULES_NOT_LOADED;
6125 for (int i = 0; i < TelephonyManager.getDefault().getPhoneCount(); i++) {
6126 UiccCard card = UiccController.getInstance().getUiccCard(i);
6127 if (card == null) {
Jonathan Basseri7d320df2015-06-16 12:17:08 -07006128 // No UICC in that slot.
Zach Johnson50ecba32015-05-19 00:24:21 -07006129 continue;
6130 }
6131
Nazanin Bakhshi5d0636e2019-08-19 16:29:37 -07006132 result = getCarrierPrivilegeStatusFromCarrierConfigRules(
6133 card.getCarrierPrivilegeStatus(mApp.getPackageManager(), pkgName),
6134 getPhone(i), pkgName);
Zach Johnson50ecba32015-05-19 00:24:21 -07006135 if (result == TelephonyManager.CARRIER_PRIVILEGE_STATUS_HAS_ACCESS) {
6136 break;
6137 }
6138 }
6139
6140 return result;
Junda Liu29340342014-07-10 15:23:27 -07006141 }
Derek Tan89e89d42014-07-08 17:00:10 -07006142
6143 @Override
Junda Liue64de782015-04-16 17:19:16 -07006144 public List<String> getCarrierPackageNamesForIntentAndPhone(Intent intent, int phoneId) {
6145 if (!SubscriptionManager.isValidPhoneId(phoneId)) {
6146 loge("phoneId " + phoneId + " is not valid.");
6147 return null;
6148 }
6149 UiccCard card = UiccController.getInstance().getUiccCard(phoneId);
Shishir Agrawaleb6439a2014-07-21 13:19:38 -07006150 if (card == null) {
Taesu Leef8fbed92019-10-07 18:47:02 +09006151 loge("getCarrierPackageNamesForIntentAndPhone: No UICC");
Shishir Agrawaleb6439a2014-07-21 13:19:38 -07006152 return null ;
6153 }
Nazanin Bakhshif782e562018-12-11 15:15:39 -08006154 return card.getCarrierPackageNamesForIntent(mApp.getPackageManager(), intent);
Shishir Agrawaleb6439a2014-07-21 13:19:38 -07006155 }
6156
Amith Yamasani6e118872016-02-19 12:53:51 -08006157 @Override
chen xuf7e9fe82019-05-09 19:31:02 -07006158 public List<String> getPackagesWithCarrierPrivileges(int phoneId) {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08006159 PackageManager pm = mApp.getPackageManager();
Amith Yamasani6e118872016-02-19 12:53:51 -08006160 List<String> privilegedPackages = new ArrayList<>();
6161 List<PackageInfo> packages = null;
chen xuf7e9fe82019-05-09 19:31:02 -07006162 UiccCard card = UiccController.getInstance().getUiccCard(phoneId);
6163 // has UICC in that slot.
6164 if (card != null) {
Amith Yamasani6e118872016-02-19 12:53:51 -08006165 if (card.hasCarrierPrivilegeRules()) {
6166 if (packages == null) {
6167 // Only check packages in user 0 for now
6168 packages = pm.getInstalledPackagesAsUser(
Nazanin Bakhshi5d0636e2019-08-19 16:29:37 -07006169 PackageManager.MATCH_DISABLED_COMPONENTS
6170 | PackageManager.MATCH_DISABLED_UNTIL_USED_COMPONENTS
Cheonho Park17089c62019-08-01 15:23:12 +09006171 | PackageManager.GET_SIGNING_CERTIFICATES,
Amit Mahajanb8f13202020-01-27 18:16:07 -08006172 UserHandle.SYSTEM.getIdentifier());
Amith Yamasani6e118872016-02-19 12:53:51 -08006173 }
6174 for (int p = packages.size() - 1; p >= 0; p--) {
6175 PackageInfo pkgInfo = packages.get(p);
6176 if (pkgInfo != null && pkgInfo.packageName != null
6177 && card.getCarrierPrivilegeStatus(pkgInfo)
chen xuf7e9fe82019-05-09 19:31:02 -07006178 == TelephonyManager.CARRIER_PRIVILEGE_STATUS_HAS_ACCESS) {
Amith Yamasani6e118872016-02-19 12:53:51 -08006179 privilegedPackages.add(pkgInfo.packageName);
6180 }
6181 }
6182 }
6183 }
6184 return privilegedPackages;
6185 }
6186
chen xuf7e9fe82019-05-09 19:31:02 -07006187 @Override
6188 public List<String> getPackagesWithCarrierPrivilegesForAllPhones() {
Shuo Qian067a06d2019-12-03 23:40:18 +00006189 enforceReadPrivilegedPermission("getPackagesWithCarrierPrivilegesForAllPhones");
6190
6191 final long identity = Binder.clearCallingIdentity();
6192
chen xuf7e9fe82019-05-09 19:31:02 -07006193 List<String> privilegedPackages = new ArrayList<>();
Shuo Qian067a06d2019-12-03 23:40:18 +00006194 try {
6195 for (int i = 0; i < TelephonyManager.getDefault().getPhoneCount(); i++) {
6196 privilegedPackages.addAll(getPackagesWithCarrierPrivileges(i));
6197 }
6198 } finally {
6199 Binder.restoreCallingIdentity(identity);
chen xuf7e9fe82019-05-09 19:31:02 -07006200 }
6201 return privilegedPackages;
6202 }
6203
Wink Savilleb564aae2014-10-23 10:18:09 -07006204 private String getIccId(int subId) {
Sanket Padawe356d7632015-06-22 14:03:32 -07006205 final Phone phone = getPhone(subId);
6206 UiccCard card = phone == null ? null : phone.getUiccCard();
Derek Tan97ebb422014-09-05 16:55:38 -07006207 if (card == null) {
6208 loge("getIccId: No UICC");
6209 return null;
6210 }
6211 String iccId = card.getIccId();
6212 if (TextUtils.isEmpty(iccId)) {
6213 loge("getIccId: ICC ID is null or empty.");
6214 return null;
6215 }
6216 return iccId;
6217 }
6218
Shishir Agrawaleb6439a2014-07-21 13:19:38 -07006219 @Override
Jeff Sharkey85190e62014-12-05 09:40:12 -08006220 public boolean setLine1NumberForDisplayForSubscriber(int subId, String alphaTag,
6221 String number) {
Shuo Qian2c0ae432019-12-05 11:40:37 -08006222 TelephonyPermissions.enforceCallingOrSelfCarrierPrivilege(mApp,
Jeff Davidson7e17e312018-02-13 18:17:36 -08006223 subId, "setLine1NumberForDisplayForSubscriber");
Derek Tan97ebb422014-09-05 16:55:38 -07006224
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006225 final long identity = Binder.clearCallingIdentity();
6226 try {
6227 final String iccId = getIccId(subId);
6228 final Phone phone = getPhone(subId);
6229 if (phone == null) {
6230 return false;
6231 }
6232 final String subscriberId = phone.getSubscriberId();
6233
6234 if (DBG_MERGE) {
Amit Mahajanb8f13202020-01-27 18:16:07 -08006235 Rlog.d(LOG_TAG, "Setting line number for ICC=" + iccId + ", subscriberId="
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006236 + subscriberId + " to " + number);
6237 }
6238
6239 if (TextUtils.isEmpty(iccId)) {
6240 return false;
6241 }
6242
6243 final SharedPreferences.Editor editor = mTelephonySharedPreferences.edit();
6244
6245 final String alphaTagPrefKey = PREF_CARRIERS_ALPHATAG_PREFIX + iccId;
6246 if (alphaTag == null) {
6247 editor.remove(alphaTagPrefKey);
6248 } else {
6249 editor.putString(alphaTagPrefKey, alphaTag);
6250 }
6251
6252 // Record both the line number and IMSI for this ICCID, since we need to
6253 // track all merged IMSIs based on line number
6254 final String numberPrefKey = PREF_CARRIERS_NUMBER_PREFIX + iccId;
6255 final String subscriberPrefKey = PREF_CARRIERS_SUBSCRIBER_PREFIX + iccId;
6256 if (number == null) {
6257 editor.remove(numberPrefKey);
6258 editor.remove(subscriberPrefKey);
6259 } else {
6260 editor.putString(numberPrefKey, number);
6261 editor.putString(subscriberPrefKey, subscriberId);
6262 }
6263
6264 editor.commit();
6265 return true;
6266 } finally {
6267 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07006268 }
Derek Tan7226c842014-07-02 17:42:23 -07006269 }
6270
6271 @Override
Philip P. Moltmann700a9592019-10-03 11:53:50 -07006272 public String getLine1NumberForDisplay(int subId, String callingPackage,
6273 String callingFeatureId) {
Makoto Onukifee69342015-06-29 14:44:50 -07006274 // This is open to apps with WRITE_SMS.
Jeff Davidson7e17e312018-02-13 18:17:36 -08006275 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneNumber(
Philip P. Moltmann700a9592019-10-03 11:53:50 -07006276 mApp, subId, callingPackage, callingFeatureId, "getLine1NumberForDisplay")) {
Amit Mahajan9cf11512015-11-09 11:40:48 -08006277 if (DBG_MERGE) log("getLine1NumberForDisplay returning null due to permission");
Svet Ganovb320e182015-04-16 12:30:10 -07006278 return null;
6279 }
Derek Tan97ebb422014-09-05 16:55:38 -07006280
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006281 final long identity = Binder.clearCallingIdentity();
6282 try {
6283 String iccId = getIccId(subId);
6284 if (iccId != null) {
6285 String numberPrefKey = PREF_CARRIERS_NUMBER_PREFIX + iccId;
6286 if (DBG_MERGE) {
6287 log("getLine1NumberForDisplay returning "
6288 + mTelephonySharedPreferences.getString(numberPrefKey, null));
6289 }
6290 return mTelephonySharedPreferences.getString(numberPrefKey, null);
Amit Mahajan9cf11512015-11-09 11:40:48 -08006291 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006292 if (DBG_MERGE) log("getLine1NumberForDisplay returning null as iccId is null");
6293 return null;
6294 } finally {
6295 Binder.restoreCallingIdentity(identity);
Derek Tan7226c842014-07-02 17:42:23 -07006296 }
Derek Tan7226c842014-07-02 17:42:23 -07006297 }
6298
6299 @Override
Philip P. Moltmann700a9592019-10-03 11:53:50 -07006300 public String getLine1AlphaTagForDisplay(int subId, String callingPackage,
6301 String callingFeatureId) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08006302 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
Philip P. Moltmann700a9592019-10-03 11:53:50 -07006303 mApp, subId, callingPackage, callingFeatureId, "getLine1AlphaTagForDisplay")) {
Svet Ganovb320e182015-04-16 12:30:10 -07006304 return null;
6305 }
Derek Tan97ebb422014-09-05 16:55:38 -07006306
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006307 final long identity = Binder.clearCallingIdentity();
6308 try {
6309 String iccId = getIccId(subId);
6310 if (iccId != null) {
6311 String alphaTagPrefKey = PREF_CARRIERS_ALPHATAG_PREFIX + iccId;
6312 return mTelephonySharedPreferences.getString(alphaTagPrefKey, null);
6313 }
6314 return null;
6315 } finally {
6316 Binder.restoreCallingIdentity(identity);
Derek Tan7226c842014-07-02 17:42:23 -07006317 }
Derek Tan7226c842014-07-02 17:42:23 -07006318 }
Shishir Agrawalb1ebf8c2014-07-17 16:32:41 -07006319
6320 @Override
Philip P. Moltmann700a9592019-10-03 11:53:50 -07006321 public String[] getMergedSubscriberIds(int subId, String callingPackage,
6322 String callingFeatureId) {
Jeff Davidson913390f2018-02-23 17:11:49 -08006323 // This API isn't public, so no need to provide a valid subscription ID - we're not worried
6324 // about carrier-privileged callers not having access.
Jeff Davidson7e17e312018-02-13 18:17:36 -08006325 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
Jeff Davidson913390f2018-02-23 17:11:49 -08006326 mApp, SubscriptionManager.INVALID_SUBSCRIPTION_ID, callingPackage,
Philip P. Moltmann700a9592019-10-03 11:53:50 -07006327 callingFeatureId, "getMergedSubscriberIds")) {
Fyodor Kupolov8e53b0b2015-06-17 13:17:50 -07006328 return null;
6329 }
Jeff Sharkey85190e62014-12-05 09:40:12 -08006330
Jordan Liub49b04b2019-05-06 14:45:15 -07006331 // Clear calling identity, when calling TelephonyManager, because callerUid must be
6332 // the process, where TelephonyManager was instantiated.
6333 // Otherwise AppOps check will fail.
Fyodor Kupolov8e53b0b2015-06-17 13:17:50 -07006334 final long identity = Binder.clearCallingIdentity();
6335 try {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08006336 final Context context = mApp;
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006337 final TelephonyManager tele = TelephonyManager.from(context);
6338 final SubscriptionManager sub = SubscriptionManager.from(context);
6339
6340 // Figure out what subscribers are currently active
6341 final ArraySet<String> activeSubscriberIds = new ArraySet<>();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006342
Jordan Liub49b04b2019-05-06 14:45:15 -07006343 // Only consider subs which match the current subId
6344 // This logic can be simplified. See b/131189269 for progress.
6345 if (isActiveSubscription(subId)) {
Fyodor Kupolov8e53b0b2015-06-17 13:17:50 -07006346 activeSubscriberIds.add(tele.getSubscriberId(subId));
6347 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006348
6349 // First pass, find a number override for an active subscriber
6350 String mergeNumber = null;
6351 final Map<String, ?> prefs = mTelephonySharedPreferences.getAll();
6352 for (String key : prefs.keySet()) {
6353 if (key.startsWith(PREF_CARRIERS_SUBSCRIBER_PREFIX)) {
6354 final String subscriberId = (String) prefs.get(key);
6355 if (activeSubscriberIds.contains(subscriberId)) {
6356 final String iccId = key.substring(
6357 PREF_CARRIERS_SUBSCRIBER_PREFIX.length());
6358 final String numberKey = PREF_CARRIERS_NUMBER_PREFIX + iccId;
6359 mergeNumber = (String) prefs.get(numberKey);
6360 if (DBG_MERGE) {
Amit Mahajanb8f13202020-01-27 18:16:07 -08006361 Rlog.d(LOG_TAG, "Found line number " + mergeNumber
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006362 + " for active subscriber " + subscriberId);
6363 }
6364 if (!TextUtils.isEmpty(mergeNumber)) {
6365 break;
6366 }
6367 }
6368 }
6369 }
6370
6371 // Shortcut when no active merged subscribers
6372 if (TextUtils.isEmpty(mergeNumber)) {
6373 return null;
6374 }
6375
6376 // Second pass, find all subscribers under that line override
6377 final ArraySet<String> result = new ArraySet<>();
6378 for (String key : prefs.keySet()) {
6379 if (key.startsWith(PREF_CARRIERS_NUMBER_PREFIX)) {
6380 final String number = (String) prefs.get(key);
6381 if (mergeNumber.equals(number)) {
6382 final String iccId = key.substring(PREF_CARRIERS_NUMBER_PREFIX.length());
6383 final String subscriberKey = PREF_CARRIERS_SUBSCRIBER_PREFIX + iccId;
6384 final String subscriberId = (String) prefs.get(subscriberKey);
6385 if (!TextUtils.isEmpty(subscriberId)) {
6386 result.add(subscriberId);
6387 }
6388 }
6389 }
6390 }
6391
6392 final String[] resultArray = result.toArray(new String[result.size()]);
6393 Arrays.sort(resultArray);
6394 if (DBG_MERGE) {
Amit Mahajanb8f13202020-01-27 18:16:07 -08006395 Rlog.d(LOG_TAG,
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006396 "Found subscribers " + Arrays.toString(resultArray) + " after merge");
6397 }
6398 return resultArray;
Fyodor Kupolov8e53b0b2015-06-17 13:17:50 -07006399 } finally {
6400 Binder.restoreCallingIdentity(identity);
Jeff Sharkey85190e62014-12-05 09:40:12 -08006401 }
Jeff Sharkey85190e62014-12-05 09:40:12 -08006402 }
6403
6404 @Override
zoey chen38003472019-12-13 17:16:31 +08006405 public String[] getMergedImsisFromGroup(int subId, String callingPackage) {
6406 enforceReadPrivilegedPermission("getMergedImsisFromGroup");
Malcolm Chen6ca97372019-07-01 16:28:21 -07006407
6408 final long identity = Binder.clearCallingIdentity();
6409 try {
6410 final TelephonyManager telephonyManager = mApp.getSystemService(
6411 TelephonyManager.class);
6412 String subscriberId = telephonyManager.getSubscriberId(subId);
6413 if (subscriberId == null) {
6414 if (DBG) {
zoey chen38003472019-12-13 17:16:31 +08006415 log("getMergedImsisFromGroup can't find subscriberId for subId "
Malcolm Chen6ca97372019-07-01 16:28:21 -07006416 + subId);
6417 }
6418 return null;
6419 }
6420
6421 final SubscriptionInfo info = SubscriptionController.getInstance()
6422 .getSubscriptionInfo(subId);
6423 final ParcelUuid groupUuid = info.getGroupUuid();
6424 // If it doesn't belong to any group, return just subscriberId of itself.
6425 if (groupUuid == null) {
6426 return new String[]{subscriberId};
6427 }
6428
6429 // Get all subscriberIds from the group.
6430 final List<String> mergedSubscriberIds = new ArrayList<>();
6431 final List<SubscriptionInfo> groupInfos = SubscriptionController.getInstance()
Philip P. Moltmann700a9592019-10-03 11:53:50 -07006432 .getSubscriptionsInGroup(groupUuid, mApp.getOpPackageName(),
Philip P. Moltmann8d34f0c2020-03-05 16:24:02 -08006433 mApp.getAttributionTag());
Malcolm Chen6ca97372019-07-01 16:28:21 -07006434 for (SubscriptionInfo subInfo : groupInfos) {
6435 subscriberId = telephonyManager.getSubscriberId(subInfo.getSubscriptionId());
6436 if (subscriberId != null) {
6437 mergedSubscriberIds.add(subscriberId);
6438 }
6439 }
6440
6441 return mergedSubscriberIds.toArray(new String[mergedSubscriberIds.size()]);
6442 } finally {
6443 Binder.restoreCallingIdentity(identity);
6444
6445 }
6446 }
6447
6448 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08006449 public boolean setOperatorBrandOverride(int subId, String brand) {
Shuo Qian2c0ae432019-12-05 11:40:37 -08006450 TelephonyPermissions.enforceCallingOrSelfCarrierPrivilege(mApp,
Jeff Davidson7e17e312018-02-13 18:17:36 -08006451 subId, "setOperatorBrandOverride");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006452
6453 final long identity = Binder.clearCallingIdentity();
6454 try {
6455 final Phone phone = getPhone(subId);
6456 return phone == null ? false : phone.setOperatorBrandOverride(brand);
6457 } finally {
6458 Binder.restoreCallingIdentity(identity);
6459 }
Shishir Agrawalb1ebf8c2014-07-17 16:32:41 -07006460 }
Steven Liu4bf01bc2014-07-17 11:05:29 -05006461
6462 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08006463 public boolean setRoamingOverride(int subId, List<String> gsmRoamingList,
Shishir Agrawal621a47c2014-12-01 10:25:09 -08006464 List<String> gsmNonRoamingList, List<String> cdmaRoamingList,
6465 List<String> cdmaNonRoamingList) {
Shuo Qian2c0ae432019-12-05 11:40:37 -08006466 TelephonyPermissions.enforceCallingOrSelfCarrierPrivilege(
6467 mApp, subId, "setRoamingOverride");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006468
6469 final long identity = Binder.clearCallingIdentity();
6470 try {
6471 final Phone phone = getPhone(subId);
6472 if (phone == null) {
6473 return false;
6474 }
6475 return phone.setRoamingOverride(gsmRoamingList, gsmNonRoamingList, cdmaRoamingList,
6476 cdmaNonRoamingList);
6477 } finally {
6478 Binder.restoreCallingIdentity(identity);
Shishir Agrawalc04d9752016-02-19 10:41:00 -08006479 }
Shishir Agrawal621a47c2014-12-01 10:25:09 -08006480 }
6481
6482 @Override
Shuo Qian850e4d6a2018-04-25 21:02:08 +00006483 @Deprecated
6484 public int invokeOemRilRequestRaw(byte[] oemReq, byte[] oemResp) {
6485 enforceModifyPermission();
6486
6487 int returnValue = 0;
6488 try {
vagdeviaf9a5b92018-08-15 16:01:53 -07006489 AsyncResult result = (AsyncResult) sendRequest(CMD_INVOKE_OEM_RIL_REQUEST_RAW, oemReq);
Shuo Qian850e4d6a2018-04-25 21:02:08 +00006490 if(result.exception == null) {
6491 if (result.result != null) {
6492 byte[] responseData = (byte[])(result.result);
6493 if(responseData.length > oemResp.length) {
6494 Log.w(LOG_TAG, "Buffer to copy response too small: Response length is " +
6495 responseData.length + "bytes. Buffer Size is " +
6496 oemResp.length + "bytes.");
6497 }
6498 System.arraycopy(responseData, 0, oemResp, 0, responseData.length);
6499 returnValue = responseData.length;
6500 }
6501 } else {
6502 CommandException ex = (CommandException) result.exception;
6503 returnValue = ex.getCommandError().ordinal();
6504 if(returnValue > 0) returnValue *= -1;
6505 }
6506 } catch (RuntimeException e) {
6507 Log.w(LOG_TAG, "sendOemRilRequestRaw: Runtime Exception");
6508 returnValue = (CommandException.Error.GENERIC_FAILURE.ordinal());
6509 if(returnValue > 0) returnValue *= -1;
6510 }
6511
6512 return returnValue;
6513 }
6514
6515 @Override
Wink Saville5d475dd2014-10-17 15:00:58 -07006516 public void setRadioCapability(RadioAccessFamily[] rafs) {
6517 try {
6518 ProxyController.getInstance().setRadioCapability(rafs);
6519 } catch (RuntimeException e) {
6520 Log.w(LOG_TAG, "setRadioCapability: Runtime Exception");
6521 }
6522 }
6523
6524 @Override
Robert Greenwalt36b23af2015-07-06 17:59:14 -07006525 public int getRadioAccessFamily(int phoneId, String callingPackage) {
Jeff Davidson913390f2018-02-23 17:11:49 -08006526 Phone phone = PhoneFactory.getPhone(phoneId);
Shuo Qiandee53402020-05-29 14:08:15 -07006527 try {
6528 TelephonyPermissions
6529 .enforeceCallingOrSelfReadPrivilegedPhoneStatePermissionOrCarrierPrivilege(
6530 mApp, phone.getSubId(), "getRadioAccessFamily");
6531 } catch (SecurityException e) {
6532 EventLog.writeEvent(0x534e4554, "150857259", -1, "Missing Permission");
6533 throw e;
6534 }
chen xub97461a2018-10-26 14:17:57 -07006535 int raf = RadioAccessFamily.RAF_UNKNOWN;
Jeff Davidson913390f2018-02-23 17:11:49 -08006536 if (phone == null) {
chen xub97461a2018-10-26 14:17:57 -07006537 return raf;
Jeff Davidson913390f2018-02-23 17:11:49 -08006538 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006539 final long identity = Binder.clearCallingIdentity();
6540 try {
chen xub97461a2018-10-26 14:17:57 -07006541 TelephonyPermissions
6542 .enforeceCallingOrSelfReadPrivilegedPhoneStatePermissionOrCarrierPrivilege(
6543 mApp, phone.getSubId(), "getRadioAccessFamily");
6544 raf = ProxyController.getInstance().getRadioAccessFamily(phoneId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006545 } finally {
6546 Binder.restoreCallingIdentity(identity);
6547 }
chen xub97461a2018-10-26 14:17:57 -07006548 return raf;
Wink Saville5d475dd2014-10-17 15:00:58 -07006549 }
Andrew Leedf14ead2014-10-17 14:22:52 -07006550
6551 @Override
6552 public void enableVideoCalling(boolean enable) {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08006553 final Phone defaultPhone = getDefaultPhone();
Andrew Leedf14ead2014-10-17 14:22:52 -07006554 enforceModifyPermission();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006555
6556 final long identity = Binder.clearCallingIdentity();
6557 try {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08006558 ImsManager.getInstance(defaultPhone.getContext(),
6559 defaultPhone.getPhoneId()).setVtSetting(enable);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006560 } finally {
6561 Binder.restoreCallingIdentity(identity);
6562 }
Andrew Leedf14ead2014-10-17 14:22:52 -07006563 }
6564
6565 @Override
Philip P. Moltmann700a9592019-10-03 11:53:50 -07006566 public boolean isVideoCallingEnabled(String callingPackage, String callingFeatureId) {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08006567 final Phone defaultPhone = getDefaultPhone();
Philip P. Moltmann700a9592019-10-03 11:53:50 -07006568 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(mApp, defaultPhone.getSubId(),
6569 callingPackage, callingFeatureId, "isVideoCallingEnabled")) {
Amit Mahajan578e53d2018-03-20 16:18:38 +00006570 return false;
6571 }
Svet Ganovb320e182015-04-16 12:30:10 -07006572
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006573 final long identity = Binder.clearCallingIdentity();
6574 try {
6575 // Check the user preference and the system-level IMS setting. Even if the user has
6576 // enabled video calling, if IMS is disabled we aren't able to support video calling.
6577 // In the long run, we may instead need to check if there exists a connection service
6578 // which can support video calling.
6579 ImsManager imsManager =
Nazanin Bakhshif782e562018-12-11 15:15:39 -08006580 ImsManager.getInstance(defaultPhone.getContext(), defaultPhone.getPhoneId());
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006581 return imsManager.isVtEnabledByPlatform()
6582 && imsManager.isEnhanced4gLteModeSettingEnabledByUser()
6583 && imsManager.isVtEnabledByUser();
6584 } finally {
6585 Binder.restoreCallingIdentity(identity);
6586 }
Andrew Leedf14ead2014-10-17 14:22:52 -07006587 }
Libin.Tang@motorola.comafe82642014-12-18 13:27:53 -06006588
Andrew Leea1239f22015-03-02 17:44:07 -08006589 @Override
Philip P. Moltmann700a9592019-10-03 11:53:50 -07006590 public boolean canChangeDtmfToneLength(int subId, String callingPackage,
6591 String callingFeatureId) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006592 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
Philip P. Moltmann700a9592019-10-03 11:53:50 -07006593 mApp, subId, callingPackage, callingFeatureId,
6594 "isVideoCallingEnabled")) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006595 return false;
6596 }
6597
6598 final long identity = Binder.clearCallingIdentity();
6599 try {
6600 CarrierConfigManager configManager =
6601 (CarrierConfigManager) mApp.getSystemService(Context.CARRIER_CONFIG_SERVICE);
Nazanin Bakhshif782e562018-12-11 15:15:39 -08006602 return configManager.getConfigForSubId(subId)
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006603 .getBoolean(CarrierConfigManager.KEY_DTMF_TYPE_ENABLED_BOOL);
6604 } finally {
6605 Binder.restoreCallingIdentity(identity);
6606 }
Andrew Leea1239f22015-03-02 17:44:07 -08006607 }
6608
6609 @Override
Philip P. Moltmann700a9592019-10-03 11:53:50 -07006610 public boolean isWorldPhone(int subId, String callingPackage, String callingFeatureId) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006611 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
Philip P. Moltmann700a9592019-10-03 11:53:50 -07006612 mApp, subId, callingPackage, callingFeatureId, "isVideoCallingEnabled")) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006613 return false;
6614 }
6615
6616 final long identity = Binder.clearCallingIdentity();
6617 try {
6618 CarrierConfigManager configManager =
6619 (CarrierConfigManager) mApp.getSystemService(Context.CARRIER_CONFIG_SERVICE);
Nazanin Bakhshif782e562018-12-11 15:15:39 -08006620 return configManager.getConfigForSubId(subId)
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006621 .getBoolean(CarrierConfigManager.KEY_WORLD_PHONE_BOOL);
6622 } finally {
6623 Binder.restoreCallingIdentity(identity);
6624 }
Andrew Leea1239f22015-03-02 17:44:07 -08006625 }
6626
Andrew Lee9431b832015-03-09 18:46:45 -07006627 @Override
6628 public boolean isTtyModeSupported() {
Tyler Gunn5ddfdc92019-10-31 13:08:23 -07006629 TelecomManager telecomManager = mApp.getSystemService(TelecomManager.class);
Wooki Wu1f82f7a2016-02-15 15:59:58 +08006630 return telecomManager.isTtySupported();
Andrew Lee9431b832015-03-09 18:46:45 -07006631 }
6632
6633 @Override
6634 public boolean isHearingAidCompatibilitySupported() {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006635 final long identity = Binder.clearCallingIdentity();
6636 try {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08006637 return mApp.getResources().getBoolean(R.bool.hac_enabled);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006638 } finally {
6639 Binder.restoreCallingIdentity(identity);
6640 }
Andrew Lee9431b832015-03-09 18:46:45 -07006641 }
6642
Hall Liuf6668912018-10-31 17:05:23 -07006643 /**
6644 * Determines whether the device currently supports RTT (Real-time text). Based both on carrier
6645 * support for the feature and device firmware support.
6646 *
6647 * @return {@code true} if the device and carrier both support RTT, {@code false} otherwise.
6648 */
6649 @Override
6650 public boolean isRttSupported(int subscriptionId) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006651 final long identity = Binder.clearCallingIdentity();
Nazanin Bakhshif782e562018-12-11 15:15:39 -08006652 final Phone phone = getPhone(subscriptionId);
6653 if (phone == null) {
6654 loge("isRttSupported: no Phone found. Invalid subId:" + subscriptionId);
6655 return false;
6656 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006657 try {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08006658 boolean isCarrierSupported = mApp.getCarrierConfigForSubId(subscriptionId).getBoolean(
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006659 CarrierConfigManager.KEY_RTT_SUPPORTED_BOOL);
6660 boolean isDeviceSupported =
Nazanin Bakhshif782e562018-12-11 15:15:39 -08006661 phone.getContext().getResources().getBoolean(R.bool.config_support_rtt);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006662 return isCarrierSupported && isDeviceSupported;
6663 } finally {
6664 Binder.restoreCallingIdentity(identity);
6665 }
Hall Liu98187582018-01-22 19:15:32 -08006666 }
6667
Hall Liuf6668912018-10-31 17:05:23 -07006668 /**
Hall Liuf2daa022019-07-23 18:39:00 -07006669 * Determines whether the user has turned on RTT. If the carrier wants to ignore the user-set
6670 * RTT setting, will return true if the device and carrier both support RTT.
6671 * Otherwise. only returns true if the device and carrier both also support RTT.
Hall Liuf6668912018-10-31 17:05:23 -07006672 */
6673 public boolean isRttEnabled(int subscriptionId) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006674 final long identity = Binder.clearCallingIdentity();
6675 try {
Hall Liu5bab75c2019-12-11 23:58:20 +00006676 boolean isRttSupported = isRttSupported(subscriptionId);
6677 boolean isUserRttSettingOn = Settings.Secure.getInt(
6678 mApp.getContentResolver(), Settings.Secure.RTT_CALLING_MODE, 0) != 0;
6679 boolean shouldIgnoreUserRttSetting = mApp.getCarrierConfigForSubId(subscriptionId)
6680 .getBoolean(CarrierConfigManager.KEY_IGNORE_RTT_MODE_SETTING_BOOL);
6681 return isRttSupported && (isUserRttSettingOn || shouldIgnoreUserRttSetting);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006682 } finally {
6683 Binder.restoreCallingIdentity(identity);
6684 }
Hall Liu3ad5f012018-04-06 16:23:39 -07006685 }
6686
Philip P. Moltmann700a9592019-10-03 11:53:50 -07006687 @Deprecated
6688 @Override
6689 public String getDeviceId(String callingPackage) {
6690 return getDeviceIdWithFeature(callingPackage, null);
6691 }
6692
Sanket Padawe7310cc72015-01-14 09:53:20 -08006693 /**
6694 * Returns the unique device ID of phone, for example, the IMEI for
6695 * GSM and the MEID for CDMA phones. Return null if device ID is not available.
6696 *
6697 * <p>Requires Permission:
6698 * {@link android.Manifest.permission#READ_PHONE_STATE READ_PHONE_STATE}
6699 */
6700 @Override
Philip P. Moltmann700a9592019-10-03 11:53:50 -07006701 public String getDeviceIdWithFeature(String callingPackage, String callingFeatureId) {
Sanket Padawe7310cc72015-01-14 09:53:20 -08006702 final Phone phone = PhoneFactory.getPhone(0);
Jeff Davidson913390f2018-02-23 17:11:49 -08006703 if (phone == null) {
Sanket Padawe7310cc72015-01-14 09:53:20 -08006704 return null;
6705 }
Jeff Davidson913390f2018-02-23 17:11:49 -08006706 int subId = phone.getSubId();
Michael Groover70af6dc2018-10-01 16:23:15 -07006707 if (!TelephonyPermissions.checkCallingOrSelfReadDeviceIdentifiers(mApp, subId,
Philip P. Moltmann700a9592019-10-03 11:53:50 -07006708 callingPackage, callingFeatureId, "getDeviceId")) {
Jeff Davidson913390f2018-02-23 17:11:49 -08006709 return null;
6710 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006711
6712 final long identity = Binder.clearCallingIdentity();
6713 try {
6714 return phone.getDeviceId();
6715 } finally {
6716 Binder.restoreCallingIdentity(identity);
6717 }
Sanket Padawe7310cc72015-01-14 09:53:20 -08006718 }
6719
Ping Sunc67b7c22016-03-02 19:16:45 +08006720 /**
6721 * {@hide}
6722 * Returns the IMS Registration Status on a particular subid
6723 *
6724 * @param subId
6725 */
Brad Ebinger1f2b5082018-02-08 16:11:32 -08006726 public boolean isImsRegistered(int subId) {
Ping Sunc67b7c22016-03-02 19:16:45 +08006727 Phone phone = getPhone(subId);
6728 if (phone != null) {
6729 return phone.isImsRegistered();
6730 } else {
6731 return false;
6732 }
6733 }
6734
Santos Cordon7a1885b2015-02-03 11:15:19 -08006735 @Override
6736 public int getSubIdForPhoneAccount(PhoneAccount phoneAccount) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006737 final long identity = Binder.clearCallingIdentity();
6738 try {
6739 return PhoneUtils.getSubIdForPhoneAccount(phoneAccount);
6740 } finally {
6741 Binder.restoreCallingIdentity(identity);
6742 }
Santos Cordon7a1885b2015-02-03 11:15:19 -08006743 }
Nathan Harolddcfc7932015-03-18 10:01:20 -07006744
Tyler Gunnf70ed162019-04-03 15:28:53 -07006745 @Override
Shuo Qian6e6137d2019-10-30 16:33:31 -07006746 public int getSubIdForPhoneAccountHandle(
Philip P. Moltmann700a9592019-10-03 11:53:50 -07006747 PhoneAccountHandle phoneAccountHandle, String callingPackage, String callingFeatureId) {
Shuo Qian6e6137d2019-10-30 16:33:31 -07006748 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(mApp, getDefaultSubscription(),
Philip P. Moltmann700a9592019-10-03 11:53:50 -07006749 callingPackage, callingFeatureId, "getSubIdForPhoneAccountHandle")) {
Shuo Qian6e6137d2019-10-30 16:33:31 -07006750 throw new SecurityException("Requires READ_PHONE_STATE permission.");
6751 }
6752 final long identity = Binder.clearCallingIdentity();
6753 try {
6754 return PhoneUtils.getSubIdForPhoneAccountHandle(phoneAccountHandle);
6755 } finally {
6756 Binder.restoreCallingIdentity(identity);
6757 }
6758 }
6759
6760 @Override
Tyler Gunnf70ed162019-04-03 15:28:53 -07006761 public @Nullable PhoneAccountHandle getPhoneAccountHandleForSubscriptionId(int subscriptionId) {
duki.hongfd96bde2020-07-22 17:32:19 +09006762 enforceReadPrivilegedPermission("getPhoneAccountHandleForSubscriptionId, "
6763 + "subscriptionId: " + subscriptionId);
Tyler Gunnf70ed162019-04-03 15:28:53 -07006764 final long identity = Binder.clearCallingIdentity();
6765 try {
6766 Phone phone = getPhone(subscriptionId);
6767 if (phone == null) {
6768 return null;
6769 }
6770 return PhoneUtils.makePstnPhoneAccountHandle(phone);
6771 } finally {
6772 Binder.restoreCallingIdentity(identity);
6773 }
6774 }
6775
Brad Ebinger1f2b5082018-02-08 16:11:32 -08006776 /**
6777 * @return the VoWiFi calling availability.
Nathan Haroldc55097a2015-03-11 18:14:50 -07006778 */
Brad Ebinger1f2b5082018-02-08 16:11:32 -08006779 public boolean isWifiCallingAvailable(int subId) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006780 final long identity = Binder.clearCallingIdentity();
6781 try {
6782 Phone phone = getPhone(subId);
6783 if (phone != null) {
6784 return phone.isWifiCallingEnabled();
6785 } else {
6786 return false;
6787 }
6788 } finally {
6789 Binder.restoreCallingIdentity(identity);
Brad Ebinger1f2b5082018-02-08 16:11:32 -08006790 }
Nathan Haroldc55097a2015-03-11 18:14:50 -07006791 }
6792
Brad Ebinger1f2b5082018-02-08 16:11:32 -08006793 /**
Brad Ebinger1f2b5082018-02-08 16:11:32 -08006794 * @return the VT calling availability.
Etan Cohen3b7a1bc2015-05-28 15:57:13 -07006795 */
Brad Ebinger1f2b5082018-02-08 16:11:32 -08006796 public boolean isVideoTelephonyAvailable(int subId) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006797 final long identity = Binder.clearCallingIdentity();
6798 try {
6799 Phone phone = getPhone(subId);
6800 if (phone != null) {
6801 return phone.isVideoEnabled();
6802 } else {
6803 return false;
6804 }
6805 } finally {
6806 Binder.restoreCallingIdentity(identity);
Brad Ebinger1f2b5082018-02-08 16:11:32 -08006807 }
6808 }
6809
6810 /**
6811 * @return the IMS registration technology for the MMTEL feature. Valid return values are
6812 * defined in {@link ImsRegistrationImplBase}.
6813 */
6814 public @ImsRegistrationImplBase.ImsRegistrationTech int getImsRegTechnologyForMmTel(int subId) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006815 final long identity = Binder.clearCallingIdentity();
6816 try {
6817 Phone phone = getPhone(subId);
6818 if (phone != null) {
6819 return phone.getImsRegistrationTech();
6820 } else {
6821 return ImsRegistrationImplBase.REGISTRATION_TECH_NONE;
6822 }
6823 } finally {
6824 Binder.restoreCallingIdentity(identity);
Brad Ebinger1f2b5082018-02-08 16:11:32 -08006825 }
Etan Cohen3b7a1bc2015-05-28 15:57:13 -07006826 }
6827
Stuart Scott8eef64f2015-04-08 15:13:54 -07006828 @Override
6829 public void factoryReset(int subId) {
paulhu5a773602019-08-23 19:17:33 +08006830 enforceSettingsPermission();
Stuart Scott981d8582015-04-21 14:09:50 -07006831 if (mUserManager.hasUserRestriction(UserManager.DISALLOW_NETWORK_RESET)) {
6832 return;
6833 }
6834
Svet Ganovcc087f82015-05-12 20:35:54 -07006835 final long identity = Binder.clearCallingIdentity();
Nazanin Bakhshif782e562018-12-11 15:15:39 -08006836
Svet Ganovcc087f82015-05-12 20:35:54 -07006837 try {
Stuart Scott981d8582015-04-21 14:09:50 -07006838 if (SubscriptionManager.isUsableSubIdValue(subId) && !mUserManager.hasUserRestriction(
6839 UserManager.DISALLOW_CONFIG_MOBILE_NETWORKS)) {
Sooraj Sasindran95c07a92020-07-15 01:35:24 -07006840 setDataEnabledForReason(subId, TelephonyManager.DATA_ENABLED_REASON_USER,
Sooraj Sasindranaf1b5132020-05-05 21:06:20 +00006841 getDefaultDataEnabled());
Svet Ganovcc087f82015-05-12 20:35:54 -07006842 setNetworkSelectionModeAutomatic(subId);
Pengquan Meng85728fb2018-03-12 16:31:21 -07006843 setPreferredNetworkType(subId, getDefaultNetworkType(subId));
Nazanin Bakhshif782e562018-12-11 15:15:39 -08006844 setDataRoamingEnabled(subId, getDefaultDataRoamingEnabled(subId));
6845 CarrierInfoManager.deleteAllCarrierKeysForImsiEncryption(mApp);
Svet Ganovcc087f82015-05-12 20:35:54 -07006846 }
Amit Mahajan7dbbd822019-03-13 17:33:47 -07006847 // There has been issues when Sms raw table somehow stores orphan
6848 // fragments. They lead to garbled message when new fragments come
6849 // in and combined with those stale ones. In case this happens again,
6850 // user can reset all network settings which will clean up this table.
6851 cleanUpSmsRawTable(getDefaultPhone().getContext());
Brad Ebingerbc7dd582019-10-17 17:03:22 -07006852 // Clean up IMS settings as well here.
6853 int slotId = getSlotIndex(subId);
6854 if (slotId > SubscriptionManager.INVALID_SIM_SLOT_INDEX) {
6855 ImsManager.getInstance(mApp, slotId).factoryReset();
6856 }
Naina Nallurid63128d2019-09-17 14:10:30 -07006857
6858 // Erase modem config if erase modem on network setting is enabled.
6859 String configValue = DeviceConfig.getProperty(DeviceConfig.NAMESPACE_TELEPHONY,
6860 RESET_NETWORK_ERASE_MODEM_CONFIG_ENABLED);
6861 if (configValue != null && Boolean.parseBoolean(configValue)) {
6862 sendEraseModemConfig(getDefaultPhone());
6863 }
Svet Ganovcc087f82015-05-12 20:35:54 -07006864 } finally {
6865 Binder.restoreCallingIdentity(identity);
Stuart Scott8eef64f2015-04-08 15:13:54 -07006866 }
6867 }
Narayan Kamath1c496c22015-04-16 14:40:19 +01006868
Amit Mahajan7dbbd822019-03-13 17:33:47 -07006869 private void cleanUpSmsRawTable(Context context) {
6870 ContentResolver resolver = context.getContentResolver();
6871 Uri uri = Uri.withAppendedPath(Telephony.Sms.CONTENT_URI, "raw/permanentDelete");
6872 resolver.delete(uri, null, null);
6873 }
6874
Narayan Kamath1c496c22015-04-16 14:40:19 +01006875 @Override
chen xu5d3637b2019-01-21 23:31:38 -08006876 public String getSimLocaleForSubscriber(int subId) {
6877 enforceReadPrivilegedPermission("getSimLocaleForSubscriber, subId: " + subId);
6878 final Phone phone = getPhone(subId);
6879 if (phone == null) {
6880 log("getSimLocaleForSubscriber, invalid subId");
chen xu2bb91e42019-01-24 14:35:54 -08006881 return null;
chen xu5d3637b2019-01-21 23:31:38 -08006882 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006883 final long identity = Binder.clearCallingIdentity();
6884 try {
chen xu5d3637b2019-01-21 23:31:38 -08006885 final SubscriptionInfo info = mSubscriptionController.getActiveSubscriptionInfo(subId,
Philip P. Moltmann8d34f0c2020-03-05 16:24:02 -08006886 phone.getContext().getOpPackageName(), phone.getContext().getAttributionTag());
chen xu6291c472019-02-04 12:55:53 -08006887 if (info == null) {
6888 log("getSimLocaleForSubscriber, inactive subId: " + subId);
6889 return null;
6890 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006891 // Try and fetch the locale from the carrier properties or from the SIM language
6892 // preferences (EF-PL and EF-LI)...
6893 final int mcc = info.getMcc();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006894 String simLanguage = null;
chen xu5d3637b2019-01-21 23:31:38 -08006895 final Locale localeFromDefaultSim = phone.getLocaleFromSimAndCarrierPrefs();
6896 if (localeFromDefaultSim != null) {
6897 if (!localeFromDefaultSim.getCountry().isEmpty()) {
6898 if (DBG) log("Using locale from subId: " + subId + " locale: "
6899 + localeFromDefaultSim);
6900 return localeFromDefaultSim.toLanguageTag();
6901 } else {
6902 simLanguage = localeFromDefaultSim.getLanguage();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006903 }
6904 }
Narayan Kamath1c496c22015-04-16 14:40:19 +01006905
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006906 // The SIM language preferences only store a language (e.g. fr = French), not an
6907 // exact locale (e.g. fr_FR = French/France). So, if the locale returned from
6908 // the SIM and carrier preferences does not include a country we add the country
6909 // determined from the SIM MCC to provide an exact locale.
zoey chenc730df82019-12-18 17:07:20 +08006910 final Locale mccLocale = LocaleUtils.getLocaleFromMcc(mApp, mcc, simLanguage);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006911 if (mccLocale != null) {
chen xu5d3637b2019-01-21 23:31:38 -08006912 if (DBG) log("No locale from SIM, using mcc locale:" + mccLocale);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006913 return mccLocale.toLanguageTag();
6914 }
6915
6916 if (DBG) log("No locale found - returning null");
6917 return null;
6918 } finally {
6919 Binder.restoreCallingIdentity(identity);
6920 }
Narayan Kamath1c496c22015-04-16 14:40:19 +01006921 }
6922
6923 private List<SubscriptionInfo> getAllSubscriptionInfoList() {
Philip P. Moltmann700a9592019-10-03 11:53:50 -07006924 return mSubscriptionController.getAllSubInfoList(mApp.getOpPackageName(),
Philip P. Moltmann8d34f0c2020-03-05 16:24:02 -08006925 mApp.getAttributionTag());
Narayan Kamath1c496c22015-04-16 14:40:19 +01006926 }
6927
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006928 /**
6929 * NOTE: this method assumes permission checks are done and caller identity has been cleared.
6930 */
6931 private List<SubscriptionInfo> getActiveSubscriptionInfoListPrivileged() {
Philip P. Moltmann700a9592019-10-03 11:53:50 -07006932 return mSubscriptionController.getActiveSubscriptionInfoList(mApp.getOpPackageName(),
Philip P. Moltmann8d34f0c2020-03-05 16:24:02 -08006933 mApp.getAttributionTag());
Narayan Kamath1c496c22015-04-16 14:40:19 +01006934 }
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -07006935
Chenjie Yu1ba97252018-01-11 18:16:20 -08006936 private final ModemActivityInfo mLastModemActivityInfo =
Chen Xud78231e2019-09-10 18:49:52 -07006937 new ModemActivityInfo(0, 0, 0, new int[0], 0);
Chenjie Yu1ba97252018-01-11 18:16:20 -08006938
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -07006939 /**
Adam Lesinski903a54c2016-04-11 14:49:52 -07006940 * Responds to the ResultReceiver with the {@link android.telephony.ModemActivityInfo} object
6941 * representing the state of the modem.
6942 *
Chenjie Yu1ba97252018-01-11 18:16:20 -08006943 * NOTE: The underlying implementation clears the modem state, so there should only ever be one
6944 * caller to it. Everyone should call this class to get cumulative data.
Adam Lesinski903a54c2016-04-11 14:49:52 -07006945 * @hide
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -07006946 */
6947 @Override
Adam Lesinski903a54c2016-04-11 14:49:52 -07006948 public void requestModemActivityInfo(ResultReceiver result) {
6949 enforceModifyPermission();
vagdeviaf9a5b92018-08-15 16:01:53 -07006950 WorkSource workSource = getWorkSource(Binder.getCallingUid());
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006951
6952 final long identity = Binder.clearCallingIdentity();
6953 try {
Shuo Qian8f4750a2020-02-20 17:12:10 -08006954 sendRequestAsync(CMD_GET_MODEM_ACTIVITY_INFO, result, null, workSource);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006955 } finally {
6956 Binder.restoreCallingIdentity(identity);
Chenjie Yu1ba97252018-01-11 18:16:20 -08006957 }
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -07006958 }
Jack Yu85bd38a2015-11-09 11:34:32 -08006959
Siddharth Rayb8114062018-06-17 15:02:38 -07006960 // Checks that ModemActivityInfo is valid. Sleep time, Idle time, Rx time and Tx time should be
6961 // less than total activity duration.
6962 private boolean isModemActivityInfoValid(ModemActivityInfo info) {
6963 if (info == null) {
6964 return false;
6965 }
6966 int activityDurationMs =
6967 (int) (info.getTimestamp() - mLastModemActivityInfo.getTimestamp());
6968 int totalTxTimeMs = 0;
Chen Xud78231e2019-09-10 18:49:52 -07006969 int[] txTimeMs = info.getTransmitTimeMillis();
6970 for (int i = 0; i < info.getTransmitPowerInfo().size(); i++) {
6971 totalTxTimeMs += txTimeMs[i];
Siddharth Rayb8114062018-06-17 15:02:38 -07006972 }
6973 return (info.isValid()
6974 && (info.getSleepTimeMillis() <= activityDurationMs)
6975 && (info.getIdleTimeMillis() <= activityDurationMs)
Chen Xud78231e2019-09-10 18:49:52 -07006976 && (info.getReceiveTimeMillis() <= activityDurationMs)
Siddharth Rayb8114062018-06-17 15:02:38 -07006977 && (totalTxTimeMs <= activityDurationMs));
6978 }
6979
Jack Yu85bd38a2015-11-09 11:34:32 -08006980 /**
6981 * {@hide}
6982 * Returns the service state information on specified subscription.
6983 */
6984 @Override
Philip P. Moltmann3a2772a2019-10-04 08:15:00 -07006985 public ServiceState getServiceStateForSubscriber(int subId, String callingPackage,
6986 String callingFeatureId) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08006987 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
Philip P. Moltmann700a9592019-10-03 11:53:50 -07006988 mApp, subId, callingPackage, callingFeatureId, "getServiceStateForSubscriber")) {
Jack Yu85bd38a2015-11-09 11:34:32 -08006989 return null;
6990 }
6991
Hall Liuf19c44f2018-11-27 14:38:17 -08006992 LocationAccessPolicy.LocationPermissionResult fineLocationResult =
6993 LocationAccessPolicy.checkLocationPermission(mApp,
6994 new LocationAccessPolicy.LocationPermissionQuery.Builder()
6995 .setCallingPackage(callingPackage)
Philip P. Moltmann3a2772a2019-10-04 08:15:00 -07006996 .setCallingFeatureId(callingFeatureId)
Hall Liuf19c44f2018-11-27 14:38:17 -08006997 .setCallingPid(Binder.getCallingPid())
6998 .setCallingUid(Binder.getCallingUid())
6999 .setMethod("getServiceStateForSubscriber")
Hall Liuf18a0cf2019-04-17 13:37:11 -07007000 .setLogAsInfo(true)
Hall Liuf19c44f2018-11-27 14:38:17 -08007001 .setMinSdkVersionForFine(Build.VERSION_CODES.Q)
Hall Liuc4a3e422020-05-26 17:18:03 -07007002 .setMinSdkVersionForCoarse(Build.VERSION_CODES.Q)
7003 .setMinSdkVersionForEnforcement(Build.VERSION_CODES.Q)
Hall Liuf19c44f2018-11-27 14:38:17 -08007004 .build());
7005
7006 LocationAccessPolicy.LocationPermissionResult coarseLocationResult =
7007 LocationAccessPolicy.checkLocationPermission(mApp,
7008 new LocationAccessPolicy.LocationPermissionQuery.Builder()
7009 .setCallingPackage(callingPackage)
Philip P. Moltmann3a2772a2019-10-04 08:15:00 -07007010 .setCallingFeatureId(callingFeatureId)
Hall Liuf19c44f2018-11-27 14:38:17 -08007011 .setCallingPid(Binder.getCallingPid())
7012 .setCallingUid(Binder.getCallingUid())
7013 .setMethod("getServiceStateForSubscriber")
Hall Liuf18a0cf2019-04-17 13:37:11 -07007014 .setLogAsInfo(true)
Hall Liuf19c44f2018-11-27 14:38:17 -08007015 .setMinSdkVersionForCoarse(Build.VERSION_CODES.Q)
Hall Liuc4a3e422020-05-26 17:18:03 -07007016 .setMinSdkVersionForFine(Integer.MAX_VALUE)
7017 .setMinSdkVersionForEnforcement(Build.VERSION_CODES.Q)
Hall Liuf19c44f2018-11-27 14:38:17 -08007018 .build());
7019 // We don't care about hard or soft here -- all we need to know is how much info to scrub.
7020 boolean hasFinePermission =
7021 fineLocationResult == LocationAccessPolicy.LocationPermissionResult.ALLOWED;
7022 boolean hasCoarsePermission =
7023 coarseLocationResult == LocationAccessPolicy.LocationPermissionResult.ALLOWED;
7024
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007025 final long identity = Binder.clearCallingIdentity();
7026 try {
Jordan Liuc437b192020-08-17 10:59:12 -07007027 // isActiveSubId requires READ_PHONE_STATE, which we already check for above
7028 if (!mSubscriptionController.isActiveSubId(subId, callingPackage, callingFeatureId)) {
7029 Rlog.d(LOG_TAG,
7030 "getServiceStateForSubscriber returning null for inactive subId=" + subId);
7031 return null;
7032 }
7033
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007034 final Phone phone = getPhone(subId);
7035 if (phone == null) {
7036 return null;
7037 }
Jack Yu85bd38a2015-11-09 11:34:32 -08007038
Hall Liuf19c44f2018-11-27 14:38:17 -08007039 ServiceState ss = phone.getServiceState();
7040
7041 // Scrub out the location info in ServiceState depending on what level of access
7042 // the caller has.
7043 if (hasFinePermission) return ss;
Malcolm Chen5052de62019-12-30 13:56:38 -08007044 if (hasCoarsePermission) return ss.createLocationInfoSanitizedCopy(false);
7045 return ss.createLocationInfoSanitizedCopy(true);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007046 } finally {
7047 Binder.restoreCallingIdentity(identity);
7048 }
Jack Yu85bd38a2015-11-09 11:34:32 -08007049 }
Nancy Chen31f9ba12016-01-06 11:42:12 -08007050
7051 /**
7052 * Returns the URI for the per-account voicemail ringtone set in Phone settings.
7053 *
7054 * @param accountHandle The handle for the {@link PhoneAccount} for which to retrieve the
7055 * voicemail ringtone.
7056 * @return The URI for the ringtone to play when receiving a voicemail from a specific
7057 * PhoneAccount.
7058 */
7059 @Override
7060 public Uri getVoicemailRingtoneUri(PhoneAccountHandle accountHandle) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007061 final long identity = Binder.clearCallingIdentity();
7062 try {
7063 Phone phone = PhoneUtils.getPhoneForPhoneAccountHandle(accountHandle);
7064 if (phone == null) {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08007065 phone = getDefaultPhone();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007066 }
Nancy Chen31f9ba12016-01-06 11:42:12 -08007067
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007068 return VoicemailNotificationSettingsUtil.getRingtoneUri(phone.getContext());
7069 } finally {
7070 Binder.restoreCallingIdentity(identity);
7071 }
Nancy Chen31f9ba12016-01-06 11:42:12 -08007072 }
7073
7074 /**
Ta-wei Yenc33877d2017-01-23 18:11:21 -08007075 * Sets the per-account voicemail ringtone.
7076 *
7077 * <p>Requires that the calling app is the default dialer, or has carrier privileges, or
7078 * has permission {@link android.Manifest.permission#MODIFY_PHONE_STATE MODIFY_PHONE_STATE}.
7079 *
7080 * @param phoneAccountHandle The handle for the {@link PhoneAccount} for which to set the
7081 * voicemail ringtone.
7082 * @param uri The URI for the ringtone to play when receiving a voicemail from a specific
7083 * PhoneAccount.
7084 */
7085 @Override
7086 public void setVoicemailRingtoneUri(String callingPackage,
7087 PhoneAccountHandle phoneAccountHandle, Uri uri) {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08007088 final Phone defaultPhone = getDefaultPhone();
Ta-wei Yenc33877d2017-01-23 18:11:21 -08007089 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
Tyler Gunn5ddfdc92019-10-31 13:08:23 -07007090 TelecomManager tm = defaultPhone.getContext().getSystemService(TelecomManager.class);
7091 if (!TextUtils.equals(callingPackage, tm.getDefaultDialerPackage())) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08007092 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
7093 mApp, PhoneUtils.getSubIdForPhoneAccountHandle(phoneAccountHandle),
7094 "setVoicemailRingtoneUri");
Ta-wei Yenc33877d2017-01-23 18:11:21 -08007095 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007096
7097 final long identity = Binder.clearCallingIdentity();
7098 try {
7099 Phone phone = PhoneUtils.getPhoneForPhoneAccountHandle(phoneAccountHandle);
7100 if (phone == null) {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08007101 phone = defaultPhone;
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007102 }
7103 VoicemailNotificationSettingsUtil.setRingtoneUri(phone.getContext(), uri);
7104 } finally {
7105 Binder.restoreCallingIdentity(identity);
Ta-wei Yenc33877d2017-01-23 18:11:21 -08007106 }
Ta-wei Yenc33877d2017-01-23 18:11:21 -08007107 }
7108
7109 /**
Nancy Chen31f9ba12016-01-06 11:42:12 -08007110 * Returns whether vibration is set for voicemail notification in Phone settings.
7111 *
7112 * @param accountHandle The handle for the {@link PhoneAccount} for which to retrieve the
7113 * voicemail vibration setting.
7114 * @return {@code true} if the vibration is set for this PhoneAccount, {@code false} otherwise.
7115 */
7116 @Override
7117 public boolean isVoicemailVibrationEnabled(PhoneAccountHandle accountHandle) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007118 final long identity = Binder.clearCallingIdentity();
7119 try {
7120 Phone phone = PhoneUtils.getPhoneForPhoneAccountHandle(accountHandle);
7121 if (phone == null) {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08007122 phone = getDefaultPhone();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007123 }
Nancy Chen31f9ba12016-01-06 11:42:12 -08007124
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007125 return VoicemailNotificationSettingsUtil.isVibrationEnabled(phone.getContext());
7126 } finally {
7127 Binder.restoreCallingIdentity(identity);
7128 }
Nancy Chen31f9ba12016-01-06 11:42:12 -08007129 }
7130
Youhan Wange64578a2016-05-02 15:32:42 -07007131 /**
Ta-wei Yenc33877d2017-01-23 18:11:21 -08007132 * Sets the per-account voicemail vibration.
7133 *
7134 * <p>Requires that the calling app is the default dialer, or has carrier privileges, or
7135 * has permission {@link android.Manifest.permission#MODIFY_PHONE_STATE MODIFY_PHONE_STATE}.
7136 *
7137 * @param phoneAccountHandle The handle for the {@link PhoneAccount} for which to set the
7138 * voicemail vibration setting.
7139 * @param enabled Whether to enable or disable vibration for voicemail notifications from a
7140 * specific PhoneAccount.
7141 */
7142 @Override
7143 public void setVoicemailVibrationEnabled(String callingPackage,
7144 PhoneAccountHandle phoneAccountHandle, boolean enabled) {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08007145 final Phone defaultPhone = getDefaultPhone();
Ta-wei Yenc33877d2017-01-23 18:11:21 -08007146 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
Tyler Gunn5ddfdc92019-10-31 13:08:23 -07007147 TelecomManager tm = defaultPhone.getContext().getSystemService(TelecomManager.class);
7148 if (!TextUtils.equals(callingPackage, tm.getDefaultDialerPackage())) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08007149 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
7150 mApp, PhoneUtils.getSubIdForPhoneAccountHandle(phoneAccountHandle),
7151 "setVoicemailVibrationEnabled");
Ta-wei Yenc33877d2017-01-23 18:11:21 -08007152 }
7153
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007154 final long identity = Binder.clearCallingIdentity();
7155 try {
7156 Phone phone = PhoneUtils.getPhoneForPhoneAccountHandle(phoneAccountHandle);
7157 if (phone == null) {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08007158 phone = defaultPhone;
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007159 }
7160 VoicemailNotificationSettingsUtil.setVibrationEnabled(phone.getContext(), enabled);
7161 } finally {
7162 Binder.restoreCallingIdentity(identity);
Ta-wei Yenc33877d2017-01-23 18:11:21 -08007163 }
Ta-wei Yenc33877d2017-01-23 18:11:21 -08007164 }
7165
7166 /**
Youhan Wange64578a2016-05-02 15:32:42 -07007167 * Make sure either called from same process as self (phone) or IPC caller has read privilege.
7168 *
7169 * @throws SecurityException if the caller does not have the required permission
7170 */
Brad Ebinger35c841c2018-10-01 10:40:55 -07007171 private void enforceReadPrivilegedPermission(String message) {
Youhan Wange64578a2016-05-02 15:32:42 -07007172 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.READ_PRIVILEGED_PHONE_STATE,
Brad Ebinger35c841c2018-10-01 10:40:55 -07007173 message);
Youhan Wange64578a2016-05-02 15:32:42 -07007174 }
7175
7176 /**
Ta-wei Yen30a69c82016-12-27 14:52:32 -08007177 * Make sure either called from same process as self (phone) or IPC caller has send SMS
7178 * permission.
7179 *
7180 * @throws SecurityException if the caller does not have the required permission
7181 */
7182 private void enforceSendSmsPermission() {
7183 mApp.enforceCallingOrSelfPermission(permission.SEND_SMS, null);
7184 }
7185
7186 /**
Ta-wei Yen527a9c02017-01-06 15:29:25 -08007187 * Make sure called from the package in charge of visual voicemail.
Ta-wei Yen30a69c82016-12-27 14:52:32 -08007188 *
Ta-wei Yen527a9c02017-01-06 15:29:25 -08007189 * @throws SecurityException if the caller is not the visual voicemail package.
Ta-wei Yen30a69c82016-12-27 14:52:32 -08007190 */
Ta-wei Yen527a9c02017-01-06 15:29:25 -08007191 private void enforceVisualVoicemailPackage(String callingPackage, int subId) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007192 final long identity = Binder.clearCallingIdentity();
7193 try {
7194 ComponentName componentName =
Nazanin Bakhshif782e562018-12-11 15:15:39 -08007195 RemoteVvmTaskManager.getRemotePackage(mApp, subId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007196 if (componentName == null) {
7197 throw new SecurityException(
7198 "Caller not current active visual voicemail package[null]");
7199 }
7200 String vvmPackage = componentName.getPackageName();
7201 if (!callingPackage.equals(vvmPackage)) {
7202 throw new SecurityException("Caller not current active visual voicemail package["
7203 + vvmPackage + "]");
7204 }
7205 } finally {
7206 Binder.restoreCallingIdentity(identity);
Ta-wei Yen30a69c82016-12-27 14:52:32 -08007207 }
7208 }
7209
7210 /**
Youhan Wange64578a2016-05-02 15:32:42 -07007211 * Return the application ID for the app type.
7212 *
7213 * @param subId the subscription ID that this request applies to.
7214 * @param appType the uicc app type.
7215 * @return Application ID for specificied app type, or null if no uicc.
7216 */
7217 @Override
7218 public String getAidForAppType(int subId, int appType) {
Brad Ebinger35c841c2018-10-01 10:40:55 -07007219 enforceReadPrivilegedPermission("getAidForAppType");
Youhan Wange64578a2016-05-02 15:32:42 -07007220 Phone phone = getPhone(subId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007221
7222 final long identity = Binder.clearCallingIdentity();
Youhan Wange64578a2016-05-02 15:32:42 -07007223 try {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007224 if (phone == null) {
7225 return null;
7226 }
7227 String aid = null;
7228 try {
7229 aid = UiccController.getInstance().getUiccCard(phone.getPhoneId())
7230 .getApplicationByType(appType).getAid();
7231 } catch (Exception e) {
7232 Log.e(LOG_TAG, "Not getting aid. Exception ex=" + e);
7233 }
7234 return aid;
7235 } finally {
7236 Binder.restoreCallingIdentity(identity);
Youhan Wange64578a2016-05-02 15:32:42 -07007237 }
Youhan Wange64578a2016-05-02 15:32:42 -07007238 }
7239
Youhan Wang4001d252016-05-11 10:29:41 -07007240 /**
7241 * Return the Electronic Serial Number.
7242 *
7243 * @param subId the subscription ID that this request applies to.
7244 * @return ESN or null if error.
7245 */
7246 @Override
7247 public String getEsn(int subId) {
Brad Ebinger35c841c2018-10-01 10:40:55 -07007248 enforceReadPrivilegedPermission("getEsn");
Youhan Wang4001d252016-05-11 10:29:41 -07007249 Phone phone = getPhone(subId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007250
7251 final long identity = Binder.clearCallingIdentity();
Youhan Wang4001d252016-05-11 10:29:41 -07007252 try {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007253 if (phone == null) {
7254 return null;
7255 }
7256 String esn = null;
7257 try {
7258 esn = phone.getEsn();
7259 } catch (Exception e) {
7260 Log.e(LOG_TAG, "Not getting ESN. Exception ex=" + e);
7261 }
7262 return esn;
7263 } finally {
7264 Binder.restoreCallingIdentity(identity);
Youhan Wang4001d252016-05-11 10:29:41 -07007265 }
Youhan Wang4001d252016-05-11 10:29:41 -07007266 }
7267
Sanket Padawe99ef1e32016-05-18 16:12:33 -07007268 /**
Youhan Wang66ad5d72016-07-18 17:56:58 -07007269 * Return the Preferred Roaming List Version.
7270 *
7271 * @param subId the subscription ID that this request applies to.
7272 * @return PRLVersion or null if error.
7273 */
7274 @Override
7275 public String getCdmaPrlVersion(int subId) {
Brad Ebinger35c841c2018-10-01 10:40:55 -07007276 enforceReadPrivilegedPermission("getCdmaPrlVersion");
Youhan Wang66ad5d72016-07-18 17:56:58 -07007277 Phone phone = getPhone(subId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007278
7279 final long identity = Binder.clearCallingIdentity();
Youhan Wang66ad5d72016-07-18 17:56:58 -07007280 try {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007281 if (phone == null) {
7282 return null;
7283 }
7284 String cdmaPrlVersion = null;
7285 try {
7286 cdmaPrlVersion = phone.getCdmaPrlVersion();
7287 } catch (Exception e) {
7288 Log.e(LOG_TAG, "Not getting PRLVersion", e);
7289 }
7290 return cdmaPrlVersion;
7291 } finally {
7292 Binder.restoreCallingIdentity(identity);
Youhan Wang66ad5d72016-07-18 17:56:58 -07007293 }
Youhan Wang66ad5d72016-07-18 17:56:58 -07007294 }
7295
7296 /**
Sanket Padawe99ef1e32016-05-18 16:12:33 -07007297 * Get snapshot of Telephony histograms
7298 * @return List of Telephony histograms
7299 * @hide
7300 */
7301 @Override
7302 public List<TelephonyHistogram> getTelephonyHistograms() {
Jeff Davidson7e17e312018-02-13 18:17:36 -08007303 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
7304 mApp, getDefaultSubscription(), "getTelephonyHistograms");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007305
7306 final long identity = Binder.clearCallingIdentity();
7307 try {
7308 return RIL.getTelephonyRILTimingHistograms();
7309 } finally {
7310 Binder.restoreCallingIdentity(identity);
7311 }
Sanket Padawe99ef1e32016-05-18 16:12:33 -07007312 }
Meng Wang1a7c35a2016-05-05 20:56:15 -07007313
7314 /**
7315 * {@hide}
Michele Berionne482f8202018-11-27 18:57:59 -08007316 * Set the allowed carrier list and the excluded carrier list, indicating the priority between
7317 * the two lists.
Meng Wang1a7c35a2016-05-05 20:56:15 -07007318 * Require system privileges. In the future we may add this to carrier APIs.
7319 *
Michele Berionne482f8202018-11-27 18:57:59 -08007320 * @return Integer with the result of the operation, as defined in {@link TelephonyManager}.
Meng Wang1a7c35a2016-05-05 20:56:15 -07007321 */
7322 @Override
Michele Berionne482f8202018-11-27 18:57:59 -08007323 @TelephonyManager.SetCarrierRestrictionResult
7324 public int setAllowedCarriers(CarrierRestrictionRules carrierRestrictionRules) {
Meng Wang1a7c35a2016-05-05 20:56:15 -07007325 enforceModifyPermission();
vagdeviaf9a5b92018-08-15 16:01:53 -07007326 WorkSource workSource = getWorkSource(Binder.getCallingUid());
Sanket Padawe13bac7b2017-03-20 15:04:47 -07007327
Michele Berionne482f8202018-11-27 18:57:59 -08007328 if (carrierRestrictionRules == null) {
7329 throw new NullPointerException("carrier restriction cannot be null");
Meng Wang9b7c4e92017-02-17 11:41:27 -08007330 }
Sanket Padawe13bac7b2017-03-20 15:04:47 -07007331
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007332 final long identity = Binder.clearCallingIdentity();
7333 try {
Michele Berionne482f8202018-11-27 18:57:59 -08007334 return (int) sendRequest(CMD_SET_ALLOWED_CARRIERS, carrierRestrictionRules,
vagdeviaf9a5b92018-08-15 16:01:53 -07007335 workSource);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007336 } finally {
7337 Binder.restoreCallingIdentity(identity);
7338 }
Meng Wang1a7c35a2016-05-05 20:56:15 -07007339 }
7340
7341 /**
7342 * {@hide}
Michele Berionne482f8202018-11-27 18:57:59 -08007343 * Get the allowed carrier list and the excluded carrier list, including the priority between
7344 * the two lists.
Meng Wang1a7c35a2016-05-05 20:56:15 -07007345 * Require system privileges. In the future we may add this to carrier APIs.
7346 *
Michele Berionne482f8202018-11-27 18:57:59 -08007347 * @return {@link android.telephony.CarrierRestrictionRules}
Meng Wang1a7c35a2016-05-05 20:56:15 -07007348 */
7349 @Override
Michele Berionne482f8202018-11-27 18:57:59 -08007350 public CarrierRestrictionRules getAllowedCarriers() {
Brad Ebinger35c841c2018-10-01 10:40:55 -07007351 enforceReadPrivilegedPermission("getAllowedCarriers");
vagdeviaf9a5b92018-08-15 16:01:53 -07007352 WorkSource workSource = getWorkSource(Binder.getCallingUid());
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007353
7354 final long identity = Binder.clearCallingIdentity();
7355 try {
Michele Berionne482f8202018-11-27 18:57:59 -08007356 Object response = sendRequest(CMD_GET_ALLOWED_CARRIERS, null, workSource);
7357 if (response instanceof CarrierRestrictionRules) {
7358 return (CarrierRestrictionRules) response;
7359 }
7360 // Response is an Exception of some kind,
7361 // which is signalled to the user as a NULL retval
7362 return null;
7363 } catch (Exception e) {
7364 Log.e(LOG_TAG, "getAllowedCarriers. Exception ex=" + e);
7365 return null;
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007366 } finally {
7367 Binder.restoreCallingIdentity(identity);
7368 }
Meng Wang1a7c35a2016-05-05 20:56:15 -07007369 }
7370
fionaxu59545b42016-05-25 15:53:37 -07007371 /**
fionaxu59545b42016-05-25 15:53:37 -07007372 * Action set from carrier signalling broadcast receivers to enable/disable radio
7373 * @param subId the subscription ID that this action applies to.
7374 * @param enabled control enable or disable radio.
7375 * {@hide}
7376 */
7377 @Override
7378 public void carrierActionSetRadioEnabled(int subId, boolean enabled) {
7379 enforceModifyPermission();
7380 final Phone phone = getPhone(subId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007381
7382 final long identity = Binder.clearCallingIdentity();
fionaxu59545b42016-05-25 15:53:37 -07007383 if (phone == null) {
7384 loge("carrierAction: SetRadioEnabled fails with invalid sibId: " + subId);
7385 return;
7386 }
7387 try {
7388 phone.carrierActionSetRadioEnabled(enabled);
7389 } catch (Exception e) {
7390 Log.e(LOG_TAG, "carrierAction: SetRadioEnabled fails. Exception ex=" + e);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007391 } finally {
7392 Binder.restoreCallingIdentity(identity);
fionaxu59545b42016-05-25 15:53:37 -07007393 }
7394 }
7395
Ta-wei Yenc236d6b2016-06-21 13:33:12 -07007396 /**
fionaxu8da9cb12017-05-23 15:02:46 -07007397 * Action set from carrier signalling broadcast receivers to start/stop reporting the default
7398 * network status based on which carrier apps could apply actions accordingly,
7399 * enable/disable default url handler for example.
7400 *
7401 * @param subId the subscription ID that this action applies to.
7402 * @param report control start/stop reporting the default network status.
7403 * {@hide}
7404 */
7405 @Override
7406 public void carrierActionReportDefaultNetworkStatus(int subId, boolean report) {
7407 enforceModifyPermission();
7408 final Phone phone = getPhone(subId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007409
7410 final long identity = Binder.clearCallingIdentity();
fionaxu8da9cb12017-05-23 15:02:46 -07007411 if (phone == null) {
7412 loge("carrierAction: ReportDefaultNetworkStatus fails with invalid sibId: " + subId);
7413 return;
7414 }
7415 try {
7416 phone.carrierActionReportDefaultNetworkStatus(report);
7417 } catch (Exception e) {
7418 Log.e(LOG_TAG, "carrierAction: ReportDefaultNetworkStatus fails. Exception ex=" + e);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007419 } finally {
7420 Binder.restoreCallingIdentity(identity);
fionaxu8da9cb12017-05-23 15:02:46 -07007421 }
7422 }
7423
7424 /**
fionaxud9622282017-07-17 17:51:30 -07007425 * Action set from carrier signalling broadcast receivers to reset all carrier actions
7426 * @param subId the subscription ID that this action applies to.
7427 * {@hide}
7428 */
7429 @Override
7430 public void carrierActionResetAll(int subId) {
7431 enforceModifyPermission();
7432 final Phone phone = getPhone(subId);
7433 if (phone == null) {
7434 loge("carrierAction: ResetAll fails with invalid sibId: " + subId);
7435 return;
7436 }
7437 try {
7438 phone.carrierActionResetAll();
7439 } catch (Exception e) {
7440 Log.e(LOG_TAG, "carrierAction: ResetAll fails. Exception ex=" + e);
7441 }
7442 }
7443
7444 /**
Ta-wei Yenc236d6b2016-06-21 13:33:12 -07007445 * Called when "adb shell dumpsys phone" is invoked. Dump is also automatically invoked when a
7446 * bug report is being generated.
7447 */
7448 @Override
Ta-wei Yen99282e02016-06-21 18:19:35 -07007449 protected void dump(FileDescriptor fd, PrintWriter writer, String[] args) {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08007450 if (mApp.checkCallingOrSelfPermission(android.Manifest.permission.DUMP)
7451 != PackageManager.PERMISSION_GRANTED) {
dcashman22b950d2016-06-27 11:39:02 -07007452 writer.println("Permission Denial: can't dump Phone from pid="
7453 + Binder.getCallingPid()
7454 + ", uid=" + Binder.getCallingUid()
7455 + "without permission "
7456 + android.Manifest.permission.DUMP);
7457 return;
7458 }
Nazanin Bakhshif782e562018-12-11 15:15:39 -08007459 DumpsysHandler.dump(mApp, fd, writer, args);
Ta-wei Yenc236d6b2016-06-21 13:33:12 -07007460 }
Jack Yueb89b242016-06-22 13:27:47 -07007461
Brad Ebingerdac2f002018-04-03 15:17:52 -07007462 @Override
Hall Liua1548bd2019-12-24 14:14:12 -08007463 public int handleShellCommand(@NonNull ParcelFileDescriptor in,
7464 @NonNull ParcelFileDescriptor out, @NonNull ParcelFileDescriptor err,
7465 @NonNull String[] args) {
7466 return new TelephonyShellCommand(this, getDefaultPhone().getContext()).exec(
7467 this, in.getFileDescriptor(), out.getFileDescriptor(),
7468 err.getFileDescriptor(), args);
Brad Ebingerdac2f002018-04-03 15:17:52 -07007469 }
7470
Jack Yueb89b242016-06-22 13:27:47 -07007471 /**
Sooraj Sasindranaf1b5132020-05-05 21:06:20 +00007472 * Policy control of data connection with reason {@@TelephonyManager.DataEnabledReason}
Greg Kaiser17f41752020-05-05 16:47:47 +00007473 * @param subId Subscription index
Sooraj Sasindranaf1b5132020-05-05 21:06:20 +00007474 * @param reason the reason the data enable change is taking place
7475 * @param enabled True if enabling the data, otherwise disabling.
7476 * @hide
Jack Yu75ab2952016-07-08 14:29:33 -07007477 */
7478 @Override
Sooraj Sasindran95c07a92020-07-15 01:35:24 -07007479 public void setDataEnabledForReason(int subId, @TelephonyManager.DataEnabledReason int reason,
Sooraj Sasindranaf1b5132020-05-05 21:06:20 +00007480 boolean enabled) {
7481 if (reason == TelephonyManager.DATA_ENABLED_REASON_USER
7482 || reason == TelephonyManager.DATA_ENABLED_REASON_CARRIER) {
7483 try {
7484 TelephonyPermissions.enforceCallingOrSelfCarrierPrivilege(
Sooraj Sasindran95c07a92020-07-15 01:35:24 -07007485 mApp, subId, "setDataEnabledForReason");
Sooraj Sasindranaf1b5132020-05-05 21:06:20 +00007486 } catch (SecurityException se) {
7487 enforceModifyPermission();
7488 }
7489 } else {
7490 enforceModifyPermission();
7491 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007492
7493 final long identity = Binder.clearCallingIdentity();
7494 try {
7495 Phone phone = getPhone(subId);
7496 if (phone != null) {
Sooraj Sasindranaf1b5132020-05-05 21:06:20 +00007497 if (reason == TelephonyManager.DATA_ENABLED_REASON_CARRIER) {
7498 phone.carrierActionSetMeteredApnsEnabled(enabled);
7499 } else {
7500 phone.getDataEnabledSettings().setDataEnabled(reason, enabled);
7501 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007502 }
7503 } finally {
7504 Binder.restoreCallingIdentity(identity);
Jack Yu75ab2952016-07-08 14:29:33 -07007505 }
7506 }
Sooraj Sasindran9a909312016-07-18 11:57:25 -07007507
7508 /**
7509 * Get Client request stats
7510 * @return List of Client Request Stats
7511 * @hide
7512 */
7513 @Override
Philip P. Moltmann700a9592019-10-03 11:53:50 -07007514 public List<ClientRequestStats> getClientRequestStats(String callingPackage,
7515 String callingFeatureId, int subId) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08007516 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
Philip P. Moltmann700a9592019-10-03 11:53:50 -07007517 mApp, subId, callingPackage, callingFeatureId, "getClientRequestStats")) {
Sooraj Sasindran9a909312016-07-18 11:57:25 -07007518 return null;
7519 }
Sooraj Sasindran9a909312016-07-18 11:57:25 -07007520 Phone phone = getPhone(subId);
Sooraj Sasindran9a909312016-07-18 11:57:25 -07007521
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007522 final long identity = Binder.clearCallingIdentity();
7523 try {
7524 if (phone != null) {
7525 return phone.getClientRequestStats();
7526 }
7527
7528 return null;
7529 } finally {
7530 Binder.restoreCallingIdentity(identity);
7531 }
Sooraj Sasindran9a909312016-07-18 11:57:25 -07007532 }
7533
Narayan Kamathf04b5a12018-01-09 11:47:15 +00007534 private WorkSource getWorkSource(int uid) {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08007535 String packageName = mApp.getPackageManager().getNameForUid(uid);
Narayan Kamathf04b5a12018-01-09 11:47:15 +00007536 return new WorkSource(uid, packageName);
Sooraj Sasindran9a909312016-07-18 11:57:25 -07007537 }
Jack Yueb4124c2017-02-16 15:32:43 -08007538
7539 /**
Grace Chen70990072017-03-24 17:21:30 -07007540 * Set SIM card power state.
Jack Yueb4124c2017-02-16 15:32:43 -08007541 *
Sanket Padawe13bac7b2017-03-20 15:04:47 -07007542 * @param slotIndex SIM slot id.
Grace Chen70990072017-03-24 17:21:30 -07007543 * @param state State of SIM (power down, power up, pass through)
7544 * - {@link android.telephony.TelephonyManager#CARD_POWER_DOWN}
7545 * - {@link android.telephony.TelephonyManager#CARD_POWER_UP}
7546 * - {@link android.telephony.TelephonyManager#CARD_POWER_UP_PASS_THROUGH}
Jack Yueb4124c2017-02-16 15:32:43 -08007547 *
7548 **/
7549 @Override
Grace Chen70990072017-03-24 17:21:30 -07007550 public void setSimPowerStateForSlot(int slotIndex, int state) {
Jack Yueb4124c2017-02-16 15:32:43 -08007551 enforceModifyPermission();
Sanket Padawe13bac7b2017-03-20 15:04:47 -07007552 Phone phone = PhoneFactory.getPhone(slotIndex);
7553
vagdeviaf9a5b92018-08-15 16:01:53 -07007554 WorkSource workSource = getWorkSource(Binder.getCallingUid());
7555
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007556 final long identity = Binder.clearCallingIdentity();
7557 try {
7558 if (phone != null) {
vagdeviaf9a5b92018-08-15 16:01:53 -07007559 phone.setSimPowerState(state, workSource);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007560 }
7561 } finally {
7562 Binder.restoreCallingIdentity(identity);
Jack Yueb4124c2017-02-16 15:32:43 -08007563 }
7564 }
Shuo Qiandd210312017-04-12 22:11:33 +00007565
Tyler Gunn65d45c22017-06-05 11:22:26 -07007566 private boolean isUssdApiAllowed(int subId) {
7567 CarrierConfigManager configManager =
Nazanin Bakhshif782e562018-12-11 15:15:39 -08007568 (CarrierConfigManager) mApp.getSystemService(Context.CARRIER_CONFIG_SERVICE);
Tyler Gunn65d45c22017-06-05 11:22:26 -07007569 if (configManager == null) {
7570 return false;
7571 }
7572 PersistableBundle pb = configManager.getConfigForSubId(subId);
7573 if (pb == null) {
7574 return false;
7575 }
7576 return pb.getBoolean(
7577 CarrierConfigManager.KEY_ALLOW_USSD_REQUESTS_VIA_TELEPHONY_MANAGER_BOOL);
7578 }
7579
Shuo Qiandd210312017-04-12 22:11:33 +00007580 /**
7581 * Check if phone is in emergency callback mode
7582 * @return true if phone is in emergency callback mode
7583 * @param subId sub id
7584 */
goneil9c5f4872017-12-05 14:07:56 -08007585 @Override
Shuo Qiandd210312017-04-12 22:11:33 +00007586 public boolean getEmergencyCallbackMode(int subId) {
Brad Ebinger35c841c2018-10-01 10:40:55 -07007587 enforceReadPrivilegedPermission("getEmergencyCallbackMode");
Shuo Qiandd210312017-04-12 22:11:33 +00007588 final Phone phone = getPhone(subId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007589
7590 final long identity = Binder.clearCallingIdentity();
7591 try {
7592 if (phone != null) {
7593 return phone.isInEcm();
7594 } else {
7595 return false;
7596 }
7597 } finally {
7598 Binder.restoreCallingIdentity(identity);
Shuo Qiandd210312017-04-12 22:11:33 +00007599 }
7600 }
Nathan Harold46b42aa2017-03-10 19:38:22 -08007601
7602 /**
7603 * Get the current signal strength information for the given subscription.
7604 * Because this information is not updated when the device is in a low power state
7605 * it should not be relied-upon to be current.
7606 * @param subId Subscription index
7607 * @return the most recent cached signal strength info from the modem
7608 */
7609 @Override
7610 public SignalStrength getSignalStrength(int subId) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007611 final long identity = Binder.clearCallingIdentity();
7612 try {
7613 Phone p = getPhone(subId);
7614 if (p == null) {
7615 return null;
7616 }
Nathan Harold46b42aa2017-03-10 19:38:22 -08007617
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007618 return p.getSignalStrength();
7619 } finally {
7620 Binder.restoreCallingIdentity(identity);
7621 }
Nathan Harold46b42aa2017-03-10 19:38:22 -08007622 }
Holly Jiuyu Sun01c47ad2018-01-24 17:56:33 +00007623
Pengquan Meng77b7f132018-08-22 14:49:57 -07007624 /**
Chen Xuf792fd62018-10-17 17:54:36 +00007625 * Get the current modem radio state for the given slot.
7626 * @param slotIndex slot index.
7627 * @param callingPackage the name of the package making the call.
Philip P. Moltmann700a9592019-10-03 11:53:50 -07007628 * @param callingFeatureId The feature in the package.
Chen Xuf792fd62018-10-17 17:54:36 +00007629 * @return the current radio power state from the modem
7630 */
7631 @Override
Philip P. Moltmann700a9592019-10-03 11:53:50 -07007632 public int getRadioPowerState(int slotIndex, String callingPackage, String callingFeatureId) {
Chen Xuf792fd62018-10-17 17:54:36 +00007633 Phone phone = PhoneFactory.getPhone(slotIndex);
7634 if (phone != null) {
Philip P. Moltmann700a9592019-10-03 11:53:50 -07007635 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(mApp, phone.getSubId(),
7636 callingPackage, callingFeatureId, "getRadioPowerState")) {
Chen Xuf792fd62018-10-17 17:54:36 +00007637 return TelephonyManager.RADIO_POWER_UNAVAILABLE;
7638 }
7639
7640 final long identity = Binder.clearCallingIdentity();
7641 try {
7642 return phone.getRadioPowerState();
7643 } finally {
7644 Binder.restoreCallingIdentity(identity);
7645 }
7646 }
7647 return TelephonyManager.RADIO_POWER_UNAVAILABLE;
7648 }
7649
7650 /**
Pengquan Meng77b7f132018-08-22 14:49:57 -07007651 * Checks if data roaming is enabled on the subscription with id {@code subId}.
7652 *
7653 * <p>Requires one of the following permissions:
7654 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE},
7655 * {@link android.Manifest.permission#READ_PHONE_STATE} or that the calling app has carrier
7656 * privileges.
7657 *
7658 * @param subId subscription id
7659 * @return {@code true} if data roaming is enabled on this subscription, otherwise return
7660 * {@code false}.
7661 */
7662 @Override
7663 public boolean isDataRoamingEnabled(int subId) {
Shuo Qian093013d2020-08-13 15:42:55 -07007664 try {
7665 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.ACCESS_NETWORK_STATE,
7666 null);
7667 } catch (Exception e) {
7668 TelephonyPermissions.enforeceCallingOrSelfReadPhoneStatePermissionOrCarrierPrivilege(
7669 mApp, subId, "isDataRoamingEnabled");
7670 }
Pengquan Meng44e66f12019-04-01 10:48:20 -07007671
Pengquan Menga1bb6272018-09-06 09:59:22 -07007672 boolean isEnabled = false;
7673 final long identity = Binder.clearCallingIdentity();
Pengquan Meng77b7f132018-08-22 14:49:57 -07007674 try {
Pengquan Menga1bb6272018-09-06 09:59:22 -07007675 Phone phone = getPhone(subId);
7676 isEnabled = phone != null ? phone.getDataRoamingEnabled() : false;
Pengquan Menga1bb6272018-09-06 09:59:22 -07007677 } finally {
7678 Binder.restoreCallingIdentity(identity);
Pengquan Meng77b7f132018-08-22 14:49:57 -07007679 }
Pengquan Menga1bb6272018-09-06 09:59:22 -07007680 return isEnabled;
Pengquan Meng77b7f132018-08-22 14:49:57 -07007681 }
7682
7683
7684 /**
7685 * Enables/Disables the data roaming on the subscription with id {@code subId}.
7686 *
7687 * <p> Requires permission:
7688 * {@link android.Manifest.permission#MODIFY_PHONE_STATE} or that the calling app has carrier
7689 * privileges.
7690 *
7691 * @param subId subscription id
7692 * @param isEnabled {@code true} means enable, {@code false} means disable.
7693 */
7694 @Override
7695 public void setDataRoamingEnabled(int subId, boolean isEnabled) {
Pengquan Meng44e66f12019-04-01 10:48:20 -07007696 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
7697 mApp, subId, "setDataRoamingEnabled");
7698
Pengquan Menga1bb6272018-09-06 09:59:22 -07007699 final long identity = Binder.clearCallingIdentity();
7700 try {
Pengquan Menga1bb6272018-09-06 09:59:22 -07007701 Phone phone = getPhone(subId);
7702 if (phone != null) {
7703 phone.setDataRoamingEnabled(isEnabled);
7704 }
7705 } finally {
7706 Binder.restoreCallingIdentity(identity);
Pengquan Meng77b7f132018-08-22 14:49:57 -07007707 }
7708 }
7709
Holly Jiuyu Sun01c47ad2018-01-24 17:56:33 +00007710 @Override
Pengquan Meng6884a2c2018-10-03 12:19:13 -07007711 public boolean isManualNetworkSelectionAllowed(int subId) {
tom hsuc91afc72020-01-06 23:46:07 +08007712 TelephonyPermissions
7713 .enforeceCallingOrSelfReadPrivilegedPhoneStatePermissionOrCarrierPrivilege(
Pengquan Meng44e66f12019-04-01 10:48:20 -07007714 mApp, subId, "isManualNetworkSelectionAllowed");
7715
Pengquan Meng6884a2c2018-10-03 12:19:13 -07007716 boolean isAllowed = true;
7717 final long identity = Binder.clearCallingIdentity();
7718 try {
Pengquan Meng6884a2c2018-10-03 12:19:13 -07007719 Phone phone = getPhone(subId);
7720 if (phone != null) {
7721 isAllowed = phone.isCspPlmnEnabled();
7722 }
7723 } finally {
7724 Binder.restoreCallingIdentity(identity);
7725 }
7726 return isAllowed;
7727 }
7728
7729 @Override
Jordan Liu75f43ea2019-01-17 16:56:37 -08007730 public List<UiccCardInfo> getUiccCardsInfo(String callingPackage) {
Jordan Liu4cda4552020-03-23 11:55:07 -07007731 // Verify that tha callingPackage belongs to the calling UID
7732 mApp.getSystemService(AppOpsManager.class)
7733 .checkPackage(Binder.getCallingUid(), callingPackage);
7734
Jordan Liu1e142fc2019-04-22 15:10:43 -07007735 boolean hasReadPermission = false;
Jordan Liuc65bc952019-02-12 17:54:02 -08007736 try {
7737 enforceReadPrivilegedPermission("getUiccCardsInfo");
Jordan Liu1e142fc2019-04-22 15:10:43 -07007738 hasReadPermission = true;
Jordan Liuc65bc952019-02-12 17:54:02 -08007739 } catch (SecurityException e) {
7740 // even without READ_PRIVILEGED_PHONE_STATE, we allow the call to continue if the caller
7741 // has carrier privileges on an active UICC
7742 if (checkCarrierPrivilegesForPackageAnyPhone(callingPackage)
7743 != TelephonyManager.CARRIER_PRIVILEGE_STATUS_HAS_ACCESS) {
Jordan Liu1e142fc2019-04-22 15:10:43 -07007744 throw new SecurityException("Caller does not have permission.");
Jordan Liuc65bc952019-02-12 17:54:02 -08007745 }
Jordan Liu75f43ea2019-01-17 16:56:37 -08007746 }
Jordan Liu5aa07002018-12-18 15:44:48 -08007747
7748 final long identity = Binder.clearCallingIdentity();
7749 try {
Jordan Liu75f43ea2019-01-17 16:56:37 -08007750 UiccController uiccController = UiccController.getInstance();
7751 ArrayList<UiccCardInfo> cardInfos = uiccController.getAllUiccCardInfos();
Jordan Liu1e142fc2019-04-22 15:10:43 -07007752 if (hasReadPermission) {
7753 return cardInfos;
Jordan Liu75f43ea2019-01-17 16:56:37 -08007754 }
Jordan Liu1e142fc2019-04-22 15:10:43 -07007755
7756 // Remove private info if the caller doesn't have access
7757 ArrayList<UiccCardInfo> filteredInfos = new ArrayList<>();
7758 for (UiccCardInfo cardInfo : cardInfos) {
7759 // For an inactive eUICC, the UiccCard will be null even though the UiccCardInfo
7760 // is available
7761 UiccCard card = uiccController.getUiccCardForSlot(cardInfo.getSlotIndex());
7762 if (card == null || card.getUiccProfile() == null) {
7763 // assume no access if the card or profile is unavailable
7764 filteredInfos.add(cardInfo.getUnprivileged());
7765 continue;
7766 }
7767 UiccProfile profile = card.getUiccProfile();
7768 if (profile.getCarrierPrivilegeStatus(mApp.getPackageManager(), callingPackage)
7769 == TelephonyManager.CARRIER_PRIVILEGE_STATUS_HAS_ACCESS) {
7770 filteredInfos.add(cardInfo);
7771 } else {
7772 filteredInfos.add(cardInfo.getUnprivileged());
7773 }
7774 }
7775 return filteredInfos;
Jordan Liu5aa07002018-12-18 15:44:48 -08007776 } finally {
7777 Binder.restoreCallingIdentity(identity);
7778 }
7779 }
7780
7781 @Override
Holly Jiuyu Sun01c47ad2018-01-24 17:56:33 +00007782 public UiccSlotInfo[] getUiccSlotsInfo() {
Brad Ebinger35c841c2018-10-01 10:40:55 -07007783 enforceReadPrivilegedPermission("getUiccSlotsInfo");
Holly Jiuyu Sun01c47ad2018-01-24 17:56:33 +00007784
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007785 final long identity = Binder.clearCallingIdentity();
7786 try {
7787 UiccSlot[] slots = UiccController.getInstance().getUiccSlots();
7788 if (slots == null) {
7789 Rlog.i(LOG_TAG, "slots is null.");
7790 return null;
7791 }
7792
7793 UiccSlotInfo[] infos = new UiccSlotInfo[slots.length];
7794 for (int i = 0; i < slots.length; i++) {
7795 UiccSlot slot = slots[i];
7796 if (slot == null) {
7797 continue;
7798 }
7799
Jordan Liu7be7e652019-05-06 18:55:02 +00007800 String cardId;
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007801 UiccCard card = slot.getUiccCard();
7802 if (card != null) {
7803 cardId = card.getCardId();
Jordan Liu7be7e652019-05-06 18:55:02 +00007804 } else {
Jordan Liu01bd00d2019-09-12 16:19:43 -07007805 cardId = slot.getEid();
7806 if (TextUtils.isEmpty(cardId)) {
7807 cardId = slot.getIccId();
7808 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007809 }
7810
Jordan Liu857451f2019-05-09 16:35:35 -07007811 if (cardId != null) {
7812 // if cardId is an ICCID, strip off trailing Fs before exposing to user
7813 // if cardId is an EID, it's all digits so this is fine
7814 cardId = IccUtils.stripTrailingFs(cardId);
7815 }
7816
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007817 int cardState = 0;
7818 switch (slot.getCardState()) {
7819 case CARDSTATE_ABSENT:
7820 cardState = UiccSlotInfo.CARD_STATE_INFO_ABSENT;
7821 break;
7822 case CARDSTATE_PRESENT:
7823 cardState = UiccSlotInfo.CARD_STATE_INFO_PRESENT;
7824 break;
7825 case CARDSTATE_ERROR:
7826 cardState = UiccSlotInfo.CARD_STATE_INFO_ERROR;
7827 break;
7828 case CARDSTATE_RESTRICTED:
7829 cardState = UiccSlotInfo.CARD_STATE_INFO_RESTRICTED;
7830 break;
7831 default:
7832 break;
7833
7834 }
7835
7836 infos[i] = new UiccSlotInfo(
7837 slot.isActive(),
7838 slot.isEuicc(),
7839 cardId,
7840 cardState,
7841 slot.getPhoneId(),
Jordan Liua2619582019-02-14 12:56:40 -08007842 slot.isExtendedApduSupported(),
7843 slot.isRemovable());
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007844 }
7845 return infos;
7846 } finally {
7847 Binder.restoreCallingIdentity(identity);
Holly Jiuyu Sun1d957c52018-04-04 13:52:42 -07007848 }
Holly Jiuyu Sun01c47ad2018-01-24 17:56:33 +00007849 }
7850
7851 @Override
7852 public boolean switchSlots(int[] physicalSlots) {
7853 enforceModifyPermission();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007854
7855 final long identity = Binder.clearCallingIdentity();
7856 try {
7857 return (Boolean) sendRequest(CMD_SWITCH_SLOTS, physicalSlots);
7858 } finally {
7859 Binder.restoreCallingIdentity(identity);
7860 }
Holly Jiuyu Sun01c47ad2018-01-24 17:56:33 +00007861 }
Jack Yu4c988042018-02-27 15:30:01 -08007862
7863 @Override
Jordan Liu7de49fa2018-12-06 14:48:49 -08007864 public int getCardIdForDefaultEuicc(int subId, String callingPackage) {
Jordan Liu7de49fa2018-12-06 14:48:49 -08007865 final long identity = Binder.clearCallingIdentity();
7866 try {
7867 return UiccController.getInstance().getCardIdForDefaultEuicc();
7868 } finally {
7869 Binder.restoreCallingIdentity(identity);
7870 }
7871 }
7872
Pengquan Meng85728fb2018-03-12 16:31:21 -07007873 /**
goneil47ffb6e2018-04-06 15:40:58 -07007874 * A test API to reload the UICC profile.
7875 *
7876 * <p>Requires that the calling app has permission
7877 * {@link android.Manifest.permission#MODIFY_PHONE_STATE MODIFY_PHONE_STATE}.
7878 * @hide
7879 */
7880 @Override
7881 public void refreshUiccProfile(int subId) {
7882 enforceModifyPermission();
7883
7884 final long identity = Binder.clearCallingIdentity();
7885 try {
7886 Phone phone = getPhone(subId);
7887 if (phone == null) {
7888 return;
7889 }
7890 UiccCard uiccCard = phone.getUiccCard();
7891 if (uiccCard == null) {
7892 return;
7893 }
7894 UiccProfile uiccProfile = uiccCard.getUiccProfile();
7895 if (uiccProfile == null) {
7896 return;
7897 }
7898 uiccProfile.refresh();
7899 } finally {
7900 Binder.restoreCallingIdentity(identity);
7901 }
7902 }
7903
7904 /**
Pengquan Meng85728fb2018-03-12 16:31:21 -07007905 * Returns false if the mobile data is disabled by default, otherwise return true.
7906 */
7907 private boolean getDefaultDataEnabled() {
Inseob Kim14bb3d02018-12-13 17:11:34 +09007908 return TelephonyProperties.mobile_data().orElse(true);
Pengquan Meng85728fb2018-03-12 16:31:21 -07007909 }
7910
7911 /**
7912 * Returns true if the data roaming is enabled by default, i.e the system property
7913 * of {@link #DEFAULT_DATA_ROAMING_PROPERTY_NAME} is true or the config of
7914 * {@link CarrierConfigManager#KEY_CARRIER_DEFAULT_DATA_ROAMING_ENABLED_BOOL} is true.
7915 */
7916 private boolean getDefaultDataRoamingEnabled(int subId) {
7917 final CarrierConfigManager configMgr = (CarrierConfigManager)
Nazanin Bakhshif782e562018-12-11 15:15:39 -08007918 mApp.getSystemService(Context.CARRIER_CONFIG_SERVICE);
Shuo Qian1d84a0e2020-07-15 12:36:44 -07007919 boolean isDataRoamingEnabled = TelephonyProperties.data_roaming().orElse(false);
Pengquan Meng85728fb2018-03-12 16:31:21 -07007920 isDataRoamingEnabled |= configMgr.getConfigForSubId(subId).getBoolean(
7921 CarrierConfigManager.KEY_CARRIER_DEFAULT_DATA_ROAMING_ENABLED_BOOL);
7922 return isDataRoamingEnabled;
7923 }
7924
7925 /**
7926 * Returns the default network type for the given {@code subId}, if the default network type is
7927 * not set, return {@link Phone#PREFERRED_NT_MODE}.
7928 */
7929 private int getDefaultNetworkType(int subId) {
Inseob Kim14bb3d02018-12-13 17:11:34 +09007930 List<Integer> list = TelephonyProperties.default_network();
7931 int phoneId = mSubscriptionController.getPhoneId(subId);
7932 if (phoneId >= 0 && phoneId < list.size() && list.get(phoneId) != null) {
7933 return list.get(phoneId);
7934 }
7935 return Phone.PREFERRED_NT_MODE;
Pengquan Meng85728fb2018-03-12 16:31:21 -07007936 }
fionaxua13278b2018-03-21 00:08:13 -07007937
7938 @Override
7939 public void setCarrierTestOverride(int subId, String mccmnc, String imsi, String iccid, String
chen xueaba88a2019-03-15 13:15:10 -07007940 gid1, String gid2, String plmn, String spn, String carrierPrivilegeRules, String apn) {
fionaxua13278b2018-03-21 00:08:13 -07007941 enforceModifyPermission();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007942
7943 final long identity = Binder.clearCallingIdentity();
7944 try {
7945 final Phone phone = getPhone(subId);
7946 if (phone == null) {
7947 loge("setCarrierTestOverride fails with invalid subId: " + subId);
7948 return;
7949 }
chen xueaba88a2019-03-15 13:15:10 -07007950 phone.setCarrierTestOverride(mccmnc, imsi, iccid, gid1, gid2, plmn, spn,
7951 carrierPrivilegeRules, apn);
Jeff Davidson8ab02b22020-03-28 12:24:40 -07007952 if (carrierPrivilegeRules == null) {
7953 mCarrierPrivilegeTestOverrideSubIds.remove(subId);
7954 } else {
7955 mCarrierPrivilegeTestOverrideSubIds.add(subId);
7956 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007957 } finally {
7958 Binder.restoreCallingIdentity(identity);
fionaxua13278b2018-03-21 00:08:13 -07007959 }
fionaxua13278b2018-03-21 00:08:13 -07007960 }
7961
7962 @Override
7963 public int getCarrierIdListVersion(int subId) {
Brad Ebinger35c841c2018-10-01 10:40:55 -07007964 enforceReadPrivilegedPermission("getCarrierIdListVersion");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007965
7966 final long identity = Binder.clearCallingIdentity();
7967 try {
7968 final Phone phone = getPhone(subId);
7969 if (phone == null) {
7970 loge("getCarrierIdListVersion fails with invalid subId: " + subId);
7971 return TelephonyManager.UNKNOWN_CARRIER_ID_LIST_VERSION;
7972 }
7973 return phone.getCarrierIdListVersion();
7974 } finally {
7975 Binder.restoreCallingIdentity(identity);
fionaxua13278b2018-03-21 00:08:13 -07007976 }
fionaxua13278b2018-03-21 00:08:13 -07007977 }
Malcolm Chen2c63d402018-08-14 16:00:53 -07007978
7979 @Override
Philip P. Moltmann700a9592019-10-03 11:53:50 -07007980 public int getNumberOfModemsWithSimultaneousDataConnections(int subId, String callingPackage,
7981 String callingFeatureId) {
Malcolm Chen2c63d402018-08-14 16:00:53 -07007982 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
Philip P. Moltmann700a9592019-10-03 11:53:50 -07007983 mApp, subId, callingPackage, callingFeatureId,
7984 "getNumberOfModemsWithSimultaneousDataConnections")) {
Malcolm Chen2c63d402018-08-14 16:00:53 -07007985 return -1;
7986 }
7987
7988 final long identity = Binder.clearCallingIdentity();
7989 try {
7990 return mPhoneConfigurationManager.getNumberOfModemsWithSimultaneousDataConnections();
7991 } finally {
7992 Binder.restoreCallingIdentity(identity);
7993 }
7994 }
Pengquan Menga1bb6272018-09-06 09:59:22 -07007995
7996 @Override
7997 public int getCdmaRoamingMode(int subId) {
zoey chen7e6d4e52019-12-17 18:18:59 +08007998 TelephonyPermissions
7999 .enforeceCallingOrSelfReadPrivilegedPhoneStatePermissionOrCarrierPrivilege(
Pengquan Menga1bb6272018-09-06 09:59:22 -07008000 mApp, subId, "getCdmaRoamingMode");
8001
8002 final long identity = Binder.clearCallingIdentity();
8003 try {
8004 return (int) sendRequest(CMD_GET_CDMA_ROAMING_MODE, null /* argument */, subId);
8005 } finally {
8006 Binder.restoreCallingIdentity(identity);
8007 }
8008 }
8009
8010 @Override
8011 public boolean setCdmaRoamingMode(int subId, int mode) {
8012 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
8013 mApp, subId, "setCdmaRoamingMode");
8014
8015 final long identity = Binder.clearCallingIdentity();
8016 try {
8017 return (boolean) sendRequest(CMD_SET_CDMA_ROAMING_MODE, mode, subId);
8018 } finally {
8019 Binder.restoreCallingIdentity(identity);
8020 }
8021 }
8022
8023 @Override
8024 public boolean setCdmaSubscriptionMode(int subId, int mode) {
8025 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
8026 mApp, subId, "setCdmaSubscriptionMode");
8027
8028 final long identity = Binder.clearCallingIdentity();
8029 try {
8030 return (boolean) sendRequest(CMD_SET_CDMA_SUBSCRIPTION_MODE, mode, subId);
8031 } finally {
8032 Binder.restoreCallingIdentity(identity);
8033 }
8034 }
Makoto Onukida3bf792018-09-18 16:06:29 -07008035
sqianc5eccab2018-10-19 18:46:41 -07008036 @Override
sqian8c685422019-02-22 15:55:18 -08008037 public Map<Integer, List<EmergencyNumber>> getEmergencyNumberList(
Philip P. Moltmann700a9592019-10-03 11:53:50 -07008038 String callingPackage, String callingFeatureId) {
sqian11b7a0e2018-12-05 18:48:28 -08008039 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
Philip P. Moltmann700a9592019-10-03 11:53:50 -07008040 mApp, getDefaultSubscription(), callingPackage, callingFeatureId,
8041 "getEmergencyNumberList")) {
sqian11b7a0e2018-12-05 18:48:28 -08008042 throw new SecurityException("Requires READ_PHONE_STATE permission.");
8043 }
8044 final long identity = Binder.clearCallingIdentity();
8045 try {
sqian854d44b2018-12-12 16:48:18 -08008046 Map<Integer, List<EmergencyNumber>> emergencyNumberListInternal = new HashMap<>();
8047 for (Phone phone: PhoneFactory.getPhones()) {
8048 if (phone.getEmergencyNumberTracker() != null
8049 && phone.getEmergencyNumberTracker().getEmergencyNumberList() != null) {
8050 emergencyNumberListInternal.put(
8051 phone.getSubId(),
8052 phone.getEmergencyNumberTracker().getEmergencyNumberList());
8053 }
sqian11b7a0e2018-12-05 18:48:28 -08008054 }
sqian854d44b2018-12-12 16:48:18 -08008055 return emergencyNumberListInternal;
sqian11b7a0e2018-12-05 18:48:28 -08008056 } finally {
8057 Binder.restoreCallingIdentity(identity);
8058 }
sqianc5eccab2018-10-19 18:46:41 -07008059 }
8060
8061 @Override
sqian8c685422019-02-22 15:55:18 -08008062 public boolean isEmergencyNumber(String number, boolean exactMatch) {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08008063 final Phone defaultPhone = getDefaultPhone();
sqian11b7a0e2018-12-05 18:48:28 -08008064 if (!exactMatch) {
8065 TelephonyPermissions
8066 .enforeceCallingOrSelfReadPrivilegedPhoneStatePermissionOrCarrierPrivilege(
sqian8c685422019-02-22 15:55:18 -08008067 mApp, defaultPhone.getSubId(), "isEmergencyNumber(Potential)");
sqian11b7a0e2018-12-05 18:48:28 -08008068 }
8069 final long identity = Binder.clearCallingIdentity();
8070 try {
sqian854d44b2018-12-12 16:48:18 -08008071 for (Phone phone: PhoneFactory.getPhones()) {
8072 if (phone.getEmergencyNumberTracker() != null
8073 && phone.getEmergencyNumberTracker() != null) {
8074 if (phone.getEmergencyNumberTracker().isEmergencyNumber(
8075 number, exactMatch)) {
8076 return true;
sqian11b7a0e2018-12-05 18:48:28 -08008077 }
8078 }
sqian11b7a0e2018-12-05 18:48:28 -08008079 }
8080 return false;
8081 } finally {
8082 Binder.restoreCallingIdentity(identity);
8083 }
8084 }
8085
sqianf4ca7ed2019-01-15 18:32:07 -08008086 /**
8087 * Update emergency number list for test mode.
8088 */
8089 @Override
8090 public void updateEmergencyNumberListTestMode(int action, EmergencyNumber num) {
8091 TelephonyPermissions.enforceShellOnly(Binder.getCallingUid(),
8092 "updateEmergencyNumberListTestMode");
8093
8094 final long identity = Binder.clearCallingIdentity();
8095 try {
8096 for (Phone phone: PhoneFactory.getPhones()) {
8097 EmergencyNumberTracker tracker = phone.getEmergencyNumberTracker();
8098 if (tracker != null) {
8099 tracker.executeEmergencyNumberTestModeCommand(action, num);
8100 }
8101 }
8102 } finally {
8103 Binder.restoreCallingIdentity(identity);
8104 }
8105 }
8106
8107 /**
8108 * Get the full emergency number list for test mode.
8109 */
8110 @Override
8111 public List<String> getEmergencyNumberListTestMode() {
8112 TelephonyPermissions.enforceShellOnly(Binder.getCallingUid(),
8113 "getEmergencyNumberListTestMode");
8114
8115 final long identity = Binder.clearCallingIdentity();
8116 try {
8117 Set<String> emergencyNumbers = new HashSet<>();
8118 for (Phone phone: PhoneFactory.getPhones()) {
8119 EmergencyNumberTracker tracker = phone.getEmergencyNumberTracker();
8120 if (tracker != null) {
8121 for (EmergencyNumber num : tracker.getEmergencyNumberList()) {
8122 emergencyNumbers.add(num.getNumber());
8123 }
8124 }
8125 }
8126 return new ArrayList<>(emergencyNumbers);
8127 } finally {
8128 Binder.restoreCallingIdentity(identity);
8129 }
8130 }
8131
chen xud6b45bd2018-10-30 22:27:10 -07008132 @Override
Shuo Qian3b6ee772019-11-13 17:43:31 -08008133 public int getEmergencyNumberDbVersion(int subId) {
8134 enforceReadPrivilegedPermission("getEmergencyNumberDbVersion");
8135
8136 final long identity = Binder.clearCallingIdentity();
8137 try {
8138 final Phone phone = getPhone(subId);
8139 if (phone == null) {
8140 loge("getEmergencyNumberDbVersion fails with invalid subId: " + subId);
8141 return TelephonyManager.INVALID_EMERGENCY_NUMBER_DB_VERSION;
8142 }
8143 return phone.getEmergencyNumberDbVersion();
8144 } finally {
8145 Binder.restoreCallingIdentity(identity);
8146 }
8147 }
8148
8149 @Override
8150 public void notifyOtaEmergencyNumberDbInstalled() {
8151 enforceModifyPermission();
8152
8153 final long identity = Binder.clearCallingIdentity();
8154 try {
8155 for (Phone phone: PhoneFactory.getPhones()) {
8156 EmergencyNumberTracker tracker = phone.getEmergencyNumberTracker();
8157 if (tracker != null) {
8158 tracker.updateOtaEmergencyNumberDatabase();
8159 }
8160 }
8161 } finally {
8162 Binder.restoreCallingIdentity(identity);
8163 }
8164 }
8165
8166 @Override
Shuo Qianc373f112020-03-05 17:55:34 -08008167 public void updateOtaEmergencyNumberDbFilePath(ParcelFileDescriptor otaParcelFileDescriptor) {
Shuo Qian3b6ee772019-11-13 17:43:31 -08008168 enforceActiveEmergencySessionPermission();
8169
8170 final long identity = Binder.clearCallingIdentity();
8171 try {
8172 for (Phone phone: PhoneFactory.getPhones()) {
8173 EmergencyNumberTracker tracker = phone.getEmergencyNumberTracker();
8174 if (tracker != null) {
Shuo Qianc373f112020-03-05 17:55:34 -08008175 tracker.updateOtaEmergencyNumberDbFilePath(otaParcelFileDescriptor);
8176 }
8177 }
8178 } finally {
8179 Binder.restoreCallingIdentity(identity);
8180 }
8181 }
8182
8183 @Override
8184 public void resetOtaEmergencyNumberDbFilePath() {
8185 enforceActiveEmergencySessionPermission();
8186
8187 final long identity = Binder.clearCallingIdentity();
8188 try {
8189 for (Phone phone: PhoneFactory.getPhones()) {
8190 EmergencyNumberTracker tracker = phone.getEmergencyNumberTracker();
8191 if (tracker != null) {
8192 tracker.resetOtaEmergencyNumberDbFilePath();
Shuo Qian3b6ee772019-11-13 17:43:31 -08008193 }
8194 }
8195 } finally {
8196 Binder.restoreCallingIdentity(identity);
8197 }
8198 }
8199
8200 @Override
chen xud6b45bd2018-10-30 22:27:10 -07008201 public List<String> getCertsFromCarrierPrivilegeAccessRules(int subId) {
8202 enforceReadPrivilegedPermission("getCertsFromCarrierPrivilegeAccessRules");
8203 Phone phone = getPhone(subId);
8204 if (phone == null) {
8205 return null;
8206 }
8207 final long identity = Binder.clearCallingIdentity();
8208 try {
8209 UiccProfile profile = UiccController.getInstance()
8210 .getUiccProfileForPhone(phone.getPhoneId());
8211 if (profile != null) {
8212 return profile.getCertsFromCarrierPrivilegeAccessRules();
8213 }
8214 } finally {
8215 Binder.restoreCallingIdentity(identity);
8216 }
8217 return null;
8218 }
Malcolm Chen8e4ed912019-01-15 20:22:16 -08008219
8220 /**
8221 * Enable or disable a modem stack.
8222 */
8223 @Override
8224 public boolean enableModemForSlot(int slotIndex, boolean enable) {
8225 enforceModifyPermission();
8226
8227 final long identity = Binder.clearCallingIdentity();
8228 try {
8229 Phone phone = PhoneFactory.getPhone(slotIndex);
8230 if (phone == null) {
8231 return false;
8232 } else {
8233 return (Boolean) sendRequest(CMD_REQUEST_ENABLE_MODEM, enable, phone, null);
8234 }
8235 } finally {
8236 Binder.restoreCallingIdentity(identity);
8237 }
8238 }
Michelecea4cf22018-12-21 15:00:11 -08008239
Malcolm Chen4bcd9822019-03-27 18:34:05 -07008240 /**
8241 * Whether a modem stack is enabled or not.
8242 */
8243 @Override
Philip P. Moltmann700a9592019-10-03 11:53:50 -07008244 public boolean isModemEnabledForSlot(int slotIndex, String callingPackage,
8245 String callingFeatureId) {
Malcolm Chen4bcd9822019-03-27 18:34:05 -07008246 Phone phone = PhoneFactory.getPhone(slotIndex);
8247 if (phone == null) return false;
8248
8249 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
Philip P. Moltmann700a9592019-10-03 11:53:50 -07008250 mApp, phone.getSubId(), callingPackage, callingFeatureId,
8251 "isModemEnabledForSlot")) {
Malcolm Chen4bcd9822019-03-27 18:34:05 -07008252 throw new SecurityException("Requires READ_PHONE_STATE permission.");
8253 }
8254
8255 final long identity = Binder.clearCallingIdentity();
8256 try {
Nazanin Bakhshif71371d2019-04-29 17:29:44 -07008257 try {
8258 return mPhoneConfigurationManager.getPhoneStatusFromCache(phone.getPhoneId());
8259 } catch (NoSuchElementException ex) {
8260 return (Boolean) sendRequest(CMD_GET_MODEM_STATUS, null, phone, null);
8261 }
Malcolm Chen4bcd9822019-03-27 18:34:05 -07008262 } finally {
8263 Binder.restoreCallingIdentity(identity);
8264 }
8265 }
8266
Michelecea4cf22018-12-21 15:00:11 -08008267 @Override
Michele0ea7d782019-03-19 14:58:42 -07008268 public void setMultiSimCarrierRestriction(boolean isMultiSimCarrierRestricted) {
Michelecea4cf22018-12-21 15:00:11 -08008269 enforceModifyPermission();
8270
8271 final long identity = Binder.clearCallingIdentity();
8272 try {
8273 mTelephonySharedPreferences.edit()
Michele0ea7d782019-03-19 14:58:42 -07008274 .putBoolean(PREF_MULTI_SIM_RESTRICTED, isMultiSimCarrierRestricted)
Michelecea4cf22018-12-21 15:00:11 -08008275 .commit();
8276 } finally {
8277 Binder.restoreCallingIdentity(identity);
8278 }
8279 }
8280
8281 @Override
Michele0ea7d782019-03-19 14:58:42 -07008282 @TelephonyManager.IsMultiSimSupportedResult
Philip P. Moltmann700a9592019-10-03 11:53:50 -07008283 public int isMultiSimSupported(String callingPackage, String callingFeatureId) {
Michele4245e952019-02-04 11:36:23 -08008284 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(mApp,
Philip P. Moltmann700a9592019-10-03 11:53:50 -07008285 getDefaultPhone().getSubId(), callingPackage, callingFeatureId,
8286 "isMultiSimSupported")) {
Michele0ea7d782019-03-19 14:58:42 -07008287 return TelephonyManager.MULTISIM_NOT_SUPPORTED_BY_HARDWARE;
Michele4245e952019-02-04 11:36:23 -08008288 }
Michelecea4cf22018-12-21 15:00:11 -08008289
8290 final long identity = Binder.clearCallingIdentity();
8291 try {
Michele0ea7d782019-03-19 14:58:42 -07008292 return isMultiSimSupportedInternal();
Michelecea4cf22018-12-21 15:00:11 -08008293 } finally {
8294 Binder.restoreCallingIdentity(identity);
8295 }
8296 }
Nazanin Bakhshi628473f2019-01-29 17:37:52 -08008297
Michele0ea7d782019-03-19 14:58:42 -07008298 @TelephonyManager.IsMultiSimSupportedResult
8299 private int isMultiSimSupportedInternal() {
Michele30b57b22019-03-01 12:01:14 -08008300 // If the device has less than 2 SIM cards, indicate that multisim is restricted.
8301 int numPhysicalSlots = UiccController.getInstance().getUiccSlots().length;
8302 if (numPhysicalSlots < 2) {
Michele0ea7d782019-03-19 14:58:42 -07008303 loge("isMultiSimSupportedInternal: requires at least 2 cards");
8304 return TelephonyManager.MULTISIM_NOT_SUPPORTED_BY_HARDWARE;
Michele30b57b22019-03-01 12:01:14 -08008305 }
8306 // Check if the hardware supports multisim functionality. If usage of multisim is not
8307 // supported by the modem, indicate that it is restricted.
8308 PhoneCapability staticCapability =
8309 mPhoneConfigurationManager.getStaticPhoneCapability();
8310 if (staticCapability == null) {
Michele0ea7d782019-03-19 14:58:42 -07008311 loge("isMultiSimSupportedInternal: no static configuration available");
8312 return TelephonyManager.MULTISIM_NOT_SUPPORTED_BY_HARDWARE;
Michele30b57b22019-03-01 12:01:14 -08008313 }
Sarah Chin7caee492020-02-18 20:49:02 +00008314 if (staticCapability.logicalModemList.size() < 2) {
Michele0ea7d782019-03-19 14:58:42 -07008315 loge("isMultiSimSupportedInternal: maximum number of modem is < 2");
8316 return TelephonyManager.MULTISIM_NOT_SUPPORTED_BY_HARDWARE;
Michele30b57b22019-03-01 12:01:14 -08008317 }
8318 // Check if support of multiple SIMs is restricted by carrier
8319 if (mTelephonySharedPreferences.getBoolean(PREF_MULTI_SIM_RESTRICTED, false)) {
Michele0ea7d782019-03-19 14:58:42 -07008320 return TelephonyManager.MULTISIM_NOT_SUPPORTED_BY_CARRIER;
Michele30b57b22019-03-01 12:01:14 -08008321 }
8322
Michele0ea7d782019-03-19 14:58:42 -07008323 return TelephonyManager.MULTISIM_ALLOWED;
Michele30b57b22019-03-01 12:01:14 -08008324 }
8325
Nazanin Bakhshi628473f2019-01-29 17:37:52 -08008326 /**
8327 * Switch configs to enable multi-sim or switch back to single-sim
Nazanin Bakhshi17318782019-03-01 11:56:08 -08008328 * Note: Switch from multi-sim to single-sim is only possible with MODIFY_PHONE_STATE
8329 * permission, but the other way around is possible with either MODIFY_PHONE_STATE
8330 * or carrier privileges
Nazanin Bakhshi628473f2019-01-29 17:37:52 -08008331 * @param numOfSims number of active sims we want to switch to
8332 */
8333 @Override
8334 public void switchMultiSimConfig(int numOfSims) {
Nazanin Bakhshi17318782019-03-01 11:56:08 -08008335 if (numOfSims == 1) {
8336 enforceModifyPermission();
8337 } else {
8338 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
8339 mApp, SubscriptionManager.DEFAULT_SUBSCRIPTION_ID, "switchMultiSimConfig");
8340 }
Nazanin Bakhshi628473f2019-01-29 17:37:52 -08008341 final long identity = Binder.clearCallingIdentity();
Michele30b57b22019-03-01 12:01:14 -08008342
Nazanin Bakhshi628473f2019-01-29 17:37:52 -08008343 try {
Michele30b57b22019-03-01 12:01:14 -08008344 //only proceed if multi-sim is not restricted
Michele0ea7d782019-03-19 14:58:42 -07008345 if (isMultiSimSupportedInternal() != TelephonyManager.MULTISIM_ALLOWED) {
Michele30b57b22019-03-01 12:01:14 -08008346 loge("switchMultiSimConfig not possible. It is restricted or not supported.");
8347 return;
8348 }
Nazanin Bakhshi628473f2019-01-29 17:37:52 -08008349 mPhoneConfigurationManager.switchMultiSimConfig(numOfSims);
8350 } finally {
8351 Binder.restoreCallingIdentity(identity);
8352 }
8353 }
8354
Hyungjun Parkbb07fde2019-01-10 15:28:51 +09008355 @Override
8356 public boolean isApplicationOnUicc(int subId, int appType) {
8357 enforceReadPrivilegedPermission("isApplicationOnUicc");
8358 Phone phone = getPhone(subId);
8359 if (phone == null) {
8360 return false;
8361 }
8362 final long identity = Binder.clearCallingIdentity();
8363 try {
8364 UiccCard uiccCard = phone.getUiccCard();
8365 if (uiccCard == null) {
8366 return false;
8367 }
8368 UiccProfile uiccProfile = uiccCard.getUiccProfile();
8369 if (uiccProfile == null) {
8370 return false;
8371 }
8372 if (TelephonyManager.APPTYPE_SIM <= appType
8373 && appType <= TelephonyManager.APPTYPE_ISIM) {
8374 return uiccProfile.isApplicationOnIcc(AppType.values()[appType]);
8375 }
8376 return false;
8377 } finally {
8378 Binder.restoreCallingIdentity(identity);
8379 }
8380 }
8381
Nazanin Bakhshi628473f2019-01-29 17:37:52 -08008382 /**
chen xub4baa772019-04-03 10:23:41 -07008383 * Get whether making changes to modem configurations will trigger reboot.
8384 * Return value defaults to true.
Nazanin Bakhshi5fe5ef22019-01-30 10:52:09 -08008385 */
8386 @Override
Philip P. Moltmann700a9592019-10-03 11:53:50 -07008387 public boolean doesSwitchMultiSimConfigTriggerReboot(int subId, String callingPackage,
8388 String callingFeatureId) {
chen xub4baa772019-04-03 10:23:41 -07008389 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
Philip P. Moltmann700a9592019-10-03 11:53:50 -07008390 mApp, subId, callingPackage, callingFeatureId,
8391 "doesSwitchMultiSimConfigTriggerReboot")) {
chen xub4baa772019-04-03 10:23:41 -07008392 return false;
8393 }
Nazanin Bakhshi5fe5ef22019-01-30 10:52:09 -08008394 final long identity = Binder.clearCallingIdentity();
8395 try {
8396 return mPhoneConfigurationManager.isRebootRequiredForModemConfigChange();
8397 } finally {
8398 Binder.restoreCallingIdentity(identity);
8399 }
8400 }
8401
Nathan Harold29f5f052019-02-15 13:41:57 -08008402 private void updateModemStateMetrics() {
8403 TelephonyMetrics metrics = TelephonyMetrics.getInstance();
8404 // TODO: check the state for each modem if the api is ready.
8405 metrics.updateEnabledModemBitmap((1 << TelephonyManager.from(mApp).getPhoneCount()) - 1);
8406 }
8407
Pengquan Meng3889a572019-01-23 11:16:29 -08008408 @Override
8409 public int[] getSlotsMapping() {
8410 enforceReadPrivilegedPermission("getSlotsMapping");
8411
8412 final long identity = Binder.clearCallingIdentity();
8413 try {
8414 int phoneCount = TelephonyManager.getDefault().getPhoneCount();
8415 // All logical slots should have a mapping to a physical slot.
8416 int[] logicalSlotsMapping = new int[phoneCount];
8417 UiccSlotInfo[] slotInfos = getUiccSlotsInfo();
8418 for (int i = 0; i < slotInfos.length; i++) {
8419 if (SubscriptionManager.isValidPhoneId(slotInfos[i].getLogicalSlotIdx())) {
8420 logicalSlotsMapping[slotInfos[i].getLogicalSlotIdx()] = i;
8421 }
8422 }
8423 return logicalSlotsMapping;
8424 } finally {
8425 Binder.restoreCallingIdentity(identity);
8426 }
8427 }
Nathan Harold48d6fd52019-02-06 19:01:40 -08008428
8429 /**
8430 * Get the IRadio HAL Version
8431 */
8432 @Override
8433 public int getRadioHalVersion() {
8434 Phone phone = getDefaultPhone();
8435 if (phone == null) return -1;
8436 HalVersion hv = phone.getHalVersion();
8437 if (hv.equals(HalVersion.UNKNOWN)) return -1;
8438 return hv.major * 100 + hv.minor;
8439 }
Malcolm Chendc8c10e2019-04-10 18:25:07 -07008440
8441 /**
Shuo Qianda2d6ec2020-01-14 15:18:28 -08008442 * Get the current calling package name.
8443 * @return the current calling package name
8444 */
8445 @Override
8446 public String getCurrentPackageName() {
8447 return mApp.getPackageManager().getPackagesForUid(Binder.getCallingUid())[0];
8448 }
8449
8450 /**
Malcolm Chene5ad5792019-04-18 13:51:02 -07008451 * Return whether data is enabled for certain APN type. This will tell if framework will accept
8452 * corresponding network requests on a subId.
8453 *
8454 * Data is enabled if:
Malcolm Chendc8c10e2019-04-10 18:25:07 -07008455 * 1) user data is turned on, or
Malcolm Chene5ad5792019-04-18 13:51:02 -07008456 * 2) APN is un-metered for this subscription, or
8457 * 3) APN type is whitelisted. E.g. MMS is whitelisted if
changbettyd5c246e2019-12-24 15:40:37 +08008458 * {@link TelephonyManager#setAlwaysAllowMmsData} is turned on.
Malcolm Chene5ad5792019-04-18 13:51:02 -07008459 *
8460 * @return whether data is allowed for a apn type.
8461 *
8462 * @hide
Malcolm Chendc8c10e2019-04-10 18:25:07 -07008463 */
8464 @Override
Malcolm Chene5ad5792019-04-18 13:51:02 -07008465 public boolean isDataEnabledForApn(int apnType, int subId, String callingPackage) {
Amit Mahajan5d4e1922019-10-07 16:20:43 -07008466 enforceReadPrivilegedPermission("Needs READ_PRIVILEGED_PHONE_STATE for "
8467 + "isDataEnabledForApn");
Malcolm Chendc8c10e2019-04-10 18:25:07 -07008468
8469 // Now that all security checks passes, perform the operation as ourselves.
8470 final long identity = Binder.clearCallingIdentity();
8471 try {
8472 Phone phone = getPhone(subId);
8473 if (phone == null) return false;
8474
Jack Yu41407ee2019-05-13 16:54:09 -07008475 boolean isMetered = ApnSettingUtils.isMeteredApnType(apnType, phone);
Malcolm Chene5ad5792019-04-18 13:51:02 -07008476 return !isMetered || phone.getDataEnabledSettings().isDataEnabled(apnType);
8477 } finally {
8478 Binder.restoreCallingIdentity(identity);
8479 }
8480 }
8481
8482 @Override
Jack Yu41407ee2019-05-13 16:54:09 -07008483 public boolean isApnMetered(@ApnType int apnType, int subId) {
Malcolm Chene5ad5792019-04-18 13:51:02 -07008484 enforceReadPrivilegedPermission("isApnMetered");
8485
8486 // Now that all security checks passes, perform the operation as ourselves.
8487 final long identity = Binder.clearCallingIdentity();
8488 try {
8489 Phone phone = getPhone(subId);
8490 if (phone == null) return true; // By default return true.
8491
Jack Yu41407ee2019-05-13 16:54:09 -07008492 return ApnSettingUtils.isMeteredApnType(apnType, phone);
Malcolm Chendc8c10e2019-04-10 18:25:07 -07008493 } finally {
8494 Binder.restoreCallingIdentity(identity);
8495 }
8496 }
Brad Ebingera63db5f2019-04-23 16:31:13 -07008497
8498 @Override
Hall Liu73f5d362020-01-20 13:42:00 -08008499 public void setSystemSelectionChannels(List<RadioAccessSpecifier> specifiers,
8500 int subscriptionId, IBooleanConsumer resultCallback) {
8501 enforceModifyPermission();
8502 long token = Binder.clearCallingIdentity();
8503 try {
8504 Phone phone = getPhone(subscriptionId);
8505 if (phone == null) {
8506 try {
8507 if (resultCallback != null) {
8508 resultCallback.accept(false);
8509 }
8510 } catch (RemoteException e) {
8511 // ignore
8512 }
8513 return;
8514 }
8515 Pair<List<RadioAccessSpecifier>, Consumer<Boolean>> argument =
8516 Pair.create(specifiers, (x) -> {
8517 try {
8518 if (resultCallback != null) {
8519 resultCallback.accept(x);
8520 }
8521 } catch (RemoteException e) {
8522 // ignore
8523 }
8524 });
8525 sendRequestAsync(CMD_SET_SYSTEM_SELECTION_CHANNELS, argument, phone, null);
8526 } finally {
8527 Binder.restoreCallingIdentity(token);
8528 }
8529 }
8530
8531 @Override
changbetty7157e9e2019-12-06 18:16:37 +08008532 public boolean isMvnoMatched(int subId, int mvnoType, @NonNull String mvnoMatchData) {
changbettyca3d40d2020-03-03 16:27:31 +08008533 enforceReadPrivilegedPermission("isMvnoMatched");
changbetty7157e9e2019-12-06 18:16:37 +08008534 IccRecords iccRecords = UiccController.getInstance().getIccRecords(
8535 SubscriptionManager.getPhoneId(subId), UiccController.APP_FAM_3GPP);
8536 if (iccRecords == null) {
8537 Log.d(LOG_TAG, "isMvnoMatched# IccRecords is null");
8538 return false;
8539 }
8540 return ApnSettingUtils.mvnoMatches(iccRecords, mvnoType, mvnoMatchData);
8541 }
8542
8543 @Override
Philip P. Moltmannd02b7372020-03-18 17:06:12 -07008544 public void enqueueSmsPickResult(String callingPackage, String callingAttributionTag,
8545 IIntegerConsumer pendingSubIdResult) {
Shuo Qianda2d6ec2020-01-14 15:18:28 -08008546 if (callingPackage == null) {
8547 callingPackage = getCurrentPackageName();
8548 }
Brad Ebingera63db5f2019-04-23 16:31:13 -07008549 SmsPermissions permissions = new SmsPermissions(getDefaultPhone(), mApp,
8550 (AppOpsManager) mApp.getSystemService(Context.APP_OPS_SERVICE));
Philip P. Moltmannd02b7372020-03-18 17:06:12 -07008551 if (!permissions.checkCallingCanSendSms(callingPackage, callingAttributionTag,
8552 "Sending message")) {
Brad Ebingera63db5f2019-04-23 16:31:13 -07008553 throw new SecurityException("Requires SEND_SMS permission to perform this operation");
8554 }
8555 PickSmsSubscriptionActivity.addPendingResult(pendingSubIdResult);
8556 Intent intent = new Intent();
8557 intent.setClass(mApp, PickSmsSubscriptionActivity.class);
8558 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
8559 // Bring up choose default SMS subscription dialog right now
8560 intent.putExtra(PickSmsSubscriptionActivity.DIALOG_TYPE_KEY,
8561 PickSmsSubscriptionActivity.SMS_PICK_FOR_MESSAGE);
8562 mApp.startActivity(intent);
8563 }
chen xud5ca2d52019-05-28 15:20:57 -07008564
8565 @Override
8566 public String getMmsUAProfUrl(int subId) {
8567 //TODO investigate if this API should require proper permission check in R b/133791609
8568 final long identity = Binder.clearCallingIdentity();
8569 try {
Guoqiang.Qiu171383d2020-07-13 09:38:32 +08008570 String carrierUAProfUrl = mApp.getCarrierConfigForSubId(subId).getString(
8571 CarrierConfigManager.KEY_MMS_UA_PROF_URL_STRING);
8572 if (!TextUtils.isEmpty(carrierUAProfUrl)) {
8573 return carrierUAProfUrl;
8574 }
Daniel Brightebb4eb72020-02-18 15:16:33 -08008575 return SubscriptionManager.getResourcesForSubId(getDefaultPhone().getContext(), subId)
8576 .getString(com.android.internal.R.string.config_mms_user_agent_profile_url);
chen xud5ca2d52019-05-28 15:20:57 -07008577 } finally {
8578 Binder.restoreCallingIdentity(identity);
8579 }
8580 }
8581
8582 @Override
8583 public String getMmsUserAgent(int subId) {
8584 //TODO investigate if this API should require proper permission check in R b/133791609
8585 final long identity = Binder.clearCallingIdentity();
8586 try {
Guoqiang.Qiu171383d2020-07-13 09:38:32 +08008587 String carrierUserAgent = mApp.getCarrierConfigForSubId(subId).getString(
8588 CarrierConfigManager.KEY_MMS_USER_AGENT_STRING);
8589 if (!TextUtils.isEmpty(carrierUserAgent)) {
8590 return carrierUserAgent;
8591 }
Daniel Brightebb4eb72020-02-18 15:16:33 -08008592 return SubscriptionManager.getResourcesForSubId(getDefaultPhone().getContext(), subId)
8593 .getString(com.android.internal.R.string.config_mms_user_agent);
chen xud5ca2d52019-05-28 15:20:57 -07008594 } finally {
8595 Binder.restoreCallingIdentity(identity);
8596 }
8597 }
Jack Yub07d4972019-05-28 16:12:25 -07008598
8599 @Override
8600 public boolean setDataAllowedDuringVoiceCall(int subId, boolean allow) {
8601 enforceModifyPermission();
8602
8603 // Now that all security checks passes, perform the operation as ourselves.
8604 final long identity = Binder.clearCallingIdentity();
8605 try {
8606 Phone phone = getPhone(subId);
8607 if (phone == null) return false;
8608
8609 return phone.getDataEnabledSettings().setAllowDataDuringVoiceCall(allow);
8610 } finally {
8611 Binder.restoreCallingIdentity(identity);
8612 }
8613 }
8614
8615 @Override
8616 public boolean isDataAllowedInVoiceCall(int subId) {
8617 enforceReadPrivilegedPermission("isDataAllowedInVoiceCall");
8618
8619 // Now that all security checks passes, perform the operation as ourselves.
8620 final long identity = Binder.clearCallingIdentity();
8621 try {
8622 Phone phone = getPhone(subId);
8623 if (phone == null) return false;
8624
8625 return phone.getDataEnabledSettings().isDataAllowedInVoiceCall();
8626 } finally {
8627 Binder.restoreCallingIdentity(identity);
8628 }
8629 }
Tyler Gunn7bcdc742019-10-04 15:56:59 -07008630
changbettyd5c246e2019-12-24 15:40:37 +08008631 @Override
8632 public boolean setAlwaysAllowMmsData(int subId, boolean alwaysAllow) {
8633 enforceModifyPermission();
8634
8635 // Now that all security checks passes, perform the operation as ourselves.
8636 final long identity = Binder.clearCallingIdentity();
8637 try {
8638 Phone phone = getPhone(subId);
8639 if (phone == null) return false;
8640
8641 return phone.getDataEnabledSettings().setAlwaysAllowMmsData(alwaysAllow);
8642 } finally {
8643 Binder.restoreCallingIdentity(identity);
8644 }
8645 }
8646
Tyler Gunn7bcdc742019-10-04 15:56:59 -07008647 /**
8648 * Updates whether conference event pacakge handling is enabled.
8649 * @param isCepEnabled {@code true} if CEP handling is enabled (default), or {@code false}
8650 * otherwise.
8651 */
8652 @Override
8653 public void setCepEnabled(boolean isCepEnabled) {
8654 TelephonyPermissions.enforceShellOnly(Binder.getCallingUid(), "setCepEnabled");
8655
8656 final long identity = Binder.clearCallingIdentity();
8657 try {
8658 Rlog.i(LOG_TAG, "setCepEnabled isCepEnabled=" + isCepEnabled);
8659 for (Phone phone : PhoneFactory.getPhones()) {
8660 Phone defaultPhone = phone.getImsPhone();
8661 if (defaultPhone != null && defaultPhone.getPhoneType() == PHONE_TYPE_IMS) {
8662 ImsPhone imsPhone = (ImsPhone) defaultPhone;
8663 ImsPhoneCallTracker imsPhoneCallTracker =
8664 (ImsPhoneCallTracker) imsPhone.getCallTracker();
8665 imsPhoneCallTracker.setConferenceEventPackageEnabled(isCepEnabled);
8666 Rlog.i(LOG_TAG, "setCepEnabled isCepEnabled=" + isCepEnabled + ", for imsPhone "
8667 + imsPhone.getMsisdn());
8668 }
8669 }
8670 } finally {
8671 Binder.restoreCallingIdentity(identity);
8672 }
8673 }
allenwtsu46dcc572020-01-08 18:24:03 +08008674
8675 /**
8676 * Notify that an RCS autoconfiguration XML file has been received for provisioning.
8677 *
8678 * @param config The XML file to be read. ASCII/UTF8 encoded text if not compressed.
8679 * @param isCompressed The XML file is compressed in gzip format and must be decompressed
8680 * before being read.
8681 */
8682 @Override
8683 public void notifyRcsAutoConfigurationReceived(int subId, @NonNull byte[] config, boolean
8684 isCompressed) {
8685 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
8686 mApp, subId, "notifyRcsAutoConfigurationReceived");
8687 try {
8688 IImsConfig configBinder = getImsConfig(getSlotIndex(subId), ImsFeature.FEATURE_RCS);
8689 if (configBinder == null) {
8690 Rlog.e(LOG_TAG, "null result for getImsConfig");
8691 } else {
8692 configBinder.notifyRcsAutoConfigurationReceived(config, isCompressed);
8693 }
8694 } catch (RemoteException e) {
8695 Rlog.e(LOG_TAG, "fail to getImsConfig " + e.getMessage());
8696 }
8697 }
zoey chene02881a2019-12-30 16:11:23 +08008698
8699 @Override
8700 public boolean isIccLockEnabled(int subId) {
8701 enforceReadPrivilegedPermission("isIccLockEnabled");
8702
8703 // Now that all security checks passes, perform the operation as ourselves.
8704 final long identity = Binder.clearCallingIdentity();
8705 try {
8706 Phone phone = getPhone(subId);
8707 if (phone != null && phone.getIccCard() != null) {
8708 return phone.getIccCard().getIccLockEnabled();
8709 } else {
8710 return false;
8711 }
8712 } finally {
8713 Binder.restoreCallingIdentity(identity);
8714 }
8715 }
8716
8717 /**
8718 * Set the ICC pin lock enabled or disabled.
8719 *
8720 * @return an integer representing the status of IccLock enabled or disabled in the following
8721 * three cases:
8722 * - {@link TelephonyManager#CHANGE_ICC_LOCK_SUCCESS} if enabled or disabled IccLock
8723 * successfully.
8724 * - Positive number and zero for remaining password attempts.
8725 * - Negative number for other failure cases (such like enabling/disabling PIN failed).
8726 *
8727 */
8728 @Override
8729 public int setIccLockEnabled(int subId, boolean enabled, String password) {
8730 enforceModifyPermission();
8731
8732 Phone phone = getPhone(subId);
8733 if (phone == null) {
8734 return 0;
8735 }
8736 // Now that all security checks passes, perform the operation as ourselves.
8737 final long identity = Binder.clearCallingIdentity();
8738 try {
8739 int attemptsRemaining = (int) sendRequest(CMD_SET_ICC_LOCK_ENABLED,
8740 new Pair<Boolean, String>(enabled, password), phone, null);
8741 return attemptsRemaining;
8742
8743 } catch (Exception e) {
8744 Log.e(LOG_TAG, "setIccLockEnabled. Exception e =" + e);
8745 } finally {
8746 Binder.restoreCallingIdentity(identity);
8747 }
8748 return 0;
8749 }
8750
8751 /**
8752 * Change the ICC password used in ICC pin lock.
8753 *
8754 * @return an integer representing the status of IccLock changed in the following three cases:
8755 * - {@link TelephonyManager#CHANGE_ICC_LOCK_SUCCESS} if changed IccLock successfully.
8756 * - Positive number and zero for remaining password attempts.
8757 * - Negative number for other failure cases (such like enabling/disabling PIN failed).
8758 *
8759 */
8760 @Override
8761 public int changeIccLockPassword(int subId, String oldPassword, String newPassword) {
8762 enforceModifyPermission();
8763
8764 Phone phone = getPhone(subId);
8765 if (phone == null) {
8766 return 0;
8767 }
8768 // Now that all security checks passes, perform the operation as ourselves.
8769 final long identity = Binder.clearCallingIdentity();
8770 try {
8771 int attemptsRemaining = (int) sendRequest(CMD_CHANGE_ICC_LOCK_PASSWORD,
8772 new Pair<String, String>(oldPassword, newPassword), phone, null);
8773 return attemptsRemaining;
8774
8775 } catch (Exception e) {
8776 Log.e(LOG_TAG, "changeIccLockPassword. Exception e =" + e);
8777 } finally {
8778 Binder.restoreCallingIdentity(identity);
8779 }
8780 return 0;
8781 }
Peter Wangdafb9ac2020-01-15 14:13:38 -08008782
8783 /**
8784 * Request for receiving user activity notification
8785 */
8786 @Override
8787 public void requestUserActivityNotification() {
8788 if (!mNotifyUserActivity.get()
8789 && !mMainThreadHandler.hasMessages(MSG_NOTIFY_USER_ACTIVITY)) {
8790 mNotifyUserActivity.set(true);
8791 }
8792 }
8793
8794 /**
8795 * Called when userActivity is signalled in the power manager.
8796 * This is safe to call from any thread, with any window manager locks held or not.
8797 */
8798 @Override
8799 public void userActivity() {
8800 // ***************************************
8801 // * Inherited from PhoneWindowManager *
8802 // ***************************************
8803 // THIS IS CALLED FROM DEEP IN THE POWER MANAGER
8804 // WITH ITS LOCKS HELD.
8805 //
8806 // This code must be VERY careful about the locks
8807 // it acquires.
8808 // In fact, the current code acquires way too many,
8809 // and probably has lurking deadlocks.
8810
8811 if (Binder.getCallingUid() != Process.SYSTEM_UID) {
8812 throw new SecurityException("Only the OS may call notifyUserActivity()");
8813 }
8814
8815 if (mNotifyUserActivity.getAndSet(false)) {
8816 mMainThreadHandler.sendEmptyMessageDelayed(MSG_NOTIFY_USER_ACTIVITY,
8817 USER_ACTIVITY_NOTIFICATION_DELAY);
8818 }
8819 }
Malcolm Chen4639c562020-04-13 11:59:40 -07008820
8821 @Override
8822 public boolean canConnectTo5GInDsdsMode() {
8823 return mApp.getResources().getBoolean(R.bool.config_5g_connection_in_dsds_mode);
8824 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07008825}