blob: e9cd63878c596739d2a769b6ff76e9d807410064 [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;
Tyler Gunnf70ed162019-04-03 15:28:53 -070025import android.annotation.Nullable;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070026import android.app.AppOpsManager;
Ta-wei Yen30a69c82016-12-27 14:52:32 -080027import android.app.PendingIntent;
Sailesh Nepalbd76e4e2013-10-27 13:59:44 -070028import android.content.ComponentName;
Amit Mahajan7dbbd822019-03-13 17:33:47 -070029import android.content.ContentResolver;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070030import android.content.Context;
31import android.content.Intent;
Derek Tan97ebb422014-09-05 16:55:38 -070032import android.content.SharedPreferences;
Nathan Harold31d7ff32018-10-15 20:20:30 -070033import android.content.pm.ApplicationInfo;
Derek Tan740e1672017-06-27 14:56:27 -070034import android.content.pm.ComponentInfo;
Amith Yamasani6e118872016-02-19 12:53:51 -080035import android.content.pm.PackageInfo;
Shishir Agrawal60f9c952014-06-23 12:00:43 -070036import android.content.pm.PackageManager;
Jack Yu84291ec2017-05-26 16:07:50 -070037import android.net.NetworkStats;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070038import android.net.Uri;
39import android.os.AsyncResult;
40import android.os.Binder;
Hall Liuf19c44f2018-11-27 14:38:17 -080041import android.os.Build;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070042import android.os.Bundle;
43import android.os.Handler;
yinxu504e1392017-04-12 16:03:22 -070044import android.os.IBinder;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070045import android.os.Looper;
46import android.os.Message;
yinxu504e1392017-04-12 16:03:22 -070047import android.os.Messenger;
Malcolm Chen6ca97372019-07-01 16:28:21 -070048import android.os.ParcelUuid;
Tyler Gunn65d45c22017-06-05 11:22:26 -070049import android.os.PersistableBundle;
Brad Ebinger5f64b052017-12-14 14:26:15 -080050import android.os.RemoteException;
Adam Lesinski903a54c2016-04-11 14:49:52 -070051import android.os.ResultReceiver;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070052import android.os.ServiceManager;
Brad Ebinger1ce9c432019-07-16 13:19:44 -070053import android.os.ServiceSpecificException;
Brad Ebingerdac2f002018-04-03 15:17:52 -070054import android.os.ShellCallback;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070055import android.os.UserHandle;
Stuart Scott981d8582015-04-21 14:09:50 -070056import android.os.UserManager;
Sooraj Sasindran9a909312016-07-18 11:57:25 -070057import android.os.WorkSource;
Derek Tan97ebb422014-09-05 16:55:38 -070058import android.preference.PreferenceManager;
Naina Nalluri8ff344d2019-09-17 14:10:30 -070059import android.provider.DeviceConfig;
Ihab Awadf2177b72013-11-25 13:33:23 -080060import android.provider.Settings;
Amit Mahajan7dbbd822019-03-13 17:33:47 -070061import android.provider.Telephony;
Inseob Kim8d298d42018-12-13 17:11:34 +090062import android.sysprop.TelephonyProperties;
Santos Cordon7a1885b2015-02-03 11:15:19 -080063import android.telecom.PhoneAccount;
Nancy Chen31f9ba12016-01-06 11:42:12 -080064import android.telecom.PhoneAccountHandle;
Andrew Lee9431b832015-03-09 18:46:45 -070065import android.telecom.TelecomManager;
Chen Xuc7b18ec2019-09-26 22:48:11 -070066import android.telephony.Annotation.ApnType;
Junda Liu12f7d802015-05-01 12:06:44 -070067import android.telephony.CarrierConfigManager;
Michele Berionne482f8202018-11-27 18:57:59 -080068import android.telephony.CarrierRestrictionRules;
yincheng zhaod698b842019-09-06 17:06:54 -070069import android.telephony.CellIdentity;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070070import android.telephony.CellInfo;
Nathan Haroldf180aac2018-06-01 18:43:55 -070071import android.telephony.CellInfoGsm;
72import android.telephony.CellInfoWcdma;
Nathan Harold3ff88932018-08-14 10:19:49 -070073import android.telephony.CellLocation;
Sooraj Sasindran9a909312016-07-18 11:57:25 -070074import android.telephony.ClientRequestStats;
Nathan Haroldfa8da0f2018-09-27 18:51:29 -070075import android.telephony.ICellInfoCallback;
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -070076import android.telephony.IccOpenLogicalChannelResponse;
Hall Liu1aa510f2017-11-22 17:40:08 -080077import android.telephony.LocationAccessPolicy;
Ta-wei Yen87c49842016-05-13 21:19:52 -070078import android.telephony.ModemActivityInfo;
Jake Hambye994d462014-02-03 13:10:13 -080079import android.telephony.NeighboringCellInfo;
yinxu504e1392017-04-12 16:03:22 -070080import android.telephony.NetworkScanRequest;
Michele4245e952019-02-04 11:36:23 -080081import android.telephony.PhoneCapability;
Hall Liud892bec2018-11-30 14:51:45 -080082import android.telephony.PhoneNumberRange;
Wink Saville5d475dd2014-10-17 15:00:58 -070083import android.telephony.RadioAccessFamily;
Hall Liub2ac8ef2019-02-28 15:56:23 -080084import android.telephony.RadioAccessSpecifier;
Tyler Gunn65d45c22017-06-05 11:22:26 -070085import android.telephony.Rlog;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070086import android.telephony.ServiceState;
Nathan Harold46b42aa2017-03-10 19:38:22 -080087import android.telephony.SignalStrength;
Wink Saville0f3b5fc2014-11-11 08:40:49 -080088import android.telephony.SubscriptionInfo;
Jeff Sharkey85190e62014-12-05 09:40:12 -080089import android.telephony.SubscriptionManager;
Sanket Padawe99ef1e32016-05-18 16:12:33 -070090import android.telephony.TelephonyHistogram;
Ta-wei Yenb6929602016-05-24 15:48:27 -070091import android.telephony.TelephonyManager;
Hall Liub2ac8ef2019-02-28 15:56:23 -080092import android.telephony.TelephonyScanManager;
Jordan Liu5aa07002018-12-18 15:44:48 -080093import android.telephony.UiccCardInfo;
Holly Jiuyu Sun01c47ad2018-01-24 17:56:33 +000094import android.telephony.UiccSlotInfo;
Tyler Gunn65d45c22017-06-05 11:22:26 -070095import android.telephony.UssdResponse;
Ta-wei Yenb6929602016-05-24 15:48:27 -070096import android.telephony.VisualVoicemailSmsFilterSettings;
Nathan Harold3ff88932018-08-14 10:19:49 -070097import android.telephony.cdma.CdmaCellLocation;
Jack Yub5d8f642018-11-26 11:20:48 -080098import android.telephony.data.ApnSetting;
99import android.telephony.emergency.EmergencyNumber;
Nathan Harold3ff88932018-08-14 10:19:49 -0700100import android.telephony.gsm.GsmCellLocation;
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;
161import com.android.internal.telephony.uicc.IccUtils;
Nathan Haroldb3014052017-01-25 15:57:32 -0800162import com.android.internal.telephony.uicc.SIMRecords;
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700163import com.android.internal.telephony.uicc.UiccCard;
Nathan Haroldb3014052017-01-25 15:57:32 -0800164import com.android.internal.telephony.uicc.UiccCardApplication;
Shishir Agrawal566b7612013-10-28 14:41:00 -0700165import com.android.internal.telephony.uicc.UiccController;
Jeff Davidson7e17e312018-02-13 18:17:36 -0800166import com.android.internal.telephony.uicc.UiccProfile;
Holly Jiuyu Sun01c47ad2018-01-24 17:56:33 +0000167import com.android.internal.telephony.uicc.UiccSlot;
zoey chen84e2b212019-12-18 17:07:20 +0800168import com.android.internal.telephony.util.LocaleUtils;
fionaxu7ed723d2017-05-30 18:58:54 -0700169import com.android.internal.telephony.util.VoicemailNotificationSettingsUtil;
Jake Hambye994d462014-02-03 13:10:13 -0800170import com.android.internal.util.HexDump;
Brad Ebingera63db5f2019-04-23 16:31:13 -0700171import com.android.phone.settings.PickSmsSubscriptionActivity;
Ta-wei Yenc9df0432017-04-17 17:09:07 -0700172import com.android.phone.vvm.PhoneAccountHandleConverter;
Ta-wei Yen527a9c02017-01-06 15:29:25 -0800173import com.android.phone.vvm.RemoteVvmTaskManager;
Ta-wei Yenc9df0432017-04-17 17:09:07 -0700174import com.android.phone.vvm.VisualVoicemailSettingsUtil;
Ta-wei Yenc8905312017-03-28 11:14:45 -0700175import com.android.phone.vvm.VisualVoicemailSmsFilterConfig;
Ta-wei Yen30a69c82016-12-27 14:52:32 -0800176
Ta-wei Yenc236d6b2016-06-21 13:33:12 -0700177import java.io.FileDescriptor;
178import java.io.PrintWriter;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700179import java.util.ArrayList;
Jeff Sharkey85190e62014-12-05 09:40:12 -0800180import java.util.Arrays;
sqian11b7a0e2018-12-05 18:48:28 -0800181import java.util.HashMap;
sqianf4ca7ed2019-01-15 18:32:07 -0800182import java.util.HashSet;
Jake Hambye994d462014-02-03 13:10:13 -0800183import java.util.List;
Narayan Kamath1c496c22015-04-16 14:40:19 +0100184import java.util.Locale;
Jeff Sharkey85190e62014-12-05 09:40:12 -0800185import java.util.Map;
Nazanin Bakhshif71371d2019-04-29 17:29:44 -0700186import java.util.NoSuchElementException;
sqianf4ca7ed2019-01-15 18:32:07 -0800187import java.util.Set;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700188
189/**
190 * Implementation of the ITelephony interface.
191 */
Santos Cordon117fee72014-05-16 17:56:12 -0700192public class PhoneInterfaceManager extends ITelephony.Stub {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700193 private static final String LOG_TAG = "PhoneInterfaceManager";
194 private static final boolean DBG = (PhoneGlobals.DBG_LEVEL >= 2);
195 private static final boolean DBG_LOC = false;
Jeff Sharkey85190e62014-12-05 09:40:12 -0800196 private static final boolean DBG_MERGE = false;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700197
198 // Message codes used with mMainThreadHandler
199 private static final int CMD_HANDLE_PIN_MMI = 1;
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700200 private static final int CMD_TRANSMIT_APDU_LOGICAL_CHANNEL = 7;
201 private static final int EVENT_TRANSMIT_APDU_LOGICAL_CHANNEL_DONE = 8;
Shishir Agrawal566b7612013-10-28 14:41:00 -0700202 private static final int CMD_OPEN_CHANNEL = 9;
203 private static final int EVENT_OPEN_CHANNEL_DONE = 10;
204 private static final int CMD_CLOSE_CHANNEL = 11;
205 private static final int EVENT_CLOSE_CHANNEL_DONE = 12;
Jake Hambye994d462014-02-03 13:10:13 -0800206 private static final int CMD_NV_READ_ITEM = 13;
207 private static final int EVENT_NV_READ_ITEM_DONE = 14;
208 private static final int CMD_NV_WRITE_ITEM = 15;
209 private static final int EVENT_NV_WRITE_ITEM_DONE = 16;
210 private static final int CMD_NV_WRITE_CDMA_PRL = 17;
211 private static final int EVENT_NV_WRITE_CDMA_PRL_DONE = 18;
chen xu6dac5ab2018-10-26 17:39:23 -0700212 private static final int CMD_RESET_MODEM_CONFIG = 19;
213 private static final int EVENT_RESET_MODEM_CONFIG_DONE = 20;
Jake Hamby7c27be32014-03-03 13:25:59 -0800214 private static final int CMD_GET_PREFERRED_NETWORK_TYPE = 21;
215 private static final int EVENT_GET_PREFERRED_NETWORK_TYPE_DONE = 22;
216 private static final int CMD_SET_PREFERRED_NETWORK_TYPE = 23;
217 private static final int EVENT_SET_PREFERRED_NETWORK_TYPE_DONE = 24;
Sailesh Nepal35b59452014-03-06 09:26:56 -0800218 private static final int CMD_SEND_ENVELOPE = 25;
219 private static final int EVENT_SEND_ENVELOPE_DONE = 26;
Shuo Qian850e4d6a2018-04-25 21:02:08 +0000220 private static final int CMD_INVOKE_OEM_RIL_REQUEST_RAW = 27;
221 private static final int EVENT_INVOKE_OEM_RIL_REQUEST_RAW_DONE = 28;
Derek Tan6b088ee2014-09-05 14:15:18 -0700222 private static final int CMD_TRANSMIT_APDU_BASIC_CHANNEL = 29;
223 private static final int EVENT_TRANSMIT_APDU_BASIC_CHANNEL_DONE = 30;
224 private static final int CMD_EXCHANGE_SIM_IO = 31;
225 private static final int EVENT_EXCHANGE_SIM_IO_DONE = 32;
Shishir Agrawal76d5da92014-11-09 16:17:25 -0800226 private static final int CMD_SET_VOICEMAIL_NUMBER = 33;
227 private static final int EVENT_SET_VOICEMAIL_NUMBER_DONE = 34;
Stuart Scott54788802015-03-30 13:18:01 -0700228 private static final int CMD_SET_NETWORK_SELECTION_MODE_AUTOMATIC = 35;
229 private static final int EVENT_SET_NETWORK_SELECTION_MODE_AUTOMATIC_DONE = 36;
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -0700230 private static final int CMD_GET_MODEM_ACTIVITY_INFO = 37;
231 private static final int EVENT_GET_MODEM_ACTIVITY_INFO_DONE = 38;
Shishir Agrawal302c8692015-06-19 13:49:39 -0700232 private static final int CMD_PERFORM_NETWORK_SCAN = 39;
233 private static final int EVENT_PERFORM_NETWORK_SCAN_DONE = 40;
234 private static final int CMD_SET_NETWORK_SELECTION_MODE_MANUAL = 41;
235 private static final int EVENT_SET_NETWORK_SELECTION_MODE_MANUAL_DONE = 42;
Meng Wang1a7c35a2016-05-05 20:56:15 -0700236 private static final int CMD_SET_ALLOWED_CARRIERS = 43;
237 private static final int EVENT_SET_ALLOWED_CARRIERS_DONE = 44;
238 private static final int CMD_GET_ALLOWED_CARRIERS = 45;
239 private static final int EVENT_GET_ALLOWED_CARRIERS_DONE = 46;
pkanwar32d516d2016-10-14 19:37:38 -0700240 private static final int CMD_HANDLE_USSD_REQUEST = 47;
Nathan Haroldb3014052017-01-25 15:57:32 -0800241 private static final int CMD_GET_FORBIDDEN_PLMNS = 48;
242 private static final int EVENT_GET_FORBIDDEN_PLMNS_DONE = 49;
Holly Jiuyu Sun01c47ad2018-01-24 17:56:33 +0000243 private static final int CMD_SWITCH_SLOTS = 50;
244 private static final int EVENT_SWITCH_SLOTS_DONE = 51;
Pengquan Menga1bb6272018-09-06 09:59:22 -0700245 private static final int CMD_GET_NETWORK_SELECTION_MODE = 52;
246 private static final int EVENT_GET_NETWORK_SELECTION_MODE_DONE = 53;
247 private static final int CMD_GET_CDMA_ROAMING_MODE = 54;
248 private static final int EVENT_GET_CDMA_ROAMING_MODE_DONE = 55;
249 private static final int CMD_SET_CDMA_ROAMING_MODE = 56;
250 private static final int EVENT_SET_CDMA_ROAMING_MODE_DONE = 57;
251 private static final int CMD_SET_CDMA_SUBSCRIPTION_MODE = 58;
252 private static final int EVENT_SET_CDMA_SUBSCRIPTION_MODE_DONE = 59;
Nathan Harold3ff88932018-08-14 10:19:49 -0700253 private static final int CMD_GET_ALL_CELL_INFO = 60;
254 private static final int EVENT_GET_ALL_CELL_INFO_DONE = 61;
255 private static final int CMD_GET_CELL_LOCATION = 62;
256 private static final int EVENT_GET_CELL_LOCATION_DONE = 63;
chen xu6dac5ab2018-10-26 17:39:23 -0700257 private static final int CMD_MODEM_REBOOT = 64;
258 private static final int EVENT_CMD_MODEM_REBOOT_DONE = 65;
Nathan Haroldfa8da0f2018-09-27 18:51:29 -0700259 private static final int CMD_REQUEST_CELL_INFO_UPDATE = 66;
260 private static final int EVENT_REQUEST_CELL_INFO_UPDATE_DONE = 67;
Malcolm Chen8e4ed912019-01-15 20:22:16 -0800261 private static final int CMD_REQUEST_ENABLE_MODEM = 68;
262 private static final int EVENT_ENABLE_MODEM_DONE = 69;
Nazanin Bakhshif71371d2019-04-29 17:29:44 -0700263 private static final int CMD_GET_MODEM_STATUS = 70;
264 private static final int EVENT_GET_MODEM_STATUS_DONE = 71;
yincheng zhaod698b842019-09-06 17:06:54 -0700265 private static final int CMD_SET_FORBIDDEN_PLMNS = 72;
266 private static final int EVENT_SET_FORBIDDEN_PLMNS_DONE = 73;
Naina Nalluri8ff344d2019-09-17 14:10:30 -0700267 private static final int CMD_ERASE_MODEM_CONFIG = 74;
268 private static final int EVENT_ERASE_MODEM_CONFIG_DONE = 75;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700269
Holly Jiuyu Sun1cc2d552018-01-26 15:51:16 -0800270 // Parameters of select command.
271 private static final int SELECT_COMMAND = 0xA4;
272 private static final int SELECT_P1 = 0x04;
273 private static final int SELECT_P2 = 0;
274 private static final int SELECT_P3 = 0x10;
275
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700276 /** The singleton instance. */
277 private static PhoneInterfaceManager sInstance;
278
Wink Saville3ab207e2014-11-20 13:07:20 -0800279 private PhoneGlobals mApp;
Wink Saville3ab207e2014-11-20 13:07:20 -0800280 private CallManager mCM;
Brad Ebinger05f52c22019-12-05 13:03:21 -0800281 private ImsResolver mImsResolver;
Stuart Scott981d8582015-04-21 14:09:50 -0700282 private UserManager mUserManager;
Wink Saville3ab207e2014-11-20 13:07:20 -0800283 private AppOpsManager mAppOps;
284 private MainThreadHandler mMainThreadHandler;
Wink Savilleac1bdfd2014-11-20 23:04:44 -0800285 private SubscriptionController mSubscriptionController;
Wink Saville3ab207e2014-11-20 13:07:20 -0800286 private SharedPreferences mTelephonySharedPreferences;
Malcolm Chen2c63d402018-08-14 16:00:53 -0700287 private PhoneConfigurationManager mPhoneConfigurationManager;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700288
Derek Tan97ebb422014-09-05 16:55:38 -0700289 private static final String PREF_CARRIERS_ALPHATAG_PREFIX = "carrier_alphtag_";
290 private static final String PREF_CARRIERS_NUMBER_PREFIX = "carrier_number_";
Jeff Sharkey85190e62014-12-05 09:40:12 -0800291 private static final String PREF_CARRIERS_SUBSCRIBER_PREFIX = "carrier_subscriber_";
Brad Ebinger1c8542e2019-01-14 13:43:14 -0800292 private static final String PREF_PROVISION_IMS_MMTEL_PREFIX = "provision_ims_mmtel_";
Derek Tan89e89d42014-07-08 17:00:10 -0700293
Michelecea4cf22018-12-21 15:00:11 -0800294 // String to store multi SIM allowed
295 private static final String PREF_MULTI_SIM_RESTRICTED = "multisim_restricted";
296
Derek Tan740e1672017-06-27 14:56:27 -0700297 // The AID of ISD-R.
298 private static final String ISDR_AID = "A0000005591010FFFFFFFF8900000100";
299
yinxub1bed742017-04-17 11:45:04 -0700300 private NetworkScanRequestTracker mNetworkScanRequestTracker;
301
David Kelly5e06a7f2018-03-12 14:10:59 +0000302 private static final int TYPE_ALLOCATION_CODE_LENGTH = 8;
303 private static final int MANUFACTURER_CODE_LENGTH = 8;
304
Derek Tan89e89d42014-07-08 17:00:10 -0700305 /**
Naina Nalluri8ff344d2019-09-17 14:10:30 -0700306 * Experiment flag to enable erase modem config on reset network, default value is false
307 */
308 public static final String RESET_NETWORK_ERASE_MODEM_CONFIG_ENABLED =
309 "reset_network_erase_modem_config_enabled";
310
311 /**
Shishir Agrawal566b7612013-10-28 14:41:00 -0700312 * A request object to use for transmitting data to an ICC.
313 */
314 private static final class IccAPDUArgument {
315 public int channel, cla, command, p1, p2, p3;
316 public String data;
317
318 public IccAPDUArgument(int channel, int cla, int command,
319 int p1, int p2, int p3, String data) {
320 this.channel = channel;
321 this.cla = cla;
322 this.command = command;
323 this.p1 = p1;
324 this.p2 = p2;
325 this.p3 = p3;
326 this.data = data;
327 }
328 }
329
330 /**
Shishir Agrawal77ba3172015-09-10 14:50:19 -0700331 * A request object to use for transmitting data to an ICC.
332 */
333 private static final class ManualNetworkSelectionArgument {
334 public OperatorInfo operatorInfo;
335 public boolean persistSelection;
336
337 public ManualNetworkSelectionArgument(OperatorInfo operatorInfo, boolean persistSelection) {
338 this.operatorInfo = operatorInfo;
339 this.persistSelection = persistSelection;
340 }
341 }
342
343 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700344 * A request object for use with {@link MainThreadHandler}. Requesters should wait() on the
345 * request after sending. The main thread will notify the request when it is complete.
346 */
347 private static final class MainThreadRequest {
348 /** The argument to use for the request */
349 public Object argument;
350 /** The result of the request that is run on the main thread */
351 public Object result;
Sanket Padawe56e75a32016-02-08 12:18:19 -0800352 // The subscriber id that this request applies to. Defaults to
353 // SubscriptionManager.INVALID_SUBSCRIPTION_ID
354 public Integer subId = SubscriptionManager.INVALID_SUBSCRIPTION_ID;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700355
Nathan Harold92bed182018-10-12 18:16:49 -0700356 // In cases where subId is unavailable, the caller needs to specify the phone.
357 public Phone phone;
358
vagdeviaf9a5b92018-08-15 16:01:53 -0700359 public WorkSource workSource;
360
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700361 public MainThreadRequest(Object argument) {
362 this.argument = argument;
363 }
Shishir Agrawal76d5da92014-11-09 16:17:25 -0800364
Nathan Harold92bed182018-10-12 18:16:49 -0700365 MainThreadRequest(Object argument, Phone phone, WorkSource workSource) {
366 this.argument = argument;
367 if (phone != null) {
368 this.phone = phone;
369 }
370 this.workSource = workSource;
371 }
372
vagdeviaf9a5b92018-08-15 16:01:53 -0700373 MainThreadRequest(Object argument, Integer subId, WorkSource workSource) {
Shishir Agrawal76d5da92014-11-09 16:17:25 -0800374 this.argument = argument;
Sanket Padawe56e75a32016-02-08 12:18:19 -0800375 if (subId != null) {
376 this.subId = subId;
377 }
vagdeviaf9a5b92018-08-15 16:01:53 -0700378 this.workSource = workSource;
Shishir Agrawal76d5da92014-11-09 16:17:25 -0800379 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700380 }
381
Sailesh Nepalcc0375f2013-11-13 09:15:18 -0800382 private static final class IncomingThirdPartyCallArgs {
383 public final ComponentName component;
384 public final String callId;
385 public final String callerDisplayName;
386
387 public IncomingThirdPartyCallArgs(ComponentName component, String callId,
388 String callerDisplayName) {
389 this.component = component;
390 this.callId = callId;
391 this.callerDisplayName = callerDisplayName;
392 }
393 }
394
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700395 /**
396 * A handler that processes messages on the main thread in the phone process. Since many
397 * of the Phone calls are not thread safe this is needed to shuttle the requests from the
398 * inbound binder threads to the main thread in the phone process. The Binder thread
399 * may provide a {@link MainThreadRequest} object in the msg.obj field that they are waiting
400 * on, which will be notified when the operation completes and will contain the result of the
401 * request.
402 *
403 * <p>If a MainThreadRequest object is provided in the msg.obj field,
404 * note that request.result must be set to something non-null for the calling thread to
405 * unblock.
406 */
407 private final class MainThreadHandler extends Handler {
408 @Override
409 public void handleMessage(Message msg) {
410 MainThreadRequest request;
411 Message onCompleted;
412 AsyncResult ar;
Shishir Agrawalc04d9752016-02-19 10:41:00 -0800413 UiccCard uiccCard;
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700414 IccAPDUArgument iccArgument;
Nazanin Bakhshif782e562018-12-11 15:15:39 -0800415 final Phone defaultPhone = getDefaultPhone();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700416
417 switch (msg.what) {
Pengquan Menga1bb6272018-09-06 09:59:22 -0700418 case CMD_HANDLE_USSD_REQUEST: {
419 request = (MainThreadRequest) msg.obj;
420 final Phone phone = getPhoneFromRequest(request);
421 Pair<String, ResultReceiver> ussdObject = (Pair) request.argument;
422 String ussdRequest = ussdObject.first;
423 ResultReceiver wrappedCallback = ussdObject.second;
Tyler Gunn65d45c22017-06-05 11:22:26 -0700424
Pengquan Menga1bb6272018-09-06 09:59:22 -0700425 if (!isUssdApiAllowed(request.subId)) {
426 // Carrier does not support use of this API, return failure.
427 Rlog.w(LOG_TAG, "handleUssdRequest: carrier does not support USSD apis.");
428 UssdResponse response = new UssdResponse(ussdRequest, null);
429 Bundle returnData = new Bundle();
430 returnData.putParcelable(TelephonyManager.USSD_RESPONSE, response);
431 wrappedCallback.send(TelephonyManager.USSD_RETURN_FAILURE, returnData);
Tyler Gunn65d45c22017-06-05 11:22:26 -0700432
Pengquan Menga1bb6272018-09-06 09:59:22 -0700433 request.result = true;
434 notifyRequester(request);
435 return;
436 }
Tyler Gunn65d45c22017-06-05 11:22:26 -0700437
Pengquan Menga1bb6272018-09-06 09:59:22 -0700438 try {
439 request.result = phone != null
440 ? phone.handleUssdRequest(ussdRequest, wrappedCallback) : false;
441 } catch (CallStateException cse) {
442 request.result = false;
443 }
444 // Wake up the requesting thread
445 notifyRequester(request);
446 break;
pkanwar32d516d2016-10-14 19:37:38 -0700447 }
448
Yorke Lee716f67e2015-06-17 15:39:16 -0700449 case CMD_HANDLE_PIN_MMI: {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700450 request = (MainThreadRequest) msg.obj;
Yorke Lee716f67e2015-06-17 15:39:16 -0700451 final Phone phone = getPhoneFromRequest(request);
452 request.result = phone != null ?
453 getPhoneFromRequest(request).handlePinMmi((String) request.argument)
454 : false;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700455 // Wake up the requesting thread
Pengquan Menga1bb6272018-09-06 09:59:22 -0700456 notifyRequester(request);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700457 break;
Yorke Lee716f67e2015-06-17 15:39:16 -0700458 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700459
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700460 case CMD_TRANSMIT_APDU_LOGICAL_CHANNEL:
Shishir Agrawal566b7612013-10-28 14:41:00 -0700461 request = (MainThreadRequest) msg.obj;
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700462 iccArgument = (IccAPDUArgument) request.argument;
Shishir Agrawalc04d9752016-02-19 10:41:00 -0800463 uiccCard = getUiccCardFromRequest(request);
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700464 if (uiccCard == null) {
465 loge("iccTransmitApduLogicalChannel: No UICC");
466 request.result = new IccIoResult(0x6F, 0, (byte[])null);
Pengquan Menga1bb6272018-09-06 09:59:22 -0700467 notifyRequester(request);
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700468 } else {
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700469 onCompleted = obtainMessage(EVENT_TRANSMIT_APDU_LOGICAL_CHANNEL_DONE,
470 request);
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700471 uiccCard.iccTransmitApduLogicalChannel(
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700472 iccArgument.channel, iccArgument.cla, iccArgument.command,
473 iccArgument.p1, iccArgument.p2, iccArgument.p3, iccArgument.data,
Shishir Agrawal566b7612013-10-28 14:41:00 -0700474 onCompleted);
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700475 }
Shishir Agrawal566b7612013-10-28 14:41:00 -0700476 break;
477
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700478 case EVENT_TRANSMIT_APDU_LOGICAL_CHANNEL_DONE:
Shishir Agrawal566b7612013-10-28 14:41:00 -0700479 ar = (AsyncResult) msg.obj;
480 request = (MainThreadRequest) ar.userObj;
481 if (ar.exception == null && ar.result != null) {
482 request.result = ar.result;
483 } else {
484 request.result = new IccIoResult(0x6F, 0, (byte[])null);
485 if (ar.result == null) {
486 loge("iccTransmitApduLogicalChannel: Empty response");
Jake Hambye994d462014-02-03 13:10:13 -0800487 } else if (ar.exception instanceof CommandException) {
Shishir Agrawal566b7612013-10-28 14:41:00 -0700488 loge("iccTransmitApduLogicalChannel: CommandException: " +
Jake Hambye994d462014-02-03 13:10:13 -0800489 ar.exception);
Shishir Agrawal566b7612013-10-28 14:41:00 -0700490 } else {
491 loge("iccTransmitApduLogicalChannel: Unknown exception");
492 }
493 }
Pengquan Menga1bb6272018-09-06 09:59:22 -0700494 notifyRequester(request);
Shishir Agrawal566b7612013-10-28 14:41:00 -0700495 break;
496
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700497 case CMD_TRANSMIT_APDU_BASIC_CHANNEL:
498 request = (MainThreadRequest) msg.obj;
499 iccArgument = (IccAPDUArgument) request.argument;
Shishir Agrawalc04d9752016-02-19 10:41:00 -0800500 uiccCard = getUiccCardFromRequest(request);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700501 if (uiccCard == null) {
502 loge("iccTransmitApduBasicChannel: No UICC");
503 request.result = new IccIoResult(0x6F, 0, (byte[])null);
Pengquan Menga1bb6272018-09-06 09:59:22 -0700504 notifyRequester(request);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700505 } else {
506 onCompleted = obtainMessage(EVENT_TRANSMIT_APDU_BASIC_CHANNEL_DONE,
507 request);
508 uiccCard.iccTransmitApduBasicChannel(
509 iccArgument.cla, iccArgument.command, iccArgument.p1, iccArgument.p2,
510 iccArgument.p3, iccArgument.data, onCompleted);
511 }
512 break;
513
514 case EVENT_TRANSMIT_APDU_BASIC_CHANNEL_DONE:
515 ar = (AsyncResult) msg.obj;
516 request = (MainThreadRequest) ar.userObj;
517 if (ar.exception == null && ar.result != null) {
518 request.result = ar.result;
519 } else {
520 request.result = new IccIoResult(0x6F, 0, (byte[])null);
521 if (ar.result == null) {
522 loge("iccTransmitApduBasicChannel: Empty response");
523 } else if (ar.exception instanceof CommandException) {
524 loge("iccTransmitApduBasicChannel: CommandException: " +
525 ar.exception);
526 } else {
527 loge("iccTransmitApduBasicChannel: Unknown exception");
528 }
529 }
Pengquan Menga1bb6272018-09-06 09:59:22 -0700530 notifyRequester(request);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700531 break;
532
533 case CMD_EXCHANGE_SIM_IO:
534 request = (MainThreadRequest) msg.obj;
535 iccArgument = (IccAPDUArgument) request.argument;
Shishir Agrawalc04d9752016-02-19 10:41:00 -0800536 uiccCard = getUiccCardFromRequest(request);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700537 if (uiccCard == null) {
538 loge("iccExchangeSimIO: No UICC");
539 request.result = new IccIoResult(0x6F, 0, (byte[])null);
Pengquan Menga1bb6272018-09-06 09:59:22 -0700540 notifyRequester(request);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700541 } else {
542 onCompleted = obtainMessage(EVENT_EXCHANGE_SIM_IO_DONE,
543 request);
544 uiccCard.iccExchangeSimIO(iccArgument.cla, /* fileID */
545 iccArgument.command, iccArgument.p1, iccArgument.p2, iccArgument.p3,
546 iccArgument.data, onCompleted);
547 }
548 break;
549
550 case EVENT_EXCHANGE_SIM_IO_DONE:
551 ar = (AsyncResult) msg.obj;
552 request = (MainThreadRequest) ar.userObj;
553 if (ar.exception == null && ar.result != null) {
554 request.result = ar.result;
555 } else {
556 request.result = new IccIoResult(0x6f, 0, (byte[])null);
557 }
Pengquan Menga1bb6272018-09-06 09:59:22 -0700558 notifyRequester(request);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700559 break;
560
Derek Tan4d5e5c12014-02-04 11:54:58 -0800561 case CMD_SEND_ENVELOPE:
562 request = (MainThreadRequest) msg.obj;
Shishir Agrawalc04d9752016-02-19 10:41:00 -0800563 uiccCard = getUiccCardFromRequest(request);
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700564 if (uiccCard == null) {
565 loge("sendEnvelopeWithStatus: No UICC");
566 request.result = new IccIoResult(0x6F, 0, (byte[])null);
Pengquan Menga1bb6272018-09-06 09:59:22 -0700567 notifyRequester(request);
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700568 } else {
569 onCompleted = obtainMessage(EVENT_SEND_ENVELOPE_DONE, request);
570 uiccCard.sendEnvelopeWithStatus((String)request.argument, onCompleted);
571 }
Derek Tan4d5e5c12014-02-04 11:54:58 -0800572 break;
573
574 case EVENT_SEND_ENVELOPE_DONE:
575 ar = (AsyncResult) msg.obj;
576 request = (MainThreadRequest) ar.userObj;
Shishir Agrawal9f9877d2014-03-14 09:36:27 -0700577 if (ar.exception == null && ar.result != null) {
578 request.result = ar.result;
Derek Tan4d5e5c12014-02-04 11:54:58 -0800579 } else {
Shishir Agrawal9f9877d2014-03-14 09:36:27 -0700580 request.result = new IccIoResult(0x6F, 0, (byte[])null);
581 if (ar.result == null) {
582 loge("sendEnvelopeWithStatus: Empty response");
583 } else if (ar.exception instanceof CommandException) {
584 loge("sendEnvelopeWithStatus: CommandException: " +
585 ar.exception);
586 } else {
587 loge("sendEnvelopeWithStatus: exception:" + ar.exception);
588 }
Derek Tan4d5e5c12014-02-04 11:54:58 -0800589 }
Pengquan Menga1bb6272018-09-06 09:59:22 -0700590 notifyRequester(request);
Derek Tan4d5e5c12014-02-04 11:54:58 -0800591 break;
592
Shishir Agrawal566b7612013-10-28 14:41:00 -0700593 case CMD_OPEN_CHANNEL:
594 request = (MainThreadRequest) msg.obj;
Shishir Agrawalc04d9752016-02-19 10:41:00 -0800595 uiccCard = getUiccCardFromRequest(request);
Ajay Nambid7454d32015-12-03 13:50:00 -0800596 Pair<String, Integer> openChannelArgs = (Pair<String, Integer>) request.argument;
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700597 if (uiccCard == null) {
598 loge("iccOpenLogicalChannel: No UICC");
Shishir Agrawalfc0492a2016-02-17 11:15:33 -0800599 request.result = new IccOpenLogicalChannelResponse(-1,
600 IccOpenLogicalChannelResponse.STATUS_MISSING_RESOURCE, null);
Pengquan Menga1bb6272018-09-06 09:59:22 -0700601 notifyRequester(request);
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700602 } else {
603 onCompleted = obtainMessage(EVENT_OPEN_CHANNEL_DONE, request);
Ajay Nambid7454d32015-12-03 13:50:00 -0800604 uiccCard.iccOpenLogicalChannel(openChannelArgs.first,
605 openChannelArgs.second, onCompleted);
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700606 }
Shishir Agrawal566b7612013-10-28 14:41:00 -0700607 break;
608
609 case EVENT_OPEN_CHANNEL_DONE:
610 ar = (AsyncResult) msg.obj;
611 request = (MainThreadRequest) ar.userObj;
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700612 IccOpenLogicalChannelResponse openChannelResp;
Shishir Agrawal566b7612013-10-28 14:41:00 -0700613 if (ar.exception == null && ar.result != null) {
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700614 int[] result = (int[]) ar.result;
615 int channelId = result[0];
616 byte[] selectResponse = null;
617 if (result.length > 1) {
618 selectResponse = new byte[result.length - 1];
619 for (int i = 1; i < result.length; ++i) {
620 selectResponse[i - 1] = (byte) result[i];
621 }
622 }
623 openChannelResp = new IccOpenLogicalChannelResponse(channelId,
Shishir Agrawal527e8bf2014-08-25 08:54:56 -0700624 IccOpenLogicalChannelResponse.STATUS_NO_ERROR, selectResponse);
Shishir Agrawal566b7612013-10-28 14:41:00 -0700625 } else {
Shishir Agrawal566b7612013-10-28 14:41:00 -0700626 if (ar.result == null) {
627 loge("iccOpenLogicalChannel: Empty response");
Shishir Agrawal566b7612013-10-28 14:41:00 -0700628 }
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700629 if (ar.exception != null) {
630 loge("iccOpenLogicalChannel: Exception: " + ar.exception);
631 }
632
Shishir Agrawal527e8bf2014-08-25 08:54:56 -0700633 int errorCode = IccOpenLogicalChannelResponse.STATUS_UNKNOWN_ERROR;
Junda Liua754ba12015-05-20 01:17:52 -0700634 if (ar.exception instanceof CommandException) {
635 CommandException.Error error =
636 ((CommandException) (ar.exception)).getCommandError();
637 if (error == CommandException.Error.MISSING_RESOURCE) {
Shishir Agrawal527e8bf2014-08-25 08:54:56 -0700638 errorCode = IccOpenLogicalChannelResponse.STATUS_MISSING_RESOURCE;
Junda Liua754ba12015-05-20 01:17:52 -0700639 } else if (error == CommandException.Error.NO_SUCH_ELEMENT) {
Shishir Agrawal527e8bf2014-08-25 08:54:56 -0700640 errorCode = IccOpenLogicalChannelResponse.STATUS_NO_SUCH_ELEMENT;
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700641 }
642 }
643 openChannelResp = new IccOpenLogicalChannelResponse(
644 IccOpenLogicalChannelResponse.INVALID_CHANNEL, errorCode, null);
Shishir Agrawal566b7612013-10-28 14:41:00 -0700645 }
Shishir Agrawal82c8a462014-07-31 18:13:17 -0700646 request.result = openChannelResp;
Pengquan Menga1bb6272018-09-06 09:59:22 -0700647 notifyRequester(request);
Shishir Agrawal566b7612013-10-28 14:41:00 -0700648 break;
649
650 case CMD_CLOSE_CHANNEL:
651 request = (MainThreadRequest) msg.obj;
Shishir Agrawalc04d9752016-02-19 10:41:00 -0800652 uiccCard = getUiccCardFromRequest(request);
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700653 if (uiccCard == null) {
654 loge("iccCloseLogicalChannel: No UICC");
Yoshiaki Naka2e29d822016-09-02 19:27:39 +0900655 request.result = false;
Pengquan Menga1bb6272018-09-06 09:59:22 -0700656 notifyRequester(request);
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700657 } else {
658 onCompleted = obtainMessage(EVENT_CLOSE_CHANNEL_DONE, request);
659 uiccCard.iccCloseLogicalChannel((Integer) request.argument, onCompleted);
660 }
Shishir Agrawal566b7612013-10-28 14:41:00 -0700661 break;
662
663 case EVENT_CLOSE_CHANNEL_DONE:
Jake Hambye994d462014-02-03 13:10:13 -0800664 handleNullReturnEvent(msg, "iccCloseLogicalChannel");
665 break;
666
667 case CMD_NV_READ_ITEM:
668 request = (MainThreadRequest) msg.obj;
669 onCompleted = obtainMessage(EVENT_NV_READ_ITEM_DONE, request);
Nazanin Bakhshif782e562018-12-11 15:15:39 -0800670 defaultPhone.nvReadItem((Integer) request.argument, onCompleted,
671 request.workSource);
Jake Hambye994d462014-02-03 13:10:13 -0800672 break;
673
674 case EVENT_NV_READ_ITEM_DONE:
Shishir Agrawal566b7612013-10-28 14:41:00 -0700675 ar = (AsyncResult) msg.obj;
676 request = (MainThreadRequest) ar.userObj;
Jake Hambye994d462014-02-03 13:10:13 -0800677 if (ar.exception == null && ar.result != null) {
678 request.result = ar.result; // String
Shishir Agrawal566b7612013-10-28 14:41:00 -0700679 } else {
Jake Hambye994d462014-02-03 13:10:13 -0800680 request.result = "";
681 if (ar.result == null) {
682 loge("nvReadItem: Empty response");
683 } else if (ar.exception instanceof CommandException) {
684 loge("nvReadItem: CommandException: " +
685 ar.exception);
Shishir Agrawal566b7612013-10-28 14:41:00 -0700686 } else {
Jake Hambye994d462014-02-03 13:10:13 -0800687 loge("nvReadItem: Unknown exception");
Shishir Agrawal566b7612013-10-28 14:41:00 -0700688 }
689 }
Pengquan Menga1bb6272018-09-06 09:59:22 -0700690 notifyRequester(request);
Shishir Agrawal566b7612013-10-28 14:41:00 -0700691 break;
692
Jake Hambye994d462014-02-03 13:10:13 -0800693 case CMD_NV_WRITE_ITEM:
694 request = (MainThreadRequest) msg.obj;
695 onCompleted = obtainMessage(EVENT_NV_WRITE_ITEM_DONE, request);
696 Pair<Integer, String> idValue = (Pair<Integer, String>) request.argument;
Nazanin Bakhshif782e562018-12-11 15:15:39 -0800697 defaultPhone.nvWriteItem(idValue.first, idValue.second, onCompleted,
vagdeviaf9a5b92018-08-15 16:01:53 -0700698 request.workSource);
Jake Hambye994d462014-02-03 13:10:13 -0800699 break;
700
701 case EVENT_NV_WRITE_ITEM_DONE:
702 handleNullReturnEvent(msg, "nvWriteItem");
703 break;
704
705 case CMD_NV_WRITE_CDMA_PRL:
706 request = (MainThreadRequest) msg.obj;
707 onCompleted = obtainMessage(EVENT_NV_WRITE_CDMA_PRL_DONE, request);
Nazanin Bakhshif782e562018-12-11 15:15:39 -0800708 defaultPhone.nvWriteCdmaPrl((byte[]) request.argument, onCompleted);
Jake Hambye994d462014-02-03 13:10:13 -0800709 break;
710
711 case EVENT_NV_WRITE_CDMA_PRL_DONE:
712 handleNullReturnEvent(msg, "nvWriteCdmaPrl");
713 break;
714
chen xu6dac5ab2018-10-26 17:39:23 -0700715 case CMD_RESET_MODEM_CONFIG:
Jake Hambye994d462014-02-03 13:10:13 -0800716 request = (MainThreadRequest) msg.obj;
chen xu6dac5ab2018-10-26 17:39:23 -0700717 onCompleted = obtainMessage(EVENT_RESET_MODEM_CONFIG_DONE, request);
Nazanin Bakhshif782e562018-12-11 15:15:39 -0800718 defaultPhone.resetModemConfig(onCompleted);
Jake Hambye994d462014-02-03 13:10:13 -0800719 break;
720
chen xu6dac5ab2018-10-26 17:39:23 -0700721 case EVENT_RESET_MODEM_CONFIG_DONE:
722 handleNullReturnEvent(msg, "resetModemConfig");
Jake Hambye994d462014-02-03 13:10:13 -0800723 break;
724
Jake Hamby7c27be32014-03-03 13:25:59 -0800725 case CMD_GET_PREFERRED_NETWORK_TYPE:
726 request = (MainThreadRequest) msg.obj;
727 onCompleted = obtainMessage(EVENT_GET_PREFERRED_NETWORK_TYPE_DONE, request);
Stuart Scott54788802015-03-30 13:18:01 -0700728 getPhoneFromRequest(request).getPreferredNetworkType(onCompleted);
Jake Hamby7c27be32014-03-03 13:25:59 -0800729 break;
730
731 case EVENT_GET_PREFERRED_NETWORK_TYPE_DONE:
732 ar = (AsyncResult) msg.obj;
733 request = (MainThreadRequest) ar.userObj;
734 if (ar.exception == null && ar.result != null) {
735 request.result = ar.result; // Integer
736 } else {
Sanket Padawecfc2d352016-01-05 19:52:14 -0800737 request.result = null;
Jake Hamby7c27be32014-03-03 13:25:59 -0800738 if (ar.result == null) {
739 loge("getPreferredNetworkType: Empty response");
740 } else if (ar.exception instanceof CommandException) {
741 loge("getPreferredNetworkType: CommandException: " +
742 ar.exception);
743 } else {
744 loge("getPreferredNetworkType: Unknown exception");
745 }
746 }
Pengquan Menga1bb6272018-09-06 09:59:22 -0700747 notifyRequester(request);
Jake Hamby7c27be32014-03-03 13:25:59 -0800748 break;
749
750 case CMD_SET_PREFERRED_NETWORK_TYPE:
751 request = (MainThreadRequest) msg.obj;
752 onCompleted = obtainMessage(EVENT_SET_PREFERRED_NETWORK_TYPE_DONE, request);
753 int networkType = (Integer) request.argument;
Stuart Scott54788802015-03-30 13:18:01 -0700754 getPhoneFromRequest(request).setPreferredNetworkType(networkType, onCompleted);
Jake Hamby7c27be32014-03-03 13:25:59 -0800755 break;
756
757 case EVENT_SET_PREFERRED_NETWORK_TYPE_DONE:
758 handleNullReturnEvent(msg, "setPreferredNetworkType");
759 break;
760
Shuo Qian850e4d6a2018-04-25 21:02:08 +0000761 case CMD_INVOKE_OEM_RIL_REQUEST_RAW:
762 request = (MainThreadRequest)msg.obj;
763 onCompleted = obtainMessage(EVENT_INVOKE_OEM_RIL_REQUEST_RAW_DONE, request);
Nazanin Bakhshif782e562018-12-11 15:15:39 -0800764 defaultPhone.invokeOemRilRequestRaw((byte[]) request.argument, onCompleted);
Shuo Qian850e4d6a2018-04-25 21:02:08 +0000765 break;
766
767 case EVENT_INVOKE_OEM_RIL_REQUEST_RAW_DONE:
768 ar = (AsyncResult)msg.obj;
769 request = (MainThreadRequest)ar.userObj;
770 request.result = ar;
Pengquan Menga1bb6272018-09-06 09:59:22 -0700771 notifyRequester(request);
Shuo Qian850e4d6a2018-04-25 21:02:08 +0000772 break;
773
Shishir Agrawal76d5da92014-11-09 16:17:25 -0800774 case CMD_SET_VOICEMAIL_NUMBER:
775 request = (MainThreadRequest) msg.obj;
776 onCompleted = obtainMessage(EVENT_SET_VOICEMAIL_NUMBER_DONE, request);
777 Pair<String, String> tagNum = (Pair<String, String>) request.argument;
Stuart Scott584921c2015-01-15 17:10:34 -0800778 getPhoneFromRequest(request).setVoiceMailNumber(tagNum.first, tagNum.second,
779 onCompleted);
Shishir Agrawal76d5da92014-11-09 16:17:25 -0800780 break;
781
782 case EVENT_SET_VOICEMAIL_NUMBER_DONE:
783 handleNullReturnEvent(msg, "setVoicemailNumber");
784 break;
785
Stuart Scott54788802015-03-30 13:18:01 -0700786 case CMD_SET_NETWORK_SELECTION_MODE_AUTOMATIC:
787 request = (MainThreadRequest) msg.obj;
788 onCompleted = obtainMessage(EVENT_SET_NETWORK_SELECTION_MODE_AUTOMATIC_DONE,
789 request);
790 getPhoneFromRequest(request).setNetworkSelectionModeAutomatic(onCompleted);
791 break;
792
793 case EVENT_SET_NETWORK_SELECTION_MODE_AUTOMATIC_DONE:
794 handleNullReturnEvent(msg, "setNetworkSelectionModeAutomatic");
795 break;
796
Shishir Agrawal302c8692015-06-19 13:49:39 -0700797 case CMD_PERFORM_NETWORK_SCAN:
798 request = (MainThreadRequest) msg.obj;
799 onCompleted = obtainMessage(EVENT_PERFORM_NETWORK_SCAN_DONE, request);
800 getPhoneFromRequest(request).getAvailableNetworks(onCompleted);
801 break;
802
803 case EVENT_PERFORM_NETWORK_SCAN_DONE:
804 ar = (AsyncResult) msg.obj;
805 request = (MainThreadRequest) ar.userObj;
806 CellNetworkScanResult cellScanResult;
807 if (ar.exception == null && ar.result != null) {
808 cellScanResult = new CellNetworkScanResult(
809 CellNetworkScanResult.STATUS_SUCCESS,
810 (List<OperatorInfo>) ar.result);
811 } else {
812 if (ar.result == null) {
813 loge("getCellNetworkScanResults: Empty response");
814 }
815 if (ar.exception != null) {
816 loge("getCellNetworkScanResults: Exception: " + ar.exception);
817 }
818 int errorCode = CellNetworkScanResult.STATUS_UNKNOWN_ERROR;
819 if (ar.exception instanceof CommandException) {
820 CommandException.Error error =
821 ((CommandException) (ar.exception)).getCommandError();
822 if (error == CommandException.Error.RADIO_NOT_AVAILABLE) {
823 errorCode = CellNetworkScanResult.STATUS_RADIO_NOT_AVAILABLE;
824 } else if (error == CommandException.Error.GENERIC_FAILURE) {
825 errorCode = CellNetworkScanResult.STATUS_RADIO_GENERIC_FAILURE;
826 }
827 }
828 cellScanResult = new CellNetworkScanResult(errorCode, null);
829 }
830 request.result = cellScanResult;
Pengquan Menga1bb6272018-09-06 09:59:22 -0700831 notifyRequester(request);
Shishir Agrawal302c8692015-06-19 13:49:39 -0700832 break;
833
834 case CMD_SET_NETWORK_SELECTION_MODE_MANUAL:
835 request = (MainThreadRequest) msg.obj;
Shishir Agrawal77ba3172015-09-10 14:50:19 -0700836 ManualNetworkSelectionArgument selArg =
837 (ManualNetworkSelectionArgument) request.argument;
Shishir Agrawal302c8692015-06-19 13:49:39 -0700838 onCompleted = obtainMessage(EVENT_SET_NETWORK_SELECTION_MODE_MANUAL_DONE,
839 request);
Shishir Agrawal77ba3172015-09-10 14:50:19 -0700840 getPhoneFromRequest(request).selectNetworkManually(selArg.operatorInfo,
841 selArg.persistSelection, onCompleted);
Shishir Agrawal302c8692015-06-19 13:49:39 -0700842 break;
843
844 case EVENT_SET_NETWORK_SELECTION_MODE_MANUAL_DONE:
Pengquan Menge3d01e22018-09-20 15:25:35 -0700845 ar = (AsyncResult) msg.obj;
846 request = (MainThreadRequest) ar.userObj;
847 if (ar.exception == null) {
848 request.result = true;
849 } else {
850 request.result = false;
851 loge("setNetworkSelectionModeManual " + ar.exception);
852 }
853 notifyRequester(request);
854 mApp.onNetworkSelectionChanged(request.subId);
Shishir Agrawal302c8692015-06-19 13:49:39 -0700855 break;
856
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -0700857 case CMD_GET_MODEM_ACTIVITY_INFO:
858 request = (MainThreadRequest) msg.obj;
859 onCompleted = obtainMessage(EVENT_GET_MODEM_ACTIVITY_INFO_DONE, request);
James Mattisab947702019-04-03 14:18:34 -0700860 if (defaultPhone != null) {
861 defaultPhone.getModemActivityInfo(onCompleted, request.workSource);
862 }
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -0700863 break;
864
865 case EVENT_GET_MODEM_ACTIVITY_INFO_DONE:
866 ar = (AsyncResult) msg.obj;
867 request = (MainThreadRequest) ar.userObj;
868 if (ar.exception == null && ar.result != null) {
869 request.result = ar.result;
870 } else {
871 if (ar.result == null) {
872 loge("queryModemActivityInfo: Empty response");
873 } else if (ar.exception instanceof CommandException) {
874 loge("queryModemActivityInfo: CommandException: " +
875 ar.exception);
876 } else {
877 loge("queryModemActivityInfo: Unknown exception");
878 }
879 }
Amit Mahajand4766222016-01-28 15:28:28 -0800880 // Result cannot be null. Return ModemActivityInfo with all fields set to 0.
881 if (request.result == null) {
Chen Xu13851032019-09-10 18:49:52 -0700882 request.result = new ModemActivityInfo(0, 0, 0, null, 0);
Amit Mahajand4766222016-01-28 15:28:28 -0800883 }
Pengquan Menga1bb6272018-09-06 09:59:22 -0700884 notifyRequester(request);
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -0700885 break;
886
Meng Wang1a7c35a2016-05-05 20:56:15 -0700887 case CMD_SET_ALLOWED_CARRIERS:
888 request = (MainThreadRequest) msg.obj;
Michele Berionne482f8202018-11-27 18:57:59 -0800889 CarrierRestrictionRules argument =
890 (CarrierRestrictionRules) request.argument;
Meng Wang1a7c35a2016-05-05 20:56:15 -0700891 onCompleted = obtainMessage(EVENT_SET_ALLOWED_CARRIERS_DONE, request);
Michele Berionne482f8202018-11-27 18:57:59 -0800892 defaultPhone.setAllowedCarriers(argument, onCompleted, request.workSource);
Meng Wang1a7c35a2016-05-05 20:56:15 -0700893 break;
894
895 case EVENT_SET_ALLOWED_CARRIERS_DONE:
896 ar = (AsyncResult) msg.obj;
897 request = (MainThreadRequest) ar.userObj;
898 if (ar.exception == null && ar.result != null) {
899 request.result = ar.result;
900 } else {
Michele Berionne482f8202018-11-27 18:57:59 -0800901 request.result = TelephonyManager.SET_CARRIER_RESTRICTION_ERROR;
902 if (ar.exception instanceof CommandException) {
903 loge("setAllowedCarriers: CommandException: " + ar.exception);
904 CommandException.Error error =
905 ((CommandException) (ar.exception)).getCommandError();
906 if (error == CommandException.Error.REQUEST_NOT_SUPPORTED) {
907 request.result =
908 TelephonyManager.SET_CARRIER_RESTRICTION_NOT_SUPPORTED;
909 }
Meng Wang1a7c35a2016-05-05 20:56:15 -0700910 } else {
911 loge("setAllowedCarriers: Unknown exception");
912 }
913 }
Pengquan Menga1bb6272018-09-06 09:59:22 -0700914 notifyRequester(request);
Meng Wang1a7c35a2016-05-05 20:56:15 -0700915 break;
916
917 case CMD_GET_ALLOWED_CARRIERS:
918 request = (MainThreadRequest) msg.obj;
919 onCompleted = obtainMessage(EVENT_GET_ALLOWED_CARRIERS_DONE, request);
Nazanin Bakhshif782e562018-12-11 15:15:39 -0800920 defaultPhone.getAllowedCarriers(onCompleted, request.workSource);
Meng Wang1a7c35a2016-05-05 20:56:15 -0700921 break;
922
923 case EVENT_GET_ALLOWED_CARRIERS_DONE:
924 ar = (AsyncResult) msg.obj;
925 request = (MainThreadRequest) ar.userObj;
926 if (ar.exception == null && ar.result != null) {
927 request.result = ar.result;
928 } else {
Michele Berionne482f8202018-11-27 18:57:59 -0800929 request.result = new IllegalStateException(
930 "Failed to get carrier restrictions");
Meng Wang1a7c35a2016-05-05 20:56:15 -0700931 if (ar.result == null) {
932 loge("getAllowedCarriers: Empty response");
933 } else if (ar.exception instanceof CommandException) {
934 loge("getAllowedCarriers: CommandException: " +
935 ar.exception);
936 } else {
937 loge("getAllowedCarriers: Unknown exception");
938 }
939 }
Pengquan Menga1bb6272018-09-06 09:59:22 -0700940 notifyRequester(request);
Meng Wang1a7c35a2016-05-05 20:56:15 -0700941 break;
942
Nathan Haroldb3014052017-01-25 15:57:32 -0800943 case EVENT_GET_FORBIDDEN_PLMNS_DONE:
944 ar = (AsyncResult) msg.obj;
945 request = (MainThreadRequest) ar.userObj;
946 if (ar.exception == null && ar.result != null) {
947 request.result = ar.result;
948 } else {
949 request.result = new IllegalArgumentException(
950 "Failed to retrieve Forbidden Plmns");
951 if (ar.result == null) {
952 loge("getForbiddenPlmns: Empty response");
953 } else {
954 loge("getForbiddenPlmns: Unknown exception");
955 }
956 }
Pengquan Menga1bb6272018-09-06 09:59:22 -0700957 notifyRequester(request);
Nathan Haroldb3014052017-01-25 15:57:32 -0800958 break;
959
960 case CMD_GET_FORBIDDEN_PLMNS:
961 request = (MainThreadRequest) msg.obj;
962 uiccCard = getUiccCardFromRequest(request);
963 if (uiccCard == null) {
964 loge("getForbiddenPlmns() UiccCard is null");
965 request.result = new IllegalArgumentException(
966 "getForbiddenPlmns() UiccCard is null");
Pengquan Menga1bb6272018-09-06 09:59:22 -0700967 notifyRequester(request);
Nathan Haroldb3014052017-01-25 15:57:32 -0800968 break;
969 }
970 Integer appType = (Integer) request.argument;
971 UiccCardApplication uiccApp = uiccCard.getApplicationByType(appType);
972 if (uiccApp == null) {
973 loge("getForbiddenPlmns() no app with specified type -- "
974 + appType);
975 request.result = new IllegalArgumentException("Failed to get UICC App");
Pengquan Menga1bb6272018-09-06 09:59:22 -0700976 notifyRequester(request);
Nathan Haroldb3014052017-01-25 15:57:32 -0800977 break;
978 } else {
979 if (DBG) logv("getForbiddenPlmns() found app " + uiccApp.getAid()
980 + " specified type -- " + appType);
981 }
982 onCompleted = obtainMessage(EVENT_GET_FORBIDDEN_PLMNS_DONE, request);
983 ((SIMRecords) uiccApp.getIccRecords()).getForbiddenPlmns(
984 onCompleted);
985 break;
986
Holly Jiuyu Sun01c47ad2018-01-24 17:56:33 +0000987 case CMD_SWITCH_SLOTS:
988 request = (MainThreadRequest) msg.obj;
989 int[] physicalSlots = (int[]) request.argument;
990 onCompleted = obtainMessage(EVENT_SWITCH_SLOTS_DONE, request);
991 UiccController.getInstance().switchSlots(physicalSlots, onCompleted);
992 break;
993
994 case EVENT_SWITCH_SLOTS_DONE:
995 ar = (AsyncResult) msg.obj;
996 request = (MainThreadRequest) ar.userObj;
997 request.result = (ar.exception == null);
Pengquan Menga1bb6272018-09-06 09:59:22 -0700998 notifyRequester(request);
999 break;
1000 case CMD_GET_NETWORK_SELECTION_MODE:
1001 request = (MainThreadRequest) msg.obj;
1002 onCompleted = obtainMessage(EVENT_GET_NETWORK_SELECTION_MODE_DONE, request);
1003 getPhoneFromRequest(request).getNetworkSelectionMode(onCompleted);
1004 break;
1005
1006 case EVENT_GET_NETWORK_SELECTION_MODE_DONE:
1007 ar = (AsyncResult) msg.obj;
1008 request = (MainThreadRequest) ar.userObj;
1009 if (ar.exception != null) {
1010 request.result = TelephonyManager.NETWORK_SELECTION_MODE_UNKNOWN;
1011 } else {
1012 int mode = ((int[]) ar.result)[0];
1013 if (mode == 0) {
1014 request.result = TelephonyManager.NETWORK_SELECTION_MODE_AUTO;
1015 } else {
1016 request.result = TelephonyManager.NETWORK_SELECTION_MODE_MANUAL;
1017 }
Holly Jiuyu Sun01c47ad2018-01-24 17:56:33 +00001018 }
Pengquan Menga1bb6272018-09-06 09:59:22 -07001019 notifyRequester(request);
1020 break;
1021 case CMD_GET_CDMA_ROAMING_MODE:
1022 request = (MainThreadRequest) msg.obj;
1023 onCompleted = obtainMessage(EVENT_GET_CDMA_ROAMING_MODE_DONE, request);
1024 getPhoneFromRequest(request).queryCdmaRoamingPreference(onCompleted);
1025 break;
1026 case EVENT_GET_CDMA_ROAMING_MODE_DONE:
1027 ar = (AsyncResult) msg.obj;
1028 request = (MainThreadRequest) ar.userObj;
1029 if (ar.exception != null) {
1030 request.result = TelephonyManager.CDMA_ROAMING_MODE_RADIO_DEFAULT;
1031 } else {
1032 request.result = ((int[]) ar.result)[0];
1033 }
1034 notifyRequester(request);
1035 break;
1036 case CMD_SET_CDMA_ROAMING_MODE:
1037 request = (MainThreadRequest) msg.obj;
1038 onCompleted = obtainMessage(EVENT_SET_CDMA_ROAMING_MODE_DONE, request);
1039 int mode = (int) request.argument;
1040 getPhoneFromRequest(request).setCdmaRoamingPreference(mode, onCompleted);
1041 break;
1042 case EVENT_SET_CDMA_ROAMING_MODE_DONE:
1043 ar = (AsyncResult) msg.obj;
1044 request = (MainThreadRequest) ar.userObj;
1045 request.result = ar.exception == null;
1046 notifyRequester(request);
1047 break;
1048 case CMD_SET_CDMA_SUBSCRIPTION_MODE:
1049 request = (MainThreadRequest) msg.obj;
1050 onCompleted = obtainMessage(EVENT_SET_CDMA_SUBSCRIPTION_MODE_DONE, request);
1051 int subscriptionMode = (int) request.argument;
1052 getPhoneFromRequest(request).setCdmaSubscription(subscriptionMode, onCompleted);
1053 break;
1054 case EVENT_SET_CDMA_SUBSCRIPTION_MODE_DONE:
1055 ar = (AsyncResult) msg.obj;
1056 request = (MainThreadRequest) ar.userObj;
1057 request.result = ar.exception == null;
1058 notifyRequester(request);
Holly Jiuyu Sun01c47ad2018-01-24 17:56:33 +00001059 break;
Nathan Harold3ff88932018-08-14 10:19:49 -07001060 case CMD_GET_ALL_CELL_INFO:
1061 request = (MainThreadRequest) msg.obj;
Nathan Harold3ff88932018-08-14 10:19:49 -07001062 onCompleted = obtainMessage(EVENT_GET_ALL_CELL_INFO_DONE, request);
Nathan Harold92bed182018-10-12 18:16:49 -07001063 request.phone.requestCellInfoUpdate(request.workSource, onCompleted);
Nathan Harold3ff88932018-08-14 10:19:49 -07001064 break;
Nathan Harold3ff88932018-08-14 10:19:49 -07001065 case EVENT_GET_ALL_CELL_INFO_DONE:
1066 ar = (AsyncResult) msg.obj;
1067 request = (MainThreadRequest) ar.userObj;
Nathan Harold8d0f1742018-10-02 12:14:47 -07001068 // If a timeout occurs, the response will be null
1069 request.result = (ar.exception == null && ar.result != null)
1070 ? ar.result : new ArrayList<CellInfo>();
Nathan Harold3ff88932018-08-14 10:19:49 -07001071 synchronized (request) {
1072 request.notifyAll();
1073 }
1074 break;
Nathan Haroldfa8da0f2018-09-27 18:51:29 -07001075 case CMD_REQUEST_CELL_INFO_UPDATE:
1076 request = (MainThreadRequest) msg.obj;
1077 request.phone.requestCellInfoUpdate(request.workSource,
1078 obtainMessage(EVENT_REQUEST_CELL_INFO_UPDATE_DONE, request));
1079 break;
1080 case EVENT_REQUEST_CELL_INFO_UPDATE_DONE:
1081 ar = (AsyncResult) msg.obj;
1082 request = (MainThreadRequest) ar.userObj;
1083 ICellInfoCallback cb = (ICellInfoCallback) request.argument;
1084 try {
1085 if (ar.exception != null) {
Nathan Haroldfa8da0f2018-09-27 18:51:29 -07001086 Log.e(LOG_TAG, "Exception retrieving CellInfo=" + ar.exception);
Meng Wang6c08ecd2019-09-30 17:13:54 -07001087 cb.onError(
1088 TelephonyManager.CellInfoCallback.ERROR_MODEM_ERROR,
1089 ar.exception.getClass().getName(),
1090 ar.exception.toString());
Nathan Haroldfa8da0f2018-09-27 18:51:29 -07001091 } else if (ar.result == null) {
Nathan Haroldfa8da0f2018-09-27 18:51:29 -07001092 Log.w(LOG_TAG, "Timeout Waiting for CellInfo!");
Meng Wang6c08ecd2019-09-30 17:13:54 -07001093 cb.onError(TelephonyManager.CellInfoCallback.ERROR_TIMEOUT, null, null);
Nathan Haroldfa8da0f2018-09-27 18:51:29 -07001094 } else {
1095 // use the result as returned
1096 cb.onCellInfo((List<CellInfo>) ar.result);
1097 }
1098 } catch (RemoteException re) {
1099 Log.w(LOG_TAG, "Discarded CellInfo due to Callback RemoteException");
1100 }
1101 break;
1102 case CMD_GET_CELL_LOCATION:
Nathan Harold3ff88932018-08-14 10:19:49 -07001103 request = (MainThreadRequest) msg.obj;
1104 WorkSource ws = (WorkSource) request.argument;
1105 Phone phone = getPhoneFromRequest(request);
1106 phone.getCellLocation(ws, obtainMessage(EVENT_GET_CELL_LOCATION_DONE, request));
1107 break;
Nathan Haroldfa8da0f2018-09-27 18:51:29 -07001108 case EVENT_GET_CELL_LOCATION_DONE:
Nathan Harold3ff88932018-08-14 10:19:49 -07001109 ar = (AsyncResult) msg.obj;
1110 request = (MainThreadRequest) ar.userObj;
1111 if (ar.exception == null) {
1112 request.result = ar.result;
1113 } else {
Nathan Haroldfa8da0f2018-09-27 18:51:29 -07001114 phone = getPhoneFromRequest(request);
Nathan Harold3ff88932018-08-14 10:19:49 -07001115 request.result = (phone.getPhoneType() == PhoneConstants.PHONE_TYPE_CDMA)
1116 ? new CdmaCellLocation() : new GsmCellLocation();
1117 }
1118
1119 synchronized (request) {
1120 request.notifyAll();
1121 }
1122 break;
chen xu6dac5ab2018-10-26 17:39:23 -07001123 case CMD_MODEM_REBOOT:
1124 request = (MainThreadRequest) msg.obj;
1125 onCompleted = obtainMessage(EVENT_RESET_MODEM_CONFIG_DONE, request);
Nazanin Bakhshif782e562018-12-11 15:15:39 -08001126 defaultPhone.rebootModem(onCompleted);
chen xu6dac5ab2018-10-26 17:39:23 -07001127 break;
chen xu6dac5ab2018-10-26 17:39:23 -07001128 case EVENT_CMD_MODEM_REBOOT_DONE:
1129 handleNullReturnEvent(msg, "rebootModem");
1130 break;
Malcolm Chen8e4ed912019-01-15 20:22:16 -08001131 case CMD_REQUEST_ENABLE_MODEM:
1132 request = (MainThreadRequest) msg.obj;
1133 boolean enable = (boolean) request.argument;
1134 onCompleted = obtainMessage(EVENT_ENABLE_MODEM_DONE, request);
Nazanin Bakhshi33d584b2019-02-27 10:44:32 -08001135 onCompleted.arg1 = enable ? 1 : 0;
Malcolm Chen8e4ed912019-01-15 20:22:16 -08001136 PhoneConfigurationManager.getInstance()
1137 .enablePhone(request.phone, enable, onCompleted);
1138 break;
1139 case EVENT_ENABLE_MODEM_DONE:
1140 ar = (AsyncResult) msg.obj;
1141 request = (MainThreadRequest) ar.userObj;
1142 request.result = (ar.exception == null);
Nazanin Bakhshif71371d2019-04-29 17:29:44 -07001143 int phoneId = request.phone.getPhoneId();
Nazanin Bakhshi33d584b2019-02-27 10:44:32 -08001144 //update the cache as modem status has changed
Nazanin Bakhshif71371d2019-04-29 17:29:44 -07001145 if ((boolean) request.result) {
1146 mPhoneConfigurationManager.addToPhoneStatusCache(phoneId, msg.arg1 == 1);
1147 updateModemStateMetrics();
1148 } else {
1149 Log.e(LOG_TAG, msg.what + " failure. Not updating modem status."
1150 + ar.exception);
1151 }
1152 notifyRequester(request);
1153 break;
1154 case CMD_GET_MODEM_STATUS:
1155 request = (MainThreadRequest) msg.obj;
1156 onCompleted = obtainMessage(EVENT_GET_MODEM_STATUS_DONE, request);
1157 PhoneConfigurationManager.getInstance()
1158 .getPhoneStatusFromModem(request.phone, onCompleted);
1159 break;
1160 case EVENT_GET_MODEM_STATUS_DONE:
1161 ar = (AsyncResult) msg.obj;
1162 request = (MainThreadRequest) ar.userObj;
1163 int id = request.phone.getPhoneId();
1164 if (ar.exception == null && ar.result != null) {
1165 request.result = ar.result;
1166 //update the cache as modem status has changed
1167 mPhoneConfigurationManager.addToPhoneStatusCache(id,
1168 (boolean) request.result);
1169 } else {
1170 // Return true if modem status cannot be retrieved. For most cases,
1171 // modem status is on. And for older version modems, GET_MODEM_STATUS
1172 // and disable modem are not supported. Modem is always on.
1173 // TODO: this should be fixed in R to support a third
1174 // status UNKNOWN b/131631629
1175 request.result = true;
1176 Log.e(LOG_TAG, msg.what + " failure. Not updating modem status."
1177 + ar.exception);
1178 }
Malcolm Chen8e4ed912019-01-15 20:22:16 -08001179 notifyRequester(request);
1180 break;
yincheng zhaod698b842019-09-06 17:06:54 -07001181 case EVENT_SET_FORBIDDEN_PLMNS_DONE:
1182 ar = (AsyncResult) msg.obj;
1183 request = (MainThreadRequest) ar.userObj;
1184 if (ar.exception == null && ar.result != null) {
1185 request.result = ar.result;
1186 } else {
1187 request.result = -1;
1188 loge("Failed to set Forbidden Plmns");
1189 if (ar.result == null) {
1190 loge("setForbidenPlmns: Empty response");
1191 } else if (ar.exception != null) {
1192 loge("setForbiddenPlmns: Exception: " + ar.exception);
1193 request.result = -1;
1194 } else {
1195 loge("setForbiddenPlmns: Unknown exception");
1196 }
1197 }
1198 notifyRequester(request);
1199 break;
1200 case CMD_SET_FORBIDDEN_PLMNS:
1201 request = (MainThreadRequest) msg.obj;
1202 uiccCard = getUiccCardFromRequest(request);
1203 if (uiccCard == null) {
1204 loge("setForbiddenPlmns: UiccCard is null");
1205 request.result = -1;
1206 notifyRequester(request);
1207 break;
1208 }
1209 Pair<Integer, List<String>> setFplmnsArgs =
1210 (Pair<Integer, List<String>>) request.argument;
1211 appType = setFplmnsArgs.first;
1212 List<String> fplmns = setFplmnsArgs.second;
1213 uiccApp = uiccCard.getApplicationByType(appType);
1214 if (uiccApp == null) {
1215 loge("setForbiddenPlmns: no app with specified type -- " + appType);
1216 request.result = -1;
1217 loge("Failed to get UICC App");
1218 notifyRequester(request);
1219 } else {
1220 onCompleted = obtainMessage(EVENT_SET_FORBIDDEN_PLMNS_DONE, request);
1221 ((SIMRecords) uiccApp.getIccRecords())
1222 .setForbiddenPlmns(onCompleted, fplmns);
1223 }
yinchengzhao4d163c02019-12-12 15:21:47 -08001224 break;
Naina Nalluri8ff344d2019-09-17 14:10:30 -07001225 case CMD_ERASE_MODEM_CONFIG:
1226 request = (MainThreadRequest) msg.obj;
1227 onCompleted = obtainMessage(EVENT_ERASE_MODEM_CONFIG_DONE, request);
1228 defaultPhone.eraseModemConfig(onCompleted);
1229 break;
1230 case EVENT_ERASE_MODEM_CONFIG_DONE:
1231 handleNullReturnEvent(msg, "eraseModemConfig");
yincheng zhaod698b842019-09-06 17:06:54 -07001232 break;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001233 default:
1234 Log.w(LOG_TAG, "MainThreadHandler: unexpected message code: " + msg.what);
1235 break;
1236 }
1237 }
Jake Hambye994d462014-02-03 13:10:13 -08001238
Pengquan Menga1bb6272018-09-06 09:59:22 -07001239 private void notifyRequester(MainThreadRequest request) {
1240 synchronized (request) {
1241 request.notifyAll();
1242 }
1243 }
1244
Jake Hambye994d462014-02-03 13:10:13 -08001245 private void handleNullReturnEvent(Message msg, String command) {
1246 AsyncResult ar = (AsyncResult) msg.obj;
1247 MainThreadRequest request = (MainThreadRequest) ar.userObj;
1248 if (ar.exception == null) {
1249 request.result = true;
1250 } else {
1251 request.result = false;
1252 if (ar.exception instanceof CommandException) {
1253 loge(command + ": CommandException: " + ar.exception);
1254 } else {
1255 loge(command + ": Unknown exception");
1256 }
1257 }
Pengquan Menga1bb6272018-09-06 09:59:22 -07001258 notifyRequester(request);
Jake Hambye994d462014-02-03 13:10:13 -08001259 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001260 }
1261
1262 /**
1263 * Posts the specified command to be executed on the main thread,
1264 * waits for the request to complete, and returns the result.
1265 * @see #sendRequestAsync
1266 */
1267 private Object sendRequest(int command, Object argument) {
Nathan Harold92bed182018-10-12 18:16:49 -07001268 return sendRequest(
1269 command, argument, SubscriptionManager.INVALID_SUBSCRIPTION_ID, null, null);
vagdeviaf9a5b92018-08-15 16:01:53 -07001270 }
1271
1272 /**
1273 * Posts the specified command to be executed on the main thread,
1274 * waits for the request to complete, and returns the result.
1275 * @see #sendRequestAsync
1276 */
1277 private Object sendRequest(int command, Object argument, WorkSource workSource) {
1278 return sendRequest(command, argument, SubscriptionManager.INVALID_SUBSCRIPTION_ID,
Nathan Harold92bed182018-10-12 18:16:49 -07001279 null, workSource);
Wink Saville36469e72014-06-11 15:17:00 -07001280 }
1281
1282 /**
1283 * Posts the specified command to be executed on the main thread,
1284 * waits for the request to complete, and returns the result.
1285 * @see #sendRequestAsync
1286 */
Shishir Agrawal76d5da92014-11-09 16:17:25 -08001287 private Object sendRequest(int command, Object argument, Integer subId) {
Nathan Harold92bed182018-10-12 18:16:49 -07001288 return sendRequest(command, argument, subId, null, null);
vagdeviaf9a5b92018-08-15 16:01:53 -07001289 }
1290
1291 /**
1292 * Posts the specified command to be executed on the main thread,
1293 * waits for the request to complete, and returns the result.
1294 * @see #sendRequestAsync
1295 */
Nathan Harold92bed182018-10-12 18:16:49 -07001296 private Object sendRequest(int command, Object argument, int subId, WorkSource workSource) {
1297 return sendRequest(command, argument, subId, null, workSource);
1298 }
1299
1300 /**
1301 * Posts the specified command to be executed on the main thread,
1302 * waits for the request to complete, and returns the result.
1303 * @see #sendRequestAsync
1304 */
1305 private Object sendRequest(int command, Object argument, Phone phone, WorkSource workSource) {
1306 return sendRequest(
1307 command, argument, SubscriptionManager.INVALID_SUBSCRIPTION_ID, phone, workSource);
1308 }
1309
1310 /**
1311 * Posts the specified command to be executed on the main thread,
1312 * waits for the request to complete, and returns the result.
1313 * @see #sendRequestAsync
1314 */
1315 private Object sendRequest(
1316 int command, Object argument, Integer subId, Phone phone, WorkSource workSource) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001317 if (Looper.myLooper() == mMainThreadHandler.getLooper()) {
1318 throw new RuntimeException("This method will deadlock if called from the main thread.");
1319 }
1320
Nathan Harold92bed182018-10-12 18:16:49 -07001321 MainThreadRequest request = null;
1322 if (subId != SubscriptionManager.INVALID_SUBSCRIPTION_ID && phone != null) {
1323 throw new IllegalArgumentException("subId and phone cannot both be specified!");
1324 } else if (phone != null) {
1325 request = new MainThreadRequest(argument, phone, workSource);
1326 } else {
1327 request = new MainThreadRequest(argument, subId, workSource);
1328 }
1329
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001330 Message msg = mMainThreadHandler.obtainMessage(command, request);
1331 msg.sendToTarget();
1332
1333 // Wait for the request to complete
1334 synchronized (request) {
1335 while (request.result == null) {
1336 try {
1337 request.wait();
1338 } catch (InterruptedException e) {
1339 // Do nothing, go back and wait until the request is complete
1340 }
1341 }
1342 }
1343 return request.result;
1344 }
1345
1346 /**
1347 * Asynchronous ("fire and forget") version of sendRequest():
1348 * Posts the specified command to be executed on the main thread, and
1349 * returns immediately.
1350 * @see #sendRequest
1351 */
1352 private void sendRequestAsync(int command) {
1353 mMainThreadHandler.sendEmptyMessage(command);
1354 }
1355
1356 /**
Sailesh Nepalbd76e4e2013-10-27 13:59:44 -07001357 * Same as {@link #sendRequestAsync(int)} except it takes an argument.
Nathan Haroldfa8da0f2018-09-27 18:51:29 -07001358 * @see {@link #sendRequest(int)}
Sailesh Nepalbd76e4e2013-10-27 13:59:44 -07001359 */
1360 private void sendRequestAsync(int command, Object argument) {
Nathan Haroldfa8da0f2018-09-27 18:51:29 -07001361 sendRequestAsync(command, argument, null, null);
1362 }
1363
1364 /**
1365 * Same as {@link #sendRequestAsync(int,Object)} except it takes a Phone and WorkSource.
1366 * @see {@link #sendRequest(int,Object)}
1367 */
1368 private void sendRequestAsync(
1369 int command, Object argument, Phone phone, WorkSource workSource) {
1370 MainThreadRequest request = new MainThreadRequest(argument, phone, workSource);
Sailesh Nepalbd76e4e2013-10-27 13:59:44 -07001371 Message msg = mMainThreadHandler.obtainMessage(command, request);
1372 msg.sendToTarget();
1373 }
1374
1375 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001376 * Initialize the singleton PhoneInterfaceManager instance.
1377 * This is only done once, at startup, from PhoneApp.onCreate().
1378 */
Nazanin Bakhshif782e562018-12-11 15:15:39 -08001379 /* package */ static PhoneInterfaceManager init(PhoneGlobals app) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001380 synchronized (PhoneInterfaceManager.class) {
1381 if (sInstance == null) {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08001382 sInstance = new PhoneInterfaceManager(app);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001383 } else {
1384 Log.wtf(LOG_TAG, "init() called multiple times! sInstance = " + sInstance);
1385 }
1386 return sInstance;
1387 }
1388 }
1389
1390 /** Private constructor; @see init() */
Nazanin Bakhshif782e562018-12-11 15:15:39 -08001391 private PhoneInterfaceManager(PhoneGlobals app) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001392 mApp = app;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001393 mCM = PhoneGlobals.getInstance().mCM;
Brad Ebinger05f52c22019-12-05 13:03:21 -08001394 mImsResolver = PhoneGlobals.getInstance().getImsResolver();
Stuart Scott981d8582015-04-21 14:09:50 -07001395 mUserManager = (UserManager) app.getSystemService(Context.USER_SERVICE);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001396 mAppOps = (AppOpsManager)app.getSystemService(Context.APP_OPS_SERVICE);
1397 mMainThreadHandler = new MainThreadHandler();
Tobias Thiererb19e1f12018-12-11 17:54:03 +00001398 mSubscriptionController = SubscriptionController.getInstance();
Nazanin Bakhshif782e562018-12-11 15:15:39 -08001399 mTelephonySharedPreferences =
1400 PreferenceManager.getDefaultSharedPreferences(mApp);
yinxub1bed742017-04-17 11:45:04 -07001401 mNetworkScanRequestTracker = new NetworkScanRequestTracker();
Malcolm Chen2c63d402018-08-14 16:00:53 -07001402 mPhoneConfigurationManager = PhoneConfigurationManager.getInstance();
Wink Saville3ab207e2014-11-20 13:07:20 -08001403
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001404 publish();
1405 }
1406
Nazanin Bakhshif782e562018-12-11 15:15:39 -08001407 private Phone getDefaultPhone() {
1408 Phone thePhone = getPhone(getDefaultSubscription());
1409 return (thePhone != null) ? thePhone : PhoneFactory.getDefaultPhone();
1410 }
1411
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001412 private void publish() {
1413 if (DBG) log("publish: " + this);
1414
1415 ServiceManager.addService("phone", this);
1416 }
1417
Stuart Scott584921c2015-01-15 17:10:34 -08001418 private Phone getPhoneFromRequest(MainThreadRequest request) {
Jordan Liu4c733742019-02-28 12:03:40 -08001419 if (request.phone != null) {
1420 return request.phone;
1421 } else {
1422 return getPhoneFromSubId(request.subId);
1423 }
1424 }
1425
1426 private Phone getPhoneFromSubId(int subId) {
1427 return (subId == SubscriptionManager.INVALID_SUBSCRIPTION_ID)
1428 ? getDefaultPhone() : getPhone(subId);
Stuart Scott584921c2015-01-15 17:10:34 -08001429 }
1430
Shishir Agrawalc04d9752016-02-19 10:41:00 -08001431 private UiccCard getUiccCardFromRequest(MainThreadRequest request) {
1432 Phone phone = getPhoneFromRequest(request);
1433 return phone == null ? null :
1434 UiccController.getInstance().getUiccCard(phone.getPhoneId());
1435 }
1436
Wink Saville36469e72014-06-11 15:17:00 -07001437 // returns phone associated with the subId.
Wink Savilleb564aae2014-10-23 10:18:09 -07001438 private Phone getPhone(int subId) {
Wink Savilleac1bdfd2014-11-20 23:04:44 -08001439 return PhoneFactory.getPhone(mSubscriptionController.getPhoneId(subId));
Wink Saville36469e72014-06-11 15:17:00 -07001440 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001441
Naina Nalluri8ff344d2019-09-17 14:10:30 -07001442 private void sendEraseModemConfig(Phone phone) {
1443 if (phone != null) {
1444 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
1445 mApp, phone.getSubId(), "eraseModemConfig");
1446 final long identity = Binder.clearCallingIdentity();
1447 try {
1448 Boolean success = (Boolean) sendRequest(CMD_ERASE_MODEM_CONFIG, null);
1449 if (DBG) log("eraseModemConfig:" + ' ' + (success ? "ok" : "fail"));
1450 } finally {
1451 Binder.restoreCallingIdentity(identity);
1452 }
1453 }
1454 }
1455
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001456 public void dial(String number) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001457 dialForSubscriber(getPreferredVoiceSubscription(), number);
Wink Saville36469e72014-06-11 15:17:00 -07001458 }
1459
Wink Savilleb564aae2014-10-23 10:18:09 -07001460 public void dialForSubscriber(int subId, String number) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001461 if (DBG) log("dial: " + number);
1462 // No permission check needed here: This is just a wrapper around the
1463 // ACTION_DIAL intent, which is available to any app since it puts up
1464 // the UI before it does anything.
1465
Malcolm Chenaa4a8532018-02-28 15:00:40 -08001466 final long identity = Binder.clearCallingIdentity();
1467 try {
1468 String url = createTelUrl(number);
1469 if (url == null) {
1470 return;
1471 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001472
Malcolm Chenaa4a8532018-02-28 15:00:40 -08001473 // PENDING: should we just silently fail if phone is offhook or ringing?
1474 PhoneConstants.State state = mCM.getState(subId);
1475 if (state != PhoneConstants.State.OFFHOOK && state != PhoneConstants.State.RINGING) {
1476 Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse(url));
1477 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
1478 mApp.startActivity(intent);
1479 }
1480 } finally {
1481 Binder.restoreCallingIdentity(identity);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001482 }
1483 }
1484
1485 public void call(String callingPackage, String number) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001486 callForSubscriber(getPreferredVoiceSubscription(), callingPackage, number);
Wink Saville36469e72014-06-11 15:17:00 -07001487 }
1488
Wink Savilleb564aae2014-10-23 10:18:09 -07001489 public void callForSubscriber(int subId, String callingPackage, String number) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001490 if (DBG) log("call: " + number);
1491
1492 // This is just a wrapper around the ACTION_CALL intent, but we still
1493 // need to do a permission check since we're calling startActivity()
1494 // from the context of the phone app.
1495 enforceCallPermission();
1496
Jordan Liu1617b712019-07-10 15:06:26 -07001497 if (mAppOps.noteOp(AppOpsManager.OPSTR_CALL_PHONE, Binder.getCallingUid(), callingPackage)
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001498 != AppOpsManager.MODE_ALLOWED) {
1499 return;
1500 }
1501
Malcolm Chenaa4a8532018-02-28 15:00:40 -08001502 final long identity = Binder.clearCallingIdentity();
1503 try {
1504 String url = createTelUrl(number);
1505 if (url == null) {
1506 return;
1507 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001508
Malcolm Chenaa4a8532018-02-28 15:00:40 -08001509 boolean isValid = false;
1510 final List<SubscriptionInfo> slist = getActiveSubscriptionInfoListPrivileged();
1511 if (slist != null) {
1512 for (SubscriptionInfo subInfoRecord : slist) {
1513 if (subInfoRecord.getSubscriptionId() == subId) {
1514 isValid = true;
1515 break;
1516 }
Wink Saville3ab207e2014-11-20 13:07:20 -08001517 }
Wink Saville08874612014-08-31 19:19:58 -07001518 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08001519 if (!isValid) {
1520 return;
1521 }
Wink Saville08874612014-08-31 19:19:58 -07001522
Malcolm Chenaa4a8532018-02-28 15:00:40 -08001523 Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse(url));
1524 intent.putExtra(SUBSCRIPTION_KEY, subId);
1525 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
1526 mApp.startActivity(intent);
1527 } finally {
1528 Binder.restoreCallingIdentity(identity);
1529 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001530 }
1531
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001532 public boolean supplyPin(String pin) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001533 return supplyPinForSubscriber(getDefaultSubscription(), pin);
Wink Saville36469e72014-06-11 15:17:00 -07001534 }
1535
Wink Savilleb564aae2014-10-23 10:18:09 -07001536 public boolean supplyPinForSubscriber(int subId, String pin) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001537 int [] resultArray = supplyPinReportResultForSubscriber(subId, pin);
Wink Saville9de0f752013-10-22 19:04:03 -07001538 return (resultArray[0] == PhoneConstants.PIN_RESULT_SUCCESS) ? true : false;
1539 }
1540
1541 public boolean supplyPuk(String puk, String pin) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001542 return supplyPukForSubscriber(getDefaultSubscription(), puk, pin);
Wink Saville36469e72014-06-11 15:17:00 -07001543 }
1544
Wink Savilleb564aae2014-10-23 10:18:09 -07001545 public boolean supplyPukForSubscriber(int subId, String puk, String pin) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001546 int [] resultArray = supplyPukReportResultForSubscriber(subId, puk, pin);
Wink Saville9de0f752013-10-22 19:04:03 -07001547 return (resultArray[0] == PhoneConstants.PIN_RESULT_SUCCESS) ? true : false;
1548 }
1549
1550 /** {@hide} */
1551 public int[] supplyPinReportResult(String pin) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001552 return supplyPinReportResultForSubscriber(getDefaultSubscription(), pin);
Wink Saville36469e72014-06-11 15:17:00 -07001553 }
1554
Wink Savilleb564aae2014-10-23 10:18:09 -07001555 public int[] supplyPinReportResultForSubscriber(int subId, String pin) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001556 enforceModifyPermission();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08001557
1558 final long identity = Binder.clearCallingIdentity();
1559 try {
1560 final UnlockSim checkSimPin = new UnlockSim(getPhone(subId).getIccCard());
1561 checkSimPin.start();
1562 return checkSimPin.unlockSim(null, pin);
1563 } finally {
1564 Binder.restoreCallingIdentity(identity);
1565 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001566 }
1567
Wink Saville9de0f752013-10-22 19:04:03 -07001568 /** {@hide} */
1569 public int[] supplyPukReportResult(String puk, String pin) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001570 return supplyPukReportResultForSubscriber(getDefaultSubscription(), puk, pin);
Wink Saville36469e72014-06-11 15:17:00 -07001571 }
1572
Wink Savilleb564aae2014-10-23 10:18:09 -07001573 public int[] supplyPukReportResultForSubscriber(int subId, String puk, String pin) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001574 enforceModifyPermission();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08001575
1576 final long identity = Binder.clearCallingIdentity();
1577 try {
1578 final UnlockSim checkSimPuk = new UnlockSim(getPhone(subId).getIccCard());
1579 checkSimPuk.start();
1580 return checkSimPuk.unlockSim(puk, pin);
1581 } finally {
1582 Binder.restoreCallingIdentity(identity);
1583 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001584 }
1585
1586 /**
Wink Saville9de0f752013-10-22 19:04:03 -07001587 * Helper thread to turn async call to SimCard#supplyPin into
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001588 * a synchronous one.
1589 */
1590 private static class UnlockSim extends Thread {
1591
1592 private final IccCard mSimCard;
1593
1594 private boolean mDone = false;
Wink Saville9de0f752013-10-22 19:04:03 -07001595 private int mResult = PhoneConstants.PIN_GENERAL_FAILURE;
1596 private int mRetryCount = -1;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001597
1598 // For replies from SimCard interface
1599 private Handler mHandler;
1600
1601 // For async handler to identify request type
1602 private static final int SUPPLY_PIN_COMPLETE = 100;
1603
1604 public UnlockSim(IccCard simCard) {
1605 mSimCard = simCard;
1606 }
1607
1608 @Override
1609 public void run() {
1610 Looper.prepare();
1611 synchronized (UnlockSim.this) {
1612 mHandler = new Handler() {
1613 @Override
1614 public void handleMessage(Message msg) {
1615 AsyncResult ar = (AsyncResult) msg.obj;
1616 switch (msg.what) {
1617 case SUPPLY_PIN_COMPLETE:
1618 Log.d(LOG_TAG, "SUPPLY_PIN_COMPLETE");
1619 synchronized (UnlockSim.this) {
Wink Saville9de0f752013-10-22 19:04:03 -07001620 mRetryCount = msg.arg1;
1621 if (ar.exception != null) {
1622 if (ar.exception instanceof CommandException &&
1623 ((CommandException)(ar.exception)).getCommandError()
1624 == CommandException.Error.PASSWORD_INCORRECT) {
1625 mResult = PhoneConstants.PIN_PASSWORD_INCORRECT;
1626 } else {
1627 mResult = PhoneConstants.PIN_GENERAL_FAILURE;
1628 }
1629 } else {
1630 mResult = PhoneConstants.PIN_RESULT_SUCCESS;
1631 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001632 mDone = true;
1633 UnlockSim.this.notifyAll();
1634 }
1635 break;
1636 }
1637 }
1638 };
1639 UnlockSim.this.notifyAll();
1640 }
1641 Looper.loop();
1642 }
1643
1644 /*
1645 * Use PIN or PUK to unlock SIM card
1646 *
1647 * If PUK is null, unlock SIM card with PIN
1648 *
1649 * If PUK is not null, unlock SIM card with PUK and set PIN code
1650 */
Wink Saville9de0f752013-10-22 19:04:03 -07001651 synchronized int[] unlockSim(String puk, String pin) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001652
1653 while (mHandler == null) {
1654 try {
1655 wait();
1656 } catch (InterruptedException e) {
1657 Thread.currentThread().interrupt();
1658 }
1659 }
1660 Message callback = Message.obtain(mHandler, SUPPLY_PIN_COMPLETE);
1661
1662 if (puk == null) {
1663 mSimCard.supplyPin(pin, callback);
1664 } else {
1665 mSimCard.supplyPuk(puk, pin, callback);
1666 }
1667
1668 while (!mDone) {
1669 try {
1670 Log.d(LOG_TAG, "wait for done");
1671 wait();
1672 } catch (InterruptedException e) {
1673 // Restore the interrupted status
1674 Thread.currentThread().interrupt();
1675 }
1676 }
1677 Log.d(LOG_TAG, "done");
Wink Saville9de0f752013-10-22 19:04:03 -07001678 int[] resultArray = new int[2];
1679 resultArray[0] = mResult;
1680 resultArray[1] = mRetryCount;
1681 return resultArray;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001682 }
1683 }
1684
1685 public void updateServiceLocation() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001686 updateServiceLocationForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001687
1688 }
1689
Wink Savilleb564aae2014-10-23 10:18:09 -07001690 public void updateServiceLocationForSubscriber(int subId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001691 // No permission check needed here: this call is harmless, and it's
1692 // needed for the ServiceState.requestStateUpdate() call (which is
1693 // already intentionally exposed to 3rd parties.)
Malcolm Chenaa4a8532018-02-28 15:00:40 -08001694 final long identity = Binder.clearCallingIdentity();
1695 try {
1696 final Phone phone = getPhone(subId);
1697 if (phone != null) {
1698 phone.updateServiceLocation();
1699 }
1700 } finally {
1701 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07001702 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001703 }
1704
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001705 @Override
1706 public boolean isRadioOn(String callingPackage) {
1707 return isRadioOnForSubscriber(getDefaultSubscription(), callingPackage);
Wink Saville36469e72014-06-11 15:17:00 -07001708 }
1709
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001710 @Override
1711 public boolean isRadioOnForSubscriber(int subId, String callingPackage) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08001712 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
Jeff Davidson913390f2018-02-23 17:11:49 -08001713 mApp, subId, callingPackage, "isRadioOnForSubscriber")) {
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001714 return false;
1715 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08001716
1717 final long identity = Binder.clearCallingIdentity();
1718 try {
1719 return isRadioOnForSubscriber(subId);
1720 } finally {
1721 Binder.restoreCallingIdentity(identity);
1722 }
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001723 }
1724
1725 private boolean isRadioOnForSubscriber(int subId) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08001726 final long identity = Binder.clearCallingIdentity();
1727 try {
1728 final Phone phone = getPhone(subId);
1729 if (phone != null) {
1730 return phone.getServiceState().getState() != ServiceState.STATE_POWER_OFF;
1731 } else {
1732 return false;
1733 }
1734 } finally {
1735 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07001736 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001737 }
1738
1739 public void toggleRadioOnOff() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001740 toggleRadioOnOffForSubscriber(getDefaultSubscription());
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001741 }
Wink Saville36469e72014-06-11 15:17:00 -07001742
Wink Savilleb564aae2014-10-23 10:18:09 -07001743 public void toggleRadioOnOffForSubscriber(int subId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001744 enforceModifyPermission();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08001745
1746 final long identity = Binder.clearCallingIdentity();
1747 try {
1748 final Phone phone = getPhone(subId);
1749 if (phone != null) {
1750 phone.setRadioPower(!isRadioOnForSubscriber(subId));
1751 }
1752 } finally {
1753 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07001754 }
Wink Saville36469e72014-06-11 15:17:00 -07001755 }
1756
1757 public boolean setRadio(boolean turnOn) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001758 return setRadioForSubscriber(getDefaultSubscription(), turnOn);
Wink Saville36469e72014-06-11 15:17:00 -07001759 }
1760
Wink Savilleb564aae2014-10-23 10:18:09 -07001761 public boolean setRadioForSubscriber(int subId, boolean turnOn) {
Wink Saville36469e72014-06-11 15:17:00 -07001762 enforceModifyPermission();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08001763
1764 final long identity = Binder.clearCallingIdentity();
1765 try {
1766 final Phone phone = getPhone(subId);
1767 if (phone == null) {
1768 return false;
1769 }
1770 if ((phone.getServiceState().getState() != ServiceState.STATE_POWER_OFF) != turnOn) {
1771 toggleRadioOnOffForSubscriber(subId);
1772 }
1773 return true;
1774 } finally {
1775 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07001776 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001777 }
Wink Saville36469e72014-06-11 15:17:00 -07001778
Naveen Kalla1fd79bd2014-08-08 00:48:59 -07001779 public boolean needMobileRadioShutdown() {
Shuo Qianafeaf7d2019-12-10 10:40:38 -08001780 enforceReadPrivilegedPermission("needMobileRadioShutdown");
Naveen Kalla1fd79bd2014-08-08 00:48:59 -07001781 /*
1782 * If any of the Radios are available, it will need to be
1783 * shutdown. So return true if any Radio is available.
1784 */
Malcolm Chenaa4a8532018-02-28 15:00:40 -08001785 final long identity = Binder.clearCallingIdentity();
1786 try {
1787 for (int i = 0; i < TelephonyManager.getDefault().getPhoneCount(); i++) {
1788 Phone phone = PhoneFactory.getPhone(i);
1789 if (phone != null && phone.isRadioAvailable()) return true;
1790 }
1791 logv(TelephonyManager.getDefault().getPhoneCount() + " Phones are shutdown.");
1792 return false;
1793 } finally {
1794 Binder.restoreCallingIdentity(identity);
Naveen Kalla1fd79bd2014-08-08 00:48:59 -07001795 }
Naveen Kalla1fd79bd2014-08-08 00:48:59 -07001796 }
1797
Malcolm Chenaa4a8532018-02-28 15:00:40 -08001798 @Override
Naveen Kalla1fd79bd2014-08-08 00:48:59 -07001799 public void shutdownMobileRadios() {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08001800 enforceModifyPermission();
1801
1802 final long identity = Binder.clearCallingIdentity();
1803 try {
1804 for (int i = 0; i < TelephonyManager.getDefault().getPhoneCount(); i++) {
1805 logv("Shutting down Phone " + i);
1806 shutdownRadioUsingPhoneId(i);
1807 }
1808 } finally {
1809 Binder.restoreCallingIdentity(identity);
Naveen Kalla1fd79bd2014-08-08 00:48:59 -07001810 }
1811 }
1812
1813 private void shutdownRadioUsingPhoneId(int phoneId) {
Naveen Kalla1fd79bd2014-08-08 00:48:59 -07001814 Phone phone = PhoneFactory.getPhone(phoneId);
1815 if (phone != null && phone.isRadioAvailable()) {
1816 phone.shutdownRadio();
1817 }
1818 }
1819
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001820 public boolean setRadioPower(boolean turnOn) {
Jack Yub4e16162017-05-15 12:48:40 -07001821 enforceModifyPermission();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08001822
1823 final long identity = Binder.clearCallingIdentity();
1824 try {
1825 final Phone defaultPhone = PhoneFactory.getDefaultPhone();
1826 if (defaultPhone != null) {
1827 defaultPhone.setRadioPower(turnOn);
1828 return true;
1829 } else {
1830 loge("There's no default phone.");
1831 return false;
1832 }
1833 } finally {
1834 Binder.restoreCallingIdentity(identity);
Wei Liu9ae2a062016-08-08 11:09:34 -07001835 }
Wink Saville36469e72014-06-11 15:17:00 -07001836 }
1837
Wink Savilleb564aae2014-10-23 10:18:09 -07001838 public boolean setRadioPowerForSubscriber(int subId, boolean turnOn) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001839 enforceModifyPermission();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08001840
1841 final long identity = Binder.clearCallingIdentity();
1842 try {
1843 final Phone phone = getPhone(subId);
1844 if (phone != null) {
1845 phone.setRadioPower(turnOn);
1846 return true;
1847 } else {
1848 return false;
1849 }
1850 } finally {
1851 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07001852 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001853 }
1854
Wink Saville36469e72014-06-11 15:17:00 -07001855 // FIXME: subId version needed
Sanket Padawe356d7632015-06-22 14:03:32 -07001856 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001857 public boolean enableDataConnectivity() {
1858 enforceModifyPermission();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08001859
1860 final long identity = Binder.clearCallingIdentity();
1861 try {
1862 int subId = mSubscriptionController.getDefaultDataSubId();
1863 final Phone phone = getPhone(subId);
1864 if (phone != null) {
Jack Yud79fba22018-12-13 11:51:28 -08001865 phone.getDataEnabledSettings().setUserDataEnabled(true);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08001866 return true;
1867 } else {
1868 return false;
1869 }
1870 } finally {
1871 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07001872 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001873 }
1874
Wink Saville36469e72014-06-11 15:17:00 -07001875 // FIXME: subId version needed
Sanket Padawe356d7632015-06-22 14:03:32 -07001876 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001877 public boolean disableDataConnectivity() {
1878 enforceModifyPermission();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08001879
1880 final long identity = Binder.clearCallingIdentity();
1881 try {
1882 int subId = mSubscriptionController.getDefaultDataSubId();
1883 final Phone phone = getPhone(subId);
1884 if (phone != null) {
Jack Yud79fba22018-12-13 11:51:28 -08001885 phone.getDataEnabledSettings().setUserDataEnabled(false);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08001886 return true;
1887 } else {
1888 return false;
1889 }
1890 } finally {
1891 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07001892 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001893 }
1894
Sanket Padawe356d7632015-06-22 14:03:32 -07001895 @Override
Jack Yuacf8a132017-05-01 17:00:48 -07001896 public boolean isDataConnectivityPossible(int subId) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08001897 final long identity = Binder.clearCallingIdentity();
1898 try {
1899 final Phone phone = getPhone(subId);
1900 if (phone != null) {
Jack Yub5d8f642018-11-26 11:20:48 -08001901 return phone.isDataAllowed(ApnSetting.TYPE_DEFAULT);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08001902 } else {
1903 return false;
1904 }
1905 } finally {
1906 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07001907 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001908 }
1909
1910 public boolean handlePinMmi(String dialString) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001911 return handlePinMmiForSubscriber(getDefaultSubscription(), dialString);
Wink Saville36469e72014-06-11 15:17:00 -07001912 }
1913
pkanwarae03a6b2016-11-06 20:37:09 -08001914 public void handleUssdRequest(int subId, String ussdRequest, ResultReceiver wrappedCallback) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08001915 enforceCallPermission();
1916
1917 final long identity = Binder.clearCallingIdentity();
1918 try {
1919 if (!SubscriptionManager.isValidSubscriptionId(subId)) {
1920 return;
1921 }
1922 Pair<String, ResultReceiver> ussdObject = new Pair(ussdRequest, wrappedCallback);
1923 sendRequest(CMD_HANDLE_USSD_REQUEST, ussdObject, subId);
1924 } finally {
1925 Binder.restoreCallingIdentity(identity);
1926 }
pkanwar32d516d2016-10-14 19:37:38 -07001927 };
1928
Wink Savilleb564aae2014-10-23 10:18:09 -07001929 public boolean handlePinMmiForSubscriber(int subId, String dialString) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001930 enforceModifyPermission();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08001931
1932 final long identity = Binder.clearCallingIdentity();
1933 try {
1934 if (!SubscriptionManager.isValidSubscriptionId(subId)) {
1935 return false;
1936 }
1937 return (Boolean) sendRequest(CMD_HANDLE_PIN_MMI, dialString, subId);
1938 } finally {
1939 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07001940 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001941 }
1942
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001943 public int getCallState() {
Shishir Agrawala9f32182016-04-12 12:00:16 -07001944 return getCallStateForSlot(getSlotForDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001945 }
1946
Sanket Padawe13bac7b2017-03-20 15:04:47 -07001947 public int getCallStateForSlot(int slotIndex) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08001948 final long identity = Binder.clearCallingIdentity();
1949 try {
1950 Phone phone = PhoneFactory.getPhone(slotIndex);
1951 return phone == null ? TelephonyManager.CALL_STATE_IDLE :
1952 PhoneConstantConversions.convertCallState(phone.getState());
1953 } finally {
1954 Binder.restoreCallingIdentity(identity);
1955 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001956 }
1957
Sanket Padawe356d7632015-06-22 14:03:32 -07001958 @Override
Nathan Harolde037c472019-06-26 00:41:07 +00001959 public int getDataState() {
Nathan Haroldc4689b12019-06-14 16:58:30 -07001960 return getDataStateForSubId(mSubscriptionController.getDefaultDataSubId());
1961 }
1962
1963 @Override
1964 public int getDataStateForSubId(int subId) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08001965 final long identity = Binder.clearCallingIdentity();
1966 try {
Nathan Haroldc4689b12019-06-14 16:58:30 -07001967 final Phone phone = getPhone(subId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08001968 if (phone != null) {
1969 return PhoneConstantConversions.convertDataState(phone.getDataConnectionState());
1970 } else {
1971 return PhoneConstantConversions.convertDataState(
1972 PhoneConstants.DataState.DISCONNECTED);
1973 }
1974 } finally {
1975 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07001976 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001977 }
1978
Sanket Padawe356d7632015-06-22 14:03:32 -07001979 @Override
Nathan Harolde037c472019-06-26 00:41:07 +00001980 public int getDataActivity() {
Nathan Haroldc4689b12019-06-14 16:58:30 -07001981 return getDataActivityForSubId(mSubscriptionController.getDefaultDataSubId());
1982 }
1983
1984 @Override
1985 public int getDataActivityForSubId(int subId) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08001986 final long identity = Binder.clearCallingIdentity();
1987 try {
Nathan Haroldc4689b12019-06-14 16:58:30 -07001988 final Phone phone = getPhone(subId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08001989 if (phone != null) {
1990 return DefaultPhoneNotifier.convertDataActivityState(phone.getDataActivityState());
1991 } else {
1992 return TelephonyManager.DATA_ACTIVITY_NONE;
1993 }
1994 } finally {
1995 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07001996 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001997 }
1998
1999 @Override
Svetoslav64fad262015-04-14 14:35:21 -07002000 public Bundle getCellLocation(String callingPackage) {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08002001 mApp.getSystemService(AppOpsManager.class)
Hall Liu1aa510f2017-11-22 17:40:08 -08002002 .checkPackage(Binder.getCallingUid(), callingPackage);
Hall Liuf19c44f2018-11-27 14:38:17 -08002003
2004 LocationAccessPolicy.LocationPermissionResult locationResult =
2005 LocationAccessPolicy.checkLocationPermission(mApp,
2006 new LocationAccessPolicy.LocationPermissionQuery.Builder()
2007 .setCallingPackage(callingPackage)
2008 .setCallingPid(Binder.getCallingPid())
2009 .setCallingUid(Binder.getCallingUid())
2010 .setMethod("getCellLocation")
2011 .setMinSdkVersionForFine(Build.VERSION_CODES.Q)
2012 .build());
2013 switch (locationResult) {
2014 case DENIED_HARD:
2015 throw new SecurityException("Not allowed to access cell location");
2016 case DENIED_SOFT:
2017 return new Bundle();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002018 }
2019
Narayan Kamathf04b5a12018-01-09 11:47:15 +00002020 WorkSource workSource = getWorkSource(Binder.getCallingUid());
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002021 final long identity = Binder.clearCallingIdentity();
2022 try {
2023 if (DBG_LOC) log("getCellLocation: is active user");
2024 Bundle data = new Bundle();
Nathan Harold3ff88932018-08-14 10:19:49 -07002025 int subId = mSubscriptionController.getDefaultDataSubId();
2026 CellLocation cl = (CellLocation) sendRequest(CMD_GET_CELL_LOCATION, workSource, subId);
2027 cl.fillInNotifierBundle(data);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002028 return data;
2029 } finally {
2030 Binder.restoreCallingIdentity(identity);
2031 }
Svetoslav64fad262015-04-14 14:35:21 -07002032 }
2033
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002034 @Override
Jack Yu7c5d91a2019-09-26 11:48:33 -07002035 public String getNetworkCountryIsoForPhone(int phoneId, String callingPackage) {
2036 if (!TextUtils.isEmpty(callingPackage)) {
2037 final int subId = mSubscriptionController.getSubIdUsingPhoneId(phoneId);
2038 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
2039 mApp, subId, callingPackage, "getNetworkCountryIsoForPhone")) {
2040 return "";
2041 }
2042 }
2043
Jonathan Basseribf5362b2017-07-19 12:22:35 -07002044 // Reporting the correct network country is ambiguous when IWLAN could conflict with
2045 // registered cell info, so return a NULL country instead.
2046 final long identity = Binder.clearCallingIdentity();
2047 try {
Malcolm Chen3732c2b2018-07-18 20:15:24 -07002048 if (phoneId == SubscriptionManager.INVALID_PHONE_INDEX) {
2049 // Get default phone in this case.
2050 phoneId = SubscriptionManager.DEFAULT_PHONE_INDEX;
2051 }
Jonathan Basseribf5362b2017-07-19 12:22:35 -07002052 final int subId = mSubscriptionController.getSubIdUsingPhoneId(phoneId);
Jack Yu5f7092c2018-04-13 14:05:37 -07002053 // Todo: fix this when we can get the actual cellular network info when the device
2054 // is on IWLAN.
Jonathan Basseribf5362b2017-07-19 12:22:35 -07002055 if (TelephonyManager.NETWORK_TYPE_IWLAN
2056 == getVoiceNetworkTypeForSubscriber(subId, mApp.getPackageName())) {
2057 return "";
2058 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002059 Phone phone = PhoneFactory.getPhone(phoneId);
2060 if (phone != null) {
2061 ServiceStateTracker sst = phone.getServiceStateTracker();
sqianb9d961a2019-07-31 20:23:45 -07002062 EmergencyNumberTracker emergencyNumberTracker = phone.getEmergencyNumberTracker();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002063 if (sst != null) {
2064 LocaleTracker lt = sst.getLocaleTracker();
2065 if (lt != null) {
sqianb9d961a2019-07-31 20:23:45 -07002066 if (!TextUtils.isEmpty(lt.getCurrentCountry())) {
2067 return lt.getCurrentCountry();
2068 } else if (emergencyNumberTracker != null) {
2069 return emergencyNumberTracker.getEmergencyCountryIso();
2070 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002071 }
2072 }
2073 }
2074 return "";
Jonathan Basseribf5362b2017-07-19 12:22:35 -07002075 } finally {
2076 Binder.restoreCallingIdentity(identity);
2077 }
Jonathan Basseribf5362b2017-07-19 12:22:35 -07002078 }
2079
2080 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002081 public void enableLocationUpdates() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07002082 enableLocationUpdatesForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07002083 }
2084
Sanket Padawe356d7632015-06-22 14:03:32 -07002085 @Override
Wink Savilleb564aae2014-10-23 10:18:09 -07002086 public void enableLocationUpdatesForSubscriber(int subId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002087 mApp.enforceCallingOrSelfPermission(
2088 android.Manifest.permission.CONTROL_LOCATION_UPDATES, null);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002089
2090 final long identity = Binder.clearCallingIdentity();
2091 try {
2092 final Phone phone = getPhone(subId);
2093 if (phone != null) {
2094 phone.enableLocationUpdates();
2095 }
2096 } finally {
2097 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07002098 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002099 }
2100
2101 @Override
2102 public void disableLocationUpdates() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07002103 disableLocationUpdatesForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07002104 }
2105
Sanket Padawe356d7632015-06-22 14:03:32 -07002106 @Override
Wink Savilleb564aae2014-10-23 10:18:09 -07002107 public void disableLocationUpdatesForSubscriber(int subId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002108 mApp.enforceCallingOrSelfPermission(
2109 android.Manifest.permission.CONTROL_LOCATION_UPDATES, null);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002110
2111 final long identity = Binder.clearCallingIdentity();
2112 try {
2113 final Phone phone = getPhone(subId);
2114 if (phone != null) {
2115 phone.disableLocationUpdates();
2116 }
2117 } finally {
2118 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07002119 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002120 }
2121
Nathan Harold31d7ff32018-10-15 20:20:30 -07002122 /**
2123 * Returns the target SDK version number for a given package name.
2124 *
Nathan Haroldec184742019-07-10 17:04:16 -07002125 * This call MUST be invoked before clearing the calling UID.
2126 *
Nathan Harold31d7ff32018-10-15 20:20:30 -07002127 * @return target SDK if the package is found or INT_MAX.
2128 */
2129 private int getTargetSdk(String packageName) {
2130 try {
Nathan Haroldec184742019-07-10 17:04:16 -07002131 final ApplicationInfo ai = mApp.getPackageManager().getApplicationInfoAsUser(
Chen Xu54d20302019-07-30 15:12:06 -07002132 packageName, 0, UserHandle.getUserHandleForUid(Binder.getCallingUid()));
Nathan Harold31d7ff32018-10-15 20:20:30 -07002133 if (ai != null) return ai.targetSdkVersion;
2134 } catch (PackageManager.NameNotFoundException unexpected) {
Nathan Haroldec184742019-07-10 17:04:16 -07002135 loge("Failed to get package info for pkg="
2136 + packageName + ", uid=" + Binder.getCallingUid());
Nathan Harold31d7ff32018-10-15 20:20:30 -07002137 }
2138 return Integer.MAX_VALUE;
2139 }
2140
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002141 @Override
2142 @SuppressWarnings("unchecked")
Nathan Harold31d7ff32018-10-15 20:20:30 -07002143 public List<NeighboringCellInfo> getNeighboringCellInfo(String callingPackage) {
2144 final int targetSdk = getTargetSdk(callingPackage);
Nathan Harolddbea45a2018-08-30 14:35:07 -07002145 if (targetSdk >= android.os.Build.VERSION_CODES.Q) {
2146 throw new SecurityException(
2147 "getNeighboringCellInfo() is unavailable to callers targeting Q+ SDK levels.");
2148 }
Nathan Haroldb4d55612018-07-20 13:13:08 -07002149
Jordan Liu1617b712019-07-10 15:06:26 -07002150 if (mAppOps.noteOp(AppOpsManager.OPSTR_NEIGHBORING_CELLS, Binder.getCallingUid(),
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002151 callingPackage) != AppOpsManager.MODE_ALLOWED) {
2152 return null;
2153 }
Svetoslav64fad262015-04-14 14:35:21 -07002154
Svetoslav Ganov4a9d4482017-06-20 19:53:35 -07002155 if (DBG_LOC) log("getNeighboringCellInfo: is active user");
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002156
Nathan Haroldf180aac2018-06-01 18:43:55 -07002157 List<CellInfo> info = getAllCellInfo(callingPackage);
2158 if (info == null) return null;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002159
Nathan Haroldf180aac2018-06-01 18:43:55 -07002160 List<NeighboringCellInfo> neighbors = new ArrayList<NeighboringCellInfo>();
2161 for (CellInfo ci : info) {
2162 if (ci instanceof CellInfoGsm) {
2163 neighbors.add(new NeighboringCellInfo((CellInfoGsm) ci));
2164 } else if (ci instanceof CellInfoWcdma) {
2165 neighbors.add(new NeighboringCellInfo((CellInfoWcdma) ci));
2166 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002167 }
Nathan Haroldf180aac2018-06-01 18:43:55 -07002168 return (neighbors.size()) > 0 ? neighbors : null;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002169 }
2170
Nathan Haroldfa8da0f2018-09-27 18:51:29 -07002171 private List<CellInfo> getCachedCellInfo() {
2172 List<CellInfo> cellInfos = new ArrayList<CellInfo>();
2173 for (Phone phone : PhoneFactory.getPhones()) {
2174 List<CellInfo> info = phone.getAllCellInfo();
2175 if (info != null) cellInfos.addAll(info);
2176 }
2177 return cellInfos;
2178 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002179
2180 @Override
Svetoslav64fad262015-04-14 14:35:21 -07002181 public List<CellInfo> getAllCellInfo(String callingPackage) {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08002182 mApp.getSystemService(AppOpsManager.class)
Hall Liu1aa510f2017-11-22 17:40:08 -08002183 .checkPackage(Binder.getCallingUid(), callingPackage);
Hall Liuf19c44f2018-11-27 14:38:17 -08002184
2185 LocationAccessPolicy.LocationPermissionResult locationResult =
2186 LocationAccessPolicy.checkLocationPermission(mApp,
2187 new LocationAccessPolicy.LocationPermissionQuery.Builder()
2188 .setCallingPackage(callingPackage)
2189 .setCallingPid(Binder.getCallingPid())
2190 .setCallingUid(Binder.getCallingUid())
2191 .setMethod("getAllCellInfo")
Nathan Harold5ae50b52019-02-20 15:46:36 -08002192 .setMinSdkVersionForCoarse(Build.VERSION_CODES.BASE)
Hall Liuf19c44f2018-11-27 14:38:17 -08002193 .setMinSdkVersionForFine(Build.VERSION_CODES.Q)
2194 .build());
2195 switch (locationResult) {
2196 case DENIED_HARD:
2197 throw new SecurityException("Not allowed to access cell info");
2198 case DENIED_SOFT:
2199 return new ArrayList<>();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002200 }
2201
Nathan Haroldfa8da0f2018-09-27 18:51:29 -07002202 final int targetSdk = getTargetSdk(callingPackage);
2203 if (targetSdk >= android.os.Build.VERSION_CODES.Q) {
2204 return getCachedCellInfo();
2205 }
2206
Svetoslav Ganov4a9d4482017-06-20 19:53:35 -07002207 if (DBG_LOC) log("getAllCellInfo: is active user");
Narayan Kamathf04b5a12018-01-09 11:47:15 +00002208 WorkSource workSource = getWorkSource(Binder.getCallingUid());
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002209 final long identity = Binder.clearCallingIdentity();
2210 try {
2211 List<CellInfo> cellInfos = new ArrayList<CellInfo>();
2212 for (Phone phone : PhoneFactory.getPhones()) {
Nathan Harold3ff88932018-08-14 10:19:49 -07002213 final List<CellInfo> info = (List<CellInfo>) sendRequest(
Nathan Harold92bed182018-10-12 18:16:49 -07002214 CMD_GET_ALL_CELL_INFO, null, phone, workSource);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002215 if (info != null) cellInfos.addAll(info);
2216 }
2217 return cellInfos;
2218 } finally {
2219 Binder.restoreCallingIdentity(identity);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002220 }
2221 }
2222
Sailesh Nepalbd76e4e2013-10-27 13:59:44 -07002223 @Override
Nathan Haroldfa8da0f2018-09-27 18:51:29 -07002224 public void requestCellInfoUpdate(int subId, ICellInfoCallback cb, String callingPackage) {
2225 requestCellInfoUpdateInternal(
2226 subId, cb, callingPackage, getWorkSource(Binder.getCallingUid()));
2227 }
2228
2229 @Override
2230 public void requestCellInfoUpdateWithWorkSource(
2231 int subId, ICellInfoCallback cb, String callingPackage, WorkSource workSource) {
2232 enforceModifyPermission();
2233 requestCellInfoUpdateInternal(subId, cb, callingPackage, workSource);
2234 }
2235
2236 private void requestCellInfoUpdateInternal(
2237 int subId, ICellInfoCallback cb, String callingPackage, WorkSource workSource) {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08002238 mApp.getSystemService(AppOpsManager.class)
Nathan Haroldfa8da0f2018-09-27 18:51:29 -07002239 .checkPackage(Binder.getCallingUid(), callingPackage);
Hall Liuf19c44f2018-11-27 14:38:17 -08002240
2241 LocationAccessPolicy.LocationPermissionResult locationResult =
2242 LocationAccessPolicy.checkLocationPermission(mApp,
2243 new LocationAccessPolicy.LocationPermissionQuery.Builder()
2244 .setCallingPackage(callingPackage)
2245 .setCallingPid(Binder.getCallingPid())
2246 .setCallingUid(Binder.getCallingUid())
2247 .setMethod("requestCellInfoUpdate")
2248 .setMinSdkVersionForFine(Build.VERSION_CODES.Q)
2249 .build());
2250 switch (locationResult) {
2251 case DENIED_HARD:
2252 throw new SecurityException("Not allowed to access cell info");
2253 case DENIED_SOFT:
Nathan Harold5320c422019-05-09 10:26:08 -07002254 try {
2255 cb.onCellInfo(new ArrayList<CellInfo>());
2256 } catch (RemoteException re) {
2257 // Drop without consequences
2258 }
Hall Liuf19c44f2018-11-27 14:38:17 -08002259 return;
Nathan Haroldfa8da0f2018-09-27 18:51:29 -07002260 }
2261
Nathan Harolda939a962019-05-09 10:13:47 -07002262
2263 final Phone phone = getPhoneFromSubId(subId);
Nathan Haroldfa8da0f2018-09-27 18:51:29 -07002264 if (phone == null) throw new IllegalArgumentException("Invalid Subscription Id: " + subId);
2265
2266 sendRequestAsync(CMD_REQUEST_CELL_INFO_UPDATE, cb, phone, workSource);
2267 }
2268
2269 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002270 public void setCellInfoListRate(int rateInMillis) {
Jack Yua8d8cb82017-01-16 10:15:34 -08002271 enforceModifyPermission();
Narayan Kamathf04b5a12018-01-09 11:47:15 +00002272 WorkSource workSource = getWorkSource(Binder.getCallingUid());
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002273
2274 final long identity = Binder.clearCallingIdentity();
2275 try {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08002276 getDefaultPhone().setCellInfoListRate(rateInMillis, workSource);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002277 } finally {
2278 Binder.restoreCallingIdentity(identity);
2279 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002280 }
2281
Shishir Agrawala9f32182016-04-12 12:00:16 -07002282 @Override
Sanket Padawe13bac7b2017-03-20 15:04:47 -07002283 public String getImeiForSlot(int slotIndex, String callingPackage) {
Jeff Davidson913390f2018-02-23 17:11:49 -08002284 Phone phone = PhoneFactory.getPhone(slotIndex);
2285 if (phone == null) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08002286 return null;
2287 }
Jeff Davidson913390f2018-02-23 17:11:49 -08002288 int subId = phone.getSubId();
Michael Groover70af6dc2018-10-01 16:23:15 -07002289 if (!TelephonyPermissions.checkCallingOrSelfReadDeviceIdentifiers(mApp, subId,
2290 callingPackage, "getImeiForSlot")) {
Jeff Davidson913390f2018-02-23 17:11:49 -08002291 return null;
2292 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002293
2294 final long identity = Binder.clearCallingIdentity();
2295 try {
2296 return phone.getImei();
2297 } finally {
2298 Binder.restoreCallingIdentity(identity);
2299 }
Shishir Agrawala9f32182016-04-12 12:00:16 -07002300 }
2301
2302 @Override
David Kelly5e06a7f2018-03-12 14:10:59 +00002303 public String getTypeAllocationCodeForSlot(int slotIndex) {
2304 Phone phone = PhoneFactory.getPhone(slotIndex);
2305 String tac = null;
2306 if (phone != null) {
2307 String imei = phone.getImei();
2308 tac = imei == null ? null : imei.substring(0, TYPE_ALLOCATION_CODE_LENGTH);
2309 }
2310 return tac;
2311 }
2312
2313 @Override
Jack Yu2af8d712017-03-15 17:14:14 -07002314 public String getMeidForSlot(int slotIndex, String callingPackage) {
Jeff Davidson913390f2018-02-23 17:11:49 -08002315 Phone phone = PhoneFactory.getPhone(slotIndex);
2316 if (phone == null) {
Jack Yu2af8d712017-03-15 17:14:14 -07002317 return null;
2318 }
Nazanin Bakhshif782e562018-12-11 15:15:39 -08002319
Jeff Davidson913390f2018-02-23 17:11:49 -08002320 int subId = phone.getSubId();
Michael Groover70af6dc2018-10-01 16:23:15 -07002321 if (!TelephonyPermissions.checkCallingOrSelfReadDeviceIdentifiers(mApp, subId,
2322 callingPackage, "getMeidForSlot")) {
Jeff Davidson913390f2018-02-23 17:11:49 -08002323 return null;
2324 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002325
2326 final long identity = Binder.clearCallingIdentity();
2327 try {
2328 return phone.getMeid();
2329 } finally {
2330 Binder.restoreCallingIdentity(identity);
2331 }
Jack Yu2af8d712017-03-15 17:14:14 -07002332 }
2333
2334 @Override
David Kelly5e06a7f2018-03-12 14:10:59 +00002335 public String getManufacturerCodeForSlot(int slotIndex) {
2336 Phone phone = PhoneFactory.getPhone(slotIndex);
2337 String manufacturerCode = null;
2338 if (phone != null) {
2339 String meid = phone.getMeid();
2340 manufacturerCode = meid == null ? null : meid.substring(0, MANUFACTURER_CODE_LENGTH);
2341 }
2342 return manufacturerCode;
2343 }
2344
2345 @Override
Sanket Padawe13bac7b2017-03-20 15:04:47 -07002346 public String getDeviceSoftwareVersionForSlot(int slotIndex, String callingPackage) {
Jeff Davidson913390f2018-02-23 17:11:49 -08002347 Phone phone = PhoneFactory.getPhone(slotIndex);
2348 if (phone == null) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08002349 return null;
2350 }
Jeff Davidson913390f2018-02-23 17:11:49 -08002351 int subId = phone.getSubId();
2352 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
2353 mApp, subId, callingPackage, "getDeviceSoftwareVersionForSlot")) {
2354 return null;
2355 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002356
2357 final long identity = Binder.clearCallingIdentity();
2358 try {
2359 return phone.getDeviceSvn();
2360 } finally {
2361 Binder.restoreCallingIdentity(identity);
2362 }
Shishir Agrawala9f32182016-04-12 12:00:16 -07002363 }
2364
fionaxu43304da2017-11-27 22:51:16 -08002365 @Override
2366 public int getSubscriptionCarrierId(int subId) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002367 final long identity = Binder.clearCallingIdentity();
2368 try {
2369 final Phone phone = getPhone(subId);
2370 return phone == null ? TelephonyManager.UNKNOWN_CARRIER_ID : phone.getCarrierId();
2371 } finally {
2372 Binder.restoreCallingIdentity(identity);
2373 }
fionaxu43304da2017-11-27 22:51:16 -08002374 }
2375
2376 @Override
2377 public String getSubscriptionCarrierName(int subId) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002378 final long identity = Binder.clearCallingIdentity();
2379 try {
2380 final Phone phone = getPhone(subId);
2381 return phone == null ? null : phone.getCarrierName();
2382 } finally {
2383 Binder.restoreCallingIdentity(identity);
2384 }
fionaxu43304da2017-11-27 22:51:16 -08002385 }
2386
calvinpanffe225e2018-11-01 19:43:06 +08002387 @Override
chen xu0026ca62019-03-06 15:28:50 -08002388 public int getSubscriptionSpecificCarrierId(int subId) {
chen xu25637222018-11-04 17:17:00 -08002389 final long identity = Binder.clearCallingIdentity();
2390 try {
2391 final Phone phone = getPhone(subId);
2392 return phone == null ? TelephonyManager.UNKNOWN_CARRIER_ID
chen xu0026ca62019-03-06 15:28:50 -08002393 : phone.getSpecificCarrierId();
chen xu25637222018-11-04 17:17:00 -08002394 } finally {
2395 Binder.restoreCallingIdentity(identity);
2396 }
2397 }
2398
2399 @Override
chen xu0026ca62019-03-06 15:28:50 -08002400 public String getSubscriptionSpecificCarrierName(int subId) {
chen xu25637222018-11-04 17:17:00 -08002401 final long identity = Binder.clearCallingIdentity();
2402 try {
2403 final Phone phone = getPhone(subId);
chen xu0026ca62019-03-06 15:28:50 -08002404 return phone == null ? null : phone.getSpecificCarrierName();
chen xu25637222018-11-04 17:17:00 -08002405 } finally {
2406 Binder.restoreCallingIdentity(identity);
2407 }
2408 }
2409
chen xu651eec72018-11-11 19:03:44 -08002410 @Override
chen xu864e11c2018-12-06 22:10:03 -08002411 public int getCarrierIdFromMccMnc(int slotIndex, String mccmnc, boolean isSubscriptionMccMnc) {
2412 if (!isSubscriptionMccMnc) {
2413 enforceReadPrivilegedPermission("getCarrierIdFromMccMnc");
2414 }
chen xu651eec72018-11-11 19:03:44 -08002415 final Phone phone = PhoneFactory.getPhone(slotIndex);
2416 if (phone == null) {
2417 return TelephonyManager.UNKNOWN_CARRIER_ID;
2418 }
2419 final long identity = Binder.clearCallingIdentity();
2420 try {
2421 return CarrierResolver.getCarrierIdFromMccMnc(phone.getContext(), mccmnc);
2422 } finally {
2423 Binder.restoreCallingIdentity(identity);
2424 }
2425 }
2426
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002427 //
2428 // Internal helper methods.
2429 //
2430
Sanket Padaweee13a9b2016-03-08 17:30:28 -08002431 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002432 * Make sure the caller has the MODIFY_PHONE_STATE permission.
2433 *
2434 * @throws SecurityException if the caller does not have the required permission
2435 */
2436 private void enforceModifyPermission() {
2437 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.MODIFY_PHONE_STATE, null);
2438 }
2439
Shuo Qianf2b2df42019-11-13 17:43:31 -08002440 private void enforceActiveEmergencySessionPermission() {
2441 mApp.enforceCallingOrSelfPermission(
2442 android.Manifest.permission.READ_ACTIVE_EMERGENCY_SESSION, null);
2443 }
2444
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002445 /**
2446 * Make sure the caller has the CALL_PHONE permission.
2447 *
2448 * @throws SecurityException if the caller does not have the required permission
2449 */
2450 private void enforceCallPermission() {
2451 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.CALL_PHONE, null);
2452 }
2453
paulhu423b5f22019-08-23 19:17:33 +08002454 private void enforceSettingsPermission() {
2455 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.NETWORK_SETTINGS, null);
Stuart Scott8eef64f2015-04-08 15:13:54 -07002456 }
2457
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002458 private String createTelUrl(String number) {
2459 if (TextUtils.isEmpty(number)) {
2460 return null;
2461 }
2462
Jake Hambye994d462014-02-03 13:10:13 -08002463 return "tel:" + number;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002464 }
2465
Ihab Awadf9e92732013-12-05 18:02:52 -08002466 private static void log(String msg) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002467 Log.d(LOG_TAG, "[PhoneIntfMgr] " + msg);
2468 }
2469
Naveen Kalla1fd79bd2014-08-08 00:48:59 -07002470 private static void logv(String msg) {
2471 Log.v(LOG_TAG, "[PhoneIntfMgr] " + msg);
2472 }
2473
Ihab Awadf9e92732013-12-05 18:02:52 -08002474 private static void loge(String msg) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002475 Log.e(LOG_TAG, "[PhoneIntfMgr] " + msg);
2476 }
2477
Robert Greenwalt36b23af2015-07-06 17:59:14 -07002478 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002479 public int getActivePhoneType() {
Shishir Agrawala9f32182016-04-12 12:00:16 -07002480 return getActivePhoneTypeForSlot(getSlotForDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07002481 }
2482
Sanket Padawe356d7632015-06-22 14:03:32 -07002483 @Override
Sanket Padawe13bac7b2017-03-20 15:04:47 -07002484 public int getActivePhoneTypeForSlot(int slotIndex) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002485 final long identity = Binder.clearCallingIdentity();
2486 try {
2487 final Phone phone = PhoneFactory.getPhone(slotIndex);
2488 if (phone == null) {
2489 return PhoneConstants.PHONE_TYPE_NONE;
2490 } else {
2491 return phone.getPhoneType();
2492 }
2493 } finally {
2494 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07002495 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002496 }
2497
2498 /**
2499 * Returns the CDMA ERI icon index to display
2500 */
Robert Greenwalt36b23af2015-07-06 17:59:14 -07002501 @Override
2502 public int getCdmaEriIconIndex(String callingPackage) {
2503 return getCdmaEriIconIndexForSubscriber(getDefaultSubscription(), callingPackage);
Wink Saville36469e72014-06-11 15:17:00 -07002504 }
2505
Sanket Padawe356d7632015-06-22 14:03:32 -07002506 @Override
Robert Greenwalt36b23af2015-07-06 17:59:14 -07002507 public int getCdmaEriIconIndexForSubscriber(int subId, String callingPackage) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08002508 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
Jeff Davidson913390f2018-02-23 17:11:49 -08002509 mApp, subId, callingPackage, "getCdmaEriIconIndexForSubscriber")) {
Robert Greenwalt36b23af2015-07-06 17:59:14 -07002510 return -1;
2511 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002512
2513 final long identity = Binder.clearCallingIdentity();
2514 try {
2515 final Phone phone = getPhone(subId);
2516 if (phone != null) {
2517 return phone.getCdmaEriIconIndex();
2518 } else {
2519 return -1;
2520 }
2521 } finally {
2522 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07002523 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002524 }
2525
2526 /**
2527 * Returns the CDMA ERI icon mode,
2528 * 0 - ON
2529 * 1 - FLASHING
2530 */
Robert Greenwalt36b23af2015-07-06 17:59:14 -07002531 @Override
2532 public int getCdmaEriIconMode(String callingPackage) {
2533 return getCdmaEriIconModeForSubscriber(getDefaultSubscription(), callingPackage);
Wink Saville36469e72014-06-11 15:17:00 -07002534 }
2535
Sanket Padawe356d7632015-06-22 14:03:32 -07002536 @Override
Robert Greenwalt36b23af2015-07-06 17:59:14 -07002537 public int getCdmaEriIconModeForSubscriber(int subId, String callingPackage) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08002538 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
Jeff Davidson913390f2018-02-23 17:11:49 -08002539 mApp, subId, callingPackage, "getCdmaEriIconModeForSubscriber")) {
Robert Greenwalt36b23af2015-07-06 17:59:14 -07002540 return -1;
2541 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002542
2543 final long identity = Binder.clearCallingIdentity();
2544 try {
2545 final Phone phone = getPhone(subId);
2546 if (phone != null) {
2547 return phone.getCdmaEriIconMode();
2548 } else {
2549 return -1;
2550 }
2551 } finally {
2552 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07002553 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002554 }
2555
2556 /**
2557 * Returns the CDMA ERI text,
2558 */
Robert Greenwalt36b23af2015-07-06 17:59:14 -07002559 @Override
2560 public String getCdmaEriText(String callingPackage) {
2561 return getCdmaEriTextForSubscriber(getDefaultSubscription(), callingPackage);
Wink Saville36469e72014-06-11 15:17:00 -07002562 }
2563
Sanket Padawe356d7632015-06-22 14:03:32 -07002564 @Override
Robert Greenwalt36b23af2015-07-06 17:59:14 -07002565 public String getCdmaEriTextForSubscriber(int subId, String callingPackage) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08002566 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
Jeff Davidson913390f2018-02-23 17:11:49 -08002567 mApp, subId, callingPackage, "getCdmaEriIconTextForSubscriber")) {
Robert Greenwalt36b23af2015-07-06 17:59:14 -07002568 return null;
2569 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002570
2571 final long identity = Binder.clearCallingIdentity();
2572 try {
2573 final Phone phone = getPhone(subId);
2574 if (phone != null) {
2575 return phone.getCdmaEriText();
2576 } else {
2577 return null;
2578 }
2579 } finally {
2580 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07002581 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002582 }
2583
2584 /**
Junda Liuca05d5d2014-08-14 22:36:34 -07002585 * Returns the CDMA MDN.
2586 */
Sanket Padawe356d7632015-06-22 14:03:32 -07002587 @Override
Wink Savilleb564aae2014-10-23 10:18:09 -07002588 public String getCdmaMdn(int subId) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08002589 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
2590 mApp, subId, "getCdmaMdn");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002591
2592 final long identity = Binder.clearCallingIdentity();
2593 try {
2594 final Phone phone = getPhone(subId);
Nazanin Bakhshif782e562018-12-11 15:15:39 -08002595 if (phone != null && phone.getPhoneType() == PhoneConstants.PHONE_TYPE_CDMA) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002596 return phone.getLine1Number();
2597 } else {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08002598 loge("getCdmaMdn: no phone found. Invalid subId: " + subId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002599 return null;
2600 }
2601 } finally {
2602 Binder.restoreCallingIdentity(identity);
Junda Liuca05d5d2014-08-14 22:36:34 -07002603 }
2604 }
2605
2606 /**
2607 * Returns the CDMA MIN.
2608 */
Sanket Padawe356d7632015-06-22 14:03:32 -07002609 @Override
Wink Savilleb564aae2014-10-23 10:18:09 -07002610 public String getCdmaMin(int subId) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08002611 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
2612 mApp, subId, "getCdmaMin");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002613
2614 final long identity = Binder.clearCallingIdentity();
2615 try {
2616 final Phone phone = getPhone(subId);
2617 if (phone != null && phone.getPhoneType() == PhoneConstants.PHONE_TYPE_CDMA) {
2618 return phone.getCdmaMin();
2619 } else {
2620 return null;
2621 }
2622 } finally {
2623 Binder.restoreCallingIdentity(identity);
Junda Liuca05d5d2014-08-14 22:36:34 -07002624 }
2625 }
2626
Hall Liud892bec2018-11-30 14:51:45 -08002627 @Override
2628 public void requestNumberVerification(PhoneNumberRange range, long timeoutMillis,
2629 INumberVerificationCallback callback, String callingPackage) {
2630 if (mApp.checkCallingOrSelfPermission(android.Manifest.permission.MODIFY_PHONE_STATE)
2631 != PERMISSION_GRANTED) {
2632 throw new SecurityException("Caller must hold the MODIFY_PHONE_STATE permission");
2633 }
2634 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
2635
2636 String authorizedPackage = NumberVerificationManager.getAuthorizedPackage(mApp);
2637 if (!TextUtils.equals(callingPackage, authorizedPackage)) {
2638 throw new SecurityException("Calling package must be configured in the device config");
2639 }
2640
2641 if (range == null) {
2642 throw new NullPointerException("Range must be non-null");
2643 }
2644
2645 timeoutMillis = Math.min(timeoutMillis,
Hall Liubd069e32019-02-28 18:56:30 -08002646 TelephonyManager.getMaxNumberVerificationTimeoutMillis());
Hall Liud892bec2018-11-30 14:51:45 -08002647
2648 NumberVerificationManager.getInstance().requestVerification(range, callback, timeoutMillis);
2649 }
2650
Junda Liuca05d5d2014-08-14 22:36:34 -07002651 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002652 * Returns true if CDMA provisioning needs to run.
2653 */
2654 public boolean needsOtaServiceProvisioning() {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002655 final long identity = Binder.clearCallingIdentity();
2656 try {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08002657 return getDefaultPhone().needsOtaServiceProvisioning();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002658 } finally {
2659 Binder.restoreCallingIdentity(identity);
2660 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002661 }
2662
2663 /**
Shishir Agrawal76d5da92014-11-09 16:17:25 -08002664 * Sets the voice mail number of a given subId.
2665 */
2666 @Override
2667 public boolean setVoiceMailNumber(int subId, String alphaTag, String number) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08002668 TelephonyPermissions.enforceCallingOrSelfCarrierPrivilege(subId, "setVoiceMailNumber");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002669
2670 final long identity = Binder.clearCallingIdentity();
2671 try {
2672 Boolean success = (Boolean) sendRequest(CMD_SET_VOICEMAIL_NUMBER,
2673 new Pair<String, String>(alphaTag, number), new Integer(subId));
2674 return success;
2675 } finally {
2676 Binder.restoreCallingIdentity(identity);
2677 }
Shishir Agrawal76d5da92014-11-09 16:17:25 -08002678 }
2679
Ta-wei Yen87c49842016-05-13 21:19:52 -07002680 @Override
Ta-wei Yenc9df0432017-04-17 17:09:07 -07002681 public Bundle getVisualVoicemailSettings(String callingPackage, int subId) {
2682 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
Tyler Gunn77ee9382019-10-31 13:08:23 -07002683 TelecomManager tm = mApp.getSystemService(TelecomManager.class);
2684 String systemDialer = tm.getSystemDialerPackage();
Ta-wei Yenc9df0432017-04-17 17:09:07 -07002685 if (!TextUtils.equals(callingPackage, systemDialer)) {
2686 throw new SecurityException("caller must be system dialer");
2687 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002688
2689 final long identity = Binder.clearCallingIdentity();
2690 try {
2691 PhoneAccountHandle phoneAccountHandle = PhoneAccountHandleConverter.fromSubId(subId);
2692 if (phoneAccountHandle == null) {
2693 return null;
2694 }
Nazanin Bakhshif782e562018-12-11 15:15:39 -08002695 return VisualVoicemailSettingsUtil.dump(mApp, phoneAccountHandle);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002696 } finally {
2697 Binder.restoreCallingIdentity(identity);
Ta-wei Yenc9df0432017-04-17 17:09:07 -07002698 }
Ta-wei Yenc9df0432017-04-17 17:09:07 -07002699 }
2700
2701 @Override
Ta-wei Yen409ac562017-03-06 16:00:44 -08002702 public String getVisualVoicemailPackageName(String callingPackage, int subId) {
Ta-wei Yendca928f2017-01-10 16:17:08 -08002703 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
Jeff Davidson7e17e312018-02-13 18:17:36 -08002704 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
Jeff Davidson913390f2018-02-23 17:11:49 -08002705 mApp, subId, callingPackage, "getVisualVoicemailPackageName")) {
Ta-wei Yendca928f2017-01-10 16:17:08 -08002706 return null;
2707 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002708
Jeff Davidsona8e4e242018-03-15 17:16:18 -07002709 final long identity = Binder.clearCallingIdentity();
2710 try {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08002711 return RemoteVvmTaskManager.getRemotePackage(mApp, subId).getPackageName();
Jeff Davidsona8e4e242018-03-15 17:16:18 -07002712 } finally {
2713 Binder.restoreCallingIdentity(identity);
2714 }
Ta-wei Yendca928f2017-01-10 16:17:08 -08002715 }
2716
2717 @Override
Ta-wei Yenb6929602016-05-24 15:48:27 -07002718 public void enableVisualVoicemailSmsFilter(String callingPackage, int subId,
2719 VisualVoicemailSmsFilterSettings settings) {
2720 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002721
2722 final long identity = Binder.clearCallingIdentity();
2723 try {
2724 VisualVoicemailSmsFilterConfig.enableVisualVoicemailSmsFilter(
Nazanin Bakhshif782e562018-12-11 15:15:39 -08002725 mApp, callingPackage, subId, settings);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002726 } finally {
2727 Binder.restoreCallingIdentity(identity);
2728 }
Ta-wei Yen87c49842016-05-13 21:19:52 -07002729 }
2730
2731 @Override
Ta-wei Yenb6929602016-05-24 15:48:27 -07002732 public void disableVisualVoicemailSmsFilter(String callingPackage, int subId) {
2733 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002734
2735 final long identity = Binder.clearCallingIdentity();
2736 try {
2737 VisualVoicemailSmsFilterConfig.disableVisualVoicemailSmsFilter(
Nazanin Bakhshif782e562018-12-11 15:15:39 -08002738 mApp, callingPackage, subId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002739 } finally {
2740 Binder.restoreCallingIdentity(identity);
2741 }
Ta-wei Yen87c49842016-05-13 21:19:52 -07002742 }
2743
2744 @Override
Ta-wei Yenb6929602016-05-24 15:48:27 -07002745 public VisualVoicemailSmsFilterSettings getVisualVoicemailSmsFilterSettings(
2746 String callingPackage, int subId) {
2747 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002748
2749 final long identity = Binder.clearCallingIdentity();
2750 try {
2751 return VisualVoicemailSmsFilterConfig.getVisualVoicemailSmsFilterSettings(
Nazanin Bakhshif782e562018-12-11 15:15:39 -08002752 mApp, callingPackage, subId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002753 } finally {
2754 Binder.restoreCallingIdentity(identity);
2755 }
Ta-wei Yen87c49842016-05-13 21:19:52 -07002756 }
2757
2758 @Override
Ta-wei Yen30a69c82016-12-27 14:52:32 -08002759 public VisualVoicemailSmsFilterSettings getActiveVisualVoicemailSmsFilterSettings(int subId) {
Brad Ebinger35c841c2018-10-01 10:40:55 -07002760 enforceReadPrivilegedPermission("getActiveVisualVoicemailSmsFilterSettings");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002761
2762 final long identity = Binder.clearCallingIdentity();
2763 try {
2764 return VisualVoicemailSmsFilterConfig.getActiveVisualVoicemailSmsFilterSettings(
Nazanin Bakhshif782e562018-12-11 15:15:39 -08002765 mApp, subId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002766 } finally {
2767 Binder.restoreCallingIdentity(identity);
2768 }
Ta-wei Yen30a69c82016-12-27 14:52:32 -08002769 }
2770
2771 @Override
2772 public void sendVisualVoicemailSmsForSubscriber(String callingPackage, int subId,
2773 String number, int port, String text, PendingIntent sentIntent) {
2774 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
Ta-wei Yen527a9c02017-01-06 15:29:25 -08002775 enforceVisualVoicemailPackage(callingPackage, subId);
Ta-wei Yen30a69c82016-12-27 14:52:32 -08002776 enforceSendSmsPermission();
Amit Mahajandccb3f12019-05-13 13:48:32 -07002777 SmsController smsController = PhoneFactory.getSmsController();
2778 smsController.sendVisualVoicemailSmsForSubscriber(callingPackage, subId, number, port, text,
2779 sentIntent);
Ta-wei Yen87c49842016-05-13 21:19:52 -07002780 }
Amit Mahajandccb3f12019-05-13 13:48:32 -07002781
Shishir Agrawal76d5da92014-11-09 16:17:25 -08002782 /**
fionaxu0152e512016-11-14 13:36:14 -08002783 * Sets the voice activation state of a given subId.
2784 */
2785 @Override
2786 public void setVoiceActivationState(int subId, int activationState) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08002787 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
2788 mApp, subId, "setVoiceActivationState");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002789
2790 final long identity = Binder.clearCallingIdentity();
2791 try {
2792 final Phone phone = getPhone(subId);
2793 if (phone != null) {
2794 phone.setVoiceActivationState(activationState);
2795 } else {
2796 loge("setVoiceActivationState fails with invalid subId: " + subId);
2797 }
2798 } finally {
2799 Binder.restoreCallingIdentity(identity);
fionaxu0152e512016-11-14 13:36:14 -08002800 }
2801 }
2802
2803 /**
2804 * Sets the data activation state of a given subId.
2805 */
2806 @Override
2807 public void setDataActivationState(int subId, int activationState) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08002808 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
2809 mApp, subId, "setDataActivationState");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002810
2811 final long identity = Binder.clearCallingIdentity();
2812 try {
2813 final Phone phone = getPhone(subId);
2814 if (phone != null) {
2815 phone.setDataActivationState(activationState);
2816 } else {
Taesu Leef8fbed92019-10-07 18:47:02 +09002817 loge("setDataActivationState fails with invalid subId: " + subId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002818 }
2819 } finally {
2820 Binder.restoreCallingIdentity(identity);
fionaxu0152e512016-11-14 13:36:14 -08002821 }
2822 }
2823
2824 /**
2825 * Returns the voice activation state of a given subId.
2826 */
2827 @Override
2828 public int getVoiceActivationState(int subId, String callingPackage) {
Brad Ebinger35c841c2018-10-01 10:40:55 -07002829 enforceReadPrivilegedPermission("getVoiceActivationState");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002830
fionaxu0152e512016-11-14 13:36:14 -08002831 final Phone phone = getPhone(subId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002832 final long identity = Binder.clearCallingIdentity();
2833 try {
2834 if (phone != null) {
2835 return phone.getVoiceActivationState();
2836 } else {
2837 return TelephonyManager.SIM_ACTIVATION_STATE_UNKNOWN;
2838 }
2839 } finally {
2840 Binder.restoreCallingIdentity(identity);
fionaxu0152e512016-11-14 13:36:14 -08002841 }
2842 }
2843
2844 /**
2845 * Returns the data activation state of a given subId.
2846 */
2847 @Override
2848 public int getDataActivationState(int subId, String callingPackage) {
Brad Ebinger35c841c2018-10-01 10:40:55 -07002849 enforceReadPrivilegedPermission("getDataActivationState");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002850
fionaxu0152e512016-11-14 13:36:14 -08002851 final Phone phone = getPhone(subId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002852 final long identity = Binder.clearCallingIdentity();
2853 try {
2854 if (phone != null) {
2855 return phone.getDataActivationState();
2856 } else {
2857 return TelephonyManager.SIM_ACTIVATION_STATE_UNKNOWN;
2858 }
2859 } finally {
2860 Binder.restoreCallingIdentity(identity);
fionaxu0152e512016-11-14 13:36:14 -08002861 }
2862 }
2863
2864 /**
Wink Saville36469e72014-06-11 15:17:00 -07002865 * Returns the unread count of voicemails for a subId
2866 */
Sanket Padawe356d7632015-06-22 14:03:32 -07002867 @Override
Brad Ebingerf7664ba2018-11-29 12:43:38 -08002868 public int getVoiceMessageCountForSubscriber(int subId, String callingPackage) {
2869 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
2870 mApp, subId, callingPackage, "getVoiceMessageCountForSubscriber")) {
2871 return 0;
2872 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002873 final long identity = Binder.clearCallingIdentity();
2874 try {
2875 final Phone phone = getPhone(subId);
2876 if (phone != null) {
2877 return phone.getVoiceMessageCount();
2878 } else {
2879 return 0;
2880 }
2881 } finally {
2882 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07002883 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002884 }
2885
2886 /**
pkanwar8a4dcfb2017-01-19 13:43:16 -08002887 * returns true, if the device is in a state where both voice and data
2888 * are supported simultaneously. This can change based on location or network condition.
2889 */
2890 @Override
2891 public boolean isConcurrentVoiceAndDataAllowed(int subId) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002892 final long identity = Binder.clearCallingIdentity();
2893 try {
2894 final Phone phone = getPhone(subId);
2895 return (phone == null ? false : phone.isConcurrentVoiceAndDataAllowed());
2896 } finally {
2897 Binder.restoreCallingIdentity(identity);
2898 }
pkanwar8a4dcfb2017-01-19 13:43:16 -08002899 }
2900
2901 /**
fionaxu235cc5e2017-03-06 22:25:57 -08002902 * Send the dialer code if called from the current default dialer or the caller has
2903 * carrier privilege.
2904 * @param inputCode The dialer code to send
2905 */
2906 @Override
2907 public void sendDialerSpecialCode(String callingPackage, String inputCode) {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08002908 final Phone defaultPhone = getDefaultPhone();
fionaxu235cc5e2017-03-06 22:25:57 -08002909 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
Tyler Gunn77ee9382019-10-31 13:08:23 -07002910 TelecomManager tm = defaultPhone.getContext().getSystemService(TelecomManager.class);
2911 String defaultDialer = tm.getDefaultDialerPackage();
fionaxu235cc5e2017-03-06 22:25:57 -08002912 if (!TextUtils.equals(callingPackage, defaultDialer)) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08002913 TelephonyPermissions.enforceCallingOrSelfCarrierPrivilege(
2914 getDefaultSubscription(), "sendDialerSpecialCode");
fionaxu235cc5e2017-03-06 22:25:57 -08002915 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002916
2917 final long identity = Binder.clearCallingIdentity();
2918 try {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08002919 defaultPhone.sendDialerSpecialCode(inputCode);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002920 } finally {
2921 Binder.restoreCallingIdentity(identity);
2922 }
fionaxu235cc5e2017-03-06 22:25:57 -08002923 }
2924
Pengquan Menga1bb6272018-09-06 09:59:22 -07002925 @Override
2926 public int getNetworkSelectionMode(int subId) {
Pengquan Menge92a50d2018-09-21 15:54:48 -07002927 if (!isActiveSubscription(subId)) {
2928 return TelephonyManager.NETWORK_SELECTION_MODE_UNKNOWN;
2929 }
2930
Pengquan Menga1bb6272018-09-06 09:59:22 -07002931 return (int) sendRequest(CMD_GET_NETWORK_SELECTION_MODE, null /* argument */, subId);
2932 }
2933
Brad Ebinger35c841c2018-10-01 10:40:55 -07002934 @Override
Brad Ebingerb2b65522019-03-15 13:48:47 -07002935 public boolean isInEmergencySmsMode() {
2936 enforceReadPrivilegedPermission("isInEmergencySmsMode");
2937 final long identity = Binder.clearCallingIdentity();
2938 try {
2939 for (Phone phone : PhoneFactory.getPhones()) {
2940 if (phone.isInEmergencySmsMode()) {
2941 return true;
2942 }
2943 }
2944 } finally {
2945 Binder.restoreCallingIdentity(identity);
2946 }
2947 return false;
2948 }
2949
2950 @Override
Brad Ebinger9878b0b2018-11-08 17:43:22 -08002951 public void registerImsRegistrationCallback(int subId, IImsRegistrationCallback c)
2952 throws RemoteException {
2953 enforceReadPrivilegedPermission("registerImsRegistrationCallback");
Brad Ebinger77b832e2019-10-17 17:03:22 -07002954 if (!ImsManager.isImsSupportedOnDevice(mApp)) {
2955 throw new ServiceSpecificException(ImsException.CODE_ERROR_UNSUPPORTED_OPERATION,
2956 "IMS not available on device.");
2957 }
Brad Ebinger35c841c2018-10-01 10:40:55 -07002958 final long token = Binder.clearCallingIdentity();
2959 try {
Brad Ebinger4ae57f92019-01-09 16:51:30 -08002960 // TODO: Refactor to remove ImsManager dependence and query through ImsPhone directly.
Nazanin Bakhshif782e562018-12-11 15:15:39 -08002961 ImsManager.getInstance(mApp, getSlotIndexOrException(subId))
Brad Ebinger35c841c2018-10-01 10:40:55 -07002962 .addRegistrationCallbackForSubscription(c, subId);
Brad Ebinger1ce9c432019-07-16 13:19:44 -07002963 } catch (ImsException e) {
2964 throw new ServiceSpecificException(e.getCode());
Brad Ebinger35c841c2018-10-01 10:40:55 -07002965 } finally {
2966 Binder.restoreCallingIdentity(token);
2967 }
2968 }
2969
2970 @Override
Brad Ebinger9878b0b2018-11-08 17:43:22 -08002971 public void unregisterImsRegistrationCallback(int subId, IImsRegistrationCallback c) {
2972 enforceReadPrivilegedPermission("unregisterImsRegistrationCallback");
Brad Ebinger4ae57f92019-01-09 16:51:30 -08002973 if (!SubscriptionManager.isValidSubscriptionId(subId)) {
2974 throw new IllegalArgumentException("Invalid Subscription ID: " + subId);
2975 }
Meng Wangd20ad6b2019-09-19 17:37:13 -07002976 final long token = Binder.clearCallingIdentity();
2977 try {
2978 // TODO: Refactor to remove ImsManager dependence and query through ImsPhone.
2979 ImsManager.getInstance(mApp, getSlotIndexOrException(subId))
2980 .removeRegistrationCallbackForSubscription(c, subId);
2981 } catch (ImsException e) {
2982 Log.i(LOG_TAG, "unregisterImsRegistrationCallback: " + subId
2983 + "is inactive, ignoring unregister.");
2984 // If the subscription is no longer active, just return, since the callback
2985 // will already have been removed internally.
2986 } finally {
2987 Binder.restoreCallingIdentity(token);
2988 }
Brad Ebinger35c841c2018-10-01 10:40:55 -07002989 }
2990
Brad Ebinger774ba362019-10-22 17:36:18 -07002991 /**
2992 * Get the IMS service registration state for the MmTelFeature associated with this sub id.
2993 */
2994 @Override
2995 public void getImsMmTelRegistrationState(int subId, IIntegerConsumer consumer) {
2996 enforceReadPrivilegedPermission("getImsMmTelRegistrationState");
2997 if (!ImsManager.isImsSupportedOnDevice(mApp)) {
2998 throw new ServiceSpecificException(ImsException.CODE_ERROR_UNSUPPORTED_OPERATION,
2999 "IMS not available on device.");
3000 }
3001 final long token = Binder.clearCallingIdentity();
3002 try {
3003 Phone phone = getPhone(subId);
3004 if (phone == null) {
3005 Log.w(LOG_TAG, "getImsMmTelRegistrationState: called with an invalid subscription '"
3006 + subId + "'");
3007 throw new ServiceSpecificException(ImsException.CODE_ERROR_INVALID_SUBSCRIPTION);
3008 }
3009 phone.getImsRegistrationState(regState -> {
3010 try {
3011 consumer.accept((regState == null)
3012 ? RegistrationManager.REGISTRATION_STATE_NOT_REGISTERED : regState);
3013 } catch (RemoteException e) {
3014 // Ignore if the remote process is no longer available to call back.
3015 Log.w(LOG_TAG, "getImsMmTelRegistrationState: callback not available.");
3016 }
3017 });
3018 } finally {
3019 Binder.restoreCallingIdentity(token);
3020 }
3021 }
3022
3023 /**
3024 * Get the transport type for the IMS service registration state.
3025 */
3026 @Override
3027 public void getImsMmTelRegistrationTransportType(int subId, IIntegerConsumer consumer) {
3028 enforceReadPrivilegedPermission("getImsMmTelRegistrationTransportType");
3029 if (!ImsManager.isImsSupportedOnDevice(mApp)) {
3030 throw new ServiceSpecificException(ImsException.CODE_ERROR_UNSUPPORTED_OPERATION,
3031 "IMS not available on device.");
3032 }
3033 final long token = Binder.clearCallingIdentity();
3034 try {
3035 Phone phone = getPhone(subId);
3036 if (phone == null) {
3037 Log.w(LOG_TAG, "getImsMmTelRegistrationState: called with an invalid subscription '"
3038 + subId + "'");
3039 throw new ServiceSpecificException(ImsException.CODE_ERROR_INVALID_SUBSCRIPTION);
3040 }
3041 phone.getImsRegistrationTech(regTech -> {
3042 // Convert registration tech from ImsRegistrationImplBase -> RegistrationManager
3043 int regTechConverted = (regTech == null)
3044 ? ImsRegistrationImplBase.REGISTRATION_TECH_NONE : regTech;
3045 regTechConverted = RegistrationManager.IMS_REG_TO_ACCESS_TYPE_MAP.get(
3046 regTechConverted);
3047 try {
3048 consumer.accept(regTechConverted);
3049 } catch (RemoteException e) {
3050 // Ignore if the remote process is no longer available to call back.
3051 Log.w(LOG_TAG, "getImsMmTelRegistrationState: callback not available.");
3052 }
3053 });
3054 } finally {
3055 Binder.restoreCallingIdentity(token);
3056 }
3057 }
3058
Brad Ebinger35c841c2018-10-01 10:40:55 -07003059 @Override
Brad Ebinger9878b0b2018-11-08 17:43:22 -08003060 public void registerMmTelCapabilityCallback(int subId, IImsCapabilityCallback c)
3061 throws RemoteException {
3062 enforceReadPrivilegedPermission("registerMmTelCapabilityCallback");
Brad Ebinger77b832e2019-10-17 17:03:22 -07003063 if (!ImsManager.isImsSupportedOnDevice(mApp)) {
3064 throw new ServiceSpecificException(ImsException.CODE_ERROR_UNSUPPORTED_OPERATION,
3065 "IMS not available on device.");
3066 }
Brad Ebinger35c841c2018-10-01 10:40:55 -07003067 // TODO: Refactor to remove ImsManager dependence and query through ImsPhone directly.
3068 final long token = Binder.clearCallingIdentity();
3069 try {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08003070 ImsManager.getInstance(mApp, getSlotIndexOrException(subId))
Brad Ebinger35c841c2018-10-01 10:40:55 -07003071 .addCapabilitiesCallbackForSubscription(c, subId);
Brad Ebinger1ce9c432019-07-16 13:19:44 -07003072 } catch (ImsException e) {
3073 throw new ServiceSpecificException(e.getCode());
Brad Ebinger35c841c2018-10-01 10:40:55 -07003074 } finally {
3075 Binder.restoreCallingIdentity(token);
3076 }
3077 }
3078
3079 @Override
Brad Ebinger9878b0b2018-11-08 17:43:22 -08003080 public void unregisterMmTelCapabilityCallback(int subId, IImsCapabilityCallback c) {
3081 enforceReadPrivilegedPermission("unregisterMmTelCapabilityCallback");
Brad Ebinger4ae57f92019-01-09 16:51:30 -08003082 if (!SubscriptionManager.isValidSubscriptionId(subId)) {
3083 throw new IllegalArgumentException("Invalid Subscription ID: " + subId);
3084 }
Meng Wangd20ad6b2019-09-19 17:37:13 -07003085
3086 final long token = Binder.clearCallingIdentity();
3087 try {
3088 // TODO: Refactor to remove ImsManager dependence and query through ImsPhone.
3089 ImsManager.getInstance(mApp, getSlotIndexOrException(subId))
Brad Ebinger4ae57f92019-01-09 16:51:30 -08003090 .removeCapabilitiesCallbackForSubscription(c, subId);
Meng Wangd20ad6b2019-09-19 17:37:13 -07003091 } catch (ImsException e) {
3092 Log.i(LOG_TAG, "unregisterMmTelCapabilityCallback: " + subId
3093 + "is inactive, ignoring unregister.");
3094 // If the subscription is no longer active, just return, since the callback
3095 // will already have been removed internally.
3096 } finally {
3097 Binder.restoreCallingIdentity(token);
3098 }
Brad Ebinger35c841c2018-10-01 10:40:55 -07003099 }
3100
3101 @Override
Brad Ebinger9878b0b2018-11-08 17:43:22 -08003102 public boolean isCapable(int subId, int capability, int regTech) {
3103 enforceReadPrivilegedPermission("isCapable");
Brad Ebinger35c841c2018-10-01 10:40:55 -07003104 // TODO: Refactor to remove ImsManager dependence and query through ImsPhone directly.
3105 final long token = Binder.clearCallingIdentity();
3106 try {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08003107 return ImsManager.getInstance(mApp,
Brad Ebinger35c841c2018-10-01 10:40:55 -07003108 getSlotIndexOrException(subId)).queryMmTelCapability(capability, regTech);
Brad Ebinger1ce9c432019-07-16 13:19:44 -07003109 } catch (com.android.ims.ImsException e) {
Brad Ebinger35c841c2018-10-01 10:40:55 -07003110 Log.w(LOG_TAG, "IMS isCapable - service unavailable: " + e.getMessage());
3111 return false;
Brad Ebinger1ce9c432019-07-16 13:19:44 -07003112 } catch (ImsException e) {
Brad Ebinger6b5ac222019-02-04 14:36:52 -08003113 Log.i(LOG_TAG, "isCapable: " + subId + " is inactive, returning false.");
3114 return false;
Brad Ebinger35c841c2018-10-01 10:40:55 -07003115 } finally {
3116 Binder.restoreCallingIdentity(token);
3117 }
3118 }
3119
3120 @Override
Brad Ebinger9878b0b2018-11-08 17:43:22 -08003121 public boolean isAvailable(int subId, int capability, int regTech) {
3122 enforceReadPrivilegedPermission("isAvailable");
Brad Ebinger35c841c2018-10-01 10:40:55 -07003123 final long token = Binder.clearCallingIdentity();
3124 try {
3125 Phone phone = getPhone(subId);
3126 if (phone == null) return false;
3127 return phone.isImsCapabilityAvailable(capability, regTech);
3128 } finally {
3129 Binder.restoreCallingIdentity(token);
3130 }
3131 }
3132
Brad Ebinger77b832e2019-10-17 17:03:22 -07003133 /**
3134 * Determines if the MmTel feature capability is supported by the carrier configuration for this
3135 * subscription.
3136 * @param subId The subscription to use to check the configuration.
3137 * @param callback The callback that will be used to send the result.
3138 * @param capability The MmTelFeature capability that will be used to send the result.
3139 * @param transportType The transport type of the MmTelFeature capability.
3140 */
3141 @Override
3142 public void isMmTelCapabilitySupported(int subId, IIntegerConsumer callback, int capability,
3143 int transportType) {
3144 enforceReadPrivilegedPermission("isMmTelCapabilitySupported");
3145 if (!ImsManager.isImsSupportedOnDevice(mApp)) {
3146 throw new ServiceSpecificException(ImsException.CODE_ERROR_UNSUPPORTED_OPERATION,
3147 "IMS not available on device.");
3148 }
3149 final long token = Binder.clearCallingIdentity();
3150 try {
3151 int slotId = getSlotIndex(subId);
3152 if (slotId <= SubscriptionManager.INVALID_SIM_SLOT_INDEX) {
3153 Log.w(LOG_TAG, "isMmTelCapabilitySupported: called with an inactive subscription '"
3154 + subId + "'");
3155 throw new ServiceSpecificException(ImsException.CODE_ERROR_INVALID_SUBSCRIPTION);
3156 }
3157 ImsManager.getInstance(mApp, slotId).isSupported(capability,
3158 transportType, aBoolean -> {
3159 try {
3160 callback.accept((aBoolean == null) ? 0 : (aBoolean ? 1 : 0));
3161 } catch (RemoteException e) {
3162 Log.w(LOG_TAG, "isMmTelCapabilitySupported: remote caller is not "
3163 + "running. Ignore");
3164 }
3165 });
3166 } finally {
3167 Binder.restoreCallingIdentity(token);
3168 }
3169 }
3170
Brad Ebinger35c841c2018-10-01 10:40:55 -07003171 @Override
3172 public boolean isAdvancedCallingSettingEnabled(int subId) {
3173 enforceReadPrivilegedPermission("enforceReadPrivilegedPermission");
3174 // TODO: Refactor to remove ImsManager dependence and query through ImsPhone directly.
3175 final long token = Binder.clearCallingIdentity();
3176 try {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08003177 return ImsManager.getInstance(mApp,
Brad Ebinger35c841c2018-10-01 10:40:55 -07003178 getSlotIndexOrException(subId)).isEnhanced4gLteModeSettingEnabledByUser();
Brad Ebinger1ce9c432019-07-16 13:19:44 -07003179 } catch (ImsException e) {
3180 throw new ServiceSpecificException(e.getCode());
Brad Ebinger35c841c2018-10-01 10:40:55 -07003181 } finally {
3182 Binder.restoreCallingIdentity(token);
3183 }
3184 }
3185
3186 @Override
Brad Ebinger1c162042019-02-21 14:49:10 -08003187 public void setAdvancedCallingSettingEnabled(int subId, boolean isEnabled) {
Brad Ebinger35c841c2018-10-01 10:40:55 -07003188 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(mApp, subId,
Brad Ebinger1c162042019-02-21 14:49:10 -08003189 "setAdvancedCallingSettingEnabled");
Brad Ebinger35c841c2018-10-01 10:40:55 -07003190 final long identity = Binder.clearCallingIdentity();
3191 try {
3192 // TODO: Refactor to remove ImsManager dependence and query through ImsPhone directly.
Nazanin Bakhshif782e562018-12-11 15:15:39 -08003193 ImsManager.getInstance(mApp,
Brad Ebinger35c841c2018-10-01 10:40:55 -07003194 getSlotIndexOrException(subId)).setEnhanced4gLteModeSetting(isEnabled);
Brad Ebinger1ce9c432019-07-16 13:19:44 -07003195 } catch (ImsException e) {
3196 throw new ServiceSpecificException(e.getCode());
Brad Ebinger35c841c2018-10-01 10:40:55 -07003197 } finally {
3198 Binder.restoreCallingIdentity(identity);
3199 }
3200 }
3201
3202 @Override
Brad Ebinger9878b0b2018-11-08 17:43:22 -08003203 public boolean isVtSettingEnabled(int subId) {
3204 enforceReadPrivilegedPermission("isVtSettingEnabled");
Brad Ebinger35c841c2018-10-01 10:40:55 -07003205 final long identity = Binder.clearCallingIdentity();
3206 try {
3207 // TODO: Refactor to remove ImsManager dependence and query through ImsPhone directly.
Brad Ebinger1ce9c432019-07-16 13:19:44 -07003208 return ImsManager.getInstance(mApp, getSlotIndexOrException(subId)).isVtEnabledByUser();
3209 } catch (ImsException e) {
3210 throw new ServiceSpecificException(e.getCode());
Brad Ebinger35c841c2018-10-01 10:40:55 -07003211 } finally {
3212 Binder.restoreCallingIdentity(identity);
3213 }
3214 }
3215
3216 @Override
Brad Ebinger1c162042019-02-21 14:49:10 -08003217 public void setVtSettingEnabled(int subId, boolean isEnabled) {
Brad Ebinger35c841c2018-10-01 10:40:55 -07003218 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(mApp, subId,
Brad Ebinger1c162042019-02-21 14:49:10 -08003219 "setVtSettingEnabled");
Brad Ebinger35c841c2018-10-01 10:40:55 -07003220 final long identity = Binder.clearCallingIdentity();
3221 try {
3222 // TODO: Refactor to remove ImsManager dependence and query through ImsPhone directly.
Nazanin Bakhshif782e562018-12-11 15:15:39 -08003223 ImsManager.getInstance(mApp, getSlotIndexOrException(subId)).setVtSetting(isEnabled);
Brad Ebinger1ce9c432019-07-16 13:19:44 -07003224 } catch (ImsException e) {
3225 throw new ServiceSpecificException(e.getCode());
Brad Ebinger35c841c2018-10-01 10:40:55 -07003226 } finally {
3227 Binder.restoreCallingIdentity(identity);
3228 }
3229 }
3230
3231 @Override
3232 public boolean isVoWiFiSettingEnabled(int subId) {
3233 enforceReadPrivilegedPermission("isVoWiFiSettingEnabled");
3234 final long identity = Binder.clearCallingIdentity();
3235 try {
3236 // TODO: Refactor to remove ImsManager dependence and query through ImsPhone directly.
Nazanin Bakhshif782e562018-12-11 15:15:39 -08003237 return ImsManager.getInstance(mApp,
Brad Ebinger35c841c2018-10-01 10:40:55 -07003238 getSlotIndexOrException(subId)).isWfcEnabledByUser();
Brad Ebinger1ce9c432019-07-16 13:19:44 -07003239 } catch (ImsException e) {
3240 throw new ServiceSpecificException(e.getCode());
Brad Ebinger35c841c2018-10-01 10:40:55 -07003241 } finally {
3242 Binder.restoreCallingIdentity(identity);
3243 }
3244 }
3245
3246 @Override
Brad Ebinger1c162042019-02-21 14:49:10 -08003247 public void setVoWiFiSettingEnabled(int subId, boolean isEnabled) {
Brad Ebinger35c841c2018-10-01 10:40:55 -07003248 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(mApp, subId,
Brad Ebinger1c162042019-02-21 14:49:10 -08003249 "setVoWiFiSettingEnabled");
Brad Ebinger35c841c2018-10-01 10:40:55 -07003250 final long identity = Binder.clearCallingIdentity();
3251 try {
3252 // TODO: Refactor to remove ImsManager dependence and query through ImsPhone directly.
Nazanin Bakhshif782e562018-12-11 15:15:39 -08003253 ImsManager.getInstance(mApp, getSlotIndexOrException(subId)).setWfcSetting(isEnabled);
Brad Ebinger1ce9c432019-07-16 13:19:44 -07003254 } catch (ImsException e) {
3255 throw new ServiceSpecificException(e.getCode());
Brad Ebinger35c841c2018-10-01 10:40:55 -07003256 } finally {
3257 Binder.restoreCallingIdentity(identity);
3258 }
3259 }
3260
3261 @Override
3262 public boolean isVoWiFiRoamingSettingEnabled(int subId) {
3263 enforceReadPrivilegedPermission("isVoWiFiRoamingSettingEnabled");
3264 final long identity = Binder.clearCallingIdentity();
3265 try {
3266 // TODO: Refactor to remove ImsManager dependence and query through ImsPhone directly.
Nazanin Bakhshif782e562018-12-11 15:15:39 -08003267 return ImsManager.getInstance(mApp,
Brad Ebinger35c841c2018-10-01 10:40:55 -07003268 getSlotIndexOrException(subId)).isWfcRoamingEnabledByUser();
Brad Ebinger1ce9c432019-07-16 13:19:44 -07003269 } catch (ImsException e) {
3270 throw new ServiceSpecificException(e.getCode());
Brad Ebinger35c841c2018-10-01 10:40:55 -07003271 } finally {
3272 Binder.restoreCallingIdentity(identity);
3273 }
3274 }
3275
3276 @Override
Brad Ebinger1c162042019-02-21 14:49:10 -08003277 public void setVoWiFiRoamingSettingEnabled(int subId, boolean isEnabled) {
Brad Ebinger35c841c2018-10-01 10:40:55 -07003278 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(mApp, subId,
Brad Ebinger1c162042019-02-21 14:49:10 -08003279 "setVoWiFiRoamingSettingEnabled");
Brad Ebinger35c841c2018-10-01 10:40:55 -07003280 final long identity = Binder.clearCallingIdentity();
3281 try {
3282 // TODO: Refactor to remove ImsManager dependence and query through ImsPhone directly.
Nazanin Bakhshif782e562018-12-11 15:15:39 -08003283 ImsManager.getInstance(mApp,
Brad Ebinger35c841c2018-10-01 10:40:55 -07003284 getSlotIndexOrException(subId)).setWfcRoamingSetting(isEnabled);
Brad Ebinger1ce9c432019-07-16 13:19:44 -07003285 } catch (ImsException e) {
3286 throw new ServiceSpecificException(e.getCode());
Brad Ebinger35c841c2018-10-01 10:40:55 -07003287 } finally {
3288 Binder.restoreCallingIdentity(identity);
3289 }
3290 }
3291
3292 @Override
3293 public void setVoWiFiNonPersistent(int subId, boolean isCapable, int mode) {
3294 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(mApp, subId,
3295 "setVoWiFiNonPersistent");
3296 final long identity = Binder.clearCallingIdentity();
3297 try {
3298 // TODO: Refactor to remove ImsManager dependence and query through ImsPhone directly.
Nazanin Bakhshif782e562018-12-11 15:15:39 -08003299 ImsManager.getInstance(mApp,
Brad Ebinger2d29c012019-05-07 18:33:46 -07003300 getSlotIndexOrException(subId)).setWfcNonPersistent(isCapable, mode);
Brad Ebinger1ce9c432019-07-16 13:19:44 -07003301 } catch (ImsException e) {
3302 throw new ServiceSpecificException(e.getCode());
Brad Ebinger35c841c2018-10-01 10:40:55 -07003303 } finally {
3304 Binder.restoreCallingIdentity(identity);
3305 }
3306 }
3307
3308 @Override
3309 public int getVoWiFiModeSetting(int subId) {
3310 enforceReadPrivilegedPermission("getVoWiFiModeSetting");
3311 final long identity = Binder.clearCallingIdentity();
3312 try {
3313 // TODO: Refactor to remove ImsManager dependence and query through ImsPhone directly.
Nazanin Bakhshif782e562018-12-11 15:15:39 -08003314 return ImsManager.getInstance(mApp,
Brad Ebinger35c841c2018-10-01 10:40:55 -07003315 getSlotIndexOrException(subId)).getWfcMode(false /*isRoaming*/);
Brad Ebinger1ce9c432019-07-16 13:19:44 -07003316 } catch (ImsException e) {
3317 throw new ServiceSpecificException(e.getCode());
Brad Ebinger35c841c2018-10-01 10:40:55 -07003318 } finally {
3319 Binder.restoreCallingIdentity(identity);
3320 }
3321 }
3322
3323 @Override
3324 public void setVoWiFiModeSetting(int subId, int mode) {
3325 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(mApp, subId,
3326 "setVoWiFiModeSetting");
3327 final long identity = Binder.clearCallingIdentity();
3328 try {
3329 // TODO: Refactor to remove ImsManager dependence and query through ImsPhone directly.
Nazanin Bakhshif782e562018-12-11 15:15:39 -08003330 ImsManager.getInstance(mApp,
Brad Ebinger35c841c2018-10-01 10:40:55 -07003331 getSlotIndexOrException(subId)).setWfcMode(mode, false /*isRoaming*/);
Brad Ebinger1ce9c432019-07-16 13:19:44 -07003332 } catch (ImsException e) {
3333 throw new ServiceSpecificException(e.getCode());
Brad Ebinger35c841c2018-10-01 10:40:55 -07003334 } finally {
3335 Binder.restoreCallingIdentity(identity);
3336 }
3337 }
3338
3339 @Override
3340 public int getVoWiFiRoamingModeSetting(int subId) {
3341 enforceReadPrivilegedPermission("getVoWiFiRoamingModeSetting");
3342 final long identity = Binder.clearCallingIdentity();
3343 try {
3344 // TODO: Refactor to remove ImsManager dependence and query through ImsPhone directly.
Nazanin Bakhshif782e562018-12-11 15:15:39 -08003345 return ImsManager.getInstance(mApp,
Brad Ebinger35c841c2018-10-01 10:40:55 -07003346 getSlotIndexOrException(subId)).getWfcMode(true /*isRoaming*/);
Brad Ebinger1ce9c432019-07-16 13:19:44 -07003347 } catch (ImsException e) {
3348 throw new ServiceSpecificException(e.getCode());
Brad Ebinger35c841c2018-10-01 10:40:55 -07003349 } finally {
3350 Binder.restoreCallingIdentity(identity);
3351 }
3352 }
3353
3354 @Override
3355 public void setVoWiFiRoamingModeSetting(int subId, int mode) {
3356 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(mApp, subId,
3357 "setVoWiFiRoamingModeSetting");
3358 final long identity = Binder.clearCallingIdentity();
3359 try {
3360 // TODO: Refactor to remove ImsManager dependence and query through ImsPhone directly.
Nazanin Bakhshif782e562018-12-11 15:15:39 -08003361 ImsManager.getInstance(mApp,
Brad Ebinger35c841c2018-10-01 10:40:55 -07003362 getSlotIndexOrException(subId)).setWfcMode(mode, true /*isRoaming*/);
Brad Ebinger1ce9c432019-07-16 13:19:44 -07003363 } catch (ImsException e) {
3364 throw new ServiceSpecificException(e.getCode());
Brad Ebinger35c841c2018-10-01 10:40:55 -07003365 } finally {
3366 Binder.restoreCallingIdentity(identity);
3367 }
3368 }
3369
3370 @Override
3371 public void setRttCapabilitySetting(int subId, boolean isEnabled) {
3372 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(mApp, subId,
3373 "setRttCapabilityEnabled");
3374 final long identity = Binder.clearCallingIdentity();
3375 try {
3376 // TODO: Refactor to remove ImsManager dependence and query through ImsPhone directly.
Brad Ebinger1ce9c432019-07-16 13:19:44 -07003377 ImsManager.getInstance(mApp, getSlotIndexOrException(subId)).setRttEnabled(isEnabled);
3378 } 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
3386 public boolean isTtyOverVolteEnabled(int subId) {
3387 enforceReadPrivilegedPermission("isTtyOverVolteEnabled");
3388 final long identity = Binder.clearCallingIdentity();
3389 try {
3390 // TODO: Refactor to remove ImsManager dependence and query through ImsPhone directly.
Nazanin Bakhshif782e562018-12-11 15:15:39 -08003391 return ImsManager.getInstance(mApp,
Brad Ebinger35c841c2018-10-01 10:40:55 -07003392 getSlotIndexOrException(subId)).isTtyOnVoLteCapable();
Brad Ebinger1ce9c432019-07-16 13:19:44 -07003393 } catch (ImsException e) {
3394 throw new ServiceSpecificException(e.getCode());
Brad Ebinger35c841c2018-10-01 10:40:55 -07003395 } finally {
3396 Binder.restoreCallingIdentity(identity);
3397 }
3398 }
3399
Brad Ebingerdf5b4f02018-10-31 11:24:17 -07003400 @Override
3401 public void registerImsProvisioningChangedCallback(int subId, IImsConfigCallback callback) {
3402 enforceReadPrivilegedPermission("registerImsProvisioningChangedCallback");
3403 final long identity = Binder.clearCallingIdentity();
3404 try {
3405 // TODO: Refactor to remove ImsManager dependence and query through ImsPhone directly.
Nazanin Bakhshif782e562018-12-11 15:15:39 -08003406 ImsManager.getInstance(mApp, getSlotIndexOrException(subId))
Brad Ebinger4ae57f92019-01-09 16:51:30 -08003407 .addProvisioningCallbackForSubscription(callback, subId);
Brad Ebinger1ce9c432019-07-16 13:19:44 -07003408 } catch (ImsException e) {
3409 throw new ServiceSpecificException(e.getCode());
Brad Ebingerdf5b4f02018-10-31 11:24:17 -07003410 } finally {
3411 Binder.restoreCallingIdentity(identity);
3412 }
3413 }
3414
3415 @Override
3416 public void unregisterImsProvisioningChangedCallback(int subId, IImsConfigCallback callback) {
3417 enforceReadPrivilegedPermission("unregisterImsProvisioningChangedCallback");
3418 final long identity = Binder.clearCallingIdentity();
Brad Ebinger4ae57f92019-01-09 16:51:30 -08003419 if (!SubscriptionManager.isValidSubscriptionId(subId)) {
3420 throw new IllegalArgumentException("Invalid Subscription ID: " + subId);
3421 }
Brad Ebingerdf5b4f02018-10-31 11:24:17 -07003422 try {
3423 // TODO: Refactor to remove ImsManager dependence and query through ImsPhone directly.
Nazanin Bakhshif782e562018-12-11 15:15:39 -08003424 ImsManager.getInstance(mApp, getSlotIndexOrException(subId))
Brad Ebinger4ae57f92019-01-09 16:51:30 -08003425 .removeProvisioningCallbackForSubscription(callback, subId);
Brad Ebinger1ce9c432019-07-16 13:19:44 -07003426 } catch (ImsException e) {
Brad Ebinger4ae57f92019-01-09 16:51:30 -08003427 Log.i(LOG_TAG, "unregisterImsProvisioningChangedCallback: " + subId
3428 + "is inactive, ignoring unregister.");
3429 // If the subscription is no longer active, just return, since the callback will already
3430 // have been removed internally.
Brad Ebingerdf5b4f02018-10-31 11:24:17 -07003431 } finally {
3432 Binder.restoreCallingIdentity(identity);
3433 }
3434 }
3435
allenwtsu99c623b2020-01-03 18:24:23 +08003436
3437 private void checkModifyPhoneStatePermission(int subId, String message) {
3438 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(mApp, subId,
3439 message);
3440 }
3441
3442 private boolean isImsProvisioningRequired(int subId, int capability,
3443 boolean isMmtelCapability) {
3444 Phone phone = getPhone(subId);
3445 if (phone == null) {
3446 loge("phone instance null for subid " + subId);
3447 return false;
3448 }
3449 if (isMmtelCapability) {
3450 if (!doesImsCapabilityRequireProvisioning(phone.getContext(), subId, capability)) {
3451 return false;
3452 }
3453 } else {
3454 if (!doesRcsCapabilityRequireProvisioning(phone.getContext(), subId, capability)) {
3455 return false;
3456 }
3457 }
3458 return true;
3459 }
3460
3461 @Override
3462 public void setRcsProvisioningStatusForCapability(int subId, int capability,
3463 boolean isProvisioned) {
3464 checkModifyPhoneStatePermission(subId, "setRcsProvisioningStatusForCapability");
3465
3466 final long identity = Binder.clearCallingIdentity();
3467 try {
3468 // TODO: Refactor to remove ImsManager dependence and query through ImsPhone directly.
3469 if (!isImsProvisioningRequired(subId, capability, false)) {
3470 return;
3471 }
3472
3473 // this capability requires provisioning, route to the correct API.
3474 ImsManager ims = ImsManager.getInstance(mApp, getSlotIndex(subId));
3475 switch (capability) {
3476 case RcsFeature.RcsImsCapabilities.CAPABILITY_TYPE_OPTIONS_UCE:
3477 case RcsFeature.RcsImsCapabilities.CAPABILITY_TYPE_PRESENCE_UCE:
3478 ims.setEabProvisioned(isProvisioned);
3479 break;
3480 default: {
3481 throw new IllegalArgumentException("Tried to set provisioning for "
3482 + "rcs capability '" + capability + "', which does not require "
3483 + "provisioning.");
3484 }
3485 }
3486 } finally {
3487 Binder.restoreCallingIdentity(identity);
3488 }
3489
3490 }
3491
3492
3493 @Override
3494 public boolean getRcsProvisioningStatusForCapability(int subId, int capability) {
3495 enforceReadPrivilegedPermission("getRcsProvisioningStatusForCapability");
3496 final long identity = Binder.clearCallingIdentity();
3497 try {
3498 // TODO: Refactor to remove ImsManager dependence and query through ImsPhone directly.
3499 if (!isImsProvisioningRequired(subId, capability, false)) {
3500 return true;
3501 }
3502
3503 ImsManager ims = ImsManager.getInstance(mApp, getSlotIndex(subId));
3504 switch (capability) {
3505 case RcsFeature.RcsImsCapabilities.CAPABILITY_TYPE_OPTIONS_UCE:
3506 case RcsFeature.RcsImsCapabilities.CAPABILITY_TYPE_PRESENCE_UCE:
3507 return ims.isEabProvisionedOnDevice();
3508
3509 default: {
3510 throw new IllegalArgumentException("Tried to get rcs provisioning for "
3511 + "capability '" + capability + "', which does not require "
3512 + "provisioning.");
3513 }
3514 }
3515
3516 } finally {
3517 Binder.restoreCallingIdentity(identity);
3518 }
3519 }
3520
Brad Ebingerdf5b4f02018-10-31 11:24:17 -07003521 @Override
Brad Ebinger1c8542e2019-01-14 13:43:14 -08003522 public void setImsProvisioningStatusForCapability(int subId, int capability, int tech,
3523 boolean isProvisioned) {
3524 if (tech != ImsRegistrationImplBase.REGISTRATION_TECH_IWLAN
3525 && tech != ImsRegistrationImplBase.REGISTRATION_TECH_LTE) {
3526 throw new IllegalArgumentException("Registration technology '" + tech + "' is invalid");
3527 }
allenwtsu99c623b2020-01-03 18:24:23 +08003528 checkModifyPhoneStatePermission(subId, "setImsProvisioningStatusForCapability");
Brad Ebinger1c8542e2019-01-14 13:43:14 -08003529 final long identity = Binder.clearCallingIdentity();
3530 try {
3531 // TODO: Refactor to remove ImsManager dependence and query through ImsPhone directly.
allenwtsu99c623b2020-01-03 18:24:23 +08003532 if (!isImsProvisioningRequired(subId, capability, true)) {
Brad Ebinger1c8542e2019-01-14 13:43:14 -08003533 return;
3534 }
3535
3536 // this capability requires provisioning, route to the correct API.
3537 ImsManager ims = ImsManager.getInstance(mApp, getSlotIndex(subId));
3538 switch (capability) {
3539 case MmTelFeature.MmTelCapabilities.CAPABILITY_TYPE_VOICE: {
3540 if (tech == ImsRegistrationImplBase.REGISTRATION_TECH_LTE) {
3541 ims.setVolteProvisioned(isProvisioned);
3542 } else if (tech == ImsRegistrationImplBase.REGISTRATION_TECH_IWLAN) {
3543 ims.setWfcProvisioned(isProvisioned);
3544 }
3545 break;
3546 }
3547 case MmTelFeature.MmTelCapabilities.CAPABILITY_TYPE_VIDEO: {
3548 // There is currently no difference in VT provisioning type.
3549 ims.setVtProvisioned(isProvisioned);
3550 break;
3551 }
3552 case MmTelFeature.MmTelCapabilities.CAPABILITY_TYPE_UT: {
3553 // There is no "deprecated" UT provisioning mechanism through ImsConfig, so
3554 // change the capability of the feature instead if needed.
3555 if (isMmTelCapabilityProvisionedInCache(subId, capability, tech)
3556 == isProvisioned) {
3557 // No change in provisioning.
3558 return;
3559 }
3560 cacheMmTelCapabilityProvisioning(subId, capability, tech, isProvisioned);
3561 try {
3562 ims.changeMmTelCapability(capability, tech, isProvisioned);
Brad Ebinger1ce9c432019-07-16 13:19:44 -07003563 } catch (com.android.ims.ImsException e) {
Brad Ebinger1c8542e2019-01-14 13:43:14 -08003564 loge("setImsProvisioningStatusForCapability: couldn't change UT capability"
3565 + ", Exception" + e.getMessage());
3566 }
3567 break;
3568 }
3569 default: {
allenwtsu99c623b2020-01-03 18:24:23 +08003570 throw new IllegalArgumentException("Tried to set provisioning for "
3571 + "MmTel capability '" + capability + "', which does not require "
3572 + "provisioning. ");
Brad Ebinger1c8542e2019-01-14 13:43:14 -08003573 }
3574 }
3575
3576 } finally {
3577 Binder.restoreCallingIdentity(identity);
3578 }
3579 }
3580
3581 @Override
3582 public boolean getImsProvisioningStatusForCapability(int subId, int capability, int tech) {
3583 if (tech != ImsRegistrationImplBase.REGISTRATION_TECH_IWLAN
3584 && tech != ImsRegistrationImplBase.REGISTRATION_TECH_LTE) {
3585 throw new IllegalArgumentException("Registration technology '" + tech + "' is invalid");
3586 }
3587 enforceReadPrivilegedPermission("getProvisioningStatusForCapability");
3588 final long identity = Binder.clearCallingIdentity();
3589 try {
3590 // TODO: Refactor to remove ImsManager dependence and query through ImsPhone directly.
allenwtsu99c623b2020-01-03 18:24:23 +08003591 if (!isImsProvisioningRequired(subId, capability, true)) {
Brad Ebinger1c8542e2019-01-14 13:43:14 -08003592 return true;
3593 }
3594
3595 ImsManager ims = ImsManager.getInstance(mApp, getSlotIndex(subId));
3596 switch (capability) {
3597 case MmTelFeature.MmTelCapabilities.CAPABILITY_TYPE_VOICE: {
3598 if (tech == ImsRegistrationImplBase.REGISTRATION_TECH_LTE) {
3599 return ims.isVolteProvisionedOnDevice();
3600 } else if (tech == ImsRegistrationImplBase.REGISTRATION_TECH_IWLAN) {
3601 return ims.isWfcProvisionedOnDevice();
3602 }
3603 // This should never happen, since we are checking tech above to make sure it
3604 // is either LTE or IWLAN.
3605 throw new IllegalArgumentException("Invalid radio technology for voice "
3606 + "capability.");
3607 }
3608 case MmTelFeature.MmTelCapabilities.CAPABILITY_TYPE_VIDEO: {
3609 // There is currently no difference in VT provisioning type.
3610 return ims.isVtProvisionedOnDevice();
3611 }
3612 case MmTelFeature.MmTelCapabilities.CAPABILITY_TYPE_UT: {
3613 // There is no "deprecated" UT provisioning mechanism, so get from shared prefs.
3614 return isMmTelCapabilityProvisionedInCache(subId, capability, tech);
3615 }
3616 default: {
allenwtsu99c623b2020-01-03 18:24:23 +08003617 throw new IllegalArgumentException(
3618 "Tried to get provisioning for MmTel capability '" + capability
3619 + "', which does not require provisioning.");
Brad Ebinger1c8542e2019-01-14 13:43:14 -08003620 }
3621 }
3622
3623 } finally {
3624 Binder.restoreCallingIdentity(identity);
3625 }
3626 }
3627
3628 @Override
3629 public boolean isMmTelCapabilityProvisionedInCache(int subId, int capability, int tech) {
3630 if (tech != ImsRegistrationImplBase.REGISTRATION_TECH_IWLAN
3631 && tech != ImsRegistrationImplBase.REGISTRATION_TECH_LTE) {
3632 throw new IllegalArgumentException("Registration technology '" + tech + "' is invalid");
3633 }
3634 enforceReadPrivilegedPermission("isMmTelCapabilityProvisionedInCache");
3635 int provisionedBits = getMmTelCapabilityProvisioningBitfield(subId, tech);
3636 return (provisionedBits & capability) > 0;
3637 }
3638
3639 @Override
3640 public void cacheMmTelCapabilityProvisioning(int subId, int capability, int tech,
3641 boolean isProvisioned) {
3642 if (tech != ImsRegistrationImplBase.REGISTRATION_TECH_IWLAN
3643 && tech != ImsRegistrationImplBase.REGISTRATION_TECH_LTE) {
3644 throw new IllegalArgumentException("Registration technology '" + tech + "' is invalid");
3645 }
3646 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(mApp, subId,
3647 "setProvisioningStatusForCapability");
3648 int provisionedBits = getMmTelCapabilityProvisioningBitfield(subId, tech);
3649 // If the current provisioning status for capability already matches isProvisioned,
3650 // do nothing.
3651 if (((provisionedBits & capability) > 0) == isProvisioned) {
3652 return;
3653 }
3654 if (isProvisioned) {
3655 setMmTelCapabilityProvisioningBitfield(subId, tech, (provisionedBits | capability));
3656 } else {
3657 setMmTelCapabilityProvisioningBitfield(subId, tech, (provisionedBits & ~capability));
3658 }
3659 }
3660
3661 /**
3662 * @return the bitfield containing the MmTel provisioning for the provided subscription and
3663 * technology. The bitfield should mirror the bitfield defined by
3664 * {@link MmTelFeature.MmTelCapabilities.MmTelCapability}.
3665 */
3666 private int getMmTelCapabilityProvisioningBitfield(int subId, int tech) {
3667 String key = getMmTelProvisioningKey(subId, tech);
3668 // Default is no capabilities are provisioned.
3669 return mTelephonySharedPreferences.getInt(key, 0 /*default*/);
3670 }
3671
3672 /**
3673 * Sets the MmTel capability provisioning bitfield (defined by
3674 * {@link MmTelFeature.MmTelCapabilities.MmTelCapability}) for the subscription and
3675 * technology specified.
3676 *
3677 * Note: This is a synchronous command and should not be called on UI thread.
3678 */
3679 private void setMmTelCapabilityProvisioningBitfield(int subId, int tech, int newField) {
3680 final SharedPreferences.Editor editor = mTelephonySharedPreferences.edit();
3681 String key = getMmTelProvisioningKey(subId, tech);
3682 editor.putInt(key, newField);
3683 editor.commit();
3684 }
3685
3686 private static String getMmTelProvisioningKey(int subId, int tech) {
3687 // resulting key is provision_ims_mmtel_{subId}_{tech}
3688 return PREF_PROVISION_IMS_MMTEL_PREFIX + subId + "_" + tech;
3689 }
3690
3691 /**
3692 * Query CarrierConfig to see if the specified capability requires provisioning for the
3693 * carrier associated with the subscription id.
3694 */
3695 private boolean doesImsCapabilityRequireProvisioning(Context context, int subId,
3696 int capability) {
3697 CarrierConfigManager configManager = new CarrierConfigManager(context);
3698 PersistableBundle c = configManager.getConfigForSubId(subId);
3699 boolean requireUtProvisioning = c.getBoolean(
Brad Ebinger076903f2019-05-13 10:00:22 -07003700 CarrierConfigManager.KEY_CARRIER_SUPPORTS_SS_OVER_UT_BOOL, false)
Brad Ebinger1c8542e2019-01-14 13:43:14 -08003701 && c.getBoolean(CarrierConfigManager.KEY_CARRIER_UT_PROVISIONING_REQUIRED_BOOL,
3702 false);
3703 boolean requireVoiceVtProvisioning = c.getBoolean(
3704 CarrierConfigManager.KEY_CARRIER_VOLTE_PROVISIONING_REQUIRED_BOOL, false);
3705
3706 // First check to make sure that the capability requires provisioning.
3707 switch (capability) {
3708 case MmTelFeature.MmTelCapabilities.CAPABILITY_TYPE_VOICE:
3709 // intentional fallthrough
3710 case MmTelFeature.MmTelCapabilities.CAPABILITY_TYPE_VIDEO: {
3711 if (requireVoiceVtProvisioning) {
3712 // Voice and Video requires provisioning
3713 return true;
3714 }
3715 break;
3716 }
3717 case MmTelFeature.MmTelCapabilities.CAPABILITY_TYPE_UT: {
3718 if (requireUtProvisioning) {
3719 // UT requires provisioning
3720 return true;
3721 }
3722 break;
3723 }
3724 }
3725 return false;
3726 }
3727
allenwtsu99c623b2020-01-03 18:24:23 +08003728 private boolean doesRcsCapabilityRequireProvisioning(Context context, int subId,
3729 int capability) {
3730 CarrierConfigManager configManager = new CarrierConfigManager(context);
3731 PersistableBundle c = configManager.getConfigForSubId(subId);
3732
3733 boolean requireRcsProvisioning = c.getBoolean(
3734 CarrierConfigManager.KEY_CARRIER_RCS_PROVISIONING_REQUIRED_BOOL, false);
3735
3736 // First check to make sure that the capability requires provisioning.
3737 switch (capability) {
3738 case RcsFeature.RcsImsCapabilities.CAPABILITY_TYPE_PRESENCE_UCE:
3739 // intentional fallthrough
3740 case RcsFeature.RcsImsCapabilities.CAPABILITY_TYPE_OPTIONS_UCE: {
3741 if (requireRcsProvisioning) {
3742 // OPTION or PRESENCE requires provisioning
3743 return true;
3744 }
3745 break;
3746 }
3747 }
3748 return false;
3749 }
3750
Brad Ebinger1c8542e2019-01-14 13:43:14 -08003751 @Override
Brad Ebingerdf5b4f02018-10-31 11:24:17 -07003752 public int getImsProvisioningInt(int subId, int key) {
Brad Ebinger1c8542e2019-01-14 13:43:14 -08003753 if (!SubscriptionManager.isValidSubscriptionId(subId)) {
3754 throw new IllegalArgumentException("Invalid Subscription id '" + subId + "'");
3755 }
Brad Ebingerdf5b4f02018-10-31 11:24:17 -07003756 enforceReadPrivilegedPermission("getImsProvisioningInt");
3757 final long identity = Binder.clearCallingIdentity();
3758 try {
3759 // TODO: Refactor to remove ImsManager dependence and query through ImsPhone directly.
Brad Ebinger1c8542e2019-01-14 13:43:14 -08003760 int slotId = getSlotIndex(subId);
3761 if (slotId <= SubscriptionManager.INVALID_SIM_SLOT_INDEX) {
3762 Log.w(LOG_TAG, "getImsProvisioningInt: called with an inactive subscription '"
3763 + subId + "' for key:" + key);
3764 return ImsConfigImplBase.CONFIG_RESULT_UNKNOWN;
3765 }
3766 return ImsManager.getInstance(mApp, slotId).getConfigInterface().getConfigInt(key);
Brad Ebinger1ce9c432019-07-16 13:19:44 -07003767 } catch (com.android.ims.ImsException e) {
Brad Ebinger1c8542e2019-01-14 13:43:14 -08003768 Log.w(LOG_TAG, "getImsProvisioningInt: ImsService is not available for subscription '"
3769 + subId + "' for key:" + key);
3770 return ImsConfigImplBase.CONFIG_RESULT_UNKNOWN;
Brad Ebingerdf5b4f02018-10-31 11:24:17 -07003771 } finally {
3772 Binder.restoreCallingIdentity(identity);
3773 }
3774 }
3775
3776 @Override
3777 public String getImsProvisioningString(int subId, int key) {
Brad Ebinger1c8542e2019-01-14 13:43:14 -08003778 if (!SubscriptionManager.isValidSubscriptionId(subId)) {
3779 throw new IllegalArgumentException("Invalid Subscription id '" + subId + "'");
3780 }
Brad Ebingerdf5b4f02018-10-31 11:24:17 -07003781 enforceReadPrivilegedPermission("getImsProvisioningString");
3782 final long identity = Binder.clearCallingIdentity();
3783 try {
3784 // TODO: Refactor to remove ImsManager dependence and query through ImsPhone directly.
Brad Ebinger1c8542e2019-01-14 13:43:14 -08003785 int slotId = getSlotIndex(subId);
3786 if (slotId <= SubscriptionManager.INVALID_SIM_SLOT_INDEX) {
3787 Log.w(LOG_TAG, "getImsProvisioningString: called for an inactive subscription id '"
3788 + subId + "' for key:" + key);
3789 return ProvisioningManager.STRING_QUERY_RESULT_ERROR_GENERIC;
3790 }
3791 return ImsManager.getInstance(mApp, slotId).getConfigInterface().getConfigString(key);
Brad Ebinger1ce9c432019-07-16 13:19:44 -07003792 } catch (com.android.ims.ImsException e) {
Brad Ebinger1c8542e2019-01-14 13:43:14 -08003793 Log.w(LOG_TAG, "getImsProvisioningString: ImsService is not available for sub '"
3794 + subId + "' for key:" + key);
3795 return ProvisioningManager.STRING_QUERY_RESULT_ERROR_NOT_READY;
Brad Ebingerdf5b4f02018-10-31 11:24:17 -07003796 } finally {
3797 Binder.restoreCallingIdentity(identity);
3798 }
3799 }
3800
3801 @Override
3802 public int setImsProvisioningInt(int subId, int key, int value) {
Brad Ebinger1c8542e2019-01-14 13:43:14 -08003803 if (!SubscriptionManager.isValidSubscriptionId(subId)) {
3804 throw new IllegalArgumentException("Invalid Subscription id '" + subId + "'");
3805 }
Brad Ebinger3d0b34e2018-11-15 14:13:12 -08003806 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(mApp, subId,
3807 "setImsProvisioningInt");
Brad Ebingerdf5b4f02018-10-31 11:24:17 -07003808 final long identity = Binder.clearCallingIdentity();
3809 try {
3810 // TODO: Refactor to remove ImsManager dependence and query through ImsPhone directly.
Brad Ebinger1c8542e2019-01-14 13:43:14 -08003811 int slotId = getSlotIndex(subId);
3812 if (slotId <= SubscriptionManager.INVALID_SIM_SLOT_INDEX) {
3813 Log.w(LOG_TAG, "setImsProvisioningInt: called with an inactive subscription id '"
3814 + subId + "' for key:" + key);
3815 return ImsConfigImplBase.CONFIG_RESULT_FAILED;
3816 }
3817 return ImsManager.getInstance(mApp, slotId).getConfigInterface().setConfig(key, value);
Brad Ebinger1ce9c432019-07-16 13:19:44 -07003818 } catch (com.android.ims.ImsException e) {
Brad Ebinger1c8542e2019-01-14 13:43:14 -08003819 Log.w(LOG_TAG, "setImsProvisioningInt: ImsService unavailable for sub '" + subId
3820 + "' for key:" + key);
3821 return ImsConfigImplBase.CONFIG_RESULT_FAILED;
Brad Ebingerdf5b4f02018-10-31 11:24:17 -07003822 } finally {
3823 Binder.restoreCallingIdentity(identity);
3824 }
3825 }
3826
3827 @Override
3828 public int setImsProvisioningString(int subId, int key, String value) {
Brad Ebinger1c8542e2019-01-14 13:43:14 -08003829 if (!SubscriptionManager.isValidSubscriptionId(subId)) {
3830 throw new IllegalArgumentException("Invalid Subscription id '" + subId + "'");
3831 }
Brad Ebinger3d0b34e2018-11-15 14:13:12 -08003832 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(mApp, subId,
3833 "setImsProvisioningString");
Brad Ebingerdf5b4f02018-10-31 11:24:17 -07003834 final long identity = Binder.clearCallingIdentity();
3835 try {
3836 // TODO: Refactor to remove ImsManager dependence and query through ImsPhone directly.
Brad Ebinger1c8542e2019-01-14 13:43:14 -08003837 int slotId = getSlotIndex(subId);
3838 if (slotId <= SubscriptionManager.INVALID_SIM_SLOT_INDEX) {
3839 Log.w(LOG_TAG, "setImsProvisioningString: called with an inactive subscription id '"
3840 + subId + "' for key:" + key);
3841 return ImsConfigImplBase.CONFIG_RESULT_FAILED;
3842 }
3843 return ImsManager.getInstance(mApp, slotId).getConfigInterface().setConfig(key, value);
Brad Ebinger1ce9c432019-07-16 13:19:44 -07003844 } catch (com.android.ims.ImsException e) {
Brad Ebinger1c8542e2019-01-14 13:43:14 -08003845 Log.w(LOG_TAG, "setImsProvisioningString: ImsService unavailable for sub '" + subId
3846 + "' for key:" + key);
3847 return ImsConfigImplBase.CONFIG_RESULT_FAILED;
Brad Ebingerdf5b4f02018-10-31 11:24:17 -07003848 } finally {
3849 Binder.restoreCallingIdentity(identity);
3850 }
3851 }
3852
Brad Ebinger1ce9c432019-07-16 13:19:44 -07003853 private int getSlotIndexOrException(int subId) throws ImsException {
Brad Ebinger35c841c2018-10-01 10:40:55 -07003854 int slotId = SubscriptionManager.getSlotIndex(subId);
3855 if (!SubscriptionManager.isValidSlotIndex(slotId)) {
Brad Ebinger1ce9c432019-07-16 13:19:44 -07003856 throw new ImsException("Invalid Subscription Id, subId=" + subId,
3857 ImsException.CODE_ERROR_INVALID_SUBSCRIPTION);
Brad Ebinger35c841c2018-10-01 10:40:55 -07003858 }
3859 return slotId;
3860 }
3861
Brad Ebinger1c8542e2019-01-14 13:43:14 -08003862 private int getSlotIndex(int subId) {
3863 int slotId = SubscriptionManager.getSlotIndex(subId);
3864 if (!SubscriptionManager.isValidSlotIndex(slotId)) {
3865 return SubscriptionManager.INVALID_SIM_SLOT_INDEX;
3866 }
3867 return slotId;
3868 }
3869
Wink Saville36469e72014-06-11 15:17:00 -07003870 /**
Nathan Harold9042f0b2019-05-21 15:51:27 -07003871 * Returns the data network type for a subId; does not throw SecurityException.
Wink Saville36469e72014-06-11 15:17:00 -07003872 */
3873 @Override
Robert Greenwalt36b23af2015-07-06 17:59:14 -07003874 public int getNetworkTypeForSubscriber(int subId, String callingPackage) {
Nathan Haroldef60dba2019-05-22 13:55:14 -07003875 final int targetSdk = getTargetSdk(callingPackage);
3876 if (targetSdk > android.os.Build.VERSION_CODES.Q) {
3877 return getDataNetworkTypeForSubscriber(subId, callingPackage);
3878 } else if (targetSdk == android.os.Build.VERSION_CODES.Q
Nathan Harold9042f0b2019-05-21 15:51:27 -07003879 && !TelephonyPermissions.checkCallingOrSelfReadPhoneStateNoThrow(
3880 mApp, subId, callingPackage, "getNetworkTypeForSubscriber")) {
Robert Greenwalt36b23af2015-07-06 17:59:14 -07003881 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
3882 }
Robert Greenwalta5dcfcb2015-07-10 09:06:29 -07003883
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003884 final long identity = Binder.clearCallingIdentity();
3885 try {
3886 final Phone phone = getPhone(subId);
3887 if (phone != null) {
3888 return phone.getServiceState().getDataNetworkType();
3889 } else {
3890 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
3891 }
3892 } finally {
3893 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07003894 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003895 }
3896
3897 /**
3898 * Returns the data network type
3899 */
3900 @Override
Robert Greenwalt36b23af2015-07-06 17:59:14 -07003901 public int getDataNetworkType(String callingPackage) {
3902 return getDataNetworkTypeForSubscriber(getDefaultSubscription(), callingPackage);
Wink Saville36469e72014-06-11 15:17:00 -07003903 }
3904
3905 /**
3906 * Returns the data network type for a subId
3907 */
3908 @Override
Robert Greenwalt36b23af2015-07-06 17:59:14 -07003909 public int getDataNetworkTypeForSubscriber(int subId, String callingPackage) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08003910 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
Jeff Davidson913390f2018-02-23 17:11:49 -08003911 mApp, subId, callingPackage, "getDataNetworkTypeForSubscriber")) {
Robert Greenwalt36b23af2015-07-06 17:59:14 -07003912 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
3913 }
3914
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003915 final long identity = Binder.clearCallingIdentity();
3916 try {
3917 final Phone phone = getPhone(subId);
3918 if (phone != null) {
3919 return phone.getServiceState().getDataNetworkType();
3920 } else {
3921 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
3922 }
3923 } finally {
3924 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07003925 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003926 }
3927
3928 /**
Wink Saville36469e72014-06-11 15:17:00 -07003929 * Returns the Voice network type for a subId
3930 */
3931 @Override
Robert Greenwalta5dcfcb2015-07-10 09:06:29 -07003932 public int getVoiceNetworkTypeForSubscriber(int subId, String callingPackage) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08003933 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
Jeff Davidson913390f2018-02-23 17:11:49 -08003934 mApp, subId, callingPackage, "getDataNetworkTypeForSubscriber")) {
Robert Greenwalta5dcfcb2015-07-10 09:06:29 -07003935 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
3936 }
3937
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003938 final long identity = Binder.clearCallingIdentity();
3939 try {
3940 final Phone phone = getPhone(subId);
3941 if (phone != null) {
3942 return phone.getServiceState().getVoiceNetworkType();
3943 } else {
3944 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
3945 }
3946 } finally {
3947 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07003948 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003949 }
3950
3951 /**
3952 * @return true if a ICC card is present
3953 */
3954 public boolean hasIccCard() {
Wink Saville36469e72014-06-11 15:17:00 -07003955 // FIXME Make changes to pass defaultSimId of type int
Sanket Padawe13bac7b2017-03-20 15:04:47 -07003956 return hasIccCardUsingSlotIndex(mSubscriptionController.getSlotIndex(
3957 getDefaultSubscription()));
Wink Saville36469e72014-06-11 15:17:00 -07003958 }
3959
3960 /**
Sanket Padawe13bac7b2017-03-20 15:04:47 -07003961 * @return true if a ICC card is present for a slotIndex
Wink Saville36469e72014-06-11 15:17:00 -07003962 */
Sanket Padawe356d7632015-06-22 14:03:32 -07003963 @Override
Sanket Padawe13bac7b2017-03-20 15:04:47 -07003964 public boolean hasIccCardUsingSlotIndex(int slotIndex) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003965 final long identity = Binder.clearCallingIdentity();
3966 try {
3967 final Phone phone = PhoneFactory.getPhone(slotIndex);
3968 if (phone != null) {
3969 return phone.getIccCard().hasIccCard();
3970 } else {
3971 return false;
3972 }
3973 } finally {
3974 Binder.restoreCallingIdentity(identity);
Amit Mahajana6fc2a82015-01-06 11:53:51 -08003975 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003976 }
3977
3978 /**
3979 * Return if the current radio is LTE on CDMA. This
3980 * is a tri-state return value as for a period of time
3981 * the mode may be unknown.
3982 *
Robert Greenwalt36b23af2015-07-06 17:59:14 -07003983 * @param callingPackage the name of the package making the call.
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003984 * @return {@link Phone#LTE_ON_CDMA_UNKNOWN}, {@link Phone#LTE_ON_CDMA_FALSE}
Jake Hambye994d462014-02-03 13:10:13 -08003985 * or {@link Phone#LTE_ON_CDMA_TRUE}
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003986 */
Robert Greenwalt36b23af2015-07-06 17:59:14 -07003987 @Override
3988 public int getLteOnCdmaMode(String callingPackage) {
3989 return getLteOnCdmaModeForSubscriber(getDefaultSubscription(), callingPackage);
Wink Saville36469e72014-06-11 15:17:00 -07003990 }
3991
Sanket Padawe356d7632015-06-22 14:03:32 -07003992 @Override
Robert Greenwalt36b23af2015-07-06 17:59:14 -07003993 public int getLteOnCdmaModeForSubscriber(int subId, String callingPackage) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08003994 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
Jeff Davidson913390f2018-02-23 17:11:49 -08003995 mApp, subId, callingPackage, "getLteOnCdmaModeForSubscriber")) {
Robert Greenwalt36b23af2015-07-06 17:59:14 -07003996 return PhoneConstants.LTE_ON_CDMA_UNKNOWN;
3997 }
3998
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003999 final long identity = Binder.clearCallingIdentity();
4000 try {
4001 final Phone phone = getPhone(subId);
4002 if (phone == null) {
4003 return PhoneConstants.LTE_ON_CDMA_UNKNOWN;
4004 } else {
4005 return phone.getLteOnCdmaMode();
4006 }
4007 } finally {
4008 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07004009 }
Wink Saville36469e72014-06-11 15:17:00 -07004010 }
4011
Wink Saville36469e72014-06-11 15:17:00 -07004012 /**
4013 * {@hide}
4014 * Returns Default subId, 0 in the case of single standby.
4015 */
Wink Savilleb564aae2014-10-23 10:18:09 -07004016 private int getDefaultSubscription() {
Wink Savilleac1bdfd2014-11-20 23:04:44 -08004017 return mSubscriptionController.getDefaultSubId();
Wink Saville36469e72014-06-11 15:17:00 -07004018 }
4019
Shishir Agrawala9f32182016-04-12 12:00:16 -07004020 private int getSlotForDefaultSubscription() {
4021 return mSubscriptionController.getPhoneId(getDefaultSubscription());
4022 }
4023
Wink Savilleb564aae2014-10-23 10:18:09 -07004024 private int getPreferredVoiceSubscription() {
Wink Savilleac1bdfd2014-11-20 23:04:44 -08004025 return mSubscriptionController.getDefaultVoiceSubId();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07004026 }
Ihab Awadf2177b72013-11-25 13:33:23 -08004027
Pengquan Menge92a50d2018-09-21 15:54:48 -07004028 private boolean isActiveSubscription(int subId) {
4029 return mSubscriptionController.isActiveSubId(subId);
4030 }
4031
Ihab Awadf2177b72013-11-25 13:33:23 -08004032 /**
4033 * @see android.telephony.TelephonyManager.WifiCallingChoices
4034 */
4035 public int getWhenToMakeWifiCalls() {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004036 final long identity = Binder.clearCallingIdentity();
4037 try {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08004038 return Settings.System.getInt(mApp.getContentResolver(),
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004039 Settings.System.WHEN_TO_MAKE_WIFI_CALLS,
4040 getWhenToMakeWifiCallsDefaultPreference());
4041 } finally {
4042 Binder.restoreCallingIdentity(identity);
4043 }
Ihab Awadf2177b72013-11-25 13:33:23 -08004044 }
4045
4046 /**
4047 * @see android.telephony.TelephonyManager.WifiCallingChoices
4048 */
4049 public void setWhenToMakeWifiCalls(int preference) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004050 final long identity = Binder.clearCallingIdentity();
4051 try {
4052 if (DBG) log("setWhenToMakeWifiCallsStr, storing setting = " + preference);
Nazanin Bakhshif782e562018-12-11 15:15:39 -08004053 Settings.System.putInt(mApp.getContentResolver(),
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004054 Settings.System.WHEN_TO_MAKE_WIFI_CALLS, preference);
4055 } finally {
4056 Binder.restoreCallingIdentity(identity);
4057 }
Ihab Awadf9e92732013-12-05 18:02:52 -08004058 }
4059
Sailesh Nepald1e68152013-12-12 19:08:02 -08004060 private static int getWhenToMakeWifiCallsDefaultPreference() {
Santos Cordonda120f42014-08-06 04:44:34 -07004061 // TODO: Use a build property to choose this value.
Evan Charlton9829e882013-12-19 15:30:38 -08004062 return TelephonyManager.WifiCallingChoices.ALWAYS_USE;
Ihab Awadf2177b72013-11-25 13:33:23 -08004063 }
Shishir Agrawal69f68122013-12-16 17:25:49 -08004064
Jordan Liu4c733742019-02-28 12:03:40 -08004065 private Phone getPhoneFromSlotIdOrThrowException(int slotIndex) {
4066 int phoneId = UiccController.getInstance().getPhoneIdFromSlotId(slotIndex);
4067 if (phoneId == -1) {
4068 throw new IllegalArgumentException("Given slot index: " + slotIndex
4069 + " does not correspond to an active phone");
4070 }
4071 return PhoneFactory.getPhone(phoneId);
4072 }
4073
Shishir Agrawal566b7612013-10-28 14:41:00 -07004074 @Override
Derek Tan740e1672017-06-27 14:56:27 -07004075 public IccOpenLogicalChannelResponse iccOpenLogicalChannel(
4076 int subId, String callingPackage, String aid, int p2) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08004077 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
4078 mApp, subId, "iccOpenLogicalChannel");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004079 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
Jordan Liu4c733742019-02-28 12:03:40 -08004080 if (DBG) {
4081 log("iccOpenLogicalChannel: subId=" + subId + " aid=" + aid + " p2=" + p2);
4082 }
4083 return iccOpenLogicalChannelWithPermission(getPhoneFromSubId(subId), callingPackage, aid,
4084 p2);
4085 }
Shishir Agrawal566b7612013-10-28 14:41:00 -07004086
Jordan Liu4c733742019-02-28 12:03:40 -08004087
4088 @Override
4089 public IccOpenLogicalChannelResponse iccOpenLogicalChannelBySlot(
4090 int slotIndex, String callingPackage, String aid, int p2) {
4091 enforceModifyPermission();
4092 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
4093 if (DBG) {
4094 log("iccOpenLogicalChannelBySlot: slot=" + slotIndex + " aid=" + aid + " p2=" + p2);
4095 }
4096 return iccOpenLogicalChannelWithPermission(getPhoneFromSlotIdOrThrowException(slotIndex),
4097 callingPackage, aid, p2);
4098 }
4099
4100 private IccOpenLogicalChannelResponse iccOpenLogicalChannelWithPermission(Phone phone,
4101 String callingPackage, String aid, int p2) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004102 final long identity = Binder.clearCallingIdentity();
4103 try {
4104 if (TextUtils.equals(ISDR_AID, aid)) {
4105 // Only allows LPA to open logical channel to ISD-R.
Nazanin Bakhshif782e562018-12-11 15:15:39 -08004106 ComponentInfo bestComponent = EuiccConnector.findBestComponent(getDefaultPhone()
4107 .getContext().getPackageManager());
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004108 if (bestComponent == null
4109 || !TextUtils.equals(callingPackage, bestComponent.packageName)) {
4110 loge("The calling package is not allowed to access ISD-R.");
4111 throw new SecurityException(
4112 "The calling package is not allowed to access ISD-R.");
4113 }
Derek Tan740e1672017-06-27 14:56:27 -07004114 }
Derek Tan740e1672017-06-27 14:56:27 -07004115
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004116 IccOpenLogicalChannelResponse response = (IccOpenLogicalChannelResponse) sendRequest(
Jordan Liu4c733742019-02-28 12:03:40 -08004117 CMD_OPEN_CHANNEL, new Pair<String, Integer>(aid, p2), phone,
4118 null /* workSource */);
4119 if (DBG) log("iccOpenLogicalChannelWithPermission: " + response);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004120 return response;
4121 } finally {
4122 Binder.restoreCallingIdentity(identity);
4123 }
Shishir Agrawal566b7612013-10-28 14:41:00 -07004124 }
4125
4126 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08004127 public boolean iccCloseLogicalChannel(int subId, int channel) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08004128 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
4129 mApp, subId, "iccCloseLogicalChannel");
Jordan Liu4c733742019-02-28 12:03:40 -08004130 if (DBG) log("iccCloseLogicalChannel: subId=" + subId + " chnl=" + channel);
4131 return iccCloseLogicalChannelWithPermission(getPhoneFromSubId(subId), channel);
4132 }
Shishir Agrawal566b7612013-10-28 14:41:00 -07004133
Jordan Liu4c733742019-02-28 12:03:40 -08004134 @Override
4135 public boolean iccCloseLogicalChannelBySlot(int slotIndex, int channel) {
4136 enforceModifyPermission();
4137 if (DBG) log("iccCloseLogicalChannelBySlot: slotIndex=" + slotIndex + " chnl=" + channel);
4138 return iccCloseLogicalChannelWithPermission(getPhoneFromSlotIdOrThrowException(slotIndex),
4139 channel);
4140 }
4141
4142 private boolean iccCloseLogicalChannelWithPermission(Phone phone, int channel) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004143 final long identity = Binder.clearCallingIdentity();
4144 try {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004145 if (channel < 0) {
4146 return false;
4147 }
Jordan Liu4c733742019-02-28 12:03:40 -08004148 Boolean success = (Boolean) sendRequest(CMD_CLOSE_CHANNEL, channel, phone,
4149 null /* workSource */);
4150 if (DBG) log("iccCloseLogicalChannelWithPermission: " + success);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004151 return success;
4152 } finally {
4153 Binder.restoreCallingIdentity(identity);
Shishir Agrawal566b7612013-10-28 14:41:00 -07004154 }
Shishir Agrawal566b7612013-10-28 14:41:00 -07004155 }
4156
4157 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08004158 public String iccTransmitApduLogicalChannel(int subId, int channel, int cla,
Shishir Agrawal566b7612013-10-28 14:41:00 -07004159 int command, int p1, int p2, int p3, String data) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08004160 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
4161 mApp, subId, "iccTransmitApduLogicalChannel");
Jordan Liu4c733742019-02-28 12:03:40 -08004162 if (DBG) {
4163 log("iccTransmitApduLogicalChannel: subId=" + subId + " chnl=" + channel
4164 + " cla=" + cla + " cmd=" + command + " p1=" + p1 + " p2=" + p2 + " p3="
4165 + p3 + " data=" + data);
4166 }
4167 return iccTransmitApduLogicalChannelWithPermission(getPhoneFromSubId(subId), channel, cla,
4168 command, p1, p2, p3, data);
4169 }
Shishir Agrawal566b7612013-10-28 14:41:00 -07004170
Jordan Liu4c733742019-02-28 12:03:40 -08004171 @Override
4172 public String iccTransmitApduLogicalChannelBySlot(int slotIndex, int channel, int cla,
4173 int command, int p1, int p2, int p3, String data) {
4174 enforceModifyPermission();
4175 if (DBG) {
4176 log("iccTransmitApduLogicalChannelBySlot: slotIndex=" + slotIndex + " chnl=" + channel
4177 + " cla=" + cla + " cmd=" + command + " p1=" + p1 + " p2=" + p2 + " p3="
4178 + p3 + " data=" + data);
4179 }
4180 return iccTransmitApduLogicalChannelWithPermission(
4181 getPhoneFromSlotIdOrThrowException(slotIndex), channel, cla, command, p1, p2, p3,
4182 data);
4183 }
4184
4185 private String iccTransmitApduLogicalChannelWithPermission(Phone phone, int channel, int cla,
4186 int command, int p1, int p2, int p3, String data) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004187 final long identity = Binder.clearCallingIdentity();
4188 try {
Hall Liu4fd771b2019-05-02 09:16:29 -07004189 if (channel <= 0) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004190 return "";
4191 }
Shishir Agrawal566b7612013-10-28 14:41:00 -07004192
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004193 IccIoResult response = (IccIoResult) sendRequest(CMD_TRANSMIT_APDU_LOGICAL_CHANNEL,
Jordan Liu4c733742019-02-28 12:03:40 -08004194 new IccAPDUArgument(channel, cla, command, p1, p2, p3, data), phone,
4195 null /* workSource */);
4196 if (DBG) log("iccTransmitApduLogicalChannelWithPermission: " + response);
Shishir Agrawal566b7612013-10-28 14:41:00 -07004197
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004198 // Append the returned status code to the end of the response payload.
4199 String s = Integer.toHexString(
4200 (response.sw1 << 8) + response.sw2 + 0x10000).substring(1);
4201 if (response.payload != null) {
4202 s = IccUtils.bytesToHexString(response.payload) + s;
4203 }
4204 return s;
4205 } finally {
4206 Binder.restoreCallingIdentity(identity);
Shishir Agrawal5ec14172014-08-05 17:05:45 -07004207 }
Shishir Agrawal566b7612013-10-28 14:41:00 -07004208 }
Jake Hambye994d462014-02-03 13:10:13 -08004209
Evan Charltonc66da362014-05-16 14:06:40 -07004210 @Override
Holly Jiuyu Sun1cc2d552018-01-26 15:51:16 -08004211 public String iccTransmitApduBasicChannel(int subId, String callingPackage, int cla,
4212 int command, int p1, int p2, int p3, String data) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08004213 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
4214 mApp, subId, "iccTransmitApduBasicChannel");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004215 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
Jordan Liu4c733742019-02-28 12:03:40 -08004216 if (DBG) {
4217 log("iccTransmitApduBasicChannel: subId=" + subId + " cla=" + cla + " cmd="
4218 + command + " p1=" + p1 + " p2=" + p2 + " p3=" + p3 + " data=" + data);
4219 }
4220 return iccTransmitApduBasicChannelWithPermission(getPhoneFromSubId(subId), callingPackage,
4221 cla, command, p1, p2, p3, data);
4222 }
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07004223
Jordan Liu4c733742019-02-28 12:03:40 -08004224 @Override
4225 public String iccTransmitApduBasicChannelBySlot(int slotIndex, String callingPackage, int cla,
4226 int command, int p1, int p2, int p3, String data) {
4227 enforceModifyPermission();
4228 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
4229 if (DBG) {
4230 log("iccTransmitApduBasicChannelBySlot: slotIndex=" + slotIndex + " cla=" + cla
4231 + " cmd=" + command + " p1=" + p1 + " p2=" + p2 + " p3=" + p3
4232 + " data=" + data);
4233 }
4234
4235 return iccTransmitApduBasicChannelWithPermission(
4236 getPhoneFromSlotIdOrThrowException(slotIndex), callingPackage, cla, command, p1,
4237 p2, p3, data);
4238 }
4239
4240 // open APDU basic channel assuming the caller has sufficient permissions
4241 private String iccTransmitApduBasicChannelWithPermission(Phone phone, String callingPackage,
4242 int cla, int command, int p1, int p2, int p3, String data) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004243 final long identity = Binder.clearCallingIdentity();
4244 try {
4245 if (command == SELECT_COMMAND && p1 == SELECT_P1 && p2 == SELECT_P2 && p3 == SELECT_P3
4246 && TextUtils.equals(ISDR_AID, data)) {
4247 // Only allows LPA to select ISD-R.
Nazanin Bakhshif782e562018-12-11 15:15:39 -08004248 ComponentInfo bestComponent = EuiccConnector.findBestComponent(getDefaultPhone()
4249 .getContext().getPackageManager());
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004250 if (bestComponent == null
4251 || !TextUtils.equals(callingPackage, bestComponent.packageName)) {
4252 loge("The calling package is not allowed to select ISD-R.");
4253 throw new SecurityException(
4254 "The calling package is not allowed to select ISD-R.");
4255 }
Holly Jiuyu Sun1cc2d552018-01-26 15:51:16 -08004256 }
Holly Jiuyu Sun1cc2d552018-01-26 15:51:16 -08004257
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004258 IccIoResult response = (IccIoResult) sendRequest(CMD_TRANSMIT_APDU_BASIC_CHANNEL,
Jordan Liu4c733742019-02-28 12:03:40 -08004259 new IccAPDUArgument(0, cla, command, p1, p2, p3, data), phone,
4260 null /* workSource */);
4261 if (DBG) log("iccTransmitApduBasicChannelWithPermission: " + response);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07004262
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004263 // Append the returned status code to the end of the response payload.
4264 String s = Integer.toHexString(
4265 (response.sw1 << 8) + response.sw2 + 0x10000).substring(1);
4266 if (response.payload != null) {
4267 s = IccUtils.bytesToHexString(response.payload) + s;
4268 }
4269 return s;
4270 } finally {
4271 Binder.restoreCallingIdentity(identity);
Shishir Agrawal5ec14172014-08-05 17:05:45 -07004272 }
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07004273 }
4274
4275 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08004276 public byte[] iccExchangeSimIO(int subId, int fileID, int command, int p1, int p2, int p3,
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07004277 String filePath) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08004278 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
4279 mApp, subId, "iccExchangeSimIO");
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07004280
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004281 final long identity = Binder.clearCallingIdentity();
4282 try {
4283 if (DBG) {
4284 log("Exchange SIM_IO " + subId + ":" + fileID + ":" + command + " "
4285 + p1 + " " + p2 + " " + p3 + ":" + filePath);
4286 }
4287
4288 IccIoResult response =
4289 (IccIoResult) sendRequest(CMD_EXCHANGE_SIM_IO,
4290 new IccAPDUArgument(-1, fileID, command, p1, p2, p3, filePath),
4291 subId);
4292
4293 if (DBG) {
4294 log("Exchange SIM_IO [R]" + response);
4295 }
4296
4297 byte[] result = null;
4298 int length = 2;
4299 if (response.payload != null) {
4300 length = 2 + response.payload.length;
4301 result = new byte[length];
4302 System.arraycopy(response.payload, 0, result, 0, response.payload.length);
4303 } else {
4304 result = new byte[length];
4305 }
4306
4307 result[length - 1] = (byte) response.sw2;
4308 result[length - 2] = (byte) response.sw1;
4309 return result;
4310 } finally {
4311 Binder.restoreCallingIdentity(identity);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07004312 }
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07004313 }
4314
Nathan Haroldb3014052017-01-25 15:57:32 -08004315 /**
4316 * Get the forbidden PLMN List from the given app type (ex APPTYPE_USIM)
4317 * on a particular subscription
4318 */
sqianb6e41952018-03-12 14:54:01 -07004319 public String[] getForbiddenPlmns(int subId, int appType, String callingPackage) {
4320 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
4321 mApp, subId, callingPackage, "getForbiddenPlmns")) {
4322 return null;
4323 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004324
4325 final long identity = Binder.clearCallingIdentity();
4326 try {
4327 if (appType != TelephonyManager.APPTYPE_USIM
4328 && appType != TelephonyManager.APPTYPE_SIM) {
4329 loge("getForbiddenPlmnList(): App Type must be USIM or SIM");
4330 return null;
4331 }
4332 Object response = sendRequest(
4333 CMD_GET_FORBIDDEN_PLMNS, new Integer(appType), subId);
4334 if (response instanceof String[]) {
4335 return (String[]) response;
4336 }
yincheng zhaod698b842019-09-06 17:06:54 -07004337 // Response is an Exception of some kind
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004338 // which is signalled to the user as a NULL retval
Nathan Haroldb3014052017-01-25 15:57:32 -08004339 return null;
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004340 } finally {
4341 Binder.restoreCallingIdentity(identity);
Nathan Haroldb3014052017-01-25 15:57:32 -08004342 }
Nathan Haroldb3014052017-01-25 15:57:32 -08004343 }
4344
yincheng zhaod698b842019-09-06 17:06:54 -07004345 /**
4346 * Set the forbidden PLMN list from the given app type (ex APPTYPE_USIM) on a particular
4347 * subscription.
4348 *
4349 * @param subId the id of the subscription.
4350 * @param appType the uicc app type, must be USIM or SIM.
4351 * @param fplmns the Forbiden plmns list that needed to be written to the SIM.
4352 * @param callingPackage the op Package name.
4353 * @return number of fplmns that is successfully written to the SIM.
4354 */
4355 public int setForbiddenPlmns(
4356 int subId, int appType, List<String> fplmns, String callingPackage) {
4357 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
4358 mApp, subId, callingPackage, "setForbiddenPlmns")) {
4359 if (DBG) logv("no permissions for setForbiddenplmns");
4360 throw new IllegalStateException("No Permissions for setForbiddenPlmns");
4361 }
4362 if (appType != TelephonyManager.APPTYPE_USIM && appType != TelephonyManager.APPTYPE_SIM) {
4363 loge("setForbiddenPlmnList(): App Type must be USIM or SIM");
4364 throw new IllegalArgumentException("Invalid appType: App Type must be USIM or SIM");
4365 }
4366 if (fplmns == null) {
4367 throw new IllegalArgumentException("Fplmn List provided is null");
4368 }
4369 for (String fplmn : fplmns) {
4370 if (!CellIdentity.isValidPlmn(fplmn)) {
4371 throw new IllegalArgumentException("Invalid fplmn provided: " + fplmn);
4372 }
4373 }
4374 final long identity = Binder.clearCallingIdentity();
4375 try {
4376 Object response = sendRequest(
4377 CMD_SET_FORBIDDEN_PLMNS,
4378 new Pair<Integer, List<String>>(new Integer(appType), fplmns),
4379 subId);
4380 return (int) response;
4381 } finally {
4382 Binder.restoreCallingIdentity(identity);
4383 }
4384 }
4385
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07004386 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08004387 public String sendEnvelopeWithStatus(int subId, String content) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08004388 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
4389 mApp, subId, "sendEnvelopeWithStatus");
Evan Charltonc66da362014-05-16 14:06:40 -07004390
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004391 final long identity = Binder.clearCallingIdentity();
4392 try {
4393 IccIoResult response = (IccIoResult) sendRequest(CMD_SEND_ENVELOPE, content, subId);
4394 if (response.payload == null) {
4395 return "";
4396 }
Evan Charltonc66da362014-05-16 14:06:40 -07004397
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004398 // Append the returned status code to the end of the response payload.
4399 String s = Integer.toHexString(
4400 (response.sw1 << 8) + response.sw2 + 0x10000).substring(1);
4401 s = IccUtils.bytesToHexString(response.payload) + s;
4402 return s;
4403 } finally {
4404 Binder.restoreCallingIdentity(identity);
4405 }
Evan Charltonc66da362014-05-16 14:06:40 -07004406 }
4407
Jake Hambye994d462014-02-03 13:10:13 -08004408 /**
4409 * Read one of the NV items defined in {@link com.android.internal.telephony.RadioNVItems}
4410 * and {@code ril_nv_items.h}. Used for device configuration by some CDMA operators.
4411 *
4412 * @param itemID the ID of the item to read
4413 * @return the NV item as a String, or null on error.
4414 */
4415 @Override
4416 public String nvReadItem(int itemID) {
vagdeviaf9a5b92018-08-15 16:01:53 -07004417 WorkSource workSource = getWorkSource(Binder.getCallingUid());
Jeff Davidson7e17e312018-02-13 18:17:36 -08004418 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
4419 mApp, getDefaultSubscription(), "nvReadItem");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004420
4421 final long identity = Binder.clearCallingIdentity();
4422 try {
4423 if (DBG) log("nvReadItem: item " + itemID);
vagdeviaf9a5b92018-08-15 16:01:53 -07004424 String value = (String) sendRequest(CMD_NV_READ_ITEM, itemID, workSource);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004425 if (DBG) log("nvReadItem: item " + itemID + " is \"" + value + '"');
4426 return value;
4427 } finally {
4428 Binder.restoreCallingIdentity(identity);
4429 }
Jake Hambye994d462014-02-03 13:10:13 -08004430 }
4431
4432 /**
4433 * Write one of the NV items defined in {@link com.android.internal.telephony.RadioNVItems}
4434 * and {@code ril_nv_items.h}. Used for device configuration by some CDMA operators.
4435 *
4436 * @param itemID the ID of the item to read
4437 * @param itemValue the value to write, as a String
4438 * @return true on success; false on any failure
4439 */
4440 @Override
4441 public boolean nvWriteItem(int itemID, String itemValue) {
vagdeviaf9a5b92018-08-15 16:01:53 -07004442 WorkSource workSource = getWorkSource(Binder.getCallingUid());
Jeff Davidson7e17e312018-02-13 18:17:36 -08004443 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
4444 mApp, getDefaultSubscription(), "nvWriteItem");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004445
4446 final long identity = Binder.clearCallingIdentity();
4447 try {
4448 if (DBG) log("nvWriteItem: item " + itemID + " value \"" + itemValue + '"');
4449 Boolean success = (Boolean) sendRequest(CMD_NV_WRITE_ITEM,
vagdeviaf9a5b92018-08-15 16:01:53 -07004450 new Pair<Integer, String>(itemID, itemValue), workSource);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004451 if (DBG) log("nvWriteItem: item " + itemID + ' ' + (success ? "ok" : "fail"));
4452 return success;
4453 } finally {
4454 Binder.restoreCallingIdentity(identity);
4455 }
Jake Hambye994d462014-02-03 13:10:13 -08004456 }
4457
4458 /**
4459 * Update the CDMA Preferred Roaming List (PRL) in the radio NV storage.
4460 * Used for device configuration by some CDMA operators.
4461 *
4462 * @param preferredRoamingList byte array containing the new PRL
4463 * @return true on success; false on any failure
4464 */
4465 @Override
4466 public boolean nvWriteCdmaPrl(byte[] preferredRoamingList) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08004467 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
4468 mApp, getDefaultSubscription(), "nvWriteCdmaPrl");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004469
4470 final long identity = Binder.clearCallingIdentity();
4471 try {
4472 if (DBG) log("nvWriteCdmaPrl: value: " + HexDump.toHexString(preferredRoamingList));
4473 Boolean success = (Boolean) sendRequest(CMD_NV_WRITE_CDMA_PRL, preferredRoamingList);
4474 if (DBG) log("nvWriteCdmaPrl: " + (success ? "ok" : "fail"));
4475 return success;
4476 } finally {
4477 Binder.restoreCallingIdentity(identity);
4478 }
Jake Hambye994d462014-02-03 13:10:13 -08004479 }
4480
4481 /**
chen xu6dac5ab2018-10-26 17:39:23 -07004482 * Rollback modem configurations to factory default except some config which are in whitelist.
Jake Hambye994d462014-02-03 13:10:13 -08004483 * Used for device configuration by some CDMA operators.
4484 *
chen xu6dac5ab2018-10-26 17:39:23 -07004485 * @param slotIndex - device slot.
4486 *
Jake Hambye994d462014-02-03 13:10:13 -08004487 * @return true on success; false on any failure
4488 */
4489 @Override
chen xu6dac5ab2018-10-26 17:39:23 -07004490 public boolean resetModemConfig(int slotIndex) {
4491 Phone phone = PhoneFactory.getPhone(slotIndex);
4492 if (phone != null) {
4493 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
4494 mApp, phone.getSubId(), "resetModemConfig");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004495
chen xu6dac5ab2018-10-26 17:39:23 -07004496 final long identity = Binder.clearCallingIdentity();
4497 try {
4498 Boolean success = (Boolean) sendRequest(CMD_RESET_MODEM_CONFIG, null);
4499 if (DBG) log("resetModemConfig:" + ' ' + (success ? "ok" : "fail"));
4500 return success;
4501 } finally {
4502 Binder.restoreCallingIdentity(identity);
4503 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004504 }
chen xu6dac5ab2018-10-26 17:39:23 -07004505 return false;
4506 }
4507
4508 /**
4509 * Generate a radio modem reset. Used for device configuration by some CDMA operators.
4510 *
4511 * @param slotIndex - device slot.
4512 *
4513 * @return true on success; false on any failure
4514 */
4515 @Override
4516 public boolean rebootModem(int slotIndex) {
4517 Phone phone = PhoneFactory.getPhone(slotIndex);
4518 if (phone != null) {
4519 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
4520 mApp, phone.getSubId(), "rebootModem");
4521
4522 final long identity = Binder.clearCallingIdentity();
4523 try {
4524 Boolean success = (Boolean) sendRequest(CMD_MODEM_REBOOT, null);
4525 if (DBG) log("rebootModem:" + ' ' + (success ? "ok" : "fail"));
4526 return success;
4527 } finally {
4528 Binder.restoreCallingIdentity(identity);
4529 }
4530 }
4531 return false;
Jake Hambye994d462014-02-03 13:10:13 -08004532 }
Jake Hamby7c27be32014-03-03 13:25:59 -08004533
Svet Ganovb320e182015-04-16 12:30:10 -07004534 public String[] getPcscfAddress(String apnType, String callingPackage) {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08004535 final Phone defaultPhone = getDefaultPhone();
Jeff Davidson7e17e312018-02-13 18:17:36 -08004536 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
Nazanin Bakhshif782e562018-12-11 15:15:39 -08004537 mApp, defaultPhone.getSubId(), callingPackage, "getPcscfAddress")) {
Svet Ganovb320e182015-04-16 12:30:10 -07004538 return new String[0];
4539 }
4540
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004541 final long identity = Binder.clearCallingIdentity();
4542 try {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08004543 return defaultPhone.getPcscfAddress(apnType);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004544 } finally {
4545 Binder.restoreCallingIdentity(identity);
4546 }
Wink Saville36469e72014-06-11 15:17:00 -07004547 }
4548
Brad Ebinger51f743a2017-01-23 13:50:20 -08004549 /**
Brad Ebinger22bc3e42018-01-16 09:39:35 -08004550 * Enables IMS for the framework. This will trigger IMS registration and ImsFeature capability
4551 * status updates, if not already enabled.
Brad Ebinger51f743a2017-01-23 13:50:20 -08004552 */
Brad Ebinger22bc3e42018-01-16 09:39:35 -08004553 public void enableIms(int slotId) {
Brad Ebinger51f743a2017-01-23 13:50:20 -08004554 enforceModifyPermission();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004555
4556 final long identity = Binder.clearCallingIdentity();
4557 try {
Brad Ebinger05f52c22019-12-05 13:03:21 -08004558 if (mImsResolver == null) {
Brad Ebinger9c0eb502019-01-23 15:06:19 -08004559 // may happen if the device does not support IMS.
4560 return;
4561 }
Brad Ebinger05f52c22019-12-05 13:03:21 -08004562 mImsResolver.enableIms(slotId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004563 } finally {
4564 Binder.restoreCallingIdentity(identity);
4565 }
Brad Ebinger34bef922017-11-09 10:27:08 -08004566 }
4567
4568 /**
Brad Ebinger22bc3e42018-01-16 09:39:35 -08004569 * Disables IMS for the framework. This will trigger IMS de-registration and trigger ImsFeature
4570 * status updates to disabled.
Brad Ebinger34bef922017-11-09 10:27:08 -08004571 */
Brad Ebinger22bc3e42018-01-16 09:39:35 -08004572 public void disableIms(int slotId) {
4573 enforceModifyPermission();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004574
4575 final long identity = Binder.clearCallingIdentity();
4576 try {
Brad Ebinger05f52c22019-12-05 13:03:21 -08004577 if (mImsResolver == null) {
Brad Ebinger9c0eb502019-01-23 15:06:19 -08004578 // may happen if the device does not support IMS.
4579 return;
4580 }
Brad Ebinger05f52c22019-12-05 13:03:21 -08004581 mImsResolver.disableIms(slotId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004582 } finally {
4583 Binder.restoreCallingIdentity(identity);
4584 }
Brad Ebinger22bc3e42018-01-16 09:39:35 -08004585 }
4586
4587 /**
4588 * Returns the {@link IImsMmTelFeature} that corresponds to the given slot Id for the MMTel
4589 * feature or {@link null} if the service is not available. If the feature is available, the
4590 * {@link IImsServiceFeatureCallback} callback is registered as a listener for feature updates.
4591 */
4592 public IImsMmTelFeature getMmTelFeatureAndListen(int slotId,
Brad Ebinger34bef922017-11-09 10:27:08 -08004593 IImsServiceFeatureCallback callback) {
4594 enforceModifyPermission();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004595
4596 final long identity = Binder.clearCallingIdentity();
4597 try {
Brad Ebinger05f52c22019-12-05 13:03:21 -08004598 if (mImsResolver == null) {
Brad Ebinger9c0eb502019-01-23 15:06:19 -08004599 // may happen if the device does not support IMS.
4600 return null;
4601 }
Brad Ebinger05f52c22019-12-05 13:03:21 -08004602 return mImsResolver.getMmTelFeatureAndListen(slotId, callback);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004603 } finally {
4604 Binder.restoreCallingIdentity(identity);
4605 }
Brad Ebinger34bef922017-11-09 10:27:08 -08004606 }
4607
4608 /**
4609 * Returns the {@link IImsRcsFeature} that corresponds to the given slot Id for the RCS
4610 * feature during emergency calling or {@link null} if the service is not available. If the
4611 * feature is available, the {@link IImsServiceFeatureCallback} callback is registered as a
4612 * listener for feature updates.
4613 */
4614 public IImsRcsFeature getRcsFeatureAndListen(int slotId, IImsServiceFeatureCallback callback) {
4615 enforceModifyPermission();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004616
4617 final long identity = Binder.clearCallingIdentity();
4618 try {
Brad Ebinger05f52c22019-12-05 13:03:21 -08004619 if (mImsResolver == null) {
Brad Ebinger9c0eb502019-01-23 15:06:19 -08004620 // may happen if the device does not support IMS.
4621 return null;
4622 }
Brad Ebinger05f52c22019-12-05 13:03:21 -08004623 return mImsResolver.getRcsFeatureAndListen(slotId, callback);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004624 } finally {
4625 Binder.restoreCallingIdentity(identity);
4626 }
Brad Ebinger51f743a2017-01-23 13:50:20 -08004627 }
4628
Brad Ebinger5f64b052017-12-14 14:26:15 -08004629 /**
4630 * Returns the {@link IImsRegistration} structure associated with the slotId and feature
Brad Ebinger9c0eb502019-01-23 15:06:19 -08004631 * specified or null if IMS is not supported on the slot specified.
Brad Ebinger5f64b052017-12-14 14:26:15 -08004632 */
4633 public IImsRegistration getImsRegistration(int slotId, int feature) throws RemoteException {
4634 enforceModifyPermission();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004635
4636 final long identity = Binder.clearCallingIdentity();
4637 try {
Brad Ebinger05f52c22019-12-05 13:03:21 -08004638 if (mImsResolver == null) {
Brad Ebinger9c0eb502019-01-23 15:06:19 -08004639 // may happen if the device does not support IMS.
4640 return null;
4641 }
Brad Ebinger05f52c22019-12-05 13:03:21 -08004642 return mImsResolver.getImsRegistration(slotId, feature);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004643 } finally {
4644 Binder.restoreCallingIdentity(identity);
4645 }
Brad Ebinger5f64b052017-12-14 14:26:15 -08004646 }
4647
Brad Ebinger22bc3e42018-01-16 09:39:35 -08004648 /**
4649 * Returns the {@link IImsConfig} structure associated with the slotId and feature
Brad Ebinger9c0eb502019-01-23 15:06:19 -08004650 * specified or null if IMS is not supported on the slot specified.
Brad Ebinger22bc3e42018-01-16 09:39:35 -08004651 */
4652 public IImsConfig getImsConfig(int slotId, int feature) throws RemoteException {
4653 enforceModifyPermission();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004654
4655 final long identity = Binder.clearCallingIdentity();
4656 try {
Brad Ebinger05f52c22019-12-05 13:03:21 -08004657 if (mImsResolver == null) {
Brad Ebinger9c0eb502019-01-23 15:06:19 -08004658 // may happen if the device does not support IMS.
4659 return null;
4660 }
Brad Ebinger05f52c22019-12-05 13:03:21 -08004661 return mImsResolver.getImsConfig(slotId, feature);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004662 } finally {
4663 Binder.restoreCallingIdentity(identity);
4664 }
Brad Ebinger22bc3e42018-01-16 09:39:35 -08004665 }
4666
Brad Ebinger884c07b2018-02-15 16:17:40 -08004667 /**
Brad Ebingerdac2f002018-04-03 15:17:52 -07004668 * Sets the ImsService Package Name that Telephony will bind to.
4669 *
Brad Ebinger05f52c22019-12-05 13:03:21 -08004670 * @param slotIndex the slot ID that the ImsService should bind for.
4671 * @param isCarrierService true if the ImsService is the carrier override, false if the
Brad Ebingerdac2f002018-04-03 15:17:52 -07004672 * ImsService is the device default ImsService.
Brad Ebinger05f52c22019-12-05 13:03:21 -08004673 * @param featureTypes An integer array of feature types associated with a packageName.
4674 * @param packageName The name of the package that the current configuration will be replaced
4675 * with.
Brad Ebingerdac2f002018-04-03 15:17:52 -07004676 * @return true if setting the ImsService to bind to succeeded, false if it did not.
Brad Ebingerdac2f002018-04-03 15:17:52 -07004677 */
Brad Ebinger05f52c22019-12-05 13:03:21 -08004678 public boolean setBoundImsServiceOverride(int slotIndex, boolean isCarrierService,
4679 int[] featureTypes, String packageName) {
4680 int[] subIds = SubscriptionManager.getSubId(slotIndex);
4681 TelephonyPermissions.enforceShellOnly(Binder.getCallingUid(), "setBoundImsServiceOverride");
Brad Ebingerde696de2018-04-06 09:56:40 -07004682 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(mApp,
4683 (subIds != null ? subIds[0] : SubscriptionManager.INVALID_SUBSCRIPTION_ID),
Brad Ebinger05f52c22019-12-05 13:03:21 -08004684 "setBoundImsServiceOverride");
Brad Ebingerde696de2018-04-06 09:56:40 -07004685
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004686 final long identity = Binder.clearCallingIdentity();
4687 try {
Brad Ebinger05f52c22019-12-05 13:03:21 -08004688 if (mImsResolver == null) {
Brad Ebinger9c0eb502019-01-23 15:06:19 -08004689 // may happen if the device does not support IMS.
4690 return false;
4691 }
Brad Ebinger05f52c22019-12-05 13:03:21 -08004692 Map<Integer, String> featureConfig = new HashMap<>();
4693 for (int featureType : featureTypes) {
4694 featureConfig.put(featureType, packageName);
4695 }
4696 return mImsResolver.overrideImsServiceConfiguration(slotIndex, isCarrierService,
4697 featureConfig);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004698 } finally {
4699 Binder.restoreCallingIdentity(identity);
4700 }
Brad Ebingerdac2f002018-04-03 15:17:52 -07004701 }
4702
4703 /**
Brad Ebinger05f52c22019-12-05 13:03:21 -08004704 * Return the package name of the currently bound ImsService.
Brad Ebingerdac2f002018-04-03 15:17:52 -07004705 *
4706 * @param slotId The slot that the ImsService is associated with.
4707 * @param isCarrierImsService true, if the ImsService is a carrier override, false if it is
4708 * the device default.
Brad Ebinger05f52c22019-12-05 13:03:21 -08004709 * @param featureType The feature associated with the queried configuration.
Brad Ebingerdac2f002018-04-03 15:17:52 -07004710 * @return the package name of the ImsService configuration.
4711 */
Brad Ebinger05f52c22019-12-05 13:03:21 -08004712 public String getBoundImsServicePackage(int slotId, boolean isCarrierImsService,
4713 @ImsFeature.FeatureType int featureType) {
Brad Ebingerde696de2018-04-06 09:56:40 -07004714 int[] subIds = SubscriptionManager.getSubId(slotId);
Brad Ebinger05f52c22019-12-05 13:03:21 -08004715 TelephonyPermissions
4716 .enforeceCallingOrSelfReadPrivilegedPhoneStatePermissionOrCarrierPrivilege(
4717 mApp, (subIds != null ? subIds[0] : SubscriptionManager.INVALID_SUBSCRIPTION_ID),
4718 "getBoundImsServicePackage");
Brad Ebingerde696de2018-04-06 09:56:40 -07004719
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004720 final long identity = Binder.clearCallingIdentity();
4721 try {
Brad Ebinger05f52c22019-12-05 13:03:21 -08004722 if (mImsResolver == null) {
Brad Ebinger9c0eb502019-01-23 15:06:19 -08004723 // may happen if the device does not support IMS.
4724 return "";
4725 }
Brad Ebingera80c3312019-12-02 10:59:39 -08004726 // TODO: change API to query RCS separately.
Brad Ebinger05f52c22019-12-05 13:03:21 -08004727 return mImsResolver.getImsServiceConfiguration(slotId, isCarrierImsService,
4728 featureType);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004729 } finally {
4730 Binder.restoreCallingIdentity(identity);
4731 }
Brad Ebingerdac2f002018-04-03 15:17:52 -07004732 }
4733
Brad Ebinger77b832e2019-10-17 17:03:22 -07004734 /**
4735 * Get the MmTelFeature state associated with the requested subscription id.
4736 * @param subId The subscription that the MmTelFeature is associated with.
4737 * @param callback A callback with an integer containing the
4738 * {@link android.telephony.ims.feature.ImsFeature.ImsState} associated with the MmTelFeature.
4739 */
4740 @Override
4741 public void getImsMmTelFeatureState(int subId, IIntegerConsumer callback) {
4742 enforceReadPrivilegedPermission("getImsMmTelFeatureState");
4743 if (!ImsManager.isImsSupportedOnDevice(mApp)) {
4744 throw new ServiceSpecificException(ImsException.CODE_ERROR_UNSUPPORTED_OPERATION,
4745 "IMS not available on device.");
4746 }
4747 final long token = Binder.clearCallingIdentity();
4748 try {
4749 int slotId = getSlotIndex(subId);
4750 if (slotId <= SubscriptionManager.INVALID_SIM_SLOT_INDEX) {
4751 Log.w(LOG_TAG, "getImsMmTelFeatureState: called with an inactive subscription '"
4752 + subId + "'");
4753 throw new ServiceSpecificException(ImsException.CODE_ERROR_INVALID_SUBSCRIPTION);
4754 }
4755 ImsManager.getInstance(mApp, slotId).getImsServiceState(anInteger -> {
4756 try {
4757 callback.accept(anInteger == null ? ImsFeature.STATE_UNAVAILABLE : anInteger);
4758 } catch (RemoteException e) {
4759 Log.w(LOG_TAG, "getImsMmTelFeatureState: remote caller is no longer running. "
4760 + "Ignore");
4761 }
4762 });
4763 } finally {
4764 Binder.restoreCallingIdentity(token);
4765 }
4766 }
4767
Wink Saville36469e72014-06-11 15:17:00 -07004768 public void setImsRegistrationState(boolean registered) {
4769 enforceModifyPermission();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004770
4771 final long identity = Binder.clearCallingIdentity();
4772 try {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08004773 getDefaultPhone().setImsRegistrationState(registered);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004774 } finally {
4775 Binder.restoreCallingIdentity(identity);
4776 }
Wink Saville36469e72014-06-11 15:17:00 -07004777 }
4778
4779 /**
Stuart Scott54788802015-03-30 13:18:01 -07004780 * Set the network selection mode to automatic.
4781 *
4782 */
4783 @Override
4784 public void setNetworkSelectionModeAutomatic(int subId) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08004785 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
4786 mApp, subId, "setNetworkSelectionModeAutomatic");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004787
Pengquan Menge92a50d2018-09-21 15:54:48 -07004788 if (!isActiveSubscription(subId)) {
4789 return;
4790 }
4791
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004792 final long identity = Binder.clearCallingIdentity();
4793 try {
4794 if (DBG) log("setNetworkSelectionModeAutomatic: subId " + subId);
4795 sendRequest(CMD_SET_NETWORK_SELECTION_MODE_AUTOMATIC, null, subId);
4796 } finally {
4797 Binder.restoreCallingIdentity(identity);
4798 }
Stuart Scott54788802015-03-30 13:18:01 -07004799 }
4800
Pengquan Mengea84e042018-09-20 14:57:26 -07004801 /**
4802 * Ask the radio to connect to the input network and change selection mode to manual.
4803 *
4804 * @param subId the id of the subscription.
4805 * @param operatorInfo the operator information, included the PLMN, long name and short name of
4806 * the operator to attach to.
4807 * @param persistSelection whether the selection will persist until reboot. If true, only allows
4808 * attaching to the selected PLMN until reboot; otherwise, attach to the chosen PLMN and resume
4809 * normal network selection next time.
4810 * @return {@code true} on success; {@code true} on any failure.
Shishir Agrawal302c8692015-06-19 13:49:39 -07004811 */
4812 @Override
Pengquan Mengea84e042018-09-20 14:57:26 -07004813 public boolean setNetworkSelectionModeManual(
4814 int subId, OperatorInfo operatorInfo, boolean persistSelection) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08004815 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
4816 mApp, subId, "setNetworkSelectionModeManual");
Pengquan Menge92a50d2018-09-21 15:54:48 -07004817
4818 if (!isActiveSubscription(subId)) {
4819 return false;
4820 }
4821
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004822 final long identity = Binder.clearCallingIdentity();
4823 try {
Pengquan Mengea84e042018-09-20 14:57:26 -07004824 ManualNetworkSelectionArgument arg = new ManualNetworkSelectionArgument(operatorInfo,
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004825 persistSelection);
Pengquan Mengea84e042018-09-20 14:57:26 -07004826 if (DBG) {
4827 log("setNetworkSelectionModeManual: subId: " + subId
4828 + " operator: " + operatorInfo);
4829 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004830 return (Boolean) sendRequest(CMD_SET_NETWORK_SELECTION_MODE_MANUAL, arg, subId);
4831 } finally {
4832 Binder.restoreCallingIdentity(identity);
4833 }
Shishir Agrawal302c8692015-06-19 13:49:39 -07004834 }
4835
4836 /**
4837 * Scans for available networks.
4838 */
4839 @Override
Hall Liuf19c44f2018-11-27 14:38:17 -08004840 public CellNetworkScanResult getCellNetworkScanResults(int subId, String callingPackage) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08004841 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
4842 mApp, subId, "getCellNetworkScanResults");
Hall Liuf19c44f2018-11-27 14:38:17 -08004843 LocationAccessPolicy.LocationPermissionResult locationResult =
4844 LocationAccessPolicy.checkLocationPermission(mApp,
4845 new LocationAccessPolicy.LocationPermissionQuery.Builder()
4846 .setCallingPackage(callingPackage)
4847 .setCallingPid(Binder.getCallingPid())
4848 .setCallingUid(Binder.getCallingUid())
4849 .setMethod("getCellNetworkScanResults")
4850 .setMinSdkVersionForFine(Build.VERSION_CODES.Q)
4851 .build());
4852 switch (locationResult) {
4853 case DENIED_HARD:
4854 throw new SecurityException("Not allowed to access scan results -- location");
4855 case DENIED_SOFT:
4856 return null;
4857 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004858
Pengquan Menga1bb6272018-09-06 09:59:22 -07004859 long identity = Binder.clearCallingIdentity();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004860 try {
4861 if (DBG) log("getCellNetworkScanResults: subId " + subId);
Pengquan Menga1bb6272018-09-06 09:59:22 -07004862 return (CellNetworkScanResult) sendRequest(
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004863 CMD_PERFORM_NETWORK_SCAN, null, subId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004864 } finally {
4865 Binder.restoreCallingIdentity(identity);
4866 }
Shishir Agrawal302c8692015-06-19 13:49:39 -07004867 }
4868
4869 /**
yinxub1bed742017-04-17 11:45:04 -07004870 * Starts a new network scan and returns the id of this scan.
yinxu504e1392017-04-12 16:03:22 -07004871 *
yinxub1bed742017-04-17 11:45:04 -07004872 * @param subId id of the subscription
4873 * @param request contains the radio access networks with bands/channels to scan
4874 * @param messenger callback messenger for scan results or errors
4875 * @param binder for the purpose of auto clean when the user thread crashes
yinxu504e1392017-04-12 16:03:22 -07004876 * @return the id of the requested scan which can be used to stop the scan.
4877 */
4878 @Override
4879 public int requestNetworkScan(int subId, NetworkScanRequest request, Messenger messenger,
Hall Liuf19c44f2018-11-27 14:38:17 -08004880 IBinder binder, String callingPackage) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08004881 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
4882 mApp, subId, "requestNetworkScan");
Hall Liuf19c44f2018-11-27 14:38:17 -08004883 LocationAccessPolicy.LocationPermissionResult locationResult =
4884 LocationAccessPolicy.checkLocationPermission(mApp,
4885 new LocationAccessPolicy.LocationPermissionQuery.Builder()
4886 .setCallingPackage(callingPackage)
4887 .setCallingPid(Binder.getCallingPid())
4888 .setCallingUid(Binder.getCallingUid())
4889 .setMethod("requestNetworkScan")
4890 .setMinSdkVersionForFine(Build.VERSION_CODES.Q)
4891 .build());
Hall Liub2ac8ef2019-02-28 15:56:23 -08004892 if (locationResult != LocationAccessPolicy.LocationPermissionResult.ALLOWED) {
Hall Liu558027f2019-05-15 19:14:05 -07004893 SecurityException e = checkNetworkRequestForSanitizedLocationAccess(request, subId);
Hall Liub2ac8ef2019-02-28 15:56:23 -08004894 if (e != null) {
4895 if (locationResult == LocationAccessPolicy.LocationPermissionResult.DENIED_HARD) {
4896 throw e;
4897 } else {
Hall Liu0e5abaf2019-04-04 01:25:30 -07004898 loge(e.getMessage());
Hall Liub2ac8ef2019-02-28 15:56:23 -08004899 return TelephonyScanManager.INVALID_SCAN_ID;
4900 }
4901 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004902 }
Hall Liu912dfd32019-04-25 14:02:26 -07004903 int callingUid = Binder.getCallingUid();
4904 int callingPid = Binder.getCallingPid();
Ying Xu94a46582019-04-18 17:14:56 -07004905 final long identity = Binder.clearCallingIdentity();
4906 try {
4907 return mNetworkScanRequestTracker.startNetworkScan(
4908 request, messenger, binder, getPhone(subId),
Hall Liu912dfd32019-04-25 14:02:26 -07004909 callingUid, callingPid, callingPackage);
Ying Xu94a46582019-04-18 17:14:56 -07004910 } finally {
4911 Binder.restoreCallingIdentity(identity);
4912 }
yinxu504e1392017-04-12 16:03:22 -07004913 }
4914
Hall Liub2ac8ef2019-02-28 15:56:23 -08004915 private SecurityException checkNetworkRequestForSanitizedLocationAccess(
Hall Liu558027f2019-05-15 19:14:05 -07004916 NetworkScanRequest request, int subId) {
4917 boolean hasCarrierPriv = getCarrierPrivilegeStatusForUid(subId, Binder.getCallingUid())
4918 == TelephonyManager.CARRIER_PRIVILEGE_STATUS_HAS_ACCESS;
4919 boolean hasNetworkScanPermission =
4920 mApp.checkCallingOrSelfPermission(android.Manifest.permission.NETWORK_SCAN)
4921 == PERMISSION_GRANTED;
4922
4923 if (!hasCarrierPriv && !hasNetworkScanPermission) {
4924 return new SecurityException("permission.NETWORK_SCAN or carrier privileges is needed"
4925 + " for network scans without location access.");
Hall Liub2ac8ef2019-02-28 15:56:23 -08004926 }
4927
4928 if (request.getSpecifiers() != null && request.getSpecifiers().length > 0) {
4929 for (RadioAccessSpecifier ras : request.getSpecifiers()) {
Hall Liub2ac8ef2019-02-28 15:56:23 -08004930 if (ras.getChannels() != null && ras.getChannels().length > 0) {
4931 return new SecurityException("Specific channels must not be"
4932 + " scanned without location access.");
4933 }
4934 }
4935 }
4936
Hall Liub2ac8ef2019-02-28 15:56:23 -08004937 return null;
4938 }
4939
yinxu504e1392017-04-12 16:03:22 -07004940 /**
4941 * Stops an existing network scan with the given scanId.
yinxub1bed742017-04-17 11:45:04 -07004942 *
4943 * @param subId id of the subscription
4944 * @param scanId id of the scan that needs to be stopped
yinxu504e1392017-04-12 16:03:22 -07004945 */
4946 @Override
4947 public void stopNetworkScan(int subId, int scanId) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08004948 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
4949 mApp, subId, "stopNetworkScan");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004950
Hall Liu912dfd32019-04-25 14:02:26 -07004951 int callingUid = Binder.getCallingUid();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004952 final long identity = Binder.clearCallingIdentity();
4953 try {
Hall Liu912dfd32019-04-25 14:02:26 -07004954 mNetworkScanRequestTracker.stopNetworkScan(scanId, callingUid);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004955 } finally {
4956 Binder.restoreCallingIdentity(identity);
4957 }
yinxu504e1392017-04-12 16:03:22 -07004958 }
4959
4960 /**
Junda Liu84d15a22014-07-02 11:21:04 -07004961 * Get the calculated preferred network type.
4962 * Used for debugging incorrect network type.
4963 *
4964 * @return the preferred network type, defined in RILConstants.java.
4965 */
4966 @Override
Svet Ganovb320e182015-04-16 12:30:10 -07004967 public int getCalculatedPreferredNetworkType(String callingPackage) {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08004968 final Phone defaultPhone = getDefaultPhone();
4969 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(mApp, defaultPhone.getSubId(),
4970 callingPackage, "getCalculatedPreferredNetworkType")) {
Svet Ganovb320e182015-04-16 12:30:10 -07004971 return RILConstants.PREFERRED_NETWORK_MODE;
4972 }
4973
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004974 final long identity = Binder.clearCallingIdentity();
4975 try {
4976 // FIXME: need to get SubId from somewhere.
Nazanin Bakhshif782e562018-12-11 15:15:39 -08004977 return PhoneFactory.calculatePreferredNetworkType(defaultPhone.getContext(), 0);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004978 } finally {
4979 Binder.restoreCallingIdentity(identity);
4980 }
Junda Liu84d15a22014-07-02 11:21:04 -07004981 }
4982
4983 /**
Jake Hamby7c27be32014-03-03 13:25:59 -08004984 * Get the preferred network type.
4985 * Used for device configuration by some CDMA operators.
4986 *
4987 * @return the preferred network type, defined in RILConstants.java.
4988 */
4989 @Override
Stuart Scott54788802015-03-30 13:18:01 -07004990 public int getPreferredNetworkType(int subId) {
Pengquan Menga4009cb2018-12-20 11:00:24 -08004991 TelephonyPermissions
4992 .enforeceCallingOrSelfReadPrivilegedPhoneStatePermissionOrCarrierPrivilege(
4993 mApp, subId, "getPreferredNetworkType");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004994
4995 final long identity = Binder.clearCallingIdentity();
4996 try {
4997 if (DBG) log("getPreferredNetworkType");
4998 int[] result = (int[]) sendRequest(CMD_GET_PREFERRED_NETWORK_TYPE, null, subId);
4999 int networkType = (result != null ? result[0] : -1);
5000 if (DBG) log("getPreferredNetworkType: " + networkType);
5001 return networkType;
5002 } finally {
5003 Binder.restoreCallingIdentity(identity);
5004 }
Jake Hamby7c27be32014-03-03 13:25:59 -08005005 }
5006
5007 /**
5008 * Set the preferred network type.
5009 * Used for device configuration by some CDMA operators.
5010 *
5011 * @param networkType the preferred network type, defined in RILConstants.java.
5012 * @return true on success; false on any failure.
5013 */
5014 @Override
Stuart Scott54788802015-03-30 13:18:01 -07005015 public boolean setPreferredNetworkType(int subId, int networkType) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08005016 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
5017 mApp, subId, "setPreferredNetworkType");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005018
5019 final long identity = Binder.clearCallingIdentity();
5020 try {
5021 if (DBG) log("setPreferredNetworkType: subId " + subId + " type " + networkType);
5022 Boolean success = (Boolean) sendRequest(
5023 CMD_SET_PREFERRED_NETWORK_TYPE, networkType, subId);
5024 if (DBG) log("setPreferredNetworkType: " + (success ? "ok" : "fail"));
5025 if (success) {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08005026 Settings.Global.putInt(mApp.getContentResolver(),
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005027 Settings.Global.PREFERRED_NETWORK_MODE + subId, networkType);
5028 }
5029 return success;
5030 } finally {
5031 Binder.restoreCallingIdentity(identity);
Junda Liu80bc0d12014-07-14 16:36:44 -07005032 }
Jake Hamby7c27be32014-03-03 13:25:59 -08005033 }
Robert Greenwalted86e582014-05-21 20:03:20 -07005034
5035 /**
Miaoa84611c2019-03-15 09:21:10 +08005036 * Check whether DUN APN is required for tethering with subId.
Junda Liu475951f2014-11-07 16:45:03 -08005037 *
Miaoa84611c2019-03-15 09:21:10 +08005038 * @param subId the id of the subscription to require tethering.
Amit Mahajanfe58cdf2017-07-11 12:01:53 -07005039 * @return {@code true} if DUN APN is required for tethering.
Junda Liu475951f2014-11-07 16:45:03 -08005040 * @hide
5041 */
5042 @Override
SongFerngWangf08d8122019-11-15 14:58:44 +08005043 public boolean isTetheringApnRequiredForSubscriber(int subId) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08005044 enforceModifyPermission();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005045 final long identity = Binder.clearCallingIdentity();
Miaoa84611c2019-03-15 09:21:10 +08005046 final Phone phone = getPhone(subId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005047 try {
Miaoa84611c2019-03-15 09:21:10 +08005048 if (phone != null) {
5049 return phone.hasMatchedTetherApnSetting();
5050 } else {
5051 return false;
5052 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005053 } finally {
5054 Binder.restoreCallingIdentity(identity);
Junda Liu475951f2014-11-07 16:45:03 -08005055 }
Junda Liu475951f2014-11-07 16:45:03 -08005056 }
5057
5058 /**
Robert Greenwalted86e582014-05-21 20:03:20 -07005059 * Set mobile data enabled
5060 * Used by the user through settings etc to turn on/off mobile data
5061 *
5062 * @param enable {@code true} turn turn data on, else {@code false}
5063 */
5064 @Override
Malcolm Chen964682d2017-11-28 16:20:07 -08005065 public void setUserDataEnabled(int subId, boolean enable) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08005066 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
5067 mApp, subId, "setUserDataEnabled");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005068
5069 final long identity = Binder.clearCallingIdentity();
5070 try {
5071 int phoneId = mSubscriptionController.getPhoneId(subId);
5072 if (DBG) log("setUserDataEnabled: subId=" + subId + " phoneId=" + phoneId);
5073 Phone phone = PhoneFactory.getPhone(phoneId);
5074 if (phone != null) {
5075 if (DBG) log("setUserDataEnabled: subId=" + subId + " enable=" + enable);
Jack Yud79fba22018-12-13 11:51:28 -08005076 phone.getDataEnabledSettings().setUserDataEnabled(enable);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005077 } else {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08005078 loge("setUserDataEnabled: no phone found. Invalid subId=" + subId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005079 }
5080 } finally {
5081 Binder.restoreCallingIdentity(identity);
Wink Savillee7353bb2014-12-05 14:21:41 -08005082 }
Robert Greenwalted86e582014-05-21 20:03:20 -07005083 }
5084
5085 /**
Malcolm Chen964682d2017-11-28 16:20:07 -08005086 * Get the user enabled state of Mobile Data.
5087 *
5088 * TODO: remove and use isUserDataEnabled.
5089 * This can't be removed now because some vendor codes
5090 * calls through ITelephony directly while they should
5091 * use TelephonyManager.
5092 *
5093 * @return true on enabled
5094 */
5095 @Override
5096 public boolean getDataEnabled(int subId) {
5097 return isUserDataEnabled(subId);
5098 }
5099
5100 /**
5101 * Get whether mobile data is enabled per user setting.
5102 *
5103 * There are other factors deciding whether mobile data is actually enabled, but they are
5104 * not considered here. See {@link #isDataEnabled(int)} for more details.
Robert Greenwalt646120a2014-05-23 11:54:03 -07005105 *
Jeff Davidsona1920712016-11-18 17:05:56 -08005106 * Accepts either ACCESS_NETWORK_STATE, MODIFY_PHONE_STATE or carrier privileges.
Robert Greenwalted86e582014-05-21 20:03:20 -07005107 *
5108 * @return {@code true} if data is enabled else {@code false}
5109 */
5110 @Override
Malcolm Chen964682d2017-11-28 16:20:07 -08005111 public boolean isUserDataEnabled(int subId) {
Robert Greenwalt646120a2014-05-23 11:54:03 -07005112 try {
5113 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.ACCESS_NETWORK_STATE,
5114 null);
5115 } catch (Exception e) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08005116 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
5117 mApp, subId, "isUserDataEnabled");
Robert Greenwalt646120a2014-05-23 11:54:03 -07005118 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005119
5120 final long identity = Binder.clearCallingIdentity();
5121 try {
5122 int phoneId = mSubscriptionController.getPhoneId(subId);
5123 if (DBG) log("isUserDataEnabled: subId=" + subId + " phoneId=" + phoneId);
5124 Phone phone = PhoneFactory.getPhone(phoneId);
5125 if (phone != null) {
5126 boolean retVal = phone.isUserDataEnabled();
5127 if (DBG) log("isUserDataEnabled: subId=" + subId + " retVal=" + retVal);
5128 return retVal;
5129 } else {
5130 if (DBG) loge("isUserDataEnabled: no phone subId=" + subId + " retVal=false");
5131 return false;
5132 }
5133 } finally {
5134 Binder.restoreCallingIdentity(identity);
Malcolm Chen964682d2017-11-28 16:20:07 -08005135 }
5136 }
5137
5138 /**
5139 * Get whether mobile data is enabled.
5140 *
5141 * Comparable to {@link #isUserDataEnabled(int)}, this considers all factors deciding
5142 * whether mobile data is actually enabled.
5143 *
5144 * Accepts either ACCESS_NETWORK_STATE, MODIFY_PHONE_STATE or carrier privileges.
5145 *
5146 * @return {@code true} if data is enabled else {@code false}
5147 */
5148 @Override
5149 public boolean isDataEnabled(int subId) {
5150 try {
5151 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.ACCESS_NETWORK_STATE,
5152 null);
5153 } catch (Exception e) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08005154 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
5155 mApp, subId, "isDataEnabled");
Malcolm Chen964682d2017-11-28 16:20:07 -08005156 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005157
5158 final long identity = Binder.clearCallingIdentity();
5159 try {
5160 int phoneId = mSubscriptionController.getPhoneId(subId);
5161 if (DBG) log("isDataEnabled: subId=" + subId + " phoneId=" + phoneId);
5162 Phone phone = PhoneFactory.getPhone(phoneId);
5163 if (phone != null) {
Jack Yud79fba22018-12-13 11:51:28 -08005164 boolean retVal = phone.getDataEnabledSettings().isDataEnabled();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005165 if (DBG) log("isDataEnabled: subId=" + subId + " retVal=" + retVal);
5166 return retVal;
5167 } else {
5168 if (DBG) loge("isDataEnabled: no phone subId=" + subId + " retVal=false");
5169 return false;
5170 }
5171 } finally {
5172 Binder.restoreCallingIdentity(identity);
Wink Savillee7353bb2014-12-05 14:21:41 -08005173 }
Robert Greenwalted86e582014-05-21 20:03:20 -07005174 }
Shishir Agrawal60f9c952014-06-23 12:00:43 -07005175
Nazanin Bakhshi5d0636e2019-08-19 16:29:37 -07005176 private int getCarrierPrivilegeStatusFromCarrierConfigRules(int privilegeFromSim,
5177 Phone phone) {
5178 //load access rules from carrier configs, and check those as well: b/139133814
5179 SubscriptionController subController = SubscriptionController.getInstance();
5180 if (privilegeFromSim == TelephonyManager.CARRIER_PRIVILEGE_STATUS_HAS_ACCESS
5181 || subController == null) return privilegeFromSim;
5182
5183 int uid = Binder.getCallingUid();
5184 PackageManager pkgMgr = phone.getContext().getPackageManager();
5185 String[] packages = pkgMgr.getPackagesForUid(uid);
5186
5187 final long identity = Binder.clearCallingIdentity();
5188 try {
5189 SubscriptionInfo subInfo = subController.getSubscriptionInfo(phone.getSubId());
5190 SubscriptionManager subManager = (SubscriptionManager)
5191 phone.getContext().getSystemService(Context.TELEPHONY_SUBSCRIPTION_SERVICE);
5192 for (String pkg : packages) {
5193 if (subManager.canManageSubscription(subInfo, pkg)) {
5194 return TelephonyManager.CARRIER_PRIVILEGE_STATUS_HAS_ACCESS;
5195 }
5196 }
5197 return privilegeFromSim;
5198 } finally {
5199 Binder.restoreCallingIdentity(identity);
5200 }
5201 }
5202
5203 private int getCarrierPrivilegeStatusFromCarrierConfigRules(int privilegeFromSim, Phone phone,
5204 String pkgName) {
5205 //load access rules from carrier configs, and check those as well: b/139133814
5206 SubscriptionController subController = SubscriptionController.getInstance();
5207 if (privilegeFromSim == TelephonyManager.CARRIER_PRIVILEGE_STATUS_HAS_ACCESS
5208 || subController == null) return privilegeFromSim;
5209
5210 final long identity = Binder.clearCallingIdentity();
5211 try {
5212 SubscriptionInfo subInfo = subController.getSubscriptionInfo(phone.getSubId());
5213 SubscriptionManager subManager = (SubscriptionManager)
5214 phone.getContext().getSystemService(Context.TELEPHONY_SUBSCRIPTION_SERVICE);
5215 return subManager.canManageSubscription(subInfo, pkgName)
5216 ? TelephonyManager.CARRIER_PRIVILEGE_STATUS_HAS_ACCESS : privilegeFromSim;
5217 } finally {
5218 Binder.restoreCallingIdentity(identity);
5219 }
5220 }
5221
Shishir Agrawal60f9c952014-06-23 12:00:43 -07005222 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08005223 public int getCarrierPrivilegeStatus(int subId) {
5224 final Phone phone = getPhone(subId);
5225 if (phone == null) {
5226 loge("getCarrierPrivilegeStatus: Invalid subId");
5227 return TelephonyManager.CARRIER_PRIVILEGE_STATUS_NO_ACCESS;
5228 }
5229 UiccCard card = UiccController.getInstance().getUiccCard(phone.getPhoneId());
Shishir Agrawaleb8771e2014-07-22 11:24:08 -07005230 if (card == null) {
Shishir Agrawal5e5becd2014-11-18 11:38:23 -08005231 loge("getCarrierPrivilegeStatus: No UICC");
Shishir Agrawaleb8771e2014-07-22 11:24:08 -07005232 return TelephonyManager.CARRIER_PRIVILEGE_STATUS_RULES_NOT_LOADED;
5233 }
Nazanin Bakhshi5d0636e2019-08-19 16:29:37 -07005234
5235 return getCarrierPrivilegeStatusFromCarrierConfigRules(
5236 card.getCarrierPrivilegeStatusForCurrentTransaction(
5237 phone.getContext().getPackageManager()), phone);
Shishir Agrawal60f9c952014-06-23 12:00:43 -07005238 }
Junda Liu29340342014-07-10 15:23:27 -07005239
5240 @Override
Jeff Davidson7e17e312018-02-13 18:17:36 -08005241 public int getCarrierPrivilegeStatusForUid(int subId, int uid) {
5242 final Phone phone = getPhone(subId);
5243 if (phone == null) {
Taesu Leef8fbed92019-10-07 18:47:02 +09005244 loge("getCarrierPrivilegeStatusForUid: Invalid subId");
Jeff Davidson7e17e312018-02-13 18:17:36 -08005245 return TelephonyManager.CARRIER_PRIVILEGE_STATUS_NO_ACCESS;
5246 }
5247 UiccProfile profile =
5248 UiccController.getInstance().getUiccProfileForPhone(phone.getPhoneId());
5249 if (profile == null) {
Taesu Leef8fbed92019-10-07 18:47:02 +09005250 loge("getCarrierPrivilegeStatusForUid: No UICC");
Jeff Davidson7e17e312018-02-13 18:17:36 -08005251 return TelephonyManager.CARRIER_PRIVILEGE_STATUS_RULES_NOT_LOADED;
5252 }
Nazanin Bakhshi5d0636e2019-08-19 16:29:37 -07005253 return getCarrierPrivilegeStatusFromCarrierConfigRules(
5254 profile.getCarrierPrivilegeStatusForUid(
5255 phone.getContext().getPackageManager(), uid), phone);
Jeff Davidson7e17e312018-02-13 18:17:36 -08005256 }
5257
5258 @Override
chen xuf7e9fe82019-05-09 19:31:02 -07005259 public int checkCarrierPrivilegesForPackage(int subId, String pkgName) {
5260 if (TextUtils.isEmpty(pkgName)) {
Junda Liu317d70b2016-03-08 09:33:53 -08005261 return TelephonyManager.CARRIER_PRIVILEGE_STATUS_NO_ACCESS;
chen xuf7e9fe82019-05-09 19:31:02 -07005262 }
5263
5264 int phoneId = SubscriptionManager.getPhoneId(subId);
5265 UiccCard card = UiccController.getInstance().getUiccCard(phoneId);
Shishir Agrawaleb8771e2014-07-22 11:24:08 -07005266 if (card == null) {
chen xuf7e9fe82019-05-09 19:31:02 -07005267 loge("checkCarrierPrivilegesForPackage: No UICC on subId " + subId);
Shishir Agrawaleb8771e2014-07-22 11:24:08 -07005268 return TelephonyManager.CARRIER_PRIVILEGE_STATUS_RULES_NOT_LOADED;
5269 }
Nazanin Bakhshi5d0636e2019-08-19 16:29:37 -07005270 return getCarrierPrivilegeStatusFromCarrierConfigRules(
5271 card.getCarrierPrivilegeStatus(mApp.getPackageManager(), pkgName),
5272 getPhone(phoneId), pkgName);
Zach Johnson50ecba32015-05-19 00:24:21 -07005273 }
5274
5275 @Override
5276 public int checkCarrierPrivilegesForPackageAnyPhone(String pkgName) {
Junda Liu317d70b2016-03-08 09:33:53 -08005277 if (TextUtils.isEmpty(pkgName))
5278 return TelephonyManager.CARRIER_PRIVILEGE_STATUS_NO_ACCESS;
Zach Johnson50ecba32015-05-19 00:24:21 -07005279 int result = TelephonyManager.CARRIER_PRIVILEGE_STATUS_RULES_NOT_LOADED;
5280 for (int i = 0; i < TelephonyManager.getDefault().getPhoneCount(); i++) {
5281 UiccCard card = UiccController.getInstance().getUiccCard(i);
5282 if (card == null) {
Jonathan Basseri7d320df2015-06-16 12:17:08 -07005283 // No UICC in that slot.
Zach Johnson50ecba32015-05-19 00:24:21 -07005284 continue;
5285 }
5286
Nazanin Bakhshi5d0636e2019-08-19 16:29:37 -07005287 result = getCarrierPrivilegeStatusFromCarrierConfigRules(
5288 card.getCarrierPrivilegeStatus(mApp.getPackageManager(), pkgName),
5289 getPhone(i), pkgName);
Zach Johnson50ecba32015-05-19 00:24:21 -07005290 if (result == TelephonyManager.CARRIER_PRIVILEGE_STATUS_HAS_ACCESS) {
5291 break;
5292 }
5293 }
5294
5295 return result;
Junda Liu29340342014-07-10 15:23:27 -07005296 }
Derek Tan89e89d42014-07-08 17:00:10 -07005297
5298 @Override
Junda Liue64de782015-04-16 17:19:16 -07005299 public List<String> getCarrierPackageNamesForIntentAndPhone(Intent intent, int phoneId) {
5300 if (!SubscriptionManager.isValidPhoneId(phoneId)) {
5301 loge("phoneId " + phoneId + " is not valid.");
5302 return null;
5303 }
5304 UiccCard card = UiccController.getInstance().getUiccCard(phoneId);
Shishir Agrawaleb6439a2014-07-21 13:19:38 -07005305 if (card == null) {
Taesu Leef8fbed92019-10-07 18:47:02 +09005306 loge("getCarrierPackageNamesForIntentAndPhone: No UICC");
Shishir Agrawaleb6439a2014-07-21 13:19:38 -07005307 return null ;
5308 }
Nazanin Bakhshif782e562018-12-11 15:15:39 -08005309 return card.getCarrierPackageNamesForIntent(mApp.getPackageManager(), intent);
Shishir Agrawaleb6439a2014-07-21 13:19:38 -07005310 }
5311
Amith Yamasani6e118872016-02-19 12:53:51 -08005312 @Override
chen xuf7e9fe82019-05-09 19:31:02 -07005313 public List<String> getPackagesWithCarrierPrivileges(int phoneId) {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08005314 PackageManager pm = mApp.getPackageManager();
Amith Yamasani6e118872016-02-19 12:53:51 -08005315 List<String> privilegedPackages = new ArrayList<>();
5316 List<PackageInfo> packages = null;
chen xuf7e9fe82019-05-09 19:31:02 -07005317 UiccCard card = UiccController.getInstance().getUiccCard(phoneId);
5318 // has UICC in that slot.
5319 if (card != null) {
Amith Yamasani6e118872016-02-19 12:53:51 -08005320 if (card.hasCarrierPrivilegeRules()) {
5321 if (packages == null) {
5322 // Only check packages in user 0 for now
5323 packages = pm.getInstalledPackagesAsUser(
Nazanin Bakhshi5d0636e2019-08-19 16:29:37 -07005324 PackageManager.MATCH_DISABLED_COMPONENTS
5325 | PackageManager.MATCH_DISABLED_UNTIL_USED_COMPONENTS
Cheonho Park17089c62019-08-01 15:23:12 +09005326 | PackageManager.GET_SIGNING_CERTIFICATES,
5327 UserHandle.USER_SYSTEM);
Amith Yamasani6e118872016-02-19 12:53:51 -08005328 }
5329 for (int p = packages.size() - 1; p >= 0; p--) {
5330 PackageInfo pkgInfo = packages.get(p);
5331 if (pkgInfo != null && pkgInfo.packageName != null
5332 && card.getCarrierPrivilegeStatus(pkgInfo)
chen xuf7e9fe82019-05-09 19:31:02 -07005333 == TelephonyManager.CARRIER_PRIVILEGE_STATUS_HAS_ACCESS) {
Amith Yamasani6e118872016-02-19 12:53:51 -08005334 privilegedPackages.add(pkgInfo.packageName);
5335 }
5336 }
5337 }
5338 }
5339 return privilegedPackages;
5340 }
5341
chen xuf7e9fe82019-05-09 19:31:02 -07005342 @Override
5343 public List<String> getPackagesWithCarrierPrivilegesForAllPhones() {
Shuo Qiand54f9f32019-12-03 23:40:18 +00005344 enforceReadPrivilegedPermission("getPackagesWithCarrierPrivilegesForAllPhones");
5345
5346 final long identity = Binder.clearCallingIdentity();
5347
chen xuf7e9fe82019-05-09 19:31:02 -07005348 List<String> privilegedPackages = new ArrayList<>();
Shuo Qiand54f9f32019-12-03 23:40:18 +00005349 try {
5350 for (int i = 0; i < TelephonyManager.getDefault().getPhoneCount(); i++) {
5351 privilegedPackages.addAll(getPackagesWithCarrierPrivileges(i));
5352 }
5353 } finally {
5354 Binder.restoreCallingIdentity(identity);
chen xuf7e9fe82019-05-09 19:31:02 -07005355 }
5356 return privilegedPackages;
5357 }
5358
Wink Savilleb564aae2014-10-23 10:18:09 -07005359 private String getIccId(int subId) {
Sanket Padawe356d7632015-06-22 14:03:32 -07005360 final Phone phone = getPhone(subId);
5361 UiccCard card = phone == null ? null : phone.getUiccCard();
Derek Tan97ebb422014-09-05 16:55:38 -07005362 if (card == null) {
5363 loge("getIccId: No UICC");
5364 return null;
5365 }
5366 String iccId = card.getIccId();
5367 if (TextUtils.isEmpty(iccId)) {
5368 loge("getIccId: ICC ID is null or empty.");
5369 return null;
5370 }
5371 return iccId;
5372 }
5373
Shishir Agrawaleb6439a2014-07-21 13:19:38 -07005374 @Override
Jeff Sharkey85190e62014-12-05 09:40:12 -08005375 public boolean setLine1NumberForDisplayForSubscriber(int subId, String alphaTag,
5376 String number) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08005377 TelephonyPermissions.enforceCallingOrSelfCarrierPrivilege(
5378 subId, "setLine1NumberForDisplayForSubscriber");
Derek Tan97ebb422014-09-05 16:55:38 -07005379
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005380 final long identity = Binder.clearCallingIdentity();
5381 try {
5382 final String iccId = getIccId(subId);
5383 final Phone phone = getPhone(subId);
5384 if (phone == null) {
5385 return false;
5386 }
5387 final String subscriberId = phone.getSubscriberId();
5388
5389 if (DBG_MERGE) {
5390 Slog.d(LOG_TAG, "Setting line number for ICC=" + iccId + ", subscriberId="
5391 + subscriberId + " to " + number);
5392 }
5393
5394 if (TextUtils.isEmpty(iccId)) {
5395 return false;
5396 }
5397
5398 final SharedPreferences.Editor editor = mTelephonySharedPreferences.edit();
5399
5400 final String alphaTagPrefKey = PREF_CARRIERS_ALPHATAG_PREFIX + iccId;
5401 if (alphaTag == null) {
5402 editor.remove(alphaTagPrefKey);
5403 } else {
5404 editor.putString(alphaTagPrefKey, alphaTag);
5405 }
5406
5407 // Record both the line number and IMSI for this ICCID, since we need to
5408 // track all merged IMSIs based on line number
5409 final String numberPrefKey = PREF_CARRIERS_NUMBER_PREFIX + iccId;
5410 final String subscriberPrefKey = PREF_CARRIERS_SUBSCRIBER_PREFIX + iccId;
5411 if (number == null) {
5412 editor.remove(numberPrefKey);
5413 editor.remove(subscriberPrefKey);
5414 } else {
5415 editor.putString(numberPrefKey, number);
5416 editor.putString(subscriberPrefKey, subscriberId);
5417 }
5418
5419 editor.commit();
5420 return true;
5421 } finally {
5422 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07005423 }
Derek Tan7226c842014-07-02 17:42:23 -07005424 }
5425
5426 @Override
Svet Ganovb320e182015-04-16 12:30:10 -07005427 public String getLine1NumberForDisplay(int subId, String callingPackage) {
Makoto Onukifee69342015-06-29 14:44:50 -07005428 // This is open to apps with WRITE_SMS.
Jeff Davidson7e17e312018-02-13 18:17:36 -08005429 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneNumber(
Jeff Davidson913390f2018-02-23 17:11:49 -08005430 mApp, subId, callingPackage, "getLine1NumberForDisplay")) {
Amit Mahajan9cf11512015-11-09 11:40:48 -08005431 if (DBG_MERGE) log("getLine1NumberForDisplay returning null due to permission");
Svet Ganovb320e182015-04-16 12:30:10 -07005432 return null;
5433 }
Derek Tan97ebb422014-09-05 16:55:38 -07005434
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005435 final long identity = Binder.clearCallingIdentity();
5436 try {
5437 String iccId = getIccId(subId);
5438 if (iccId != null) {
5439 String numberPrefKey = PREF_CARRIERS_NUMBER_PREFIX + iccId;
5440 if (DBG_MERGE) {
5441 log("getLine1NumberForDisplay returning "
5442 + mTelephonySharedPreferences.getString(numberPrefKey, null));
5443 }
5444 return mTelephonySharedPreferences.getString(numberPrefKey, null);
Amit Mahajan9cf11512015-11-09 11:40:48 -08005445 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005446 if (DBG_MERGE) log("getLine1NumberForDisplay returning null as iccId is null");
5447 return null;
5448 } finally {
5449 Binder.restoreCallingIdentity(identity);
Derek Tan7226c842014-07-02 17:42:23 -07005450 }
Derek Tan7226c842014-07-02 17:42:23 -07005451 }
5452
5453 @Override
Svet Ganovb320e182015-04-16 12:30:10 -07005454 public String getLine1AlphaTagForDisplay(int subId, String callingPackage) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08005455 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
Jeff Davidson913390f2018-02-23 17:11:49 -08005456 mApp, subId, callingPackage, "getLine1AlphaTagForDisplay")) {
Svet Ganovb320e182015-04-16 12:30:10 -07005457 return null;
5458 }
Derek Tan97ebb422014-09-05 16:55:38 -07005459
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005460 final long identity = Binder.clearCallingIdentity();
5461 try {
5462 String iccId = getIccId(subId);
5463 if (iccId != null) {
5464 String alphaTagPrefKey = PREF_CARRIERS_ALPHATAG_PREFIX + iccId;
5465 return mTelephonySharedPreferences.getString(alphaTagPrefKey, null);
5466 }
5467 return null;
5468 } finally {
5469 Binder.restoreCallingIdentity(identity);
Derek Tan7226c842014-07-02 17:42:23 -07005470 }
Derek Tan7226c842014-07-02 17:42:23 -07005471 }
Shishir Agrawalb1ebf8c2014-07-17 16:32:41 -07005472
5473 @Override
Jordan Liub49b04b2019-05-06 14:45:15 -07005474 public String[] getMergedSubscriberIds(int subId, String callingPackage) {
Jeff Davidson913390f2018-02-23 17:11:49 -08005475 // This API isn't public, so no need to provide a valid subscription ID - we're not worried
5476 // about carrier-privileged callers not having access.
Jeff Davidson7e17e312018-02-13 18:17:36 -08005477 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
Jeff Davidson913390f2018-02-23 17:11:49 -08005478 mApp, SubscriptionManager.INVALID_SUBSCRIPTION_ID, callingPackage,
5479 "getMergedSubscriberIds")) {
Fyodor Kupolov8e53b0b2015-06-17 13:17:50 -07005480 return null;
5481 }
Jeff Sharkey85190e62014-12-05 09:40:12 -08005482
Jordan Liub49b04b2019-05-06 14:45:15 -07005483 // Clear calling identity, when calling TelephonyManager, because callerUid must be
5484 // the process, where TelephonyManager was instantiated.
5485 // Otherwise AppOps check will fail.
Fyodor Kupolov8e53b0b2015-06-17 13:17:50 -07005486 final long identity = Binder.clearCallingIdentity();
5487 try {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08005488 final Context context = mApp;
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005489 final TelephonyManager tele = TelephonyManager.from(context);
5490 final SubscriptionManager sub = SubscriptionManager.from(context);
5491
5492 // Figure out what subscribers are currently active
5493 final ArraySet<String> activeSubscriberIds = new ArraySet<>();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005494
Jordan Liub49b04b2019-05-06 14:45:15 -07005495 // Only consider subs which match the current subId
5496 // This logic can be simplified. See b/131189269 for progress.
5497 if (isActiveSubscription(subId)) {
Fyodor Kupolov8e53b0b2015-06-17 13:17:50 -07005498 activeSubscriberIds.add(tele.getSubscriberId(subId));
5499 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005500
5501 // First pass, find a number override for an active subscriber
5502 String mergeNumber = null;
5503 final Map<String, ?> prefs = mTelephonySharedPreferences.getAll();
5504 for (String key : prefs.keySet()) {
5505 if (key.startsWith(PREF_CARRIERS_SUBSCRIBER_PREFIX)) {
5506 final String subscriberId = (String) prefs.get(key);
5507 if (activeSubscriberIds.contains(subscriberId)) {
5508 final String iccId = key.substring(
5509 PREF_CARRIERS_SUBSCRIBER_PREFIX.length());
5510 final String numberKey = PREF_CARRIERS_NUMBER_PREFIX + iccId;
5511 mergeNumber = (String) prefs.get(numberKey);
5512 if (DBG_MERGE) {
5513 Slog.d(LOG_TAG, "Found line number " + mergeNumber
5514 + " for active subscriber " + subscriberId);
5515 }
5516 if (!TextUtils.isEmpty(mergeNumber)) {
5517 break;
5518 }
5519 }
5520 }
5521 }
5522
5523 // Shortcut when no active merged subscribers
5524 if (TextUtils.isEmpty(mergeNumber)) {
5525 return null;
5526 }
5527
5528 // Second pass, find all subscribers under that line override
5529 final ArraySet<String> result = new ArraySet<>();
5530 for (String key : prefs.keySet()) {
5531 if (key.startsWith(PREF_CARRIERS_NUMBER_PREFIX)) {
5532 final String number = (String) prefs.get(key);
5533 if (mergeNumber.equals(number)) {
5534 final String iccId = key.substring(PREF_CARRIERS_NUMBER_PREFIX.length());
5535 final String subscriberKey = PREF_CARRIERS_SUBSCRIBER_PREFIX + iccId;
5536 final String subscriberId = (String) prefs.get(subscriberKey);
5537 if (!TextUtils.isEmpty(subscriberId)) {
5538 result.add(subscriberId);
5539 }
5540 }
5541 }
5542 }
5543
5544 final String[] resultArray = result.toArray(new String[result.size()]);
5545 Arrays.sort(resultArray);
5546 if (DBG_MERGE) {
5547 Slog.d(LOG_TAG,
5548 "Found subscribers " + Arrays.toString(resultArray) + " after merge");
5549 }
5550 return resultArray;
Fyodor Kupolov8e53b0b2015-06-17 13:17:50 -07005551 } finally {
5552 Binder.restoreCallingIdentity(identity);
Jeff Sharkey85190e62014-12-05 09:40:12 -08005553 }
Jeff Sharkey85190e62014-12-05 09:40:12 -08005554 }
5555
5556 @Override
Malcolm Chen6ca97372019-07-01 16:28:21 -07005557 public String[] getMergedSubscriberIdsFromGroup(int subId, String callingPackage) {
5558 enforceReadPrivilegedPermission("getMergedSubscriberIdsFromGroup");
5559
5560 final long identity = Binder.clearCallingIdentity();
5561 try {
5562 final TelephonyManager telephonyManager = mApp.getSystemService(
5563 TelephonyManager.class);
5564 String subscriberId = telephonyManager.getSubscriberId(subId);
5565 if (subscriberId == null) {
5566 if (DBG) {
5567 log("getMergedSubscriberIdsFromGroup can't find subscriberId for subId "
5568 + subId);
5569 }
5570 return null;
5571 }
5572
5573 final SubscriptionInfo info = SubscriptionController.getInstance()
5574 .getSubscriptionInfo(subId);
5575 final ParcelUuid groupUuid = info.getGroupUuid();
5576 // If it doesn't belong to any group, return just subscriberId of itself.
5577 if (groupUuid == null) {
5578 return new String[]{subscriberId};
5579 }
5580
5581 // Get all subscriberIds from the group.
5582 final List<String> mergedSubscriberIds = new ArrayList<>();
5583 final List<SubscriptionInfo> groupInfos = SubscriptionController.getInstance()
5584 .getSubscriptionsInGroup(groupUuid, mApp.getOpPackageName());
5585 for (SubscriptionInfo subInfo : groupInfos) {
5586 subscriberId = telephonyManager.getSubscriberId(subInfo.getSubscriptionId());
5587 if (subscriberId != null) {
5588 mergedSubscriberIds.add(subscriberId);
5589 }
5590 }
5591
5592 return mergedSubscriberIds.toArray(new String[mergedSubscriberIds.size()]);
5593 } finally {
5594 Binder.restoreCallingIdentity(identity);
5595
5596 }
5597 }
5598
5599 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08005600 public boolean setOperatorBrandOverride(int subId, String brand) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08005601 TelephonyPermissions.enforceCallingOrSelfCarrierPrivilege(
5602 subId, "setOperatorBrandOverride");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005603
5604 final long identity = Binder.clearCallingIdentity();
5605 try {
5606 final Phone phone = getPhone(subId);
5607 return phone == null ? false : phone.setOperatorBrandOverride(brand);
5608 } finally {
5609 Binder.restoreCallingIdentity(identity);
5610 }
Shishir Agrawalb1ebf8c2014-07-17 16:32:41 -07005611 }
Steven Liu4bf01bc2014-07-17 11:05:29 -05005612
5613 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08005614 public boolean setRoamingOverride(int subId, List<String> gsmRoamingList,
Shishir Agrawal621a47c2014-12-01 10:25:09 -08005615 List<String> gsmNonRoamingList, List<String> cdmaRoamingList,
5616 List<String> cdmaNonRoamingList) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08005617 TelephonyPermissions.enforceCallingOrSelfCarrierPrivilege(subId, "setRoamingOverride");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005618
5619 final long identity = Binder.clearCallingIdentity();
5620 try {
5621 final Phone phone = getPhone(subId);
5622 if (phone == null) {
5623 return false;
5624 }
5625 return phone.setRoamingOverride(gsmRoamingList, gsmNonRoamingList, cdmaRoamingList,
5626 cdmaNonRoamingList);
5627 } finally {
5628 Binder.restoreCallingIdentity(identity);
Shishir Agrawalc04d9752016-02-19 10:41:00 -08005629 }
Shishir Agrawal621a47c2014-12-01 10:25:09 -08005630 }
5631
5632 @Override
Shuo Qian850e4d6a2018-04-25 21:02:08 +00005633 @Deprecated
5634 public int invokeOemRilRequestRaw(byte[] oemReq, byte[] oemResp) {
5635 enforceModifyPermission();
5636
5637 int returnValue = 0;
5638 try {
vagdeviaf9a5b92018-08-15 16:01:53 -07005639 AsyncResult result = (AsyncResult) sendRequest(CMD_INVOKE_OEM_RIL_REQUEST_RAW, oemReq);
Shuo Qian850e4d6a2018-04-25 21:02:08 +00005640 if(result.exception == null) {
5641 if (result.result != null) {
5642 byte[] responseData = (byte[])(result.result);
5643 if(responseData.length > oemResp.length) {
5644 Log.w(LOG_TAG, "Buffer to copy response too small: Response length is " +
5645 responseData.length + "bytes. Buffer Size is " +
5646 oemResp.length + "bytes.");
5647 }
5648 System.arraycopy(responseData, 0, oemResp, 0, responseData.length);
5649 returnValue = responseData.length;
5650 }
5651 } else {
5652 CommandException ex = (CommandException) result.exception;
5653 returnValue = ex.getCommandError().ordinal();
5654 if(returnValue > 0) returnValue *= -1;
5655 }
5656 } catch (RuntimeException e) {
5657 Log.w(LOG_TAG, "sendOemRilRequestRaw: Runtime Exception");
5658 returnValue = (CommandException.Error.GENERIC_FAILURE.ordinal());
5659 if(returnValue > 0) returnValue *= -1;
5660 }
5661
5662 return returnValue;
5663 }
5664
5665 @Override
Wink Saville5d475dd2014-10-17 15:00:58 -07005666 public void setRadioCapability(RadioAccessFamily[] rafs) {
5667 try {
5668 ProxyController.getInstance().setRadioCapability(rafs);
5669 } catch (RuntimeException e) {
5670 Log.w(LOG_TAG, "setRadioCapability: Runtime Exception");
5671 }
5672 }
5673
5674 @Override
Robert Greenwalt36b23af2015-07-06 17:59:14 -07005675 public int getRadioAccessFamily(int phoneId, String callingPackage) {
Jeff Davidson913390f2018-02-23 17:11:49 -08005676 Phone phone = PhoneFactory.getPhone(phoneId);
chen xub97461a2018-10-26 14:17:57 -07005677 int raf = RadioAccessFamily.RAF_UNKNOWN;
Jeff Davidson913390f2018-02-23 17:11:49 -08005678 if (phone == null) {
chen xub97461a2018-10-26 14:17:57 -07005679 return raf;
Jeff Davidson913390f2018-02-23 17:11:49 -08005680 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005681 final long identity = Binder.clearCallingIdentity();
5682 try {
chen xub97461a2018-10-26 14:17:57 -07005683 TelephonyPermissions
5684 .enforeceCallingOrSelfReadPrivilegedPhoneStatePermissionOrCarrierPrivilege(
5685 mApp, phone.getSubId(), "getRadioAccessFamily");
5686 raf = ProxyController.getInstance().getRadioAccessFamily(phoneId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005687 } finally {
5688 Binder.restoreCallingIdentity(identity);
5689 }
chen xub97461a2018-10-26 14:17:57 -07005690 return raf;
Wink Saville5d475dd2014-10-17 15:00:58 -07005691 }
Andrew Leedf14ead2014-10-17 14:22:52 -07005692
5693 @Override
5694 public void enableVideoCalling(boolean enable) {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08005695 final Phone defaultPhone = getDefaultPhone();
Andrew Leedf14ead2014-10-17 14:22:52 -07005696 enforceModifyPermission();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005697
5698 final long identity = Binder.clearCallingIdentity();
5699 try {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08005700 ImsManager.getInstance(defaultPhone.getContext(),
5701 defaultPhone.getPhoneId()).setVtSetting(enable);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005702 } finally {
5703 Binder.restoreCallingIdentity(identity);
5704 }
Andrew Leedf14ead2014-10-17 14:22:52 -07005705 }
5706
5707 @Override
Svet Ganovb320e182015-04-16 12:30:10 -07005708 public boolean isVideoCallingEnabled(String callingPackage) {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08005709 final Phone defaultPhone = getDefaultPhone();
Amit Mahajan578e53d2018-03-20 16:18:38 +00005710 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
Nazanin Bakhshif782e562018-12-11 15:15:39 -08005711 mApp, defaultPhone.getSubId(), callingPackage, "isVideoCallingEnabled")) {
Amit Mahajan578e53d2018-03-20 16:18:38 +00005712 return false;
5713 }
Svet Ganovb320e182015-04-16 12:30:10 -07005714
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005715 final long identity = Binder.clearCallingIdentity();
5716 try {
5717 // Check the user preference and the system-level IMS setting. Even if the user has
5718 // enabled video calling, if IMS is disabled we aren't able to support video calling.
5719 // In the long run, we may instead need to check if there exists a connection service
5720 // which can support video calling.
5721 ImsManager imsManager =
Nazanin Bakhshif782e562018-12-11 15:15:39 -08005722 ImsManager.getInstance(defaultPhone.getContext(), defaultPhone.getPhoneId());
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005723 return imsManager.isVtEnabledByPlatform()
5724 && imsManager.isEnhanced4gLteModeSettingEnabledByUser()
5725 && imsManager.isVtEnabledByUser();
5726 } finally {
5727 Binder.restoreCallingIdentity(identity);
5728 }
Andrew Leedf14ead2014-10-17 14:22:52 -07005729 }
Libin.Tang@motorola.comafe82642014-12-18 13:27:53 -06005730
Andrew Leea1239f22015-03-02 17:44:07 -08005731 @Override
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005732 public boolean canChangeDtmfToneLength(int subId, String callingPackage) {
5733 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
5734 mApp, subId, callingPackage, "isVideoCallingEnabled")) {
5735 return false;
5736 }
5737
5738 final long identity = Binder.clearCallingIdentity();
5739 try {
5740 CarrierConfigManager configManager =
5741 (CarrierConfigManager) mApp.getSystemService(Context.CARRIER_CONFIG_SERVICE);
Nazanin Bakhshif782e562018-12-11 15:15:39 -08005742 return configManager.getConfigForSubId(subId)
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005743 .getBoolean(CarrierConfigManager.KEY_DTMF_TYPE_ENABLED_BOOL);
5744 } finally {
5745 Binder.restoreCallingIdentity(identity);
5746 }
Andrew Leea1239f22015-03-02 17:44:07 -08005747 }
5748
5749 @Override
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005750 public boolean isWorldPhone(int subId, String callingPackage) {
5751 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
5752 mApp, subId, callingPackage, "isVideoCallingEnabled")) {
5753 return false;
5754 }
5755
5756 final long identity = Binder.clearCallingIdentity();
5757 try {
5758 CarrierConfigManager configManager =
5759 (CarrierConfigManager) mApp.getSystemService(Context.CARRIER_CONFIG_SERVICE);
Nazanin Bakhshif782e562018-12-11 15:15:39 -08005760 return configManager.getConfigForSubId(subId)
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005761 .getBoolean(CarrierConfigManager.KEY_WORLD_PHONE_BOOL);
5762 } finally {
5763 Binder.restoreCallingIdentity(identity);
5764 }
Andrew Leea1239f22015-03-02 17:44:07 -08005765 }
5766
Andrew Lee9431b832015-03-09 18:46:45 -07005767 @Override
5768 public boolean isTtyModeSupported() {
Tyler Gunn77ee9382019-10-31 13:08:23 -07005769 TelecomManager telecomManager = mApp.getSystemService(TelecomManager.class);
Wooki Wu1f82f7a2016-02-15 15:59:58 +08005770 return telecomManager.isTtySupported();
Andrew Lee9431b832015-03-09 18:46:45 -07005771 }
5772
5773 @Override
5774 public boolean isHearingAidCompatibilitySupported() {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005775 final long identity = Binder.clearCallingIdentity();
5776 try {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08005777 return mApp.getResources().getBoolean(R.bool.hac_enabled);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005778 } finally {
5779 Binder.restoreCallingIdentity(identity);
5780 }
Andrew Lee9431b832015-03-09 18:46:45 -07005781 }
5782
Hall Liuf6668912018-10-31 17:05:23 -07005783 /**
5784 * Determines whether the device currently supports RTT (Real-time text). Based both on carrier
5785 * support for the feature and device firmware support.
5786 *
5787 * @return {@code true} if the device and carrier both support RTT, {@code false} otherwise.
5788 */
5789 @Override
5790 public boolean isRttSupported(int subscriptionId) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005791 final long identity = Binder.clearCallingIdentity();
Nazanin Bakhshif782e562018-12-11 15:15:39 -08005792 final Phone phone = getPhone(subscriptionId);
5793 if (phone == null) {
5794 loge("isRttSupported: no Phone found. Invalid subId:" + subscriptionId);
5795 return false;
5796 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005797 try {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08005798 boolean isCarrierSupported = mApp.getCarrierConfigForSubId(subscriptionId).getBoolean(
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005799 CarrierConfigManager.KEY_RTT_SUPPORTED_BOOL);
5800 boolean isDeviceSupported =
Nazanin Bakhshif782e562018-12-11 15:15:39 -08005801 phone.getContext().getResources().getBoolean(R.bool.config_support_rtt);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005802 return isCarrierSupported && isDeviceSupported;
5803 } finally {
5804 Binder.restoreCallingIdentity(identity);
5805 }
Hall Liu98187582018-01-22 19:15:32 -08005806 }
5807
Hall Liuf6668912018-10-31 17:05:23 -07005808 /**
Hall Liu6a06be62019-07-23 18:39:00 -07005809 * Determines whether the user has turned on RTT. If the carrier wants to ignore the user-set
5810 * RTT setting, will return true if the device and carrier both support RTT.
5811 * Otherwise. only returns true if the device and carrier both also support RTT.
Hall Liuf6668912018-10-31 17:05:23 -07005812 */
5813 public boolean isRttEnabled(int subscriptionId) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005814 final long identity = Binder.clearCallingIdentity();
5815 try {
Hall Liu81eb08c2019-10-29 16:50:20 -07005816 return isRttSupported(subscriptionId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005817 } finally {
5818 Binder.restoreCallingIdentity(identity);
5819 }
Hall Liu3ad5f012018-04-06 16:23:39 -07005820 }
5821
Sanket Padawe7310cc72015-01-14 09:53:20 -08005822 /**
5823 * Returns the unique device ID of phone, for example, the IMEI for
5824 * GSM and the MEID for CDMA phones. Return null if device ID is not available.
5825 *
5826 * <p>Requires Permission:
5827 * {@link android.Manifest.permission#READ_PHONE_STATE READ_PHONE_STATE}
5828 */
5829 @Override
Svet Ganovb320e182015-04-16 12:30:10 -07005830 public String getDeviceId(String callingPackage) {
Sanket Padawe7310cc72015-01-14 09:53:20 -08005831 final Phone phone = PhoneFactory.getPhone(0);
Jeff Davidson913390f2018-02-23 17:11:49 -08005832 if (phone == null) {
Sanket Padawe7310cc72015-01-14 09:53:20 -08005833 return null;
5834 }
Jeff Davidson913390f2018-02-23 17:11:49 -08005835 int subId = phone.getSubId();
Michael Groover70af6dc2018-10-01 16:23:15 -07005836 if (!TelephonyPermissions.checkCallingOrSelfReadDeviceIdentifiers(mApp, subId,
5837 callingPackage, "getDeviceId")) {
Jeff Davidson913390f2018-02-23 17:11:49 -08005838 return null;
5839 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005840
5841 final long identity = Binder.clearCallingIdentity();
5842 try {
5843 return phone.getDeviceId();
5844 } finally {
5845 Binder.restoreCallingIdentity(identity);
5846 }
Sanket Padawe7310cc72015-01-14 09:53:20 -08005847 }
5848
Ping Sunc67b7c22016-03-02 19:16:45 +08005849 /**
5850 * {@hide}
5851 * Returns the IMS Registration Status on a particular subid
5852 *
5853 * @param subId
5854 */
Brad Ebinger1f2b5082018-02-08 16:11:32 -08005855 public boolean isImsRegistered(int subId) {
Ping Sunc67b7c22016-03-02 19:16:45 +08005856 Phone phone = getPhone(subId);
5857 if (phone != null) {
5858 return phone.isImsRegistered();
5859 } else {
5860 return false;
5861 }
5862 }
5863
Santos Cordon7a1885b2015-02-03 11:15:19 -08005864 @Override
5865 public int getSubIdForPhoneAccount(PhoneAccount phoneAccount) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005866 final long identity = Binder.clearCallingIdentity();
5867 try {
5868 return PhoneUtils.getSubIdForPhoneAccount(phoneAccount);
5869 } finally {
5870 Binder.restoreCallingIdentity(identity);
5871 }
Santos Cordon7a1885b2015-02-03 11:15:19 -08005872 }
Nathan Harolddcfc7932015-03-18 10:01:20 -07005873
Tyler Gunnf70ed162019-04-03 15:28:53 -07005874 @Override
Shuo Qian0762a782019-10-30 16:33:31 -07005875 public int getSubIdForPhoneAccountHandle(
5876 PhoneAccountHandle phoneAccountHandle, String callingPackage) {
5877 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(mApp, getDefaultSubscription(),
5878 callingPackage, "getSubIdForPhoneAccountHandle")) {
5879 throw new SecurityException("Requires READ_PHONE_STATE permission.");
5880 }
5881 final long identity = Binder.clearCallingIdentity();
5882 try {
5883 return PhoneUtils.getSubIdForPhoneAccountHandle(phoneAccountHandle);
5884 } finally {
5885 Binder.restoreCallingIdentity(identity);
5886 }
5887 }
5888
5889 @Override
Tyler Gunnf70ed162019-04-03 15:28:53 -07005890 public @Nullable PhoneAccountHandle getPhoneAccountHandleForSubscriptionId(int subscriptionId) {
5891 final long identity = Binder.clearCallingIdentity();
5892 try {
5893 Phone phone = getPhone(subscriptionId);
5894 if (phone == null) {
5895 return null;
5896 }
5897 return PhoneUtils.makePstnPhoneAccountHandle(phone);
5898 } finally {
5899 Binder.restoreCallingIdentity(identity);
5900 }
5901 }
5902
Brad Ebinger1f2b5082018-02-08 16:11:32 -08005903 /**
5904 * @return the VoWiFi calling availability.
Nathan Haroldc55097a2015-03-11 18:14:50 -07005905 */
Brad Ebinger1f2b5082018-02-08 16:11:32 -08005906 public boolean isWifiCallingAvailable(int subId) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005907 final long identity = Binder.clearCallingIdentity();
5908 try {
5909 Phone phone = getPhone(subId);
5910 if (phone != null) {
5911 return phone.isWifiCallingEnabled();
5912 } else {
5913 return false;
5914 }
5915 } finally {
5916 Binder.restoreCallingIdentity(identity);
Brad Ebinger1f2b5082018-02-08 16:11:32 -08005917 }
Nathan Haroldc55097a2015-03-11 18:14:50 -07005918 }
5919
Brad Ebinger1f2b5082018-02-08 16:11:32 -08005920 /**
Brad Ebinger1f2b5082018-02-08 16:11:32 -08005921 * @return the VT calling availability.
Etan Cohen3b7a1bc2015-05-28 15:57:13 -07005922 */
Brad Ebinger1f2b5082018-02-08 16:11:32 -08005923 public boolean isVideoTelephonyAvailable(int subId) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005924 final long identity = Binder.clearCallingIdentity();
5925 try {
5926 Phone phone = getPhone(subId);
5927 if (phone != null) {
5928 return phone.isVideoEnabled();
5929 } else {
5930 return false;
5931 }
5932 } finally {
5933 Binder.restoreCallingIdentity(identity);
Brad Ebinger1f2b5082018-02-08 16:11:32 -08005934 }
5935 }
5936
5937 /**
5938 * @return the IMS registration technology for the MMTEL feature. Valid return values are
5939 * defined in {@link ImsRegistrationImplBase}.
5940 */
5941 public @ImsRegistrationImplBase.ImsRegistrationTech int getImsRegTechnologyForMmTel(int subId) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005942 final long identity = Binder.clearCallingIdentity();
5943 try {
5944 Phone phone = getPhone(subId);
5945 if (phone != null) {
5946 return phone.getImsRegistrationTech();
5947 } else {
5948 return ImsRegistrationImplBase.REGISTRATION_TECH_NONE;
5949 }
5950 } finally {
5951 Binder.restoreCallingIdentity(identity);
Brad Ebinger1f2b5082018-02-08 16:11:32 -08005952 }
Etan Cohen3b7a1bc2015-05-28 15:57:13 -07005953 }
5954
Stuart Scott8eef64f2015-04-08 15:13:54 -07005955 @Override
5956 public void factoryReset(int subId) {
paulhu423b5f22019-08-23 19:17:33 +08005957 enforceSettingsPermission();
Stuart Scott981d8582015-04-21 14:09:50 -07005958 if (mUserManager.hasUserRestriction(UserManager.DISALLOW_NETWORK_RESET)) {
5959 return;
5960 }
5961
Svet Ganovcc087f82015-05-12 20:35:54 -07005962 final long identity = Binder.clearCallingIdentity();
Nazanin Bakhshif782e562018-12-11 15:15:39 -08005963
Svet Ganovcc087f82015-05-12 20:35:54 -07005964 try {
Stuart Scott981d8582015-04-21 14:09:50 -07005965 if (SubscriptionManager.isUsableSubIdValue(subId) && !mUserManager.hasUserRestriction(
5966 UserManager.DISALLOW_CONFIG_MOBILE_NETWORKS)) {
Pengquan Meng85728fb2018-03-12 16:31:21 -07005967 setUserDataEnabled(subId, getDefaultDataEnabled());
Svet Ganovcc087f82015-05-12 20:35:54 -07005968 setNetworkSelectionModeAutomatic(subId);
Pengquan Meng85728fb2018-03-12 16:31:21 -07005969 setPreferredNetworkType(subId, getDefaultNetworkType(subId));
Nazanin Bakhshif782e562018-12-11 15:15:39 -08005970 setDataRoamingEnabled(subId, getDefaultDataRoamingEnabled(subId));
5971 CarrierInfoManager.deleteAllCarrierKeysForImsiEncryption(mApp);
Svet Ganovcc087f82015-05-12 20:35:54 -07005972 }
Amit Mahajan7dbbd822019-03-13 17:33:47 -07005973 // There has been issues when Sms raw table somehow stores orphan
5974 // fragments. They lead to garbled message when new fragments come
5975 // in and combined with those stale ones. In case this happens again,
5976 // user can reset all network settings which will clean up this table.
5977 cleanUpSmsRawTable(getDefaultPhone().getContext());
Brad Ebinger77b832e2019-10-17 17:03:22 -07005978 // Clean up IMS settings as well here.
5979 int slotId = getSlotIndex(subId);
5980 if (slotId > SubscriptionManager.INVALID_SIM_SLOT_INDEX) {
5981 ImsManager.getInstance(mApp, slotId).factoryReset();
5982 }
Naina Nalluri8ff344d2019-09-17 14:10:30 -07005983
5984 // Erase modem config if erase modem on network setting is enabled.
5985 String configValue = DeviceConfig.getProperty(DeviceConfig.NAMESPACE_TELEPHONY,
5986 RESET_NETWORK_ERASE_MODEM_CONFIG_ENABLED);
5987 if (configValue != null && Boolean.parseBoolean(configValue)) {
5988 sendEraseModemConfig(getDefaultPhone());
5989 }
Svet Ganovcc087f82015-05-12 20:35:54 -07005990 } finally {
5991 Binder.restoreCallingIdentity(identity);
Stuart Scott8eef64f2015-04-08 15:13:54 -07005992 }
5993 }
Narayan Kamath1c496c22015-04-16 14:40:19 +01005994
Amit Mahajan7dbbd822019-03-13 17:33:47 -07005995 private void cleanUpSmsRawTable(Context context) {
5996 ContentResolver resolver = context.getContentResolver();
5997 Uri uri = Uri.withAppendedPath(Telephony.Sms.CONTENT_URI, "raw/permanentDelete");
5998 resolver.delete(uri, null, null);
5999 }
6000
Narayan Kamath1c496c22015-04-16 14:40:19 +01006001 @Override
chen xu5d3637b2019-01-21 23:31:38 -08006002 public String getSimLocaleForSubscriber(int subId) {
6003 enforceReadPrivilegedPermission("getSimLocaleForSubscriber, subId: " + subId);
6004 final Phone phone = getPhone(subId);
6005 if (phone == null) {
6006 log("getSimLocaleForSubscriber, invalid subId");
chen xu2bb91e42019-01-24 14:35:54 -08006007 return null;
chen xu5d3637b2019-01-21 23:31:38 -08006008 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006009 final long identity = Binder.clearCallingIdentity();
6010 try {
chen xu5d3637b2019-01-21 23:31:38 -08006011 final SubscriptionInfo info = mSubscriptionController.getActiveSubscriptionInfo(subId,
6012 phone.getContext().getOpPackageName());
chen xu6291c472019-02-04 12:55:53 -08006013 if (info == null) {
6014 log("getSimLocaleForSubscriber, inactive subId: " + subId);
6015 return null;
6016 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006017 // Try and fetch the locale from the carrier properties or from the SIM language
6018 // preferences (EF-PL and EF-LI)...
6019 final int mcc = info.getMcc();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006020 String simLanguage = null;
chen xu5d3637b2019-01-21 23:31:38 -08006021 final Locale localeFromDefaultSim = phone.getLocaleFromSimAndCarrierPrefs();
6022 if (localeFromDefaultSim != null) {
6023 if (!localeFromDefaultSim.getCountry().isEmpty()) {
6024 if (DBG) log("Using locale from subId: " + subId + " locale: "
6025 + localeFromDefaultSim);
6026 return localeFromDefaultSim.toLanguageTag();
6027 } else {
6028 simLanguage = localeFromDefaultSim.getLanguage();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006029 }
6030 }
Narayan Kamath1c496c22015-04-16 14:40:19 +01006031
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006032 // The SIM language preferences only store a language (e.g. fr = French), not an
6033 // exact locale (e.g. fr_FR = French/France). So, if the locale returned from
6034 // the SIM and carrier preferences does not include a country we add the country
6035 // determined from the SIM MCC to provide an exact locale.
zoey chen84e2b212019-12-18 17:07:20 +08006036 final Locale mccLocale = LocaleUtils.getLocaleFromMcc(mApp, mcc, simLanguage);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006037 if (mccLocale != null) {
chen xu5d3637b2019-01-21 23:31:38 -08006038 if (DBG) log("No locale from SIM, using mcc locale:" + mccLocale);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006039 return mccLocale.toLanguageTag();
6040 }
6041
6042 if (DBG) log("No locale found - returning null");
6043 return null;
6044 } finally {
6045 Binder.restoreCallingIdentity(identity);
6046 }
Narayan Kamath1c496c22015-04-16 14:40:19 +01006047 }
6048
6049 private List<SubscriptionInfo> getAllSubscriptionInfoList() {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08006050 return mSubscriptionController.getAllSubInfoList(mApp.getOpPackageName());
Narayan Kamath1c496c22015-04-16 14:40:19 +01006051 }
6052
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006053 /**
6054 * NOTE: this method assumes permission checks are done and caller identity has been cleared.
6055 */
6056 private List<SubscriptionInfo> getActiveSubscriptionInfoListPrivileged() {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08006057 return mSubscriptionController.getActiveSubscriptionInfoList(mApp.getOpPackageName());
Narayan Kamath1c496c22015-04-16 14:40:19 +01006058 }
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -07006059
Chenjie Yu1ba97252018-01-11 18:16:20 -08006060 private final ModemActivityInfo mLastModemActivityInfo =
Chen Xu13851032019-09-10 18:49:52 -07006061 new ModemActivityInfo(0, 0, 0, new int[0], 0);
Chenjie Yu1ba97252018-01-11 18:16:20 -08006062
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -07006063 /**
Adam Lesinski903a54c2016-04-11 14:49:52 -07006064 * Responds to the ResultReceiver with the {@link android.telephony.ModemActivityInfo} object
6065 * representing the state of the modem.
6066 *
Chenjie Yu1ba97252018-01-11 18:16:20 -08006067 * NOTE: The underlying implementation clears the modem state, so there should only ever be one
6068 * caller to it. Everyone should call this class to get cumulative data.
Adam Lesinski903a54c2016-04-11 14:49:52 -07006069 * @hide
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -07006070 */
6071 @Override
Adam Lesinski903a54c2016-04-11 14:49:52 -07006072 public void requestModemActivityInfo(ResultReceiver result) {
6073 enforceModifyPermission();
vagdeviaf9a5b92018-08-15 16:01:53 -07006074 WorkSource workSource = getWorkSource(Binder.getCallingUid());
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006075
6076 final long identity = Binder.clearCallingIdentity();
6077 try {
6078 ModemActivityInfo ret = null;
6079 synchronized (mLastModemActivityInfo) {
6080 ModemActivityInfo info = (ModemActivityInfo) sendRequest(
6081 CMD_GET_MODEM_ACTIVITY_INFO,
vagdeviaf9a5b92018-08-15 16:01:53 -07006082 null, workSource);
Siddharth Rayb8114062018-06-17 15:02:38 -07006083 if (isModemActivityInfoValid(info)) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006084 int[] mergedTxTimeMs = new int[ModemActivityInfo.TX_POWER_LEVELS];
Chen Xu13851032019-09-10 18:49:52 -07006085 int[] txTimeMs = info.getTransmitTimeMillis();
6086 int[] lastModemTxTimeMs = mLastModemActivityInfo.getTransmitTimeMillis();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006087 for (int i = 0; i < mergedTxTimeMs.length; i++) {
Chen Xu13851032019-09-10 18:49:52 -07006088 mergedTxTimeMs[i] = txTimeMs[i] + lastModemTxTimeMs[i];
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006089 }
6090 mLastModemActivityInfo.setTimestamp(info.getTimestamp());
Nazanin Bakhshif782e562018-12-11 15:15:39 -08006091 mLastModemActivityInfo.setSleepTimeMillis(info.getSleepTimeMillis()
6092 + mLastModemActivityInfo.getSleepTimeMillis());
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006093 mLastModemActivityInfo.setIdleTimeMillis(
6094 info.getIdleTimeMillis() + mLastModemActivityInfo.getIdleTimeMillis());
Chen Xu13851032019-09-10 18:49:52 -07006095 mLastModemActivityInfo.setTransmitTimeMillis(mergedTxTimeMs);
6096 mLastModemActivityInfo.setReceiveTimeMillis(
6097 info.getReceiveTimeMillis() + mLastModemActivityInfo
6098 .getReceiveTimeMillis());
Chenjie Yu1ba97252018-01-11 18:16:20 -08006099 }
Chen Xu13851032019-09-10 18:49:52 -07006100
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006101 ret = new ModemActivityInfo(mLastModemActivityInfo.getTimestamp(),
6102 mLastModemActivityInfo.getSleepTimeMillis(),
6103 mLastModemActivityInfo.getIdleTimeMillis(),
Chen Xu13851032019-09-10 18:49:52 -07006104 mLastModemActivityInfo.getTransmitTimeMillis(),
6105 mLastModemActivityInfo.getReceiveTimeMillis());
Chenjie Yu1ba97252018-01-11 18:16:20 -08006106 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006107 Bundle bundle = new Bundle();
6108 bundle.putParcelable(TelephonyManager.MODEM_ACTIVITY_RESULT_KEY, ret);
6109 result.send(0, bundle);
6110 } finally {
6111 Binder.restoreCallingIdentity(identity);
Chenjie Yu1ba97252018-01-11 18:16:20 -08006112 }
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -07006113 }
Jack Yu85bd38a2015-11-09 11:34:32 -08006114
Siddharth Rayb8114062018-06-17 15:02:38 -07006115 // Checks that ModemActivityInfo is valid. Sleep time, Idle time, Rx time and Tx time should be
6116 // less than total activity duration.
6117 private boolean isModemActivityInfoValid(ModemActivityInfo info) {
6118 if (info == null) {
6119 return false;
6120 }
6121 int activityDurationMs =
6122 (int) (info.getTimestamp() - mLastModemActivityInfo.getTimestamp());
6123 int totalTxTimeMs = 0;
Chen Xu13851032019-09-10 18:49:52 -07006124 int[] txTimeMs = info.getTransmitTimeMillis();
6125 for (int i = 0; i < info.getTransmitPowerInfo().size(); i++) {
6126 totalTxTimeMs += txTimeMs[i];
Siddharth Rayb8114062018-06-17 15:02:38 -07006127 }
6128 return (info.isValid()
6129 && (info.getSleepTimeMillis() <= activityDurationMs)
6130 && (info.getIdleTimeMillis() <= activityDurationMs)
Chen Xu13851032019-09-10 18:49:52 -07006131 && (info.getReceiveTimeMillis() <= activityDurationMs)
Siddharth Rayb8114062018-06-17 15:02:38 -07006132 && (totalTxTimeMs <= activityDurationMs));
6133 }
6134
Jack Yu85bd38a2015-11-09 11:34:32 -08006135 /**
6136 * {@hide}
6137 * Returns the service state information on specified subscription.
6138 */
6139 @Override
6140 public ServiceState getServiceStateForSubscriber(int subId, String callingPackage) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08006141 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
Jeff Davidson913390f2018-02-23 17:11:49 -08006142 mApp, subId, callingPackage, "getServiceStateForSubscriber")) {
Jack Yu85bd38a2015-11-09 11:34:32 -08006143 return null;
6144 }
6145
Hall Liuf19c44f2018-11-27 14:38:17 -08006146 LocationAccessPolicy.LocationPermissionResult fineLocationResult =
6147 LocationAccessPolicy.checkLocationPermission(mApp,
6148 new LocationAccessPolicy.LocationPermissionQuery.Builder()
6149 .setCallingPackage(callingPackage)
6150 .setCallingPid(Binder.getCallingPid())
6151 .setCallingUid(Binder.getCallingUid())
6152 .setMethod("getServiceStateForSubscriber")
Hall Liuf18a0cf2019-04-17 13:37:11 -07006153 .setLogAsInfo(true)
Hall Liuf19c44f2018-11-27 14:38:17 -08006154 .setMinSdkVersionForFine(Build.VERSION_CODES.Q)
6155 .build());
6156
6157 LocationAccessPolicy.LocationPermissionResult coarseLocationResult =
6158 LocationAccessPolicy.checkLocationPermission(mApp,
6159 new LocationAccessPolicy.LocationPermissionQuery.Builder()
6160 .setCallingPackage(callingPackage)
6161 .setCallingPid(Binder.getCallingPid())
6162 .setCallingUid(Binder.getCallingUid())
6163 .setMethod("getServiceStateForSubscriber")
Hall Liuf18a0cf2019-04-17 13:37:11 -07006164 .setLogAsInfo(true)
Hall Liuf19c44f2018-11-27 14:38:17 -08006165 .setMinSdkVersionForCoarse(Build.VERSION_CODES.Q)
6166 .build());
6167 // We don't care about hard or soft here -- all we need to know is how much info to scrub.
6168 boolean hasFinePermission =
6169 fineLocationResult == LocationAccessPolicy.LocationPermissionResult.ALLOWED;
6170 boolean hasCoarsePermission =
6171 coarseLocationResult == LocationAccessPolicy.LocationPermissionResult.ALLOWED;
6172
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006173 final long identity = Binder.clearCallingIdentity();
6174 try {
6175 final Phone phone = getPhone(subId);
6176 if (phone == null) {
6177 return null;
6178 }
Jack Yu85bd38a2015-11-09 11:34:32 -08006179
Hall Liuf19c44f2018-11-27 14:38:17 -08006180 ServiceState ss = phone.getServiceState();
6181
6182 // Scrub out the location info in ServiceState depending on what level of access
6183 // the caller has.
6184 if (hasFinePermission) return ss;
6185 if (hasCoarsePermission) return ss.sanitizeLocationInfo(false);
6186 return ss.sanitizeLocationInfo(true);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006187 } finally {
6188 Binder.restoreCallingIdentity(identity);
6189 }
Jack Yu85bd38a2015-11-09 11:34:32 -08006190 }
Nancy Chen31f9ba12016-01-06 11:42:12 -08006191
6192 /**
6193 * Returns the URI for the per-account voicemail ringtone set in Phone settings.
6194 *
6195 * @param accountHandle The handle for the {@link PhoneAccount} for which to retrieve the
6196 * voicemail ringtone.
6197 * @return The URI for the ringtone to play when receiving a voicemail from a specific
6198 * PhoneAccount.
6199 */
6200 @Override
6201 public Uri getVoicemailRingtoneUri(PhoneAccountHandle accountHandle) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006202 final long identity = Binder.clearCallingIdentity();
6203 try {
6204 Phone phone = PhoneUtils.getPhoneForPhoneAccountHandle(accountHandle);
6205 if (phone == null) {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08006206 phone = getDefaultPhone();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006207 }
Nancy Chen31f9ba12016-01-06 11:42:12 -08006208
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006209 return VoicemailNotificationSettingsUtil.getRingtoneUri(phone.getContext());
6210 } finally {
6211 Binder.restoreCallingIdentity(identity);
6212 }
Nancy Chen31f9ba12016-01-06 11:42:12 -08006213 }
6214
6215 /**
Ta-wei Yenc33877d2017-01-23 18:11:21 -08006216 * Sets the per-account voicemail ringtone.
6217 *
6218 * <p>Requires that the calling app is the default dialer, or has carrier privileges, or
6219 * has permission {@link android.Manifest.permission#MODIFY_PHONE_STATE MODIFY_PHONE_STATE}.
6220 *
6221 * @param phoneAccountHandle The handle for the {@link PhoneAccount} for which to set the
6222 * voicemail ringtone.
6223 * @param uri The URI for the ringtone to play when receiving a voicemail from a specific
6224 * PhoneAccount.
6225 */
6226 @Override
6227 public void setVoicemailRingtoneUri(String callingPackage,
6228 PhoneAccountHandle phoneAccountHandle, Uri uri) {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08006229 final Phone defaultPhone = getDefaultPhone();
Ta-wei Yenc33877d2017-01-23 18:11:21 -08006230 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
Tyler Gunn77ee9382019-10-31 13:08:23 -07006231 TelecomManager tm = defaultPhone.getContext().getSystemService(TelecomManager.class);
6232 if (!TextUtils.equals(callingPackage, tm.getDefaultDialerPackage())) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08006233 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
6234 mApp, PhoneUtils.getSubIdForPhoneAccountHandle(phoneAccountHandle),
6235 "setVoicemailRingtoneUri");
Ta-wei Yenc33877d2017-01-23 18:11:21 -08006236 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006237
6238 final long identity = Binder.clearCallingIdentity();
6239 try {
6240 Phone phone = PhoneUtils.getPhoneForPhoneAccountHandle(phoneAccountHandle);
6241 if (phone == null) {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08006242 phone = defaultPhone;
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006243 }
6244 VoicemailNotificationSettingsUtil.setRingtoneUri(phone.getContext(), uri);
6245 } finally {
6246 Binder.restoreCallingIdentity(identity);
Ta-wei Yenc33877d2017-01-23 18:11:21 -08006247 }
Ta-wei Yenc33877d2017-01-23 18:11:21 -08006248 }
6249
6250 /**
Nancy Chen31f9ba12016-01-06 11:42:12 -08006251 * Returns whether vibration is set for voicemail notification in Phone settings.
6252 *
6253 * @param accountHandle The handle for the {@link PhoneAccount} for which to retrieve the
6254 * voicemail vibration setting.
6255 * @return {@code true} if the vibration is set for this PhoneAccount, {@code false} otherwise.
6256 */
6257 @Override
6258 public boolean isVoicemailVibrationEnabled(PhoneAccountHandle accountHandle) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006259 final long identity = Binder.clearCallingIdentity();
6260 try {
6261 Phone phone = PhoneUtils.getPhoneForPhoneAccountHandle(accountHandle);
6262 if (phone == null) {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08006263 phone = getDefaultPhone();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006264 }
Nancy Chen31f9ba12016-01-06 11:42:12 -08006265
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006266 return VoicemailNotificationSettingsUtil.isVibrationEnabled(phone.getContext());
6267 } finally {
6268 Binder.restoreCallingIdentity(identity);
6269 }
Nancy Chen31f9ba12016-01-06 11:42:12 -08006270 }
6271
Youhan Wange64578a2016-05-02 15:32:42 -07006272 /**
Ta-wei Yenc33877d2017-01-23 18:11:21 -08006273 * Sets the per-account voicemail vibration.
6274 *
6275 * <p>Requires that the calling app is the default dialer, or has carrier privileges, or
6276 * has permission {@link android.Manifest.permission#MODIFY_PHONE_STATE MODIFY_PHONE_STATE}.
6277 *
6278 * @param phoneAccountHandle The handle for the {@link PhoneAccount} for which to set the
6279 * voicemail vibration setting.
6280 * @param enabled Whether to enable or disable vibration for voicemail notifications from a
6281 * specific PhoneAccount.
6282 */
6283 @Override
6284 public void setVoicemailVibrationEnabled(String callingPackage,
6285 PhoneAccountHandle phoneAccountHandle, boolean enabled) {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08006286 final Phone defaultPhone = getDefaultPhone();
Ta-wei Yenc33877d2017-01-23 18:11:21 -08006287 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
Tyler Gunn77ee9382019-10-31 13:08:23 -07006288 TelecomManager tm = defaultPhone.getContext().getSystemService(TelecomManager.class);
6289 if (!TextUtils.equals(callingPackage, tm.getDefaultDialerPackage())) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08006290 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
6291 mApp, PhoneUtils.getSubIdForPhoneAccountHandle(phoneAccountHandle),
6292 "setVoicemailVibrationEnabled");
Ta-wei Yenc33877d2017-01-23 18:11:21 -08006293 }
6294
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006295 final long identity = Binder.clearCallingIdentity();
6296 try {
6297 Phone phone = PhoneUtils.getPhoneForPhoneAccountHandle(phoneAccountHandle);
6298 if (phone == null) {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08006299 phone = defaultPhone;
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006300 }
6301 VoicemailNotificationSettingsUtil.setVibrationEnabled(phone.getContext(), enabled);
6302 } finally {
6303 Binder.restoreCallingIdentity(identity);
Ta-wei Yenc33877d2017-01-23 18:11:21 -08006304 }
Ta-wei Yenc33877d2017-01-23 18:11:21 -08006305 }
6306
6307 /**
Youhan Wange64578a2016-05-02 15:32:42 -07006308 * Make sure either called from same process as self (phone) or IPC caller has read privilege.
6309 *
6310 * @throws SecurityException if the caller does not have the required permission
6311 */
Brad Ebinger35c841c2018-10-01 10:40:55 -07006312 private void enforceReadPrivilegedPermission(String message) {
Youhan Wange64578a2016-05-02 15:32:42 -07006313 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.READ_PRIVILEGED_PHONE_STATE,
Brad Ebinger35c841c2018-10-01 10:40:55 -07006314 message);
Youhan Wange64578a2016-05-02 15:32:42 -07006315 }
6316
6317 /**
Ta-wei Yen30a69c82016-12-27 14:52:32 -08006318 * Make sure either called from same process as self (phone) or IPC caller has send SMS
6319 * permission.
6320 *
6321 * @throws SecurityException if the caller does not have the required permission
6322 */
6323 private void enforceSendSmsPermission() {
6324 mApp.enforceCallingOrSelfPermission(permission.SEND_SMS, null);
6325 }
6326
6327 /**
Ta-wei Yen527a9c02017-01-06 15:29:25 -08006328 * Make sure called from the package in charge of visual voicemail.
Ta-wei Yen30a69c82016-12-27 14:52:32 -08006329 *
Ta-wei Yen527a9c02017-01-06 15:29:25 -08006330 * @throws SecurityException if the caller is not the visual voicemail package.
Ta-wei Yen30a69c82016-12-27 14:52:32 -08006331 */
Ta-wei Yen527a9c02017-01-06 15:29:25 -08006332 private void enforceVisualVoicemailPackage(String callingPackage, int subId) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006333 final long identity = Binder.clearCallingIdentity();
6334 try {
6335 ComponentName componentName =
Nazanin Bakhshif782e562018-12-11 15:15:39 -08006336 RemoteVvmTaskManager.getRemotePackage(mApp, subId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006337 if (componentName == null) {
6338 throw new SecurityException(
6339 "Caller not current active visual voicemail package[null]");
6340 }
6341 String vvmPackage = componentName.getPackageName();
6342 if (!callingPackage.equals(vvmPackage)) {
6343 throw new SecurityException("Caller not current active visual voicemail package["
6344 + vvmPackage + "]");
6345 }
6346 } finally {
6347 Binder.restoreCallingIdentity(identity);
Ta-wei Yen30a69c82016-12-27 14:52:32 -08006348 }
6349 }
6350
6351 /**
Youhan Wange64578a2016-05-02 15:32:42 -07006352 * Return the application ID for the app type.
6353 *
6354 * @param subId the subscription ID that this request applies to.
6355 * @param appType the uicc app type.
6356 * @return Application ID for specificied app type, or null if no uicc.
6357 */
6358 @Override
6359 public String getAidForAppType(int subId, int appType) {
Brad Ebinger35c841c2018-10-01 10:40:55 -07006360 enforceReadPrivilegedPermission("getAidForAppType");
Youhan Wange64578a2016-05-02 15:32:42 -07006361 Phone phone = getPhone(subId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006362
6363 final long identity = Binder.clearCallingIdentity();
Youhan Wange64578a2016-05-02 15:32:42 -07006364 try {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006365 if (phone == null) {
6366 return null;
6367 }
6368 String aid = null;
6369 try {
6370 aid = UiccController.getInstance().getUiccCard(phone.getPhoneId())
6371 .getApplicationByType(appType).getAid();
6372 } catch (Exception e) {
6373 Log.e(LOG_TAG, "Not getting aid. Exception ex=" + e);
6374 }
6375 return aid;
6376 } finally {
6377 Binder.restoreCallingIdentity(identity);
Youhan Wange64578a2016-05-02 15:32:42 -07006378 }
Youhan Wange64578a2016-05-02 15:32:42 -07006379 }
6380
Youhan Wang4001d252016-05-11 10:29:41 -07006381 /**
6382 * Return the Electronic Serial Number.
6383 *
6384 * @param subId the subscription ID that this request applies to.
6385 * @return ESN or null if error.
6386 */
6387 @Override
6388 public String getEsn(int subId) {
Brad Ebinger35c841c2018-10-01 10:40:55 -07006389 enforceReadPrivilegedPermission("getEsn");
Youhan Wang4001d252016-05-11 10:29:41 -07006390 Phone phone = getPhone(subId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006391
6392 final long identity = Binder.clearCallingIdentity();
Youhan Wang4001d252016-05-11 10:29:41 -07006393 try {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006394 if (phone == null) {
6395 return null;
6396 }
6397 String esn = null;
6398 try {
6399 esn = phone.getEsn();
6400 } catch (Exception e) {
6401 Log.e(LOG_TAG, "Not getting ESN. Exception ex=" + e);
6402 }
6403 return esn;
6404 } finally {
6405 Binder.restoreCallingIdentity(identity);
Youhan Wang4001d252016-05-11 10:29:41 -07006406 }
Youhan Wang4001d252016-05-11 10:29:41 -07006407 }
6408
Sanket Padawe99ef1e32016-05-18 16:12:33 -07006409 /**
Youhan Wang66ad5d72016-07-18 17:56:58 -07006410 * Return the Preferred Roaming List Version.
6411 *
6412 * @param subId the subscription ID that this request applies to.
6413 * @return PRLVersion or null if error.
6414 */
6415 @Override
6416 public String getCdmaPrlVersion(int subId) {
Brad Ebinger35c841c2018-10-01 10:40:55 -07006417 enforceReadPrivilegedPermission("getCdmaPrlVersion");
Youhan Wang66ad5d72016-07-18 17:56:58 -07006418 Phone phone = getPhone(subId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006419
6420 final long identity = Binder.clearCallingIdentity();
Youhan Wang66ad5d72016-07-18 17:56:58 -07006421 try {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006422 if (phone == null) {
6423 return null;
6424 }
6425 String cdmaPrlVersion = null;
6426 try {
6427 cdmaPrlVersion = phone.getCdmaPrlVersion();
6428 } catch (Exception e) {
6429 Log.e(LOG_TAG, "Not getting PRLVersion", e);
6430 }
6431 return cdmaPrlVersion;
6432 } finally {
6433 Binder.restoreCallingIdentity(identity);
Youhan Wang66ad5d72016-07-18 17:56:58 -07006434 }
Youhan Wang66ad5d72016-07-18 17:56:58 -07006435 }
6436
6437 /**
Sanket Padawe99ef1e32016-05-18 16:12:33 -07006438 * Get snapshot of Telephony histograms
6439 * @return List of Telephony histograms
6440 * @hide
6441 */
6442 @Override
6443 public List<TelephonyHistogram> getTelephonyHistograms() {
Jeff Davidson7e17e312018-02-13 18:17:36 -08006444 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
6445 mApp, getDefaultSubscription(), "getTelephonyHistograms");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006446
6447 final long identity = Binder.clearCallingIdentity();
6448 try {
6449 return RIL.getTelephonyRILTimingHistograms();
6450 } finally {
6451 Binder.restoreCallingIdentity(identity);
6452 }
Sanket Padawe99ef1e32016-05-18 16:12:33 -07006453 }
Meng Wang1a7c35a2016-05-05 20:56:15 -07006454
6455 /**
6456 * {@hide}
Michele Berionne482f8202018-11-27 18:57:59 -08006457 * Set the allowed carrier list and the excluded carrier list, indicating the priority between
6458 * the two lists.
Meng Wang1a7c35a2016-05-05 20:56:15 -07006459 * Require system privileges. In the future we may add this to carrier APIs.
6460 *
Michele Berionne482f8202018-11-27 18:57:59 -08006461 * @return Integer with the result of the operation, as defined in {@link TelephonyManager}.
Meng Wang1a7c35a2016-05-05 20:56:15 -07006462 */
6463 @Override
Michele Berionne482f8202018-11-27 18:57:59 -08006464 @TelephonyManager.SetCarrierRestrictionResult
6465 public int setAllowedCarriers(CarrierRestrictionRules carrierRestrictionRules) {
Meng Wang1a7c35a2016-05-05 20:56:15 -07006466 enforceModifyPermission();
vagdeviaf9a5b92018-08-15 16:01:53 -07006467 WorkSource workSource = getWorkSource(Binder.getCallingUid());
Sanket Padawe13bac7b2017-03-20 15:04:47 -07006468
Michele Berionne482f8202018-11-27 18:57:59 -08006469 if (carrierRestrictionRules == null) {
6470 throw new NullPointerException("carrier restriction cannot be null");
Meng Wang9b7c4e92017-02-17 11:41:27 -08006471 }
Sanket Padawe13bac7b2017-03-20 15:04:47 -07006472
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006473 final long identity = Binder.clearCallingIdentity();
6474 try {
Michele Berionne482f8202018-11-27 18:57:59 -08006475 return (int) sendRequest(CMD_SET_ALLOWED_CARRIERS, carrierRestrictionRules,
vagdeviaf9a5b92018-08-15 16:01:53 -07006476 workSource);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006477 } finally {
6478 Binder.restoreCallingIdentity(identity);
6479 }
Meng Wang1a7c35a2016-05-05 20:56:15 -07006480 }
6481
6482 /**
6483 * {@hide}
Michele Berionne482f8202018-11-27 18:57:59 -08006484 * Get the allowed carrier list and the excluded carrier list, including the priority between
6485 * the two lists.
Meng Wang1a7c35a2016-05-05 20:56:15 -07006486 * Require system privileges. In the future we may add this to carrier APIs.
6487 *
Michele Berionne482f8202018-11-27 18:57:59 -08006488 * @return {@link android.telephony.CarrierRestrictionRules}
Meng Wang1a7c35a2016-05-05 20:56:15 -07006489 */
6490 @Override
Michele Berionne482f8202018-11-27 18:57:59 -08006491 public CarrierRestrictionRules getAllowedCarriers() {
Brad Ebinger35c841c2018-10-01 10:40:55 -07006492 enforceReadPrivilegedPermission("getAllowedCarriers");
vagdeviaf9a5b92018-08-15 16:01:53 -07006493 WorkSource workSource = getWorkSource(Binder.getCallingUid());
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006494
6495 final long identity = Binder.clearCallingIdentity();
6496 try {
Michele Berionne482f8202018-11-27 18:57:59 -08006497 Object response = sendRequest(CMD_GET_ALLOWED_CARRIERS, null, workSource);
6498 if (response instanceof CarrierRestrictionRules) {
6499 return (CarrierRestrictionRules) response;
6500 }
6501 // Response is an Exception of some kind,
6502 // which is signalled to the user as a NULL retval
6503 return null;
6504 } catch (Exception e) {
6505 Log.e(LOG_TAG, "getAllowedCarriers. Exception ex=" + e);
6506 return null;
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006507 } finally {
6508 Binder.restoreCallingIdentity(identity);
6509 }
Meng Wang1a7c35a2016-05-05 20:56:15 -07006510 }
6511
fionaxu59545b42016-05-25 15:53:37 -07006512 /**
6513 * Action set from carrier signalling broadcast receivers to enable/disable metered apns
6514 * @param subId the subscription ID that this action applies to.
6515 * @param enabled control enable or disable metered apns.
6516 * {@hide}
6517 */
6518 @Override
6519 public void carrierActionSetMeteredApnsEnabled(int subId, boolean enabled) {
6520 enforceModifyPermission();
6521 final Phone phone = getPhone(subId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006522
6523 final long identity = Binder.clearCallingIdentity();
fionaxu59545b42016-05-25 15:53:37 -07006524 if (phone == null) {
6525 loge("carrierAction: SetMeteredApnsEnabled fails with invalid subId: " + subId);
6526 return;
6527 }
6528 try {
6529 phone.carrierActionSetMeteredApnsEnabled(enabled);
6530 } catch (Exception e) {
6531 Log.e(LOG_TAG, "carrierAction: SetMeteredApnsEnabled fails. Exception ex=" + e);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006532 } finally {
6533 Binder.restoreCallingIdentity(identity);
fionaxu59545b42016-05-25 15:53:37 -07006534 }
6535 }
6536
6537 /**
6538 * Action set from carrier signalling broadcast receivers to enable/disable radio
6539 * @param subId the subscription ID that this action applies to.
6540 * @param enabled control enable or disable radio.
6541 * {@hide}
6542 */
6543 @Override
6544 public void carrierActionSetRadioEnabled(int subId, boolean enabled) {
6545 enforceModifyPermission();
6546 final Phone phone = getPhone(subId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006547
6548 final long identity = Binder.clearCallingIdentity();
fionaxu59545b42016-05-25 15:53:37 -07006549 if (phone == null) {
6550 loge("carrierAction: SetRadioEnabled fails with invalid sibId: " + subId);
6551 return;
6552 }
6553 try {
6554 phone.carrierActionSetRadioEnabled(enabled);
6555 } catch (Exception e) {
6556 Log.e(LOG_TAG, "carrierAction: SetRadioEnabled fails. Exception ex=" + e);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006557 } finally {
6558 Binder.restoreCallingIdentity(identity);
fionaxu59545b42016-05-25 15:53:37 -07006559 }
6560 }
6561
Ta-wei Yenc236d6b2016-06-21 13:33:12 -07006562 /**
fionaxu8da9cb12017-05-23 15:02:46 -07006563 * Action set from carrier signalling broadcast receivers to start/stop reporting the default
6564 * network status based on which carrier apps could apply actions accordingly,
6565 * enable/disable default url handler for example.
6566 *
6567 * @param subId the subscription ID that this action applies to.
6568 * @param report control start/stop reporting the default network status.
6569 * {@hide}
6570 */
6571 @Override
6572 public void carrierActionReportDefaultNetworkStatus(int subId, boolean report) {
6573 enforceModifyPermission();
6574 final Phone phone = getPhone(subId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006575
6576 final long identity = Binder.clearCallingIdentity();
fionaxu8da9cb12017-05-23 15:02:46 -07006577 if (phone == null) {
6578 loge("carrierAction: ReportDefaultNetworkStatus fails with invalid sibId: " + subId);
6579 return;
6580 }
6581 try {
6582 phone.carrierActionReportDefaultNetworkStatus(report);
6583 } catch (Exception e) {
6584 Log.e(LOG_TAG, "carrierAction: ReportDefaultNetworkStatus fails. Exception ex=" + e);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006585 } finally {
6586 Binder.restoreCallingIdentity(identity);
fionaxu8da9cb12017-05-23 15:02:46 -07006587 }
6588 }
6589
6590 /**
fionaxud9622282017-07-17 17:51:30 -07006591 * Action set from carrier signalling broadcast receivers to reset all carrier actions
6592 * @param subId the subscription ID that this action applies to.
6593 * {@hide}
6594 */
6595 @Override
6596 public void carrierActionResetAll(int subId) {
6597 enforceModifyPermission();
6598 final Phone phone = getPhone(subId);
6599 if (phone == null) {
6600 loge("carrierAction: ResetAll fails with invalid sibId: " + subId);
6601 return;
6602 }
6603 try {
6604 phone.carrierActionResetAll();
6605 } catch (Exception e) {
6606 Log.e(LOG_TAG, "carrierAction: ResetAll fails. Exception ex=" + e);
6607 }
6608 }
6609
6610 /**
Ta-wei Yenc236d6b2016-06-21 13:33:12 -07006611 * Called when "adb shell dumpsys phone" is invoked. Dump is also automatically invoked when a
6612 * bug report is being generated.
6613 */
6614 @Override
Ta-wei Yen99282e02016-06-21 18:19:35 -07006615 protected void dump(FileDescriptor fd, PrintWriter writer, String[] args) {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08006616 if (mApp.checkCallingOrSelfPermission(android.Manifest.permission.DUMP)
6617 != PackageManager.PERMISSION_GRANTED) {
dcashman22b950d2016-06-27 11:39:02 -07006618 writer.println("Permission Denial: can't dump Phone from pid="
6619 + Binder.getCallingPid()
6620 + ", uid=" + Binder.getCallingUid()
6621 + "without permission "
6622 + android.Manifest.permission.DUMP);
6623 return;
6624 }
Nazanin Bakhshif782e562018-12-11 15:15:39 -08006625 DumpsysHandler.dump(mApp, fd, writer, args);
Ta-wei Yenc236d6b2016-06-21 13:33:12 -07006626 }
Jack Yueb89b242016-06-22 13:27:47 -07006627
Brad Ebingerdac2f002018-04-03 15:17:52 -07006628 @Override
6629 public void onShellCommand(FileDescriptor in, FileDescriptor out, FileDescriptor err,
6630 String[] args, ShellCallback callback, ResultReceiver resultReceiver)
6631 throws RemoteException {
Torbjorn Eklund1050cb02018-11-16 14:05:38 +01006632 (new TelephonyShellCommand(this, getDefaultPhone().getContext()))
6633 .exec(this, in, out, err, args, callback, resultReceiver);
Brad Ebingerdac2f002018-04-03 15:17:52 -07006634 }
6635
Jack Yueb89b242016-06-22 13:27:47 -07006636 /**
Jack Yu84291ec2017-05-26 16:07:50 -07006637 * Get aggregated video call data usage since boot.
6638 *
6639 * @param perUidStats True if requesting data usage per uid, otherwise overall usage.
6640 * @return Snapshot of video call data usage
Jack Yueb89b242016-06-22 13:27:47 -07006641 * {@hide}
6642 */
6643 @Override
Jack Yu84291ec2017-05-26 16:07:50 -07006644 public NetworkStats getVtDataUsage(int subId, boolean perUidStats) {
Jack Yueb89b242016-06-22 13:27:47 -07006645 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.READ_NETWORK_USAGE_HISTORY,
6646 null);
6647
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006648 final long identity = Binder.clearCallingIdentity();
6649 try {
6650 // NetworkStatsService keeps tracking the active network interface and identity. It
6651 // records the delta with the corresponding network identity.
6652 // We just return the total video call data usage snapshot since boot.
6653 Phone phone = getPhone(subId);
6654 if (phone != null) {
6655 return phone.getVtDataUsage(perUidStats);
6656 }
6657 return null;
6658 } finally {
6659 Binder.restoreCallingIdentity(identity);
Jack Yueb89b242016-06-22 13:27:47 -07006660 }
Jack Yueb89b242016-06-22 13:27:47 -07006661 }
Jack Yu75ab2952016-07-08 14:29:33 -07006662
6663 /**
6664 * Policy control of data connection. Usually used when data limit is passed.
6665 * @param enabled True if enabling the data, otherwise disabling.
6666 * @param subId Subscription index
6667 * {@hide}
6668 */
6669 @Override
6670 public void setPolicyDataEnabled(boolean enabled, int subId) {
6671 enforceModifyPermission();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006672
6673 final long identity = Binder.clearCallingIdentity();
6674 try {
6675 Phone phone = getPhone(subId);
6676 if (phone != null) {
Jack Yud79fba22018-12-13 11:51:28 -08006677 phone.getDataEnabledSettings().setPolicyDataEnabled(enabled);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006678 }
6679 } finally {
6680 Binder.restoreCallingIdentity(identity);
Jack Yu75ab2952016-07-08 14:29:33 -07006681 }
6682 }
Sooraj Sasindran9a909312016-07-18 11:57:25 -07006683
6684 /**
6685 * Get Client request stats
6686 * @return List of Client Request Stats
6687 * @hide
6688 */
6689 @Override
6690 public List<ClientRequestStats> getClientRequestStats(String callingPackage, int subId) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08006691 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
Jeff Davidson913390f2018-02-23 17:11:49 -08006692 mApp, subId, callingPackage, "getClientRequestStats")) {
Sooraj Sasindran9a909312016-07-18 11:57:25 -07006693 return null;
6694 }
Sooraj Sasindran9a909312016-07-18 11:57:25 -07006695 Phone phone = getPhone(subId);
Sooraj Sasindran9a909312016-07-18 11:57:25 -07006696
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006697 final long identity = Binder.clearCallingIdentity();
6698 try {
6699 if (phone != null) {
6700 return phone.getClientRequestStats();
6701 }
6702
6703 return null;
6704 } finally {
6705 Binder.restoreCallingIdentity(identity);
6706 }
Sooraj Sasindran9a909312016-07-18 11:57:25 -07006707 }
6708
Narayan Kamathf04b5a12018-01-09 11:47:15 +00006709 private WorkSource getWorkSource(int uid) {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08006710 String packageName = mApp.getPackageManager().getNameForUid(uid);
Narayan Kamathf04b5a12018-01-09 11:47:15 +00006711 return new WorkSource(uid, packageName);
Sooraj Sasindran9a909312016-07-18 11:57:25 -07006712 }
Jack Yueb4124c2017-02-16 15:32:43 -08006713
6714 /**
Grace Chen70990072017-03-24 17:21:30 -07006715 * Set SIM card power state.
Jack Yueb4124c2017-02-16 15:32:43 -08006716 *
Sanket Padawe13bac7b2017-03-20 15:04:47 -07006717 * @param slotIndex SIM slot id.
Grace Chen70990072017-03-24 17:21:30 -07006718 * @param state State of SIM (power down, power up, pass through)
6719 * - {@link android.telephony.TelephonyManager#CARD_POWER_DOWN}
6720 * - {@link android.telephony.TelephonyManager#CARD_POWER_UP}
6721 * - {@link android.telephony.TelephonyManager#CARD_POWER_UP_PASS_THROUGH}
Jack Yueb4124c2017-02-16 15:32:43 -08006722 *
6723 **/
6724 @Override
Grace Chen70990072017-03-24 17:21:30 -07006725 public void setSimPowerStateForSlot(int slotIndex, int state) {
Jack Yueb4124c2017-02-16 15:32:43 -08006726 enforceModifyPermission();
Sanket Padawe13bac7b2017-03-20 15:04:47 -07006727 Phone phone = PhoneFactory.getPhone(slotIndex);
6728
vagdeviaf9a5b92018-08-15 16:01:53 -07006729 WorkSource workSource = getWorkSource(Binder.getCallingUid());
6730
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006731 final long identity = Binder.clearCallingIdentity();
6732 try {
6733 if (phone != null) {
vagdeviaf9a5b92018-08-15 16:01:53 -07006734 phone.setSimPowerState(state, workSource);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006735 }
6736 } finally {
6737 Binder.restoreCallingIdentity(identity);
Jack Yueb4124c2017-02-16 15:32:43 -08006738 }
6739 }
Shuo Qiandd210312017-04-12 22:11:33 +00006740
Tyler Gunn65d45c22017-06-05 11:22:26 -07006741 private boolean isUssdApiAllowed(int subId) {
6742 CarrierConfigManager configManager =
Nazanin Bakhshif782e562018-12-11 15:15:39 -08006743 (CarrierConfigManager) mApp.getSystemService(Context.CARRIER_CONFIG_SERVICE);
Tyler Gunn65d45c22017-06-05 11:22:26 -07006744 if (configManager == null) {
6745 return false;
6746 }
6747 PersistableBundle pb = configManager.getConfigForSubId(subId);
6748 if (pb == null) {
6749 return false;
6750 }
6751 return pb.getBoolean(
6752 CarrierConfigManager.KEY_ALLOW_USSD_REQUESTS_VIA_TELEPHONY_MANAGER_BOOL);
6753 }
6754
Shuo Qiandd210312017-04-12 22:11:33 +00006755 /**
6756 * Check if phone is in emergency callback mode
6757 * @return true if phone is in emergency callback mode
6758 * @param subId sub id
6759 */
goneil9c5f4872017-12-05 14:07:56 -08006760 @Override
Shuo Qiandd210312017-04-12 22:11:33 +00006761 public boolean getEmergencyCallbackMode(int subId) {
Brad Ebinger35c841c2018-10-01 10:40:55 -07006762 enforceReadPrivilegedPermission("getEmergencyCallbackMode");
Shuo Qiandd210312017-04-12 22:11:33 +00006763 final Phone phone = getPhone(subId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006764
6765 final long identity = Binder.clearCallingIdentity();
6766 try {
6767 if (phone != null) {
6768 return phone.isInEcm();
6769 } else {
6770 return false;
6771 }
6772 } finally {
6773 Binder.restoreCallingIdentity(identity);
Shuo Qiandd210312017-04-12 22:11:33 +00006774 }
6775 }
Nathan Harold46b42aa2017-03-10 19:38:22 -08006776
6777 /**
6778 * Get the current signal strength information for the given subscription.
6779 * Because this information is not updated when the device is in a low power state
6780 * it should not be relied-upon to be current.
6781 * @param subId Subscription index
6782 * @return the most recent cached signal strength info from the modem
6783 */
6784 @Override
6785 public SignalStrength getSignalStrength(int subId) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006786 final long identity = Binder.clearCallingIdentity();
6787 try {
6788 Phone p = getPhone(subId);
6789 if (p == null) {
6790 return null;
6791 }
Nathan Harold46b42aa2017-03-10 19:38:22 -08006792
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006793 return p.getSignalStrength();
6794 } finally {
6795 Binder.restoreCallingIdentity(identity);
6796 }
Nathan Harold46b42aa2017-03-10 19:38:22 -08006797 }
Holly Jiuyu Sun01c47ad2018-01-24 17:56:33 +00006798
Pengquan Meng77b7f132018-08-22 14:49:57 -07006799 /**
Chen Xuf792fd62018-10-17 17:54:36 +00006800 * Get the current modem radio state for the given slot.
6801 * @param slotIndex slot index.
6802 * @param callingPackage the name of the package making the call.
6803 * @return the current radio power state from the modem
6804 */
6805 @Override
6806 public int getRadioPowerState(int slotIndex, String callingPackage) {
6807 Phone phone = PhoneFactory.getPhone(slotIndex);
6808 if (phone != null) {
6809 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
6810 mApp, phone.getSubId(), callingPackage, "getRadioPowerState")) {
6811 return TelephonyManager.RADIO_POWER_UNAVAILABLE;
6812 }
6813
6814 final long identity = Binder.clearCallingIdentity();
6815 try {
6816 return phone.getRadioPowerState();
6817 } finally {
6818 Binder.restoreCallingIdentity(identity);
6819 }
6820 }
6821 return TelephonyManager.RADIO_POWER_UNAVAILABLE;
6822 }
6823
6824 /**
Pengquan Meng77b7f132018-08-22 14:49:57 -07006825 * Checks if data roaming is enabled on the subscription with id {@code subId}.
6826 *
6827 * <p>Requires one of the following permissions:
6828 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE},
6829 * {@link android.Manifest.permission#READ_PHONE_STATE} or that the calling app has carrier
6830 * privileges.
6831 *
6832 * @param subId subscription id
6833 * @return {@code true} if data roaming is enabled on this subscription, otherwise return
6834 * {@code false}.
6835 */
6836 @Override
6837 public boolean isDataRoamingEnabled(int subId) {
Pengquan Meng44e66f12019-04-01 10:48:20 -07006838 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.ACCESS_NETWORK_STATE,
6839 null /* message */);
6840
Pengquan Menga1bb6272018-09-06 09:59:22 -07006841 boolean isEnabled = false;
6842 final long identity = Binder.clearCallingIdentity();
Pengquan Meng77b7f132018-08-22 14:49:57 -07006843 try {
Pengquan Menga1bb6272018-09-06 09:59:22 -07006844 Phone phone = getPhone(subId);
6845 isEnabled = phone != null ? phone.getDataRoamingEnabled() : false;
Pengquan Meng77b7f132018-08-22 14:49:57 -07006846 } catch (Exception e) {
6847 TelephonyPermissions.enforeceCallingOrSelfReadPhoneStatePermissionOrCarrierPrivilege(
6848 mApp, subId, "isDataRoamingEnabled");
Pengquan Menga1bb6272018-09-06 09:59:22 -07006849 } finally {
6850 Binder.restoreCallingIdentity(identity);
Pengquan Meng77b7f132018-08-22 14:49:57 -07006851 }
Pengquan Menga1bb6272018-09-06 09:59:22 -07006852 return isEnabled;
Pengquan Meng77b7f132018-08-22 14:49:57 -07006853 }
6854
6855
6856 /**
6857 * Enables/Disables the data roaming on the subscription with id {@code subId}.
6858 *
6859 * <p> Requires permission:
6860 * {@link android.Manifest.permission#MODIFY_PHONE_STATE} or that the calling app has carrier
6861 * privileges.
6862 *
6863 * @param subId subscription id
6864 * @param isEnabled {@code true} means enable, {@code false} means disable.
6865 */
6866 @Override
6867 public void setDataRoamingEnabled(int subId, boolean isEnabled) {
Pengquan Meng44e66f12019-04-01 10:48:20 -07006868 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
6869 mApp, subId, "setDataRoamingEnabled");
6870
Pengquan Menga1bb6272018-09-06 09:59:22 -07006871 final long identity = Binder.clearCallingIdentity();
6872 try {
Pengquan Menga1bb6272018-09-06 09:59:22 -07006873 Phone phone = getPhone(subId);
6874 if (phone != null) {
6875 phone.setDataRoamingEnabled(isEnabled);
6876 }
6877 } finally {
6878 Binder.restoreCallingIdentity(identity);
Pengquan Meng77b7f132018-08-22 14:49:57 -07006879 }
6880 }
6881
Holly Jiuyu Sun01c47ad2018-01-24 17:56:33 +00006882 @Override
Pengquan Meng6884a2c2018-10-03 12:19:13 -07006883 public boolean isManualNetworkSelectionAllowed(int subId) {
Pengquan Meng44e66f12019-04-01 10:48:20 -07006884 TelephonyPermissions.enforeceCallingOrSelfReadPhoneStatePermissionOrCarrierPrivilege(
6885 mApp, subId, "isManualNetworkSelectionAllowed");
6886
Pengquan Meng6884a2c2018-10-03 12:19:13 -07006887 boolean isAllowed = true;
6888 final long identity = Binder.clearCallingIdentity();
6889 try {
Pengquan Meng6884a2c2018-10-03 12:19:13 -07006890 Phone phone = getPhone(subId);
6891 if (phone != null) {
6892 isAllowed = phone.isCspPlmnEnabled();
6893 }
6894 } finally {
6895 Binder.restoreCallingIdentity(identity);
6896 }
6897 return isAllowed;
6898 }
6899
6900 @Override
Jordan Liu75f43ea2019-01-17 16:56:37 -08006901 public List<UiccCardInfo> getUiccCardsInfo(String callingPackage) {
Jordan Liu1e142fc2019-04-22 15:10:43 -07006902 boolean hasReadPermission = false;
Jordan Liuc65bc952019-02-12 17:54:02 -08006903 try {
6904 enforceReadPrivilegedPermission("getUiccCardsInfo");
Jordan Liu1e142fc2019-04-22 15:10:43 -07006905 hasReadPermission = true;
Jordan Liuc65bc952019-02-12 17:54:02 -08006906 } catch (SecurityException e) {
6907 // even without READ_PRIVILEGED_PHONE_STATE, we allow the call to continue if the caller
6908 // has carrier privileges on an active UICC
6909 if (checkCarrierPrivilegesForPackageAnyPhone(callingPackage)
6910 != TelephonyManager.CARRIER_PRIVILEGE_STATUS_HAS_ACCESS) {
Jordan Liu1e142fc2019-04-22 15:10:43 -07006911 throw new SecurityException("Caller does not have permission.");
Jordan Liuc65bc952019-02-12 17:54:02 -08006912 }
Jordan Liu75f43ea2019-01-17 16:56:37 -08006913 }
Jordan Liu5aa07002018-12-18 15:44:48 -08006914
6915 final long identity = Binder.clearCallingIdentity();
6916 try {
Jordan Liu75f43ea2019-01-17 16:56:37 -08006917 UiccController uiccController = UiccController.getInstance();
6918 ArrayList<UiccCardInfo> cardInfos = uiccController.getAllUiccCardInfos();
Jordan Liu1e142fc2019-04-22 15:10:43 -07006919 if (hasReadPermission) {
6920 return cardInfos;
Jordan Liu75f43ea2019-01-17 16:56:37 -08006921 }
Jordan Liu1e142fc2019-04-22 15:10:43 -07006922
6923 // Remove private info if the caller doesn't have access
6924 ArrayList<UiccCardInfo> filteredInfos = new ArrayList<>();
6925 for (UiccCardInfo cardInfo : cardInfos) {
6926 // For an inactive eUICC, the UiccCard will be null even though the UiccCardInfo
6927 // is available
6928 UiccCard card = uiccController.getUiccCardForSlot(cardInfo.getSlotIndex());
6929 if (card == null || card.getUiccProfile() == null) {
6930 // assume no access if the card or profile is unavailable
6931 filteredInfos.add(cardInfo.getUnprivileged());
6932 continue;
6933 }
6934 UiccProfile profile = card.getUiccProfile();
6935 if (profile.getCarrierPrivilegeStatus(mApp.getPackageManager(), callingPackage)
6936 == TelephonyManager.CARRIER_PRIVILEGE_STATUS_HAS_ACCESS) {
6937 filteredInfos.add(cardInfo);
6938 } else {
6939 filteredInfos.add(cardInfo.getUnprivileged());
6940 }
6941 }
6942 return filteredInfos;
Jordan Liu5aa07002018-12-18 15:44:48 -08006943 } finally {
6944 Binder.restoreCallingIdentity(identity);
6945 }
6946 }
6947
6948 @Override
Holly Jiuyu Sun01c47ad2018-01-24 17:56:33 +00006949 public UiccSlotInfo[] getUiccSlotsInfo() {
Brad Ebinger35c841c2018-10-01 10:40:55 -07006950 enforceReadPrivilegedPermission("getUiccSlotsInfo");
Holly Jiuyu Sun01c47ad2018-01-24 17:56:33 +00006951
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006952 final long identity = Binder.clearCallingIdentity();
6953 try {
6954 UiccSlot[] slots = UiccController.getInstance().getUiccSlots();
6955 if (slots == null) {
6956 Rlog.i(LOG_TAG, "slots is null.");
6957 return null;
6958 }
6959
6960 UiccSlotInfo[] infos = new UiccSlotInfo[slots.length];
6961 for (int i = 0; i < slots.length; i++) {
6962 UiccSlot slot = slots[i];
6963 if (slot == null) {
6964 continue;
6965 }
6966
Jordan Liu7be7e652019-05-06 18:55:02 +00006967 String cardId;
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006968 UiccCard card = slot.getUiccCard();
6969 if (card != null) {
6970 cardId = card.getCardId();
Jordan Liu7be7e652019-05-06 18:55:02 +00006971 } else {
Jordan Liud96b5292019-09-12 16:19:43 -07006972 cardId = slot.getEid();
6973 if (TextUtils.isEmpty(cardId)) {
6974 cardId = slot.getIccId();
6975 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006976 }
6977
Jordan Liu857451f2019-05-09 16:35:35 -07006978 if (cardId != null) {
6979 // if cardId is an ICCID, strip off trailing Fs before exposing to user
6980 // if cardId is an EID, it's all digits so this is fine
6981 cardId = IccUtils.stripTrailingFs(cardId);
6982 }
6983
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006984 int cardState = 0;
6985 switch (slot.getCardState()) {
6986 case CARDSTATE_ABSENT:
6987 cardState = UiccSlotInfo.CARD_STATE_INFO_ABSENT;
6988 break;
6989 case CARDSTATE_PRESENT:
6990 cardState = UiccSlotInfo.CARD_STATE_INFO_PRESENT;
6991 break;
6992 case CARDSTATE_ERROR:
6993 cardState = UiccSlotInfo.CARD_STATE_INFO_ERROR;
6994 break;
6995 case CARDSTATE_RESTRICTED:
6996 cardState = UiccSlotInfo.CARD_STATE_INFO_RESTRICTED;
6997 break;
6998 default:
6999 break;
7000
7001 }
7002
7003 infos[i] = new UiccSlotInfo(
7004 slot.isActive(),
7005 slot.isEuicc(),
7006 cardId,
7007 cardState,
7008 slot.getPhoneId(),
Jordan Liua2619582019-02-14 12:56:40 -08007009 slot.isExtendedApduSupported(),
7010 slot.isRemovable());
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007011 }
7012 return infos;
7013 } finally {
7014 Binder.restoreCallingIdentity(identity);
Holly Jiuyu Sun1d957c52018-04-04 13:52:42 -07007015 }
Holly Jiuyu Sun01c47ad2018-01-24 17:56:33 +00007016 }
7017
7018 @Override
7019 public boolean switchSlots(int[] physicalSlots) {
7020 enforceModifyPermission();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007021
7022 final long identity = Binder.clearCallingIdentity();
7023 try {
7024 return (Boolean) sendRequest(CMD_SWITCH_SLOTS, physicalSlots);
7025 } finally {
7026 Binder.restoreCallingIdentity(identity);
7027 }
Holly Jiuyu Sun01c47ad2018-01-24 17:56:33 +00007028 }
Jack Yu4c988042018-02-27 15:30:01 -08007029
7030 @Override
Jordan Liu7de49fa2018-12-06 14:48:49 -08007031 public int getCardIdForDefaultEuicc(int subId, String callingPackage) {
Jordan Liu7de49fa2018-12-06 14:48:49 -08007032 final long identity = Binder.clearCallingIdentity();
7033 try {
7034 return UiccController.getInstance().getCardIdForDefaultEuicc();
7035 } finally {
7036 Binder.restoreCallingIdentity(identity);
7037 }
7038 }
7039
7040 @Override
Jack Yu4c988042018-02-27 15:30:01 -08007041 public void setRadioIndicationUpdateMode(int subId, int filters, int mode) {
7042 enforceModifyPermission();
7043 final Phone phone = getPhone(subId);
7044 if (phone == null) {
7045 loge("setRadioIndicationUpdateMode fails with invalid subId: " + subId);
7046 return;
7047 }
7048
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007049 final long identity = Binder.clearCallingIdentity();
7050 try {
7051 phone.setRadioIndicationUpdateMode(filters, mode);
7052 } finally {
7053 Binder.restoreCallingIdentity(identity);
7054 }
Jack Yu4c988042018-02-27 15:30:01 -08007055 }
Pengquan Meng85728fb2018-03-12 16:31:21 -07007056
7057 /**
goneil47ffb6e2018-04-06 15:40:58 -07007058 * A test API to reload the UICC profile.
7059 *
7060 * <p>Requires that the calling app has permission
7061 * {@link android.Manifest.permission#MODIFY_PHONE_STATE MODIFY_PHONE_STATE}.
7062 * @hide
7063 */
7064 @Override
7065 public void refreshUiccProfile(int subId) {
7066 enforceModifyPermission();
7067
7068 final long identity = Binder.clearCallingIdentity();
7069 try {
7070 Phone phone = getPhone(subId);
7071 if (phone == null) {
7072 return;
7073 }
7074 UiccCard uiccCard = phone.getUiccCard();
7075 if (uiccCard == null) {
7076 return;
7077 }
7078 UiccProfile uiccProfile = uiccCard.getUiccProfile();
7079 if (uiccProfile == null) {
7080 return;
7081 }
7082 uiccProfile.refresh();
7083 } finally {
7084 Binder.restoreCallingIdentity(identity);
7085 }
7086 }
7087
7088 /**
Pengquan Meng85728fb2018-03-12 16:31:21 -07007089 * Returns false if the mobile data is disabled by default, otherwise return true.
7090 */
7091 private boolean getDefaultDataEnabled() {
Inseob Kim8d298d42018-12-13 17:11:34 +09007092 return TelephonyProperties.mobile_data().orElse(true);
Pengquan Meng85728fb2018-03-12 16:31:21 -07007093 }
7094
7095 /**
7096 * Returns true if the data roaming is enabled by default, i.e the system property
7097 * of {@link #DEFAULT_DATA_ROAMING_PROPERTY_NAME} is true or the config of
7098 * {@link CarrierConfigManager#KEY_CARRIER_DEFAULT_DATA_ROAMING_ENABLED_BOOL} is true.
7099 */
7100 private boolean getDefaultDataRoamingEnabled(int subId) {
7101 final CarrierConfigManager configMgr = (CarrierConfigManager)
Nazanin Bakhshif782e562018-12-11 15:15:39 -08007102 mApp.getSystemService(Context.CARRIER_CONFIG_SERVICE);
Inseob Kim8d298d42018-12-13 17:11:34 +09007103 boolean isDataRoamingEnabled = TelephonyProperties.data_roaming().orElse(true);
Pengquan Meng85728fb2018-03-12 16:31:21 -07007104 isDataRoamingEnabled |= configMgr.getConfigForSubId(subId).getBoolean(
7105 CarrierConfigManager.KEY_CARRIER_DEFAULT_DATA_ROAMING_ENABLED_BOOL);
7106 return isDataRoamingEnabled;
7107 }
7108
7109 /**
7110 * Returns the default network type for the given {@code subId}, if the default network type is
7111 * not set, return {@link Phone#PREFERRED_NT_MODE}.
7112 */
7113 private int getDefaultNetworkType(int subId) {
Inseob Kim8d298d42018-12-13 17:11:34 +09007114 List<Integer> list = TelephonyProperties.default_network();
7115 int phoneId = mSubscriptionController.getPhoneId(subId);
7116 if (phoneId >= 0 && phoneId < list.size() && list.get(phoneId) != null) {
7117 return list.get(phoneId);
7118 }
7119 return Phone.PREFERRED_NT_MODE;
Pengquan Meng85728fb2018-03-12 16:31:21 -07007120 }
fionaxua13278b2018-03-21 00:08:13 -07007121
7122 @Override
7123 public void setCarrierTestOverride(int subId, String mccmnc, String imsi, String iccid, String
chen xueaba88a2019-03-15 13:15:10 -07007124 gid1, String gid2, String plmn, String spn, String carrierPrivilegeRules, String apn) {
fionaxua13278b2018-03-21 00:08:13 -07007125 enforceModifyPermission();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007126
7127 final long identity = Binder.clearCallingIdentity();
7128 try {
7129 final Phone phone = getPhone(subId);
7130 if (phone == null) {
7131 loge("setCarrierTestOverride fails with invalid subId: " + subId);
7132 return;
7133 }
chen xueaba88a2019-03-15 13:15:10 -07007134 phone.setCarrierTestOverride(mccmnc, imsi, iccid, gid1, gid2, plmn, spn,
7135 carrierPrivilegeRules, apn);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007136 } finally {
7137 Binder.restoreCallingIdentity(identity);
fionaxua13278b2018-03-21 00:08:13 -07007138 }
fionaxua13278b2018-03-21 00:08:13 -07007139 }
7140
7141 @Override
7142 public int getCarrierIdListVersion(int subId) {
Brad Ebinger35c841c2018-10-01 10:40:55 -07007143 enforceReadPrivilegedPermission("getCarrierIdListVersion");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007144
7145 final long identity = Binder.clearCallingIdentity();
7146 try {
7147 final Phone phone = getPhone(subId);
7148 if (phone == null) {
7149 loge("getCarrierIdListVersion fails with invalid subId: " + subId);
7150 return TelephonyManager.UNKNOWN_CARRIER_ID_LIST_VERSION;
7151 }
7152 return phone.getCarrierIdListVersion();
7153 } finally {
7154 Binder.restoreCallingIdentity(identity);
fionaxua13278b2018-03-21 00:08:13 -07007155 }
fionaxua13278b2018-03-21 00:08:13 -07007156 }
Malcolm Chen2c63d402018-08-14 16:00:53 -07007157
7158 @Override
7159 public int getNumberOfModemsWithSimultaneousDataConnections(int subId, String callingPackage) {
7160 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
7161 mApp, subId, callingPackage, "getNumberOfModemsWithSimultaneousDataConnections")) {
7162 return -1;
7163 }
7164
7165 final long identity = Binder.clearCallingIdentity();
7166 try {
7167 return mPhoneConfigurationManager.getNumberOfModemsWithSimultaneousDataConnections();
7168 } finally {
7169 Binder.restoreCallingIdentity(identity);
7170 }
7171 }
Pengquan Menga1bb6272018-09-06 09:59:22 -07007172
7173 @Override
7174 public int getCdmaRoamingMode(int subId) {
7175 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
7176 mApp, subId, "getCdmaRoamingMode");
7177
7178 final long identity = Binder.clearCallingIdentity();
7179 try {
7180 return (int) sendRequest(CMD_GET_CDMA_ROAMING_MODE, null /* argument */, subId);
7181 } finally {
7182 Binder.restoreCallingIdentity(identity);
7183 }
7184 }
7185
7186 @Override
7187 public boolean setCdmaRoamingMode(int subId, int mode) {
7188 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
7189 mApp, subId, "setCdmaRoamingMode");
7190
7191 final long identity = Binder.clearCallingIdentity();
7192 try {
7193 return (boolean) sendRequest(CMD_SET_CDMA_ROAMING_MODE, mode, subId);
7194 } finally {
7195 Binder.restoreCallingIdentity(identity);
7196 }
7197 }
7198
7199 @Override
7200 public boolean setCdmaSubscriptionMode(int subId, int mode) {
7201 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
7202 mApp, subId, "setCdmaSubscriptionMode");
7203
7204 final long identity = Binder.clearCallingIdentity();
7205 try {
7206 return (boolean) sendRequest(CMD_SET_CDMA_SUBSCRIPTION_MODE, mode, subId);
7207 } finally {
7208 Binder.restoreCallingIdentity(identity);
7209 }
7210 }
Makoto Onukida3bf792018-09-18 16:06:29 -07007211
sqianc5eccab2018-10-19 18:46:41 -07007212 @Override
sqian8c685422019-02-22 15:55:18 -08007213 public Map<Integer, List<EmergencyNumber>> getEmergencyNumberList(
sqianc5eccab2018-10-19 18:46:41 -07007214 String callingPackage) {
sqian11b7a0e2018-12-05 18:48:28 -08007215 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
sqian8c685422019-02-22 15:55:18 -08007216 mApp, getDefaultSubscription(), callingPackage, "getEmergencyNumberList")) {
sqian11b7a0e2018-12-05 18:48:28 -08007217 throw new SecurityException("Requires READ_PHONE_STATE permission.");
7218 }
7219 final long identity = Binder.clearCallingIdentity();
7220 try {
sqian854d44b2018-12-12 16:48:18 -08007221 Map<Integer, List<EmergencyNumber>> emergencyNumberListInternal = new HashMap<>();
7222 for (Phone phone: PhoneFactory.getPhones()) {
7223 if (phone.getEmergencyNumberTracker() != null
7224 && phone.getEmergencyNumberTracker().getEmergencyNumberList() != null) {
7225 emergencyNumberListInternal.put(
7226 phone.getSubId(),
7227 phone.getEmergencyNumberTracker().getEmergencyNumberList());
7228 }
sqian11b7a0e2018-12-05 18:48:28 -08007229 }
sqian854d44b2018-12-12 16:48:18 -08007230 return emergencyNumberListInternal;
sqian11b7a0e2018-12-05 18:48:28 -08007231 } finally {
7232 Binder.restoreCallingIdentity(identity);
7233 }
sqianc5eccab2018-10-19 18:46:41 -07007234 }
7235
7236 @Override
sqian8c685422019-02-22 15:55:18 -08007237 public boolean isEmergencyNumber(String number, boolean exactMatch) {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08007238 final Phone defaultPhone = getDefaultPhone();
sqian11b7a0e2018-12-05 18:48:28 -08007239 if (!exactMatch) {
7240 TelephonyPermissions
7241 .enforeceCallingOrSelfReadPrivilegedPhoneStatePermissionOrCarrierPrivilege(
sqian8c685422019-02-22 15:55:18 -08007242 mApp, defaultPhone.getSubId(), "isEmergencyNumber(Potential)");
sqian11b7a0e2018-12-05 18:48:28 -08007243 }
7244 final long identity = Binder.clearCallingIdentity();
7245 try {
sqian854d44b2018-12-12 16:48:18 -08007246 for (Phone phone: PhoneFactory.getPhones()) {
7247 if (phone.getEmergencyNumberTracker() != null
7248 && phone.getEmergencyNumberTracker() != null) {
7249 if (phone.getEmergencyNumberTracker().isEmergencyNumber(
7250 number, exactMatch)) {
7251 return true;
sqian11b7a0e2018-12-05 18:48:28 -08007252 }
7253 }
sqian11b7a0e2018-12-05 18:48:28 -08007254 }
7255 return false;
7256 } finally {
7257 Binder.restoreCallingIdentity(identity);
7258 }
7259 }
7260
sqianf4ca7ed2019-01-15 18:32:07 -08007261 /**
7262 * Update emergency number list for test mode.
7263 */
7264 @Override
7265 public void updateEmergencyNumberListTestMode(int action, EmergencyNumber num) {
7266 TelephonyPermissions.enforceShellOnly(Binder.getCallingUid(),
7267 "updateEmergencyNumberListTestMode");
7268
7269 final long identity = Binder.clearCallingIdentity();
7270 try {
7271 for (Phone phone: PhoneFactory.getPhones()) {
7272 EmergencyNumberTracker tracker = phone.getEmergencyNumberTracker();
7273 if (tracker != null) {
7274 tracker.executeEmergencyNumberTestModeCommand(action, num);
7275 }
7276 }
7277 } finally {
7278 Binder.restoreCallingIdentity(identity);
7279 }
7280 }
7281
7282 /**
7283 * Get the full emergency number list for test mode.
7284 */
7285 @Override
7286 public List<String> getEmergencyNumberListTestMode() {
7287 TelephonyPermissions.enforceShellOnly(Binder.getCallingUid(),
7288 "getEmergencyNumberListTestMode");
7289
7290 final long identity = Binder.clearCallingIdentity();
7291 try {
7292 Set<String> emergencyNumbers = new HashSet<>();
7293 for (Phone phone: PhoneFactory.getPhones()) {
7294 EmergencyNumberTracker tracker = phone.getEmergencyNumberTracker();
7295 if (tracker != null) {
7296 for (EmergencyNumber num : tracker.getEmergencyNumberList()) {
7297 emergencyNumbers.add(num.getNumber());
7298 }
7299 }
7300 }
7301 return new ArrayList<>(emergencyNumbers);
7302 } finally {
7303 Binder.restoreCallingIdentity(identity);
7304 }
7305 }
7306
chen xud6b45bd2018-10-30 22:27:10 -07007307 @Override
Shuo Qianf2b2df42019-11-13 17:43:31 -08007308 public int getEmergencyNumberDbVersion(int subId) {
7309 enforceReadPrivilegedPermission("getEmergencyNumberDbVersion");
7310
7311 final long identity = Binder.clearCallingIdentity();
7312 try {
7313 final Phone phone = getPhone(subId);
7314 if (phone == null) {
7315 loge("getEmergencyNumberDbVersion fails with invalid subId: " + subId);
7316 return TelephonyManager.INVALID_EMERGENCY_NUMBER_DB_VERSION;
7317 }
7318 return phone.getEmergencyNumberDbVersion();
7319 } finally {
7320 Binder.restoreCallingIdentity(identity);
7321 }
7322 }
7323
7324 @Override
7325 public void notifyOtaEmergencyNumberDbInstalled() {
7326 enforceModifyPermission();
7327
7328 final long identity = Binder.clearCallingIdentity();
7329 try {
7330 for (Phone phone: PhoneFactory.getPhones()) {
7331 EmergencyNumberTracker tracker = phone.getEmergencyNumberTracker();
7332 if (tracker != null) {
7333 tracker.updateOtaEmergencyNumberDatabase();
7334 }
7335 }
7336 } finally {
7337 Binder.restoreCallingIdentity(identity);
7338 }
7339 }
7340
7341 @Override
7342 public void updateTestOtaEmergencyNumberDbFilePath(String otaFilePath) {
7343 enforceActiveEmergencySessionPermission();
7344
7345 final long identity = Binder.clearCallingIdentity();
7346 try {
7347 for (Phone phone: PhoneFactory.getPhones()) {
7348 EmergencyNumberTracker tracker = phone.getEmergencyNumberTracker();
7349 if (tracker != null) {
7350 tracker.updateTestOtaEmergencyNumberDbFilePath(otaFilePath);
7351 }
7352 }
7353 } finally {
7354 Binder.restoreCallingIdentity(identity);
7355 }
7356 }
7357
7358 @Override
chen xud6b45bd2018-10-30 22:27:10 -07007359 public List<String> getCertsFromCarrierPrivilegeAccessRules(int subId) {
7360 enforceReadPrivilegedPermission("getCertsFromCarrierPrivilegeAccessRules");
7361 Phone phone = getPhone(subId);
7362 if (phone == null) {
7363 return null;
7364 }
7365 final long identity = Binder.clearCallingIdentity();
7366 try {
7367 UiccProfile profile = UiccController.getInstance()
7368 .getUiccProfileForPhone(phone.getPhoneId());
7369 if (profile != null) {
7370 return profile.getCertsFromCarrierPrivilegeAccessRules();
7371 }
7372 } finally {
7373 Binder.restoreCallingIdentity(identity);
7374 }
7375 return null;
7376 }
Malcolm Chen8e4ed912019-01-15 20:22:16 -08007377
7378 /**
7379 * Enable or disable a modem stack.
7380 */
7381 @Override
7382 public boolean enableModemForSlot(int slotIndex, boolean enable) {
7383 enforceModifyPermission();
7384
7385 final long identity = Binder.clearCallingIdentity();
7386 try {
7387 Phone phone = PhoneFactory.getPhone(slotIndex);
7388 if (phone == null) {
7389 return false;
7390 } else {
7391 return (Boolean) sendRequest(CMD_REQUEST_ENABLE_MODEM, enable, phone, null);
7392 }
7393 } finally {
7394 Binder.restoreCallingIdentity(identity);
7395 }
7396 }
Michelecea4cf22018-12-21 15:00:11 -08007397
Malcolm Chen4bcd9822019-03-27 18:34:05 -07007398 /**
7399 * Whether a modem stack is enabled or not.
7400 */
7401 @Override
7402 public boolean isModemEnabledForSlot(int slotIndex, String callingPackage) {
7403 Phone phone = PhoneFactory.getPhone(slotIndex);
7404 if (phone == null) return false;
7405
7406 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
7407 mApp, phone.getSubId(), callingPackage, "isModemEnabledForSlot")) {
7408 throw new SecurityException("Requires READ_PHONE_STATE permission.");
7409 }
7410
7411 final long identity = Binder.clearCallingIdentity();
7412 try {
Nazanin Bakhshif71371d2019-04-29 17:29:44 -07007413 try {
7414 return mPhoneConfigurationManager.getPhoneStatusFromCache(phone.getPhoneId());
7415 } catch (NoSuchElementException ex) {
7416 return (Boolean) sendRequest(CMD_GET_MODEM_STATUS, null, phone, null);
7417 }
Malcolm Chen4bcd9822019-03-27 18:34:05 -07007418 } finally {
7419 Binder.restoreCallingIdentity(identity);
7420 }
7421 }
7422
Michelecea4cf22018-12-21 15:00:11 -08007423 @Override
Michele0ea7d782019-03-19 14:58:42 -07007424 public void setMultiSimCarrierRestriction(boolean isMultiSimCarrierRestricted) {
Michelecea4cf22018-12-21 15:00:11 -08007425 enforceModifyPermission();
7426
7427 final long identity = Binder.clearCallingIdentity();
7428 try {
7429 mTelephonySharedPreferences.edit()
Michele0ea7d782019-03-19 14:58:42 -07007430 .putBoolean(PREF_MULTI_SIM_RESTRICTED, isMultiSimCarrierRestricted)
Michelecea4cf22018-12-21 15:00:11 -08007431 .commit();
7432 } finally {
7433 Binder.restoreCallingIdentity(identity);
7434 }
7435 }
7436
7437 @Override
Michele0ea7d782019-03-19 14:58:42 -07007438 @TelephonyManager.IsMultiSimSupportedResult
7439 public int isMultiSimSupported(String callingPackage) {
Michele4245e952019-02-04 11:36:23 -08007440 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(mApp,
Michele0ea7d782019-03-19 14:58:42 -07007441 getDefaultPhone().getSubId(), callingPackage, "isMultiSimSupported")) {
7442 return TelephonyManager.MULTISIM_NOT_SUPPORTED_BY_HARDWARE;
Michele4245e952019-02-04 11:36:23 -08007443 }
Michelecea4cf22018-12-21 15:00:11 -08007444
7445 final long identity = Binder.clearCallingIdentity();
7446 try {
Michele0ea7d782019-03-19 14:58:42 -07007447 return isMultiSimSupportedInternal();
Michelecea4cf22018-12-21 15:00:11 -08007448 } finally {
7449 Binder.restoreCallingIdentity(identity);
7450 }
7451 }
Nazanin Bakhshi628473f2019-01-29 17:37:52 -08007452
Michele0ea7d782019-03-19 14:58:42 -07007453 @TelephonyManager.IsMultiSimSupportedResult
7454 private int isMultiSimSupportedInternal() {
Michele30b57b22019-03-01 12:01:14 -08007455 // If the device has less than 2 SIM cards, indicate that multisim is restricted.
7456 int numPhysicalSlots = UiccController.getInstance().getUiccSlots().length;
7457 if (numPhysicalSlots < 2) {
Michele0ea7d782019-03-19 14:58:42 -07007458 loge("isMultiSimSupportedInternal: requires at least 2 cards");
7459 return TelephonyManager.MULTISIM_NOT_SUPPORTED_BY_HARDWARE;
Michele30b57b22019-03-01 12:01:14 -08007460 }
7461 // Check if the hardware supports multisim functionality. If usage of multisim is not
7462 // supported by the modem, indicate that it is restricted.
7463 PhoneCapability staticCapability =
7464 mPhoneConfigurationManager.getStaticPhoneCapability();
7465 if (staticCapability == null) {
Michele0ea7d782019-03-19 14:58:42 -07007466 loge("isMultiSimSupportedInternal: no static configuration available");
7467 return TelephonyManager.MULTISIM_NOT_SUPPORTED_BY_HARDWARE;
Michele30b57b22019-03-01 12:01:14 -08007468 }
7469 if (staticCapability.logicalModemList.size() < 2) {
Michele0ea7d782019-03-19 14:58:42 -07007470 loge("isMultiSimSupportedInternal: maximum number of modem is < 2");
7471 return TelephonyManager.MULTISIM_NOT_SUPPORTED_BY_HARDWARE;
Michele30b57b22019-03-01 12:01:14 -08007472 }
7473 // Check if support of multiple SIMs is restricted by carrier
7474 if (mTelephonySharedPreferences.getBoolean(PREF_MULTI_SIM_RESTRICTED, false)) {
Michele0ea7d782019-03-19 14:58:42 -07007475 return TelephonyManager.MULTISIM_NOT_SUPPORTED_BY_CARRIER;
Michele30b57b22019-03-01 12:01:14 -08007476 }
7477
Michele0ea7d782019-03-19 14:58:42 -07007478 return TelephonyManager.MULTISIM_ALLOWED;
Michele30b57b22019-03-01 12:01:14 -08007479 }
7480
Nazanin Bakhshi628473f2019-01-29 17:37:52 -08007481 /**
7482 * Switch configs to enable multi-sim or switch back to single-sim
Nazanin Bakhshi17318782019-03-01 11:56:08 -08007483 * Note: Switch from multi-sim to single-sim is only possible with MODIFY_PHONE_STATE
7484 * permission, but the other way around is possible with either MODIFY_PHONE_STATE
7485 * or carrier privileges
Nazanin Bakhshi628473f2019-01-29 17:37:52 -08007486 * @param numOfSims number of active sims we want to switch to
7487 */
7488 @Override
7489 public void switchMultiSimConfig(int numOfSims) {
Nazanin Bakhshi17318782019-03-01 11:56:08 -08007490 if (numOfSims == 1) {
7491 enforceModifyPermission();
7492 } else {
7493 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
7494 mApp, SubscriptionManager.DEFAULT_SUBSCRIPTION_ID, "switchMultiSimConfig");
7495 }
Nazanin Bakhshi628473f2019-01-29 17:37:52 -08007496 final long identity = Binder.clearCallingIdentity();
Michele30b57b22019-03-01 12:01:14 -08007497
Nazanin Bakhshi628473f2019-01-29 17:37:52 -08007498 try {
Michele30b57b22019-03-01 12:01:14 -08007499 //only proceed if multi-sim is not restricted
Michele0ea7d782019-03-19 14:58:42 -07007500 if (isMultiSimSupportedInternal() != TelephonyManager.MULTISIM_ALLOWED) {
Michele30b57b22019-03-01 12:01:14 -08007501 loge("switchMultiSimConfig not possible. It is restricted or not supported.");
7502 return;
7503 }
Nazanin Bakhshi628473f2019-01-29 17:37:52 -08007504 mPhoneConfigurationManager.switchMultiSimConfig(numOfSims);
7505 } finally {
7506 Binder.restoreCallingIdentity(identity);
7507 }
7508 }
7509
Hyungjun Parkbb07fde2019-01-10 15:28:51 +09007510 @Override
7511 public boolean isApplicationOnUicc(int subId, int appType) {
7512 enforceReadPrivilegedPermission("isApplicationOnUicc");
7513 Phone phone = getPhone(subId);
7514 if (phone == null) {
7515 return false;
7516 }
7517 final long identity = Binder.clearCallingIdentity();
7518 try {
7519 UiccCard uiccCard = phone.getUiccCard();
7520 if (uiccCard == null) {
7521 return false;
7522 }
7523 UiccProfile uiccProfile = uiccCard.getUiccProfile();
7524 if (uiccProfile == null) {
7525 return false;
7526 }
7527 if (TelephonyManager.APPTYPE_SIM <= appType
7528 && appType <= TelephonyManager.APPTYPE_ISIM) {
7529 return uiccProfile.isApplicationOnIcc(AppType.values()[appType]);
7530 }
7531 return false;
7532 } finally {
7533 Binder.restoreCallingIdentity(identity);
7534 }
7535 }
7536
Nazanin Bakhshi628473f2019-01-29 17:37:52 -08007537 /**
chen xub4baa772019-04-03 10:23:41 -07007538 * Get whether making changes to modem configurations will trigger reboot.
7539 * Return value defaults to true.
Nazanin Bakhshi5fe5ef22019-01-30 10:52:09 -08007540 */
7541 @Override
chen xub4baa772019-04-03 10:23:41 -07007542 public boolean doesSwitchMultiSimConfigTriggerReboot(int subId, String callingPackage) {
7543 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
7544 mApp, subId, callingPackage, "doesSwitchMultiSimConfigTriggerReboot")) {
7545 return false;
7546 }
Nazanin Bakhshi5fe5ef22019-01-30 10:52:09 -08007547 final long identity = Binder.clearCallingIdentity();
7548 try {
7549 return mPhoneConfigurationManager.isRebootRequiredForModemConfigChange();
7550 } finally {
7551 Binder.restoreCallingIdentity(identity);
7552 }
7553 }
7554
Nathan Harold29f5f052019-02-15 13:41:57 -08007555 private void updateModemStateMetrics() {
7556 TelephonyMetrics metrics = TelephonyMetrics.getInstance();
7557 // TODO: check the state for each modem if the api is ready.
7558 metrics.updateEnabledModemBitmap((1 << TelephonyManager.from(mApp).getPhoneCount()) - 1);
7559 }
7560
Pengquan Meng3889a572019-01-23 11:16:29 -08007561 @Override
7562 public int[] getSlotsMapping() {
7563 enforceReadPrivilegedPermission("getSlotsMapping");
7564
7565 final long identity = Binder.clearCallingIdentity();
7566 try {
7567 int phoneCount = TelephonyManager.getDefault().getPhoneCount();
7568 // All logical slots should have a mapping to a physical slot.
7569 int[] logicalSlotsMapping = new int[phoneCount];
7570 UiccSlotInfo[] slotInfos = getUiccSlotsInfo();
7571 for (int i = 0; i < slotInfos.length; i++) {
7572 if (SubscriptionManager.isValidPhoneId(slotInfos[i].getLogicalSlotIdx())) {
7573 logicalSlotsMapping[slotInfos[i].getLogicalSlotIdx()] = i;
7574 }
7575 }
7576 return logicalSlotsMapping;
7577 } finally {
7578 Binder.restoreCallingIdentity(identity);
7579 }
7580 }
Nathan Harold48d6fd52019-02-06 19:01:40 -08007581
7582 /**
7583 * Get the IRadio HAL Version
7584 */
7585 @Override
7586 public int getRadioHalVersion() {
7587 Phone phone = getDefaultPhone();
7588 if (phone == null) return -1;
7589 HalVersion hv = phone.getHalVersion();
7590 if (hv.equals(HalVersion.UNKNOWN)) return -1;
7591 return hv.major * 100 + hv.minor;
7592 }
Malcolm Chendc8c10e2019-04-10 18:25:07 -07007593
7594 /**
Malcolm Chene5ad5792019-04-18 13:51:02 -07007595 * Return whether data is enabled for certain APN type. This will tell if framework will accept
7596 * corresponding network requests on a subId.
7597 *
7598 * Data is enabled if:
Malcolm Chendc8c10e2019-04-10 18:25:07 -07007599 * 1) user data is turned on, or
Malcolm Chene5ad5792019-04-18 13:51:02 -07007600 * 2) APN is un-metered for this subscription, or
7601 * 3) APN type is whitelisted. E.g. MMS is whitelisted if
7602 * {@link SubscriptionManager#setAlwaysAllowMmsData} is turned on.
7603 *
7604 * @return whether data is allowed for a apn type.
7605 *
7606 * @hide
Malcolm Chendc8c10e2019-04-10 18:25:07 -07007607 */
7608 @Override
Malcolm Chene5ad5792019-04-18 13:51:02 -07007609 public boolean isDataEnabledForApn(int apnType, int subId, String callingPackage) {
Amit Mahajanf2509742019-10-07 16:20:43 -07007610 enforceReadPrivilegedPermission("Needs READ_PRIVILEGED_PHONE_STATE for "
7611 + "isDataEnabledForApn");
Malcolm Chendc8c10e2019-04-10 18:25:07 -07007612
7613 // Now that all security checks passes, perform the operation as ourselves.
7614 final long identity = Binder.clearCallingIdentity();
7615 try {
7616 Phone phone = getPhone(subId);
7617 if (phone == null) return false;
7618
Jack Yu41407ee2019-05-13 16:54:09 -07007619 boolean isMetered = ApnSettingUtils.isMeteredApnType(apnType, phone);
Malcolm Chene5ad5792019-04-18 13:51:02 -07007620 return !isMetered || phone.getDataEnabledSettings().isDataEnabled(apnType);
7621 } finally {
7622 Binder.restoreCallingIdentity(identity);
7623 }
7624 }
7625
7626 @Override
Jack Yu41407ee2019-05-13 16:54:09 -07007627 public boolean isApnMetered(@ApnType int apnType, int subId) {
Malcolm Chene5ad5792019-04-18 13:51:02 -07007628 enforceReadPrivilegedPermission("isApnMetered");
7629
7630 // Now that all security checks passes, perform the operation as ourselves.
7631 final long identity = Binder.clearCallingIdentity();
7632 try {
7633 Phone phone = getPhone(subId);
7634 if (phone == null) return true; // By default return true.
7635
Jack Yu41407ee2019-05-13 16:54:09 -07007636 return ApnSettingUtils.isMeteredApnType(apnType, phone);
Malcolm Chendc8c10e2019-04-10 18:25:07 -07007637 } finally {
7638 Binder.restoreCallingIdentity(identity);
7639 }
7640 }
Brad Ebingera63db5f2019-04-23 16:31:13 -07007641
7642 @Override
7643 public void enqueueSmsPickResult(String callingPackage, IIntegerConsumer pendingSubIdResult) {
7644 SmsPermissions permissions = new SmsPermissions(getDefaultPhone(), mApp,
7645 (AppOpsManager) mApp.getSystemService(Context.APP_OPS_SERVICE));
7646 if (!permissions.checkCallingCanSendSms(callingPackage, "Sending message")) {
7647 throw new SecurityException("Requires SEND_SMS permission to perform this operation");
7648 }
7649 PickSmsSubscriptionActivity.addPendingResult(pendingSubIdResult);
7650 Intent intent = new Intent();
7651 intent.setClass(mApp, PickSmsSubscriptionActivity.class);
7652 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
7653 // Bring up choose default SMS subscription dialog right now
7654 intent.putExtra(PickSmsSubscriptionActivity.DIALOG_TYPE_KEY,
7655 PickSmsSubscriptionActivity.SMS_PICK_FOR_MESSAGE);
7656 mApp.startActivity(intent);
7657 }
chen xud5ca2d52019-05-28 15:20:57 -07007658
7659 @Override
7660 public String getMmsUAProfUrl(int subId) {
7661 //TODO investigate if this API should require proper permission check in R b/133791609
7662 final long identity = Binder.clearCallingIdentity();
7663 try {
7664 return SubscriptionManager.getResourcesForSubId(getDefaultPhone().getContext(), subId)
7665 .getString(com.android.internal.R.string.config_mms_user_agent_profile_url);
7666 } finally {
7667 Binder.restoreCallingIdentity(identity);
7668 }
7669 }
7670
7671 @Override
7672 public String getMmsUserAgent(int subId) {
7673 //TODO investigate if this API should require proper permission check in R b/133791609
7674 final long identity = Binder.clearCallingIdentity();
7675 try {
7676 return SubscriptionManager.getResourcesForSubId(getDefaultPhone().getContext(), subId)
7677 .getString(com.android.internal.R.string.config_mms_user_agent);
7678 } finally {
7679 Binder.restoreCallingIdentity(identity);
7680 }
7681 }
Jack Yub07d4972019-05-28 16:12:25 -07007682
7683 @Override
7684 public boolean setDataAllowedDuringVoiceCall(int subId, boolean allow) {
7685 enforceModifyPermission();
7686
7687 // Now that all security checks passes, perform the operation as ourselves.
7688 final long identity = Binder.clearCallingIdentity();
7689 try {
7690 Phone phone = getPhone(subId);
7691 if (phone == null) return false;
7692
7693 return phone.getDataEnabledSettings().setAllowDataDuringVoiceCall(allow);
7694 } finally {
7695 Binder.restoreCallingIdentity(identity);
7696 }
7697 }
7698
7699 @Override
7700 public boolean isDataAllowedInVoiceCall(int subId) {
7701 enforceReadPrivilegedPermission("isDataAllowedInVoiceCall");
7702
7703 // Now that all security checks passes, perform the operation as ourselves.
7704 final long identity = Binder.clearCallingIdentity();
7705 try {
7706 Phone phone = getPhone(subId);
7707 if (phone == null) return false;
7708
7709 return phone.getDataEnabledSettings().isDataAllowedInVoiceCall();
7710 } finally {
7711 Binder.restoreCallingIdentity(identity);
7712 }
7713 }
Tyler Gunn7bcdc742019-10-04 15:56:59 -07007714
7715 /**
7716 * Updates whether conference event pacakge handling is enabled.
7717 * @param isCepEnabled {@code true} if CEP handling is enabled (default), or {@code false}
7718 * otherwise.
7719 */
7720 @Override
7721 public void setCepEnabled(boolean isCepEnabled) {
7722 TelephonyPermissions.enforceShellOnly(Binder.getCallingUid(), "setCepEnabled");
7723
7724 final long identity = Binder.clearCallingIdentity();
7725 try {
7726 Rlog.i(LOG_TAG, "setCepEnabled isCepEnabled=" + isCepEnabled);
7727 for (Phone phone : PhoneFactory.getPhones()) {
7728 Phone defaultPhone = phone.getImsPhone();
7729 if (defaultPhone != null && defaultPhone.getPhoneType() == PHONE_TYPE_IMS) {
7730 ImsPhone imsPhone = (ImsPhone) defaultPhone;
7731 ImsPhoneCallTracker imsPhoneCallTracker =
7732 (ImsPhoneCallTracker) imsPhone.getCallTracker();
7733 imsPhoneCallTracker.setConferenceEventPackageEnabled(isCepEnabled);
7734 Rlog.i(LOG_TAG, "setCepEnabled isCepEnabled=" + isCepEnabled + ", for imsPhone "
7735 + imsPhone.getMsisdn());
7736 }
7737 }
7738 } finally {
7739 Binder.restoreCallingIdentity(identity);
7740 }
7741 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07007742}