blob: e1e1a06cf25781bcd5c7019f6f83ba35628e9fe6 [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")
1958 .setMinSdkVersionForFine(Build.VERSION_CODES.Q)
1959 .build());
1960 switch (locationResult) {
1961 case DENIED_HARD:
1962 throw new SecurityException("Not allowed to access cell location");
1963 case DENIED_SOFT:
1964 return new Bundle();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001965 }
1966
Narayan Kamathf04b5a12018-01-09 11:47:15 +00001967 WorkSource workSource = getWorkSource(Binder.getCallingUid());
Malcolm Chenaa4a8532018-02-28 15:00:40 -08001968 final long identity = Binder.clearCallingIdentity();
1969 try {
1970 if (DBG_LOC) log("getCellLocation: is active user");
1971 Bundle data = new Bundle();
Nathan Harold3ff88932018-08-14 10:19:49 -07001972 int subId = mSubscriptionController.getDefaultDataSubId();
1973 CellLocation cl = (CellLocation) sendRequest(CMD_GET_CELL_LOCATION, workSource, subId);
1974 cl.fillInNotifierBundle(data);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08001975 return data;
1976 } finally {
1977 Binder.restoreCallingIdentity(identity);
1978 }
Svetoslav64fad262015-04-14 14:35:21 -07001979 }
1980
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001981 @Override
Jonathan Basseribf5362b2017-07-19 12:22:35 -07001982 public String getNetworkCountryIsoForPhone(int phoneId) {
1983 // Reporting the correct network country is ambiguous when IWLAN could conflict with
1984 // registered cell info, so return a NULL country instead.
1985 final long identity = Binder.clearCallingIdentity();
1986 try {
Malcolm Chen3732c2b2018-07-18 20:15:24 -07001987 if (phoneId == SubscriptionManager.INVALID_PHONE_INDEX) {
1988 // Get default phone in this case.
1989 phoneId = SubscriptionManager.DEFAULT_PHONE_INDEX;
1990 }
Jonathan Basseribf5362b2017-07-19 12:22:35 -07001991 final int subId = mSubscriptionController.getSubIdUsingPhoneId(phoneId);
Jack Yu5f7092c2018-04-13 14:05:37 -07001992 // Todo: fix this when we can get the actual cellular network info when the device
1993 // is on IWLAN.
Jonathan Basseribf5362b2017-07-19 12:22:35 -07001994 if (TelephonyManager.NETWORK_TYPE_IWLAN
1995 == getVoiceNetworkTypeForSubscriber(subId, mApp.getPackageName())) {
1996 return "";
1997 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08001998 Phone phone = PhoneFactory.getPhone(phoneId);
1999 if (phone != null) {
2000 ServiceStateTracker sst = phone.getServiceStateTracker();
sqianb9d961a2019-07-31 20:23:45 -07002001 EmergencyNumberTracker emergencyNumberTracker = phone.getEmergencyNumberTracker();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002002 if (sst != null) {
2003 LocaleTracker lt = sst.getLocaleTracker();
2004 if (lt != null) {
sqianb9d961a2019-07-31 20:23:45 -07002005 if (!TextUtils.isEmpty(lt.getCurrentCountry())) {
2006 return lt.getCurrentCountry();
2007 } else if (emergencyNumberTracker != null) {
2008 return emergencyNumberTracker.getEmergencyCountryIso();
2009 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002010 }
2011 }
2012 }
2013 return "";
Jonathan Basseribf5362b2017-07-19 12:22:35 -07002014 } finally {
2015 Binder.restoreCallingIdentity(identity);
2016 }
Jonathan Basseribf5362b2017-07-19 12:22:35 -07002017 }
2018
2019 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002020 public void enableLocationUpdates() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07002021 enableLocationUpdatesForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07002022 }
2023
Sanket Padawe356d7632015-06-22 14:03:32 -07002024 @Override
Wink Savilleb564aae2014-10-23 10:18:09 -07002025 public void enableLocationUpdatesForSubscriber(int subId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002026 mApp.enforceCallingOrSelfPermission(
2027 android.Manifest.permission.CONTROL_LOCATION_UPDATES, null);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002028
2029 final long identity = Binder.clearCallingIdentity();
2030 try {
2031 final Phone phone = getPhone(subId);
2032 if (phone != null) {
2033 phone.enableLocationUpdates();
2034 }
2035 } finally {
2036 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07002037 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002038 }
2039
2040 @Override
2041 public void disableLocationUpdates() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07002042 disableLocationUpdatesForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07002043 }
2044
Sanket Padawe356d7632015-06-22 14:03:32 -07002045 @Override
Wink Savilleb564aae2014-10-23 10:18:09 -07002046 public void disableLocationUpdatesForSubscriber(int subId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002047 mApp.enforceCallingOrSelfPermission(
2048 android.Manifest.permission.CONTROL_LOCATION_UPDATES, null);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002049
2050 final long identity = Binder.clearCallingIdentity();
2051 try {
2052 final Phone phone = getPhone(subId);
2053 if (phone != null) {
2054 phone.disableLocationUpdates();
2055 }
2056 } finally {
2057 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07002058 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002059 }
2060
Nathan Harold31d7ff32018-10-15 20:20:30 -07002061 /**
2062 * Returns the target SDK version number for a given package name.
2063 *
Nathan Haroldba61eeb2019-07-10 17:04:16 -07002064 * This call MUST be invoked before clearing the calling UID.
2065 *
Nathan Harold31d7ff32018-10-15 20:20:30 -07002066 * @return target SDK if the package is found or INT_MAX.
2067 */
2068 private int getTargetSdk(String packageName) {
2069 try {
Nathan Haroldba61eeb2019-07-10 17:04:16 -07002070 final ApplicationInfo ai = mApp.getPackageManager().getApplicationInfoAsUser(
2071 packageName, 0, UserHandle.getUserId(Binder.getCallingUid()));
Nathan Harold31d7ff32018-10-15 20:20:30 -07002072 if (ai != null) return ai.targetSdkVersion;
2073 } catch (PackageManager.NameNotFoundException unexpected) {
Nathan Haroldba61eeb2019-07-10 17:04:16 -07002074 loge("Failed to get package info for pkg="
2075 + packageName + ", uid=" + Binder.getCallingUid());
Nathan Harold31d7ff32018-10-15 20:20:30 -07002076 }
2077 return Integer.MAX_VALUE;
2078 }
2079
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002080 @Override
2081 @SuppressWarnings("unchecked")
Nathan Harold31d7ff32018-10-15 20:20:30 -07002082 public List<NeighboringCellInfo> getNeighboringCellInfo(String callingPackage) {
2083 final int targetSdk = getTargetSdk(callingPackage);
Nathan Harolddbea45a2018-08-30 14:35:07 -07002084 if (targetSdk >= android.os.Build.VERSION_CODES.Q) {
2085 throw new SecurityException(
2086 "getNeighboringCellInfo() is unavailable to callers targeting Q+ SDK levels.");
2087 }
Nathan Haroldb4d55612018-07-20 13:13:08 -07002088
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002089 if (mAppOps.noteOp(AppOpsManager.OP_NEIGHBORING_CELLS, Binder.getCallingUid(),
2090 callingPackage) != AppOpsManager.MODE_ALLOWED) {
2091 return null;
2092 }
Svetoslav64fad262015-04-14 14:35:21 -07002093
Svetoslav Ganov4a9d4482017-06-20 19:53:35 -07002094 if (DBG_LOC) log("getNeighboringCellInfo: is active user");
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002095
Nathan Haroldf180aac2018-06-01 18:43:55 -07002096 List<CellInfo> info = getAllCellInfo(callingPackage);
2097 if (info == null) return null;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002098
Nathan Haroldf180aac2018-06-01 18:43:55 -07002099 List<NeighboringCellInfo> neighbors = new ArrayList<NeighboringCellInfo>();
2100 for (CellInfo ci : info) {
2101 if (ci instanceof CellInfoGsm) {
2102 neighbors.add(new NeighboringCellInfo((CellInfoGsm) ci));
2103 } else if (ci instanceof CellInfoWcdma) {
2104 neighbors.add(new NeighboringCellInfo((CellInfoWcdma) ci));
2105 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002106 }
Nathan Haroldf180aac2018-06-01 18:43:55 -07002107 return (neighbors.size()) > 0 ? neighbors : null;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002108 }
2109
Nathan Haroldfa8da0f2018-09-27 18:51:29 -07002110 private List<CellInfo> getCachedCellInfo() {
2111 List<CellInfo> cellInfos = new ArrayList<CellInfo>();
2112 for (Phone phone : PhoneFactory.getPhones()) {
2113 List<CellInfo> info = phone.getAllCellInfo();
2114 if (info != null) cellInfos.addAll(info);
2115 }
2116 return cellInfos;
2117 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002118
2119 @Override
Svetoslav64fad262015-04-14 14:35:21 -07002120 public List<CellInfo> getAllCellInfo(String callingPackage) {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08002121 mApp.getSystemService(AppOpsManager.class)
Hall Liu1aa510f2017-11-22 17:40:08 -08002122 .checkPackage(Binder.getCallingUid(), callingPackage);
Hall Liuf19c44f2018-11-27 14:38:17 -08002123
2124 LocationAccessPolicy.LocationPermissionResult locationResult =
2125 LocationAccessPolicy.checkLocationPermission(mApp,
2126 new LocationAccessPolicy.LocationPermissionQuery.Builder()
2127 .setCallingPackage(callingPackage)
2128 .setCallingPid(Binder.getCallingPid())
2129 .setCallingUid(Binder.getCallingUid())
2130 .setMethod("getAllCellInfo")
Nathan Harold5ae50b52019-02-20 15:46:36 -08002131 .setMinSdkVersionForCoarse(Build.VERSION_CODES.BASE)
Hall Liuf19c44f2018-11-27 14:38:17 -08002132 .setMinSdkVersionForFine(Build.VERSION_CODES.Q)
2133 .build());
2134 switch (locationResult) {
2135 case DENIED_HARD:
2136 throw new SecurityException("Not allowed to access cell info");
2137 case DENIED_SOFT:
2138 return new ArrayList<>();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002139 }
2140
Nathan Haroldfa8da0f2018-09-27 18:51:29 -07002141 final int targetSdk = getTargetSdk(callingPackage);
2142 if (targetSdk >= android.os.Build.VERSION_CODES.Q) {
2143 return getCachedCellInfo();
2144 }
2145
Svetoslav Ganov4a9d4482017-06-20 19:53:35 -07002146 if (DBG_LOC) log("getAllCellInfo: is active user");
Narayan Kamathf04b5a12018-01-09 11:47:15 +00002147 WorkSource workSource = getWorkSource(Binder.getCallingUid());
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002148 final long identity = Binder.clearCallingIdentity();
2149 try {
2150 List<CellInfo> cellInfos = new ArrayList<CellInfo>();
2151 for (Phone phone : PhoneFactory.getPhones()) {
Nathan Harold3ff88932018-08-14 10:19:49 -07002152 final List<CellInfo> info = (List<CellInfo>) sendRequest(
Nathan Harold92bed182018-10-12 18:16:49 -07002153 CMD_GET_ALL_CELL_INFO, null, phone, workSource);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002154 if (info != null) cellInfos.addAll(info);
2155 }
2156 return cellInfos;
2157 } finally {
2158 Binder.restoreCallingIdentity(identity);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002159 }
2160 }
2161
Sailesh Nepalbd76e4e2013-10-27 13:59:44 -07002162 @Override
Nathan Haroldfa8da0f2018-09-27 18:51:29 -07002163 public void requestCellInfoUpdate(int subId, ICellInfoCallback cb, String callingPackage) {
2164 requestCellInfoUpdateInternal(
2165 subId, cb, callingPackage, getWorkSource(Binder.getCallingUid()));
2166 }
2167
2168 @Override
2169 public void requestCellInfoUpdateWithWorkSource(
2170 int subId, ICellInfoCallback cb, String callingPackage, WorkSource workSource) {
2171 enforceModifyPermission();
2172 requestCellInfoUpdateInternal(subId, cb, callingPackage, workSource);
2173 }
2174
2175 private void requestCellInfoUpdateInternal(
2176 int subId, ICellInfoCallback cb, String callingPackage, WorkSource workSource) {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08002177 mApp.getSystemService(AppOpsManager.class)
Nathan Haroldfa8da0f2018-09-27 18:51:29 -07002178 .checkPackage(Binder.getCallingUid(), callingPackage);
Hall Liuf19c44f2018-11-27 14:38:17 -08002179
2180 LocationAccessPolicy.LocationPermissionResult locationResult =
2181 LocationAccessPolicy.checkLocationPermission(mApp,
2182 new LocationAccessPolicy.LocationPermissionQuery.Builder()
2183 .setCallingPackage(callingPackage)
2184 .setCallingPid(Binder.getCallingPid())
2185 .setCallingUid(Binder.getCallingUid())
2186 .setMethod("requestCellInfoUpdate")
2187 .setMinSdkVersionForFine(Build.VERSION_CODES.Q)
2188 .build());
2189 switch (locationResult) {
2190 case DENIED_HARD:
2191 throw new SecurityException("Not allowed to access cell info");
2192 case DENIED_SOFT:
Nathan Haroldb3c5da42019-05-09 10:26:08 -07002193 try {
2194 cb.onCellInfo(new ArrayList<CellInfo>());
2195 } catch (RemoteException re) {
2196 // Drop without consequences
2197 }
Hall Liuf19c44f2018-11-27 14:38:17 -08002198 return;
Nathan Haroldfa8da0f2018-09-27 18:51:29 -07002199 }
2200
Nathan Harold32e84c42019-05-09 10:13:47 -07002201
2202 final Phone phone = getPhoneFromSubId(subId);
Nathan Haroldfa8da0f2018-09-27 18:51:29 -07002203 if (phone == null) throw new IllegalArgumentException("Invalid Subscription Id: " + subId);
2204
2205 sendRequestAsync(CMD_REQUEST_CELL_INFO_UPDATE, cb, phone, workSource);
2206 }
2207
2208 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002209 public void setCellInfoListRate(int rateInMillis) {
Jack Yua8d8cb82017-01-16 10:15:34 -08002210 enforceModifyPermission();
Narayan Kamathf04b5a12018-01-09 11:47:15 +00002211 WorkSource workSource = getWorkSource(Binder.getCallingUid());
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002212
2213 final long identity = Binder.clearCallingIdentity();
2214 try {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08002215 getDefaultPhone().setCellInfoListRate(rateInMillis, workSource);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002216 } finally {
2217 Binder.restoreCallingIdentity(identity);
2218 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002219 }
2220
Shishir Agrawala9f32182016-04-12 12:00:16 -07002221 @Override
Sanket Padawe13bac7b2017-03-20 15:04:47 -07002222 public String getImeiForSlot(int slotIndex, String callingPackage) {
Jeff Davidson913390f2018-02-23 17:11:49 -08002223 Phone phone = PhoneFactory.getPhone(slotIndex);
2224 if (phone == null) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08002225 return null;
2226 }
Jeff Davidson913390f2018-02-23 17:11:49 -08002227 int subId = phone.getSubId();
Michael Groover70af6dc2018-10-01 16:23:15 -07002228 if (!TelephonyPermissions.checkCallingOrSelfReadDeviceIdentifiers(mApp, subId,
2229 callingPackage, "getImeiForSlot")) {
Jeff Davidson913390f2018-02-23 17:11:49 -08002230 return null;
2231 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002232
2233 final long identity = Binder.clearCallingIdentity();
2234 try {
2235 return phone.getImei();
2236 } finally {
2237 Binder.restoreCallingIdentity(identity);
2238 }
Shishir Agrawala9f32182016-04-12 12:00:16 -07002239 }
2240
2241 @Override
David Kelly5e06a7f2018-03-12 14:10:59 +00002242 public String getTypeAllocationCodeForSlot(int slotIndex) {
2243 Phone phone = PhoneFactory.getPhone(slotIndex);
2244 String tac = null;
2245 if (phone != null) {
2246 String imei = phone.getImei();
2247 tac = imei == null ? null : imei.substring(0, TYPE_ALLOCATION_CODE_LENGTH);
2248 }
2249 return tac;
2250 }
2251
2252 @Override
Jack Yu2af8d712017-03-15 17:14:14 -07002253 public String getMeidForSlot(int slotIndex, String callingPackage) {
Jeff Davidson913390f2018-02-23 17:11:49 -08002254 Phone phone = PhoneFactory.getPhone(slotIndex);
2255 if (phone == null) {
Jack Yu2af8d712017-03-15 17:14:14 -07002256 return null;
2257 }
Nazanin Bakhshif782e562018-12-11 15:15:39 -08002258
Jeff Davidson913390f2018-02-23 17:11:49 -08002259 int subId = phone.getSubId();
Michael Groover70af6dc2018-10-01 16:23:15 -07002260 if (!TelephonyPermissions.checkCallingOrSelfReadDeviceIdentifiers(mApp, subId,
2261 callingPackage, "getMeidForSlot")) {
Jeff Davidson913390f2018-02-23 17:11:49 -08002262 return null;
2263 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002264
2265 final long identity = Binder.clearCallingIdentity();
2266 try {
2267 return phone.getMeid();
2268 } finally {
2269 Binder.restoreCallingIdentity(identity);
2270 }
Jack Yu2af8d712017-03-15 17:14:14 -07002271 }
2272
2273 @Override
David Kelly5e06a7f2018-03-12 14:10:59 +00002274 public String getManufacturerCodeForSlot(int slotIndex) {
2275 Phone phone = PhoneFactory.getPhone(slotIndex);
2276 String manufacturerCode = null;
2277 if (phone != null) {
2278 String meid = phone.getMeid();
2279 manufacturerCode = meid == null ? null : meid.substring(0, MANUFACTURER_CODE_LENGTH);
2280 }
2281 return manufacturerCode;
2282 }
2283
2284 @Override
Sanket Padawe13bac7b2017-03-20 15:04:47 -07002285 public String getDeviceSoftwareVersionForSlot(int slotIndex, String callingPackage) {
Jeff Davidson913390f2018-02-23 17:11:49 -08002286 Phone phone = PhoneFactory.getPhone(slotIndex);
2287 if (phone == null) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08002288 return null;
2289 }
Jeff Davidson913390f2018-02-23 17:11:49 -08002290 int subId = phone.getSubId();
2291 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
2292 mApp, subId, callingPackage, "getDeviceSoftwareVersionForSlot")) {
2293 return null;
2294 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002295
2296 final long identity = Binder.clearCallingIdentity();
2297 try {
2298 return phone.getDeviceSvn();
2299 } finally {
2300 Binder.restoreCallingIdentity(identity);
2301 }
Shishir Agrawala9f32182016-04-12 12:00:16 -07002302 }
2303
fionaxu43304da2017-11-27 22:51:16 -08002304 @Override
2305 public int getSubscriptionCarrierId(int subId) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002306 final long identity = Binder.clearCallingIdentity();
2307 try {
2308 final Phone phone = getPhone(subId);
2309 return phone == null ? TelephonyManager.UNKNOWN_CARRIER_ID : phone.getCarrierId();
2310 } finally {
2311 Binder.restoreCallingIdentity(identity);
2312 }
fionaxu43304da2017-11-27 22:51:16 -08002313 }
2314
2315 @Override
2316 public String getSubscriptionCarrierName(int subId) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002317 final long identity = Binder.clearCallingIdentity();
2318 try {
2319 final Phone phone = getPhone(subId);
2320 return phone == null ? null : phone.getCarrierName();
2321 } finally {
2322 Binder.restoreCallingIdentity(identity);
2323 }
fionaxu43304da2017-11-27 22:51:16 -08002324 }
2325
calvinpanffe225e2018-11-01 19:43:06 +08002326 @Override
chen xu0026ca62019-03-06 15:28:50 -08002327 public int getSubscriptionSpecificCarrierId(int subId) {
chen xu25637222018-11-04 17:17:00 -08002328 final long identity = Binder.clearCallingIdentity();
2329 try {
2330 final Phone phone = getPhone(subId);
2331 return phone == null ? TelephonyManager.UNKNOWN_CARRIER_ID
chen xu0026ca62019-03-06 15:28:50 -08002332 : phone.getSpecificCarrierId();
chen xu25637222018-11-04 17:17:00 -08002333 } finally {
2334 Binder.restoreCallingIdentity(identity);
2335 }
2336 }
2337
2338 @Override
chen xu0026ca62019-03-06 15:28:50 -08002339 public String getSubscriptionSpecificCarrierName(int subId) {
chen xu25637222018-11-04 17:17:00 -08002340 final long identity = Binder.clearCallingIdentity();
2341 try {
2342 final Phone phone = getPhone(subId);
chen xu0026ca62019-03-06 15:28:50 -08002343 return phone == null ? null : phone.getSpecificCarrierName();
chen xu25637222018-11-04 17:17:00 -08002344 } finally {
2345 Binder.restoreCallingIdentity(identity);
2346 }
2347 }
2348
chen xu651eec72018-11-11 19:03:44 -08002349 @Override
chen xu864e11c2018-12-06 22:10:03 -08002350 public int getCarrierIdFromMccMnc(int slotIndex, String mccmnc, boolean isSubscriptionMccMnc) {
2351 if (!isSubscriptionMccMnc) {
2352 enforceReadPrivilegedPermission("getCarrierIdFromMccMnc");
2353 }
chen xu651eec72018-11-11 19:03:44 -08002354 final Phone phone = PhoneFactory.getPhone(slotIndex);
2355 if (phone == null) {
2356 return TelephonyManager.UNKNOWN_CARRIER_ID;
2357 }
2358 final long identity = Binder.clearCallingIdentity();
2359 try {
2360 return CarrierResolver.getCarrierIdFromMccMnc(phone.getContext(), mccmnc);
2361 } finally {
2362 Binder.restoreCallingIdentity(identity);
2363 }
2364 }
2365
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002366 //
2367 // Internal helper methods.
2368 //
2369
Sanket Padaweee13a9b2016-03-08 17:30:28 -08002370 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002371 * Make sure the caller has the MODIFY_PHONE_STATE permission.
2372 *
2373 * @throws SecurityException if the caller does not have the required permission
2374 */
2375 private void enforceModifyPermission() {
2376 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.MODIFY_PHONE_STATE, null);
2377 }
2378
2379 /**
2380 * Make sure the caller has the CALL_PHONE permission.
2381 *
2382 * @throws SecurityException if the caller does not have the required permission
2383 */
2384 private void enforceCallPermission() {
2385 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.CALL_PHONE, null);
2386 }
2387
Stuart Scott8eef64f2015-04-08 15:13:54 -07002388 private void enforceConnectivityInternalPermission() {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08002389 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.CONNECTIVITY_INTERNAL,
Stuart Scott8eef64f2015-04-08 15:13:54 -07002390 "ConnectivityService");
2391 }
2392
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002393 private String createTelUrl(String number) {
2394 if (TextUtils.isEmpty(number)) {
2395 return null;
2396 }
2397
Jake Hambye994d462014-02-03 13:10:13 -08002398 return "tel:" + number;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002399 }
2400
Ihab Awadf9e92732013-12-05 18:02:52 -08002401 private static void log(String msg) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002402 Log.d(LOG_TAG, "[PhoneIntfMgr] " + msg);
2403 }
2404
Naveen Kalla1fd79bd2014-08-08 00:48:59 -07002405 private static void logv(String msg) {
2406 Log.v(LOG_TAG, "[PhoneIntfMgr] " + msg);
2407 }
2408
Ihab Awadf9e92732013-12-05 18:02:52 -08002409 private static void loge(String msg) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002410 Log.e(LOG_TAG, "[PhoneIntfMgr] " + msg);
2411 }
2412
Robert Greenwalt36b23af2015-07-06 17:59:14 -07002413 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002414 public int getActivePhoneType() {
Shishir Agrawala9f32182016-04-12 12:00:16 -07002415 return getActivePhoneTypeForSlot(getSlotForDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07002416 }
2417
Sanket Padawe356d7632015-06-22 14:03:32 -07002418 @Override
Sanket Padawe13bac7b2017-03-20 15:04:47 -07002419 public int getActivePhoneTypeForSlot(int slotIndex) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002420 final long identity = Binder.clearCallingIdentity();
2421 try {
2422 final Phone phone = PhoneFactory.getPhone(slotIndex);
2423 if (phone == null) {
2424 return PhoneConstants.PHONE_TYPE_NONE;
2425 } else {
2426 return phone.getPhoneType();
2427 }
2428 } finally {
2429 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07002430 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002431 }
2432
2433 /**
2434 * Returns the CDMA ERI icon index to display
2435 */
Robert Greenwalt36b23af2015-07-06 17:59:14 -07002436 @Override
2437 public int getCdmaEriIconIndex(String callingPackage) {
2438 return getCdmaEriIconIndexForSubscriber(getDefaultSubscription(), callingPackage);
Wink Saville36469e72014-06-11 15:17:00 -07002439 }
2440
Sanket Padawe356d7632015-06-22 14:03:32 -07002441 @Override
Robert Greenwalt36b23af2015-07-06 17:59:14 -07002442 public int getCdmaEriIconIndexForSubscriber(int subId, String callingPackage) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08002443 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
Jeff Davidson913390f2018-02-23 17:11:49 -08002444 mApp, subId, callingPackage, "getCdmaEriIconIndexForSubscriber")) {
Robert Greenwalt36b23af2015-07-06 17:59:14 -07002445 return -1;
2446 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002447
2448 final long identity = Binder.clearCallingIdentity();
2449 try {
2450 final Phone phone = getPhone(subId);
2451 if (phone != null) {
2452 return phone.getCdmaEriIconIndex();
2453 } else {
2454 return -1;
2455 }
2456 } finally {
2457 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07002458 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002459 }
2460
2461 /**
2462 * Returns the CDMA ERI icon mode,
2463 * 0 - ON
2464 * 1 - FLASHING
2465 */
Robert Greenwalt36b23af2015-07-06 17:59:14 -07002466 @Override
2467 public int getCdmaEriIconMode(String callingPackage) {
2468 return getCdmaEriIconModeForSubscriber(getDefaultSubscription(), callingPackage);
Wink Saville36469e72014-06-11 15:17:00 -07002469 }
2470
Sanket Padawe356d7632015-06-22 14:03:32 -07002471 @Override
Robert Greenwalt36b23af2015-07-06 17:59:14 -07002472 public int getCdmaEriIconModeForSubscriber(int subId, String callingPackage) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08002473 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
Jeff Davidson913390f2018-02-23 17:11:49 -08002474 mApp, subId, callingPackage, "getCdmaEriIconModeForSubscriber")) {
Robert Greenwalt36b23af2015-07-06 17:59:14 -07002475 return -1;
2476 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002477
2478 final long identity = Binder.clearCallingIdentity();
2479 try {
2480 final Phone phone = getPhone(subId);
2481 if (phone != null) {
2482 return phone.getCdmaEriIconMode();
2483 } else {
2484 return -1;
2485 }
2486 } finally {
2487 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07002488 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002489 }
2490
2491 /**
2492 * Returns the CDMA ERI text,
2493 */
Robert Greenwalt36b23af2015-07-06 17:59:14 -07002494 @Override
2495 public String getCdmaEriText(String callingPackage) {
2496 return getCdmaEriTextForSubscriber(getDefaultSubscription(), callingPackage);
Wink Saville36469e72014-06-11 15:17:00 -07002497 }
2498
Sanket Padawe356d7632015-06-22 14:03:32 -07002499 @Override
Robert Greenwalt36b23af2015-07-06 17:59:14 -07002500 public String getCdmaEriTextForSubscriber(int subId, String callingPackage) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08002501 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
Jeff Davidson913390f2018-02-23 17:11:49 -08002502 mApp, subId, callingPackage, "getCdmaEriIconTextForSubscriber")) {
Robert Greenwalt36b23af2015-07-06 17:59:14 -07002503 return null;
2504 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002505
2506 final long identity = Binder.clearCallingIdentity();
2507 try {
2508 final Phone phone = getPhone(subId);
2509 if (phone != null) {
2510 return phone.getCdmaEriText();
2511 } else {
2512 return null;
2513 }
2514 } finally {
2515 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07002516 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002517 }
2518
2519 /**
Junda Liuca05d5d2014-08-14 22:36:34 -07002520 * Returns the CDMA MDN.
2521 */
Sanket Padawe356d7632015-06-22 14:03:32 -07002522 @Override
Wink Savilleb564aae2014-10-23 10:18:09 -07002523 public String getCdmaMdn(int subId) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08002524 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
2525 mApp, subId, "getCdmaMdn");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002526
2527 final long identity = Binder.clearCallingIdentity();
2528 try {
2529 final Phone phone = getPhone(subId);
Nazanin Bakhshif782e562018-12-11 15:15:39 -08002530 if (phone != null && phone.getPhoneType() == PhoneConstants.PHONE_TYPE_CDMA) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002531 return phone.getLine1Number();
2532 } else {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08002533 loge("getCdmaMdn: no phone found. Invalid subId: " + subId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002534 return null;
2535 }
2536 } finally {
2537 Binder.restoreCallingIdentity(identity);
Junda Liuca05d5d2014-08-14 22:36:34 -07002538 }
2539 }
2540
2541 /**
2542 * Returns the CDMA MIN.
2543 */
Sanket Padawe356d7632015-06-22 14:03:32 -07002544 @Override
Wink Savilleb564aae2014-10-23 10:18:09 -07002545 public String getCdmaMin(int subId) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08002546 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
2547 mApp, subId, "getCdmaMin");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002548
2549 final long identity = Binder.clearCallingIdentity();
2550 try {
2551 final Phone phone = getPhone(subId);
2552 if (phone != null && phone.getPhoneType() == PhoneConstants.PHONE_TYPE_CDMA) {
2553 return phone.getCdmaMin();
2554 } else {
2555 return null;
2556 }
2557 } finally {
2558 Binder.restoreCallingIdentity(identity);
Junda Liuca05d5d2014-08-14 22:36:34 -07002559 }
2560 }
2561
Hall Liud892bec2018-11-30 14:51:45 -08002562 @Override
2563 public void requestNumberVerification(PhoneNumberRange range, long timeoutMillis,
2564 INumberVerificationCallback callback, String callingPackage) {
2565 if (mApp.checkCallingOrSelfPermission(android.Manifest.permission.MODIFY_PHONE_STATE)
2566 != PERMISSION_GRANTED) {
2567 throw new SecurityException("Caller must hold the MODIFY_PHONE_STATE permission");
2568 }
2569 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
2570
2571 String authorizedPackage = NumberVerificationManager.getAuthorizedPackage(mApp);
2572 if (!TextUtils.equals(callingPackage, authorizedPackage)) {
2573 throw new SecurityException("Calling package must be configured in the device config");
2574 }
2575
2576 if (range == null) {
2577 throw new NullPointerException("Range must be non-null");
2578 }
2579
2580 timeoutMillis = Math.min(timeoutMillis,
Hall Liubd069e32019-02-28 18:56:30 -08002581 TelephonyManager.getMaxNumberVerificationTimeoutMillis());
Hall Liud892bec2018-11-30 14:51:45 -08002582
2583 NumberVerificationManager.getInstance().requestVerification(range, callback, timeoutMillis);
2584 }
2585
Junda Liuca05d5d2014-08-14 22:36:34 -07002586 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002587 * Returns true if CDMA provisioning needs to run.
2588 */
2589 public boolean needsOtaServiceProvisioning() {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002590 final long identity = Binder.clearCallingIdentity();
2591 try {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08002592 return getDefaultPhone().needsOtaServiceProvisioning();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002593 } finally {
2594 Binder.restoreCallingIdentity(identity);
2595 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002596 }
2597
2598 /**
Shishir Agrawal76d5da92014-11-09 16:17:25 -08002599 * Sets the voice mail number of a given subId.
2600 */
2601 @Override
2602 public boolean setVoiceMailNumber(int subId, String alphaTag, String number) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08002603 TelephonyPermissions.enforceCallingOrSelfCarrierPrivilege(subId, "setVoiceMailNumber");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002604
2605 final long identity = Binder.clearCallingIdentity();
2606 try {
2607 Boolean success = (Boolean) sendRequest(CMD_SET_VOICEMAIL_NUMBER,
2608 new Pair<String, String>(alphaTag, number), new Integer(subId));
2609 return success;
2610 } finally {
2611 Binder.restoreCallingIdentity(identity);
2612 }
Shishir Agrawal76d5da92014-11-09 16:17:25 -08002613 }
2614
Ta-wei Yen87c49842016-05-13 21:19:52 -07002615 @Override
Ta-wei Yenc9df0432017-04-17 17:09:07 -07002616 public Bundle getVisualVoicemailSettings(String callingPackage, int subId) {
2617 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
Nazanin Bakhshif782e562018-12-11 15:15:39 -08002618 String systemDialer = TelecomManager.from(mApp).getSystemDialerPackage();
Ta-wei Yenc9df0432017-04-17 17:09:07 -07002619 if (!TextUtils.equals(callingPackage, systemDialer)) {
2620 throw new SecurityException("caller must be system dialer");
2621 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002622
2623 final long identity = Binder.clearCallingIdentity();
2624 try {
2625 PhoneAccountHandle phoneAccountHandle = PhoneAccountHandleConverter.fromSubId(subId);
2626 if (phoneAccountHandle == null) {
2627 return null;
2628 }
Nazanin Bakhshif782e562018-12-11 15:15:39 -08002629 return VisualVoicemailSettingsUtil.dump(mApp, phoneAccountHandle);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002630 } finally {
2631 Binder.restoreCallingIdentity(identity);
Ta-wei Yenc9df0432017-04-17 17:09:07 -07002632 }
Ta-wei Yenc9df0432017-04-17 17:09:07 -07002633 }
2634
2635 @Override
Ta-wei Yen409ac562017-03-06 16:00:44 -08002636 public String getVisualVoicemailPackageName(String callingPackage, int subId) {
Ta-wei Yendca928f2017-01-10 16:17:08 -08002637 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
Jeff Davidson7e17e312018-02-13 18:17:36 -08002638 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
Jeff Davidson913390f2018-02-23 17:11:49 -08002639 mApp, subId, callingPackage, "getVisualVoicemailPackageName")) {
Ta-wei Yendca928f2017-01-10 16:17:08 -08002640 return null;
2641 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002642
Jeff Davidsona8e4e242018-03-15 17:16:18 -07002643 final long identity = Binder.clearCallingIdentity();
2644 try {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08002645 return RemoteVvmTaskManager.getRemotePackage(mApp, subId).getPackageName();
Jeff Davidsona8e4e242018-03-15 17:16:18 -07002646 } finally {
2647 Binder.restoreCallingIdentity(identity);
2648 }
Ta-wei Yendca928f2017-01-10 16:17:08 -08002649 }
2650
2651 @Override
Ta-wei Yenb6929602016-05-24 15:48:27 -07002652 public void enableVisualVoicemailSmsFilter(String callingPackage, int subId,
2653 VisualVoicemailSmsFilterSettings settings) {
2654 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002655
2656 final long identity = Binder.clearCallingIdentity();
2657 try {
2658 VisualVoicemailSmsFilterConfig.enableVisualVoicemailSmsFilter(
Nazanin Bakhshif782e562018-12-11 15:15:39 -08002659 mApp, callingPackage, subId, settings);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002660 } finally {
2661 Binder.restoreCallingIdentity(identity);
2662 }
Ta-wei Yen87c49842016-05-13 21:19:52 -07002663 }
2664
2665 @Override
Ta-wei Yenb6929602016-05-24 15:48:27 -07002666 public void disableVisualVoicemailSmsFilter(String callingPackage, int subId) {
2667 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002668
2669 final long identity = Binder.clearCallingIdentity();
2670 try {
2671 VisualVoicemailSmsFilterConfig.disableVisualVoicemailSmsFilter(
Nazanin Bakhshif782e562018-12-11 15:15:39 -08002672 mApp, callingPackage, subId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002673 } finally {
2674 Binder.restoreCallingIdentity(identity);
2675 }
Ta-wei Yen87c49842016-05-13 21:19:52 -07002676 }
2677
2678 @Override
Ta-wei Yenb6929602016-05-24 15:48:27 -07002679 public VisualVoicemailSmsFilterSettings getVisualVoicemailSmsFilterSettings(
2680 String callingPackage, int subId) {
2681 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002682
2683 final long identity = Binder.clearCallingIdentity();
2684 try {
2685 return VisualVoicemailSmsFilterConfig.getVisualVoicemailSmsFilterSettings(
Nazanin Bakhshif782e562018-12-11 15:15:39 -08002686 mApp, callingPackage, subId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002687 } finally {
2688 Binder.restoreCallingIdentity(identity);
2689 }
Ta-wei Yen87c49842016-05-13 21:19:52 -07002690 }
2691
2692 @Override
Ta-wei Yen30a69c82016-12-27 14:52:32 -08002693 public VisualVoicemailSmsFilterSettings getActiveVisualVoicemailSmsFilterSettings(int subId) {
Brad Ebinger35c841c2018-10-01 10:40:55 -07002694 enforceReadPrivilegedPermission("getActiveVisualVoicemailSmsFilterSettings");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002695
2696 final long identity = Binder.clearCallingIdentity();
2697 try {
2698 return VisualVoicemailSmsFilterConfig.getActiveVisualVoicemailSmsFilterSettings(
Nazanin Bakhshif782e562018-12-11 15:15:39 -08002699 mApp, subId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002700 } finally {
2701 Binder.restoreCallingIdentity(identity);
2702 }
Ta-wei Yen30a69c82016-12-27 14:52:32 -08002703 }
2704
2705 @Override
2706 public void sendVisualVoicemailSmsForSubscriber(String callingPackage, int subId,
2707 String number, int port, String text, PendingIntent sentIntent) {
2708 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
Ta-wei Yen527a9c02017-01-06 15:29:25 -08002709 enforceVisualVoicemailPackage(callingPackage, subId);
Ta-wei Yen30a69c82016-12-27 14:52:32 -08002710 enforceSendSmsPermission();
Amit Mahajandccb3f12019-05-13 13:48:32 -07002711 SmsController smsController = PhoneFactory.getSmsController();
2712 smsController.sendVisualVoicemailSmsForSubscriber(callingPackage, subId, number, port, text,
2713 sentIntent);
Ta-wei Yen87c49842016-05-13 21:19:52 -07002714 }
Amit Mahajandccb3f12019-05-13 13:48:32 -07002715
Shishir Agrawal76d5da92014-11-09 16:17:25 -08002716 /**
fionaxu0152e512016-11-14 13:36:14 -08002717 * Sets the voice activation state of a given subId.
2718 */
2719 @Override
2720 public void setVoiceActivationState(int subId, int activationState) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08002721 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
2722 mApp, subId, "setVoiceActivationState");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002723
2724 final long identity = Binder.clearCallingIdentity();
2725 try {
2726 final Phone phone = getPhone(subId);
2727 if (phone != null) {
2728 phone.setVoiceActivationState(activationState);
2729 } else {
2730 loge("setVoiceActivationState fails with invalid subId: " + subId);
2731 }
2732 } finally {
2733 Binder.restoreCallingIdentity(identity);
fionaxu0152e512016-11-14 13:36:14 -08002734 }
2735 }
2736
2737 /**
2738 * Sets the data activation state of a given subId.
2739 */
2740 @Override
2741 public void setDataActivationState(int subId, int activationState) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08002742 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
2743 mApp, subId, "setDataActivationState");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002744
2745 final long identity = Binder.clearCallingIdentity();
2746 try {
2747 final Phone phone = getPhone(subId);
2748 if (phone != null) {
2749 phone.setDataActivationState(activationState);
2750 } else {
2751 loge("setVoiceActivationState fails with invalid subId: " + subId);
2752 }
2753 } finally {
2754 Binder.restoreCallingIdentity(identity);
fionaxu0152e512016-11-14 13:36:14 -08002755 }
2756 }
2757
2758 /**
2759 * Returns the voice activation state of a given subId.
2760 */
2761 @Override
2762 public int getVoiceActivationState(int subId, String callingPackage) {
Brad Ebinger35c841c2018-10-01 10:40:55 -07002763 enforceReadPrivilegedPermission("getVoiceActivationState");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002764
fionaxu0152e512016-11-14 13:36:14 -08002765 final Phone phone = getPhone(subId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002766 final long identity = Binder.clearCallingIdentity();
2767 try {
2768 if (phone != null) {
2769 return phone.getVoiceActivationState();
2770 } else {
2771 return TelephonyManager.SIM_ACTIVATION_STATE_UNKNOWN;
2772 }
2773 } finally {
2774 Binder.restoreCallingIdentity(identity);
fionaxu0152e512016-11-14 13:36:14 -08002775 }
2776 }
2777
2778 /**
2779 * Returns the data activation state of a given subId.
2780 */
2781 @Override
2782 public int getDataActivationState(int subId, String callingPackage) {
Brad Ebinger35c841c2018-10-01 10:40:55 -07002783 enforceReadPrivilegedPermission("getDataActivationState");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002784
fionaxu0152e512016-11-14 13:36:14 -08002785 final Phone phone = getPhone(subId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002786 final long identity = Binder.clearCallingIdentity();
2787 try {
2788 if (phone != null) {
2789 return phone.getDataActivationState();
2790 } else {
2791 return TelephonyManager.SIM_ACTIVATION_STATE_UNKNOWN;
2792 }
2793 } finally {
2794 Binder.restoreCallingIdentity(identity);
fionaxu0152e512016-11-14 13:36:14 -08002795 }
2796 }
2797
2798 /**
Wink Saville36469e72014-06-11 15:17:00 -07002799 * Returns the unread count of voicemails for a subId
2800 */
Sanket Padawe356d7632015-06-22 14:03:32 -07002801 @Override
Brad Ebingerf7664ba2018-11-29 12:43:38 -08002802 public int getVoiceMessageCountForSubscriber(int subId, String callingPackage) {
2803 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
2804 mApp, subId, callingPackage, "getVoiceMessageCountForSubscriber")) {
2805 return 0;
2806 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002807 final long identity = Binder.clearCallingIdentity();
2808 try {
2809 final Phone phone = getPhone(subId);
2810 if (phone != null) {
2811 return phone.getVoiceMessageCount();
2812 } else {
2813 return 0;
2814 }
2815 } finally {
2816 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07002817 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002818 }
2819
2820 /**
pkanwar8a4dcfb2017-01-19 13:43:16 -08002821 * returns true, if the device is in a state where both voice and data
2822 * are supported simultaneously. This can change based on location or network condition.
2823 */
2824 @Override
2825 public boolean isConcurrentVoiceAndDataAllowed(int subId) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002826 final long identity = Binder.clearCallingIdentity();
2827 try {
2828 final Phone phone = getPhone(subId);
2829 return (phone == null ? false : phone.isConcurrentVoiceAndDataAllowed());
2830 } finally {
2831 Binder.restoreCallingIdentity(identity);
2832 }
pkanwar8a4dcfb2017-01-19 13:43:16 -08002833 }
2834
2835 /**
fionaxu235cc5e2017-03-06 22:25:57 -08002836 * Send the dialer code if called from the current default dialer or the caller has
2837 * carrier privilege.
2838 * @param inputCode The dialer code to send
2839 */
2840 @Override
2841 public void sendDialerSpecialCode(String callingPackage, String inputCode) {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08002842 final Phone defaultPhone = getDefaultPhone();
fionaxu235cc5e2017-03-06 22:25:57 -08002843 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
Nazanin Bakhshif782e562018-12-11 15:15:39 -08002844 String defaultDialer = TelecomManager.from(defaultPhone.getContext())
2845 .getDefaultDialerPackage();
fionaxu235cc5e2017-03-06 22:25:57 -08002846 if (!TextUtils.equals(callingPackage, defaultDialer)) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08002847 TelephonyPermissions.enforceCallingOrSelfCarrierPrivilege(
2848 getDefaultSubscription(), "sendDialerSpecialCode");
fionaxu235cc5e2017-03-06 22:25:57 -08002849 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002850
2851 final long identity = Binder.clearCallingIdentity();
2852 try {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08002853 defaultPhone.sendDialerSpecialCode(inputCode);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002854 } finally {
2855 Binder.restoreCallingIdentity(identity);
2856 }
fionaxu235cc5e2017-03-06 22:25:57 -08002857 }
2858
Pengquan Menga1bb6272018-09-06 09:59:22 -07002859 @Override
2860 public int getNetworkSelectionMode(int subId) {
Pengquan Menge92a50d2018-09-21 15:54:48 -07002861 if (!isActiveSubscription(subId)) {
2862 return TelephonyManager.NETWORK_SELECTION_MODE_UNKNOWN;
2863 }
2864
Pengquan Menga1bb6272018-09-06 09:59:22 -07002865 return (int) sendRequest(CMD_GET_NETWORK_SELECTION_MODE, null /* argument */, subId);
2866 }
2867
Brad Ebinger35c841c2018-10-01 10:40:55 -07002868 @Override
Brad Ebingerb2b65522019-03-15 13:48:47 -07002869 public boolean isInEmergencySmsMode() {
2870 enforceReadPrivilegedPermission("isInEmergencySmsMode");
2871 final long identity = Binder.clearCallingIdentity();
2872 try {
2873 for (Phone phone : PhoneFactory.getPhones()) {
2874 if (phone.isInEmergencySmsMode()) {
2875 return true;
2876 }
2877 }
2878 } finally {
2879 Binder.restoreCallingIdentity(identity);
2880 }
2881 return false;
2882 }
2883
2884 @Override
Brad Ebinger9878b0b2018-11-08 17:43:22 -08002885 public void registerImsRegistrationCallback(int subId, IImsRegistrationCallback c)
2886 throws RemoteException {
2887 enforceReadPrivilegedPermission("registerImsRegistrationCallback");
Brad Ebinger35c841c2018-10-01 10:40:55 -07002888 final long token = Binder.clearCallingIdentity();
2889 try {
Brad Ebinger4ae57f92019-01-09 16:51:30 -08002890 // TODO: Refactor to remove ImsManager dependence and query through ImsPhone directly.
Nazanin Bakhshif782e562018-12-11 15:15:39 -08002891 ImsManager.getInstance(mApp, getSlotIndexOrException(subId))
Brad Ebinger35c841c2018-10-01 10:40:55 -07002892 .addRegistrationCallbackForSubscription(c, subId);
2893 } finally {
2894 Binder.restoreCallingIdentity(token);
2895 }
2896 }
2897
2898 @Override
Brad Ebinger9878b0b2018-11-08 17:43:22 -08002899 public void unregisterImsRegistrationCallback(int subId, IImsRegistrationCallback c) {
2900 enforceReadPrivilegedPermission("unregisterImsRegistrationCallback");
Brad Ebinger4ae57f92019-01-09 16:51:30 -08002901 if (!SubscriptionManager.isValidSubscriptionId(subId)) {
2902 throw new IllegalArgumentException("Invalid Subscription ID: " + subId);
2903 }
2904 Binder.withCleanCallingIdentity(() -> {
2905 try {
2906 // TODO: Refactor to remove ImsManager dependence and query through ImsPhone.
Nazanin Bakhshif782e562018-12-11 15:15:39 -08002907 ImsManager.getInstance(mApp, getSlotIndexOrException(subId))
Brad Ebinger4ae57f92019-01-09 16:51:30 -08002908 .removeRegistrationCallbackForSubscription(c, subId);
2909 } catch (IllegalArgumentException e) {
2910 Log.i(LOG_TAG, "unregisterImsRegistrationCallback: " + subId
2911 + "is inactive, ignoring unregister.");
2912 // If the subscription is no longer active, just return, since the callback
2913 // will already have been removed internally.
2914 }
2915 });
Brad Ebinger35c841c2018-10-01 10:40:55 -07002916 }
2917
2918 @Override
Brad Ebinger9878b0b2018-11-08 17:43:22 -08002919 public void registerMmTelCapabilityCallback(int subId, IImsCapabilityCallback c)
2920 throws RemoteException {
2921 enforceReadPrivilegedPermission("registerMmTelCapabilityCallback");
Brad Ebinger35c841c2018-10-01 10:40:55 -07002922 // TODO: Refactor to remove ImsManager dependence and query through ImsPhone directly.
2923 final long token = Binder.clearCallingIdentity();
2924 try {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08002925 ImsManager.getInstance(mApp, getSlotIndexOrException(subId))
Brad Ebinger35c841c2018-10-01 10:40:55 -07002926 .addCapabilitiesCallbackForSubscription(c, subId);
2927 } finally {
2928 Binder.restoreCallingIdentity(token);
2929 }
2930 }
2931
2932 @Override
Brad Ebinger9878b0b2018-11-08 17:43:22 -08002933 public void unregisterMmTelCapabilityCallback(int subId, IImsCapabilityCallback c) {
2934 enforceReadPrivilegedPermission("unregisterMmTelCapabilityCallback");
Brad Ebinger4ae57f92019-01-09 16:51:30 -08002935
2936 if (!SubscriptionManager.isValidSubscriptionId(subId)) {
2937 throw new IllegalArgumentException("Invalid Subscription ID: " + subId);
2938 }
2939 Binder.withCleanCallingIdentity(() -> {
2940 try {
2941 // TODO: Refactor to remove ImsManager dependence and query through ImsPhone.
Nazanin Bakhshif782e562018-12-11 15:15:39 -08002942 ImsManager.getInstance(mApp, getSlotIndexOrException(subId))
Brad Ebinger4ae57f92019-01-09 16:51:30 -08002943 .removeCapabilitiesCallbackForSubscription(c, subId);
2944 } catch (IllegalArgumentException e) {
2945 Log.i(LOG_TAG, "unregisterMmTelCapabilityCallback: " + subId
2946 + "is inactive, ignoring unregister.");
2947 // If the subscription is no longer active, just return, since the callback
2948 // will already have been removed internally.
2949 }
2950 });
Brad Ebinger35c841c2018-10-01 10:40:55 -07002951 }
2952
2953 @Override
Brad Ebinger9878b0b2018-11-08 17:43:22 -08002954 public boolean isCapable(int subId, int capability, int regTech) {
2955 enforceReadPrivilegedPermission("isCapable");
Brad Ebinger35c841c2018-10-01 10:40:55 -07002956 // TODO: Refactor to remove ImsManager dependence and query through ImsPhone directly.
2957 final long token = Binder.clearCallingIdentity();
2958 try {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08002959 return ImsManager.getInstance(mApp,
Brad Ebinger35c841c2018-10-01 10:40:55 -07002960 getSlotIndexOrException(subId)).queryMmTelCapability(capability, regTech);
2961 } catch (ImsException e) {
2962 Log.w(LOG_TAG, "IMS isCapable - service unavailable: " + e.getMessage());
2963 return false;
Brad Ebinger6b5ac222019-02-04 14:36:52 -08002964 } catch (IllegalArgumentException e) {
2965 Log.i(LOG_TAG, "isCapable: " + subId + " is inactive, returning false.");
2966 return false;
Brad Ebinger35c841c2018-10-01 10:40:55 -07002967 } finally {
2968 Binder.restoreCallingIdentity(token);
2969 }
2970 }
2971
2972 @Override
Brad Ebinger9878b0b2018-11-08 17:43:22 -08002973 public boolean isAvailable(int subId, int capability, int regTech) {
2974 enforceReadPrivilegedPermission("isAvailable");
Brad Ebinger35c841c2018-10-01 10:40:55 -07002975 final long token = Binder.clearCallingIdentity();
2976 try {
2977 Phone phone = getPhone(subId);
2978 if (phone == null) return false;
2979 return phone.isImsCapabilityAvailable(capability, regTech);
2980 } finally {
2981 Binder.restoreCallingIdentity(token);
2982 }
2983 }
2984
2985 @Override
2986 public boolean isAdvancedCallingSettingEnabled(int subId) {
2987 enforceReadPrivilegedPermission("enforceReadPrivilegedPermission");
2988 // TODO: Refactor to remove ImsManager dependence and query through ImsPhone directly.
2989 final long token = Binder.clearCallingIdentity();
2990 try {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08002991 return ImsManager.getInstance(mApp,
Brad Ebinger35c841c2018-10-01 10:40:55 -07002992 getSlotIndexOrException(subId)).isEnhanced4gLteModeSettingEnabledByUser();
2993 } finally {
2994 Binder.restoreCallingIdentity(token);
2995 }
2996 }
2997
2998 @Override
Brad Ebinger1c162042019-02-21 14:49:10 -08002999 public void setAdvancedCallingSettingEnabled(int subId, boolean isEnabled) {
Brad Ebinger35c841c2018-10-01 10:40:55 -07003000 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(mApp, subId,
Brad Ebinger1c162042019-02-21 14:49:10 -08003001 "setAdvancedCallingSettingEnabled");
Brad Ebinger35c841c2018-10-01 10:40:55 -07003002 final long identity = Binder.clearCallingIdentity();
3003 try {
3004 // TODO: Refactor to remove ImsManager dependence and query through ImsPhone directly.
Nazanin Bakhshif782e562018-12-11 15:15:39 -08003005 ImsManager.getInstance(mApp,
Brad Ebinger35c841c2018-10-01 10:40:55 -07003006 getSlotIndexOrException(subId)).setEnhanced4gLteModeSetting(isEnabled);
3007 } finally {
3008 Binder.restoreCallingIdentity(identity);
3009 }
3010 }
3011
3012 @Override
Brad Ebinger9878b0b2018-11-08 17:43:22 -08003013 public boolean isVtSettingEnabled(int subId) {
3014 enforceReadPrivilegedPermission("isVtSettingEnabled");
Brad Ebinger35c841c2018-10-01 10:40:55 -07003015 final long identity = Binder.clearCallingIdentity();
3016 try {
3017 // TODO: Refactor to remove ImsManager dependence and query through ImsPhone directly.
Nazanin Bakhshif782e562018-12-11 15:15:39 -08003018 return ImsManager.getInstance(mApp,
Brad Ebinger35c841c2018-10-01 10:40:55 -07003019 getSlotIndexOrException(subId)).isVtEnabledByUser();
3020 } finally {
3021 Binder.restoreCallingIdentity(identity);
3022 }
3023 }
3024
3025 @Override
Brad Ebinger1c162042019-02-21 14:49:10 -08003026 public void setVtSettingEnabled(int subId, boolean isEnabled) {
Brad Ebinger35c841c2018-10-01 10:40:55 -07003027 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(mApp, subId,
Brad Ebinger1c162042019-02-21 14:49:10 -08003028 "setVtSettingEnabled");
Brad Ebinger35c841c2018-10-01 10:40:55 -07003029 final long identity = Binder.clearCallingIdentity();
3030 try {
3031 // TODO: Refactor to remove ImsManager dependence and query through ImsPhone directly.
Nazanin Bakhshif782e562018-12-11 15:15:39 -08003032 ImsManager.getInstance(mApp, getSlotIndexOrException(subId)).setVtSetting(isEnabled);
Brad Ebinger35c841c2018-10-01 10:40:55 -07003033 } finally {
3034 Binder.restoreCallingIdentity(identity);
3035 }
3036 }
3037
3038 @Override
3039 public boolean isVoWiFiSettingEnabled(int subId) {
3040 enforceReadPrivilegedPermission("isVoWiFiSettingEnabled");
3041 final long identity = Binder.clearCallingIdentity();
3042 try {
3043 // TODO: Refactor to remove ImsManager dependence and query through ImsPhone directly.
Nazanin Bakhshif782e562018-12-11 15:15:39 -08003044 return ImsManager.getInstance(mApp,
Brad Ebinger35c841c2018-10-01 10:40:55 -07003045 getSlotIndexOrException(subId)).isWfcEnabledByUser();
3046 } finally {
3047 Binder.restoreCallingIdentity(identity);
3048 }
3049 }
3050
3051 @Override
Brad Ebinger1c162042019-02-21 14:49:10 -08003052 public void setVoWiFiSettingEnabled(int subId, boolean isEnabled) {
Brad Ebinger35c841c2018-10-01 10:40:55 -07003053 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(mApp, subId,
Brad Ebinger1c162042019-02-21 14:49:10 -08003054 "setVoWiFiSettingEnabled");
Brad Ebinger35c841c2018-10-01 10:40:55 -07003055 final long identity = Binder.clearCallingIdentity();
3056 try {
3057 // TODO: Refactor to remove ImsManager dependence and query through ImsPhone directly.
Nazanin Bakhshif782e562018-12-11 15:15:39 -08003058 ImsManager.getInstance(mApp, getSlotIndexOrException(subId)).setWfcSetting(isEnabled);
Brad Ebinger35c841c2018-10-01 10:40:55 -07003059 } finally {
3060 Binder.restoreCallingIdentity(identity);
3061 }
3062 }
3063
3064 @Override
3065 public boolean isVoWiFiRoamingSettingEnabled(int subId) {
3066 enforceReadPrivilegedPermission("isVoWiFiRoamingSettingEnabled");
3067 final long identity = Binder.clearCallingIdentity();
3068 try {
3069 // TODO: Refactor to remove ImsManager dependence and query through ImsPhone directly.
Nazanin Bakhshif782e562018-12-11 15:15:39 -08003070 return ImsManager.getInstance(mApp,
Brad Ebinger35c841c2018-10-01 10:40:55 -07003071 getSlotIndexOrException(subId)).isWfcRoamingEnabledByUser();
3072 } finally {
3073 Binder.restoreCallingIdentity(identity);
3074 }
3075 }
3076
3077 @Override
Brad Ebinger1c162042019-02-21 14:49:10 -08003078 public void setVoWiFiRoamingSettingEnabled(int subId, boolean isEnabled) {
Brad Ebinger35c841c2018-10-01 10:40:55 -07003079 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(mApp, subId,
Brad Ebinger1c162042019-02-21 14:49:10 -08003080 "setVoWiFiRoamingSettingEnabled");
Brad Ebinger35c841c2018-10-01 10:40:55 -07003081 final long identity = Binder.clearCallingIdentity();
3082 try {
3083 // TODO: Refactor to remove ImsManager dependence and query through ImsPhone directly.
Nazanin Bakhshif782e562018-12-11 15:15:39 -08003084 ImsManager.getInstance(mApp,
Brad Ebinger35c841c2018-10-01 10:40:55 -07003085 getSlotIndexOrException(subId)).setWfcRoamingSetting(isEnabled);
3086 } finally {
3087 Binder.restoreCallingIdentity(identity);
3088 }
3089 }
3090
3091 @Override
3092 public void setVoWiFiNonPersistent(int subId, boolean isCapable, int mode) {
3093 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(mApp, subId,
3094 "setVoWiFiNonPersistent");
3095 final long identity = Binder.clearCallingIdentity();
3096 try {
3097 // TODO: Refactor to remove ImsManager dependence and query through ImsPhone directly.
Nazanin Bakhshif782e562018-12-11 15:15:39 -08003098 ImsManager.getInstance(mApp,
Brad Ebinger2d29c012019-05-07 18:33:46 -07003099 getSlotIndexOrException(subId)).setWfcNonPersistent(isCapable, mode);
Brad Ebinger35c841c2018-10-01 10:40:55 -07003100 } finally {
3101 Binder.restoreCallingIdentity(identity);
3102 }
3103 }
3104
3105 @Override
3106 public int getVoWiFiModeSetting(int subId) {
3107 enforceReadPrivilegedPermission("getVoWiFiModeSetting");
3108 final long identity = Binder.clearCallingIdentity();
3109 try {
3110 // TODO: Refactor to remove ImsManager dependence and query through ImsPhone directly.
Nazanin Bakhshif782e562018-12-11 15:15:39 -08003111 return ImsManager.getInstance(mApp,
Brad Ebinger35c841c2018-10-01 10:40:55 -07003112 getSlotIndexOrException(subId)).getWfcMode(false /*isRoaming*/);
3113 } finally {
3114 Binder.restoreCallingIdentity(identity);
3115 }
3116 }
3117
3118 @Override
3119 public void setVoWiFiModeSetting(int subId, int mode) {
3120 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(mApp, subId,
3121 "setVoWiFiModeSetting");
3122 final long identity = Binder.clearCallingIdentity();
3123 try {
3124 // TODO: Refactor to remove ImsManager dependence and query through ImsPhone directly.
Nazanin Bakhshif782e562018-12-11 15:15:39 -08003125 ImsManager.getInstance(mApp,
Brad Ebinger35c841c2018-10-01 10:40:55 -07003126 getSlotIndexOrException(subId)).setWfcMode(mode, false /*isRoaming*/);
3127 } finally {
3128 Binder.restoreCallingIdentity(identity);
3129 }
3130 }
3131
3132 @Override
3133 public int getVoWiFiRoamingModeSetting(int subId) {
3134 enforceReadPrivilegedPermission("getVoWiFiRoamingModeSetting");
3135 final long identity = Binder.clearCallingIdentity();
3136 try {
3137 // TODO: Refactor to remove ImsManager dependence and query through ImsPhone directly.
Nazanin Bakhshif782e562018-12-11 15:15:39 -08003138 return ImsManager.getInstance(mApp,
Brad Ebinger35c841c2018-10-01 10:40:55 -07003139 getSlotIndexOrException(subId)).getWfcMode(true /*isRoaming*/);
3140 } finally {
3141 Binder.restoreCallingIdentity(identity);
3142 }
3143 }
3144
3145 @Override
3146 public void setVoWiFiRoamingModeSetting(int subId, int mode) {
3147 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(mApp, subId,
3148 "setVoWiFiRoamingModeSetting");
3149 final long identity = Binder.clearCallingIdentity();
3150 try {
3151 // TODO: Refactor to remove ImsManager dependence and query through ImsPhone directly.
Nazanin Bakhshif782e562018-12-11 15:15:39 -08003152 ImsManager.getInstance(mApp,
Brad Ebinger35c841c2018-10-01 10:40:55 -07003153 getSlotIndexOrException(subId)).setWfcMode(mode, true /*isRoaming*/);
3154 } finally {
3155 Binder.restoreCallingIdentity(identity);
3156 }
3157 }
3158
3159 @Override
3160 public void setRttCapabilitySetting(int subId, boolean isEnabled) {
3161 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(mApp, subId,
3162 "setRttCapabilityEnabled");
3163 final long identity = Binder.clearCallingIdentity();
3164 try {
3165 // TODO: Refactor to remove ImsManager dependence and query through ImsPhone directly.
Nazanin Bakhshif782e562018-12-11 15:15:39 -08003166 ImsManager.getInstance(mApp,
Brad Ebinger35c841c2018-10-01 10:40:55 -07003167 getSlotIndexOrException(subId)).setRttEnabled(isEnabled);
3168 } finally {
3169 Binder.restoreCallingIdentity(identity);
3170 }
3171 }
3172
3173 @Override
3174 public boolean isTtyOverVolteEnabled(int subId) {
3175 enforceReadPrivilegedPermission("isTtyOverVolteEnabled");
3176 final long identity = Binder.clearCallingIdentity();
3177 try {
3178 // TODO: Refactor to remove ImsManager dependence and query through ImsPhone directly.
Nazanin Bakhshif782e562018-12-11 15:15:39 -08003179 return ImsManager.getInstance(mApp,
Brad Ebinger35c841c2018-10-01 10:40:55 -07003180 getSlotIndexOrException(subId)).isTtyOnVoLteCapable();
3181 } finally {
3182 Binder.restoreCallingIdentity(identity);
3183 }
3184 }
3185
Brad Ebingerdf5b4f02018-10-31 11:24:17 -07003186 @Override
3187 public void registerImsProvisioningChangedCallback(int subId, IImsConfigCallback callback) {
3188 enforceReadPrivilegedPermission("registerImsProvisioningChangedCallback");
3189 final long identity = Binder.clearCallingIdentity();
3190 try {
3191 // TODO: Refactor to remove ImsManager dependence and query through ImsPhone directly.
Nazanin Bakhshif782e562018-12-11 15:15:39 -08003192 ImsManager.getInstance(mApp, getSlotIndexOrException(subId))
Brad Ebinger4ae57f92019-01-09 16:51:30 -08003193 .addProvisioningCallbackForSubscription(callback, subId);
Brad Ebingerdf5b4f02018-10-31 11:24:17 -07003194 } finally {
3195 Binder.restoreCallingIdentity(identity);
3196 }
3197 }
3198
3199 @Override
3200 public void unregisterImsProvisioningChangedCallback(int subId, IImsConfigCallback callback) {
3201 enforceReadPrivilegedPermission("unregisterImsProvisioningChangedCallback");
3202 final long identity = Binder.clearCallingIdentity();
Brad Ebinger4ae57f92019-01-09 16:51:30 -08003203 if (!SubscriptionManager.isValidSubscriptionId(subId)) {
3204 throw new IllegalArgumentException("Invalid Subscription ID: " + subId);
3205 }
Brad Ebingerdf5b4f02018-10-31 11:24:17 -07003206 try {
3207 // TODO: Refactor to remove ImsManager dependence and query through ImsPhone directly.
Nazanin Bakhshif782e562018-12-11 15:15:39 -08003208 ImsManager.getInstance(mApp, getSlotIndexOrException(subId))
Brad Ebinger4ae57f92019-01-09 16:51:30 -08003209 .removeProvisioningCallbackForSubscription(callback, subId);
3210 } catch (IllegalArgumentException e) {
3211 Log.i(LOG_TAG, "unregisterImsProvisioningChangedCallback: " + subId
3212 + "is inactive, ignoring unregister.");
3213 // If the subscription is no longer active, just return, since the callback will already
3214 // have been removed internally.
Brad Ebingerdf5b4f02018-10-31 11:24:17 -07003215 } finally {
3216 Binder.restoreCallingIdentity(identity);
3217 }
3218 }
3219
3220 @Override
Brad Ebinger1c8542e2019-01-14 13:43:14 -08003221 public void setImsProvisioningStatusForCapability(int subId, int capability, int tech,
3222 boolean isProvisioned) {
3223 if (tech != ImsRegistrationImplBase.REGISTRATION_TECH_IWLAN
3224 && tech != ImsRegistrationImplBase.REGISTRATION_TECH_LTE) {
3225 throw new IllegalArgumentException("Registration technology '" + tech + "' is invalid");
3226 }
3227 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(mApp, subId,
3228 "setProvisioningStatusForCapability");
3229 final long identity = Binder.clearCallingIdentity();
3230 try {
3231 // TODO: Refactor to remove ImsManager dependence and query through ImsPhone directly.
3232 Phone phone = getPhone(subId);
3233 if (phone == null) {
3234 loge("setImsProvisioningStatusForCapability: phone instance null for subid "
3235 + subId);
3236 return;
3237 }
3238 if (!doesImsCapabilityRequireProvisioning(phone.getContext(), subId, capability)) {
3239 return;
3240 }
3241
3242 // this capability requires provisioning, route to the correct API.
3243 ImsManager ims = ImsManager.getInstance(mApp, getSlotIndex(subId));
3244 switch (capability) {
3245 case MmTelFeature.MmTelCapabilities.CAPABILITY_TYPE_VOICE: {
3246 if (tech == ImsRegistrationImplBase.REGISTRATION_TECH_LTE) {
3247 ims.setVolteProvisioned(isProvisioned);
3248 } else if (tech == ImsRegistrationImplBase.REGISTRATION_TECH_IWLAN) {
3249 ims.setWfcProvisioned(isProvisioned);
3250 }
3251 break;
3252 }
3253 case MmTelFeature.MmTelCapabilities.CAPABILITY_TYPE_VIDEO: {
3254 // There is currently no difference in VT provisioning type.
3255 ims.setVtProvisioned(isProvisioned);
3256 break;
3257 }
3258 case MmTelFeature.MmTelCapabilities.CAPABILITY_TYPE_UT: {
3259 // There is no "deprecated" UT provisioning mechanism through ImsConfig, so
3260 // change the capability of the feature instead if needed.
3261 if (isMmTelCapabilityProvisionedInCache(subId, capability, tech)
3262 == isProvisioned) {
3263 // No change in provisioning.
3264 return;
3265 }
3266 cacheMmTelCapabilityProvisioning(subId, capability, tech, isProvisioned);
3267 try {
3268 ims.changeMmTelCapability(capability, tech, isProvisioned);
3269 } catch (ImsException e) {
3270 loge("setImsProvisioningStatusForCapability: couldn't change UT capability"
3271 + ", Exception" + e.getMessage());
3272 }
3273 break;
3274 }
3275 default: {
3276 throw new IllegalArgumentException("Tried to set provisioning for capability '"
3277 + capability + "', which does not require provisioning.");
3278 }
3279 }
3280
3281 } finally {
3282 Binder.restoreCallingIdentity(identity);
3283 }
3284 }
3285
3286 @Override
3287 public boolean getImsProvisioningStatusForCapability(int subId, int capability, int tech) {
3288 if (tech != ImsRegistrationImplBase.REGISTRATION_TECH_IWLAN
3289 && tech != ImsRegistrationImplBase.REGISTRATION_TECH_LTE) {
3290 throw new IllegalArgumentException("Registration technology '" + tech + "' is invalid");
3291 }
3292 enforceReadPrivilegedPermission("getProvisioningStatusForCapability");
3293 final long identity = Binder.clearCallingIdentity();
3294 try {
3295 // TODO: Refactor to remove ImsManager dependence and query through ImsPhone directly.
3296 Phone phone = getPhone(subId);
3297 if (phone == null) {
3298 loge("getImsProvisioningStatusForCapability: phone instance null for subid "
3299 + subId);
3300 // We will fail with "true" as the provisioning status because this is the default
3301 // if we do not require provisioning.
3302 return true;
3303 }
3304
3305 if (!doesImsCapabilityRequireProvisioning(phone.getContext(), subId, capability)) {
3306 return true;
3307 }
3308
3309 ImsManager ims = ImsManager.getInstance(mApp, getSlotIndex(subId));
3310 switch (capability) {
3311 case MmTelFeature.MmTelCapabilities.CAPABILITY_TYPE_VOICE: {
3312 if (tech == ImsRegistrationImplBase.REGISTRATION_TECH_LTE) {
3313 return ims.isVolteProvisionedOnDevice();
3314 } else if (tech == ImsRegistrationImplBase.REGISTRATION_TECH_IWLAN) {
3315 return ims.isWfcProvisionedOnDevice();
3316 }
3317 // This should never happen, since we are checking tech above to make sure it
3318 // is either LTE or IWLAN.
3319 throw new IllegalArgumentException("Invalid radio technology for voice "
3320 + "capability.");
3321 }
3322 case MmTelFeature.MmTelCapabilities.CAPABILITY_TYPE_VIDEO: {
3323 // There is currently no difference in VT provisioning type.
3324 return ims.isVtProvisionedOnDevice();
3325 }
3326 case MmTelFeature.MmTelCapabilities.CAPABILITY_TYPE_UT: {
3327 // There is no "deprecated" UT provisioning mechanism, so get from shared prefs.
3328 return isMmTelCapabilityProvisionedInCache(subId, capability, tech);
3329 }
3330 default: {
3331 throw new IllegalArgumentException("Tried to get provisioning for capability '"
3332 + capability + "', which does not require provisioning.");
3333 }
3334 }
3335
3336 } finally {
3337 Binder.restoreCallingIdentity(identity);
3338 }
3339 }
3340
3341 @Override
3342 public boolean isMmTelCapabilityProvisionedInCache(int subId, int capability, int tech) {
3343 if (tech != ImsRegistrationImplBase.REGISTRATION_TECH_IWLAN
3344 && tech != ImsRegistrationImplBase.REGISTRATION_TECH_LTE) {
3345 throw new IllegalArgumentException("Registration technology '" + tech + "' is invalid");
3346 }
3347 enforceReadPrivilegedPermission("isMmTelCapabilityProvisionedInCache");
3348 int provisionedBits = getMmTelCapabilityProvisioningBitfield(subId, tech);
3349 return (provisionedBits & capability) > 0;
3350 }
3351
3352 @Override
3353 public void cacheMmTelCapabilityProvisioning(int subId, int capability, int tech,
3354 boolean isProvisioned) {
3355 if (tech != ImsRegistrationImplBase.REGISTRATION_TECH_IWLAN
3356 && tech != ImsRegistrationImplBase.REGISTRATION_TECH_LTE) {
3357 throw new IllegalArgumentException("Registration technology '" + tech + "' is invalid");
3358 }
3359 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(mApp, subId,
3360 "setProvisioningStatusForCapability");
3361 int provisionedBits = getMmTelCapabilityProvisioningBitfield(subId, tech);
3362 // If the current provisioning status for capability already matches isProvisioned,
3363 // do nothing.
3364 if (((provisionedBits & capability) > 0) == isProvisioned) {
3365 return;
3366 }
3367 if (isProvisioned) {
3368 setMmTelCapabilityProvisioningBitfield(subId, tech, (provisionedBits | capability));
3369 } else {
3370 setMmTelCapabilityProvisioningBitfield(subId, tech, (provisionedBits & ~capability));
3371 }
3372 }
3373
3374 /**
3375 * @return the bitfield containing the MmTel provisioning for the provided subscription and
3376 * technology. The bitfield should mirror the bitfield defined by
3377 * {@link MmTelFeature.MmTelCapabilities.MmTelCapability}.
3378 */
3379 private int getMmTelCapabilityProvisioningBitfield(int subId, int tech) {
3380 String key = getMmTelProvisioningKey(subId, tech);
3381 // Default is no capabilities are provisioned.
3382 return mTelephonySharedPreferences.getInt(key, 0 /*default*/);
3383 }
3384
3385 /**
3386 * Sets the MmTel capability provisioning bitfield (defined by
3387 * {@link MmTelFeature.MmTelCapabilities.MmTelCapability}) for the subscription and
3388 * technology specified.
3389 *
3390 * Note: This is a synchronous command and should not be called on UI thread.
3391 */
3392 private void setMmTelCapabilityProvisioningBitfield(int subId, int tech, int newField) {
3393 final SharedPreferences.Editor editor = mTelephonySharedPreferences.edit();
3394 String key = getMmTelProvisioningKey(subId, tech);
3395 editor.putInt(key, newField);
3396 editor.commit();
3397 }
3398
3399 private static String getMmTelProvisioningKey(int subId, int tech) {
3400 // resulting key is provision_ims_mmtel_{subId}_{tech}
3401 return PREF_PROVISION_IMS_MMTEL_PREFIX + subId + "_" + tech;
3402 }
3403
3404 /**
3405 * Query CarrierConfig to see if the specified capability requires provisioning for the
3406 * carrier associated with the subscription id.
3407 */
3408 private boolean doesImsCapabilityRequireProvisioning(Context context, int subId,
3409 int capability) {
3410 CarrierConfigManager configManager = new CarrierConfigManager(context);
3411 PersistableBundle c = configManager.getConfigForSubId(subId);
3412 boolean requireUtProvisioning = c.getBoolean(
3413 // By default, this config is true (even if there is no SIM). We also check to make
3414 // sure the subscription needs provisioning here, so we do not need to check for
3415 // the no-SIM case, where we would normally shortcut this to false.
3416 CarrierConfigManager.KEY_CARRIER_SUPPORTS_SS_OVER_UT_BOOL, true)
3417 && c.getBoolean(CarrierConfigManager.KEY_CARRIER_UT_PROVISIONING_REQUIRED_BOOL,
3418 false);
3419 boolean requireVoiceVtProvisioning = c.getBoolean(
3420 CarrierConfigManager.KEY_CARRIER_VOLTE_PROVISIONING_REQUIRED_BOOL, false);
3421
3422 // First check to make sure that the capability requires provisioning.
3423 switch (capability) {
3424 case MmTelFeature.MmTelCapabilities.CAPABILITY_TYPE_VOICE:
3425 // intentional fallthrough
3426 case MmTelFeature.MmTelCapabilities.CAPABILITY_TYPE_VIDEO: {
3427 if (requireVoiceVtProvisioning) {
3428 // Voice and Video requires provisioning
3429 return true;
3430 }
3431 break;
3432 }
3433 case MmTelFeature.MmTelCapabilities.CAPABILITY_TYPE_UT: {
3434 if (requireUtProvisioning) {
3435 // UT requires provisioning
3436 return true;
3437 }
3438 break;
3439 }
3440 }
3441 return false;
3442 }
3443
3444 @Override
Brad Ebingerdf5b4f02018-10-31 11:24:17 -07003445 public int getImsProvisioningInt(int subId, int key) {
Brad Ebinger1c8542e2019-01-14 13:43:14 -08003446 if (!SubscriptionManager.isValidSubscriptionId(subId)) {
3447 throw new IllegalArgumentException("Invalid Subscription id '" + subId + "'");
3448 }
Brad Ebingerdf5b4f02018-10-31 11:24:17 -07003449 enforceReadPrivilegedPermission("getImsProvisioningInt");
3450 final long identity = Binder.clearCallingIdentity();
3451 try {
3452 // TODO: Refactor to remove ImsManager dependence and query through ImsPhone directly.
Brad Ebinger1c8542e2019-01-14 13:43:14 -08003453 int slotId = getSlotIndex(subId);
3454 if (slotId <= SubscriptionManager.INVALID_SIM_SLOT_INDEX) {
3455 Log.w(LOG_TAG, "getImsProvisioningInt: called with an inactive subscription '"
3456 + subId + "' for key:" + key);
3457 return ImsConfigImplBase.CONFIG_RESULT_UNKNOWN;
3458 }
3459 return ImsManager.getInstance(mApp, slotId).getConfigInterface().getConfigInt(key);
Brad Ebingerdf5b4f02018-10-31 11:24:17 -07003460 } catch (ImsException e) {
Brad Ebinger1c8542e2019-01-14 13:43:14 -08003461 Log.w(LOG_TAG, "getImsProvisioningInt: ImsService is not available for subscription '"
3462 + subId + "' for key:" + key);
3463 return ImsConfigImplBase.CONFIG_RESULT_UNKNOWN;
Brad Ebingerdf5b4f02018-10-31 11:24:17 -07003464 } finally {
3465 Binder.restoreCallingIdentity(identity);
3466 }
3467 }
3468
3469 @Override
3470 public String getImsProvisioningString(int subId, int key) {
Brad Ebinger1c8542e2019-01-14 13:43:14 -08003471 if (!SubscriptionManager.isValidSubscriptionId(subId)) {
3472 throw new IllegalArgumentException("Invalid Subscription id '" + subId + "'");
3473 }
Brad Ebingerdf5b4f02018-10-31 11:24:17 -07003474 enforceReadPrivilegedPermission("getImsProvisioningString");
3475 final long identity = Binder.clearCallingIdentity();
3476 try {
3477 // TODO: Refactor to remove ImsManager dependence and query through ImsPhone directly.
Brad Ebinger1c8542e2019-01-14 13:43:14 -08003478 int slotId = getSlotIndex(subId);
3479 if (slotId <= SubscriptionManager.INVALID_SIM_SLOT_INDEX) {
3480 Log.w(LOG_TAG, "getImsProvisioningString: called for an inactive subscription id '"
3481 + subId + "' for key:" + key);
3482 return ProvisioningManager.STRING_QUERY_RESULT_ERROR_GENERIC;
3483 }
3484 return ImsManager.getInstance(mApp, slotId).getConfigInterface().getConfigString(key);
Brad Ebingerdf5b4f02018-10-31 11:24:17 -07003485 } catch (ImsException e) {
Brad Ebinger1c8542e2019-01-14 13:43:14 -08003486 Log.w(LOG_TAG, "getImsProvisioningString: ImsService is not available for sub '"
3487 + subId + "' for key:" + key);
3488 return ProvisioningManager.STRING_QUERY_RESULT_ERROR_NOT_READY;
Brad Ebingerdf5b4f02018-10-31 11:24:17 -07003489 } finally {
3490 Binder.restoreCallingIdentity(identity);
3491 }
3492 }
3493
3494 @Override
3495 public int setImsProvisioningInt(int subId, int key, int value) {
Brad Ebinger1c8542e2019-01-14 13:43:14 -08003496 if (!SubscriptionManager.isValidSubscriptionId(subId)) {
3497 throw new IllegalArgumentException("Invalid Subscription id '" + subId + "'");
3498 }
Brad Ebinger3d0b34e2018-11-15 14:13:12 -08003499 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(mApp, subId,
3500 "setImsProvisioningInt");
Brad Ebingerdf5b4f02018-10-31 11:24:17 -07003501 final long identity = Binder.clearCallingIdentity();
3502 try {
3503 // TODO: Refactor to remove ImsManager dependence and query through ImsPhone directly.
Brad Ebinger1c8542e2019-01-14 13:43:14 -08003504 int slotId = getSlotIndex(subId);
3505 if (slotId <= SubscriptionManager.INVALID_SIM_SLOT_INDEX) {
3506 Log.w(LOG_TAG, "setImsProvisioningInt: called with an inactive subscription id '"
3507 + subId + "' for key:" + key);
3508 return ImsConfigImplBase.CONFIG_RESULT_FAILED;
3509 }
3510 return ImsManager.getInstance(mApp, slotId).getConfigInterface().setConfig(key, value);
Brad Ebingerdf5b4f02018-10-31 11:24:17 -07003511 } catch (ImsException e) {
Brad Ebinger1c8542e2019-01-14 13:43:14 -08003512 Log.w(LOG_TAG, "setImsProvisioningInt: ImsService unavailable for sub '" + subId
3513 + "' for key:" + key);
3514 return ImsConfigImplBase.CONFIG_RESULT_FAILED;
Brad Ebingerdf5b4f02018-10-31 11:24:17 -07003515 } finally {
3516 Binder.restoreCallingIdentity(identity);
3517 }
3518 }
3519
3520 @Override
3521 public int setImsProvisioningString(int subId, int key, String value) {
Brad Ebinger1c8542e2019-01-14 13:43:14 -08003522 if (!SubscriptionManager.isValidSubscriptionId(subId)) {
3523 throw new IllegalArgumentException("Invalid Subscription id '" + subId + "'");
3524 }
Brad Ebinger3d0b34e2018-11-15 14:13:12 -08003525 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(mApp, subId,
3526 "setImsProvisioningString");
Brad Ebingerdf5b4f02018-10-31 11:24:17 -07003527 final long identity = Binder.clearCallingIdentity();
3528 try {
3529 // TODO: Refactor to remove ImsManager dependence and query through ImsPhone directly.
Brad Ebinger1c8542e2019-01-14 13:43:14 -08003530 int slotId = getSlotIndex(subId);
3531 if (slotId <= SubscriptionManager.INVALID_SIM_SLOT_INDEX) {
3532 Log.w(LOG_TAG, "setImsProvisioningString: called with an inactive subscription id '"
3533 + subId + "' for key:" + key);
3534 return ImsConfigImplBase.CONFIG_RESULT_FAILED;
3535 }
3536 return ImsManager.getInstance(mApp, slotId).getConfigInterface().setConfig(key, value);
Brad Ebingerdf5b4f02018-10-31 11:24:17 -07003537 } catch (ImsException e) {
Brad Ebinger1c8542e2019-01-14 13:43:14 -08003538 Log.w(LOG_TAG, "setImsProvisioningString: ImsService unavailable for sub '" + subId
3539 + "' for key:" + key);
3540 return ImsConfigImplBase.CONFIG_RESULT_FAILED;
Brad Ebingerdf5b4f02018-10-31 11:24:17 -07003541 } finally {
3542 Binder.restoreCallingIdentity(identity);
3543 }
3544 }
3545
Brad Ebinger35c841c2018-10-01 10:40:55 -07003546 private int getSlotIndexOrException(int subId) throws IllegalArgumentException {
3547 int slotId = SubscriptionManager.getSlotIndex(subId);
3548 if (!SubscriptionManager.isValidSlotIndex(slotId)) {
Brad Ebinger6b5ac222019-02-04 14:36:52 -08003549 throw new IllegalArgumentException("Invalid Subscription Id, subId=" + subId);
Brad Ebinger35c841c2018-10-01 10:40:55 -07003550 }
3551 return slotId;
3552 }
3553
Brad Ebinger1c8542e2019-01-14 13:43:14 -08003554 private int getSlotIndex(int subId) {
3555 int slotId = SubscriptionManager.getSlotIndex(subId);
3556 if (!SubscriptionManager.isValidSlotIndex(slotId)) {
3557 return SubscriptionManager.INVALID_SIM_SLOT_INDEX;
3558 }
3559 return slotId;
3560 }
3561
Wink Saville36469e72014-06-11 15:17:00 -07003562 /**
Nathan Harold9042f0b2019-05-21 15:51:27 -07003563 * Returns the data network type for a subId; does not throw SecurityException.
Wink Saville36469e72014-06-11 15:17:00 -07003564 */
3565 @Override
Robert Greenwalt36b23af2015-07-06 17:59:14 -07003566 public int getNetworkTypeForSubscriber(int subId, String callingPackage) {
Nathan Harold9042f0b2019-05-21 15:51:27 -07003567 if (getTargetSdk(callingPackage) >= android.os.Build.VERSION_CODES.Q
3568 && !TelephonyPermissions.checkCallingOrSelfReadPhoneStateNoThrow(
3569 mApp, subId, callingPackage, "getNetworkTypeForSubscriber")) {
Robert Greenwalt36b23af2015-07-06 17:59:14 -07003570 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
3571 }
Robert Greenwalta5dcfcb2015-07-10 09:06:29 -07003572
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003573 final long identity = Binder.clearCallingIdentity();
3574 try {
3575 final Phone phone = getPhone(subId);
3576 if (phone != null) {
3577 return phone.getServiceState().getDataNetworkType();
3578 } else {
3579 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
3580 }
3581 } finally {
3582 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07003583 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003584 }
3585
3586 /**
3587 * Returns the data network type
3588 */
3589 @Override
Robert Greenwalt36b23af2015-07-06 17:59:14 -07003590 public int getDataNetworkType(String callingPackage) {
3591 return getDataNetworkTypeForSubscriber(getDefaultSubscription(), callingPackage);
Wink Saville36469e72014-06-11 15:17:00 -07003592 }
3593
3594 /**
3595 * Returns the data network type for a subId
3596 */
3597 @Override
Robert Greenwalt36b23af2015-07-06 17:59:14 -07003598 public int getDataNetworkTypeForSubscriber(int subId, String callingPackage) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08003599 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
Jeff Davidson913390f2018-02-23 17:11:49 -08003600 mApp, subId, callingPackage, "getDataNetworkTypeForSubscriber")) {
Robert Greenwalt36b23af2015-07-06 17:59:14 -07003601 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
3602 }
3603
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003604 final long identity = Binder.clearCallingIdentity();
3605 try {
3606 final Phone phone = getPhone(subId);
3607 if (phone != null) {
3608 return phone.getServiceState().getDataNetworkType();
3609 } else {
3610 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
3611 }
3612 } finally {
3613 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07003614 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003615 }
3616
3617 /**
Wink Saville36469e72014-06-11 15:17:00 -07003618 * Returns the Voice network type for a subId
3619 */
3620 @Override
Robert Greenwalta5dcfcb2015-07-10 09:06:29 -07003621 public int getVoiceNetworkTypeForSubscriber(int subId, String callingPackage) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08003622 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
Jeff Davidson913390f2018-02-23 17:11:49 -08003623 mApp, subId, callingPackage, "getDataNetworkTypeForSubscriber")) {
Robert Greenwalta5dcfcb2015-07-10 09:06:29 -07003624 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
3625 }
3626
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003627 final long identity = Binder.clearCallingIdentity();
3628 try {
3629 final Phone phone = getPhone(subId);
3630 if (phone != null) {
3631 return phone.getServiceState().getVoiceNetworkType();
3632 } else {
3633 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
3634 }
3635 } finally {
3636 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07003637 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003638 }
3639
3640 /**
3641 * @return true if a ICC card is present
3642 */
3643 public boolean hasIccCard() {
Wink Saville36469e72014-06-11 15:17:00 -07003644 // FIXME Make changes to pass defaultSimId of type int
Sanket Padawe13bac7b2017-03-20 15:04:47 -07003645 return hasIccCardUsingSlotIndex(mSubscriptionController.getSlotIndex(
3646 getDefaultSubscription()));
Wink Saville36469e72014-06-11 15:17:00 -07003647 }
3648
3649 /**
Sanket Padawe13bac7b2017-03-20 15:04:47 -07003650 * @return true if a ICC card is present for a slotIndex
Wink Saville36469e72014-06-11 15:17:00 -07003651 */
Sanket Padawe356d7632015-06-22 14:03:32 -07003652 @Override
Sanket Padawe13bac7b2017-03-20 15:04:47 -07003653 public boolean hasIccCardUsingSlotIndex(int slotIndex) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003654 final long identity = Binder.clearCallingIdentity();
3655 try {
3656 final Phone phone = PhoneFactory.getPhone(slotIndex);
3657 if (phone != null) {
3658 return phone.getIccCard().hasIccCard();
3659 } else {
3660 return false;
3661 }
3662 } finally {
3663 Binder.restoreCallingIdentity(identity);
Amit Mahajana6fc2a82015-01-06 11:53:51 -08003664 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003665 }
3666
3667 /**
3668 * Return if the current radio is LTE on CDMA. This
3669 * is a tri-state return value as for a period of time
3670 * the mode may be unknown.
3671 *
Robert Greenwalt36b23af2015-07-06 17:59:14 -07003672 * @param callingPackage the name of the package making the call.
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003673 * @return {@link Phone#LTE_ON_CDMA_UNKNOWN}, {@link Phone#LTE_ON_CDMA_FALSE}
Jake Hambye994d462014-02-03 13:10:13 -08003674 * or {@link Phone#LTE_ON_CDMA_TRUE}
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003675 */
Robert Greenwalt36b23af2015-07-06 17:59:14 -07003676 @Override
3677 public int getLteOnCdmaMode(String callingPackage) {
3678 return getLteOnCdmaModeForSubscriber(getDefaultSubscription(), callingPackage);
Wink Saville36469e72014-06-11 15:17:00 -07003679 }
3680
Sanket Padawe356d7632015-06-22 14:03:32 -07003681 @Override
Robert Greenwalt36b23af2015-07-06 17:59:14 -07003682 public int getLteOnCdmaModeForSubscriber(int subId, String callingPackage) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08003683 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
Jeff Davidson913390f2018-02-23 17:11:49 -08003684 mApp, subId, callingPackage, "getLteOnCdmaModeForSubscriber")) {
Robert Greenwalt36b23af2015-07-06 17:59:14 -07003685 return PhoneConstants.LTE_ON_CDMA_UNKNOWN;
3686 }
3687
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003688 final long identity = Binder.clearCallingIdentity();
3689 try {
3690 final Phone phone = getPhone(subId);
3691 if (phone == null) {
3692 return PhoneConstants.LTE_ON_CDMA_UNKNOWN;
3693 } else {
3694 return phone.getLteOnCdmaMode();
3695 }
3696 } finally {
3697 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07003698 }
Wink Saville36469e72014-06-11 15:17:00 -07003699 }
3700
Wink Saville36469e72014-06-11 15:17:00 -07003701 /**
3702 * {@hide}
3703 * Returns Default subId, 0 in the case of single standby.
3704 */
Wink Savilleb564aae2014-10-23 10:18:09 -07003705 private int getDefaultSubscription() {
Wink Savilleac1bdfd2014-11-20 23:04:44 -08003706 return mSubscriptionController.getDefaultSubId();
Wink Saville36469e72014-06-11 15:17:00 -07003707 }
3708
Shishir Agrawala9f32182016-04-12 12:00:16 -07003709 private int getSlotForDefaultSubscription() {
3710 return mSubscriptionController.getPhoneId(getDefaultSubscription());
3711 }
3712
Wink Savilleb564aae2014-10-23 10:18:09 -07003713 private int getPreferredVoiceSubscription() {
Wink Savilleac1bdfd2014-11-20 23:04:44 -08003714 return mSubscriptionController.getDefaultVoiceSubId();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003715 }
Ihab Awadf2177b72013-11-25 13:33:23 -08003716
Pengquan Menge92a50d2018-09-21 15:54:48 -07003717 private boolean isActiveSubscription(int subId) {
3718 return mSubscriptionController.isActiveSubId(subId);
3719 }
3720
Ihab Awadf2177b72013-11-25 13:33:23 -08003721 /**
3722 * @see android.telephony.TelephonyManager.WifiCallingChoices
3723 */
3724 public int getWhenToMakeWifiCalls() {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003725 final long identity = Binder.clearCallingIdentity();
3726 try {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08003727 return Settings.System.getInt(mApp.getContentResolver(),
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003728 Settings.System.WHEN_TO_MAKE_WIFI_CALLS,
3729 getWhenToMakeWifiCallsDefaultPreference());
3730 } finally {
3731 Binder.restoreCallingIdentity(identity);
3732 }
Ihab Awadf2177b72013-11-25 13:33:23 -08003733 }
3734
3735 /**
3736 * @see android.telephony.TelephonyManager.WifiCallingChoices
3737 */
3738 public void setWhenToMakeWifiCalls(int preference) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003739 final long identity = Binder.clearCallingIdentity();
3740 try {
3741 if (DBG) log("setWhenToMakeWifiCallsStr, storing setting = " + preference);
Nazanin Bakhshif782e562018-12-11 15:15:39 -08003742 Settings.System.putInt(mApp.getContentResolver(),
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003743 Settings.System.WHEN_TO_MAKE_WIFI_CALLS, preference);
3744 } finally {
3745 Binder.restoreCallingIdentity(identity);
3746 }
Ihab Awadf9e92732013-12-05 18:02:52 -08003747 }
3748
Sailesh Nepald1e68152013-12-12 19:08:02 -08003749 private static int getWhenToMakeWifiCallsDefaultPreference() {
Santos Cordonda120f42014-08-06 04:44:34 -07003750 // TODO: Use a build property to choose this value.
Evan Charlton9829e882013-12-19 15:30:38 -08003751 return TelephonyManager.WifiCallingChoices.ALWAYS_USE;
Ihab Awadf2177b72013-11-25 13:33:23 -08003752 }
Shishir Agrawal69f68122013-12-16 17:25:49 -08003753
Jordan Liu4c733742019-02-28 12:03:40 -08003754 private Phone getPhoneFromSlotIdOrThrowException(int slotIndex) {
3755 int phoneId = UiccController.getInstance().getPhoneIdFromSlotId(slotIndex);
3756 if (phoneId == -1) {
3757 throw new IllegalArgumentException("Given slot index: " + slotIndex
3758 + " does not correspond to an active phone");
3759 }
3760 return PhoneFactory.getPhone(phoneId);
3761 }
3762
Shishir Agrawal566b7612013-10-28 14:41:00 -07003763 @Override
Derek Tan740e1672017-06-27 14:56:27 -07003764 public IccOpenLogicalChannelResponse iccOpenLogicalChannel(
3765 int subId, String callingPackage, String aid, int p2) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08003766 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
3767 mApp, subId, "iccOpenLogicalChannel");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003768 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
Jordan Liu4c733742019-02-28 12:03:40 -08003769 if (DBG) {
3770 log("iccOpenLogicalChannel: subId=" + subId + " aid=" + aid + " p2=" + p2);
3771 }
3772 return iccOpenLogicalChannelWithPermission(getPhoneFromSubId(subId), callingPackage, aid,
3773 p2);
3774 }
Shishir Agrawal566b7612013-10-28 14:41:00 -07003775
Jordan Liu4c733742019-02-28 12:03:40 -08003776
3777 @Override
3778 public IccOpenLogicalChannelResponse iccOpenLogicalChannelBySlot(
3779 int slotIndex, String callingPackage, String aid, int p2) {
3780 enforceModifyPermission();
3781 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
3782 if (DBG) {
3783 log("iccOpenLogicalChannelBySlot: slot=" + slotIndex + " aid=" + aid + " p2=" + p2);
3784 }
3785 return iccOpenLogicalChannelWithPermission(getPhoneFromSlotIdOrThrowException(slotIndex),
3786 callingPackage, aid, p2);
3787 }
3788
3789 private IccOpenLogicalChannelResponse iccOpenLogicalChannelWithPermission(Phone phone,
3790 String callingPackage, String aid, int p2) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003791 final long identity = Binder.clearCallingIdentity();
3792 try {
3793 if (TextUtils.equals(ISDR_AID, aid)) {
3794 // Only allows LPA to open logical channel to ISD-R.
Nazanin Bakhshif782e562018-12-11 15:15:39 -08003795 ComponentInfo bestComponent = EuiccConnector.findBestComponent(getDefaultPhone()
3796 .getContext().getPackageManager());
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003797 if (bestComponent == null
3798 || !TextUtils.equals(callingPackage, bestComponent.packageName)) {
3799 loge("The calling package is not allowed to access ISD-R.");
3800 throw new SecurityException(
3801 "The calling package is not allowed to access ISD-R.");
3802 }
Derek Tan740e1672017-06-27 14:56:27 -07003803 }
Derek Tan740e1672017-06-27 14:56:27 -07003804
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003805 IccOpenLogicalChannelResponse response = (IccOpenLogicalChannelResponse) sendRequest(
Jordan Liu4c733742019-02-28 12:03:40 -08003806 CMD_OPEN_CHANNEL, new Pair<String, Integer>(aid, p2), phone,
3807 null /* workSource */);
3808 if (DBG) log("iccOpenLogicalChannelWithPermission: " + response);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003809 return response;
3810 } finally {
3811 Binder.restoreCallingIdentity(identity);
3812 }
Shishir Agrawal566b7612013-10-28 14:41:00 -07003813 }
3814
3815 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08003816 public boolean iccCloseLogicalChannel(int subId, int channel) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08003817 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
3818 mApp, subId, "iccCloseLogicalChannel");
Jordan Liu4c733742019-02-28 12:03:40 -08003819 if (DBG) log("iccCloseLogicalChannel: subId=" + subId + " chnl=" + channel);
3820 return iccCloseLogicalChannelWithPermission(getPhoneFromSubId(subId), channel);
3821 }
Shishir Agrawal566b7612013-10-28 14:41:00 -07003822
Jordan Liu4c733742019-02-28 12:03:40 -08003823 @Override
3824 public boolean iccCloseLogicalChannelBySlot(int slotIndex, int channel) {
3825 enforceModifyPermission();
3826 if (DBG) log("iccCloseLogicalChannelBySlot: slotIndex=" + slotIndex + " chnl=" + channel);
3827 return iccCloseLogicalChannelWithPermission(getPhoneFromSlotIdOrThrowException(slotIndex),
3828 channel);
3829 }
3830
3831 private boolean iccCloseLogicalChannelWithPermission(Phone phone, int channel) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003832 final long identity = Binder.clearCallingIdentity();
3833 try {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003834 if (channel < 0) {
3835 return false;
3836 }
Jordan Liu4c733742019-02-28 12:03:40 -08003837 Boolean success = (Boolean) sendRequest(CMD_CLOSE_CHANNEL, channel, phone,
3838 null /* workSource */);
3839 if (DBG) log("iccCloseLogicalChannelWithPermission: " + success);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003840 return success;
3841 } finally {
3842 Binder.restoreCallingIdentity(identity);
Shishir Agrawal566b7612013-10-28 14:41:00 -07003843 }
Shishir Agrawal566b7612013-10-28 14:41:00 -07003844 }
3845
3846 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08003847 public String iccTransmitApduLogicalChannel(int subId, int channel, int cla,
Shishir Agrawal566b7612013-10-28 14:41:00 -07003848 int command, int p1, int p2, int p3, String data) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08003849 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
3850 mApp, subId, "iccTransmitApduLogicalChannel");
Jordan Liu4c733742019-02-28 12:03:40 -08003851 if (DBG) {
3852 log("iccTransmitApduLogicalChannel: subId=" + subId + " chnl=" + channel
3853 + " cla=" + cla + " cmd=" + command + " p1=" + p1 + " p2=" + p2 + " p3="
3854 + p3 + " data=" + data);
3855 }
3856 return iccTransmitApduLogicalChannelWithPermission(getPhoneFromSubId(subId), channel, cla,
3857 command, p1, p2, p3, data);
3858 }
Shishir Agrawal566b7612013-10-28 14:41:00 -07003859
Jordan Liu4c733742019-02-28 12:03:40 -08003860 @Override
3861 public String iccTransmitApduLogicalChannelBySlot(int slotIndex, int channel, int cla,
3862 int command, int p1, int p2, int p3, String data) {
3863 enforceModifyPermission();
3864 if (DBG) {
3865 log("iccTransmitApduLogicalChannelBySlot: slotIndex=" + slotIndex + " chnl=" + channel
3866 + " cla=" + cla + " cmd=" + command + " p1=" + p1 + " p2=" + p2 + " p3="
3867 + p3 + " data=" + data);
3868 }
3869 return iccTransmitApduLogicalChannelWithPermission(
3870 getPhoneFromSlotIdOrThrowException(slotIndex), channel, cla, command, p1, p2, p3,
3871 data);
3872 }
3873
3874 private String iccTransmitApduLogicalChannelWithPermission(Phone phone, int channel, int cla,
3875 int command, int p1, int p2, int p3, String data) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003876 final long identity = Binder.clearCallingIdentity();
3877 try {
Hall Liu4fd771b2019-05-02 09:16:29 -07003878 if (channel <= 0) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003879 return "";
3880 }
Shishir Agrawal566b7612013-10-28 14:41:00 -07003881
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003882 IccIoResult response = (IccIoResult) sendRequest(CMD_TRANSMIT_APDU_LOGICAL_CHANNEL,
Jordan Liu4c733742019-02-28 12:03:40 -08003883 new IccAPDUArgument(channel, cla, command, p1, p2, p3, data), phone,
3884 null /* workSource */);
3885 if (DBG) log("iccTransmitApduLogicalChannelWithPermission: " + response);
Shishir Agrawal566b7612013-10-28 14:41:00 -07003886
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003887 // Append the returned status code to the end of the response payload.
3888 String s = Integer.toHexString(
3889 (response.sw1 << 8) + response.sw2 + 0x10000).substring(1);
3890 if (response.payload != null) {
3891 s = IccUtils.bytesToHexString(response.payload) + s;
3892 }
3893 return s;
3894 } finally {
3895 Binder.restoreCallingIdentity(identity);
Shishir Agrawal5ec14172014-08-05 17:05:45 -07003896 }
Shishir Agrawal566b7612013-10-28 14:41:00 -07003897 }
Jake Hambye994d462014-02-03 13:10:13 -08003898
Evan Charltonc66da362014-05-16 14:06:40 -07003899 @Override
Holly Jiuyu Sun1cc2d552018-01-26 15:51:16 -08003900 public String iccTransmitApduBasicChannel(int subId, String callingPackage, int cla,
3901 int command, int p1, int p2, int p3, String data) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08003902 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
3903 mApp, subId, "iccTransmitApduBasicChannel");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003904 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
Jordan Liu4c733742019-02-28 12:03:40 -08003905 if (DBG) {
3906 log("iccTransmitApduBasicChannel: subId=" + subId + " cla=" + cla + " cmd="
3907 + command + " p1=" + p1 + " p2=" + p2 + " p3=" + p3 + " data=" + data);
3908 }
3909 return iccTransmitApduBasicChannelWithPermission(getPhoneFromSubId(subId), callingPackage,
3910 cla, command, p1, p2, p3, data);
3911 }
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07003912
Jordan Liu4c733742019-02-28 12:03:40 -08003913 @Override
3914 public String iccTransmitApduBasicChannelBySlot(int slotIndex, String callingPackage, int cla,
3915 int command, int p1, int p2, int p3, String data) {
3916 enforceModifyPermission();
3917 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
3918 if (DBG) {
3919 log("iccTransmitApduBasicChannelBySlot: slotIndex=" + slotIndex + " cla=" + cla
3920 + " cmd=" + command + " p1=" + p1 + " p2=" + p2 + " p3=" + p3
3921 + " data=" + data);
3922 }
3923
3924 return iccTransmitApduBasicChannelWithPermission(
3925 getPhoneFromSlotIdOrThrowException(slotIndex), callingPackage, cla, command, p1,
3926 p2, p3, data);
3927 }
3928
3929 // open APDU basic channel assuming the caller has sufficient permissions
3930 private String iccTransmitApduBasicChannelWithPermission(Phone phone, String callingPackage,
3931 int cla, int command, int p1, int p2, int p3, String data) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003932 final long identity = Binder.clearCallingIdentity();
3933 try {
3934 if (command == SELECT_COMMAND && p1 == SELECT_P1 && p2 == SELECT_P2 && p3 == SELECT_P3
3935 && TextUtils.equals(ISDR_AID, data)) {
3936 // Only allows LPA to select ISD-R.
Nazanin Bakhshif782e562018-12-11 15:15:39 -08003937 ComponentInfo bestComponent = EuiccConnector.findBestComponent(getDefaultPhone()
3938 .getContext().getPackageManager());
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003939 if (bestComponent == null
3940 || !TextUtils.equals(callingPackage, bestComponent.packageName)) {
3941 loge("The calling package is not allowed to select ISD-R.");
3942 throw new SecurityException(
3943 "The calling package is not allowed to select ISD-R.");
3944 }
Holly Jiuyu Sun1cc2d552018-01-26 15:51:16 -08003945 }
Holly Jiuyu Sun1cc2d552018-01-26 15:51:16 -08003946
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003947 IccIoResult response = (IccIoResult) sendRequest(CMD_TRANSMIT_APDU_BASIC_CHANNEL,
Jordan Liu4c733742019-02-28 12:03:40 -08003948 new IccAPDUArgument(0, cla, command, p1, p2, p3, data), phone,
3949 null /* workSource */);
3950 if (DBG) log("iccTransmitApduBasicChannelWithPermission: " + response);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07003951
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003952 // Append the returned status code to the end of the response payload.
3953 String s = Integer.toHexString(
3954 (response.sw1 << 8) + response.sw2 + 0x10000).substring(1);
3955 if (response.payload != null) {
3956 s = IccUtils.bytesToHexString(response.payload) + s;
3957 }
3958 return s;
3959 } finally {
3960 Binder.restoreCallingIdentity(identity);
Shishir Agrawal5ec14172014-08-05 17:05:45 -07003961 }
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07003962 }
3963
3964 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08003965 public byte[] iccExchangeSimIO(int subId, int fileID, int command, int p1, int p2, int p3,
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07003966 String filePath) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08003967 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
3968 mApp, subId, "iccExchangeSimIO");
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07003969
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003970 final long identity = Binder.clearCallingIdentity();
3971 try {
3972 if (DBG) {
3973 log("Exchange SIM_IO " + subId + ":" + fileID + ":" + command + " "
3974 + p1 + " " + p2 + " " + p3 + ":" + filePath);
3975 }
3976
3977 IccIoResult response =
3978 (IccIoResult) sendRequest(CMD_EXCHANGE_SIM_IO,
3979 new IccAPDUArgument(-1, fileID, command, p1, p2, p3, filePath),
3980 subId);
3981
3982 if (DBG) {
3983 log("Exchange SIM_IO [R]" + response);
3984 }
3985
3986 byte[] result = null;
3987 int length = 2;
3988 if (response.payload != null) {
3989 length = 2 + response.payload.length;
3990 result = new byte[length];
3991 System.arraycopy(response.payload, 0, result, 0, response.payload.length);
3992 } else {
3993 result = new byte[length];
3994 }
3995
3996 result[length - 1] = (byte) response.sw2;
3997 result[length - 2] = (byte) response.sw1;
3998 return result;
3999 } finally {
4000 Binder.restoreCallingIdentity(identity);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07004001 }
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07004002 }
4003
Nathan Haroldb3014052017-01-25 15:57:32 -08004004 /**
4005 * Get the forbidden PLMN List from the given app type (ex APPTYPE_USIM)
4006 * on a particular subscription
4007 */
sqianb6e41952018-03-12 14:54:01 -07004008 public String[] getForbiddenPlmns(int subId, int appType, String callingPackage) {
4009 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
4010 mApp, subId, callingPackage, "getForbiddenPlmns")) {
4011 return null;
4012 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004013
4014 final long identity = Binder.clearCallingIdentity();
4015 try {
4016 if (appType != TelephonyManager.APPTYPE_USIM
4017 && appType != TelephonyManager.APPTYPE_SIM) {
4018 loge("getForbiddenPlmnList(): App Type must be USIM or SIM");
4019 return null;
4020 }
4021 Object response = sendRequest(
4022 CMD_GET_FORBIDDEN_PLMNS, new Integer(appType), subId);
4023 if (response instanceof String[]) {
4024 return (String[]) response;
4025 }
4026 // Response is an Exception of some kind,
4027 // which is signalled to the user as a NULL retval
Nathan Haroldb3014052017-01-25 15:57:32 -08004028 return null;
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004029 } finally {
4030 Binder.restoreCallingIdentity(identity);
Nathan Haroldb3014052017-01-25 15:57:32 -08004031 }
Nathan Haroldb3014052017-01-25 15:57:32 -08004032 }
4033
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07004034 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08004035 public String sendEnvelopeWithStatus(int subId, String content) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08004036 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
4037 mApp, subId, "sendEnvelopeWithStatus");
Evan Charltonc66da362014-05-16 14:06:40 -07004038
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004039 final long identity = Binder.clearCallingIdentity();
4040 try {
4041 IccIoResult response = (IccIoResult) sendRequest(CMD_SEND_ENVELOPE, content, subId);
4042 if (response.payload == null) {
4043 return "";
4044 }
Evan Charltonc66da362014-05-16 14:06:40 -07004045
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004046 // Append the returned status code to the end of the response payload.
4047 String s = Integer.toHexString(
4048 (response.sw1 << 8) + response.sw2 + 0x10000).substring(1);
4049 s = IccUtils.bytesToHexString(response.payload) + s;
4050 return s;
4051 } finally {
4052 Binder.restoreCallingIdentity(identity);
4053 }
Evan Charltonc66da362014-05-16 14:06:40 -07004054 }
4055
Jake Hambye994d462014-02-03 13:10:13 -08004056 /**
4057 * Read one of the NV items defined in {@link com.android.internal.telephony.RadioNVItems}
4058 * and {@code ril_nv_items.h}. Used for device configuration by some CDMA operators.
4059 *
4060 * @param itemID the ID of the item to read
4061 * @return the NV item as a String, or null on error.
4062 */
4063 @Override
4064 public String nvReadItem(int itemID) {
vagdeviaf9a5b92018-08-15 16:01:53 -07004065 WorkSource workSource = getWorkSource(Binder.getCallingUid());
Jeff Davidson7e17e312018-02-13 18:17:36 -08004066 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
4067 mApp, getDefaultSubscription(), "nvReadItem");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004068
4069 final long identity = Binder.clearCallingIdentity();
4070 try {
4071 if (DBG) log("nvReadItem: item " + itemID);
vagdeviaf9a5b92018-08-15 16:01:53 -07004072 String value = (String) sendRequest(CMD_NV_READ_ITEM, itemID, workSource);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004073 if (DBG) log("nvReadItem: item " + itemID + " is \"" + value + '"');
4074 return value;
4075 } finally {
4076 Binder.restoreCallingIdentity(identity);
4077 }
Jake Hambye994d462014-02-03 13:10:13 -08004078 }
4079
4080 /**
4081 * Write one of the NV items defined in {@link com.android.internal.telephony.RadioNVItems}
4082 * and {@code ril_nv_items.h}. Used for device configuration by some CDMA operators.
4083 *
4084 * @param itemID the ID of the item to read
4085 * @param itemValue the value to write, as a String
4086 * @return true on success; false on any failure
4087 */
4088 @Override
4089 public boolean nvWriteItem(int itemID, String itemValue) {
vagdeviaf9a5b92018-08-15 16:01:53 -07004090 WorkSource workSource = getWorkSource(Binder.getCallingUid());
Jeff Davidson7e17e312018-02-13 18:17:36 -08004091 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
4092 mApp, getDefaultSubscription(), "nvWriteItem");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004093
4094 final long identity = Binder.clearCallingIdentity();
4095 try {
4096 if (DBG) log("nvWriteItem: item " + itemID + " value \"" + itemValue + '"');
4097 Boolean success = (Boolean) sendRequest(CMD_NV_WRITE_ITEM,
vagdeviaf9a5b92018-08-15 16:01:53 -07004098 new Pair<Integer, String>(itemID, itemValue), workSource);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004099 if (DBG) log("nvWriteItem: item " + itemID + ' ' + (success ? "ok" : "fail"));
4100 return success;
4101 } finally {
4102 Binder.restoreCallingIdentity(identity);
4103 }
Jake Hambye994d462014-02-03 13:10:13 -08004104 }
4105
4106 /**
4107 * Update the CDMA Preferred Roaming List (PRL) in the radio NV storage.
4108 * Used for device configuration by some CDMA operators.
4109 *
4110 * @param preferredRoamingList byte array containing the new PRL
4111 * @return true on success; false on any failure
4112 */
4113 @Override
4114 public boolean nvWriteCdmaPrl(byte[] preferredRoamingList) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08004115 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
4116 mApp, getDefaultSubscription(), "nvWriteCdmaPrl");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004117
4118 final long identity = Binder.clearCallingIdentity();
4119 try {
4120 if (DBG) log("nvWriteCdmaPrl: value: " + HexDump.toHexString(preferredRoamingList));
4121 Boolean success = (Boolean) sendRequest(CMD_NV_WRITE_CDMA_PRL, preferredRoamingList);
4122 if (DBG) log("nvWriteCdmaPrl: " + (success ? "ok" : "fail"));
4123 return success;
4124 } finally {
4125 Binder.restoreCallingIdentity(identity);
4126 }
Jake Hambye994d462014-02-03 13:10:13 -08004127 }
4128
4129 /**
chen xu6dac5ab2018-10-26 17:39:23 -07004130 * Rollback modem configurations to factory default except some config which are in whitelist.
Jake Hambye994d462014-02-03 13:10:13 -08004131 * Used for device configuration by some CDMA operators.
4132 *
chen xu6dac5ab2018-10-26 17:39:23 -07004133 * @param slotIndex - device slot.
4134 *
Jake Hambye994d462014-02-03 13:10:13 -08004135 * @return true on success; false on any failure
4136 */
4137 @Override
chen xu6dac5ab2018-10-26 17:39:23 -07004138 public boolean resetModemConfig(int slotIndex) {
4139 Phone phone = PhoneFactory.getPhone(slotIndex);
4140 if (phone != null) {
4141 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
4142 mApp, phone.getSubId(), "resetModemConfig");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004143
chen xu6dac5ab2018-10-26 17:39:23 -07004144 final long identity = Binder.clearCallingIdentity();
4145 try {
4146 Boolean success = (Boolean) sendRequest(CMD_RESET_MODEM_CONFIG, null);
4147 if (DBG) log("resetModemConfig:" + ' ' + (success ? "ok" : "fail"));
4148 return success;
4149 } finally {
4150 Binder.restoreCallingIdentity(identity);
4151 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004152 }
chen xu6dac5ab2018-10-26 17:39:23 -07004153 return false;
4154 }
4155
4156 /**
4157 * Generate a radio modem reset. Used for device configuration by some CDMA operators.
4158 *
4159 * @param slotIndex - device slot.
4160 *
4161 * @return true on success; false on any failure
4162 */
4163 @Override
4164 public boolean rebootModem(int slotIndex) {
4165 Phone phone = PhoneFactory.getPhone(slotIndex);
4166 if (phone != null) {
4167 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
4168 mApp, phone.getSubId(), "rebootModem");
4169
4170 final long identity = Binder.clearCallingIdentity();
4171 try {
4172 Boolean success = (Boolean) sendRequest(CMD_MODEM_REBOOT, null);
4173 if (DBG) log("rebootModem:" + ' ' + (success ? "ok" : "fail"));
4174 return success;
4175 } finally {
4176 Binder.restoreCallingIdentity(identity);
4177 }
4178 }
4179 return false;
Jake Hambye994d462014-02-03 13:10:13 -08004180 }
Jake Hamby7c27be32014-03-03 13:25:59 -08004181
Svet Ganovb320e182015-04-16 12:30:10 -07004182 public String[] getPcscfAddress(String apnType, String callingPackage) {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08004183 final Phone defaultPhone = getDefaultPhone();
Jeff Davidson7e17e312018-02-13 18:17:36 -08004184 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
Nazanin Bakhshif782e562018-12-11 15:15:39 -08004185 mApp, defaultPhone.getSubId(), callingPackage, "getPcscfAddress")) {
Svet Ganovb320e182015-04-16 12:30:10 -07004186 return new String[0];
4187 }
4188
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004189 final long identity = Binder.clearCallingIdentity();
4190 try {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08004191 return defaultPhone.getPcscfAddress(apnType);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004192 } finally {
4193 Binder.restoreCallingIdentity(identity);
4194 }
Wink Saville36469e72014-06-11 15:17:00 -07004195 }
4196
Brad Ebinger51f743a2017-01-23 13:50:20 -08004197 /**
Brad Ebinger22bc3e42018-01-16 09:39:35 -08004198 * Enables IMS for the framework. This will trigger IMS registration and ImsFeature capability
4199 * status updates, if not already enabled.
Brad Ebinger51f743a2017-01-23 13:50:20 -08004200 */
Brad Ebinger22bc3e42018-01-16 09:39:35 -08004201 public void enableIms(int slotId) {
Brad Ebinger51f743a2017-01-23 13:50:20 -08004202 enforceModifyPermission();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004203
4204 final long identity = Binder.clearCallingIdentity();
4205 try {
Brad Ebinger9c0eb502019-01-23 15:06:19 -08004206 ImsResolver resolver = PhoneFactory.getImsResolver();
4207 if (resolver == null) {
4208 // may happen if the device does not support IMS.
4209 return;
4210 }
4211 resolver.enableIms(slotId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004212 } finally {
4213 Binder.restoreCallingIdentity(identity);
4214 }
Brad Ebinger34bef922017-11-09 10:27:08 -08004215 }
4216
4217 /**
Brad Ebinger22bc3e42018-01-16 09:39:35 -08004218 * Disables IMS for the framework. This will trigger IMS de-registration and trigger ImsFeature
4219 * status updates to disabled.
Brad Ebinger34bef922017-11-09 10:27:08 -08004220 */
Brad Ebinger22bc3e42018-01-16 09:39:35 -08004221 public void disableIms(int slotId) {
4222 enforceModifyPermission();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004223
4224 final long identity = Binder.clearCallingIdentity();
4225 try {
Brad Ebinger9c0eb502019-01-23 15:06:19 -08004226 ImsResolver resolver = PhoneFactory.getImsResolver();
4227 if (resolver == null) {
4228 // may happen if the device does not support IMS.
4229 return;
4230 }
4231 resolver.disableIms(slotId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004232 } finally {
4233 Binder.restoreCallingIdentity(identity);
4234 }
Brad Ebinger22bc3e42018-01-16 09:39:35 -08004235 }
4236
4237 /**
4238 * Returns the {@link IImsMmTelFeature} that corresponds to the given slot Id for the MMTel
4239 * feature or {@link null} if the service is not available. If the feature is available, the
4240 * {@link IImsServiceFeatureCallback} callback is registered as a listener for feature updates.
4241 */
4242 public IImsMmTelFeature getMmTelFeatureAndListen(int slotId,
Brad Ebinger34bef922017-11-09 10:27:08 -08004243 IImsServiceFeatureCallback callback) {
4244 enforceModifyPermission();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004245
4246 final long identity = Binder.clearCallingIdentity();
4247 try {
Brad Ebinger9c0eb502019-01-23 15:06:19 -08004248 ImsResolver resolver = PhoneFactory.getImsResolver();
4249 if (resolver == null) {
4250 // may happen if the device does not support IMS.
4251 return null;
4252 }
4253 return resolver.getMmTelFeatureAndListen(slotId, callback);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004254 } finally {
4255 Binder.restoreCallingIdentity(identity);
4256 }
Brad Ebinger34bef922017-11-09 10:27:08 -08004257 }
4258
4259 /**
4260 * Returns the {@link IImsRcsFeature} that corresponds to the given slot Id for the RCS
4261 * feature during emergency calling or {@link null} if the service is not available. If the
4262 * feature is available, the {@link IImsServiceFeatureCallback} callback is registered as a
4263 * listener for feature updates.
4264 */
4265 public IImsRcsFeature getRcsFeatureAndListen(int slotId, IImsServiceFeatureCallback callback) {
4266 enforceModifyPermission();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004267
4268 final long identity = Binder.clearCallingIdentity();
4269 try {
Brad Ebinger9c0eb502019-01-23 15:06:19 -08004270 ImsResolver resolver = PhoneFactory.getImsResolver();
4271 if (resolver == null) {
4272 // may happen if the device does not support IMS.
4273 return null;
4274 }
4275 return resolver.getRcsFeatureAndListen(slotId, callback);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004276 } finally {
4277 Binder.restoreCallingIdentity(identity);
4278 }
Brad Ebinger51f743a2017-01-23 13:50:20 -08004279 }
4280
Brad Ebinger5f64b052017-12-14 14:26:15 -08004281 /**
4282 * Returns the {@link IImsRegistration} structure associated with the slotId and feature
Brad Ebinger9c0eb502019-01-23 15:06:19 -08004283 * specified or null if IMS is not supported on the slot specified.
Brad Ebinger5f64b052017-12-14 14:26:15 -08004284 */
4285 public IImsRegistration getImsRegistration(int slotId, int feature) throws RemoteException {
4286 enforceModifyPermission();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004287
4288 final long identity = Binder.clearCallingIdentity();
4289 try {
Brad Ebinger9c0eb502019-01-23 15:06:19 -08004290 ImsResolver resolver = PhoneFactory.getImsResolver();
4291 if (resolver == null) {
4292 // may happen if the device does not support IMS.
4293 return null;
4294 }
4295 return resolver.getImsRegistration(slotId, feature);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004296 } finally {
4297 Binder.restoreCallingIdentity(identity);
4298 }
Brad Ebinger5f64b052017-12-14 14:26:15 -08004299 }
4300
Brad Ebinger22bc3e42018-01-16 09:39:35 -08004301 /**
4302 * Returns the {@link IImsConfig} structure associated with the slotId and feature
Brad Ebinger9c0eb502019-01-23 15:06:19 -08004303 * specified or null if IMS is not supported on the slot specified.
Brad Ebinger22bc3e42018-01-16 09:39:35 -08004304 */
4305 public IImsConfig getImsConfig(int slotId, int feature) throws RemoteException {
4306 enforceModifyPermission();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004307
4308 final long identity = Binder.clearCallingIdentity();
4309 try {
Brad Ebinger9c0eb502019-01-23 15:06:19 -08004310 ImsResolver resolver = PhoneFactory.getImsResolver();
4311 if (resolver == null) {
4312 // may happen if the device does not support IMS.
4313 return null;
4314 }
4315 return resolver.getImsConfig(slotId, feature);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004316 } finally {
4317 Binder.restoreCallingIdentity(identity);
4318 }
Brad Ebinger22bc3e42018-01-16 09:39:35 -08004319 }
4320
Brad Ebinger884c07b2018-02-15 16:17:40 -08004321 /**
Brad Ebingerdac2f002018-04-03 15:17:52 -07004322 * Sets the ImsService Package Name that Telephony will bind to.
4323 *
4324 * @param slotId the slot ID that the ImsService should bind for.
4325 * @param isCarrierImsService true if the ImsService is the carrier override, false if the
4326 * ImsService is the device default ImsService.
4327 * @param packageName The package name of the application that contains the ImsService to bind
4328 * to.
4329 * @return true if setting the ImsService to bind to succeeded, false if it did not.
4330 * @hide
4331 */
4332 public boolean setImsService(int slotId, boolean isCarrierImsService, String packageName) {
Brad Ebingerde696de2018-04-06 09:56:40 -07004333 int[] subIds = SubscriptionManager.getSubId(slotId);
4334 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(mApp,
4335 (subIds != null ? subIds[0] : SubscriptionManager.INVALID_SUBSCRIPTION_ID),
4336 "setImsService");
4337
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004338 final long identity = Binder.clearCallingIdentity();
4339 try {
Brad Ebinger9c0eb502019-01-23 15:06:19 -08004340 ImsResolver resolver = PhoneFactory.getImsResolver();
4341 if (resolver == null) {
4342 // may happen if the device does not support IMS.
4343 return false;
4344 }
4345 return resolver.overrideImsServiceConfiguration(slotId, isCarrierImsService,
4346 packageName);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004347 } finally {
4348 Binder.restoreCallingIdentity(identity);
4349 }
Brad Ebingerdac2f002018-04-03 15:17:52 -07004350 }
4351
4352 /**
4353 * Return the ImsService configuration.
4354 *
4355 * @param slotId The slot that the ImsService is associated with.
4356 * @param isCarrierImsService true, if the ImsService is a carrier override, false if it is
4357 * the device default.
4358 * @return the package name of the ImsService configuration.
4359 */
4360 public String getImsService(int slotId, boolean isCarrierImsService) {
Brad Ebingerde696de2018-04-06 09:56:40 -07004361 int[] subIds = SubscriptionManager.getSubId(slotId);
4362 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(mApp,
4363 (subIds != null ? subIds[0] : SubscriptionManager.INVALID_SUBSCRIPTION_ID),
4364 "getImsService");
4365
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004366 final long identity = Binder.clearCallingIdentity();
4367 try {
Brad Ebinger9c0eb502019-01-23 15:06:19 -08004368 ImsResolver resolver = PhoneFactory.getImsResolver();
4369 if (resolver == null) {
4370 // may happen if the device does not support IMS.
4371 return "";
4372 }
4373 return resolver.getImsServiceConfiguration(slotId, isCarrierImsService);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004374 } finally {
4375 Binder.restoreCallingIdentity(identity);
4376 }
Brad Ebingerdac2f002018-04-03 15:17:52 -07004377 }
4378
Wink Saville36469e72014-06-11 15:17:00 -07004379 public void setImsRegistrationState(boolean registered) {
4380 enforceModifyPermission();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004381
4382 final long identity = Binder.clearCallingIdentity();
4383 try {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08004384 getDefaultPhone().setImsRegistrationState(registered);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004385 } finally {
4386 Binder.restoreCallingIdentity(identity);
4387 }
Wink Saville36469e72014-06-11 15:17:00 -07004388 }
4389
4390 /**
Stuart Scott54788802015-03-30 13:18:01 -07004391 * Set the network selection mode to automatic.
4392 *
4393 */
4394 @Override
4395 public void setNetworkSelectionModeAutomatic(int subId) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08004396 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
4397 mApp, subId, "setNetworkSelectionModeAutomatic");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004398
Pengquan Menge92a50d2018-09-21 15:54:48 -07004399 if (!isActiveSubscription(subId)) {
4400 return;
4401 }
4402
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004403 final long identity = Binder.clearCallingIdentity();
4404 try {
4405 if (DBG) log("setNetworkSelectionModeAutomatic: subId " + subId);
4406 sendRequest(CMD_SET_NETWORK_SELECTION_MODE_AUTOMATIC, null, subId);
4407 } finally {
4408 Binder.restoreCallingIdentity(identity);
4409 }
Stuart Scott54788802015-03-30 13:18:01 -07004410 }
4411
Pengquan Mengea84e042018-09-20 14:57:26 -07004412 /**
4413 * Ask the radio to connect to the input network and change selection mode to manual.
4414 *
4415 * @param subId the id of the subscription.
4416 * @param operatorInfo the operator information, included the PLMN, long name and short name of
4417 * the operator to attach to.
4418 * @param persistSelection whether the selection will persist until reboot. If true, only allows
4419 * attaching to the selected PLMN until reboot; otherwise, attach to the chosen PLMN and resume
4420 * normal network selection next time.
4421 * @return {@code true} on success; {@code true} on any failure.
Shishir Agrawal302c8692015-06-19 13:49:39 -07004422 */
4423 @Override
Pengquan Mengea84e042018-09-20 14:57:26 -07004424 public boolean setNetworkSelectionModeManual(
4425 int subId, OperatorInfo operatorInfo, boolean persistSelection) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08004426 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
4427 mApp, subId, "setNetworkSelectionModeManual");
Pengquan Menge92a50d2018-09-21 15:54:48 -07004428
4429 if (!isActiveSubscription(subId)) {
4430 return false;
4431 }
4432
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004433 final long identity = Binder.clearCallingIdentity();
4434 try {
Pengquan Mengea84e042018-09-20 14:57:26 -07004435 ManualNetworkSelectionArgument arg = new ManualNetworkSelectionArgument(operatorInfo,
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004436 persistSelection);
Pengquan Mengea84e042018-09-20 14:57:26 -07004437 if (DBG) {
4438 log("setNetworkSelectionModeManual: subId: " + subId
4439 + " operator: " + operatorInfo);
4440 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004441 return (Boolean) sendRequest(CMD_SET_NETWORK_SELECTION_MODE_MANUAL, arg, subId);
4442 } finally {
4443 Binder.restoreCallingIdentity(identity);
4444 }
Shishir Agrawal302c8692015-06-19 13:49:39 -07004445 }
4446
4447 /**
4448 * Scans for available networks.
4449 */
4450 @Override
Hall Liuf19c44f2018-11-27 14:38:17 -08004451 public CellNetworkScanResult getCellNetworkScanResults(int subId, String callingPackage) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08004452 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
4453 mApp, subId, "getCellNetworkScanResults");
Hall Liuf19c44f2018-11-27 14:38:17 -08004454 LocationAccessPolicy.LocationPermissionResult locationResult =
4455 LocationAccessPolicy.checkLocationPermission(mApp,
4456 new LocationAccessPolicy.LocationPermissionQuery.Builder()
4457 .setCallingPackage(callingPackage)
4458 .setCallingPid(Binder.getCallingPid())
4459 .setCallingUid(Binder.getCallingUid())
4460 .setMethod("getCellNetworkScanResults")
4461 .setMinSdkVersionForFine(Build.VERSION_CODES.Q)
4462 .build());
4463 switch (locationResult) {
4464 case DENIED_HARD:
4465 throw new SecurityException("Not allowed to access scan results -- location");
4466 case DENIED_SOFT:
4467 return null;
4468 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004469
Pengquan Menga1bb6272018-09-06 09:59:22 -07004470 long identity = Binder.clearCallingIdentity();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004471 try {
4472 if (DBG) log("getCellNetworkScanResults: subId " + subId);
Pengquan Menga1bb6272018-09-06 09:59:22 -07004473 return (CellNetworkScanResult) sendRequest(
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004474 CMD_PERFORM_NETWORK_SCAN, null, subId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004475 } finally {
4476 Binder.restoreCallingIdentity(identity);
4477 }
Shishir Agrawal302c8692015-06-19 13:49:39 -07004478 }
4479
4480 /**
yinxub1bed742017-04-17 11:45:04 -07004481 * Starts a new network scan and returns the id of this scan.
yinxu504e1392017-04-12 16:03:22 -07004482 *
yinxub1bed742017-04-17 11:45:04 -07004483 * @param subId id of the subscription
4484 * @param request contains the radio access networks with bands/channels to scan
4485 * @param messenger callback messenger for scan results or errors
4486 * @param binder for the purpose of auto clean when the user thread crashes
yinxu504e1392017-04-12 16:03:22 -07004487 * @return the id of the requested scan which can be used to stop the scan.
4488 */
4489 @Override
4490 public int requestNetworkScan(int subId, NetworkScanRequest request, Messenger messenger,
Hall Liuf19c44f2018-11-27 14:38:17 -08004491 IBinder binder, String callingPackage) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08004492 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
4493 mApp, subId, "requestNetworkScan");
Hall Liuf19c44f2018-11-27 14:38:17 -08004494 LocationAccessPolicy.LocationPermissionResult locationResult =
4495 LocationAccessPolicy.checkLocationPermission(mApp,
4496 new LocationAccessPolicy.LocationPermissionQuery.Builder()
4497 .setCallingPackage(callingPackage)
4498 .setCallingPid(Binder.getCallingPid())
4499 .setCallingUid(Binder.getCallingUid())
4500 .setMethod("requestNetworkScan")
4501 .setMinSdkVersionForFine(Build.VERSION_CODES.Q)
4502 .build());
Hall Liub2ac8ef2019-02-28 15:56:23 -08004503 if (locationResult != LocationAccessPolicy.LocationPermissionResult.ALLOWED) {
Hall Liu558027f2019-05-15 19:14:05 -07004504 SecurityException e = checkNetworkRequestForSanitizedLocationAccess(request, subId);
Hall Liub2ac8ef2019-02-28 15:56:23 -08004505 if (e != null) {
4506 if (locationResult == LocationAccessPolicy.LocationPermissionResult.DENIED_HARD) {
4507 throw e;
4508 } else {
Hall Liu0e5abaf2019-04-04 01:25:30 -07004509 loge(e.getMessage());
Hall Liub2ac8ef2019-02-28 15:56:23 -08004510 return TelephonyScanManager.INVALID_SCAN_ID;
4511 }
4512 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004513 }
Hall Liu912dfd32019-04-25 14:02:26 -07004514 int callingUid = Binder.getCallingUid();
4515 int callingPid = Binder.getCallingPid();
Ying Xu94a46582019-04-18 17:14:56 -07004516 final long identity = Binder.clearCallingIdentity();
4517 try {
4518 return mNetworkScanRequestTracker.startNetworkScan(
4519 request, messenger, binder, getPhone(subId),
Hall Liu912dfd32019-04-25 14:02:26 -07004520 callingUid, callingPid, callingPackage);
Ying Xu94a46582019-04-18 17:14:56 -07004521 } finally {
4522 Binder.restoreCallingIdentity(identity);
4523 }
yinxu504e1392017-04-12 16:03:22 -07004524 }
4525
Hall Liub2ac8ef2019-02-28 15:56:23 -08004526 private SecurityException checkNetworkRequestForSanitizedLocationAccess(
Hall Liu558027f2019-05-15 19:14:05 -07004527 NetworkScanRequest request, int subId) {
4528 boolean hasCarrierPriv = getCarrierPrivilegeStatusForUid(subId, Binder.getCallingUid())
4529 == TelephonyManager.CARRIER_PRIVILEGE_STATUS_HAS_ACCESS;
4530 boolean hasNetworkScanPermission =
4531 mApp.checkCallingOrSelfPermission(android.Manifest.permission.NETWORK_SCAN)
4532 == PERMISSION_GRANTED;
4533
4534 if (!hasCarrierPriv && !hasNetworkScanPermission) {
4535 return new SecurityException("permission.NETWORK_SCAN or carrier privileges is needed"
4536 + " for network scans without location access.");
Hall Liub2ac8ef2019-02-28 15:56:23 -08004537 }
4538
4539 if (request.getSpecifiers() != null && request.getSpecifiers().length > 0) {
4540 for (RadioAccessSpecifier ras : request.getSpecifiers()) {
Hall Liub2ac8ef2019-02-28 15:56:23 -08004541 if (ras.getChannels() != null && ras.getChannels().length > 0) {
4542 return new SecurityException("Specific channels must not be"
4543 + " scanned without location access.");
4544 }
4545 }
4546 }
4547
Hall Liub2ac8ef2019-02-28 15:56:23 -08004548 return null;
4549 }
4550
yinxu504e1392017-04-12 16:03:22 -07004551 /**
4552 * Stops an existing network scan with the given scanId.
yinxub1bed742017-04-17 11:45:04 -07004553 *
4554 * @param subId id of the subscription
4555 * @param scanId id of the scan that needs to be stopped
yinxu504e1392017-04-12 16:03:22 -07004556 */
4557 @Override
4558 public void stopNetworkScan(int subId, int scanId) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08004559 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
4560 mApp, subId, "stopNetworkScan");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004561
Hall Liu912dfd32019-04-25 14:02:26 -07004562 int callingUid = Binder.getCallingUid();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004563 final long identity = Binder.clearCallingIdentity();
4564 try {
Hall Liu912dfd32019-04-25 14:02:26 -07004565 mNetworkScanRequestTracker.stopNetworkScan(scanId, callingUid);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004566 } finally {
4567 Binder.restoreCallingIdentity(identity);
4568 }
yinxu504e1392017-04-12 16:03:22 -07004569 }
4570
4571 /**
Junda Liu84d15a22014-07-02 11:21:04 -07004572 * Get the calculated preferred network type.
4573 * Used for debugging incorrect network type.
4574 *
4575 * @return the preferred network type, defined in RILConstants.java.
4576 */
4577 @Override
Svet Ganovb320e182015-04-16 12:30:10 -07004578 public int getCalculatedPreferredNetworkType(String callingPackage) {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08004579 final Phone defaultPhone = getDefaultPhone();
4580 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(mApp, defaultPhone.getSubId(),
4581 callingPackage, "getCalculatedPreferredNetworkType")) {
Svet Ganovb320e182015-04-16 12:30:10 -07004582 return RILConstants.PREFERRED_NETWORK_MODE;
4583 }
4584
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004585 final long identity = Binder.clearCallingIdentity();
4586 try {
4587 // FIXME: need to get SubId from somewhere.
Nazanin Bakhshif782e562018-12-11 15:15:39 -08004588 return PhoneFactory.calculatePreferredNetworkType(defaultPhone.getContext(), 0);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004589 } finally {
4590 Binder.restoreCallingIdentity(identity);
4591 }
Junda Liu84d15a22014-07-02 11:21:04 -07004592 }
4593
4594 /**
Jake Hamby7c27be32014-03-03 13:25:59 -08004595 * Get the preferred network type.
4596 * Used for device configuration by some CDMA operators.
4597 *
4598 * @return the preferred network type, defined in RILConstants.java.
4599 */
4600 @Override
Stuart Scott54788802015-03-30 13:18:01 -07004601 public int getPreferredNetworkType(int subId) {
Pengquan Menga4009cb2018-12-20 11:00:24 -08004602 TelephonyPermissions
4603 .enforeceCallingOrSelfReadPrivilegedPhoneStatePermissionOrCarrierPrivilege(
4604 mApp, subId, "getPreferredNetworkType");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004605
4606 final long identity = Binder.clearCallingIdentity();
4607 try {
4608 if (DBG) log("getPreferredNetworkType");
4609 int[] result = (int[]) sendRequest(CMD_GET_PREFERRED_NETWORK_TYPE, null, subId);
4610 int networkType = (result != null ? result[0] : -1);
4611 if (DBG) log("getPreferredNetworkType: " + networkType);
4612 return networkType;
4613 } finally {
4614 Binder.restoreCallingIdentity(identity);
4615 }
Jake Hamby7c27be32014-03-03 13:25:59 -08004616 }
4617
4618 /**
4619 * Set the preferred network type.
4620 * Used for device configuration by some CDMA operators.
4621 *
4622 * @param networkType the preferred network type, defined in RILConstants.java.
4623 * @return true on success; false on any failure.
4624 */
4625 @Override
Stuart Scott54788802015-03-30 13:18:01 -07004626 public boolean setPreferredNetworkType(int subId, int networkType) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08004627 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
4628 mApp, subId, "setPreferredNetworkType");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004629
4630 final long identity = Binder.clearCallingIdentity();
4631 try {
4632 if (DBG) log("setPreferredNetworkType: subId " + subId + " type " + networkType);
4633 Boolean success = (Boolean) sendRequest(
4634 CMD_SET_PREFERRED_NETWORK_TYPE, networkType, subId);
4635 if (DBG) log("setPreferredNetworkType: " + (success ? "ok" : "fail"));
4636 if (success) {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08004637 Settings.Global.putInt(mApp.getContentResolver(),
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004638 Settings.Global.PREFERRED_NETWORK_MODE + subId, networkType);
4639 }
4640 return success;
4641 } finally {
4642 Binder.restoreCallingIdentity(identity);
Junda Liu80bc0d12014-07-14 16:36:44 -07004643 }
Jake Hamby7c27be32014-03-03 13:25:59 -08004644 }
Robert Greenwalted86e582014-05-21 20:03:20 -07004645
4646 /**
Miaoa84611c2019-03-15 09:21:10 +08004647 * Check whether DUN APN is required for tethering with subId.
Junda Liu475951f2014-11-07 16:45:03 -08004648 *
Miaoa84611c2019-03-15 09:21:10 +08004649 * @param subId the id of the subscription to require tethering.
Amit Mahajanfe58cdf2017-07-11 12:01:53 -07004650 * @return {@code true} if DUN APN is required for tethering.
Junda Liu475951f2014-11-07 16:45:03 -08004651 * @hide
4652 */
4653 @Override
Miaoa84611c2019-03-15 09:21:10 +08004654 public boolean getTetherApnRequiredForSubscriber(int subId) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08004655 enforceModifyPermission();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004656 final long identity = Binder.clearCallingIdentity();
Miaoa84611c2019-03-15 09:21:10 +08004657 final Phone phone = getPhone(subId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004658 try {
Miaoa84611c2019-03-15 09:21:10 +08004659 if (phone != null) {
4660 return phone.hasMatchedTetherApnSetting();
4661 } else {
4662 return false;
4663 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004664 } finally {
4665 Binder.restoreCallingIdentity(identity);
Junda Liu475951f2014-11-07 16:45:03 -08004666 }
Junda Liu475951f2014-11-07 16:45:03 -08004667 }
4668
4669 /**
Robert Greenwalted86e582014-05-21 20:03:20 -07004670 * Set mobile data enabled
4671 * Used by the user through settings etc to turn on/off mobile data
4672 *
4673 * @param enable {@code true} turn turn data on, else {@code false}
4674 */
4675 @Override
Malcolm Chen964682d2017-11-28 16:20:07 -08004676 public void setUserDataEnabled(int subId, boolean enable) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08004677 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
4678 mApp, subId, "setUserDataEnabled");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004679
4680 final long identity = Binder.clearCallingIdentity();
4681 try {
4682 int phoneId = mSubscriptionController.getPhoneId(subId);
4683 if (DBG) log("setUserDataEnabled: subId=" + subId + " phoneId=" + phoneId);
4684 Phone phone = PhoneFactory.getPhone(phoneId);
4685 if (phone != null) {
4686 if (DBG) log("setUserDataEnabled: subId=" + subId + " enable=" + enable);
Jack Yud79fba22018-12-13 11:51:28 -08004687 phone.getDataEnabledSettings().setUserDataEnabled(enable);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004688 } else {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08004689 loge("setUserDataEnabled: no phone found. Invalid subId=" + subId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004690 }
4691 } finally {
4692 Binder.restoreCallingIdentity(identity);
Wink Savillee7353bb2014-12-05 14:21:41 -08004693 }
Robert Greenwalted86e582014-05-21 20:03:20 -07004694 }
4695
4696 /**
Malcolm Chen964682d2017-11-28 16:20:07 -08004697 * Get the user enabled state of Mobile Data.
4698 *
4699 * TODO: remove and use isUserDataEnabled.
4700 * This can't be removed now because some vendor codes
4701 * calls through ITelephony directly while they should
4702 * use TelephonyManager.
4703 *
4704 * @return true on enabled
4705 */
4706 @Override
4707 public boolean getDataEnabled(int subId) {
4708 return isUserDataEnabled(subId);
4709 }
4710
4711 /**
4712 * Get whether mobile data is enabled per user setting.
4713 *
4714 * There are other factors deciding whether mobile data is actually enabled, but they are
4715 * not considered here. See {@link #isDataEnabled(int)} for more details.
Robert Greenwalt646120a2014-05-23 11:54:03 -07004716 *
Jeff Davidsona1920712016-11-18 17:05:56 -08004717 * Accepts either ACCESS_NETWORK_STATE, MODIFY_PHONE_STATE or carrier privileges.
Robert Greenwalted86e582014-05-21 20:03:20 -07004718 *
4719 * @return {@code true} if data is enabled else {@code false}
4720 */
4721 @Override
Malcolm Chen964682d2017-11-28 16:20:07 -08004722 public boolean isUserDataEnabled(int subId) {
Robert Greenwalt646120a2014-05-23 11:54:03 -07004723 try {
4724 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.ACCESS_NETWORK_STATE,
4725 null);
4726 } catch (Exception e) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08004727 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
4728 mApp, subId, "isUserDataEnabled");
Robert Greenwalt646120a2014-05-23 11:54:03 -07004729 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004730
4731 final long identity = Binder.clearCallingIdentity();
4732 try {
4733 int phoneId = mSubscriptionController.getPhoneId(subId);
4734 if (DBG) log("isUserDataEnabled: subId=" + subId + " phoneId=" + phoneId);
4735 Phone phone = PhoneFactory.getPhone(phoneId);
4736 if (phone != null) {
4737 boolean retVal = phone.isUserDataEnabled();
4738 if (DBG) log("isUserDataEnabled: subId=" + subId + " retVal=" + retVal);
4739 return retVal;
4740 } else {
4741 if (DBG) loge("isUserDataEnabled: no phone subId=" + subId + " retVal=false");
4742 return false;
4743 }
4744 } finally {
4745 Binder.restoreCallingIdentity(identity);
Malcolm Chen964682d2017-11-28 16:20:07 -08004746 }
4747 }
4748
4749 /**
4750 * Get whether mobile data is enabled.
4751 *
4752 * Comparable to {@link #isUserDataEnabled(int)}, this considers all factors deciding
4753 * whether mobile data is actually enabled.
4754 *
4755 * Accepts either ACCESS_NETWORK_STATE, MODIFY_PHONE_STATE or carrier privileges.
4756 *
4757 * @return {@code true} if data is enabled else {@code false}
4758 */
4759 @Override
4760 public boolean isDataEnabled(int subId) {
4761 try {
4762 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.ACCESS_NETWORK_STATE,
4763 null);
4764 } catch (Exception e) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08004765 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
4766 mApp, subId, "isDataEnabled");
Malcolm Chen964682d2017-11-28 16:20:07 -08004767 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004768
4769 final long identity = Binder.clearCallingIdentity();
4770 try {
4771 int phoneId = mSubscriptionController.getPhoneId(subId);
4772 if (DBG) log("isDataEnabled: subId=" + subId + " phoneId=" + phoneId);
4773 Phone phone = PhoneFactory.getPhone(phoneId);
4774 if (phone != null) {
Jack Yud79fba22018-12-13 11:51:28 -08004775 boolean retVal = phone.getDataEnabledSettings().isDataEnabled();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004776 if (DBG) log("isDataEnabled: subId=" + subId + " retVal=" + retVal);
4777 return retVal;
4778 } else {
4779 if (DBG) loge("isDataEnabled: no phone subId=" + subId + " retVal=false");
4780 return false;
4781 }
4782 } finally {
4783 Binder.restoreCallingIdentity(identity);
Wink Savillee7353bb2014-12-05 14:21:41 -08004784 }
Robert Greenwalted86e582014-05-21 20:03:20 -07004785 }
Shishir Agrawal60f9c952014-06-23 12:00:43 -07004786
Nazanin Bakhshi5d0636e2019-08-19 16:29:37 -07004787 private int getCarrierPrivilegeStatusFromCarrierConfigRules(int privilegeFromSim,
4788 Phone phone) {
4789 //load access rules from carrier configs, and check those as well: b/139133814
4790 SubscriptionController subController = SubscriptionController.getInstance();
4791 if (privilegeFromSim == TelephonyManager.CARRIER_PRIVILEGE_STATUS_HAS_ACCESS
4792 || subController == null) return privilegeFromSim;
4793
4794 int uid = Binder.getCallingUid();
4795 PackageManager pkgMgr = phone.getContext().getPackageManager();
4796 String[] packages = pkgMgr.getPackagesForUid(uid);
4797
4798 final long identity = Binder.clearCallingIdentity();
4799 try {
4800 SubscriptionInfo subInfo = subController.getSubscriptionInfo(phone.getSubId());
4801 SubscriptionManager subManager = (SubscriptionManager)
4802 phone.getContext().getSystemService(Context.TELEPHONY_SUBSCRIPTION_SERVICE);
4803 for (String pkg : packages) {
4804 if (subManager.canManageSubscription(subInfo, pkg)) {
4805 return TelephonyManager.CARRIER_PRIVILEGE_STATUS_HAS_ACCESS;
4806 }
4807 }
4808 return privilegeFromSim;
4809 } finally {
4810 Binder.restoreCallingIdentity(identity);
4811 }
4812 }
4813
4814 private int getCarrierPrivilegeStatusFromCarrierConfigRules(int privilegeFromSim, Phone phone,
4815 String pkgName) {
4816 //load access rules from carrier configs, and check those as well: b/139133814
4817 SubscriptionController subController = SubscriptionController.getInstance();
4818 if (privilegeFromSim == TelephonyManager.CARRIER_PRIVILEGE_STATUS_HAS_ACCESS
4819 || subController == null) return privilegeFromSim;
4820
4821 final long identity = Binder.clearCallingIdentity();
4822 try {
4823 SubscriptionInfo subInfo = subController.getSubscriptionInfo(phone.getSubId());
4824 SubscriptionManager subManager = (SubscriptionManager)
4825 phone.getContext().getSystemService(Context.TELEPHONY_SUBSCRIPTION_SERVICE);
4826 return subManager.canManageSubscription(subInfo, pkgName)
4827 ? TelephonyManager.CARRIER_PRIVILEGE_STATUS_HAS_ACCESS : privilegeFromSim;
4828 } finally {
4829 Binder.restoreCallingIdentity(identity);
4830 }
4831 }
4832
Shishir Agrawal60f9c952014-06-23 12:00:43 -07004833 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08004834 public int getCarrierPrivilegeStatus(int subId) {
4835 final Phone phone = getPhone(subId);
4836 if (phone == null) {
4837 loge("getCarrierPrivilegeStatus: Invalid subId");
4838 return TelephonyManager.CARRIER_PRIVILEGE_STATUS_NO_ACCESS;
4839 }
4840 UiccCard card = UiccController.getInstance().getUiccCard(phone.getPhoneId());
Shishir Agrawaleb8771e2014-07-22 11:24:08 -07004841 if (card == null) {
Shishir Agrawal5e5becd2014-11-18 11:38:23 -08004842 loge("getCarrierPrivilegeStatus: No UICC");
Shishir Agrawaleb8771e2014-07-22 11:24:08 -07004843 return TelephonyManager.CARRIER_PRIVILEGE_STATUS_RULES_NOT_LOADED;
4844 }
Nazanin Bakhshi5d0636e2019-08-19 16:29:37 -07004845
4846 return getCarrierPrivilegeStatusFromCarrierConfigRules(
4847 card.getCarrierPrivilegeStatusForCurrentTransaction(
4848 phone.getContext().getPackageManager()), phone);
Shishir Agrawal60f9c952014-06-23 12:00:43 -07004849 }
Junda Liu29340342014-07-10 15:23:27 -07004850
4851 @Override
Jeff Davidson7e17e312018-02-13 18:17:36 -08004852 public int getCarrierPrivilegeStatusForUid(int subId, int uid) {
4853 final Phone phone = getPhone(subId);
4854 if (phone == null) {
4855 loge("getCarrierPrivilegeStatus: Invalid subId");
4856 return TelephonyManager.CARRIER_PRIVILEGE_STATUS_NO_ACCESS;
4857 }
4858 UiccProfile profile =
4859 UiccController.getInstance().getUiccProfileForPhone(phone.getPhoneId());
4860 if (profile == null) {
4861 loge("getCarrierPrivilegeStatus: No UICC");
4862 return TelephonyManager.CARRIER_PRIVILEGE_STATUS_RULES_NOT_LOADED;
4863 }
Nazanin Bakhshi5d0636e2019-08-19 16:29:37 -07004864 return getCarrierPrivilegeStatusFromCarrierConfigRules(
4865 profile.getCarrierPrivilegeStatusForUid(
4866 phone.getContext().getPackageManager(), uid), phone);
Jeff Davidson7e17e312018-02-13 18:17:36 -08004867 }
4868
4869 @Override
chen xuf7e9fe82019-05-09 19:31:02 -07004870 public int checkCarrierPrivilegesForPackage(int subId, String pkgName) {
4871 if (TextUtils.isEmpty(pkgName)) {
Junda Liu317d70b2016-03-08 09:33:53 -08004872 return TelephonyManager.CARRIER_PRIVILEGE_STATUS_NO_ACCESS;
chen xuf7e9fe82019-05-09 19:31:02 -07004873 }
4874
4875 int phoneId = SubscriptionManager.getPhoneId(subId);
4876 UiccCard card = UiccController.getInstance().getUiccCard(phoneId);
Shishir Agrawaleb8771e2014-07-22 11:24:08 -07004877 if (card == null) {
chen xuf7e9fe82019-05-09 19:31:02 -07004878 loge("checkCarrierPrivilegesForPackage: No UICC on subId " + subId);
Shishir Agrawaleb8771e2014-07-22 11:24:08 -07004879 return TelephonyManager.CARRIER_PRIVILEGE_STATUS_RULES_NOT_LOADED;
4880 }
Nazanin Bakhshi5d0636e2019-08-19 16:29:37 -07004881 return getCarrierPrivilegeStatusFromCarrierConfigRules(
4882 card.getCarrierPrivilegeStatus(mApp.getPackageManager(), pkgName),
4883 getPhone(phoneId), pkgName);
Zach Johnson50ecba32015-05-19 00:24:21 -07004884 }
4885
4886 @Override
4887 public int checkCarrierPrivilegesForPackageAnyPhone(String pkgName) {
Junda Liu317d70b2016-03-08 09:33:53 -08004888 if (TextUtils.isEmpty(pkgName))
4889 return TelephonyManager.CARRIER_PRIVILEGE_STATUS_NO_ACCESS;
Zach Johnson50ecba32015-05-19 00:24:21 -07004890 int result = TelephonyManager.CARRIER_PRIVILEGE_STATUS_RULES_NOT_LOADED;
4891 for (int i = 0; i < TelephonyManager.getDefault().getPhoneCount(); i++) {
4892 UiccCard card = UiccController.getInstance().getUiccCard(i);
4893 if (card == null) {
Jonathan Basseri7d320df2015-06-16 12:17:08 -07004894 // No UICC in that slot.
Zach Johnson50ecba32015-05-19 00:24:21 -07004895 continue;
4896 }
4897
Nazanin Bakhshi5d0636e2019-08-19 16:29:37 -07004898 result = getCarrierPrivilegeStatusFromCarrierConfigRules(
4899 card.getCarrierPrivilegeStatus(mApp.getPackageManager(), pkgName),
4900 getPhone(i), pkgName);
Zach Johnson50ecba32015-05-19 00:24:21 -07004901 if (result == TelephonyManager.CARRIER_PRIVILEGE_STATUS_HAS_ACCESS) {
4902 break;
4903 }
4904 }
4905
4906 return result;
Junda Liu29340342014-07-10 15:23:27 -07004907 }
Derek Tan89e89d42014-07-08 17:00:10 -07004908
4909 @Override
Junda Liue64de782015-04-16 17:19:16 -07004910 public List<String> getCarrierPackageNamesForIntentAndPhone(Intent intent, int phoneId) {
4911 if (!SubscriptionManager.isValidPhoneId(phoneId)) {
4912 loge("phoneId " + phoneId + " is not valid.");
4913 return null;
4914 }
4915 UiccCard card = UiccController.getInstance().getUiccCard(phoneId);
Shishir Agrawaleb6439a2014-07-21 13:19:38 -07004916 if (card == null) {
Diego Pontorieroaf74c862014-08-28 11:51:16 -07004917 loge("getCarrierPackageNamesForIntent: No UICC");
Shishir Agrawaleb6439a2014-07-21 13:19:38 -07004918 return null ;
4919 }
Nazanin Bakhshif782e562018-12-11 15:15:39 -08004920 return card.getCarrierPackageNamesForIntent(mApp.getPackageManager(), intent);
Shishir Agrawaleb6439a2014-07-21 13:19:38 -07004921 }
4922
Amith Yamasani6e118872016-02-19 12:53:51 -08004923 @Override
chen xuf7e9fe82019-05-09 19:31:02 -07004924 public List<String> getPackagesWithCarrierPrivileges(int phoneId) {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08004925 PackageManager pm = mApp.getPackageManager();
Amith Yamasani6e118872016-02-19 12:53:51 -08004926 List<String> privilegedPackages = new ArrayList<>();
4927 List<PackageInfo> packages = null;
chen xuf7e9fe82019-05-09 19:31:02 -07004928 UiccCard card = UiccController.getInstance().getUiccCard(phoneId);
4929 // has UICC in that slot.
4930 if (card != null) {
Amith Yamasani6e118872016-02-19 12:53:51 -08004931 if (card.hasCarrierPrivilegeRules()) {
4932 if (packages == null) {
4933 // Only check packages in user 0 for now
4934 packages = pm.getInstalledPackagesAsUser(
Nazanin Bakhshi5d0636e2019-08-19 16:29:37 -07004935 PackageManager.MATCH_DISABLED_COMPONENTS
4936 | PackageManager.MATCH_DISABLED_UNTIL_USED_COMPONENTS
Cheonho Park17089c62019-08-01 15:23:12 +09004937 | PackageManager.GET_SIGNING_CERTIFICATES,
4938 UserHandle.USER_SYSTEM);
Amith Yamasani6e118872016-02-19 12:53:51 -08004939 }
4940 for (int p = packages.size() - 1; p >= 0; p--) {
4941 PackageInfo pkgInfo = packages.get(p);
4942 if (pkgInfo != null && pkgInfo.packageName != null
4943 && card.getCarrierPrivilegeStatus(pkgInfo)
chen xuf7e9fe82019-05-09 19:31:02 -07004944 == TelephonyManager.CARRIER_PRIVILEGE_STATUS_HAS_ACCESS) {
Amith Yamasani6e118872016-02-19 12:53:51 -08004945 privilegedPackages.add(pkgInfo.packageName);
4946 }
4947 }
4948 }
4949 }
4950 return privilegedPackages;
4951 }
4952
chen xuf7e9fe82019-05-09 19:31:02 -07004953 @Override
4954 public List<String> getPackagesWithCarrierPrivilegesForAllPhones() {
4955 List<String> privilegedPackages = new ArrayList<>();
4956 for (int i = 0; i < TelephonyManager.getDefault().getPhoneCount(); i++) {
4957 privilegedPackages.addAll(getPackagesWithCarrierPrivileges(i));
4958 }
4959 return privilegedPackages;
4960 }
4961
Wink Savilleb564aae2014-10-23 10:18:09 -07004962 private String getIccId(int subId) {
Sanket Padawe356d7632015-06-22 14:03:32 -07004963 final Phone phone = getPhone(subId);
4964 UiccCard card = phone == null ? null : phone.getUiccCard();
Derek Tan97ebb422014-09-05 16:55:38 -07004965 if (card == null) {
4966 loge("getIccId: No UICC");
4967 return null;
4968 }
4969 String iccId = card.getIccId();
4970 if (TextUtils.isEmpty(iccId)) {
4971 loge("getIccId: ICC ID is null or empty.");
4972 return null;
4973 }
4974 return iccId;
4975 }
4976
Shishir Agrawaleb6439a2014-07-21 13:19:38 -07004977 @Override
Jeff Sharkey85190e62014-12-05 09:40:12 -08004978 public boolean setLine1NumberForDisplayForSubscriber(int subId, String alphaTag,
4979 String number) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08004980 TelephonyPermissions.enforceCallingOrSelfCarrierPrivilege(
4981 subId, "setLine1NumberForDisplayForSubscriber");
Derek Tan97ebb422014-09-05 16:55:38 -07004982
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004983 final long identity = Binder.clearCallingIdentity();
4984 try {
4985 final String iccId = getIccId(subId);
4986 final Phone phone = getPhone(subId);
4987 if (phone == null) {
4988 return false;
4989 }
4990 final String subscriberId = phone.getSubscriberId();
4991
4992 if (DBG_MERGE) {
4993 Slog.d(LOG_TAG, "Setting line number for ICC=" + iccId + ", subscriberId="
4994 + subscriberId + " to " + number);
4995 }
4996
4997 if (TextUtils.isEmpty(iccId)) {
4998 return false;
4999 }
5000
5001 final SharedPreferences.Editor editor = mTelephonySharedPreferences.edit();
5002
5003 final String alphaTagPrefKey = PREF_CARRIERS_ALPHATAG_PREFIX + iccId;
5004 if (alphaTag == null) {
5005 editor.remove(alphaTagPrefKey);
5006 } else {
5007 editor.putString(alphaTagPrefKey, alphaTag);
5008 }
5009
5010 // Record both the line number and IMSI for this ICCID, since we need to
5011 // track all merged IMSIs based on line number
5012 final String numberPrefKey = PREF_CARRIERS_NUMBER_PREFIX + iccId;
5013 final String subscriberPrefKey = PREF_CARRIERS_SUBSCRIBER_PREFIX + iccId;
5014 if (number == null) {
5015 editor.remove(numberPrefKey);
5016 editor.remove(subscriberPrefKey);
5017 } else {
5018 editor.putString(numberPrefKey, number);
5019 editor.putString(subscriberPrefKey, subscriberId);
5020 }
5021
5022 editor.commit();
5023 return true;
5024 } finally {
5025 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07005026 }
Derek Tan7226c842014-07-02 17:42:23 -07005027 }
5028
5029 @Override
Svet Ganovb320e182015-04-16 12:30:10 -07005030 public String getLine1NumberForDisplay(int subId, String callingPackage) {
Makoto Onukifee69342015-06-29 14:44:50 -07005031 // This is open to apps with WRITE_SMS.
Jeff Davidson7e17e312018-02-13 18:17:36 -08005032 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneNumber(
Jeff Davidson913390f2018-02-23 17:11:49 -08005033 mApp, subId, callingPackage, "getLine1NumberForDisplay")) {
Amit Mahajan9cf11512015-11-09 11:40:48 -08005034 if (DBG_MERGE) log("getLine1NumberForDisplay returning null due to permission");
Svet Ganovb320e182015-04-16 12:30:10 -07005035 return null;
5036 }
Derek Tan97ebb422014-09-05 16:55:38 -07005037
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005038 final long identity = Binder.clearCallingIdentity();
5039 try {
5040 String iccId = getIccId(subId);
5041 if (iccId != null) {
5042 String numberPrefKey = PREF_CARRIERS_NUMBER_PREFIX + iccId;
5043 if (DBG_MERGE) {
5044 log("getLine1NumberForDisplay returning "
5045 + mTelephonySharedPreferences.getString(numberPrefKey, null));
5046 }
5047 return mTelephonySharedPreferences.getString(numberPrefKey, null);
Amit Mahajan9cf11512015-11-09 11:40:48 -08005048 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005049 if (DBG_MERGE) log("getLine1NumberForDisplay returning null as iccId is null");
5050 return null;
5051 } finally {
5052 Binder.restoreCallingIdentity(identity);
Derek Tan7226c842014-07-02 17:42:23 -07005053 }
Derek Tan7226c842014-07-02 17:42:23 -07005054 }
5055
5056 @Override
Svet Ganovb320e182015-04-16 12:30:10 -07005057 public String getLine1AlphaTagForDisplay(int subId, String callingPackage) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08005058 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
Jeff Davidson913390f2018-02-23 17:11:49 -08005059 mApp, subId, callingPackage, "getLine1AlphaTagForDisplay")) {
Svet Ganovb320e182015-04-16 12:30:10 -07005060 return null;
5061 }
Derek Tan97ebb422014-09-05 16:55:38 -07005062
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005063 final long identity = Binder.clearCallingIdentity();
5064 try {
5065 String iccId = getIccId(subId);
5066 if (iccId != null) {
5067 String alphaTagPrefKey = PREF_CARRIERS_ALPHATAG_PREFIX + iccId;
5068 return mTelephonySharedPreferences.getString(alphaTagPrefKey, null);
5069 }
5070 return null;
5071 } finally {
5072 Binder.restoreCallingIdentity(identity);
Derek Tan7226c842014-07-02 17:42:23 -07005073 }
Derek Tan7226c842014-07-02 17:42:23 -07005074 }
Shishir Agrawalb1ebf8c2014-07-17 16:32:41 -07005075
5076 @Override
Jordan Liub49b04b2019-05-06 14:45:15 -07005077 public String[] getMergedSubscriberIds(int subId, String callingPackage) {
Jeff Davidson913390f2018-02-23 17:11:49 -08005078 // This API isn't public, so no need to provide a valid subscription ID - we're not worried
5079 // about carrier-privileged callers not having access.
Jeff Davidson7e17e312018-02-13 18:17:36 -08005080 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
Jeff Davidson913390f2018-02-23 17:11:49 -08005081 mApp, SubscriptionManager.INVALID_SUBSCRIPTION_ID, callingPackage,
5082 "getMergedSubscriberIds")) {
Fyodor Kupolov8e53b0b2015-06-17 13:17:50 -07005083 return null;
5084 }
Jeff Sharkey85190e62014-12-05 09:40:12 -08005085
Jordan Liub49b04b2019-05-06 14:45:15 -07005086 // Clear calling identity, when calling TelephonyManager, because callerUid must be
5087 // the process, where TelephonyManager was instantiated.
5088 // Otherwise AppOps check will fail.
Fyodor Kupolov8e53b0b2015-06-17 13:17:50 -07005089 final long identity = Binder.clearCallingIdentity();
5090 try {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08005091 final Context context = mApp;
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005092 final TelephonyManager tele = TelephonyManager.from(context);
5093 final SubscriptionManager sub = SubscriptionManager.from(context);
5094
5095 // Figure out what subscribers are currently active
5096 final ArraySet<String> activeSubscriberIds = new ArraySet<>();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005097
Jordan Liub49b04b2019-05-06 14:45:15 -07005098 // Only consider subs which match the current subId
5099 // This logic can be simplified. See b/131189269 for progress.
5100 if (isActiveSubscription(subId)) {
Fyodor Kupolov8e53b0b2015-06-17 13:17:50 -07005101 activeSubscriberIds.add(tele.getSubscriberId(subId));
5102 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005103
5104 // First pass, find a number override for an active subscriber
5105 String mergeNumber = null;
5106 final Map<String, ?> prefs = mTelephonySharedPreferences.getAll();
5107 for (String key : prefs.keySet()) {
5108 if (key.startsWith(PREF_CARRIERS_SUBSCRIBER_PREFIX)) {
5109 final String subscriberId = (String) prefs.get(key);
5110 if (activeSubscriberIds.contains(subscriberId)) {
5111 final String iccId = key.substring(
5112 PREF_CARRIERS_SUBSCRIBER_PREFIX.length());
5113 final String numberKey = PREF_CARRIERS_NUMBER_PREFIX + iccId;
5114 mergeNumber = (String) prefs.get(numberKey);
5115 if (DBG_MERGE) {
5116 Slog.d(LOG_TAG, "Found line number " + mergeNumber
5117 + " for active subscriber " + subscriberId);
5118 }
5119 if (!TextUtils.isEmpty(mergeNumber)) {
5120 break;
5121 }
5122 }
5123 }
5124 }
5125
5126 // Shortcut when no active merged subscribers
5127 if (TextUtils.isEmpty(mergeNumber)) {
5128 return null;
5129 }
5130
5131 // Second pass, find all subscribers under that line override
5132 final ArraySet<String> result = new ArraySet<>();
5133 for (String key : prefs.keySet()) {
5134 if (key.startsWith(PREF_CARRIERS_NUMBER_PREFIX)) {
5135 final String number = (String) prefs.get(key);
5136 if (mergeNumber.equals(number)) {
5137 final String iccId = key.substring(PREF_CARRIERS_NUMBER_PREFIX.length());
5138 final String subscriberKey = PREF_CARRIERS_SUBSCRIBER_PREFIX + iccId;
5139 final String subscriberId = (String) prefs.get(subscriberKey);
5140 if (!TextUtils.isEmpty(subscriberId)) {
5141 result.add(subscriberId);
5142 }
5143 }
5144 }
5145 }
5146
5147 final String[] resultArray = result.toArray(new String[result.size()]);
5148 Arrays.sort(resultArray);
5149 if (DBG_MERGE) {
5150 Slog.d(LOG_TAG,
5151 "Found subscribers " + Arrays.toString(resultArray) + " after merge");
5152 }
5153 return resultArray;
Fyodor Kupolov8e53b0b2015-06-17 13:17:50 -07005154 } finally {
5155 Binder.restoreCallingIdentity(identity);
Jeff Sharkey85190e62014-12-05 09:40:12 -08005156 }
Jeff Sharkey85190e62014-12-05 09:40:12 -08005157 }
5158
5159 @Override
Malcolm Chen6ca97372019-07-01 16:28:21 -07005160 public String[] getMergedSubscriberIdsFromGroup(int subId, String callingPackage) {
5161 enforceReadPrivilegedPermission("getMergedSubscriberIdsFromGroup");
5162
5163 final long identity = Binder.clearCallingIdentity();
5164 try {
5165 final TelephonyManager telephonyManager = mApp.getSystemService(
5166 TelephonyManager.class);
5167 String subscriberId = telephonyManager.getSubscriberId(subId);
5168 if (subscriberId == null) {
5169 if (DBG) {
5170 log("getMergedSubscriberIdsFromGroup can't find subscriberId for subId "
5171 + subId);
5172 }
5173 return null;
5174 }
5175
5176 final SubscriptionInfo info = SubscriptionController.getInstance()
5177 .getSubscriptionInfo(subId);
5178 final ParcelUuid groupUuid = info.getGroupUuid();
5179 // If it doesn't belong to any group, return just subscriberId of itself.
5180 if (groupUuid == null) {
5181 return new String[]{subscriberId};
5182 }
5183
5184 // Get all subscriberIds from the group.
5185 final List<String> mergedSubscriberIds = new ArrayList<>();
5186 final List<SubscriptionInfo> groupInfos = SubscriptionController.getInstance()
5187 .getSubscriptionsInGroup(groupUuid, mApp.getOpPackageName());
5188 for (SubscriptionInfo subInfo : groupInfos) {
5189 subscriberId = telephonyManager.getSubscriberId(subInfo.getSubscriptionId());
5190 if (subscriberId != null) {
5191 mergedSubscriberIds.add(subscriberId);
5192 }
5193 }
5194
5195 return mergedSubscriberIds.toArray(new String[mergedSubscriberIds.size()]);
5196 } finally {
5197 Binder.restoreCallingIdentity(identity);
5198
5199 }
5200 }
5201
5202 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08005203 public boolean setOperatorBrandOverride(int subId, String brand) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08005204 TelephonyPermissions.enforceCallingOrSelfCarrierPrivilege(
5205 subId, "setOperatorBrandOverride");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005206
5207 final long identity = Binder.clearCallingIdentity();
5208 try {
5209 final Phone phone = getPhone(subId);
5210 return phone == null ? false : phone.setOperatorBrandOverride(brand);
5211 } finally {
5212 Binder.restoreCallingIdentity(identity);
5213 }
Shishir Agrawalb1ebf8c2014-07-17 16:32:41 -07005214 }
Steven Liu4bf01bc2014-07-17 11:05:29 -05005215
5216 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08005217 public boolean setRoamingOverride(int subId, List<String> gsmRoamingList,
Shishir Agrawal621a47c2014-12-01 10:25:09 -08005218 List<String> gsmNonRoamingList, List<String> cdmaRoamingList,
5219 List<String> cdmaNonRoamingList) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08005220 TelephonyPermissions.enforceCallingOrSelfCarrierPrivilege(subId, "setRoamingOverride");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005221
5222 final long identity = Binder.clearCallingIdentity();
5223 try {
5224 final Phone phone = getPhone(subId);
5225 if (phone == null) {
5226 return false;
5227 }
5228 return phone.setRoamingOverride(gsmRoamingList, gsmNonRoamingList, cdmaRoamingList,
5229 cdmaNonRoamingList);
5230 } finally {
5231 Binder.restoreCallingIdentity(identity);
Shishir Agrawalc04d9752016-02-19 10:41:00 -08005232 }
Shishir Agrawal621a47c2014-12-01 10:25:09 -08005233 }
5234
5235 @Override
Shuo Qian850e4d6a2018-04-25 21:02:08 +00005236 @Deprecated
5237 public int invokeOemRilRequestRaw(byte[] oemReq, byte[] oemResp) {
5238 enforceModifyPermission();
5239
5240 int returnValue = 0;
5241 try {
vagdeviaf9a5b92018-08-15 16:01:53 -07005242 AsyncResult result = (AsyncResult) sendRequest(CMD_INVOKE_OEM_RIL_REQUEST_RAW, oemReq);
Shuo Qian850e4d6a2018-04-25 21:02:08 +00005243 if(result.exception == null) {
5244 if (result.result != null) {
5245 byte[] responseData = (byte[])(result.result);
5246 if(responseData.length > oemResp.length) {
5247 Log.w(LOG_TAG, "Buffer to copy response too small: Response length is " +
5248 responseData.length + "bytes. Buffer Size is " +
5249 oemResp.length + "bytes.");
5250 }
5251 System.arraycopy(responseData, 0, oemResp, 0, responseData.length);
5252 returnValue = responseData.length;
5253 }
5254 } else {
5255 CommandException ex = (CommandException) result.exception;
5256 returnValue = ex.getCommandError().ordinal();
5257 if(returnValue > 0) returnValue *= -1;
5258 }
5259 } catch (RuntimeException e) {
5260 Log.w(LOG_TAG, "sendOemRilRequestRaw: Runtime Exception");
5261 returnValue = (CommandException.Error.GENERIC_FAILURE.ordinal());
5262 if(returnValue > 0) returnValue *= -1;
5263 }
5264
5265 return returnValue;
5266 }
5267
5268 @Override
Wink Saville5d475dd2014-10-17 15:00:58 -07005269 public void setRadioCapability(RadioAccessFamily[] rafs) {
5270 try {
5271 ProxyController.getInstance().setRadioCapability(rafs);
5272 } catch (RuntimeException e) {
5273 Log.w(LOG_TAG, "setRadioCapability: Runtime Exception");
5274 }
5275 }
5276
5277 @Override
Robert Greenwalt36b23af2015-07-06 17:59:14 -07005278 public int getRadioAccessFamily(int phoneId, String callingPackage) {
Jeff Davidson913390f2018-02-23 17:11:49 -08005279 Phone phone = PhoneFactory.getPhone(phoneId);
chen xub97461a2018-10-26 14:17:57 -07005280 int raf = RadioAccessFamily.RAF_UNKNOWN;
Jeff Davidson913390f2018-02-23 17:11:49 -08005281 if (phone == null) {
chen xub97461a2018-10-26 14:17:57 -07005282 return raf;
Jeff Davidson913390f2018-02-23 17:11:49 -08005283 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005284 final long identity = Binder.clearCallingIdentity();
5285 try {
chen xub97461a2018-10-26 14:17:57 -07005286 TelephonyPermissions
5287 .enforeceCallingOrSelfReadPrivilegedPhoneStatePermissionOrCarrierPrivilege(
5288 mApp, phone.getSubId(), "getRadioAccessFamily");
5289 raf = ProxyController.getInstance().getRadioAccessFamily(phoneId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005290 } finally {
5291 Binder.restoreCallingIdentity(identity);
5292 }
chen xub97461a2018-10-26 14:17:57 -07005293 return raf;
Wink Saville5d475dd2014-10-17 15:00:58 -07005294 }
Andrew Leedf14ead2014-10-17 14:22:52 -07005295
5296 @Override
5297 public void enableVideoCalling(boolean enable) {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08005298 final Phone defaultPhone = getDefaultPhone();
Andrew Leedf14ead2014-10-17 14:22:52 -07005299 enforceModifyPermission();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005300
5301 final long identity = Binder.clearCallingIdentity();
5302 try {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08005303 ImsManager.getInstance(defaultPhone.getContext(),
5304 defaultPhone.getPhoneId()).setVtSetting(enable);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005305 } finally {
5306 Binder.restoreCallingIdentity(identity);
5307 }
Andrew Leedf14ead2014-10-17 14:22:52 -07005308 }
5309
5310 @Override
Svet Ganovb320e182015-04-16 12:30:10 -07005311 public boolean isVideoCallingEnabled(String callingPackage) {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08005312 final Phone defaultPhone = getDefaultPhone();
Amit Mahajan578e53d2018-03-20 16:18:38 +00005313 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
Nazanin Bakhshif782e562018-12-11 15:15:39 -08005314 mApp, defaultPhone.getSubId(), callingPackage, "isVideoCallingEnabled")) {
Amit Mahajan578e53d2018-03-20 16:18:38 +00005315 return false;
5316 }
Svet Ganovb320e182015-04-16 12:30:10 -07005317
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005318 final long identity = Binder.clearCallingIdentity();
5319 try {
5320 // Check the user preference and the system-level IMS setting. Even if the user has
5321 // enabled video calling, if IMS is disabled we aren't able to support video calling.
5322 // In the long run, we may instead need to check if there exists a connection service
5323 // which can support video calling.
5324 ImsManager imsManager =
Nazanin Bakhshif782e562018-12-11 15:15:39 -08005325 ImsManager.getInstance(defaultPhone.getContext(), defaultPhone.getPhoneId());
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005326 return imsManager.isVtEnabledByPlatform()
5327 && imsManager.isEnhanced4gLteModeSettingEnabledByUser()
5328 && imsManager.isVtEnabledByUser();
5329 } finally {
5330 Binder.restoreCallingIdentity(identity);
5331 }
Andrew Leedf14ead2014-10-17 14:22:52 -07005332 }
Libin.Tang@motorola.comafe82642014-12-18 13:27:53 -06005333
Andrew Leea1239f22015-03-02 17:44:07 -08005334 @Override
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005335 public boolean canChangeDtmfToneLength(int subId, String callingPackage) {
5336 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
5337 mApp, subId, callingPackage, "isVideoCallingEnabled")) {
5338 return false;
5339 }
5340
5341 final long identity = Binder.clearCallingIdentity();
5342 try {
5343 CarrierConfigManager configManager =
5344 (CarrierConfigManager) mApp.getSystemService(Context.CARRIER_CONFIG_SERVICE);
Nazanin Bakhshif782e562018-12-11 15:15:39 -08005345 return configManager.getConfigForSubId(subId)
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005346 .getBoolean(CarrierConfigManager.KEY_DTMF_TYPE_ENABLED_BOOL);
5347 } finally {
5348 Binder.restoreCallingIdentity(identity);
5349 }
Andrew Leea1239f22015-03-02 17:44:07 -08005350 }
5351
5352 @Override
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005353 public boolean isWorldPhone(int subId, String callingPackage) {
5354 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
5355 mApp, subId, callingPackage, "isVideoCallingEnabled")) {
5356 return false;
5357 }
5358
5359 final long identity = Binder.clearCallingIdentity();
5360 try {
5361 CarrierConfigManager configManager =
5362 (CarrierConfigManager) mApp.getSystemService(Context.CARRIER_CONFIG_SERVICE);
Nazanin Bakhshif782e562018-12-11 15:15:39 -08005363 return configManager.getConfigForSubId(subId)
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005364 .getBoolean(CarrierConfigManager.KEY_WORLD_PHONE_BOOL);
5365 } finally {
5366 Binder.restoreCallingIdentity(identity);
5367 }
Andrew Leea1239f22015-03-02 17:44:07 -08005368 }
5369
Andrew Lee9431b832015-03-09 18:46:45 -07005370 @Override
5371 public boolean isTtyModeSupported() {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08005372 TelecomManager telecomManager = TelecomManager.from(mApp);
Wooki Wu1f82f7a2016-02-15 15:59:58 +08005373 return telecomManager.isTtySupported();
Andrew Lee9431b832015-03-09 18:46:45 -07005374 }
5375
5376 @Override
5377 public boolean isHearingAidCompatibilitySupported() {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005378 final long identity = Binder.clearCallingIdentity();
5379 try {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08005380 return mApp.getResources().getBoolean(R.bool.hac_enabled);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005381 } finally {
5382 Binder.restoreCallingIdentity(identity);
5383 }
Andrew Lee9431b832015-03-09 18:46:45 -07005384 }
5385
Hall Liuf6668912018-10-31 17:05:23 -07005386 /**
5387 * Determines whether the device currently supports RTT (Real-time text). Based both on carrier
5388 * support for the feature and device firmware support.
5389 *
5390 * @return {@code true} if the device and carrier both support RTT, {@code false} otherwise.
5391 */
5392 @Override
5393 public boolean isRttSupported(int subscriptionId) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005394 final long identity = Binder.clearCallingIdentity();
Nazanin Bakhshif782e562018-12-11 15:15:39 -08005395 final Phone phone = getPhone(subscriptionId);
5396 if (phone == null) {
5397 loge("isRttSupported: no Phone found. Invalid subId:" + subscriptionId);
5398 return false;
5399 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005400 try {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08005401 boolean isCarrierSupported = mApp.getCarrierConfigForSubId(subscriptionId).getBoolean(
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005402 CarrierConfigManager.KEY_RTT_SUPPORTED_BOOL);
5403 boolean isDeviceSupported =
Nazanin Bakhshif782e562018-12-11 15:15:39 -08005404 phone.getContext().getResources().getBoolean(R.bool.config_support_rtt);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005405 return isCarrierSupported && isDeviceSupported;
5406 } finally {
5407 Binder.restoreCallingIdentity(identity);
5408 }
Hall Liu98187582018-01-22 19:15:32 -08005409 }
5410
Hall Liuf6668912018-10-31 17:05:23 -07005411 /**
Hall Liuf2daa022019-07-23 18:39:00 -07005412 * Determines whether the user has turned on RTT. If the carrier wants to ignore the user-set
5413 * RTT setting, will return true if the device and carrier both support RTT.
5414 * Otherwise. only returns true if the device and carrier both also support RTT.
Hall Liuf6668912018-10-31 17:05:23 -07005415 */
5416 public boolean isRttEnabled(int subscriptionId) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005417 final long identity = Binder.clearCallingIdentity();
5418 try {
Hall Liuf2daa022019-07-23 18:39:00 -07005419 boolean isRttSupported = isRttSupported(subscriptionId);
5420 boolean isUserRttSettingOn = Settings.Secure.getInt(
Nazanin Bakhshif782e562018-12-11 15:15:39 -08005421 mApp.getContentResolver(), Settings.Secure.RTT_CALLING_MODE, 0) != 0;
Hall Liuf2daa022019-07-23 18:39:00 -07005422 boolean shouldIgnoreUserRttSetting = mApp.getCarrierConfigForSubId(subscriptionId)
5423 .getBoolean(CarrierConfigManager.KEY_IGNORE_RTT_MODE_SETTING_BOOL);
5424 return isRttSupported && (isUserRttSettingOn || shouldIgnoreUserRttSetting);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005425 } finally {
5426 Binder.restoreCallingIdentity(identity);
5427 }
Hall Liu3ad5f012018-04-06 16:23:39 -07005428 }
5429
Sanket Padawe7310cc72015-01-14 09:53:20 -08005430 /**
5431 * Returns the unique device ID of phone, for example, the IMEI for
5432 * GSM and the MEID for CDMA phones. Return null if device ID is not available.
5433 *
5434 * <p>Requires Permission:
5435 * {@link android.Manifest.permission#READ_PHONE_STATE READ_PHONE_STATE}
5436 */
5437 @Override
Svet Ganovb320e182015-04-16 12:30:10 -07005438 public String getDeviceId(String callingPackage) {
Sanket Padawe7310cc72015-01-14 09:53:20 -08005439 final Phone phone = PhoneFactory.getPhone(0);
Jeff Davidson913390f2018-02-23 17:11:49 -08005440 if (phone == null) {
Sanket Padawe7310cc72015-01-14 09:53:20 -08005441 return null;
5442 }
Jeff Davidson913390f2018-02-23 17:11:49 -08005443 int subId = phone.getSubId();
Michael Groover70af6dc2018-10-01 16:23:15 -07005444 if (!TelephonyPermissions.checkCallingOrSelfReadDeviceIdentifiers(mApp, subId,
5445 callingPackage, "getDeviceId")) {
Jeff Davidson913390f2018-02-23 17:11:49 -08005446 return null;
5447 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005448
5449 final long identity = Binder.clearCallingIdentity();
5450 try {
5451 return phone.getDeviceId();
5452 } finally {
5453 Binder.restoreCallingIdentity(identity);
5454 }
Sanket Padawe7310cc72015-01-14 09:53:20 -08005455 }
5456
Ping Sunc67b7c22016-03-02 19:16:45 +08005457 /**
5458 * {@hide}
5459 * Returns the IMS Registration Status on a particular subid
5460 *
5461 * @param subId
5462 */
Brad Ebinger1f2b5082018-02-08 16:11:32 -08005463 public boolean isImsRegistered(int subId) {
Ping Sunc67b7c22016-03-02 19:16:45 +08005464 Phone phone = getPhone(subId);
5465 if (phone != null) {
5466 return phone.isImsRegistered();
5467 } else {
5468 return false;
5469 }
5470 }
5471
Santos Cordon7a1885b2015-02-03 11:15:19 -08005472 @Override
5473 public int getSubIdForPhoneAccount(PhoneAccount phoneAccount) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005474 final long identity = Binder.clearCallingIdentity();
5475 try {
5476 return PhoneUtils.getSubIdForPhoneAccount(phoneAccount);
5477 } finally {
5478 Binder.restoreCallingIdentity(identity);
5479 }
Santos Cordon7a1885b2015-02-03 11:15:19 -08005480 }
Nathan Harolddcfc7932015-03-18 10:01:20 -07005481
Tyler Gunnf70ed162019-04-03 15:28:53 -07005482 @Override
5483 public @Nullable PhoneAccountHandle getPhoneAccountHandleForSubscriptionId(int subscriptionId) {
5484 final long identity = Binder.clearCallingIdentity();
5485 try {
5486 Phone phone = getPhone(subscriptionId);
5487 if (phone == null) {
5488 return null;
5489 }
5490 return PhoneUtils.makePstnPhoneAccountHandle(phone);
5491 } finally {
5492 Binder.restoreCallingIdentity(identity);
5493 }
5494 }
5495
Brad Ebinger1f2b5082018-02-08 16:11:32 -08005496 /**
5497 * @return the VoWiFi calling availability.
Nathan Haroldc55097a2015-03-11 18:14:50 -07005498 */
Brad Ebinger1f2b5082018-02-08 16:11:32 -08005499 public boolean isWifiCallingAvailable(int subId) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005500 final long identity = Binder.clearCallingIdentity();
5501 try {
5502 Phone phone = getPhone(subId);
5503 if (phone != null) {
5504 return phone.isWifiCallingEnabled();
5505 } else {
5506 return false;
5507 }
5508 } finally {
5509 Binder.restoreCallingIdentity(identity);
Brad Ebinger1f2b5082018-02-08 16:11:32 -08005510 }
Nathan Haroldc55097a2015-03-11 18:14:50 -07005511 }
5512
Brad Ebinger1f2b5082018-02-08 16:11:32 -08005513 /**
Brad Ebinger1f2b5082018-02-08 16:11:32 -08005514 * @return the VT calling availability.
Etan Cohen3b7a1bc2015-05-28 15:57:13 -07005515 */
Brad Ebinger1f2b5082018-02-08 16:11:32 -08005516 public boolean isVideoTelephonyAvailable(int subId) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005517 final long identity = Binder.clearCallingIdentity();
5518 try {
5519 Phone phone = getPhone(subId);
5520 if (phone != null) {
5521 return phone.isVideoEnabled();
5522 } else {
5523 return false;
5524 }
5525 } finally {
5526 Binder.restoreCallingIdentity(identity);
Brad Ebinger1f2b5082018-02-08 16:11:32 -08005527 }
5528 }
5529
5530 /**
5531 * @return the IMS registration technology for the MMTEL feature. Valid return values are
5532 * defined in {@link ImsRegistrationImplBase}.
5533 */
5534 public @ImsRegistrationImplBase.ImsRegistrationTech int getImsRegTechnologyForMmTel(int subId) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005535 final long identity = Binder.clearCallingIdentity();
5536 try {
5537 Phone phone = getPhone(subId);
5538 if (phone != null) {
5539 return phone.getImsRegistrationTech();
5540 } else {
5541 return ImsRegistrationImplBase.REGISTRATION_TECH_NONE;
5542 }
5543 } finally {
5544 Binder.restoreCallingIdentity(identity);
Brad Ebinger1f2b5082018-02-08 16:11:32 -08005545 }
Etan Cohen3b7a1bc2015-05-28 15:57:13 -07005546 }
5547
Stuart Scott8eef64f2015-04-08 15:13:54 -07005548 @Override
5549 public void factoryReset(int subId) {
5550 enforceConnectivityInternalPermission();
Stuart Scott981d8582015-04-21 14:09:50 -07005551 if (mUserManager.hasUserRestriction(UserManager.DISALLOW_NETWORK_RESET)) {
5552 return;
5553 }
5554
Svet Ganovcc087f82015-05-12 20:35:54 -07005555 final long identity = Binder.clearCallingIdentity();
Nazanin Bakhshif782e562018-12-11 15:15:39 -08005556
Svet Ganovcc087f82015-05-12 20:35:54 -07005557 try {
Stuart Scott981d8582015-04-21 14:09:50 -07005558 if (SubscriptionManager.isUsableSubIdValue(subId) && !mUserManager.hasUserRestriction(
5559 UserManager.DISALLOW_CONFIG_MOBILE_NETWORKS)) {
Pengquan Meng85728fb2018-03-12 16:31:21 -07005560 setUserDataEnabled(subId, getDefaultDataEnabled());
Svet Ganovcc087f82015-05-12 20:35:54 -07005561 setNetworkSelectionModeAutomatic(subId);
Pengquan Meng85728fb2018-03-12 16:31:21 -07005562 setPreferredNetworkType(subId, getDefaultNetworkType(subId));
Nazanin Bakhshif782e562018-12-11 15:15:39 -08005563 setDataRoamingEnabled(subId, getDefaultDataRoamingEnabled(subId));
5564 CarrierInfoManager.deleteAllCarrierKeysForImsiEncryption(mApp);
Svet Ganovcc087f82015-05-12 20:35:54 -07005565 }
Amit Mahajan7dbbd822019-03-13 17:33:47 -07005566 // There has been issues when Sms raw table somehow stores orphan
5567 // fragments. They lead to garbled message when new fragments come
5568 // in and combined with those stale ones. In case this happens again,
5569 // user can reset all network settings which will clean up this table.
5570 cleanUpSmsRawTable(getDefaultPhone().getContext());
Naina Nalluri1264a9f2019-09-17 14:10:30 -07005571
5572 // Erase modem config if erase modem on network setting is enabled.
5573 String configValue = DeviceConfig.getProperty(DeviceConfig.NAMESPACE_TELEPHONY,
5574 RESET_NETWORK_ERASE_MODEM_CONFIG_ENABLED);
5575 if (configValue != null && Boolean.parseBoolean(configValue)) {
5576 sendEraseModemConfig(getDefaultPhone());
5577 }
Svet Ganovcc087f82015-05-12 20:35:54 -07005578 } finally {
5579 Binder.restoreCallingIdentity(identity);
Stuart Scott8eef64f2015-04-08 15:13:54 -07005580 }
5581 }
Narayan Kamath1c496c22015-04-16 14:40:19 +01005582
Amit Mahajan7dbbd822019-03-13 17:33:47 -07005583 private void cleanUpSmsRawTable(Context context) {
5584 ContentResolver resolver = context.getContentResolver();
5585 Uri uri = Uri.withAppendedPath(Telephony.Sms.CONTENT_URI, "raw/permanentDelete");
5586 resolver.delete(uri, null, null);
5587 }
5588
Narayan Kamath1c496c22015-04-16 14:40:19 +01005589 @Override
chen xu5d3637b2019-01-21 23:31:38 -08005590 public String getSimLocaleForSubscriber(int subId) {
5591 enforceReadPrivilegedPermission("getSimLocaleForSubscriber, subId: " + subId);
5592 final Phone phone = getPhone(subId);
5593 if (phone == null) {
5594 log("getSimLocaleForSubscriber, invalid subId");
chen xu2bb91e42019-01-24 14:35:54 -08005595 return null;
chen xu5d3637b2019-01-21 23:31:38 -08005596 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005597 final long identity = Binder.clearCallingIdentity();
5598 try {
chen xu5d3637b2019-01-21 23:31:38 -08005599 final SubscriptionInfo info = mSubscriptionController.getActiveSubscriptionInfo(subId,
5600 phone.getContext().getOpPackageName());
chen xu6291c472019-02-04 12:55:53 -08005601 if (info == null) {
5602 log("getSimLocaleForSubscriber, inactive subId: " + subId);
5603 return null;
5604 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005605 // Try and fetch the locale from the carrier properties or from the SIM language
5606 // preferences (EF-PL and EF-LI)...
5607 final int mcc = info.getMcc();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005608 String simLanguage = null;
chen xu5d3637b2019-01-21 23:31:38 -08005609 final Locale localeFromDefaultSim = phone.getLocaleFromSimAndCarrierPrefs();
5610 if (localeFromDefaultSim != null) {
5611 if (!localeFromDefaultSim.getCountry().isEmpty()) {
5612 if (DBG) log("Using locale from subId: " + subId + " locale: "
5613 + localeFromDefaultSim);
5614 return localeFromDefaultSim.toLanguageTag();
5615 } else {
5616 simLanguage = localeFromDefaultSim.getLanguage();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005617 }
5618 }
Narayan Kamath1c496c22015-04-16 14:40:19 +01005619
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005620 // The SIM language preferences only store a language (e.g. fr = French), not an
5621 // exact locale (e.g. fr_FR = French/France). So, if the locale returned from
5622 // the SIM and carrier preferences does not include a country we add the country
5623 // determined from the SIM MCC to provide an exact locale.
Nazanin Bakhshif782e562018-12-11 15:15:39 -08005624 final Locale mccLocale = MccTable.getLocaleFromMcc(mApp, mcc, simLanguage);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005625 if (mccLocale != null) {
chen xu5d3637b2019-01-21 23:31:38 -08005626 if (DBG) log("No locale from SIM, using mcc locale:" + mccLocale);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005627 return mccLocale.toLanguageTag();
5628 }
5629
5630 if (DBG) log("No locale found - returning null");
5631 return null;
5632 } finally {
5633 Binder.restoreCallingIdentity(identity);
5634 }
Narayan Kamath1c496c22015-04-16 14:40:19 +01005635 }
5636
5637 private List<SubscriptionInfo> getAllSubscriptionInfoList() {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08005638 return mSubscriptionController.getAllSubInfoList(mApp.getOpPackageName());
Narayan Kamath1c496c22015-04-16 14:40:19 +01005639 }
5640
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005641 /**
5642 * NOTE: this method assumes permission checks are done and caller identity has been cleared.
5643 */
5644 private List<SubscriptionInfo> getActiveSubscriptionInfoListPrivileged() {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08005645 return mSubscriptionController.getActiveSubscriptionInfoList(mApp.getOpPackageName());
Narayan Kamath1c496c22015-04-16 14:40:19 +01005646 }
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -07005647
Chenjie Yu1ba97252018-01-11 18:16:20 -08005648 private final ModemActivityInfo mLastModemActivityInfo =
5649 new ModemActivityInfo(0, 0, 0, new int[0], 0, 0);
5650
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -07005651 /**
Adam Lesinski903a54c2016-04-11 14:49:52 -07005652 * Responds to the ResultReceiver with the {@link android.telephony.ModemActivityInfo} object
5653 * representing the state of the modem.
5654 *
Chenjie Yu1ba97252018-01-11 18:16:20 -08005655 * NOTE: The underlying implementation clears the modem state, so there should only ever be one
5656 * caller to it. Everyone should call this class to get cumulative data.
Adam Lesinski903a54c2016-04-11 14:49:52 -07005657 * @hide
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -07005658 */
5659 @Override
Adam Lesinski903a54c2016-04-11 14:49:52 -07005660 public void requestModemActivityInfo(ResultReceiver result) {
5661 enforceModifyPermission();
vagdeviaf9a5b92018-08-15 16:01:53 -07005662 WorkSource workSource = getWorkSource(Binder.getCallingUid());
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005663
5664 final long identity = Binder.clearCallingIdentity();
5665 try {
5666 ModemActivityInfo ret = null;
5667 synchronized (mLastModemActivityInfo) {
5668 ModemActivityInfo info = (ModemActivityInfo) sendRequest(
5669 CMD_GET_MODEM_ACTIVITY_INFO,
vagdeviaf9a5b92018-08-15 16:01:53 -07005670 null, workSource);
Siddharth Rayb8114062018-06-17 15:02:38 -07005671 if (isModemActivityInfoValid(info)) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005672 int[] mergedTxTimeMs = new int[ModemActivityInfo.TX_POWER_LEVELS];
5673 for (int i = 0; i < mergedTxTimeMs.length; i++) {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08005674 mergedTxTimeMs[i] = info.getTxTimeMillis()[i]
5675 + mLastModemActivityInfo.getTxTimeMillis()[i];
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005676 }
5677 mLastModemActivityInfo.setTimestamp(info.getTimestamp());
Nazanin Bakhshif782e562018-12-11 15:15:39 -08005678 mLastModemActivityInfo.setSleepTimeMillis(info.getSleepTimeMillis()
5679 + mLastModemActivityInfo.getSleepTimeMillis());
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005680 mLastModemActivityInfo.setIdleTimeMillis(
5681 info.getIdleTimeMillis() + mLastModemActivityInfo.getIdleTimeMillis());
5682 mLastModemActivityInfo.setTxTimeMillis(mergedTxTimeMs);
5683 mLastModemActivityInfo.setRxTimeMillis(
5684 info.getRxTimeMillis() + mLastModemActivityInfo.getRxTimeMillis());
5685 mLastModemActivityInfo.setEnergyUsed(
5686 info.getEnergyUsed() + mLastModemActivityInfo.getEnergyUsed());
Chenjie Yu1ba97252018-01-11 18:16:20 -08005687 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005688 ret = new ModemActivityInfo(mLastModemActivityInfo.getTimestamp(),
5689 mLastModemActivityInfo.getSleepTimeMillis(),
5690 mLastModemActivityInfo.getIdleTimeMillis(),
5691 mLastModemActivityInfo.getTxTimeMillis(),
5692 mLastModemActivityInfo.getRxTimeMillis(),
5693 mLastModemActivityInfo.getEnergyUsed());
Chenjie Yu1ba97252018-01-11 18:16:20 -08005694 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005695 Bundle bundle = new Bundle();
5696 bundle.putParcelable(TelephonyManager.MODEM_ACTIVITY_RESULT_KEY, ret);
5697 result.send(0, bundle);
5698 } finally {
5699 Binder.restoreCallingIdentity(identity);
Chenjie Yu1ba97252018-01-11 18:16:20 -08005700 }
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -07005701 }
Jack Yu85bd38a2015-11-09 11:34:32 -08005702
Siddharth Rayb8114062018-06-17 15:02:38 -07005703 // Checks that ModemActivityInfo is valid. Sleep time, Idle time, Rx time and Tx time should be
5704 // less than total activity duration.
5705 private boolean isModemActivityInfoValid(ModemActivityInfo info) {
5706 if (info == null) {
5707 return false;
5708 }
5709 int activityDurationMs =
5710 (int) (info.getTimestamp() - mLastModemActivityInfo.getTimestamp());
5711 int totalTxTimeMs = 0;
5712 for (int i = 0; i < info.getTxTimeMillis().length; i++) {
5713 totalTxTimeMs += info.getTxTimeMillis()[i];
5714 }
5715 return (info.isValid()
5716 && (info.getSleepTimeMillis() <= activityDurationMs)
5717 && (info.getIdleTimeMillis() <= activityDurationMs)
5718 && (info.getRxTimeMillis() <= activityDurationMs)
5719 && (totalTxTimeMs <= activityDurationMs));
5720 }
5721
Jack Yu85bd38a2015-11-09 11:34:32 -08005722 /**
5723 * {@hide}
5724 * Returns the service state information on specified subscription.
5725 */
5726 @Override
5727 public ServiceState getServiceStateForSubscriber(int subId, String callingPackage) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08005728 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
Jeff Davidson913390f2018-02-23 17:11:49 -08005729 mApp, subId, callingPackage, "getServiceStateForSubscriber")) {
Jack Yu85bd38a2015-11-09 11:34:32 -08005730 return null;
5731 }
5732
Hall Liuf19c44f2018-11-27 14:38:17 -08005733 LocationAccessPolicy.LocationPermissionResult fineLocationResult =
5734 LocationAccessPolicy.checkLocationPermission(mApp,
5735 new LocationAccessPolicy.LocationPermissionQuery.Builder()
5736 .setCallingPackage(callingPackage)
5737 .setCallingPid(Binder.getCallingPid())
5738 .setCallingUid(Binder.getCallingUid())
5739 .setMethod("getServiceStateForSubscriber")
Hall Liuf18a0cf2019-04-17 13:37:11 -07005740 .setLogAsInfo(true)
Hall Liuf19c44f2018-11-27 14:38:17 -08005741 .setMinSdkVersionForFine(Build.VERSION_CODES.Q)
5742 .build());
5743
5744 LocationAccessPolicy.LocationPermissionResult coarseLocationResult =
5745 LocationAccessPolicy.checkLocationPermission(mApp,
5746 new LocationAccessPolicy.LocationPermissionQuery.Builder()
5747 .setCallingPackage(callingPackage)
5748 .setCallingPid(Binder.getCallingPid())
5749 .setCallingUid(Binder.getCallingUid())
5750 .setMethod("getServiceStateForSubscriber")
Hall Liuf18a0cf2019-04-17 13:37:11 -07005751 .setLogAsInfo(true)
Hall Liuf19c44f2018-11-27 14:38:17 -08005752 .setMinSdkVersionForCoarse(Build.VERSION_CODES.Q)
5753 .build());
5754 // We don't care about hard or soft here -- all we need to know is how much info to scrub.
5755 boolean hasFinePermission =
5756 fineLocationResult == LocationAccessPolicy.LocationPermissionResult.ALLOWED;
5757 boolean hasCoarsePermission =
5758 coarseLocationResult == LocationAccessPolicy.LocationPermissionResult.ALLOWED;
5759
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005760 final long identity = Binder.clearCallingIdentity();
5761 try {
5762 final Phone phone = getPhone(subId);
5763 if (phone == null) {
5764 return null;
5765 }
Jack Yu85bd38a2015-11-09 11:34:32 -08005766
Hall Liuf19c44f2018-11-27 14:38:17 -08005767 ServiceState ss = phone.getServiceState();
5768
5769 // Scrub out the location info in ServiceState depending on what level of access
5770 // the caller has.
5771 if (hasFinePermission) return ss;
5772 if (hasCoarsePermission) return ss.sanitizeLocationInfo(false);
5773 return ss.sanitizeLocationInfo(true);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005774 } finally {
5775 Binder.restoreCallingIdentity(identity);
5776 }
Jack Yu85bd38a2015-11-09 11:34:32 -08005777 }
Nancy Chen31f9ba12016-01-06 11:42:12 -08005778
5779 /**
5780 * Returns the URI for the per-account voicemail ringtone set in Phone settings.
5781 *
5782 * @param accountHandle The handle for the {@link PhoneAccount} for which to retrieve the
5783 * voicemail ringtone.
5784 * @return The URI for the ringtone to play when receiving a voicemail from a specific
5785 * PhoneAccount.
5786 */
5787 @Override
5788 public Uri getVoicemailRingtoneUri(PhoneAccountHandle accountHandle) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005789 final long identity = Binder.clearCallingIdentity();
5790 try {
5791 Phone phone = PhoneUtils.getPhoneForPhoneAccountHandle(accountHandle);
5792 if (phone == null) {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08005793 phone = getDefaultPhone();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005794 }
Nancy Chen31f9ba12016-01-06 11:42:12 -08005795
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005796 return VoicemailNotificationSettingsUtil.getRingtoneUri(phone.getContext());
5797 } finally {
5798 Binder.restoreCallingIdentity(identity);
5799 }
Nancy Chen31f9ba12016-01-06 11:42:12 -08005800 }
5801
5802 /**
Ta-wei Yenc33877d2017-01-23 18:11:21 -08005803 * Sets the per-account voicemail ringtone.
5804 *
5805 * <p>Requires that the calling app is the default dialer, or has carrier privileges, or
5806 * has permission {@link android.Manifest.permission#MODIFY_PHONE_STATE MODIFY_PHONE_STATE}.
5807 *
5808 * @param phoneAccountHandle The handle for the {@link PhoneAccount} for which to set the
5809 * voicemail ringtone.
5810 * @param uri The URI for the ringtone to play when receiving a voicemail from a specific
5811 * PhoneAccount.
5812 */
5813 @Override
5814 public void setVoicemailRingtoneUri(String callingPackage,
5815 PhoneAccountHandle phoneAccountHandle, Uri uri) {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08005816 final Phone defaultPhone = getDefaultPhone();
Ta-wei Yenc33877d2017-01-23 18:11:21 -08005817 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
5818 if (!TextUtils.equals(callingPackage,
Nazanin Bakhshif782e562018-12-11 15:15:39 -08005819 TelecomManager.from(defaultPhone.getContext()).getDefaultDialerPackage())) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08005820 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
5821 mApp, PhoneUtils.getSubIdForPhoneAccountHandle(phoneAccountHandle),
5822 "setVoicemailRingtoneUri");
Ta-wei Yenc33877d2017-01-23 18:11:21 -08005823 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005824
5825 final long identity = Binder.clearCallingIdentity();
5826 try {
5827 Phone phone = PhoneUtils.getPhoneForPhoneAccountHandle(phoneAccountHandle);
5828 if (phone == null) {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08005829 phone = defaultPhone;
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005830 }
5831 VoicemailNotificationSettingsUtil.setRingtoneUri(phone.getContext(), uri);
5832 } finally {
5833 Binder.restoreCallingIdentity(identity);
Ta-wei Yenc33877d2017-01-23 18:11:21 -08005834 }
Ta-wei Yenc33877d2017-01-23 18:11:21 -08005835 }
5836
5837 /**
Nancy Chen31f9ba12016-01-06 11:42:12 -08005838 * Returns whether vibration is set for voicemail notification in Phone settings.
5839 *
5840 * @param accountHandle The handle for the {@link PhoneAccount} for which to retrieve the
5841 * voicemail vibration setting.
5842 * @return {@code true} if the vibration is set for this PhoneAccount, {@code false} otherwise.
5843 */
5844 @Override
5845 public boolean isVoicemailVibrationEnabled(PhoneAccountHandle accountHandle) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005846 final long identity = Binder.clearCallingIdentity();
5847 try {
5848 Phone phone = PhoneUtils.getPhoneForPhoneAccountHandle(accountHandle);
5849 if (phone == null) {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08005850 phone = getDefaultPhone();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005851 }
Nancy Chen31f9ba12016-01-06 11:42:12 -08005852
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005853 return VoicemailNotificationSettingsUtil.isVibrationEnabled(phone.getContext());
5854 } finally {
5855 Binder.restoreCallingIdentity(identity);
5856 }
Nancy Chen31f9ba12016-01-06 11:42:12 -08005857 }
5858
Youhan Wange64578a2016-05-02 15:32:42 -07005859 /**
Ta-wei Yenc33877d2017-01-23 18:11:21 -08005860 * Sets the per-account voicemail vibration.
5861 *
5862 * <p>Requires that the calling app is the default dialer, or has carrier privileges, or
5863 * has permission {@link android.Manifest.permission#MODIFY_PHONE_STATE MODIFY_PHONE_STATE}.
5864 *
5865 * @param phoneAccountHandle The handle for the {@link PhoneAccount} for which to set the
5866 * voicemail vibration setting.
5867 * @param enabled Whether to enable or disable vibration for voicemail notifications from a
5868 * specific PhoneAccount.
5869 */
5870 @Override
5871 public void setVoicemailVibrationEnabled(String callingPackage,
5872 PhoneAccountHandle phoneAccountHandle, boolean enabled) {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08005873 final Phone defaultPhone = getDefaultPhone();
Ta-wei Yenc33877d2017-01-23 18:11:21 -08005874 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
5875 if (!TextUtils.equals(callingPackage,
Nazanin Bakhshif782e562018-12-11 15:15:39 -08005876 TelecomManager.from(defaultPhone.getContext()).getDefaultDialerPackage())) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08005877 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
5878 mApp, PhoneUtils.getSubIdForPhoneAccountHandle(phoneAccountHandle),
5879 "setVoicemailVibrationEnabled");
Ta-wei Yenc33877d2017-01-23 18:11:21 -08005880 }
5881
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005882 final long identity = Binder.clearCallingIdentity();
5883 try {
5884 Phone phone = PhoneUtils.getPhoneForPhoneAccountHandle(phoneAccountHandle);
5885 if (phone == null) {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08005886 phone = defaultPhone;
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005887 }
5888 VoicemailNotificationSettingsUtil.setVibrationEnabled(phone.getContext(), enabled);
5889 } finally {
5890 Binder.restoreCallingIdentity(identity);
Ta-wei Yenc33877d2017-01-23 18:11:21 -08005891 }
Ta-wei Yenc33877d2017-01-23 18:11:21 -08005892 }
5893
5894 /**
Youhan Wange64578a2016-05-02 15:32:42 -07005895 * Make sure either called from same process as self (phone) or IPC caller has read privilege.
5896 *
5897 * @throws SecurityException if the caller does not have the required permission
5898 */
Brad Ebinger35c841c2018-10-01 10:40:55 -07005899 private void enforceReadPrivilegedPermission(String message) {
Youhan Wange64578a2016-05-02 15:32:42 -07005900 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.READ_PRIVILEGED_PHONE_STATE,
Brad Ebinger35c841c2018-10-01 10:40:55 -07005901 message);
Youhan Wange64578a2016-05-02 15:32:42 -07005902 }
5903
5904 /**
Ta-wei Yen30a69c82016-12-27 14:52:32 -08005905 * Make sure either called from same process as self (phone) or IPC caller has send SMS
5906 * permission.
5907 *
5908 * @throws SecurityException if the caller does not have the required permission
5909 */
5910 private void enforceSendSmsPermission() {
5911 mApp.enforceCallingOrSelfPermission(permission.SEND_SMS, null);
5912 }
5913
5914 /**
Ta-wei Yen527a9c02017-01-06 15:29:25 -08005915 * Make sure called from the package in charge of visual voicemail.
Ta-wei Yen30a69c82016-12-27 14:52:32 -08005916 *
Ta-wei Yen527a9c02017-01-06 15:29:25 -08005917 * @throws SecurityException if the caller is not the visual voicemail package.
Ta-wei Yen30a69c82016-12-27 14:52:32 -08005918 */
Ta-wei Yen527a9c02017-01-06 15:29:25 -08005919 private void enforceVisualVoicemailPackage(String callingPackage, int subId) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005920 final long identity = Binder.clearCallingIdentity();
5921 try {
5922 ComponentName componentName =
Nazanin Bakhshif782e562018-12-11 15:15:39 -08005923 RemoteVvmTaskManager.getRemotePackage(mApp, subId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005924 if (componentName == null) {
5925 throw new SecurityException(
5926 "Caller not current active visual voicemail package[null]");
5927 }
5928 String vvmPackage = componentName.getPackageName();
5929 if (!callingPackage.equals(vvmPackage)) {
5930 throw new SecurityException("Caller not current active visual voicemail package["
5931 + vvmPackage + "]");
5932 }
5933 } finally {
5934 Binder.restoreCallingIdentity(identity);
Ta-wei Yen30a69c82016-12-27 14:52:32 -08005935 }
5936 }
5937
5938 /**
Youhan Wange64578a2016-05-02 15:32:42 -07005939 * Return the application ID for the app type.
5940 *
5941 * @param subId the subscription ID that this request applies to.
5942 * @param appType the uicc app type.
5943 * @return Application ID for specificied app type, or null if no uicc.
5944 */
5945 @Override
5946 public String getAidForAppType(int subId, int appType) {
Brad Ebinger35c841c2018-10-01 10:40:55 -07005947 enforceReadPrivilegedPermission("getAidForAppType");
Youhan Wange64578a2016-05-02 15:32:42 -07005948 Phone phone = getPhone(subId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005949
5950 final long identity = Binder.clearCallingIdentity();
Youhan Wange64578a2016-05-02 15:32:42 -07005951 try {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005952 if (phone == null) {
5953 return null;
5954 }
5955 String aid = null;
5956 try {
5957 aid = UiccController.getInstance().getUiccCard(phone.getPhoneId())
5958 .getApplicationByType(appType).getAid();
5959 } catch (Exception e) {
5960 Log.e(LOG_TAG, "Not getting aid. Exception ex=" + e);
5961 }
5962 return aid;
5963 } finally {
5964 Binder.restoreCallingIdentity(identity);
Youhan Wange64578a2016-05-02 15:32:42 -07005965 }
Youhan Wange64578a2016-05-02 15:32:42 -07005966 }
5967
Youhan Wang4001d252016-05-11 10:29:41 -07005968 /**
5969 * Return the Electronic Serial Number.
5970 *
5971 * @param subId the subscription ID that this request applies to.
5972 * @return ESN or null if error.
5973 */
5974 @Override
5975 public String getEsn(int subId) {
Brad Ebinger35c841c2018-10-01 10:40:55 -07005976 enforceReadPrivilegedPermission("getEsn");
Youhan Wang4001d252016-05-11 10:29:41 -07005977 Phone phone = getPhone(subId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005978
5979 final long identity = Binder.clearCallingIdentity();
Youhan Wang4001d252016-05-11 10:29:41 -07005980 try {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005981 if (phone == null) {
5982 return null;
5983 }
5984 String esn = null;
5985 try {
5986 esn = phone.getEsn();
5987 } catch (Exception e) {
5988 Log.e(LOG_TAG, "Not getting ESN. Exception ex=" + e);
5989 }
5990 return esn;
5991 } finally {
5992 Binder.restoreCallingIdentity(identity);
Youhan Wang4001d252016-05-11 10:29:41 -07005993 }
Youhan Wang4001d252016-05-11 10:29:41 -07005994 }
5995
Sanket Padawe99ef1e32016-05-18 16:12:33 -07005996 /**
Youhan Wang66ad5d72016-07-18 17:56:58 -07005997 * Return the Preferred Roaming List Version.
5998 *
5999 * @param subId the subscription ID that this request applies to.
6000 * @return PRLVersion or null if error.
6001 */
6002 @Override
6003 public String getCdmaPrlVersion(int subId) {
Brad Ebinger35c841c2018-10-01 10:40:55 -07006004 enforceReadPrivilegedPermission("getCdmaPrlVersion");
Youhan Wang66ad5d72016-07-18 17:56:58 -07006005 Phone phone = getPhone(subId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006006
6007 final long identity = Binder.clearCallingIdentity();
Youhan Wang66ad5d72016-07-18 17:56:58 -07006008 try {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006009 if (phone == null) {
6010 return null;
6011 }
6012 String cdmaPrlVersion = null;
6013 try {
6014 cdmaPrlVersion = phone.getCdmaPrlVersion();
6015 } catch (Exception e) {
6016 Log.e(LOG_TAG, "Not getting PRLVersion", e);
6017 }
6018 return cdmaPrlVersion;
6019 } finally {
6020 Binder.restoreCallingIdentity(identity);
Youhan Wang66ad5d72016-07-18 17:56:58 -07006021 }
Youhan Wang66ad5d72016-07-18 17:56:58 -07006022 }
6023
6024 /**
Sanket Padawe99ef1e32016-05-18 16:12:33 -07006025 * Get snapshot of Telephony histograms
6026 * @return List of Telephony histograms
6027 * @hide
6028 */
6029 @Override
6030 public List<TelephonyHistogram> getTelephonyHistograms() {
Jeff Davidson7e17e312018-02-13 18:17:36 -08006031 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
6032 mApp, getDefaultSubscription(), "getTelephonyHistograms");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006033
6034 final long identity = Binder.clearCallingIdentity();
6035 try {
6036 return RIL.getTelephonyRILTimingHistograms();
6037 } finally {
6038 Binder.restoreCallingIdentity(identity);
6039 }
Sanket Padawe99ef1e32016-05-18 16:12:33 -07006040 }
Meng Wang1a7c35a2016-05-05 20:56:15 -07006041
6042 /**
6043 * {@hide}
Michele Berionne482f8202018-11-27 18:57:59 -08006044 * Set the allowed carrier list and the excluded carrier list, indicating the priority between
6045 * the two lists.
Meng Wang1a7c35a2016-05-05 20:56:15 -07006046 * Require system privileges. In the future we may add this to carrier APIs.
6047 *
Michele Berionne482f8202018-11-27 18:57:59 -08006048 * @return Integer with the result of the operation, as defined in {@link TelephonyManager}.
Meng Wang1a7c35a2016-05-05 20:56:15 -07006049 */
6050 @Override
Michele Berionne482f8202018-11-27 18:57:59 -08006051 @TelephonyManager.SetCarrierRestrictionResult
6052 public int setAllowedCarriers(CarrierRestrictionRules carrierRestrictionRules) {
Meng Wang1a7c35a2016-05-05 20:56:15 -07006053 enforceModifyPermission();
vagdeviaf9a5b92018-08-15 16:01:53 -07006054 WorkSource workSource = getWorkSource(Binder.getCallingUid());
Sanket Padawe13bac7b2017-03-20 15:04:47 -07006055
Michele Berionne482f8202018-11-27 18:57:59 -08006056 if (carrierRestrictionRules == null) {
6057 throw new NullPointerException("carrier restriction cannot be null");
Meng Wang9b7c4e92017-02-17 11:41:27 -08006058 }
Sanket Padawe13bac7b2017-03-20 15:04:47 -07006059
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006060 final long identity = Binder.clearCallingIdentity();
6061 try {
Michele Berionne482f8202018-11-27 18:57:59 -08006062 return (int) sendRequest(CMD_SET_ALLOWED_CARRIERS, carrierRestrictionRules,
vagdeviaf9a5b92018-08-15 16:01:53 -07006063 workSource);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006064 } finally {
6065 Binder.restoreCallingIdentity(identity);
6066 }
Meng Wang1a7c35a2016-05-05 20:56:15 -07006067 }
6068
6069 /**
6070 * {@hide}
Michele Berionne482f8202018-11-27 18:57:59 -08006071 * Get the allowed carrier list and the excluded carrier list, including the priority between
6072 * the two lists.
Meng Wang1a7c35a2016-05-05 20:56:15 -07006073 * Require system privileges. In the future we may add this to carrier APIs.
6074 *
Michele Berionne482f8202018-11-27 18:57:59 -08006075 * @return {@link android.telephony.CarrierRestrictionRules}
Meng Wang1a7c35a2016-05-05 20:56:15 -07006076 */
6077 @Override
Michele Berionne482f8202018-11-27 18:57:59 -08006078 public CarrierRestrictionRules getAllowedCarriers() {
Brad Ebinger35c841c2018-10-01 10:40:55 -07006079 enforceReadPrivilegedPermission("getAllowedCarriers");
vagdeviaf9a5b92018-08-15 16:01:53 -07006080 WorkSource workSource = getWorkSource(Binder.getCallingUid());
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006081
6082 final long identity = Binder.clearCallingIdentity();
6083 try {
Michele Berionne482f8202018-11-27 18:57:59 -08006084 Object response = sendRequest(CMD_GET_ALLOWED_CARRIERS, null, workSource);
6085 if (response instanceof CarrierRestrictionRules) {
6086 return (CarrierRestrictionRules) response;
6087 }
6088 // Response is an Exception of some kind,
6089 // which is signalled to the user as a NULL retval
6090 return null;
6091 } catch (Exception e) {
6092 Log.e(LOG_TAG, "getAllowedCarriers. Exception ex=" + e);
6093 return null;
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006094 } finally {
6095 Binder.restoreCallingIdentity(identity);
6096 }
Meng Wang1a7c35a2016-05-05 20:56:15 -07006097 }
6098
fionaxu59545b42016-05-25 15:53:37 -07006099 /**
6100 * Action set from carrier signalling broadcast receivers to enable/disable metered apns
6101 * @param subId the subscription ID that this action applies to.
6102 * @param enabled control enable or disable metered apns.
6103 * {@hide}
6104 */
6105 @Override
6106 public void carrierActionSetMeteredApnsEnabled(int subId, boolean enabled) {
6107 enforceModifyPermission();
6108 final Phone phone = getPhone(subId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006109
6110 final long identity = Binder.clearCallingIdentity();
fionaxu59545b42016-05-25 15:53:37 -07006111 if (phone == null) {
6112 loge("carrierAction: SetMeteredApnsEnabled fails with invalid subId: " + subId);
6113 return;
6114 }
6115 try {
6116 phone.carrierActionSetMeteredApnsEnabled(enabled);
6117 } catch (Exception e) {
6118 Log.e(LOG_TAG, "carrierAction: SetMeteredApnsEnabled fails. Exception ex=" + e);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006119 } finally {
6120 Binder.restoreCallingIdentity(identity);
fionaxu59545b42016-05-25 15:53:37 -07006121 }
6122 }
6123
6124 /**
6125 * Action set from carrier signalling broadcast receivers to enable/disable radio
6126 * @param subId the subscription ID that this action applies to.
6127 * @param enabled control enable or disable radio.
6128 * {@hide}
6129 */
6130 @Override
6131 public void carrierActionSetRadioEnabled(int subId, boolean enabled) {
6132 enforceModifyPermission();
6133 final Phone phone = getPhone(subId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006134
6135 final long identity = Binder.clearCallingIdentity();
fionaxu59545b42016-05-25 15:53:37 -07006136 if (phone == null) {
6137 loge("carrierAction: SetRadioEnabled fails with invalid sibId: " + subId);
6138 return;
6139 }
6140 try {
6141 phone.carrierActionSetRadioEnabled(enabled);
6142 } catch (Exception e) {
6143 Log.e(LOG_TAG, "carrierAction: SetRadioEnabled fails. Exception ex=" + e);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006144 } finally {
6145 Binder.restoreCallingIdentity(identity);
fionaxu59545b42016-05-25 15:53:37 -07006146 }
6147 }
6148
Ta-wei Yenc236d6b2016-06-21 13:33:12 -07006149 /**
fionaxu8da9cb12017-05-23 15:02:46 -07006150 * Action set from carrier signalling broadcast receivers to start/stop reporting the default
6151 * network status based on which carrier apps could apply actions accordingly,
6152 * enable/disable default url handler for example.
6153 *
6154 * @param subId the subscription ID that this action applies to.
6155 * @param report control start/stop reporting the default network status.
6156 * {@hide}
6157 */
6158 @Override
6159 public void carrierActionReportDefaultNetworkStatus(int subId, boolean report) {
6160 enforceModifyPermission();
6161 final Phone phone = getPhone(subId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006162
6163 final long identity = Binder.clearCallingIdentity();
fionaxu8da9cb12017-05-23 15:02:46 -07006164 if (phone == null) {
6165 loge("carrierAction: ReportDefaultNetworkStatus fails with invalid sibId: " + subId);
6166 return;
6167 }
6168 try {
6169 phone.carrierActionReportDefaultNetworkStatus(report);
6170 } catch (Exception e) {
6171 Log.e(LOG_TAG, "carrierAction: ReportDefaultNetworkStatus fails. Exception ex=" + e);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006172 } finally {
6173 Binder.restoreCallingIdentity(identity);
fionaxu8da9cb12017-05-23 15:02:46 -07006174 }
6175 }
6176
6177 /**
fionaxud9622282017-07-17 17:51:30 -07006178 * Action set from carrier signalling broadcast receivers to reset all carrier actions
6179 * @param subId the subscription ID that this action applies to.
6180 * {@hide}
6181 */
6182 @Override
6183 public void carrierActionResetAll(int subId) {
6184 enforceModifyPermission();
6185 final Phone phone = getPhone(subId);
6186 if (phone == null) {
6187 loge("carrierAction: ResetAll fails with invalid sibId: " + subId);
6188 return;
6189 }
6190 try {
6191 phone.carrierActionResetAll();
6192 } catch (Exception e) {
6193 Log.e(LOG_TAG, "carrierAction: ResetAll fails. Exception ex=" + e);
6194 }
6195 }
6196
6197 /**
Ta-wei Yenc236d6b2016-06-21 13:33:12 -07006198 * Called when "adb shell dumpsys phone" is invoked. Dump is also automatically invoked when a
6199 * bug report is being generated.
6200 */
6201 @Override
Ta-wei Yen99282e02016-06-21 18:19:35 -07006202 protected void dump(FileDescriptor fd, PrintWriter writer, String[] args) {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08006203 if (mApp.checkCallingOrSelfPermission(android.Manifest.permission.DUMP)
6204 != PackageManager.PERMISSION_GRANTED) {
dcashman22b950d2016-06-27 11:39:02 -07006205 writer.println("Permission Denial: can't dump Phone from pid="
6206 + Binder.getCallingPid()
6207 + ", uid=" + Binder.getCallingUid()
6208 + "without permission "
6209 + android.Manifest.permission.DUMP);
6210 return;
6211 }
Nazanin Bakhshif782e562018-12-11 15:15:39 -08006212 DumpsysHandler.dump(mApp, fd, writer, args);
Ta-wei Yenc236d6b2016-06-21 13:33:12 -07006213 }
Jack Yueb89b242016-06-22 13:27:47 -07006214
Brad Ebingerdac2f002018-04-03 15:17:52 -07006215 @Override
6216 public void onShellCommand(FileDescriptor in, FileDescriptor out, FileDescriptor err,
6217 String[] args, ShellCallback callback, ResultReceiver resultReceiver)
6218 throws RemoteException {
6219 (new TelephonyShellCommand(this)).exec(this, in, out, err, args, callback, resultReceiver);
6220 }
6221
Jack Yueb89b242016-06-22 13:27:47 -07006222 /**
Jack Yu84291ec2017-05-26 16:07:50 -07006223 * Get aggregated video call data usage since boot.
6224 *
6225 * @param perUidStats True if requesting data usage per uid, otherwise overall usage.
6226 * @return Snapshot of video call data usage
Jack Yueb89b242016-06-22 13:27:47 -07006227 * {@hide}
6228 */
6229 @Override
Jack Yu84291ec2017-05-26 16:07:50 -07006230 public NetworkStats getVtDataUsage(int subId, boolean perUidStats) {
Jack Yueb89b242016-06-22 13:27:47 -07006231 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.READ_NETWORK_USAGE_HISTORY,
6232 null);
6233
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006234 final long identity = Binder.clearCallingIdentity();
6235 try {
6236 // NetworkStatsService keeps tracking the active network interface and identity. It
6237 // records the delta with the corresponding network identity.
6238 // We just return the total video call data usage snapshot since boot.
6239 Phone phone = getPhone(subId);
6240 if (phone != null) {
6241 return phone.getVtDataUsage(perUidStats);
6242 }
6243 return null;
6244 } finally {
6245 Binder.restoreCallingIdentity(identity);
Jack Yueb89b242016-06-22 13:27:47 -07006246 }
Jack Yueb89b242016-06-22 13:27:47 -07006247 }
Jack Yu75ab2952016-07-08 14:29:33 -07006248
6249 /**
6250 * Policy control of data connection. Usually used when data limit is passed.
6251 * @param enabled True if enabling the data, otherwise disabling.
6252 * @param subId Subscription index
6253 * {@hide}
6254 */
6255 @Override
6256 public void setPolicyDataEnabled(boolean enabled, int subId) {
6257 enforceModifyPermission();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006258
6259 final long identity = Binder.clearCallingIdentity();
6260 try {
6261 Phone phone = getPhone(subId);
6262 if (phone != null) {
Jack Yud79fba22018-12-13 11:51:28 -08006263 phone.getDataEnabledSettings().setPolicyDataEnabled(enabled);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006264 }
6265 } finally {
6266 Binder.restoreCallingIdentity(identity);
Jack Yu75ab2952016-07-08 14:29:33 -07006267 }
6268 }
Sooraj Sasindran9a909312016-07-18 11:57:25 -07006269
6270 /**
6271 * Get Client request stats
6272 * @return List of Client Request Stats
6273 * @hide
6274 */
6275 @Override
6276 public List<ClientRequestStats> getClientRequestStats(String callingPackage, int subId) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08006277 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
Jeff Davidson913390f2018-02-23 17:11:49 -08006278 mApp, subId, callingPackage, "getClientRequestStats")) {
Sooraj Sasindran9a909312016-07-18 11:57:25 -07006279 return null;
6280 }
Sooraj Sasindran9a909312016-07-18 11:57:25 -07006281 Phone phone = getPhone(subId);
Sooraj Sasindran9a909312016-07-18 11:57:25 -07006282
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006283 final long identity = Binder.clearCallingIdentity();
6284 try {
6285 if (phone != null) {
6286 return phone.getClientRequestStats();
6287 }
6288
6289 return null;
6290 } finally {
6291 Binder.restoreCallingIdentity(identity);
6292 }
Sooraj Sasindran9a909312016-07-18 11:57:25 -07006293 }
6294
Narayan Kamathf04b5a12018-01-09 11:47:15 +00006295 private WorkSource getWorkSource(int uid) {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08006296 String packageName = mApp.getPackageManager().getNameForUid(uid);
Narayan Kamathf04b5a12018-01-09 11:47:15 +00006297 return new WorkSource(uid, packageName);
Sooraj Sasindran9a909312016-07-18 11:57:25 -07006298 }
Jack Yueb4124c2017-02-16 15:32:43 -08006299
6300 /**
Grace Chen70990072017-03-24 17:21:30 -07006301 * Set SIM card power state.
Jack Yueb4124c2017-02-16 15:32:43 -08006302 *
Sanket Padawe13bac7b2017-03-20 15:04:47 -07006303 * @param slotIndex SIM slot id.
Grace Chen70990072017-03-24 17:21:30 -07006304 * @param state State of SIM (power down, power up, pass through)
6305 * - {@link android.telephony.TelephonyManager#CARD_POWER_DOWN}
6306 * - {@link android.telephony.TelephonyManager#CARD_POWER_UP}
6307 * - {@link android.telephony.TelephonyManager#CARD_POWER_UP_PASS_THROUGH}
Jack Yueb4124c2017-02-16 15:32:43 -08006308 *
6309 **/
6310 @Override
Grace Chen70990072017-03-24 17:21:30 -07006311 public void setSimPowerStateForSlot(int slotIndex, int state) {
Jack Yueb4124c2017-02-16 15:32:43 -08006312 enforceModifyPermission();
Sanket Padawe13bac7b2017-03-20 15:04:47 -07006313 Phone phone = PhoneFactory.getPhone(slotIndex);
6314
vagdeviaf9a5b92018-08-15 16:01:53 -07006315 WorkSource workSource = getWorkSource(Binder.getCallingUid());
6316
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006317 final long identity = Binder.clearCallingIdentity();
6318 try {
6319 if (phone != null) {
vagdeviaf9a5b92018-08-15 16:01:53 -07006320 phone.setSimPowerState(state, workSource);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006321 }
6322 } finally {
6323 Binder.restoreCallingIdentity(identity);
Jack Yueb4124c2017-02-16 15:32:43 -08006324 }
6325 }
Shuo Qiandd210312017-04-12 22:11:33 +00006326
Tyler Gunn65d45c22017-06-05 11:22:26 -07006327 private boolean isUssdApiAllowed(int subId) {
6328 CarrierConfigManager configManager =
Nazanin Bakhshif782e562018-12-11 15:15:39 -08006329 (CarrierConfigManager) mApp.getSystemService(Context.CARRIER_CONFIG_SERVICE);
Tyler Gunn65d45c22017-06-05 11:22:26 -07006330 if (configManager == null) {
6331 return false;
6332 }
6333 PersistableBundle pb = configManager.getConfigForSubId(subId);
6334 if (pb == null) {
6335 return false;
6336 }
6337 return pb.getBoolean(
6338 CarrierConfigManager.KEY_ALLOW_USSD_REQUESTS_VIA_TELEPHONY_MANAGER_BOOL);
6339 }
6340
Shuo Qiandd210312017-04-12 22:11:33 +00006341 /**
6342 * Check if phone is in emergency callback mode
6343 * @return true if phone is in emergency callback mode
6344 * @param subId sub id
6345 */
goneil9c5f4872017-12-05 14:07:56 -08006346 @Override
Shuo Qiandd210312017-04-12 22:11:33 +00006347 public boolean getEmergencyCallbackMode(int subId) {
Brad Ebinger35c841c2018-10-01 10:40:55 -07006348 enforceReadPrivilegedPermission("getEmergencyCallbackMode");
Shuo Qiandd210312017-04-12 22:11:33 +00006349 final Phone phone = getPhone(subId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006350
6351 final long identity = Binder.clearCallingIdentity();
6352 try {
6353 if (phone != null) {
6354 return phone.isInEcm();
6355 } else {
6356 return false;
6357 }
6358 } finally {
6359 Binder.restoreCallingIdentity(identity);
Shuo Qiandd210312017-04-12 22:11:33 +00006360 }
6361 }
Nathan Harold46b42aa2017-03-10 19:38:22 -08006362
6363 /**
6364 * Get the current signal strength information for the given subscription.
6365 * Because this information is not updated when the device is in a low power state
6366 * it should not be relied-upon to be current.
6367 * @param subId Subscription index
6368 * @return the most recent cached signal strength info from the modem
6369 */
6370 @Override
6371 public SignalStrength getSignalStrength(int subId) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006372 final long identity = Binder.clearCallingIdentity();
6373 try {
6374 Phone p = getPhone(subId);
6375 if (p == null) {
6376 return null;
6377 }
Nathan Harold46b42aa2017-03-10 19:38:22 -08006378
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006379 return p.getSignalStrength();
6380 } finally {
6381 Binder.restoreCallingIdentity(identity);
6382 }
Nathan Harold46b42aa2017-03-10 19:38:22 -08006383 }
Holly Jiuyu Sun01c47ad2018-01-24 17:56:33 +00006384
Pengquan Meng77b7f132018-08-22 14:49:57 -07006385 /**
Chen Xuf792fd62018-10-17 17:54:36 +00006386 * Get the current modem radio state for the given slot.
6387 * @param slotIndex slot index.
6388 * @param callingPackage the name of the package making the call.
6389 * @return the current radio power state from the modem
6390 */
6391 @Override
6392 public int getRadioPowerState(int slotIndex, String callingPackage) {
6393 Phone phone = PhoneFactory.getPhone(slotIndex);
6394 if (phone != null) {
6395 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
6396 mApp, phone.getSubId(), callingPackage, "getRadioPowerState")) {
6397 return TelephonyManager.RADIO_POWER_UNAVAILABLE;
6398 }
6399
6400 final long identity = Binder.clearCallingIdentity();
6401 try {
6402 return phone.getRadioPowerState();
6403 } finally {
6404 Binder.restoreCallingIdentity(identity);
6405 }
6406 }
6407 return TelephonyManager.RADIO_POWER_UNAVAILABLE;
6408 }
6409
6410 /**
Pengquan Meng77b7f132018-08-22 14:49:57 -07006411 * Checks if data roaming is enabled on the subscription with id {@code subId}.
6412 *
6413 * <p>Requires one of the following permissions:
6414 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE},
6415 * {@link android.Manifest.permission#READ_PHONE_STATE} or that the calling app has carrier
6416 * privileges.
6417 *
6418 * @param subId subscription id
6419 * @return {@code true} if data roaming is enabled on this subscription, otherwise return
6420 * {@code false}.
6421 */
6422 @Override
6423 public boolean isDataRoamingEnabled(int subId) {
Pengquan Meng44e66f12019-04-01 10:48:20 -07006424 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.ACCESS_NETWORK_STATE,
6425 null /* message */);
6426
Pengquan Menga1bb6272018-09-06 09:59:22 -07006427 boolean isEnabled = false;
6428 final long identity = Binder.clearCallingIdentity();
Pengquan Meng77b7f132018-08-22 14:49:57 -07006429 try {
Pengquan Menga1bb6272018-09-06 09:59:22 -07006430 Phone phone = getPhone(subId);
6431 isEnabled = phone != null ? phone.getDataRoamingEnabled() : false;
Pengquan Meng77b7f132018-08-22 14:49:57 -07006432 } catch (Exception e) {
6433 TelephonyPermissions.enforeceCallingOrSelfReadPhoneStatePermissionOrCarrierPrivilege(
6434 mApp, subId, "isDataRoamingEnabled");
Pengquan Menga1bb6272018-09-06 09:59:22 -07006435 } finally {
6436 Binder.restoreCallingIdentity(identity);
Pengquan Meng77b7f132018-08-22 14:49:57 -07006437 }
Pengquan Menga1bb6272018-09-06 09:59:22 -07006438 return isEnabled;
Pengquan Meng77b7f132018-08-22 14:49:57 -07006439 }
6440
6441
6442 /**
6443 * Enables/Disables the data roaming on the subscription with id {@code subId}.
6444 *
6445 * <p> Requires permission:
6446 * {@link android.Manifest.permission#MODIFY_PHONE_STATE} or that the calling app has carrier
6447 * privileges.
6448 *
6449 * @param subId subscription id
6450 * @param isEnabled {@code true} means enable, {@code false} means disable.
6451 */
6452 @Override
6453 public void setDataRoamingEnabled(int subId, boolean isEnabled) {
Pengquan Meng44e66f12019-04-01 10:48:20 -07006454 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
6455 mApp, subId, "setDataRoamingEnabled");
6456
Pengquan Menga1bb6272018-09-06 09:59:22 -07006457 final long identity = Binder.clearCallingIdentity();
6458 try {
Pengquan Menga1bb6272018-09-06 09:59:22 -07006459 Phone phone = getPhone(subId);
6460 if (phone != null) {
6461 phone.setDataRoamingEnabled(isEnabled);
6462 }
6463 } finally {
6464 Binder.restoreCallingIdentity(identity);
Pengquan Meng77b7f132018-08-22 14:49:57 -07006465 }
6466 }
6467
Holly Jiuyu Sun01c47ad2018-01-24 17:56:33 +00006468 @Override
Pengquan Meng6884a2c2018-10-03 12:19:13 -07006469 public boolean isManualNetworkSelectionAllowed(int subId) {
Pengquan Meng44e66f12019-04-01 10:48:20 -07006470 TelephonyPermissions.enforeceCallingOrSelfReadPhoneStatePermissionOrCarrierPrivilege(
6471 mApp, subId, "isManualNetworkSelectionAllowed");
6472
Pengquan Meng6884a2c2018-10-03 12:19:13 -07006473 boolean isAllowed = true;
6474 final long identity = Binder.clearCallingIdentity();
6475 try {
Pengquan Meng6884a2c2018-10-03 12:19:13 -07006476 Phone phone = getPhone(subId);
6477 if (phone != null) {
6478 isAllowed = phone.isCspPlmnEnabled();
6479 }
6480 } finally {
6481 Binder.restoreCallingIdentity(identity);
6482 }
6483 return isAllowed;
6484 }
6485
6486 @Override
Jordan Liu75f43ea2019-01-17 16:56:37 -08006487 public List<UiccCardInfo> getUiccCardsInfo(String callingPackage) {
Jordan Liu1e142fc2019-04-22 15:10:43 -07006488 boolean hasReadPermission = false;
Jordan Liuc65bc952019-02-12 17:54:02 -08006489 try {
6490 enforceReadPrivilegedPermission("getUiccCardsInfo");
Jordan Liu1e142fc2019-04-22 15:10:43 -07006491 hasReadPermission = true;
Jordan Liuc65bc952019-02-12 17:54:02 -08006492 } catch (SecurityException e) {
6493 // even without READ_PRIVILEGED_PHONE_STATE, we allow the call to continue if the caller
6494 // has carrier privileges on an active UICC
6495 if (checkCarrierPrivilegesForPackageAnyPhone(callingPackage)
6496 != TelephonyManager.CARRIER_PRIVILEGE_STATUS_HAS_ACCESS) {
Jordan Liu1e142fc2019-04-22 15:10:43 -07006497 throw new SecurityException("Caller does not have permission.");
Jordan Liuc65bc952019-02-12 17:54:02 -08006498 }
Jordan Liu75f43ea2019-01-17 16:56:37 -08006499 }
Jordan Liu5aa07002018-12-18 15:44:48 -08006500
6501 final long identity = Binder.clearCallingIdentity();
6502 try {
Jordan Liu75f43ea2019-01-17 16:56:37 -08006503 UiccController uiccController = UiccController.getInstance();
6504 ArrayList<UiccCardInfo> cardInfos = uiccController.getAllUiccCardInfos();
Jordan Liu1e142fc2019-04-22 15:10:43 -07006505 if (hasReadPermission) {
6506 return cardInfos;
Jordan Liu75f43ea2019-01-17 16:56:37 -08006507 }
Jordan Liu1e142fc2019-04-22 15:10:43 -07006508
6509 // Remove private info if the caller doesn't have access
6510 ArrayList<UiccCardInfo> filteredInfos = new ArrayList<>();
6511 for (UiccCardInfo cardInfo : cardInfos) {
6512 // For an inactive eUICC, the UiccCard will be null even though the UiccCardInfo
6513 // is available
6514 UiccCard card = uiccController.getUiccCardForSlot(cardInfo.getSlotIndex());
6515 if (card == null || card.getUiccProfile() == null) {
6516 // assume no access if the card or profile is unavailable
6517 filteredInfos.add(cardInfo.getUnprivileged());
6518 continue;
6519 }
6520 UiccProfile profile = card.getUiccProfile();
6521 if (profile.getCarrierPrivilegeStatus(mApp.getPackageManager(), callingPackage)
6522 == TelephonyManager.CARRIER_PRIVILEGE_STATUS_HAS_ACCESS) {
6523 filteredInfos.add(cardInfo);
6524 } else {
6525 filteredInfos.add(cardInfo.getUnprivileged());
6526 }
6527 }
6528 return filteredInfos;
Jordan Liu5aa07002018-12-18 15:44:48 -08006529 } finally {
6530 Binder.restoreCallingIdentity(identity);
6531 }
6532 }
6533
6534 @Override
Holly Jiuyu Sun01c47ad2018-01-24 17:56:33 +00006535 public UiccSlotInfo[] getUiccSlotsInfo() {
Brad Ebinger35c841c2018-10-01 10:40:55 -07006536 enforceReadPrivilegedPermission("getUiccSlotsInfo");
Holly Jiuyu Sun01c47ad2018-01-24 17:56:33 +00006537
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006538 final long identity = Binder.clearCallingIdentity();
6539 try {
6540 UiccSlot[] slots = UiccController.getInstance().getUiccSlots();
6541 if (slots == null) {
6542 Rlog.i(LOG_TAG, "slots is null.");
6543 return null;
6544 }
6545
6546 UiccSlotInfo[] infos = new UiccSlotInfo[slots.length];
6547 for (int i = 0; i < slots.length; i++) {
6548 UiccSlot slot = slots[i];
6549 if (slot == null) {
6550 continue;
6551 }
6552
Jordan Liu7be7e652019-05-06 18:55:02 +00006553 String cardId;
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006554 UiccCard card = slot.getUiccCard();
6555 if (card != null) {
6556 cardId = card.getCardId();
Jordan Liu7be7e652019-05-06 18:55:02 +00006557 } else {
Jordan Liu01bd00d2019-09-12 16:19:43 -07006558 cardId = slot.getEid();
6559 if (TextUtils.isEmpty(cardId)) {
6560 cardId = slot.getIccId();
6561 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006562 }
6563
6564 int cardState = 0;
6565 switch (slot.getCardState()) {
6566 case CARDSTATE_ABSENT:
6567 cardState = UiccSlotInfo.CARD_STATE_INFO_ABSENT;
6568 break;
6569 case CARDSTATE_PRESENT:
6570 cardState = UiccSlotInfo.CARD_STATE_INFO_PRESENT;
6571 break;
6572 case CARDSTATE_ERROR:
6573 cardState = UiccSlotInfo.CARD_STATE_INFO_ERROR;
6574 break;
6575 case CARDSTATE_RESTRICTED:
6576 cardState = UiccSlotInfo.CARD_STATE_INFO_RESTRICTED;
6577 break;
6578 default:
6579 break;
6580
6581 }
6582
6583 infos[i] = new UiccSlotInfo(
6584 slot.isActive(),
6585 slot.isEuicc(),
6586 cardId,
6587 cardState,
6588 slot.getPhoneId(),
Jordan Liua2619582019-02-14 12:56:40 -08006589 slot.isExtendedApduSupported(),
6590 slot.isRemovable());
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006591 }
6592 return infos;
6593 } finally {
6594 Binder.restoreCallingIdentity(identity);
Holly Jiuyu Sun1d957c52018-04-04 13:52:42 -07006595 }
Holly Jiuyu Sun01c47ad2018-01-24 17:56:33 +00006596 }
6597
6598 @Override
6599 public boolean switchSlots(int[] physicalSlots) {
6600 enforceModifyPermission();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006601
6602 final long identity = Binder.clearCallingIdentity();
6603 try {
6604 return (Boolean) sendRequest(CMD_SWITCH_SLOTS, physicalSlots);
6605 } finally {
6606 Binder.restoreCallingIdentity(identity);
6607 }
Holly Jiuyu Sun01c47ad2018-01-24 17:56:33 +00006608 }
Jack Yu4c988042018-02-27 15:30:01 -08006609
6610 @Override
Jordan Liu7de49fa2018-12-06 14:48:49 -08006611 public int getCardIdForDefaultEuicc(int subId, String callingPackage) {
Jordan Liu7de49fa2018-12-06 14:48:49 -08006612 final long identity = Binder.clearCallingIdentity();
6613 try {
6614 return UiccController.getInstance().getCardIdForDefaultEuicc();
6615 } finally {
6616 Binder.restoreCallingIdentity(identity);
6617 }
6618 }
6619
6620 @Override
Jack Yu4c988042018-02-27 15:30:01 -08006621 public void setRadioIndicationUpdateMode(int subId, int filters, int mode) {
6622 enforceModifyPermission();
6623 final Phone phone = getPhone(subId);
6624 if (phone == null) {
6625 loge("setRadioIndicationUpdateMode fails with invalid subId: " + subId);
6626 return;
6627 }
6628
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006629 final long identity = Binder.clearCallingIdentity();
6630 try {
6631 phone.setRadioIndicationUpdateMode(filters, mode);
6632 } finally {
6633 Binder.restoreCallingIdentity(identity);
6634 }
Jack Yu4c988042018-02-27 15:30:01 -08006635 }
Pengquan Meng85728fb2018-03-12 16:31:21 -07006636
6637 /**
goneil47ffb6e2018-04-06 15:40:58 -07006638 * A test API to reload the UICC profile.
6639 *
6640 * <p>Requires that the calling app has permission
6641 * {@link android.Manifest.permission#MODIFY_PHONE_STATE MODIFY_PHONE_STATE}.
6642 * @hide
6643 */
6644 @Override
6645 public void refreshUiccProfile(int subId) {
6646 enforceModifyPermission();
6647
6648 final long identity = Binder.clearCallingIdentity();
6649 try {
6650 Phone phone = getPhone(subId);
6651 if (phone == null) {
6652 return;
6653 }
6654 UiccCard uiccCard = phone.getUiccCard();
6655 if (uiccCard == null) {
6656 return;
6657 }
6658 UiccProfile uiccProfile = uiccCard.getUiccProfile();
6659 if (uiccProfile == null) {
6660 return;
6661 }
6662 uiccProfile.refresh();
6663 } finally {
6664 Binder.restoreCallingIdentity(identity);
6665 }
6666 }
6667
6668 /**
Pengquan Meng85728fb2018-03-12 16:31:21 -07006669 * Returns false if the mobile data is disabled by default, otherwise return true.
6670 */
6671 private boolean getDefaultDataEnabled() {
6672 return "true".equalsIgnoreCase(
6673 SystemProperties.get(DEFAULT_MOBILE_DATA_PROPERTY_NAME, "true"));
6674 }
6675
6676 /**
6677 * Returns true if the data roaming is enabled by default, i.e the system property
6678 * of {@link #DEFAULT_DATA_ROAMING_PROPERTY_NAME} is true or the config of
6679 * {@link CarrierConfigManager#KEY_CARRIER_DEFAULT_DATA_ROAMING_ENABLED_BOOL} is true.
6680 */
6681 private boolean getDefaultDataRoamingEnabled(int subId) {
6682 final CarrierConfigManager configMgr = (CarrierConfigManager)
Nazanin Bakhshif782e562018-12-11 15:15:39 -08006683 mApp.getSystemService(Context.CARRIER_CONFIG_SERVICE);
Pengquan Meng85728fb2018-03-12 16:31:21 -07006684 boolean isDataRoamingEnabled = "true".equalsIgnoreCase(
6685 SystemProperties.get(DEFAULT_DATA_ROAMING_PROPERTY_NAME, "false"));
6686 isDataRoamingEnabled |= configMgr.getConfigForSubId(subId).getBoolean(
6687 CarrierConfigManager.KEY_CARRIER_DEFAULT_DATA_ROAMING_ENABLED_BOOL);
6688 return isDataRoamingEnabled;
6689 }
6690
6691 /**
6692 * Returns the default network type for the given {@code subId}, if the default network type is
6693 * not set, return {@link Phone#PREFERRED_NT_MODE}.
6694 */
6695 private int getDefaultNetworkType(int subId) {
6696 return Integer.parseInt(
6697 TelephonyManager.getTelephonyProperty(
6698 mSubscriptionController.getPhoneId(subId),
6699 DEFAULT_NETWORK_MODE_PROPERTY_NAME,
6700 String.valueOf(Phone.PREFERRED_NT_MODE)));
6701 }
fionaxua13278b2018-03-21 00:08:13 -07006702
6703 @Override
6704 public void setCarrierTestOverride(int subId, String mccmnc, String imsi, String iccid, String
chen xueaba88a2019-03-15 13:15:10 -07006705 gid1, String gid2, String plmn, String spn, String carrierPrivilegeRules, String apn) {
fionaxua13278b2018-03-21 00:08:13 -07006706 enforceModifyPermission();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006707
6708 final long identity = Binder.clearCallingIdentity();
6709 try {
6710 final Phone phone = getPhone(subId);
6711 if (phone == null) {
6712 loge("setCarrierTestOverride fails with invalid subId: " + subId);
6713 return;
6714 }
chen xueaba88a2019-03-15 13:15:10 -07006715 phone.setCarrierTestOverride(mccmnc, imsi, iccid, gid1, gid2, plmn, spn,
6716 carrierPrivilegeRules, apn);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006717 } finally {
6718 Binder.restoreCallingIdentity(identity);
fionaxua13278b2018-03-21 00:08:13 -07006719 }
fionaxua13278b2018-03-21 00:08:13 -07006720 }
6721
6722 @Override
6723 public int getCarrierIdListVersion(int subId) {
Brad Ebinger35c841c2018-10-01 10:40:55 -07006724 enforceReadPrivilegedPermission("getCarrierIdListVersion");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006725
6726 final long identity = Binder.clearCallingIdentity();
6727 try {
6728 final Phone phone = getPhone(subId);
6729 if (phone == null) {
6730 loge("getCarrierIdListVersion fails with invalid subId: " + subId);
6731 return TelephonyManager.UNKNOWN_CARRIER_ID_LIST_VERSION;
6732 }
6733 return phone.getCarrierIdListVersion();
6734 } finally {
6735 Binder.restoreCallingIdentity(identity);
fionaxua13278b2018-03-21 00:08:13 -07006736 }
fionaxua13278b2018-03-21 00:08:13 -07006737 }
Malcolm Chen2c63d402018-08-14 16:00:53 -07006738
6739 @Override
6740 public int getNumberOfModemsWithSimultaneousDataConnections(int subId, String callingPackage) {
6741 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
6742 mApp, subId, callingPackage, "getNumberOfModemsWithSimultaneousDataConnections")) {
6743 return -1;
6744 }
6745
6746 final long identity = Binder.clearCallingIdentity();
6747 try {
6748 return mPhoneConfigurationManager.getNumberOfModemsWithSimultaneousDataConnections();
6749 } finally {
6750 Binder.restoreCallingIdentity(identity);
6751 }
6752 }
Pengquan Menga1bb6272018-09-06 09:59:22 -07006753
6754 @Override
6755 public int getCdmaRoamingMode(int subId) {
6756 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
6757 mApp, subId, "getCdmaRoamingMode");
6758
6759 final long identity = Binder.clearCallingIdentity();
6760 try {
6761 return (int) sendRequest(CMD_GET_CDMA_ROAMING_MODE, null /* argument */, subId);
6762 } finally {
6763 Binder.restoreCallingIdentity(identity);
6764 }
6765 }
6766
6767 @Override
6768 public boolean setCdmaRoamingMode(int subId, int mode) {
6769 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
6770 mApp, subId, "setCdmaRoamingMode");
6771
6772 final long identity = Binder.clearCallingIdentity();
6773 try {
6774 return (boolean) sendRequest(CMD_SET_CDMA_ROAMING_MODE, mode, subId);
6775 } finally {
6776 Binder.restoreCallingIdentity(identity);
6777 }
6778 }
6779
6780 @Override
6781 public boolean setCdmaSubscriptionMode(int subId, int mode) {
6782 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
6783 mApp, subId, "setCdmaSubscriptionMode");
6784
6785 final long identity = Binder.clearCallingIdentity();
6786 try {
6787 return (boolean) sendRequest(CMD_SET_CDMA_SUBSCRIPTION_MODE, mode, subId);
6788 } finally {
6789 Binder.restoreCallingIdentity(identity);
6790 }
6791 }
Makoto Onukida3bf792018-09-18 16:06:29 -07006792
6793 private void ensureUserRunning(int userId) {
6794 if (!mUserManager.isUserRunning(userId)) {
6795 throw new IllegalStateException("User " + userId + " does not exist or not running");
6796 }
6797 }
6798
6799 /**
6800 * Returns a list of SMS apps on a given user.
6801 *
6802 * Only the shell user (UID 2000 or 0) can call it.
6803 * Target user must be running.
6804 */
6805 @Override
6806 public String[] getSmsApps(int userId) {
6807 TelephonyPermissions.enforceShellOnly(Binder.getCallingUid(), "getSmsApps");
6808 ensureUserRunning(userId);
6809
6810 final Collection<SmsApplicationData> apps =
6811 SmsApplication.getApplicationCollectionAsUser(mApp, userId);
6812
6813 String[] ret = new String[apps.size()];
6814 int i = 0;
6815 for (SmsApplicationData app : apps) {
6816 ret[i++] = app.mPackageName;
6817 }
6818 return ret;
6819 }
6820
6821 /**
6822 * Returns the default SMS app package name on a given user.
6823 *
6824 * Only the shell user (UID 2000 or 0) can call it.
6825 * Target user must be running.
6826 */
6827 @Override
6828 public String getDefaultSmsApp(int userId) {
6829 TelephonyPermissions.enforceShellOnly(Binder.getCallingUid(), "getDefaultSmsApp");
6830 ensureUserRunning(userId);
6831
6832 final ComponentName cn = SmsApplication.getDefaultSmsApplicationAsUser(mApp,
6833 /* updateIfNeeded= */ true, userId);
6834 return cn == null ? null : cn.getPackageName();
6835 }
6836
6837 /**
6838 * Set a package as the default SMS app on a given user.
6839 *
6840 * Only the shell user (UID 2000 or 0) can call it.
6841 * Target user must be running.
6842 */
6843 @Override
6844 public void setDefaultSmsApp(int userId, String packageName) {
6845 TelephonyPermissions.enforceShellOnly(Binder.getCallingUid(), "setDefaultSmsApp");
6846 ensureUserRunning(userId);
6847
6848 boolean found = false;
6849 for (String pkg : getSmsApps(userId)) {
6850 if (TextUtils.equals(packageName, pkg)) {
6851 found = true;
6852 break;
6853 }
6854 }
6855 if (!found) {
6856 throw new IllegalArgumentException("Package " + packageName + " is not an SMS app");
6857 }
6858
6859 SmsApplication.setDefaultApplicationAsUser(packageName, mApp, userId);
6860 }
sqianc5eccab2018-10-19 18:46:41 -07006861
6862 @Override
sqian8c685422019-02-22 15:55:18 -08006863 public Map<Integer, List<EmergencyNumber>> getEmergencyNumberList(
sqianc5eccab2018-10-19 18:46:41 -07006864 String callingPackage) {
sqian11b7a0e2018-12-05 18:48:28 -08006865 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
sqian8c685422019-02-22 15:55:18 -08006866 mApp, getDefaultSubscription(), callingPackage, "getEmergencyNumberList")) {
sqian11b7a0e2018-12-05 18:48:28 -08006867 throw new SecurityException("Requires READ_PHONE_STATE permission.");
6868 }
6869 final long identity = Binder.clearCallingIdentity();
6870 try {
sqian854d44b2018-12-12 16:48:18 -08006871 Map<Integer, List<EmergencyNumber>> emergencyNumberListInternal = new HashMap<>();
6872 for (Phone phone: PhoneFactory.getPhones()) {
6873 if (phone.getEmergencyNumberTracker() != null
6874 && phone.getEmergencyNumberTracker().getEmergencyNumberList() != null) {
6875 emergencyNumberListInternal.put(
6876 phone.getSubId(),
6877 phone.getEmergencyNumberTracker().getEmergencyNumberList());
6878 }
sqian11b7a0e2018-12-05 18:48:28 -08006879 }
sqian854d44b2018-12-12 16:48:18 -08006880 return emergencyNumberListInternal;
sqian11b7a0e2018-12-05 18:48:28 -08006881 } finally {
6882 Binder.restoreCallingIdentity(identity);
6883 }
sqianc5eccab2018-10-19 18:46:41 -07006884 }
6885
6886 @Override
sqian8c685422019-02-22 15:55:18 -08006887 public boolean isEmergencyNumber(String number, boolean exactMatch) {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08006888 final Phone defaultPhone = getDefaultPhone();
sqian11b7a0e2018-12-05 18:48:28 -08006889 if (!exactMatch) {
6890 TelephonyPermissions
6891 .enforeceCallingOrSelfReadPrivilegedPhoneStatePermissionOrCarrierPrivilege(
sqian8c685422019-02-22 15:55:18 -08006892 mApp, defaultPhone.getSubId(), "isEmergencyNumber(Potential)");
sqian11b7a0e2018-12-05 18:48:28 -08006893 }
6894 final long identity = Binder.clearCallingIdentity();
6895 try {
sqian854d44b2018-12-12 16:48:18 -08006896 for (Phone phone: PhoneFactory.getPhones()) {
6897 if (phone.getEmergencyNumberTracker() != null
6898 && phone.getEmergencyNumberTracker() != null) {
6899 if (phone.getEmergencyNumberTracker().isEmergencyNumber(
6900 number, exactMatch)) {
6901 return true;
sqian11b7a0e2018-12-05 18:48:28 -08006902 }
6903 }
sqian11b7a0e2018-12-05 18:48:28 -08006904 }
6905 return false;
6906 } finally {
6907 Binder.restoreCallingIdentity(identity);
6908 }
6909 }
6910
sqianf4ca7ed2019-01-15 18:32:07 -08006911 /**
6912 * Update emergency number list for test mode.
6913 */
6914 @Override
6915 public void updateEmergencyNumberListTestMode(int action, EmergencyNumber num) {
6916 TelephonyPermissions.enforceShellOnly(Binder.getCallingUid(),
6917 "updateEmergencyNumberListTestMode");
6918
6919 final long identity = Binder.clearCallingIdentity();
6920 try {
6921 for (Phone phone: PhoneFactory.getPhones()) {
6922 EmergencyNumberTracker tracker = phone.getEmergencyNumberTracker();
6923 if (tracker != null) {
6924 tracker.executeEmergencyNumberTestModeCommand(action, num);
6925 }
6926 }
6927 } finally {
6928 Binder.restoreCallingIdentity(identity);
6929 }
6930 }
6931
6932 /**
6933 * Get the full emergency number list for test mode.
6934 */
6935 @Override
6936 public List<String> getEmergencyNumberListTestMode() {
6937 TelephonyPermissions.enforceShellOnly(Binder.getCallingUid(),
6938 "getEmergencyNumberListTestMode");
6939
6940 final long identity = Binder.clearCallingIdentity();
6941 try {
6942 Set<String> emergencyNumbers = new HashSet<>();
6943 for (Phone phone: PhoneFactory.getPhones()) {
6944 EmergencyNumberTracker tracker = phone.getEmergencyNumberTracker();
6945 if (tracker != null) {
6946 for (EmergencyNumber num : tracker.getEmergencyNumberList()) {
6947 emergencyNumbers.add(num.getNumber());
6948 }
6949 }
6950 }
6951 return new ArrayList<>(emergencyNumbers);
6952 } finally {
6953 Binder.restoreCallingIdentity(identity);
6954 }
6955 }
6956
chen xud6b45bd2018-10-30 22:27:10 -07006957 @Override
6958 public List<String> getCertsFromCarrierPrivilegeAccessRules(int subId) {
6959 enforceReadPrivilegedPermission("getCertsFromCarrierPrivilegeAccessRules");
6960 Phone phone = getPhone(subId);
6961 if (phone == null) {
6962 return null;
6963 }
6964 final long identity = Binder.clearCallingIdentity();
6965 try {
6966 UiccProfile profile = UiccController.getInstance()
6967 .getUiccProfileForPhone(phone.getPhoneId());
6968 if (profile != null) {
6969 return profile.getCertsFromCarrierPrivilegeAccessRules();
6970 }
6971 } finally {
6972 Binder.restoreCallingIdentity(identity);
6973 }
6974 return null;
6975 }
Malcolm Chen8e4ed912019-01-15 20:22:16 -08006976
6977 /**
6978 * Enable or disable a modem stack.
6979 */
6980 @Override
6981 public boolean enableModemForSlot(int slotIndex, boolean enable) {
6982 enforceModifyPermission();
6983
6984 final long identity = Binder.clearCallingIdentity();
6985 try {
6986 Phone phone = PhoneFactory.getPhone(slotIndex);
6987 if (phone == null) {
6988 return false;
6989 } else {
6990 return (Boolean) sendRequest(CMD_REQUEST_ENABLE_MODEM, enable, phone, null);
6991 }
6992 } finally {
6993 Binder.restoreCallingIdentity(identity);
6994 }
6995 }
Michelecea4cf22018-12-21 15:00:11 -08006996
Malcolm Chen4bcd9822019-03-27 18:34:05 -07006997 /**
6998 * Whether a modem stack is enabled or not.
6999 */
7000 @Override
7001 public boolean isModemEnabledForSlot(int slotIndex, String callingPackage) {
7002 Phone phone = PhoneFactory.getPhone(slotIndex);
7003 if (phone == null) return false;
7004
7005 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
7006 mApp, phone.getSubId(), callingPackage, "isModemEnabledForSlot")) {
7007 throw new SecurityException("Requires READ_PHONE_STATE permission.");
7008 }
7009
7010 final long identity = Binder.clearCallingIdentity();
7011 try {
Nazanin Bakhshif71371d2019-04-29 17:29:44 -07007012 try {
7013 return mPhoneConfigurationManager.getPhoneStatusFromCache(phone.getPhoneId());
7014 } catch (NoSuchElementException ex) {
7015 return (Boolean) sendRequest(CMD_GET_MODEM_STATUS, null, phone, null);
7016 }
Malcolm Chen4bcd9822019-03-27 18:34:05 -07007017 } finally {
7018 Binder.restoreCallingIdentity(identity);
7019 }
7020 }
7021
Michelecea4cf22018-12-21 15:00:11 -08007022 @Override
Michele0ea7d782019-03-19 14:58:42 -07007023 public void setMultiSimCarrierRestriction(boolean isMultiSimCarrierRestricted) {
Michelecea4cf22018-12-21 15:00:11 -08007024 enforceModifyPermission();
7025
7026 final long identity = Binder.clearCallingIdentity();
7027 try {
7028 mTelephonySharedPreferences.edit()
Michele0ea7d782019-03-19 14:58:42 -07007029 .putBoolean(PREF_MULTI_SIM_RESTRICTED, isMultiSimCarrierRestricted)
Michelecea4cf22018-12-21 15:00:11 -08007030 .commit();
7031 } finally {
7032 Binder.restoreCallingIdentity(identity);
7033 }
7034 }
7035
7036 @Override
Michele0ea7d782019-03-19 14:58:42 -07007037 @TelephonyManager.IsMultiSimSupportedResult
7038 public int isMultiSimSupported(String callingPackage) {
Michele4245e952019-02-04 11:36:23 -08007039 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(mApp,
Michele0ea7d782019-03-19 14:58:42 -07007040 getDefaultPhone().getSubId(), callingPackage, "isMultiSimSupported")) {
7041 return TelephonyManager.MULTISIM_NOT_SUPPORTED_BY_HARDWARE;
Michele4245e952019-02-04 11:36:23 -08007042 }
Michelecea4cf22018-12-21 15:00:11 -08007043
7044 final long identity = Binder.clearCallingIdentity();
7045 try {
Michele0ea7d782019-03-19 14:58:42 -07007046 return isMultiSimSupportedInternal();
Michelecea4cf22018-12-21 15:00:11 -08007047 } finally {
7048 Binder.restoreCallingIdentity(identity);
7049 }
7050 }
Nazanin Bakhshi628473f2019-01-29 17:37:52 -08007051
Michele0ea7d782019-03-19 14:58:42 -07007052 @TelephonyManager.IsMultiSimSupportedResult
7053 private int isMultiSimSupportedInternal() {
Michele30b57b22019-03-01 12:01:14 -08007054 // If the device has less than 2 SIM cards, indicate that multisim is restricted.
7055 int numPhysicalSlots = UiccController.getInstance().getUiccSlots().length;
7056 if (numPhysicalSlots < 2) {
Michele0ea7d782019-03-19 14:58:42 -07007057 loge("isMultiSimSupportedInternal: requires at least 2 cards");
7058 return TelephonyManager.MULTISIM_NOT_SUPPORTED_BY_HARDWARE;
Michele30b57b22019-03-01 12:01:14 -08007059 }
7060 // Check if the hardware supports multisim functionality. If usage of multisim is not
7061 // supported by the modem, indicate that it is restricted.
7062 PhoneCapability staticCapability =
7063 mPhoneConfigurationManager.getStaticPhoneCapability();
7064 if (staticCapability == null) {
Michele0ea7d782019-03-19 14:58:42 -07007065 loge("isMultiSimSupportedInternal: no static configuration available");
7066 return TelephonyManager.MULTISIM_NOT_SUPPORTED_BY_HARDWARE;
Michele30b57b22019-03-01 12:01:14 -08007067 }
7068 if (staticCapability.logicalModemList.size() < 2) {
Michele0ea7d782019-03-19 14:58:42 -07007069 loge("isMultiSimSupportedInternal: maximum number of modem is < 2");
7070 return TelephonyManager.MULTISIM_NOT_SUPPORTED_BY_HARDWARE;
Michele30b57b22019-03-01 12:01:14 -08007071 }
7072 // Check if support of multiple SIMs is restricted by carrier
7073 if (mTelephonySharedPreferences.getBoolean(PREF_MULTI_SIM_RESTRICTED, false)) {
Michele0ea7d782019-03-19 14:58:42 -07007074 return TelephonyManager.MULTISIM_NOT_SUPPORTED_BY_CARRIER;
Michele30b57b22019-03-01 12:01:14 -08007075 }
7076
Michele0ea7d782019-03-19 14:58:42 -07007077 return TelephonyManager.MULTISIM_ALLOWED;
Michele30b57b22019-03-01 12:01:14 -08007078 }
7079
Nazanin Bakhshi628473f2019-01-29 17:37:52 -08007080 /**
7081 * Switch configs to enable multi-sim or switch back to single-sim
Nazanin Bakhshi17318782019-03-01 11:56:08 -08007082 * Note: Switch from multi-sim to single-sim is only possible with MODIFY_PHONE_STATE
7083 * permission, but the other way around is possible with either MODIFY_PHONE_STATE
7084 * or carrier privileges
Nazanin Bakhshi628473f2019-01-29 17:37:52 -08007085 * @param numOfSims number of active sims we want to switch to
7086 */
7087 @Override
7088 public void switchMultiSimConfig(int numOfSims) {
Nazanin Bakhshi17318782019-03-01 11:56:08 -08007089 if (numOfSims == 1) {
7090 enforceModifyPermission();
7091 } else {
7092 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
7093 mApp, SubscriptionManager.DEFAULT_SUBSCRIPTION_ID, "switchMultiSimConfig");
7094 }
Nazanin Bakhshi628473f2019-01-29 17:37:52 -08007095 final long identity = Binder.clearCallingIdentity();
Michele30b57b22019-03-01 12:01:14 -08007096
Nazanin Bakhshi628473f2019-01-29 17:37:52 -08007097 try {
Michele30b57b22019-03-01 12:01:14 -08007098 //only proceed if multi-sim is not restricted
Michele0ea7d782019-03-19 14:58:42 -07007099 if (isMultiSimSupportedInternal() != TelephonyManager.MULTISIM_ALLOWED) {
Michele30b57b22019-03-01 12:01:14 -08007100 loge("switchMultiSimConfig not possible. It is restricted or not supported.");
7101 return;
7102 }
Nazanin Bakhshi628473f2019-01-29 17:37:52 -08007103 mPhoneConfigurationManager.switchMultiSimConfig(numOfSims);
7104 } finally {
7105 Binder.restoreCallingIdentity(identity);
7106 }
7107 }
7108
7109 /**
chen xub4baa772019-04-03 10:23:41 -07007110 * Get whether making changes to modem configurations will trigger reboot.
7111 * Return value defaults to true.
Nazanin Bakhshi5fe5ef22019-01-30 10:52:09 -08007112 */
7113 @Override
chen xub4baa772019-04-03 10:23:41 -07007114 public boolean doesSwitchMultiSimConfigTriggerReboot(int subId, String callingPackage) {
7115 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
7116 mApp, subId, callingPackage, "doesSwitchMultiSimConfigTriggerReboot")) {
7117 return false;
7118 }
Nazanin Bakhshi5fe5ef22019-01-30 10:52:09 -08007119 final long identity = Binder.clearCallingIdentity();
7120 try {
7121 return mPhoneConfigurationManager.isRebootRequiredForModemConfigChange();
7122 } finally {
7123 Binder.restoreCallingIdentity(identity);
7124 }
7125 }
7126
Nathan Harold29f5f052019-02-15 13:41:57 -08007127 private void updateModemStateMetrics() {
7128 TelephonyMetrics metrics = TelephonyMetrics.getInstance();
7129 // TODO: check the state for each modem if the api is ready.
7130 metrics.updateEnabledModemBitmap((1 << TelephonyManager.from(mApp).getPhoneCount()) - 1);
7131 }
7132
Pengquan Meng3889a572019-01-23 11:16:29 -08007133 @Override
7134 public int[] getSlotsMapping() {
7135 enforceReadPrivilegedPermission("getSlotsMapping");
7136
7137 final long identity = Binder.clearCallingIdentity();
7138 try {
7139 int phoneCount = TelephonyManager.getDefault().getPhoneCount();
7140 // All logical slots should have a mapping to a physical slot.
7141 int[] logicalSlotsMapping = new int[phoneCount];
7142 UiccSlotInfo[] slotInfos = getUiccSlotsInfo();
7143 for (int i = 0; i < slotInfos.length; i++) {
7144 if (SubscriptionManager.isValidPhoneId(slotInfos[i].getLogicalSlotIdx())) {
7145 logicalSlotsMapping[slotInfos[i].getLogicalSlotIdx()] = i;
7146 }
7147 }
7148 return logicalSlotsMapping;
7149 } finally {
7150 Binder.restoreCallingIdentity(identity);
7151 }
7152 }
Nathan Harold48d6fd52019-02-06 19:01:40 -08007153
7154 /**
7155 * Get the IRadio HAL Version
7156 */
7157 @Override
7158 public int getRadioHalVersion() {
7159 Phone phone = getDefaultPhone();
7160 if (phone == null) return -1;
7161 HalVersion hv = phone.getHalVersion();
7162 if (hv.equals(HalVersion.UNKNOWN)) return -1;
7163 return hv.major * 100 + hv.minor;
7164 }
Malcolm Chendc8c10e2019-04-10 18:25:07 -07007165
7166 /**
Malcolm Chene5ad5792019-04-18 13:51:02 -07007167 * Return whether data is enabled for certain APN type. This will tell if framework will accept
7168 * corresponding network requests on a subId.
7169 *
7170 * Data is enabled if:
Malcolm Chendc8c10e2019-04-10 18:25:07 -07007171 * 1) user data is turned on, or
Malcolm Chene5ad5792019-04-18 13:51:02 -07007172 * 2) APN is un-metered for this subscription, or
7173 * 3) APN type is whitelisted. E.g. MMS is whitelisted if
7174 * {@link SubscriptionManager#setAlwaysAllowMmsData} is turned on.
7175 *
7176 * @return whether data is allowed for a apn type.
7177 *
7178 * @hide
Malcolm Chendc8c10e2019-04-10 18:25:07 -07007179 */
7180 @Override
Malcolm Chene5ad5792019-04-18 13:51:02 -07007181 public boolean isDataEnabledForApn(int apnType, int subId, String callingPackage) {
Malcolm Chendc8c10e2019-04-10 18:25:07 -07007182 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
Malcolm Chene5ad5792019-04-18 13:51:02 -07007183 mApp, subId, callingPackage, "isDataEnabledForApn")) {
7184 throw new SecurityException("Needs READ_PHONE_STATE for isDataEnabledForApn");
Malcolm Chendc8c10e2019-04-10 18:25:07 -07007185 }
7186
7187 // Now that all security checks passes, perform the operation as ourselves.
7188 final long identity = Binder.clearCallingIdentity();
7189 try {
7190 Phone phone = getPhone(subId);
7191 if (phone == null) return false;
7192
Jack Yu41407ee2019-05-13 16:54:09 -07007193 boolean isMetered = ApnSettingUtils.isMeteredApnType(apnType, phone);
Malcolm Chene5ad5792019-04-18 13:51:02 -07007194 return !isMetered || phone.getDataEnabledSettings().isDataEnabled(apnType);
7195 } finally {
7196 Binder.restoreCallingIdentity(identity);
7197 }
7198 }
7199
7200 @Override
Jack Yu41407ee2019-05-13 16:54:09 -07007201 public boolean isApnMetered(@ApnType int apnType, int subId) {
Malcolm Chene5ad5792019-04-18 13:51:02 -07007202 enforceReadPrivilegedPermission("isApnMetered");
7203
7204 // Now that all security checks passes, perform the operation as ourselves.
7205 final long identity = Binder.clearCallingIdentity();
7206 try {
7207 Phone phone = getPhone(subId);
7208 if (phone == null) return true; // By default return true.
7209
Jack Yu41407ee2019-05-13 16:54:09 -07007210 return ApnSettingUtils.isMeteredApnType(apnType, phone);
Malcolm Chendc8c10e2019-04-10 18:25:07 -07007211 } finally {
7212 Binder.restoreCallingIdentity(identity);
7213 }
7214 }
Brad Ebingera63db5f2019-04-23 16:31:13 -07007215
7216 @Override
7217 public void enqueueSmsPickResult(String callingPackage, IIntegerConsumer pendingSubIdResult) {
7218 SmsPermissions permissions = new SmsPermissions(getDefaultPhone(), mApp,
7219 (AppOpsManager) mApp.getSystemService(Context.APP_OPS_SERVICE));
7220 if (!permissions.checkCallingCanSendSms(callingPackage, "Sending message")) {
7221 throw new SecurityException("Requires SEND_SMS permission to perform this operation");
7222 }
7223 PickSmsSubscriptionActivity.addPendingResult(pendingSubIdResult);
7224 Intent intent = new Intent();
7225 intent.setClass(mApp, PickSmsSubscriptionActivity.class);
7226 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
7227 // Bring up choose default SMS subscription dialog right now
7228 intent.putExtra(PickSmsSubscriptionActivity.DIALOG_TYPE_KEY,
7229 PickSmsSubscriptionActivity.SMS_PICK_FOR_MESSAGE);
7230 mApp.startActivity(intent);
7231 }
chen xud5ca2d52019-05-28 15:20:57 -07007232
7233 @Override
7234 public String getMmsUAProfUrl(int subId) {
7235 //TODO investigate if this API should require proper permission check in R b/133791609
7236 final long identity = Binder.clearCallingIdentity();
7237 try {
7238 return SubscriptionManager.getResourcesForSubId(getDefaultPhone().getContext(), subId)
7239 .getString(com.android.internal.R.string.config_mms_user_agent_profile_url);
7240 } finally {
7241 Binder.restoreCallingIdentity(identity);
7242 }
7243 }
7244
7245 @Override
7246 public String getMmsUserAgent(int subId) {
7247 //TODO investigate if this API should require proper permission check in R b/133791609
7248 final long identity = Binder.clearCallingIdentity();
7249 try {
7250 return SubscriptionManager.getResourcesForSubId(getDefaultPhone().getContext(), subId)
7251 .getString(com.android.internal.R.string.config_mms_user_agent);
7252 } finally {
7253 Binder.restoreCallingIdentity(identity);
7254 }
7255 }
Jack Yub07d4972019-05-28 16:12:25 -07007256
7257 @Override
7258 public boolean setDataAllowedDuringVoiceCall(int subId, boolean allow) {
7259 enforceModifyPermission();
7260
7261 // Now that all security checks passes, perform the operation as ourselves.
7262 final long identity = Binder.clearCallingIdentity();
7263 try {
7264 Phone phone = getPhone(subId);
7265 if (phone == null) return false;
7266
7267 return phone.getDataEnabledSettings().setAllowDataDuringVoiceCall(allow);
7268 } finally {
7269 Binder.restoreCallingIdentity(identity);
7270 }
7271 }
7272
7273 @Override
7274 public boolean isDataAllowedInVoiceCall(int subId) {
7275 enforceReadPrivilegedPermission("isDataAllowedInVoiceCall");
7276
7277 // Now that all security checks passes, perform the operation as ourselves.
7278 final long identity = Binder.clearCallingIdentity();
7279 try {
7280 Phone phone = getPhone(subId);
7281 if (phone == null) return false;
7282
7283 return phone.getDataEnabledSettings().isDataAllowedInVoiceCall();
7284 } finally {
7285 Binder.restoreCallingIdentity(identity);
7286 }
7287 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07007288}