blob: e28c521a242fb6530cd173db486ef6ceff563bdd [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
Ta-wei Yen87c49842016-05-13 21:19:52 -070021import static com.android.internal.telephony.PhoneConstants.SUBSCRIPTION_KEY;
22
Ta-wei Yen30a69c82016-12-27 14:52:32 -080023import android.Manifest.permission;
Tyler Gunn327a9722019-04-03 15:28:53 -070024import android.annotation.Nullable;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070025import android.app.AppOpsManager;
Ta-wei Yen30a69c82016-12-27 14:52:32 -080026import android.app.PendingIntent;
Sailesh Nepalbd76e4e2013-10-27 13:59:44 -070027import android.content.ComponentName;
Amit Mahajan7dbbd822019-03-13 17:33:47 -070028import android.content.ContentResolver;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070029import android.content.Context;
30import android.content.Intent;
Derek Tan97ebb422014-09-05 16:55:38 -070031import android.content.SharedPreferences;
Nathan Harold31d7ff32018-10-15 20:20:30 -070032import android.content.pm.ApplicationInfo;
Derek Tan740e1672017-06-27 14:56:27 -070033import android.content.pm.ComponentInfo;
Amith Yamasani6e118872016-02-19 12:53:51 -080034import android.content.pm.PackageInfo;
Shishir Agrawal60f9c952014-06-23 12:00:43 -070035import android.content.pm.PackageManager;
Jack Yu84291ec2017-05-26 16:07:50 -070036import android.net.NetworkStats;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070037import android.net.Uri;
38import android.os.AsyncResult;
39import android.os.Binder;
Hall Liuf19c44f2018-11-27 14:38:17 -080040import android.os.Build;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070041import android.os.Bundle;
42import android.os.Handler;
yinxu504e1392017-04-12 16:03:22 -070043import android.os.IBinder;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070044import android.os.Looper;
45import android.os.Message;
yinxu504e1392017-04-12 16:03:22 -070046import android.os.Messenger;
Tyler Gunn65d45c22017-06-05 11:22:26 -070047import android.os.PersistableBundle;
Brad Ebinger5f64b052017-12-14 14:26:15 -080048import android.os.RemoteException;
Adam Lesinski903a54c2016-04-11 14:49:52 -070049import android.os.ResultReceiver;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070050import android.os.ServiceManager;
Brad Ebingerdac2f002018-04-03 15:17:52 -070051import android.os.ShellCallback;
Pengquan Meng85728fb2018-03-12 16:31:21 -070052import android.os.SystemProperties;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070053import android.os.UserHandle;
Stuart Scott981d8582015-04-21 14:09:50 -070054import android.os.UserManager;
Sooraj Sasindran9a909312016-07-18 11:57:25 -070055import android.os.WorkSource;
Derek Tan97ebb422014-09-05 16:55:38 -070056import android.preference.PreferenceManager;
Ihab Awadf2177b72013-11-25 13:33:23 -080057import android.provider.Settings;
Amit Mahajan7dbbd822019-03-13 17:33:47 -070058import android.provider.Telephony;
Santos Cordon7a1885b2015-02-03 11:15:19 -080059import android.telecom.PhoneAccount;
Nancy Chen31f9ba12016-01-06 11:42:12 -080060import android.telecom.PhoneAccountHandle;
Andrew Lee9431b832015-03-09 18:46:45 -070061import android.telecom.TelecomManager;
Junda Liu12f7d802015-05-01 12:06:44 -070062import android.telephony.CarrierConfigManager;
Michele Berionne0963c862018-11-27 18:57:59 -080063import android.telephony.CarrierRestrictionRules;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070064import android.telephony.CellInfo;
Nathan Haroldf180aac2018-06-01 18:43:55 -070065import android.telephony.CellInfoGsm;
66import android.telephony.CellInfoWcdma;
Nathan Harold3ff88932018-08-14 10:19:49 -070067import android.telephony.CellLocation;
Sooraj Sasindran9a909312016-07-18 11:57:25 -070068import android.telephony.ClientRequestStats;
Nathan Haroldfa8da0f2018-09-27 18:51:29 -070069import android.telephony.ICellInfoCallback;
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -070070import android.telephony.IccOpenLogicalChannelResponse;
Hall Liu1aa510f2017-11-22 17:40:08 -080071import android.telephony.LocationAccessPolicy;
Ta-wei Yen87c49842016-05-13 21:19:52 -070072import android.telephony.ModemActivityInfo;
Jake Hambye994d462014-02-03 13:10:13 -080073import android.telephony.NeighboringCellInfo;
yinxu504e1392017-04-12 16:03:22 -070074import android.telephony.NetworkScanRequest;
Michelebcb01bc2019-02-04 11:36:23 -080075import android.telephony.PhoneCapability;
Hall Liud892bec2018-11-30 14:51:45 -080076import android.telephony.PhoneNumberRange;
Wink Saville5d475dd2014-10-17 15:00:58 -070077import android.telephony.RadioAccessFamily;
Hall Liub2ac8ef2019-02-28 15:56:23 -080078import android.telephony.RadioAccessSpecifier;
Tyler Gunn65d45c22017-06-05 11:22:26 -070079import android.telephony.Rlog;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070080import android.telephony.ServiceState;
Nathan Harold46b42aa2017-03-10 19:38:22 -080081import android.telephony.SignalStrength;
Jack Yu84291ec2017-05-26 16:07:50 -070082import android.telephony.SmsManager;
Wink Saville0f3b5fc2014-11-11 08:40:49 -080083import android.telephony.SubscriptionInfo;
Jeff Sharkey85190e62014-12-05 09:40:12 -080084import android.telephony.SubscriptionManager;
Sanket Padawe99ef1e32016-05-18 16:12:33 -070085import android.telephony.TelephonyHistogram;
Ta-wei Yenb6929602016-05-24 15:48:27 -070086import android.telephony.TelephonyManager;
Hall Liub2ac8ef2019-02-28 15:56:23 -080087import android.telephony.TelephonyScanManager;
Jordan Liu5aa07002018-12-18 15:44:48 -080088import android.telephony.UiccCardInfo;
Holly Jiuyu Sun01c47ad2018-01-24 17:56:33 +000089import android.telephony.UiccSlotInfo;
Tyler Gunn65d45c22017-06-05 11:22:26 -070090import android.telephony.UssdResponse;
Ta-wei Yenb6929602016-05-24 15:48:27 -070091import android.telephony.VisualVoicemailSmsFilterSettings;
Nathan Harold3ff88932018-08-14 10:19:49 -070092import android.telephony.cdma.CdmaCellLocation;
Jack Yu311536f2018-11-26 11:20:48 -080093import android.telephony.data.ApnSetting;
calvinpaneed9ae82018-11-01 19:43:06 +080094import android.telephony.emergency.EmergencyNumber;
Nathan Harold3ff88932018-08-14 10:19:49 -070095import android.telephony.gsm.GsmCellLocation;
Brad Ebinger1c8542e2019-01-14 13:43:14 -080096import android.telephony.ims.ProvisioningManager;
Brad Ebinger4c460712018-10-01 10:40:55 -070097import android.telephony.ims.aidl.IImsCapabilityCallback;
Brad Ebinger22bc3e42018-01-16 09:39:35 -080098import android.telephony.ims.aidl.IImsConfig;
Brad Ebingerdf5b4f02018-10-31 11:24:17 -070099import android.telephony.ims.aidl.IImsConfigCallback;
Brad Ebinger22bc3e42018-01-16 09:39:35 -0800100import android.telephony.ims.aidl.IImsMmTelFeature;
101import android.telephony.ims.aidl.IImsRcsFeature;
102import android.telephony.ims.aidl.IImsRegistration;
Brad Ebinger4c460712018-10-01 10:40:55 -0700103import android.telephony.ims.aidl.IImsRegistrationCallback;
Brad Ebinger1c8542e2019-01-14 13:43:14 -0800104import android.telephony.ims.feature.MmTelFeature;
105import android.telephony.ims.stub.ImsConfigImplBase;
Brad Ebinger1f2b5082018-02-08 16:11:32 -0800106import android.telephony.ims.stub.ImsRegistrationImplBase;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700107import android.text.TextUtils;
Jeff Sharkey85190e62014-12-05 09:40:12 -0800108import android.util.ArraySet;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700109import android.util.Log;
Jake Hambye994d462014-02-03 13:10:13 -0800110import android.util.Pair;
Jeff Sharkey85190e62014-12-05 09:40:12 -0800111import android.util.Slog;
Ta-wei Yen30a69c82016-12-27 14:52:32 -0800112
Brad Ebinger4c460712018-10-01 10:40:55 -0700113import com.android.ims.ImsException;
Andrew Lee312e8172014-10-23 17:01:36 -0700114import com.android.ims.ImsManager;
Brad Ebinger34bef922017-11-09 10:27:08 -0800115import com.android.ims.internal.IImsServiceFeatureCallback;
Shishir Agrawal566b7612013-10-28 14:41:00 -0700116import com.android.internal.telephony.CallManager;
Tyler Gunn52dcf772017-04-26 11:30:31 -0700117import com.android.internal.telephony.CallStateException;
pkanwar79ec0542017-07-31 14:10:01 -0700118import com.android.internal.telephony.CarrierInfoManager;
chen xu02581692018-11-11 19:03:44 -0800119import com.android.internal.telephony.CarrierResolver;
Shishir Agrawal302c8692015-06-19 13:49:39 -0700120import com.android.internal.telephony.CellNetworkScanResult;
Shishir Agrawal566b7612013-10-28 14:41:00 -0700121import com.android.internal.telephony.CommandException;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700122import com.android.internal.telephony.DefaultPhoneNotifier;
Nathan Harold48d6fd52019-02-06 19:01:40 -0800123import com.android.internal.telephony.HalVersion;
Hall Liud892bec2018-11-30 14:51:45 -0800124import com.android.internal.telephony.INumberVerificationCallback;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700125import com.android.internal.telephony.ITelephony;
Jake Hambye994d462014-02-03 13:10:13 -0800126import com.android.internal.telephony.IccCard;
Jack Yu5f7092c2018-04-13 14:05:37 -0700127import com.android.internal.telephony.LocaleTracker;
Narayan Kamath1c496c22015-04-16 14:40:19 +0100128import com.android.internal.telephony.MccTable;
yinxub1bed742017-04-17 11:45:04 -0700129import com.android.internal.telephony.NetworkScanRequestTracker;
Shishir Agrawal302c8692015-06-19 13:49:39 -0700130import com.android.internal.telephony.OperatorInfo;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700131import com.android.internal.telephony.Phone;
Malcolm Chenf144d942018-08-14 16:00:53 -0700132import com.android.internal.telephony.PhoneConfigurationManager;
Nathan Harolda667c152016-12-14 11:27:20 -0800133import com.android.internal.telephony.PhoneConstantConversions;
Ta-wei Yen87c49842016-05-13 21:19:52 -0700134import com.android.internal.telephony.PhoneConstants;
Wink Saville36469e72014-06-11 15:17:00 -0700135import com.android.internal.telephony.PhoneFactory;
Wink Saville5d475dd2014-10-17 15:00:58 -0700136import com.android.internal.telephony.ProxyController;
Sanket Padawe99ef1e32016-05-18 16:12:33 -0700137import com.android.internal.telephony.RIL;
Svet Ganovb320e182015-04-16 12:30:10 -0700138import com.android.internal.telephony.RILConstants;
Jack Yu5f7092c2018-04-13 14:05:37 -0700139import com.android.internal.telephony.ServiceStateTracker;
sqian2fff4a32018-11-05 14:18:37 -0800140import com.android.internal.telephony.SmsApplication;
141import com.android.internal.telephony.SmsApplication.SmsApplicationData;
Wink Savilleac1bdfd2014-11-20 23:04:44 -0800142import com.android.internal.telephony.SubscriptionController;
Jeff Davidson7e17e312018-02-13 18:17:36 -0800143import com.android.internal.telephony.TelephonyPermissions;
Malcolm Chen460810d2019-04-10 18:25:07 -0700144import com.android.internal.telephony.dataconnection.ApnSettingUtils;
sqian9d4df8b2019-01-15 18:32:07 -0800145import com.android.internal.telephony.emergency.EmergencyNumberTracker;
Derek Tan740e1672017-06-27 14:56:27 -0700146import com.android.internal.telephony.euicc.EuiccConnector;
Brad Ebingereb160e22019-01-23 15:06:19 -0800147import com.android.internal.telephony.ims.ImsResolver;
Pengquan Meng92d253b2019-02-06 11:12:53 -0800148import com.android.internal.telephony.metrics.TelephonyMetrics;
Shishir Agrawal566b7612013-10-28 14:41:00 -0700149import com.android.internal.telephony.uicc.IccIoResult;
150import com.android.internal.telephony.uicc.IccUtils;
Nathan Haroldb3014052017-01-25 15:57:32 -0800151import com.android.internal.telephony.uicc.SIMRecords;
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700152import com.android.internal.telephony.uicc.UiccCard;
Nathan Haroldb3014052017-01-25 15:57:32 -0800153import com.android.internal.telephony.uicc.UiccCardApplication;
Shishir Agrawal566b7612013-10-28 14:41:00 -0700154import com.android.internal.telephony.uicc.UiccController;
Jeff Davidson7e17e312018-02-13 18:17:36 -0800155import com.android.internal.telephony.uicc.UiccProfile;
Holly Jiuyu Sun01c47ad2018-01-24 17:56:33 +0000156import com.android.internal.telephony.uicc.UiccSlot;
fionaxu7ed723d2017-05-30 18:58:54 -0700157import com.android.internal.telephony.util.VoicemailNotificationSettingsUtil;
Jake Hambye994d462014-02-03 13:10:13 -0800158import com.android.internal.util.HexDump;
Ta-wei Yenc9df0432017-04-17 17:09:07 -0700159import com.android.phone.vvm.PhoneAccountHandleConverter;
Ta-wei Yen527a9c02017-01-06 15:29:25 -0800160import com.android.phone.vvm.RemoteVvmTaskManager;
Ta-wei Yenc9df0432017-04-17 17:09:07 -0700161import com.android.phone.vvm.VisualVoicemailSettingsUtil;
Ta-wei Yenc8905312017-03-28 11:14:45 -0700162import com.android.phone.vvm.VisualVoicemailSmsFilterConfig;
Ta-wei Yen30a69c82016-12-27 14:52:32 -0800163
Ta-wei Yenc236d6b2016-06-21 13:33:12 -0700164import java.io.FileDescriptor;
165import java.io.PrintWriter;
Ta-wei Yen30a69c82016-12-27 14:52:32 -0800166import java.nio.charset.StandardCharsets;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700167import java.util.ArrayList;
Jeff Sharkey85190e62014-12-05 09:40:12 -0800168import java.util.Arrays;
sqian2fff4a32018-11-05 14:18:37 -0800169import java.util.Collection;
sqian03bca152018-12-05 18:48:28 -0800170import java.util.HashMap;
sqian9d4df8b2019-01-15 18:32:07 -0800171import java.util.HashSet;
Jake Hambye994d462014-02-03 13:10:13 -0800172import java.util.List;
Narayan Kamath1c496c22015-04-16 14:40:19 +0100173import java.util.Locale;
Jeff Sharkey85190e62014-12-05 09:40:12 -0800174import java.util.Map;
sqian9d4df8b2019-01-15 18:32:07 -0800175import java.util.Set;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700176
177/**
178 * Implementation of the ITelephony interface.
179 */
Santos Cordon117fee72014-05-16 17:56:12 -0700180public class PhoneInterfaceManager extends ITelephony.Stub {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700181 private static final String LOG_TAG = "PhoneInterfaceManager";
182 private static final boolean DBG = (PhoneGlobals.DBG_LEVEL >= 2);
183 private static final boolean DBG_LOC = false;
Jeff Sharkey85190e62014-12-05 09:40:12 -0800184 private static final boolean DBG_MERGE = false;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700185
186 // Message codes used with mMainThreadHandler
187 private static final int CMD_HANDLE_PIN_MMI = 1;
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700188 private static final int CMD_TRANSMIT_APDU_LOGICAL_CHANNEL = 7;
189 private static final int EVENT_TRANSMIT_APDU_LOGICAL_CHANNEL_DONE = 8;
Shishir Agrawal566b7612013-10-28 14:41:00 -0700190 private static final int CMD_OPEN_CHANNEL = 9;
191 private static final int EVENT_OPEN_CHANNEL_DONE = 10;
192 private static final int CMD_CLOSE_CHANNEL = 11;
193 private static final int EVENT_CLOSE_CHANNEL_DONE = 12;
Jake Hambye994d462014-02-03 13:10:13 -0800194 private static final int CMD_NV_READ_ITEM = 13;
195 private static final int EVENT_NV_READ_ITEM_DONE = 14;
196 private static final int CMD_NV_WRITE_ITEM = 15;
197 private static final int EVENT_NV_WRITE_ITEM_DONE = 16;
198 private static final int CMD_NV_WRITE_CDMA_PRL = 17;
199 private static final int EVENT_NV_WRITE_CDMA_PRL_DONE = 18;
chen xu1cc0abe2018-10-26 17:39:23 -0700200 private static final int CMD_RESET_MODEM_CONFIG = 19;
201 private static final int EVENT_RESET_MODEM_CONFIG_DONE = 20;
Jake Hamby7c27be32014-03-03 13:25:59 -0800202 private static final int CMD_GET_PREFERRED_NETWORK_TYPE = 21;
203 private static final int EVENT_GET_PREFERRED_NETWORK_TYPE_DONE = 22;
204 private static final int CMD_SET_PREFERRED_NETWORK_TYPE = 23;
205 private static final int EVENT_SET_PREFERRED_NETWORK_TYPE_DONE = 24;
Sailesh Nepal35b59452014-03-06 09:26:56 -0800206 private static final int CMD_SEND_ENVELOPE = 25;
207 private static final int EVENT_SEND_ENVELOPE_DONE = 26;
Shuo Qian850e4d6a2018-04-25 21:02:08 +0000208 private static final int CMD_INVOKE_OEM_RIL_REQUEST_RAW = 27;
209 private static final int EVENT_INVOKE_OEM_RIL_REQUEST_RAW_DONE = 28;
Derek Tan6b088ee2014-09-05 14:15:18 -0700210 private static final int CMD_TRANSMIT_APDU_BASIC_CHANNEL = 29;
211 private static final int EVENT_TRANSMIT_APDU_BASIC_CHANNEL_DONE = 30;
212 private static final int CMD_EXCHANGE_SIM_IO = 31;
213 private static final int EVENT_EXCHANGE_SIM_IO_DONE = 32;
Shishir Agrawal76d5da92014-11-09 16:17:25 -0800214 private static final int CMD_SET_VOICEMAIL_NUMBER = 33;
215 private static final int EVENT_SET_VOICEMAIL_NUMBER_DONE = 34;
Stuart Scott54788802015-03-30 13:18:01 -0700216 private static final int CMD_SET_NETWORK_SELECTION_MODE_AUTOMATIC = 35;
217 private static final int EVENT_SET_NETWORK_SELECTION_MODE_AUTOMATIC_DONE = 36;
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -0700218 private static final int CMD_GET_MODEM_ACTIVITY_INFO = 37;
219 private static final int EVENT_GET_MODEM_ACTIVITY_INFO_DONE = 38;
Shishir Agrawal302c8692015-06-19 13:49:39 -0700220 private static final int CMD_PERFORM_NETWORK_SCAN = 39;
221 private static final int EVENT_PERFORM_NETWORK_SCAN_DONE = 40;
222 private static final int CMD_SET_NETWORK_SELECTION_MODE_MANUAL = 41;
223 private static final int EVENT_SET_NETWORK_SELECTION_MODE_MANUAL_DONE = 42;
Meng Wang1a7c35a2016-05-05 20:56:15 -0700224 private static final int CMD_SET_ALLOWED_CARRIERS = 43;
225 private static final int EVENT_SET_ALLOWED_CARRIERS_DONE = 44;
226 private static final int CMD_GET_ALLOWED_CARRIERS = 45;
227 private static final int EVENT_GET_ALLOWED_CARRIERS_DONE = 46;
pkanwar32d516d2016-10-14 19:37:38 -0700228 private static final int CMD_HANDLE_USSD_REQUEST = 47;
Nathan Haroldb3014052017-01-25 15:57:32 -0800229 private static final int CMD_GET_FORBIDDEN_PLMNS = 48;
230 private static final int EVENT_GET_FORBIDDEN_PLMNS_DONE = 49;
Holly Jiuyu Sun01c47ad2018-01-24 17:56:33 +0000231 private static final int CMD_SWITCH_SLOTS = 50;
232 private static final int EVENT_SWITCH_SLOTS_DONE = 51;
Pengquan Meng0c05b502018-09-06 09:59:22 -0700233 private static final int CMD_GET_NETWORK_SELECTION_MODE = 52;
234 private static final int EVENT_GET_NETWORK_SELECTION_MODE_DONE = 53;
235 private static final int CMD_GET_CDMA_ROAMING_MODE = 54;
236 private static final int EVENT_GET_CDMA_ROAMING_MODE_DONE = 55;
237 private static final int CMD_SET_CDMA_ROAMING_MODE = 56;
238 private static final int EVENT_SET_CDMA_ROAMING_MODE_DONE = 57;
239 private static final int CMD_SET_CDMA_SUBSCRIPTION_MODE = 58;
240 private static final int EVENT_SET_CDMA_SUBSCRIPTION_MODE_DONE = 59;
Nathan Harold3ff88932018-08-14 10:19:49 -0700241 private static final int CMD_GET_ALL_CELL_INFO = 60;
242 private static final int EVENT_GET_ALL_CELL_INFO_DONE = 61;
243 private static final int CMD_GET_CELL_LOCATION = 62;
244 private static final int EVENT_GET_CELL_LOCATION_DONE = 63;
chen xu1cc0abe2018-10-26 17:39:23 -0700245 private static final int CMD_MODEM_REBOOT = 64;
246 private static final int EVENT_CMD_MODEM_REBOOT_DONE = 65;
Nathan Haroldfa8da0f2018-09-27 18:51:29 -0700247 private static final int CMD_REQUEST_CELL_INFO_UPDATE = 66;
248 private static final int EVENT_REQUEST_CELL_INFO_UPDATE_DONE = 67;
Malcolm Chen509b5b72019-01-15 20:22:16 -0800249 private static final int CMD_REQUEST_ENABLE_MODEM = 68;
250 private static final int EVENT_ENABLE_MODEM_DONE = 69;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700251
Holly Jiuyu Sun1cc2d552018-01-26 15:51:16 -0800252 // Parameters of select command.
253 private static final int SELECT_COMMAND = 0xA4;
254 private static final int SELECT_P1 = 0x04;
255 private static final int SELECT_P2 = 0;
256 private static final int SELECT_P3 = 0x10;
257
Pengquan Meng85728fb2018-03-12 16:31:21 -0700258 private static final String DEFAULT_NETWORK_MODE_PROPERTY_NAME = "ro.telephony.default_network";
259 private static final String DEFAULT_DATA_ROAMING_PROPERTY_NAME = "ro.com.android.dataroaming";
260 private static final String DEFAULT_MOBILE_DATA_PROPERTY_NAME = "ro.com.android.mobiledata";
261
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700262 /** The singleton instance. */
263 private static PhoneInterfaceManager sInstance;
264
Wink Saville3ab207e2014-11-20 13:07:20 -0800265 private PhoneGlobals mApp;
Wink Saville3ab207e2014-11-20 13:07:20 -0800266 private CallManager mCM;
Stuart Scott981d8582015-04-21 14:09:50 -0700267 private UserManager mUserManager;
Wink Saville3ab207e2014-11-20 13:07:20 -0800268 private AppOpsManager mAppOps;
269 private MainThreadHandler mMainThreadHandler;
Wink Savilleac1bdfd2014-11-20 23:04:44 -0800270 private SubscriptionController mSubscriptionController;
Wink Saville3ab207e2014-11-20 13:07:20 -0800271 private SharedPreferences mTelephonySharedPreferences;
Malcolm Chenf144d942018-08-14 16:00:53 -0700272 private PhoneConfigurationManager mPhoneConfigurationManager;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700273
Derek Tan97ebb422014-09-05 16:55:38 -0700274 private static final String PREF_CARRIERS_ALPHATAG_PREFIX = "carrier_alphtag_";
275 private static final String PREF_CARRIERS_NUMBER_PREFIX = "carrier_number_";
Jeff Sharkey85190e62014-12-05 09:40:12 -0800276 private static final String PREF_CARRIERS_SUBSCRIBER_PREFIX = "carrier_subscriber_";
Brad Ebinger1c8542e2019-01-14 13:43:14 -0800277 private static final String PREF_PROVISION_IMS_MMTEL_PREFIX = "provision_ims_mmtel_";
Derek Tan89e89d42014-07-08 17:00:10 -0700278
Micheled3107c52018-12-21 15:00:11 -0800279 // String to store multi SIM allowed
280 private static final String PREF_MULTI_SIM_RESTRICTED = "multisim_restricted";
281
Derek Tan740e1672017-06-27 14:56:27 -0700282 // The AID of ISD-R.
283 private static final String ISDR_AID = "A0000005591010FFFFFFFF8900000100";
284
yinxub1bed742017-04-17 11:45:04 -0700285 private NetworkScanRequestTracker mNetworkScanRequestTracker;
286
David Kelly5e06a7f2018-03-12 14:10:59 +0000287 private static final int TYPE_ALLOCATION_CODE_LENGTH = 8;
288 private static final int MANUFACTURER_CODE_LENGTH = 8;
289
Derek Tan89e89d42014-07-08 17:00:10 -0700290 /**
Shishir Agrawal566b7612013-10-28 14:41:00 -0700291 * A request object to use for transmitting data to an ICC.
292 */
293 private static final class IccAPDUArgument {
294 public int channel, cla, command, p1, p2, p3;
295 public String data;
296
297 public IccAPDUArgument(int channel, int cla, int command,
298 int p1, int p2, int p3, String data) {
299 this.channel = channel;
300 this.cla = cla;
301 this.command = command;
302 this.p1 = p1;
303 this.p2 = p2;
304 this.p3 = p3;
305 this.data = data;
306 }
307 }
308
309 /**
Shishir Agrawal77ba3172015-09-10 14:50:19 -0700310 * A request object to use for transmitting data to an ICC.
311 */
312 private static final class ManualNetworkSelectionArgument {
313 public OperatorInfo operatorInfo;
314 public boolean persistSelection;
315
316 public ManualNetworkSelectionArgument(OperatorInfo operatorInfo, boolean persistSelection) {
317 this.operatorInfo = operatorInfo;
318 this.persistSelection = persistSelection;
319 }
320 }
321
322 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700323 * A request object for use with {@link MainThreadHandler}. Requesters should wait() on the
324 * request after sending. The main thread will notify the request when it is complete.
325 */
326 private static final class MainThreadRequest {
327 /** The argument to use for the request */
328 public Object argument;
329 /** The result of the request that is run on the main thread */
330 public Object result;
Sanket Padawe56e75a32016-02-08 12:18:19 -0800331 // The subscriber id that this request applies to. Defaults to
332 // SubscriptionManager.INVALID_SUBSCRIPTION_ID
333 public Integer subId = SubscriptionManager.INVALID_SUBSCRIPTION_ID;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700334
Nathan Harold92bed182018-10-12 18:16:49 -0700335 // In cases where subId is unavailable, the caller needs to specify the phone.
336 public Phone phone;
337
vagdevie435a3e2018-08-15 16:01:53 -0700338 public WorkSource workSource;
339
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700340 public MainThreadRequest(Object argument) {
341 this.argument = argument;
342 }
Shishir Agrawal76d5da92014-11-09 16:17:25 -0800343
Nathan Harold92bed182018-10-12 18:16:49 -0700344 MainThreadRequest(Object argument, Phone phone, WorkSource workSource) {
345 this.argument = argument;
346 if (phone != null) {
347 this.phone = phone;
348 }
349 this.workSource = workSource;
350 }
351
vagdevie435a3e2018-08-15 16:01:53 -0700352 MainThreadRequest(Object argument, Integer subId, WorkSource workSource) {
Shishir Agrawal76d5da92014-11-09 16:17:25 -0800353 this.argument = argument;
Sanket Padawe56e75a32016-02-08 12:18:19 -0800354 if (subId != null) {
355 this.subId = subId;
356 }
vagdevie435a3e2018-08-15 16:01:53 -0700357 this.workSource = workSource;
Shishir Agrawal76d5da92014-11-09 16:17:25 -0800358 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700359 }
360
Sailesh Nepalcc0375f2013-11-13 09:15:18 -0800361 private static final class IncomingThirdPartyCallArgs {
362 public final ComponentName component;
363 public final String callId;
364 public final String callerDisplayName;
365
366 public IncomingThirdPartyCallArgs(ComponentName component, String callId,
367 String callerDisplayName) {
368 this.component = component;
369 this.callId = callId;
370 this.callerDisplayName = callerDisplayName;
371 }
372 }
373
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700374 /**
375 * A handler that processes messages on the main thread in the phone process. Since many
376 * of the Phone calls are not thread safe this is needed to shuttle the requests from the
377 * inbound binder threads to the main thread in the phone process. The Binder thread
378 * may provide a {@link MainThreadRequest} object in the msg.obj field that they are waiting
379 * on, which will be notified when the operation completes and will contain the result of the
380 * request.
381 *
382 * <p>If a MainThreadRequest object is provided in the msg.obj field,
383 * note that request.result must be set to something non-null for the calling thread to
384 * unblock.
385 */
386 private final class MainThreadHandler extends Handler {
387 @Override
388 public void handleMessage(Message msg) {
389 MainThreadRequest request;
390 Message onCompleted;
391 AsyncResult ar;
Shishir Agrawalc04d9752016-02-19 10:41:00 -0800392 UiccCard uiccCard;
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700393 IccAPDUArgument iccArgument;
Nazanin Bakhshi2f2cc692018-12-11 15:15:39 -0800394 final Phone defaultPhone = getDefaultPhone();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700395
396 switch (msg.what) {
Pengquan Meng0c05b502018-09-06 09:59:22 -0700397 case CMD_HANDLE_USSD_REQUEST: {
398 request = (MainThreadRequest) msg.obj;
399 final Phone phone = getPhoneFromRequest(request);
400 Pair<String, ResultReceiver> ussdObject = (Pair) request.argument;
401 String ussdRequest = ussdObject.first;
402 ResultReceiver wrappedCallback = ussdObject.second;
Tyler Gunn65d45c22017-06-05 11:22:26 -0700403
Pengquan Meng0c05b502018-09-06 09:59:22 -0700404 if (!isUssdApiAllowed(request.subId)) {
405 // Carrier does not support use of this API, return failure.
406 Rlog.w(LOG_TAG, "handleUssdRequest: carrier does not support USSD apis.");
407 UssdResponse response = new UssdResponse(ussdRequest, null);
408 Bundle returnData = new Bundle();
409 returnData.putParcelable(TelephonyManager.USSD_RESPONSE, response);
410 wrappedCallback.send(TelephonyManager.USSD_RETURN_FAILURE, returnData);
Tyler Gunn65d45c22017-06-05 11:22:26 -0700411
Pengquan Meng0c05b502018-09-06 09:59:22 -0700412 request.result = true;
413 notifyRequester(request);
414 return;
415 }
Tyler Gunn65d45c22017-06-05 11:22:26 -0700416
Pengquan Meng0c05b502018-09-06 09:59:22 -0700417 try {
418 request.result = phone != null
419 ? phone.handleUssdRequest(ussdRequest, wrappedCallback) : false;
420 } catch (CallStateException cse) {
421 request.result = false;
422 }
423 // Wake up the requesting thread
424 notifyRequester(request);
425 break;
pkanwar32d516d2016-10-14 19:37:38 -0700426 }
427
Yorke Lee716f67e2015-06-17 15:39:16 -0700428 case CMD_HANDLE_PIN_MMI: {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700429 request = (MainThreadRequest) msg.obj;
Yorke Lee716f67e2015-06-17 15:39:16 -0700430 final Phone phone = getPhoneFromRequest(request);
431 request.result = phone != null ?
432 getPhoneFromRequest(request).handlePinMmi((String) request.argument)
433 : false;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700434 // Wake up the requesting thread
Pengquan Meng0c05b502018-09-06 09:59:22 -0700435 notifyRequester(request);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700436 break;
Yorke Lee716f67e2015-06-17 15:39:16 -0700437 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700438
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700439 case CMD_TRANSMIT_APDU_LOGICAL_CHANNEL:
Shishir Agrawal566b7612013-10-28 14:41:00 -0700440 request = (MainThreadRequest) msg.obj;
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700441 iccArgument = (IccAPDUArgument) request.argument;
Shishir Agrawalc04d9752016-02-19 10:41:00 -0800442 uiccCard = getUiccCardFromRequest(request);
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700443 if (uiccCard == null) {
444 loge("iccTransmitApduLogicalChannel: No UICC");
445 request.result = new IccIoResult(0x6F, 0, (byte[])null);
Pengquan Meng0c05b502018-09-06 09:59:22 -0700446 notifyRequester(request);
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700447 } else {
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700448 onCompleted = obtainMessage(EVENT_TRANSMIT_APDU_LOGICAL_CHANNEL_DONE,
449 request);
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700450 uiccCard.iccTransmitApduLogicalChannel(
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700451 iccArgument.channel, iccArgument.cla, iccArgument.command,
452 iccArgument.p1, iccArgument.p2, iccArgument.p3, iccArgument.data,
Shishir Agrawal566b7612013-10-28 14:41:00 -0700453 onCompleted);
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700454 }
Shishir Agrawal566b7612013-10-28 14:41:00 -0700455 break;
456
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700457 case EVENT_TRANSMIT_APDU_LOGICAL_CHANNEL_DONE:
Shishir Agrawal566b7612013-10-28 14:41:00 -0700458 ar = (AsyncResult) msg.obj;
459 request = (MainThreadRequest) ar.userObj;
460 if (ar.exception == null && ar.result != null) {
461 request.result = ar.result;
462 } else {
463 request.result = new IccIoResult(0x6F, 0, (byte[])null);
464 if (ar.result == null) {
465 loge("iccTransmitApduLogicalChannel: Empty response");
Jake Hambye994d462014-02-03 13:10:13 -0800466 } else if (ar.exception instanceof CommandException) {
Shishir Agrawal566b7612013-10-28 14:41:00 -0700467 loge("iccTransmitApduLogicalChannel: CommandException: " +
Jake Hambye994d462014-02-03 13:10:13 -0800468 ar.exception);
Shishir Agrawal566b7612013-10-28 14:41:00 -0700469 } else {
470 loge("iccTransmitApduLogicalChannel: Unknown exception");
471 }
472 }
Pengquan Meng0c05b502018-09-06 09:59:22 -0700473 notifyRequester(request);
Shishir Agrawal566b7612013-10-28 14:41:00 -0700474 break;
475
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700476 case CMD_TRANSMIT_APDU_BASIC_CHANNEL:
477 request = (MainThreadRequest) msg.obj;
478 iccArgument = (IccAPDUArgument) request.argument;
Shishir Agrawalc04d9752016-02-19 10:41:00 -0800479 uiccCard = getUiccCardFromRequest(request);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700480 if (uiccCard == null) {
481 loge("iccTransmitApduBasicChannel: No UICC");
482 request.result = new IccIoResult(0x6F, 0, (byte[])null);
Pengquan Meng0c05b502018-09-06 09:59:22 -0700483 notifyRequester(request);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700484 } else {
485 onCompleted = obtainMessage(EVENT_TRANSMIT_APDU_BASIC_CHANNEL_DONE,
486 request);
487 uiccCard.iccTransmitApduBasicChannel(
488 iccArgument.cla, iccArgument.command, iccArgument.p1, iccArgument.p2,
489 iccArgument.p3, iccArgument.data, onCompleted);
490 }
491 break;
492
493 case EVENT_TRANSMIT_APDU_BASIC_CHANNEL_DONE:
494 ar = (AsyncResult) msg.obj;
495 request = (MainThreadRequest) ar.userObj;
496 if (ar.exception == null && ar.result != null) {
497 request.result = ar.result;
498 } else {
499 request.result = new IccIoResult(0x6F, 0, (byte[])null);
500 if (ar.result == null) {
501 loge("iccTransmitApduBasicChannel: Empty response");
502 } else if (ar.exception instanceof CommandException) {
503 loge("iccTransmitApduBasicChannel: CommandException: " +
504 ar.exception);
505 } else {
506 loge("iccTransmitApduBasicChannel: Unknown exception");
507 }
508 }
Pengquan Meng0c05b502018-09-06 09:59:22 -0700509 notifyRequester(request);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700510 break;
511
512 case CMD_EXCHANGE_SIM_IO:
513 request = (MainThreadRequest) msg.obj;
514 iccArgument = (IccAPDUArgument) request.argument;
Shishir Agrawalc04d9752016-02-19 10:41:00 -0800515 uiccCard = getUiccCardFromRequest(request);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700516 if (uiccCard == null) {
517 loge("iccExchangeSimIO: No UICC");
518 request.result = new IccIoResult(0x6F, 0, (byte[])null);
Pengquan Meng0c05b502018-09-06 09:59:22 -0700519 notifyRequester(request);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700520 } else {
521 onCompleted = obtainMessage(EVENT_EXCHANGE_SIM_IO_DONE,
522 request);
523 uiccCard.iccExchangeSimIO(iccArgument.cla, /* fileID */
524 iccArgument.command, iccArgument.p1, iccArgument.p2, iccArgument.p3,
525 iccArgument.data, onCompleted);
526 }
527 break;
528
529 case EVENT_EXCHANGE_SIM_IO_DONE:
530 ar = (AsyncResult) msg.obj;
531 request = (MainThreadRequest) ar.userObj;
532 if (ar.exception == null && ar.result != null) {
533 request.result = ar.result;
534 } else {
535 request.result = new IccIoResult(0x6f, 0, (byte[])null);
536 }
Pengquan Meng0c05b502018-09-06 09:59:22 -0700537 notifyRequester(request);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700538 break;
539
Derek Tan4d5e5c12014-02-04 11:54:58 -0800540 case CMD_SEND_ENVELOPE:
541 request = (MainThreadRequest) msg.obj;
Shishir Agrawalc04d9752016-02-19 10:41:00 -0800542 uiccCard = getUiccCardFromRequest(request);
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700543 if (uiccCard == null) {
544 loge("sendEnvelopeWithStatus: No UICC");
545 request.result = new IccIoResult(0x6F, 0, (byte[])null);
Pengquan Meng0c05b502018-09-06 09:59:22 -0700546 notifyRequester(request);
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700547 } else {
548 onCompleted = obtainMessage(EVENT_SEND_ENVELOPE_DONE, request);
549 uiccCard.sendEnvelopeWithStatus((String)request.argument, onCompleted);
550 }
Derek Tan4d5e5c12014-02-04 11:54:58 -0800551 break;
552
553 case EVENT_SEND_ENVELOPE_DONE:
554 ar = (AsyncResult) msg.obj;
555 request = (MainThreadRequest) ar.userObj;
Shishir Agrawal9f9877d2014-03-14 09:36:27 -0700556 if (ar.exception == null && ar.result != null) {
557 request.result = ar.result;
Derek Tan4d5e5c12014-02-04 11:54:58 -0800558 } else {
Shishir Agrawal9f9877d2014-03-14 09:36:27 -0700559 request.result = new IccIoResult(0x6F, 0, (byte[])null);
560 if (ar.result == null) {
561 loge("sendEnvelopeWithStatus: Empty response");
562 } else if (ar.exception instanceof CommandException) {
563 loge("sendEnvelopeWithStatus: CommandException: " +
564 ar.exception);
565 } else {
566 loge("sendEnvelopeWithStatus: exception:" + ar.exception);
567 }
Derek Tan4d5e5c12014-02-04 11:54:58 -0800568 }
Pengquan Meng0c05b502018-09-06 09:59:22 -0700569 notifyRequester(request);
Derek Tan4d5e5c12014-02-04 11:54:58 -0800570 break;
571
Shishir Agrawal566b7612013-10-28 14:41:00 -0700572 case CMD_OPEN_CHANNEL:
573 request = (MainThreadRequest) msg.obj;
Shishir Agrawalc04d9752016-02-19 10:41:00 -0800574 uiccCard = getUiccCardFromRequest(request);
Ajay Nambid7454d32015-12-03 13:50:00 -0800575 Pair<String, Integer> openChannelArgs = (Pair<String, Integer>) request.argument;
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700576 if (uiccCard == null) {
577 loge("iccOpenLogicalChannel: No UICC");
Shishir Agrawalfc0492a2016-02-17 11:15:33 -0800578 request.result = new IccOpenLogicalChannelResponse(-1,
579 IccOpenLogicalChannelResponse.STATUS_MISSING_RESOURCE, null);
Pengquan Meng0c05b502018-09-06 09:59:22 -0700580 notifyRequester(request);
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700581 } else {
582 onCompleted = obtainMessage(EVENT_OPEN_CHANNEL_DONE, request);
Ajay Nambid7454d32015-12-03 13:50:00 -0800583 uiccCard.iccOpenLogicalChannel(openChannelArgs.first,
584 openChannelArgs.second, onCompleted);
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700585 }
Shishir Agrawal566b7612013-10-28 14:41:00 -0700586 break;
587
588 case EVENT_OPEN_CHANNEL_DONE:
589 ar = (AsyncResult) msg.obj;
590 request = (MainThreadRequest) ar.userObj;
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700591 IccOpenLogicalChannelResponse openChannelResp;
Shishir Agrawal566b7612013-10-28 14:41:00 -0700592 if (ar.exception == null && ar.result != null) {
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700593 int[] result = (int[]) ar.result;
594 int channelId = result[0];
595 byte[] selectResponse = null;
596 if (result.length > 1) {
597 selectResponse = new byte[result.length - 1];
598 for (int i = 1; i < result.length; ++i) {
599 selectResponse[i - 1] = (byte) result[i];
600 }
601 }
602 openChannelResp = new IccOpenLogicalChannelResponse(channelId,
Shishir Agrawal527e8bf2014-08-25 08:54:56 -0700603 IccOpenLogicalChannelResponse.STATUS_NO_ERROR, selectResponse);
Shishir Agrawal566b7612013-10-28 14:41:00 -0700604 } else {
Shishir Agrawal566b7612013-10-28 14:41:00 -0700605 if (ar.result == null) {
606 loge("iccOpenLogicalChannel: Empty response");
Shishir Agrawal566b7612013-10-28 14:41:00 -0700607 }
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700608 if (ar.exception != null) {
609 loge("iccOpenLogicalChannel: Exception: " + ar.exception);
610 }
611
Shishir Agrawal527e8bf2014-08-25 08:54:56 -0700612 int errorCode = IccOpenLogicalChannelResponse.STATUS_UNKNOWN_ERROR;
Junda Liua754ba12015-05-20 01:17:52 -0700613 if (ar.exception instanceof CommandException) {
614 CommandException.Error error =
615 ((CommandException) (ar.exception)).getCommandError();
616 if (error == CommandException.Error.MISSING_RESOURCE) {
Shishir Agrawal527e8bf2014-08-25 08:54:56 -0700617 errorCode = IccOpenLogicalChannelResponse.STATUS_MISSING_RESOURCE;
Junda Liua754ba12015-05-20 01:17:52 -0700618 } else if (error == CommandException.Error.NO_SUCH_ELEMENT) {
Shishir Agrawal527e8bf2014-08-25 08:54:56 -0700619 errorCode = IccOpenLogicalChannelResponse.STATUS_NO_SUCH_ELEMENT;
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700620 }
621 }
622 openChannelResp = new IccOpenLogicalChannelResponse(
623 IccOpenLogicalChannelResponse.INVALID_CHANNEL, errorCode, null);
Shishir Agrawal566b7612013-10-28 14:41:00 -0700624 }
Shishir Agrawal82c8a462014-07-31 18:13:17 -0700625 request.result = openChannelResp;
Pengquan Meng0c05b502018-09-06 09:59:22 -0700626 notifyRequester(request);
Shishir Agrawal566b7612013-10-28 14:41:00 -0700627 break;
628
629 case CMD_CLOSE_CHANNEL:
630 request = (MainThreadRequest) msg.obj;
Shishir Agrawalc04d9752016-02-19 10:41:00 -0800631 uiccCard = getUiccCardFromRequest(request);
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700632 if (uiccCard == null) {
633 loge("iccCloseLogicalChannel: No UICC");
Yoshiaki Naka2e29d822016-09-02 19:27:39 +0900634 request.result = false;
Pengquan Meng0c05b502018-09-06 09:59:22 -0700635 notifyRequester(request);
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700636 } else {
637 onCompleted = obtainMessage(EVENT_CLOSE_CHANNEL_DONE, request);
638 uiccCard.iccCloseLogicalChannel((Integer) request.argument, onCompleted);
639 }
Shishir Agrawal566b7612013-10-28 14:41:00 -0700640 break;
641
642 case EVENT_CLOSE_CHANNEL_DONE:
Jake Hambye994d462014-02-03 13:10:13 -0800643 handleNullReturnEvent(msg, "iccCloseLogicalChannel");
644 break;
645
646 case CMD_NV_READ_ITEM:
647 request = (MainThreadRequest) msg.obj;
648 onCompleted = obtainMessage(EVENT_NV_READ_ITEM_DONE, request);
Nazanin Bakhshi2f2cc692018-12-11 15:15:39 -0800649 defaultPhone.nvReadItem((Integer) request.argument, onCompleted,
650 request.workSource);
Jake Hambye994d462014-02-03 13:10:13 -0800651 break;
652
653 case EVENT_NV_READ_ITEM_DONE:
Shishir Agrawal566b7612013-10-28 14:41:00 -0700654 ar = (AsyncResult) msg.obj;
655 request = (MainThreadRequest) ar.userObj;
Jake Hambye994d462014-02-03 13:10:13 -0800656 if (ar.exception == null && ar.result != null) {
657 request.result = ar.result; // String
Shishir Agrawal566b7612013-10-28 14:41:00 -0700658 } else {
Jake Hambye994d462014-02-03 13:10:13 -0800659 request.result = "";
660 if (ar.result == null) {
661 loge("nvReadItem: Empty response");
662 } else if (ar.exception instanceof CommandException) {
663 loge("nvReadItem: CommandException: " +
664 ar.exception);
Shishir Agrawal566b7612013-10-28 14:41:00 -0700665 } else {
Jake Hambye994d462014-02-03 13:10:13 -0800666 loge("nvReadItem: Unknown exception");
Shishir Agrawal566b7612013-10-28 14:41:00 -0700667 }
668 }
Pengquan Meng0c05b502018-09-06 09:59:22 -0700669 notifyRequester(request);
Shishir Agrawal566b7612013-10-28 14:41:00 -0700670 break;
671
Jake Hambye994d462014-02-03 13:10:13 -0800672 case CMD_NV_WRITE_ITEM:
673 request = (MainThreadRequest) msg.obj;
674 onCompleted = obtainMessage(EVENT_NV_WRITE_ITEM_DONE, request);
675 Pair<Integer, String> idValue = (Pair<Integer, String>) request.argument;
Nazanin Bakhshi2f2cc692018-12-11 15:15:39 -0800676 defaultPhone.nvWriteItem(idValue.first, idValue.second, onCompleted,
vagdevie435a3e2018-08-15 16:01:53 -0700677 request.workSource);
Jake Hambye994d462014-02-03 13:10:13 -0800678 break;
679
680 case EVENT_NV_WRITE_ITEM_DONE:
681 handleNullReturnEvent(msg, "nvWriteItem");
682 break;
683
684 case CMD_NV_WRITE_CDMA_PRL:
685 request = (MainThreadRequest) msg.obj;
686 onCompleted = obtainMessage(EVENT_NV_WRITE_CDMA_PRL_DONE, request);
Nazanin Bakhshi2f2cc692018-12-11 15:15:39 -0800687 defaultPhone.nvWriteCdmaPrl((byte[]) request.argument, onCompleted);
Jake Hambye994d462014-02-03 13:10:13 -0800688 break;
689
690 case EVENT_NV_WRITE_CDMA_PRL_DONE:
691 handleNullReturnEvent(msg, "nvWriteCdmaPrl");
692 break;
693
chen xu1cc0abe2018-10-26 17:39:23 -0700694 case CMD_RESET_MODEM_CONFIG:
Jake Hambye994d462014-02-03 13:10:13 -0800695 request = (MainThreadRequest) msg.obj;
chen xu1cc0abe2018-10-26 17:39:23 -0700696 onCompleted = obtainMessage(EVENT_RESET_MODEM_CONFIG_DONE, request);
Nazanin Bakhshi2f2cc692018-12-11 15:15:39 -0800697 defaultPhone.resetModemConfig(onCompleted);
Jake Hambye994d462014-02-03 13:10:13 -0800698 break;
699
chen xu1cc0abe2018-10-26 17:39:23 -0700700 case EVENT_RESET_MODEM_CONFIG_DONE:
701 handleNullReturnEvent(msg, "resetModemConfig");
Jake Hambye994d462014-02-03 13:10:13 -0800702 break;
703
Jake Hamby7c27be32014-03-03 13:25:59 -0800704 case CMD_GET_PREFERRED_NETWORK_TYPE:
705 request = (MainThreadRequest) msg.obj;
706 onCompleted = obtainMessage(EVENT_GET_PREFERRED_NETWORK_TYPE_DONE, request);
Stuart Scott54788802015-03-30 13:18:01 -0700707 getPhoneFromRequest(request).getPreferredNetworkType(onCompleted);
Jake Hamby7c27be32014-03-03 13:25:59 -0800708 break;
709
710 case EVENT_GET_PREFERRED_NETWORK_TYPE_DONE:
711 ar = (AsyncResult) msg.obj;
712 request = (MainThreadRequest) ar.userObj;
713 if (ar.exception == null && ar.result != null) {
714 request.result = ar.result; // Integer
715 } else {
Sanket Padawecfc2d352016-01-05 19:52:14 -0800716 request.result = null;
Jake Hamby7c27be32014-03-03 13:25:59 -0800717 if (ar.result == null) {
718 loge("getPreferredNetworkType: Empty response");
719 } else if (ar.exception instanceof CommandException) {
720 loge("getPreferredNetworkType: CommandException: " +
721 ar.exception);
722 } else {
723 loge("getPreferredNetworkType: Unknown exception");
724 }
725 }
Pengquan Meng0c05b502018-09-06 09:59:22 -0700726 notifyRequester(request);
Jake Hamby7c27be32014-03-03 13:25:59 -0800727 break;
728
729 case CMD_SET_PREFERRED_NETWORK_TYPE:
730 request = (MainThreadRequest) msg.obj;
731 onCompleted = obtainMessage(EVENT_SET_PREFERRED_NETWORK_TYPE_DONE, request);
732 int networkType = (Integer) request.argument;
Stuart Scott54788802015-03-30 13:18:01 -0700733 getPhoneFromRequest(request).setPreferredNetworkType(networkType, onCompleted);
Jake Hamby7c27be32014-03-03 13:25:59 -0800734 break;
735
736 case EVENT_SET_PREFERRED_NETWORK_TYPE_DONE:
737 handleNullReturnEvent(msg, "setPreferredNetworkType");
738 break;
739
Shuo Qian850e4d6a2018-04-25 21:02:08 +0000740 case CMD_INVOKE_OEM_RIL_REQUEST_RAW:
741 request = (MainThreadRequest)msg.obj;
742 onCompleted = obtainMessage(EVENT_INVOKE_OEM_RIL_REQUEST_RAW_DONE, request);
Nazanin Bakhshi2f2cc692018-12-11 15:15:39 -0800743 defaultPhone.invokeOemRilRequestRaw((byte[]) request.argument, onCompleted);
Shuo Qian850e4d6a2018-04-25 21:02:08 +0000744 break;
745
746 case EVENT_INVOKE_OEM_RIL_REQUEST_RAW_DONE:
747 ar = (AsyncResult)msg.obj;
748 request = (MainThreadRequest)ar.userObj;
749 request.result = ar;
Pengquan Meng0c05b502018-09-06 09:59:22 -0700750 notifyRequester(request);
Shuo Qian850e4d6a2018-04-25 21:02:08 +0000751 break;
752
Shishir Agrawal76d5da92014-11-09 16:17:25 -0800753 case CMD_SET_VOICEMAIL_NUMBER:
754 request = (MainThreadRequest) msg.obj;
755 onCompleted = obtainMessage(EVENT_SET_VOICEMAIL_NUMBER_DONE, request);
756 Pair<String, String> tagNum = (Pair<String, String>) request.argument;
Stuart Scott584921c2015-01-15 17:10:34 -0800757 getPhoneFromRequest(request).setVoiceMailNumber(tagNum.first, tagNum.second,
758 onCompleted);
Shishir Agrawal76d5da92014-11-09 16:17:25 -0800759 break;
760
761 case EVENT_SET_VOICEMAIL_NUMBER_DONE:
762 handleNullReturnEvent(msg, "setVoicemailNumber");
763 break;
764
Stuart Scott54788802015-03-30 13:18:01 -0700765 case CMD_SET_NETWORK_SELECTION_MODE_AUTOMATIC:
766 request = (MainThreadRequest) msg.obj;
767 onCompleted = obtainMessage(EVENT_SET_NETWORK_SELECTION_MODE_AUTOMATIC_DONE,
768 request);
769 getPhoneFromRequest(request).setNetworkSelectionModeAutomatic(onCompleted);
770 break;
771
772 case EVENT_SET_NETWORK_SELECTION_MODE_AUTOMATIC_DONE:
773 handleNullReturnEvent(msg, "setNetworkSelectionModeAutomatic");
774 break;
775
Shishir Agrawal302c8692015-06-19 13:49:39 -0700776 case CMD_PERFORM_NETWORK_SCAN:
777 request = (MainThreadRequest) msg.obj;
778 onCompleted = obtainMessage(EVENT_PERFORM_NETWORK_SCAN_DONE, request);
779 getPhoneFromRequest(request).getAvailableNetworks(onCompleted);
780 break;
781
782 case EVENT_PERFORM_NETWORK_SCAN_DONE:
783 ar = (AsyncResult) msg.obj;
784 request = (MainThreadRequest) ar.userObj;
785 CellNetworkScanResult cellScanResult;
786 if (ar.exception == null && ar.result != null) {
787 cellScanResult = new CellNetworkScanResult(
788 CellNetworkScanResult.STATUS_SUCCESS,
789 (List<OperatorInfo>) ar.result);
790 } else {
791 if (ar.result == null) {
792 loge("getCellNetworkScanResults: Empty response");
793 }
794 if (ar.exception != null) {
795 loge("getCellNetworkScanResults: Exception: " + ar.exception);
796 }
797 int errorCode = CellNetworkScanResult.STATUS_UNKNOWN_ERROR;
798 if (ar.exception instanceof CommandException) {
799 CommandException.Error error =
800 ((CommandException) (ar.exception)).getCommandError();
801 if (error == CommandException.Error.RADIO_NOT_AVAILABLE) {
802 errorCode = CellNetworkScanResult.STATUS_RADIO_NOT_AVAILABLE;
803 } else if (error == CommandException.Error.GENERIC_FAILURE) {
804 errorCode = CellNetworkScanResult.STATUS_RADIO_GENERIC_FAILURE;
805 }
806 }
807 cellScanResult = new CellNetworkScanResult(errorCode, null);
808 }
809 request.result = cellScanResult;
Pengquan Meng0c05b502018-09-06 09:59:22 -0700810 notifyRequester(request);
Shishir Agrawal302c8692015-06-19 13:49:39 -0700811 break;
812
813 case CMD_SET_NETWORK_SELECTION_MODE_MANUAL:
814 request = (MainThreadRequest) msg.obj;
Shishir Agrawal77ba3172015-09-10 14:50:19 -0700815 ManualNetworkSelectionArgument selArg =
816 (ManualNetworkSelectionArgument) request.argument;
Shishir Agrawal302c8692015-06-19 13:49:39 -0700817 onCompleted = obtainMessage(EVENT_SET_NETWORK_SELECTION_MODE_MANUAL_DONE,
818 request);
Shishir Agrawal77ba3172015-09-10 14:50:19 -0700819 getPhoneFromRequest(request).selectNetworkManually(selArg.operatorInfo,
820 selArg.persistSelection, onCompleted);
Shishir Agrawal302c8692015-06-19 13:49:39 -0700821 break;
822
823 case EVENT_SET_NETWORK_SELECTION_MODE_MANUAL_DONE:
Pengquan Mengdd9ac822018-09-20 15:25:35 -0700824 ar = (AsyncResult) msg.obj;
825 request = (MainThreadRequest) ar.userObj;
826 if (ar.exception == null) {
827 request.result = true;
828 } else {
829 request.result = false;
830 loge("setNetworkSelectionModeManual " + ar.exception);
831 }
832 notifyRequester(request);
833 mApp.onNetworkSelectionChanged(request.subId);
Shishir Agrawal302c8692015-06-19 13:49:39 -0700834 break;
835
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -0700836 case CMD_GET_MODEM_ACTIVITY_INFO:
837 request = (MainThreadRequest) msg.obj;
838 onCompleted = obtainMessage(EVENT_GET_MODEM_ACTIVITY_INFO_DONE, request);
James Mattisc56281c2019-04-03 14:18:34 -0700839 if (defaultPhone != null) {
840 defaultPhone.getModemActivityInfo(onCompleted, request.workSource);
841 }
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -0700842 break;
843
844 case EVENT_GET_MODEM_ACTIVITY_INFO_DONE:
845 ar = (AsyncResult) msg.obj;
846 request = (MainThreadRequest) ar.userObj;
847 if (ar.exception == null && ar.result != null) {
848 request.result = ar.result;
849 } else {
850 if (ar.result == null) {
851 loge("queryModemActivityInfo: Empty response");
852 } else if (ar.exception instanceof CommandException) {
853 loge("queryModemActivityInfo: CommandException: " +
854 ar.exception);
855 } else {
856 loge("queryModemActivityInfo: Unknown exception");
857 }
858 }
Amit Mahajand4766222016-01-28 15:28:28 -0800859 // Result cannot be null. Return ModemActivityInfo with all fields set to 0.
860 if (request.result == null) {
861 request.result = new ModemActivityInfo(0, 0, 0, null, 0, 0);
862 }
Pengquan Meng0c05b502018-09-06 09:59:22 -0700863 notifyRequester(request);
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -0700864 break;
865
Meng Wang1a7c35a2016-05-05 20:56:15 -0700866 case CMD_SET_ALLOWED_CARRIERS:
867 request = (MainThreadRequest) msg.obj;
Michele Berionne0963c862018-11-27 18:57:59 -0800868 CarrierRestrictionRules argument =
869 (CarrierRestrictionRules) request.argument;
Meng Wang1a7c35a2016-05-05 20:56:15 -0700870 onCompleted = obtainMessage(EVENT_SET_ALLOWED_CARRIERS_DONE, request);
Michele Berionne0963c862018-11-27 18:57:59 -0800871 defaultPhone.setAllowedCarriers(argument, onCompleted, request.workSource);
Meng Wang1a7c35a2016-05-05 20:56:15 -0700872 break;
873
874 case EVENT_SET_ALLOWED_CARRIERS_DONE:
875 ar = (AsyncResult) msg.obj;
876 request = (MainThreadRequest) ar.userObj;
877 if (ar.exception == null && ar.result != null) {
878 request.result = ar.result;
879 } else {
Michele Berionne0963c862018-11-27 18:57:59 -0800880 request.result = TelephonyManager.SET_CARRIER_RESTRICTION_ERROR;
881 if (ar.exception instanceof CommandException) {
882 loge("setAllowedCarriers: CommandException: " + ar.exception);
883 CommandException.Error error =
884 ((CommandException) (ar.exception)).getCommandError();
885 if (error == CommandException.Error.REQUEST_NOT_SUPPORTED) {
886 request.result =
887 TelephonyManager.SET_CARRIER_RESTRICTION_NOT_SUPPORTED;
888 }
Meng Wang1a7c35a2016-05-05 20:56:15 -0700889 } else {
890 loge("setAllowedCarriers: Unknown exception");
891 }
892 }
Pengquan Meng0c05b502018-09-06 09:59:22 -0700893 notifyRequester(request);
Meng Wang1a7c35a2016-05-05 20:56:15 -0700894 break;
895
896 case CMD_GET_ALLOWED_CARRIERS:
897 request = (MainThreadRequest) msg.obj;
898 onCompleted = obtainMessage(EVENT_GET_ALLOWED_CARRIERS_DONE, request);
Nazanin Bakhshi2f2cc692018-12-11 15:15:39 -0800899 defaultPhone.getAllowedCarriers(onCompleted, request.workSource);
Meng Wang1a7c35a2016-05-05 20:56:15 -0700900 break;
901
902 case EVENT_GET_ALLOWED_CARRIERS_DONE:
903 ar = (AsyncResult) msg.obj;
904 request = (MainThreadRequest) ar.userObj;
905 if (ar.exception == null && ar.result != null) {
906 request.result = ar.result;
907 } else {
Michele Berionne0963c862018-11-27 18:57:59 -0800908 request.result = new IllegalStateException(
909 "Failed to get carrier restrictions");
Meng Wang1a7c35a2016-05-05 20:56:15 -0700910 if (ar.result == null) {
911 loge("getAllowedCarriers: Empty response");
912 } else if (ar.exception instanceof CommandException) {
913 loge("getAllowedCarriers: CommandException: " +
914 ar.exception);
915 } else {
916 loge("getAllowedCarriers: Unknown exception");
917 }
918 }
Pengquan Meng0c05b502018-09-06 09:59:22 -0700919 notifyRequester(request);
Meng Wang1a7c35a2016-05-05 20:56:15 -0700920 break;
921
Nathan Haroldb3014052017-01-25 15:57:32 -0800922 case EVENT_GET_FORBIDDEN_PLMNS_DONE:
923 ar = (AsyncResult) msg.obj;
924 request = (MainThreadRequest) ar.userObj;
925 if (ar.exception == null && ar.result != null) {
926 request.result = ar.result;
927 } else {
928 request.result = new IllegalArgumentException(
929 "Failed to retrieve Forbidden Plmns");
930 if (ar.result == null) {
931 loge("getForbiddenPlmns: Empty response");
932 } else {
933 loge("getForbiddenPlmns: Unknown exception");
934 }
935 }
Pengquan Meng0c05b502018-09-06 09:59:22 -0700936 notifyRequester(request);
Nathan Haroldb3014052017-01-25 15:57:32 -0800937 break;
938
939 case CMD_GET_FORBIDDEN_PLMNS:
940 request = (MainThreadRequest) msg.obj;
941 uiccCard = getUiccCardFromRequest(request);
942 if (uiccCard == null) {
943 loge("getForbiddenPlmns() UiccCard is null");
944 request.result = new IllegalArgumentException(
945 "getForbiddenPlmns() UiccCard is null");
Pengquan Meng0c05b502018-09-06 09:59:22 -0700946 notifyRequester(request);
Nathan Haroldb3014052017-01-25 15:57:32 -0800947 break;
948 }
949 Integer appType = (Integer) request.argument;
950 UiccCardApplication uiccApp = uiccCard.getApplicationByType(appType);
951 if (uiccApp == null) {
952 loge("getForbiddenPlmns() no app with specified type -- "
953 + appType);
954 request.result = new IllegalArgumentException("Failed to get UICC App");
Pengquan Meng0c05b502018-09-06 09:59:22 -0700955 notifyRequester(request);
Nathan Haroldb3014052017-01-25 15:57:32 -0800956 break;
957 } else {
958 if (DBG) logv("getForbiddenPlmns() found app " + uiccApp.getAid()
959 + " specified type -- " + appType);
960 }
961 onCompleted = obtainMessage(EVENT_GET_FORBIDDEN_PLMNS_DONE, request);
962 ((SIMRecords) uiccApp.getIccRecords()).getForbiddenPlmns(
963 onCompleted);
964 break;
965
Holly Jiuyu Sun01c47ad2018-01-24 17:56:33 +0000966 case CMD_SWITCH_SLOTS:
967 request = (MainThreadRequest) msg.obj;
968 int[] physicalSlots = (int[]) request.argument;
969 onCompleted = obtainMessage(EVENT_SWITCH_SLOTS_DONE, request);
970 UiccController.getInstance().switchSlots(physicalSlots, onCompleted);
971 break;
972
973 case EVENT_SWITCH_SLOTS_DONE:
974 ar = (AsyncResult) msg.obj;
975 request = (MainThreadRequest) ar.userObj;
976 request.result = (ar.exception == null);
Pengquan Meng0c05b502018-09-06 09:59:22 -0700977 notifyRequester(request);
978 break;
979 case CMD_GET_NETWORK_SELECTION_MODE:
980 request = (MainThreadRequest) msg.obj;
981 onCompleted = obtainMessage(EVENT_GET_NETWORK_SELECTION_MODE_DONE, request);
982 getPhoneFromRequest(request).getNetworkSelectionMode(onCompleted);
983 break;
984
985 case EVENT_GET_NETWORK_SELECTION_MODE_DONE:
986 ar = (AsyncResult) msg.obj;
987 request = (MainThreadRequest) ar.userObj;
988 if (ar.exception != null) {
989 request.result = TelephonyManager.NETWORK_SELECTION_MODE_UNKNOWN;
990 } else {
991 int mode = ((int[]) ar.result)[0];
992 if (mode == 0) {
993 request.result = TelephonyManager.NETWORK_SELECTION_MODE_AUTO;
994 } else {
995 request.result = TelephonyManager.NETWORK_SELECTION_MODE_MANUAL;
996 }
Holly Jiuyu Sun01c47ad2018-01-24 17:56:33 +0000997 }
Pengquan Meng0c05b502018-09-06 09:59:22 -0700998 notifyRequester(request);
999 break;
1000 case CMD_GET_CDMA_ROAMING_MODE:
1001 request = (MainThreadRequest) msg.obj;
1002 onCompleted = obtainMessage(EVENT_GET_CDMA_ROAMING_MODE_DONE, request);
1003 getPhoneFromRequest(request).queryCdmaRoamingPreference(onCompleted);
1004 break;
1005 case EVENT_GET_CDMA_ROAMING_MODE_DONE:
1006 ar = (AsyncResult) msg.obj;
1007 request = (MainThreadRequest) ar.userObj;
1008 if (ar.exception != null) {
1009 request.result = TelephonyManager.CDMA_ROAMING_MODE_RADIO_DEFAULT;
1010 } else {
1011 request.result = ((int[]) ar.result)[0];
1012 }
1013 notifyRequester(request);
1014 break;
1015 case CMD_SET_CDMA_ROAMING_MODE:
1016 request = (MainThreadRequest) msg.obj;
1017 onCompleted = obtainMessage(EVENT_SET_CDMA_ROAMING_MODE_DONE, request);
1018 int mode = (int) request.argument;
1019 getPhoneFromRequest(request).setCdmaRoamingPreference(mode, onCompleted);
1020 break;
1021 case EVENT_SET_CDMA_ROAMING_MODE_DONE:
1022 ar = (AsyncResult) msg.obj;
1023 request = (MainThreadRequest) ar.userObj;
1024 request.result = ar.exception == null;
1025 notifyRequester(request);
1026 break;
1027 case CMD_SET_CDMA_SUBSCRIPTION_MODE:
1028 request = (MainThreadRequest) msg.obj;
1029 onCompleted = obtainMessage(EVENT_SET_CDMA_SUBSCRIPTION_MODE_DONE, request);
1030 int subscriptionMode = (int) request.argument;
1031 getPhoneFromRequest(request).setCdmaSubscription(subscriptionMode, onCompleted);
1032 break;
1033 case EVENT_SET_CDMA_SUBSCRIPTION_MODE_DONE:
1034 ar = (AsyncResult) msg.obj;
1035 request = (MainThreadRequest) ar.userObj;
1036 request.result = ar.exception == null;
1037 notifyRequester(request);
Holly Jiuyu Sun01c47ad2018-01-24 17:56:33 +00001038 break;
Nathan Harold3ff88932018-08-14 10:19:49 -07001039 case CMD_GET_ALL_CELL_INFO:
1040 request = (MainThreadRequest) msg.obj;
Nathan Harold3ff88932018-08-14 10:19:49 -07001041 onCompleted = obtainMessage(EVENT_GET_ALL_CELL_INFO_DONE, request);
Nathan Harold92bed182018-10-12 18:16:49 -07001042 request.phone.requestCellInfoUpdate(request.workSource, onCompleted);
Nathan Harold3ff88932018-08-14 10:19:49 -07001043 break;
Nathan Harold3ff88932018-08-14 10:19:49 -07001044 case EVENT_GET_ALL_CELL_INFO_DONE:
1045 ar = (AsyncResult) msg.obj;
1046 request = (MainThreadRequest) ar.userObj;
Nathan Harold8d0f1742018-10-02 12:14:47 -07001047 // If a timeout occurs, the response will be null
1048 request.result = (ar.exception == null && ar.result != null)
1049 ? ar.result : new ArrayList<CellInfo>();
Nathan Harold3ff88932018-08-14 10:19:49 -07001050 synchronized (request) {
1051 request.notifyAll();
1052 }
1053 break;
Nathan Haroldfa8da0f2018-09-27 18:51:29 -07001054 case CMD_REQUEST_CELL_INFO_UPDATE:
1055 request = (MainThreadRequest) msg.obj;
1056 request.phone.requestCellInfoUpdate(request.workSource,
1057 obtainMessage(EVENT_REQUEST_CELL_INFO_UPDATE_DONE, request));
1058 break;
1059 case EVENT_REQUEST_CELL_INFO_UPDATE_DONE:
1060 ar = (AsyncResult) msg.obj;
1061 request = (MainThreadRequest) ar.userObj;
1062 ICellInfoCallback cb = (ICellInfoCallback) request.argument;
1063 try {
1064 if (ar.exception != null) {
Nathan Haroldfa8da0f2018-09-27 18:51:29 -07001065 Log.e(LOG_TAG, "Exception retrieving CellInfo=" + ar.exception);
Nathan Harolde82c4b82018-12-18 19:40:37 -08001066 cb.onError(TelephonyManager.CellInfoCallback.ERROR_MODEM_ERROR,
1067 new android.os.ParcelableException(ar.exception));
Nathan Haroldfa8da0f2018-09-27 18:51:29 -07001068 } else if (ar.result == null) {
Nathan Haroldfa8da0f2018-09-27 18:51:29 -07001069 Log.w(LOG_TAG, "Timeout Waiting for CellInfo!");
Nathan Harolde82c4b82018-12-18 19:40:37 -08001070 cb.onError(TelephonyManager.CellInfoCallback.ERROR_TIMEOUT, null);
Nathan Haroldfa8da0f2018-09-27 18:51:29 -07001071 } else {
1072 // use the result as returned
1073 cb.onCellInfo((List<CellInfo>) ar.result);
1074 }
1075 } catch (RemoteException re) {
1076 Log.w(LOG_TAG, "Discarded CellInfo due to Callback RemoteException");
1077 }
1078 break;
1079 case CMD_GET_CELL_LOCATION:
Nathan Harold3ff88932018-08-14 10:19:49 -07001080 request = (MainThreadRequest) msg.obj;
1081 WorkSource ws = (WorkSource) request.argument;
1082 Phone phone = getPhoneFromRequest(request);
1083 phone.getCellLocation(ws, obtainMessage(EVENT_GET_CELL_LOCATION_DONE, request));
1084 break;
Nathan Haroldfa8da0f2018-09-27 18:51:29 -07001085 case EVENT_GET_CELL_LOCATION_DONE:
Nathan Harold3ff88932018-08-14 10:19:49 -07001086 ar = (AsyncResult) msg.obj;
1087 request = (MainThreadRequest) ar.userObj;
1088 if (ar.exception == null) {
1089 request.result = ar.result;
1090 } else {
Nathan Haroldfa8da0f2018-09-27 18:51:29 -07001091 phone = getPhoneFromRequest(request);
Nathan Harold3ff88932018-08-14 10:19:49 -07001092 request.result = (phone.getPhoneType() == PhoneConstants.PHONE_TYPE_CDMA)
1093 ? new CdmaCellLocation() : new GsmCellLocation();
1094 }
1095
1096 synchronized (request) {
1097 request.notifyAll();
1098 }
1099 break;
chen xu1cc0abe2018-10-26 17:39:23 -07001100 case CMD_MODEM_REBOOT:
1101 request = (MainThreadRequest) msg.obj;
1102 onCompleted = obtainMessage(EVENT_RESET_MODEM_CONFIG_DONE, request);
Nazanin Bakhshi2f2cc692018-12-11 15:15:39 -08001103 defaultPhone.rebootModem(onCompleted);
chen xu1cc0abe2018-10-26 17:39:23 -07001104 break;
chen xu1cc0abe2018-10-26 17:39:23 -07001105 case EVENT_CMD_MODEM_REBOOT_DONE:
1106 handleNullReturnEvent(msg, "rebootModem");
1107 break;
Malcolm Chen509b5b72019-01-15 20:22:16 -08001108 case CMD_REQUEST_ENABLE_MODEM:
1109 request = (MainThreadRequest) msg.obj;
1110 boolean enable = (boolean) request.argument;
1111 onCompleted = obtainMessage(EVENT_ENABLE_MODEM_DONE, request);
Nazanin Bakhshi9b379572019-03-01 17:27:47 -08001112 onCompleted.arg1 = enable ? 1 : 0;
Malcolm Chen509b5b72019-01-15 20:22:16 -08001113 PhoneConfigurationManager.getInstance()
1114 .enablePhone(request.phone, enable, onCompleted);
1115 break;
1116 case EVENT_ENABLE_MODEM_DONE:
1117 ar = (AsyncResult) msg.obj;
1118 request = (MainThreadRequest) ar.userObj;
1119 request.result = (ar.exception == null);
Nazanin Bakhshi9b379572019-03-01 17:27:47 -08001120 //update the cache as modem status has changed
1121 mPhoneConfigurationManager.addToPhoneStatusCache(
1122 request.phone.getPhoneId(), msg.arg1 == 1);
Pengquan Meng92d253b2019-02-06 11:12:53 -08001123 updateModemStateMetrics();
Malcolm Chen509b5b72019-01-15 20:22:16 -08001124 notifyRequester(request);
1125 break;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001126 default:
1127 Log.w(LOG_TAG, "MainThreadHandler: unexpected message code: " + msg.what);
1128 break;
1129 }
1130 }
Jake Hambye994d462014-02-03 13:10:13 -08001131
Pengquan Meng0c05b502018-09-06 09:59:22 -07001132 private void notifyRequester(MainThreadRequest request) {
1133 synchronized (request) {
1134 request.notifyAll();
1135 }
1136 }
1137
Jake Hambye994d462014-02-03 13:10:13 -08001138 private void handleNullReturnEvent(Message msg, String command) {
1139 AsyncResult ar = (AsyncResult) msg.obj;
1140 MainThreadRequest request = (MainThreadRequest) ar.userObj;
1141 if (ar.exception == null) {
1142 request.result = true;
1143 } else {
1144 request.result = false;
1145 if (ar.exception instanceof CommandException) {
1146 loge(command + ": CommandException: " + ar.exception);
1147 } else {
1148 loge(command + ": Unknown exception");
1149 }
1150 }
Pengquan Meng0c05b502018-09-06 09:59:22 -07001151 notifyRequester(request);
Jake Hambye994d462014-02-03 13:10:13 -08001152 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001153 }
1154
1155 /**
1156 * Posts the specified command to be executed on the main thread,
1157 * waits for the request to complete, and returns the result.
1158 * @see #sendRequestAsync
1159 */
1160 private Object sendRequest(int command, Object argument) {
Nathan Harold92bed182018-10-12 18:16:49 -07001161 return sendRequest(
1162 command, argument, SubscriptionManager.INVALID_SUBSCRIPTION_ID, null, null);
vagdevie435a3e2018-08-15 16:01:53 -07001163 }
1164
1165 /**
1166 * Posts the specified command to be executed on the main thread,
1167 * waits for the request to complete, and returns the result.
1168 * @see #sendRequestAsync
1169 */
1170 private Object sendRequest(int command, Object argument, WorkSource workSource) {
1171 return sendRequest(command, argument, SubscriptionManager.INVALID_SUBSCRIPTION_ID,
Nathan Harold92bed182018-10-12 18:16:49 -07001172 null, workSource);
Wink Saville36469e72014-06-11 15:17:00 -07001173 }
1174
1175 /**
1176 * Posts the specified command to be executed on the main thread,
1177 * waits for the request to complete, and returns the result.
1178 * @see #sendRequestAsync
1179 */
Shishir Agrawal76d5da92014-11-09 16:17:25 -08001180 private Object sendRequest(int command, Object argument, Integer subId) {
Nathan Harold92bed182018-10-12 18:16:49 -07001181 return sendRequest(command, argument, subId, null, null);
vagdevie435a3e2018-08-15 16:01:53 -07001182 }
1183
1184 /**
1185 * Posts the specified command to be executed on the main thread,
1186 * waits for the request to complete, and returns the result.
1187 * @see #sendRequestAsync
1188 */
Nathan Harold92bed182018-10-12 18:16:49 -07001189 private Object sendRequest(int command, Object argument, int subId, WorkSource workSource) {
1190 return sendRequest(command, argument, subId, null, workSource);
1191 }
1192
1193 /**
1194 * Posts the specified command to be executed on the main thread,
1195 * waits for the request to complete, and returns the result.
1196 * @see #sendRequestAsync
1197 */
1198 private Object sendRequest(int command, Object argument, Phone phone, WorkSource workSource) {
1199 return sendRequest(
1200 command, argument, SubscriptionManager.INVALID_SUBSCRIPTION_ID, phone, workSource);
1201 }
1202
1203 /**
1204 * Posts the specified command to be executed on the main thread,
1205 * waits for the request to complete, and returns the result.
1206 * @see #sendRequestAsync
1207 */
1208 private Object sendRequest(
1209 int command, Object argument, Integer subId, Phone phone, WorkSource workSource) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001210 if (Looper.myLooper() == mMainThreadHandler.getLooper()) {
1211 throw new RuntimeException("This method will deadlock if called from the main thread.");
1212 }
1213
Nathan Harold92bed182018-10-12 18:16:49 -07001214 MainThreadRequest request = null;
1215 if (subId != SubscriptionManager.INVALID_SUBSCRIPTION_ID && phone != null) {
1216 throw new IllegalArgumentException("subId and phone cannot both be specified!");
1217 } else if (phone != null) {
1218 request = new MainThreadRequest(argument, phone, workSource);
1219 } else {
1220 request = new MainThreadRequest(argument, subId, workSource);
1221 }
1222
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001223 Message msg = mMainThreadHandler.obtainMessage(command, request);
1224 msg.sendToTarget();
1225
1226 // Wait for the request to complete
1227 synchronized (request) {
1228 while (request.result == null) {
1229 try {
1230 request.wait();
1231 } catch (InterruptedException e) {
1232 // Do nothing, go back and wait until the request is complete
1233 }
1234 }
1235 }
1236 return request.result;
1237 }
1238
1239 /**
1240 * Asynchronous ("fire and forget") version of sendRequest():
1241 * Posts the specified command to be executed on the main thread, and
1242 * returns immediately.
1243 * @see #sendRequest
1244 */
1245 private void sendRequestAsync(int command) {
1246 mMainThreadHandler.sendEmptyMessage(command);
1247 }
1248
1249 /**
Sailesh Nepalbd76e4e2013-10-27 13:59:44 -07001250 * Same as {@link #sendRequestAsync(int)} except it takes an argument.
Nathan Haroldfa8da0f2018-09-27 18:51:29 -07001251 * @see {@link #sendRequest(int)}
Sailesh Nepalbd76e4e2013-10-27 13:59:44 -07001252 */
1253 private void sendRequestAsync(int command, Object argument) {
Nathan Haroldfa8da0f2018-09-27 18:51:29 -07001254 sendRequestAsync(command, argument, null, null);
1255 }
1256
1257 /**
1258 * Same as {@link #sendRequestAsync(int,Object)} except it takes a Phone and WorkSource.
1259 * @see {@link #sendRequest(int,Object)}
1260 */
1261 private void sendRequestAsync(
1262 int command, Object argument, Phone phone, WorkSource workSource) {
1263 MainThreadRequest request = new MainThreadRequest(argument, phone, workSource);
Sailesh Nepalbd76e4e2013-10-27 13:59:44 -07001264 Message msg = mMainThreadHandler.obtainMessage(command, request);
1265 msg.sendToTarget();
1266 }
1267
1268 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001269 * Initialize the singleton PhoneInterfaceManager instance.
1270 * This is only done once, at startup, from PhoneApp.onCreate().
1271 */
Nazanin Bakhshi2f2cc692018-12-11 15:15:39 -08001272 /* package */ static PhoneInterfaceManager init(PhoneGlobals app) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001273 synchronized (PhoneInterfaceManager.class) {
1274 if (sInstance == null) {
Nazanin Bakhshi2f2cc692018-12-11 15:15:39 -08001275 sInstance = new PhoneInterfaceManager(app);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001276 } else {
1277 Log.wtf(LOG_TAG, "init() called multiple times! sInstance = " + sInstance);
1278 }
1279 return sInstance;
1280 }
1281 }
1282
1283 /** Private constructor; @see init() */
Nazanin Bakhshi2f2cc692018-12-11 15:15:39 -08001284 private PhoneInterfaceManager(PhoneGlobals app) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001285 mApp = app;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001286 mCM = PhoneGlobals.getInstance().mCM;
Stuart Scott981d8582015-04-21 14:09:50 -07001287 mUserManager = (UserManager) app.getSystemService(Context.USER_SERVICE);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001288 mAppOps = (AppOpsManager)app.getSystemService(Context.APP_OPS_SERVICE);
1289 mMainThreadHandler = new MainThreadHandler();
Wink Savilleac1bdfd2014-11-20 23:04:44 -08001290 mSubscriptionController = SubscriptionController.getInstance();
Nazanin Bakhshi2f2cc692018-12-11 15:15:39 -08001291 mTelephonySharedPreferences =
1292 PreferenceManager.getDefaultSharedPreferences(mApp);
yinxub1bed742017-04-17 11:45:04 -07001293 mNetworkScanRequestTracker = new NetworkScanRequestTracker();
Malcolm Chenf144d942018-08-14 16:00:53 -07001294 mPhoneConfigurationManager = PhoneConfigurationManager.getInstance();
Wink Saville3ab207e2014-11-20 13:07:20 -08001295
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001296 publish();
1297 }
1298
Nazanin Bakhshi2f2cc692018-12-11 15:15:39 -08001299 private Phone getDefaultPhone() {
1300 Phone thePhone = getPhone(getDefaultSubscription());
1301 return (thePhone != null) ? thePhone : PhoneFactory.getDefaultPhone();
1302 }
1303
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001304 private void publish() {
1305 if (DBG) log("publish: " + this);
1306
1307 ServiceManager.addService("phone", this);
1308 }
1309
Stuart Scott584921c2015-01-15 17:10:34 -08001310 private Phone getPhoneFromRequest(MainThreadRequest request) {
Jordan Liu73b078d2019-02-28 12:03:40 -08001311 if (request.phone != null) {
1312 return request.phone;
1313 } else {
1314 return getPhoneFromSubId(request.subId);
1315 }
1316 }
1317
1318 private Phone getPhoneFromSubId(int subId) {
1319 return (subId == SubscriptionManager.INVALID_SUBSCRIPTION_ID)
1320 ? getDefaultPhone() : getPhone(subId);
Stuart Scott584921c2015-01-15 17:10:34 -08001321 }
1322
Shishir Agrawalc04d9752016-02-19 10:41:00 -08001323 private UiccCard getUiccCardFromRequest(MainThreadRequest request) {
1324 Phone phone = getPhoneFromRequest(request);
1325 return phone == null ? null :
1326 UiccController.getInstance().getUiccCard(phone.getPhoneId());
1327 }
1328
Wink Saville36469e72014-06-11 15:17:00 -07001329 // returns phone associated with the subId.
Wink Savilleb564aae2014-10-23 10:18:09 -07001330 private Phone getPhone(int subId) {
Wink Savilleac1bdfd2014-11-20 23:04:44 -08001331 return PhoneFactory.getPhone(mSubscriptionController.getPhoneId(subId));
Wink Saville36469e72014-06-11 15:17:00 -07001332 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001333
1334 public void dial(String number) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001335 dialForSubscriber(getPreferredVoiceSubscription(), number);
Wink Saville36469e72014-06-11 15:17:00 -07001336 }
1337
Wink Savilleb564aae2014-10-23 10:18:09 -07001338 public void dialForSubscriber(int subId, String number) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001339 if (DBG) log("dial: " + number);
1340 // No permission check needed here: This is just a wrapper around the
1341 // ACTION_DIAL intent, which is available to any app since it puts up
1342 // the UI before it does anything.
1343
Malcolm Chend965c8b2018-02-28 15:00:40 -08001344 final long identity = Binder.clearCallingIdentity();
1345 try {
1346 String url = createTelUrl(number);
1347 if (url == null) {
1348 return;
1349 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001350
Malcolm Chend965c8b2018-02-28 15:00:40 -08001351 // PENDING: should we just silently fail if phone is offhook or ringing?
1352 PhoneConstants.State state = mCM.getState(subId);
1353 if (state != PhoneConstants.State.OFFHOOK && state != PhoneConstants.State.RINGING) {
1354 Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse(url));
1355 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
1356 mApp.startActivity(intent);
1357 }
1358 } finally {
1359 Binder.restoreCallingIdentity(identity);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001360 }
1361 }
1362
1363 public void call(String callingPackage, String number) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001364 callForSubscriber(getPreferredVoiceSubscription(), callingPackage, number);
Wink Saville36469e72014-06-11 15:17:00 -07001365 }
1366
Wink Savilleb564aae2014-10-23 10:18:09 -07001367 public void callForSubscriber(int subId, String callingPackage, String number) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001368 if (DBG) log("call: " + number);
1369
1370 // This is just a wrapper around the ACTION_CALL intent, but we still
1371 // need to do a permission check since we're calling startActivity()
1372 // from the context of the phone app.
1373 enforceCallPermission();
1374
1375 if (mAppOps.noteOp(AppOpsManager.OP_CALL_PHONE, Binder.getCallingUid(), callingPackage)
1376 != AppOpsManager.MODE_ALLOWED) {
1377 return;
1378 }
1379
Malcolm Chend965c8b2018-02-28 15:00:40 -08001380 final long identity = Binder.clearCallingIdentity();
1381 try {
1382 String url = createTelUrl(number);
1383 if (url == null) {
1384 return;
1385 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001386
Malcolm Chend965c8b2018-02-28 15:00:40 -08001387 boolean isValid = false;
1388 final List<SubscriptionInfo> slist = getActiveSubscriptionInfoListPrivileged();
1389 if (slist != null) {
1390 for (SubscriptionInfo subInfoRecord : slist) {
1391 if (subInfoRecord.getSubscriptionId() == subId) {
1392 isValid = true;
1393 break;
1394 }
Wink Saville3ab207e2014-11-20 13:07:20 -08001395 }
Wink Saville08874612014-08-31 19:19:58 -07001396 }
Malcolm Chend965c8b2018-02-28 15:00:40 -08001397 if (!isValid) {
1398 return;
1399 }
Wink Saville08874612014-08-31 19:19:58 -07001400
Malcolm Chend965c8b2018-02-28 15:00:40 -08001401 Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse(url));
1402 intent.putExtra(SUBSCRIPTION_KEY, subId);
1403 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
1404 mApp.startActivity(intent);
1405 } finally {
1406 Binder.restoreCallingIdentity(identity);
1407 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001408 }
1409
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001410 public boolean supplyPin(String pin) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001411 return supplyPinForSubscriber(getDefaultSubscription(), pin);
Wink Saville36469e72014-06-11 15:17:00 -07001412 }
1413
Wink Savilleb564aae2014-10-23 10:18:09 -07001414 public boolean supplyPinForSubscriber(int subId, String pin) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001415 int [] resultArray = supplyPinReportResultForSubscriber(subId, pin);
Wink Saville9de0f752013-10-22 19:04:03 -07001416 return (resultArray[0] == PhoneConstants.PIN_RESULT_SUCCESS) ? true : false;
1417 }
1418
1419 public boolean supplyPuk(String puk, String pin) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001420 return supplyPukForSubscriber(getDefaultSubscription(), puk, pin);
Wink Saville36469e72014-06-11 15:17:00 -07001421 }
1422
Wink Savilleb564aae2014-10-23 10:18:09 -07001423 public boolean supplyPukForSubscriber(int subId, String puk, String pin) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001424 int [] resultArray = supplyPukReportResultForSubscriber(subId, puk, pin);
Wink Saville9de0f752013-10-22 19:04:03 -07001425 return (resultArray[0] == PhoneConstants.PIN_RESULT_SUCCESS) ? true : false;
1426 }
1427
1428 /** {@hide} */
1429 public int[] supplyPinReportResult(String pin) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001430 return supplyPinReportResultForSubscriber(getDefaultSubscription(), pin);
Wink Saville36469e72014-06-11 15:17:00 -07001431 }
1432
Wink Savilleb564aae2014-10-23 10:18:09 -07001433 public int[] supplyPinReportResultForSubscriber(int subId, String pin) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001434 enforceModifyPermission();
Malcolm Chend965c8b2018-02-28 15:00:40 -08001435
1436 final long identity = Binder.clearCallingIdentity();
1437 try {
1438 final UnlockSim checkSimPin = new UnlockSim(getPhone(subId).getIccCard());
1439 checkSimPin.start();
1440 return checkSimPin.unlockSim(null, pin);
1441 } finally {
1442 Binder.restoreCallingIdentity(identity);
1443 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001444 }
1445
Wink Saville9de0f752013-10-22 19:04:03 -07001446 /** {@hide} */
1447 public int[] supplyPukReportResult(String puk, String pin) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001448 return supplyPukReportResultForSubscriber(getDefaultSubscription(), puk, pin);
Wink Saville36469e72014-06-11 15:17:00 -07001449 }
1450
Wink Savilleb564aae2014-10-23 10:18:09 -07001451 public int[] supplyPukReportResultForSubscriber(int subId, String puk, String pin) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001452 enforceModifyPermission();
Malcolm Chend965c8b2018-02-28 15:00:40 -08001453
1454 final long identity = Binder.clearCallingIdentity();
1455 try {
1456 final UnlockSim checkSimPuk = new UnlockSim(getPhone(subId).getIccCard());
1457 checkSimPuk.start();
1458 return checkSimPuk.unlockSim(puk, pin);
1459 } finally {
1460 Binder.restoreCallingIdentity(identity);
1461 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001462 }
1463
1464 /**
Wink Saville9de0f752013-10-22 19:04:03 -07001465 * Helper thread to turn async call to SimCard#supplyPin into
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001466 * a synchronous one.
1467 */
1468 private static class UnlockSim extends Thread {
1469
1470 private final IccCard mSimCard;
1471
1472 private boolean mDone = false;
Wink Saville9de0f752013-10-22 19:04:03 -07001473 private int mResult = PhoneConstants.PIN_GENERAL_FAILURE;
1474 private int mRetryCount = -1;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001475
1476 // For replies from SimCard interface
1477 private Handler mHandler;
1478
1479 // For async handler to identify request type
1480 private static final int SUPPLY_PIN_COMPLETE = 100;
1481
1482 public UnlockSim(IccCard simCard) {
1483 mSimCard = simCard;
1484 }
1485
1486 @Override
1487 public void run() {
1488 Looper.prepare();
1489 synchronized (UnlockSim.this) {
1490 mHandler = new Handler() {
1491 @Override
1492 public void handleMessage(Message msg) {
1493 AsyncResult ar = (AsyncResult) msg.obj;
1494 switch (msg.what) {
1495 case SUPPLY_PIN_COMPLETE:
1496 Log.d(LOG_TAG, "SUPPLY_PIN_COMPLETE");
1497 synchronized (UnlockSim.this) {
Wink Saville9de0f752013-10-22 19:04:03 -07001498 mRetryCount = msg.arg1;
1499 if (ar.exception != null) {
1500 if (ar.exception instanceof CommandException &&
1501 ((CommandException)(ar.exception)).getCommandError()
1502 == CommandException.Error.PASSWORD_INCORRECT) {
1503 mResult = PhoneConstants.PIN_PASSWORD_INCORRECT;
1504 } else {
1505 mResult = PhoneConstants.PIN_GENERAL_FAILURE;
1506 }
1507 } else {
1508 mResult = PhoneConstants.PIN_RESULT_SUCCESS;
1509 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001510 mDone = true;
1511 UnlockSim.this.notifyAll();
1512 }
1513 break;
1514 }
1515 }
1516 };
1517 UnlockSim.this.notifyAll();
1518 }
1519 Looper.loop();
1520 }
1521
1522 /*
1523 * Use PIN or PUK to unlock SIM card
1524 *
1525 * If PUK is null, unlock SIM card with PIN
1526 *
1527 * If PUK is not null, unlock SIM card with PUK and set PIN code
1528 */
Wink Saville9de0f752013-10-22 19:04:03 -07001529 synchronized int[] unlockSim(String puk, String pin) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001530
1531 while (mHandler == null) {
1532 try {
1533 wait();
1534 } catch (InterruptedException e) {
1535 Thread.currentThread().interrupt();
1536 }
1537 }
1538 Message callback = Message.obtain(mHandler, SUPPLY_PIN_COMPLETE);
1539
1540 if (puk == null) {
1541 mSimCard.supplyPin(pin, callback);
1542 } else {
1543 mSimCard.supplyPuk(puk, pin, callback);
1544 }
1545
1546 while (!mDone) {
1547 try {
1548 Log.d(LOG_TAG, "wait for done");
1549 wait();
1550 } catch (InterruptedException e) {
1551 // Restore the interrupted status
1552 Thread.currentThread().interrupt();
1553 }
1554 }
1555 Log.d(LOG_TAG, "done");
Wink Saville9de0f752013-10-22 19:04:03 -07001556 int[] resultArray = new int[2];
1557 resultArray[0] = mResult;
1558 resultArray[1] = mRetryCount;
1559 return resultArray;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001560 }
1561 }
1562
1563 public void updateServiceLocation() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001564 updateServiceLocationForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001565
1566 }
1567
Wink Savilleb564aae2014-10-23 10:18:09 -07001568 public void updateServiceLocationForSubscriber(int subId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001569 // No permission check needed here: this call is harmless, and it's
1570 // needed for the ServiceState.requestStateUpdate() call (which is
1571 // already intentionally exposed to 3rd parties.)
Malcolm Chend965c8b2018-02-28 15:00:40 -08001572 final long identity = Binder.clearCallingIdentity();
1573 try {
1574 final Phone phone = getPhone(subId);
1575 if (phone != null) {
1576 phone.updateServiceLocation();
1577 }
1578 } finally {
1579 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07001580 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001581 }
1582
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001583 @Override
1584 public boolean isRadioOn(String callingPackage) {
1585 return isRadioOnForSubscriber(getDefaultSubscription(), callingPackage);
Wink Saville36469e72014-06-11 15:17:00 -07001586 }
1587
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001588 @Override
1589 public boolean isRadioOnForSubscriber(int subId, String callingPackage) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08001590 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
Jeff Davidson913390f2018-02-23 17:11:49 -08001591 mApp, subId, callingPackage, "isRadioOnForSubscriber")) {
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001592 return false;
1593 }
Malcolm Chend965c8b2018-02-28 15:00:40 -08001594
1595 final long identity = Binder.clearCallingIdentity();
1596 try {
1597 return isRadioOnForSubscriber(subId);
1598 } finally {
1599 Binder.restoreCallingIdentity(identity);
1600 }
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001601 }
1602
1603 private boolean isRadioOnForSubscriber(int subId) {
Malcolm Chend965c8b2018-02-28 15:00:40 -08001604 final long identity = Binder.clearCallingIdentity();
1605 try {
1606 final Phone phone = getPhone(subId);
1607 if (phone != null) {
1608 return phone.getServiceState().getState() != ServiceState.STATE_POWER_OFF;
1609 } else {
1610 return false;
1611 }
1612 } finally {
1613 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07001614 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001615 }
1616
1617 public void toggleRadioOnOff() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001618 toggleRadioOnOffForSubscriber(getDefaultSubscription());
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001619 }
Wink Saville36469e72014-06-11 15:17:00 -07001620
Wink Savilleb564aae2014-10-23 10:18:09 -07001621 public void toggleRadioOnOffForSubscriber(int subId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001622 enforceModifyPermission();
Malcolm Chend965c8b2018-02-28 15:00:40 -08001623
1624 final long identity = Binder.clearCallingIdentity();
1625 try {
1626 final Phone phone = getPhone(subId);
1627 if (phone != null) {
1628 phone.setRadioPower(!isRadioOnForSubscriber(subId));
1629 }
1630 } finally {
1631 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07001632 }
Wink Saville36469e72014-06-11 15:17:00 -07001633 }
1634
1635 public boolean setRadio(boolean turnOn) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001636 return setRadioForSubscriber(getDefaultSubscription(), turnOn);
Wink Saville36469e72014-06-11 15:17:00 -07001637 }
1638
Wink Savilleb564aae2014-10-23 10:18:09 -07001639 public boolean setRadioForSubscriber(int subId, boolean turnOn) {
Wink Saville36469e72014-06-11 15:17:00 -07001640 enforceModifyPermission();
Malcolm Chend965c8b2018-02-28 15:00:40 -08001641
1642 final long identity = Binder.clearCallingIdentity();
1643 try {
1644 final Phone phone = getPhone(subId);
1645 if (phone == null) {
1646 return false;
1647 }
1648 if ((phone.getServiceState().getState() != ServiceState.STATE_POWER_OFF) != turnOn) {
1649 toggleRadioOnOffForSubscriber(subId);
1650 }
1651 return true;
1652 } finally {
1653 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07001654 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001655 }
Wink Saville36469e72014-06-11 15:17:00 -07001656
Naveen Kalla1fd79bd2014-08-08 00:48:59 -07001657 public boolean needMobileRadioShutdown() {
1658 /*
1659 * If any of the Radios are available, it will need to be
1660 * shutdown. So return true if any Radio is available.
1661 */
Malcolm Chend965c8b2018-02-28 15:00:40 -08001662 final long identity = Binder.clearCallingIdentity();
1663 try {
1664 for (int i = 0; i < TelephonyManager.getDefault().getPhoneCount(); i++) {
1665 Phone phone = PhoneFactory.getPhone(i);
1666 if (phone != null && phone.isRadioAvailable()) return true;
1667 }
1668 logv(TelephonyManager.getDefault().getPhoneCount() + " Phones are shutdown.");
1669 return false;
1670 } finally {
1671 Binder.restoreCallingIdentity(identity);
Naveen Kalla1fd79bd2014-08-08 00:48:59 -07001672 }
Naveen Kalla1fd79bd2014-08-08 00:48:59 -07001673 }
1674
Malcolm Chend965c8b2018-02-28 15:00:40 -08001675 @Override
Naveen Kalla1fd79bd2014-08-08 00:48:59 -07001676 public void shutdownMobileRadios() {
Malcolm Chend965c8b2018-02-28 15:00:40 -08001677 enforceModifyPermission();
1678
1679 final long identity = Binder.clearCallingIdentity();
1680 try {
1681 for (int i = 0; i < TelephonyManager.getDefault().getPhoneCount(); i++) {
1682 logv("Shutting down Phone " + i);
1683 shutdownRadioUsingPhoneId(i);
1684 }
1685 } finally {
1686 Binder.restoreCallingIdentity(identity);
Naveen Kalla1fd79bd2014-08-08 00:48:59 -07001687 }
1688 }
1689
1690 private void shutdownRadioUsingPhoneId(int phoneId) {
Naveen Kalla1fd79bd2014-08-08 00:48:59 -07001691 Phone phone = PhoneFactory.getPhone(phoneId);
1692 if (phone != null && phone.isRadioAvailable()) {
1693 phone.shutdownRadio();
1694 }
1695 }
1696
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001697 public boolean setRadioPower(boolean turnOn) {
Jack Yub4e16162017-05-15 12:48:40 -07001698 enforceModifyPermission();
Malcolm Chend965c8b2018-02-28 15:00:40 -08001699
1700 final long identity = Binder.clearCallingIdentity();
1701 try {
1702 final Phone defaultPhone = PhoneFactory.getDefaultPhone();
1703 if (defaultPhone != null) {
1704 defaultPhone.setRadioPower(turnOn);
1705 return true;
1706 } else {
1707 loge("There's no default phone.");
1708 return false;
1709 }
1710 } finally {
1711 Binder.restoreCallingIdentity(identity);
Wei Liu9ae2a062016-08-08 11:09:34 -07001712 }
Wink Saville36469e72014-06-11 15:17:00 -07001713 }
1714
Wink Savilleb564aae2014-10-23 10:18:09 -07001715 public boolean setRadioPowerForSubscriber(int subId, boolean turnOn) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001716 enforceModifyPermission();
Malcolm Chend965c8b2018-02-28 15:00:40 -08001717
1718 final long identity = Binder.clearCallingIdentity();
1719 try {
1720 final Phone phone = getPhone(subId);
1721 if (phone != null) {
1722 phone.setRadioPower(turnOn);
1723 return true;
1724 } else {
1725 return false;
1726 }
1727 } finally {
1728 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07001729 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001730 }
1731
Wink Saville36469e72014-06-11 15:17:00 -07001732 // FIXME: subId version needed
Sanket Padawe356d7632015-06-22 14:03:32 -07001733 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001734 public boolean enableDataConnectivity() {
1735 enforceModifyPermission();
Malcolm Chend965c8b2018-02-28 15:00:40 -08001736
1737 final long identity = Binder.clearCallingIdentity();
1738 try {
1739 int subId = mSubscriptionController.getDefaultDataSubId();
1740 final Phone phone = getPhone(subId);
1741 if (phone != null) {
Jack Yu7a030e52018-12-13 11:51:28 -08001742 phone.getDataEnabledSettings().setUserDataEnabled(true);
Malcolm Chend965c8b2018-02-28 15:00:40 -08001743 return true;
1744 } else {
1745 return false;
1746 }
1747 } finally {
1748 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07001749 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001750 }
1751
Wink Saville36469e72014-06-11 15:17:00 -07001752 // FIXME: subId version needed
Sanket Padawe356d7632015-06-22 14:03:32 -07001753 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001754 public boolean disableDataConnectivity() {
1755 enforceModifyPermission();
Malcolm Chend965c8b2018-02-28 15:00:40 -08001756
1757 final long identity = Binder.clearCallingIdentity();
1758 try {
1759 int subId = mSubscriptionController.getDefaultDataSubId();
1760 final Phone phone = getPhone(subId);
1761 if (phone != null) {
Jack Yu7a030e52018-12-13 11:51:28 -08001762 phone.getDataEnabledSettings().setUserDataEnabled(false);
Malcolm Chend965c8b2018-02-28 15:00:40 -08001763 return true;
1764 } else {
1765 return false;
1766 }
1767 } finally {
1768 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07001769 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001770 }
1771
Sanket Padawe356d7632015-06-22 14:03:32 -07001772 @Override
Jack Yuacf8a132017-05-01 17:00:48 -07001773 public boolean isDataConnectivityPossible(int subId) {
Malcolm Chend965c8b2018-02-28 15:00:40 -08001774 final long identity = Binder.clearCallingIdentity();
1775 try {
1776 final Phone phone = getPhone(subId);
1777 if (phone != null) {
Jack Yu311536f2018-11-26 11:20:48 -08001778 return phone.isDataAllowed(ApnSetting.TYPE_DEFAULT);
Malcolm Chend965c8b2018-02-28 15:00:40 -08001779 } else {
1780 return false;
1781 }
1782 } finally {
1783 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07001784 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001785 }
1786
1787 public boolean handlePinMmi(String dialString) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001788 return handlePinMmiForSubscriber(getDefaultSubscription(), dialString);
Wink Saville36469e72014-06-11 15:17:00 -07001789 }
1790
pkanwarae03a6b2016-11-06 20:37:09 -08001791 public void handleUssdRequest(int subId, String ussdRequest, ResultReceiver wrappedCallback) {
Malcolm Chend965c8b2018-02-28 15:00:40 -08001792 enforceCallPermission();
1793
1794 final long identity = Binder.clearCallingIdentity();
1795 try {
1796 if (!SubscriptionManager.isValidSubscriptionId(subId)) {
1797 return;
1798 }
1799 Pair<String, ResultReceiver> ussdObject = new Pair(ussdRequest, wrappedCallback);
1800 sendRequest(CMD_HANDLE_USSD_REQUEST, ussdObject, subId);
1801 } finally {
1802 Binder.restoreCallingIdentity(identity);
1803 }
pkanwar32d516d2016-10-14 19:37:38 -07001804 };
1805
Wink Savilleb564aae2014-10-23 10:18:09 -07001806 public boolean handlePinMmiForSubscriber(int subId, String dialString) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001807 enforceModifyPermission();
Malcolm Chend965c8b2018-02-28 15:00:40 -08001808
1809 final long identity = Binder.clearCallingIdentity();
1810 try {
1811 if (!SubscriptionManager.isValidSubscriptionId(subId)) {
1812 return false;
1813 }
1814 return (Boolean) sendRequest(CMD_HANDLE_PIN_MMI, dialString, subId);
1815 } finally {
1816 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07001817 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001818 }
1819
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001820 public int getCallState() {
Shishir Agrawala9f32182016-04-12 12:00:16 -07001821 return getCallStateForSlot(getSlotForDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001822 }
1823
Sanket Padawe13bac7b2017-03-20 15:04:47 -07001824 public int getCallStateForSlot(int slotIndex) {
Malcolm Chend965c8b2018-02-28 15:00:40 -08001825 final long identity = Binder.clearCallingIdentity();
1826 try {
1827 Phone phone = PhoneFactory.getPhone(slotIndex);
1828 return phone == null ? TelephonyManager.CALL_STATE_IDLE :
1829 PhoneConstantConversions.convertCallState(phone.getState());
1830 } finally {
1831 Binder.restoreCallingIdentity(identity);
1832 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001833 }
1834
Sanket Padawe356d7632015-06-22 14:03:32 -07001835 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001836 public int getDataState() {
Malcolm Chend965c8b2018-02-28 15:00:40 -08001837 final long identity = Binder.clearCallingIdentity();
1838 try {
1839 Phone phone = getPhone(mSubscriptionController.getDefaultDataSubId());
1840 if (phone != null) {
1841 return PhoneConstantConversions.convertDataState(phone.getDataConnectionState());
1842 } else {
1843 return PhoneConstantConversions.convertDataState(
1844 PhoneConstants.DataState.DISCONNECTED);
1845 }
1846 } finally {
1847 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07001848 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001849 }
1850
Sanket Padawe356d7632015-06-22 14:03:32 -07001851 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001852 public int getDataActivity() {
Malcolm Chend965c8b2018-02-28 15:00:40 -08001853 final long identity = Binder.clearCallingIdentity();
1854 try {
1855 Phone phone = getPhone(mSubscriptionController.getDefaultDataSubId());
1856 if (phone != null) {
1857 return DefaultPhoneNotifier.convertDataActivityState(phone.getDataActivityState());
1858 } else {
1859 return TelephonyManager.DATA_ACTIVITY_NONE;
1860 }
1861 } finally {
1862 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07001863 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001864 }
1865
1866 @Override
Svetoslav64fad262015-04-14 14:35:21 -07001867 public Bundle getCellLocation(String callingPackage) {
Nazanin Bakhshi2f2cc692018-12-11 15:15:39 -08001868 mApp.getSystemService(AppOpsManager.class)
Hall Liu1aa510f2017-11-22 17:40:08 -08001869 .checkPackage(Binder.getCallingUid(), callingPackage);
Hall Liuf19c44f2018-11-27 14:38:17 -08001870
1871 LocationAccessPolicy.LocationPermissionResult locationResult =
1872 LocationAccessPolicy.checkLocationPermission(mApp,
1873 new LocationAccessPolicy.LocationPermissionQuery.Builder()
1874 .setCallingPackage(callingPackage)
1875 .setCallingPid(Binder.getCallingPid())
1876 .setCallingUid(Binder.getCallingUid())
1877 .setMethod("getCellLocation")
1878 .setMinSdkVersionForFine(Build.VERSION_CODES.Q)
1879 .build());
1880 switch (locationResult) {
1881 case DENIED_HARD:
1882 throw new SecurityException("Not allowed to access cell location");
1883 case DENIED_SOFT:
1884 return new Bundle();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001885 }
1886
Narayan Kamathf04b5a12018-01-09 11:47:15 +00001887 WorkSource workSource = getWorkSource(Binder.getCallingUid());
Malcolm Chend965c8b2018-02-28 15:00:40 -08001888 final long identity = Binder.clearCallingIdentity();
1889 try {
1890 if (DBG_LOC) log("getCellLocation: is active user");
1891 Bundle data = new Bundle();
Nathan Harold3ff88932018-08-14 10:19:49 -07001892 int subId = mSubscriptionController.getDefaultDataSubId();
1893 CellLocation cl = (CellLocation) sendRequest(CMD_GET_CELL_LOCATION, workSource, subId);
1894 cl.fillInNotifierBundle(data);
Malcolm Chend965c8b2018-02-28 15:00:40 -08001895 return data;
1896 } finally {
1897 Binder.restoreCallingIdentity(identity);
1898 }
Svetoslav64fad262015-04-14 14:35:21 -07001899 }
1900
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001901 @Override
Jonathan Basseribf5362b2017-07-19 12:22:35 -07001902 public String getNetworkCountryIsoForPhone(int phoneId) {
1903 // Reporting the correct network country is ambiguous when IWLAN could conflict with
1904 // registered cell info, so return a NULL country instead.
1905 final long identity = Binder.clearCallingIdentity();
1906 try {
Malcolm Chen3732c2b2018-07-18 20:15:24 -07001907 if (phoneId == SubscriptionManager.INVALID_PHONE_INDEX) {
1908 // Get default phone in this case.
1909 phoneId = SubscriptionManager.DEFAULT_PHONE_INDEX;
1910 }
Jonathan Basseribf5362b2017-07-19 12:22:35 -07001911 final int subId = mSubscriptionController.getSubIdUsingPhoneId(phoneId);
Jack Yu5f7092c2018-04-13 14:05:37 -07001912 // Todo: fix this when we can get the actual cellular network info when the device
1913 // is on IWLAN.
Jonathan Basseribf5362b2017-07-19 12:22:35 -07001914 if (TelephonyManager.NETWORK_TYPE_IWLAN
1915 == getVoiceNetworkTypeForSubscriber(subId, mApp.getPackageName())) {
1916 return "";
1917 }
Malcolm Chend965c8b2018-02-28 15:00:40 -08001918 Phone phone = PhoneFactory.getPhone(phoneId);
1919 if (phone != null) {
1920 ServiceStateTracker sst = phone.getServiceStateTracker();
1921 if (sst != null) {
1922 LocaleTracker lt = sst.getLocaleTracker();
1923 if (lt != null) {
1924 return lt.getCurrentCountry();
1925 }
1926 }
1927 }
1928 return "";
Jonathan Basseribf5362b2017-07-19 12:22:35 -07001929 } finally {
1930 Binder.restoreCallingIdentity(identity);
1931 }
Jonathan Basseribf5362b2017-07-19 12:22:35 -07001932 }
1933
1934 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001935 public void enableLocationUpdates() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001936 enableLocationUpdatesForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001937 }
1938
Sanket Padawe356d7632015-06-22 14:03:32 -07001939 @Override
Wink Savilleb564aae2014-10-23 10:18:09 -07001940 public void enableLocationUpdatesForSubscriber(int subId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001941 mApp.enforceCallingOrSelfPermission(
1942 android.Manifest.permission.CONTROL_LOCATION_UPDATES, null);
Malcolm Chend965c8b2018-02-28 15:00:40 -08001943
1944 final long identity = Binder.clearCallingIdentity();
1945 try {
1946 final Phone phone = getPhone(subId);
1947 if (phone != null) {
1948 phone.enableLocationUpdates();
1949 }
1950 } finally {
1951 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07001952 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001953 }
1954
1955 @Override
1956 public void disableLocationUpdates() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001957 disableLocationUpdatesForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001958 }
1959
Sanket Padawe356d7632015-06-22 14:03:32 -07001960 @Override
Wink Savilleb564aae2014-10-23 10:18:09 -07001961 public void disableLocationUpdatesForSubscriber(int subId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001962 mApp.enforceCallingOrSelfPermission(
1963 android.Manifest.permission.CONTROL_LOCATION_UPDATES, null);
Malcolm Chend965c8b2018-02-28 15:00:40 -08001964
1965 final long identity = Binder.clearCallingIdentity();
1966 try {
1967 final Phone phone = getPhone(subId);
1968 if (phone != null) {
1969 phone.disableLocationUpdates();
1970 }
1971 } finally {
1972 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07001973 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001974 }
1975
Nathan Harold31d7ff32018-10-15 20:20:30 -07001976 /**
1977 * Returns the target SDK version number for a given package name.
1978 *
1979 * @return target SDK if the package is found or INT_MAX.
1980 */
1981 private int getTargetSdk(String packageName) {
1982 try {
Nazanin Bakhshi2f2cc692018-12-11 15:15:39 -08001983 final ApplicationInfo ai = mApp.getPackageManager().getApplicationInfo(
1984 packageName, 0);
Nathan Harold31d7ff32018-10-15 20:20:30 -07001985 if (ai != null) return ai.targetSdkVersion;
1986 } catch (PackageManager.NameNotFoundException unexpected) {
1987 }
1988 return Integer.MAX_VALUE;
1989 }
1990
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001991 @Override
1992 @SuppressWarnings("unchecked")
Nathan Harold31d7ff32018-10-15 20:20:30 -07001993 public List<NeighboringCellInfo> getNeighboringCellInfo(String callingPackage) {
1994 final int targetSdk = getTargetSdk(callingPackage);
Nathan Harolddbea45a2018-08-30 14:35:07 -07001995 if (targetSdk >= android.os.Build.VERSION_CODES.Q) {
1996 throw new SecurityException(
1997 "getNeighboringCellInfo() is unavailable to callers targeting Q+ SDK levels.");
1998 }
Nathan Haroldb4d55612018-07-20 13:13:08 -07001999
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002000 if (mAppOps.noteOp(AppOpsManager.OP_NEIGHBORING_CELLS, Binder.getCallingUid(),
2001 callingPackage) != AppOpsManager.MODE_ALLOWED) {
2002 return null;
2003 }
Svetoslav64fad262015-04-14 14:35:21 -07002004
Svetoslav Ganov4a9d4482017-06-20 19:53:35 -07002005 if (DBG_LOC) log("getNeighboringCellInfo: is active user");
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002006
Nathan Haroldf180aac2018-06-01 18:43:55 -07002007 List<CellInfo> info = getAllCellInfo(callingPackage);
2008 if (info == null) return null;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002009
Nathan Haroldf180aac2018-06-01 18:43:55 -07002010 List<NeighboringCellInfo> neighbors = new ArrayList<NeighboringCellInfo>();
2011 for (CellInfo ci : info) {
2012 if (ci instanceof CellInfoGsm) {
2013 neighbors.add(new NeighboringCellInfo((CellInfoGsm) ci));
2014 } else if (ci instanceof CellInfoWcdma) {
2015 neighbors.add(new NeighboringCellInfo((CellInfoWcdma) ci));
2016 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002017 }
Nathan Haroldf180aac2018-06-01 18:43:55 -07002018 return (neighbors.size()) > 0 ? neighbors : null;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002019 }
2020
Nathan Haroldfa8da0f2018-09-27 18:51:29 -07002021 private List<CellInfo> getCachedCellInfo() {
2022 List<CellInfo> cellInfos = new ArrayList<CellInfo>();
2023 for (Phone phone : PhoneFactory.getPhones()) {
2024 List<CellInfo> info = phone.getAllCellInfo();
2025 if (info != null) cellInfos.addAll(info);
2026 }
2027 return cellInfos;
2028 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002029
2030 @Override
Svetoslav64fad262015-04-14 14:35:21 -07002031 public List<CellInfo> getAllCellInfo(String callingPackage) {
Nazanin Bakhshi2f2cc692018-12-11 15:15:39 -08002032 mApp.getSystemService(AppOpsManager.class)
Hall Liu1aa510f2017-11-22 17:40:08 -08002033 .checkPackage(Binder.getCallingUid(), callingPackage);
Hall Liuf19c44f2018-11-27 14:38:17 -08002034
2035 LocationAccessPolicy.LocationPermissionResult locationResult =
2036 LocationAccessPolicy.checkLocationPermission(mApp,
2037 new LocationAccessPolicy.LocationPermissionQuery.Builder()
2038 .setCallingPackage(callingPackage)
2039 .setCallingPid(Binder.getCallingPid())
2040 .setCallingUid(Binder.getCallingUid())
2041 .setMethod("getAllCellInfo")
Nathan Harold5ae50b52019-02-20 15:46:36 -08002042 .setMinSdkVersionForCoarse(Build.VERSION_CODES.BASE)
Hall Liuf19c44f2018-11-27 14:38:17 -08002043 .setMinSdkVersionForFine(Build.VERSION_CODES.Q)
2044 .build());
2045 switch (locationResult) {
2046 case DENIED_HARD:
2047 throw new SecurityException("Not allowed to access cell info");
2048 case DENIED_SOFT:
2049 return new ArrayList<>();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002050 }
2051
Nathan Haroldfa8da0f2018-09-27 18:51:29 -07002052 final int targetSdk = getTargetSdk(callingPackage);
2053 if (targetSdk >= android.os.Build.VERSION_CODES.Q) {
2054 return getCachedCellInfo();
2055 }
2056
Svetoslav Ganov4a9d4482017-06-20 19:53:35 -07002057 if (DBG_LOC) log("getAllCellInfo: is active user");
Narayan Kamathf04b5a12018-01-09 11:47:15 +00002058 WorkSource workSource = getWorkSource(Binder.getCallingUid());
Malcolm Chend965c8b2018-02-28 15:00:40 -08002059 final long identity = Binder.clearCallingIdentity();
2060 try {
2061 List<CellInfo> cellInfos = new ArrayList<CellInfo>();
2062 for (Phone phone : PhoneFactory.getPhones()) {
Nathan Harold3ff88932018-08-14 10:19:49 -07002063 final List<CellInfo> info = (List<CellInfo>) sendRequest(
Nathan Harold92bed182018-10-12 18:16:49 -07002064 CMD_GET_ALL_CELL_INFO, null, phone, workSource);
Malcolm Chend965c8b2018-02-28 15:00:40 -08002065 if (info != null) cellInfos.addAll(info);
2066 }
2067 return cellInfos;
2068 } finally {
2069 Binder.restoreCallingIdentity(identity);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002070 }
2071 }
2072
Sailesh Nepalbd76e4e2013-10-27 13:59:44 -07002073 @Override
Nathan Haroldfa8da0f2018-09-27 18:51:29 -07002074 public void requestCellInfoUpdate(int subId, ICellInfoCallback cb, String callingPackage) {
2075 requestCellInfoUpdateInternal(
2076 subId, cb, callingPackage, getWorkSource(Binder.getCallingUid()));
2077 }
2078
2079 @Override
2080 public void requestCellInfoUpdateWithWorkSource(
2081 int subId, ICellInfoCallback cb, String callingPackage, WorkSource workSource) {
2082 enforceModifyPermission();
2083 requestCellInfoUpdateInternal(subId, cb, callingPackage, workSource);
2084 }
2085
2086 private void requestCellInfoUpdateInternal(
2087 int subId, ICellInfoCallback cb, String callingPackage, WorkSource workSource) {
Nazanin Bakhshi2f2cc692018-12-11 15:15:39 -08002088 mApp.getSystemService(AppOpsManager.class)
Nathan Haroldfa8da0f2018-09-27 18:51:29 -07002089 .checkPackage(Binder.getCallingUid(), callingPackage);
Hall Liuf19c44f2018-11-27 14:38:17 -08002090
2091 LocationAccessPolicy.LocationPermissionResult locationResult =
2092 LocationAccessPolicy.checkLocationPermission(mApp,
2093 new LocationAccessPolicy.LocationPermissionQuery.Builder()
2094 .setCallingPackage(callingPackage)
2095 .setCallingPid(Binder.getCallingPid())
2096 .setCallingUid(Binder.getCallingUid())
2097 .setMethod("requestCellInfoUpdate")
2098 .setMinSdkVersionForFine(Build.VERSION_CODES.Q)
2099 .build());
2100 switch (locationResult) {
2101 case DENIED_HARD:
2102 throw new SecurityException("Not allowed to access cell info");
2103 case DENIED_SOFT:
2104 return;
Nathan Haroldfa8da0f2018-09-27 18:51:29 -07002105 }
2106
2107 final Phone phone = getPhone(subId);
2108 if (phone == null) throw new IllegalArgumentException("Invalid Subscription Id: " + subId);
2109
2110 sendRequestAsync(CMD_REQUEST_CELL_INFO_UPDATE, cb, phone, workSource);
2111 }
2112
2113 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002114 public void setCellInfoListRate(int rateInMillis) {
Jack Yua8d8cb82017-01-16 10:15:34 -08002115 enforceModifyPermission();
Narayan Kamathf04b5a12018-01-09 11:47:15 +00002116 WorkSource workSource = getWorkSource(Binder.getCallingUid());
Malcolm Chend965c8b2018-02-28 15:00:40 -08002117
2118 final long identity = Binder.clearCallingIdentity();
2119 try {
Nazanin Bakhshi2f2cc692018-12-11 15:15:39 -08002120 getDefaultPhone().setCellInfoListRate(rateInMillis, workSource);
Malcolm Chend965c8b2018-02-28 15:00:40 -08002121 } finally {
2122 Binder.restoreCallingIdentity(identity);
2123 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002124 }
2125
Shishir Agrawala9f32182016-04-12 12:00:16 -07002126 @Override
Sanket Padawe13bac7b2017-03-20 15:04:47 -07002127 public String getImeiForSlot(int slotIndex, String callingPackage) {
Jeff Davidson913390f2018-02-23 17:11:49 -08002128 Phone phone = PhoneFactory.getPhone(slotIndex);
2129 if (phone == null) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08002130 return null;
2131 }
Jeff Davidson913390f2018-02-23 17:11:49 -08002132 int subId = phone.getSubId();
2133 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
2134 mApp, subId, callingPackage, "getImeiForSlot")) {
2135 return null;
2136 }
Malcolm Chend965c8b2018-02-28 15:00:40 -08002137
2138 final long identity = Binder.clearCallingIdentity();
2139 try {
2140 return phone.getImei();
2141 } finally {
2142 Binder.restoreCallingIdentity(identity);
2143 }
Shishir Agrawala9f32182016-04-12 12:00:16 -07002144 }
2145
2146 @Override
David Kelly5e06a7f2018-03-12 14:10:59 +00002147 public String getTypeAllocationCodeForSlot(int slotIndex) {
2148 Phone phone = PhoneFactory.getPhone(slotIndex);
2149 String tac = null;
2150 if (phone != null) {
2151 String imei = phone.getImei();
2152 tac = imei == null ? null : imei.substring(0, TYPE_ALLOCATION_CODE_LENGTH);
2153 }
2154 return tac;
2155 }
2156
2157 @Override
Jack Yu2af8d712017-03-15 17:14:14 -07002158 public String getMeidForSlot(int slotIndex, String callingPackage) {
Jeff Davidson913390f2018-02-23 17:11:49 -08002159 Phone phone = PhoneFactory.getPhone(slotIndex);
2160 if (phone == null) {
Jack Yu2af8d712017-03-15 17:14:14 -07002161 return null;
2162 }
Nazanin Bakhshi2f2cc692018-12-11 15:15:39 -08002163
Jeff Davidson913390f2018-02-23 17:11:49 -08002164 int subId = phone.getSubId();
2165 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
2166 mApp, subId, callingPackage, "getMeidForSlot")) {
2167 return null;
2168 }
Malcolm Chend965c8b2018-02-28 15:00:40 -08002169
2170 final long identity = Binder.clearCallingIdentity();
2171 try {
2172 return phone.getMeid();
2173 } finally {
2174 Binder.restoreCallingIdentity(identity);
2175 }
Jack Yu2af8d712017-03-15 17:14:14 -07002176 }
2177
2178 @Override
David Kelly5e06a7f2018-03-12 14:10:59 +00002179 public String getManufacturerCodeForSlot(int slotIndex) {
2180 Phone phone = PhoneFactory.getPhone(slotIndex);
2181 String manufacturerCode = null;
2182 if (phone != null) {
2183 String meid = phone.getMeid();
2184 manufacturerCode = meid == null ? null : meid.substring(0, MANUFACTURER_CODE_LENGTH);
2185 }
2186 return manufacturerCode;
2187 }
2188
2189 @Override
Sanket Padawe13bac7b2017-03-20 15:04:47 -07002190 public String getDeviceSoftwareVersionForSlot(int slotIndex, String callingPackage) {
Jeff Davidson913390f2018-02-23 17:11:49 -08002191 Phone phone = PhoneFactory.getPhone(slotIndex);
2192 if (phone == null) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08002193 return null;
2194 }
Jeff Davidson913390f2018-02-23 17:11:49 -08002195 int subId = phone.getSubId();
2196 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
2197 mApp, subId, callingPackage, "getDeviceSoftwareVersionForSlot")) {
2198 return null;
2199 }
Malcolm Chend965c8b2018-02-28 15:00:40 -08002200
2201 final long identity = Binder.clearCallingIdentity();
2202 try {
2203 return phone.getDeviceSvn();
2204 } finally {
2205 Binder.restoreCallingIdentity(identity);
2206 }
Shishir Agrawala9f32182016-04-12 12:00:16 -07002207 }
2208
fionaxu43304da2017-11-27 22:51:16 -08002209 @Override
2210 public int getSubscriptionCarrierId(int subId) {
Malcolm Chend965c8b2018-02-28 15:00:40 -08002211 final long identity = Binder.clearCallingIdentity();
2212 try {
2213 final Phone phone = getPhone(subId);
2214 return phone == null ? TelephonyManager.UNKNOWN_CARRIER_ID : phone.getCarrierId();
2215 } finally {
2216 Binder.restoreCallingIdentity(identity);
2217 }
fionaxu43304da2017-11-27 22:51:16 -08002218 }
2219
2220 @Override
2221 public String getSubscriptionCarrierName(int subId) {
Malcolm Chend965c8b2018-02-28 15:00:40 -08002222 final long identity = Binder.clearCallingIdentity();
2223 try {
2224 final Phone phone = getPhone(subId);
2225 return phone == null ? null : phone.getCarrierName();
2226 } finally {
2227 Binder.restoreCallingIdentity(identity);
2228 }
fionaxu43304da2017-11-27 22:51:16 -08002229 }
2230
calvinpaneed9ae82018-11-01 19:43:06 +08002231 @Override
chen xua31f22b2019-03-06 15:28:50 -08002232 public int getSubscriptionSpecificCarrierId(int subId) {
chen xuc93cc282018-11-04 17:17:00 -08002233 final long identity = Binder.clearCallingIdentity();
2234 try {
2235 final Phone phone = getPhone(subId);
2236 return phone == null ? TelephonyManager.UNKNOWN_CARRIER_ID
chen xua31f22b2019-03-06 15:28:50 -08002237 : phone.getSpecificCarrierId();
chen xuc93cc282018-11-04 17:17:00 -08002238 } finally {
2239 Binder.restoreCallingIdentity(identity);
2240 }
2241 }
2242
2243 @Override
chen xua31f22b2019-03-06 15:28:50 -08002244 public String getSubscriptionSpecificCarrierName(int subId) {
chen xuc93cc282018-11-04 17:17:00 -08002245 final long identity = Binder.clearCallingIdentity();
2246 try {
2247 final Phone phone = getPhone(subId);
chen xua31f22b2019-03-06 15:28:50 -08002248 return phone == null ? null : phone.getSpecificCarrierName();
chen xuc93cc282018-11-04 17:17:00 -08002249 } finally {
2250 Binder.restoreCallingIdentity(identity);
2251 }
2252 }
2253
chen xu02581692018-11-11 19:03:44 -08002254 @Override
chen xu4ca4e692018-12-06 22:10:03 -08002255 public int getCarrierIdFromMccMnc(int slotIndex, String mccmnc, boolean isSubscriptionMccMnc) {
2256 if (!isSubscriptionMccMnc) {
2257 enforceReadPrivilegedPermission("getCarrierIdFromMccMnc");
2258 }
chen xu02581692018-11-11 19:03:44 -08002259 final Phone phone = PhoneFactory.getPhone(slotIndex);
2260 if (phone == null) {
2261 return TelephonyManager.UNKNOWN_CARRIER_ID;
2262 }
2263 final long identity = Binder.clearCallingIdentity();
2264 try {
2265 return CarrierResolver.getCarrierIdFromMccMnc(phone.getContext(), mccmnc);
2266 } finally {
2267 Binder.restoreCallingIdentity(identity);
2268 }
2269 }
2270
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002271 //
2272 // Internal helper methods.
2273 //
2274
Sanket Padaweee13a9b2016-03-08 17:30:28 -08002275 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002276 * Make sure the caller has the MODIFY_PHONE_STATE permission.
2277 *
2278 * @throws SecurityException if the caller does not have the required permission
2279 */
2280 private void enforceModifyPermission() {
2281 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.MODIFY_PHONE_STATE, null);
2282 }
2283
2284 /**
2285 * Make sure the caller has the CALL_PHONE permission.
2286 *
2287 * @throws SecurityException if the caller does not have the required permission
2288 */
2289 private void enforceCallPermission() {
2290 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.CALL_PHONE, null);
2291 }
2292
Stuart Scott8eef64f2015-04-08 15:13:54 -07002293 private void enforceConnectivityInternalPermission() {
Nazanin Bakhshi2f2cc692018-12-11 15:15:39 -08002294 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.CONNECTIVITY_INTERNAL,
Stuart Scott8eef64f2015-04-08 15:13:54 -07002295 "ConnectivityService");
2296 }
2297
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002298 private String createTelUrl(String number) {
2299 if (TextUtils.isEmpty(number)) {
2300 return null;
2301 }
2302
Jake Hambye994d462014-02-03 13:10:13 -08002303 return "tel:" + number;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002304 }
2305
Ihab Awadf9e92732013-12-05 18:02:52 -08002306 private static void log(String msg) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002307 Log.d(LOG_TAG, "[PhoneIntfMgr] " + msg);
2308 }
2309
Naveen Kalla1fd79bd2014-08-08 00:48:59 -07002310 private static void logv(String msg) {
2311 Log.v(LOG_TAG, "[PhoneIntfMgr] " + msg);
2312 }
2313
Ihab Awadf9e92732013-12-05 18:02:52 -08002314 private static void loge(String msg) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002315 Log.e(LOG_TAG, "[PhoneIntfMgr] " + msg);
2316 }
2317
Robert Greenwalt36b23af2015-07-06 17:59:14 -07002318 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002319 public int getActivePhoneType() {
Shishir Agrawala9f32182016-04-12 12:00:16 -07002320 return getActivePhoneTypeForSlot(getSlotForDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07002321 }
2322
Sanket Padawe356d7632015-06-22 14:03:32 -07002323 @Override
Sanket Padawe13bac7b2017-03-20 15:04:47 -07002324 public int getActivePhoneTypeForSlot(int slotIndex) {
Malcolm Chend965c8b2018-02-28 15:00:40 -08002325 final long identity = Binder.clearCallingIdentity();
2326 try {
2327 final Phone phone = PhoneFactory.getPhone(slotIndex);
2328 if (phone == null) {
2329 return PhoneConstants.PHONE_TYPE_NONE;
2330 } else {
2331 return phone.getPhoneType();
2332 }
2333 } finally {
2334 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07002335 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002336 }
2337
2338 /**
2339 * Returns the CDMA ERI icon index to display
2340 */
Robert Greenwalt36b23af2015-07-06 17:59:14 -07002341 @Override
2342 public int getCdmaEriIconIndex(String callingPackage) {
2343 return getCdmaEriIconIndexForSubscriber(getDefaultSubscription(), callingPackage);
Wink Saville36469e72014-06-11 15:17:00 -07002344 }
2345
Sanket Padawe356d7632015-06-22 14:03:32 -07002346 @Override
Robert Greenwalt36b23af2015-07-06 17:59:14 -07002347 public int getCdmaEriIconIndexForSubscriber(int subId, String callingPackage) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08002348 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
Jeff Davidson913390f2018-02-23 17:11:49 -08002349 mApp, subId, callingPackage, "getCdmaEriIconIndexForSubscriber")) {
Robert Greenwalt36b23af2015-07-06 17:59:14 -07002350 return -1;
2351 }
Malcolm Chend965c8b2018-02-28 15:00:40 -08002352
2353 final long identity = Binder.clearCallingIdentity();
2354 try {
2355 final Phone phone = getPhone(subId);
2356 if (phone != null) {
2357 return phone.getCdmaEriIconIndex();
2358 } else {
2359 return -1;
2360 }
2361 } finally {
2362 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07002363 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002364 }
2365
2366 /**
2367 * Returns the CDMA ERI icon mode,
2368 * 0 - ON
2369 * 1 - FLASHING
2370 */
Robert Greenwalt36b23af2015-07-06 17:59:14 -07002371 @Override
2372 public int getCdmaEriIconMode(String callingPackage) {
2373 return getCdmaEriIconModeForSubscriber(getDefaultSubscription(), callingPackage);
Wink Saville36469e72014-06-11 15:17:00 -07002374 }
2375
Sanket Padawe356d7632015-06-22 14:03:32 -07002376 @Override
Robert Greenwalt36b23af2015-07-06 17:59:14 -07002377 public int getCdmaEriIconModeForSubscriber(int subId, String callingPackage) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08002378 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
Jeff Davidson913390f2018-02-23 17:11:49 -08002379 mApp, subId, callingPackage, "getCdmaEriIconModeForSubscriber")) {
Robert Greenwalt36b23af2015-07-06 17:59:14 -07002380 return -1;
2381 }
Malcolm Chend965c8b2018-02-28 15:00:40 -08002382
2383 final long identity = Binder.clearCallingIdentity();
2384 try {
2385 final Phone phone = getPhone(subId);
2386 if (phone != null) {
2387 return phone.getCdmaEriIconMode();
2388 } else {
2389 return -1;
2390 }
2391 } finally {
2392 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07002393 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002394 }
2395
2396 /**
2397 * Returns the CDMA ERI text,
2398 */
Robert Greenwalt36b23af2015-07-06 17:59:14 -07002399 @Override
2400 public String getCdmaEriText(String callingPackage) {
2401 return getCdmaEriTextForSubscriber(getDefaultSubscription(), callingPackage);
Wink Saville36469e72014-06-11 15:17:00 -07002402 }
2403
Sanket Padawe356d7632015-06-22 14:03:32 -07002404 @Override
Robert Greenwalt36b23af2015-07-06 17:59:14 -07002405 public String getCdmaEriTextForSubscriber(int subId, String callingPackage) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08002406 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
Jeff Davidson913390f2018-02-23 17:11:49 -08002407 mApp, subId, callingPackage, "getCdmaEriIconTextForSubscriber")) {
Robert Greenwalt36b23af2015-07-06 17:59:14 -07002408 return null;
2409 }
Malcolm Chend965c8b2018-02-28 15:00:40 -08002410
2411 final long identity = Binder.clearCallingIdentity();
2412 try {
2413 final Phone phone = getPhone(subId);
2414 if (phone != null) {
2415 return phone.getCdmaEriText();
2416 } else {
2417 return null;
2418 }
2419 } finally {
2420 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07002421 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002422 }
2423
2424 /**
Junda Liuca05d5d2014-08-14 22:36:34 -07002425 * Returns the CDMA MDN.
2426 */
Sanket Padawe356d7632015-06-22 14:03:32 -07002427 @Override
Wink Savilleb564aae2014-10-23 10:18:09 -07002428 public String getCdmaMdn(int subId) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08002429 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
2430 mApp, subId, "getCdmaMdn");
Malcolm Chend965c8b2018-02-28 15:00:40 -08002431
2432 final long identity = Binder.clearCallingIdentity();
2433 try {
2434 final Phone phone = getPhone(subId);
Nazanin Bakhshi2f2cc692018-12-11 15:15:39 -08002435 if (phone != null && phone.getPhoneType() == PhoneConstants.PHONE_TYPE_CDMA) {
Malcolm Chend965c8b2018-02-28 15:00:40 -08002436 return phone.getLine1Number();
2437 } else {
Nazanin Bakhshi2f2cc692018-12-11 15:15:39 -08002438 loge("getCdmaMdn: no phone found. Invalid subId: " + subId);
Malcolm Chend965c8b2018-02-28 15:00:40 -08002439 return null;
2440 }
2441 } finally {
2442 Binder.restoreCallingIdentity(identity);
Junda Liuca05d5d2014-08-14 22:36:34 -07002443 }
2444 }
2445
2446 /**
2447 * Returns the CDMA MIN.
2448 */
Sanket Padawe356d7632015-06-22 14:03:32 -07002449 @Override
Wink Savilleb564aae2014-10-23 10:18:09 -07002450 public String getCdmaMin(int subId) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08002451 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
2452 mApp, subId, "getCdmaMin");
Malcolm Chend965c8b2018-02-28 15:00:40 -08002453
2454 final long identity = Binder.clearCallingIdentity();
2455 try {
2456 final Phone phone = getPhone(subId);
2457 if (phone != null && phone.getPhoneType() == PhoneConstants.PHONE_TYPE_CDMA) {
2458 return phone.getCdmaMin();
2459 } else {
2460 return null;
2461 }
2462 } finally {
2463 Binder.restoreCallingIdentity(identity);
Junda Liuca05d5d2014-08-14 22:36:34 -07002464 }
2465 }
2466
Hall Liud892bec2018-11-30 14:51:45 -08002467 @Override
2468 public void requestNumberVerification(PhoneNumberRange range, long timeoutMillis,
2469 INumberVerificationCallback callback, String callingPackage) {
2470 if (mApp.checkCallingOrSelfPermission(android.Manifest.permission.MODIFY_PHONE_STATE)
2471 != PERMISSION_GRANTED) {
2472 throw new SecurityException("Caller must hold the MODIFY_PHONE_STATE permission");
2473 }
2474 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
2475
2476 String authorizedPackage = NumberVerificationManager.getAuthorizedPackage(mApp);
2477 if (!TextUtils.equals(callingPackage, authorizedPackage)) {
2478 throw new SecurityException("Calling package must be configured in the device config");
2479 }
2480
2481 if (range == null) {
2482 throw new NullPointerException("Range must be non-null");
2483 }
2484
2485 timeoutMillis = Math.min(timeoutMillis,
Hall Liubd069e32019-02-28 18:56:30 -08002486 TelephonyManager.getMaxNumberVerificationTimeoutMillis());
Hall Liud892bec2018-11-30 14:51:45 -08002487
2488 NumberVerificationManager.getInstance().requestVerification(range, callback, timeoutMillis);
2489 }
2490
Junda Liuca05d5d2014-08-14 22:36:34 -07002491 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002492 * Returns true if CDMA provisioning needs to run.
2493 */
2494 public boolean needsOtaServiceProvisioning() {
Malcolm Chend965c8b2018-02-28 15:00:40 -08002495 final long identity = Binder.clearCallingIdentity();
2496 try {
Nazanin Bakhshi2f2cc692018-12-11 15:15:39 -08002497 return getDefaultPhone().needsOtaServiceProvisioning();
Malcolm Chend965c8b2018-02-28 15:00:40 -08002498 } finally {
2499 Binder.restoreCallingIdentity(identity);
2500 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002501 }
2502
2503 /**
Shishir Agrawal76d5da92014-11-09 16:17:25 -08002504 * Sets the voice mail number of a given subId.
2505 */
2506 @Override
2507 public boolean setVoiceMailNumber(int subId, String alphaTag, String number) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08002508 TelephonyPermissions.enforceCallingOrSelfCarrierPrivilege(subId, "setVoiceMailNumber");
Malcolm Chend965c8b2018-02-28 15:00:40 -08002509
2510 final long identity = Binder.clearCallingIdentity();
2511 try {
2512 Boolean success = (Boolean) sendRequest(CMD_SET_VOICEMAIL_NUMBER,
2513 new Pair<String, String>(alphaTag, number), new Integer(subId));
2514 return success;
2515 } finally {
2516 Binder.restoreCallingIdentity(identity);
2517 }
Shishir Agrawal76d5da92014-11-09 16:17:25 -08002518 }
2519
Ta-wei Yen87c49842016-05-13 21:19:52 -07002520 @Override
Ta-wei Yenc9df0432017-04-17 17:09:07 -07002521 public Bundle getVisualVoicemailSettings(String callingPackage, int subId) {
2522 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
Nazanin Bakhshi2f2cc692018-12-11 15:15:39 -08002523 String systemDialer = TelecomManager.from(mApp).getSystemDialerPackage();
Ta-wei Yenc9df0432017-04-17 17:09:07 -07002524 if (!TextUtils.equals(callingPackage, systemDialer)) {
2525 throw new SecurityException("caller must be system dialer");
2526 }
Malcolm Chend965c8b2018-02-28 15:00:40 -08002527
2528 final long identity = Binder.clearCallingIdentity();
2529 try {
2530 PhoneAccountHandle phoneAccountHandle = PhoneAccountHandleConverter.fromSubId(subId);
2531 if (phoneAccountHandle == null) {
2532 return null;
2533 }
Nazanin Bakhshi2f2cc692018-12-11 15:15:39 -08002534 return VisualVoicemailSettingsUtil.dump(mApp, phoneAccountHandle);
Malcolm Chend965c8b2018-02-28 15:00:40 -08002535 } finally {
2536 Binder.restoreCallingIdentity(identity);
Ta-wei Yenc9df0432017-04-17 17:09:07 -07002537 }
Ta-wei Yenc9df0432017-04-17 17:09:07 -07002538 }
2539
2540 @Override
Ta-wei Yen409ac562017-03-06 16:00:44 -08002541 public String getVisualVoicemailPackageName(String callingPackage, int subId) {
Ta-wei Yendca928f2017-01-10 16:17:08 -08002542 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
Jeff Davidson7e17e312018-02-13 18:17:36 -08002543 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
Jeff Davidson913390f2018-02-23 17:11:49 -08002544 mApp, subId, callingPackage, "getVisualVoicemailPackageName")) {
Ta-wei Yendca928f2017-01-10 16:17:08 -08002545 return null;
2546 }
Malcolm Chend965c8b2018-02-28 15:00:40 -08002547
Jeff Davidsona8e4e242018-03-15 17:16:18 -07002548 final long identity = Binder.clearCallingIdentity();
2549 try {
Nazanin Bakhshi2f2cc692018-12-11 15:15:39 -08002550 return RemoteVvmTaskManager.getRemotePackage(mApp, subId).getPackageName();
Jeff Davidsona8e4e242018-03-15 17:16:18 -07002551 } finally {
2552 Binder.restoreCallingIdentity(identity);
2553 }
Ta-wei Yendca928f2017-01-10 16:17:08 -08002554 }
2555
2556 @Override
Ta-wei Yenb6929602016-05-24 15:48:27 -07002557 public void enableVisualVoicemailSmsFilter(String callingPackage, int subId,
2558 VisualVoicemailSmsFilterSettings settings) {
2559 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
Malcolm Chend965c8b2018-02-28 15:00:40 -08002560
2561 final long identity = Binder.clearCallingIdentity();
2562 try {
2563 VisualVoicemailSmsFilterConfig.enableVisualVoicemailSmsFilter(
Nazanin Bakhshi2f2cc692018-12-11 15:15:39 -08002564 mApp, callingPackage, subId, settings);
Malcolm Chend965c8b2018-02-28 15:00:40 -08002565 } finally {
2566 Binder.restoreCallingIdentity(identity);
2567 }
Ta-wei Yen87c49842016-05-13 21:19:52 -07002568 }
2569
2570 @Override
Ta-wei Yenb6929602016-05-24 15:48:27 -07002571 public void disableVisualVoicemailSmsFilter(String callingPackage, int subId) {
2572 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
Malcolm Chend965c8b2018-02-28 15:00:40 -08002573
2574 final long identity = Binder.clearCallingIdentity();
2575 try {
2576 VisualVoicemailSmsFilterConfig.disableVisualVoicemailSmsFilter(
Nazanin Bakhshi2f2cc692018-12-11 15:15:39 -08002577 mApp, callingPackage, subId);
Malcolm Chend965c8b2018-02-28 15:00:40 -08002578 } finally {
2579 Binder.restoreCallingIdentity(identity);
2580 }
Ta-wei Yen87c49842016-05-13 21:19:52 -07002581 }
2582
2583 @Override
Ta-wei Yenb6929602016-05-24 15:48:27 -07002584 public VisualVoicemailSmsFilterSettings getVisualVoicemailSmsFilterSettings(
2585 String callingPackage, int subId) {
2586 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
Malcolm Chend965c8b2018-02-28 15:00:40 -08002587
2588 final long identity = Binder.clearCallingIdentity();
2589 try {
2590 return VisualVoicemailSmsFilterConfig.getVisualVoicemailSmsFilterSettings(
Nazanin Bakhshi2f2cc692018-12-11 15:15:39 -08002591 mApp, callingPackage, subId);
Malcolm Chend965c8b2018-02-28 15:00:40 -08002592 } finally {
2593 Binder.restoreCallingIdentity(identity);
2594 }
Ta-wei Yen87c49842016-05-13 21:19:52 -07002595 }
2596
2597 @Override
Ta-wei Yen30a69c82016-12-27 14:52:32 -08002598 public VisualVoicemailSmsFilterSettings getActiveVisualVoicemailSmsFilterSettings(int subId) {
Brad Ebinger4c460712018-10-01 10:40:55 -07002599 enforceReadPrivilegedPermission("getActiveVisualVoicemailSmsFilterSettings");
Malcolm Chend965c8b2018-02-28 15:00:40 -08002600
2601 final long identity = Binder.clearCallingIdentity();
2602 try {
2603 return VisualVoicemailSmsFilterConfig.getActiveVisualVoicemailSmsFilterSettings(
Nazanin Bakhshi2f2cc692018-12-11 15:15:39 -08002604 mApp, subId);
Malcolm Chend965c8b2018-02-28 15:00:40 -08002605 } finally {
2606 Binder.restoreCallingIdentity(identity);
2607 }
Ta-wei Yen30a69c82016-12-27 14:52:32 -08002608 }
2609
2610 @Override
2611 public void sendVisualVoicemailSmsForSubscriber(String callingPackage, int subId,
2612 String number, int port, String text, PendingIntent sentIntent) {
2613 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
Ta-wei Yen527a9c02017-01-06 15:29:25 -08002614 enforceVisualVoicemailPackage(callingPackage, subId);
Ta-wei Yen30a69c82016-12-27 14:52:32 -08002615 enforceSendSmsPermission();
2616 // Make the calls as the phone process.
2617 final long identity = Binder.clearCallingIdentity();
2618 try {
2619 SmsManager smsManager = SmsManager.getSmsManagerForSubscriptionId(subId);
2620 if (port == 0) {
2621 smsManager.sendTextMessageWithSelfPermissions(number, null, text,
2622 sentIntent, null, false);
2623 } else {
2624 byte[] data = text.getBytes(StandardCharsets.UTF_8);
2625 smsManager.sendDataMessageWithSelfPermissions(number, null,
2626 (short) port, data, sentIntent, null);
2627 }
2628 } finally {
2629 Binder.restoreCallingIdentity(identity);
2630 }
Ta-wei Yen87c49842016-05-13 21:19:52 -07002631 }
Shishir Agrawal76d5da92014-11-09 16:17:25 -08002632 /**
fionaxu0152e512016-11-14 13:36:14 -08002633 * Sets the voice activation state of a given subId.
2634 */
2635 @Override
2636 public void setVoiceActivationState(int subId, int activationState) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08002637 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
2638 mApp, subId, "setVoiceActivationState");
Malcolm Chend965c8b2018-02-28 15:00:40 -08002639
2640 final long identity = Binder.clearCallingIdentity();
2641 try {
2642 final Phone phone = getPhone(subId);
2643 if (phone != null) {
2644 phone.setVoiceActivationState(activationState);
2645 } else {
2646 loge("setVoiceActivationState fails with invalid subId: " + subId);
2647 }
2648 } finally {
2649 Binder.restoreCallingIdentity(identity);
fionaxu0152e512016-11-14 13:36:14 -08002650 }
2651 }
2652
2653 /**
2654 * Sets the data activation state of a given subId.
2655 */
2656 @Override
2657 public void setDataActivationState(int subId, int activationState) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08002658 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
2659 mApp, subId, "setDataActivationState");
Malcolm Chend965c8b2018-02-28 15:00:40 -08002660
2661 final long identity = Binder.clearCallingIdentity();
2662 try {
2663 final Phone phone = getPhone(subId);
2664 if (phone != null) {
2665 phone.setDataActivationState(activationState);
2666 } else {
2667 loge("setVoiceActivationState fails with invalid subId: " + subId);
2668 }
2669 } finally {
2670 Binder.restoreCallingIdentity(identity);
fionaxu0152e512016-11-14 13:36:14 -08002671 }
2672 }
2673
2674 /**
2675 * Returns the voice activation state of a given subId.
2676 */
2677 @Override
2678 public int getVoiceActivationState(int subId, String callingPackage) {
Brad Ebinger4c460712018-10-01 10:40:55 -07002679 enforceReadPrivilegedPermission("getVoiceActivationState");
Malcolm Chend965c8b2018-02-28 15:00:40 -08002680
fionaxu0152e512016-11-14 13:36:14 -08002681 final Phone phone = getPhone(subId);
Malcolm Chend965c8b2018-02-28 15:00:40 -08002682 final long identity = Binder.clearCallingIdentity();
2683 try {
2684 if (phone != null) {
2685 return phone.getVoiceActivationState();
2686 } else {
2687 return TelephonyManager.SIM_ACTIVATION_STATE_UNKNOWN;
2688 }
2689 } finally {
2690 Binder.restoreCallingIdentity(identity);
fionaxu0152e512016-11-14 13:36:14 -08002691 }
2692 }
2693
2694 /**
2695 * Returns the data activation state of a given subId.
2696 */
2697 @Override
2698 public int getDataActivationState(int subId, String callingPackage) {
Brad Ebinger4c460712018-10-01 10:40:55 -07002699 enforceReadPrivilegedPermission("getDataActivationState");
Malcolm Chend965c8b2018-02-28 15:00:40 -08002700
fionaxu0152e512016-11-14 13:36:14 -08002701 final Phone phone = getPhone(subId);
Malcolm Chend965c8b2018-02-28 15:00:40 -08002702 final long identity = Binder.clearCallingIdentity();
2703 try {
2704 if (phone != null) {
2705 return phone.getDataActivationState();
2706 } else {
2707 return TelephonyManager.SIM_ACTIVATION_STATE_UNKNOWN;
2708 }
2709 } finally {
2710 Binder.restoreCallingIdentity(identity);
fionaxu0152e512016-11-14 13:36:14 -08002711 }
2712 }
2713
2714 /**
Wink Saville36469e72014-06-11 15:17:00 -07002715 * Returns the unread count of voicemails for a subId
2716 */
Sanket Padawe356d7632015-06-22 14:03:32 -07002717 @Override
Brad Ebingerf7664ba2018-11-29 12:43:38 -08002718 public int getVoiceMessageCountForSubscriber(int subId, String callingPackage) {
2719 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
2720 mApp, subId, callingPackage, "getVoiceMessageCountForSubscriber")) {
2721 return 0;
2722 }
Malcolm Chend965c8b2018-02-28 15:00:40 -08002723 final long identity = Binder.clearCallingIdentity();
2724 try {
2725 final Phone phone = getPhone(subId);
2726 if (phone != null) {
2727 return phone.getVoiceMessageCount();
2728 } else {
2729 return 0;
2730 }
2731 } finally {
2732 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07002733 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002734 }
2735
2736 /**
pkanwar8a4dcfb2017-01-19 13:43:16 -08002737 * returns true, if the device is in a state where both voice and data
2738 * are supported simultaneously. This can change based on location or network condition.
2739 */
2740 @Override
2741 public boolean isConcurrentVoiceAndDataAllowed(int subId) {
Malcolm Chend965c8b2018-02-28 15:00:40 -08002742 final long identity = Binder.clearCallingIdentity();
2743 try {
2744 final Phone phone = getPhone(subId);
2745 return (phone == null ? false : phone.isConcurrentVoiceAndDataAllowed());
2746 } finally {
2747 Binder.restoreCallingIdentity(identity);
2748 }
pkanwar8a4dcfb2017-01-19 13:43:16 -08002749 }
2750
2751 /**
fionaxu235cc5e2017-03-06 22:25:57 -08002752 * Send the dialer code if called from the current default dialer or the caller has
2753 * carrier privilege.
2754 * @param inputCode The dialer code to send
2755 */
2756 @Override
2757 public void sendDialerSpecialCode(String callingPackage, String inputCode) {
Nazanin Bakhshi2f2cc692018-12-11 15:15:39 -08002758 final Phone defaultPhone = getDefaultPhone();
fionaxu235cc5e2017-03-06 22:25:57 -08002759 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
Nazanin Bakhshi2f2cc692018-12-11 15:15:39 -08002760 String defaultDialer = TelecomManager.from(defaultPhone.getContext())
2761 .getDefaultDialerPackage();
fionaxu235cc5e2017-03-06 22:25:57 -08002762 if (!TextUtils.equals(callingPackage, defaultDialer)) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08002763 TelephonyPermissions.enforceCallingOrSelfCarrierPrivilege(
2764 getDefaultSubscription(), "sendDialerSpecialCode");
fionaxu235cc5e2017-03-06 22:25:57 -08002765 }
Malcolm Chend965c8b2018-02-28 15:00:40 -08002766
2767 final long identity = Binder.clearCallingIdentity();
2768 try {
Nazanin Bakhshi2f2cc692018-12-11 15:15:39 -08002769 defaultPhone.sendDialerSpecialCode(inputCode);
Malcolm Chend965c8b2018-02-28 15:00:40 -08002770 } finally {
2771 Binder.restoreCallingIdentity(identity);
2772 }
fionaxu235cc5e2017-03-06 22:25:57 -08002773 }
2774
2775 /**
Robert Greenwalta5dcfcb2015-07-10 09:06:29 -07002776 * Returns the data network type.
2777 * Legacy call, permission-free.
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002778 *
2779 * @Deprecated to be removed Q3 2013 use {@link #getDataNetworkType}.
2780 */
2781 @Override
Robert Greenwalta5dcfcb2015-07-10 09:06:29 -07002782 public int getNetworkType() {
Malcolm Chend965c8b2018-02-28 15:00:40 -08002783 final long identity = Binder.clearCallingIdentity();
2784 try {
2785 final Phone phone = getPhone(getDefaultSubscription());
2786 if (phone != null) {
2787 return phone.getServiceState().getDataNetworkType();
2788 } else {
2789 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
2790 }
2791 } finally {
2792 Binder.restoreCallingIdentity(identity);
Robert Greenwalta5dcfcb2015-07-10 09:06:29 -07002793 }
Wink Saville36469e72014-06-11 15:17:00 -07002794 }
2795
Pengquan Meng0c05b502018-09-06 09:59:22 -07002796 @Override
2797 public int getNetworkSelectionMode(int subId) {
Pengquan Meng466e2482018-09-21 15:54:48 -07002798 if (!isActiveSubscription(subId)) {
2799 return TelephonyManager.NETWORK_SELECTION_MODE_UNKNOWN;
2800 }
2801
Pengquan Meng0c05b502018-09-06 09:59:22 -07002802 return (int) sendRequest(CMD_GET_NETWORK_SELECTION_MODE, null /* argument */, subId);
2803 }
2804
Brad Ebinger4c460712018-10-01 10:40:55 -07002805 @Override
Brad Ebingerb2b65522019-03-15 13:48:47 -07002806 public boolean isInEmergencySmsMode() {
2807 enforceReadPrivilegedPermission("isInEmergencySmsMode");
2808 final long identity = Binder.clearCallingIdentity();
2809 try {
2810 for (Phone phone : PhoneFactory.getPhones()) {
2811 if (phone.isInEmergencySmsMode()) {
2812 return true;
2813 }
2814 }
2815 } finally {
2816 Binder.restoreCallingIdentity(identity);
2817 }
2818 return false;
2819 }
2820
2821 @Override
Brad Ebinger9878b0b2018-11-08 17:43:22 -08002822 public void registerImsRegistrationCallback(int subId, IImsRegistrationCallback c)
2823 throws RemoteException {
2824 enforceReadPrivilegedPermission("registerImsRegistrationCallback");
Brad Ebinger4c460712018-10-01 10:40:55 -07002825 final long token = Binder.clearCallingIdentity();
2826 try {
Brad Ebinger4ae57f92019-01-09 16:51:30 -08002827 // TODO: Refactor to remove ImsManager dependence and query through ImsPhone directly.
Nazanin Bakhshi2f2cc692018-12-11 15:15:39 -08002828 ImsManager.getInstance(mApp, getSlotIndexOrException(subId))
Brad Ebinger4c460712018-10-01 10:40:55 -07002829 .addRegistrationCallbackForSubscription(c, subId);
2830 } finally {
2831 Binder.restoreCallingIdentity(token);
2832 }
2833 }
2834
2835 @Override
Brad Ebinger9878b0b2018-11-08 17:43:22 -08002836 public void unregisterImsRegistrationCallback(int subId, IImsRegistrationCallback c) {
2837 enforceReadPrivilegedPermission("unregisterImsRegistrationCallback");
Brad Ebinger4ae57f92019-01-09 16:51:30 -08002838 if (!SubscriptionManager.isValidSubscriptionId(subId)) {
2839 throw new IllegalArgumentException("Invalid Subscription ID: " + subId);
2840 }
2841 Binder.withCleanCallingIdentity(() -> {
2842 try {
2843 // TODO: Refactor to remove ImsManager dependence and query through ImsPhone.
Nazanin Bakhshi2f2cc692018-12-11 15:15:39 -08002844 ImsManager.getInstance(mApp, getSlotIndexOrException(subId))
Brad Ebinger4ae57f92019-01-09 16:51:30 -08002845 .removeRegistrationCallbackForSubscription(c, subId);
2846 } catch (IllegalArgumentException e) {
2847 Log.i(LOG_TAG, "unregisterImsRegistrationCallback: " + subId
2848 + "is inactive, ignoring unregister.");
2849 // If the subscription is no longer active, just return, since the callback
2850 // will already have been removed internally.
2851 }
2852 });
Brad Ebinger4c460712018-10-01 10:40:55 -07002853 }
2854
2855 @Override
Brad Ebinger9878b0b2018-11-08 17:43:22 -08002856 public void registerMmTelCapabilityCallback(int subId, IImsCapabilityCallback c)
2857 throws RemoteException {
2858 enforceReadPrivilegedPermission("registerMmTelCapabilityCallback");
Brad Ebinger4c460712018-10-01 10:40:55 -07002859 // TODO: Refactor to remove ImsManager dependence and query through ImsPhone directly.
2860 final long token = Binder.clearCallingIdentity();
2861 try {
Nazanin Bakhshi2f2cc692018-12-11 15:15:39 -08002862 ImsManager.getInstance(mApp, getSlotIndexOrException(subId))
Brad Ebinger4c460712018-10-01 10:40:55 -07002863 .addCapabilitiesCallbackForSubscription(c, subId);
2864 } finally {
2865 Binder.restoreCallingIdentity(token);
2866 }
2867 }
2868
2869 @Override
Brad Ebinger9878b0b2018-11-08 17:43:22 -08002870 public void unregisterMmTelCapabilityCallback(int subId, IImsCapabilityCallback c) {
2871 enforceReadPrivilegedPermission("unregisterMmTelCapabilityCallback");
Brad Ebinger4ae57f92019-01-09 16:51:30 -08002872
2873 if (!SubscriptionManager.isValidSubscriptionId(subId)) {
2874 throw new IllegalArgumentException("Invalid Subscription ID: " + subId);
2875 }
2876 Binder.withCleanCallingIdentity(() -> {
2877 try {
2878 // TODO: Refactor to remove ImsManager dependence and query through ImsPhone.
Nazanin Bakhshi2f2cc692018-12-11 15:15:39 -08002879 ImsManager.getInstance(mApp, getSlotIndexOrException(subId))
Brad Ebinger4ae57f92019-01-09 16:51:30 -08002880 .removeCapabilitiesCallbackForSubscription(c, subId);
2881 } catch (IllegalArgumentException e) {
2882 Log.i(LOG_TAG, "unregisterMmTelCapabilityCallback: " + subId
2883 + "is inactive, ignoring unregister.");
2884 // If the subscription is no longer active, just return, since the callback
2885 // will already have been removed internally.
2886 }
2887 });
Brad Ebinger4c460712018-10-01 10:40:55 -07002888 }
2889
2890 @Override
Brad Ebinger9878b0b2018-11-08 17:43:22 -08002891 public boolean isCapable(int subId, int capability, int regTech) {
2892 enforceReadPrivilegedPermission("isCapable");
Brad Ebinger4c460712018-10-01 10:40:55 -07002893 // TODO: Refactor to remove ImsManager dependence and query through ImsPhone directly.
2894 final long token = Binder.clearCallingIdentity();
2895 try {
Nazanin Bakhshi2f2cc692018-12-11 15:15:39 -08002896 return ImsManager.getInstance(mApp,
Brad Ebinger4c460712018-10-01 10:40:55 -07002897 getSlotIndexOrException(subId)).queryMmTelCapability(capability, regTech);
2898 } catch (ImsException e) {
2899 Log.w(LOG_TAG, "IMS isCapable - service unavailable: " + e.getMessage());
2900 return false;
Brad Ebinger6b5ac222019-02-04 14:36:52 -08002901 } catch (IllegalArgumentException e) {
2902 Log.i(LOG_TAG, "isCapable: " + subId + " is inactive, returning false.");
2903 return false;
Brad Ebinger4c460712018-10-01 10:40:55 -07002904 } finally {
2905 Binder.restoreCallingIdentity(token);
2906 }
2907 }
2908
2909 @Override
Brad Ebinger9878b0b2018-11-08 17:43:22 -08002910 public boolean isAvailable(int subId, int capability, int regTech) {
2911 enforceReadPrivilegedPermission("isAvailable");
Brad Ebinger4c460712018-10-01 10:40:55 -07002912 final long token = Binder.clearCallingIdentity();
2913 try {
2914 Phone phone = getPhone(subId);
2915 if (phone == null) return false;
2916 return phone.isImsCapabilityAvailable(capability, regTech);
2917 } finally {
2918 Binder.restoreCallingIdentity(token);
2919 }
2920 }
2921
2922 @Override
2923 public boolean isAdvancedCallingSettingEnabled(int subId) {
2924 enforceReadPrivilegedPermission("enforceReadPrivilegedPermission");
2925 // TODO: Refactor to remove ImsManager dependence and query through ImsPhone directly.
2926 final long token = Binder.clearCallingIdentity();
2927 try {
Nazanin Bakhshi2f2cc692018-12-11 15:15:39 -08002928 return ImsManager.getInstance(mApp,
Brad Ebinger4c460712018-10-01 10:40:55 -07002929 getSlotIndexOrException(subId)).isEnhanced4gLteModeSettingEnabledByUser();
2930 } finally {
2931 Binder.restoreCallingIdentity(token);
2932 }
2933 }
2934
2935 @Override
Brad Ebinger0de268e2019-02-21 14:49:10 -08002936 public void setAdvancedCallingSettingEnabled(int subId, boolean isEnabled) {
Brad Ebinger4c460712018-10-01 10:40:55 -07002937 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(mApp, subId,
Brad Ebinger0de268e2019-02-21 14:49:10 -08002938 "setAdvancedCallingSettingEnabled");
Brad Ebinger4c460712018-10-01 10:40:55 -07002939 final long identity = Binder.clearCallingIdentity();
2940 try {
2941 // TODO: Refactor to remove ImsManager dependence and query through ImsPhone directly.
Nazanin Bakhshi2f2cc692018-12-11 15:15:39 -08002942 ImsManager.getInstance(mApp,
Brad Ebinger4c460712018-10-01 10:40:55 -07002943 getSlotIndexOrException(subId)).setEnhanced4gLteModeSetting(isEnabled);
2944 } finally {
2945 Binder.restoreCallingIdentity(identity);
2946 }
2947 }
2948
2949 @Override
Brad Ebinger9878b0b2018-11-08 17:43:22 -08002950 public boolean isVtSettingEnabled(int subId) {
2951 enforceReadPrivilegedPermission("isVtSettingEnabled");
Brad Ebinger4c460712018-10-01 10:40:55 -07002952 final long identity = Binder.clearCallingIdentity();
2953 try {
2954 // TODO: Refactor to remove ImsManager dependence and query through ImsPhone directly.
Nazanin Bakhshi2f2cc692018-12-11 15:15:39 -08002955 return ImsManager.getInstance(mApp,
Brad Ebinger4c460712018-10-01 10:40:55 -07002956 getSlotIndexOrException(subId)).isVtEnabledByUser();
2957 } finally {
2958 Binder.restoreCallingIdentity(identity);
2959 }
2960 }
2961
2962 @Override
Brad Ebinger0de268e2019-02-21 14:49:10 -08002963 public void setVtSettingEnabled(int subId, boolean isEnabled) {
Brad Ebinger4c460712018-10-01 10:40:55 -07002964 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(mApp, subId,
Brad Ebinger0de268e2019-02-21 14:49:10 -08002965 "setVtSettingEnabled");
Brad Ebinger4c460712018-10-01 10:40:55 -07002966 final long identity = Binder.clearCallingIdentity();
2967 try {
2968 // TODO: Refactor to remove ImsManager dependence and query through ImsPhone directly.
Nazanin Bakhshi2f2cc692018-12-11 15:15:39 -08002969 ImsManager.getInstance(mApp, getSlotIndexOrException(subId)).setVtSetting(isEnabled);
Brad Ebinger4c460712018-10-01 10:40:55 -07002970 } finally {
2971 Binder.restoreCallingIdentity(identity);
2972 }
2973 }
2974
2975 @Override
2976 public boolean isVoWiFiSettingEnabled(int subId) {
2977 enforceReadPrivilegedPermission("isVoWiFiSettingEnabled");
2978 final long identity = Binder.clearCallingIdentity();
2979 try {
2980 // TODO: Refactor to remove ImsManager dependence and query through ImsPhone directly.
Nazanin Bakhshi2f2cc692018-12-11 15:15:39 -08002981 return ImsManager.getInstance(mApp,
Brad Ebinger4c460712018-10-01 10:40:55 -07002982 getSlotIndexOrException(subId)).isWfcEnabledByUser();
2983 } finally {
2984 Binder.restoreCallingIdentity(identity);
2985 }
2986 }
2987
2988 @Override
Brad Ebinger0de268e2019-02-21 14:49:10 -08002989 public void setVoWiFiSettingEnabled(int subId, boolean isEnabled) {
Brad Ebinger4c460712018-10-01 10:40:55 -07002990 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(mApp, subId,
Brad Ebinger0de268e2019-02-21 14:49:10 -08002991 "setVoWiFiSettingEnabled");
Brad Ebinger4c460712018-10-01 10:40:55 -07002992 final long identity = Binder.clearCallingIdentity();
2993 try {
2994 // TODO: Refactor to remove ImsManager dependence and query through ImsPhone directly.
Nazanin Bakhshi2f2cc692018-12-11 15:15:39 -08002995 ImsManager.getInstance(mApp, getSlotIndexOrException(subId)).setWfcSetting(isEnabled);
Brad Ebinger4c460712018-10-01 10:40:55 -07002996 } finally {
2997 Binder.restoreCallingIdentity(identity);
2998 }
2999 }
3000
3001 @Override
3002 public boolean isVoWiFiRoamingSettingEnabled(int subId) {
3003 enforceReadPrivilegedPermission("isVoWiFiRoamingSettingEnabled");
3004 final long identity = Binder.clearCallingIdentity();
3005 try {
3006 // TODO: Refactor to remove ImsManager dependence and query through ImsPhone directly.
Nazanin Bakhshi2f2cc692018-12-11 15:15:39 -08003007 return ImsManager.getInstance(mApp,
Brad Ebinger4c460712018-10-01 10:40:55 -07003008 getSlotIndexOrException(subId)).isWfcRoamingEnabledByUser();
3009 } finally {
3010 Binder.restoreCallingIdentity(identity);
3011 }
3012 }
3013
3014 @Override
Brad Ebinger0de268e2019-02-21 14:49:10 -08003015 public void setVoWiFiRoamingSettingEnabled(int subId, boolean isEnabled) {
Brad Ebinger4c460712018-10-01 10:40:55 -07003016 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(mApp, subId,
Brad Ebinger0de268e2019-02-21 14:49:10 -08003017 "setVoWiFiRoamingSettingEnabled");
Brad Ebinger4c460712018-10-01 10:40:55 -07003018 final long identity = Binder.clearCallingIdentity();
3019 try {
3020 // TODO: Refactor to remove ImsManager dependence and query through ImsPhone directly.
Nazanin Bakhshi2f2cc692018-12-11 15:15:39 -08003021 ImsManager.getInstance(mApp,
Brad Ebinger4c460712018-10-01 10:40:55 -07003022 getSlotIndexOrException(subId)).setWfcRoamingSetting(isEnabled);
3023 } finally {
3024 Binder.restoreCallingIdentity(identity);
3025 }
3026 }
3027
3028 @Override
3029 public void setVoWiFiNonPersistent(int subId, boolean isCapable, int mode) {
3030 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(mApp, subId,
3031 "setVoWiFiNonPersistent");
3032 final long identity = Binder.clearCallingIdentity();
3033 try {
3034 // TODO: Refactor to remove ImsManager dependence and query through ImsPhone directly.
Nazanin Bakhshi2f2cc692018-12-11 15:15:39 -08003035 ImsManager.getInstance(mApp,
Brad Ebinger2bc95702019-05-07 18:33:46 -07003036 getSlotIndexOrException(subId)).setWfcNonPersistent(isCapable, mode);
Brad Ebinger4c460712018-10-01 10:40:55 -07003037 } finally {
3038 Binder.restoreCallingIdentity(identity);
3039 }
3040 }
3041
3042 @Override
3043 public int getVoWiFiModeSetting(int subId) {
3044 enforceReadPrivilegedPermission("getVoWiFiModeSetting");
3045 final long identity = Binder.clearCallingIdentity();
3046 try {
3047 // TODO: Refactor to remove ImsManager dependence and query through ImsPhone directly.
Nazanin Bakhshi2f2cc692018-12-11 15:15:39 -08003048 return ImsManager.getInstance(mApp,
Brad Ebinger4c460712018-10-01 10:40:55 -07003049 getSlotIndexOrException(subId)).getWfcMode(false /*isRoaming*/);
3050 } finally {
3051 Binder.restoreCallingIdentity(identity);
3052 }
3053 }
3054
3055 @Override
3056 public void setVoWiFiModeSetting(int subId, int mode) {
3057 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(mApp, subId,
3058 "setVoWiFiModeSetting");
3059 final long identity = Binder.clearCallingIdentity();
3060 try {
3061 // TODO: Refactor to remove ImsManager dependence and query through ImsPhone directly.
Nazanin Bakhshi2f2cc692018-12-11 15:15:39 -08003062 ImsManager.getInstance(mApp,
Brad Ebinger4c460712018-10-01 10:40:55 -07003063 getSlotIndexOrException(subId)).setWfcMode(mode, false /*isRoaming*/);
3064 } finally {
3065 Binder.restoreCallingIdentity(identity);
3066 }
3067 }
3068
3069 @Override
3070 public int getVoWiFiRoamingModeSetting(int subId) {
3071 enforceReadPrivilegedPermission("getVoWiFiRoamingModeSetting");
3072 final long identity = Binder.clearCallingIdentity();
3073 try {
3074 // TODO: Refactor to remove ImsManager dependence and query through ImsPhone directly.
Nazanin Bakhshi2f2cc692018-12-11 15:15:39 -08003075 return ImsManager.getInstance(mApp,
Brad Ebinger4c460712018-10-01 10:40:55 -07003076 getSlotIndexOrException(subId)).getWfcMode(true /*isRoaming*/);
3077 } finally {
3078 Binder.restoreCallingIdentity(identity);
3079 }
3080 }
3081
3082 @Override
3083 public void setVoWiFiRoamingModeSetting(int subId, int mode) {
3084 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(mApp, subId,
3085 "setVoWiFiRoamingModeSetting");
3086 final long identity = Binder.clearCallingIdentity();
3087 try {
3088 // TODO: Refactor to remove ImsManager dependence and query through ImsPhone directly.
Nazanin Bakhshi2f2cc692018-12-11 15:15:39 -08003089 ImsManager.getInstance(mApp,
Brad Ebinger4c460712018-10-01 10:40:55 -07003090 getSlotIndexOrException(subId)).setWfcMode(mode, true /*isRoaming*/);
3091 } finally {
3092 Binder.restoreCallingIdentity(identity);
3093 }
3094 }
3095
3096 @Override
3097 public void setRttCapabilitySetting(int subId, boolean isEnabled) {
3098 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(mApp, subId,
3099 "setRttCapabilityEnabled");
3100 final long identity = Binder.clearCallingIdentity();
3101 try {
3102 // TODO: Refactor to remove ImsManager dependence and query through ImsPhone directly.
Nazanin Bakhshi2f2cc692018-12-11 15:15:39 -08003103 ImsManager.getInstance(mApp,
Brad Ebinger4c460712018-10-01 10:40:55 -07003104 getSlotIndexOrException(subId)).setRttEnabled(isEnabled);
3105 } finally {
3106 Binder.restoreCallingIdentity(identity);
3107 }
3108 }
3109
3110 @Override
3111 public boolean isTtyOverVolteEnabled(int subId) {
3112 enforceReadPrivilegedPermission("isTtyOverVolteEnabled");
3113 final long identity = Binder.clearCallingIdentity();
3114 try {
3115 // TODO: Refactor to remove ImsManager dependence and query through ImsPhone directly.
Nazanin Bakhshi2f2cc692018-12-11 15:15:39 -08003116 return ImsManager.getInstance(mApp,
Brad Ebinger4c460712018-10-01 10:40:55 -07003117 getSlotIndexOrException(subId)).isTtyOnVoLteCapable();
3118 } finally {
3119 Binder.restoreCallingIdentity(identity);
3120 }
3121 }
3122
Brad Ebingerdf5b4f02018-10-31 11:24:17 -07003123 @Override
3124 public void registerImsProvisioningChangedCallback(int subId, IImsConfigCallback callback) {
3125 enforceReadPrivilegedPermission("registerImsProvisioningChangedCallback");
3126 final long identity = Binder.clearCallingIdentity();
3127 try {
3128 // TODO: Refactor to remove ImsManager dependence and query through ImsPhone directly.
Nazanin Bakhshi2f2cc692018-12-11 15:15:39 -08003129 ImsManager.getInstance(mApp, getSlotIndexOrException(subId))
Brad Ebinger4ae57f92019-01-09 16:51:30 -08003130 .addProvisioningCallbackForSubscription(callback, subId);
Brad Ebingerdf5b4f02018-10-31 11:24:17 -07003131 } finally {
3132 Binder.restoreCallingIdentity(identity);
3133 }
3134 }
3135
3136 @Override
3137 public void unregisterImsProvisioningChangedCallback(int subId, IImsConfigCallback callback) {
3138 enforceReadPrivilegedPermission("unregisterImsProvisioningChangedCallback");
3139 final long identity = Binder.clearCallingIdentity();
Brad Ebinger4ae57f92019-01-09 16:51:30 -08003140 if (!SubscriptionManager.isValidSubscriptionId(subId)) {
3141 throw new IllegalArgumentException("Invalid Subscription ID: " + subId);
3142 }
Brad Ebingerdf5b4f02018-10-31 11:24:17 -07003143 try {
3144 // TODO: Refactor to remove ImsManager dependence and query through ImsPhone directly.
Nazanin Bakhshi2f2cc692018-12-11 15:15:39 -08003145 ImsManager.getInstance(mApp, getSlotIndexOrException(subId))
Brad Ebinger4ae57f92019-01-09 16:51:30 -08003146 .removeProvisioningCallbackForSubscription(callback, subId);
3147 } catch (IllegalArgumentException e) {
3148 Log.i(LOG_TAG, "unregisterImsProvisioningChangedCallback: " + subId
3149 + "is inactive, ignoring unregister.");
3150 // If the subscription is no longer active, just return, since the callback will already
3151 // have been removed internally.
Brad Ebingerdf5b4f02018-10-31 11:24:17 -07003152 } finally {
3153 Binder.restoreCallingIdentity(identity);
3154 }
3155 }
3156
3157 @Override
Brad Ebinger1c8542e2019-01-14 13:43:14 -08003158 public void setImsProvisioningStatusForCapability(int subId, int capability, int tech,
3159 boolean isProvisioned) {
3160 if (tech != ImsRegistrationImplBase.REGISTRATION_TECH_IWLAN
3161 && tech != ImsRegistrationImplBase.REGISTRATION_TECH_LTE) {
3162 throw new IllegalArgumentException("Registration technology '" + tech + "' is invalid");
3163 }
3164 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(mApp, subId,
3165 "setProvisioningStatusForCapability");
3166 final long identity = Binder.clearCallingIdentity();
3167 try {
3168 // TODO: Refactor to remove ImsManager dependence and query through ImsPhone directly.
3169 Phone phone = getPhone(subId);
3170 if (phone == null) {
3171 loge("setImsProvisioningStatusForCapability: phone instance null for subid "
3172 + subId);
3173 return;
3174 }
3175 if (!doesImsCapabilityRequireProvisioning(phone.getContext(), subId, capability)) {
3176 return;
3177 }
3178
3179 // this capability requires provisioning, route to the correct API.
3180 ImsManager ims = ImsManager.getInstance(mApp, getSlotIndex(subId));
3181 switch (capability) {
3182 case MmTelFeature.MmTelCapabilities.CAPABILITY_TYPE_VOICE: {
3183 if (tech == ImsRegistrationImplBase.REGISTRATION_TECH_LTE) {
3184 ims.setVolteProvisioned(isProvisioned);
3185 } else if (tech == ImsRegistrationImplBase.REGISTRATION_TECH_IWLAN) {
3186 ims.setWfcProvisioned(isProvisioned);
3187 }
3188 break;
3189 }
3190 case MmTelFeature.MmTelCapabilities.CAPABILITY_TYPE_VIDEO: {
3191 // There is currently no difference in VT provisioning type.
3192 ims.setVtProvisioned(isProvisioned);
3193 break;
3194 }
3195 case MmTelFeature.MmTelCapabilities.CAPABILITY_TYPE_UT: {
3196 // There is no "deprecated" UT provisioning mechanism through ImsConfig, so
3197 // change the capability of the feature instead if needed.
3198 if (isMmTelCapabilityProvisionedInCache(subId, capability, tech)
3199 == isProvisioned) {
3200 // No change in provisioning.
3201 return;
3202 }
3203 cacheMmTelCapabilityProvisioning(subId, capability, tech, isProvisioned);
3204 try {
3205 ims.changeMmTelCapability(capability, tech, isProvisioned);
3206 } catch (ImsException e) {
3207 loge("setImsProvisioningStatusForCapability: couldn't change UT capability"
3208 + ", Exception" + e.getMessage());
3209 }
3210 break;
3211 }
3212 default: {
3213 throw new IllegalArgumentException("Tried to set provisioning for capability '"
3214 + capability + "', which does not require provisioning.");
3215 }
3216 }
3217
3218 } finally {
3219 Binder.restoreCallingIdentity(identity);
3220 }
3221 }
3222
3223 @Override
3224 public boolean getImsProvisioningStatusForCapability(int subId, int capability, int tech) {
3225 if (tech != ImsRegistrationImplBase.REGISTRATION_TECH_IWLAN
3226 && tech != ImsRegistrationImplBase.REGISTRATION_TECH_LTE) {
3227 throw new IllegalArgumentException("Registration technology '" + tech + "' is invalid");
3228 }
3229 enforceReadPrivilegedPermission("getProvisioningStatusForCapability");
3230 final long identity = Binder.clearCallingIdentity();
3231 try {
3232 // TODO: Refactor to remove ImsManager dependence and query through ImsPhone directly.
3233 Phone phone = getPhone(subId);
3234 if (phone == null) {
3235 loge("getImsProvisioningStatusForCapability: phone instance null for subid "
3236 + subId);
3237 // We will fail with "true" as the provisioning status because this is the default
3238 // if we do not require provisioning.
3239 return true;
3240 }
3241
3242 if (!doesImsCapabilityRequireProvisioning(phone.getContext(), subId, capability)) {
3243 return true;
3244 }
3245
3246 ImsManager ims = ImsManager.getInstance(mApp, getSlotIndex(subId));
3247 switch (capability) {
3248 case MmTelFeature.MmTelCapabilities.CAPABILITY_TYPE_VOICE: {
3249 if (tech == ImsRegistrationImplBase.REGISTRATION_TECH_LTE) {
3250 return ims.isVolteProvisionedOnDevice();
3251 } else if (tech == ImsRegistrationImplBase.REGISTRATION_TECH_IWLAN) {
3252 return ims.isWfcProvisionedOnDevice();
3253 }
3254 // This should never happen, since we are checking tech above to make sure it
3255 // is either LTE or IWLAN.
3256 throw new IllegalArgumentException("Invalid radio technology for voice "
3257 + "capability.");
3258 }
3259 case MmTelFeature.MmTelCapabilities.CAPABILITY_TYPE_VIDEO: {
3260 // There is currently no difference in VT provisioning type.
3261 return ims.isVtProvisionedOnDevice();
3262 }
3263 case MmTelFeature.MmTelCapabilities.CAPABILITY_TYPE_UT: {
3264 // There is no "deprecated" UT provisioning mechanism, so get from shared prefs.
3265 return isMmTelCapabilityProvisionedInCache(subId, capability, tech);
3266 }
3267 default: {
3268 throw new IllegalArgumentException("Tried to get provisioning for capability '"
3269 + capability + "', which does not require provisioning.");
3270 }
3271 }
3272
3273 } finally {
3274 Binder.restoreCallingIdentity(identity);
3275 }
3276 }
3277
3278 @Override
3279 public boolean isMmTelCapabilityProvisionedInCache(int subId, int capability, int tech) {
3280 if (tech != ImsRegistrationImplBase.REGISTRATION_TECH_IWLAN
3281 && tech != ImsRegistrationImplBase.REGISTRATION_TECH_LTE) {
3282 throw new IllegalArgumentException("Registration technology '" + tech + "' is invalid");
3283 }
3284 enforceReadPrivilegedPermission("isMmTelCapabilityProvisionedInCache");
3285 int provisionedBits = getMmTelCapabilityProvisioningBitfield(subId, tech);
3286 return (provisionedBits & capability) > 0;
3287 }
3288
3289 @Override
3290 public void cacheMmTelCapabilityProvisioning(int subId, int capability, int tech,
3291 boolean isProvisioned) {
3292 if (tech != ImsRegistrationImplBase.REGISTRATION_TECH_IWLAN
3293 && tech != ImsRegistrationImplBase.REGISTRATION_TECH_LTE) {
3294 throw new IllegalArgumentException("Registration technology '" + tech + "' is invalid");
3295 }
3296 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(mApp, subId,
3297 "setProvisioningStatusForCapability");
3298 int provisionedBits = getMmTelCapabilityProvisioningBitfield(subId, tech);
3299 // If the current provisioning status for capability already matches isProvisioned,
3300 // do nothing.
3301 if (((provisionedBits & capability) > 0) == isProvisioned) {
3302 return;
3303 }
3304 if (isProvisioned) {
3305 setMmTelCapabilityProvisioningBitfield(subId, tech, (provisionedBits | capability));
3306 } else {
3307 setMmTelCapabilityProvisioningBitfield(subId, tech, (provisionedBits & ~capability));
3308 }
3309 }
3310
3311 /**
3312 * @return the bitfield containing the MmTel provisioning for the provided subscription and
3313 * technology. The bitfield should mirror the bitfield defined by
3314 * {@link MmTelFeature.MmTelCapabilities.MmTelCapability}.
3315 */
3316 private int getMmTelCapabilityProvisioningBitfield(int subId, int tech) {
3317 String key = getMmTelProvisioningKey(subId, tech);
3318 // Default is no capabilities are provisioned.
3319 return mTelephonySharedPreferences.getInt(key, 0 /*default*/);
3320 }
3321
3322 /**
3323 * Sets the MmTel capability provisioning bitfield (defined by
3324 * {@link MmTelFeature.MmTelCapabilities.MmTelCapability}) for the subscription and
3325 * technology specified.
3326 *
3327 * Note: This is a synchronous command and should not be called on UI thread.
3328 */
3329 private void setMmTelCapabilityProvisioningBitfield(int subId, int tech, int newField) {
3330 final SharedPreferences.Editor editor = mTelephonySharedPreferences.edit();
3331 String key = getMmTelProvisioningKey(subId, tech);
3332 editor.putInt(key, newField);
3333 editor.commit();
3334 }
3335
3336 private static String getMmTelProvisioningKey(int subId, int tech) {
3337 // resulting key is provision_ims_mmtel_{subId}_{tech}
3338 return PREF_PROVISION_IMS_MMTEL_PREFIX + subId + "_" + tech;
3339 }
3340
3341 /**
3342 * Query CarrierConfig to see if the specified capability requires provisioning for the
3343 * carrier associated with the subscription id.
3344 */
3345 private boolean doesImsCapabilityRequireProvisioning(Context context, int subId,
3346 int capability) {
3347 CarrierConfigManager configManager = new CarrierConfigManager(context);
3348 PersistableBundle c = configManager.getConfigForSubId(subId);
3349 boolean requireUtProvisioning = c.getBoolean(
3350 // By default, this config is true (even if there is no SIM). We also check to make
3351 // sure the subscription needs provisioning here, so we do not need to check for
3352 // the no-SIM case, where we would normally shortcut this to false.
3353 CarrierConfigManager.KEY_CARRIER_SUPPORTS_SS_OVER_UT_BOOL, true)
3354 && c.getBoolean(CarrierConfigManager.KEY_CARRIER_UT_PROVISIONING_REQUIRED_BOOL,
3355 false);
3356 boolean requireVoiceVtProvisioning = c.getBoolean(
3357 CarrierConfigManager.KEY_CARRIER_VOLTE_PROVISIONING_REQUIRED_BOOL, false);
3358
3359 // First check to make sure that the capability requires provisioning.
3360 switch (capability) {
3361 case MmTelFeature.MmTelCapabilities.CAPABILITY_TYPE_VOICE:
3362 // intentional fallthrough
3363 case MmTelFeature.MmTelCapabilities.CAPABILITY_TYPE_VIDEO: {
3364 if (requireVoiceVtProvisioning) {
3365 // Voice and Video requires provisioning
3366 return true;
3367 }
3368 break;
3369 }
3370 case MmTelFeature.MmTelCapabilities.CAPABILITY_TYPE_UT: {
3371 if (requireUtProvisioning) {
3372 // UT requires provisioning
3373 return true;
3374 }
3375 break;
3376 }
3377 }
3378 return false;
3379 }
3380
3381 @Override
Brad Ebingerdf5b4f02018-10-31 11:24:17 -07003382 public int getImsProvisioningInt(int subId, int key) {
Brad Ebinger1c8542e2019-01-14 13:43:14 -08003383 if (!SubscriptionManager.isValidSubscriptionId(subId)) {
3384 throw new IllegalArgumentException("Invalid Subscription id '" + subId + "'");
3385 }
Brad Ebingerdf5b4f02018-10-31 11:24:17 -07003386 enforceReadPrivilegedPermission("getImsProvisioningInt");
3387 final long identity = Binder.clearCallingIdentity();
3388 try {
3389 // TODO: Refactor to remove ImsManager dependence and query through ImsPhone directly.
Brad Ebinger1c8542e2019-01-14 13:43:14 -08003390 int slotId = getSlotIndex(subId);
3391 if (slotId <= SubscriptionManager.INVALID_SIM_SLOT_INDEX) {
3392 Log.w(LOG_TAG, "getImsProvisioningInt: called with an inactive subscription '"
3393 + subId + "' for key:" + key);
3394 return ImsConfigImplBase.CONFIG_RESULT_UNKNOWN;
3395 }
3396 return ImsManager.getInstance(mApp, slotId).getConfigInterface().getConfigInt(key);
Brad Ebingerdf5b4f02018-10-31 11:24:17 -07003397 } catch (ImsException e) {
Brad Ebinger1c8542e2019-01-14 13:43:14 -08003398 Log.w(LOG_TAG, "getImsProvisioningInt: ImsService is not available for subscription '"
3399 + subId + "' for key:" + key);
3400 return ImsConfigImplBase.CONFIG_RESULT_UNKNOWN;
Brad Ebingerdf5b4f02018-10-31 11:24:17 -07003401 } finally {
3402 Binder.restoreCallingIdentity(identity);
3403 }
3404 }
3405
3406 @Override
3407 public String getImsProvisioningString(int subId, int key) {
Brad Ebinger1c8542e2019-01-14 13:43:14 -08003408 if (!SubscriptionManager.isValidSubscriptionId(subId)) {
3409 throw new IllegalArgumentException("Invalid Subscription id '" + subId + "'");
3410 }
Brad Ebingerdf5b4f02018-10-31 11:24:17 -07003411 enforceReadPrivilegedPermission("getImsProvisioningString");
3412 final long identity = Binder.clearCallingIdentity();
3413 try {
3414 // TODO: Refactor to remove ImsManager dependence and query through ImsPhone directly.
Brad Ebinger1c8542e2019-01-14 13:43:14 -08003415 int slotId = getSlotIndex(subId);
3416 if (slotId <= SubscriptionManager.INVALID_SIM_SLOT_INDEX) {
3417 Log.w(LOG_TAG, "getImsProvisioningString: called for an inactive subscription id '"
3418 + subId + "' for key:" + key);
3419 return ProvisioningManager.STRING_QUERY_RESULT_ERROR_GENERIC;
3420 }
3421 return ImsManager.getInstance(mApp, slotId).getConfigInterface().getConfigString(key);
Brad Ebingerdf5b4f02018-10-31 11:24:17 -07003422 } catch (ImsException e) {
Brad Ebinger1c8542e2019-01-14 13:43:14 -08003423 Log.w(LOG_TAG, "getImsProvisioningString: ImsService is not available for sub '"
3424 + subId + "' for key:" + key);
3425 return ProvisioningManager.STRING_QUERY_RESULT_ERROR_NOT_READY;
Brad Ebingerdf5b4f02018-10-31 11:24:17 -07003426 } finally {
3427 Binder.restoreCallingIdentity(identity);
3428 }
3429 }
3430
3431 @Override
3432 public int setImsProvisioningInt(int subId, int key, int value) {
Brad Ebinger1c8542e2019-01-14 13:43:14 -08003433 if (!SubscriptionManager.isValidSubscriptionId(subId)) {
3434 throw new IllegalArgumentException("Invalid Subscription id '" + subId + "'");
3435 }
Brad Ebinger3d0b34e2018-11-15 14:13:12 -08003436 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(mApp, subId,
3437 "setImsProvisioningInt");
Brad Ebingerdf5b4f02018-10-31 11:24:17 -07003438 final long identity = Binder.clearCallingIdentity();
3439 try {
3440 // TODO: Refactor to remove ImsManager dependence and query through ImsPhone directly.
Brad Ebinger1c8542e2019-01-14 13:43:14 -08003441 int slotId = getSlotIndex(subId);
3442 if (slotId <= SubscriptionManager.INVALID_SIM_SLOT_INDEX) {
3443 Log.w(LOG_TAG, "setImsProvisioningInt: called with an inactive subscription id '"
3444 + subId + "' for key:" + key);
3445 return ImsConfigImplBase.CONFIG_RESULT_FAILED;
3446 }
3447 return ImsManager.getInstance(mApp, slotId).getConfigInterface().setConfig(key, value);
Brad Ebingerdf5b4f02018-10-31 11:24:17 -07003448 } catch (ImsException e) {
Brad Ebinger1c8542e2019-01-14 13:43:14 -08003449 Log.w(LOG_TAG, "setImsProvisioningInt: ImsService unavailable for sub '" + subId
3450 + "' for key:" + key);
3451 return ImsConfigImplBase.CONFIG_RESULT_FAILED;
Brad Ebingerdf5b4f02018-10-31 11:24:17 -07003452 } finally {
3453 Binder.restoreCallingIdentity(identity);
3454 }
3455 }
3456
3457 @Override
3458 public int setImsProvisioningString(int subId, int key, String value) {
Brad Ebinger1c8542e2019-01-14 13:43:14 -08003459 if (!SubscriptionManager.isValidSubscriptionId(subId)) {
3460 throw new IllegalArgumentException("Invalid Subscription id '" + subId + "'");
3461 }
Brad Ebinger3d0b34e2018-11-15 14:13:12 -08003462 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(mApp, subId,
3463 "setImsProvisioningString");
Brad Ebingerdf5b4f02018-10-31 11:24:17 -07003464 final long identity = Binder.clearCallingIdentity();
3465 try {
3466 // TODO: Refactor to remove ImsManager dependence and query through ImsPhone directly.
Brad Ebinger1c8542e2019-01-14 13:43:14 -08003467 int slotId = getSlotIndex(subId);
3468 if (slotId <= SubscriptionManager.INVALID_SIM_SLOT_INDEX) {
3469 Log.w(LOG_TAG, "setImsProvisioningString: called with an inactive subscription id '"
3470 + subId + "' for key:" + key);
3471 return ImsConfigImplBase.CONFIG_RESULT_FAILED;
3472 }
3473 return ImsManager.getInstance(mApp, slotId).getConfigInterface().setConfig(key, value);
Brad Ebingerdf5b4f02018-10-31 11:24:17 -07003474 } catch (ImsException e) {
Brad Ebinger1c8542e2019-01-14 13:43:14 -08003475 Log.w(LOG_TAG, "setImsProvisioningString: ImsService unavailable for sub '" + subId
3476 + "' for key:" + key);
3477 return ImsConfigImplBase.CONFIG_RESULT_FAILED;
Brad Ebingerdf5b4f02018-10-31 11:24:17 -07003478 } finally {
3479 Binder.restoreCallingIdentity(identity);
3480 }
3481 }
3482
Brad Ebinger4c460712018-10-01 10:40:55 -07003483 private int getSlotIndexOrException(int subId) throws IllegalArgumentException {
3484 int slotId = SubscriptionManager.getSlotIndex(subId);
3485 if (!SubscriptionManager.isValidSlotIndex(slotId)) {
Brad Ebinger6b5ac222019-02-04 14:36:52 -08003486 throw new IllegalArgumentException("Invalid Subscription Id, subId=" + subId);
Brad Ebinger4c460712018-10-01 10:40:55 -07003487 }
3488 return slotId;
3489 }
3490
Brad Ebinger1c8542e2019-01-14 13:43:14 -08003491 private int getSlotIndex(int subId) {
3492 int slotId = SubscriptionManager.getSlotIndex(subId);
3493 if (!SubscriptionManager.isValidSlotIndex(slotId)) {
3494 return SubscriptionManager.INVALID_SIM_SLOT_INDEX;
3495 }
3496 return slotId;
3497 }
3498
Wink Saville36469e72014-06-11 15:17:00 -07003499 /**
3500 * Returns the network type for a subId
3501 */
3502 @Override
Robert Greenwalt36b23af2015-07-06 17:59:14 -07003503 public int getNetworkTypeForSubscriber(int subId, String callingPackage) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08003504 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
Jeff Davidson913390f2018-02-23 17:11:49 -08003505 mApp, subId, callingPackage, "getNetworkTypeForSubscriber")) {
Robert Greenwalt36b23af2015-07-06 17:59:14 -07003506 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
3507 }
Robert Greenwalta5dcfcb2015-07-10 09:06:29 -07003508
Malcolm Chend965c8b2018-02-28 15:00:40 -08003509 final long identity = Binder.clearCallingIdentity();
3510 try {
3511 final Phone phone = getPhone(subId);
3512 if (phone != null) {
3513 return phone.getServiceState().getDataNetworkType();
3514 } else {
3515 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
3516 }
3517 } finally {
3518 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07003519 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003520 }
3521
3522 /**
3523 * Returns the data network type
3524 */
3525 @Override
Robert Greenwalt36b23af2015-07-06 17:59:14 -07003526 public int getDataNetworkType(String callingPackage) {
3527 return getDataNetworkTypeForSubscriber(getDefaultSubscription(), callingPackage);
Wink Saville36469e72014-06-11 15:17:00 -07003528 }
3529
3530 /**
3531 * Returns the data network type for a subId
3532 */
3533 @Override
Robert Greenwalt36b23af2015-07-06 17:59:14 -07003534 public int getDataNetworkTypeForSubscriber(int subId, String callingPackage) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08003535 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
Jeff Davidson913390f2018-02-23 17:11:49 -08003536 mApp, subId, callingPackage, "getDataNetworkTypeForSubscriber")) {
Robert Greenwalt36b23af2015-07-06 17:59:14 -07003537 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
3538 }
3539
Malcolm Chend965c8b2018-02-28 15:00:40 -08003540 final long identity = Binder.clearCallingIdentity();
3541 try {
3542 final Phone phone = getPhone(subId);
3543 if (phone != null) {
3544 return phone.getServiceState().getDataNetworkType();
3545 } else {
3546 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
3547 }
3548 } finally {
3549 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07003550 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003551 }
3552
3553 /**
Wink Saville36469e72014-06-11 15:17:00 -07003554 * Returns the Voice network type for a subId
3555 */
3556 @Override
Robert Greenwalta5dcfcb2015-07-10 09:06:29 -07003557 public int getVoiceNetworkTypeForSubscriber(int subId, String callingPackage) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08003558 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
Jeff Davidson913390f2018-02-23 17:11:49 -08003559 mApp, subId, callingPackage, "getDataNetworkTypeForSubscriber")) {
Robert Greenwalta5dcfcb2015-07-10 09:06:29 -07003560 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
3561 }
3562
Malcolm Chend965c8b2018-02-28 15:00:40 -08003563 final long identity = Binder.clearCallingIdentity();
3564 try {
3565 final Phone phone = getPhone(subId);
3566 if (phone != null) {
3567 return phone.getServiceState().getVoiceNetworkType();
3568 } else {
3569 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
3570 }
3571 } finally {
3572 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07003573 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003574 }
3575
3576 /**
3577 * @return true if a ICC card is present
3578 */
3579 public boolean hasIccCard() {
Wink Saville36469e72014-06-11 15:17:00 -07003580 // FIXME Make changes to pass defaultSimId of type int
Sanket Padawe13bac7b2017-03-20 15:04:47 -07003581 return hasIccCardUsingSlotIndex(mSubscriptionController.getSlotIndex(
3582 getDefaultSubscription()));
Wink Saville36469e72014-06-11 15:17:00 -07003583 }
3584
3585 /**
Sanket Padawe13bac7b2017-03-20 15:04:47 -07003586 * @return true if a ICC card is present for a slotIndex
Wink Saville36469e72014-06-11 15:17:00 -07003587 */
Sanket Padawe356d7632015-06-22 14:03:32 -07003588 @Override
Sanket Padawe13bac7b2017-03-20 15:04:47 -07003589 public boolean hasIccCardUsingSlotIndex(int slotIndex) {
Malcolm Chend965c8b2018-02-28 15:00:40 -08003590 final long identity = Binder.clearCallingIdentity();
3591 try {
3592 final Phone phone = PhoneFactory.getPhone(slotIndex);
3593 if (phone != null) {
3594 return phone.getIccCard().hasIccCard();
3595 } else {
3596 return false;
3597 }
3598 } finally {
3599 Binder.restoreCallingIdentity(identity);
Amit Mahajana6fc2a82015-01-06 11:53:51 -08003600 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003601 }
3602
3603 /**
3604 * Return if the current radio is LTE on CDMA. This
3605 * is a tri-state return value as for a period of time
3606 * the mode may be unknown.
3607 *
Robert Greenwalt36b23af2015-07-06 17:59:14 -07003608 * @param callingPackage the name of the package making the call.
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003609 * @return {@link Phone#LTE_ON_CDMA_UNKNOWN}, {@link Phone#LTE_ON_CDMA_FALSE}
Jake Hambye994d462014-02-03 13:10:13 -08003610 * or {@link Phone#LTE_ON_CDMA_TRUE}
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003611 */
Robert Greenwalt36b23af2015-07-06 17:59:14 -07003612 @Override
3613 public int getLteOnCdmaMode(String callingPackage) {
3614 return getLteOnCdmaModeForSubscriber(getDefaultSubscription(), callingPackage);
Wink Saville36469e72014-06-11 15:17:00 -07003615 }
3616
Sanket Padawe356d7632015-06-22 14:03:32 -07003617 @Override
Robert Greenwalt36b23af2015-07-06 17:59:14 -07003618 public int getLteOnCdmaModeForSubscriber(int subId, String callingPackage) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08003619 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
Jeff Davidson913390f2018-02-23 17:11:49 -08003620 mApp, subId, callingPackage, "getLteOnCdmaModeForSubscriber")) {
Robert Greenwalt36b23af2015-07-06 17:59:14 -07003621 return PhoneConstants.LTE_ON_CDMA_UNKNOWN;
3622 }
3623
Malcolm Chend965c8b2018-02-28 15:00:40 -08003624 final long identity = Binder.clearCallingIdentity();
3625 try {
3626 final Phone phone = getPhone(subId);
3627 if (phone == null) {
3628 return PhoneConstants.LTE_ON_CDMA_UNKNOWN;
3629 } else {
3630 return phone.getLteOnCdmaMode();
3631 }
3632 } finally {
3633 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07003634 }
Wink Saville36469e72014-06-11 15:17:00 -07003635 }
3636
Wink Saville36469e72014-06-11 15:17:00 -07003637 /**
3638 * {@hide}
3639 * Returns Default subId, 0 in the case of single standby.
3640 */
Wink Savilleb564aae2014-10-23 10:18:09 -07003641 private int getDefaultSubscription() {
Wink Savilleac1bdfd2014-11-20 23:04:44 -08003642 return mSubscriptionController.getDefaultSubId();
Wink Saville36469e72014-06-11 15:17:00 -07003643 }
3644
Shishir Agrawala9f32182016-04-12 12:00:16 -07003645 private int getSlotForDefaultSubscription() {
3646 return mSubscriptionController.getPhoneId(getDefaultSubscription());
3647 }
3648
Wink Savilleb564aae2014-10-23 10:18:09 -07003649 private int getPreferredVoiceSubscription() {
Wink Savilleac1bdfd2014-11-20 23:04:44 -08003650 return mSubscriptionController.getDefaultVoiceSubId();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003651 }
Ihab Awadf2177b72013-11-25 13:33:23 -08003652
Pengquan Meng466e2482018-09-21 15:54:48 -07003653 private boolean isActiveSubscription(int subId) {
3654 return mSubscriptionController.isActiveSubId(subId);
3655 }
3656
Ihab Awadf2177b72013-11-25 13:33:23 -08003657 /**
3658 * @see android.telephony.TelephonyManager.WifiCallingChoices
3659 */
3660 public int getWhenToMakeWifiCalls() {
Malcolm Chend965c8b2018-02-28 15:00:40 -08003661 final long identity = Binder.clearCallingIdentity();
3662 try {
Nazanin Bakhshi2f2cc692018-12-11 15:15:39 -08003663 return Settings.System.getInt(mApp.getContentResolver(),
Malcolm Chend965c8b2018-02-28 15:00:40 -08003664 Settings.System.WHEN_TO_MAKE_WIFI_CALLS,
3665 getWhenToMakeWifiCallsDefaultPreference());
3666 } finally {
3667 Binder.restoreCallingIdentity(identity);
3668 }
Ihab Awadf2177b72013-11-25 13:33:23 -08003669 }
3670
3671 /**
3672 * @see android.telephony.TelephonyManager.WifiCallingChoices
3673 */
3674 public void setWhenToMakeWifiCalls(int preference) {
Malcolm Chend965c8b2018-02-28 15:00:40 -08003675 final long identity = Binder.clearCallingIdentity();
3676 try {
3677 if (DBG) log("setWhenToMakeWifiCallsStr, storing setting = " + preference);
Nazanin Bakhshi2f2cc692018-12-11 15:15:39 -08003678 Settings.System.putInt(mApp.getContentResolver(),
Malcolm Chend965c8b2018-02-28 15:00:40 -08003679 Settings.System.WHEN_TO_MAKE_WIFI_CALLS, preference);
3680 } finally {
3681 Binder.restoreCallingIdentity(identity);
3682 }
Ihab Awadf9e92732013-12-05 18:02:52 -08003683 }
3684
Sailesh Nepald1e68152013-12-12 19:08:02 -08003685 private static int getWhenToMakeWifiCallsDefaultPreference() {
Santos Cordonda120f42014-08-06 04:44:34 -07003686 // TODO: Use a build property to choose this value.
Evan Charlton9829e882013-12-19 15:30:38 -08003687 return TelephonyManager.WifiCallingChoices.ALWAYS_USE;
Ihab Awadf2177b72013-11-25 13:33:23 -08003688 }
Shishir Agrawal69f68122013-12-16 17:25:49 -08003689
Jordan Liu73b078d2019-02-28 12:03:40 -08003690 private Phone getPhoneFromSlotIdOrThrowException(int slotIndex) {
3691 int phoneId = UiccController.getInstance().getPhoneIdFromSlotId(slotIndex);
3692 if (phoneId == -1) {
3693 throw new IllegalArgumentException("Given slot index: " + slotIndex
3694 + " does not correspond to an active phone");
3695 }
3696 return PhoneFactory.getPhone(phoneId);
3697 }
3698
Shishir Agrawal566b7612013-10-28 14:41:00 -07003699 @Override
Derek Tan740e1672017-06-27 14:56:27 -07003700 public IccOpenLogicalChannelResponse iccOpenLogicalChannel(
3701 int subId, String callingPackage, String aid, int p2) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08003702 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
3703 mApp, subId, "iccOpenLogicalChannel");
Malcolm Chend965c8b2018-02-28 15:00:40 -08003704 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
Jordan Liu73b078d2019-02-28 12:03:40 -08003705 if (DBG) {
3706 log("iccOpenLogicalChannel: subId=" + subId + " aid=" + aid + " p2=" + p2);
3707 }
3708 return iccOpenLogicalChannelWithPermission(getPhoneFromSubId(subId), callingPackage, aid,
3709 p2);
3710 }
Shishir Agrawal566b7612013-10-28 14:41:00 -07003711
Jordan Liu73b078d2019-02-28 12:03:40 -08003712
3713 @Override
3714 public IccOpenLogicalChannelResponse iccOpenLogicalChannelBySlot(
3715 int slotIndex, String callingPackage, String aid, int p2) {
3716 enforceModifyPermission();
3717 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
3718 if (DBG) {
3719 log("iccOpenLogicalChannelBySlot: slot=" + slotIndex + " aid=" + aid + " p2=" + p2);
3720 }
3721 return iccOpenLogicalChannelWithPermission(getPhoneFromSlotIdOrThrowException(slotIndex),
3722 callingPackage, aid, p2);
3723 }
3724
3725 private IccOpenLogicalChannelResponse iccOpenLogicalChannelWithPermission(Phone phone,
3726 String callingPackage, String aid, int p2) {
Malcolm Chend965c8b2018-02-28 15:00:40 -08003727 final long identity = Binder.clearCallingIdentity();
3728 try {
3729 if (TextUtils.equals(ISDR_AID, aid)) {
3730 // Only allows LPA to open logical channel to ISD-R.
Nazanin Bakhshi2f2cc692018-12-11 15:15:39 -08003731 ComponentInfo bestComponent = EuiccConnector.findBestComponent(getDefaultPhone()
3732 .getContext().getPackageManager());
Malcolm Chend965c8b2018-02-28 15:00:40 -08003733 if (bestComponent == null
3734 || !TextUtils.equals(callingPackage, bestComponent.packageName)) {
3735 loge("The calling package is not allowed to access ISD-R.");
3736 throw new SecurityException(
3737 "The calling package is not allowed to access ISD-R.");
3738 }
Derek Tan740e1672017-06-27 14:56:27 -07003739 }
Derek Tan740e1672017-06-27 14:56:27 -07003740
Malcolm Chend965c8b2018-02-28 15:00:40 -08003741 IccOpenLogicalChannelResponse response = (IccOpenLogicalChannelResponse) sendRequest(
Jordan Liu73b078d2019-02-28 12:03:40 -08003742 CMD_OPEN_CHANNEL, new Pair<String, Integer>(aid, p2), phone,
3743 null /* workSource */);
3744 if (DBG) log("iccOpenLogicalChannelWithPermission: " + response);
Malcolm Chend965c8b2018-02-28 15:00:40 -08003745 return response;
3746 } finally {
3747 Binder.restoreCallingIdentity(identity);
3748 }
Shishir Agrawal566b7612013-10-28 14:41:00 -07003749 }
3750
3751 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08003752 public boolean iccCloseLogicalChannel(int subId, int channel) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08003753 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
3754 mApp, subId, "iccCloseLogicalChannel");
Jordan Liu73b078d2019-02-28 12:03:40 -08003755 if (DBG) log("iccCloseLogicalChannel: subId=" + subId + " chnl=" + channel);
3756 return iccCloseLogicalChannelWithPermission(getPhoneFromSubId(subId), channel);
3757 }
Shishir Agrawal566b7612013-10-28 14:41:00 -07003758
Jordan Liu73b078d2019-02-28 12:03:40 -08003759 @Override
3760 public boolean iccCloseLogicalChannelBySlot(int slotIndex, int channel) {
3761 enforceModifyPermission();
3762 if (DBG) log("iccCloseLogicalChannelBySlot: slotIndex=" + slotIndex + " chnl=" + channel);
3763 return iccCloseLogicalChannelWithPermission(getPhoneFromSlotIdOrThrowException(slotIndex),
3764 channel);
3765 }
3766
3767 private boolean iccCloseLogicalChannelWithPermission(Phone phone, int channel) {
Malcolm Chend965c8b2018-02-28 15:00:40 -08003768 final long identity = Binder.clearCallingIdentity();
3769 try {
Malcolm Chend965c8b2018-02-28 15:00:40 -08003770 if (channel < 0) {
3771 return false;
3772 }
Jordan Liu73b078d2019-02-28 12:03:40 -08003773 Boolean success = (Boolean) sendRequest(CMD_CLOSE_CHANNEL, channel, phone,
3774 null /* workSource */);
3775 if (DBG) log("iccCloseLogicalChannelWithPermission: " + success);
Malcolm Chend965c8b2018-02-28 15:00:40 -08003776 return success;
3777 } finally {
3778 Binder.restoreCallingIdentity(identity);
Shishir Agrawal566b7612013-10-28 14:41:00 -07003779 }
Shishir Agrawal566b7612013-10-28 14:41:00 -07003780 }
3781
3782 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08003783 public String iccTransmitApduLogicalChannel(int subId, int channel, int cla,
Shishir Agrawal566b7612013-10-28 14:41:00 -07003784 int command, int p1, int p2, int p3, String data) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08003785 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
3786 mApp, subId, "iccTransmitApduLogicalChannel");
Jordan Liu73b078d2019-02-28 12:03:40 -08003787 if (DBG) {
3788 log("iccTransmitApduLogicalChannel: subId=" + subId + " chnl=" + channel
3789 + " cla=" + cla + " cmd=" + command + " p1=" + p1 + " p2=" + p2 + " p3="
3790 + p3 + " data=" + data);
3791 }
3792 return iccTransmitApduLogicalChannelWithPermission(getPhoneFromSubId(subId), channel, cla,
3793 command, p1, p2, p3, data);
3794 }
Shishir Agrawal566b7612013-10-28 14:41:00 -07003795
Jordan Liu73b078d2019-02-28 12:03:40 -08003796 @Override
3797 public String iccTransmitApduLogicalChannelBySlot(int slotIndex, int channel, int cla,
3798 int command, int p1, int p2, int p3, String data) {
3799 enforceModifyPermission();
3800 if (DBG) {
3801 log("iccTransmitApduLogicalChannelBySlot: slotIndex=" + slotIndex + " chnl=" + channel
3802 + " cla=" + cla + " cmd=" + command + " p1=" + p1 + " p2=" + p2 + " p3="
3803 + p3 + " data=" + data);
3804 }
3805 return iccTransmitApduLogicalChannelWithPermission(
3806 getPhoneFromSlotIdOrThrowException(slotIndex), channel, cla, command, p1, p2, p3,
3807 data);
3808 }
3809
3810 private String iccTransmitApduLogicalChannelWithPermission(Phone phone, int channel, int cla,
3811 int command, int p1, int p2, int p3, String data) {
Malcolm Chend965c8b2018-02-28 15:00:40 -08003812 final long identity = Binder.clearCallingIdentity();
3813 try {
Malcolm Chend965c8b2018-02-28 15:00:40 -08003814 if (channel < 0) {
3815 return "";
3816 }
Shishir Agrawal566b7612013-10-28 14:41:00 -07003817
Malcolm Chend965c8b2018-02-28 15:00:40 -08003818 IccIoResult response = (IccIoResult) sendRequest(CMD_TRANSMIT_APDU_LOGICAL_CHANNEL,
Jordan Liu73b078d2019-02-28 12:03:40 -08003819 new IccAPDUArgument(channel, cla, command, p1, p2, p3, data), phone,
3820 null /* workSource */);
3821 if (DBG) log("iccTransmitApduLogicalChannelWithPermission: " + response);
Shishir Agrawal566b7612013-10-28 14:41:00 -07003822
Malcolm Chend965c8b2018-02-28 15:00:40 -08003823 // Append the returned status code to the end of the response payload.
3824 String s = Integer.toHexString(
3825 (response.sw1 << 8) + response.sw2 + 0x10000).substring(1);
3826 if (response.payload != null) {
3827 s = IccUtils.bytesToHexString(response.payload) + s;
3828 }
3829 return s;
3830 } finally {
3831 Binder.restoreCallingIdentity(identity);
Shishir Agrawal5ec14172014-08-05 17:05:45 -07003832 }
Shishir Agrawal566b7612013-10-28 14:41:00 -07003833 }
Jake Hambye994d462014-02-03 13:10:13 -08003834
Evan Charltonc66da362014-05-16 14:06:40 -07003835 @Override
Holly Jiuyu Sun1cc2d552018-01-26 15:51:16 -08003836 public String iccTransmitApduBasicChannel(int subId, String callingPackage, int cla,
3837 int command, int p1, int p2, int p3, String data) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08003838 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
3839 mApp, subId, "iccTransmitApduBasicChannel");
Malcolm Chend965c8b2018-02-28 15:00:40 -08003840 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
Jordan Liu73b078d2019-02-28 12:03:40 -08003841 if (DBG) {
3842 log("iccTransmitApduBasicChannel: subId=" + subId + " cla=" + cla + " cmd="
3843 + command + " p1=" + p1 + " p2=" + p2 + " p3=" + p3 + " data=" + data);
3844 }
3845 return iccTransmitApduBasicChannelWithPermission(getPhoneFromSubId(subId), callingPackage,
3846 cla, command, p1, p2, p3, data);
3847 }
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07003848
Jordan Liu73b078d2019-02-28 12:03:40 -08003849 @Override
3850 public String iccTransmitApduBasicChannelBySlot(int slotIndex, String callingPackage, int cla,
3851 int command, int p1, int p2, int p3, String data) {
3852 enforceModifyPermission();
3853 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
3854 if (DBG) {
3855 log("iccTransmitApduBasicChannelBySlot: slotIndex=" + slotIndex + " cla=" + cla
3856 + " cmd=" + command + " p1=" + p1 + " p2=" + p2 + " p3=" + p3
3857 + " data=" + data);
3858 }
3859
3860 return iccTransmitApduBasicChannelWithPermission(
3861 getPhoneFromSlotIdOrThrowException(slotIndex), callingPackage, cla, command, p1,
3862 p2, p3, data);
3863 }
3864
3865 // open APDU basic channel assuming the caller has sufficient permissions
3866 private String iccTransmitApduBasicChannelWithPermission(Phone phone, String callingPackage,
3867 int cla, int command, int p1, int p2, int p3, String data) {
Malcolm Chend965c8b2018-02-28 15:00:40 -08003868 final long identity = Binder.clearCallingIdentity();
3869 try {
3870 if (command == SELECT_COMMAND && p1 == SELECT_P1 && p2 == SELECT_P2 && p3 == SELECT_P3
3871 && TextUtils.equals(ISDR_AID, data)) {
3872 // Only allows LPA to select ISD-R.
Nazanin Bakhshi2f2cc692018-12-11 15:15:39 -08003873 ComponentInfo bestComponent = EuiccConnector.findBestComponent(getDefaultPhone()
3874 .getContext().getPackageManager());
Malcolm Chend965c8b2018-02-28 15:00:40 -08003875 if (bestComponent == null
3876 || !TextUtils.equals(callingPackage, bestComponent.packageName)) {
3877 loge("The calling package is not allowed to select ISD-R.");
3878 throw new SecurityException(
3879 "The calling package is not allowed to select ISD-R.");
3880 }
Holly Jiuyu Sun1cc2d552018-01-26 15:51:16 -08003881 }
Holly Jiuyu Sun1cc2d552018-01-26 15:51:16 -08003882
Malcolm Chend965c8b2018-02-28 15:00:40 -08003883 IccIoResult response = (IccIoResult) sendRequest(CMD_TRANSMIT_APDU_BASIC_CHANNEL,
Jordan Liu73b078d2019-02-28 12:03:40 -08003884 new IccAPDUArgument(0, cla, command, p1, p2, p3, data), phone,
3885 null /* workSource */);
3886 if (DBG) log("iccTransmitApduBasicChannelWithPermission: " + response);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07003887
Malcolm Chend965c8b2018-02-28 15:00:40 -08003888 // Append the returned status code to the end of the response payload.
3889 String s = Integer.toHexString(
3890 (response.sw1 << 8) + response.sw2 + 0x10000).substring(1);
3891 if (response.payload != null) {
3892 s = IccUtils.bytesToHexString(response.payload) + s;
3893 }
3894 return s;
3895 } finally {
3896 Binder.restoreCallingIdentity(identity);
Shishir Agrawal5ec14172014-08-05 17:05:45 -07003897 }
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07003898 }
3899
3900 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08003901 public byte[] iccExchangeSimIO(int subId, int fileID, int command, int p1, int p2, int p3,
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07003902 String filePath) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08003903 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
3904 mApp, subId, "iccExchangeSimIO");
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07003905
Malcolm Chend965c8b2018-02-28 15:00:40 -08003906 final long identity = Binder.clearCallingIdentity();
3907 try {
3908 if (DBG) {
3909 log("Exchange SIM_IO " + subId + ":" + fileID + ":" + command + " "
3910 + p1 + " " + p2 + " " + p3 + ":" + filePath);
3911 }
3912
3913 IccIoResult response =
3914 (IccIoResult) sendRequest(CMD_EXCHANGE_SIM_IO,
3915 new IccAPDUArgument(-1, fileID, command, p1, p2, p3, filePath),
3916 subId);
3917
3918 if (DBG) {
3919 log("Exchange SIM_IO [R]" + response);
3920 }
3921
3922 byte[] result = null;
3923 int length = 2;
3924 if (response.payload != null) {
3925 length = 2 + response.payload.length;
3926 result = new byte[length];
3927 System.arraycopy(response.payload, 0, result, 0, response.payload.length);
3928 } else {
3929 result = new byte[length];
3930 }
3931
3932 result[length - 1] = (byte) response.sw2;
3933 result[length - 2] = (byte) response.sw1;
3934 return result;
3935 } finally {
3936 Binder.restoreCallingIdentity(identity);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07003937 }
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07003938 }
3939
Nathan Haroldb3014052017-01-25 15:57:32 -08003940 /**
3941 * Get the forbidden PLMN List from the given app type (ex APPTYPE_USIM)
3942 * on a particular subscription
3943 */
sqianb6e41952018-03-12 14:54:01 -07003944 public String[] getForbiddenPlmns(int subId, int appType, String callingPackage) {
3945 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
3946 mApp, subId, callingPackage, "getForbiddenPlmns")) {
3947 return null;
3948 }
Malcolm Chend965c8b2018-02-28 15:00:40 -08003949
3950 final long identity = Binder.clearCallingIdentity();
3951 try {
3952 if (appType != TelephonyManager.APPTYPE_USIM
3953 && appType != TelephonyManager.APPTYPE_SIM) {
3954 loge("getForbiddenPlmnList(): App Type must be USIM or SIM");
3955 return null;
3956 }
3957 Object response = sendRequest(
3958 CMD_GET_FORBIDDEN_PLMNS, new Integer(appType), subId);
3959 if (response instanceof String[]) {
3960 return (String[]) response;
3961 }
3962 // Response is an Exception of some kind,
3963 // which is signalled to the user as a NULL retval
Nathan Haroldb3014052017-01-25 15:57:32 -08003964 return null;
Malcolm Chend965c8b2018-02-28 15:00:40 -08003965 } finally {
3966 Binder.restoreCallingIdentity(identity);
Nathan Haroldb3014052017-01-25 15:57:32 -08003967 }
Nathan Haroldb3014052017-01-25 15:57:32 -08003968 }
3969
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07003970 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08003971 public String sendEnvelopeWithStatus(int subId, String content) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08003972 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
3973 mApp, subId, "sendEnvelopeWithStatus");
Evan Charltonc66da362014-05-16 14:06:40 -07003974
Malcolm Chend965c8b2018-02-28 15:00:40 -08003975 final long identity = Binder.clearCallingIdentity();
3976 try {
3977 IccIoResult response = (IccIoResult) sendRequest(CMD_SEND_ENVELOPE, content, subId);
3978 if (response.payload == null) {
3979 return "";
3980 }
Evan Charltonc66da362014-05-16 14:06:40 -07003981
Malcolm Chend965c8b2018-02-28 15:00:40 -08003982 // Append the returned status code to the end of the response payload.
3983 String s = Integer.toHexString(
3984 (response.sw1 << 8) + response.sw2 + 0x10000).substring(1);
3985 s = IccUtils.bytesToHexString(response.payload) + s;
3986 return s;
3987 } finally {
3988 Binder.restoreCallingIdentity(identity);
3989 }
Evan Charltonc66da362014-05-16 14:06:40 -07003990 }
3991
Jake Hambye994d462014-02-03 13:10:13 -08003992 /**
3993 * Read one of the NV items defined in {@link com.android.internal.telephony.RadioNVItems}
3994 * and {@code ril_nv_items.h}. Used for device configuration by some CDMA operators.
3995 *
3996 * @param itemID the ID of the item to read
3997 * @return the NV item as a String, or null on error.
3998 */
3999 @Override
4000 public String nvReadItem(int itemID) {
vagdevie435a3e2018-08-15 16:01:53 -07004001 WorkSource workSource = getWorkSource(Binder.getCallingUid());
Jeff Davidson7e17e312018-02-13 18:17:36 -08004002 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
4003 mApp, getDefaultSubscription(), "nvReadItem");
Malcolm Chend965c8b2018-02-28 15:00:40 -08004004
4005 final long identity = Binder.clearCallingIdentity();
4006 try {
4007 if (DBG) log("nvReadItem: item " + itemID);
vagdevie435a3e2018-08-15 16:01:53 -07004008 String value = (String) sendRequest(CMD_NV_READ_ITEM, itemID, workSource);
Malcolm Chend965c8b2018-02-28 15:00:40 -08004009 if (DBG) log("nvReadItem: item " + itemID + " is \"" + value + '"');
4010 return value;
4011 } finally {
4012 Binder.restoreCallingIdentity(identity);
4013 }
Jake Hambye994d462014-02-03 13:10:13 -08004014 }
4015
4016 /**
4017 * Write one of the NV items defined in {@link com.android.internal.telephony.RadioNVItems}
4018 * and {@code ril_nv_items.h}. Used for device configuration by some CDMA operators.
4019 *
4020 * @param itemID the ID of the item to read
4021 * @param itemValue the value to write, as a String
4022 * @return true on success; false on any failure
4023 */
4024 @Override
4025 public boolean nvWriteItem(int itemID, String itemValue) {
vagdevie435a3e2018-08-15 16:01:53 -07004026 WorkSource workSource = getWorkSource(Binder.getCallingUid());
Jeff Davidson7e17e312018-02-13 18:17:36 -08004027 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
4028 mApp, getDefaultSubscription(), "nvWriteItem");
Malcolm Chend965c8b2018-02-28 15:00:40 -08004029
4030 final long identity = Binder.clearCallingIdentity();
4031 try {
4032 if (DBG) log("nvWriteItem: item " + itemID + " value \"" + itemValue + '"');
4033 Boolean success = (Boolean) sendRequest(CMD_NV_WRITE_ITEM,
vagdevie435a3e2018-08-15 16:01:53 -07004034 new Pair<Integer, String>(itemID, itemValue), workSource);
Malcolm Chend965c8b2018-02-28 15:00:40 -08004035 if (DBG) log("nvWriteItem: item " + itemID + ' ' + (success ? "ok" : "fail"));
4036 return success;
4037 } finally {
4038 Binder.restoreCallingIdentity(identity);
4039 }
Jake Hambye994d462014-02-03 13:10:13 -08004040 }
4041
4042 /**
4043 * Update the CDMA Preferred Roaming List (PRL) in the radio NV storage.
4044 * Used for device configuration by some CDMA operators.
4045 *
4046 * @param preferredRoamingList byte array containing the new PRL
4047 * @return true on success; false on any failure
4048 */
4049 @Override
4050 public boolean nvWriteCdmaPrl(byte[] preferredRoamingList) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08004051 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
4052 mApp, getDefaultSubscription(), "nvWriteCdmaPrl");
Malcolm Chend965c8b2018-02-28 15:00:40 -08004053
4054 final long identity = Binder.clearCallingIdentity();
4055 try {
4056 if (DBG) log("nvWriteCdmaPrl: value: " + HexDump.toHexString(preferredRoamingList));
4057 Boolean success = (Boolean) sendRequest(CMD_NV_WRITE_CDMA_PRL, preferredRoamingList);
4058 if (DBG) log("nvWriteCdmaPrl: " + (success ? "ok" : "fail"));
4059 return success;
4060 } finally {
4061 Binder.restoreCallingIdentity(identity);
4062 }
Jake Hambye994d462014-02-03 13:10:13 -08004063 }
4064
4065 /**
chen xu1cc0abe2018-10-26 17:39:23 -07004066 * Rollback modem configurations to factory default except some config which are in whitelist.
Jake Hambye994d462014-02-03 13:10:13 -08004067 * Used for device configuration by some CDMA operators.
4068 *
chen xu1cc0abe2018-10-26 17:39:23 -07004069 * @param slotIndex - device slot.
4070 *
Jake Hambye994d462014-02-03 13:10:13 -08004071 * @return true on success; false on any failure
4072 */
4073 @Override
chen xu1cc0abe2018-10-26 17:39:23 -07004074 public boolean resetModemConfig(int slotIndex) {
4075 Phone phone = PhoneFactory.getPhone(slotIndex);
4076 if (phone != null) {
4077 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
4078 mApp, phone.getSubId(), "resetModemConfig");
Malcolm Chend965c8b2018-02-28 15:00:40 -08004079
chen xu1cc0abe2018-10-26 17:39:23 -07004080 final long identity = Binder.clearCallingIdentity();
4081 try {
4082 Boolean success = (Boolean) sendRequest(CMD_RESET_MODEM_CONFIG, null);
4083 if (DBG) log("resetModemConfig:" + ' ' + (success ? "ok" : "fail"));
4084 return success;
4085 } finally {
4086 Binder.restoreCallingIdentity(identity);
4087 }
Malcolm Chend965c8b2018-02-28 15:00:40 -08004088 }
chen xu1cc0abe2018-10-26 17:39:23 -07004089 return false;
4090 }
4091
4092 /**
4093 * Generate a radio modem reset. Used for device configuration by some CDMA operators.
4094 *
4095 * @param slotIndex - device slot.
4096 *
4097 * @return true on success; false on any failure
4098 */
4099 @Override
4100 public boolean rebootModem(int slotIndex) {
4101 Phone phone = PhoneFactory.getPhone(slotIndex);
4102 if (phone != null) {
4103 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
4104 mApp, phone.getSubId(), "rebootModem");
4105
4106 final long identity = Binder.clearCallingIdentity();
4107 try {
4108 Boolean success = (Boolean) sendRequest(CMD_MODEM_REBOOT, null);
4109 if (DBG) log("rebootModem:" + ' ' + (success ? "ok" : "fail"));
4110 return success;
4111 } finally {
4112 Binder.restoreCallingIdentity(identity);
4113 }
4114 }
4115 return false;
Jake Hambye994d462014-02-03 13:10:13 -08004116 }
Jake Hamby7c27be32014-03-03 13:25:59 -08004117
Svet Ganovb320e182015-04-16 12:30:10 -07004118 public String[] getPcscfAddress(String apnType, String callingPackage) {
Nazanin Bakhshi2f2cc692018-12-11 15:15:39 -08004119 final Phone defaultPhone = getDefaultPhone();
Jeff Davidson7e17e312018-02-13 18:17:36 -08004120 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
Nazanin Bakhshi2f2cc692018-12-11 15:15:39 -08004121 mApp, defaultPhone.getSubId(), callingPackage, "getPcscfAddress")) {
Svet Ganovb320e182015-04-16 12:30:10 -07004122 return new String[0];
4123 }
4124
Malcolm Chend965c8b2018-02-28 15:00:40 -08004125 final long identity = Binder.clearCallingIdentity();
4126 try {
Nazanin Bakhshi2f2cc692018-12-11 15:15:39 -08004127 return defaultPhone.getPcscfAddress(apnType);
Malcolm Chend965c8b2018-02-28 15:00:40 -08004128 } finally {
4129 Binder.restoreCallingIdentity(identity);
4130 }
Wink Saville36469e72014-06-11 15:17:00 -07004131 }
4132
Brad Ebinger51f743a2017-01-23 13:50:20 -08004133 /**
Brad Ebinger22bc3e42018-01-16 09:39:35 -08004134 * Enables IMS for the framework. This will trigger IMS registration and ImsFeature capability
4135 * status updates, if not already enabled.
Brad Ebinger51f743a2017-01-23 13:50:20 -08004136 */
Brad Ebinger22bc3e42018-01-16 09:39:35 -08004137 public void enableIms(int slotId) {
Brad Ebinger51f743a2017-01-23 13:50:20 -08004138 enforceModifyPermission();
Malcolm Chend965c8b2018-02-28 15:00:40 -08004139
4140 final long identity = Binder.clearCallingIdentity();
4141 try {
Brad Ebingereb160e22019-01-23 15:06:19 -08004142 ImsResolver resolver = PhoneFactory.getImsResolver();
4143 if (resolver == null) {
4144 // may happen if the device does not support IMS.
4145 return;
4146 }
4147 resolver.enableIms(slotId);
Malcolm Chend965c8b2018-02-28 15:00:40 -08004148 } finally {
4149 Binder.restoreCallingIdentity(identity);
4150 }
Brad Ebinger34bef922017-11-09 10:27:08 -08004151 }
4152
4153 /**
Brad Ebinger22bc3e42018-01-16 09:39:35 -08004154 * Disables IMS for the framework. This will trigger IMS de-registration and trigger ImsFeature
4155 * status updates to disabled.
Brad Ebinger34bef922017-11-09 10:27:08 -08004156 */
Brad Ebinger22bc3e42018-01-16 09:39:35 -08004157 public void disableIms(int slotId) {
4158 enforceModifyPermission();
Malcolm Chend965c8b2018-02-28 15:00:40 -08004159
4160 final long identity = Binder.clearCallingIdentity();
4161 try {
Brad Ebingereb160e22019-01-23 15:06:19 -08004162 ImsResolver resolver = PhoneFactory.getImsResolver();
4163 if (resolver == null) {
4164 // may happen if the device does not support IMS.
4165 return;
4166 }
4167 resolver.disableIms(slotId);
Malcolm Chend965c8b2018-02-28 15:00:40 -08004168 } finally {
4169 Binder.restoreCallingIdentity(identity);
4170 }
Brad Ebinger22bc3e42018-01-16 09:39:35 -08004171 }
4172
4173 /**
4174 * Returns the {@link IImsMmTelFeature} that corresponds to the given slot Id for the MMTel
4175 * feature or {@link null} if the service is not available. If the feature is available, the
4176 * {@link IImsServiceFeatureCallback} callback is registered as a listener for feature updates.
4177 */
4178 public IImsMmTelFeature getMmTelFeatureAndListen(int slotId,
Brad Ebinger34bef922017-11-09 10:27:08 -08004179 IImsServiceFeatureCallback callback) {
4180 enforceModifyPermission();
Malcolm Chend965c8b2018-02-28 15:00:40 -08004181
4182 final long identity = Binder.clearCallingIdentity();
4183 try {
Brad Ebingereb160e22019-01-23 15:06:19 -08004184 ImsResolver resolver = PhoneFactory.getImsResolver();
4185 if (resolver == null) {
4186 // may happen if the device does not support IMS.
4187 return null;
4188 }
4189 return resolver.getMmTelFeatureAndListen(slotId, callback);
Malcolm Chend965c8b2018-02-28 15:00:40 -08004190 } finally {
4191 Binder.restoreCallingIdentity(identity);
4192 }
Brad Ebinger34bef922017-11-09 10:27:08 -08004193 }
4194
4195 /**
4196 * Returns the {@link IImsRcsFeature} that corresponds to the given slot Id for the RCS
4197 * feature during emergency calling or {@link null} if the service is not available. If the
4198 * feature is available, the {@link IImsServiceFeatureCallback} callback is registered as a
4199 * listener for feature updates.
4200 */
4201 public IImsRcsFeature getRcsFeatureAndListen(int slotId, IImsServiceFeatureCallback callback) {
4202 enforceModifyPermission();
Malcolm Chend965c8b2018-02-28 15:00:40 -08004203
4204 final long identity = Binder.clearCallingIdentity();
4205 try {
Brad Ebingereb160e22019-01-23 15:06:19 -08004206 ImsResolver resolver = PhoneFactory.getImsResolver();
4207 if (resolver == null) {
4208 // may happen if the device does not support IMS.
4209 return null;
4210 }
4211 return resolver.getRcsFeatureAndListen(slotId, callback);
Malcolm Chend965c8b2018-02-28 15:00:40 -08004212 } finally {
4213 Binder.restoreCallingIdentity(identity);
4214 }
Brad Ebinger51f743a2017-01-23 13:50:20 -08004215 }
4216
Brad Ebinger5f64b052017-12-14 14:26:15 -08004217 /**
4218 * Returns the {@link IImsRegistration} structure associated with the slotId and feature
Brad Ebingereb160e22019-01-23 15:06:19 -08004219 * specified or null if IMS is not supported on the slot specified.
Brad Ebinger5f64b052017-12-14 14:26:15 -08004220 */
4221 public IImsRegistration getImsRegistration(int slotId, int feature) throws RemoteException {
4222 enforceModifyPermission();
Malcolm Chend965c8b2018-02-28 15:00:40 -08004223
4224 final long identity = Binder.clearCallingIdentity();
4225 try {
Brad Ebingereb160e22019-01-23 15:06:19 -08004226 ImsResolver resolver = PhoneFactory.getImsResolver();
4227 if (resolver == null) {
4228 // may happen if the device does not support IMS.
4229 return null;
4230 }
4231 return resolver.getImsRegistration(slotId, feature);
Malcolm Chend965c8b2018-02-28 15:00:40 -08004232 } finally {
4233 Binder.restoreCallingIdentity(identity);
4234 }
Brad Ebinger5f64b052017-12-14 14:26:15 -08004235 }
4236
Brad Ebinger22bc3e42018-01-16 09:39:35 -08004237 /**
4238 * Returns the {@link IImsConfig} structure associated with the slotId and feature
Brad Ebingereb160e22019-01-23 15:06:19 -08004239 * specified or null if IMS is not supported on the slot specified.
Brad Ebinger22bc3e42018-01-16 09:39:35 -08004240 */
4241 public IImsConfig getImsConfig(int slotId, int feature) throws RemoteException {
4242 enforceModifyPermission();
Malcolm Chend965c8b2018-02-28 15:00:40 -08004243
4244 final long identity = Binder.clearCallingIdentity();
4245 try {
Brad Ebingereb160e22019-01-23 15:06:19 -08004246 ImsResolver resolver = PhoneFactory.getImsResolver();
4247 if (resolver == null) {
4248 // may happen if the device does not support IMS.
4249 return null;
4250 }
4251 return resolver.getImsConfig(slotId, feature);
Malcolm Chend965c8b2018-02-28 15:00:40 -08004252 } finally {
4253 Binder.restoreCallingIdentity(identity);
4254 }
Brad Ebinger22bc3e42018-01-16 09:39:35 -08004255 }
4256
Brad Ebinger884c07b2018-02-15 16:17:40 -08004257 /**
Brad Ebingerdac2f002018-04-03 15:17:52 -07004258 * Sets the ImsService Package Name that Telephony will bind to.
4259 *
4260 * @param slotId the slot ID that the ImsService should bind for.
4261 * @param isCarrierImsService true if the ImsService is the carrier override, false if the
4262 * ImsService is the device default ImsService.
4263 * @param packageName The package name of the application that contains the ImsService to bind
4264 * to.
4265 * @return true if setting the ImsService to bind to succeeded, false if it did not.
4266 * @hide
4267 */
4268 public boolean setImsService(int slotId, boolean isCarrierImsService, String packageName) {
Brad Ebingerde696de2018-04-06 09:56:40 -07004269 int[] subIds = SubscriptionManager.getSubId(slotId);
4270 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(mApp,
4271 (subIds != null ? subIds[0] : SubscriptionManager.INVALID_SUBSCRIPTION_ID),
4272 "setImsService");
4273
Malcolm Chend965c8b2018-02-28 15:00:40 -08004274 final long identity = Binder.clearCallingIdentity();
4275 try {
Brad Ebingereb160e22019-01-23 15:06:19 -08004276 ImsResolver resolver = PhoneFactory.getImsResolver();
4277 if (resolver == null) {
4278 // may happen if the device does not support IMS.
4279 return false;
4280 }
4281 return resolver.overrideImsServiceConfiguration(slotId, isCarrierImsService,
4282 packageName);
Malcolm Chend965c8b2018-02-28 15:00:40 -08004283 } finally {
4284 Binder.restoreCallingIdentity(identity);
4285 }
Brad Ebingerdac2f002018-04-03 15:17:52 -07004286 }
4287
4288 /**
4289 * Return the ImsService configuration.
4290 *
4291 * @param slotId The slot that the ImsService is associated with.
4292 * @param isCarrierImsService true, if the ImsService is a carrier override, false if it is
4293 * the device default.
4294 * @return the package name of the ImsService configuration.
4295 */
4296 public String getImsService(int slotId, boolean isCarrierImsService) {
Brad Ebingerde696de2018-04-06 09:56:40 -07004297 int[] subIds = SubscriptionManager.getSubId(slotId);
4298 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(mApp,
4299 (subIds != null ? subIds[0] : SubscriptionManager.INVALID_SUBSCRIPTION_ID),
4300 "getImsService");
4301
Malcolm Chend965c8b2018-02-28 15:00:40 -08004302 final long identity = Binder.clearCallingIdentity();
4303 try {
Brad Ebingereb160e22019-01-23 15:06:19 -08004304 ImsResolver resolver = PhoneFactory.getImsResolver();
4305 if (resolver == null) {
4306 // may happen if the device does not support IMS.
4307 return "";
4308 }
4309 return resolver.getImsServiceConfiguration(slotId, isCarrierImsService);
Malcolm Chend965c8b2018-02-28 15:00:40 -08004310 } finally {
4311 Binder.restoreCallingIdentity(identity);
4312 }
Brad Ebingerdac2f002018-04-03 15:17:52 -07004313 }
4314
Wink Saville36469e72014-06-11 15:17:00 -07004315 public void setImsRegistrationState(boolean registered) {
4316 enforceModifyPermission();
Malcolm Chend965c8b2018-02-28 15:00:40 -08004317
4318 final long identity = Binder.clearCallingIdentity();
4319 try {
Nazanin Bakhshi2f2cc692018-12-11 15:15:39 -08004320 getDefaultPhone().setImsRegistrationState(registered);
Malcolm Chend965c8b2018-02-28 15:00:40 -08004321 } finally {
4322 Binder.restoreCallingIdentity(identity);
4323 }
Wink Saville36469e72014-06-11 15:17:00 -07004324 }
4325
4326 /**
Stuart Scott54788802015-03-30 13:18:01 -07004327 * Set the network selection mode to automatic.
4328 *
4329 */
4330 @Override
4331 public void setNetworkSelectionModeAutomatic(int subId) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08004332 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
4333 mApp, subId, "setNetworkSelectionModeAutomatic");
Malcolm Chend965c8b2018-02-28 15:00:40 -08004334
Pengquan Meng466e2482018-09-21 15:54:48 -07004335 if (!isActiveSubscription(subId)) {
4336 return;
4337 }
4338
Malcolm Chend965c8b2018-02-28 15:00:40 -08004339 final long identity = Binder.clearCallingIdentity();
4340 try {
4341 if (DBG) log("setNetworkSelectionModeAutomatic: subId " + subId);
4342 sendRequest(CMD_SET_NETWORK_SELECTION_MODE_AUTOMATIC, null, subId);
4343 } finally {
4344 Binder.restoreCallingIdentity(identity);
4345 }
Stuart Scott54788802015-03-30 13:18:01 -07004346 }
4347
Pengquan Menga4d9cff2018-09-20 14:57:26 -07004348 /**
4349 * Ask the radio to connect to the input network and change selection mode to manual.
4350 *
4351 * @param subId the id of the subscription.
4352 * @param operatorInfo the operator information, included the PLMN, long name and short name of
4353 * the operator to attach to.
4354 * @param persistSelection whether the selection will persist until reboot. If true, only allows
4355 * attaching to the selected PLMN until reboot; otherwise, attach to the chosen PLMN and resume
4356 * normal network selection next time.
4357 * @return {@code true} on success; {@code true} on any failure.
Shishir Agrawal302c8692015-06-19 13:49:39 -07004358 */
4359 @Override
Pengquan Menga4d9cff2018-09-20 14:57:26 -07004360 public boolean setNetworkSelectionModeManual(
4361 int subId, OperatorInfo operatorInfo, boolean persistSelection) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08004362 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
4363 mApp, subId, "setNetworkSelectionModeManual");
Pengquan Meng466e2482018-09-21 15:54:48 -07004364
4365 if (!isActiveSubscription(subId)) {
4366 return false;
4367 }
4368
Malcolm Chend965c8b2018-02-28 15:00:40 -08004369 final long identity = Binder.clearCallingIdentity();
4370 try {
Pengquan Menga4d9cff2018-09-20 14:57:26 -07004371 ManualNetworkSelectionArgument arg = new ManualNetworkSelectionArgument(operatorInfo,
Malcolm Chend965c8b2018-02-28 15:00:40 -08004372 persistSelection);
Pengquan Menga4d9cff2018-09-20 14:57:26 -07004373 if (DBG) {
4374 log("setNetworkSelectionModeManual: subId: " + subId
4375 + " operator: " + operatorInfo);
4376 }
Malcolm Chend965c8b2018-02-28 15:00:40 -08004377 return (Boolean) sendRequest(CMD_SET_NETWORK_SELECTION_MODE_MANUAL, arg, subId);
4378 } finally {
4379 Binder.restoreCallingIdentity(identity);
4380 }
Shishir Agrawal302c8692015-06-19 13:49:39 -07004381 }
4382
4383 /**
4384 * Scans for available networks.
4385 */
4386 @Override
Hall Liuf19c44f2018-11-27 14:38:17 -08004387 public CellNetworkScanResult getCellNetworkScanResults(int subId, String callingPackage) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08004388 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
4389 mApp, subId, "getCellNetworkScanResults");
Hall Liuf19c44f2018-11-27 14:38:17 -08004390 LocationAccessPolicy.LocationPermissionResult locationResult =
4391 LocationAccessPolicy.checkLocationPermission(mApp,
4392 new LocationAccessPolicy.LocationPermissionQuery.Builder()
4393 .setCallingPackage(callingPackage)
4394 .setCallingPid(Binder.getCallingPid())
4395 .setCallingUid(Binder.getCallingUid())
4396 .setMethod("getCellNetworkScanResults")
4397 .setMinSdkVersionForFine(Build.VERSION_CODES.Q)
4398 .build());
4399 switch (locationResult) {
4400 case DENIED_HARD:
4401 throw new SecurityException("Not allowed to access scan results -- location");
4402 case DENIED_SOFT:
4403 return null;
4404 }
Malcolm Chend965c8b2018-02-28 15:00:40 -08004405
Pengquan Meng0c05b502018-09-06 09:59:22 -07004406 long identity = Binder.clearCallingIdentity();
Malcolm Chend965c8b2018-02-28 15:00:40 -08004407 try {
4408 if (DBG) log("getCellNetworkScanResults: subId " + subId);
Pengquan Meng0c05b502018-09-06 09:59:22 -07004409 return (CellNetworkScanResult) sendRequest(
Malcolm Chend965c8b2018-02-28 15:00:40 -08004410 CMD_PERFORM_NETWORK_SCAN, null, subId);
Malcolm Chend965c8b2018-02-28 15:00:40 -08004411 } finally {
4412 Binder.restoreCallingIdentity(identity);
4413 }
Shishir Agrawal302c8692015-06-19 13:49:39 -07004414 }
4415
4416 /**
yinxub1bed742017-04-17 11:45:04 -07004417 * Starts a new network scan and returns the id of this scan.
yinxu504e1392017-04-12 16:03:22 -07004418 *
yinxub1bed742017-04-17 11:45:04 -07004419 * @param subId id of the subscription
4420 * @param request contains the radio access networks with bands/channels to scan
4421 * @param messenger callback messenger for scan results or errors
4422 * @param binder for the purpose of auto clean when the user thread crashes
yinxu504e1392017-04-12 16:03:22 -07004423 * @return the id of the requested scan which can be used to stop the scan.
4424 */
4425 @Override
4426 public int requestNetworkScan(int subId, NetworkScanRequest request, Messenger messenger,
Hall Liuf19c44f2018-11-27 14:38:17 -08004427 IBinder binder, String callingPackage) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08004428 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
4429 mApp, subId, "requestNetworkScan");
Hall Liuf19c44f2018-11-27 14:38:17 -08004430 LocationAccessPolicy.LocationPermissionResult locationResult =
4431 LocationAccessPolicy.checkLocationPermission(mApp,
4432 new LocationAccessPolicy.LocationPermissionQuery.Builder()
4433 .setCallingPackage(callingPackage)
4434 .setCallingPid(Binder.getCallingPid())
4435 .setCallingUid(Binder.getCallingUid())
4436 .setMethod("requestNetworkScan")
4437 .setMinSdkVersionForFine(Build.VERSION_CODES.Q)
4438 .build());
Hall Liub2ac8ef2019-02-28 15:56:23 -08004439 if (locationResult != LocationAccessPolicy.LocationPermissionResult.ALLOWED) {
4440 SecurityException e = checkNetworkRequestForSanitizedLocationAccess(request);
4441 if (e != null) {
4442 if (locationResult == LocationAccessPolicy.LocationPermissionResult.DENIED_HARD) {
4443 throw e;
4444 } else {
Hall Liu46dd96a2019-04-03 12:50:44 -07004445 loge(e.getMessage());
Hall Liub2ac8ef2019-02-28 15:56:23 -08004446 return TelephonyScanManager.INVALID_SCAN_ID;
4447 }
4448 }
Malcolm Chend965c8b2018-02-28 15:00:40 -08004449 }
Hall Liubac7d482019-04-25 14:02:26 -07004450 int callingUid = Binder.getCallingUid();
4451 int callingPid = Binder.getCallingPid();
yinxu612a8192019-04-18 10:38:27 -07004452 final long identity = Binder.clearCallingIdentity();
4453 try {
4454 return mNetworkScanRequestTracker.startNetworkScan(
4455 request, messenger, binder, getPhone(subId),
Hall Liubac7d482019-04-25 14:02:26 -07004456 callingUid, callingPid, callingPackage);
yinxu612a8192019-04-18 10:38:27 -07004457 } finally {
4458 Binder.restoreCallingIdentity(identity);
4459 }
yinxu504e1392017-04-12 16:03:22 -07004460 }
4461
Hall Liub2ac8ef2019-02-28 15:56:23 -08004462 private SecurityException checkNetworkRequestForSanitizedLocationAccess(
4463 NetworkScanRequest request) {
4464 if (mApp.checkCallingOrSelfPermission(android.Manifest.permission.NETWORK_SCAN)
4465 != PERMISSION_GRANTED) {
4466 return new SecurityException("permission.NETWORK_SCAN is needed for network scans"
4467 + " without location access.");
4468 }
4469
4470 if (request.getSpecifiers() != null && request.getSpecifiers().length > 0) {
4471 for (RadioAccessSpecifier ras : request.getSpecifiers()) {
Hall Liub2ac8ef2019-02-28 15:56:23 -08004472 if (ras.getChannels() != null && ras.getChannels().length > 0) {
4473 return new SecurityException("Specific channels must not be"
4474 + " scanned without location access.");
4475 }
4476 }
4477 }
4478
Hall Liub2ac8ef2019-02-28 15:56:23 -08004479 return null;
4480 }
4481
yinxu504e1392017-04-12 16:03:22 -07004482 /**
4483 * Stops an existing network scan with the given scanId.
yinxub1bed742017-04-17 11:45:04 -07004484 *
4485 * @param subId id of the subscription
4486 * @param scanId id of the scan that needs to be stopped
yinxu504e1392017-04-12 16:03:22 -07004487 */
4488 @Override
4489 public void stopNetworkScan(int subId, int scanId) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08004490 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
4491 mApp, subId, "stopNetworkScan");
Malcolm Chend965c8b2018-02-28 15:00:40 -08004492
Hall Liubac7d482019-04-25 14:02:26 -07004493 int callingUid = Binder.getCallingUid();
Malcolm Chend965c8b2018-02-28 15:00:40 -08004494 final long identity = Binder.clearCallingIdentity();
4495 try {
Hall Liubac7d482019-04-25 14:02:26 -07004496 mNetworkScanRequestTracker.stopNetworkScan(scanId, callingUid);
Malcolm Chend965c8b2018-02-28 15:00:40 -08004497 } finally {
4498 Binder.restoreCallingIdentity(identity);
4499 }
yinxu504e1392017-04-12 16:03:22 -07004500 }
4501
4502 /**
Junda Liu84d15a22014-07-02 11:21:04 -07004503 * Get the calculated preferred network type.
4504 * Used for debugging incorrect network type.
4505 *
4506 * @return the preferred network type, defined in RILConstants.java.
4507 */
4508 @Override
Svet Ganovb320e182015-04-16 12:30:10 -07004509 public int getCalculatedPreferredNetworkType(String callingPackage) {
Nazanin Bakhshi2f2cc692018-12-11 15:15:39 -08004510 final Phone defaultPhone = getDefaultPhone();
4511 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(mApp, defaultPhone.getSubId(),
4512 callingPackage, "getCalculatedPreferredNetworkType")) {
Svet Ganovb320e182015-04-16 12:30:10 -07004513 return RILConstants.PREFERRED_NETWORK_MODE;
4514 }
4515
Malcolm Chend965c8b2018-02-28 15:00:40 -08004516 final long identity = Binder.clearCallingIdentity();
4517 try {
4518 // FIXME: need to get SubId from somewhere.
Nazanin Bakhshi2f2cc692018-12-11 15:15:39 -08004519 return PhoneFactory.calculatePreferredNetworkType(defaultPhone.getContext(), 0);
Malcolm Chend965c8b2018-02-28 15:00:40 -08004520 } finally {
4521 Binder.restoreCallingIdentity(identity);
4522 }
Junda Liu84d15a22014-07-02 11:21:04 -07004523 }
4524
4525 /**
Jake Hamby7c27be32014-03-03 13:25:59 -08004526 * Get the preferred network type.
4527 * Used for device configuration by some CDMA operators.
4528 *
4529 * @return the preferred network type, defined in RILConstants.java.
4530 */
4531 @Override
Stuart Scott54788802015-03-30 13:18:01 -07004532 public int getPreferredNetworkType(int subId) {
Pengquan Meng4848cd02018-12-20 11:00:24 -08004533 TelephonyPermissions
4534 .enforeceCallingOrSelfReadPrivilegedPhoneStatePermissionOrCarrierPrivilege(
4535 mApp, subId, "getPreferredNetworkType");
Malcolm Chend965c8b2018-02-28 15:00:40 -08004536
4537 final long identity = Binder.clearCallingIdentity();
4538 try {
4539 if (DBG) log("getPreferredNetworkType");
4540 int[] result = (int[]) sendRequest(CMD_GET_PREFERRED_NETWORK_TYPE, null, subId);
4541 int networkType = (result != null ? result[0] : -1);
4542 if (DBG) log("getPreferredNetworkType: " + networkType);
4543 return networkType;
4544 } finally {
4545 Binder.restoreCallingIdentity(identity);
4546 }
Jake Hamby7c27be32014-03-03 13:25:59 -08004547 }
4548
4549 /**
4550 * Set the preferred network type.
4551 * Used for device configuration by some CDMA operators.
4552 *
4553 * @param networkType the preferred network type, defined in RILConstants.java.
4554 * @return true on success; false on any failure.
4555 */
4556 @Override
Stuart Scott54788802015-03-30 13:18:01 -07004557 public boolean setPreferredNetworkType(int subId, int networkType) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08004558 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
4559 mApp, subId, "setPreferredNetworkType");
Malcolm Chend965c8b2018-02-28 15:00:40 -08004560
4561 final long identity = Binder.clearCallingIdentity();
4562 try {
4563 if (DBG) log("setPreferredNetworkType: subId " + subId + " type " + networkType);
4564 Boolean success = (Boolean) sendRequest(
4565 CMD_SET_PREFERRED_NETWORK_TYPE, networkType, subId);
4566 if (DBG) log("setPreferredNetworkType: " + (success ? "ok" : "fail"));
4567 if (success) {
Nazanin Bakhshi2f2cc692018-12-11 15:15:39 -08004568 Settings.Global.putInt(mApp.getContentResolver(),
Malcolm Chend965c8b2018-02-28 15:00:40 -08004569 Settings.Global.PREFERRED_NETWORK_MODE + subId, networkType);
4570 }
4571 return success;
4572 } finally {
4573 Binder.restoreCallingIdentity(identity);
Junda Liu80bc0d12014-07-14 16:36:44 -07004574 }
Jake Hamby7c27be32014-03-03 13:25:59 -08004575 }
Robert Greenwalted86e582014-05-21 20:03:20 -07004576
4577 /**
Miaoa84611c2019-03-15 09:21:10 +08004578 * Check whether DUN APN is required for tethering with subId.
Junda Liu475951f2014-11-07 16:45:03 -08004579 *
Miaoa84611c2019-03-15 09:21:10 +08004580 * @param subId the id of the subscription to require tethering.
Amit Mahajanfe58cdf2017-07-11 12:01:53 -07004581 * @return {@code true} if DUN APN is required for tethering.
Junda Liu475951f2014-11-07 16:45:03 -08004582 * @hide
4583 */
4584 @Override
Miaoa84611c2019-03-15 09:21:10 +08004585 public boolean getTetherApnRequiredForSubscriber(int subId) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08004586 enforceModifyPermission();
Malcolm Chend965c8b2018-02-28 15:00:40 -08004587 final long identity = Binder.clearCallingIdentity();
Miaoa84611c2019-03-15 09:21:10 +08004588 final Phone phone = getPhone(subId);
Malcolm Chend965c8b2018-02-28 15:00:40 -08004589 try {
Miaoa84611c2019-03-15 09:21:10 +08004590 if (phone != null) {
4591 return phone.hasMatchedTetherApnSetting();
4592 } else {
4593 return false;
4594 }
Malcolm Chend965c8b2018-02-28 15:00:40 -08004595 } finally {
4596 Binder.restoreCallingIdentity(identity);
Junda Liu475951f2014-11-07 16:45:03 -08004597 }
Junda Liu475951f2014-11-07 16:45:03 -08004598 }
4599
4600 /**
Robert Greenwalted86e582014-05-21 20:03:20 -07004601 * Set mobile data enabled
4602 * Used by the user through settings etc to turn on/off mobile data
4603 *
4604 * @param enable {@code true} turn turn data on, else {@code false}
4605 */
4606 @Override
Malcolm Chen964682d2017-11-28 16:20:07 -08004607 public void setUserDataEnabled(int subId, boolean enable) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08004608 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
4609 mApp, subId, "setUserDataEnabled");
Malcolm Chend965c8b2018-02-28 15:00:40 -08004610
4611 final long identity = Binder.clearCallingIdentity();
4612 try {
4613 int phoneId = mSubscriptionController.getPhoneId(subId);
4614 if (DBG) log("setUserDataEnabled: subId=" + subId + " phoneId=" + phoneId);
4615 Phone phone = PhoneFactory.getPhone(phoneId);
4616 if (phone != null) {
4617 if (DBG) log("setUserDataEnabled: subId=" + subId + " enable=" + enable);
Jack Yu7a030e52018-12-13 11:51:28 -08004618 phone.getDataEnabledSettings().setUserDataEnabled(enable);
Malcolm Chend965c8b2018-02-28 15:00:40 -08004619 } else {
Nazanin Bakhshi2f2cc692018-12-11 15:15:39 -08004620 loge("setUserDataEnabled: no phone found. Invalid subId=" + subId);
Malcolm Chend965c8b2018-02-28 15:00:40 -08004621 }
4622 } finally {
4623 Binder.restoreCallingIdentity(identity);
Wink Savillee7353bb2014-12-05 14:21:41 -08004624 }
Robert Greenwalted86e582014-05-21 20:03:20 -07004625 }
4626
4627 /**
Malcolm Chen964682d2017-11-28 16:20:07 -08004628 * Get the user enabled state of Mobile Data.
4629 *
4630 * TODO: remove and use isUserDataEnabled.
4631 * This can't be removed now because some vendor codes
4632 * calls through ITelephony directly while they should
4633 * use TelephonyManager.
4634 *
4635 * @return true on enabled
4636 */
4637 @Override
4638 public boolean getDataEnabled(int subId) {
4639 return isUserDataEnabled(subId);
4640 }
4641
4642 /**
4643 * Get whether mobile data is enabled per user setting.
4644 *
4645 * There are other factors deciding whether mobile data is actually enabled, but they are
4646 * not considered here. See {@link #isDataEnabled(int)} for more details.
Robert Greenwalt646120a2014-05-23 11:54:03 -07004647 *
Jeff Davidsona1920712016-11-18 17:05:56 -08004648 * Accepts either ACCESS_NETWORK_STATE, MODIFY_PHONE_STATE or carrier privileges.
Robert Greenwalted86e582014-05-21 20:03:20 -07004649 *
4650 * @return {@code true} if data is enabled else {@code false}
4651 */
4652 @Override
Malcolm Chen964682d2017-11-28 16:20:07 -08004653 public boolean isUserDataEnabled(int subId) {
Robert Greenwalt646120a2014-05-23 11:54:03 -07004654 try {
4655 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.ACCESS_NETWORK_STATE,
4656 null);
4657 } catch (Exception e) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08004658 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
4659 mApp, subId, "isUserDataEnabled");
Robert Greenwalt646120a2014-05-23 11:54:03 -07004660 }
Malcolm Chend965c8b2018-02-28 15:00:40 -08004661
4662 final long identity = Binder.clearCallingIdentity();
4663 try {
4664 int phoneId = mSubscriptionController.getPhoneId(subId);
4665 if (DBG) log("isUserDataEnabled: subId=" + subId + " phoneId=" + phoneId);
4666 Phone phone = PhoneFactory.getPhone(phoneId);
4667 if (phone != null) {
4668 boolean retVal = phone.isUserDataEnabled();
4669 if (DBG) log("isUserDataEnabled: subId=" + subId + " retVal=" + retVal);
4670 return retVal;
4671 } else {
4672 if (DBG) loge("isUserDataEnabled: no phone subId=" + subId + " retVal=false");
4673 return false;
4674 }
4675 } finally {
4676 Binder.restoreCallingIdentity(identity);
Malcolm Chen964682d2017-11-28 16:20:07 -08004677 }
4678 }
4679
4680 /**
4681 * Get whether mobile data is enabled.
4682 *
4683 * Comparable to {@link #isUserDataEnabled(int)}, this considers all factors deciding
4684 * whether mobile data is actually enabled.
4685 *
4686 * Accepts either ACCESS_NETWORK_STATE, MODIFY_PHONE_STATE or carrier privileges.
4687 *
4688 * @return {@code true} if data is enabled else {@code false}
4689 */
4690 @Override
4691 public boolean isDataEnabled(int subId) {
4692 try {
4693 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.ACCESS_NETWORK_STATE,
4694 null);
4695 } catch (Exception e) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08004696 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
4697 mApp, subId, "isDataEnabled");
Malcolm Chen964682d2017-11-28 16:20:07 -08004698 }
Malcolm Chend965c8b2018-02-28 15:00:40 -08004699
4700 final long identity = Binder.clearCallingIdentity();
4701 try {
4702 int phoneId = mSubscriptionController.getPhoneId(subId);
4703 if (DBG) log("isDataEnabled: subId=" + subId + " phoneId=" + phoneId);
4704 Phone phone = PhoneFactory.getPhone(phoneId);
4705 if (phone != null) {
Jack Yu7a030e52018-12-13 11:51:28 -08004706 boolean retVal = phone.getDataEnabledSettings().isDataEnabled();
Malcolm Chend965c8b2018-02-28 15:00:40 -08004707 if (DBG) log("isDataEnabled: subId=" + subId + " retVal=" + retVal);
4708 return retVal;
4709 } else {
4710 if (DBG) loge("isDataEnabled: no phone subId=" + subId + " retVal=false");
4711 return false;
4712 }
4713 } finally {
4714 Binder.restoreCallingIdentity(identity);
Wink Savillee7353bb2014-12-05 14:21:41 -08004715 }
Robert Greenwalted86e582014-05-21 20:03:20 -07004716 }
Shishir Agrawal60f9c952014-06-23 12:00:43 -07004717
4718 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08004719 public int getCarrierPrivilegeStatus(int subId) {
4720 final Phone phone = getPhone(subId);
4721 if (phone == null) {
4722 loge("getCarrierPrivilegeStatus: Invalid subId");
4723 return TelephonyManager.CARRIER_PRIVILEGE_STATUS_NO_ACCESS;
4724 }
4725 UiccCard card = UiccController.getInstance().getUiccCard(phone.getPhoneId());
Shishir Agrawaleb8771e2014-07-22 11:24:08 -07004726 if (card == null) {
Shishir Agrawal5e5becd2014-11-18 11:38:23 -08004727 loge("getCarrierPrivilegeStatus: No UICC");
Shishir Agrawaleb8771e2014-07-22 11:24:08 -07004728 return TelephonyManager.CARRIER_PRIVILEGE_STATUS_RULES_NOT_LOADED;
4729 }
4730 return card.getCarrierPrivilegeStatusForCurrentTransaction(
Shishir Agrawalc04d9752016-02-19 10:41:00 -08004731 phone.getContext().getPackageManager());
Shishir Agrawal60f9c952014-06-23 12:00:43 -07004732 }
Junda Liu29340342014-07-10 15:23:27 -07004733
4734 @Override
Jeff Davidson7e17e312018-02-13 18:17:36 -08004735 public int getCarrierPrivilegeStatusForUid(int subId, int uid) {
4736 final Phone phone = getPhone(subId);
4737 if (phone == null) {
4738 loge("getCarrierPrivilegeStatus: Invalid subId");
4739 return TelephonyManager.CARRIER_PRIVILEGE_STATUS_NO_ACCESS;
4740 }
4741 UiccProfile profile =
4742 UiccController.getInstance().getUiccProfileForPhone(phone.getPhoneId());
4743 if (profile == null) {
4744 loge("getCarrierPrivilegeStatus: No UICC");
4745 return TelephonyManager.CARRIER_PRIVILEGE_STATUS_RULES_NOT_LOADED;
4746 }
4747 return profile.getCarrierPrivilegeStatusForUid(phone.getContext().getPackageManager(), uid);
4748 }
4749
4750 @Override
Zach Johnson50ecba32015-05-19 00:24:21 -07004751 public int checkCarrierPrivilegesForPackage(String pkgName) {
Nazanin Bakhshi2f2cc692018-12-11 15:15:39 -08004752 final Phone defaultPhone = getDefaultPhone();
Junda Liu317d70b2016-03-08 09:33:53 -08004753 if (TextUtils.isEmpty(pkgName))
4754 return TelephonyManager.CARRIER_PRIVILEGE_STATUS_NO_ACCESS;
Nazanin Bakhshi2f2cc692018-12-11 15:15:39 -08004755 UiccCard card = UiccController.getInstance().getUiccCard(defaultPhone.getPhoneId());
Shishir Agrawaleb8771e2014-07-22 11:24:08 -07004756 if (card == null) {
4757 loge("checkCarrierPrivilegesForPackage: No UICC");
4758 return TelephonyManager.CARRIER_PRIVILEGE_STATUS_RULES_NOT_LOADED;
4759 }
Nazanin Bakhshi2f2cc692018-12-11 15:15:39 -08004760 return card.getCarrierPrivilegeStatus(defaultPhone.getContext().getPackageManager(),
4761 pkgName);
Zach Johnson50ecba32015-05-19 00:24:21 -07004762 }
4763
4764 @Override
4765 public int checkCarrierPrivilegesForPackageAnyPhone(String pkgName) {
Junda Liu317d70b2016-03-08 09:33:53 -08004766 if (TextUtils.isEmpty(pkgName))
4767 return TelephonyManager.CARRIER_PRIVILEGE_STATUS_NO_ACCESS;
Zach Johnson50ecba32015-05-19 00:24:21 -07004768 int result = TelephonyManager.CARRIER_PRIVILEGE_STATUS_RULES_NOT_LOADED;
4769 for (int i = 0; i < TelephonyManager.getDefault().getPhoneCount(); i++) {
4770 UiccCard card = UiccController.getInstance().getUiccCard(i);
4771 if (card == null) {
Jonathan Basseri7d320df2015-06-16 12:17:08 -07004772 // No UICC in that slot.
Zach Johnson50ecba32015-05-19 00:24:21 -07004773 continue;
4774 }
4775
Nazanin Bakhshi2f2cc692018-12-11 15:15:39 -08004776 result = card.getCarrierPrivilegeStatus(mApp.getPackageManager(), pkgName);
Zach Johnson50ecba32015-05-19 00:24:21 -07004777 if (result == TelephonyManager.CARRIER_PRIVILEGE_STATUS_HAS_ACCESS) {
4778 break;
4779 }
4780 }
4781
4782 return result;
Junda Liu29340342014-07-10 15:23:27 -07004783 }
Derek Tan89e89d42014-07-08 17:00:10 -07004784
4785 @Override
Junda Liue64de782015-04-16 17:19:16 -07004786 public List<String> getCarrierPackageNamesForIntentAndPhone(Intent intent, int phoneId) {
4787 if (!SubscriptionManager.isValidPhoneId(phoneId)) {
4788 loge("phoneId " + phoneId + " is not valid.");
4789 return null;
4790 }
4791 UiccCard card = UiccController.getInstance().getUiccCard(phoneId);
Shishir Agrawaleb6439a2014-07-21 13:19:38 -07004792 if (card == null) {
Diego Pontorieroaf74c862014-08-28 11:51:16 -07004793 loge("getCarrierPackageNamesForIntent: No UICC");
Shishir Agrawaleb6439a2014-07-21 13:19:38 -07004794 return null ;
4795 }
Nazanin Bakhshi2f2cc692018-12-11 15:15:39 -08004796 return card.getCarrierPackageNamesForIntent(mApp.getPackageManager(), intent);
Shishir Agrawaleb6439a2014-07-21 13:19:38 -07004797 }
4798
Amith Yamasani6e118872016-02-19 12:53:51 -08004799 @Override
4800 public List<String> getPackagesWithCarrierPrivileges() {
Nazanin Bakhshi2f2cc692018-12-11 15:15:39 -08004801 PackageManager pm = mApp.getPackageManager();
Amith Yamasani6e118872016-02-19 12:53:51 -08004802 List<String> privilegedPackages = new ArrayList<>();
4803 List<PackageInfo> packages = null;
4804 for (int i = 0; i < TelephonyManager.getDefault().getPhoneCount(); i++) {
4805 UiccCard card = UiccController.getInstance().getUiccCard(i);
4806 if (card == null) {
4807 // No UICC in that slot.
4808 continue;
4809 }
4810 if (card.hasCarrierPrivilegeRules()) {
4811 if (packages == null) {
4812 // Only check packages in user 0 for now
4813 packages = pm.getInstalledPackagesAsUser(
4814 PackageManager.MATCH_DISABLED_COMPONENTS
4815 | PackageManager.MATCH_DISABLED_UNTIL_USED_COMPONENTS
4816 | PackageManager.GET_SIGNATURES, UserHandle.USER_SYSTEM);
4817 }
4818 for (int p = packages.size() - 1; p >= 0; p--) {
4819 PackageInfo pkgInfo = packages.get(p);
4820 if (pkgInfo != null && pkgInfo.packageName != null
4821 && card.getCarrierPrivilegeStatus(pkgInfo)
4822 == TelephonyManager.CARRIER_PRIVILEGE_STATUS_HAS_ACCESS) {
4823 privilegedPackages.add(pkgInfo.packageName);
4824 }
4825 }
4826 }
4827 }
4828 return privilegedPackages;
4829 }
4830
Wink Savilleb564aae2014-10-23 10:18:09 -07004831 private String getIccId(int subId) {
Sanket Padawe356d7632015-06-22 14:03:32 -07004832 final Phone phone = getPhone(subId);
4833 UiccCard card = phone == null ? null : phone.getUiccCard();
Derek Tan97ebb422014-09-05 16:55:38 -07004834 if (card == null) {
4835 loge("getIccId: No UICC");
4836 return null;
4837 }
4838 String iccId = card.getIccId();
4839 if (TextUtils.isEmpty(iccId)) {
4840 loge("getIccId: ICC ID is null or empty.");
4841 return null;
4842 }
4843 return iccId;
4844 }
4845
Shishir Agrawaleb6439a2014-07-21 13:19:38 -07004846 @Override
Jeff Sharkey85190e62014-12-05 09:40:12 -08004847 public boolean setLine1NumberForDisplayForSubscriber(int subId, String alphaTag,
4848 String number) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08004849 TelephonyPermissions.enforceCallingOrSelfCarrierPrivilege(
4850 subId, "setLine1NumberForDisplayForSubscriber");
Derek Tan97ebb422014-09-05 16:55:38 -07004851
Malcolm Chend965c8b2018-02-28 15:00:40 -08004852 final long identity = Binder.clearCallingIdentity();
4853 try {
4854 final String iccId = getIccId(subId);
4855 final Phone phone = getPhone(subId);
4856 if (phone == null) {
4857 return false;
4858 }
4859 final String subscriberId = phone.getSubscriberId();
4860
4861 if (DBG_MERGE) {
4862 Slog.d(LOG_TAG, "Setting line number for ICC=" + iccId + ", subscriberId="
4863 + subscriberId + " to " + number);
4864 }
4865
4866 if (TextUtils.isEmpty(iccId)) {
4867 return false;
4868 }
4869
4870 final SharedPreferences.Editor editor = mTelephonySharedPreferences.edit();
4871
4872 final String alphaTagPrefKey = PREF_CARRIERS_ALPHATAG_PREFIX + iccId;
4873 if (alphaTag == null) {
4874 editor.remove(alphaTagPrefKey);
4875 } else {
4876 editor.putString(alphaTagPrefKey, alphaTag);
4877 }
4878
4879 // Record both the line number and IMSI for this ICCID, since we need to
4880 // track all merged IMSIs based on line number
4881 final String numberPrefKey = PREF_CARRIERS_NUMBER_PREFIX + iccId;
4882 final String subscriberPrefKey = PREF_CARRIERS_SUBSCRIBER_PREFIX + iccId;
4883 if (number == null) {
4884 editor.remove(numberPrefKey);
4885 editor.remove(subscriberPrefKey);
4886 } else {
4887 editor.putString(numberPrefKey, number);
4888 editor.putString(subscriberPrefKey, subscriberId);
4889 }
4890
4891 editor.commit();
4892 return true;
4893 } finally {
4894 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07004895 }
Derek Tan7226c842014-07-02 17:42:23 -07004896 }
4897
4898 @Override
Svet Ganovb320e182015-04-16 12:30:10 -07004899 public String getLine1NumberForDisplay(int subId, String callingPackage) {
Makoto Onukifee69342015-06-29 14:44:50 -07004900 // This is open to apps with WRITE_SMS.
Jeff Davidson7e17e312018-02-13 18:17:36 -08004901 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneNumber(
Jeff Davidson913390f2018-02-23 17:11:49 -08004902 mApp, subId, callingPackage, "getLine1NumberForDisplay")) {
Amit Mahajan9cf11512015-11-09 11:40:48 -08004903 if (DBG_MERGE) log("getLine1NumberForDisplay returning null due to permission");
Svet Ganovb320e182015-04-16 12:30:10 -07004904 return null;
4905 }
Derek Tan97ebb422014-09-05 16:55:38 -07004906
Malcolm Chend965c8b2018-02-28 15:00:40 -08004907 final long identity = Binder.clearCallingIdentity();
4908 try {
4909 String iccId = getIccId(subId);
4910 if (iccId != null) {
4911 String numberPrefKey = PREF_CARRIERS_NUMBER_PREFIX + iccId;
4912 if (DBG_MERGE) {
4913 log("getLine1NumberForDisplay returning "
4914 + mTelephonySharedPreferences.getString(numberPrefKey, null));
4915 }
4916 return mTelephonySharedPreferences.getString(numberPrefKey, null);
Amit Mahajan9cf11512015-11-09 11:40:48 -08004917 }
Malcolm Chend965c8b2018-02-28 15:00:40 -08004918 if (DBG_MERGE) log("getLine1NumberForDisplay returning null as iccId is null");
4919 return null;
4920 } finally {
4921 Binder.restoreCallingIdentity(identity);
Derek Tan7226c842014-07-02 17:42:23 -07004922 }
Derek Tan7226c842014-07-02 17:42:23 -07004923 }
4924
4925 @Override
Svet Ganovb320e182015-04-16 12:30:10 -07004926 public String getLine1AlphaTagForDisplay(int subId, String callingPackage) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08004927 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
Jeff Davidson913390f2018-02-23 17:11:49 -08004928 mApp, subId, callingPackage, "getLine1AlphaTagForDisplay")) {
Svet Ganovb320e182015-04-16 12:30:10 -07004929 return null;
4930 }
Derek Tan97ebb422014-09-05 16:55:38 -07004931
Malcolm Chend965c8b2018-02-28 15:00:40 -08004932 final long identity = Binder.clearCallingIdentity();
4933 try {
4934 String iccId = getIccId(subId);
4935 if (iccId != null) {
4936 String alphaTagPrefKey = PREF_CARRIERS_ALPHATAG_PREFIX + iccId;
4937 return mTelephonySharedPreferences.getString(alphaTagPrefKey, null);
4938 }
4939 return null;
4940 } finally {
4941 Binder.restoreCallingIdentity(identity);
Derek Tan7226c842014-07-02 17:42:23 -07004942 }
Derek Tan7226c842014-07-02 17:42:23 -07004943 }
Shishir Agrawalb1ebf8c2014-07-17 16:32:41 -07004944
4945 @Override
Fyodor Kupolov8e53b0b2015-06-17 13:17:50 -07004946 public String[] getMergedSubscriberIds(String callingPackage) {
Jeff Davidson913390f2018-02-23 17:11:49 -08004947 // This API isn't public, so no need to provide a valid subscription ID - we're not worried
4948 // about carrier-privileged callers not having access.
Jeff Davidson7e17e312018-02-13 18:17:36 -08004949 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
Jeff Davidson913390f2018-02-23 17:11:49 -08004950 mApp, SubscriptionManager.INVALID_SUBSCRIPTION_ID, callingPackage,
4951 "getMergedSubscriberIds")) {
Fyodor Kupolov8e53b0b2015-06-17 13:17:50 -07004952 return null;
4953 }
Jeff Sharkey85190e62014-12-05 09:40:12 -08004954
Fyodor Kupolov8e53b0b2015-06-17 13:17:50 -07004955 final long identity = Binder.clearCallingIdentity();
4956 try {
Nazanin Bakhshi2f2cc692018-12-11 15:15:39 -08004957 final Context context = mApp;
Malcolm Chend965c8b2018-02-28 15:00:40 -08004958 final TelephonyManager tele = TelephonyManager.from(context);
4959 final SubscriptionManager sub = SubscriptionManager.from(context);
4960
4961 // Figure out what subscribers are currently active
4962 final ArraySet<String> activeSubscriberIds = new ArraySet<>();
4963 // Clear calling identity, when calling TelephonyManager, because callerUid must be
4964 // the process, where TelephonyManager was instantiated.
4965 // Otherwise AppOps check will fail.
4966
Fyodor Kupolov8e53b0b2015-06-17 13:17:50 -07004967 final int[] subIds = sub.getActiveSubscriptionIdList();
4968 for (int subId : subIds) {
4969 activeSubscriberIds.add(tele.getSubscriberId(subId));
4970 }
Malcolm Chend965c8b2018-02-28 15:00:40 -08004971
4972 // First pass, find a number override for an active subscriber
4973 String mergeNumber = null;
4974 final Map<String, ?> prefs = mTelephonySharedPreferences.getAll();
4975 for (String key : prefs.keySet()) {
4976 if (key.startsWith(PREF_CARRIERS_SUBSCRIBER_PREFIX)) {
4977 final String subscriberId = (String) prefs.get(key);
4978 if (activeSubscriberIds.contains(subscriberId)) {
4979 final String iccId = key.substring(
4980 PREF_CARRIERS_SUBSCRIBER_PREFIX.length());
4981 final String numberKey = PREF_CARRIERS_NUMBER_PREFIX + iccId;
4982 mergeNumber = (String) prefs.get(numberKey);
4983 if (DBG_MERGE) {
4984 Slog.d(LOG_TAG, "Found line number " + mergeNumber
4985 + " for active subscriber " + subscriberId);
4986 }
4987 if (!TextUtils.isEmpty(mergeNumber)) {
4988 break;
4989 }
4990 }
4991 }
4992 }
4993
4994 // Shortcut when no active merged subscribers
4995 if (TextUtils.isEmpty(mergeNumber)) {
4996 return null;
4997 }
4998
4999 // Second pass, find all subscribers under that line override
5000 final ArraySet<String> result = new ArraySet<>();
5001 for (String key : prefs.keySet()) {
5002 if (key.startsWith(PREF_CARRIERS_NUMBER_PREFIX)) {
5003 final String number = (String) prefs.get(key);
5004 if (mergeNumber.equals(number)) {
5005 final String iccId = key.substring(PREF_CARRIERS_NUMBER_PREFIX.length());
5006 final String subscriberKey = PREF_CARRIERS_SUBSCRIBER_PREFIX + iccId;
5007 final String subscriberId = (String) prefs.get(subscriberKey);
5008 if (!TextUtils.isEmpty(subscriberId)) {
5009 result.add(subscriberId);
5010 }
5011 }
5012 }
5013 }
5014
5015 final String[] resultArray = result.toArray(new String[result.size()]);
5016 Arrays.sort(resultArray);
5017 if (DBG_MERGE) {
5018 Slog.d(LOG_TAG,
5019 "Found subscribers " + Arrays.toString(resultArray) + " after merge");
5020 }
5021 return resultArray;
Fyodor Kupolov8e53b0b2015-06-17 13:17:50 -07005022 } finally {
5023 Binder.restoreCallingIdentity(identity);
Jeff Sharkey85190e62014-12-05 09:40:12 -08005024 }
Jeff Sharkey85190e62014-12-05 09:40:12 -08005025 }
5026
5027 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08005028 public boolean setOperatorBrandOverride(int subId, String brand) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08005029 TelephonyPermissions.enforceCallingOrSelfCarrierPrivilege(
5030 subId, "setOperatorBrandOverride");
Malcolm Chend965c8b2018-02-28 15:00:40 -08005031
5032 final long identity = Binder.clearCallingIdentity();
5033 try {
5034 final Phone phone = getPhone(subId);
5035 return phone == null ? false : phone.setOperatorBrandOverride(brand);
5036 } finally {
5037 Binder.restoreCallingIdentity(identity);
5038 }
Shishir Agrawalb1ebf8c2014-07-17 16:32:41 -07005039 }
Steven Liu4bf01bc2014-07-17 11:05:29 -05005040
5041 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08005042 public boolean setRoamingOverride(int subId, List<String> gsmRoamingList,
Shishir Agrawal621a47c2014-12-01 10:25:09 -08005043 List<String> gsmNonRoamingList, List<String> cdmaRoamingList,
5044 List<String> cdmaNonRoamingList) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08005045 TelephonyPermissions.enforceCallingOrSelfCarrierPrivilege(subId, "setRoamingOverride");
Malcolm Chend965c8b2018-02-28 15:00:40 -08005046
5047 final long identity = Binder.clearCallingIdentity();
5048 try {
5049 final Phone phone = getPhone(subId);
5050 if (phone == null) {
5051 return false;
5052 }
5053 return phone.setRoamingOverride(gsmRoamingList, gsmNonRoamingList, cdmaRoamingList,
5054 cdmaNonRoamingList);
5055 } finally {
5056 Binder.restoreCallingIdentity(identity);
Shishir Agrawalc04d9752016-02-19 10:41:00 -08005057 }
Shishir Agrawal621a47c2014-12-01 10:25:09 -08005058 }
5059
5060 @Override
Shuo Qian850e4d6a2018-04-25 21:02:08 +00005061 @Deprecated
5062 public int invokeOemRilRequestRaw(byte[] oemReq, byte[] oemResp) {
5063 enforceModifyPermission();
5064
5065 int returnValue = 0;
5066 try {
vagdevie435a3e2018-08-15 16:01:53 -07005067 AsyncResult result = (AsyncResult) sendRequest(CMD_INVOKE_OEM_RIL_REQUEST_RAW, oemReq);
Shuo Qian850e4d6a2018-04-25 21:02:08 +00005068 if(result.exception == null) {
5069 if (result.result != null) {
5070 byte[] responseData = (byte[])(result.result);
5071 if(responseData.length > oemResp.length) {
5072 Log.w(LOG_TAG, "Buffer to copy response too small: Response length is " +
5073 responseData.length + "bytes. Buffer Size is " +
5074 oemResp.length + "bytes.");
5075 }
5076 System.arraycopy(responseData, 0, oemResp, 0, responseData.length);
5077 returnValue = responseData.length;
5078 }
5079 } else {
5080 CommandException ex = (CommandException) result.exception;
5081 returnValue = ex.getCommandError().ordinal();
5082 if(returnValue > 0) returnValue *= -1;
5083 }
5084 } catch (RuntimeException e) {
5085 Log.w(LOG_TAG, "sendOemRilRequestRaw: Runtime Exception");
5086 returnValue = (CommandException.Error.GENERIC_FAILURE.ordinal());
5087 if(returnValue > 0) returnValue *= -1;
5088 }
5089
5090 return returnValue;
5091 }
5092
5093 @Override
Wink Saville5d475dd2014-10-17 15:00:58 -07005094 public void setRadioCapability(RadioAccessFamily[] rafs) {
5095 try {
5096 ProxyController.getInstance().setRadioCapability(rafs);
5097 } catch (RuntimeException e) {
5098 Log.w(LOG_TAG, "setRadioCapability: Runtime Exception");
5099 }
5100 }
5101
5102 @Override
Robert Greenwalt36b23af2015-07-06 17:59:14 -07005103 public int getRadioAccessFamily(int phoneId, String callingPackage) {
Jeff Davidson913390f2018-02-23 17:11:49 -08005104 Phone phone = PhoneFactory.getPhone(phoneId);
chen xufeeed752018-10-26 14:17:57 -07005105 int raf = RadioAccessFamily.RAF_UNKNOWN;
Jeff Davidson913390f2018-02-23 17:11:49 -08005106 if (phone == null) {
chen xufeeed752018-10-26 14:17:57 -07005107 return raf;
Jeff Davidson913390f2018-02-23 17:11:49 -08005108 }
Malcolm Chend965c8b2018-02-28 15:00:40 -08005109 final long identity = Binder.clearCallingIdentity();
5110 try {
chen xufeeed752018-10-26 14:17:57 -07005111 TelephonyPermissions
5112 .enforeceCallingOrSelfReadPrivilegedPhoneStatePermissionOrCarrierPrivilege(
5113 mApp, phone.getSubId(), "getRadioAccessFamily");
5114 raf = ProxyController.getInstance().getRadioAccessFamily(phoneId);
Malcolm Chend965c8b2018-02-28 15:00:40 -08005115 } finally {
5116 Binder.restoreCallingIdentity(identity);
5117 }
chen xufeeed752018-10-26 14:17:57 -07005118 return raf;
Wink Saville5d475dd2014-10-17 15:00:58 -07005119 }
Andrew Leedf14ead2014-10-17 14:22:52 -07005120
5121 @Override
5122 public void enableVideoCalling(boolean enable) {
Nazanin Bakhshi2f2cc692018-12-11 15:15:39 -08005123 final Phone defaultPhone = getDefaultPhone();
Andrew Leedf14ead2014-10-17 14:22:52 -07005124 enforceModifyPermission();
Malcolm Chend965c8b2018-02-28 15:00:40 -08005125
5126 final long identity = Binder.clearCallingIdentity();
5127 try {
Nazanin Bakhshi2f2cc692018-12-11 15:15:39 -08005128 ImsManager.getInstance(defaultPhone.getContext(),
5129 defaultPhone.getPhoneId()).setVtSetting(enable);
Malcolm Chend965c8b2018-02-28 15:00:40 -08005130 } finally {
5131 Binder.restoreCallingIdentity(identity);
5132 }
Andrew Leedf14ead2014-10-17 14:22:52 -07005133 }
5134
5135 @Override
Svet Ganovb320e182015-04-16 12:30:10 -07005136 public boolean isVideoCallingEnabled(String callingPackage) {
Nazanin Bakhshi2f2cc692018-12-11 15:15:39 -08005137 final Phone defaultPhone = getDefaultPhone();
Amit Mahajan578e53d2018-03-20 16:18:38 +00005138 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
Nazanin Bakhshi2f2cc692018-12-11 15:15:39 -08005139 mApp, defaultPhone.getSubId(), callingPackage, "isVideoCallingEnabled")) {
Amit Mahajan578e53d2018-03-20 16:18:38 +00005140 return false;
5141 }
Svet Ganovb320e182015-04-16 12:30:10 -07005142
Malcolm Chend965c8b2018-02-28 15:00:40 -08005143 final long identity = Binder.clearCallingIdentity();
5144 try {
5145 // Check the user preference and the system-level IMS setting. Even if the user has
5146 // enabled video calling, if IMS is disabled we aren't able to support video calling.
5147 // In the long run, we may instead need to check if there exists a connection service
5148 // which can support video calling.
5149 ImsManager imsManager =
Nazanin Bakhshi2f2cc692018-12-11 15:15:39 -08005150 ImsManager.getInstance(defaultPhone.getContext(), defaultPhone.getPhoneId());
Malcolm Chend965c8b2018-02-28 15:00:40 -08005151 return imsManager.isVtEnabledByPlatform()
5152 && imsManager.isEnhanced4gLteModeSettingEnabledByUser()
5153 && imsManager.isVtEnabledByUser();
5154 } finally {
5155 Binder.restoreCallingIdentity(identity);
5156 }
Andrew Leedf14ead2014-10-17 14:22:52 -07005157 }
Libin.Tang@motorola.comafe82642014-12-18 13:27:53 -06005158
Andrew Leea1239f22015-03-02 17:44:07 -08005159 @Override
Malcolm Chend965c8b2018-02-28 15:00:40 -08005160 public boolean canChangeDtmfToneLength(int subId, String callingPackage) {
5161 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
5162 mApp, subId, callingPackage, "isVideoCallingEnabled")) {
5163 return false;
5164 }
5165
5166 final long identity = Binder.clearCallingIdentity();
5167 try {
5168 CarrierConfigManager configManager =
5169 (CarrierConfigManager) mApp.getSystemService(Context.CARRIER_CONFIG_SERVICE);
Nazanin Bakhshi2f2cc692018-12-11 15:15:39 -08005170 return configManager.getConfigForSubId(subId)
Malcolm Chend965c8b2018-02-28 15:00:40 -08005171 .getBoolean(CarrierConfigManager.KEY_DTMF_TYPE_ENABLED_BOOL);
5172 } finally {
5173 Binder.restoreCallingIdentity(identity);
5174 }
Andrew Leea1239f22015-03-02 17:44:07 -08005175 }
5176
5177 @Override
Malcolm Chend965c8b2018-02-28 15:00:40 -08005178 public boolean isWorldPhone(int subId, String callingPackage) {
5179 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
5180 mApp, subId, callingPackage, "isVideoCallingEnabled")) {
5181 return false;
5182 }
5183
5184 final long identity = Binder.clearCallingIdentity();
5185 try {
5186 CarrierConfigManager configManager =
5187 (CarrierConfigManager) mApp.getSystemService(Context.CARRIER_CONFIG_SERVICE);
Nazanin Bakhshi2f2cc692018-12-11 15:15:39 -08005188 return configManager.getConfigForSubId(subId)
Malcolm Chend965c8b2018-02-28 15:00:40 -08005189 .getBoolean(CarrierConfigManager.KEY_WORLD_PHONE_BOOL);
5190 } finally {
5191 Binder.restoreCallingIdentity(identity);
5192 }
Andrew Leea1239f22015-03-02 17:44:07 -08005193 }
5194
Andrew Lee9431b832015-03-09 18:46:45 -07005195 @Override
5196 public boolean isTtyModeSupported() {
Nazanin Bakhshi2f2cc692018-12-11 15:15:39 -08005197 TelecomManager telecomManager = TelecomManager.from(mApp);
Wooki Wu1f82f7a2016-02-15 15:59:58 +08005198 return telecomManager.isTtySupported();
Andrew Lee9431b832015-03-09 18:46:45 -07005199 }
5200
5201 @Override
5202 public boolean isHearingAidCompatibilitySupported() {
Malcolm Chend965c8b2018-02-28 15:00:40 -08005203 final long identity = Binder.clearCallingIdentity();
5204 try {
Nazanin Bakhshi2f2cc692018-12-11 15:15:39 -08005205 return mApp.getResources().getBoolean(R.bool.hac_enabled);
Malcolm Chend965c8b2018-02-28 15:00:40 -08005206 } finally {
5207 Binder.restoreCallingIdentity(identity);
5208 }
Andrew Lee9431b832015-03-09 18:46:45 -07005209 }
5210
Hall Liuf6668912018-10-31 17:05:23 -07005211 /**
5212 * Determines whether the device currently supports RTT (Real-time text). Based both on carrier
5213 * support for the feature and device firmware support.
5214 *
5215 * @return {@code true} if the device and carrier both support RTT, {@code false} otherwise.
5216 */
5217 @Override
5218 public boolean isRttSupported(int subscriptionId) {
Malcolm Chend965c8b2018-02-28 15:00:40 -08005219 final long identity = Binder.clearCallingIdentity();
Nazanin Bakhshi2f2cc692018-12-11 15:15:39 -08005220 final Phone phone = getPhone(subscriptionId);
5221 if (phone == null) {
5222 loge("isRttSupported: no Phone found. Invalid subId:" + subscriptionId);
5223 return false;
5224 }
Malcolm Chend965c8b2018-02-28 15:00:40 -08005225 try {
Nazanin Bakhshi2f2cc692018-12-11 15:15:39 -08005226 boolean isCarrierSupported = mApp.getCarrierConfigForSubId(subscriptionId).getBoolean(
Malcolm Chend965c8b2018-02-28 15:00:40 -08005227 CarrierConfigManager.KEY_RTT_SUPPORTED_BOOL);
5228 boolean isDeviceSupported =
Nazanin Bakhshi2f2cc692018-12-11 15:15:39 -08005229 phone.getContext().getResources().getBoolean(R.bool.config_support_rtt);
Malcolm Chend965c8b2018-02-28 15:00:40 -08005230 return isCarrierSupported && isDeviceSupported;
5231 } finally {
5232 Binder.restoreCallingIdentity(identity);
5233 }
Hall Liu98187582018-01-22 19:15:32 -08005234 }
5235
Hall Liuf6668912018-10-31 17:05:23 -07005236 /**
5237 * Determines whether the user has turned on RTT. Only returns true if the device and carrier
5238 * both also support RTT.
5239 */
5240 public boolean isRttEnabled(int subscriptionId) {
Malcolm Chend965c8b2018-02-28 15:00:40 -08005241 final long identity = Binder.clearCallingIdentity();
5242 try {
Hall Liuf6668912018-10-31 17:05:23 -07005243 return isRttSupported(subscriptionId) && Settings.Secure.getInt(
Nazanin Bakhshi2f2cc692018-12-11 15:15:39 -08005244 mApp.getContentResolver(), Settings.Secure.RTT_CALLING_MODE, 0) != 0;
Malcolm Chend965c8b2018-02-28 15:00:40 -08005245 } finally {
5246 Binder.restoreCallingIdentity(identity);
5247 }
Hall Liu3ad5f012018-04-06 16:23:39 -07005248 }
5249
Sanket Padawe7310cc72015-01-14 09:53:20 -08005250 /**
5251 * Returns the unique device ID of phone, for example, the IMEI for
5252 * GSM and the MEID for CDMA phones. Return null if device ID is not available.
5253 *
5254 * <p>Requires Permission:
5255 * {@link android.Manifest.permission#READ_PHONE_STATE READ_PHONE_STATE}
5256 */
5257 @Override
Svet Ganovb320e182015-04-16 12:30:10 -07005258 public String getDeviceId(String callingPackage) {
Sanket Padawe7310cc72015-01-14 09:53:20 -08005259 final Phone phone = PhoneFactory.getPhone(0);
Jeff Davidson913390f2018-02-23 17:11:49 -08005260 if (phone == null) {
Sanket Padawe7310cc72015-01-14 09:53:20 -08005261 return null;
5262 }
Jeff Davidson913390f2018-02-23 17:11:49 -08005263 int subId = phone.getSubId();
5264 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
5265 mApp, subId, callingPackage, "getDeviceId")) {
5266 return null;
5267 }
Malcolm Chend965c8b2018-02-28 15:00:40 -08005268
5269 final long identity = Binder.clearCallingIdentity();
5270 try {
5271 return phone.getDeviceId();
5272 } finally {
5273 Binder.restoreCallingIdentity(identity);
5274 }
Sanket Padawe7310cc72015-01-14 09:53:20 -08005275 }
5276
Ping Sunc67b7c22016-03-02 19:16:45 +08005277 /**
5278 * {@hide}
5279 * Returns the IMS Registration Status on a particular subid
5280 *
5281 * @param subId
5282 */
Brad Ebinger1f2b5082018-02-08 16:11:32 -08005283 public boolean isImsRegistered(int subId) {
Ping Sunc67b7c22016-03-02 19:16:45 +08005284 Phone phone = getPhone(subId);
5285 if (phone != null) {
5286 return phone.isImsRegistered();
5287 } else {
5288 return false;
5289 }
5290 }
5291
Santos Cordon7a1885b2015-02-03 11:15:19 -08005292 @Override
5293 public int getSubIdForPhoneAccount(PhoneAccount phoneAccount) {
Malcolm Chend965c8b2018-02-28 15:00:40 -08005294 final long identity = Binder.clearCallingIdentity();
5295 try {
5296 return PhoneUtils.getSubIdForPhoneAccount(phoneAccount);
5297 } finally {
5298 Binder.restoreCallingIdentity(identity);
5299 }
Santos Cordon7a1885b2015-02-03 11:15:19 -08005300 }
Nathan Harolddcfc7932015-03-18 10:01:20 -07005301
Tyler Gunn327a9722019-04-03 15:28:53 -07005302 @Override
5303 public @Nullable PhoneAccountHandle getPhoneAccountHandleForSubscriptionId(int subscriptionId) {
5304 final long identity = Binder.clearCallingIdentity();
5305 try {
5306 Phone phone = getPhone(subscriptionId);
5307 if (phone == null) {
5308 return null;
5309 }
5310 return PhoneUtils.makePstnPhoneAccountHandle(phone);
5311 } finally {
5312 Binder.restoreCallingIdentity(identity);
5313 }
5314 }
5315
Brad Ebinger1f2b5082018-02-08 16:11:32 -08005316 /**
5317 * @return the VoWiFi calling availability.
Nathan Haroldc55097a2015-03-11 18:14:50 -07005318 */
Brad Ebinger1f2b5082018-02-08 16:11:32 -08005319 public boolean isWifiCallingAvailable(int subId) {
Malcolm Chend965c8b2018-02-28 15:00:40 -08005320 final long identity = Binder.clearCallingIdentity();
5321 try {
5322 Phone phone = getPhone(subId);
5323 if (phone != null) {
5324 return phone.isWifiCallingEnabled();
5325 } else {
5326 return false;
5327 }
5328 } finally {
5329 Binder.restoreCallingIdentity(identity);
Brad Ebinger1f2b5082018-02-08 16:11:32 -08005330 }
Nathan Haroldc55097a2015-03-11 18:14:50 -07005331 }
5332
Brad Ebinger1f2b5082018-02-08 16:11:32 -08005333 /**
Brad Ebinger1f2b5082018-02-08 16:11:32 -08005334 * @return the VT calling availability.
Etan Cohen3b7a1bc2015-05-28 15:57:13 -07005335 */
Brad Ebinger1f2b5082018-02-08 16:11:32 -08005336 public boolean isVideoTelephonyAvailable(int subId) {
Malcolm Chend965c8b2018-02-28 15:00:40 -08005337 final long identity = Binder.clearCallingIdentity();
5338 try {
5339 Phone phone = getPhone(subId);
5340 if (phone != null) {
5341 return phone.isVideoEnabled();
5342 } else {
5343 return false;
5344 }
5345 } finally {
5346 Binder.restoreCallingIdentity(identity);
Brad Ebinger1f2b5082018-02-08 16:11:32 -08005347 }
5348 }
5349
5350 /**
5351 * @return the IMS registration technology for the MMTEL feature. Valid return values are
5352 * defined in {@link ImsRegistrationImplBase}.
5353 */
5354 public @ImsRegistrationImplBase.ImsRegistrationTech int getImsRegTechnologyForMmTel(int subId) {
Malcolm Chend965c8b2018-02-28 15:00:40 -08005355 final long identity = Binder.clearCallingIdentity();
5356 try {
5357 Phone phone = getPhone(subId);
5358 if (phone != null) {
5359 return phone.getImsRegistrationTech();
5360 } else {
5361 return ImsRegistrationImplBase.REGISTRATION_TECH_NONE;
5362 }
5363 } finally {
5364 Binder.restoreCallingIdentity(identity);
Brad Ebinger1f2b5082018-02-08 16:11:32 -08005365 }
Etan Cohen3b7a1bc2015-05-28 15:57:13 -07005366 }
5367
Stuart Scott8eef64f2015-04-08 15:13:54 -07005368 @Override
5369 public void factoryReset(int subId) {
5370 enforceConnectivityInternalPermission();
Stuart Scott981d8582015-04-21 14:09:50 -07005371 if (mUserManager.hasUserRestriction(UserManager.DISALLOW_NETWORK_RESET)) {
5372 return;
5373 }
5374
Svet Ganovcc087f82015-05-12 20:35:54 -07005375 final long identity = Binder.clearCallingIdentity();
Nazanin Bakhshi2f2cc692018-12-11 15:15:39 -08005376
Svet Ganovcc087f82015-05-12 20:35:54 -07005377 try {
Stuart Scott981d8582015-04-21 14:09:50 -07005378 if (SubscriptionManager.isUsableSubIdValue(subId) && !mUserManager.hasUserRestriction(
5379 UserManager.DISALLOW_CONFIG_MOBILE_NETWORKS)) {
Pengquan Meng85728fb2018-03-12 16:31:21 -07005380 setUserDataEnabled(subId, getDefaultDataEnabled());
Svet Ganovcc087f82015-05-12 20:35:54 -07005381 setNetworkSelectionModeAutomatic(subId);
Pengquan Meng85728fb2018-03-12 16:31:21 -07005382 setPreferredNetworkType(subId, getDefaultNetworkType(subId));
Nazanin Bakhshi2f2cc692018-12-11 15:15:39 -08005383 setDataRoamingEnabled(subId, getDefaultDataRoamingEnabled(subId));
5384 CarrierInfoManager.deleteAllCarrierKeysForImsiEncryption(mApp);
Svet Ganovcc087f82015-05-12 20:35:54 -07005385 }
Amit Mahajan7dbbd822019-03-13 17:33:47 -07005386 // There has been issues when Sms raw table somehow stores orphan
5387 // fragments. They lead to garbled message when new fragments come
5388 // in and combined with those stale ones. In case this happens again,
5389 // user can reset all network settings which will clean up this table.
5390 cleanUpSmsRawTable(getDefaultPhone().getContext());
Svet Ganovcc087f82015-05-12 20:35:54 -07005391 } finally {
5392 Binder.restoreCallingIdentity(identity);
Stuart Scott8eef64f2015-04-08 15:13:54 -07005393 }
5394 }
Narayan Kamath1c496c22015-04-16 14:40:19 +01005395
Amit Mahajan7dbbd822019-03-13 17:33:47 -07005396 private void cleanUpSmsRawTable(Context context) {
5397 ContentResolver resolver = context.getContentResolver();
5398 Uri uri = Uri.withAppendedPath(Telephony.Sms.CONTENT_URI, "raw/permanentDelete");
5399 resolver.delete(uri, null, null);
5400 }
5401
Narayan Kamath1c496c22015-04-16 14:40:19 +01005402 @Override
chen xu2e6dfec2019-01-21 23:31:38 -08005403 public String getSimLocaleForSubscriber(int subId) {
5404 enforceReadPrivilegedPermission("getSimLocaleForSubscriber, subId: " + subId);
5405 final Phone phone = getPhone(subId);
5406 if (phone == null) {
5407 log("getSimLocaleForSubscriber, invalid subId");
Pengquan Meng9c291482019-01-28 16:26:29 -08005408 return null;
chen xu2e6dfec2019-01-21 23:31:38 -08005409 }
Malcolm Chend965c8b2018-02-28 15:00:40 -08005410 final long identity = Binder.clearCallingIdentity();
5411 try {
chen xu2e6dfec2019-01-21 23:31:38 -08005412 final SubscriptionInfo info = mSubscriptionController.getActiveSubscriptionInfo(subId,
5413 phone.getContext().getOpPackageName());
Malcolm Chend965c8b2018-02-28 15:00:40 -08005414 // Try and fetch the locale from the carrier properties or from the SIM language
5415 // preferences (EF-PL and EF-LI)...
5416 final int mcc = info.getMcc();
Malcolm Chend965c8b2018-02-28 15:00:40 -08005417 String simLanguage = null;
chen xu2e6dfec2019-01-21 23:31:38 -08005418 final Locale localeFromDefaultSim = phone.getLocaleFromSimAndCarrierPrefs();
5419 if (localeFromDefaultSim != null) {
5420 if (!localeFromDefaultSim.getCountry().isEmpty()) {
5421 if (DBG) log("Using locale from subId: " + subId + " locale: "
5422 + localeFromDefaultSim);
5423 return localeFromDefaultSim.toLanguageTag();
5424 } else {
5425 simLanguage = localeFromDefaultSim.getLanguage();
Malcolm Chend965c8b2018-02-28 15:00:40 -08005426 }
5427 }
Narayan Kamath1c496c22015-04-16 14:40:19 +01005428
Malcolm Chend965c8b2018-02-28 15:00:40 -08005429 // The SIM language preferences only store a language (e.g. fr = French), not an
5430 // exact locale (e.g. fr_FR = French/France). So, if the locale returned from
5431 // the SIM and carrier preferences does not include a country we add the country
5432 // determined from the SIM MCC to provide an exact locale.
Nazanin Bakhshi2f2cc692018-12-11 15:15:39 -08005433 final Locale mccLocale = MccTable.getLocaleFromMcc(mApp, mcc, simLanguage);
Malcolm Chend965c8b2018-02-28 15:00:40 -08005434 if (mccLocale != null) {
chen xu2e6dfec2019-01-21 23:31:38 -08005435 if (DBG) log("No locale from SIM, using mcc locale:" + mccLocale);
Malcolm Chend965c8b2018-02-28 15:00:40 -08005436 return mccLocale.toLanguageTag();
5437 }
5438
5439 if (DBG) log("No locale found - returning null");
5440 return null;
5441 } finally {
5442 Binder.restoreCallingIdentity(identity);
5443 }
Narayan Kamath1c496c22015-04-16 14:40:19 +01005444 }
5445
5446 private List<SubscriptionInfo> getAllSubscriptionInfoList() {
Nazanin Bakhshi2f2cc692018-12-11 15:15:39 -08005447 return mSubscriptionController.getAllSubInfoList(mApp.getOpPackageName());
Narayan Kamath1c496c22015-04-16 14:40:19 +01005448 }
5449
Malcolm Chend965c8b2018-02-28 15:00:40 -08005450 /**
5451 * NOTE: this method assumes permission checks are done and caller identity has been cleared.
5452 */
5453 private List<SubscriptionInfo> getActiveSubscriptionInfoListPrivileged() {
Nazanin Bakhshi2f2cc692018-12-11 15:15:39 -08005454 return mSubscriptionController.getActiveSubscriptionInfoList(mApp.getOpPackageName());
Narayan Kamath1c496c22015-04-16 14:40:19 +01005455 }
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -07005456
Chenjie Yu1ba97252018-01-11 18:16:20 -08005457 private final ModemActivityInfo mLastModemActivityInfo =
5458 new ModemActivityInfo(0, 0, 0, new int[0], 0, 0);
5459
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -07005460 /**
Adam Lesinski903a54c2016-04-11 14:49:52 -07005461 * Responds to the ResultReceiver with the {@link android.telephony.ModemActivityInfo} object
5462 * representing the state of the modem.
5463 *
Chenjie Yu1ba97252018-01-11 18:16:20 -08005464 * NOTE: The underlying implementation clears the modem state, so there should only ever be one
5465 * caller to it. Everyone should call this class to get cumulative data.
Adam Lesinski903a54c2016-04-11 14:49:52 -07005466 * @hide
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -07005467 */
5468 @Override
Adam Lesinski903a54c2016-04-11 14:49:52 -07005469 public void requestModemActivityInfo(ResultReceiver result) {
5470 enforceModifyPermission();
vagdevie435a3e2018-08-15 16:01:53 -07005471 WorkSource workSource = getWorkSource(Binder.getCallingUid());
Malcolm Chend965c8b2018-02-28 15:00:40 -08005472
5473 final long identity = Binder.clearCallingIdentity();
5474 try {
5475 ModemActivityInfo ret = null;
5476 synchronized (mLastModemActivityInfo) {
vagdevie435a3e2018-08-15 16:01:53 -07005477 ModemActivityInfo info = (ModemActivityInfo) sendRequest(
5478 CMD_GET_MODEM_ACTIVITY_INFO,
5479 null, workSource);
Malcolm Chend965c8b2018-02-28 15:00:40 -08005480 if (isModemActivityInfoValid(info)) {
5481 int[] mergedTxTimeMs = new int[ModemActivityInfo.TX_POWER_LEVELS];
5482 for (int i = 0; i < mergedTxTimeMs.length; i++) {
5483 mergedTxTimeMs[i] =
5484 info.getTxTimeMillis()[i] + mLastModemActivityInfo.getTxTimeMillis()[i];
5485 }
5486 mLastModemActivityInfo.setTimestamp(info.getTimestamp());
5487 mLastModemActivityInfo.setSleepTimeMillis(
5488 info.getSleepTimeMillis() + mLastModemActivityInfo.getSleepTimeMillis());
5489 mLastModemActivityInfo.setIdleTimeMillis(
5490 info.getIdleTimeMillis() + mLastModemActivityInfo.getIdleTimeMillis());
5491 mLastModemActivityInfo.setTxTimeMillis(mergedTxTimeMs);
5492 mLastModemActivityInfo.setRxTimeMillis(
5493 info.getRxTimeMillis() + mLastModemActivityInfo.getRxTimeMillis());
5494 mLastModemActivityInfo.setEnergyUsed(
5495 info.getEnergyUsed() + mLastModemActivityInfo.getEnergyUsed());
Chenjie Yu1ba97252018-01-11 18:16:20 -08005496 }
Malcolm Chend965c8b2018-02-28 15:00:40 -08005497 ret = new ModemActivityInfo(mLastModemActivityInfo.getTimestamp(),
5498 mLastModemActivityInfo.getSleepTimeMillis(),
5499 mLastModemActivityInfo.getIdleTimeMillis(),
5500 mLastModemActivityInfo.getTxTimeMillis(),
5501 mLastModemActivityInfo.getRxTimeMillis(),
5502 mLastModemActivityInfo.getEnergyUsed());
Chenjie Yu1ba97252018-01-11 18:16:20 -08005503 }
Malcolm Chend965c8b2018-02-28 15:00:40 -08005504 Bundle bundle = new Bundle();
5505 bundle.putParcelable(TelephonyManager.MODEM_ACTIVITY_RESULT_KEY, ret);
5506 result.send(0, bundle);
5507 } finally {
5508 Binder.restoreCallingIdentity(identity);
Chenjie Yu1ba97252018-01-11 18:16:20 -08005509 }
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -07005510 }
Jack Yu85bd38a2015-11-09 11:34:32 -08005511
Siddharth Rayf5d29552018-06-17 15:02:38 -07005512 // Checks that ModemActivityInfo is valid. Sleep time, Idle time, Rx time and Tx time should be
5513 // less than total activity duration.
5514 private boolean isModemActivityInfoValid(ModemActivityInfo info) {
5515 if (info == null) {
5516 return false;
5517 }
5518 int activityDurationMs =
5519 (int) (info.getTimestamp() - mLastModemActivityInfo.getTimestamp());
5520 int totalTxTimeMs = 0;
5521 for (int i = 0; i < info.getTxTimeMillis().length; i++) {
5522 totalTxTimeMs += info.getTxTimeMillis()[i];
5523 }
5524 return (info.isValid()
5525 && (info.getSleepTimeMillis() <= activityDurationMs)
5526 && (info.getIdleTimeMillis() <= activityDurationMs)
5527 && (info.getRxTimeMillis() <= activityDurationMs)
5528 && (totalTxTimeMs <= activityDurationMs));
5529 }
5530
Jack Yu85bd38a2015-11-09 11:34:32 -08005531 /**
5532 * {@hide}
5533 * Returns the service state information on specified subscription.
5534 */
5535 @Override
5536 public ServiceState getServiceStateForSubscriber(int subId, String callingPackage) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08005537 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
Jeff Davidson913390f2018-02-23 17:11:49 -08005538 mApp, subId, callingPackage, "getServiceStateForSubscriber")) {
Jack Yu85bd38a2015-11-09 11:34:32 -08005539 return null;
5540 }
5541
Hall Liuf19c44f2018-11-27 14:38:17 -08005542 LocationAccessPolicy.LocationPermissionResult fineLocationResult =
5543 LocationAccessPolicy.checkLocationPermission(mApp,
5544 new LocationAccessPolicy.LocationPermissionQuery.Builder()
5545 .setCallingPackage(callingPackage)
5546 .setCallingPid(Binder.getCallingPid())
5547 .setCallingUid(Binder.getCallingUid())
5548 .setMethod("getServiceStateForSubscriber")
Hall Liu8b0cba22019-04-17 13:37:11 -07005549 .setLogAsInfo(true)
Hall Liuf19c44f2018-11-27 14:38:17 -08005550 .setMinSdkVersionForFine(Build.VERSION_CODES.Q)
5551 .build());
5552
5553 LocationAccessPolicy.LocationPermissionResult coarseLocationResult =
5554 LocationAccessPolicy.checkLocationPermission(mApp,
5555 new LocationAccessPolicy.LocationPermissionQuery.Builder()
5556 .setCallingPackage(callingPackage)
5557 .setCallingPid(Binder.getCallingPid())
5558 .setCallingUid(Binder.getCallingUid())
5559 .setMethod("getServiceStateForSubscriber")
Hall Liu8b0cba22019-04-17 13:37:11 -07005560 .setLogAsInfo(true)
Hall Liuf19c44f2018-11-27 14:38:17 -08005561 .setMinSdkVersionForCoarse(Build.VERSION_CODES.Q)
5562 .build());
5563 // We don't care about hard or soft here -- all we need to know is how much info to scrub.
5564 boolean hasFinePermission =
5565 fineLocationResult == LocationAccessPolicy.LocationPermissionResult.ALLOWED;
5566 boolean hasCoarsePermission =
5567 coarseLocationResult == LocationAccessPolicy.LocationPermissionResult.ALLOWED;
5568
Malcolm Chend965c8b2018-02-28 15:00:40 -08005569 final long identity = Binder.clearCallingIdentity();
5570 try {
5571 final Phone phone = getPhone(subId);
5572 if (phone == null) {
5573 return null;
5574 }
Jack Yu85bd38a2015-11-09 11:34:32 -08005575
Hall Liuf19c44f2018-11-27 14:38:17 -08005576 ServiceState ss = phone.getServiceState();
5577
5578 // Scrub out the location info in ServiceState depending on what level of access
5579 // the caller has.
5580 if (hasFinePermission) return ss;
5581 if (hasCoarsePermission) return ss.sanitizeLocationInfo(false);
5582 return ss.sanitizeLocationInfo(true);
Malcolm Chend965c8b2018-02-28 15:00:40 -08005583 } finally {
5584 Binder.restoreCallingIdentity(identity);
5585 }
Jack Yu85bd38a2015-11-09 11:34:32 -08005586 }
Nancy Chen31f9ba12016-01-06 11:42:12 -08005587
5588 /**
5589 * Returns the URI for the per-account voicemail ringtone set in Phone settings.
5590 *
5591 * @param accountHandle The handle for the {@link PhoneAccount} for which to retrieve the
5592 * voicemail ringtone.
5593 * @return The URI for the ringtone to play when receiving a voicemail from a specific
5594 * PhoneAccount.
5595 */
5596 @Override
5597 public Uri getVoicemailRingtoneUri(PhoneAccountHandle accountHandle) {
Malcolm Chend965c8b2018-02-28 15:00:40 -08005598 final long identity = Binder.clearCallingIdentity();
5599 try {
5600 Phone phone = PhoneUtils.getPhoneForPhoneAccountHandle(accountHandle);
5601 if (phone == null) {
Nazanin Bakhshi2f2cc692018-12-11 15:15:39 -08005602 phone = getDefaultPhone();
Malcolm Chend965c8b2018-02-28 15:00:40 -08005603 }
Nancy Chen31f9ba12016-01-06 11:42:12 -08005604
Malcolm Chend965c8b2018-02-28 15:00:40 -08005605 return VoicemailNotificationSettingsUtil.getRingtoneUri(phone.getContext());
5606 } finally {
5607 Binder.restoreCallingIdentity(identity);
5608 }
Nancy Chen31f9ba12016-01-06 11:42:12 -08005609 }
5610
5611 /**
Ta-wei Yenc33877d2017-01-23 18:11:21 -08005612 * Sets the per-account voicemail ringtone.
5613 *
5614 * <p>Requires that the calling app is the default dialer, or has carrier privileges, or
5615 * has permission {@link android.Manifest.permission#MODIFY_PHONE_STATE MODIFY_PHONE_STATE}.
5616 *
5617 * @param phoneAccountHandle The handle for the {@link PhoneAccount} for which to set the
5618 * voicemail ringtone.
5619 * @param uri The URI for the ringtone to play when receiving a voicemail from a specific
5620 * PhoneAccount.
5621 */
5622 @Override
5623 public void setVoicemailRingtoneUri(String callingPackage,
5624 PhoneAccountHandle phoneAccountHandle, Uri uri) {
Nazanin Bakhshi2f2cc692018-12-11 15:15:39 -08005625 final Phone defaultPhone = getDefaultPhone();
Ta-wei Yenc33877d2017-01-23 18:11:21 -08005626 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
5627 if (!TextUtils.equals(callingPackage,
Nazanin Bakhshi2f2cc692018-12-11 15:15:39 -08005628 TelecomManager.from(defaultPhone.getContext()).getDefaultDialerPackage())) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08005629 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
5630 mApp, PhoneUtils.getSubIdForPhoneAccountHandle(phoneAccountHandle),
5631 "setVoicemailRingtoneUri");
Ta-wei Yenc33877d2017-01-23 18:11:21 -08005632 }
Malcolm Chend965c8b2018-02-28 15:00:40 -08005633
5634 final long identity = Binder.clearCallingIdentity();
5635 try {
5636 Phone phone = PhoneUtils.getPhoneForPhoneAccountHandle(phoneAccountHandle);
5637 if (phone == null) {
Nazanin Bakhshi2f2cc692018-12-11 15:15:39 -08005638 phone = defaultPhone;
Malcolm Chend965c8b2018-02-28 15:00:40 -08005639 }
5640 VoicemailNotificationSettingsUtil.setRingtoneUri(phone.getContext(), uri);
5641 } finally {
5642 Binder.restoreCallingIdentity(identity);
Ta-wei Yenc33877d2017-01-23 18:11:21 -08005643 }
Ta-wei Yenc33877d2017-01-23 18:11:21 -08005644 }
5645
5646 /**
Nancy Chen31f9ba12016-01-06 11:42:12 -08005647 * Returns whether vibration is set for voicemail notification in Phone settings.
5648 *
5649 * @param accountHandle The handle for the {@link PhoneAccount} for which to retrieve the
5650 * voicemail vibration setting.
5651 * @return {@code true} if the vibration is set for this PhoneAccount, {@code false} otherwise.
5652 */
5653 @Override
5654 public boolean isVoicemailVibrationEnabled(PhoneAccountHandle accountHandle) {
Malcolm Chend965c8b2018-02-28 15:00:40 -08005655 final long identity = Binder.clearCallingIdentity();
5656 try {
5657 Phone phone = PhoneUtils.getPhoneForPhoneAccountHandle(accountHandle);
5658 if (phone == null) {
Nazanin Bakhshi2f2cc692018-12-11 15:15:39 -08005659 phone = getDefaultPhone();
Malcolm Chend965c8b2018-02-28 15:00:40 -08005660 }
Nancy Chen31f9ba12016-01-06 11:42:12 -08005661
Malcolm Chend965c8b2018-02-28 15:00:40 -08005662 return VoicemailNotificationSettingsUtil.isVibrationEnabled(phone.getContext());
5663 } finally {
5664 Binder.restoreCallingIdentity(identity);
5665 }
Nancy Chen31f9ba12016-01-06 11:42:12 -08005666 }
5667
Youhan Wange64578a2016-05-02 15:32:42 -07005668 /**
Ta-wei Yenc33877d2017-01-23 18:11:21 -08005669 * Sets the per-account voicemail vibration.
5670 *
5671 * <p>Requires that the calling app is the default dialer, or has carrier privileges, or
5672 * has permission {@link android.Manifest.permission#MODIFY_PHONE_STATE MODIFY_PHONE_STATE}.
5673 *
5674 * @param phoneAccountHandle The handle for the {@link PhoneAccount} for which to set the
5675 * voicemail vibration setting.
5676 * @param enabled Whether to enable or disable vibration for voicemail notifications from a
5677 * specific PhoneAccount.
5678 */
5679 @Override
5680 public void setVoicemailVibrationEnabled(String callingPackage,
5681 PhoneAccountHandle phoneAccountHandle, boolean enabled) {
Nazanin Bakhshi2f2cc692018-12-11 15:15:39 -08005682 final Phone defaultPhone = getDefaultPhone();
Ta-wei Yenc33877d2017-01-23 18:11:21 -08005683 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
5684 if (!TextUtils.equals(callingPackage,
Nazanin Bakhshi2f2cc692018-12-11 15:15:39 -08005685 TelecomManager.from(defaultPhone.getContext()).getDefaultDialerPackage())) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08005686 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
5687 mApp, PhoneUtils.getSubIdForPhoneAccountHandle(phoneAccountHandle),
5688 "setVoicemailVibrationEnabled");
Ta-wei Yenc33877d2017-01-23 18:11:21 -08005689 }
5690
Malcolm Chend965c8b2018-02-28 15:00:40 -08005691 final long identity = Binder.clearCallingIdentity();
5692 try {
5693 Phone phone = PhoneUtils.getPhoneForPhoneAccountHandle(phoneAccountHandle);
5694 if (phone == null) {
Nazanin Bakhshi2f2cc692018-12-11 15:15:39 -08005695 phone = defaultPhone;
Malcolm Chend965c8b2018-02-28 15:00:40 -08005696 }
5697 VoicemailNotificationSettingsUtil.setVibrationEnabled(phone.getContext(), enabled);
5698 } finally {
5699 Binder.restoreCallingIdentity(identity);
Ta-wei Yenc33877d2017-01-23 18:11:21 -08005700 }
Ta-wei Yenc33877d2017-01-23 18:11:21 -08005701 }
5702
5703 /**
Youhan Wange64578a2016-05-02 15:32:42 -07005704 * Make sure either called from same process as self (phone) or IPC caller has read privilege.
5705 *
5706 * @throws SecurityException if the caller does not have the required permission
5707 */
Brad Ebinger4c460712018-10-01 10:40:55 -07005708 private void enforceReadPrivilegedPermission(String message) {
Youhan Wange64578a2016-05-02 15:32:42 -07005709 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.READ_PRIVILEGED_PHONE_STATE,
Brad Ebinger4c460712018-10-01 10:40:55 -07005710 message);
Youhan Wange64578a2016-05-02 15:32:42 -07005711 }
5712
5713 /**
Ta-wei Yen30a69c82016-12-27 14:52:32 -08005714 * Make sure either called from same process as self (phone) or IPC caller has send SMS
5715 * permission.
5716 *
5717 * @throws SecurityException if the caller does not have the required permission
5718 */
5719 private void enforceSendSmsPermission() {
5720 mApp.enforceCallingOrSelfPermission(permission.SEND_SMS, null);
5721 }
5722
5723 /**
Ta-wei Yen527a9c02017-01-06 15:29:25 -08005724 * Make sure called from the package in charge of visual voicemail.
Ta-wei Yen30a69c82016-12-27 14:52:32 -08005725 *
Ta-wei Yen527a9c02017-01-06 15:29:25 -08005726 * @throws SecurityException if the caller is not the visual voicemail package.
Ta-wei Yen30a69c82016-12-27 14:52:32 -08005727 */
Ta-wei Yen527a9c02017-01-06 15:29:25 -08005728 private void enforceVisualVoicemailPackage(String callingPackage, int subId) {
Malcolm Chend965c8b2018-02-28 15:00:40 -08005729 final long identity = Binder.clearCallingIdentity();
5730 try {
5731 ComponentName componentName =
Nazanin Bakhshi2f2cc692018-12-11 15:15:39 -08005732 RemoteVvmTaskManager.getRemotePackage(mApp, subId);
Malcolm Chend965c8b2018-02-28 15:00:40 -08005733 if (componentName == null) {
5734 throw new SecurityException(
5735 "Caller not current active visual voicemail package[null]");
5736 }
5737 String vvmPackage = componentName.getPackageName();
5738 if (!callingPackage.equals(vvmPackage)) {
5739 throw new SecurityException("Caller not current active visual voicemail package["
5740 + vvmPackage + "]");
5741 }
5742 } finally {
5743 Binder.restoreCallingIdentity(identity);
Ta-wei Yen30a69c82016-12-27 14:52:32 -08005744 }
5745 }
5746
5747 /**
Youhan Wange64578a2016-05-02 15:32:42 -07005748 * Return the application ID for the app type.
5749 *
5750 * @param subId the subscription ID that this request applies to.
5751 * @param appType the uicc app type.
5752 * @return Application ID for specificied app type, or null if no uicc.
5753 */
5754 @Override
5755 public String getAidForAppType(int subId, int appType) {
Brad Ebinger4c460712018-10-01 10:40:55 -07005756 enforceReadPrivilegedPermission("getAidForAppType");
Youhan Wange64578a2016-05-02 15:32:42 -07005757 Phone phone = getPhone(subId);
Malcolm Chend965c8b2018-02-28 15:00:40 -08005758
5759 final long identity = Binder.clearCallingIdentity();
Youhan Wange64578a2016-05-02 15:32:42 -07005760 try {
Malcolm Chend965c8b2018-02-28 15:00:40 -08005761 if (phone == null) {
5762 return null;
5763 }
5764 String aid = null;
5765 try {
5766 aid = UiccController.getInstance().getUiccCard(phone.getPhoneId())
5767 .getApplicationByType(appType).getAid();
5768 } catch (Exception e) {
5769 Log.e(LOG_TAG, "Not getting aid. Exception ex=" + e);
5770 }
5771 return aid;
5772 } finally {
5773 Binder.restoreCallingIdentity(identity);
Youhan Wange64578a2016-05-02 15:32:42 -07005774 }
Youhan Wange64578a2016-05-02 15:32:42 -07005775 }
5776
Youhan Wang4001d252016-05-11 10:29:41 -07005777 /**
5778 * Return the Electronic Serial Number.
5779 *
5780 * @param subId the subscription ID that this request applies to.
5781 * @return ESN or null if error.
5782 */
5783 @Override
5784 public String getEsn(int subId) {
Brad Ebinger4c460712018-10-01 10:40:55 -07005785 enforceReadPrivilegedPermission("getEsn");
Youhan Wang4001d252016-05-11 10:29:41 -07005786 Phone phone = getPhone(subId);
Malcolm Chend965c8b2018-02-28 15:00:40 -08005787
5788 final long identity = Binder.clearCallingIdentity();
Youhan Wang4001d252016-05-11 10:29:41 -07005789 try {
Malcolm Chend965c8b2018-02-28 15:00:40 -08005790 if (phone == null) {
5791 return null;
5792 }
5793 String esn = null;
5794 try {
5795 esn = phone.getEsn();
5796 } catch (Exception e) {
5797 Log.e(LOG_TAG, "Not getting ESN. Exception ex=" + e);
5798 }
5799 return esn;
5800 } finally {
5801 Binder.restoreCallingIdentity(identity);
Youhan Wang4001d252016-05-11 10:29:41 -07005802 }
Youhan Wang4001d252016-05-11 10:29:41 -07005803 }
5804
Sanket Padawe99ef1e32016-05-18 16:12:33 -07005805 /**
Youhan Wang66ad5d72016-07-18 17:56:58 -07005806 * Return the Preferred Roaming List Version.
5807 *
5808 * @param subId the subscription ID that this request applies to.
5809 * @return PRLVersion or null if error.
5810 */
5811 @Override
5812 public String getCdmaPrlVersion(int subId) {
Brad Ebinger4c460712018-10-01 10:40:55 -07005813 enforceReadPrivilegedPermission("getCdmaPrlVersion");
Youhan Wang66ad5d72016-07-18 17:56:58 -07005814 Phone phone = getPhone(subId);
Malcolm Chend965c8b2018-02-28 15:00:40 -08005815
5816 final long identity = Binder.clearCallingIdentity();
Youhan Wang66ad5d72016-07-18 17:56:58 -07005817 try {
Malcolm Chend965c8b2018-02-28 15:00:40 -08005818 if (phone == null) {
5819 return null;
5820 }
5821 String cdmaPrlVersion = null;
5822 try {
5823 cdmaPrlVersion = phone.getCdmaPrlVersion();
5824 } catch (Exception e) {
5825 Log.e(LOG_TAG, "Not getting PRLVersion", e);
5826 }
5827 return cdmaPrlVersion;
5828 } finally {
5829 Binder.restoreCallingIdentity(identity);
Youhan Wang66ad5d72016-07-18 17:56:58 -07005830 }
Youhan Wang66ad5d72016-07-18 17:56:58 -07005831 }
5832
5833 /**
Sanket Padawe99ef1e32016-05-18 16:12:33 -07005834 * Get snapshot of Telephony histograms
5835 * @return List of Telephony histograms
5836 * @hide
5837 */
5838 @Override
5839 public List<TelephonyHistogram> getTelephonyHistograms() {
Jeff Davidson7e17e312018-02-13 18:17:36 -08005840 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
5841 mApp, getDefaultSubscription(), "getTelephonyHistograms");
Malcolm Chend965c8b2018-02-28 15:00:40 -08005842
5843 final long identity = Binder.clearCallingIdentity();
5844 try {
5845 return RIL.getTelephonyRILTimingHistograms();
5846 } finally {
5847 Binder.restoreCallingIdentity(identity);
5848 }
Sanket Padawe99ef1e32016-05-18 16:12:33 -07005849 }
Meng Wang1a7c35a2016-05-05 20:56:15 -07005850
5851 /**
5852 * {@hide}
Michele Berionne0963c862018-11-27 18:57:59 -08005853 * Set the allowed carrier list and the excluded carrier list, indicating the priority between
5854 * the two lists.
Meng Wang1a7c35a2016-05-05 20:56:15 -07005855 * Require system privileges. In the future we may add this to carrier APIs.
5856 *
Michele Berionne0963c862018-11-27 18:57:59 -08005857 * @return Integer with the result of the operation, as defined in {@link TelephonyManager}.
Meng Wang1a7c35a2016-05-05 20:56:15 -07005858 */
5859 @Override
Michele Berionne0963c862018-11-27 18:57:59 -08005860 @TelephonyManager.SetCarrierRestrictionResult
5861 public int setAllowedCarriers(CarrierRestrictionRules carrierRestrictionRules) {
Meng Wang1a7c35a2016-05-05 20:56:15 -07005862 enforceModifyPermission();
vagdevie435a3e2018-08-15 16:01:53 -07005863 WorkSource workSource = getWorkSource(Binder.getCallingUid());
Sanket Padawe13bac7b2017-03-20 15:04:47 -07005864
Michele Berionne0963c862018-11-27 18:57:59 -08005865 if (carrierRestrictionRules == null) {
5866 throw new NullPointerException("carrier restriction cannot be null");
Meng Wang9b7c4e92017-02-17 11:41:27 -08005867 }
Sanket Padawe13bac7b2017-03-20 15:04:47 -07005868
Malcolm Chend965c8b2018-02-28 15:00:40 -08005869 final long identity = Binder.clearCallingIdentity();
5870 try {
Michele Berionne0963c862018-11-27 18:57:59 -08005871 return (int) sendRequest(CMD_SET_ALLOWED_CARRIERS, carrierRestrictionRules,
vagdevie435a3e2018-08-15 16:01:53 -07005872 workSource);
Malcolm Chend965c8b2018-02-28 15:00:40 -08005873 } finally {
5874 Binder.restoreCallingIdentity(identity);
5875 }
Meng Wang1a7c35a2016-05-05 20:56:15 -07005876 }
5877
5878 /**
5879 * {@hide}
Michele Berionne0963c862018-11-27 18:57:59 -08005880 * Get the allowed carrier list and the excluded carrier list, including the priority between
5881 * the two lists.
Meng Wang1a7c35a2016-05-05 20:56:15 -07005882 * Require system privileges. In the future we may add this to carrier APIs.
5883 *
Michele Berionne0963c862018-11-27 18:57:59 -08005884 * @return {@link android.telephony.CarrierRestrictionRules}
Meng Wang1a7c35a2016-05-05 20:56:15 -07005885 */
5886 @Override
Michele Berionne0963c862018-11-27 18:57:59 -08005887 public CarrierRestrictionRules getAllowedCarriers() {
Brad Ebinger4c460712018-10-01 10:40:55 -07005888 enforceReadPrivilegedPermission("getAllowedCarriers");
vagdevie435a3e2018-08-15 16:01:53 -07005889 WorkSource workSource = getWorkSource(Binder.getCallingUid());
Malcolm Chend965c8b2018-02-28 15:00:40 -08005890
5891 final long identity = Binder.clearCallingIdentity();
5892 try {
Michele Berionne0963c862018-11-27 18:57:59 -08005893 Object response = sendRequest(CMD_GET_ALLOWED_CARRIERS, null, workSource);
5894 if (response instanceof CarrierRestrictionRules) {
5895 return (CarrierRestrictionRules) response;
5896 }
5897 // Response is an Exception of some kind,
5898 // which is signalled to the user as a NULL retval
5899 return null;
5900 } catch (Exception e) {
5901 Log.e(LOG_TAG, "getAllowedCarriers. Exception ex=" + e);
5902 return null;
Malcolm Chend965c8b2018-02-28 15:00:40 -08005903 } finally {
5904 Binder.restoreCallingIdentity(identity);
5905 }
Meng Wang1a7c35a2016-05-05 20:56:15 -07005906 }
5907
fionaxu59545b42016-05-25 15:53:37 -07005908 /**
5909 * Action set from carrier signalling broadcast receivers to enable/disable metered apns
5910 * @param subId the subscription ID that this action applies to.
5911 * @param enabled control enable or disable metered apns.
5912 * {@hide}
5913 */
5914 @Override
5915 public void carrierActionSetMeteredApnsEnabled(int subId, boolean enabled) {
5916 enforceModifyPermission();
5917 final Phone phone = getPhone(subId);
Malcolm Chend965c8b2018-02-28 15:00:40 -08005918
5919 final long identity = Binder.clearCallingIdentity();
fionaxu59545b42016-05-25 15:53:37 -07005920 if (phone == null) {
5921 loge("carrierAction: SetMeteredApnsEnabled fails with invalid subId: " + subId);
5922 return;
5923 }
5924 try {
5925 phone.carrierActionSetMeteredApnsEnabled(enabled);
5926 } catch (Exception e) {
5927 Log.e(LOG_TAG, "carrierAction: SetMeteredApnsEnabled fails. Exception ex=" + e);
Malcolm Chend965c8b2018-02-28 15:00:40 -08005928 } finally {
5929 Binder.restoreCallingIdentity(identity);
fionaxu59545b42016-05-25 15:53:37 -07005930 }
5931 }
5932
5933 /**
5934 * Action set from carrier signalling broadcast receivers to enable/disable radio
5935 * @param subId the subscription ID that this action applies to.
5936 * @param enabled control enable or disable radio.
5937 * {@hide}
5938 */
5939 @Override
5940 public void carrierActionSetRadioEnabled(int subId, boolean enabled) {
5941 enforceModifyPermission();
5942 final Phone phone = getPhone(subId);
Malcolm Chend965c8b2018-02-28 15:00:40 -08005943
5944 final long identity = Binder.clearCallingIdentity();
fionaxu59545b42016-05-25 15:53:37 -07005945 if (phone == null) {
5946 loge("carrierAction: SetRadioEnabled fails with invalid sibId: " + subId);
5947 return;
5948 }
5949 try {
5950 phone.carrierActionSetRadioEnabled(enabled);
5951 } catch (Exception e) {
5952 Log.e(LOG_TAG, "carrierAction: SetRadioEnabled fails. Exception ex=" + e);
Malcolm Chend965c8b2018-02-28 15:00:40 -08005953 } finally {
5954 Binder.restoreCallingIdentity(identity);
fionaxu59545b42016-05-25 15:53:37 -07005955 }
5956 }
5957
Ta-wei Yenc236d6b2016-06-21 13:33:12 -07005958 /**
fionaxu8da9cb12017-05-23 15:02:46 -07005959 * Action set from carrier signalling broadcast receivers to start/stop reporting the default
5960 * network status based on which carrier apps could apply actions accordingly,
5961 * enable/disable default url handler for example.
5962 *
5963 * @param subId the subscription ID that this action applies to.
5964 * @param report control start/stop reporting the default network status.
5965 * {@hide}
5966 */
5967 @Override
5968 public void carrierActionReportDefaultNetworkStatus(int subId, boolean report) {
5969 enforceModifyPermission();
5970 final Phone phone = getPhone(subId);
Malcolm Chend965c8b2018-02-28 15:00:40 -08005971
5972 final long identity = Binder.clearCallingIdentity();
fionaxu8da9cb12017-05-23 15:02:46 -07005973 if (phone == null) {
5974 loge("carrierAction: ReportDefaultNetworkStatus fails with invalid sibId: " + subId);
5975 return;
5976 }
5977 try {
5978 phone.carrierActionReportDefaultNetworkStatus(report);
5979 } catch (Exception e) {
5980 Log.e(LOG_TAG, "carrierAction: ReportDefaultNetworkStatus fails. Exception ex=" + e);
Malcolm Chend965c8b2018-02-28 15:00:40 -08005981 } finally {
5982 Binder.restoreCallingIdentity(identity);
fionaxu8da9cb12017-05-23 15:02:46 -07005983 }
5984 }
5985
5986 /**
Ta-wei Yenc236d6b2016-06-21 13:33:12 -07005987 * Called when "adb shell dumpsys phone" is invoked. Dump is also automatically invoked when a
5988 * bug report is being generated.
5989 */
5990 @Override
Ta-wei Yen99282e02016-06-21 18:19:35 -07005991 protected void dump(FileDescriptor fd, PrintWriter writer, String[] args) {
Nazanin Bakhshi2f2cc692018-12-11 15:15:39 -08005992 if (mApp.checkCallingOrSelfPermission(android.Manifest.permission.DUMP)
5993 != PackageManager.PERMISSION_GRANTED) {
dcashman22b950d2016-06-27 11:39:02 -07005994 writer.println("Permission Denial: can't dump Phone from pid="
5995 + Binder.getCallingPid()
5996 + ", uid=" + Binder.getCallingUid()
5997 + "without permission "
5998 + android.Manifest.permission.DUMP);
5999 return;
6000 }
Nazanin Bakhshi2f2cc692018-12-11 15:15:39 -08006001 DumpsysHandler.dump(mApp, fd, writer, args);
Ta-wei Yenc236d6b2016-06-21 13:33:12 -07006002 }
Jack Yueb89b242016-06-22 13:27:47 -07006003
Brad Ebingerdac2f002018-04-03 15:17:52 -07006004 @Override
6005 public void onShellCommand(FileDescriptor in, FileDescriptor out, FileDescriptor err,
6006 String[] args, ShellCallback callback, ResultReceiver resultReceiver)
6007 throws RemoteException {
6008 (new TelephonyShellCommand(this)).exec(this, in, out, err, args, callback, resultReceiver);
6009 }
6010
Jack Yueb89b242016-06-22 13:27:47 -07006011 /**
Jack Yu84291ec2017-05-26 16:07:50 -07006012 * Get aggregated video call data usage since boot.
6013 *
6014 * @param perUidStats True if requesting data usage per uid, otherwise overall usage.
6015 * @return Snapshot of video call data usage
Jack Yueb89b242016-06-22 13:27:47 -07006016 * {@hide}
6017 */
6018 @Override
Jack Yu84291ec2017-05-26 16:07:50 -07006019 public NetworkStats getVtDataUsage(int subId, boolean perUidStats) {
Jack Yueb89b242016-06-22 13:27:47 -07006020 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.READ_NETWORK_USAGE_HISTORY,
6021 null);
6022
Malcolm Chend965c8b2018-02-28 15:00:40 -08006023 final long identity = Binder.clearCallingIdentity();
6024 try {
6025 // NetworkStatsService keeps tracking the active network interface and identity. It
6026 // records the delta with the corresponding network identity.
6027 // We just return the total video call data usage snapshot since boot.
6028 Phone phone = getPhone(subId);
6029 if (phone != null) {
6030 return phone.getVtDataUsage(perUidStats);
6031 }
6032 return null;
6033 } finally {
6034 Binder.restoreCallingIdentity(identity);
Jack Yueb89b242016-06-22 13:27:47 -07006035 }
Jack Yueb89b242016-06-22 13:27:47 -07006036 }
Jack Yu75ab2952016-07-08 14:29:33 -07006037
6038 /**
6039 * Policy control of data connection. Usually used when data limit is passed.
6040 * @param enabled True if enabling the data, otherwise disabling.
6041 * @param subId Subscription index
6042 * {@hide}
6043 */
6044 @Override
6045 public void setPolicyDataEnabled(boolean enabled, int subId) {
6046 enforceModifyPermission();
Malcolm Chend965c8b2018-02-28 15:00:40 -08006047
6048 final long identity = Binder.clearCallingIdentity();
6049 try {
6050 Phone phone = getPhone(subId);
6051 if (phone != null) {
Jack Yu7a030e52018-12-13 11:51:28 -08006052 phone.getDataEnabledSettings().setPolicyDataEnabled(enabled);
Malcolm Chend965c8b2018-02-28 15:00:40 -08006053 }
6054 } finally {
6055 Binder.restoreCallingIdentity(identity);
Jack Yu75ab2952016-07-08 14:29:33 -07006056 }
6057 }
Sooraj Sasindran9a909312016-07-18 11:57:25 -07006058
6059 /**
6060 * Get Client request stats
6061 * @return List of Client Request Stats
6062 * @hide
6063 */
6064 @Override
6065 public List<ClientRequestStats> getClientRequestStats(String callingPackage, int subId) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08006066 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
Jeff Davidson913390f2018-02-23 17:11:49 -08006067 mApp, subId, callingPackage, "getClientRequestStats")) {
Sooraj Sasindran9a909312016-07-18 11:57:25 -07006068 return null;
6069 }
Sooraj Sasindran9a909312016-07-18 11:57:25 -07006070 Phone phone = getPhone(subId);
Sooraj Sasindran9a909312016-07-18 11:57:25 -07006071
Malcolm Chend965c8b2018-02-28 15:00:40 -08006072 final long identity = Binder.clearCallingIdentity();
6073 try {
6074 if (phone != null) {
6075 return phone.getClientRequestStats();
6076 }
6077
6078 return null;
6079 } finally {
6080 Binder.restoreCallingIdentity(identity);
6081 }
Sooraj Sasindran9a909312016-07-18 11:57:25 -07006082 }
6083
Narayan Kamathf04b5a12018-01-09 11:47:15 +00006084 private WorkSource getWorkSource(int uid) {
Nazanin Bakhshi2f2cc692018-12-11 15:15:39 -08006085 String packageName = mApp.getPackageManager().getNameForUid(uid);
Narayan Kamathf04b5a12018-01-09 11:47:15 +00006086 return new WorkSource(uid, packageName);
Sooraj Sasindran9a909312016-07-18 11:57:25 -07006087 }
Jack Yueb4124c2017-02-16 15:32:43 -08006088
6089 /**
Grace Chen70990072017-03-24 17:21:30 -07006090 * Set SIM card power state.
Jack Yueb4124c2017-02-16 15:32:43 -08006091 *
Sanket Padawe13bac7b2017-03-20 15:04:47 -07006092 * @param slotIndex SIM slot id.
Grace Chen70990072017-03-24 17:21:30 -07006093 * @param state State of SIM (power down, power up, pass through)
6094 * - {@link android.telephony.TelephonyManager#CARD_POWER_DOWN}
6095 * - {@link android.telephony.TelephonyManager#CARD_POWER_UP}
6096 * - {@link android.telephony.TelephonyManager#CARD_POWER_UP_PASS_THROUGH}
Jack Yueb4124c2017-02-16 15:32:43 -08006097 *
6098 **/
6099 @Override
Grace Chen70990072017-03-24 17:21:30 -07006100 public void setSimPowerStateForSlot(int slotIndex, int state) {
Jack Yueb4124c2017-02-16 15:32:43 -08006101 enforceModifyPermission();
Sanket Padawe13bac7b2017-03-20 15:04:47 -07006102 Phone phone = PhoneFactory.getPhone(slotIndex);
6103
vagdevie435a3e2018-08-15 16:01:53 -07006104 WorkSource workSource = getWorkSource(Binder.getCallingUid());
6105
Malcolm Chend965c8b2018-02-28 15:00:40 -08006106 final long identity = Binder.clearCallingIdentity();
6107 try {
6108 if (phone != null) {
vagdevie435a3e2018-08-15 16:01:53 -07006109 phone.setSimPowerState(state, workSource);
Malcolm Chend965c8b2018-02-28 15:00:40 -08006110 }
6111 } finally {
6112 Binder.restoreCallingIdentity(identity);
Jack Yueb4124c2017-02-16 15:32:43 -08006113 }
6114 }
Shuo Qiandd210312017-04-12 22:11:33 +00006115
Tyler Gunn65d45c22017-06-05 11:22:26 -07006116 private boolean isUssdApiAllowed(int subId) {
6117 CarrierConfigManager configManager =
Nazanin Bakhshi2f2cc692018-12-11 15:15:39 -08006118 (CarrierConfigManager) mApp.getSystemService(Context.CARRIER_CONFIG_SERVICE);
Tyler Gunn65d45c22017-06-05 11:22:26 -07006119 if (configManager == null) {
6120 return false;
6121 }
6122 PersistableBundle pb = configManager.getConfigForSubId(subId);
6123 if (pb == null) {
6124 return false;
6125 }
6126 return pb.getBoolean(
6127 CarrierConfigManager.KEY_ALLOW_USSD_REQUESTS_VIA_TELEPHONY_MANAGER_BOOL);
6128 }
6129
Shuo Qiandd210312017-04-12 22:11:33 +00006130 /**
6131 * Check if phone is in emergency callback mode
6132 * @return true if phone is in emergency callback mode
6133 * @param subId sub id
6134 */
goneil9c5f4872017-12-05 14:07:56 -08006135 @Override
Shuo Qiandd210312017-04-12 22:11:33 +00006136 public boolean getEmergencyCallbackMode(int subId) {
Brad Ebinger4c460712018-10-01 10:40:55 -07006137 enforceReadPrivilegedPermission("getEmergencyCallbackMode");
Shuo Qiandd210312017-04-12 22:11:33 +00006138 final Phone phone = getPhone(subId);
Malcolm Chend965c8b2018-02-28 15:00:40 -08006139
6140 final long identity = Binder.clearCallingIdentity();
6141 try {
6142 if (phone != null) {
6143 return phone.isInEcm();
6144 } else {
6145 return false;
6146 }
6147 } finally {
6148 Binder.restoreCallingIdentity(identity);
Shuo Qiandd210312017-04-12 22:11:33 +00006149 }
6150 }
Nathan Harold46b42aa2017-03-10 19:38:22 -08006151
6152 /**
6153 * Get the current signal strength information for the given subscription.
6154 * Because this information is not updated when the device is in a low power state
6155 * it should not be relied-upon to be current.
6156 * @param subId Subscription index
6157 * @return the most recent cached signal strength info from the modem
6158 */
6159 @Override
6160 public SignalStrength getSignalStrength(int subId) {
Malcolm Chend965c8b2018-02-28 15:00:40 -08006161 final long identity = Binder.clearCallingIdentity();
6162 try {
6163 Phone p = getPhone(subId);
6164 if (p == null) {
6165 return null;
6166 }
Nathan Harold46b42aa2017-03-10 19:38:22 -08006167
Malcolm Chend965c8b2018-02-28 15:00:40 -08006168 return p.getSignalStrength();
6169 } finally {
6170 Binder.restoreCallingIdentity(identity);
6171 }
Nathan Harold46b42aa2017-03-10 19:38:22 -08006172 }
Holly Jiuyu Sun01c47ad2018-01-24 17:56:33 +00006173
Pengquan Meng9140aec2018-08-22 14:49:57 -07006174 /**
chen xu907e5a22018-10-11 13:21:04 -07006175 * Get the current modem radio state for the given slot.
6176 * @param slotIndex slot index.
6177 * @param callingPackage the name of the package making the call.
6178 * @return the current radio power state from the modem
6179 */
6180 @Override
6181 public int getRadioPowerState(int slotIndex, String callingPackage) {
6182 Phone phone = PhoneFactory.getPhone(slotIndex);
6183 if (phone != null) {
6184 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
6185 mApp, phone.getSubId(), callingPackage, "getRadioPowerState")) {
6186 return TelephonyManager.RADIO_POWER_UNAVAILABLE;
6187 }
6188
6189 final long identity = Binder.clearCallingIdentity();
6190 try {
6191 return phone.getRadioPowerState();
6192 } finally {
6193 Binder.restoreCallingIdentity(identity);
6194 }
6195 }
6196 return TelephonyManager.RADIO_POWER_UNAVAILABLE;
6197 }
6198
6199 /**
Pengquan Meng9140aec2018-08-22 14:49:57 -07006200 * Checks if data roaming is enabled on the subscription with id {@code subId}.
6201 *
6202 * <p>Requires one of the following permissions:
6203 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE},
6204 * {@link android.Manifest.permission#READ_PHONE_STATE} or that the calling app has carrier
6205 * privileges.
6206 *
6207 * @param subId subscription id
6208 * @return {@code true} if data roaming is enabled on this subscription, otherwise return
6209 * {@code false}.
6210 */
6211 @Override
6212 public boolean isDataRoamingEnabled(int subId) {
Pengquan Meng44e66f12019-04-01 10:48:20 -07006213 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.ACCESS_NETWORK_STATE,
6214 null /* message */);
6215
Pengquan Meng0c05b502018-09-06 09:59:22 -07006216 boolean isEnabled = false;
6217 final long identity = Binder.clearCallingIdentity();
Pengquan Meng9140aec2018-08-22 14:49:57 -07006218 try {
Pengquan Meng0c05b502018-09-06 09:59:22 -07006219 Phone phone = getPhone(subId);
6220 isEnabled = phone != null ? phone.getDataRoamingEnabled() : false;
Pengquan Meng9140aec2018-08-22 14:49:57 -07006221 } catch (Exception e) {
6222 TelephonyPermissions.enforeceCallingOrSelfReadPhoneStatePermissionOrCarrierPrivilege(
6223 mApp, subId, "isDataRoamingEnabled");
Pengquan Meng0c05b502018-09-06 09:59:22 -07006224 } finally {
6225 Binder.restoreCallingIdentity(identity);
Pengquan Meng9140aec2018-08-22 14:49:57 -07006226 }
Pengquan Meng0c05b502018-09-06 09:59:22 -07006227 return isEnabled;
Pengquan Meng9140aec2018-08-22 14:49:57 -07006228 }
6229
6230
6231 /**
6232 * Enables/Disables the data roaming on the subscription with id {@code subId}.
6233 *
6234 * <p> Requires permission:
6235 * {@link android.Manifest.permission#MODIFY_PHONE_STATE} or that the calling app has carrier
6236 * privileges.
6237 *
6238 * @param subId subscription id
6239 * @param isEnabled {@code true} means enable, {@code false} means disable.
6240 */
6241 @Override
6242 public void setDataRoamingEnabled(int subId, boolean isEnabled) {
Pengquan Meng44e66f12019-04-01 10:48:20 -07006243 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
6244 mApp, subId, "setDataRoamingEnabled");
6245
Pengquan Meng0c05b502018-09-06 09:59:22 -07006246 final long identity = Binder.clearCallingIdentity();
6247 try {
Pengquan Meng0c05b502018-09-06 09:59:22 -07006248 Phone phone = getPhone(subId);
6249 if (phone != null) {
6250 phone.setDataRoamingEnabled(isEnabled);
6251 }
6252 } finally {
6253 Binder.restoreCallingIdentity(identity);
Pengquan Meng9140aec2018-08-22 14:49:57 -07006254 }
6255 }
6256
Holly Jiuyu Sun01c47ad2018-01-24 17:56:33 +00006257 @Override
Pengquan Meng312de0c2018-10-03 12:19:13 -07006258 public boolean isManualNetworkSelectionAllowed(int subId) {
Pengquan Meng44e66f12019-04-01 10:48:20 -07006259 TelephonyPermissions.enforeceCallingOrSelfReadPhoneStatePermissionOrCarrierPrivilege(
6260 mApp, subId, "isManualNetworkSelectionAllowed");
6261
Pengquan Meng312de0c2018-10-03 12:19:13 -07006262 boolean isAllowed = true;
6263 final long identity = Binder.clearCallingIdentity();
6264 try {
Pengquan Meng312de0c2018-10-03 12:19:13 -07006265 Phone phone = getPhone(subId);
6266 if (phone != null) {
6267 isAllowed = phone.isCspPlmnEnabled();
6268 }
6269 } finally {
6270 Binder.restoreCallingIdentity(identity);
6271 }
6272 return isAllowed;
6273 }
6274
6275 @Override
Jordan Liu75f43ea2019-01-17 16:56:37 -08006276 public List<UiccCardInfo> getUiccCardsInfo(String callingPackage) {
Jordan Liu67997952019-04-22 15:10:43 -07006277 boolean hasReadPermission = false;
Jordan Liu53fdb782019-02-12 17:54:02 -08006278 try {
6279 enforceReadPrivilegedPermission("getUiccCardsInfo");
Jordan Liu67997952019-04-22 15:10:43 -07006280 hasReadPermission = true;
Jordan Liu53fdb782019-02-12 17:54:02 -08006281 } catch (SecurityException e) {
6282 // even without READ_PRIVILEGED_PHONE_STATE, we allow the call to continue if the caller
6283 // has carrier privileges on an active UICC
6284 if (checkCarrierPrivilegesForPackageAnyPhone(callingPackage)
6285 != TelephonyManager.CARRIER_PRIVILEGE_STATUS_HAS_ACCESS) {
Jordan Liu67997952019-04-22 15:10:43 -07006286 throw new SecurityException("Caller does not have permission.");
Jordan Liu53fdb782019-02-12 17:54:02 -08006287 }
Jordan Liu75f43ea2019-01-17 16:56:37 -08006288 }
Jordan Liu5aa07002018-12-18 15:44:48 -08006289
6290 final long identity = Binder.clearCallingIdentity();
6291 try {
Jordan Liu75f43ea2019-01-17 16:56:37 -08006292 UiccController uiccController = UiccController.getInstance();
6293 ArrayList<UiccCardInfo> cardInfos = uiccController.getAllUiccCardInfos();
Jordan Liu67997952019-04-22 15:10:43 -07006294 if (hasReadPermission) {
6295 return cardInfos;
Jordan Liu75f43ea2019-01-17 16:56:37 -08006296 }
Jordan Liu67997952019-04-22 15:10:43 -07006297
6298 // Remove private info if the caller doesn't have access
6299 ArrayList<UiccCardInfo> filteredInfos = new ArrayList<>();
6300 for (UiccCardInfo cardInfo : cardInfos) {
6301 // For an inactive eUICC, the UiccCard will be null even though the UiccCardInfo
6302 // is available
6303 UiccCard card = uiccController.getUiccCardForSlot(cardInfo.getSlotIndex());
6304 if (card == null || card.getUiccProfile() == null) {
6305 // assume no access if the card or profile is unavailable
6306 filteredInfos.add(cardInfo.getUnprivileged());
6307 continue;
6308 }
6309 UiccProfile profile = card.getUiccProfile();
6310 if (profile.getCarrierPrivilegeStatus(mApp.getPackageManager(), callingPackage)
6311 == TelephonyManager.CARRIER_PRIVILEGE_STATUS_HAS_ACCESS) {
6312 filteredInfos.add(cardInfo);
6313 } else {
6314 filteredInfos.add(cardInfo.getUnprivileged());
6315 }
6316 }
6317 return filteredInfos;
Jordan Liu5aa07002018-12-18 15:44:48 -08006318 } finally {
6319 Binder.restoreCallingIdentity(identity);
6320 }
6321 }
6322
6323 @Override
Holly Jiuyu Sun01c47ad2018-01-24 17:56:33 +00006324 public UiccSlotInfo[] getUiccSlotsInfo() {
Brad Ebinger4c460712018-10-01 10:40:55 -07006325 enforceReadPrivilegedPermission("getUiccSlotsInfo");
Holly Jiuyu Sun01c47ad2018-01-24 17:56:33 +00006326
Malcolm Chend965c8b2018-02-28 15:00:40 -08006327 final long identity = Binder.clearCallingIdentity();
6328 try {
6329 UiccSlot[] slots = UiccController.getInstance().getUiccSlots();
6330 if (slots == null) {
6331 Rlog.i(LOG_TAG, "slots is null.");
6332 return null;
6333 }
6334
6335 UiccSlotInfo[] infos = new UiccSlotInfo[slots.length];
6336 for (int i = 0; i < slots.length; i++) {
6337 UiccSlot slot = slots[i];
6338 if (slot == null) {
6339 continue;
6340 }
6341
6342 String cardId;
6343 UiccCard card = slot.getUiccCard();
6344 if (card != null) {
6345 cardId = card.getCardId();
6346 } else {
6347 cardId = slot.getIccId();
6348 }
6349
6350 int cardState = 0;
6351 switch (slot.getCardState()) {
6352 case CARDSTATE_ABSENT:
6353 cardState = UiccSlotInfo.CARD_STATE_INFO_ABSENT;
6354 break;
6355 case CARDSTATE_PRESENT:
6356 cardState = UiccSlotInfo.CARD_STATE_INFO_PRESENT;
6357 break;
6358 case CARDSTATE_ERROR:
6359 cardState = UiccSlotInfo.CARD_STATE_INFO_ERROR;
6360 break;
6361 case CARDSTATE_RESTRICTED:
6362 cardState = UiccSlotInfo.CARD_STATE_INFO_RESTRICTED;
6363 break;
6364 default:
6365 break;
6366
6367 }
6368
6369 infos[i] = new UiccSlotInfo(
6370 slot.isActive(),
6371 slot.isEuicc(),
6372 cardId,
6373 cardState,
6374 slot.getPhoneId(),
Jordan Liuef65d872019-02-14 12:56:40 -08006375 slot.isExtendedApduSupported(),
6376 slot.isRemovable());
Malcolm Chend965c8b2018-02-28 15:00:40 -08006377 }
6378 return infos;
6379 } finally {
6380 Binder.restoreCallingIdentity(identity);
Holly Jiuyu Sun1d957c52018-04-04 13:52:42 -07006381 }
Holly Jiuyu Sun01c47ad2018-01-24 17:56:33 +00006382 }
6383
6384 @Override
6385 public boolean switchSlots(int[] physicalSlots) {
6386 enforceModifyPermission();
Malcolm Chend965c8b2018-02-28 15:00:40 -08006387
6388 final long identity = Binder.clearCallingIdentity();
6389 try {
6390 return (Boolean) sendRequest(CMD_SWITCH_SLOTS, physicalSlots);
6391 } finally {
6392 Binder.restoreCallingIdentity(identity);
6393 }
Holly Jiuyu Sun01c47ad2018-01-24 17:56:33 +00006394 }
Jack Yu4c988042018-02-27 15:30:01 -08006395
6396 @Override
Jordan Liu7de49fa2018-12-06 14:48:49 -08006397 public int getCardIdForDefaultEuicc(int subId, String callingPackage) {
Jordan Liu7de49fa2018-12-06 14:48:49 -08006398 final long identity = Binder.clearCallingIdentity();
6399 try {
6400 return UiccController.getInstance().getCardIdForDefaultEuicc();
6401 } finally {
6402 Binder.restoreCallingIdentity(identity);
6403 }
6404 }
6405
6406 @Override
Jack Yu4c988042018-02-27 15:30:01 -08006407 public void setRadioIndicationUpdateMode(int subId, int filters, int mode) {
6408 enforceModifyPermission();
6409 final Phone phone = getPhone(subId);
6410 if (phone == null) {
6411 loge("setRadioIndicationUpdateMode fails with invalid subId: " + subId);
6412 return;
6413 }
6414
Malcolm Chend965c8b2018-02-28 15:00:40 -08006415 final long identity = Binder.clearCallingIdentity();
6416 try {
6417 phone.setRadioIndicationUpdateMode(filters, mode);
6418 } finally {
6419 Binder.restoreCallingIdentity(identity);
6420 }
Jack Yu4c988042018-02-27 15:30:01 -08006421 }
Pengquan Meng85728fb2018-03-12 16:31:21 -07006422
6423 /**
goneil47ffb6e2018-04-06 15:40:58 -07006424 * A test API to reload the UICC profile.
6425 *
6426 * <p>Requires that the calling app has permission
6427 * {@link android.Manifest.permission#MODIFY_PHONE_STATE MODIFY_PHONE_STATE}.
6428 * @hide
6429 */
6430 @Override
6431 public void refreshUiccProfile(int subId) {
6432 enforceModifyPermission();
6433
6434 final long identity = Binder.clearCallingIdentity();
6435 try {
6436 Phone phone = getPhone(subId);
6437 if (phone == null) {
6438 return;
6439 }
6440 UiccCard uiccCard = phone.getUiccCard();
6441 if (uiccCard == null) {
6442 return;
6443 }
6444 UiccProfile uiccProfile = uiccCard.getUiccProfile();
6445 if (uiccProfile == null) {
6446 return;
6447 }
6448 uiccProfile.refresh();
6449 } finally {
6450 Binder.restoreCallingIdentity(identity);
6451 }
6452 }
6453
6454 /**
Pengquan Meng85728fb2018-03-12 16:31:21 -07006455 * Returns false if the mobile data is disabled by default, otherwise return true.
6456 */
6457 private boolean getDefaultDataEnabled() {
6458 return "true".equalsIgnoreCase(
6459 SystemProperties.get(DEFAULT_MOBILE_DATA_PROPERTY_NAME, "true"));
6460 }
6461
6462 /**
6463 * Returns true if the data roaming is enabled by default, i.e the system property
6464 * of {@link #DEFAULT_DATA_ROAMING_PROPERTY_NAME} is true or the config of
6465 * {@link CarrierConfigManager#KEY_CARRIER_DEFAULT_DATA_ROAMING_ENABLED_BOOL} is true.
6466 */
6467 private boolean getDefaultDataRoamingEnabled(int subId) {
6468 final CarrierConfigManager configMgr = (CarrierConfigManager)
Nazanin Bakhshi2f2cc692018-12-11 15:15:39 -08006469 mApp.getSystemService(Context.CARRIER_CONFIG_SERVICE);
Pengquan Meng85728fb2018-03-12 16:31:21 -07006470 boolean isDataRoamingEnabled = "true".equalsIgnoreCase(
6471 SystemProperties.get(DEFAULT_DATA_ROAMING_PROPERTY_NAME, "false"));
6472 isDataRoamingEnabled |= configMgr.getConfigForSubId(subId).getBoolean(
6473 CarrierConfigManager.KEY_CARRIER_DEFAULT_DATA_ROAMING_ENABLED_BOOL);
6474 return isDataRoamingEnabled;
6475 }
6476
6477 /**
6478 * Returns the default network type for the given {@code subId}, if the default network type is
6479 * not set, return {@link Phone#PREFERRED_NT_MODE}.
6480 */
6481 private int getDefaultNetworkType(int subId) {
6482 return Integer.parseInt(
6483 TelephonyManager.getTelephonyProperty(
6484 mSubscriptionController.getPhoneId(subId),
6485 DEFAULT_NETWORK_MODE_PROPERTY_NAME,
6486 String.valueOf(Phone.PREFERRED_NT_MODE)));
6487 }
fionaxua13278b2018-03-21 00:08:13 -07006488
6489 @Override
6490 public void setCarrierTestOverride(int subId, String mccmnc, String imsi, String iccid, String
6491 gid1, String gid2, String plmn, String spn) {
6492 enforceModifyPermission();
Malcolm Chend965c8b2018-02-28 15:00:40 -08006493
6494 final long identity = Binder.clearCallingIdentity();
6495 try {
6496 final Phone phone = getPhone(subId);
6497 if (phone == null) {
6498 loge("setCarrierTestOverride fails with invalid subId: " + subId);
6499 return;
6500 }
6501 phone.setCarrierTestOverride(mccmnc, imsi, iccid, gid1, gid2, plmn, spn);
6502 } finally {
6503 Binder.restoreCallingIdentity(identity);
fionaxua13278b2018-03-21 00:08:13 -07006504 }
fionaxua13278b2018-03-21 00:08:13 -07006505 }
6506
6507 @Override
6508 public int getCarrierIdListVersion(int subId) {
Brad Ebinger4c460712018-10-01 10:40:55 -07006509 enforceReadPrivilegedPermission("getCarrierIdListVersion");
Malcolm Chend965c8b2018-02-28 15:00:40 -08006510
6511 final long identity = Binder.clearCallingIdentity();
6512 try {
6513 final Phone phone = getPhone(subId);
6514 if (phone == null) {
6515 loge("getCarrierIdListVersion fails with invalid subId: " + subId);
6516 return TelephonyManager.UNKNOWN_CARRIER_ID_LIST_VERSION;
6517 }
6518 return phone.getCarrierIdListVersion();
6519 } finally {
6520 Binder.restoreCallingIdentity(identity);
fionaxua13278b2018-03-21 00:08:13 -07006521 }
fionaxua13278b2018-03-21 00:08:13 -07006522 }
Malcolm Chenf144d942018-08-14 16:00:53 -07006523
6524 @Override
6525 public int getNumberOfModemsWithSimultaneousDataConnections(int subId, String callingPackage) {
6526 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
6527 mApp, subId, callingPackage, "getNumberOfModemsWithSimultaneousDataConnections")) {
6528 return -1;
6529 }
6530
6531 final long identity = Binder.clearCallingIdentity();
6532 try {
6533 return mPhoneConfigurationManager.getNumberOfModemsWithSimultaneousDataConnections();
6534 } finally {
6535 Binder.restoreCallingIdentity(identity);
6536 }
6537 }
Pengquan Meng0c05b502018-09-06 09:59:22 -07006538
6539 @Override
6540 public int getCdmaRoamingMode(int subId) {
6541 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
6542 mApp, subId, "getCdmaRoamingMode");
6543
6544 final long identity = Binder.clearCallingIdentity();
6545 try {
6546 return (int) sendRequest(CMD_GET_CDMA_ROAMING_MODE, null /* argument */, subId);
6547 } finally {
6548 Binder.restoreCallingIdentity(identity);
6549 }
6550 }
6551
6552 @Override
6553 public boolean setCdmaRoamingMode(int subId, int mode) {
6554 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
6555 mApp, subId, "setCdmaRoamingMode");
6556
6557 final long identity = Binder.clearCallingIdentity();
6558 try {
6559 return (boolean) sendRequest(CMD_SET_CDMA_ROAMING_MODE, mode, subId);
6560 } finally {
6561 Binder.restoreCallingIdentity(identity);
6562 }
6563 }
6564
6565 @Override
6566 public boolean setCdmaSubscriptionMode(int subId, int mode) {
6567 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
6568 mApp, subId, "setCdmaSubscriptionMode");
6569
6570 final long identity = Binder.clearCallingIdentity();
6571 try {
6572 return (boolean) sendRequest(CMD_SET_CDMA_SUBSCRIPTION_MODE, mode, subId);
6573 } finally {
6574 Binder.restoreCallingIdentity(identity);
6575 }
6576 }
chen xu7ee67862018-10-30 22:27:10 -07006577
sqian2fff4a32018-11-05 14:18:37 -08006578 private void ensureUserRunning(int userId) {
6579 if (!mUserManager.isUserRunning(userId)) {
6580 throw new IllegalStateException("User " + userId + " does not exist or not running");
6581 }
6582 }
6583
6584 /**
6585 * Returns a list of SMS apps on a given user.
6586 *
6587 * Only the shell user (UID 2000 or 0) can call it.
6588 * Target user must be running.
6589 */
6590 @Override
6591 public String[] getSmsApps(int userId) {
6592 TelephonyPermissions.enforceShellOnly(Binder.getCallingUid(), "getSmsApps");
6593 ensureUserRunning(userId);
6594
6595 final Collection<SmsApplicationData> apps =
6596 SmsApplication.getApplicationCollectionAsUser(mApp, userId);
6597
6598 String[] ret = new String[apps.size()];
6599 int i = 0;
6600 for (SmsApplicationData app : apps) {
6601 ret[i++] = app.mPackageName;
6602 }
6603 return ret;
6604 }
6605
6606 /**
6607 * Returns the default SMS app package name on a given user.
6608 *
6609 * Only the shell user (UID 2000 or 0) can call it.
6610 * Target user must be running.
6611 */
6612 @Override
6613 public String getDefaultSmsApp(int userId) {
6614 TelephonyPermissions.enforceShellOnly(Binder.getCallingUid(), "getDefaultSmsApp");
6615 ensureUserRunning(userId);
6616
6617 final ComponentName cn = SmsApplication.getDefaultSmsApplicationAsUser(mApp,
6618 /* updateIfNeeded= */ true, userId);
6619 return cn == null ? null : cn.getPackageName();
6620 }
6621
6622 /**
6623 * Set a package as the default SMS app on a given user.
6624 *
6625 * Only the shell user (UID 2000 or 0) can call it.
6626 * Target user must be running.
6627 */
6628 @Override
6629 public void setDefaultSmsApp(int userId, String packageName) {
6630 TelephonyPermissions.enforceShellOnly(Binder.getCallingUid(), "setDefaultSmsApp");
6631 ensureUserRunning(userId);
6632
6633 boolean found = false;
6634 for (String pkg : getSmsApps(userId)) {
6635 if (TextUtils.equals(packageName, pkg)) {
6636 found = true;
6637 break;
6638 }
6639 }
6640 if (!found) {
6641 throw new IllegalArgumentException("Package " + packageName + " is not an SMS app");
6642 }
6643
6644 SmsApplication.setDefaultApplicationAsUser(packageName, mApp, userId);
6645 }
6646
chen xu7ee67862018-10-30 22:27:10 -07006647 @Override
sqian65eefe12019-02-22 15:55:18 -08006648 public Map<Integer, List<EmergencyNumber>> getEmergencyNumberList(
sqian04b86072018-11-07 14:02:21 -08006649 String callingPackage) {
sqian03bca152018-12-05 18:48:28 -08006650 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
sqian65eefe12019-02-22 15:55:18 -08006651 mApp, getDefaultSubscription(), callingPackage, "getEmergencyNumberList")) {
sqian03bca152018-12-05 18:48:28 -08006652 throw new SecurityException("Requires READ_PHONE_STATE permission.");
6653 }
6654 final long identity = Binder.clearCallingIdentity();
6655 try {
sqian991b35e2018-12-12 16:48:18 -08006656 Map<Integer, List<EmergencyNumber>> emergencyNumberListInternal = new HashMap<>();
6657 for (Phone phone: PhoneFactory.getPhones()) {
6658 if (phone.getEmergencyNumberTracker() != null
6659 && phone.getEmergencyNumberTracker().getEmergencyNumberList() != null) {
6660 emergencyNumberListInternal.put(
6661 phone.getSubId(),
6662 phone.getEmergencyNumberTracker().getEmergencyNumberList());
6663 }
sqian03bca152018-12-05 18:48:28 -08006664 }
sqian991b35e2018-12-12 16:48:18 -08006665 return emergencyNumberListInternal;
sqian03bca152018-12-05 18:48:28 -08006666 } finally {
6667 Binder.restoreCallingIdentity(identity);
6668 }
sqian04b86072018-11-07 14:02:21 -08006669 }
6670
6671 @Override
sqian65eefe12019-02-22 15:55:18 -08006672 public boolean isEmergencyNumber(String number, boolean exactMatch) {
sqian03bca152018-12-05 18:48:28 -08006673 final Phone defaultPhone = getDefaultPhone();
6674 if (!exactMatch) {
6675 TelephonyPermissions
6676 .enforeceCallingOrSelfReadPrivilegedPhoneStatePermissionOrCarrierPrivilege(
sqian65eefe12019-02-22 15:55:18 -08006677 mApp, defaultPhone.getSubId(), "isEmergencyNumber(Potential)");
sqian03bca152018-12-05 18:48:28 -08006678 }
6679 final long identity = Binder.clearCallingIdentity();
6680 try {
sqian991b35e2018-12-12 16:48:18 -08006681 for (Phone phone: PhoneFactory.getPhones()) {
6682 if (phone.getEmergencyNumberTracker() != null
6683 && phone.getEmergencyNumberTracker() != null) {
6684 if (phone.getEmergencyNumberTracker().isEmergencyNumber(
6685 number, exactMatch)) {
6686 return true;
sqian03bca152018-12-05 18:48:28 -08006687 }
6688 }
sqian03bca152018-12-05 18:48:28 -08006689 }
6690 return false;
6691 } finally {
6692 Binder.restoreCallingIdentity(identity);
6693 }
6694 }
6695
sqian9d4df8b2019-01-15 18:32:07 -08006696 /**
6697 * Update emergency number list for test mode.
6698 */
6699 @Override
6700 public void updateEmergencyNumberListTestMode(int action, EmergencyNumber num) {
6701 TelephonyPermissions.enforceShellOnly(Binder.getCallingUid(),
6702 "updateEmergencyNumberListTestMode");
6703
6704 final long identity = Binder.clearCallingIdentity();
6705 try {
6706 for (Phone phone: PhoneFactory.getPhones()) {
6707 EmergencyNumberTracker tracker = phone.getEmergencyNumberTracker();
6708 if (tracker != null) {
6709 tracker.executeEmergencyNumberTestModeCommand(action, num);
6710 }
6711 }
6712 } finally {
6713 Binder.restoreCallingIdentity(identity);
6714 }
6715 }
6716
6717 /**
6718 * Get the full emergency number list for test mode.
6719 */
6720 @Override
6721 public List<String> getEmergencyNumberListTestMode() {
6722 TelephonyPermissions.enforceShellOnly(Binder.getCallingUid(),
6723 "getEmergencyNumberListTestMode");
6724
6725 final long identity = Binder.clearCallingIdentity();
6726 try {
6727 Set<String> emergencyNumbers = new HashSet<>();
6728 for (Phone phone: PhoneFactory.getPhones()) {
6729 EmergencyNumberTracker tracker = phone.getEmergencyNumberTracker();
6730 if (tracker != null) {
6731 for (EmergencyNumber num : tracker.getEmergencyNumberList()) {
6732 emergencyNumbers.add(num.getNumber());
6733 }
6734 }
6735 }
6736 return new ArrayList<>(emergencyNumbers);
6737 } finally {
6738 Binder.restoreCallingIdentity(identity);
6739 }
6740 }
6741
sqian04b86072018-11-07 14:02:21 -08006742 @Override
chen xu7ee67862018-10-30 22:27:10 -07006743 public List<String> getCertsFromCarrierPrivilegeAccessRules(int subId) {
6744 enforceReadPrivilegedPermission("getCertsFromCarrierPrivilegeAccessRules");
6745 Phone phone = getPhone(subId);
6746 if (phone == null) {
6747 return null;
6748 }
6749 final long identity = Binder.clearCallingIdentity();
6750 try {
6751 UiccProfile profile = UiccController.getInstance()
6752 .getUiccProfileForPhone(phone.getPhoneId());
6753 if (profile != null) {
6754 return profile.getCertsFromCarrierPrivilegeAccessRules();
6755 }
6756 } finally {
6757 Binder.restoreCallingIdentity(identity);
6758 }
6759 return null;
6760 }
Malcolm Chen509b5b72019-01-15 20:22:16 -08006761
6762 /**
6763 * Enable or disable a modem stack.
6764 */
6765 @Override
6766 public boolean enableModemForSlot(int slotIndex, boolean enable) {
6767 enforceModifyPermission();
6768
6769 final long identity = Binder.clearCallingIdentity();
6770 try {
6771 Phone phone = PhoneFactory.getPhone(slotIndex);
6772 if (phone == null) {
6773 return false;
6774 } else {
6775 return (Boolean) sendRequest(CMD_REQUEST_ENABLE_MODEM, enable, phone, null);
6776 }
6777 } finally {
6778 Binder.restoreCallingIdentity(identity);
6779 }
6780 }
Micheled3107c52018-12-21 15:00:11 -08006781
Malcolm Chen33f55e12019-03-27 18:34:05 -07006782 /**
6783 * Whether a modem stack is enabled or not.
6784 */
6785 @Override
6786 public boolean isModemEnabledForSlot(int slotIndex, String callingPackage) {
6787 Phone phone = PhoneFactory.getPhone(slotIndex);
6788 if (phone == null) return false;
6789
6790 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
6791 mApp, phone.getSubId(), callingPackage, "isModemEnabledForSlot")) {
6792 throw new SecurityException("Requires READ_PHONE_STATE permission.");
6793 }
6794
6795 final long identity = Binder.clearCallingIdentity();
6796 try {
6797 return PhoneConfigurationManager.getInstance().getPhoneStatus(phone);
6798 } finally {
6799 Binder.restoreCallingIdentity(identity);
6800 }
6801 }
6802
Micheled3107c52018-12-21 15:00:11 -08006803 @Override
Michele2fb0f222019-03-19 14:58:42 -07006804 public void setMultiSimCarrierRestriction(boolean isMultiSimCarrierRestricted) {
Micheled3107c52018-12-21 15:00:11 -08006805 enforceModifyPermission();
6806
6807 final long identity = Binder.clearCallingIdentity();
6808 try {
6809 mTelephonySharedPreferences.edit()
Michele2fb0f222019-03-19 14:58:42 -07006810 .putBoolean(PREF_MULTI_SIM_RESTRICTED, isMultiSimCarrierRestricted)
Micheled3107c52018-12-21 15:00:11 -08006811 .commit();
6812 } finally {
6813 Binder.restoreCallingIdentity(identity);
6814 }
6815 }
6816
6817 @Override
Michele2fb0f222019-03-19 14:58:42 -07006818 @TelephonyManager.IsMultiSimSupportedResult
6819 public int isMultiSimSupported(String callingPackage) {
Michelebcb01bc2019-02-04 11:36:23 -08006820 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(mApp,
Michele2fb0f222019-03-19 14:58:42 -07006821 getDefaultPhone().getSubId(), callingPackage, "isMultiSimSupported")) {
6822 return TelephonyManager.MULTISIM_NOT_SUPPORTED_BY_HARDWARE;
Michelebcb01bc2019-02-04 11:36:23 -08006823 }
Micheled3107c52018-12-21 15:00:11 -08006824
6825 final long identity = Binder.clearCallingIdentity();
6826 try {
Michele2fb0f222019-03-19 14:58:42 -07006827 return isMultiSimSupportedInternal();
Micheled3107c52018-12-21 15:00:11 -08006828 } finally {
6829 Binder.restoreCallingIdentity(identity);
6830 }
6831 }
Nazanin Bakhshiadbfdcc2019-01-30 14:59:50 -08006832
Michele2fb0f222019-03-19 14:58:42 -07006833 @TelephonyManager.IsMultiSimSupportedResult
6834 private int isMultiSimSupportedInternal() {
Michele30b57b22019-03-01 12:01:14 -08006835 // If the device has less than 2 SIM cards, indicate that multisim is restricted.
6836 int numPhysicalSlots = UiccController.getInstance().getUiccSlots().length;
6837 if (numPhysicalSlots < 2) {
Michele2fb0f222019-03-19 14:58:42 -07006838 loge("isMultiSimSupportedInternal: requires at least 2 cards");
6839 return TelephonyManager.MULTISIM_NOT_SUPPORTED_BY_HARDWARE;
Michele30b57b22019-03-01 12:01:14 -08006840 }
6841 // Check if the hardware supports multisim functionality. If usage of multisim is not
6842 // supported by the modem, indicate that it is restricted.
6843 PhoneCapability staticCapability =
6844 mPhoneConfigurationManager.getStaticPhoneCapability();
6845 if (staticCapability == null) {
Michele2fb0f222019-03-19 14:58:42 -07006846 loge("isMultiSimSupportedInternal: no static configuration available");
6847 return TelephonyManager.MULTISIM_NOT_SUPPORTED_BY_HARDWARE;
Michele30b57b22019-03-01 12:01:14 -08006848 }
6849 if (staticCapability.logicalModemList.size() < 2) {
Michele2fb0f222019-03-19 14:58:42 -07006850 loge("isMultiSimSupportedInternal: maximum number of modem is < 2");
6851 return TelephonyManager.MULTISIM_NOT_SUPPORTED_BY_HARDWARE;
Michele30b57b22019-03-01 12:01:14 -08006852 }
6853 // Check if support of multiple SIMs is restricted by carrier
6854 if (mTelephonySharedPreferences.getBoolean(PREF_MULTI_SIM_RESTRICTED, false)) {
Michele2fb0f222019-03-19 14:58:42 -07006855 return TelephonyManager.MULTISIM_NOT_SUPPORTED_BY_CARRIER;
Michele30b57b22019-03-01 12:01:14 -08006856 }
6857
Michele2fb0f222019-03-19 14:58:42 -07006858 return TelephonyManager.MULTISIM_ALLOWED;
Michele30b57b22019-03-01 12:01:14 -08006859 }
6860
Nazanin Bakhshiadbfdcc2019-01-30 14:59:50 -08006861 /**
6862 * Switch configs to enable multi-sim or switch back to single-sim
Nazanin Bakhshi814dc222019-03-01 11:56:08 -08006863 * Note: Switch from multi-sim to single-sim is only possible with MODIFY_PHONE_STATE
6864 * permission, but the other way around is possible with either MODIFY_PHONE_STATE
6865 * or carrier privileges
Nazanin Bakhshiadbfdcc2019-01-30 14:59:50 -08006866 * @param numOfSims number of active sims we want to switch to
6867 */
6868 @Override
6869 public void switchMultiSimConfig(int numOfSims) {
Nazanin Bakhshi814dc222019-03-01 11:56:08 -08006870 if (numOfSims == 1) {
6871 enforceModifyPermission();
6872 } else {
6873 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
6874 mApp, SubscriptionManager.DEFAULT_SUBSCRIPTION_ID, "switchMultiSimConfig");
6875 }
Nazanin Bakhshiadbfdcc2019-01-30 14:59:50 -08006876 final long identity = Binder.clearCallingIdentity();
Michele30b57b22019-03-01 12:01:14 -08006877
Nazanin Bakhshiadbfdcc2019-01-30 14:59:50 -08006878 try {
Michele30b57b22019-03-01 12:01:14 -08006879 //only proceed if multi-sim is not restricted
Michele2fb0f222019-03-19 14:58:42 -07006880 if (isMultiSimSupportedInternal() != TelephonyManager.MULTISIM_ALLOWED) {
Michele30b57b22019-03-01 12:01:14 -08006881 loge("switchMultiSimConfig not possible. It is restricted or not supported.");
6882 return;
6883 }
Nazanin Bakhshiadbfdcc2019-01-30 14:59:50 -08006884 mPhoneConfigurationManager.switchMultiSimConfig(numOfSims);
6885 } finally {
6886 Binder.restoreCallingIdentity(identity);
6887 }
6888 }
6889
6890 /**
Nazanin Bakhshicd417af2019-01-30 10:52:09 -08006891 * Get whether reboot is required or not after making changes to modem configurations.
Nazanin Bakhshi4a68f5f2019-02-08 14:31:19 -08006892 * Return value defaults to true
Nazanin Bakhshicd417af2019-01-30 10:52:09 -08006893 */
6894 @Override
6895 public boolean isRebootRequiredForModemConfigChange() {
6896 enforceReadPrivilegedPermission("isRebootRequiredForModemConfigChange");
6897 final long identity = Binder.clearCallingIdentity();
6898 try {
6899 return mPhoneConfigurationManager.isRebootRequiredForModemConfigChange();
6900 } finally {
6901 Binder.restoreCallingIdentity(identity);
6902 }
6903 }
6904
Pengquan Meng92d253b2019-02-06 11:12:53 -08006905 private void updateModemStateMetrics() {
6906 TelephonyMetrics metrics = TelephonyMetrics.getInstance();
6907 // TODO: check the state for each modem if the api is ready.
6908 metrics.updateEnabledModemBitmap((1 << TelephonyManager.from(mApp).getPhoneCount()) - 1);
6909 }
6910
Pengquan Meng3aceaec2019-01-23 11:16:29 -08006911 @Override
6912 public int[] getSlotsMapping() {
6913 enforceReadPrivilegedPermission("getSlotsMapping");
6914
6915 final long identity = Binder.clearCallingIdentity();
6916 try {
6917 int phoneCount = TelephonyManager.getDefault().getPhoneCount();
6918 // All logical slots should have a mapping to a physical slot.
6919 int[] logicalSlotsMapping = new int[phoneCount];
6920 UiccSlotInfo[] slotInfos = getUiccSlotsInfo();
6921 for (int i = 0; i < slotInfos.length; i++) {
6922 if (SubscriptionManager.isValidPhoneId(slotInfos[i].getLogicalSlotIdx())) {
6923 logicalSlotsMapping[slotInfos[i].getLogicalSlotIdx()] = i;
6924 }
6925 }
6926 return logicalSlotsMapping;
6927 } finally {
6928 Binder.restoreCallingIdentity(identity);
6929 }
6930 }
Nathan Harold48d6fd52019-02-06 19:01:40 -08006931
6932 /**
6933 * Get the IRadio HAL Version
6934 */
6935 @Override
6936 public int getRadioHalVersion() {
6937 Phone phone = getDefaultPhone();
6938 if (phone == null) return -1;
6939 HalVersion hv = phone.getHalVersion();
6940 if (hv.equals(HalVersion.UNKNOWN)) return -1;
6941 return hv.major * 100 + hv.minor;
6942 }
Malcolm Chen460810d2019-04-10 18:25:07 -07006943
6944 /**
Malcolm Chen29739692019-04-18 13:51:02 -07006945 * Return whether data is enabled for certain APN type. This will tell if framework will accept
6946 * corresponding network requests on a subId.
6947 *
6948 * Data is enabled if:
Malcolm Chen460810d2019-04-10 18:25:07 -07006949 * 1) user data is turned on, or
Malcolm Chen29739692019-04-18 13:51:02 -07006950 * 2) APN is un-metered for this subscription, or
6951 * 3) APN type is whitelisted. E.g. MMS is whitelisted if
6952 * {@link SubscriptionManager#setAlwaysAllowMmsData} is turned on.
6953 *
6954 * @return whether data is allowed for a apn type.
6955 *
6956 * @hide
Malcolm Chen460810d2019-04-10 18:25:07 -07006957 */
6958 @Override
Malcolm Chen29739692019-04-18 13:51:02 -07006959 public boolean isDataEnabledForApn(int apnType, int subId, String callingPackage) {
Malcolm Chen460810d2019-04-10 18:25:07 -07006960 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
Malcolm Chen29739692019-04-18 13:51:02 -07006961 mApp, subId, callingPackage, "isDataEnabledForApn")) {
6962 throw new SecurityException("Needs READ_PHONE_STATE for isDataEnabledForApn");
Malcolm Chen460810d2019-04-10 18:25:07 -07006963 }
6964
6965 // Now that all security checks passes, perform the operation as ourselves.
6966 final long identity = Binder.clearCallingIdentity();
6967 try {
6968 Phone phone = getPhone(subId);
6969 if (phone == null) return false;
6970
6971 boolean isMetered = ApnSettingUtils.isMeteredApnType(ApnSetting.getApnTypeString(
Malcolm Chen29739692019-04-18 13:51:02 -07006972 apnType), phone);
6973 return !isMetered || phone.getDataEnabledSettings().isDataEnabled(apnType);
6974 } finally {
6975 Binder.restoreCallingIdentity(identity);
6976 }
6977 }
6978
6979 @Override
6980 public boolean isApnMetered(int apnType, int subId) {
6981 enforceReadPrivilegedPermission("isApnMetered");
6982
6983 // Now that all security checks passes, perform the operation as ourselves.
6984 final long identity = Binder.clearCallingIdentity();
6985 try {
6986 Phone phone = getPhone(subId);
6987 if (phone == null) return true; // By default return true.
6988
6989 return ApnSettingUtils.isMeteredApnType(ApnSetting.getApnTypeString(
6990 apnType), phone);
Malcolm Chen460810d2019-04-10 18:25:07 -07006991 } finally {
6992 Binder.restoreCallingIdentity(identity);
6993 }
6994 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07006995}