blob: 9b75007de8d1f887d5d1879f01c0712ad363fde3 [file] [log] [blame]
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001/*
2 * Copyright (C) 2006 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.phone;
18
Hall Liud892bec2018-11-30 14:51:45 -080019import static android.content.pm.PackageManager.PERMISSION_GRANTED;
20
Tyler Gunn7bcdc742019-10-04 15:56:59 -070021import static com.android.internal.telephony.PhoneConstants.PHONE_TYPE_IMS;
Ta-wei Yen87c49842016-05-13 21:19:52 -070022import static com.android.internal.telephony.PhoneConstants.SUBSCRIPTION_KEY;
23
Ta-wei Yen30a69c82016-12-27 14:52:32 -080024import android.Manifest.permission;
changbetty363e8ac2019-12-06 18:16:37 +080025import android.annotation.NonNull;
Tyler Gunnf70ed162019-04-03 15:28:53 -070026import android.annotation.Nullable;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070027import android.app.AppOpsManager;
Ta-wei Yen30a69c82016-12-27 14:52:32 -080028import android.app.PendingIntent;
Sailesh Nepalbd76e4e2013-10-27 13:59:44 -070029import android.content.ComponentName;
Amit Mahajan7dbbd822019-03-13 17:33:47 -070030import android.content.ContentResolver;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070031import android.content.Context;
32import android.content.Intent;
Derek Tan97ebb422014-09-05 16:55:38 -070033import android.content.SharedPreferences;
Nathan Harold31d7ff32018-10-15 20:20:30 -070034import android.content.pm.ApplicationInfo;
Derek Tan740e1672017-06-27 14:56:27 -070035import android.content.pm.ComponentInfo;
Amith Yamasani6e118872016-02-19 12:53:51 -080036import android.content.pm.PackageInfo;
Shishir Agrawal60f9c952014-06-23 12:00:43 -070037import android.content.pm.PackageManager;
Jack Yu84291ec2017-05-26 16:07:50 -070038import android.net.NetworkStats;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070039import android.net.Uri;
40import android.os.AsyncResult;
41import android.os.Binder;
Hall Liuf19c44f2018-11-27 14:38:17 -080042import android.os.Build;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070043import android.os.Bundle;
44import android.os.Handler;
yinxu504e1392017-04-12 16:03:22 -070045import android.os.IBinder;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070046import android.os.Looper;
47import android.os.Message;
yinxu504e1392017-04-12 16:03:22 -070048import android.os.Messenger;
Malcolm Chen6ca97372019-07-01 16:28:21 -070049import android.os.ParcelUuid;
Tyler Gunn65d45c22017-06-05 11:22:26 -070050import android.os.PersistableBundle;
Shuo Qian5bac1ee2020-01-16 20:51:11 -080051import android.os.Process;
Brad Ebinger5f64b052017-12-14 14:26:15 -080052import android.os.RemoteException;
Adam Lesinski903a54c2016-04-11 14:49:52 -070053import android.os.ResultReceiver;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070054import android.os.ServiceManager;
Brad Ebinger1ce9c432019-07-16 13:19:44 -070055import android.os.ServiceSpecificException;
Brad Ebingerdac2f002018-04-03 15:17:52 -070056import android.os.ShellCallback;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070057import android.os.UserHandle;
Stuart Scott981d8582015-04-21 14:09:50 -070058import android.os.UserManager;
Sooraj Sasindran9a909312016-07-18 11:57:25 -070059import android.os.WorkSource;
Derek Tan97ebb422014-09-05 16:55:38 -070060import android.preference.PreferenceManager;
Naina Nalluri8ff344d2019-09-17 14:10:30 -070061import android.provider.DeviceConfig;
Ihab Awadf2177b72013-11-25 13:33:23 -080062import android.provider.Settings;
Amit Mahajan7dbbd822019-03-13 17:33:47 -070063import android.provider.Telephony;
Inseob Kim8d298d42018-12-13 17:11:34 +090064import android.sysprop.TelephonyProperties;
Santos Cordon7a1885b2015-02-03 11:15:19 -080065import android.telecom.PhoneAccount;
Nancy Chen31f9ba12016-01-06 11:42:12 -080066import android.telecom.PhoneAccountHandle;
Andrew Lee9431b832015-03-09 18:46:45 -070067import android.telecom.TelecomManager;
Chen Xuc7b18ec2019-09-26 22:48:11 -070068import android.telephony.Annotation.ApnType;
Junda Liu12f7d802015-05-01 12:06:44 -070069import android.telephony.CarrierConfigManager;
Michele Berionne482f8202018-11-27 18:57:59 -080070import android.telephony.CarrierRestrictionRules;
yincheng zhaod698b842019-09-06 17:06:54 -070071import android.telephony.CellIdentity;
Meng Wangd64acad2019-12-09 13:13:01 -080072import android.telephony.CellIdentityCdma;
73import android.telephony.CellIdentityGsm;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070074import android.telephony.CellInfo;
Nathan Haroldf180aac2018-06-01 18:43:55 -070075import android.telephony.CellInfoGsm;
76import android.telephony.CellInfoWcdma;
Sooraj Sasindran9a909312016-07-18 11:57:25 -070077import android.telephony.ClientRequestStats;
Nathan Haroldfa8da0f2018-09-27 18:51:29 -070078import android.telephony.ICellInfoCallback;
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -070079import android.telephony.IccOpenLogicalChannelResponse;
Hall Liu1aa510f2017-11-22 17:40:08 -080080import android.telephony.LocationAccessPolicy;
Ta-wei Yen87c49842016-05-13 21:19:52 -070081import android.telephony.ModemActivityInfo;
Jake Hambye994d462014-02-03 13:10:13 -080082import android.telephony.NeighboringCellInfo;
yinxu504e1392017-04-12 16:03:22 -070083import android.telephony.NetworkScanRequest;
Michele4245e952019-02-04 11:36:23 -080084import android.telephony.PhoneCapability;
Hall Liud892bec2018-11-30 14:51:45 -080085import android.telephony.PhoneNumberRange;
Wink Saville5d475dd2014-10-17 15:00:58 -070086import android.telephony.RadioAccessFamily;
Hall Liub2ac8ef2019-02-28 15:56:23 -080087import android.telephony.RadioAccessSpecifier;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070088import android.telephony.ServiceState;
Nathan Harold46b42aa2017-03-10 19:38:22 -080089import android.telephony.SignalStrength;
Wink Saville0f3b5fc2014-11-11 08:40:49 -080090import android.telephony.SubscriptionInfo;
Jeff Sharkey85190e62014-12-05 09:40:12 -080091import android.telephony.SubscriptionManager;
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 Ebinger774ba362019-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 Ebinger77b832e2019-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;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700118import android.util.Log;
Jake Hambye994d462014-02-03 13:10:13 -0800119import android.util.Pair;
Jeff Sharkey85190e62014-12-05 09:40:12 -0800120import android.util.Slog;
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;
Shishir Agrawal566b7612013-10-28 14:41:00 -0700124import com.android.internal.telephony.CallManager;
Tyler Gunn52dcf772017-04-26 11:30:31 -0700125import com.android.internal.telephony.CallStateException;
pkanwar79ec0542017-07-31 14:10:01 -0700126import com.android.internal.telephony.CarrierInfoManager;
chen xu651eec72018-11-11 19:03:44 -0800127import com.android.internal.telephony.CarrierResolver;
Shishir Agrawal302c8692015-06-19 13:49:39 -0700128import com.android.internal.telephony.CellNetworkScanResult;
Shishir Agrawal566b7612013-10-28 14:41:00 -0700129import com.android.internal.telephony.CommandException;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700130import com.android.internal.telephony.DefaultPhoneNotifier;
Nathan Harold48d6fd52019-02-06 19:01:40 -0800131import com.android.internal.telephony.HalVersion;
Brad Ebingera63db5f2019-04-23 16:31:13 -0700132import com.android.internal.telephony.IIntegerConsumer;
Hall Liud892bec2018-11-30 14:51:45 -0800133import com.android.internal.telephony.INumberVerificationCallback;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700134import com.android.internal.telephony.ITelephony;
Jake Hambye994d462014-02-03 13:10:13 -0800135import com.android.internal.telephony.IccCard;
Jack Yu5f7092c2018-04-13 14:05:37 -0700136import com.android.internal.telephony.LocaleTracker;
yinxub1bed742017-04-17 11:45:04 -0700137import com.android.internal.telephony.NetworkScanRequestTracker;
Shishir Agrawal302c8692015-06-19 13:49:39 -0700138import com.android.internal.telephony.OperatorInfo;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700139import com.android.internal.telephony.Phone;
Malcolm Chen2c63d402018-08-14 16:00:53 -0700140import com.android.internal.telephony.PhoneConfigurationManager;
Nathan Harolda667c152016-12-14 11:27:20 -0800141import com.android.internal.telephony.PhoneConstantConversions;
Ta-wei Yen87c49842016-05-13 21:19:52 -0700142import com.android.internal.telephony.PhoneConstants;
Wink Saville36469e72014-06-11 15:17:00 -0700143import com.android.internal.telephony.PhoneFactory;
Wink Saville5d475dd2014-10-17 15:00:58 -0700144import com.android.internal.telephony.ProxyController;
Sanket Padawe99ef1e32016-05-18 16:12:33 -0700145import com.android.internal.telephony.RIL;
Svet Ganovb320e182015-04-16 12:30:10 -0700146import com.android.internal.telephony.RILConstants;
Jack Yu5f7092c2018-04-13 14:05:37 -0700147import com.android.internal.telephony.ServiceStateTracker;
Amit Mahajandccb3f12019-05-13 13:48:32 -0700148import com.android.internal.telephony.SmsController;
Brad Ebingera63db5f2019-04-23 16:31:13 -0700149import com.android.internal.telephony.SmsPermissions;
Wink Savilleac1bdfd2014-11-20 23:04:44 -0800150import com.android.internal.telephony.SubscriptionController;
Jeff Davidson7e17e312018-02-13 18:17:36 -0800151import com.android.internal.telephony.TelephonyPermissions;
Malcolm Chendc8c10e2019-04-10 18:25:07 -0700152import com.android.internal.telephony.dataconnection.ApnSettingUtils;
sqianf4ca7ed2019-01-15 18:32:07 -0800153import com.android.internal.telephony.emergency.EmergencyNumberTracker;
Derek Tan740e1672017-06-27 14:56:27 -0700154import com.android.internal.telephony.euicc.EuiccConnector;
Brad Ebinger9c0eb502019-01-23 15:06:19 -0800155import com.android.internal.telephony.ims.ImsResolver;
Tyler Gunn7bcdc742019-10-04 15:56:59 -0700156import com.android.internal.telephony.imsphone.ImsPhone;
157import com.android.internal.telephony.imsphone.ImsPhoneCallTracker;
Pengquan Meng6c2dc9f2019-02-06 11:12:53 -0800158import com.android.internal.telephony.metrics.TelephonyMetrics;
Taesu Leef8fbed92019-10-07 18:47:02 +0900159import com.android.internal.telephony.uicc.IccCardApplicationStatus.AppType;
Shishir Agrawal566b7612013-10-28 14:41:00 -0700160import com.android.internal.telephony.uicc.IccIoResult;
changbetty363e8ac2019-12-06 18:16:37 +0800161import com.android.internal.telephony.uicc.IccRecords;
Shishir Agrawal566b7612013-10-28 14:41:00 -0700162import com.android.internal.telephony.uicc.IccUtils;
Nathan Haroldb3014052017-01-25 15:57:32 -0800163import com.android.internal.telephony.uicc.SIMRecords;
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700164import com.android.internal.telephony.uicc.UiccCard;
Nathan Haroldb3014052017-01-25 15:57:32 -0800165import com.android.internal.telephony.uicc.UiccCardApplication;
Shishir Agrawal566b7612013-10-28 14:41:00 -0700166import com.android.internal.telephony.uicc.UiccController;
Jeff Davidson7e17e312018-02-13 18:17:36 -0800167import com.android.internal.telephony.uicc.UiccProfile;
Holly Jiuyu Sun01c47ad2018-01-24 17:56:33 +0000168import com.android.internal.telephony.uicc.UiccSlot;
zoey chen84e2b212019-12-18 17:07:20 +0800169import com.android.internal.telephony.util.LocaleUtils;
fionaxu7ed723d2017-05-30 18:58:54 -0700170import com.android.internal.telephony.util.VoicemailNotificationSettingsUtil;
Jake Hambye994d462014-02-03 13:10:13 -0800171import com.android.internal.util.HexDump;
Brad Ebingera63db5f2019-04-23 16:31:13 -0700172import com.android.phone.settings.PickSmsSubscriptionActivity;
Ta-wei Yenc9df0432017-04-17 17:09:07 -0700173import com.android.phone.vvm.PhoneAccountHandleConverter;
Ta-wei Yen527a9c02017-01-06 15:29:25 -0800174import com.android.phone.vvm.RemoteVvmTaskManager;
Ta-wei Yenc9df0432017-04-17 17:09:07 -0700175import com.android.phone.vvm.VisualVoicemailSettingsUtil;
Ta-wei Yenc8905312017-03-28 11:14:45 -0700176import com.android.phone.vvm.VisualVoicemailSmsFilterConfig;
Peter Wang050bb052020-01-13 23:33:09 -0800177import com.android.telephony.Rlog;
Ta-wei Yen30a69c82016-12-27 14:52:32 -0800178
Ta-wei Yenc236d6b2016-06-21 13:33:12 -0700179import java.io.FileDescriptor;
180import java.io.PrintWriter;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700181import java.util.ArrayList;
Jeff Sharkey85190e62014-12-05 09:40:12 -0800182import java.util.Arrays;
sqian11b7a0e2018-12-05 18:48:28 -0800183import java.util.HashMap;
sqianf4ca7ed2019-01-15 18:32:07 -0800184import java.util.HashSet;
Jake Hambye994d462014-02-03 13:10:13 -0800185import java.util.List;
Narayan Kamath1c496c22015-04-16 14:40:19 +0100186import java.util.Locale;
Jeff Sharkey85190e62014-12-05 09:40:12 -0800187import java.util.Map;
Nazanin Bakhshif71371d2019-04-29 17:29:44 -0700188import java.util.NoSuchElementException;
sqianf4ca7ed2019-01-15 18:32:07 -0800189import java.util.Set;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700190
191/**
192 * Implementation of the ITelephony interface.
193 */
Santos Cordon117fee72014-05-16 17:56:12 -0700194public class PhoneInterfaceManager extends ITelephony.Stub {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700195 private static final String LOG_TAG = "PhoneInterfaceManager";
196 private static final boolean DBG = (PhoneGlobals.DBG_LEVEL >= 2);
197 private static final boolean DBG_LOC = false;
Jeff Sharkey85190e62014-12-05 09:40:12 -0800198 private static final boolean DBG_MERGE = false;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700199
200 // Message codes used with mMainThreadHandler
201 private static final int CMD_HANDLE_PIN_MMI = 1;
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700202 private static final int CMD_TRANSMIT_APDU_LOGICAL_CHANNEL = 7;
203 private static final int EVENT_TRANSMIT_APDU_LOGICAL_CHANNEL_DONE = 8;
Shishir Agrawal566b7612013-10-28 14:41:00 -0700204 private static final int CMD_OPEN_CHANNEL = 9;
205 private static final int EVENT_OPEN_CHANNEL_DONE = 10;
206 private static final int CMD_CLOSE_CHANNEL = 11;
207 private static final int EVENT_CLOSE_CHANNEL_DONE = 12;
Jake Hambye994d462014-02-03 13:10:13 -0800208 private static final int CMD_NV_READ_ITEM = 13;
209 private static final int EVENT_NV_READ_ITEM_DONE = 14;
210 private static final int CMD_NV_WRITE_ITEM = 15;
211 private static final int EVENT_NV_WRITE_ITEM_DONE = 16;
212 private static final int CMD_NV_WRITE_CDMA_PRL = 17;
213 private static final int EVENT_NV_WRITE_CDMA_PRL_DONE = 18;
chen xu6dac5ab2018-10-26 17:39:23 -0700214 private static final int CMD_RESET_MODEM_CONFIG = 19;
215 private static final int EVENT_RESET_MODEM_CONFIG_DONE = 20;
Jake Hamby7c27be32014-03-03 13:25:59 -0800216 private static final int CMD_GET_PREFERRED_NETWORK_TYPE = 21;
217 private static final int EVENT_GET_PREFERRED_NETWORK_TYPE_DONE = 22;
218 private static final int CMD_SET_PREFERRED_NETWORK_TYPE = 23;
219 private static final int EVENT_SET_PREFERRED_NETWORK_TYPE_DONE = 24;
Sailesh Nepal35b59452014-03-06 09:26:56 -0800220 private static final int CMD_SEND_ENVELOPE = 25;
221 private static final int EVENT_SEND_ENVELOPE_DONE = 26;
Shuo Qian850e4d6a2018-04-25 21:02:08 +0000222 private static final int CMD_INVOKE_OEM_RIL_REQUEST_RAW = 27;
223 private static final int EVENT_INVOKE_OEM_RIL_REQUEST_RAW_DONE = 28;
Derek Tan6b088ee2014-09-05 14:15:18 -0700224 private static final int CMD_TRANSMIT_APDU_BASIC_CHANNEL = 29;
225 private static final int EVENT_TRANSMIT_APDU_BASIC_CHANNEL_DONE = 30;
226 private static final int CMD_EXCHANGE_SIM_IO = 31;
227 private static final int EVENT_EXCHANGE_SIM_IO_DONE = 32;
Shishir Agrawal76d5da92014-11-09 16:17:25 -0800228 private static final int CMD_SET_VOICEMAIL_NUMBER = 33;
229 private static final int EVENT_SET_VOICEMAIL_NUMBER_DONE = 34;
Stuart Scott54788802015-03-30 13:18:01 -0700230 private static final int CMD_SET_NETWORK_SELECTION_MODE_AUTOMATIC = 35;
231 private static final int EVENT_SET_NETWORK_SELECTION_MODE_AUTOMATIC_DONE = 36;
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -0700232 private static final int CMD_GET_MODEM_ACTIVITY_INFO = 37;
233 private static final int EVENT_GET_MODEM_ACTIVITY_INFO_DONE = 38;
Shishir Agrawal302c8692015-06-19 13:49:39 -0700234 private static final int CMD_PERFORM_NETWORK_SCAN = 39;
235 private static final int EVENT_PERFORM_NETWORK_SCAN_DONE = 40;
236 private static final int CMD_SET_NETWORK_SELECTION_MODE_MANUAL = 41;
237 private static final int EVENT_SET_NETWORK_SELECTION_MODE_MANUAL_DONE = 42;
Meng Wang1a7c35a2016-05-05 20:56:15 -0700238 private static final int CMD_SET_ALLOWED_CARRIERS = 43;
239 private static final int EVENT_SET_ALLOWED_CARRIERS_DONE = 44;
240 private static final int CMD_GET_ALLOWED_CARRIERS = 45;
241 private static final int EVENT_GET_ALLOWED_CARRIERS_DONE = 46;
pkanwar32d516d2016-10-14 19:37:38 -0700242 private static final int CMD_HANDLE_USSD_REQUEST = 47;
Nathan Haroldb3014052017-01-25 15:57:32 -0800243 private static final int CMD_GET_FORBIDDEN_PLMNS = 48;
244 private static final int EVENT_GET_FORBIDDEN_PLMNS_DONE = 49;
Holly Jiuyu Sun01c47ad2018-01-24 17:56:33 +0000245 private static final int CMD_SWITCH_SLOTS = 50;
246 private static final int EVENT_SWITCH_SLOTS_DONE = 51;
Pengquan Menga1bb6272018-09-06 09:59:22 -0700247 private static final int CMD_GET_NETWORK_SELECTION_MODE = 52;
248 private static final int EVENT_GET_NETWORK_SELECTION_MODE_DONE = 53;
249 private static final int CMD_GET_CDMA_ROAMING_MODE = 54;
250 private static final int EVENT_GET_CDMA_ROAMING_MODE_DONE = 55;
251 private static final int CMD_SET_CDMA_ROAMING_MODE = 56;
252 private static final int EVENT_SET_CDMA_ROAMING_MODE_DONE = 57;
253 private static final int CMD_SET_CDMA_SUBSCRIPTION_MODE = 58;
254 private static final int EVENT_SET_CDMA_SUBSCRIPTION_MODE_DONE = 59;
Nathan Harold3ff88932018-08-14 10:19:49 -0700255 private static final int CMD_GET_ALL_CELL_INFO = 60;
256 private static final int EVENT_GET_ALL_CELL_INFO_DONE = 61;
257 private static final int CMD_GET_CELL_LOCATION = 62;
258 private static final int EVENT_GET_CELL_LOCATION_DONE = 63;
chen xu6dac5ab2018-10-26 17:39:23 -0700259 private static final int CMD_MODEM_REBOOT = 64;
260 private static final int EVENT_CMD_MODEM_REBOOT_DONE = 65;
Nathan Haroldfa8da0f2018-09-27 18:51:29 -0700261 private static final int CMD_REQUEST_CELL_INFO_UPDATE = 66;
262 private static final int EVENT_REQUEST_CELL_INFO_UPDATE_DONE = 67;
Malcolm Chen8e4ed912019-01-15 20:22:16 -0800263 private static final int CMD_REQUEST_ENABLE_MODEM = 68;
264 private static final int EVENT_ENABLE_MODEM_DONE = 69;
Nazanin Bakhshif71371d2019-04-29 17:29:44 -0700265 private static final int CMD_GET_MODEM_STATUS = 70;
266 private static final int EVENT_GET_MODEM_STATUS_DONE = 71;
yincheng zhaod698b842019-09-06 17:06:54 -0700267 private static final int CMD_SET_FORBIDDEN_PLMNS = 72;
268 private static final int EVENT_SET_FORBIDDEN_PLMNS_DONE = 73;
Naina Nalluri8ff344d2019-09-17 14:10:30 -0700269 private static final int CMD_ERASE_MODEM_CONFIG = 74;
270 private static final int EVENT_ERASE_MODEM_CONFIG_DONE = 75;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700271
Holly Jiuyu Sun1cc2d552018-01-26 15:51:16 -0800272 // Parameters of select command.
273 private static final int SELECT_COMMAND = 0xA4;
274 private static final int SELECT_P1 = 0x04;
275 private static final int SELECT_P2 = 0;
276 private static final int SELECT_P3 = 0x10;
277
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700278 /** The singleton instance. */
279 private static PhoneInterfaceManager sInstance;
280
Wink Saville3ab207e2014-11-20 13:07:20 -0800281 private PhoneGlobals mApp;
Wink Saville3ab207e2014-11-20 13:07:20 -0800282 private CallManager mCM;
Brad Ebinger05f52c22019-12-05 13:03:21 -0800283 private ImsResolver mImsResolver;
Stuart Scott981d8582015-04-21 14:09:50 -0700284 private UserManager mUserManager;
Wink Saville3ab207e2014-11-20 13:07:20 -0800285 private AppOpsManager mAppOps;
286 private MainThreadHandler mMainThreadHandler;
Wink Savilleac1bdfd2014-11-20 23:04:44 -0800287 private SubscriptionController mSubscriptionController;
Wink Saville3ab207e2014-11-20 13:07:20 -0800288 private SharedPreferences mTelephonySharedPreferences;
Malcolm Chen2c63d402018-08-14 16:00:53 -0700289 private PhoneConfigurationManager mPhoneConfigurationManager;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700290
Derek Tan97ebb422014-09-05 16:55:38 -0700291 private static final String PREF_CARRIERS_ALPHATAG_PREFIX = "carrier_alphtag_";
292 private static final String PREF_CARRIERS_NUMBER_PREFIX = "carrier_number_";
Jeff Sharkey85190e62014-12-05 09:40:12 -0800293 private static final String PREF_CARRIERS_SUBSCRIBER_PREFIX = "carrier_subscriber_";
Brad Ebinger1c8542e2019-01-14 13:43:14 -0800294 private static final String PREF_PROVISION_IMS_MMTEL_PREFIX = "provision_ims_mmtel_";
Derek Tan89e89d42014-07-08 17:00:10 -0700295
Michelecea4cf22018-12-21 15:00:11 -0800296 // String to store multi SIM allowed
297 private static final String PREF_MULTI_SIM_RESTRICTED = "multisim_restricted";
298
Derek Tan740e1672017-06-27 14:56:27 -0700299 // The AID of ISD-R.
300 private static final String ISDR_AID = "A0000005591010FFFFFFFF8900000100";
301
yinxub1bed742017-04-17 11:45:04 -0700302 private NetworkScanRequestTracker mNetworkScanRequestTracker;
303
David Kelly5e06a7f2018-03-12 14:10:59 +0000304 private static final int TYPE_ALLOCATION_CODE_LENGTH = 8;
305 private static final int MANUFACTURER_CODE_LENGTH = 8;
306
Derek Tan89e89d42014-07-08 17:00:10 -0700307 /**
Naina Nalluri8ff344d2019-09-17 14:10:30 -0700308 * Experiment flag to enable erase modem config on reset network, default value is false
309 */
310 public static final String RESET_NETWORK_ERASE_MODEM_CONFIG_ENABLED =
311 "reset_network_erase_modem_config_enabled";
312
313 /**
Shishir Agrawal566b7612013-10-28 14:41:00 -0700314 * A request object to use for transmitting data to an ICC.
315 */
316 private static final class IccAPDUArgument {
317 public int channel, cla, command, p1, p2, p3;
318 public String data;
319
320 public IccAPDUArgument(int channel, int cla, int command,
321 int p1, int p2, int p3, String data) {
322 this.channel = channel;
323 this.cla = cla;
324 this.command = command;
325 this.p1 = p1;
326 this.p2 = p2;
327 this.p3 = p3;
328 this.data = data;
329 }
330 }
331
332 /**
Shishir Agrawal77ba3172015-09-10 14:50:19 -0700333 * A request object to use for transmitting data to an ICC.
334 */
335 private static final class ManualNetworkSelectionArgument {
336 public OperatorInfo operatorInfo;
337 public boolean persistSelection;
338
339 public ManualNetworkSelectionArgument(OperatorInfo operatorInfo, boolean persistSelection) {
340 this.operatorInfo = operatorInfo;
341 this.persistSelection = persistSelection;
342 }
343 }
344
345 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700346 * A request object for use with {@link MainThreadHandler}. Requesters should wait() on the
347 * request after sending. The main thread will notify the request when it is complete.
348 */
349 private static final class MainThreadRequest {
350 /** The argument to use for the request */
351 public Object argument;
352 /** The result of the request that is run on the main thread */
353 public Object result;
Sanket Padawe56e75a32016-02-08 12:18:19 -0800354 // The subscriber id that this request applies to. Defaults to
355 // SubscriptionManager.INVALID_SUBSCRIPTION_ID
356 public Integer subId = SubscriptionManager.INVALID_SUBSCRIPTION_ID;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700357
Nathan Harold92bed182018-10-12 18:16:49 -0700358 // In cases where subId is unavailable, the caller needs to specify the phone.
359 public Phone phone;
360
vagdeviaf9a5b92018-08-15 16:01:53 -0700361 public WorkSource workSource;
362
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700363 public MainThreadRequest(Object argument) {
364 this.argument = argument;
365 }
Shishir Agrawal76d5da92014-11-09 16:17:25 -0800366
Nathan Harold92bed182018-10-12 18:16:49 -0700367 MainThreadRequest(Object argument, Phone phone, WorkSource workSource) {
368 this.argument = argument;
369 if (phone != null) {
370 this.phone = phone;
371 }
372 this.workSource = workSource;
373 }
374
vagdeviaf9a5b92018-08-15 16:01:53 -0700375 MainThreadRequest(Object argument, Integer subId, WorkSource workSource) {
Shishir Agrawal76d5da92014-11-09 16:17:25 -0800376 this.argument = argument;
Sanket Padawe56e75a32016-02-08 12:18:19 -0800377 if (subId != null) {
378 this.subId = subId;
379 }
vagdeviaf9a5b92018-08-15 16:01:53 -0700380 this.workSource = workSource;
Shishir Agrawal76d5da92014-11-09 16:17:25 -0800381 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700382 }
383
Sailesh Nepalcc0375f2013-11-13 09:15:18 -0800384 private static final class IncomingThirdPartyCallArgs {
385 public final ComponentName component;
386 public final String callId;
387 public final String callerDisplayName;
388
389 public IncomingThirdPartyCallArgs(ComponentName component, String callId,
390 String callerDisplayName) {
391 this.component = component;
392 this.callId = callId;
393 this.callerDisplayName = callerDisplayName;
394 }
395 }
396
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700397 /**
398 * A handler that processes messages on the main thread in the phone process. Since many
399 * of the Phone calls are not thread safe this is needed to shuttle the requests from the
400 * inbound binder threads to the main thread in the phone process. The Binder thread
401 * may provide a {@link MainThreadRequest} object in the msg.obj field that they are waiting
402 * on, which will be notified when the operation completes and will contain the result of the
403 * request.
404 *
405 * <p>If a MainThreadRequest object is provided in the msg.obj field,
406 * note that request.result must be set to something non-null for the calling thread to
407 * unblock.
408 */
409 private final class MainThreadHandler extends Handler {
410 @Override
411 public void handleMessage(Message msg) {
412 MainThreadRequest request;
413 Message onCompleted;
414 AsyncResult ar;
Shishir Agrawalc04d9752016-02-19 10:41:00 -0800415 UiccCard uiccCard;
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700416 IccAPDUArgument iccArgument;
Nazanin Bakhshif782e562018-12-11 15:15:39 -0800417 final Phone defaultPhone = getDefaultPhone();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700418
419 switch (msg.what) {
Pengquan Menga1bb6272018-09-06 09:59:22 -0700420 case CMD_HANDLE_USSD_REQUEST: {
421 request = (MainThreadRequest) msg.obj;
422 final Phone phone = getPhoneFromRequest(request);
423 Pair<String, ResultReceiver> ussdObject = (Pair) request.argument;
424 String ussdRequest = ussdObject.first;
425 ResultReceiver wrappedCallback = ussdObject.second;
Tyler Gunn65d45c22017-06-05 11:22:26 -0700426
Pengquan Menga1bb6272018-09-06 09:59:22 -0700427 if (!isUssdApiAllowed(request.subId)) {
428 // Carrier does not support use of this API, return failure.
429 Rlog.w(LOG_TAG, "handleUssdRequest: carrier does not support USSD apis.");
430 UssdResponse response = new UssdResponse(ussdRequest, null);
431 Bundle returnData = new Bundle();
432 returnData.putParcelable(TelephonyManager.USSD_RESPONSE, response);
433 wrappedCallback.send(TelephonyManager.USSD_RETURN_FAILURE, returnData);
Tyler Gunn65d45c22017-06-05 11:22:26 -0700434
Pengquan Menga1bb6272018-09-06 09:59:22 -0700435 request.result = true;
436 notifyRequester(request);
437 return;
438 }
Tyler Gunn65d45c22017-06-05 11:22:26 -0700439
Pengquan Menga1bb6272018-09-06 09:59:22 -0700440 try {
441 request.result = phone != null
442 ? phone.handleUssdRequest(ussdRequest, wrappedCallback) : false;
443 } catch (CallStateException cse) {
444 request.result = false;
445 }
446 // Wake up the requesting thread
447 notifyRequester(request);
448 break;
pkanwar32d516d2016-10-14 19:37:38 -0700449 }
450
Yorke Lee716f67e2015-06-17 15:39:16 -0700451 case CMD_HANDLE_PIN_MMI: {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700452 request = (MainThreadRequest) msg.obj;
Yorke Lee716f67e2015-06-17 15:39:16 -0700453 final Phone phone = getPhoneFromRequest(request);
454 request.result = phone != null ?
455 getPhoneFromRequest(request).handlePinMmi((String) request.argument)
456 : false;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700457 // Wake up the requesting thread
Pengquan Menga1bb6272018-09-06 09:59:22 -0700458 notifyRequester(request);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700459 break;
Yorke Lee716f67e2015-06-17 15:39:16 -0700460 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700461
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700462 case CMD_TRANSMIT_APDU_LOGICAL_CHANNEL:
Shishir Agrawal566b7612013-10-28 14:41:00 -0700463 request = (MainThreadRequest) msg.obj;
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700464 iccArgument = (IccAPDUArgument) request.argument;
Shishir Agrawalc04d9752016-02-19 10:41:00 -0800465 uiccCard = getUiccCardFromRequest(request);
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700466 if (uiccCard == null) {
467 loge("iccTransmitApduLogicalChannel: No UICC");
468 request.result = new IccIoResult(0x6F, 0, (byte[])null);
Pengquan Menga1bb6272018-09-06 09:59:22 -0700469 notifyRequester(request);
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700470 } else {
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700471 onCompleted = obtainMessage(EVENT_TRANSMIT_APDU_LOGICAL_CHANNEL_DONE,
472 request);
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700473 uiccCard.iccTransmitApduLogicalChannel(
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700474 iccArgument.channel, iccArgument.cla, iccArgument.command,
475 iccArgument.p1, iccArgument.p2, iccArgument.p3, iccArgument.data,
Shishir Agrawal566b7612013-10-28 14:41:00 -0700476 onCompleted);
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700477 }
Shishir Agrawal566b7612013-10-28 14:41:00 -0700478 break;
479
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700480 case EVENT_TRANSMIT_APDU_LOGICAL_CHANNEL_DONE:
Shishir Agrawal566b7612013-10-28 14:41:00 -0700481 ar = (AsyncResult) msg.obj;
482 request = (MainThreadRequest) ar.userObj;
483 if (ar.exception == null && ar.result != null) {
484 request.result = ar.result;
485 } else {
486 request.result = new IccIoResult(0x6F, 0, (byte[])null);
487 if (ar.result == null) {
488 loge("iccTransmitApduLogicalChannel: Empty response");
Jake Hambye994d462014-02-03 13:10:13 -0800489 } else if (ar.exception instanceof CommandException) {
Shishir Agrawal566b7612013-10-28 14:41:00 -0700490 loge("iccTransmitApduLogicalChannel: CommandException: " +
Jake Hambye994d462014-02-03 13:10:13 -0800491 ar.exception);
Shishir Agrawal566b7612013-10-28 14:41:00 -0700492 } else {
493 loge("iccTransmitApduLogicalChannel: Unknown exception");
494 }
495 }
Pengquan Menga1bb6272018-09-06 09:59:22 -0700496 notifyRequester(request);
Shishir Agrawal566b7612013-10-28 14:41:00 -0700497 break;
498
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700499 case CMD_TRANSMIT_APDU_BASIC_CHANNEL:
500 request = (MainThreadRequest) msg.obj;
501 iccArgument = (IccAPDUArgument) request.argument;
Shishir Agrawalc04d9752016-02-19 10:41:00 -0800502 uiccCard = getUiccCardFromRequest(request);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700503 if (uiccCard == null) {
504 loge("iccTransmitApduBasicChannel: No UICC");
505 request.result = new IccIoResult(0x6F, 0, (byte[])null);
Pengquan Menga1bb6272018-09-06 09:59:22 -0700506 notifyRequester(request);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700507 } else {
508 onCompleted = obtainMessage(EVENT_TRANSMIT_APDU_BASIC_CHANNEL_DONE,
509 request);
510 uiccCard.iccTransmitApduBasicChannel(
511 iccArgument.cla, iccArgument.command, iccArgument.p1, iccArgument.p2,
512 iccArgument.p3, iccArgument.data, onCompleted);
513 }
514 break;
515
516 case EVENT_TRANSMIT_APDU_BASIC_CHANNEL_DONE:
517 ar = (AsyncResult) msg.obj;
518 request = (MainThreadRequest) ar.userObj;
519 if (ar.exception == null && ar.result != null) {
520 request.result = ar.result;
521 } else {
522 request.result = new IccIoResult(0x6F, 0, (byte[])null);
523 if (ar.result == null) {
524 loge("iccTransmitApduBasicChannel: Empty response");
525 } else if (ar.exception instanceof CommandException) {
526 loge("iccTransmitApduBasicChannel: CommandException: " +
527 ar.exception);
528 } else {
529 loge("iccTransmitApduBasicChannel: Unknown exception");
530 }
531 }
Pengquan Menga1bb6272018-09-06 09:59:22 -0700532 notifyRequester(request);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700533 break;
534
535 case CMD_EXCHANGE_SIM_IO:
536 request = (MainThreadRequest) msg.obj;
537 iccArgument = (IccAPDUArgument) request.argument;
Shishir Agrawalc04d9752016-02-19 10:41:00 -0800538 uiccCard = getUiccCardFromRequest(request);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700539 if (uiccCard == null) {
540 loge("iccExchangeSimIO: No UICC");
541 request.result = new IccIoResult(0x6F, 0, (byte[])null);
Pengquan Menga1bb6272018-09-06 09:59:22 -0700542 notifyRequester(request);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700543 } else {
544 onCompleted = obtainMessage(EVENT_EXCHANGE_SIM_IO_DONE,
545 request);
546 uiccCard.iccExchangeSimIO(iccArgument.cla, /* fileID */
547 iccArgument.command, iccArgument.p1, iccArgument.p2, iccArgument.p3,
548 iccArgument.data, onCompleted);
549 }
550 break;
551
552 case EVENT_EXCHANGE_SIM_IO_DONE:
553 ar = (AsyncResult) msg.obj;
554 request = (MainThreadRequest) ar.userObj;
555 if (ar.exception == null && ar.result != null) {
556 request.result = ar.result;
557 } else {
558 request.result = new IccIoResult(0x6f, 0, (byte[])null);
559 }
Pengquan Menga1bb6272018-09-06 09:59:22 -0700560 notifyRequester(request);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700561 break;
562
Derek Tan4d5e5c12014-02-04 11:54:58 -0800563 case CMD_SEND_ENVELOPE:
564 request = (MainThreadRequest) msg.obj;
Shishir Agrawalc04d9752016-02-19 10:41:00 -0800565 uiccCard = getUiccCardFromRequest(request);
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700566 if (uiccCard == null) {
567 loge("sendEnvelopeWithStatus: No UICC");
568 request.result = new IccIoResult(0x6F, 0, (byte[])null);
Pengquan Menga1bb6272018-09-06 09:59:22 -0700569 notifyRequester(request);
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700570 } else {
571 onCompleted = obtainMessage(EVENT_SEND_ENVELOPE_DONE, request);
572 uiccCard.sendEnvelopeWithStatus((String)request.argument, onCompleted);
573 }
Derek Tan4d5e5c12014-02-04 11:54:58 -0800574 break;
575
576 case EVENT_SEND_ENVELOPE_DONE:
577 ar = (AsyncResult) msg.obj;
578 request = (MainThreadRequest) ar.userObj;
Shishir Agrawal9f9877d2014-03-14 09:36:27 -0700579 if (ar.exception == null && ar.result != null) {
580 request.result = ar.result;
Derek Tan4d5e5c12014-02-04 11:54:58 -0800581 } else {
Shishir Agrawal9f9877d2014-03-14 09:36:27 -0700582 request.result = new IccIoResult(0x6F, 0, (byte[])null);
583 if (ar.result == null) {
584 loge("sendEnvelopeWithStatus: Empty response");
585 } else if (ar.exception instanceof CommandException) {
586 loge("sendEnvelopeWithStatus: CommandException: " +
587 ar.exception);
588 } else {
589 loge("sendEnvelopeWithStatus: exception:" + ar.exception);
590 }
Derek Tan4d5e5c12014-02-04 11:54:58 -0800591 }
Pengquan Menga1bb6272018-09-06 09:59:22 -0700592 notifyRequester(request);
Derek Tan4d5e5c12014-02-04 11:54:58 -0800593 break;
594
Shishir Agrawal566b7612013-10-28 14:41:00 -0700595 case CMD_OPEN_CHANNEL:
596 request = (MainThreadRequest) msg.obj;
Shishir Agrawalc04d9752016-02-19 10:41:00 -0800597 uiccCard = getUiccCardFromRequest(request);
Ajay Nambid7454d32015-12-03 13:50:00 -0800598 Pair<String, Integer> openChannelArgs = (Pair<String, Integer>) request.argument;
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700599 if (uiccCard == null) {
600 loge("iccOpenLogicalChannel: No UICC");
Shishir Agrawalfc0492a2016-02-17 11:15:33 -0800601 request.result = new IccOpenLogicalChannelResponse(-1,
602 IccOpenLogicalChannelResponse.STATUS_MISSING_RESOURCE, null);
Pengquan Menga1bb6272018-09-06 09:59:22 -0700603 notifyRequester(request);
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700604 } else {
605 onCompleted = obtainMessage(EVENT_OPEN_CHANNEL_DONE, request);
Ajay Nambid7454d32015-12-03 13:50:00 -0800606 uiccCard.iccOpenLogicalChannel(openChannelArgs.first,
607 openChannelArgs.second, onCompleted);
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700608 }
Shishir Agrawal566b7612013-10-28 14:41:00 -0700609 break;
610
611 case EVENT_OPEN_CHANNEL_DONE:
612 ar = (AsyncResult) msg.obj;
613 request = (MainThreadRequest) ar.userObj;
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700614 IccOpenLogicalChannelResponse openChannelResp;
Shishir Agrawal566b7612013-10-28 14:41:00 -0700615 if (ar.exception == null && ar.result != null) {
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700616 int[] result = (int[]) ar.result;
617 int channelId = result[0];
618 byte[] selectResponse = null;
619 if (result.length > 1) {
620 selectResponse = new byte[result.length - 1];
621 for (int i = 1; i < result.length; ++i) {
622 selectResponse[i - 1] = (byte) result[i];
623 }
624 }
625 openChannelResp = new IccOpenLogicalChannelResponse(channelId,
Shishir Agrawal527e8bf2014-08-25 08:54:56 -0700626 IccOpenLogicalChannelResponse.STATUS_NO_ERROR, selectResponse);
Shishir Agrawal566b7612013-10-28 14:41:00 -0700627 } else {
Shishir Agrawal566b7612013-10-28 14:41:00 -0700628 if (ar.result == null) {
629 loge("iccOpenLogicalChannel: Empty response");
Shishir Agrawal566b7612013-10-28 14:41:00 -0700630 }
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700631 if (ar.exception != null) {
632 loge("iccOpenLogicalChannel: Exception: " + ar.exception);
633 }
634
Shishir Agrawal527e8bf2014-08-25 08:54:56 -0700635 int errorCode = IccOpenLogicalChannelResponse.STATUS_UNKNOWN_ERROR;
Junda Liua754ba12015-05-20 01:17:52 -0700636 if (ar.exception instanceof CommandException) {
637 CommandException.Error error =
638 ((CommandException) (ar.exception)).getCommandError();
639 if (error == CommandException.Error.MISSING_RESOURCE) {
Shishir Agrawal527e8bf2014-08-25 08:54:56 -0700640 errorCode = IccOpenLogicalChannelResponse.STATUS_MISSING_RESOURCE;
Junda Liua754ba12015-05-20 01:17:52 -0700641 } else if (error == CommandException.Error.NO_SUCH_ELEMENT) {
Shishir Agrawal527e8bf2014-08-25 08:54:56 -0700642 errorCode = IccOpenLogicalChannelResponse.STATUS_NO_SUCH_ELEMENT;
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700643 }
644 }
645 openChannelResp = new IccOpenLogicalChannelResponse(
646 IccOpenLogicalChannelResponse.INVALID_CHANNEL, errorCode, null);
Shishir Agrawal566b7612013-10-28 14:41:00 -0700647 }
Shishir Agrawal82c8a462014-07-31 18:13:17 -0700648 request.result = openChannelResp;
Pengquan Menga1bb6272018-09-06 09:59:22 -0700649 notifyRequester(request);
Shishir Agrawal566b7612013-10-28 14:41:00 -0700650 break;
651
652 case CMD_CLOSE_CHANNEL:
653 request = (MainThreadRequest) msg.obj;
Shishir Agrawalc04d9752016-02-19 10:41:00 -0800654 uiccCard = getUiccCardFromRequest(request);
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700655 if (uiccCard == null) {
656 loge("iccCloseLogicalChannel: No UICC");
Yoshiaki Naka2e29d822016-09-02 19:27:39 +0900657 request.result = false;
Pengquan Menga1bb6272018-09-06 09:59:22 -0700658 notifyRequester(request);
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700659 } else {
660 onCompleted = obtainMessage(EVENT_CLOSE_CHANNEL_DONE, request);
661 uiccCard.iccCloseLogicalChannel((Integer) request.argument, onCompleted);
662 }
Shishir Agrawal566b7612013-10-28 14:41:00 -0700663 break;
664
665 case EVENT_CLOSE_CHANNEL_DONE:
Jake Hambye994d462014-02-03 13:10:13 -0800666 handleNullReturnEvent(msg, "iccCloseLogicalChannel");
667 break;
668
669 case CMD_NV_READ_ITEM:
670 request = (MainThreadRequest) msg.obj;
671 onCompleted = obtainMessage(EVENT_NV_READ_ITEM_DONE, request);
Nazanin Bakhshif782e562018-12-11 15:15:39 -0800672 defaultPhone.nvReadItem((Integer) request.argument, onCompleted,
673 request.workSource);
Jake Hambye994d462014-02-03 13:10:13 -0800674 break;
675
676 case EVENT_NV_READ_ITEM_DONE:
Shishir Agrawal566b7612013-10-28 14:41:00 -0700677 ar = (AsyncResult) msg.obj;
678 request = (MainThreadRequest) ar.userObj;
Jake Hambye994d462014-02-03 13:10:13 -0800679 if (ar.exception == null && ar.result != null) {
680 request.result = ar.result; // String
Shishir Agrawal566b7612013-10-28 14:41:00 -0700681 } else {
Jake Hambye994d462014-02-03 13:10:13 -0800682 request.result = "";
683 if (ar.result == null) {
684 loge("nvReadItem: Empty response");
685 } else if (ar.exception instanceof CommandException) {
686 loge("nvReadItem: CommandException: " +
687 ar.exception);
Shishir Agrawal566b7612013-10-28 14:41:00 -0700688 } else {
Jake Hambye994d462014-02-03 13:10:13 -0800689 loge("nvReadItem: Unknown exception");
Shishir Agrawal566b7612013-10-28 14:41:00 -0700690 }
691 }
Pengquan Menga1bb6272018-09-06 09:59:22 -0700692 notifyRequester(request);
Shishir Agrawal566b7612013-10-28 14:41:00 -0700693 break;
694
Jake Hambye994d462014-02-03 13:10:13 -0800695 case CMD_NV_WRITE_ITEM:
696 request = (MainThreadRequest) msg.obj;
697 onCompleted = obtainMessage(EVENT_NV_WRITE_ITEM_DONE, request);
698 Pair<Integer, String> idValue = (Pair<Integer, String>) request.argument;
Nazanin Bakhshif782e562018-12-11 15:15:39 -0800699 defaultPhone.nvWriteItem(idValue.first, idValue.second, onCompleted,
vagdeviaf9a5b92018-08-15 16:01:53 -0700700 request.workSource);
Jake Hambye994d462014-02-03 13:10:13 -0800701 break;
702
703 case EVENT_NV_WRITE_ITEM_DONE:
704 handleNullReturnEvent(msg, "nvWriteItem");
705 break;
706
707 case CMD_NV_WRITE_CDMA_PRL:
708 request = (MainThreadRequest) msg.obj;
709 onCompleted = obtainMessage(EVENT_NV_WRITE_CDMA_PRL_DONE, request);
Nazanin Bakhshif782e562018-12-11 15:15:39 -0800710 defaultPhone.nvWriteCdmaPrl((byte[]) request.argument, onCompleted);
Jake Hambye994d462014-02-03 13:10:13 -0800711 break;
712
713 case EVENT_NV_WRITE_CDMA_PRL_DONE:
714 handleNullReturnEvent(msg, "nvWriteCdmaPrl");
715 break;
716
chen xu6dac5ab2018-10-26 17:39:23 -0700717 case CMD_RESET_MODEM_CONFIG:
Jake Hambye994d462014-02-03 13:10:13 -0800718 request = (MainThreadRequest) msg.obj;
chen xu6dac5ab2018-10-26 17:39:23 -0700719 onCompleted = obtainMessage(EVENT_RESET_MODEM_CONFIG_DONE, request);
Nazanin Bakhshif782e562018-12-11 15:15:39 -0800720 defaultPhone.resetModemConfig(onCompleted);
Jake Hambye994d462014-02-03 13:10:13 -0800721 break;
722
chen xu6dac5ab2018-10-26 17:39:23 -0700723 case EVENT_RESET_MODEM_CONFIG_DONE:
724 handleNullReturnEvent(msg, "resetModemConfig");
Jake Hambye994d462014-02-03 13:10:13 -0800725 break;
726
Jake Hamby7c27be32014-03-03 13:25:59 -0800727 case CMD_GET_PREFERRED_NETWORK_TYPE:
728 request = (MainThreadRequest) msg.obj;
729 onCompleted = obtainMessage(EVENT_GET_PREFERRED_NETWORK_TYPE_DONE, request);
Stuart Scott54788802015-03-30 13:18:01 -0700730 getPhoneFromRequest(request).getPreferredNetworkType(onCompleted);
Jake Hamby7c27be32014-03-03 13:25:59 -0800731 break;
732
733 case EVENT_GET_PREFERRED_NETWORK_TYPE_DONE:
734 ar = (AsyncResult) msg.obj;
735 request = (MainThreadRequest) ar.userObj;
736 if (ar.exception == null && ar.result != null) {
737 request.result = ar.result; // Integer
738 } else {
Sanket Padawecfc2d352016-01-05 19:52:14 -0800739 request.result = null;
Jake Hamby7c27be32014-03-03 13:25:59 -0800740 if (ar.result == null) {
741 loge("getPreferredNetworkType: Empty response");
742 } else if (ar.exception instanceof CommandException) {
743 loge("getPreferredNetworkType: CommandException: " +
744 ar.exception);
745 } else {
746 loge("getPreferredNetworkType: Unknown exception");
747 }
748 }
Pengquan Menga1bb6272018-09-06 09:59:22 -0700749 notifyRequester(request);
Jake Hamby7c27be32014-03-03 13:25:59 -0800750 break;
751
752 case CMD_SET_PREFERRED_NETWORK_TYPE:
753 request = (MainThreadRequest) msg.obj;
754 onCompleted = obtainMessage(EVENT_SET_PREFERRED_NETWORK_TYPE_DONE, request);
755 int networkType = (Integer) request.argument;
Stuart Scott54788802015-03-30 13:18:01 -0700756 getPhoneFromRequest(request).setPreferredNetworkType(networkType, onCompleted);
Jake Hamby7c27be32014-03-03 13:25:59 -0800757 break;
758
759 case EVENT_SET_PREFERRED_NETWORK_TYPE_DONE:
760 handleNullReturnEvent(msg, "setPreferredNetworkType");
761 break;
762
Shuo Qian850e4d6a2018-04-25 21:02:08 +0000763 case CMD_INVOKE_OEM_RIL_REQUEST_RAW:
764 request = (MainThreadRequest)msg.obj;
765 onCompleted = obtainMessage(EVENT_INVOKE_OEM_RIL_REQUEST_RAW_DONE, request);
Nazanin Bakhshif782e562018-12-11 15:15:39 -0800766 defaultPhone.invokeOemRilRequestRaw((byte[]) request.argument, onCompleted);
Shuo Qian850e4d6a2018-04-25 21:02:08 +0000767 break;
768
769 case EVENT_INVOKE_OEM_RIL_REQUEST_RAW_DONE:
770 ar = (AsyncResult)msg.obj;
771 request = (MainThreadRequest)ar.userObj;
772 request.result = ar;
Pengquan Menga1bb6272018-09-06 09:59:22 -0700773 notifyRequester(request);
Shuo Qian850e4d6a2018-04-25 21:02:08 +0000774 break;
775
Shishir Agrawal76d5da92014-11-09 16:17:25 -0800776 case CMD_SET_VOICEMAIL_NUMBER:
777 request = (MainThreadRequest) msg.obj;
778 onCompleted = obtainMessage(EVENT_SET_VOICEMAIL_NUMBER_DONE, request);
779 Pair<String, String> tagNum = (Pair<String, String>) request.argument;
Stuart Scott584921c2015-01-15 17:10:34 -0800780 getPhoneFromRequest(request).setVoiceMailNumber(tagNum.first, tagNum.second,
781 onCompleted);
Shishir Agrawal76d5da92014-11-09 16:17:25 -0800782 break;
783
784 case EVENT_SET_VOICEMAIL_NUMBER_DONE:
785 handleNullReturnEvent(msg, "setVoicemailNumber");
786 break;
787
Stuart Scott54788802015-03-30 13:18:01 -0700788 case CMD_SET_NETWORK_SELECTION_MODE_AUTOMATIC:
789 request = (MainThreadRequest) msg.obj;
790 onCompleted = obtainMessage(EVENT_SET_NETWORK_SELECTION_MODE_AUTOMATIC_DONE,
791 request);
792 getPhoneFromRequest(request).setNetworkSelectionModeAutomatic(onCompleted);
793 break;
794
795 case EVENT_SET_NETWORK_SELECTION_MODE_AUTOMATIC_DONE:
796 handleNullReturnEvent(msg, "setNetworkSelectionModeAutomatic");
797 break;
798
Shishir Agrawal302c8692015-06-19 13:49:39 -0700799 case CMD_PERFORM_NETWORK_SCAN:
800 request = (MainThreadRequest) msg.obj;
801 onCompleted = obtainMessage(EVENT_PERFORM_NETWORK_SCAN_DONE, request);
802 getPhoneFromRequest(request).getAvailableNetworks(onCompleted);
803 break;
804
805 case EVENT_PERFORM_NETWORK_SCAN_DONE:
806 ar = (AsyncResult) msg.obj;
807 request = (MainThreadRequest) ar.userObj;
808 CellNetworkScanResult cellScanResult;
809 if (ar.exception == null && ar.result != null) {
810 cellScanResult = new CellNetworkScanResult(
811 CellNetworkScanResult.STATUS_SUCCESS,
812 (List<OperatorInfo>) ar.result);
813 } else {
814 if (ar.result == null) {
815 loge("getCellNetworkScanResults: Empty response");
816 }
817 if (ar.exception != null) {
818 loge("getCellNetworkScanResults: Exception: " + ar.exception);
819 }
820 int errorCode = CellNetworkScanResult.STATUS_UNKNOWN_ERROR;
821 if (ar.exception instanceof CommandException) {
822 CommandException.Error error =
823 ((CommandException) (ar.exception)).getCommandError();
824 if (error == CommandException.Error.RADIO_NOT_AVAILABLE) {
825 errorCode = CellNetworkScanResult.STATUS_RADIO_NOT_AVAILABLE;
826 } else if (error == CommandException.Error.GENERIC_FAILURE) {
827 errorCode = CellNetworkScanResult.STATUS_RADIO_GENERIC_FAILURE;
828 }
829 }
830 cellScanResult = new CellNetworkScanResult(errorCode, null);
831 }
832 request.result = cellScanResult;
Pengquan Menga1bb6272018-09-06 09:59:22 -0700833 notifyRequester(request);
Shishir Agrawal302c8692015-06-19 13:49:39 -0700834 break;
835
836 case CMD_SET_NETWORK_SELECTION_MODE_MANUAL:
837 request = (MainThreadRequest) msg.obj;
Shishir Agrawal77ba3172015-09-10 14:50:19 -0700838 ManualNetworkSelectionArgument selArg =
839 (ManualNetworkSelectionArgument) request.argument;
Shishir Agrawal302c8692015-06-19 13:49:39 -0700840 onCompleted = obtainMessage(EVENT_SET_NETWORK_SELECTION_MODE_MANUAL_DONE,
841 request);
Shishir Agrawal77ba3172015-09-10 14:50:19 -0700842 getPhoneFromRequest(request).selectNetworkManually(selArg.operatorInfo,
843 selArg.persistSelection, onCompleted);
Shishir Agrawal302c8692015-06-19 13:49:39 -0700844 break;
845
846 case EVENT_SET_NETWORK_SELECTION_MODE_MANUAL_DONE:
Pengquan Menge3d01e22018-09-20 15:25:35 -0700847 ar = (AsyncResult) msg.obj;
848 request = (MainThreadRequest) ar.userObj;
849 if (ar.exception == null) {
850 request.result = true;
851 } else {
852 request.result = false;
853 loge("setNetworkSelectionModeManual " + ar.exception);
854 }
855 notifyRequester(request);
856 mApp.onNetworkSelectionChanged(request.subId);
Shishir Agrawal302c8692015-06-19 13:49:39 -0700857 break;
858
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -0700859 case CMD_GET_MODEM_ACTIVITY_INFO:
860 request = (MainThreadRequest) msg.obj;
861 onCompleted = obtainMessage(EVENT_GET_MODEM_ACTIVITY_INFO_DONE, request);
James Mattisab947702019-04-03 14:18:34 -0700862 if (defaultPhone != null) {
863 defaultPhone.getModemActivityInfo(onCompleted, request.workSource);
864 }
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -0700865 break;
866
867 case EVENT_GET_MODEM_ACTIVITY_INFO_DONE:
868 ar = (AsyncResult) msg.obj;
869 request = (MainThreadRequest) ar.userObj;
870 if (ar.exception == null && ar.result != null) {
871 request.result = ar.result;
872 } else {
873 if (ar.result == null) {
874 loge("queryModemActivityInfo: Empty response");
875 } else if (ar.exception instanceof CommandException) {
876 loge("queryModemActivityInfo: CommandException: " +
877 ar.exception);
878 } else {
879 loge("queryModemActivityInfo: Unknown exception");
880 }
881 }
Amit Mahajand4766222016-01-28 15:28:28 -0800882 // Result cannot be null. Return ModemActivityInfo with all fields set to 0.
883 if (request.result == null) {
Chen Xu13851032019-09-10 18:49:52 -0700884 request.result = new ModemActivityInfo(0, 0, 0, null, 0);
Amit Mahajand4766222016-01-28 15:28:28 -0800885 }
Pengquan Menga1bb6272018-09-06 09:59:22 -0700886 notifyRequester(request);
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -0700887 break;
888
Meng Wang1a7c35a2016-05-05 20:56:15 -0700889 case CMD_SET_ALLOWED_CARRIERS:
890 request = (MainThreadRequest) msg.obj;
Michele Berionne482f8202018-11-27 18:57:59 -0800891 CarrierRestrictionRules argument =
892 (CarrierRestrictionRules) request.argument;
Meng Wang1a7c35a2016-05-05 20:56:15 -0700893 onCompleted = obtainMessage(EVENT_SET_ALLOWED_CARRIERS_DONE, request);
Michele Berionne482f8202018-11-27 18:57:59 -0800894 defaultPhone.setAllowedCarriers(argument, onCompleted, request.workSource);
Meng Wang1a7c35a2016-05-05 20:56:15 -0700895 break;
896
897 case EVENT_SET_ALLOWED_CARRIERS_DONE:
898 ar = (AsyncResult) msg.obj;
899 request = (MainThreadRequest) ar.userObj;
900 if (ar.exception == null && ar.result != null) {
901 request.result = ar.result;
902 } else {
Michele Berionne482f8202018-11-27 18:57:59 -0800903 request.result = TelephonyManager.SET_CARRIER_RESTRICTION_ERROR;
904 if (ar.exception instanceof CommandException) {
905 loge("setAllowedCarriers: CommandException: " + ar.exception);
906 CommandException.Error error =
907 ((CommandException) (ar.exception)).getCommandError();
908 if (error == CommandException.Error.REQUEST_NOT_SUPPORTED) {
909 request.result =
910 TelephonyManager.SET_CARRIER_RESTRICTION_NOT_SUPPORTED;
911 }
Meng Wang1a7c35a2016-05-05 20:56:15 -0700912 } else {
913 loge("setAllowedCarriers: Unknown exception");
914 }
915 }
Pengquan Menga1bb6272018-09-06 09:59:22 -0700916 notifyRequester(request);
Meng Wang1a7c35a2016-05-05 20:56:15 -0700917 break;
918
919 case CMD_GET_ALLOWED_CARRIERS:
920 request = (MainThreadRequest) msg.obj;
921 onCompleted = obtainMessage(EVENT_GET_ALLOWED_CARRIERS_DONE, request);
Nazanin Bakhshif782e562018-12-11 15:15:39 -0800922 defaultPhone.getAllowedCarriers(onCompleted, request.workSource);
Meng Wang1a7c35a2016-05-05 20:56:15 -0700923 break;
924
925 case EVENT_GET_ALLOWED_CARRIERS_DONE:
926 ar = (AsyncResult) msg.obj;
927 request = (MainThreadRequest) ar.userObj;
928 if (ar.exception == null && ar.result != null) {
929 request.result = ar.result;
930 } else {
Michele Berionne482f8202018-11-27 18:57:59 -0800931 request.result = new IllegalStateException(
932 "Failed to get carrier restrictions");
Meng Wang1a7c35a2016-05-05 20:56:15 -0700933 if (ar.result == null) {
934 loge("getAllowedCarriers: Empty response");
935 } else if (ar.exception instanceof CommandException) {
936 loge("getAllowedCarriers: CommandException: " +
937 ar.exception);
938 } else {
939 loge("getAllowedCarriers: Unknown exception");
940 }
941 }
Pengquan Menga1bb6272018-09-06 09:59:22 -0700942 notifyRequester(request);
Meng Wang1a7c35a2016-05-05 20:56:15 -0700943 break;
944
Nathan Haroldb3014052017-01-25 15:57:32 -0800945 case EVENT_GET_FORBIDDEN_PLMNS_DONE:
946 ar = (AsyncResult) msg.obj;
947 request = (MainThreadRequest) ar.userObj;
948 if (ar.exception == null && ar.result != null) {
949 request.result = ar.result;
950 } else {
951 request.result = new IllegalArgumentException(
952 "Failed to retrieve Forbidden Plmns");
953 if (ar.result == null) {
954 loge("getForbiddenPlmns: Empty response");
955 } else {
956 loge("getForbiddenPlmns: Unknown exception");
957 }
958 }
Pengquan Menga1bb6272018-09-06 09:59:22 -0700959 notifyRequester(request);
Nathan Haroldb3014052017-01-25 15:57:32 -0800960 break;
961
962 case CMD_GET_FORBIDDEN_PLMNS:
963 request = (MainThreadRequest) msg.obj;
964 uiccCard = getUiccCardFromRequest(request);
965 if (uiccCard == null) {
966 loge("getForbiddenPlmns() UiccCard is null");
967 request.result = new IllegalArgumentException(
968 "getForbiddenPlmns() UiccCard is null");
Pengquan Menga1bb6272018-09-06 09:59:22 -0700969 notifyRequester(request);
Nathan Haroldb3014052017-01-25 15:57:32 -0800970 break;
971 }
972 Integer appType = (Integer) request.argument;
973 UiccCardApplication uiccApp = uiccCard.getApplicationByType(appType);
974 if (uiccApp == null) {
975 loge("getForbiddenPlmns() no app with specified type -- "
976 + appType);
977 request.result = new IllegalArgumentException("Failed to get UICC App");
Pengquan Menga1bb6272018-09-06 09:59:22 -0700978 notifyRequester(request);
Nathan Haroldb3014052017-01-25 15:57:32 -0800979 break;
980 } else {
981 if (DBG) logv("getForbiddenPlmns() found app " + uiccApp.getAid()
982 + " specified type -- " + appType);
983 }
984 onCompleted = obtainMessage(EVENT_GET_FORBIDDEN_PLMNS_DONE, request);
985 ((SIMRecords) uiccApp.getIccRecords()).getForbiddenPlmns(
986 onCompleted);
987 break;
988
Holly Jiuyu Sun01c47ad2018-01-24 17:56:33 +0000989 case CMD_SWITCH_SLOTS:
990 request = (MainThreadRequest) msg.obj;
991 int[] physicalSlots = (int[]) request.argument;
992 onCompleted = obtainMessage(EVENT_SWITCH_SLOTS_DONE, request);
993 UiccController.getInstance().switchSlots(physicalSlots, onCompleted);
994 break;
995
996 case EVENT_SWITCH_SLOTS_DONE:
997 ar = (AsyncResult) msg.obj;
998 request = (MainThreadRequest) ar.userObj;
999 request.result = (ar.exception == null);
Pengquan Menga1bb6272018-09-06 09:59:22 -07001000 notifyRequester(request);
1001 break;
1002 case CMD_GET_NETWORK_SELECTION_MODE:
1003 request = (MainThreadRequest) msg.obj;
1004 onCompleted = obtainMessage(EVENT_GET_NETWORK_SELECTION_MODE_DONE, request);
1005 getPhoneFromRequest(request).getNetworkSelectionMode(onCompleted);
1006 break;
1007
1008 case EVENT_GET_NETWORK_SELECTION_MODE_DONE:
1009 ar = (AsyncResult) msg.obj;
1010 request = (MainThreadRequest) ar.userObj;
1011 if (ar.exception != null) {
1012 request.result = TelephonyManager.NETWORK_SELECTION_MODE_UNKNOWN;
1013 } else {
1014 int mode = ((int[]) ar.result)[0];
1015 if (mode == 0) {
1016 request.result = TelephonyManager.NETWORK_SELECTION_MODE_AUTO;
1017 } else {
1018 request.result = TelephonyManager.NETWORK_SELECTION_MODE_MANUAL;
1019 }
Holly Jiuyu Sun01c47ad2018-01-24 17:56:33 +00001020 }
Pengquan Menga1bb6272018-09-06 09:59:22 -07001021 notifyRequester(request);
1022 break;
1023 case CMD_GET_CDMA_ROAMING_MODE:
1024 request = (MainThreadRequest) msg.obj;
1025 onCompleted = obtainMessage(EVENT_GET_CDMA_ROAMING_MODE_DONE, request);
1026 getPhoneFromRequest(request).queryCdmaRoamingPreference(onCompleted);
1027 break;
1028 case EVENT_GET_CDMA_ROAMING_MODE_DONE:
1029 ar = (AsyncResult) msg.obj;
1030 request = (MainThreadRequest) ar.userObj;
1031 if (ar.exception != null) {
1032 request.result = TelephonyManager.CDMA_ROAMING_MODE_RADIO_DEFAULT;
1033 } else {
1034 request.result = ((int[]) ar.result)[0];
1035 }
1036 notifyRequester(request);
1037 break;
1038 case CMD_SET_CDMA_ROAMING_MODE:
1039 request = (MainThreadRequest) msg.obj;
1040 onCompleted = obtainMessage(EVENT_SET_CDMA_ROAMING_MODE_DONE, request);
1041 int mode = (int) request.argument;
1042 getPhoneFromRequest(request).setCdmaRoamingPreference(mode, onCompleted);
1043 break;
1044 case EVENT_SET_CDMA_ROAMING_MODE_DONE:
1045 ar = (AsyncResult) msg.obj;
1046 request = (MainThreadRequest) ar.userObj;
1047 request.result = ar.exception == null;
1048 notifyRequester(request);
1049 break;
1050 case CMD_SET_CDMA_SUBSCRIPTION_MODE:
1051 request = (MainThreadRequest) msg.obj;
1052 onCompleted = obtainMessage(EVENT_SET_CDMA_SUBSCRIPTION_MODE_DONE, request);
1053 int subscriptionMode = (int) request.argument;
1054 getPhoneFromRequest(request).setCdmaSubscription(subscriptionMode, onCompleted);
1055 break;
1056 case EVENT_SET_CDMA_SUBSCRIPTION_MODE_DONE:
1057 ar = (AsyncResult) msg.obj;
1058 request = (MainThreadRequest) ar.userObj;
1059 request.result = ar.exception == null;
1060 notifyRequester(request);
Holly Jiuyu Sun01c47ad2018-01-24 17:56:33 +00001061 break;
Nathan Harold3ff88932018-08-14 10:19:49 -07001062 case CMD_GET_ALL_CELL_INFO:
1063 request = (MainThreadRequest) msg.obj;
Nathan Harold3ff88932018-08-14 10:19:49 -07001064 onCompleted = obtainMessage(EVENT_GET_ALL_CELL_INFO_DONE, request);
Nathan Harold92bed182018-10-12 18:16:49 -07001065 request.phone.requestCellInfoUpdate(request.workSource, onCompleted);
Nathan Harold3ff88932018-08-14 10:19:49 -07001066 break;
Nathan Harold3ff88932018-08-14 10:19:49 -07001067 case EVENT_GET_ALL_CELL_INFO_DONE:
1068 ar = (AsyncResult) msg.obj;
1069 request = (MainThreadRequest) ar.userObj;
Nathan Harold8d0f1742018-10-02 12:14:47 -07001070 // If a timeout occurs, the response will be null
1071 request.result = (ar.exception == null && ar.result != null)
1072 ? ar.result : new ArrayList<CellInfo>();
Nathan Harold3ff88932018-08-14 10:19:49 -07001073 synchronized (request) {
1074 request.notifyAll();
1075 }
1076 break;
Nathan Haroldfa8da0f2018-09-27 18:51:29 -07001077 case CMD_REQUEST_CELL_INFO_UPDATE:
1078 request = (MainThreadRequest) msg.obj;
1079 request.phone.requestCellInfoUpdate(request.workSource,
1080 obtainMessage(EVENT_REQUEST_CELL_INFO_UPDATE_DONE, request));
1081 break;
1082 case EVENT_REQUEST_CELL_INFO_UPDATE_DONE:
1083 ar = (AsyncResult) msg.obj;
1084 request = (MainThreadRequest) ar.userObj;
1085 ICellInfoCallback cb = (ICellInfoCallback) request.argument;
1086 try {
1087 if (ar.exception != null) {
Nathan Haroldfa8da0f2018-09-27 18:51:29 -07001088 Log.e(LOG_TAG, "Exception retrieving CellInfo=" + ar.exception);
Meng Wang6c08ecd2019-09-30 17:13:54 -07001089 cb.onError(
1090 TelephonyManager.CellInfoCallback.ERROR_MODEM_ERROR,
1091 ar.exception.getClass().getName(),
1092 ar.exception.toString());
Nathan Haroldfa8da0f2018-09-27 18:51:29 -07001093 } else if (ar.result == null) {
Nathan Haroldfa8da0f2018-09-27 18:51:29 -07001094 Log.w(LOG_TAG, "Timeout Waiting for CellInfo!");
Meng Wang6c08ecd2019-09-30 17:13:54 -07001095 cb.onError(TelephonyManager.CellInfoCallback.ERROR_TIMEOUT, null, null);
Nathan Haroldfa8da0f2018-09-27 18:51:29 -07001096 } else {
1097 // use the result as returned
1098 cb.onCellInfo((List<CellInfo>) ar.result);
1099 }
1100 } catch (RemoteException re) {
1101 Log.w(LOG_TAG, "Discarded CellInfo due to Callback RemoteException");
1102 }
1103 break;
1104 case CMD_GET_CELL_LOCATION:
Nathan Harold3ff88932018-08-14 10:19:49 -07001105 request = (MainThreadRequest) msg.obj;
1106 WorkSource ws = (WorkSource) request.argument;
1107 Phone phone = getPhoneFromRequest(request);
Meng Wangd64acad2019-12-09 13:13:01 -08001108 phone.getCellIdentity(ws, obtainMessage(EVENT_GET_CELL_LOCATION_DONE, request));
Nathan Harold3ff88932018-08-14 10:19:49 -07001109 break;
Nathan Haroldfa8da0f2018-09-27 18:51:29 -07001110 case EVENT_GET_CELL_LOCATION_DONE:
Nathan Harold3ff88932018-08-14 10:19:49 -07001111 ar = (AsyncResult) msg.obj;
1112 request = (MainThreadRequest) ar.userObj;
1113 if (ar.exception == null) {
1114 request.result = ar.result;
1115 } else {
Nathan Haroldfa8da0f2018-09-27 18:51:29 -07001116 phone = getPhoneFromRequest(request);
Nathan Harold3ff88932018-08-14 10:19:49 -07001117 request.result = (phone.getPhoneType() == PhoneConstants.PHONE_TYPE_CDMA)
Meng Wangd64acad2019-12-09 13:13:01 -08001118 ? new CellIdentityCdma() : new CellIdentityGsm();
Nathan Harold3ff88932018-08-14 10:19:49 -07001119 }
1120
1121 synchronized (request) {
1122 request.notifyAll();
1123 }
1124 break;
chen xu6dac5ab2018-10-26 17:39:23 -07001125 case CMD_MODEM_REBOOT:
1126 request = (MainThreadRequest) msg.obj;
1127 onCompleted = obtainMessage(EVENT_RESET_MODEM_CONFIG_DONE, request);
Nazanin Bakhshif782e562018-12-11 15:15:39 -08001128 defaultPhone.rebootModem(onCompleted);
chen xu6dac5ab2018-10-26 17:39:23 -07001129 break;
chen xu6dac5ab2018-10-26 17:39:23 -07001130 case EVENT_CMD_MODEM_REBOOT_DONE:
1131 handleNullReturnEvent(msg, "rebootModem");
1132 break;
Malcolm Chen8e4ed912019-01-15 20:22:16 -08001133 case CMD_REQUEST_ENABLE_MODEM:
1134 request = (MainThreadRequest) msg.obj;
1135 boolean enable = (boolean) request.argument;
1136 onCompleted = obtainMessage(EVENT_ENABLE_MODEM_DONE, request);
Nazanin Bakhshi33d584b2019-02-27 10:44:32 -08001137 onCompleted.arg1 = enable ? 1 : 0;
Malcolm Chen8e4ed912019-01-15 20:22:16 -08001138 PhoneConfigurationManager.getInstance()
1139 .enablePhone(request.phone, enable, onCompleted);
1140 break;
1141 case EVENT_ENABLE_MODEM_DONE:
1142 ar = (AsyncResult) msg.obj;
1143 request = (MainThreadRequest) ar.userObj;
1144 request.result = (ar.exception == null);
Nazanin Bakhshif71371d2019-04-29 17:29:44 -07001145 int phoneId = request.phone.getPhoneId();
Nazanin Bakhshi33d584b2019-02-27 10:44:32 -08001146 //update the cache as modem status has changed
Nazanin Bakhshif71371d2019-04-29 17:29:44 -07001147 if ((boolean) request.result) {
1148 mPhoneConfigurationManager.addToPhoneStatusCache(phoneId, msg.arg1 == 1);
1149 updateModemStateMetrics();
1150 } else {
1151 Log.e(LOG_TAG, msg.what + " failure. Not updating modem status."
1152 + ar.exception);
1153 }
1154 notifyRequester(request);
1155 break;
1156 case CMD_GET_MODEM_STATUS:
1157 request = (MainThreadRequest) msg.obj;
1158 onCompleted = obtainMessage(EVENT_GET_MODEM_STATUS_DONE, request);
1159 PhoneConfigurationManager.getInstance()
1160 .getPhoneStatusFromModem(request.phone, onCompleted);
1161 break;
1162 case EVENT_GET_MODEM_STATUS_DONE:
1163 ar = (AsyncResult) msg.obj;
1164 request = (MainThreadRequest) ar.userObj;
1165 int id = request.phone.getPhoneId();
1166 if (ar.exception == null && ar.result != null) {
1167 request.result = ar.result;
1168 //update the cache as modem status has changed
1169 mPhoneConfigurationManager.addToPhoneStatusCache(id,
1170 (boolean) request.result);
1171 } else {
1172 // Return true if modem status cannot be retrieved. For most cases,
1173 // modem status is on. And for older version modems, GET_MODEM_STATUS
1174 // and disable modem are not supported. Modem is always on.
1175 // TODO: this should be fixed in R to support a third
1176 // status UNKNOWN b/131631629
1177 request.result = true;
1178 Log.e(LOG_TAG, msg.what + " failure. Not updating modem status."
1179 + ar.exception);
1180 }
Malcolm Chen8e4ed912019-01-15 20:22:16 -08001181 notifyRequester(request);
1182 break;
yincheng zhaod698b842019-09-06 17:06:54 -07001183 case EVENT_SET_FORBIDDEN_PLMNS_DONE:
1184 ar = (AsyncResult) msg.obj;
1185 request = (MainThreadRequest) ar.userObj;
1186 if (ar.exception == null && ar.result != null) {
1187 request.result = ar.result;
1188 } else {
1189 request.result = -1;
1190 loge("Failed to set Forbidden Plmns");
1191 if (ar.result == null) {
1192 loge("setForbidenPlmns: Empty response");
1193 } else if (ar.exception != null) {
1194 loge("setForbiddenPlmns: Exception: " + ar.exception);
1195 request.result = -1;
1196 } else {
1197 loge("setForbiddenPlmns: Unknown exception");
1198 }
1199 }
1200 notifyRequester(request);
1201 break;
1202 case CMD_SET_FORBIDDEN_PLMNS:
1203 request = (MainThreadRequest) msg.obj;
1204 uiccCard = getUiccCardFromRequest(request);
1205 if (uiccCard == null) {
1206 loge("setForbiddenPlmns: UiccCard is null");
1207 request.result = -1;
1208 notifyRequester(request);
1209 break;
1210 }
1211 Pair<Integer, List<String>> setFplmnsArgs =
1212 (Pair<Integer, List<String>>) request.argument;
1213 appType = setFplmnsArgs.first;
1214 List<String> fplmns = setFplmnsArgs.second;
1215 uiccApp = uiccCard.getApplicationByType(appType);
1216 if (uiccApp == null) {
1217 loge("setForbiddenPlmns: no app with specified type -- " + appType);
1218 request.result = -1;
1219 loge("Failed to get UICC App");
1220 notifyRequester(request);
1221 } else {
1222 onCompleted = obtainMessage(EVENT_SET_FORBIDDEN_PLMNS_DONE, request);
1223 ((SIMRecords) uiccApp.getIccRecords())
1224 .setForbiddenPlmns(onCompleted, fplmns);
1225 }
yinchengzhao4d163c02019-12-12 15:21:47 -08001226 break;
Naina Nalluri8ff344d2019-09-17 14:10:30 -07001227 case CMD_ERASE_MODEM_CONFIG:
1228 request = (MainThreadRequest) msg.obj;
1229 onCompleted = obtainMessage(EVENT_ERASE_MODEM_CONFIG_DONE, request);
1230 defaultPhone.eraseModemConfig(onCompleted);
1231 break;
1232 case EVENT_ERASE_MODEM_CONFIG_DONE:
1233 handleNullReturnEvent(msg, "eraseModemConfig");
yincheng zhaod698b842019-09-06 17:06:54 -07001234 break;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001235 default:
1236 Log.w(LOG_TAG, "MainThreadHandler: unexpected message code: " + msg.what);
1237 break;
1238 }
1239 }
Jake Hambye994d462014-02-03 13:10:13 -08001240
Pengquan Menga1bb6272018-09-06 09:59:22 -07001241 private void notifyRequester(MainThreadRequest request) {
1242 synchronized (request) {
1243 request.notifyAll();
1244 }
1245 }
1246
Jake Hambye994d462014-02-03 13:10:13 -08001247 private void handleNullReturnEvent(Message msg, String command) {
1248 AsyncResult ar = (AsyncResult) msg.obj;
1249 MainThreadRequest request = (MainThreadRequest) ar.userObj;
1250 if (ar.exception == null) {
1251 request.result = true;
1252 } else {
1253 request.result = false;
1254 if (ar.exception instanceof CommandException) {
1255 loge(command + ": CommandException: " + ar.exception);
1256 } else {
1257 loge(command + ": Unknown exception");
1258 }
1259 }
Pengquan Menga1bb6272018-09-06 09:59:22 -07001260 notifyRequester(request);
Jake Hambye994d462014-02-03 13:10:13 -08001261 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001262 }
1263
1264 /**
1265 * Posts the specified command to be executed on the main thread,
1266 * waits for the request to complete, and returns the result.
1267 * @see #sendRequestAsync
1268 */
1269 private Object sendRequest(int command, Object argument) {
Nathan Harold92bed182018-10-12 18:16:49 -07001270 return sendRequest(
1271 command, argument, SubscriptionManager.INVALID_SUBSCRIPTION_ID, null, null);
vagdeviaf9a5b92018-08-15 16:01:53 -07001272 }
1273
1274 /**
1275 * Posts the specified command to be executed on the main thread,
1276 * waits for the request to complete, and returns the result.
1277 * @see #sendRequestAsync
1278 */
1279 private Object sendRequest(int command, Object argument, WorkSource workSource) {
1280 return sendRequest(command, argument, SubscriptionManager.INVALID_SUBSCRIPTION_ID,
Nathan Harold92bed182018-10-12 18:16:49 -07001281 null, workSource);
Wink Saville36469e72014-06-11 15:17:00 -07001282 }
1283
1284 /**
1285 * Posts the specified command to be executed on the main thread,
1286 * waits for the request to complete, and returns the result.
1287 * @see #sendRequestAsync
1288 */
Shishir Agrawal76d5da92014-11-09 16:17:25 -08001289 private Object sendRequest(int command, Object argument, Integer subId) {
Nathan Harold92bed182018-10-12 18:16:49 -07001290 return sendRequest(command, argument, subId, null, null);
vagdeviaf9a5b92018-08-15 16:01:53 -07001291 }
1292
1293 /**
1294 * Posts the specified command to be executed on the main thread,
1295 * waits for the request to complete, and returns the result.
1296 * @see #sendRequestAsync
1297 */
Nathan Harold92bed182018-10-12 18:16:49 -07001298 private Object sendRequest(int command, Object argument, int subId, WorkSource workSource) {
1299 return sendRequest(command, argument, subId, null, workSource);
1300 }
1301
1302 /**
1303 * Posts the specified command to be executed on the main thread,
1304 * waits for the request to complete, and returns the result.
1305 * @see #sendRequestAsync
1306 */
1307 private Object sendRequest(int command, Object argument, Phone phone, WorkSource workSource) {
1308 return sendRequest(
1309 command, argument, SubscriptionManager.INVALID_SUBSCRIPTION_ID, phone, workSource);
1310 }
1311
1312 /**
1313 * Posts the specified command to be executed on the main thread,
1314 * waits for the request to complete, and returns the result.
1315 * @see #sendRequestAsync
1316 */
1317 private Object sendRequest(
1318 int command, Object argument, Integer subId, Phone phone, WorkSource workSource) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001319 if (Looper.myLooper() == mMainThreadHandler.getLooper()) {
1320 throw new RuntimeException("This method will deadlock if called from the main thread.");
1321 }
1322
Nathan Harold92bed182018-10-12 18:16:49 -07001323 MainThreadRequest request = null;
1324 if (subId != SubscriptionManager.INVALID_SUBSCRIPTION_ID && phone != null) {
1325 throw new IllegalArgumentException("subId and phone cannot both be specified!");
1326 } else if (phone != null) {
1327 request = new MainThreadRequest(argument, phone, workSource);
1328 } else {
1329 request = new MainThreadRequest(argument, subId, workSource);
1330 }
1331
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001332 Message msg = mMainThreadHandler.obtainMessage(command, request);
1333 msg.sendToTarget();
1334
1335 // Wait for the request to complete
1336 synchronized (request) {
1337 while (request.result == null) {
1338 try {
1339 request.wait();
1340 } catch (InterruptedException e) {
1341 // Do nothing, go back and wait until the request is complete
1342 }
1343 }
1344 }
1345 return request.result;
1346 }
1347
1348 /**
1349 * Asynchronous ("fire and forget") version of sendRequest():
1350 * Posts the specified command to be executed on the main thread, and
1351 * returns immediately.
1352 * @see #sendRequest
1353 */
1354 private void sendRequestAsync(int command) {
1355 mMainThreadHandler.sendEmptyMessage(command);
1356 }
1357
1358 /**
Sailesh Nepalbd76e4e2013-10-27 13:59:44 -07001359 * Same as {@link #sendRequestAsync(int)} except it takes an argument.
Nathan Haroldfa8da0f2018-09-27 18:51:29 -07001360 * @see {@link #sendRequest(int)}
Sailesh Nepalbd76e4e2013-10-27 13:59:44 -07001361 */
1362 private void sendRequestAsync(int command, Object argument) {
Nathan Haroldfa8da0f2018-09-27 18:51:29 -07001363 sendRequestAsync(command, argument, null, null);
1364 }
1365
1366 /**
1367 * Same as {@link #sendRequestAsync(int,Object)} except it takes a Phone and WorkSource.
1368 * @see {@link #sendRequest(int,Object)}
1369 */
1370 private void sendRequestAsync(
1371 int command, Object argument, Phone phone, WorkSource workSource) {
1372 MainThreadRequest request = new MainThreadRequest(argument, phone, workSource);
Sailesh Nepalbd76e4e2013-10-27 13:59:44 -07001373 Message msg = mMainThreadHandler.obtainMessage(command, request);
1374 msg.sendToTarget();
1375 }
1376
1377 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001378 * Initialize the singleton PhoneInterfaceManager instance.
1379 * This is only done once, at startup, from PhoneApp.onCreate().
1380 */
Nazanin Bakhshif782e562018-12-11 15:15:39 -08001381 /* package */ static PhoneInterfaceManager init(PhoneGlobals app) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001382 synchronized (PhoneInterfaceManager.class) {
1383 if (sInstance == null) {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08001384 sInstance = new PhoneInterfaceManager(app);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001385 } else {
1386 Log.wtf(LOG_TAG, "init() called multiple times! sInstance = " + sInstance);
1387 }
1388 return sInstance;
1389 }
1390 }
1391
1392 /** Private constructor; @see init() */
Nazanin Bakhshif782e562018-12-11 15:15:39 -08001393 private PhoneInterfaceManager(PhoneGlobals app) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001394 mApp = app;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001395 mCM = PhoneGlobals.getInstance().mCM;
Brad Ebinger05f52c22019-12-05 13:03:21 -08001396 mImsResolver = PhoneGlobals.getInstance().getImsResolver();
Stuart Scott981d8582015-04-21 14:09:50 -07001397 mUserManager = (UserManager) app.getSystemService(Context.USER_SERVICE);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001398 mAppOps = (AppOpsManager)app.getSystemService(Context.APP_OPS_SERVICE);
1399 mMainThreadHandler = new MainThreadHandler();
Tobias Thiererb19e1f12018-12-11 17:54:03 +00001400 mSubscriptionController = SubscriptionController.getInstance();
Nazanin Bakhshif782e562018-12-11 15:15:39 -08001401 mTelephonySharedPreferences =
1402 PreferenceManager.getDefaultSharedPreferences(mApp);
yinxub1bed742017-04-17 11:45:04 -07001403 mNetworkScanRequestTracker = new NetworkScanRequestTracker();
Malcolm Chen2c63d402018-08-14 16:00:53 -07001404 mPhoneConfigurationManager = PhoneConfigurationManager.getInstance();
Wink Saville3ab207e2014-11-20 13:07:20 -08001405
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001406 publish();
1407 }
1408
Nazanin Bakhshif782e562018-12-11 15:15:39 -08001409 private Phone getDefaultPhone() {
1410 Phone thePhone = getPhone(getDefaultSubscription());
1411 return (thePhone != null) ? thePhone : PhoneFactory.getDefaultPhone();
1412 }
1413
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001414 private void publish() {
1415 if (DBG) log("publish: " + this);
1416
1417 ServiceManager.addService("phone", this);
1418 }
1419
Stuart Scott584921c2015-01-15 17:10:34 -08001420 private Phone getPhoneFromRequest(MainThreadRequest request) {
Jordan Liu4c733742019-02-28 12:03:40 -08001421 if (request.phone != null) {
1422 return request.phone;
1423 } else {
1424 return getPhoneFromSubId(request.subId);
1425 }
1426 }
1427
1428 private Phone getPhoneFromSubId(int subId) {
1429 return (subId == SubscriptionManager.INVALID_SUBSCRIPTION_ID)
1430 ? getDefaultPhone() : getPhone(subId);
Stuart Scott584921c2015-01-15 17:10:34 -08001431 }
1432
Shishir Agrawalc04d9752016-02-19 10:41:00 -08001433 private UiccCard getUiccCardFromRequest(MainThreadRequest request) {
1434 Phone phone = getPhoneFromRequest(request);
1435 return phone == null ? null :
1436 UiccController.getInstance().getUiccCard(phone.getPhoneId());
1437 }
1438
Wink Saville36469e72014-06-11 15:17:00 -07001439 // returns phone associated with the subId.
Wink Savilleb564aae2014-10-23 10:18:09 -07001440 private Phone getPhone(int subId) {
Wink Savilleac1bdfd2014-11-20 23:04:44 -08001441 return PhoneFactory.getPhone(mSubscriptionController.getPhoneId(subId));
Wink Saville36469e72014-06-11 15:17:00 -07001442 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001443
Naina Nalluri8ff344d2019-09-17 14:10:30 -07001444 private void sendEraseModemConfig(Phone phone) {
1445 if (phone != null) {
1446 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
1447 mApp, phone.getSubId(), "eraseModemConfig");
1448 final long identity = Binder.clearCallingIdentity();
1449 try {
1450 Boolean success = (Boolean) sendRequest(CMD_ERASE_MODEM_CONFIG, null);
1451 if (DBG) log("eraseModemConfig:" + ' ' + (success ? "ok" : "fail"));
1452 } finally {
1453 Binder.restoreCallingIdentity(identity);
1454 }
1455 }
1456 }
1457
Peter Wang050bb052020-01-13 23:33:09 -08001458 private boolean isImsAvailableOnDevice() {
1459 PackageManager pm = getDefaultPhone().getContext().getPackageManager();
1460 if (pm == null) {
1461 // For some reason package manger is not available.. This will fail internally anyway,
1462 // so do not throw error and allow.
1463 return true;
1464 }
1465 return pm.hasSystemFeature(PackageManager.FEATURE_TELEPHONY_IMS, 0);
1466 }
1467
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001468 public void dial(String number) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001469 dialForSubscriber(getPreferredVoiceSubscription(), number);
Wink Saville36469e72014-06-11 15:17:00 -07001470 }
1471
Wink Savilleb564aae2014-10-23 10:18:09 -07001472 public void dialForSubscriber(int subId, String number) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001473 if (DBG) log("dial: " + number);
1474 // No permission check needed here: This is just a wrapper around the
1475 // ACTION_DIAL intent, which is available to any app since it puts up
1476 // the UI before it does anything.
1477
Malcolm Chenaa4a8532018-02-28 15:00:40 -08001478 final long identity = Binder.clearCallingIdentity();
1479 try {
1480 String url = createTelUrl(number);
1481 if (url == null) {
1482 return;
1483 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001484
Malcolm Chenaa4a8532018-02-28 15:00:40 -08001485 // PENDING: should we just silently fail if phone is offhook or ringing?
1486 PhoneConstants.State state = mCM.getState(subId);
1487 if (state != PhoneConstants.State.OFFHOOK && state != PhoneConstants.State.RINGING) {
1488 Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse(url));
1489 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
1490 mApp.startActivity(intent);
1491 }
1492 } finally {
1493 Binder.restoreCallingIdentity(identity);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001494 }
1495 }
1496
1497 public void call(String callingPackage, String number) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001498 callForSubscriber(getPreferredVoiceSubscription(), callingPackage, number);
Wink Saville36469e72014-06-11 15:17:00 -07001499 }
1500
Wink Savilleb564aae2014-10-23 10:18:09 -07001501 public void callForSubscriber(int subId, String callingPackage, String number) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001502 if (DBG) log("call: " + number);
1503
1504 // This is just a wrapper around the ACTION_CALL intent, but we still
1505 // need to do a permission check since we're calling startActivity()
1506 // from the context of the phone app.
1507 enforceCallPermission();
1508
Jordan Liu1617b712019-07-10 15:06:26 -07001509 if (mAppOps.noteOp(AppOpsManager.OPSTR_CALL_PHONE, Binder.getCallingUid(), callingPackage)
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001510 != AppOpsManager.MODE_ALLOWED) {
1511 return;
1512 }
1513
Malcolm Chenaa4a8532018-02-28 15:00:40 -08001514 final long identity = Binder.clearCallingIdentity();
1515 try {
1516 String url = createTelUrl(number);
1517 if (url == null) {
1518 return;
1519 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001520
Malcolm Chenaa4a8532018-02-28 15:00:40 -08001521 boolean isValid = false;
1522 final List<SubscriptionInfo> slist = getActiveSubscriptionInfoListPrivileged();
1523 if (slist != null) {
1524 for (SubscriptionInfo subInfoRecord : slist) {
1525 if (subInfoRecord.getSubscriptionId() == subId) {
1526 isValid = true;
1527 break;
1528 }
Wink Saville3ab207e2014-11-20 13:07:20 -08001529 }
Wink Saville08874612014-08-31 19:19:58 -07001530 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08001531 if (!isValid) {
1532 return;
1533 }
Wink Saville08874612014-08-31 19:19:58 -07001534
Malcolm Chenaa4a8532018-02-28 15:00:40 -08001535 Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse(url));
1536 intent.putExtra(SUBSCRIPTION_KEY, subId);
1537 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
1538 mApp.startActivity(intent);
1539 } finally {
1540 Binder.restoreCallingIdentity(identity);
1541 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001542 }
1543
Wink Savilleb564aae2014-10-23 10:18:09 -07001544 public boolean supplyPinForSubscriber(int subId, String pin) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001545 int [] resultArray = supplyPinReportResultForSubscriber(subId, pin);
Wink Saville9de0f752013-10-22 19:04:03 -07001546 return (resultArray[0] == PhoneConstants.PIN_RESULT_SUCCESS) ? true : false;
1547 }
1548
Wink Savilleb564aae2014-10-23 10:18:09 -07001549 public boolean supplyPukForSubscriber(int subId, String puk, String pin) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001550 int [] resultArray = supplyPukReportResultForSubscriber(subId, puk, pin);
Wink Saville9de0f752013-10-22 19:04:03 -07001551 return (resultArray[0] == PhoneConstants.PIN_RESULT_SUCCESS) ? true : false;
1552 }
1553
Wink Savilleb564aae2014-10-23 10:18:09 -07001554 public int[] supplyPinReportResultForSubscriber(int subId, String pin) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001555 enforceModifyPermission();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08001556
1557 final long identity = Binder.clearCallingIdentity();
1558 try {
1559 final UnlockSim checkSimPin = new UnlockSim(getPhone(subId).getIccCard());
1560 checkSimPin.start();
1561 return checkSimPin.unlockSim(null, pin);
1562 } finally {
1563 Binder.restoreCallingIdentity(identity);
1564 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001565 }
1566
Wink Savilleb564aae2014-10-23 10:18:09 -07001567 public int[] supplyPukReportResultForSubscriber(int subId, String puk, String pin) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001568 enforceModifyPermission();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08001569
1570 final long identity = Binder.clearCallingIdentity();
1571 try {
1572 final UnlockSim checkSimPuk = new UnlockSim(getPhone(subId).getIccCard());
1573 checkSimPuk.start();
1574 return checkSimPuk.unlockSim(puk, pin);
1575 } finally {
1576 Binder.restoreCallingIdentity(identity);
1577 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001578 }
1579
1580 /**
Wink Saville9de0f752013-10-22 19:04:03 -07001581 * Helper thread to turn async call to SimCard#supplyPin into
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001582 * a synchronous one.
1583 */
1584 private static class UnlockSim extends Thread {
1585
1586 private final IccCard mSimCard;
1587
1588 private boolean mDone = false;
Wink Saville9de0f752013-10-22 19:04:03 -07001589 private int mResult = PhoneConstants.PIN_GENERAL_FAILURE;
1590 private int mRetryCount = -1;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001591
1592 // For replies from SimCard interface
1593 private Handler mHandler;
1594
1595 // For async handler to identify request type
1596 private static final int SUPPLY_PIN_COMPLETE = 100;
1597
1598 public UnlockSim(IccCard simCard) {
1599 mSimCard = simCard;
1600 }
1601
1602 @Override
1603 public void run() {
1604 Looper.prepare();
1605 synchronized (UnlockSim.this) {
1606 mHandler = new Handler() {
1607 @Override
1608 public void handleMessage(Message msg) {
1609 AsyncResult ar = (AsyncResult) msg.obj;
1610 switch (msg.what) {
1611 case SUPPLY_PIN_COMPLETE:
1612 Log.d(LOG_TAG, "SUPPLY_PIN_COMPLETE");
1613 synchronized (UnlockSim.this) {
Wink Saville9de0f752013-10-22 19:04:03 -07001614 mRetryCount = msg.arg1;
1615 if (ar.exception != null) {
1616 if (ar.exception instanceof CommandException &&
1617 ((CommandException)(ar.exception)).getCommandError()
1618 == CommandException.Error.PASSWORD_INCORRECT) {
1619 mResult = PhoneConstants.PIN_PASSWORD_INCORRECT;
1620 } else {
1621 mResult = PhoneConstants.PIN_GENERAL_FAILURE;
1622 }
1623 } else {
1624 mResult = PhoneConstants.PIN_RESULT_SUCCESS;
1625 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001626 mDone = true;
1627 UnlockSim.this.notifyAll();
1628 }
1629 break;
1630 }
1631 }
1632 };
1633 UnlockSim.this.notifyAll();
1634 }
1635 Looper.loop();
1636 }
1637
1638 /*
1639 * Use PIN or PUK to unlock SIM card
1640 *
1641 * If PUK is null, unlock SIM card with PIN
1642 *
1643 * If PUK is not null, unlock SIM card with PUK and set PIN code
1644 */
Wink Saville9de0f752013-10-22 19:04:03 -07001645 synchronized int[] unlockSim(String puk, String pin) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001646
1647 while (mHandler == null) {
1648 try {
1649 wait();
1650 } catch (InterruptedException e) {
1651 Thread.currentThread().interrupt();
1652 }
1653 }
1654 Message callback = Message.obtain(mHandler, SUPPLY_PIN_COMPLETE);
1655
1656 if (puk == null) {
1657 mSimCard.supplyPin(pin, callback);
1658 } else {
1659 mSimCard.supplyPuk(puk, pin, callback);
1660 }
1661
1662 while (!mDone) {
1663 try {
1664 Log.d(LOG_TAG, "wait for done");
1665 wait();
1666 } catch (InterruptedException e) {
1667 // Restore the interrupted status
1668 Thread.currentThread().interrupt();
1669 }
1670 }
1671 Log.d(LOG_TAG, "done");
Wink Saville9de0f752013-10-22 19:04:03 -07001672 int[] resultArray = new int[2];
1673 resultArray[0] = mResult;
1674 resultArray[1] = mRetryCount;
1675 return resultArray;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001676 }
1677 }
1678
1679 public void updateServiceLocation() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001680 updateServiceLocationForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001681
1682 }
1683
Wink Savilleb564aae2014-10-23 10:18:09 -07001684 public void updateServiceLocationForSubscriber(int subId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001685 // No permission check needed here: this call is harmless, and it's
1686 // needed for the ServiceState.requestStateUpdate() call (which is
1687 // already intentionally exposed to 3rd parties.)
Malcolm Chenaa4a8532018-02-28 15:00:40 -08001688 final long identity = Binder.clearCallingIdentity();
1689 try {
1690 final Phone phone = getPhone(subId);
1691 if (phone != null) {
1692 phone.updateServiceLocation();
1693 }
1694 } finally {
1695 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07001696 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001697 }
1698
Philip P. Moltmann9797cbb2020-01-07 13:45:00 -08001699 @Deprecated
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001700 @Override
1701 public boolean isRadioOn(String callingPackage) {
Philip P. Moltmann9797cbb2020-01-07 13:45:00 -08001702 return isRadioOnWithFeature(callingPackage, null);
Wink Saville36469e72014-06-11 15:17:00 -07001703 }
1704
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001705 @Override
Philip P. Moltmann9797cbb2020-01-07 13:45:00 -08001706 public boolean isRadioOnWithFeature(String callingPackage, String callingFeatureId) {
1707 return isRadioOnForSubscriberWithFeature(getDefaultSubscription(), callingPackage,
1708 callingFeatureId);
1709 }
1710
1711 @Deprecated
1712 @Override
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001713 public boolean isRadioOnForSubscriber(int subId, String callingPackage) {
Philip P. Moltmann9797cbb2020-01-07 13:45:00 -08001714 return isRadioOnForSubscriberWithFeature(subId, callingPackage, null);
1715 }
1716
1717 @Override
1718 public boolean isRadioOnForSubscriberWithFeature(int subId, String callingPackage,
1719 String callingFeatureId) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08001720 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
Philip P. Moltmann9797cbb2020-01-07 13:45:00 -08001721 mApp, subId, callingPackage, callingFeatureId, "isRadioOnForSubscriber")) {
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001722 return false;
1723 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08001724
1725 final long identity = Binder.clearCallingIdentity();
1726 try {
1727 return isRadioOnForSubscriber(subId);
1728 } finally {
1729 Binder.restoreCallingIdentity(identity);
1730 }
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001731 }
1732
1733 private boolean isRadioOnForSubscriber(int subId) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08001734 final long identity = Binder.clearCallingIdentity();
1735 try {
1736 final Phone phone = getPhone(subId);
1737 if (phone != null) {
1738 return phone.getServiceState().getState() != ServiceState.STATE_POWER_OFF;
1739 } else {
1740 return false;
1741 }
1742 } finally {
1743 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07001744 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001745 }
1746
1747 public void toggleRadioOnOff() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001748 toggleRadioOnOffForSubscriber(getDefaultSubscription());
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001749 }
Wink Saville36469e72014-06-11 15:17:00 -07001750
Wink Savilleb564aae2014-10-23 10:18:09 -07001751 public void toggleRadioOnOffForSubscriber(int subId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001752 enforceModifyPermission();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08001753
1754 final long identity = Binder.clearCallingIdentity();
1755 try {
1756 final Phone phone = getPhone(subId);
1757 if (phone != null) {
1758 phone.setRadioPower(!isRadioOnForSubscriber(subId));
1759 }
1760 } finally {
1761 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07001762 }
Wink Saville36469e72014-06-11 15:17:00 -07001763 }
1764
1765 public boolean setRadio(boolean turnOn) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001766 return setRadioForSubscriber(getDefaultSubscription(), turnOn);
Wink Saville36469e72014-06-11 15:17:00 -07001767 }
1768
Wink Savilleb564aae2014-10-23 10:18:09 -07001769 public boolean setRadioForSubscriber(int subId, boolean turnOn) {
Wink Saville36469e72014-06-11 15:17:00 -07001770 enforceModifyPermission();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08001771
1772 final long identity = Binder.clearCallingIdentity();
1773 try {
1774 final Phone phone = getPhone(subId);
1775 if (phone == null) {
1776 return false;
1777 }
1778 if ((phone.getServiceState().getState() != ServiceState.STATE_POWER_OFF) != turnOn) {
1779 toggleRadioOnOffForSubscriber(subId);
1780 }
1781 return true;
1782 } finally {
1783 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07001784 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001785 }
Wink Saville36469e72014-06-11 15:17:00 -07001786
Naveen Kalla1fd79bd2014-08-08 00:48:59 -07001787 public boolean needMobileRadioShutdown() {
Shuo Qianafeaf7d2019-12-10 10:40:38 -08001788 enforceReadPrivilegedPermission("needMobileRadioShutdown");
Naveen Kalla1fd79bd2014-08-08 00:48:59 -07001789 /*
1790 * If any of the Radios are available, it will need to be
1791 * shutdown. So return true if any Radio is available.
1792 */
Malcolm Chenaa4a8532018-02-28 15:00:40 -08001793 final long identity = Binder.clearCallingIdentity();
1794 try {
1795 for (int i = 0; i < TelephonyManager.getDefault().getPhoneCount(); i++) {
1796 Phone phone = PhoneFactory.getPhone(i);
1797 if (phone != null && phone.isRadioAvailable()) return true;
1798 }
1799 logv(TelephonyManager.getDefault().getPhoneCount() + " Phones are shutdown.");
1800 return false;
1801 } finally {
1802 Binder.restoreCallingIdentity(identity);
Naveen Kalla1fd79bd2014-08-08 00:48:59 -07001803 }
Naveen Kalla1fd79bd2014-08-08 00:48:59 -07001804 }
1805
Malcolm Chenaa4a8532018-02-28 15:00:40 -08001806 @Override
Naveen Kalla1fd79bd2014-08-08 00:48:59 -07001807 public void shutdownMobileRadios() {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08001808 enforceModifyPermission();
1809
1810 final long identity = Binder.clearCallingIdentity();
1811 try {
1812 for (int i = 0; i < TelephonyManager.getDefault().getPhoneCount(); i++) {
1813 logv("Shutting down Phone " + i);
1814 shutdownRadioUsingPhoneId(i);
1815 }
1816 } finally {
1817 Binder.restoreCallingIdentity(identity);
Naveen Kalla1fd79bd2014-08-08 00:48:59 -07001818 }
1819 }
1820
1821 private void shutdownRadioUsingPhoneId(int phoneId) {
Naveen Kalla1fd79bd2014-08-08 00:48:59 -07001822 Phone phone = PhoneFactory.getPhone(phoneId);
1823 if (phone != null && phone.isRadioAvailable()) {
1824 phone.shutdownRadio();
1825 }
1826 }
1827
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001828 public boolean setRadioPower(boolean turnOn) {
Jack Yub4e16162017-05-15 12:48:40 -07001829 enforceModifyPermission();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08001830
1831 final long identity = Binder.clearCallingIdentity();
1832 try {
1833 final Phone defaultPhone = PhoneFactory.getDefaultPhone();
1834 if (defaultPhone != null) {
1835 defaultPhone.setRadioPower(turnOn);
1836 return true;
1837 } else {
1838 loge("There's no default phone.");
1839 return false;
1840 }
1841 } finally {
1842 Binder.restoreCallingIdentity(identity);
Wei Liu9ae2a062016-08-08 11:09:34 -07001843 }
Wink Saville36469e72014-06-11 15:17:00 -07001844 }
1845
Wink Savilleb564aae2014-10-23 10:18:09 -07001846 public boolean setRadioPowerForSubscriber(int subId, boolean turnOn) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001847 enforceModifyPermission();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08001848
1849 final long identity = Binder.clearCallingIdentity();
1850 try {
1851 final Phone phone = getPhone(subId);
1852 if (phone != null) {
1853 phone.setRadioPower(turnOn);
1854 return true;
1855 } else {
1856 return false;
1857 }
1858 } finally {
1859 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07001860 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001861 }
1862
Wink Saville36469e72014-06-11 15:17:00 -07001863 // FIXME: subId version needed
Sanket Padawe356d7632015-06-22 14:03:32 -07001864 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001865 public boolean enableDataConnectivity() {
1866 enforceModifyPermission();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08001867
1868 final long identity = Binder.clearCallingIdentity();
1869 try {
1870 int subId = mSubscriptionController.getDefaultDataSubId();
1871 final Phone phone = getPhone(subId);
1872 if (phone != null) {
Jack Yud79fba22018-12-13 11:51:28 -08001873 phone.getDataEnabledSettings().setUserDataEnabled(true);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08001874 return true;
1875 } else {
1876 return false;
1877 }
1878 } finally {
1879 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07001880 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001881 }
1882
Wink Saville36469e72014-06-11 15:17:00 -07001883 // FIXME: subId version needed
Sanket Padawe356d7632015-06-22 14:03:32 -07001884 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001885 public boolean disableDataConnectivity() {
1886 enforceModifyPermission();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08001887
1888 final long identity = Binder.clearCallingIdentity();
1889 try {
1890 int subId = mSubscriptionController.getDefaultDataSubId();
1891 final Phone phone = getPhone(subId);
1892 if (phone != null) {
Jack Yud79fba22018-12-13 11:51:28 -08001893 phone.getDataEnabledSettings().setUserDataEnabled(false);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08001894 return true;
1895 } else {
1896 return false;
1897 }
1898 } finally {
1899 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07001900 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001901 }
1902
Sanket Padawe356d7632015-06-22 14:03:32 -07001903 @Override
Jack Yuacf8a132017-05-01 17:00:48 -07001904 public boolean isDataConnectivityPossible(int subId) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08001905 final long identity = Binder.clearCallingIdentity();
1906 try {
1907 final Phone phone = getPhone(subId);
1908 if (phone != null) {
Jack Yub5d8f642018-11-26 11:20:48 -08001909 return phone.isDataAllowed(ApnSetting.TYPE_DEFAULT);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08001910 } else {
1911 return false;
1912 }
1913 } finally {
1914 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07001915 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001916 }
1917
1918 public boolean handlePinMmi(String dialString) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001919 return handlePinMmiForSubscriber(getDefaultSubscription(), dialString);
Wink Saville36469e72014-06-11 15:17:00 -07001920 }
1921
pkanwarae03a6b2016-11-06 20:37:09 -08001922 public void handleUssdRequest(int subId, String ussdRequest, ResultReceiver wrappedCallback) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08001923 enforceCallPermission();
1924
1925 final long identity = Binder.clearCallingIdentity();
1926 try {
1927 if (!SubscriptionManager.isValidSubscriptionId(subId)) {
1928 return;
1929 }
1930 Pair<String, ResultReceiver> ussdObject = new Pair(ussdRequest, wrappedCallback);
1931 sendRequest(CMD_HANDLE_USSD_REQUEST, ussdObject, subId);
1932 } finally {
1933 Binder.restoreCallingIdentity(identity);
1934 }
pkanwar32d516d2016-10-14 19:37:38 -07001935 };
1936
Wink Savilleb564aae2014-10-23 10:18:09 -07001937 public boolean handlePinMmiForSubscriber(int subId, String dialString) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001938 enforceModifyPermission();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08001939
1940 final long identity = Binder.clearCallingIdentity();
1941 try {
1942 if (!SubscriptionManager.isValidSubscriptionId(subId)) {
1943 return false;
1944 }
1945 return (Boolean) sendRequest(CMD_HANDLE_PIN_MMI, dialString, subId);
1946 } finally {
1947 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07001948 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001949 }
1950
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001951 public int getCallState() {
Shishir Agrawala9f32182016-04-12 12:00:16 -07001952 return getCallStateForSlot(getSlotForDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001953 }
1954
Sanket Padawe13bac7b2017-03-20 15:04:47 -07001955 public int getCallStateForSlot(int slotIndex) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08001956 final long identity = Binder.clearCallingIdentity();
1957 try {
1958 Phone phone = PhoneFactory.getPhone(slotIndex);
1959 return phone == null ? TelephonyManager.CALL_STATE_IDLE :
1960 PhoneConstantConversions.convertCallState(phone.getState());
1961 } finally {
1962 Binder.restoreCallingIdentity(identity);
1963 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001964 }
1965
Sanket Padawe356d7632015-06-22 14:03:32 -07001966 @Override
Nathan Harolde037c472019-06-26 00:41:07 +00001967 public int getDataState() {
Nathan Haroldc4689b12019-06-14 16:58:30 -07001968 return getDataStateForSubId(mSubscriptionController.getDefaultDataSubId());
1969 }
1970
1971 @Override
1972 public int getDataStateForSubId(int subId) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08001973 final long identity = Binder.clearCallingIdentity();
1974 try {
Nathan Haroldc4689b12019-06-14 16:58:30 -07001975 final Phone phone = getPhone(subId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08001976 if (phone != null) {
1977 return PhoneConstantConversions.convertDataState(phone.getDataConnectionState());
1978 } else {
1979 return PhoneConstantConversions.convertDataState(
1980 PhoneConstants.DataState.DISCONNECTED);
1981 }
1982 } finally {
1983 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07001984 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001985 }
1986
Sanket Padawe356d7632015-06-22 14:03:32 -07001987 @Override
Nathan Harolde037c472019-06-26 00:41:07 +00001988 public int getDataActivity() {
Nathan Haroldc4689b12019-06-14 16:58:30 -07001989 return getDataActivityForSubId(mSubscriptionController.getDefaultDataSubId());
1990 }
1991
1992 @Override
1993 public int getDataActivityForSubId(int subId) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08001994 final long identity = Binder.clearCallingIdentity();
1995 try {
Nathan Haroldc4689b12019-06-14 16:58:30 -07001996 final Phone phone = getPhone(subId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08001997 if (phone != null) {
1998 return DefaultPhoneNotifier.convertDataActivityState(phone.getDataActivityState());
1999 } else {
2000 return TelephonyManager.DATA_ACTIVITY_NONE;
2001 }
2002 } finally {
2003 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07002004 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002005 }
2006
2007 @Override
Meng Wangd64acad2019-12-09 13:13:01 -08002008 public CellIdentity getCellLocation(String callingPackage, String callingFeatureId) {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08002009 mApp.getSystemService(AppOpsManager.class)
Hall Liu1aa510f2017-11-22 17:40:08 -08002010 .checkPackage(Binder.getCallingUid(), callingPackage);
Hall Liuf19c44f2018-11-27 14:38:17 -08002011
2012 LocationAccessPolicy.LocationPermissionResult locationResult =
2013 LocationAccessPolicy.checkLocationPermission(mApp,
2014 new LocationAccessPolicy.LocationPermissionQuery.Builder()
2015 .setCallingPackage(callingPackage)
Philip P. Moltmann9797cbb2020-01-07 13:45:00 -08002016 .setCallingFeatureId(callingFeatureId)
Hall Liuf19c44f2018-11-27 14:38:17 -08002017 .setCallingPid(Binder.getCallingPid())
2018 .setCallingUid(Binder.getCallingUid())
2019 .setMethod("getCellLocation")
2020 .setMinSdkVersionForFine(Build.VERSION_CODES.Q)
2021 .build());
2022 switch (locationResult) {
2023 case DENIED_HARD:
2024 throw new SecurityException("Not allowed to access cell location");
2025 case DENIED_SOFT:
Meng Wangd64acad2019-12-09 13:13:01 -08002026 return (getDefaultPhone().getPhoneType() == PhoneConstants.PHONE_TYPE_CDMA)
2027 ? new CellIdentityCdma() : new CellIdentityGsm();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002028 }
2029
Narayan Kamathf04b5a12018-01-09 11:47:15 +00002030 WorkSource workSource = getWorkSource(Binder.getCallingUid());
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002031 final long identity = Binder.clearCallingIdentity();
2032 try {
2033 if (DBG_LOC) log("getCellLocation: is active user");
Nathan Harold3ff88932018-08-14 10:19:49 -07002034 int subId = mSubscriptionController.getDefaultDataSubId();
Meng Wangd64acad2019-12-09 13:13:01 -08002035 return (CellIdentity) sendRequest(CMD_GET_CELL_LOCATION, workSource, subId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002036 } finally {
2037 Binder.restoreCallingIdentity(identity);
2038 }
Svetoslav64fad262015-04-14 14:35:21 -07002039 }
2040
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002041 @Override
Philip P. Moltmann9797cbb2020-01-07 13:45:00 -08002042 public String getNetworkCountryIsoForPhone(int phoneId, String callingPackage,
2043 String callingFeatureId) {
Jack Yu7c5d91a2019-09-26 11:48:33 -07002044 if (!TextUtils.isEmpty(callingPackage)) {
2045 final int subId = mSubscriptionController.getSubIdUsingPhoneId(phoneId);
Philip P. Moltmann9797cbb2020-01-07 13:45:00 -08002046 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(mApp, subId, callingPackage,
2047 callingFeatureId, "getNetworkCountryIsoForPhone")) {
Jack Yu7c5d91a2019-09-26 11:48:33 -07002048 return "";
2049 }
2050 }
2051
Jonathan Basseribf5362b2017-07-19 12:22:35 -07002052 // Reporting the correct network country is ambiguous when IWLAN could conflict with
2053 // registered cell info, so return a NULL country instead.
2054 final long identity = Binder.clearCallingIdentity();
2055 try {
Malcolm Chen3732c2b2018-07-18 20:15:24 -07002056 if (phoneId == SubscriptionManager.INVALID_PHONE_INDEX) {
2057 // Get default phone in this case.
2058 phoneId = SubscriptionManager.DEFAULT_PHONE_INDEX;
2059 }
Jonathan Basseribf5362b2017-07-19 12:22:35 -07002060 final int subId = mSubscriptionController.getSubIdUsingPhoneId(phoneId);
Jack Yu5f7092c2018-04-13 14:05:37 -07002061 // Todo: fix this when we can get the actual cellular network info when the device
2062 // is on IWLAN.
Jonathan Basseribf5362b2017-07-19 12:22:35 -07002063 if (TelephonyManager.NETWORK_TYPE_IWLAN
Philip P. Moltmann9797cbb2020-01-07 13:45:00 -08002064 == getVoiceNetworkTypeForSubscriber(subId, mApp.getPackageName(),
2065 null)) {
Jonathan Basseribf5362b2017-07-19 12:22:35 -07002066 return "";
2067 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002068 Phone phone = PhoneFactory.getPhone(phoneId);
2069 if (phone != null) {
2070 ServiceStateTracker sst = phone.getServiceStateTracker();
sqianb9d961a2019-07-31 20:23:45 -07002071 EmergencyNumberTracker emergencyNumberTracker = phone.getEmergencyNumberTracker();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002072 if (sst != null) {
2073 LocaleTracker lt = sst.getLocaleTracker();
2074 if (lt != null) {
sqianb9d961a2019-07-31 20:23:45 -07002075 if (!TextUtils.isEmpty(lt.getCurrentCountry())) {
2076 return lt.getCurrentCountry();
2077 } else if (emergencyNumberTracker != null) {
2078 return emergencyNumberTracker.getEmergencyCountryIso();
2079 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002080 }
2081 }
2082 }
2083 return "";
Jonathan Basseribf5362b2017-07-19 12:22:35 -07002084 } finally {
2085 Binder.restoreCallingIdentity(identity);
2086 }
Jonathan Basseribf5362b2017-07-19 12:22:35 -07002087 }
2088
2089 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002090 public void enableLocationUpdates() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07002091 enableLocationUpdatesForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07002092 }
2093
Sanket Padawe356d7632015-06-22 14:03:32 -07002094 @Override
Wink Savilleb564aae2014-10-23 10:18:09 -07002095 public void enableLocationUpdatesForSubscriber(int subId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002096 mApp.enforceCallingOrSelfPermission(
2097 android.Manifest.permission.CONTROL_LOCATION_UPDATES, null);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002098
2099 final long identity = Binder.clearCallingIdentity();
2100 try {
2101 final Phone phone = getPhone(subId);
2102 if (phone != null) {
2103 phone.enableLocationUpdates();
2104 }
2105 } finally {
2106 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07002107 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002108 }
2109
2110 @Override
2111 public void disableLocationUpdates() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07002112 disableLocationUpdatesForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07002113 }
2114
Sanket Padawe356d7632015-06-22 14:03:32 -07002115 @Override
Wink Savilleb564aae2014-10-23 10:18:09 -07002116 public void disableLocationUpdatesForSubscriber(int subId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002117 mApp.enforceCallingOrSelfPermission(
2118 android.Manifest.permission.CONTROL_LOCATION_UPDATES, null);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002119
2120 final long identity = Binder.clearCallingIdentity();
2121 try {
2122 final Phone phone = getPhone(subId);
2123 if (phone != null) {
2124 phone.disableLocationUpdates();
2125 }
2126 } finally {
2127 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07002128 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002129 }
2130
Nathan Harold31d7ff32018-10-15 20:20:30 -07002131 /**
2132 * Returns the target SDK version number for a given package name.
2133 *
Nathan Haroldec184742019-07-10 17:04:16 -07002134 * This call MUST be invoked before clearing the calling UID.
2135 *
Nathan Harold31d7ff32018-10-15 20:20:30 -07002136 * @return target SDK if the package is found or INT_MAX.
2137 */
2138 private int getTargetSdk(String packageName) {
2139 try {
Nathan Haroldec184742019-07-10 17:04:16 -07002140 final ApplicationInfo ai = mApp.getPackageManager().getApplicationInfoAsUser(
Chen Xu54d20302019-07-30 15:12:06 -07002141 packageName, 0, UserHandle.getUserHandleForUid(Binder.getCallingUid()));
Nathan Harold31d7ff32018-10-15 20:20:30 -07002142 if (ai != null) return ai.targetSdkVersion;
2143 } catch (PackageManager.NameNotFoundException unexpected) {
Nathan Haroldec184742019-07-10 17:04:16 -07002144 loge("Failed to get package info for pkg="
2145 + packageName + ", uid=" + Binder.getCallingUid());
Nathan Harold31d7ff32018-10-15 20:20:30 -07002146 }
2147 return Integer.MAX_VALUE;
2148 }
2149
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002150 @Override
2151 @SuppressWarnings("unchecked")
Philip P. Moltmann9797cbb2020-01-07 13:45:00 -08002152 public List<NeighboringCellInfo> getNeighboringCellInfo(String callingPackage,
2153 String callingFeatureId) {
Nathan Harold31d7ff32018-10-15 20:20:30 -07002154 final int targetSdk = getTargetSdk(callingPackage);
Nathan Harolddbea45a2018-08-30 14:35:07 -07002155 if (targetSdk >= android.os.Build.VERSION_CODES.Q) {
2156 throw new SecurityException(
2157 "getNeighboringCellInfo() is unavailable to callers targeting Q+ SDK levels.");
2158 }
Nathan Haroldb4d55612018-07-20 13:13:08 -07002159
Jordan Liu1617b712019-07-10 15:06:26 -07002160 if (mAppOps.noteOp(AppOpsManager.OPSTR_NEIGHBORING_CELLS, Binder.getCallingUid(),
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002161 callingPackage) != AppOpsManager.MODE_ALLOWED) {
2162 return null;
2163 }
Svetoslav64fad262015-04-14 14:35:21 -07002164
Svetoslav Ganov4a9d4482017-06-20 19:53:35 -07002165 if (DBG_LOC) log("getNeighboringCellInfo: is active user");
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002166
Philip P. Moltmann9797cbb2020-01-07 13:45:00 -08002167 List<CellInfo> info = getAllCellInfo(callingPackage, callingFeatureId);
Nathan Haroldf180aac2018-06-01 18:43:55 -07002168 if (info == null) return null;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002169
Nathan Haroldf180aac2018-06-01 18:43:55 -07002170 List<NeighboringCellInfo> neighbors = new ArrayList<NeighboringCellInfo>();
2171 for (CellInfo ci : info) {
2172 if (ci instanceof CellInfoGsm) {
2173 neighbors.add(new NeighboringCellInfo((CellInfoGsm) ci));
2174 } else if (ci instanceof CellInfoWcdma) {
2175 neighbors.add(new NeighboringCellInfo((CellInfoWcdma) ci));
2176 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002177 }
Nathan Haroldf180aac2018-06-01 18:43:55 -07002178 return (neighbors.size()) > 0 ? neighbors : null;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002179 }
2180
Nathan Haroldfa8da0f2018-09-27 18:51:29 -07002181 private List<CellInfo> getCachedCellInfo() {
2182 List<CellInfo> cellInfos = new ArrayList<CellInfo>();
2183 for (Phone phone : PhoneFactory.getPhones()) {
2184 List<CellInfo> info = phone.getAllCellInfo();
2185 if (info != null) cellInfos.addAll(info);
2186 }
2187 return cellInfos;
2188 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002189
2190 @Override
Philip P. Moltmann9797cbb2020-01-07 13:45:00 -08002191 public List<CellInfo> getAllCellInfo(String callingPackage, String callingFeatureId) {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08002192 mApp.getSystemService(AppOpsManager.class)
Hall Liu1aa510f2017-11-22 17:40:08 -08002193 .checkPackage(Binder.getCallingUid(), callingPackage);
Hall Liuf19c44f2018-11-27 14:38:17 -08002194
2195 LocationAccessPolicy.LocationPermissionResult locationResult =
2196 LocationAccessPolicy.checkLocationPermission(mApp,
2197 new LocationAccessPolicy.LocationPermissionQuery.Builder()
2198 .setCallingPackage(callingPackage)
Philip P. Moltmann9797cbb2020-01-07 13:45:00 -08002199 .setCallingFeatureId(callingFeatureId)
Hall Liuf19c44f2018-11-27 14:38:17 -08002200 .setCallingPid(Binder.getCallingPid())
2201 .setCallingUid(Binder.getCallingUid())
2202 .setMethod("getAllCellInfo")
Nathan Harold5ae50b52019-02-20 15:46:36 -08002203 .setMinSdkVersionForCoarse(Build.VERSION_CODES.BASE)
Hall Liuf19c44f2018-11-27 14:38:17 -08002204 .setMinSdkVersionForFine(Build.VERSION_CODES.Q)
2205 .build());
2206 switch (locationResult) {
2207 case DENIED_HARD:
2208 throw new SecurityException("Not allowed to access cell info");
2209 case DENIED_SOFT:
2210 return new ArrayList<>();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002211 }
2212
Nathan Haroldfa8da0f2018-09-27 18:51:29 -07002213 final int targetSdk = getTargetSdk(callingPackage);
2214 if (targetSdk >= android.os.Build.VERSION_CODES.Q) {
2215 return getCachedCellInfo();
2216 }
2217
Svetoslav Ganov4a9d4482017-06-20 19:53:35 -07002218 if (DBG_LOC) log("getAllCellInfo: is active user");
Narayan Kamathf04b5a12018-01-09 11:47:15 +00002219 WorkSource workSource = getWorkSource(Binder.getCallingUid());
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002220 final long identity = Binder.clearCallingIdentity();
2221 try {
2222 List<CellInfo> cellInfos = new ArrayList<CellInfo>();
2223 for (Phone phone : PhoneFactory.getPhones()) {
Nathan Harold3ff88932018-08-14 10:19:49 -07002224 final List<CellInfo> info = (List<CellInfo>) sendRequest(
Nathan Harold92bed182018-10-12 18:16:49 -07002225 CMD_GET_ALL_CELL_INFO, null, phone, workSource);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002226 if (info != null) cellInfos.addAll(info);
2227 }
2228 return cellInfos;
2229 } finally {
2230 Binder.restoreCallingIdentity(identity);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002231 }
2232 }
2233
Sailesh Nepalbd76e4e2013-10-27 13:59:44 -07002234 @Override
Philip P. Moltmann9797cbb2020-01-07 13:45:00 -08002235 public void requestCellInfoUpdate(int subId, ICellInfoCallback cb, String callingPackage,
2236 String callingFeatureId) {
2237 requestCellInfoUpdateInternal(subId, cb, callingPackage, callingFeatureId,
2238 getWorkSource(Binder.getCallingUid()));
Nathan Haroldfa8da0f2018-09-27 18:51:29 -07002239 }
2240
2241 @Override
Philip P. Moltmann9797cbb2020-01-07 13:45:00 -08002242 public void requestCellInfoUpdateWithWorkSource(int subId, ICellInfoCallback cb,
2243 String callingPackage, String callingFeatureId, WorkSource workSource) {
Nathan Haroldfa8da0f2018-09-27 18:51:29 -07002244 enforceModifyPermission();
Philip P. Moltmann9797cbb2020-01-07 13:45:00 -08002245 requestCellInfoUpdateInternal(subId, cb, callingPackage, callingFeatureId, workSource);
Nathan Haroldfa8da0f2018-09-27 18:51:29 -07002246 }
2247
Philip P. Moltmann9797cbb2020-01-07 13:45:00 -08002248 private void requestCellInfoUpdateInternal(int subId, ICellInfoCallback cb,
2249 String callingPackage, String callingFeatureId, WorkSource workSource) {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08002250 mApp.getSystemService(AppOpsManager.class)
Nathan Haroldfa8da0f2018-09-27 18:51:29 -07002251 .checkPackage(Binder.getCallingUid(), callingPackage);
Hall Liuf19c44f2018-11-27 14:38:17 -08002252
2253 LocationAccessPolicy.LocationPermissionResult locationResult =
2254 LocationAccessPolicy.checkLocationPermission(mApp,
2255 new LocationAccessPolicy.LocationPermissionQuery.Builder()
2256 .setCallingPackage(callingPackage)
Philip P. Moltmann9797cbb2020-01-07 13:45:00 -08002257 .setCallingFeatureId(callingFeatureId)
Hall Liuf19c44f2018-11-27 14:38:17 -08002258 .setCallingPid(Binder.getCallingPid())
2259 .setCallingUid(Binder.getCallingUid())
2260 .setMethod("requestCellInfoUpdate")
2261 .setMinSdkVersionForFine(Build.VERSION_CODES.Q)
2262 .build());
2263 switch (locationResult) {
2264 case DENIED_HARD:
2265 throw new SecurityException("Not allowed to access cell info");
2266 case DENIED_SOFT:
Nathan Harold5320c422019-05-09 10:26:08 -07002267 try {
2268 cb.onCellInfo(new ArrayList<CellInfo>());
2269 } catch (RemoteException re) {
2270 // Drop without consequences
2271 }
Hall Liuf19c44f2018-11-27 14:38:17 -08002272 return;
Nathan Haroldfa8da0f2018-09-27 18:51:29 -07002273 }
2274
Nathan Harolda939a962019-05-09 10:13:47 -07002275
2276 final Phone phone = getPhoneFromSubId(subId);
Nathan Haroldfa8da0f2018-09-27 18:51:29 -07002277 if (phone == null) throw new IllegalArgumentException("Invalid Subscription Id: " + subId);
2278
2279 sendRequestAsync(CMD_REQUEST_CELL_INFO_UPDATE, cb, phone, workSource);
2280 }
2281
2282 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002283 public void setCellInfoListRate(int rateInMillis) {
Jack Yua8d8cb82017-01-16 10:15:34 -08002284 enforceModifyPermission();
Narayan Kamathf04b5a12018-01-09 11:47:15 +00002285 WorkSource workSource = getWorkSource(Binder.getCallingUid());
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002286
2287 final long identity = Binder.clearCallingIdentity();
2288 try {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08002289 getDefaultPhone().setCellInfoListRate(rateInMillis, workSource);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002290 } finally {
2291 Binder.restoreCallingIdentity(identity);
2292 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002293 }
2294
Shishir Agrawala9f32182016-04-12 12:00:16 -07002295 @Override
Philip P. Moltmann9797cbb2020-01-07 13:45:00 -08002296 public String getImeiForSlot(int slotIndex, String callingPackage, String callingFeatureId) {
Jeff Davidson913390f2018-02-23 17:11:49 -08002297 Phone phone = PhoneFactory.getPhone(slotIndex);
2298 if (phone == null) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08002299 return null;
2300 }
Jeff Davidson913390f2018-02-23 17:11:49 -08002301 int subId = phone.getSubId();
Michael Groover70af6dc2018-10-01 16:23:15 -07002302 if (!TelephonyPermissions.checkCallingOrSelfReadDeviceIdentifiers(mApp, subId,
Philip P. Moltmann9797cbb2020-01-07 13:45:00 -08002303 callingPackage, callingFeatureId, "getImeiForSlot")) {
Jeff Davidson913390f2018-02-23 17:11:49 -08002304 return null;
2305 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002306
2307 final long identity = Binder.clearCallingIdentity();
2308 try {
2309 return phone.getImei();
2310 } finally {
2311 Binder.restoreCallingIdentity(identity);
2312 }
Shishir Agrawala9f32182016-04-12 12:00:16 -07002313 }
2314
2315 @Override
David Kelly5e06a7f2018-03-12 14:10:59 +00002316 public String getTypeAllocationCodeForSlot(int slotIndex) {
2317 Phone phone = PhoneFactory.getPhone(slotIndex);
2318 String tac = null;
2319 if (phone != null) {
2320 String imei = phone.getImei();
2321 tac = imei == null ? null : imei.substring(0, TYPE_ALLOCATION_CODE_LENGTH);
2322 }
2323 return tac;
2324 }
2325
2326 @Override
Philip P. Moltmann9797cbb2020-01-07 13:45:00 -08002327 public String getMeidForSlot(int slotIndex, String callingPackage, String callingFeatureId) {
Jeff Davidson913390f2018-02-23 17:11:49 -08002328 Phone phone = PhoneFactory.getPhone(slotIndex);
2329 if (phone == null) {
Jack Yu2af8d712017-03-15 17:14:14 -07002330 return null;
2331 }
Nazanin Bakhshif782e562018-12-11 15:15:39 -08002332
Jeff Davidson913390f2018-02-23 17:11:49 -08002333 int subId = phone.getSubId();
Michael Groover70af6dc2018-10-01 16:23:15 -07002334 if (!TelephonyPermissions.checkCallingOrSelfReadDeviceIdentifiers(mApp, subId,
Philip P. Moltmann9797cbb2020-01-07 13:45:00 -08002335 callingPackage, callingFeatureId, "getMeidForSlot")) {
Jeff Davidson913390f2018-02-23 17:11:49 -08002336 return null;
2337 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002338
2339 final long identity = Binder.clearCallingIdentity();
2340 try {
2341 return phone.getMeid();
2342 } finally {
2343 Binder.restoreCallingIdentity(identity);
2344 }
Jack Yu2af8d712017-03-15 17:14:14 -07002345 }
2346
2347 @Override
David Kelly5e06a7f2018-03-12 14:10:59 +00002348 public String getManufacturerCodeForSlot(int slotIndex) {
2349 Phone phone = PhoneFactory.getPhone(slotIndex);
2350 String manufacturerCode = null;
2351 if (phone != null) {
2352 String meid = phone.getMeid();
2353 manufacturerCode = meid == null ? null : meid.substring(0, MANUFACTURER_CODE_LENGTH);
2354 }
2355 return manufacturerCode;
2356 }
2357
2358 @Override
Philip P. Moltmann9797cbb2020-01-07 13:45:00 -08002359 public String getDeviceSoftwareVersionForSlot(int slotIndex, String callingPackage,
2360 String callingFeatureId) {
Jeff Davidson913390f2018-02-23 17:11:49 -08002361 Phone phone = PhoneFactory.getPhone(slotIndex);
2362 if (phone == null) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08002363 return null;
2364 }
Jeff Davidson913390f2018-02-23 17:11:49 -08002365 int subId = phone.getSubId();
2366 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
Philip P. Moltmann9797cbb2020-01-07 13:45:00 -08002367 mApp, subId, callingPackage, callingFeatureId,
2368 "getDeviceSoftwareVersionForSlot")) {
Jeff Davidson913390f2018-02-23 17:11:49 -08002369 return null;
2370 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002371
2372 final long identity = Binder.clearCallingIdentity();
2373 try {
2374 return phone.getDeviceSvn();
2375 } finally {
2376 Binder.restoreCallingIdentity(identity);
2377 }
Shishir Agrawala9f32182016-04-12 12:00:16 -07002378 }
2379
fionaxu43304da2017-11-27 22:51:16 -08002380 @Override
2381 public int getSubscriptionCarrierId(int subId) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002382 final long identity = Binder.clearCallingIdentity();
2383 try {
2384 final Phone phone = getPhone(subId);
2385 return phone == null ? TelephonyManager.UNKNOWN_CARRIER_ID : phone.getCarrierId();
2386 } finally {
2387 Binder.restoreCallingIdentity(identity);
2388 }
fionaxu43304da2017-11-27 22:51:16 -08002389 }
2390
2391 @Override
2392 public String getSubscriptionCarrierName(int subId) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002393 final long identity = Binder.clearCallingIdentity();
2394 try {
2395 final Phone phone = getPhone(subId);
2396 return phone == null ? null : phone.getCarrierName();
2397 } finally {
2398 Binder.restoreCallingIdentity(identity);
2399 }
fionaxu43304da2017-11-27 22:51:16 -08002400 }
2401
calvinpanffe225e2018-11-01 19:43:06 +08002402 @Override
chen xu0026ca62019-03-06 15:28:50 -08002403 public int getSubscriptionSpecificCarrierId(int subId) {
chen xu25637222018-11-04 17:17:00 -08002404 final long identity = Binder.clearCallingIdentity();
2405 try {
2406 final Phone phone = getPhone(subId);
2407 return phone == null ? TelephonyManager.UNKNOWN_CARRIER_ID
chen xu0026ca62019-03-06 15:28:50 -08002408 : phone.getSpecificCarrierId();
chen xu25637222018-11-04 17:17:00 -08002409 } finally {
2410 Binder.restoreCallingIdentity(identity);
2411 }
2412 }
2413
2414 @Override
chen xu0026ca62019-03-06 15:28:50 -08002415 public String getSubscriptionSpecificCarrierName(int subId) {
chen xu25637222018-11-04 17:17:00 -08002416 final long identity = Binder.clearCallingIdentity();
2417 try {
2418 final Phone phone = getPhone(subId);
chen xu0026ca62019-03-06 15:28:50 -08002419 return phone == null ? null : phone.getSpecificCarrierName();
chen xu25637222018-11-04 17:17:00 -08002420 } finally {
2421 Binder.restoreCallingIdentity(identity);
2422 }
2423 }
2424
chen xu651eec72018-11-11 19:03:44 -08002425 @Override
chen xu864e11c2018-12-06 22:10:03 -08002426 public int getCarrierIdFromMccMnc(int slotIndex, String mccmnc, boolean isSubscriptionMccMnc) {
2427 if (!isSubscriptionMccMnc) {
2428 enforceReadPrivilegedPermission("getCarrierIdFromMccMnc");
2429 }
chen xu651eec72018-11-11 19:03:44 -08002430 final Phone phone = PhoneFactory.getPhone(slotIndex);
2431 if (phone == null) {
2432 return TelephonyManager.UNKNOWN_CARRIER_ID;
2433 }
2434 final long identity = Binder.clearCallingIdentity();
2435 try {
2436 return CarrierResolver.getCarrierIdFromMccMnc(phone.getContext(), mccmnc);
2437 } finally {
2438 Binder.restoreCallingIdentity(identity);
2439 }
2440 }
2441
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002442 //
2443 // Internal helper methods.
2444 //
2445
Sanket Padaweee13a9b2016-03-08 17:30:28 -08002446 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002447 * Make sure the caller has the MODIFY_PHONE_STATE permission.
2448 *
2449 * @throws SecurityException if the caller does not have the required permission
2450 */
2451 private void enforceModifyPermission() {
2452 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.MODIFY_PHONE_STATE, null);
2453 }
2454
Shuo Qian5bac1ee2020-01-16 20:51:11 -08002455 /**
2456 * Make sure the caller is system.
2457 *
2458 * @throws SecurityException if the caller is not system.
2459 */
2460 private void enforceSystemCaller() {
2461 if (Binder.getCallingUid() != Process.SYSTEM_UID) {
2462 throw new SecurityException("Caller must be system");
2463 }
2464 }
2465
Shuo Qianf2b2df42019-11-13 17:43:31 -08002466 private void enforceActiveEmergencySessionPermission() {
2467 mApp.enforceCallingOrSelfPermission(
2468 android.Manifest.permission.READ_ACTIVE_EMERGENCY_SESSION, null);
2469 }
2470
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002471 /**
2472 * Make sure the caller has the CALL_PHONE permission.
2473 *
2474 * @throws SecurityException if the caller does not have the required permission
2475 */
2476 private void enforceCallPermission() {
2477 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.CALL_PHONE, null);
2478 }
2479
paulhu423b5f22019-08-23 19:17:33 +08002480 private void enforceSettingsPermission() {
2481 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.NETWORK_SETTINGS, null);
Stuart Scott8eef64f2015-04-08 15:13:54 -07002482 }
2483
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002484 private String createTelUrl(String number) {
2485 if (TextUtils.isEmpty(number)) {
2486 return null;
2487 }
2488
Jake Hambye994d462014-02-03 13:10:13 -08002489 return "tel:" + number;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002490 }
2491
Ihab Awadf9e92732013-12-05 18:02:52 -08002492 private static void log(String msg) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002493 Log.d(LOG_TAG, "[PhoneIntfMgr] " + msg);
2494 }
2495
Naveen Kalla1fd79bd2014-08-08 00:48:59 -07002496 private static void logv(String msg) {
2497 Log.v(LOG_TAG, "[PhoneIntfMgr] " + msg);
2498 }
2499
Ihab Awadf9e92732013-12-05 18:02:52 -08002500 private static void loge(String msg) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002501 Log.e(LOG_TAG, "[PhoneIntfMgr] " + msg);
2502 }
2503
Robert Greenwalt36b23af2015-07-06 17:59:14 -07002504 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002505 public int getActivePhoneType() {
Shishir Agrawala9f32182016-04-12 12:00:16 -07002506 return getActivePhoneTypeForSlot(getSlotForDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07002507 }
2508
Sanket Padawe356d7632015-06-22 14:03:32 -07002509 @Override
Sanket Padawe13bac7b2017-03-20 15:04:47 -07002510 public int getActivePhoneTypeForSlot(int slotIndex) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002511 final long identity = Binder.clearCallingIdentity();
2512 try {
2513 final Phone phone = PhoneFactory.getPhone(slotIndex);
2514 if (phone == null) {
2515 return PhoneConstants.PHONE_TYPE_NONE;
2516 } else {
2517 return phone.getPhoneType();
2518 }
2519 } finally {
2520 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07002521 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002522 }
2523
2524 /**
2525 * Returns the CDMA ERI icon index to display
2526 */
Robert Greenwalt36b23af2015-07-06 17:59:14 -07002527 @Override
Philip P. Moltmann9797cbb2020-01-07 13:45:00 -08002528 public int getCdmaEriIconIndex(String callingPackage, String callingFeatureId) {
2529 return getCdmaEriIconIndexForSubscriber(getDefaultSubscription(), callingPackage,
2530 callingFeatureId);
Wink Saville36469e72014-06-11 15:17:00 -07002531 }
2532
Sanket Padawe356d7632015-06-22 14:03:32 -07002533 @Override
Philip P. Moltmann9797cbb2020-01-07 13:45:00 -08002534 public int getCdmaEriIconIndexForSubscriber(int subId, String callingPackage,
2535 String callingFeatureId) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08002536 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
Philip P. Moltmann9797cbb2020-01-07 13:45:00 -08002537 mApp, subId, callingPackage, callingFeatureId,
2538 "getCdmaEriIconIndexForSubscriber")) {
Robert Greenwalt36b23af2015-07-06 17:59:14 -07002539 return -1;
2540 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002541
2542 final long identity = Binder.clearCallingIdentity();
2543 try {
2544 final Phone phone = getPhone(subId);
2545 if (phone != null) {
2546 return phone.getCdmaEriIconIndex();
2547 } else {
2548 return -1;
2549 }
2550 } finally {
2551 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07002552 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002553 }
2554
2555 /**
2556 * Returns the CDMA ERI icon mode,
2557 * 0 - ON
2558 * 1 - FLASHING
2559 */
Robert Greenwalt36b23af2015-07-06 17:59:14 -07002560 @Override
Philip P. Moltmann9797cbb2020-01-07 13:45:00 -08002561 public int getCdmaEriIconMode(String callingPackage, String callingFeatureId) {
2562 return getCdmaEriIconModeForSubscriber(getDefaultSubscription(), callingPackage,
2563 callingFeatureId);
Wink Saville36469e72014-06-11 15:17:00 -07002564 }
2565
Sanket Padawe356d7632015-06-22 14:03:32 -07002566 @Override
Philip P. Moltmann9797cbb2020-01-07 13:45:00 -08002567 public int getCdmaEriIconModeForSubscriber(int subId, String callingPackage,
2568 String callingFeatureId) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08002569 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
Philip P. Moltmann9797cbb2020-01-07 13:45:00 -08002570 mApp, subId, callingPackage, callingFeatureId,
2571 "getCdmaEriIconModeForSubscriber")) {
Robert Greenwalt36b23af2015-07-06 17:59:14 -07002572 return -1;
2573 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002574
2575 final long identity = Binder.clearCallingIdentity();
2576 try {
2577 final Phone phone = getPhone(subId);
2578 if (phone != null) {
2579 return phone.getCdmaEriIconMode();
2580 } else {
2581 return -1;
2582 }
2583 } finally {
2584 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07002585 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002586 }
2587
2588 /**
2589 * Returns the CDMA ERI text,
2590 */
Robert Greenwalt36b23af2015-07-06 17:59:14 -07002591 @Override
Philip P. Moltmann9797cbb2020-01-07 13:45:00 -08002592 public String getCdmaEriText(String callingPackage, String callingFeatureId) {
2593 return getCdmaEriTextForSubscriber(getDefaultSubscription(), callingPackage,
2594 callingFeatureId);
Wink Saville36469e72014-06-11 15:17:00 -07002595 }
2596
Sanket Padawe356d7632015-06-22 14:03:32 -07002597 @Override
Philip P. Moltmann9797cbb2020-01-07 13:45:00 -08002598 public String getCdmaEriTextForSubscriber(int subId, String callingPackage,
2599 String callingFeatureId) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08002600 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
Philip P. Moltmann9797cbb2020-01-07 13:45:00 -08002601 mApp, subId, callingPackage, callingFeatureId,
2602 "getCdmaEriIconTextForSubscriber")) {
Robert Greenwalt36b23af2015-07-06 17:59:14 -07002603 return null;
2604 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002605
2606 final long identity = Binder.clearCallingIdentity();
2607 try {
2608 final Phone phone = getPhone(subId);
2609 if (phone != null) {
2610 return phone.getCdmaEriText();
2611 } else {
2612 return null;
2613 }
2614 } finally {
2615 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07002616 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002617 }
2618
2619 /**
Junda Liuca05d5d2014-08-14 22:36:34 -07002620 * Returns the CDMA MDN.
2621 */
Sanket Padawe356d7632015-06-22 14:03:32 -07002622 @Override
Wink Savilleb564aae2014-10-23 10:18:09 -07002623 public String getCdmaMdn(int subId) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08002624 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
2625 mApp, subId, "getCdmaMdn");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002626
2627 final long identity = Binder.clearCallingIdentity();
2628 try {
2629 final Phone phone = getPhone(subId);
Nazanin Bakhshif782e562018-12-11 15:15:39 -08002630 if (phone != null && phone.getPhoneType() == PhoneConstants.PHONE_TYPE_CDMA) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002631 return phone.getLine1Number();
2632 } else {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08002633 loge("getCdmaMdn: no phone found. Invalid subId: " + subId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002634 return null;
2635 }
2636 } finally {
2637 Binder.restoreCallingIdentity(identity);
Junda Liuca05d5d2014-08-14 22:36:34 -07002638 }
2639 }
2640
2641 /**
2642 * Returns the CDMA MIN.
2643 */
Sanket Padawe356d7632015-06-22 14:03:32 -07002644 @Override
Wink Savilleb564aae2014-10-23 10:18:09 -07002645 public String getCdmaMin(int subId) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08002646 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
2647 mApp, subId, "getCdmaMin");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002648
2649 final long identity = Binder.clearCallingIdentity();
2650 try {
2651 final Phone phone = getPhone(subId);
2652 if (phone != null && phone.getPhoneType() == PhoneConstants.PHONE_TYPE_CDMA) {
2653 return phone.getCdmaMin();
2654 } else {
2655 return null;
2656 }
2657 } finally {
2658 Binder.restoreCallingIdentity(identity);
Junda Liuca05d5d2014-08-14 22:36:34 -07002659 }
2660 }
2661
Hall Liud892bec2018-11-30 14:51:45 -08002662 @Override
2663 public void requestNumberVerification(PhoneNumberRange range, long timeoutMillis,
2664 INumberVerificationCallback callback, String callingPackage) {
2665 if (mApp.checkCallingOrSelfPermission(android.Manifest.permission.MODIFY_PHONE_STATE)
2666 != PERMISSION_GRANTED) {
2667 throw new SecurityException("Caller must hold the MODIFY_PHONE_STATE permission");
2668 }
2669 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
2670
2671 String authorizedPackage = NumberVerificationManager.getAuthorizedPackage(mApp);
2672 if (!TextUtils.equals(callingPackage, authorizedPackage)) {
2673 throw new SecurityException("Calling package must be configured in the device config");
2674 }
2675
2676 if (range == null) {
2677 throw new NullPointerException("Range must be non-null");
2678 }
2679
2680 timeoutMillis = Math.min(timeoutMillis,
Hall Liubd069e32019-02-28 18:56:30 -08002681 TelephonyManager.getMaxNumberVerificationTimeoutMillis());
Hall Liud892bec2018-11-30 14:51:45 -08002682
2683 NumberVerificationManager.getInstance().requestVerification(range, callback, timeoutMillis);
2684 }
2685
Junda Liuca05d5d2014-08-14 22:36:34 -07002686 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002687 * Returns true if CDMA provisioning needs to run.
2688 */
2689 public boolean needsOtaServiceProvisioning() {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002690 final long identity = Binder.clearCallingIdentity();
2691 try {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08002692 return getDefaultPhone().needsOtaServiceProvisioning();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002693 } finally {
2694 Binder.restoreCallingIdentity(identity);
2695 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002696 }
2697
2698 /**
Shishir Agrawal76d5da92014-11-09 16:17:25 -08002699 * Sets the voice mail number of a given subId.
2700 */
2701 @Override
2702 public boolean setVoiceMailNumber(int subId, String alphaTag, String number) {
Shuo Qian6d927452019-12-05 11:40:37 -08002703 TelephonyPermissions.enforceCallingOrSelfCarrierPrivilege(
2704 mApp, subId, "setVoiceMailNumber");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002705
2706 final long identity = Binder.clearCallingIdentity();
2707 try {
2708 Boolean success = (Boolean) sendRequest(CMD_SET_VOICEMAIL_NUMBER,
2709 new Pair<String, String>(alphaTag, number), new Integer(subId));
2710 return success;
2711 } finally {
2712 Binder.restoreCallingIdentity(identity);
2713 }
Shishir Agrawal76d5da92014-11-09 16:17:25 -08002714 }
2715
Ta-wei Yen87c49842016-05-13 21:19:52 -07002716 @Override
Ta-wei Yenc9df0432017-04-17 17:09:07 -07002717 public Bundle getVisualVoicemailSettings(String callingPackage, int subId) {
2718 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
Tyler Gunn77ee9382019-10-31 13:08:23 -07002719 TelecomManager tm = mApp.getSystemService(TelecomManager.class);
2720 String systemDialer = tm.getSystemDialerPackage();
Ta-wei Yenc9df0432017-04-17 17:09:07 -07002721 if (!TextUtils.equals(callingPackage, systemDialer)) {
2722 throw new SecurityException("caller must be system dialer");
2723 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002724
2725 final long identity = Binder.clearCallingIdentity();
2726 try {
2727 PhoneAccountHandle phoneAccountHandle = PhoneAccountHandleConverter.fromSubId(subId);
2728 if (phoneAccountHandle == null) {
2729 return null;
2730 }
Nazanin Bakhshif782e562018-12-11 15:15:39 -08002731 return VisualVoicemailSettingsUtil.dump(mApp, phoneAccountHandle);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002732 } finally {
2733 Binder.restoreCallingIdentity(identity);
Ta-wei Yenc9df0432017-04-17 17:09:07 -07002734 }
Ta-wei Yenc9df0432017-04-17 17:09:07 -07002735 }
2736
2737 @Override
Philip P. Moltmann9797cbb2020-01-07 13:45:00 -08002738 public String getVisualVoicemailPackageName(String callingPackage, String callingFeatureId,
2739 int subId) {
Ta-wei Yendca928f2017-01-10 16:17:08 -08002740 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
Jeff Davidson7e17e312018-02-13 18:17:36 -08002741 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
Philip P. Moltmann9797cbb2020-01-07 13:45:00 -08002742 mApp, subId, callingPackage, callingFeatureId,
2743 "getVisualVoicemailPackageName")) {
Ta-wei Yendca928f2017-01-10 16:17:08 -08002744 return null;
2745 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002746
Jeff Davidsona8e4e242018-03-15 17:16:18 -07002747 final long identity = Binder.clearCallingIdentity();
2748 try {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08002749 return RemoteVvmTaskManager.getRemotePackage(mApp, subId).getPackageName();
Jeff Davidsona8e4e242018-03-15 17:16:18 -07002750 } finally {
2751 Binder.restoreCallingIdentity(identity);
2752 }
Ta-wei Yendca928f2017-01-10 16:17:08 -08002753 }
2754
2755 @Override
Ta-wei Yenb6929602016-05-24 15:48:27 -07002756 public void enableVisualVoicemailSmsFilter(String callingPackage, int subId,
2757 VisualVoicemailSmsFilterSettings settings) {
2758 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002759
2760 final long identity = Binder.clearCallingIdentity();
2761 try {
2762 VisualVoicemailSmsFilterConfig.enableVisualVoicemailSmsFilter(
Nazanin Bakhshif782e562018-12-11 15:15:39 -08002763 mApp, callingPackage, subId, settings);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002764 } finally {
2765 Binder.restoreCallingIdentity(identity);
2766 }
Ta-wei Yen87c49842016-05-13 21:19:52 -07002767 }
2768
2769 @Override
Ta-wei Yenb6929602016-05-24 15:48:27 -07002770 public void disableVisualVoicemailSmsFilter(String callingPackage, int subId) {
2771 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002772
2773 final long identity = Binder.clearCallingIdentity();
2774 try {
2775 VisualVoicemailSmsFilterConfig.disableVisualVoicemailSmsFilter(
Nazanin Bakhshif782e562018-12-11 15:15:39 -08002776 mApp, callingPackage, subId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002777 } finally {
2778 Binder.restoreCallingIdentity(identity);
2779 }
Ta-wei Yen87c49842016-05-13 21:19:52 -07002780 }
2781
2782 @Override
Ta-wei Yenb6929602016-05-24 15:48:27 -07002783 public VisualVoicemailSmsFilterSettings getVisualVoicemailSmsFilterSettings(
2784 String callingPackage, int subId) {
2785 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002786
2787 final long identity = Binder.clearCallingIdentity();
2788 try {
2789 return VisualVoicemailSmsFilterConfig.getVisualVoicemailSmsFilterSettings(
Nazanin Bakhshif782e562018-12-11 15:15:39 -08002790 mApp, callingPackage, subId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002791 } finally {
2792 Binder.restoreCallingIdentity(identity);
2793 }
Ta-wei Yen87c49842016-05-13 21:19:52 -07002794 }
2795
2796 @Override
Ta-wei Yen30a69c82016-12-27 14:52:32 -08002797 public VisualVoicemailSmsFilterSettings getActiveVisualVoicemailSmsFilterSettings(int subId) {
Brad Ebinger35c841c2018-10-01 10:40:55 -07002798 enforceReadPrivilegedPermission("getActiveVisualVoicemailSmsFilterSettings");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002799
2800 final long identity = Binder.clearCallingIdentity();
2801 try {
2802 return VisualVoicemailSmsFilterConfig.getActiveVisualVoicemailSmsFilterSettings(
Nazanin Bakhshif782e562018-12-11 15:15:39 -08002803 mApp, subId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002804 } finally {
2805 Binder.restoreCallingIdentity(identity);
2806 }
Ta-wei Yen30a69c82016-12-27 14:52:32 -08002807 }
2808
2809 @Override
2810 public void sendVisualVoicemailSmsForSubscriber(String callingPackage, int subId,
2811 String number, int port, String text, PendingIntent sentIntent) {
2812 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
Ta-wei Yen527a9c02017-01-06 15:29:25 -08002813 enforceVisualVoicemailPackage(callingPackage, subId);
Ta-wei Yen30a69c82016-12-27 14:52:32 -08002814 enforceSendSmsPermission();
Amit Mahajandccb3f12019-05-13 13:48:32 -07002815 SmsController smsController = PhoneFactory.getSmsController();
2816 smsController.sendVisualVoicemailSmsForSubscriber(callingPackage, subId, number, port, text,
2817 sentIntent);
Ta-wei Yen87c49842016-05-13 21:19:52 -07002818 }
Amit Mahajandccb3f12019-05-13 13:48:32 -07002819
Shishir Agrawal76d5da92014-11-09 16:17:25 -08002820 /**
fionaxu0152e512016-11-14 13:36:14 -08002821 * Sets the voice activation state of a given subId.
2822 */
2823 @Override
2824 public void setVoiceActivationState(int subId, int activationState) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08002825 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
2826 mApp, subId, "setVoiceActivationState");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002827
2828 final long identity = Binder.clearCallingIdentity();
2829 try {
2830 final Phone phone = getPhone(subId);
2831 if (phone != null) {
2832 phone.setVoiceActivationState(activationState);
2833 } else {
2834 loge("setVoiceActivationState fails with invalid subId: " + subId);
2835 }
2836 } finally {
2837 Binder.restoreCallingIdentity(identity);
fionaxu0152e512016-11-14 13:36:14 -08002838 }
2839 }
2840
2841 /**
2842 * Sets the data activation state of a given subId.
2843 */
2844 @Override
2845 public void setDataActivationState(int subId, int activationState) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08002846 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
2847 mApp, subId, "setDataActivationState");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002848
2849 final long identity = Binder.clearCallingIdentity();
2850 try {
2851 final Phone phone = getPhone(subId);
2852 if (phone != null) {
2853 phone.setDataActivationState(activationState);
2854 } else {
Taesu Leef8fbed92019-10-07 18:47:02 +09002855 loge("setDataActivationState fails with invalid subId: " + subId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002856 }
2857 } finally {
2858 Binder.restoreCallingIdentity(identity);
fionaxu0152e512016-11-14 13:36:14 -08002859 }
2860 }
2861
2862 /**
2863 * Returns the voice activation state of a given subId.
2864 */
2865 @Override
2866 public int getVoiceActivationState(int subId, String callingPackage) {
Brad Ebinger35c841c2018-10-01 10:40:55 -07002867 enforceReadPrivilegedPermission("getVoiceActivationState");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002868
fionaxu0152e512016-11-14 13:36:14 -08002869 final Phone phone = getPhone(subId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002870 final long identity = Binder.clearCallingIdentity();
2871 try {
2872 if (phone != null) {
2873 return phone.getVoiceActivationState();
2874 } else {
2875 return TelephonyManager.SIM_ACTIVATION_STATE_UNKNOWN;
2876 }
2877 } finally {
2878 Binder.restoreCallingIdentity(identity);
fionaxu0152e512016-11-14 13:36:14 -08002879 }
2880 }
2881
2882 /**
2883 * Returns the data activation state of a given subId.
2884 */
2885 @Override
2886 public int getDataActivationState(int subId, String callingPackage) {
Brad Ebinger35c841c2018-10-01 10:40:55 -07002887 enforceReadPrivilegedPermission("getDataActivationState");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002888
fionaxu0152e512016-11-14 13:36:14 -08002889 final Phone phone = getPhone(subId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002890 final long identity = Binder.clearCallingIdentity();
2891 try {
2892 if (phone != null) {
2893 return phone.getDataActivationState();
2894 } else {
2895 return TelephonyManager.SIM_ACTIVATION_STATE_UNKNOWN;
2896 }
2897 } finally {
2898 Binder.restoreCallingIdentity(identity);
fionaxu0152e512016-11-14 13:36:14 -08002899 }
2900 }
2901
2902 /**
Wink Saville36469e72014-06-11 15:17:00 -07002903 * Returns the unread count of voicemails for a subId
2904 */
Sanket Padawe356d7632015-06-22 14:03:32 -07002905 @Override
Philip P. Moltmann9797cbb2020-01-07 13:45:00 -08002906 public int getVoiceMessageCountForSubscriber(int subId, String callingPackage,
2907 String callingFeatureId) {
Brad Ebingerf7664ba2018-11-29 12:43:38 -08002908 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
Philip P. Moltmann9797cbb2020-01-07 13:45:00 -08002909 mApp, subId, callingPackage, callingFeatureId,
2910 "getVoiceMessageCountForSubscriber")) {
Brad Ebingerf7664ba2018-11-29 12:43:38 -08002911 return 0;
2912 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002913 final long identity = Binder.clearCallingIdentity();
2914 try {
2915 final Phone phone = getPhone(subId);
2916 if (phone != null) {
2917 return phone.getVoiceMessageCount();
2918 } else {
2919 return 0;
2920 }
2921 } finally {
2922 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07002923 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002924 }
2925
2926 /**
pkanwar8a4dcfb2017-01-19 13:43:16 -08002927 * returns true, if the device is in a state where both voice and data
2928 * are supported simultaneously. This can change based on location or network condition.
2929 */
2930 @Override
2931 public boolean isConcurrentVoiceAndDataAllowed(int subId) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002932 final long identity = Binder.clearCallingIdentity();
2933 try {
2934 final Phone phone = getPhone(subId);
2935 return (phone == null ? false : phone.isConcurrentVoiceAndDataAllowed());
2936 } finally {
2937 Binder.restoreCallingIdentity(identity);
2938 }
pkanwar8a4dcfb2017-01-19 13:43:16 -08002939 }
2940
2941 /**
fionaxu235cc5e2017-03-06 22:25:57 -08002942 * Send the dialer code if called from the current default dialer or the caller has
2943 * carrier privilege.
2944 * @param inputCode The dialer code to send
2945 */
2946 @Override
2947 public void sendDialerSpecialCode(String callingPackage, String inputCode) {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08002948 final Phone defaultPhone = getDefaultPhone();
fionaxu235cc5e2017-03-06 22:25:57 -08002949 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
Tyler Gunn77ee9382019-10-31 13:08:23 -07002950 TelecomManager tm = defaultPhone.getContext().getSystemService(TelecomManager.class);
2951 String defaultDialer = tm.getDefaultDialerPackage();
fionaxu235cc5e2017-03-06 22:25:57 -08002952 if (!TextUtils.equals(callingPackage, defaultDialer)) {
Shuo Qian6d927452019-12-05 11:40:37 -08002953 TelephonyPermissions.enforceCallingOrSelfCarrierPrivilege(mApp,
Jeff Davidson7e17e312018-02-13 18:17:36 -08002954 getDefaultSubscription(), "sendDialerSpecialCode");
fionaxu235cc5e2017-03-06 22:25:57 -08002955 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002956
2957 final long identity = Binder.clearCallingIdentity();
2958 try {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08002959 defaultPhone.sendDialerSpecialCode(inputCode);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002960 } finally {
2961 Binder.restoreCallingIdentity(identity);
2962 }
fionaxu235cc5e2017-03-06 22:25:57 -08002963 }
2964
Pengquan Menga1bb6272018-09-06 09:59:22 -07002965 @Override
2966 public int getNetworkSelectionMode(int subId) {
shilufc958392020-01-20 11:36:01 -08002967 TelephonyPermissions
2968 .enforeceCallingOrSelfReadPrecisePhoneStatePermissionOrCarrierPrivilege(
2969 mApp, subId, "getNetworkSelectionMode");
2970 final long identity = Binder.clearCallingIdentity();
2971 try {
2972 if (!isActiveSubscription(subId)) {
2973 return TelephonyManager.NETWORK_SELECTION_MODE_UNKNOWN;
2974 }
2975 return (int) sendRequest(CMD_GET_NETWORK_SELECTION_MODE, null /* argument */, subId);
2976 } finally {
2977 Binder.restoreCallingIdentity(identity);
Pengquan Menge92a50d2018-09-21 15:54:48 -07002978 }
Pengquan Menga1bb6272018-09-06 09:59:22 -07002979 }
2980
Brad Ebinger35c841c2018-10-01 10:40:55 -07002981 @Override
Sarah Chin5f0ecd72019-12-06 10:24:18 -08002982 public PhoneCapability getPhoneCapability(int subId, String callingPackage,
2983 String callingFeatureId) {
2984 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
2985 mApp, subId, callingPackage, callingFeatureId, "getPhoneCapability")) {
2986 return null;
2987 }
2988 final long identity = Binder.clearCallingIdentity();
2989 try {
2990 return mPhoneConfigurationManager.getStaticPhoneCapability();
2991 } finally {
2992 Binder.restoreCallingIdentity(identity);
2993 }
2994 }
2995
2996 @Override
Brad Ebingerb2b65522019-03-15 13:48:47 -07002997 public boolean isInEmergencySmsMode() {
2998 enforceReadPrivilegedPermission("isInEmergencySmsMode");
2999 final long identity = Binder.clearCallingIdentity();
3000 try {
3001 for (Phone phone : PhoneFactory.getPhones()) {
3002 if (phone.isInEmergencySmsMode()) {
3003 return true;
3004 }
3005 }
3006 } finally {
3007 Binder.restoreCallingIdentity(identity);
3008 }
3009 return false;
3010 }
3011
shilu366312e2019-12-17 09:28:10 -08003012 /**
3013 * Requires carrier privileges or READ_PRECISE_PHONE_STATE permission.
3014 * @param subId The subscription to use to check the configuration.
3015 * @param c The callback that will be used to send the result.
3016 */
Brad Ebingerb2b65522019-03-15 13:48:47 -07003017 @Override
Brad Ebinger9878b0b2018-11-08 17:43:22 -08003018 public void registerImsRegistrationCallback(int subId, IImsRegistrationCallback c)
3019 throws RemoteException {
shilu366312e2019-12-17 09:28:10 -08003020 //TODO: b/147498511 will add TelephonyPermissions#checkCallingOrSelfReadPrecisePhoneState
Brad Ebinger9878b0b2018-11-08 17:43:22 -08003021 enforceReadPrivilegedPermission("registerImsRegistrationCallback");
shilu366312e2019-12-17 09:28:10 -08003022
Brad Ebinger77b832e2019-10-17 17:03:22 -07003023 if (!ImsManager.isImsSupportedOnDevice(mApp)) {
3024 throw new ServiceSpecificException(ImsException.CODE_ERROR_UNSUPPORTED_OPERATION,
3025 "IMS not available on device.");
3026 }
Brad Ebinger35c841c2018-10-01 10:40:55 -07003027 final long token = Binder.clearCallingIdentity();
3028 try {
Brad Ebinger4ae57f92019-01-09 16:51:30 -08003029 // TODO: Refactor to remove ImsManager dependence and query through ImsPhone directly.
Nazanin Bakhshif782e562018-12-11 15:15:39 -08003030 ImsManager.getInstance(mApp, getSlotIndexOrException(subId))
Brad Ebinger35c841c2018-10-01 10:40:55 -07003031 .addRegistrationCallbackForSubscription(c, subId);
Brad Ebinger1ce9c432019-07-16 13:19:44 -07003032 } catch (ImsException e) {
3033 throw new ServiceSpecificException(e.getCode());
Brad Ebinger35c841c2018-10-01 10:40:55 -07003034 } finally {
3035 Binder.restoreCallingIdentity(token);
3036 }
3037 }
3038
shilu366312e2019-12-17 09:28:10 -08003039 /**
3040 * Requires carrier privileges or READ_PRECISE_PHONE_STATE permission.
3041 * @param subId The subscription to use to check the configuration.
3042 * @param c The callback that will be used to send the result.
3043 */
Brad Ebinger35c841c2018-10-01 10:40:55 -07003044 @Override
Brad Ebinger9878b0b2018-11-08 17:43:22 -08003045 public void unregisterImsRegistrationCallback(int subId, IImsRegistrationCallback c) {
shilu366312e2019-12-17 09:28:10 -08003046 //TODO: b/147498511 will add TelephonyPermissions#checkCallingOrSelfReadPrecisePhoneState
Brad Ebinger9878b0b2018-11-08 17:43:22 -08003047 enforceReadPrivilegedPermission("unregisterImsRegistrationCallback");
Brad Ebinger4ae57f92019-01-09 16:51:30 -08003048 if (!SubscriptionManager.isValidSubscriptionId(subId)) {
3049 throw new IllegalArgumentException("Invalid Subscription ID: " + subId);
3050 }
Meng Wangd20ad6b2019-09-19 17:37:13 -07003051 final long token = Binder.clearCallingIdentity();
3052 try {
3053 // TODO: Refactor to remove ImsManager dependence and query through ImsPhone.
3054 ImsManager.getInstance(mApp, getSlotIndexOrException(subId))
3055 .removeRegistrationCallbackForSubscription(c, subId);
3056 } catch (ImsException e) {
3057 Log.i(LOG_TAG, "unregisterImsRegistrationCallback: " + subId
3058 + "is inactive, ignoring unregister.");
3059 // If the subscription is no longer active, just return, since the callback
3060 // will already have been removed internally.
3061 } finally {
3062 Binder.restoreCallingIdentity(token);
3063 }
Brad Ebinger35c841c2018-10-01 10:40:55 -07003064 }
3065
Brad Ebinger774ba362019-10-22 17:36:18 -07003066 /**
3067 * Get the IMS service registration state for the MmTelFeature associated with this sub id.
3068 */
3069 @Override
3070 public void getImsMmTelRegistrationState(int subId, IIntegerConsumer consumer) {
3071 enforceReadPrivilegedPermission("getImsMmTelRegistrationState");
3072 if (!ImsManager.isImsSupportedOnDevice(mApp)) {
3073 throw new ServiceSpecificException(ImsException.CODE_ERROR_UNSUPPORTED_OPERATION,
3074 "IMS not available on device.");
3075 }
3076 final long token = Binder.clearCallingIdentity();
3077 try {
3078 Phone phone = getPhone(subId);
3079 if (phone == null) {
3080 Log.w(LOG_TAG, "getImsMmTelRegistrationState: called with an invalid subscription '"
3081 + subId + "'");
3082 throw new ServiceSpecificException(ImsException.CODE_ERROR_INVALID_SUBSCRIPTION);
3083 }
3084 phone.getImsRegistrationState(regState -> {
3085 try {
3086 consumer.accept((regState == null)
3087 ? RegistrationManager.REGISTRATION_STATE_NOT_REGISTERED : regState);
3088 } catch (RemoteException e) {
3089 // Ignore if the remote process is no longer available to call back.
3090 Log.w(LOG_TAG, "getImsMmTelRegistrationState: callback not available.");
3091 }
3092 });
3093 } finally {
3094 Binder.restoreCallingIdentity(token);
3095 }
3096 }
3097
3098 /**
3099 * Get the transport type for the IMS service registration state.
3100 */
3101 @Override
3102 public void getImsMmTelRegistrationTransportType(int subId, IIntegerConsumer consumer) {
shilu366312e2019-12-17 09:28:10 -08003103 //TODO: b/147498511 will add TelephonyPermissions#checkCallingOrSelfReadPrecisePhoneState
Brad Ebinger774ba362019-10-22 17:36:18 -07003104 enforceReadPrivilegedPermission("getImsMmTelRegistrationTransportType");
3105 if (!ImsManager.isImsSupportedOnDevice(mApp)) {
3106 throw new ServiceSpecificException(ImsException.CODE_ERROR_UNSUPPORTED_OPERATION,
3107 "IMS not available on device.");
3108 }
3109 final long token = Binder.clearCallingIdentity();
3110 try {
3111 Phone phone = getPhone(subId);
3112 if (phone == null) {
3113 Log.w(LOG_TAG, "getImsMmTelRegistrationState: called with an invalid subscription '"
3114 + subId + "'");
3115 throw new ServiceSpecificException(ImsException.CODE_ERROR_INVALID_SUBSCRIPTION);
3116 }
3117 phone.getImsRegistrationTech(regTech -> {
3118 // Convert registration tech from ImsRegistrationImplBase -> RegistrationManager
3119 int regTechConverted = (regTech == null)
3120 ? ImsRegistrationImplBase.REGISTRATION_TECH_NONE : regTech;
3121 regTechConverted = RegistrationManager.IMS_REG_TO_ACCESS_TYPE_MAP.get(
3122 regTechConverted);
3123 try {
3124 consumer.accept(regTechConverted);
3125 } catch (RemoteException e) {
3126 // Ignore if the remote process is no longer available to call back.
3127 Log.w(LOG_TAG, "getImsMmTelRegistrationState: callback not available.");
3128 }
3129 });
3130 } finally {
3131 Binder.restoreCallingIdentity(token);
3132 }
3133 }
3134
shilu366312e2019-12-17 09:28:10 -08003135 /**
3136 * Requires carrier privileges or READ_PRECISE_PHONE_STATE permission.
3137 * @param subId The subscription to use to check the configuration.
3138 * @param c The callback that will be used to send the result.
3139 */
Brad Ebinger35c841c2018-10-01 10:40:55 -07003140 @Override
Brad Ebinger9878b0b2018-11-08 17:43:22 -08003141 public void registerMmTelCapabilityCallback(int subId, IImsCapabilityCallback c)
3142 throws RemoteException {
shilu366312e2019-12-17 09:28:10 -08003143 //TODO: b/147498511 will add TelephonyPermissions#checkCallingOrSelfReadPrecisePhoneState
Brad Ebinger9878b0b2018-11-08 17:43:22 -08003144 enforceReadPrivilegedPermission("registerMmTelCapabilityCallback");
Brad Ebinger77b832e2019-10-17 17:03:22 -07003145 if (!ImsManager.isImsSupportedOnDevice(mApp)) {
3146 throw new ServiceSpecificException(ImsException.CODE_ERROR_UNSUPPORTED_OPERATION,
3147 "IMS not available on device.");
3148 }
Brad Ebinger35c841c2018-10-01 10:40:55 -07003149 // TODO: Refactor to remove ImsManager dependence and query through ImsPhone directly.
3150 final long token = Binder.clearCallingIdentity();
3151 try {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08003152 ImsManager.getInstance(mApp, getSlotIndexOrException(subId))
Brad Ebinger35c841c2018-10-01 10:40:55 -07003153 .addCapabilitiesCallbackForSubscription(c, subId);
Brad Ebinger1ce9c432019-07-16 13:19:44 -07003154 } catch (ImsException e) {
3155 throw new ServiceSpecificException(e.getCode());
Brad Ebinger35c841c2018-10-01 10:40:55 -07003156 } finally {
3157 Binder.restoreCallingIdentity(token);
3158 }
3159 }
3160
shilu366312e2019-12-17 09:28:10 -08003161 /**
3162 * Requires carrier privileges or READ_PRECISE_PHONE_STATE permission.
3163 * @param subId The subscription to use to check the configuration.
3164 * @param c The callback that will be used to send the result.
3165 */
Brad Ebinger35c841c2018-10-01 10:40:55 -07003166 @Override
Brad Ebinger9878b0b2018-11-08 17:43:22 -08003167 public void unregisterMmTelCapabilityCallback(int subId, IImsCapabilityCallback c) {
shilu366312e2019-12-17 09:28:10 -08003168 //TODO: b/147498511 will add TelephonyPermissions#checkCallingOrSelfReadPrecisePhoneState
Brad Ebinger9878b0b2018-11-08 17:43:22 -08003169 enforceReadPrivilegedPermission("unregisterMmTelCapabilityCallback");
Brad Ebinger4ae57f92019-01-09 16:51:30 -08003170 if (!SubscriptionManager.isValidSubscriptionId(subId)) {
3171 throw new IllegalArgumentException("Invalid Subscription ID: " + subId);
3172 }
Meng Wangd20ad6b2019-09-19 17:37:13 -07003173
3174 final long token = Binder.clearCallingIdentity();
3175 try {
3176 // TODO: Refactor to remove ImsManager dependence and query through ImsPhone.
3177 ImsManager.getInstance(mApp, getSlotIndexOrException(subId))
Brad Ebinger4ae57f92019-01-09 16:51:30 -08003178 .removeCapabilitiesCallbackForSubscription(c, subId);
Meng Wangd20ad6b2019-09-19 17:37:13 -07003179 } catch (ImsException e) {
3180 Log.i(LOG_TAG, "unregisterMmTelCapabilityCallback: " + subId
3181 + "is inactive, ignoring unregister.");
3182 // If the subscription is no longer active, just return, since the callback
3183 // will already have been removed internally.
3184 } finally {
3185 Binder.restoreCallingIdentity(token);
3186 }
Brad Ebinger35c841c2018-10-01 10:40:55 -07003187 }
3188
3189 @Override
Brad Ebinger9878b0b2018-11-08 17:43:22 -08003190 public boolean isCapable(int subId, int capability, int regTech) {
3191 enforceReadPrivilegedPermission("isCapable");
Brad Ebinger35c841c2018-10-01 10:40:55 -07003192 // TODO: Refactor to remove ImsManager dependence and query through ImsPhone directly.
3193 final long token = Binder.clearCallingIdentity();
3194 try {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08003195 return ImsManager.getInstance(mApp,
Brad Ebinger35c841c2018-10-01 10:40:55 -07003196 getSlotIndexOrException(subId)).queryMmTelCapability(capability, regTech);
Brad Ebinger1ce9c432019-07-16 13:19:44 -07003197 } catch (com.android.ims.ImsException e) {
Brad Ebinger35c841c2018-10-01 10:40:55 -07003198 Log.w(LOG_TAG, "IMS isCapable - service unavailable: " + e.getMessage());
3199 return false;
Brad Ebinger1ce9c432019-07-16 13:19:44 -07003200 } catch (ImsException e) {
Brad Ebinger6b5ac222019-02-04 14:36:52 -08003201 Log.i(LOG_TAG, "isCapable: " + subId + " is inactive, returning false.");
3202 return false;
Brad Ebinger35c841c2018-10-01 10:40:55 -07003203 } finally {
3204 Binder.restoreCallingIdentity(token);
3205 }
3206 }
3207
3208 @Override
Brad Ebinger9878b0b2018-11-08 17:43:22 -08003209 public boolean isAvailable(int subId, int capability, int regTech) {
3210 enforceReadPrivilegedPermission("isAvailable");
Brad Ebinger35c841c2018-10-01 10:40:55 -07003211 final long token = Binder.clearCallingIdentity();
3212 try {
3213 Phone phone = getPhone(subId);
3214 if (phone == null) return false;
3215 return phone.isImsCapabilityAvailable(capability, regTech);
3216 } finally {
3217 Binder.restoreCallingIdentity(token);
3218 }
3219 }
3220
Brad Ebinger77b832e2019-10-17 17:03:22 -07003221 /**
3222 * Determines if the MmTel feature capability is supported by the carrier configuration for this
3223 * subscription.
3224 * @param subId The subscription to use to check the configuration.
3225 * @param callback The callback that will be used to send the result.
3226 * @param capability The MmTelFeature capability that will be used to send the result.
3227 * @param transportType The transport type of the MmTelFeature capability.
3228 */
3229 @Override
3230 public void isMmTelCapabilitySupported(int subId, IIntegerConsumer callback, int capability,
3231 int transportType) {
3232 enforceReadPrivilegedPermission("isMmTelCapabilitySupported");
3233 if (!ImsManager.isImsSupportedOnDevice(mApp)) {
3234 throw new ServiceSpecificException(ImsException.CODE_ERROR_UNSUPPORTED_OPERATION,
3235 "IMS not available on device.");
3236 }
3237 final long token = Binder.clearCallingIdentity();
3238 try {
3239 int slotId = getSlotIndex(subId);
3240 if (slotId <= SubscriptionManager.INVALID_SIM_SLOT_INDEX) {
3241 Log.w(LOG_TAG, "isMmTelCapabilitySupported: called with an inactive subscription '"
3242 + subId + "'");
3243 throw new ServiceSpecificException(ImsException.CODE_ERROR_INVALID_SUBSCRIPTION);
3244 }
3245 ImsManager.getInstance(mApp, slotId).isSupported(capability,
3246 transportType, aBoolean -> {
3247 try {
3248 callback.accept((aBoolean == null) ? 0 : (aBoolean ? 1 : 0));
3249 } catch (RemoteException e) {
3250 Log.w(LOG_TAG, "isMmTelCapabilitySupported: remote caller is not "
3251 + "running. Ignore");
3252 }
3253 });
3254 } finally {
3255 Binder.restoreCallingIdentity(token);
3256 }
3257 }
3258
shilu366312e2019-12-17 09:28:10 -08003259 /**
3260 * Requires carrier privileges or READ_PRECISE_PHONE_STATE permission.
3261 * @param subId The subscription to use to check the configuration.
3262 */
Brad Ebinger35c841c2018-10-01 10:40:55 -07003263 @Override
3264 public boolean isAdvancedCallingSettingEnabled(int subId) {
shilu366312e2019-12-17 09:28:10 -08003265 //TODO: b/147498511 will add TelephonyPermissions#checkCallingOrSelfReadPrecisePhoneState
3266 enforceReadPrivilegedPermission("isAdvancedCallingSettingEnabled");
3267
Brad Ebinger35c841c2018-10-01 10:40:55 -07003268 // TODO: Refactor to remove ImsManager dependence and query through ImsPhone directly.
3269 final long token = Binder.clearCallingIdentity();
3270 try {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08003271 return ImsManager.getInstance(mApp,
Brad Ebinger35c841c2018-10-01 10:40:55 -07003272 getSlotIndexOrException(subId)).isEnhanced4gLteModeSettingEnabledByUser();
Brad Ebinger1ce9c432019-07-16 13:19:44 -07003273 } catch (ImsException e) {
3274 throw new ServiceSpecificException(e.getCode());
Brad Ebinger35c841c2018-10-01 10:40:55 -07003275 } finally {
3276 Binder.restoreCallingIdentity(token);
3277 }
3278 }
3279
3280 @Override
Brad Ebinger1c162042019-02-21 14:49:10 -08003281 public void setAdvancedCallingSettingEnabled(int subId, boolean isEnabled) {
Brad Ebinger35c841c2018-10-01 10:40:55 -07003282 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(mApp, subId,
Brad Ebinger1c162042019-02-21 14:49:10 -08003283 "setAdvancedCallingSettingEnabled");
Brad Ebinger35c841c2018-10-01 10:40:55 -07003284 final long identity = Binder.clearCallingIdentity();
3285 try {
3286 // TODO: Refactor to remove ImsManager dependence and query through ImsPhone directly.
Nazanin Bakhshif782e562018-12-11 15:15:39 -08003287 ImsManager.getInstance(mApp,
Brad Ebinger35c841c2018-10-01 10:40:55 -07003288 getSlotIndexOrException(subId)).setEnhanced4gLteModeSetting(isEnabled);
Brad Ebinger1ce9c432019-07-16 13:19:44 -07003289 } catch (ImsException e) {
3290 throw new ServiceSpecificException(e.getCode());
Brad Ebinger35c841c2018-10-01 10:40:55 -07003291 } finally {
3292 Binder.restoreCallingIdentity(identity);
3293 }
3294 }
3295
shilu366312e2019-12-17 09:28:10 -08003296 /**
3297 * Requires carrier privileges or READ_PRECISE_PHONE_STATE permission.
3298 * @param subId The subscription to use to check the configuration.
3299 */
Brad Ebinger35c841c2018-10-01 10:40:55 -07003300 @Override
Brad Ebinger9878b0b2018-11-08 17:43:22 -08003301 public boolean isVtSettingEnabled(int subId) {
shilu366312e2019-12-17 09:28:10 -08003302 //TODO: b/147498511 will add TelephonyPermissions#checkCallingOrSelfReadPrecisePhoneState
Brad Ebinger9878b0b2018-11-08 17:43:22 -08003303 enforceReadPrivilegedPermission("isVtSettingEnabled");
Brad Ebinger35c841c2018-10-01 10:40:55 -07003304 final long identity = Binder.clearCallingIdentity();
3305 try {
3306 // TODO: Refactor to remove ImsManager dependence and query through ImsPhone directly.
Brad Ebinger1ce9c432019-07-16 13:19:44 -07003307 return ImsManager.getInstance(mApp, getSlotIndexOrException(subId)).isVtEnabledByUser();
3308 } catch (ImsException e) {
3309 throw new ServiceSpecificException(e.getCode());
Brad Ebinger35c841c2018-10-01 10:40:55 -07003310 } finally {
3311 Binder.restoreCallingIdentity(identity);
3312 }
3313 }
3314
3315 @Override
Brad Ebinger1c162042019-02-21 14:49:10 -08003316 public void setVtSettingEnabled(int subId, boolean isEnabled) {
Brad Ebinger35c841c2018-10-01 10:40:55 -07003317 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(mApp, subId,
Brad Ebinger1c162042019-02-21 14:49:10 -08003318 "setVtSettingEnabled");
Brad Ebinger35c841c2018-10-01 10:40:55 -07003319 final long identity = Binder.clearCallingIdentity();
3320 try {
3321 // TODO: Refactor to remove ImsManager dependence and query through ImsPhone directly.
Nazanin Bakhshif782e562018-12-11 15:15:39 -08003322 ImsManager.getInstance(mApp, getSlotIndexOrException(subId)).setVtSetting(isEnabled);
Brad Ebinger1ce9c432019-07-16 13:19:44 -07003323 } catch (ImsException e) {
3324 throw new ServiceSpecificException(e.getCode());
Brad Ebinger35c841c2018-10-01 10:40:55 -07003325 } finally {
3326 Binder.restoreCallingIdentity(identity);
3327 }
3328 }
3329
shilu366312e2019-12-17 09:28:10 -08003330 /**
3331 * Requires carrier privileges or READ_PRECISE_PHONE_STATE permission.
3332 * @param subId The subscription to use to check the configuration.
3333 */
Brad Ebinger35c841c2018-10-01 10:40:55 -07003334 @Override
3335 public boolean isVoWiFiSettingEnabled(int subId) {
shilu366312e2019-12-17 09:28:10 -08003336 //TODO: b/147498511 will add TelephonyPermissions#checkCallingOrSelfReadPrecisePhoneState
Brad Ebinger35c841c2018-10-01 10:40:55 -07003337 enforceReadPrivilegedPermission("isVoWiFiSettingEnabled");
3338 final long identity = Binder.clearCallingIdentity();
3339 try {
3340 // TODO: Refactor to remove ImsManager dependence and query through ImsPhone directly.
Nazanin Bakhshif782e562018-12-11 15:15:39 -08003341 return ImsManager.getInstance(mApp,
Brad Ebinger35c841c2018-10-01 10:40:55 -07003342 getSlotIndexOrException(subId)).isWfcEnabledByUser();
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(identity);
3347 }
3348 }
3349
3350 @Override
Brad Ebinger1c162042019-02-21 14:49:10 -08003351 public void setVoWiFiSettingEnabled(int subId, boolean isEnabled) {
Brad Ebinger35c841c2018-10-01 10:40:55 -07003352 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(mApp, subId,
Brad Ebinger1c162042019-02-21 14:49:10 -08003353 "setVoWiFiSettingEnabled");
Brad Ebinger35c841c2018-10-01 10:40:55 -07003354 final long identity = Binder.clearCallingIdentity();
3355 try {
3356 // TODO: Refactor to remove ImsManager dependence and query through ImsPhone directly.
Nazanin Bakhshif782e562018-12-11 15:15:39 -08003357 ImsManager.getInstance(mApp, getSlotIndexOrException(subId)).setWfcSetting(isEnabled);
Brad Ebinger1ce9c432019-07-16 13:19:44 -07003358 } catch (ImsException e) {
3359 throw new ServiceSpecificException(e.getCode());
Brad Ebinger35c841c2018-10-01 10:40:55 -07003360 } finally {
3361 Binder.restoreCallingIdentity(identity);
3362 }
3363 }
3364
shilu366312e2019-12-17 09:28:10 -08003365 /**
3366 * Requires carrier privileges or READ_PRECISE_PHONE_STATE permission.
3367 * @param subId The subscription to use to check the configuration.
3368 */
Brad Ebinger35c841c2018-10-01 10:40:55 -07003369 @Override
3370 public boolean isVoWiFiRoamingSettingEnabled(int subId) {
shilu366312e2019-12-17 09:28:10 -08003371 //TODO: b/147498511 will add TelephonyPermissions#checkCallingOrSelfReadPrecisePhoneState
Brad Ebinger35c841c2018-10-01 10:40:55 -07003372 enforceReadPrivilegedPermission("isVoWiFiRoamingSettingEnabled");
3373 final long identity = Binder.clearCallingIdentity();
3374 try {
3375 // TODO: Refactor to remove ImsManager dependence and query through ImsPhone directly.
Nazanin Bakhshif782e562018-12-11 15:15:39 -08003376 return ImsManager.getInstance(mApp,
Brad Ebinger35c841c2018-10-01 10:40:55 -07003377 getSlotIndexOrException(subId)).isWfcRoamingEnabledByUser();
Brad Ebinger1ce9c432019-07-16 13:19:44 -07003378 } catch (ImsException e) {
3379 throw new ServiceSpecificException(e.getCode());
Brad Ebinger35c841c2018-10-01 10:40:55 -07003380 } finally {
3381 Binder.restoreCallingIdentity(identity);
3382 }
3383 }
3384
3385 @Override
Brad Ebinger1c162042019-02-21 14:49:10 -08003386 public void setVoWiFiRoamingSettingEnabled(int subId, boolean isEnabled) {
Brad Ebinger35c841c2018-10-01 10:40:55 -07003387 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(mApp, subId,
Brad Ebinger1c162042019-02-21 14:49:10 -08003388 "setVoWiFiRoamingSettingEnabled");
Brad Ebinger35c841c2018-10-01 10:40:55 -07003389 final long identity = Binder.clearCallingIdentity();
3390 try {
3391 // TODO: Refactor to remove ImsManager dependence and query through ImsPhone directly.
Nazanin Bakhshif782e562018-12-11 15:15:39 -08003392 ImsManager.getInstance(mApp,
Brad Ebinger35c841c2018-10-01 10:40:55 -07003393 getSlotIndexOrException(subId)).setWfcRoamingSetting(isEnabled);
Brad Ebinger1ce9c432019-07-16 13:19:44 -07003394 } catch (ImsException e) {
3395 throw new ServiceSpecificException(e.getCode());
Brad Ebinger35c841c2018-10-01 10:40:55 -07003396 } finally {
3397 Binder.restoreCallingIdentity(identity);
3398 }
3399 }
3400
3401 @Override
3402 public void setVoWiFiNonPersistent(int subId, boolean isCapable, int mode) {
3403 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(mApp, subId,
3404 "setVoWiFiNonPersistent");
3405 final long identity = Binder.clearCallingIdentity();
3406 try {
3407 // TODO: Refactor to remove ImsManager dependence and query through ImsPhone directly.
Nazanin Bakhshif782e562018-12-11 15:15:39 -08003408 ImsManager.getInstance(mApp,
Brad Ebinger2d29c012019-05-07 18:33:46 -07003409 getSlotIndexOrException(subId)).setWfcNonPersistent(isCapable, mode);
Brad Ebinger1ce9c432019-07-16 13:19:44 -07003410 } catch (ImsException e) {
3411 throw new ServiceSpecificException(e.getCode());
Brad Ebinger35c841c2018-10-01 10:40:55 -07003412 } finally {
3413 Binder.restoreCallingIdentity(identity);
3414 }
3415 }
3416
shilu366312e2019-12-17 09:28:10 -08003417 /**
3418 * Requires carrier privileges or READ_PRECISE_PHONE_STATE permission.
3419 * @param subId The subscription to use to check the configuration.
3420 */
Brad Ebinger35c841c2018-10-01 10:40:55 -07003421 @Override
3422 public int getVoWiFiModeSetting(int subId) {
shilu366312e2019-12-17 09:28:10 -08003423 //TODO: b/147498511 will add TelephonyPermissions#checkCallingOrSelfReadPrecisePhoneState
Brad Ebinger35c841c2018-10-01 10:40:55 -07003424 enforceReadPrivilegedPermission("getVoWiFiModeSetting");
3425 final long identity = Binder.clearCallingIdentity();
3426 try {
3427 // TODO: Refactor to remove ImsManager dependence and query through ImsPhone directly.
Nazanin Bakhshif782e562018-12-11 15:15:39 -08003428 return ImsManager.getInstance(mApp,
Brad Ebinger35c841c2018-10-01 10:40:55 -07003429 getSlotIndexOrException(subId)).getWfcMode(false /*isRoaming*/);
Brad Ebinger1ce9c432019-07-16 13:19:44 -07003430 } catch (ImsException e) {
3431 throw new ServiceSpecificException(e.getCode());
Brad Ebinger35c841c2018-10-01 10:40:55 -07003432 } finally {
3433 Binder.restoreCallingIdentity(identity);
3434 }
3435 }
3436
3437 @Override
3438 public void setVoWiFiModeSetting(int subId, int mode) {
3439 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(mApp, subId,
3440 "setVoWiFiModeSetting");
3441 final long identity = Binder.clearCallingIdentity();
3442 try {
3443 // TODO: Refactor to remove ImsManager dependence and query through ImsPhone directly.
Nazanin Bakhshif782e562018-12-11 15:15:39 -08003444 ImsManager.getInstance(mApp,
Brad Ebinger35c841c2018-10-01 10:40:55 -07003445 getSlotIndexOrException(subId)).setWfcMode(mode, false /*isRoaming*/);
Brad Ebinger1ce9c432019-07-16 13:19:44 -07003446 } catch (ImsException e) {
3447 throw new ServiceSpecificException(e.getCode());
Brad Ebinger35c841c2018-10-01 10:40:55 -07003448 } finally {
3449 Binder.restoreCallingIdentity(identity);
3450 }
3451 }
3452
3453 @Override
3454 public int getVoWiFiRoamingModeSetting(int subId) {
3455 enforceReadPrivilegedPermission("getVoWiFiRoamingModeSetting");
3456 final long identity = Binder.clearCallingIdentity();
3457 try {
3458 // TODO: Refactor to remove ImsManager dependence and query through ImsPhone directly.
Nazanin Bakhshif782e562018-12-11 15:15:39 -08003459 return ImsManager.getInstance(mApp,
Brad Ebinger35c841c2018-10-01 10:40:55 -07003460 getSlotIndexOrException(subId)).getWfcMode(true /*isRoaming*/);
Brad Ebinger1ce9c432019-07-16 13:19:44 -07003461 } catch (ImsException e) {
3462 throw new ServiceSpecificException(e.getCode());
Brad Ebinger35c841c2018-10-01 10:40:55 -07003463 } finally {
3464 Binder.restoreCallingIdentity(identity);
3465 }
3466 }
3467
3468 @Override
3469 public void setVoWiFiRoamingModeSetting(int subId, int mode) {
3470 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(mApp, subId,
3471 "setVoWiFiRoamingModeSetting");
3472 final long identity = Binder.clearCallingIdentity();
3473 try {
3474 // TODO: Refactor to remove ImsManager dependence and query through ImsPhone directly.
Nazanin Bakhshif782e562018-12-11 15:15:39 -08003475 ImsManager.getInstance(mApp,
Brad Ebinger35c841c2018-10-01 10:40:55 -07003476 getSlotIndexOrException(subId)).setWfcMode(mode, true /*isRoaming*/);
Brad Ebinger1ce9c432019-07-16 13:19:44 -07003477 } catch (ImsException e) {
3478 throw new ServiceSpecificException(e.getCode());
Brad Ebinger35c841c2018-10-01 10:40:55 -07003479 } finally {
3480 Binder.restoreCallingIdentity(identity);
3481 }
3482 }
3483
3484 @Override
3485 public void setRttCapabilitySetting(int subId, boolean isEnabled) {
3486 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(mApp, subId,
3487 "setRttCapabilityEnabled");
3488 final long identity = Binder.clearCallingIdentity();
3489 try {
3490 // TODO: Refactor to remove ImsManager dependence and query through ImsPhone directly.
Brad Ebinger1ce9c432019-07-16 13:19:44 -07003491 ImsManager.getInstance(mApp, getSlotIndexOrException(subId)).setRttEnabled(isEnabled);
3492 } catch (ImsException e) {
3493 throw new ServiceSpecificException(e.getCode());
Brad Ebinger35c841c2018-10-01 10:40:55 -07003494 } finally {
3495 Binder.restoreCallingIdentity(identity);
3496 }
3497 }
3498
shilu366312e2019-12-17 09:28:10 -08003499 /**
3500 * Requires carrier privileges or READ_PRECISE_PHONE_STATE permission.
3501 * @param subId The subscription to use to check the configuration.
3502 */
Brad Ebinger35c841c2018-10-01 10:40:55 -07003503 @Override
3504 public boolean isTtyOverVolteEnabled(int subId) {
shilu366312e2019-12-17 09:28:10 -08003505 //TODO: b/147498511 will add TelephonyPermissions#checkCallingOrSelfReadPrecisePhoneState
Brad Ebinger35c841c2018-10-01 10:40:55 -07003506 enforceReadPrivilegedPermission("isTtyOverVolteEnabled");
3507 final long identity = Binder.clearCallingIdentity();
3508 try {
3509 // TODO: Refactor to remove ImsManager dependence and query through ImsPhone directly.
Nazanin Bakhshif782e562018-12-11 15:15:39 -08003510 return ImsManager.getInstance(mApp,
Brad Ebinger35c841c2018-10-01 10:40:55 -07003511 getSlotIndexOrException(subId)).isTtyOnVoLteCapable();
Brad Ebinger1ce9c432019-07-16 13:19:44 -07003512 } catch (ImsException e) {
3513 throw new ServiceSpecificException(e.getCode());
Brad Ebinger35c841c2018-10-01 10:40:55 -07003514 } finally {
3515 Binder.restoreCallingIdentity(identity);
3516 }
3517 }
3518
Brad Ebingerdf5b4f02018-10-31 11:24:17 -07003519 @Override
3520 public void registerImsProvisioningChangedCallback(int subId, IImsConfigCallback callback) {
3521 enforceReadPrivilegedPermission("registerImsProvisioningChangedCallback");
3522 final long identity = Binder.clearCallingIdentity();
3523 try {
Brad Ebinger6cf0f652020-01-16 11:21:18 -08003524 if (!isImsAvailableOnDevice()) {
3525 throw new ServiceSpecificException(ImsException.CODE_ERROR_UNSUPPORTED_OPERATION,
3526 "IMS not available on device.");
Peter Wang050bb052020-01-13 23:33:09 -08003527 }
Brad Ebingerdf5b4f02018-10-31 11:24:17 -07003528 // TODO: Refactor to remove ImsManager dependence and query through ImsPhone directly.
Nazanin Bakhshif782e562018-12-11 15:15:39 -08003529 ImsManager.getInstance(mApp, getSlotIndexOrException(subId))
Brad Ebinger4ae57f92019-01-09 16:51:30 -08003530 .addProvisioningCallbackForSubscription(callback, subId);
Brad Ebinger1ce9c432019-07-16 13:19:44 -07003531 } catch (ImsException e) {
3532 throw new ServiceSpecificException(e.getCode());
Brad Ebingerdf5b4f02018-10-31 11:24:17 -07003533 } finally {
3534 Binder.restoreCallingIdentity(identity);
3535 }
3536 }
3537
3538 @Override
3539 public void unregisterImsProvisioningChangedCallback(int subId, IImsConfigCallback callback) {
3540 enforceReadPrivilegedPermission("unregisterImsProvisioningChangedCallback");
3541 final long identity = Binder.clearCallingIdentity();
Brad Ebinger4ae57f92019-01-09 16:51:30 -08003542 if (!SubscriptionManager.isValidSubscriptionId(subId)) {
3543 throw new IllegalArgumentException("Invalid Subscription ID: " + subId);
3544 }
Brad Ebingerdf5b4f02018-10-31 11:24:17 -07003545 try {
3546 // TODO: Refactor to remove ImsManager dependence and query through ImsPhone directly.
Nazanin Bakhshif782e562018-12-11 15:15:39 -08003547 ImsManager.getInstance(mApp, getSlotIndexOrException(subId))
Brad Ebinger4ae57f92019-01-09 16:51:30 -08003548 .removeProvisioningCallbackForSubscription(callback, subId);
Brad Ebinger1ce9c432019-07-16 13:19:44 -07003549 } catch (ImsException e) {
Brad Ebinger4ae57f92019-01-09 16:51:30 -08003550 Log.i(LOG_TAG, "unregisterImsProvisioningChangedCallback: " + subId
3551 + "is inactive, ignoring unregister.");
3552 // If the subscription is no longer active, just return, since the callback will already
3553 // have been removed internally.
Brad Ebingerdf5b4f02018-10-31 11:24:17 -07003554 } finally {
3555 Binder.restoreCallingIdentity(identity);
3556 }
3557 }
3558
allenwtsu99c623b2020-01-03 18:24:23 +08003559
3560 private void checkModifyPhoneStatePermission(int subId, String message) {
3561 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(mApp, subId,
3562 message);
3563 }
3564
3565 private boolean isImsProvisioningRequired(int subId, int capability,
3566 boolean isMmtelCapability) {
3567 Phone phone = getPhone(subId);
3568 if (phone == null) {
3569 loge("phone instance null for subid " + subId);
3570 return false;
3571 }
3572 if (isMmtelCapability) {
3573 if (!doesImsCapabilityRequireProvisioning(phone.getContext(), subId, capability)) {
3574 return false;
3575 }
3576 } else {
3577 if (!doesRcsCapabilityRequireProvisioning(phone.getContext(), subId, capability)) {
3578 return false;
3579 }
3580 }
3581 return true;
3582 }
3583
3584 @Override
3585 public void setRcsProvisioningStatusForCapability(int subId, int capability,
3586 boolean isProvisioned) {
3587 checkModifyPhoneStatePermission(subId, "setRcsProvisioningStatusForCapability");
3588
3589 final long identity = Binder.clearCallingIdentity();
3590 try {
3591 // TODO: Refactor to remove ImsManager dependence and query through ImsPhone directly.
3592 if (!isImsProvisioningRequired(subId, capability, false)) {
3593 return;
3594 }
3595
3596 // this capability requires provisioning, route to the correct API.
3597 ImsManager ims = ImsManager.getInstance(mApp, getSlotIndex(subId));
3598 switch (capability) {
3599 case RcsFeature.RcsImsCapabilities.CAPABILITY_TYPE_OPTIONS_UCE:
3600 case RcsFeature.RcsImsCapabilities.CAPABILITY_TYPE_PRESENCE_UCE:
3601 ims.setEabProvisioned(isProvisioned);
3602 break;
3603 default: {
3604 throw new IllegalArgumentException("Tried to set provisioning for "
3605 + "rcs capability '" + capability + "', which does not require "
3606 + "provisioning.");
3607 }
3608 }
3609 } finally {
3610 Binder.restoreCallingIdentity(identity);
3611 }
3612
3613 }
3614
3615
3616 @Override
3617 public boolean getRcsProvisioningStatusForCapability(int subId, int capability) {
3618 enforceReadPrivilegedPermission("getRcsProvisioningStatusForCapability");
3619 final long identity = Binder.clearCallingIdentity();
3620 try {
3621 // TODO: Refactor to remove ImsManager dependence and query through ImsPhone directly.
3622 if (!isImsProvisioningRequired(subId, capability, false)) {
3623 return true;
3624 }
3625
3626 ImsManager ims = ImsManager.getInstance(mApp, getSlotIndex(subId));
3627 switch (capability) {
3628 case RcsFeature.RcsImsCapabilities.CAPABILITY_TYPE_OPTIONS_UCE:
3629 case RcsFeature.RcsImsCapabilities.CAPABILITY_TYPE_PRESENCE_UCE:
3630 return ims.isEabProvisionedOnDevice();
3631
3632 default: {
3633 throw new IllegalArgumentException("Tried to get rcs provisioning for "
3634 + "capability '" + capability + "', which does not require "
3635 + "provisioning.");
3636 }
3637 }
3638
3639 } finally {
3640 Binder.restoreCallingIdentity(identity);
3641 }
3642 }
3643
Brad Ebingerdf5b4f02018-10-31 11:24:17 -07003644 @Override
Brad Ebinger1c8542e2019-01-14 13:43:14 -08003645 public void setImsProvisioningStatusForCapability(int subId, int capability, int tech,
3646 boolean isProvisioned) {
3647 if (tech != ImsRegistrationImplBase.REGISTRATION_TECH_IWLAN
3648 && tech != ImsRegistrationImplBase.REGISTRATION_TECH_LTE) {
3649 throw new IllegalArgumentException("Registration technology '" + tech + "' is invalid");
3650 }
allenwtsu99c623b2020-01-03 18:24:23 +08003651 checkModifyPhoneStatePermission(subId, "setImsProvisioningStatusForCapability");
Brad Ebinger1c8542e2019-01-14 13:43:14 -08003652 final long identity = Binder.clearCallingIdentity();
3653 try {
3654 // TODO: Refactor to remove ImsManager dependence and query through ImsPhone directly.
allenwtsu99c623b2020-01-03 18:24:23 +08003655 if (!isImsProvisioningRequired(subId, capability, true)) {
Brad Ebinger1c8542e2019-01-14 13:43:14 -08003656 return;
3657 }
3658
3659 // this capability requires provisioning, route to the correct API.
3660 ImsManager ims = ImsManager.getInstance(mApp, getSlotIndex(subId));
3661 switch (capability) {
3662 case MmTelFeature.MmTelCapabilities.CAPABILITY_TYPE_VOICE: {
3663 if (tech == ImsRegistrationImplBase.REGISTRATION_TECH_LTE) {
3664 ims.setVolteProvisioned(isProvisioned);
3665 } else if (tech == ImsRegistrationImplBase.REGISTRATION_TECH_IWLAN) {
3666 ims.setWfcProvisioned(isProvisioned);
3667 }
3668 break;
3669 }
3670 case MmTelFeature.MmTelCapabilities.CAPABILITY_TYPE_VIDEO: {
3671 // There is currently no difference in VT provisioning type.
3672 ims.setVtProvisioned(isProvisioned);
3673 break;
3674 }
3675 case MmTelFeature.MmTelCapabilities.CAPABILITY_TYPE_UT: {
3676 // There is no "deprecated" UT provisioning mechanism through ImsConfig, so
3677 // change the capability of the feature instead if needed.
3678 if (isMmTelCapabilityProvisionedInCache(subId, capability, tech)
3679 == isProvisioned) {
3680 // No change in provisioning.
3681 return;
3682 }
3683 cacheMmTelCapabilityProvisioning(subId, capability, tech, isProvisioned);
3684 try {
3685 ims.changeMmTelCapability(capability, tech, isProvisioned);
Brad Ebinger1ce9c432019-07-16 13:19:44 -07003686 } catch (com.android.ims.ImsException e) {
Brad Ebinger1c8542e2019-01-14 13:43:14 -08003687 loge("setImsProvisioningStatusForCapability: couldn't change UT capability"
3688 + ", Exception" + e.getMessage());
3689 }
3690 break;
3691 }
3692 default: {
allenwtsu99c623b2020-01-03 18:24:23 +08003693 throw new IllegalArgumentException("Tried to set provisioning for "
3694 + "MmTel capability '" + capability + "', which does not require "
3695 + "provisioning. ");
Brad Ebinger1c8542e2019-01-14 13:43:14 -08003696 }
3697 }
3698
3699 } finally {
3700 Binder.restoreCallingIdentity(identity);
3701 }
3702 }
3703
3704 @Override
3705 public boolean getImsProvisioningStatusForCapability(int subId, int capability, int tech) {
3706 if (tech != ImsRegistrationImplBase.REGISTRATION_TECH_IWLAN
3707 && tech != ImsRegistrationImplBase.REGISTRATION_TECH_LTE) {
3708 throw new IllegalArgumentException("Registration technology '" + tech + "' is invalid");
3709 }
3710 enforceReadPrivilegedPermission("getProvisioningStatusForCapability");
3711 final long identity = Binder.clearCallingIdentity();
3712 try {
3713 // TODO: Refactor to remove ImsManager dependence and query through ImsPhone directly.
allenwtsu99c623b2020-01-03 18:24:23 +08003714 if (!isImsProvisioningRequired(subId, capability, true)) {
Brad Ebinger1c8542e2019-01-14 13:43:14 -08003715 return true;
3716 }
3717
3718 ImsManager ims = ImsManager.getInstance(mApp, getSlotIndex(subId));
3719 switch (capability) {
3720 case MmTelFeature.MmTelCapabilities.CAPABILITY_TYPE_VOICE: {
3721 if (tech == ImsRegistrationImplBase.REGISTRATION_TECH_LTE) {
3722 return ims.isVolteProvisionedOnDevice();
3723 } else if (tech == ImsRegistrationImplBase.REGISTRATION_TECH_IWLAN) {
3724 return ims.isWfcProvisionedOnDevice();
3725 }
3726 // This should never happen, since we are checking tech above to make sure it
3727 // is either LTE or IWLAN.
3728 throw new IllegalArgumentException("Invalid radio technology for voice "
3729 + "capability.");
3730 }
3731 case MmTelFeature.MmTelCapabilities.CAPABILITY_TYPE_VIDEO: {
3732 // There is currently no difference in VT provisioning type.
3733 return ims.isVtProvisionedOnDevice();
3734 }
3735 case MmTelFeature.MmTelCapabilities.CAPABILITY_TYPE_UT: {
3736 // There is no "deprecated" UT provisioning mechanism, so get from shared prefs.
3737 return isMmTelCapabilityProvisionedInCache(subId, capability, tech);
3738 }
3739 default: {
allenwtsu99c623b2020-01-03 18:24:23 +08003740 throw new IllegalArgumentException(
3741 "Tried to get provisioning for MmTel capability '" + capability
3742 + "', which does not require provisioning.");
Brad Ebinger1c8542e2019-01-14 13:43:14 -08003743 }
3744 }
3745
3746 } finally {
3747 Binder.restoreCallingIdentity(identity);
3748 }
3749 }
3750
3751 @Override
3752 public boolean isMmTelCapabilityProvisionedInCache(int subId, int capability, int tech) {
3753 if (tech != ImsRegistrationImplBase.REGISTRATION_TECH_IWLAN
3754 && tech != ImsRegistrationImplBase.REGISTRATION_TECH_LTE) {
3755 throw new IllegalArgumentException("Registration technology '" + tech + "' is invalid");
3756 }
3757 enforceReadPrivilegedPermission("isMmTelCapabilityProvisionedInCache");
3758 int provisionedBits = getMmTelCapabilityProvisioningBitfield(subId, tech);
3759 return (provisionedBits & capability) > 0;
3760 }
3761
3762 @Override
3763 public void cacheMmTelCapabilityProvisioning(int subId, int capability, int tech,
3764 boolean isProvisioned) {
3765 if (tech != ImsRegistrationImplBase.REGISTRATION_TECH_IWLAN
3766 && tech != ImsRegistrationImplBase.REGISTRATION_TECH_LTE) {
3767 throw new IllegalArgumentException("Registration technology '" + tech + "' is invalid");
3768 }
3769 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(mApp, subId,
3770 "setProvisioningStatusForCapability");
3771 int provisionedBits = getMmTelCapabilityProvisioningBitfield(subId, tech);
3772 // If the current provisioning status for capability already matches isProvisioned,
3773 // do nothing.
3774 if (((provisionedBits & capability) > 0) == isProvisioned) {
3775 return;
3776 }
3777 if (isProvisioned) {
3778 setMmTelCapabilityProvisioningBitfield(subId, tech, (provisionedBits | capability));
3779 } else {
3780 setMmTelCapabilityProvisioningBitfield(subId, tech, (provisionedBits & ~capability));
3781 }
3782 }
3783
3784 /**
3785 * @return the bitfield containing the MmTel provisioning for the provided subscription and
3786 * technology. The bitfield should mirror the bitfield defined by
3787 * {@link MmTelFeature.MmTelCapabilities.MmTelCapability}.
3788 */
3789 private int getMmTelCapabilityProvisioningBitfield(int subId, int tech) {
3790 String key = getMmTelProvisioningKey(subId, tech);
3791 // Default is no capabilities are provisioned.
3792 return mTelephonySharedPreferences.getInt(key, 0 /*default*/);
3793 }
3794
3795 /**
3796 * Sets the MmTel capability provisioning bitfield (defined by
3797 * {@link MmTelFeature.MmTelCapabilities.MmTelCapability}) for the subscription and
3798 * technology specified.
3799 *
3800 * Note: This is a synchronous command and should not be called on UI thread.
3801 */
3802 private void setMmTelCapabilityProvisioningBitfield(int subId, int tech, int newField) {
3803 final SharedPreferences.Editor editor = mTelephonySharedPreferences.edit();
3804 String key = getMmTelProvisioningKey(subId, tech);
3805 editor.putInt(key, newField);
3806 editor.commit();
3807 }
3808
3809 private static String getMmTelProvisioningKey(int subId, int tech) {
3810 // resulting key is provision_ims_mmtel_{subId}_{tech}
3811 return PREF_PROVISION_IMS_MMTEL_PREFIX + subId + "_" + tech;
3812 }
3813
3814 /**
3815 * Query CarrierConfig to see if the specified capability requires provisioning for the
3816 * carrier associated with the subscription id.
3817 */
3818 private boolean doesImsCapabilityRequireProvisioning(Context context, int subId,
3819 int capability) {
3820 CarrierConfigManager configManager = new CarrierConfigManager(context);
3821 PersistableBundle c = configManager.getConfigForSubId(subId);
3822 boolean requireUtProvisioning = c.getBoolean(
Brad Ebinger076903f2019-05-13 10:00:22 -07003823 CarrierConfigManager.KEY_CARRIER_SUPPORTS_SS_OVER_UT_BOOL, false)
Brad Ebinger1c8542e2019-01-14 13:43:14 -08003824 && c.getBoolean(CarrierConfigManager.KEY_CARRIER_UT_PROVISIONING_REQUIRED_BOOL,
3825 false);
3826 boolean requireVoiceVtProvisioning = c.getBoolean(
3827 CarrierConfigManager.KEY_CARRIER_VOLTE_PROVISIONING_REQUIRED_BOOL, false);
3828
3829 // First check to make sure that the capability requires provisioning.
3830 switch (capability) {
3831 case MmTelFeature.MmTelCapabilities.CAPABILITY_TYPE_VOICE:
3832 // intentional fallthrough
3833 case MmTelFeature.MmTelCapabilities.CAPABILITY_TYPE_VIDEO: {
3834 if (requireVoiceVtProvisioning) {
3835 // Voice and Video requires provisioning
3836 return true;
3837 }
3838 break;
3839 }
3840 case MmTelFeature.MmTelCapabilities.CAPABILITY_TYPE_UT: {
3841 if (requireUtProvisioning) {
3842 // UT requires provisioning
3843 return true;
3844 }
3845 break;
3846 }
3847 }
3848 return false;
3849 }
3850
allenwtsu99c623b2020-01-03 18:24:23 +08003851 private boolean doesRcsCapabilityRequireProvisioning(Context context, int subId,
3852 int capability) {
3853 CarrierConfigManager configManager = new CarrierConfigManager(context);
3854 PersistableBundle c = configManager.getConfigForSubId(subId);
3855
3856 boolean requireRcsProvisioning = c.getBoolean(
3857 CarrierConfigManager.KEY_CARRIER_RCS_PROVISIONING_REQUIRED_BOOL, false);
3858
3859 // First check to make sure that the capability requires provisioning.
3860 switch (capability) {
3861 case RcsFeature.RcsImsCapabilities.CAPABILITY_TYPE_PRESENCE_UCE:
3862 // intentional fallthrough
3863 case RcsFeature.RcsImsCapabilities.CAPABILITY_TYPE_OPTIONS_UCE: {
3864 if (requireRcsProvisioning) {
3865 // OPTION or PRESENCE requires provisioning
3866 return true;
3867 }
3868 break;
3869 }
3870 }
3871 return false;
3872 }
3873
Brad Ebinger1c8542e2019-01-14 13:43:14 -08003874 @Override
Brad Ebingerdf5b4f02018-10-31 11:24:17 -07003875 public int getImsProvisioningInt(int subId, int key) {
Brad Ebinger1c8542e2019-01-14 13:43:14 -08003876 if (!SubscriptionManager.isValidSubscriptionId(subId)) {
3877 throw new IllegalArgumentException("Invalid Subscription id '" + subId + "'");
3878 }
Brad Ebingerdf5b4f02018-10-31 11:24:17 -07003879 enforceReadPrivilegedPermission("getImsProvisioningInt");
3880 final long identity = Binder.clearCallingIdentity();
3881 try {
3882 // TODO: Refactor to remove ImsManager dependence and query through ImsPhone directly.
Brad Ebinger1c8542e2019-01-14 13:43:14 -08003883 int slotId = getSlotIndex(subId);
3884 if (slotId <= SubscriptionManager.INVALID_SIM_SLOT_INDEX) {
3885 Log.w(LOG_TAG, "getImsProvisioningInt: called with an inactive subscription '"
3886 + subId + "' for key:" + key);
3887 return ImsConfigImplBase.CONFIG_RESULT_UNKNOWN;
3888 }
3889 return ImsManager.getInstance(mApp, slotId).getConfigInterface().getConfigInt(key);
Brad Ebinger1ce9c432019-07-16 13:19:44 -07003890 } catch (com.android.ims.ImsException e) {
Brad Ebinger1c8542e2019-01-14 13:43:14 -08003891 Log.w(LOG_TAG, "getImsProvisioningInt: ImsService is not available for subscription '"
3892 + subId + "' for key:" + key);
3893 return ImsConfigImplBase.CONFIG_RESULT_UNKNOWN;
Brad Ebingerdf5b4f02018-10-31 11:24:17 -07003894 } finally {
3895 Binder.restoreCallingIdentity(identity);
3896 }
3897 }
3898
3899 @Override
3900 public String getImsProvisioningString(int subId, int key) {
Brad Ebinger1c8542e2019-01-14 13:43:14 -08003901 if (!SubscriptionManager.isValidSubscriptionId(subId)) {
3902 throw new IllegalArgumentException("Invalid Subscription id '" + subId + "'");
3903 }
Brad Ebingerdf5b4f02018-10-31 11:24:17 -07003904 enforceReadPrivilegedPermission("getImsProvisioningString");
3905 final long identity = Binder.clearCallingIdentity();
3906 try {
3907 // TODO: Refactor to remove ImsManager dependence and query through ImsPhone directly.
Brad Ebinger1c8542e2019-01-14 13:43:14 -08003908 int slotId = getSlotIndex(subId);
3909 if (slotId <= SubscriptionManager.INVALID_SIM_SLOT_INDEX) {
3910 Log.w(LOG_TAG, "getImsProvisioningString: called for an inactive subscription id '"
3911 + subId + "' for key:" + key);
3912 return ProvisioningManager.STRING_QUERY_RESULT_ERROR_GENERIC;
3913 }
3914 return ImsManager.getInstance(mApp, slotId).getConfigInterface().getConfigString(key);
Brad Ebinger1ce9c432019-07-16 13:19:44 -07003915 } catch (com.android.ims.ImsException e) {
Brad Ebinger1c8542e2019-01-14 13:43:14 -08003916 Log.w(LOG_TAG, "getImsProvisioningString: ImsService is not available for sub '"
3917 + subId + "' for key:" + key);
3918 return ProvisioningManager.STRING_QUERY_RESULT_ERROR_NOT_READY;
Brad Ebingerdf5b4f02018-10-31 11:24:17 -07003919 } finally {
3920 Binder.restoreCallingIdentity(identity);
3921 }
3922 }
3923
3924 @Override
3925 public int setImsProvisioningInt(int subId, int key, int value) {
Brad Ebinger1c8542e2019-01-14 13:43:14 -08003926 if (!SubscriptionManager.isValidSubscriptionId(subId)) {
3927 throw new IllegalArgumentException("Invalid Subscription id '" + subId + "'");
3928 }
Brad Ebinger3d0b34e2018-11-15 14:13:12 -08003929 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(mApp, subId,
3930 "setImsProvisioningInt");
Brad Ebingerdf5b4f02018-10-31 11:24:17 -07003931 final long identity = Binder.clearCallingIdentity();
3932 try {
3933 // TODO: Refactor to remove ImsManager dependence and query through ImsPhone directly.
Brad Ebinger1c8542e2019-01-14 13:43:14 -08003934 int slotId = getSlotIndex(subId);
3935 if (slotId <= SubscriptionManager.INVALID_SIM_SLOT_INDEX) {
3936 Log.w(LOG_TAG, "setImsProvisioningInt: called with an inactive subscription id '"
3937 + subId + "' for key:" + key);
3938 return ImsConfigImplBase.CONFIG_RESULT_FAILED;
3939 }
3940 return ImsManager.getInstance(mApp, slotId).getConfigInterface().setConfig(key, value);
Brad Ebinger1ce9c432019-07-16 13:19:44 -07003941 } catch (com.android.ims.ImsException e) {
Brad Ebinger1c8542e2019-01-14 13:43:14 -08003942 Log.w(LOG_TAG, "setImsProvisioningInt: ImsService unavailable for sub '" + subId
3943 + "' for key:" + key);
3944 return ImsConfigImplBase.CONFIG_RESULT_FAILED;
Brad Ebingerdf5b4f02018-10-31 11:24:17 -07003945 } finally {
3946 Binder.restoreCallingIdentity(identity);
3947 }
3948 }
3949
3950 @Override
3951 public int setImsProvisioningString(int subId, int key, String value) {
Brad Ebinger1c8542e2019-01-14 13:43:14 -08003952 if (!SubscriptionManager.isValidSubscriptionId(subId)) {
3953 throw new IllegalArgumentException("Invalid Subscription id '" + subId + "'");
3954 }
Brad Ebinger3d0b34e2018-11-15 14:13:12 -08003955 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(mApp, subId,
3956 "setImsProvisioningString");
Brad Ebingerdf5b4f02018-10-31 11:24:17 -07003957 final long identity = Binder.clearCallingIdentity();
3958 try {
3959 // TODO: Refactor to remove ImsManager dependence and query through ImsPhone directly.
Brad Ebinger1c8542e2019-01-14 13:43:14 -08003960 int slotId = getSlotIndex(subId);
3961 if (slotId <= SubscriptionManager.INVALID_SIM_SLOT_INDEX) {
3962 Log.w(LOG_TAG, "setImsProvisioningString: called with an inactive subscription id '"
3963 + subId + "' for key:" + key);
3964 return ImsConfigImplBase.CONFIG_RESULT_FAILED;
3965 }
3966 return ImsManager.getInstance(mApp, slotId).getConfigInterface().setConfig(key, value);
Brad Ebinger1ce9c432019-07-16 13:19:44 -07003967 } catch (com.android.ims.ImsException e) {
Brad Ebinger1c8542e2019-01-14 13:43:14 -08003968 Log.w(LOG_TAG, "setImsProvisioningString: ImsService unavailable for sub '" + subId
3969 + "' for key:" + key);
3970 return ImsConfigImplBase.CONFIG_RESULT_FAILED;
Brad Ebingerdf5b4f02018-10-31 11:24:17 -07003971 } finally {
3972 Binder.restoreCallingIdentity(identity);
3973 }
3974 }
3975
Brad Ebinger1ce9c432019-07-16 13:19:44 -07003976 private int getSlotIndexOrException(int subId) throws ImsException {
Brad Ebinger35c841c2018-10-01 10:40:55 -07003977 int slotId = SubscriptionManager.getSlotIndex(subId);
3978 if (!SubscriptionManager.isValidSlotIndex(slotId)) {
Brad Ebinger1ce9c432019-07-16 13:19:44 -07003979 throw new ImsException("Invalid Subscription Id, subId=" + subId,
3980 ImsException.CODE_ERROR_INVALID_SUBSCRIPTION);
Brad Ebinger35c841c2018-10-01 10:40:55 -07003981 }
3982 return slotId;
3983 }
3984
Brad Ebinger1c8542e2019-01-14 13:43:14 -08003985 private int getSlotIndex(int subId) {
3986 int slotId = SubscriptionManager.getSlotIndex(subId);
3987 if (!SubscriptionManager.isValidSlotIndex(slotId)) {
3988 return SubscriptionManager.INVALID_SIM_SLOT_INDEX;
3989 }
3990 return slotId;
3991 }
3992
Wink Saville36469e72014-06-11 15:17:00 -07003993 /**
Nathan Harold9042f0b2019-05-21 15:51:27 -07003994 * Returns the data network type for a subId; does not throw SecurityException.
Wink Saville36469e72014-06-11 15:17:00 -07003995 */
3996 @Override
Philip P. Moltmann9797cbb2020-01-07 13:45:00 -08003997 public int getNetworkTypeForSubscriber(int subId, String callingPackage,
3998 String callingFeatureId) {
Nathan Haroldef60dba2019-05-22 13:55:14 -07003999 final int targetSdk = getTargetSdk(callingPackage);
4000 if (targetSdk > android.os.Build.VERSION_CODES.Q) {
Philip P. Moltmann9797cbb2020-01-07 13:45:00 -08004001 return getDataNetworkTypeForSubscriber(subId, callingPackage, callingFeatureId);
Nathan Haroldef60dba2019-05-22 13:55:14 -07004002 } else if (targetSdk == android.os.Build.VERSION_CODES.Q
Nathan Harold9042f0b2019-05-21 15:51:27 -07004003 && !TelephonyPermissions.checkCallingOrSelfReadPhoneStateNoThrow(
Philip P. Moltmann9797cbb2020-01-07 13:45:00 -08004004 mApp, subId, callingPackage, callingFeatureId,
4005 "getNetworkTypeForSubscriber")) {
Robert Greenwalt36b23af2015-07-06 17:59:14 -07004006 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
4007 }
Robert Greenwalta5dcfcb2015-07-10 09:06:29 -07004008
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004009 final long identity = Binder.clearCallingIdentity();
4010 try {
4011 final Phone phone = getPhone(subId);
4012 if (phone != null) {
4013 return phone.getServiceState().getDataNetworkType();
4014 } else {
4015 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
4016 }
4017 } finally {
4018 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07004019 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07004020 }
4021
4022 /**
4023 * Returns the data network type
4024 */
4025 @Override
Philip P. Moltmann9797cbb2020-01-07 13:45:00 -08004026 public int getDataNetworkType(String callingPackage, String callingFeatureId) {
4027 return getDataNetworkTypeForSubscriber(getDefaultSubscription(), callingPackage,
4028 callingFeatureId);
Wink Saville36469e72014-06-11 15:17:00 -07004029 }
4030
4031 /**
4032 * Returns the data network type for a subId
4033 */
4034 @Override
Philip P. Moltmann9797cbb2020-01-07 13:45:00 -08004035 public int getDataNetworkTypeForSubscriber(int subId, String callingPackage,
4036 String callingFeatureId) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08004037 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
Philip P. Moltmann9797cbb2020-01-07 13:45:00 -08004038 mApp, subId, callingPackage, callingFeatureId,
4039 "getDataNetworkTypeForSubscriber")) {
Robert Greenwalt36b23af2015-07-06 17:59:14 -07004040 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
4041 }
4042
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004043 final long identity = Binder.clearCallingIdentity();
4044 try {
4045 final Phone phone = getPhone(subId);
4046 if (phone != null) {
4047 return phone.getServiceState().getDataNetworkType();
4048 } else {
4049 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
4050 }
4051 } finally {
4052 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07004053 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07004054 }
4055
4056 /**
Wink Saville36469e72014-06-11 15:17:00 -07004057 * Returns the Voice network type for a subId
4058 */
4059 @Override
Philip P. Moltmann9797cbb2020-01-07 13:45:00 -08004060 public int getVoiceNetworkTypeForSubscriber(int subId, String callingPackage,
4061 String callingFeatureId) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08004062 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
Philip P. Moltmann9797cbb2020-01-07 13:45:00 -08004063 mApp, subId, callingPackage, callingFeatureId,
4064 "getDataNetworkTypeForSubscriber")) {
Robert Greenwalta5dcfcb2015-07-10 09:06:29 -07004065 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
4066 }
4067
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004068 final long identity = Binder.clearCallingIdentity();
4069 try {
4070 final Phone phone = getPhone(subId);
4071 if (phone != null) {
4072 return phone.getServiceState().getVoiceNetworkType();
4073 } else {
4074 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
4075 }
4076 } finally {
4077 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07004078 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07004079 }
4080
4081 /**
4082 * @return true if a ICC card is present
4083 */
4084 public boolean hasIccCard() {
Wink Saville36469e72014-06-11 15:17:00 -07004085 // FIXME Make changes to pass defaultSimId of type int
Sanket Padawe13bac7b2017-03-20 15:04:47 -07004086 return hasIccCardUsingSlotIndex(mSubscriptionController.getSlotIndex(
4087 getDefaultSubscription()));
Wink Saville36469e72014-06-11 15:17:00 -07004088 }
4089
4090 /**
Sanket Padawe13bac7b2017-03-20 15:04:47 -07004091 * @return true if a ICC card is present for a slotIndex
Wink Saville36469e72014-06-11 15:17:00 -07004092 */
Sanket Padawe356d7632015-06-22 14:03:32 -07004093 @Override
Sanket Padawe13bac7b2017-03-20 15:04:47 -07004094 public boolean hasIccCardUsingSlotIndex(int slotIndex) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004095 final long identity = Binder.clearCallingIdentity();
4096 try {
4097 final Phone phone = PhoneFactory.getPhone(slotIndex);
4098 if (phone != null) {
4099 return phone.getIccCard().hasIccCard();
4100 } else {
4101 return false;
4102 }
4103 } finally {
4104 Binder.restoreCallingIdentity(identity);
Amit Mahajana6fc2a82015-01-06 11:53:51 -08004105 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07004106 }
4107
4108 /**
4109 * Return if the current radio is LTE on CDMA. This
4110 * is a tri-state return value as for a period of time
4111 * the mode may be unknown.
4112 *
Robert Greenwalt36b23af2015-07-06 17:59:14 -07004113 * @param callingPackage the name of the package making the call.
Santos Cordon7d4ddf62013-07-10 11:58:08 -07004114 * @return {@link Phone#LTE_ON_CDMA_UNKNOWN}, {@link Phone#LTE_ON_CDMA_FALSE}
Jake Hambye994d462014-02-03 13:10:13 -08004115 * or {@link Phone#LTE_ON_CDMA_TRUE}
Santos Cordon7d4ddf62013-07-10 11:58:08 -07004116 */
Robert Greenwalt36b23af2015-07-06 17:59:14 -07004117 @Override
Philip P. Moltmann9797cbb2020-01-07 13:45:00 -08004118 public int getLteOnCdmaMode(String callingPackage, String callingFeatureId) {
4119 return getLteOnCdmaModeForSubscriber(getDefaultSubscription(), callingPackage,
4120 callingFeatureId);
Wink Saville36469e72014-06-11 15:17:00 -07004121 }
4122
Sanket Padawe356d7632015-06-22 14:03:32 -07004123 @Override
Philip P. Moltmann9797cbb2020-01-07 13:45:00 -08004124 public int getLteOnCdmaModeForSubscriber(int subId, String callingPackage,
4125 String callingFeatureId) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08004126 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
Philip P. Moltmann9797cbb2020-01-07 13:45:00 -08004127 mApp, subId, callingPackage, callingFeatureId,
4128 "getLteOnCdmaModeForSubscriber")) {
Robert Greenwalt36b23af2015-07-06 17:59:14 -07004129 return PhoneConstants.LTE_ON_CDMA_UNKNOWN;
4130 }
4131
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004132 final long identity = Binder.clearCallingIdentity();
4133 try {
4134 final Phone phone = getPhone(subId);
4135 if (phone == null) {
4136 return PhoneConstants.LTE_ON_CDMA_UNKNOWN;
4137 } else {
4138 return phone.getLteOnCdmaMode();
4139 }
4140 } finally {
4141 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07004142 }
Wink Saville36469e72014-06-11 15:17:00 -07004143 }
4144
Wink Saville36469e72014-06-11 15:17:00 -07004145 /**
4146 * {@hide}
4147 * Returns Default subId, 0 in the case of single standby.
4148 */
Wink Savilleb564aae2014-10-23 10:18:09 -07004149 private int getDefaultSubscription() {
Wink Savilleac1bdfd2014-11-20 23:04:44 -08004150 return mSubscriptionController.getDefaultSubId();
Wink Saville36469e72014-06-11 15:17:00 -07004151 }
4152
Shishir Agrawala9f32182016-04-12 12:00:16 -07004153 private int getSlotForDefaultSubscription() {
4154 return mSubscriptionController.getPhoneId(getDefaultSubscription());
4155 }
4156
Wink Savilleb564aae2014-10-23 10:18:09 -07004157 private int getPreferredVoiceSubscription() {
Wink Savilleac1bdfd2014-11-20 23:04:44 -08004158 return mSubscriptionController.getDefaultVoiceSubId();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07004159 }
Ihab Awadf2177b72013-11-25 13:33:23 -08004160
Pengquan Menge92a50d2018-09-21 15:54:48 -07004161 private boolean isActiveSubscription(int subId) {
4162 return mSubscriptionController.isActiveSubId(subId);
4163 }
4164
Ihab Awadf2177b72013-11-25 13:33:23 -08004165 /**
4166 * @see android.telephony.TelephonyManager.WifiCallingChoices
4167 */
4168 public int getWhenToMakeWifiCalls() {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004169 final long identity = Binder.clearCallingIdentity();
4170 try {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08004171 return Settings.System.getInt(mApp.getContentResolver(),
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004172 Settings.System.WHEN_TO_MAKE_WIFI_CALLS,
4173 getWhenToMakeWifiCallsDefaultPreference());
4174 } finally {
4175 Binder.restoreCallingIdentity(identity);
4176 }
Ihab Awadf2177b72013-11-25 13:33:23 -08004177 }
4178
4179 /**
4180 * @see android.telephony.TelephonyManager.WifiCallingChoices
4181 */
4182 public void setWhenToMakeWifiCalls(int preference) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004183 final long identity = Binder.clearCallingIdentity();
4184 try {
4185 if (DBG) log("setWhenToMakeWifiCallsStr, storing setting = " + preference);
Nazanin Bakhshif782e562018-12-11 15:15:39 -08004186 Settings.System.putInt(mApp.getContentResolver(),
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004187 Settings.System.WHEN_TO_MAKE_WIFI_CALLS, preference);
4188 } finally {
4189 Binder.restoreCallingIdentity(identity);
4190 }
Ihab Awadf9e92732013-12-05 18:02:52 -08004191 }
4192
Sailesh Nepald1e68152013-12-12 19:08:02 -08004193 private static int getWhenToMakeWifiCallsDefaultPreference() {
Santos Cordonda120f42014-08-06 04:44:34 -07004194 // TODO: Use a build property to choose this value.
Evan Charlton9829e882013-12-19 15:30:38 -08004195 return TelephonyManager.WifiCallingChoices.ALWAYS_USE;
Ihab Awadf2177b72013-11-25 13:33:23 -08004196 }
Shishir Agrawal69f68122013-12-16 17:25:49 -08004197
Jordan Liu4c733742019-02-28 12:03:40 -08004198 private Phone getPhoneFromSlotIdOrThrowException(int slotIndex) {
4199 int phoneId = UiccController.getInstance().getPhoneIdFromSlotId(slotIndex);
4200 if (phoneId == -1) {
4201 throw new IllegalArgumentException("Given slot index: " + slotIndex
4202 + " does not correspond to an active phone");
4203 }
4204 return PhoneFactory.getPhone(phoneId);
4205 }
4206
Shishir Agrawal566b7612013-10-28 14:41:00 -07004207 @Override
Derek Tan740e1672017-06-27 14:56:27 -07004208 public IccOpenLogicalChannelResponse iccOpenLogicalChannel(
4209 int subId, String callingPackage, String aid, int p2) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08004210 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
4211 mApp, subId, "iccOpenLogicalChannel");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004212 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
Jordan Liu4c733742019-02-28 12:03:40 -08004213 if (DBG) {
4214 log("iccOpenLogicalChannel: subId=" + subId + " aid=" + aid + " p2=" + p2);
4215 }
4216 return iccOpenLogicalChannelWithPermission(getPhoneFromSubId(subId), callingPackage, aid,
4217 p2);
4218 }
Shishir Agrawal566b7612013-10-28 14:41:00 -07004219
Jordan Liu4c733742019-02-28 12:03:40 -08004220
4221 @Override
4222 public IccOpenLogicalChannelResponse iccOpenLogicalChannelBySlot(
4223 int slotIndex, String callingPackage, String aid, int p2) {
4224 enforceModifyPermission();
4225 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
4226 if (DBG) {
4227 log("iccOpenLogicalChannelBySlot: slot=" + slotIndex + " aid=" + aid + " p2=" + p2);
4228 }
4229 return iccOpenLogicalChannelWithPermission(getPhoneFromSlotIdOrThrowException(slotIndex),
4230 callingPackage, aid, p2);
4231 }
4232
4233 private IccOpenLogicalChannelResponse iccOpenLogicalChannelWithPermission(Phone phone,
4234 String callingPackage, String aid, int p2) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004235 final long identity = Binder.clearCallingIdentity();
4236 try {
4237 if (TextUtils.equals(ISDR_AID, aid)) {
4238 // Only allows LPA to open logical channel to ISD-R.
Nazanin Bakhshif782e562018-12-11 15:15:39 -08004239 ComponentInfo bestComponent = EuiccConnector.findBestComponent(getDefaultPhone()
4240 .getContext().getPackageManager());
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004241 if (bestComponent == null
4242 || !TextUtils.equals(callingPackage, bestComponent.packageName)) {
4243 loge("The calling package is not allowed to access ISD-R.");
4244 throw new SecurityException(
4245 "The calling package is not allowed to access ISD-R.");
4246 }
Derek Tan740e1672017-06-27 14:56:27 -07004247 }
Derek Tan740e1672017-06-27 14:56:27 -07004248
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004249 IccOpenLogicalChannelResponse response = (IccOpenLogicalChannelResponse) sendRequest(
Jordan Liu4c733742019-02-28 12:03:40 -08004250 CMD_OPEN_CHANNEL, new Pair<String, Integer>(aid, p2), phone,
4251 null /* workSource */);
4252 if (DBG) log("iccOpenLogicalChannelWithPermission: " + response);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004253 return response;
4254 } finally {
4255 Binder.restoreCallingIdentity(identity);
4256 }
Shishir Agrawal566b7612013-10-28 14:41:00 -07004257 }
4258
4259 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08004260 public boolean iccCloseLogicalChannel(int subId, int channel) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08004261 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
4262 mApp, subId, "iccCloseLogicalChannel");
Jordan Liu4c733742019-02-28 12:03:40 -08004263 if (DBG) log("iccCloseLogicalChannel: subId=" + subId + " chnl=" + channel);
4264 return iccCloseLogicalChannelWithPermission(getPhoneFromSubId(subId), channel);
4265 }
Shishir Agrawal566b7612013-10-28 14:41:00 -07004266
Jordan Liu4c733742019-02-28 12:03:40 -08004267 @Override
4268 public boolean iccCloseLogicalChannelBySlot(int slotIndex, int channel) {
4269 enforceModifyPermission();
4270 if (DBG) log("iccCloseLogicalChannelBySlot: slotIndex=" + slotIndex + " chnl=" + channel);
4271 return iccCloseLogicalChannelWithPermission(getPhoneFromSlotIdOrThrowException(slotIndex),
4272 channel);
4273 }
4274
4275 private boolean iccCloseLogicalChannelWithPermission(Phone phone, int channel) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004276 final long identity = Binder.clearCallingIdentity();
4277 try {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004278 if (channel < 0) {
4279 return false;
4280 }
Jordan Liu4c733742019-02-28 12:03:40 -08004281 Boolean success = (Boolean) sendRequest(CMD_CLOSE_CHANNEL, channel, phone,
4282 null /* workSource */);
4283 if (DBG) log("iccCloseLogicalChannelWithPermission: " + success);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004284 return success;
4285 } finally {
4286 Binder.restoreCallingIdentity(identity);
Shishir Agrawal566b7612013-10-28 14:41:00 -07004287 }
Shishir Agrawal566b7612013-10-28 14:41:00 -07004288 }
4289
4290 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08004291 public String iccTransmitApduLogicalChannel(int subId, int channel, int cla,
Shishir Agrawal566b7612013-10-28 14:41:00 -07004292 int command, int p1, int p2, int p3, String data) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08004293 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
4294 mApp, subId, "iccTransmitApduLogicalChannel");
Jordan Liu4c733742019-02-28 12:03:40 -08004295 if (DBG) {
4296 log("iccTransmitApduLogicalChannel: subId=" + subId + " chnl=" + channel
4297 + " cla=" + cla + " cmd=" + command + " p1=" + p1 + " p2=" + p2 + " p3="
4298 + p3 + " data=" + data);
4299 }
4300 return iccTransmitApduLogicalChannelWithPermission(getPhoneFromSubId(subId), channel, cla,
4301 command, p1, p2, p3, data);
4302 }
Shishir Agrawal566b7612013-10-28 14:41:00 -07004303
Jordan Liu4c733742019-02-28 12:03:40 -08004304 @Override
4305 public String iccTransmitApduLogicalChannelBySlot(int slotIndex, int channel, int cla,
4306 int command, int p1, int p2, int p3, String data) {
4307 enforceModifyPermission();
4308 if (DBG) {
4309 log("iccTransmitApduLogicalChannelBySlot: slotIndex=" + slotIndex + " chnl=" + channel
4310 + " cla=" + cla + " cmd=" + command + " p1=" + p1 + " p2=" + p2 + " p3="
4311 + p3 + " data=" + data);
4312 }
4313 return iccTransmitApduLogicalChannelWithPermission(
4314 getPhoneFromSlotIdOrThrowException(slotIndex), channel, cla, command, p1, p2, p3,
4315 data);
4316 }
4317
4318 private String iccTransmitApduLogicalChannelWithPermission(Phone phone, int channel, int cla,
4319 int command, int p1, int p2, int p3, String data) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004320 final long identity = Binder.clearCallingIdentity();
4321 try {
Hall Liu4fd771b2019-05-02 09:16:29 -07004322 if (channel <= 0) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004323 return "";
4324 }
Shishir Agrawal566b7612013-10-28 14:41:00 -07004325
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004326 IccIoResult response = (IccIoResult) sendRequest(CMD_TRANSMIT_APDU_LOGICAL_CHANNEL,
Jordan Liu4c733742019-02-28 12:03:40 -08004327 new IccAPDUArgument(channel, cla, command, p1, p2, p3, data), phone,
4328 null /* workSource */);
4329 if (DBG) log("iccTransmitApduLogicalChannelWithPermission: " + response);
Shishir Agrawal566b7612013-10-28 14:41:00 -07004330
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004331 // Append the returned status code to the end of the response payload.
4332 String s = Integer.toHexString(
4333 (response.sw1 << 8) + response.sw2 + 0x10000).substring(1);
4334 if (response.payload != null) {
4335 s = IccUtils.bytesToHexString(response.payload) + s;
4336 }
4337 return s;
4338 } finally {
4339 Binder.restoreCallingIdentity(identity);
Shishir Agrawal5ec14172014-08-05 17:05:45 -07004340 }
Shishir Agrawal566b7612013-10-28 14:41:00 -07004341 }
Jake Hambye994d462014-02-03 13:10:13 -08004342
Evan Charltonc66da362014-05-16 14:06:40 -07004343 @Override
Holly Jiuyu Sun1cc2d552018-01-26 15:51:16 -08004344 public String iccTransmitApduBasicChannel(int subId, String callingPackage, int cla,
4345 int command, int p1, int p2, int p3, String data) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08004346 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
4347 mApp, subId, "iccTransmitApduBasicChannel");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004348 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
Jordan Liu4c733742019-02-28 12:03:40 -08004349 if (DBG) {
4350 log("iccTransmitApduBasicChannel: subId=" + subId + " cla=" + cla + " cmd="
4351 + command + " p1=" + p1 + " p2=" + p2 + " p3=" + p3 + " data=" + data);
4352 }
4353 return iccTransmitApduBasicChannelWithPermission(getPhoneFromSubId(subId), callingPackage,
4354 cla, command, p1, p2, p3, data);
4355 }
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07004356
Jordan Liu4c733742019-02-28 12:03:40 -08004357 @Override
4358 public String iccTransmitApduBasicChannelBySlot(int slotIndex, String callingPackage, int cla,
4359 int command, int p1, int p2, int p3, String data) {
4360 enforceModifyPermission();
4361 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
4362 if (DBG) {
4363 log("iccTransmitApduBasicChannelBySlot: slotIndex=" + slotIndex + " cla=" + cla
4364 + " cmd=" + command + " p1=" + p1 + " p2=" + p2 + " p3=" + p3
4365 + " data=" + data);
4366 }
4367
4368 return iccTransmitApduBasicChannelWithPermission(
4369 getPhoneFromSlotIdOrThrowException(slotIndex), callingPackage, cla, command, p1,
4370 p2, p3, data);
4371 }
4372
4373 // open APDU basic channel assuming the caller has sufficient permissions
4374 private String iccTransmitApduBasicChannelWithPermission(Phone phone, String callingPackage,
4375 int cla, int command, int p1, int p2, int p3, String data) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004376 final long identity = Binder.clearCallingIdentity();
4377 try {
4378 if (command == SELECT_COMMAND && p1 == SELECT_P1 && p2 == SELECT_P2 && p3 == SELECT_P3
4379 && TextUtils.equals(ISDR_AID, data)) {
4380 // Only allows LPA to select ISD-R.
Nazanin Bakhshif782e562018-12-11 15:15:39 -08004381 ComponentInfo bestComponent = EuiccConnector.findBestComponent(getDefaultPhone()
4382 .getContext().getPackageManager());
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004383 if (bestComponent == null
4384 || !TextUtils.equals(callingPackage, bestComponent.packageName)) {
4385 loge("The calling package is not allowed to select ISD-R.");
4386 throw new SecurityException(
4387 "The calling package is not allowed to select ISD-R.");
4388 }
Holly Jiuyu Sun1cc2d552018-01-26 15:51:16 -08004389 }
Holly Jiuyu Sun1cc2d552018-01-26 15:51:16 -08004390
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004391 IccIoResult response = (IccIoResult) sendRequest(CMD_TRANSMIT_APDU_BASIC_CHANNEL,
Jordan Liu4c733742019-02-28 12:03:40 -08004392 new IccAPDUArgument(0, cla, command, p1, p2, p3, data), phone,
4393 null /* workSource */);
4394 if (DBG) log("iccTransmitApduBasicChannelWithPermission: " + response);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07004395
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004396 // Append the returned status code to the end of the response payload.
4397 String s = Integer.toHexString(
4398 (response.sw1 << 8) + response.sw2 + 0x10000).substring(1);
4399 if (response.payload != null) {
4400 s = IccUtils.bytesToHexString(response.payload) + s;
4401 }
4402 return s;
4403 } finally {
4404 Binder.restoreCallingIdentity(identity);
Shishir Agrawal5ec14172014-08-05 17:05:45 -07004405 }
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07004406 }
4407
4408 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08004409 public byte[] iccExchangeSimIO(int subId, int fileID, int command, int p1, int p2, int p3,
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07004410 String filePath) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08004411 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
4412 mApp, subId, "iccExchangeSimIO");
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07004413
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004414 final long identity = Binder.clearCallingIdentity();
4415 try {
4416 if (DBG) {
4417 log("Exchange SIM_IO " + subId + ":" + fileID + ":" + command + " "
4418 + p1 + " " + p2 + " " + p3 + ":" + filePath);
4419 }
4420
4421 IccIoResult response =
4422 (IccIoResult) sendRequest(CMD_EXCHANGE_SIM_IO,
4423 new IccAPDUArgument(-1, fileID, command, p1, p2, p3, filePath),
4424 subId);
4425
4426 if (DBG) {
4427 log("Exchange SIM_IO [R]" + response);
4428 }
4429
4430 byte[] result = null;
4431 int length = 2;
4432 if (response.payload != null) {
4433 length = 2 + response.payload.length;
4434 result = new byte[length];
4435 System.arraycopy(response.payload, 0, result, 0, response.payload.length);
4436 } else {
4437 result = new byte[length];
4438 }
4439
4440 result[length - 1] = (byte) response.sw2;
4441 result[length - 2] = (byte) response.sw1;
4442 return result;
4443 } finally {
4444 Binder.restoreCallingIdentity(identity);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07004445 }
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07004446 }
4447
Nathan Haroldb3014052017-01-25 15:57:32 -08004448 /**
4449 * Get the forbidden PLMN List from the given app type (ex APPTYPE_USIM)
4450 * on a particular subscription
4451 */
Philip P. Moltmann9797cbb2020-01-07 13:45:00 -08004452 public String[] getForbiddenPlmns(int subId, int appType, String callingPackage,
4453 String callingFeatureId) {
sqianb6e41952018-03-12 14:54:01 -07004454 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
Philip P. Moltmann9797cbb2020-01-07 13:45:00 -08004455 mApp, subId, callingPackage, callingFeatureId, "getForbiddenPlmns")) {
sqianb6e41952018-03-12 14:54:01 -07004456 return null;
4457 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004458
4459 final long identity = Binder.clearCallingIdentity();
4460 try {
4461 if (appType != TelephonyManager.APPTYPE_USIM
4462 && appType != TelephonyManager.APPTYPE_SIM) {
4463 loge("getForbiddenPlmnList(): App Type must be USIM or SIM");
4464 return null;
4465 }
4466 Object response = sendRequest(
4467 CMD_GET_FORBIDDEN_PLMNS, new Integer(appType), subId);
4468 if (response instanceof String[]) {
4469 return (String[]) response;
4470 }
yincheng zhaod698b842019-09-06 17:06:54 -07004471 // Response is an Exception of some kind
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004472 // which is signalled to the user as a NULL retval
Nathan Haroldb3014052017-01-25 15:57:32 -08004473 return null;
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004474 } finally {
4475 Binder.restoreCallingIdentity(identity);
Nathan Haroldb3014052017-01-25 15:57:32 -08004476 }
Nathan Haroldb3014052017-01-25 15:57:32 -08004477 }
4478
yincheng zhaod698b842019-09-06 17:06:54 -07004479 /**
4480 * Set the forbidden PLMN list from the given app type (ex APPTYPE_USIM) on a particular
4481 * subscription.
4482 *
4483 * @param subId the id of the subscription.
4484 * @param appType the uicc app type, must be USIM or SIM.
4485 * @param fplmns the Forbiden plmns list that needed to be written to the SIM.
4486 * @param callingPackage the op Package name.
Philip P. Moltmann9797cbb2020-01-07 13:45:00 -08004487 * @param callingFeatureId the feature in the package.
yincheng zhaod698b842019-09-06 17:06:54 -07004488 * @return number of fplmns that is successfully written to the SIM.
4489 */
Philip P. Moltmann9797cbb2020-01-07 13:45:00 -08004490 public int setForbiddenPlmns(int subId, int appType, List<String> fplmns, String callingPackage,
4491 String callingFeatureId) {
4492 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(mApp, subId, callingPackage,
4493 callingFeatureId, "setForbiddenPlmns")) {
yincheng zhaod698b842019-09-06 17:06:54 -07004494 if (DBG) logv("no permissions for setForbiddenplmns");
4495 throw new IllegalStateException("No Permissions for setForbiddenPlmns");
4496 }
4497 if (appType != TelephonyManager.APPTYPE_USIM && appType != TelephonyManager.APPTYPE_SIM) {
4498 loge("setForbiddenPlmnList(): App Type must be USIM or SIM");
4499 throw new IllegalArgumentException("Invalid appType: App Type must be USIM or SIM");
4500 }
4501 if (fplmns == null) {
4502 throw new IllegalArgumentException("Fplmn List provided is null");
4503 }
4504 for (String fplmn : fplmns) {
4505 if (!CellIdentity.isValidPlmn(fplmn)) {
4506 throw new IllegalArgumentException("Invalid fplmn provided: " + fplmn);
4507 }
4508 }
4509 final long identity = Binder.clearCallingIdentity();
4510 try {
4511 Object response = sendRequest(
4512 CMD_SET_FORBIDDEN_PLMNS,
4513 new Pair<Integer, List<String>>(new Integer(appType), fplmns),
4514 subId);
4515 return (int) response;
4516 } finally {
4517 Binder.restoreCallingIdentity(identity);
4518 }
4519 }
4520
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07004521 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08004522 public String sendEnvelopeWithStatus(int subId, String content) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08004523 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
4524 mApp, subId, "sendEnvelopeWithStatus");
Evan Charltonc66da362014-05-16 14:06:40 -07004525
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004526 final long identity = Binder.clearCallingIdentity();
4527 try {
4528 IccIoResult response = (IccIoResult) sendRequest(CMD_SEND_ENVELOPE, content, subId);
4529 if (response.payload == null) {
4530 return "";
4531 }
Evan Charltonc66da362014-05-16 14:06:40 -07004532
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004533 // Append the returned status code to the end of the response payload.
4534 String s = Integer.toHexString(
4535 (response.sw1 << 8) + response.sw2 + 0x10000).substring(1);
4536 s = IccUtils.bytesToHexString(response.payload) + s;
4537 return s;
4538 } finally {
4539 Binder.restoreCallingIdentity(identity);
4540 }
Evan Charltonc66da362014-05-16 14:06:40 -07004541 }
4542
Jake Hambye994d462014-02-03 13:10:13 -08004543 /**
4544 * Read one of the NV items defined in {@link com.android.internal.telephony.RadioNVItems}
4545 * and {@code ril_nv_items.h}. Used for device configuration by some CDMA operators.
4546 *
4547 * @param itemID the ID of the item to read
4548 * @return the NV item as a String, or null on error.
4549 */
4550 @Override
4551 public String nvReadItem(int itemID) {
vagdeviaf9a5b92018-08-15 16:01:53 -07004552 WorkSource workSource = getWorkSource(Binder.getCallingUid());
Jeff Davidson7e17e312018-02-13 18:17:36 -08004553 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
4554 mApp, getDefaultSubscription(), "nvReadItem");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004555
4556 final long identity = Binder.clearCallingIdentity();
4557 try {
4558 if (DBG) log("nvReadItem: item " + itemID);
vagdeviaf9a5b92018-08-15 16:01:53 -07004559 String value = (String) sendRequest(CMD_NV_READ_ITEM, itemID, workSource);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004560 if (DBG) log("nvReadItem: item " + itemID + " is \"" + value + '"');
4561 return value;
4562 } finally {
4563 Binder.restoreCallingIdentity(identity);
4564 }
Jake Hambye994d462014-02-03 13:10:13 -08004565 }
4566
4567 /**
4568 * Write one of the NV items defined in {@link com.android.internal.telephony.RadioNVItems}
4569 * and {@code ril_nv_items.h}. Used for device configuration by some CDMA operators.
4570 *
4571 * @param itemID the ID of the item to read
4572 * @param itemValue the value to write, as a String
4573 * @return true on success; false on any failure
4574 */
4575 @Override
4576 public boolean nvWriteItem(int itemID, String itemValue) {
vagdeviaf9a5b92018-08-15 16:01:53 -07004577 WorkSource workSource = getWorkSource(Binder.getCallingUid());
Jeff Davidson7e17e312018-02-13 18:17:36 -08004578 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
4579 mApp, getDefaultSubscription(), "nvWriteItem");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004580
4581 final long identity = Binder.clearCallingIdentity();
4582 try {
4583 if (DBG) log("nvWriteItem: item " + itemID + " value \"" + itemValue + '"');
4584 Boolean success = (Boolean) sendRequest(CMD_NV_WRITE_ITEM,
vagdeviaf9a5b92018-08-15 16:01:53 -07004585 new Pair<Integer, String>(itemID, itemValue), workSource);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004586 if (DBG) log("nvWriteItem: item " + itemID + ' ' + (success ? "ok" : "fail"));
4587 return success;
4588 } finally {
4589 Binder.restoreCallingIdentity(identity);
4590 }
Jake Hambye994d462014-02-03 13:10:13 -08004591 }
4592
4593 /**
4594 * Update the CDMA Preferred Roaming List (PRL) in the radio NV storage.
4595 * Used for device configuration by some CDMA operators.
4596 *
4597 * @param preferredRoamingList byte array containing the new PRL
4598 * @return true on success; false on any failure
4599 */
4600 @Override
4601 public boolean nvWriteCdmaPrl(byte[] preferredRoamingList) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08004602 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
4603 mApp, getDefaultSubscription(), "nvWriteCdmaPrl");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004604
4605 final long identity = Binder.clearCallingIdentity();
4606 try {
4607 if (DBG) log("nvWriteCdmaPrl: value: " + HexDump.toHexString(preferredRoamingList));
4608 Boolean success = (Boolean) sendRequest(CMD_NV_WRITE_CDMA_PRL, preferredRoamingList);
4609 if (DBG) log("nvWriteCdmaPrl: " + (success ? "ok" : "fail"));
4610 return success;
4611 } finally {
4612 Binder.restoreCallingIdentity(identity);
4613 }
Jake Hambye994d462014-02-03 13:10:13 -08004614 }
4615
4616 /**
chen xu6dac5ab2018-10-26 17:39:23 -07004617 * Rollback modem configurations to factory default except some config which are in whitelist.
Jake Hambye994d462014-02-03 13:10:13 -08004618 * Used for device configuration by some CDMA operators.
4619 *
chen xu6dac5ab2018-10-26 17:39:23 -07004620 * @param slotIndex - device slot.
4621 *
Jake Hambye994d462014-02-03 13:10:13 -08004622 * @return true on success; false on any failure
4623 */
4624 @Override
chen xu6dac5ab2018-10-26 17:39:23 -07004625 public boolean resetModemConfig(int slotIndex) {
4626 Phone phone = PhoneFactory.getPhone(slotIndex);
4627 if (phone != null) {
4628 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
4629 mApp, phone.getSubId(), "resetModemConfig");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004630
chen xu6dac5ab2018-10-26 17:39:23 -07004631 final long identity = Binder.clearCallingIdentity();
4632 try {
4633 Boolean success = (Boolean) sendRequest(CMD_RESET_MODEM_CONFIG, null);
4634 if (DBG) log("resetModemConfig:" + ' ' + (success ? "ok" : "fail"));
4635 return success;
4636 } finally {
4637 Binder.restoreCallingIdentity(identity);
4638 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004639 }
chen xu6dac5ab2018-10-26 17:39:23 -07004640 return false;
4641 }
4642
4643 /**
4644 * Generate a radio modem reset. Used for device configuration by some CDMA operators.
4645 *
4646 * @param slotIndex - device slot.
4647 *
4648 * @return true on success; false on any failure
4649 */
4650 @Override
4651 public boolean rebootModem(int slotIndex) {
4652 Phone phone = PhoneFactory.getPhone(slotIndex);
4653 if (phone != null) {
4654 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
4655 mApp, phone.getSubId(), "rebootModem");
4656
4657 final long identity = Binder.clearCallingIdentity();
4658 try {
4659 Boolean success = (Boolean) sendRequest(CMD_MODEM_REBOOT, null);
4660 if (DBG) log("rebootModem:" + ' ' + (success ? "ok" : "fail"));
4661 return success;
4662 } finally {
4663 Binder.restoreCallingIdentity(identity);
4664 }
4665 }
4666 return false;
Jake Hambye994d462014-02-03 13:10:13 -08004667 }
Jake Hamby7c27be32014-03-03 13:25:59 -08004668
Philip P. Moltmann9797cbb2020-01-07 13:45:00 -08004669 public String[] getPcscfAddress(String apnType, String callingPackage,
4670 String callingFeatureId) {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08004671 final Phone defaultPhone = getDefaultPhone();
Philip P. Moltmann9797cbb2020-01-07 13:45:00 -08004672 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(mApp, defaultPhone.getSubId(),
4673 callingPackage, callingFeatureId, "getPcscfAddress")) {
Svet Ganovb320e182015-04-16 12:30:10 -07004674 return new String[0];
4675 }
4676
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004677 final long identity = Binder.clearCallingIdentity();
4678 try {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08004679 return defaultPhone.getPcscfAddress(apnType);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004680 } finally {
4681 Binder.restoreCallingIdentity(identity);
4682 }
Wink Saville36469e72014-06-11 15:17:00 -07004683 }
4684
Brad Ebinger51f743a2017-01-23 13:50:20 -08004685 /**
Grace Jiaaa2eb6b2020-01-09 16:26:08 -08004686 * Toggle IMS disable and enable for the framework to reset it. See {@link #enableIms(int)} and
4687 * {@link #disableIms(int)}.
4688 * @param slotIndex device slot.
4689 */
4690 public void resetIms(int slotIndex) {
4691 enforceModifyPermission();
4692
4693 final long identity = Binder.clearCallingIdentity();
4694 try {
4695 if (mImsResolver == null) {
4696 // may happen if the does not support IMS.
4697 return;
4698 }
4699 mImsResolver.disableIms(slotIndex);
4700 mImsResolver.enableIms(slotIndex);
4701 } finally {
4702 Binder.restoreCallingIdentity(identity);
4703 }
4704 }
4705
4706 /**
Brad Ebinger22bc3e42018-01-16 09:39:35 -08004707 * Enables IMS for the framework. This will trigger IMS registration and ImsFeature capability
4708 * status updates, if not already enabled.
Brad Ebinger51f743a2017-01-23 13:50:20 -08004709 */
Brad Ebinger22bc3e42018-01-16 09:39:35 -08004710 public void enableIms(int slotId) {
Brad Ebinger51f743a2017-01-23 13:50:20 -08004711 enforceModifyPermission();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004712
4713 final long identity = Binder.clearCallingIdentity();
4714 try {
Brad Ebinger05f52c22019-12-05 13:03:21 -08004715 if (mImsResolver == null) {
Brad Ebinger9c0eb502019-01-23 15:06:19 -08004716 // may happen if the device does not support IMS.
4717 return;
4718 }
Brad Ebinger05f52c22019-12-05 13:03:21 -08004719 mImsResolver.enableIms(slotId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004720 } finally {
4721 Binder.restoreCallingIdentity(identity);
4722 }
Brad Ebinger34bef922017-11-09 10:27:08 -08004723 }
4724
4725 /**
Brad Ebinger22bc3e42018-01-16 09:39:35 -08004726 * Disables IMS for the framework. This will trigger IMS de-registration and trigger ImsFeature
4727 * status updates to disabled.
Brad Ebinger34bef922017-11-09 10:27:08 -08004728 */
Brad Ebinger22bc3e42018-01-16 09:39:35 -08004729 public void disableIms(int slotId) {
4730 enforceModifyPermission();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004731
4732 final long identity = Binder.clearCallingIdentity();
4733 try {
Brad Ebinger05f52c22019-12-05 13:03:21 -08004734 if (mImsResolver == null) {
Brad Ebinger9c0eb502019-01-23 15:06:19 -08004735 // may happen if the device does not support IMS.
4736 return;
4737 }
Brad Ebinger05f52c22019-12-05 13:03:21 -08004738 mImsResolver.disableIms(slotId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004739 } finally {
4740 Binder.restoreCallingIdentity(identity);
4741 }
Brad Ebinger22bc3e42018-01-16 09:39:35 -08004742 }
4743
4744 /**
4745 * Returns the {@link IImsMmTelFeature} that corresponds to the given slot Id for the MMTel
4746 * feature or {@link null} if the service is not available. If the feature is available, the
4747 * {@link IImsServiceFeatureCallback} callback is registered as a listener for feature updates.
4748 */
4749 public IImsMmTelFeature getMmTelFeatureAndListen(int slotId,
Brad Ebinger34bef922017-11-09 10:27:08 -08004750 IImsServiceFeatureCallback callback) {
4751 enforceModifyPermission();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004752
4753 final long identity = Binder.clearCallingIdentity();
4754 try {
Brad Ebinger05f52c22019-12-05 13:03:21 -08004755 if (mImsResolver == null) {
Brad Ebinger9c0eb502019-01-23 15:06:19 -08004756 // may happen if the device does not support IMS.
4757 return null;
4758 }
Brad Ebinger05f52c22019-12-05 13:03:21 -08004759 return mImsResolver.getMmTelFeatureAndListen(slotId, callback);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004760 } finally {
4761 Binder.restoreCallingIdentity(identity);
4762 }
Brad Ebinger34bef922017-11-09 10:27:08 -08004763 }
4764
4765 /**
4766 * Returns the {@link IImsRcsFeature} that corresponds to the given slot Id for the RCS
4767 * feature during emergency calling or {@link null} if the service is not available. If the
4768 * feature is available, the {@link IImsServiceFeatureCallback} callback is registered as a
4769 * listener for feature updates.
4770 */
4771 public IImsRcsFeature getRcsFeatureAndListen(int slotId, IImsServiceFeatureCallback callback) {
4772 enforceModifyPermission();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004773
4774 final long identity = Binder.clearCallingIdentity();
4775 try {
Brad Ebinger05f52c22019-12-05 13:03:21 -08004776 if (mImsResolver == null) {
Brad Ebinger9c0eb502019-01-23 15:06:19 -08004777 // may happen if the device does not support IMS.
4778 return null;
4779 }
Brad Ebinger05f52c22019-12-05 13:03:21 -08004780 return mImsResolver.getRcsFeatureAndListen(slotId, callback);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004781 } finally {
4782 Binder.restoreCallingIdentity(identity);
4783 }
Brad Ebinger51f743a2017-01-23 13:50:20 -08004784 }
4785
Brad Ebinger5f64b052017-12-14 14:26:15 -08004786 /**
4787 * Returns the {@link IImsRegistration} structure associated with the slotId and feature
Brad Ebinger9c0eb502019-01-23 15:06:19 -08004788 * specified or null if IMS is not supported on the slot specified.
Brad Ebinger5f64b052017-12-14 14:26:15 -08004789 */
4790 public IImsRegistration getImsRegistration(int slotId, int feature) throws RemoteException {
4791 enforceModifyPermission();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004792
4793 final long identity = Binder.clearCallingIdentity();
4794 try {
Brad Ebinger05f52c22019-12-05 13:03:21 -08004795 if (mImsResolver == null) {
Brad Ebinger9c0eb502019-01-23 15:06:19 -08004796 // may happen if the device does not support IMS.
4797 return null;
4798 }
Brad Ebinger05f52c22019-12-05 13:03:21 -08004799 return mImsResolver.getImsRegistration(slotId, feature);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004800 } finally {
4801 Binder.restoreCallingIdentity(identity);
4802 }
Brad Ebinger5f64b052017-12-14 14:26:15 -08004803 }
4804
Brad Ebinger22bc3e42018-01-16 09:39:35 -08004805 /**
4806 * Returns the {@link IImsConfig} structure associated with the slotId and feature
Brad Ebinger9c0eb502019-01-23 15:06:19 -08004807 * specified or null if IMS is not supported on the slot specified.
Brad Ebinger22bc3e42018-01-16 09:39:35 -08004808 */
4809 public IImsConfig getImsConfig(int slotId, int feature) throws RemoteException {
4810 enforceModifyPermission();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004811
4812 final long identity = Binder.clearCallingIdentity();
4813 try {
Brad Ebinger05f52c22019-12-05 13:03:21 -08004814 if (mImsResolver == null) {
Brad Ebinger9c0eb502019-01-23 15:06:19 -08004815 // may happen if the device does not support IMS.
4816 return null;
4817 }
Brad Ebinger05f52c22019-12-05 13:03:21 -08004818 return mImsResolver.getImsConfig(slotId, feature);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004819 } finally {
4820 Binder.restoreCallingIdentity(identity);
4821 }
Brad Ebinger22bc3e42018-01-16 09:39:35 -08004822 }
4823
Brad Ebinger884c07b2018-02-15 16:17:40 -08004824 /**
Brad Ebingerdac2f002018-04-03 15:17:52 -07004825 * Sets the ImsService Package Name that Telephony will bind to.
4826 *
Brad Ebinger05f52c22019-12-05 13:03:21 -08004827 * @param slotIndex the slot ID that the ImsService should bind for.
4828 * @param isCarrierService true if the ImsService is the carrier override, false if the
Brad Ebingerdac2f002018-04-03 15:17:52 -07004829 * ImsService is the device default ImsService.
Brad Ebinger05f52c22019-12-05 13:03:21 -08004830 * @param featureTypes An integer array of feature types associated with a packageName.
4831 * @param packageName The name of the package that the current configuration will be replaced
4832 * with.
Brad Ebingerdac2f002018-04-03 15:17:52 -07004833 * @return true if setting the ImsService to bind to succeeded, false if it did not.
Brad Ebingerdac2f002018-04-03 15:17:52 -07004834 */
Brad Ebinger05f52c22019-12-05 13:03:21 -08004835 public boolean setBoundImsServiceOverride(int slotIndex, boolean isCarrierService,
4836 int[] featureTypes, String packageName) {
4837 int[] subIds = SubscriptionManager.getSubId(slotIndex);
4838 TelephonyPermissions.enforceShellOnly(Binder.getCallingUid(), "setBoundImsServiceOverride");
Brad Ebingerde696de2018-04-06 09:56:40 -07004839 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(mApp,
4840 (subIds != null ? subIds[0] : SubscriptionManager.INVALID_SUBSCRIPTION_ID),
Brad Ebinger05f52c22019-12-05 13:03:21 -08004841 "setBoundImsServiceOverride");
Brad Ebingerde696de2018-04-06 09:56:40 -07004842
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004843 final long identity = Binder.clearCallingIdentity();
4844 try {
Brad Ebinger05f52c22019-12-05 13:03:21 -08004845 if (mImsResolver == null) {
Brad Ebinger9c0eb502019-01-23 15:06:19 -08004846 // may happen if the device does not support IMS.
4847 return false;
4848 }
Brad Ebinger05f52c22019-12-05 13:03:21 -08004849 Map<Integer, String> featureConfig = new HashMap<>();
4850 for (int featureType : featureTypes) {
4851 featureConfig.put(featureType, packageName);
4852 }
4853 return mImsResolver.overrideImsServiceConfiguration(slotIndex, isCarrierService,
4854 featureConfig);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004855 } finally {
4856 Binder.restoreCallingIdentity(identity);
4857 }
Brad Ebingerdac2f002018-04-03 15:17:52 -07004858 }
4859
4860 /**
Brad Ebinger05f52c22019-12-05 13:03:21 -08004861 * Return the package name of the currently bound ImsService.
Brad Ebingerdac2f002018-04-03 15:17:52 -07004862 *
4863 * @param slotId The slot that the ImsService is associated with.
4864 * @param isCarrierImsService true, if the ImsService is a carrier override, false if it is
4865 * the device default.
Brad Ebinger05f52c22019-12-05 13:03:21 -08004866 * @param featureType The feature associated with the queried configuration.
Brad Ebingerdac2f002018-04-03 15:17:52 -07004867 * @return the package name of the ImsService configuration.
4868 */
Brad Ebinger05f52c22019-12-05 13:03:21 -08004869 public String getBoundImsServicePackage(int slotId, boolean isCarrierImsService,
4870 @ImsFeature.FeatureType int featureType) {
Brad Ebingerde696de2018-04-06 09:56:40 -07004871 int[] subIds = SubscriptionManager.getSubId(slotId);
Brad Ebinger05f52c22019-12-05 13:03:21 -08004872 TelephonyPermissions
4873 .enforeceCallingOrSelfReadPrivilegedPhoneStatePermissionOrCarrierPrivilege(
4874 mApp, (subIds != null ? subIds[0] : SubscriptionManager.INVALID_SUBSCRIPTION_ID),
4875 "getBoundImsServicePackage");
Brad Ebingerde696de2018-04-06 09:56:40 -07004876
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004877 final long identity = Binder.clearCallingIdentity();
4878 try {
Brad Ebinger05f52c22019-12-05 13:03:21 -08004879 if (mImsResolver == null) {
Brad Ebinger9c0eb502019-01-23 15:06:19 -08004880 // may happen if the device does not support IMS.
4881 return "";
4882 }
Brad Ebingera80c3312019-12-02 10:59:39 -08004883 // TODO: change API to query RCS separately.
Brad Ebinger05f52c22019-12-05 13:03:21 -08004884 return mImsResolver.getImsServiceConfiguration(slotId, isCarrierImsService,
4885 featureType);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004886 } finally {
4887 Binder.restoreCallingIdentity(identity);
4888 }
Brad Ebingerdac2f002018-04-03 15:17:52 -07004889 }
4890
Brad Ebinger77b832e2019-10-17 17:03:22 -07004891 /**
4892 * Get the MmTelFeature state associated with the requested subscription id.
4893 * @param subId The subscription that the MmTelFeature is associated with.
4894 * @param callback A callback with an integer containing the
4895 * {@link android.telephony.ims.feature.ImsFeature.ImsState} associated with the MmTelFeature.
4896 */
4897 @Override
4898 public void getImsMmTelFeatureState(int subId, IIntegerConsumer callback) {
4899 enforceReadPrivilegedPermission("getImsMmTelFeatureState");
4900 if (!ImsManager.isImsSupportedOnDevice(mApp)) {
4901 throw new ServiceSpecificException(ImsException.CODE_ERROR_UNSUPPORTED_OPERATION,
4902 "IMS not available on device.");
4903 }
4904 final long token = Binder.clearCallingIdentity();
4905 try {
4906 int slotId = getSlotIndex(subId);
4907 if (slotId <= SubscriptionManager.INVALID_SIM_SLOT_INDEX) {
4908 Log.w(LOG_TAG, "getImsMmTelFeatureState: called with an inactive subscription '"
4909 + subId + "'");
4910 throw new ServiceSpecificException(ImsException.CODE_ERROR_INVALID_SUBSCRIPTION);
4911 }
4912 ImsManager.getInstance(mApp, slotId).getImsServiceState(anInteger -> {
4913 try {
4914 callback.accept(anInteger == null ? ImsFeature.STATE_UNAVAILABLE : anInteger);
4915 } catch (RemoteException e) {
4916 Log.w(LOG_TAG, "getImsMmTelFeatureState: remote caller is no longer running. "
4917 + "Ignore");
4918 }
4919 });
4920 } finally {
4921 Binder.restoreCallingIdentity(token);
4922 }
4923 }
4924
Wink Saville36469e72014-06-11 15:17:00 -07004925 public void setImsRegistrationState(boolean registered) {
4926 enforceModifyPermission();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004927
4928 final long identity = Binder.clearCallingIdentity();
4929 try {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08004930 getDefaultPhone().setImsRegistrationState(registered);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004931 } finally {
4932 Binder.restoreCallingIdentity(identity);
4933 }
Wink Saville36469e72014-06-11 15:17:00 -07004934 }
4935
4936 /**
Stuart Scott54788802015-03-30 13:18:01 -07004937 * Set the network selection mode to automatic.
4938 *
4939 */
4940 @Override
4941 public void setNetworkSelectionModeAutomatic(int subId) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08004942 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
4943 mApp, subId, "setNetworkSelectionModeAutomatic");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004944
4945 final long identity = Binder.clearCallingIdentity();
4946 try {
shilufc958392020-01-20 11:36:01 -08004947 if (!isActiveSubscription(subId)) {
4948 return;
4949 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004950 if (DBG) log("setNetworkSelectionModeAutomatic: subId " + subId);
4951 sendRequest(CMD_SET_NETWORK_SELECTION_MODE_AUTOMATIC, null, subId);
4952 } finally {
4953 Binder.restoreCallingIdentity(identity);
4954 }
Stuart Scott54788802015-03-30 13:18:01 -07004955 }
4956
Pengquan Mengea84e042018-09-20 14:57:26 -07004957 /**
4958 * Ask the radio to connect to the input network and change selection mode to manual.
4959 *
4960 * @param subId the id of the subscription.
4961 * @param operatorInfo the operator information, included the PLMN, long name and short name of
4962 * the operator to attach to.
4963 * @param persistSelection whether the selection will persist until reboot. If true, only allows
4964 * attaching to the selected PLMN until reboot; otherwise, attach to the chosen PLMN and resume
4965 * normal network selection next time.
4966 * @return {@code true} on success; {@code true} on any failure.
Shishir Agrawal302c8692015-06-19 13:49:39 -07004967 */
4968 @Override
Pengquan Mengea84e042018-09-20 14:57:26 -07004969 public boolean setNetworkSelectionModeManual(
4970 int subId, OperatorInfo operatorInfo, boolean persistSelection) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08004971 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
4972 mApp, subId, "setNetworkSelectionModeManual");
Pengquan Menge92a50d2018-09-21 15:54:48 -07004973
4974 if (!isActiveSubscription(subId)) {
4975 return false;
4976 }
4977
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004978 final long identity = Binder.clearCallingIdentity();
4979 try {
Pengquan Mengea84e042018-09-20 14:57:26 -07004980 ManualNetworkSelectionArgument arg = new ManualNetworkSelectionArgument(operatorInfo,
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004981 persistSelection);
Pengquan Mengea84e042018-09-20 14:57:26 -07004982 if (DBG) {
4983 log("setNetworkSelectionModeManual: subId: " + subId
4984 + " operator: " + operatorInfo);
4985 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004986 return (Boolean) sendRequest(CMD_SET_NETWORK_SELECTION_MODE_MANUAL, arg, subId);
4987 } finally {
4988 Binder.restoreCallingIdentity(identity);
4989 }
Shishir Agrawal302c8692015-06-19 13:49:39 -07004990 }
4991
4992 /**
4993 * Scans for available networks.
4994 */
4995 @Override
Philip P. Moltmann9797cbb2020-01-07 13:45:00 -08004996 public CellNetworkScanResult getCellNetworkScanResults(int subId, String callingPackage,
4997 String callingFeatureId) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08004998 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
4999 mApp, subId, "getCellNetworkScanResults");
Hall Liuf19c44f2018-11-27 14:38:17 -08005000 LocationAccessPolicy.LocationPermissionResult locationResult =
5001 LocationAccessPolicy.checkLocationPermission(mApp,
5002 new LocationAccessPolicy.LocationPermissionQuery.Builder()
5003 .setCallingPackage(callingPackage)
Philip P. Moltmann9797cbb2020-01-07 13:45:00 -08005004 .setCallingFeatureId(callingFeatureId)
Hall Liuf19c44f2018-11-27 14:38:17 -08005005 .setCallingPid(Binder.getCallingPid())
5006 .setCallingUid(Binder.getCallingUid())
5007 .setMethod("getCellNetworkScanResults")
5008 .setMinSdkVersionForFine(Build.VERSION_CODES.Q)
5009 .build());
5010 switch (locationResult) {
5011 case DENIED_HARD:
5012 throw new SecurityException("Not allowed to access scan results -- location");
5013 case DENIED_SOFT:
5014 return null;
5015 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005016
Pengquan Menga1bb6272018-09-06 09:59:22 -07005017 long identity = Binder.clearCallingIdentity();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005018 try {
5019 if (DBG) log("getCellNetworkScanResults: subId " + subId);
Pengquan Menga1bb6272018-09-06 09:59:22 -07005020 return (CellNetworkScanResult) sendRequest(
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005021 CMD_PERFORM_NETWORK_SCAN, null, subId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005022 } finally {
5023 Binder.restoreCallingIdentity(identity);
5024 }
Shishir Agrawal302c8692015-06-19 13:49:39 -07005025 }
5026
5027 /**
yinxub1bed742017-04-17 11:45:04 -07005028 * Starts a new network scan and returns the id of this scan.
yinxu504e1392017-04-12 16:03:22 -07005029 *
yinxub1bed742017-04-17 11:45:04 -07005030 * @param subId id of the subscription
5031 * @param request contains the radio access networks with bands/channels to scan
5032 * @param messenger callback messenger for scan results or errors
5033 * @param binder for the purpose of auto clean when the user thread crashes
yinxu504e1392017-04-12 16:03:22 -07005034 * @return the id of the requested scan which can be used to stop the scan.
5035 */
5036 @Override
5037 public int requestNetworkScan(int subId, NetworkScanRequest request, Messenger messenger,
Philip P. Moltmann9797cbb2020-01-07 13:45:00 -08005038 IBinder binder, String callingPackage, String callingFeatureId) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08005039 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
5040 mApp, subId, "requestNetworkScan");
Hall Liuf19c44f2018-11-27 14:38:17 -08005041 LocationAccessPolicy.LocationPermissionResult locationResult =
5042 LocationAccessPolicy.checkLocationPermission(mApp,
5043 new LocationAccessPolicy.LocationPermissionQuery.Builder()
5044 .setCallingPackage(callingPackage)
Philip P. Moltmann9797cbb2020-01-07 13:45:00 -08005045 .setCallingFeatureId(callingFeatureId)
Hall Liuf19c44f2018-11-27 14:38:17 -08005046 .setCallingPid(Binder.getCallingPid())
5047 .setCallingUid(Binder.getCallingUid())
5048 .setMethod("requestNetworkScan")
5049 .setMinSdkVersionForFine(Build.VERSION_CODES.Q)
5050 .build());
Hall Liub2ac8ef2019-02-28 15:56:23 -08005051 if (locationResult != LocationAccessPolicy.LocationPermissionResult.ALLOWED) {
Hall Liu558027f2019-05-15 19:14:05 -07005052 SecurityException e = checkNetworkRequestForSanitizedLocationAccess(request, subId);
Hall Liub2ac8ef2019-02-28 15:56:23 -08005053 if (e != null) {
5054 if (locationResult == LocationAccessPolicy.LocationPermissionResult.DENIED_HARD) {
5055 throw e;
5056 } else {
Hall Liu0e5abaf2019-04-04 01:25:30 -07005057 loge(e.getMessage());
Hall Liub2ac8ef2019-02-28 15:56:23 -08005058 return TelephonyScanManager.INVALID_SCAN_ID;
5059 }
5060 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005061 }
Hall Liu912dfd32019-04-25 14:02:26 -07005062 int callingUid = Binder.getCallingUid();
5063 int callingPid = Binder.getCallingPid();
Ying Xu94a46582019-04-18 17:14:56 -07005064 final long identity = Binder.clearCallingIdentity();
5065 try {
5066 return mNetworkScanRequestTracker.startNetworkScan(
5067 request, messenger, binder, getPhone(subId),
Hall Liu912dfd32019-04-25 14:02:26 -07005068 callingUid, callingPid, callingPackage);
Ying Xu94a46582019-04-18 17:14:56 -07005069 } finally {
5070 Binder.restoreCallingIdentity(identity);
5071 }
yinxu504e1392017-04-12 16:03:22 -07005072 }
5073
Hall Liub2ac8ef2019-02-28 15:56:23 -08005074 private SecurityException checkNetworkRequestForSanitizedLocationAccess(
Hall Liu558027f2019-05-15 19:14:05 -07005075 NetworkScanRequest request, int subId) {
5076 boolean hasCarrierPriv = getCarrierPrivilegeStatusForUid(subId, Binder.getCallingUid())
5077 == TelephonyManager.CARRIER_PRIVILEGE_STATUS_HAS_ACCESS;
5078 boolean hasNetworkScanPermission =
5079 mApp.checkCallingOrSelfPermission(android.Manifest.permission.NETWORK_SCAN)
5080 == PERMISSION_GRANTED;
5081
5082 if (!hasCarrierPriv && !hasNetworkScanPermission) {
5083 return new SecurityException("permission.NETWORK_SCAN or carrier privileges is needed"
5084 + " for network scans without location access.");
Hall Liub2ac8ef2019-02-28 15:56:23 -08005085 }
5086
5087 if (request.getSpecifiers() != null && request.getSpecifiers().length > 0) {
5088 for (RadioAccessSpecifier ras : request.getSpecifiers()) {
Hall Liub2ac8ef2019-02-28 15:56:23 -08005089 if (ras.getChannels() != null && ras.getChannels().length > 0) {
5090 return new SecurityException("Specific channels must not be"
5091 + " scanned without location access.");
5092 }
5093 }
5094 }
5095
Hall Liub2ac8ef2019-02-28 15:56:23 -08005096 return null;
5097 }
5098
yinxu504e1392017-04-12 16:03:22 -07005099 /**
5100 * Stops an existing network scan with the given scanId.
yinxub1bed742017-04-17 11:45:04 -07005101 *
5102 * @param subId id of the subscription
5103 * @param scanId id of the scan that needs to be stopped
yinxu504e1392017-04-12 16:03:22 -07005104 */
5105 @Override
5106 public void stopNetworkScan(int subId, int scanId) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08005107 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
5108 mApp, subId, "stopNetworkScan");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005109
Hall Liu912dfd32019-04-25 14:02:26 -07005110 int callingUid = Binder.getCallingUid();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005111 final long identity = Binder.clearCallingIdentity();
5112 try {
Hall Liu912dfd32019-04-25 14:02:26 -07005113 mNetworkScanRequestTracker.stopNetworkScan(scanId, callingUid);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005114 } finally {
5115 Binder.restoreCallingIdentity(identity);
5116 }
yinxu504e1392017-04-12 16:03:22 -07005117 }
5118
5119 /**
Junda Liu84d15a22014-07-02 11:21:04 -07005120 * Get the calculated preferred network type.
5121 * Used for debugging incorrect network type.
5122 *
5123 * @return the preferred network type, defined in RILConstants.java.
5124 */
5125 @Override
Philip P. Moltmann9797cbb2020-01-07 13:45:00 -08005126 public int getCalculatedPreferredNetworkType(String callingPackage, String callingFeatureId) {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08005127 final Phone defaultPhone = getDefaultPhone();
5128 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(mApp, defaultPhone.getSubId(),
Philip P. Moltmann9797cbb2020-01-07 13:45:00 -08005129 callingPackage, callingFeatureId, "getCalculatedPreferredNetworkType")) {
Svet Ganovb320e182015-04-16 12:30:10 -07005130 return RILConstants.PREFERRED_NETWORK_MODE;
5131 }
5132
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005133 final long identity = Binder.clearCallingIdentity();
5134 try {
5135 // FIXME: need to get SubId from somewhere.
Nazanin Bakhshif782e562018-12-11 15:15:39 -08005136 return PhoneFactory.calculatePreferredNetworkType(defaultPhone.getContext(), 0);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005137 } finally {
5138 Binder.restoreCallingIdentity(identity);
5139 }
Junda Liu84d15a22014-07-02 11:21:04 -07005140 }
5141
5142 /**
Jake Hamby7c27be32014-03-03 13:25:59 -08005143 * Get the preferred network type.
5144 * Used for device configuration by some CDMA operators.
5145 *
5146 * @return the preferred network type, defined in RILConstants.java.
5147 */
5148 @Override
Stuart Scott54788802015-03-30 13:18:01 -07005149 public int getPreferredNetworkType(int subId) {
Pengquan Menga4009cb2018-12-20 11:00:24 -08005150 TelephonyPermissions
5151 .enforeceCallingOrSelfReadPrivilegedPhoneStatePermissionOrCarrierPrivilege(
5152 mApp, subId, "getPreferredNetworkType");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005153
5154 final long identity = Binder.clearCallingIdentity();
5155 try {
5156 if (DBG) log("getPreferredNetworkType");
5157 int[] result = (int[]) sendRequest(CMD_GET_PREFERRED_NETWORK_TYPE, null, subId);
5158 int networkType = (result != null ? result[0] : -1);
5159 if (DBG) log("getPreferredNetworkType: " + networkType);
5160 return networkType;
5161 } finally {
5162 Binder.restoreCallingIdentity(identity);
5163 }
Jake Hamby7c27be32014-03-03 13:25:59 -08005164 }
5165
5166 /**
5167 * Set the preferred network type.
5168 * Used for device configuration by some CDMA operators.
5169 *
5170 * @param networkType the preferred network type, defined in RILConstants.java.
5171 * @return true on success; false on any failure.
5172 */
5173 @Override
Stuart Scott54788802015-03-30 13:18:01 -07005174 public boolean setPreferredNetworkType(int subId, int networkType) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08005175 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
5176 mApp, subId, "setPreferredNetworkType");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005177
5178 final long identity = Binder.clearCallingIdentity();
5179 try {
calvinpan677fc2b2020-01-14 20:42:55 +08005180 Settings.Global.putInt(mApp.getContentResolver(),
5181 Settings.Global.PREFERRED_NETWORK_MODE + subId, networkType);
5182 return setPreferredNetworkTypesInternal(subId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005183 } finally {
5184 Binder.restoreCallingIdentity(identity);
Junda Liu80bc0d12014-07-14 16:36:44 -07005185 }
Jake Hamby7c27be32014-03-03 13:25:59 -08005186 }
Robert Greenwalted86e582014-05-21 20:03:20 -07005187
5188 /**
calvinpan677fc2b2020-01-14 20:42:55 +08005189 * Get the allowed network types that store in the telephony provider.
5190 *
5191 * @param subId the id of the subscription.
5192 * @return allowedNetworkTypes the allowed network types.
5193 */
5194 @Override
5195 public long getAllowedNetworkTypes(int subId) {
5196 TelephonyPermissions
5197 .enforeceCallingOrSelfReadPrivilegedPhoneStatePermissionOrCarrierPrivilege(
5198 mApp, subId, "getAllowedNetworkTypes");
5199
5200 final long identity = Binder.clearCallingIdentity();
5201 try {
5202 return SubscriptionManager.getLongSubscriptionProperty(
5203 subId, SubscriptionManager.ALLOWED_NETWORK_TYPES, -1, mApp);
5204 } finally {
5205 Binder.restoreCallingIdentity(identity);
5206 }
5207 }
5208
5209 /**
5210 * Set the allowed network types.
5211 *
5212 * @param subId the id of the subscription.
5213 * @param allowedNetworkTypes the allowed network types.
5214 * @return true on success; false on any failure.
5215 */
5216 @Override
5217 public boolean setAllowedNetworkTypes(int subId, long allowedNetworkTypes) {
5218 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
5219 mApp, subId, "setAllowedNetworkTypes");
5220 final long identity = Binder.clearCallingIdentity();
5221 try {
5222 SubscriptionManager.setSubscriptionProperty(subId,
5223 SubscriptionManager.ALLOWED_NETWORK_TYPES,
5224 String.valueOf(allowedNetworkTypes));
5225 return setPreferredNetworkTypesInternal(subId);
5226 } finally {
5227 Binder.restoreCallingIdentity(identity);
5228 }
5229 }
5230
5231 private boolean setPreferredNetworkTypesInternal(int subId) {
5232 long networkTypeBitMask = RadioAccessFamily.getRafFromNetworkType(
5233 Settings.Global.getInt(mApp.getContentResolver(),
5234 Settings.Global.PREFERRED_NETWORK_MODE + subId,
5235 RILConstants.PREFERRED_NETWORK_MODE));
5236 long allowedNetworkTypes = SubscriptionManager.getLongSubscriptionProperty(
5237 subId, SubscriptionManager.ALLOWED_NETWORK_TYPES, -1, mApp);
5238 int networkMode = RadioAccessFamily.getNetworkTypeFromRaf(
5239 (int) (networkTypeBitMask & allowedNetworkTypes));
5240
5241 if (DBG) {
5242 log("setPreferredNetworkTypesInternal: subId " + subId
5243 + " networkTypes " + networkTypeBitMask
5244 + " allowedNetworkTypes " + allowedNetworkTypes
5245 + " networkMode " + networkMode);
5246 }
5247
5248 Boolean success = (Boolean) sendRequest(
5249 CMD_SET_PREFERRED_NETWORK_TYPE, networkMode, subId);
5250 if (DBG) log("setPreferredNetworkTypesInternal: " + (success ? "ok" : "fail"));
5251 return success;
5252 }
5253
5254 /**
Miaoa84611c2019-03-15 09:21:10 +08005255 * Check whether DUN APN is required for tethering with subId.
Junda Liu475951f2014-11-07 16:45:03 -08005256 *
Miaoa84611c2019-03-15 09:21:10 +08005257 * @param subId the id of the subscription to require tethering.
Amit Mahajanfe58cdf2017-07-11 12:01:53 -07005258 * @return {@code true} if DUN APN is required for tethering.
Junda Liu475951f2014-11-07 16:45:03 -08005259 * @hide
5260 */
5261 @Override
SongFerngWangf08d8122019-11-15 14:58:44 +08005262 public boolean isTetheringApnRequiredForSubscriber(int subId) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08005263 enforceModifyPermission();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005264 final long identity = Binder.clearCallingIdentity();
Miaoa84611c2019-03-15 09:21:10 +08005265 final Phone phone = getPhone(subId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005266 try {
Miaoa84611c2019-03-15 09:21:10 +08005267 if (phone != null) {
5268 return phone.hasMatchedTetherApnSetting();
5269 } else {
5270 return false;
5271 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005272 } finally {
5273 Binder.restoreCallingIdentity(identity);
Junda Liu475951f2014-11-07 16:45:03 -08005274 }
Junda Liu475951f2014-11-07 16:45:03 -08005275 }
5276
5277 /**
Robert Greenwalted86e582014-05-21 20:03:20 -07005278 * Set mobile data enabled
5279 * Used by the user through settings etc to turn on/off mobile data
5280 *
5281 * @param enable {@code true} turn turn data on, else {@code false}
5282 */
5283 @Override
Malcolm Chen964682d2017-11-28 16:20:07 -08005284 public void setUserDataEnabled(int subId, boolean enable) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08005285 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
5286 mApp, subId, "setUserDataEnabled");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005287
5288 final long identity = Binder.clearCallingIdentity();
5289 try {
5290 int phoneId = mSubscriptionController.getPhoneId(subId);
5291 if (DBG) log("setUserDataEnabled: subId=" + subId + " phoneId=" + phoneId);
5292 Phone phone = PhoneFactory.getPhone(phoneId);
5293 if (phone != null) {
5294 if (DBG) log("setUserDataEnabled: subId=" + subId + " enable=" + enable);
Jack Yud79fba22018-12-13 11:51:28 -08005295 phone.getDataEnabledSettings().setUserDataEnabled(enable);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005296 } else {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08005297 loge("setUserDataEnabled: no phone found. Invalid subId=" + subId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005298 }
5299 } finally {
5300 Binder.restoreCallingIdentity(identity);
Wink Savillee7353bb2014-12-05 14:21:41 -08005301 }
Robert Greenwalted86e582014-05-21 20:03:20 -07005302 }
5303
5304 /**
Shuo Qian5bac1ee2020-01-16 20:51:11 -08005305 * Enable or disable always reporting signal strength changes from radio.
5306 *
5307 * @param isEnable {@code true} for enabling; {@code false} for disabling.
5308 */
5309 @Override
5310 public void setAlwaysReportSignalStrength(int subId, boolean isEnable) {
5311 enforceModifyPermission();
5312 enforceSystemCaller();
5313
5314 final long identity = Binder.clearCallingIdentity();
5315 final Phone phone = getPhone(subId);
5316 try {
5317 if (phone != null) {
5318 if (DBG) {
5319 log("setAlwaysReportSignalStrength: subId=" + subId
5320 + " isEnable=" + isEnable);
5321 }
5322 phone.setAlwaysReportSignalStrength(isEnable);
5323 } else {
5324 loge("setAlwaysReportSignalStrength: no phone found for subId="
5325 + subId);
5326 }
5327 } finally {
5328 Binder.restoreCallingIdentity(identity);
5329 }
5330 }
5331
5332 /**
Malcolm Chen964682d2017-11-28 16:20:07 -08005333 * Get the user enabled state of Mobile Data.
5334 *
5335 * TODO: remove and use isUserDataEnabled.
5336 * This can't be removed now because some vendor codes
5337 * calls through ITelephony directly while they should
5338 * use TelephonyManager.
5339 *
5340 * @return true on enabled
5341 */
5342 @Override
5343 public boolean getDataEnabled(int subId) {
5344 return isUserDataEnabled(subId);
5345 }
5346
5347 /**
5348 * Get whether mobile data is enabled per user setting.
5349 *
5350 * There are other factors deciding whether mobile data is actually enabled, but they are
5351 * not considered here. See {@link #isDataEnabled(int)} for more details.
Robert Greenwalt646120a2014-05-23 11:54:03 -07005352 *
Jeff Davidsona1920712016-11-18 17:05:56 -08005353 * Accepts either ACCESS_NETWORK_STATE, MODIFY_PHONE_STATE or carrier privileges.
Robert Greenwalted86e582014-05-21 20:03:20 -07005354 *
5355 * @return {@code true} if data is enabled else {@code false}
5356 */
5357 @Override
Malcolm Chen964682d2017-11-28 16:20:07 -08005358 public boolean isUserDataEnabled(int subId) {
Robert Greenwalt646120a2014-05-23 11:54:03 -07005359 try {
5360 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.ACCESS_NETWORK_STATE,
5361 null);
5362 } catch (Exception e) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08005363 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
5364 mApp, subId, "isUserDataEnabled");
Robert Greenwalt646120a2014-05-23 11:54:03 -07005365 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005366
5367 final long identity = Binder.clearCallingIdentity();
5368 try {
5369 int phoneId = mSubscriptionController.getPhoneId(subId);
5370 if (DBG) log("isUserDataEnabled: subId=" + subId + " phoneId=" + phoneId);
5371 Phone phone = PhoneFactory.getPhone(phoneId);
5372 if (phone != null) {
5373 boolean retVal = phone.isUserDataEnabled();
5374 if (DBG) log("isUserDataEnabled: subId=" + subId + " retVal=" + retVal);
5375 return retVal;
5376 } else {
5377 if (DBG) loge("isUserDataEnabled: no phone subId=" + subId + " retVal=false");
5378 return false;
5379 }
5380 } finally {
5381 Binder.restoreCallingIdentity(identity);
Malcolm Chen964682d2017-11-28 16:20:07 -08005382 }
5383 }
5384
5385 /**
5386 * Get whether mobile data is enabled.
5387 *
5388 * Comparable to {@link #isUserDataEnabled(int)}, this considers all factors deciding
5389 * whether mobile data is actually enabled.
5390 *
5391 * Accepts either ACCESS_NETWORK_STATE, MODIFY_PHONE_STATE or carrier privileges.
5392 *
5393 * @return {@code true} if data is enabled else {@code false}
5394 */
5395 @Override
5396 public boolean isDataEnabled(int subId) {
5397 try {
5398 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.ACCESS_NETWORK_STATE,
5399 null);
5400 } catch (Exception e) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08005401 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
5402 mApp, subId, "isDataEnabled");
Malcolm Chen964682d2017-11-28 16:20:07 -08005403 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005404
5405 final long identity = Binder.clearCallingIdentity();
5406 try {
5407 int phoneId = mSubscriptionController.getPhoneId(subId);
5408 if (DBG) log("isDataEnabled: subId=" + subId + " phoneId=" + phoneId);
5409 Phone phone = PhoneFactory.getPhone(phoneId);
5410 if (phone != null) {
Jack Yud79fba22018-12-13 11:51:28 -08005411 boolean retVal = phone.getDataEnabledSettings().isDataEnabled();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005412 if (DBG) log("isDataEnabled: subId=" + subId + " retVal=" + retVal);
5413 return retVal;
5414 } else {
5415 if (DBG) loge("isDataEnabled: no phone subId=" + subId + " retVal=false");
5416 return false;
5417 }
5418 } finally {
5419 Binder.restoreCallingIdentity(identity);
Wink Savillee7353bb2014-12-05 14:21:41 -08005420 }
Robert Greenwalted86e582014-05-21 20:03:20 -07005421 }
Shishir Agrawal60f9c952014-06-23 12:00:43 -07005422
Nazanin Bakhshi5d0636e2019-08-19 16:29:37 -07005423 private int getCarrierPrivilegeStatusFromCarrierConfigRules(int privilegeFromSim,
5424 Phone phone) {
5425 //load access rules from carrier configs, and check those as well: b/139133814
5426 SubscriptionController subController = SubscriptionController.getInstance();
5427 if (privilegeFromSim == TelephonyManager.CARRIER_PRIVILEGE_STATUS_HAS_ACCESS
5428 || subController == null) return privilegeFromSim;
5429
5430 int uid = Binder.getCallingUid();
5431 PackageManager pkgMgr = phone.getContext().getPackageManager();
5432 String[] packages = pkgMgr.getPackagesForUid(uid);
5433
5434 final long identity = Binder.clearCallingIdentity();
5435 try {
5436 SubscriptionInfo subInfo = subController.getSubscriptionInfo(phone.getSubId());
5437 SubscriptionManager subManager = (SubscriptionManager)
5438 phone.getContext().getSystemService(Context.TELEPHONY_SUBSCRIPTION_SERVICE);
5439 for (String pkg : packages) {
5440 if (subManager.canManageSubscription(subInfo, pkg)) {
5441 return TelephonyManager.CARRIER_PRIVILEGE_STATUS_HAS_ACCESS;
5442 }
5443 }
5444 return privilegeFromSim;
5445 } finally {
5446 Binder.restoreCallingIdentity(identity);
5447 }
5448 }
5449
5450 private int getCarrierPrivilegeStatusFromCarrierConfigRules(int privilegeFromSim, Phone phone,
5451 String pkgName) {
5452 //load access rules from carrier configs, and check those as well: b/139133814
5453 SubscriptionController subController = SubscriptionController.getInstance();
5454 if (privilegeFromSim == TelephonyManager.CARRIER_PRIVILEGE_STATUS_HAS_ACCESS
5455 || subController == null) return privilegeFromSim;
5456
5457 final long identity = Binder.clearCallingIdentity();
5458 try {
5459 SubscriptionInfo subInfo = subController.getSubscriptionInfo(phone.getSubId());
5460 SubscriptionManager subManager = (SubscriptionManager)
5461 phone.getContext().getSystemService(Context.TELEPHONY_SUBSCRIPTION_SERVICE);
5462 return subManager.canManageSubscription(subInfo, pkgName)
5463 ? TelephonyManager.CARRIER_PRIVILEGE_STATUS_HAS_ACCESS : privilegeFromSim;
5464 } finally {
5465 Binder.restoreCallingIdentity(identity);
5466 }
5467 }
5468
Shishir Agrawal60f9c952014-06-23 12:00:43 -07005469 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08005470 public int getCarrierPrivilegeStatus(int subId) {
5471 final Phone phone = getPhone(subId);
5472 if (phone == null) {
5473 loge("getCarrierPrivilegeStatus: Invalid subId");
5474 return TelephonyManager.CARRIER_PRIVILEGE_STATUS_NO_ACCESS;
5475 }
5476 UiccCard card = UiccController.getInstance().getUiccCard(phone.getPhoneId());
Shishir Agrawaleb8771e2014-07-22 11:24:08 -07005477 if (card == null) {
Shishir Agrawal5e5becd2014-11-18 11:38:23 -08005478 loge("getCarrierPrivilegeStatus: No UICC");
Shishir Agrawaleb8771e2014-07-22 11:24:08 -07005479 return TelephonyManager.CARRIER_PRIVILEGE_STATUS_RULES_NOT_LOADED;
5480 }
Nazanin Bakhshi5d0636e2019-08-19 16:29:37 -07005481
5482 return getCarrierPrivilegeStatusFromCarrierConfigRules(
5483 card.getCarrierPrivilegeStatusForCurrentTransaction(
5484 phone.getContext().getPackageManager()), phone);
Shishir Agrawal60f9c952014-06-23 12:00:43 -07005485 }
Junda Liu29340342014-07-10 15:23:27 -07005486
5487 @Override
Jeff Davidson7e17e312018-02-13 18:17:36 -08005488 public int getCarrierPrivilegeStatusForUid(int subId, int uid) {
Shuo Qian6d927452019-12-05 11:40:37 -08005489 enforceReadPrivilegedPermission("getCarrierPrivilegeStatusForUid");
Jeff Davidson7e17e312018-02-13 18:17:36 -08005490 final Phone phone = getPhone(subId);
5491 if (phone == null) {
Taesu Leef8fbed92019-10-07 18:47:02 +09005492 loge("getCarrierPrivilegeStatusForUid: Invalid subId");
Jeff Davidson7e17e312018-02-13 18:17:36 -08005493 return TelephonyManager.CARRIER_PRIVILEGE_STATUS_NO_ACCESS;
5494 }
5495 UiccProfile profile =
5496 UiccController.getInstance().getUiccProfileForPhone(phone.getPhoneId());
5497 if (profile == null) {
Taesu Leef8fbed92019-10-07 18:47:02 +09005498 loge("getCarrierPrivilegeStatusForUid: No UICC");
Jeff Davidson7e17e312018-02-13 18:17:36 -08005499 return TelephonyManager.CARRIER_PRIVILEGE_STATUS_RULES_NOT_LOADED;
5500 }
Nazanin Bakhshi5d0636e2019-08-19 16:29:37 -07005501 return getCarrierPrivilegeStatusFromCarrierConfigRules(
Shuo Qian6d927452019-12-05 11:40:37 -08005502 profile.getCarrierPrivilegeStatusForUid(
5503 phone.getContext().getPackageManager(), uid), phone);
Jeff Davidson7e17e312018-02-13 18:17:36 -08005504 }
5505
5506 @Override
chen xuf7e9fe82019-05-09 19:31:02 -07005507 public int checkCarrierPrivilegesForPackage(int subId, String pkgName) {
5508 if (TextUtils.isEmpty(pkgName)) {
Junda Liu317d70b2016-03-08 09:33:53 -08005509 return TelephonyManager.CARRIER_PRIVILEGE_STATUS_NO_ACCESS;
chen xuf7e9fe82019-05-09 19:31:02 -07005510 }
5511
5512 int phoneId = SubscriptionManager.getPhoneId(subId);
5513 UiccCard card = UiccController.getInstance().getUiccCard(phoneId);
Shishir Agrawaleb8771e2014-07-22 11:24:08 -07005514 if (card == null) {
chen xuf7e9fe82019-05-09 19:31:02 -07005515 loge("checkCarrierPrivilegesForPackage: No UICC on subId " + subId);
Shishir Agrawaleb8771e2014-07-22 11:24:08 -07005516 return TelephonyManager.CARRIER_PRIVILEGE_STATUS_RULES_NOT_LOADED;
5517 }
Nazanin Bakhshi5d0636e2019-08-19 16:29:37 -07005518 return getCarrierPrivilegeStatusFromCarrierConfigRules(
5519 card.getCarrierPrivilegeStatus(mApp.getPackageManager(), pkgName),
5520 getPhone(phoneId), pkgName);
Zach Johnson50ecba32015-05-19 00:24:21 -07005521 }
5522
5523 @Override
5524 public int checkCarrierPrivilegesForPackageAnyPhone(String pkgName) {
Junda Liu317d70b2016-03-08 09:33:53 -08005525 if (TextUtils.isEmpty(pkgName))
5526 return TelephonyManager.CARRIER_PRIVILEGE_STATUS_NO_ACCESS;
Zach Johnson50ecba32015-05-19 00:24:21 -07005527 int result = TelephonyManager.CARRIER_PRIVILEGE_STATUS_RULES_NOT_LOADED;
5528 for (int i = 0; i < TelephonyManager.getDefault().getPhoneCount(); i++) {
5529 UiccCard card = UiccController.getInstance().getUiccCard(i);
5530 if (card == null) {
Jonathan Basseri7d320df2015-06-16 12:17:08 -07005531 // No UICC in that slot.
Zach Johnson50ecba32015-05-19 00:24:21 -07005532 continue;
5533 }
5534
Nazanin Bakhshi5d0636e2019-08-19 16:29:37 -07005535 result = getCarrierPrivilegeStatusFromCarrierConfigRules(
5536 card.getCarrierPrivilegeStatus(mApp.getPackageManager(), pkgName),
5537 getPhone(i), pkgName);
Zach Johnson50ecba32015-05-19 00:24:21 -07005538 if (result == TelephonyManager.CARRIER_PRIVILEGE_STATUS_HAS_ACCESS) {
5539 break;
5540 }
5541 }
5542
5543 return result;
Junda Liu29340342014-07-10 15:23:27 -07005544 }
Derek Tan89e89d42014-07-08 17:00:10 -07005545
5546 @Override
Junda Liue64de782015-04-16 17:19:16 -07005547 public List<String> getCarrierPackageNamesForIntentAndPhone(Intent intent, int phoneId) {
5548 if (!SubscriptionManager.isValidPhoneId(phoneId)) {
5549 loge("phoneId " + phoneId + " is not valid.");
5550 return null;
5551 }
5552 UiccCard card = UiccController.getInstance().getUiccCard(phoneId);
Shishir Agrawaleb6439a2014-07-21 13:19:38 -07005553 if (card == null) {
Taesu Leef8fbed92019-10-07 18:47:02 +09005554 loge("getCarrierPackageNamesForIntentAndPhone: No UICC");
Shishir Agrawaleb6439a2014-07-21 13:19:38 -07005555 return null ;
5556 }
Nazanin Bakhshif782e562018-12-11 15:15:39 -08005557 return card.getCarrierPackageNamesForIntent(mApp.getPackageManager(), intent);
Shishir Agrawaleb6439a2014-07-21 13:19:38 -07005558 }
5559
Amith Yamasani6e118872016-02-19 12:53:51 -08005560 @Override
chen xuf7e9fe82019-05-09 19:31:02 -07005561 public List<String> getPackagesWithCarrierPrivileges(int phoneId) {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08005562 PackageManager pm = mApp.getPackageManager();
Amith Yamasani6e118872016-02-19 12:53:51 -08005563 List<String> privilegedPackages = new ArrayList<>();
5564 List<PackageInfo> packages = null;
chen xuf7e9fe82019-05-09 19:31:02 -07005565 UiccCard card = UiccController.getInstance().getUiccCard(phoneId);
5566 // has UICC in that slot.
5567 if (card != null) {
Amith Yamasani6e118872016-02-19 12:53:51 -08005568 if (card.hasCarrierPrivilegeRules()) {
5569 if (packages == null) {
5570 // Only check packages in user 0 for now
5571 packages = pm.getInstalledPackagesAsUser(
Nazanin Bakhshi5d0636e2019-08-19 16:29:37 -07005572 PackageManager.MATCH_DISABLED_COMPONENTS
5573 | PackageManager.MATCH_DISABLED_UNTIL_USED_COMPONENTS
Cheonho Park17089c62019-08-01 15:23:12 +09005574 | PackageManager.GET_SIGNING_CERTIFICATES,
5575 UserHandle.USER_SYSTEM);
Amith Yamasani6e118872016-02-19 12:53:51 -08005576 }
5577 for (int p = packages.size() - 1; p >= 0; p--) {
5578 PackageInfo pkgInfo = packages.get(p);
5579 if (pkgInfo != null && pkgInfo.packageName != null
5580 && card.getCarrierPrivilegeStatus(pkgInfo)
chen xuf7e9fe82019-05-09 19:31:02 -07005581 == TelephonyManager.CARRIER_PRIVILEGE_STATUS_HAS_ACCESS) {
Amith Yamasani6e118872016-02-19 12:53:51 -08005582 privilegedPackages.add(pkgInfo.packageName);
5583 }
5584 }
5585 }
5586 }
5587 return privilegedPackages;
5588 }
5589
chen xuf7e9fe82019-05-09 19:31:02 -07005590 @Override
5591 public List<String> getPackagesWithCarrierPrivilegesForAllPhones() {
Shuo Qiand54f9f32019-12-03 23:40:18 +00005592 enforceReadPrivilegedPermission("getPackagesWithCarrierPrivilegesForAllPhones");
5593
5594 final long identity = Binder.clearCallingIdentity();
5595
chen xuf7e9fe82019-05-09 19:31:02 -07005596 List<String> privilegedPackages = new ArrayList<>();
Shuo Qiand54f9f32019-12-03 23:40:18 +00005597 try {
5598 for (int i = 0; i < TelephonyManager.getDefault().getPhoneCount(); i++) {
5599 privilegedPackages.addAll(getPackagesWithCarrierPrivileges(i));
5600 }
5601 } finally {
5602 Binder.restoreCallingIdentity(identity);
chen xuf7e9fe82019-05-09 19:31:02 -07005603 }
5604 return privilegedPackages;
5605 }
5606
Wink Savilleb564aae2014-10-23 10:18:09 -07005607 private String getIccId(int subId) {
Sanket Padawe356d7632015-06-22 14:03:32 -07005608 final Phone phone = getPhone(subId);
5609 UiccCard card = phone == null ? null : phone.getUiccCard();
Derek Tan97ebb422014-09-05 16:55:38 -07005610 if (card == null) {
5611 loge("getIccId: No UICC");
5612 return null;
5613 }
5614 String iccId = card.getIccId();
5615 if (TextUtils.isEmpty(iccId)) {
5616 loge("getIccId: ICC ID is null or empty.");
5617 return null;
5618 }
5619 return iccId;
5620 }
5621
Shishir Agrawaleb6439a2014-07-21 13:19:38 -07005622 @Override
Jeff Sharkey85190e62014-12-05 09:40:12 -08005623 public boolean setLine1NumberForDisplayForSubscriber(int subId, String alphaTag,
5624 String number) {
Shuo Qian6d927452019-12-05 11:40:37 -08005625 TelephonyPermissions.enforceCallingOrSelfCarrierPrivilege(mApp,
Jeff Davidson7e17e312018-02-13 18:17:36 -08005626 subId, "setLine1NumberForDisplayForSubscriber");
Derek Tan97ebb422014-09-05 16:55:38 -07005627
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005628 final long identity = Binder.clearCallingIdentity();
5629 try {
5630 final String iccId = getIccId(subId);
5631 final Phone phone = getPhone(subId);
5632 if (phone == null) {
5633 return false;
5634 }
5635 final String subscriberId = phone.getSubscriberId();
5636
5637 if (DBG_MERGE) {
5638 Slog.d(LOG_TAG, "Setting line number for ICC=" + iccId + ", subscriberId="
5639 + subscriberId + " to " + number);
5640 }
5641
5642 if (TextUtils.isEmpty(iccId)) {
5643 return false;
5644 }
5645
5646 final SharedPreferences.Editor editor = mTelephonySharedPreferences.edit();
5647
5648 final String alphaTagPrefKey = PREF_CARRIERS_ALPHATAG_PREFIX + iccId;
5649 if (alphaTag == null) {
5650 editor.remove(alphaTagPrefKey);
5651 } else {
5652 editor.putString(alphaTagPrefKey, alphaTag);
5653 }
5654
5655 // Record both the line number and IMSI for this ICCID, since we need to
5656 // track all merged IMSIs based on line number
5657 final String numberPrefKey = PREF_CARRIERS_NUMBER_PREFIX + iccId;
5658 final String subscriberPrefKey = PREF_CARRIERS_SUBSCRIBER_PREFIX + iccId;
5659 if (number == null) {
5660 editor.remove(numberPrefKey);
5661 editor.remove(subscriberPrefKey);
5662 } else {
5663 editor.putString(numberPrefKey, number);
5664 editor.putString(subscriberPrefKey, subscriberId);
5665 }
5666
5667 editor.commit();
5668 return true;
5669 } finally {
5670 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07005671 }
Derek Tan7226c842014-07-02 17:42:23 -07005672 }
5673
5674 @Override
Philip P. Moltmann9797cbb2020-01-07 13:45:00 -08005675 public String getLine1NumberForDisplay(int subId, String callingPackage,
5676 String callingFeatureId) {
Makoto Onukifee69342015-06-29 14:44:50 -07005677 // This is open to apps with WRITE_SMS.
Jeff Davidson7e17e312018-02-13 18:17:36 -08005678 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneNumber(
Philip P. Moltmann9797cbb2020-01-07 13:45:00 -08005679 mApp, subId, callingPackage, callingFeatureId, "getLine1NumberForDisplay")) {
Amit Mahajan9cf11512015-11-09 11:40:48 -08005680 if (DBG_MERGE) log("getLine1NumberForDisplay returning null due to permission");
Svet Ganovb320e182015-04-16 12:30:10 -07005681 return null;
5682 }
Derek Tan97ebb422014-09-05 16:55:38 -07005683
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005684 final long identity = Binder.clearCallingIdentity();
5685 try {
5686 String iccId = getIccId(subId);
5687 if (iccId != null) {
5688 String numberPrefKey = PREF_CARRIERS_NUMBER_PREFIX + iccId;
5689 if (DBG_MERGE) {
5690 log("getLine1NumberForDisplay returning "
5691 + mTelephonySharedPreferences.getString(numberPrefKey, null));
5692 }
5693 return mTelephonySharedPreferences.getString(numberPrefKey, null);
Amit Mahajan9cf11512015-11-09 11:40:48 -08005694 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005695 if (DBG_MERGE) log("getLine1NumberForDisplay returning null as iccId is null");
5696 return null;
5697 } finally {
5698 Binder.restoreCallingIdentity(identity);
Derek Tan7226c842014-07-02 17:42:23 -07005699 }
Derek Tan7226c842014-07-02 17:42:23 -07005700 }
5701
5702 @Override
Philip P. Moltmann9797cbb2020-01-07 13:45:00 -08005703 public String getLine1AlphaTagForDisplay(int subId, String callingPackage,
5704 String callingFeatureId) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08005705 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
Philip P. Moltmann9797cbb2020-01-07 13:45:00 -08005706 mApp, subId, callingPackage, callingFeatureId, "getLine1AlphaTagForDisplay")) {
Svet Ganovb320e182015-04-16 12:30:10 -07005707 return null;
5708 }
Derek Tan97ebb422014-09-05 16:55:38 -07005709
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005710 final long identity = Binder.clearCallingIdentity();
5711 try {
5712 String iccId = getIccId(subId);
5713 if (iccId != null) {
5714 String alphaTagPrefKey = PREF_CARRIERS_ALPHATAG_PREFIX + iccId;
5715 return mTelephonySharedPreferences.getString(alphaTagPrefKey, null);
5716 }
5717 return null;
5718 } finally {
5719 Binder.restoreCallingIdentity(identity);
Derek Tan7226c842014-07-02 17:42:23 -07005720 }
Derek Tan7226c842014-07-02 17:42:23 -07005721 }
Shishir Agrawalb1ebf8c2014-07-17 16:32:41 -07005722
5723 @Override
Philip P. Moltmann9797cbb2020-01-07 13:45:00 -08005724 public String[] getMergedSubscriberIds(int subId, String callingPackage,
5725 String callingFeatureId) {
Jeff Davidson913390f2018-02-23 17:11:49 -08005726 // This API isn't public, so no need to provide a valid subscription ID - we're not worried
5727 // about carrier-privileged callers not having access.
Jeff Davidson7e17e312018-02-13 18:17:36 -08005728 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
Jeff Davidson913390f2018-02-23 17:11:49 -08005729 mApp, SubscriptionManager.INVALID_SUBSCRIPTION_ID, callingPackage,
Philip P. Moltmann9797cbb2020-01-07 13:45:00 -08005730 callingFeatureId, "getMergedSubscriberIds")) {
Fyodor Kupolov8e53b0b2015-06-17 13:17:50 -07005731 return null;
5732 }
Jeff Sharkey85190e62014-12-05 09:40:12 -08005733
Jordan Liub49b04b2019-05-06 14:45:15 -07005734 // Clear calling identity, when calling TelephonyManager, because callerUid must be
5735 // the process, where TelephonyManager was instantiated.
5736 // Otherwise AppOps check will fail.
Fyodor Kupolov8e53b0b2015-06-17 13:17:50 -07005737 final long identity = Binder.clearCallingIdentity();
5738 try {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08005739 final Context context = mApp;
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005740 final TelephonyManager tele = TelephonyManager.from(context);
5741 final SubscriptionManager sub = SubscriptionManager.from(context);
5742
5743 // Figure out what subscribers are currently active
5744 final ArraySet<String> activeSubscriberIds = new ArraySet<>();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005745
Jordan Liub49b04b2019-05-06 14:45:15 -07005746 // Only consider subs which match the current subId
5747 // This logic can be simplified. See b/131189269 for progress.
5748 if (isActiveSubscription(subId)) {
Fyodor Kupolov8e53b0b2015-06-17 13:17:50 -07005749 activeSubscriberIds.add(tele.getSubscriberId(subId));
5750 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005751
5752 // First pass, find a number override for an active subscriber
5753 String mergeNumber = null;
5754 final Map<String, ?> prefs = mTelephonySharedPreferences.getAll();
5755 for (String key : prefs.keySet()) {
5756 if (key.startsWith(PREF_CARRIERS_SUBSCRIBER_PREFIX)) {
5757 final String subscriberId = (String) prefs.get(key);
5758 if (activeSubscriberIds.contains(subscriberId)) {
5759 final String iccId = key.substring(
5760 PREF_CARRIERS_SUBSCRIBER_PREFIX.length());
5761 final String numberKey = PREF_CARRIERS_NUMBER_PREFIX + iccId;
5762 mergeNumber = (String) prefs.get(numberKey);
5763 if (DBG_MERGE) {
5764 Slog.d(LOG_TAG, "Found line number " + mergeNumber
5765 + " for active subscriber " + subscriberId);
5766 }
5767 if (!TextUtils.isEmpty(mergeNumber)) {
5768 break;
5769 }
5770 }
5771 }
5772 }
5773
5774 // Shortcut when no active merged subscribers
5775 if (TextUtils.isEmpty(mergeNumber)) {
5776 return null;
5777 }
5778
5779 // Second pass, find all subscribers under that line override
5780 final ArraySet<String> result = new ArraySet<>();
5781 for (String key : prefs.keySet()) {
5782 if (key.startsWith(PREF_CARRIERS_NUMBER_PREFIX)) {
5783 final String number = (String) prefs.get(key);
5784 if (mergeNumber.equals(number)) {
5785 final String iccId = key.substring(PREF_CARRIERS_NUMBER_PREFIX.length());
5786 final String subscriberKey = PREF_CARRIERS_SUBSCRIBER_PREFIX + iccId;
5787 final String subscriberId = (String) prefs.get(subscriberKey);
5788 if (!TextUtils.isEmpty(subscriberId)) {
5789 result.add(subscriberId);
5790 }
5791 }
5792 }
5793 }
5794
5795 final String[] resultArray = result.toArray(new String[result.size()]);
5796 Arrays.sort(resultArray);
5797 if (DBG_MERGE) {
5798 Slog.d(LOG_TAG,
5799 "Found subscribers " + Arrays.toString(resultArray) + " after merge");
5800 }
5801 return resultArray;
Fyodor Kupolov8e53b0b2015-06-17 13:17:50 -07005802 } finally {
5803 Binder.restoreCallingIdentity(identity);
Jeff Sharkey85190e62014-12-05 09:40:12 -08005804 }
Jeff Sharkey85190e62014-12-05 09:40:12 -08005805 }
5806
5807 @Override
Malcolm Chen6ca97372019-07-01 16:28:21 -07005808 public String[] getMergedSubscriberIdsFromGroup(int subId, String callingPackage) {
5809 enforceReadPrivilegedPermission("getMergedSubscriberIdsFromGroup");
5810
5811 final long identity = Binder.clearCallingIdentity();
5812 try {
5813 final TelephonyManager telephonyManager = mApp.getSystemService(
5814 TelephonyManager.class);
5815 String subscriberId = telephonyManager.getSubscriberId(subId);
5816 if (subscriberId == null) {
5817 if (DBG) {
5818 log("getMergedSubscriberIdsFromGroup can't find subscriberId for subId "
5819 + subId);
5820 }
5821 return null;
5822 }
5823
5824 final SubscriptionInfo info = SubscriptionController.getInstance()
5825 .getSubscriptionInfo(subId);
5826 final ParcelUuid groupUuid = info.getGroupUuid();
5827 // If it doesn't belong to any group, return just subscriberId of itself.
5828 if (groupUuid == null) {
5829 return new String[]{subscriberId};
5830 }
5831
5832 // Get all subscriberIds from the group.
5833 final List<String> mergedSubscriberIds = new ArrayList<>();
5834 final List<SubscriptionInfo> groupInfos = SubscriptionController.getInstance()
Philip P. Moltmann9797cbb2020-01-07 13:45:00 -08005835 .getSubscriptionsInGroup(groupUuid, mApp.getOpPackageName(),
5836 null);
Malcolm Chen6ca97372019-07-01 16:28:21 -07005837 for (SubscriptionInfo subInfo : groupInfos) {
5838 subscriberId = telephonyManager.getSubscriberId(subInfo.getSubscriptionId());
5839 if (subscriberId != null) {
5840 mergedSubscriberIds.add(subscriberId);
5841 }
5842 }
5843
5844 return mergedSubscriberIds.toArray(new String[mergedSubscriberIds.size()]);
5845 } finally {
5846 Binder.restoreCallingIdentity(identity);
5847
5848 }
5849 }
5850
5851 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08005852 public boolean setOperatorBrandOverride(int subId, String brand) {
Shuo Qian6d927452019-12-05 11:40:37 -08005853 TelephonyPermissions.enforceCallingOrSelfCarrierPrivilege(mApp,
Jeff Davidson7e17e312018-02-13 18:17:36 -08005854 subId, "setOperatorBrandOverride");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005855
5856 final long identity = Binder.clearCallingIdentity();
5857 try {
5858 final Phone phone = getPhone(subId);
5859 return phone == null ? false : phone.setOperatorBrandOverride(brand);
5860 } finally {
5861 Binder.restoreCallingIdentity(identity);
5862 }
Shishir Agrawalb1ebf8c2014-07-17 16:32:41 -07005863 }
Steven Liu4bf01bc2014-07-17 11:05:29 -05005864
5865 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08005866 public boolean setRoamingOverride(int subId, List<String> gsmRoamingList,
Shishir Agrawal621a47c2014-12-01 10:25:09 -08005867 List<String> gsmNonRoamingList, List<String> cdmaRoamingList,
5868 List<String> cdmaNonRoamingList) {
Shuo Qian6d927452019-12-05 11:40:37 -08005869 TelephonyPermissions.enforceCallingOrSelfCarrierPrivilege(
5870 mApp, subId, "setRoamingOverride");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005871
5872 final long identity = Binder.clearCallingIdentity();
5873 try {
5874 final Phone phone = getPhone(subId);
5875 if (phone == null) {
5876 return false;
5877 }
5878 return phone.setRoamingOverride(gsmRoamingList, gsmNonRoamingList, cdmaRoamingList,
5879 cdmaNonRoamingList);
5880 } finally {
5881 Binder.restoreCallingIdentity(identity);
Shishir Agrawalc04d9752016-02-19 10:41:00 -08005882 }
Shishir Agrawal621a47c2014-12-01 10:25:09 -08005883 }
5884
5885 @Override
Shuo Qian850e4d6a2018-04-25 21:02:08 +00005886 @Deprecated
5887 public int invokeOemRilRequestRaw(byte[] oemReq, byte[] oemResp) {
5888 enforceModifyPermission();
5889
5890 int returnValue = 0;
5891 try {
vagdeviaf9a5b92018-08-15 16:01:53 -07005892 AsyncResult result = (AsyncResult) sendRequest(CMD_INVOKE_OEM_RIL_REQUEST_RAW, oemReq);
Shuo Qian850e4d6a2018-04-25 21:02:08 +00005893 if(result.exception == null) {
5894 if (result.result != null) {
5895 byte[] responseData = (byte[])(result.result);
5896 if(responseData.length > oemResp.length) {
5897 Log.w(LOG_TAG, "Buffer to copy response too small: Response length is " +
5898 responseData.length + "bytes. Buffer Size is " +
5899 oemResp.length + "bytes.");
5900 }
5901 System.arraycopy(responseData, 0, oemResp, 0, responseData.length);
5902 returnValue = responseData.length;
5903 }
5904 } else {
5905 CommandException ex = (CommandException) result.exception;
5906 returnValue = ex.getCommandError().ordinal();
5907 if(returnValue > 0) returnValue *= -1;
5908 }
5909 } catch (RuntimeException e) {
5910 Log.w(LOG_TAG, "sendOemRilRequestRaw: Runtime Exception");
5911 returnValue = (CommandException.Error.GENERIC_FAILURE.ordinal());
5912 if(returnValue > 0) returnValue *= -1;
5913 }
5914
5915 return returnValue;
5916 }
5917
5918 @Override
Wink Saville5d475dd2014-10-17 15:00:58 -07005919 public void setRadioCapability(RadioAccessFamily[] rafs) {
5920 try {
5921 ProxyController.getInstance().setRadioCapability(rafs);
5922 } catch (RuntimeException e) {
5923 Log.w(LOG_TAG, "setRadioCapability: Runtime Exception");
5924 }
5925 }
5926
5927 @Override
Robert Greenwalt36b23af2015-07-06 17:59:14 -07005928 public int getRadioAccessFamily(int phoneId, String callingPackage) {
Jeff Davidson913390f2018-02-23 17:11:49 -08005929 Phone phone = PhoneFactory.getPhone(phoneId);
chen xub97461a2018-10-26 14:17:57 -07005930 int raf = RadioAccessFamily.RAF_UNKNOWN;
Jeff Davidson913390f2018-02-23 17:11:49 -08005931 if (phone == null) {
chen xub97461a2018-10-26 14:17:57 -07005932 return raf;
Jeff Davidson913390f2018-02-23 17:11:49 -08005933 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005934 final long identity = Binder.clearCallingIdentity();
5935 try {
chen xub97461a2018-10-26 14:17:57 -07005936 TelephonyPermissions
5937 .enforeceCallingOrSelfReadPrivilegedPhoneStatePermissionOrCarrierPrivilege(
5938 mApp, phone.getSubId(), "getRadioAccessFamily");
5939 raf = ProxyController.getInstance().getRadioAccessFamily(phoneId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005940 } finally {
5941 Binder.restoreCallingIdentity(identity);
5942 }
chen xub97461a2018-10-26 14:17:57 -07005943 return raf;
Wink Saville5d475dd2014-10-17 15:00:58 -07005944 }
Andrew Leedf14ead2014-10-17 14:22:52 -07005945
5946 @Override
5947 public void enableVideoCalling(boolean enable) {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08005948 final Phone defaultPhone = getDefaultPhone();
Andrew Leedf14ead2014-10-17 14:22:52 -07005949 enforceModifyPermission();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005950
5951 final long identity = Binder.clearCallingIdentity();
5952 try {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08005953 ImsManager.getInstance(defaultPhone.getContext(),
5954 defaultPhone.getPhoneId()).setVtSetting(enable);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005955 } finally {
5956 Binder.restoreCallingIdentity(identity);
5957 }
Andrew Leedf14ead2014-10-17 14:22:52 -07005958 }
5959
5960 @Override
Philip P. Moltmann9797cbb2020-01-07 13:45:00 -08005961 public boolean isVideoCallingEnabled(String callingPackage, String callingFeatureId) {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08005962 final Phone defaultPhone = getDefaultPhone();
Philip P. Moltmann9797cbb2020-01-07 13:45:00 -08005963 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(mApp, defaultPhone.getSubId(),
5964 callingPackage, callingFeatureId, "isVideoCallingEnabled")) {
Amit Mahajan578e53d2018-03-20 16:18:38 +00005965 return false;
5966 }
Svet Ganovb320e182015-04-16 12:30:10 -07005967
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005968 final long identity = Binder.clearCallingIdentity();
5969 try {
5970 // Check the user preference and the system-level IMS setting. Even if the user has
5971 // enabled video calling, if IMS is disabled we aren't able to support video calling.
5972 // In the long run, we may instead need to check if there exists a connection service
5973 // which can support video calling.
5974 ImsManager imsManager =
Nazanin Bakhshif782e562018-12-11 15:15:39 -08005975 ImsManager.getInstance(defaultPhone.getContext(), defaultPhone.getPhoneId());
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005976 return imsManager.isVtEnabledByPlatform()
5977 && imsManager.isEnhanced4gLteModeSettingEnabledByUser()
5978 && imsManager.isVtEnabledByUser();
5979 } finally {
5980 Binder.restoreCallingIdentity(identity);
5981 }
Andrew Leedf14ead2014-10-17 14:22:52 -07005982 }
Libin.Tang@motorola.comafe82642014-12-18 13:27:53 -06005983
Andrew Leea1239f22015-03-02 17:44:07 -08005984 @Override
Philip P. Moltmann9797cbb2020-01-07 13:45:00 -08005985 public boolean canChangeDtmfToneLength(int subId, String callingPackage,
5986 String callingFeatureId) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005987 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
Philip P. Moltmann9797cbb2020-01-07 13:45:00 -08005988 mApp, subId, callingPackage, callingFeatureId,
5989 "isVideoCallingEnabled")) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005990 return false;
5991 }
5992
5993 final long identity = Binder.clearCallingIdentity();
5994 try {
5995 CarrierConfigManager configManager =
5996 (CarrierConfigManager) mApp.getSystemService(Context.CARRIER_CONFIG_SERVICE);
Nazanin Bakhshif782e562018-12-11 15:15:39 -08005997 return configManager.getConfigForSubId(subId)
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005998 .getBoolean(CarrierConfigManager.KEY_DTMF_TYPE_ENABLED_BOOL);
5999 } finally {
6000 Binder.restoreCallingIdentity(identity);
6001 }
Andrew Leea1239f22015-03-02 17:44:07 -08006002 }
6003
6004 @Override
Philip P. Moltmann9797cbb2020-01-07 13:45:00 -08006005 public boolean isWorldPhone(int subId, String callingPackage, String callingFeatureId) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006006 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
Philip P. Moltmann9797cbb2020-01-07 13:45:00 -08006007 mApp, subId, callingPackage, callingFeatureId, "isVideoCallingEnabled")) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006008 return false;
6009 }
6010
6011 final long identity = Binder.clearCallingIdentity();
6012 try {
6013 CarrierConfigManager configManager =
6014 (CarrierConfigManager) mApp.getSystemService(Context.CARRIER_CONFIG_SERVICE);
Nazanin Bakhshif782e562018-12-11 15:15:39 -08006015 return configManager.getConfigForSubId(subId)
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006016 .getBoolean(CarrierConfigManager.KEY_WORLD_PHONE_BOOL);
6017 } finally {
6018 Binder.restoreCallingIdentity(identity);
6019 }
Andrew Leea1239f22015-03-02 17:44:07 -08006020 }
6021
Andrew Lee9431b832015-03-09 18:46:45 -07006022 @Override
6023 public boolean isTtyModeSupported() {
Tyler Gunn77ee9382019-10-31 13:08:23 -07006024 TelecomManager telecomManager = mApp.getSystemService(TelecomManager.class);
Wooki Wu1f82f7a2016-02-15 15:59:58 +08006025 return telecomManager.isTtySupported();
Andrew Lee9431b832015-03-09 18:46:45 -07006026 }
6027
6028 @Override
6029 public boolean isHearingAidCompatibilitySupported() {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006030 final long identity = Binder.clearCallingIdentity();
6031 try {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08006032 return mApp.getResources().getBoolean(R.bool.hac_enabled);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006033 } finally {
6034 Binder.restoreCallingIdentity(identity);
6035 }
Andrew Lee9431b832015-03-09 18:46:45 -07006036 }
6037
Hall Liuf6668912018-10-31 17:05:23 -07006038 /**
6039 * Determines whether the device currently supports RTT (Real-time text). Based both on carrier
6040 * support for the feature and device firmware support.
6041 *
6042 * @return {@code true} if the device and carrier both support RTT, {@code false} otherwise.
6043 */
6044 @Override
6045 public boolean isRttSupported(int subscriptionId) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006046 final long identity = Binder.clearCallingIdentity();
Nazanin Bakhshif782e562018-12-11 15:15:39 -08006047 final Phone phone = getPhone(subscriptionId);
6048 if (phone == null) {
6049 loge("isRttSupported: no Phone found. Invalid subId:" + subscriptionId);
6050 return false;
6051 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006052 try {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08006053 boolean isCarrierSupported = mApp.getCarrierConfigForSubId(subscriptionId).getBoolean(
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006054 CarrierConfigManager.KEY_RTT_SUPPORTED_BOOL);
6055 boolean isDeviceSupported =
Nazanin Bakhshif782e562018-12-11 15:15:39 -08006056 phone.getContext().getResources().getBoolean(R.bool.config_support_rtt);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006057 return isCarrierSupported && isDeviceSupported;
6058 } finally {
6059 Binder.restoreCallingIdentity(identity);
6060 }
Hall Liu98187582018-01-22 19:15:32 -08006061 }
6062
Hall Liuf6668912018-10-31 17:05:23 -07006063 /**
Hall Liu6a06be62019-07-23 18:39:00 -07006064 * Determines whether the user has turned on RTT. If the carrier wants to ignore the user-set
6065 * RTT setting, will return true if the device and carrier both support RTT.
6066 * Otherwise. only returns true if the device and carrier both also support RTT.
Hall Liuf6668912018-10-31 17:05:23 -07006067 */
6068 public boolean isRttEnabled(int subscriptionId) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006069 final long identity = Binder.clearCallingIdentity();
6070 try {
Hall Liu63767ec2019-12-11 23:58:20 +00006071 boolean isRttSupported = isRttSupported(subscriptionId);
6072 boolean isUserRttSettingOn = Settings.Secure.getInt(
6073 mApp.getContentResolver(), Settings.Secure.RTT_CALLING_MODE, 0) != 0;
6074 boolean shouldIgnoreUserRttSetting = mApp.getCarrierConfigForSubId(subscriptionId)
6075 .getBoolean(CarrierConfigManager.KEY_IGNORE_RTT_MODE_SETTING_BOOL);
6076 return isRttSupported && (isUserRttSettingOn || shouldIgnoreUserRttSetting);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006077 } finally {
6078 Binder.restoreCallingIdentity(identity);
6079 }
Hall Liu3ad5f012018-04-06 16:23:39 -07006080 }
6081
Philip P. Moltmann9797cbb2020-01-07 13:45:00 -08006082 @Deprecated
6083 @Override
6084 public String getDeviceId(String callingPackage) {
6085 return getDeviceIdWithFeature(callingPackage, null);
6086 }
6087
Sanket Padawe7310cc72015-01-14 09:53:20 -08006088 /**
6089 * Returns the unique device ID of phone, for example, the IMEI for
6090 * GSM and the MEID for CDMA phones. Return null if device ID is not available.
6091 *
6092 * <p>Requires Permission:
6093 * {@link android.Manifest.permission#READ_PHONE_STATE READ_PHONE_STATE}
6094 */
6095 @Override
Philip P. Moltmann9797cbb2020-01-07 13:45:00 -08006096 public String getDeviceIdWithFeature(String callingPackage, String callingFeatureId) {
Sanket Padawe7310cc72015-01-14 09:53:20 -08006097 final Phone phone = PhoneFactory.getPhone(0);
Jeff Davidson913390f2018-02-23 17:11:49 -08006098 if (phone == null) {
Sanket Padawe7310cc72015-01-14 09:53:20 -08006099 return null;
6100 }
Jeff Davidson913390f2018-02-23 17:11:49 -08006101 int subId = phone.getSubId();
Michael Groover70af6dc2018-10-01 16:23:15 -07006102 if (!TelephonyPermissions.checkCallingOrSelfReadDeviceIdentifiers(mApp, subId,
Philip P. Moltmann9797cbb2020-01-07 13:45:00 -08006103 callingPackage, callingFeatureId, "getDeviceId")) {
Jeff Davidson913390f2018-02-23 17:11:49 -08006104 return null;
6105 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006106
6107 final long identity = Binder.clearCallingIdentity();
6108 try {
6109 return phone.getDeviceId();
6110 } finally {
6111 Binder.restoreCallingIdentity(identity);
6112 }
Sanket Padawe7310cc72015-01-14 09:53:20 -08006113 }
6114
Ping Sunc67b7c22016-03-02 19:16:45 +08006115 /**
6116 * {@hide}
6117 * Returns the IMS Registration Status on a particular subid
6118 *
6119 * @param subId
6120 */
Brad Ebinger1f2b5082018-02-08 16:11:32 -08006121 public boolean isImsRegistered(int subId) {
Ping Sunc67b7c22016-03-02 19:16:45 +08006122 Phone phone = getPhone(subId);
6123 if (phone != null) {
6124 return phone.isImsRegistered();
6125 } else {
6126 return false;
6127 }
6128 }
6129
Santos Cordon7a1885b2015-02-03 11:15:19 -08006130 @Override
6131 public int getSubIdForPhoneAccount(PhoneAccount phoneAccount) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006132 final long identity = Binder.clearCallingIdentity();
6133 try {
6134 return PhoneUtils.getSubIdForPhoneAccount(phoneAccount);
6135 } finally {
6136 Binder.restoreCallingIdentity(identity);
6137 }
Santos Cordon7a1885b2015-02-03 11:15:19 -08006138 }
Nathan Harolddcfc7932015-03-18 10:01:20 -07006139
Tyler Gunnf70ed162019-04-03 15:28:53 -07006140 @Override
Shuo Qian0762a782019-10-30 16:33:31 -07006141 public int getSubIdForPhoneAccountHandle(
Philip P. Moltmann9797cbb2020-01-07 13:45:00 -08006142 PhoneAccountHandle phoneAccountHandle, String callingPackage, String callingFeatureId) {
Shuo Qian0762a782019-10-30 16:33:31 -07006143 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(mApp, getDefaultSubscription(),
Philip P. Moltmann9797cbb2020-01-07 13:45:00 -08006144 callingPackage, callingFeatureId, "getSubIdForPhoneAccountHandle")) {
Shuo Qian0762a782019-10-30 16:33:31 -07006145 throw new SecurityException("Requires READ_PHONE_STATE permission.");
6146 }
6147 final long identity = Binder.clearCallingIdentity();
6148 try {
6149 return PhoneUtils.getSubIdForPhoneAccountHandle(phoneAccountHandle);
6150 } finally {
6151 Binder.restoreCallingIdentity(identity);
6152 }
6153 }
6154
6155 @Override
Tyler Gunnf70ed162019-04-03 15:28:53 -07006156 public @Nullable PhoneAccountHandle getPhoneAccountHandleForSubscriptionId(int subscriptionId) {
6157 final long identity = Binder.clearCallingIdentity();
6158 try {
6159 Phone phone = getPhone(subscriptionId);
6160 if (phone == null) {
6161 return null;
6162 }
6163 return PhoneUtils.makePstnPhoneAccountHandle(phone);
6164 } finally {
6165 Binder.restoreCallingIdentity(identity);
6166 }
6167 }
6168
Brad Ebinger1f2b5082018-02-08 16:11:32 -08006169 /**
6170 * @return the VoWiFi calling availability.
Nathan Haroldc55097a2015-03-11 18:14:50 -07006171 */
Brad Ebinger1f2b5082018-02-08 16:11:32 -08006172 public boolean isWifiCallingAvailable(int subId) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006173 final long identity = Binder.clearCallingIdentity();
6174 try {
6175 Phone phone = getPhone(subId);
6176 if (phone != null) {
6177 return phone.isWifiCallingEnabled();
6178 } else {
6179 return false;
6180 }
6181 } finally {
6182 Binder.restoreCallingIdentity(identity);
Brad Ebinger1f2b5082018-02-08 16:11:32 -08006183 }
Nathan Haroldc55097a2015-03-11 18:14:50 -07006184 }
6185
Brad Ebinger1f2b5082018-02-08 16:11:32 -08006186 /**
Brad Ebinger1f2b5082018-02-08 16:11:32 -08006187 * @return the VT calling availability.
Etan Cohen3b7a1bc2015-05-28 15:57:13 -07006188 */
Brad Ebinger1f2b5082018-02-08 16:11:32 -08006189 public boolean isVideoTelephonyAvailable(int subId) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006190 final long identity = Binder.clearCallingIdentity();
6191 try {
6192 Phone phone = getPhone(subId);
6193 if (phone != null) {
6194 return phone.isVideoEnabled();
6195 } else {
6196 return false;
6197 }
6198 } finally {
6199 Binder.restoreCallingIdentity(identity);
Brad Ebinger1f2b5082018-02-08 16:11:32 -08006200 }
6201 }
6202
6203 /**
6204 * @return the IMS registration technology for the MMTEL feature. Valid return values are
6205 * defined in {@link ImsRegistrationImplBase}.
6206 */
6207 public @ImsRegistrationImplBase.ImsRegistrationTech int getImsRegTechnologyForMmTel(int subId) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006208 final long identity = Binder.clearCallingIdentity();
6209 try {
6210 Phone phone = getPhone(subId);
6211 if (phone != null) {
6212 return phone.getImsRegistrationTech();
6213 } else {
6214 return ImsRegistrationImplBase.REGISTRATION_TECH_NONE;
6215 }
6216 } finally {
6217 Binder.restoreCallingIdentity(identity);
Brad Ebinger1f2b5082018-02-08 16:11:32 -08006218 }
Etan Cohen3b7a1bc2015-05-28 15:57:13 -07006219 }
6220
Stuart Scott8eef64f2015-04-08 15:13:54 -07006221 @Override
6222 public void factoryReset(int subId) {
paulhu423b5f22019-08-23 19:17:33 +08006223 enforceSettingsPermission();
Stuart Scott981d8582015-04-21 14:09:50 -07006224 if (mUserManager.hasUserRestriction(UserManager.DISALLOW_NETWORK_RESET)) {
6225 return;
6226 }
6227
Svet Ganovcc087f82015-05-12 20:35:54 -07006228 final long identity = Binder.clearCallingIdentity();
Nazanin Bakhshif782e562018-12-11 15:15:39 -08006229
Svet Ganovcc087f82015-05-12 20:35:54 -07006230 try {
Stuart Scott981d8582015-04-21 14:09:50 -07006231 if (SubscriptionManager.isUsableSubIdValue(subId) && !mUserManager.hasUserRestriction(
6232 UserManager.DISALLOW_CONFIG_MOBILE_NETWORKS)) {
Pengquan Meng85728fb2018-03-12 16:31:21 -07006233 setUserDataEnabled(subId, getDefaultDataEnabled());
Svet Ganovcc087f82015-05-12 20:35:54 -07006234 setNetworkSelectionModeAutomatic(subId);
Pengquan Meng85728fb2018-03-12 16:31:21 -07006235 setPreferredNetworkType(subId, getDefaultNetworkType(subId));
Nazanin Bakhshif782e562018-12-11 15:15:39 -08006236 setDataRoamingEnabled(subId, getDefaultDataRoamingEnabled(subId));
6237 CarrierInfoManager.deleteAllCarrierKeysForImsiEncryption(mApp);
Svet Ganovcc087f82015-05-12 20:35:54 -07006238 }
Amit Mahajan7dbbd822019-03-13 17:33:47 -07006239 // There has been issues when Sms raw table somehow stores orphan
6240 // fragments. They lead to garbled message when new fragments come
6241 // in and combined with those stale ones. In case this happens again,
6242 // user can reset all network settings which will clean up this table.
6243 cleanUpSmsRawTable(getDefaultPhone().getContext());
Brad Ebinger77b832e2019-10-17 17:03:22 -07006244 // Clean up IMS settings as well here.
6245 int slotId = getSlotIndex(subId);
6246 if (slotId > SubscriptionManager.INVALID_SIM_SLOT_INDEX) {
6247 ImsManager.getInstance(mApp, slotId).factoryReset();
6248 }
Naina Nalluri8ff344d2019-09-17 14:10:30 -07006249
6250 // Erase modem config if erase modem on network setting is enabled.
6251 String configValue = DeviceConfig.getProperty(DeviceConfig.NAMESPACE_TELEPHONY,
6252 RESET_NETWORK_ERASE_MODEM_CONFIG_ENABLED);
6253 if (configValue != null && Boolean.parseBoolean(configValue)) {
6254 sendEraseModemConfig(getDefaultPhone());
6255 }
Svet Ganovcc087f82015-05-12 20:35:54 -07006256 } finally {
6257 Binder.restoreCallingIdentity(identity);
Stuart Scott8eef64f2015-04-08 15:13:54 -07006258 }
6259 }
Narayan Kamath1c496c22015-04-16 14:40:19 +01006260
Amit Mahajan7dbbd822019-03-13 17:33:47 -07006261 private void cleanUpSmsRawTable(Context context) {
6262 ContentResolver resolver = context.getContentResolver();
6263 Uri uri = Uri.withAppendedPath(Telephony.Sms.CONTENT_URI, "raw/permanentDelete");
6264 resolver.delete(uri, null, null);
6265 }
6266
Narayan Kamath1c496c22015-04-16 14:40:19 +01006267 @Override
chen xu5d3637b2019-01-21 23:31:38 -08006268 public String getSimLocaleForSubscriber(int subId) {
6269 enforceReadPrivilegedPermission("getSimLocaleForSubscriber, subId: " + subId);
6270 final Phone phone = getPhone(subId);
6271 if (phone == null) {
6272 log("getSimLocaleForSubscriber, invalid subId");
chen xu2bb91e42019-01-24 14:35:54 -08006273 return null;
chen xu5d3637b2019-01-21 23:31:38 -08006274 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006275 final long identity = Binder.clearCallingIdentity();
6276 try {
chen xu5d3637b2019-01-21 23:31:38 -08006277 final SubscriptionInfo info = mSubscriptionController.getActiveSubscriptionInfo(subId,
Philip P. Moltmann9797cbb2020-01-07 13:45:00 -08006278 phone.getContext().getOpPackageName(), null);
chen xu6291c472019-02-04 12:55:53 -08006279 if (info == null) {
6280 log("getSimLocaleForSubscriber, inactive subId: " + subId);
6281 return null;
6282 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006283 // Try and fetch the locale from the carrier properties or from the SIM language
6284 // preferences (EF-PL and EF-LI)...
6285 final int mcc = info.getMcc();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006286 String simLanguage = null;
chen xu5d3637b2019-01-21 23:31:38 -08006287 final Locale localeFromDefaultSim = phone.getLocaleFromSimAndCarrierPrefs();
6288 if (localeFromDefaultSim != null) {
6289 if (!localeFromDefaultSim.getCountry().isEmpty()) {
6290 if (DBG) log("Using locale from subId: " + subId + " locale: "
6291 + localeFromDefaultSim);
6292 return localeFromDefaultSim.toLanguageTag();
6293 } else {
6294 simLanguage = localeFromDefaultSim.getLanguage();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006295 }
6296 }
Narayan Kamath1c496c22015-04-16 14:40:19 +01006297
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006298 // The SIM language preferences only store a language (e.g. fr = French), not an
6299 // exact locale (e.g. fr_FR = French/France). So, if the locale returned from
6300 // the SIM and carrier preferences does not include a country we add the country
6301 // determined from the SIM MCC to provide an exact locale.
zoey chen84e2b212019-12-18 17:07:20 +08006302 final Locale mccLocale = LocaleUtils.getLocaleFromMcc(mApp, mcc, simLanguage);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006303 if (mccLocale != null) {
chen xu5d3637b2019-01-21 23:31:38 -08006304 if (DBG) log("No locale from SIM, using mcc locale:" + mccLocale);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006305 return mccLocale.toLanguageTag();
6306 }
6307
6308 if (DBG) log("No locale found - returning null");
6309 return null;
6310 } finally {
6311 Binder.restoreCallingIdentity(identity);
6312 }
Narayan Kamath1c496c22015-04-16 14:40:19 +01006313 }
6314
6315 private List<SubscriptionInfo> getAllSubscriptionInfoList() {
Philip P. Moltmann9797cbb2020-01-07 13:45:00 -08006316 return mSubscriptionController.getAllSubInfoList(mApp.getOpPackageName(),
6317 null);
Narayan Kamath1c496c22015-04-16 14:40:19 +01006318 }
6319
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006320 /**
6321 * NOTE: this method assumes permission checks are done and caller identity has been cleared.
6322 */
6323 private List<SubscriptionInfo> getActiveSubscriptionInfoListPrivileged() {
Philip P. Moltmann9797cbb2020-01-07 13:45:00 -08006324 return mSubscriptionController.getActiveSubscriptionInfoList(mApp.getOpPackageName(),
6325 null);
Narayan Kamath1c496c22015-04-16 14:40:19 +01006326 }
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -07006327
Chenjie Yu1ba97252018-01-11 18:16:20 -08006328 private final ModemActivityInfo mLastModemActivityInfo =
Chen Xu13851032019-09-10 18:49:52 -07006329 new ModemActivityInfo(0, 0, 0, new int[0], 0);
Chenjie Yu1ba97252018-01-11 18:16:20 -08006330
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -07006331 /**
Adam Lesinski903a54c2016-04-11 14:49:52 -07006332 * Responds to the ResultReceiver with the {@link android.telephony.ModemActivityInfo} object
6333 * representing the state of the modem.
6334 *
Chenjie Yu1ba97252018-01-11 18:16:20 -08006335 * NOTE: The underlying implementation clears the modem state, so there should only ever be one
6336 * caller to it. Everyone should call this class to get cumulative data.
Adam Lesinski903a54c2016-04-11 14:49:52 -07006337 * @hide
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -07006338 */
6339 @Override
Adam Lesinski903a54c2016-04-11 14:49:52 -07006340 public void requestModemActivityInfo(ResultReceiver result) {
6341 enforceModifyPermission();
vagdeviaf9a5b92018-08-15 16:01:53 -07006342 WorkSource workSource = getWorkSource(Binder.getCallingUid());
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006343
6344 final long identity = Binder.clearCallingIdentity();
6345 try {
6346 ModemActivityInfo ret = null;
6347 synchronized (mLastModemActivityInfo) {
6348 ModemActivityInfo info = (ModemActivityInfo) sendRequest(
6349 CMD_GET_MODEM_ACTIVITY_INFO,
vagdeviaf9a5b92018-08-15 16:01:53 -07006350 null, workSource);
Siddharth Rayb8114062018-06-17 15:02:38 -07006351 if (isModemActivityInfoValid(info)) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006352 int[] mergedTxTimeMs = new int[ModemActivityInfo.TX_POWER_LEVELS];
Chen Xu13851032019-09-10 18:49:52 -07006353 int[] txTimeMs = info.getTransmitTimeMillis();
6354 int[] lastModemTxTimeMs = mLastModemActivityInfo.getTransmitTimeMillis();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006355 for (int i = 0; i < mergedTxTimeMs.length; i++) {
Chen Xu13851032019-09-10 18:49:52 -07006356 mergedTxTimeMs[i] = txTimeMs[i] + lastModemTxTimeMs[i];
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006357 }
6358 mLastModemActivityInfo.setTimestamp(info.getTimestamp());
Nazanin Bakhshif782e562018-12-11 15:15:39 -08006359 mLastModemActivityInfo.setSleepTimeMillis(info.getSleepTimeMillis()
6360 + mLastModemActivityInfo.getSleepTimeMillis());
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006361 mLastModemActivityInfo.setIdleTimeMillis(
6362 info.getIdleTimeMillis() + mLastModemActivityInfo.getIdleTimeMillis());
Chen Xu13851032019-09-10 18:49:52 -07006363 mLastModemActivityInfo.setTransmitTimeMillis(mergedTxTimeMs);
6364 mLastModemActivityInfo.setReceiveTimeMillis(
6365 info.getReceiveTimeMillis() + mLastModemActivityInfo
6366 .getReceiveTimeMillis());
Chenjie Yu1ba97252018-01-11 18:16:20 -08006367 }
Chen Xu13851032019-09-10 18:49:52 -07006368
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006369 ret = new ModemActivityInfo(mLastModemActivityInfo.getTimestamp(),
6370 mLastModemActivityInfo.getSleepTimeMillis(),
6371 mLastModemActivityInfo.getIdleTimeMillis(),
Chen Xu13851032019-09-10 18:49:52 -07006372 mLastModemActivityInfo.getTransmitTimeMillis(),
6373 mLastModemActivityInfo.getReceiveTimeMillis());
Chenjie Yu1ba97252018-01-11 18:16:20 -08006374 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006375 Bundle bundle = new Bundle();
6376 bundle.putParcelable(TelephonyManager.MODEM_ACTIVITY_RESULT_KEY, ret);
6377 result.send(0, bundle);
6378 } finally {
6379 Binder.restoreCallingIdentity(identity);
Chenjie Yu1ba97252018-01-11 18:16:20 -08006380 }
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -07006381 }
Jack Yu85bd38a2015-11-09 11:34:32 -08006382
Siddharth Rayb8114062018-06-17 15:02:38 -07006383 // Checks that ModemActivityInfo is valid. Sleep time, Idle time, Rx time and Tx time should be
6384 // less than total activity duration.
6385 private boolean isModemActivityInfoValid(ModemActivityInfo info) {
6386 if (info == null) {
6387 return false;
6388 }
6389 int activityDurationMs =
6390 (int) (info.getTimestamp() - mLastModemActivityInfo.getTimestamp());
6391 int totalTxTimeMs = 0;
Chen Xu13851032019-09-10 18:49:52 -07006392 int[] txTimeMs = info.getTransmitTimeMillis();
6393 for (int i = 0; i < info.getTransmitPowerInfo().size(); i++) {
6394 totalTxTimeMs += txTimeMs[i];
Siddharth Rayb8114062018-06-17 15:02:38 -07006395 }
6396 return (info.isValid()
6397 && (info.getSleepTimeMillis() <= activityDurationMs)
6398 && (info.getIdleTimeMillis() <= activityDurationMs)
Chen Xu13851032019-09-10 18:49:52 -07006399 && (info.getReceiveTimeMillis() <= activityDurationMs)
Siddharth Rayb8114062018-06-17 15:02:38 -07006400 && (totalTxTimeMs <= activityDurationMs));
6401 }
6402
Jack Yu85bd38a2015-11-09 11:34:32 -08006403 /**
6404 * {@hide}
6405 * Returns the service state information on specified subscription.
6406 */
6407 @Override
Philip P. Moltmann9797cbb2020-01-07 13:45:00 -08006408 public ServiceState getServiceStateForSubscriber(int subId, String callingPackage,
6409 String callingFeatureId) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08006410 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
Philip P. Moltmann9797cbb2020-01-07 13:45:00 -08006411 mApp, subId, callingPackage, callingFeatureId, "getServiceStateForSubscriber")) {
Jack Yu85bd38a2015-11-09 11:34:32 -08006412 return null;
6413 }
6414
Hall Liuf19c44f2018-11-27 14:38:17 -08006415 LocationAccessPolicy.LocationPermissionResult fineLocationResult =
6416 LocationAccessPolicy.checkLocationPermission(mApp,
6417 new LocationAccessPolicy.LocationPermissionQuery.Builder()
6418 .setCallingPackage(callingPackage)
Philip P. Moltmann9797cbb2020-01-07 13:45:00 -08006419 .setCallingFeatureId(callingFeatureId)
Hall Liuf19c44f2018-11-27 14:38:17 -08006420 .setCallingPid(Binder.getCallingPid())
6421 .setCallingUid(Binder.getCallingUid())
6422 .setMethod("getServiceStateForSubscriber")
Hall Liuf18a0cf2019-04-17 13:37:11 -07006423 .setLogAsInfo(true)
Hall Liuf19c44f2018-11-27 14:38:17 -08006424 .setMinSdkVersionForFine(Build.VERSION_CODES.Q)
6425 .build());
6426
6427 LocationAccessPolicy.LocationPermissionResult coarseLocationResult =
6428 LocationAccessPolicy.checkLocationPermission(mApp,
6429 new LocationAccessPolicy.LocationPermissionQuery.Builder()
6430 .setCallingPackage(callingPackage)
Philip P. Moltmann9797cbb2020-01-07 13:45:00 -08006431 .setCallingFeatureId(callingFeatureId)
Hall Liuf19c44f2018-11-27 14:38:17 -08006432 .setCallingPid(Binder.getCallingPid())
6433 .setCallingUid(Binder.getCallingUid())
6434 .setMethod("getServiceStateForSubscriber")
Hall Liuf18a0cf2019-04-17 13:37:11 -07006435 .setLogAsInfo(true)
Hall Liuf19c44f2018-11-27 14:38:17 -08006436 .setMinSdkVersionForCoarse(Build.VERSION_CODES.Q)
6437 .build());
6438 // We don't care about hard or soft here -- all we need to know is how much info to scrub.
6439 boolean hasFinePermission =
6440 fineLocationResult == LocationAccessPolicy.LocationPermissionResult.ALLOWED;
6441 boolean hasCoarsePermission =
6442 coarseLocationResult == LocationAccessPolicy.LocationPermissionResult.ALLOWED;
6443
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006444 final long identity = Binder.clearCallingIdentity();
6445 try {
6446 final Phone phone = getPhone(subId);
6447 if (phone == null) {
6448 return null;
6449 }
Jack Yu85bd38a2015-11-09 11:34:32 -08006450
Hall Liuf19c44f2018-11-27 14:38:17 -08006451 ServiceState ss = phone.getServiceState();
6452
6453 // Scrub out the location info in ServiceState depending on what level of access
6454 // the caller has.
6455 if (hasFinePermission) return ss;
Malcolm Chendb337972019-12-30 13:56:38 -08006456 if (hasCoarsePermission) return ss.createLocationInfoSanitizedCopy(false);
6457 return ss.createLocationInfoSanitizedCopy(true);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006458 } finally {
6459 Binder.restoreCallingIdentity(identity);
6460 }
Jack Yu85bd38a2015-11-09 11:34:32 -08006461 }
Nancy Chen31f9ba12016-01-06 11:42:12 -08006462
6463 /**
6464 * Returns the URI for the per-account voicemail ringtone set in Phone settings.
6465 *
6466 * @param accountHandle The handle for the {@link PhoneAccount} for which to retrieve the
6467 * voicemail ringtone.
6468 * @return The URI for the ringtone to play when receiving a voicemail from a specific
6469 * PhoneAccount.
6470 */
6471 @Override
6472 public Uri getVoicemailRingtoneUri(PhoneAccountHandle accountHandle) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006473 final long identity = Binder.clearCallingIdentity();
6474 try {
6475 Phone phone = PhoneUtils.getPhoneForPhoneAccountHandle(accountHandle);
6476 if (phone == null) {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08006477 phone = getDefaultPhone();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006478 }
Nancy Chen31f9ba12016-01-06 11:42:12 -08006479
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006480 return VoicemailNotificationSettingsUtil.getRingtoneUri(phone.getContext());
6481 } finally {
6482 Binder.restoreCallingIdentity(identity);
6483 }
Nancy Chen31f9ba12016-01-06 11:42:12 -08006484 }
6485
6486 /**
Ta-wei Yenc33877d2017-01-23 18:11:21 -08006487 * Sets the per-account voicemail ringtone.
6488 *
6489 * <p>Requires that the calling app is the default dialer, or has carrier privileges, or
6490 * has permission {@link android.Manifest.permission#MODIFY_PHONE_STATE MODIFY_PHONE_STATE}.
6491 *
6492 * @param phoneAccountHandle The handle for the {@link PhoneAccount} for which to set the
6493 * voicemail ringtone.
6494 * @param uri The URI for the ringtone to play when receiving a voicemail from a specific
6495 * PhoneAccount.
6496 */
6497 @Override
6498 public void setVoicemailRingtoneUri(String callingPackage,
6499 PhoneAccountHandle phoneAccountHandle, Uri uri) {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08006500 final Phone defaultPhone = getDefaultPhone();
Ta-wei Yenc33877d2017-01-23 18:11:21 -08006501 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
Tyler Gunn77ee9382019-10-31 13:08:23 -07006502 TelecomManager tm = defaultPhone.getContext().getSystemService(TelecomManager.class);
6503 if (!TextUtils.equals(callingPackage, tm.getDefaultDialerPackage())) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08006504 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
6505 mApp, PhoneUtils.getSubIdForPhoneAccountHandle(phoneAccountHandle),
6506 "setVoicemailRingtoneUri");
Ta-wei Yenc33877d2017-01-23 18:11:21 -08006507 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006508
6509 final long identity = Binder.clearCallingIdentity();
6510 try {
6511 Phone phone = PhoneUtils.getPhoneForPhoneAccountHandle(phoneAccountHandle);
6512 if (phone == null) {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08006513 phone = defaultPhone;
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006514 }
6515 VoicemailNotificationSettingsUtil.setRingtoneUri(phone.getContext(), uri);
6516 } finally {
6517 Binder.restoreCallingIdentity(identity);
Ta-wei Yenc33877d2017-01-23 18:11:21 -08006518 }
Ta-wei Yenc33877d2017-01-23 18:11:21 -08006519 }
6520
6521 /**
Nancy Chen31f9ba12016-01-06 11:42:12 -08006522 * Returns whether vibration is set for voicemail notification in Phone settings.
6523 *
6524 * @param accountHandle The handle for the {@link PhoneAccount} for which to retrieve the
6525 * voicemail vibration setting.
6526 * @return {@code true} if the vibration is set for this PhoneAccount, {@code false} otherwise.
6527 */
6528 @Override
6529 public boolean isVoicemailVibrationEnabled(PhoneAccountHandle accountHandle) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006530 final long identity = Binder.clearCallingIdentity();
6531 try {
6532 Phone phone = PhoneUtils.getPhoneForPhoneAccountHandle(accountHandle);
6533 if (phone == null) {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08006534 phone = getDefaultPhone();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006535 }
Nancy Chen31f9ba12016-01-06 11:42:12 -08006536
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006537 return VoicemailNotificationSettingsUtil.isVibrationEnabled(phone.getContext());
6538 } finally {
6539 Binder.restoreCallingIdentity(identity);
6540 }
Nancy Chen31f9ba12016-01-06 11:42:12 -08006541 }
6542
Youhan Wange64578a2016-05-02 15:32:42 -07006543 /**
Ta-wei Yenc33877d2017-01-23 18:11:21 -08006544 * Sets the per-account voicemail vibration.
6545 *
6546 * <p>Requires that the calling app is the default dialer, or has carrier privileges, or
6547 * has permission {@link android.Manifest.permission#MODIFY_PHONE_STATE MODIFY_PHONE_STATE}.
6548 *
6549 * @param phoneAccountHandle The handle for the {@link PhoneAccount} for which to set the
6550 * voicemail vibration setting.
6551 * @param enabled Whether to enable or disable vibration for voicemail notifications from a
6552 * specific PhoneAccount.
6553 */
6554 @Override
6555 public void setVoicemailVibrationEnabled(String callingPackage,
6556 PhoneAccountHandle phoneAccountHandle, boolean enabled) {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08006557 final Phone defaultPhone = getDefaultPhone();
Ta-wei Yenc33877d2017-01-23 18:11:21 -08006558 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
Tyler Gunn77ee9382019-10-31 13:08:23 -07006559 TelecomManager tm = defaultPhone.getContext().getSystemService(TelecomManager.class);
6560 if (!TextUtils.equals(callingPackage, tm.getDefaultDialerPackage())) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08006561 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
6562 mApp, PhoneUtils.getSubIdForPhoneAccountHandle(phoneAccountHandle),
6563 "setVoicemailVibrationEnabled");
Ta-wei Yenc33877d2017-01-23 18:11:21 -08006564 }
6565
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006566 final long identity = Binder.clearCallingIdentity();
6567 try {
6568 Phone phone = PhoneUtils.getPhoneForPhoneAccountHandle(phoneAccountHandle);
6569 if (phone == null) {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08006570 phone = defaultPhone;
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006571 }
6572 VoicemailNotificationSettingsUtil.setVibrationEnabled(phone.getContext(), enabled);
6573 } finally {
6574 Binder.restoreCallingIdentity(identity);
Ta-wei Yenc33877d2017-01-23 18:11:21 -08006575 }
Ta-wei Yenc33877d2017-01-23 18:11:21 -08006576 }
6577
6578 /**
Youhan Wange64578a2016-05-02 15:32:42 -07006579 * Make sure either called from same process as self (phone) or IPC caller has read privilege.
6580 *
6581 * @throws SecurityException if the caller does not have the required permission
6582 */
Brad Ebinger35c841c2018-10-01 10:40:55 -07006583 private void enforceReadPrivilegedPermission(String message) {
Youhan Wange64578a2016-05-02 15:32:42 -07006584 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.READ_PRIVILEGED_PHONE_STATE,
Brad Ebinger35c841c2018-10-01 10:40:55 -07006585 message);
Youhan Wange64578a2016-05-02 15:32:42 -07006586 }
6587
6588 /**
Ta-wei Yen30a69c82016-12-27 14:52:32 -08006589 * Make sure either called from same process as self (phone) or IPC caller has send SMS
6590 * permission.
6591 *
6592 * @throws SecurityException if the caller does not have the required permission
6593 */
6594 private void enforceSendSmsPermission() {
6595 mApp.enforceCallingOrSelfPermission(permission.SEND_SMS, null);
6596 }
6597
6598 /**
Ta-wei Yen527a9c02017-01-06 15:29:25 -08006599 * Make sure called from the package in charge of visual voicemail.
Ta-wei Yen30a69c82016-12-27 14:52:32 -08006600 *
Ta-wei Yen527a9c02017-01-06 15:29:25 -08006601 * @throws SecurityException if the caller is not the visual voicemail package.
Ta-wei Yen30a69c82016-12-27 14:52:32 -08006602 */
Ta-wei Yen527a9c02017-01-06 15:29:25 -08006603 private void enforceVisualVoicemailPackage(String callingPackage, int subId) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006604 final long identity = Binder.clearCallingIdentity();
6605 try {
6606 ComponentName componentName =
Nazanin Bakhshif782e562018-12-11 15:15:39 -08006607 RemoteVvmTaskManager.getRemotePackage(mApp, subId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006608 if (componentName == null) {
6609 throw new SecurityException(
6610 "Caller not current active visual voicemail package[null]");
6611 }
6612 String vvmPackage = componentName.getPackageName();
6613 if (!callingPackage.equals(vvmPackage)) {
6614 throw new SecurityException("Caller not current active visual voicemail package["
6615 + vvmPackage + "]");
6616 }
6617 } finally {
6618 Binder.restoreCallingIdentity(identity);
Ta-wei Yen30a69c82016-12-27 14:52:32 -08006619 }
6620 }
6621
6622 /**
Youhan Wange64578a2016-05-02 15:32:42 -07006623 * Return the application ID for the app type.
6624 *
6625 * @param subId the subscription ID that this request applies to.
6626 * @param appType the uicc app type.
6627 * @return Application ID for specificied app type, or null if no uicc.
6628 */
6629 @Override
6630 public String getAidForAppType(int subId, int appType) {
Brad Ebinger35c841c2018-10-01 10:40:55 -07006631 enforceReadPrivilegedPermission("getAidForAppType");
Youhan Wange64578a2016-05-02 15:32:42 -07006632 Phone phone = getPhone(subId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006633
6634 final long identity = Binder.clearCallingIdentity();
Youhan Wange64578a2016-05-02 15:32:42 -07006635 try {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006636 if (phone == null) {
6637 return null;
6638 }
6639 String aid = null;
6640 try {
6641 aid = UiccController.getInstance().getUiccCard(phone.getPhoneId())
6642 .getApplicationByType(appType).getAid();
6643 } catch (Exception e) {
6644 Log.e(LOG_TAG, "Not getting aid. Exception ex=" + e);
6645 }
6646 return aid;
6647 } finally {
6648 Binder.restoreCallingIdentity(identity);
Youhan Wange64578a2016-05-02 15:32:42 -07006649 }
Youhan Wange64578a2016-05-02 15:32:42 -07006650 }
6651
Youhan Wang4001d252016-05-11 10:29:41 -07006652 /**
6653 * Return the Electronic Serial Number.
6654 *
6655 * @param subId the subscription ID that this request applies to.
6656 * @return ESN or null if error.
6657 */
6658 @Override
6659 public String getEsn(int subId) {
Brad Ebinger35c841c2018-10-01 10:40:55 -07006660 enforceReadPrivilegedPermission("getEsn");
Youhan Wang4001d252016-05-11 10:29:41 -07006661 Phone phone = getPhone(subId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006662
6663 final long identity = Binder.clearCallingIdentity();
Youhan Wang4001d252016-05-11 10:29:41 -07006664 try {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006665 if (phone == null) {
6666 return null;
6667 }
6668 String esn = null;
6669 try {
6670 esn = phone.getEsn();
6671 } catch (Exception e) {
6672 Log.e(LOG_TAG, "Not getting ESN. Exception ex=" + e);
6673 }
6674 return esn;
6675 } finally {
6676 Binder.restoreCallingIdentity(identity);
Youhan Wang4001d252016-05-11 10:29:41 -07006677 }
Youhan Wang4001d252016-05-11 10:29:41 -07006678 }
6679
Sanket Padawe99ef1e32016-05-18 16:12:33 -07006680 /**
Youhan Wang66ad5d72016-07-18 17:56:58 -07006681 * Return the Preferred Roaming List Version.
6682 *
6683 * @param subId the subscription ID that this request applies to.
6684 * @return PRLVersion or null if error.
6685 */
6686 @Override
6687 public String getCdmaPrlVersion(int subId) {
Brad Ebinger35c841c2018-10-01 10:40:55 -07006688 enforceReadPrivilegedPermission("getCdmaPrlVersion");
Youhan Wang66ad5d72016-07-18 17:56:58 -07006689 Phone phone = getPhone(subId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006690
6691 final long identity = Binder.clearCallingIdentity();
Youhan Wang66ad5d72016-07-18 17:56:58 -07006692 try {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006693 if (phone == null) {
6694 return null;
6695 }
6696 String cdmaPrlVersion = null;
6697 try {
6698 cdmaPrlVersion = phone.getCdmaPrlVersion();
6699 } catch (Exception e) {
6700 Log.e(LOG_TAG, "Not getting PRLVersion", e);
6701 }
6702 return cdmaPrlVersion;
6703 } finally {
6704 Binder.restoreCallingIdentity(identity);
Youhan Wang66ad5d72016-07-18 17:56:58 -07006705 }
Youhan Wang66ad5d72016-07-18 17:56:58 -07006706 }
6707
6708 /**
Sanket Padawe99ef1e32016-05-18 16:12:33 -07006709 * Get snapshot of Telephony histograms
6710 * @return List of Telephony histograms
6711 * @hide
6712 */
6713 @Override
6714 public List<TelephonyHistogram> getTelephonyHistograms() {
Jeff Davidson7e17e312018-02-13 18:17:36 -08006715 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
6716 mApp, getDefaultSubscription(), "getTelephonyHistograms");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006717
6718 final long identity = Binder.clearCallingIdentity();
6719 try {
6720 return RIL.getTelephonyRILTimingHistograms();
6721 } finally {
6722 Binder.restoreCallingIdentity(identity);
6723 }
Sanket Padawe99ef1e32016-05-18 16:12:33 -07006724 }
Meng Wang1a7c35a2016-05-05 20:56:15 -07006725
6726 /**
6727 * {@hide}
Michele Berionne482f8202018-11-27 18:57:59 -08006728 * Set the allowed carrier list and the excluded carrier list, indicating the priority between
6729 * the two lists.
Meng Wang1a7c35a2016-05-05 20:56:15 -07006730 * Require system privileges. In the future we may add this to carrier APIs.
6731 *
Michele Berionne482f8202018-11-27 18:57:59 -08006732 * @return Integer with the result of the operation, as defined in {@link TelephonyManager}.
Meng Wang1a7c35a2016-05-05 20:56:15 -07006733 */
6734 @Override
Michele Berionne482f8202018-11-27 18:57:59 -08006735 @TelephonyManager.SetCarrierRestrictionResult
6736 public int setAllowedCarriers(CarrierRestrictionRules carrierRestrictionRules) {
Meng Wang1a7c35a2016-05-05 20:56:15 -07006737 enforceModifyPermission();
vagdeviaf9a5b92018-08-15 16:01:53 -07006738 WorkSource workSource = getWorkSource(Binder.getCallingUid());
Sanket Padawe13bac7b2017-03-20 15:04:47 -07006739
Michele Berionne482f8202018-11-27 18:57:59 -08006740 if (carrierRestrictionRules == null) {
6741 throw new NullPointerException("carrier restriction cannot be null");
Meng Wang9b7c4e92017-02-17 11:41:27 -08006742 }
Sanket Padawe13bac7b2017-03-20 15:04:47 -07006743
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006744 final long identity = Binder.clearCallingIdentity();
6745 try {
Michele Berionne482f8202018-11-27 18:57:59 -08006746 return (int) sendRequest(CMD_SET_ALLOWED_CARRIERS, carrierRestrictionRules,
vagdeviaf9a5b92018-08-15 16:01:53 -07006747 workSource);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006748 } finally {
6749 Binder.restoreCallingIdentity(identity);
6750 }
Meng Wang1a7c35a2016-05-05 20:56:15 -07006751 }
6752
6753 /**
6754 * {@hide}
Michele Berionne482f8202018-11-27 18:57:59 -08006755 * Get the allowed carrier list and the excluded carrier list, including the priority between
6756 * the two lists.
Meng Wang1a7c35a2016-05-05 20:56:15 -07006757 * Require system privileges. In the future we may add this to carrier APIs.
6758 *
Michele Berionne482f8202018-11-27 18:57:59 -08006759 * @return {@link android.telephony.CarrierRestrictionRules}
Meng Wang1a7c35a2016-05-05 20:56:15 -07006760 */
6761 @Override
Michele Berionne482f8202018-11-27 18:57:59 -08006762 public CarrierRestrictionRules getAllowedCarriers() {
Brad Ebinger35c841c2018-10-01 10:40:55 -07006763 enforceReadPrivilegedPermission("getAllowedCarriers");
vagdeviaf9a5b92018-08-15 16:01:53 -07006764 WorkSource workSource = getWorkSource(Binder.getCallingUid());
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006765
6766 final long identity = Binder.clearCallingIdentity();
6767 try {
Michele Berionne482f8202018-11-27 18:57:59 -08006768 Object response = sendRequest(CMD_GET_ALLOWED_CARRIERS, null, workSource);
6769 if (response instanceof CarrierRestrictionRules) {
6770 return (CarrierRestrictionRules) response;
6771 }
6772 // Response is an Exception of some kind,
6773 // which is signalled to the user as a NULL retval
6774 return null;
6775 } catch (Exception e) {
6776 Log.e(LOG_TAG, "getAllowedCarriers. Exception ex=" + e);
6777 return null;
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006778 } finally {
6779 Binder.restoreCallingIdentity(identity);
6780 }
Meng Wang1a7c35a2016-05-05 20:56:15 -07006781 }
6782
fionaxu59545b42016-05-25 15:53:37 -07006783 /**
6784 * Action set from carrier signalling broadcast receivers to enable/disable metered apns
6785 * @param subId the subscription ID that this action applies to.
6786 * @param enabled control enable or disable metered apns.
6787 * {@hide}
6788 */
6789 @Override
6790 public void carrierActionSetMeteredApnsEnabled(int subId, boolean enabled) {
6791 enforceModifyPermission();
6792 final Phone phone = getPhone(subId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006793
6794 final long identity = Binder.clearCallingIdentity();
fionaxu59545b42016-05-25 15:53:37 -07006795 if (phone == null) {
6796 loge("carrierAction: SetMeteredApnsEnabled fails with invalid subId: " + subId);
6797 return;
6798 }
6799 try {
6800 phone.carrierActionSetMeteredApnsEnabled(enabled);
6801 } catch (Exception e) {
6802 Log.e(LOG_TAG, "carrierAction: SetMeteredApnsEnabled fails. Exception ex=" + e);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006803 } finally {
6804 Binder.restoreCallingIdentity(identity);
fionaxu59545b42016-05-25 15:53:37 -07006805 }
6806 }
6807
6808 /**
6809 * Action set from carrier signalling broadcast receivers to enable/disable radio
6810 * @param subId the subscription ID that this action applies to.
6811 * @param enabled control enable or disable radio.
6812 * {@hide}
6813 */
6814 @Override
6815 public void carrierActionSetRadioEnabled(int subId, boolean enabled) {
6816 enforceModifyPermission();
6817 final Phone phone = getPhone(subId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006818
6819 final long identity = Binder.clearCallingIdentity();
fionaxu59545b42016-05-25 15:53:37 -07006820 if (phone == null) {
6821 loge("carrierAction: SetRadioEnabled fails with invalid sibId: " + subId);
6822 return;
6823 }
6824 try {
6825 phone.carrierActionSetRadioEnabled(enabled);
6826 } catch (Exception e) {
6827 Log.e(LOG_TAG, "carrierAction: SetRadioEnabled fails. Exception ex=" + e);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006828 } finally {
6829 Binder.restoreCallingIdentity(identity);
fionaxu59545b42016-05-25 15:53:37 -07006830 }
6831 }
6832
Ta-wei Yenc236d6b2016-06-21 13:33:12 -07006833 /**
fionaxu8da9cb12017-05-23 15:02:46 -07006834 * Action set from carrier signalling broadcast receivers to start/stop reporting the default
6835 * network status based on which carrier apps could apply actions accordingly,
6836 * enable/disable default url handler for example.
6837 *
6838 * @param subId the subscription ID that this action applies to.
6839 * @param report control start/stop reporting the default network status.
6840 * {@hide}
6841 */
6842 @Override
6843 public void carrierActionReportDefaultNetworkStatus(int subId, boolean report) {
6844 enforceModifyPermission();
6845 final Phone phone = getPhone(subId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006846
6847 final long identity = Binder.clearCallingIdentity();
fionaxu8da9cb12017-05-23 15:02:46 -07006848 if (phone == null) {
6849 loge("carrierAction: ReportDefaultNetworkStatus fails with invalid sibId: " + subId);
6850 return;
6851 }
6852 try {
6853 phone.carrierActionReportDefaultNetworkStatus(report);
6854 } catch (Exception e) {
6855 Log.e(LOG_TAG, "carrierAction: ReportDefaultNetworkStatus fails. Exception ex=" + e);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006856 } finally {
6857 Binder.restoreCallingIdentity(identity);
fionaxu8da9cb12017-05-23 15:02:46 -07006858 }
6859 }
6860
6861 /**
fionaxud9622282017-07-17 17:51:30 -07006862 * Action set from carrier signalling broadcast receivers to reset all carrier actions
6863 * @param subId the subscription ID that this action applies to.
6864 * {@hide}
6865 */
6866 @Override
6867 public void carrierActionResetAll(int subId) {
6868 enforceModifyPermission();
6869 final Phone phone = getPhone(subId);
6870 if (phone == null) {
6871 loge("carrierAction: ResetAll fails with invalid sibId: " + subId);
6872 return;
6873 }
6874 try {
6875 phone.carrierActionResetAll();
6876 } catch (Exception e) {
6877 Log.e(LOG_TAG, "carrierAction: ResetAll fails. Exception ex=" + e);
6878 }
6879 }
6880
6881 /**
Ta-wei Yenc236d6b2016-06-21 13:33:12 -07006882 * Called when "adb shell dumpsys phone" is invoked. Dump is also automatically invoked when a
6883 * bug report is being generated.
6884 */
6885 @Override
Ta-wei Yen99282e02016-06-21 18:19:35 -07006886 protected void dump(FileDescriptor fd, PrintWriter writer, String[] args) {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08006887 if (mApp.checkCallingOrSelfPermission(android.Manifest.permission.DUMP)
6888 != PackageManager.PERMISSION_GRANTED) {
dcashman22b950d2016-06-27 11:39:02 -07006889 writer.println("Permission Denial: can't dump Phone from pid="
6890 + Binder.getCallingPid()
6891 + ", uid=" + Binder.getCallingUid()
6892 + "without permission "
6893 + android.Manifest.permission.DUMP);
6894 return;
6895 }
Nazanin Bakhshif782e562018-12-11 15:15:39 -08006896 DumpsysHandler.dump(mApp, fd, writer, args);
Ta-wei Yenc236d6b2016-06-21 13:33:12 -07006897 }
Jack Yueb89b242016-06-22 13:27:47 -07006898
Brad Ebingerdac2f002018-04-03 15:17:52 -07006899 @Override
6900 public void onShellCommand(FileDescriptor in, FileDescriptor out, FileDescriptor err,
6901 String[] args, ShellCallback callback, ResultReceiver resultReceiver)
6902 throws RemoteException {
Torbjorn Eklund1050cb02018-11-16 14:05:38 +01006903 (new TelephonyShellCommand(this, getDefaultPhone().getContext()))
6904 .exec(this, in, out, err, args, callback, resultReceiver);
Brad Ebingerdac2f002018-04-03 15:17:52 -07006905 }
6906
Jack Yueb89b242016-06-22 13:27:47 -07006907 /**
Jack Yu84291ec2017-05-26 16:07:50 -07006908 * Get aggregated video call data usage since boot.
6909 *
6910 * @param perUidStats True if requesting data usage per uid, otherwise overall usage.
6911 * @return Snapshot of video call data usage
Jack Yueb89b242016-06-22 13:27:47 -07006912 * {@hide}
6913 */
6914 @Override
Jack Yu84291ec2017-05-26 16:07:50 -07006915 public NetworkStats getVtDataUsage(int subId, boolean perUidStats) {
Jack Yueb89b242016-06-22 13:27:47 -07006916 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.READ_NETWORK_USAGE_HISTORY,
6917 null);
6918
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006919 final long identity = Binder.clearCallingIdentity();
6920 try {
6921 // NetworkStatsService keeps tracking the active network interface and identity. It
6922 // records the delta with the corresponding network identity.
6923 // We just return the total video call data usage snapshot since boot.
6924 Phone phone = getPhone(subId);
6925 if (phone != null) {
6926 return phone.getVtDataUsage(perUidStats);
6927 }
6928 return null;
6929 } finally {
6930 Binder.restoreCallingIdentity(identity);
Jack Yueb89b242016-06-22 13:27:47 -07006931 }
Jack Yueb89b242016-06-22 13:27:47 -07006932 }
Jack Yu75ab2952016-07-08 14:29:33 -07006933
6934 /**
6935 * Policy control of data connection. Usually used when data limit is passed.
6936 * @param enabled True if enabling the data, otherwise disabling.
6937 * @param subId Subscription index
6938 * {@hide}
6939 */
6940 @Override
6941 public void setPolicyDataEnabled(boolean enabled, int subId) {
6942 enforceModifyPermission();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006943
6944 final long identity = Binder.clearCallingIdentity();
6945 try {
6946 Phone phone = getPhone(subId);
6947 if (phone != null) {
Jack Yud79fba22018-12-13 11:51:28 -08006948 phone.getDataEnabledSettings().setPolicyDataEnabled(enabled);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006949 }
6950 } finally {
6951 Binder.restoreCallingIdentity(identity);
Jack Yu75ab2952016-07-08 14:29:33 -07006952 }
6953 }
Sooraj Sasindran9a909312016-07-18 11:57:25 -07006954
6955 /**
6956 * Get Client request stats
6957 * @return List of Client Request Stats
6958 * @hide
6959 */
6960 @Override
Philip P. Moltmann9797cbb2020-01-07 13:45:00 -08006961 public List<ClientRequestStats> getClientRequestStats(String callingPackage,
6962 String callingFeatureId, int subId) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08006963 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
Philip P. Moltmann9797cbb2020-01-07 13:45:00 -08006964 mApp, subId, callingPackage, callingFeatureId, "getClientRequestStats")) {
Sooraj Sasindran9a909312016-07-18 11:57:25 -07006965 return null;
6966 }
Sooraj Sasindran9a909312016-07-18 11:57:25 -07006967 Phone phone = getPhone(subId);
Sooraj Sasindran9a909312016-07-18 11:57:25 -07006968
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006969 final long identity = Binder.clearCallingIdentity();
6970 try {
6971 if (phone != null) {
6972 return phone.getClientRequestStats();
6973 }
6974
6975 return null;
6976 } finally {
6977 Binder.restoreCallingIdentity(identity);
6978 }
Sooraj Sasindran9a909312016-07-18 11:57:25 -07006979 }
6980
Narayan Kamathf04b5a12018-01-09 11:47:15 +00006981 private WorkSource getWorkSource(int uid) {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08006982 String packageName = mApp.getPackageManager().getNameForUid(uid);
Narayan Kamathf04b5a12018-01-09 11:47:15 +00006983 return new WorkSource(uid, packageName);
Sooraj Sasindran9a909312016-07-18 11:57:25 -07006984 }
Jack Yueb4124c2017-02-16 15:32:43 -08006985
6986 /**
Grace Chen70990072017-03-24 17:21:30 -07006987 * Set SIM card power state.
Jack Yueb4124c2017-02-16 15:32:43 -08006988 *
Sanket Padawe13bac7b2017-03-20 15:04:47 -07006989 * @param slotIndex SIM slot id.
Grace Chen70990072017-03-24 17:21:30 -07006990 * @param state State of SIM (power down, power up, pass through)
6991 * - {@link android.telephony.TelephonyManager#CARD_POWER_DOWN}
6992 * - {@link android.telephony.TelephonyManager#CARD_POWER_UP}
6993 * - {@link android.telephony.TelephonyManager#CARD_POWER_UP_PASS_THROUGH}
Jack Yueb4124c2017-02-16 15:32:43 -08006994 *
6995 **/
6996 @Override
Grace Chen70990072017-03-24 17:21:30 -07006997 public void setSimPowerStateForSlot(int slotIndex, int state) {
Jack Yueb4124c2017-02-16 15:32:43 -08006998 enforceModifyPermission();
Sanket Padawe13bac7b2017-03-20 15:04:47 -07006999 Phone phone = PhoneFactory.getPhone(slotIndex);
7000
vagdeviaf9a5b92018-08-15 16:01:53 -07007001 WorkSource workSource = getWorkSource(Binder.getCallingUid());
7002
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007003 final long identity = Binder.clearCallingIdentity();
7004 try {
7005 if (phone != null) {
vagdeviaf9a5b92018-08-15 16:01:53 -07007006 phone.setSimPowerState(state, workSource);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007007 }
7008 } finally {
7009 Binder.restoreCallingIdentity(identity);
Jack Yueb4124c2017-02-16 15:32:43 -08007010 }
7011 }
Shuo Qiandd210312017-04-12 22:11:33 +00007012
Tyler Gunn65d45c22017-06-05 11:22:26 -07007013 private boolean isUssdApiAllowed(int subId) {
7014 CarrierConfigManager configManager =
Nazanin Bakhshif782e562018-12-11 15:15:39 -08007015 (CarrierConfigManager) mApp.getSystemService(Context.CARRIER_CONFIG_SERVICE);
Tyler Gunn65d45c22017-06-05 11:22:26 -07007016 if (configManager == null) {
7017 return false;
7018 }
7019 PersistableBundle pb = configManager.getConfigForSubId(subId);
7020 if (pb == null) {
7021 return false;
7022 }
7023 return pb.getBoolean(
7024 CarrierConfigManager.KEY_ALLOW_USSD_REQUESTS_VIA_TELEPHONY_MANAGER_BOOL);
7025 }
7026
Shuo Qiandd210312017-04-12 22:11:33 +00007027 /**
7028 * Check if phone is in emergency callback mode
7029 * @return true if phone is in emergency callback mode
7030 * @param subId sub id
7031 */
goneil9c5f4872017-12-05 14:07:56 -08007032 @Override
Shuo Qiandd210312017-04-12 22:11:33 +00007033 public boolean getEmergencyCallbackMode(int subId) {
Brad Ebinger35c841c2018-10-01 10:40:55 -07007034 enforceReadPrivilegedPermission("getEmergencyCallbackMode");
Shuo Qiandd210312017-04-12 22:11:33 +00007035 final Phone phone = getPhone(subId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007036
7037 final long identity = Binder.clearCallingIdentity();
7038 try {
7039 if (phone != null) {
7040 return phone.isInEcm();
7041 } else {
7042 return false;
7043 }
7044 } finally {
7045 Binder.restoreCallingIdentity(identity);
Shuo Qiandd210312017-04-12 22:11:33 +00007046 }
7047 }
Nathan Harold46b42aa2017-03-10 19:38:22 -08007048
7049 /**
7050 * Get the current signal strength information for the given subscription.
7051 * Because this information is not updated when the device is in a low power state
7052 * it should not be relied-upon to be current.
7053 * @param subId Subscription index
7054 * @return the most recent cached signal strength info from the modem
7055 */
7056 @Override
7057 public SignalStrength getSignalStrength(int subId) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007058 final long identity = Binder.clearCallingIdentity();
7059 try {
7060 Phone p = getPhone(subId);
7061 if (p == null) {
7062 return null;
7063 }
Nathan Harold46b42aa2017-03-10 19:38:22 -08007064
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007065 return p.getSignalStrength();
7066 } finally {
7067 Binder.restoreCallingIdentity(identity);
7068 }
Nathan Harold46b42aa2017-03-10 19:38:22 -08007069 }
Holly Jiuyu Sun01c47ad2018-01-24 17:56:33 +00007070
Pengquan Meng77b7f132018-08-22 14:49:57 -07007071 /**
Chen Xuf792fd62018-10-17 17:54:36 +00007072 * Get the current modem radio state for the given slot.
7073 * @param slotIndex slot index.
7074 * @param callingPackage the name of the package making the call.
Philip P. Moltmann9797cbb2020-01-07 13:45:00 -08007075 * @param callingFeatureId The feature in the package.
Chen Xuf792fd62018-10-17 17:54:36 +00007076 * @return the current radio power state from the modem
7077 */
7078 @Override
Philip P. Moltmann9797cbb2020-01-07 13:45:00 -08007079 public int getRadioPowerState(int slotIndex, String callingPackage, String callingFeatureId) {
Chen Xuf792fd62018-10-17 17:54:36 +00007080 Phone phone = PhoneFactory.getPhone(slotIndex);
7081 if (phone != null) {
Philip P. Moltmann9797cbb2020-01-07 13:45:00 -08007082 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(mApp, phone.getSubId(),
7083 callingPackage, callingFeatureId, "getRadioPowerState")) {
Chen Xuf792fd62018-10-17 17:54:36 +00007084 return TelephonyManager.RADIO_POWER_UNAVAILABLE;
7085 }
7086
7087 final long identity = Binder.clearCallingIdentity();
7088 try {
7089 return phone.getRadioPowerState();
7090 } finally {
7091 Binder.restoreCallingIdentity(identity);
7092 }
7093 }
7094 return TelephonyManager.RADIO_POWER_UNAVAILABLE;
7095 }
7096
7097 /**
Pengquan Meng77b7f132018-08-22 14:49:57 -07007098 * Checks if data roaming is enabled on the subscription with id {@code subId}.
7099 *
7100 * <p>Requires one of the following permissions:
7101 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE},
7102 * {@link android.Manifest.permission#READ_PHONE_STATE} or that the calling app has carrier
7103 * privileges.
7104 *
7105 * @param subId subscription id
7106 * @return {@code true} if data roaming is enabled on this subscription, otherwise return
7107 * {@code false}.
7108 */
7109 @Override
7110 public boolean isDataRoamingEnabled(int subId) {
Pengquan Meng44e66f12019-04-01 10:48:20 -07007111 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.ACCESS_NETWORK_STATE,
7112 null /* message */);
7113
Pengquan Menga1bb6272018-09-06 09:59:22 -07007114 boolean isEnabled = false;
7115 final long identity = Binder.clearCallingIdentity();
Pengquan Meng77b7f132018-08-22 14:49:57 -07007116 try {
Pengquan Menga1bb6272018-09-06 09:59:22 -07007117 Phone phone = getPhone(subId);
7118 isEnabled = phone != null ? phone.getDataRoamingEnabled() : false;
Pengquan Meng77b7f132018-08-22 14:49:57 -07007119 } catch (Exception e) {
7120 TelephonyPermissions.enforeceCallingOrSelfReadPhoneStatePermissionOrCarrierPrivilege(
7121 mApp, subId, "isDataRoamingEnabled");
Pengquan Menga1bb6272018-09-06 09:59:22 -07007122 } finally {
7123 Binder.restoreCallingIdentity(identity);
Pengquan Meng77b7f132018-08-22 14:49:57 -07007124 }
Pengquan Menga1bb6272018-09-06 09:59:22 -07007125 return isEnabled;
Pengquan Meng77b7f132018-08-22 14:49:57 -07007126 }
7127
7128
7129 /**
7130 * Enables/Disables the data roaming on the subscription with id {@code subId}.
7131 *
7132 * <p> Requires permission:
7133 * {@link android.Manifest.permission#MODIFY_PHONE_STATE} or that the calling app has carrier
7134 * privileges.
7135 *
7136 * @param subId subscription id
7137 * @param isEnabled {@code true} means enable, {@code false} means disable.
7138 */
7139 @Override
7140 public void setDataRoamingEnabled(int subId, boolean isEnabled) {
Pengquan Meng44e66f12019-04-01 10:48:20 -07007141 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
7142 mApp, subId, "setDataRoamingEnabled");
7143
Pengquan Menga1bb6272018-09-06 09:59:22 -07007144 final long identity = Binder.clearCallingIdentity();
7145 try {
Pengquan Menga1bb6272018-09-06 09:59:22 -07007146 Phone phone = getPhone(subId);
7147 if (phone != null) {
7148 phone.setDataRoamingEnabled(isEnabled);
7149 }
7150 } finally {
7151 Binder.restoreCallingIdentity(identity);
Pengquan Meng77b7f132018-08-22 14:49:57 -07007152 }
7153 }
7154
Holly Jiuyu Sun01c47ad2018-01-24 17:56:33 +00007155 @Override
Pengquan Meng6884a2c2018-10-03 12:19:13 -07007156 public boolean isManualNetworkSelectionAllowed(int subId) {
tom hsuc91afc72020-01-06 23:46:07 +08007157 TelephonyPermissions
7158 .enforeceCallingOrSelfReadPrivilegedPhoneStatePermissionOrCarrierPrivilege(
Pengquan Meng44e66f12019-04-01 10:48:20 -07007159 mApp, subId, "isManualNetworkSelectionAllowed");
7160
Pengquan Meng6884a2c2018-10-03 12:19:13 -07007161 boolean isAllowed = true;
7162 final long identity = Binder.clearCallingIdentity();
7163 try {
Pengquan Meng6884a2c2018-10-03 12:19:13 -07007164 Phone phone = getPhone(subId);
7165 if (phone != null) {
7166 isAllowed = phone.isCspPlmnEnabled();
7167 }
7168 } finally {
7169 Binder.restoreCallingIdentity(identity);
7170 }
7171 return isAllowed;
7172 }
7173
7174 @Override
Jordan Liu75f43ea2019-01-17 16:56:37 -08007175 public List<UiccCardInfo> getUiccCardsInfo(String callingPackage) {
Jordan Liu1e142fc2019-04-22 15:10:43 -07007176 boolean hasReadPermission = false;
Jordan Liuc65bc952019-02-12 17:54:02 -08007177 try {
7178 enforceReadPrivilegedPermission("getUiccCardsInfo");
Jordan Liu1e142fc2019-04-22 15:10:43 -07007179 hasReadPermission = true;
Jordan Liuc65bc952019-02-12 17:54:02 -08007180 } catch (SecurityException e) {
7181 // even without READ_PRIVILEGED_PHONE_STATE, we allow the call to continue if the caller
7182 // has carrier privileges on an active UICC
7183 if (checkCarrierPrivilegesForPackageAnyPhone(callingPackage)
7184 != TelephonyManager.CARRIER_PRIVILEGE_STATUS_HAS_ACCESS) {
Jordan Liu1e142fc2019-04-22 15:10:43 -07007185 throw new SecurityException("Caller does not have permission.");
Jordan Liuc65bc952019-02-12 17:54:02 -08007186 }
Jordan Liu75f43ea2019-01-17 16:56:37 -08007187 }
Jordan Liu5aa07002018-12-18 15:44:48 -08007188
7189 final long identity = Binder.clearCallingIdentity();
7190 try {
Jordan Liu75f43ea2019-01-17 16:56:37 -08007191 UiccController uiccController = UiccController.getInstance();
7192 ArrayList<UiccCardInfo> cardInfos = uiccController.getAllUiccCardInfos();
Jordan Liu1e142fc2019-04-22 15:10:43 -07007193 if (hasReadPermission) {
7194 return cardInfos;
Jordan Liu75f43ea2019-01-17 16:56:37 -08007195 }
Jordan Liu1e142fc2019-04-22 15:10:43 -07007196
7197 // Remove private info if the caller doesn't have access
7198 ArrayList<UiccCardInfo> filteredInfos = new ArrayList<>();
7199 for (UiccCardInfo cardInfo : cardInfos) {
7200 // For an inactive eUICC, the UiccCard will be null even though the UiccCardInfo
7201 // is available
7202 UiccCard card = uiccController.getUiccCardForSlot(cardInfo.getSlotIndex());
7203 if (card == null || card.getUiccProfile() == null) {
7204 // assume no access if the card or profile is unavailable
7205 filteredInfos.add(cardInfo.getUnprivileged());
7206 continue;
7207 }
7208 UiccProfile profile = card.getUiccProfile();
7209 if (profile.getCarrierPrivilegeStatus(mApp.getPackageManager(), callingPackage)
7210 == TelephonyManager.CARRIER_PRIVILEGE_STATUS_HAS_ACCESS) {
7211 filteredInfos.add(cardInfo);
7212 } else {
7213 filteredInfos.add(cardInfo.getUnprivileged());
7214 }
7215 }
7216 return filteredInfos;
Jordan Liu5aa07002018-12-18 15:44:48 -08007217 } finally {
7218 Binder.restoreCallingIdentity(identity);
7219 }
7220 }
7221
7222 @Override
Holly Jiuyu Sun01c47ad2018-01-24 17:56:33 +00007223 public UiccSlotInfo[] getUiccSlotsInfo() {
Brad Ebinger35c841c2018-10-01 10:40:55 -07007224 enforceReadPrivilegedPermission("getUiccSlotsInfo");
Holly Jiuyu Sun01c47ad2018-01-24 17:56:33 +00007225
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007226 final long identity = Binder.clearCallingIdentity();
7227 try {
7228 UiccSlot[] slots = UiccController.getInstance().getUiccSlots();
7229 if (slots == null) {
7230 Rlog.i(LOG_TAG, "slots is null.");
7231 return null;
7232 }
7233
7234 UiccSlotInfo[] infos = new UiccSlotInfo[slots.length];
7235 for (int i = 0; i < slots.length; i++) {
7236 UiccSlot slot = slots[i];
7237 if (slot == null) {
7238 continue;
7239 }
7240
Jordan Liu7be7e652019-05-06 18:55:02 +00007241 String cardId;
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007242 UiccCard card = slot.getUiccCard();
7243 if (card != null) {
7244 cardId = card.getCardId();
Jordan Liu7be7e652019-05-06 18:55:02 +00007245 } else {
Jordan Liud96b5292019-09-12 16:19:43 -07007246 cardId = slot.getEid();
7247 if (TextUtils.isEmpty(cardId)) {
7248 cardId = slot.getIccId();
7249 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007250 }
7251
Jordan Liu857451f2019-05-09 16:35:35 -07007252 if (cardId != null) {
7253 // if cardId is an ICCID, strip off trailing Fs before exposing to user
7254 // if cardId is an EID, it's all digits so this is fine
7255 cardId = IccUtils.stripTrailingFs(cardId);
7256 }
7257
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007258 int cardState = 0;
7259 switch (slot.getCardState()) {
7260 case CARDSTATE_ABSENT:
7261 cardState = UiccSlotInfo.CARD_STATE_INFO_ABSENT;
7262 break;
7263 case CARDSTATE_PRESENT:
7264 cardState = UiccSlotInfo.CARD_STATE_INFO_PRESENT;
7265 break;
7266 case CARDSTATE_ERROR:
7267 cardState = UiccSlotInfo.CARD_STATE_INFO_ERROR;
7268 break;
7269 case CARDSTATE_RESTRICTED:
7270 cardState = UiccSlotInfo.CARD_STATE_INFO_RESTRICTED;
7271 break;
7272 default:
7273 break;
7274
7275 }
7276
7277 infos[i] = new UiccSlotInfo(
7278 slot.isActive(),
7279 slot.isEuicc(),
7280 cardId,
7281 cardState,
7282 slot.getPhoneId(),
Jordan Liua2619582019-02-14 12:56:40 -08007283 slot.isExtendedApduSupported(),
7284 slot.isRemovable());
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007285 }
7286 return infos;
7287 } finally {
7288 Binder.restoreCallingIdentity(identity);
Holly Jiuyu Sun1d957c52018-04-04 13:52:42 -07007289 }
Holly Jiuyu Sun01c47ad2018-01-24 17:56:33 +00007290 }
7291
7292 @Override
7293 public boolean switchSlots(int[] physicalSlots) {
7294 enforceModifyPermission();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007295
7296 final long identity = Binder.clearCallingIdentity();
7297 try {
7298 return (Boolean) sendRequest(CMD_SWITCH_SLOTS, physicalSlots);
7299 } finally {
7300 Binder.restoreCallingIdentity(identity);
7301 }
Holly Jiuyu Sun01c47ad2018-01-24 17:56:33 +00007302 }
Jack Yu4c988042018-02-27 15:30:01 -08007303
7304 @Override
Jordan Liu7de49fa2018-12-06 14:48:49 -08007305 public int getCardIdForDefaultEuicc(int subId, String callingPackage) {
Jordan Liu7de49fa2018-12-06 14:48:49 -08007306 final long identity = Binder.clearCallingIdentity();
7307 try {
7308 return UiccController.getInstance().getCardIdForDefaultEuicc();
7309 } finally {
7310 Binder.restoreCallingIdentity(identity);
7311 }
7312 }
7313
7314 @Override
Jack Yu4c988042018-02-27 15:30:01 -08007315 public void setRadioIndicationUpdateMode(int subId, int filters, int mode) {
7316 enforceModifyPermission();
7317 final Phone phone = getPhone(subId);
7318 if (phone == null) {
7319 loge("setRadioIndicationUpdateMode fails with invalid subId: " + subId);
7320 return;
7321 }
7322
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007323 final long identity = Binder.clearCallingIdentity();
7324 try {
7325 phone.setRadioIndicationUpdateMode(filters, mode);
7326 } finally {
7327 Binder.restoreCallingIdentity(identity);
7328 }
Jack Yu4c988042018-02-27 15:30:01 -08007329 }
Pengquan Meng85728fb2018-03-12 16:31:21 -07007330
7331 /**
goneil47ffb6e2018-04-06 15:40:58 -07007332 * A test API to reload the UICC profile.
7333 *
7334 * <p>Requires that the calling app has permission
7335 * {@link android.Manifest.permission#MODIFY_PHONE_STATE MODIFY_PHONE_STATE}.
7336 * @hide
7337 */
7338 @Override
7339 public void refreshUiccProfile(int subId) {
7340 enforceModifyPermission();
7341
7342 final long identity = Binder.clearCallingIdentity();
7343 try {
7344 Phone phone = getPhone(subId);
7345 if (phone == null) {
7346 return;
7347 }
7348 UiccCard uiccCard = phone.getUiccCard();
7349 if (uiccCard == null) {
7350 return;
7351 }
7352 UiccProfile uiccProfile = uiccCard.getUiccProfile();
7353 if (uiccProfile == null) {
7354 return;
7355 }
7356 uiccProfile.refresh();
7357 } finally {
7358 Binder.restoreCallingIdentity(identity);
7359 }
7360 }
7361
7362 /**
Pengquan Meng85728fb2018-03-12 16:31:21 -07007363 * Returns false if the mobile data is disabled by default, otherwise return true.
7364 */
7365 private boolean getDefaultDataEnabled() {
Inseob Kim8d298d42018-12-13 17:11:34 +09007366 return TelephonyProperties.mobile_data().orElse(true);
Pengquan Meng85728fb2018-03-12 16:31:21 -07007367 }
7368
7369 /**
7370 * Returns true if the data roaming is enabled by default, i.e the system property
7371 * of {@link #DEFAULT_DATA_ROAMING_PROPERTY_NAME} is true or the config of
7372 * {@link CarrierConfigManager#KEY_CARRIER_DEFAULT_DATA_ROAMING_ENABLED_BOOL} is true.
7373 */
7374 private boolean getDefaultDataRoamingEnabled(int subId) {
7375 final CarrierConfigManager configMgr = (CarrierConfigManager)
Nazanin Bakhshif782e562018-12-11 15:15:39 -08007376 mApp.getSystemService(Context.CARRIER_CONFIG_SERVICE);
Inseob Kim8d298d42018-12-13 17:11:34 +09007377 boolean isDataRoamingEnabled = TelephonyProperties.data_roaming().orElse(true);
Pengquan Meng85728fb2018-03-12 16:31:21 -07007378 isDataRoamingEnabled |= configMgr.getConfigForSubId(subId).getBoolean(
7379 CarrierConfigManager.KEY_CARRIER_DEFAULT_DATA_ROAMING_ENABLED_BOOL);
7380 return isDataRoamingEnabled;
7381 }
7382
7383 /**
7384 * Returns the default network type for the given {@code subId}, if the default network type is
7385 * not set, return {@link Phone#PREFERRED_NT_MODE}.
7386 */
7387 private int getDefaultNetworkType(int subId) {
Inseob Kim8d298d42018-12-13 17:11:34 +09007388 List<Integer> list = TelephonyProperties.default_network();
7389 int phoneId = mSubscriptionController.getPhoneId(subId);
7390 if (phoneId >= 0 && phoneId < list.size() && list.get(phoneId) != null) {
7391 return list.get(phoneId);
7392 }
7393 return Phone.PREFERRED_NT_MODE;
Pengquan Meng85728fb2018-03-12 16:31:21 -07007394 }
fionaxua13278b2018-03-21 00:08:13 -07007395
7396 @Override
7397 public void setCarrierTestOverride(int subId, String mccmnc, String imsi, String iccid, String
chen xueaba88a2019-03-15 13:15:10 -07007398 gid1, String gid2, String plmn, String spn, String carrierPrivilegeRules, String apn) {
fionaxua13278b2018-03-21 00:08:13 -07007399 enforceModifyPermission();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007400
7401 final long identity = Binder.clearCallingIdentity();
7402 try {
7403 final Phone phone = getPhone(subId);
7404 if (phone == null) {
7405 loge("setCarrierTestOverride fails with invalid subId: " + subId);
7406 return;
7407 }
chen xueaba88a2019-03-15 13:15:10 -07007408 phone.setCarrierTestOverride(mccmnc, imsi, iccid, gid1, gid2, plmn, spn,
7409 carrierPrivilegeRules, apn);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007410 } finally {
7411 Binder.restoreCallingIdentity(identity);
fionaxua13278b2018-03-21 00:08:13 -07007412 }
fionaxua13278b2018-03-21 00:08:13 -07007413 }
7414
7415 @Override
7416 public int getCarrierIdListVersion(int subId) {
Brad Ebinger35c841c2018-10-01 10:40:55 -07007417 enforceReadPrivilegedPermission("getCarrierIdListVersion");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007418
7419 final long identity = Binder.clearCallingIdentity();
7420 try {
7421 final Phone phone = getPhone(subId);
7422 if (phone == null) {
7423 loge("getCarrierIdListVersion fails with invalid subId: " + subId);
7424 return TelephonyManager.UNKNOWN_CARRIER_ID_LIST_VERSION;
7425 }
7426 return phone.getCarrierIdListVersion();
7427 } finally {
7428 Binder.restoreCallingIdentity(identity);
fionaxua13278b2018-03-21 00:08:13 -07007429 }
fionaxua13278b2018-03-21 00:08:13 -07007430 }
Malcolm Chen2c63d402018-08-14 16:00:53 -07007431
7432 @Override
Philip P. Moltmann9797cbb2020-01-07 13:45:00 -08007433 public int getNumberOfModemsWithSimultaneousDataConnections(int subId, String callingPackage,
7434 String callingFeatureId) {
Malcolm Chen2c63d402018-08-14 16:00:53 -07007435 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
Philip P. Moltmann9797cbb2020-01-07 13:45:00 -08007436 mApp, subId, callingPackage, callingFeatureId,
7437 "getNumberOfModemsWithSimultaneousDataConnections")) {
Malcolm Chen2c63d402018-08-14 16:00:53 -07007438 return -1;
7439 }
7440
7441 final long identity = Binder.clearCallingIdentity();
7442 try {
7443 return mPhoneConfigurationManager.getNumberOfModemsWithSimultaneousDataConnections();
7444 } finally {
7445 Binder.restoreCallingIdentity(identity);
7446 }
7447 }
Pengquan Menga1bb6272018-09-06 09:59:22 -07007448
7449 @Override
7450 public int getCdmaRoamingMode(int subId) {
7451 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
7452 mApp, subId, "getCdmaRoamingMode");
7453
7454 final long identity = Binder.clearCallingIdentity();
7455 try {
7456 return (int) sendRequest(CMD_GET_CDMA_ROAMING_MODE, null /* argument */, subId);
7457 } finally {
7458 Binder.restoreCallingIdentity(identity);
7459 }
7460 }
7461
7462 @Override
7463 public boolean setCdmaRoamingMode(int subId, int mode) {
7464 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
7465 mApp, subId, "setCdmaRoamingMode");
7466
7467 final long identity = Binder.clearCallingIdentity();
7468 try {
7469 return (boolean) sendRequest(CMD_SET_CDMA_ROAMING_MODE, mode, subId);
7470 } finally {
7471 Binder.restoreCallingIdentity(identity);
7472 }
7473 }
7474
7475 @Override
7476 public boolean setCdmaSubscriptionMode(int subId, int mode) {
7477 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
7478 mApp, subId, "setCdmaSubscriptionMode");
7479
7480 final long identity = Binder.clearCallingIdentity();
7481 try {
7482 return (boolean) sendRequest(CMD_SET_CDMA_SUBSCRIPTION_MODE, mode, subId);
7483 } finally {
7484 Binder.restoreCallingIdentity(identity);
7485 }
7486 }
Makoto Onukida3bf792018-09-18 16:06:29 -07007487
sqianc5eccab2018-10-19 18:46:41 -07007488 @Override
sqian8c685422019-02-22 15:55:18 -08007489 public Map<Integer, List<EmergencyNumber>> getEmergencyNumberList(
Philip P. Moltmann9797cbb2020-01-07 13:45:00 -08007490 String callingPackage, String callingFeatureId) {
sqian11b7a0e2018-12-05 18:48:28 -08007491 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
Philip P. Moltmann9797cbb2020-01-07 13:45:00 -08007492 mApp, getDefaultSubscription(), callingPackage, callingFeatureId,
7493 "getEmergencyNumberList")) {
sqian11b7a0e2018-12-05 18:48:28 -08007494 throw new SecurityException("Requires READ_PHONE_STATE permission.");
7495 }
7496 final long identity = Binder.clearCallingIdentity();
7497 try {
sqian854d44b2018-12-12 16:48:18 -08007498 Map<Integer, List<EmergencyNumber>> emergencyNumberListInternal = new HashMap<>();
7499 for (Phone phone: PhoneFactory.getPhones()) {
7500 if (phone.getEmergencyNumberTracker() != null
7501 && phone.getEmergencyNumberTracker().getEmergencyNumberList() != null) {
7502 emergencyNumberListInternal.put(
7503 phone.getSubId(),
7504 phone.getEmergencyNumberTracker().getEmergencyNumberList());
7505 }
sqian11b7a0e2018-12-05 18:48:28 -08007506 }
sqian854d44b2018-12-12 16:48:18 -08007507 return emergencyNumberListInternal;
sqian11b7a0e2018-12-05 18:48:28 -08007508 } finally {
7509 Binder.restoreCallingIdentity(identity);
7510 }
sqianc5eccab2018-10-19 18:46:41 -07007511 }
7512
7513 @Override
sqian8c685422019-02-22 15:55:18 -08007514 public boolean isEmergencyNumber(String number, boolean exactMatch) {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08007515 final Phone defaultPhone = getDefaultPhone();
sqian11b7a0e2018-12-05 18:48:28 -08007516 if (!exactMatch) {
7517 TelephonyPermissions
7518 .enforeceCallingOrSelfReadPrivilegedPhoneStatePermissionOrCarrierPrivilege(
sqian8c685422019-02-22 15:55:18 -08007519 mApp, defaultPhone.getSubId(), "isEmergencyNumber(Potential)");
sqian11b7a0e2018-12-05 18:48:28 -08007520 }
7521 final long identity = Binder.clearCallingIdentity();
7522 try {
sqian854d44b2018-12-12 16:48:18 -08007523 for (Phone phone: PhoneFactory.getPhones()) {
7524 if (phone.getEmergencyNumberTracker() != null
7525 && phone.getEmergencyNumberTracker() != null) {
7526 if (phone.getEmergencyNumberTracker().isEmergencyNumber(
7527 number, exactMatch)) {
7528 return true;
sqian11b7a0e2018-12-05 18:48:28 -08007529 }
7530 }
sqian11b7a0e2018-12-05 18:48:28 -08007531 }
7532 return false;
7533 } finally {
7534 Binder.restoreCallingIdentity(identity);
7535 }
7536 }
7537
sqianf4ca7ed2019-01-15 18:32:07 -08007538 /**
7539 * Update emergency number list for test mode.
7540 */
7541 @Override
7542 public void updateEmergencyNumberListTestMode(int action, EmergencyNumber num) {
7543 TelephonyPermissions.enforceShellOnly(Binder.getCallingUid(),
7544 "updateEmergencyNumberListTestMode");
7545
7546 final long identity = Binder.clearCallingIdentity();
7547 try {
7548 for (Phone phone: PhoneFactory.getPhones()) {
7549 EmergencyNumberTracker tracker = phone.getEmergencyNumberTracker();
7550 if (tracker != null) {
7551 tracker.executeEmergencyNumberTestModeCommand(action, num);
7552 }
7553 }
7554 } finally {
7555 Binder.restoreCallingIdentity(identity);
7556 }
7557 }
7558
7559 /**
7560 * Get the full emergency number list for test mode.
7561 */
7562 @Override
7563 public List<String> getEmergencyNumberListTestMode() {
7564 TelephonyPermissions.enforceShellOnly(Binder.getCallingUid(),
7565 "getEmergencyNumberListTestMode");
7566
7567 final long identity = Binder.clearCallingIdentity();
7568 try {
7569 Set<String> emergencyNumbers = new HashSet<>();
7570 for (Phone phone: PhoneFactory.getPhones()) {
7571 EmergencyNumberTracker tracker = phone.getEmergencyNumberTracker();
7572 if (tracker != null) {
7573 for (EmergencyNumber num : tracker.getEmergencyNumberList()) {
7574 emergencyNumbers.add(num.getNumber());
7575 }
7576 }
7577 }
7578 return new ArrayList<>(emergencyNumbers);
7579 } finally {
7580 Binder.restoreCallingIdentity(identity);
7581 }
7582 }
7583
chen xud6b45bd2018-10-30 22:27:10 -07007584 @Override
Shuo Qianf2b2df42019-11-13 17:43:31 -08007585 public int getEmergencyNumberDbVersion(int subId) {
7586 enforceReadPrivilegedPermission("getEmergencyNumberDbVersion");
7587
7588 final long identity = Binder.clearCallingIdentity();
7589 try {
7590 final Phone phone = getPhone(subId);
7591 if (phone == null) {
7592 loge("getEmergencyNumberDbVersion fails with invalid subId: " + subId);
7593 return TelephonyManager.INVALID_EMERGENCY_NUMBER_DB_VERSION;
7594 }
7595 return phone.getEmergencyNumberDbVersion();
7596 } finally {
7597 Binder.restoreCallingIdentity(identity);
7598 }
7599 }
7600
7601 @Override
7602 public void notifyOtaEmergencyNumberDbInstalled() {
7603 enforceModifyPermission();
7604
7605 final long identity = Binder.clearCallingIdentity();
7606 try {
7607 for (Phone phone: PhoneFactory.getPhones()) {
7608 EmergencyNumberTracker tracker = phone.getEmergencyNumberTracker();
7609 if (tracker != null) {
7610 tracker.updateOtaEmergencyNumberDatabase();
7611 }
7612 }
7613 } finally {
7614 Binder.restoreCallingIdentity(identity);
7615 }
7616 }
7617
7618 @Override
7619 public void updateTestOtaEmergencyNumberDbFilePath(String otaFilePath) {
7620 enforceActiveEmergencySessionPermission();
7621
7622 final long identity = Binder.clearCallingIdentity();
7623 try {
7624 for (Phone phone: PhoneFactory.getPhones()) {
7625 EmergencyNumberTracker tracker = phone.getEmergencyNumberTracker();
7626 if (tracker != null) {
7627 tracker.updateTestOtaEmergencyNumberDbFilePath(otaFilePath);
7628 }
7629 }
7630 } finally {
7631 Binder.restoreCallingIdentity(identity);
7632 }
7633 }
7634
7635 @Override
chen xud6b45bd2018-10-30 22:27:10 -07007636 public List<String> getCertsFromCarrierPrivilegeAccessRules(int subId) {
7637 enforceReadPrivilegedPermission("getCertsFromCarrierPrivilegeAccessRules");
7638 Phone phone = getPhone(subId);
7639 if (phone == null) {
7640 return null;
7641 }
7642 final long identity = Binder.clearCallingIdentity();
7643 try {
7644 UiccProfile profile = UiccController.getInstance()
7645 .getUiccProfileForPhone(phone.getPhoneId());
7646 if (profile != null) {
7647 return profile.getCertsFromCarrierPrivilegeAccessRules();
7648 }
7649 } finally {
7650 Binder.restoreCallingIdentity(identity);
7651 }
7652 return null;
7653 }
Malcolm Chen8e4ed912019-01-15 20:22:16 -08007654
7655 /**
7656 * Enable or disable a modem stack.
7657 */
7658 @Override
7659 public boolean enableModemForSlot(int slotIndex, boolean enable) {
7660 enforceModifyPermission();
7661
7662 final long identity = Binder.clearCallingIdentity();
7663 try {
7664 Phone phone = PhoneFactory.getPhone(slotIndex);
7665 if (phone == null) {
7666 return false;
7667 } else {
7668 return (Boolean) sendRequest(CMD_REQUEST_ENABLE_MODEM, enable, phone, null);
7669 }
7670 } finally {
7671 Binder.restoreCallingIdentity(identity);
7672 }
7673 }
Michelecea4cf22018-12-21 15:00:11 -08007674
Malcolm Chen4bcd9822019-03-27 18:34:05 -07007675 /**
7676 * Whether a modem stack is enabled or not.
7677 */
7678 @Override
Philip P. Moltmann9797cbb2020-01-07 13:45:00 -08007679 public boolean isModemEnabledForSlot(int slotIndex, String callingPackage,
7680 String callingFeatureId) {
Malcolm Chen4bcd9822019-03-27 18:34:05 -07007681 Phone phone = PhoneFactory.getPhone(slotIndex);
7682 if (phone == null) return false;
7683
7684 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
Philip P. Moltmann9797cbb2020-01-07 13:45:00 -08007685 mApp, phone.getSubId(), callingPackage, callingFeatureId,
7686 "isModemEnabledForSlot")) {
Malcolm Chen4bcd9822019-03-27 18:34:05 -07007687 throw new SecurityException("Requires READ_PHONE_STATE permission.");
7688 }
7689
7690 final long identity = Binder.clearCallingIdentity();
7691 try {
Nazanin Bakhshif71371d2019-04-29 17:29:44 -07007692 try {
7693 return mPhoneConfigurationManager.getPhoneStatusFromCache(phone.getPhoneId());
7694 } catch (NoSuchElementException ex) {
7695 return (Boolean) sendRequest(CMD_GET_MODEM_STATUS, null, phone, null);
7696 }
Malcolm Chen4bcd9822019-03-27 18:34:05 -07007697 } finally {
7698 Binder.restoreCallingIdentity(identity);
7699 }
7700 }
7701
Michelecea4cf22018-12-21 15:00:11 -08007702 @Override
Michele0ea7d782019-03-19 14:58:42 -07007703 public void setMultiSimCarrierRestriction(boolean isMultiSimCarrierRestricted) {
Michelecea4cf22018-12-21 15:00:11 -08007704 enforceModifyPermission();
7705
7706 final long identity = Binder.clearCallingIdentity();
7707 try {
7708 mTelephonySharedPreferences.edit()
Michele0ea7d782019-03-19 14:58:42 -07007709 .putBoolean(PREF_MULTI_SIM_RESTRICTED, isMultiSimCarrierRestricted)
Michelecea4cf22018-12-21 15:00:11 -08007710 .commit();
7711 } finally {
7712 Binder.restoreCallingIdentity(identity);
7713 }
7714 }
7715
7716 @Override
Michele0ea7d782019-03-19 14:58:42 -07007717 @TelephonyManager.IsMultiSimSupportedResult
Philip P. Moltmann9797cbb2020-01-07 13:45:00 -08007718 public int isMultiSimSupported(String callingPackage, String callingFeatureId) {
Michele4245e952019-02-04 11:36:23 -08007719 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(mApp,
Philip P. Moltmann9797cbb2020-01-07 13:45:00 -08007720 getDefaultPhone().getSubId(), callingPackage, callingFeatureId,
7721 "isMultiSimSupported")) {
Michele0ea7d782019-03-19 14:58:42 -07007722 return TelephonyManager.MULTISIM_NOT_SUPPORTED_BY_HARDWARE;
Michele4245e952019-02-04 11:36:23 -08007723 }
Michelecea4cf22018-12-21 15:00:11 -08007724
7725 final long identity = Binder.clearCallingIdentity();
7726 try {
Michele0ea7d782019-03-19 14:58:42 -07007727 return isMultiSimSupportedInternal();
Michelecea4cf22018-12-21 15:00:11 -08007728 } finally {
7729 Binder.restoreCallingIdentity(identity);
7730 }
7731 }
Nazanin Bakhshi628473f2019-01-29 17:37:52 -08007732
Michele0ea7d782019-03-19 14:58:42 -07007733 @TelephonyManager.IsMultiSimSupportedResult
7734 private int isMultiSimSupportedInternal() {
Michele30b57b22019-03-01 12:01:14 -08007735 // If the device has less than 2 SIM cards, indicate that multisim is restricted.
7736 int numPhysicalSlots = UiccController.getInstance().getUiccSlots().length;
7737 if (numPhysicalSlots < 2) {
Michele0ea7d782019-03-19 14:58:42 -07007738 loge("isMultiSimSupportedInternal: requires at least 2 cards");
7739 return TelephonyManager.MULTISIM_NOT_SUPPORTED_BY_HARDWARE;
Michele30b57b22019-03-01 12:01:14 -08007740 }
7741 // Check if the hardware supports multisim functionality. If usage of multisim is not
7742 // supported by the modem, indicate that it is restricted.
7743 PhoneCapability staticCapability =
7744 mPhoneConfigurationManager.getStaticPhoneCapability();
7745 if (staticCapability == null) {
Michele0ea7d782019-03-19 14:58:42 -07007746 loge("isMultiSimSupportedInternal: no static configuration available");
7747 return TelephonyManager.MULTISIM_NOT_SUPPORTED_BY_HARDWARE;
Michele30b57b22019-03-01 12:01:14 -08007748 }
Sarah Chin5f0ecd72019-12-06 10:24:18 -08007749 if (staticCapability.getLogicalModemUuids().size() < 2) {
Michele0ea7d782019-03-19 14:58:42 -07007750 loge("isMultiSimSupportedInternal: maximum number of modem is < 2");
7751 return TelephonyManager.MULTISIM_NOT_SUPPORTED_BY_HARDWARE;
Michele30b57b22019-03-01 12:01:14 -08007752 }
7753 // Check if support of multiple SIMs is restricted by carrier
7754 if (mTelephonySharedPreferences.getBoolean(PREF_MULTI_SIM_RESTRICTED, false)) {
Michele0ea7d782019-03-19 14:58:42 -07007755 return TelephonyManager.MULTISIM_NOT_SUPPORTED_BY_CARRIER;
Michele30b57b22019-03-01 12:01:14 -08007756 }
7757
Michele0ea7d782019-03-19 14:58:42 -07007758 return TelephonyManager.MULTISIM_ALLOWED;
Michele30b57b22019-03-01 12:01:14 -08007759 }
7760
Nazanin Bakhshi628473f2019-01-29 17:37:52 -08007761 /**
7762 * Switch configs to enable multi-sim or switch back to single-sim
Nazanin Bakhshi17318782019-03-01 11:56:08 -08007763 * Note: Switch from multi-sim to single-sim is only possible with MODIFY_PHONE_STATE
7764 * permission, but the other way around is possible with either MODIFY_PHONE_STATE
7765 * or carrier privileges
Nazanin Bakhshi628473f2019-01-29 17:37:52 -08007766 * @param numOfSims number of active sims we want to switch to
7767 */
7768 @Override
7769 public void switchMultiSimConfig(int numOfSims) {
Nazanin Bakhshi17318782019-03-01 11:56:08 -08007770 if (numOfSims == 1) {
7771 enforceModifyPermission();
7772 } else {
7773 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
7774 mApp, SubscriptionManager.DEFAULT_SUBSCRIPTION_ID, "switchMultiSimConfig");
7775 }
Nazanin Bakhshi628473f2019-01-29 17:37:52 -08007776 final long identity = Binder.clearCallingIdentity();
Michele30b57b22019-03-01 12:01:14 -08007777
Nazanin Bakhshi628473f2019-01-29 17:37:52 -08007778 try {
Michele30b57b22019-03-01 12:01:14 -08007779 //only proceed if multi-sim is not restricted
Michele0ea7d782019-03-19 14:58:42 -07007780 if (isMultiSimSupportedInternal() != TelephonyManager.MULTISIM_ALLOWED) {
Michele30b57b22019-03-01 12:01:14 -08007781 loge("switchMultiSimConfig not possible. It is restricted or not supported.");
7782 return;
7783 }
Nazanin Bakhshi628473f2019-01-29 17:37:52 -08007784 mPhoneConfigurationManager.switchMultiSimConfig(numOfSims);
7785 } finally {
7786 Binder.restoreCallingIdentity(identity);
7787 }
7788 }
7789
Hyungjun Parkbb07fde2019-01-10 15:28:51 +09007790 @Override
7791 public boolean isApplicationOnUicc(int subId, int appType) {
7792 enforceReadPrivilegedPermission("isApplicationOnUicc");
7793 Phone phone = getPhone(subId);
7794 if (phone == null) {
7795 return false;
7796 }
7797 final long identity = Binder.clearCallingIdentity();
7798 try {
7799 UiccCard uiccCard = phone.getUiccCard();
7800 if (uiccCard == null) {
7801 return false;
7802 }
7803 UiccProfile uiccProfile = uiccCard.getUiccProfile();
7804 if (uiccProfile == null) {
7805 return false;
7806 }
7807 if (TelephonyManager.APPTYPE_SIM <= appType
7808 && appType <= TelephonyManager.APPTYPE_ISIM) {
7809 return uiccProfile.isApplicationOnIcc(AppType.values()[appType]);
7810 }
7811 return false;
7812 } finally {
7813 Binder.restoreCallingIdentity(identity);
7814 }
7815 }
7816
Nazanin Bakhshi628473f2019-01-29 17:37:52 -08007817 /**
chen xub4baa772019-04-03 10:23:41 -07007818 * Get whether making changes to modem configurations will trigger reboot.
7819 * Return value defaults to true.
Nazanin Bakhshi5fe5ef22019-01-30 10:52:09 -08007820 */
7821 @Override
Philip P. Moltmann9797cbb2020-01-07 13:45:00 -08007822 public boolean doesSwitchMultiSimConfigTriggerReboot(int subId, String callingPackage,
7823 String callingFeatureId) {
chen xub4baa772019-04-03 10:23:41 -07007824 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
Philip P. Moltmann9797cbb2020-01-07 13:45:00 -08007825 mApp, subId, callingPackage, callingFeatureId,
7826 "doesSwitchMultiSimConfigTriggerReboot")) {
chen xub4baa772019-04-03 10:23:41 -07007827 return false;
7828 }
Nazanin Bakhshi5fe5ef22019-01-30 10:52:09 -08007829 final long identity = Binder.clearCallingIdentity();
7830 try {
7831 return mPhoneConfigurationManager.isRebootRequiredForModemConfigChange();
7832 } finally {
7833 Binder.restoreCallingIdentity(identity);
7834 }
7835 }
7836
Nathan Harold29f5f052019-02-15 13:41:57 -08007837 private void updateModemStateMetrics() {
7838 TelephonyMetrics metrics = TelephonyMetrics.getInstance();
7839 // TODO: check the state for each modem if the api is ready.
7840 metrics.updateEnabledModemBitmap((1 << TelephonyManager.from(mApp).getPhoneCount()) - 1);
7841 }
7842
Pengquan Meng3889a572019-01-23 11:16:29 -08007843 @Override
7844 public int[] getSlotsMapping() {
7845 enforceReadPrivilegedPermission("getSlotsMapping");
7846
7847 final long identity = Binder.clearCallingIdentity();
7848 try {
7849 int phoneCount = TelephonyManager.getDefault().getPhoneCount();
7850 // All logical slots should have a mapping to a physical slot.
7851 int[] logicalSlotsMapping = new int[phoneCount];
7852 UiccSlotInfo[] slotInfos = getUiccSlotsInfo();
7853 for (int i = 0; i < slotInfos.length; i++) {
7854 if (SubscriptionManager.isValidPhoneId(slotInfos[i].getLogicalSlotIdx())) {
7855 logicalSlotsMapping[slotInfos[i].getLogicalSlotIdx()] = i;
7856 }
7857 }
7858 return logicalSlotsMapping;
7859 } finally {
7860 Binder.restoreCallingIdentity(identity);
7861 }
7862 }
Nathan Harold48d6fd52019-02-06 19:01:40 -08007863
7864 /**
7865 * Get the IRadio HAL Version
7866 */
7867 @Override
7868 public int getRadioHalVersion() {
7869 Phone phone = getDefaultPhone();
7870 if (phone == null) return -1;
7871 HalVersion hv = phone.getHalVersion();
7872 if (hv.equals(HalVersion.UNKNOWN)) return -1;
7873 return hv.major * 100 + hv.minor;
7874 }
Malcolm Chendc8c10e2019-04-10 18:25:07 -07007875
7876 /**
Shuo Qianaf42a312020-01-14 15:18:28 -08007877 * Get the current calling package name.
7878 * @return the current calling package name
7879 */
7880 @Override
7881 public String getCurrentPackageName() {
7882 return mApp.getPackageManager().getPackagesForUid(Binder.getCallingUid())[0];
7883 }
7884
7885 /**
Malcolm Chene5ad5792019-04-18 13:51:02 -07007886 * Return whether data is enabled for certain APN type. This will tell if framework will accept
7887 * corresponding network requests on a subId.
7888 *
7889 * Data is enabled if:
Malcolm Chendc8c10e2019-04-10 18:25:07 -07007890 * 1) user data is turned on, or
Malcolm Chene5ad5792019-04-18 13:51:02 -07007891 * 2) APN is un-metered for this subscription, or
7892 * 3) APN type is whitelisted. E.g. MMS is whitelisted if
7893 * {@link SubscriptionManager#setAlwaysAllowMmsData} is turned on.
7894 *
7895 * @return whether data is allowed for a apn type.
7896 *
7897 * @hide
Malcolm Chendc8c10e2019-04-10 18:25:07 -07007898 */
7899 @Override
Malcolm Chene5ad5792019-04-18 13:51:02 -07007900 public boolean isDataEnabledForApn(int apnType, int subId, String callingPackage) {
Amit Mahajanf2509742019-10-07 16:20:43 -07007901 enforceReadPrivilegedPermission("Needs READ_PRIVILEGED_PHONE_STATE for "
7902 + "isDataEnabledForApn");
Malcolm Chendc8c10e2019-04-10 18:25:07 -07007903
7904 // Now that all security checks passes, perform the operation as ourselves.
7905 final long identity = Binder.clearCallingIdentity();
7906 try {
7907 Phone phone = getPhone(subId);
7908 if (phone == null) return false;
7909
Jack Yu41407ee2019-05-13 16:54:09 -07007910 boolean isMetered = ApnSettingUtils.isMeteredApnType(apnType, phone);
Malcolm Chene5ad5792019-04-18 13:51:02 -07007911 return !isMetered || phone.getDataEnabledSettings().isDataEnabled(apnType);
7912 } finally {
7913 Binder.restoreCallingIdentity(identity);
7914 }
7915 }
7916
7917 @Override
Jack Yu41407ee2019-05-13 16:54:09 -07007918 public boolean isApnMetered(@ApnType int apnType, int subId) {
Malcolm Chene5ad5792019-04-18 13:51:02 -07007919 enforceReadPrivilegedPermission("isApnMetered");
7920
7921 // Now that all security checks passes, perform the operation as ourselves.
7922 final long identity = Binder.clearCallingIdentity();
7923 try {
7924 Phone phone = getPhone(subId);
7925 if (phone == null) return true; // By default return true.
7926
Jack Yu41407ee2019-05-13 16:54:09 -07007927 return ApnSettingUtils.isMeteredApnType(apnType, phone);
Malcolm Chendc8c10e2019-04-10 18:25:07 -07007928 } finally {
7929 Binder.restoreCallingIdentity(identity);
7930 }
7931 }
Brad Ebingera63db5f2019-04-23 16:31:13 -07007932
7933 @Override
changbetty363e8ac2019-12-06 18:16:37 +08007934 public boolean isMvnoMatched(int subId, int mvnoType, @NonNull String mvnoMatchData) {
7935 IccRecords iccRecords = UiccController.getInstance().getIccRecords(
7936 SubscriptionManager.getPhoneId(subId), UiccController.APP_FAM_3GPP);
7937 if (iccRecords == null) {
7938 Log.d(LOG_TAG, "isMvnoMatched# IccRecords is null");
7939 return false;
7940 }
7941 return ApnSettingUtils.mvnoMatches(iccRecords, mvnoType, mvnoMatchData);
7942 }
7943
7944 @Override
Brad Ebingera63db5f2019-04-23 16:31:13 -07007945 public void enqueueSmsPickResult(String callingPackage, IIntegerConsumer pendingSubIdResult) {
Shuo Qianaf42a312020-01-14 15:18:28 -08007946 if (callingPackage == null) {
7947 callingPackage = getCurrentPackageName();
7948 }
Brad Ebingera63db5f2019-04-23 16:31:13 -07007949 SmsPermissions permissions = new SmsPermissions(getDefaultPhone(), mApp,
7950 (AppOpsManager) mApp.getSystemService(Context.APP_OPS_SERVICE));
7951 if (!permissions.checkCallingCanSendSms(callingPackage, "Sending message")) {
7952 throw new SecurityException("Requires SEND_SMS permission to perform this operation");
7953 }
7954 PickSmsSubscriptionActivity.addPendingResult(pendingSubIdResult);
7955 Intent intent = new Intent();
7956 intent.setClass(mApp, PickSmsSubscriptionActivity.class);
7957 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
7958 // Bring up choose default SMS subscription dialog right now
7959 intent.putExtra(PickSmsSubscriptionActivity.DIALOG_TYPE_KEY,
7960 PickSmsSubscriptionActivity.SMS_PICK_FOR_MESSAGE);
7961 mApp.startActivity(intent);
7962 }
chen xud5ca2d52019-05-28 15:20:57 -07007963
7964 @Override
7965 public String getMmsUAProfUrl(int subId) {
7966 //TODO investigate if this API should require proper permission check in R b/133791609
7967 final long identity = Binder.clearCallingIdentity();
7968 try {
7969 return SubscriptionManager.getResourcesForSubId(getDefaultPhone().getContext(), subId)
7970 .getString(com.android.internal.R.string.config_mms_user_agent_profile_url);
7971 } finally {
7972 Binder.restoreCallingIdentity(identity);
7973 }
7974 }
7975
7976 @Override
7977 public String getMmsUserAgent(int subId) {
7978 //TODO investigate if this API should require proper permission check in R b/133791609
7979 final long identity = Binder.clearCallingIdentity();
7980 try {
7981 return SubscriptionManager.getResourcesForSubId(getDefaultPhone().getContext(), subId)
7982 .getString(com.android.internal.R.string.config_mms_user_agent);
7983 } finally {
7984 Binder.restoreCallingIdentity(identity);
7985 }
7986 }
Jack Yub07d4972019-05-28 16:12:25 -07007987
7988 @Override
7989 public boolean setDataAllowedDuringVoiceCall(int subId, boolean allow) {
7990 enforceModifyPermission();
7991
7992 // Now that all security checks passes, perform the operation as ourselves.
7993 final long identity = Binder.clearCallingIdentity();
7994 try {
7995 Phone phone = getPhone(subId);
7996 if (phone == null) return false;
7997
7998 return phone.getDataEnabledSettings().setAllowDataDuringVoiceCall(allow);
7999 } finally {
8000 Binder.restoreCallingIdentity(identity);
8001 }
8002 }
8003
8004 @Override
8005 public boolean isDataAllowedInVoiceCall(int subId) {
8006 enforceReadPrivilegedPermission("isDataAllowedInVoiceCall");
8007
8008 // Now that all security checks passes, perform the operation as ourselves.
8009 final long identity = Binder.clearCallingIdentity();
8010 try {
8011 Phone phone = getPhone(subId);
8012 if (phone == null) return false;
8013
8014 return phone.getDataEnabledSettings().isDataAllowedInVoiceCall();
8015 } finally {
8016 Binder.restoreCallingIdentity(identity);
8017 }
8018 }
Tyler Gunn7bcdc742019-10-04 15:56:59 -07008019
8020 /**
8021 * Updates whether conference event pacakge handling is enabled.
8022 * @param isCepEnabled {@code true} if CEP handling is enabled (default), or {@code false}
8023 * otherwise.
8024 */
8025 @Override
8026 public void setCepEnabled(boolean isCepEnabled) {
8027 TelephonyPermissions.enforceShellOnly(Binder.getCallingUid(), "setCepEnabled");
8028
8029 final long identity = Binder.clearCallingIdentity();
8030 try {
8031 Rlog.i(LOG_TAG, "setCepEnabled isCepEnabled=" + isCepEnabled);
8032 for (Phone phone : PhoneFactory.getPhones()) {
8033 Phone defaultPhone = phone.getImsPhone();
8034 if (defaultPhone != null && defaultPhone.getPhoneType() == PHONE_TYPE_IMS) {
8035 ImsPhone imsPhone = (ImsPhone) defaultPhone;
8036 ImsPhoneCallTracker imsPhoneCallTracker =
8037 (ImsPhoneCallTracker) imsPhone.getCallTracker();
8038 imsPhoneCallTracker.setConferenceEventPackageEnabled(isCepEnabled);
8039 Rlog.i(LOG_TAG, "setCepEnabled isCepEnabled=" + isCepEnabled + ", for imsPhone "
8040 + imsPhone.getMsisdn());
8041 }
8042 }
8043 } finally {
8044 Binder.restoreCallingIdentity(identity);
8045 }
8046 }
allenwtsu46dcc572020-01-08 18:24:03 +08008047
8048 /**
8049 * Notify that an RCS autoconfiguration XML file has been received for provisioning.
8050 *
8051 * @param config The XML file to be read. ASCII/UTF8 encoded text if not compressed.
8052 * @param isCompressed The XML file is compressed in gzip format and must be decompressed
8053 * before being read.
8054 */
8055 @Override
8056 public void notifyRcsAutoConfigurationReceived(int subId, @NonNull byte[] config, boolean
8057 isCompressed) {
8058 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
8059 mApp, subId, "notifyRcsAutoConfigurationReceived");
8060 try {
8061 IImsConfig configBinder = getImsConfig(getSlotIndex(subId), ImsFeature.FEATURE_RCS);
8062 if (configBinder == null) {
8063 Rlog.e(LOG_TAG, "null result for getImsConfig");
8064 } else {
8065 configBinder.notifyRcsAutoConfigurationReceived(config, isCompressed);
8066 }
8067 } catch (RemoteException e) {
8068 Rlog.e(LOG_TAG, "fail to getImsConfig " + e.getMessage());
8069 }
8070 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07008071}