blob: 435cd9601b47a65783a66a86f6822346c7805b01 [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 Gunnf70ed162019-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;
Malcolm Chen6ca97372019-07-01 16:28:21 -070047import android.os.ParcelUuid;
Tyler Gunn65d45c22017-06-05 11:22:26 -070048import android.os.PersistableBundle;
Brad Ebinger5f64b052017-12-14 14:26:15 -080049import android.os.RemoteException;
Adam Lesinski903a54c2016-04-11 14:49:52 -070050import android.os.ResultReceiver;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070051import android.os.ServiceManager;
Brad Ebingerdac2f002018-04-03 15:17:52 -070052import android.os.ShellCallback;
Pengquan Meng85728fb2018-03-12 16:31:21 -070053import android.os.SystemProperties;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070054import android.os.UserHandle;
Stuart Scott981d8582015-04-21 14:09:50 -070055import android.os.UserManager;
Sooraj Sasindran9a909312016-07-18 11:57:25 -070056import android.os.WorkSource;
Derek Tan97ebb422014-09-05 16:55:38 -070057import android.preference.PreferenceManager;
Naina Nalluri1264a9f2019-09-17 14:10:30 -070058import android.provider.DeviceConfig;
Ihab Awadf2177b72013-11-25 13:33:23 -080059import android.provider.Settings;
Amit Mahajan7dbbd822019-03-13 17:33:47 -070060import android.provider.Telephony;
Santos Cordon7a1885b2015-02-03 11:15:19 -080061import android.telecom.PhoneAccount;
Nancy Chen31f9ba12016-01-06 11:42:12 -080062import android.telecom.PhoneAccountHandle;
Andrew Lee9431b832015-03-09 18:46:45 -070063import android.telecom.TelecomManager;
Junda Liu12f7d802015-05-01 12:06:44 -070064import android.telephony.CarrierConfigManager;
Michele Berionne482f8202018-11-27 18:57:59 -080065import android.telephony.CarrierRestrictionRules;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070066import android.telephony.CellInfo;
Nathan Haroldf180aac2018-06-01 18:43:55 -070067import android.telephony.CellInfoGsm;
68import android.telephony.CellInfoWcdma;
Nathan Harold3ff88932018-08-14 10:19:49 -070069import android.telephony.CellLocation;
Sooraj Sasindran9a909312016-07-18 11:57:25 -070070import android.telephony.ClientRequestStats;
Nathan Haroldfa8da0f2018-09-27 18:51:29 -070071import android.telephony.ICellInfoCallback;
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -070072import android.telephony.IccOpenLogicalChannelResponse;
Hall Liu1aa510f2017-11-22 17:40:08 -080073import android.telephony.LocationAccessPolicy;
Ta-wei Yen87c49842016-05-13 21:19:52 -070074import android.telephony.ModemActivityInfo;
Jake Hambye994d462014-02-03 13:10:13 -080075import android.telephony.NeighboringCellInfo;
yinxu504e1392017-04-12 16:03:22 -070076import android.telephony.NetworkScanRequest;
Michele4245e952019-02-04 11:36:23 -080077import android.telephony.PhoneCapability;
Hall Liud892bec2018-11-30 14:51:45 -080078import android.telephony.PhoneNumberRange;
Wink Saville5d475dd2014-10-17 15:00:58 -070079import android.telephony.RadioAccessFamily;
Hall Liub2ac8ef2019-02-28 15:56:23 -080080import android.telephony.RadioAccessSpecifier;
Tyler Gunn65d45c22017-06-05 11:22:26 -070081import android.telephony.Rlog;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070082import android.telephony.ServiceState;
Nathan Harold46b42aa2017-03-10 19:38:22 -080083import android.telephony.SignalStrength;
Wink Saville0f3b5fc2014-11-11 08:40:49 -080084import android.telephony.SubscriptionInfo;
Jeff Sharkey85190e62014-12-05 09:40:12 -080085import android.telephony.SubscriptionManager;
Sanket Padawe99ef1e32016-05-18 16:12:33 -070086import android.telephony.TelephonyHistogram;
Ta-wei Yenb6929602016-05-24 15:48:27 -070087import android.telephony.TelephonyManager;
Hall Liub2ac8ef2019-02-28 15:56:23 -080088import android.telephony.TelephonyScanManager;
Jordan Liu5aa07002018-12-18 15:44:48 -080089import android.telephony.UiccCardInfo;
Holly Jiuyu Sun01c47ad2018-01-24 17:56:33 +000090import android.telephony.UiccSlotInfo;
Tyler Gunn65d45c22017-06-05 11:22:26 -070091import android.telephony.UssdResponse;
Ta-wei Yenb6929602016-05-24 15:48:27 -070092import android.telephony.VisualVoicemailSmsFilterSettings;
Nathan Harold3ff88932018-08-14 10:19:49 -070093import android.telephony.cdma.CdmaCellLocation;
Jack Yub5d8f642018-11-26 11:20:48 -080094import android.telephony.data.ApnSetting;
Jack Yu41407ee2019-05-13 16:54:09 -070095import android.telephony.data.ApnSetting.ApnType;
Jack Yub5d8f642018-11-26 11:20:48 -080096import android.telephony.emergency.EmergencyNumber;
Nathan Harold3ff88932018-08-14 10:19:49 -070097import android.telephony.gsm.GsmCellLocation;
Brad Ebinger1c8542e2019-01-14 13:43:14 -080098import android.telephony.ims.ProvisioningManager;
Brad Ebinger35c841c2018-10-01 10:40:55 -070099import android.telephony.ims.aidl.IImsCapabilityCallback;
Brad Ebinger22bc3e42018-01-16 09:39:35 -0800100import android.telephony.ims.aidl.IImsConfig;
Brad Ebingerdf5b4f02018-10-31 11:24:17 -0700101import android.telephony.ims.aidl.IImsConfigCallback;
Brad Ebinger22bc3e42018-01-16 09:39:35 -0800102import android.telephony.ims.aidl.IImsMmTelFeature;
103import android.telephony.ims.aidl.IImsRcsFeature;
104import android.telephony.ims.aidl.IImsRegistration;
Brad Ebinger35c841c2018-10-01 10:40:55 -0700105import android.telephony.ims.aidl.IImsRegistrationCallback;
Brad Ebinger1c8542e2019-01-14 13:43:14 -0800106import android.telephony.ims.feature.MmTelFeature;
107import android.telephony.ims.stub.ImsConfigImplBase;
Brad Ebinger1f2b5082018-02-08 16:11:32 -0800108import android.telephony.ims.stub.ImsRegistrationImplBase;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700109import android.text.TextUtils;
Jeff Sharkey85190e62014-12-05 09:40:12 -0800110import android.util.ArraySet;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700111import android.util.Log;
Jake Hambye994d462014-02-03 13:10:13 -0800112import android.util.Pair;
Jeff Sharkey85190e62014-12-05 09:40:12 -0800113import android.util.Slog;
Ta-wei Yen30a69c82016-12-27 14:52:32 -0800114
Brad Ebinger35c841c2018-10-01 10:40:55 -0700115import com.android.ims.ImsException;
Andrew Lee312e8172014-10-23 17:01:36 -0700116import com.android.ims.ImsManager;
Brad Ebinger34bef922017-11-09 10:27:08 -0800117import com.android.ims.internal.IImsServiceFeatureCallback;
Shishir Agrawal566b7612013-10-28 14:41:00 -0700118import com.android.internal.telephony.CallManager;
Tyler Gunn52dcf772017-04-26 11:30:31 -0700119import com.android.internal.telephony.CallStateException;
pkanwar79ec0542017-07-31 14:10:01 -0700120import com.android.internal.telephony.CarrierInfoManager;
chen xu651eec72018-11-11 19:03:44 -0800121import com.android.internal.telephony.CarrierResolver;
Shishir Agrawal302c8692015-06-19 13:49:39 -0700122import com.android.internal.telephony.CellNetworkScanResult;
Shishir Agrawal566b7612013-10-28 14:41:00 -0700123import com.android.internal.telephony.CommandException;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700124import com.android.internal.telephony.DefaultPhoneNotifier;
Nathan Harold48d6fd52019-02-06 19:01:40 -0800125import com.android.internal.telephony.HalVersion;
Brad Ebingera63db5f2019-04-23 16:31:13 -0700126import com.android.internal.telephony.IIntegerConsumer;
Hall Liud892bec2018-11-30 14:51:45 -0800127import com.android.internal.telephony.INumberVerificationCallback;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700128import com.android.internal.telephony.ITelephony;
Jake Hambye994d462014-02-03 13:10:13 -0800129import com.android.internal.telephony.IccCard;
Jack Yu5f7092c2018-04-13 14:05:37 -0700130import com.android.internal.telephony.LocaleTracker;
Narayan Kamath1c496c22015-04-16 14:40:19 +0100131import com.android.internal.telephony.MccTable;
yinxub1bed742017-04-17 11:45:04 -0700132import com.android.internal.telephony.NetworkScanRequestTracker;
Shishir Agrawal302c8692015-06-19 13:49:39 -0700133import com.android.internal.telephony.OperatorInfo;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700134import com.android.internal.telephony.Phone;
Malcolm Chen2c63d402018-08-14 16:00:53 -0700135import com.android.internal.telephony.PhoneConfigurationManager;
Nathan Harolda667c152016-12-14 11:27:20 -0800136import com.android.internal.telephony.PhoneConstantConversions;
Ta-wei Yen87c49842016-05-13 21:19:52 -0700137import com.android.internal.telephony.PhoneConstants;
Wink Saville36469e72014-06-11 15:17:00 -0700138import com.android.internal.telephony.PhoneFactory;
Wink Saville5d475dd2014-10-17 15:00:58 -0700139import com.android.internal.telephony.ProxyController;
Sanket Padawe99ef1e32016-05-18 16:12:33 -0700140import com.android.internal.telephony.RIL;
Svet Ganovb320e182015-04-16 12:30:10 -0700141import com.android.internal.telephony.RILConstants;
Jack Yu5f7092c2018-04-13 14:05:37 -0700142import com.android.internal.telephony.ServiceStateTracker;
Makoto Onukida3bf792018-09-18 16:06:29 -0700143import com.android.internal.telephony.SmsApplication;
144import com.android.internal.telephony.SmsApplication.SmsApplicationData;
Amit Mahajandccb3f12019-05-13 13:48:32 -0700145import com.android.internal.telephony.SmsController;
Brad Ebingera63db5f2019-04-23 16:31:13 -0700146import com.android.internal.telephony.SmsPermissions;
Wink Savilleac1bdfd2014-11-20 23:04:44 -0800147import com.android.internal.telephony.SubscriptionController;
Jeff Davidson7e17e312018-02-13 18:17:36 -0800148import com.android.internal.telephony.TelephonyPermissions;
Malcolm Chendc8c10e2019-04-10 18:25:07 -0700149import com.android.internal.telephony.dataconnection.ApnSettingUtils;
sqianf4ca7ed2019-01-15 18:32:07 -0800150import com.android.internal.telephony.emergency.EmergencyNumberTracker;
Derek Tan740e1672017-06-27 14:56:27 -0700151import com.android.internal.telephony.euicc.EuiccConnector;
Brad Ebinger9c0eb502019-01-23 15:06:19 -0800152import com.android.internal.telephony.ims.ImsResolver;
Pengquan Meng6c2dc9f2019-02-06 11:12:53 -0800153import com.android.internal.telephony.metrics.TelephonyMetrics;
Shishir Agrawal566b7612013-10-28 14:41:00 -0700154import com.android.internal.telephony.uicc.IccIoResult;
155import com.android.internal.telephony.uicc.IccUtils;
Nathan Haroldb3014052017-01-25 15:57:32 -0800156import com.android.internal.telephony.uicc.SIMRecords;
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700157import com.android.internal.telephony.uicc.UiccCard;
Nathan Haroldb3014052017-01-25 15:57:32 -0800158import com.android.internal.telephony.uicc.UiccCardApplication;
Shishir Agrawal566b7612013-10-28 14:41:00 -0700159import com.android.internal.telephony.uicc.UiccController;
Jeff Davidson7e17e312018-02-13 18:17:36 -0800160import com.android.internal.telephony.uicc.UiccProfile;
Holly Jiuyu Sun01c47ad2018-01-24 17:56:33 +0000161import com.android.internal.telephony.uicc.UiccSlot;
fionaxu7ed723d2017-05-30 18:58:54 -0700162import com.android.internal.telephony.util.VoicemailNotificationSettingsUtil;
Jake Hambye994d462014-02-03 13:10:13 -0800163import com.android.internal.util.HexDump;
Brad Ebingera63db5f2019-04-23 16:31:13 -0700164import com.android.phone.settings.PickSmsSubscriptionActivity;
Ta-wei Yenc9df0432017-04-17 17:09:07 -0700165import com.android.phone.vvm.PhoneAccountHandleConverter;
Ta-wei Yen527a9c02017-01-06 15:29:25 -0800166import com.android.phone.vvm.RemoteVvmTaskManager;
Ta-wei Yenc9df0432017-04-17 17:09:07 -0700167import com.android.phone.vvm.VisualVoicemailSettingsUtil;
Ta-wei Yenc8905312017-03-28 11:14:45 -0700168import com.android.phone.vvm.VisualVoicemailSmsFilterConfig;
Ta-wei Yen30a69c82016-12-27 14:52:32 -0800169
Ta-wei Yenc236d6b2016-06-21 13:33:12 -0700170import java.io.FileDescriptor;
171import java.io.PrintWriter;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700172import java.util.ArrayList;
Jeff Sharkey85190e62014-12-05 09:40:12 -0800173import java.util.Arrays;
Makoto Onukida3bf792018-09-18 16:06:29 -0700174import java.util.Collection;
sqian11b7a0e2018-12-05 18:48:28 -0800175import java.util.HashMap;
sqianf4ca7ed2019-01-15 18:32:07 -0800176import java.util.HashSet;
Jake Hambye994d462014-02-03 13:10:13 -0800177import java.util.List;
Narayan Kamath1c496c22015-04-16 14:40:19 +0100178import java.util.Locale;
Jeff Sharkey85190e62014-12-05 09:40:12 -0800179import java.util.Map;
Nazanin Bakhshif71371d2019-04-29 17:29:44 -0700180import java.util.NoSuchElementException;
sqianf4ca7ed2019-01-15 18:32:07 -0800181import java.util.Set;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700182
183/**
184 * Implementation of the ITelephony interface.
185 */
Santos Cordon117fee72014-05-16 17:56:12 -0700186public class PhoneInterfaceManager extends ITelephony.Stub {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700187 private static final String LOG_TAG = "PhoneInterfaceManager";
188 private static final boolean DBG = (PhoneGlobals.DBG_LEVEL >= 2);
189 private static final boolean DBG_LOC = false;
Jeff Sharkey85190e62014-12-05 09:40:12 -0800190 private static final boolean DBG_MERGE = false;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700191
192 // Message codes used with mMainThreadHandler
193 private static final int CMD_HANDLE_PIN_MMI = 1;
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700194 private static final int CMD_TRANSMIT_APDU_LOGICAL_CHANNEL = 7;
195 private static final int EVENT_TRANSMIT_APDU_LOGICAL_CHANNEL_DONE = 8;
Shishir Agrawal566b7612013-10-28 14:41:00 -0700196 private static final int CMD_OPEN_CHANNEL = 9;
197 private static final int EVENT_OPEN_CHANNEL_DONE = 10;
198 private static final int CMD_CLOSE_CHANNEL = 11;
199 private static final int EVENT_CLOSE_CHANNEL_DONE = 12;
Jake Hambye994d462014-02-03 13:10:13 -0800200 private static final int CMD_NV_READ_ITEM = 13;
201 private static final int EVENT_NV_READ_ITEM_DONE = 14;
202 private static final int CMD_NV_WRITE_ITEM = 15;
203 private static final int EVENT_NV_WRITE_ITEM_DONE = 16;
204 private static final int CMD_NV_WRITE_CDMA_PRL = 17;
205 private static final int EVENT_NV_WRITE_CDMA_PRL_DONE = 18;
chen xu6dac5ab2018-10-26 17:39:23 -0700206 private static final int CMD_RESET_MODEM_CONFIG = 19;
207 private static final int EVENT_RESET_MODEM_CONFIG_DONE = 20;
Jake Hamby7c27be32014-03-03 13:25:59 -0800208 private static final int CMD_GET_PREFERRED_NETWORK_TYPE = 21;
209 private static final int EVENT_GET_PREFERRED_NETWORK_TYPE_DONE = 22;
210 private static final int CMD_SET_PREFERRED_NETWORK_TYPE = 23;
211 private static final int EVENT_SET_PREFERRED_NETWORK_TYPE_DONE = 24;
Sailesh Nepal35b59452014-03-06 09:26:56 -0800212 private static final int CMD_SEND_ENVELOPE = 25;
213 private static final int EVENT_SEND_ENVELOPE_DONE = 26;
Shuo Qian850e4d6a2018-04-25 21:02:08 +0000214 private static final int CMD_INVOKE_OEM_RIL_REQUEST_RAW = 27;
215 private static final int EVENT_INVOKE_OEM_RIL_REQUEST_RAW_DONE = 28;
Derek Tan6b088ee2014-09-05 14:15:18 -0700216 private static final int CMD_TRANSMIT_APDU_BASIC_CHANNEL = 29;
217 private static final int EVENT_TRANSMIT_APDU_BASIC_CHANNEL_DONE = 30;
218 private static final int CMD_EXCHANGE_SIM_IO = 31;
219 private static final int EVENT_EXCHANGE_SIM_IO_DONE = 32;
Shishir Agrawal76d5da92014-11-09 16:17:25 -0800220 private static final int CMD_SET_VOICEMAIL_NUMBER = 33;
221 private static final int EVENT_SET_VOICEMAIL_NUMBER_DONE = 34;
Stuart Scott54788802015-03-30 13:18:01 -0700222 private static final int CMD_SET_NETWORK_SELECTION_MODE_AUTOMATIC = 35;
223 private static final int EVENT_SET_NETWORK_SELECTION_MODE_AUTOMATIC_DONE = 36;
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -0700224 private static final int CMD_GET_MODEM_ACTIVITY_INFO = 37;
225 private static final int EVENT_GET_MODEM_ACTIVITY_INFO_DONE = 38;
Shishir Agrawal302c8692015-06-19 13:49:39 -0700226 private static final int CMD_PERFORM_NETWORK_SCAN = 39;
227 private static final int EVENT_PERFORM_NETWORK_SCAN_DONE = 40;
228 private static final int CMD_SET_NETWORK_SELECTION_MODE_MANUAL = 41;
229 private static final int EVENT_SET_NETWORK_SELECTION_MODE_MANUAL_DONE = 42;
Meng Wang1a7c35a2016-05-05 20:56:15 -0700230 private static final int CMD_SET_ALLOWED_CARRIERS = 43;
231 private static final int EVENT_SET_ALLOWED_CARRIERS_DONE = 44;
232 private static final int CMD_GET_ALLOWED_CARRIERS = 45;
233 private static final int EVENT_GET_ALLOWED_CARRIERS_DONE = 46;
pkanwar32d516d2016-10-14 19:37:38 -0700234 private static final int CMD_HANDLE_USSD_REQUEST = 47;
Nathan Haroldb3014052017-01-25 15:57:32 -0800235 private static final int CMD_GET_FORBIDDEN_PLMNS = 48;
236 private static final int EVENT_GET_FORBIDDEN_PLMNS_DONE = 49;
Holly Jiuyu Sun01c47ad2018-01-24 17:56:33 +0000237 private static final int CMD_SWITCH_SLOTS = 50;
238 private static final int EVENT_SWITCH_SLOTS_DONE = 51;
Pengquan Menga1bb6272018-09-06 09:59:22 -0700239 private static final int CMD_GET_NETWORK_SELECTION_MODE = 52;
240 private static final int EVENT_GET_NETWORK_SELECTION_MODE_DONE = 53;
241 private static final int CMD_GET_CDMA_ROAMING_MODE = 54;
242 private static final int EVENT_GET_CDMA_ROAMING_MODE_DONE = 55;
243 private static final int CMD_SET_CDMA_ROAMING_MODE = 56;
244 private static final int EVENT_SET_CDMA_ROAMING_MODE_DONE = 57;
245 private static final int CMD_SET_CDMA_SUBSCRIPTION_MODE = 58;
246 private static final int EVENT_SET_CDMA_SUBSCRIPTION_MODE_DONE = 59;
Nathan Harold3ff88932018-08-14 10:19:49 -0700247 private static final int CMD_GET_ALL_CELL_INFO = 60;
248 private static final int EVENT_GET_ALL_CELL_INFO_DONE = 61;
249 private static final int CMD_GET_CELL_LOCATION = 62;
250 private static final int EVENT_GET_CELL_LOCATION_DONE = 63;
chen xu6dac5ab2018-10-26 17:39:23 -0700251 private static final int CMD_MODEM_REBOOT = 64;
252 private static final int EVENT_CMD_MODEM_REBOOT_DONE = 65;
Nathan Haroldfa8da0f2018-09-27 18:51:29 -0700253 private static final int CMD_REQUEST_CELL_INFO_UPDATE = 66;
254 private static final int EVENT_REQUEST_CELL_INFO_UPDATE_DONE = 67;
Malcolm Chen8e4ed912019-01-15 20:22:16 -0800255 private static final int CMD_REQUEST_ENABLE_MODEM = 68;
256 private static final int EVENT_ENABLE_MODEM_DONE = 69;
Nazanin Bakhshif71371d2019-04-29 17:29:44 -0700257 private static final int CMD_GET_MODEM_STATUS = 70;
258 private static final int EVENT_GET_MODEM_STATUS_DONE = 71;
Naina Nalluri1264a9f2019-09-17 14:10:30 -0700259 private static final int CMD_ERASE_MODEM_CONFIG = 72;
260 private static final int EVENT_ERASE_MODEM_CONFIG_DONE = 73;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700261
Holly Jiuyu Sun1cc2d552018-01-26 15:51:16 -0800262 // Parameters of select command.
263 private static final int SELECT_COMMAND = 0xA4;
264 private static final int SELECT_P1 = 0x04;
265 private static final int SELECT_P2 = 0;
266 private static final int SELECT_P3 = 0x10;
267
Pengquan Meng85728fb2018-03-12 16:31:21 -0700268 private static final String DEFAULT_NETWORK_MODE_PROPERTY_NAME = "ro.telephony.default_network";
269 private static final String DEFAULT_DATA_ROAMING_PROPERTY_NAME = "ro.com.android.dataroaming";
270 private static final String DEFAULT_MOBILE_DATA_PROPERTY_NAME = "ro.com.android.mobiledata";
271
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700272 /** The singleton instance. */
273 private static PhoneInterfaceManager sInstance;
274
Wink Saville3ab207e2014-11-20 13:07:20 -0800275 private PhoneGlobals mApp;
Wink Saville3ab207e2014-11-20 13:07:20 -0800276 private CallManager mCM;
Stuart Scott981d8582015-04-21 14:09:50 -0700277 private UserManager mUserManager;
Wink Saville3ab207e2014-11-20 13:07:20 -0800278 private AppOpsManager mAppOps;
279 private MainThreadHandler mMainThreadHandler;
Wink Savilleac1bdfd2014-11-20 23:04:44 -0800280 private SubscriptionController mSubscriptionController;
Wink Saville3ab207e2014-11-20 13:07:20 -0800281 private SharedPreferences mTelephonySharedPreferences;
Malcolm Chen2c63d402018-08-14 16:00:53 -0700282 private PhoneConfigurationManager mPhoneConfigurationManager;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700283
Derek Tan97ebb422014-09-05 16:55:38 -0700284 private static final String PREF_CARRIERS_ALPHATAG_PREFIX = "carrier_alphtag_";
285 private static final String PREF_CARRIERS_NUMBER_PREFIX = "carrier_number_";
Jeff Sharkey85190e62014-12-05 09:40:12 -0800286 private static final String PREF_CARRIERS_SUBSCRIBER_PREFIX = "carrier_subscriber_";
Brad Ebinger1c8542e2019-01-14 13:43:14 -0800287 private static final String PREF_PROVISION_IMS_MMTEL_PREFIX = "provision_ims_mmtel_";
Derek Tan89e89d42014-07-08 17:00:10 -0700288
Michelecea4cf22018-12-21 15:00:11 -0800289 // String to store multi SIM allowed
290 private static final String PREF_MULTI_SIM_RESTRICTED = "multisim_restricted";
291
Derek Tan740e1672017-06-27 14:56:27 -0700292 // The AID of ISD-R.
293 private static final String ISDR_AID = "A0000005591010FFFFFFFF8900000100";
294
yinxub1bed742017-04-17 11:45:04 -0700295 private NetworkScanRequestTracker mNetworkScanRequestTracker;
296
David Kelly5e06a7f2018-03-12 14:10:59 +0000297 private static final int TYPE_ALLOCATION_CODE_LENGTH = 8;
298 private static final int MANUFACTURER_CODE_LENGTH = 8;
299
Derek Tan89e89d42014-07-08 17:00:10 -0700300 /**
Naina Nalluri1264a9f2019-09-17 14:10:30 -0700301 * Experiment flag to enable erase modem config on reset network, default value is false
302 */
303 public static final String RESET_NETWORK_ERASE_MODEM_CONFIG_ENABLED =
304 "reset_network_erase_modem_config_enabled";
305
306 /**
Shishir Agrawal566b7612013-10-28 14:41:00 -0700307 * A request object to use for transmitting data to an ICC.
308 */
309 private static final class IccAPDUArgument {
310 public int channel, cla, command, p1, p2, p3;
311 public String data;
312
313 public IccAPDUArgument(int channel, int cla, int command,
314 int p1, int p2, int p3, String data) {
315 this.channel = channel;
316 this.cla = cla;
317 this.command = command;
318 this.p1 = p1;
319 this.p2 = p2;
320 this.p3 = p3;
321 this.data = data;
322 }
323 }
324
325 /**
Shishir Agrawal77ba3172015-09-10 14:50:19 -0700326 * A request object to use for transmitting data to an ICC.
327 */
328 private static final class ManualNetworkSelectionArgument {
329 public OperatorInfo operatorInfo;
330 public boolean persistSelection;
331
332 public ManualNetworkSelectionArgument(OperatorInfo operatorInfo, boolean persistSelection) {
333 this.operatorInfo = operatorInfo;
334 this.persistSelection = persistSelection;
335 }
336 }
337
338 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700339 * A request object for use with {@link MainThreadHandler}. Requesters should wait() on the
340 * request after sending. The main thread will notify the request when it is complete.
341 */
342 private static final class MainThreadRequest {
343 /** The argument to use for the request */
344 public Object argument;
345 /** The result of the request that is run on the main thread */
346 public Object result;
Sanket Padawe56e75a32016-02-08 12:18:19 -0800347 // The subscriber id that this request applies to. Defaults to
348 // SubscriptionManager.INVALID_SUBSCRIPTION_ID
349 public Integer subId = SubscriptionManager.INVALID_SUBSCRIPTION_ID;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700350
Nathan Harold92bed182018-10-12 18:16:49 -0700351 // In cases where subId is unavailable, the caller needs to specify the phone.
352 public Phone phone;
353
vagdeviaf9a5b92018-08-15 16:01:53 -0700354 public WorkSource workSource;
355
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700356 public MainThreadRequest(Object argument) {
357 this.argument = argument;
358 }
Shishir Agrawal76d5da92014-11-09 16:17:25 -0800359
Nathan Harold92bed182018-10-12 18:16:49 -0700360 MainThreadRequest(Object argument, Phone phone, WorkSource workSource) {
361 this.argument = argument;
362 if (phone != null) {
363 this.phone = phone;
364 }
365 this.workSource = workSource;
366 }
367
vagdeviaf9a5b92018-08-15 16:01:53 -0700368 MainThreadRequest(Object argument, Integer subId, WorkSource workSource) {
Shishir Agrawal76d5da92014-11-09 16:17:25 -0800369 this.argument = argument;
Sanket Padawe56e75a32016-02-08 12:18:19 -0800370 if (subId != null) {
371 this.subId = subId;
372 }
vagdeviaf9a5b92018-08-15 16:01:53 -0700373 this.workSource = workSource;
Shishir Agrawal76d5da92014-11-09 16:17:25 -0800374 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700375 }
376
Sailesh Nepalcc0375f2013-11-13 09:15:18 -0800377 private static final class IncomingThirdPartyCallArgs {
378 public final ComponentName component;
379 public final String callId;
380 public final String callerDisplayName;
381
382 public IncomingThirdPartyCallArgs(ComponentName component, String callId,
383 String callerDisplayName) {
384 this.component = component;
385 this.callId = callId;
386 this.callerDisplayName = callerDisplayName;
387 }
388 }
389
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700390 /**
391 * A handler that processes messages on the main thread in the phone process. Since many
392 * of the Phone calls are not thread safe this is needed to shuttle the requests from the
393 * inbound binder threads to the main thread in the phone process. The Binder thread
394 * may provide a {@link MainThreadRequest} object in the msg.obj field that they are waiting
395 * on, which will be notified when the operation completes and will contain the result of the
396 * request.
397 *
398 * <p>If a MainThreadRequest object is provided in the msg.obj field,
399 * note that request.result must be set to something non-null for the calling thread to
400 * unblock.
401 */
402 private final class MainThreadHandler extends Handler {
403 @Override
404 public void handleMessage(Message msg) {
405 MainThreadRequest request;
406 Message onCompleted;
407 AsyncResult ar;
Shishir Agrawalc04d9752016-02-19 10:41:00 -0800408 UiccCard uiccCard;
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700409 IccAPDUArgument iccArgument;
Nazanin Bakhshif782e562018-12-11 15:15:39 -0800410 final Phone defaultPhone = getDefaultPhone();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700411
412 switch (msg.what) {
Pengquan Menga1bb6272018-09-06 09:59:22 -0700413 case CMD_HANDLE_USSD_REQUEST: {
414 request = (MainThreadRequest) msg.obj;
415 final Phone phone = getPhoneFromRequest(request);
416 Pair<String, ResultReceiver> ussdObject = (Pair) request.argument;
417 String ussdRequest = ussdObject.first;
418 ResultReceiver wrappedCallback = ussdObject.second;
Tyler Gunn65d45c22017-06-05 11:22:26 -0700419
Pengquan Menga1bb6272018-09-06 09:59:22 -0700420 if (!isUssdApiAllowed(request.subId)) {
421 // Carrier does not support use of this API, return failure.
422 Rlog.w(LOG_TAG, "handleUssdRequest: carrier does not support USSD apis.");
423 UssdResponse response = new UssdResponse(ussdRequest, null);
424 Bundle returnData = new Bundle();
425 returnData.putParcelable(TelephonyManager.USSD_RESPONSE, response);
426 wrappedCallback.send(TelephonyManager.USSD_RETURN_FAILURE, returnData);
Tyler Gunn65d45c22017-06-05 11:22:26 -0700427
Pengquan Menga1bb6272018-09-06 09:59:22 -0700428 request.result = true;
429 notifyRequester(request);
430 return;
431 }
Tyler Gunn65d45c22017-06-05 11:22:26 -0700432
Pengquan Menga1bb6272018-09-06 09:59:22 -0700433 try {
434 request.result = phone != null
435 ? phone.handleUssdRequest(ussdRequest, wrappedCallback) : false;
436 } catch (CallStateException cse) {
437 request.result = false;
438 }
439 // Wake up the requesting thread
440 notifyRequester(request);
441 break;
pkanwar32d516d2016-10-14 19:37:38 -0700442 }
443
Yorke Lee716f67e2015-06-17 15:39:16 -0700444 case CMD_HANDLE_PIN_MMI: {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700445 request = (MainThreadRequest) msg.obj;
Yorke Lee716f67e2015-06-17 15:39:16 -0700446 final Phone phone = getPhoneFromRequest(request);
447 request.result = phone != null ?
448 getPhoneFromRequest(request).handlePinMmi((String) request.argument)
449 : false;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700450 // Wake up the requesting thread
Pengquan Menga1bb6272018-09-06 09:59:22 -0700451 notifyRequester(request);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700452 break;
Yorke Lee716f67e2015-06-17 15:39:16 -0700453 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700454
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700455 case CMD_TRANSMIT_APDU_LOGICAL_CHANNEL:
Shishir Agrawal566b7612013-10-28 14:41:00 -0700456 request = (MainThreadRequest) msg.obj;
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700457 iccArgument = (IccAPDUArgument) request.argument;
Shishir Agrawalc04d9752016-02-19 10:41:00 -0800458 uiccCard = getUiccCardFromRequest(request);
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700459 if (uiccCard == null) {
460 loge("iccTransmitApduLogicalChannel: No UICC");
461 request.result = new IccIoResult(0x6F, 0, (byte[])null);
Pengquan Menga1bb6272018-09-06 09:59:22 -0700462 notifyRequester(request);
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700463 } else {
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700464 onCompleted = obtainMessage(EVENT_TRANSMIT_APDU_LOGICAL_CHANNEL_DONE,
465 request);
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700466 uiccCard.iccTransmitApduLogicalChannel(
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700467 iccArgument.channel, iccArgument.cla, iccArgument.command,
468 iccArgument.p1, iccArgument.p2, iccArgument.p3, iccArgument.data,
Shishir Agrawal566b7612013-10-28 14:41:00 -0700469 onCompleted);
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700470 }
Shishir Agrawal566b7612013-10-28 14:41:00 -0700471 break;
472
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700473 case EVENT_TRANSMIT_APDU_LOGICAL_CHANNEL_DONE:
Shishir Agrawal566b7612013-10-28 14:41:00 -0700474 ar = (AsyncResult) msg.obj;
475 request = (MainThreadRequest) ar.userObj;
476 if (ar.exception == null && ar.result != null) {
477 request.result = ar.result;
478 } else {
479 request.result = new IccIoResult(0x6F, 0, (byte[])null);
480 if (ar.result == null) {
481 loge("iccTransmitApduLogicalChannel: Empty response");
Jake Hambye994d462014-02-03 13:10:13 -0800482 } else if (ar.exception instanceof CommandException) {
Shishir Agrawal566b7612013-10-28 14:41:00 -0700483 loge("iccTransmitApduLogicalChannel: CommandException: " +
Jake Hambye994d462014-02-03 13:10:13 -0800484 ar.exception);
Shishir Agrawal566b7612013-10-28 14:41:00 -0700485 } else {
486 loge("iccTransmitApduLogicalChannel: Unknown exception");
487 }
488 }
Pengquan Menga1bb6272018-09-06 09:59:22 -0700489 notifyRequester(request);
Shishir Agrawal566b7612013-10-28 14:41:00 -0700490 break;
491
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700492 case CMD_TRANSMIT_APDU_BASIC_CHANNEL:
493 request = (MainThreadRequest) msg.obj;
494 iccArgument = (IccAPDUArgument) request.argument;
Shishir Agrawalc04d9752016-02-19 10:41:00 -0800495 uiccCard = getUiccCardFromRequest(request);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700496 if (uiccCard == null) {
497 loge("iccTransmitApduBasicChannel: No UICC");
498 request.result = new IccIoResult(0x6F, 0, (byte[])null);
Pengquan Menga1bb6272018-09-06 09:59:22 -0700499 notifyRequester(request);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700500 } else {
501 onCompleted = obtainMessage(EVENT_TRANSMIT_APDU_BASIC_CHANNEL_DONE,
502 request);
503 uiccCard.iccTransmitApduBasicChannel(
504 iccArgument.cla, iccArgument.command, iccArgument.p1, iccArgument.p2,
505 iccArgument.p3, iccArgument.data, onCompleted);
506 }
507 break;
508
509 case EVENT_TRANSMIT_APDU_BASIC_CHANNEL_DONE:
510 ar = (AsyncResult) msg.obj;
511 request = (MainThreadRequest) ar.userObj;
512 if (ar.exception == null && ar.result != null) {
513 request.result = ar.result;
514 } else {
515 request.result = new IccIoResult(0x6F, 0, (byte[])null);
516 if (ar.result == null) {
517 loge("iccTransmitApduBasicChannel: Empty response");
518 } else if (ar.exception instanceof CommandException) {
519 loge("iccTransmitApduBasicChannel: CommandException: " +
520 ar.exception);
521 } else {
522 loge("iccTransmitApduBasicChannel: Unknown exception");
523 }
524 }
Pengquan Menga1bb6272018-09-06 09:59:22 -0700525 notifyRequester(request);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700526 break;
527
528 case CMD_EXCHANGE_SIM_IO:
529 request = (MainThreadRequest) msg.obj;
530 iccArgument = (IccAPDUArgument) request.argument;
Shishir Agrawalc04d9752016-02-19 10:41:00 -0800531 uiccCard = getUiccCardFromRequest(request);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700532 if (uiccCard == null) {
533 loge("iccExchangeSimIO: No UICC");
534 request.result = new IccIoResult(0x6F, 0, (byte[])null);
Pengquan Menga1bb6272018-09-06 09:59:22 -0700535 notifyRequester(request);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700536 } else {
537 onCompleted = obtainMessage(EVENT_EXCHANGE_SIM_IO_DONE,
538 request);
539 uiccCard.iccExchangeSimIO(iccArgument.cla, /* fileID */
540 iccArgument.command, iccArgument.p1, iccArgument.p2, iccArgument.p3,
541 iccArgument.data, onCompleted);
542 }
543 break;
544
545 case EVENT_EXCHANGE_SIM_IO_DONE:
546 ar = (AsyncResult) msg.obj;
547 request = (MainThreadRequest) ar.userObj;
548 if (ar.exception == null && ar.result != null) {
549 request.result = ar.result;
550 } else {
551 request.result = new IccIoResult(0x6f, 0, (byte[])null);
552 }
Pengquan Menga1bb6272018-09-06 09:59:22 -0700553 notifyRequester(request);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700554 break;
555
Derek Tan4d5e5c12014-02-04 11:54:58 -0800556 case CMD_SEND_ENVELOPE:
557 request = (MainThreadRequest) msg.obj;
Shishir Agrawalc04d9752016-02-19 10:41:00 -0800558 uiccCard = getUiccCardFromRequest(request);
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700559 if (uiccCard == null) {
560 loge("sendEnvelopeWithStatus: No UICC");
561 request.result = new IccIoResult(0x6F, 0, (byte[])null);
Pengquan Menga1bb6272018-09-06 09:59:22 -0700562 notifyRequester(request);
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700563 } else {
564 onCompleted = obtainMessage(EVENT_SEND_ENVELOPE_DONE, request);
565 uiccCard.sendEnvelopeWithStatus((String)request.argument, onCompleted);
566 }
Derek Tan4d5e5c12014-02-04 11:54:58 -0800567 break;
568
569 case EVENT_SEND_ENVELOPE_DONE:
570 ar = (AsyncResult) msg.obj;
571 request = (MainThreadRequest) ar.userObj;
Shishir Agrawal9f9877d2014-03-14 09:36:27 -0700572 if (ar.exception == null && ar.result != null) {
573 request.result = ar.result;
Derek Tan4d5e5c12014-02-04 11:54:58 -0800574 } else {
Shishir Agrawal9f9877d2014-03-14 09:36:27 -0700575 request.result = new IccIoResult(0x6F, 0, (byte[])null);
576 if (ar.result == null) {
577 loge("sendEnvelopeWithStatus: Empty response");
578 } else if (ar.exception instanceof CommandException) {
579 loge("sendEnvelopeWithStatus: CommandException: " +
580 ar.exception);
581 } else {
582 loge("sendEnvelopeWithStatus: exception:" + ar.exception);
583 }
Derek Tan4d5e5c12014-02-04 11:54:58 -0800584 }
Pengquan Menga1bb6272018-09-06 09:59:22 -0700585 notifyRequester(request);
Derek Tan4d5e5c12014-02-04 11:54:58 -0800586 break;
587
Shishir Agrawal566b7612013-10-28 14:41:00 -0700588 case CMD_OPEN_CHANNEL:
589 request = (MainThreadRequest) msg.obj;
Shishir Agrawalc04d9752016-02-19 10:41:00 -0800590 uiccCard = getUiccCardFromRequest(request);
Ajay Nambid7454d32015-12-03 13:50:00 -0800591 Pair<String, Integer> openChannelArgs = (Pair<String, Integer>) request.argument;
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700592 if (uiccCard == null) {
593 loge("iccOpenLogicalChannel: No UICC");
Shishir Agrawalfc0492a2016-02-17 11:15:33 -0800594 request.result = new IccOpenLogicalChannelResponse(-1,
595 IccOpenLogicalChannelResponse.STATUS_MISSING_RESOURCE, null);
Pengquan Menga1bb6272018-09-06 09:59:22 -0700596 notifyRequester(request);
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700597 } else {
598 onCompleted = obtainMessage(EVENT_OPEN_CHANNEL_DONE, request);
Ajay Nambid7454d32015-12-03 13:50:00 -0800599 uiccCard.iccOpenLogicalChannel(openChannelArgs.first,
600 openChannelArgs.second, onCompleted);
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700601 }
Shishir Agrawal566b7612013-10-28 14:41:00 -0700602 break;
603
604 case EVENT_OPEN_CHANNEL_DONE:
605 ar = (AsyncResult) msg.obj;
606 request = (MainThreadRequest) ar.userObj;
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700607 IccOpenLogicalChannelResponse openChannelResp;
Shishir Agrawal566b7612013-10-28 14:41:00 -0700608 if (ar.exception == null && ar.result != null) {
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700609 int[] result = (int[]) ar.result;
610 int channelId = result[0];
611 byte[] selectResponse = null;
612 if (result.length > 1) {
613 selectResponse = new byte[result.length - 1];
614 for (int i = 1; i < result.length; ++i) {
615 selectResponse[i - 1] = (byte) result[i];
616 }
617 }
618 openChannelResp = new IccOpenLogicalChannelResponse(channelId,
Shishir Agrawal527e8bf2014-08-25 08:54:56 -0700619 IccOpenLogicalChannelResponse.STATUS_NO_ERROR, selectResponse);
Shishir Agrawal566b7612013-10-28 14:41:00 -0700620 } else {
Shishir Agrawal566b7612013-10-28 14:41:00 -0700621 if (ar.result == null) {
622 loge("iccOpenLogicalChannel: Empty response");
Shishir Agrawal566b7612013-10-28 14:41:00 -0700623 }
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700624 if (ar.exception != null) {
625 loge("iccOpenLogicalChannel: Exception: " + ar.exception);
626 }
627
Shishir Agrawal527e8bf2014-08-25 08:54:56 -0700628 int errorCode = IccOpenLogicalChannelResponse.STATUS_UNKNOWN_ERROR;
Junda Liua754ba12015-05-20 01:17:52 -0700629 if (ar.exception instanceof CommandException) {
630 CommandException.Error error =
631 ((CommandException) (ar.exception)).getCommandError();
632 if (error == CommandException.Error.MISSING_RESOURCE) {
Shishir Agrawal527e8bf2014-08-25 08:54:56 -0700633 errorCode = IccOpenLogicalChannelResponse.STATUS_MISSING_RESOURCE;
Junda Liua754ba12015-05-20 01:17:52 -0700634 } else if (error == CommandException.Error.NO_SUCH_ELEMENT) {
Shishir Agrawal527e8bf2014-08-25 08:54:56 -0700635 errorCode = IccOpenLogicalChannelResponse.STATUS_NO_SUCH_ELEMENT;
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700636 }
637 }
638 openChannelResp = new IccOpenLogicalChannelResponse(
639 IccOpenLogicalChannelResponse.INVALID_CHANNEL, errorCode, null);
Shishir Agrawal566b7612013-10-28 14:41:00 -0700640 }
Shishir Agrawal82c8a462014-07-31 18:13:17 -0700641 request.result = openChannelResp;
Pengquan Menga1bb6272018-09-06 09:59:22 -0700642 notifyRequester(request);
Shishir Agrawal566b7612013-10-28 14:41:00 -0700643 break;
644
645 case CMD_CLOSE_CHANNEL:
646 request = (MainThreadRequest) msg.obj;
Shishir Agrawalc04d9752016-02-19 10:41:00 -0800647 uiccCard = getUiccCardFromRequest(request);
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700648 if (uiccCard == null) {
649 loge("iccCloseLogicalChannel: No UICC");
Yoshiaki Naka2e29d822016-09-02 19:27:39 +0900650 request.result = false;
Pengquan Menga1bb6272018-09-06 09:59:22 -0700651 notifyRequester(request);
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700652 } else {
653 onCompleted = obtainMessage(EVENT_CLOSE_CHANNEL_DONE, request);
654 uiccCard.iccCloseLogicalChannel((Integer) request.argument, onCompleted);
655 }
Shishir Agrawal566b7612013-10-28 14:41:00 -0700656 break;
657
658 case EVENT_CLOSE_CHANNEL_DONE:
Jake Hambye994d462014-02-03 13:10:13 -0800659 handleNullReturnEvent(msg, "iccCloseLogicalChannel");
660 break;
661
662 case CMD_NV_READ_ITEM:
663 request = (MainThreadRequest) msg.obj;
664 onCompleted = obtainMessage(EVENT_NV_READ_ITEM_DONE, request);
Nazanin Bakhshif782e562018-12-11 15:15:39 -0800665 defaultPhone.nvReadItem((Integer) request.argument, onCompleted,
666 request.workSource);
Jake Hambye994d462014-02-03 13:10:13 -0800667 break;
668
669 case EVENT_NV_READ_ITEM_DONE:
Shishir Agrawal566b7612013-10-28 14:41:00 -0700670 ar = (AsyncResult) msg.obj;
671 request = (MainThreadRequest) ar.userObj;
Jake Hambye994d462014-02-03 13:10:13 -0800672 if (ar.exception == null && ar.result != null) {
673 request.result = ar.result; // String
Shishir Agrawal566b7612013-10-28 14:41:00 -0700674 } else {
Jake Hambye994d462014-02-03 13:10:13 -0800675 request.result = "";
676 if (ar.result == null) {
677 loge("nvReadItem: Empty response");
678 } else if (ar.exception instanceof CommandException) {
679 loge("nvReadItem: CommandException: " +
680 ar.exception);
Shishir Agrawal566b7612013-10-28 14:41:00 -0700681 } else {
Jake Hambye994d462014-02-03 13:10:13 -0800682 loge("nvReadItem: Unknown exception");
Shishir Agrawal566b7612013-10-28 14:41:00 -0700683 }
684 }
Pengquan Menga1bb6272018-09-06 09:59:22 -0700685 notifyRequester(request);
Shishir Agrawal566b7612013-10-28 14:41:00 -0700686 break;
687
Jake Hambye994d462014-02-03 13:10:13 -0800688 case CMD_NV_WRITE_ITEM:
689 request = (MainThreadRequest) msg.obj;
690 onCompleted = obtainMessage(EVENT_NV_WRITE_ITEM_DONE, request);
691 Pair<Integer, String> idValue = (Pair<Integer, String>) request.argument;
Nazanin Bakhshif782e562018-12-11 15:15:39 -0800692 defaultPhone.nvWriteItem(idValue.first, idValue.second, onCompleted,
vagdeviaf9a5b92018-08-15 16:01:53 -0700693 request.workSource);
Jake Hambye994d462014-02-03 13:10:13 -0800694 break;
695
696 case EVENT_NV_WRITE_ITEM_DONE:
697 handleNullReturnEvent(msg, "nvWriteItem");
698 break;
699
700 case CMD_NV_WRITE_CDMA_PRL:
701 request = (MainThreadRequest) msg.obj;
702 onCompleted = obtainMessage(EVENT_NV_WRITE_CDMA_PRL_DONE, request);
Nazanin Bakhshif782e562018-12-11 15:15:39 -0800703 defaultPhone.nvWriteCdmaPrl((byte[]) request.argument, onCompleted);
Jake Hambye994d462014-02-03 13:10:13 -0800704 break;
705
706 case EVENT_NV_WRITE_CDMA_PRL_DONE:
707 handleNullReturnEvent(msg, "nvWriteCdmaPrl");
708 break;
709
chen xu6dac5ab2018-10-26 17:39:23 -0700710 case CMD_RESET_MODEM_CONFIG:
Jake Hambye994d462014-02-03 13:10:13 -0800711 request = (MainThreadRequest) msg.obj;
chen xu6dac5ab2018-10-26 17:39:23 -0700712 onCompleted = obtainMessage(EVENT_RESET_MODEM_CONFIG_DONE, request);
Nazanin Bakhshif782e562018-12-11 15:15:39 -0800713 defaultPhone.resetModemConfig(onCompleted);
Jake Hambye994d462014-02-03 13:10:13 -0800714 break;
715
chen xu6dac5ab2018-10-26 17:39:23 -0700716 case EVENT_RESET_MODEM_CONFIG_DONE:
717 handleNullReturnEvent(msg, "resetModemConfig");
Jake Hambye994d462014-02-03 13:10:13 -0800718 break;
719
Jake Hamby7c27be32014-03-03 13:25:59 -0800720 case CMD_GET_PREFERRED_NETWORK_TYPE:
721 request = (MainThreadRequest) msg.obj;
722 onCompleted = obtainMessage(EVENT_GET_PREFERRED_NETWORK_TYPE_DONE, request);
Stuart Scott54788802015-03-30 13:18:01 -0700723 getPhoneFromRequest(request).getPreferredNetworkType(onCompleted);
Jake Hamby7c27be32014-03-03 13:25:59 -0800724 break;
725
726 case EVENT_GET_PREFERRED_NETWORK_TYPE_DONE:
727 ar = (AsyncResult) msg.obj;
728 request = (MainThreadRequest) ar.userObj;
729 if (ar.exception == null && ar.result != null) {
730 request.result = ar.result; // Integer
731 } else {
Sanket Padawecfc2d352016-01-05 19:52:14 -0800732 request.result = null;
Jake Hamby7c27be32014-03-03 13:25:59 -0800733 if (ar.result == null) {
734 loge("getPreferredNetworkType: Empty response");
735 } else if (ar.exception instanceof CommandException) {
736 loge("getPreferredNetworkType: CommandException: " +
737 ar.exception);
738 } else {
739 loge("getPreferredNetworkType: Unknown exception");
740 }
741 }
Pengquan Menga1bb6272018-09-06 09:59:22 -0700742 notifyRequester(request);
Jake Hamby7c27be32014-03-03 13:25:59 -0800743 break;
744
745 case CMD_SET_PREFERRED_NETWORK_TYPE:
746 request = (MainThreadRequest) msg.obj;
747 onCompleted = obtainMessage(EVENT_SET_PREFERRED_NETWORK_TYPE_DONE, request);
748 int networkType = (Integer) request.argument;
Stuart Scott54788802015-03-30 13:18:01 -0700749 getPhoneFromRequest(request).setPreferredNetworkType(networkType, onCompleted);
Jake Hamby7c27be32014-03-03 13:25:59 -0800750 break;
751
752 case EVENT_SET_PREFERRED_NETWORK_TYPE_DONE:
753 handleNullReturnEvent(msg, "setPreferredNetworkType");
754 break;
755
Shuo Qian850e4d6a2018-04-25 21:02:08 +0000756 case CMD_INVOKE_OEM_RIL_REQUEST_RAW:
757 request = (MainThreadRequest)msg.obj;
758 onCompleted = obtainMessage(EVENT_INVOKE_OEM_RIL_REQUEST_RAW_DONE, request);
Nazanin Bakhshif782e562018-12-11 15:15:39 -0800759 defaultPhone.invokeOemRilRequestRaw((byte[]) request.argument, onCompleted);
Shuo Qian850e4d6a2018-04-25 21:02:08 +0000760 break;
761
762 case EVENT_INVOKE_OEM_RIL_REQUEST_RAW_DONE:
763 ar = (AsyncResult)msg.obj;
764 request = (MainThreadRequest)ar.userObj;
765 request.result = ar;
Pengquan Menga1bb6272018-09-06 09:59:22 -0700766 notifyRequester(request);
Shuo Qian850e4d6a2018-04-25 21:02:08 +0000767 break;
768
Shishir Agrawal76d5da92014-11-09 16:17:25 -0800769 case CMD_SET_VOICEMAIL_NUMBER:
770 request = (MainThreadRequest) msg.obj;
771 onCompleted = obtainMessage(EVENT_SET_VOICEMAIL_NUMBER_DONE, request);
772 Pair<String, String> tagNum = (Pair<String, String>) request.argument;
Stuart Scott584921c2015-01-15 17:10:34 -0800773 getPhoneFromRequest(request).setVoiceMailNumber(tagNum.first, tagNum.second,
774 onCompleted);
Shishir Agrawal76d5da92014-11-09 16:17:25 -0800775 break;
776
777 case EVENT_SET_VOICEMAIL_NUMBER_DONE:
778 handleNullReturnEvent(msg, "setVoicemailNumber");
779 break;
780
Stuart Scott54788802015-03-30 13:18:01 -0700781 case CMD_SET_NETWORK_SELECTION_MODE_AUTOMATIC:
782 request = (MainThreadRequest) msg.obj;
783 onCompleted = obtainMessage(EVENT_SET_NETWORK_SELECTION_MODE_AUTOMATIC_DONE,
784 request);
785 getPhoneFromRequest(request).setNetworkSelectionModeAutomatic(onCompleted);
786 break;
787
788 case EVENT_SET_NETWORK_SELECTION_MODE_AUTOMATIC_DONE:
789 handleNullReturnEvent(msg, "setNetworkSelectionModeAutomatic");
790 break;
791
Shishir Agrawal302c8692015-06-19 13:49:39 -0700792 case CMD_PERFORM_NETWORK_SCAN:
793 request = (MainThreadRequest) msg.obj;
794 onCompleted = obtainMessage(EVENT_PERFORM_NETWORK_SCAN_DONE, request);
795 getPhoneFromRequest(request).getAvailableNetworks(onCompleted);
796 break;
797
798 case EVENT_PERFORM_NETWORK_SCAN_DONE:
799 ar = (AsyncResult) msg.obj;
800 request = (MainThreadRequest) ar.userObj;
801 CellNetworkScanResult cellScanResult;
802 if (ar.exception == null && ar.result != null) {
803 cellScanResult = new CellNetworkScanResult(
804 CellNetworkScanResult.STATUS_SUCCESS,
805 (List<OperatorInfo>) ar.result);
806 } else {
807 if (ar.result == null) {
808 loge("getCellNetworkScanResults: Empty response");
809 }
810 if (ar.exception != null) {
811 loge("getCellNetworkScanResults: Exception: " + ar.exception);
812 }
813 int errorCode = CellNetworkScanResult.STATUS_UNKNOWN_ERROR;
814 if (ar.exception instanceof CommandException) {
815 CommandException.Error error =
816 ((CommandException) (ar.exception)).getCommandError();
817 if (error == CommandException.Error.RADIO_NOT_AVAILABLE) {
818 errorCode = CellNetworkScanResult.STATUS_RADIO_NOT_AVAILABLE;
819 } else if (error == CommandException.Error.GENERIC_FAILURE) {
820 errorCode = CellNetworkScanResult.STATUS_RADIO_GENERIC_FAILURE;
821 }
822 }
823 cellScanResult = new CellNetworkScanResult(errorCode, null);
824 }
825 request.result = cellScanResult;
Pengquan Menga1bb6272018-09-06 09:59:22 -0700826 notifyRequester(request);
Shishir Agrawal302c8692015-06-19 13:49:39 -0700827 break;
828
829 case CMD_SET_NETWORK_SELECTION_MODE_MANUAL:
830 request = (MainThreadRequest) msg.obj;
Shishir Agrawal77ba3172015-09-10 14:50:19 -0700831 ManualNetworkSelectionArgument selArg =
832 (ManualNetworkSelectionArgument) request.argument;
Shishir Agrawal302c8692015-06-19 13:49:39 -0700833 onCompleted = obtainMessage(EVENT_SET_NETWORK_SELECTION_MODE_MANUAL_DONE,
834 request);
Shishir Agrawal77ba3172015-09-10 14:50:19 -0700835 getPhoneFromRequest(request).selectNetworkManually(selArg.operatorInfo,
836 selArg.persistSelection, onCompleted);
Shishir Agrawal302c8692015-06-19 13:49:39 -0700837 break;
838
839 case EVENT_SET_NETWORK_SELECTION_MODE_MANUAL_DONE:
Pengquan Menge3d01e22018-09-20 15:25:35 -0700840 ar = (AsyncResult) msg.obj;
841 request = (MainThreadRequest) ar.userObj;
842 if (ar.exception == null) {
843 request.result = true;
844 } else {
845 request.result = false;
846 loge("setNetworkSelectionModeManual " + ar.exception);
847 }
848 notifyRequester(request);
849 mApp.onNetworkSelectionChanged(request.subId);
Shishir Agrawal302c8692015-06-19 13:49:39 -0700850 break;
851
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -0700852 case CMD_GET_MODEM_ACTIVITY_INFO:
853 request = (MainThreadRequest) msg.obj;
854 onCompleted = obtainMessage(EVENT_GET_MODEM_ACTIVITY_INFO_DONE, request);
James Mattisab947702019-04-03 14:18:34 -0700855 if (defaultPhone != null) {
856 defaultPhone.getModemActivityInfo(onCompleted, request.workSource);
857 }
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -0700858 break;
859
860 case EVENT_GET_MODEM_ACTIVITY_INFO_DONE:
861 ar = (AsyncResult) msg.obj;
862 request = (MainThreadRequest) ar.userObj;
863 if (ar.exception == null && ar.result != null) {
864 request.result = ar.result;
865 } else {
866 if (ar.result == null) {
867 loge("queryModemActivityInfo: Empty response");
868 } else if (ar.exception instanceof CommandException) {
869 loge("queryModemActivityInfo: CommandException: " +
870 ar.exception);
871 } else {
872 loge("queryModemActivityInfo: Unknown exception");
873 }
874 }
Amit Mahajand4766222016-01-28 15:28:28 -0800875 // Result cannot be null. Return ModemActivityInfo with all fields set to 0.
876 if (request.result == null) {
877 request.result = new ModemActivityInfo(0, 0, 0, null, 0, 0);
878 }
Pengquan Menga1bb6272018-09-06 09:59:22 -0700879 notifyRequester(request);
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -0700880 break;
881
Meng Wang1a7c35a2016-05-05 20:56:15 -0700882 case CMD_SET_ALLOWED_CARRIERS:
883 request = (MainThreadRequest) msg.obj;
Michele Berionne482f8202018-11-27 18:57:59 -0800884 CarrierRestrictionRules argument =
885 (CarrierRestrictionRules) request.argument;
Meng Wang1a7c35a2016-05-05 20:56:15 -0700886 onCompleted = obtainMessage(EVENT_SET_ALLOWED_CARRIERS_DONE, request);
Michele Berionne482f8202018-11-27 18:57:59 -0800887 defaultPhone.setAllowedCarriers(argument, onCompleted, request.workSource);
Meng Wang1a7c35a2016-05-05 20:56:15 -0700888 break;
889
890 case EVENT_SET_ALLOWED_CARRIERS_DONE:
891 ar = (AsyncResult) msg.obj;
892 request = (MainThreadRequest) ar.userObj;
893 if (ar.exception == null && ar.result != null) {
894 request.result = ar.result;
895 } else {
Michele Berionne482f8202018-11-27 18:57:59 -0800896 request.result = TelephonyManager.SET_CARRIER_RESTRICTION_ERROR;
897 if (ar.exception instanceof CommandException) {
898 loge("setAllowedCarriers: CommandException: " + ar.exception);
899 CommandException.Error error =
900 ((CommandException) (ar.exception)).getCommandError();
901 if (error == CommandException.Error.REQUEST_NOT_SUPPORTED) {
902 request.result =
903 TelephonyManager.SET_CARRIER_RESTRICTION_NOT_SUPPORTED;
904 }
Meng Wang1a7c35a2016-05-05 20:56:15 -0700905 } else {
906 loge("setAllowedCarriers: Unknown exception");
907 }
908 }
Pengquan Menga1bb6272018-09-06 09:59:22 -0700909 notifyRequester(request);
Meng Wang1a7c35a2016-05-05 20:56:15 -0700910 break;
911
912 case CMD_GET_ALLOWED_CARRIERS:
913 request = (MainThreadRequest) msg.obj;
914 onCompleted = obtainMessage(EVENT_GET_ALLOWED_CARRIERS_DONE, request);
Nazanin Bakhshif782e562018-12-11 15:15:39 -0800915 defaultPhone.getAllowedCarriers(onCompleted, request.workSource);
Meng Wang1a7c35a2016-05-05 20:56:15 -0700916 break;
917
918 case EVENT_GET_ALLOWED_CARRIERS_DONE:
919 ar = (AsyncResult) msg.obj;
920 request = (MainThreadRequest) ar.userObj;
921 if (ar.exception == null && ar.result != null) {
922 request.result = ar.result;
923 } else {
Michele Berionne482f8202018-11-27 18:57:59 -0800924 request.result = new IllegalStateException(
925 "Failed to get carrier restrictions");
Meng Wang1a7c35a2016-05-05 20:56:15 -0700926 if (ar.result == null) {
927 loge("getAllowedCarriers: Empty response");
928 } else if (ar.exception instanceof CommandException) {
929 loge("getAllowedCarriers: CommandException: " +
930 ar.exception);
931 } else {
932 loge("getAllowedCarriers: Unknown exception");
933 }
934 }
Pengquan Menga1bb6272018-09-06 09:59:22 -0700935 notifyRequester(request);
Meng Wang1a7c35a2016-05-05 20:56:15 -0700936 break;
937
Nathan Haroldb3014052017-01-25 15:57:32 -0800938 case EVENT_GET_FORBIDDEN_PLMNS_DONE:
939 ar = (AsyncResult) msg.obj;
940 request = (MainThreadRequest) ar.userObj;
941 if (ar.exception == null && ar.result != null) {
942 request.result = ar.result;
943 } else {
944 request.result = new IllegalArgumentException(
945 "Failed to retrieve Forbidden Plmns");
946 if (ar.result == null) {
947 loge("getForbiddenPlmns: Empty response");
948 } else {
949 loge("getForbiddenPlmns: Unknown exception");
950 }
951 }
Pengquan Menga1bb6272018-09-06 09:59:22 -0700952 notifyRequester(request);
Nathan Haroldb3014052017-01-25 15:57:32 -0800953 break;
954
955 case CMD_GET_FORBIDDEN_PLMNS:
956 request = (MainThreadRequest) msg.obj;
957 uiccCard = getUiccCardFromRequest(request);
958 if (uiccCard == null) {
959 loge("getForbiddenPlmns() UiccCard is null");
960 request.result = new IllegalArgumentException(
961 "getForbiddenPlmns() UiccCard is null");
Pengquan Menga1bb6272018-09-06 09:59:22 -0700962 notifyRequester(request);
Nathan Haroldb3014052017-01-25 15:57:32 -0800963 break;
964 }
965 Integer appType = (Integer) request.argument;
966 UiccCardApplication uiccApp = uiccCard.getApplicationByType(appType);
967 if (uiccApp == null) {
968 loge("getForbiddenPlmns() no app with specified type -- "
969 + appType);
970 request.result = new IllegalArgumentException("Failed to get UICC App");
Pengquan Menga1bb6272018-09-06 09:59:22 -0700971 notifyRequester(request);
Nathan Haroldb3014052017-01-25 15:57:32 -0800972 break;
973 } else {
974 if (DBG) logv("getForbiddenPlmns() found app " + uiccApp.getAid()
975 + " specified type -- " + appType);
976 }
977 onCompleted = obtainMessage(EVENT_GET_FORBIDDEN_PLMNS_DONE, request);
978 ((SIMRecords) uiccApp.getIccRecords()).getForbiddenPlmns(
979 onCompleted);
980 break;
981
Holly Jiuyu Sun01c47ad2018-01-24 17:56:33 +0000982 case CMD_SWITCH_SLOTS:
983 request = (MainThreadRequest) msg.obj;
984 int[] physicalSlots = (int[]) request.argument;
985 onCompleted = obtainMessage(EVENT_SWITCH_SLOTS_DONE, request);
986 UiccController.getInstance().switchSlots(physicalSlots, onCompleted);
987 break;
988
989 case EVENT_SWITCH_SLOTS_DONE:
990 ar = (AsyncResult) msg.obj;
991 request = (MainThreadRequest) ar.userObj;
992 request.result = (ar.exception == null);
Pengquan Menga1bb6272018-09-06 09:59:22 -0700993 notifyRequester(request);
994 break;
995 case CMD_GET_NETWORK_SELECTION_MODE:
996 request = (MainThreadRequest) msg.obj;
997 onCompleted = obtainMessage(EVENT_GET_NETWORK_SELECTION_MODE_DONE, request);
998 getPhoneFromRequest(request).getNetworkSelectionMode(onCompleted);
999 break;
1000
1001 case EVENT_GET_NETWORK_SELECTION_MODE_DONE:
1002 ar = (AsyncResult) msg.obj;
1003 request = (MainThreadRequest) ar.userObj;
1004 if (ar.exception != null) {
1005 request.result = TelephonyManager.NETWORK_SELECTION_MODE_UNKNOWN;
1006 } else {
1007 int mode = ((int[]) ar.result)[0];
1008 if (mode == 0) {
1009 request.result = TelephonyManager.NETWORK_SELECTION_MODE_AUTO;
1010 } else {
1011 request.result = TelephonyManager.NETWORK_SELECTION_MODE_MANUAL;
1012 }
Holly Jiuyu Sun01c47ad2018-01-24 17:56:33 +00001013 }
Pengquan Menga1bb6272018-09-06 09:59:22 -07001014 notifyRequester(request);
1015 break;
1016 case CMD_GET_CDMA_ROAMING_MODE:
1017 request = (MainThreadRequest) msg.obj;
1018 onCompleted = obtainMessage(EVENT_GET_CDMA_ROAMING_MODE_DONE, request);
1019 getPhoneFromRequest(request).queryCdmaRoamingPreference(onCompleted);
1020 break;
1021 case EVENT_GET_CDMA_ROAMING_MODE_DONE:
1022 ar = (AsyncResult) msg.obj;
1023 request = (MainThreadRequest) ar.userObj;
1024 if (ar.exception != null) {
1025 request.result = TelephonyManager.CDMA_ROAMING_MODE_RADIO_DEFAULT;
1026 } else {
1027 request.result = ((int[]) ar.result)[0];
1028 }
1029 notifyRequester(request);
1030 break;
1031 case CMD_SET_CDMA_ROAMING_MODE:
1032 request = (MainThreadRequest) msg.obj;
1033 onCompleted = obtainMessage(EVENT_SET_CDMA_ROAMING_MODE_DONE, request);
1034 int mode = (int) request.argument;
1035 getPhoneFromRequest(request).setCdmaRoamingPreference(mode, onCompleted);
1036 break;
1037 case EVENT_SET_CDMA_ROAMING_MODE_DONE:
1038 ar = (AsyncResult) msg.obj;
1039 request = (MainThreadRequest) ar.userObj;
1040 request.result = ar.exception == null;
1041 notifyRequester(request);
1042 break;
1043 case CMD_SET_CDMA_SUBSCRIPTION_MODE:
1044 request = (MainThreadRequest) msg.obj;
1045 onCompleted = obtainMessage(EVENT_SET_CDMA_SUBSCRIPTION_MODE_DONE, request);
1046 int subscriptionMode = (int) request.argument;
1047 getPhoneFromRequest(request).setCdmaSubscription(subscriptionMode, onCompleted);
1048 break;
1049 case EVENT_SET_CDMA_SUBSCRIPTION_MODE_DONE:
1050 ar = (AsyncResult) msg.obj;
1051 request = (MainThreadRequest) ar.userObj;
1052 request.result = ar.exception == null;
1053 notifyRequester(request);
Holly Jiuyu Sun01c47ad2018-01-24 17:56:33 +00001054 break;
Nathan Harold3ff88932018-08-14 10:19:49 -07001055 case CMD_GET_ALL_CELL_INFO:
1056 request = (MainThreadRequest) msg.obj;
Nathan Harold3ff88932018-08-14 10:19:49 -07001057 onCompleted = obtainMessage(EVENT_GET_ALL_CELL_INFO_DONE, request);
Nathan Harold92bed182018-10-12 18:16:49 -07001058 request.phone.requestCellInfoUpdate(request.workSource, onCompleted);
Nathan Harold3ff88932018-08-14 10:19:49 -07001059 break;
Nathan Harold3ff88932018-08-14 10:19:49 -07001060 case EVENT_GET_ALL_CELL_INFO_DONE:
1061 ar = (AsyncResult) msg.obj;
1062 request = (MainThreadRequest) ar.userObj;
Nathan Harold8d0f1742018-10-02 12:14:47 -07001063 // If a timeout occurs, the response will be null
1064 request.result = (ar.exception == null && ar.result != null)
1065 ? ar.result : new ArrayList<CellInfo>();
Nathan Harold3ff88932018-08-14 10:19:49 -07001066 synchronized (request) {
1067 request.notifyAll();
1068 }
1069 break;
Nathan Haroldfa8da0f2018-09-27 18:51:29 -07001070 case CMD_REQUEST_CELL_INFO_UPDATE:
1071 request = (MainThreadRequest) msg.obj;
1072 request.phone.requestCellInfoUpdate(request.workSource,
1073 obtainMessage(EVENT_REQUEST_CELL_INFO_UPDATE_DONE, request));
1074 break;
1075 case EVENT_REQUEST_CELL_INFO_UPDATE_DONE:
1076 ar = (AsyncResult) msg.obj;
1077 request = (MainThreadRequest) ar.userObj;
1078 ICellInfoCallback cb = (ICellInfoCallback) request.argument;
1079 try {
1080 if (ar.exception != null) {
Nathan Haroldfa8da0f2018-09-27 18:51:29 -07001081 Log.e(LOG_TAG, "Exception retrieving CellInfo=" + ar.exception);
Nathan Harolde82c4b82018-12-18 19:40:37 -08001082 cb.onError(TelephonyManager.CellInfoCallback.ERROR_MODEM_ERROR,
1083 new android.os.ParcelableException(ar.exception));
Nathan Haroldfa8da0f2018-09-27 18:51:29 -07001084 } else if (ar.result == null) {
Nathan Haroldfa8da0f2018-09-27 18:51:29 -07001085 Log.w(LOG_TAG, "Timeout Waiting for CellInfo!");
Nathan Harolde82c4b82018-12-18 19:40:37 -08001086 cb.onError(TelephonyManager.CellInfoCallback.ERROR_TIMEOUT, null);
Nathan Haroldfa8da0f2018-09-27 18:51:29 -07001087 } else {
1088 // use the result as returned
1089 cb.onCellInfo((List<CellInfo>) ar.result);
1090 }
1091 } catch (RemoteException re) {
1092 Log.w(LOG_TAG, "Discarded CellInfo due to Callback RemoteException");
1093 }
1094 break;
1095 case CMD_GET_CELL_LOCATION:
Nathan Harold3ff88932018-08-14 10:19:49 -07001096 request = (MainThreadRequest) msg.obj;
1097 WorkSource ws = (WorkSource) request.argument;
1098 Phone phone = getPhoneFromRequest(request);
1099 phone.getCellLocation(ws, obtainMessage(EVENT_GET_CELL_LOCATION_DONE, request));
1100 break;
Nathan Haroldfa8da0f2018-09-27 18:51:29 -07001101 case EVENT_GET_CELL_LOCATION_DONE:
Nathan Harold3ff88932018-08-14 10:19:49 -07001102 ar = (AsyncResult) msg.obj;
1103 request = (MainThreadRequest) ar.userObj;
1104 if (ar.exception == null) {
1105 request.result = ar.result;
1106 } else {
Nathan Haroldfa8da0f2018-09-27 18:51:29 -07001107 phone = getPhoneFromRequest(request);
Nathan Harold3ff88932018-08-14 10:19:49 -07001108 request.result = (phone.getPhoneType() == PhoneConstants.PHONE_TYPE_CDMA)
1109 ? new CdmaCellLocation() : new GsmCellLocation();
1110 }
1111
1112 synchronized (request) {
1113 request.notifyAll();
1114 }
1115 break;
chen xu6dac5ab2018-10-26 17:39:23 -07001116 case CMD_MODEM_REBOOT:
1117 request = (MainThreadRequest) msg.obj;
1118 onCompleted = obtainMessage(EVENT_RESET_MODEM_CONFIG_DONE, request);
Nazanin Bakhshif782e562018-12-11 15:15:39 -08001119 defaultPhone.rebootModem(onCompleted);
chen xu6dac5ab2018-10-26 17:39:23 -07001120 break;
chen xu6dac5ab2018-10-26 17:39:23 -07001121 case EVENT_CMD_MODEM_REBOOT_DONE:
1122 handleNullReturnEvent(msg, "rebootModem");
1123 break;
Malcolm Chen8e4ed912019-01-15 20:22:16 -08001124 case CMD_REQUEST_ENABLE_MODEM:
1125 request = (MainThreadRequest) msg.obj;
1126 boolean enable = (boolean) request.argument;
1127 onCompleted = obtainMessage(EVENT_ENABLE_MODEM_DONE, request);
Nazanin Bakhshi33d584b2019-02-27 10:44:32 -08001128 onCompleted.arg1 = enable ? 1 : 0;
Malcolm Chen8e4ed912019-01-15 20:22:16 -08001129 PhoneConfigurationManager.getInstance()
1130 .enablePhone(request.phone, enable, onCompleted);
1131 break;
1132 case EVENT_ENABLE_MODEM_DONE:
1133 ar = (AsyncResult) msg.obj;
1134 request = (MainThreadRequest) ar.userObj;
1135 request.result = (ar.exception == null);
Nazanin Bakhshif71371d2019-04-29 17:29:44 -07001136 int phoneId = request.phone.getPhoneId();
Nazanin Bakhshi33d584b2019-02-27 10:44:32 -08001137 //update the cache as modem status has changed
Nazanin Bakhshif71371d2019-04-29 17:29:44 -07001138 if ((boolean) request.result) {
1139 mPhoneConfigurationManager.addToPhoneStatusCache(phoneId, msg.arg1 == 1);
1140 updateModemStateMetrics();
1141 } else {
1142 Log.e(LOG_TAG, msg.what + " failure. Not updating modem status."
1143 + ar.exception);
1144 }
1145 notifyRequester(request);
1146 break;
1147 case CMD_GET_MODEM_STATUS:
1148 request = (MainThreadRequest) msg.obj;
1149 onCompleted = obtainMessage(EVENT_GET_MODEM_STATUS_DONE, request);
1150 PhoneConfigurationManager.getInstance()
1151 .getPhoneStatusFromModem(request.phone, onCompleted);
1152 break;
1153 case EVENT_GET_MODEM_STATUS_DONE:
1154 ar = (AsyncResult) msg.obj;
1155 request = (MainThreadRequest) ar.userObj;
1156 int id = request.phone.getPhoneId();
1157 if (ar.exception == null && ar.result != null) {
1158 request.result = ar.result;
1159 //update the cache as modem status has changed
1160 mPhoneConfigurationManager.addToPhoneStatusCache(id,
1161 (boolean) request.result);
1162 } else {
1163 // Return true if modem status cannot be retrieved. For most cases,
1164 // modem status is on. And for older version modems, GET_MODEM_STATUS
1165 // and disable modem are not supported. Modem is always on.
1166 // TODO: this should be fixed in R to support a third
1167 // status UNKNOWN b/131631629
1168 request.result = true;
1169 Log.e(LOG_TAG, msg.what + " failure. Not updating modem status."
1170 + ar.exception);
1171 }
Malcolm Chen8e4ed912019-01-15 20:22:16 -08001172 notifyRequester(request);
1173 break;
Naina Nalluri1264a9f2019-09-17 14:10:30 -07001174 case CMD_ERASE_MODEM_CONFIG:
1175 request = (MainThreadRequest) msg.obj;
1176 onCompleted = obtainMessage(EVENT_ERASE_MODEM_CONFIG_DONE, request);
1177 defaultPhone.eraseModemConfig(onCompleted);
1178 break;
1179 case EVENT_ERASE_MODEM_CONFIG_DONE:
1180 handleNullReturnEvent(msg, "eraseModemConfig");
1181 break;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001182 default:
1183 Log.w(LOG_TAG, "MainThreadHandler: unexpected message code: " + msg.what);
1184 break;
1185 }
1186 }
Jake Hambye994d462014-02-03 13:10:13 -08001187
Pengquan Menga1bb6272018-09-06 09:59:22 -07001188 private void notifyRequester(MainThreadRequest request) {
1189 synchronized (request) {
1190 request.notifyAll();
1191 }
1192 }
1193
Jake Hambye994d462014-02-03 13:10:13 -08001194 private void handleNullReturnEvent(Message msg, String command) {
1195 AsyncResult ar = (AsyncResult) msg.obj;
1196 MainThreadRequest request = (MainThreadRequest) ar.userObj;
1197 if (ar.exception == null) {
1198 request.result = true;
1199 } else {
1200 request.result = false;
1201 if (ar.exception instanceof CommandException) {
1202 loge(command + ": CommandException: " + ar.exception);
1203 } else {
1204 loge(command + ": Unknown exception");
1205 }
1206 }
Pengquan Menga1bb6272018-09-06 09:59:22 -07001207 notifyRequester(request);
Jake Hambye994d462014-02-03 13:10:13 -08001208 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001209 }
1210
1211 /**
1212 * Posts the specified command to be executed on the main thread,
1213 * waits for the request to complete, and returns the result.
1214 * @see #sendRequestAsync
1215 */
1216 private Object sendRequest(int command, Object argument) {
Nathan Harold92bed182018-10-12 18:16:49 -07001217 return sendRequest(
1218 command, argument, SubscriptionManager.INVALID_SUBSCRIPTION_ID, null, null);
vagdeviaf9a5b92018-08-15 16:01:53 -07001219 }
1220
1221 /**
1222 * Posts the specified command to be executed on the main thread,
1223 * waits for the request to complete, and returns the result.
1224 * @see #sendRequestAsync
1225 */
1226 private Object sendRequest(int command, Object argument, WorkSource workSource) {
1227 return sendRequest(command, argument, SubscriptionManager.INVALID_SUBSCRIPTION_ID,
Nathan Harold92bed182018-10-12 18:16:49 -07001228 null, workSource);
Wink Saville36469e72014-06-11 15:17:00 -07001229 }
1230
1231 /**
1232 * Posts the specified command to be executed on the main thread,
1233 * waits for the request to complete, and returns the result.
1234 * @see #sendRequestAsync
1235 */
Shishir Agrawal76d5da92014-11-09 16:17:25 -08001236 private Object sendRequest(int command, Object argument, Integer subId) {
Nathan Harold92bed182018-10-12 18:16:49 -07001237 return sendRequest(command, argument, subId, null, null);
vagdeviaf9a5b92018-08-15 16:01:53 -07001238 }
1239
1240 /**
1241 * Posts the specified command to be executed on the main thread,
1242 * waits for the request to complete, and returns the result.
1243 * @see #sendRequestAsync
1244 */
Nathan Harold92bed182018-10-12 18:16:49 -07001245 private Object sendRequest(int command, Object argument, int subId, WorkSource workSource) {
1246 return sendRequest(command, argument, subId, null, workSource);
1247 }
1248
1249 /**
1250 * Posts the specified command to be executed on the main thread,
1251 * waits for the request to complete, and returns the result.
1252 * @see #sendRequestAsync
1253 */
1254 private Object sendRequest(int command, Object argument, Phone phone, WorkSource workSource) {
1255 return sendRequest(
1256 command, argument, SubscriptionManager.INVALID_SUBSCRIPTION_ID, phone, workSource);
1257 }
1258
1259 /**
1260 * Posts the specified command to be executed on the main thread,
1261 * waits for the request to complete, and returns the result.
1262 * @see #sendRequestAsync
1263 */
1264 private Object sendRequest(
1265 int command, Object argument, Integer subId, Phone phone, WorkSource workSource) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001266 if (Looper.myLooper() == mMainThreadHandler.getLooper()) {
1267 throw new RuntimeException("This method will deadlock if called from the main thread.");
1268 }
1269
Nathan Harold92bed182018-10-12 18:16:49 -07001270 MainThreadRequest request = null;
1271 if (subId != SubscriptionManager.INVALID_SUBSCRIPTION_ID && phone != null) {
1272 throw new IllegalArgumentException("subId and phone cannot both be specified!");
1273 } else if (phone != null) {
1274 request = new MainThreadRequest(argument, phone, workSource);
1275 } else {
1276 request = new MainThreadRequest(argument, subId, workSource);
1277 }
1278
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001279 Message msg = mMainThreadHandler.obtainMessage(command, request);
1280 msg.sendToTarget();
1281
1282 // Wait for the request to complete
1283 synchronized (request) {
1284 while (request.result == null) {
1285 try {
1286 request.wait();
1287 } catch (InterruptedException e) {
1288 // Do nothing, go back and wait until the request is complete
1289 }
1290 }
1291 }
1292 return request.result;
1293 }
1294
1295 /**
1296 * Asynchronous ("fire and forget") version of sendRequest():
1297 * Posts the specified command to be executed on the main thread, and
1298 * returns immediately.
1299 * @see #sendRequest
1300 */
1301 private void sendRequestAsync(int command) {
1302 mMainThreadHandler.sendEmptyMessage(command);
1303 }
1304
1305 /**
Sailesh Nepalbd76e4e2013-10-27 13:59:44 -07001306 * Same as {@link #sendRequestAsync(int)} except it takes an argument.
Nathan Haroldfa8da0f2018-09-27 18:51:29 -07001307 * @see {@link #sendRequest(int)}
Sailesh Nepalbd76e4e2013-10-27 13:59:44 -07001308 */
1309 private void sendRequestAsync(int command, Object argument) {
Nathan Haroldfa8da0f2018-09-27 18:51:29 -07001310 sendRequestAsync(command, argument, null, null);
1311 }
1312
1313 /**
1314 * Same as {@link #sendRequestAsync(int,Object)} except it takes a Phone and WorkSource.
1315 * @see {@link #sendRequest(int,Object)}
1316 */
1317 private void sendRequestAsync(
1318 int command, Object argument, Phone phone, WorkSource workSource) {
1319 MainThreadRequest request = new MainThreadRequest(argument, phone, workSource);
Sailesh Nepalbd76e4e2013-10-27 13:59:44 -07001320 Message msg = mMainThreadHandler.obtainMessage(command, request);
1321 msg.sendToTarget();
1322 }
1323
1324 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001325 * Initialize the singleton PhoneInterfaceManager instance.
1326 * This is only done once, at startup, from PhoneApp.onCreate().
1327 */
Nazanin Bakhshif782e562018-12-11 15:15:39 -08001328 /* package */ static PhoneInterfaceManager init(PhoneGlobals app) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001329 synchronized (PhoneInterfaceManager.class) {
1330 if (sInstance == null) {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08001331 sInstance = new PhoneInterfaceManager(app);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001332 } else {
1333 Log.wtf(LOG_TAG, "init() called multiple times! sInstance = " + sInstance);
1334 }
1335 return sInstance;
1336 }
1337 }
1338
1339 /** Private constructor; @see init() */
Nazanin Bakhshif782e562018-12-11 15:15:39 -08001340 private PhoneInterfaceManager(PhoneGlobals app) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001341 mApp = app;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001342 mCM = PhoneGlobals.getInstance().mCM;
Stuart Scott981d8582015-04-21 14:09:50 -07001343 mUserManager = (UserManager) app.getSystemService(Context.USER_SERVICE);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001344 mAppOps = (AppOpsManager)app.getSystemService(Context.APP_OPS_SERVICE);
1345 mMainThreadHandler = new MainThreadHandler();
Tobias Thiererb19e1f12018-12-11 17:54:03 +00001346 mSubscriptionController = SubscriptionController.getInstance();
Nazanin Bakhshif782e562018-12-11 15:15:39 -08001347 mTelephonySharedPreferences =
1348 PreferenceManager.getDefaultSharedPreferences(mApp);
yinxub1bed742017-04-17 11:45:04 -07001349 mNetworkScanRequestTracker = new NetworkScanRequestTracker();
Malcolm Chen2c63d402018-08-14 16:00:53 -07001350 mPhoneConfigurationManager = PhoneConfigurationManager.getInstance();
Wink Saville3ab207e2014-11-20 13:07:20 -08001351
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001352 publish();
1353 }
1354
Nazanin Bakhshif782e562018-12-11 15:15:39 -08001355 private Phone getDefaultPhone() {
1356 Phone thePhone = getPhone(getDefaultSubscription());
1357 return (thePhone != null) ? thePhone : PhoneFactory.getDefaultPhone();
1358 }
1359
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001360 private void publish() {
1361 if (DBG) log("publish: " + this);
1362
1363 ServiceManager.addService("phone", this);
1364 }
1365
Stuart Scott584921c2015-01-15 17:10:34 -08001366 private Phone getPhoneFromRequest(MainThreadRequest request) {
Jordan Liu4c733742019-02-28 12:03:40 -08001367 if (request.phone != null) {
1368 return request.phone;
1369 } else {
1370 return getPhoneFromSubId(request.subId);
1371 }
1372 }
1373
1374 private Phone getPhoneFromSubId(int subId) {
1375 return (subId == SubscriptionManager.INVALID_SUBSCRIPTION_ID)
1376 ? getDefaultPhone() : getPhone(subId);
Stuart Scott584921c2015-01-15 17:10:34 -08001377 }
1378
Shishir Agrawalc04d9752016-02-19 10:41:00 -08001379 private UiccCard getUiccCardFromRequest(MainThreadRequest request) {
1380 Phone phone = getPhoneFromRequest(request);
1381 return phone == null ? null :
1382 UiccController.getInstance().getUiccCard(phone.getPhoneId());
1383 }
1384
Wink Saville36469e72014-06-11 15:17:00 -07001385 // returns phone associated with the subId.
Wink Savilleb564aae2014-10-23 10:18:09 -07001386 private Phone getPhone(int subId) {
Wink Savilleac1bdfd2014-11-20 23:04:44 -08001387 return PhoneFactory.getPhone(mSubscriptionController.getPhoneId(subId));
Wink Saville36469e72014-06-11 15:17:00 -07001388 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001389
Naina Nalluri1264a9f2019-09-17 14:10:30 -07001390 private void sendEraseModemConfig(Phone phone) {
1391 if (phone != null) {
1392 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
1393 mApp, phone.getSubId(), "eraseModemConfig");
1394 final long identity = Binder.clearCallingIdentity();
1395 try {
1396 Boolean success = (Boolean) sendRequest(CMD_ERASE_MODEM_CONFIG, null);
1397 if (DBG) log("eraseModemConfig:" + ' ' + (success ? "ok" : "fail"));
1398 } finally {
1399 Binder.restoreCallingIdentity(identity);
1400 }
1401 }
1402 }
1403
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001404 public void dial(String number) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001405 dialForSubscriber(getPreferredVoiceSubscription(), number);
Wink Saville36469e72014-06-11 15:17:00 -07001406 }
1407
Wink Savilleb564aae2014-10-23 10:18:09 -07001408 public void dialForSubscriber(int subId, String number) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001409 if (DBG) log("dial: " + number);
1410 // No permission check needed here: This is just a wrapper around the
1411 // ACTION_DIAL intent, which is available to any app since it puts up
1412 // the UI before it does anything.
1413
Malcolm Chenaa4a8532018-02-28 15:00:40 -08001414 final long identity = Binder.clearCallingIdentity();
1415 try {
1416 String url = createTelUrl(number);
1417 if (url == null) {
1418 return;
1419 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001420
Malcolm Chenaa4a8532018-02-28 15:00:40 -08001421 // PENDING: should we just silently fail if phone is offhook or ringing?
1422 PhoneConstants.State state = mCM.getState(subId);
1423 if (state != PhoneConstants.State.OFFHOOK && state != PhoneConstants.State.RINGING) {
1424 Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse(url));
1425 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
1426 mApp.startActivity(intent);
1427 }
1428 } finally {
1429 Binder.restoreCallingIdentity(identity);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001430 }
1431 }
1432
1433 public void call(String callingPackage, String number) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001434 callForSubscriber(getPreferredVoiceSubscription(), callingPackage, number);
Wink Saville36469e72014-06-11 15:17:00 -07001435 }
1436
Wink Savilleb564aae2014-10-23 10:18:09 -07001437 public void callForSubscriber(int subId, String callingPackage, String number) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001438 if (DBG) log("call: " + number);
1439
1440 // This is just a wrapper around the ACTION_CALL intent, but we still
1441 // need to do a permission check since we're calling startActivity()
1442 // from the context of the phone app.
1443 enforceCallPermission();
1444
1445 if (mAppOps.noteOp(AppOpsManager.OP_CALL_PHONE, Binder.getCallingUid(), callingPackage)
1446 != AppOpsManager.MODE_ALLOWED) {
1447 return;
1448 }
1449
Malcolm Chenaa4a8532018-02-28 15:00:40 -08001450 final long identity = Binder.clearCallingIdentity();
1451 try {
1452 String url = createTelUrl(number);
1453 if (url == null) {
1454 return;
1455 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001456
Malcolm Chenaa4a8532018-02-28 15:00:40 -08001457 boolean isValid = false;
1458 final List<SubscriptionInfo> slist = getActiveSubscriptionInfoListPrivileged();
1459 if (slist != null) {
1460 for (SubscriptionInfo subInfoRecord : slist) {
1461 if (subInfoRecord.getSubscriptionId() == subId) {
1462 isValid = true;
1463 break;
1464 }
Wink Saville3ab207e2014-11-20 13:07:20 -08001465 }
Wink Saville08874612014-08-31 19:19:58 -07001466 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08001467 if (!isValid) {
1468 return;
1469 }
Wink Saville08874612014-08-31 19:19:58 -07001470
Malcolm Chenaa4a8532018-02-28 15:00:40 -08001471 Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse(url));
1472 intent.putExtra(SUBSCRIPTION_KEY, subId);
1473 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
1474 mApp.startActivity(intent);
1475 } finally {
1476 Binder.restoreCallingIdentity(identity);
1477 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001478 }
1479
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001480 public boolean supplyPin(String pin) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001481 return supplyPinForSubscriber(getDefaultSubscription(), pin);
Wink Saville36469e72014-06-11 15:17:00 -07001482 }
1483
Wink Savilleb564aae2014-10-23 10:18:09 -07001484 public boolean supplyPinForSubscriber(int subId, String pin) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001485 int [] resultArray = supplyPinReportResultForSubscriber(subId, pin);
Wink Saville9de0f752013-10-22 19:04:03 -07001486 return (resultArray[0] == PhoneConstants.PIN_RESULT_SUCCESS) ? true : false;
1487 }
1488
1489 public boolean supplyPuk(String puk, String pin) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001490 return supplyPukForSubscriber(getDefaultSubscription(), puk, pin);
Wink Saville36469e72014-06-11 15:17:00 -07001491 }
1492
Wink Savilleb564aae2014-10-23 10:18:09 -07001493 public boolean supplyPukForSubscriber(int subId, String puk, String pin) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001494 int [] resultArray = supplyPukReportResultForSubscriber(subId, puk, pin);
Wink Saville9de0f752013-10-22 19:04:03 -07001495 return (resultArray[0] == PhoneConstants.PIN_RESULT_SUCCESS) ? true : false;
1496 }
1497
1498 /** {@hide} */
1499 public int[] supplyPinReportResult(String pin) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001500 return supplyPinReportResultForSubscriber(getDefaultSubscription(), pin);
Wink Saville36469e72014-06-11 15:17:00 -07001501 }
1502
Wink Savilleb564aae2014-10-23 10:18:09 -07001503 public int[] supplyPinReportResultForSubscriber(int subId, String pin) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001504 enforceModifyPermission();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08001505
1506 final long identity = Binder.clearCallingIdentity();
1507 try {
1508 final UnlockSim checkSimPin = new UnlockSim(getPhone(subId).getIccCard());
1509 checkSimPin.start();
1510 return checkSimPin.unlockSim(null, pin);
1511 } finally {
1512 Binder.restoreCallingIdentity(identity);
1513 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001514 }
1515
Wink Saville9de0f752013-10-22 19:04:03 -07001516 /** {@hide} */
1517 public int[] supplyPukReportResult(String puk, String pin) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001518 return supplyPukReportResultForSubscriber(getDefaultSubscription(), puk, pin);
Wink Saville36469e72014-06-11 15:17:00 -07001519 }
1520
Wink Savilleb564aae2014-10-23 10:18:09 -07001521 public int[] supplyPukReportResultForSubscriber(int subId, String puk, String pin) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001522 enforceModifyPermission();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08001523
1524 final long identity = Binder.clearCallingIdentity();
1525 try {
1526 final UnlockSim checkSimPuk = new UnlockSim(getPhone(subId).getIccCard());
1527 checkSimPuk.start();
1528 return checkSimPuk.unlockSim(puk, pin);
1529 } finally {
1530 Binder.restoreCallingIdentity(identity);
1531 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001532 }
1533
1534 /**
Wink Saville9de0f752013-10-22 19:04:03 -07001535 * Helper thread to turn async call to SimCard#supplyPin into
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001536 * a synchronous one.
1537 */
1538 private static class UnlockSim extends Thread {
1539
1540 private final IccCard mSimCard;
1541
1542 private boolean mDone = false;
Wink Saville9de0f752013-10-22 19:04:03 -07001543 private int mResult = PhoneConstants.PIN_GENERAL_FAILURE;
1544 private int mRetryCount = -1;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001545
1546 // For replies from SimCard interface
1547 private Handler mHandler;
1548
1549 // For async handler to identify request type
1550 private static final int SUPPLY_PIN_COMPLETE = 100;
1551
1552 public UnlockSim(IccCard simCard) {
1553 mSimCard = simCard;
1554 }
1555
1556 @Override
1557 public void run() {
1558 Looper.prepare();
1559 synchronized (UnlockSim.this) {
1560 mHandler = new Handler() {
1561 @Override
1562 public void handleMessage(Message msg) {
1563 AsyncResult ar = (AsyncResult) msg.obj;
1564 switch (msg.what) {
1565 case SUPPLY_PIN_COMPLETE:
1566 Log.d(LOG_TAG, "SUPPLY_PIN_COMPLETE");
1567 synchronized (UnlockSim.this) {
Wink Saville9de0f752013-10-22 19:04:03 -07001568 mRetryCount = msg.arg1;
1569 if (ar.exception != null) {
1570 if (ar.exception instanceof CommandException &&
1571 ((CommandException)(ar.exception)).getCommandError()
1572 == CommandException.Error.PASSWORD_INCORRECT) {
1573 mResult = PhoneConstants.PIN_PASSWORD_INCORRECT;
1574 } else {
1575 mResult = PhoneConstants.PIN_GENERAL_FAILURE;
1576 }
1577 } else {
1578 mResult = PhoneConstants.PIN_RESULT_SUCCESS;
1579 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001580 mDone = true;
1581 UnlockSim.this.notifyAll();
1582 }
1583 break;
1584 }
1585 }
1586 };
1587 UnlockSim.this.notifyAll();
1588 }
1589 Looper.loop();
1590 }
1591
1592 /*
1593 * Use PIN or PUK to unlock SIM card
1594 *
1595 * If PUK is null, unlock SIM card with PIN
1596 *
1597 * If PUK is not null, unlock SIM card with PUK and set PIN code
1598 */
Wink Saville9de0f752013-10-22 19:04:03 -07001599 synchronized int[] unlockSim(String puk, String pin) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001600
1601 while (mHandler == null) {
1602 try {
1603 wait();
1604 } catch (InterruptedException e) {
1605 Thread.currentThread().interrupt();
1606 }
1607 }
1608 Message callback = Message.obtain(mHandler, SUPPLY_PIN_COMPLETE);
1609
1610 if (puk == null) {
1611 mSimCard.supplyPin(pin, callback);
1612 } else {
1613 mSimCard.supplyPuk(puk, pin, callback);
1614 }
1615
1616 while (!mDone) {
1617 try {
1618 Log.d(LOG_TAG, "wait for done");
1619 wait();
1620 } catch (InterruptedException e) {
1621 // Restore the interrupted status
1622 Thread.currentThread().interrupt();
1623 }
1624 }
1625 Log.d(LOG_TAG, "done");
Wink Saville9de0f752013-10-22 19:04:03 -07001626 int[] resultArray = new int[2];
1627 resultArray[0] = mResult;
1628 resultArray[1] = mRetryCount;
1629 return resultArray;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001630 }
1631 }
1632
1633 public void updateServiceLocation() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001634 updateServiceLocationForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001635
1636 }
1637
Wink Savilleb564aae2014-10-23 10:18:09 -07001638 public void updateServiceLocationForSubscriber(int subId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001639 // No permission check needed here: this call is harmless, and it's
1640 // needed for the ServiceState.requestStateUpdate() call (which is
1641 // already intentionally exposed to 3rd parties.)
Malcolm Chenaa4a8532018-02-28 15:00:40 -08001642 final long identity = Binder.clearCallingIdentity();
1643 try {
1644 final Phone phone = getPhone(subId);
1645 if (phone != null) {
1646 phone.updateServiceLocation();
1647 }
1648 } finally {
1649 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07001650 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001651 }
1652
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001653 @Override
1654 public boolean isRadioOn(String callingPackage) {
1655 return isRadioOnForSubscriber(getDefaultSubscription(), callingPackage);
Wink Saville36469e72014-06-11 15:17:00 -07001656 }
1657
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001658 @Override
1659 public boolean isRadioOnForSubscriber(int subId, String callingPackage) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08001660 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
Jeff Davidson913390f2018-02-23 17:11:49 -08001661 mApp, subId, callingPackage, "isRadioOnForSubscriber")) {
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001662 return false;
1663 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08001664
1665 final long identity = Binder.clearCallingIdentity();
1666 try {
1667 return isRadioOnForSubscriber(subId);
1668 } finally {
1669 Binder.restoreCallingIdentity(identity);
1670 }
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001671 }
1672
1673 private boolean isRadioOnForSubscriber(int subId) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08001674 final long identity = Binder.clearCallingIdentity();
1675 try {
1676 final Phone phone = getPhone(subId);
1677 if (phone != null) {
1678 return phone.getServiceState().getState() != ServiceState.STATE_POWER_OFF;
1679 } else {
1680 return false;
1681 }
1682 } finally {
1683 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07001684 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001685 }
1686
1687 public void toggleRadioOnOff() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001688 toggleRadioOnOffForSubscriber(getDefaultSubscription());
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001689 }
Wink Saville36469e72014-06-11 15:17:00 -07001690
Wink Savilleb564aae2014-10-23 10:18:09 -07001691 public void toggleRadioOnOffForSubscriber(int subId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001692 enforceModifyPermission();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08001693
1694 final long identity = Binder.clearCallingIdentity();
1695 try {
1696 final Phone phone = getPhone(subId);
1697 if (phone != null) {
1698 phone.setRadioPower(!isRadioOnForSubscriber(subId));
1699 }
1700 } finally {
1701 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07001702 }
Wink Saville36469e72014-06-11 15:17:00 -07001703 }
1704
1705 public boolean setRadio(boolean turnOn) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001706 return setRadioForSubscriber(getDefaultSubscription(), turnOn);
Wink Saville36469e72014-06-11 15:17:00 -07001707 }
1708
Wink Savilleb564aae2014-10-23 10:18:09 -07001709 public boolean setRadioForSubscriber(int subId, boolean turnOn) {
Wink Saville36469e72014-06-11 15:17:00 -07001710 enforceModifyPermission();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08001711
1712 final long identity = Binder.clearCallingIdentity();
1713 try {
1714 final Phone phone = getPhone(subId);
1715 if (phone == null) {
1716 return false;
1717 }
1718 if ((phone.getServiceState().getState() != ServiceState.STATE_POWER_OFF) != turnOn) {
1719 toggleRadioOnOffForSubscriber(subId);
1720 }
1721 return true;
1722 } finally {
1723 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07001724 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001725 }
Wink Saville36469e72014-06-11 15:17:00 -07001726
Naveen Kalla1fd79bd2014-08-08 00:48:59 -07001727 public boolean needMobileRadioShutdown() {
1728 /*
1729 * If any of the Radios are available, it will need to be
1730 * shutdown. So return true if any Radio is available.
1731 */
Malcolm Chenaa4a8532018-02-28 15:00:40 -08001732 final long identity = Binder.clearCallingIdentity();
1733 try {
1734 for (int i = 0; i < TelephonyManager.getDefault().getPhoneCount(); i++) {
1735 Phone phone = PhoneFactory.getPhone(i);
1736 if (phone != null && phone.isRadioAvailable()) return true;
1737 }
1738 logv(TelephonyManager.getDefault().getPhoneCount() + " Phones are shutdown.");
1739 return false;
1740 } finally {
1741 Binder.restoreCallingIdentity(identity);
Naveen Kalla1fd79bd2014-08-08 00:48:59 -07001742 }
Naveen Kalla1fd79bd2014-08-08 00:48:59 -07001743 }
1744
Malcolm Chenaa4a8532018-02-28 15:00:40 -08001745 @Override
Naveen Kalla1fd79bd2014-08-08 00:48:59 -07001746 public void shutdownMobileRadios() {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08001747 enforceModifyPermission();
1748
1749 final long identity = Binder.clearCallingIdentity();
1750 try {
1751 for (int i = 0; i < TelephonyManager.getDefault().getPhoneCount(); i++) {
1752 logv("Shutting down Phone " + i);
1753 shutdownRadioUsingPhoneId(i);
1754 }
1755 } finally {
1756 Binder.restoreCallingIdentity(identity);
Naveen Kalla1fd79bd2014-08-08 00:48:59 -07001757 }
1758 }
1759
1760 private void shutdownRadioUsingPhoneId(int phoneId) {
Naveen Kalla1fd79bd2014-08-08 00:48:59 -07001761 Phone phone = PhoneFactory.getPhone(phoneId);
1762 if (phone != null && phone.isRadioAvailable()) {
1763 phone.shutdownRadio();
1764 }
1765 }
1766
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001767 public boolean setRadioPower(boolean turnOn) {
Jack Yub4e16162017-05-15 12:48:40 -07001768 enforceModifyPermission();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08001769
1770 final long identity = Binder.clearCallingIdentity();
1771 try {
1772 final Phone defaultPhone = PhoneFactory.getDefaultPhone();
1773 if (defaultPhone != null) {
1774 defaultPhone.setRadioPower(turnOn);
1775 return true;
1776 } else {
1777 loge("There's no default phone.");
1778 return false;
1779 }
1780 } finally {
1781 Binder.restoreCallingIdentity(identity);
Wei Liu9ae2a062016-08-08 11:09:34 -07001782 }
Wink Saville36469e72014-06-11 15:17:00 -07001783 }
1784
Wink Savilleb564aae2014-10-23 10:18:09 -07001785 public boolean setRadioPowerForSubscriber(int subId, boolean turnOn) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001786 enforceModifyPermission();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08001787
1788 final long identity = Binder.clearCallingIdentity();
1789 try {
1790 final Phone phone = getPhone(subId);
1791 if (phone != null) {
1792 phone.setRadioPower(turnOn);
1793 return true;
1794 } else {
1795 return false;
1796 }
1797 } finally {
1798 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07001799 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001800 }
1801
Wink Saville36469e72014-06-11 15:17:00 -07001802 // FIXME: subId version needed
Sanket Padawe356d7632015-06-22 14:03:32 -07001803 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001804 public boolean enableDataConnectivity() {
1805 enforceModifyPermission();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08001806
1807 final long identity = Binder.clearCallingIdentity();
1808 try {
1809 int subId = mSubscriptionController.getDefaultDataSubId();
1810 final Phone phone = getPhone(subId);
1811 if (phone != null) {
Jack Yud79fba22018-12-13 11:51:28 -08001812 phone.getDataEnabledSettings().setUserDataEnabled(true);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08001813 return true;
1814 } else {
1815 return false;
1816 }
1817 } finally {
1818 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07001819 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001820 }
1821
Wink Saville36469e72014-06-11 15:17:00 -07001822 // FIXME: subId version needed
Sanket Padawe356d7632015-06-22 14:03:32 -07001823 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001824 public boolean disableDataConnectivity() {
1825 enforceModifyPermission();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08001826
1827 final long identity = Binder.clearCallingIdentity();
1828 try {
1829 int subId = mSubscriptionController.getDefaultDataSubId();
1830 final Phone phone = getPhone(subId);
1831 if (phone != null) {
Jack Yud79fba22018-12-13 11:51:28 -08001832 phone.getDataEnabledSettings().setUserDataEnabled(false);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08001833 return true;
1834 } else {
1835 return false;
1836 }
1837 } finally {
1838 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07001839 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001840 }
1841
Sanket Padawe356d7632015-06-22 14:03:32 -07001842 @Override
Jack Yuacf8a132017-05-01 17:00:48 -07001843 public boolean isDataConnectivityPossible(int subId) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08001844 final long identity = Binder.clearCallingIdentity();
1845 try {
1846 final Phone phone = getPhone(subId);
1847 if (phone != null) {
Jack Yub5d8f642018-11-26 11:20:48 -08001848 return phone.isDataAllowed(ApnSetting.TYPE_DEFAULT);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08001849 } else {
1850 return false;
1851 }
1852 } finally {
1853 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07001854 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001855 }
1856
1857 public boolean handlePinMmi(String dialString) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001858 return handlePinMmiForSubscriber(getDefaultSubscription(), dialString);
Wink Saville36469e72014-06-11 15:17:00 -07001859 }
1860
pkanwarae03a6b2016-11-06 20:37:09 -08001861 public void handleUssdRequest(int subId, String ussdRequest, ResultReceiver wrappedCallback) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08001862 enforceCallPermission();
1863
1864 final long identity = Binder.clearCallingIdentity();
1865 try {
1866 if (!SubscriptionManager.isValidSubscriptionId(subId)) {
1867 return;
1868 }
1869 Pair<String, ResultReceiver> ussdObject = new Pair(ussdRequest, wrappedCallback);
1870 sendRequest(CMD_HANDLE_USSD_REQUEST, ussdObject, subId);
1871 } finally {
1872 Binder.restoreCallingIdentity(identity);
1873 }
pkanwar32d516d2016-10-14 19:37:38 -07001874 };
1875
Wink Savilleb564aae2014-10-23 10:18:09 -07001876 public boolean handlePinMmiForSubscriber(int subId, String dialString) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001877 enforceModifyPermission();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08001878
1879 final long identity = Binder.clearCallingIdentity();
1880 try {
1881 if (!SubscriptionManager.isValidSubscriptionId(subId)) {
1882 return false;
1883 }
1884 return (Boolean) sendRequest(CMD_HANDLE_PIN_MMI, dialString, subId);
1885 } finally {
1886 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07001887 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001888 }
1889
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001890 public int getCallState() {
Shishir Agrawala9f32182016-04-12 12:00:16 -07001891 return getCallStateForSlot(getSlotForDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001892 }
1893
Sanket Padawe13bac7b2017-03-20 15:04:47 -07001894 public int getCallStateForSlot(int slotIndex) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08001895 final long identity = Binder.clearCallingIdentity();
1896 try {
1897 Phone phone = PhoneFactory.getPhone(slotIndex);
1898 return phone == null ? TelephonyManager.CALL_STATE_IDLE :
1899 PhoneConstantConversions.convertCallState(phone.getState());
1900 } finally {
1901 Binder.restoreCallingIdentity(identity);
1902 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001903 }
1904
Sanket Padawe356d7632015-06-22 14:03:32 -07001905 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001906 public int getDataState() {
Nathan Haroldcbce7262019-06-14 16:58:30 -07001907 return getDataStateForSubId(mSubscriptionController.getDefaultDataSubId());
1908 }
1909
1910 @Override
1911 public int getDataStateForSubId(int subId) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08001912 final long identity = Binder.clearCallingIdentity();
1913 try {
Nathan Haroldcbce7262019-06-14 16:58:30 -07001914 final Phone phone = getPhone(subId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08001915 if (phone != null) {
1916 return PhoneConstantConversions.convertDataState(phone.getDataConnectionState());
1917 } else {
1918 return PhoneConstantConversions.convertDataState(
1919 PhoneConstants.DataState.DISCONNECTED);
1920 }
1921 } finally {
1922 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07001923 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001924 }
1925
Sanket Padawe356d7632015-06-22 14:03:32 -07001926 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001927 public int getDataActivity() {
Nathan Haroldcbce7262019-06-14 16:58:30 -07001928 return getDataActivityForSubId(mSubscriptionController.getDefaultDataSubId());
1929 }
1930
1931 @Override
1932 public int getDataActivityForSubId(int subId) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08001933 final long identity = Binder.clearCallingIdentity();
1934 try {
Nathan Haroldcbce7262019-06-14 16:58:30 -07001935 final Phone phone = getPhone(subId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08001936 if (phone != null) {
1937 return DefaultPhoneNotifier.convertDataActivityState(phone.getDataActivityState());
1938 } else {
1939 return TelephonyManager.DATA_ACTIVITY_NONE;
1940 }
1941 } finally {
1942 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07001943 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001944 }
1945
1946 @Override
Svetoslav64fad262015-04-14 14:35:21 -07001947 public Bundle getCellLocation(String callingPackage) {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08001948 mApp.getSystemService(AppOpsManager.class)
Hall Liu1aa510f2017-11-22 17:40:08 -08001949 .checkPackage(Binder.getCallingUid(), callingPackage);
Hall Liuf19c44f2018-11-27 14:38:17 -08001950
1951 LocationAccessPolicy.LocationPermissionResult locationResult =
1952 LocationAccessPolicy.checkLocationPermission(mApp,
1953 new LocationAccessPolicy.LocationPermissionQuery.Builder()
1954 .setCallingPackage(callingPackage)
1955 .setCallingPid(Binder.getCallingPid())
1956 .setCallingUid(Binder.getCallingUid())
1957 .setMethod("getCellLocation")
Hall Liu460a6de2020-01-24 18:07:12 -08001958 .setMinSdkVersionForCoarse(Build.VERSION_CODES.BASE)
Hall Liuf19c44f2018-11-27 14:38:17 -08001959 .setMinSdkVersionForFine(Build.VERSION_CODES.Q)
1960 .build());
1961 switch (locationResult) {
1962 case DENIED_HARD:
1963 throw new SecurityException("Not allowed to access cell location");
1964 case DENIED_SOFT:
1965 return new Bundle();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001966 }
1967
Narayan Kamathf04b5a12018-01-09 11:47:15 +00001968 WorkSource workSource = getWorkSource(Binder.getCallingUid());
Malcolm Chenaa4a8532018-02-28 15:00:40 -08001969 final long identity = Binder.clearCallingIdentity();
1970 try {
1971 if (DBG_LOC) log("getCellLocation: is active user");
1972 Bundle data = new Bundle();
Nathan Harold3ff88932018-08-14 10:19:49 -07001973 int subId = mSubscriptionController.getDefaultDataSubId();
1974 CellLocation cl = (CellLocation) sendRequest(CMD_GET_CELL_LOCATION, workSource, subId);
1975 cl.fillInNotifierBundle(data);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08001976 return data;
1977 } finally {
1978 Binder.restoreCallingIdentity(identity);
1979 }
Svetoslav64fad262015-04-14 14:35:21 -07001980 }
1981
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001982 @Override
Jonathan Basseribf5362b2017-07-19 12:22:35 -07001983 public String getNetworkCountryIsoForPhone(int phoneId) {
1984 // Reporting the correct network country is ambiguous when IWLAN could conflict with
1985 // registered cell info, so return a NULL country instead.
1986 final long identity = Binder.clearCallingIdentity();
1987 try {
Malcolm Chen3732c2b2018-07-18 20:15:24 -07001988 if (phoneId == SubscriptionManager.INVALID_PHONE_INDEX) {
1989 // Get default phone in this case.
1990 phoneId = SubscriptionManager.DEFAULT_PHONE_INDEX;
1991 }
Jonathan Basseribf5362b2017-07-19 12:22:35 -07001992 final int subId = mSubscriptionController.getSubIdUsingPhoneId(phoneId);
Jack Yu5f7092c2018-04-13 14:05:37 -07001993 // Todo: fix this when we can get the actual cellular network info when the device
1994 // is on IWLAN.
Jonathan Basseribf5362b2017-07-19 12:22:35 -07001995 if (TelephonyManager.NETWORK_TYPE_IWLAN
1996 == getVoiceNetworkTypeForSubscriber(subId, mApp.getPackageName())) {
1997 return "";
1998 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08001999 Phone phone = PhoneFactory.getPhone(phoneId);
2000 if (phone != null) {
2001 ServiceStateTracker sst = phone.getServiceStateTracker();
sqianb9d961a2019-07-31 20:23:45 -07002002 EmergencyNumberTracker emergencyNumberTracker = phone.getEmergencyNumberTracker();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002003 if (sst != null) {
2004 LocaleTracker lt = sst.getLocaleTracker();
2005 if (lt != null) {
sqianb9d961a2019-07-31 20:23:45 -07002006 if (!TextUtils.isEmpty(lt.getCurrentCountry())) {
2007 return lt.getCurrentCountry();
2008 } else if (emergencyNumberTracker != null) {
2009 return emergencyNumberTracker.getEmergencyCountryIso();
2010 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002011 }
2012 }
2013 }
2014 return "";
Jonathan Basseribf5362b2017-07-19 12:22:35 -07002015 } finally {
2016 Binder.restoreCallingIdentity(identity);
2017 }
Jonathan Basseribf5362b2017-07-19 12:22:35 -07002018 }
2019
2020 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002021 public void enableLocationUpdates() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07002022 enableLocationUpdatesForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07002023 }
2024
Sanket Padawe356d7632015-06-22 14:03:32 -07002025 @Override
Wink Savilleb564aae2014-10-23 10:18:09 -07002026 public void enableLocationUpdatesForSubscriber(int subId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002027 mApp.enforceCallingOrSelfPermission(
2028 android.Manifest.permission.CONTROL_LOCATION_UPDATES, null);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002029
2030 final long identity = Binder.clearCallingIdentity();
2031 try {
2032 final Phone phone = getPhone(subId);
2033 if (phone != null) {
2034 phone.enableLocationUpdates();
2035 }
2036 } finally {
2037 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07002038 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002039 }
2040
2041 @Override
2042 public void disableLocationUpdates() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07002043 disableLocationUpdatesForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07002044 }
2045
Sanket Padawe356d7632015-06-22 14:03:32 -07002046 @Override
Wink Savilleb564aae2014-10-23 10:18:09 -07002047 public void disableLocationUpdatesForSubscriber(int subId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002048 mApp.enforceCallingOrSelfPermission(
2049 android.Manifest.permission.CONTROL_LOCATION_UPDATES, null);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002050
2051 final long identity = Binder.clearCallingIdentity();
2052 try {
2053 final Phone phone = getPhone(subId);
2054 if (phone != null) {
2055 phone.disableLocationUpdates();
2056 }
2057 } finally {
2058 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07002059 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002060 }
2061
Nathan Harold31d7ff32018-10-15 20:20:30 -07002062 /**
2063 * Returns the target SDK version number for a given package name.
2064 *
Nathan Haroldba61eeb2019-07-10 17:04:16 -07002065 * This call MUST be invoked before clearing the calling UID.
2066 *
Nathan Harold31d7ff32018-10-15 20:20:30 -07002067 * @return target SDK if the package is found or INT_MAX.
2068 */
2069 private int getTargetSdk(String packageName) {
2070 try {
Nathan Haroldba61eeb2019-07-10 17:04:16 -07002071 final ApplicationInfo ai = mApp.getPackageManager().getApplicationInfoAsUser(
2072 packageName, 0, UserHandle.getUserId(Binder.getCallingUid()));
Nathan Harold31d7ff32018-10-15 20:20:30 -07002073 if (ai != null) return ai.targetSdkVersion;
2074 } catch (PackageManager.NameNotFoundException unexpected) {
Nathan Haroldba61eeb2019-07-10 17:04:16 -07002075 loge("Failed to get package info for pkg="
2076 + packageName + ", uid=" + Binder.getCallingUid());
Nathan Harold31d7ff32018-10-15 20:20:30 -07002077 }
2078 return Integer.MAX_VALUE;
2079 }
2080
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002081 @Override
2082 @SuppressWarnings("unchecked")
Nathan Harold31d7ff32018-10-15 20:20:30 -07002083 public List<NeighboringCellInfo> getNeighboringCellInfo(String callingPackage) {
2084 final int targetSdk = getTargetSdk(callingPackage);
Nathan Harolddbea45a2018-08-30 14:35:07 -07002085 if (targetSdk >= android.os.Build.VERSION_CODES.Q) {
2086 throw new SecurityException(
2087 "getNeighboringCellInfo() is unavailable to callers targeting Q+ SDK levels.");
2088 }
Nathan Haroldb4d55612018-07-20 13:13:08 -07002089
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002090 if (mAppOps.noteOp(AppOpsManager.OP_NEIGHBORING_CELLS, Binder.getCallingUid(),
2091 callingPackage) != AppOpsManager.MODE_ALLOWED) {
2092 return null;
2093 }
Svetoslav64fad262015-04-14 14:35:21 -07002094
Svetoslav Ganov4a9d4482017-06-20 19:53:35 -07002095 if (DBG_LOC) log("getNeighboringCellInfo: is active user");
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002096
Nathan Haroldf180aac2018-06-01 18:43:55 -07002097 List<CellInfo> info = getAllCellInfo(callingPackage);
2098 if (info == null) return null;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002099
Nathan Haroldf180aac2018-06-01 18:43:55 -07002100 List<NeighboringCellInfo> neighbors = new ArrayList<NeighboringCellInfo>();
2101 for (CellInfo ci : info) {
2102 if (ci instanceof CellInfoGsm) {
2103 neighbors.add(new NeighboringCellInfo((CellInfoGsm) ci));
2104 } else if (ci instanceof CellInfoWcdma) {
2105 neighbors.add(new NeighboringCellInfo((CellInfoWcdma) ci));
2106 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002107 }
Nathan Haroldf180aac2018-06-01 18:43:55 -07002108 return (neighbors.size()) > 0 ? neighbors : null;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002109 }
2110
Nathan Haroldfa8da0f2018-09-27 18:51:29 -07002111 private List<CellInfo> getCachedCellInfo() {
2112 List<CellInfo> cellInfos = new ArrayList<CellInfo>();
2113 for (Phone phone : PhoneFactory.getPhones()) {
2114 List<CellInfo> info = phone.getAllCellInfo();
2115 if (info != null) cellInfos.addAll(info);
2116 }
2117 return cellInfos;
2118 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002119
2120 @Override
Svetoslav64fad262015-04-14 14:35:21 -07002121 public List<CellInfo> getAllCellInfo(String callingPackage) {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08002122 mApp.getSystemService(AppOpsManager.class)
Hall Liu1aa510f2017-11-22 17:40:08 -08002123 .checkPackage(Binder.getCallingUid(), callingPackage);
Hall Liuf19c44f2018-11-27 14:38:17 -08002124
2125 LocationAccessPolicy.LocationPermissionResult locationResult =
2126 LocationAccessPolicy.checkLocationPermission(mApp,
2127 new LocationAccessPolicy.LocationPermissionQuery.Builder()
2128 .setCallingPackage(callingPackage)
2129 .setCallingPid(Binder.getCallingPid())
2130 .setCallingUid(Binder.getCallingUid())
2131 .setMethod("getAllCellInfo")
Nathan Harold5ae50b52019-02-20 15:46:36 -08002132 .setMinSdkVersionForCoarse(Build.VERSION_CODES.BASE)
Hall Liuf19c44f2018-11-27 14:38:17 -08002133 .setMinSdkVersionForFine(Build.VERSION_CODES.Q)
2134 .build());
2135 switch (locationResult) {
2136 case DENIED_HARD:
2137 throw new SecurityException("Not allowed to access cell info");
2138 case DENIED_SOFT:
2139 return new ArrayList<>();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002140 }
2141
Nathan Haroldfa8da0f2018-09-27 18:51:29 -07002142 final int targetSdk = getTargetSdk(callingPackage);
2143 if (targetSdk >= android.os.Build.VERSION_CODES.Q) {
2144 return getCachedCellInfo();
2145 }
2146
Svetoslav Ganov4a9d4482017-06-20 19:53:35 -07002147 if (DBG_LOC) log("getAllCellInfo: is active user");
Narayan Kamathf04b5a12018-01-09 11:47:15 +00002148 WorkSource workSource = getWorkSource(Binder.getCallingUid());
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002149 final long identity = Binder.clearCallingIdentity();
2150 try {
2151 List<CellInfo> cellInfos = new ArrayList<CellInfo>();
2152 for (Phone phone : PhoneFactory.getPhones()) {
Nathan Harold3ff88932018-08-14 10:19:49 -07002153 final List<CellInfo> info = (List<CellInfo>) sendRequest(
Nathan Harold92bed182018-10-12 18:16:49 -07002154 CMD_GET_ALL_CELL_INFO, null, phone, workSource);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002155 if (info != null) cellInfos.addAll(info);
2156 }
2157 return cellInfos;
2158 } finally {
2159 Binder.restoreCallingIdentity(identity);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002160 }
2161 }
2162
Sailesh Nepalbd76e4e2013-10-27 13:59:44 -07002163 @Override
Nathan Haroldfa8da0f2018-09-27 18:51:29 -07002164 public void requestCellInfoUpdate(int subId, ICellInfoCallback cb, String callingPackage) {
2165 requestCellInfoUpdateInternal(
2166 subId, cb, callingPackage, getWorkSource(Binder.getCallingUid()));
2167 }
2168
2169 @Override
2170 public void requestCellInfoUpdateWithWorkSource(
2171 int subId, ICellInfoCallback cb, String callingPackage, WorkSource workSource) {
2172 enforceModifyPermission();
2173 requestCellInfoUpdateInternal(subId, cb, callingPackage, workSource);
2174 }
2175
2176 private void requestCellInfoUpdateInternal(
2177 int subId, ICellInfoCallback cb, String callingPackage, WorkSource workSource) {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08002178 mApp.getSystemService(AppOpsManager.class)
Nathan Haroldfa8da0f2018-09-27 18:51:29 -07002179 .checkPackage(Binder.getCallingUid(), callingPackage);
Hall Liuf19c44f2018-11-27 14:38:17 -08002180
2181 LocationAccessPolicy.LocationPermissionResult locationResult =
2182 LocationAccessPolicy.checkLocationPermission(mApp,
2183 new LocationAccessPolicy.LocationPermissionQuery.Builder()
2184 .setCallingPackage(callingPackage)
2185 .setCallingPid(Binder.getCallingPid())
2186 .setCallingUid(Binder.getCallingUid())
2187 .setMethod("requestCellInfoUpdate")
2188 .setMinSdkVersionForFine(Build.VERSION_CODES.Q)
2189 .build());
2190 switch (locationResult) {
2191 case DENIED_HARD:
2192 throw new SecurityException("Not allowed to access cell info");
2193 case DENIED_SOFT:
Nathan Haroldb3c5da42019-05-09 10:26:08 -07002194 try {
2195 cb.onCellInfo(new ArrayList<CellInfo>());
2196 } catch (RemoteException re) {
2197 // Drop without consequences
2198 }
Hall Liuf19c44f2018-11-27 14:38:17 -08002199 return;
Nathan Haroldfa8da0f2018-09-27 18:51:29 -07002200 }
2201
Nathan Harold32e84c42019-05-09 10:13:47 -07002202
2203 final Phone phone = getPhoneFromSubId(subId);
Nathan Haroldfa8da0f2018-09-27 18:51:29 -07002204 if (phone == null) throw new IllegalArgumentException("Invalid Subscription Id: " + subId);
2205
2206 sendRequestAsync(CMD_REQUEST_CELL_INFO_UPDATE, cb, phone, workSource);
2207 }
2208
2209 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002210 public void setCellInfoListRate(int rateInMillis) {
Jack Yua8d8cb82017-01-16 10:15:34 -08002211 enforceModifyPermission();
Narayan Kamathf04b5a12018-01-09 11:47:15 +00002212 WorkSource workSource = getWorkSource(Binder.getCallingUid());
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002213
2214 final long identity = Binder.clearCallingIdentity();
2215 try {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08002216 getDefaultPhone().setCellInfoListRate(rateInMillis, workSource);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002217 } finally {
2218 Binder.restoreCallingIdentity(identity);
2219 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002220 }
2221
Shishir Agrawala9f32182016-04-12 12:00:16 -07002222 @Override
Sanket Padawe13bac7b2017-03-20 15:04:47 -07002223 public String getImeiForSlot(int slotIndex, String callingPackage) {
Jeff Davidson913390f2018-02-23 17:11:49 -08002224 Phone phone = PhoneFactory.getPhone(slotIndex);
2225 if (phone == null) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08002226 return null;
2227 }
Jeff Davidson913390f2018-02-23 17:11:49 -08002228 int subId = phone.getSubId();
Michael Groover70af6dc2018-10-01 16:23:15 -07002229 if (!TelephonyPermissions.checkCallingOrSelfReadDeviceIdentifiers(mApp, subId,
2230 callingPackage, "getImeiForSlot")) {
Jeff Davidson913390f2018-02-23 17:11:49 -08002231 return null;
2232 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002233
2234 final long identity = Binder.clearCallingIdentity();
2235 try {
2236 return phone.getImei();
2237 } finally {
2238 Binder.restoreCallingIdentity(identity);
2239 }
Shishir Agrawala9f32182016-04-12 12:00:16 -07002240 }
2241
2242 @Override
David Kelly5e06a7f2018-03-12 14:10:59 +00002243 public String getTypeAllocationCodeForSlot(int slotIndex) {
2244 Phone phone = PhoneFactory.getPhone(slotIndex);
2245 String tac = null;
2246 if (phone != null) {
2247 String imei = phone.getImei();
2248 tac = imei == null ? null : imei.substring(0, TYPE_ALLOCATION_CODE_LENGTH);
2249 }
2250 return tac;
2251 }
2252
2253 @Override
Jack Yu2af8d712017-03-15 17:14:14 -07002254 public String getMeidForSlot(int slotIndex, String callingPackage) {
Jeff Davidson913390f2018-02-23 17:11:49 -08002255 Phone phone = PhoneFactory.getPhone(slotIndex);
2256 if (phone == null) {
Jack Yu2af8d712017-03-15 17:14:14 -07002257 return null;
2258 }
Nazanin Bakhshif782e562018-12-11 15:15:39 -08002259
Jeff Davidson913390f2018-02-23 17:11:49 -08002260 int subId = phone.getSubId();
Michael Groover70af6dc2018-10-01 16:23:15 -07002261 if (!TelephonyPermissions.checkCallingOrSelfReadDeviceIdentifiers(mApp, subId,
2262 callingPackage, "getMeidForSlot")) {
Jeff Davidson913390f2018-02-23 17:11:49 -08002263 return null;
2264 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002265
2266 final long identity = Binder.clearCallingIdentity();
2267 try {
2268 return phone.getMeid();
2269 } finally {
2270 Binder.restoreCallingIdentity(identity);
2271 }
Jack Yu2af8d712017-03-15 17:14:14 -07002272 }
2273
2274 @Override
David Kelly5e06a7f2018-03-12 14:10:59 +00002275 public String getManufacturerCodeForSlot(int slotIndex) {
2276 Phone phone = PhoneFactory.getPhone(slotIndex);
2277 String manufacturerCode = null;
2278 if (phone != null) {
2279 String meid = phone.getMeid();
2280 manufacturerCode = meid == null ? null : meid.substring(0, MANUFACTURER_CODE_LENGTH);
2281 }
2282 return manufacturerCode;
2283 }
2284
2285 @Override
Sanket Padawe13bac7b2017-03-20 15:04:47 -07002286 public String getDeviceSoftwareVersionForSlot(int slotIndex, String callingPackage) {
Jeff Davidson913390f2018-02-23 17:11:49 -08002287 Phone phone = PhoneFactory.getPhone(slotIndex);
2288 if (phone == null) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08002289 return null;
2290 }
Jeff Davidson913390f2018-02-23 17:11:49 -08002291 int subId = phone.getSubId();
2292 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
2293 mApp, subId, callingPackage, "getDeviceSoftwareVersionForSlot")) {
2294 return null;
2295 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002296
2297 final long identity = Binder.clearCallingIdentity();
2298 try {
2299 return phone.getDeviceSvn();
2300 } finally {
2301 Binder.restoreCallingIdentity(identity);
2302 }
Shishir Agrawala9f32182016-04-12 12:00:16 -07002303 }
2304
fionaxu43304da2017-11-27 22:51:16 -08002305 @Override
2306 public int getSubscriptionCarrierId(int subId) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002307 final long identity = Binder.clearCallingIdentity();
2308 try {
2309 final Phone phone = getPhone(subId);
2310 return phone == null ? TelephonyManager.UNKNOWN_CARRIER_ID : phone.getCarrierId();
2311 } finally {
2312 Binder.restoreCallingIdentity(identity);
2313 }
fionaxu43304da2017-11-27 22:51:16 -08002314 }
2315
2316 @Override
2317 public String getSubscriptionCarrierName(int subId) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002318 final long identity = Binder.clearCallingIdentity();
2319 try {
2320 final Phone phone = getPhone(subId);
2321 return phone == null ? null : phone.getCarrierName();
2322 } finally {
2323 Binder.restoreCallingIdentity(identity);
2324 }
fionaxu43304da2017-11-27 22:51:16 -08002325 }
2326
calvinpanffe225e2018-11-01 19:43:06 +08002327 @Override
chen xu0026ca62019-03-06 15:28:50 -08002328 public int getSubscriptionSpecificCarrierId(int subId) {
chen xu25637222018-11-04 17:17:00 -08002329 final long identity = Binder.clearCallingIdentity();
2330 try {
2331 final Phone phone = getPhone(subId);
2332 return phone == null ? TelephonyManager.UNKNOWN_CARRIER_ID
chen xu0026ca62019-03-06 15:28:50 -08002333 : phone.getSpecificCarrierId();
chen xu25637222018-11-04 17:17:00 -08002334 } finally {
2335 Binder.restoreCallingIdentity(identity);
2336 }
2337 }
2338
2339 @Override
chen xu0026ca62019-03-06 15:28:50 -08002340 public String getSubscriptionSpecificCarrierName(int subId) {
chen xu25637222018-11-04 17:17:00 -08002341 final long identity = Binder.clearCallingIdentity();
2342 try {
2343 final Phone phone = getPhone(subId);
chen xu0026ca62019-03-06 15:28:50 -08002344 return phone == null ? null : phone.getSpecificCarrierName();
chen xu25637222018-11-04 17:17:00 -08002345 } finally {
2346 Binder.restoreCallingIdentity(identity);
2347 }
2348 }
2349
chen xu651eec72018-11-11 19:03:44 -08002350 @Override
chen xu864e11c2018-12-06 22:10:03 -08002351 public int getCarrierIdFromMccMnc(int slotIndex, String mccmnc, boolean isSubscriptionMccMnc) {
2352 if (!isSubscriptionMccMnc) {
2353 enforceReadPrivilegedPermission("getCarrierIdFromMccMnc");
2354 }
chen xu651eec72018-11-11 19:03:44 -08002355 final Phone phone = PhoneFactory.getPhone(slotIndex);
2356 if (phone == null) {
2357 return TelephonyManager.UNKNOWN_CARRIER_ID;
2358 }
2359 final long identity = Binder.clearCallingIdentity();
2360 try {
2361 return CarrierResolver.getCarrierIdFromMccMnc(phone.getContext(), mccmnc);
2362 } finally {
2363 Binder.restoreCallingIdentity(identity);
2364 }
2365 }
2366
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002367 //
2368 // Internal helper methods.
2369 //
2370
Sanket Padaweee13a9b2016-03-08 17:30:28 -08002371 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002372 * Make sure the caller has the MODIFY_PHONE_STATE permission.
2373 *
2374 * @throws SecurityException if the caller does not have the required permission
2375 */
2376 private void enforceModifyPermission() {
2377 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.MODIFY_PHONE_STATE, null);
2378 }
2379
2380 /**
2381 * Make sure the caller has the CALL_PHONE permission.
2382 *
2383 * @throws SecurityException if the caller does not have the required permission
2384 */
2385 private void enforceCallPermission() {
2386 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.CALL_PHONE, null);
2387 }
2388
Stuart Scott8eef64f2015-04-08 15:13:54 -07002389 private void enforceConnectivityInternalPermission() {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08002390 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.CONNECTIVITY_INTERNAL,
Stuart Scott8eef64f2015-04-08 15:13:54 -07002391 "ConnectivityService");
2392 }
2393
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002394 private String createTelUrl(String number) {
2395 if (TextUtils.isEmpty(number)) {
2396 return null;
2397 }
2398
Jake Hambye994d462014-02-03 13:10:13 -08002399 return "tel:" + number;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002400 }
2401
Ihab Awadf9e92732013-12-05 18:02:52 -08002402 private static void log(String msg) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002403 Log.d(LOG_TAG, "[PhoneIntfMgr] " + msg);
2404 }
2405
Naveen Kalla1fd79bd2014-08-08 00:48:59 -07002406 private static void logv(String msg) {
2407 Log.v(LOG_TAG, "[PhoneIntfMgr] " + msg);
2408 }
2409
Ihab Awadf9e92732013-12-05 18:02:52 -08002410 private static void loge(String msg) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002411 Log.e(LOG_TAG, "[PhoneIntfMgr] " + msg);
2412 }
2413
Robert Greenwalt36b23af2015-07-06 17:59:14 -07002414 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002415 public int getActivePhoneType() {
Shishir Agrawala9f32182016-04-12 12:00:16 -07002416 return getActivePhoneTypeForSlot(getSlotForDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07002417 }
2418
Sanket Padawe356d7632015-06-22 14:03:32 -07002419 @Override
Sanket Padawe13bac7b2017-03-20 15:04:47 -07002420 public int getActivePhoneTypeForSlot(int slotIndex) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002421 final long identity = Binder.clearCallingIdentity();
2422 try {
2423 final Phone phone = PhoneFactory.getPhone(slotIndex);
2424 if (phone == null) {
2425 return PhoneConstants.PHONE_TYPE_NONE;
2426 } else {
2427 return phone.getPhoneType();
2428 }
2429 } finally {
2430 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07002431 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002432 }
2433
2434 /**
2435 * Returns the CDMA ERI icon index to display
2436 */
Robert Greenwalt36b23af2015-07-06 17:59:14 -07002437 @Override
2438 public int getCdmaEriIconIndex(String callingPackage) {
2439 return getCdmaEriIconIndexForSubscriber(getDefaultSubscription(), callingPackage);
Wink Saville36469e72014-06-11 15:17:00 -07002440 }
2441
Sanket Padawe356d7632015-06-22 14:03:32 -07002442 @Override
Robert Greenwalt36b23af2015-07-06 17:59:14 -07002443 public int getCdmaEriIconIndexForSubscriber(int subId, String callingPackage) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08002444 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
Jeff Davidson913390f2018-02-23 17:11:49 -08002445 mApp, subId, callingPackage, "getCdmaEriIconIndexForSubscriber")) {
Robert Greenwalt36b23af2015-07-06 17:59:14 -07002446 return -1;
2447 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002448
2449 final long identity = Binder.clearCallingIdentity();
2450 try {
2451 final Phone phone = getPhone(subId);
2452 if (phone != null) {
2453 return phone.getCdmaEriIconIndex();
2454 } else {
2455 return -1;
2456 }
2457 } finally {
2458 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07002459 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002460 }
2461
2462 /**
2463 * Returns the CDMA ERI icon mode,
2464 * 0 - ON
2465 * 1 - FLASHING
2466 */
Robert Greenwalt36b23af2015-07-06 17:59:14 -07002467 @Override
2468 public int getCdmaEriIconMode(String callingPackage) {
2469 return getCdmaEriIconModeForSubscriber(getDefaultSubscription(), callingPackage);
Wink Saville36469e72014-06-11 15:17:00 -07002470 }
2471
Sanket Padawe356d7632015-06-22 14:03:32 -07002472 @Override
Robert Greenwalt36b23af2015-07-06 17:59:14 -07002473 public int getCdmaEriIconModeForSubscriber(int subId, String callingPackage) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08002474 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
Jeff Davidson913390f2018-02-23 17:11:49 -08002475 mApp, subId, callingPackage, "getCdmaEriIconModeForSubscriber")) {
Robert Greenwalt36b23af2015-07-06 17:59:14 -07002476 return -1;
2477 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002478
2479 final long identity = Binder.clearCallingIdentity();
2480 try {
2481 final Phone phone = getPhone(subId);
2482 if (phone != null) {
2483 return phone.getCdmaEriIconMode();
2484 } else {
2485 return -1;
2486 }
2487 } finally {
2488 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07002489 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002490 }
2491
2492 /**
2493 * Returns the CDMA ERI text,
2494 */
Robert Greenwalt36b23af2015-07-06 17:59:14 -07002495 @Override
2496 public String getCdmaEriText(String callingPackage) {
2497 return getCdmaEriTextForSubscriber(getDefaultSubscription(), callingPackage);
Wink Saville36469e72014-06-11 15:17:00 -07002498 }
2499
Sanket Padawe356d7632015-06-22 14:03:32 -07002500 @Override
Robert Greenwalt36b23af2015-07-06 17:59:14 -07002501 public String getCdmaEriTextForSubscriber(int subId, String callingPackage) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08002502 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
Jeff Davidson913390f2018-02-23 17:11:49 -08002503 mApp, subId, callingPackage, "getCdmaEriIconTextForSubscriber")) {
Robert Greenwalt36b23af2015-07-06 17:59:14 -07002504 return null;
2505 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002506
2507 final long identity = Binder.clearCallingIdentity();
2508 try {
2509 final Phone phone = getPhone(subId);
2510 if (phone != null) {
2511 return phone.getCdmaEriText();
2512 } else {
2513 return null;
2514 }
2515 } finally {
2516 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07002517 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002518 }
2519
2520 /**
Junda Liuca05d5d2014-08-14 22:36:34 -07002521 * Returns the CDMA MDN.
2522 */
Sanket Padawe356d7632015-06-22 14:03:32 -07002523 @Override
Wink Savilleb564aae2014-10-23 10:18:09 -07002524 public String getCdmaMdn(int subId) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08002525 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
2526 mApp, subId, "getCdmaMdn");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002527
2528 final long identity = Binder.clearCallingIdentity();
2529 try {
2530 final Phone phone = getPhone(subId);
Nazanin Bakhshif782e562018-12-11 15:15:39 -08002531 if (phone != null && phone.getPhoneType() == PhoneConstants.PHONE_TYPE_CDMA) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002532 return phone.getLine1Number();
2533 } else {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08002534 loge("getCdmaMdn: no phone found. Invalid subId: " + subId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002535 return null;
2536 }
2537 } finally {
2538 Binder.restoreCallingIdentity(identity);
Junda Liuca05d5d2014-08-14 22:36:34 -07002539 }
2540 }
2541
2542 /**
2543 * Returns the CDMA MIN.
2544 */
Sanket Padawe356d7632015-06-22 14:03:32 -07002545 @Override
Wink Savilleb564aae2014-10-23 10:18:09 -07002546 public String getCdmaMin(int subId) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08002547 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
2548 mApp, subId, "getCdmaMin");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002549
2550 final long identity = Binder.clearCallingIdentity();
2551 try {
2552 final Phone phone = getPhone(subId);
2553 if (phone != null && phone.getPhoneType() == PhoneConstants.PHONE_TYPE_CDMA) {
2554 return phone.getCdmaMin();
2555 } else {
2556 return null;
2557 }
2558 } finally {
2559 Binder.restoreCallingIdentity(identity);
Junda Liuca05d5d2014-08-14 22:36:34 -07002560 }
2561 }
2562
Hall Liud892bec2018-11-30 14:51:45 -08002563 @Override
2564 public void requestNumberVerification(PhoneNumberRange range, long timeoutMillis,
2565 INumberVerificationCallback callback, String callingPackage) {
2566 if (mApp.checkCallingOrSelfPermission(android.Manifest.permission.MODIFY_PHONE_STATE)
2567 != PERMISSION_GRANTED) {
2568 throw new SecurityException("Caller must hold the MODIFY_PHONE_STATE permission");
2569 }
2570 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
2571
2572 String authorizedPackage = NumberVerificationManager.getAuthorizedPackage(mApp);
2573 if (!TextUtils.equals(callingPackage, authorizedPackage)) {
2574 throw new SecurityException("Calling package must be configured in the device config");
2575 }
2576
2577 if (range == null) {
2578 throw new NullPointerException("Range must be non-null");
2579 }
2580
2581 timeoutMillis = Math.min(timeoutMillis,
Hall Liubd069e32019-02-28 18:56:30 -08002582 TelephonyManager.getMaxNumberVerificationTimeoutMillis());
Hall Liud892bec2018-11-30 14:51:45 -08002583
2584 NumberVerificationManager.getInstance().requestVerification(range, callback, timeoutMillis);
2585 }
2586
Junda Liuca05d5d2014-08-14 22:36:34 -07002587 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002588 * Returns true if CDMA provisioning needs to run.
2589 */
2590 public boolean needsOtaServiceProvisioning() {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002591 final long identity = Binder.clearCallingIdentity();
2592 try {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08002593 return getDefaultPhone().needsOtaServiceProvisioning();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002594 } finally {
2595 Binder.restoreCallingIdentity(identity);
2596 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002597 }
2598
2599 /**
Shishir Agrawal76d5da92014-11-09 16:17:25 -08002600 * Sets the voice mail number of a given subId.
2601 */
2602 @Override
2603 public boolean setVoiceMailNumber(int subId, String alphaTag, String number) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08002604 TelephonyPermissions.enforceCallingOrSelfCarrierPrivilege(subId, "setVoiceMailNumber");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002605
2606 final long identity = Binder.clearCallingIdentity();
2607 try {
2608 Boolean success = (Boolean) sendRequest(CMD_SET_VOICEMAIL_NUMBER,
2609 new Pair<String, String>(alphaTag, number), new Integer(subId));
2610 return success;
2611 } finally {
2612 Binder.restoreCallingIdentity(identity);
2613 }
Shishir Agrawal76d5da92014-11-09 16:17:25 -08002614 }
2615
Ta-wei Yen87c49842016-05-13 21:19:52 -07002616 @Override
Ta-wei Yenc9df0432017-04-17 17:09:07 -07002617 public Bundle getVisualVoicemailSettings(String callingPackage, int subId) {
2618 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
Nazanin Bakhshif782e562018-12-11 15:15:39 -08002619 String systemDialer = TelecomManager.from(mApp).getSystemDialerPackage();
Ta-wei Yenc9df0432017-04-17 17:09:07 -07002620 if (!TextUtils.equals(callingPackage, systemDialer)) {
2621 throw new SecurityException("caller must be system dialer");
2622 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002623
2624 final long identity = Binder.clearCallingIdentity();
2625 try {
2626 PhoneAccountHandle phoneAccountHandle = PhoneAccountHandleConverter.fromSubId(subId);
2627 if (phoneAccountHandle == null) {
2628 return null;
2629 }
Nazanin Bakhshif782e562018-12-11 15:15:39 -08002630 return VisualVoicemailSettingsUtil.dump(mApp, phoneAccountHandle);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002631 } finally {
2632 Binder.restoreCallingIdentity(identity);
Ta-wei Yenc9df0432017-04-17 17:09:07 -07002633 }
Ta-wei Yenc9df0432017-04-17 17:09:07 -07002634 }
2635
2636 @Override
Ta-wei Yen409ac562017-03-06 16:00:44 -08002637 public String getVisualVoicemailPackageName(String callingPackage, int subId) {
Ta-wei Yendca928f2017-01-10 16:17:08 -08002638 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
Jeff Davidson7e17e312018-02-13 18:17:36 -08002639 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
Jeff Davidson913390f2018-02-23 17:11:49 -08002640 mApp, subId, callingPackage, "getVisualVoicemailPackageName")) {
Ta-wei Yendca928f2017-01-10 16:17:08 -08002641 return null;
2642 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002643
Jeff Davidsona8e4e242018-03-15 17:16:18 -07002644 final long identity = Binder.clearCallingIdentity();
2645 try {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08002646 return RemoteVvmTaskManager.getRemotePackage(mApp, subId).getPackageName();
Jeff Davidsona8e4e242018-03-15 17:16:18 -07002647 } finally {
2648 Binder.restoreCallingIdentity(identity);
2649 }
Ta-wei Yendca928f2017-01-10 16:17:08 -08002650 }
2651
2652 @Override
Ta-wei Yenb6929602016-05-24 15:48:27 -07002653 public void enableVisualVoicemailSmsFilter(String callingPackage, int subId,
2654 VisualVoicemailSmsFilterSettings settings) {
2655 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002656
2657 final long identity = Binder.clearCallingIdentity();
2658 try {
2659 VisualVoicemailSmsFilterConfig.enableVisualVoicemailSmsFilter(
Nazanin Bakhshif782e562018-12-11 15:15:39 -08002660 mApp, callingPackage, subId, settings);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002661 } finally {
2662 Binder.restoreCallingIdentity(identity);
2663 }
Ta-wei Yen87c49842016-05-13 21:19:52 -07002664 }
2665
2666 @Override
Ta-wei Yenb6929602016-05-24 15:48:27 -07002667 public void disableVisualVoicemailSmsFilter(String callingPackage, int subId) {
2668 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002669
2670 final long identity = Binder.clearCallingIdentity();
2671 try {
2672 VisualVoicemailSmsFilterConfig.disableVisualVoicemailSmsFilter(
Nazanin Bakhshif782e562018-12-11 15:15:39 -08002673 mApp, callingPackage, subId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002674 } finally {
2675 Binder.restoreCallingIdentity(identity);
2676 }
Ta-wei Yen87c49842016-05-13 21:19:52 -07002677 }
2678
2679 @Override
Ta-wei Yenb6929602016-05-24 15:48:27 -07002680 public VisualVoicemailSmsFilterSettings getVisualVoicemailSmsFilterSettings(
2681 String callingPackage, int subId) {
2682 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002683
2684 final long identity = Binder.clearCallingIdentity();
2685 try {
2686 return VisualVoicemailSmsFilterConfig.getVisualVoicemailSmsFilterSettings(
Nazanin Bakhshif782e562018-12-11 15:15:39 -08002687 mApp, callingPackage, subId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002688 } finally {
2689 Binder.restoreCallingIdentity(identity);
2690 }
Ta-wei Yen87c49842016-05-13 21:19:52 -07002691 }
2692
2693 @Override
Ta-wei Yen30a69c82016-12-27 14:52:32 -08002694 public VisualVoicemailSmsFilterSettings getActiveVisualVoicemailSmsFilterSettings(int subId) {
Brad Ebinger35c841c2018-10-01 10:40:55 -07002695 enforceReadPrivilegedPermission("getActiveVisualVoicemailSmsFilterSettings");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002696
2697 final long identity = Binder.clearCallingIdentity();
2698 try {
2699 return VisualVoicemailSmsFilterConfig.getActiveVisualVoicemailSmsFilterSettings(
Nazanin Bakhshif782e562018-12-11 15:15:39 -08002700 mApp, subId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002701 } finally {
2702 Binder.restoreCallingIdentity(identity);
2703 }
Ta-wei Yen30a69c82016-12-27 14:52:32 -08002704 }
2705
2706 @Override
2707 public void sendVisualVoicemailSmsForSubscriber(String callingPackage, int subId,
2708 String number, int port, String text, PendingIntent sentIntent) {
2709 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
Ta-wei Yen527a9c02017-01-06 15:29:25 -08002710 enforceVisualVoicemailPackage(callingPackage, subId);
Ta-wei Yen30a69c82016-12-27 14:52:32 -08002711 enforceSendSmsPermission();
Amit Mahajandccb3f12019-05-13 13:48:32 -07002712 SmsController smsController = PhoneFactory.getSmsController();
2713 smsController.sendVisualVoicemailSmsForSubscriber(callingPackage, subId, number, port, text,
2714 sentIntent);
Ta-wei Yen87c49842016-05-13 21:19:52 -07002715 }
Amit Mahajandccb3f12019-05-13 13:48:32 -07002716
Shishir Agrawal76d5da92014-11-09 16:17:25 -08002717 /**
fionaxu0152e512016-11-14 13:36:14 -08002718 * Sets the voice activation state of a given subId.
2719 */
2720 @Override
2721 public void setVoiceActivationState(int subId, int activationState) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08002722 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
2723 mApp, subId, "setVoiceActivationState");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002724
2725 final long identity = Binder.clearCallingIdentity();
2726 try {
2727 final Phone phone = getPhone(subId);
2728 if (phone != null) {
2729 phone.setVoiceActivationState(activationState);
2730 } else {
2731 loge("setVoiceActivationState fails with invalid subId: " + subId);
2732 }
2733 } finally {
2734 Binder.restoreCallingIdentity(identity);
fionaxu0152e512016-11-14 13:36:14 -08002735 }
2736 }
2737
2738 /**
2739 * Sets the data activation state of a given subId.
2740 */
2741 @Override
2742 public void setDataActivationState(int subId, int activationState) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08002743 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
2744 mApp, subId, "setDataActivationState");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002745
2746 final long identity = Binder.clearCallingIdentity();
2747 try {
2748 final Phone phone = getPhone(subId);
2749 if (phone != null) {
2750 phone.setDataActivationState(activationState);
2751 } else {
2752 loge("setVoiceActivationState fails with invalid subId: " + subId);
2753 }
2754 } finally {
2755 Binder.restoreCallingIdentity(identity);
fionaxu0152e512016-11-14 13:36:14 -08002756 }
2757 }
2758
2759 /**
2760 * Returns the voice activation state of a given subId.
2761 */
2762 @Override
2763 public int getVoiceActivationState(int subId, String callingPackage) {
Brad Ebinger35c841c2018-10-01 10:40:55 -07002764 enforceReadPrivilegedPermission("getVoiceActivationState");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002765
fionaxu0152e512016-11-14 13:36:14 -08002766 final Phone phone = getPhone(subId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002767 final long identity = Binder.clearCallingIdentity();
2768 try {
2769 if (phone != null) {
2770 return phone.getVoiceActivationState();
2771 } else {
2772 return TelephonyManager.SIM_ACTIVATION_STATE_UNKNOWN;
2773 }
2774 } finally {
2775 Binder.restoreCallingIdentity(identity);
fionaxu0152e512016-11-14 13:36:14 -08002776 }
2777 }
2778
2779 /**
2780 * Returns the data activation state of a given subId.
2781 */
2782 @Override
2783 public int getDataActivationState(int subId, String callingPackage) {
Brad Ebinger35c841c2018-10-01 10:40:55 -07002784 enforceReadPrivilegedPermission("getDataActivationState");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002785
fionaxu0152e512016-11-14 13:36:14 -08002786 final Phone phone = getPhone(subId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002787 final long identity = Binder.clearCallingIdentity();
2788 try {
2789 if (phone != null) {
2790 return phone.getDataActivationState();
2791 } else {
2792 return TelephonyManager.SIM_ACTIVATION_STATE_UNKNOWN;
2793 }
2794 } finally {
2795 Binder.restoreCallingIdentity(identity);
fionaxu0152e512016-11-14 13:36:14 -08002796 }
2797 }
2798
2799 /**
Wink Saville36469e72014-06-11 15:17:00 -07002800 * Returns the unread count of voicemails for a subId
2801 */
Sanket Padawe356d7632015-06-22 14:03:32 -07002802 @Override
Brad Ebingerf7664ba2018-11-29 12:43:38 -08002803 public int getVoiceMessageCountForSubscriber(int subId, String callingPackage) {
2804 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
2805 mApp, subId, callingPackage, "getVoiceMessageCountForSubscriber")) {
2806 return 0;
2807 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002808 final long identity = Binder.clearCallingIdentity();
2809 try {
2810 final Phone phone = getPhone(subId);
2811 if (phone != null) {
2812 return phone.getVoiceMessageCount();
2813 } else {
2814 return 0;
2815 }
2816 } finally {
2817 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07002818 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002819 }
2820
2821 /**
pkanwar8a4dcfb2017-01-19 13:43:16 -08002822 * returns true, if the device is in a state where both voice and data
2823 * are supported simultaneously. This can change based on location or network condition.
2824 */
2825 @Override
2826 public boolean isConcurrentVoiceAndDataAllowed(int subId) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002827 final long identity = Binder.clearCallingIdentity();
2828 try {
2829 final Phone phone = getPhone(subId);
2830 return (phone == null ? false : phone.isConcurrentVoiceAndDataAllowed());
2831 } finally {
2832 Binder.restoreCallingIdentity(identity);
2833 }
pkanwar8a4dcfb2017-01-19 13:43:16 -08002834 }
2835
2836 /**
fionaxu235cc5e2017-03-06 22:25:57 -08002837 * Send the dialer code if called from the current default dialer or the caller has
2838 * carrier privilege.
2839 * @param inputCode The dialer code to send
2840 */
2841 @Override
2842 public void sendDialerSpecialCode(String callingPackage, String inputCode) {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08002843 final Phone defaultPhone = getDefaultPhone();
fionaxu235cc5e2017-03-06 22:25:57 -08002844 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
Nazanin Bakhshif782e562018-12-11 15:15:39 -08002845 String defaultDialer = TelecomManager.from(defaultPhone.getContext())
2846 .getDefaultDialerPackage();
fionaxu235cc5e2017-03-06 22:25:57 -08002847 if (!TextUtils.equals(callingPackage, defaultDialer)) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08002848 TelephonyPermissions.enforceCallingOrSelfCarrierPrivilege(
2849 getDefaultSubscription(), "sendDialerSpecialCode");
fionaxu235cc5e2017-03-06 22:25:57 -08002850 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002851
2852 final long identity = Binder.clearCallingIdentity();
2853 try {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08002854 defaultPhone.sendDialerSpecialCode(inputCode);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002855 } finally {
2856 Binder.restoreCallingIdentity(identity);
2857 }
fionaxu235cc5e2017-03-06 22:25:57 -08002858 }
2859
Pengquan Menga1bb6272018-09-06 09:59:22 -07002860 @Override
2861 public int getNetworkSelectionMode(int subId) {
Pengquan Menge92a50d2018-09-21 15:54:48 -07002862 if (!isActiveSubscription(subId)) {
2863 return TelephonyManager.NETWORK_SELECTION_MODE_UNKNOWN;
2864 }
2865
Pengquan Menga1bb6272018-09-06 09:59:22 -07002866 return (int) sendRequest(CMD_GET_NETWORK_SELECTION_MODE, null /* argument */, subId);
2867 }
2868
Brad Ebinger35c841c2018-10-01 10:40:55 -07002869 @Override
Brad Ebingerb2b65522019-03-15 13:48:47 -07002870 public boolean isInEmergencySmsMode() {
2871 enforceReadPrivilegedPermission("isInEmergencySmsMode");
2872 final long identity = Binder.clearCallingIdentity();
2873 try {
2874 for (Phone phone : PhoneFactory.getPhones()) {
2875 if (phone.isInEmergencySmsMode()) {
2876 return true;
2877 }
2878 }
2879 } finally {
2880 Binder.restoreCallingIdentity(identity);
2881 }
2882 return false;
2883 }
2884
2885 @Override
Brad Ebinger9878b0b2018-11-08 17:43:22 -08002886 public void registerImsRegistrationCallback(int subId, IImsRegistrationCallback c)
2887 throws RemoteException {
2888 enforceReadPrivilegedPermission("registerImsRegistrationCallback");
Brad Ebinger35c841c2018-10-01 10:40:55 -07002889 final long token = Binder.clearCallingIdentity();
2890 try {
Brad Ebinger4ae57f92019-01-09 16:51:30 -08002891 // TODO: Refactor to remove ImsManager dependence and query through ImsPhone directly.
Nazanin Bakhshif782e562018-12-11 15:15:39 -08002892 ImsManager.getInstance(mApp, getSlotIndexOrException(subId))
Brad Ebinger35c841c2018-10-01 10:40:55 -07002893 .addRegistrationCallbackForSubscription(c, subId);
2894 } finally {
2895 Binder.restoreCallingIdentity(token);
2896 }
2897 }
2898
2899 @Override
Brad Ebinger9878b0b2018-11-08 17:43:22 -08002900 public void unregisterImsRegistrationCallback(int subId, IImsRegistrationCallback c) {
2901 enforceReadPrivilegedPermission("unregisterImsRegistrationCallback");
Brad Ebinger4ae57f92019-01-09 16:51:30 -08002902 if (!SubscriptionManager.isValidSubscriptionId(subId)) {
2903 throw new IllegalArgumentException("Invalid Subscription ID: " + subId);
2904 }
2905 Binder.withCleanCallingIdentity(() -> {
2906 try {
2907 // TODO: Refactor to remove ImsManager dependence and query through ImsPhone.
Nazanin Bakhshif782e562018-12-11 15:15:39 -08002908 ImsManager.getInstance(mApp, getSlotIndexOrException(subId))
Brad Ebinger4ae57f92019-01-09 16:51:30 -08002909 .removeRegistrationCallbackForSubscription(c, subId);
2910 } catch (IllegalArgumentException e) {
2911 Log.i(LOG_TAG, "unregisterImsRegistrationCallback: " + subId
2912 + "is inactive, ignoring unregister.");
2913 // If the subscription is no longer active, just return, since the callback
2914 // will already have been removed internally.
2915 }
2916 });
Brad Ebinger35c841c2018-10-01 10:40:55 -07002917 }
2918
2919 @Override
Brad Ebinger9878b0b2018-11-08 17:43:22 -08002920 public void registerMmTelCapabilityCallback(int subId, IImsCapabilityCallback c)
2921 throws RemoteException {
2922 enforceReadPrivilegedPermission("registerMmTelCapabilityCallback");
Brad Ebinger35c841c2018-10-01 10:40:55 -07002923 // TODO: Refactor to remove ImsManager dependence and query through ImsPhone directly.
2924 final long token = Binder.clearCallingIdentity();
2925 try {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08002926 ImsManager.getInstance(mApp, getSlotIndexOrException(subId))
Brad Ebinger35c841c2018-10-01 10:40:55 -07002927 .addCapabilitiesCallbackForSubscription(c, subId);
2928 } finally {
2929 Binder.restoreCallingIdentity(token);
2930 }
2931 }
2932
2933 @Override
Brad Ebinger9878b0b2018-11-08 17:43:22 -08002934 public void unregisterMmTelCapabilityCallback(int subId, IImsCapabilityCallback c) {
2935 enforceReadPrivilegedPermission("unregisterMmTelCapabilityCallback");
Brad Ebinger4ae57f92019-01-09 16:51:30 -08002936
2937 if (!SubscriptionManager.isValidSubscriptionId(subId)) {
2938 throw new IllegalArgumentException("Invalid Subscription ID: " + subId);
2939 }
2940 Binder.withCleanCallingIdentity(() -> {
2941 try {
2942 // TODO: Refactor to remove ImsManager dependence and query through ImsPhone.
Nazanin Bakhshif782e562018-12-11 15:15:39 -08002943 ImsManager.getInstance(mApp, getSlotIndexOrException(subId))
Brad Ebinger4ae57f92019-01-09 16:51:30 -08002944 .removeCapabilitiesCallbackForSubscription(c, subId);
2945 } catch (IllegalArgumentException e) {
2946 Log.i(LOG_TAG, "unregisterMmTelCapabilityCallback: " + subId
2947 + "is inactive, ignoring unregister.");
2948 // If the subscription is no longer active, just return, since the callback
2949 // will already have been removed internally.
2950 }
2951 });
Brad Ebinger35c841c2018-10-01 10:40:55 -07002952 }
2953
2954 @Override
Brad Ebinger9878b0b2018-11-08 17:43:22 -08002955 public boolean isCapable(int subId, int capability, int regTech) {
2956 enforceReadPrivilegedPermission("isCapable");
Brad Ebinger35c841c2018-10-01 10:40:55 -07002957 // TODO: Refactor to remove ImsManager dependence and query through ImsPhone directly.
2958 final long token = Binder.clearCallingIdentity();
2959 try {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08002960 return ImsManager.getInstance(mApp,
Brad Ebinger35c841c2018-10-01 10:40:55 -07002961 getSlotIndexOrException(subId)).queryMmTelCapability(capability, regTech);
2962 } catch (ImsException e) {
2963 Log.w(LOG_TAG, "IMS isCapable - service unavailable: " + e.getMessage());
2964 return false;
Brad Ebinger6b5ac222019-02-04 14:36:52 -08002965 } catch (IllegalArgumentException e) {
2966 Log.i(LOG_TAG, "isCapable: " + subId + " is inactive, returning false.");
2967 return false;
Brad Ebinger35c841c2018-10-01 10:40:55 -07002968 } finally {
2969 Binder.restoreCallingIdentity(token);
2970 }
2971 }
2972
2973 @Override
Brad Ebinger9878b0b2018-11-08 17:43:22 -08002974 public boolean isAvailable(int subId, int capability, int regTech) {
2975 enforceReadPrivilegedPermission("isAvailable");
Brad Ebinger35c841c2018-10-01 10:40:55 -07002976 final long token = Binder.clearCallingIdentity();
2977 try {
2978 Phone phone = getPhone(subId);
2979 if (phone == null) return false;
2980 return phone.isImsCapabilityAvailable(capability, regTech);
2981 } finally {
2982 Binder.restoreCallingIdentity(token);
2983 }
2984 }
2985
2986 @Override
2987 public boolean isAdvancedCallingSettingEnabled(int subId) {
2988 enforceReadPrivilegedPermission("enforceReadPrivilegedPermission");
2989 // TODO: Refactor to remove ImsManager dependence and query through ImsPhone directly.
2990 final long token = Binder.clearCallingIdentity();
2991 try {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08002992 return ImsManager.getInstance(mApp,
Brad Ebinger35c841c2018-10-01 10:40:55 -07002993 getSlotIndexOrException(subId)).isEnhanced4gLteModeSettingEnabledByUser();
2994 } finally {
2995 Binder.restoreCallingIdentity(token);
2996 }
2997 }
2998
2999 @Override
Brad Ebinger1c162042019-02-21 14:49:10 -08003000 public void setAdvancedCallingSettingEnabled(int subId, boolean isEnabled) {
Brad Ebinger35c841c2018-10-01 10:40:55 -07003001 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(mApp, subId,
Brad Ebinger1c162042019-02-21 14:49:10 -08003002 "setAdvancedCallingSettingEnabled");
Brad Ebinger35c841c2018-10-01 10:40:55 -07003003 final long identity = Binder.clearCallingIdentity();
3004 try {
3005 // TODO: Refactor to remove ImsManager dependence and query through ImsPhone directly.
Nazanin Bakhshif782e562018-12-11 15:15:39 -08003006 ImsManager.getInstance(mApp,
Brad Ebinger35c841c2018-10-01 10:40:55 -07003007 getSlotIndexOrException(subId)).setEnhanced4gLteModeSetting(isEnabled);
3008 } finally {
3009 Binder.restoreCallingIdentity(identity);
3010 }
3011 }
3012
3013 @Override
Brad Ebinger9878b0b2018-11-08 17:43:22 -08003014 public boolean isVtSettingEnabled(int subId) {
3015 enforceReadPrivilegedPermission("isVtSettingEnabled");
Brad Ebinger35c841c2018-10-01 10:40:55 -07003016 final long identity = Binder.clearCallingIdentity();
3017 try {
3018 // TODO: Refactor to remove ImsManager dependence and query through ImsPhone directly.
Nazanin Bakhshif782e562018-12-11 15:15:39 -08003019 return ImsManager.getInstance(mApp,
Brad Ebinger35c841c2018-10-01 10:40:55 -07003020 getSlotIndexOrException(subId)).isVtEnabledByUser();
3021 } finally {
3022 Binder.restoreCallingIdentity(identity);
3023 }
3024 }
3025
3026 @Override
Brad Ebinger1c162042019-02-21 14:49:10 -08003027 public void setVtSettingEnabled(int subId, boolean isEnabled) {
Brad Ebinger35c841c2018-10-01 10:40:55 -07003028 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(mApp, subId,
Brad Ebinger1c162042019-02-21 14:49:10 -08003029 "setVtSettingEnabled");
Brad Ebinger35c841c2018-10-01 10:40:55 -07003030 final long identity = Binder.clearCallingIdentity();
3031 try {
3032 // TODO: Refactor to remove ImsManager dependence and query through ImsPhone directly.
Nazanin Bakhshif782e562018-12-11 15:15:39 -08003033 ImsManager.getInstance(mApp, getSlotIndexOrException(subId)).setVtSetting(isEnabled);
Brad Ebinger35c841c2018-10-01 10:40:55 -07003034 } finally {
3035 Binder.restoreCallingIdentity(identity);
3036 }
3037 }
3038
3039 @Override
3040 public boolean isVoWiFiSettingEnabled(int subId) {
3041 enforceReadPrivilegedPermission("isVoWiFiSettingEnabled");
3042 final long identity = Binder.clearCallingIdentity();
3043 try {
3044 // TODO: Refactor to remove ImsManager dependence and query through ImsPhone directly.
Nazanin Bakhshif782e562018-12-11 15:15:39 -08003045 return ImsManager.getInstance(mApp,
Brad Ebinger35c841c2018-10-01 10:40:55 -07003046 getSlotIndexOrException(subId)).isWfcEnabledByUser();
3047 } finally {
3048 Binder.restoreCallingIdentity(identity);
3049 }
3050 }
3051
3052 @Override
Brad Ebinger1c162042019-02-21 14:49:10 -08003053 public void setVoWiFiSettingEnabled(int subId, boolean isEnabled) {
Brad Ebinger35c841c2018-10-01 10:40:55 -07003054 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(mApp, subId,
Brad Ebinger1c162042019-02-21 14:49:10 -08003055 "setVoWiFiSettingEnabled");
Brad Ebinger35c841c2018-10-01 10:40:55 -07003056 final long identity = Binder.clearCallingIdentity();
3057 try {
3058 // TODO: Refactor to remove ImsManager dependence and query through ImsPhone directly.
Nazanin Bakhshif782e562018-12-11 15:15:39 -08003059 ImsManager.getInstance(mApp, getSlotIndexOrException(subId)).setWfcSetting(isEnabled);
Brad Ebinger35c841c2018-10-01 10:40:55 -07003060 } finally {
3061 Binder.restoreCallingIdentity(identity);
3062 }
3063 }
3064
3065 @Override
3066 public boolean isVoWiFiRoamingSettingEnabled(int subId) {
3067 enforceReadPrivilegedPermission("isVoWiFiRoamingSettingEnabled");
3068 final long identity = Binder.clearCallingIdentity();
3069 try {
3070 // TODO: Refactor to remove ImsManager dependence and query through ImsPhone directly.
Nazanin Bakhshif782e562018-12-11 15:15:39 -08003071 return ImsManager.getInstance(mApp,
Brad Ebinger35c841c2018-10-01 10:40:55 -07003072 getSlotIndexOrException(subId)).isWfcRoamingEnabledByUser();
3073 } finally {
3074 Binder.restoreCallingIdentity(identity);
3075 }
3076 }
3077
3078 @Override
Brad Ebinger1c162042019-02-21 14:49:10 -08003079 public void setVoWiFiRoamingSettingEnabled(int subId, boolean isEnabled) {
Brad Ebinger35c841c2018-10-01 10:40:55 -07003080 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(mApp, subId,
Brad Ebinger1c162042019-02-21 14:49:10 -08003081 "setVoWiFiRoamingSettingEnabled");
Brad Ebinger35c841c2018-10-01 10:40:55 -07003082 final long identity = Binder.clearCallingIdentity();
3083 try {
3084 // TODO: Refactor to remove ImsManager dependence and query through ImsPhone directly.
Nazanin Bakhshif782e562018-12-11 15:15:39 -08003085 ImsManager.getInstance(mApp,
Brad Ebinger35c841c2018-10-01 10:40:55 -07003086 getSlotIndexOrException(subId)).setWfcRoamingSetting(isEnabled);
3087 } finally {
3088 Binder.restoreCallingIdentity(identity);
3089 }
3090 }
3091
3092 @Override
3093 public void setVoWiFiNonPersistent(int subId, boolean isCapable, int mode) {
3094 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(mApp, subId,
3095 "setVoWiFiNonPersistent");
3096 final long identity = Binder.clearCallingIdentity();
3097 try {
3098 // TODO: Refactor to remove ImsManager dependence and query through ImsPhone directly.
Nazanin Bakhshif782e562018-12-11 15:15:39 -08003099 ImsManager.getInstance(mApp,
Brad Ebinger2d29c012019-05-07 18:33:46 -07003100 getSlotIndexOrException(subId)).setWfcNonPersistent(isCapable, mode);
Brad Ebinger35c841c2018-10-01 10:40:55 -07003101 } finally {
3102 Binder.restoreCallingIdentity(identity);
3103 }
3104 }
3105
3106 @Override
3107 public int getVoWiFiModeSetting(int subId) {
3108 enforceReadPrivilegedPermission("getVoWiFiModeSetting");
3109 final long identity = Binder.clearCallingIdentity();
3110 try {
3111 // TODO: Refactor to remove ImsManager dependence and query through ImsPhone directly.
Nazanin Bakhshif782e562018-12-11 15:15:39 -08003112 return ImsManager.getInstance(mApp,
Brad Ebinger35c841c2018-10-01 10:40:55 -07003113 getSlotIndexOrException(subId)).getWfcMode(false /*isRoaming*/);
3114 } finally {
3115 Binder.restoreCallingIdentity(identity);
3116 }
3117 }
3118
3119 @Override
3120 public void setVoWiFiModeSetting(int subId, int mode) {
3121 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(mApp, subId,
3122 "setVoWiFiModeSetting");
3123 final long identity = Binder.clearCallingIdentity();
3124 try {
3125 // TODO: Refactor to remove ImsManager dependence and query through ImsPhone directly.
Nazanin Bakhshif782e562018-12-11 15:15:39 -08003126 ImsManager.getInstance(mApp,
Brad Ebinger35c841c2018-10-01 10:40:55 -07003127 getSlotIndexOrException(subId)).setWfcMode(mode, false /*isRoaming*/);
3128 } finally {
3129 Binder.restoreCallingIdentity(identity);
3130 }
3131 }
3132
3133 @Override
3134 public int getVoWiFiRoamingModeSetting(int subId) {
3135 enforceReadPrivilegedPermission("getVoWiFiRoamingModeSetting");
3136 final long identity = Binder.clearCallingIdentity();
3137 try {
3138 // TODO: Refactor to remove ImsManager dependence and query through ImsPhone directly.
Nazanin Bakhshif782e562018-12-11 15:15:39 -08003139 return ImsManager.getInstance(mApp,
Brad Ebinger35c841c2018-10-01 10:40:55 -07003140 getSlotIndexOrException(subId)).getWfcMode(true /*isRoaming*/);
3141 } finally {
3142 Binder.restoreCallingIdentity(identity);
3143 }
3144 }
3145
3146 @Override
3147 public void setVoWiFiRoamingModeSetting(int subId, int mode) {
3148 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(mApp, subId,
3149 "setVoWiFiRoamingModeSetting");
3150 final long identity = Binder.clearCallingIdentity();
3151 try {
3152 // TODO: Refactor to remove ImsManager dependence and query through ImsPhone directly.
Nazanin Bakhshif782e562018-12-11 15:15:39 -08003153 ImsManager.getInstance(mApp,
Brad Ebinger35c841c2018-10-01 10:40:55 -07003154 getSlotIndexOrException(subId)).setWfcMode(mode, true /*isRoaming*/);
3155 } finally {
3156 Binder.restoreCallingIdentity(identity);
3157 }
3158 }
3159
3160 @Override
3161 public void setRttCapabilitySetting(int subId, boolean isEnabled) {
3162 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(mApp, subId,
3163 "setRttCapabilityEnabled");
3164 final long identity = Binder.clearCallingIdentity();
3165 try {
3166 // TODO: Refactor to remove ImsManager dependence and query through ImsPhone directly.
Nazanin Bakhshif782e562018-12-11 15:15:39 -08003167 ImsManager.getInstance(mApp,
Brad Ebinger35c841c2018-10-01 10:40:55 -07003168 getSlotIndexOrException(subId)).setRttEnabled(isEnabled);
3169 } finally {
3170 Binder.restoreCallingIdentity(identity);
3171 }
3172 }
3173
3174 @Override
3175 public boolean isTtyOverVolteEnabled(int subId) {
3176 enforceReadPrivilegedPermission("isTtyOverVolteEnabled");
3177 final long identity = Binder.clearCallingIdentity();
3178 try {
3179 // TODO: Refactor to remove ImsManager dependence and query through ImsPhone directly.
Nazanin Bakhshif782e562018-12-11 15:15:39 -08003180 return ImsManager.getInstance(mApp,
Brad Ebinger35c841c2018-10-01 10:40:55 -07003181 getSlotIndexOrException(subId)).isTtyOnVoLteCapable();
3182 } finally {
3183 Binder.restoreCallingIdentity(identity);
3184 }
3185 }
3186
Brad Ebingerdf5b4f02018-10-31 11:24:17 -07003187 @Override
3188 public void registerImsProvisioningChangedCallback(int subId, IImsConfigCallback callback) {
3189 enforceReadPrivilegedPermission("registerImsProvisioningChangedCallback");
3190 final long identity = Binder.clearCallingIdentity();
3191 try {
3192 // TODO: Refactor to remove ImsManager dependence and query through ImsPhone directly.
Nazanin Bakhshif782e562018-12-11 15:15:39 -08003193 ImsManager.getInstance(mApp, getSlotIndexOrException(subId))
Brad Ebinger4ae57f92019-01-09 16:51:30 -08003194 .addProvisioningCallbackForSubscription(callback, subId);
Brad Ebingerdf5b4f02018-10-31 11:24:17 -07003195 } finally {
3196 Binder.restoreCallingIdentity(identity);
3197 }
3198 }
3199
3200 @Override
3201 public void unregisterImsProvisioningChangedCallback(int subId, IImsConfigCallback callback) {
3202 enforceReadPrivilegedPermission("unregisterImsProvisioningChangedCallback");
3203 final long identity = Binder.clearCallingIdentity();
Brad Ebinger4ae57f92019-01-09 16:51:30 -08003204 if (!SubscriptionManager.isValidSubscriptionId(subId)) {
3205 throw new IllegalArgumentException("Invalid Subscription ID: " + subId);
3206 }
Brad Ebingerdf5b4f02018-10-31 11:24:17 -07003207 try {
3208 // TODO: Refactor to remove ImsManager dependence and query through ImsPhone directly.
Nazanin Bakhshif782e562018-12-11 15:15:39 -08003209 ImsManager.getInstance(mApp, getSlotIndexOrException(subId))
Brad Ebinger4ae57f92019-01-09 16:51:30 -08003210 .removeProvisioningCallbackForSubscription(callback, subId);
3211 } catch (IllegalArgumentException e) {
3212 Log.i(LOG_TAG, "unregisterImsProvisioningChangedCallback: " + subId
3213 + "is inactive, ignoring unregister.");
3214 // If the subscription is no longer active, just return, since the callback will already
3215 // have been removed internally.
Brad Ebingerdf5b4f02018-10-31 11:24:17 -07003216 } finally {
3217 Binder.restoreCallingIdentity(identity);
3218 }
3219 }
3220
3221 @Override
Brad Ebinger1c8542e2019-01-14 13:43:14 -08003222 public void setImsProvisioningStatusForCapability(int subId, int capability, int tech,
3223 boolean isProvisioned) {
3224 if (tech != ImsRegistrationImplBase.REGISTRATION_TECH_IWLAN
3225 && tech != ImsRegistrationImplBase.REGISTRATION_TECH_LTE) {
3226 throw new IllegalArgumentException("Registration technology '" + tech + "' is invalid");
3227 }
3228 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(mApp, subId,
3229 "setProvisioningStatusForCapability");
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("setImsProvisioningStatusForCapability: phone instance null for subid "
3236 + subId);
3237 return;
3238 }
3239 if (!doesImsCapabilityRequireProvisioning(phone.getContext(), subId, capability)) {
3240 return;
3241 }
3242
3243 // this capability requires provisioning, route to the correct API.
3244 ImsManager ims = ImsManager.getInstance(mApp, getSlotIndex(subId));
3245 switch (capability) {
3246 case MmTelFeature.MmTelCapabilities.CAPABILITY_TYPE_VOICE: {
3247 if (tech == ImsRegistrationImplBase.REGISTRATION_TECH_LTE) {
3248 ims.setVolteProvisioned(isProvisioned);
3249 } else if (tech == ImsRegistrationImplBase.REGISTRATION_TECH_IWLAN) {
3250 ims.setWfcProvisioned(isProvisioned);
3251 }
3252 break;
3253 }
3254 case MmTelFeature.MmTelCapabilities.CAPABILITY_TYPE_VIDEO: {
3255 // There is currently no difference in VT provisioning type.
3256 ims.setVtProvisioned(isProvisioned);
3257 break;
3258 }
3259 case MmTelFeature.MmTelCapabilities.CAPABILITY_TYPE_UT: {
3260 // There is no "deprecated" UT provisioning mechanism through ImsConfig, so
3261 // change the capability of the feature instead if needed.
3262 if (isMmTelCapabilityProvisionedInCache(subId, capability, tech)
3263 == isProvisioned) {
3264 // No change in provisioning.
3265 return;
3266 }
3267 cacheMmTelCapabilityProvisioning(subId, capability, tech, isProvisioned);
3268 try {
3269 ims.changeMmTelCapability(capability, tech, isProvisioned);
3270 } catch (ImsException e) {
3271 loge("setImsProvisioningStatusForCapability: couldn't change UT capability"
3272 + ", Exception" + e.getMessage());
3273 }
3274 break;
3275 }
3276 default: {
3277 throw new IllegalArgumentException("Tried to set provisioning for capability '"
3278 + capability + "', which does not require provisioning.");
3279 }
3280 }
3281
3282 } finally {
3283 Binder.restoreCallingIdentity(identity);
3284 }
3285 }
3286
3287 @Override
3288 public boolean getImsProvisioningStatusForCapability(int subId, int capability, int tech) {
3289 if (tech != ImsRegistrationImplBase.REGISTRATION_TECH_IWLAN
3290 && tech != ImsRegistrationImplBase.REGISTRATION_TECH_LTE) {
3291 throw new IllegalArgumentException("Registration technology '" + tech + "' is invalid");
3292 }
3293 enforceReadPrivilegedPermission("getProvisioningStatusForCapability");
3294 final long identity = Binder.clearCallingIdentity();
3295 try {
3296 // TODO: Refactor to remove ImsManager dependence and query through ImsPhone directly.
3297 Phone phone = getPhone(subId);
3298 if (phone == null) {
3299 loge("getImsProvisioningStatusForCapability: phone instance null for subid "
3300 + subId);
3301 // We will fail with "true" as the provisioning status because this is the default
3302 // if we do not require provisioning.
3303 return true;
3304 }
3305
3306 if (!doesImsCapabilityRequireProvisioning(phone.getContext(), subId, capability)) {
3307 return true;
3308 }
3309
3310 ImsManager ims = ImsManager.getInstance(mApp, getSlotIndex(subId));
3311 switch (capability) {
3312 case MmTelFeature.MmTelCapabilities.CAPABILITY_TYPE_VOICE: {
3313 if (tech == ImsRegistrationImplBase.REGISTRATION_TECH_LTE) {
3314 return ims.isVolteProvisionedOnDevice();
3315 } else if (tech == ImsRegistrationImplBase.REGISTRATION_TECH_IWLAN) {
3316 return ims.isWfcProvisionedOnDevice();
3317 }
3318 // This should never happen, since we are checking tech above to make sure it
3319 // is either LTE or IWLAN.
3320 throw new IllegalArgumentException("Invalid radio technology for voice "
3321 + "capability.");
3322 }
3323 case MmTelFeature.MmTelCapabilities.CAPABILITY_TYPE_VIDEO: {
3324 // There is currently no difference in VT provisioning type.
3325 return ims.isVtProvisionedOnDevice();
3326 }
3327 case MmTelFeature.MmTelCapabilities.CAPABILITY_TYPE_UT: {
3328 // There is no "deprecated" UT provisioning mechanism, so get from shared prefs.
3329 return isMmTelCapabilityProvisionedInCache(subId, capability, tech);
3330 }
3331 default: {
3332 throw new IllegalArgumentException("Tried to get provisioning for capability '"
3333 + capability + "', which does not require provisioning.");
3334 }
3335 }
3336
3337 } finally {
3338 Binder.restoreCallingIdentity(identity);
3339 }
3340 }
3341
3342 @Override
3343 public boolean isMmTelCapabilityProvisionedInCache(int subId, int capability, int tech) {
3344 if (tech != ImsRegistrationImplBase.REGISTRATION_TECH_IWLAN
3345 && tech != ImsRegistrationImplBase.REGISTRATION_TECH_LTE) {
3346 throw new IllegalArgumentException("Registration technology '" + tech + "' is invalid");
3347 }
3348 enforceReadPrivilegedPermission("isMmTelCapabilityProvisionedInCache");
3349 int provisionedBits = getMmTelCapabilityProvisioningBitfield(subId, tech);
3350 return (provisionedBits & capability) > 0;
3351 }
3352
3353 @Override
3354 public void cacheMmTelCapabilityProvisioning(int subId, int capability, int tech,
3355 boolean isProvisioned) {
3356 if (tech != ImsRegistrationImplBase.REGISTRATION_TECH_IWLAN
3357 && tech != ImsRegistrationImplBase.REGISTRATION_TECH_LTE) {
3358 throw new IllegalArgumentException("Registration technology '" + tech + "' is invalid");
3359 }
3360 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(mApp, subId,
3361 "setProvisioningStatusForCapability");
3362 int provisionedBits = getMmTelCapabilityProvisioningBitfield(subId, tech);
3363 // If the current provisioning status for capability already matches isProvisioned,
3364 // do nothing.
3365 if (((provisionedBits & capability) > 0) == isProvisioned) {
3366 return;
3367 }
3368 if (isProvisioned) {
3369 setMmTelCapabilityProvisioningBitfield(subId, tech, (provisionedBits | capability));
3370 } else {
3371 setMmTelCapabilityProvisioningBitfield(subId, tech, (provisionedBits & ~capability));
3372 }
3373 }
3374
3375 /**
3376 * @return the bitfield containing the MmTel provisioning for the provided subscription and
3377 * technology. The bitfield should mirror the bitfield defined by
3378 * {@link MmTelFeature.MmTelCapabilities.MmTelCapability}.
3379 */
3380 private int getMmTelCapabilityProvisioningBitfield(int subId, int tech) {
3381 String key = getMmTelProvisioningKey(subId, tech);
3382 // Default is no capabilities are provisioned.
3383 return mTelephonySharedPreferences.getInt(key, 0 /*default*/);
3384 }
3385
3386 /**
3387 * Sets the MmTel capability provisioning bitfield (defined by
3388 * {@link MmTelFeature.MmTelCapabilities.MmTelCapability}) for the subscription and
3389 * technology specified.
3390 *
3391 * Note: This is a synchronous command and should not be called on UI thread.
3392 */
3393 private void setMmTelCapabilityProvisioningBitfield(int subId, int tech, int newField) {
3394 final SharedPreferences.Editor editor = mTelephonySharedPreferences.edit();
3395 String key = getMmTelProvisioningKey(subId, tech);
3396 editor.putInt(key, newField);
3397 editor.commit();
3398 }
3399
3400 private static String getMmTelProvisioningKey(int subId, int tech) {
3401 // resulting key is provision_ims_mmtel_{subId}_{tech}
3402 return PREF_PROVISION_IMS_MMTEL_PREFIX + subId + "_" + tech;
3403 }
3404
3405 /**
3406 * Query CarrierConfig to see if the specified capability requires provisioning for the
3407 * carrier associated with the subscription id.
3408 */
3409 private boolean doesImsCapabilityRequireProvisioning(Context context, int subId,
3410 int capability) {
3411 CarrierConfigManager configManager = new CarrierConfigManager(context);
3412 PersistableBundle c = configManager.getConfigForSubId(subId);
3413 boolean requireUtProvisioning = c.getBoolean(
3414 // By default, this config is true (even if there is no SIM). We also check to make
3415 // sure the subscription needs provisioning here, so we do not need to check for
3416 // the no-SIM case, where we would normally shortcut this to false.
3417 CarrierConfigManager.KEY_CARRIER_SUPPORTS_SS_OVER_UT_BOOL, true)
3418 && c.getBoolean(CarrierConfigManager.KEY_CARRIER_UT_PROVISIONING_REQUIRED_BOOL,
3419 false);
3420 boolean requireVoiceVtProvisioning = c.getBoolean(
3421 CarrierConfigManager.KEY_CARRIER_VOLTE_PROVISIONING_REQUIRED_BOOL, false);
3422
3423 // First check to make sure that the capability requires provisioning.
3424 switch (capability) {
3425 case MmTelFeature.MmTelCapabilities.CAPABILITY_TYPE_VOICE:
3426 // intentional fallthrough
3427 case MmTelFeature.MmTelCapabilities.CAPABILITY_TYPE_VIDEO: {
3428 if (requireVoiceVtProvisioning) {
3429 // Voice and Video requires provisioning
3430 return true;
3431 }
3432 break;
3433 }
3434 case MmTelFeature.MmTelCapabilities.CAPABILITY_TYPE_UT: {
3435 if (requireUtProvisioning) {
3436 // UT requires provisioning
3437 return true;
3438 }
3439 break;
3440 }
3441 }
3442 return false;
3443 }
3444
3445 @Override
Brad Ebingerdf5b4f02018-10-31 11:24:17 -07003446 public int getImsProvisioningInt(int subId, int key) {
Brad Ebinger1c8542e2019-01-14 13:43:14 -08003447 if (!SubscriptionManager.isValidSubscriptionId(subId)) {
3448 throw new IllegalArgumentException("Invalid Subscription id '" + subId + "'");
3449 }
Brad Ebingerdf5b4f02018-10-31 11:24:17 -07003450 enforceReadPrivilegedPermission("getImsProvisioningInt");
3451 final long identity = Binder.clearCallingIdentity();
3452 try {
3453 // TODO: Refactor to remove ImsManager dependence and query through ImsPhone directly.
Brad Ebinger1c8542e2019-01-14 13:43:14 -08003454 int slotId = getSlotIndex(subId);
3455 if (slotId <= SubscriptionManager.INVALID_SIM_SLOT_INDEX) {
3456 Log.w(LOG_TAG, "getImsProvisioningInt: called with an inactive subscription '"
3457 + subId + "' for key:" + key);
3458 return ImsConfigImplBase.CONFIG_RESULT_UNKNOWN;
3459 }
3460 return ImsManager.getInstance(mApp, slotId).getConfigInterface().getConfigInt(key);
Brad Ebingerdf5b4f02018-10-31 11:24:17 -07003461 } catch (ImsException e) {
Brad Ebinger1c8542e2019-01-14 13:43:14 -08003462 Log.w(LOG_TAG, "getImsProvisioningInt: ImsService is not available for subscription '"
3463 + subId + "' for key:" + key);
3464 return ImsConfigImplBase.CONFIG_RESULT_UNKNOWN;
Brad Ebingerdf5b4f02018-10-31 11:24:17 -07003465 } finally {
3466 Binder.restoreCallingIdentity(identity);
3467 }
3468 }
3469
3470 @Override
3471 public String getImsProvisioningString(int subId, int key) {
Brad Ebinger1c8542e2019-01-14 13:43:14 -08003472 if (!SubscriptionManager.isValidSubscriptionId(subId)) {
3473 throw new IllegalArgumentException("Invalid Subscription id '" + subId + "'");
3474 }
Brad Ebingerdf5b4f02018-10-31 11:24:17 -07003475 enforceReadPrivilegedPermission("getImsProvisioningString");
3476 final long identity = Binder.clearCallingIdentity();
3477 try {
3478 // TODO: Refactor to remove ImsManager dependence and query through ImsPhone directly.
Brad Ebinger1c8542e2019-01-14 13:43:14 -08003479 int slotId = getSlotIndex(subId);
3480 if (slotId <= SubscriptionManager.INVALID_SIM_SLOT_INDEX) {
3481 Log.w(LOG_TAG, "getImsProvisioningString: called for an inactive subscription id '"
3482 + subId + "' for key:" + key);
3483 return ProvisioningManager.STRING_QUERY_RESULT_ERROR_GENERIC;
3484 }
3485 return ImsManager.getInstance(mApp, slotId).getConfigInterface().getConfigString(key);
Brad Ebingerdf5b4f02018-10-31 11:24:17 -07003486 } catch (ImsException e) {
Brad Ebinger1c8542e2019-01-14 13:43:14 -08003487 Log.w(LOG_TAG, "getImsProvisioningString: ImsService is not available for sub '"
3488 + subId + "' for key:" + key);
3489 return ProvisioningManager.STRING_QUERY_RESULT_ERROR_NOT_READY;
Brad Ebingerdf5b4f02018-10-31 11:24:17 -07003490 } finally {
3491 Binder.restoreCallingIdentity(identity);
3492 }
3493 }
3494
3495 @Override
3496 public int setImsProvisioningInt(int subId, int key, int value) {
Brad Ebinger1c8542e2019-01-14 13:43:14 -08003497 if (!SubscriptionManager.isValidSubscriptionId(subId)) {
3498 throw new IllegalArgumentException("Invalid Subscription id '" + subId + "'");
3499 }
Brad Ebinger3d0b34e2018-11-15 14:13:12 -08003500 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(mApp, subId,
3501 "setImsProvisioningInt");
Brad Ebingerdf5b4f02018-10-31 11:24:17 -07003502 final long identity = Binder.clearCallingIdentity();
3503 try {
3504 // TODO: Refactor to remove ImsManager dependence and query through ImsPhone directly.
Brad Ebinger1c8542e2019-01-14 13:43:14 -08003505 int slotId = getSlotIndex(subId);
3506 if (slotId <= SubscriptionManager.INVALID_SIM_SLOT_INDEX) {
3507 Log.w(LOG_TAG, "setImsProvisioningInt: called with an inactive subscription id '"
3508 + subId + "' for key:" + key);
3509 return ImsConfigImplBase.CONFIG_RESULT_FAILED;
3510 }
3511 return ImsManager.getInstance(mApp, slotId).getConfigInterface().setConfig(key, value);
Brad Ebingerdf5b4f02018-10-31 11:24:17 -07003512 } catch (ImsException e) {
Brad Ebinger1c8542e2019-01-14 13:43:14 -08003513 Log.w(LOG_TAG, "setImsProvisioningInt: ImsService unavailable for sub '" + subId
3514 + "' for key:" + key);
3515 return ImsConfigImplBase.CONFIG_RESULT_FAILED;
Brad Ebingerdf5b4f02018-10-31 11:24:17 -07003516 } finally {
3517 Binder.restoreCallingIdentity(identity);
3518 }
3519 }
3520
3521 @Override
3522 public int setImsProvisioningString(int subId, int key, String value) {
Brad Ebinger1c8542e2019-01-14 13:43:14 -08003523 if (!SubscriptionManager.isValidSubscriptionId(subId)) {
3524 throw new IllegalArgumentException("Invalid Subscription id '" + subId + "'");
3525 }
Brad Ebinger3d0b34e2018-11-15 14:13:12 -08003526 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(mApp, subId,
3527 "setImsProvisioningString");
Brad Ebingerdf5b4f02018-10-31 11:24:17 -07003528 final long identity = Binder.clearCallingIdentity();
3529 try {
3530 // TODO: Refactor to remove ImsManager dependence and query through ImsPhone directly.
Brad Ebinger1c8542e2019-01-14 13:43:14 -08003531 int slotId = getSlotIndex(subId);
3532 if (slotId <= SubscriptionManager.INVALID_SIM_SLOT_INDEX) {
3533 Log.w(LOG_TAG, "setImsProvisioningString: called with an inactive subscription id '"
3534 + subId + "' for key:" + key);
3535 return ImsConfigImplBase.CONFIG_RESULT_FAILED;
3536 }
3537 return ImsManager.getInstance(mApp, slotId).getConfigInterface().setConfig(key, value);
Brad Ebingerdf5b4f02018-10-31 11:24:17 -07003538 } catch (ImsException e) {
Brad Ebinger1c8542e2019-01-14 13:43:14 -08003539 Log.w(LOG_TAG, "setImsProvisioningString: ImsService unavailable for sub '" + subId
3540 + "' for key:" + key);
3541 return ImsConfigImplBase.CONFIG_RESULT_FAILED;
Brad Ebingerdf5b4f02018-10-31 11:24:17 -07003542 } finally {
3543 Binder.restoreCallingIdentity(identity);
3544 }
3545 }
3546
Brad Ebinger35c841c2018-10-01 10:40:55 -07003547 private int getSlotIndexOrException(int subId) throws IllegalArgumentException {
3548 int slotId = SubscriptionManager.getSlotIndex(subId);
3549 if (!SubscriptionManager.isValidSlotIndex(slotId)) {
Brad Ebinger6b5ac222019-02-04 14:36:52 -08003550 throw new IllegalArgumentException("Invalid Subscription Id, subId=" + subId);
Brad Ebinger35c841c2018-10-01 10:40:55 -07003551 }
3552 return slotId;
3553 }
3554
Brad Ebinger1c8542e2019-01-14 13:43:14 -08003555 private int getSlotIndex(int subId) {
3556 int slotId = SubscriptionManager.getSlotIndex(subId);
3557 if (!SubscriptionManager.isValidSlotIndex(slotId)) {
3558 return SubscriptionManager.INVALID_SIM_SLOT_INDEX;
3559 }
3560 return slotId;
3561 }
3562
Wink Saville36469e72014-06-11 15:17:00 -07003563 /**
Nathan Harold9042f0b2019-05-21 15:51:27 -07003564 * Returns the data network type for a subId; does not throw SecurityException.
Wink Saville36469e72014-06-11 15:17:00 -07003565 */
3566 @Override
Robert Greenwalt36b23af2015-07-06 17:59:14 -07003567 public int getNetworkTypeForSubscriber(int subId, String callingPackage) {
Nathan Harold9042f0b2019-05-21 15:51:27 -07003568 if (getTargetSdk(callingPackage) >= android.os.Build.VERSION_CODES.Q
3569 && !TelephonyPermissions.checkCallingOrSelfReadPhoneStateNoThrow(
3570 mApp, subId, callingPackage, "getNetworkTypeForSubscriber")) {
Robert Greenwalt36b23af2015-07-06 17:59:14 -07003571 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
3572 }
Robert Greenwalta5dcfcb2015-07-10 09:06:29 -07003573
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003574 final long identity = Binder.clearCallingIdentity();
3575 try {
3576 final Phone phone = getPhone(subId);
3577 if (phone != null) {
3578 return phone.getServiceState().getDataNetworkType();
3579 } else {
3580 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
3581 }
3582 } finally {
3583 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07003584 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003585 }
3586
3587 /**
3588 * Returns the data network type
3589 */
3590 @Override
Robert Greenwalt36b23af2015-07-06 17:59:14 -07003591 public int getDataNetworkType(String callingPackage) {
3592 return getDataNetworkTypeForSubscriber(getDefaultSubscription(), callingPackage);
Wink Saville36469e72014-06-11 15:17:00 -07003593 }
3594
3595 /**
3596 * Returns the data network type for a subId
3597 */
3598 @Override
Robert Greenwalt36b23af2015-07-06 17:59:14 -07003599 public int getDataNetworkTypeForSubscriber(int subId, String callingPackage) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08003600 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
Jeff Davidson913390f2018-02-23 17:11:49 -08003601 mApp, subId, callingPackage, "getDataNetworkTypeForSubscriber")) {
Robert Greenwalt36b23af2015-07-06 17:59:14 -07003602 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
3603 }
3604
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003605 final long identity = Binder.clearCallingIdentity();
3606 try {
3607 final Phone phone = getPhone(subId);
3608 if (phone != null) {
3609 return phone.getServiceState().getDataNetworkType();
3610 } else {
3611 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
3612 }
3613 } finally {
3614 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07003615 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003616 }
3617
3618 /**
Wink Saville36469e72014-06-11 15:17:00 -07003619 * Returns the Voice network type for a subId
3620 */
3621 @Override
Robert Greenwalta5dcfcb2015-07-10 09:06:29 -07003622 public int getVoiceNetworkTypeForSubscriber(int subId, String callingPackage) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08003623 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
Jeff Davidson913390f2018-02-23 17:11:49 -08003624 mApp, subId, callingPackage, "getDataNetworkTypeForSubscriber")) {
Robert Greenwalta5dcfcb2015-07-10 09:06:29 -07003625 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
3626 }
3627
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003628 final long identity = Binder.clearCallingIdentity();
3629 try {
3630 final Phone phone = getPhone(subId);
3631 if (phone != null) {
3632 return phone.getServiceState().getVoiceNetworkType();
3633 } else {
3634 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
3635 }
3636 } finally {
3637 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07003638 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003639 }
3640
3641 /**
3642 * @return true if a ICC card is present
3643 */
3644 public boolean hasIccCard() {
Wink Saville36469e72014-06-11 15:17:00 -07003645 // FIXME Make changes to pass defaultSimId of type int
Sanket Padawe13bac7b2017-03-20 15:04:47 -07003646 return hasIccCardUsingSlotIndex(mSubscriptionController.getSlotIndex(
3647 getDefaultSubscription()));
Wink Saville36469e72014-06-11 15:17:00 -07003648 }
3649
3650 /**
Sanket Padawe13bac7b2017-03-20 15:04:47 -07003651 * @return true if a ICC card is present for a slotIndex
Wink Saville36469e72014-06-11 15:17:00 -07003652 */
Sanket Padawe356d7632015-06-22 14:03:32 -07003653 @Override
Sanket Padawe13bac7b2017-03-20 15:04:47 -07003654 public boolean hasIccCardUsingSlotIndex(int slotIndex) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003655 final long identity = Binder.clearCallingIdentity();
3656 try {
3657 final Phone phone = PhoneFactory.getPhone(slotIndex);
3658 if (phone != null) {
3659 return phone.getIccCard().hasIccCard();
3660 } else {
3661 return false;
3662 }
3663 } finally {
3664 Binder.restoreCallingIdentity(identity);
Amit Mahajana6fc2a82015-01-06 11:53:51 -08003665 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003666 }
3667
3668 /**
3669 * Return if the current radio is LTE on CDMA. This
3670 * is a tri-state return value as for a period of time
3671 * the mode may be unknown.
3672 *
Robert Greenwalt36b23af2015-07-06 17:59:14 -07003673 * @param callingPackage the name of the package making the call.
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003674 * @return {@link Phone#LTE_ON_CDMA_UNKNOWN}, {@link Phone#LTE_ON_CDMA_FALSE}
Jake Hambye994d462014-02-03 13:10:13 -08003675 * or {@link Phone#LTE_ON_CDMA_TRUE}
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003676 */
Robert Greenwalt36b23af2015-07-06 17:59:14 -07003677 @Override
3678 public int getLteOnCdmaMode(String callingPackage) {
3679 return getLteOnCdmaModeForSubscriber(getDefaultSubscription(), callingPackage);
Wink Saville36469e72014-06-11 15:17:00 -07003680 }
3681
Sanket Padawe356d7632015-06-22 14:03:32 -07003682 @Override
Robert Greenwalt36b23af2015-07-06 17:59:14 -07003683 public int getLteOnCdmaModeForSubscriber(int subId, String callingPackage) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08003684 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
Jeff Davidson913390f2018-02-23 17:11:49 -08003685 mApp, subId, callingPackage, "getLteOnCdmaModeForSubscriber")) {
Robert Greenwalt36b23af2015-07-06 17:59:14 -07003686 return PhoneConstants.LTE_ON_CDMA_UNKNOWN;
3687 }
3688
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003689 final long identity = Binder.clearCallingIdentity();
3690 try {
3691 final Phone phone = getPhone(subId);
3692 if (phone == null) {
3693 return PhoneConstants.LTE_ON_CDMA_UNKNOWN;
3694 } else {
3695 return phone.getLteOnCdmaMode();
3696 }
3697 } finally {
3698 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07003699 }
Wink Saville36469e72014-06-11 15:17:00 -07003700 }
3701
Wink Saville36469e72014-06-11 15:17:00 -07003702 /**
3703 * {@hide}
3704 * Returns Default subId, 0 in the case of single standby.
3705 */
Wink Savilleb564aae2014-10-23 10:18:09 -07003706 private int getDefaultSubscription() {
Wink Savilleac1bdfd2014-11-20 23:04:44 -08003707 return mSubscriptionController.getDefaultSubId();
Wink Saville36469e72014-06-11 15:17:00 -07003708 }
3709
Shishir Agrawala9f32182016-04-12 12:00:16 -07003710 private int getSlotForDefaultSubscription() {
3711 return mSubscriptionController.getPhoneId(getDefaultSubscription());
3712 }
3713
Wink Savilleb564aae2014-10-23 10:18:09 -07003714 private int getPreferredVoiceSubscription() {
Wink Savilleac1bdfd2014-11-20 23:04:44 -08003715 return mSubscriptionController.getDefaultVoiceSubId();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003716 }
Ihab Awadf2177b72013-11-25 13:33:23 -08003717
Pengquan Menge92a50d2018-09-21 15:54:48 -07003718 private boolean isActiveSubscription(int subId) {
3719 return mSubscriptionController.isActiveSubId(subId);
3720 }
3721
Ihab Awadf2177b72013-11-25 13:33:23 -08003722 /**
3723 * @see android.telephony.TelephonyManager.WifiCallingChoices
3724 */
3725 public int getWhenToMakeWifiCalls() {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003726 final long identity = Binder.clearCallingIdentity();
3727 try {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08003728 return Settings.System.getInt(mApp.getContentResolver(),
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003729 Settings.System.WHEN_TO_MAKE_WIFI_CALLS,
3730 getWhenToMakeWifiCallsDefaultPreference());
3731 } finally {
3732 Binder.restoreCallingIdentity(identity);
3733 }
Ihab Awadf2177b72013-11-25 13:33:23 -08003734 }
3735
3736 /**
3737 * @see android.telephony.TelephonyManager.WifiCallingChoices
3738 */
3739 public void setWhenToMakeWifiCalls(int preference) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003740 final long identity = Binder.clearCallingIdentity();
3741 try {
3742 if (DBG) log("setWhenToMakeWifiCallsStr, storing setting = " + preference);
Nazanin Bakhshif782e562018-12-11 15:15:39 -08003743 Settings.System.putInt(mApp.getContentResolver(),
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003744 Settings.System.WHEN_TO_MAKE_WIFI_CALLS, preference);
3745 } finally {
3746 Binder.restoreCallingIdentity(identity);
3747 }
Ihab Awadf9e92732013-12-05 18:02:52 -08003748 }
3749
Sailesh Nepald1e68152013-12-12 19:08:02 -08003750 private static int getWhenToMakeWifiCallsDefaultPreference() {
Santos Cordonda120f42014-08-06 04:44:34 -07003751 // TODO: Use a build property to choose this value.
Evan Charlton9829e882013-12-19 15:30:38 -08003752 return TelephonyManager.WifiCallingChoices.ALWAYS_USE;
Ihab Awadf2177b72013-11-25 13:33:23 -08003753 }
Shishir Agrawal69f68122013-12-16 17:25:49 -08003754
Jordan Liu4c733742019-02-28 12:03:40 -08003755 private Phone getPhoneFromSlotIdOrThrowException(int slotIndex) {
3756 int phoneId = UiccController.getInstance().getPhoneIdFromSlotId(slotIndex);
3757 if (phoneId == -1) {
3758 throw new IllegalArgumentException("Given slot index: " + slotIndex
3759 + " does not correspond to an active phone");
3760 }
3761 return PhoneFactory.getPhone(phoneId);
3762 }
3763
Shishir Agrawal566b7612013-10-28 14:41:00 -07003764 @Override
Derek Tan740e1672017-06-27 14:56:27 -07003765 public IccOpenLogicalChannelResponse iccOpenLogicalChannel(
3766 int subId, String callingPackage, String aid, int p2) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08003767 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
3768 mApp, subId, "iccOpenLogicalChannel");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003769 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
Jordan Liu4c733742019-02-28 12:03:40 -08003770 if (DBG) {
3771 log("iccOpenLogicalChannel: subId=" + subId + " aid=" + aid + " p2=" + p2);
3772 }
3773 return iccOpenLogicalChannelWithPermission(getPhoneFromSubId(subId), callingPackage, aid,
3774 p2);
3775 }
Shishir Agrawal566b7612013-10-28 14:41:00 -07003776
Jordan Liu4c733742019-02-28 12:03:40 -08003777
3778 @Override
3779 public IccOpenLogicalChannelResponse iccOpenLogicalChannelBySlot(
3780 int slotIndex, String callingPackage, String aid, int p2) {
3781 enforceModifyPermission();
3782 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
3783 if (DBG) {
3784 log("iccOpenLogicalChannelBySlot: slot=" + slotIndex + " aid=" + aid + " p2=" + p2);
3785 }
3786 return iccOpenLogicalChannelWithPermission(getPhoneFromSlotIdOrThrowException(slotIndex),
3787 callingPackage, aid, p2);
3788 }
3789
3790 private IccOpenLogicalChannelResponse iccOpenLogicalChannelWithPermission(Phone phone,
3791 String callingPackage, String aid, int p2) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003792 final long identity = Binder.clearCallingIdentity();
3793 try {
3794 if (TextUtils.equals(ISDR_AID, aid)) {
3795 // Only allows LPA to open logical channel to ISD-R.
Nazanin Bakhshif782e562018-12-11 15:15:39 -08003796 ComponentInfo bestComponent = EuiccConnector.findBestComponent(getDefaultPhone()
3797 .getContext().getPackageManager());
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003798 if (bestComponent == null
3799 || !TextUtils.equals(callingPackage, bestComponent.packageName)) {
3800 loge("The calling package is not allowed to access ISD-R.");
3801 throw new SecurityException(
3802 "The calling package is not allowed to access ISD-R.");
3803 }
Derek Tan740e1672017-06-27 14:56:27 -07003804 }
Derek Tan740e1672017-06-27 14:56:27 -07003805
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003806 IccOpenLogicalChannelResponse response = (IccOpenLogicalChannelResponse) sendRequest(
Jordan Liu4c733742019-02-28 12:03:40 -08003807 CMD_OPEN_CHANNEL, new Pair<String, Integer>(aid, p2), phone,
3808 null /* workSource */);
3809 if (DBG) log("iccOpenLogicalChannelWithPermission: " + response);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003810 return response;
3811 } finally {
3812 Binder.restoreCallingIdentity(identity);
3813 }
Shishir Agrawal566b7612013-10-28 14:41:00 -07003814 }
3815
3816 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08003817 public boolean iccCloseLogicalChannel(int subId, int channel) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08003818 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
3819 mApp, subId, "iccCloseLogicalChannel");
Jordan Liu4c733742019-02-28 12:03:40 -08003820 if (DBG) log("iccCloseLogicalChannel: subId=" + subId + " chnl=" + channel);
3821 return iccCloseLogicalChannelWithPermission(getPhoneFromSubId(subId), channel);
3822 }
Shishir Agrawal566b7612013-10-28 14:41:00 -07003823
Jordan Liu4c733742019-02-28 12:03:40 -08003824 @Override
3825 public boolean iccCloseLogicalChannelBySlot(int slotIndex, int channel) {
3826 enforceModifyPermission();
3827 if (DBG) log("iccCloseLogicalChannelBySlot: slotIndex=" + slotIndex + " chnl=" + channel);
3828 return iccCloseLogicalChannelWithPermission(getPhoneFromSlotIdOrThrowException(slotIndex),
3829 channel);
3830 }
3831
3832 private boolean iccCloseLogicalChannelWithPermission(Phone phone, int channel) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003833 final long identity = Binder.clearCallingIdentity();
3834 try {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003835 if (channel < 0) {
3836 return false;
3837 }
Jordan Liu4c733742019-02-28 12:03:40 -08003838 Boolean success = (Boolean) sendRequest(CMD_CLOSE_CHANNEL, channel, phone,
3839 null /* workSource */);
3840 if (DBG) log("iccCloseLogicalChannelWithPermission: " + success);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003841 return success;
3842 } finally {
3843 Binder.restoreCallingIdentity(identity);
Shishir Agrawal566b7612013-10-28 14:41:00 -07003844 }
Shishir Agrawal566b7612013-10-28 14:41:00 -07003845 }
3846
3847 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08003848 public String iccTransmitApduLogicalChannel(int subId, int channel, int cla,
Shishir Agrawal566b7612013-10-28 14:41:00 -07003849 int command, int p1, int p2, int p3, String data) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08003850 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
3851 mApp, subId, "iccTransmitApduLogicalChannel");
Jordan Liu4c733742019-02-28 12:03:40 -08003852 if (DBG) {
3853 log("iccTransmitApduLogicalChannel: subId=" + subId + " chnl=" + channel
3854 + " cla=" + cla + " cmd=" + command + " p1=" + p1 + " p2=" + p2 + " p3="
3855 + p3 + " data=" + data);
3856 }
3857 return iccTransmitApduLogicalChannelWithPermission(getPhoneFromSubId(subId), channel, cla,
3858 command, p1, p2, p3, data);
3859 }
Shishir Agrawal566b7612013-10-28 14:41:00 -07003860
Jordan Liu4c733742019-02-28 12:03:40 -08003861 @Override
3862 public String iccTransmitApduLogicalChannelBySlot(int slotIndex, int channel, int cla,
3863 int command, int p1, int p2, int p3, String data) {
3864 enforceModifyPermission();
3865 if (DBG) {
3866 log("iccTransmitApduLogicalChannelBySlot: slotIndex=" + slotIndex + " chnl=" + channel
3867 + " cla=" + cla + " cmd=" + command + " p1=" + p1 + " p2=" + p2 + " p3="
3868 + p3 + " data=" + data);
3869 }
3870 return iccTransmitApduLogicalChannelWithPermission(
3871 getPhoneFromSlotIdOrThrowException(slotIndex), channel, cla, command, p1, p2, p3,
3872 data);
3873 }
3874
3875 private String iccTransmitApduLogicalChannelWithPermission(Phone phone, int channel, int cla,
3876 int command, int p1, int p2, int p3, String data) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003877 final long identity = Binder.clearCallingIdentity();
3878 try {
Hall Liu4fd771b2019-05-02 09:16:29 -07003879 if (channel <= 0) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003880 return "";
3881 }
Shishir Agrawal566b7612013-10-28 14:41:00 -07003882
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003883 IccIoResult response = (IccIoResult) sendRequest(CMD_TRANSMIT_APDU_LOGICAL_CHANNEL,
Jordan Liu4c733742019-02-28 12:03:40 -08003884 new IccAPDUArgument(channel, cla, command, p1, p2, p3, data), phone,
3885 null /* workSource */);
3886 if (DBG) log("iccTransmitApduLogicalChannelWithPermission: " + response);
Shishir Agrawal566b7612013-10-28 14:41:00 -07003887
Malcolm Chenaa4a8532018-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 Agrawal566b7612013-10-28 14:41:00 -07003898 }
Jake Hambye994d462014-02-03 13:10:13 -08003899
Evan Charltonc66da362014-05-16 14:06:40 -07003900 @Override
Holly Jiuyu Sun1cc2d552018-01-26 15:51:16 -08003901 public String iccTransmitApduBasicChannel(int subId, String callingPackage, int cla,
3902 int command, int p1, int p2, int p3, String data) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08003903 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
3904 mApp, subId, "iccTransmitApduBasicChannel");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003905 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
Jordan Liu4c733742019-02-28 12:03:40 -08003906 if (DBG) {
3907 log("iccTransmitApduBasicChannel: subId=" + subId + " cla=" + cla + " cmd="
3908 + command + " p1=" + p1 + " p2=" + p2 + " p3=" + p3 + " data=" + data);
3909 }
3910 return iccTransmitApduBasicChannelWithPermission(getPhoneFromSubId(subId), callingPackage,
3911 cla, command, p1, p2, p3, data);
3912 }
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07003913
Jordan Liu4c733742019-02-28 12:03:40 -08003914 @Override
3915 public String iccTransmitApduBasicChannelBySlot(int slotIndex, String callingPackage, int cla,
3916 int command, int p1, int p2, int p3, String data) {
3917 enforceModifyPermission();
3918 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
3919 if (DBG) {
3920 log("iccTransmitApduBasicChannelBySlot: slotIndex=" + slotIndex + " cla=" + cla
3921 + " cmd=" + command + " p1=" + p1 + " p2=" + p2 + " p3=" + p3
3922 + " data=" + data);
3923 }
3924
3925 return iccTransmitApduBasicChannelWithPermission(
3926 getPhoneFromSlotIdOrThrowException(slotIndex), callingPackage, cla, command, p1,
3927 p2, p3, data);
3928 }
3929
3930 // open APDU basic channel assuming the caller has sufficient permissions
3931 private String iccTransmitApduBasicChannelWithPermission(Phone phone, String callingPackage,
3932 int cla, int command, int p1, int p2, int p3, String data) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003933 final long identity = Binder.clearCallingIdentity();
3934 try {
3935 if (command == SELECT_COMMAND && p1 == SELECT_P1 && p2 == SELECT_P2 && p3 == SELECT_P3
3936 && TextUtils.equals(ISDR_AID, data)) {
3937 // Only allows LPA to select ISD-R.
Nazanin Bakhshif782e562018-12-11 15:15:39 -08003938 ComponentInfo bestComponent = EuiccConnector.findBestComponent(getDefaultPhone()
3939 .getContext().getPackageManager());
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003940 if (bestComponent == null
3941 || !TextUtils.equals(callingPackage, bestComponent.packageName)) {
3942 loge("The calling package is not allowed to select ISD-R.");
3943 throw new SecurityException(
3944 "The calling package is not allowed to select ISD-R.");
3945 }
Holly Jiuyu Sun1cc2d552018-01-26 15:51:16 -08003946 }
Holly Jiuyu Sun1cc2d552018-01-26 15:51:16 -08003947
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003948 IccIoResult response = (IccIoResult) sendRequest(CMD_TRANSMIT_APDU_BASIC_CHANNEL,
Jordan Liu4c733742019-02-28 12:03:40 -08003949 new IccAPDUArgument(0, cla, command, p1, p2, p3, data), phone,
3950 null /* workSource */);
3951 if (DBG) log("iccTransmitApduBasicChannelWithPermission: " + response);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07003952
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003953 // Append the returned status code to the end of the response payload.
3954 String s = Integer.toHexString(
3955 (response.sw1 << 8) + response.sw2 + 0x10000).substring(1);
3956 if (response.payload != null) {
3957 s = IccUtils.bytesToHexString(response.payload) + s;
3958 }
3959 return s;
3960 } finally {
3961 Binder.restoreCallingIdentity(identity);
Shishir Agrawal5ec14172014-08-05 17:05:45 -07003962 }
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07003963 }
3964
3965 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08003966 public byte[] iccExchangeSimIO(int subId, int fileID, int command, int p1, int p2, int p3,
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07003967 String filePath) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08003968 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
3969 mApp, subId, "iccExchangeSimIO");
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07003970
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003971 final long identity = Binder.clearCallingIdentity();
3972 try {
3973 if (DBG) {
3974 log("Exchange SIM_IO " + subId + ":" + fileID + ":" + command + " "
3975 + p1 + " " + p2 + " " + p3 + ":" + filePath);
3976 }
3977
3978 IccIoResult response =
3979 (IccIoResult) sendRequest(CMD_EXCHANGE_SIM_IO,
3980 new IccAPDUArgument(-1, fileID, command, p1, p2, p3, filePath),
3981 subId);
3982
3983 if (DBG) {
3984 log("Exchange SIM_IO [R]" + response);
3985 }
3986
3987 byte[] result = null;
3988 int length = 2;
3989 if (response.payload != null) {
3990 length = 2 + response.payload.length;
3991 result = new byte[length];
3992 System.arraycopy(response.payload, 0, result, 0, response.payload.length);
3993 } else {
3994 result = new byte[length];
3995 }
3996
3997 result[length - 1] = (byte) response.sw2;
3998 result[length - 2] = (byte) response.sw1;
3999 return result;
4000 } finally {
4001 Binder.restoreCallingIdentity(identity);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07004002 }
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07004003 }
4004
Nathan Haroldb3014052017-01-25 15:57:32 -08004005 /**
4006 * Get the forbidden PLMN List from the given app type (ex APPTYPE_USIM)
4007 * on a particular subscription
4008 */
sqianb6e41952018-03-12 14:54:01 -07004009 public String[] getForbiddenPlmns(int subId, int appType, String callingPackage) {
4010 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
4011 mApp, subId, callingPackage, "getForbiddenPlmns")) {
4012 return null;
4013 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004014
4015 final long identity = Binder.clearCallingIdentity();
4016 try {
4017 if (appType != TelephonyManager.APPTYPE_USIM
4018 && appType != TelephonyManager.APPTYPE_SIM) {
4019 loge("getForbiddenPlmnList(): App Type must be USIM or SIM");
4020 return null;
4021 }
4022 Object response = sendRequest(
4023 CMD_GET_FORBIDDEN_PLMNS, new Integer(appType), subId);
4024 if (response instanceof String[]) {
4025 return (String[]) response;
4026 }
4027 // Response is an Exception of some kind,
4028 // which is signalled to the user as a NULL retval
Nathan Haroldb3014052017-01-25 15:57:32 -08004029 return null;
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004030 } finally {
4031 Binder.restoreCallingIdentity(identity);
Nathan Haroldb3014052017-01-25 15:57:32 -08004032 }
Nathan Haroldb3014052017-01-25 15:57:32 -08004033 }
4034
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07004035 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08004036 public String sendEnvelopeWithStatus(int subId, String content) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08004037 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
4038 mApp, subId, "sendEnvelopeWithStatus");
Evan Charltonc66da362014-05-16 14:06:40 -07004039
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004040 final long identity = Binder.clearCallingIdentity();
4041 try {
4042 IccIoResult response = (IccIoResult) sendRequest(CMD_SEND_ENVELOPE, content, subId);
4043 if (response.payload == null) {
4044 return "";
4045 }
Evan Charltonc66da362014-05-16 14:06:40 -07004046
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004047 // Append the returned status code to the end of the response payload.
4048 String s = Integer.toHexString(
4049 (response.sw1 << 8) + response.sw2 + 0x10000).substring(1);
4050 s = IccUtils.bytesToHexString(response.payload) + s;
4051 return s;
4052 } finally {
4053 Binder.restoreCallingIdentity(identity);
4054 }
Evan Charltonc66da362014-05-16 14:06:40 -07004055 }
4056
Jake Hambye994d462014-02-03 13:10:13 -08004057 /**
4058 * Read one of the NV items defined in {@link com.android.internal.telephony.RadioNVItems}
4059 * and {@code ril_nv_items.h}. Used for device configuration by some CDMA operators.
4060 *
4061 * @param itemID the ID of the item to read
4062 * @return the NV item as a String, or null on error.
4063 */
4064 @Override
4065 public String nvReadItem(int itemID) {
vagdeviaf9a5b92018-08-15 16:01:53 -07004066 WorkSource workSource = getWorkSource(Binder.getCallingUid());
Jeff Davidson7e17e312018-02-13 18:17:36 -08004067 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
4068 mApp, getDefaultSubscription(), "nvReadItem");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004069
4070 final long identity = Binder.clearCallingIdentity();
4071 try {
4072 if (DBG) log("nvReadItem: item " + itemID);
vagdeviaf9a5b92018-08-15 16:01:53 -07004073 String value = (String) sendRequest(CMD_NV_READ_ITEM, itemID, workSource);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004074 if (DBG) log("nvReadItem: item " + itemID + " is \"" + value + '"');
4075 return value;
4076 } finally {
4077 Binder.restoreCallingIdentity(identity);
4078 }
Jake Hambye994d462014-02-03 13:10:13 -08004079 }
4080
4081 /**
4082 * Write one of the NV items defined in {@link com.android.internal.telephony.RadioNVItems}
4083 * and {@code ril_nv_items.h}. Used for device configuration by some CDMA operators.
4084 *
4085 * @param itemID the ID of the item to read
4086 * @param itemValue the value to write, as a String
4087 * @return true on success; false on any failure
4088 */
4089 @Override
4090 public boolean nvWriteItem(int itemID, String itemValue) {
vagdeviaf9a5b92018-08-15 16:01:53 -07004091 WorkSource workSource = getWorkSource(Binder.getCallingUid());
Jeff Davidson7e17e312018-02-13 18:17:36 -08004092 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
4093 mApp, getDefaultSubscription(), "nvWriteItem");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004094
4095 final long identity = Binder.clearCallingIdentity();
4096 try {
4097 if (DBG) log("nvWriteItem: item " + itemID + " value \"" + itemValue + '"');
4098 Boolean success = (Boolean) sendRequest(CMD_NV_WRITE_ITEM,
vagdeviaf9a5b92018-08-15 16:01:53 -07004099 new Pair<Integer, String>(itemID, itemValue), workSource);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004100 if (DBG) log("nvWriteItem: item " + itemID + ' ' + (success ? "ok" : "fail"));
4101 return success;
4102 } finally {
4103 Binder.restoreCallingIdentity(identity);
4104 }
Jake Hambye994d462014-02-03 13:10:13 -08004105 }
4106
4107 /**
4108 * Update the CDMA Preferred Roaming List (PRL) in the radio NV storage.
4109 * Used for device configuration by some CDMA operators.
4110 *
4111 * @param preferredRoamingList byte array containing the new PRL
4112 * @return true on success; false on any failure
4113 */
4114 @Override
4115 public boolean nvWriteCdmaPrl(byte[] preferredRoamingList) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08004116 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
4117 mApp, getDefaultSubscription(), "nvWriteCdmaPrl");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004118
4119 final long identity = Binder.clearCallingIdentity();
4120 try {
4121 if (DBG) log("nvWriteCdmaPrl: value: " + HexDump.toHexString(preferredRoamingList));
4122 Boolean success = (Boolean) sendRequest(CMD_NV_WRITE_CDMA_PRL, preferredRoamingList);
4123 if (DBG) log("nvWriteCdmaPrl: " + (success ? "ok" : "fail"));
4124 return success;
4125 } finally {
4126 Binder.restoreCallingIdentity(identity);
4127 }
Jake Hambye994d462014-02-03 13:10:13 -08004128 }
4129
4130 /**
chen xu6dac5ab2018-10-26 17:39:23 -07004131 * Rollback modem configurations to factory default except some config which are in whitelist.
Jake Hambye994d462014-02-03 13:10:13 -08004132 * Used for device configuration by some CDMA operators.
4133 *
chen xu6dac5ab2018-10-26 17:39:23 -07004134 * @param slotIndex - device slot.
4135 *
Jake Hambye994d462014-02-03 13:10:13 -08004136 * @return true on success; false on any failure
4137 */
4138 @Override
chen xu6dac5ab2018-10-26 17:39:23 -07004139 public boolean resetModemConfig(int slotIndex) {
4140 Phone phone = PhoneFactory.getPhone(slotIndex);
4141 if (phone != null) {
4142 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
4143 mApp, phone.getSubId(), "resetModemConfig");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004144
chen xu6dac5ab2018-10-26 17:39:23 -07004145 final long identity = Binder.clearCallingIdentity();
4146 try {
4147 Boolean success = (Boolean) sendRequest(CMD_RESET_MODEM_CONFIG, null);
4148 if (DBG) log("resetModemConfig:" + ' ' + (success ? "ok" : "fail"));
4149 return success;
4150 } finally {
4151 Binder.restoreCallingIdentity(identity);
4152 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004153 }
chen xu6dac5ab2018-10-26 17:39:23 -07004154 return false;
4155 }
4156
4157 /**
4158 * Generate a radio modem reset. Used for device configuration by some CDMA operators.
4159 *
4160 * @param slotIndex - device slot.
4161 *
4162 * @return true on success; false on any failure
4163 */
4164 @Override
4165 public boolean rebootModem(int slotIndex) {
4166 Phone phone = PhoneFactory.getPhone(slotIndex);
4167 if (phone != null) {
4168 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
4169 mApp, phone.getSubId(), "rebootModem");
4170
4171 final long identity = Binder.clearCallingIdentity();
4172 try {
4173 Boolean success = (Boolean) sendRequest(CMD_MODEM_REBOOT, null);
4174 if (DBG) log("rebootModem:" + ' ' + (success ? "ok" : "fail"));
4175 return success;
4176 } finally {
4177 Binder.restoreCallingIdentity(identity);
4178 }
4179 }
4180 return false;
Jake Hambye994d462014-02-03 13:10:13 -08004181 }
Jake Hamby7c27be32014-03-03 13:25:59 -08004182
Svet Ganovb320e182015-04-16 12:30:10 -07004183 public String[] getPcscfAddress(String apnType, String callingPackage) {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08004184 final Phone defaultPhone = getDefaultPhone();
Jeff Davidson7e17e312018-02-13 18:17:36 -08004185 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
Nazanin Bakhshif782e562018-12-11 15:15:39 -08004186 mApp, defaultPhone.getSubId(), callingPackage, "getPcscfAddress")) {
Svet Ganovb320e182015-04-16 12:30:10 -07004187 return new String[0];
4188 }
4189
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004190 final long identity = Binder.clearCallingIdentity();
4191 try {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08004192 return defaultPhone.getPcscfAddress(apnType);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004193 } finally {
4194 Binder.restoreCallingIdentity(identity);
4195 }
Wink Saville36469e72014-06-11 15:17:00 -07004196 }
4197
Brad Ebinger51f743a2017-01-23 13:50:20 -08004198 /**
Brad Ebinger22bc3e42018-01-16 09:39:35 -08004199 * Enables IMS for the framework. This will trigger IMS registration and ImsFeature capability
4200 * status updates, if not already enabled.
Brad Ebinger51f743a2017-01-23 13:50:20 -08004201 */
Brad Ebinger22bc3e42018-01-16 09:39:35 -08004202 public void enableIms(int slotId) {
Brad Ebinger51f743a2017-01-23 13:50:20 -08004203 enforceModifyPermission();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004204
4205 final long identity = Binder.clearCallingIdentity();
4206 try {
Brad Ebinger9c0eb502019-01-23 15:06:19 -08004207 ImsResolver resolver = PhoneFactory.getImsResolver();
4208 if (resolver == null) {
4209 // may happen if the device does not support IMS.
4210 return;
4211 }
4212 resolver.enableIms(slotId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004213 } finally {
4214 Binder.restoreCallingIdentity(identity);
4215 }
Brad Ebinger34bef922017-11-09 10:27:08 -08004216 }
4217
4218 /**
Brad Ebinger22bc3e42018-01-16 09:39:35 -08004219 * Disables IMS for the framework. This will trigger IMS de-registration and trigger ImsFeature
4220 * status updates to disabled.
Brad Ebinger34bef922017-11-09 10:27:08 -08004221 */
Brad Ebinger22bc3e42018-01-16 09:39:35 -08004222 public void disableIms(int slotId) {
4223 enforceModifyPermission();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004224
4225 final long identity = Binder.clearCallingIdentity();
4226 try {
Brad Ebinger9c0eb502019-01-23 15:06:19 -08004227 ImsResolver resolver = PhoneFactory.getImsResolver();
4228 if (resolver == null) {
4229 // may happen if the device does not support IMS.
4230 return;
4231 }
4232 resolver.disableIms(slotId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004233 } finally {
4234 Binder.restoreCallingIdentity(identity);
4235 }
Brad Ebinger22bc3e42018-01-16 09:39:35 -08004236 }
4237
4238 /**
4239 * Returns the {@link IImsMmTelFeature} that corresponds to the given slot Id for the MMTel
4240 * feature or {@link null} if the service is not available. If the feature is available, the
4241 * {@link IImsServiceFeatureCallback} callback is registered as a listener for feature updates.
4242 */
4243 public IImsMmTelFeature getMmTelFeatureAndListen(int slotId,
Brad Ebinger34bef922017-11-09 10:27:08 -08004244 IImsServiceFeatureCallback callback) {
4245 enforceModifyPermission();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004246
4247 final long identity = Binder.clearCallingIdentity();
4248 try {
Brad Ebinger9c0eb502019-01-23 15:06:19 -08004249 ImsResolver resolver = PhoneFactory.getImsResolver();
4250 if (resolver == null) {
4251 // may happen if the device does not support IMS.
4252 return null;
4253 }
4254 return resolver.getMmTelFeatureAndListen(slotId, callback);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004255 } finally {
4256 Binder.restoreCallingIdentity(identity);
4257 }
Brad Ebinger34bef922017-11-09 10:27:08 -08004258 }
4259
4260 /**
4261 * Returns the {@link IImsRcsFeature} that corresponds to the given slot Id for the RCS
4262 * feature during emergency calling or {@link null} if the service is not available. If the
4263 * feature is available, the {@link IImsServiceFeatureCallback} callback is registered as a
4264 * listener for feature updates.
4265 */
4266 public IImsRcsFeature getRcsFeatureAndListen(int slotId, IImsServiceFeatureCallback callback) {
4267 enforceModifyPermission();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004268
4269 final long identity = Binder.clearCallingIdentity();
4270 try {
Brad Ebinger9c0eb502019-01-23 15:06:19 -08004271 ImsResolver resolver = PhoneFactory.getImsResolver();
4272 if (resolver == null) {
4273 // may happen if the device does not support IMS.
4274 return null;
4275 }
4276 return resolver.getRcsFeatureAndListen(slotId, callback);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004277 } finally {
4278 Binder.restoreCallingIdentity(identity);
4279 }
Brad Ebinger51f743a2017-01-23 13:50:20 -08004280 }
4281
Brad Ebinger5f64b052017-12-14 14:26:15 -08004282 /**
4283 * Returns the {@link IImsRegistration} structure associated with the slotId and feature
Brad Ebinger9c0eb502019-01-23 15:06:19 -08004284 * specified or null if IMS is not supported on the slot specified.
Brad Ebinger5f64b052017-12-14 14:26:15 -08004285 */
4286 public IImsRegistration getImsRegistration(int slotId, int feature) throws RemoteException {
4287 enforceModifyPermission();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004288
4289 final long identity = Binder.clearCallingIdentity();
4290 try {
Brad Ebinger9c0eb502019-01-23 15:06:19 -08004291 ImsResolver resolver = PhoneFactory.getImsResolver();
4292 if (resolver == null) {
4293 // may happen if the device does not support IMS.
4294 return null;
4295 }
4296 return resolver.getImsRegistration(slotId, feature);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004297 } finally {
4298 Binder.restoreCallingIdentity(identity);
4299 }
Brad Ebinger5f64b052017-12-14 14:26:15 -08004300 }
4301
Brad Ebinger22bc3e42018-01-16 09:39:35 -08004302 /**
4303 * Returns the {@link IImsConfig} structure associated with the slotId and feature
Brad Ebinger9c0eb502019-01-23 15:06:19 -08004304 * specified or null if IMS is not supported on the slot specified.
Brad Ebinger22bc3e42018-01-16 09:39:35 -08004305 */
4306 public IImsConfig getImsConfig(int slotId, int feature) throws RemoteException {
4307 enforceModifyPermission();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004308
4309 final long identity = Binder.clearCallingIdentity();
4310 try {
Brad Ebinger9c0eb502019-01-23 15:06:19 -08004311 ImsResolver resolver = PhoneFactory.getImsResolver();
4312 if (resolver == null) {
4313 // may happen if the device does not support IMS.
4314 return null;
4315 }
4316 return resolver.getImsConfig(slotId, feature);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004317 } finally {
4318 Binder.restoreCallingIdentity(identity);
4319 }
Brad Ebinger22bc3e42018-01-16 09:39:35 -08004320 }
4321
Brad Ebinger884c07b2018-02-15 16:17:40 -08004322 /**
Brad Ebingerdac2f002018-04-03 15:17:52 -07004323 * Sets the ImsService Package Name that Telephony will bind to.
4324 *
4325 * @param slotId the slot ID that the ImsService should bind for.
4326 * @param isCarrierImsService true if the ImsService is the carrier override, false if the
4327 * ImsService is the device default ImsService.
4328 * @param packageName The package name of the application that contains the ImsService to bind
4329 * to.
4330 * @return true if setting the ImsService to bind to succeeded, false if it did not.
4331 * @hide
4332 */
4333 public boolean setImsService(int slotId, boolean isCarrierImsService, String packageName) {
Brad Ebingerde696de2018-04-06 09:56:40 -07004334 int[] subIds = SubscriptionManager.getSubId(slotId);
4335 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(mApp,
4336 (subIds != null ? subIds[0] : SubscriptionManager.INVALID_SUBSCRIPTION_ID),
4337 "setImsService");
4338
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004339 final long identity = Binder.clearCallingIdentity();
4340 try {
Brad Ebinger9c0eb502019-01-23 15:06:19 -08004341 ImsResolver resolver = PhoneFactory.getImsResolver();
4342 if (resolver == null) {
4343 // may happen if the device does not support IMS.
4344 return false;
4345 }
4346 return resolver.overrideImsServiceConfiguration(slotId, isCarrierImsService,
4347 packageName);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004348 } finally {
4349 Binder.restoreCallingIdentity(identity);
4350 }
Brad Ebingerdac2f002018-04-03 15:17:52 -07004351 }
4352
4353 /**
4354 * Return the ImsService configuration.
4355 *
4356 * @param slotId The slot that the ImsService is associated with.
4357 * @param isCarrierImsService true, if the ImsService is a carrier override, false if it is
4358 * the device default.
4359 * @return the package name of the ImsService configuration.
4360 */
4361 public String getImsService(int slotId, boolean isCarrierImsService) {
Brad Ebingerde696de2018-04-06 09:56:40 -07004362 int[] subIds = SubscriptionManager.getSubId(slotId);
4363 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(mApp,
4364 (subIds != null ? subIds[0] : SubscriptionManager.INVALID_SUBSCRIPTION_ID),
4365 "getImsService");
4366
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004367 final long identity = Binder.clearCallingIdentity();
4368 try {
Brad Ebinger9c0eb502019-01-23 15:06:19 -08004369 ImsResolver resolver = PhoneFactory.getImsResolver();
4370 if (resolver == null) {
4371 // may happen if the device does not support IMS.
4372 return "";
4373 }
4374 return resolver.getImsServiceConfiguration(slotId, isCarrierImsService);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004375 } finally {
4376 Binder.restoreCallingIdentity(identity);
4377 }
Brad Ebingerdac2f002018-04-03 15:17:52 -07004378 }
4379
Wink Saville36469e72014-06-11 15:17:00 -07004380 public void setImsRegistrationState(boolean registered) {
4381 enforceModifyPermission();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004382
4383 final long identity = Binder.clearCallingIdentity();
4384 try {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08004385 getDefaultPhone().setImsRegistrationState(registered);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004386 } finally {
4387 Binder.restoreCallingIdentity(identity);
4388 }
Wink Saville36469e72014-06-11 15:17:00 -07004389 }
4390
4391 /**
Stuart Scott54788802015-03-30 13:18:01 -07004392 * Set the network selection mode to automatic.
4393 *
4394 */
4395 @Override
4396 public void setNetworkSelectionModeAutomatic(int subId) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08004397 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
4398 mApp, subId, "setNetworkSelectionModeAutomatic");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004399
Pengquan Menge92a50d2018-09-21 15:54:48 -07004400 if (!isActiveSubscription(subId)) {
4401 return;
4402 }
4403
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004404 final long identity = Binder.clearCallingIdentity();
4405 try {
4406 if (DBG) log("setNetworkSelectionModeAutomatic: subId " + subId);
4407 sendRequest(CMD_SET_NETWORK_SELECTION_MODE_AUTOMATIC, null, subId);
4408 } finally {
4409 Binder.restoreCallingIdentity(identity);
4410 }
Stuart Scott54788802015-03-30 13:18:01 -07004411 }
4412
Pengquan Mengea84e042018-09-20 14:57:26 -07004413 /**
4414 * Ask the radio to connect to the input network and change selection mode to manual.
4415 *
4416 * @param subId the id of the subscription.
4417 * @param operatorInfo the operator information, included the PLMN, long name and short name of
4418 * the operator to attach to.
4419 * @param persistSelection whether the selection will persist until reboot. If true, only allows
4420 * attaching to the selected PLMN until reboot; otherwise, attach to the chosen PLMN and resume
4421 * normal network selection next time.
4422 * @return {@code true} on success; {@code true} on any failure.
Shishir Agrawal302c8692015-06-19 13:49:39 -07004423 */
4424 @Override
Pengquan Mengea84e042018-09-20 14:57:26 -07004425 public boolean setNetworkSelectionModeManual(
4426 int subId, OperatorInfo operatorInfo, boolean persistSelection) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08004427 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
4428 mApp, subId, "setNetworkSelectionModeManual");
Pengquan Menge92a50d2018-09-21 15:54:48 -07004429
4430 if (!isActiveSubscription(subId)) {
4431 return false;
4432 }
4433
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004434 final long identity = Binder.clearCallingIdentity();
4435 try {
Pengquan Mengea84e042018-09-20 14:57:26 -07004436 ManualNetworkSelectionArgument arg = new ManualNetworkSelectionArgument(operatorInfo,
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004437 persistSelection);
Pengquan Mengea84e042018-09-20 14:57:26 -07004438 if (DBG) {
4439 log("setNetworkSelectionModeManual: subId: " + subId
4440 + " operator: " + operatorInfo);
4441 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004442 return (Boolean) sendRequest(CMD_SET_NETWORK_SELECTION_MODE_MANUAL, arg, subId);
4443 } finally {
4444 Binder.restoreCallingIdentity(identity);
4445 }
Shishir Agrawal302c8692015-06-19 13:49:39 -07004446 }
4447
4448 /**
4449 * Scans for available networks.
4450 */
4451 @Override
Hall Liuf19c44f2018-11-27 14:38:17 -08004452 public CellNetworkScanResult getCellNetworkScanResults(int subId, String callingPackage) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08004453 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
4454 mApp, subId, "getCellNetworkScanResults");
Hall Liuf19c44f2018-11-27 14:38:17 -08004455 LocationAccessPolicy.LocationPermissionResult locationResult =
4456 LocationAccessPolicy.checkLocationPermission(mApp,
4457 new LocationAccessPolicy.LocationPermissionQuery.Builder()
4458 .setCallingPackage(callingPackage)
4459 .setCallingPid(Binder.getCallingPid())
4460 .setCallingUid(Binder.getCallingUid())
4461 .setMethod("getCellNetworkScanResults")
4462 .setMinSdkVersionForFine(Build.VERSION_CODES.Q)
4463 .build());
4464 switch (locationResult) {
4465 case DENIED_HARD:
4466 throw new SecurityException("Not allowed to access scan results -- location");
4467 case DENIED_SOFT:
4468 return null;
4469 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004470
Pengquan Menga1bb6272018-09-06 09:59:22 -07004471 long identity = Binder.clearCallingIdentity();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004472 try {
4473 if (DBG) log("getCellNetworkScanResults: subId " + subId);
Pengquan Menga1bb6272018-09-06 09:59:22 -07004474 return (CellNetworkScanResult) sendRequest(
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004475 CMD_PERFORM_NETWORK_SCAN, null, subId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004476 } finally {
4477 Binder.restoreCallingIdentity(identity);
4478 }
Shishir Agrawal302c8692015-06-19 13:49:39 -07004479 }
4480
4481 /**
yinxub1bed742017-04-17 11:45:04 -07004482 * Starts a new network scan and returns the id of this scan.
yinxu504e1392017-04-12 16:03:22 -07004483 *
yinxub1bed742017-04-17 11:45:04 -07004484 * @param subId id of the subscription
4485 * @param request contains the radio access networks with bands/channels to scan
4486 * @param messenger callback messenger for scan results or errors
4487 * @param binder for the purpose of auto clean when the user thread crashes
yinxu504e1392017-04-12 16:03:22 -07004488 * @return the id of the requested scan which can be used to stop the scan.
4489 */
4490 @Override
4491 public int requestNetworkScan(int subId, NetworkScanRequest request, Messenger messenger,
Hall Liuf19c44f2018-11-27 14:38:17 -08004492 IBinder binder, String callingPackage) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08004493 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
4494 mApp, subId, "requestNetworkScan");
Hall Liuf19c44f2018-11-27 14:38:17 -08004495 LocationAccessPolicy.LocationPermissionResult locationResult =
4496 LocationAccessPolicy.checkLocationPermission(mApp,
4497 new LocationAccessPolicy.LocationPermissionQuery.Builder()
4498 .setCallingPackage(callingPackage)
4499 .setCallingPid(Binder.getCallingPid())
4500 .setCallingUid(Binder.getCallingUid())
4501 .setMethod("requestNetworkScan")
4502 .setMinSdkVersionForFine(Build.VERSION_CODES.Q)
4503 .build());
Hall Liub2ac8ef2019-02-28 15:56:23 -08004504 if (locationResult != LocationAccessPolicy.LocationPermissionResult.ALLOWED) {
Hall Liu558027f2019-05-15 19:14:05 -07004505 SecurityException e = checkNetworkRequestForSanitizedLocationAccess(request, subId);
Hall Liub2ac8ef2019-02-28 15:56:23 -08004506 if (e != null) {
4507 if (locationResult == LocationAccessPolicy.LocationPermissionResult.DENIED_HARD) {
4508 throw e;
4509 } else {
Hall Liu0e5abaf2019-04-04 01:25:30 -07004510 loge(e.getMessage());
Hall Liub2ac8ef2019-02-28 15:56:23 -08004511 return TelephonyScanManager.INVALID_SCAN_ID;
4512 }
4513 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004514 }
Hall Liu912dfd32019-04-25 14:02:26 -07004515 int callingUid = Binder.getCallingUid();
4516 int callingPid = Binder.getCallingPid();
Ying Xu94a46582019-04-18 17:14:56 -07004517 final long identity = Binder.clearCallingIdentity();
4518 try {
4519 return mNetworkScanRequestTracker.startNetworkScan(
4520 request, messenger, binder, getPhone(subId),
Hall Liu912dfd32019-04-25 14:02:26 -07004521 callingUid, callingPid, callingPackage);
Ying Xu94a46582019-04-18 17:14:56 -07004522 } finally {
4523 Binder.restoreCallingIdentity(identity);
4524 }
yinxu504e1392017-04-12 16:03:22 -07004525 }
4526
Hall Liub2ac8ef2019-02-28 15:56:23 -08004527 private SecurityException checkNetworkRequestForSanitizedLocationAccess(
Hall Liu558027f2019-05-15 19:14:05 -07004528 NetworkScanRequest request, int subId) {
4529 boolean hasCarrierPriv = getCarrierPrivilegeStatusForUid(subId, Binder.getCallingUid())
4530 == TelephonyManager.CARRIER_PRIVILEGE_STATUS_HAS_ACCESS;
4531 boolean hasNetworkScanPermission =
4532 mApp.checkCallingOrSelfPermission(android.Manifest.permission.NETWORK_SCAN)
4533 == PERMISSION_GRANTED;
4534
4535 if (!hasCarrierPriv && !hasNetworkScanPermission) {
4536 return new SecurityException("permission.NETWORK_SCAN or carrier privileges is needed"
4537 + " for network scans without location access.");
Hall Liub2ac8ef2019-02-28 15:56:23 -08004538 }
4539
4540 if (request.getSpecifiers() != null && request.getSpecifiers().length > 0) {
4541 for (RadioAccessSpecifier ras : request.getSpecifiers()) {
Hall Liub2ac8ef2019-02-28 15:56:23 -08004542 if (ras.getChannels() != null && ras.getChannels().length > 0) {
4543 return new SecurityException("Specific channels must not be"
4544 + " scanned without location access.");
4545 }
4546 }
4547 }
4548
Hall Liub2ac8ef2019-02-28 15:56:23 -08004549 return null;
4550 }
4551
yinxu504e1392017-04-12 16:03:22 -07004552 /**
4553 * Stops an existing network scan with the given scanId.
yinxub1bed742017-04-17 11:45:04 -07004554 *
4555 * @param subId id of the subscription
4556 * @param scanId id of the scan that needs to be stopped
yinxu504e1392017-04-12 16:03:22 -07004557 */
4558 @Override
4559 public void stopNetworkScan(int subId, int scanId) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08004560 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
4561 mApp, subId, "stopNetworkScan");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004562
Hall Liu912dfd32019-04-25 14:02:26 -07004563 int callingUid = Binder.getCallingUid();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004564 final long identity = Binder.clearCallingIdentity();
4565 try {
Hall Liu912dfd32019-04-25 14:02:26 -07004566 mNetworkScanRequestTracker.stopNetworkScan(scanId, callingUid);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004567 } finally {
4568 Binder.restoreCallingIdentity(identity);
4569 }
yinxu504e1392017-04-12 16:03:22 -07004570 }
4571
4572 /**
Junda Liu84d15a22014-07-02 11:21:04 -07004573 * Get the calculated preferred network type.
4574 * Used for debugging incorrect network type.
4575 *
4576 * @return the preferred network type, defined in RILConstants.java.
4577 */
4578 @Override
Svet Ganovb320e182015-04-16 12:30:10 -07004579 public int getCalculatedPreferredNetworkType(String callingPackage) {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08004580 final Phone defaultPhone = getDefaultPhone();
4581 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(mApp, defaultPhone.getSubId(),
4582 callingPackage, "getCalculatedPreferredNetworkType")) {
Svet Ganovb320e182015-04-16 12:30:10 -07004583 return RILConstants.PREFERRED_NETWORK_MODE;
4584 }
4585
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004586 final long identity = Binder.clearCallingIdentity();
4587 try {
4588 // FIXME: need to get SubId from somewhere.
Nazanin Bakhshif782e562018-12-11 15:15:39 -08004589 return PhoneFactory.calculatePreferredNetworkType(defaultPhone.getContext(), 0);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004590 } finally {
4591 Binder.restoreCallingIdentity(identity);
4592 }
Junda Liu84d15a22014-07-02 11:21:04 -07004593 }
4594
4595 /**
Jake Hamby7c27be32014-03-03 13:25:59 -08004596 * Get the preferred network type.
4597 * Used for device configuration by some CDMA operators.
4598 *
4599 * @return the preferred network type, defined in RILConstants.java.
4600 */
4601 @Override
Stuart Scott54788802015-03-30 13:18:01 -07004602 public int getPreferredNetworkType(int subId) {
Pengquan Menga4009cb2018-12-20 11:00:24 -08004603 TelephonyPermissions
4604 .enforeceCallingOrSelfReadPrivilegedPhoneStatePermissionOrCarrierPrivilege(
4605 mApp, subId, "getPreferredNetworkType");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004606
4607 final long identity = Binder.clearCallingIdentity();
4608 try {
4609 if (DBG) log("getPreferredNetworkType");
4610 int[] result = (int[]) sendRequest(CMD_GET_PREFERRED_NETWORK_TYPE, null, subId);
4611 int networkType = (result != null ? result[0] : -1);
4612 if (DBG) log("getPreferredNetworkType: " + networkType);
4613 return networkType;
4614 } finally {
4615 Binder.restoreCallingIdentity(identity);
4616 }
Jake Hamby7c27be32014-03-03 13:25:59 -08004617 }
4618
4619 /**
4620 * Set the preferred network type.
4621 * Used for device configuration by some CDMA operators.
4622 *
4623 * @param networkType the preferred network type, defined in RILConstants.java.
4624 * @return true on success; false on any failure.
4625 */
4626 @Override
Stuart Scott54788802015-03-30 13:18:01 -07004627 public boolean setPreferredNetworkType(int subId, int networkType) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08004628 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
4629 mApp, subId, "setPreferredNetworkType");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004630
4631 final long identity = Binder.clearCallingIdentity();
4632 try {
4633 if (DBG) log("setPreferredNetworkType: subId " + subId + " type " + networkType);
4634 Boolean success = (Boolean) sendRequest(
4635 CMD_SET_PREFERRED_NETWORK_TYPE, networkType, subId);
4636 if (DBG) log("setPreferredNetworkType: " + (success ? "ok" : "fail"));
4637 if (success) {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08004638 Settings.Global.putInt(mApp.getContentResolver(),
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004639 Settings.Global.PREFERRED_NETWORK_MODE + subId, networkType);
4640 }
4641 return success;
4642 } finally {
4643 Binder.restoreCallingIdentity(identity);
Junda Liu80bc0d12014-07-14 16:36:44 -07004644 }
Jake Hamby7c27be32014-03-03 13:25:59 -08004645 }
Robert Greenwalted86e582014-05-21 20:03:20 -07004646
4647 /**
Miaoa84611c2019-03-15 09:21:10 +08004648 * Check whether DUN APN is required for tethering with subId.
Junda Liu475951f2014-11-07 16:45:03 -08004649 *
Miaoa84611c2019-03-15 09:21:10 +08004650 * @param subId the id of the subscription to require tethering.
Amit Mahajanfe58cdf2017-07-11 12:01:53 -07004651 * @return {@code true} if DUN APN is required for tethering.
Junda Liu475951f2014-11-07 16:45:03 -08004652 * @hide
4653 */
4654 @Override
Miaoa84611c2019-03-15 09:21:10 +08004655 public boolean getTetherApnRequiredForSubscriber(int subId) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08004656 enforceModifyPermission();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004657 final long identity = Binder.clearCallingIdentity();
Miaoa84611c2019-03-15 09:21:10 +08004658 final Phone phone = getPhone(subId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004659 try {
Miaoa84611c2019-03-15 09:21:10 +08004660 if (phone != null) {
4661 return phone.hasMatchedTetherApnSetting();
4662 } else {
4663 return false;
4664 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004665 } finally {
4666 Binder.restoreCallingIdentity(identity);
Junda Liu475951f2014-11-07 16:45:03 -08004667 }
Junda Liu475951f2014-11-07 16:45:03 -08004668 }
4669
4670 /**
Robert Greenwalted86e582014-05-21 20:03:20 -07004671 * Set mobile data enabled
4672 * Used by the user through settings etc to turn on/off mobile data
4673 *
4674 * @param enable {@code true} turn turn data on, else {@code false}
4675 */
4676 @Override
Malcolm Chen964682d2017-11-28 16:20:07 -08004677 public void setUserDataEnabled(int subId, boolean enable) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08004678 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
4679 mApp, subId, "setUserDataEnabled");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004680
4681 final long identity = Binder.clearCallingIdentity();
4682 try {
4683 int phoneId = mSubscriptionController.getPhoneId(subId);
4684 if (DBG) log("setUserDataEnabled: subId=" + subId + " phoneId=" + phoneId);
4685 Phone phone = PhoneFactory.getPhone(phoneId);
4686 if (phone != null) {
4687 if (DBG) log("setUserDataEnabled: subId=" + subId + " enable=" + enable);
Jack Yud79fba22018-12-13 11:51:28 -08004688 phone.getDataEnabledSettings().setUserDataEnabled(enable);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004689 } else {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08004690 loge("setUserDataEnabled: no phone found. Invalid subId=" + subId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004691 }
4692 } finally {
4693 Binder.restoreCallingIdentity(identity);
Wink Savillee7353bb2014-12-05 14:21:41 -08004694 }
Robert Greenwalted86e582014-05-21 20:03:20 -07004695 }
4696
4697 /**
Malcolm Chen964682d2017-11-28 16:20:07 -08004698 * Get the user enabled state of Mobile Data.
4699 *
4700 * TODO: remove and use isUserDataEnabled.
4701 * This can't be removed now because some vendor codes
4702 * calls through ITelephony directly while they should
4703 * use TelephonyManager.
4704 *
4705 * @return true on enabled
4706 */
4707 @Override
4708 public boolean getDataEnabled(int subId) {
4709 return isUserDataEnabled(subId);
4710 }
4711
4712 /**
4713 * Get whether mobile data is enabled per user setting.
4714 *
4715 * There are other factors deciding whether mobile data is actually enabled, but they are
4716 * not considered here. See {@link #isDataEnabled(int)} for more details.
Robert Greenwalt646120a2014-05-23 11:54:03 -07004717 *
Jeff Davidsona1920712016-11-18 17:05:56 -08004718 * Accepts either ACCESS_NETWORK_STATE, MODIFY_PHONE_STATE or carrier privileges.
Robert Greenwalted86e582014-05-21 20:03:20 -07004719 *
4720 * @return {@code true} if data is enabled else {@code false}
4721 */
4722 @Override
Malcolm Chen964682d2017-11-28 16:20:07 -08004723 public boolean isUserDataEnabled(int subId) {
Robert Greenwalt646120a2014-05-23 11:54:03 -07004724 try {
4725 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.ACCESS_NETWORK_STATE,
4726 null);
4727 } catch (Exception e) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08004728 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
4729 mApp, subId, "isUserDataEnabled");
Robert Greenwalt646120a2014-05-23 11:54:03 -07004730 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004731
4732 final long identity = Binder.clearCallingIdentity();
4733 try {
4734 int phoneId = mSubscriptionController.getPhoneId(subId);
4735 if (DBG) log("isUserDataEnabled: subId=" + subId + " phoneId=" + phoneId);
4736 Phone phone = PhoneFactory.getPhone(phoneId);
4737 if (phone != null) {
4738 boolean retVal = phone.isUserDataEnabled();
4739 if (DBG) log("isUserDataEnabled: subId=" + subId + " retVal=" + retVal);
4740 return retVal;
4741 } else {
4742 if (DBG) loge("isUserDataEnabled: no phone subId=" + subId + " retVal=false");
4743 return false;
4744 }
4745 } finally {
4746 Binder.restoreCallingIdentity(identity);
Malcolm Chen964682d2017-11-28 16:20:07 -08004747 }
4748 }
4749
4750 /**
4751 * Get whether mobile data is enabled.
4752 *
4753 * Comparable to {@link #isUserDataEnabled(int)}, this considers all factors deciding
4754 * whether mobile data is actually enabled.
4755 *
4756 * Accepts either ACCESS_NETWORK_STATE, MODIFY_PHONE_STATE or carrier privileges.
4757 *
4758 * @return {@code true} if data is enabled else {@code false}
4759 */
4760 @Override
4761 public boolean isDataEnabled(int subId) {
4762 try {
4763 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.ACCESS_NETWORK_STATE,
4764 null);
4765 } catch (Exception e) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08004766 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
4767 mApp, subId, "isDataEnabled");
Malcolm Chen964682d2017-11-28 16:20:07 -08004768 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004769
4770 final long identity = Binder.clearCallingIdentity();
4771 try {
4772 int phoneId = mSubscriptionController.getPhoneId(subId);
4773 if (DBG) log("isDataEnabled: subId=" + subId + " phoneId=" + phoneId);
4774 Phone phone = PhoneFactory.getPhone(phoneId);
4775 if (phone != null) {
Jack Yud79fba22018-12-13 11:51:28 -08004776 boolean retVal = phone.getDataEnabledSettings().isDataEnabled();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004777 if (DBG) log("isDataEnabled: subId=" + subId + " retVal=" + retVal);
4778 return retVal;
4779 } else {
4780 if (DBG) loge("isDataEnabled: no phone subId=" + subId + " retVal=false");
4781 return false;
4782 }
4783 } finally {
4784 Binder.restoreCallingIdentity(identity);
Wink Savillee7353bb2014-12-05 14:21:41 -08004785 }
Robert Greenwalted86e582014-05-21 20:03:20 -07004786 }
Shishir Agrawal60f9c952014-06-23 12:00:43 -07004787
Nazanin Bakhshi5d0636e2019-08-19 16:29:37 -07004788 private int getCarrierPrivilegeStatusFromCarrierConfigRules(int privilegeFromSim,
4789 Phone phone) {
4790 //load access rules from carrier configs, and check those as well: b/139133814
4791 SubscriptionController subController = SubscriptionController.getInstance();
4792 if (privilegeFromSim == TelephonyManager.CARRIER_PRIVILEGE_STATUS_HAS_ACCESS
4793 || subController == null) return privilegeFromSim;
4794
4795 int uid = Binder.getCallingUid();
4796 PackageManager pkgMgr = phone.getContext().getPackageManager();
4797 String[] packages = pkgMgr.getPackagesForUid(uid);
4798
4799 final long identity = Binder.clearCallingIdentity();
4800 try {
4801 SubscriptionInfo subInfo = subController.getSubscriptionInfo(phone.getSubId());
4802 SubscriptionManager subManager = (SubscriptionManager)
4803 phone.getContext().getSystemService(Context.TELEPHONY_SUBSCRIPTION_SERVICE);
4804 for (String pkg : packages) {
4805 if (subManager.canManageSubscription(subInfo, pkg)) {
4806 return TelephonyManager.CARRIER_PRIVILEGE_STATUS_HAS_ACCESS;
4807 }
4808 }
4809 return privilegeFromSim;
4810 } finally {
4811 Binder.restoreCallingIdentity(identity);
4812 }
4813 }
4814
4815 private int getCarrierPrivilegeStatusFromCarrierConfigRules(int privilegeFromSim, Phone phone,
4816 String pkgName) {
4817 //load access rules from carrier configs, and check those as well: b/139133814
4818 SubscriptionController subController = SubscriptionController.getInstance();
4819 if (privilegeFromSim == TelephonyManager.CARRIER_PRIVILEGE_STATUS_HAS_ACCESS
4820 || subController == null) return privilegeFromSim;
4821
4822 final long identity = Binder.clearCallingIdentity();
4823 try {
4824 SubscriptionInfo subInfo = subController.getSubscriptionInfo(phone.getSubId());
4825 SubscriptionManager subManager = (SubscriptionManager)
4826 phone.getContext().getSystemService(Context.TELEPHONY_SUBSCRIPTION_SERVICE);
4827 return subManager.canManageSubscription(subInfo, pkgName)
4828 ? TelephonyManager.CARRIER_PRIVILEGE_STATUS_HAS_ACCESS : privilegeFromSim;
4829 } finally {
4830 Binder.restoreCallingIdentity(identity);
4831 }
4832 }
4833
Shishir Agrawal60f9c952014-06-23 12:00:43 -07004834 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08004835 public int getCarrierPrivilegeStatus(int subId) {
4836 final Phone phone = getPhone(subId);
4837 if (phone == null) {
4838 loge("getCarrierPrivilegeStatus: Invalid subId");
4839 return TelephonyManager.CARRIER_PRIVILEGE_STATUS_NO_ACCESS;
4840 }
4841 UiccCard card = UiccController.getInstance().getUiccCard(phone.getPhoneId());
Shishir Agrawaleb8771e2014-07-22 11:24:08 -07004842 if (card == null) {
Shishir Agrawal5e5becd2014-11-18 11:38:23 -08004843 loge("getCarrierPrivilegeStatus: No UICC");
Shishir Agrawaleb8771e2014-07-22 11:24:08 -07004844 return TelephonyManager.CARRIER_PRIVILEGE_STATUS_RULES_NOT_LOADED;
4845 }
Nazanin Bakhshi5d0636e2019-08-19 16:29:37 -07004846
4847 return getCarrierPrivilegeStatusFromCarrierConfigRules(
4848 card.getCarrierPrivilegeStatusForCurrentTransaction(
4849 phone.getContext().getPackageManager()), phone);
Shishir Agrawal60f9c952014-06-23 12:00:43 -07004850 }
Junda Liu29340342014-07-10 15:23:27 -07004851
4852 @Override
Jeff Davidson7e17e312018-02-13 18:17:36 -08004853 public int getCarrierPrivilegeStatusForUid(int subId, int uid) {
4854 final Phone phone = getPhone(subId);
4855 if (phone == null) {
4856 loge("getCarrierPrivilegeStatus: Invalid subId");
4857 return TelephonyManager.CARRIER_PRIVILEGE_STATUS_NO_ACCESS;
4858 }
4859 UiccProfile profile =
4860 UiccController.getInstance().getUiccProfileForPhone(phone.getPhoneId());
4861 if (profile == null) {
4862 loge("getCarrierPrivilegeStatus: No UICC");
4863 return TelephonyManager.CARRIER_PRIVILEGE_STATUS_RULES_NOT_LOADED;
4864 }
Nazanin Bakhshi5d0636e2019-08-19 16:29:37 -07004865 return getCarrierPrivilegeStatusFromCarrierConfigRules(
4866 profile.getCarrierPrivilegeStatusForUid(
4867 phone.getContext().getPackageManager(), uid), phone);
Jeff Davidson7e17e312018-02-13 18:17:36 -08004868 }
4869
4870 @Override
chen xuf7e9fe82019-05-09 19:31:02 -07004871 public int checkCarrierPrivilegesForPackage(int subId, String pkgName) {
4872 if (TextUtils.isEmpty(pkgName)) {
Junda Liu317d70b2016-03-08 09:33:53 -08004873 return TelephonyManager.CARRIER_PRIVILEGE_STATUS_NO_ACCESS;
chen xuf7e9fe82019-05-09 19:31:02 -07004874 }
4875
4876 int phoneId = SubscriptionManager.getPhoneId(subId);
4877 UiccCard card = UiccController.getInstance().getUiccCard(phoneId);
Shishir Agrawaleb8771e2014-07-22 11:24:08 -07004878 if (card == null) {
chen xuf7e9fe82019-05-09 19:31:02 -07004879 loge("checkCarrierPrivilegesForPackage: No UICC on subId " + subId);
Shishir Agrawaleb8771e2014-07-22 11:24:08 -07004880 return TelephonyManager.CARRIER_PRIVILEGE_STATUS_RULES_NOT_LOADED;
4881 }
Nazanin Bakhshi5d0636e2019-08-19 16:29:37 -07004882 return getCarrierPrivilegeStatusFromCarrierConfigRules(
4883 card.getCarrierPrivilegeStatus(mApp.getPackageManager(), pkgName),
4884 getPhone(phoneId), pkgName);
Zach Johnson50ecba32015-05-19 00:24:21 -07004885 }
4886
4887 @Override
4888 public int checkCarrierPrivilegesForPackageAnyPhone(String pkgName) {
Junda Liu317d70b2016-03-08 09:33:53 -08004889 if (TextUtils.isEmpty(pkgName))
4890 return TelephonyManager.CARRIER_PRIVILEGE_STATUS_NO_ACCESS;
Zach Johnson50ecba32015-05-19 00:24:21 -07004891 int result = TelephonyManager.CARRIER_PRIVILEGE_STATUS_RULES_NOT_LOADED;
4892 for (int i = 0; i < TelephonyManager.getDefault().getPhoneCount(); i++) {
4893 UiccCard card = UiccController.getInstance().getUiccCard(i);
4894 if (card == null) {
Jonathan Basseri7d320df2015-06-16 12:17:08 -07004895 // No UICC in that slot.
Zach Johnson50ecba32015-05-19 00:24:21 -07004896 continue;
4897 }
4898
Nazanin Bakhshi5d0636e2019-08-19 16:29:37 -07004899 result = getCarrierPrivilegeStatusFromCarrierConfigRules(
4900 card.getCarrierPrivilegeStatus(mApp.getPackageManager(), pkgName),
4901 getPhone(i), pkgName);
Zach Johnson50ecba32015-05-19 00:24:21 -07004902 if (result == TelephonyManager.CARRIER_PRIVILEGE_STATUS_HAS_ACCESS) {
4903 break;
4904 }
4905 }
4906
4907 return result;
Junda Liu29340342014-07-10 15:23:27 -07004908 }
Derek Tan89e89d42014-07-08 17:00:10 -07004909
4910 @Override
Junda Liue64de782015-04-16 17:19:16 -07004911 public List<String> getCarrierPackageNamesForIntentAndPhone(Intent intent, int phoneId) {
4912 if (!SubscriptionManager.isValidPhoneId(phoneId)) {
4913 loge("phoneId " + phoneId + " is not valid.");
4914 return null;
4915 }
4916 UiccCard card = UiccController.getInstance().getUiccCard(phoneId);
Shishir Agrawaleb6439a2014-07-21 13:19:38 -07004917 if (card == null) {
Diego Pontorieroaf74c862014-08-28 11:51:16 -07004918 loge("getCarrierPackageNamesForIntent: No UICC");
Shishir Agrawaleb6439a2014-07-21 13:19:38 -07004919 return null ;
4920 }
Nazanin Bakhshif782e562018-12-11 15:15:39 -08004921 return card.getCarrierPackageNamesForIntent(mApp.getPackageManager(), intent);
Shishir Agrawaleb6439a2014-07-21 13:19:38 -07004922 }
4923
Amith Yamasani6e118872016-02-19 12:53:51 -08004924 @Override
chen xuf7e9fe82019-05-09 19:31:02 -07004925 public List<String> getPackagesWithCarrierPrivileges(int phoneId) {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08004926 PackageManager pm = mApp.getPackageManager();
Amith Yamasani6e118872016-02-19 12:53:51 -08004927 List<String> privilegedPackages = new ArrayList<>();
4928 List<PackageInfo> packages = null;
chen xuf7e9fe82019-05-09 19:31:02 -07004929 UiccCard card = UiccController.getInstance().getUiccCard(phoneId);
4930 // has UICC in that slot.
4931 if (card != null) {
Amith Yamasani6e118872016-02-19 12:53:51 -08004932 if (card.hasCarrierPrivilegeRules()) {
4933 if (packages == null) {
4934 // Only check packages in user 0 for now
4935 packages = pm.getInstalledPackagesAsUser(
Nazanin Bakhshi5d0636e2019-08-19 16:29:37 -07004936 PackageManager.MATCH_DISABLED_COMPONENTS
4937 | PackageManager.MATCH_DISABLED_UNTIL_USED_COMPONENTS
Cheonho Park17089c62019-08-01 15:23:12 +09004938 | PackageManager.GET_SIGNING_CERTIFICATES,
4939 UserHandle.USER_SYSTEM);
Amith Yamasani6e118872016-02-19 12:53:51 -08004940 }
4941 for (int p = packages.size() - 1; p >= 0; p--) {
4942 PackageInfo pkgInfo = packages.get(p);
4943 if (pkgInfo != null && pkgInfo.packageName != null
4944 && card.getCarrierPrivilegeStatus(pkgInfo)
chen xuf7e9fe82019-05-09 19:31:02 -07004945 == TelephonyManager.CARRIER_PRIVILEGE_STATUS_HAS_ACCESS) {
Amith Yamasani6e118872016-02-19 12:53:51 -08004946 privilegedPackages.add(pkgInfo.packageName);
4947 }
4948 }
4949 }
4950 }
4951 return privilegedPackages;
4952 }
4953
chen xuf7e9fe82019-05-09 19:31:02 -07004954 @Override
4955 public List<String> getPackagesWithCarrierPrivilegesForAllPhones() {
4956 List<String> privilegedPackages = new ArrayList<>();
4957 for (int i = 0; i < TelephonyManager.getDefault().getPhoneCount(); i++) {
4958 privilegedPackages.addAll(getPackagesWithCarrierPrivileges(i));
4959 }
4960 return privilegedPackages;
4961 }
4962
Wink Savilleb564aae2014-10-23 10:18:09 -07004963 private String getIccId(int subId) {
Sanket Padawe356d7632015-06-22 14:03:32 -07004964 final Phone phone = getPhone(subId);
4965 UiccCard card = phone == null ? null : phone.getUiccCard();
Derek Tan97ebb422014-09-05 16:55:38 -07004966 if (card == null) {
4967 loge("getIccId: No UICC");
4968 return null;
4969 }
4970 String iccId = card.getIccId();
4971 if (TextUtils.isEmpty(iccId)) {
4972 loge("getIccId: ICC ID is null or empty.");
4973 return null;
4974 }
4975 return iccId;
4976 }
4977
Shishir Agrawaleb6439a2014-07-21 13:19:38 -07004978 @Override
Jeff Sharkey85190e62014-12-05 09:40:12 -08004979 public boolean setLine1NumberForDisplayForSubscriber(int subId, String alphaTag,
4980 String number) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08004981 TelephonyPermissions.enforceCallingOrSelfCarrierPrivilege(
4982 subId, "setLine1NumberForDisplayForSubscriber");
Derek Tan97ebb422014-09-05 16:55:38 -07004983
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004984 final long identity = Binder.clearCallingIdentity();
4985 try {
4986 final String iccId = getIccId(subId);
4987 final Phone phone = getPhone(subId);
4988 if (phone == null) {
4989 return false;
4990 }
4991 final String subscriberId = phone.getSubscriberId();
4992
4993 if (DBG_MERGE) {
4994 Slog.d(LOG_TAG, "Setting line number for ICC=" + iccId + ", subscriberId="
4995 + subscriberId + " to " + number);
4996 }
4997
4998 if (TextUtils.isEmpty(iccId)) {
4999 return false;
5000 }
5001
5002 final SharedPreferences.Editor editor = mTelephonySharedPreferences.edit();
5003
5004 final String alphaTagPrefKey = PREF_CARRIERS_ALPHATAG_PREFIX + iccId;
5005 if (alphaTag == null) {
5006 editor.remove(alphaTagPrefKey);
5007 } else {
5008 editor.putString(alphaTagPrefKey, alphaTag);
5009 }
5010
5011 // Record both the line number and IMSI for this ICCID, since we need to
5012 // track all merged IMSIs based on line number
5013 final String numberPrefKey = PREF_CARRIERS_NUMBER_PREFIX + iccId;
5014 final String subscriberPrefKey = PREF_CARRIERS_SUBSCRIBER_PREFIX + iccId;
5015 if (number == null) {
5016 editor.remove(numberPrefKey);
5017 editor.remove(subscriberPrefKey);
5018 } else {
5019 editor.putString(numberPrefKey, number);
5020 editor.putString(subscriberPrefKey, subscriberId);
5021 }
5022
5023 editor.commit();
5024 return true;
5025 } finally {
5026 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07005027 }
Derek Tan7226c842014-07-02 17:42:23 -07005028 }
5029
5030 @Override
Svet Ganovb320e182015-04-16 12:30:10 -07005031 public String getLine1NumberForDisplay(int subId, String callingPackage) {
Makoto Onukifee69342015-06-29 14:44:50 -07005032 // This is open to apps with WRITE_SMS.
Jeff Davidson7e17e312018-02-13 18:17:36 -08005033 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneNumber(
Jeff Davidson913390f2018-02-23 17:11:49 -08005034 mApp, subId, callingPackage, "getLine1NumberForDisplay")) {
Amit Mahajan9cf11512015-11-09 11:40:48 -08005035 if (DBG_MERGE) log("getLine1NumberForDisplay returning null due to permission");
Svet Ganovb320e182015-04-16 12:30:10 -07005036 return null;
5037 }
Derek Tan97ebb422014-09-05 16:55:38 -07005038
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005039 final long identity = Binder.clearCallingIdentity();
5040 try {
5041 String iccId = getIccId(subId);
5042 if (iccId != null) {
5043 String numberPrefKey = PREF_CARRIERS_NUMBER_PREFIX + iccId;
5044 if (DBG_MERGE) {
5045 log("getLine1NumberForDisplay returning "
5046 + mTelephonySharedPreferences.getString(numberPrefKey, null));
5047 }
5048 return mTelephonySharedPreferences.getString(numberPrefKey, null);
Amit Mahajan9cf11512015-11-09 11:40:48 -08005049 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005050 if (DBG_MERGE) log("getLine1NumberForDisplay returning null as iccId is null");
5051 return null;
5052 } finally {
5053 Binder.restoreCallingIdentity(identity);
Derek Tan7226c842014-07-02 17:42:23 -07005054 }
Derek Tan7226c842014-07-02 17:42:23 -07005055 }
5056
5057 @Override
Svet Ganovb320e182015-04-16 12:30:10 -07005058 public String getLine1AlphaTagForDisplay(int subId, String callingPackage) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08005059 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
Jeff Davidson913390f2018-02-23 17:11:49 -08005060 mApp, subId, callingPackage, "getLine1AlphaTagForDisplay")) {
Svet Ganovb320e182015-04-16 12:30:10 -07005061 return null;
5062 }
Derek Tan97ebb422014-09-05 16:55:38 -07005063
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005064 final long identity = Binder.clearCallingIdentity();
5065 try {
5066 String iccId = getIccId(subId);
5067 if (iccId != null) {
5068 String alphaTagPrefKey = PREF_CARRIERS_ALPHATAG_PREFIX + iccId;
5069 return mTelephonySharedPreferences.getString(alphaTagPrefKey, null);
5070 }
5071 return null;
5072 } finally {
5073 Binder.restoreCallingIdentity(identity);
Derek Tan7226c842014-07-02 17:42:23 -07005074 }
Derek Tan7226c842014-07-02 17:42:23 -07005075 }
Shishir Agrawalb1ebf8c2014-07-17 16:32:41 -07005076
5077 @Override
Jordan Liub49b04b2019-05-06 14:45:15 -07005078 public String[] getMergedSubscriberIds(int subId, String callingPackage) {
Jeff Davidson913390f2018-02-23 17:11:49 -08005079 // This API isn't public, so no need to provide a valid subscription ID - we're not worried
5080 // about carrier-privileged callers not having access.
Jeff Davidson7e17e312018-02-13 18:17:36 -08005081 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
Jeff Davidson913390f2018-02-23 17:11:49 -08005082 mApp, SubscriptionManager.INVALID_SUBSCRIPTION_ID, callingPackage,
5083 "getMergedSubscriberIds")) {
Fyodor Kupolov8e53b0b2015-06-17 13:17:50 -07005084 return null;
5085 }
Jeff Sharkey85190e62014-12-05 09:40:12 -08005086
Jordan Liub49b04b2019-05-06 14:45:15 -07005087 // Clear calling identity, when calling TelephonyManager, because callerUid must be
5088 // the process, where TelephonyManager was instantiated.
5089 // Otherwise AppOps check will fail.
Fyodor Kupolov8e53b0b2015-06-17 13:17:50 -07005090 final long identity = Binder.clearCallingIdentity();
5091 try {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08005092 final Context context = mApp;
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005093 final TelephonyManager tele = TelephonyManager.from(context);
5094 final SubscriptionManager sub = SubscriptionManager.from(context);
5095
5096 // Figure out what subscribers are currently active
5097 final ArraySet<String> activeSubscriberIds = new ArraySet<>();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005098
Jordan Liub49b04b2019-05-06 14:45:15 -07005099 // Only consider subs which match the current subId
5100 // This logic can be simplified. See b/131189269 for progress.
5101 if (isActiveSubscription(subId)) {
Fyodor Kupolov8e53b0b2015-06-17 13:17:50 -07005102 activeSubscriberIds.add(tele.getSubscriberId(subId));
5103 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005104
5105 // First pass, find a number override for an active subscriber
5106 String mergeNumber = null;
5107 final Map<String, ?> prefs = mTelephonySharedPreferences.getAll();
5108 for (String key : prefs.keySet()) {
5109 if (key.startsWith(PREF_CARRIERS_SUBSCRIBER_PREFIX)) {
5110 final String subscriberId = (String) prefs.get(key);
5111 if (activeSubscriberIds.contains(subscriberId)) {
5112 final String iccId = key.substring(
5113 PREF_CARRIERS_SUBSCRIBER_PREFIX.length());
5114 final String numberKey = PREF_CARRIERS_NUMBER_PREFIX + iccId;
5115 mergeNumber = (String) prefs.get(numberKey);
5116 if (DBG_MERGE) {
5117 Slog.d(LOG_TAG, "Found line number " + mergeNumber
5118 + " for active subscriber " + subscriberId);
5119 }
5120 if (!TextUtils.isEmpty(mergeNumber)) {
5121 break;
5122 }
5123 }
5124 }
5125 }
5126
5127 // Shortcut when no active merged subscribers
5128 if (TextUtils.isEmpty(mergeNumber)) {
5129 return null;
5130 }
5131
5132 // Second pass, find all subscribers under that line override
5133 final ArraySet<String> result = new ArraySet<>();
5134 for (String key : prefs.keySet()) {
5135 if (key.startsWith(PREF_CARRIERS_NUMBER_PREFIX)) {
5136 final String number = (String) prefs.get(key);
5137 if (mergeNumber.equals(number)) {
5138 final String iccId = key.substring(PREF_CARRIERS_NUMBER_PREFIX.length());
5139 final String subscriberKey = PREF_CARRIERS_SUBSCRIBER_PREFIX + iccId;
5140 final String subscriberId = (String) prefs.get(subscriberKey);
5141 if (!TextUtils.isEmpty(subscriberId)) {
5142 result.add(subscriberId);
5143 }
5144 }
5145 }
5146 }
5147
5148 final String[] resultArray = result.toArray(new String[result.size()]);
5149 Arrays.sort(resultArray);
5150 if (DBG_MERGE) {
5151 Slog.d(LOG_TAG,
5152 "Found subscribers " + Arrays.toString(resultArray) + " after merge");
5153 }
5154 return resultArray;
Fyodor Kupolov8e53b0b2015-06-17 13:17:50 -07005155 } finally {
5156 Binder.restoreCallingIdentity(identity);
Jeff Sharkey85190e62014-12-05 09:40:12 -08005157 }
Jeff Sharkey85190e62014-12-05 09:40:12 -08005158 }
5159
5160 @Override
Malcolm Chen6ca97372019-07-01 16:28:21 -07005161 public String[] getMergedSubscriberIdsFromGroup(int subId, String callingPackage) {
5162 enforceReadPrivilegedPermission("getMergedSubscriberIdsFromGroup");
5163
5164 final long identity = Binder.clearCallingIdentity();
5165 try {
5166 final TelephonyManager telephonyManager = mApp.getSystemService(
5167 TelephonyManager.class);
5168 String subscriberId = telephonyManager.getSubscriberId(subId);
5169 if (subscriberId == null) {
5170 if (DBG) {
5171 log("getMergedSubscriberIdsFromGroup can't find subscriberId for subId "
5172 + subId);
5173 }
5174 return null;
5175 }
5176
5177 final SubscriptionInfo info = SubscriptionController.getInstance()
5178 .getSubscriptionInfo(subId);
5179 final ParcelUuid groupUuid = info.getGroupUuid();
5180 // If it doesn't belong to any group, return just subscriberId of itself.
5181 if (groupUuid == null) {
5182 return new String[]{subscriberId};
5183 }
5184
5185 // Get all subscriberIds from the group.
5186 final List<String> mergedSubscriberIds = new ArrayList<>();
5187 final List<SubscriptionInfo> groupInfos = SubscriptionController.getInstance()
5188 .getSubscriptionsInGroup(groupUuid, mApp.getOpPackageName());
5189 for (SubscriptionInfo subInfo : groupInfos) {
5190 subscriberId = telephonyManager.getSubscriberId(subInfo.getSubscriptionId());
5191 if (subscriberId != null) {
5192 mergedSubscriberIds.add(subscriberId);
5193 }
5194 }
5195
5196 return mergedSubscriberIds.toArray(new String[mergedSubscriberIds.size()]);
5197 } finally {
5198 Binder.restoreCallingIdentity(identity);
5199
5200 }
5201 }
5202
5203 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08005204 public boolean setOperatorBrandOverride(int subId, String brand) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08005205 TelephonyPermissions.enforceCallingOrSelfCarrierPrivilege(
5206 subId, "setOperatorBrandOverride");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005207
5208 final long identity = Binder.clearCallingIdentity();
5209 try {
5210 final Phone phone = getPhone(subId);
5211 return phone == null ? false : phone.setOperatorBrandOverride(brand);
5212 } finally {
5213 Binder.restoreCallingIdentity(identity);
5214 }
Shishir Agrawalb1ebf8c2014-07-17 16:32:41 -07005215 }
Steven Liu4bf01bc2014-07-17 11:05:29 -05005216
5217 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08005218 public boolean setRoamingOverride(int subId, List<String> gsmRoamingList,
Shishir Agrawal621a47c2014-12-01 10:25:09 -08005219 List<String> gsmNonRoamingList, List<String> cdmaRoamingList,
5220 List<String> cdmaNonRoamingList) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08005221 TelephonyPermissions.enforceCallingOrSelfCarrierPrivilege(subId, "setRoamingOverride");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005222
5223 final long identity = Binder.clearCallingIdentity();
5224 try {
5225 final Phone phone = getPhone(subId);
5226 if (phone == null) {
5227 return false;
5228 }
5229 return phone.setRoamingOverride(gsmRoamingList, gsmNonRoamingList, cdmaRoamingList,
5230 cdmaNonRoamingList);
5231 } finally {
5232 Binder.restoreCallingIdentity(identity);
Shishir Agrawalc04d9752016-02-19 10:41:00 -08005233 }
Shishir Agrawal621a47c2014-12-01 10:25:09 -08005234 }
5235
5236 @Override
Shuo Qian850e4d6a2018-04-25 21:02:08 +00005237 @Deprecated
5238 public int invokeOemRilRequestRaw(byte[] oemReq, byte[] oemResp) {
5239 enforceModifyPermission();
5240
5241 int returnValue = 0;
5242 try {
vagdeviaf9a5b92018-08-15 16:01:53 -07005243 AsyncResult result = (AsyncResult) sendRequest(CMD_INVOKE_OEM_RIL_REQUEST_RAW, oemReq);
Shuo Qian850e4d6a2018-04-25 21:02:08 +00005244 if(result.exception == null) {
5245 if (result.result != null) {
5246 byte[] responseData = (byte[])(result.result);
5247 if(responseData.length > oemResp.length) {
5248 Log.w(LOG_TAG, "Buffer to copy response too small: Response length is " +
5249 responseData.length + "bytes. Buffer Size is " +
5250 oemResp.length + "bytes.");
5251 }
5252 System.arraycopy(responseData, 0, oemResp, 0, responseData.length);
5253 returnValue = responseData.length;
5254 }
5255 } else {
5256 CommandException ex = (CommandException) result.exception;
5257 returnValue = ex.getCommandError().ordinal();
5258 if(returnValue > 0) returnValue *= -1;
5259 }
5260 } catch (RuntimeException e) {
5261 Log.w(LOG_TAG, "sendOemRilRequestRaw: Runtime Exception");
5262 returnValue = (CommandException.Error.GENERIC_FAILURE.ordinal());
5263 if(returnValue > 0) returnValue *= -1;
5264 }
5265
5266 return returnValue;
5267 }
5268
5269 @Override
Wink Saville5d475dd2014-10-17 15:00:58 -07005270 public void setRadioCapability(RadioAccessFamily[] rafs) {
5271 try {
5272 ProxyController.getInstance().setRadioCapability(rafs);
5273 } catch (RuntimeException e) {
5274 Log.w(LOG_TAG, "setRadioCapability: Runtime Exception");
5275 }
5276 }
5277
5278 @Override
Robert Greenwalt36b23af2015-07-06 17:59:14 -07005279 public int getRadioAccessFamily(int phoneId, String callingPackage) {
Jeff Davidson913390f2018-02-23 17:11:49 -08005280 Phone phone = PhoneFactory.getPhone(phoneId);
chen xub97461a2018-10-26 14:17:57 -07005281 int raf = RadioAccessFamily.RAF_UNKNOWN;
Jeff Davidson913390f2018-02-23 17:11:49 -08005282 if (phone == null) {
chen xub97461a2018-10-26 14:17:57 -07005283 return raf;
Jeff Davidson913390f2018-02-23 17:11:49 -08005284 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005285 final long identity = Binder.clearCallingIdentity();
5286 try {
chen xub97461a2018-10-26 14:17:57 -07005287 TelephonyPermissions
5288 .enforeceCallingOrSelfReadPrivilegedPhoneStatePermissionOrCarrierPrivilege(
5289 mApp, phone.getSubId(), "getRadioAccessFamily");
5290 raf = ProxyController.getInstance().getRadioAccessFamily(phoneId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005291 } finally {
5292 Binder.restoreCallingIdentity(identity);
5293 }
chen xub97461a2018-10-26 14:17:57 -07005294 return raf;
Wink Saville5d475dd2014-10-17 15:00:58 -07005295 }
Andrew Leedf14ead2014-10-17 14:22:52 -07005296
5297 @Override
5298 public void enableVideoCalling(boolean enable) {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08005299 final Phone defaultPhone = getDefaultPhone();
Andrew Leedf14ead2014-10-17 14:22:52 -07005300 enforceModifyPermission();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005301
5302 final long identity = Binder.clearCallingIdentity();
5303 try {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08005304 ImsManager.getInstance(defaultPhone.getContext(),
5305 defaultPhone.getPhoneId()).setVtSetting(enable);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005306 } finally {
5307 Binder.restoreCallingIdentity(identity);
5308 }
Andrew Leedf14ead2014-10-17 14:22:52 -07005309 }
5310
5311 @Override
Svet Ganovb320e182015-04-16 12:30:10 -07005312 public boolean isVideoCallingEnabled(String callingPackage) {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08005313 final Phone defaultPhone = getDefaultPhone();
Amit Mahajan578e53d2018-03-20 16:18:38 +00005314 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
Nazanin Bakhshif782e562018-12-11 15:15:39 -08005315 mApp, defaultPhone.getSubId(), callingPackage, "isVideoCallingEnabled")) {
Amit Mahajan578e53d2018-03-20 16:18:38 +00005316 return false;
5317 }
Svet Ganovb320e182015-04-16 12:30:10 -07005318
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005319 final long identity = Binder.clearCallingIdentity();
5320 try {
5321 // Check the user preference and the system-level IMS setting. Even if the user has
5322 // enabled video calling, if IMS is disabled we aren't able to support video calling.
5323 // In the long run, we may instead need to check if there exists a connection service
5324 // which can support video calling.
5325 ImsManager imsManager =
Nazanin Bakhshif782e562018-12-11 15:15:39 -08005326 ImsManager.getInstance(defaultPhone.getContext(), defaultPhone.getPhoneId());
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005327 return imsManager.isVtEnabledByPlatform()
5328 && imsManager.isEnhanced4gLteModeSettingEnabledByUser()
5329 && imsManager.isVtEnabledByUser();
5330 } finally {
5331 Binder.restoreCallingIdentity(identity);
5332 }
Andrew Leedf14ead2014-10-17 14:22:52 -07005333 }
Libin.Tang@motorola.comafe82642014-12-18 13:27:53 -06005334
Andrew Leea1239f22015-03-02 17:44:07 -08005335 @Override
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005336 public boolean canChangeDtmfToneLength(int subId, String callingPackage) {
5337 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
5338 mApp, subId, callingPackage, "isVideoCallingEnabled")) {
5339 return false;
5340 }
5341
5342 final long identity = Binder.clearCallingIdentity();
5343 try {
5344 CarrierConfigManager configManager =
5345 (CarrierConfigManager) mApp.getSystemService(Context.CARRIER_CONFIG_SERVICE);
Nazanin Bakhshif782e562018-12-11 15:15:39 -08005346 return configManager.getConfigForSubId(subId)
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005347 .getBoolean(CarrierConfigManager.KEY_DTMF_TYPE_ENABLED_BOOL);
5348 } finally {
5349 Binder.restoreCallingIdentity(identity);
5350 }
Andrew Leea1239f22015-03-02 17:44:07 -08005351 }
5352
5353 @Override
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005354 public boolean isWorldPhone(int subId, String callingPackage) {
5355 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
5356 mApp, subId, callingPackage, "isVideoCallingEnabled")) {
5357 return false;
5358 }
5359
5360 final long identity = Binder.clearCallingIdentity();
5361 try {
5362 CarrierConfigManager configManager =
5363 (CarrierConfigManager) mApp.getSystemService(Context.CARRIER_CONFIG_SERVICE);
Nazanin Bakhshif782e562018-12-11 15:15:39 -08005364 return configManager.getConfigForSubId(subId)
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005365 .getBoolean(CarrierConfigManager.KEY_WORLD_PHONE_BOOL);
5366 } finally {
5367 Binder.restoreCallingIdentity(identity);
5368 }
Andrew Leea1239f22015-03-02 17:44:07 -08005369 }
5370
Andrew Lee9431b832015-03-09 18:46:45 -07005371 @Override
5372 public boolean isTtyModeSupported() {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08005373 TelecomManager telecomManager = TelecomManager.from(mApp);
Wooki Wu1f82f7a2016-02-15 15:59:58 +08005374 return telecomManager.isTtySupported();
Andrew Lee9431b832015-03-09 18:46:45 -07005375 }
5376
5377 @Override
5378 public boolean isHearingAidCompatibilitySupported() {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005379 final long identity = Binder.clearCallingIdentity();
5380 try {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08005381 return mApp.getResources().getBoolean(R.bool.hac_enabled);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005382 } finally {
5383 Binder.restoreCallingIdentity(identity);
5384 }
Andrew Lee9431b832015-03-09 18:46:45 -07005385 }
5386
Hall Liuf6668912018-10-31 17:05:23 -07005387 /**
5388 * Determines whether the device currently supports RTT (Real-time text). Based both on carrier
5389 * support for the feature and device firmware support.
5390 *
5391 * @return {@code true} if the device and carrier both support RTT, {@code false} otherwise.
5392 */
5393 @Override
5394 public boolean isRttSupported(int subscriptionId) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005395 final long identity = Binder.clearCallingIdentity();
Nazanin Bakhshif782e562018-12-11 15:15:39 -08005396 final Phone phone = getPhone(subscriptionId);
5397 if (phone == null) {
5398 loge("isRttSupported: no Phone found. Invalid subId:" + subscriptionId);
5399 return false;
5400 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005401 try {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08005402 boolean isCarrierSupported = mApp.getCarrierConfigForSubId(subscriptionId).getBoolean(
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005403 CarrierConfigManager.KEY_RTT_SUPPORTED_BOOL);
5404 boolean isDeviceSupported =
Nazanin Bakhshif782e562018-12-11 15:15:39 -08005405 phone.getContext().getResources().getBoolean(R.bool.config_support_rtt);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005406 return isCarrierSupported && isDeviceSupported;
5407 } finally {
5408 Binder.restoreCallingIdentity(identity);
5409 }
Hall Liu98187582018-01-22 19:15:32 -08005410 }
5411
Hall Liuf6668912018-10-31 17:05:23 -07005412 /**
Hall Liuf2daa022019-07-23 18:39:00 -07005413 * Determines whether the user has turned on RTT. If the carrier wants to ignore the user-set
5414 * RTT setting, will return true if the device and carrier both support RTT.
5415 * Otherwise. only returns true if the device and carrier both also support RTT.
Hall Liuf6668912018-10-31 17:05:23 -07005416 */
5417 public boolean isRttEnabled(int subscriptionId) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005418 final long identity = Binder.clearCallingIdentity();
5419 try {
Hall Liuf2daa022019-07-23 18:39:00 -07005420 boolean isRttSupported = isRttSupported(subscriptionId);
5421 boolean isUserRttSettingOn = Settings.Secure.getInt(
Nazanin Bakhshif782e562018-12-11 15:15:39 -08005422 mApp.getContentResolver(), Settings.Secure.RTT_CALLING_MODE, 0) != 0;
Hall Liuf2daa022019-07-23 18:39:00 -07005423 boolean shouldIgnoreUserRttSetting = mApp.getCarrierConfigForSubId(subscriptionId)
5424 .getBoolean(CarrierConfigManager.KEY_IGNORE_RTT_MODE_SETTING_BOOL);
5425 return isRttSupported && (isUserRttSettingOn || shouldIgnoreUserRttSetting);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005426 } finally {
5427 Binder.restoreCallingIdentity(identity);
5428 }
Hall Liu3ad5f012018-04-06 16:23:39 -07005429 }
5430
Sanket Padawe7310cc72015-01-14 09:53:20 -08005431 /**
5432 * Returns the unique device ID of phone, for example, the IMEI for
5433 * GSM and the MEID for CDMA phones. Return null if device ID is not available.
5434 *
5435 * <p>Requires Permission:
5436 * {@link android.Manifest.permission#READ_PHONE_STATE READ_PHONE_STATE}
5437 */
5438 @Override
Svet Ganovb320e182015-04-16 12:30:10 -07005439 public String getDeviceId(String callingPackage) {
Sanket Padawe7310cc72015-01-14 09:53:20 -08005440 final Phone phone = PhoneFactory.getPhone(0);
Jeff Davidson913390f2018-02-23 17:11:49 -08005441 if (phone == null) {
Sanket Padawe7310cc72015-01-14 09:53:20 -08005442 return null;
5443 }
Jeff Davidson913390f2018-02-23 17:11:49 -08005444 int subId = phone.getSubId();
Michael Groover70af6dc2018-10-01 16:23:15 -07005445 if (!TelephonyPermissions.checkCallingOrSelfReadDeviceIdentifiers(mApp, subId,
5446 callingPackage, "getDeviceId")) {
Jeff Davidson913390f2018-02-23 17:11:49 -08005447 return null;
5448 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005449
5450 final long identity = Binder.clearCallingIdentity();
5451 try {
5452 return phone.getDeviceId();
5453 } finally {
5454 Binder.restoreCallingIdentity(identity);
5455 }
Sanket Padawe7310cc72015-01-14 09:53:20 -08005456 }
5457
Ping Sunc67b7c22016-03-02 19:16:45 +08005458 /**
5459 * {@hide}
5460 * Returns the IMS Registration Status on a particular subid
5461 *
5462 * @param subId
5463 */
Brad Ebinger1f2b5082018-02-08 16:11:32 -08005464 public boolean isImsRegistered(int subId) {
Ping Sunc67b7c22016-03-02 19:16:45 +08005465 Phone phone = getPhone(subId);
5466 if (phone != null) {
5467 return phone.isImsRegistered();
5468 } else {
5469 return false;
5470 }
5471 }
5472
Santos Cordon7a1885b2015-02-03 11:15:19 -08005473 @Override
5474 public int getSubIdForPhoneAccount(PhoneAccount phoneAccount) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005475 final long identity = Binder.clearCallingIdentity();
5476 try {
5477 return PhoneUtils.getSubIdForPhoneAccount(phoneAccount);
5478 } finally {
5479 Binder.restoreCallingIdentity(identity);
5480 }
Santos Cordon7a1885b2015-02-03 11:15:19 -08005481 }
Nathan Harolddcfc7932015-03-18 10:01:20 -07005482
Tyler Gunnf70ed162019-04-03 15:28:53 -07005483 @Override
5484 public @Nullable PhoneAccountHandle getPhoneAccountHandleForSubscriptionId(int subscriptionId) {
5485 final long identity = Binder.clearCallingIdentity();
5486 try {
5487 Phone phone = getPhone(subscriptionId);
5488 if (phone == null) {
5489 return null;
5490 }
5491 return PhoneUtils.makePstnPhoneAccountHandle(phone);
5492 } finally {
5493 Binder.restoreCallingIdentity(identity);
5494 }
5495 }
5496
Brad Ebinger1f2b5082018-02-08 16:11:32 -08005497 /**
5498 * @return the VoWiFi calling availability.
Nathan Haroldc55097a2015-03-11 18:14:50 -07005499 */
Brad Ebinger1f2b5082018-02-08 16:11:32 -08005500 public boolean isWifiCallingAvailable(int subId) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005501 final long identity = Binder.clearCallingIdentity();
5502 try {
5503 Phone phone = getPhone(subId);
5504 if (phone != null) {
5505 return phone.isWifiCallingEnabled();
5506 } else {
5507 return false;
5508 }
5509 } finally {
5510 Binder.restoreCallingIdentity(identity);
Brad Ebinger1f2b5082018-02-08 16:11:32 -08005511 }
Nathan Haroldc55097a2015-03-11 18:14:50 -07005512 }
5513
Brad Ebinger1f2b5082018-02-08 16:11:32 -08005514 /**
Brad Ebinger1f2b5082018-02-08 16:11:32 -08005515 * @return the VT calling availability.
Etan Cohen3b7a1bc2015-05-28 15:57:13 -07005516 */
Brad Ebinger1f2b5082018-02-08 16:11:32 -08005517 public boolean isVideoTelephonyAvailable(int subId) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005518 final long identity = Binder.clearCallingIdentity();
5519 try {
5520 Phone phone = getPhone(subId);
5521 if (phone != null) {
5522 return phone.isVideoEnabled();
5523 } else {
5524 return false;
5525 }
5526 } finally {
5527 Binder.restoreCallingIdentity(identity);
Brad Ebinger1f2b5082018-02-08 16:11:32 -08005528 }
5529 }
5530
5531 /**
5532 * @return the IMS registration technology for the MMTEL feature. Valid return values are
5533 * defined in {@link ImsRegistrationImplBase}.
5534 */
5535 public @ImsRegistrationImplBase.ImsRegistrationTech int getImsRegTechnologyForMmTel(int subId) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005536 final long identity = Binder.clearCallingIdentity();
5537 try {
5538 Phone phone = getPhone(subId);
5539 if (phone != null) {
5540 return phone.getImsRegistrationTech();
5541 } else {
5542 return ImsRegistrationImplBase.REGISTRATION_TECH_NONE;
5543 }
5544 } finally {
5545 Binder.restoreCallingIdentity(identity);
Brad Ebinger1f2b5082018-02-08 16:11:32 -08005546 }
Etan Cohen3b7a1bc2015-05-28 15:57:13 -07005547 }
5548
Stuart Scott8eef64f2015-04-08 15:13:54 -07005549 @Override
5550 public void factoryReset(int subId) {
5551 enforceConnectivityInternalPermission();
Stuart Scott981d8582015-04-21 14:09:50 -07005552 if (mUserManager.hasUserRestriction(UserManager.DISALLOW_NETWORK_RESET)) {
5553 return;
5554 }
5555
Svet Ganovcc087f82015-05-12 20:35:54 -07005556 final long identity = Binder.clearCallingIdentity();
Nazanin Bakhshif782e562018-12-11 15:15:39 -08005557
Svet Ganovcc087f82015-05-12 20:35:54 -07005558 try {
Stuart Scott981d8582015-04-21 14:09:50 -07005559 if (SubscriptionManager.isUsableSubIdValue(subId) && !mUserManager.hasUserRestriction(
5560 UserManager.DISALLOW_CONFIG_MOBILE_NETWORKS)) {
Pengquan Meng85728fb2018-03-12 16:31:21 -07005561 setUserDataEnabled(subId, getDefaultDataEnabled());
Svet Ganovcc087f82015-05-12 20:35:54 -07005562 setNetworkSelectionModeAutomatic(subId);
Pengquan Meng85728fb2018-03-12 16:31:21 -07005563 setPreferredNetworkType(subId, getDefaultNetworkType(subId));
Nazanin Bakhshif782e562018-12-11 15:15:39 -08005564 setDataRoamingEnabled(subId, getDefaultDataRoamingEnabled(subId));
5565 CarrierInfoManager.deleteAllCarrierKeysForImsiEncryption(mApp);
Svet Ganovcc087f82015-05-12 20:35:54 -07005566 }
Amit Mahajan7dbbd822019-03-13 17:33:47 -07005567 // There has been issues when Sms raw table somehow stores orphan
5568 // fragments. They lead to garbled message when new fragments come
5569 // in and combined with those stale ones. In case this happens again,
5570 // user can reset all network settings which will clean up this table.
5571 cleanUpSmsRawTable(getDefaultPhone().getContext());
Naina Nalluri1264a9f2019-09-17 14:10:30 -07005572
5573 // Erase modem config if erase modem on network setting is enabled.
5574 String configValue = DeviceConfig.getProperty(DeviceConfig.NAMESPACE_TELEPHONY,
5575 RESET_NETWORK_ERASE_MODEM_CONFIG_ENABLED);
5576 if (configValue != null && Boolean.parseBoolean(configValue)) {
5577 sendEraseModemConfig(getDefaultPhone());
5578 }
Svet Ganovcc087f82015-05-12 20:35:54 -07005579 } finally {
5580 Binder.restoreCallingIdentity(identity);
Stuart Scott8eef64f2015-04-08 15:13:54 -07005581 }
5582 }
Narayan Kamath1c496c22015-04-16 14:40:19 +01005583
Amit Mahajan7dbbd822019-03-13 17:33:47 -07005584 private void cleanUpSmsRawTable(Context context) {
5585 ContentResolver resolver = context.getContentResolver();
5586 Uri uri = Uri.withAppendedPath(Telephony.Sms.CONTENT_URI, "raw/permanentDelete");
5587 resolver.delete(uri, null, null);
5588 }
5589
Narayan Kamath1c496c22015-04-16 14:40:19 +01005590 @Override
chen xu5d3637b2019-01-21 23:31:38 -08005591 public String getSimLocaleForSubscriber(int subId) {
5592 enforceReadPrivilegedPermission("getSimLocaleForSubscriber, subId: " + subId);
5593 final Phone phone = getPhone(subId);
5594 if (phone == null) {
5595 log("getSimLocaleForSubscriber, invalid subId");
chen xu2bb91e42019-01-24 14:35:54 -08005596 return null;
chen xu5d3637b2019-01-21 23:31:38 -08005597 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005598 final long identity = Binder.clearCallingIdentity();
5599 try {
chen xu5d3637b2019-01-21 23:31:38 -08005600 final SubscriptionInfo info = mSubscriptionController.getActiveSubscriptionInfo(subId,
5601 phone.getContext().getOpPackageName());
chen xu6291c472019-02-04 12:55:53 -08005602 if (info == null) {
5603 log("getSimLocaleForSubscriber, inactive subId: " + subId);
5604 return null;
5605 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005606 // Try and fetch the locale from the carrier properties or from the SIM language
5607 // preferences (EF-PL and EF-LI)...
5608 final int mcc = info.getMcc();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005609 String simLanguage = null;
chen xu5d3637b2019-01-21 23:31:38 -08005610 final Locale localeFromDefaultSim = phone.getLocaleFromSimAndCarrierPrefs();
5611 if (localeFromDefaultSim != null) {
5612 if (!localeFromDefaultSim.getCountry().isEmpty()) {
5613 if (DBG) log("Using locale from subId: " + subId + " locale: "
5614 + localeFromDefaultSim);
5615 return localeFromDefaultSim.toLanguageTag();
5616 } else {
5617 simLanguage = localeFromDefaultSim.getLanguage();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005618 }
5619 }
Narayan Kamath1c496c22015-04-16 14:40:19 +01005620
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005621 // The SIM language preferences only store a language (e.g. fr = French), not an
5622 // exact locale (e.g. fr_FR = French/France). So, if the locale returned from
5623 // the SIM and carrier preferences does not include a country we add the country
5624 // determined from the SIM MCC to provide an exact locale.
Nazanin Bakhshif782e562018-12-11 15:15:39 -08005625 final Locale mccLocale = MccTable.getLocaleFromMcc(mApp, mcc, simLanguage);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005626 if (mccLocale != null) {
chen xu5d3637b2019-01-21 23:31:38 -08005627 if (DBG) log("No locale from SIM, using mcc locale:" + mccLocale);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005628 return mccLocale.toLanguageTag();
5629 }
5630
5631 if (DBG) log("No locale found - returning null");
5632 return null;
5633 } finally {
5634 Binder.restoreCallingIdentity(identity);
5635 }
Narayan Kamath1c496c22015-04-16 14:40:19 +01005636 }
5637
5638 private List<SubscriptionInfo> getAllSubscriptionInfoList() {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08005639 return mSubscriptionController.getAllSubInfoList(mApp.getOpPackageName());
Narayan Kamath1c496c22015-04-16 14:40:19 +01005640 }
5641
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005642 /**
5643 * NOTE: this method assumes permission checks are done and caller identity has been cleared.
5644 */
5645 private List<SubscriptionInfo> getActiveSubscriptionInfoListPrivileged() {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08005646 return mSubscriptionController.getActiveSubscriptionInfoList(mApp.getOpPackageName());
Narayan Kamath1c496c22015-04-16 14:40:19 +01005647 }
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -07005648
Chenjie Yu1ba97252018-01-11 18:16:20 -08005649 private final ModemActivityInfo mLastModemActivityInfo =
5650 new ModemActivityInfo(0, 0, 0, new int[0], 0, 0);
5651
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -07005652 /**
Adam Lesinski903a54c2016-04-11 14:49:52 -07005653 * Responds to the ResultReceiver with the {@link android.telephony.ModemActivityInfo} object
5654 * representing the state of the modem.
5655 *
Chenjie Yu1ba97252018-01-11 18:16:20 -08005656 * NOTE: The underlying implementation clears the modem state, so there should only ever be one
5657 * caller to it. Everyone should call this class to get cumulative data.
Adam Lesinski903a54c2016-04-11 14:49:52 -07005658 * @hide
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -07005659 */
5660 @Override
Adam Lesinski903a54c2016-04-11 14:49:52 -07005661 public void requestModemActivityInfo(ResultReceiver result) {
5662 enforceModifyPermission();
vagdeviaf9a5b92018-08-15 16:01:53 -07005663 WorkSource workSource = getWorkSource(Binder.getCallingUid());
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005664
5665 final long identity = Binder.clearCallingIdentity();
5666 try {
5667 ModemActivityInfo ret = null;
5668 synchronized (mLastModemActivityInfo) {
5669 ModemActivityInfo info = (ModemActivityInfo) sendRequest(
5670 CMD_GET_MODEM_ACTIVITY_INFO,
vagdeviaf9a5b92018-08-15 16:01:53 -07005671 null, workSource);
Siddharth Rayb8114062018-06-17 15:02:38 -07005672 if (isModemActivityInfoValid(info)) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005673 int[] mergedTxTimeMs = new int[ModemActivityInfo.TX_POWER_LEVELS];
5674 for (int i = 0; i < mergedTxTimeMs.length; i++) {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08005675 mergedTxTimeMs[i] = info.getTxTimeMillis()[i]
5676 + mLastModemActivityInfo.getTxTimeMillis()[i];
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005677 }
5678 mLastModemActivityInfo.setTimestamp(info.getTimestamp());
Nazanin Bakhshif782e562018-12-11 15:15:39 -08005679 mLastModemActivityInfo.setSleepTimeMillis(info.getSleepTimeMillis()
5680 + mLastModemActivityInfo.getSleepTimeMillis());
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005681 mLastModemActivityInfo.setIdleTimeMillis(
5682 info.getIdleTimeMillis() + mLastModemActivityInfo.getIdleTimeMillis());
5683 mLastModemActivityInfo.setTxTimeMillis(mergedTxTimeMs);
5684 mLastModemActivityInfo.setRxTimeMillis(
5685 info.getRxTimeMillis() + mLastModemActivityInfo.getRxTimeMillis());
5686 mLastModemActivityInfo.setEnergyUsed(
5687 info.getEnergyUsed() + mLastModemActivityInfo.getEnergyUsed());
Chenjie Yu1ba97252018-01-11 18:16:20 -08005688 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005689 ret = new ModemActivityInfo(mLastModemActivityInfo.getTimestamp(),
5690 mLastModemActivityInfo.getSleepTimeMillis(),
5691 mLastModemActivityInfo.getIdleTimeMillis(),
5692 mLastModemActivityInfo.getTxTimeMillis(),
5693 mLastModemActivityInfo.getRxTimeMillis(),
5694 mLastModemActivityInfo.getEnergyUsed());
Chenjie Yu1ba97252018-01-11 18:16:20 -08005695 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005696 Bundle bundle = new Bundle();
5697 bundle.putParcelable(TelephonyManager.MODEM_ACTIVITY_RESULT_KEY, ret);
5698 result.send(0, bundle);
5699 } finally {
5700 Binder.restoreCallingIdentity(identity);
Chenjie Yu1ba97252018-01-11 18:16:20 -08005701 }
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -07005702 }
Jack Yu85bd38a2015-11-09 11:34:32 -08005703
Siddharth Rayb8114062018-06-17 15:02:38 -07005704 // Checks that ModemActivityInfo is valid. Sleep time, Idle time, Rx time and Tx time should be
5705 // less than total activity duration.
5706 private boolean isModemActivityInfoValid(ModemActivityInfo info) {
5707 if (info == null) {
5708 return false;
5709 }
5710 int activityDurationMs =
5711 (int) (info.getTimestamp() - mLastModemActivityInfo.getTimestamp());
5712 int totalTxTimeMs = 0;
5713 for (int i = 0; i < info.getTxTimeMillis().length; i++) {
5714 totalTxTimeMs += info.getTxTimeMillis()[i];
5715 }
5716 return (info.isValid()
5717 && (info.getSleepTimeMillis() <= activityDurationMs)
5718 && (info.getIdleTimeMillis() <= activityDurationMs)
5719 && (info.getRxTimeMillis() <= activityDurationMs)
5720 && (totalTxTimeMs <= activityDurationMs));
5721 }
5722
Jack Yu85bd38a2015-11-09 11:34:32 -08005723 /**
5724 * {@hide}
5725 * Returns the service state information on specified subscription.
5726 */
5727 @Override
5728 public ServiceState getServiceStateForSubscriber(int subId, String callingPackage) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08005729 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
Jeff Davidson913390f2018-02-23 17:11:49 -08005730 mApp, subId, callingPackage, "getServiceStateForSubscriber")) {
Jack Yu85bd38a2015-11-09 11:34:32 -08005731 return null;
5732 }
5733
Hall Liuf19c44f2018-11-27 14:38:17 -08005734 LocationAccessPolicy.LocationPermissionResult fineLocationResult =
5735 LocationAccessPolicy.checkLocationPermission(mApp,
5736 new LocationAccessPolicy.LocationPermissionQuery.Builder()
5737 .setCallingPackage(callingPackage)
5738 .setCallingPid(Binder.getCallingPid())
5739 .setCallingUid(Binder.getCallingUid())
5740 .setMethod("getServiceStateForSubscriber")
Hall Liuf18a0cf2019-04-17 13:37:11 -07005741 .setLogAsInfo(true)
Hall Liuf19c44f2018-11-27 14:38:17 -08005742 .setMinSdkVersionForFine(Build.VERSION_CODES.Q)
5743 .build());
5744
5745 LocationAccessPolicy.LocationPermissionResult coarseLocationResult =
5746 LocationAccessPolicy.checkLocationPermission(mApp,
5747 new LocationAccessPolicy.LocationPermissionQuery.Builder()
5748 .setCallingPackage(callingPackage)
5749 .setCallingPid(Binder.getCallingPid())
5750 .setCallingUid(Binder.getCallingUid())
5751 .setMethod("getServiceStateForSubscriber")
Hall Liuf18a0cf2019-04-17 13:37:11 -07005752 .setLogAsInfo(true)
Hall Liuf19c44f2018-11-27 14:38:17 -08005753 .setMinSdkVersionForCoarse(Build.VERSION_CODES.Q)
5754 .build());
5755 // We don't care about hard or soft here -- all we need to know is how much info to scrub.
5756 boolean hasFinePermission =
5757 fineLocationResult == LocationAccessPolicy.LocationPermissionResult.ALLOWED;
5758 boolean hasCoarsePermission =
5759 coarseLocationResult == LocationAccessPolicy.LocationPermissionResult.ALLOWED;
5760
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005761 final long identity = Binder.clearCallingIdentity();
5762 try {
5763 final Phone phone = getPhone(subId);
5764 if (phone == null) {
5765 return null;
5766 }
Jack Yu85bd38a2015-11-09 11:34:32 -08005767
Hall Liuf19c44f2018-11-27 14:38:17 -08005768 ServiceState ss = phone.getServiceState();
5769
5770 // Scrub out the location info in ServiceState depending on what level of access
5771 // the caller has.
5772 if (hasFinePermission) return ss;
5773 if (hasCoarsePermission) return ss.sanitizeLocationInfo(false);
5774 return ss.sanitizeLocationInfo(true);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005775 } finally {
5776 Binder.restoreCallingIdentity(identity);
5777 }
Jack Yu85bd38a2015-11-09 11:34:32 -08005778 }
Nancy Chen31f9ba12016-01-06 11:42:12 -08005779
5780 /**
5781 * Returns the URI for the per-account voicemail ringtone set in Phone settings.
5782 *
5783 * @param accountHandle The handle for the {@link PhoneAccount} for which to retrieve the
5784 * voicemail ringtone.
5785 * @return The URI for the ringtone to play when receiving a voicemail from a specific
5786 * PhoneAccount.
5787 */
5788 @Override
5789 public Uri getVoicemailRingtoneUri(PhoneAccountHandle accountHandle) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005790 final long identity = Binder.clearCallingIdentity();
5791 try {
5792 Phone phone = PhoneUtils.getPhoneForPhoneAccountHandle(accountHandle);
5793 if (phone == null) {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08005794 phone = getDefaultPhone();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005795 }
Nancy Chen31f9ba12016-01-06 11:42:12 -08005796
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005797 return VoicemailNotificationSettingsUtil.getRingtoneUri(phone.getContext());
5798 } finally {
5799 Binder.restoreCallingIdentity(identity);
5800 }
Nancy Chen31f9ba12016-01-06 11:42:12 -08005801 }
5802
5803 /**
Ta-wei Yenc33877d2017-01-23 18:11:21 -08005804 * Sets the per-account voicemail ringtone.
5805 *
5806 * <p>Requires that the calling app is the default dialer, or has carrier privileges, or
5807 * has permission {@link android.Manifest.permission#MODIFY_PHONE_STATE MODIFY_PHONE_STATE}.
5808 *
5809 * @param phoneAccountHandle The handle for the {@link PhoneAccount} for which to set the
5810 * voicemail ringtone.
5811 * @param uri The URI for the ringtone to play when receiving a voicemail from a specific
5812 * PhoneAccount.
5813 */
5814 @Override
5815 public void setVoicemailRingtoneUri(String callingPackage,
5816 PhoneAccountHandle phoneAccountHandle, Uri uri) {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08005817 final Phone defaultPhone = getDefaultPhone();
Ta-wei Yenc33877d2017-01-23 18:11:21 -08005818 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
5819 if (!TextUtils.equals(callingPackage,
Nazanin Bakhshif782e562018-12-11 15:15:39 -08005820 TelecomManager.from(defaultPhone.getContext()).getDefaultDialerPackage())) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08005821 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
5822 mApp, PhoneUtils.getSubIdForPhoneAccountHandle(phoneAccountHandle),
5823 "setVoicemailRingtoneUri");
Ta-wei Yenc33877d2017-01-23 18:11:21 -08005824 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005825
5826 final long identity = Binder.clearCallingIdentity();
5827 try {
5828 Phone phone = PhoneUtils.getPhoneForPhoneAccountHandle(phoneAccountHandle);
5829 if (phone == null) {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08005830 phone = defaultPhone;
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005831 }
5832 VoicemailNotificationSettingsUtil.setRingtoneUri(phone.getContext(), uri);
5833 } finally {
5834 Binder.restoreCallingIdentity(identity);
Ta-wei Yenc33877d2017-01-23 18:11:21 -08005835 }
Ta-wei Yenc33877d2017-01-23 18:11:21 -08005836 }
5837
5838 /**
Nancy Chen31f9ba12016-01-06 11:42:12 -08005839 * Returns whether vibration is set for voicemail notification in Phone settings.
5840 *
5841 * @param accountHandle The handle for the {@link PhoneAccount} for which to retrieve the
5842 * voicemail vibration setting.
5843 * @return {@code true} if the vibration is set for this PhoneAccount, {@code false} otherwise.
5844 */
5845 @Override
5846 public boolean isVoicemailVibrationEnabled(PhoneAccountHandle accountHandle) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005847 final long identity = Binder.clearCallingIdentity();
5848 try {
5849 Phone phone = PhoneUtils.getPhoneForPhoneAccountHandle(accountHandle);
5850 if (phone == null) {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08005851 phone = getDefaultPhone();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005852 }
Nancy Chen31f9ba12016-01-06 11:42:12 -08005853
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005854 return VoicemailNotificationSettingsUtil.isVibrationEnabled(phone.getContext());
5855 } finally {
5856 Binder.restoreCallingIdentity(identity);
5857 }
Nancy Chen31f9ba12016-01-06 11:42:12 -08005858 }
5859
Youhan Wange64578a2016-05-02 15:32:42 -07005860 /**
Ta-wei Yenc33877d2017-01-23 18:11:21 -08005861 * Sets the per-account voicemail vibration.
5862 *
5863 * <p>Requires that the calling app is the default dialer, or has carrier privileges, or
5864 * has permission {@link android.Manifest.permission#MODIFY_PHONE_STATE MODIFY_PHONE_STATE}.
5865 *
5866 * @param phoneAccountHandle The handle for the {@link PhoneAccount} for which to set the
5867 * voicemail vibration setting.
5868 * @param enabled Whether to enable or disable vibration for voicemail notifications from a
5869 * specific PhoneAccount.
5870 */
5871 @Override
5872 public void setVoicemailVibrationEnabled(String callingPackage,
5873 PhoneAccountHandle phoneAccountHandle, boolean enabled) {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08005874 final Phone defaultPhone = getDefaultPhone();
Ta-wei Yenc33877d2017-01-23 18:11:21 -08005875 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
5876 if (!TextUtils.equals(callingPackage,
Nazanin Bakhshif782e562018-12-11 15:15:39 -08005877 TelecomManager.from(defaultPhone.getContext()).getDefaultDialerPackage())) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08005878 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
5879 mApp, PhoneUtils.getSubIdForPhoneAccountHandle(phoneAccountHandle),
5880 "setVoicemailVibrationEnabled");
Ta-wei Yenc33877d2017-01-23 18:11:21 -08005881 }
5882
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005883 final long identity = Binder.clearCallingIdentity();
5884 try {
5885 Phone phone = PhoneUtils.getPhoneForPhoneAccountHandle(phoneAccountHandle);
5886 if (phone == null) {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08005887 phone = defaultPhone;
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005888 }
5889 VoicemailNotificationSettingsUtil.setVibrationEnabled(phone.getContext(), enabled);
5890 } finally {
5891 Binder.restoreCallingIdentity(identity);
Ta-wei Yenc33877d2017-01-23 18:11:21 -08005892 }
Ta-wei Yenc33877d2017-01-23 18:11:21 -08005893 }
5894
5895 /**
Youhan Wange64578a2016-05-02 15:32:42 -07005896 * Make sure either called from same process as self (phone) or IPC caller has read privilege.
5897 *
5898 * @throws SecurityException if the caller does not have the required permission
5899 */
Brad Ebinger35c841c2018-10-01 10:40:55 -07005900 private void enforceReadPrivilegedPermission(String message) {
Youhan Wange64578a2016-05-02 15:32:42 -07005901 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.READ_PRIVILEGED_PHONE_STATE,
Brad Ebinger35c841c2018-10-01 10:40:55 -07005902 message);
Youhan Wange64578a2016-05-02 15:32:42 -07005903 }
5904
5905 /**
Ta-wei Yen30a69c82016-12-27 14:52:32 -08005906 * Make sure either called from same process as self (phone) or IPC caller has send SMS
5907 * permission.
5908 *
5909 * @throws SecurityException if the caller does not have the required permission
5910 */
5911 private void enforceSendSmsPermission() {
5912 mApp.enforceCallingOrSelfPermission(permission.SEND_SMS, null);
5913 }
5914
5915 /**
Ta-wei Yen527a9c02017-01-06 15:29:25 -08005916 * Make sure called from the package in charge of visual voicemail.
Ta-wei Yen30a69c82016-12-27 14:52:32 -08005917 *
Ta-wei Yen527a9c02017-01-06 15:29:25 -08005918 * @throws SecurityException if the caller is not the visual voicemail package.
Ta-wei Yen30a69c82016-12-27 14:52:32 -08005919 */
Ta-wei Yen527a9c02017-01-06 15:29:25 -08005920 private void enforceVisualVoicemailPackage(String callingPackage, int subId) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005921 final long identity = Binder.clearCallingIdentity();
5922 try {
5923 ComponentName componentName =
Nazanin Bakhshif782e562018-12-11 15:15:39 -08005924 RemoteVvmTaskManager.getRemotePackage(mApp, subId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005925 if (componentName == null) {
5926 throw new SecurityException(
5927 "Caller not current active visual voicemail package[null]");
5928 }
5929 String vvmPackage = componentName.getPackageName();
5930 if (!callingPackage.equals(vvmPackage)) {
5931 throw new SecurityException("Caller not current active visual voicemail package["
5932 + vvmPackage + "]");
5933 }
5934 } finally {
5935 Binder.restoreCallingIdentity(identity);
Ta-wei Yen30a69c82016-12-27 14:52:32 -08005936 }
5937 }
5938
5939 /**
Youhan Wange64578a2016-05-02 15:32:42 -07005940 * Return the application ID for the app type.
5941 *
5942 * @param subId the subscription ID that this request applies to.
5943 * @param appType the uicc app type.
5944 * @return Application ID for specificied app type, or null if no uicc.
5945 */
5946 @Override
5947 public String getAidForAppType(int subId, int appType) {
Brad Ebinger35c841c2018-10-01 10:40:55 -07005948 enforceReadPrivilegedPermission("getAidForAppType");
Youhan Wange64578a2016-05-02 15:32:42 -07005949 Phone phone = getPhone(subId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005950
5951 final long identity = Binder.clearCallingIdentity();
Youhan Wange64578a2016-05-02 15:32:42 -07005952 try {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005953 if (phone == null) {
5954 return null;
5955 }
5956 String aid = null;
5957 try {
5958 aid = UiccController.getInstance().getUiccCard(phone.getPhoneId())
5959 .getApplicationByType(appType).getAid();
5960 } catch (Exception e) {
5961 Log.e(LOG_TAG, "Not getting aid. Exception ex=" + e);
5962 }
5963 return aid;
5964 } finally {
5965 Binder.restoreCallingIdentity(identity);
Youhan Wange64578a2016-05-02 15:32:42 -07005966 }
Youhan Wange64578a2016-05-02 15:32:42 -07005967 }
5968
Youhan Wang4001d252016-05-11 10:29:41 -07005969 /**
5970 * Return the Electronic Serial Number.
5971 *
5972 * @param subId the subscription ID that this request applies to.
5973 * @return ESN or null if error.
5974 */
5975 @Override
5976 public String getEsn(int subId) {
Brad Ebinger35c841c2018-10-01 10:40:55 -07005977 enforceReadPrivilegedPermission("getEsn");
Youhan Wang4001d252016-05-11 10:29:41 -07005978 Phone phone = getPhone(subId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005979
5980 final long identity = Binder.clearCallingIdentity();
Youhan Wang4001d252016-05-11 10:29:41 -07005981 try {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005982 if (phone == null) {
5983 return null;
5984 }
5985 String esn = null;
5986 try {
5987 esn = phone.getEsn();
5988 } catch (Exception e) {
5989 Log.e(LOG_TAG, "Not getting ESN. Exception ex=" + e);
5990 }
5991 return esn;
5992 } finally {
5993 Binder.restoreCallingIdentity(identity);
Youhan Wang4001d252016-05-11 10:29:41 -07005994 }
Youhan Wang4001d252016-05-11 10:29:41 -07005995 }
5996
Sanket Padawe99ef1e32016-05-18 16:12:33 -07005997 /**
Youhan Wang66ad5d72016-07-18 17:56:58 -07005998 * Return the Preferred Roaming List Version.
5999 *
6000 * @param subId the subscription ID that this request applies to.
6001 * @return PRLVersion or null if error.
6002 */
6003 @Override
6004 public String getCdmaPrlVersion(int subId) {
Brad Ebinger35c841c2018-10-01 10:40:55 -07006005 enforceReadPrivilegedPermission("getCdmaPrlVersion");
Youhan Wang66ad5d72016-07-18 17:56:58 -07006006 Phone phone = getPhone(subId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006007
6008 final long identity = Binder.clearCallingIdentity();
Youhan Wang66ad5d72016-07-18 17:56:58 -07006009 try {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006010 if (phone == null) {
6011 return null;
6012 }
6013 String cdmaPrlVersion = null;
6014 try {
6015 cdmaPrlVersion = phone.getCdmaPrlVersion();
6016 } catch (Exception e) {
6017 Log.e(LOG_TAG, "Not getting PRLVersion", e);
6018 }
6019 return cdmaPrlVersion;
6020 } finally {
6021 Binder.restoreCallingIdentity(identity);
Youhan Wang66ad5d72016-07-18 17:56:58 -07006022 }
Youhan Wang66ad5d72016-07-18 17:56:58 -07006023 }
6024
6025 /**
Sanket Padawe99ef1e32016-05-18 16:12:33 -07006026 * Get snapshot of Telephony histograms
6027 * @return List of Telephony histograms
6028 * @hide
6029 */
6030 @Override
6031 public List<TelephonyHistogram> getTelephonyHistograms() {
Jeff Davidson7e17e312018-02-13 18:17:36 -08006032 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
6033 mApp, getDefaultSubscription(), "getTelephonyHistograms");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006034
6035 final long identity = Binder.clearCallingIdentity();
6036 try {
6037 return RIL.getTelephonyRILTimingHistograms();
6038 } finally {
6039 Binder.restoreCallingIdentity(identity);
6040 }
Sanket Padawe99ef1e32016-05-18 16:12:33 -07006041 }
Meng Wang1a7c35a2016-05-05 20:56:15 -07006042
6043 /**
6044 * {@hide}
Michele Berionne482f8202018-11-27 18:57:59 -08006045 * Set the allowed carrier list and the excluded carrier list, indicating the priority between
6046 * the two lists.
Meng Wang1a7c35a2016-05-05 20:56:15 -07006047 * Require system privileges. In the future we may add this to carrier APIs.
6048 *
Michele Berionne482f8202018-11-27 18:57:59 -08006049 * @return Integer with the result of the operation, as defined in {@link TelephonyManager}.
Meng Wang1a7c35a2016-05-05 20:56:15 -07006050 */
6051 @Override
Michele Berionne482f8202018-11-27 18:57:59 -08006052 @TelephonyManager.SetCarrierRestrictionResult
6053 public int setAllowedCarriers(CarrierRestrictionRules carrierRestrictionRules) {
Meng Wang1a7c35a2016-05-05 20:56:15 -07006054 enforceModifyPermission();
vagdeviaf9a5b92018-08-15 16:01:53 -07006055 WorkSource workSource = getWorkSource(Binder.getCallingUid());
Sanket Padawe13bac7b2017-03-20 15:04:47 -07006056
Michele Berionne482f8202018-11-27 18:57:59 -08006057 if (carrierRestrictionRules == null) {
6058 throw new NullPointerException("carrier restriction cannot be null");
Meng Wang9b7c4e92017-02-17 11:41:27 -08006059 }
Sanket Padawe13bac7b2017-03-20 15:04:47 -07006060
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006061 final long identity = Binder.clearCallingIdentity();
6062 try {
Michele Berionne482f8202018-11-27 18:57:59 -08006063 return (int) sendRequest(CMD_SET_ALLOWED_CARRIERS, carrierRestrictionRules,
vagdeviaf9a5b92018-08-15 16:01:53 -07006064 workSource);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006065 } finally {
6066 Binder.restoreCallingIdentity(identity);
6067 }
Meng Wang1a7c35a2016-05-05 20:56:15 -07006068 }
6069
6070 /**
6071 * {@hide}
Michele Berionne482f8202018-11-27 18:57:59 -08006072 * Get the allowed carrier list and the excluded carrier list, including the priority between
6073 * the two lists.
Meng Wang1a7c35a2016-05-05 20:56:15 -07006074 * Require system privileges. In the future we may add this to carrier APIs.
6075 *
Michele Berionne482f8202018-11-27 18:57:59 -08006076 * @return {@link android.telephony.CarrierRestrictionRules}
Meng Wang1a7c35a2016-05-05 20:56:15 -07006077 */
6078 @Override
Michele Berionne482f8202018-11-27 18:57:59 -08006079 public CarrierRestrictionRules getAllowedCarriers() {
Brad Ebinger35c841c2018-10-01 10:40:55 -07006080 enforceReadPrivilegedPermission("getAllowedCarriers");
vagdeviaf9a5b92018-08-15 16:01:53 -07006081 WorkSource workSource = getWorkSource(Binder.getCallingUid());
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006082
6083 final long identity = Binder.clearCallingIdentity();
6084 try {
Michele Berionne482f8202018-11-27 18:57:59 -08006085 Object response = sendRequest(CMD_GET_ALLOWED_CARRIERS, null, workSource);
6086 if (response instanceof CarrierRestrictionRules) {
6087 return (CarrierRestrictionRules) response;
6088 }
6089 // Response is an Exception of some kind,
6090 // which is signalled to the user as a NULL retval
6091 return null;
6092 } catch (Exception e) {
6093 Log.e(LOG_TAG, "getAllowedCarriers. Exception ex=" + e);
6094 return null;
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006095 } finally {
6096 Binder.restoreCallingIdentity(identity);
6097 }
Meng Wang1a7c35a2016-05-05 20:56:15 -07006098 }
6099
fionaxu59545b42016-05-25 15:53:37 -07006100 /**
6101 * Action set from carrier signalling broadcast receivers to enable/disable metered apns
6102 * @param subId the subscription ID that this action applies to.
6103 * @param enabled control enable or disable metered apns.
6104 * {@hide}
6105 */
6106 @Override
6107 public void carrierActionSetMeteredApnsEnabled(int subId, boolean enabled) {
6108 enforceModifyPermission();
6109 final Phone phone = getPhone(subId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006110
6111 final long identity = Binder.clearCallingIdentity();
fionaxu59545b42016-05-25 15:53:37 -07006112 if (phone == null) {
6113 loge("carrierAction: SetMeteredApnsEnabled fails with invalid subId: " + subId);
6114 return;
6115 }
6116 try {
6117 phone.carrierActionSetMeteredApnsEnabled(enabled);
6118 } catch (Exception e) {
6119 Log.e(LOG_TAG, "carrierAction: SetMeteredApnsEnabled fails. Exception ex=" + e);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006120 } finally {
6121 Binder.restoreCallingIdentity(identity);
fionaxu59545b42016-05-25 15:53:37 -07006122 }
6123 }
6124
6125 /**
6126 * Action set from carrier signalling broadcast receivers to enable/disable radio
6127 * @param subId the subscription ID that this action applies to.
6128 * @param enabled control enable or disable radio.
6129 * {@hide}
6130 */
6131 @Override
6132 public void carrierActionSetRadioEnabled(int subId, boolean enabled) {
6133 enforceModifyPermission();
6134 final Phone phone = getPhone(subId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006135
6136 final long identity = Binder.clearCallingIdentity();
fionaxu59545b42016-05-25 15:53:37 -07006137 if (phone == null) {
6138 loge("carrierAction: SetRadioEnabled fails with invalid sibId: " + subId);
6139 return;
6140 }
6141 try {
6142 phone.carrierActionSetRadioEnabled(enabled);
6143 } catch (Exception e) {
6144 Log.e(LOG_TAG, "carrierAction: SetRadioEnabled fails. Exception ex=" + e);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006145 } finally {
6146 Binder.restoreCallingIdentity(identity);
fionaxu59545b42016-05-25 15:53:37 -07006147 }
6148 }
6149
Ta-wei Yenc236d6b2016-06-21 13:33:12 -07006150 /**
fionaxu8da9cb12017-05-23 15:02:46 -07006151 * Action set from carrier signalling broadcast receivers to start/stop reporting the default
6152 * network status based on which carrier apps could apply actions accordingly,
6153 * enable/disable default url handler for example.
6154 *
6155 * @param subId the subscription ID that this action applies to.
6156 * @param report control start/stop reporting the default network status.
6157 * {@hide}
6158 */
6159 @Override
6160 public void carrierActionReportDefaultNetworkStatus(int subId, boolean report) {
6161 enforceModifyPermission();
6162 final Phone phone = getPhone(subId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006163
6164 final long identity = Binder.clearCallingIdentity();
fionaxu8da9cb12017-05-23 15:02:46 -07006165 if (phone == null) {
6166 loge("carrierAction: ReportDefaultNetworkStatus fails with invalid sibId: " + subId);
6167 return;
6168 }
6169 try {
6170 phone.carrierActionReportDefaultNetworkStatus(report);
6171 } catch (Exception e) {
6172 Log.e(LOG_TAG, "carrierAction: ReportDefaultNetworkStatus fails. Exception ex=" + e);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006173 } finally {
6174 Binder.restoreCallingIdentity(identity);
fionaxu8da9cb12017-05-23 15:02:46 -07006175 }
6176 }
6177
6178 /**
fionaxud9622282017-07-17 17:51:30 -07006179 * Action set from carrier signalling broadcast receivers to reset all carrier actions
6180 * @param subId the subscription ID that this action applies to.
6181 * {@hide}
6182 */
6183 @Override
6184 public void carrierActionResetAll(int subId) {
6185 enforceModifyPermission();
6186 final Phone phone = getPhone(subId);
6187 if (phone == null) {
6188 loge("carrierAction: ResetAll fails with invalid sibId: " + subId);
6189 return;
6190 }
6191 try {
6192 phone.carrierActionResetAll();
6193 } catch (Exception e) {
6194 Log.e(LOG_TAG, "carrierAction: ResetAll fails. Exception ex=" + e);
6195 }
6196 }
6197
6198 /**
Ta-wei Yenc236d6b2016-06-21 13:33:12 -07006199 * Called when "adb shell dumpsys phone" is invoked. Dump is also automatically invoked when a
6200 * bug report is being generated.
6201 */
6202 @Override
Ta-wei Yen99282e02016-06-21 18:19:35 -07006203 protected void dump(FileDescriptor fd, PrintWriter writer, String[] args) {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08006204 if (mApp.checkCallingOrSelfPermission(android.Manifest.permission.DUMP)
6205 != PackageManager.PERMISSION_GRANTED) {
dcashman22b950d2016-06-27 11:39:02 -07006206 writer.println("Permission Denial: can't dump Phone from pid="
6207 + Binder.getCallingPid()
6208 + ", uid=" + Binder.getCallingUid()
6209 + "without permission "
6210 + android.Manifest.permission.DUMP);
6211 return;
6212 }
Nazanin Bakhshif782e562018-12-11 15:15:39 -08006213 DumpsysHandler.dump(mApp, fd, writer, args);
Ta-wei Yenc236d6b2016-06-21 13:33:12 -07006214 }
Jack Yueb89b242016-06-22 13:27:47 -07006215
Brad Ebingerdac2f002018-04-03 15:17:52 -07006216 @Override
6217 public void onShellCommand(FileDescriptor in, FileDescriptor out, FileDescriptor err,
6218 String[] args, ShellCallback callback, ResultReceiver resultReceiver)
6219 throws RemoteException {
6220 (new TelephonyShellCommand(this)).exec(this, in, out, err, args, callback, resultReceiver);
6221 }
6222
Jack Yueb89b242016-06-22 13:27:47 -07006223 /**
Jack Yu84291ec2017-05-26 16:07:50 -07006224 * Get aggregated video call data usage since boot.
6225 *
6226 * @param perUidStats True if requesting data usage per uid, otherwise overall usage.
6227 * @return Snapshot of video call data usage
Jack Yueb89b242016-06-22 13:27:47 -07006228 * {@hide}
6229 */
6230 @Override
Jack Yu84291ec2017-05-26 16:07:50 -07006231 public NetworkStats getVtDataUsage(int subId, boolean perUidStats) {
Jack Yueb89b242016-06-22 13:27:47 -07006232 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.READ_NETWORK_USAGE_HISTORY,
6233 null);
6234
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006235 final long identity = Binder.clearCallingIdentity();
6236 try {
6237 // NetworkStatsService keeps tracking the active network interface and identity. It
6238 // records the delta with the corresponding network identity.
6239 // We just return the total video call data usage snapshot since boot.
6240 Phone phone = getPhone(subId);
6241 if (phone != null) {
6242 return phone.getVtDataUsage(perUidStats);
6243 }
6244 return null;
6245 } finally {
6246 Binder.restoreCallingIdentity(identity);
Jack Yueb89b242016-06-22 13:27:47 -07006247 }
Jack Yueb89b242016-06-22 13:27:47 -07006248 }
Jack Yu75ab2952016-07-08 14:29:33 -07006249
6250 /**
6251 * Policy control of data connection. Usually used when data limit is passed.
6252 * @param enabled True if enabling the data, otherwise disabling.
6253 * @param subId Subscription index
6254 * {@hide}
6255 */
6256 @Override
6257 public void setPolicyDataEnabled(boolean enabled, int subId) {
6258 enforceModifyPermission();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006259
6260 final long identity = Binder.clearCallingIdentity();
6261 try {
6262 Phone phone = getPhone(subId);
6263 if (phone != null) {
Jack Yud79fba22018-12-13 11:51:28 -08006264 phone.getDataEnabledSettings().setPolicyDataEnabled(enabled);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006265 }
6266 } finally {
6267 Binder.restoreCallingIdentity(identity);
Jack Yu75ab2952016-07-08 14:29:33 -07006268 }
6269 }
Sooraj Sasindran9a909312016-07-18 11:57:25 -07006270
6271 /**
6272 * Get Client request stats
6273 * @return List of Client Request Stats
6274 * @hide
6275 */
6276 @Override
6277 public List<ClientRequestStats> getClientRequestStats(String callingPackage, int subId) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08006278 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
Jeff Davidson913390f2018-02-23 17:11:49 -08006279 mApp, subId, callingPackage, "getClientRequestStats")) {
Sooraj Sasindran9a909312016-07-18 11:57:25 -07006280 return null;
6281 }
Sooraj Sasindran9a909312016-07-18 11:57:25 -07006282 Phone phone = getPhone(subId);
Sooraj Sasindran9a909312016-07-18 11:57:25 -07006283
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006284 final long identity = Binder.clearCallingIdentity();
6285 try {
6286 if (phone != null) {
6287 return phone.getClientRequestStats();
6288 }
6289
6290 return null;
6291 } finally {
6292 Binder.restoreCallingIdentity(identity);
6293 }
Sooraj Sasindran9a909312016-07-18 11:57:25 -07006294 }
6295
Narayan Kamathf04b5a12018-01-09 11:47:15 +00006296 private WorkSource getWorkSource(int uid) {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08006297 String packageName = mApp.getPackageManager().getNameForUid(uid);
Narayan Kamathf04b5a12018-01-09 11:47:15 +00006298 return new WorkSource(uid, packageName);
Sooraj Sasindran9a909312016-07-18 11:57:25 -07006299 }
Jack Yueb4124c2017-02-16 15:32:43 -08006300
6301 /**
Grace Chen70990072017-03-24 17:21:30 -07006302 * Set SIM card power state.
Jack Yueb4124c2017-02-16 15:32:43 -08006303 *
Sanket Padawe13bac7b2017-03-20 15:04:47 -07006304 * @param slotIndex SIM slot id.
Grace Chen70990072017-03-24 17:21:30 -07006305 * @param state State of SIM (power down, power up, pass through)
6306 * - {@link android.telephony.TelephonyManager#CARD_POWER_DOWN}
6307 * - {@link android.telephony.TelephonyManager#CARD_POWER_UP}
6308 * - {@link android.telephony.TelephonyManager#CARD_POWER_UP_PASS_THROUGH}
Jack Yueb4124c2017-02-16 15:32:43 -08006309 *
6310 **/
6311 @Override
Grace Chen70990072017-03-24 17:21:30 -07006312 public void setSimPowerStateForSlot(int slotIndex, int state) {
Jack Yueb4124c2017-02-16 15:32:43 -08006313 enforceModifyPermission();
Sanket Padawe13bac7b2017-03-20 15:04:47 -07006314 Phone phone = PhoneFactory.getPhone(slotIndex);
6315
vagdeviaf9a5b92018-08-15 16:01:53 -07006316 WorkSource workSource = getWorkSource(Binder.getCallingUid());
6317
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006318 final long identity = Binder.clearCallingIdentity();
6319 try {
6320 if (phone != null) {
vagdeviaf9a5b92018-08-15 16:01:53 -07006321 phone.setSimPowerState(state, workSource);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006322 }
6323 } finally {
6324 Binder.restoreCallingIdentity(identity);
Jack Yueb4124c2017-02-16 15:32:43 -08006325 }
6326 }
Shuo Qiandd210312017-04-12 22:11:33 +00006327
Tyler Gunn65d45c22017-06-05 11:22:26 -07006328 private boolean isUssdApiAllowed(int subId) {
6329 CarrierConfigManager configManager =
Nazanin Bakhshif782e562018-12-11 15:15:39 -08006330 (CarrierConfigManager) mApp.getSystemService(Context.CARRIER_CONFIG_SERVICE);
Tyler Gunn65d45c22017-06-05 11:22:26 -07006331 if (configManager == null) {
6332 return false;
6333 }
6334 PersistableBundle pb = configManager.getConfigForSubId(subId);
6335 if (pb == null) {
6336 return false;
6337 }
6338 return pb.getBoolean(
6339 CarrierConfigManager.KEY_ALLOW_USSD_REQUESTS_VIA_TELEPHONY_MANAGER_BOOL);
6340 }
6341
Shuo Qiandd210312017-04-12 22:11:33 +00006342 /**
6343 * Check if phone is in emergency callback mode
6344 * @return true if phone is in emergency callback mode
6345 * @param subId sub id
6346 */
goneil9c5f4872017-12-05 14:07:56 -08006347 @Override
Shuo Qiandd210312017-04-12 22:11:33 +00006348 public boolean getEmergencyCallbackMode(int subId) {
Brad Ebinger35c841c2018-10-01 10:40:55 -07006349 enforceReadPrivilegedPermission("getEmergencyCallbackMode");
Shuo Qiandd210312017-04-12 22:11:33 +00006350 final Phone phone = getPhone(subId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006351
6352 final long identity = Binder.clearCallingIdentity();
6353 try {
6354 if (phone != null) {
6355 return phone.isInEcm();
6356 } else {
6357 return false;
6358 }
6359 } finally {
6360 Binder.restoreCallingIdentity(identity);
Shuo Qiandd210312017-04-12 22:11:33 +00006361 }
6362 }
Nathan Harold46b42aa2017-03-10 19:38:22 -08006363
6364 /**
6365 * Get the current signal strength information for the given subscription.
6366 * Because this information is not updated when the device is in a low power state
6367 * it should not be relied-upon to be current.
6368 * @param subId Subscription index
6369 * @return the most recent cached signal strength info from the modem
6370 */
6371 @Override
6372 public SignalStrength getSignalStrength(int subId) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006373 final long identity = Binder.clearCallingIdentity();
6374 try {
6375 Phone p = getPhone(subId);
6376 if (p == null) {
6377 return null;
6378 }
Nathan Harold46b42aa2017-03-10 19:38:22 -08006379
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006380 return p.getSignalStrength();
6381 } finally {
6382 Binder.restoreCallingIdentity(identity);
6383 }
Nathan Harold46b42aa2017-03-10 19:38:22 -08006384 }
Holly Jiuyu Sun01c47ad2018-01-24 17:56:33 +00006385
Pengquan Meng77b7f132018-08-22 14:49:57 -07006386 /**
Chen Xuf792fd62018-10-17 17:54:36 +00006387 * Get the current modem radio state for the given slot.
6388 * @param slotIndex slot index.
6389 * @param callingPackage the name of the package making the call.
6390 * @return the current radio power state from the modem
6391 */
6392 @Override
6393 public int getRadioPowerState(int slotIndex, String callingPackage) {
6394 Phone phone = PhoneFactory.getPhone(slotIndex);
6395 if (phone != null) {
6396 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
6397 mApp, phone.getSubId(), callingPackage, "getRadioPowerState")) {
6398 return TelephonyManager.RADIO_POWER_UNAVAILABLE;
6399 }
6400
6401 final long identity = Binder.clearCallingIdentity();
6402 try {
6403 return phone.getRadioPowerState();
6404 } finally {
6405 Binder.restoreCallingIdentity(identity);
6406 }
6407 }
6408 return TelephonyManager.RADIO_POWER_UNAVAILABLE;
6409 }
6410
6411 /**
Pengquan Meng77b7f132018-08-22 14:49:57 -07006412 * Checks if data roaming is enabled on the subscription with id {@code subId}.
6413 *
6414 * <p>Requires one of the following permissions:
6415 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE},
6416 * {@link android.Manifest.permission#READ_PHONE_STATE} or that the calling app has carrier
6417 * privileges.
6418 *
6419 * @param subId subscription id
6420 * @return {@code true} if data roaming is enabled on this subscription, otherwise return
6421 * {@code false}.
6422 */
6423 @Override
6424 public boolean isDataRoamingEnabled(int subId) {
Pengquan Meng44e66f12019-04-01 10:48:20 -07006425 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.ACCESS_NETWORK_STATE,
6426 null /* message */);
6427
Pengquan Menga1bb6272018-09-06 09:59:22 -07006428 boolean isEnabled = false;
6429 final long identity = Binder.clearCallingIdentity();
Pengquan Meng77b7f132018-08-22 14:49:57 -07006430 try {
Pengquan Menga1bb6272018-09-06 09:59:22 -07006431 Phone phone = getPhone(subId);
6432 isEnabled = phone != null ? phone.getDataRoamingEnabled() : false;
Pengquan Meng77b7f132018-08-22 14:49:57 -07006433 } catch (Exception e) {
6434 TelephonyPermissions.enforeceCallingOrSelfReadPhoneStatePermissionOrCarrierPrivilege(
6435 mApp, subId, "isDataRoamingEnabled");
Pengquan Menga1bb6272018-09-06 09:59:22 -07006436 } finally {
6437 Binder.restoreCallingIdentity(identity);
Pengquan Meng77b7f132018-08-22 14:49:57 -07006438 }
Pengquan Menga1bb6272018-09-06 09:59:22 -07006439 return isEnabled;
Pengquan Meng77b7f132018-08-22 14:49:57 -07006440 }
6441
6442
6443 /**
6444 * Enables/Disables the data roaming on the subscription with id {@code subId}.
6445 *
6446 * <p> Requires permission:
6447 * {@link android.Manifest.permission#MODIFY_PHONE_STATE} or that the calling app has carrier
6448 * privileges.
6449 *
6450 * @param subId subscription id
6451 * @param isEnabled {@code true} means enable, {@code false} means disable.
6452 */
6453 @Override
6454 public void setDataRoamingEnabled(int subId, boolean isEnabled) {
Pengquan Meng44e66f12019-04-01 10:48:20 -07006455 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
6456 mApp, subId, "setDataRoamingEnabled");
6457
Pengquan Menga1bb6272018-09-06 09:59:22 -07006458 final long identity = Binder.clearCallingIdentity();
6459 try {
Pengquan Menga1bb6272018-09-06 09:59:22 -07006460 Phone phone = getPhone(subId);
6461 if (phone != null) {
6462 phone.setDataRoamingEnabled(isEnabled);
6463 }
6464 } finally {
6465 Binder.restoreCallingIdentity(identity);
Pengquan Meng77b7f132018-08-22 14:49:57 -07006466 }
6467 }
6468
Holly Jiuyu Sun01c47ad2018-01-24 17:56:33 +00006469 @Override
Pengquan Meng6884a2c2018-10-03 12:19:13 -07006470 public boolean isManualNetworkSelectionAllowed(int subId) {
Pengquan Meng44e66f12019-04-01 10:48:20 -07006471 TelephonyPermissions.enforeceCallingOrSelfReadPhoneStatePermissionOrCarrierPrivilege(
6472 mApp, subId, "isManualNetworkSelectionAllowed");
6473
Pengquan Meng6884a2c2018-10-03 12:19:13 -07006474 boolean isAllowed = true;
6475 final long identity = Binder.clearCallingIdentity();
6476 try {
Pengquan Meng6884a2c2018-10-03 12:19:13 -07006477 Phone phone = getPhone(subId);
6478 if (phone != null) {
6479 isAllowed = phone.isCspPlmnEnabled();
6480 }
6481 } finally {
6482 Binder.restoreCallingIdentity(identity);
6483 }
6484 return isAllowed;
6485 }
6486
6487 @Override
Jordan Liu75f43ea2019-01-17 16:56:37 -08006488 public List<UiccCardInfo> getUiccCardsInfo(String callingPackage) {
Jordan Liu1e142fc2019-04-22 15:10:43 -07006489 boolean hasReadPermission = false;
Jordan Liuc65bc952019-02-12 17:54:02 -08006490 try {
6491 enforceReadPrivilegedPermission("getUiccCardsInfo");
Jordan Liu1e142fc2019-04-22 15:10:43 -07006492 hasReadPermission = true;
Jordan Liuc65bc952019-02-12 17:54:02 -08006493 } catch (SecurityException e) {
6494 // even without READ_PRIVILEGED_PHONE_STATE, we allow the call to continue if the caller
6495 // has carrier privileges on an active UICC
6496 if (checkCarrierPrivilegesForPackageAnyPhone(callingPackage)
6497 != TelephonyManager.CARRIER_PRIVILEGE_STATUS_HAS_ACCESS) {
Jordan Liu1e142fc2019-04-22 15:10:43 -07006498 throw new SecurityException("Caller does not have permission.");
Jordan Liuc65bc952019-02-12 17:54:02 -08006499 }
Jordan Liu75f43ea2019-01-17 16:56:37 -08006500 }
Jordan Liu5aa07002018-12-18 15:44:48 -08006501
6502 final long identity = Binder.clearCallingIdentity();
6503 try {
Jordan Liu75f43ea2019-01-17 16:56:37 -08006504 UiccController uiccController = UiccController.getInstance();
6505 ArrayList<UiccCardInfo> cardInfos = uiccController.getAllUiccCardInfos();
Jordan Liu1e142fc2019-04-22 15:10:43 -07006506 if (hasReadPermission) {
6507 return cardInfos;
Jordan Liu75f43ea2019-01-17 16:56:37 -08006508 }
Jordan Liu1e142fc2019-04-22 15:10:43 -07006509
6510 // Remove private info if the caller doesn't have access
6511 ArrayList<UiccCardInfo> filteredInfos = new ArrayList<>();
6512 for (UiccCardInfo cardInfo : cardInfos) {
6513 // For an inactive eUICC, the UiccCard will be null even though the UiccCardInfo
6514 // is available
6515 UiccCard card = uiccController.getUiccCardForSlot(cardInfo.getSlotIndex());
6516 if (card == null || card.getUiccProfile() == null) {
6517 // assume no access if the card or profile is unavailable
6518 filteredInfos.add(cardInfo.getUnprivileged());
6519 continue;
6520 }
6521 UiccProfile profile = card.getUiccProfile();
6522 if (profile.getCarrierPrivilegeStatus(mApp.getPackageManager(), callingPackage)
6523 == TelephonyManager.CARRIER_PRIVILEGE_STATUS_HAS_ACCESS) {
6524 filteredInfos.add(cardInfo);
6525 } else {
6526 filteredInfos.add(cardInfo.getUnprivileged());
6527 }
6528 }
6529 return filteredInfos;
Jordan Liu5aa07002018-12-18 15:44:48 -08006530 } finally {
6531 Binder.restoreCallingIdentity(identity);
6532 }
6533 }
6534
6535 @Override
Holly Jiuyu Sun01c47ad2018-01-24 17:56:33 +00006536 public UiccSlotInfo[] getUiccSlotsInfo() {
Brad Ebinger35c841c2018-10-01 10:40:55 -07006537 enforceReadPrivilegedPermission("getUiccSlotsInfo");
Holly Jiuyu Sun01c47ad2018-01-24 17:56:33 +00006538
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006539 final long identity = Binder.clearCallingIdentity();
6540 try {
6541 UiccSlot[] slots = UiccController.getInstance().getUiccSlots();
6542 if (slots == null) {
6543 Rlog.i(LOG_TAG, "slots is null.");
6544 return null;
6545 }
6546
6547 UiccSlotInfo[] infos = new UiccSlotInfo[slots.length];
6548 for (int i = 0; i < slots.length; i++) {
6549 UiccSlot slot = slots[i];
6550 if (slot == null) {
6551 continue;
6552 }
6553
Jordan Liu7be7e652019-05-06 18:55:02 +00006554 String cardId;
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006555 UiccCard card = slot.getUiccCard();
6556 if (card != null) {
6557 cardId = card.getCardId();
Jordan Liu7be7e652019-05-06 18:55:02 +00006558 } else {
Jordan Liu01bd00d2019-09-12 16:19:43 -07006559 cardId = slot.getEid();
6560 if (TextUtils.isEmpty(cardId)) {
6561 cardId = slot.getIccId();
6562 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006563 }
6564
6565 int cardState = 0;
6566 switch (slot.getCardState()) {
6567 case CARDSTATE_ABSENT:
6568 cardState = UiccSlotInfo.CARD_STATE_INFO_ABSENT;
6569 break;
6570 case CARDSTATE_PRESENT:
6571 cardState = UiccSlotInfo.CARD_STATE_INFO_PRESENT;
6572 break;
6573 case CARDSTATE_ERROR:
6574 cardState = UiccSlotInfo.CARD_STATE_INFO_ERROR;
6575 break;
6576 case CARDSTATE_RESTRICTED:
6577 cardState = UiccSlotInfo.CARD_STATE_INFO_RESTRICTED;
6578 break;
6579 default:
6580 break;
6581
6582 }
6583
6584 infos[i] = new UiccSlotInfo(
6585 slot.isActive(),
6586 slot.isEuicc(),
6587 cardId,
6588 cardState,
6589 slot.getPhoneId(),
Jordan Liua2619582019-02-14 12:56:40 -08006590 slot.isExtendedApduSupported(),
6591 slot.isRemovable());
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006592 }
6593 return infos;
6594 } finally {
6595 Binder.restoreCallingIdentity(identity);
Holly Jiuyu Sun1d957c52018-04-04 13:52:42 -07006596 }
Holly Jiuyu Sun01c47ad2018-01-24 17:56:33 +00006597 }
6598
6599 @Override
6600 public boolean switchSlots(int[] physicalSlots) {
6601 enforceModifyPermission();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006602
6603 final long identity = Binder.clearCallingIdentity();
6604 try {
6605 return (Boolean) sendRequest(CMD_SWITCH_SLOTS, physicalSlots);
6606 } finally {
6607 Binder.restoreCallingIdentity(identity);
6608 }
Holly Jiuyu Sun01c47ad2018-01-24 17:56:33 +00006609 }
Jack Yu4c988042018-02-27 15:30:01 -08006610
6611 @Override
Jordan Liu7de49fa2018-12-06 14:48:49 -08006612 public int getCardIdForDefaultEuicc(int subId, String callingPackage) {
Jordan Liu7de49fa2018-12-06 14:48:49 -08006613 final long identity = Binder.clearCallingIdentity();
6614 try {
6615 return UiccController.getInstance().getCardIdForDefaultEuicc();
6616 } finally {
6617 Binder.restoreCallingIdentity(identity);
6618 }
6619 }
6620
6621 @Override
Jack Yu4c988042018-02-27 15:30:01 -08006622 public void setRadioIndicationUpdateMode(int subId, int filters, int mode) {
6623 enforceModifyPermission();
6624 final Phone phone = getPhone(subId);
6625 if (phone == null) {
6626 loge("setRadioIndicationUpdateMode fails with invalid subId: " + subId);
6627 return;
6628 }
6629
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006630 final long identity = Binder.clearCallingIdentity();
6631 try {
6632 phone.setRadioIndicationUpdateMode(filters, mode);
6633 } finally {
6634 Binder.restoreCallingIdentity(identity);
6635 }
Jack Yu4c988042018-02-27 15:30:01 -08006636 }
Pengquan Meng85728fb2018-03-12 16:31:21 -07006637
6638 /**
goneil47ffb6e2018-04-06 15:40:58 -07006639 * A test API to reload the UICC profile.
6640 *
6641 * <p>Requires that the calling app has permission
6642 * {@link android.Manifest.permission#MODIFY_PHONE_STATE MODIFY_PHONE_STATE}.
6643 * @hide
6644 */
6645 @Override
6646 public void refreshUiccProfile(int subId) {
6647 enforceModifyPermission();
6648
6649 final long identity = Binder.clearCallingIdentity();
6650 try {
6651 Phone phone = getPhone(subId);
6652 if (phone == null) {
6653 return;
6654 }
6655 UiccCard uiccCard = phone.getUiccCard();
6656 if (uiccCard == null) {
6657 return;
6658 }
6659 UiccProfile uiccProfile = uiccCard.getUiccProfile();
6660 if (uiccProfile == null) {
6661 return;
6662 }
6663 uiccProfile.refresh();
6664 } finally {
6665 Binder.restoreCallingIdentity(identity);
6666 }
6667 }
6668
6669 /**
Pengquan Meng85728fb2018-03-12 16:31:21 -07006670 * Returns false if the mobile data is disabled by default, otherwise return true.
6671 */
6672 private boolean getDefaultDataEnabled() {
6673 return "true".equalsIgnoreCase(
6674 SystemProperties.get(DEFAULT_MOBILE_DATA_PROPERTY_NAME, "true"));
6675 }
6676
6677 /**
6678 * Returns true if the data roaming is enabled by default, i.e the system property
6679 * of {@link #DEFAULT_DATA_ROAMING_PROPERTY_NAME} is true or the config of
6680 * {@link CarrierConfigManager#KEY_CARRIER_DEFAULT_DATA_ROAMING_ENABLED_BOOL} is true.
6681 */
6682 private boolean getDefaultDataRoamingEnabled(int subId) {
6683 final CarrierConfigManager configMgr = (CarrierConfigManager)
Nazanin Bakhshif782e562018-12-11 15:15:39 -08006684 mApp.getSystemService(Context.CARRIER_CONFIG_SERVICE);
Pengquan Meng85728fb2018-03-12 16:31:21 -07006685 boolean isDataRoamingEnabled = "true".equalsIgnoreCase(
6686 SystemProperties.get(DEFAULT_DATA_ROAMING_PROPERTY_NAME, "false"));
6687 isDataRoamingEnabled |= configMgr.getConfigForSubId(subId).getBoolean(
6688 CarrierConfigManager.KEY_CARRIER_DEFAULT_DATA_ROAMING_ENABLED_BOOL);
6689 return isDataRoamingEnabled;
6690 }
6691
6692 /**
6693 * Returns the default network type for the given {@code subId}, if the default network type is
6694 * not set, return {@link Phone#PREFERRED_NT_MODE}.
6695 */
6696 private int getDefaultNetworkType(int subId) {
6697 return Integer.parseInt(
6698 TelephonyManager.getTelephonyProperty(
6699 mSubscriptionController.getPhoneId(subId),
6700 DEFAULT_NETWORK_MODE_PROPERTY_NAME,
6701 String.valueOf(Phone.PREFERRED_NT_MODE)));
6702 }
fionaxua13278b2018-03-21 00:08:13 -07006703
6704 @Override
6705 public void setCarrierTestOverride(int subId, String mccmnc, String imsi, String iccid, String
chen xueaba88a2019-03-15 13:15:10 -07006706 gid1, String gid2, String plmn, String spn, String carrierPrivilegeRules, String apn) {
fionaxua13278b2018-03-21 00:08:13 -07006707 enforceModifyPermission();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006708
6709 final long identity = Binder.clearCallingIdentity();
6710 try {
6711 final Phone phone = getPhone(subId);
6712 if (phone == null) {
6713 loge("setCarrierTestOverride fails with invalid subId: " + subId);
6714 return;
6715 }
chen xueaba88a2019-03-15 13:15:10 -07006716 phone.setCarrierTestOverride(mccmnc, imsi, iccid, gid1, gid2, plmn, spn,
6717 carrierPrivilegeRules, apn);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006718 } finally {
6719 Binder.restoreCallingIdentity(identity);
fionaxua13278b2018-03-21 00:08:13 -07006720 }
fionaxua13278b2018-03-21 00:08:13 -07006721 }
6722
6723 @Override
6724 public int getCarrierIdListVersion(int subId) {
Brad Ebinger35c841c2018-10-01 10:40:55 -07006725 enforceReadPrivilegedPermission("getCarrierIdListVersion");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006726
6727 final long identity = Binder.clearCallingIdentity();
6728 try {
6729 final Phone phone = getPhone(subId);
6730 if (phone == null) {
6731 loge("getCarrierIdListVersion fails with invalid subId: " + subId);
6732 return TelephonyManager.UNKNOWN_CARRIER_ID_LIST_VERSION;
6733 }
6734 return phone.getCarrierIdListVersion();
6735 } finally {
6736 Binder.restoreCallingIdentity(identity);
fionaxua13278b2018-03-21 00:08:13 -07006737 }
fionaxua13278b2018-03-21 00:08:13 -07006738 }
Malcolm Chen2c63d402018-08-14 16:00:53 -07006739
6740 @Override
6741 public int getNumberOfModemsWithSimultaneousDataConnections(int subId, String callingPackage) {
6742 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
6743 mApp, subId, callingPackage, "getNumberOfModemsWithSimultaneousDataConnections")) {
6744 return -1;
6745 }
6746
6747 final long identity = Binder.clearCallingIdentity();
6748 try {
6749 return mPhoneConfigurationManager.getNumberOfModemsWithSimultaneousDataConnections();
6750 } finally {
6751 Binder.restoreCallingIdentity(identity);
6752 }
6753 }
Pengquan Menga1bb6272018-09-06 09:59:22 -07006754
6755 @Override
6756 public int getCdmaRoamingMode(int subId) {
6757 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
6758 mApp, subId, "getCdmaRoamingMode");
6759
6760 final long identity = Binder.clearCallingIdentity();
6761 try {
6762 return (int) sendRequest(CMD_GET_CDMA_ROAMING_MODE, null /* argument */, subId);
6763 } finally {
6764 Binder.restoreCallingIdentity(identity);
6765 }
6766 }
6767
6768 @Override
6769 public boolean setCdmaRoamingMode(int subId, int mode) {
6770 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
6771 mApp, subId, "setCdmaRoamingMode");
6772
6773 final long identity = Binder.clearCallingIdentity();
6774 try {
6775 return (boolean) sendRequest(CMD_SET_CDMA_ROAMING_MODE, mode, subId);
6776 } finally {
6777 Binder.restoreCallingIdentity(identity);
6778 }
6779 }
6780
6781 @Override
6782 public boolean setCdmaSubscriptionMode(int subId, int mode) {
6783 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
6784 mApp, subId, "setCdmaSubscriptionMode");
6785
6786 final long identity = Binder.clearCallingIdentity();
6787 try {
6788 return (boolean) sendRequest(CMD_SET_CDMA_SUBSCRIPTION_MODE, mode, subId);
6789 } finally {
6790 Binder.restoreCallingIdentity(identity);
6791 }
6792 }
Makoto Onukida3bf792018-09-18 16:06:29 -07006793
6794 private void ensureUserRunning(int userId) {
6795 if (!mUserManager.isUserRunning(userId)) {
6796 throw new IllegalStateException("User " + userId + " does not exist or not running");
6797 }
6798 }
6799
6800 /**
6801 * Returns a list of SMS apps on a given user.
6802 *
6803 * Only the shell user (UID 2000 or 0) can call it.
6804 * Target user must be running.
6805 */
6806 @Override
6807 public String[] getSmsApps(int userId) {
6808 TelephonyPermissions.enforceShellOnly(Binder.getCallingUid(), "getSmsApps");
6809 ensureUserRunning(userId);
6810
6811 final Collection<SmsApplicationData> apps =
6812 SmsApplication.getApplicationCollectionAsUser(mApp, userId);
6813
6814 String[] ret = new String[apps.size()];
6815 int i = 0;
6816 for (SmsApplicationData app : apps) {
6817 ret[i++] = app.mPackageName;
6818 }
6819 return ret;
6820 }
6821
6822 /**
6823 * Returns the default SMS app package name on a given user.
6824 *
6825 * Only the shell user (UID 2000 or 0) can call it.
6826 * Target user must be running.
6827 */
6828 @Override
6829 public String getDefaultSmsApp(int userId) {
6830 TelephonyPermissions.enforceShellOnly(Binder.getCallingUid(), "getDefaultSmsApp");
6831 ensureUserRunning(userId);
6832
6833 final ComponentName cn = SmsApplication.getDefaultSmsApplicationAsUser(mApp,
6834 /* updateIfNeeded= */ true, userId);
6835 return cn == null ? null : cn.getPackageName();
6836 }
6837
6838 /**
6839 * Set a package as the default SMS app on a given user.
6840 *
6841 * Only the shell user (UID 2000 or 0) can call it.
6842 * Target user must be running.
6843 */
6844 @Override
6845 public void setDefaultSmsApp(int userId, String packageName) {
6846 TelephonyPermissions.enforceShellOnly(Binder.getCallingUid(), "setDefaultSmsApp");
6847 ensureUserRunning(userId);
6848
6849 boolean found = false;
6850 for (String pkg : getSmsApps(userId)) {
6851 if (TextUtils.equals(packageName, pkg)) {
6852 found = true;
6853 break;
6854 }
6855 }
6856 if (!found) {
6857 throw new IllegalArgumentException("Package " + packageName + " is not an SMS app");
6858 }
6859
6860 SmsApplication.setDefaultApplicationAsUser(packageName, mApp, userId);
6861 }
sqianc5eccab2018-10-19 18:46:41 -07006862
6863 @Override
sqian8c685422019-02-22 15:55:18 -08006864 public Map<Integer, List<EmergencyNumber>> getEmergencyNumberList(
sqianc5eccab2018-10-19 18:46:41 -07006865 String callingPackage) {
sqian11b7a0e2018-12-05 18:48:28 -08006866 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
sqian8c685422019-02-22 15:55:18 -08006867 mApp, getDefaultSubscription(), callingPackage, "getEmergencyNumberList")) {
sqian11b7a0e2018-12-05 18:48:28 -08006868 throw new SecurityException("Requires READ_PHONE_STATE permission.");
6869 }
6870 final long identity = Binder.clearCallingIdentity();
6871 try {
sqian854d44b2018-12-12 16:48:18 -08006872 Map<Integer, List<EmergencyNumber>> emergencyNumberListInternal = new HashMap<>();
6873 for (Phone phone: PhoneFactory.getPhones()) {
6874 if (phone.getEmergencyNumberTracker() != null
6875 && phone.getEmergencyNumberTracker().getEmergencyNumberList() != null) {
6876 emergencyNumberListInternal.put(
6877 phone.getSubId(),
6878 phone.getEmergencyNumberTracker().getEmergencyNumberList());
6879 }
sqian11b7a0e2018-12-05 18:48:28 -08006880 }
sqian854d44b2018-12-12 16:48:18 -08006881 return emergencyNumberListInternal;
sqian11b7a0e2018-12-05 18:48:28 -08006882 } finally {
6883 Binder.restoreCallingIdentity(identity);
6884 }
sqianc5eccab2018-10-19 18:46:41 -07006885 }
6886
6887 @Override
sqian8c685422019-02-22 15:55:18 -08006888 public boolean isEmergencyNumber(String number, boolean exactMatch) {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08006889 final Phone defaultPhone = getDefaultPhone();
sqian11b7a0e2018-12-05 18:48:28 -08006890 if (!exactMatch) {
6891 TelephonyPermissions
6892 .enforeceCallingOrSelfReadPrivilegedPhoneStatePermissionOrCarrierPrivilege(
sqian8c685422019-02-22 15:55:18 -08006893 mApp, defaultPhone.getSubId(), "isEmergencyNumber(Potential)");
sqian11b7a0e2018-12-05 18:48:28 -08006894 }
6895 final long identity = Binder.clearCallingIdentity();
6896 try {
sqian854d44b2018-12-12 16:48:18 -08006897 for (Phone phone: PhoneFactory.getPhones()) {
6898 if (phone.getEmergencyNumberTracker() != null
6899 && phone.getEmergencyNumberTracker() != null) {
6900 if (phone.getEmergencyNumberTracker().isEmergencyNumber(
6901 number, exactMatch)) {
6902 return true;
sqian11b7a0e2018-12-05 18:48:28 -08006903 }
6904 }
sqian11b7a0e2018-12-05 18:48:28 -08006905 }
6906 return false;
6907 } finally {
6908 Binder.restoreCallingIdentity(identity);
6909 }
6910 }
6911
sqianf4ca7ed2019-01-15 18:32:07 -08006912 /**
6913 * Update emergency number list for test mode.
6914 */
6915 @Override
6916 public void updateEmergencyNumberListTestMode(int action, EmergencyNumber num) {
6917 TelephonyPermissions.enforceShellOnly(Binder.getCallingUid(),
6918 "updateEmergencyNumberListTestMode");
6919
6920 final long identity = Binder.clearCallingIdentity();
6921 try {
6922 for (Phone phone: PhoneFactory.getPhones()) {
6923 EmergencyNumberTracker tracker = phone.getEmergencyNumberTracker();
6924 if (tracker != null) {
6925 tracker.executeEmergencyNumberTestModeCommand(action, num);
6926 }
6927 }
6928 } finally {
6929 Binder.restoreCallingIdentity(identity);
6930 }
6931 }
6932
6933 /**
6934 * Get the full emergency number list for test mode.
6935 */
6936 @Override
6937 public List<String> getEmergencyNumberListTestMode() {
6938 TelephonyPermissions.enforceShellOnly(Binder.getCallingUid(),
6939 "getEmergencyNumberListTestMode");
6940
6941 final long identity = Binder.clearCallingIdentity();
6942 try {
6943 Set<String> emergencyNumbers = new HashSet<>();
6944 for (Phone phone: PhoneFactory.getPhones()) {
6945 EmergencyNumberTracker tracker = phone.getEmergencyNumberTracker();
6946 if (tracker != null) {
6947 for (EmergencyNumber num : tracker.getEmergencyNumberList()) {
6948 emergencyNumbers.add(num.getNumber());
6949 }
6950 }
6951 }
6952 return new ArrayList<>(emergencyNumbers);
6953 } finally {
6954 Binder.restoreCallingIdentity(identity);
6955 }
6956 }
6957
chen xud6b45bd2018-10-30 22:27:10 -07006958 @Override
6959 public List<String> getCertsFromCarrierPrivilegeAccessRules(int subId) {
6960 enforceReadPrivilegedPermission("getCertsFromCarrierPrivilegeAccessRules");
6961 Phone phone = getPhone(subId);
6962 if (phone == null) {
6963 return null;
6964 }
6965 final long identity = Binder.clearCallingIdentity();
6966 try {
6967 UiccProfile profile = UiccController.getInstance()
6968 .getUiccProfileForPhone(phone.getPhoneId());
6969 if (profile != null) {
6970 return profile.getCertsFromCarrierPrivilegeAccessRules();
6971 }
6972 } finally {
6973 Binder.restoreCallingIdentity(identity);
6974 }
6975 return null;
6976 }
Malcolm Chen8e4ed912019-01-15 20:22:16 -08006977
6978 /**
6979 * Enable or disable a modem stack.
6980 */
6981 @Override
6982 public boolean enableModemForSlot(int slotIndex, boolean enable) {
6983 enforceModifyPermission();
6984
6985 final long identity = Binder.clearCallingIdentity();
6986 try {
6987 Phone phone = PhoneFactory.getPhone(slotIndex);
6988 if (phone == null) {
6989 return false;
6990 } else {
6991 return (Boolean) sendRequest(CMD_REQUEST_ENABLE_MODEM, enable, phone, null);
6992 }
6993 } finally {
6994 Binder.restoreCallingIdentity(identity);
6995 }
6996 }
Michelecea4cf22018-12-21 15:00:11 -08006997
Malcolm Chen4bcd9822019-03-27 18:34:05 -07006998 /**
6999 * Whether a modem stack is enabled or not.
7000 */
7001 @Override
7002 public boolean isModemEnabledForSlot(int slotIndex, String callingPackage) {
7003 Phone phone = PhoneFactory.getPhone(slotIndex);
7004 if (phone == null) return false;
7005
7006 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
7007 mApp, phone.getSubId(), callingPackage, "isModemEnabledForSlot")) {
7008 throw new SecurityException("Requires READ_PHONE_STATE permission.");
7009 }
7010
7011 final long identity = Binder.clearCallingIdentity();
7012 try {
Nazanin Bakhshif71371d2019-04-29 17:29:44 -07007013 try {
7014 return mPhoneConfigurationManager.getPhoneStatusFromCache(phone.getPhoneId());
7015 } catch (NoSuchElementException ex) {
7016 return (Boolean) sendRequest(CMD_GET_MODEM_STATUS, null, phone, null);
7017 }
Malcolm Chen4bcd9822019-03-27 18:34:05 -07007018 } finally {
7019 Binder.restoreCallingIdentity(identity);
7020 }
7021 }
7022
Michelecea4cf22018-12-21 15:00:11 -08007023 @Override
Michele0ea7d782019-03-19 14:58:42 -07007024 public void setMultiSimCarrierRestriction(boolean isMultiSimCarrierRestricted) {
Michelecea4cf22018-12-21 15:00:11 -08007025 enforceModifyPermission();
7026
7027 final long identity = Binder.clearCallingIdentity();
7028 try {
7029 mTelephonySharedPreferences.edit()
Michele0ea7d782019-03-19 14:58:42 -07007030 .putBoolean(PREF_MULTI_SIM_RESTRICTED, isMultiSimCarrierRestricted)
Michelecea4cf22018-12-21 15:00:11 -08007031 .commit();
7032 } finally {
7033 Binder.restoreCallingIdentity(identity);
7034 }
7035 }
7036
7037 @Override
Michele0ea7d782019-03-19 14:58:42 -07007038 @TelephonyManager.IsMultiSimSupportedResult
7039 public int isMultiSimSupported(String callingPackage) {
Michele4245e952019-02-04 11:36:23 -08007040 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(mApp,
Michele0ea7d782019-03-19 14:58:42 -07007041 getDefaultPhone().getSubId(), callingPackage, "isMultiSimSupported")) {
7042 return TelephonyManager.MULTISIM_NOT_SUPPORTED_BY_HARDWARE;
Michele4245e952019-02-04 11:36:23 -08007043 }
Michelecea4cf22018-12-21 15:00:11 -08007044
7045 final long identity = Binder.clearCallingIdentity();
7046 try {
Michele0ea7d782019-03-19 14:58:42 -07007047 return isMultiSimSupportedInternal();
Michelecea4cf22018-12-21 15:00:11 -08007048 } finally {
7049 Binder.restoreCallingIdentity(identity);
7050 }
7051 }
Nazanin Bakhshi628473f2019-01-29 17:37:52 -08007052
Michele0ea7d782019-03-19 14:58:42 -07007053 @TelephonyManager.IsMultiSimSupportedResult
7054 private int isMultiSimSupportedInternal() {
Michele30b57b22019-03-01 12:01:14 -08007055 // If the device has less than 2 SIM cards, indicate that multisim is restricted.
7056 int numPhysicalSlots = UiccController.getInstance().getUiccSlots().length;
7057 if (numPhysicalSlots < 2) {
Michele0ea7d782019-03-19 14:58:42 -07007058 loge("isMultiSimSupportedInternal: requires at least 2 cards");
7059 return TelephonyManager.MULTISIM_NOT_SUPPORTED_BY_HARDWARE;
Michele30b57b22019-03-01 12:01:14 -08007060 }
7061 // Check if the hardware supports multisim functionality. If usage of multisim is not
7062 // supported by the modem, indicate that it is restricted.
7063 PhoneCapability staticCapability =
7064 mPhoneConfigurationManager.getStaticPhoneCapability();
7065 if (staticCapability == null) {
Michele0ea7d782019-03-19 14:58:42 -07007066 loge("isMultiSimSupportedInternal: no static configuration available");
7067 return TelephonyManager.MULTISIM_NOT_SUPPORTED_BY_HARDWARE;
Michele30b57b22019-03-01 12:01:14 -08007068 }
7069 if (staticCapability.logicalModemList.size() < 2) {
Michele0ea7d782019-03-19 14:58:42 -07007070 loge("isMultiSimSupportedInternal: maximum number of modem is < 2");
7071 return TelephonyManager.MULTISIM_NOT_SUPPORTED_BY_HARDWARE;
Michele30b57b22019-03-01 12:01:14 -08007072 }
7073 // Check if support of multiple SIMs is restricted by carrier
7074 if (mTelephonySharedPreferences.getBoolean(PREF_MULTI_SIM_RESTRICTED, false)) {
Michele0ea7d782019-03-19 14:58:42 -07007075 return TelephonyManager.MULTISIM_NOT_SUPPORTED_BY_CARRIER;
Michele30b57b22019-03-01 12:01:14 -08007076 }
7077
Michele0ea7d782019-03-19 14:58:42 -07007078 return TelephonyManager.MULTISIM_ALLOWED;
Michele30b57b22019-03-01 12:01:14 -08007079 }
7080
Nazanin Bakhshi628473f2019-01-29 17:37:52 -08007081 /**
7082 * Switch configs to enable multi-sim or switch back to single-sim
Nazanin Bakhshi17318782019-03-01 11:56:08 -08007083 * Note: Switch from multi-sim to single-sim is only possible with MODIFY_PHONE_STATE
7084 * permission, but the other way around is possible with either MODIFY_PHONE_STATE
7085 * or carrier privileges
Nazanin Bakhshi628473f2019-01-29 17:37:52 -08007086 * @param numOfSims number of active sims we want to switch to
7087 */
7088 @Override
7089 public void switchMultiSimConfig(int numOfSims) {
Nazanin Bakhshi17318782019-03-01 11:56:08 -08007090 if (numOfSims == 1) {
7091 enforceModifyPermission();
7092 } else {
7093 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
7094 mApp, SubscriptionManager.DEFAULT_SUBSCRIPTION_ID, "switchMultiSimConfig");
7095 }
Nazanin Bakhshi628473f2019-01-29 17:37:52 -08007096 final long identity = Binder.clearCallingIdentity();
Michele30b57b22019-03-01 12:01:14 -08007097
Nazanin Bakhshi628473f2019-01-29 17:37:52 -08007098 try {
Michele30b57b22019-03-01 12:01:14 -08007099 //only proceed if multi-sim is not restricted
Michele0ea7d782019-03-19 14:58:42 -07007100 if (isMultiSimSupportedInternal() != TelephonyManager.MULTISIM_ALLOWED) {
Michele30b57b22019-03-01 12:01:14 -08007101 loge("switchMultiSimConfig not possible. It is restricted or not supported.");
7102 return;
7103 }
Nazanin Bakhshi628473f2019-01-29 17:37:52 -08007104 mPhoneConfigurationManager.switchMultiSimConfig(numOfSims);
7105 } finally {
7106 Binder.restoreCallingIdentity(identity);
7107 }
7108 }
7109
7110 /**
chen xub4baa772019-04-03 10:23:41 -07007111 * Get whether making changes to modem configurations will trigger reboot.
7112 * Return value defaults to true.
Nazanin Bakhshi5fe5ef22019-01-30 10:52:09 -08007113 */
7114 @Override
chen xub4baa772019-04-03 10:23:41 -07007115 public boolean doesSwitchMultiSimConfigTriggerReboot(int subId, String callingPackage) {
7116 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
7117 mApp, subId, callingPackage, "doesSwitchMultiSimConfigTriggerReboot")) {
7118 return false;
7119 }
Nazanin Bakhshi5fe5ef22019-01-30 10:52:09 -08007120 final long identity = Binder.clearCallingIdentity();
7121 try {
7122 return mPhoneConfigurationManager.isRebootRequiredForModemConfigChange();
7123 } finally {
7124 Binder.restoreCallingIdentity(identity);
7125 }
7126 }
7127
Nathan Harold29f5f052019-02-15 13:41:57 -08007128 private void updateModemStateMetrics() {
7129 TelephonyMetrics metrics = TelephonyMetrics.getInstance();
7130 // TODO: check the state for each modem if the api is ready.
7131 metrics.updateEnabledModemBitmap((1 << TelephonyManager.from(mApp).getPhoneCount()) - 1);
7132 }
7133
Pengquan Meng3889a572019-01-23 11:16:29 -08007134 @Override
7135 public int[] getSlotsMapping() {
7136 enforceReadPrivilegedPermission("getSlotsMapping");
7137
7138 final long identity = Binder.clearCallingIdentity();
7139 try {
7140 int phoneCount = TelephonyManager.getDefault().getPhoneCount();
7141 // All logical slots should have a mapping to a physical slot.
7142 int[] logicalSlotsMapping = new int[phoneCount];
7143 UiccSlotInfo[] slotInfos = getUiccSlotsInfo();
7144 for (int i = 0; i < slotInfos.length; i++) {
7145 if (SubscriptionManager.isValidPhoneId(slotInfos[i].getLogicalSlotIdx())) {
7146 logicalSlotsMapping[slotInfos[i].getLogicalSlotIdx()] = i;
7147 }
7148 }
7149 return logicalSlotsMapping;
7150 } finally {
7151 Binder.restoreCallingIdentity(identity);
7152 }
7153 }
Nathan Harold48d6fd52019-02-06 19:01:40 -08007154
7155 /**
7156 * Get the IRadio HAL Version
7157 */
7158 @Override
7159 public int getRadioHalVersion() {
7160 Phone phone = getDefaultPhone();
7161 if (phone == null) return -1;
7162 HalVersion hv = phone.getHalVersion();
7163 if (hv.equals(HalVersion.UNKNOWN)) return -1;
7164 return hv.major * 100 + hv.minor;
7165 }
Malcolm Chendc8c10e2019-04-10 18:25:07 -07007166
7167 /**
Malcolm Chene5ad5792019-04-18 13:51:02 -07007168 * Return whether data is enabled for certain APN type. This will tell if framework will accept
7169 * corresponding network requests on a subId.
7170 *
7171 * Data is enabled if:
Malcolm Chendc8c10e2019-04-10 18:25:07 -07007172 * 1) user data is turned on, or
Malcolm Chene5ad5792019-04-18 13:51:02 -07007173 * 2) APN is un-metered for this subscription, or
7174 * 3) APN type is whitelisted. E.g. MMS is whitelisted if
7175 * {@link SubscriptionManager#setAlwaysAllowMmsData} is turned on.
7176 *
7177 * @return whether data is allowed for a apn type.
7178 *
7179 * @hide
Malcolm Chendc8c10e2019-04-10 18:25:07 -07007180 */
7181 @Override
Malcolm Chene5ad5792019-04-18 13:51:02 -07007182 public boolean isDataEnabledForApn(int apnType, int subId, String callingPackage) {
Malcolm Chendc8c10e2019-04-10 18:25:07 -07007183 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
Malcolm Chene5ad5792019-04-18 13:51:02 -07007184 mApp, subId, callingPackage, "isDataEnabledForApn")) {
7185 throw new SecurityException("Needs READ_PHONE_STATE for isDataEnabledForApn");
Malcolm Chendc8c10e2019-04-10 18:25:07 -07007186 }
7187
7188 // Now that all security checks passes, perform the operation as ourselves.
7189 final long identity = Binder.clearCallingIdentity();
7190 try {
7191 Phone phone = getPhone(subId);
7192 if (phone == null) return false;
7193
Jack Yu41407ee2019-05-13 16:54:09 -07007194 boolean isMetered = ApnSettingUtils.isMeteredApnType(apnType, phone);
Malcolm Chene5ad5792019-04-18 13:51:02 -07007195 return !isMetered || phone.getDataEnabledSettings().isDataEnabled(apnType);
7196 } finally {
7197 Binder.restoreCallingIdentity(identity);
7198 }
7199 }
7200
7201 @Override
Jack Yu41407ee2019-05-13 16:54:09 -07007202 public boolean isApnMetered(@ApnType int apnType, int subId) {
Malcolm Chene5ad5792019-04-18 13:51:02 -07007203 enforceReadPrivilegedPermission("isApnMetered");
7204
7205 // Now that all security checks passes, perform the operation as ourselves.
7206 final long identity = Binder.clearCallingIdentity();
7207 try {
7208 Phone phone = getPhone(subId);
7209 if (phone == null) return true; // By default return true.
7210
Jack Yu41407ee2019-05-13 16:54:09 -07007211 return ApnSettingUtils.isMeteredApnType(apnType, phone);
Malcolm Chendc8c10e2019-04-10 18:25:07 -07007212 } finally {
7213 Binder.restoreCallingIdentity(identity);
7214 }
7215 }
Brad Ebingera63db5f2019-04-23 16:31:13 -07007216
7217 @Override
7218 public void enqueueSmsPickResult(String callingPackage, IIntegerConsumer pendingSubIdResult) {
7219 SmsPermissions permissions = new SmsPermissions(getDefaultPhone(), mApp,
7220 (AppOpsManager) mApp.getSystemService(Context.APP_OPS_SERVICE));
7221 if (!permissions.checkCallingCanSendSms(callingPackage, "Sending message")) {
7222 throw new SecurityException("Requires SEND_SMS permission to perform this operation");
7223 }
7224 PickSmsSubscriptionActivity.addPendingResult(pendingSubIdResult);
7225 Intent intent = new Intent();
7226 intent.setClass(mApp, PickSmsSubscriptionActivity.class);
7227 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
7228 // Bring up choose default SMS subscription dialog right now
7229 intent.putExtra(PickSmsSubscriptionActivity.DIALOG_TYPE_KEY,
7230 PickSmsSubscriptionActivity.SMS_PICK_FOR_MESSAGE);
7231 mApp.startActivity(intent);
7232 }
chen xud5ca2d52019-05-28 15:20:57 -07007233
7234 @Override
7235 public String getMmsUAProfUrl(int subId) {
7236 //TODO investigate if this API should require proper permission check in R b/133791609
7237 final long identity = Binder.clearCallingIdentity();
7238 try {
7239 return SubscriptionManager.getResourcesForSubId(getDefaultPhone().getContext(), subId)
7240 .getString(com.android.internal.R.string.config_mms_user_agent_profile_url);
7241 } finally {
7242 Binder.restoreCallingIdentity(identity);
7243 }
7244 }
7245
7246 @Override
7247 public String getMmsUserAgent(int subId) {
7248 //TODO investigate if this API should require proper permission check in R b/133791609
7249 final long identity = Binder.clearCallingIdentity();
7250 try {
7251 return SubscriptionManager.getResourcesForSubId(getDefaultPhone().getContext(), subId)
7252 .getString(com.android.internal.R.string.config_mms_user_agent);
7253 } finally {
7254 Binder.restoreCallingIdentity(identity);
7255 }
7256 }
Jack Yub07d4972019-05-28 16:12:25 -07007257
7258 @Override
7259 public boolean setDataAllowedDuringVoiceCall(int subId, boolean allow) {
7260 enforceModifyPermission();
7261
7262 // Now that all security checks passes, perform the operation as ourselves.
7263 final long identity = Binder.clearCallingIdentity();
7264 try {
7265 Phone phone = getPhone(subId);
7266 if (phone == null) return false;
7267
7268 return phone.getDataEnabledSettings().setAllowDataDuringVoiceCall(allow);
7269 } finally {
7270 Binder.restoreCallingIdentity(identity);
7271 }
7272 }
7273
7274 @Override
7275 public boolean isDataAllowedInVoiceCall(int subId) {
7276 enforceReadPrivilegedPermission("isDataAllowedInVoiceCall");
7277
7278 // Now that all security checks passes, perform the operation as ourselves.
7279 final long identity = Binder.clearCallingIdentity();
7280 try {
7281 Phone phone = getPhone(subId);
7282 if (phone == null) return false;
7283
7284 return phone.getDataEnabledSettings().isDataAllowedInVoiceCall();
7285 } finally {
7286 Binder.restoreCallingIdentity(identity);
7287 }
7288 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07007289}